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