iPXE
vlan.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <stdio.h>
00029 #include <errno.h>
00030 #include <byteswap.h>
00031 #include <ipxe/features.h>
00032 #include <ipxe/if_ether.h>
00033 #include <ipxe/ethernet.h>
00034 #include <ipxe/netdevice.h>
00035 #include <ipxe/iobuf.h>
00036 #include <ipxe/vlan.h>
00037 
00038 /** @file
00039  *
00040  * Virtual LANs
00041  *
00042  */
00043 
00044 FEATURE ( FEATURE_PROTOCOL, "VLAN", DHCP_EB_FEATURE_VLAN, 1 );
00045 
00046 struct net_protocol vlan_protocol __net_protocol;
00047 
00048 /** VLAN device private data */
00049 struct vlan_device {
00050         /** Trunk network device */
00051         struct net_device *trunk;
00052         /** VLAN tag */
00053         unsigned int tag;
00054         /** Default priority */
00055         unsigned int priority;
00056 };
00057 
00058 /**
00059  * Open VLAN device
00060  *
00061  * @v netdev            Network device
00062  * @ret rc              Return status code
00063  */
00064 static int vlan_open ( struct net_device *netdev ) {
00065         struct vlan_device *vlan = netdev->priv;
00066 
00067         return netdev_open ( vlan->trunk );
00068 }
00069 
00070 /**
00071  * Close VLAN device
00072  *
00073  * @v netdev            Network device
00074  */
00075 static void vlan_close ( struct net_device *netdev ) {
00076         struct vlan_device *vlan = netdev->priv;
00077 
00078         netdev_close ( vlan->trunk );
00079 }
00080 
00081 /**
00082  * Transmit packet on VLAN device
00083  *
00084  * @v netdev            Network device
00085  * @v iobuf             I/O buffer
00086  * @ret rc              Return status code
00087  */
00088 static int vlan_transmit ( struct net_device *netdev,
00089                            struct io_buffer *iobuf ) {
00090         struct vlan_device *vlan = netdev->priv;
00091         struct net_device *trunk = vlan->trunk;
00092         struct ll_protocol *ll_protocol;
00093         struct vlan_header *vlanhdr;
00094         uint8_t ll_dest_copy[ETH_ALEN];
00095         uint8_t ll_source_copy[ETH_ALEN];
00096         const void *ll_dest;
00097         const void *ll_source;
00098         uint16_t net_proto;
00099         unsigned int flags;
00100         int rc;
00101 
00102         /* Strip link-layer header and preserve link-layer header fields */
00103         ll_protocol = netdev->ll_protocol;
00104         if ( ( rc = ll_protocol->pull ( netdev, iobuf, &ll_dest, &ll_source,
00105                                         &net_proto, &flags ) ) != 0 ) {
00106                 DBGC ( netdev, "VLAN %s could not parse link-layer header: "
00107                        "%s\n", netdev->name, strerror ( rc ) );
00108                 return rc;
00109         }
00110         memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
00111         memcpy ( ll_source_copy, ll_source, ETH_ALEN );
00112 
00113         /* Construct VLAN header */
00114         vlanhdr = iob_push ( iobuf, sizeof ( *vlanhdr ) );
00115         vlanhdr->tci = htons ( VLAN_TCI ( vlan->tag, vlan->priority ) );
00116         vlanhdr->net_proto = net_proto;
00117 
00118         /* Reclaim I/O buffer from VLAN device's TX queue */
00119         list_del ( &iobuf->list );
00120 
00121         /* Transmit packet on trunk device */
00122         if ( ( rc = net_tx ( iob_disown ( iobuf ), trunk, &vlan_protocol,
00123                              ll_dest_copy, ll_source_copy ) ) != 0 ) {
00124                 DBGC ( netdev, "VLAN %s could not transmit: %s\n",
00125                        netdev->name, strerror ( rc ) );
00126                 /* Cannot return an error status, since that would
00127                  * cause the I/O buffer to be double-freed.
00128                  */
00129                 return 0;
00130         }
00131 
00132         return 0;
00133 }
00134 
00135 /**
00136  * Poll VLAN device
00137  *
00138  * @v netdev            Network device
00139  */
00140 static void vlan_poll ( struct net_device *netdev ) {
00141         struct vlan_device *vlan = netdev->priv;
00142 
00143         /* Poll trunk device */
00144         netdev_poll ( vlan->trunk );
00145 }
00146 
00147 /**
00148  * Enable/disable interrupts on VLAN device
00149  *
00150  * @v netdev            Network device
00151  * @v enable            Interrupts should be enabled
00152  */
00153 static void vlan_irq ( struct net_device *netdev, int enable ) {
00154         struct vlan_device *vlan = netdev->priv;
00155 
00156         /* Enable/disable interrupts on trunk device.  This is not at
00157          * all robust, but there is no sensible course of action
00158          * available.
00159          */
00160         netdev_irq ( vlan->trunk, enable );
00161 }
00162 
00163 /** VLAN device operations */
00164 static struct net_device_operations vlan_operations = {
00165         .open           = vlan_open,
00166         .close          = vlan_close,
00167         .transmit       = vlan_transmit,
00168         .poll           = vlan_poll,
00169         .irq            = vlan_irq,
00170 };
00171 
00172 /**
00173  * Synchronise VLAN device
00174  *
00175  * @v netdev            Network device
00176  */
00177 static void vlan_sync ( struct net_device *netdev ) {
00178         struct vlan_device *vlan = netdev->priv;
00179         struct net_device *trunk = vlan->trunk;
00180 
00181         /* Synchronise link status */
00182         if ( netdev->link_rc != trunk->link_rc )
00183                 netdev_link_err ( netdev, trunk->link_rc );
00184 
00185         /* Synchronise open/closed status */
00186         if ( netdev_is_open ( trunk ) ) {
00187                 if ( ! netdev_is_open ( netdev ) )
00188                         netdev_open ( netdev );
00189         } else {
00190                 if ( netdev_is_open ( netdev ) )
00191                         netdev_close ( netdev );
00192         }
00193 }
00194 
00195 /**
00196  * Identify VLAN device
00197  *
00198  * @v trunk             Trunk network device
00199  * @v tag               VLAN tag
00200  * @ret netdev          VLAN device, if any
00201  */
00202 struct net_device * vlan_find ( struct net_device *trunk, unsigned int tag ) {
00203         struct net_device *netdev;
00204         struct vlan_device *vlan;
00205 
00206         for_each_netdev ( netdev ) {
00207                 if ( netdev->op != &vlan_operations )
00208                         continue;
00209                 vlan = netdev->priv;
00210                 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
00211                         return netdev;
00212         }
00213         return NULL;
00214 }
00215 
00216 /**
00217  * Process incoming VLAN packet
00218  *
00219  * @v iobuf             I/O buffer
00220  * @v trunk             Trunk network device
00221  * @v ll_dest           Link-layer destination address
00222  * @v ll_source         Link-layer source address
00223  * @v flags             Packet flags
00224  * @ret rc              Return status code
00225  */
00226 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
00227                      const void *ll_dest, const void *ll_source,
00228                      unsigned int flags __unused ) {
00229         struct vlan_header *vlanhdr = iobuf->data;
00230         struct net_device *netdev;
00231         struct ll_protocol *ll_protocol;
00232         uint8_t ll_dest_copy[ETH_ALEN];
00233         uint8_t ll_source_copy[ETH_ALEN];
00234         uint16_t tag;
00235         int rc;
00236 
00237         /* Sanity check */
00238         if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
00239                 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
00240                        "bytes)\n", trunk->name, iob_len ( iobuf ) );
00241                 rc = -EINVAL;
00242                 goto err_sanity;
00243         }
00244 
00245         /* Identify VLAN device */
00246         tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
00247         netdev = vlan_find ( trunk, tag );
00248         if ( ! netdev ) {
00249                 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
00250                         "%d\n", trunk->name, tag );
00251                 rc = -EPIPE;
00252                 goto err_no_vlan;
00253         }
00254 
00255         /* Strip VLAN header and preserve original link-layer header fields */
00256         iob_pull ( iobuf, sizeof ( *vlanhdr ) );
00257         ll_protocol = trunk->ll_protocol;
00258         memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
00259         memcpy ( ll_source_copy, ll_source, ETH_ALEN );
00260 
00261         /* Reconstruct link-layer header for VLAN device */
00262         ll_protocol = netdev->ll_protocol;
00263         if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
00264                                         ll_source_copy,
00265                                         vlanhdr->net_proto ) ) != 0 ) {
00266                 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
00267                        "header: %s\n", netdev->name, strerror ( rc ) );
00268                 goto err_ll_push;
00269         }
00270 
00271         /* Enqueue packet on VLAN device */
00272         netdev_rx ( netdev, iob_disown ( iobuf ) );
00273         return 0;
00274 
00275  err_ll_push:
00276  err_no_vlan:
00277  err_sanity:
00278         free_iob ( iobuf );
00279         return rc;
00280 }
00281 
00282 /** VLAN protocol */
00283 struct net_protocol vlan_protocol __net_protocol = {
00284         .name = "VLAN",
00285         .net_proto = htons ( ETH_P_8021Q ),
00286         .rx = vlan_rx,
00287 };
00288 
00289 /**
00290  * Get the VLAN tag
00291  *
00292  * @v netdev            Network device
00293  * @ret tag             VLAN tag, or 0 if device is not a VLAN device
00294  */
00295 unsigned int vlan_tag ( struct net_device *netdev ) {
00296         struct vlan_device *vlan;
00297 
00298         if ( netdev->op == &vlan_operations ) {
00299                 vlan = netdev->priv;
00300                 return vlan->tag;
00301         } else {
00302                 return 0;
00303         }
00304 }
00305 
00306 /**
00307  * Check if network device can be used as a VLAN trunk device
00308  *
00309  * @v trunk             Trunk network device
00310  * @ret is_ok           Trunk network device is usable
00311  *
00312  * VLAN devices will be created as Ethernet devices.  (We cannot
00313  * simply clone the link layer of the trunk network device, because
00314  * this link layer may expect the network device structure to contain
00315  * some link-layer-private data.)  The trunk network device must
00316  * therefore have a link layer that is in some sense 'compatible' with
00317  * Ethernet; specifically, it must have link-layer addresses that are
00318  * the same length as Ethernet link-layer addresses.
00319  *
00320  * As an additional check, and primarily to assist with the sanity of
00321  * the FCoE code, we refuse to allow nested VLANs.
00322  */
00323 int vlan_can_be_trunk ( struct net_device *trunk ) {
00324 
00325         return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
00326                  ( trunk->op != &vlan_operations ) );
00327 }
00328 
00329 /**
00330  * Create VLAN device
00331  *
00332  * @v trunk             Trunk network device
00333  * @v tag               VLAN tag
00334  * @v priority          Default VLAN priority
00335  * @ret rc              Return status code
00336  */
00337 int vlan_create ( struct net_device *trunk, unsigned int tag,
00338                   unsigned int priority ) {
00339         struct net_device *netdev;
00340         struct vlan_device *vlan;
00341         int rc;
00342 
00343         /* If VLAN already exists, just update the priority */
00344         if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
00345                 vlan = netdev->priv;
00346                 if ( priority != vlan->priority ) {
00347                         DBGC ( netdev, "VLAN %s priority changed from %d to "
00348                                "%d\n", netdev->name, vlan->priority, priority );
00349                 }
00350                 vlan->priority = priority;
00351                 return 0;
00352         }
00353 
00354         /* Sanity checks */
00355         if ( ! vlan_can_be_trunk ( trunk ) ) {
00356                 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
00357                        "device\n", trunk->name );
00358                 rc = -ENOTTY;
00359                 goto err_sanity;
00360         }
00361         if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
00362                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
00363                        "%d\n", trunk->name, tag );
00364                 rc = -EINVAL;
00365                 goto err_sanity;
00366         }
00367         if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
00368                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
00369                        "priority %d\n", trunk->name, priority );
00370                 rc = -EINVAL;
00371                 goto err_sanity;
00372         }
00373 
00374         /* Allocate and initialise structure */
00375         netdev = alloc_etherdev ( sizeof ( *vlan ) );
00376         if ( ! netdev ) {
00377                 rc = -ENOMEM;
00378                 goto err_alloc_etherdev;
00379         }
00380         netdev_init ( netdev, &vlan_operations );
00381         netdev->dev = trunk->dev;
00382         memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
00383         vlan = netdev->priv;
00384         vlan->trunk = netdev_get ( trunk );
00385         vlan->tag = tag;
00386         vlan->priority = priority;
00387 
00388         /* Construct VLAN device name */
00389         snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
00390                    trunk->name, vlan->tag );
00391 
00392         /* Mark device as not supporting interrupts, if applicable */
00393         if ( ! netdev_irq_supported ( trunk ) )
00394                 netdev->state |= NETDEV_IRQ_UNSUPPORTED;
00395 
00396         /* Register VLAN device */
00397         if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
00398                 DBGC ( netdev, "VLAN %s could not register: %s\n",
00399                        netdev->name, strerror ( rc ) );
00400                 goto err_register;
00401         }
00402 
00403         /* Synchronise with trunk device */
00404         vlan_sync ( netdev );
00405 
00406         DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
00407                netdev->name, vlan->tag, vlan->priority );
00408 
00409         return 0;
00410 
00411         unregister_netdev ( netdev );
00412  err_register:
00413         netdev_nullify ( netdev );
00414         netdev_put ( netdev );
00415         netdev_put ( trunk );
00416  err_alloc_etherdev:
00417  err_sanity:
00418         return rc;
00419 }
00420 
00421 /**
00422  * Destroy VLAN device
00423  *
00424  * @v netdev            Network device
00425  * @ret rc              Return status code
00426  */
00427 int vlan_destroy ( struct net_device *netdev ) {
00428         struct vlan_device *vlan = netdev->priv;
00429         struct net_device *trunk;
00430 
00431         /* Sanity check */
00432         if ( netdev->op != &vlan_operations ) {
00433                 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
00434                        netdev->name );
00435                 return -ENOTTY;
00436         }
00437 
00438         DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
00439 
00440         /* Remove VLAN device */
00441         unregister_netdev ( netdev );
00442         trunk = vlan->trunk;
00443         netdev_nullify ( netdev );
00444         netdev_put ( netdev );
00445         netdev_put ( trunk );
00446 
00447         return 0;
00448 }
00449 
00450 /**
00451  * Handle trunk network device link state change
00452  *
00453  * @v trunk             Trunk network device
00454  */
00455 static void vlan_notify ( struct net_device *trunk ) {
00456         struct net_device *netdev;
00457         struct vlan_device *vlan;
00458 
00459         for_each_netdev ( netdev ) {
00460                 if ( netdev->op != &vlan_operations )
00461                         continue;
00462                 vlan = netdev->priv;
00463                 if ( vlan->trunk == trunk )
00464                         vlan_sync ( netdev );
00465         }
00466 }
00467 
00468 /**
00469  * Destroy first VLAN device for a given trunk
00470  *
00471  * @v trunk             Trunk network device
00472  * @ret found           A VLAN device was found
00473  */
00474 static int vlan_remove_first ( struct net_device *trunk ) {
00475         struct net_device *netdev;
00476         struct vlan_device *vlan;
00477 
00478         for_each_netdev ( netdev ) {
00479                 if ( netdev->op != &vlan_operations )
00480                         continue;
00481                 vlan = netdev->priv;
00482                 if ( vlan->trunk == trunk ) {
00483                         vlan_destroy ( netdev );
00484                         return 1;
00485                 }
00486         }
00487         return 0;
00488 }
00489 
00490 /**
00491  * Destroy all VLAN devices for a given trunk
00492  *
00493  * @v trunk             Trunk network device
00494  */
00495 static void vlan_remove ( struct net_device *trunk ) {
00496 
00497         /* Remove all VLAN devices attached to this trunk, safe
00498          * against arbitrary net device removal.
00499          */
00500         while ( vlan_remove_first ( trunk ) ) {}
00501 }
00502 
00503 /** VLAN driver */
00504 struct net_driver vlan_driver __net_driver = {
00505         .name = "VLAN",
00506         .notify = vlan_notify,
00507         .remove = vlan_remove,
00508 };