2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 #include <ipxe/features.h>
32 #include <ipxe/if_ether.h>
33 #include <ipxe/ethernet.h>
34 #include <ipxe/netdevice.h>
35 #include <ipxe/iobuf.h>
36 #include <ipxe/vlan.h>
44 FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
46 struct net_protocol vlan_protocol __net_protocol;
48 /** VLAN device private data */
50 /** Trunk network device */
51 struct net_device *trunk;
54 /** Default priority */
55 unsigned int priority;
61 * @v netdev Network device
62 * @ret rc Return status code
64 static int vlan_open ( struct net_device *netdev ) {
65 struct vlan_device *vlan = netdev->priv;
67 return netdev_open ( vlan->trunk );
73 * @v netdev Network device
75 static void vlan_close ( struct net_device *netdev ) {
76 struct vlan_device *vlan = netdev->priv;
78 netdev_close ( vlan->trunk );
82 * Transmit packet on VLAN device
84 * @v netdev Network device
86 * @ret rc Return status code
88 static int vlan_transmit ( struct net_device *netdev,
89 struct io_buffer *iobuf ) {
90 struct vlan_device *vlan = netdev->priv;
91 struct net_device *trunk = vlan->trunk;
92 struct ll_protocol *ll_protocol;
93 struct vlan_header *vlanhdr;
94 uint8_t ll_dest_copy[ETH_ALEN];
95 uint8_t ll_source_copy[ETH_ALEN];
97 const void *ll_source;
102 /* Strip link-layer header and preserve link-layer header fields */
103 ll_protocol = netdev->ll_protocol;
104 if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
105 &net_proto, &flags ) ) != 0 ) {
106 DBGC ( netdev, "VLAN %s could not parse link-layer header: "
107 "%s\n", netdev->name, strerror ( rc ) );
110 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
111 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
113 /* Construct VLAN header */
114 vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
115 vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
116 vlanhdr->net_proto = net_proto;
118 /* Reclaim I/O buffer from VLAN device's TX queue */
119 list_del ( &iobuf->list );
121 /* Transmit packet on trunk device */
122 if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
123 ll_dest_copy, ll_source_copy ) ) != 0 ) {
124 DBGC ( netdev, "VLAN %s could not transmit: %s\n",
125 netdev->name, strerror ( rc ) );
126 /* Cannot return an error status, since that would
127 * cause the I/O buffer to be double-freed.
138 * @v netdev Network device
140 static void vlan_poll ( struct net_device *netdev ) {
141 struct vlan_device *vlan = netdev->priv;
143 /* Poll trunk device */
144 netdev_poll ( vlan->trunk );
148 * Enable/disable interrupts on VLAN device
150 * @v netdev Network device
151 * @v enable Interrupts should be enabled
153 static void vlan_irq ( struct net_device *netdev, int enable ) {
154 struct vlan_device *vlan = netdev->priv;
156 /* Enable/disable interrupts on trunk device. This is not at
157 * all robust, but there is no sensible course of action
160 netdev_irq ( vlan->trunk, enable );
163 /** VLAN device operations */
164 static struct net_device_operations vlan_operations = {
167 .transmit = vlan_transmit,
173 * Synchronise VLAN device
175 * @v netdev Network device
177 static void vlan_sync ( struct net_device *netdev ) {
178 struct vlan_device *vlan = netdev->priv;
179 struct net_device *trunk = vlan->trunk;
181 /* Synchronise link status */
182 if ( netdev->link_rc != trunk->link_rc )
183 netdev_link_err ( netdev, trunk->link_rc );
185 /* Synchronise open/closed status */
186 if ( netdev_is_open ( trunk ) ) {
187 if ( ! netdev_is_open ( netdev ) )
188 netdev_open ( netdev );
190 if ( netdev_is_open ( netdev ) )
191 netdev_close ( netdev );
196 * Identify VLAN device
198 * @v trunk Trunk network device
200 * @ret netdev VLAN device, if any
202 struct net_device * vlan_find ( struct net_device *trunk, unsigned int tag ) {
203 struct net_device *netdev;
204 struct vlan_device *vlan;
206 for_each_netdev ( netdev ) {
207 if ( netdev->op != &vlan_operations )
210 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
217 * Process incoming VLAN packet
219 * @v iobuf I/O buffer
220 * @v trunk Trunk network device
221 * @v ll_dest Link-layer destination address
222 * @v ll_source Link-layer source address
223 * @v flags Packet flags
224 * @ret rc Return status code
226 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
227 const void *ll_dest, const void *ll_source,
228 unsigned int flags __unused ) {
229 struct vlan_header *vlanhdr = iobuf->data;
230 struct net_device *netdev;
231 struct ll_protocol *ll_protocol;
232 uint8_t ll_dest_copy[ETH_ALEN];
233 uint8_t ll_source_copy[ETH_ALEN];
238 if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
239 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
240 "bytes)\n", trunk->name, iob_len ( iobuf ) );
245 /* Identify VLAN device */
246 tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
247 netdev = vlan_find ( trunk, tag );
249 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
250 "%d\n", trunk->name, tag );
255 /* Strip VLAN header and preserve original link-layer header fields */
256 iob_pull ( iobuf, sizeof ( *vlanhdr ) );
257 ll_protocol = trunk->ll_protocol;
258 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
259 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
261 /* Reconstruct link-layer header for VLAN device */
262 ll_protocol = netdev->ll_protocol;
263 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
265 vlanhdr->net_proto ) ) != 0 ) {
266 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
267 "header: %s\n", netdev->name, strerror ( rc ) );
271 /* Enqueue packet on VLAN device */
272 netdev_rx ( netdev, iob_disown ( iobuf ) );
283 struct net_protocol vlan_protocol __net_protocol = {
285 .net_proto = htons ( ETH_P_8021Q ),
292 * @v netdev Network device
293 * @ret tag VLAN tag, or 0 if device is not a VLAN device
295 unsigned int vlan_tag ( struct net_device *netdev ) {
296 struct vlan_device *vlan;
298 if ( netdev->op == &vlan_operations ) {
307 * Check if network device can be used as a VLAN trunk device
309 * @v trunk Trunk network device
310 * @ret is_ok Trunk network device is usable
312 * VLAN devices will be created as Ethernet devices. (We cannot
313 * simply clone the link layer of the trunk network device, because
314 * this link layer may expect the network device structure to contain
315 * some link-layer-private data.) The trunk network device must
316 * therefore have a link layer that is in some sense 'compatible' with
317 * Ethernet; specifically, it must have link-layer addresses that are
318 * the same length as Ethernet link-layer addresses.
320 * As an additional check, and primarily to assist with the sanity of
321 * the FCoE code, we refuse to allow nested VLANs.
323 int vlan_can_be_trunk ( struct net_device *trunk ) {
325 return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
326 ( trunk->op != &vlan_operations ) );
332 * @v trunk Trunk network device
334 * @v priority Default VLAN priority
335 * @ret rc Return status code
337 int vlan_create ( struct net_device *trunk, unsigned int tag,
338 unsigned int priority ) {
339 struct net_device *netdev;
340 struct vlan_device *vlan;
343 /* If VLAN already exists, just update the priority */
344 if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
346 if ( priority != vlan->priority ) {
347 DBGC ( netdev, "VLAN %s priority changed from %d to "
348 "%d\n", netdev->name, vlan->priority, priority );
350 vlan->priority = priority;
355 if ( ! vlan_can_be_trunk ( trunk ) ) {
356 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
357 "device\n", trunk->name );
361 if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
362 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
363 "%d\n", trunk->name, tag );
367 if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
368 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
369 "priority %d\n", trunk->name, priority );
374 /* Allocate and initialise structure */
375 netdev = alloc_etherdev ( sizeof ( *vlan ) );
378 goto err_alloc_etherdev;
380 netdev_init ( netdev, &vlan_operations );
381 netdev->dev = trunk->dev;
382 memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
384 vlan->trunk = netdev_get ( trunk );
386 vlan->priority = priority;
388 /* Construct VLAN device name */
389 snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
390 trunk->name, vlan->tag );
392 /* Mark device as not supporting interrupts, if applicable */
393 if ( ! netdev_irq_supported ( trunk ) )
394 netdev->state |= NETDEV_IRQ_UNSUPPORTED;
396 /* Register VLAN device */
397 if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
398 DBGC ( netdev, "VLAN %s could not register: %s\n",
399 netdev->name, strerror ( rc ) );
403 /* Synchronise with trunk device */
404 vlan_sync ( netdev );
406 DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
407 netdev->name, vlan->tag, vlan->priority );
411 unregister_netdev ( netdev );
413 netdev_nullify ( netdev );
414 netdev_put ( netdev );
415 netdev_put ( trunk );
422 * Destroy VLAN device
424 * @v netdev Network device
425 * @ret rc Return status code
427 int vlan_destroy ( struct net_device *netdev ) {
428 struct vlan_device *vlan = netdev->priv;
429 struct net_device *trunk;
432 if ( netdev->op != &vlan_operations ) {
433 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
438 DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
440 /* Remove VLAN device */
441 unregister_netdev ( netdev );
443 netdev_nullify ( netdev );
444 netdev_put ( netdev );
445 netdev_put ( trunk );
451 * Handle trunk network device link state change
453 * @v trunk Trunk network device
455 static void vlan_notify ( struct net_device *trunk ) {
456 struct net_device *netdev;
457 struct vlan_device *vlan;
459 for_each_netdev ( netdev ) {
460 if ( netdev->op != &vlan_operations )
463 if ( vlan->trunk == trunk )
464 vlan_sync ( netdev );
469 * Destroy first VLAN device for a given trunk
471 * @v trunk Trunk network device
472 * @ret found A VLAN device was found
474 static int vlan_remove_first ( struct net_device *trunk ) {
475 struct net_device *netdev;
476 struct vlan_device *vlan;
478 for_each_netdev ( netdev ) {
479 if ( netdev->op != &vlan_operations )
482 if ( vlan->trunk == trunk ) {
483 vlan_destroy ( netdev );
491 * Destroy all VLAN devices for a given trunk
493 * @v trunk Trunk network device
495 static void vlan_remove ( struct net_device *trunk ) {
497 /* Remove all VLAN devices attached to this trunk, safe
498 * against arbitrary net device removal.
500 while ( vlan_remove_first ( trunk ) ) {}
504 struct net_driver vlan_driver __net_driver = {
506 .notify = vlan_notify,
507 .remove = vlan_remove,