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