]>
Commit | Line | Data |
---|---|---|
c7232c99 | 1 | /* nf_nat_helper.c - generic support functions for NAT helpers |
5b1158e9 JK |
2 | * |
3 | * (C) 2000-2002 Harald Welte <laforge@netfilter.org> | |
4 | * (C) 2003-2006 Netfilter Core Team <coreteam@netfilter.org> | |
f229f6ce | 5 | * (C) 2007-2012 Patrick McHardy <kaber@trash.net> |
5b1158e9 JK |
6 | * |
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. | |
10 | */ | |
11 | #include <linux/module.h> | |
5a0e3ad6 | 12 | #include <linux/gfp.h> |
5b1158e9 | 13 | #include <linux/types.h> |
5b1158e9 JK |
14 | #include <linux/skbuff.h> |
15 | #include <linux/tcp.h> | |
16 | #include <linux/udp.h> | |
5b1158e9 JK |
17 | #include <net/tcp.h> |
18 | ||
5b1158e9 JK |
19 | #include <net/netfilter/nf_conntrack.h> |
20 | #include <net/netfilter/nf_conntrack_helper.h> | |
13eae15a | 21 | #include <net/netfilter/nf_conntrack_ecache.h> |
5b1158e9 | 22 | #include <net/netfilter/nf_conntrack_expect.h> |
41d73ec0 | 23 | #include <net/netfilter/nf_conntrack_seqadj.h> |
5b1158e9 | 24 | #include <net/netfilter/nf_nat.h> |
c7232c99 PM |
25 | #include <net/netfilter/nf_nat_l3proto.h> |
26 | #include <net/netfilter/nf_nat_l4proto.h> | |
5b1158e9 JK |
27 | #include <net/netfilter/nf_nat_core.h> |
28 | #include <net/netfilter/nf_nat_helper.h> | |
29 | ||
5b1158e9 JK |
30 | /* Frobs data inside this packet, which is linear. */ |
31 | static void mangle_contents(struct sk_buff *skb, | |
32 | unsigned int dataoff, | |
33 | unsigned int match_offset, | |
34 | unsigned int match_len, | |
35 | const char *rep_buffer, | |
36 | unsigned int rep_len) | |
37 | { | |
38 | unsigned char *data; | |
39 | ||
40 | BUG_ON(skb_is_nonlinear(skb)); | |
eddc9ec5 | 41 | data = skb_network_header(skb) + dataoff; |
5b1158e9 JK |
42 | |
43 | /* move post-replacement */ | |
44 | memmove(data + match_offset + rep_len, | |
45 | data + match_offset + match_len, | |
938177e9 | 46 | skb_tail_pointer(skb) - (skb_network_header(skb) + dataoff + |
27a884dc | 47 | match_offset + match_len)); |
5b1158e9 JK |
48 | |
49 | /* insert data from buffer */ | |
50 | memcpy(data + match_offset, rep_buffer, rep_len); | |
51 | ||
52 | /* update skb info */ | |
53 | if (rep_len > match_len) { | |
0d53778e PM |
54 | pr_debug("nf_nat_mangle_packet: Extending packet by " |
55 | "%u from %u bytes\n", rep_len - match_len, skb->len); | |
5b1158e9 JK |
56 | skb_put(skb, rep_len - match_len); |
57 | } else { | |
0d53778e PM |
58 | pr_debug("nf_nat_mangle_packet: Shrinking packet from " |
59 | "%u from %u bytes\n", match_len - rep_len, skb->len); | |
5b1158e9 JK |
60 | __skb_trim(skb, skb->len + rep_len - match_len); |
61 | } | |
62 | ||
cb9c6836 | 63 | if (nf_ct_l3num((struct nf_conn *)skb_nfct(skb)) == NFPROTO_IPV4) { |
c7232c99 PM |
64 | /* fix IP hdr checksum information */ |
65 | ip_hdr(skb)->tot_len = htons(skb->len); | |
66 | ip_send_check(ip_hdr(skb)); | |
67 | } else | |
68 | ipv6_hdr(skb)->payload_len = | |
69 | htons(skb->len - sizeof(struct ipv6hdr)); | |
5b1158e9 JK |
70 | } |
71 | ||
72 | /* Unusual, but possible case. */ | |
cba81cc4 | 73 | static bool enlarge_skb(struct sk_buff *skb, unsigned int extra) |
5b1158e9 | 74 | { |
3db05fea | 75 | if (skb->len + extra > 65535) |
cba81cc4 | 76 | return false; |
5b1158e9 | 77 | |
3db05fea | 78 | if (pskb_expand_head(skb, 0, extra - skb_tailroom(skb), GFP_ATOMIC)) |
cba81cc4 | 79 | return false; |
5b1158e9 | 80 | |
cba81cc4 | 81 | return true; |
5b1158e9 JK |
82 | } |
83 | ||
84 | /* Generic function for mangling variable-length address changes inside | |
85 | * NATed TCP connections (like the PORT XXX,XXX,XXX,XXX,XXX,XXX | |
86 | * command in FTP). | |
87 | * | |
88 | * Takes care about all the nasty sequence number changes, checksumming, | |
89 | * skb enlargement, ... | |
90 | * | |
91 | * */ | |
cba81cc4 GF |
92 | bool __nf_nat_mangle_tcp_packet(struct sk_buff *skb, |
93 | struct nf_conn *ct, | |
94 | enum ip_conntrack_info ctinfo, | |
95 | unsigned int protoff, | |
96 | unsigned int match_offset, | |
97 | unsigned int match_len, | |
98 | const char *rep_buffer, | |
99 | unsigned int rep_len, bool adjust) | |
5b1158e9 | 100 | { |
c7232c99 | 101 | const struct nf_nat_l3proto *l3proto; |
5b1158e9 JK |
102 | struct tcphdr *tcph; |
103 | int oldlen, datalen; | |
104 | ||
3db05fea | 105 | if (!skb_make_writable(skb, skb->len)) |
cba81cc4 | 106 | return false; |
5b1158e9 JK |
107 | |
108 | if (rep_len > match_len && | |
3db05fea HX |
109 | rep_len - match_len > skb_tailroom(skb) && |
110 | !enlarge_skb(skb, rep_len - match_len)) | |
cba81cc4 | 111 | return false; |
5b1158e9 | 112 | |
3db05fea | 113 | SKB_LINEAR_ASSERT(skb); |
5b1158e9 | 114 | |
c7232c99 | 115 | tcph = (void *)skb->data + protoff; |
5b1158e9 | 116 | |
c7232c99 PM |
117 | oldlen = skb->len - protoff; |
118 | mangle_contents(skb, protoff + tcph->doff*4, | |
5b1158e9 JK |
119 | match_offset, match_len, rep_buffer, rep_len); |
120 | ||
c7232c99 PM |
121 | datalen = skb->len - protoff; |
122 | ||
123 | l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct)); | |
124 | l3proto->csum_recalc(skb, IPPROTO_TCP, tcph, &tcph->check, | |
125 | datalen, oldlen); | |
5b1158e9 | 126 | |
010c0b9f | 127 | if (adjust && rep_len != match_len) |
41d73ec0 PM |
128 | nf_ct_seqadj_set(ct, ctinfo, tcph->seq, |
129 | (int)rep_len - (int)match_len); | |
010c0b9f | 130 | |
cba81cc4 | 131 | return true; |
5b1158e9 | 132 | } |
010c0b9f | 133 | EXPORT_SYMBOL(__nf_nat_mangle_tcp_packet); |
5b1158e9 JK |
134 | |
135 | /* Generic function for mangling variable-length address changes inside | |
136 | * NATed UDP connections (like the CONNECT DATA XXXXX MESG XXXXX INDEX XXXXX | |
137 | * command in the Amanda protocol) | |
138 | * | |
139 | * Takes care about all the nasty sequence number changes, checksumming, | |
140 | * skb enlargement, ... | |
141 | * | |
142 | * XXX - This function could be merged with nf_nat_mangle_tcp_packet which | |
143 | * should be fairly easy to do. | |
144 | */ | |
cba81cc4 | 145 | bool |
3db05fea | 146 | nf_nat_mangle_udp_packet(struct sk_buff *skb, |
5b1158e9 JK |
147 | struct nf_conn *ct, |
148 | enum ip_conntrack_info ctinfo, | |
051966c0 | 149 | unsigned int protoff, |
5b1158e9 JK |
150 | unsigned int match_offset, |
151 | unsigned int match_len, | |
152 | const char *rep_buffer, | |
153 | unsigned int rep_len) | |
154 | { | |
c7232c99 | 155 | const struct nf_nat_l3proto *l3proto; |
5b1158e9 JK |
156 | struct udphdr *udph; |
157 | int datalen, oldlen; | |
158 | ||
3db05fea | 159 | if (!skb_make_writable(skb, skb->len)) |
cba81cc4 | 160 | return false; |
5b1158e9 JK |
161 | |
162 | if (rep_len > match_len && | |
3db05fea HX |
163 | rep_len - match_len > skb_tailroom(skb) && |
164 | !enlarge_skb(skb, rep_len - match_len)) | |
cba81cc4 | 165 | return false; |
5b1158e9 | 166 | |
c7232c99 | 167 | udph = (void *)skb->data + protoff; |
5b1158e9 | 168 | |
c7232c99 PM |
169 | oldlen = skb->len - protoff; |
170 | mangle_contents(skb, protoff + sizeof(*udph), | |
5b1158e9 JK |
171 | match_offset, match_len, rep_buffer, rep_len); |
172 | ||
173 | /* update the length of the UDP packet */ | |
c7232c99 | 174 | datalen = skb->len - protoff; |
5b1158e9 JK |
175 | udph->len = htons(datalen); |
176 | ||
177 | /* fix udp checksum if udp checksum was previously calculated */ | |
3db05fea | 178 | if (!udph->check && skb->ip_summed != CHECKSUM_PARTIAL) |
cba81cc4 | 179 | return true; |
5b1158e9 | 180 | |
c7232c99 PM |
181 | l3proto = __nf_nat_l3proto_find(nf_ct_l3num(ct)); |
182 | l3proto->csum_recalc(skb, IPPROTO_UDP, udph, &udph->check, | |
183 | datalen, oldlen); | |
5b1158e9 | 184 | |
cba81cc4 | 185 | return true; |
5b1158e9 JK |
186 | } |
187 | EXPORT_SYMBOL(nf_nat_mangle_udp_packet); | |
188 | ||
5b1158e9 JK |
189 | /* Setup NAT on this expected conntrack so it follows master. */ |
190 | /* If we fail to get a free NAT slot, we'll get dropped on confirm */ | |
191 | void nf_nat_follow_master(struct nf_conn *ct, | |
192 | struct nf_conntrack_expect *exp) | |
193 | { | |
c7232c99 | 194 | struct nf_nat_range range; |
5b1158e9 JK |
195 | |
196 | /* This must be a fresh one. */ | |
197 | BUG_ON(ct->status & IPS_NAT_DONE_MASK); | |
198 | ||
199 | /* Change src to where master sends to */ | |
cbc9f2f4 | 200 | range.flags = NF_NAT_RANGE_MAP_IPS; |
c7232c99 PM |
201 | range.min_addr = range.max_addr |
202 | = ct->master->tuplehash[!exp->dir].tuple.dst.u3; | |
cbc9f2f4 | 203 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_SRC); |
5b1158e9 JK |
204 | |
205 | /* For DST manip, map port here to where it's expected. */ | |
cbc9f2f4 | 206 | range.flags = (NF_NAT_RANGE_MAP_IPS | NF_NAT_RANGE_PROTO_SPECIFIED); |
c7232c99 PM |
207 | range.min_proto = range.max_proto = exp->saved_proto; |
208 | range.min_addr = range.max_addr | |
209 | = ct->master->tuplehash[!exp->dir].tuple.src.u3; | |
cbc9f2f4 | 210 | nf_nat_setup_info(ct, &range, NF_NAT_MANIP_DST); |
5b1158e9 JK |
211 | } |
212 | EXPORT_SYMBOL(nf_nat_follow_master); |