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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*
  3  *      Handle firewalling
  4  *      Linux ethernet bridge
  5  *
  6  *      Authors:
  7  *      Lennert Buytenhek               <buytenh@gnu.org>
  8  *      Bart De Schuymer                <bdschuym@pandora.be>
  9  *
 10  *      Lennert dedicates this file to Kerstin Wurdinger.
 11  */
 12 
 13 #include <linux/module.h>
 14 #include <linux/kernel.h>
 15 #include <linux/slab.h>
 16 #include <linux/ip.h>
 17 #include <linux/netdevice.h>
 18 #include <linux/skbuff.h>
 19 #include <linux/if_arp.h>
 20 #include <linux/if_ether.h>
 21 #include <linux/if_vlan.h>
 22 #include <linux/if_pppox.h>
 23 #include <linux/ppp_defs.h>
 24 #include <linux/netfilter_bridge.h>
 25 #include <uapi/linux/netfilter_bridge.h>
 26 #include <linux/netfilter_ipv4.h>
 27 #include <linux/netfilter_ipv6.h>
 28 #include <linux/netfilter_arp.h>
 29 #include <linux/in_route.h>
 30 #include <linux/rculist.h>
 31 #include <linux/inetdevice.h>
 32 
 33 #include <net/ip.h>
 34 #include <net/ipv6.h>
 35 #include <net/addrconf.h>
 36 #include <net/route.h>
 37 #include <net/netfilter/br_netfilter.h>
 38 #include <net/netns/generic.h>
 39 
 40 #include <linux/uaccess.h>
 41 #include "br_private.h"
 42 #ifdef CONFIG_SYSCTL
 43 #include <linux/sysctl.h>
 44 #endif
 45 
 46 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
 47 #include <net/netfilter/nf_conntrack_core.h>
 48 #endif
 49 
 50 static unsigned int brnf_net_id __read_mostly;
 51 
 52 struct brnf_net {
 53         bool enabled;
 54 
 55 #ifdef CONFIG_SYSCTL
 56         struct ctl_table_header *ctl_hdr;
 57 #endif
 58 
 59         /* default value is 1 */
 60         int call_iptables;
 61         int call_ip6tables;
 62         int call_arptables;
 63 
 64         /* default value is 0 */
 65         int filter_vlan_tagged;
 66         int filter_pppoe_tagged;
 67         int pass_vlan_indev;
 68 };
 69 
 70 #define IS_IP(skb) \
 71         (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP))
 72 
 73 #define IS_IPV6(skb) \
 74         (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6))
 75 
 76 #define IS_ARP(skb) \
 77         (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
 78 
 79 static inline __be16 vlan_proto(const struct sk_buff *skb)
 80 {
 81         if (skb_vlan_tag_present(skb))
 82                 return skb->protocol;
 83         else if (skb->protocol == htons(ETH_P_8021Q))
 84                 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
 85         else
 86                 return 0;
 87 }
 88 
 89 static inline bool is_vlan_ip(const struct sk_buff *skb, const struct net *net)
 90 {
 91         struct brnf_net *brnet = net_generic(net, brnf_net_id);
 92 
 93         return vlan_proto(skb) == htons(ETH_P_IP) && brnet->filter_vlan_tagged;
 94 }
 95 
 96 static inline bool is_vlan_ipv6(const struct sk_buff *skb,
 97                                 const struct net *net)
 98 {
 99         struct brnf_net *brnet = net_generic(net, brnf_net_id);
100 
101         return vlan_proto(skb) == htons(ETH_P_IPV6) &&
102                brnet->filter_vlan_tagged;
103 }
104 
105 static inline bool is_vlan_arp(const struct sk_buff *skb, const struct net *net)
106 {
107         struct brnf_net *brnet = net_generic(net, brnf_net_id);
108 
109         return vlan_proto(skb) == htons(ETH_P_ARP) && brnet->filter_vlan_tagged;
110 }
111 
112 static inline __be16 pppoe_proto(const struct sk_buff *skb)
113 {
114         return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
115                             sizeof(struct pppoe_hdr)));
116 }
117 
118 static inline bool is_pppoe_ip(const struct sk_buff *skb, const struct net *net)
119 {
120         struct brnf_net *brnet = net_generic(net, brnf_net_id);
121 
122         return skb->protocol == htons(ETH_P_PPP_SES) &&
123                pppoe_proto(skb) == htons(PPP_IP) && brnet->filter_pppoe_tagged;
124 }
125 
126 static inline bool is_pppoe_ipv6(const struct sk_buff *skb,
127                                  const struct net *net)
128 {
129         struct brnf_net *brnet = net_generic(net, brnf_net_id);
130 
131         return skb->protocol == htons(ETH_P_PPP_SES) &&
132                pppoe_proto(skb) == htons(PPP_IPV6) &&
133                brnet->filter_pppoe_tagged;
134 }
135 
136 /* largest possible L2 header, see br_nf_dev_queue_xmit() */
137 #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
138 
139 struct brnf_frag_data {
140         local_lock_t bh_lock;
141         char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
142         u8 encap_size;
143         u8 size;
144         u16 vlan_tci;
145         __be16 vlan_proto;
146 };
147 
148 static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage) = {
149         .bh_lock = INIT_LOCAL_LOCK(bh_lock),
150 };
151 
152 static void nf_bridge_info_free(struct sk_buff *skb)
153 {
154         skb_ext_del(skb, SKB_EXT_BRIDGE_NF);
155 }
156 
157 static inline struct net_device *bridge_parent(const struct net_device *dev)
158 {
159         struct net_bridge_port *port;
160 
161         port = br_port_get_rcu(dev);
162         return port ? port->br->dev : NULL;
163 }
164 
165 static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
166 {
167         return skb_ext_add(skb, SKB_EXT_BRIDGE_NF);
168 }
169 
170 unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
171 {
172         switch (skb->protocol) {
173         case __cpu_to_be16(ETH_P_8021Q):
174                 return VLAN_HLEN;
175         case __cpu_to_be16(ETH_P_PPP_SES):
176                 return PPPOE_SES_HLEN;
177         default:
178                 return 0;
179         }
180 }
181 
182 static inline void nf_bridge_pull_encap_header(struct sk_buff *skb)
183 {
184         unsigned int len = nf_bridge_encap_header_len(skb);
185 
186         skb_pull(skb, len);
187         skb->network_header += len;
188 }
189 
190 static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
191 {
192         unsigned int len = nf_bridge_encap_header_len(skb);
193 
194         skb_pull_rcsum(skb, len);
195         skb->network_header += len;
196 }
197 
198 /* When handing a packet over to the IP layer
199  * check whether we have a skb that is in the
200  * expected format
201  */
202 
203 static int br_validate_ipv4(struct net *net, struct sk_buff *skb)
204 {
205         const struct iphdr *iph;
206         u32 len;
207 
208         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
209                 goto inhdr_error;
210 
211         iph = ip_hdr(skb);
212 
213         /* Basic sanity checks */
214         if (iph->ihl < 5 || iph->version != 4)
215                 goto inhdr_error;
216 
217         if (!pskb_may_pull(skb, iph->ihl*4))
218                 goto inhdr_error;
219 
220         iph = ip_hdr(skb);
221         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
222                 goto csum_error;
223 
224         len = skb_ip_totlen(skb);
225         if (skb->len < len) {
226                 __IP_INC_STATS(net, IPSTATS_MIB_INTRUNCATEDPKTS);
227                 goto drop;
228         } else if (len < (iph->ihl*4))
229                 goto inhdr_error;
230 
231         if (pskb_trim_rcsum(skb, len)) {
232                 __IP_INC_STATS(net, IPSTATS_MIB_INDISCARDS);
233                 goto drop;
234         }
235 
236         memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
237         /* We should really parse IP options here but until
238          * somebody who actually uses IP options complains to
239          * us we'll just silently ignore the options because
240          * we're lazy!
241          */
242         return 0;
243 
244 csum_error:
245         __IP_INC_STATS(net, IPSTATS_MIB_CSUMERRORS);
246 inhdr_error:
247         __IP_INC_STATS(net, IPSTATS_MIB_INHDRERRORS);
248 drop:
249         return -1;
250 }
251 
252 void nf_bridge_update_protocol(struct sk_buff *skb)
253 {
254         const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
255 
256         switch (nf_bridge->orig_proto) {
257         case BRNF_PROTO_8021Q:
258                 skb->protocol = htons(ETH_P_8021Q);
259                 break;
260         case BRNF_PROTO_PPPOE:
261                 skb->protocol = htons(ETH_P_PPP_SES);
262                 break;
263         case BRNF_PROTO_UNCHANGED:
264                 break;
265         }
266 }
267 
268 /* Obtain the correct destination MAC address, while preserving the original
269  * source MAC address. If we already know this address, we just copy it. If we
270  * don't, we use the neighbour framework to find out. In both cases, we make
271  * sure that br_handle_frame_finish() is called afterwards.
272  */
273 int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb)
274 {
275         struct neighbour *neigh;
276         struct dst_entry *dst;
277 
278         skb->dev = bridge_parent(skb->dev);
279         if (!skb->dev)
280                 goto free_skb;
281         dst = skb_dst(skb);
282         neigh = dst_neigh_lookup_skb(dst, skb);
283         if (neigh) {
284                 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
285                 int ret;
286 
287                 if ((READ_ONCE(neigh->nud_state) & NUD_CONNECTED) &&
288                     READ_ONCE(neigh->hh.hh_len)) {
289                         struct net_device *br_indev;
290 
291                         br_indev = nf_bridge_get_physindev(skb, net);
292                         if (!br_indev) {
293                                 neigh_release(neigh);
294                                 goto free_skb;
295                         }
296 
297                         neigh_hh_bridge(&neigh->hh, skb);
298                         skb->dev = br_indev;
299 
300                         ret = br_handle_frame_finish(net, sk, skb);
301                 } else {
302                         /* the neighbour function below overwrites the complete
303                          * MAC header, so we save the Ethernet source address and
304                          * protocol number.
305                          */
306                         skb_copy_from_linear_data_offset(skb,
307                                                          -(ETH_HLEN-ETH_ALEN),
308                                                          nf_bridge->neigh_header,
309                                                          ETH_HLEN-ETH_ALEN);
310                         /* tell br_dev_xmit to continue with forwarding */
311                         nf_bridge->bridged_dnat = 1;
312                         /* FIXME Need to refragment */
313                         ret = READ_ONCE(neigh->output)(neigh, skb);
314                 }
315                 neigh_release(neigh);
316                 return ret;
317         }
318 free_skb:
319         kfree_skb(skb);
320         return 0;
321 }
322 
323 static inline bool
324 br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb,
325                              const struct nf_bridge_info *nf_bridge)
326 {
327         return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr;
328 }
329 
330 /* This requires some explaining. If DNAT has taken place,
331  * we will need to fix up the destination Ethernet address.
332  * This is also true when SNAT takes place (for the reply direction).
333  *
334  * There are two cases to consider:
335  * 1. The packet was DNAT'ed to a device in the same bridge
336  *    port group as it was received on. We can still bridge
337  *    the packet.
338  * 2. The packet was DNAT'ed to a different device, either
339  *    a non-bridged device or another bridge port group.
340  *    The packet will need to be routed.
341  *
342  * The correct way of distinguishing between these two cases is to
343  * call ip_route_input() and to look at skb->dst->dev, which is
344  * changed to the destination device if ip_route_input() succeeds.
345  *
346  * Let's first consider the case that ip_route_input() succeeds:
347  *
348  * If the output device equals the logical bridge device the packet
349  * came in on, we can consider this bridging. The corresponding MAC
350  * address will be obtained in br_nf_pre_routing_finish_bridge.
351  * Otherwise, the packet is considered to be routed and we just
352  * change the destination MAC address so that the packet will
353  * later be passed up to the IP stack to be routed. For a redirected
354  * packet, ip_route_input() will give back the localhost as output device,
355  * which differs from the bridge device.
356  *
357  * Let's now consider the case that ip_route_input() fails:
358  *
359  * This can be because the destination address is martian, in which case
360  * the packet will be dropped.
361  * If IP forwarding is disabled, ip_route_input() will fail, while
362  * ip_route_output_key() can return success. The source
363  * address for ip_route_output_key() is set to zero, so ip_route_output_key()
364  * thinks we're handling a locally generated packet and won't care
365  * if IP forwarding is enabled. If the output device equals the logical bridge
366  * device, we proceed as if ip_route_input() succeeded. If it differs from the
367  * logical bridge port or if ip_route_output_key() fails we drop the packet.
368  */
369 static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
370 {
371         struct net_device *dev = skb->dev, *br_indev;
372         struct iphdr *iph = ip_hdr(skb);
373         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
374         struct rtable *rt;
375         int err;
376 
377         br_indev = nf_bridge_get_physindev(skb, net);
378         if (!br_indev) {
379                 kfree_skb(skb);
380                 return 0;
381         }
382 
383         nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
384 
385         if (nf_bridge->pkt_otherhost) {
386                 skb->pkt_type = PACKET_OTHERHOST;
387                 nf_bridge->pkt_otherhost = false;
388         }
389         nf_bridge->in_prerouting = 0;
390         if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) {
391                 if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) {
392                         struct in_device *in_dev = __in_dev_get_rcu(dev);
393 
394                         /* If err equals -EHOSTUNREACH the error is due to a
395                          * martian destination or due to the fact that
396                          * forwarding is disabled. For most martian packets,
397                          * ip_route_output_key() will fail. It won't fail for 2 types of
398                          * martian destinations: loopback destinations and destination
399                          * 0.0.0.0. In both cases the packet will be dropped because the
400                          * destination is the loopback device and not the bridge. */
401                         if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev))
402                                 goto free_skb;
403 
404                         rt = ip_route_output(net, iph->daddr, 0,
405                                              RT_TOS(iph->tos), 0,
406                                              RT_SCOPE_UNIVERSE);
407                         if (!IS_ERR(rt)) {
408                                 /* - Bridged-and-DNAT'ed traffic doesn't
409                                  *   require ip_forwarding. */
410                                 if (rt->dst.dev == dev) {
411                                         skb_dst_drop(skb);
412                                         skb_dst_set(skb, &rt->dst);
413                                         goto bridged_dnat;
414                                 }
415                                 ip_rt_put(rt);
416                         }
417 free_skb:
418                         kfree_skb(skb);
419                         return 0;
420                 } else {
421                         if (skb_dst(skb)->dev == dev) {
422 bridged_dnat:
423                                 skb->dev = br_indev;
424                                 nf_bridge_update_protocol(skb);
425                                 nf_bridge_push_encap_header(skb);
426                                 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
427                                                   net, sk, skb, skb->dev,
428                                                   NULL,
429                                                   br_nf_pre_routing_finish_bridge);
430                                 return 0;
431                         }
432                         ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
433                         skb->pkt_type = PACKET_HOST;
434                 }
435         } else {
436                 rt = bridge_parent_rtable(br_indev);
437                 if (!rt) {
438                         kfree_skb(skb);
439                         return 0;
440                 }
441                 skb_dst_drop(skb);
442                 skb_dst_set_noref(skb, &rt->dst);
443         }
444 
445         skb->dev = br_indev;
446         nf_bridge_update_protocol(skb);
447         nf_bridge_push_encap_header(skb);
448         br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb, skb->dev, NULL,
449                           br_handle_frame_finish);
450         return 0;
451 }
452 
453 static struct net_device *brnf_get_logical_dev(struct sk_buff *skb,
454                                                const struct net_device *dev,
455                                                const struct net *net)
456 {
457         struct net_device *vlan, *br;
458         struct brnf_net *brnet = net_generic(net, brnf_net_id);
459 
460         br = bridge_parent(dev);
461 
462         if (brnet->pass_vlan_indev == 0 || !skb_vlan_tag_present(skb))
463                 return br;
464 
465         vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto,
466                                     skb_vlan_tag_get(skb) & VLAN_VID_MASK);
467 
468         return vlan ? vlan : br;
469 }
470 
471 /* Some common code for IPv4/IPv6 */
472 struct net_device *setup_pre_routing(struct sk_buff *skb, const struct net *net)
473 {
474         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
475 
476         if (skb->pkt_type == PACKET_OTHERHOST) {
477                 skb->pkt_type = PACKET_HOST;
478                 nf_bridge->pkt_otherhost = true;
479         }
480 
481         nf_bridge->in_prerouting = 1;
482         nf_bridge->physinif = skb->dev->ifindex;
483         skb->dev = brnf_get_logical_dev(skb, skb->dev, net);
484 
485         if (skb->protocol == htons(ETH_P_8021Q))
486                 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
487         else if (skb->protocol == htons(ETH_P_PPP_SES))
488                 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
489 
490         /* Must drop socket now because of tproxy. */
491         skb_orphan(skb);
492         return skb->dev;
493 }
494 
495 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
496  * Replicate the checks that IPv4 does on packet reception.
497  * Set skb->dev to the bridge device (i.e. parent of the
498  * receiving device) to make netfilter happy, the REDIRECT
499  * target in particular.  Save the original destination IP
500  * address to be able to detect DNAT afterwards. */
501 static unsigned int br_nf_pre_routing(void *priv,
502                                       struct sk_buff *skb,
503                                       const struct nf_hook_state *state)
504 {
505         struct nf_bridge_info *nf_bridge;
506         struct net_bridge_port *p;
507         struct net_bridge *br;
508         __u32 len = nf_bridge_encap_header_len(skb);
509         struct brnf_net *brnet;
510 
511         if (unlikely(!pskb_may_pull(skb, len)))
512                 return NF_DROP_REASON(skb, SKB_DROP_REASON_PKT_TOO_SMALL, 0);
513 
514         p = br_port_get_rcu(state->in);
515         if (p == NULL)
516                 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
517         br = p->br;
518 
519         brnet = net_generic(state->net, brnf_net_id);
520         if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
521             is_pppoe_ipv6(skb, state->net)) {
522                 if (!brnet->call_ip6tables &&
523                     !br_opt_get(br, BROPT_NF_CALL_IP6TABLES))
524                         return NF_ACCEPT;
525                 if (!ipv6_mod_enabled()) {
526                         pr_warn_once("Module ipv6 is disabled, so call_ip6tables is not supported.");
527                         return NF_DROP_REASON(skb, SKB_DROP_REASON_IPV6DISABLED, 0);
528                 }
529 
530                 nf_bridge_pull_encap_header_rcsum(skb);
531                 return br_nf_pre_routing_ipv6(priv, skb, state);
532         }
533 
534         if (!brnet->call_iptables && !br_opt_get(br, BROPT_NF_CALL_IPTABLES))
535                 return NF_ACCEPT;
536 
537         if (!IS_IP(skb) && !is_vlan_ip(skb, state->net) &&
538             !is_pppoe_ip(skb, state->net))
539                 return NF_ACCEPT;
540 
541         nf_bridge_pull_encap_header_rcsum(skb);
542 
543         if (br_validate_ipv4(state->net, skb))
544                 return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
545 
546         if (!nf_bridge_alloc(skb))
547                 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
548         if (!setup_pre_routing(skb, state->net))
549                 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
550 
551         nf_bridge = nf_bridge_info_get(skb);
552         nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr;
553 
554         skb->protocol = htons(ETH_P_IP);
555         skb->transport_header = skb->network_header + ip_hdr(skb)->ihl * 4;
556 
557         NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
558                 skb->dev, NULL,
559                 br_nf_pre_routing_finish);
560 
561         return NF_STOLEN;
562 }
563 
564 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
565 /* conntracks' nf_confirm logic cannot handle cloned skbs referencing
566  * the same nf_conn entry, which will happen for multicast (broadcast)
567  * Frames on bridges.
568  *
569  * Example:
570  *      macvlan0
571  *      br0
572  *  ethX  ethY
573  *
574  * ethX (or Y) receives multicast or broadcast packet containing
575  * an IP packet, not yet in conntrack table.
576  *
577  * 1. skb passes through bridge and fake-ip (br_netfilter)Prerouting.
578  *    -> skb->_nfct now references a unconfirmed entry
579  * 2. skb is broad/mcast packet. bridge now passes clones out on each bridge
580  *    interface.
581  * 3. skb gets passed up the stack.
582  * 4. In macvlan case, macvlan driver retains clone(s) of the mcast skb
583  *    and schedules a work queue to send them out on the lower devices.
584  *
585  *    The clone skb->_nfct is not a copy, it is the same entry as the
586  *    original skb.  The macvlan rx handler then returns RX_HANDLER_PASS.
587  * 5. Normal conntrack hooks (in NF_INET_LOCAL_IN) confirm the orig skb.
588  *
589  * The Macvlan broadcast worker and normal confirm path will race.
590  *
591  * This race will not happen if step 2 already confirmed a clone. In that
592  * case later steps perform skb_clone() with skb->_nfct already confirmed (in
593  * hash table).  This works fine.
594  *
595  * But such confirmation won't happen when eb/ip/nftables rules dropped the
596  * packets before they reached the nf_confirm step in postrouting.
597  *
598  * Work around this problem by explicit confirmation of the entry at
599  * LOCAL_IN time, before upper layer has a chance to clone the unconfirmed
600  * entry.
601  *
602  */
603 static unsigned int br_nf_local_in(void *priv,
604                                    struct sk_buff *skb,
605                                    const struct nf_hook_state *state)
606 {
607         bool promisc = BR_INPUT_SKB_CB(skb)->promisc;
608         struct nf_conntrack *nfct = skb_nfct(skb);
609         const struct nf_ct_hook *ct_hook;
610         struct nf_conn *ct;
611         int ret;
612 
613         if (promisc) {
614                 nf_reset_ct(skb);
615                 return NF_ACCEPT;
616         }
617 
618         if (!nfct || skb->pkt_type == PACKET_HOST)
619                 return NF_ACCEPT;
620 
621         ct = container_of(nfct, struct nf_conn, ct_general);
622         if (likely(nf_ct_is_confirmed(ct)))
623                 return NF_ACCEPT;
624 
625         WARN_ON_ONCE(skb_shared(skb));
626         WARN_ON_ONCE(refcount_read(&nfct->use) != 1);
627 
628         /* We can't call nf_confirm here, it would create a dependency
629          * on nf_conntrack module.
630          */
631         ct_hook = rcu_dereference(nf_ct_hook);
632         if (!ct_hook) {
633                 skb->_nfct = 0ul;
634                 nf_conntrack_put(nfct);
635                 return NF_ACCEPT;
636         }
637 
638         nf_bridge_pull_encap_header(skb);
639         ret = ct_hook->confirm(skb);
640         switch (ret & NF_VERDICT_MASK) {
641         case NF_STOLEN:
642                 return NF_STOLEN;
643         default:
644                 nf_bridge_push_encap_header(skb);
645                 break;
646         }
647 
648         ct = container_of(nfct, struct nf_conn, ct_general);
649         WARN_ON_ONCE(!nf_ct_is_confirmed(ct));
650 
651         return ret;
652 }
653 #endif
654 
655 /* PF_BRIDGE/FORWARD *************************************************/
656 static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
657 {
658         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
659         struct net_device *in;
660 
661         if (!IS_ARP(skb) && !is_vlan_arp(skb, net)) {
662 
663                 if (skb->protocol == htons(ETH_P_IP))
664                         nf_bridge->frag_max_size = IPCB(skb)->frag_max_size;
665 
666                 if (skb->protocol == htons(ETH_P_IPV6))
667                         nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
668 
669                 in = nf_bridge_get_physindev(skb, net);
670                 if (!in) {
671                         kfree_skb(skb);
672                         return 0;
673                 }
674                 if (nf_bridge->pkt_otherhost) {
675                         skb->pkt_type = PACKET_OTHERHOST;
676                         nf_bridge->pkt_otherhost = false;
677                 }
678                 nf_bridge_update_protocol(skb);
679         } else {
680                 in = *((struct net_device **)(skb->cb));
681         }
682         nf_bridge_push_encap_header(skb);
683 
684         br_nf_hook_thresh(NF_BR_FORWARD, net, sk, skb, in, skb->dev,
685                           br_forward_finish);
686         return 0;
687 }
688 
689 
690 static unsigned int br_nf_forward_ip(struct sk_buff *skb,
691                                      const struct nf_hook_state *state,
692                                      u8 pf)
693 {
694         struct nf_bridge_info *nf_bridge;
695         struct net_device *parent;
696 
697         nf_bridge = nf_bridge_info_get(skb);
698         if (!nf_bridge)
699                 return NF_ACCEPT;
700 
701         /* Need exclusive nf_bridge_info since we might have multiple
702          * different physoutdevs. */
703         if (!nf_bridge_unshare(skb))
704                 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
705 
706         nf_bridge = nf_bridge_info_get(skb);
707         if (!nf_bridge)
708                 return NF_DROP_REASON(skb, SKB_DROP_REASON_NOMEM, 0);
709 
710         parent = bridge_parent(state->out);
711         if (!parent)
712                 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
713 
714         nf_bridge_pull_encap_header(skb);
715 
716         if (skb->pkt_type == PACKET_OTHERHOST) {
717                 skb->pkt_type = PACKET_HOST;
718                 nf_bridge->pkt_otherhost = true;
719         }
720 
721         if (pf == NFPROTO_IPV4) {
722                 if (br_validate_ipv4(state->net, skb))
723                         return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
724                 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
725                 skb->protocol = htons(ETH_P_IP);
726         } else if (pf == NFPROTO_IPV6) {
727                 if (br_validate_ipv6(state->net, skb))
728                         return NF_DROP_REASON(skb, SKB_DROP_REASON_IP_INHDR, 0);
729                 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
730                 skb->protocol = htons(ETH_P_IPV6);
731         } else {
732                 WARN_ON_ONCE(1);
733                 return NF_DROP;
734         }
735 
736         nf_bridge->physoutdev = skb->dev;
737 
738         NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb,
739                 brnf_get_logical_dev(skb, state->in, state->net),
740                 parent, br_nf_forward_finish);
741 
742         return NF_STOLEN;
743 }
744 
745 static unsigned int br_nf_forward_arp(struct sk_buff *skb,
746                                       const struct nf_hook_state *state)
747 {
748         struct net_bridge_port *p;
749         struct net_bridge *br;
750         struct net_device **d = (struct net_device **)(skb->cb);
751         struct brnf_net *brnet;
752 
753         p = br_port_get_rcu(state->out);
754         if (p == NULL)
755                 return NF_ACCEPT;
756         br = p->br;
757 
758         brnet = net_generic(state->net, brnf_net_id);
759         if (!brnet->call_arptables && !br_opt_get(br, BROPT_NF_CALL_ARPTABLES))
760                 return NF_ACCEPT;
761 
762         if (is_vlan_arp(skb, state->net))
763                 nf_bridge_pull_encap_header(skb);
764 
765         if (unlikely(!pskb_may_pull(skb, sizeof(struct arphdr))))
766                 return NF_DROP_REASON(skb, SKB_DROP_REASON_PKT_TOO_SMALL, 0);
767 
768         if (arp_hdr(skb)->ar_pln != 4) {
769                 if (is_vlan_arp(skb, state->net))
770                         nf_bridge_push_encap_header(skb);
771                 return NF_ACCEPT;
772         }
773         *d = state->in;
774         NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb,
775                 state->in, state->out, br_nf_forward_finish);
776 
777         return NF_STOLEN;
778 }
779 
780 /* This is the 'purely bridged' case.  For IP, we pass the packet to
781  * netfilter with indev and outdev set to the bridge device,
782  * but we are still able to filter on the 'real' indev/outdev
783  * because of the physdev module. For ARP, indev and outdev are the
784  * bridge ports.
785  */
786 static unsigned int br_nf_forward(void *priv,
787                                   struct sk_buff *skb,
788                                   const struct nf_hook_state *state)
789 {
790         if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
791             is_pppoe_ip(skb, state->net))
792                 return br_nf_forward_ip(skb, state, NFPROTO_IPV4);
793         if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
794             is_pppoe_ipv6(skb, state->net))
795                 return br_nf_forward_ip(skb, state, NFPROTO_IPV6);
796         if (IS_ARP(skb) || is_vlan_arp(skb, state->net))
797                 return br_nf_forward_arp(skb, state);
798 
799         return NF_ACCEPT;
800 }
801 
802 static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
803 {
804         struct brnf_frag_data *data;
805         int err;
806 
807         data = this_cpu_ptr(&brnf_frag_data_storage);
808         err = skb_cow_head(skb, data->size);
809 
810         if (err) {
811                 kfree_skb(skb);
812                 return 0;
813         }
814 
815         if (data->vlan_proto)
816                 __vlan_hwaccel_put_tag(skb, data->vlan_proto, data->vlan_tci);
817 
818         skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
819         __skb_push(skb, data->encap_size);
820 
821         nf_bridge_info_free(skb);
822         return br_dev_queue_push_xmit(net, sk, skb);
823 }
824 
825 static int
826 br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
827                   int (*output)(struct net *, struct sock *, struct sk_buff *))
828 {
829         unsigned int mtu = ip_skb_dst_mtu(sk, skb);
830         struct iphdr *iph = ip_hdr(skb);
831 
832         if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
833                      (IPCB(skb)->frag_max_size &&
834                       IPCB(skb)->frag_max_size > mtu))) {
835                 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
836                 kfree_skb(skb);
837                 return -EMSGSIZE;
838         }
839 
840         return ip_do_fragment(net, sk, skb, output);
841 }
842 
843 static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb)
844 {
845         const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
846 
847         if (nf_bridge->orig_proto == BRNF_PROTO_PPPOE)
848                 return PPPOE_SES_HLEN;
849         return 0;
850 }
851 
852 static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
853 {
854         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
855         unsigned int mtu, mtu_reserved;
856         int ret;
857 
858         mtu_reserved = nf_bridge_mtu_reduction(skb);
859         mtu = skb->dev->mtu;
860 
861         if (nf_bridge->pkt_otherhost) {
862                 skb->pkt_type = PACKET_OTHERHOST;
863                 nf_bridge->pkt_otherhost = false;
864         }
865 
866         if (nf_bridge->frag_max_size && nf_bridge->frag_max_size < mtu)
867                 mtu = nf_bridge->frag_max_size;
868 
869         nf_bridge_update_protocol(skb);
870         nf_bridge_push_encap_header(skb);
871 
872         if (skb_is_gso(skb) || skb->len + mtu_reserved <= mtu) {
873                 nf_bridge_info_free(skb);
874                 return br_dev_queue_push_xmit(net, sk, skb);
875         }
876 
877         /* This is wrong! We should preserve the original fragment
878          * boundaries by preserving frag_list rather than refragmenting.
879          */
880         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) &&
881             skb->protocol == htons(ETH_P_IP)) {
882                 struct brnf_frag_data *data;
883 
884                 if (br_validate_ipv4(net, skb))
885                         goto drop;
886 
887                 IPCB(skb)->frag_max_size = nf_bridge->frag_max_size;
888 
889                 local_lock_nested_bh(&brnf_frag_data_storage.bh_lock);
890                 data = this_cpu_ptr(&brnf_frag_data_storage);
891 
892                 if (skb_vlan_tag_present(skb)) {
893                         data->vlan_tci = skb->vlan_tci;
894                         data->vlan_proto = skb->vlan_proto;
895                 } else {
896                         data->vlan_proto = 0;
897                 }
898 
899                 data->encap_size = nf_bridge_encap_header_len(skb);
900                 data->size = ETH_HLEN + data->encap_size;
901 
902                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
903                                                  data->size);
904 
905                 ret = br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit);
906                 local_unlock_nested_bh(&brnf_frag_data_storage.bh_lock);
907                 return ret;
908         }
909         if (IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) &&
910             skb->protocol == htons(ETH_P_IPV6)) {
911                 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
912                 struct brnf_frag_data *data;
913 
914                 if (br_validate_ipv6(net, skb))
915                         goto drop;
916 
917                 IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size;
918 
919                 local_lock_nested_bh(&brnf_frag_data_storage.bh_lock);
920                 data = this_cpu_ptr(&brnf_frag_data_storage);
921                 data->encap_size = nf_bridge_encap_header_len(skb);
922                 data->size = ETH_HLEN + data->encap_size;
923 
924                 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
925                                                  data->size);
926 
927                 if (v6ops) {
928                         ret = v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit);
929                         local_unlock_nested_bh(&brnf_frag_data_storage.bh_lock);
930                         return ret;
931                 }
932                 local_unlock_nested_bh(&brnf_frag_data_storage.bh_lock);
933 
934                 kfree_skb(skb);
935                 return -EMSGSIZE;
936         }
937         nf_bridge_info_free(skb);
938         return br_dev_queue_push_xmit(net, sk, skb);
939  drop:
940         kfree_skb(skb);
941         return 0;
942 }
943 
944 /* PF_BRIDGE/POST_ROUTING ********************************************/
945 static unsigned int br_nf_post_routing(void *priv,
946                                        struct sk_buff *skb,
947                                        const struct nf_hook_state *state)
948 {
949         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
950         struct net_device *realoutdev = bridge_parent(skb->dev);
951         u_int8_t pf;
952 
953         /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
954          * on a bridge, but was delivered locally and is now being routed:
955          *
956          * POST_ROUTING was already invoked from the ip stack.
957          */
958         if (!nf_bridge || !nf_bridge->physoutdev)
959                 return NF_ACCEPT;
960 
961         if (!realoutdev)
962                 return NF_DROP_REASON(skb, SKB_DROP_REASON_DEV_READY, 0);
963 
964         if (IS_IP(skb) || is_vlan_ip(skb, state->net) ||
965             is_pppoe_ip(skb, state->net))
966                 pf = NFPROTO_IPV4;
967         else if (IS_IPV6(skb) || is_vlan_ipv6(skb, state->net) ||
968                  is_pppoe_ipv6(skb, state->net))
969                 pf = NFPROTO_IPV6;
970         else
971                 return NF_ACCEPT;
972 
973         if (skb->pkt_type == PACKET_OTHERHOST) {
974                 skb->pkt_type = PACKET_HOST;
975                 nf_bridge->pkt_otherhost = true;
976         }
977 
978         nf_bridge_pull_encap_header(skb);
979         if (pf == NFPROTO_IPV4)
980                 skb->protocol = htons(ETH_P_IP);
981         else
982                 skb->protocol = htons(ETH_P_IPV6);
983 
984         NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb,
985                 NULL, realoutdev,
986                 br_nf_dev_queue_xmit);
987 
988         return NF_STOLEN;
989 }
990 
991 /* IP/SABOTAGE *****************************************************/
992 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
993  * for the second time. */
994 static unsigned int ip_sabotage_in(void *priv,
995                                    struct sk_buff *skb,
996                                    const struct nf_hook_state *state)
997 {
998         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
999 
1000         if (nf_bridge) {
1001                 if (nf_bridge->sabotage_in_done)
1002                         return NF_ACCEPT;
1003 
1004                 if (!nf_bridge->in_prerouting &&
1005                     !netif_is_l3_master(skb->dev) &&
1006                     !netif_is_l3_slave(skb->dev)) {
1007                         nf_bridge->sabotage_in_done = 1;
1008                         state->okfn(state->net, state->sk, skb);
1009                         return NF_STOLEN;
1010                 }
1011         }
1012 
1013         return NF_ACCEPT;
1014 }
1015 
1016 /* This is called when br_netfilter has called into iptables/netfilter,
1017  * and DNAT has taken place on a bridge-forwarded packet.
1018  *
1019  * neigh->output has created a new MAC header, with local br0 MAC
1020  * as saddr.
1021  *
1022  * This restores the original MAC saddr of the bridged packet
1023  * before invoking bridge forward logic to transmit the packet.
1024  */
1025 static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
1026 {
1027         struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1028         struct net_device *br_indev;
1029 
1030         br_indev = nf_bridge_get_physindev(skb, dev_net(skb->dev));
1031         if (!br_indev) {
1032                 kfree_skb(skb);
1033                 return;
1034         }
1035 
1036         skb_pull(skb, ETH_HLEN);
1037         nf_bridge->bridged_dnat = 0;
1038 
1039         BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
1040 
1041         skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
1042                                        nf_bridge->neigh_header,
1043                                        ETH_HLEN - ETH_ALEN);
1044         skb->dev = br_indev;
1045 
1046         nf_bridge->physoutdev = NULL;
1047         br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
1048 }
1049 
1050 static int br_nf_dev_xmit(struct sk_buff *skb)
1051 {
1052         const struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
1053 
1054         if (nf_bridge && nf_bridge->bridged_dnat) {
1055                 br_nf_pre_routing_finish_bridge_slow(skb);
1056                 return 1;
1057         }
1058         return 0;
1059 }
1060 
1061 static const struct nf_br_ops br_ops = {
1062         .br_dev_xmit_hook =     br_nf_dev_xmit,
1063 };
1064 
1065 /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
1066  * br_dev_queue_push_xmit is called afterwards */
1067 static const struct nf_hook_ops br_nf_ops[] = {
1068         {
1069                 .hook = br_nf_pre_routing,
1070                 .pf = NFPROTO_BRIDGE,
1071                 .hooknum = NF_BR_PRE_ROUTING,
1072                 .priority = NF_BR_PRI_BRNF,
1073         },
1074 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
1075         {
1076                 .hook = br_nf_local_in,
1077                 .pf = NFPROTO_BRIDGE,
1078                 .hooknum = NF_BR_LOCAL_IN,
1079                 .priority = NF_BR_PRI_LAST,
1080         },
1081 #endif
1082         {
1083                 .hook = br_nf_forward,
1084                 .pf = NFPROTO_BRIDGE,
1085                 .hooknum = NF_BR_FORWARD,
1086                 .priority = NF_BR_PRI_BRNF,
1087         },
1088         {
1089                 .hook = br_nf_post_routing,
1090                 .pf = NFPROTO_BRIDGE,
1091                 .hooknum = NF_BR_POST_ROUTING,
1092                 .priority = NF_BR_PRI_LAST,
1093         },
1094         {
1095                 .hook = ip_sabotage_in,
1096                 .pf = NFPROTO_IPV4,
1097                 .hooknum = NF_INET_PRE_ROUTING,
1098                 .priority = NF_IP_PRI_FIRST,
1099         },
1100         {
1101                 .hook = ip_sabotage_in,
1102                 .pf = NFPROTO_IPV6,
1103                 .hooknum = NF_INET_PRE_ROUTING,
1104                 .priority = NF_IP6_PRI_FIRST,
1105         },
1106 };
1107 
1108 static int brnf_device_event(struct notifier_block *unused, unsigned long event,
1109                              void *ptr)
1110 {
1111         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1112         struct brnf_net *brnet;
1113         struct net *net;
1114         int ret;
1115 
1116         if (event != NETDEV_REGISTER || !netif_is_bridge_master(dev))
1117                 return NOTIFY_DONE;
1118 
1119         ASSERT_RTNL();
1120 
1121         net = dev_net(dev);
1122         brnet = net_generic(net, brnf_net_id);
1123         if (brnet->enabled)
1124                 return NOTIFY_OK;
1125 
1126         ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1127         if (ret)
1128                 return NOTIFY_BAD;
1129 
1130         brnet->enabled = true;
1131         return NOTIFY_OK;
1132 }
1133 
1134 static struct notifier_block brnf_notifier __read_mostly = {
1135         .notifier_call = brnf_device_event,
1136 };
1137 
1138 /* recursively invokes nf_hook_slow (again), skipping already-called
1139  * hooks (< NF_BR_PRI_BRNF).
1140  *
1141  * Called with rcu read lock held.
1142  */
1143 int br_nf_hook_thresh(unsigned int hook, struct net *net,
1144                       struct sock *sk, struct sk_buff *skb,
1145                       struct net_device *indev,
1146                       struct net_device *outdev,
1147                       int (*okfn)(struct net *, struct sock *,
1148                                   struct sk_buff *))
1149 {
1150         const struct nf_hook_entries *e;
1151         struct nf_hook_state state;
1152         struct nf_hook_ops **ops;
1153         unsigned int i;
1154         int ret;
1155 
1156         e = rcu_dereference(net->nf.hooks_bridge[hook]);
1157         if (!e)
1158                 return okfn(net, sk, skb);
1159 
1160         ops = nf_hook_entries_get_hook_ops(e);
1161         for (i = 0; i < e->num_hook_entries; i++) {
1162                 /* These hooks have already been called */
1163                 if (ops[i]->priority < NF_BR_PRI_BRNF)
1164                         continue;
1165 
1166                 /* These hooks have not been called yet, run them. */
1167                 if (ops[i]->priority > NF_BR_PRI_BRNF)
1168                         break;
1169 
1170                 /* take a closer look at NF_BR_PRI_BRNF. */
1171                 if (ops[i]->hook == br_nf_pre_routing) {
1172                         /* This hook diverted the skb to this function,
1173                          * hooks after this have not been run yet.
1174                          */
1175                         i++;
1176                         break;
1177                 }
1178         }
1179 
1180         nf_hook_state_init(&state, hook, NFPROTO_BRIDGE, indev, outdev,
1181                            sk, net, okfn);
1182 
1183         ret = nf_hook_slow(skb, &state, e, i);
1184         if (ret == 1)
1185                 ret = okfn(net, sk, skb);
1186 
1187         return ret;
1188 }
1189 
1190 #ifdef CONFIG_SYSCTL
1191 static
1192 int brnf_sysctl_call_tables(const struct ctl_table *ctl, int write,
1193                             void *buffer, size_t *lenp, loff_t *ppos)
1194 {
1195         int ret;
1196 
1197         ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
1198 
1199         if (write && *(int *)(ctl->data))
1200                 *(int *)(ctl->data) = 1;
1201         return ret;
1202 }
1203 
1204 static struct ctl_table brnf_table[] = {
1205         {
1206                 .procname       = "bridge-nf-call-arptables",
1207                 .maxlen         = sizeof(int),
1208                 .mode           = 0644,
1209                 .proc_handler   = brnf_sysctl_call_tables,
1210         },
1211         {
1212                 .procname       = "bridge-nf-call-iptables",
1213                 .maxlen         = sizeof(int),
1214                 .mode           = 0644,
1215                 .proc_handler   = brnf_sysctl_call_tables,
1216         },
1217         {
1218                 .procname       = "bridge-nf-call-ip6tables",
1219                 .maxlen         = sizeof(int),
1220                 .mode           = 0644,
1221                 .proc_handler   = brnf_sysctl_call_tables,
1222         },
1223         {
1224                 .procname       = "bridge-nf-filter-vlan-tagged",
1225                 .maxlen         = sizeof(int),
1226                 .mode           = 0644,
1227                 .proc_handler   = brnf_sysctl_call_tables,
1228         },
1229         {
1230                 .procname       = "bridge-nf-filter-pppoe-tagged",
1231                 .maxlen         = sizeof(int),
1232                 .mode           = 0644,
1233                 .proc_handler   = brnf_sysctl_call_tables,
1234         },
1235         {
1236                 .procname       = "bridge-nf-pass-vlan-input-dev",
1237                 .maxlen         = sizeof(int),
1238                 .mode           = 0644,
1239                 .proc_handler   = brnf_sysctl_call_tables,
1240         },
1241 };
1242 
1243 static inline void br_netfilter_sysctl_default(struct brnf_net *brnf)
1244 {
1245         brnf->call_iptables = 1;
1246         brnf->call_ip6tables = 1;
1247         brnf->call_arptables = 1;
1248         brnf->filter_vlan_tagged = 0;
1249         brnf->filter_pppoe_tagged = 0;
1250         brnf->pass_vlan_indev = 0;
1251 }
1252 
1253 static int br_netfilter_sysctl_init_net(struct net *net)
1254 {
1255         struct ctl_table *table = brnf_table;
1256         struct brnf_net *brnet;
1257 
1258         if (!net_eq(net, &init_net)) {
1259                 table = kmemdup(table, sizeof(brnf_table), GFP_KERNEL);
1260                 if (!table)
1261                         return -ENOMEM;
1262         }
1263 
1264         brnet = net_generic(net, brnf_net_id);
1265         table[0].data = &brnet->call_arptables;
1266         table[1].data = &brnet->call_iptables;
1267         table[2].data = &brnet->call_ip6tables;
1268         table[3].data = &brnet->filter_vlan_tagged;
1269         table[4].data = &brnet->filter_pppoe_tagged;
1270         table[5].data = &brnet->pass_vlan_indev;
1271 
1272         br_netfilter_sysctl_default(brnet);
1273 
1274         brnet->ctl_hdr = register_net_sysctl_sz(net, "net/bridge", table,
1275                                                 ARRAY_SIZE(brnf_table));
1276         if (!brnet->ctl_hdr) {
1277                 if (!net_eq(net, &init_net))
1278                         kfree(table);
1279 
1280                 return -ENOMEM;
1281         }
1282 
1283         return 0;
1284 }
1285 
1286 static void br_netfilter_sysctl_exit_net(struct net *net,
1287                                          struct brnf_net *brnet)
1288 {
1289         const struct ctl_table *table = brnet->ctl_hdr->ctl_table_arg;
1290 
1291         unregister_net_sysctl_table(brnet->ctl_hdr);
1292         if (!net_eq(net, &init_net))
1293                 kfree(table);
1294 }
1295 
1296 static int __net_init brnf_init_net(struct net *net)
1297 {
1298         return br_netfilter_sysctl_init_net(net);
1299 }
1300 #endif
1301 
1302 static void __net_exit brnf_exit_net(struct net *net)
1303 {
1304         struct brnf_net *brnet;
1305 
1306         brnet = net_generic(net, brnf_net_id);
1307         if (brnet->enabled) {
1308                 nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
1309                 brnet->enabled = false;
1310         }
1311 
1312 #ifdef CONFIG_SYSCTL
1313         br_netfilter_sysctl_exit_net(net, brnet);
1314 #endif
1315 }
1316 
1317 static struct pernet_operations brnf_net_ops __read_mostly = {
1318 #ifdef CONFIG_SYSCTL
1319         .init = brnf_init_net,
1320 #endif
1321         .exit = brnf_exit_net,
1322         .id   = &brnf_net_id,
1323         .size = sizeof(struct brnf_net),
1324 };
1325 
1326 static int __init br_netfilter_init(void)
1327 {
1328         int ret;
1329 
1330         ret = register_pernet_subsys(&brnf_net_ops);
1331         if (ret < 0)
1332                 return ret;
1333 
1334         ret = register_netdevice_notifier(&brnf_notifier);
1335         if (ret < 0) {
1336                 unregister_pernet_subsys(&brnf_net_ops);
1337                 return ret;
1338         }
1339 
1340         RCU_INIT_POINTER(nf_br_ops, &br_ops);
1341         printk(KERN_NOTICE "Bridge firewalling registered\n");
1342         return 0;
1343 }
1344 
1345 static void __exit br_netfilter_fini(void)
1346 {
1347         RCU_INIT_POINTER(nf_br_ops, NULL);
1348         unregister_netdevice_notifier(&brnf_notifier);
1349         unregister_pernet_subsys(&brnf_net_ops);
1350 }
1351 
1352 module_init(br_netfilter_init);
1353 module_exit(br_netfilter_fini);
1354 
1355 MODULE_LICENSE("GPL");
1356 MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>");
1357 MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
1358 MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge");
1359 

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

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

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

sflogo.php