Blame view

net/bridge/br_netfilter.c 28 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*
   *	Handle firewalling
   *	Linux ethernet bridge
   *
   *	Authors:
   *	Lennert Buytenhek               <buytenh@gnu.org>
   *	Bart De Schuymer (maintainer)	<bdschuym@pandora.be>
   *
   *	Changes:
   *	Apr 29 2003: physdev module support (bdschuym)
   *	Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
   *	Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
   *	             (bdschuym)
   *	Sep 01 2004: add IPv6 filtering (bdschuym)
   *
   *	This program is free software; you can redistribute it and/or
   *	modify it under the terms of the GNU General Public License
   *	as published by the Free Software Foundation; either version
   *	2 of the License, or (at your option) any later version.
   *
   *	Lennert dedicates this file to Kerstin Wurdinger.
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
26
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
  #include <linux/ip.h>
  #include <linux/netdevice.h>
  #include <linux/skbuff.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
30
  #include <linux/if_arp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
  #include <linux/if_ether.h>
  #include <linux/if_vlan.h>
516299d2f   Michael Milner   [NETFILTER]: brid...
33
34
  #include <linux/if_pppox.h>
  #include <linux/ppp_defs.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
35
36
37
38
39
  #include <linux/netfilter_bridge.h>
  #include <linux/netfilter_ipv4.h>
  #include <linux/netfilter_ipv6.h>
  #include <linux/netfilter_arp.h>
  #include <linux/in_route.h>
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
40
  #include <linux/inetdevice.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
41

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
  #include <net/ip.h>
  #include <net/ipv6.h>
14c850212   Arnaldo Carvalho de Melo   [INET_SOCK]: Move...
44
  #include <net/route.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  #include <asm/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
49
50
51
52
  #include "br_private.h"
  #ifdef CONFIG_SYSCTL
  #include <linux/sysctl.h>
  #endif
  
  #define skb_origaddr(skb)	 (((struct bridge_skb_cb *) \
  				 (skb->nf_bridge->data))->daddr.ipv4)
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
53
54
  #define store_orig_dstaddr(skb)	 (skb_origaddr(skb) = ip_hdr(skb)->daddr)
  #define dnat_took_place(skb)	 (skb_origaddr(skb) != ip_hdr(skb)->daddr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
  #ifdef CONFIG_SYSCTL
  static struct ctl_table_header *brnf_sysctl_header;
9c1ea148a   Brian Haley   [BRIDGE]: Change ...
58
59
60
  static int brnf_call_iptables __read_mostly = 1;
  static int brnf_call_ip6tables __read_mostly = 1;
  static int brnf_call_arptables __read_mostly = 1;
47e0e1ca1   Herbert Xu   netfilter 03/09: ...
61
62
  static int brnf_filter_vlan_tagged __read_mostly = 0;
  static int brnf_filter_pppoe_tagged __read_mostly = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  #else
47e0e1ca1   Herbert Xu   netfilter 03/09: ...
64
65
  #define brnf_filter_vlan_tagged 0
  #define brnf_filter_pppoe_tagged 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  #endif
b6f99a211   Dave Jones   [NET]: fix up mis...
67
  static inline __be16 vlan_proto(const struct sk_buff *skb)
8b42ec392   Stephen Hemminger   [BRIDGE]: netfilt...
68
69
70
71
72
73
  {
  	return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
  }
  
  #define IS_VLAN_IP(skb) \
  	(skb->protocol == htons(ETH_P_8021Q) && \
9d6f229fc   YOSHIFUJI Hideaki   [NET] BRIDGE: Fix...
74
  	 vlan_proto(skb) == htons(ETH_P_IP) && 	\
8b42ec392   Stephen Hemminger   [BRIDGE]: netfilt...
75
76
77
78
79
80
81
82
83
84
85
  	 brnf_filter_vlan_tagged)
  
  #define IS_VLAN_IPV6(skb) \
  	(skb->protocol == htons(ETH_P_8021Q) && \
  	 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
  	 brnf_filter_vlan_tagged)
  
  #define IS_VLAN_ARP(skb) \
  	(skb->protocol == htons(ETH_P_8021Q) &&	\
  	 vlan_proto(skb) == htons(ETH_P_ARP) &&	\
  	 brnf_filter_vlan_tagged)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86

516299d2f   Michael Milner   [NETFILTER]: brid...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  static inline __be16 pppoe_proto(const struct sk_buff *skb)
  {
  	return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
  			    sizeof(struct pppoe_hdr)));
  }
  
  #define IS_PPPOE_IP(skb) \
  	(skb->protocol == htons(ETH_P_PPP_SES) && \
  	 pppoe_proto(skb) == htons(PPP_IP) && \
  	 brnf_filter_pppoe_tagged)
  
  #define IS_PPPOE_IPV6(skb) \
  	(skb->protocol == htons(ETH_P_PPP_SES) && \
  	 pppoe_proto(skb) == htons(PPP_IPV6) && \
  	 brnf_filter_pppoe_tagged)
631339f1e   Herbert Xu   bridge: netfilter...
102
103
104
105
106
107
  static void fake_update_pmtu(struct dst_entry *dst, u32 mtu)
  {
  }
  
  static struct dst_ops fake_dst_ops = {
  	.family =		AF_INET,
09640e636   Harvey Harrison   net: replace uses...
108
  	.protocol =		cpu_to_be16(ETH_P_IP),
631339f1e   Herbert Xu   bridge: netfilter...
109
  	.update_pmtu =		fake_update_pmtu,
631339f1e   Herbert Xu   bridge: netfilter...
110
111
  	.entries =		ATOMIC_INIT(0),
  };
