~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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, true);
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 &
136                      (NETIF_F_IP_CSUM | NETIF_F_HW_CSUM))) {
137                         skb->ip_summed = CHECKSUM_PARTIAL;
138                         skb->csum_start = skb_headroom(skb) +
139                                           skb_network_offset(skb) +
140                                           ip_hdrlen(skb);
141                         skb->csum_offset = (void *)check - data;
142                         *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
143                                                     datalen, proto, 0);
144                 } else {
145                         *check = 0;
146                         *check = csum_tcpudp_magic(iph->saddr, iph->daddr,
147                                                    datalen, proto,
148                                                    csum_partial(data, datalen,
149                                                                 0));
150                         if (proto == IPPROTO_UDP && !*check)
151                                 *check = CSUM_MANGLED_0;
152                 }
153         } else
154                 inet_proto_csum_replace2(check, skb,
155                                          htons(oldlen), htons(datalen), true);
156 }
157 
158 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
159 static int nf_nat_ipv4_nlattr_to_range(struct nlattr *tb[],
160                                        struct nf_nat_range *range)
161 {
162         if (tb[CTA_NAT_V4_MINIP]) {
163                 range->min_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MINIP]);
164                 range->flags |= NF_NAT_RANGE_MAP_IPS;
165         }
166 
167         if (tb[CTA_NAT_V4_MAXIP])
168                 range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
169         else
170                 range->max_addr.ip = range->min_addr.ip;
171 
172         return 0;
173 }
174 #endif
175 
176 static const struct nf_nat_l3proto nf_nat_l3proto_ipv4 = {
177         .l3proto                = NFPROTO_IPV4,
178         .in_range               = nf_nat_ipv4_in_range,
179         .secure_port            = nf_nat_ipv4_secure_port,
180         .manip_pkt              = nf_nat_ipv4_manip_pkt,
181         .csum_update            = nf_nat_ipv4_csum_update,
182         .csum_recalc            = nf_nat_ipv4_csum_recalc,
183 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
184         .nlattr_to_range        = nf_nat_ipv4_nlattr_to_range,
185 #endif
186 #ifdef CONFIG_XFRM
187         .decode_session         = nf_nat_ipv4_decode_session,
188 #endif
189 };
190 
191 int nf_nat_icmp_reply_translation(struct sk_buff *skb,
192                                   struct nf_conn *ct,
193                                   enum ip_conntrack_info ctinfo,
194                                   unsigned int hooknum)
195 {
196         struct {
197                 struct icmphdr  icmp;
198                 struct iphdr    ip;
199         } *inside;
200         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
201         enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
202         unsigned int hdrlen = ip_hdrlen(skb);
203         const struct nf_nat_l4proto *l4proto;
204         struct nf_conntrack_tuple target;
205         unsigned long statusbit;
206 
207         NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
208 
209         if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
210                 return 0;
211         if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
212                 return 0;
213 
214         inside = (void *)skb->data + hdrlen;
215         if (inside->icmp.type == ICMP_REDIRECT) {
216                 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
217                         return 0;
218                 if (ct->status & IPS_NAT_MASK)
219                         return 0;
220         }
221 
222         if (manip == NF_NAT_MANIP_SRC)
223                 statusbit = IPS_SRC_NAT;
224         else
225                 statusbit = IPS_DST_NAT;
226 
227         /* Invert if this is reply direction */
228         if (dir == IP_CT_DIR_REPLY)
229                 statusbit ^= IPS_NAT_MASK;
230 
231         if (!(ct->status & statusbit))
232                 return 1;
233 
234         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, inside->ip.protocol);
235         if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp),
236                                    l4proto, &ct->tuplehash[!dir].tuple, !manip))
237                 return 0;
238 
239         if (skb->ip_summed != CHECKSUM_PARTIAL) {
240                 /* Reloading "inside" here since manip_pkt may reallocate */
241                 inside = (void *)skb->data + hdrlen;
242                 inside->icmp.checksum = 0;
243                 inside->icmp.checksum =
244                         csum_fold(skb_checksum(skb, hdrlen,
245                                                skb->len - hdrlen, 0));
246         }
247 
248         /* Change outer to look like the reply to an incoming packet */
249         nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
250         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV4, 0);
251         if (!nf_nat_ipv4_manip_pkt(skb, 0, l4proto, &target, manip))
252                 return 0;
253 
254         return 1;
255 }
256 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
257 
258 unsigned int
259 nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,
260                const struct nf_hook_state *state,
261                unsigned int (*do_chain)(void *priv,
262                                         struct sk_buff *skb,
263                                         const struct nf_hook_state *state,
264                                         struct nf_conn *ct))
265 {
266         struct nf_conn *ct;
267         enum ip_conntrack_info ctinfo;
268         struct nf_conn_nat *nat;
269         /* maniptype == SRC for postrouting. */
270         enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
271 
272         /* We never see fragments: conntrack defrags on pre-routing
273          * and local-out, and nf_nat_out protects post-routing.
274          */
275         NF_CT_ASSERT(!ip_is_fragment(ip_hdr(skb)));
276 
277         ct = nf_ct_get(skb, &ctinfo);
278         /* Can't track?  It's not due to stress, or conntrack would
279          * have dropped it.  Hence it's the user's responsibilty to
280          * packet filter it out, or implement conntrack/NAT for that
281          * protocol. 8) --RR
282          */
283         if (!ct)
284                 return NF_ACCEPT;
285 
286         /* Don't try to NAT if this packet is not conntracked */
287         if (nf_ct_is_untracked(ct))
288                 return NF_ACCEPT;
289 
290         nat = nf_ct_nat_ext_add(ct);
291         if (nat == NULL)
292                 return NF_ACCEPT;
293 
294         switch (ctinfo) {
295         case IP_CT_RELATED:
296         case IP_CT_RELATED_REPLY:
297                 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
298                         if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
299                                                            state->hook))
300                                 return NF_DROP;
301                         else
302                                 return NF_ACCEPT;
303                 }
304                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
305         case IP_CT_NEW:
306                 /* Seen it before?  This can happen for loopback, retrans,
307                  * or local packets.
308                  */
309                 if (!nf_nat_initialized(ct, maniptype)) {
310                         unsigned int ret;
311 
312                         ret = do_chain(priv, skb, state, ct);
313                         if (ret != NF_ACCEPT)
314                                 return ret;
315 
316                         if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
317                                 break;
318 
319                         ret = nf_nat_alloc_null_binding(ct, state->hook);
320                         if (ret != NF_ACCEPT)
321                                 return ret;
322                 } else {
323                         pr_debug("Already setup manip %s for ct %p\n",
324                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
325                                  ct);
326                         if (nf_nat_oif_changed(state->hook, ctinfo, nat,
327                                                state->out))
328                                 goto oif_changed;
329                 }
330                 break;
331 
332         default:
333                 /* ESTABLISHED */
334                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
335                              ctinfo == IP_CT_ESTABLISHED_REPLY);
336                 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
337                         goto oif_changed;
338         }
339 
340         return nf_nat_packet(ct, ctinfo, state->hook, skb);
341 
342 oif_changed:
343         nf_ct_kill_acct(ct, ctinfo, skb);
344         return NF_DROP;
345 }
346 EXPORT_SYMBOL_GPL(nf_nat_ipv4_fn);
347 
348 unsigned int
349 nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
350                const struct nf_hook_state *state,
351                unsigned int (*do_chain)(void *priv,
352                                          struct sk_buff *skb,
353                                          const struct nf_hook_state *state,
354                                          struct nf_conn *ct))
355 {
356         unsigned int ret;
357         __be32 daddr = ip_hdr(skb)->daddr;
358 
359         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
360         if (ret != NF_DROP && ret != NF_STOLEN &&
361             daddr != ip_hdr(skb)->daddr)
362                 skb_dst_drop(skb);
363 
364         return ret;
365 }
366 EXPORT_SYMBOL_GPL(nf_nat_ipv4_in);
367 
368 unsigned int
369 nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
370                 const struct nf_hook_state *state,
371                 unsigned int (*do_chain)(void *priv,
372                                           struct sk_buff *skb,
373                                           const struct nf_hook_state *state,
374                                           struct nf_conn *ct))
375 {
376 #ifdef CONFIG_XFRM
377         const struct nf_conn *ct;
378         enum ip_conntrack_info ctinfo;
379         int err;
380 #endif
381         unsigned int ret;
382 
383         /* root is playing with raw sockets. */
384         if (skb->len < sizeof(struct iphdr) ||
385             ip_hdrlen(skb) < sizeof(struct iphdr))
386                 return NF_ACCEPT;
387 
388         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
389 #ifdef CONFIG_XFRM
390         if (ret != NF_DROP && ret != NF_STOLEN &&
391             !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
392             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
393                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
394 
395                 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
396                      ct->tuplehash[!dir].tuple.dst.u3.ip) ||
397                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
398                      ct->tuplehash[dir].tuple.src.u.all !=
399                      ct->tuplehash[!dir].tuple.dst.u.all)) {
400                         err = nf_xfrm_me_harder(state->net, skb, AF_INET);
401                         if (err < 0)
402                                 ret = NF_DROP_ERR(err);
403                 }
404         }
405 #endif
406         return ret;
407 }
408 EXPORT_SYMBOL_GPL(nf_nat_ipv4_out);
409 
410 unsigned int
411 nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
412                      const struct nf_hook_state *state,
413                      unsigned int (*do_chain)(void *priv,
414                                                struct sk_buff *skb,
415                                                const struct nf_hook_state *state,
416                                                struct nf_conn *ct))
417 {
418         const struct nf_conn *ct;
419         enum ip_conntrack_info ctinfo;
420         unsigned int ret;
421         int err;
422 
423         /* root is playing with raw sockets. */
424         if (skb->len < sizeof(struct iphdr) ||
425             ip_hdrlen(skb) < sizeof(struct iphdr))
426                 return NF_ACCEPT;
427 
428         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
429         if (ret != NF_DROP && ret != NF_STOLEN &&
430             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
431                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
432 
433                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
434                     ct->tuplehash[!dir].tuple.src.u3.ip) {
435                         err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
436                         if (err < 0)
437                                 ret = NF_DROP_ERR(err);
438                 }
439 #ifdef CONFIG_XFRM
440                 else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
441                          ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
442                          ct->tuplehash[dir].tuple.dst.u.all !=
443                          ct->tuplehash[!dir].tuple.src.u.all) {
444                         err = nf_xfrm_me_harder(state->net, skb, AF_INET);
445                         if (err < 0)
446                                 ret = NF_DROP_ERR(err);
447                 }
448 #endif
449         }
450         return ret;
451 }
452 EXPORT_SYMBOL_GPL(nf_nat_ipv4_local_fn);
453 
454 static int __init nf_nat_l3proto_ipv4_init(void)
455 {
456         int err;
457 
458         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
459         if (err < 0)
460                 goto err1;
461         err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv4);
462         if (err < 0)
463                 goto err2;
464         return err;
465 
466 err2:
467         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
468 err1:
469         return err;
470 }
471 
472 static void __exit nf_nat_l3proto_ipv4_exit(void)
473 {
474         nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv4);
475         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
476 }
477 
478 MODULE_LICENSE("GPL");
479 MODULE_ALIAS("nf-nat-" __stringify(AF_INET));
480 
481 module_init(nf_nat_l3proto_ipv4_init);
482 module_exit(nf_nat_l3proto_ipv4_exit);
483 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp