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