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