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