4adf0af68   Simon Wunderlich   bridge: send corr...
112
113
  /*
   * Initialize bogus route table used to keep netfilter happy.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
   * Currently, we fill in the PMTU entry because netfilter
   * refragmentation needs it, and the rt_flags entry because
   * ipt_REJECT needs it.  Future netfilter modules might
4adf0af68   Simon Wunderlich   bridge: send corr...
117
118
119
120
121
122
123
   * require us to fill additional fields.
   */
  void br_netfilter_rtable_init(struct net_bridge *br)
  {
  	struct rtable *rt = &br->fake_rtable;
  
  	atomic_set(&rt->u.dst.__refcnt, 1);
ad619800e   Rami Rosen   bridge: fix compi...
124
  	rt->u.dst.dev = br->dev;
4adf0af68   Simon Wunderlich   bridge: send corr...
125
126
127
  	rt->u.dst.path = &rt->u.dst;
  	rt->u.dst.metrics[RTAX_MTU - 1] = 1500;
  	rt->u.dst.flags	= DST_NOXFRM;
631339f1e   Herbert Xu   bridge: netfilter...
128
  	rt->u.dst.ops = &fake_dst_ops;
4adf0af68   Simon Wunderlich   bridge: send corr...
129
130
131
132
133
134
135
136
  }
  
  static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
  {
  	struct net_bridge_port *port = rcu_dereference(dev->br_port);
  
  	return port ? &port->br->fake_rtable : NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137

5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
138
139
140
141
142
143
  static inline struct net_device *bridge_parent(const struct net_device *dev)
  {
  	struct net_bridge_port *port = rcu_dereference(dev->br_port);
  
  	return port ? port->br->dev : NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144

fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
145
146
147
148
149
150
151
152
  static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
  {
  	skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
  	if (likely(skb->nf_bridge))
  		atomic_set(&(skb->nf_bridge->use), 1);
  
  	return skb->nf_bridge;
  }
2dc2f207f   Patrick McHardy   [NETFILTER]: brid...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
  {
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  
  	if (atomic_read(&nf_bridge->use) > 1) {
  		struct nf_bridge_info *tmp = nf_bridge_alloc(skb);
  
  		if (tmp) {
  			memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info));
  			atomic_set(&tmp->use, 1);
  			nf_bridge_put(nf_bridge);
  		}
  		nf_bridge = tmp;
  	}
  	return nf_bridge;
  }
fc38582db   Patrick McHardy   [NETFILTER]: brid...
169
170
171
172
173
174
175
176
177
  static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
  {
  	unsigned int len = nf_bridge_encap_header_len(skb);
  
  	skb_push(skb, len);
  	skb->network_header -= len;
  }
  
  static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
178
  {
fc38582db   Patrick McHardy   [NETFILTER]: brid...
179
180
181
182
183
  	unsigned int len = nf_bridge_encap_header_len(skb);
  
  	skb_pull(skb, len);
  	skb->network_header += len;
  }
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
184

fc38582db   Patrick McHardy   [NETFILTER]: brid...
185
186
187
188
189
190
191
192
193
194
195
  static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
  {
  	unsigned int len = nf_bridge_encap_header_len(skb);
  
  	skb_pull_rcsum(skb, len);
  	skb->network_header += len;
  }
  
  static inline void nf_bridge_save_header(struct sk_buff *skb)
  {
  	int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
196

d626f62b1   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
197
198
  	skb_copy_from_linear_data_offset(skb, -header_size,
  					 skb->nf_bridge->data, header_size);
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
199
  }
