Home | History | Annotate | Line # | Download | only in npf
npf_alg_icmp.c revision 1.25.2.1
      1 /*	$NetBSD: npf_alg_icmp.c,v 1.25.2.1 2018/03/15 09:12:06 pgoyette 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.25.2.1 2018/03/15 09:12:06 pgoyette 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_cache_t enpc;
    250 
    251 	/* Inspect ICMP packet for an embedded packet. */
    252 	if (!npf_iscached(npc, NPC_ICMP))
    253 		return NULL;
    254 	if (!npfa_icmp_inspect(npc, &enpc))
    255 		return NULL;
    256 
    257 	/*
    258 	 * Invert the identifiers of the embedded packet.
    259 	 * If it is ICMP, then ensure ICMP ID.
    260 	 */
    261 	union l4 {
    262 		struct tcphdr th;
    263 		struct udphdr uh;
    264 	} l4;
    265 	bool ret, forw;
    266 
    267 	#define	SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
    268 	SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
    269 
    270 	switch (enpc.npc_proto) {
    271 	case IPPROTO_TCP:
    272 		l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
    273 		l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
    274 		enpc.npc_l4.tcp = &l4.th;
    275 		break;
    276 	case IPPROTO_UDP:
    277 		l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
    278 		l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
    279 		enpc.npc_l4.udp = &l4.uh;
    280 		break;
    281 	case IPPROTO_ICMP: {
    282 		const struct icmp *ic = enpc.npc_l4.icmp;
    283 		ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
    284 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    285 			return false;
    286 		break;
    287 	}
    288 	case IPPROTO_ICMPV6: {
    289 		const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
    290 		ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
    291 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    292 			return false;
    293 		break;
    294 	}
    295 	default:
    296 		return false;
    297 	}
    298 
    299 	/* Lookup a connection using the embedded packet. */
    300 	return npf_conn_lookup(&enpc, di, &forw);
    301 }
    302 
    303 /*
    304  * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
    305  * which is embedded in ICMP packet.  Note: backwards stream only.
    306  */
    307 static bool
    308 npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    309 {
    310 	const u_int which = NPF_SRC;
    311 	npf_cache_t enpc;
    312 
    313 	if (forw || !npf_iscached(npc, NPC_ICMP))
    314 		return false;
    315 	if (!npfa_icmp_inspect(npc, &enpc))
    316 		return false;
    317 
    318 	KASSERT(npf_iscached(&enpc, NPC_IP46));
    319 	KASSERT(npf_iscached(&enpc, NPC_LAYER4));
    320 
    321 	/*
    322 	 * ICMP: fetch the current checksum we are going to fixup.
    323 	 */
    324 	struct icmp *ic = npc->npc_l4.icmp;
    325 	uint16_t cksum = ic->icmp_cksum;
    326 
    327 	CTASSERT(offsetof(struct icmp, icmp_cksum) ==
    328 	    offsetof(struct icmp6_hdr, icmp6_cksum));
    329 
    330 	/*
    331 	 * Fetch the IP and port in the _embedded_ packet.  Also, fetch
    332 	 * the IPv4 and TCP/UDP checksums before they are rewritten.
    333 	 */
    334 	const int proto = enpc.npc_proto;
    335 	uint16_t ipcksum = 0, l4cksum = 0;
    336 	in_port_t old_port = 0;
    337 
    338 	if (npf_iscached(&enpc, NPC_IP4)) {
    339 		const struct ip *eip = enpc.npc_ip.v4;
    340 		ipcksum = eip->ip_sum;
    341 	}
    342 	switch (proto) {
    343 	case IPPROTO_TCP: {
    344 		const struct tcphdr *th = enpc.npc_l4.tcp;
    345 		old_port = th->th_sport;
    346 		l4cksum = th->th_sum;
    347 		break;
    348 	}
    349 	case IPPROTO_UDP: {
    350 		const struct udphdr *uh = enpc.npc_l4.udp;
    351 		old_port = uh->uh_sport;
    352 		l4cksum = uh->uh_sum;
    353 		break;
    354 	}
    355 	case IPPROTO_ICMP:
    356 	case IPPROTO_ICMPV6:
    357 		break;
    358 	default:
    359 		return false;
    360 	}
    361 
    362 	/*
    363 	 * Get the original IP address and port.
    364 	 * Calculate the part of the ICMP checksum fixup.
    365 	 */
    366 	npf_addr_t *addr;
    367 	in_port_t port;
    368 
    369 	npf_nat_getorig(nt, &addr, &port);
    370 
    371 	cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
    372 	if (port) {
    373 		cksum = npf_fixup16_cksum(cksum, old_port, port);
    374 	}
    375 
    376 	/*
    377 	 * Translate the embedded packet.  The following changes will
    378 	 * be performed by npf_napt_rwr():
    379 	 *
    380 	 *	1) Rewrite the IP address and, if not ICMP, port.
    381 	 *	2) Rewrite the TCP/UDP checksum (if not ICMP).
    382 	 *	3) Rewrite the IPv4 checksum for (1) and (2).
    383 	 *
    384 	 * XXX: Assumes NPF_NATOUT (source address/port).  Currently,
    385 	 * npfa_icmp_match() matches only for the PFIL_OUT traffic.
    386 	 */
    387 	if (npf_napt_rwr(&enpc, which, addr, port)) {
    388 		return false;
    389 	}
    390 
    391 	/*
    392 	 * Finally, finish the ICMP checksum fixup: include the checksum
    393 	 * changes in the embedded packet.
    394 	 */
    395 	if (npf_iscached(&enpc, NPC_IP4)) {
    396 		const struct ip *eip = enpc.npc_ip.v4;
    397 		cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
    398 	}
    399 	switch (proto) {
    400 	case IPPROTO_TCP: {
    401 		const struct tcphdr *th = enpc.npc_l4.tcp;
    402 		cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
    403 		break;
    404 	}
    405 	case IPPROTO_UDP:
    406 		if (l4cksum) {
    407 			const struct udphdr *uh = enpc.npc_l4.udp;
    408 			cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
    409 		}
    410 		break;
    411 	}
    412 	ic->icmp_cksum = cksum;
    413 	return true;
    414 }
    415 
    416 /*
    417  * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
    418  * and module interface.
    419  */
    420 
    421 static int
    422 npf_alg_icmp_init(void)
    423 {
    424 	static const npfa_funcs_t icmp = {
    425 		.match		= npfa_icmp_match,
    426 		.translate	= npfa_icmp_nat,
    427 		.inspect	= npfa_icmp_conn,
    428 	};
    429 	alg_icmp = npf_alg_register(npf_getkernctx(), "icmp", &icmp);
    430 	return alg_icmp ? 0 : ENOMEM;
    431 }
    432 
    433 static int
    434 npf_alg_icmp_fini(void)
    435 {
    436 	KASSERT(alg_icmp != NULL);
    437 	return npf_alg_unregister(npf_getkernctx(), alg_icmp);
    438 }
    439 
    440 static int
    441 npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
    442 {
    443 	switch (cmd) {
    444 	case MODULE_CMD_INIT:
    445 		return npf_alg_icmp_init();
    446 	case MODULE_CMD_FINI:
    447 		return npf_alg_icmp_fini();
    448 	case MODULE_CMD_AUTOUNLOAD:
    449 		return EBUSY;
    450 	default:
    451 		return ENOTTY;
    452 	}
    453 	return 0;
    454 }
    455