選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

vlan.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. * Get the VLAN tag
  252. *
  253. * @v netdev Network device
  254. * @ret tag VLAN tag, or 0 if device is not a VLAN device
  255. */
  256. unsigned int vlan_tag ( struct net_device *netdev ) {
  257. struct vlan_device *vlan;
  258. if ( netdev->op == &vlan_operations ) {
  259. vlan = netdev->priv;
  260. return vlan->tag;
  261. } else {
  262. return 0;
  263. }
  264. }
  265. /**
  266. * Check if network device can be used as a VLAN trunk device
  267. *
  268. * @v trunk Trunk network device
  269. * @ret is_ok Trunk network device is usable
  270. *
  271. * VLAN devices will be created as Ethernet devices. (We cannot
  272. * simply clone the link layer of the trunk network device, because
  273. * this link layer may expect the network device structure to contain
  274. * some link-layer-private data.) The trunk network device must
  275. * therefore have a link layer that is in some sense 'compatible' with
  276. * Ethernet; specifically, it must have link-layer addresses that are
  277. * the same length as Ethernet link-layer addresses.
  278. *
  279. * As an additional check, and primarily to assist with the sanity of
  280. * the FCoE code, we refuse to allow nested VLANs.
  281. */
  282. int vlan_can_be_trunk ( struct net_device *trunk ) {
  283. return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
  284. ( trunk->op != &vlan_operations ) );
  285. }
  286. /**
  287. * Create VLAN device
  288. *
  289. * @v trunk Trunk network device
  290. * @v tag VLAN tag
  291. * @v priority Default VLAN priority
  292. * @ret rc Return status code
  293. */
  294. int vlan_create ( struct net_device *trunk, unsigned int tag,
  295. unsigned int priority ) {
  296. struct net_device *netdev;
  297. struct vlan_device *vlan;
  298. int rc;
  299. /* If VLAN already exists, just update the priority */
  300. if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
  301. vlan = netdev->priv;
  302. if ( priority != vlan->priority ) {
  303. DBGC ( netdev, "VLAN %s priority changed from %d to "
  304. "%d\n", netdev->name, vlan->priority, priority );
  305. }
  306. vlan->priority = priority;
  307. return 0;
  308. }
  309. /* Sanity checks */
  310. if ( ! vlan_can_be_trunk ( trunk ) ) {
  311. DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
  312. "device\n", trunk->name );
  313. rc = -ENOTTY;
  314. goto err_sanity;
  315. }
  316. if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
  317. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
  318. "%d\n", trunk->name, tag );
  319. rc = -EINVAL;
  320. goto err_sanity;
  321. }
  322. if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
  323. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
  324. "priority %d\n", trunk->name, priority );
  325. rc = -EINVAL;
  326. goto err_sanity;
  327. }
  328. /* Allocate and initialise structure */
  329. netdev = alloc_etherdev ( sizeof ( *vlan ) );
  330. if ( ! netdev ) {
  331. rc = -ENOMEM;
  332. goto err_alloc_etherdev;
  333. }
  334. netdev_init ( netdev, &vlan_operations );
  335. netdev->dev = trunk->dev;
  336. memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
  337. vlan = netdev->priv;
  338. vlan->trunk = netdev_get ( trunk );
  339. vlan->tag = tag;
  340. vlan->priority = priority;
  341. /* Construct VLAN device name */
  342. snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
  343. trunk->name, vlan->tag );
  344. /* Register VLAN device */
  345. if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
  346. DBGC ( netdev, "VLAN %s could not register: %s\n",
  347. netdev->name, strerror ( rc ) );
  348. goto err_register;
  349. }
  350. /* Synchronise with trunk device */
  351. vlan_sync ( netdev );
  352. DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
  353. netdev->name, vlan->tag, vlan->priority );
  354. return 0;
  355. unregister_netdev ( netdev );
  356. err_register:
  357. netdev_nullify ( netdev );
  358. netdev_put ( netdev );
  359. netdev_put ( trunk );
  360. err_alloc_etherdev:
  361. err_sanity:
  362. return rc;
  363. }
  364. /**
  365. * Destroy VLAN device
  366. *
  367. * @v netdev Network device
  368. * @ret rc Return status code
  369. */
  370. int vlan_destroy ( struct net_device *netdev ) {
  371. struct vlan_device *vlan = netdev->priv;
  372. struct net_device *trunk;
  373. /* Sanity check */
  374. if ( netdev->op != &vlan_operations ) {
  375. DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
  376. netdev->name );
  377. return -ENOTTY;
  378. }
  379. DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
  380. /* Remove VLAN device */
  381. unregister_netdev ( netdev );
  382. trunk = vlan->trunk;
  383. netdev_nullify ( netdev );
  384. netdev_put ( netdev );
  385. netdev_put ( trunk );
  386. return 0;
  387. }
  388. /**
  389. * Handle trunk network device link state change
  390. *
  391. * @v trunk Trunk network device
  392. */
  393. static void vlan_notify ( struct net_device *trunk ) {
  394. struct net_device *netdev;
  395. struct vlan_device *vlan;
  396. for_each_netdev ( netdev ) {
  397. if ( netdev->op != &vlan_operations )
  398. continue;
  399. vlan = netdev->priv;
  400. if ( vlan->trunk == trunk )
  401. vlan_sync ( netdev );
  402. }
  403. }
  404. /**
  405. * Destroy first VLAN device for a given trunk
  406. *
  407. * @v trunk Trunk network device
  408. * @ret found A VLAN device was found
  409. */
  410. static int vlan_remove_first ( struct net_device *trunk ) {
  411. struct net_device *netdev;
  412. struct vlan_device *vlan;
  413. for_each_netdev ( netdev ) {
  414. if ( netdev->op != &vlan_operations )
  415. continue;
  416. vlan = netdev->priv;
  417. if ( vlan->trunk == trunk ) {
  418. vlan_destroy ( netdev );
  419. return 1;
  420. }
  421. }
  422. return 0;
  423. }
  424. /**
  425. * Destroy all VLAN devices for a given trunk
  426. *
  427. * @v trunk Trunk network device
  428. */
  429. static void vlan_remove ( struct net_device *trunk ) {
  430. /* Remove all VLAN devices attached to this trunk, safe
  431. * against arbitrary net device removal.
  432. */
  433. while ( vlan_remove_first ( trunk ) ) {}
  434. }
  435. /** VLAN driver */
  436. struct net_driver vlan_driver __net_driver = {
  437. .name = "VLAN",
  438. .notify = vlan_notify,
  439. .remove = vlan_remove,
  440. };