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_helper.h>
27 /* Frobs data inside this packet, which is linear. */
28 static void mangle_contents(struct sk_buff
*skb
,
30 unsigned int match_offset
,
31 unsigned int match_len
,
32 const char *rep_buffer
,
37 SKB_LINEAR_ASSERT(skb
);
38 data
= skb_network_header(skb
) + dataoff
;
40 /* move post-replacement */
41 memmove(data
+ match_offset
+ rep_len
,
42 data
+ match_offset
+ match_len
,
43 skb_tail_pointer(skb
) - (skb_network_header(skb
) + dataoff
+
44 match_offset
+ match_len
));
46 /* insert data from buffer */
47 memcpy(data
+ match_offset
, rep_buffer
, rep_len
);
50 if (rep_len
> match_len
) {
51 pr_debug("nf_nat_mangle_packet: Extending packet by "
52 "%u from %u bytes\n", rep_len
- match_len
, skb
->len
);
53 skb_put(skb
, rep_len
- match_len
);
55 pr_debug("nf_nat_mangle_packet: Shrinking packet from "
56 "%u from %u bytes\n", match_len
- rep_len
, skb
->len
);
57 __skb_trim(skb
, skb
->len
+ rep_len
- match_len
);
60 if (nf_ct_l3num((struct nf_conn
*)skb_nfct(skb
)) == NFPROTO_IPV4
) {
61 /* fix IP hdr checksum information */
62 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
63 ip_send_check(ip_hdr(skb
));
65 ipv6_hdr(skb
)->payload_len
=
66 htons(skb
->len
- sizeof(struct ipv6hdr
));
69 /* Unusual, but possible case. */
70 static bool enlarge_skb(struct sk_buff
*skb
, unsigned int extra
)
72 if (skb
->len
+ extra
> 65535)
75 if (pskb_expand_head(skb
, 0, extra
- skb_tailroom(skb
), GFP_ATOMIC
))
81 /* Generic function for mangling variable-length address changes inside
82 * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX
85 * Takes care about all the nasty sequence number changes, checksumming,
86 * skb enlargement, ...
89 bool __nf_nat_mangle_tcp_packet(struct sk_buff
*skb
,
91 enum ip_conntrack_info ctinfo
,
93 unsigned int match_offset
,
94 unsigned int match_len
,
95 const char *rep_buffer
,
96 unsigned int rep_len
, bool adjust
)
101 if (!skb_make_writable(skb
, skb
->len
))
104 if (rep_len
> match_len
&&
105 rep_len
- match_len
> skb_tailroom(skb
) &&
106 !enlarge_skb(skb
, rep_len
- match_len
))
109 tcph
= (void *)skb
->data
+ protoff
;
111 oldlen
= skb
->len
- protoff
;
112 mangle_contents(skb
, protoff
+ tcph
->doff
*4,
113 match_offset
, match_len
, rep_buffer
, rep_len
);
115 datalen
= skb
->len
- protoff
;
117 nf_nat_csum_recalc(skb
, nf_ct_l3num(ct
), IPPROTO_TCP
,
118 tcph
, &tcph
->check
, datalen
, oldlen
);
120 if (adjust
&& rep_len
!= match_len
)
121 nf_ct_seqadj_set(ct
, ctinfo
, tcph
->seq
,
122 (int)rep_len
- (int)match_len
);
126 EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet
);
128 /* Generic function for mangling variable-length address changes inside
129 * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX
130 * command in the Amanda protocol)
132 * Takes care about all the nasty sequence number changes, checksumming,
133 * skb enlargement, ...
135 * XXX - This function could be merged with nf_nat_mangle_tcp_packet which
136 * should be fairly easy to do.
139 nf_nat_mangle_udp_packet(struct sk_buff
*skb
,
141 enum ip_conntrack_info ctinfo
,
142 unsigned int protoff
,
143 unsigned int match_offset
,
144 unsigned int match_len
,
145 const char *rep_buffer
,
146 unsigned int rep_len
)
151 if (!skb_make_writable(skb
, skb
->len
))
154 if (rep_len
> match_len
&&
155 rep_len
- match_len
> skb_tailroom(skb
) &&
156 !enlarge_skb(skb
, rep_len
- match_len
))
159 udph
= (void *)skb
->data
+ protoff
;
161 oldlen
= skb
->len
- protoff
;
162 mangle_contents(skb
, protoff
+ sizeof(*udph
),
163 match_offset
, match_len
, rep_buffer
, rep_len
);
165 /* update the length of the UDP packet */
166 datalen
= skb
->len
- protoff
;
167 udph
->len
= htons(datalen
);
169 /* fix udp checksum if udp checksum was previously calculated */
170 if (!udph
->check
&& skb
->ip_summed
!= CHECKSUM_PARTIAL
)
173 nf_nat_csum_recalc(skb
, nf_ct_l3num(ct
), IPPROTO_TCP
,
174 udph
, &udph
->check
, datalen
, oldlen
);
178 EXPORT_SYMBOL(nf_nat_mangle_udp_packet
);
180 /* Setup NAT on this expected conntrack so it follows master. */
181 /* If we fail to get a free NAT slot, we'll get dropped on confirm */
182 void nf_nat_follow_master(struct nf_conn
*ct
,
183 struct nf_conntrack_expect
*exp
)
185 struct nf_nat_range2 range
;
187 /* This must be a fresh one. */
188 BUG_ON(ct
->status
& IPS_NAT_DONE_MASK
);
190 /* Change src to where master sends to */
191 range
.flags
= NF_NAT_RANGE_MAP_IPS
;
192 range
.min_addr
= range
.max_addr
193 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.dst
.u3
;
194 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_SRC
);
196 /* For DST manip, map port here to where it's expected. */
197 range
.flags
= (NF_NAT_RANGE_MAP_IPS
| NF_NAT_RANGE_PROTO_SPECIFIED
);
198 range
.min_proto
= range
.max_proto
= exp
->saved_proto
;
199 range
.min_addr
= range
.max_addr
200 = ct
->master
->tuplehash
[!exp
->dir
].tuple
.src
.u3
;
201 nf_nat_setup_info(ct
, &range
, NF_NAT_MANIP_DST
);
203 EXPORT_SYMBOL(nf_nat_follow_master
);