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