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