Home | History | Annotate | Line # | Download | only in npf
npf_inet.c revision 1.6.8.5
      1  1.6.8.5   yamt /*	$NetBSD: npf_inet.c,v 1.6.8.5 2013/01/23 00:06:25 yamt Exp $	*/
      2      1.1  rmind 
      3      1.1  rmind /*-
      4  1.6.8.3   yamt  * Copyright (c) 2009-2012 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  * Various procotol related helper routines.
     34  1.6.8.3   yamt  *
     35  1.6.8.3   yamt  * This layer manipulates npf_cache_t structure i.e. caches requested headers
     36  1.6.8.3   yamt  * and stores which information was cached in the information bit field.
     37  1.6.8.3   yamt  * It is also responsibility of this layer to update or invalidate the cache
     38  1.6.8.3   yamt  * on rewrites (e.g. by translation routines).
     39      1.1  rmind  */
     40      1.1  rmind 
     41      1.1  rmind #include <sys/cdefs.h>
     42  1.6.8.5   yamt __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.6.8.5 2013/01/23 00:06:25 yamt Exp $");
     43      1.1  rmind 
     44      1.1  rmind #include <sys/param.h>
     45  1.6.8.2   yamt #include <sys/types.h>
     46      1.1  rmind 
     47      1.4  rmind #include <net/pfil.h>
     48      1.4  rmind #include <net/if.h>
     49      1.4  rmind #include <net/ethertypes.h>
     50      1.4  rmind #include <net/if_ether.h>
     51      1.4  rmind 
     52      1.1  rmind #include <netinet/in_systm.h>
     53      1.1  rmind #include <netinet/in.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.1  rmind 
     60      1.1  rmind #include "npf_impl.h"
     61      1.1  rmind 
     62      1.1  rmind /*
     63      1.1  rmind  * npf_fixup{16,32}_cksum: update IPv4 checksum.
     64      1.1  rmind  */
     65      1.1  rmind 
     66      1.1  rmind uint16_t
     67      1.1  rmind npf_fixup16_cksum(uint16_t cksum, uint16_t odatum, uint16_t ndatum)
     68      1.1  rmind {
     69      1.1  rmind 	uint32_t sum;
     70      1.1  rmind 
     71      1.1  rmind 	/*
     72      1.1  rmind 	 * RFC 1624:
     73      1.1  rmind 	 *	HC' = ~(~HC + ~m + m')
     74      1.1  rmind 	 */
     75      1.1  rmind 	sum = ~ntohs(cksum) & 0xffff;
     76      1.1  rmind 	sum += (~ntohs(odatum) & 0xffff) + ntohs(ndatum);
     77      1.1  rmind 	sum = (sum >> 16) + (sum & 0xffff);
     78      1.1  rmind 	sum += (sum >> 16);
     79      1.1  rmind 
     80      1.1  rmind 	return htons(~sum & 0xffff);
     81      1.1  rmind }
     82      1.1  rmind 
     83      1.1  rmind uint16_t
     84      1.1  rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
     85      1.1  rmind {
     86      1.1  rmind 
     87      1.1  rmind 	cksum = npf_fixup16_cksum(cksum, odatum & 0xffff, ndatum & 0xffff);
     88      1.1  rmind 	cksum = npf_fixup16_cksum(cksum, odatum >> 16, ndatum >> 16);
     89      1.1  rmind 	return cksum;
     90      1.1  rmind }
     91      1.1  rmind 
     92      1.1  rmind /*
     93      1.4  rmind  * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
     94      1.4  rmind  */
     95      1.4  rmind uint16_t
     96  1.6.8.5   yamt npf_addr_cksum(uint16_t cksum, int sz, const npf_addr_t *oaddr,
     97  1.6.8.5   yamt     const npf_addr_t *naddr)
     98      1.4  rmind {
     99  1.6.8.5   yamt 	const uint32_t *oip32 = (const uint32_t *)oaddr;
    100  1.6.8.5   yamt 	const uint32_t *nip32 = (const uint32_t *)naddr;
    101      1.4  rmind 
    102      1.4  rmind 	KASSERT(sz % sizeof(uint32_t) == 0);
    103      1.4  rmind 	do {
    104      1.4  rmind 		cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
    105      1.4  rmind 		sz -= sizeof(uint32_t);
    106      1.4  rmind 	} while (sz);
    107      1.4  rmind 
    108      1.4  rmind 	return cksum;
    109      1.4  rmind }
    110      1.4  rmind 
    111      1.4  rmind /*
    112      1.4  rmind  * npf_addr_sum: provide IP address as a summed (if needed) 32-bit integer.
    113      1.4  rmind  * Note: used for hash function.
    114      1.1  rmind  */
    115      1.4  rmind uint32_t
    116      1.4  rmind npf_addr_sum(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
    117      1.1  rmind {
    118      1.4  rmind 	uint32_t mix = 0;
    119      1.4  rmind 	int i;
    120      1.1  rmind 
    121      1.5  rmind 	KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
    122      1.5  rmind 
    123      1.4  rmind 	for (i = 0; i < (sz >> 2); i++) {
    124      1.4  rmind 		mix += a1->s6_addr32[i];
    125      1.4  rmind 		mix += a2->s6_addr32[i];
    126      1.4  rmind 	}
    127      1.4  rmind 	return mix;
    128      1.4  rmind }
    129      1.1  rmind 
    130      1.4  rmind /*
    131  1.6.8.3   yamt  * npf_addr_mask: apply the mask to a given address and store the result.
    132  1.6.8.3   yamt  */
    133  1.6.8.3   yamt void
    134  1.6.8.3   yamt npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask,
    135  1.6.8.3   yamt     const int alen, npf_addr_t *out)
    136  1.6.8.3   yamt {
    137  1.6.8.3   yamt 	const int nwords = alen >> 2;
    138  1.6.8.3   yamt 	uint_fast8_t length = mask;
    139  1.6.8.3   yamt 
    140  1.6.8.3   yamt 	/* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
    141  1.6.8.3   yamt 	KASSERT(length <= NPF_MAX_NETMASK);
    142  1.6.8.3   yamt 
    143  1.6.8.3   yamt 	for (int i = 0; i < nwords; i++) {
    144  1.6.8.3   yamt 		uint32_t wordmask;
    145  1.6.8.3   yamt 
    146  1.6.8.3   yamt 		if (length >= 32) {
    147  1.6.8.3   yamt 			wordmask = htonl(0xffffffff);
    148  1.6.8.3   yamt 			length -= 32;
    149  1.6.8.3   yamt 		} else if (length) {
    150  1.6.8.3   yamt 			wordmask = htonl(0xffffffff << (32 - length));
    151  1.6.8.3   yamt 			length = 0;
    152  1.6.8.3   yamt 		} else {
    153  1.6.8.3   yamt 			wordmask = 0;
    154  1.6.8.3   yamt 		}
    155  1.6.8.3   yamt 		out->s6_addr32[i] = addr->s6_addr32[i] & wordmask;
    156  1.6.8.3   yamt 	}
    157  1.6.8.3   yamt }
    158  1.6.8.3   yamt 
    159  1.6.8.3   yamt /*
    160  1.6.8.3   yamt  * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
    161  1.6.8.3   yamt  *
    162  1.6.8.3   yamt  * => Return 0 if equal and negative/positive if less/greater accordingly.
    163  1.6.8.3   yamt  * => Ignore the mask, if NPF_NO_NETMASK is specified.
    164  1.6.8.3   yamt  */
    165  1.6.8.3   yamt int
    166  1.6.8.3   yamt npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
    167  1.6.8.3   yamt     const npf_addr_t *addr2, const npf_netmask_t mask2, const int alen)
    168  1.6.8.3   yamt {
    169  1.6.8.3   yamt 	npf_addr_t realaddr1, realaddr2;
    170  1.6.8.3   yamt 
    171  1.6.8.3   yamt 	if (mask1 != NPF_NO_NETMASK) {
    172  1.6.8.3   yamt 		npf_addr_mask(addr1, mask1, alen, &realaddr1);
    173  1.6.8.3   yamt 		addr1 = &realaddr1;
    174  1.6.8.3   yamt 	}
    175  1.6.8.3   yamt 	if (mask2 != NPF_NO_NETMASK) {
    176  1.6.8.3   yamt 		npf_addr_mask(addr2, mask2, alen, &realaddr2);
    177  1.6.8.3   yamt 		addr2 = &realaddr2;
    178  1.6.8.3   yamt 	}
    179  1.6.8.3   yamt 	return memcmp(addr1, addr2, alen);
    180  1.6.8.3   yamt }
    181  1.6.8.3   yamt 
    182  1.6.8.3   yamt /*
    183      1.4  rmind  * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
    184  1.6.8.3   yamt  *
    185  1.6.8.3   yamt  * => Returns all values in host byte-order.
    186      1.4  rmind  */
    187      1.4  rmind int
    188  1.6.8.3   yamt npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
    189      1.4  rmind {
    190  1.6.8.5   yamt 	const struct tcphdr *th = npc->npc_l4.tcp;
    191  1.6.8.1   yamt 	u_int thlen;
    192      1.1  rmind 
    193  1.6.8.1   yamt 	KASSERT(npf_iscached(npc, NPC_TCP));
    194      1.1  rmind 
    195      1.4  rmind 	*seq = ntohl(th->th_seq);
    196      1.4  rmind 	*ack = ntohl(th->th_ack);
    197      1.4  rmind 	*win = (uint32_t)ntohs(th->th_win);
    198  1.6.8.1   yamt 	thlen = th->th_off << 2;
    199      1.1  rmind 
    200  1.6.8.1   yamt 	if (npf_iscached(npc, NPC_IP4)) {
    201  1.6.8.5   yamt 		const struct ip *ip = npc->npc_ip.v4;
    202  1.6.8.2   yamt 		return ntohs(ip->ip_len) - npf_cache_hlen(npc) - thlen;
    203  1.6.8.3   yamt 	} else if (npf_iscached(npc, NPC_IP6)) {
    204  1.6.8.5   yamt 		const struct ip6_hdr *ip6 = npc->npc_ip.v6;
    205  1.6.8.1   yamt 		return ntohs(ip6->ip6_plen) - thlen;
    206  1.6.8.1   yamt 	}
    207  1.6.8.1   yamt 	return 0;
    208      1.1  rmind }
    209      1.1  rmind 
    210      1.1  rmind /*
    211      1.4  rmind  * npf_fetch_tcpopts: parse and return TCP options.
    212      1.1  rmind  */
    213      1.1  rmind bool
    214  1.6.8.5   yamt npf_fetch_tcpopts(npf_cache_t *npc, nbuf_t *nbuf, uint16_t *mss, int *wscale)
    215      1.1  rmind {
    216  1.6.8.5   yamt 	const struct tcphdr *th = npc->npc_l4.tcp;
    217      1.4  rmind 	int topts_len, step;
    218  1.6.8.5   yamt 	void *nptr;
    219      1.4  rmind 	uint8_t val;
    220  1.6.8.5   yamt 	bool ok;
    221      1.4  rmind 
    222  1.6.8.1   yamt 	KASSERT(npf_iscached(npc, NPC_IP46));
    223  1.6.8.1   yamt 	KASSERT(npf_iscached(npc, NPC_TCP));
    224  1.6.8.2   yamt 
    225      1.4  rmind 	/* Determine if there are any TCP options, get their length. */
    226      1.4  rmind 	topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
    227      1.4  rmind 	if (topts_len <= 0) {
    228      1.4  rmind 		/* No options. */
    229      1.1  rmind 		return false;
    230      1.4  rmind 	}
    231      1.4  rmind 	KASSERT(topts_len <= MAX_TCPOPTLEN);
    232      1.1  rmind 
    233      1.4  rmind 	/* First step: IP and TCP header up to options. */
    234  1.6.8.2   yamt 	step = npf_cache_hlen(npc) + sizeof(struct tcphdr);
    235  1.6.8.5   yamt 	nbuf_reset(nbuf);
    236      1.4  rmind next:
    237  1.6.8.5   yamt 	if ((nptr = nbuf_advance(nbuf, step, 1)) == NULL) {
    238  1.6.8.5   yamt 		ok = false;
    239  1.6.8.5   yamt 		goto done;
    240      1.4  rmind 	}
    241  1.6.8.5   yamt 	val = *(uint8_t *)nptr;
    242  1.6.8.3   yamt 
    243      1.4  rmind 	switch (val) {
    244      1.4  rmind 	case TCPOPT_EOL:
    245      1.4  rmind 		/* Done. */
    246  1.6.8.5   yamt 		ok = true;
    247  1.6.8.5   yamt 		goto done;
    248      1.4  rmind 	case TCPOPT_NOP:
    249      1.4  rmind 		topts_len--;
    250      1.4  rmind 		step = 1;
    251      1.4  rmind 		break;
    252      1.4  rmind 	case TCPOPT_MAXSEG:
    253  1.6.8.5   yamt 		if ((nptr = nbuf_advance(nbuf, 2, 2)) == NULL) {
    254  1.6.8.5   yamt 			ok = false;
    255  1.6.8.5   yamt 			goto done;
    256      1.4  rmind 		}
    257      1.4  rmind 		if (mss) {
    258  1.6.8.5   yamt 			if (*mss) {
    259  1.6.8.5   yamt 				memcpy(nptr, mss, sizeof(uint16_t));
    260  1.6.8.5   yamt 			} else {
    261  1.6.8.5   yamt 				memcpy(mss, nptr, sizeof(uint16_t));
    262  1.6.8.5   yamt 			}
    263      1.4  rmind 		}
    264      1.4  rmind 		topts_len -= TCPOLEN_MAXSEG;
    265  1.6.8.5   yamt 		step = 2;
    266      1.4  rmind 		break;
    267      1.4  rmind 	case TCPOPT_WINDOW:
    268  1.6.8.2   yamt 		/* TCP Window Scaling (RFC 1323). */
    269  1.6.8.5   yamt 		if ((nptr = nbuf_advance(nbuf, 2, 1)) == NULL) {
    270  1.6.8.5   yamt 			ok = false;
    271  1.6.8.5   yamt 			goto done;
    272      1.4  rmind 		}
    273  1.6.8.5   yamt 		val = *(uint8_t *)nptr;
    274      1.4  rmind 		*wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
    275      1.4  rmind 		topts_len -= TCPOLEN_WINDOW;
    276  1.6.8.5   yamt 		step = 1;
    277      1.4  rmind 		break;
    278      1.4  rmind 	default:
    279  1.6.8.5   yamt 		if ((nptr = nbuf_advance(nbuf, 1, 1)) == NULL) {
    280  1.6.8.5   yamt 			ok = false;
    281  1.6.8.5   yamt 			goto done;
    282      1.4  rmind 		}
    283  1.6.8.5   yamt 		val = *(uint8_t *)nptr;
    284  1.6.8.3   yamt 		if (val < 2 || val > topts_len) {
    285  1.6.8.5   yamt 			ok = false;
    286  1.6.8.5   yamt 			goto done;
    287      1.4  rmind 		}
    288      1.4  rmind 		topts_len -= val;
    289      1.4  rmind 		step = val - 1;
    290      1.4  rmind 	}
    291  1.6.8.3   yamt 
    292      1.6  rmind 	/* Any options left? */
    293      1.4  rmind 	if (__predict_true(topts_len > 0)) {
    294      1.4  rmind 		goto next;
    295      1.4  rmind 	}
    296  1.6.8.5   yamt 	ok = true;
    297  1.6.8.5   yamt done:
    298  1.6.8.5   yamt 	if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
    299  1.6.8.5   yamt 		npf_recache(npc, nbuf);
    300  1.6.8.5   yamt 	}
    301  1.6.8.5   yamt 	return ok;
    302      1.1  rmind }
    303      1.1  rmind 
    304  1.6.8.5   yamt static int
    305  1.6.8.5   yamt npf_cache_ip(npf_cache_t *npc, nbuf_t *nbuf)
    306      1.1  rmind {
    307  1.6.8.5   yamt 	const void *nptr = nbuf_dataptr(nbuf);
    308  1.6.8.5   yamt 	const uint8_t ver = *(const uint8_t *)nptr;
    309  1.6.8.5   yamt 	int flags = 0;
    310  1.6.8.3   yamt 
    311      1.4  rmind 	switch (ver >> 4) {
    312  1.6.8.3   yamt 	case IPVERSION: {
    313  1.6.8.5   yamt 		struct ip *ip;
    314  1.6.8.3   yamt 
    315  1.6.8.5   yamt 		ip = nbuf_ensure_contig(nbuf, sizeof(struct ip));
    316  1.6.8.5   yamt 		if (ip == NULL) {
    317  1.6.8.5   yamt 			return 0;
    318      1.4  rmind 		}
    319  1.6.8.3   yamt 
    320      1.4  rmind 		/* Check header length and fragment offset. */
    321  1.6.8.2   yamt 		if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
    322  1.6.8.5   yamt 			return 0;
    323      1.4  rmind 		}
    324      1.4  rmind 		if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
    325      1.4  rmind 			/* Note fragmentation. */
    326  1.6.8.5   yamt 			flags |= NPC_IPFRAG;
    327      1.4  rmind 		}
    328  1.6.8.3   yamt 
    329      1.4  rmind 		/* Cache: layer 3 - IPv4. */
    330  1.6.8.3   yamt 		npc->npc_alen = sizeof(struct in_addr);
    331      1.4  rmind 		npc->npc_srcip = (npf_addr_t *)&ip->ip_src;
    332      1.4  rmind 		npc->npc_dstip = (npf_addr_t *)&ip->ip_dst;
    333  1.6.8.1   yamt 		npc->npc_hlen = ip->ip_hl << 2;
    334  1.6.8.5   yamt 		npc->npc_proto = ip->ip_p;
    335  1.6.8.5   yamt 
    336  1.6.8.5   yamt 		npc->npc_ip.v4 = ip;
    337  1.6.8.5   yamt 		flags |= NPC_IP4;
    338      1.4  rmind 		break;
    339  1.6.8.3   yamt 	}
    340  1.6.8.3   yamt 
    341  1.6.8.3   yamt 	case (IPV6_VERSION >> 4): {
    342  1.6.8.5   yamt 		struct ip6_hdr *ip6;
    343  1.6.8.5   yamt 		struct ip6_ext *ip6e;
    344  1.6.8.5   yamt 		size_t off, hlen;
    345  1.6.8.5   yamt 
    346  1.6.8.5   yamt 		ip6 = nbuf_ensure_contig(nbuf, sizeof(struct ip6_hdr));
    347  1.6.8.5   yamt 		if (ip6 == NULL) {
    348  1.6.8.5   yamt 			return 0;
    349  1.6.8.1   yamt 		}
    350  1.6.8.5   yamt 
    351  1.6.8.5   yamt 		/* Set initial next-protocol value. */
    352  1.6.8.5   yamt 		hlen = sizeof(struct ip6_hdr);
    353  1.6.8.5   yamt 		npc->npc_proto = ip6->ip6_nxt;
    354  1.6.8.3   yamt 		npc->npc_hlen = hlen;
    355  1.6.8.1   yamt 
    356  1.6.8.3   yamt 		/*
    357  1.6.8.5   yamt 		 * Advance by the length of the current header.
    358  1.6.8.3   yamt 		 */
    359  1.6.8.5   yamt 		off = nbuf_offset(nbuf);
    360  1.6.8.5   yamt 		while (nbuf_advance(nbuf, hlen, 0) != NULL) {
    361  1.6.8.5   yamt 			ip6e = nbuf_ensure_contig(nbuf, sizeof(*ip6e));
    362  1.6.8.5   yamt 			if (ip6e == NULL) {
    363  1.6.8.5   yamt 				return 0;
    364  1.6.8.5   yamt 			}
    365  1.6.8.5   yamt 
    366  1.6.8.1   yamt 			/*
    367  1.6.8.3   yamt 			 * Determine whether we are going to continue.
    368  1.6.8.1   yamt 			 */
    369  1.6.8.5   yamt 			switch (npc->npc_proto) {
    370  1.6.8.3   yamt 			case IPPROTO_HOPOPTS:
    371  1.6.8.1   yamt 			case IPPROTO_DSTOPTS:
    372  1.6.8.1   yamt 			case IPPROTO_ROUTING:
    373  1.6.8.5   yamt 				hlen = (ip6e->ip6e_len + 1) << 3;
    374  1.6.8.1   yamt 				break;
    375  1.6.8.1   yamt 			case IPPROTO_FRAGMENT:
    376  1.6.8.3   yamt 				hlen = sizeof(struct ip6_frag);
    377  1.6.8.5   yamt 				flags |= NPC_IPFRAG;
    378  1.6.8.1   yamt 				break;
    379  1.6.8.1   yamt 			case IPPROTO_AH:
    380  1.6.8.5   yamt 				hlen = (ip6e->ip6e_len + 2) << 2;
    381  1.6.8.1   yamt 				break;
    382  1.6.8.1   yamt 			default:
    383  1.6.8.3   yamt 				hlen = 0;
    384  1.6.8.1   yamt 				break;
    385  1.6.8.1   yamt 			}
    386  1.6.8.1   yamt 
    387  1.6.8.3   yamt 			if (!hlen) {
    388  1.6.8.3   yamt 				break;
    389  1.6.8.3   yamt 			}
    390  1.6.8.5   yamt 			npc->npc_proto = ip6e->ip6e_nxt;
    391  1.6.8.3   yamt 			npc->npc_hlen += hlen;
    392  1.6.8.3   yamt 		}
    393  1.6.8.1   yamt 
    394  1.6.8.5   yamt 		/* Restore the offset. */
    395  1.6.8.5   yamt 		nbuf_reset(nbuf);
    396  1.6.8.5   yamt 		if (off) {
    397  1.6.8.5   yamt 			nbuf_advance(nbuf, off, 0);
    398  1.6.8.5   yamt 		}
    399  1.6.8.5   yamt 
    400  1.6.8.3   yamt 		/* Cache: layer 3 - IPv6. */
    401  1.6.8.3   yamt 		npc->npc_alen = sizeof(struct in6_addr);
    402  1.6.8.1   yamt 		npc->npc_srcip = (npf_addr_t *)&ip6->ip6_src;
    403  1.6.8.1   yamt 		npc->npc_dstip = (npf_addr_t *)&ip6->ip6_dst;
    404  1.6.8.5   yamt 
    405  1.6.8.5   yamt 		npc->npc_ip.v6 = ip6;
    406  1.6.8.5   yamt 		flags |= NPC_IP6;
    407  1.6.8.1   yamt 		break;
    408  1.6.8.3   yamt 	}
    409      1.4  rmind 	default:
    410  1.6.8.5   yamt 		break;
    411      1.4  rmind 	}
    412  1.6.8.5   yamt 	return flags;
    413      1.1  rmind }
    414      1.1  rmind 
    415      1.1  rmind /*
    416      1.4  rmind  * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
    417  1.6.8.3   yamt  * and TCP, UDP or ICMP headers.
    418  1.6.8.5   yamt  *
    419  1.6.8.5   yamt  * => nbuf offset shall be set accordingly.
    420      1.1  rmind  */
    421  1.6.8.2   yamt int
    422      1.2  rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
    423      1.1  rmind {
    424  1.6.8.5   yamt 	int flags, l4flags;
    425  1.6.8.5   yamt 	u_int hlen;
    426      1.1  rmind 
    427  1.6.8.5   yamt 	/*
    428  1.6.8.5   yamt 	 * This routine is a main point where the references are cached,
    429  1.6.8.5   yamt 	 * therefore clear the flag as we reset.
    430  1.6.8.5   yamt 	 */
    431  1.6.8.5   yamt again:
    432  1.6.8.5   yamt 	nbuf_unset_flag(nbuf, NBUF_DATAREF_RESET);
    433  1.6.8.5   yamt 
    434  1.6.8.5   yamt 	/*
    435  1.6.8.5   yamt 	 * First, cache the L3 header (IPv4 or IPv6).  If IP packet is
    436  1.6.8.5   yamt 	 * fragmented, then we cannot look into L4.
    437  1.6.8.5   yamt 	 */
    438  1.6.8.5   yamt 	flags = npf_cache_ip(npc, nbuf);
    439  1.6.8.5   yamt 	if ((flags & NPC_IP46) == 0 || (flags & NPC_IPFRAG) != 0) {
    440  1.6.8.5   yamt 		npc->npc_info |= flags;
    441  1.6.8.5   yamt 		return flags;
    442      1.1  rmind 	}
    443  1.6.8.5   yamt 	hlen = npc->npc_hlen;
    444  1.6.8.5   yamt 
    445  1.6.8.5   yamt 	switch (npc->npc_proto) {
    446      1.1  rmind 	case IPPROTO_TCP:
    447  1.6.8.5   yamt 		/* Cache: layer 4 - TCP. */
    448  1.6.8.5   yamt 		npc->npc_l4.tcp = nbuf_advance(nbuf, hlen,
    449  1.6.8.5   yamt 		    sizeof(struct tcphdr));
    450  1.6.8.5   yamt 		l4flags = NPC_LAYER4 | NPC_TCP;
    451  1.6.8.2   yamt 		break;
    452      1.1  rmind 	case IPPROTO_UDP:
    453  1.6.8.5   yamt 		/* Cache: layer 4 - UDP. */
    454  1.6.8.5   yamt 		npc->npc_l4.udp = nbuf_advance(nbuf, hlen,
    455  1.6.8.5   yamt 		    sizeof(struct udphdr));
    456  1.6.8.5   yamt 		l4flags = NPC_LAYER4 | NPC_UDP;
    457  1.6.8.2   yamt 		break;
    458      1.1  rmind 	case IPPROTO_ICMP:
    459  1.6.8.5   yamt 		/* Cache: layer 4 - ICMPv4. */
    460  1.6.8.5   yamt 		npc->npc_l4.icmp = nbuf_advance(nbuf, hlen,
    461  1.6.8.5   yamt 		    offsetof(struct icmp, icmp_void));
    462  1.6.8.5   yamt 		l4flags = NPC_LAYER4 | NPC_ICMP;
    463  1.6.8.5   yamt 		break;
    464  1.6.8.3   yamt 	case IPPROTO_ICMPV6:
    465  1.6.8.5   yamt 		/* Cache: layer 4 - ICMPv6. */
    466  1.6.8.5   yamt 		npc->npc_l4.icmp6 = nbuf_advance(nbuf, hlen,
    467  1.6.8.5   yamt 		    offsetof(struct icmp6_hdr, icmp6_data32));
    468  1.6.8.5   yamt 		l4flags = NPC_LAYER4 | NPC_ICMP;
    469  1.6.8.5   yamt 		break;
    470  1.6.8.5   yamt 	default:
    471  1.6.8.5   yamt 		l4flags = 0;
    472  1.6.8.2   yamt 		break;
    473      1.1  rmind 	}
    474  1.6.8.5   yamt 
    475  1.6.8.5   yamt 	if (nbuf_flag_p(nbuf, NBUF_DATAREF_RESET)) {
    476  1.6.8.5   yamt 		goto again;
    477  1.6.8.5   yamt 	}
    478  1.6.8.5   yamt 
    479  1.6.8.5   yamt 	/* Add the L4 flags if nbuf_advance() succeeded. */
    480  1.6.8.5   yamt 	if (l4flags && npc->npc_l4.hdr) {
    481  1.6.8.5   yamt 		flags |= l4flags;
    482  1.6.8.5   yamt 	}
    483  1.6.8.5   yamt 	npc->npc_info |= flags;
    484  1.6.8.5   yamt 	return flags;
    485  1.6.8.5   yamt }
    486  1.6.8.5   yamt 
    487  1.6.8.5   yamt void
    488  1.6.8.5   yamt npf_recache(npf_cache_t *npc, nbuf_t *nbuf)
    489  1.6.8.5   yamt {
    490  1.6.8.5   yamt 	const int mflags __unused = npc->npc_info & (NPC_IP46 | NPC_LAYER4);
    491  1.6.8.5   yamt 	int flags;
    492  1.6.8.5   yamt 
    493  1.6.8.5   yamt 	nbuf_reset(nbuf);
    494  1.6.8.5   yamt 	npc->npc_info = 0;
    495  1.6.8.5   yamt 	flags = npf_cache_all(npc, nbuf);
    496  1.6.8.5   yamt 	KASSERT((flags & mflags) == mflags);
    497  1.6.8.5   yamt 	KASSERT(nbuf_flag_p(nbuf, NBUF_DATAREF_RESET) == 0);
    498      1.1  rmind }
    499      1.1  rmind 
    500      1.1  rmind /*
    501  1.6.8.5   yamt  * npf_rwrip: rewrite required IP address.
    502      1.4  rmind  */
    503      1.4  rmind bool
    504  1.6.8.5   yamt npf_rwrip(const npf_cache_t *npc, int di, const npf_addr_t *addr)
    505      1.4  rmind {
    506      1.4  rmind 	npf_addr_t *oaddr;
    507      1.4  rmind 
    508      1.4  rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    509      1.4  rmind 
    510  1.6.8.5   yamt 	/*
    511  1.6.8.5   yamt 	 * Rewrite source address if outgoing and destination if incoming.
    512  1.6.8.5   yamt 	 */
    513  1.6.8.5   yamt 	oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
    514  1.6.8.3   yamt 	memcpy(oaddr, addr, npc->npc_alen);
    515      1.4  rmind 	return true;
    516      1.4  rmind }
    517      1.4  rmind 
    518      1.4  rmind /*
    519  1.6.8.5   yamt  * npf_rwrport: rewrite required TCP/UDP port.
    520      1.1  rmind  */
    521      1.1  rmind bool
    522  1.6.8.5   yamt npf_rwrport(const npf_cache_t *npc, int di, const in_port_t port)
    523      1.1  rmind {
    524      1.4  rmind 	const int proto = npf_cache_ipproto(npc);
    525      1.4  rmind 	in_port_t *oport;
    526      1.1  rmind 
    527      1.4  rmind 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    528      1.1  rmind 	KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
    529      1.1  rmind 
    530  1.6.8.5   yamt 	/* Get the offset and store the port in it. */
    531      1.4  rmind 	if (proto == IPPROTO_TCP) {
    532  1.6.8.5   yamt 		struct tcphdr *th = npc->npc_l4.tcp;
    533  1.6.8.5   yamt 		oport = (di == PFIL_OUT) ? &th->th_sport : &th->th_dport;
    534      1.1  rmind 	} else {
    535  1.6.8.5   yamt 		struct udphdr *uh = npc->npc_l4.udp;
    536  1.6.8.5   yamt 		oport = (di == PFIL_OUT) ? &uh->uh_sport : &uh->uh_dport;
    537      1.1  rmind 	}
    538  1.6.8.5   yamt 	memcpy(oport, &port, sizeof(in_port_t));
    539      1.1  rmind 	return true;
    540      1.1  rmind }
    541      1.1  rmind 
    542      1.1  rmind /*
    543  1.6.8.5   yamt  * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum.
    544      1.1  rmind  */
    545      1.1  rmind bool
    546  1.6.8.5   yamt npf_rwrcksum(const npf_cache_t *npc, const int di,
    547  1.6.8.5   yamt     const npf_addr_t *addr, const in_port_t port)
    548      1.1  rmind {
    549      1.4  rmind 	const int proto = npf_cache_ipproto(npc);
    550  1.6.8.5   yamt 	const int alen = npc->npc_alen;
    551      1.4  rmind 	npf_addr_t *oaddr;
    552  1.6.8.4   yamt 	uint16_t *ocksum;
    553  1.6.8.4   yamt 	in_port_t oport;
    554  1.6.8.4   yamt 
    555  1.6.8.5   yamt 	KASSERT(npf_iscached(npc, NPC_LAYER4));
    556  1.6.8.4   yamt 	oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
    557  1.6.8.4   yamt 
    558      1.4  rmind 	if (npf_iscached(npc, NPC_IP4)) {
    559  1.6.8.5   yamt 		struct ip *ip = npc->npc_ip.v4;
    560  1.6.8.5   yamt 		uint16_t ipsum = ip->ip_sum;
    561      1.4  rmind 
    562  1.6.8.5   yamt 		/* Recalculate IPv4 checksum and rewrite. */
    563  1.6.8.5   yamt 		ip->ip_sum = npf_addr_cksum(ipsum, alen, oaddr, addr);
    564      1.4  rmind 	} else {
    565      1.4  rmind 		/* No checksum for IPv6. */
    566      1.4  rmind 		KASSERT(npf_iscached(npc, NPC_IP6));
    567      1.4  rmind 	}
    568      1.4  rmind 
    569  1.6.8.4   yamt 	/* Nothing else to do for ICMP. */
    570  1.6.8.4   yamt 	if (proto == IPPROTO_ICMP) {
    571      1.4  rmind 		return true;
    572      1.4  rmind 	}
    573  1.6.8.1   yamt 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    574      1.4  rmind 
    575  1.6.8.4   yamt 	/*
    576  1.6.8.4   yamt 	 * Calculate TCP/UDP checksum:
    577  1.6.8.4   yamt 	 * - Skip if UDP and the current checksum is zero.
    578  1.6.8.4   yamt 	 * - Fixup the IP address change.
    579  1.6.8.4   yamt 	 * - Fixup the port change, if required (non-zero).
    580  1.6.8.4   yamt 	 */
    581      1.4  rmind 	if (proto == IPPROTO_TCP) {
    582  1.6.8.5   yamt 		struct tcphdr *th = npc->npc_l4.tcp;
    583      1.4  rmind 
    584  1.6.8.4   yamt 		ocksum = &th->th_sum;
    585  1.6.8.4   yamt 		oport = (di == PFIL_OUT) ? th->th_sport : th->th_dport;
    586      1.4  rmind 	} else {
    587  1.6.8.5   yamt 		struct udphdr *uh = npc->npc_l4.udp;
    588      1.4  rmind 
    589      1.4  rmind 		KASSERT(proto == IPPROTO_UDP);
    590  1.6.8.4   yamt 		ocksum = &uh->uh_sum;
    591  1.6.8.4   yamt 		if (*ocksum == 0) {
    592      1.4  rmind 			/* No need to update. */
    593      1.4  rmind 			return true;
    594      1.4  rmind 		}
    595  1.6.8.4   yamt 		oport = (di == PFIL_OUT) ? uh->uh_sport : uh->uh_dport;
    596  1.6.8.4   yamt 	}
    597  1.6.8.4   yamt 
    598  1.6.8.5   yamt 	uint16_t cksum = npf_addr_cksum(*ocksum, alen, oaddr, addr);
    599  1.6.8.4   yamt 	if (port) {
    600  1.6.8.4   yamt 		cksum = npf_fixup16_cksum(cksum, oport, port);
    601      1.4  rmind 	}
    602      1.1  rmind 
    603  1.6.8.5   yamt 	/* Rewrite TCP/UDP checksum. */
    604  1.6.8.5   yamt 	memcpy(ocksum, &cksum, sizeof(uint16_t));
    605      1.4  rmind 	return true;
    606      1.4  rmind }
    607      1.4  rmind 
    608  1.6.8.3   yamt #if defined(DDB) || defined(_NPF_TESTING)
    609      1.4  rmind 
    610  1.6.8.3   yamt void
    611  1.6.8.3   yamt npf_addr_dump(const npf_addr_t *addr)
    612      1.4  rmind {
    613  1.6.8.3   yamt 	printf("IP[%x:%x:%x:%x]\n",
    614  1.6.8.3   yamt 	    addr->s6_addr32[0], addr->s6_addr32[1],
    615  1.6.8.3   yamt 	    addr->s6_addr32[2], addr->s6_addr32[3]);
    616      1.1  rmind }
    617  1.6.8.3   yamt 
    618  1.6.8.3   yamt #endif
    619