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