1 /* ip_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>
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 #include <linux/module.h>
11 #include <linux/gfp.h>
12 #include <linux/kmod.h>
13 #include <linux/types.h>
14 #include <linux/timer.h>
15 #include <linux/skbuff.h>
16 #include <linux/tcp.h>
17 #include <linux/udp.h>
18 #include <net/checksum.h>
20 #include <net/route.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <net/netfilter/nf_conntrack_ecache.h>
26 #include <net/netfilter/nf_conntrack_expect.h>
27 #include <net/netfilter/nf_nat.h>
28 #include <net/netfilter/nf_nat_protocol.h>
29 #include <net/netfilter/nf_nat_core.h>
30 #include <net/netfilter/nf_nat_helper.h>
32 #define DUMP_OFFSET(x) \
33 pr_debug("offset_before=%d, offset_after=%d, correction_pos=%u\n", \
34 x->offset_before, x->offset_after, x->correction_pos);
36 static DEFINE_SPINLOCK(nf_nat_seqofs_lock
);
38 /* Setup TCP sequence correction given this change at this sequence */
40 adjust_tcp_sequence(u32 seq
,
43 enum ip_conntrack_info ctinfo
)
45 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
46 struct nf_conn_nat
*nat
= nfct_nat(ct
);
47 struct nf_nat_seq
*this_way
= &nat
->seq
[dir
];
49 pr_debug("adjust_tcp_sequence: seq = %u, sizediff = %d\n",
52 pr_debug("adjust_tcp_sequence: Seq_offset before: ");
53 DUMP_OFFSET(this_way
);
55 spin_lock_bh(&nf_nat_seqofs_lock
);
57 /* SYN adjust. If it's uninitialized, or this is after last
58 * correction, record it: we don't handle more than one
59 * adjustment in the window, but do deal with common case of a
61 if (this_way
->offset_before
== this_way
->offset_after
||
62 before(this_way
->correction_pos
, seq
)) {
63 this_way
->correction_pos
= seq
;
64 this_way
->offset_before
= this_way
->offset_after
;
65 this_way
->offset_after
+= sizediff
;
67 spin_unlock_bh(&nf_nat_seqofs_lock
);
69 pr_debug("adjust_tcp_sequence: Seq_offset after: ");
70 DUMP_OFFSET(this_way
);
73 /* Get the offset value, for conntrack */
74 s16
nf_nat_get_offset(const struct nf_conn
*ct
,
75 enum ip_conntrack_dir dir
,
78 struct nf_conn_nat
*nat
= nfct_nat(ct
);
79 struct nf_nat_seq
*this_way
;
85 this_way
= &nat
->seq
[dir
];
86 spin_lock_bh(&nf_nat_seqofs_lock
);
87 offset
= after(seq
, this_way
->correction_pos
)
88 ? this_way
->offset_after
: this_way
->offset_before
;
89 spin_unlock_bh(&nf_nat_seqofs_lock
);
93 EXPORT_SYMBOL_GPL(nf_nat_get_offset
);
95 /* Frobs data inside this packet, which is linear. */
96 static void mangle_contents(struct sk_buff
*skb
,
98 unsigned int match_offset
,
99 unsigned int match_len
,
100 const char *rep_buffer
,
101 unsigned int rep_len
)
105 BUG_ON(skb_is_nonlinear(skb
));
106 data
= skb_network_header(skb
) + dataoff
;
108 /* move post-replacement */
109 memmove(data
+ match_offset
+ rep_len
,
110 data
+ match_offset
+ match_len
,
111 skb
->tail
- (skb
->network_header
+ dataoff
+
112 match_offset
+ match_len
));
114 /* insert data from buffer */
115 memcpy(data
+ match_offset
, rep_buffer
, rep_len
);
117 /* update skb info */
118 if (rep_len
> match_len
) {
119 pr_debug("nf_nat_mangle_packet: Extending packet by "
120 "%u from %u bytes\n", rep_len
- match_len
, skb
->len
);
121 skb_put(skb
, rep_len
- match_len
);
123 pr_debug("nf_nat_mangle_packet: Shrinking packet from "
124 "%u from %u bytes\n", match_len
- rep_len
, skb
->len
);
125 __skb_trim(skb
, skb
->len
+ rep_len
- match_len
);
128 /* fix IP hdr checksum information */
129 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
130 ip_send_check(ip_hdr(skb
));
133 /* Unusual, but possible case. */
134 static int enlarge_skb(struct sk_buff
*skb
, unsigned int extra
)
136 if (skb
->len
+ extra
> 65535)
139 if (pskb_expand_head(skb
, 0, extra
- skb_tailroom(skb
), GFP_ATOMIC
))
145 void nf_nat_set_seq_adjust(struct nf_conn
*ct
, enum ip_conntrack_info ctinfo
,
150 set_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
);
151 adjust_tcp_sequence(ntohl(seq
), off
, ct
, ctinfo
);
152 nf_conntrack_event_cache(IPCT_NATSEQADJ
, ct
);
154 EXPORT_SYMBOL_GPL(nf_nat_set_seq_adjust
);
156 void nf_nat_tcp_seq_adjust(struct sk_buff
*skb
, struct nf_conn
*ct
,
159 const struct tcphdr
*th
;
161 if (nf_ct_protonum(ct
) != IPPROTO_TCP
)
164 th
= (struct tcphdr
*)(skb_network_header(skb
)+ ip_hdrlen(skb
));
165 nf_nat_set_seq_adjust(ct
, ctinfo
, th
->seq
, off
);
167 EXPORT_SYMBOL_GPL(nf_nat_tcp_seq_adjust
);
169 static void nf_nat_csum(struct sk_buff
*skb
, const struct iphdr
*iph
, void *data
,
170 int datalen
, __sum16
*check
, int oldlen
)
172 struct rtable
*rt
= skb_rtable(skb
);
174 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
) {
175 if (!(rt
->rt_flags
& RTCF_LOCAL
) &&
176 (!skb
->dev
|| skb
->dev
->features
& NETIF_F_V4_CSUM
)) {
177 skb
->ip_summed
= CHECKSUM_PARTIAL
;
178 skb
->csum_start
= skb_headroom(skb
) +
179 skb_network_offset(skb
) +
181 skb
->csum_offset
= (void *)check
- data
;
182 *check
= ~csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
183 datalen
, iph
->protocol
, 0);
186 *check
= csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
187 datalen
, iph
->protocol
,
188 csum_partial(data
, datalen
,
190 if (iph
->protocol
== IPPROTO_UDP
&& !*check
)
191 *check
= CSUM_MANGLED_0
;
194 inet_proto_csum_replace2(check
, skb
,
195 htons(oldlen
), htons(datalen
), 1);
198 /* Generic function for mangling variable-length address changes inside
199 * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
202 * Takes care about all the nasty sequence number changes, checksumming,
203 * skb enlargement, ...
206 int __nf_nat_mangle_tcp_packet(struct sk_buff
*skb
,
208 enum ip_conntrack_info ctinfo
,
209 unsigned int match_offset
,
210 unsigned int match_len
,
211 const char *rep_buffer
,
212 unsigned int rep_len
, bool adjust
)
218 if (!skb_make_writable(skb
, skb
->len
))
221 if (rep_len
> match_len
&&
222 rep_len
- match_len
> skb_tailroom(skb
) &&
223 !enlarge_skb(skb
, rep_len
- match_len
))
226 SKB_LINEAR_ASSERT(skb
);
229 tcph
= (void *)iph
+ iph
->ihl
*4;
231 oldlen
= skb
->len
- iph
->ihl
*4;
232 mangle_contents(skb
, iph
->ihl
*4 + tcph
->doff
*4,
233 match_offset
, match_len
, rep_buffer
, rep_len
);
235 datalen
= skb
->len
- iph
->ihl
*4;
236 nf_nat_csum(skb
, iph
, tcph
, datalen
, &tcph
->check
, oldlen
);
238 if (adjust
&& rep_len
!= match_len
)
239 nf_nat_set_seq_adjust(ct
, ctinfo
, tcph
->seq
,
240 (int)rep_len
- (int)match_len
);
244 EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet
);
246 /* Generic function for mangling variable-length address changes inside
247 * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
248 * command in the Amanda protocol)
250 * Takes care about all the nasty sequence number changes, checksumming,
251 * skb enlargement, ...
253 * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
254 * should be fairly easy to do.
257 nf_nat_mangle_udp_packet(struct sk_buff
*skb
,
259 enum ip_conntrack_info ctinfo
,
260 unsigned int match_offset
,
261 unsigned int match_len
,
262 const char *rep_buffer
,
263 unsigned int rep_len
)
269 if (!skb_make_writable(skb
, skb
->len
))
272 if (rep_len
> match_len
&&
273 rep_len
- match_len
> skb_tailroom(skb
) &&
274 !enlarge_skb(skb
, rep_len
- match_len
))
278 udph
= (void *)iph
+ iph
->ihl
*4;
280 oldlen
= skb
->len
- iph
->ihl
*4;
281 mangle_contents(skb
, iph
->ihl
*4 + sizeof(*udph
),
282 match_offset
, match_len
, rep_buffer
, rep_len
);
284 /* update the length of the UDP packet */
285 datalen
= skb
->len
- iph
->ihl
*4;
286 udph
->len
= htons(datalen
);
288 /* fix udp checksum if udp checksum was previously calculated */
289 if (!udph
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
)
292 nf_nat_csum(skb
, iph
, udph
, datalen
, &udph
->check
, oldlen
);
296 EXPORT_SYMBOL(nf_nat_mangle_udp_packet
);
298 /* Adjust one found SACK option including checksum correction */
300 sack_adjust(struct sk_buff
*skb
,
302 unsigned int sackoff
,
303 unsigned int sackend
,
304 struct nf_nat_seq
*natseq
)
306 while (sackoff
< sackend
) {
307 struct tcp_sack_block_wire
*sack
;
308 __be32 new_start_seq
, new_end_seq
;
310 sack
= (void *)skb
->data
+ sackoff
;
311 if (after(ntohl(sack
->start_seq
) - natseq
->offset_before
,
312 natseq
->correction_pos
))
313 new_start_seq
= htonl(ntohl(sack
->start_seq
)
314 - natseq
->offset_after
);
316 new_start_seq
= htonl(ntohl(sack
->start_seq
)
317 - natseq
->offset_before
);
319 if (after(ntohl(sack
->end_seq
) - natseq
->offset_before
,
320 natseq
->correction_pos
))
321 new_end_seq
= htonl(ntohl(sack
->end_seq
)
322 - natseq
->offset_after
);
324 new_end_seq
= htonl(ntohl(sack
->end_seq
)
325 - natseq
->offset_before
);
327 pr_debug("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n",
328 ntohl(sack
->start_seq
), new_start_seq
,
329 ntohl(sack
->end_seq
), new_end_seq
);
331 inet_proto_csum_replace4(&tcph
->check
, skb
,
332 sack
->start_seq
, new_start_seq
, 0);
333 inet_proto_csum_replace4(&tcph
->check
, skb
,
334 sack
->end_seq
, new_end_seq
, 0);
335 sack
->start_seq
= new_start_seq
;
336 sack
->end_seq
= new_end_seq
;
337 sackoff
+= sizeof(*sack
);
341 /* TCP SACK sequence number adjustment */
342 static inline unsigned int
343 nf_nat_sack_adjust(struct sk_buff
*skb
,
346 enum ip_conntrack_info ctinfo
)
348 unsigned int dir
, optoff
, optend
;
349 struct nf_conn_nat
*nat
= nfct_nat(ct
);
351 optoff
= ip_hdrlen(skb
) + sizeof(struct tcphdr
);
352 optend
= ip_hdrlen(skb
) + tcph
->doff
* 4;
354 if (!skb_make_writable(skb
, optend
))
357 dir
= CTINFO2DIR(ctinfo
);
359 while (optoff
< optend
) {
360 /* Usually: option, length. */
361 unsigned char *op
= skb
->data
+ optoff
;
370 /* no partial options */
371 if (optoff
+ 1 == optend
||
372 optoff
+ op
[1] > optend
||
375 if (op
[0] == TCPOPT_SACK
&&
376 op
[1] >= 2+TCPOLEN_SACK_PERBLOCK
&&
377 ((op
[1] - 2) % TCPOLEN_SACK_PERBLOCK
) == 0)
378 sack_adjust(skb
, tcph
, optoff
+2,
379 optoff
+op
[1], &nat
->seq
[!dir
]);
386 /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */
388 nf_nat_seq_adjust(struct sk_buff
*skb
,
390 enum ip_conntrack_info ctinfo
)
394 __be32 newseq
, newack
;
396 struct nf_conn_nat
*nat
= nfct_nat(ct
);
397 struct nf_nat_seq
*this_way
, *other_way
;
399 dir
= CTINFO2DIR(ctinfo
);
401 this_way
= &nat
->seq
[dir
];
402 other_way
= &nat
->seq
[!dir
];
404 if (!skb_make_writable(skb
, ip_hdrlen(skb
) + sizeof(*tcph
)))
407 tcph
= (void *)skb
->data
+ ip_hdrlen(skb
);
408 if (after(ntohl(tcph
->seq
), this_way
->correction_pos
))
409 seqoff
= this_way
->offset_after
;
411 seqoff
= this_way
->offset_before
;
413 if (after(ntohl(tcph
->ack_seq
) - other_way
->offset_before
,
414 other_way
->correction_pos
))
415 ackoff
= other_way
->offset_after
;
417 ackoff
= other_way
->offset_before
;
419 newseq
= htonl(ntohl(tcph
->seq
) + seqoff
);
420 newack
= htonl(ntohl(tcph
->ack_seq
) - ackoff
);
422 inet_proto_csum_replace4(&tcph
->check
, skb
, tcph
->seq
, newseq
, 0);
423 inet_proto_csum_replace4(&tcph
->check
, skb
, tcph
->ack_seq
, newack
, 0);
425 pr_debug("Adjusting sequence number from %u->%u, ack from %u->%u\n",
426 ntohl(tcph
->seq
), ntohl(newseq
), ntohl(tcph
->ack_seq
),
430 tcph
->ack_seq
= newack
;
432 return nf_nat_sack_adjust(skb
, tcph
, ct
, ctinfo
);
435 /* Setup NAT on this expected conntrack so it follows master. */
436 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
437 void nf_nat_follow_master(struct nf_conn
*ct
,
438 struct nf_conntrack_expect
*exp
)
440 struct nf_nat_ipv4_range range
;
442 /* This must be a fresh one. */
443 BUG_ON(ct
->status
& IPS_NAT_DONE_MASK
);
445 /* Change src to where master sends to */
446 range
.flags
= NF_NAT_RANGE_MAP_IPS
;
447 range
.min_ip
= range
.max_ip
448 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.dst
.u3
.ip
;
449 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
451 /* For DST manip, map port here to where it's expected. */
452 range
.flags
= (NF_NAT_RANGE_MAP_IPS
| NF_NAT_RANGE_PROTO_SPECIFIED
);
453 range
.min
= range
.max
= exp
->saved_proto
;
454 range
.min_ip
= range
.max_ip
455 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.src
.u3
.ip
;
456 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
458 EXPORT_SYMBOL(nf_nat_follow_master
);