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_nat.h>
24 #include <net/netfilter/nf_nat_l3proto.h>
25 #include <net/netfilter/nf_nat_l4proto.h>
26 #include <net/netfilter/nf_nat_core.h>
27 #include <net/netfilter/nf_nat_helper.h>
29 #define DUMP_OFFSET(x) \
30 pr_debug("offset_before=%d, offset_after=%d, correction_pos=%u\n", \
31 x->offset_before, x->offset_after, x->correction_pos);
33 static DEFINE_SPINLOCK(nf_nat_seqofs_lock
);
35 /* Setup TCP sequence correction given this change at this sequence */
37 adjust_tcp_sequence(u32 seq
,
40 enum ip_conntrack_info ctinfo
)
42 enum ip_conntrack_dir dir
= CTINFO2DIR(ctinfo
);
43 struct nf_conn_nat
*nat
= nfct_nat(ct
);
44 struct nf_nat_seq
*this_way
= &nat
->seq
[dir
];
46 pr_debug("adjust_tcp_sequence: seq = %u, sizediff = %d\n",
49 pr_debug("adjust_tcp_sequence: Seq_offset before: ");
50 DUMP_OFFSET(this_way
);
52 spin_lock_bh(&nf_nat_seqofs_lock
);
54 /* SYN adjust. If it's uninitialized, or this is after last
55 * correction, record it: we don't handle more than one
56 * adjustment in the window, but do deal with common case of a
58 if (this_way
->offset_before
== this_way
->offset_after
||
59 before(this_way
->correction_pos
, seq
)) {
60 this_way
->correction_pos
= seq
;
61 this_way
->offset_before
= this_way
->offset_after
;
62 this_way
->offset_after
+= sizediff
;
64 spin_unlock_bh(&nf_nat_seqofs_lock
);
66 pr_debug("adjust_tcp_sequence: Seq_offset after: ");
67 DUMP_OFFSET(this_way
);
70 /* Get the offset value, for conntrack */
71 s16
nf_nat_get_offset(const struct nf_conn
*ct
,
72 enum ip_conntrack_dir dir
,
75 struct nf_conn_nat
*nat
= nfct_nat(ct
);
76 struct nf_nat_seq
*this_way
;
82 this_way
= &nat
->seq
[dir
];
83 spin_lock_bh(&nf_nat_seqofs_lock
);
84 offset
= after(seq
, this_way
->correction_pos
)
85 ? this_way
->offset_after
: this_way
->offset_before
;
86 spin_unlock_bh(&nf_nat_seqofs_lock
);
91 /* Frobs data inside this packet, which is linear. */
92 static void mangle_contents(struct sk_buff
*skb
,
94 unsigned int match_offset
,
95 unsigned int match_len
,
96 const char *rep_buffer
,
101 BUG_ON(skb_is_nonlinear(skb
));
102 data
= skb_network_header(skb
) + dataoff
;
104 /* move post-replacement */
105 memmove(data
+ match_offset
+ rep_len
,
106 data
+ match_offset
+ match_len
,
107 skb_tail_pointer(skb
) - (skb_network_header(skb
) + dataoff
+
108 match_offset
+ match_len
));
110 /* insert data from buffer */
111 memcpy(data
+ match_offset
, rep_buffer
, rep_len
);
113 /* update skb info */
114 if (rep_len
> match_len
) {
115 pr_debug("nf_nat_mangle_packet: Extending packet by "
116 "%u from %u bytes\n", rep_len
- match_len
, skb
->len
);
117 skb_put(skb
, rep_len
- match_len
);
119 pr_debug("nf_nat_mangle_packet: Shrinking packet from "
120 "%u from %u bytes\n", match_len
- rep_len
, skb
->len
);
121 __skb_trim(skb
, skb
->len
+ rep_len
- match_len
);
124 if (nf_ct_l3num((struct nf_conn
*)skb
->nfct
) == NFPROTO_IPV4
) {
125 /* fix IP hdr checksum information */
126 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
127 ip_send_check(ip_hdr(skb
));
129 ipv6_hdr(skb
)->payload_len
=
130 htons(skb
->len
- sizeof(struct ipv6hdr
));
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 /* Generic function for mangling variable-length address changes inside
170 * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
173 * Takes care about all the nasty sequence number changes, checksumming,
174 * skb enlargement, ...
177 int __nf_nat_mangle_tcp_packet(struct sk_buff
*skb
,
179 enum ip_conntrack_info ctinfo
,
180 unsigned int protoff
,
181 unsigned int match_offset
,
182 unsigned int match_len
,
183 const char *rep_buffer
,
184 unsigned int rep_len
, bool adjust
)
186 const struct nf_nat_l3proto
*l3proto
;
190 if (!skb_make_writable(skb
, skb
->len
))
193 if (rep_len
> match_len
&&
194 rep_len
- match_len
> skb_tailroom(skb
) &&
195 !enlarge_skb(skb
, rep_len
- match_len
))
198 SKB_LINEAR_ASSERT(skb
);
200 tcph
= (void *)skb
->data
+ protoff
;
202 oldlen
= skb
->len
- protoff
;
203 mangle_contents(skb
, protoff
+ tcph
->doff
*4,
204 match_offset
, match_len
, rep_buffer
, rep_len
);
206 datalen
= skb
->len
- protoff
;
208 l3proto
= __nf_nat_l3proto_find(nf_ct_l3num(ct
));
209 l3proto
->csum_recalc(skb
, IPPROTO_TCP
, tcph
, &tcph
->check
,
212 if (adjust
&& rep_len
!= match_len
)
213 nf_nat_set_seq_adjust(ct
, ctinfo
, tcph
->seq
,
214 (int)rep_len
- (int)match_len
);
218 EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet
);
220 /* Generic function for mangling variable-length address changes inside
221 * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
222 * command in the Amanda protocol)
224 * Takes care about all the nasty sequence number changes, checksumming,
225 * skb enlargement, ...
227 * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
228 * should be fairly easy to do.
231 nf_nat_mangle_udp_packet(struct sk_buff
*skb
,
233 enum ip_conntrack_info ctinfo
,
234 unsigned int protoff
,
235 unsigned int match_offset
,
236 unsigned int match_len
,
237 const char *rep_buffer
,
238 unsigned int rep_len
)
240 const struct nf_nat_l3proto
*l3proto
;
244 if (!skb_make_writable(skb
, skb
->len
))
247 if (rep_len
> match_len
&&
248 rep_len
- match_len
> skb_tailroom(skb
) &&
249 !enlarge_skb(skb
, rep_len
- match_len
))
252 udph
= (void *)skb
->data
+ protoff
;
254 oldlen
= skb
->len
- protoff
;
255 mangle_contents(skb
, protoff
+ sizeof(*udph
),
256 match_offset
, match_len
, rep_buffer
, rep_len
);
258 /* update the length of the UDP packet */
259 datalen
= skb
->len
- protoff
;
260 udph
->len
= htons(datalen
);
262 /* fix udp checksum if udp checksum was previously calculated */
263 if (!udph
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
)
266 l3proto
= __nf_nat_l3proto_find(nf_ct_l3num(ct
));
267 l3proto
->csum_recalc(skb
, IPPROTO_UDP
, udph
, &udph
->check
,
272 EXPORT_SYMBOL(nf_nat_mangle_udp_packet
);
274 /* Adjust one found SACK option including checksum correction */
276 sack_adjust(struct sk_buff
*skb
,
278 unsigned int sackoff
,
279 unsigned int sackend
,
280 struct nf_nat_seq
*natseq
)
282 while (sackoff
< sackend
) {
283 struct tcp_sack_block_wire
*sack
;
284 __be32 new_start_seq
, new_end_seq
;
286 sack
= (void *)skb
->data
+ sackoff
;
287 if (after(ntohl(sack
->start_seq
) - natseq
->offset_before
,
288 natseq
->correction_pos
))
289 new_start_seq
= htonl(ntohl(sack
->start_seq
)
290 - natseq
->offset_after
);
292 new_start_seq
= htonl(ntohl(sack
->start_seq
)
293 - natseq
->offset_before
);
295 if (after(ntohl(sack
->end_seq
) - natseq
->offset_before
,
296 natseq
->correction_pos
))
297 new_end_seq
= htonl(ntohl(sack
->end_seq
)
298 - natseq
->offset_after
);
300 new_end_seq
= htonl(ntohl(sack
->end_seq
)
301 - natseq
->offset_before
);
303 pr_debug("sack_adjust: start_seq: %d->%d, end_seq: %d->%d\n",
304 ntohl(sack
->start_seq
), new_start_seq
,
305 ntohl(sack
->end_seq
), new_end_seq
);
307 inet_proto_csum_replace4(&tcph
->check
, skb
,
308 sack
->start_seq
, new_start_seq
, 0);
309 inet_proto_csum_replace4(&tcph
->check
, skb
,
310 sack
->end_seq
, new_end_seq
, 0);
311 sack
->start_seq
= new_start_seq
;
312 sack
->end_seq
= new_end_seq
;
313 sackoff
+= sizeof(*sack
);
317 /* TCP SACK sequence number adjustment */
318 static inline unsigned int
319 nf_nat_sack_adjust(struct sk_buff
*skb
,
320 unsigned int protoff
,
323 enum ip_conntrack_info ctinfo
)
325 unsigned int dir
, optoff
, optend
;
326 struct nf_conn_nat
*nat
= nfct_nat(ct
);
328 optoff
= protoff
+ sizeof(struct tcphdr
);
329 optend
= protoff
+ tcph
->doff
* 4;
331 if (!skb_make_writable(skb
, optend
))
334 dir
= CTINFO2DIR(ctinfo
);
336 while (optoff
< optend
) {
337 /* Usually: option, length. */
338 unsigned char *op
= skb
->data
+ optoff
;
347 /* no partial options */
348 if (optoff
+ 1 == optend
||
349 optoff
+ op
[1] > optend
||
352 if (op
[0] == TCPOPT_SACK
&&
353 op
[1] >= 2+TCPOLEN_SACK_PERBLOCK
&&
354 ((op
[1] - 2) % TCPOLEN_SACK_PERBLOCK
) == 0)
355 sack_adjust(skb
, tcph
, optoff
+2,
356 optoff
+op
[1], &nat
->seq
[!dir
]);
363 /* TCP sequence number adjustment. Returns 1 on success, 0 on failure */
365 nf_nat_seq_adjust(struct sk_buff
*skb
,
367 enum ip_conntrack_info ctinfo
,
368 unsigned int protoff
)
372 __be32 newseq
, newack
;
374 struct nf_conn_nat
*nat
= nfct_nat(ct
);
375 struct nf_nat_seq
*this_way
, *other_way
;
377 dir
= CTINFO2DIR(ctinfo
);
379 this_way
= &nat
->seq
[dir
];
380 other_way
= &nat
->seq
[!dir
];
382 if (!skb_make_writable(skb
, protoff
+ sizeof(*tcph
)))
385 tcph
= (void *)skb
->data
+ protoff
;
386 if (after(ntohl(tcph
->seq
), this_way
->correction_pos
))
387 seqoff
= this_way
->offset_after
;
389 seqoff
= this_way
->offset_before
;
391 if (after(ntohl(tcph
->ack_seq
) - other_way
->offset_before
,
392 other_way
->correction_pos
))
393 ackoff
= other_way
->offset_after
;
395 ackoff
= other_way
->offset_before
;
397 newseq
= htonl(ntohl(tcph
->seq
) + seqoff
);
398 newack
= htonl(ntohl(tcph
->ack_seq
) - ackoff
);
400 inet_proto_csum_replace4(&tcph
->check
, skb
, tcph
->seq
, newseq
, 0);
401 inet_proto_csum_replace4(&tcph
->check
, skb
, tcph
->ack_seq
, newack
, 0);
403 pr_debug("Adjusting sequence number from %u->%u, ack from %u->%u\n",
404 ntohl(tcph
->seq
), ntohl(newseq
), ntohl(tcph
->ack_seq
),
408 tcph
->ack_seq
= newack
;
410 return nf_nat_sack_adjust(skb
, protoff
, tcph
, ct
, ctinfo
);
413 /* Setup NAT on this expected conntrack so it follows master. */
414 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
415 void nf_nat_follow_master(struct nf_conn
*ct
,
416 struct nf_conntrack_expect
*exp
)
418 struct nf_nat_range range
;
420 /* This must be a fresh one. */
421 BUG_ON(ct
->status
& IPS_NAT_DONE_MASK
);
423 /* Change src to where master sends to */
424 range
.flags
= NF_NAT_RANGE_MAP_IPS
;
425 range
.min_addr
= range
.max_addr
426 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.dst
.u3
;
427 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
429 /* For DST manip, map port here to where it's expected. */
430 range
.flags
= (NF_NAT_RANGE_MAP_IPS
| NF_NAT_RANGE_PROTO_SPECIFIED
);
431 range
.min_proto
= range
.max_proto
= exp
->saved_proto
;
432 range
.min_addr
= range
.max_addr
433 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.src
.u3
;
434 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
436 EXPORT_SYMBOL(nf_nat_follow_master
);