You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vlan.c 12KB

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