Home | History | Annotate | Line # | Download | only in npf
npf_alg_icmp.c revision 1.3.2.2
      1 /*	$NetBSD: npf_alg_icmp.c,v 1.3.2.2 2010/10/09 03:32:37 yamt 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.3.2.2 2010/10/09 03:32:37 yamt Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/kernel.h>
     42 #endif
     43 #include <sys/module.h>
     44 #include <sys/pool.h>
     45 
     46 #include <netinet/in_systm.h>
     47 #include <netinet/in.h>
     48 #include <netinet/ip.h>
     49 #include <netinet/tcp.h>
     50 #include <netinet/udp.h>
     51 #include <netinet/ip_icmp.h>
     52 #include <net/pfil.h>
     53 
     54 #include "npf_impl.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 TTL < 50.
     63  */
     64 
     65 #define	TR_BASE_PORT	33434
     66 #define	TR_PORT_RANGE	33484
     67 #define	TR_MAX_TTL	50
     68 
     69 static npf_alg_t *	alg_icmp;
     70 
     71 static bool		npfa_icmp_match(npf_cache_t *, nbuf_t *, void *);
     72 static bool		npfa_icmp_natin(npf_cache_t *, nbuf_t *, void *);
     73 static bool		npfa_icmp_session(npf_cache_t *, nbuf_t *, void *);
     74 
     75 /*
     76  * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
     77  * and module interface.
     78  */
     79 
     80 static int
     81 npf_alg_icmp_init(void)
     82 {
     83 
     84 	alg_icmp = npf_alg_register(npfa_icmp_match, NULL,
     85 	    npfa_icmp_natin, npfa_icmp_session);
     86 	KASSERT(alg_icmp != NULL);
     87 	return 0;
     88 }
     89 
     90 static int
     91 npf_alg_icmp_fini(void)
     92 {
     93 
     94 	KASSERT(alg_icmp != NULL);
     95 	return npf_alg_unregister(alg_icmp);
     96 }
     97 
     98 static int
     99 npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
    100 {
    101 
    102 	switch (cmd) {
    103 	case MODULE_CMD_INIT:
    104 		return npf_alg_icmp_init();
    105 	case MODULE_CMD_FINI:
    106 		return npf_alg_icmp_fini();
    107 	default:
    108 		return ENOTTY;
    109 	}
    110 	return 0;
    111 }
    112 
    113 /*
    114  * npfa_icmp_match: ALG matching inspector, determines ALG case and
    115  * establishes a session for "backwards" stream.
    116  */
    117 static bool
    118 npfa_icmp_match(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
    119 {
    120 	const int proto = npc->npc_proto;
    121 	void *n_ptr = nbuf_dataptr(nbuf);
    122 	u_int offby;
    123 	uint8_t ttl;
    124 
    125 	/* Handle TCP/UDP traceroute - check for port range. */
    126 	if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
    127 		return false;
    128 	}
    129 	KASSERT(npf_iscached(npc, NPC_PORTS));
    130 	in_port_t dport = ntohs(npc->npc_dport);
    131 	if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
    132 		return false;
    133 	}
    134 
    135 	/* Check for low TTL. */
    136 	offby = offsetof(struct ip, ip_ttl);
    137 	if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint8_t), &ttl))
    138 		return false;
    139 	if (ttl > TR_MAX_TTL)
    140 		return false;
    141 
    142 	/* Associate ALG with translation entry. */
    143 	npf_nat_t *nt = ntptr;
    144 	npf_nat_setalg(nt, alg_icmp, 0);
    145 	return true;
    146 }
    147 
    148 /*
    149  * npf_icmp_uniqid: retrieve unique identifiers - either ICMP query ID
    150  * or TCP/UDP ports of the original packet, which is embedded.
    151  */
    152 static inline bool
    153 npf_icmp_uniqid(const int type, npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    154 {
    155 	u_int offby;
    156 
    157 	/* Per RFC 792. */
    158 	switch (type) {
    159 	case ICMP_UNREACH:
    160 	case ICMP_SOURCEQUENCH:
    161 	case ICMP_REDIRECT:
    162 	case ICMP_TIMXCEED:
    163 	case ICMP_PARAMPROB:
    164 		/* Should contain original IP header. */
    165 		offby = offsetof(struct icmp, icmp_ip);
    166 		if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
    167 			return false;
    168 		}
    169 		/* Fetch into the cache. */
    170 		if (!npf_ip4_proto(npc, nbuf, n_ptr)) {
    171 			return false;
    172 		}
    173 		const int proto = npc->npc_proto;
    174 		if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
    175 			return false;
    176 		}
    177 		if (!npf_fetch_ip4addrs(npc, nbuf, n_ptr)) {
    178 			return false;
    179 		}
    180 		if (!npf_fetch_ports(npc, nbuf, n_ptr, proto)) {
    181 			return false;
    182 		}
    183 		return true;
    184 
    185 	case ICMP_ECHOREPLY:
    186 	case ICMP_ECHO:
    187 	case ICMP_TSTAMP:
    188 	case ICMP_TSTAMPREPLY:
    189 	case ICMP_IREQ:
    190 	case ICMP_IREQREPLY:
    191 		/* Should contain ICMP query ID. */
    192 		offby = offsetof(struct icmp, icmp_id);
    193 		if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint16_t),
    194 		    &npc->npc_icmp_id)) {
    195 			return false;
    196 		}
    197 		npc->npc_info |= NPC_ICMP_ID;
    198 		return true;
    199 	default:
    200 		break;
    201 	}
    202 	/* No unique IDs. */
    203 	return false;
    204 }
    205 
    206 /*
    207  * npfa_icmp_session: ALG session inspector, determines unique identifiers.
    208  */
    209 static bool
    210 npfa_icmp_session(npf_cache_t *npc, nbuf_t *nbuf, void *keyptr)
    211 {
    212 	npf_cache_t *key = keyptr;
    213 	void *n_ptr;
    214 
    215 	/* ICMP? Get unique identifiers from ICMP packet. */
    216 	if (npc->npc_proto != IPPROTO_ICMP) {
    217 		return false;
    218 	}
    219 	KASSERT(npf_iscached(npc, NPC_IP46 | NPC_ICMP));
    220 	key->npc_info = NPC_ICMP;
    221 
    222 	/* Advance to ICMP header. */
    223 	n_ptr = nbuf_dataptr(nbuf);
    224 	if ((n_ptr = nbuf_advance(&nbuf, n_ptr, npc->npc_hlen)) == NULL) {
    225 		return false;
    226 	}
    227 
    228 	/* Fetch into the separate (key) cache. */
    229 	if (!npf_icmp_uniqid(npc->npc_icmp_type, key, nbuf, n_ptr)) {
    230 		return false;
    231 	}
    232 
    233 	if (npf_iscached(key, NPC_ICMP_ID)) {
    234 		/* Construct the key. */
    235 		key->npc_proto = npc->npc_proto;
    236 		key->npc_dir = npc->npc_dir;
    237 		/* Save IP addresses. */
    238 		key->npc_srcip = npc->npc_srcip;
    239 		key->npc_dstip = npc->npc_dstip;
    240 		key->npc_info |= NPC_IP46 | NPC_ADDRS | NPC_PORTS;
    241 		/* Fake ports with ICMP query IDs. */
    242 		key->npc_sport = key->npc_icmp_id;
    243 		key->npc_dport = key->npc_icmp_id;
    244 	} else {
    245 		in_addr_t addr;
    246 		in_port_t port;
    247 		/*
    248 		 * Embedded IP packet is the original of "forwards" stream.
    249 		 * We should imitate the "backwards" stream for inspection.
    250 		 */
    251 		KASSERT(npf_iscached(key, NPC_IP46 | NPC_ADDRS | NPC_PORTS));
    252 		addr = key->npc_srcip;
    253 		port = key->npc_sport;
    254 		key->npc_srcip = key->npc_dstip;
    255 		key->npc_dstip = addr;
    256 		key->npc_sport = key->npc_dport;
    257 		key->npc_dport = port;
    258 	}
    259 	return true;
    260 }
    261 
    262 /*
    263  * npfa_icmp_natin: ALG inbound translation inspector, rewrite IP address
    264  * in the IP header, which is embedded in ICMP packet.
    265  */
    266 static bool
    267 npfa_icmp_natin(npf_cache_t *npc, nbuf_t *nbuf, void *ntptr)
    268 {
    269 	void *n_ptr = nbuf_dataptr(nbuf);
    270 	npf_cache_t enpc;
    271 	u_int offby;
    272 	uint16_t cksum;
    273 
    274 	/* XXX: Duplicated work. */
    275 	if (!npfa_icmp_session(npc, nbuf, &enpc)) {
    276 		return false;
    277 	}
    278 	KASSERT(npf_iscached(&enpc, NPC_IP46 | NPC_ADDRS | NPC_PORTS));
    279 
    280 	/* Advance to ICMP checksum and fetch it. */
    281 	offby = npc->npc_hlen + offsetof(struct icmp, icmp_cksum);
    282 	if (nbuf_advfetch(&nbuf, &n_ptr, offby, sizeof(uint16_t), &cksum)) {
    283 		return false;
    284 	}
    285 
    286 	/* Save the data for checksum update later. */
    287 	void *cnbuf = nbuf, *cnptr = n_ptr;
    288 	uint16_t ecksum = enpc.npc_ipsum;
    289 
    290 	/* Advance to the original IP header, which is embedded after ICMP. */
    291 	offby = offsetof(struct icmp, icmp_ip) -
    292 	    offsetof(struct icmp, icmp_cksum);
    293 	if ((n_ptr = nbuf_advance(&nbuf, n_ptr, offby)) == NULL) {
    294 		return false;
    295 	}
    296 
    297 	/*
    298 	 * Rewrite source IP address and port of the embedded IP header,
    299 	 * which represents original packet - therefore passing PFIL_OUT.
    300 	 */
    301 	npf_nat_t *nt = ntptr;
    302 	in_addr_t addr;
    303 	in_port_t port;
    304 
    305 	npf_nat_getorig(nt, &addr, &port);
    306 
    307 	if (!npf_rwrip(&enpc, nbuf, n_ptr, PFIL_OUT, addr)) {
    308 		return false;
    309 	}
    310 	if (!npf_rwrport(&enpc, nbuf, n_ptr, PFIL_OUT, port, addr)) {
    311 		return false;
    312 	}
    313 
    314 	/*
    315 	 * Fixup and update ICMP checksum.
    316 	 * Note: npf_rwrip() has updated the IP checksum.
    317 	 */
    318 	cksum = npf_fixup32_cksum(cksum, enpc.npc_srcip, addr);
    319 	cksum = npf_fixup16_cksum(cksum, enpc.npc_sport, port);
    320 	cksum = npf_fixup16_cksum(cksum, ecksum, enpc.npc_ipsum);
    321 	/* FIXME: Updated UDP/TCP checksum joins-in too., when != 0, sigh. */
    322 	if (nbuf_store_datum(cnbuf, cnptr, sizeof(uint16_t), &cksum)){
    323 		return false;
    324 	}
    325 	return true;
    326 }
    327