npf_alg_icmp.c revision 1.28 1 /* $NetBSD: npf_alg_icmp.c,v 1.28 2018/03/22 08:57:47 maxv 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 #ifdef _KERNEL
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.28 2018/03/22 08:57:47 maxv Exp $");
39
40 #include <sys/param.h>
41 #include <sys/module.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 <netinet/icmp6.h>
50 #include <net/pfil.h>
51 #endif
52
53 #include "npf_impl.h"
54 #include "npf_conn.h"
55
56 MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
57
58 /*
59 * Traceroute criteria.
60 *
61 * IANA assigned base port: 33434. However, common practice is to increase
62 * the port, thus monitor [33434-33484] range. Additional filter is low TTL.
63 */
64
65 #define TR_BASE_PORT 33434
66 #define TR_PORT_RANGE 33484
67 #define TR_MAX_TTL 48
68
69 static npf_alg_t * alg_icmp __read_mostly;
70
71 /*
72 * npfa_icmp_match: matching inspector determines ALG case and associates
73 * our ALG with the NAT entry.
74 */
75 static bool
76 npfa_icmp_match(npf_cache_t *npc, npf_nat_t *nt, int di)
77 {
78 const int proto = npc->npc_proto;
79 const struct ip *ip = npc->npc_ip.v4;
80 in_port_t dport;
81
82 KASSERT(npf_iscached(npc, NPC_IP46));
83 KASSERT(npf_iscached(npc, NPC_LAYER4));
84
85 /* Check for low TTL. Also, we support outbound NAT only. */
86 if (ip->ip_ttl > TR_MAX_TTL || di != PFIL_OUT) {
87 return false;
88 }
89
90 switch (proto) {
91 case IPPROTO_TCP: {
92 const struct tcphdr *th = npc->npc_l4.tcp;
93 dport = ntohs(th->th_dport);
94 break;
95 }
96 case IPPROTO_UDP: {
97 const struct udphdr *uh = npc->npc_l4.udp;
98 dport = ntohs(uh->uh_dport);
99 break;
100 }
101 case IPPROTO_ICMP:
102 case IPPROTO_ICMPV6:
103 /* Just to pass the test below. */
104 dport = TR_BASE_PORT;
105 break;
106 default:
107 return false;
108 }
109
110 /* Handle TCP/UDP traceroute - check for port range. */
111 if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
112 return false;
113 }
114
115 /* Associate ALG with translation entry. */
116 npf_nat_setalg(nt, alg_icmp, 0);
117 return true;
118 }
119
120 /*
121 * npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
122 * ID or TCP/UDP ports of the original packet, which is embedded.
123 *
124 * => Sets hasqid=true if the packet has a Query Id. In this case neither
125 * the nbuf nor npc is touched.
126 */
127
128 static bool
129 npfa_icmp4_inspect(const int type, npf_cache_t *npc, bool *hasqid)
130 {
131 nbuf_t *nbuf = npc->npc_nbuf;
132
133 /* Per RFC 792. */
134 switch (type) {
135 case ICMP_UNREACH:
136 case ICMP_SOURCEQUENCH:
137 case ICMP_REDIRECT:
138 case ICMP_TIMXCEED:
139 case ICMP_PARAMPROB:
140 /* Should contain original IP header. */
141 if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
142 return false;
143 }
144 return (npf_cache_all(npc) & NPC_LAYER4) != 0;
145
146 case ICMP_ECHOREPLY:
147 case ICMP_ECHO:
148 case ICMP_TSTAMP:
149 case ICMP_TSTAMPREPLY:
150 case ICMP_IREQ:
151 case ICMP_IREQREPLY:
152 /* Contains ICMP query ID. */
153 *hasqid = true;
154 return true;
155 default:
156 break;
157 }
158 return false;
159 }
160
161 static bool
162 npfa_icmp6_inspect(const int type, npf_cache_t *npc, bool *hasqid)
163 {
164 nbuf_t *nbuf = npc->npc_nbuf;
165
166 /* Per RFC 4443. */
167 switch (type) {
168 case ICMP6_DST_UNREACH:
169 case ICMP6_PACKET_TOO_BIG:
170 case ICMP6_TIME_EXCEEDED:
171 case ICMP6_PARAM_PROB:
172 /* Should contain original IP header. */
173 if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
174 return false;
175 }
176 return (npf_cache_all(npc) & NPC_LAYER4) != 0;
177
178 case ICMP6_ECHO_REQUEST:
179 case ICMP6_ECHO_REPLY:
180 /* Contains ICMP query ID. */
181 *hasqid = true;
182 return true;
183 default:
184 break;
185 }
186 return false;
187 }
188
189 /*
190 * npfa_icmp_inspect: ALG ICMP inspector.
191 *
192 * => Returns false if there is a problem with the format.
193 */
194 static bool
195 npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
196 {
197 nbuf_t *nbuf = npc->npc_nbuf;
198 bool ret, hasqid = false;
199
200 KASSERT(npf_iscached(npc, NPC_IP46));
201 KASSERT(npf_iscached(npc, NPC_ICMP));
202
203 /* Advance to ICMP header. */
204 nbuf_reset(nbuf);
205 if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
206 return false;
207 }
208 enpc->npc_ctx = npc->npc_ctx;
209 enpc->npc_nbuf = nbuf;
210 enpc->npc_info = 0;
211
212 /*
213 * Inspect the ICMP packet. The relevant data might be in the
214 * embedded packet. Fill the "enpc" cache, if so.
215 */
216 if (npf_iscached(npc, NPC_IP4)) {
217 const struct icmp *ic = npc->npc_l4.icmp;
218 ret = npfa_icmp4_inspect(ic->icmp_type, enpc, &hasqid);
219 } else if (npf_iscached(npc, NPC_IP6)) {
220 const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
221 ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, &hasqid);
222 } else {
223 ret = false;
224 }
225 if (!ret) {
226 return false;
227 }
228
229 /* ICMP ID is the original packet, just indicate it. */
230 if (hasqid) {
231 npc->npc_info |= NPC_ICMP_ID;
232 }
233
234 return true;
235 }
236
237 static npf_conn_t *
238 npfa_icmp_conn(npf_cache_t *npc, int di)
239 {
240 npf_conn_t *conn = NULL;
241 npf_cache_t enpc;
242 bool hasqid = false;
243
244 /* Inspect ICMP packet for an embedded packet. */
245 if (!npf_iscached(npc, NPC_ICMP))
246 return NULL;
247 if (!npfa_icmp_inspect(npc, &enpc))
248 goto out;
249
250 /*
251 * If the ICMP packet had a Query Id, leave now. The packet didn't get
252 * modified, so no need to recache npc.
253 */
254 if (npf_iscached(npc, NPC_ICMP_ID)) {
255 KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
256 return NULL;
257 }
258
259 /*
260 * Invert the identifiers of the embedded packet.
261 * If it is ICMP, then ensure ICMP ID.
262 */
263 union l4 {
264 struct tcphdr th;
265 struct udphdr uh;
266 } l4;
267 bool ret, forw;
268
269 #define SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
270 SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
271
272 switch (enpc.npc_proto) {
273 case IPPROTO_TCP:
274 l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
275 l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
276 enpc.npc_l4.tcp = &l4.th;
277 break;
278 case IPPROTO_UDP:
279 l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
280 l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
281 enpc.npc_l4.udp = &l4.uh;
282 break;
283 case IPPROTO_ICMP: {
284 const struct icmp *ic = enpc.npc_l4.icmp;
285 ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, &hasqid);
286 if (!ret || !hasqid)
287 goto out;
288 enpc.npc_info |= NPC_ICMP_ID;
289 break;
290 }
291 case IPPROTO_ICMPV6: {
292 const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
293 ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc, &hasqid);
294 if (!ret || !hasqid)
295 goto out;
296 enpc.npc_info |= NPC_ICMP_ID;
297 break;
298 }
299 default:
300 goto out;
301 }
302
303 /* Lookup a connection using the embedded packet. */
304 conn = npf_conn_lookup(&enpc, di, &forw);
305
306 out:
307 /*
308 * Recache npc. The nbuf may have been updated as a result of
309 * caching enpc.
310 */
311 npf_recache(npc);
312 return conn;
313 }
314
315 /*
316 * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
317 * which is embedded in ICMP packet. Note: backwards stream only.
318 */
319 static bool
320 npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
321 {
322 const u_int which = NPF_SRC;
323 npf_cache_t enpc;
324 struct icmp *ic;
325 uint16_t cksum;
326
327 if (forw || !npf_iscached(npc, NPC_ICMP))
328 return false;
329
330 /*
331 * ICMP: fetch the current checksum we are going to fixup.
332 */
333 ic = npc->npc_l4.icmp;
334 cksum = ic->icmp_cksum;
335
336 if (!npfa_icmp_inspect(npc, &enpc))
337 goto err;
338
339 /*
340 * If the ICMP packet had a Query Id, leave now. The packet didn't get
341 * modified, so no need to recache npc.
342 */
343 if (npf_iscached(npc, NPC_ICMP_ID)) {
344 KASSERT(!nbuf_flag_p(nbuf, NBUF_DATAREF_RESET));
345 return false;
346 }
347
348 KASSERT(npf_iscached(&enpc, NPC_IP46));
349 KASSERT(npf_iscached(&enpc, NPC_LAYER4));
350
351 CTASSERT(offsetof(struct icmp, icmp_cksum) ==
352 offsetof(struct icmp6_hdr, icmp6_cksum));
353
354 /*
355 * Fetch the IP and port in the _embedded_ packet. Also, fetch
356 * the IPv4 and TCP/UDP checksums before they are rewritten.
357 */
358 const int proto = enpc.npc_proto;
359 uint16_t ipcksum = 0, l4cksum = 0;
360 in_port_t old_port = 0;
361
362 if (npf_iscached(&enpc, NPC_IP4)) {
363 const struct ip *eip = enpc.npc_ip.v4;
364 ipcksum = eip->ip_sum;
365 }
366 switch (proto) {
367 case IPPROTO_TCP: {
368 const struct tcphdr *th = enpc.npc_l4.tcp;
369 old_port = th->th_sport;
370 l4cksum = th->th_sum;
371 break;
372 }
373 case IPPROTO_UDP: {
374 const struct udphdr *uh = enpc.npc_l4.udp;
375 old_port = uh->uh_sport;
376 l4cksum = uh->uh_sum;
377 break;
378 }
379 case IPPROTO_ICMP:
380 case IPPROTO_ICMPV6:
381 break;
382 default:
383 goto err;
384 }
385
386 /*
387 * Get the original IP address and port.
388 * Calculate the part of the ICMP checksum fixup.
389 */
390 npf_addr_t *addr;
391 in_port_t port;
392
393 npf_nat_getorig(nt, &addr, &port);
394
395 cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
396 if (port) {
397 cksum = npf_fixup16_cksum(cksum, old_port, port);
398 }
399
400 /*
401 * Translate the embedded packet. The following changes will
402 * be performed by npf_napt_rwr():
403 *
404 * 1) Rewrite the IP address and, if not ICMP, port.
405 * 2) Rewrite the TCP/UDP checksum (if not ICMP).
406 * 3) Rewrite the IPv4 checksum for (1) and (2).
407 *
408 * XXX: Assumes NPF_NATOUT (source address/port). Currently,
409 * npfa_icmp_match() matches only for the PFIL_OUT traffic.
410 */
411 if (npf_napt_rwr(&enpc, which, addr, port)) {
412 goto err;
413 }
414
415 /*
416 * Finally, finish the ICMP checksum fixup: include the checksum
417 * changes in the embedded packet.
418 */
419 if (npf_iscached(&enpc, NPC_IP4)) {
420 const struct ip *eip = enpc.npc_ip.v4;
421 cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
422 }
423 switch (proto) {
424 case IPPROTO_TCP: {
425 const struct tcphdr *th = enpc.npc_l4.tcp;
426 cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
427 break;
428 }
429 case IPPROTO_UDP:
430 if (l4cksum) {
431 const struct udphdr *uh = enpc.npc_l4.udp;
432 cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
433 }
434 break;
435 }
436 npf_recache(npc);
437 KASSERT(npf_iscached(npc, NPC_ICMP));
438 ic = npc->npc_l4.icmp;
439 ic->icmp_cksum = cksum;
440 return true;
441
442 err:
443 /*
444 * Recache npc. The nbuf may have been updated as a result of
445 * caching enpc.
446 */
447 npf_recache(npc);
448 return false;
449 }
450
451 /*
452 * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
453 * and module interface.
454 */
455
456 static int
457 npf_alg_icmp_init(void)
458 {
459 static const npfa_funcs_t icmp = {
460 .match = npfa_icmp_match,
461 .translate = npfa_icmp_nat,
462 .inspect = npfa_icmp_conn,
463 };
464 alg_icmp = npf_alg_register(npf_getkernctx(), "icmp", &icmp);
465 return alg_icmp ? 0 : ENOMEM;
466 }
467
468 static int
469 npf_alg_icmp_fini(void)
470 {
471 KASSERT(alg_icmp != NULL);
472 return npf_alg_unregister(npf_getkernctx(), alg_icmp);
473 }
474
475 static int
476 npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
477 {
478 switch (cmd) {
479 case MODULE_CMD_INIT:
480 return npf_alg_icmp_init();
481 case MODULE_CMD_FINI:
482 return npf_alg_icmp_fini();
483 case MODULE_CMD_AUTOUNLOAD:
484 return EBUSY;
485 default:
486 return ENOTTY;
487 }
488 return 0;
489 }
490