1 /* ip_nat_helper.c - generic support functions for NAT helpers
3 * (C) 2000-2002 Harald Welte <laforge@netfilter.org>
4 * (C) 2003-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
10 * 14 Jan 2002 Harald Welte <laforge@gnumonks.org>:
11 * - add support for SACK adjustment
12 * 14 Mar 2002 Harald Welte <laforge@gnumonks.org>:
13 * - merge SACK support into newnat API
14 * 16 Aug 2002 Brian J. Murrell <netfilter@interlinx.bc.ca>:
15 * - make ip_nat_resize_packet more generic (TCP and UDP)
16 * - add ip_nat_mangle_udp_packet
18 #include <linux/module.h>
19 #include <linux/kmod.h>
20 #include <linux/types.h>
21 #include <linux/timer.h>
22 #include <linux/skbuff.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <net/checksum.h>
30 #define ASSERT_READ_LOCK(x)
31 #define ASSERT_WRITE_LOCK(x)
33 #include <linux/netfilter_ipv4/ip_conntrack.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
35 #include <linux/netfilter_ipv4/ip_nat.h>
36 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
37 #include <linux/netfilter_ipv4/ip_nat_core.h>
38 #include <linux/netfilter_ipv4/ip_nat_helper.h>
39 #include <linux/netfilter_ipv4/listhelp.h>
43 #define DUMP_OFFSET(x) printk("offset_before=%d, offset_after=%d, correction_pos=%u\n", x->offset_before, x->offset_after, x->correction_pos);
45 #define DEBUGP(format, args...)
46 #define DUMP_OFFSET(x)
49 static DEFINE_SPINLOCK(ip_nat_seqofs_lock);
51 /* Setup TCP sequence correction given this change at this sequence */
53 adjust_tcp_sequence(u32 seq,
55 struct ip_conntrack *ct,
56 enum ip_conntrack_info ctinfo)
59 struct ip_nat_seq *this_way, *other_way;
61 DEBUGP("ip_nat_resize_packet: old_size = %u, new_size = %u\n",
62 (*skb)->len, new_size);
64 dir = CTINFO2DIR(ctinfo);
66 this_way = &ct->nat.info.seq[dir];
67 other_way = &ct->nat.info.seq[!dir];
69 DEBUGP("ip_nat_resize_packet: Seq_offset before: ");
70 DUMP_OFFSET(this_way);
72 spin_lock_bh(&ip_nat_seqofs_lock);
74 /* SYN adjust. If it's uninitialized, or this is after last
75 * correction, record it: we don't handle more than one
76 * adjustment in the window, but do deal with common case of a
78 if (this_way->offset_before == this_way->offset_after
79 || before(this_way->correction_pos, seq)) {
80 this_way->correction_pos = seq;
81 this_way->offset_before = this_way->offset_after;
82 this_way->offset_after += sizediff;
84 spin_unlock_bh(&ip_nat_seqofs_lock);
86 DEBUGP("ip_nat_resize_packet: Seq_offset after: ");
87 DUMP_OFFSET(this_way);
90 /* Frobs data inside this packet, which is linear. */
91 static void mangle_contents(struct sk_buff *skb,
93 unsigned int match_offset,
94 unsigned int match_len,
95 const char *rep_buffer,
100 BUG_ON(skb_is_nonlinear(skb));
101 data = (unsigned char *)skb->nh.iph + dataoff;
103 /* move post-replacement */
104 memmove(data + match_offset + rep_len,
105 data + match_offset + match_len,
106 skb->tail - (data + match_offset + match_len));
108 /* insert data from buffer */
109 memcpy(data + match_offset, rep_buffer, rep_len);
111 /* update skb info */
112 if (rep_len > match_len) {
113 DEBUGP("ip_nat_mangle_packet: Extending packet by "
114 "%u from %u bytes\n", rep_len - match_len,
116 skb_put(skb, rep_len - match_len);
118 DEBUGP("ip_nat_mangle_packet: Shrinking packet from "
119 "%u from %u bytes\n", match_len - rep_len,
121 __skb_trim(skb, skb->len + rep_len - match_len);
124 /* fix IP hdr checksum information */
125 skb->nh.iph->tot_len = htons(skb->len);
126 ip_send_check(skb->nh.iph);
129 /* Unusual, but possible case. */
130 static int enlarge_skb(struct sk_buff **pskb, unsigned int extra)
132 struct sk_buff *nskb;
134 if ((*pskb)->len + extra > 65535)
137 nskb = skb_copy_expand(*pskb, skb_headroom(*pskb), extra, GFP_ATOMIC);
141 /* Transfer socket to new skb. */
143 skb_set_owner_w(nskb, (*pskb)->sk);
149 /* Generic function for mangling variable-length address changes inside
150 * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
153 * Takes care about all the nasty sequence number changes, checksumming,
154 * skb enlargement, ...
158 ip_nat_mangle_tcp_packet(struct sk_buff **pskb,
159 struct ip_conntrack *ct,
160 enum ip_conntrack_info ctinfo,
161 unsigned int match_offset,
162 unsigned int match_len,
163 const char *rep_buffer,
164 unsigned int rep_len)
170 if (!skb_make_writable(pskb, (*pskb)->len))
173 if (rep_len > match_len
174 && rep_len - match_len > skb_tailroom(*pskb)
175 && !enlarge_skb(pskb, rep_len - match_len))
178 SKB_LINEAR_ASSERT(*pskb);
180 iph = (*pskb)->nh.iph;
181 tcph = (void *)iph + iph->ihl*4;
183 oldlen = (*pskb)->len - iph->ihl*4;
184 mangle_contents(*pskb, iph->ihl*4 + tcph->doff*4,
185 match_offset, match_len, rep_buffer, rep_len);
187 datalen = (*pskb)->len - iph->ihl*4;
188 if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) {
190 tcph->check = tcp_v4_check(tcph, datalen,
191 iph->saddr, iph->daddr,
192 csum_partial((char *)tcph,
195 tcph->check = nf_proto_csum_update(*pskb,
196 htons(oldlen) ^ 0xFFFF,
200 if (rep_len != match_len) {
201 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
202 adjust_tcp_sequence(ntohl(tcph->seq),
203 (int)rep_len - (int)match_len,
205 /* Tell TCP window tracking about seq change */
206 ip_conntrack_tcp_update(*pskb, ct, CTINFO2DIR(ctinfo));
210 EXPORT_SYMBOL(ip_nat_mangle_tcp_packet);
212 /* Generic function for mangling variable-length address changes inside
213 * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
214 * command in the Amanda protocol)
216 * Takes care about all the nasty sequence number changes, checksumming,
217 * skb enlargement, ...
219 * XXX - This function could be merged with ip_nat_mangle_tcp_packet which
220 * should be fairly easy to do.
223 ip_nat_mangle_udp_packet(struct sk_buff **pskb,
224 struct ip_conntrack *ct,
225 enum ip_conntrack_info ctinfo,
226 unsigned int match_offset,
227 unsigned int match_len,
228 const char *rep_buffer,
229 unsigned int rep_len)
235 /* UDP helpers might accidentally mangle the wrong packet */
236 iph = (*pskb)->nh.iph;
237 if ((*pskb)->len < iph->ihl*4 + sizeof(*udph) +
238 match_offset + match_len)
241 if (!skb_make_writable(pskb, (*pskb)->len))
244 if (rep_len > match_len
245 && rep_len - match_len > skb_tailroom(*pskb)
246 && !enlarge_skb(pskb, rep_len - match_len))
249 iph = (*pskb)->nh.iph;
250 udph = (void *)iph + iph->ihl*4;
252 oldlen = (*pskb)->len - iph->ihl*4;
253 mangle_contents(*pskb, iph->ihl*4 + sizeof(*udph),
254 match_offset, match_len, rep_buffer, rep_len);
256 /* update the length of the UDP packet */
257 datalen = (*pskb)->len - iph->ihl*4;
258 udph->len = htons(datalen);
260 /* fix udp checksum if udp checksum was previously calculated */
261 if (!udph->check && (*pskb)->ip_summed != CHECKSUM_PARTIAL)
264 if ((*pskb)->ip_summed != CHECKSUM_PARTIAL) {
266 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
267 datalen, IPPROTO_UDP,
268 csum_partial((char *)udph,
273 udph->check = nf_proto_csum_update(*pskb,
274 htons(oldlen) ^ 0xFFFF,
279 EXPORT_SYMBOL(ip_nat_mangle_udp_packet);
281 /* Adjust one found SACK option including checksum correction */
283 sack_adjust(struct sk_buff *skb,
285 unsigned int sackoff,
286 unsigned int sackend,
287 struct ip_nat_seq *natseq)
289 while (sackoff < sackend) {
290 struct tcp_sack_block *sack;
291 u_int32_t new_start_seq, new_end_seq;
293 sack = (void *)skb->data + sackoff;
294 if (after(ntohl(sack->start_seq) - natseq->offset_before,
295 natseq->correction_pos))
296 new_start_seq = ntohl(sack->start_seq)
297 - natseq->offset_after;
299 new_start_seq = ntohl(sack->start_seq)
300 - natseq->offset_before;
301 new_start_seq = htonl(new_start_seq);
303 if (after(ntohl(sack->end_seq) - natseq->offset_before,
304 natseq->correction_pos))
305 new_end_seq = ntohl(sack->end_seq)
306 - natseq->offset_after;
308 new_end_seq = ntohl(sack->end_seq)
309 - natseq->offset_before;
310 new_end_seq = htonl(new_end_seq);
312 DEBUGP("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n",
313 ntohl(sack->start_seq), new_start_seq,
314 ntohl(sack->end_seq), new_end_seq);
316 tcph->check = nf_proto_csum_update(skb,
320 tcph->check = nf_proto_csum_update(skb,
324 sack->start_seq = new_start_seq;
325 sack->end_seq = new_end_seq;
326 sackoff += sizeof(*sack);
330 /* TCP SACK sequence number adjustment */
331 static inline unsigned int
332 ip_nat_sack_adjust(struct sk_buff **pskb,
334 struct ip_conntrack *ct,
335 enum ip_conntrack_info ctinfo)
337 unsigned int dir, optoff, optend;
339 optoff = (*pskb)->nh.iph->ihl*4 + sizeof(struct tcphdr);
340 optend = (*pskb)->nh.iph->ihl*4 + tcph->doff*4;
342 if (!skb_make_writable(pskb, optend))
345 dir = CTINFO2DIR(ctinfo);
347 while (optoff < optend) {
348 /* Usually: option, length. */
349 unsigned char *op = (*pskb)->data + optoff;
358 /* no partial options */
359 if (optoff + 1 == optend
360 || optoff + op[1] > optend
363 if (op[0] == TCPOPT_SACK
364 && op[1] >= 2+TCPOLEN_SACK_PERBLOCK
365 && ((op[1] - 2) % TCPOLEN_SACK_PERBLOCK) == 0)
366 sack_adjust(*pskb, tcph, optoff+2,
368 &ct->nat.info.seq[!dir]);
375 /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */
377 ip_nat_seq_adjust(struct sk_buff **pskb,
378 struct ip_conntrack *ct,
379 enum ip_conntrack_info ctinfo)
382 int dir, newseq, newack;
383 struct ip_nat_seq *this_way, *other_way;
385 dir = CTINFO2DIR(ctinfo);
387 this_way = &ct->nat.info.seq[dir];
388 other_way = &ct->nat.info.seq[!dir];
390 if (!skb_make_writable(pskb, (*pskb)->nh.iph->ihl*4+sizeof(*tcph)))
393 tcph = (void *)(*pskb)->data + (*pskb)->nh.iph->ihl*4;
394 if (after(ntohl(tcph->seq), this_way->correction_pos))
395 newseq = ntohl(tcph->seq) + this_way->offset_after;
397 newseq = ntohl(tcph->seq) + this_way->offset_before;
398 newseq = htonl(newseq);
400 if (after(ntohl(tcph->ack_seq) - other_way->offset_before,
401 other_way->correction_pos))
402 newack = ntohl(tcph->ack_seq) - other_way->offset_after;
404 newack = ntohl(tcph->ack_seq) - other_way->offset_before;
405 newack = htonl(newack);
407 tcph->check = nf_proto_csum_update(*pskb, ~tcph->seq, newseq,
409 tcph->check = nf_proto_csum_update(*pskb, ~tcph->ack_seq, newack,
412 DEBUGP("Adjusting sequence number from %u->%u, ack from %u->%u\n",
413 ntohl(tcph->seq), ntohl(newseq), ntohl(tcph->ack_seq),
417 tcph->ack_seq = newack;
419 if (!ip_nat_sack_adjust(pskb, tcph, ct, ctinfo))
422 ip_conntrack_tcp_update(*pskb, ct, dir);
426 EXPORT_SYMBOL(ip_nat_seq_adjust);
428 /* Setup NAT on this expected conntrack so it follows master. */
429 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
430 void ip_nat_follow_master(struct ip_conntrack *ct,
431 struct ip_conntrack_expect *exp)
433 struct ip_nat_range range;
435 /* This must be a fresh one. */
436 BUG_ON(ct->status & IPS_NAT_DONE_MASK);
438 /* Change src to where master sends to */
439 range.flags = IP_NAT_RANGE_MAP_IPS;
440 range.min_ip = range.max_ip
441 = ct->master->tuplehash[!exp->dir].tuple.dst.ip;
442 /* hook doesn't matter, but it has to do source manip */
443 ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
445 /* For DST manip, map port here to where it's expected. */
446 range.flags = (IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED);
447 range.min = range.max = exp->saved_proto;
448 range.min_ip = range.max_ip
449 = ct->master->tuplehash[!exp->dir].tuple.src.ip;
450 /* hook doesn't matter, but it has to do destination manip */
451 ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
453 EXPORT_SYMBOL(ip_nat_follow_master);