Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

vlan.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <byteswap.h>
  29. #include <ipxe/features.h>
  30. #include <ipxe/if_ether.h>
  31. #include <ipxe/ethernet.h>
  32. #include <ipxe/netdevice.h>
  33. #include <ipxe/iobuf.h>
  34. #include <ipxe/vlan.h>
  35. /** @file
  36. *
  37. * Virtual LANs
  38. *
  39. */
  40. FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
  41. struct net_protocol vlan_protocol __net_protocol;
  42. /** VLAN device private data */
  43. struct vlan_device {
  44. /** Trunk network device */
  45. struct net_device *trunk;
  46. /** VLAN tag */
  47. unsigned int tag;
  48. /** Default priority */
  49. unsigned int priority;
  50. };
  51. /**
  52. * Open VLAN device
  53. *
  54. * @v netdev Network device
  55. * @ret rc Return status code
  56. */
  57. static int vlan_open ( struct net_device *netdev ) {
  58. struct vlan_device *vlan = netdev->priv;
  59. return netdev_open ( vlan->trunk );
  60. }
  61. /**
  62. * Close VLAN device
  63. *
  64. * @v netdev Network device
  65. */
  66. static void vlan_close ( struct net_device *netdev ) {
  67. struct vlan_device *vlan = netdev->priv;
  68. netdev_close ( vlan->trunk );
  69. }
  70. /**
  71. * Transmit packet on VLAN device
  72. *
  73. * @v netdev Network device
  74. * @v iobuf I/O buffer
  75. * @ret rc Return status code
  76. */
  77. static int vlan_transmit ( struct net_device *netdev,
  78. struct io_buffer *iobuf ) {
  79. struct vlan_device *vlan = netdev->priv;
  80. struct net_device *trunk = vlan->trunk;
  81. struct ll_protocol *ll_protocol;
  82. struct vlan_header *vlanhdr;
  83. uint8_t ll_dest_copy[ETH_ALEN];
  84. uint8_t ll_source_copy[ETH_ALEN];
  85. const void *ll_dest;
  86. const void *ll_source;
  87. uint16_t net_proto;
  88. unsigned int flags;
  89. int rc;
  90. /* Strip link-layer header and preserve link-layer header fields */
  91. ll_protocol = netdev->ll_protocol;
  92. if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
  93. &net_proto, &flags ) ) != 0 ) {
  94. DBGC ( netdev, "VLAN %s could not parse link-layer header: "
  95. "%s\n", netdev->name, strerror ( rc ) );
  96. return rc;
  97. }
  98. memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
  99. memcpy ( ll_source_copy, ll_source, ETH_ALEN );
  100. /* Construct VLAN header */
  101. vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
  102. vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
  103. vlanhdr->net_proto = net_proto;
  104. /* Reclaim I/O buffer from VLAN device's TX queue */
  105. list_del ( &iobuf->list );
  106. /* Transmit packet on trunk device */
  107. if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
  108. ll_dest_copy, ll_source_copy ) ) != 0 ) {
  109. DBGC ( netdev, "VLAN %s could not transmit: %s\n",
  110. netdev->name, strerror ( rc ) );
  111. /* Cannot return an error status, since that would
  112. * cause the I/O buffer to be double-freed.
  113. */
  114. return 0;
  115. }
  116. return 0;
  117. }
  118. /**
  119. * Poll VLAN device
  120. *
  121. * @v netdev Network device
  122. */
  123. static void vlan_poll ( struct net_device *netdev ) {
  124. struct vlan_device *vlan = netdev->priv;
  125. /* Poll trunk device */
  126. netdev_poll ( vlan->trunk );
  127. }
  128. /**
  129. * Enable/disable interrupts on VLAN device
  130. *
  131. * @v netdev Network device
  132. * @v enable Interrupts should be enabled
  133. */
  134. static void vlan_irq ( struct net_device *netdev, int enable ) {
  135. struct vlan_device *vlan = netdev->priv;
  136. /* Enable/disable interrupts on trunk device. This is not at
  137. * all robust, but there is no sensible course of action
  138. * available.
  139. */
  140. netdev_irq ( vlan->trunk, enable );
  141. }
  142. /** VLAN device operations */
  143. static struct net_device_operations vlan_operations = {
  144. .open = vlan_open,
  145. .close = vlan_close,
  146. .transmit = vlan_transmit,
  147. .poll = vlan_poll,
  148. .irq = vlan_irq,
  149. };
  150. /**
  151. * Synchronise VLAN device
  152. *
  153. * @v netdev Network device
  154. */
  155. static void vlan_sync ( struct net_device *netdev ) {
  156. struct vlan_device *vlan = netdev->priv;
  157. struct net_device *trunk = vlan->trunk;
  158. /* Synchronise link status */
  159. if ( netdev->link_rc != trunk->link_rc )
  160. netdev_link_err ( netdev, trunk->link_rc );
  161. /* Synchronise open/closed status */
  162. if ( netdev_is_open ( trunk ) ) {
  163. if ( ! netdev_is_open ( netdev ) )
  164. netdev_open ( netdev );
  165. } else {
  166. if ( netdev_is_open ( netdev ) )
  167. netdev_close ( netdev );
  168. }
  169. }
  170. /**
  171. * Identify VLAN device
  172. *
  173. * @v trunk Trunk network device
  174. * @v tag VLAN tag
  175. * @ret netdev VLAN device, if any
  176. */
  177. struct net_device * vlan_find ( struct net_device *trunk, unsigned int tag ) {
  178. struct net_device *netdev;
  179. struct vlan_device *vlan;
  180. for_each_netdev ( netdev ) {
  181. if ( netdev->op != &vlan_operations )
  182. continue;
  183. vlan = netdev->priv;
  184. if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
  185. return netdev;
  186. }
  187. return NULL;
  188. }
  189. /**
  190. * Process incoming VLAN packet
  191. *
  192. * @v iobuf I/O buffer
  193. * @v trunk Trunk network device
  194. * @v ll_dest Link-layer destination address
  195. * @v ll_source Link-layer source address
  196. * @v flags Packet flags
  197. * @ret rc Return status code
  198. */
  199. static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
  200. const void *ll_dest, const void *ll_source,
  201. unsigned int flags __unused ) {
  202. struct vlan_header *vlanhdr = iobuf->data;
  203. struct net_device *netdev;
  204. struct ll_protocol *ll_protocol;
  205. uint8_t ll_dest_copy[ETH_ALEN];
  206. uint8_t ll_source_copy[ETH_ALEN];
  207. uint16_t tag;
  208. int rc;
  209. /* Sanity check */
  210. if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
  211. DBGC ( trunk, "VLAN %s received underlength packet (%zd "
  212. "bytes)\n", trunk->name, iob_len ( iobuf ) );
  213. rc = -EINVAL;
  214. goto err_sanity;
  215. }
  216. /* Identify VLAN device */
  217. tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
  218. netdev = vlan_find ( trunk, tag );
  219. if ( ! netdev ) {
  220. DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
  221. "%d\n", trunk->name, tag );
  222. rc = -EPIPE;
  223. goto err_no_vlan;
  224. }
  225. /* Strip VLAN header and preserve original link-layer header fields */
  226. iob_pull ( iobuf, sizeof ( *vlanhdr ) );
  227. ll_protocol = trunk->ll_protocol;
  228. memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
  229. memcpy ( ll_source_copy, ll_source, ETH_ALEN );
  230. /* Reconstruct link-layer header for VLAN device */
  231. ll_protocol = netdev->ll_protocol;
  232. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
  233. ll_source_copy,
  234. vlanhdr->net_proto ) ) != 0 ) {
  235. DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
  236. "header: %s\n", netdev->name, strerror ( rc ) );
  237. goto err_ll_push;
  238. }
  239. /* Enqueue packet on VLAN device */
  240. netdev_rx ( netdev, iob_disown ( iobuf ) );
  241. return 0;
  242. err_ll_push:
  243. err_no_vlan:
  244. err_sanity:
  245. free_iob ( iobuf );
  246. return rc;
  247. }
  248. /** VLAN protocol */
  249. struct net_protocol vlan_protocol __net_protocol = {
  250. .name = "VLAN",
  251. .net_proto = htons ( ETH_P_8021Q ),
  252. .rx = vlan_rx,
  253. };
  254. /**
  255. * Get the VLAN tag
  256. *
  257. * @v netdev Network device
  258. * @ret tag VLAN tag, or 0 if device is not a VLAN device
  259. */
  260. unsigned int vlan_tag ( struct net_device *netdev ) {
  261. struct vlan_device *vlan;
  262. if ( netdev->op == &vlan_operations ) {
  263. vlan = netdev->priv;
  264. return vlan->tag;
  265. } else {
  266. return 0;
  267. }
  268. }
  269. /**
  270. * Check if network device can be used as a VLAN trunk device
  271. *
  272. * @v trunk Trunk network device
  273. * @ret is_ok Trunk network device is usable
  274. *
  275. * VLAN devices will be created as Ethernet devices. (We cannot
  276. * simply clone the link layer of the trunk network device, because
  277. * this link layer may expect the network device structure to contain
  278. * some link-layer-private data.) The trunk network device must
  279. * therefore have a link layer that is in some sense 'compatible' with
  280. * Ethernet; specifically, it must have link-layer addresses that are
  281. * the same length as Ethernet link-layer addresses.
  282. *
  283. * As an additional check, and primarily to assist with the sanity of
  284. * the FCoE code, we refuse to allow nested VLANs.
  285. */
  286. int vlan_can_be_trunk ( struct net_device *trunk ) {
  287. return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
  288. ( trunk->op != &vlan_operations ) );
  289. }
  290. /**
  291. * Create VLAN device
  292. *
  293. * @v trunk Trunk network device
  294. * @v tag VLAN tag
  295. * @v priority Default VLAN priority
  296. * @ret rc Return status code
  297. */
  298. int vlan_create ( struct net_device *trunk, unsigned int tag,
  299. unsigned int priority ) {
  300. struct net_device *netdev;
  301. struct vlan_device *vlan;
  302. int rc;
  303. /* If VLAN already exists, just update the priority */
  304. if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
  305. vlan = netdev->priv;
  306. if ( priority != vlan->priority ) {
  307. DBGC ( netdev, "VLAN %s priority changed from %d to "
  308. "%d\n", netdev->name, vlan->priority, priority );
  309. }
  310. vlan->priority = priority;
  311. return 0;
  312. }
  313. /* Sanity checks */
  314. if ( ! vlan_can_be_trunk ( trunk ) ) {
  315. DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
  316. "device\n", trunk->name );
  317. rc = -ENOTTY;
  318. goto err_sanity;
  319. }
  320. if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
  321. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
  322. "%d\n", trunk->name, tag );
  323. rc = -EINVAL;
  324. goto err_sanity;
  325. }
  326. if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
  327. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
  328. "priority %d\n", trunk->name, priority );
  329. rc = -EINVAL;
  330. goto err_sanity;
  331. }
  332. /* Allocate and initialise structure */
  333. netdev = alloc_etherdev ( sizeof ( *vlan ) );
  334. if ( ! netdev ) {
  335. rc = -ENOMEM;
  336. goto err_alloc_etherdev;
  337. }
  338. netdev_init ( netdev, &vlan_operations );
  339. netdev->dev = trunk->dev;
  340. memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
  341. vlan = netdev->priv;
  342. vlan->trunk = netdev_get ( trunk );
  343. vlan->tag = tag;
  344. vlan->priority = priority;
  345. /* Construct VLAN device name */
  346. snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
  347. trunk->name, vlan->tag );
  348. /* Mark device as not supporting interrupts, if applicable */
  349. if ( ! netdev_irq_supported ( trunk ) )
  350. netdev->state |= NETDEV_IRQ_UNSUPPORTED;
  351. /* Register VLAN device */
  352. if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
  353. DBGC ( netdev, "VLAN %s could not register: %s\n",
  354. netdev->name, strerror ( rc ) );
  355. goto err_register;
  356. }
  357. /* Synchronise with trunk device */
  358. vlan_sync ( netdev );
  359. DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
  360. netdev->name, vlan->tag, vlan->priority );
  361. return 0;
  362. unregister_netdev ( netdev );
  363. err_register:
  364. netdev_nullify ( netdev );
  365. netdev_put ( netdev );
  366. netdev_put ( trunk );
  367. err_alloc_etherdev:
  368. err_sanity:
  369. return rc;
  370. }
  371. /**
  372. * Destroy VLAN device
  373. *
  374. * @v netdev Network device
  375. * @ret rc Return status code
  376. */
  377. int vlan_destroy ( struct net_device *netdev ) {
  378. struct vlan_device *vlan = netdev->priv;
  379. struct net_device *trunk;
  380. /* Sanity check */
  381. if ( netdev->op != &vlan_operations ) {
  382. DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
  383. netdev->name );
  384. return -ENOTTY;
  385. }
  386. DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
  387. /* Remove VLAN device */
  388. unregister_netdev ( netdev );
  389. trunk = vlan->trunk;
  390. netdev_nullify ( netdev );
  391. netdev_put ( netdev );
  392. netdev_put ( trunk );
  393. return 0;
  394. }
  395. /**
  396. * Handle trunk network device link state change
  397. *
  398. * @v trunk Trunk network device
  399. */
  400. static void vlan_notify ( struct net_device *trunk ) {
  401. struct net_device *netdev;
  402. struct vlan_device *vlan;
  403. for_each_netdev ( netdev ) {
  404. if ( netdev->op != &vlan_operations )
  405. continue;
  406. vlan = netdev->priv;
  407. if ( vlan->trunk == trunk )
  408. vlan_sync ( netdev );
  409. }
  410. }
  411. /**
  412. * Destroy first VLAN device for a given trunk
  413. *
  414. * @v trunk Trunk network device
  415. * @ret found A VLAN device was found
  416. */
  417. static int vlan_remove_first ( struct net_device *trunk ) {
  418. struct net_device *netdev;
  419. struct vlan_device *vlan;
  420. for_each_netdev ( netdev ) {
  421. if ( netdev->op != &vlan_operations )
  422. continue;
  423. vlan = netdev->priv;
  424. if ( vlan->trunk == trunk ) {
  425. vlan_destroy ( netdev );
  426. return 1;
  427. }
  428. }
  429. return 0;
  430. }
  431. /**
  432. * Destroy all VLAN devices for a given trunk
  433. *
  434. * @v trunk Trunk network device
  435. */
  436. static void vlan_remove ( struct net_device *trunk ) {
  437. /* Remove all VLAN devices attached to this trunk, safe
  438. * against arbitrary net device removal.
  439. */
  440. while ( vlan_remove_first ( trunk ) ) {}
  441. }
  442. /** VLAN driver */
  443. struct net_driver vlan_driver __net_driver = {
  444. .name = "VLAN",
  445. .notify = vlan_notify,
  446. .remove = vlan_remove,
  447. };