Home | History | Annotate | Line # | Download | only in npf
npf_inet.c revision 1.10.4.2
      1  1.10.4.2       riz /*	$NetBSD: npf_inet.c,v 1.10.4.2 2012/06/26 14:49:10 riz Exp $	*/
      2       1.1     rmind 
      3       1.1     rmind /*-
      4  1.10.4.2       riz  * 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.10.4.2       riz  *
     35  1.10.4.2       riz  * This layer manipulates npf_cache_t structure i.e. caches requested headers
     36  1.10.4.2       riz  * and stores which information was cached in the information bit field.
     37  1.10.4.2       riz  * It is also responsibility of this layer to update or invalidate the cache
     38  1.10.4.2       riz  * 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.10.4.2       riz __KERNEL_RCSID(0, "$NetBSD: npf_inet.c,v 1.10.4.2 2012/06/26 14:49:10 riz Exp $");
     43       1.1     rmind 
     44       1.1     rmind #include <sys/param.h>
     45  1.10.4.1       riz #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.4     rmind #include <netinet/in_var.h>
     55       1.1     rmind #include <netinet/ip.h>
     56       1.4     rmind #include <netinet/ip6.h>
     57       1.1     rmind #include <netinet/tcp.h>
     58       1.1     rmind #include <netinet/udp.h>
     59       1.1     rmind #include <netinet/ip_icmp.h>
     60       1.1     rmind 
     61       1.1     rmind #include "npf_impl.h"
     62       1.1     rmind 
     63       1.1     rmind /*
     64       1.1     rmind  * npf_fixup{16,32}_cksum: update IPv4 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.1     rmind 	 */
     76       1.1     rmind 	sum = ~ntohs(cksum) & 0xffff;
     77       1.1     rmind 	sum += (~ntohs(odatum) & 0xffff) + ntohs(ndatum);
     78       1.1     rmind 	sum = (sum >> 16) + (sum & 0xffff);
     79       1.1     rmind 	sum += (sum >> 16);
     80       1.1     rmind 
     81       1.1     rmind 	return htons(~sum & 0xffff);
     82       1.1     rmind }
     83       1.1     rmind 
     84       1.1     rmind uint16_t
     85       1.1     rmind npf_fixup32_cksum(uint16_t cksum, uint32_t odatum, uint32_t ndatum)
     86       1.1     rmind {
     87       1.1     rmind 
     88       1.1     rmind 	cksum = npf_fixup16_cksum(cksum, odatum & 0xffff, ndatum & 0xffff);
     89       1.1     rmind 	cksum = npf_fixup16_cksum(cksum, odatum >> 16, ndatum >> 16);
     90       1.1     rmind 	return cksum;
     91       1.1     rmind }
     92       1.1     rmind 
     93       1.1     rmind /*
     94       1.4     rmind  * npf_addr_cksum: calculate checksum of the address, either IPv4 or IPv6.
     95       1.4     rmind  */
     96       1.4     rmind uint16_t
     97       1.4     rmind npf_addr_cksum(uint16_t cksum, int sz, npf_addr_t *oaddr, npf_addr_t *naddr)
     98       1.4     rmind {
     99       1.4     rmind 	uint32_t *oip32 = (uint32_t *)oaddr, *nip32 = (uint32_t *)naddr;
    100       1.4     rmind 
    101       1.4     rmind 	KASSERT(sz % sizeof(uint32_t) == 0);
    102       1.4     rmind 	do {
    103       1.4     rmind 		cksum = npf_fixup32_cksum(cksum, *oip32++, *nip32++);
    104       1.4     rmind 		sz -= sizeof(uint32_t);
    105       1.4     rmind 	} while (sz);
    106       1.4     rmind 
    107       1.4     rmind 	return cksum;
    108       1.4     rmind }
    109       1.4     rmind 
    110       1.4     rmind /*
    111       1.4     rmind  * npf_addr_sum: provide IP address as a summed (if needed) 32-bit integer.
    112       1.4     rmind  * Note: used for hash function.
    113       1.1     rmind  */
    114       1.4     rmind uint32_t
    115       1.4     rmind npf_addr_sum(const int sz, const npf_addr_t *a1, const npf_addr_t *a2)
    116       1.1     rmind {
    117       1.4     rmind 	uint32_t mix = 0;
    118       1.4     rmind 	int i;
    119       1.1     rmind 
    120       1.5     rmind 	KASSERT(sz > 0 && a1 != NULL && a2 != NULL);
    121       1.5     rmind 
    122       1.4     rmind 	for (i = 0; i < (sz >> 2); i++) {
    123       1.4     rmind 		mix += a1->s6_addr32[i];
    124       1.4     rmind 		mix += a2->s6_addr32[i];
    125       1.4     rmind 	}
    126       1.4     rmind 	return mix;
    127       1.4     rmind }
    128       1.1     rmind 
    129  1.10.4.2       riz static inline void
    130  1.10.4.2       riz npf_generate_mask(npf_addr_t *out, const npf_netmask_t mask)
    131  1.10.4.2       riz {
    132  1.10.4.2       riz 	uint_fast8_t length = mask;
    133  1.10.4.2       riz 
    134  1.10.4.2       riz 	/* Note: maximum length is 32 for IPv4 and 128 for IPv6. */
    135  1.10.4.2       riz 	KASSERT(length <= NPF_MAX_NETMASK);
    136  1.10.4.2       riz 
    137  1.10.4.2       riz 	for (int i = 0; i < 4; i++) {
    138  1.10.4.2       riz 		if (length >= 32) {
    139  1.10.4.2       riz 			out->s6_addr32[i] = htonl(0xffffffff);
    140  1.10.4.2       riz 			length -= 32;
    141  1.10.4.2       riz 		} else {
    142  1.10.4.2       riz 			out->s6_addr32[i] = htonl(0xffffffff << (32 - length));
    143  1.10.4.2       riz 			length = 0;
    144  1.10.4.2       riz 		}
    145  1.10.4.2       riz 	}
    146  1.10.4.2       riz }
    147  1.10.4.2       riz 
    148  1.10.4.2       riz /*
    149  1.10.4.2       riz  * npf_addr_mask: apply the mask to a given address and store the result.
    150  1.10.4.2       riz  */
    151  1.10.4.2       riz void
    152  1.10.4.2       riz npf_addr_mask(const npf_addr_t *addr, const npf_netmask_t mask, npf_addr_t *out)
    153  1.10.4.2       riz {
    154  1.10.4.2       riz 	npf_addr_t realmask;
    155  1.10.4.2       riz 
    156  1.10.4.2       riz 	npf_generate_mask(&realmask, mask);
    157  1.10.4.2       riz 
    158  1.10.4.2       riz 	for (int i = 0; i < 4; i++) {
    159  1.10.4.2       riz 		out->s6_addr32[i] = addr->s6_addr32[i] & realmask.s6_addr32[i];
    160  1.10.4.2       riz 	}
    161  1.10.4.2       riz }
    162  1.10.4.2       riz 
    163  1.10.4.2       riz /*
    164  1.10.4.2       riz  * npf_addr_cmp: compare two addresses, either IPv4 or IPv6.
    165  1.10.4.2       riz  *
    166  1.10.4.2       riz  * => Ignore the mask, if NPF_NO_NETMASK is specified.
    167  1.10.4.2       riz  * => Return 0 if equal and -1 or 1 if less or greater accordingly.
    168  1.10.4.2       riz  */
    169  1.10.4.2       riz int
    170  1.10.4.2       riz npf_addr_cmp(const npf_addr_t *addr1, const npf_netmask_t mask1,
    171  1.10.4.2       riz     const npf_addr_t *addr2, const npf_netmask_t mask2)
    172  1.10.4.2       riz {
    173  1.10.4.2       riz 	npf_addr_t realmask1, realmask2;
    174  1.10.4.2       riz 
    175  1.10.4.2       riz 	if (mask1 != NPF_NO_NETMASK) {
    176  1.10.4.2       riz 		npf_generate_mask(&realmask1, mask1);
    177  1.10.4.2       riz 	}
    178  1.10.4.2       riz 	if (mask2 != NPF_NO_NETMASK) {
    179  1.10.4.2       riz 		npf_generate_mask(&realmask2, mask2);
    180  1.10.4.2       riz 	}
    181  1.10.4.2       riz 
    182  1.10.4.2       riz 	for (int i = 0; i < 4; i++) {
    183  1.10.4.2       riz 		const uint32_t x = mask1 != NPF_NO_NETMASK ?
    184  1.10.4.2       riz 		    addr1->s6_addr32[i] & realmask1.s6_addr32[i] :
    185  1.10.4.2       riz 		    addr1->s6_addr32[i];
    186  1.10.4.2       riz 		const uint32_t y = mask2 != NPF_NO_NETMASK ?
    187  1.10.4.2       riz 		    addr2->s6_addr32[i] & realmask2.s6_addr32[i] :
    188  1.10.4.2       riz 		    addr2->s6_addr32[i];
    189  1.10.4.2       riz 		if (x < y) {
    190  1.10.4.2       riz 			return -1;
    191  1.10.4.2       riz 		}
    192  1.10.4.2       riz 		if (x > y) {
    193  1.10.4.2       riz 			return 1;
    194  1.10.4.2       riz 		}
    195  1.10.4.2       riz 	}
    196  1.10.4.2       riz 
    197  1.10.4.2       riz 	return 0;
    198  1.10.4.2       riz }
    199  1.10.4.2       riz 
    200       1.4     rmind /*
    201       1.4     rmind  * npf_tcpsaw: helper to fetch SEQ, ACK, WIN and return TCP data length.
    202  1.10.4.2       riz  *
    203  1.10.4.2       riz  * => Returns all values in host byte-order.
    204       1.4     rmind  */
    205       1.4     rmind int
    206  1.10.4.2       riz npf_tcpsaw(const npf_cache_t *npc, tcp_seq *seq, tcp_seq *ack, uint32_t *win)
    207       1.4     rmind {
    208  1.10.4.2       riz 	const struct tcphdr *th = &npc->npc_l4.tcp;
    209       1.8     rmind 	u_int thlen;
    210       1.1     rmind 
    211       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    212       1.1     rmind 
    213       1.4     rmind 	*seq = ntohl(th->th_seq);
    214       1.4     rmind 	*ack = ntohl(th->th_ack);
    215       1.4     rmind 	*win = (uint32_t)ntohs(th->th_win);
    216       1.8     rmind 	thlen = th->th_off << 2;
    217       1.1     rmind 
    218       1.7    zoltan 	if (npf_iscached(npc, NPC_IP4)) {
    219  1.10.4.2       riz 		const struct ip *ip = &npc->npc_ip.v4;
    220      1.10     rmind 		return ntohs(ip->ip_len) - npf_cache_hlen(npc) - thlen;
    221  1.10.4.2       riz 	} else if (npf_iscached(npc, NPC_IP6)) {
    222  1.10.4.2       riz 		const struct ip6_hdr *ip6 = &npc->npc_ip.v6;
    223       1.8     rmind 		return ntohs(ip6->ip6_plen) - thlen;
    224       1.7    zoltan 	}
    225       1.7    zoltan 	return 0;
    226       1.1     rmind }
    227       1.1     rmind 
    228       1.1     rmind /*
    229       1.4     rmind  * npf_fetch_tcpopts: parse and return TCP options.
    230       1.1     rmind  */
    231       1.1     rmind bool
    232       1.4     rmind npf_fetch_tcpopts(const npf_cache_t *npc, nbuf_t *nbuf,
    233       1.4     rmind     uint16_t *mss, int *wscale)
    234       1.1     rmind {
    235       1.4     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    236       1.4     rmind 	const struct tcphdr *th = &npc->npc_l4.tcp;
    237       1.4     rmind 	int topts_len, step;
    238       1.4     rmind 	uint16_t val16;
    239       1.4     rmind 	uint8_t val;
    240       1.4     rmind 
    241       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_IP46));
    242       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP));
    243      1.10     rmind 
    244       1.4     rmind 	/* Determine if there are any TCP options, get their length. */
    245       1.4     rmind 	topts_len = (th->th_off << 2) - sizeof(struct tcphdr);
    246       1.4     rmind 	if (topts_len <= 0) {
    247       1.4     rmind 		/* No options. */
    248       1.1     rmind 		return false;
    249       1.4     rmind 	}
    250       1.4     rmind 	KASSERT(topts_len <= MAX_TCPOPTLEN);
    251       1.1     rmind 
    252       1.4     rmind 	/* First step: IP and TCP header up to options. */
    253      1.10     rmind 	step = npf_cache_hlen(npc) + sizeof(struct tcphdr);
    254       1.4     rmind next:
    255       1.4     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, step, sizeof(val), &val)) {
    256       1.1     rmind 		return false;
    257       1.4     rmind 	}
    258  1.10.4.2       riz 
    259       1.4     rmind 	switch (val) {
    260       1.4     rmind 	case TCPOPT_EOL:
    261       1.4     rmind 		/* Done. */
    262       1.4     rmind 		return true;
    263       1.4     rmind 	case TCPOPT_NOP:
    264       1.4     rmind 		topts_len--;
    265       1.4     rmind 		step = 1;
    266       1.4     rmind 		break;
    267       1.4     rmind 	case TCPOPT_MAXSEG:
    268       1.4     rmind 		/*
    269       1.4     rmind 		 * XXX: clean this mess.
    270       1.4     rmind 		 */
    271       1.4     rmind 		if (mss && *mss) {
    272       1.4     rmind 			val16 = *mss;
    273       1.4     rmind 			if (nbuf_advstore(&nbuf, &n_ptr, 2,
    274       1.4     rmind 			    sizeof(val16), &val16))
    275       1.4     rmind 				return false;
    276       1.4     rmind 		} else if (nbuf_advfetch(&nbuf, &n_ptr, 2,
    277       1.4     rmind 		    sizeof(val16), &val16)) {
    278       1.4     rmind 			return false;
    279       1.4     rmind 		}
    280       1.4     rmind 		if (mss) {
    281       1.4     rmind 			*mss = val16;
    282       1.4     rmind 		}
    283       1.4     rmind 		topts_len -= TCPOLEN_MAXSEG;
    284       1.4     rmind 		step = sizeof(val16);
    285       1.4     rmind 		break;
    286       1.4     rmind 	case TCPOPT_WINDOW:
    287      1.10     rmind 		/* TCP Window Scaling (RFC 1323). */
    288       1.4     rmind 		if (nbuf_advfetch(&nbuf, &n_ptr, 2, sizeof(val), &val)) {
    289       1.4     rmind 			return false;
    290       1.4     rmind 		}
    291       1.4     rmind 		*wscale = (val > TCP_MAX_WINSHIFT) ? TCP_MAX_WINSHIFT : val;
    292       1.4     rmind 		topts_len -= TCPOLEN_WINDOW;
    293       1.4     rmind 		step = sizeof(val);
    294       1.4     rmind 		break;
    295       1.4     rmind 	default:
    296       1.4     rmind 		if (nbuf_advfetch(&nbuf, &n_ptr, 1, sizeof(val), &val)) {
    297       1.4     rmind 			return false;
    298       1.4     rmind 		}
    299       1.4     rmind 		if (val < 2 || val >= topts_len) {
    300       1.4     rmind 			return false;
    301       1.4     rmind 		}
    302       1.4     rmind 		topts_len -= val;
    303       1.4     rmind 		step = val - 1;
    304       1.4     rmind 	}
    305  1.10.4.2       riz 
    306       1.6     rmind 	/* Any options left? */
    307       1.4     rmind 	if (__predict_true(topts_len > 0)) {
    308       1.4     rmind 		goto next;
    309       1.4     rmind 	}
    310       1.6     rmind 	return true;
    311       1.1     rmind }
    312       1.1     rmind 
    313       1.1     rmind /*
    314       1.4     rmind  * npf_fetch_ip: fetch, check and cache IP header.
    315       1.1     rmind  */
    316       1.1     rmind bool
    317       1.4     rmind npf_fetch_ip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    318       1.1     rmind {
    319       1.4     rmind 	uint8_t ver;
    320       1.1     rmind 
    321       1.4     rmind 	if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(uint8_t), &ver)) {
    322       1.1     rmind 		return false;
    323       1.4     rmind 	}
    324  1.10.4.2       riz 
    325       1.4     rmind 	switch (ver >> 4) {
    326  1.10.4.2       riz 	case IPVERSION: {
    327  1.10.4.2       riz 		struct ip *ip = &npc->npc_ip.v4;
    328  1.10.4.2       riz 
    329  1.10.4.2       riz 		/* Fetch IPv4 header. */
    330       1.4     rmind 		if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(struct ip), ip)) {
    331       1.4     rmind 			return false;
    332       1.4     rmind 		}
    333  1.10.4.2       riz 
    334       1.4     rmind 		/* Check header length and fragment offset. */
    335      1.10     rmind 		if ((u_int)(ip->ip_hl << 2) < sizeof(struct ip)) {
    336       1.4     rmind 			return false;
    337       1.4     rmind 		}
    338       1.4     rmind 		if (ip->ip_off & ~htons(IP_DF | IP_RF)) {
    339       1.4     rmind 			/* Note fragmentation. */
    340       1.4     rmind 			npc->npc_info |= NPC_IPFRAG;
    341       1.4     rmind 		}
    342  1.10.4.2       riz 
    343       1.4     rmind 		/* Cache: layer 3 - IPv4. */
    344       1.4     rmind 		npc->npc_ipsz = sizeof(struct in_addr);
    345       1.4     rmind 		npc->npc_srcip = (npf_addr_t *)&ip->ip_src;
    346       1.4     rmind 		npc->npc_dstip = (npf_addr_t *)&ip->ip_dst;
    347       1.4     rmind 		npc->npc_info |= NPC_IP4;
    348       1.7    zoltan 		npc->npc_hlen = ip->ip_hl << 2;
    349       1.7    zoltan 		npc->npc_next_proto = npc->npc_ip.v4.ip_p;
    350       1.4     rmind 		break;
    351  1.10.4.2       riz 	}
    352       1.4     rmind 
    353  1.10.4.2       riz 	case (IPV6_VERSION >> 4): {
    354  1.10.4.2       riz 		struct ip6_hdr *ip6 = &npc->npc_ip.v6;
    355  1.10.4.2       riz 		size_t toskip;
    356  1.10.4.2       riz 		bool done;
    357  1.10.4.2       riz 
    358  1.10.4.2       riz 		/* Fetch IPv6 header. */
    359       1.7    zoltan 		if (nbuf_fetch_datum(nbuf, n_ptr, sizeof(struct ip6_hdr), ip6)) {
    360       1.7    zoltan 			return false;
    361       1.7    zoltan 		}
    362       1.7    zoltan 
    363      1.10     rmind 		/* Initial next-protocol value. */
    364  1.10.4.2       riz 		npc->npc_next_proto = ip6->ip6_nxt;
    365      1.10     rmind 		toskip = sizeof(struct ip6_hdr);
    366       1.8     rmind 		npc->npc_hlen = 0;
    367  1.10.4.2       riz 		done = false;
    368       1.7    zoltan 
    369  1.10.4.2       riz 		/*
    370  1.10.4.2       riz 		 * Advance by the length of the previous known header and
    371  1.10.4.2       riz 		 * fetch by the lengh of next extension header.
    372  1.10.4.2       riz 		 */
    373       1.7    zoltan 		do {
    374       1.8     rmind 			struct ip6_ext ip6e;
    375       1.8     rmind 
    376       1.8     rmind 			if (nbuf_advfetch(&nbuf, &n_ptr, toskip,
    377       1.8     rmind 			    sizeof(struct ip6_ext), &ip6e)) {
    378       1.7    zoltan 				return false;
    379       1.7    zoltan 			}
    380       1.7    zoltan 			switch (npc->npc_next_proto) {
    381       1.7    zoltan 			case IPPROTO_DSTOPTS:
    382       1.7    zoltan 			case IPPROTO_ROUTING:
    383       1.7    zoltan 				toskip = (ip6e.ip6e_len + 1) << 3;
    384       1.7    zoltan 				break;
    385       1.7    zoltan 			case IPPROTO_FRAGMENT:
    386       1.7    zoltan 				npc->npc_info |= NPC_IPFRAG;
    387       1.7    zoltan 				toskip = sizeof(struct ip6_frag);
    388       1.7    zoltan 				break;
    389       1.7    zoltan 			case IPPROTO_AH:
    390       1.7    zoltan 				toskip = (ip6e.ip6e_len + 2) << 2;
    391       1.7    zoltan 				break;
    392       1.7    zoltan 			default:
    393      1.10     rmind 				done = true;
    394       1.7    zoltan 				break;
    395       1.7    zoltan 			}
    396       1.7    zoltan 			npc->npc_hlen += toskip;
    397  1.10.4.2       riz 			npc->npc_next_proto = ip6e.ip6e_nxt;
    398       1.8     rmind 
    399      1.10     rmind 		} while (!done);
    400       1.7    zoltan 
    401  1.10.4.2       riz 		/* Cache: layer 3 - IPv6. */
    402       1.7    zoltan 		npc->npc_ipsz = sizeof(struct in6_addr);
    403       1.7    zoltan 		npc->npc_srcip = (npf_addr_t *)&ip6->ip6_src;
    404       1.7    zoltan 		npc->npc_dstip = (npf_addr_t *)&ip6->ip6_dst;
    405       1.7    zoltan 		npc->npc_info |= NPC_IP6;
    406       1.7    zoltan 		break;
    407  1.10.4.2       riz 	}
    408       1.4     rmind 	default:
    409       1.1     rmind 		return false;
    410       1.4     rmind 	}
    411  1.10.4.2       riz 
    412       1.4     rmind 	return true;
    413       1.4     rmind }
    414       1.1     rmind 
    415  1.10.4.2       riz /*
    416  1.10.4.2       riz  * npf_fetch_tcp: fetch, check and cache TCP header.  If necessary,
    417  1.10.4.2       riz  * fetch and cache layer 3 as well.
    418  1.10.4.2       riz  */
    419       1.4     rmind bool
    420       1.4     rmind npf_fetch_tcp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    421       1.4     rmind {
    422       1.4     rmind 	struct tcphdr *th;
    423       1.1     rmind 
    424       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    425       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    426       1.1     rmind 		return false;
    427       1.4     rmind 	}
    428       1.7    zoltan 	if (npf_cache_ipproto(npc) != IPPROTO_TCP) {
    429       1.1     rmind 		return false;
    430       1.4     rmind 	}
    431       1.4     rmind 	th = &npc->npc_l4.tcp;
    432       1.4     rmind 
    433       1.4     rmind 	/* Fetch TCP header. */
    434      1.10     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, npf_cache_hlen(npc),
    435       1.8     rmind 	    sizeof(struct tcphdr), th)) {
    436       1.1     rmind 		return false;
    437       1.4     rmind 	}
    438       1.1     rmind 
    439       1.4     rmind 	/* Cache: layer 4 - TCP. */
    440       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_TCP);
    441       1.1     rmind 	return true;
    442       1.1     rmind }
    443       1.1     rmind 
    444  1.10.4.2       riz /*
    445  1.10.4.2       riz  * npf_fetch_udp: fetch, check and cache UDP header.  If necessary,
    446  1.10.4.2       riz  * fetch and cache layer 3 as well.
    447  1.10.4.2       riz  */
    448       1.1     rmind bool
    449       1.4     rmind npf_fetch_udp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    450       1.1     rmind {
    451       1.4     rmind 	struct ip *ip = &npc->npc_ip.v4;
    452       1.4     rmind 	struct udphdr *uh;
    453      1.10     rmind 	u_int hlen;
    454       1.1     rmind 
    455       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    456       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    457       1.4     rmind 		return false;
    458       1.4     rmind 	}
    459       1.4     rmind 	if (ip->ip_p != IPPROTO_UDP) {
    460       1.1     rmind 		return false;
    461       1.4     rmind 	}
    462       1.4     rmind 	uh = &npc->npc_l4.udp;
    463      1.10     rmind 	hlen = npf_cache_hlen(npc);
    464       1.1     rmind 
    465  1.10.4.2       riz 	/* Fetch UDP header. */
    466       1.4     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, hlen, sizeof(struct udphdr), uh)) {
    467       1.1     rmind 		return false;
    468       1.4     rmind 	}
    469       1.1     rmind 
    470       1.9  jakllsch 	/* Cache: layer 4 - UDP. */
    471       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_UDP);
    472       1.1     rmind 	return true;
    473       1.1     rmind }
    474       1.1     rmind 
    475       1.2     rmind /*
    476       1.4     rmind  * npf_fetch_icmp: fetch ICMP code, type and possible query ID.
    477       1.2     rmind  */
    478       1.2     rmind bool
    479       1.4     rmind npf_fetch_icmp(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr)
    480       1.1     rmind {
    481       1.4     rmind 	struct ip *ip = &npc->npc_ip.v4;
    482       1.4     rmind 	struct icmp *ic;
    483      1.10     rmind 	u_int hlen, iclen;
    484       1.1     rmind 
    485       1.4     rmind 	/* Must have IP header processed for its length and protocol. */
    486       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    487       1.4     rmind 		return false;
    488       1.4     rmind 	}
    489       1.4     rmind 	if (ip->ip_p != IPPROTO_ICMP) {
    490       1.1     rmind 		return false;
    491       1.3     rmind 	}
    492       1.4     rmind 	ic = &npc->npc_l4.icmp;
    493      1.10     rmind 	hlen = npf_cache_hlen(npc);
    494       1.4     rmind 
    495       1.4     rmind 	/* Fetch basic ICMP header, up to the "data" point. */
    496       1.6     rmind 	iclen = offsetof(struct icmp, icmp_data);
    497       1.6     rmind 	if (nbuf_advfetch(&nbuf, &n_ptr, hlen, iclen, ic)) {
    498       1.4     rmind 		return false;
    499       1.4     rmind 	}
    500       1.4     rmind 
    501       1.4     rmind 	/* Cache: layer 4 - ICMP. */
    502       1.4     rmind 	npc->npc_info |= (NPC_LAYER4 | NPC_ICMP);
    503       1.1     rmind 	return true;
    504       1.1     rmind }
    505       1.1     rmind 
    506       1.1     rmind /*
    507       1.4     rmind  * npf_cache_all: general routine to cache all relevant IP (v4 or v6)
    508  1.10.4.2       riz  * and TCP, UDP or ICMP headers.
    509       1.1     rmind  */
    510      1.10     rmind int
    511       1.2     rmind npf_cache_all(npf_cache_t *npc, nbuf_t *nbuf)
    512       1.1     rmind {
    513       1.1     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    514       1.1     rmind 
    515       1.4     rmind 	if (!npf_iscached(npc, NPC_IP46) && !npf_fetch_ip(npc, nbuf, n_ptr)) {
    516      1.10     rmind 		return npc->npc_info;
    517       1.1     rmind 	}
    518       1.4     rmind 	if (npf_iscached(npc, NPC_IPFRAG)) {
    519      1.10     rmind 		return npc->npc_info;
    520       1.1     rmind 	}
    521       1.4     rmind 	switch (npf_cache_ipproto(npc)) {
    522       1.1     rmind 	case IPPROTO_TCP:
    523      1.10     rmind 		(void)npf_fetch_tcp(npc, nbuf, n_ptr);
    524      1.10     rmind 		break;
    525       1.1     rmind 	case IPPROTO_UDP:
    526      1.10     rmind 		(void)npf_fetch_udp(npc, nbuf, n_ptr);
    527      1.10     rmind 		break;
    528       1.1     rmind 	case IPPROTO_ICMP:
    529      1.10     rmind 		(void)npf_fetch_icmp(npc, nbuf, n_ptr);
    530      1.10     rmind 		break;
    531       1.1     rmind 	}
    532      1.10     rmind 	return npc->npc_info;
    533       1.1     rmind }
    534       1.1     rmind 
    535       1.1     rmind /*
    536       1.4     rmind  * npf_rwrip: rewrite required IP address, update the cache.
    537       1.4     rmind  */
    538       1.4     rmind bool
    539       1.4     rmind npf_rwrip(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    540       1.4     rmind     npf_addr_t *addr)
    541       1.4     rmind {
    542       1.4     rmind 	npf_addr_t *oaddr;
    543       1.4     rmind 	u_int offby;
    544       1.4     rmind 
    545       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_IP46));
    546       1.4     rmind 
    547       1.4     rmind 	if (di == PFIL_OUT) {
    548       1.4     rmind 		/* Rewrite source address, if outgoing. */
    549       1.4     rmind 		offby = offsetof(struct ip, ip_src);
    550       1.4     rmind 		oaddr = npc->npc_srcip;
    551       1.4     rmind 	} else {
    552       1.4     rmind 		/* Rewrite destination, if incoming. */
    553       1.4     rmind 		offby = offsetof(struct ip, ip_dst);
    554       1.4     rmind 		oaddr = npc->npc_dstip;
    555       1.4     rmind 	}
    556       1.4     rmind 
    557       1.4     rmind 	/* Advance to the address and rewrite it. */
    558       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, npc->npc_ipsz, addr))
    559       1.4     rmind 		return false;
    560       1.4     rmind 
    561       1.4     rmind 	/* Cache: IP address. */
    562       1.4     rmind 	memcpy(oaddr, addr, npc->npc_ipsz);
    563       1.4     rmind 	return true;
    564       1.4     rmind }
    565       1.4     rmind 
    566       1.4     rmind /*
    567       1.4     rmind  * npf_rwrport: rewrite required TCP/UDP port, update the cache.
    568       1.1     rmind  */
    569       1.1     rmind bool
    570       1.1     rmind npf_rwrport(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    571       1.4     rmind     in_port_t port)
    572       1.1     rmind {
    573       1.4     rmind 	const int proto = npf_cache_ipproto(npc);
    574      1.10     rmind 	u_int offby = npf_cache_hlen(npc);
    575       1.4     rmind 	in_port_t *oport;
    576       1.1     rmind 
    577       1.4     rmind 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    578       1.1     rmind 	KASSERT(proto == IPPROTO_TCP || proto == IPPROTO_UDP);
    579       1.1     rmind 
    580       1.4     rmind 	/* Offset to the port and pointer in the cache. */
    581       1.4     rmind 	if (proto == IPPROTO_TCP) {
    582       1.4     rmind 		struct tcphdr *th = &npc->npc_l4.tcp;
    583       1.4     rmind 		if (di == PFIL_OUT) {
    584       1.4     rmind 			CTASSERT(offsetof(struct tcphdr, th_sport) == 0);
    585       1.4     rmind 			oport = &th->th_sport;
    586       1.1     rmind 		} else {
    587       1.4     rmind 			offby += offsetof(struct tcphdr, th_dport);
    588       1.4     rmind 			oport = &th->th_dport;
    589       1.1     rmind 		}
    590       1.1     rmind 	} else {
    591       1.4     rmind 		struct udphdr *uh = &npc->npc_l4.udp;
    592       1.4     rmind 		if (di == PFIL_OUT) {
    593       1.4     rmind 			CTASSERT(offsetof(struct udphdr, uh_sport) == 0);
    594       1.4     rmind 			oport = &uh->uh_sport;
    595       1.1     rmind 		} else {
    596       1.4     rmind 			offby += offsetof(struct udphdr, uh_dport);
    597       1.4     rmind 			oport = &uh->uh_dport;
    598       1.1     rmind 		}
    599       1.1     rmind 	}
    600       1.1     rmind 
    601       1.4     rmind 	/* Advance and rewrite the port. */
    602       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(in_port_t), &port))
    603       1.1     rmind 		return false;
    604       1.1     rmind 
    605       1.4     rmind 	/* Cache: TCP/UDP port. */
    606       1.4     rmind 	*oport = port;
    607       1.1     rmind 	return true;
    608       1.1     rmind }
    609       1.1     rmind 
    610       1.1     rmind /*
    611       1.6     rmind  * npf_rwrcksum: rewrite IPv4 and/or TCP/UDP checksum, update the cache.
    612       1.1     rmind  */
    613       1.1     rmind bool
    614       1.4     rmind npf_rwrcksum(npf_cache_t *npc, nbuf_t *nbuf, void *n_ptr, const int di,
    615       1.4     rmind     npf_addr_t *addr, in_port_t port)
    616       1.1     rmind {
    617       1.4     rmind 	const int proto = npf_cache_ipproto(npc);
    618       1.4     rmind 	npf_addr_t *oaddr;
    619       1.4     rmind 	in_port_t *oport;
    620       1.4     rmind 	uint16_t *cksum;
    621       1.1     rmind 	u_int offby;
    622       1.1     rmind 
    623       1.4     rmind 	/* Checksum update for IPv4 header. */
    624       1.4     rmind 	if (npf_iscached(npc, NPC_IP4)) {
    625       1.4     rmind 		struct ip *ip = &npc->npc_ip.v4;
    626       1.4     rmind 		uint16_t ipsum;
    627       1.4     rmind 
    628       1.4     rmind 		oaddr = (di == PFIL_OUT) ? npc->npc_srcip : npc->npc_dstip;
    629       1.4     rmind 		ipsum = npf_addr_cksum(ip->ip_sum, npc->npc_ipsz, oaddr, addr);
    630       1.4     rmind 
    631       1.4     rmind 		/* Advance to the IPv4 checksum and rewrite it. */
    632       1.4     rmind 		offby = offsetof(struct ip, ip_sum);
    633       1.4     rmind 		if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(ipsum), &ipsum))
    634       1.4     rmind 			return false;
    635       1.4     rmind 
    636       1.4     rmind 		ip->ip_sum = ipsum;
    637      1.10     rmind 		offby = npf_cache_hlen(npc) - offby;
    638       1.4     rmind 	} else {
    639       1.4     rmind 		/* No checksum for IPv6. */
    640       1.4     rmind 		KASSERT(npf_iscached(npc, NPC_IP6));
    641       1.4     rmind 		oaddr = NULL;
    642       1.4     rmind 		offby = 0;
    643       1.6     rmind 		return false;	/* XXX: Not yet supported. */
    644       1.4     rmind 	}
    645       1.4     rmind 
    646       1.4     rmind 	/* Determine whether TCP/UDP checksum update is needed. */
    647       1.6     rmind 	if (proto == IPPROTO_ICMP || port == 0) {
    648       1.4     rmind 		return true;
    649       1.4     rmind 	}
    650       1.7    zoltan 	KASSERT(npf_iscached(npc, NPC_TCP) || npf_iscached(npc, NPC_UDP));
    651       1.4     rmind 
    652       1.4     rmind 	/* Calculate TCP/UDP checksum. */
    653       1.4     rmind 	if (proto == IPPROTO_TCP) {
    654       1.4     rmind 		struct tcphdr *th = &npc->npc_l4.tcp;
    655       1.4     rmind 
    656       1.4     rmind 		cksum = &th->th_sum;
    657       1.4     rmind 		offby += offsetof(struct tcphdr, th_sum);
    658       1.4     rmind 		oport = (di == PFIL_OUT) ? &th->th_sport : &th->th_dport;
    659       1.4     rmind 	} else {
    660       1.4     rmind 		struct udphdr *uh = &npc->npc_l4.udp;
    661       1.4     rmind 
    662       1.4     rmind 		KASSERT(proto == IPPROTO_UDP);
    663       1.4     rmind 		cksum = &uh->uh_sum;
    664       1.4     rmind 		if (*cksum == 0) {
    665       1.4     rmind 			/* No need to update. */
    666       1.4     rmind 			return true;
    667       1.4     rmind 		}
    668       1.4     rmind 		offby += offsetof(struct udphdr, uh_sum);
    669       1.4     rmind 		oport = (di == PFIL_OUT) ? &uh->uh_sport : &uh->uh_dport;
    670       1.4     rmind 	}
    671       1.4     rmind 	*cksum = npf_addr_cksum(*cksum, npc->npc_ipsz, oaddr, addr);
    672       1.4     rmind 	*cksum = npf_fixup16_cksum(*cksum, *oport, port);
    673       1.1     rmind 
    674       1.4     rmind 	/* Advance to TCP/UDP checksum and rewrite it. */
    675       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(uint16_t), cksum)) {
    676       1.1     rmind 		return false;
    677       1.4     rmind 	}
    678       1.4     rmind 	return true;
    679       1.4     rmind }
    680       1.4     rmind 
    681       1.4     rmind static inline bool
    682       1.5     rmind npf_normalize_ip4(npf_cache_t *npc, nbuf_t *nbuf,
    683       1.5     rmind     bool rnd, bool no_df, int minttl)
    684       1.4     rmind {
    685       1.4     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    686       1.4     rmind 	struct ip *ip = &npc->npc_ip.v4;
    687       1.4     rmind 	uint16_t cksum = ip->ip_sum;
    688       1.5     rmind 	uint16_t ip_off = ip->ip_off;
    689       1.4     rmind 	uint8_t ttl = ip->ip_ttl;
    690       1.4     rmind 	u_int offby = 0;
    691       1.4     rmind 
    692       1.5     rmind 	KASSERT(rnd || minttl || no_df);
    693       1.4     rmind 
    694       1.4     rmind 	/* Randomize IPv4 ID. */
    695       1.4     rmind 	if (rnd) {
    696       1.4     rmind 		uint16_t oid = ip->ip_id, nid;
    697       1.4     rmind 
    698       1.4     rmind 		nid = htons(ip_randomid(ip_ids, 0));
    699       1.4     rmind 		offby = offsetof(struct ip, ip_id);
    700       1.4     rmind 		if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(nid), &nid)) {
    701       1.4     rmind 			return false;
    702       1.4     rmind 		}
    703       1.4     rmind 		cksum = npf_fixup16_cksum(cksum, oid, nid);
    704       1.4     rmind 		ip->ip_id = nid;
    705       1.4     rmind 	}
    706       1.1     rmind 
    707       1.5     rmind 	/* IP_DF flag cleansing. */
    708       1.5     rmind 	if (no_df && (ip_off & htons(IP_DF)) != 0) {
    709       1.5     rmind 		uint16_t nip_off = ip_off & ~htons(IP_DF);
    710       1.5     rmind 
    711       1.5     rmind 		if (nbuf_advstore(&nbuf, &n_ptr,
    712       1.5     rmind 		    offsetof(struct ip, ip_off) - offby,
    713       1.6     rmind 		    sizeof(uint16_t), &nip_off)) {
    714       1.5     rmind 			return false;
    715       1.5     rmind 		}
    716       1.5     rmind 		cksum = npf_fixup16_cksum(cksum, ip_off, nip_off);
    717       1.5     rmind 		ip->ip_off = nip_off;
    718       1.5     rmind 		offby = offsetof(struct ip, ip_off);
    719       1.5     rmind 	}
    720       1.5     rmind 
    721       1.4     rmind 	/* Enforce minimum TTL. */
    722       1.4     rmind 	if (minttl && ttl < minttl) {
    723       1.4     rmind 		if (nbuf_advstore(&nbuf, &n_ptr,
    724       1.4     rmind 		    offsetof(struct ip, ip_ttl) - offby,
    725       1.4     rmind 		    sizeof(uint8_t), &minttl)) {
    726       1.4     rmind 			return false;
    727       1.4     rmind 		}
    728       1.4     rmind 		cksum = npf_fixup16_cksum(cksum, ttl, minttl);
    729       1.4     rmind 		ip->ip_ttl = minttl;
    730       1.4     rmind 		offby = offsetof(struct ip, ip_ttl);
    731       1.1     rmind 	}
    732       1.1     rmind 
    733       1.4     rmind 	/* Update IP checksum. */
    734       1.4     rmind 	offby = offsetof(struct ip, ip_sum) - offby;
    735       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(cksum), &cksum)) {
    736       1.1     rmind 		return false;
    737       1.4     rmind 	}
    738       1.4     rmind 	ip->ip_sum = cksum;
    739       1.4     rmind 	return true;
    740       1.4     rmind }
    741       1.4     rmind 
    742       1.4     rmind bool
    743       1.4     rmind npf_normalize(npf_cache_t *npc, nbuf_t *nbuf,
    744       1.5     rmind     bool no_df, bool rnd, u_int minttl, u_int maxmss)
    745       1.4     rmind {
    746       1.4     rmind 	void *n_ptr = nbuf_dataptr(nbuf);
    747       1.4     rmind 	struct tcphdr *th = &npc->npc_l4.tcp;
    748       1.4     rmind 	uint16_t cksum, mss;
    749      1.10     rmind 	u_int offby;
    750      1.10     rmind 	int wscale;
    751       1.4     rmind 
    752       1.4     rmind 	/* Normalize IPv4. */
    753       1.4     rmind 	if (npf_iscached(npc, NPC_IP4) && (rnd || minttl)) {
    754       1.5     rmind 		if (!npf_normalize_ip4(npc, nbuf, rnd, no_df, minttl)) {
    755       1.4     rmind 			return false;
    756       1.4     rmind 		}
    757       1.6     rmind 	} else if (!npf_iscached(npc, NPC_IP4)) {
    758       1.6     rmind 		/* XXX: no IPv6 */
    759       1.6     rmind 		return false;
    760       1.4     rmind 	}
    761       1.1     rmind 
    762       1.4     rmind 	/*
    763       1.4     rmind 	 * TCP Maximum Segment Size (MSS) "clamping".  Only if SYN packet.
    764       1.6     rmind 	 * Fetch MSS and check whether rewrite to lower is needed.
    765       1.4     rmind 	 */
    766       1.4     rmind 	if (maxmss == 0 || !npf_iscached(npc, NPC_TCP) ||
    767       1.4     rmind 	    (th->th_flags & TH_SYN) == 0) {
    768       1.4     rmind 		/* Not required; done. */
    769       1.4     rmind 		return true;
    770       1.4     rmind 	}
    771       1.4     rmind 	mss = 0;
    772       1.4     rmind 	if (!npf_fetch_tcpopts(npc, nbuf, &mss, &wscale)) {
    773       1.4     rmind 		return false;
    774       1.4     rmind 	}
    775       1.4     rmind 	if (ntohs(mss) <= maxmss) {
    776       1.4     rmind 		return true;
    777       1.4     rmind 	}
    778       1.4     rmind 
    779       1.6     rmind 	/* Calculate TCP checksum, then rewrite MSS and the checksum. */
    780       1.4     rmind 	maxmss = htons(maxmss);
    781       1.4     rmind 	cksum = npf_fixup16_cksum(th->th_sum, mss, maxmss);
    782       1.4     rmind 	th->th_sum = cksum;
    783       1.4     rmind 	mss = maxmss;
    784       1.4     rmind 	if (!npf_fetch_tcpopts(npc, nbuf, &mss, &wscale)) {
    785       1.1     rmind 		return false;
    786       1.4     rmind 	}
    787      1.10     rmind 	offby = npf_cache_hlen(npc) + offsetof(struct tcphdr, th_sum);
    788       1.4     rmind 	if (nbuf_advstore(&nbuf, &n_ptr, offby, sizeof(cksum), &cksum)) {
    789       1.4     rmind 		return false;
    790       1.4     rmind 	}
    791       1.1     rmind 	return true;
    792       1.1     rmind }
    793