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

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

Version: ~ [ linux-5.12 ] ~ [ linux-5.11.16 ] ~ [ linux-5.10.32 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.114 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.188 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.231 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.267 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.267 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ 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         if (skb->ip_summed != CHECKSUM_PARTIAL) {
131                 const struct iphdr *iph = ip_hdr(skb);
132 
133                 skb->ip_summed = CHECKSUM_PARTIAL;
134                 skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
135                         ip_hdrlen(skb);
136                 skb->csum_offset = (void *)check - data;
137                 *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
138                                             proto, 0);
139         } else
140                 inet_proto_csum_replace2(check, skb,
141                                          htons(oldlen), htons(datalen), true);
142 }
143 
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)
147 {
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;
151         }
152 
153         if (tb[CTA_NAT_V4_MAXIP])
154                 range->max_addr.ip = nla_get_be32(tb[CTA_NAT_V4_MAXIP]);
155         else
156                 range->max_addr.ip = range->min_addr.ip;
157 
158         return 0;
159 }
160 #endif
161 
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,
171 #endif
172 #ifdef CONFIG_XFRM
173         .decode_session         = nf_nat_ipv4_decode_session,
174 #endif
175 };
176 
177 int nf_nat_icmp_reply_translation(struct sk_buff *skb,
178                                   struct nf_conn *ct,
179                                   enum ip_conntrack_info ctinfo,
180                                   unsigned int hooknum)
181 {
182         struct {
183                 struct icmphdr  icmp;
184                 struct iphdr    ip;
185         } *inside;
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;
192 
193         NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY);
194 
195         if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
196                 return 0;
197         if (nf_ip_checksum(skb, hooknum, hdrlen, 0))
198                 return 0;
199 
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)
203                         return 0;
204                 if (ct->status & IPS_NAT_MASK)
205                         return 0;
206         }
207 
208         if (manip == NF_NAT_MANIP_SRC)
209                 statusbit = IPS_SRC_NAT;
210         else
211                 statusbit = IPS_DST_NAT;
212 
213         /* Invert if this is reply direction */
214         if (dir == IP_CT_DIR_REPLY)
215                 statusbit ^= IPS_NAT_MASK;
216 
217         if (!(ct->status & statusbit))
218                 return 1;
219 
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))
223                 return 0;
224 
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));
232         }
233 
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))
238                 return 0;
239 
240         return 1;
241 }
242 EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
243 
244 unsigned int
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,
248                                         struct sk_buff *skb,
249                                         const struct nf_hook_state *state,
250                                         struct nf_conn *ct))
251 {
252         struct nf_conn *ct;
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);
257 
258         ct = nf_ct_get(skb, &ctinfo);
259         /* Can't track?  It's not due to stress, or conntrack would
260          * have dropped it.  Hence it's the user's responsibilty to
261          * packet filter it out, or implement conntrack/NAT for that
262          * protocol. 8) --RR
263          */
264         if (!ct)
265                 return NF_ACCEPT;
266 
267         nat = nfct_nat(ct);
268 
269         switch (ctinfo) {
270         case IP_CT_RELATED:
271         case IP_CT_RELATED_REPLY:
272                 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
273                         if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
274                                                            state->hook))
275                                 return NF_DROP;
276                         else
277                                 return NF_ACCEPT;
278                 }
279                 /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */
280         case IP_CT_NEW:
281                 /* Seen it before?  This can happen for loopback, retrans,
282                  * or local packets.
283                  */
284                 if (!nf_nat_initialized(ct, maniptype)) {
285                         unsigned int ret;
286 
287                         ret = do_chain(priv, skb, state, ct);
288                         if (ret != NF_ACCEPT)
289                                 return ret;
290 
291                         if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
292                                 break;
293 
294                         ret = nf_nat_alloc_null_binding(ct, state->hook);
295                         if (ret != NF_ACCEPT)
296                                 return ret;
297                 } else {
298                         pr_debug("Already setup manip %s for ct %p\n",
299                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
300                                  ct);
301                         if (nf_nat_oif_changed(state->hook, ctinfo, nat,
302                                                state->out))
303                                 goto oif_changed;
304                 }
305                 break;
306 
307         default:
308                 /* ESTABLISHED */
309                 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
310                              ctinfo == IP_CT_ESTABLISHED_REPLY);
311                 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
312                         goto oif_changed;
313         }
314 
315         return nf_nat_packet(ct, ctinfo, state->hook, skb);
316 
317 oif_changed:
318         nf_ct_kill_acct(ct, ctinfo, skb);
319         return NF_DROP;
320 }
321 EXPORT_SYMBOL_GPL(nf_nat_ipv4_fn);
322 
323 unsigned int
324 nf_nat_ipv4_in(void *priv, struct sk_buff *skb,
325                const struct nf_hook_state *state,
326                unsigned int (*do_chain)(void *priv,
327                                          struct sk_buff *skb,
328                                          const struct nf_hook_state *state,
329                                          struct nf_conn *ct))
330 {
331         unsigned int ret;
332         __be32 daddr = ip_hdr(skb)->daddr;
333 
334         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
335         if (ret != NF_DROP && ret != NF_STOLEN &&
336             daddr != ip_hdr(skb)->daddr)
337                 skb_dst_drop(skb);
338 
339         return ret;
340 }
341 EXPORT_SYMBOL_GPL(nf_nat_ipv4_in);
342 
343 unsigned int
344 nf_nat_ipv4_out(void *priv, struct sk_buff *skb,
345                 const struct nf_hook_state *state,
346                 unsigned int (*do_chain)(void *priv,
347                                           struct sk_buff *skb,
348                                           const struct nf_hook_state *state,
349                                           struct nf_conn *ct))
350 {
351 #ifdef CONFIG_XFRM
352         const struct nf_conn *ct;
353         enum ip_conntrack_info ctinfo;
354         int err;
355 #endif
356         unsigned int ret;
357 
358         /* root is playing with raw sockets. */
359         if (skb->len < sizeof(struct iphdr) ||
360             ip_hdrlen(skb) < sizeof(struct iphdr))
361                 return NF_ACCEPT;
362 
363         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
364 #ifdef CONFIG_XFRM
365         if (ret != NF_DROP && ret != NF_STOLEN &&
366             !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
367             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
368                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
369 
370                 if ((ct->tuplehash[dir].tuple.src.u3.ip !=
371                      ct->tuplehash[!dir].tuple.dst.u3.ip) ||
372                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
373                      ct->tuplehash[dir].tuple.src.u.all !=
374                      ct->tuplehash[!dir].tuple.dst.u.all)) {
375                         err = nf_xfrm_me_harder(state->net, skb, AF_INET);
376                         if (err < 0)
377                                 ret = NF_DROP_ERR(err);
378                 }
379         }
380 #endif
381         return ret;
382 }
383 EXPORT_SYMBOL_GPL(nf_nat_ipv4_out);
384 
385 unsigned int
386 nf_nat_ipv4_local_fn(void *priv, struct sk_buff *skb,
387                      const struct nf_hook_state *state,
388                      unsigned int (*do_chain)(void *priv,
389                                                struct sk_buff *skb,
390                                                const struct nf_hook_state *state,
391                                                struct nf_conn *ct))
392 {
393         const struct nf_conn *ct;
394         enum ip_conntrack_info ctinfo;
395         unsigned int ret;
396         int err;
397 
398         /* root is playing with raw sockets. */
399         if (skb->len < sizeof(struct iphdr) ||
400             ip_hdrlen(skb) < sizeof(struct iphdr))
401                 return NF_ACCEPT;
402 
403         ret = nf_nat_ipv4_fn(priv, skb, state, do_chain);
404         if (ret != NF_DROP && ret != NF_STOLEN &&
405             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
406                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
407 
408                 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
409                     ct->tuplehash[!dir].tuple.src.u3.ip) {
410                         err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
411                         if (err < 0)
412                                 ret = NF_DROP_ERR(err);
413                 }
414 #ifdef CONFIG_XFRM
415                 else if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
416                          ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMP &&
417                          ct->tuplehash[dir].tuple.dst.u.all !=
418                          ct->tuplehash[!dir].tuple.src.u.all) {
419                         err = nf_xfrm_me_harder(state->net, skb, AF_INET);
420                         if (err < 0)
421                                 ret = NF_DROP_ERR(err);
422                 }
423 #endif
424         }
425         return ret;
426 }
427 EXPORT_SYMBOL_GPL(nf_nat_ipv4_local_fn);
428 
429 static int __init nf_nat_l3proto_ipv4_init(void)
430 {
431         int err;
432 
433         err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
434         if (err < 0)
435                 goto err1;
436         err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv4);
437         if (err < 0)
438                 goto err2;
439         return err;
440 
441 err2:
442         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
443 err1:
444         return err;
445 }
446 
447 static void __exit nf_nat_l3proto_ipv4_exit(void)
448 {
449         nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv4);
450         nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_icmp);
451 }
452 
453 MODULE_LICENSE("GPL");
454 MODULE_ALIAS("nf-nat-" __stringify(AF_INET));
455 
456 module_init(nf_nat_l3proto_ipv4_init);
457 module_exit(nf_nat_l3proto_ipv4_exit);
458 

~ [ 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