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