~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/bridge/br_netfilter.c

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  *
 15  *      This program is free software; you can redistribute it and/or
 16  *      modify it under the terms of the GNU General Public License
 17  *      as published by the Free Software Foundation; either version
 18  *      2 of the License, or (at your option) any later version.
 19  *
 20  *      Lennert dedicates this file to Kerstin Wurdinger.
 21  */
 22 
 23 #include <linux/module.h>
 24 #include <linux/kernel.h>
 25 #include <linux/ip.h>
 26 #include <linux/netdevice.h>
 27 #include <linux/skbuff.h>
 28 #include <linux/if_ether.h>
 29 #include <linux/if_vlan.h>
 30 #include <linux/netfilter_bridge.h>
 31 #include <linux/netfilter_ipv4.h>
 32 #include <linux/netfilter_arp.h>
 33 #include <linux/in_route.h>
 34 #include <net/ip.h>
 35 #include <asm/uaccess.h>
 36 #include <asm/checksum.h>
 37 #include "br_private.h"
 38 
 39 
 40 #define skb_origaddr(skb)        (((struct bridge_skb_cb *) \
 41                                  (skb->cb))->daddr.ipv4)
 42 #define store_orig_dstaddr(skb)  (skb_origaddr(skb) = (skb)->nh.iph->daddr)
 43 #define dnat_took_place(skb)     (skb_origaddr(skb) != (skb)->nh.iph->daddr)
 44 #define clear_cb(skb)            (memset(&skb_origaddr(skb), 0, \
 45                                  sizeof(struct bridge_skb_cb)))
 46 
 47 #define has_bridge_parent(device)       ((device)->br_port != NULL)
 48 #define bridge_parent(device)           ((device)->br_port->br->dev)
 49 
 50 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
 51         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP))
 52 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
 53         hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP))
 54 
 55 /* We need these fake structures to make netfilter happy --
 56  * lots of places assume that skb->dst != NULL, which isn't
 57  * all that unreasonable.
 58  *
 59  * Currently, we fill in the PMTU entry because netfilter
 60  * refragmentation needs it, and the rt_flags entry because
 61  * ipt_REJECT needs it.  Future netfilter modules might
 62  * require us to fill additional fields.
 63  */
 64 static struct net_device __fake_net_device = {
 65         .hard_header_len        = ETH_HLEN
 66 };
 67 
 68 static struct rtable __fake_rtable = {
 69         .u = {
 70                 .dst = {
 71                         .__refcnt               = ATOMIC_INIT(1),
 72                         .dev                    = &__fake_net_device,
 73                         .path                   = &__fake_rtable.u.dst,
 74                         .metrics                = {[RTAX_MTU - 1] = 1500},
 75                 }
 76         },
 77 
 78         .rt_flags       = 0
 79 };
 80 
 81 
 82 /* PF_BRIDGE/PRE_ROUTING *********************************************/
 83 static void __br_dnat_complain(void)
 84 {
 85         static unsigned long last_complaint;
 86 
 87         if (jiffies - last_complaint >= 5 * HZ) {
 88                 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
 89                         "forwarding to be enabled\n");
 90                 last_complaint = jiffies;
 91         }
 92 }
 93 
 94 
 95 /* This requires some explaining. If DNAT has taken place,
 96  * we will need to fix up the destination Ethernet address,
 97  * and this is a tricky process.
 98  *
 99  * There are two cases to consider:
100  * 1. The packet was DNAT'ed to a device in the same bridge
101  *    port group as it was received on. We can still bridge
102  *    the packet.
103  * 2. The packet was DNAT'ed to a different device, either
104  *    a non-bridged device or another bridge port group.
105  *    The packet will need to be routed.
106  *
107  * The correct way of distinguishing between these two cases is to
108  * call ip_route_input() and to look at skb->dst->dev, which is
109  * changed to the destination device if ip_route_input() succeeds.
110  *
111  * Let us first consider the case that ip_route_input() succeeds:
112  *
113  * If skb->dst->dev equals the logical bridge device the packet
114  * came in on, we can consider this bridging. We then call
115  * skb->dst->output() which will make the packet enter br_nf_local_out()
116  * not much later. In that function it is assured that the iptables
117  * FORWARD chain is traversed for the packet.
118  *
119  * Otherwise, the packet is considered to be routed and we just
120  * change the destination MAC address so that the packet will
121  * later be passed up to the IP stack to be routed.
122  *
123  * Let us now consider the case that ip_route_input() fails:
124  *
125  * After a "echo '' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
126  * will fail, while __ip_route_output_key() will return success. The source
127  * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
128  * thinks we're handling a locally generated packet and won't care
129  * if IP forwarding is allowed. We send a warning message to the users's
130  * log telling her to put IP forwarding on.
131  *
132  * ip_route_input() will also fail if there is no route available.
133  * In that case we just drop the packet.
134  *
135  * --Lennert, 20020411
136  * --Bart, 20020416 (updated)
137  * --Bart, 20021007 (updated)
138  */
139 
140 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
141 {
142 #ifdef CONFIG_NETFILTER_DEBUG
143         skb->nf_debug |= (1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_FORWARD);
144 #endif
145 
146         if (skb->pkt_type == PACKET_OTHERHOST) {
147                 skb->pkt_type = PACKET_HOST;
148                 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
149         }
150         skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
151 
152         skb->dev = bridge_parent(skb->dev);
153         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
154                 skb_pull(skb, VLAN_HLEN);
155                 skb->nh.raw += VLAN_HLEN;
156         }
157         skb->dst->output(skb);
158         return 0;
159 }
160 
161 static int br_nf_pre_routing_finish(struct sk_buff *skb)
162 {
163         struct net_device *dev = skb->dev;
164         struct iphdr *iph = skb->nh.iph;
165         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
166 
167 #ifdef CONFIG_NETFILTER_DEBUG
168         skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
169 #endif
170 
171         if (nf_bridge->mask & BRNF_PKT_TYPE) {
172                 skb->pkt_type = PACKET_OTHERHOST;
173                 nf_bridge->mask ^= BRNF_PKT_TYPE;
174         }
175         nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
176 
177         if (dnat_took_place(skb)) {
178                 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
179                     dev)) {
180                         struct rtable *rt;
181                         struct flowi fl = { .nl_u = 
182                         { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
183                                      .tos = RT_TOS(iph->tos)} }, .proto = 0};
184 
185                         if (!ip_route_output_key(&rt, &fl)) {
186                                 /* Bridged-and-DNAT'ed traffic doesn't
187                                  * require ip_forwarding.
188                                  */
189                                 if (((struct dst_entry *)rt)->dev == dev) {
190                                         skb->dst = (struct dst_entry *)rt;
191                                         goto bridged_dnat;
192                                 }
193                                 __br_dnat_complain();
194                                 dst_release((struct dst_entry *)rt);
195                         }
196                         kfree_skb(skb);
197                         return 0;
198                 } else {
199                         if (skb->dst->dev == dev) {
200 bridged_dnat:
201                                 /* Tell br_nf_local_out this is a
202                                  * bridged frame
203                                  */
204                                 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
205                                 skb->dev = nf_bridge->physindev;
206                                 clear_cb(skb);
207                                 if (skb->protocol ==
208                                     __constant_htons(ETH_P_8021Q)) {
209                                         skb_push(skb, VLAN_HLEN);
210                                         skb->nh.raw -= VLAN_HLEN;
211                                 }
212                                 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
213                                                skb, skb->dev, NULL,
214                                                br_nf_pre_routing_finish_bridge,
215                                                1);
216                                 return 0;
217                         }
218                         memcpy(skb->mac.ethernet->h_dest, dev->dev_addr,
219                                ETH_ALEN);
220                         skb->pkt_type = PACKET_HOST;
221                 }
222         } else {
223                 skb->dst = (struct dst_entry *)&__fake_rtable;
224                 dst_hold(skb->dst);
225         }
226 
227         clear_cb(skb);
228         skb->dev = nf_bridge->physindev;
229         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
230                 skb_push(skb, VLAN_HLEN);
231                 skb->nh.raw -= VLAN_HLEN;
232         }
233         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
234                        br_handle_frame_finish, 1);
235 
236         return 0;
237 }
238 
239 /* Replicate the checks that IPv4 does on packet reception.
240  * Set skb->dev to the bridge device (i.e. parent of the
241  * receiving device) to make netfilter happy, the REDIRECT
242  * target in particular.  Save the original destination IP
243  * address to be able to detect DNAT afterwards.
244  */
245 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
246    const struct net_device *in, const struct net_device *out,
247    int (*okfn)(struct sk_buff *))
248 {
249         struct iphdr *iph;
250         __u32 len;
251         struct sk_buff *skb = *pskb;
252         struct nf_bridge_info *nf_bridge;
253 
254         if (skb->protocol != __constant_htons(ETH_P_IP)) {
255                 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)
256                                           ((*pskb)->mac.ethernet);
257 
258                 if (!IS_VLAN_IP)
259                         return NF_ACCEPT;
260                 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
261                         goto out;
262                 skb_pull(*pskb, VLAN_HLEN);
263                 (*pskb)->nh.raw += VLAN_HLEN;
264         } else if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
265                 goto out;
266 
267         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
268                 goto inhdr_error;
269 
270         iph = skb->nh.iph;
271         if (iph->ihl < 5 || iph->version != 4)
272                 goto inhdr_error;
273 
274         if (!pskb_may_pull(skb, 4*iph->ihl))
275                 goto inhdr_error;
276 
277         iph = skb->nh.iph;
278         if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
279                 goto inhdr_error;
280 
281         len = ntohs(iph->tot_len);
282         if (skb->len < len || len < 4*iph->ihl)
283                 goto inhdr_error;
284 
285         if (skb->len > len) {
286                 __pskb_trim(skb, len);
287                 if (skb->ip_summed == CHECKSUM_HW)
288                         skb->ip_summed = CHECKSUM_NONE;
289         }
290 
291 #ifdef CONFIG_NETFILTER_DEBUG
292         skb->nf_debug ^= (1 << NF_IP_PRE_ROUTING);
293 #endif
294         if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
295                 return NF_DROP;
296 
297         if (skb->pkt_type == PACKET_OTHERHOST) {
298                 skb->pkt_type = PACKET_HOST;
299                 nf_bridge->mask |= BRNF_PKT_TYPE;
300         }
301 
302         nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
303         nf_bridge->physindev = skb->dev;
304         skb->dev = bridge_parent(skb->dev);
305         store_orig_dstaddr(skb);
306 
307         NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
308                 br_nf_pre_routing_finish);
309 
310         return NF_STOLEN;
311 
312 inhdr_error:
313 //      IP_INC_STATS_BH(IpInHdrErrors);
314 out:
315         return NF_DROP;
316 }
317 
318 
319 /* PF_BRIDGE/LOCAL_IN ************************************************/
320 /* The packet is locally destined, which requires a real
321  * dst_entry, so detach the fake one.  On the way up, the
322  * packet would pass through PRE_ROUTING again (which already
323  * took place when the packet entered the bridge), but we
324  * register an IPv4 PRE_ROUTING 'sabotage' hook that will
325  * prevent this from happening.
326  */
327 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
328    const struct net_device *in, const struct net_device *out,
329    int (*okfn)(struct sk_buff *))
330 {
331         struct sk_buff *skb = *pskb;
332 
333         if (skb->dst == (struct dst_entry *)&__fake_rtable) {
334                 dst_release(skb->dst);
335                 skb->dst = NULL;
336         }
337 
338         return NF_ACCEPT;
339 }
340 
341 /* PF_BRIDGE/FORWARD *************************************************/
342 static int br_nf_forward_finish(struct sk_buff *skb)
343 {
344         struct nf_bridge_info *nf_bridge = skb->nf_bridge;
345         struct net_device *in;
346         struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
347 
348 #ifdef CONFIG_NETFILTER_DEBUG
349         skb->nf_debug ^= (1 << NF_BR_FORWARD);
350 #endif
351 
352         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP) {
353                 in = nf_bridge->physindev;
354                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
355                         skb->pkt_type = PACKET_OTHERHOST;
356                         nf_bridge->mask ^= BRNF_PKT_TYPE;
357                 }
358         } else {
359                 in = *((struct net_device **)(skb->cb));
360         }
361         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
362                 skb_push(skb, VLAN_HLEN);
363                 skb->nh.raw -= VLAN_HLEN;
364         }
365         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
366                         skb->dev, br_forward_finish, 1);
367         return 0;
368 }
369 
370 /* This is the 'purely bridged' case.  For IP, we pass the packet to
371  * netfilter with indev and outdev set to the bridge device,
372  * but we are still able to filter on the 'real' indev/outdev
373  * because of the ipt_physdev.c module. For ARP, indev and outdev are the
374  * bridge ports.
375  */
376 static unsigned int br_nf_forward(unsigned int hook, struct sk_buff **pskb,
377    const struct net_device *in, const struct net_device *out,
378    int (*okfn)(struct sk_buff *))
379 {
380         struct sk_buff *skb = *pskb;
381         struct nf_bridge_info *nf_bridge;
382         struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
383 
384         if (skb->protocol != __constant_htons(ETH_P_IP) &&
385             skb->protocol != __constant_htons(ETH_P_ARP)) {
386                 if (!IS_VLAN_IP && !IS_VLAN_ARP)
387                         return NF_ACCEPT;
388                 skb_pull(*pskb, VLAN_HLEN);
389                 (*pskb)->nh.raw += VLAN_HLEN;
390         }
391 
392 #ifdef CONFIG_NETFILTER_DEBUG
393         skb->nf_debug ^= (1 << NF_BR_FORWARD);
394 #endif
395         if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP) {
396                 nf_bridge = skb->nf_bridge;
397                 if (skb->pkt_type == PACKET_OTHERHOST) {
398                         skb->pkt_type = PACKET_HOST;
399                         nf_bridge->mask |= BRNF_PKT_TYPE;
400                 }
401 
402                 /* The physdev module checks on this */
403                 nf_bridge->mask |= BRNF_BRIDGED;
404                 nf_bridge->physoutdev = skb->dev;
405 
406                 NF_HOOK(PF_INET, NF_IP_FORWARD, skb, bridge_parent(in),
407                         bridge_parent(out), br_nf_forward_finish);
408         } else {
409                 struct net_device **d = (struct net_device **)(skb->cb);
410                 struct arphdr *arp = skb->nh.arph;
411 
412                 if (arp->ar_pln != 4) {
413                         if (IS_VLAN_ARP) {
414                                 skb_push(*pskb, VLAN_HLEN);
415                                 (*pskb)->nh.raw -= VLAN_HLEN;
416                         }
417                         return NF_ACCEPT;
418                 }
419                 *d = (struct net_device *)in;
420                 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
421                         (struct net_device *)out, br_nf_forward_finish);
422         }
423 
424         return NF_STOLEN;
425 }
426 
427 
428 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
429 static int br_nf_local_out_finish(struct sk_buff *skb)
430 {
431 #ifdef CONFIG_NETFILTER_DEBUG
432         skb->nf_debug &= ~(1 << NF_BR_LOCAL_OUT);
433 #endif
434         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
435                 skb_push(skb, VLAN_HLEN);
436                 skb->nh.raw -= VLAN_HLEN;
437         }
438 
439         NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
440                         br_forward_finish, NF_BR_PRI_FIRST + 1);
441 
442         return 0;
443 }
444 
445 
446 /* This function sees both locally originated IP packets and forwarded
447  * IP packets (in both cases the destination device is a bridge
448  * device). It also sees bridged-and-DNAT'ed packets.
449  * To be able to filter on the physical bridge devices (with the ipt_physdev.c
450  * module), we steal packets destined to a bridge device away from the
451  * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
452  * when we have determined the real output device. This is done in here.
453  *
454  * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
455  * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
456  * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
457  * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
458  * will be executed.
459  * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
460  * this packet before, and so the packet was locally originated. We fake
461  * the PF_INET/LOCAL_OUT hook.
462  * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
463  * so we fake the PF_INET/FORWARD hook. ipv4_sabotage_out() makes sure
464  * even routed packets that didn't arrive on a bridge interface have their
465  * nf_bridge->physindev set.
466  */
467 
468 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
469    const struct net_device *in, const struct net_device *out,
470    int (*_okfn)(struct sk_buff *))
471 {
472         int (*okfn)(struct sk_buff *skb);
473         struct net_device *realindev;
474         struct sk_buff *skb = *pskb;
475         struct nf_bridge_info *nf_bridge;
476         struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
477 
478         if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
479                 return NF_ACCEPT;
480 
481         /* Sometimes we get packets with NULL ->dst here (for example,
482          * running a dhcp client daemon triggers this).
483          */
484         if (skb->dst == NULL)
485                 return NF_ACCEPT;
486 
487         nf_bridge = skb->nf_bridge;
488         nf_bridge->physoutdev = skb->dev;
489 
490         realindev = nf_bridge->physindev;
491 
492         /* Bridged, take PF_BRIDGE/FORWARD.
493          * (see big note in front of br_nf_pre_routing_finish)
494          */
495         if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
496                 okfn = br_forward_finish;
497 
498                 if (nf_bridge->mask & BRNF_PKT_TYPE) {
499                         skb->pkt_type = PACKET_OTHERHOST;
500                         nf_bridge->mask ^= BRNF_PKT_TYPE;
501                 }
502                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
503                         skb_push(skb, VLAN_HLEN);
504                         skb->nh.raw -= VLAN_HLEN;
505                 }
506 
507                 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
508                         skb->dev, okfn);
509         } else {
510                 struct net_device *realoutdev = bridge_parent(skb->dev);
511 
512 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
513                 /* iptables should match -o br0.x */
514                 if (nf_bridge->netoutdev)
515                         realoutdev = nf_bridge->netoutdev;
516 #endif
517                 okfn = br_nf_local_out_finish;
518                 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
519                         skb_pull(skb, VLAN_HLEN);
520                         (*pskb)->nh.raw += VLAN_HLEN;
521                 }
522                 /* IP forwarded traffic has a physindev, locally
523                  * generated traffic hasn't.
524                  */
525                 if (realindev != NULL) {
526                         if (((nf_bridge->mask & BRNF_DONT_TAKE_PARENT) == 0) &&
527                             has_bridge_parent(realindev))
528                                 realindev = bridge_parent(realindev);
529                         NF_HOOK_THRESH(PF_INET, NF_IP_FORWARD, skb, realindev,
530                                        realoutdev, okfn,
531                                        NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
532                 } else {
533 #ifdef CONFIG_NETFILTER_DEBUG
534                         skb->nf_debug ^= (1 << NF_IP_LOCAL_OUT);
535 #endif
536 
537                         NF_HOOK_THRESH(PF_INET, NF_IP_LOCAL_OUT, skb, realindev,
538                                        realoutdev, okfn,
539                                        NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
540                 }
541         }
542 
543         return NF_STOLEN;
544 }
545 
546 
547 /* PF_BRIDGE/POST_ROUTING ********************************************/
548 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
549    const struct net_device *in, const struct net_device *out,
550    int (*okfn)(struct sk_buff *))
551 {
552         struct sk_buff *skb = *pskb;
553         struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
554         struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
555         struct net_device *realoutdev = bridge_parent(skb->dev);
556 
557         /* Be very paranoid. Must be a device driver bug. */
558         if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
559                 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
560                                  "bad mac.raw pointer.");
561                 if (skb->dev != NULL) {
562                         printk("[%s]", skb->dev->name);
563                         if (has_bridge_parent(skb->dev))
564                                 printk("[%s]", bridge_parent(skb->dev)->name);
565                 }
566                 printk(" head:%p, raw:%p\n", skb->head, skb->mac.raw);
567                 return NF_ACCEPT;
568         }
569 
570         if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
571                 return NF_ACCEPT;
572 
573         /* Sometimes we get packets with NULL ->dst here (for example,
574          * running a dhcp client daemon triggers this).
575          */
576         if (skb->dst == NULL)
577                 return NF_ACCEPT;
578 
579 #ifdef CONFIG_NETFILTER_DEBUG
580         skb->nf_debug ^= (1 << NF_IP_POST_ROUTING);
581 #endif
582 
583         /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
584          * about the value of skb->pkt_type.
585          */
586         if (skb->pkt_type == PACKET_OTHERHOST) {
587                 skb->pkt_type = PACKET_HOST;
588                 nf_bridge->mask |= BRNF_PKT_TYPE;
589         }
590 
591         if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
592                 skb_pull(skb, VLAN_HLEN);
593                 skb->nh.raw += VLAN_HLEN;
594         }
595 
596         nf_bridge_save_header(skb);
597 
598 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
599         if (nf_bridge->netoutdev)
600                 realoutdev = nf_bridge->netoutdev;
601 #endif
602         NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
603                 realoutdev, br_dev_queue_push_xmit);
604 
605         return NF_STOLEN;
606 }
607 
608 
609 /* IPv4/SABOTAGE *****************************************************/
610 
611 /* Don't hand locally destined packets to PF_INET/PRE_ROUTING
612  * for the second time.
613  */
614 static unsigned int ipv4_sabotage_in(unsigned int hook, struct sk_buff **pskb,
615    const struct net_device *in, const struct net_device *out,
616    int (*okfn)(struct sk_buff *))
617 {
618         if ((*pskb)->nf_bridge &&
619             !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
620                 okfn(*pskb);
621                 return NF_STOLEN;
622         }
623 
624         return NF_ACCEPT;
625 }
626 
627 /* Postpone execution of PF_INET/FORWARD, PF_INET/LOCAL_OUT
628  * and PF_INET/POST_ROUTING until we have done the forwarding
629  * decision in the bridge code and have determined skb->physoutdev.
630  */
631 static unsigned int ipv4_sabotage_out(unsigned int hook, struct sk_buff **pskb,
632    const struct net_device *in, const struct net_device *out,
633    int (*okfn)(struct sk_buff *))
634 {
635         if ((out->hard_start_xmit == br_dev_xmit &&
636             okfn != br_nf_forward_finish &&
637             okfn != br_nf_local_out_finish &&
638             okfn != br_dev_queue_push_xmit)
639 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
640             || ((out->priv_flags & IFF_802_1Q_VLAN) &&
641             VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
642 #endif
643             ) {
644                 struct sk_buff *skb = *pskb;
645                 struct nf_bridge_info *nf_bridge;
646 
647                 if (!skb->nf_bridge && !nf_bridge_alloc(skb))
648                         return NF_DROP;
649 
650                 nf_bridge = skb->nf_bridge;
651 
652                 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
653                  * will need the indev then. For a brouter, the real indev
654                  * can be a bridge port, so we make sure br_nf_local_out()
655                  * doesn't use the bridge parent of the indev by using
656                  * the BRNF_DONT_TAKE_PARENT mask.
657                  */
658                 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
659                         nf_bridge->mask &= BRNF_DONT_TAKE_PARENT;
660                         nf_bridge->physindev = (struct net_device *)in;
661                 }
662 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
663                 /* the iptables outdev is br0.x, not br0 */
664                 if (out->priv_flags & IFF_802_1Q_VLAN)
665                         nf_bridge->netoutdev = (struct net_device *)out;
666 #endif
667                 okfn(skb);
668                 return NF_STOLEN;
669         }
670 
671         return NF_ACCEPT;
672 }
673 
674 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
675  * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
676  * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
677  * ip_refrag() can return NF_STOLEN.
678  */
679 static struct nf_hook_ops br_nf_ops[] = {
680         { .hook = br_nf_pre_routing, 
681           .owner = THIS_MODULE, 
682           .pf = PF_BRIDGE, 
683           .hooknum = NF_BR_PRE_ROUTING, 
684           .priority = NF_BR_PRI_BRNF, },
685         { .hook = br_nf_local_in,
686           .owner = THIS_MODULE,
687           .pf = PF_BRIDGE,
688           .hooknum = NF_BR_LOCAL_IN,
689           .priority = NF_BR_PRI_BRNF, },
690         { .hook = br_nf_forward,
691           .owner = THIS_MODULE,
692           .pf = PF_BRIDGE,
693           .hooknum = NF_BR_FORWARD,
694           .priority = NF_BR_PRI_BRNF, },
695         { .hook = br_nf_local_out,
696           .owner = THIS_MODULE,
697           .pf = PF_BRIDGE,
698           .hooknum = NF_BR_LOCAL_OUT,
699           .priority = NF_BR_PRI_FIRST, },
700         { .hook = br_nf_post_routing,
701           .owner = THIS_MODULE,
702           .pf = PF_BRIDGE,
703           .hooknum = NF_BR_POST_ROUTING,
704           .priority = NF_BR_PRI_LAST, },
705         { .hook = ipv4_sabotage_in,
706           .owner = THIS_MODULE,
707           .pf = PF_INET,
708           .hooknum = NF_IP_PRE_ROUTING,
709           .priority = NF_IP_PRI_FIRST, },
710         { .hook = ipv4_sabotage_out,
711           .owner = THIS_MODULE,
712           .pf = PF_INET,
713           .hooknum = NF_IP_FORWARD,
714           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
715         { .hook = ipv4_sabotage_out,
716           .owner = THIS_MODULE,
717           .pf = PF_INET,
718           .hooknum = NF_IP_LOCAL_OUT,
719           .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
720         { .hook = ipv4_sabotage_out,
721           .owner = THIS_MODULE,
722           .pf = PF_INET,
723           .hooknum = NF_IP_POST_ROUTING,
724           .priority = NF_IP_PRI_FIRST, },
725 };
726 
727 int br_netfilter_init(void)
728 {
729         int i;
730 
731         for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
732                 int ret;
733 
734                 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
735                         continue;
736 
737                 while (i--)
738                         nf_unregister_hook(&br_nf_ops[i]);
739 
740                 return ret;
741         }
742 
743         printk(KERN_NOTICE "Bridge firewalling registered\n");
744 
745         return 0;
746 }
747 
748 void br_netfilter_fini(void)
749 {
750         int i;
751 
752         for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
753                 nf_unregister_hook(&br_nf_ops[i]);
754 }
755 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp