]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/br_netfilter.c
70a395baaa01de2191317fdf2f06095331042f61
[karo-tx-linux.git] / net / bridge / br_netfilter.c
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>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/netfilter_bridge.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/in_route.h>
37
38 #include <net/ip.h>
39 #include <net/ipv6.h>
40 #include <net/route.h>
41
42 #include <asm/uaccess.h>
43 #include <asm/checksum.h>
44 #include "br_private.h"
45 #ifdef CONFIG_SYSCTL
46 #include <linux/sysctl.h>
47 #endif
48
49 #define skb_origaddr(skb)        (((struct bridge_skb_cb *) \
50                                  (skb->nf_bridge->data))->daddr.ipv4)
51 #define store_orig_dstaddr(skb)  (skb_origaddr(skb) = (skb)->nh.iph->daddr)
52 #define dnat_took_place(skb)     (skb_origaddr(skb) != (skb)->nh.iph->daddr)
53
54 #ifdef CONFIG_SYSCTL
55 static struct ctl_table_header *brnf_sysctl_header;
56 static int brnf_call_iptables = 1;
57 static int brnf_call_ip6tables = 1;
58 static int brnf_call_arptables = 1;
59 static int brnf_filter_vlan_tagged = 1;
60 #else
61 #define brnf_filter_vlan_tagged 1
62 #endif
63
64 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) &&    \
65         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) &&  \
66         brnf_filter_vlan_tagged)
67 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) &&    \
68         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) &&  \
69         brnf_filter_vlan_tagged)
70 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) &&   \
71         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
72         brnf_filter_vlan_tagged)
73
74 /* We need these fake structures to make netfilter happy --
75  * lots of places assume that skb->dst != NULL, which isn't
76  * all that unreasonable.
77  *
78  * Currently, we fill in the PMTU entry because netfilter
79  * refragmentation needs it, and the rt_flags entry because
80  * ipt_REJECT needs it.  Future netfilter modules might
81  * require us to fill additional fields. */
82 static struct net_device __fake_net_device = {
83         .hard_header_len        = ETH_HLEN
84 };
85
86 static struct rtable __fake_rtable = {
87         .u = {
88                 .dst = {
89                         .__refcnt               = ATOMIC_INIT(1),
90                         .dev                    = &__fake_net_device,
91                         .path                   = &__fake_rtable.u.dst,
92                         .metrics                = {[RTAX_MTU - 1] = 1500},
93                         .flags                  = DST_NOXFRM,
94                 }
95         },
96         .rt_flags       = 0,
97 };
98
99 static inline struct net_device *bridge_parent(const struct net_device *dev)
100 {
101         struct net_bridge_port *port = rcu_dereference(dev->br_port);
102
103         return port ? port->br->dev : NULL;
104 }
105
106 /* PF_BRIDGE/PRE_ROUTING *********************************************/
107 /* Undo the changes made for ip6tables PREROUTING and continue the
108  * bridge PRE_ROUTING hook. */
109 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
110 {
111         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
112
113         if (nf_bridge->mask & BRNF_PKT_TYPE) {
114                 skb->pkt_type = PACKET_OTHERHOST;
115                 nf_bridge->mask ^= BRNF_PKT_TYPE;
116         }
117         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
118
119         skb->dst = (struct dst_entry *)&__fake_rtable;
120         dst_hold(skb->dst);
121
122         skb->dev = nf_bridge->physindev;
123         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
124                 skb_push(skb, VLAN_HLEN);
125                 skb->nh.raw -= VLAN_HLEN;
126         }
127         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
128                        br_handle_frame_finish, 1);
129
130         return 0;
131 }
132
133 static void __br_dnat_complain(void)
134 {
135         static unsigned long last_complaint;
136
137         if (jiffies - last_complaint >= 5 * HZ) {
138                 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
139                         "forwarding to be enabled\n");
140                 last_complaint = jiffies;
141         }
142 }
143
144 /* This requires some explaining. If DNAT has taken place,
145  * we will need to fix up the destination Ethernet address,
146  * and this is a tricky process.
147  *
148  * There are two cases to consider:
149  * 1. The packet was DNAT'ed to a device in the same bridge
150  *    port group as it was received on. We can still bridge
151  *    the packet.
152  * 2. The packet was DNAT'ed to a different device, either
153  *    a non-bridged device or another bridge port group.
154  *    The packet will need to be routed.
155  *
156  * The correct way of distinguishing between these two cases is to
157  * call ip_route_input() and to look at skb->dst->dev, which is
158  * changed to the destination device if ip_route_input() succeeds.
159  *
160  * Let us first consider the case that ip_route_input() succeeds:
161  *
162  * If skb->dst->dev equals the logical bridge device the packet
163  * came in on, we can consider this bridging. We then call
164  * skb->dst->output() which will make the packet enter br_nf_local_out()
165  * not much later. In that function it is assured that the iptables
166  * FORWARD chain is traversed for the packet.
167  *
168  * Otherwise, the packet is considered to be routed and we just
169  * change the destination MAC address so that the packet will
170  * later be passed up to the IP stack to be routed.
171  *
172  * Let us now consider the case that ip_route_input() fails:
173  *
174  * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
175  * will fail, while __ip_route_output_key() will return success. The source
176  * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
177  * thinks we're handling a locally generated packet and won't care
178  * if IP forwarding is allowed. We send a warning message to the users's
179  * log telling her to put IP forwarding on.
180  *
181  * ip_route_input() will also fail if there is no route available.
182  * In that case we just drop the packet.
183  *
184  * --Lennert, 20020411
185  * --Bart, 20020416 (updated)
186  * --Bart, 20021007 (updated) */
187 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
188 {
189         if (skb->pkt_type == PACKET_OTHERHOST) {
190                 skb->pkt_type = PACKET_HOST;
191                 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
192         }
193         skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
194
195         skb->dev = bridge_parent(skb->dev);
196         if (!skb->dev)
197                 kfree_skb(skb);
198         else {
199                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
200                         skb_pull(skb, VLAN_HLEN);
201                         skb->nh.raw += VLAN_HLEN;
202                 }
203                 skb->dst->output(skb);
204         }
205         return 0;
206 }
207
208 static int br_nf_pre_routing_finish(struct sk_buff *skb)
209 {
210         struct net_device *dev = skb->dev;
211         struct iphdr *iph = skb->nh.iph;
212         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
213
214         if (nf_bridge->mask & BRNF_PKT_TYPE) {
215                 skb->pkt_type = PACKET_OTHERHOST;
216                 nf_bridge->mask ^= BRNF_PKT_TYPE;
217         }
218         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
219
220         if (dnat_took_place(skb)) {
221                 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
222                     dev)) {
223                         struct rtable *rt;
224                         struct flowi fl = { .nl_u = 
225                         { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
226                                      .tos = RT_TOS(iph->tos)} }, .proto = 0};
227
228                         if (!ip_route_output_key(&rt, &fl)) {
229                                 /* - Bridged-and-DNAT'ed traffic doesn't
230                                  *   require ip_forwarding.
231                                  * - Deal with redirected traffic. */
232                                 if (((struct dst_entry *)rt)->dev == dev ||
233                                     rt->rt_type == RTN_LOCAL) {
234                                         skb->dst = (struct dst_entry *)rt;
235                                         goto bridged_dnat;
236                                 }
237                                 __br_dnat_complain();
238                                 dst_release((struct dst_entry *)rt);
239                         }
240                         kfree_skb(skb);
241                         return 0;
242                 } else {
243                         if (skb->dst->dev == dev) {
244 bridged_dnat:
245                                 /* Tell br_nf_local_out this is a
246                                  * bridged frame */
247                                 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
248                                 skb->dev = nf_bridge->physindev;
249                                 if (skb->protocol ==
250                                     __constant_htons(ETH_P_8021Q)) {
251                                         skb_push(skb, VLAN_HLEN);
252                                         skb->nh.raw -= VLAN_HLEN;
253                                 }
254                                 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
255                                                skb, skb->dev, NULL,
256                                                br_nf_pre_routing_finish_bridge,
257                                                1);
258                                 return 0;
259                         }
260                         memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
261                                ETH_ALEN);
262                         skb->pkt_type = PACKET_HOST;
263                 }
264         } else {
265                 skb->dst = (struct dst_entry *)&__fake_rtable;
266                 dst_hold(skb->dst);
267         }
268
269         skb->dev = nf_bridge->physindev;
270         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
271                 skb_push(skb, VLAN_HLEN);
272                 skb->nh.raw -= VLAN_HLEN;
273         }
274         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
275                        br_handle_frame_finish, 1);
276
277         return 0;
278 }
279
280 /* Some common code for IPv4/IPv6 */
281 static struct net_device *setup_pre_routing(struct sk_buff *skb)
282 {
283         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
284
285         if (skb->pkt_type == PACKET_OTHERHOST) {
286                 skb->pkt_type = PACKET_HOST;
287                 nf_bridge->mask |= BRNF_PKT_TYPE;
288         }
289
290         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
291         nf_bridge->physindev = skb->dev;
292         skb->dev = bridge_parent(skb->dev);
293
294         return skb->dev;
295 }
296
297 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
298 static int check_hbh_len(struct sk_buff *skb)
299 {
300         unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
301         u32 pkt_len;
302         int off = raw - skb->nh.raw;
303         int len = (raw[1]+1)<<3;
304
305         if ((raw + len) - skb->data > skb_headlen(skb))
306                 goto bad;
307
308         off += 2;
309         len -= 2;
310
311         while (len > 0) {
312                 int optlen = skb->nh.raw[off+1]+2;
313
314                 switch (skb->nh.raw[off]) {
315                 case IPV6_TLV_PAD0:
316                         optlen = 1;
317                         break;
318
319                 case IPV6_TLV_PADN:
320                         break;
321
322                 case IPV6_TLV_JUMBO:
323                         if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
324                                 goto bad;
325                         pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
326                         if (pkt_len <= IPV6_MAXPLEN ||
327                             skb->nh.ipv6h->payload_len)
328                                 goto bad;
329                         if (pkt_len > skb->len - sizeof(struct ipv6hdr))
330                                 goto bad;
331                         if (pskb_trim_rcsum(skb,
332                             pkt_len+sizeof(struct ipv6hdr)))
333                                 goto bad;
334                         break;
335                 default:
336                         if (optlen > len)
337                                 goto bad;
338                         break;
339                 }
340                 off += optlen;
341                 len -= optlen;
342         }
343         if (len == 0)
344                 return 0;
345 bad:
346         return -1;
347
348 }
349
350 /* Replicate the checks that IPv6 does on packet reception and pass the packet
351  * to ip6tables, which doesn't support NAT, so things are fairly simple. */
352 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
353    struct sk_buff *skb, const struct net_device *in,
354    const struct net_device *out, int (*okfn)(struct sk_buff *))
355 {
356         struct ipv6hdr *hdr;
357         u32 pkt_len;
358         struct nf_bridge_info *nf_bridge;
359
360         if (skb->len < sizeof(struct ipv6hdr))
361                 goto inhdr_error;
362
363         if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
364                 goto inhdr_error;
365
366         hdr = skb->nh.ipv6h;
367
368         if (hdr->version != 6)
369                 goto inhdr_error;
370
371         pkt_len = ntohs(hdr->payload_len);
372
373         if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
374                 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
375                         goto inhdr_error;
376                 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
377                         if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
378                                 goto inhdr_error;
379                         if (skb->ip_summed == CHECKSUM_HW)
380                                 skb->ip_summed = CHECKSUM_NONE;
381                 }
382         }
383         if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
384                         goto inhdr_error;
385
386         nf_bridge_put(skb->nf_bridge);
387         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
388                 return NF_DROP;
389         if (!setup_pre_routing(skb))
390                 return NF_DROP;
391
392         NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
393                 br_nf_pre_routing_finish_ipv6);
394
395         return NF_STOLEN;
396
397 inhdr_error:
398         return NF_DROP;
399 }
400
401 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
402  * Replicate the checks that IPv4 does on packet reception.
403  * Set skb->dev to the bridge device (i.e. parent of the
404  * receiving device) to make netfilter happy, the REDIRECT
405  * target in particular.  Save the original destination IP
406  * address to be able to detect DNAT afterwards. */
407 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
408                                       const struct net_device *in,
409                                       const struct net_device *out,
410                                       int (*okfn)(struct sk_buff *))
411 {
412         struct iphdr *iph;
413         __u32 len;
414         struct sk_buff *skb = *pskb;
415         struct nf_bridge_info *nf_bridge;
416         struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
417
418         if (skb->protocol == __constant_htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
419 #ifdef CONFIG_SYSCTL
420                 if (!brnf_call_ip6tables)
421                         return NF_ACCEPT;
422 #endif
423                 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
424                         goto out;
425
426                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
427                         skb_pull_rcsum(skb, VLAN_HLEN);
428                         skb->nh.raw += VLAN_HLEN;
429                 }
430                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
431         }
432 #ifdef CONFIG_SYSCTL
433         if (!brnf_call_iptables)
434                 return NF_ACCEPT;
435 #endif
436
437         if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
438                 return NF_ACCEPT;
439
440         if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
441                 goto out;
442
443         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
444                 skb_pull_rcsum(skb, VLAN_HLEN);
445                 skb->nh.raw += VLAN_HLEN;
446         }
447
448         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
449                 goto inhdr_error;
450
451         iph = skb->nh.iph;
452         if (iph->ihl < 5 || iph->version != 4)
453                 goto inhdr_error;
454
455         if (!pskb_may_pull(skb, 4*iph->ihl))
456                 goto inhdr_error;
457
458         iph = skb->nh.iph;
459         if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
460                 goto inhdr_error;
461
462         len = ntohs(iph->tot_len);
463         if (skb->len < len || len < 4*iph->ihl)
464                 goto inhdr_error;
465
466         if (skb->len > len) {
467                 __pskb_trim(skb, len);
468                 if (skb->ip_summed == CHECKSUM_HW)
469                         skb->ip_summed = CHECKSUM_NONE;
470         }
471
472         nf_bridge_put(skb->nf_bridge);
473         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
474                 return NF_DROP;
475         if (!setup_pre_routing(skb))
476                 return NF_DROP;
477         store_orig_dstaddr(skb);
478
479         NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
480                 br_nf_pre_routing_finish);
481
482         return NF_STOLEN;
483
484 inhdr_error:
485 //      IP_INC_STATS_BH(IpInHdrErrors);
486 out:
487         return NF_DROP;
488 }
489
490
491 /* PF_BRIDGE/LOCAL_IN ************************************************/
492 /* The packet is locally destined, which requires a real
493  * dst_entry, so detach the fake one.  On the way up, the
494  * packet would pass through PRE_ROUTING again (which already
495  * took place when the packet entered the bridge), but we
496  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
497  * prevent this from happening. */
498 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
499    const struct net_device *in, const struct net_device *out,
500    int (*okfn)(struct sk_buff *))
501 {
502         struct sk_buff *skb = *pskb;
503
504         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
505                 dst_release(skb->dst);
506                 skb->dst = NULL;
507         }
508
509         return NF_ACCEPT;
510 }
511
512
513 /* PF_BRIDGE/FORWARD *************************************************/
514 static int br_nf_forward_finish(struct sk_buff *skb)
515 {
516         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
517         struct net_device *in;
518         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
519
520         if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
521                 in = nf_bridge->physindev;
522                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
523                         skb->pkt_type = PACKET_OTHERHOST;
524                         nf_bridge->mask ^= BRNF_PKT_TYPE;
525                 }
526         } else {
527                 in = *((struct net_device **)(skb->cb));
528         }
529         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
530                 skb_push(skb, VLAN_HLEN);
531                 skb->nh.raw -= VLAN_HLEN;
532         }
533         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
534                         skb->dev, br_forward_finish, 1);
535         return 0;
536 }
537
538 /* This is the 'purely bridged' case.  For IP, we pass the packet to
539  * netfilter with indev and outdev set to the bridge device,
540  * but we are still able to filter on the 'real' indev/outdev
541  * because of the physdev module. For ARP, indev and outdev are the
542  * bridge ports. */
543 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
544    const struct net_device *in, const struct net_device *out,
545    int (*okfn)(struct sk_buff *))
546 {
547         struct sk_buff *skb = *pskb;
548         struct nf_bridge_info *nf_bridge;
549         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
550         struct net_device *parent;
551         int pf;
552
553         if (!skb->nf_bridge)
554                 return NF_ACCEPT;
555
556         parent = bridge_parent(out);
557         if (!parent)
558                 return NF_DROP;
559
560         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
561                 pf = PF_INET;
562         else
563                 pf = PF_INET6;
564
565         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
566                 skb_pull(*pskb, VLAN_HLEN);
567                 (*pskb)->nh.raw += VLAN_HLEN;
568         }
569
570         nf_bridge = skb->nf_bridge;
571         if (skb->pkt_type == PACKET_OTHERHOST) {
572                 skb->pkt_type = PACKET_HOST;
573                 nf_bridge->mask |= BRNF_PKT_TYPE;
574         }
575
576         /* The physdev module checks on this */
577         nf_bridge->mask |= BRNF_BRIDGED;
578         nf_bridge->physoutdev = skb->dev;
579
580         NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
581                 br_nf_forward_finish);
582
583         return NF_STOLEN;
584 }
585
586 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
587    const struct net_device *in, const struct net_device *out,
588    int (*okfn)(struct sk_buff *))
589 {
590         struct sk_buff *skb = *pskb;
591         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
592         struct net_device **d = (struct net_device **)(skb->cb);
593
594 #ifdef CONFIG_SYSCTL
595         if (!brnf_call_arptables)
596                 return NF_ACCEPT;
597 #endif
598
599         if (skb->protocol != __constant_htons(ETH_P_ARP)) {
600                 if (!IS_VLAN_ARP)
601                         return NF_ACCEPT;
602                 skb_pull(*pskb, VLAN_HLEN);
603                 (*pskb)->nh.raw += VLAN_HLEN;
604         }
605
606         if (skb->nh.arph->ar_pln != 4) {
607                 if (IS_VLAN_ARP) {
608                         skb_push(*pskb, VLAN_HLEN);
609                         (*pskb)->nh.raw -= VLAN_HLEN;
610                 }
611                 return NF_ACCEPT;
612         }
613         *d = (struct net_device *)in;
614         NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
615                 (struct net_device *)out, br_nf_forward_finish);
616
617         return NF_STOLEN;
618 }
619
620
621 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
622 static int br_nf_local_out_finish(struct sk_buff *skb)
623 {
624         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
625                 skb_push(skb, VLAN_HLEN);
626                 skb->nh.raw -= VLAN_HLEN;
627         }
628
629         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
630                         br_forward_finish, NF_BR_PRI_FIRST + 1);
631
632         return 0;
633 }
634
635 /* This function sees both locally originated IP packets and forwarded
636  * IP packets (in both cases the destination device is a bridge
637  * device). It also sees bridged-and-DNAT'ed packets.
638  * To be able to filter on the physical bridge devices (with the physdev
639  * module), we steal packets destined to a bridge device away from the
640  * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
641  * when we have determined the real output device. This is done in here.
642  *
643  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
644  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
645  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
646  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
647  * will be executed.
648  * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
649  * this packet before, and so the packet was locally originated. We fake
650  * the PF_INET/LOCAL_OUT hook.
651  * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
652  * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
653  * even routed packets that didn't arrive on a bridge interface have their
654  * nf_bridge->physindev set. */
655 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
656    const struct net_device *in, const struct net_device *out,
657    int (*okfn)(struct sk_buff *))
658 {
659         struct net_device *realindev, *realoutdev;
660         struct sk_buff *skb = *pskb;
661         struct nf_bridge_info *nf_bridge;
662         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
663         int pf;
664
665         if (!skb->nf_bridge)
666                 return NF_ACCEPT;
667
668         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
669                 pf = PF_INET;
670         else
671                 pf = PF_INET6;
672
673 #ifdef CONFIG_NETFILTER_DEBUG
674         /* Sometimes we get packets with NULL ->dst here (for example,
675          * running a dhcp client daemon triggers this). This should now
676          * be fixed, but let's keep the check around. */
677         if (skb->dst == NULL) {
678                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
679                 return NF_ACCEPT;
680         }
681 #endif
682
683         nf_bridge = skb->nf_bridge;
684         nf_bridge->physoutdev = skb->dev;
685         realindev = nf_bridge->physindev;
686
687         /* Bridged, take PF_BRIDGE/FORWARD.
688          * (see big note in front of br_nf_pre_routing_finish) */
689         if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
690                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
691                         skb->pkt_type = PACKET_OTHERHOST;
692                         nf_bridge->mask ^= BRNF_PKT_TYPE;
693                 }
694                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
695                         skb_push(skb, VLAN_HLEN);
696                         skb->nh.raw -= VLAN_HLEN;
697                 }
698
699                 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
700                         skb->dev, br_forward_finish);
701                 goto out;
702         }
703         realoutdev = bridge_parent(skb->dev);
704         if (!realoutdev)
705                 return NF_DROP;
706
707 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
708         /* iptables should match -o br0.x */
709         if (nf_bridge->netoutdev)
710                 realoutdev = nf_bridge->netoutdev;
711 #endif
712         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
713                 skb_pull(skb, VLAN_HLEN);
714                 (*pskb)->nh.raw += VLAN_HLEN;
715         }
716         /* IP forwarded traffic has a physindev, locally
717          * generated traffic hasn't. */
718         if (realindev != NULL) {
719                 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) ) {
720                         struct net_device *parent = bridge_parent(realindev);
721                         if (parent)
722                                 realindev = parent;
723                 }
724
725                 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
726                                realoutdev, br_nf_local_out_finish,
727                                NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
728         } else {
729                 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
730                                realoutdev, br_nf_local_out_finish,
731                                NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
732         }
733
734 out:
735         return NF_STOLEN;
736 }
737
738
739 /* PF_BRIDGE/POST_ROUTING ********************************************/
740 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
741    const struct net_device *in, const struct net_device *out,
742    int (*okfn)(struct sk_buff *))
743 {
744         struct sk_buff *skb = *pskb;
745         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
746         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
747         struct net_device *realoutdev = bridge_parent(skb->dev);
748         int pf;
749
750 #ifdef CONFIG_NETFILTER_DEBUG
751         /* Be very paranoid. This probably won't happen anymore, but let's
752          * keep the check just to be sure... */
753         if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
754                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
755                                  "bad mac.raw pointer.");
756                 goto print_error;
757         }
758 #endif
759
760         if (!nf_bridge)
761                 return NF_ACCEPT;
762
763         if (!realoutdev)
764                 return NF_DROP;
765
766         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
767                 pf = PF_INET;
768         else
769                 pf = PF_INET6;
770
771 #ifdef CONFIG_NETFILTER_DEBUG
772         if (skb->dst == NULL) {
773                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
774                 goto print_error;
775         }
776 #endif
777
778         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
779          * about the value of skb->pkt_type. */
780         if (skb->pkt_type == PACKET_OTHERHOST) {
781                 skb->pkt_type = PACKET_HOST;
782                 nf_bridge->mask |= BRNF_PKT_TYPE;
783         }
784
785         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
786                 skb_pull(skb, VLAN_HLEN);
787                 skb->nh.raw += VLAN_HLEN;
788         }
789
790         nf_bridge_save_header(skb);
791
792 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
793         if (nf_bridge->netoutdev)
794                 realoutdev = nf_bridge->netoutdev;
795 #endif
796         NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
797                 br_dev_queue_push_xmit);
798
799         return NF_STOLEN;
800
801 #ifdef CONFIG_NETFILTER_DEBUG
802 print_error:
803         if (skb->dev != NULL) {
804                 printk("[%s]", skb->dev->name);
805                 if (realoutdev)
806                         printk("[%s]", realoutdev->name);
807         }
808         printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
809                                               skb->data);
810         return NF_ACCEPT;
811 #endif
812 }
813
814
815 /* IP/SABOTAGE *****************************************************/
816 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
817  * for the second time. */
818 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
819    const struct net_device *in, const struct net_device *out,
820    int (*okfn)(struct sk_buff *))
821 {
822         if ((*pskb)->nf_bridge &&
823             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
824                 return NF_STOP;
825         }
826
827         return NF_ACCEPT;
828 }
829
830 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
831  * and PF_INET(6)/POST_ROUTING until we have done the forwarding
832  * decision in the bridge code and have determined nf_bridge->physoutdev. */
833 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
834    const struct net_device *in, const struct net_device *out,
835    int (*okfn)(struct sk_buff *))
836 {
837         struct sk_buff *skb = *pskb;
838
839         if ((out->hard_start_xmit == br_dev_xmit &&
840             okfn != br_nf_forward_finish &&
841             okfn != br_nf_local_out_finish &&
842             okfn != br_dev_queue_push_xmit)
843 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
844             || ((out->priv_flags & IFF_802_1Q_VLAN) &&
845             VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
846 #endif
847             ) {
848                 struct nf_bridge_info *nf_bridge;
849
850                 if (!skb->nf_bridge) {
851 #ifdef CONFIG_SYSCTL
852                         /* This code is executed while in the IP(v6) stack,
853                            the version should be 4 or 6. We can't use
854                            skb->protocol because that isn't set on
855                            PF_INET(6)/LOCAL_OUT. */
856                         struct iphdr *ip = skb->nh.iph;
857
858                         if (ip->version == 4 && !brnf_call_iptables)
859                                 return NF_ACCEPT;
860                         else if (ip->version == 6 && !brnf_call_ip6tables)
861                                 return NF_ACCEPT;
862 #endif
863                         if (hook == NF_IP_POST_ROUTING)
864                                 return NF_ACCEPT;
865                         if (!nf_bridge_alloc(skb))
866                                 return NF_DROP;
867                 }
868
869                 nf_bridge = skb->nf_bridge;
870
871                 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
872                  * will need the indev then. For a brouter, the real indev
873                  * can be a bridge port, so we make sure br_nf_local_out()
874                  * doesn't use the bridge parent of the indev by using
875                  * the BRNF_DONT_TAKE_PARENT mask. */
876                 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
877                         nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
878                         nf_bridge->physindev = (struct net_device *)in;
879                 }
880 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
881                 /* the iptables outdev is br0.x, not br0 */
882                 if (out->priv_flags & IFF_802_1Q_VLAN)
883                         nf_bridge->netoutdev = (struct net_device *)out;
884 #endif
885                 return NF_STOP;
886         }
887
888         return NF_ACCEPT;
889 }
890
891 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
892  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
893  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
894  * ip_refrag() can return NF_STOLEN. */
895 static struct nf_hook_ops br_nf_ops[] = {
896         { .hook = br_nf_pre_routing, 
897           .owner = THIS_MODULE, 
898           .pf = PF_BRIDGE, 
899           .hooknum = NF_BR_PRE_ROUTING, 
900           .priority = NF_BR_PRI_BRNF, },
901         { .hook = br_nf_local_in,
902           .owner = THIS_MODULE,
903           .pf = PF_BRIDGE,
904           .hooknum = NF_BR_LOCAL_IN,
905           .priority = NF_BR_PRI_BRNF, },
906         { .hook = br_nf_forward_ip,
907           .owner = THIS_MODULE,
908           .pf = PF_BRIDGE,
909           .hooknum = NF_BR_FORWARD,
910           .priority = NF_BR_PRI_BRNF - 1, },
911         { .hook = br_nf_forward_arp,
912           .owner = THIS_MODULE,
913           .pf = PF_BRIDGE,
914           .hooknum = NF_BR_FORWARD,
915           .priority = NF_BR_PRI_BRNF, },
916         { .hook = br_nf_local_out,
917           .owner = THIS_MODULE,
918           .pf = PF_BRIDGE,
919           .hooknum = NF_BR_LOCAL_OUT,
920           .priority = NF_BR_PRI_FIRST, },
921         { .hook = br_nf_post_routing,
922           .owner = THIS_MODULE,
923           .pf = PF_BRIDGE,
924           .hooknum = NF_BR_POST_ROUTING,
925           .priority = NF_BR_PRI_LAST, },
926         { .hook = ip_sabotage_in,
927           .owner = THIS_MODULE,
928           .pf = PF_INET,
929           .hooknum = NF_IP_PRE_ROUTING,
930           .priority = NF_IP_PRI_FIRST, },
931         { .hook = ip_sabotage_in,
932           .owner = THIS_MODULE,
933           .pf = PF_INET6,
934           .hooknum = NF_IP6_PRE_ROUTING,
935           .priority = NF_IP6_PRI_FIRST, },
936         { .hook = ip_sabotage_out,
937           .owner = THIS_MODULE,
938           .pf = PF_INET,
939           .hooknum = NF_IP_FORWARD,
940           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
941         { .hook = ip_sabotage_out,
942           .owner = THIS_MODULE,
943           .pf = PF_INET6,
944           .hooknum = NF_IP6_FORWARD,
945           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
946         { .hook = ip_sabotage_out,
947           .owner = THIS_MODULE,
948           .pf = PF_INET,
949           .hooknum = NF_IP_LOCAL_OUT,
950           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
951         { .hook = ip_sabotage_out,
952           .owner = THIS_MODULE,
953           .pf = PF_INET6,
954           .hooknum = NF_IP6_LOCAL_OUT,
955           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
956         { .hook = ip_sabotage_out,
957           .owner = THIS_MODULE,
958           .pf = PF_INET,
959           .hooknum = NF_IP_POST_ROUTING,
960           .priority = NF_IP_PRI_FIRST, },
961         { .hook = ip_sabotage_out,
962           .owner = THIS_MODULE,
963           .pf = PF_INET6,
964           .hooknum = NF_IP6_POST_ROUTING,
965           .priority = NF_IP6_PRI_FIRST, },
966 };
967
968 #ifdef CONFIG_SYSCTL
969 static
970 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
971                         void __user *buffer, size_t *lenp, loff_t *ppos)
972 {
973         int ret;
974
975         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
976
977         if (write && *(int *)(ctl->data))
978                 *(int *)(ctl->data) = 1;
979         return ret;
980 }
981
982 static ctl_table brnf_table[] = {
983         {
984                 .ctl_name       = NET_BRIDGE_NF_CALL_ARPTABLES,
985                 .procname       = "bridge-nf-call-arptables",
986                 .data           = &brnf_call_arptables,
987                 .maxlen         = sizeof(int),
988                 .mode           = 0644,
989                 .proc_handler   = &brnf_sysctl_call_tables,
990         },
991         {
992                 .ctl_name       = NET_BRIDGE_NF_CALL_IPTABLES,
993                 .procname       = "bridge-nf-call-iptables",
994                 .data           = &brnf_call_iptables,
995                 .maxlen         = sizeof(int),
996                 .mode           = 0644,
997                 .proc_handler   = &brnf_sysctl_call_tables,
998         },
999         {
1000                 .ctl_name       = NET_BRIDGE_NF_CALL_IP6TABLES,
1001                 .procname       = "bridge-nf-call-ip6tables",
1002                 .data           = &brnf_call_ip6tables,
1003                 .maxlen         = sizeof(int),
1004                 .mode           = 0644,
1005                 .proc_handler   = &brnf_sysctl_call_tables,
1006         },
1007         {
1008                 .ctl_name       = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1009                 .procname       = "bridge-nf-filter-vlan-tagged",
1010                 .data           = &brnf_filter_vlan_tagged,
1011                 .maxlen         = sizeof(int),
1012                 .mode           = 0644,
1013                 .proc_handler   = &brnf_sysctl_call_tables,
1014         },
1015         { .ctl_name = 0 }
1016 };
1017
1018 static ctl_table brnf_bridge_table[] = {
1019         {
1020                 .ctl_name       = NET_BRIDGE,
1021                 .procname       = "bridge",
1022                 .mode           = 0555,
1023                 .child          = brnf_table,
1024         },
1025         { .ctl_name = 0 }
1026 };
1027
1028 static ctl_table brnf_net_table[] = {
1029         {
1030                 .ctl_name       = CTL_NET,
1031                 .procname       = "net",
1032                 .mode           = 0555,
1033                 .child          = brnf_bridge_table,
1034         },
1035         { .ctl_name = 0 }
1036 };
1037 #endif
1038
1039 int br_netfilter_init(void)
1040 {
1041         int i;
1042
1043         for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1044                 int ret;
1045
1046                 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1047                         continue;
1048
1049                 while (i--)
1050                         nf_unregister_hook(&br_nf_ops[i]);
1051
1052                 return ret;
1053         }
1054
1055 #ifdef CONFIG_SYSCTL
1056         brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1057         if (brnf_sysctl_header == NULL) {
1058                 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1059                 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1060                         nf_unregister_hook(&br_nf_ops[i]);
1061                 return -EFAULT;
1062         }
1063 #endif
1064
1065         printk(KERN_NOTICE "Bridge firewalling registered\n");
1066
1067         return 0;
1068 }
1069
1070 void br_netfilter_fini(void)
1071 {
1072         int i;
1073
1074         for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1075                 nf_unregister_hook(&br_nf_ops[i]);
1076 #ifdef CONFIG_SYSCTL
1077         unregister_sysctl_table(brnf_sysctl_header);
1078 #endif
1079 }