Home | History | Annotate | Line # | Download | only in npf
npf_alg_icmp.c revision 1.23.8.1
      1  1.23.8.1  pgoyette /*	$NetBSD: npf_alg_icmp.c,v 1.23.8.1 2017/01/07 08:56:50 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.23.8.1  pgoyette #ifdef _KERNEL
     37       1.1     rmind #include <sys/cdefs.h>
     38  1.23.8.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: npf_alg_icmp.c,v 1.23.8.1 2017/01/07 08:56:50 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.23.8.1  pgoyette #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.1     rmind  */
    124      1.13     rmind 
    125       1.5     rmind static bool
    126      1.23     rmind npfa_icmp4_inspect(const int type, npf_cache_t *npc)
    127       1.1     rmind {
    128      1.23     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    129      1.13     rmind 	u_int offby;
    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 		if (npc == NULL) {
    139      1.13     rmind 			return false;
    140      1.13     rmind 		}
    141      1.14     rmind 		/* Should contain original IP header. */
    142      1.14     rmind 		if (!nbuf_advance(nbuf, offsetof(struct icmp, icmp_ip), 0)) {
    143      1.13     rmind 			return false;
    144       1.1     rmind 		}
    145      1.23     rmind 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    146      1.13     rmind 
    147      1.13     rmind 	case ICMP_ECHOREPLY:
    148      1.13     rmind 	case ICMP_ECHO:
    149      1.13     rmind 	case ICMP_TSTAMP:
    150      1.13     rmind 	case ICMP_TSTAMPREPLY:
    151      1.13     rmind 	case ICMP_IREQ:
    152      1.13     rmind 	case ICMP_IREQREPLY:
    153      1.14     rmind 		/* Should contain ICMP query ID - ensure. */
    154      1.13     rmind 		offby = offsetof(struct icmp, icmp_id);
    155      1.14     rmind 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    156      1.13     rmind 			return false;
    157      1.13     rmind 		}
    158      1.13     rmind 		npc->npc_info |= NPC_ICMP_ID;
    159      1.13     rmind 		return true;
    160      1.13     rmind 	default:
    161      1.13     rmind 		break;
    162      1.11       spz 	}
    163      1.13     rmind 	return false;
    164      1.13     rmind }
    165      1.13     rmind 
    166      1.13     rmind static bool
    167      1.23     rmind npfa_icmp6_inspect(const int type, npf_cache_t *npc)
    168      1.13     rmind {
    169      1.23     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    170      1.13     rmind 	u_int offby;
    171      1.13     rmind 
    172      1.13     rmind 	/* Per RFC 4443. */
    173      1.13     rmind 	switch (type) {
    174      1.13     rmind 	case ICMP6_DST_UNREACH:
    175      1.13     rmind 	case ICMP6_PACKET_TOO_BIG:
    176      1.13     rmind 	case ICMP6_TIME_EXCEEDED:
    177      1.13     rmind 	case ICMP6_PARAM_PROB:
    178      1.14     rmind 		if (npc == NULL) {
    179      1.13     rmind 			return false;
    180      1.13     rmind 		}
    181      1.14     rmind 		/* Should contain original IP header. */
    182      1.14     rmind 		if (!nbuf_advance(nbuf, sizeof(struct icmp6_hdr), 0)) {
    183      1.13     rmind 			return false;
    184       1.1     rmind 		}
    185      1.23     rmind 		return (npf_cache_all(npc) & NPC_LAYER4) != 0;
    186      1.13     rmind 
    187      1.13     rmind 	case ICMP6_ECHO_REQUEST:
    188      1.13     rmind 	case ICMP6_ECHO_REPLY:
    189      1.14     rmind 		/* Should contain ICMP query ID - ensure. */
    190      1.13     rmind 		offby = offsetof(struct icmp6_hdr, icmp6_id);
    191      1.14     rmind 		if (!nbuf_advance(nbuf, offby, sizeof(uint16_t))) {
    192      1.13     rmind 			return false;
    193      1.13     rmind 		}
    194      1.13     rmind 		npc->npc_info |= NPC_ICMP_ID;
    195      1.13     rmind 		return true;
    196      1.13     rmind 	default:
    197      1.13     rmind 		break;
    198       1.1     rmind 	}
    199       1.1     rmind 	return false;
    200       1.1     rmind }
    201       1.1     rmind 
    202       1.1     rmind /*
    203      1.22     rmind  * npfa_icmp_inspect: ALG ICMP inspector.
    204      1.14     rmind  *
    205      1.14     rmind  * => Returns true if "enpc" is filled.
    206       1.1     rmind  */
    207       1.1     rmind static bool
    208      1.23     rmind npfa_icmp_inspect(npf_cache_t *npc, npf_cache_t *enpc)
    209       1.1     rmind {
    210      1.23     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    211      1.13     rmind 	bool ret;
    212      1.13     rmind 
    213      1.14     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    214       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_ICMP));
    215       1.1     rmind 
    216       1.1     rmind 	/* Advance to ICMP header. */
    217      1.14     rmind 	nbuf_reset(nbuf);
    218      1.15     rmind 	if (!nbuf_advance(nbuf, npc->npc_hlen, 0)) {
    219       1.1     rmind 		return false;
    220       1.1     rmind 	}
    221  1.23.8.1  pgoyette 	enpc->npc_ctx = npc->npc_ctx;
    222      1.23     rmind 	enpc->npc_nbuf = nbuf;
    223      1.14     rmind 	enpc->npc_info = 0;
    224       1.1     rmind 
    225      1.13     rmind 	/*
    226      1.14     rmind 	 * Inspect the ICMP packet.  The relevant data might be in the
    227      1.14     rmind 	 * embedded packet.  Fill the "enpc" cache, if so.
    228      1.13     rmind 	 */
    229      1.13     rmind 	if (npf_iscached(npc, NPC_IP4)) {
    230      1.14     rmind 		const struct icmp *ic = npc->npc_l4.icmp;
    231      1.23     rmind 		ret = npfa_icmp4_inspect(ic->icmp_type, enpc);
    232      1.13     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
    233      1.14     rmind 		const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
    234      1.23     rmind 		ret = npfa_icmp6_inspect(ic6->icmp6_type, enpc);
    235      1.13     rmind 	} else {
    236      1.13     rmind 		ret = false;
    237      1.13     rmind 	}
    238      1.13     rmind 	if (!ret) {
    239       1.1     rmind 		return false;
    240       1.1     rmind 	}
    241       1.1     rmind 
    242      1.14     rmind 	/* ICMP ID is the original packet, just indicate it. */
    243      1.14     rmind 	if (npf_iscached(enpc, NPC_ICMP_ID)) {
    244       1.4     rmind 		npc->npc_info |= NPC_ICMP_ID;
    245       1.4     rmind 		return false;
    246       1.1     rmind 	}
    247       1.4     rmind 
    248      1.14     rmind 	/* Indicate that embedded packet is in the cache. */
    249      1.14     rmind 	return true;
    250      1.14     rmind }
    251      1.14     rmind 
    252      1.22     rmind static npf_conn_t *
    253      1.23     rmind npfa_icmp_conn(npf_cache_t *npc, int di)
    254      1.14     rmind {
    255      1.14     rmind 	npf_cache_t enpc;
    256      1.14     rmind 
    257      1.14     rmind 	/* Inspect ICMP packet for an embedded packet. */
    258      1.14     rmind 	if (!npf_iscached(npc, NPC_ICMP))
    259      1.14     rmind 		return NULL;
    260      1.23     rmind 	if (!npfa_icmp_inspect(npc, &enpc))
    261      1.14     rmind 		return NULL;
    262      1.14     rmind 
    263       1.4     rmind 	/*
    264      1.14     rmind 	 * Invert the identifiers of the embedded packet.
    265      1.14     rmind 	 * If it is ICMP, then ensure ICMP ID.
    266       1.4     rmind 	 */
    267      1.14     rmind 	union l4 {
    268      1.14     rmind 		struct tcphdr th;
    269      1.14     rmind 		struct udphdr uh;
    270      1.14     rmind 	} l4;
    271      1.14     rmind 	bool ret, forw;
    272      1.14     rmind 
    273      1.14     rmind 	#define	SWAP(type, x, y) { type tmp = x; x = y; y = tmp; }
    274      1.18     rmind 	SWAP(npf_addr_t *, enpc.npc_ips[NPF_SRC], enpc.npc_ips[NPF_DST]);
    275      1.14     rmind 
    276      1.15     rmind 	switch (enpc.npc_proto) {
    277      1.14     rmind 	case IPPROTO_TCP:
    278      1.14     rmind 		l4.th.th_sport = enpc.npc_l4.tcp->th_dport;
    279      1.14     rmind 		l4.th.th_dport = enpc.npc_l4.tcp->th_sport;
    280      1.14     rmind 		enpc.npc_l4.tcp = &l4.th;
    281      1.14     rmind 		break;
    282      1.14     rmind 	case IPPROTO_UDP:
    283      1.14     rmind 		l4.uh.uh_sport = enpc.npc_l4.udp->uh_dport;
    284      1.14     rmind 		l4.uh.uh_dport = enpc.npc_l4.udp->uh_sport;
    285      1.14     rmind 		enpc.npc_l4.udp = &l4.uh;
    286      1.14     rmind 		break;
    287      1.14     rmind 	case IPPROTO_ICMP: {
    288      1.14     rmind 		const struct icmp *ic = enpc.npc_l4.icmp;
    289      1.23     rmind 		ret = npfa_icmp4_inspect(ic->icmp_type, &enpc);
    290      1.14     rmind 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    291      1.14     rmind 			return false;
    292      1.14     rmind 		break;
    293      1.14     rmind 	}
    294      1.14     rmind 	case IPPROTO_ICMPV6: {
    295      1.14     rmind 		const struct icmp6_hdr *ic6 = enpc.npc_l4.icmp6;
    296      1.23     rmind 		ret = npfa_icmp6_inspect(ic6->icmp6_type, &enpc);
    297      1.14     rmind 		if (!ret || !npf_iscached(&enpc, NPC_ICMP_ID))
    298      1.14     rmind 			return false;
    299      1.14     rmind 		break;
    300      1.14     rmind 	}
    301      1.14     rmind 	default:
    302      1.14     rmind 		return false;
    303      1.14     rmind 	}
    304       1.4     rmind 
    305      1.22     rmind 	/* Lookup a connection using the embedded packet. */
    306      1.23     rmind 	return npf_conn_lookup(&enpc, di, &forw);
    307       1.1     rmind }
    308       1.1     rmind 
    309       1.1     rmind /*
    310      1.18     rmind  * npfa_icmp_nat: ALG translator - rewrites IP address in the IP header
    311      1.18     rmind  * which is embedded in ICMP packet.  Note: backwards stream only.
    312       1.1     rmind  */
    313       1.1     rmind static bool
    314      1.23     rmind npfa_icmp_nat(npf_cache_t *npc, npf_nat_t *nt, bool forw)
    315       1.1     rmind {
    316      1.20     rmind 	const u_int which = NPF_SRC;
    317      1.14     rmind 	npf_cache_t enpc;
    318       1.1     rmind 
    319      1.18     rmind 	if (forw || !npf_iscached(npc, NPC_ICMP))
    320      1.14     rmind 		return false;
    321      1.23     rmind 	if (!npfa_icmp_inspect(npc, &enpc))
    322       1.1     rmind 		return false;
    323      1.14     rmind 
    324       1.7    zoltan 	KASSERT(npf_iscached(&enpc, NPC_IP46));
    325       1.7    zoltan 	KASSERT(npf_iscached(&enpc, NPC_LAYER4));
    326      1.14     rmind 
    327      1.20     rmind 	/*
    328      1.20     rmind 	 * ICMP: fetch the current checksum we are going to fixup.
    329      1.20     rmind 	 */
    330      1.14     rmind 	struct icmp *ic = npc->npc_l4.icmp;
    331      1.14     rmind 	uint16_t cksum = ic->icmp_cksum;
    332      1.14     rmind 
    333      1.14     rmind 	CTASSERT(offsetof(struct icmp, icmp_cksum) ==
    334      1.14     rmind 	    offsetof(struct icmp6_hdr, icmp6_cksum));
    335       1.1     rmind 
    336       1.6     rmind 	/*
    337      1.20     rmind 	 * Fetch the IP and port in the _embedded_ packet.  Also, fetch
    338      1.20     rmind 	 * the IPv4 and TCP/UDP checksums before they are rewritten.
    339      1.20     rmind 	 * Calculate the part of the ICMP checksum fixup.
    340       1.6     rmind 	 */
    341      1.15     rmind 	const int proto = enpc.npc_proto;
    342      1.14     rmind 	uint16_t ipcksum = 0, l4cksum = 0;
    343       1.6     rmind 	npf_addr_t *addr;
    344       1.6     rmind 	in_port_t port;
    345       1.6     rmind 
    346       1.6     rmind 	npf_nat_getorig(nt, &addr, &port);
    347       1.4     rmind 
    348      1.14     rmind 	if (npf_iscached(&enpc, NPC_IP4)) {
    349      1.14     rmind 		const struct ip *eip = enpc.npc_ip.v4;
    350      1.14     rmind 		ipcksum = eip->ip_sum;
    351      1.14     rmind 	}
    352      1.20     rmind 	cksum = npf_addr_cksum(cksum, enpc.npc_alen, enpc.npc_ips[which], addr);
    353      1.14     rmind 
    354      1.14     rmind 	switch (proto) {
    355      1.14     rmind 	case IPPROTO_TCP: {
    356      1.14     rmind 		const struct tcphdr *th = enpc.npc_l4.tcp;
    357       1.6     rmind 		cksum = npf_fixup16_cksum(cksum, th->th_sport, port);
    358       1.4     rmind 		l4cksum = th->th_sum;
    359      1.14     rmind 		break;
    360      1.14     rmind 	}
    361      1.14     rmind 	case IPPROTO_UDP: {
    362      1.14     rmind 		const struct udphdr *uh = enpc.npc_l4.udp;
    363       1.6     rmind 		cksum = npf_fixup16_cksum(cksum, uh->uh_sport, port);
    364       1.4     rmind 		l4cksum = uh->uh_sum;
    365      1.14     rmind 		break;
    366       1.1     rmind 	}
    367      1.14     rmind 	case IPPROTO_ICMP:
    368      1.14     rmind 	case IPPROTO_ICMPV6:
    369      1.14     rmind 		break;
    370      1.14     rmind 	default:
    371       1.1     rmind 		return false;
    372       1.1     rmind 	}
    373       1.1     rmind 
    374       1.4     rmind 	/*
    375      1.20     rmind 	 * Translate the embedded packet.  The following changes will
    376      1.20     rmind 	 * be performed by npf_napt_rwr():
    377      1.20     rmind 	 *
    378      1.20     rmind 	 *	1) Rewrite the IP address and, if not ICMP, port.
    379      1.20     rmind 	 *	2) Rewrite the TCP/UDP checksum (if not ICMP).
    380      1.20     rmind 	 *	3) Rewrite the IPv4 checksum for (1) and (2).
    381      1.20     rmind 	 *
    382      1.20     rmind 	 * XXX: Assumes NPF_NATOUT (source address/port).  Currently,
    383      1.20     rmind 	 * npfa_icmp_match() matches only for the PFIL_OUT traffic.
    384       1.4     rmind 	 */
    385      1.20     rmind 	if (npf_napt_rwr(&enpc, which, addr, port)) {
    386       1.1     rmind 		return false;
    387       1.1     rmind 	}
    388       1.1     rmind 
    389       1.1     rmind 	/*
    390      1.20     rmind 	 * Finally, finish the ICMP checksum fixup: include the checksum
    391      1.20     rmind 	 * changes in the embedded packet.
    392       1.1     rmind 	 */
    393      1.14     rmind 	if (npf_iscached(&enpc, NPC_IP4)) {
    394      1.14     rmind 		const struct ip *eip = enpc.npc_ip.v4;
    395      1.14     rmind 		cksum = npf_fixup16_cksum(cksum, ipcksum, eip->ip_sum);
    396      1.14     rmind 	}
    397      1.14     rmind 	switch (proto) {
    398      1.14     rmind 	case IPPROTO_TCP: {
    399      1.14     rmind 		const struct tcphdr *th = enpc.npc_l4.tcp;
    400       1.4     rmind 		cksum = npf_fixup16_cksum(cksum, l4cksum, th->th_sum);
    401      1.14     rmind 		break;
    402       1.1     rmind 	}
    403      1.14     rmind 	case IPPROTO_UDP:
    404      1.14     rmind 		if (l4cksum) {
    405      1.14     rmind 			const struct udphdr *uh = enpc.npc_l4.udp;
    406      1.14     rmind 			cksum = npf_fixup16_cksum(cksum, l4cksum, uh->uh_sum);
    407      1.14     rmind 		}
    408      1.14     rmind 		break;
    409       1.6     rmind 	}
    410      1.14     rmind 	ic->icmp_cksum = cksum;
    411       1.6     rmind 	return true;
    412       1.1     rmind }
    413      1.19     rmind 
    414      1.19     rmind /*
    415      1.19     rmind  * npf_alg_icmp_{init,fini,modcmd}: ICMP ALG initialization, destruction
    416      1.19     rmind  * and module interface.
    417      1.19     rmind  */
    418      1.19     rmind 
    419      1.19     rmind static int
    420      1.19     rmind npf_alg_icmp_init(void)
    421      1.19     rmind {
    422      1.19     rmind 	static const npfa_funcs_t icmp = {
    423      1.19     rmind 		.match		= npfa_icmp_match,
    424      1.19     rmind 		.translate	= npfa_icmp_nat,
    425      1.22     rmind 		.inspect	= npfa_icmp_conn,
    426      1.19     rmind 	};
    427  1.23.8.1  pgoyette 	alg_icmp = npf_alg_register(npf_getkernctx(), "icmp", &icmp);
    428      1.19     rmind 	return alg_icmp ? 0 : ENOMEM;
    429      1.19     rmind }
    430      1.19     rmind 
    431      1.19     rmind static int
    432      1.19     rmind npf_alg_icmp_fini(void)
    433      1.19     rmind {
    434      1.19     rmind 	KASSERT(alg_icmp != NULL);
    435  1.23.8.1  pgoyette 	return npf_alg_unregister(npf_getkernctx(), alg_icmp);
    436      1.19     rmind }
    437      1.19     rmind 
    438      1.19     rmind static int
    439      1.19     rmind npf_alg_icmp_modcmd(modcmd_t cmd, void *arg)
    440      1.19     rmind {
    441      1.19     rmind 	switch (cmd) {
    442      1.19     rmind 	case MODULE_CMD_INIT:
    443      1.19     rmind 		return npf_alg_icmp_init();
    444      1.19     rmind 	case MODULE_CMD_FINI:
    445      1.19     rmind 		return npf_alg_icmp_fini();
    446      1.19     rmind 	case MODULE_CMD_AUTOUNLOAD:
    447      1.19     rmind 		return EBUSY;
    448      1.19     rmind 	default:
    449      1.19     rmind 		return ENOTTY;
    450      1.19     rmind 	}
    451      1.19     rmind 	return 0;
    452      1.19     rmind }
    453