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

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