Home | History | Annotate | Line # | Download | only in npf
npf_inet.c revision 1.16.2.1
      1  1.16.2.1       tls /*	$NetBSD: npf_inet.c,v 1.16.2.1 2012/11/20 03:02:47 tls Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4      1.12     rmind  * 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.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.1     rmind #include <sys/cdefs.h>
     42  1.16.2.1       tls __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.16.2.1 2012/11/20 03:02:47 tls Exp $");
     43       1.1     rmind 
     44       1.1     rmind #include <sys/param.h>
     45      1.11     rmind #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.4     rmind npf_addr_cksum(uint16_t cksum, int sz, npf_addr_t *oaddr, npf_addr_t *naddr)
     97       1.4     rmind {
     98       1.4     rmind 	uint32_t *oip32 = (uint32_t *)oaddr, *nip32 = (uint32_t *)naddr;
     99       1.4     rmind 
    100       1.4     rmind 	KASSERT(sz % sizeof(uint32_t) == 0);
    101       1.4     rmind 	do {
    102       1.4     rmind 		cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
    103       1.4     rmind 		sz -= sizeof(uint32_t);
    104       1.4     rmind 	} while (sz);
    105       1.4     rmind 
    106       1.4     rmind 	return cksum;
    107       1.4     rmind }
    108       1.4     rmind 
    109       1.4     rmind /*
    110       1.4     rmind  * npf_addr_sum: provide IP address as a summed (if needed) 32-bit integer.
    111       1.4     rmind  * Note: used for hash function.
    112       1.1     rmind  */
    113       1.4     rmind uint32_t
    114       1.4     rmind npf_addr_sum(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
    115       1.1     rmind {
    116       1.4     rmind 	uint32_t mix = 0;
    117       1.4     rmind 	int i;
    118       1.1     rmind 
    119       1.5     rmind 	KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
    120       1.5     rmind 
    121       1.4     rmind 	for (i = 0; i < (sz >> 2); i++) {
    122       1.4     rmind 		mix += a1->s6_addr32[i];
    123       1.4     rmind 		mix += a2->s6_addr32[i];
    124       1.4     rmind 	}
    125       1.4     rmind 	return mix;
    126       1.4     rmind }
    127       1.1     rmind 
    128      1.13     rmind /*
    129      1.13     rmind  * npf_addr_mask: apply the mask to a given address and store the result.
    130      1.13     rmind  */
    131      1.13     rmind void
    132      1.13     rmind npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask,
    133      1.13     rmind     const int alen, npf_addr_t *out)
    134      1.12     rmind {
    135      1.13     rmind 	const int nwords = alen >> 2;
    136      1.12     rmind 	uint_fast8_t length = mask;
    137      1.12     rmind 
    138      1.12     rmind 	/* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
    139      1.12     rmind 	KASSERT(length <= NPF_MAX_NETMASK);
    140      1.12     rmind 
    141      1.13     rmind 	for (int i = 0; i < nwords; i++) {
    142      1.13     rmind 		uint32_t wordmask;
    143      1.13     rmind 
    144      1.12     rmind 		if (length >= 32) {
    145      1.13     rmind 			wordmask = htonl(0xffffffff);
    146      1.12     rmind 			length -= 32;
    147      1.13     rmind 		} else if (length) {
    148      1.13     rmind 			wordmask = htonl(0xffffffff << (32 - length));
    149      1.13     rmind 			length = 0;
    150      1.12     rmind 		} else {
    151      1.13     rmind 			wordmask = 0;
    152      1.12     rmind 		}
    153      1.13     rmind 		out->s6_addr32[i] = addr->s6_addr32[i] & wordmask;
    154      1.12     rmind 	}
    155      1.12     rmind }
    156      1.12     rmind 
    157      1.12     rmind /*
    158      1.12     rmind  * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
    159      1.12     rmind  *
    160      1.13     rmind  * => Return 0 if equal and negative/positive if less/greater accordingly.
    161      1.12     rmind  * => Ignore the mask, if NPF_NO_NETMASK is specified.
    162      1.12     rmind  */
    163      1.12     rmind int
    164      1.12     rmind npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
    165      1.13     rmind     const npf_addr_t *addr2, const npf_netmask_t mask2, const int alen)
    166      1.12     rmind {
    167      1.13     rmind 	npf_addr_t realaddr1, realaddr2;
    168      1.12     rmind 
    169      1.12     rmind 	if (mask1 != NPF_NO_NETMASK) {
    170      1.13     rmind 		npf_addr_mask(addr1, mask1, alen, &realaddr1);
    171      1.13     rmind 		addr1 = &realaddr1;
    172      1.12     rmind 	}
    173      1.12     rmind 	if (mask2 != NPF_NO_NETMASK) {
    174      1.13     rmind 		npf_addr_mask(addr2, mask2, alen, &realaddr2);
    175      1.13     rmind 		addr2 = &realaddr2;
    176      1.12     rmind 	}
    177      1.13     rmind 	return memcmp(addr1, addr2, alen);
    178      1.12     rmind }
    179      1.12     rmind 
    180       1.4     rmind /*
    181       1.4     rmind  * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
    182      1.12     rmind  *
    183      1.12     rmind  * => Returns all values in host byte-order.
    184       1.4     rmind  */
    185       1.4     rmind int
    186      1.12     rmind npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
    187       1.4     rmind {
    188      1.12     rmind 	const struct tcphdr *th = &npc->npc_l4.tcp;
    189       1.8     rmind 	u_int thlen;
    190       1.1     rmind 
    191       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    192       1.1     rmind 
    193       1.4     rmind 	*seq = ntohl(th->th_seq);
    194       1.4     rmind 	*ack = ntohl(th->th_ack);
    195       1.4     rmind 	*win = (uint32_t)ntohs(th->th_win);
    196       1.8     rmind 	thlen = th->th_off << 2;
    197       1.1     rmind 
    198       1.7    zoltan 	if (npf_iscached(npc, NPC_IP4)) {
    199      1.12     rmind 		const struct ip *ip = &npc->npc_ip.v4;
    200      1.10     rmind 		return ntohs(ip->ip_len) - npf_cache_hlen(npc) - thlen;
    201      1.12     rmind 	} else if (npf_iscached(npc, NPC_IP6)) {
    202      1.12     rmind 		const struct ip6_hdr *ip6 = &npc->npc_ip.v6;
    203       1.8     rmind 		return ntohs(ip6->ip6_plen) - thlen;
    204       1.7    zoltan 	}
    205       1.7    zoltan 	return 0;
    206       1.1     rmind }
    207       1.1     rmind 
    208       1.1     rmind /*
    209       1.4     rmind  * npf_fetch_tcpopts: parse and return TCP options.
    210       1.1     rmind  */
    211       1.1     rmind bool
    212       1.4     rmind npf_fetch_tcpopts(const npf_cache_t *npc, nbuf_t *nbuf,
    213       1.4     rmind     uint16_t *mss, int *wscale)
    214       1.1     rmind {
    215       1.4     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    216       1.4     rmind 	const struct tcphdr *th = &npc->npc_l4.tcp;
    217       1.4     rmind 	int topts_len, step;
    218       1.4     rmind 	uint16_t val16;
    219       1.4     rmind 	uint8_t val;
    220       1.4     rmind 
    221       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
    222       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    223      1.10     rmind 
    224       1.4     rmind 	/* Determine if there are any TCP options, get their length. */
    225       1.4     rmind 	topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
    226       1.4     rmind 	if (topts_len <= 0) {
    227       1.4     rmind 		/* No options. */
    228       1.1     rmind 		return false;
    229       1.4     rmind 	}
    230       1.4     rmind 	KASSERT(topts_len <= MAX_TCPOPTLEN);
    231       1.1     rmind 
    232       1.4     rmind 	/* First step: IP and TCP header up to options. */
    233      1.10     rmind 	step = npf_cache_hlen(npc) + sizeof(struct tcphdr);
    234       1.4     rmind next:
    235       1.4     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, step, sizeof(val), &val)) {
    236       1.1     rmind 		return false;
    237       1.4     rmind 	}
    238      1.12     rmind 
    239       1.4     rmind 	switch (val) {
    240       1.4     rmind 	case TCPOPT_EOL:
    241       1.4     rmind 		/* Done. */
    242       1.4     rmind 		return true;
    243       1.4     rmind 	case TCPOPT_NOP:
    244       1.4     rmind 		topts_len--;
    245       1.4     rmind 		step = 1;
    246       1.4     rmind 		break;
    247       1.4     rmind 	case TCPOPT_MAXSEG:
    248       1.4     rmind 		/*
    249       1.4     rmind 		 * XXX: clean this mess.
    250       1.4     rmind 		 */
    251       1.4     rmind 		if (mss && *mss) {
    252       1.4     rmind 			val16 = *mss;
    253       1.4     rmind 			if (nbuf_advstore(&nbuf, &n_ptr, 2,
    254       1.4     rmind 			    sizeof(val16), &val16))
    255       1.4     rmind 				return false;
    256       1.4     rmind 		} else if (nbuf_advfetch(&nbuf, &n_ptr, 2,
    257       1.4     rmind 		    sizeof(val16), &val16)) {
    258       1.4     rmind 			return false;
    259       1.4     rmind 		}
    260       1.4     rmind 		if (mss) {
    261       1.4     rmind 			*mss = val16;
    262       1.4     rmind 		}
    263       1.4     rmind 		topts_len -= TCPOLEN_MAXSEG;
    264       1.4     rmind 		step = sizeof(val16);
    265       1.4     rmind 		break;
    266       1.4     rmind 	case TCPOPT_WINDOW:
    267      1.10     rmind 		/* TCP Window Scaling (RFC 1323). */
    268       1.4     rmind 		if (nbuf_advfetch(&nbuf, &n_ptr, 2, sizeof(val), &val)) {
    269       1.4     rmind 			return false;
    270       1.4     rmind 		}
    271       1.4     rmind 		*wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
    272       1.4     rmind 		topts_len -= TCPOLEN_WINDOW;
    273       1.4     rmind 		step = sizeof(val);
    274       1.4     rmind 		break;
    275       1.4     rmind 	default:
    276       1.4     rmind 		if (nbuf_advfetch(&nbuf, &n_ptr, 1, sizeof(val), &val)) {
    277       1.4     rmind 			return false;
    278       1.4     rmind 		}
    279      1.16     rmind 		if (val < 2 || val > topts_len) {
    280       1.4     rmind 			return false;
    281       1.4     rmind 		}
    282       1.4     rmind 		topts_len -= val;
    283       1.4     rmind 		step = val - 1;
    284       1.4     rmind 	}
    285      1.12     rmind 
    286       1.6     rmind 	/* Any options left? */
    287       1.4     rmind 	if (__predict_true(topts_len > 0)) {
    288       1.4     rmind 		goto next;
    289       1.4     rmind 	}
    290       1.6     rmind 	return true;
    291       1.1     rmind }
    292       1.1     rmind 
    293       1.1     rmind /*
    294       1.4     rmind  * npf_fetch_ip: fetch, check and cache IP header.
    295       1.1     rmind  */
    296       1.1     rmind bool
    297       1.4     rmind npf_fetch_ip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    298       1.1     rmind {
    299       1.4     rmind 	uint8_t ver;
    300       1.1     rmind 
    301       1.4     rmind 	if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &ver)) {
    302       1.1     rmind 		return false;
    303       1.4     rmind 	}
    304      1.12     rmind 
    305       1.4     rmind 	switch (ver >> 4) {
    306      1.12     rmind 	case IPVERSION: {
    307      1.12     rmind 		struct ip *ip = &npc->npc_ip.v4;
    308      1.12     rmind 
    309      1.12     rmind 		/* Fetch IPv4 header. */
    310       1.4     rmind 		if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(struct ip), ip)) {
    311       1.4     rmind 			return false;
    312       1.4     rmind 		}
    313      1.12     rmind 
    314       1.4     rmind 		/* Check header length and fragment offset. */
    315      1.10     rmind 		if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
    316       1.4     rmind 			return false;
    317       1.4     rmind 		}
    318       1.4     rmind 		if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
    319       1.4     rmind 			/* Note fragmentation. */
    320       1.4     rmind 			npc->npc_info |= NPC_IPFRAG;
    321       1.4     rmind 		}
    322      1.12     rmind 
    323       1.4     rmind 		/* Cache: layer 3 - IPv4. */
    324      1.14     rmind 		npc->npc_alen = sizeof(struct in_addr);
    325       1.4     rmind 		npc->npc_srcip = (npf_addr_t *)&ip->ip_src;
    326       1.4     rmind 		npc->npc_dstip = (npf_addr_t *)&ip->ip_dst;
    327       1.4     rmind 		npc->npc_info |= NPC_IP4;
    328       1.7    zoltan 		npc->npc_hlen = ip->ip_hl << 2;
    329       1.7    zoltan 		npc->npc_next_proto = npc->npc_ip.v4.ip_p;
    330       1.4     rmind 		break;
    331      1.12     rmind 	}
    332       1.4     rmind 
    333      1.12     rmind 	case (IPV6_VERSION >> 4): {
    334      1.12     rmind 		struct ip6_hdr *ip6 = &npc->npc_ip.v6;
    335      1.13     rmind 		size_t hlen = sizeof(struct ip6_hdr);
    336      1.13     rmind 		struct ip6_ext ip6e;
    337      1.12     rmind 
    338      1.13     rmind 		/* Fetch IPv6 header and set initial next-protocol value. */
    339      1.13     rmind 		if (nbuf_fetch_datum(nbuf, n_ptr, hlen, ip6)) {
    340       1.7    zoltan 			return false;
    341       1.7    zoltan 		}
    342      1.12     rmind 		npc->npc_next_proto = ip6->ip6_nxt;
    343      1.13     rmind 		npc->npc_hlen = hlen;
    344       1.7    zoltan 
    345      1.12     rmind 		/*
    346      1.13     rmind 		 * Advance by the length of the current header and
    347      1.13     rmind 		 * prefetch the extension header.
    348      1.12     rmind 		 */
    349      1.13     rmind 		while (nbuf_advfetch(&nbuf, &n_ptr, hlen,
    350      1.13     rmind 		    sizeof(struct ip6_ext), &ip6e) == 0) {
    351      1.13     rmind 			/*
    352      1.13     rmind 			 * Determine whether we are going to continue.
    353      1.13     rmind 			 */
    354       1.7    zoltan 			switch (npc->npc_next_proto) {
    355      1.13     rmind 			case IPPROTO_HOPOPTS:
    356       1.7    zoltan 			case IPPROTO_DSTOPTS:
    357       1.7    zoltan 			case IPPROTO_ROUTING:
    358      1.13     rmind 				hlen = (ip6e.ip6e_len + 1) << 3;
    359       1.7    zoltan 				break;
    360       1.7    zoltan 			case IPPROTO_FRAGMENT:
    361       1.7    zoltan 				npc->npc_info |= NPC_IPFRAG;
    362      1.13     rmind 				hlen = sizeof(struct ip6_frag);
    363       1.7    zoltan 				break;
    364       1.7    zoltan 			case IPPROTO_AH:
    365      1.13     rmind 				hlen = (ip6e.ip6e_len + 2) << 2;
    366       1.7    zoltan 				break;
    367       1.7    zoltan 			default:
    368      1.13     rmind 				hlen = 0;
    369      1.13     rmind 				break;
    370      1.13     rmind 			}
    371      1.13     rmind 
    372      1.13     rmind 			if (!hlen) {
    373       1.7    zoltan 				break;
    374       1.7    zoltan 			}
    375      1.12     rmind 			npc->npc_next_proto = ip6e.ip6e_nxt;
    376      1.13     rmind 			npc->npc_hlen += hlen;
    377      1.13     rmind 		}
    378       1.7    zoltan 
    379      1.12     rmind 		/* Cache: layer 3 - IPv6. */
    380      1.14     rmind 		npc->npc_alen = sizeof(struct in6_addr);
    381       1.7    zoltan 		npc->npc_srcip = (npf_addr_t *)&ip6->ip6_src;
    382       1.7    zoltan 		npc->npc_dstip = (npf_addr_t *)&ip6->ip6_dst;
    383       1.7    zoltan 		npc->npc_info |= NPC_IP6;
    384       1.7    zoltan 		break;
    385      1.12     rmind 	}
    386       1.4     rmind 	default:
    387       1.1     rmind 		return false;
    388       1.4     rmind 	}
    389      1.12     rmind 
    390       1.4     rmind 	return true;
    391       1.4     rmind }
    392       1.1     rmind 
    393      1.12     rmind /*
    394      1.12     rmind  * npf_fetch_tcp: fetch, check and cache TCP header.  If necessary,
    395      1.12     rmind  * fetch and cache layer 3 as well.
    396      1.12     rmind  */
    397       1.4     rmind bool
    398       1.4     rmind npf_fetch_tcp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    399       1.4     rmind {
    400       1.4     rmind 	struct tcphdr *th;
    401       1.1     rmind 
    402       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    403       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    404       1.1     rmind 		return false;
    405       1.4     rmind 	}
    406       1.7    zoltan 	if (npf_cache_ipproto(npc) != IPPROTO_TCP) {
    407       1.1     rmind 		return false;
    408       1.4     rmind 	}
    409       1.4     rmind 	th = &npc->npc_l4.tcp;
    410       1.4     rmind 
    411       1.4     rmind 	/* Fetch TCP header. */
    412      1.10     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, npf_cache_hlen(npc),
    413       1.8     rmind 	    sizeof(struct tcphdr), th)) {
    414       1.1     rmind 		return false;
    415       1.4     rmind 	}
    416       1.1     rmind 
    417       1.4     rmind 	/* Cache: layer 4 - TCP. */
    418       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_TCP);
    419       1.1     rmind 	return true;
    420       1.1     rmind }
    421       1.1     rmind 
    422      1.12     rmind /*
    423      1.12     rmind  * npf_fetch_udp: fetch, check and cache UDP header.  If necessary,
    424      1.12     rmind  * fetch and cache layer 3 as well.
    425      1.12     rmind  */
    426       1.1     rmind bool
    427       1.4     rmind npf_fetch_udp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    428       1.1     rmind {
    429       1.4     rmind 	struct udphdr *uh;
    430      1.10     rmind 	u_int hlen;
    431       1.1     rmind 
    432       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    433       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    434       1.4     rmind 		return false;
    435       1.4     rmind 	}
    436      1.14     rmind 	if (npf_cache_ipproto(npc) != IPPROTO_UDP) {
    437       1.1     rmind 		return false;
    438       1.4     rmind 	}
    439       1.4     rmind 	uh = &npc->npc_l4.udp;
    440      1.10     rmind 	hlen = npf_cache_hlen(npc);
    441       1.1     rmind 
    442      1.12     rmind 	/* Fetch UDP header. */
    443       1.4     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, hlen, sizeof(struct udphdr), uh)) {
    444       1.1     rmind 		return false;
    445       1.4     rmind 	}
    446       1.1     rmind 
    447       1.9  jakllsch 	/* Cache: layer 4 - UDP. */
    448       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_UDP);
    449       1.1     rmind 	return true;
    450       1.1     rmind }
    451       1.1     rmind 
    452       1.2     rmind /*
    453       1.4     rmind  * npf_fetch_icmp: fetch ICMP code, type and possible query ID.
    454       1.2     rmind  */
    455       1.2     rmind bool
    456       1.4     rmind npf_fetch_icmp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    457       1.1     rmind {
    458       1.4     rmind 	struct icmp *ic;
    459      1.10     rmind 	u_int hlen, iclen;
    460       1.1     rmind 
    461       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    462       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    463       1.4     rmind 		return false;
    464       1.4     rmind 	}
    465      1.15       spz 	if (npf_cache_ipproto(npc) != IPPROTO_ICMP &&
    466      1.15       spz 	    npf_cache_ipproto(npc) != IPPROTO_ICMPV6) {
    467       1.1     rmind 		return false;
    468       1.3     rmind 	}
    469       1.4     rmind 	ic = &npc->npc_l4.icmp;
    470      1.10     rmind 	hlen = npf_cache_hlen(npc);
    471       1.4     rmind 
    472       1.4     rmind 	/* Fetch basic ICMP header, up to the "data" point. */
    473      1.15       spz 	CTASSERT(offsetof(struct icmp, icmp_void) ==
    474      1.15       spz 	         offsetof(struct icmp6_hdr, icmp6_data32));
    475      1.15       spz 
    476      1.15       spz 	iclen = offsetof(struct icmp, icmp_void);
    477       1.6     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, hlen, iclen, ic)) {
    478       1.4     rmind 		return false;
    479       1.4     rmind 	}
    480       1.4     rmind 
    481       1.4     rmind 	/* Cache: layer 4 - ICMP. */
    482       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_ICMP);
    483       1.1     rmind 	return true;
    484       1.1     rmind }
    485       1.1     rmind 
    486       1.1     rmind /*
    487       1.4     rmind  * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
    488      1.12     rmind  * and TCP, UDP or ICMP headers.
    489       1.1     rmind  */
    490      1.10     rmind int
    491       1.2     rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
    492       1.1     rmind {
    493       1.1     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    494       1.1     rmind 
    495       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    496      1.10     rmind 		return npc->npc_info;
    497       1.1     rmind 	}
    498       1.4     rmind 	if (npf_iscached(npc, NPC_IPFRAG)) {
    499      1.10     rmind 		return npc->npc_info;
    500       1.1     rmind 	}
    501       1.4     rmind 	switch (npf_cache_ipproto(npc)) {
    502       1.1     rmind 	case IPPROTO_TCP:
    503      1.10     rmind 		(void)npf_fetch_tcp(npc, nbuf, n_ptr);
    504      1.10     rmind 		break;
    505       1.1     rmind 	case IPPROTO_UDP:
    506      1.10     rmind 		(void)npf_fetch_udp(npc, nbuf, n_ptr);
    507      1.10     rmind 		break;
    508       1.1     rmind 	case IPPROTO_ICMP:
    509      1.15       spz 	case IPPROTO_ICMPV6:
    510      1.10     rmind 		(void)npf_fetch_icmp(npc, nbuf, n_ptr);
    511      1.10     rmind 		break;
    512       1.1     rmind 	}
    513      1.10     rmind 	return npc->npc_info;
    514       1.1     rmind }
    515       1.1     rmind 
    516       1.1     rmind /*
    517       1.4     rmind  * npf_rwrip: rewrite required IP address, update the cache.
    518       1.4     rmind  */
    519       1.4     rmind bool
    520       1.4     rmind npf_rwrip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    521       1.4     rmind     npf_addr_t *addr)
    522       1.4     rmind {
    523       1.4     rmind 	npf_addr_t *oaddr;
    524       1.4     rmind 	u_int offby;
    525       1.4     rmind 
    526       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    527       1.4     rmind 
    528       1.4     rmind 	if (di == PFIL_OUT) {
    529       1.4     rmind 		/* Rewrite source address, if outgoing. */
    530       1.4     rmind 		offby = offsetof(struct ip, ip_src);
    531       1.4     rmind 		oaddr = npc->npc_srcip;
    532       1.4     rmind 	} else {
    533       1.4     rmind 		/* Rewrite destination, if incoming. */
    534       1.4     rmind 		offby = offsetof(struct ip, ip_dst);
    535       1.4     rmind 		oaddr = npc->npc_dstip;
    536       1.4     rmind 	}
    537       1.4     rmind 
    538       1.4     rmind 	/* Advance to the address and rewrite it. */
    539      1.14     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, npc->npc_alen, addr))
    540       1.4     rmind 		return false;
    541       1.4     rmind 
    542       1.4     rmind 	/* Cache: IP address. */
    543      1.14     rmind 	memcpy(oaddr, addr, npc->npc_alen);
    544       1.4     rmind 	return true;
    545       1.4     rmind }
    546       1.4     rmind 
    547       1.4     rmind /*
    548       1.4     rmind  * npf_rwrport: rewrite required TCP/UDP port, update the cache.
    549       1.1     rmind  */
    550       1.1     rmind bool
    551       1.1     rmind npf_rwrport(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    552       1.4     rmind     in_port_t port)
    553       1.1     rmind {
    554       1.4     rmind 	const int proto = npf_cache_ipproto(npc);
    555      1.10     rmind 	u_int offby = npf_cache_hlen(npc);
    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.1     rmind 
    561       1.4     rmind 	/* Offset to the port and pointer in the cache. */
    562       1.4     rmind 	if (proto == IPPROTO_TCP) {
    563       1.4     rmind 		struct tcphdr *th = &npc->npc_l4.tcp;
    564       1.4     rmind 		if (di == PFIL_OUT) {
    565       1.4     rmind 			CTASSERT(offsetof(struct tcphdr, th_sport) == 0);
    566       1.4     rmind 			oport = &th->th_sport;
    567       1.1     rmind 		} else {
    568       1.4     rmind 			offby += offsetof(struct tcphdr, th_dport);
    569       1.4     rmind 			oport = &th->th_dport;
    570       1.1     rmind 		}
    571       1.1     rmind 	} else {
    572       1.4     rmind 		struct udphdr *uh = &npc->npc_l4.udp;
    573       1.4     rmind 		if (di == PFIL_OUT) {
    574       1.4     rmind 			CTASSERT(offsetof(struct udphdr, uh_sport) == 0);
    575       1.4     rmind 			oport = &uh->uh_sport;
    576       1.1     rmind 		} else {
    577       1.4     rmind 			offby += offsetof(struct udphdr, uh_dport);
    578       1.4     rmind 			oport = &uh->uh_dport;
    579       1.1     rmind 		}
    580       1.1     rmind 	}
    581       1.1     rmind 
    582       1.4     rmind 	/* Advance and rewrite the port. */
    583       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(in_port_t), &port))
    584       1.1     rmind 		return false;
    585       1.1     rmind 
    586       1.4     rmind 	/* Cache: TCP/UDP port. */
    587       1.4     rmind 	*oport = port;
    588       1.1     rmind 	return true;
    589       1.1     rmind }
    590       1.1     rmind 
    591       1.1     rmind /*
    592       1.6     rmind  * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum, update the cache.
    593       1.1     rmind  */
    594       1.1     rmind bool
    595       1.4     rmind npf_rwrcksum(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    596       1.4     rmind     npf_addr_t *addr, in_port_t port)
    597       1.1     rmind {
    598       1.4     rmind 	const int proto = npf_cache_ipproto(npc);
    599       1.4     rmind 	npf_addr_t *oaddr;
    600       1.4     rmind 	in_port_t *oport;
    601       1.4     rmind 	uint16_t *cksum;
    602       1.1     rmind 	u_int offby;
    603       1.1     rmind 
    604       1.4     rmind 	/* Checksum update for IPv4 header. */
    605       1.4     rmind 	if (npf_iscached(npc, NPC_IP4)) {
    606       1.4     rmind 		struct ip *ip = &npc->npc_ip.v4;
    607       1.4     rmind 		uint16_t ipsum;
    608       1.4     rmind 
    609       1.4     rmind 		oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
    610      1.14     rmind 		ipsum = npf_addr_cksum(ip->ip_sum, npc->npc_alen, oaddr, addr);
    611       1.4     rmind 
    612       1.4     rmind 		/* Advance to the IPv4 checksum and rewrite it. */
    613       1.4     rmind 		offby = offsetof(struct ip, ip_sum);
    614       1.4     rmind 		if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(ipsum), &ipsum))
    615       1.4     rmind 			return false;
    616       1.4     rmind 
    617       1.4     rmind 		ip->ip_sum = ipsum;
    618      1.10     rmind 		offby = npf_cache_hlen(npc) - offby;
    619       1.4     rmind 	} else {
    620       1.4     rmind 		/* No checksum for IPv6. */
    621       1.4     rmind 		KASSERT(npf_iscached(npc, NPC_IP6));
    622       1.4     rmind 		oaddr = NULL;
    623       1.4     rmind 		offby = 0;
    624       1.6     rmind 		return false;	/* XXX: Not yet supported. */
    625       1.4     rmind 	}
    626       1.4     rmind 
    627       1.4     rmind 	/* Determine whether TCP/UDP checksum update is needed. */
    628       1.6     rmind 	if (proto == IPPROTO_ICMP || port == 0) {
    629       1.4     rmind 		return true;
    630       1.4     rmind 	}
    631       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    632       1.4     rmind 
    633       1.4     rmind 	/* Calculate TCP/UDP checksum. */
    634       1.4     rmind 	if (proto == IPPROTO_TCP) {
    635       1.4     rmind 		struct tcphdr *th = &npc->npc_l4.tcp;
    636       1.4     rmind 
    637       1.4     rmind 		cksum = &th->th_sum;
    638       1.4     rmind 		offby += offsetof(struct tcphdr, th_sum);
    639       1.4     rmind 		oport = (di == PFIL_OUT) ? &th->th_sport : &th->th_dport;
    640       1.4     rmind 	} else {
    641       1.4     rmind 		struct udphdr *uh = &npc->npc_l4.udp;
    642       1.4     rmind 
    643       1.4     rmind 		KASSERT(proto == IPPROTO_UDP);
    644       1.4     rmind 		cksum = &uh->uh_sum;
    645       1.4     rmind 		if (*cksum == 0) {
    646       1.4     rmind 			/* No need to update. */
    647       1.4     rmind 			return true;
    648       1.4     rmind 		}
    649       1.4     rmind 		offby += offsetof(struct udphdr, uh_sum);
    650       1.4     rmind 		oport = (di == PFIL_OUT) ? &uh->uh_sport : &uh->uh_dport;
    651       1.4     rmind 	}
    652      1.14     rmind 	*cksum = npf_addr_cksum(*cksum, npc->npc_alen, oaddr, addr);
    653       1.4     rmind 	*cksum = npf_fixup16_cksum(*cksum, *oport, port);
    654       1.1     rmind 
    655       1.4     rmind 	/* Advance to TCP/UDP checksum and rewrite it. */
    656       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(uint16_t), cksum)) {
    657       1.1     rmind 		return false;
    658       1.4     rmind 	}
    659       1.4     rmind 	return true;
    660       1.4     rmind }
    661       1.4     rmind 
    662      1.13     rmind #if defined(DDB) || defined(_NPF_TESTING)
    663      1.13     rmind 
    664      1.13     rmind void
    665      1.13     rmind npf_addr_dump(const npf_addr_t *addr)
    666      1.13     rmind {
    667      1.13     rmind 	printf("IP[%x:%x:%x:%x]\n",
    668      1.13     rmind 	    addr->s6_addr32[0], addr->s6_addr32[1],
    669      1.13     rmind 	    addr->s6_addr32[2], addr->s6_addr32[3]);
    670      1.13     rmind }
    671      1.13     rmind 
    672      1.13     rmind #endif
    673