npf_alg_icmp.c revision 1.16 1 1.16 christos /* $NetBSD: npf_alg_icmp.c,v 1.16 2013/03/20 00:29:47 christos 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.1 rmind #include <sys/cdefs.h>
37 1.16 christos __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.16 2013/03/20 00:29:47 christos Exp $");
38 1.1 rmind
39 1.1 rmind #include <sys/param.h>
40 1.1 rmind #include <sys/module.h>
41 1.1 rmind #include <sys/pool.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.1 rmind
52 1.1 rmind #include "npf_impl.h"
53 1.1 rmind
54 1.1 rmind MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
55 1.1 rmind
56 1.1 rmind /*
57 1.1 rmind * Traceroute criteria.
58 1.1 rmind *
59 1.1 rmind * IANA assigned base port: 33434. However, common practice is to increase
60 1.14 rmind * the port, thus monitor [33434-33484] range. Additional filter is low TTL.
61 1.1 rmind */
62 1.1 rmind
63 1.1 rmind #define TR_BASE_PORT 33434
64 1.1 rmind #define TR_PORT_RANGE 33484
65 1.14 rmind #define TR_MAX_TTL 48
66 1.1 rmind
67 1.6 rmind static npf_alg_t * alg_icmp __read_mostly;
68 1.1 rmind
69 1.14 rmind static bool npfa_icmp_match(npf_cache_t *, nbuf_t *, npf_nat_t *, int);
70 1.14 rmind static bool npfa_icmp_nat(npf_cache_t *, nbuf_t *, npf_nat_t *, int);
71 1.14 rmind static npf_session_t *npfa_icmp_session(npf_cache_t *, nbuf_t *, int);
72 1.1 rmind
73 1.1 rmind /*
74 1.1 rmind * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
75 1.1 rmind * and module interface.
76 1.1 rmind */
77 1.1 rmind
78 1.1 rmind static int
79 1.1 rmind npf_alg_icmp_init(void)
80 1.1 rmind {
81 1.1 rmind
82 1.16 christos alg_icmp = npf_alg_register("icmp", npfa_icmp_match,
83 1.14 rmind npfa_icmp_nat, npfa_icmp_session);
84 1.1 rmind KASSERT(alg_icmp != NULL);
85 1.1 rmind return 0;
86 1.1 rmind }
87 1.1 rmind
88 1.1 rmind static int
89 1.1 rmind npf_alg_icmp_fini(void)
90 1.1 rmind {
91 1.1 rmind
92 1.1 rmind KASSERT(alg_icmp != NULL);
93 1.1 rmind return npf_alg_unregister(alg_icmp);
94 1.1 rmind }
95 1.1 rmind
96 1.1 rmind static int
97 1.1 rmind npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
98 1.1 rmind {
99 1.1 rmind
100 1.1 rmind switch (cmd) {
101 1.1 rmind case MODULE_CMD_INIT:
102 1.1 rmind return npf_alg_icmp_init();
103 1.1 rmind case MODULE_CMD_FINI:
104 1.1 rmind return npf_alg_icmp_fini();
105 1.10 rmind case MODULE_CMD_AUTOUNLOAD:
106 1.10 rmind return EBUSY;
107 1.1 rmind default:
108 1.1 rmind return ENOTTY;
109 1.1 rmind }
110 1.1 rmind return 0;
111 1.1 rmind }
112 1.1 rmind
113 1.1 rmind /*
114 1.4 rmind * npfa_icmp_match: ALG matching inspector - determines ALG case and
115 1.4 rmind * associates ALG with NAT entry.
116 1.1 rmind */
117 1.1 rmind static bool
118 1.14 rmind npfa_icmp_match(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
119 1.1 rmind {
120 1.15 rmind const int proto = npc->npc_proto;
121 1.14 rmind const struct ip *ip = npc->npc_ip.v4;
122 1.4 rmind in_port_t dport;
123 1.4 rmind
124 1.7 zoltan KASSERT(npf_iscached(npc, NPC_IP46));
125 1.7 zoltan KASSERT(npf_iscached(npc, NPC_LAYER4));
126 1.4 rmind
127 1.6 rmind /* Check for low TTL. */
128 1.6 rmind if (ip->ip_ttl > TR_MAX_TTL) {
129 1.6 rmind return false;
130 1.6 rmind }
131 1.6 rmind
132 1.14 rmind switch (proto) {
133 1.14 rmind case IPPROTO_TCP: {
134 1.14 rmind const struct tcphdr *th = npc->npc_l4.tcp;
135 1.4 rmind dport = ntohs(th->th_dport);
136 1.14 rmind break;
137 1.14 rmind }
138 1.14 rmind case IPPROTO_UDP: {
139 1.14 rmind const struct udphdr *uh = npc->npc_l4.udp;
140 1.4 rmind dport = ntohs(uh->uh_dport);
141 1.14 rmind break;
142 1.14 rmind }
143 1.14 rmind case IPPROTO_ICMP:
144 1.14 rmind case IPPROTO_ICMPV6:
145 1.14 rmind /* Just to pass the test below. */
146 1.14 rmind dport = TR_BASE_PORT;
147 1.14 rmind break;
148 1.14 rmind default:
149 1.4 rmind return false;
150 1.4 rmind }
151 1.1 rmind
152 1.1 rmind /* Handle TCP/UDP traceroute - check for port range. */
153 1.1 rmind if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
154 1.1 rmind return false;
155 1.1 rmind }
156 1.1 rmind
157 1.1 rmind /* Associate ALG with translation entry. */
158 1.1 rmind npf_nat_setalg(nt, alg_icmp, 0);
159 1.1 rmind return true;
160 1.1 rmind }
161 1.1 rmind
162 1.1 rmind /*
163 1.14 rmind * npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
164 1.14 rmind * ID or TCP/UDP ports of the original packet, which is embedded.
165 1.1 rmind */
166 1.13 rmind
167 1.5 rmind static bool
168 1.14 rmind npfa_icmp4_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf)
169 1.1 rmind {
170 1.13 rmind u_int offby;
171 1.11 spz
172 1.13 rmind /* Per RFC 792. */
173 1.13 rmind switch (type) {
174 1.13 rmind case ICMP_UNREACH:
175 1.13 rmind case ICMP_SOURCEQUENCH:
176 1.13 rmind case ICMP_REDIRECT:
177 1.13 rmind case ICMP_TIMXCEED:
178 1.13 rmind case ICMP_PARAMPROB:
179 1.14 rmind if (npc == NULL) {
180 1.13 rmind return false;
181 1.13 rmind }
182 1.14 rmind /* Should contain original IP header. */
183 1.14 rmind if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
184 1.13 rmind return false;
185 1.1 rmind }
186 1.14 rmind return (npf_cache_all(npc, nbuf) & NPC_LAYER4) != 0;
187 1.13 rmind
188 1.13 rmind case ICMP_ECHOREPLY:
189 1.13 rmind case ICMP_ECHO:
190 1.13 rmind case ICMP_TSTAMP:
191 1.13 rmind case ICMP_TSTAMPREPLY:
192 1.13 rmind case ICMP_IREQ:
193 1.13 rmind case ICMP_IREQREPLY:
194 1.14 rmind /* Should contain ICMP query ID - ensure. */
195 1.13 rmind offby = offsetof(struct icmp, icmp_id);
196 1.14 rmind if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
197 1.13 rmind return false;
198 1.13 rmind }
199 1.13 rmind npc->npc_info |= NPC_ICMP_ID;
200 1.13 rmind return true;
201 1.13 rmind default:
202 1.13 rmind break;
203 1.11 spz }
204 1.13 rmind return false;
205 1.13 rmind }
206 1.13 rmind
207 1.13 rmind static bool
208 1.14 rmind npfa_icmp6_inspect(const int type, npf_cache_t *npc, nbuf_t *nbuf)
209 1.13 rmind {
210 1.13 rmind u_int offby;
211 1.13 rmind
212 1.13 rmind /* Per RFC 4443. */
213 1.13 rmind switch (type) {
214 1.13 rmind case ICMP6_DST_UNREACH:
215 1.13 rmind case ICMP6_PACKET_TOO_BIG:
216 1.13 rmind case ICMP6_TIME_EXCEEDED:
217 1.13 rmind case ICMP6_PARAM_PROB:
218 1.14 rmind if (npc == NULL) {
219 1.13 rmind return false;
220 1.13 rmind }
221 1.14 rmind /* Should contain original IP header. */
222 1.14 rmind if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
223 1.13 rmind return false;
224 1.1 rmind }
225 1.14 rmind return (npf_cache_all(npc, nbuf) & NPC_LAYER4) != 0;
226 1.13 rmind
227 1.13 rmind case ICMP6_ECHO_REQUEST:
228 1.13 rmind case ICMP6_ECHO_REPLY:
229 1.14 rmind /* Should contain ICMP query ID - ensure. */
230 1.13 rmind offby = offsetof(struct icmp6_hdr, icmp6_id);
231 1.14 rmind if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
232 1.13 rmind return false;
233 1.13 rmind }
234 1.13 rmind npc->npc_info |= NPC_ICMP_ID;
235 1.13 rmind return true;
236 1.13 rmind default:
237 1.13 rmind break;
238 1.1 rmind }
239 1.1 rmind return false;
240 1.1 rmind }
241 1.1 rmind
242 1.1 rmind /*
243 1.14 rmind * npfa_icmp_session: ALG ICMP inspector.
244 1.14 rmind *
245 1.14 rmind * => Returns true if "enpc" is filled.
246 1.1 rmind */
247 1.1 rmind static bool
248 1.14 rmind npfa_icmp_inspect(npf_cache_t *npc, nbuf_t *nbuf, npf_cache_t *enpc)
249 1.1 rmind {
250 1.13 rmind bool ret;
251 1.13 rmind
252 1.14 rmind KASSERT(npf_iscached(npc, NPC_IP46));
253 1.4 rmind KASSERT(npf_iscached(npc, NPC_ICMP));
254 1.1 rmind
255 1.1 rmind /* Advance to ICMP header. */
256 1.14 rmind nbuf_reset(nbuf);
257 1.15 rmind if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
258 1.1 rmind return false;
259 1.1 rmind }
260 1.14 rmind enpc->npc_info = 0;
261 1.1 rmind
262 1.13 rmind /*
263 1.14 rmind * Inspect the ICMP packet. The relevant data might be in the
264 1.14 rmind * embedded packet. Fill the "enpc" cache, if so.
265 1.13 rmind */
266 1.13 rmind if (npf_iscached(npc, NPC_IP4)) {
267 1.14 rmind const struct icmp *ic = npc->npc_l4.icmp;
268 1.14 rmind ret = npfa_icmp4_inspect(ic->icmp_type, enpc, nbuf);
269 1.13 rmind } else if (npf_iscached(npc, NPC_IP6)) {
270 1.14 rmind const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
271 1.14 rmind ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc, nbuf);
272 1.13 rmind } else {
273 1.13 rmind ret = false;
274 1.13 rmind }
275 1.13 rmind if (!ret) {
276 1.1 rmind return false;
277 1.1 rmind }
278 1.1 rmind
279 1.14 rmind /* ICMP ID is the original packet, just indicate it. */
280 1.14 rmind if (npf_iscached(enpc, NPC_ICMP_ID)) {
281 1.4 rmind npc->npc_info |= NPC_ICMP_ID;
282 1.4 rmind return false;
283 1.1 rmind }
284 1.4 rmind
285 1.14 rmind /* Indicate that embedded packet is in the cache. */
286 1.14 rmind return true;
287 1.14 rmind }
288 1.14 rmind
289 1.14 rmind static npf_session_t *
290 1.14 rmind npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, int di)
291 1.14 rmind {
292 1.14 rmind npf_cache_t enpc;
293 1.14 rmind
294 1.14 rmind /* Inspect ICMP packet for an embedded packet. */
295 1.14 rmind if (!npf_iscached(npc, NPC_ICMP))
296 1.14 rmind return NULL;
297 1.14 rmind if (!npfa_icmp_inspect(npc, nbuf, &enpc))
298 1.14 rmind return NULL;
299 1.14 rmind
300 1.4 rmind /*
301 1.14 rmind * Invert the identifiers of the embedded packet.
302 1.14 rmind * If it is ICMP, then ensure ICMP ID.
303 1.4 rmind */
304 1.14 rmind union l4 {
305 1.14 rmind struct tcphdr th;
306 1.14 rmind struct udphdr uh;
307 1.14 rmind } l4;
308 1.14 rmind bool ret, forw;
309 1.14 rmind
310 1.14 rmind #define SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
311 1.14 rmind SWAP(npf_addr_t *, enpc.npc_srcip, enpc.npc_dstip);
312 1.14 rmind
313 1.15 rmind switch (enpc.npc_proto) {
314 1.14 rmind case IPPROTO_TCP:
315 1.14 rmind l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
316 1.14 rmind l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
317 1.14 rmind enpc.npc_l4.tcp = &l4.th;
318 1.14 rmind break;
319 1.14 rmind case IPPROTO_UDP:
320 1.14 rmind l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
321 1.14 rmind l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
322 1.14 rmind enpc.npc_l4.udp = &l4.uh;
323 1.14 rmind break;
324 1.14 rmind case IPPROTO_ICMP: {
325 1.14 rmind const struct icmp *ic = enpc.npc_l4.icmp;
326 1.14 rmind ret = npfa_icmp4_inspect(ic->icmp_type, &enpc, nbuf);
327 1.14 rmind if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
328 1.14 rmind return false;
329 1.14 rmind break;
330 1.14 rmind }
331 1.14 rmind case IPPROTO_ICMPV6: {
332 1.14 rmind const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
333 1.14 rmind ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc, nbuf);
334 1.14 rmind if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
335 1.14 rmind return false;
336 1.14 rmind break;
337 1.14 rmind }
338 1.14 rmind default:
339 1.14 rmind return false;
340 1.14 rmind }
341 1.4 rmind
342 1.14 rmind /* Lookup for a session using embedded packet. */
343 1.14 rmind return npf_session_lookup(&enpc, nbuf, di, &forw);
344 1.1 rmind }
345 1.1 rmind
346 1.1 rmind /*
347 1.14 rmind * npfa_icmp_nat: ALG inbound translation inspector, rewrite IP address
348 1.1 rmind * in the IP header, which is embedded in ICMP packet.
349 1.1 rmind */
350 1.1 rmind static bool
351 1.14 rmind npfa_icmp_nat(npf_cache_t *npc, nbuf_t *nbuf, npf_nat_t *nt, int di)
352 1.1 rmind {
353 1.14 rmind npf_cache_t enpc;
354 1.1 rmind
355 1.14 rmind if (di != PFIL_IN || !npf_iscached(npc, NPC_ICMP))
356 1.14 rmind return false;
357 1.14 rmind if (!npfa_icmp_inspect(npc, nbuf, &enpc))
358 1.1 rmind return false;
359 1.14 rmind
360 1.7 zoltan KASSERT(npf_iscached(&enpc, NPC_IP46));
361 1.7 zoltan KASSERT(npf_iscached(&enpc, NPC_LAYER4));
362 1.14 rmind
363 1.14 rmind struct icmp *ic = npc->npc_l4.icmp;
364 1.14 rmind uint16_t cksum = ic->icmp_cksum;
365 1.14 rmind
366 1.14 rmind CTASSERT(offsetof(struct icmp, icmp_cksum) ==
367 1.14 rmind offsetof(struct icmp6_hdr, icmp6_cksum));
368 1.1 rmind
369 1.6 rmind /*
370 1.14 rmind * Retrieve the original address and port, then calculate ICMP
371 1.14 rmind * checksum for these changes in the embedded packet. While data
372 1.14 rmind * is not rewritten in the cache, save IP and TCP/UDP checksums.
373 1.6 rmind */
374 1.15 rmind const int proto = enpc.npc_proto;
375 1.14 rmind uint16_t ipcksum = 0, l4cksum = 0;
376 1.6 rmind npf_addr_t *addr;
377 1.6 rmind in_port_t port;
378 1.6 rmind
379 1.6 rmind npf_nat_getorig(nt, &addr, &port);
380 1.4 rmind
381 1.14 rmind if (npf_iscached(&enpc, NPC_IP4)) {
382 1.14 rmind const struct ip *eip = enpc.npc_ip.v4;
383 1.14 rmind ipcksum = eip->ip_sum;
384 1.14 rmind }
385 1.14 rmind cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_srcip, addr);
386 1.14 rmind
387 1.14 rmind switch (proto) {
388 1.14 rmind case IPPROTO_TCP: {
389 1.14 rmind const struct tcphdr *th = enpc.npc_l4.tcp;
390 1.6 rmind cksum = npf_fixup16_cksum(cksum, th->th_sport, port);
391 1.4 rmind l4cksum = th->th_sum;
392 1.14 rmind break;
393 1.14 rmind }
394 1.14 rmind case IPPROTO_UDP: {
395 1.14 rmind const struct udphdr *uh = enpc.npc_l4.udp;
396 1.6 rmind cksum = npf_fixup16_cksum(cksum, uh->uh_sport, port);
397 1.4 rmind l4cksum = uh->uh_sum;
398 1.14 rmind break;
399 1.1 rmind }
400 1.14 rmind case IPPROTO_ICMP:
401 1.14 rmind case IPPROTO_ICMPV6:
402 1.14 rmind break;
403 1.14 rmind default:
404 1.1 rmind return false;
405 1.1 rmind }
406 1.1 rmind
407 1.4 rmind /*
408 1.14 rmind * Rewrite the source IP address and port of the embedded IP header,
409 1.14 rmind * which represents the original packet, therefore passing PFIL_OUT.
410 1.14 rmind * This updates the checksums in the embedded packet.
411 1.4 rmind */
412 1.14 rmind if (npf_nat_translate(&enpc, nbuf, nt, false, PFIL_OUT)) {
413 1.1 rmind return false;
414 1.1 rmind }
415 1.1 rmind
416 1.1 rmind /*
417 1.14 rmind * Finish calculation of the ICMP checksum: include the checksum
418 1.14 rmind * change in the embedded packet.
419 1.1 rmind */
420 1.14 rmind if (npf_iscached(&enpc, NPC_IP4)) {
421 1.14 rmind const struct ip *eip = enpc.npc_ip.v4;
422 1.14 rmind cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
423 1.14 rmind }
424 1.14 rmind switch (proto) {
425 1.14 rmind case IPPROTO_TCP: {
426 1.14 rmind const struct tcphdr *th = enpc.npc_l4.tcp;
427 1.4 rmind cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
428 1.14 rmind break;
429 1.1 rmind }
430 1.14 rmind case IPPROTO_UDP:
431 1.14 rmind if (l4cksum) {
432 1.14 rmind const struct udphdr *uh = enpc.npc_l4.udp;
433 1.14 rmind cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
434 1.14 rmind }
435 1.14 rmind break;
436 1.6 rmind }
437 1.14 rmind ic->icmp_cksum = cksum;
438 1.6 rmind return true;
439 1.1 rmind }
440