]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/br_netfilter.c
[IPV6] ROUTE: Make sending algorithm more friendly with RFC 4861.
[net-next-2.6.git] / net / bridge / br_netfilter.c
CommitLineData
1da177e4
LT
1/*
2 * Handle firewalling
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
8 *
9 * Changes:
10 * Apr 29 2003: physdev module support (bdschuym)
11 * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
12 * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
13 * (bdschuym)
14 * Sep 01 2004: add IPv6 filtering (bdschuym)
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 * Lennert dedicates this file to Kerstin Wurdinger.
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/ip.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
14c85021 29#include <linux/if_arp.h>
1da177e4
LT
30#include <linux/if_ether.h>
31#include <linux/if_vlan.h>
516299d2
MM
32#include <linux/if_pppox.h>
33#include <linux/ppp_defs.h>
1da177e4
LT
34#include <linux/netfilter_bridge.h>
35#include <linux/netfilter_ipv4.h>
36#include <linux/netfilter_ipv6.h>
37#include <linux/netfilter_arp.h>
38#include <linux/in_route.h>
f216f082 39#include <linux/inetdevice.h>
14c85021 40
1da177e4
LT
41#include <net/ip.h>
42#include <net/ipv6.h>
14c85021
ACM
43#include <net/route.h>
44
1da177e4 45#include <asm/uaccess.h>
1da177e4
LT
46#include "br_private.h"
47#ifdef CONFIG_SYSCTL
48#include <linux/sysctl.h>
49#endif
50
51#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
52 (skb->nf_bridge->data))->daddr.ipv4)
eddc9ec5
ACM
53#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
54#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
1da177e4 55
1da177e4
LT
56#ifdef CONFIG_SYSCTL
57static struct ctl_table_header *brnf_sysctl_header;
9c1ea148
BH
58static int brnf_call_iptables __read_mostly = 1;
59static int brnf_call_ip6tables __read_mostly = 1;
60static int brnf_call_arptables __read_mostly = 1;
61static int brnf_filter_vlan_tagged __read_mostly = 1;
516299d2 62static int brnf_filter_pppoe_tagged __read_mostly = 1;
1da177e4
LT
63#else
64#define brnf_filter_vlan_tagged 1
516299d2 65#define brnf_filter_pppoe_tagged 1
1da177e4
LT
66#endif
67
b6f99a21 68static inline __be16 vlan_proto(const struct sk_buff *skb)
8b42ec39
SH
69{
70 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
71}
72
73#define IS_VLAN_IP(skb) \
74 (skb->protocol == htons(ETH_P_8021Q) && \
9d6f229f 75 vlan_proto(skb) == htons(ETH_P_IP) && \
8b42ec39
SH
76 brnf_filter_vlan_tagged)
77
78#define IS_VLAN_IPV6(skb) \
79 (skb->protocol == htons(ETH_P_8021Q) && \
80 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
81 brnf_filter_vlan_tagged)
82
83#define IS_VLAN_ARP(skb) \
84 (skb->protocol == htons(ETH_P_8021Q) && \
85 vlan_proto(skb) == htons(ETH_P_ARP) && \
86 brnf_filter_vlan_tagged)
1da177e4 87
516299d2
MM
88static inline __be16 pppoe_proto(const struct sk_buff *skb)
89{
90 return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
91 sizeof(struct pppoe_hdr)));
92}
93
94#define IS_PPPOE_IP(skb) \
95 (skb->protocol == htons(ETH_P_PPP_SES) && \
96 pppoe_proto(skb) == htons(PPP_IP) && \
97 brnf_filter_pppoe_tagged)
98
99#define IS_PPPOE_IPV6(skb) \
100 (skb->protocol == htons(ETH_P_PPP_SES) && \
101 pppoe_proto(skb) == htons(PPP_IPV6) && \
102 brnf_filter_pppoe_tagged)
103
1da177e4
LT
104/* We need these fake structures to make netfilter happy --
105 * lots of places assume that skb->dst != NULL, which isn't
106 * all that unreasonable.
107 *
108 * Currently, we fill in the PMTU entry because netfilter
109 * refragmentation needs it, and the rt_flags entry because
110 * ipt_REJECT needs it. Future netfilter modules might
111 * require us to fill additional fields. */
112static struct net_device __fake_net_device = {
113 .hard_header_len = ETH_HLEN
114};
115
116static struct rtable __fake_rtable = {
117 .u = {
118 .dst = {
119 .__refcnt = ATOMIC_INIT(1),
120 .dev = &__fake_net_device,
121 .path = &__fake_rtable.u.dst,
122 .metrics = {[RTAX_MTU - 1] = 1500},
42cf93cd 123 .flags = DST_NOXFRM,
1da177e4
LT
124 }
125 },
126 .rt_flags = 0,
127};
128
5dce971a
SH
129static inline struct net_device *bridge_parent(const struct net_device *dev)
130{
131 struct net_bridge_port *port = rcu_dereference(dev->br_port);
132
133 return port ? port->br->dev : NULL;
134}
1da177e4 135
fdeabdef
SH
136static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
137{
138 skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
139 if (likely(skb->nf_bridge))
140 atomic_set(&(skb->nf_bridge->use), 1);
141
142 return skb->nf_bridge;
143}
144
fc38582d
PM
145static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
146{
147 unsigned int len = nf_bridge_encap_header_len(skb);
148
149 skb_push(skb, len);
150 skb->network_header -= len;
151}
152
153static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
fdeabdef 154{
fc38582d
PM
155 unsigned int len = nf_bridge_encap_header_len(skb);
156
157 skb_pull(skb, len);
158 skb->network_header += len;
159}
fdeabdef 160
fc38582d
PM
161static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
162{
163 unsigned int len = nf_bridge_encap_header_len(skb);
164
165 skb_pull_rcsum(skb, len);
166 skb->network_header += len;
167}
168
169static inline void nf_bridge_save_header(struct sk_buff *skb)
170{
171 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
fdeabdef 172
d626f62b
ACM
173 skb_copy_from_linear_data_offset(skb, -header_size,
174 skb->nf_bridge->data, header_size);
fdeabdef
SH
175}
176
07317621
SH
177/*
178 * When forwarding bridge frames, we save a copy of the original
179 * header before processing.
180 */
181int nf_bridge_copy_header(struct sk_buff *skb)
182{
183 int err;
fc38582d 184 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
07317621 185
d9cc2048 186 err = skb_cow_head(skb, header_size);
07317621
SH
187 if (err)
188 return err;
189
27d7ff46
ACM
190 skb_copy_to_linear_data_offset(skb, -header_size,
191 skb->nf_bridge->data, header_size);
fc38582d 192 __skb_push(skb, nf_bridge_encap_header_len(skb));
07317621
SH
193 return 0;
194}
195
1da177e4
LT
196/* PF_BRIDGE/PRE_ROUTING *********************************************/
197/* Undo the changes made for ip6tables PREROUTING and continue the
198 * bridge PRE_ROUTING hook. */
199static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
200{
201 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
202
1da177e4
LT
203 if (nf_bridge->mask & BRNF_PKT_TYPE) {
204 skb->pkt_type = PACKET_OTHERHOST;
205 nf_bridge->mask ^= BRNF_PKT_TYPE;
206 }
207 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
208
209 skb->dst = (struct dst_entry *)&__fake_rtable;
210 dst_hold(skb->dst);
211
212 skb->dev = nf_bridge->physindev;
fc38582d 213 nf_bridge_push_encap_header(skb);
1da177e4
LT
214 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
215 br_handle_frame_finish, 1);
216
217 return 0;
218}
219
220static void __br_dnat_complain(void)
221{
222 static unsigned long last_complaint;
223
224 if (jiffies - last_complaint >= 5 * HZ) {
225 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
789bc3e5 226 "forwarding to be enabled\n");
1da177e4
LT
227 last_complaint = jiffies;
228 }
229}
230
231/* This requires some explaining. If DNAT has taken place,
232 * we will need to fix up the destination Ethernet address,
233 * and this is a tricky process.
234 *
235 * There are two cases to consider:
236 * 1. The packet was DNAT'ed to a device in the same bridge
237 * port group as it was received on. We can still bridge
238 * the packet.
239 * 2. The packet was DNAT'ed to a different device, either
240 * a non-bridged device or another bridge port group.
241 * The packet will need to be routed.
242 *
243 * The correct way of distinguishing between these two cases is to
244 * call ip_route_input() and to look at skb->dst->dev, which is
245 * changed to the destination device if ip_route_input() succeeds.
246 *
247 * Let us first consider the case that ip_route_input() succeeds:
248 *
249 * If skb->dst->dev equals the logical bridge device the packet
2948d2eb
PM
250 * came in on, we can consider this bridging. The packet is passed
251 * through the neighbour output function to build a new destination
252 * MAC address, which will make the packet enter br_nf_local_out()
1da177e4
LT
253 * not much later. In that function it is assured that the iptables
254 * FORWARD chain is traversed for the packet.
255 *
256 * Otherwise, the packet is considered to be routed and we just
257 * change the destination MAC address so that the packet will
f216f082
BDS
258 * later be passed up to the IP stack to be routed. For a redirected
259 * packet, ip_route_input() will give back the localhost as output device,
260 * which differs from the bridge device.
1da177e4
LT
261 *
262 * Let us now consider the case that ip_route_input() fails:
263 *
f216f082
BDS
264 * This can be because the destination address is martian, in which case
265 * the packet will be dropped.
1da177e4
LT
266 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
267 * will fail, while __ip_route_output_key() will return success. The source
268 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
269 * thinks we're handling a locally generated packet and won't care
270 * if IP forwarding is allowed. We send a warning message to the users's
271 * log telling her to put IP forwarding on.
272 *
273 * ip_route_input() will also fail if there is no route available.
274 * In that case we just drop the packet.
275 *
276 * --Lennert, 20020411
277 * --Bart, 20020416 (updated)
f216f082
BDS
278 * --Bart, 20021007 (updated)
279 * --Bart, 20062711 (updated) */
1da177e4
LT
280static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
281{
1da177e4
LT
282 if (skb->pkt_type == PACKET_OTHERHOST) {
283 skb->pkt_type = PACKET_HOST;
284 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
285 }
286 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
287
288 skb->dev = bridge_parent(skb->dev);
2948d2eb
PM
289 if (skb->dev) {
290 struct dst_entry *dst = skb->dst;
291
fc38582d 292 nf_bridge_pull_encap_header(skb);
2948d2eb
PM
293
294 if (dst->hh)
295 return neigh_hh_output(dst->hh, skb);
296 else if (dst->neighbour)
297 return dst->neighbour->output(skb);
1da177e4 298 }
2948d2eb 299 kfree_skb(skb);
1da177e4
LT
300 return 0;
301}
302
303static int br_nf_pre_routing_finish(struct sk_buff *skb)
304{
305 struct net_device *dev = skb->dev;
eddc9ec5 306 struct iphdr *iph = ip_hdr(skb);
1da177e4 307 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
f216f082 308 int err;
1da177e4 309
1da177e4
LT
310 if (nf_bridge->mask & BRNF_PKT_TYPE) {
311 skb->pkt_type = PACKET_OTHERHOST;
312 nf_bridge->mask ^= BRNF_PKT_TYPE;
313 }
314 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
1da177e4 315 if (dnat_took_place(skb)) {
f216f082 316 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
1da177e4 317 struct rtable *rt;
789bc3e5
SH
318 struct flowi fl = {
319 .nl_u = {
320 .ip4_u = {
321 .daddr = iph->daddr,
322 .saddr = 0,
323 .tos = RT_TOS(iph->tos) },
324 },
325 .proto = 0,
326 };
f216f082
BDS
327 struct in_device *in_dev = in_dev_get(dev);
328
329 /* If err equals -EHOSTUNREACH the error is due to a
330 * martian destination or due to the fact that
331 * forwarding is disabled. For most martian packets,
332 * ip_route_output_key() will fail. It won't fail for 2 types of
333 * martian destinations: loopback destinations and destination
334 * 0.0.0.0. In both cases the packet will be dropped because the
335 * destination is the loopback device and not the bridge. */
336 if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
337 goto free_skb;
1da177e4
LT
338
339 if (!ip_route_output_key(&rt, &fl)) {
1c011bed 340 /* - Bridged-and-DNAT'ed traffic doesn't
f216f082
BDS
341 * require ip_forwarding. */
342 if (((struct dst_entry *)rt)->dev == dev) {
1da177e4
LT
343 skb->dst = (struct dst_entry *)rt;
344 goto bridged_dnat;
345 }
f216f082
BDS
346 /* we are sure that forwarding is disabled, so printing
347 * this message is no problem. Note that the packet could
348 * still have a martian destination address, in which case
349 * the packet could be dropped even if forwarding were enabled */
1da177e4
LT
350 __br_dnat_complain();
351 dst_release((struct dst_entry *)rt);
352 }
f216f082 353free_skb:
1da177e4
LT
354 kfree_skb(skb);
355 return 0;
356 } else {
357 if (skb->dst->dev == dev) {
358bridged_dnat:
359 /* Tell br_nf_local_out this is a
360 * bridged frame */
361 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
362 skb->dev = nf_bridge->physindev;
fc38582d 363 nf_bridge_push_encap_header(skb);
1da177e4
LT
364 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
365 skb, skb->dev, NULL,
366 br_nf_pre_routing_finish_bridge,
367 1);
368 return 0;
369 }
789bc3e5 370 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
1da177e4
LT
371 skb->pkt_type = PACKET_HOST;
372 }
373 } else {
374 skb->dst = (struct dst_entry *)&__fake_rtable;
375 dst_hold(skb->dst);
376 }
377
378 skb->dev = nf_bridge->physindev;
fc38582d 379 nf_bridge_push_encap_header(skb);
1da177e4
LT
380 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
381 br_handle_frame_finish, 1);
382
383 return 0;
384}
385
386/* Some common code for IPv4/IPv6 */
5dce971a 387static struct net_device *setup_pre_routing(struct sk_buff *skb)
1da177e4
LT
388{
389 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
390
391 if (skb->pkt_type == PACKET_OTHERHOST) {
392 skb->pkt_type = PACKET_HOST;
393 nf_bridge->mask |= BRNF_PKT_TYPE;
394 }
395
396 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
397 nf_bridge->physindev = skb->dev;
398 skb->dev = bridge_parent(skb->dev);
5dce971a
SH
399
400 return skb->dev;
1da177e4
LT
401}
402
403/* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
404static int check_hbh_len(struct sk_buff *skb)
405{
0660e03f 406 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
1da177e4 407 u32 pkt_len;
d56f90a7
ACM
408 const unsigned char *nh = skb_network_header(skb);
409 int off = raw - nh;
789bc3e5 410 int len = (raw[1] + 1) << 3;
1da177e4
LT
411
412 if ((raw + len) - skb->data > skb_headlen(skb))
413 goto bad;
414
415 off += 2;
416 len -= 2;
417
418 while (len > 0) {
d56f90a7 419 int optlen = nh[off + 1] + 2;
1da177e4 420
d56f90a7 421 switch (nh[off]) {
1da177e4
LT
422 case IPV6_TLV_PAD0:
423 optlen = 1;
424 break;
425
426 case IPV6_TLV_PADN:
427 break;
428
429 case IPV6_TLV_JUMBO:
d56f90a7 430 if (nh[off + 1] != 4 || (off & 3) != 2)
1da177e4 431 goto bad;
d56f90a7 432 pkt_len = ntohl(*(__be32 *) (nh + off + 2));
b0366486 433 if (pkt_len <= IPV6_MAXPLEN ||
0660e03f 434 ipv6_hdr(skb)->payload_len)
b0366486 435 goto bad;
1da177e4
LT
436 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
437 goto bad;
b0366486 438 if (pskb_trim_rcsum(skb,
789bc3e5 439 pkt_len + sizeof(struct ipv6hdr)))
b0366486 440 goto bad;
d56f90a7 441 nh = skb_network_header(skb);
1da177e4
LT
442 break;
443 default:
444 if (optlen > len)
445 goto bad;
446 break;
447 }
448 off += optlen;
449 len -= optlen;
450 }
451 if (len == 0)
452 return 0;
453bad:
454 return -1;
455
456}
457
458/* Replicate the checks that IPv6 does on packet reception and pass the packet
459 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
460static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
789bc3e5
SH
461 struct sk_buff *skb,
462 const struct net_device *in,
463 const struct net_device *out,
464 int (*okfn)(struct sk_buff *))
1da177e4
LT
465{
466 struct ipv6hdr *hdr;
467 u32 pkt_len;
1da177e4
LT
468
469 if (skb->len < sizeof(struct ipv6hdr))
470 goto inhdr_error;
471
472 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
473 goto inhdr_error;
474
0660e03f 475 hdr = ipv6_hdr(skb);
1da177e4
LT
476
477 if (hdr->version != 6)
478 goto inhdr_error;
479
480 pkt_len = ntohs(hdr->payload_len);
481
482 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
483 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
484 goto inhdr_error;
b38dfee3
HX
485 if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr)))
486 goto inhdr_error;
1da177e4
LT
487 }
488 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
789bc3e5 489 goto inhdr_error;
1da177e4 490
789bc3e5 491 nf_bridge_put(skb->nf_bridge);
fdeabdef 492 if (!nf_bridge_alloc(skb))
1da177e4 493 return NF_DROP;
5dce971a
SH
494 if (!setup_pre_routing(skb))
495 return NF_DROP;
1da177e4
LT
496
497 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
498 br_nf_pre_routing_finish_ipv6);
499
500 return NF_STOLEN;
501
502inhdr_error:
503 return NF_DROP;
504}
505
506/* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
507 * Replicate the checks that IPv4 does on packet reception.
508 * Set skb->dev to the bridge device (i.e. parent of the
509 * receiving device) to make netfilter happy, the REDIRECT
510 * target in particular. Save the original destination IP
511 * address to be able to detect DNAT afterwards. */
3db05fea 512static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
ee02b3a6
SH
513 const struct net_device *in,
514 const struct net_device *out,
515 int (*okfn)(struct sk_buff *))
1da177e4
LT
516{
517 struct iphdr *iph;
e7c243c9
EP
518 __u32 len = nf_bridge_encap_header_len(skb);
519
e7c243c9
EP
520 if (unlikely(!pskb_may_pull(skb, len)))
521 goto out;
1da177e4 522
516299d2
MM
523 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb) ||
524 IS_PPPOE_IPV6(skb)) {
1da177e4
LT
525#ifdef CONFIG_SYSCTL
526 if (!brnf_call_ip6tables)
527 return NF_ACCEPT;
528#endif
fc38582d 529 nf_bridge_pull_encap_header_rcsum(skb);
1da177e4
LT
530 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
531 }
532#ifdef CONFIG_SYSCTL
533 if (!brnf_call_iptables)
534 return NF_ACCEPT;
535#endif
536
516299d2
MM
537 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb) &&
538 !IS_PPPOE_IP(skb))
1da177e4
LT
539 return NF_ACCEPT;
540
fc38582d 541 nf_bridge_pull_encap_header_rcsum(skb);
1da177e4
LT
542
543 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
544 goto inhdr_error;
545
eddc9ec5 546 iph = ip_hdr(skb);
1da177e4
LT
547 if (iph->ihl < 5 || iph->version != 4)
548 goto inhdr_error;
549
789bc3e5 550 if (!pskb_may_pull(skb, 4 * iph->ihl))
1da177e4
LT
551 goto inhdr_error;
552
eddc9ec5 553 iph = ip_hdr(skb);
789bc3e5 554 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
1da177e4
LT
555 goto inhdr_error;
556
557 len = ntohs(iph->tot_len);
789bc3e5 558 if (skb->len < len || len < 4 * iph->ihl)
1da177e4
LT
559 goto inhdr_error;
560
b38dfee3 561 pskb_trim_rcsum(skb, len);
1da177e4 562
789bc3e5 563 nf_bridge_put(skb->nf_bridge);
fdeabdef 564 if (!nf_bridge_alloc(skb))
1da177e4 565 return NF_DROP;
5dce971a
SH
566 if (!setup_pre_routing(skb))
567 return NF_DROP;
1da177e4
LT
568 store_orig_dstaddr(skb);
569
570 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
571 br_nf_pre_routing_finish);
572
573 return NF_STOLEN;
574
575inhdr_error:
789bc3e5 576// IP_INC_STATS_BH(IpInHdrErrors);
1da177e4
LT
577out:
578 return NF_DROP;
579}
580
581
582/* PF_BRIDGE/LOCAL_IN ************************************************/
583/* The packet is locally destined, which requires a real
584 * dst_entry, so detach the fake one. On the way up, the
585 * packet would pass through PRE_ROUTING again (which already
586 * took place when the packet entered the bridge), but we
587 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
588 * prevent this from happening. */
3db05fea 589static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
590 const struct net_device *in,
591 const struct net_device *out,
592 int (*okfn)(struct sk_buff *))
1da177e4 593{
1da177e4
LT
594 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
595 dst_release(skb->dst);
596 skb->dst = NULL;
597 }
598
599 return NF_ACCEPT;
600}
601
1da177e4
LT
602/* PF_BRIDGE/FORWARD *************************************************/
603static int br_nf_forward_finish(struct sk_buff *skb)
604{
605 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
606 struct net_device *in;
1da177e4 607
8b42ec39 608 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
1da177e4
LT
609 in = nf_bridge->physindev;
610 if (nf_bridge->mask & BRNF_PKT_TYPE) {
611 skb->pkt_type = PACKET_OTHERHOST;
612 nf_bridge->mask ^= BRNF_PKT_TYPE;
613 }
614 } else {
615 in = *((struct net_device **)(skb->cb));
616 }
fc38582d 617 nf_bridge_push_encap_header(skb);
1da177e4 618 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
789bc3e5 619 skb->dev, br_forward_finish, 1);
1da177e4
LT
620 return 0;
621}
622
623/* This is the 'purely bridged' case. For IP, we pass the packet to
624 * netfilter with indev and outdev set to the bridge device,
625 * but we are still able to filter on the 'real' indev/outdev
626 * because of the physdev module. For ARP, indev and outdev are the
627 * bridge ports. */
3db05fea 628static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
629 const struct net_device *in,
630 const struct net_device *out,
631 int (*okfn)(struct sk_buff *))
1da177e4 632{
1da177e4 633 struct nf_bridge_info *nf_bridge;
5dce971a 634 struct net_device *parent;
1da177e4
LT
635 int pf;
636
637 if (!skb->nf_bridge)
638 return NF_ACCEPT;
639
5dce971a
SH
640 parent = bridge_parent(out);
641 if (!parent)
642 return NF_DROP;
643
516299d2
MM
644 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
645 IS_PPPOE_IP(skb))
1da177e4
LT
646 pf = PF_INET;
647 else
648 pf = PF_INET6;
649
3db05fea 650 nf_bridge_pull_encap_header(skb);
1da177e4 651
1da177e4
LT
652 nf_bridge = skb->nf_bridge;
653 if (skb->pkt_type == PACKET_OTHERHOST) {
654 skb->pkt_type = PACKET_HOST;
655 nf_bridge->mask |= BRNF_PKT_TYPE;
656 }
657
658 /* The physdev module checks on this */
659 nf_bridge->mask |= BRNF_BRIDGED;
660 nf_bridge->physoutdev = skb->dev;
661
5dce971a
SH
662 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
663 br_nf_forward_finish);
1da177e4
LT
664
665 return NF_STOLEN;
666}
667
3db05fea 668static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
669 const struct net_device *in,
670 const struct net_device *out,
671 int (*okfn)(struct sk_buff *))
1da177e4 672{
1da177e4
LT
673 struct net_device **d = (struct net_device **)(skb->cb);
674
675#ifdef CONFIG_SYSCTL
676 if (!brnf_call_arptables)
677 return NF_ACCEPT;
678#endif
679
f8a26028 680 if (skb->protocol != htons(ETH_P_ARP)) {
8b42ec39 681 if (!IS_VLAN_ARP(skb))
1da177e4 682 return NF_ACCEPT;
3db05fea 683 nf_bridge_pull_encap_header(skb);
1da177e4
LT
684 }
685
d0a92be0 686 if (arp_hdr(skb)->ar_pln != 4) {
fc38582d 687 if (IS_VLAN_ARP(skb))
3db05fea 688 nf_bridge_push_encap_header(skb);
1da177e4
LT
689 return NF_ACCEPT;
690 }
691 *d = (struct net_device *)in;
692 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
693 (struct net_device *)out, br_nf_forward_finish);
694
695 return NF_STOLEN;
696}
697
2bf540b7
PM
698/* PF_BRIDGE/LOCAL_OUT ***********************************************
699 *
700 * This function sees both locally originated IP packets and forwarded
1da177e4
LT
701 * IP packets (in both cases the destination device is a bridge
702 * device). It also sees bridged-and-DNAT'ed packets.
1da177e4
LT
703 *
704 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
705 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
706 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
707 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
708 * will be executed.
2bf540b7 709 */
3db05fea 710static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
711 const struct net_device *in,
712 const struct net_device *out,
713 int (*okfn)(struct sk_buff *))
1da177e4 714{
2bf540b7 715 struct net_device *realindev;
1da177e4 716 struct nf_bridge_info *nf_bridge;
1da177e4
LT
717
718 if (!skb->nf_bridge)
719 return NF_ACCEPT;
720
1da177e4 721 nf_bridge = skb->nf_bridge;
2bf540b7
PM
722 if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
723 return NF_ACCEPT;
1da177e4
LT
724
725 /* Bridged, take PF_BRIDGE/FORWARD.
726 * (see big note in front of br_nf_pre_routing_finish) */
2bf540b7
PM
727 nf_bridge->physoutdev = skb->dev;
728 realindev = nf_bridge->physindev;
1da177e4 729
2bf540b7
PM
730 if (nf_bridge->mask & BRNF_PKT_TYPE) {
731 skb->pkt_type = PACKET_OTHERHOST;
732 nf_bridge->mask ^= BRNF_PKT_TYPE;
1da177e4 733 }
fc38582d 734 nf_bridge_push_encap_header(skb);
1da177e4 735
2bf540b7
PM
736 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
737 br_forward_finish);
1da177e4
LT
738 return NF_STOLEN;
739}
740
2e2f7aef
PM
741static int br_nf_dev_queue_xmit(struct sk_buff *skb)
742{
743 if (skb->protocol == htons(ETH_P_IP) &&
744 skb->len > skb->dev->mtu &&
89114afd 745 !skb_is_gso(skb))
2e2f7aef
PM
746 return ip_fragment(skb, br_dev_queue_push_xmit);
747 else
748 return br_dev_queue_push_xmit(skb);
749}
1da177e4
LT
750
751/* PF_BRIDGE/POST_ROUTING ********************************************/
3db05fea 752static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
753 const struct net_device *in,
754 const struct net_device *out,
755 int (*okfn)(struct sk_buff *))
1da177e4 756{
3db05fea 757 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
1da177e4
LT
758 struct net_device *realoutdev = bridge_parent(skb->dev);
759 int pf;
760
761#ifdef CONFIG_NETFILTER_DEBUG
762 /* Be very paranoid. This probably won't happen anymore, but let's
763 * keep the check just to be sure... */
98e399f8
ACM
764 if (skb_mac_header(skb) < skb->head ||
765 skb_mac_header(skb) + ETH_HLEN > skb->data) {
1da177e4 766 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
8394e9b2 767 "bad mac.raw pointer.\n");
1da177e4
LT
768 goto print_error;
769 }
770#endif
771
772 if (!nf_bridge)
773 return NF_ACCEPT;
774
81d9ddae
PM
775 if (!(nf_bridge->mask & (BRNF_BRIDGED | BRNF_BRIDGED_DNAT)))
776 return NF_ACCEPT;
777
5dce971a
SH
778 if (!realoutdev)
779 return NF_DROP;
780
516299d2
MM
781 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb) ||
782 IS_PPPOE_IP(skb))
1da177e4
LT
783 pf = PF_INET;
784 else
785 pf = PF_INET6;
786
787#ifdef CONFIG_NETFILTER_DEBUG
788 if (skb->dst == NULL) {
8394e9b2 789 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
1da177e4
LT
790 goto print_error;
791 }
1da177e4
LT
792#endif
793
794 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
795 * about the value of skb->pkt_type. */
796 if (skb->pkt_type == PACKET_OTHERHOST) {
797 skb->pkt_type = PACKET_HOST;
798 nf_bridge->mask |= BRNF_PKT_TYPE;
799 }
800
fc38582d 801 nf_bridge_pull_encap_header(skb);
1da177e4
LT
802 nf_bridge_save_header(skb);
803
804#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
805 if (nf_bridge->netoutdev)
806 realoutdev = nf_bridge->netoutdev;
807#endif
808 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
2e2f7aef 809 br_nf_dev_queue_xmit);
1da177e4
LT
810
811 return NF_STOLEN;
812
813#ifdef CONFIG_NETFILTER_DEBUG
814print_error:
815 if (skb->dev != NULL) {
816 printk("[%s]", skb->dev->name);
178a3259
SH
817 if (realoutdev)
818 printk("[%s]", realoutdev->name);
1da177e4 819 }
98e399f8 820 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
789bc3e5 821 skb->data);
8394e9b2 822 dump_stack();
1da177e4
LT
823 return NF_ACCEPT;
824#endif
825}
826
1da177e4
LT
827/* IP/SABOTAGE *****************************************************/
828/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
829 * for the second time. */
3db05fea 830static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb,
789bc3e5
SH
831 const struct net_device *in,
832 const struct net_device *out,
833 int (*okfn)(struct sk_buff *))
1da177e4 834{
3db05fea
HX
835 if (skb->nf_bridge &&
836 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
1da177e4
LT
837 return NF_STOP;
838 }
839
840 return NF_ACCEPT;
841}
842
1da177e4
LT
843/* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
844 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
845 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
846 * ip_refrag() can return NF_STOLEN. */
847static struct nf_hook_ops br_nf_ops[] = {
9d6f229f
YH
848 { .hook = br_nf_pre_routing,
849 .owner = THIS_MODULE,
850 .pf = PF_BRIDGE,
851 .hooknum = NF_BR_PRE_ROUTING,
1da177e4
LT
852 .priority = NF_BR_PRI_BRNF, },
853 { .hook = br_nf_local_in,
854 .owner = THIS_MODULE,
855 .pf = PF_BRIDGE,
856 .hooknum = NF_BR_LOCAL_IN,
857 .priority = NF_BR_PRI_BRNF, },
858 { .hook = br_nf_forward_ip,
859 .owner = THIS_MODULE,
860 .pf = PF_BRIDGE,
861 .hooknum = NF_BR_FORWARD,
862 .priority = NF_BR_PRI_BRNF - 1, },
863 { .hook = br_nf_forward_arp,
864 .owner = THIS_MODULE,
865 .pf = PF_BRIDGE,
866 .hooknum = NF_BR_FORWARD,
867 .priority = NF_BR_PRI_BRNF, },
868 { .hook = br_nf_local_out,
869 .owner = THIS_MODULE,
870 .pf = PF_BRIDGE,
871 .hooknum = NF_BR_LOCAL_OUT,
872 .priority = NF_BR_PRI_FIRST, },
873 { .hook = br_nf_post_routing,
874 .owner = THIS_MODULE,
875 .pf = PF_BRIDGE,
876 .hooknum = NF_BR_POST_ROUTING,
877 .priority = NF_BR_PRI_LAST, },
878 { .hook = ip_sabotage_in,
879 .owner = THIS_MODULE,
880 .pf = PF_INET,
881 .hooknum = NF_IP_PRE_ROUTING,
882 .priority = NF_IP_PRI_FIRST, },
883 { .hook = ip_sabotage_in,
884 .owner = THIS_MODULE,
885 .pf = PF_INET6,
886 .hooknum = NF_IP6_PRE_ROUTING,
887 .priority = NF_IP6_PRI_FIRST, },
1da177e4
LT
888};
889
890#ifdef CONFIG_SYSCTL
891static
789bc3e5
SH
892int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
893 void __user * buffer, size_t * lenp, loff_t * ppos)
1da177e4
LT
894{
895 int ret;
896
897 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
898
899 if (write && *(int *)(ctl->data))
900 *(int *)(ctl->data) = 1;
901 return ret;
902}
903
904static ctl_table brnf_table[] = {
905 {
1da177e4
LT
906 .procname = "bridge-nf-call-arptables",
907 .data = &brnf_call_arptables,
908 .maxlen = sizeof(int),
909 .mode = 0644,
910 .proc_handler = &brnf_sysctl_call_tables,
911 },
912 {
1da177e4
LT
913 .procname = "bridge-nf-call-iptables",
914 .data = &brnf_call_iptables,
915 .maxlen = sizeof(int),
916 .mode = 0644,
917 .proc_handler = &brnf_sysctl_call_tables,
918 },
919 {
1da177e4
LT
920 .procname = "bridge-nf-call-ip6tables",
921 .data = &brnf_call_ip6tables,
922 .maxlen = sizeof(int),
923 .mode = 0644,
924 .proc_handler = &brnf_sysctl_call_tables,
925 },
926 {
1da177e4
LT
927 .procname = "bridge-nf-filter-vlan-tagged",
928 .data = &brnf_filter_vlan_tagged,
929 .maxlen = sizeof(int),
930 .mode = 0644,
931 .proc_handler = &brnf_sysctl_call_tables,
516299d2
MM
932 },
933 {
516299d2
MM
934 .procname = "bridge-nf-filter-pppoe-tagged",
935 .data = &brnf_filter_pppoe_tagged,
936 .maxlen = sizeof(int),
937 .mode = 0644,
938 .proc_handler = &brnf_sysctl_call_tables,
1da177e4
LT
939 },
940 { .ctl_name = 0 }
941};
942
943static ctl_table brnf_bridge_table[] = {
944 {
945 .ctl_name = NET_BRIDGE,
946 .procname = "bridge",
947 .mode = 0555,
948 .child = brnf_table,
949 },
950 { .ctl_name = 0 }
951};
952
953static ctl_table brnf_net_table[] = {
954 {
955 .ctl_name = CTL_NET,
956 .procname = "net",
957 .mode = 0555,
958 .child = brnf_bridge_table,
959 },
960 { .ctl_name = 0 }
961};
962#endif
963
5eb87f45 964int __init br_netfilter_init(void)
1da177e4 965{
5eb87f45 966 int ret;
1da177e4 967
5eb87f45
PM
968 ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
969 if (ret < 0)
1da177e4 970 return ret;
1da177e4 971#ifdef CONFIG_SYSCTL
0b4d4147 972 brnf_sysctl_header = register_sysctl_table(brnf_net_table);
1da177e4 973 if (brnf_sysctl_header == NULL) {
789bc3e5
SH
974 printk(KERN_WARNING
975 "br_netfilter: can't register to sysctl.\n");
5eb87f45
PM
976 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
977 return -ENOMEM;
1da177e4
LT
978 }
979#endif
1da177e4 980 printk(KERN_NOTICE "Bridge firewalling registered\n");
1da177e4
LT
981 return 0;
982}
983
984void br_netfilter_fini(void)
985{
5eb87f45 986 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1da177e4
LT
987#ifdef CONFIG_SYSCTL
988 unregister_sysctl_table(brnf_sysctl_header);
989#endif
990}