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