npf_alg_icmp.c revision 1.8.4.2 1 /* $NetBSD: npf_alg_icmp.c,v 1.8.4.2 2012/07/16 22:13:26 riz Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This material is based upon work partially supported by The
8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * NPF ALG for ICMP and traceroute translations.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.8.4.2 2012/07/16 22:13:26 riz Exp $");
38
39 #include <sys/param.h>
40 #include <sys/module.h>
41 #include <sys/pool.h>
42
43 #include <netinet/in_systm.h>
44 #include <netinet/in.h>
45 #include <netinet/ip.h>
46 #include <netinet/tcp.h>
47 #include <netinet/udp.h>
48 #include <netinet/ip_icmp.h>
49 #include <net/pfil.h>
50
51 #include "npf_impl.h"
52
53 MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
54
55 /*
56 * Traceroute criteria.
57 *
58 * IANA assigned base port: 33434. However, common practice is to increase
59 * the port, thus monitor [33434-33484] range. Additional filter is TTL < 50.
60 */
61
62 #define TR_BASE_PORT 33434
63 #define TR_PORT_RANGE 33484
64 #define TR_MAX_TTL 50
65
66 static npf_alg_t * alg_icmp __read_mostly;
67
68 static bool npfa_icmp_match(npf_cache_t *, nbuf_t *, void *);
69 static bool npfa_icmp_natin(npf_cache_t *, nbuf_t *, void *);
70 static bool npfa_icmp_session(npf_cache_t *, nbuf_t *, void *);
71
72 /*
73 * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
74 * and module interface.
75 */
76
77 static int
78 npf_alg_icmp_init(void)
79 {
80
81 alg_icmp = npf_alg_register(npfa_icmp_match, NULL,
82 npfa_icmp_natin, npfa_icmp_session);
83 KASSERT(alg_icmp != NULL);
84 return 0;
85 }
86
87 static int
88 npf_alg_icmp_fini(void)
89 {
90
91 KASSERT(alg_icmp != NULL);
92 return npf_alg_unregister(alg_icmp);
93 }
94
95 static int
96 npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
97 {
98
99 switch (cmd) {
100 case MODULE_CMD_INIT:
101 return npf_alg_icmp_init();
102 case MODULE_CMD_FINI:
103 return npf_alg_icmp_fini();
104 case MODULE_CMD_AUTOUNLOAD:
105 return EBUSY;
106 default:
107 return ENOTTY;
108 }
109 return 0;
110 }
111
112 /*
113 * npfa_icmp_match: ALG matching inspector - determines ALG case and
114 * associates ALG with NAT entry.
115 */
116 static bool
117 npfa_icmp_match(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
118 {
119 const int proto = npf_cache_ipproto(npc);
120 struct ip *ip = &npc->npc_ip.v4;
121 in_port_t dport;
122
123 KASSERT(npf_iscached(npc, NPC_IP46));
124 KASSERT(npf_iscached(npc, NPC_LAYER4));
125
126 /* Check for low TTL. */
127 if (ip->ip_ttl > TR_MAX_TTL) {
128 return false;
129 }
130
131 if (proto == IPPROTO_TCP) {
132 struct tcphdr *th = &npc->npc_l4.tcp;
133 dport = ntohs(th->th_dport);
134 } else if (proto == IPPROTO_UDP) {
135 struct udphdr *uh = &npc->npc_l4.udp;
136 dport = ntohs(uh->uh_dport);
137 } else {
138 return false;
139 }
140
141 /* Handle TCP/UDP traceroute - check for port range. */
142 if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
143 return false;
144 }
145
146 /* Associate ALG with translation entry. */
147 npf_nat_t *nt = ntptr;
148 npf_nat_setalg(nt, alg_icmp, 0);
149 return true;
150 }
151
152 /*
153 * npf_icmp_uniqid: retrieve unique identifiers - either ICMP query ID
154 * or TCP/UDP ports of the original packet, which is embedded.
155 */
156 static bool
157 npf_icmp_uniqid(const int type, npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
158 {
159 struct icmp *ic;
160 u_int offby;
161
162 /* Per RFC 792. */
163 switch (type) {
164 case ICMP_UNREACH:
165 case ICMP_SOURCEQUENCH:
166 case ICMP_REDIRECT:
167 case ICMP_TIMXCEED:
168 case ICMP_PARAMPROB:
169 /* Should contain original IP header. */
170 offby = offsetof(struct icmp, icmp_ip);
171 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
172 return false;
173 }
174 /* Fetch into the cache. */
175 if (!npf_fetch_ip(npc, nbuf, n_ptr)) {
176 return false;
177 }
178 switch (npf_cache_ipproto(npc)) {
179 case IPPROTO_TCP:
180 return npf_fetch_tcp(npc, nbuf, n_ptr);
181 case IPPROTO_UDP:
182 return npf_fetch_udp(npc, nbuf, n_ptr);
183 default:
184 return false;
185 }
186 return true;
187
188 case ICMP_ECHOREPLY:
189 case ICMP_ECHO:
190 case ICMP_TSTAMP:
191 case ICMP_TSTAMPREPLY:
192 case ICMP_IREQ:
193 case ICMP_IREQREPLY:
194 /* Should contain ICMP query ID. */
195 ic = &npc->npc_l4.icmp;
196 offby = offsetof(struct icmp, icmp_id);
197 if (nbuf_advfetch(&nbuf, &n_ptr, offby,
198 sizeof(uint16_t), &ic->icmp_id)) {
199 return false;
200 }
201 npc->npc_info |= NPC_ICMP_ID;
202 return true;
203 default:
204 break;
205 }
206 /* No unique IDs. */
207 return false;
208 }
209
210 static void
211 npfa_srcdst_invert(npf_cache_t *npc)
212 {
213 const int proto = npf_cache_ipproto(npc);
214 npf_addr_t *tmp_ip;
215
216 if (proto == IPPROTO_TCP) {
217 struct tcphdr *th = &npc->npc_l4.tcp;
218 in_port_t tmp_sport = th->th_sport;
219 th->th_sport = th->th_dport;
220 th->th_dport = tmp_sport;
221
222 } else if (proto == IPPROTO_UDP) {
223 struct udphdr *uh = &npc->npc_l4.udp;
224 in_port_t tmp_sport = uh->uh_sport;
225 uh->uh_sport = uh->uh_dport;
226 uh->uh_dport = tmp_sport;
227 }
228 tmp_ip = npc->npc_srcip;
229 npc->npc_srcip = npc->npc_dstip;
230 npc->npc_dstip = tmp_ip;
231 }
232
233 /*
234 * npfa_icmp_session: ALG session inspector, returns unique identifiers.
235 */
236 static bool
237 npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, void *keyptr)
238 {
239 npf_cache_t *key = keyptr;
240 KASSERT(key->npc_info == 0);
241
242 /* IP + ICMP? Get unique identifiers from ICMP packet. */
243 if (!npf_iscached(npc, NPC_IP4)) {
244 return false;
245 }
246 if (npf_cache_ipproto(npc) != IPPROTO_ICMP) {
247 return false;
248 }
249 KASSERT(npf_iscached(npc, NPC_ICMP));
250
251 /* Advance to ICMP header. */
252 void *n_ptr = nbuf_dataptr(nbuf);
253 const u_int hlen = npf_cache_hlen(npc);
254
255 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, hlen)) == NULL) {
256 return false;
257 }
258
259 /* Fetch relevant data into the separate ("key") cache. */
260 struct icmp *ic = &npc->npc_l4.icmp;
261 if (!npf_icmp_uniqid(ic->icmp_type, key, nbuf, n_ptr)) {
262 return false;
263 }
264
265 if (npf_iscached(key, NPC_ICMP_ID)) {
266 struct icmp *keyic = &key->npc_l4.icmp;
267
268 /* Copy ICMP ID to the cache and flag it. */
269 npc->npc_info |= NPC_ICMP_ID;
270 ic->icmp_id = keyic->icmp_id;
271
272 /* Note: return False, since key is the original cache. */
273 return false;
274 }
275
276 /*
277 * Embedded IP packet is the original of "forwards" stream.
278 * We should imitate the "backwards" stream for inspection.
279 */
280 KASSERT(npf_iscached(key, NPC_IP46));
281 KASSERT(npf_iscached(key, NPC_LAYER4));
282 npfa_srcdst_invert(key);
283 key->npc_alen = npc->npc_alen;
284
285 return true;
286 }
287
288 /*
289 * npfa_icmp_natin: ALG inbound translation inspector, rewrite IP address
290 * in the IP header, which is embedded in ICMP packet.
291 */
292 static bool
293 npfa_icmp_natin(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
294 {
295 npf_cache_t enpc = { .npc_info = 0 };
296
297 /* XXX: Duplicated work (done at session inspection). */
298 if (!npfa_icmp_session(npc, nbuf, &enpc)) {
299 return false;
300 }
301 /* XXX: Restore inversion (inefficient). */
302 KASSERT(npf_iscached(&enpc, NPC_IP46));
303 KASSERT(npf_iscached(&enpc, NPC_LAYER4));
304 npfa_srcdst_invert(&enpc);
305
306 /*
307 * Save ICMP and embedded IP with TCP/UDP header checksums, retrieve
308 * the original address and port, and calculate ICMP checksum for
309 * embedded packet changes, while data is not rewritten in the cache.
310 */
311 const int proto = npf_cache_ipproto(&enpc);
312 const struct ip *eip = &enpc.npc_ip.v4;
313 const struct icmp * const ic = &npc->npc_l4.icmp;
314 uint16_t cksum = ic->icmp_cksum, ecksum = eip->ip_sum, l4cksum;
315 npf_nat_t *nt = ntptr;
316 npf_addr_t *addr;
317 in_port_t port;
318
319 npf_nat_getorig(nt, &addr, &port);
320
321 if (proto == IPPROTO_TCP) {
322 struct tcphdr *th = &enpc.npc_l4.tcp;
323 cksum = npf_fixup16_cksum(cksum, th->th_sport, port);
324 l4cksum = th->th_sum;
325 } else {
326 struct udphdr *uh = &enpc.npc_l4.udp;
327 cksum = npf_fixup16_cksum(cksum, uh->uh_sport, port);
328 l4cksum = uh->uh_sum;
329 }
330 cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_srcip, addr);
331
332 /*
333 * Save the original pointers to the main IP header and then advance
334 * to the embedded IP header after ICMP header.
335 */
336 void *n_ptr = nbuf_dataptr(nbuf), *cnbuf = nbuf, *cnptr = n_ptr;
337 u_int offby = npf_cache_hlen(npc) + offsetof(struct icmp, icmp_ip);
338
339 if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
340 return false;
341 }
342
343 /*
344 * Rewrite source IP address and port of the embedded IP header,
345 * which represents original packet - therefore passing PFIL_OUT.
346 * Note: checksums are first, since it uses values from the cache.
347 */
348 if (!npf_rwrcksum(&enpc, nbuf, n_ptr, PFIL_OUT, addr, port)) {
349 return false;
350 }
351 if (!npf_rwrip(&enpc, nbuf, n_ptr, PFIL_OUT, addr)) {
352 return false;
353 }
354 if (!npf_rwrport(&enpc, nbuf, n_ptr, PFIL_OUT, port)) {
355 return false;
356 }
357
358 /*
359 * Finish calculation of the ICMP checksum. Update for embedded IP
360 * and TCP/UDP checksum changes. Finally, rewrite ICMP checksum.
361 */
362 if (proto == IPPROTO_TCP) {
363 struct tcphdr *th = &enpc.npc_l4.tcp;
364 cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
365 } else if (l4cksum) {
366 struct udphdr *uh = &enpc.npc_l4.udp;
367 cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
368 }
369 cksum = npf_fixup16_cksum(cksum, ecksum, eip->ip_sum);
370
371 offby = npf_cache_hlen(npc) + offsetof(struct icmp, icmp_cksum);
372 if (nbuf_advstore(&cnbuf, &cnptr, offby, sizeof(uint16_t), &cksum)) {
373 return false;
374 }
375 return true;
376 }
377