iPXE
stp.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 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 (at your option) 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 <errno.h>
00027 #include <byteswap.h>
00028 #include <ipxe/netdevice.h>
00029 #include <ipxe/ethernet.h>
00030 #include <ipxe/iobuf.h>
00031 #include <ipxe/timer.h>
00032 #include <ipxe/stp.h>
00033 
00034 /** @file
00035  *
00036  * Spanning Tree Protocol (STP)
00037  *
00038  */
00039 
00040 /* Disambiguate the various error causes */
00041 #define ENOTSUP_PROTOCOL __einfo_error ( EINFO_ENOTSUP_PROTOCOL )
00042 #define EINFO_ENOTSUP_PROTOCOL                                  \
00043         __einfo_uniqify ( EINFO_ENOTSUP, 0x02,                  \
00044                           "Non-STP packet received" )
00045 #define ENOTSUP_VERSION __einfo_error ( EINFO_ENOTSUP_VERSION )
00046 #define EINFO_ENOTSUP_VERSION                                   \
00047         __einfo_uniqify ( EINFO_ENOTSUP, 0x03,                  \
00048                           "Legacy STP packet received" )
00049 #define ENOTSUP_TYPE __einfo_error ( EINFO_ENOTSUP_TYPE )
00050 #define EINFO_ENOTSUP_TYPE                                      \
00051         __einfo_uniqify ( EINFO_ENOTSUP, 0x04,                  \
00052                           "Non-RSTP packet received" )
00053 
00054 /**
00055  * Process incoming STP packets
00056  *
00057  * @v iobuf             I/O buffer
00058  * @v netdev            Network device
00059  * @v ll_source         Link-layer source address
00060  * @v flags             Packet flags
00061  * @ret rc              Return status code
00062  */
00063 static int stp_rx ( struct io_buffer *iobuf, struct net_device *netdev,
00064                     const void *ll_dest __unused,
00065                     const void *ll_source __unused,
00066                     unsigned int flags __unused ) {
00067         struct stp_bpdu *stp;
00068         unsigned int hello;
00069         int rc;
00070 
00071         /* Sanity check */
00072         if ( iob_len ( iobuf ) < sizeof ( *stp ) ) {
00073                 DBGC ( netdev, "STP %s received underlength packet (%zd "
00074                        "bytes):\n", netdev->name, iob_len ( iobuf ) );
00075                 DBGC_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
00076                 rc = -EINVAL;
00077                 goto done;
00078         }
00079         stp = iobuf->data;
00080 
00081         /* Ignore non-RSTP packets */
00082         if ( stp->protocol != htons ( STP_PROTOCOL ) ) {
00083                 DBGC ( netdev, "STP %s ignoring non-STP packet (protocol "
00084                        "%#04x)\n", netdev->name, ntohs ( stp->protocol ) );
00085                 rc = -ENOTSUP_PROTOCOL;
00086                 goto done;
00087         }
00088         if ( stp->version < STP_VERSION_RSTP ) {
00089                 DBGC ( netdev, "STP %s received legacy STP packet (version "
00090                        "%#02x)\n", netdev->name, stp->version );
00091                 rc = -ENOTSUP_VERSION;
00092                 goto done;
00093         }
00094         if ( stp->type != STP_TYPE_RSTP ) {
00095                 DBGC ( netdev, "STP %s received non-RSTP packet (type %#02x)\n",
00096                        netdev->name, stp->type );
00097                 rc = -ENOTSUP_TYPE;
00098                 goto done;
00099         }
00100 
00101         /* Dump information */
00102         DBGC2 ( netdev, "STP %s %s port %#04x flags %#02x hello %d delay %d\n",
00103                 netdev->name, eth_ntoa ( stp->sender.mac ), ntohs ( stp->port ),
00104                 stp->flags, ntohs ( stp->hello ), ntohs ( stp->delay ) );
00105 
00106         /* Check if port is forwarding */
00107         if ( ! ( stp->flags & STP_FL_FORWARDING ) ) {
00108                 /* Port is not forwarding: block link for two hello times */
00109                 DBGC ( netdev, "STP %s %s port %#04x flags %#02x is not "
00110                        "forwarding\n",
00111                        netdev->name, eth_ntoa ( stp->sender.mac ),
00112                        ntohs ( stp->port ), stp->flags );
00113                 hello = ( ntohs ( stp->hello ) * ( TICKS_PER_SEC / 256 ) );
00114                 netdev_link_block ( netdev, ( hello * 2 ) );
00115                 rc = -ENETUNREACH;
00116                 goto done;
00117         }
00118 
00119         /* Success */
00120         if ( netdev_link_blocked ( netdev ) ) {
00121                 DBGC ( netdev, "STP %s %s port %#04x flags %#02x is "
00122                        "forwarding\n",
00123                        netdev->name, eth_ntoa ( stp->sender.mac ),
00124                        ntohs ( stp->port ), stp->flags );
00125         }
00126         netdev_link_unblock ( netdev );
00127         rc = 0;
00128 
00129  done:
00130         free_iob ( iobuf );
00131         return rc;
00132 }
00133 
00134 /**
00135  * Transcribe STP address
00136  *
00137  * @v net_addr          STP address
00138  * @ret string          "<STP>"
00139  *
00140  * This operation is meaningless for the STP protocol.
00141  */
00142 static const char * stp_ntoa ( const void *net_addr __unused ) {
00143         return "<STP>";
00144 }
00145 
00146 /** STP network protocol */
00147 struct net_protocol stp_protocol __net_protocol = {
00148         .name = "STP",
00149         .net_proto = htons ( ETH_P_STP ),
00150         .rx = stp_rx,
00151         .ntoa = stp_ntoa,
00152 };