Home | History | Annotate | Line # | Download | only in npf
npf_inet.c revision 1.36.2.1
      1  1.36.2.1    bouyer /*	$NetBSD: npf_inet.c,v 1.36.2.1 2017/04/21 16:54:05 bouyer Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4      1.29     rmind  * Copyright (c) 2009-2014 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.22     rmind  * Various protocol related helper routines.
     34      1.12     rmind  *
     35      1.12     rmind  * This layer manipulates npf_cache_t structure i.e. caches requested headers
     36      1.12     rmind  * and stores which information was cached in the information bit field.
     37      1.12     rmind  * It is also responsibility of this layer to update or invalidate the cache
     38      1.12     rmind  * on rewrites (e.g. by translation routines).
     39       1.1     rmind  */
     40       1.1     rmind 
     41      1.36  christos #ifdef _KERNEL
     42       1.1     rmind #include <sys/cdefs.h>
     43  1.36.2.1    bouyer __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.36.2.1 2017/04/21 16:54:05 bouyer Exp $");
     44       1.1     rmind 
     45       1.1     rmind #include <sys/param.h>
     46      1.11     rmind #include <sys/types.h>
     47       1.1     rmind 
     48       1.4     rmind #include <net/pfil.h>
     49       1.4     rmind #include <net/if.h>
     50       1.4     rmind #include <net/ethertypes.h>
     51       1.4     rmind #include <net/if_ether.h>
     52       1.4     rmind 
     53       1.1     rmind #include <netinet/in_systm.h>
     54       1.1     rmind #include <netinet/in.h>
     55      1.33   mlelstv #include <netinet6/in6_var.h>
     56       1.1     rmind #include <netinet/ip.h>
     57       1.4     rmind #include <netinet/ip6.h>
     58       1.1     rmind #include <netinet/tcp.h>
     59       1.1     rmind #include <netinet/udp.h>
     60       1.1     rmind #include <netinet/ip_icmp.h>
     61      1.36  christos #endif
     62       1.1     rmind 
     63       1.1     rmind #include "npf_impl.h"
     64       1.1     rmind 
     65       1.1     rmind /*
     66      1.27     rmind  * npf_fixup{16,32}_cksum: incremental update of the Internet checksum.
     67       1.1     rmind  */
     68       1.1     rmind 
     69       1.1     rmind uint16_t
     70       1.1     rmind npf_fixup16_cksum(uint16_t cksum, uint16_t odatum, uint16_t ndatum)
     71       1.1     rmind {
     72       1.1     rmind 	uint32_t sum;
     73       1.1     rmind 
     74       1.1     rmind 	/*
     75       1.1     rmind 	 * RFC 1624:
     76       1.1     rmind 	 *	HC' = ~(~HC + ~m + m')
     77      1.27     rmind 	 *
     78      1.27     rmind 	 * Note: 1's complement sum is endian-independent (RFC 1071, page 2).
     79       1.1     rmind 	 */
     80      1.27     rmind 	sum = ~cksum & 0xffff;
     81      1.27     rmind 	sum += (~odatum & 0xffff) + ndatum;
     82       1.1     rmind 	sum = (sum >> 16) + (sum & 0xffff);
     83       1.1     rmind 	sum += (sum >> 16);
     84       1.1     rmind 
     85      1.27     rmind 	return ~sum & 0xffff;
     86       1.1     rmind }
     87       1.1     rmind 
     88       1.1     rmind uint16_t
     89       1.1     rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
     90       1.1     rmind {
     91      1.27     rmind 	uint32_t sum;
     92      1.27     rmind 
     93      1.27     rmind 	/*
     94      1.27     rmind 	 * Checksum 32-bit datum as as two 16-bit.  Note, the first
     95      1.27     rmind 	 * 32->16 bit reduction is not necessary.
     96      1.27     rmind 	 */
     97      1.27     rmind 	sum = ~cksum & 0xffff;
     98      1.27     rmind 	sum += (~odatum & 0xffff) + (ndatum & 0xffff);
     99       1.1     rmind 
    100      1.27     rmind 	sum += (~odatum >> 16) + (ndatum >> 16);
    101      1.27     rmind 	sum = (sum >> 16) + (sum & 0xffff);
    102      1.27     rmind 	sum += (sum >> 16);
    103      1.27     rmind 	return ~sum & 0xffff;
    104       1.1     rmind }
    105       1.1     rmind 
    106       1.1     rmind /*
    107       1.4     rmind  * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
    108       1.4     rmind  */
    109       1.4     rmind uint16_t
    110      1.19     rmind npf_addr_cksum(uint16_t cksum, int sz, const npf_addr_t *oaddr,
    111      1.19     rmind     const npf_addr_t *naddr)
    112       1.4     rmind {
    113      1.19     rmind 	const uint32_t *oip32 = (const uint32_t *)oaddr;
    114      1.19     rmind 	const uint32_t *nip32 = (const uint32_t *)naddr;
    115       1.4     rmind 
    116       1.4     rmind 	KASSERT(sz % sizeof(uint32_t) == 0);
    117       1.4     rmind 	do {
    118       1.4     rmind 		cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
    119       1.4     rmind 		sz -= sizeof(uint32_t);
    120       1.4     rmind 	} while (sz);
    121       1.4     rmind 
    122       1.4     rmind 	return cksum;
    123       1.4     rmind }
    124       1.4     rmind 
    125       1.4     rmind /*
    126      1.26     rmind  * npf_addr_sum: provide IP addresses as a XORed 32-bit integer.
    127       1.4     rmind  * Note: used for hash function.
    128       1.1     rmind  */
    129       1.4     rmind uint32_t
    130      1.26     rmind npf_addr_mix(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
    131       1.1     rmind {
    132       1.4     rmind 	uint32_t mix = 0;
    133       1.1     rmind 
    134       1.5     rmind 	KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
    135       1.5     rmind 
    136      1.26     rmind 	for (int i = 0; i < (sz >> 2); i++) {
    137      1.36  christos 		mix ^= a1->word32[i];
    138      1.36  christos 		mix ^= a2->word32[i];
    139       1.4     rmind 	}
    140       1.4     rmind 	return mix;
    141       1.4     rmind }
    142       1.1     rmind 
    143      1.13     rmind /*
    144      1.13     rmind  * npf_addr_mask: apply the mask to a given address and store the result.
    145      1.13     rmind  */
    146      1.13     rmind void
    147      1.13     rmind npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask,
    148      1.13     rmind     const int alen, npf_addr_t *out)
    149      1.12     rmind {
    150      1.13     rmind 	const int nwords = alen >> 2;
    151      1.12     rmind 	uint_fast8_t length = mask;
    152      1.12     rmind 
    153      1.12     rmind 	/* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
    154      1.12     rmind 	KASSERT(length <= NPF_MAX_NETMASK);
    155      1.12     rmind 
    156      1.13     rmind 	for (int i = 0; i < nwords; i++) {
    157      1.13     rmind 		uint32_t wordmask;
    158      1.13     rmind 
    159      1.12     rmind 		if (length >= 32) {
    160      1.13     rmind 			wordmask = htonl(0xffffffff);
    161      1.12     rmind 			length -= 32;
    162      1.13     rmind 		} else if (length) {
    163      1.13     rmind 			wordmask = htonl(0xffffffff << (32 - length));
    164      1.13     rmind 			length = 0;
    165      1.12     rmind 		} else {
    166      1.13     rmind 			wordmask = 0;
    167      1.12     rmind 		}
    168      1.36  christos 		out->word32[i] = addr->word32[i] & wordmask;
    169      1.12     rmind 	}
    170      1.12     rmind }
    171      1.12     rmind 
    172      1.12     rmind /*
    173      1.12     rmind  * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
    174      1.12     rmind  *
    175      1.13     rmind  * => Return 0 if equal and negative/positive if less/greater accordingly.
    176      1.12     rmind  * => Ignore the mask, if NPF_NO_NETMASK is specified.
    177      1.12     rmind  */
    178      1.12     rmind int
    179      1.12     rmind npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
    180      1.13     rmind     const npf_addr_t *addr2, const npf_netmask_t mask2, const int alen)
    181      1.12     rmind {
    182      1.13     rmind 	npf_addr_t realaddr1, realaddr2;
    183      1.12     rmind 
    184      1.12     rmind 	if (mask1 != NPF_NO_NETMASK) {
    185      1.13     rmind 		npf_addr_mask(addr1, mask1, alen, &realaddr1);
    186      1.13     rmind 		addr1 = &realaddr1;
    187      1.12     rmind 	}
    188      1.12     rmind 	if (mask2 != NPF_NO_NETMASK) {
    189      1.13     rmind 		npf_addr_mask(addr2, mask2, alen, &realaddr2);
    190      1.13     rmind 		addr2 = &realaddr2;
    191      1.12     rmind 	}
    192      1.13     rmind 	return memcmp(addr1, addr2, alen);
    193      1.12     rmind }
    194      1.12     rmind 
    195       1.4     rmind /*
    196       1.4     rmind  * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
    197      1.12     rmind  *
    198      1.12     rmind  * => Returns all values in host byte-order.
    199       1.4     rmind  */
    200       1.4     rmind int
    201      1.12     rmind npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
    202       1.4     rmind {
    203      1.19     rmind 	const struct tcphdr *th = npc->npc_l4.tcp;
    204       1.8     rmind 	u_int thlen;
    205       1.1     rmind 
    206       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    207       1.1     rmind 
    208       1.4     rmind 	*seq = ntohl(th->th_seq);
    209       1.4     rmind 	*ack = ntohl(th->th_ack);
    210       1.4     rmind 	*win = (uint32_t)ntohs(th->th_win);
    211       1.8     rmind 	thlen = th->th_off << 2;
    212       1.1     rmind 
    213       1.7    zoltan 	if (npf_iscached(npc, NPC_IP4)) {
    214      1.19     rmind 		const struct ip *ip = npc->npc_ip.v4;
    215      1.21     rmind 		return ntohs(ip->ip_len) - npc->npc_hlen - thlen;
    216      1.12     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
    217      1.19     rmind 		const struct ip6_hdr *ip6 = npc->npc_ip.v6;
    218       1.8     rmind 		return ntohs(ip6->ip6_plen) - thlen;
    219       1.7    zoltan 	}
    220       1.7    zoltan 	return 0;
    221       1.1     rmind }
    222       1.1     rmind 
    223       1.1     rmind /*
    224       1.4     rmind  * npf_fetch_tcpopts: parse and return TCP options.
    225       1.1     rmind  */
    226       1.1     rmind bool
    227      1.32     rmind npf_fetch_tcpopts(npf_cache_t *npc, uint16_t *mss, int *wscale)
    228       1.1     rmind {
    229      1.32     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    230      1.19     rmind 	const struct tcphdr *th = npc->npc_l4.tcp;
    231       1.4     rmind 	int topts_len, step;
    232      1.19     rmind 	void *nptr;
    233       1.4     rmind 	uint8_t val;
    234      1.19     rmind 	bool ok;
    235       1.4     rmind 
    236       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
    237       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    238      1.10     rmind 
    239       1.4     rmind 	/* Determine if there are any TCP options, get their length. */
    240       1.4     rmind 	topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
    241       1.4     rmind 	if (topts_len <= 0) {
    242       1.4     rmind 		/* No options. */
    243       1.1     rmind 		return false;
    244       1.4     rmind 	}
    245       1.4     rmind 	KASSERT(topts_len <= MAX_TCPOPTLEN);
    246       1.1     rmind 
    247       1.4     rmind 	/* First step: IP and TCP header up to options. */
    248      1.21     rmind 	step = npc->npc_hlen + sizeof(struct tcphdr);
    249      1.19     rmind 	nbuf_reset(nbuf);
    250       1.4     rmind next:
    251      1.19     rmind 	if ((nptr = nbuf_advance(nbuf, step, 1)) == NULL) {
    252      1.19     rmind 		ok = false;
    253      1.19     rmind 		goto done;
    254       1.4     rmind 	}
    255      1.19     rmind 	val = *(uint8_t *)nptr;
    256      1.12     rmind 
    257       1.4     rmind 	switch (val) {
    258       1.4     rmind 	case TCPOPT_EOL:
    259       1.4     rmind 		/* Done. */
    260      1.19     rmind 		ok = true;
    261      1.19     rmind 		goto done;
    262       1.4     rmind 	case TCPOPT_NOP:
    263       1.4     rmind 		topts_len--;
    264       1.4     rmind 		step = 1;
    265       1.4     rmind 		break;
    266       1.4     rmind 	case TCPOPT_MAXSEG:
    267      1.19     rmind 		if ((nptr = nbuf_advance(nbuf, 2, 2)) == NULL) {
    268      1.19     rmind 			ok = false;
    269      1.19     rmind 			goto done;
    270       1.4     rmind 		}
    271       1.4     rmind 		if (mss) {
    272      1.19     rmind 			if (*mss) {
    273      1.19     rmind 				memcpy(nptr, mss, sizeof(uint16_t));
    274      1.19     rmind 			} else {
    275      1.19     rmind 				memcpy(mss, nptr, sizeof(uint16_t));
    276      1.19     rmind 			}
    277       1.4     rmind 		}
    278       1.4     rmind 		topts_len -= TCPOLEN_MAXSEG;
    279      1.19     rmind 		step = 2;
    280       1.4     rmind 		break;
    281       1.4     rmind 	case TCPOPT_WINDOW:
    282      1.10     rmind 		/* TCP Window Scaling (RFC 1323). */
    283      1.19     rmind 		if ((nptr = nbuf_advance(nbuf, 2, 1)) == NULL) {
    284      1.19     rmind 			ok = false;
    285      1.19     rmind 			goto done;
    286       1.4     rmind 		}
    287      1.19     rmind 		val = *(uint8_t *)nptr;
    288       1.4     rmind 		*wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
    289       1.4     rmind 		topts_len -= TCPOLEN_WINDOW;
    290      1.19     rmind 		step = 1;
    291       1.4     rmind 		break;
    292       1.4     rmind 	default:
    293      1.19     rmind 		if ((nptr = nbuf_advance(nbuf, 1, 1)) == NULL) {
    294      1.19     rmind 			ok = false;
    295      1.19     rmind 			goto done;
    296       1.4     rmind 		}
    297      1.19     rmind 		val = *(uint8_t *)nptr;
    298      1.16     rmind 		if (val < 2 || val > topts_len) {
    299      1.19     rmind 			ok = false;
    300      1.19     rmind 			goto done;
    301       1.4     rmind 		}
    302       1.4     rmind 		topts_len -= val;
    303       1.4     rmind 		step = val - 1;
    304       1.4     rmind 	}
    305      1.12     rmind 
    306       1.6     rmind 	/* Any options left? */
    307       1.4     rmind 	if (__predict_true(topts_len > 0)) {
    308       1.4     rmind 		goto next;
    309       1.4     rmind 	}
    310      1.19     rmind 	ok = true;
    311      1.19     rmind done:
    312      1.19     rmind 	if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
    313      1.32     rmind 		npf_recache(npc);
    314      1.19     rmind 	}
    315      1.19     rmind 	return ok;
    316       1.1     rmind }
    317       1.1     rmind 
    318      1.19     rmind static int
    319      1.19     rmind npf_cache_ip(npf_cache_t *npc, nbuf_t *nbuf)
    320       1.1     rmind {
    321      1.19     rmind 	const void *nptr = nbuf_dataptr(nbuf);
    322      1.19     rmind 	const uint8_t ver = *(const uint8_t *)nptr;
    323      1.19     rmind 	int flags = 0;
    324      1.12     rmind 
    325       1.4     rmind 	switch (ver >> 4) {
    326      1.12     rmind 	case IPVERSION: {
    327      1.19     rmind 		struct ip *ip;
    328      1.12     rmind 
    329      1.19     rmind 		ip = nbuf_ensure_contig(nbuf, sizeof(struct ip));
    330      1.19     rmind 		if (ip == NULL) {
    331      1.19     rmind 			return 0;
    332       1.4     rmind 		}
    333      1.12     rmind 
    334       1.4     rmind 		/* Check header length and fragment offset. */
    335      1.10     rmind 		if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
    336      1.19     rmind 			return 0;
    337       1.4     rmind 		}
    338       1.4     rmind 		if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
    339       1.4     rmind 			/* Note fragmentation. */
    340      1.19     rmind 			flags |= NPC_IPFRAG;
    341       1.4     rmind 		}
    342      1.12     rmind 
    343       1.4     rmind 		/* Cache: layer 3 - IPv4. */
    344      1.14     rmind 		npc->npc_alen = sizeof(struct in_addr);
    345      1.28     rmind 		npc->npc_ips[NPF_SRC] = (npf_addr_t *)&ip->ip_src;
    346      1.28     rmind 		npc->npc_ips[NPF_DST] = (npf_addr_t *)&ip->ip_dst;
    347       1.7    zoltan 		npc->npc_hlen = ip->ip_hl << 2;
    348      1.19     rmind 		npc->npc_proto = ip->ip_p;
    349      1.19     rmind 
    350      1.19     rmind 		npc->npc_ip.v4 = ip;
    351      1.19     rmind 		flags |= NPC_IP4;
    352       1.4     rmind 		break;
    353      1.12     rmind 	}
    354       1.4     rmind 
    355      1.12     rmind 	case (IPV6_VERSION >> 4): {
    356      1.19     rmind 		struct ip6_hdr *ip6;
    357      1.19     rmind 		struct ip6_ext *ip6e;
    358  1.36.2.1    bouyer 		struct ip6_frag *ip6f;
    359      1.19     rmind 		size_t off, hlen;
    360      1.19     rmind 
    361      1.19     rmind 		ip6 = nbuf_ensure_contig(nbuf, sizeof(struct ip6_hdr));
    362      1.19     rmind 		if (ip6 == NULL) {
    363      1.19     rmind 			return 0;
    364       1.7    zoltan 		}
    365      1.19     rmind 
    366      1.19     rmind 		/* Set initial next-protocol value. */
    367      1.19     rmind 		hlen = sizeof(struct ip6_hdr);
    368      1.19     rmind 		npc->npc_proto = ip6->ip6_nxt;
    369      1.13     rmind 		npc->npc_hlen = hlen;
    370       1.7    zoltan 
    371      1.12     rmind 		/*
    372      1.19     rmind 		 * Advance by the length of the current header.
    373      1.12     rmind 		 */
    374      1.19     rmind 		off = nbuf_offset(nbuf);
    375      1.19     rmind 		while (nbuf_advance(nbuf, hlen, 0) != NULL) {
    376      1.19     rmind 			ip6e = nbuf_ensure_contig(nbuf, sizeof(*ip6e));
    377      1.19     rmind 			if (ip6e == NULL) {
    378      1.19     rmind 				return 0;
    379      1.19     rmind 			}
    380      1.19     rmind 
    381      1.13     rmind 			/*
    382      1.13     rmind 			 * Determine whether we are going to continue.
    383      1.13     rmind 			 */
    384      1.19     rmind 			switch (npc->npc_proto) {
    385      1.13     rmind 			case IPPROTO_HOPOPTS:
    386       1.7    zoltan 			case IPPROTO_DSTOPTS:
    387       1.7    zoltan 			case IPPROTO_ROUTING:
    388      1.19     rmind 				hlen = (ip6e->ip6e_len + 1) << 3;
    389       1.7    zoltan 				break;
    390       1.7    zoltan 			case IPPROTO_FRAGMENT:
    391  1.36.2.1    bouyer 				ip6f = nbuf_ensure_contig(nbuf, sizeof(*ip6f));
    392  1.36.2.1    bouyer 				if (ip6f == NULL)
    393  1.36.2.1    bouyer 					return 0;
    394  1.36.2.1    bouyer 				/*
    395  1.36.2.1    bouyer 				 * We treat the first fragment as a regular
    396  1.36.2.1    bouyer 				 * packet and then we pass the rest of the
    397  1.36.2.1    bouyer 				 * fragments unconditionally. This way if
    398  1.36.2.1    bouyer 				 * the first packet passes the rest will
    399  1.36.2.1    bouyer 				 * be able to reassembled, if not they will
    400  1.36.2.1    bouyer 				 * be ignored. We can do better later.
    401  1.36.2.1    bouyer 				 */
    402  1.36.2.1    bouyer 				if (ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK) != 0)
    403  1.36.2.1    bouyer 					flags |= NPC_IPFRAG;
    404  1.36.2.1    bouyer 
    405      1.13     rmind 				hlen = sizeof(struct ip6_frag);
    406       1.7    zoltan 				break;
    407       1.7    zoltan 			case IPPROTO_AH:
    408      1.19     rmind 				hlen = (ip6e->ip6e_len + 2) << 2;
    409       1.7    zoltan 				break;
    410       1.7    zoltan 			default:
    411      1.13     rmind 				hlen = 0;
    412      1.13     rmind 				break;
    413      1.13     rmind 			}
    414      1.13     rmind 
    415      1.13     rmind 			if (!hlen) {
    416       1.7    zoltan 				break;
    417       1.7    zoltan 			}
    418      1.19     rmind 			npc->npc_proto = ip6e->ip6e_nxt;
    419      1.13     rmind 			npc->npc_hlen += hlen;
    420      1.13     rmind 		}
    421       1.7    zoltan 
    422      1.23     rmind 		/*
    423      1.23     rmind 		 * Re-fetch the header pointers (nbufs might have been
    424      1.23     rmind 		 * reallocated).  Restore the original offset (if any).
    425      1.23     rmind 		 */
    426      1.19     rmind 		nbuf_reset(nbuf);
    427      1.23     rmind 		ip6 = nbuf_dataptr(nbuf);
    428      1.19     rmind 		if (off) {
    429      1.19     rmind 			nbuf_advance(nbuf, off, 0);
    430      1.19     rmind 		}
    431      1.19     rmind 
    432      1.12     rmind 		/* Cache: layer 3 - IPv6. */
    433      1.14     rmind 		npc->npc_alen = sizeof(struct in6_addr);
    434      1.28     rmind 		npc->npc_ips[NPF_SRC] = (npf_addr_t *)&ip6->ip6_src;
    435      1.28     rmind 		npc->npc_ips[NPF_DST]= (npf_addr_t *)&ip6->ip6_dst;
    436      1.19     rmind 
    437      1.19     rmind 		npc->npc_ip.v6 = ip6;
    438      1.19     rmind 		flags |= NPC_IP6;
    439       1.7    zoltan 		break;
    440      1.12     rmind 	}
    441       1.4     rmind 	default:
    442      1.19     rmind 		break;
    443       1.4     rmind 	}
    444      1.19     rmind 	return flags;
    445       1.1     rmind }
    446       1.1     rmind 
    447       1.1     rmind /*
    448       1.4     rmind  * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
    449      1.12     rmind  * and TCP, UDP or ICMP headers.
    450      1.19     rmind  *
    451      1.19     rmind  * => nbuf offset shall be set accordingly.
    452       1.1     rmind  */
    453      1.10     rmind int
    454      1.32     rmind npf_cache_all(npf_cache_t *npc)
    455       1.1     rmind {
    456      1.32     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    457      1.19     rmind 	int flags, l4flags;
    458      1.19     rmind 	u_int hlen;
    459      1.19     rmind 
    460      1.19     rmind 	/*
    461      1.19     rmind 	 * This routine is a main point where the references are cached,
    462      1.19     rmind 	 * therefore clear the flag as we reset.
    463      1.19     rmind 	 */
    464      1.19     rmind again:
    465      1.19     rmind 	nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
    466       1.1     rmind 
    467      1.19     rmind 	/*
    468      1.19     rmind 	 * First, cache the L3 header (IPv4 or IPv6).  If IP packet is
    469      1.19     rmind 	 * fragmented, then we cannot look into L4.
    470      1.19     rmind 	 */
    471      1.19     rmind 	flags = npf_cache_ip(npc, nbuf);
    472      1.19     rmind 	if ((flags & NPC_IP46) == 0 || (flags & NPC_IPFRAG) != 0) {
    473      1.23     rmind 		nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
    474      1.19     rmind 		npc->npc_info |= flags;
    475      1.19     rmind 		return flags;
    476       1.1     rmind 	}
    477      1.19     rmind 	hlen = npc->npc_hlen;
    478      1.19     rmind 
    479      1.19     rmind 	switch (npc->npc_proto) {
    480       1.1     rmind 	case IPPROTO_TCP:
    481      1.19     rmind 		/* Cache: layer 4 - TCP. */
    482      1.19     rmind 		npc->npc_l4.tcp = nbuf_advance(nbuf, hlen,
    483      1.19     rmind 		    sizeof(struct tcphdr));
    484      1.19     rmind 		l4flags = NPC_LAYER4 | NPC_TCP;
    485      1.10     rmind 		break;
    486       1.1     rmind 	case IPPROTO_UDP:
    487      1.19     rmind 		/* Cache: layer 4 - UDP. */
    488      1.19     rmind 		npc->npc_l4.udp = nbuf_advance(nbuf, hlen,
    489      1.19     rmind 		    sizeof(struct udphdr));
    490      1.19     rmind 		l4flags = NPC_LAYER4 | NPC_UDP;
    491      1.10     rmind 		break;
    492       1.1     rmind 	case IPPROTO_ICMP:
    493      1.19     rmind 		/* Cache: layer 4 - ICMPv4. */
    494      1.19     rmind 		npc->npc_l4.icmp = nbuf_advance(nbuf, hlen,
    495      1.19     rmind 		    offsetof(struct icmp, icmp_void));
    496      1.19     rmind 		l4flags = NPC_LAYER4 | NPC_ICMP;
    497      1.19     rmind 		break;
    498      1.15       spz 	case IPPROTO_ICMPV6:
    499      1.19     rmind 		/* Cache: layer 4 - ICMPv6. */
    500      1.19     rmind 		npc->npc_l4.icmp6 = nbuf_advance(nbuf, hlen,
    501      1.19     rmind 		    offsetof(struct icmp6_hdr, icmp6_data32));
    502      1.19     rmind 		l4flags = NPC_LAYER4 | NPC_ICMP;
    503      1.19     rmind 		break;
    504      1.19     rmind 	default:
    505      1.19     rmind 		l4flags = 0;
    506      1.10     rmind 		break;
    507       1.1     rmind 	}
    508      1.19     rmind 
    509      1.19     rmind 	if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
    510      1.19     rmind 		goto again;
    511      1.19     rmind 	}
    512      1.19     rmind 
    513      1.19     rmind 	/* Add the L4 flags if nbuf_advance() succeeded. */
    514      1.19     rmind 	if (l4flags && npc->npc_l4.hdr) {
    515      1.19     rmind 		flags |= l4flags;
    516      1.19     rmind 	}
    517      1.19     rmind 	npc->npc_info |= flags;
    518      1.19     rmind 	return flags;
    519      1.19     rmind }
    520      1.19     rmind 
    521      1.19     rmind void
    522      1.32     rmind npf_recache(npf_cache_t *npc)
    523      1.19     rmind {
    524      1.32     rmind 	nbuf_t *nbuf = npc->npc_nbuf;
    525      1.24    martin 	const int mflags __diagused = npc->npc_info & (NPC_IP46 | NPC_LAYER4);
    526      1.25       mrg 	int flags __diagused;
    527      1.19     rmind 
    528      1.19     rmind 	nbuf_reset(nbuf);
    529      1.19     rmind 	npc->npc_info = 0;
    530      1.32     rmind 	flags = npf_cache_all(npc);
    531      1.32     rmind 
    532      1.19     rmind 	KASSERT((flags & mflags) == mflags);
    533      1.19     rmind 	KASSERT(nbuf_flag_p(nbuf, NBUF_DATAREF_RESET) == 0);
    534       1.1     rmind }
    535       1.1     rmind 
    536       1.1     rmind /*
    537      1.19     rmind  * npf_rwrip: rewrite required IP address.
    538       1.4     rmind  */
    539       1.4     rmind bool
    540      1.28     rmind npf_rwrip(const npf_cache_t *npc, u_int which, const npf_addr_t *addr)
    541       1.4     rmind {
    542       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    543      1.28     rmind 	KASSERT(which == NPF_SRC || which == NPF_DST);
    544       1.4     rmind 
    545      1.28     rmind 	memcpy(npc->npc_ips[which], addr, npc->npc_alen);
    546       1.4     rmind 	return true;
    547       1.4     rmind }
    548       1.4     rmind 
    549       1.4     rmind /*
    550      1.19     rmind  * npf_rwrport: rewrite required TCP/UDP port.
    551       1.1     rmind  */
    552       1.1     rmind bool
    553      1.28     rmind npf_rwrport(const npf_cache_t *npc, u_int which, const in_port_t port)
    554       1.1     rmind {
    555      1.21     rmind 	const int proto = npc->npc_proto;
    556       1.4     rmind 	in_port_t *oport;
    557       1.1     rmind 
    558       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    559       1.1     rmind 	KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
    560      1.28     rmind 	KASSERT(which == NPF_SRC || which == NPF_DST);
    561       1.1     rmind 
    562      1.19     rmind 	/* Get the offset and store the port in it. */
    563       1.4     rmind 	if (proto == IPPROTO_TCP) {
    564      1.19     rmind 		struct tcphdr *th = npc->npc_l4.tcp;
    565      1.28     rmind 		oport = (which == NPF_SRC) ? &th->th_sport : &th->th_dport;
    566       1.1     rmind 	} else {
    567      1.19     rmind 		struct udphdr *uh = npc->npc_l4.udp;
    568      1.28     rmind 		oport = (which == NPF_SRC) ? &uh->uh_sport : &uh->uh_dport;
    569       1.1     rmind 	}
    570      1.19     rmind 	memcpy(oport, &port, sizeof(in_port_t));
    571       1.1     rmind 	return true;
    572       1.1     rmind }
    573       1.1     rmind 
    574       1.1     rmind /*
    575      1.19     rmind  * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum.
    576       1.1     rmind  */
    577       1.1     rmind bool
    578      1.28     rmind npf_rwrcksum(const npf_cache_t *npc, u_int which,
    579      1.19     rmind     const npf_addr_t *addr, const in_port_t port)
    580       1.1     rmind {
    581      1.28     rmind 	const npf_addr_t *oaddr = npc->npc_ips[which];
    582      1.21     rmind 	const int proto = npc->npc_proto;
    583      1.19     rmind 	const int alen = npc->npc_alen;
    584      1.18     rmind 	uint16_t *ocksum;
    585      1.18     rmind 	in_port_t oport;
    586      1.18     rmind 
    587      1.19     rmind 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    588      1.28     rmind 	KASSERT(which == NPF_SRC || which == NPF_DST);
    589      1.18     rmind 
    590       1.4     rmind 	if (npf_iscached(npc, NPC_IP4)) {
    591      1.19     rmind 		struct ip *ip = npc->npc_ip.v4;
    592      1.19     rmind 		uint16_t ipsum = ip->ip_sum;
    593       1.4     rmind 
    594      1.19     rmind 		/* Recalculate IPv4 checksum and rewrite. */
    595      1.19     rmind 		ip->ip_sum = npf_addr_cksum(ipsum, alen, oaddr, addr);
    596       1.4     rmind 	} else {
    597       1.4     rmind 		/* No checksum for IPv6. */
    598       1.4     rmind 		KASSERT(npf_iscached(npc, NPC_IP6));
    599       1.4     rmind 	}
    600       1.4     rmind 
    601      1.18     rmind 	/* Nothing else to do for ICMP. */
    602      1.30     rmind 	if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) {
    603       1.4     rmind 		return true;
    604       1.4     rmind 	}
    605       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    606       1.4     rmind 
    607      1.18     rmind 	/*
    608      1.18     rmind 	 * Calculate TCP/UDP checksum:
    609      1.18     rmind 	 * - Skip if UDP and the current checksum is zero.
    610      1.18     rmind 	 * - Fixup the IP address change.
    611      1.18     rmind 	 * - Fixup the port change, if required (non-zero).
    612      1.18     rmind 	 */
    613       1.4     rmind 	if (proto == IPPROTO_TCP) {
    614      1.19     rmind 		struct tcphdr *th = npc->npc_l4.tcp;
    615       1.4     rmind 
    616      1.18     rmind 		ocksum = &th->th_sum;
    617      1.28     rmind 		oport = (which == NPF_SRC) ? th->th_sport : th->th_dport;
    618       1.4     rmind 	} else {
    619      1.19     rmind 		struct udphdr *uh = npc->npc_l4.udp;
    620       1.4     rmind 
    621       1.4     rmind 		KASSERT(proto == IPPROTO_UDP);
    622      1.18     rmind 		ocksum = &uh->uh_sum;
    623      1.18     rmind 		if (*ocksum == 0) {
    624       1.4     rmind 			/* No need to update. */
    625       1.4     rmind 			return true;
    626       1.4     rmind 		}
    627      1.28     rmind 		oport = (which == NPF_SRC) ? uh->uh_sport : uh->uh_dport;
    628      1.18     rmind 	}
    629      1.18     rmind 
    630      1.19     rmind 	uint16_t cksum = npf_addr_cksum(*ocksum, alen, oaddr, addr);
    631      1.18     rmind 	if (port) {
    632      1.18     rmind 		cksum = npf_fixup16_cksum(cksum, oport, port);
    633       1.4     rmind 	}
    634       1.1     rmind 
    635      1.19     rmind 	/* Rewrite TCP/UDP checksum. */
    636      1.19     rmind 	memcpy(ocksum, &cksum, sizeof(uint16_t));
    637       1.4     rmind 	return true;
    638       1.4     rmind }
    639       1.4     rmind 
    640      1.29     rmind /*
    641      1.30     rmind  * npf_napt_rwr: perform address and/or port translation.
    642      1.30     rmind  */
    643      1.30     rmind int
    644      1.30     rmind npf_napt_rwr(const npf_cache_t *npc, u_int which,
    645      1.30     rmind     const npf_addr_t *addr, const in_addr_t port)
    646      1.30     rmind {
    647      1.30     rmind 	const unsigned proto = npc->npc_proto;
    648      1.30     rmind 
    649      1.30     rmind 	/*
    650      1.30     rmind 	 * Rewrite IP and/or TCP/UDP checksums first, since we need the
    651      1.30     rmind 	 * current (old) address/port for the calculations.  Then perform
    652      1.30     rmind 	 * the address translation i.e. rewrite source or destination.
    653      1.30     rmind 	 */
    654      1.30     rmind 	if (!npf_rwrcksum(npc, which, addr, port)) {
    655      1.30     rmind 		return EINVAL;
    656      1.30     rmind 	}
    657      1.30     rmind 	if (!npf_rwrip(npc, which, addr)) {
    658      1.30     rmind 		return EINVAL;
    659      1.30     rmind 	}
    660      1.30     rmind 	if (port == 0) {
    661      1.30     rmind 		/* Done. */
    662      1.30     rmind 		return 0;
    663      1.30     rmind 	}
    664      1.30     rmind 
    665      1.30     rmind 	switch (proto) {
    666      1.30     rmind 	case IPPROTO_TCP:
    667      1.30     rmind 	case IPPROTO_UDP:
    668      1.30     rmind 		/* Rewrite source/destination port. */
    669      1.30     rmind 		if (!npf_rwrport(npc, which, port)) {
    670      1.30     rmind 			return EINVAL;
    671      1.30     rmind 		}
    672      1.30     rmind 		break;
    673      1.30     rmind 	case IPPROTO_ICMP:
    674      1.30     rmind 	case IPPROTO_ICMPV6:
    675      1.30     rmind 		KASSERT(npf_iscached(npc, NPC_ICMP));
    676      1.30     rmind 		/* Nothing. */
    677      1.30     rmind 		break;
    678      1.30     rmind 	default:
    679      1.30     rmind 		return ENOTSUP;
    680      1.30     rmind 	}
    681      1.30     rmind 	return 0;
    682      1.30     rmind }
    683      1.30     rmind 
    684      1.30     rmind /*
    685      1.29     rmind  * IPv6-to-IPv6 Network Prefix Translation (NPTv6), as per RFC 6296.
    686      1.29     rmind  */
    687      1.29     rmind 
    688      1.29     rmind int
    689      1.29     rmind npf_npt66_rwr(const npf_cache_t *npc, u_int which, const npf_addr_t *pref,
    690      1.29     rmind     npf_netmask_t len, uint16_t adj)
    691      1.29     rmind {
    692      1.29     rmind 	npf_addr_t *addr = npc->npc_ips[which];
    693      1.29     rmind 	unsigned remnant, word, preflen = len >> 4;
    694      1.29     rmind 	uint32_t sum;
    695      1.29     rmind 
    696      1.29     rmind 	KASSERT(which == NPF_SRC || which == NPF_DST);
    697      1.29     rmind 
    698      1.29     rmind 	if (!npf_iscached(npc, NPC_IP6)) {
    699      1.29     rmind 		return EINVAL;
    700      1.29     rmind 	}
    701      1.29     rmind 	if (len <= 48) {
    702      1.29     rmind 		/*
    703      1.29     rmind 		 * The word to adjust.  Cannot translate the 0xffff
    704      1.29     rmind 		 * subnet if /48 or shorter.
    705      1.29     rmind 		 */
    706      1.29     rmind 		word = 3;
    707      1.36  christos 		if (addr->word16[word] == 0xffff) {
    708      1.29     rmind 			return EINVAL;
    709      1.29     rmind 		}
    710      1.29     rmind 	} else {
    711      1.29     rmind 		/*
    712      1.29     rmind 		 * Also, all 0s or 1s in the host part are disallowed for
    713      1.29     rmind 		 * longer than /48 prefixes.
    714      1.29     rmind 		 */
    715      1.36  christos 		if ((addr->word32[2] == 0 && addr->word32[3] == 0) ||
    716      1.36  christos 		    (addr->word32[2] == ~0U && addr->word32[3] == ~0U))
    717      1.29     rmind 			return EINVAL;
    718      1.29     rmind 
    719      1.29     rmind 		/* Determine the 16-bit word to adjust. */
    720      1.29     rmind 		for (word = 4; word < 8; word++)
    721      1.36  christos 			if (addr->word16[word] != 0xffff)
    722      1.29     rmind 				break;
    723      1.29     rmind 	}
    724      1.29     rmind 
    725      1.29     rmind 	/* Rewrite the prefix. */
    726      1.29     rmind 	for (unsigned i = 0; i < preflen; i++) {
    727      1.36  christos 		addr->word16[i] = pref->word16[i];
    728      1.29     rmind 	}
    729      1.29     rmind 
    730      1.29     rmind 	/*
    731      1.29     rmind 	 * If prefix length is within a 16-bit word (not dividable by 16),
    732      1.29     rmind 	 * then prepare a mask, determine the word and adjust it.
    733      1.29     rmind 	 */
    734      1.29     rmind 	if ((remnant = len - (preflen << 4)) != 0) {
    735      1.29     rmind 		const uint16_t wordmask = (1U << remnant) - 1;
    736      1.29     rmind 		const unsigned i = preflen;
    737      1.29     rmind 
    738      1.36  christos 		addr->word16[i] = (pref->word16[i] & wordmask) |
    739      1.36  christos 		    (addr->word16[i] & ~wordmask);
    740      1.29     rmind 	}
    741      1.29     rmind 
    742      1.29     rmind 	/*
    743      1.29     rmind 	 * Performing 1's complement sum/difference.
    744      1.29     rmind 	 */
    745      1.36  christos 	sum = addr->word16[word] + adj;
    746      1.29     rmind 	while (sum >> 16) {
    747      1.29     rmind 		sum = (sum >> 16) + (sum & 0xffff);
    748      1.29     rmind 	}
    749      1.29     rmind 	if (sum == 0xffff) {
    750      1.29     rmind 		/* RFC 1071. */
    751      1.29     rmind 		sum = 0x0000;
    752      1.29     rmind 	}
    753      1.36  christos 	addr->word16[word] = sum;
    754      1.29     rmind 	return 0;
    755      1.29     rmind }
    756      1.29     rmind 
    757      1.13     rmind #if defined(DDB) || defined(_NPF_TESTING)
    758      1.13     rmind 
    759      1.31     rmind const char *
    760      1.31     rmind npf_addr_dump(const npf_addr_t *addr, int alen)
    761      1.13     rmind {
    762      1.31     rmind 	if (alen == sizeof(struct in_addr)) {
    763      1.31     rmind 		struct in_addr ip;
    764      1.31     rmind 		memcpy(&ip, addr, alen);
    765      1.31     rmind 		return inet_ntoa(ip);
    766      1.31     rmind 	}
    767      1.36  christos 	return "[IPv6]";
    768      1.13     rmind }
    769      1.13     rmind 
    770      1.13     rmind #endif
    771