Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* nf_nat_helper.c - generic support functions for NAT helpers
0003  *
0004  * (C) 2000-2002 Harald Welte <laforge@netfilter.org>
0005  * (C) 2003-2006 Netfilter Core Team <coreteam@netfilter.org>
0006  * (C) 2007-2012 Patrick McHardy <kaber@trash.net>
0007  */
0008 #include <linux/module.h>
0009 #include <linux/gfp.h>
0010 #include <linux/types.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/tcp.h>
0013 #include <linux/udp.h>
0014 #include <net/tcp.h>
0015 
0016 #include <net/netfilter/nf_conntrack.h>
0017 #include <net/netfilter/nf_conntrack_helper.h>
0018 #include <net/netfilter/nf_conntrack_ecache.h>
0019 #include <net/netfilter/nf_conntrack_expect.h>
0020 #include <net/netfilter/nf_conntrack_seqadj.h>
0021 #include <net/netfilter/nf_nat.h>
0022 #include <net/netfilter/nf_nat_helper.h>
0023 
0024 /* Frobs data inside this packet, which is linear. */
0025 static void mangle_contents(struct sk_buff *skb,
0026                 unsigned int dataoff,
0027                 unsigned int match_offset,
0028                 unsigned int match_len,
0029                 const char *rep_buffer,
0030                 unsigned int rep_len)
0031 {
0032     unsigned char *data;
0033 
0034     SKB_LINEAR_ASSERT(skb);
0035     data = skb_network_header(skb) + dataoff;
0036 
0037     /* move post-replacement */
0038     memmove(data + match_offset + rep_len,
0039         data + match_offset + match_len,
0040         skb_tail_pointer(skb) - (skb_network_header(skb) + dataoff +
0041                  match_offset + match_len));
0042 
0043     /* insert data from buffer */
0044     memcpy(data + match_offset, rep_buffer, rep_len);
0045 
0046     /* update skb info */
0047     if (rep_len > match_len) {
0048         pr_debug("nf_nat_mangle_packet: Extending packet by "
0049              "%u from %u bytes\n", rep_len - match_len, skb->len);
0050         skb_put(skb, rep_len - match_len);
0051     } else {
0052         pr_debug("nf_nat_mangle_packet: Shrinking packet from "
0053              "%u from %u bytes\n", match_len - rep_len, skb->len);
0054         __skb_trim(skb, skb->len + rep_len - match_len);
0055     }
0056 
0057     if (nf_ct_l3num((struct nf_conn *)skb_nfct(skb)) == NFPROTO_IPV4) {
0058         /* fix IP hdr checksum information */
0059         ip_hdr(skb)->tot_len = htons(skb->len);
0060         ip_send_check(ip_hdr(skb));
0061     } else
0062         ipv6_hdr(skb)->payload_len =
0063             htons(skb->len - sizeof(struct ipv6hdr));
0064 }
0065 
0066 /* Unusual, but possible case. */
0067 static bool enlarge_skb(struct sk_buff *skb, unsigned int extra)
0068 {
0069     if (skb->len + extra > 65535)
0070         return false;
0071 
0072     if (pskb_expand_head(skb, 0, extra - skb_tailroom(skb), GFP_ATOMIC))
0073         return false;
0074 
0075     return true;
0076 }
0077 
0078 /* Generic function for mangling variable-length address changes inside
0079  * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
0080  * command in FTP).
0081  *
0082  * Takes care about all the nasty sequence number changes, checksumming,
0083  * skb enlargement, ...
0084  *
0085  * */
0086 bool __nf_nat_mangle_tcp_packet(struct sk_buff *skb,
0087                 struct nf_conn *ct,
0088                 enum ip_conntrack_info ctinfo,
0089                 unsigned int protoff,
0090                 unsigned int match_offset,
0091                 unsigned int match_len,
0092                 const char *rep_buffer,
0093                 unsigned int rep_len, bool adjust)
0094 {
0095     struct tcphdr *tcph;
0096     int oldlen, datalen;
0097 
0098     if (skb_ensure_writable(skb, skb->len))
0099         return false;
0100 
0101     if (rep_len > match_len &&
0102         rep_len - match_len > skb_tailroom(skb) &&
0103         !enlarge_skb(skb, rep_len - match_len))
0104         return false;
0105 
0106     tcph = (void *)skb->data + protoff;
0107 
0108     oldlen = skb->len - protoff;
0109     mangle_contents(skb, protoff + tcph->doff*4,
0110             match_offset, match_len, rep_buffer, rep_len);
0111 
0112     datalen = skb->len - protoff;
0113 
0114     nf_nat_csum_recalc(skb, nf_ct_l3num(ct), IPPROTO_TCP,
0115                tcph, &tcph->check, datalen, oldlen);
0116 
0117     if (adjust && rep_len != match_len)
0118         nf_ct_seqadj_set(ct, ctinfo, tcph->seq,
0119                  (int)rep_len - (int)match_len);
0120 
0121     return true;
0122 }
0123 EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet);
0124 
0125 /* Generic function for mangling variable-length address changes inside
0126  * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
0127  * command in the Amanda protocol)
0128  *
0129  * Takes care about all the nasty sequence number changes, checksumming,
0130  * skb enlargement, ...
0131  *
0132  * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
0133  *       should be fairly easy to do.
0134  */
0135 bool
0136 nf_nat_mangle_udp_packet(struct sk_buff *skb,
0137              struct nf_conn *ct,
0138              enum ip_conntrack_info ctinfo,
0139              unsigned int protoff,
0140              unsigned int match_offset,
0141              unsigned int match_len,
0142              const char *rep_buffer,
0143              unsigned int rep_len)
0144 {
0145     struct udphdr *udph;
0146     int datalen, oldlen;
0147 
0148     if (skb_ensure_writable(skb, skb->len))
0149         return false;
0150 
0151     if (rep_len > match_len &&
0152         rep_len - match_len > skb_tailroom(skb) &&
0153         !enlarge_skb(skb, rep_len - match_len))
0154         return false;
0155 
0156     udph = (void *)skb->data + protoff;
0157 
0158     oldlen = skb->len - protoff;
0159     mangle_contents(skb, protoff + sizeof(*udph),
0160             match_offset, match_len, rep_buffer, rep_len);
0161 
0162     /* update the length of the UDP packet */
0163     datalen = skb->len - protoff;
0164     udph->len = htons(datalen);
0165 
0166     /* fix udp checksum if udp checksum was previously calculated */
0167     if (!udph->check && skb->ip_summed != CHECKSUM_PARTIAL)
0168         return true;
0169 
0170     nf_nat_csum_recalc(skb, nf_ct_l3num(ct), IPPROTO_UDP,
0171                udph, &udph->check, datalen, oldlen);
0172 
0173     return true;
0174 }
0175 EXPORT_SYMBOL(nf_nat_mangle_udp_packet);
0176 
0177 /* Setup NAT on this expected conntrack so it follows master. */
0178 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
0179 void nf_nat_follow_master(struct nf_conn *ct,
0180               struct nf_conntrack_expect *exp)
0181 {
0182     struct nf_nat_range2 range;
0183 
0184     /* This must be a fresh one. */
0185     BUG_ON(ct->status & IPS_NAT_DONE_MASK);
0186 
0187     /* Change src to where master sends to */
0188     range.flags = NF_NAT_RANGE_MAP_IPS;
0189     range.min_addr = range.max_addr
0190         = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
0191     nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
0192 
0193     /* For DST manip, map port here to where it's expected. */
0194     range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
0195     range.min_proto = range.max_proto = exp->saved_proto;
0196     range.min_addr = range.max_addr
0197         = ct->master->tuplehash[!exp->dir].tuple.src.u3;
0198     nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
0199 }
0200 EXPORT_SYMBOL(nf_nat_follow_master);