Home | History | Annotate | Line # | Download | only in npf
npf_alg_icmp.c revision 1.12.2.4
      1  1.12.2.3     tls /*	$NetBSD: npf_alg_icmp.c,v 1.12.2.4 2014/08/20 00:04:35 tls Exp $	*/
      2       1.1   rmind 
      3       1.1   rmind /*-
      4       1.1   rmind  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5       1.1   rmind  * All rights reserved.
      6       1.1   rmind  *
      7       1.1   rmind  * This material is based upon work partially supported by The
      8       1.1   rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9       1.1   rmind  *
     10       1.1   rmind  * Redistribution and use in source and binary forms, with or without
     11       1.1   rmind  * modification, are permitted provided that the following conditions
     12       1.1   rmind  * are met:
     13       1.1   rmind  * 1. Redistributions of source code must retain the above copyright
     14       1.1   rmind  *    notice, this list of conditions and the following disclaimer.
     15       1.1   rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   rmind  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   rmind  *    documentation and/or other materials provided with the distribution.
     18       1.1   rmind  *
     19       1.1   rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   rmind  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   rmind  */
     31       1.1   rmind 
     32       1.1   rmind /*
     33       1.1   rmind  * NPF ALG for ICMP and traceroute translations.
     34       1.1   rmind  */
     35       1.1   rmind 
     36       1.1   rmind #include <sys/cdefs.h>
     37  1.12.2.3     tls __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.12.2.4 2014/08/20 00:04:35 tls Exp $");
     38       1.1   rmind 
     39       1.1   rmind #include <sys/param.h>
     40       1.1   rmind #include <sys/module.h>
     41       1.1   rmind 
     42       1.1   rmind #include <netinet/in_systm.h>
     43       1.1   rmind #include <netinet/in.h>
     44       1.1   rmind #include <netinet/ip.h>
     45       1.1   rmind #include <netinet/tcp.h>
     46       1.1   rmind #include <netinet/udp.h>
     47       1.1   rmind #include <netinet/ip_icmp.h>
     48      1.11     spz #include <netinet/icmp6.h>
     49       1.1   rmind #include <net/pfil.h>
     50       1.1   rmind 
     51       1.1   rmind #include "npf_impl.h"
     52  1.12.2.4     tls #include "npf_conn.h"
     53       1.1   rmind 
     54       1.1   rmind MODULE(MODULE_CLASS_MISC, npf_alg_icmp, "npf");
     55       1.1   rmind 
     56       1.1   rmind /*
     57       1.1   rmind  * Traceroute criteria.
     58       1.1   rmind  *
     59       1.1   rmind  * IANA assigned base port: 33434.  However, common practice is to increase
     60  1.12.2.2     tls  * the port, thus monitor [33434-33484] range.  Additional filter is low TTL.
     61       1.1   rmind  */
     62       1.1   rmind 
     63       1.1   rmind #define	TR_BASE_PORT	33434
     64       1.1   rmind #define	TR_PORT_RANGE	33484
     65  1.12.2.2     tls #define	TR_MAX_TTL	48
     66       1.1   rmind 
     67       1.6   rmind static npf_alg_t *	alg_icmp	__read_mostly;
     68       1.1   rmind 
     69       1.1   rmind /*
     70  1.12.2.4     tls  * npfa_icmp_match: matching inspector determines ALG case and associates
     71  1.12.2.4     tls  * our ALG with the NAT entry.
     72       1.1   rmind  */
     73       1.1   rmind static bool
     74  1.12.2.4     tls npfa_icmp_match(npf_cache_t *npc, npf_nat_t *nt, int di)
     75       1.1   rmind {
     76  1.12.2.2     tls 	const int proto = npc->npc_proto;
     77  1.12.2.2     tls 	const struct ip *ip = npc->npc_ip.v4;
     78       1.4   rmind 	in_port_t dport;
     79       1.4   rmind 
     80       1.7  zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
     81       1.7  zoltan 	KASSERT(npf_iscached(npc, NPC_LAYER4));
     82       1.4   rmind 
     83  1.12.2.4     tls 	/* Check for low TTL.  Also, we support outbound NAT only. */
     84  1.12.2.4     tls 	if (ip->ip_ttl > TR_MAX_TTL || di != PFIL_OUT) {
     85       1.6   rmind 		return false;
     86       1.6   rmind 	}
     87       1.6   rmind 
     88  1.12.2.2     tls 	switch (proto) {
     89  1.12.2.2     tls 	case IPPROTO_TCP: {
     90  1.12.2.2     tls 		const struct tcphdr *th = npc->npc_l4.tcp;
     91       1.4   rmind 		dport = ntohs(th->th_dport);
     92  1.12.2.2     tls 		break;
     93  1.12.2.2     tls 	}
     94  1.12.2.2     tls 	case IPPROTO_UDP: {
     95  1.12.2.2     tls 		const struct udphdr *uh = npc->npc_l4.udp;
     96       1.4   rmind 		dport = ntohs(uh->uh_dport);
     97  1.12.2.2     tls 		break;
     98  1.12.2.2     tls 	}
     99  1.12.2.2     tls 	case IPPROTO_ICMP:
    100  1.12.2.2     tls 	case IPPROTO_ICMPV6:
    101  1.12.2.2     tls 		/* Just to pass the test below. */
    102  1.12.2.2     tls 		dport = TR_BASE_PORT;
    103  1.12.2.2     tls 		break;
    104  1.12.2.2     tls 	default:
    105       1.4   rmind 		return false;
    106       1.4   rmind 	}
    107       1.1   rmind 
    108       1.1   rmind 	/* Handle TCP/UDP traceroute - check for port range. */
    109       1.1   rmind 	if (dport < TR_BASE_PORT || dport > TR_PORT_RANGE) {
    110       1.1   rmind 		return false;
    111       1.1   rmind 	}
    112       1.1   rmind 
    113       1.1   rmind 	/* Associate ALG with translation entry. */
    114       1.1   rmind 	npf_nat_setalg(nt, alg_icmp, 0);
    115       1.1   rmind 	return true;
    116       1.1   rmind }
    117       1.1   rmind 
    118       1.1   rmind /*
    119  1.12.2.2     tls  * npfa_icmp{4,6}_inspect: retrieve unique identifiers - either ICMP query
    120  1.12.2.2     tls  * ID or TCP/UDP ports of the original packet, which is embedded.
    121       1.1   rmind  */
    122  1.12.2.1     tls 
    123       1.5   rmind static bool
    124  1.12.2.4     tls npfa_icmp4_inspect(const int type, npf_cache_t *npc)
    125       1.1   rmind {
    126  1.12.2.4     tls 	nbuf_t *nbuf = npc->npc_nbuf;
    127  1.12.2.1     tls 	u_int offby;
    128      1.11     spz 
    129  1.12.2.1     tls 	/* Per RFC 792. */
    130  1.12.2.1     tls 	switch (type) {
    131  1.12.2.1     tls 	case ICMP_UNREACH:
    132  1.12.2.1     tls 	case ICMP_SOURCEQUENCH:
    133  1.12.2.1     tls 	case ICMP_REDIRECT:
    134  1.12.2.1     tls 	case ICMP_TIMXCEED:
    135  1.12.2.1     tls 	case ICMP_PARAMPROB:
    136  1.12.2.2     tls 		if (npc == NULL) {
    137  1.12.2.1     tls 			return false;
    138  1.12.2.1     tls 		}
    139  1.12.2.2     tls 		/* Should contain original IP header. */
    140  1.12.2.2     tls 		if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
    141  1.12.2.1     tls 			return false;
    142       1.1   rmind 		}
    143  1.12.2.4     tls 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    144  1.12.2.1     tls 
    145  1.12.2.1     tls 	case ICMP_ECHOREPLY:
    146  1.12.2.1     tls 	case ICMP_ECHO:
    147  1.12.2.1     tls 	case ICMP_TSTAMP:
    148  1.12.2.1     tls 	case ICMP_TSTAMPREPLY:
    149  1.12.2.1     tls 	case ICMP_IREQ:
    150  1.12.2.1     tls 	case ICMP_IREQREPLY:
    151  1.12.2.2     tls 		/* Should contain ICMP query ID - ensure. */
    152  1.12.2.1     tls 		offby = offsetof(struct icmp, icmp_id);
    153  1.12.2.2     tls 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    154  1.12.2.1     tls 			return false;
    155  1.12.2.1     tls 		}
    156  1.12.2.1     tls 		npc->npc_info |= NPC_ICMP_ID;
    157  1.12.2.1     tls 		return true;
    158  1.12.2.1     tls 	default:
    159  1.12.2.1     tls 		break;
    160      1.11     spz 	}
    161  1.12.2.1     tls 	return false;
    162  1.12.2.1     tls }
    163  1.12.2.1     tls 
    164  1.12.2.1     tls static bool
    165  1.12.2.4     tls npfa_icmp6_inspect(const int type, npf_cache_t *npc)
    166  1.12.2.1     tls {
    167  1.12.2.4     tls 	nbuf_t *nbuf = npc->npc_nbuf;
    168  1.12.2.1     tls 	u_int offby;
    169  1.12.2.1     tls 
    170  1.12.2.1     tls 	/* Per RFC 4443. */
    171  1.12.2.1     tls 	switch (type) {
    172  1.12.2.1     tls 	case ICMP6_DST_UNREACH:
    173  1.12.2.1     tls 	case ICMP6_PACKET_TOO_BIG:
    174  1.12.2.1     tls 	case ICMP6_TIME_EXCEEDED:
    175  1.12.2.1     tls 	case ICMP6_PARAM_PROB:
    176  1.12.2.2     tls 		if (npc == NULL) {
    177  1.12.2.1     tls 			return false;
    178  1.12.2.1     tls 		}
    179  1.12.2.2     tls 		/* Should contain original IP header. */
    180  1.12.2.2     tls 		if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
    181  1.12.2.1     tls 			return false;
    182       1.1   rmind 		}
    183  1.12.2.4     tls 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    184  1.12.2.1     tls 
    185  1.12.2.1     tls 	case ICMP6_ECHO_REQUEST:
    186  1.12.2.1     tls 	case ICMP6_ECHO_REPLY:
    187  1.12.2.2     tls 		/* Should contain ICMP query ID - ensure. */
    188  1.12.2.1     tls 		offby = offsetof(struct icmp6_hdr, icmp6_id);
    189  1.12.2.2     tls 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    190  1.12.2.1     tls 			return false;
    191  1.12.2.1     tls 		}
    192  1.12.2.1     tls 		npc->npc_info |= NPC_ICMP_ID;
    193  1.12.2.1     tls 		return true;
    194  1.12.2.1     tls 	default:
    195  1.12.2.1     tls 		break;
    196       1.1   rmind 	}
    197       1.1   rmind 	return false;
    198       1.1   rmind }
    199       1.1   rmind 
    200       1.1   rmind /*
    201  1.12.2.4     tls  * npfa_icmp_inspect: ALG ICMP inspector.
    202  1.12.2.2     tls  *
    203  1.12.2.2     tls  * => Returns true if "enpc" is filled.
    204       1.1   rmind  */
    205       1.1   rmind static bool
    206  1.12.2.4     tls npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
    207       1.1   rmind {
    208  1.12.2.4     tls 	nbuf_t *nbuf = npc->npc_nbuf;
    209  1.12.2.1     tls 	bool ret;
    210  1.12.2.1     tls 
    211  1.12.2.2     tls 	KASSERT(npf_iscached(npc, NPC_IP46));
    212       1.4   rmind 	KASSERT(npf_iscached(npc, NPC_ICMP));
    213       1.1   rmind 
    214       1.1   rmind 	/* Advance to ICMP header. */
    215  1.12.2.2     tls 	nbuf_reset(nbuf);
    216  1.12.2.2     tls 	if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
    217       1.1   rmind 		return false;
    218       1.1   rmind 	}
    219  1.12.2.4     tls 	enpc->npc_nbuf = nbuf;
    220  1.12.2.2     tls 	enpc->npc_info = 0;
    221       1.1   rmind 
    222  1.12.2.1     tls 	/*
    223  1.12.2.2     tls 	 * Inspect the ICMP packet.  The relevant data might be in the
    224  1.12.2.2     tls 	 * embedded packet.  Fill the "enpc" cache, if so.
    225  1.12.2.1     tls 	 */
    226  1.12.2.1     tls 	if (npf_iscached(npc, NPC_IP4)) {
    227  1.12.2.2     tls 		const struct icmp *ic = npc->npc_l4.icmp;
    228  1.12.2.4     tls 		ret = npfa_icmp4_inspect(ic->icmp_type, enpc);
    229  1.12.2.1     tls 	} else if (npf_iscached(npc, NPC_IP6)) {
    230  1.12.2.2     tls 		const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
    231  1.12.2.4     tls 		ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc);
    232  1.12.2.1     tls 	} else {
    233  1.12.2.1     tls 		ret = false;
    234  1.12.2.1     tls 	}
    235  1.12.2.1     tls 	if (!ret) {
    236       1.1   rmind 		return false;
    237       1.1   rmind 	}
    238       1.1   rmind 
    239  1.12.2.2     tls 	/* ICMP ID is the original packet, just indicate it. */
    240  1.12.2.2     tls 	if (npf_iscached(enpc, NPC_ICMP_ID)) {
    241       1.4   rmind 		npc->npc_info |= NPC_ICMP_ID;
    242       1.4   rmind 		return false;
    243       1.1   rmind 	}
    244       1.4   rmind 
    245  1.12.2.2     tls 	/* Indicate that embedded packet is in the cache. */
    246  1.12.2.2     tls 	return true;
    247  1.12.2.2     tls }
    248  1.12.2.2     tls 
    249  1.12.2.4     tls static npf_conn_t *
    250  1.12.2.4     tls npfa_icmp_conn(npf_cache_t *npc, int di)
    251  1.12.2.2     tls {
    252  1.12.2.2     tls 	npf_cache_t enpc;
    253  1.12.2.2     tls 
    254  1.12.2.2     tls 	/* Inspect ICMP packet for an embedded packet. */
    255  1.12.2.2     tls 	if (!npf_iscached(npc, NPC_ICMP))
    256  1.12.2.2     tls 		return NULL;
    257  1.12.2.4     tls 	if (!npfa_icmp_inspect(npc, &enpc))
    258  1.12.2.2     tls 		return NULL;
    259  1.12.2.2     tls 
    260       1.4   rmind 	/*
    261  1.12.2.2     tls 	 * Invert the identifiers of the embedded packet.
    262  1.12.2.2     tls 	 * If it is ICMP, then ensure ICMP ID.
    263       1.4   rmind 	 */
    264  1.12.2.2     tls 	union l4 {
    265  1.12.2.2     tls 		struct tcphdr th;
    266  1.12.2.2     tls 		struct udphdr uh;
    267  1.12.2.2     tls 	} l4;
    268  1.12.2.2     tls 	bool ret, forw;
    269  1.12.2.2     tls 
    270  1.12.2.2     tls 	#define	SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
    271  1.12.2.4     tls 	SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
    272  1.12.2.2     tls 
    273  1.12.2.2     tls 	switch (enpc.npc_proto) {
    274  1.12.2.2     tls 	case IPPROTO_TCP:
    275  1.12.2.2     tls 		l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
    276  1.12.2.2     tls 		l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
    277  1.12.2.2     tls 		enpc.npc_l4.tcp = &l4.th;
    278  1.12.2.2     tls 		break;
    279  1.12.2.2     tls 	case IPPROTO_UDP:
    280  1.12.2.2     tls 		l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
    281  1.12.2.2     tls 		l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
    282  1.12.2.2     tls 		enpc.npc_l4.udp = &l4.uh;
    283  1.12.2.2     tls 		break;
    284  1.12.2.2     tls 	case IPPROTO_ICMP: {
    285  1.12.2.2     tls 		const struct icmp *ic = enpc.npc_l4.icmp;
    286  1.12.2.4     tls 		ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
    287  1.12.2.2     tls 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    288  1.12.2.2     tls 			return false;
    289  1.12.2.2     tls 		break;
    290  1.12.2.2     tls 	}
    291  1.12.2.2     tls 	case IPPROTO_ICMPV6: {
    292  1.12.2.2     tls 		const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
    293  1.12.2.4     tls 		ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
    294  1.12.2.2     tls 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    295  1.12.2.2     tls 			return false;
    296  1.12.2.2     tls 		break;
    297  1.12.2.2     tls 	}
    298  1.12.2.2     tls 	default:
    299  1.12.2.2     tls 		return false;
    300  1.12.2.2     tls 	}
    301       1.4   rmind 
    302  1.12.2.4     tls 	/* Lookup a connection using the embedded packet. */
    303  1.12.2.4     tls 	return npf_conn_lookup(&enpc, di, &forw);
    304       1.1   rmind }
    305       1.1   rmind 
    306       1.1   rmind /*
    307  1.12.2.4     tls  * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
    308  1.12.2.4     tls  * which is embedded in ICMP packet.  Note: backwards stream only.
    309       1.1   rmind  */
    310       1.1   rmind static bool
    311  1.12.2.4     tls npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    312       1.1   rmind {
    313  1.12.2.4     tls 	const u_int which = NPF_SRC;
    314  1.12.2.2     tls 	npf_cache_t enpc;
    315       1.1   rmind 
    316  1.12.2.4     tls 	if (forw || !npf_iscached(npc, NPC_ICMP))
    317       1.1   rmind 		return false;
    318  1.12.2.4     tls 	if (!npfa_icmp_inspect(npc, &enpc))
    319  1.12.2.2     tls 		return false;
    320  1.12.2.2     tls 
    321       1.7  zoltan 	KASSERT(npf_iscached(&enpc, NPC_IP46));
    322       1.7  zoltan 	KASSERT(npf_iscached(&enpc, NPC_LAYER4));
    323  1.12.2.2     tls 
    324  1.12.2.4     tls 	/*
    325  1.12.2.4     tls 	 * ICMP: fetch the current checksum we are going to fixup.
    326  1.12.2.4     tls 	 */
    327  1.12.2.2     tls 	struct icmp *ic = npc->npc_l4.icmp;
    328  1.12.2.2     tls 	uint16_t cksum = ic->icmp_cksum;
    329  1.12.2.2     tls 
    330  1.12.2.2     tls 	CTASSERT(offsetof(struct icmp, icmp_cksum) ==
    331  1.12.2.2     tls 	    offsetof(struct icmp6_hdr, icmp6_cksum));
    332       1.1   rmind 
    333       1.6   rmind 	/*
    334  1.12.2.4     tls 	 * Fetch the IP and port in the _embedded_ packet.  Also, fetch
    335  1.12.2.4     tls 	 * the IPv4 and TCP/UDP checksums before they are rewritten.
    336  1.12.2.4     tls 	 * Calculate the part of the ICMP checksum fixup.
    337       1.6   rmind 	 */
    338  1.12.2.2     tls 	const int proto = enpc.npc_proto;
    339  1.12.2.2     tls 	uint16_t ipcksum = 0, l4cksum = 0;
    340       1.6   rmind 	npf_addr_t *addr;
    341       1.6   rmind 	in_port_t port;
    342       1.6   rmind 
    343       1.6   rmind 	npf_nat_getorig(nt, &addr, &port);
    344       1.4   rmind 
    345  1.12.2.2     tls 	if (npf_iscached(&enpc, NPC_IP4)) {
    346  1.12.2.2     tls 		const struct ip *eip = enpc.npc_ip.v4;
    347  1.12.2.2     tls 		ipcksum = eip->ip_sum;
    348  1.12.2.2     tls 	}
    349  1.12.2.4     tls 	cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
    350  1.12.2.2     tls 
    351  1.12.2.2     tls 	switch (proto) {
    352  1.12.2.2     tls 	case IPPROTO_TCP: {
    353  1.12.2.2     tls 		const struct tcphdr *th = enpc.npc_l4.tcp;
    354       1.6   rmind 		cksum = npf_fixup16_cksum(cksum, th->th_sport, port);
    355       1.4   rmind 		l4cksum = th->th_sum;
    356  1.12.2.2     tls 		break;
    357  1.12.2.2     tls 	}
    358  1.12.2.2     tls 	case IPPROTO_UDP: {
    359  1.12.2.2     tls 		const struct udphdr *uh = enpc.npc_l4.udp;
    360       1.6   rmind 		cksum = npf_fixup16_cksum(cksum, uh->uh_sport, port);
    361       1.4   rmind 		l4cksum = uh->uh_sum;
    362  1.12.2.2     tls 		break;
    363       1.1   rmind 	}
    364  1.12.2.2     tls 	case IPPROTO_ICMP:
    365  1.12.2.2     tls 	case IPPROTO_ICMPV6:
    366  1.12.2.2     tls 		break;
    367  1.12.2.2     tls 	default:
    368       1.1   rmind 		return false;
    369       1.1   rmind 	}
    370       1.1   rmind 
    371       1.4   rmind 	/*
    372  1.12.2.4     tls 	 * Translate the embedded packet.  The following changes will
    373  1.12.2.4     tls 	 * be performed by npf_napt_rwr():
    374  1.12.2.4     tls 	 *
    375  1.12.2.4     tls 	 *	1) Rewrite the IP address and, if not ICMP, port.
    376  1.12.2.4     tls 	 *	2) Rewrite the TCP/UDP checksum (if not ICMP).
    377  1.12.2.4     tls 	 *	3) Rewrite the IPv4 checksum for (1) and (2).
    378  1.12.2.4     tls 	 *
    379  1.12.2.4     tls 	 * XXX: Assumes NPF_NATOUT (source address/port).  Currently,
    380  1.12.2.4     tls 	 * npfa_icmp_match() matches only for the PFIL_OUT traffic.
    381       1.4   rmind 	 */
    382  1.12.2.4     tls 	if (npf_napt_rwr(&enpc, which, addr, port)) {
    383       1.1   rmind 		return false;
    384       1.1   rmind 	}
    385       1.1   rmind 
    386       1.1   rmind 	/*
    387  1.12.2.4     tls 	 * Finally, finish the ICMP checksum fixup: include the checksum
    388  1.12.2.4     tls 	 * changes in the embedded packet.
    389       1.1   rmind 	 */
    390  1.12.2.2     tls 	if (npf_iscached(&enpc, NPC_IP4)) {
    391  1.12.2.2     tls 		const struct ip *eip = enpc.npc_ip.v4;
    392  1.12.2.2     tls 		cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
    393  1.12.2.2     tls 	}
    394  1.12.2.2     tls 	switch (proto) {
    395  1.12.2.2     tls 	case IPPROTO_TCP: {
    396  1.12.2.2     tls 		const struct tcphdr *th = enpc.npc_l4.tcp;
    397       1.4   rmind 		cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
    398  1.12.2.2     tls 		break;
    399       1.1   rmind 	}
    400  1.12.2.2     tls 	case IPPROTO_UDP:
    401  1.12.2.2     tls 		if (l4cksum) {
    402  1.12.2.2     tls 			const struct udphdr *uh = enpc.npc_l4.udp;
    403  1.12.2.2     tls 			cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
    404  1.12.2.2     tls 		}
    405  1.12.2.2     tls 		break;
    406       1.6   rmind 	}
    407  1.12.2.2     tls 	ic->icmp_cksum = cksum;
    408       1.6   rmind 	return true;
    409       1.1   rmind }
    410  1.12.2.4     tls 
    411  1.12.2.4     tls /*
    412  1.12.2.4     tls  * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
    413  1.12.2.4     tls  * and module interface.
    414  1.12.2.4     tls  */
    415  1.12.2.4     tls 
    416  1.12.2.4     tls static int
    417  1.12.2.4     tls npf_alg_icmp_init(void)
    418  1.12.2.4     tls {
    419  1.12.2.4     tls 	static const npfa_funcs_t icmp = {
    420  1.12.2.4     tls 		.match		= npfa_icmp_match,
    421  1.12.2.4     tls 		.translate	= npfa_icmp_nat,
    422  1.12.2.4     tls 		.inspect	= npfa_icmp_conn,
    423  1.12.2.4     tls 	};
    424  1.12.2.4     tls 	alg_icmp = npf_alg_register("icmp", &icmp);
    425  1.12.2.4     tls 	return alg_icmp ? 0 : ENOMEM;
    426  1.12.2.4     tls }
    427  1.12.2.4     tls 
    428  1.12.2.4     tls static int
    429  1.12.2.4     tls npf_alg_icmp_fini(void)
    430  1.12.2.4     tls {
    431  1.12.2.4     tls 	KASSERT(alg_icmp != NULL);
    432  1.12.2.4     tls 	return npf_alg_unregister(alg_icmp);
    433  1.12.2.4     tls }
    434  1.12.2.4     tls 
    435  1.12.2.4     tls static int
    436  1.12.2.4     tls npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
    437  1.12.2.4     tls {
    438  1.12.2.4     tls 	switch (cmd) {
    439  1.12.2.4     tls 	case MODULE_CMD_INIT:
    440  1.12.2.4     tls 		return npf_alg_icmp_init();
    441  1.12.2.4     tls 	case MODULE_CMD_FINI:
    442  1.12.2.4     tls 		return npf_alg_icmp_fini();
    443  1.12.2.4     tls 	case MODULE_CMD_AUTOUNLOAD:
    444  1.12.2.4     tls 		return EBUSY;
    445  1.12.2.4     tls 	default:
    446  1.12.2.4     tls 		return ENOTTY;
    447  1.12.2.4     tls 	}
    448  1.12.2.4     tls 	return 0;
    449  1.12.2.4     tls }
    450