2019-05-27 09:55:01 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2005-04-17 02:20:36 +04:00
/*
* Spanning tree protocol ; BPDU handling
* Linux ethernet bridge
*
* Authors :
* Lennert Buytenhek < buytenh @ gnu . org >
*/
# include <linux/kernel.h>
# include <linux/netfilter_bridge.h>
2006-03-21 09:59:06 +03:00
# include <linux/etherdevice.h>
# include <linux/llc.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2013-02-11 12:22:22 +04:00
# include <linux/pkt_sched.h>
2007-09-17 22:53:39 +04:00
# include <net/net_namespace.h>
2006-03-21 09:59:49 +03:00
# include <net/llc.h>
2006-03-21 09:59:06 +03:00
# include <net/llc_pdu.h>
2008-07-06 08:25:56 +04:00
# include <net/stp.h>
2006-03-24 08:39:47 +03:00
# include <asm/unaligned.h>
2005-04-17 02:20:36 +04:00
# include "br_private.h"
# include "br_private_stp.h"
2006-03-21 09:58:49 +03:00
# define STP_HZ 256
2005-04-17 02:20:36 +04:00
2006-03-21 09:59:49 +03:00
# define LLC_RESERVE sizeof(struct llc_pdu_un)
2015-09-16 04:04:18 +03:00
static int br_send_bpdu_finish ( struct net * net , struct sock * sk ,
struct sk_buff * skb )
2015-09-16 04:04:06 +03:00
{
return dev_queue_xmit ( skb ) ;
}
2006-03-21 09:59:49 +03:00
static void br_send_bpdu ( struct net_bridge_port * p ,
2007-02-09 17:24:35 +03:00
const unsigned char * data , int length )
2005-04-17 02:20:36 +04:00
{
struct sk_buff * skb ;
2006-03-21 09:59:49 +03:00
skb = dev_alloc_skb ( length + LLC_RESERVE ) ;
if ( ! skb )
2005-04-17 02:20:36 +04:00
return ;
2006-03-21 09:59:49 +03:00
skb - > dev = p - > dev ;
2005-04-17 02:20:36 +04:00
skb - > protocol = htons ( ETH_P_802_2 ) ;
2013-02-11 12:22:22 +04:00
skb - > priority = TC_PRIO_CONTROL ;
2006-03-21 09:59:49 +03:00
skb_reserve ( skb , LLC_RESERVE ) ;
net: introduce __skb_put_[zero, data, u8]
follow Johannes Berg, semantic patch file as below,
@@
identifier p, p2;
expression len;
expression skb;
type t, t2;
@@
(
-p = __skb_put(skb, len);
+p = __skb_put_zero(skb, len);
|
-p = (t)__skb_put(skb, len);
+p = __skb_put_zero(skb, len);
)
... when != p
(
p2 = (t2)p;
-memset(p2, 0, len);
|
-memset(p, 0, len);
)
@@
identifier p;
expression len;
expression skb;
type t;
@@
(
-t p = __skb_put(skb, len);
+t p = __skb_put_zero(skb, len);
)
... when != p
(
-memset(p, 0, len);
)
@@
type t, t2;
identifier p, p2;
expression skb;
@@
t *p;
...
(
-p = __skb_put(skb, sizeof(t));
+p = __skb_put_zero(skb, sizeof(t));
|
-p = (t *)__skb_put(skb, sizeof(t));
+p = __skb_put_zero(skb, sizeof(t));
)
... when != p
(
p2 = (t2)p;
-memset(p2, 0, sizeof(*p));
|
-memset(p, 0, sizeof(*p));
)
@@
expression skb, len;
@@
-memset(__skb_put(skb, len), 0, len);
+__skb_put_zero(skb, len);
@@
expression skb, len, data;
@@
-memcpy(__skb_put(skb, len), data, len);
+__skb_put_data(skb, data, len);
@@
expression SKB, C, S;
typedef u8;
identifier fn = {__skb_put};
fresh identifier fn2 = fn ## "_u8";
@@
- *(u8 *)fn(SKB, S) = C;
+ fn2(SKB, C);
Signed-off-by: yuan linyu <Linyu.Yuan@alcatel-sbell.com.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-06-18 17:48:17 +03:00
__skb_put_data ( skb , data , length ) ;
2006-03-21 09:59:49 +03:00
llc_pdu_header_init ( skb , LLC_PDU_TYPE_U , LLC_SAP_BSPAN ,
LLC_SAP_BSPAN , LLC_PDU_CMD ) ;
llc_pdu_init_as_ui_cmd ( skb ) ;
llc_mac_hdr_init ( skb , p - > dev - > dev_addr , p - > br - > group_addr ) ;
2005-04-17 02:20:36 +04:00
2011-01-03 07:16:28 +03:00
skb_reset_mac_header ( skb ) ;
2015-09-16 04:04:16 +03:00
NF_HOOK ( NFPROTO_BRIDGE , NF_BR_LOCAL_OUT ,
dev_net ( p - > dev ) , NULL , skb , NULL , skb - > dev ,
2015-09-16 04:04:06 +03:00
br_send_bpdu_finish ) ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 09:58:49 +03:00
static inline void br_set_ticks ( unsigned char * dest , int j )
2005-04-17 02:20:36 +04:00
{
2006-03-21 09:58:49 +03:00
unsigned long ticks = ( STP_HZ * j ) / HZ ;
2005-04-17 02:20:36 +04:00
2008-05-03 03:26:16 +04:00
put_unaligned_be16 ( ticks , dest ) ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 09:58:49 +03:00
static inline int br_get_ticks ( const unsigned char * src )
2005-04-17 02:20:36 +04:00
{
2008-05-03 03:26:16 +04:00
unsigned long ticks = get_unaligned_be16 ( src ) ;
2006-03-21 09:58:49 +03:00
2007-08-29 02:50:33 +04:00
return DIV_ROUND_UP ( ticks * HZ , STP_HZ ) ;
2005-04-17 02:20:36 +04:00
}
/* called under bridge lock */
void br_send_config_bpdu ( struct net_bridge_port * p , struct br_config_bpdu * bpdu )
{
2006-03-21 09:59:49 +03:00
unsigned char buf [ 35 ] ;
2007-03-22 00:22:44 +03:00
if ( p - > br - > stp_enabled ! = BR_KERNEL_STP )
return ;
2006-03-21 09:59:49 +03:00
buf [ 0 ] = 0 ;
buf [ 1 ] = 0 ;
buf [ 2 ] = 0 ;
buf [ 3 ] = BPDU_TYPE_CONFIG ;
buf [ 4 ] = ( bpdu - > topology_change ? 0x01 : 0 ) |
2005-04-17 02:20:36 +04:00
( bpdu - > topology_change_ack ? 0x80 : 0 ) ;
2006-03-21 09:59:49 +03:00
buf [ 5 ] = bpdu - > root . prio [ 0 ] ;
buf [ 6 ] = bpdu - > root . prio [ 1 ] ;
buf [ 7 ] = bpdu - > root . addr [ 0 ] ;
buf [ 8 ] = bpdu - > root . addr [ 1 ] ;
buf [ 9 ] = bpdu - > root . addr [ 2 ] ;
buf [ 10 ] = bpdu - > root . addr [ 3 ] ;
buf [ 11 ] = bpdu - > root . addr [ 4 ] ;
buf [ 12 ] = bpdu - > root . addr [ 5 ] ;
buf [ 13 ] = ( bpdu - > root_path_cost > > 24 ) & 0xFF ;
buf [ 14 ] = ( bpdu - > root_path_cost > > 16 ) & 0xFF ;
buf [ 15 ] = ( bpdu - > root_path_cost > > 8 ) & 0xFF ;
buf [ 16 ] = bpdu - > root_path_cost & 0xFF ;
buf [ 17 ] = bpdu - > bridge_id . prio [ 0 ] ;
buf [ 18 ] = bpdu - > bridge_id . prio [ 1 ] ;
buf [ 19 ] = bpdu - > bridge_id . addr [ 0 ] ;
buf [ 20 ] = bpdu - > bridge_id . addr [ 1 ] ;
buf [ 21 ] = bpdu - > bridge_id . addr [ 2 ] ;
buf [ 22 ] = bpdu - > bridge_id . addr [ 3 ] ;
buf [ 23 ] = bpdu - > bridge_id . addr [ 4 ] ;
buf [ 24 ] = bpdu - > bridge_id . addr [ 5 ] ;
buf [ 25 ] = ( bpdu - > port_id > > 8 ) & 0xFF ;
buf [ 26 ] = bpdu - > port_id & 0xFF ;
br_set_ticks ( buf + 27 , bpdu - > message_age ) ;
br_set_ticks ( buf + 29 , bpdu - > max_age ) ;
br_set_ticks ( buf + 31 , bpdu - > hello_time ) ;
br_set_ticks ( buf + 33 , bpdu - > forward_delay ) ;
br_send_bpdu ( p , buf , 35 ) ;
2019-12-12 04:07:10 +03:00
p - > stp_xstats . tx_bpdu + + ;
2005-04-17 02:20:36 +04:00
}
/* called under bridge lock */
void br_send_tcn_bpdu ( struct net_bridge_port * p )
{
2006-03-21 09:59:49 +03:00
unsigned char buf [ 4 ] ;
2007-03-22 00:22:44 +03:00
if ( p - > br - > stp_enabled ! = BR_KERNEL_STP )
return ;
2006-03-21 09:59:49 +03:00
buf [ 0 ] = 0 ;
buf [ 1 ] = 0 ;
buf [ 2 ] = 0 ;
buf [ 3 ] = BPDU_TYPE_TCN ;
2006-09-14 07:12:40 +04:00
br_send_bpdu ( p , buf , 4 ) ;
2019-12-12 04:07:10 +03:00
p - > stp_xstats . tx_tcn + + ;
2005-04-17 02:20:36 +04:00
}
2006-03-21 09:59:06 +03:00
/*
* Called from llc .
*
2010-07-27 12:26:30 +04:00
* NO locks , but rcu_read_lock
2006-03-21 09:59:06 +03:00
*/
2008-07-06 08:25:56 +04:00
void br_stp_rcv ( const struct stp_proto * proto , struct sk_buff * skb ,
struct net_device * dev )
2005-04-17 02:20:36 +04:00
{
2010-06-15 10:50:45 +04:00
struct net_bridge_port * p ;
2006-02-10 04:08:52 +03:00
struct net_bridge * br ;
2006-03-21 09:59:06 +03:00
const unsigned char * buf ;
2005-04-17 02:20:36 +04:00
2006-03-21 09:59:06 +03:00
if ( ! pskb_may_pull ( skb , 4 ) )
goto err ;
/* compare of protocol id and version */
buf = skb - > data ;
if ( buf [ 0 ] ! = 0 | | buf [ 1 ] ! = 0 | | buf [ 2 ] ! = 0 )
goto err ;
2006-02-10 04:08:52 +03:00
2013-12-05 19:27:37 +04:00
p = br_port_get_check_rcu ( dev ) ;
2010-11-15 09:38:13 +03:00
if ( ! p )
goto err ;
2006-03-21 09:59:06 +03:00
br = p - > br ;
spin_lock ( & br - > lock ) ;
2006-02-10 04:08:52 +03:00
2007-03-22 00:22:44 +03:00
if ( br - > stp_enabled ! = BR_KERNEL_STP )
goto out ;
if ( ! ( br - > dev - > flags & IFF_UP ) )
goto out ;
if ( p - > state = = BR_STATE_DISABLED )
2006-02-10 04:08:52 +03:00
goto out ;
2005-05-30 01:15:55 +04:00
2019-07-02 15:00:21 +03:00
if ( ! ether_addr_equal ( eth_hdr ( skb ) - > h_dest , br - > group_addr ) )
2006-02-10 04:08:52 +03:00
goto out ;
2005-04-17 02:20:36 +04:00
2012-11-13 11:53:07 +04:00
if ( p - > flags & BR_BPDU_GUARD ) {
br_notice ( br , " BPDU received on blocked port %u(%s) \n " ,
( unsigned int ) p - > port_no , p - > dev - > name ) ;
br_stp_disable_port ( p ) ;
goto out ;
}
2006-03-21 09:59:06 +03:00
buf = skb_pull ( skb , 3 ) ;
2005-04-17 02:20:36 +04:00
if ( buf [ 0 ] = = BPDU_TYPE_CONFIG ) {
struct br_config_bpdu bpdu ;
if ( ! pskb_may_pull ( skb , 32 ) )
2006-03-21 09:59:06 +03:00
goto out ;
2005-04-17 02:20:36 +04:00
buf = skb - > data ;
bpdu . topology_change = ( buf [ 1 ] & 0x01 ) ? 1 : 0 ;
bpdu . topology_change_ack = ( buf [ 1 ] & 0x80 ) ? 1 : 0 ;
bpdu . root . prio [ 0 ] = buf [ 2 ] ;
bpdu . root . prio [ 1 ] = buf [ 3 ] ;
bpdu . root . addr [ 0 ] = buf [ 4 ] ;
bpdu . root . addr [ 1 ] = buf [ 5 ] ;
bpdu . root . addr [ 2 ] = buf [ 6 ] ;
bpdu . root . addr [ 3 ] = buf [ 7 ] ;
bpdu . root . addr [ 4 ] = buf [ 8 ] ;
bpdu . root . addr [ 5 ] = buf [ 9 ] ;
bpdu . root_path_cost =
( buf [ 10 ] < < 24 ) |
( buf [ 11 ] < < 16 ) |
( buf [ 12 ] < < 8 ) |
buf [ 13 ] ;
bpdu . bridge_id . prio [ 0 ] = buf [ 14 ] ;
bpdu . bridge_id . prio [ 1 ] = buf [ 15 ] ;
bpdu . bridge_id . addr [ 0 ] = buf [ 16 ] ;
bpdu . bridge_id . addr [ 1 ] = buf [ 17 ] ;
bpdu . bridge_id . addr [ 2 ] = buf [ 18 ] ;
bpdu . bridge_id . addr [ 3 ] = buf [ 19 ] ;
bpdu . bridge_id . addr [ 4 ] = buf [ 20 ] ;
bpdu . bridge_id . addr [ 5 ] = buf [ 21 ] ;
bpdu . port_id = ( buf [ 22 ] < < 8 ) | buf [ 23 ] ;
bpdu . message_age = br_get_ticks ( buf + 24 ) ;
bpdu . max_age = br_get_ticks ( buf + 26 ) ;
bpdu . hello_time = br_get_ticks ( buf + 28 ) ;
bpdu . forward_delay = br_get_ticks ( buf + 30 ) ;
2011-07-22 11:47:07 +04:00
if ( bpdu . message_age > bpdu . max_age ) {
if ( net_ratelimit ( ) )
br_notice ( p - > br ,
" port %u config from %pM "
" (message_age %ul > max_age %ul) \n " ,
p - > port_no ,
eth_hdr ( skb ) - > h_source ,
bpdu . message_age , bpdu . max_age ) ;
goto out ;
}
2005-04-17 02:20:36 +04:00
br_received_config_bpdu ( p , & bpdu ) ;
2011-07-22 11:47:10 +04:00
} else if ( buf [ 0 ] = = BPDU_TYPE_TCN ) {
2005-04-17 02:20:36 +04:00
br_received_tcn_bpdu ( p ) ;
}
out :
2006-02-10 04:08:52 +03:00
spin_unlock ( & br - > lock ) ;
2005-04-17 02:20:36 +04:00
err :
kfree_skb ( skb ) ;
}