Home | History | Annotate | Line # | Download | only in npf
npf_alg_icmp.c revision 1.27
      1 /*	$NetBSD: npf_alg_icmp.c,v 1.27 2018/03/22 07:32:07 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.27 2018/03/22 07:32:07 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 
    125 static bool
    126 npfa_icmp4_inspect(const int type, npf_cache_t *npc)
    127 {
    128 	nbuf_t *nbuf = npc->npc_nbuf;
    129 	u_int offby;
    130 
    131 	/* Per RFC 792. */
    132 	switch (type) {
    133 	case ICMP_UNREACH:
    134 	case ICMP_SOURCEQUENCH:
    135 	case ICMP_REDIRECT:
    136 	case ICMP_TIMXCEED:
    137 	case ICMP_PARAMPROB:
    138 		/* Should contain original IP header. */
    139 		if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
    140 			return false;
    141 		}
    142 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    143 
    144 	case ICMP_ECHOREPLY:
    145 	case ICMP_ECHO:
    146 	case ICMP_TSTAMP:
    147 	case ICMP_TSTAMPREPLY:
    148 	case ICMP_IREQ:
    149 	case ICMP_IREQREPLY:
    150 		/* Should contain ICMP query ID - ensure. */
    151 		offby = offsetof(struct icmp, icmp_id);
    152 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    153 			return false;
    154 		}
    155 		npc->npc_info |= NPC_ICMP_ID;
    156 		return true;
    157 	default:
    158 		break;
    159 	}
    160 	return false;
    161 }
    162 
    163 static bool
    164 npfa_icmp6_inspect(const int type, npf_cache_t *npc)
    165 {
    166 	nbuf_t *nbuf = npc->npc_nbuf;
    167 	u_int offby;
    168 
    169 	/* Per RFC 4443. */
    170 	switch (type) {
    171 	case ICMP6_DST_UNREACH:
    172 	case ICMP6_PACKET_TOO_BIG:
    173 	case ICMP6_TIME_EXCEEDED:
    174 	case ICMP6_PARAM_PROB:
    175 		/* Should contain original IP header. */
    176 		if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
    177 			return false;
    178 		}
    179 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    180 
    181 	case ICMP6_ECHO_REQUEST:
    182 	case ICMP6_ECHO_REPLY:
    183 		/* Should contain ICMP query ID - ensure. */
    184 		offby = offsetof(struct icmp6_hdr, icmp6_id);
    185 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    186 			return false;
    187 		}
    188 		npc->npc_info |= NPC_ICMP_ID;
    189 		return true;
    190 	default:
    191 		break;
    192 	}
    193 	return false;
    194 }
    195 
    196 /*
    197  * npfa_icmp_inspect: ALG ICMP inspector.
    198  *
    199  * => Returns true if "enpc" is filled.
    200  */
    201 static bool
    202 npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
    203 {
    204 	nbuf_t *nbuf = npc->npc_nbuf;
    205 	bool ret;
    206 
    207 	KASSERT(npf_iscached(npc, NPC_IP46));
    208 	KASSERT(npf_iscached(npc, NPC_ICMP));
    209 
    210 	/* Advance to ICMP header. */
    211 	nbuf_reset(nbuf);
    212 	if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
    213 		return false;
    214 	}
    215 	enpc->npc_ctx = npc->npc_ctx;
    216 	enpc->npc_nbuf = nbuf;
    217 	enpc->npc_info = 0;
    218 
    219 	/*
    220 	 * Inspect the ICMP packet.  The relevant data might be in the
    221 	 * embedded packet.  Fill the "enpc" cache, if so.
    222 	 */
    223 	if (npf_iscached(npc, NPC_IP4)) {
    224 		const struct icmp *ic = npc->npc_l4.icmp;
    225 		ret = npfa_icmp4_inspect(ic->icmp_type, enpc);
    226 	} else if (npf_iscached(npc, NPC_IP6)) {
    227 		const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
    228 		ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc);
    229 	} else {
    230 		ret = false;
    231 	}
    232 	if (!ret) {
    233 		return false;
    234 	}
    235 
    236 	/* ICMP ID is the original packet, just indicate it. */
    237 	if (npf_iscached(enpc, NPC_ICMP_ID)) {
    238 		npc->npc_info |= NPC_ICMP_ID;
    239 		return false;
    240 	}
    241 
    242 	/* Indicate that embedded packet is in the cache. */
    243 	return true;
    244 }
    245 
    246 static npf_conn_t *
    247 npfa_icmp_conn(npf_cache_t *npc, int di)
    248 {
    249 	npf_conn_t *conn = NULL;
    250 	npf_cache_t enpc;
    251 
    252 	/* Inspect ICMP packet for an embedded packet. */
    253 	if (!npf_iscached(npc, NPC_ICMP))
    254 		return NULL;
    255 	if (!npfa_icmp_inspect(npc, &enpc))
    256 		goto out;
    257 
    258 	/*
    259 	 * Invert the identifiers of the embedded packet.
    260 	 * If it is ICMP, then ensure ICMP ID.
    261 	 */
    262 	union l4 {
    263 		struct tcphdr th;
    264 		struct udphdr uh;
    265 	} l4;
    266 	bool ret, forw;
    267 
    268 	#define	SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
    269 	SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
    270 
    271 	switch (enpc.npc_proto) {
    272 	case IPPROTO_TCP:
    273 		l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
    274 		l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
    275 		enpc.npc_l4.tcp = &l4.th;
    276 		break;
    277 	case IPPROTO_UDP:
    278 		l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
    279 		l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
    280 		enpc.npc_l4.udp = &l4.uh;
    281 		break;
    282 	case IPPROTO_ICMP: {
    283 		const struct icmp *ic = enpc.npc_l4.icmp;
    284 		ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
    285 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    286 			goto out;
    287 		break;
    288 	}
    289 	case IPPROTO_ICMPV6: {
    290 		const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
    291 		ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
    292 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    293 			goto out;
    294 		break;
    295 	}
    296 	default:
    297 		goto out;
    298 	}
    299 
    300 	/* Lookup a connection using the embedded packet. */
    301 	conn = npf_conn_lookup(&enpc, di, &forw);
    302 
    303 out:
    304 	/*
    305 	 * Recache npc. The nbuf may have been updated as a result of
    306 	 * caching enpc.
    307 	 */
    308 	npf_recache(npc);
    309 	return conn;
    310 }
    311 
    312 /*
    313  * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
    314  * which is embedded in ICMP packet.  Note: backwards stream only.
    315  */
    316 static bool
    317 npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    318 {
    319 	const u_int which = NPF_SRC;
    320 	npf_cache_t enpc;
    321 	struct icmp *ic;
    322 	uint16_t cksum;
    323 
    324 	if (forw || !npf_iscached(npc, NPC_ICMP))
    325 		return false;
    326 
    327 	/*
    328 	 * ICMP: fetch the current checksum we are going to fixup.
    329 	 */
    330 	ic = npc->npc_l4.icmp;
    331 	cksum = ic->icmp_cksum;
    332 
    333 	if (!npfa_icmp_inspect(npc, &enpc))
    334 		goto err;
    335 
    336 	KASSERT(npf_iscached(&enpc, NPC_IP46));
    337 	KASSERT(npf_iscached(&enpc, NPC_LAYER4));
    338 
    339 	CTASSERT(offsetof(struct icmp, icmp_cksum) ==
    340 	    offsetof(struct icmp6_hdr, icmp6_cksum));
    341 
    342 	/*
    343 	 * Fetch the IP and port in the _embedded_ packet.  Also, fetch
    344 	 * the IPv4 and TCP/UDP checksums before they are rewritten.
    345 	 */
    346 	const int proto = enpc.npc_proto;
    347 	uint16_t ipcksum = 0, l4cksum = 0;
    348 	in_port_t old_port = 0;
    349 
    350 	if (npf_iscached(&enpc, NPC_IP4)) {
    351 		const struct ip *eip = enpc.npc_ip.v4;
    352 		ipcksum = eip->ip_sum;
    353 	}
    354 	switch (proto) {
    355 	case IPPROTO_TCP: {
    356 		const struct tcphdr *th = enpc.npc_l4.tcp;
    357 		old_port = th->th_sport;
    358 		l4cksum = th->th_sum;
    359 		break;
    360 	}
    361 	case IPPROTO_UDP: {
    362 		const struct udphdr *uh = enpc.npc_l4.udp;
    363 		old_port = uh->uh_sport;
    364 		l4cksum = uh->uh_sum;
    365 		break;
    366 	}
    367 	case IPPROTO_ICMP:
    368 	case IPPROTO_ICMPV6:
    369 		break;
    370 	default:
    371 		goto err;
    372 	}
    373 
    374 	/*
    375 	 * Get the original IP address and port.
    376 	 * Calculate the part of the ICMP checksum fixup.
    377 	 */
    378 	npf_addr_t *addr;
    379 	in_port_t port;
    380 
    381 	npf_nat_getorig(nt, &addr, &port);
    382 
    383 	cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
    384 	if (port) {
    385 		cksum = npf_fixup16_cksum(cksum, old_port, port);
    386 	}
    387 
    388 	/*
    389 	 * Translate the embedded packet.  The following changes will
    390 	 * be performed by npf_napt_rwr():
    391 	 *
    392 	 *	1) Rewrite the IP address and, if not ICMP, port.
    393 	 *	2) Rewrite the TCP/UDP checksum (if not ICMP).
    394 	 *	3) Rewrite the IPv4 checksum for (1) and (2).
    395 	 *
    396 	 * XXX: Assumes NPF_NATOUT (source address/port).  Currently,
    397 	 * npfa_icmp_match() matches only for the PFIL_OUT traffic.
    398 	 */
    399 	if (npf_napt_rwr(&enpc, which, addr, port)) {
    400 		goto err;
    401 	}
    402 
    403 	/*
    404 	 * Finally, finish the ICMP checksum fixup: include the checksum
    405 	 * changes in the embedded packet.
    406 	 */
    407 	if (npf_iscached(&enpc, NPC_IP4)) {
    408 		const struct ip *eip = enpc.npc_ip.v4;
    409 		cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
    410 	}
    411 	switch (proto) {
    412 	case IPPROTO_TCP: {
    413 		const struct tcphdr *th = enpc.npc_l4.tcp;
    414 		cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
    415 		break;
    416 	}
    417 	case IPPROTO_UDP:
    418 		if (l4cksum) {
    419 			const struct udphdr *uh = enpc.npc_l4.udp;
    420 			cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
    421 		}
    422 		break;
    423 	}
    424 	npf_recache(npc);
    425 	KASSERT(npf_iscached(npc, NPC_ICMP));
    426 	ic = npc->npc_l4.icmp;
    427 	ic->icmp_cksum = cksum;
    428 	return true;
    429 
    430 err:
    431 	/*
    432 	 * Recache npc. The nbuf may have been updated as a result of
    433 	 * caching enpc.
    434 	 */
    435 	npf_recache(npc);
    436 	return false;
    437 }
    438 
    439 /*
    440  * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
    441  * and module interface.
    442  */
    443 
    444 static int
    445 npf_alg_icmp_init(void)
    446 {
    447 	static const npfa_funcs_t icmp = {
    448 		.match		= npfa_icmp_match,
    449 		.translate	= npfa_icmp_nat,
    450 		.inspect	= npfa_icmp_conn,
    451 	};
    452 	alg_icmp = npf_alg_register(npf_getkernctx(), "icmp", &icmp);
    453 	return alg_icmp ? 0 : ENOMEM;
    454 }
    455 
    456 static int
    457 npf_alg_icmp_fini(void)
    458 {
    459 	KASSERT(alg_icmp != NULL);
    460 	return npf_alg_unregister(npf_getkernctx(), alg_icmp);
    461 }
    462 
    463 static int
    464 npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
    465 {
    466 	switch (cmd) {
    467 	case MODULE_CMD_INIT:
    468 		return npf_alg_icmp_init();
    469 	case MODULE_CMD_FINI:
    470 		return npf_alg_icmp_fini();
    471 	case MODULE_CMD_AUTOUNLOAD:
    472 		return EBUSY;
    473 	default:
    474 		return ENOTTY;
    475 	}
    476 	return 0;
    477 }
    478