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