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