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
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/features.h>
28 #include <ipxe/if_ether.h>
29 #include <ipxe/ethernet.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/iobuf.h>
32 #include <ipxe/vlan.h>
40 FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
42 struct net_protocol vlan_protocol __net_protocol;
44 /** VLAN device private data */
46 /** Trunk network device */
47 struct net_device *trunk;
50 /** Default priority */
51 unsigned int priority;
57 * @v netdev Network device
58 * @ret rc Return status code
60 static int vlan_open ( struct net_device *netdev ) {
61 struct vlan_device *vlan = netdev->priv;
63 return netdev_open ( vlan->trunk );
69 * @v netdev Network device
71 static void vlan_close ( struct net_device *netdev ) {
72 struct vlan_device *vlan = netdev->priv;
74 netdev_close ( vlan->trunk );
78 * Transmit packet on VLAN device
80 * @v netdev Network device
82 * @ret rc Return status code
84 static int vlan_transmit ( struct net_device *netdev,
85 struct io_buffer *iobuf ) {
86 struct vlan_device *vlan = netdev->priv;
87 struct net_device *trunk = vlan->trunk;
88 struct ll_protocol *ll_protocol;
89 struct vlan_header *vlanhdr;
90 uint8_t ll_dest_copy[ETH_ALEN];
91 uint8_t ll_source_copy[ETH_ALEN];
93 const void *ll_source;
98 /* Strip link-layer header and preserve link-layer header fields */
99 ll_protocol = netdev->ll_protocol;
100 if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
101 &net_proto, &flags ) ) != 0 ) {
102 DBGC ( netdev, "VLAN %s could not parse link-layer header: "
103 "%s\n", netdev->name, strerror ( rc ) );
106 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
107 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
109 /* Construct VLAN header */
110 vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
111 vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
112 vlanhdr->net_proto = net_proto;
114 /* Reclaim I/O buffer from VLAN device's TX queue */
115 list_del ( &iobuf->list );
117 /* Transmit packet on trunk device */
118 if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
119 ll_dest_copy, ll_source_copy ) ) != 0 ) {
120 DBGC ( netdev, "VLAN %s could not transmit: %s\n",
121 netdev->name, strerror ( rc ) );
122 /* Cannot return an error status, since that would
123 * cause the I/O buffer to be double-freed.
134 * @v netdev Network device
136 static void vlan_poll ( struct net_device *netdev ) {
137 struct vlan_device *vlan = netdev->priv;
139 /* Poll trunk device */
140 netdev_poll ( vlan->trunk );
144 * Enable/disable interrupts on VLAN device
146 * @v netdev Network device
147 * @v enable Interrupts should be enabled
149 static void vlan_irq ( struct net_device *netdev, int enable ) {
150 struct vlan_device *vlan = netdev->priv;
152 /* Enable/disable interrupts on trunk device. This is not at
153 * all robust, but there is no sensible course of action
156 netdev_irq ( vlan->trunk, enable );
159 /** VLAN device operations */
160 static struct net_device_operations vlan_operations = {
163 .transmit = vlan_transmit,
169 * Synchronise VLAN device
171 * @v netdev Network device
173 static void vlan_sync ( struct net_device *netdev ) {
174 struct vlan_device *vlan = netdev->priv;
175 struct net_device *trunk = vlan->trunk;
177 /* Synchronise link status */
178 if ( netdev->link_rc != trunk->link_rc )
179 netdev_link_err ( netdev, trunk->link_rc );
181 /* Synchronise open/closed status */
182 if ( netdev_is_open ( trunk ) ) {
183 if ( ! netdev_is_open ( netdev ) )
184 netdev_open ( netdev );
186 if ( netdev_is_open ( netdev ) )
187 netdev_close ( netdev );
192 * Identify VLAN device
194 * @v trunk Trunk network device
196 * @ret netdev VLAN device, if any
198 struct net_device * vlan_find ( struct net_device *trunk, unsigned int tag ) {
199 struct net_device *netdev;
200 struct vlan_device *vlan;
202 for_each_netdev ( netdev ) {
203 if ( netdev->op != &vlan_operations )
206 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
213 * Process incoming VLAN packet
215 * @v iobuf I/O buffer
216 * @v trunk Trunk network device
217 * @v ll_dest Link-layer destination address
218 * @v ll_source Link-layer source address
219 * @v flags Packet flags
220 * @ret rc Return status code
222 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
223 const void *ll_dest, const void *ll_source,
224 unsigned int flags __unused ) {
225 struct vlan_header *vlanhdr = iobuf->data;
226 struct net_device *netdev;
227 struct ll_protocol *ll_protocol;
228 uint8_t ll_dest_copy[ETH_ALEN];
229 uint8_t ll_source_copy[ETH_ALEN];
234 if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
235 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
236 "bytes)\n", trunk->name, iob_len ( iobuf ) );
241 /* Identify VLAN device */
242 tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
243 netdev = vlan_find ( trunk, tag );
245 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
246 "%d\n", trunk->name, tag );
251 /* Strip VLAN header and preserve original link-layer header fields */
252 iob_pull ( iobuf, sizeof ( *vlanhdr ) );
253 ll_protocol = trunk->ll_protocol;
254 memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
255 memcpy ( ll_source_copy, ll_source, ETH_ALEN );
257 /* Reconstruct link-layer header for VLAN device */
258 ll_protocol = netdev->ll_protocol;
259 if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
261 vlanhdr->net_proto ) ) != 0 ) {
262 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
263 "header: %s\n", netdev->name, strerror ( rc ) );
267 /* Enqueue packet on VLAN device */
268 netdev_rx ( netdev, iob_disown ( iobuf ) );
279 struct net_protocol vlan_protocol __net_protocol = {
281 .net_proto = htons ( ETH_P_8021Q ),
288 * @v netdev Network device
289 * @ret tag VLAN tag, or 0 if device is not a VLAN device
291 unsigned int vlan_tag ( struct net_device *netdev ) {
292 struct vlan_device *vlan;
294 if ( netdev->op == &vlan_operations ) {
303 * Check if network device can be used as a VLAN trunk device
305 * @v trunk Trunk network device
306 * @ret is_ok Trunk network device is usable
308 * VLAN devices will be created as Ethernet devices. (We cannot
309 * simply clone the link layer of the trunk network device, because
310 * this link layer may expect the network device structure to contain
311 * some link-layer-private data.) The trunk network device must
312 * therefore have a link layer that is in some sense 'compatible' with
313 * Ethernet; specifically, it must have link-layer addresses that are
314 * the same length as Ethernet link-layer addresses.
316 * As an additional check, and primarily to assist with the sanity of
317 * the FCoE code, we refuse to allow nested VLANs.
319 int vlan_can_be_trunk ( struct net_device *trunk ) {
321 return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
322 ( trunk->op != &vlan_operations ) );
328 * @v trunk Trunk network device
330 * @v priority Default VLAN priority
331 * @ret rc Return status code
333 int vlan_create ( struct net_device *trunk, unsigned int tag,
334 unsigned int priority ) {
335 struct net_device *netdev;
336 struct vlan_device *vlan;
339 /* If VLAN already exists, just update the priority */
340 if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
342 if ( priority != vlan->priority ) {
343 DBGC ( netdev, "VLAN %s priority changed from %d to "
344 "%d\n", netdev->name, vlan->priority, priority );
346 vlan->priority = priority;
351 if ( ! vlan_can_be_trunk ( trunk ) ) {
352 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
353 "device\n", trunk->name );
357 if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
358 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
359 "%d\n", trunk->name, tag );
363 if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
364 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
365 "priority %d\n", trunk->name, priority );
370 /* Allocate and initialise structure */
371 netdev = alloc_etherdev ( sizeof ( *vlan ) );
374 goto err_alloc_etherdev;
376 netdev_init ( netdev, &vlan_operations );
377 netdev->dev = trunk->dev;
378 memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
380 vlan->trunk = netdev_get ( trunk );
382 vlan->priority = priority;
384 /* Construct VLAN device name */
385 snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
386 trunk->name, vlan->tag );
388 /* Register VLAN device */
389 if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
390 DBGC ( netdev, "VLAN %s could not register: %s\n",
391 netdev->name, strerror ( rc ) );
395 /* Synchronise with trunk device */
396 vlan_sync ( netdev );
398 DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
399 netdev->name, vlan->tag, vlan->priority );
403 unregister_netdev ( netdev );
405 netdev_nullify ( netdev );
406 netdev_put ( netdev );
407 netdev_put ( trunk );
414 * Destroy VLAN device
416 * @v netdev Network device
417 * @ret rc Return status code
419 int vlan_destroy ( struct net_device *netdev ) {
420 struct vlan_device *vlan = netdev->priv;
421 struct net_device *trunk;
424 if ( netdev->op != &vlan_operations ) {
425 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
430 DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
432 /* Remove VLAN device */
433 unregister_netdev ( netdev );
435 netdev_nullify ( netdev );
436 netdev_put ( netdev );
437 netdev_put ( trunk );
443 * Handle trunk network device link state change
445 * @v trunk Trunk network device
447 static void vlan_notify ( struct net_device *trunk ) {
448 struct net_device *netdev;
449 struct vlan_device *vlan;
451 for_each_netdev ( netdev ) {
452 if ( netdev->op != &vlan_operations )
455 if ( vlan->trunk == trunk )
456 vlan_sync ( netdev );
461 * Destroy first VLAN device for a given trunk
463 * @v trunk Trunk network device
464 * @ret found A VLAN device was found
466 static int vlan_remove_first ( struct net_device *trunk ) {
467 struct net_device *netdev;
468 struct vlan_device *vlan;
470 for_each_netdev ( netdev ) {
471 if ( netdev->op != &vlan_operations )
474 if ( vlan->trunk == trunk ) {
475 vlan_destroy ( netdev );
483 * Destroy all VLAN devices for a given trunk
485 * @v trunk Trunk network device
487 static void vlan_remove ( struct net_device *trunk ) {
489 /* Remove all VLAN devices attached to this trunk, safe
490 * against arbitrary net device removal.
492 while ( vlan_remove_first ( trunk ) ) {}
496 struct net_driver vlan_driver __net_driver = {
498 .notify = vlan_notify,
499 .remove = vlan_remove,