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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. static struct net_device * vlan_find ( struct net_device *trunk,
  178. unsigned int tag ) {
  179. struct net_device *netdev;
  180. struct vlan_device *vlan;
  181. for_each_netdev ( netdev ) {
  182. if ( netdev->op != &vlan_operations )
  183. continue;
  184. vlan = netdev->priv;
  185. if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
  186. return netdev;
  187. }
  188. return NULL;
  189. }
  190. /**
  191. * Process incoming VLAN packet
  192. *
  193. * @v iobuf I/O buffer
  194. * @v trunk Trunk network device
  195. * @v ll_dest Link-layer destination address
  196. * @v ll_source Link-layer source address
  197. * @v flags Packet flags
  198. * @ret rc Return status code
  199. */
  200. static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
  201. const void *ll_dest, const void *ll_source,
  202. unsigned int flags __unused ) {
  203. struct vlan_header *vlanhdr = iobuf->data;
  204. struct net_device *netdev;
  205. struct ll_protocol *ll_protocol;
  206. uint8_t ll_dest_copy[ETH_ALEN];
  207. uint8_t ll_source_copy[ETH_ALEN];
  208. uint16_t tag;
  209. int rc;
  210. /* Sanity check */
  211. if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
  212. DBGC ( trunk, "VLAN %s received underlength packet (%zd "
  213. "bytes)\n", trunk->name, iob_len ( iobuf ) );
  214. rc = -EINVAL;
  215. goto err_sanity;
  216. }
  217. /* Identify VLAN device */
  218. tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
  219. netdev = vlan_find ( trunk, tag );
  220. if ( ! netdev ) {
  221. DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
  222. "%d\n", trunk->name, tag );
  223. rc = -EPIPE;
  224. goto err_no_vlan;
  225. }
  226. /* Strip VLAN header and preserve original link-layer header fields */
  227. iob_pull ( iobuf, sizeof ( *vlanhdr ) );
  228. ll_protocol = trunk->ll_protocol;
  229. memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
  230. memcpy ( ll_source_copy, ll_source, ETH_ALEN );
  231. /* Reconstruct link-layer header for VLAN device */
  232. ll_protocol = netdev->ll_protocol;
  233. if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
  234. ll_source_copy,
  235. vlanhdr->net_proto ) ) != 0 ) {
  236. DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
  237. "header: %s\n", netdev->name, strerror ( rc ) );
  238. goto err_ll_push;
  239. }
  240. /* Enqueue packet on VLAN device */
  241. netdev_rx ( netdev, iob_disown ( iobuf ) );
  242. return 0;
  243. err_ll_push:
  244. err_no_vlan:
  245. err_sanity:
  246. free_iob ( iobuf );
  247. return rc;
  248. }
  249. /** VLAN protocol */
  250. struct net_protocol vlan_protocol __net_protocol = {
  251. .name = "VLAN",
  252. .net_proto = htons ( ETH_P_8021Q ),
  253. .rx = vlan_rx,
  254. };
  255. /**
  256. * Get the VLAN tag
  257. *
  258. * @v netdev Network device
  259. * @ret tag VLAN tag, or 0 if device is not a VLAN device
  260. */
  261. unsigned int vlan_tag ( struct net_device *netdev ) {
  262. struct vlan_device *vlan;
  263. if ( netdev->op == &vlan_operations ) {
  264. vlan = netdev->priv;
  265. return vlan->tag;
  266. } else {
  267. return 0;
  268. }
  269. }
  270. /**
  271. * Check if network device can be used as a VLAN trunk device
  272. *
  273. * @v trunk Trunk network device
  274. * @ret is_ok Trunk network device is usable
  275. *
  276. * VLAN devices will be created as Ethernet devices. (We cannot
  277. * simply clone the link layer of the trunk network device, because
  278. * this link layer may expect the network device structure to contain
  279. * some link-layer-private data.) The trunk network device must
  280. * therefore have a link layer that is in some sense 'compatible' with
  281. * Ethernet; specifically, it must have link-layer addresses that are
  282. * the same length as Ethernet link-layer addresses.
  283. *
  284. * As an additional check, and primarily to assist with the sanity of
  285. * the FCoE code, we refuse to allow nested VLANs.
  286. */
  287. int vlan_can_be_trunk ( struct net_device *trunk ) {
  288. return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
  289. ( trunk->op != &vlan_operations ) );
  290. }
  291. /**
  292. * Create VLAN device
  293. *
  294. * @v trunk Trunk network device
  295. * @v tag VLAN tag
  296. * @v priority Default VLAN priority
  297. * @ret rc Return status code
  298. */
  299. int vlan_create ( struct net_device *trunk, unsigned int tag,
  300. unsigned int priority ) {
  301. struct net_device *netdev;
  302. struct vlan_device *vlan;
  303. int rc;
  304. /* If VLAN already exists, just update the priority */
  305. if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
  306. vlan = netdev->priv;
  307. if ( priority != vlan->priority ) {
  308. DBGC ( netdev, "VLAN %s priority changed from %d to "
  309. "%d\n", netdev->name, vlan->priority, priority );
  310. }
  311. vlan->priority = priority;
  312. return 0;
  313. }
  314. /* Sanity checks */
  315. if ( ! vlan_can_be_trunk ( trunk ) ) {
  316. DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
  317. "device\n", trunk->name );
  318. rc = -ENOTTY;
  319. goto err_sanity;
  320. }
  321. if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
  322. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
  323. "%d\n", trunk->name, tag );
  324. rc = -EINVAL;
  325. goto err_sanity;
  326. }
  327. if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
  328. DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
  329. "priority %d\n", trunk->name, priority );
  330. rc = -EINVAL;
  331. goto err_sanity;
  332. }
  333. /* Allocate and initialise structure */
  334. netdev = alloc_etherdev ( sizeof ( *vlan ) );
  335. if ( ! netdev ) {
  336. rc = -ENOMEM;
  337. goto err_alloc_etherdev;
  338. }
  339. netdev_init ( netdev, &vlan_operations );
  340. netdev->dev = trunk->dev;
  341. memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
  342. vlan = netdev->priv;
  343. vlan->trunk = netdev_get ( trunk );
  344. vlan->tag = tag;
  345. vlan->priority = priority;
  346. /* Construct VLAN device name */
  347. snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
  348. trunk->name, vlan->tag );
  349. /* Mark device as not supporting interrupts, if applicable */
  350. if ( ! netdev_irq_supported ( trunk ) )
  351. netdev->state |= NETDEV_IRQ_UNSUPPORTED;
  352. /* Register VLAN device */
  353. if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
  354. DBGC ( netdev, "VLAN %s could not register: %s\n",
  355. netdev->name, strerror ( rc ) );
  356. goto err_register;
  357. }
  358. /* Synchronise with trunk device */
  359. vlan_sync ( netdev );
  360. DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
  361. netdev->name, vlan->tag, vlan->priority );
  362. return 0;
  363. unregister_netdev ( netdev );
  364. err_register:
  365. netdev_nullify ( netdev );
  366. netdev_put ( netdev );
  367. netdev_put ( trunk );
  368. err_alloc_etherdev:
  369. err_sanity:
  370. return rc;
  371. }
  372. /**
  373. * Destroy VLAN device
  374. *
  375. * @v netdev Network device
  376. * @ret rc Return status code
  377. */
  378. int vlan_destroy ( struct net_device *netdev ) {
  379. struct vlan_device *vlan = netdev->priv;
  380. struct net_device *trunk;
  381. /* Sanity check */
  382. if ( netdev->op != &vlan_operations ) {
  383. DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
  384. netdev->name );
  385. return -ENOTTY;
  386. }
  387. DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
  388. /* Remove VLAN device */
  389. unregister_netdev ( netdev );
  390. trunk = vlan->trunk;
  391. netdev_nullify ( netdev );
  392. netdev_put ( netdev );
  393. netdev_put ( trunk );
  394. return 0;
  395. }
  396. /**
  397. * Handle trunk network device link state change
  398. *
  399. * @v trunk Trunk network device
  400. */
  401. static void vlan_notify ( struct net_device *trunk ) {
  402. struct net_device *netdev;
  403. struct vlan_device *vlan;
  404. for_each_netdev ( netdev ) {
  405. if ( netdev->op != &vlan_operations )
  406. continue;
  407. vlan = netdev->priv;
  408. if ( vlan->trunk == trunk )
  409. vlan_sync ( netdev );
  410. }
  411. }
  412. /**
  413. * Destroy first VLAN device for a given trunk
  414. *
  415. * @v trunk Trunk network device
  416. * @ret found A VLAN device was found
  417. */
  418. static int vlan_remove_first ( struct net_device *trunk ) {
  419. struct net_device *netdev;
  420. struct vlan_device *vlan;
  421. for_each_netdev ( netdev ) {
  422. if ( netdev->op != &vlan_operations )
  423. continue;
  424. vlan = netdev->priv;
  425. if ( vlan->trunk == trunk ) {
  426. vlan_destroy ( netdev );
  427. return 1;
  428. }
  429. }
  430. return 0;
  431. }
  432. /**
  433. * Destroy all VLAN devices for a given trunk
  434. *
  435. * @v trunk Trunk network device
  436. */
  437. static void vlan_remove ( struct net_device *trunk ) {
  438. /* Remove all VLAN devices attached to this trunk, safe
  439. * against arbitrary net device removal.
  440. */
  441. while ( vlan_remove_first ( trunk ) ) {}
  442. }
  443. /** VLAN driver */
  444. struct net_driver vlan_driver __net_driver = {
  445. .name = "VLAN",
  446. .notify = vlan_notify,
  447. .remove = vlan_remove,
  448. };
  449. /**
  450. * Add VLAN tag-stripped packet to receive queue
  451. *
  452. * @v netdev Network device
  453. * @v tag VLAN tag, or zero
  454. * @v iobuf I/O buffer
  455. */
  456. void vlan_netdev_rx ( struct net_device *netdev, unsigned int tag,
  457. struct io_buffer *iobuf ) {
  458. struct net_device *vlan;
  459. /* Identify VLAN device, if applicable */
  460. if ( tag ) {
  461. if ( ( vlan = vlan_find ( netdev, tag ) ) == NULL ) {
  462. netdev_rx_err ( netdev, iobuf, -ENODEV );
  463. return;
  464. }
  465. netdev = vlan;
  466. }
  467. /* Hand off to network device */
  468. netdev_rx ( netdev, iobuf );
  469. }
  470. /**
  471. * Discard received VLAN tag-stripped packet
  472. *
  473. * @v netdev Network device
  474. * @v tag VLAN tag, or zero
  475. * @v iobuf I/O buffer, or NULL
  476. * @v rc Packet status code
  477. */
  478. void vlan_netdev_rx_err ( struct net_device *netdev, unsigned int tag,
  479. struct io_buffer *iobuf, int rc ) {
  480. struct net_device *vlan;
  481. /* Identify VLAN device, if applicable */
  482. if ( tag && ( ( vlan = vlan_find ( netdev, tag ) ) != NULL ) )
  483. netdev = vlan;
  484. /* Hand off to network device */
  485. netdev_rx_err ( netdev, iobuf, rc );
  486. }