]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/ipv6/netfilter/nf_dup_ipv6.c
Merge branch 'next/cleanup' into for-next
[karo-tx-linux.git] / net / ipv6 / netfilter / nf_dup_ipv6.c
1 /*
2  * (C) 2007 by Sebastian Claßen <sebastian.classen@freenet.ag>
3  * (C) 2007-2010 by Jan Engelhardt <jengelh@medozas.de>
4  *
5  * Extracted from xt_TEE.c
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 or later, as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/module.h>
12 #include <linux/percpu.h>
13 #include <linux/skbuff.h>
14 #include <linux/netfilter.h>
15 #include <net/ipv6.h>
16 #include <net/ip6_route.h>
17 #include <net/netfilter/ipv6/nf_dup_ipv6.h>
18 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
19 #include <net/netfilter/nf_conntrack.h>
20 #endif
21
22 static struct net *pick_net(struct sk_buff *skb)
23 {
24 #ifdef CONFIG_NET_NS
25         const struct dst_entry *dst;
26
27         if (skb->dev != NULL)
28                 return dev_net(skb->dev);
29         dst = skb_dst(skb);
30         if (dst != NULL && dst->dev != NULL)
31                 return dev_net(dst->dev);
32 #endif
33         return &init_net;
34 }
35
36 static bool nf_dup_ipv6_route(struct sk_buff *skb, const struct in6_addr *gw,
37                               int oif)
38 {
39         const struct ipv6hdr *iph = ipv6_hdr(skb);
40         struct net *net = pick_net(skb);
41         struct dst_entry *dst;
42         struct flowi6 fl6;
43
44         memset(&fl6, 0, sizeof(fl6));
45         if (oif != -1)
46                 fl6.flowi6_oif = oif;
47
48         fl6.daddr = *gw;
49         fl6.flowlabel = (__force __be32)(((iph->flow_lbl[0] & 0xF) << 16) |
50                         (iph->flow_lbl[1] << 8) | iph->flow_lbl[2]);
51         dst = ip6_route_output(net, NULL, &fl6);
52         if (dst->error) {
53                 dst_release(dst);
54                 return false;
55         }
56         skb_dst_drop(skb);
57         skb_dst_set(skb, dst);
58         skb->dev      = dst->dev;
59         skb->protocol = htons(ETH_P_IPV6);
60
61         return true;
62 }
63
64 void nf_dup_ipv6(struct sk_buff *skb, unsigned int hooknum,
65                  const struct in6_addr *gw, int oif)
66 {
67         if (this_cpu_read(nf_skb_duplicated))
68                 return;
69         skb = pskb_copy(skb, GFP_ATOMIC);
70         if (skb == NULL)
71                 return;
72
73 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
74         nf_conntrack_put(skb->nfct);
75         skb->nfct     = &nf_ct_untracked_get()->ct_general;
76         skb->nfctinfo = IP_CT_NEW;
77         nf_conntrack_get(skb->nfct);
78 #endif
79         if (hooknum == NF_INET_PRE_ROUTING ||
80             hooknum == NF_INET_LOCAL_IN) {
81                 struct ipv6hdr *iph = ipv6_hdr(skb);
82                 --iph->hop_limit;
83         }
84         if (nf_dup_ipv6_route(skb, gw, oif)) {
85                 __this_cpu_write(nf_skb_duplicated, true);
86                 ip6_local_out(skb);
87                 __this_cpu_write(nf_skb_duplicated, false);
88         } else {
89                 kfree_skb(skb);
90         }
91 }
92 EXPORT_SYMBOL_GPL(nf_dup_ipv6);
93
94 MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
95 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
96 MODULE_DESCRIPTION("nf_dup_ipv6: IPv6 packet duplication");
97 MODULE_LICENSE("GPL");