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