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

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