[NET]: Replace skb_pull/skb_postpull_rcsum with skb_pull_rcsum
[linux-2.6.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                         u8 *vhdr = skb->data;
428                         skb_pull_rcsum(skb, VLAN_HLEN);
429                         skb->nh.raw += VLAN_HLEN;
430                 }
431                 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
432         }
433 #ifdef CONFIG_SYSCTL
434         if (!brnf_call_iptables)
435                 return NF_ACCEPT;
436 #endif
437
438         if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
439                 return NF_ACCEPT;
440
441         if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
442                 goto out;
443
444         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
445                 u8 *vhdr = skb->data;
446                 skb_pull_rcsum(skb, VLAN_HLEN);
447                 skb->nh.raw += VLAN_HLEN;
448         }
449
450         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
451                 goto inhdr_error;
452
453         iph = skb->nh.iph;
454         if (iph->ihl < 5 || iph->version != 4)
455                 goto inhdr_error;
456
457         if (!pskb_may_pull(skb, 4*iph->ihl))
458                 goto inhdr_error;
459
460         iph = skb->nh.iph;
461         if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
462                 goto inhdr_error;
463
464         len = ntohs(iph->tot_len);
465         if (skb->len < len || len < 4*iph->ihl)
466                 goto inhdr_error;
467
468         if (skb->len > len) {
469                 __pskb_trim(skb, len);
470                 if (skb->ip_summed == CHECKSUM_HW)
471                         skb->ip_summed = CHECKSUM_NONE;
472         }
473
474         nf_bridge_put(skb->nf_bridge);
475         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
476                 return NF_DROP;
477         if (!setup_pre_routing(skb))
478                 return NF_DROP;
479         store_orig_dstaddr(skb);
480
481         NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
482                 br_nf_pre_routing_finish);
483
484         return NF_STOLEN;
485
486 inhdr_error:
487 //      IP_INC_STATS_BH(IpInHdrErrors);
488 out:
489         return NF_DROP;
490 }
491
492
493 /* PF_BRIDGE/LOCAL_IN ************************************************/
494 /* The packet is locally destined, which requires a real
495  * dst_entry, so detach the fake one.  On the way up, the
496  * packet would pass through PRE_ROUTING again (which already
497  * took place when the packet entered the bridge), but we
498  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
499  * prevent this from happening. */
500 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
501    const struct net_device *in, const struct net_device *out,
502    int (*okfn)(struct sk_buff *))
503 {
504         struct sk_buff *skb = *pskb;
505
506         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
507                 dst_release(skb->dst);
508                 skb->dst = NULL;
509         }
510
511         return NF_ACCEPT;
512 }
513
514
515 /* PF_BRIDGE/FORWARD *************************************************/
516 static int br_nf_forward_finish(struct sk_buff *skb)
517 {
518         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
519         struct net_device *in;
520         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
521
522         if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
523                 in = nf_bridge->physindev;
524                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
525                         skb->pkt_type = PACKET_OTHERHOST;
526                         nf_bridge->mask ^= BRNF_PKT_TYPE;
527                 }
528         } else {
529                 in = *((struct net_device **)(skb->cb));
530         }
531         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
532                 skb_push(skb, VLAN_HLEN);
533                 skb->nh.raw -= VLAN_HLEN;
534         }
535         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
536                         skb->dev, br_forward_finish, 1);
537         return 0;
538 }
539
540 /* This is the 'purely bridged' case.  For IP, we pass the packet to
541  * netfilter with indev and outdev set to the bridge device,
542  * but we are still able to filter on the 'real' indev/outdev
543  * because of the physdev module. For ARP, indev and outdev are the
544  * bridge ports. */
545 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
546    const struct net_device *in, const struct net_device *out,
547    int (*okfn)(struct sk_buff *))
548 {
549         struct sk_buff *skb = *pskb;
550         struct nf_bridge_info *nf_bridge;
551         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
552         struct net_device *parent;
553         int pf;
554
555         if (!skb->nf_bridge)
556                 return NF_ACCEPT;
557
558         parent = bridge_parent(out);
559         if (!parent)
560                 return NF_DROP;
561
562         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
563                 pf = PF_INET;
564         else
565                 pf = PF_INET6;
566
567         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
568                 skb_pull(*pskb, VLAN_HLEN);
569                 (*pskb)->nh.raw += VLAN_HLEN;
570         }
571
572         nf_bridge = skb->nf_bridge;
573         if (skb->pkt_type == PACKET_OTHERHOST) {
574                 skb->pkt_type = PACKET_HOST;
575                 nf_bridge->mask |= BRNF_PKT_TYPE;
576         }
577
578         /* The physdev module checks on this */
579         nf_bridge->mask |= BRNF_BRIDGED;
580         nf_bridge->physoutdev = skb->dev;
581
582         NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
583                 br_nf_forward_finish);
584
585         return NF_STOLEN;
586 }
587
588 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
589    const struct net_device *in, const struct net_device *out,
590    int (*okfn)(struct sk_buff *))
591 {
592         struct sk_buff *skb = *pskb;
593         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
594         struct net_device **d = (struct net_device **)(skb->cb);
595
596 #ifdef CONFIG_SYSCTL
597         if (!brnf_call_arptables)
598                 return NF_ACCEPT;
599 #endif
600
601         if (skb->protocol != __constant_htons(ETH_P_ARP)) {
602                 if (!IS_VLAN_ARP)
603                         return NF_ACCEPT;
604                 skb_pull(*pskb, VLAN_HLEN);
605                 (*pskb)->nh.raw += VLAN_HLEN;
606         }
607
608         if (skb->nh.arph->ar_pln != 4) {
609                 if (IS_VLAN_ARP) {
610                         skb_push(*pskb, VLAN_HLEN);
611                         (*pskb)->nh.raw -= VLAN_HLEN;
612                 }
613                 return NF_ACCEPT;
614         }
615         *d = (struct net_device *)in;
616         NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
617                 (struct net_device *)out, br_nf_forward_finish);
618
619         return NF_STOLEN;
620 }
621
622
623 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
624 static int br_nf_local_out_finish(struct sk_buff *skb)
625 {
626         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
627                 skb_push(skb, VLAN_HLEN);
628                 skb->nh.raw -= VLAN_HLEN;
629         }
630
631         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
632                         br_forward_finish, NF_BR_PRI_FIRST + 1);
633
634         return 0;
635 }
636
637 /* This function sees both locally originated IP packets and forwarded
638  * IP packets (in both cases the destination device is a bridge
639  * device). It also sees bridged-and-DNAT'ed packets.
640  * To be able to filter on the physical bridge devices (with the physdev
641  * module), we steal packets destined to a bridge device away from the
642  * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
643  * when we have determined the real output device. This is done in here.
644  *
645  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
646  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
647  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
648  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
649  * will be executed.
650  * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
651  * this packet before, and so the packet was locally originated. We fake
652  * the PF_INET/LOCAL_OUT hook.
653  * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
654  * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
655  * even routed packets that didn't arrive on a bridge interface have their
656  * nf_bridge->physindev set. */
657 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
658    const struct net_device *in, const struct net_device *out,
659    int (*okfn)(struct sk_buff *))
660 {
661         struct net_device *realindev, *realoutdev;
662         struct sk_buff *skb = *pskb;
663         struct nf_bridge_info *nf_bridge;
664         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
665         int pf;
666
667         if (!skb->nf_bridge)
668                 return NF_ACCEPT;
669
670         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
671                 pf = PF_INET;
672         else
673                 pf = PF_INET6;
674
675 #ifdef CONFIG_NETFILTER_DEBUG
676         /* Sometimes we get packets with NULL ->dst here (for example,
677          * running a dhcp client daemon triggers this). This should now
678          * be fixed, but let's keep the check around. */
679         if (skb->dst == NULL) {
680                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
681                 return NF_ACCEPT;
682         }
683 #endif
684
685         nf_bridge = skb->nf_bridge;
686         nf_bridge->physoutdev = skb->dev;
687         realindev = nf_bridge->physindev;
688
689         /* Bridged, take PF_BRIDGE/FORWARD.
690          * (see big note in front of br_nf_pre_routing_finish) */
691         if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
692                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
693                         skb->pkt_type = PACKET_OTHERHOST;
694                         nf_bridge->mask ^= BRNF_PKT_TYPE;
695                 }
696                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
697                         skb_push(skb, VLAN_HLEN);
698                         skb->nh.raw -= VLAN_HLEN;
699                 }
700
701                 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
702                         skb->dev, br_forward_finish);
703                 goto out;
704         }
705         realoutdev = bridge_parent(skb->dev);
706         if (!realoutdev)
707                 return NF_DROP;
708
709 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
710         /* iptables should match -o br0.x */
711         if (nf_bridge->netoutdev)
712                 realoutdev = nf_bridge->netoutdev;
713 #endif
714         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
715                 skb_pull(skb, VLAN_HLEN);
716                 (*pskb)->nh.raw += VLAN_HLEN;
717         }
718         /* IP forwarded traffic has a physindev, locally
719          * generated traffic hasn't. */
720         if (realindev != NULL) {
721                 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) ) {
722                         struct net_device *parent = bridge_parent(realindev);
723                         if (parent)
724                                 realindev = parent;
725                 }
726
727                 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
728                                realoutdev, br_nf_local_out_finish,
729                                NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
730         } else {
731                 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
732                                realoutdev, br_nf_local_out_finish,
733                                NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
734         }
735
736 out:
737         return NF_STOLEN;
738 }
739
740
741 /* PF_BRIDGE/POST_ROUTING ********************************************/
742 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
743    const struct net_device *in, const struct net_device *out,
744    int (*okfn)(struct sk_buff *))
745 {
746         struct sk_buff *skb = *pskb;
747         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
748         struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
749         struct net_device *realoutdev = bridge_parent(skb->dev);
750         int pf;
751
752 #ifdef CONFIG_NETFILTER_DEBUG
753         /* Be very paranoid. This probably won't happen anymore, but let's
754          * keep the check just to be sure... */
755         if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
756                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
757                                  "bad mac.raw pointer.");
758                 goto print_error;
759         }
760 #endif
761
762         if (!nf_bridge)
763                 return NF_ACCEPT;
764
765         if (!realoutdev)
766                 return NF_DROP;
767
768         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
769                 pf = PF_INET;
770         else
771                 pf = PF_INET6;
772
773 #ifdef CONFIG_NETFILTER_DEBUG
774         if (skb->dst == NULL) {
775                 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
776                 goto print_error;
777         }
778 #endif
779
780         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
781          * about the value of skb->pkt_type. */
782         if (skb->pkt_type == PACKET_OTHERHOST) {
783                 skb->pkt_type = PACKET_HOST;
784                 nf_bridge->mask |= BRNF_PKT_TYPE;
785         }
786
787         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
788                 skb_pull(skb, VLAN_HLEN);
789                 skb->nh.raw += VLAN_HLEN;
790         }
791
792         nf_bridge_save_header(skb);
793
794 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
795         if (nf_bridge->netoutdev)
796                 realoutdev = nf_bridge->netoutdev;
797 #endif
798         NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
799                 br_dev_queue_push_xmit);
800
801         return NF_STOLEN;
802
803 #ifdef CONFIG_NETFILTER_DEBUG
804 print_error:
805         if (skb->dev != NULL) {
806                 printk("[%s]", skb->dev->name);
807                 if (realoutdev)
808                         printk("[%s]", realoutdev->name);
809         }
810         printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
811                                               skb->data);
812         return NF_ACCEPT;
813 #endif
814 }
815
816
817 /* IP/SABOTAGE *****************************************************/
818 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
819  * for the second time. */
820 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
821    const struct net_device *in, const struct net_device *out,
822    int (*okfn)(struct sk_buff *))
823 {
824         if ((*pskb)->nf_bridge &&
825             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
826                 return NF_STOP;
827         }
828
829         return NF_ACCEPT;
830 }
831
832 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
833  * and PF_INET(6)/POST_ROUTING until we have done the forwarding
834  * decision in the bridge code and have determined nf_bridge->physoutdev. */
835 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
836    const struct net_device *in, const struct net_device *out,
837    int (*okfn)(struct sk_buff *))
838 {
839         struct sk_buff *skb = *pskb;
840
841         if ((out->hard_start_xmit == br_dev_xmit &&
842             okfn != br_nf_forward_finish &&
843             okfn != br_nf_local_out_finish &&
844             okfn != br_dev_queue_push_xmit)
845 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
846             || ((out->priv_flags & IFF_802_1Q_VLAN) &&
847             VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
848 #endif
849             ) {
850                 struct nf_bridge_info *nf_bridge;
851
852                 if (!skb->nf_bridge) {
853 #ifdef CONFIG_SYSCTL
854                         /* This code is executed while in the IP(v6) stack,
855                            the version should be 4 or 6. We can't use
856                            skb->protocol because that isn't set on
857                            PF_INET(6)/LOCAL_OUT. */
858                         struct iphdr *ip = skb->nh.iph;
859
860                         if (ip->version == 4 && !brnf_call_iptables)
861                                 return NF_ACCEPT;
862                         else if (ip->version == 6 && !brnf_call_ip6tables)
863                                 return NF_ACCEPT;
864 #endif
865                         if (hook == NF_IP_POST_ROUTING)
866                                 return NF_ACCEPT;
867                         if (!nf_bridge_alloc(skb))
868                                 return NF_DROP;
869                 }
870
871                 nf_bridge = skb->nf_bridge;
872
873                 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
874                  * will need the indev then. For a brouter, the real indev
875                  * can be a bridge port, so we make sure br_nf_local_out()
876                  * doesn't use the bridge parent of the indev by using
877                  * the BRNF_DONT_TAKE_PARENT mask. */
878                 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
879                         nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
880                         nf_bridge->physindev = (struct net_device *)in;
881                 }
882 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
883                 /* the iptables outdev is br0.x, not br0 */
884                 if (out->priv_flags & IFF_802_1Q_VLAN)
885                         nf_bridge->netoutdev = (struct net_device *)out;
886 #endif
887                 return NF_STOP;
888         }
889
890         return NF_ACCEPT;
891 }
892
893 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
894  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
895  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
896  * ip_refrag() can return NF_STOLEN. */
897 static struct nf_hook_ops br_nf_ops[] = {
898         { .hook = br_nf_pre_routing, 
899           .owner = THIS_MODULE, 
900           .pf = PF_BRIDGE, 
901           .hooknum = NF_BR_PRE_ROUTING, 
902           .priority = NF_BR_PRI_BRNF, },
903         { .hook = br_nf_local_in,
904           .owner = THIS_MODULE,
905           .pf = PF_BRIDGE,
906           .hooknum = NF_BR_LOCAL_IN,
907           .priority = NF_BR_PRI_BRNF, },
908         { .hook = br_nf_forward_ip,
909           .owner = THIS_MODULE,
910           .pf = PF_BRIDGE,
911           .hooknum = NF_BR_FORWARD,
912           .priority = NF_BR_PRI_BRNF - 1, },
913         { .hook = br_nf_forward_arp,
914           .owner = THIS_MODULE,
915           .pf = PF_BRIDGE,
916           .hooknum = NF_BR_FORWARD,
917           .priority = NF_BR_PRI_BRNF, },
918         { .hook = br_nf_local_out,
919           .owner = THIS_MODULE,
920           .pf = PF_BRIDGE,
921           .hooknum = NF_BR_LOCAL_OUT,
922           .priority = NF_BR_PRI_FIRST, },
923         { .hook = br_nf_post_routing,
924           .owner = THIS_MODULE,
925           .pf = PF_BRIDGE,
926           .hooknum = NF_BR_POST_ROUTING,
927           .priority = NF_BR_PRI_LAST, },
928         { .hook = ip_sabotage_in,
929           .owner = THIS_MODULE,
930           .pf = PF_INET,
931           .hooknum = NF_IP_PRE_ROUTING,
932           .priority = NF_IP_PRI_FIRST, },
933         { .hook = ip_sabotage_in,
934           .owner = THIS_MODULE,
935           .pf = PF_INET6,
936           .hooknum = NF_IP6_PRE_ROUTING,
937           .priority = NF_IP6_PRI_FIRST, },
938         { .hook = ip_sabotage_out,
939           .owner = THIS_MODULE,
940           .pf = PF_INET,
941           .hooknum = NF_IP_FORWARD,
942           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
943         { .hook = ip_sabotage_out,
944           .owner = THIS_MODULE,
945           .pf = PF_INET6,
946           .hooknum = NF_IP6_FORWARD,
947           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
948         { .hook = ip_sabotage_out,
949           .owner = THIS_MODULE,
950           .pf = PF_INET,
951           .hooknum = NF_IP_LOCAL_OUT,
952           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
953         { .hook = ip_sabotage_out,
954           .owner = THIS_MODULE,
955           .pf = PF_INET6,
956           .hooknum = NF_IP6_LOCAL_OUT,
957           .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
958         { .hook = ip_sabotage_out,
959           .owner = THIS_MODULE,
960           .pf = PF_INET,
961           .hooknum = NF_IP_POST_ROUTING,
962           .priority = NF_IP_PRI_FIRST, },
963         { .hook = ip_sabotage_out,
964           .owner = THIS_MODULE,
965           .pf = PF_INET6,
966           .hooknum = NF_IP6_POST_ROUTING,
967           .priority = NF_IP6_PRI_FIRST, },
968 };
969
970 #ifdef CONFIG_SYSCTL
971 static
972 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
973                         void __user *buffer, size_t *lenp, loff_t *ppos)
974 {
975         int ret;
976
977         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
978
979         if (write && *(int *)(ctl->data))
980                 *(int *)(ctl->data) = 1;
981         return ret;
982 }
983
984 static ctl_table brnf_table[] = {
985         {
986                 .ctl_name       = NET_BRIDGE_NF_CALL_ARPTABLES,
987                 .procname       = "bridge-nf-call-arptables",
988                 .data           = &brnf_call_arptables,
989                 .maxlen         = sizeof(int),
990                 .mode           = 0644,
991                 .proc_handler   = &brnf_sysctl_call_tables,
992         },
993         {
994                 .ctl_name       = NET_BRIDGE_NF_CALL_IPTABLES,
995                 .procname       = "bridge-nf-call-iptables",
996                 .data           = &brnf_call_iptables,
997                 .maxlen         = sizeof(int),
998                 .mode           = 0644,
999                 .proc_handler   = &brnf_sysctl_call_tables,
1000         },
1001         {
1002                 .ctl_name       = NET_BRIDGE_NF_CALL_IP6TABLES,
1003                 .procname       = "bridge-nf-call-ip6tables",
1004                 .data           = &brnf_call_ip6tables,
1005                 .maxlen         = sizeof(int),
1006                 .mode           = 0644,
1007                 .proc_handler   = &brnf_sysctl_call_tables,
1008         },
1009         {
1010                 .ctl_name       = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1011                 .procname       = "bridge-nf-filter-vlan-tagged",
1012                 .data           = &brnf_filter_vlan_tagged,
1013                 .maxlen         = sizeof(int),
1014                 .mode           = 0644,
1015                 .proc_handler   = &brnf_sysctl_call_tables,
1016         },
1017         { .ctl_name = 0 }
1018 };
1019
1020 static ctl_table brnf_bridge_table[] = {
1021         {
1022                 .ctl_name       = NET_BRIDGE,
1023                 .procname       = "bridge",
1024                 .mode           = 0555,
1025                 .child          = brnf_table,
1026         },
1027         { .ctl_name = 0 }
1028 };
1029
1030 static ctl_table brnf_net_table[] = {
1031         {
1032                 .ctl_name       = CTL_NET,
1033                 .procname       = "net",
1034                 .mode           = 0555,
1035                 .child          = brnf_bridge_table,
1036         },
1037         { .ctl_name = 0 }
1038 };
1039 #endif
1040
1041 int br_netfilter_init(void)
1042 {
1043         int i;
1044
1045         for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1046                 int ret;
1047
1048                 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1049                         continue;
1050
1051                 while (i--)
1052                         nf_unregister_hook(&br_nf_ops[i]);
1053
1054                 return ret;
1055         }
1056
1057 #ifdef CONFIG_SYSCTL
1058         brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1059         if (brnf_sysctl_header == NULL) {
1060                 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1061                 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1062                         nf_unregister_hook(&br_nf_ops[i]);
1063                 return -EFAULT;
1064         }
1065 #endif
1066
1067         printk(KERN_NOTICE "Bridge firewalling registered\n");
1068
1069         return 0;
1070 }
1071
1072 void br_netfilter_fini(void)
1073 {
1074         int i;
1075
1076         for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1077                 nf_unregister_hook(&br_nf_ops[i]);
1078 #ifdef CONFIG_SYSCTL
1079         unregister_sysctl_table(brnf_sysctl_header);
1080 #endif
1081 }