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