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