1 /* nf_nat_helper.c - generic support functions for NAT helpers
3 * (C) 2000-2002 Harald Welte <laforge@netfilter.org>
4 * (C) 2003-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2007-2012 Patrick McHardy <kaber@trash.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/gfp.h>
13 #include <linux/types.h>
14 #include <linux/skbuff.h>
15 #include <linux/tcp.h>
16 #include <linux/udp.h>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_helper.h>
21 #include <net/netfilter/nf_conntrack_ecache.h>
22 #include <net/netfilter/nf_conntrack_expect.h>
23 #include <net/netfilter/nf_conntrack_seqadj.h>
24 #include <net/netfilter/nf_nat.h>
25 #include <net/netfilter/nf_nat_l3proto.h>
26 #include <net/netfilter/nf_nat_l4proto.h>
27 #include <net/netfilter/nf_nat_core.h>
28 #include <net/netfilter/nf_nat_helper.h>
30 /* Frobs data inside this packet, which is linear. */
31 static void mangle_contents(struct sk_buff *skb,
33 unsigned int match_offset,
34 unsigned int match_len,
35 const char *rep_buffer,
40 SKB_LINEAR_ASSERT(skb);
41 data = skb_network_header(skb) + dataoff;
43 /* move post-replacement */
44 memmove(data + match_offset + rep_len,
45 data + match_offset + match_len,
46 skb_tail_pointer(skb) - (skb_network_header(skb) + dataoff +
47 match_offset + match_len));
49 /* insert data from buffer */
50 memcpy(data + match_offset, rep_buffer, rep_len);
53 if (rep_len > match_len) {
54 pr_debug("nf_nat_mangle_packet: Extending packet by "
55 "%u from %u bytes\n", rep_len - match_len, skb->len);
56 skb_put(skb, rep_len - match_len);
58 pr_debug("nf_nat_mangle_packet: Shrinking packet from "
59 "%u from %u bytes\n", match_len - rep_len, skb->len);
60 __skb_trim(skb, skb->len + rep_len - match_len);
63 if (nf_ct_l3num((struct nf_conn *)skb_nfct(skb)) == NFPROTO_IPV4) {
64 /* fix IP hdr checksum information */
65 ip_hdr(skb)->tot_len = htons(skb->len);
66 ip_send_check(ip_hdr(skb));
68 ipv6_hdr(skb)->payload_len =
69 htons(skb->len - sizeof(struct ipv6hdr));
72 /* Unusual, but possible case. */
73 static bool enlarge_skb(struct sk_buff *skb, unsigned int extra)
75 if (skb->len + extra > 65535)
78 if (pskb_expand_head(skb, 0, extra - skb_tailroom(skb), GFP_ATOMIC))
84 /* Generic function for mangling variable-length address changes inside
85 * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
88 * Takes care about all the nasty sequence number changes, checksumming,
89 * skb enlargement, ...
92 bool __nf_nat_mangle_tcp_packet(struct sk_buff *skb,
94 enum ip_conntrack_info ctinfo,
96 unsigned int match_offset,
97 unsigned int match_len,
98 const char *rep_buffer,
99 unsigned int rep_len, bool adjust)
101 const struct nf_nat_l3proto *l3proto;
105 if (!skb_make_writable(skb, skb->len))
108 if (rep_len > match_len &&
109 rep_len - match_len > skb_tailroom(skb) &&
110 !enlarge_skb(skb, rep_len - match_len))
113 tcph = (void *)skb->data + protoff;
115 oldlen = skb->len - protoff;
116 mangle_contents(skb, protoff + tcph->doff*4,
117 match_offset, match_len, rep_buffer, rep_len);
119 datalen = skb->len - protoff;
121 l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct));
122 l3proto->csum_recalc(skb, IPPROTO_TCP, tcph, &tcph->check,
125 if (adjust && rep_len != match_len)
126 nf_ct_seqadj_set(ct, ctinfo, tcph->seq,
127 (int)rep_len - (int)match_len);
131 EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet);
133 /* Generic function for mangling variable-length address changes inside
134 * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
135 * command in the Amanda protocol)
137 * Takes care about all the nasty sequence number changes, checksumming,
138 * skb enlargement, ...
140 * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
141 * should be fairly easy to do.
144 nf_nat_mangle_udp_packet(struct sk_buff *skb,
146 enum ip_conntrack_info ctinfo,
147 unsigned int protoff,
148 unsigned int match_offset,
149 unsigned int match_len,
150 const char *rep_buffer,
151 unsigned int rep_len)
153 const struct nf_nat_l3proto *l3proto;
157 if (!skb_make_writable(skb, skb->len))
160 if (rep_len > match_len &&
161 rep_len - match_len > skb_tailroom(skb) &&
162 !enlarge_skb(skb, rep_len - match_len))
165 udph = (void *)skb->data + protoff;
167 oldlen = skb->len - protoff;
168 mangle_contents(skb, protoff + sizeof(*udph),
169 match_offset, match_len, rep_buffer, rep_len);
171 /* update the length of the UDP packet */
172 datalen = skb->len - protoff;
173 udph->len = htons(datalen);
175 /* fix udp checksum if udp checksum was previously calculated */
176 if (!udph->check && skb->ip_summed != CHECKSUM_PARTIAL)
179 l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct));
180 l3proto->csum_recalc(skb, IPPROTO_UDP, udph, &udph->check,
185 EXPORT_SYMBOL(nf_nat_mangle_udp_packet);
187 /* Setup NAT on this expected conntrack so it follows master. */
188 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
189 void nf_nat_follow_master(struct nf_conn *ct,
190 struct nf_conntrack_expect *exp)
192 struct nf_nat_range2 range;
194 /* This must be a fresh one. */
195 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
197 /* Change src to where master sends to */
198 range.flags = NF_NAT_RANGE_MAP_IPS;
199 range.min_addr = range.max_addr
200 = ct->master->tuplehash[!exp->dir].tuple.dst.u3;
201 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC);
203 /* For DST manip, map port here to where it's expected. */
204 range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED);
205 range.min_proto = range.max_proto = exp->saved_proto;
206 range.min_addr = range.max_addr
207 = ct->master->tuplehash[!exp->dir].tuple.src.u3;
208 nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST);
210 EXPORT_SYMBOL(nf_nat_follow_master);