]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/bridge/br_netfilter.c
[SK_BUFF]: Introduce skb_copy_from_linear_data{_offset}
[mv-sheeva.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) = ip_hdr(skb)->daddr)
52 #define dnat_took_place(skb)     (skb_origaddr(skb) != ip_hdr(skb)->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 static inline __be16 vlan_proto(const struct sk_buff *skb)
65 {
66         return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
67 }
68
69 #define IS_VLAN_IP(skb) \
70         (skb->protocol == htons(ETH_P_8021Q) && \
71          vlan_proto(skb) == htons(ETH_P_IP) &&  \
72          brnf_filter_vlan_tagged)
73
74 #define IS_VLAN_IPV6(skb) \
75         (skb->protocol == htons(ETH_P_8021Q) && \
76          vlan_proto(skb) == htons(ETH_P_IPV6) &&\
77          brnf_filter_vlan_tagged)
78
79 #define IS_VLAN_ARP(skb) \
80         (skb->protocol == htons(ETH_P_8021Q) && \
81          vlan_proto(skb) == htons(ETH_P_ARP) && \
82          brnf_filter_vlan_tagged)
83
84 /* We need these fake structures to make netfilter happy --
85  * lots of places assume that skb->dst != NULL, which isn't
86  * all that unreasonable.
87  *
88  * Currently, we fill in the PMTU entry because netfilter
89  * refragmentation needs it, and the rt_flags entry because
90  * ipt_REJECT needs it.  Future netfilter modules might
91  * require us to fill additional fields. */
92 static struct net_device __fake_net_device = {
93         .hard_header_len        = ETH_HLEN
94 };
95
96 static struct rtable __fake_rtable = {
97         .u = {
98                 .dst = {
99                         .__refcnt               = ATOMIC_INIT(1),
100                         .dev                    = &__fake_net_device,
101                         .path                   = &__fake_rtable.u.dst,
102                         .metrics                = {[RTAX_MTU - 1] = 1500},
103                         .flags                  = DST_NOXFRM,
104                 }
105         },
106         .rt_flags       = 0,
107 };
108
109 static inline struct net_device *bridge_parent(const struct net_device *dev)
110 {
111         struct net_bridge_port *port = rcu_dereference(dev->br_port);
112
113         return port ? port->br->dev : NULL;
114 }
115
116 static inline struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
117 {
118         skb->nf_bridge = kzalloc(sizeof(struct nf_bridge_info), GFP_ATOMIC);
119         if (likely(skb->nf_bridge))
120                 atomic_set(&(skb->nf_bridge->use), 1);
121
122         return skb->nf_bridge;
123 }
124
125 static inline void nf_bridge_save_header(struct sk_buff *skb)
126 {
127         int header_size = ETH_HLEN;
128
129         if (skb->protocol == htons(ETH_P_8021Q))
130                 header_size += VLAN_HLEN;
131
132         skb_copy_from_linear_data_offset(skb, -header_size,
133                                          skb->nf_bridge->data, header_size);
134 }
135
136 /*
137  * When forwarding bridge frames, we save a copy of the original
138  * header before processing.
139  */
140 int nf_bridge_copy_header(struct sk_buff *skb)
141 {
142         int err;
143         int header_size = ETH_HLEN;
144
145         if (skb->protocol == htons(ETH_P_8021Q))
146                 header_size += VLAN_HLEN;
147
148         err = skb_cow(skb, header_size);
149         if (err)
150                 return err;
151
152         memcpy(skb->data - header_size, skb->nf_bridge->data, header_size);
153
154         if (skb->protocol == htons(ETH_P_8021Q))
155                 __skb_push(skb, VLAN_HLEN);
156         return 0;
157 }
158
159 /* PF_BRIDGE/PRE_ROUTING *********************************************/
160 /* Undo the changes made for ip6tables PREROUTING and continue the
161  * bridge PRE_ROUTING hook. */
162 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
163 {
164         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
165
166         if (nf_bridge->mask & BRNF_PKT_TYPE) {
167                 skb->pkt_type = PACKET_OTHERHOST;
168                 nf_bridge->mask ^= BRNF_PKT_TYPE;
169         }
170         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
171
172         skb->dst = (struct dst_entry *)&__fake_rtable;
173         dst_hold(skb->dst);
174
175         skb->dev = nf_bridge->physindev;
176         if (skb->protocol == htons(ETH_P_8021Q)) {
177                 skb_push(skb, VLAN_HLEN);
178                 skb->network_header -= VLAN_HLEN;
179         }
180         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
181                        br_handle_frame_finish, 1);
182
183         return 0;
184 }
185
186 static void __br_dnat_complain(void)
187 {
188         static unsigned long last_complaint;
189
190         if (jiffies - last_complaint >= 5 * HZ) {
191                 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
192                        "forwarding to be enabled\n");
193                 last_complaint = jiffies;
194         }
195 }
196
197 /* This requires some explaining. If DNAT has taken place,
198  * we will need to fix up the destination Ethernet address,
199  * and this is a tricky process.
200  *
201  * There are two cases to consider:
202  * 1. The packet was DNAT'ed to a device in the same bridge
203  *    port group as it was received on. We can still bridge
204  *    the packet.
205  * 2. The packet was DNAT'ed to a different device, either
206  *    a non-bridged device or another bridge port group.
207  *    The packet will need to be routed.
208  *
209  * The correct way of distinguishing between these two cases is to
210  * call ip_route_input() and to look at skb->dst->dev, which is
211  * changed to the destination device if ip_route_input() succeeds.
212  *
213  * Let us first consider the case that ip_route_input() succeeds:
214  *
215  * If skb->dst->dev equals the logical bridge device the packet
216  * came in on, we can consider this bridging. We then call
217  * skb->dst->output() which will make the packet enter br_nf_local_out()
218  * not much later. In that function it is assured that the iptables
219  * FORWARD chain is traversed for the packet.
220  *
221  * Otherwise, the packet is considered to be routed and we just
222  * change the destination MAC address so that the packet will
223  * later be passed up to the IP stack to be routed. For a redirected
224  * packet, ip_route_input() will give back the localhost as output device,
225  * which differs from the bridge device.
226  *
227  * Let us now consider the case that ip_route_input() fails:
228  *
229  * This can be because the destination address is martian, in which case
230  * the packet will be dropped.
231  * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
232  * will fail, while __ip_route_output_key() will return success. The source
233  * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
234  * thinks we're handling a locally generated packet and won't care
235  * if IP forwarding is allowed. We send a warning message to the users's
236  * log telling her to put IP forwarding on.
237  *
238  * ip_route_input() will also fail if there is no route available.
239  * In that case we just drop the packet.
240  *
241  * --Lennert, 20020411
242  * --Bart, 20020416 (updated)
243  * --Bart, 20021007 (updated)
244  * --Bart, 20062711 (updated) */
245 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
246 {
247         if (skb->pkt_type == PACKET_OTHERHOST) {
248                 skb->pkt_type = PACKET_HOST;
249                 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
250         }
251         skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
252
253         skb->dev = bridge_parent(skb->dev);
254         if (!skb->dev)
255                 kfree_skb(skb);
256         else {
257                 if (skb->protocol == htons(ETH_P_8021Q)) {
258                         skb_pull(skb, VLAN_HLEN);
259                         skb->network_header += VLAN_HLEN;
260                 }
261                 skb->dst->output(skb);
262         }
263         return 0;
264 }
265
266 static int br_nf_pre_routing_finish(struct sk_buff *skb)
267 {
268         struct net_device *dev = skb->dev;
269         struct iphdr *iph = ip_hdr(skb);
270         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
271         int err;
272
273         if (nf_bridge->mask & BRNF_PKT_TYPE) {
274                 skb->pkt_type = PACKET_OTHERHOST;
275                 nf_bridge->mask ^= BRNF_PKT_TYPE;
276         }
277         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
278         if (dnat_took_place(skb)) {
279                 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
280                         struct rtable *rt;
281                         struct flowi fl = {
282                                 .nl_u = {
283                                         .ip4_u = {
284                                                  .daddr = iph->daddr,
285                                                  .saddr = 0,
286                                                  .tos = RT_TOS(iph->tos) },
287                                 },
288                                 .proto = 0,
289                         };
290                         struct in_device *in_dev = in_dev_get(dev);
291
292                         /* If err equals -EHOSTUNREACH the error is due to a
293                          * martian destination or due to the fact that
294                          * forwarding is disabled. For most martian packets,
295                          * ip_route_output_key() will fail. It won't fail for 2 types of
296                          * martian destinations: loopback destinations and destination
297                          * 0.0.0.0. In both cases the packet will be dropped because the
298                          * destination is the loopback device and not the bridge. */
299                         if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
300                                 goto free_skb;
301
302                         if (!ip_route_output_key(&rt, &fl)) {
303                                 /* - Bridged-and-DNAT'ed traffic doesn't
304                                  *   require ip_forwarding. */
305                                 if (((struct dst_entry *)rt)->dev == dev) {
306                                         skb->dst = (struct dst_entry *)rt;
307                                         goto bridged_dnat;
308                                 }
309                                 /* we are sure that forwarding is disabled, so printing
310                                  * this message is no problem. Note that the packet could
311                                  * still have a martian destination address, in which case
312                                  * the packet could be dropped even if forwarding were enabled */
313                                 __br_dnat_complain();
314                                 dst_release((struct dst_entry *)rt);
315                         }
316 free_skb:
317                         kfree_skb(skb);
318                         return 0;
319                 } else {
320                         if (skb->dst->dev == dev) {
321 bridged_dnat:
322                                 /* Tell br_nf_local_out this is a
323                                  * bridged frame */
324                                 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
325                                 skb->dev = nf_bridge->physindev;
326                                 if (skb->protocol ==
327                                     htons(ETH_P_8021Q)) {
328                                         skb_push(skb, VLAN_HLEN);
329                                         skb->network_header -= VLAN_HLEN;
330                                 }
331                                 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
332                                                skb, skb->dev, NULL,
333                                                br_nf_pre_routing_finish_bridge,
334                                                1);
335                                 return 0;
336                         }
337                         memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
338                         skb->pkt_type = PACKET_HOST;
339                 }
340         } else {
341                 skb->dst = (struct dst_entry *)&__fake_rtable;
342                 dst_hold(skb->dst);
343         }
344
345         skb->dev = nf_bridge->physindev;
346         if (skb->protocol == htons(ETH_P_8021Q)) {
347                 skb_push(skb, VLAN_HLEN);
348                 skb->network_header -= VLAN_HLEN;
349         }
350         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
351                        br_handle_frame_finish, 1);
352
353         return 0;
354 }
355
356 /* Some common code for IPv4/IPv6 */
357 static struct net_device *setup_pre_routing(struct sk_buff *skb)
358 {
359         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
360
361         if (skb->pkt_type == PACKET_OTHERHOST) {
362                 skb->pkt_type = PACKET_HOST;
363                 nf_bridge->mask |= BRNF_PKT_TYPE;
364         }
365
366         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
367         nf_bridge->physindev = skb->dev;
368         skb->dev = bridge_parent(skb->dev);
369
370         return skb->dev;
371 }
372
373 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
374 static int check_hbh_len(struct sk_buff *skb)
375 {
376         unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
377         u32 pkt_len;
378         const unsigned char *nh = skb_network_header(skb);
379         int off = raw - nh;
380         int len = (raw[1] + 1) << 3;
381
382         if ((raw + len) - skb->data > skb_headlen(skb))
383                 goto bad;
384
385         off += 2;
386         len -= 2;
387
388         while (len > 0) {
389                 int optlen = nh[off + 1] + 2;
390
391                 switch (nh[off]) {
392                 case IPV6_TLV_PAD0:
393                         optlen = 1;
394                         break;
395
396                 case IPV6_TLV_PADN:
397                         break;
398
399                 case IPV6_TLV_JUMBO:
400                         if (nh[off + 1] != 4 || (off & 3) != 2)
401                                 goto bad;
402                         pkt_len = ntohl(*(__be32 *) (nh + off + 2));
403                         if (pkt_len <= IPV6_MAXPLEN ||
404                             ipv6_hdr(skb)->payload_len)
405                                 goto bad;
406                         if (pkt_len > skb->len - sizeof(struct ipv6hdr))
407                                 goto bad;
408                         if (pskb_trim_rcsum(skb,
409                                             pkt_len + sizeof(struct ipv6hdr)))
410                                 goto bad;
411                         nh = skb_network_header(skb);
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 = ipv6_hdr(skb);
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->network_header += 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->network_header += VLAN_HLEN;
519         }
520
521         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
522                 goto inhdr_error;
523
524         iph = ip_hdr(skb);
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 = ip_hdr(skb);
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->network_header -= 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)->network_header += 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)->network_header += VLAN_HLEN;
672         }
673
674         if (arp_hdr(skb)->ar_pln != 4) {
675                 if (IS_VLAN_ARP(skb)) {
676                         skb_push(*pskb, VLAN_HLEN);
677                         (*pskb)->network_header -= 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  *
690  * This function sees both locally originated IP packets and forwarded
691  * IP packets (in both cases the destination device is a bridge
692  * device). It also sees bridged-and-DNAT'ed packets.
693  *
694  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
695  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
696  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
697  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
698  * will be executed.
699  */
700 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
701                                     const struct net_device *in,
702                                     const struct net_device *out,
703                                     int (*okfn)(struct sk_buff *))
704 {
705         struct net_device *realindev;
706         struct sk_buff *skb = *pskb;
707         struct nf_bridge_info *nf_bridge;
708
709         if (!skb->nf_bridge)
710                 return NF_ACCEPT;
711
712         nf_bridge = skb->nf_bridge;
713         if (!(nf_bridge->mask & BRNF_BRIDGED_DNAT))
714                 return NF_ACCEPT;
715
716         /* Bridged, take PF_BRIDGE/FORWARD.
717          * (see big note in front of br_nf_pre_routing_finish) */
718         nf_bridge->physoutdev = skb->dev;
719         realindev = nf_bridge->physindev;
720
721         if (nf_bridge->mask & BRNF_PKT_TYPE) {
722                 skb->pkt_type = PACKET_OTHERHOST;
723                 nf_bridge->mask ^= BRNF_PKT_TYPE;
724         }
725         if (skb->protocol == htons(ETH_P_8021Q)) {
726                 skb_push(skb, VLAN_HLEN);
727                 skb->network_header -= VLAN_HLEN;
728         }
729
730         NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev, skb->dev,
731                 br_forward_finish);
732         return NF_STOLEN;
733 }
734
735 static int br_nf_dev_queue_xmit(struct sk_buff *skb)
736 {
737         if (skb->protocol == htons(ETH_P_IP) &&
738             skb->len > skb->dev->mtu &&
739             !skb_is_gso(skb))
740                 return ip_fragment(skb, br_dev_queue_push_xmit);
741         else
742                 return br_dev_queue_push_xmit(skb);
743 }
744
745 /* PF_BRIDGE/POST_ROUTING ********************************************/
746 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
747                                        const struct net_device *in,
748                                        const struct net_device *out,
749                                        int (*okfn)(struct sk_buff *))
750 {
751         struct sk_buff *skb = *pskb;
752         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
753         struct net_device *realoutdev = bridge_parent(skb->dev);
754         int pf;
755
756 #ifdef CONFIG_NETFILTER_DEBUG
757         /* Be very paranoid. This probably won't happen anymore, but let's
758          * keep the check just to be sure... */
759         if (skb_mac_header(skb) < skb->head ||
760             skb_mac_header(skb) + ETH_HLEN > skb->data) {
761                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
762                        "bad mac.raw pointer.\n");
763                 goto print_error;
764         }
765 #endif
766
767         if (!nf_bridge)
768                 return NF_ACCEPT;
769
770         if (!realoutdev)
771                 return NF_DROP;
772
773         if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
774                 pf = PF_INET;
775         else
776                 pf = PF_INET6;
777
778 #ifdef CONFIG_NETFILTER_DEBUG
779         if (skb->dst == NULL) {
780                 printk(KERN_INFO "br_netfilter post_routing: skb->dst == NULL\n");
781                 goto print_error;
782         }
783 #endif
784
785         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
786          * about the value of skb->pkt_type. */
787         if (skb->pkt_type == PACKET_OTHERHOST) {
788                 skb->pkt_type = PACKET_HOST;
789                 nf_bridge->mask |= BRNF_PKT_TYPE;
790         }
791
792         if (skb->protocol == htons(ETH_P_8021Q)) {
793                 skb_pull(skb, VLAN_HLEN);
794                 skb->network_header += VLAN_HLEN;
795         }
796
797         nf_bridge_save_header(skb);
798
799 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
800         if (nf_bridge->netoutdev)
801                 realoutdev = nf_bridge->netoutdev;
802 #endif
803         NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
804                 br_nf_dev_queue_xmit);
805
806         return NF_STOLEN;
807
808 #ifdef CONFIG_NETFILTER_DEBUG
809 print_error:
810         if (skb->dev != NULL) {
811                 printk("[%s]", skb->dev->name);
812                 if (realoutdev)
813                         printk("[%s]", realoutdev->name);
814         }
815         printk(" head:%p, raw:%p, data:%p\n", skb->head, skb_mac_header(skb),
816                skb->data);
817         dump_stack();
818         return NF_ACCEPT;
819 #endif
820 }
821
822 /* IP/SABOTAGE *****************************************************/
823 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
824  * for the second time. */
825 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
826                                    const struct net_device *in,
827                                    const struct net_device *out,
828                                    int (*okfn)(struct sk_buff *))
829 {
830         if ((*pskb)->nf_bridge &&
831             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
832                 return NF_STOP;
833         }
834
835         return NF_ACCEPT;
836 }
837
838 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
839  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
840  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
841  * ip_refrag() can return NF_STOLEN. */
842 static struct nf_hook_ops br_nf_ops[] = {
843         { .hook = br_nf_pre_routing,
844           .owner = THIS_MODULE,
845           .pf = PF_BRIDGE,
846           .hooknum = NF_BR_PRE_ROUTING,
847           .priority = NF_BR_PRI_BRNF, },
848         { .hook = br_nf_local_in,
849           .owner = THIS_MODULE,
850           .pf = PF_BRIDGE,
851           .hooknum = NF_BR_LOCAL_IN,
852           .priority = NF_BR_PRI_BRNF, },
853         { .hook = br_nf_forward_ip,
854           .owner = THIS_MODULE,
855           .pf = PF_BRIDGE,
856           .hooknum = NF_BR_FORWARD,
857           .priority = NF_BR_PRI_BRNF - 1, },
858         { .hook = br_nf_forward_arp,
859           .owner = THIS_MODULE,
860           .pf = PF_BRIDGE,
861           .hooknum = NF_BR_FORWARD,
862           .priority = NF_BR_PRI_BRNF, },
863         { .hook = br_nf_local_out,
864           .owner = THIS_MODULE,
865           .pf = PF_BRIDGE,
866           .hooknum = NF_BR_LOCAL_OUT,
867           .priority = NF_BR_PRI_FIRST, },
868         { .hook = br_nf_post_routing,
869           .owner = THIS_MODULE,
870           .pf = PF_BRIDGE,
871           .hooknum = NF_BR_POST_ROUTING,
872           .priority = NF_BR_PRI_LAST, },
873         { .hook = ip_sabotage_in,
874           .owner = THIS_MODULE,
875           .pf = PF_INET,
876           .hooknum = NF_IP_PRE_ROUTING,
877           .priority = NF_IP_PRI_FIRST, },
878         { .hook = ip_sabotage_in,
879           .owner = THIS_MODULE,
880           .pf = PF_INET6,
881           .hooknum = NF_IP6_PRE_ROUTING,
882           .priority = NF_IP6_PRI_FIRST, },
883 };
884
885 #ifdef CONFIG_SYSCTL
886 static
887 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
888                             void __user * buffer, size_t * lenp, loff_t * ppos)
889 {
890         int ret;
891
892         ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
893
894         if (write && *(int *)(ctl->data))
895                 *(int *)(ctl->data) = 1;
896         return ret;
897 }
898
899 static ctl_table brnf_table[] = {
900         {
901                 .ctl_name       = NET_BRIDGE_NF_CALL_ARPTABLES,
902                 .procname       = "bridge-nf-call-arptables",
903                 .data           = &brnf_call_arptables,
904                 .maxlen         = sizeof(int),
905                 .mode           = 0644,
906                 .proc_handler   = &brnf_sysctl_call_tables,
907         },
908         {
909                 .ctl_name       = NET_BRIDGE_NF_CALL_IPTABLES,
910                 .procname       = "bridge-nf-call-iptables",
911                 .data           = &brnf_call_iptables,
912                 .maxlen         = sizeof(int),
913                 .mode           = 0644,
914                 .proc_handler   = &brnf_sysctl_call_tables,
915         },
916         {
917                 .ctl_name       = NET_BRIDGE_NF_CALL_IP6TABLES,
918                 .procname       = "bridge-nf-call-ip6tables",
919                 .data           = &brnf_call_ip6tables,
920                 .maxlen         = sizeof(int),
921                 .mode           = 0644,
922                 .proc_handler   = &brnf_sysctl_call_tables,
923         },
924         {
925                 .ctl_name       = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
926                 .procname       = "bridge-nf-filter-vlan-tagged",
927                 .data           = &brnf_filter_vlan_tagged,
928                 .maxlen         = sizeof(int),
929                 .mode           = 0644,
930                 .proc_handler   = &brnf_sysctl_call_tables,
931         },
932         { .ctl_name = 0 }
933 };
934
935 static ctl_table brnf_bridge_table[] = {
936         {
937                 .ctl_name       = NET_BRIDGE,
938                 .procname       = "bridge",
939                 .mode           = 0555,
940                 .child          = brnf_table,
941         },
942         { .ctl_name = 0 }
943 };
944
945 static ctl_table brnf_net_table[] = {
946         {
947                 .ctl_name       = CTL_NET,
948                 .procname       = "net",
949                 .mode           = 0555,
950                 .child          = brnf_bridge_table,
951         },
952         { .ctl_name = 0 }
953 };
954 #endif
955
956 int __init br_netfilter_init(void)
957 {
958         int ret;
959
960         ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
961         if (ret < 0)
962                 return ret;
963 #ifdef CONFIG_SYSCTL
964         brnf_sysctl_header = register_sysctl_table(brnf_net_table);
965         if (brnf_sysctl_header == NULL) {
966                 printk(KERN_WARNING
967                        "br_netfilter: can't register to sysctl.\n");
968                 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
969                 return -ENOMEM;
970         }
971 #endif
972         printk(KERN_NOTICE "Bridge firewalling registered\n");
973         return 0;
974 }
975
976 void br_netfilter_fini(void)
977 {
978         nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
979 #ifdef CONFIG_SYSCTL
980         unregister_sysctl_table(brnf_sysctl_header);
981 #endif
982 }