]> Pileus Git - ~andy/linux/blob - net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
Linux 3.14
[~andy/linux] / net / ipv4 / netfilter / nf_nat_l3proto_ipv4.c
1 /*
2  * (C) 1999-2001 Paul `Rusty' Russell
3  * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4  * (C) 2011 Patrick McHardy <kaber@trash.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/ip.h>
15 #include <linux/icmp.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/secure_seq.h>
19 #include <net/checksum.h>
20 #include <net/route.h>
21 #include <net/ip.h>
22
23 #include <net/netfilter/nf_conntrack_core.h>
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_nat_core.h>
26 #include <net/netfilter/nf_nat_l3proto.h>
27 #include <net/netfilter/nf_nat_l4proto.h>
28
29 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4;
30
31 #ifdef CONFIG_XFRM
32 static void nf_nat_ipv4_decode_session(struct sk_buff *skb,
33                                        const struct nf_conn *ct,
34                                        enum ip_conntrack_dir dir,
35                                        unsigned long statusbit,
36                                        struct flowi *fl)
37 {
38         const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
39         struct flowi4 *fl4 = &fl->u.ip4;
40
41         if (ct->status & statusbit) {
42                 fl4->daddr = t->dst.u3.ip;
43                 if (t->dst.protonum == IPPROTO_TCP ||
44                     t->dst.protonum == IPPROTO_UDP ||
45                     t->dst.protonum == IPPROTO_UDPLITE ||
46                     t->dst.protonum == IPPROTO_DCCP ||
47                     t->dst.protonum == IPPROTO_SCTP)
48                         fl4->fl4_dport = t->dst.u.all;
49         }
50
51         statusbit ^= IPS_NAT_MASK;
52
53         if (ct->status & statusbit) {
54                 fl4->saddr = t->src.u3.ip;
55                 if (t->dst.protonum == IPPROTO_TCP ||
56                     t->dst.protonum == IPPROTO_UDP ||
57                     t->dst.protonum == IPPROTO_UDPLITE ||
58                     t->dst.protonum == IPPROTO_DCCP ||
59                     t->dst.protonum == IPPROTO_SCTP)
60                         fl4->fl4_sport = t->src.u.all;
61         }
62 }
63 #endif /* CONFIG_XFRM */
64
65 static bool nf_nat_ipv4_in_range(const struct nf_conntrack_tuple *t,
66                                  const struct nf_nat_range *range)
67 {
68         return ntohl(t->src.u3.ip) >= ntohl(range->min_addr.ip) &&
69                ntohl(t->src.u3.ip) <= ntohl(range->max_addr.ip);
70 }
71
72 static u32 nf_nat_ipv4_secure_port(const struct nf_conntrack_tuple *t,
73                                    __be16 dport)
74 {
75         return secure_ipv4_port_ephemeral(t->src.u3.ip, t->dst.u3.ip, dport);
76 }
77
78 static bool nf_nat_ipv4_manip_pkt(struct sk_buff *skb,
79                                   unsigned int iphdroff,
80                                   const struct nf_nat_l4proto *l4proto,
81                                   const struct nf_conntrack_tuple *target,
82                                   enum nf_nat_manip_type maniptype)
83 {
84         struct iphdr *iph;
85         unsigned int hdroff;
86
87         if (!skb_make_writable(skb, iphdroff + sizeof(*iph)))
88                 return false;
89
90         iph = (void *)skb->data + iphdroff;
91         hdroff = iphdroff + iph->ihl * 4;
92
93         if (!l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv4, iphdroff, hdroff,
94                                 target, maniptype))
95                 return false;
96         iph = (void *)skb->data + iphdroff;
97
98         if (maniptype == NF_NAT_MANIP_SRC) {
99                 csum_replace4(&iph->check, iph->saddr, target->src.u3.ip);
100                 iph->saddr = target->src.u3.ip;
101         } else {
102                 csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip);
103                 iph->daddr = target->dst.u3.ip;
104         }
105         return true;
106 }
107
108 static void nf_nat_ipv4_csum_update(struct sk_buff *skb,
109                                     unsigned int iphdroff, __sum16 *check,
110                                     const struct nf_conntrack_tuple *t,
111                                     enum nf_nat_manip_type maniptype)
112 {
113         struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
114         __be32 oldip, newip;
115
116         if (maniptype == NF_NAT_MANIP_SRC) {
117                 oldip = iph->saddr;
118                 newip = t->src.u3.ip;
119         } else {
120                 oldip = iph->daddr;
121                 newip = t->dst.u3.ip;
122         }
123         inet_proto_csum_replace4(check, skb, oldip, newip, 1);
124 }
125
126 static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
127                                     u8 proto, void *data, __sum16 *check,
128                                     int datalen, int oldlen)
129 {
130         const struct iphdr *iph = ip_hdr(skb);
131         struct rtable *rt = skb_rtable(skb);
132
133         if (skb->ip_summed != CHECKSUM_PARTIAL) {
134                 if (!(rt->rt_flags & RTCF_LOCAL) &&
135                     (!skb->dev || skb->dev->features & NETIF_F_V4_CSUM)) {
136                         skb->ip_summed = CHECKSUM_PARTIAL;
137                         skb->csum_start = skb_headroom(skb) +
138                                           skb_network_offset(skb) +
139                                           ip_hdrlen(skb);
140                         skb->csum_offset = (void *)check - data;
141                         *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
142                                                     datalen, proto, 0);
143                 } else {
144                         *check = 0;
145                         *check = csum_tcpudp_magic(iph->saddr, iph->daddr,
146                                                    datalen, proto,
147                                                    csum_partial(data, datalen,
148                                                                 0));
149                         if (proto == IPPROTO_UDP && !*check)
150                                 *check = CSUM_MANGLED_0;
151                 }
152         } else
153                 inet_proto_csum_replace2(check, skb,
154                                          htons(oldlen), htons(datalen), 1);
155 }
156
157 static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
158                                        struct nf_nat_range *range)
159 {
160         if (tb[CTA_NAT_V4_MINIP]) {
161                 range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
162                 range->flags |= NF_NAT_RANGE_MAP_IPS;
163         }
164
165         if (tb[CTA_NAT_V4_MAXIP])
166                 range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
167         else
168                 range->max_addr.ip = range->min_addr.ip;
169
170         return 0;
171 }
172
173 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4 = {
174         .l3proto                = NFPROTO_IPV4,
175         .in_range               = nf_nat_ipv4_in_range,
176         .secure_port            = nf_nat_ipv4_secure_port,
177         .manip_pkt              = nf_nat_ipv4_manip_pkt,
178         .csum_update            = nf_nat_ipv4_csum_update,
179         .csum_recalc            = nf_nat_ipv4_csum_recalc,
180         .nlattr_to_range        = nf_nat_ipv4_nlattr_to_range,
181 #ifdef CONFIG_XFRM
182         .decode_session         = nf_nat_ipv4_decode_session,
183 #endif
184 };
185
186 int nf_nat_icmp_reply_translation(struct sk_buff *skb,
187                                   struct nf_conn *ct,
188                                   enum ip_conntrack_info ctinfo,
189                                   unsigned int hooknum)
190 {
191         struct {
192                 struct icmphdr  icmp;
193                 struct iphdr    ip;
194         } *inside;
195         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
196         enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
197         unsigned int hdrlen = ip_hdrlen(skb);
198         const struct nf_nat_l4proto *l4proto;
199         struct nf_conntrack_tuple target;
200         unsigned long statusbit;
201
202         NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
203
204         if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
205                 return 0;
206         if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
207                 return 0;
208
209         inside = (void *)skb->data + hdrlen;
210         if (inside->icmp.type == ICMP_REDIRECT) {
211                 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
212                         return 0;
213                 if (ct->status & IPS_NAT_MASK)
214                         return 0;
215         }
216
217         if (manip == NF_NAT_MANIP_SRC)
218                 statusbit = IPS_SRC_NAT;
219         else
220                 statusbit = IPS_DST_NAT;
221
222         /* Invert if this is reply direction */
223         if (dir == IP_CT_DIR_REPLY)
224                 statusbit ^= IPS_NAT_MASK;
225
226         if (!(ct->status & statusbit))
227                 return 1;
228
229         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, inside->ip.protocol);
230         if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp),
231                                    l4proto, &ct->tuplehash[!dir].tuple, !manip))
232                 return 0;
233
234         if (skb->ip_summed != CHECKSUM_PARTIAL) {
235                 /* Reloading "inside" here since manip_pkt may reallocate */
236                 inside = (void *)skb->data + hdrlen;
237                 inside->icmp.checksum = 0;
238                 inside->icmp.checksum =
239                         csum_fold(skb_checksum(skb, hdrlen,
240                                                skb->len - hdrlen, 0));
241         }
242
243         /* Change outer to look like the reply to an incoming packet */
244         nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
245         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, 0);
246         if (!nf_nat_ipv4_manip_pkt(skb, 0, l4proto, &target, manip))
247                 return 0;
248
249         return 1;
250 }
251 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
252
253 static int __init nf_nat_l3proto_ipv4_init(void)
254 {
255         int err;
256
257         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
258         if (err < 0)
259                 goto err1;
260         err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv4);
261         if (err < 0)
262                 goto err2;
263         return err;
264
265 err2:
266         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
267 err1:
268         return err;
269 }
270
271 static void __exit nf_nat_l3proto_ipv4_exit(void)
272 {
273         nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv4);
274         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
275 }
276
277 MODULE_LICENSE("GPL");
278 MODULE_ALIAS("nf-nat-" __stringify(AF_INET));
279
280 module_init(nf_nat_l3proto_ipv4_init);
281 module_exit(nf_nat_l3proto_ipv4_exit);