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> |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 33 | #include <linux/rculist.h> |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 34 | #include <linux/inetdevice.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <net/ip.h> |
| 37 | #include <net/ipv6.h> |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 38 | #include <net/addrconf.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 39 | #include <net/route.h> |
Florian Westphal | 5676864 | 2014-11-13 10:04:16 +0100 | [diff] [blame] | 40 | #include <net/netfilter/br_netfilter.h> |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 41 | #include <net/netns/generic.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include "br_private.h" |
| 45 | #ifdef CONFIG_SYSCTL |
| 46 | #include <linux/sysctl.h> |
| 47 | #endif |
| 48 | |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 49 | static int brnf_net_id __read_mostly; |
| 50 | |
| 51 | struct brnf_net { |
| 52 | bool enabled; |
| 53 | }; |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #ifdef CONFIG_SYSCTL |
| 56 | static struct ctl_table_header *brnf_sysctl_header; |
Brian Haley | 9c1ea14 | 2006-09-18 00:03:41 -0700 | [diff] [blame] | 57 | static int brnf_call_iptables __read_mostly = 1; |
| 58 | static int brnf_call_ip6tables __read_mostly = 1; |
| 59 | static int brnf_call_arptables __read_mostly = 1; |
Bernhard Thaler | f4b3eee | 2015-07-30 06:06:12 +0200 | [diff] [blame] | 60 | static int brnf_filter_vlan_tagged __read_mostly; |
| 61 | static int brnf_filter_pppoe_tagged __read_mostly; |
| 62 | static int brnf_pass_vlan_indev __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #else |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 64 | #define brnf_call_iptables 1 |
| 65 | #define brnf_call_ip6tables 1 |
| 66 | #define brnf_call_arptables 1 |
Herbert Xu | 47e0e1c | 2009-01-12 00:06:03 +0000 | [diff] [blame] | 67 | #define brnf_filter_vlan_tagged 0 |
| 68 | #define brnf_filter_pppoe_tagged 0 |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 69 | #define brnf_pass_vlan_indev 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #endif |
| 71 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 72 | #define IS_IP(skb) \ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 73 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP)) |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 74 | |
| 75 | #define IS_IPV6(skb) \ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 76 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6)) |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 77 | |
| 78 | #define IS_ARP(skb) \ |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 79 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP)) |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 80 | |
Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 81 | static inline __be16 vlan_proto(const struct sk_buff *skb) |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 82 | { |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 83 | if (skb_vlan_tag_present(skb)) |
Jesse Gross | 1393791 | 2010-10-20 13:56:01 +0000 | [diff] [blame] | 84 | return skb->protocol; |
| 85 | else if (skb->protocol == htons(ETH_P_8021Q)) |
| 86 | return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; |
| 87 | else |
| 88 | return 0; |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #define IS_VLAN_IP(skb) \ |
Jesse Gross | 1393791 | 2010-10-20 13:56:01 +0000 | [diff] [blame] | 92 | (vlan_proto(skb) == htons(ETH_P_IP) && \ |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 93 | brnf_filter_vlan_tagged) |
| 94 | |
| 95 | #define IS_VLAN_IPV6(skb) \ |
Jesse Gross | 1393791 | 2010-10-20 13:56:01 +0000 | [diff] [blame] | 96 | (vlan_proto(skb) == htons(ETH_P_IPV6) && \ |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 97 | brnf_filter_vlan_tagged) |
| 98 | |
| 99 | #define IS_VLAN_ARP(skb) \ |
Jesse Gross | 1393791 | 2010-10-20 13:56:01 +0000 | [diff] [blame] | 100 | (vlan_proto(skb) == htons(ETH_P_ARP) && \ |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 101 | brnf_filter_vlan_tagged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Michael Milner | 516299d | 2007-04-12 22:14:23 -0700 | [diff] [blame] | 103 | static inline __be16 pppoe_proto(const struct sk_buff *skb) |
| 104 | { |
| 105 | return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN + |
| 106 | sizeof(struct pppoe_hdr))); |
| 107 | } |
| 108 | |
| 109 | #define IS_PPPOE_IP(skb) \ |
| 110 | (skb->protocol == htons(ETH_P_PPP_SES) && \ |
| 111 | pppoe_proto(skb) == htons(PPP_IP) && \ |
| 112 | brnf_filter_pppoe_tagged) |
| 113 | |
| 114 | #define IS_PPPOE_IPV6(skb) \ |
| 115 | (skb->protocol == htons(ETH_P_PPP_SES) && \ |
| 116 | pppoe_proto(skb) == htons(PPP_IPV6) && \ |
| 117 | brnf_filter_pppoe_tagged) |
| 118 | |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 119 | /* largest possible L2 header, see br_nf_dev_queue_xmit() */ |
| 120 | #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN) |
| 121 | |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 122 | struct brnf_frag_data { |
| 123 | char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH]; |
| 124 | u8 encap_size; |
| 125 | u8 size; |
Florian Westphal | d7b5974 | 2015-06-05 13:27:13 +0200 | [diff] [blame] | 126 | u16 vlan_tci; |
| 127 | __be16 vlan_proto; |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage); |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 131 | |
Florian Westphal | a9fcc6a | 2015-05-03 22:06:07 +0200 | [diff] [blame] | 132 | static void nf_bridge_info_free(struct sk_buff *skb) |
| 133 | { |
| 134 | if (skb->nf_bridge) { |
| 135 | nf_bridge_put(skb->nf_bridge); |
| 136 | skb->nf_bridge = NULL; |
| 137 | } |
| 138 | } |
| 139 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 140 | static inline struct net_device *bridge_parent(const struct net_device *dev) |
| 141 | { |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 142 | struct net_bridge_port *port; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 143 | |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 144 | port = br_port_get_rcu(dev); |
| 145 | return port ? port->br->dev : NULL; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Patrick McHardy | 2dc2f20 | 2008-01-20 06:25:48 -0800 | [diff] [blame] | 148 | static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb) |
| 149 | { |
| 150 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; |
| 151 | |
| 152 | if (atomic_read(&nf_bridge->use) > 1) { |
| 153 | struct nf_bridge_info *tmp = nf_bridge_alloc(skb); |
| 154 | |
| 155 | if (tmp) { |
| 156 | memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info)); |
| 157 | atomic_set(&tmp->use, 1); |
Patrick McHardy | 2dc2f20 | 2008-01-20 06:25:48 -0800 | [diff] [blame] | 158 | } |
Changli Gao | 4c3a76a | 2010-08-22 19:03:26 +0000 | [diff] [blame] | 159 | nf_bridge_put(nf_bridge); |
Patrick McHardy | 2dc2f20 | 2008-01-20 06:25:48 -0800 | [diff] [blame] | 160 | nf_bridge = tmp; |
| 161 | } |
| 162 | return nf_bridge; |
| 163 | } |
| 164 | |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 165 | unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb) |
Florian Westphal | 8d04516 | 2015-03-18 20:55:31 +0100 | [diff] [blame] | 166 | { |
| 167 | switch (skb->protocol) { |
| 168 | case __cpu_to_be16(ETH_P_8021Q): |
| 169 | return VLAN_HLEN; |
| 170 | case __cpu_to_be16(ETH_P_PPP_SES): |
| 171 | return PPPOE_SES_HLEN; |
| 172 | default: |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 177 | static inline void nf_bridge_pull_encap_header(struct sk_buff *skb) |
| 178 | { |
| 179 | unsigned int len = nf_bridge_encap_header_len(skb); |
| 180 | |
| 181 | skb_pull(skb, len); |
| 182 | skb->network_header += len; |
| 183 | } |
| 184 | |
| 185 | static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb) |
| 186 | { |
| 187 | unsigned int len = nf_bridge_encap_header_len(skb); |
| 188 | |
| 189 | skb_pull_rcsum(skb, len); |
| 190 | skb->network_header += len; |
| 191 | } |
| 192 | |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 193 | /* When handing a packet over to the IP layer |
| 194 | * check whether we have a skb that is in the |
| 195 | * expected format |
| 196 | */ |
| 197 | |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 198 | static int br_validate_ipv4(struct net *net, struct sk_buff *skb) |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 199 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 200 | const struct iphdr *iph; |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 201 | u32 len; |
| 202 | |
Sarveshwar Bandi | 6caab7b | 2012-10-10 01:15:01 +0000 | [diff] [blame] | 203 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
| 204 | goto inhdr_error; |
| 205 | |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 206 | iph = ip_hdr(skb); |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 207 | |
| 208 | /* Basic sanity checks */ |
| 209 | if (iph->ihl < 5 || iph->version != 4) |
| 210 | goto inhdr_error; |
| 211 | |
| 212 | if (!pskb_may_pull(skb, iph->ihl*4)) |
| 213 | goto inhdr_error; |
| 214 | |
| 215 | iph = ip_hdr(skb); |
| 216 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) |
| 217 | goto inhdr_error; |
| 218 | |
| 219 | len = ntohs(iph->tot_len); |
| 220 | if (skb->len < len) { |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 221 | __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS); |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 222 | goto drop; |
| 223 | } else if (len < (iph->ihl*4)) |
| 224 | goto inhdr_error; |
| 225 | |
| 226 | if (pskb_trim_rcsum(skb, len)) { |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 227 | __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS); |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 228 | goto drop; |
| 229 | } |
| 230 | |
Eric Dumazet | f8e9881 | 2011-04-12 13:39:14 -0700 | [diff] [blame] | 231 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
Herbert Xu | 7677e86 | 2014-10-04 22:18:02 +0800 | [diff] [blame] | 232 | /* We should really parse IP options here but until |
| 233 | * somebody who actually uses IP options complains to |
| 234 | * us we'll just silently ignore the options because |
| 235 | * we're lazy! |
| 236 | */ |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | |
| 239 | inhdr_error: |
Eric Dumazet | b45386e | 2016-04-27 16:44:35 -0700 | [diff] [blame] | 240 | __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS); |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 241 | drop: |
| 242 | return -1; |
| 243 | } |
| 244 | |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 245 | void nf_bridge_update_protocol(struct sk_buff *skb) |
Florian Westphal | 4a9d2f2 | 2015-03-05 00:52:34 +0100 | [diff] [blame] | 246 | { |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 247 | switch (skb->nf_bridge->orig_proto) { |
| 248 | case BRNF_PROTO_8021Q: |
Florian Westphal | 4a9d2f2 | 2015-03-05 00:52:34 +0100 | [diff] [blame] | 249 | skb->protocol = htons(ETH_P_8021Q); |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 250 | break; |
| 251 | case BRNF_PROTO_PPPOE: |
Florian Westphal | 4a9d2f2 | 2015-03-05 00:52:34 +0100 | [diff] [blame] | 252 | skb->protocol = htons(ETH_P_PPP_SES); |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 253 | break; |
| 254 | case BRNF_PROTO_UNCHANGED: |
| 255 | break; |
| 256 | } |
Florian Westphal | 4a9d2f2 | 2015-03-05 00:52:34 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 259 | /* Obtain the correct destination MAC address, while preserving the original |
| 260 | * source MAC address. If we already know this address, we just copy it. If we |
| 261 | * don't, we use the neighbour framework to find out. In both cases, we make |
| 262 | * sure that br_handle_frame_finish() is called afterwards. |
| 263 | */ |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 264 | int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb) |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 265 | { |
David S. Miller | f6b72b6 | 2011-07-14 07:53:20 -0700 | [diff] [blame] | 266 | struct neighbour *neigh; |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 267 | struct dst_entry *dst; |
| 268 | |
| 269 | skb->dev = bridge_parent(skb->dev); |
| 270 | if (!skb->dev) |
| 271 | goto free_skb; |
| 272 | dst = skb_dst(skb); |
David S. Miller | f9d7516 | 2012-07-02 22:12:59 -0700 | [diff] [blame] | 273 | neigh = dst_neigh_lookup_skb(dst, skb); |
| 274 | if (neigh) { |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 275 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
David S. Miller | f9d7516 | 2012-07-02 22:12:59 -0700 | [diff] [blame] | 276 | int ret; |
| 277 | |
| 278 | if (neigh->hh.hh_len) { |
| 279 | neigh_hh_bridge(&neigh->hh, skb); |
| 280 | skb->dev = nf_bridge->physindev; |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 281 | ret = br_handle_frame_finish(net, sk, skb); |
David S. Miller | f9d7516 | 2012-07-02 22:12:59 -0700 | [diff] [blame] | 282 | } else { |
| 283 | /* the neighbour function below overwrites the complete |
| 284 | * MAC header, so we save the Ethernet source address and |
| 285 | * protocol number. |
| 286 | */ |
| 287 | skb_copy_from_linear_data_offset(skb, |
| 288 | -(ETH_HLEN-ETH_ALEN), |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 289 | nf_bridge->neigh_header, |
David S. Miller | f9d7516 | 2012-07-02 22:12:59 -0700 | [diff] [blame] | 290 | ETH_HLEN-ETH_ALEN); |
| 291 | /* tell br_dev_xmit to continue with forwarding */ |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 292 | nf_bridge->bridged_dnat = 1; |
Herbert Xu | 93fdd47 | 2014-10-05 12:00:22 +0800 | [diff] [blame] | 293 | /* FIXME Need to refragment */ |
David S. Miller | f9d7516 | 2012-07-02 22:12:59 -0700 | [diff] [blame] | 294 | ret = neigh->output(neigh, skb); |
| 295 | } |
| 296 | neigh_release(neigh); |
| 297 | return ret; |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 298 | } |
| 299 | free_skb: |
| 300 | kfree_skb(skb); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 304 | static inline bool |
| 305 | br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb, |
| 306 | const struct nf_bridge_info *nf_bridge) |
Florian Westphal | c055d5b | 2015-03-10 05:08:19 +0100 | [diff] [blame] | 307 | { |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 308 | return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr; |
Bernhard Thaler | 8cae308 | 2015-05-30 15:26:57 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | /* This requires some explaining. If DNAT has taken place, |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 312 | * we will need to fix up the destination Ethernet address. |
Florian Westphal | faecbb4 | 2015-05-20 13:42:25 +0200 | [diff] [blame] | 313 | * This is also true when SNAT takes place (for the reply direction). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | * |
| 315 | * There are two cases to consider: |
| 316 | * 1. The packet was DNAT'ed to a device in the same bridge |
| 317 | * port group as it was received on. We can still bridge |
| 318 | * the packet. |
| 319 | * 2. The packet was DNAT'ed to a different device, either |
| 320 | * a non-bridged device or another bridge port group. |
| 321 | * The packet will need to be routed. |
| 322 | * |
| 323 | * The correct way of distinguishing between these two cases is to |
| 324 | * call ip_route_input() and to look at skb->dst->dev, which is |
| 325 | * changed to the destination device if ip_route_input() succeeds. |
| 326 | * |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 327 | * Let's first consider the case that ip_route_input() succeeds: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | * |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 329 | * If the output device equals the logical bridge device the packet |
| 330 | * came in on, we can consider this bridging. The corresponding MAC |
| 331 | * address will be obtained in br_nf_pre_routing_finish_bridge. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * Otherwise, the packet is considered to be routed and we just |
| 333 | * change the destination MAC address so that the packet will |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 334 | * later be passed up to the IP stack to be routed. For a redirected |
| 335 | * packet, ip_route_input() will give back the localhost as output device, |
| 336 | * which differs from the bridge device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 338 | * Let's now consider the case that ip_route_input() fails: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | * |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 340 | * This can be because the destination address is martian, in which case |
| 341 | * the packet will be dropped. |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 342 | * If IP forwarding is disabled, ip_route_input() will fail, while |
| 343 | * ip_route_output_key() can return success. The source |
| 344 | * 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] | 345 | * 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] | 346 | * if IP forwarding is enabled. If the output device equals the logical bridge |
| 347 | * device, we proceed as if ip_route_input() succeeded. If it differs from the |
| 348 | * logical bridge port or if ip_route_output_key() fails we drop the packet. |
| 349 | */ |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 350 | static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
| 352 | struct net_device *dev = skb->dev; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 353 | struct iphdr *iph = ip_hdr(skb); |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 354 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 355 | struct rtable *rt; |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 356 | int err; |
Herbert Xu | 93fdd47 | 2014-10-05 12:00:22 +0800 | [diff] [blame] | 357 | |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 358 | nf_bridge->frag_max_size = IPCB(skb)->frag_max_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 360 | if (nf_bridge->pkt_otherhost) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | skb->pkt_type = PACKET_OTHERHOST; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 362 | nf_bridge->pkt_otherhost = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 364 | nf_bridge->in_prerouting = 0; |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 365 | if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) { |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 366 | if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) { |
Eric Dumazet | f3abc9b | 2009-08-24 19:35:38 +0200 | [diff] [blame] | 367 | struct in_device *in_dev = __in_dev_get_rcu(dev); |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 368 | |
| 369 | /* If err equals -EHOSTUNREACH the error is due to a |
| 370 | * martian destination or due to the fact that |
| 371 | * forwarding is disabled. For most martian packets, |
| 372 | * ip_route_output_key() will fail. It won't fail for 2 types of |
| 373 | * martian destinations: loopback destinations and destination |
| 374 | * 0.0.0.0. In both cases the packet will be dropped because the |
| 375 | * destination is the loopback device and not the bridge. */ |
| 376 | if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev)) |
| 377 | goto free_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Eric W. Biederman | f2d74cf | 2015-09-15 20:04:14 -0500 | [diff] [blame] | 379 | rt = ip_route_output(net, iph->daddr, 0, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 380 | RT_TOS(iph->tos), 0); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 381 | if (!IS_ERR(rt)) { |
Bart De Schuymer | 1c011be | 2005-09-14 20:55:16 -0700 | [diff] [blame] | 382 | /* - Bridged-and-DNAT'ed traffic doesn't |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 383 | * require ip_forwarding. */ |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 384 | if (rt->dst.dev == dev) { |
| 385 | skb_dst_set(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | goto bridged_dnat; |
| 387 | } |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 388 | ip_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
Bart De Schuymer | f216f08 | 2006-12-05 13:45:21 -0800 | [diff] [blame] | 390 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | kfree_skb(skb); |
| 392 | return 0; |
| 393 | } else { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 394 | if (skb_dst(skb)->dev == dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | bridged_dnat: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | skb->dev = nf_bridge->physindev; |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 397 | nf_bridge_update_protocol(skb); |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 398 | nf_bridge_push_encap_header(skb); |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 399 | br_nf_hook_thresh(NF_BR_PRE_ROUTING, |
| 400 | net, sk, skb, skb->dev, |
| 401 | NULL, |
Artur Molchanov | 259495a | 2016-12-30 19:46:36 +0300 | [diff] [blame] | 402 | br_nf_pre_routing_finish_bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
Joe Perches | 0409114 | 2014-02-23 00:05:26 -0800 | [diff] [blame] | 405 | ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr); |
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 | } |
Patrick McHardy | f9181f4 | 2010-06-15 17:31:06 +0200 | [diff] [blame] | 414 | skb_dst_set_noref(skb, &rt->dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | skb->dev = nf_bridge->physindev; |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 418 | nf_bridge_update_protocol(skb); |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 419 | nf_bridge_push_encap_header(skb); |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 420 | br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL, |
| 421 | br_handle_frame_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 425 | static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev) |
| 426 | { |
| 427 | struct net_device *vlan, *br; |
| 428 | |
| 429 | br = bridge_parent(dev); |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 430 | if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb)) |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 431 | return br; |
| 432 | |
dingtianhong | f06c7f9 | 2014-05-09 14:58:05 +0800 | [diff] [blame] | 433 | vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto, |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 434 | skb_vlan_tag_get(skb) & VLAN_VID_MASK); |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 435 | |
| 436 | return vlan ? vlan : br; |
| 437 | } |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* Some common code for IPv4/IPv6 */ |
Pablo Neira Ayuso | 230ac49 | 2015-06-16 14:07:03 +0200 | [diff] [blame] | 440 | struct net_device *setup_pre_routing(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 442 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 445 | skb->pkt_type = PACKET_HOST; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 446 | nf_bridge->pkt_otherhost = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 449 | nf_bridge->in_prerouting = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | nf_bridge->physindev = skb->dev; |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 451 | skb->dev = brnf_get_logical_dev(skb, skb->dev); |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 452 | |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 453 | if (skb->protocol == htons(ETH_P_8021Q)) |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 454 | nf_bridge->orig_proto = BRNF_PROTO_8021Q; |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 455 | else if (skb->protocol == htons(ETH_P_PPP_SES)) |
Florian Westphal | 3eaf402 | 2015-04-02 14:31:44 +0200 | [diff] [blame] | 456 | nf_bridge->orig_proto = BRNF_PROTO_PPPOE; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 457 | |
Florian Westphal | 6b8dbcf | 2013-10-24 21:32:42 +0200 | [diff] [blame] | 458 | /* Must drop socket now because of tproxy. */ |
| 459 | skb_orphan(skb); |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 460 | return skb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* Direct IPv6 traffic to br_nf_pre_routing_ipv6. |
| 464 | * Replicate the checks that IPv4 does on packet reception. |
| 465 | * Set skb->dev to the bridge device (i.e. parent of the |
| 466 | * receiving device) to make netfilter happy, the REDIRECT |
| 467 | * target in particular. Save the original destination IP |
| 468 | * address to be able to detect DNAT afterwards. */ |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 469 | static unsigned int br_nf_pre_routing(void *priv, |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 470 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 471 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Florian Westphal | faecbb4 | 2015-05-20 13:42:25 +0200 | [diff] [blame] | 473 | struct nf_bridge_info *nf_bridge; |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 474 | struct net_bridge_port *p; |
| 475 | struct net_bridge *br; |
Evgeniy Polyakov | e7c243c | 2007-08-24 23:36:29 -0700 | [diff] [blame] | 476 | __u32 len = nf_bridge_encap_header_len(skb); |
| 477 | |
Evgeniy Polyakov | e7c243c | 2007-08-24 23:36:29 -0700 | [diff] [blame] | 478 | if (unlikely(!pskb_may_pull(skb, len))) |
Herbert Xu | deef4b5 | 2010-12-09 17:38:11 +0000 | [diff] [blame] | 479 | return NF_DROP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 481 | p = br_port_get_rcu(state->in); |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 482 | if (p == NULL) |
Herbert Xu | deef4b5 | 2010-12-09 17:38:11 +0000 | [diff] [blame] | 483 | return NF_DROP; |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 484 | br = p->br; |
| 485 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 486 | if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) { |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 487 | if (!brnf_call_ip6tables && !br->nf_call_ip6tables) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return NF_ACCEPT; |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 489 | |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 490 | nf_bridge_pull_encap_header_rcsum(skb); |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 491 | return br_nf_pre_routing_ipv6(priv, skb, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 493 | |
| 494 | if (!brnf_call_iptables && !br->nf_call_iptables) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 497 | if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | return NF_ACCEPT; |
| 499 | |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 500 | nf_bridge_pull_encap_header_rcsum(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 502 | if (br_validate_ipv4(state->net, skb)) |
Herbert Xu | deef4b5 | 2010-12-09 17:38:11 +0000 | [diff] [blame] | 503 | return NF_DROP; |
Herbert Xu | 1776206 | 2010-07-05 21:29:28 +0000 | [diff] [blame] | 504 | |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 505 | nf_bridge_put(skb->nf_bridge); |
Stephen Hemminger | fdeabde | 2006-03-20 22:58:21 -0800 | [diff] [blame] | 506 | if (!nf_bridge_alloc(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | return NF_DROP; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 508 | if (!setup_pre_routing(skb)) |
| 509 | return NF_DROP; |
Florian Westphal | c055d5b | 2015-03-10 05:08:19 +0100 | [diff] [blame] | 510 | |
Florian Westphal | faecbb4 | 2015-05-20 13:42:25 +0200 | [diff] [blame] | 511 | nf_bridge = nf_bridge_info_get(skb); |
| 512 | nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr; |
| 513 | |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 514 | skb->protocol = htons(ETH_P_IP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 516 | NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 517 | skb->dev, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | br_nf_pre_routing_finish); |
| 519 | |
| 520 | return NF_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* PF_BRIDGE/FORWARD *************************************************/ |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 525 | static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | { |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 527 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct net_device *in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 530 | if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) { |
Florian Westphal | 0b67c43 | 2015-04-01 22:36:27 +0200 | [diff] [blame] | 531 | |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 532 | if (skb->protocol == htons(ETH_P_IP)) |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 533 | nf_bridge->frag_max_size = IPCB(skb)->frag_max_size; |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 534 | |
| 535 | if (skb->protocol == htons(ETH_P_IPV6)) |
| 536 | nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size; |
Florian Westphal | 0b67c43 | 2015-04-01 22:36:27 +0200 | [diff] [blame] | 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | in = nf_bridge->physindev; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 539 | if (nf_bridge->pkt_otherhost) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | skb->pkt_type = PACKET_OTHERHOST; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 541 | nf_bridge->pkt_otherhost = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
Bart De Schuymer | e94c674 | 2010-05-13 14:55:34 +0200 | [diff] [blame] | 543 | nf_bridge_update_protocol(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } else { |
| 545 | in = *((struct net_device **)(skb->cb)); |
| 546 | } |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 547 | nf_bridge_push_encap_header(skb); |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 548 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 549 | NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, net, sk, skb, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 550 | in, skb->dev, br_forward_finish, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | /* This is the 'purely bridged' case. For IP, we pass the packet to |
| 556 | * netfilter with indev and outdev set to the bridge device, |
| 557 | * but we are still able to filter on the 'real' indev/outdev |
| 558 | * because of the physdev module. For ARP, indev and outdev are the |
| 559 | * bridge ports. */ |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 560 | static unsigned int br_nf_forward_ip(void *priv, |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 561 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 562 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | struct nf_bridge_info *nf_bridge; |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 565 | struct net_device *parent; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 566 | u_int8_t pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
| 568 | if (!skb->nf_bridge) |
| 569 | return NF_ACCEPT; |
| 570 | |
Patrick McHardy | 2dc2f20 | 2008-01-20 06:25:48 -0800 | [diff] [blame] | 571 | /* Need exclusive nf_bridge_info since we might have multiple |
| 572 | * different physoutdevs. */ |
| 573 | if (!nf_bridge_unshare(skb)) |
| 574 | return NF_DROP; |
| 575 | |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 576 | nf_bridge = nf_bridge_info_get(skb); |
| 577 | if (!nf_bridge) |
| 578 | return NF_DROP; |
| 579 | |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 580 | parent = bridge_parent(state->out); |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 581 | if (!parent) |
| 582 | return NF_DROP; |
| 583 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 584 | if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb)) |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 585 | pf = NFPROTO_IPV4; |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 586 | else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 587 | pf = NFPROTO_IPV6; |
Herbert Xu | a2bd40a | 2009-01-12 00:06:02 +0000 | [diff] [blame] | 588 | else |
| 589 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 591 | nf_bridge_pull_encap_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 594 | skb->pkt_type = PACKET_HOST; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 595 | nf_bridge->pkt_otherhost = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Florian Westphal | 0b67c43 | 2015-04-01 22:36:27 +0200 | [diff] [blame] | 598 | if (pf == NFPROTO_IPV4) { |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 599 | if (br_validate_ipv4(state->net, skb)) |
Florian Westphal | 0b67c43 | 2015-04-01 22:36:27 +0200 | [diff] [blame] | 600 | return NF_DROP; |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 601 | IPCB(skb)->frag_max_size = nf_bridge->frag_max_size; |
Florian Westphal | 0b67c43 | 2015-04-01 22:36:27 +0200 | [diff] [blame] | 602 | } |
Herbert Xu | 6b1e960 | 2011-03-18 05:27:28 +0000 | [diff] [blame] | 603 | |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 604 | if (pf == NFPROTO_IPV6) { |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 605 | if (br_validate_ipv6(state->net, skb)) |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 606 | return NF_DROP; |
| 607 | IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size; |
| 608 | } |
| 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | nf_bridge->physoutdev = skb->dev; |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 611 | if (pf == NFPROTO_IPV4) |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 612 | skb->protocol = htons(ETH_P_IP); |
| 613 | else |
| 614 | skb->protocol = htons(ETH_P_IPV6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 616 | NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 617 | brnf_get_logical_dev(skb, state->in), |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 618 | parent, br_nf_forward_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | return NF_STOLEN; |
| 621 | } |
| 622 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 623 | static unsigned int br_nf_forward_arp(void *priv, |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 624 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 625 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 627 | struct net_bridge_port *p; |
| 628 | struct net_bridge *br; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | struct net_device **d = (struct net_device **)(skb->cb); |
| 630 | |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 631 | p = br_port_get_rcu(state->out); |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 632 | if (p == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | return NF_ACCEPT; |
Patrick McHardy | 4df53d8 | 2010-07-02 09:32:57 +0200 | [diff] [blame] | 634 | br = p->br; |
| 635 | |
| 636 | if (!brnf_call_arptables && !br->nf_call_arptables) |
| 637 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 639 | if (!IS_ARP(skb)) { |
Stephen Hemminger | 8b42ec3 | 2006-03-20 22:58:05 -0800 | [diff] [blame] | 640 | if (!IS_VLAN_ARP(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return NF_ACCEPT; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 642 | nf_bridge_pull_encap_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Arnaldo Carvalho de Melo | d0a92be | 2007-03-12 20:56:31 -0300 | [diff] [blame] | 645 | if (arp_hdr(skb)->ar_pln != 4) { |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 646 | if (IS_VLAN_ARP(skb)) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 647 | nf_bridge_push_encap_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | return NF_ACCEPT; |
| 649 | } |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 650 | *d = state->in; |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 651 | NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 652 | state->in, state->out, br_nf_forward_finish); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | return NF_STOLEN; |
| 655 | } |
| 656 | |
Eric W. Biederman | 6532948 | 2015-09-15 20:04:13 -0500 | [diff] [blame] | 657 | static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
Florian Westphal | 8bd63cf | 2015-03-05 00:52:33 +0100 | [diff] [blame] | 658 | { |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 659 | struct brnf_frag_data *data; |
| 660 | int err; |
| 661 | |
| 662 | data = this_cpu_ptr(&brnf_frag_data_storage); |
| 663 | err = skb_cow_head(skb, data->size); |
| 664 | |
| 665 | if (err) { |
Florian Westphal | 8bd63cf | 2015-03-05 00:52:33 +0100 | [diff] [blame] | 666 | kfree_skb(skb); |
| 667 | return 0; |
| 668 | } |
| 669 | |
Florian Westphal | d7b5974 | 2015-06-05 13:27:13 +0200 | [diff] [blame] | 670 | if (data->vlan_tci) { |
| 671 | skb->vlan_tci = data->vlan_tci; |
| 672 | skb->vlan_proto = data->vlan_proto; |
| 673 | } |
| 674 | |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 675 | skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size); |
| 676 | __skb_push(skb, data->encap_size); |
| 677 | |
Florian Westphal | a9fcc6a | 2015-05-03 22:06:07 +0200 | [diff] [blame] | 678 | nf_bridge_info_free(skb); |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 679 | return br_dev_queue_push_xmit(net, sk, skb); |
Florian Westphal | 8bd63cf | 2015-03-05 00:52:33 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Eric W. Biederman | 8d4df0b9 | 2015-09-15 20:04:12 -0500 | [diff] [blame] | 682 | static int |
| 683 | br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 684 | int (*output)(struct net *, struct sock *, struct sk_buff *)) |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 685 | { |
Shmulik Ladkani | fedbb6b4 | 2016-06-29 21:47:03 +0300 | [diff] [blame] | 686 | unsigned int mtu = ip_skb_dst_mtu(sk, skb); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 687 | struct iphdr *iph = ip_hdr(skb); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 688 | |
| 689 | if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) || |
| 690 | (IPCB(skb)->frag_max_size && |
| 691 | IPCB(skb)->frag_max_size > mtu))) { |
Eric W. Biederman | 8d4df0b9 | 2015-09-15 20:04:12 -0500 | [diff] [blame] | 692 | IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 693 | kfree_skb(skb); |
| 694 | return -EMSGSIZE; |
| 695 | } |
| 696 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 697 | return ip_do_fragment(net, sk, skb, output); |
Andy Zhou | 49d16b2 | 2015-05-15 14:15:37 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Florian Westphal | 33b1f31 | 2015-06-05 13:28:38 +0200 | [diff] [blame] | 700 | static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb) |
| 701 | { |
| 702 | if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE) |
| 703 | return PPPOE_SES_HLEN; |
| 704 | return 0; |
| 705 | } |
| 706 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 707 | static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 708 | { |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 709 | struct nf_bridge_info *nf_bridge; |
Florian Westphal | 7a8d831 | 2015-03-05 00:52:36 +0100 | [diff] [blame] | 710 | unsigned int mtu_reserved; |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 711 | |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 712 | mtu_reserved = nf_bridge_mtu_reduction(skb); |
| 713 | |
| 714 | if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) { |
Florian Westphal | a9fcc6a | 2015-05-03 22:06:07 +0200 | [diff] [blame] | 715 | nf_bridge_info_free(skb); |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 716 | return br_dev_queue_push_xmit(net, sk, skb); |
Florian Westphal | a9fcc6a | 2015-05-03 22:06:07 +0200 | [diff] [blame] | 717 | } |
Florian Westphal | 7a8d831 | 2015-03-05 00:52:36 +0100 | [diff] [blame] | 718 | |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 719 | nf_bridge = nf_bridge_info_get(skb); |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 720 | |
Herbert Xu | 93fdd47 | 2014-10-05 12:00:22 +0800 | [diff] [blame] | 721 | /* This is wrong! We should preserve the original fragment |
| 722 | * boundaries by preserving frag_list rather than refragmenting. |
| 723 | */ |
Arnd Bergmann | c932245 | 2015-10-08 14:30:15 +0200 | [diff] [blame] | 724 | if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) && |
| 725 | skb->protocol == htons(ETH_P_IP)) { |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 726 | struct brnf_frag_data *data; |
| 727 | |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 728 | if (br_validate_ipv4(net, skb)) |
Florian Westphal | dd302b5 | 2015-06-30 22:27:51 +0200 | [diff] [blame] | 729 | goto drop; |
Bernhard Thaler | 411ffb4 | 2015-05-30 15:28:28 +0200 | [diff] [blame] | 730 | |
| 731 | IPCB(skb)->frag_max_size = nf_bridge->frag_max_size; |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 732 | |
| 733 | nf_bridge_update_protocol(skb); |
| 734 | |
| 735 | data = this_cpu_ptr(&brnf_frag_data_storage); |
Florian Westphal | d7b5974 | 2015-06-05 13:27:13 +0200 | [diff] [blame] | 736 | |
| 737 | data->vlan_tci = skb->vlan_tci; |
| 738 | data->vlan_proto = skb->vlan_proto; |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 739 | data->encap_size = nf_bridge_encap_header_len(skb); |
| 740 | data->size = ETH_HLEN + data->encap_size; |
| 741 | |
| 742 | skb_copy_from_linear_data_offset(skb, -data->size, data->mac, |
| 743 | data->size); |
| 744 | |
Eric W. Biederman | 694869b | 2015-06-12 21:55:31 -0500 | [diff] [blame] | 745 | return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit); |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 746 | } |
Arnd Bergmann | c932245 | 2015-10-08 14:30:15 +0200 | [diff] [blame] | 747 | if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) && |
| 748 | skb->protocol == htons(ETH_P_IPV6)) { |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 749 | const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops(); |
| 750 | struct brnf_frag_data *data; |
Bandan Das | 462fb2a | 2010-09-19 09:34:33 +0000 | [diff] [blame] | 751 | |
Eric W. Biederman | c1444c6 | 2015-09-25 16:52:51 -0500 | [diff] [blame] | 752 | if (br_validate_ipv6(net, skb)) |
Florian Westphal | dd302b5 | 2015-06-30 22:27:51 +0200 | [diff] [blame] | 753 | goto drop; |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 754 | |
| 755 | IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size; |
| 756 | |
| 757 | nf_bridge_update_protocol(skb); |
| 758 | |
| 759 | data = this_cpu_ptr(&brnf_frag_data_storage); |
| 760 | data->encap_size = nf_bridge_encap_header_len(skb); |
| 761 | data->size = ETH_HLEN + data->encap_size; |
| 762 | |
| 763 | skb_copy_from_linear_data_offset(skb, -data->size, data->mac, |
| 764 | data->size); |
| 765 | |
| 766 | if (v6ops) |
Eric W. Biederman | 7d8c6e3 | 2015-06-12 22:12:04 -0500 | [diff] [blame] | 767 | return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit); |
Florian Westphal | dd302b5 | 2015-06-30 22:27:51 +0200 | [diff] [blame] | 768 | |
| 769 | kfree_skb(skb); |
| 770 | return -EMSGSIZE; |
Bernhard Thaler | efb6de9 | 2015-05-30 15:30:16 +0200 | [diff] [blame] | 771 | } |
Florian Westphal | a9fcc6a | 2015-05-03 22:06:07 +0200 | [diff] [blame] | 772 | nf_bridge_info_free(skb); |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 773 | return br_dev_queue_push_xmit(net, sk, skb); |
Florian Westphal | dd302b5 | 2015-06-30 22:27:51 +0200 | [diff] [blame] | 774 | drop: |
| 775 | kfree_skb(skb); |
| 776 | return 0; |
hummerbliss@gmail.com | c197fac | 2009-04-20 17:12:35 +0200 | [diff] [blame] | 777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | /* PF_BRIDGE/POST_ROUTING ********************************************/ |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 780 | static unsigned int br_nf_post_routing(void *priv, |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 781 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 782 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 784 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | struct net_device *realoutdev = bridge_parent(skb->dev); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 786 | u_int8_t pf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Florian Westphal | e4bb9bc | 2015-03-10 10:36:48 +0100 | [diff] [blame] | 788 | /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in |
| 789 | * on a bridge, but was delivered locally and is now being routed: |
| 790 | * |
| 791 | * POST_ROUTING was already invoked from the ip stack. |
| 792 | */ |
| 793 | if (!nf_bridge || !nf_bridge->physoutdev) |
Patrick McHardy | 81d9dda | 2007-11-13 02:58:44 -0800 | [diff] [blame] | 794 | return NF_ACCEPT; |
| 795 | |
Stephen Hemminger | 5dce971 | 2006-02-09 17:09:38 -0800 | [diff] [blame] | 796 | if (!realoutdev) |
| 797 | return NF_DROP; |
| 798 | |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 799 | if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb)) |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 800 | pf = NFPROTO_IPV4; |
Florian Westphal | 739e450 | 2012-03-06 01:22:54 +0000 | [diff] [blame] | 801 | else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 802 | pf = NFPROTO_IPV6; |
Herbert Xu | a2bd40a | 2009-01-12 00:06:02 +0000 | [diff] [blame] | 803 | else |
| 804 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | /* We assume any code from br_dev_queue_push_xmit onwards doesn't care |
| 807 | * about the value of skb->pkt_type. */ |
| 808 | if (skb->pkt_type == PACKET_OTHERHOST) { |
| 809 | skb->pkt_type = PACKET_HOST; |
Florian Westphal | a1e6795 | 2015-04-02 14:31:45 +0200 | [diff] [blame] | 810 | nf_bridge->pkt_otherhost = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Patrick McHardy | fc38582 | 2007-05-03 03:36:16 -0700 | [diff] [blame] | 813 | nf_bridge_pull_encap_header(skb); |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 814 | if (pf == NFPROTO_IPV4) |
Bart De Schuymer | e179e63 | 2010-04-15 12:26:39 +0200 | [diff] [blame] | 815 | skb->protocol = htons(ETH_P_IP); |
| 816 | else |
| 817 | skb->protocol = htons(ETH_P_IPV6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 819 | NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb, |
David Miller | 7026b1d | 2015-04-05 22:19:04 -0400 | [diff] [blame] | 820 | NULL, realoutdev, |
Patrick McHardy | 2e2f7ae | 2006-04-04 13:42:35 -0700 | [diff] [blame] | 821 | br_nf_dev_queue_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | return NF_STOLEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | /* IP/SABOTAGE *****************************************************/ |
| 827 | /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING |
| 828 | * for the second time. */ |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 829 | static unsigned int ip_sabotage_in(void *priv, |
Patrick McHardy | 795aa6e | 2013-10-10 09:21:55 +0200 | [diff] [blame] | 830 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 831 | const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 833 | if (skb->nf_bridge && !skb->nf_bridge->in_prerouting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return NF_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | return NF_ACCEPT; |
| 837 | } |
| 838 | |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 839 | /* This is called when br_netfilter has called into iptables/netfilter, |
| 840 | * and DNAT has taken place on a bridge-forwarded packet. |
| 841 | * |
| 842 | * neigh->output has created a new MAC header, with local br0 MAC |
| 843 | * as saddr. |
| 844 | * |
| 845 | * This restores the original MAC saddr of the bridged packet |
| 846 | * before invoking bridge forward logic to transmit the packet. |
| 847 | */ |
| 848 | static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb) |
| 849 | { |
Florian Westphal | 3833078 | 2015-04-02 14:31:43 +0200 | [diff] [blame] | 850 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 851 | |
| 852 | skb_pull(skb, ETH_HLEN); |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 853 | nf_bridge->bridged_dnat = 0; |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 854 | |
Florian Westphal | e70deec | 2015-04-02 14:31:40 +0200 | [diff] [blame] | 855 | BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN)); |
| 856 | |
| 857 | skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN), |
| 858 | nf_bridge->neigh_header, |
| 859 | ETH_HLEN - ETH_ALEN); |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 860 | skb->dev = nf_bridge->physindev; |
Florian Westphal | 7fb48c5 | 2015-05-03 22:05:28 +0200 | [diff] [blame] | 861 | |
| 862 | nf_bridge->physoutdev = NULL; |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 863 | br_handle_frame_finish(dev_net(skb->dev), NULL, skb); |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Pablo Neira Ayuso | 1a4ba64 | 2015-03-10 10:27:18 +0100 | [diff] [blame] | 866 | static int br_nf_dev_xmit(struct sk_buff *skb) |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 867 | { |
Florian Westphal | 72b1e5e | 2015-07-23 16:21:30 +0200 | [diff] [blame] | 868 | if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) { |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 869 | br_nf_pre_routing_finish_bridge_slow(skb); |
| 870 | return 1; |
| 871 | } |
| 872 | return 0; |
| 873 | } |
Pablo Neira Ayuso | 1a4ba64 | 2015-03-10 10:27:18 +0100 | [diff] [blame] | 874 | |
| 875 | static const struct nf_br_ops br_ops = { |
| 876 | .br_dev_xmit_hook = br_nf_dev_xmit, |
| 877 | }; |
Pablo Neira Ayuso | e5de75b | 2015-03-09 12:30:12 +0100 | [diff] [blame] | 878 | |
Pablo Neira Ayuso | 4b7fd5d | 2014-10-02 11:13:21 +0200 | [diff] [blame] | 879 | void br_netfilter_enable(void) |
| 880 | { |
| 881 | } |
| 882 | EXPORT_SYMBOL_GPL(br_netfilter_enable); |
| 883 | |
Bart De Schuymer | ea2d9b4 | 2010-04-15 12:14:51 +0200 | [diff] [blame] | 884 | /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because |
| 885 | * br_dev_queue_push_xmit is called afterwards */ |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 886 | static struct nf_hook_ops br_nf_ops[] __read_mostly = { |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 887 | { |
| 888 | .hook = br_nf_pre_routing, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 889 | .pf = NFPROTO_BRIDGE, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 890 | .hooknum = NF_BR_PRE_ROUTING, |
| 891 | .priority = NF_BR_PRI_BRNF, |
| 892 | }, |
| 893 | { |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 894 | .hook = br_nf_forward_ip, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 895 | .pf = NFPROTO_BRIDGE, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 896 | .hooknum = NF_BR_FORWARD, |
| 897 | .priority = NF_BR_PRI_BRNF - 1, |
| 898 | }, |
| 899 | { |
| 900 | .hook = br_nf_forward_arp, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 901 | .pf = NFPROTO_BRIDGE, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 902 | .hooknum = NF_BR_FORWARD, |
| 903 | .priority = NF_BR_PRI_BRNF, |
| 904 | }, |
| 905 | { |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 906 | .hook = br_nf_post_routing, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 907 | .pf = NFPROTO_BRIDGE, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 908 | .hooknum = NF_BR_POST_ROUTING, |
| 909 | .priority = NF_BR_PRI_LAST, |
| 910 | }, |
| 911 | { |
| 912 | .hook = ip_sabotage_in, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 913 | .pf = NFPROTO_IPV4, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 914 | .hooknum = NF_INET_PRE_ROUTING, |
| 915 | .priority = NF_IP_PRI_FIRST, |
| 916 | }, |
| 917 | { |
| 918 | .hook = ip_sabotage_in, |
Alban Crequy | aa740f4 | 2012-05-14 03:56:36 +0000 | [diff] [blame] | 919 | .pf = NFPROTO_IPV6, |
Cyrill Gorcunov | 1490fd8 | 2009-07-03 20:11:57 +0000 | [diff] [blame] | 920 | .hooknum = NF_INET_PRE_ROUTING, |
| 921 | .priority = NF_IP6_PRI_FIRST, |
| 922 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | }; |
| 924 | |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 925 | static int brnf_device_event(struct notifier_block *unused, unsigned long event, |
| 926 | void *ptr) |
| 927 | { |
| 928 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 929 | struct brnf_net *brnet; |
| 930 | struct net *net; |
| 931 | int ret; |
| 932 | |
| 933 | if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE)) |
| 934 | return NOTIFY_DONE; |
| 935 | |
| 936 | ASSERT_RTNL(); |
| 937 | |
| 938 | net = dev_net(dev); |
| 939 | brnet = net_generic(net, brnf_net_id); |
| 940 | if (brnet->enabled) |
| 941 | return NOTIFY_OK; |
| 942 | |
| 943 | ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops)); |
| 944 | if (ret) |
| 945 | return NOTIFY_BAD; |
| 946 | |
| 947 | brnet->enabled = true; |
| 948 | return NOTIFY_OK; |
| 949 | } |
| 950 | |
| 951 | static void __net_exit brnf_exit_net(struct net *net) |
| 952 | { |
| 953 | struct brnf_net *brnet = net_generic(net, brnf_net_id); |
| 954 | |
| 955 | if (!brnet->enabled) |
| 956 | return; |
| 957 | |
| 958 | nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops)); |
| 959 | brnet->enabled = false; |
| 960 | } |
| 961 | |
| 962 | static struct pernet_operations brnf_net_ops __read_mostly = { |
| 963 | .exit = brnf_exit_net, |
| 964 | .id = &brnf_net_id, |
| 965 | .size = sizeof(struct brnf_net), |
| 966 | }; |
| 967 | |
| 968 | static struct notifier_block brnf_notifier __read_mostly = { |
| 969 | .notifier_call = brnf_device_event, |
| 970 | }; |
| 971 | |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 972 | /* recursively invokes nf_hook_slow (again), skipping already-called |
| 973 | * hooks (< NF_BR_PRI_BRNF). |
| 974 | * |
| 975 | * Called with rcu read lock held. |
| 976 | */ |
| 977 | int br_nf_hook_thresh(unsigned int hook, struct net *net, |
| 978 | struct sock *sk, struct sk_buff *skb, |
| 979 | struct net_device *indev, |
| 980 | struct net_device *outdev, |
| 981 | int (*okfn)(struct net *, struct sock *, |
| 982 | struct sk_buff *)) |
| 983 | { |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 984 | struct nf_hook_entry *elem; |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 985 | struct nf_hook_state state; |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 986 | int ret; |
| 987 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 988 | elem = rcu_dereference(net->nf.hooks[NFPROTO_BRIDGE][hook]); |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 989 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 990 | while (elem && (elem->ops.priority <= NF_BR_PRI_BRNF)) |
| 991 | elem = rcu_dereference(elem->next); |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 992 | |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 993 | if (!elem) |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 994 | return okfn(net, sk, skb); |
| 995 | |
| 996 | /* We may already have this, but read-locks nest anyway */ |
| 997 | rcu_read_lock(); |
Aaron Conole | e3b37f1 | 2016-09-21 11:35:07 -0400 | [diff] [blame] | 998 | nf_hook_state_init(&state, elem, hook, NF_BR_PRI_BRNF + 1, |
Florian Westphal | c5136b1 | 2016-09-21 11:35:01 -0400 | [diff] [blame] | 999 | NFPROTO_BRIDGE, indev, outdev, sk, net, okfn); |
| 1000 | |
| 1001 | ret = nf_hook_slow(skb, &state); |
| 1002 | rcu_read_unlock(); |
| 1003 | if (ret == 1) |
| 1004 | ret = okfn(net, sk, skb); |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | #ifdef CONFIG_SYSCTL |
| 1010 | static |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 1011 | int brnf_sysctl_call_tables(struct ctl_table *ctl, int write, |
tanxiaojun | 56b148e | 2013-12-19 13:28:13 +0800 | [diff] [blame] | 1012 | void __user *buffer, size_t *lenp, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { |
| 1014 | int ret; |
| 1015 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 1016 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
| 1018 | if (write && *(int *)(ctl->data)) |
| 1019 | *(int *)(ctl->data) = 1; |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
Joe Perches | fe2c633 | 2013-06-11 23:04:25 -0700 | [diff] [blame] | 1023 | static struct ctl_table brnf_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | .procname = "bridge-nf-call-arptables", |
| 1026 | .data = &brnf_call_arptables, |
| 1027 | .maxlen = sizeof(int), |
| 1028 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1029 | .proc_handler = brnf_sysctl_call_tables, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | }, |
| 1031 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | .procname = "bridge-nf-call-iptables", |
| 1033 | .data = &brnf_call_iptables, |
| 1034 | .maxlen = sizeof(int), |
| 1035 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1036 | .proc_handler = brnf_sysctl_call_tables, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | }, |
| 1038 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | .procname = "bridge-nf-call-ip6tables", |
| 1040 | .data = &brnf_call_ip6tables, |
| 1041 | .maxlen = sizeof(int), |
| 1042 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1043 | .proc_handler = brnf_sysctl_call_tables, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | }, |
| 1045 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | .procname = "bridge-nf-filter-vlan-tagged", |
| 1047 | .data = &brnf_filter_vlan_tagged, |
| 1048 | .maxlen = sizeof(int), |
| 1049 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1050 | .proc_handler = brnf_sysctl_call_tables, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | }, |
Michael Milner | 516299d | 2007-04-12 22:14:23 -0700 | [diff] [blame] | 1052 | { |
Michael Milner | 516299d | 2007-04-12 22:14:23 -0700 | [diff] [blame] | 1053 | .procname = "bridge-nf-filter-pppoe-tagged", |
| 1054 | .data = &brnf_filter_pppoe_tagged, |
| 1055 | .maxlen = sizeof(int), |
| 1056 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 1057 | .proc_handler = brnf_sysctl_call_tables, |
Michael Milner | 516299d | 2007-04-12 22:14:23 -0700 | [diff] [blame] | 1058 | }, |
Pablo Neira Ayuso | 4981682 | 2012-05-08 19:36:44 +0200 | [diff] [blame] | 1059 | { |
| 1060 | .procname = "bridge-nf-pass-vlan-input-dev", |
| 1061 | .data = &brnf_pass_vlan_indev, |
| 1062 | .maxlen = sizeof(int), |
| 1063 | .mode = 0644, |
| 1064 | .proc_handler = brnf_sysctl_call_tables, |
| 1065 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 1066 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | #endif |
| 1069 | |
Pablo Neira Ayuso | 34666d4 | 2014-09-18 11:29:03 +0200 | [diff] [blame] | 1070 | static int __init br_netfilter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | { |
Patrick McHardy | 5eb87f4 | 2007-02-07 15:07:22 -0800 | [diff] [blame] | 1072 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 1074 | ret = register_pernet_subsys(&brnf_net_ops); |
Patrick McHardy | 5eb87f4 | 2007-02-07 15:07:22 -0800 | [diff] [blame] | 1075 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | return ret; |
Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1077 | |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 1078 | ret = register_netdevice_notifier(&brnf_notifier); |
| 1079 | if (ret < 0) { |
| 1080 | unregister_pernet_subsys(&brnf_net_ops); |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | ec8f23c | 2012-04-19 13:44:49 +0000 | [diff] [blame] | 1085 | brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (brnf_sysctl_header == NULL) { |
Stephen Hemminger | 789bc3e | 2006-03-20 22:57:32 -0800 | [diff] [blame] | 1087 | printk(KERN_WARNING |
| 1088 | "br_netfilter: can't register to sysctl.\n"); |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 1089 | unregister_netdevice_notifier(&brnf_notifier); |
| 1090 | unregister_pernet_subsys(&brnf_net_ops); |
Geert Uytterhoeven | 9672723 | 2015-02-12 15:17:27 +0100 | [diff] [blame] | 1091 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
| 1093 | #endif |
Pablo Neira Ayuso | 1a4ba64 | 2015-03-10 10:27:18 +0100 | [diff] [blame] | 1094 | RCU_INIT_POINTER(nf_br_ops, &br_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | printk(KERN_NOTICE "Bridge firewalling registered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
Pablo Neira Ayuso | 34666d4 | 2014-09-18 11:29:03 +0200 | [diff] [blame] | 1099 | static void __exit br_netfilter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
Pablo Neira Ayuso | 1a4ba64 | 2015-03-10 10:27:18 +0100 | [diff] [blame] | 1101 | RCU_INIT_POINTER(nf_br_ops, NULL); |
Florian Westphal | 5f6c253 | 2016-02-25 10:08:37 +0100 | [diff] [blame] | 1102 | unregister_netdevice_notifier(&brnf_notifier); |
| 1103 | unregister_pernet_subsys(&brnf_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | #ifdef CONFIG_SYSCTL |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 1105 | unregister_net_sysctl_table(brnf_sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
Pablo Neira Ayuso | 34666d4 | 2014-09-18 11:29:03 +0200 | [diff] [blame] | 1108 | |
| 1109 | module_init(br_netfilter_init); |
| 1110 | module_exit(br_netfilter_fini); |
| 1111 | |
| 1112 | MODULE_LICENSE("GPL"); |
| 1113 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>"); |
| 1114 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); |
| 1115 | MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge"); |