npf_alg_icmp.c revision 1.26 1 1.26 maxv /* $NetBSD: npf_alg_icmp.c,v 1.26 2018/03/12 12:45:26 maxv Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
9 1.1 rmind *
10 1.1 rmind * Redistribution and use in source and binary forms, with or without
11 1.1 rmind * modification, are permitted provided that the following conditions
12 1.1 rmind * are met:
13 1.1 rmind * 1. Redistributions of source code must retain the above copyright
14 1.1 rmind * notice, this list of conditions and the following disclaimer.
15 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 rmind * notice, this list of conditions and the following disclaimer in the
17 1.1 rmind * documentation and/or other materials provided with the distribution.
18 1.1 rmind *
19 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.1 rmind */
31 1.1 rmind
32 1.1 rmind /*
33 1.1 rmind * NPF ALG for ICMP and traceroute translations.
34 1.1 rmind */
35 1.1 rmind
36 1.24 christos #ifdef _KERNEL
37 1.1 rmind #include <sys/cdefs.h>
38 1.26 maxv __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.26 2018/03/12 12:45:26 maxv Exp $");
39 1.1 rmind
40 1.1 rmind #include <sys/param.h>
41 1.1 rmind #include <sys/module.h>
42 1.1 rmind
43 1.1 rmind #include <netinet/in_systm.h>
44 1.1 rmind #include <netinet/in.h>
45 1.1 rmind #include <netinet/ip.h>
46 1.1 rmind #include <netinet/tcp.h>
47 1.1 rmind #include <netinet/udp.h>
48 1.1 rmind #include <netinet/ip_icmp.h>
49 1.11 spz #include <netinet/icmp6.h>
50 1.1 rmind #include <net/pfil.h>
51 1.24 christos #endif
52 1.1 rmind
53 1.1 rmind #include "npf_impl.h"
54 1.22 rmind #include "npf_conn.h"
55 1.1 rmind
56 1.1 rmind MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
57 1.1 rmind
58 1.1 rmind /*
59 1.1 rmind * Traceroute criteria.
60 1.1 rmind *
61 1.1 rmind * IANA assigned base port: 33434. However, common practice is to increase
62 1.14 rmind * the port, thus monitor [33434-33484] range. Additional filter is low TTL.
63 1.1 rmind */
64 1.1 rmind
65 1.1 rmind #define TR_BASE_PORT 33434
66 1.1 rmind #define TR_PORT_RANGE 33484
67 1.14 rmind #define TR_MAX_TTL 48
68 1.1 rmind
69 1.6 rmind static npf_alg_t * alg_icmp __read_mostly;
70 1.1 rmind
71 1.1 rmind /*
72 1.21 spz * npfa_icmp_match: matching inspector determines ALG case and associates
73 1.18 rmind * our ALG with the NAT entry.
74 1.1 rmind */
75 1.1 rmind static bool
76 1.23 rmind npfa_icmp_match(npf_cache_t *npc, npf_nat_t *nt, int di)
77 1.1 rmind {
78 1.15 rmind const int proto = npc->npc_proto;
79 1.14 rmind const struct ip *ip = npc->npc_ip.v4;
80 1.4 rmind in_port_t dport;
81 1.4 rmind
82 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
83 1.7 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
84 1.4 rmind
85 1.18 rmind /* Check for low TTL. Also, we support outbound NAT only. */
86 1.18 rmind if (ip->ip_ttl > TR_MAX_TTL || di != PFIL_OUT) {
87 1.6 rmind return false;
88 1.6 rmind }
89 1.6 rmind
90 1.14 rmind switch (proto) {
91 1.14 rmind case IPPROTO_TCP: {
92 1.14 rmind const struct tcphdr *th = npc->npc_l4.tcp;
93 1.4 rmind dport = ntohs(th->th_dport);
94 1.14 rmind break;
95 1.14 rmind }
96 1.14 rmind case IPPROTO_UDP: {
97 1.14 rmind const struct udphdr *uh = npc->npc_l4.udp;
98 1.4 rmind dport = ntohs(uh->uh_dport);
99 1.14 rmind break;
100 1.14 rmind }
101 1.14 rmind case IPPROTO_ICMP:
102 1.14 rmind case IPPROTO_ICMPV6:
103 1.14 rmind /* Just to pass the test below. */
104 1.14 rmind dport = TR_BASE_PORT;
105 1.14 rmind break;
106 1.14 rmind default:
107 1.4 rmind return false;
108 1.4 rmind }
109 1.1 rmind
110 1.1 rmind /* Handle TCP/UDP traceroute - check for port range. */
111 1.1 rmind if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
112 1.1 rmind return false;
113 1.1 rmind }
114 1.1 rmind
115 1.1 rmind /* Associate ALG with translation entry. */
116 1.1 rmind npf_nat_setalg(nt, alg_icmp, 0);
117 1.1 rmind return true;
118 1.1 rmind }
119 1.1 rmind
120 1.1 rmind /*
121 1.14 rmind * npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
122 1.14 rmind * ID or TCP/UDP ports of the original packet, which is embedded.
123 1.1 rmind */
124 1.13 rmind
125 1.5 rmind static bool
126 1.23 rmind npfa_icmp4_inspect(const int type, npf_cache_t *npc)
127 1.1 rmind {
128 1.23 rmind nbuf_t *nbuf = npc->npc_nbuf;
129 1.13 rmind u_int offby;
130 1.11 spz
131 1.13 rmind /* Per RFC 792. */
132 1.13 rmind switch (type) {
133 1.13 rmind case ICMP_UNREACH:
134 1.13 rmind case ICMP_SOURCEQUENCH:
135 1.13 rmind case ICMP_REDIRECT:
136 1.13 rmind case ICMP_TIMXCEED:
137 1.13 rmind case ICMP_PARAMPROB:
138 1.14 rmind /* Should contain original IP header. */
139 1.14 rmind if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
140 1.13 rmind return false;
141 1.1 rmind }
142 1.23 rmind return (npf_cache_all(npc) & NPC_LAYER4) != 0;
143 1.13 rmind
144 1.13 rmind case ICMP_ECHOREPLY:
145 1.13 rmind case ICMP_ECHO:
146 1.13 rmind case ICMP_TSTAMP:
147 1.13 rmind case ICMP_TSTAMPREPLY:
148 1.13 rmind case ICMP_IREQ:
149 1.13 rmind case ICMP_IREQREPLY:
150 1.14 rmind /* Should contain ICMP query ID - ensure. */
151 1.13 rmind offby = offsetof(struct icmp, icmp_id);
152 1.14 rmind if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
153 1.13 rmind return false;
154 1.13 rmind }
155 1.13 rmind npc->npc_info |= NPC_ICMP_ID;
156 1.13 rmind return true;
157 1.13 rmind default:
158 1.13 rmind break;
159 1.11 spz }
160 1.13 rmind return false;
161 1.13 rmind }
162 1.13 rmind
163 1.13 rmind static bool
164 1.23 rmind npfa_icmp6_inspect(const int type, npf_cache_t *npc)
165 1.13 rmind {
166 1.23 rmind nbuf_t *nbuf = npc->npc_nbuf;
167 1.13 rmind u_int offby;
168 1.13 rmind
169 1.13 rmind /* Per RFC 4443. */
170 1.13 rmind switch (type) {
171 1.13 rmind case ICMP6_DST_UNREACH:
172 1.13 rmind case ICMP6_PACKET_TOO_BIG:
173 1.13 rmind case ICMP6_TIME_EXCEEDED:
174 1.13 rmind case ICMP6_PARAM_PROB:
175 1.14 rmind /* Should contain original IP header. */
176 1.14 rmind if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
177 1.13 rmind return false;
178 1.1 rmind }
179 1.23 rmind return (npf_cache_all(npc) & NPC_LAYER4) != 0;
180 1.13 rmind
181 1.13 rmind case ICMP6_ECHO_REQUEST:
182 1.13 rmind case ICMP6_ECHO_REPLY:
183 1.14 rmind /* Should contain ICMP query ID - ensure. */
184 1.13 rmind offby = offsetof(struct icmp6_hdr, icmp6_id);
185 1.14 rmind if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
186 1.13 rmind return false;
187 1.13 rmind }
188 1.13 rmind npc->npc_info |= NPC_ICMP_ID;
189 1.13 rmind return true;
190 1.13 rmind default:
191 1.13 rmind break;
192 1.1 rmind }
193 1.1 rmind return false;
194 1.1 rmind }
195 1.1 rmind
196 1.1 rmind /*
197 1.22 rmind * npfa_icmp_inspect: ALG ICMP inspector.
198 1.14 rmind *
199 1.14 rmind * => Returns true if "enpc" is filled.
200 1.1 rmind */
201 1.1 rmind static bool
202 1.23 rmind npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
203 1.1 rmind {
204 1.23 rmind nbuf_t *nbuf = npc->npc_nbuf;
205 1.13 rmind bool ret;
206 1.13 rmind
207 1.14 rmind KASSERT(npf_iscached(npc, NPC_IP46));
208 1.4 rmind KASSERT(npf_iscached(npc, NPC_ICMP));
209 1.1 rmind
210 1.1 rmind /* Advance to ICMP header. */
211 1.14 rmind nbuf_reset(nbuf);
212 1.15 rmind if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
213 1.1 rmind return false;
214 1.1 rmind }
215 1.24 christos enpc->npc_ctx = npc->npc_ctx;
216 1.23 rmind enpc->npc_nbuf = nbuf;
217 1.14 rmind enpc->npc_info = 0;
218 1.1 rmind
219 1.13 rmind /*
220 1.14 rmind * Inspect the ICMP packet. The relevant data might be in the
221 1.14 rmind * embedded packet. Fill the "enpc" cache, if so.
222 1.13 rmind */
223 1.13 rmind if (npf_iscached(npc, NPC_IP4)) {
224 1.14 rmind const struct icmp *ic = npc->npc_l4.icmp;
225 1.23 rmind ret = npfa_icmp4_inspect(ic->icmp_type, enpc);
226 1.13 rmind } else if (npf_iscached(npc, NPC_IP6)) {
227 1.14 rmind const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
228 1.23 rmind ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc);
229 1.13 rmind } else {
230 1.13 rmind ret = false;
231 1.13 rmind }
232 1.13 rmind if (!ret) {
233 1.1 rmind return false;
234 1.1 rmind }
235 1.1 rmind
236 1.14 rmind /* ICMP ID is the original packet, just indicate it. */
237 1.14 rmind if (npf_iscached(enpc, NPC_ICMP_ID)) {
238 1.4 rmind npc->npc_info |= NPC_ICMP_ID;
239 1.4 rmind return false;
240 1.1 rmind }
241 1.4 rmind
242 1.14 rmind /* Indicate that embedded packet is in the cache. */
243 1.14 rmind return true;
244 1.14 rmind }
245 1.14 rmind
246 1.22 rmind static npf_conn_t *
247 1.23 rmind npfa_icmp_conn(npf_cache_t *npc, int di)
248 1.14 rmind {
249 1.14 rmind npf_cache_t enpc;
250 1.14 rmind
251 1.14 rmind /* Inspect ICMP packet for an embedded packet. */
252 1.14 rmind if (!npf_iscached(npc, NPC_ICMP))
253 1.14 rmind return NULL;
254 1.23 rmind if (!npfa_icmp_inspect(npc, &enpc))
255 1.14 rmind return NULL;
256 1.14 rmind
257 1.4 rmind /*
258 1.14 rmind * Invert the identifiers of the embedded packet.
259 1.14 rmind * If it is ICMP, then ensure ICMP ID.
260 1.4 rmind */
261 1.14 rmind union l4 {
262 1.14 rmind struct tcphdr th;
263 1.14 rmind struct udphdr uh;
264 1.14 rmind } l4;
265 1.14 rmind bool ret, forw;
266 1.14 rmind
267 1.14 rmind #define SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
268 1.18 rmind SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
269 1.14 rmind
270 1.15 rmind switch (enpc.npc_proto) {
271 1.14 rmind case IPPROTO_TCP:
272 1.14 rmind l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
273 1.14 rmind l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
274 1.14 rmind enpc.npc_l4.tcp = &l4.th;
275 1.14 rmind break;
276 1.14 rmind case IPPROTO_UDP:
277 1.14 rmind l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
278 1.14 rmind l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
279 1.14 rmind enpc.npc_l4.udp = &l4.uh;
280 1.14 rmind break;
281 1.14 rmind case IPPROTO_ICMP: {
282 1.14 rmind const struct icmp *ic = enpc.npc_l4.icmp;
283 1.23 rmind ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
284 1.14 rmind if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
285 1.14 rmind return false;
286 1.14 rmind break;
287 1.14 rmind }
288 1.14 rmind case IPPROTO_ICMPV6: {
289 1.14 rmind const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
290 1.23 rmind ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
291 1.14 rmind if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
292 1.14 rmind return false;
293 1.14 rmind break;
294 1.14 rmind }
295 1.14 rmind default:
296 1.14 rmind return false;
297 1.14 rmind }
298 1.4 rmind
299 1.22 rmind /* Lookup a connection using the embedded packet. */
300 1.23 rmind return npf_conn_lookup(&enpc, di, &forw);
301 1.1 rmind }
302 1.1 rmind
303 1.1 rmind /*
304 1.18 rmind * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
305 1.18 rmind * which is embedded in ICMP packet. Note: backwards stream only.
306 1.1 rmind */
307 1.1 rmind static bool
308 1.23 rmind npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
309 1.1 rmind {
310 1.20 rmind const u_int which = NPF_SRC;
311 1.14 rmind npf_cache_t enpc;
312 1.1 rmind
313 1.18 rmind if (forw || !npf_iscached(npc, NPC_ICMP))
314 1.14 rmind return false;
315 1.23 rmind if (!npfa_icmp_inspect(npc, &enpc))
316 1.1 rmind return false;
317 1.14 rmind
318 1.7 zoltan KASSERT(npf_iscached(&enpc, NPC_IP46));
319 1.7 zoltan KASSERT(npf_iscached(&enpc, NPC_LAYER4));
320 1.14 rmind
321 1.20 rmind /*
322 1.20 rmind * ICMP: fetch the current checksum we are going to fixup.
323 1.20 rmind */
324 1.14 rmind struct icmp *ic = npc->npc_l4.icmp;
325 1.14 rmind uint16_t cksum = ic->icmp_cksum;
326 1.14 rmind
327 1.14 rmind CTASSERT(offsetof(struct icmp, icmp_cksum) ==
328 1.14 rmind offsetof(struct icmp6_hdr, icmp6_cksum));
329 1.1 rmind
330 1.6 rmind /*
331 1.20 rmind * Fetch the IP and port in the _embedded_ packet. Also, fetch
332 1.20 rmind * the IPv4 and TCP/UDP checksums before they are rewritten.
333 1.6 rmind */
334 1.15 rmind const int proto = enpc.npc_proto;
335 1.14 rmind uint16_t ipcksum = 0, l4cksum = 0;
336 1.25 rmind in_port_t old_port = 0;
337 1.4 rmind
338 1.14 rmind if (npf_iscached(&enpc, NPC_IP4)) {
339 1.14 rmind const struct ip *eip = enpc.npc_ip.v4;
340 1.14 rmind ipcksum = eip->ip_sum;
341 1.14 rmind }
342 1.14 rmind switch (proto) {
343 1.14 rmind case IPPROTO_TCP: {
344 1.14 rmind const struct tcphdr *th = enpc.npc_l4.tcp;
345 1.25 rmind old_port = th->th_sport;
346 1.4 rmind l4cksum = th->th_sum;
347 1.14 rmind break;
348 1.14 rmind }
349 1.14 rmind case IPPROTO_UDP: {
350 1.14 rmind const struct udphdr *uh = enpc.npc_l4.udp;
351 1.25 rmind old_port = uh->uh_sport;
352 1.4 rmind l4cksum = uh->uh_sum;
353 1.14 rmind break;
354 1.1 rmind }
355 1.14 rmind case IPPROTO_ICMP:
356 1.14 rmind case IPPROTO_ICMPV6:
357 1.14 rmind break;
358 1.14 rmind default:
359 1.1 rmind return false;
360 1.1 rmind }
361 1.1 rmind
362 1.4 rmind /*
363 1.25 rmind * Get the original IP address and port.
364 1.25 rmind * Calculate the part of the ICMP checksum fixup.
365 1.25 rmind */
366 1.25 rmind npf_addr_t *addr;
367 1.25 rmind in_port_t port;
368 1.25 rmind
369 1.25 rmind npf_nat_getorig(nt, &addr, &port);
370 1.25 rmind
371 1.25 rmind cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
372 1.25 rmind if (port) {
373 1.25 rmind cksum = npf_fixup16_cksum(cksum, old_port, port);
374 1.25 rmind }
375 1.25 rmind
376 1.25 rmind /*
377 1.20 rmind * Translate the embedded packet. The following changes will
378 1.20 rmind * be performed by npf_napt_rwr():
379 1.20 rmind *
380 1.20 rmind * 1) Rewrite the IP address and, if not ICMP, port.
381 1.20 rmind * 2) Rewrite the TCP/UDP checksum (if not ICMP).
382 1.20 rmind * 3) Rewrite the IPv4 checksum for (1) and (2).
383 1.20 rmind *
384 1.20 rmind * XXX: Assumes NPF_NATOUT (source address/port). Currently,
385 1.20 rmind * npfa_icmp_match() matches only for the PFIL_OUT traffic.
386 1.4 rmind */
387 1.20 rmind if (npf_napt_rwr(&enpc, which, addr, port)) {
388 1.1 rmind return false;
389 1.1 rmind }
390 1.1 rmind
391 1.1 rmind /*
392 1.20 rmind * Finally, finish the ICMP checksum fixup: include the checksum
393 1.20 rmind * changes in the embedded packet.
394 1.1 rmind */
395 1.14 rmind if (npf_iscached(&enpc, NPC_IP4)) {
396 1.14 rmind const struct ip *eip = enpc.npc_ip.v4;
397 1.14 rmind cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
398 1.14 rmind }
399 1.14 rmind switch (proto) {
400 1.14 rmind case IPPROTO_TCP: {
401 1.14 rmind const struct tcphdr *th = enpc.npc_l4.tcp;
402 1.4 rmind cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
403 1.14 rmind break;
404 1.1 rmind }
405 1.14 rmind case IPPROTO_UDP:
406 1.14 rmind if (l4cksum) {
407 1.14 rmind const struct udphdr *uh = enpc.npc_l4.udp;
408 1.14 rmind cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
409 1.14 rmind }
410 1.14 rmind break;
411 1.6 rmind }
412 1.14 rmind ic->icmp_cksum = cksum;
413 1.6 rmind return true;
414 1.1 rmind }
415 1.19 rmind
416 1.19 rmind /*
417 1.19 rmind * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
418 1.19 rmind * and module interface.
419 1.19 rmind */
420 1.19 rmind
421 1.19 rmind static int
422 1.19 rmind npf_alg_icmp_init(void)
423 1.19 rmind {
424 1.19 rmind static const npfa_funcs_t icmp = {
425 1.19 rmind .match = npfa_icmp_match,
426 1.19 rmind .translate = npfa_icmp_nat,
427 1.22 rmind .inspect = npfa_icmp_conn,
428 1.19 rmind };
429 1.24 christos alg_icmp = npf_alg_register(npf_getkernctx(), "icmp", &icmp);
430 1.19 rmind return alg_icmp ? 0 : ENOMEM;
431 1.19 rmind }
432 1.19 rmind
433 1.19 rmind static int
434 1.19 rmind npf_alg_icmp_fini(void)
435 1.19 rmind {
436 1.19 rmind KASSERT(alg_icmp != NULL);
437 1.24 christos return npf_alg_unregister(npf_getkernctx(), alg_icmp);
438 1.19 rmind }
439 1.19 rmind
440 1.19 rmind static int
441 1.19 rmind npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
442 1.19 rmind {
443 1.19 rmind switch (cmd) {
444 1.19 rmind case MODULE_CMD_INIT:
445 1.19 rmind return npf_alg_icmp_init();
446 1.19 rmind case MODULE_CMD_FINI:
447 1.19 rmind return npf_alg_icmp_fini();
448 1.19 rmind case MODULE_CMD_AUTOUNLOAD:
449 1.19 rmind return EBUSY;
450 1.19 rmind default:
451 1.19 rmind return ENOTTY;
452 1.19 rmind }
453 1.19 rmind return 0;
454 1.19 rmind }
455