Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[linux-2.6-microblaze.git] / net / ipv4 / netfilter / nf_dup_ipv4.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/ip.h>
12 #include <linux/module.h>
13 #include <linux/percpu.h>
14 #include <linux/route.h>
15 #include <linux/skbuff.h>
16 #include <linux/netfilter.h>
17 #include <net/checksum.h>
18 #include <net/icmp.h>
19 #include <net/ip.h>
20 #include <net/route.h>
21 #include <net/netfilter/ipv4/nf_dup_ipv4.h>
22 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
23 #include <net/netfilter/nf_conntrack.h>
24 #endif
25
26 static struct net *pick_net(struct sk_buff *skb)
27 {
28 #ifdef CONFIG_NET_NS
29         const struct dst_entry *dst;
30
31         if (skb->dev != NULL)
32                 return dev_net(skb->dev);
33         dst = skb_dst(skb);
34         if (dst != NULL && dst->dev != NULL)
35                 return dev_net(dst->dev);
36 #endif
37         return &init_net;
38 }
39
40 static bool nf_dup_ipv4_route(struct sk_buff *skb, const struct in_addr *gw,
41                               int oif)
42 {
43         const struct iphdr *iph = ip_hdr(skb);
44         struct net *net = pick_net(skb);
45         struct rtable *rt;
46         struct flowi4 fl4;
47
48         memset(&fl4, 0, sizeof(fl4));
49         if (oif != -1)
50                 fl4.flowi4_oif = oif;
51
52         fl4.daddr = gw->s_addr;
53         fl4.flowi4_tos = RT_TOS(iph->tos);
54         fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
55         fl4.flowi4_flags = FLOWI_FLAG_KNOWN_NH;
56         rt = ip_route_output_key(net, &fl4);
57         if (IS_ERR(rt))
58                 return false;
59
60         skb_dst_drop(skb);
61         skb_dst_set(skb, &rt->dst);
62         skb->dev      = rt->dst.dev;
63         skb->protocol = htons(ETH_P_IP);
64
65         return true;
66 }
67
68 void nf_dup_ipv4(struct sk_buff *skb, unsigned int hooknum,
69                  const struct in_addr *gw, int oif)
70 {
71         struct iphdr *iph;
72
73         if (this_cpu_read(nf_skb_duplicated))
74                 return;
75         /*
76          * Copy the skb, and route the copy. Will later return %XT_CONTINUE for
77          * the original skb, which should continue on its way as if nothing has
78          * happened. The copy should be independently delivered to the gateway.
79          */
80         skb = pskb_copy(skb, GFP_ATOMIC);
81         if (skb == NULL)
82                 return;
83
84 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
85         /* Avoid counting cloned packets towards the original connection. */
86         nf_conntrack_put(skb->nfct);
87         skb->nfct     = &nf_ct_untracked_get()->ct_general;
88         skb->nfctinfo = IP_CT_NEW;
89         nf_conntrack_get(skb->nfct);
90 #endif
91         /*
92          * If we are in PREROUTING/INPUT, the checksum must be recalculated
93          * since the length could have changed as a result of defragmentation.
94          *
95          * We also decrease the TTL to mitigate potential loops between two
96          * hosts.
97          *
98          * Set %IP_DF so that the original source is notified of a potentially
99          * decreased MTU on the clone route. IPv6 does this too.
100          */
101         iph = ip_hdr(skb);
102         iph->frag_off |= htons(IP_DF);
103         if (hooknum == NF_INET_PRE_ROUTING ||
104             hooknum == NF_INET_LOCAL_IN)
105                 --iph->ttl;
106         ip_send_check(iph);
107
108         if (nf_dup_ipv4_route(skb, gw, oif)) {
109                 __this_cpu_write(nf_skb_duplicated, true);
110                 ip_local_out(skb);
111                 __this_cpu_write(nf_skb_duplicated, false);
112         } else {
113                 kfree_skb(skb);
114         }
115 }
116 EXPORT_SYMBOL_GPL(nf_dup_ipv4);
117
118 MODULE_AUTHOR("Sebastian Claßen <sebastian.classen@freenet.ag>");
119 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
120 MODULE_DESCRIPTION("nf_dup_ipv4: Duplicate IPv4 packet");
121 MODULE_LICENSE("GPL");