07317621d   Stephen Hemminger   [NETFILTER] bridg...
200
201
202
203
204
205
206
  /*
   * When forwarding bridge frames, we save a copy of the original
   * header before processing.
   */
  int nf_bridge_copy_header(struct sk_buff *skb)
  {
  	int err;
fc38582db   Patrick McHardy   [NETFILTER]: brid...
207
  	int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
07317621d   Stephen Hemminger   [NETFILTER] bridg...
208

d9cc20484   Herbert Xu   [NET] skbuff: Add...
209
  	err = skb_cow_head(skb, header_size);
07317621d   Stephen Hemminger   [NETFILTER] bridg...
210
211
  	if (err)
  		return err;
27d7ff46a   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
212
213
  	skb_copy_to_linear_data_offset(skb, -header_size,
  				       skb->nf_bridge->data, header_size);
fc38582db   Patrick McHardy   [NETFILTER]: brid...
214
  	__skb_push(skb, nf_bridge_encap_header_len(skb));
07317621d   Stephen Hemminger   [NETFILTER] bridg...
215
216
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
219
220
221
222
  /* PF_BRIDGE/PRE_ROUTING *********************************************/
  /* Undo the changes made for ip6tables PREROUTING and continue the
   * bridge PRE_ROUTING hook. */
  static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
  {
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
511c3f92a   Eric Dumazet   net: skb->rtable ...
223
  	struct rtable *rt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
229
  	if (nf_bridge->mask & BRNF_PKT_TYPE) {
  		skb->pkt_type = PACKET_OTHERHOST;
  		nf_bridge->mask ^= BRNF_PKT_TYPE;
  	}
  	nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
511c3f92a   Eric Dumazet   net: skb->rtable ...
230
231
  	rt = bridge_parent_rtable(nf_bridge->physindev);
  	if (!rt) {
4adf0af68   Simon Wunderlich   bridge: send corr...
232
233
234
  		kfree_skb(skb);
  		return 0;
  	}
511c3f92a   Eric Dumazet   net: skb->rtable ...
235
  	dst_hold(&rt->u.dst);
adf30907d   Eric Dumazet   net: skb->dst acc...
236
  	skb_dst_set(skb, &rt->u.dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
  
  	skb->dev = nf_bridge->physindev;
fc38582db   Patrick McHardy   [NETFILTER]: brid...
239
  	nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
247
248
249
250
251
  	NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  		       br_handle_frame_finish, 1);
  
  	return 0;
  }
  
  static void __br_dnat_complain(void)
  {
  	static unsigned long last_complaint;
  
  	if (jiffies - last_complaint >= 5 * HZ) {
  		printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
252
253
  		       "forwarding to be enabled
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  		last_complaint = jiffies;
  	}
  }
  
  /* This requires some explaining. If DNAT has taken place,
   * we will need to fix up the destination Ethernet address,
   * and this is a tricky process.
   *
   * There are two cases to consider:
   * 1. The packet was DNAT'ed to a device in the same bridge
   *    port group as it was received on. We can still bridge
   *    the packet.
   * 2. The packet was DNAT'ed to a different device, either
   *    a non-bridged device or another bridge port group.
   *    The packet will need to be routed.
   *
   * The correct way of distinguishing between these two cases is to
   * call ip_route_input() and to look at skb->dst->dev, which is
   * changed to the destination device if ip_route_input() succeeds.
   *
   * Let us first consider the case that ip_route_input() succeeds:
   *
   * If skb->dst->dev equals the logical bridge device the packet
2948d2ebb   Patrick McHardy   [NETFILTER]: brid...
277
278
279
   * came in on, we can consider this bridging. The packet is passed
   * through the neighbour output function to build a new destination
   * MAC address, which will make the packet enter br_nf_local_out()
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
281
282
283
284
   * not much later. In that function it is assured that the iptables
   * FORWARD chain is traversed for the packet.
   *
   * Otherwise, the packet is considered to be routed and we just
   * change the destination MAC address so that the packet will
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
285
286
287
   * later be passed up to the IP stack to be routed. For a redirected
   * packet, ip_route_input() will give back the localhost as output device,
   * which differs from the bridge device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
   *
   * Let us now consider the case that ip_route_input() fails:
   *
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
291
292
   * This can be because the destination address is martian, in which case
   * the packet will be dropped.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
296
297
298
299
300
301
302
303
304
   * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
   * will fail, while __ip_route_output_key() will return success. The source
   * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
   * thinks we're handling a locally generated packet and won't care
   * if IP forwarding is allowed. We send a warning message to the users's
   * log telling her to put IP forwarding on.
   *
   * ip_route_input() will also fail if there is no route available.
   * In that case we just drop the packet.
   *
   * --Lennert, 20020411
   * --Bart, 20020416 (updated)
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
305
306
   * --Bart, 20021007 (updated)
   * --Bart, 20062711 (updated) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
  static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
310
311
312
313
314
315
  	if (skb->pkt_type == PACKET_OTHERHOST) {
  		skb->pkt_type = PACKET_HOST;
  		skb->nf_bridge->mask |= BRNF_PKT_TYPE;
  	}
  	skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
  
  	skb->dev = bridge_parent(skb->dev);
2948d2ebb   Patrick McHardy   [NETFILTER]: brid...
316
  	if (skb->dev) {
adf30907d   Eric Dumazet   net: skb->dst acc...
317
  		struct dst_entry *dst = skb_dst(skb);
2948d2ebb   Patrick McHardy   [NETFILTER]: brid...
318

fc38582db   Patrick McHardy   [NETFILTER]: brid...
319
  		nf_bridge_pull_encap_header(skb);
2948d2ebb   Patrick McHardy   [NETFILTER]: brid...
320
321
322
323
324
  
  		if (dst->hh)
  			return neigh_hh_output(dst->hh, skb);
  		else if (dst->neighbour)
  			return dst->neighbour->output(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
  	}
2948d2ebb   Patrick McHardy   [NETFILTER]: brid...
326
  	kfree_skb(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
332
  	return 0;
  }
  
  static int br_nf_pre_routing_finish(struct sk_buff *skb)
  {
  	struct net_device *dev = skb->dev;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
333
  	struct iphdr *iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
511c3f92a   Eric Dumazet   net: skb->rtable ...
335
  	struct rtable *rt;
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
336
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
340
341
342
  	if (nf_bridge->mask & BRNF_PKT_TYPE) {
  		skb->pkt_type = PACKET_OTHERHOST;
  		nf_bridge->mask ^= BRNF_PKT_TYPE;
  	}
  	nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  	if (dnat_took_place(skb)) {
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
344
  		if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
345
346
347
348
349
350
351
352
353
  			struct flowi fl = {
  				.nl_u = {
  					.ip4_u = {
  						 .daddr = iph->daddr,
  						 .saddr = 0,
  						 .tos = RT_TOS(iph->tos) },
  				},
  				.proto = 0,
  			};
f3abc9b96   Eric Dumazet   netfilter: bridge...
354
  			struct in_device *in_dev = __in_dev_get_rcu(dev);
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
355
356
357
358
359
360
361
362
363
364
  
  			/* If err equals -EHOSTUNREACH the error is due to a
  			 * martian destination or due to the fact that
  			 * forwarding is disabled. For most martian packets,
  			 * ip_route_output_key() will fail. It won't fail for 2 types of
  			 * martian destinations: loopback destinations and destination
  			 * 0.0.0.0. In both cases the packet will be dropped because the
  			 * destination is the loopback device and not the bridge. */
  			if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
  				goto free_skb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365

249b62035   Alexey Dobriyan   netfilter: netns ...
366
  			if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
1c011bed5   Bart De Schuymer   [BRIDGE-NF]: Fix ...
367
  				/* - Bridged-and-DNAT'ed traffic doesn't
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
368
369
  				 *   require ip_forwarding. */
  				if (((struct dst_entry *)rt)->dev == dev) {
adf30907d   Eric Dumazet   net: skb->dst acc...
370
  					skb_dst_set(skb, (struct dst_entry *)rt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
372
  					goto bridged_dnat;
  				}
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
373
374
375
376
  				/* we are sure that forwarding is disabled, so printing
  				 * this message is no problem. Note that the packet could
  				 * still have a martian destination address, in which case
  				 * the packet could be dropped even if forwarding were enabled */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
  				__br_dnat_complain();
  				dst_release((struct dst_entry *)rt);
  			}
f216f082b   Bart De Schuymer   [NETFILTER]: brid...
380
  free_skb:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
381
382
383
  			kfree_skb(skb);
  			return 0;
  		} else {
adf30907d   Eric Dumazet   net: skb->dst acc...
384
  			if (skb_dst(skb)->dev == dev) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
386
387
388
389
  bridged_dnat:
  				/* Tell br_nf_local_out this is a
  				 * bridged frame */
  				nf_bridge->mask |= BRNF_BRIDGED_DNAT;
  				skb->dev = nf_bridge->physindev;
fc38582db   Patrick McHardy   [NETFILTER]: brid...
390
  				nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
395
396
  				NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
  					       skb, skb->dev, NULL,
  					       br_nf_pre_routing_finish_bridge,
  					       1);
  				return 0;
  			}
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
397
  			memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398
