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>
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.
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.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>
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>
29 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4;
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,
38 const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple;
39 struct flowi4 *fl4 = &fl->u.ip4;
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;
51 statusbit ^= IPS_NAT_MASK;
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;
63 #endif /* CONFIG_XFRM */
65 static bool nf_nat_ipv4_in_range(const struct nf_conntrack_tuple *t,
66 const struct nf_nat_range *range)
68 return ntohl(t->src.u3.ip) >= ntohl(range->min_addr.ip) &&
69 ntohl(t->src.u3.ip) <= ntohl(range->max_addr.ip);
72 static u32 nf_nat_ipv4_secure_port(const struct nf_conntrack_tuple *t,
75 return secure_ipv4_port_ephemeral(t->src.u3.ip, t->dst.u3.ip, dport);
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)
87 if (!skb_make_writable(skb, iphdroff + sizeof(*iph)))
90 iph = (void *)skb->data + iphdroff;
91 hdroff = iphdroff + iph->ihl * 4;
93 if (!l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv4, iphdroff, hdroff,
96 iph = (void *)skb->data + iphdroff;
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;
102 csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip);
103 iph->daddr = target->dst.u3.ip;
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)
113 struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
116 if (maniptype == NF_NAT_MANIP_SRC) {
118 newip = t->src.u3.ip;
121 newip = t->dst.u3.ip;
123 inet_proto_csum_replace4(check, skb, oldip, newip, true);
126 static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
127 u8 proto, void *data, __sum16 *check,
128 int datalen, int oldlen)
130 if (skb->ip_summed != CHECKSUM_PARTIAL) {
131 const struct iphdr *iph = ip_hdr(skb);
133 skb->ip_summed = CHECKSUM_PARTIAL;
134 skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
136 skb->csum_offset = (void *)check - data;
137 *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
140 inet_proto_csum_replace2(check, skb,
141 htons(oldlen), htons(datalen), true);
144 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
145 static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
146 struct nf_nat_range *range)
148 if (tb[CTA_NAT_V4_MINIP]) {
149 range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
150 range->flags |= NF_NAT_RANGE_MAP_IPS;
153 if (tb[CTA_NAT_V4_MAXIP])
154 range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
156 range->max_addr.ip = range->min_addr.ip;
162 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4 = {
163 .l3proto = NFPROTO_IPV4,
164 .in_range = nf_nat_ipv4_in_range,
165 .secure_port = nf_nat_ipv4_secure_port,
166 .manip_pkt = nf_nat_ipv4_manip_pkt,
167 .csum_update = nf_nat_ipv4_csum_update,
168 .csum_recalc = nf_nat_ipv4_csum_recalc,
169 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
170 .nlattr_to_range = nf_nat_ipv4_nlattr_to_range,
173 .decode_session = nf_nat_ipv4_decode_session,
177 int nf_nat_icmp_reply_translation(struct sk_buff *skb,
179 enum ip_conntrack_info ctinfo,
180 unsigned int hooknum)
186 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
187 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
188 unsigned int hdrlen = ip_hdrlen(skb);
189 const struct nf_nat_l4proto *l4proto;
190 struct nf_conntrack_tuple target;
191 unsigned long statusbit;
193 NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
195 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
197 if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
200 inside = (void *)skb->data + hdrlen;
201 if (inside->icmp.type == ICMP_REDIRECT) {
202 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
204 if (ct->status & IPS_NAT_MASK)
208 if (manip == NF_NAT_MANIP_SRC)
209 statusbit = IPS_SRC_NAT;
211 statusbit = IPS_DST_NAT;
213 /* Invert if this is reply direction */
214 if (dir == IP_CT_DIR_REPLY)
215 statusbit ^= IPS_NAT_MASK;
217 if (!(ct->status & statusbit))
220 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, inside->ip.protocol);
221 if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp),
222 l4proto, &ct->tuplehash[!dir].tuple, !manip))
225 if (skb->ip_summed != CHECKSUM_PARTIAL) {
226 /* Reloading "inside" here since manip_pkt may reallocate */
227 inside = (void *)skb->data + hdrlen;
228 inside->icmp.checksum = 0;
229 inside->icmp.checksum =
230 csum_fold(skb_checksum(skb, hdrlen,
231 skb->len - hdrlen, 0));
234 /* Change outer to look like the reply to an incoming packet */
235 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
236 l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, 0);
237 if (!nf_nat_ipv4_manip_pkt(skb, 0, l4proto, &target, manip))
242 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
245 nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
246 const struct nf_hook_state *state,
247 unsigned int (*do_chain)(void *priv,
249 const struct nf_hook_state *state,
253 enum ip_conntrack_info ctinfo;
254 struct nf_conn_nat *nat;
255 /* maniptype == SRC for postrouting. */
256 enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
258 /* We never see fragments: conntrack defrags on pre-routing
259 * and local-out, and nf_nat_out protects post-routing.
261 NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
263 ct = nf_ct_get(skb, &ctinfo);
264 /* Can't track? It's not due to stress, or conntrack would
265 * have dropped it. Hence it's the user's responsibilty to
266 * packet filter it out, or implement conntrack/NAT for that
272 /* Don't try to NAT if this packet is not conntracked */
273 if (nf_ct_is_untracked(ct))
276 nat = nf_ct_nat_ext_add(ct);
282 case IP_CT_RELATED_REPLY:
283 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
284 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
290 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
292 /* Seen it before? This can happen for loopback, retrans,
295 if (!nf_nat_initialized(ct, maniptype)) {
298 ret = do_chain(priv, skb, state, ct);
299 if (ret != NF_ACCEPT)
302 if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
305 ret = nf_nat_alloc_null_binding(ct, state->hook);
306 if (ret != NF_ACCEPT)
309 pr_debug("Already setup manip %s for ct %p\n",
310 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
312 if (nf_nat_oif_changed(state->hook, ctinfo, nat,
320 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
321 ctinfo == IP_CT_ESTABLISHED_REPLY);
322 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
326 return nf_nat_packet(ct, ctinfo, state->hook, skb);
329 nf_ct_kill_acct(ct, ctinfo, skb);
332 EXPORT_SYMBOL_GPL(nf_nat_ipv4_fn);
335 nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
336 const struct nf_hook_state *state,
337 unsigned int (*do_chain)(void *priv,
339 const struct nf_hook_state *state,
343 __be32 daddr = ip_hdr(skb)->daddr;
345 ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
346 if (ret != NF_DROP && ret != NF_STOLEN &&
347 daddr != ip_hdr(skb)->daddr)
352 EXPORT_SYMBOL_GPL(nf_nat_ipv4_in);
355 nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
356 const struct nf_hook_state *state,
357 unsigned int (*do_chain)(void *priv,
359 const struct nf_hook_state *state,
363 const struct nf_conn *ct;
364 enum ip_conntrack_info ctinfo;
369 /* root is playing with raw sockets. */
370 if (skb->len < sizeof(struct iphdr) ||
371 ip_hdrlen(skb) < sizeof(struct iphdr))
374 ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
376 if (ret != NF_DROP && ret != NF_STOLEN &&
377 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
378 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
379 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
381 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
382 ct->tuplehash[!dir].tuple.dst.u3.ip) ||
383 (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
384 ct->tuplehash[dir].tuple.src.u.all !=
385 ct->tuplehash[!dir].tuple.dst.u.all)) {
386 err = nf_xfrm_me_harder(state->net, skb, AF_INET);
388 ret = NF_DROP_ERR(err);
394 EXPORT_SYMBOL_GPL(nf_nat_ipv4_out);
397 nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
398 const struct nf_hook_state *state,
399 unsigned int (*do_chain)(void *priv,
401 const struct nf_hook_state *state,
404 const struct nf_conn *ct;
405 enum ip_conntrack_info ctinfo;
409 /* root is playing with raw sockets. */
410 if (skb->len < sizeof(struct iphdr) ||
411 ip_hdrlen(skb) < sizeof(struct iphdr))
414 ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
415 if (ret != NF_DROP && ret != NF_STOLEN &&
416 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
417 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
419 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
420 ct->tuplehash[!dir].tuple.src.u3.ip) {
421 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
423 ret = NF_DROP_ERR(err);
426 else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
427 ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
428 ct->tuplehash[dir].tuple.dst.u.all !=
429 ct->tuplehash[!dir].tuple.src.u.all) {
430 err = nf_xfrm_me_harder(state->net, skb, AF_INET);
432 ret = NF_DROP_ERR(err);
438 EXPORT_SYMBOL_GPL(nf_nat_ipv4_local_fn);
440 static int __init nf_nat_l3proto_ipv4_init(void)
444 err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
447 err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv4);
453 nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
458 static void __exit nf_nat_l3proto_ipv4_exit(void)
460 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv4);
461 nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
464 MODULE_LICENSE("GPL");
465 MODULE_ALIAS("nf-nat-" __stringify(AF_INET));
467 module_init(nf_nat_l3proto_ipv4_init);
468 module_exit(nf_nat_l3proto_ipv4_exit);