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 static struct net_device * vlan_find ( struct net_device *trunk,
00203                                        unsigned int tag ) {
00204         struct net_device *netdev;
00205         struct vlan_device *vlan;
00206 
00207         for_each_netdev ( netdev ) {
00208                 if ( netdev->op != &vlan_operations )
00209                         continue;
00210                 vlan = netdev->priv;
00211                 if ( ( vlan->trunk == trunk ) && ( vlan->tag == tag ) )
00212                         return netdev;
00213         }
00214         return NULL;
00215 }
00216 
00217 /**
00218  * Process incoming VLAN packet
00219  *
00220  * @v iobuf             I/O buffer
00221  * @v trunk             Trunk network device
00222  * @v ll_dest           Link-layer destination address
00223  * @v ll_source         Link-layer source address
00224  * @v flags             Packet flags
00225  * @ret rc              Return status code
00226  */
00227 static int vlan_rx ( struct io_buffer *iobuf, struct net_device *trunk,
00228                      const void *ll_dest, const void *ll_source,
00229                      unsigned int flags __unused ) {
00230         struct vlan_header *vlanhdr = iobuf->data;
00231         struct net_device *netdev;
00232         struct ll_protocol *ll_protocol;
00233         uint8_t ll_dest_copy[ETH_ALEN];
00234         uint8_t ll_source_copy[ETH_ALEN];
00235         uint16_t tag;
00236         int rc;
00237 
00238         /* Sanity check */
00239         if ( iob_len ( iobuf ) < sizeof ( *vlanhdr ) ) {
00240                 DBGC ( trunk, "VLAN %s received underlength packet (%zd "
00241                        "bytes)\n", trunk->name, iob_len ( iobuf ) );
00242                 rc = -EINVAL;
00243                 goto err_sanity;
00244         }
00245 
00246         /* Identify VLAN device */
00247         tag = VLAN_TAG ( ntohs ( vlanhdr->tci ) );
00248         netdev = vlan_find ( trunk, tag );
00249         if ( ! netdev ) {
00250                 DBGC2 ( trunk, "VLAN %s received packet for unknown VLAN "
00251                         "%d\n", trunk->name, tag );
00252                 rc = -EPIPE;
00253                 goto err_no_vlan;
00254         }
00255 
00256         /* Strip VLAN header and preserve original link-layer header fields */
00257         iob_pull ( iobuf, sizeof ( *vlanhdr ) );
00258         ll_protocol = trunk->ll_protocol;
00259         memcpy ( ll_dest_copy, ll_dest, ETH_ALEN );
00260         memcpy ( ll_source_copy, ll_source, ETH_ALEN );
00261 
00262         /* Reconstruct link-layer header for VLAN device */
00263         ll_protocol = netdev->ll_protocol;
00264         if ( ( rc = ll_protocol->push ( netdev, iobuf, ll_dest_copy,
00265                                         ll_source_copy,
00266                                         vlanhdr->net_proto ) ) != 0 ) {
00267                 DBGC ( netdev, "VLAN %s could not reconstruct link-layer "
00268                        "header: %s\n", netdev->name, strerror ( rc ) );
00269                 goto err_ll_push;
00270         }
00271 
00272         /* Enqueue packet on VLAN device */
00273         netdev_rx ( netdev, iob_disown ( iobuf ) );
00274         return 0;
00275 
00276  err_ll_push:
00277  err_no_vlan:
00278  err_sanity:
00279         free_iob ( iobuf );
00280         return rc;
00281 }
00282 
00283 /** VLAN protocol */
00284 struct net_protocol vlan_protocol __net_protocol = {
00285         .name = "VLAN",
00286         .net_proto = htons ( ETH_P_8021Q ),
00287         .rx = vlan_rx,
00288 };
00289 
00290 /**
00291  * Get the VLAN tag
00292  *
00293  * @v netdev            Network device
00294  * @ret tag             VLAN tag, or 0 if device is not a VLAN device
00295  */
00296 unsigned int vlan_tag ( struct net_device *netdev ) {
00297         struct vlan_device *vlan;
00298 
00299         if ( netdev->op == &vlan_operations ) {
00300                 vlan = netdev->priv;
00301                 return vlan->tag;
00302         } else {
00303                 return 0;
00304         }
00305 }
00306 
00307 /**
00308  * Check if network device can be used as a VLAN trunk device
00309  *
00310  * @v trunk             Trunk network device
00311  * @ret is_ok           Trunk network device is usable
00312  *
00313  * VLAN devices will be created as Ethernet devices.  (We cannot
00314  * simply clone the link layer of the trunk network device, because
00315  * this link layer may expect the network device structure to contain
00316  * some link-layer-private data.)  The trunk network device must
00317  * therefore have a link layer that is in some sense 'compatible' with
00318  * Ethernet; specifically, it must have link-layer addresses that are
00319  * the same length as Ethernet link-layer addresses.
00320  *
00321  * As an additional check, and primarily to assist with the sanity of
00322  * the FCoE code, we refuse to allow nested VLANs.
00323  */
00324 int vlan_can_be_trunk ( struct net_device *trunk ) {
00325 
00326         return ( ( trunk->ll_protocol->ll_addr_len == ETH_ALEN ) &&
00327                  ( trunk->op != &vlan_operations ) );
00328 }
00329 
00330 /**
00331  * Create VLAN device
00332  *
00333  * @v trunk             Trunk network device
00334  * @v tag               VLAN tag
00335  * @v priority          Default VLAN priority
00336  * @ret rc              Return status code
00337  */
00338 int vlan_create ( struct net_device *trunk, unsigned int tag,
00339                   unsigned int priority ) {
00340         struct net_device *netdev;
00341         struct vlan_device *vlan;
00342         int rc;
00343 
00344         /* If VLAN already exists, just update the priority */
00345         if ( ( netdev = vlan_find ( trunk, tag ) ) != NULL ) {
00346                 vlan = netdev->priv;
00347                 if ( priority != vlan->priority ) {
00348                         DBGC ( netdev, "VLAN %s priority changed from %d to "
00349                                "%d\n", netdev->name, vlan->priority, priority );
00350                 }
00351                 vlan->priority = priority;
00352                 return 0;
00353         }
00354 
00355         /* Sanity checks */
00356         if ( ! vlan_can_be_trunk ( trunk ) ) {
00357                 DBGC ( trunk, "VLAN %s cannot create VLAN on non-trunk "
00358                        "device\n", trunk->name );
00359                 rc = -ENOTTY;
00360                 goto err_sanity;
00361         }
00362         if ( ! VLAN_TAG_IS_VALID ( tag ) ) {
00363                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid tag "
00364                        "%d\n", trunk->name, tag );
00365                 rc = -EINVAL;
00366                 goto err_sanity;
00367         }
00368         if ( ! VLAN_PRIORITY_IS_VALID ( priority ) ) {
00369                 DBGC ( trunk, "VLAN %s cannot create VLAN with invalid "
00370                        "priority %d\n", trunk->name, priority );
00371                 rc = -EINVAL;
00372                 goto err_sanity;
00373         }
00374 
00375         /* Allocate and initialise structure */
00376         netdev = alloc_etherdev ( sizeof ( *vlan ) );
00377         if ( ! netdev ) {
00378                 rc = -ENOMEM;
00379                 goto err_alloc_etherdev;
00380         }
00381         netdev_init ( netdev, &vlan_operations );
00382         netdev->dev = trunk->dev;
00383         memcpy ( netdev->hw_addr, trunk->ll_addr, ETH_ALEN );
00384         vlan = netdev->priv;
00385         vlan->trunk = netdev_get ( trunk );
00386         vlan->tag = tag;
00387         vlan->priority = priority;
00388 
00389         /* Construct VLAN device name */
00390         snprintf ( netdev->name, sizeof ( netdev->name ), "%s-%d",
00391                    trunk->name, vlan->tag );
00392 
00393         /* Mark device as not supporting interrupts, if applicable */
00394         if ( ! netdev_irq_supported ( trunk ) )
00395                 netdev->state |= NETDEV_IRQ_UNSUPPORTED;
00396 
00397         /* Register VLAN device */
00398         if ( ( rc = register_netdev ( netdev ) ) != 0 ) {
00399                 DBGC ( netdev, "VLAN %s could not register: %s\n",
00400                        netdev->name, strerror ( rc ) );
00401                 goto err_register;
00402         }
00403 
00404         /* Synchronise with trunk device */
00405         vlan_sync ( netdev );
00406 
00407         DBGC ( netdev, "VLAN %s created with tag %d and priority %d\n",
00408                netdev->name, vlan->tag, vlan->priority );
00409 
00410         return 0;
00411 
00412         unregister_netdev ( netdev );
00413  err_register:
00414         netdev_nullify ( netdev );
00415         netdev_put ( netdev );
00416         netdev_put ( trunk );
00417  err_alloc_etherdev:
00418  err_sanity:
00419         return rc;
00420 }
00421 
00422 /**
00423  * Destroy VLAN device
00424  *
00425  * @v netdev            Network device
00426  * @ret rc              Return status code
00427  */
00428 int vlan_destroy ( struct net_device *netdev ) {
00429         struct vlan_device *vlan = netdev->priv;
00430         struct net_device *trunk;
00431 
00432         /* Sanity check */
00433         if ( netdev->op != &vlan_operations ) {
00434                 DBGC ( netdev, "VLAN %s cannot destroy non-VLAN device\n",
00435                        netdev->name );
00436                 return -ENOTTY;
00437         }
00438 
00439         DBGC ( netdev, "VLAN %s destroyed\n", netdev->name );
00440 
00441         /* Remove VLAN device */
00442         unregister_netdev ( netdev );
00443         trunk = vlan->trunk;
00444         netdev_nullify ( netdev );
00445         netdev_put ( netdev );
00446         netdev_put ( trunk );
00447 
00448         return 0;
00449 }
00450 
00451 /**
00452  * Handle trunk network device link state change
00453  *
00454  * @v trunk             Trunk network device
00455  */
00456 static void vlan_notify ( struct net_device *trunk ) {
00457         struct net_device *netdev;
00458         struct vlan_device *vlan;
00459 
00460         for_each_netdev ( netdev ) {
00461                 if ( netdev->op != &vlan_operations )
00462                         continue;
00463                 vlan = netdev->priv;
00464                 if ( vlan->trunk == trunk )
00465                         vlan_sync ( netdev );
00466         }
00467 }
00468 
00469 /**
00470  * Destroy first VLAN device for a given trunk
00471  *
00472  * @v trunk             Trunk network device
00473  * @ret found           A VLAN device was found
00474  */
00475 static int vlan_remove_first ( struct net_device *trunk ) {
00476         struct net_device *netdev;
00477         struct vlan_device *vlan;
00478 
00479         for_each_netdev ( netdev ) {
00480                 if ( netdev->op != &vlan_operations )
00481                         continue;
00482                 vlan = netdev->priv;
00483                 if ( vlan->trunk == trunk ) {
00484                         vlan_destroy ( netdev );
00485                         return 1;
00486                 }
00487         }
00488         return 0;
00489 }
00490 
00491 /**
00492  * Destroy all VLAN devices for a given trunk
00493  *
00494  * @v trunk             Trunk network device
00495  */
00496 static void vlan_remove ( struct net_device *trunk ) {
00497 
00498         /* Remove all VLAN devices attached to this trunk, safe
00499          * against arbitrary net device removal.
00500          */
00501         while ( vlan_remove_first ( trunk ) ) {}
00502 }
00503 
00504 /** VLAN driver */
00505 struct net_driver vlan_driver __net_driver = {
00506         .name = "VLAN",
00507         .notify = vlan_notify,
00508         .remove = vlan_remove,
00509 };
00510 
00511 /**
00512  * Add VLAN tag-stripped packet to receive queue
00513  *
00514  * @v netdev            Network device
00515  * @v tag               VLAN tag, or zero
00516  * @v iobuf             I/O buffer
00517  */
00518 void vlan_netdev_rx ( struct net_device *netdev, unsigned int tag,
00519                       struct io_buffer *iobuf ) {
00520         struct net_device *vlan;
00521 
00522         /* Identify VLAN device, if applicable */
00523         if ( tag ) {
00524                 if ( ( vlan = vlan_find ( netdev, tag ) ) == NULL ) {
00525                         netdev_rx_err ( netdev, iobuf, -ENODEV );
00526                         return;
00527                 }
00528                 netdev = vlan;
00529         }
00530 
00531         /* Hand off to network device */
00532         netdev_rx ( netdev, iobuf );
00533 }
00534 
00535 /**
00536  * Discard received VLAN tag-stripped packet
00537  *
00538  * @v netdev            Network device
00539  * @v tag               VLAN tag, or zero
00540  * @v iobuf             I/O buffer, or NULL
00541  * @v rc                Packet status code
00542  */
00543 void vlan_netdev_rx_err ( struct net_device *netdev, unsigned int tag,
00544                           struct io_buffer *iobuf, int rc ) {
00545         struct net_device *vlan;
00546 
00547         /* Identify VLAN device, if applicable */
00548         if ( tag && ( ( vlan = vlan_find ( netdev, tag ) ) != NULL ) )
00549                 netdev = vlan;
00550 
00551         /* Hand off to network device */
00552         netdev_rx_err ( netdev, iobuf, rc );
00553 }