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