399
400
  			skb->pkt_type = PACKET_HOST;
  		}
  	} else {
511c3f92a   Eric Dumazet   net: skb->rtable ...
401
402
  		rt = bridge_parent_rtable(nf_bridge->physindev);
  		if (!rt) {
4adf0af68   Simon Wunderlich   bridge: send corr...
403
404
405
  			kfree_skb(skb);
  			return 0;
  		}
511c3f92a   Eric Dumazet   net: skb->rtable ...
406
  		dst_hold(&rt->u.dst);
adf30907d   Eric Dumazet   net: skb->dst acc...
407
  		skb_dst_set(skb, &rt->u.dst);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
  	}
  
  	skb->dev = nf_bridge->physindev;
fc38582db   Patrick McHardy   [NETFILTER]: brid...
411
  	nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
414
415
416
417
418
  	NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
  		       br_handle_frame_finish, 1);
  
  	return 0;
  }
  
  /* Some common code for IPv4/IPv6 */
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
419
  static struct net_device *setup_pre_routing(struct sk_buff *skb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
427
428
429
430
  {
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  
  	if (skb->pkt_type == PACKET_OTHERHOST) {
  		skb->pkt_type = PACKET_HOST;
  		nf_bridge->mask |= BRNF_PKT_TYPE;
  	}
  
  	nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
  	nf_bridge->physindev = skb->dev;
  	skb->dev = bridge_parent(skb->dev);
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
431
432
  
  	return skb->dev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
436
437
  }
  
  /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
  static int check_hbh_len(struct sk_buff *skb)
  {
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
438
  	unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
  	u32 pkt_len;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
440
441
  	const unsigned char *nh = skb_network_header(skb);
  	int off = raw - nh;
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
442
  	int len = (raw[1] + 1) << 3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
447
448
449
450
  
  	if ((raw + len) - skb->data > skb_headlen(skb))
  		goto bad;
  
  	off += 2;
  	len -= 2;
  
  	while (len > 0) {
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
451
  		int optlen = nh[off + 1] + 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452

d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
453
  		switch (nh[off]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
455
456
457
458
459
460
461
  		case IPV6_TLV_PAD0:
  			optlen = 1;
  			break;
  
  		case IPV6_TLV_PADN:
  			break;
  
  		case IPV6_TLV_JUMBO:
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
462
  			if (nh[off + 1] != 4 || (off & 3) != 2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
  				goto bad;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
464
  			pkt_len = ntohl(*(__be32 *) (nh + off + 2));
b03664869   Bart De Schuymer   [BRIDGE-NF]: Fix ...
465
  			if (pkt_len <= IPV6_MAXPLEN ||
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
466
  			    ipv6_hdr(skb)->payload_len)
b03664869   Bart De Schuymer   [BRIDGE-NF]: Fix ...
467
  				goto bad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
469
  			if (pkt_len > skb->len - sizeof(struct ipv6hdr))
  				goto bad;
b03664869   Bart De Schuymer   [BRIDGE-NF]: Fix ...
470
  			if (pskb_trim_rcsum(skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
471
  					    pkt_len + sizeof(struct ipv6hdr)))
b03664869   Bart De Schuymer   [BRIDGE-NF]: Fix ...
472
  				goto bad;
d56f90a7c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
473
  			nh = skb_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
  			break;
  		default:
  			if (optlen > len)
  				goto bad;
  			break;
  		}
  		off += optlen;
  		len -= optlen;
  	}
  	if (len == 0)
  		return 0;
  bad:
  	return -1;
  
  }
  
  /* Replicate the checks that IPv6 does on packet reception and pass the packet
   * to ip6tables, which doesn't support NAT, so things are fairly simple. */
  static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
493
494
495
496
  					   struct sk_buff *skb,
  					   const struct net_device *in,
  					   const struct net_device *out,
  					   int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
498
499
  {
  	struct ipv6hdr *hdr;
  	u32 pkt_len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
501
502
503
504
505
  
  	if (skb->len < sizeof(struct ipv6hdr))
  		goto inhdr_error;
  
  	if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  		goto inhdr_error;
0660e03f6   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
506
  	hdr = ipv6_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
514
515
  
  	if (hdr->version != 6)
  		goto inhdr_error;
  
  	pkt_len = ntohs(hdr->payload_len);
  
  	if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
  		if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
  			goto inhdr_error;
b38dfee3d   Herbert Xu   [NET]: skb_trim a...
516
517
  		if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
  			goto inhdr_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
  	}
  	if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
520
  		goto inhdr_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521

789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
522
  	nf_bridge_put(skb->nf_bridge);
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
523
  	if (!nf_bridge_alloc(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
  		return NF_DROP;
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
525
526
  	if (!setup_pre_routing(skb))
  		return NF_DROP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527

6e23ae2a4   Patrick McHardy   [NETFILTER]: Intr...
528
  	NF_HOOK(PF_INET6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  		br_nf_pre_routing_finish_ipv6);
  
  	return NF_STOLEN;
  
  inhdr_error:
  	return NF_DROP;
  }
  
  /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
   * Replicate the checks that IPv4 does on packet reception.
   * Set skb->dev to the bridge device (i.e. parent of the
   * receiving device) to make netfilter happy, the REDIRECT
   * target in particular.  Save the original destination IP
   * address to be able to detect DNAT afterwards. */
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
543
  static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
ee02b3a61   Stephen Hemminger   [BRIDGE] netfilte...
544
545
546
  				      const struct net_device *in,
  				      const struct net_device *out,
  				      int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
548
  {
  	struct iphdr *iph;
e7c243c92   Evgeniy Polyakov   [VLAN/BRIDGE]: Fi...
549
  	__u32 len = nf_bridge_encap_header_len(skb);
e7c243c92   Evgeniy Polyakov   [VLAN/BRIDGE]: Fi...
550
551
  	if (unlikely(!pskb_may_pull(skb, len)))
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552

516299d2f   Michael Milner   [NETFILTER]: brid...
553
554
  	if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
  	    IS_PPPOE_IPV6(skb)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
557
558
  #ifdef CONFIG_SYSCTL
  		if (!brnf_call_ip6tables)
  			return NF_ACCEPT;
  #endif
fc38582db   Patrick McHardy   [NETFILTER]: brid...
559
  		nf_bridge_pull_encap_header_rcsum(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
  		return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
  	}
  #ifdef CONFIG_SYSCTL
  	if (!brnf_call_iptables)
  		return NF_ACCEPT;
  #endif
516299d2f   Michael Milner   [NETFILTER]: brid...
566
567
  	if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
  	    !IS_PPPOE_IP(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  		return NF_ACCEPT;
fc38582db   Patrick McHardy   [NETFILTER]: brid...
569
  	nf_bridge_pull_encap_header_rcsum(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
570
571
572
  
  	if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  		goto inhdr_error;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
573
  	iph = ip_hdr(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
  	if (iph->ihl < 5 || iph->version != 4)
  		goto inhdr_error;
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
576
  	if (!pskb_may_pull(skb, 4 * iph->ihl))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
  		goto inhdr_error;
eddc9ec53   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
578
  	iph = ip_hdr(skb);
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
579
  	if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
581
582
  		goto inhdr_error;
  
  	len = ntohs(iph->tot_len);
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
583
  	if (skb->len < len || len < 4 * iph->ihl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
  		goto inhdr_error;
b38dfee3d   Herbert Xu   [NET]: skb_trim a...
585
  	pskb_trim_rcsum(skb, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586

789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
587
  	nf_bridge_put(skb->nf_bridge);
fdeabdefb   Stephen Hemminger   [BRIDGE]: netfilt...
588
  	if (!nf_bridge_alloc(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  		return NF_DROP;
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
590
591
  	if (!setup_pre_routing(skb))
  		return NF_DROP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
  	store_orig_dstaddr(skb);
6e23ae2a4   Patrick McHardy   [NETFILTER]: Intr...
593
  	NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, skb->dev, NULL,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
  		br_nf_pre_routing_finish);
  
  	return NF_STOLEN;
  
  inhdr_error:
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
599
  //      IP_INC_STATS_BH(IpInHdrErrors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
603
604
605
606
607
608
609
610
611
  out:
  	return NF_DROP;
  }
  
  
  /* PF_BRIDGE/LOCAL_IN ************************************************/
  /* The packet is locally destined, which requires a real
   * dst_entry, so detach the fake one.  On the way up, the
   * packet would pass through PRE_ROUTING again (which already
   * took place when the packet entered the bridge), but we
   * register an IPv4 PRE_ROUTING 'sabotage' hook that will
   * prevent this from happening. */
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
612
  static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
613
614
615
  				   const struct net_device *in,
  				   const struct net_device *out,
  				   int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
616
  {
511c3f92a   Eric Dumazet   net: skb->rtable ...
617
  	struct rtable *rt = skb_rtable(skb);
adf30907d   Eric Dumazet   net: skb->dst acc...
618
619
  	if (rt && rt == bridge_parent_rtable(in))
  		skb_dst_drop(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
620
621
622
  
  	return NF_ACCEPT;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
624
625
626
627
  /* PF_BRIDGE/FORWARD *************************************************/
  static int br_nf_forward_finish(struct sk_buff *skb)
  {
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
  	struct net_device *in;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628

8b42ec392   Stephen Hemminger   [BRIDGE]: netfilt...
629
  	if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630
631
632
633
634
635
636
637
  		in = nf_bridge->physindev;
  		if (nf_bridge->mask & BRNF_PKT_TYPE) {
  			skb->pkt_type = PACKET_OTHERHOST;
  			nf_bridge->mask ^= BRNF_PKT_TYPE;
  		}
  	} else {
  		in = *((struct net_device **)(skb->cb));
  	}
fc38582db   Patrick McHardy   [NETFILTER]: brid...
638
  	nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
639
  	NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
640
  		       skb->dev, br_forward_finish, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
643
644
645
646
647
648
  	return 0;
  }
  
  /* This is the 'purely bridged' case.  For IP, we pass the packet to
   * netfilter with indev and outdev set to the bridge device,
   * but we are still able to filter on the 'real' indev/outdev
   * because of the physdev module. For ARP, indev and outdev are the
   * bridge ports. */
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
649
  static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
650
651
652
  				     const struct net_device *in,
  				     const struct net_device *out,
  				     int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
  	struct nf_bridge_info *nf_bridge;
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
655
  	struct net_device *parent;
76108cea0   Jan Engelhardt   netfilter: Use un...
656
  	u_int8_t pf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
  
  	if (!skb->nf_bridge)
  		return NF_ACCEPT;
2dc2f207f   Patrick McHardy   [NETFILTER]: brid...
660
661
662
663
  	/* Need exclusive nf_bridge_info since we might have multiple
  	 * different physoutdevs. */
  	if (!nf_bridge_unshare(skb))
  		return NF_DROP;
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
664
665
666
  	parent = bridge_parent(out);
  	if (!parent)
  		return NF_DROP;
516299d2f   Michael Milner   [NETFILTER]: brid...
667
668
  	if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
  	    IS_PPPOE_IP(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
  		pf = PF_INET;
a2bd40ad3   Herbert Xu   netfilter 02/09: ...
670
671
  	else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
  		 IS_PPPOE_IPV6(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  		pf = PF_INET6;
a2bd40ad3   Herbert Xu   netfilter 02/09: ...
673
674
  	else
  		return NF_ACCEPT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
675

3db05fea5   Herbert Xu   [NETFILTER]: Repl...
676
  	nf_bridge_pull_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
677

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
678
679
680
681
682
683
684
685
686
  	nf_bridge = skb->nf_bridge;
  	if (skb->pkt_type == PACKET_OTHERHOST) {
  		skb->pkt_type = PACKET_HOST;
  		nf_bridge->mask |= BRNF_PKT_TYPE;
  	}
  
  	/* The physdev module checks on this */
  	nf_bridge->mask |= BRNF_BRIDGED;
  	nf_bridge->physoutdev = skb->dev;
6e23ae2a4   Patrick McHardy   [NETFILTER]: Intr...
687
  	NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
688
  		br_nf_forward_finish);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
689
690
691
  
  	return NF_STOLEN;
  }
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
692
  static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
693
694
695
  				      const struct net_device *in,
  				      const struct net_device *out,
  				      int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
697
698
699
700
701
702
  	struct net_device **d = (struct net_device **)(skb->cb);
  
  #ifdef CONFIG_SYSCTL
  	if (!brnf_call_arptables)
  		return NF_ACCEPT;
  #endif
f8a260286   Stephen Hemminger   [BRIDGE]: netfilt...
703
  	if (skb->protocol != htons(ETH_P_ARP)) {
8b42ec392   Stephen Hemminger   [BRIDGE]: netfilt...
704
  		if (!IS_VLAN_ARP(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705
  			return NF_ACCEPT;
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
706
  		nf_bridge_pull_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
707
  	}
d0a92be05   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
708
  	if (arp_hdr(skb)->ar_pln != 4) {
fc38582db   Patrick McHardy   [NETFILTER]: brid...
709
  		if (IS_VLAN_ARP(skb))
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
710
  			nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
712
713
  		return NF_ACCEPT;
  	}
  	*d = (struct net_device *)in;
fdc9314cb   Jan Engelhardt   netfilter: replac...
714
  	NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
715
716
717
718
  		(struct net_device *)out, br_nf_forward_finish);
  
  	return NF_STOLEN;
  }
2bf540b73   Patrick McHardy   [NETFILTER]: brid...
719
720
721
  /* PF_BRIDGE/LOCAL_OUT ***********************************************
   *
   * This function sees both locally originated IP packets and forwarded
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
722
723
   * IP packets (in both cases the destination device is a bridge
   * device). It also sees bridged-and-DNAT'ed packets.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
724
725
726
727
728
729
   *
   * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
   * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
   * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
   * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
   * will be executed.
2bf540b73   Patrick McHardy   [NETFILTER]: brid...
730
   */
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
731
  static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
732
733
734
  				    const struct net_device *in,
  				    const struct net_device *out,
  				    int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
  {
2bf540b73   Patrick McHardy   [NETFILTER]: brid...
736
  	struct net_device *realindev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
  	struct nf_bridge_info *nf_bridge;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
739
740
  
  	if (!skb->nf_bridge)
  		return NF_ACCEPT;
2dc2f207f   Patrick McHardy   [NETFILTER]: brid...
741
742
743
744
  	/* Need exclusive nf_bridge_info since we might have multiple
  	 * different physoutdevs. */
  	if (!nf_bridge_unshare(skb))
  		return NF_DROP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
745
  	nf_bridge = skb->nf_bridge;
2bf540b73   Patrick McHardy   [NETFILTER]: brid...
746
747
  	if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
  		return NF_ACCEPT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748
749
750
  
  	/* Bridged, take PF_BRIDGE/FORWARD.
  	 * (see big note in front of br_nf_pre_routing_finish) */
2bf540b73   Patrick McHardy   [NETFILTER]: brid...
751
752
  	nf_bridge->physoutdev = skb->dev;
  	realindev = nf_bridge->physindev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753

2bf540b73   Patrick McHardy   [NETFILTER]: brid...
754
755
756
  	if (nf_bridge->mask & BRNF_PKT_TYPE) {
  		skb->pkt_type = PACKET_OTHERHOST;
  		nf_bridge->mask ^= BRNF_PKT_TYPE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
  	}
fc38582db   Patrick McHardy   [NETFILTER]: brid...
758
  	nf_bridge_push_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
759

2bf540b73   Patrick McHardy   [NETFILTER]: brid...
760
761
  	NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
  		br_forward_finish);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
  	return NF_STOLEN;
  }
c197facc8   hummerbliss@gmail.com   netfilter: bridge...
764
  #if defined(CONFIG_NF_CONNTRACK_IPV4) || defined(CONFIG_NF_CONNTRACK_IPV4_MODULE)
2e2f7aefa   Patrick McHardy   [NETFILTER]: Fix ...
765
766
  static int br_nf_dev_queue_xmit(struct sk_buff *skb)
  {
c197facc8   hummerbliss@gmail.com   netfilter: bridge...
767
768
  	if (skb->nfct != NULL &&
  	    (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb)) &&
2e2f7aefa   Patrick McHardy   [NETFILTER]: Fix ...
769
  	    skb->len > skb->dev->mtu &&
89114afd4   Herbert Xu   [NET] gso: Add sk...
770
  	    !skb_is_gso(skb))
2e2f7aefa   Patrick McHardy   [NETFILTER]: Fix ...
771
772
773
774
  		return ip_fragment(skb, br_dev_queue_push_xmit);
  	else
  		return br_dev_queue_push_xmit(skb);
  }
c197facc8   hummerbliss@gmail.com   netfilter: bridge...
775
776
777
778
779
780
  #else
  static int br_nf_dev_queue_xmit(struct sk_buff *skb)
  {
          return br_dev_queue_push_xmit(skb);
  }
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
781
782
  
  /* PF_BRIDGE/POST_ROUTING ********************************************/
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
783
  static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
784
785
786
  				       const struct net_device *in,
  				       const struct net_device *out,
  				       int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
  {
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
788
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
  	struct net_device *realoutdev = bridge_parent(skb->dev);
76108cea0   Jan Engelhardt   netfilter: Use un...
790
  	u_int8_t pf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
791
792
793
794
  
  #ifdef CONFIG_NETFILTER_DEBUG
  	/* Be very paranoid. This probably won't happen anymore, but let's
  	 * keep the check just to be sure... */
98e399f82   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
795
796
  	if (skb_mac_header(skb) < skb->head ||
  	    skb_mac_header(skb) + ETH_HLEN > skb->data) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
797
  		printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
8394e9b2f   Stephen Hemminger   [NETFILTER] bridg...
798
799
  		       "bad mac.raw pointer.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
800
801
802
803
804
805
  		goto print_error;
  	}
  #endif
  
  	if (!nf_bridge)
  		return NF_ACCEPT;
81d9ddae8   Patrick McHardy   [NETFILTER]: brid...
806
807
  	if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
  		return NF_ACCEPT;
5dce971ac   Stephen Hemminger   [BRIDGE]: netfilt...
808
809
  	if (!realoutdev)
  		return NF_DROP;
516299d2f   Michael Milner   [NETFILTER]: brid...
810
811
  	if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
  	    IS_PPPOE_IP(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
  		pf = PF_INET;
a2bd40ad3   Herbert Xu   netfilter 02/09: ...
813
814
  	else if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
  		 IS_PPPOE_IPV6(skb))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
815
  		pf = PF_INET6;
a2bd40ad3   Herbert Xu   netfilter 02/09: ...
816
817
  	else
  		return NF_ACCEPT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
819
  
  #ifdef CONFIG_NETFILTER_DEBUG
adf30907d   Eric Dumazet   net: skb->dst acc...
820
  	if (skb_dst(skb) == NULL) {
8394e9b2f   Stephen Hemminger   [NETFILTER] bridg...
821
822
  		printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
824
  		goto print_error;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
827
828
829
830
831
832
  #endif
  
  	/* We assume any code from br_dev_queue_push_xmit onwards doesn't care
  	 * about the value of skb->pkt_type. */
  	if (skb->pkt_type == PACKET_OTHERHOST) {
  		skb->pkt_type = PACKET_HOST;
  		nf_bridge->mask |= BRNF_PKT_TYPE;
  	}
fc38582db   Patrick McHardy   [NETFILTER]: brid...
833
  	nf_bridge_pull_encap_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
  	nf_bridge_save_header(skb);
6e23ae2a4   Patrick McHardy   [NETFILTER]: Intr...
835
  	NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev,
2e2f7aefa   Patrick McHardy   [NETFILTER]: Fix ...
836
  		br_nf_dev_queue_xmit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
837
838
839
840
841
842
843
  
  	return NF_STOLEN;
  
  #ifdef CONFIG_NETFILTER_DEBUG
  print_error:
  	if (skb->dev != NULL) {
  		printk("[%s]", skb->dev->name);
178a3259f   Stephen Hemminger   [BRIDGE]: Better ...
844
845
  		if (realoutdev)
  			printk("[%s]", realoutdev->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
  	}
98e399f82   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
847
848
  	printk(" head:%p, raw:%p, data:%p
  ", skb->head, skb_mac_header(skb),
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
849
  	       skb->data);
8394e9b2f   Stephen Hemminger   [NETFILTER] bridg...
850
  	dump_stack();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
  	return NF_ACCEPT;
  #endif
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
855
856
  /* IP/SABOTAGE *****************************************************/
  /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
   * for the second time. */
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
857
  static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
858
859
860
  				   const struct net_device *in,
  				   const struct net_device *out,
  				   int (*okfn)(struct sk_buff *))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
861
  {
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
862
863
  	if (skb->nf_bridge &&
  	    !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
864
865
866
867
868
  		return NF_STOP;
  	}
  
  	return NF_ACCEPT;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
871
872
  /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
   * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
   * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
   * ip_refrag() can return NF_STOLEN. */
1999414a4   Patrick McHardy   [NETFILTER]: Mark...
873
  static struct nf_hook_ops br_nf_ops[] __read_mostly = {
1490fd894   Cyrill Gorcunov   net, bridge: alig...
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
  	{
  		.hook = br_nf_pre_routing,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_PRE_ROUTING,
  		.priority = NF_BR_PRI_BRNF,
  	},
  	{
  		.hook = br_nf_local_in,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_LOCAL_IN,
  		.priority = NF_BR_PRI_BRNF,
  	},
  	{
  		.hook = br_nf_forward_ip,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_FORWARD,
  		.priority = NF_BR_PRI_BRNF - 1,
  	},
  	{
  		.hook = br_nf_forward_arp,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_FORWARD,
  		.priority = NF_BR_PRI_BRNF,
  	},
  	{
  		.hook = br_nf_local_out,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_LOCAL_OUT,
  		.priority = NF_BR_PRI_FIRST,
  	},
  	{
  		.hook = br_nf_post_routing,
  		.owner = THIS_MODULE,
  		.pf = PF_BRIDGE,
  		.hooknum = NF_BR_POST_ROUTING,
  		.priority = NF_BR_PRI_LAST,
  	},
  	{
  		.hook = ip_sabotage_in,
  		.owner = THIS_MODULE,
  		.pf = PF_INET,
  		.hooknum = NF_INET_PRE_ROUTING,
  		.priority = NF_IP_PRI_FIRST,
  	},
  	{
  		.hook = ip_sabotage_in,
  		.owner = THIS_MODULE,
  		.pf = PF_INET6,
  		.hooknum = NF_INET_PRE_ROUTING,
  		.priority = NF_IP6_PRI_FIRST,
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
930
931
932
933
  };
  
  #ifdef CONFIG_SYSCTL
  static
8d65af789   Alexey Dobriyan   sysctl: remove "s...
934
  int brnf_sysctl_call_tables(ctl_table * ctl, int write,
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
935
  			    void __user * buffer, size_t * lenp, loff_t * ppos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
936
937
  {
  	int ret;
8d65af789   Alexey Dobriyan   sysctl: remove "s...
938
  	ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
942
943
944
945
946
  
  	if (write && *(int *)(ctl->data))
  		*(int *)(ctl->data) = 1;
  	return ret;
  }
  
  static ctl_table brnf_table[] = {
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947
948
949
950
  		.procname	= "bridge-nf-call-arptables",
  		.data		= &brnf_call_arptables,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
6d9f239a1   Alexey Dobriyan   net: '&' redux
951
  		.proc_handler	= brnf_sysctl_call_tables,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
952
953
  	},
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
955
956
957
  		.procname	= "bridge-nf-call-iptables",
  		.data		= &brnf_call_iptables,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
6d9f239a1   Alexey Dobriyan   net: '&' redux
958
  		.proc_handler	= brnf_sysctl_call_tables,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
  	},
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
961
962
963
964
  		.procname	= "bridge-nf-call-ip6tables",
  		.data		= &brnf_call_ip6tables,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
6d9f239a1   Alexey Dobriyan   net: '&' redux
965
  		.proc_handler	= brnf_sysctl_call_tables,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
966
967
  	},
  	{
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
969
970
971
  		.procname	= "bridge-nf-filter-vlan-tagged",
  		.data		= &brnf_filter_vlan_tagged,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
6d9f239a1   Alexey Dobriyan   net: '&' redux
972
  		.proc_handler	= brnf_sysctl_call_tables,
516299d2f   Michael Milner   [NETFILTER]: brid...
973
974
  	},
  	{
516299d2f   Michael Milner   [NETFILTER]: brid...
975
976
977
978
  		.procname	= "bridge-nf-filter-pppoe-tagged",
  		.data		= &brnf_filter_pppoe_tagged,
  		.maxlen		= sizeof(int),
  		.mode		= 0644,
6d9f239a1   Alexey Dobriyan   net: '&' redux
979
  		.proc_handler	= brnf_sysctl_call_tables,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
980
  	},
f8572d8f2   Eric W. Biederman   sysctl net: Remov...
981
  	{ }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
  };
b5ccd792f   Pavel Emelyanov   [NET]: Simple ctl...
983
  static struct ctl_path brnf_path[] = {
f8572d8f2   Eric W. Biederman   sysctl net: Remov...
984
985
  	{ .procname = "net", },
  	{ .procname = "bridge", },
b5ccd792f   Pavel Emelyanov   [NET]: Simple ctl...
986
  	{ }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
  };
  #endif
5eb87f456   Patrick McHardy   [NETFILTER]: brid...
989
  int __init br_netfilter_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
990
  {
5eb87f456   Patrick McHardy   [NETFILTER]: brid...
991
  	int ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
992

5eb87f456   Patrick McHardy   [NETFILTER]: brid...
993
994
  	ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  	if (ret < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
995
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
996
  #ifdef CONFIG_SYSCTL
b5ccd792f   Pavel Emelyanov   [NET]: Simple ctl...
997
  	brnf_sysctl_header = register_sysctl_paths(brnf_path, brnf_table);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
  	if (brnf_sysctl_header == NULL) {
789bc3e5b   Stephen Hemminger   [BRIDGE]: netfilt...
999
1000
1001
  		printk(KERN_WARNING
  		       "br_netfilter: can't register to sysctl.
  ");
5eb87f456   Patrick McHardy   [NETFILTER]: brid...
1002
1003
  		nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004
1005
  	}
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1006
1007
  	printk(KERN_NOTICE "Bridge firewalling registered
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1008
1009
1010
1011
1012
  	return 0;
  }
  
  void br_netfilter_fini(void)
  {
5eb87f456   Patrick McHardy   [NETFILTER]: brid...
1013
  	nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1014
1015
1016
1017
  #ifdef CONFIG_SYSCTL
  	unregister_sysctl_table(brnf_sysctl_header);
  #endif
  }