xref: /openbmc/linux/net/bridge/br_forward.c (revision 4bedea94)
1 /*
2  *	Forwarding decision
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	$Id: br_forward.c,v 1.4 2001/08/14 22:05:57 davem Exp $
9  *
10  *	This program is free software; you can redistribute it and/or
11  *	modify it under the terms of the GNU General Public License
12  *	as published by the Free Software Foundation; either version
13  *	2 of the License, or (at your option) any later version.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/netfilter_bridge.h>
20 #include "br_private.h"
21 
22 static inline int should_deliver(const struct net_bridge_port *p,
23 				 const struct sk_buff *skb)
24 {
25 	if (skb->dev == p->dev ||
26 	    p->state != BR_STATE_FORWARDING)
27 		return 0;
28 
29 	return 1;
30 }
31 
32 int br_dev_queue_push_xmit(struct sk_buff *skb)
33 {
34 	if (skb->len > skb->dev->mtu)
35 		kfree_skb(skb);
36 	else {
37 #ifdef CONFIG_BRIDGE_NETFILTER
38 		/* ip_refrag calls ip_fragment, doesn't copy the MAC header. */
39 		nf_bridge_maybe_copy_header(skb);
40 #endif
41 		skb_push(skb, ETH_HLEN);
42 
43 		dev_queue_xmit(skb);
44 	}
45 
46 	return 0;
47 }
48 
49 int br_forward_finish(struct sk_buff *skb)
50 {
51 	NF_HOOK(PF_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
52 			br_dev_queue_push_xmit);
53 
54 	return 0;
55 }
56 
57 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
58 {
59 	skb->dev = to->dev;
60 	NF_HOOK(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
61 			br_forward_finish);
62 }
63 
64 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
65 {
66 	struct net_device *indev;
67 
68 	indev = skb->dev;
69 	skb->dev = to->dev;
70 	skb->ip_summed = CHECKSUM_NONE;
71 
72 	NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
73 			br_forward_finish);
74 }
75 
76 /* called with rcu_read_lock */
77 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
78 {
79 	if (should_deliver(to, skb)) {
80 		__br_deliver(to, skb);
81 		return;
82 	}
83 
84 	kfree_skb(skb);
85 }
86 
87 /* called with rcu_read_lock */
88 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
89 {
90 	if (should_deliver(to, skb)) {
91 		__br_forward(to, skb);
92 		return;
93 	}
94 
95 	kfree_skb(skb);
96 }
97 
98 /* called under bridge lock */
99 static void br_flood(struct net_bridge *br, struct sk_buff *skb, int clone,
100 	void (*__packet_hook)(const struct net_bridge_port *p,
101 			      struct sk_buff *skb))
102 {
103 	struct net_bridge_port *p;
104 	struct net_bridge_port *prev;
105 
106 	if (clone) {
107 		struct sk_buff *skb2;
108 
109 		if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
110 			br->statistics.tx_dropped++;
111 			return;
112 		}
113 
114 		skb = skb2;
115 	}
116 
117 	prev = NULL;
118 
119 	list_for_each_entry_rcu(p, &br->port_list, list) {
120 		if (should_deliver(p, skb)) {
121 			if (prev != NULL) {
122 				struct sk_buff *skb2;
123 
124 				if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
125 					br->statistics.tx_dropped++;
126 					kfree_skb(skb);
127 					return;
128 				}
129 
130 				__packet_hook(prev, skb2);
131 			}
132 
133 			prev = p;
134 		}
135 	}
136 
137 	if (prev != NULL) {
138 		__packet_hook(prev, skb);
139 		return;
140 	}
141 
142 	kfree_skb(skb);
143 }
144 
145 
146 /* called with rcu_read_lock */
147 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, int clone)
148 {
149 	br_flood(br, skb, clone, __br_deliver);
150 }
151 
152 /* called under bridge lock */
153 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, int clone)
154 {
155 	br_flood(br, skb, clone, __br_forward);
156 }
157