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