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_manip_pkt(struct sk_buff *skb,
66 unsigned int iphdroff,
67 const struct nf_conntrack_tuple *target,
68 enum nf_nat_manip_type maniptype)
73 if (!skb_make_writable(skb, iphdroff + sizeof(*iph)))
76 iph = (void *)skb->data + iphdroff;
77 hdroff = iphdroff + iph->ihl * 4;
79 if (!nf_nat_l4proto_manip_pkt(skb, &nf_nat_l3proto_ipv4, iphdroff,
80 hdroff, target, maniptype))
82 iph = (void *)skb->data + iphdroff;
84 if (maniptype == NF_NAT_MANIP_SRC) {
85 csum_replace4(&iph->check, iph->saddr, target->src.u3.ip);
86 iph->saddr = target->src.u3.ip;
88 csum_replace4(&iph->check, iph->daddr, target->dst.u3.ip);
89 iph->daddr = target->dst.u3.ip;
94 static void nf_nat_ipv4_csum_update(struct sk_buff *skb,
95 unsigned int iphdroff, __sum16 *check,
96 const struct nf_conntrack_tuple *t,
97 enum nf_nat_manip_type maniptype)
99 struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
102 if (maniptype == NF_NAT_MANIP_SRC) {
104 newip = t->src.u3.ip;
107 newip = t->dst.u3.ip;
109 inet_proto_csum_replace4(check, skb, oldip, newip, true);
112 static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
113 u8 proto, void *data, __sum16 *check,
114 int datalen, int oldlen)
116 if (skb->ip_summed != CHECKSUM_PARTIAL) {
117 const struct iphdr *iph = ip_hdr(skb);
119 skb->ip_summed = CHECKSUM_PARTIAL;
120 skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
122 skb->csum_offset = (void *)check - data;
123 *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
126 inet_proto_csum_replace2(check, skb,
127 htons(oldlen), htons(datalen), true);
130 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
131 static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
132 struct nf_nat_range2 *range)
134 if (tb[CTA_NAT_V4_MINIP]) {
135 range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
136 range->flags |= NF_NAT_RANGE_MAP_IPS;
139 if (tb[CTA_NAT_V4_MAXIP])
140 range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
142 range->max_addr.ip = range->min_addr.ip;
148 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4 = {
149 .l3proto = NFPROTO_IPV4,
150 .manip_pkt = nf_nat_ipv4_manip_pkt,
151 .csum_update = nf_nat_ipv4_csum_update,
152 .csum_recalc = nf_nat_ipv4_csum_recalc,
153 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
154 .nlattr_to_range = nf_nat_ipv4_nlattr_to_range,
157 .decode_session = nf_nat_ipv4_decode_session,
161 int nf_nat_icmp_reply_translation(struct sk_buff *skb,
163 enum ip_conntrack_info ctinfo,
164 unsigned int hooknum)
170 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
171 enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
172 unsigned int hdrlen = ip_hdrlen(skb);
173 struct nf_conntrack_tuple target;
174 unsigned long statusbit;
176 WARN_ON(ctinfo != IP_CT_RELATED && ctinfo != IP_CT_RELATED_REPLY);
178 if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
180 if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
183 inside = (void *)skb->data + hdrlen;
184 if (inside->icmp.type == ICMP_REDIRECT) {
185 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
187 if (ct->status & IPS_NAT_MASK)
191 if (manip == NF_NAT_MANIP_SRC)
192 statusbit = IPS_SRC_NAT;
194 statusbit = IPS_DST_NAT;
196 /* Invert if this is reply direction */
197 if (dir == IP_CT_DIR_REPLY)
198 statusbit ^= IPS_NAT_MASK;
200 if (!(ct->status & statusbit))
203 if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp),
204 &ct->tuplehash[!dir].tuple, !manip))
207 if (skb->ip_summed != CHECKSUM_PARTIAL) {
208 /* Reloading "inside" here since manip_pkt may reallocate */
209 inside = (void *)skb->data + hdrlen;
210 inside->icmp.checksum = 0;
211 inside->icmp.checksum =
212 csum_fold(skb_checksum(skb, hdrlen,
213 skb->len - hdrlen, 0));
216 /* Change outer to look like the reply to an incoming packet */
217 nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
218 if (!nf_nat_ipv4_manip_pkt(skb, 0, &target, manip))
223 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
226 nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
227 const struct nf_hook_state *state)
230 enum ip_conntrack_info ctinfo;
232 ct = nf_ct_get(skb, &ctinfo);
236 if (ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY) {
237 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
238 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
246 return nf_nat_inet_fn(priv, skb, state);
250 nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
251 const struct nf_hook_state *state)
254 __be32 daddr = ip_hdr(skb)->daddr;
256 ret = nf_nat_ipv4_fn(priv, skb, state);
257 if (ret != NF_DROP && ret != NF_STOLEN &&
258 daddr != ip_hdr(skb)->daddr)
265 nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
266 const struct nf_hook_state *state)
269 const struct nf_conn *ct;
270 enum ip_conntrack_info ctinfo;
275 ret = nf_nat_ipv4_fn(priv, skb, state);
277 if (ret != NF_DROP && ret != NF_STOLEN &&
278 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
279 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
280 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
282 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
283 ct->tuplehash[!dir].tuple.dst.u3.ip) ||
284 (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
285 ct->tuplehash[dir].tuple.src.u.all !=
286 ct->tuplehash[!dir].tuple.dst.u.all)) {
287 err = nf_xfrm_me_harder(state->net, skb, AF_INET);
289 ret = NF_DROP_ERR(err);
297 nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
298 const struct nf_hook_state *state)
300 const struct nf_conn *ct;
301 enum ip_conntrack_info ctinfo;
305 ret = nf_nat_ipv4_fn(priv, skb, state);
306 if (ret != NF_DROP && ret != NF_STOLEN &&
307 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
308 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
310 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
311 ct->tuplehash[!dir].tuple.src.u3.ip) {
312 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
314 ret = NF_DROP_ERR(err);
317 else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
318 ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
319 ct->tuplehash[dir].tuple.dst.u.all !=
320 ct->tuplehash[!dir].tuple.src.u.all) {
321 err = nf_xfrm_me_harder(state->net, skb, AF_INET);
323 ret = NF_DROP_ERR(err);
330 static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
331 /* Before packet filtering, change destination */
333 .hook = nf_nat_ipv4_in,
335 .hooknum = NF_INET_PRE_ROUTING,
336 .priority = NF_IP_PRI_NAT_DST,
338 /* After packet filtering, change source */
340 .hook = nf_nat_ipv4_out,
342 .hooknum = NF_INET_POST_ROUTING,
343 .priority = NF_IP_PRI_NAT_SRC,
345 /* Before packet filtering, change destination */
347 .hook = nf_nat_ipv4_local_fn,
349 .hooknum = NF_INET_LOCAL_OUT,
350 .priority = NF_IP_PRI_NAT_DST,
352 /* After packet filtering, change source */
354 .hook = nf_nat_ipv4_fn,
356 .hooknum = NF_INET_LOCAL_IN,
357 .priority = NF_IP_PRI_NAT_SRC,
361 int nf_nat_l3proto_ipv4_register_fn(struct net *net, const struct nf_hook_ops *ops)
363 return nf_nat_register_fn(net, ops, nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
365 EXPORT_SYMBOL_GPL(nf_nat_l3proto_ipv4_register_fn);
367 void nf_nat_l3proto_ipv4_unregister_fn(struct net *net, const struct nf_hook_ops *ops)
369 nf_nat_unregister_fn(net, ops, ARRAY_SIZE(nf_nat_ipv4_ops));
371 EXPORT_SYMBOL_GPL(nf_nat_l3proto_ipv4_unregister_fn);
373 static int __init nf_nat_l3proto_ipv4_init(void)
375 return nf_nat_l3proto_register(&nf_nat_l3proto_ipv4);
378 static void __exit nf_nat_l3proto_ipv4_exit(void)
380 nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv4);
383 MODULE_LICENSE("GPL");
384 MODULE_ALIAS("nf-nat-" __stringify(AF_INET));
386 module_init(nf_nat_l3proto_ipv4_init);
387 module_exit(nf_nat_l3proto_ipv4_exit);