blob: 4a84b5ad9ecbb74b29ef509d00fbda60868833cd [file] [log] [blame]
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +02001/*
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>
Daniel Borkmanna82b0e62015-09-02 20:54:02 +020014#include <linux/netfilter.h>
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020015#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
Eric W. Biederman206e8c02015-09-18 14:33:02 -050022static bool nf_dup_ipv6_route(struct net *net, struct sk_buff *skb,
23 const struct in6_addr *gw, int oif)
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020024{
25 const struct ipv6hdr *iph = ipv6_hdr(skb);
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020026 struct dst_entry *dst;
27 struct flowi6 fl6;
28
29 memset(&fl6, 0, sizeof(fl6));
30 if (oif != -1)
31 fl6.flowi6_oif = oif;
32
33 fl6.daddr = *gw;
Pablo Neira Ayuso59e26422015-08-21 21:28:10 +020034 fl6.flowlabel = (__force __be32)(((iph->flow_lbl[0] & 0xF) << 16) |
35 (iph->flow_lbl[1] << 8) | iph->flow_lbl[2]);
Paolo Abeni83170f32016-05-26 19:08:10 +020036 fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020037 dst = ip6_route_output(net, NULL, &fl6);
38 if (dst->error) {
39 dst_release(dst);
40 return false;
41 }
42 skb_dst_drop(skb);
43 skb_dst_set(skb, dst);
44 skb->dev = dst->dev;
45 skb->protocol = htons(ETH_P_IPV6);
46
47 return true;
48}
49
Eric W. Biederman206e8c02015-09-18 14:33:02 -050050void nf_dup_ipv6(struct net *net, struct sk_buff *skb, unsigned int hooknum,
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020051 const struct in6_addr *gw, int oif)
52{
Pablo Neira Ayusod877f072015-05-31 18:04:11 +020053 if (this_cpu_read(nf_skb_duplicated))
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020054 return;
55 skb = pskb_copy(skb, GFP_ATOMIC);
56 if (skb == NULL)
57 return;
58
59#if IS_ENABLED(CONFIG_NF_CONNTRACK)
60 nf_conntrack_put(skb->nfct);
61 skb->nfct = &nf_ct_untracked_get()->ct_general;
62 skb->nfctinfo = IP_CT_NEW;
63 nf_conntrack_get(skb->nfct);
64#endif
65 if (hooknum == NF_INET_PRE_ROUTING ||
66 hooknum == NF_INET_LOCAL_IN) {
67 struct ipv6hdr *iph = ipv6_hdr(skb);
68 --iph->hop_limit;
69 }
Eric W. Biederman206e8c02015-09-18 14:33:02 -050070 if (nf_dup_ipv6_route(net, skb, gw, oif)) {
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020071 __this_cpu_write(nf_skb_duplicated, true);
Eric W. Biederman33224b12015-10-07 16:48:46 -050072 ip6_local_out(net, skb->sk, skb);
Pablo Neira Ayusobbde9fc2015-05-31 17:54:44 +020073 __this_cpu_write(nf_skb_duplicated, false);
74 } else {
75 kfree_skb(skb);
76 }
77}
78EXPORT_SYMBOL_GPL(nf_dup_ipv6);
79
80MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
81MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
82MODULE_DESCRIPTION("nf_dup_ipv6: IPv6 packet duplication");
83MODULE_LICENSE("GPL");