Home | History | Annotate | Line # | Download | only in npfctl
npf_bpf_comp.c revision 1.8.2.1
      1  1.8.2.1  pgoyette /*	$NetBSD: npf_bpf_comp.c,v 1.8.2.1 2017/01/07 08:57:00 pgoyette Exp $	*/
      2      1.1     rmind 
      3      1.1     rmind /*-
      4      1.5     rmind  * Copyright (c) 2010-2014 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  * BPF byte-code generation for NPF rules.
     34      1.1     rmind  */
     35      1.1     rmind 
     36      1.1     rmind #include <sys/cdefs.h>
     37  1.8.2.1  pgoyette __RCSID("$NetBSD: npf_bpf_comp.c,v 1.8.2.1 2017/01/07 08:57:00 pgoyette Exp $");
     38      1.1     rmind 
     39      1.1     rmind #include <stdlib.h>
     40      1.1     rmind #include <stdbool.h>
     41      1.1     rmind #include <stddef.h>
     42      1.1     rmind #include <string.h>
     43      1.1     rmind #include <inttypes.h>
     44      1.1     rmind #include <err.h>
     45      1.1     rmind #include <assert.h>
     46      1.1     rmind 
     47      1.1     rmind #include <netinet/in.h>
     48      1.1     rmind #include <netinet/in_systm.h>
     49  1.8.2.1  pgoyette #define	__FAVOR_BSD
     50      1.1     rmind #include <netinet/ip.h>
     51      1.1     rmind #include <netinet/ip6.h>
     52      1.1     rmind #include <netinet/udp.h>
     53      1.1     rmind #include <netinet/tcp.h>
     54      1.1     rmind #include <netinet/ip_icmp.h>
     55      1.1     rmind #include <netinet/icmp6.h>
     56      1.1     rmind 
     57      1.1     rmind #include <net/bpf.h>
     58      1.1     rmind 
     59      1.1     rmind #include "npfctl.h"
     60      1.1     rmind 
     61      1.1     rmind /*
     62      1.1     rmind  * Note: clear X_EQ_L4OFF when register X is invalidated i.e. it stores
     63      1.1     rmind  * something other than L4 header offset.  Generally, when BPF_LDX is used.
     64      1.1     rmind  */
     65      1.1     rmind #define	FETCHED_L3		0x01
     66      1.6     rmind #define	CHECKED_L4		0x02
     67      1.6     rmind #define	X_EQ_L4OFF		0x04
     68      1.1     rmind 
     69      1.1     rmind struct npf_bpf {
     70      1.1     rmind 	/*
     71      1.1     rmind 	 * BPF program code, the allocated length (in bytes), the number
     72      1.1     rmind 	 * of logical blocks and the flags.
     73      1.1     rmind 	 */
     74      1.1     rmind 	struct bpf_program	prog;
     75      1.1     rmind 	size_t			alen;
     76      1.1     rmind 	u_int			nblocks;
     77      1.1     rmind 	sa_family_t		af;
     78      1.1     rmind 	uint32_t		flags;
     79      1.1     rmind 
     80      1.1     rmind 	/* The current group offset and block number. */
     81      1.1     rmind 	bool			ingroup;
     82      1.1     rmind 	u_int			goff;
     83      1.1     rmind 	u_int			gblock;
     84      1.1     rmind 
     85      1.1     rmind 	/* BPF marks, allocated length and the real length. */
     86      1.1     rmind 	uint32_t *		marks;
     87      1.1     rmind 	size_t			malen;
     88      1.1     rmind 	size_t			mlen;
     89      1.1     rmind };
     90      1.1     rmind 
     91      1.1     rmind /*
     92      1.1     rmind  * NPF success and failure values to be returned from BPF.
     93      1.1     rmind  */
     94      1.1     rmind #define	NPF_BPF_SUCCESS		((u_int)-1)
     95      1.1     rmind #define	NPF_BPF_FAILURE		0
     96      1.1     rmind 
     97      1.1     rmind /*
     98      1.1     rmind  * Magic value to indicate the failure path, which is fixed up on completion.
     99      1.1     rmind  * Note: this is the longest jump offset in BPF, since the offset is one byte.
    100      1.1     rmind  */
    101      1.1     rmind #define	JUMP_MAGIC		0xff
    102      1.1     rmind 
    103      1.1     rmind /* Reduce re-allocations by expanding in 64 byte blocks. */
    104      1.1     rmind #define	ALLOC_MASK		(64 - 1)
    105      1.1     rmind #define	ALLOC_ROUND(x)		(((x) + ALLOC_MASK) & ~ALLOC_MASK)
    106      1.1     rmind 
    107  1.8.2.1  pgoyette #ifndef IPV6_VERSION
    108  1.8.2.1  pgoyette #define	IPV6_VERSION		0x60
    109  1.8.2.1  pgoyette #endif
    110  1.8.2.1  pgoyette 
    111      1.1     rmind npf_bpf_t *
    112      1.1     rmind npfctl_bpf_create(void)
    113      1.1     rmind {
    114      1.1     rmind 	return ecalloc(1, sizeof(npf_bpf_t));
    115      1.1     rmind }
    116      1.1     rmind 
    117      1.1     rmind static void
    118      1.1     rmind fixup_jumps(npf_bpf_t *ctx, u_int start, u_int end, bool swap)
    119      1.1     rmind {
    120      1.1     rmind 	struct bpf_program *bp = &ctx->prog;
    121      1.1     rmind 
    122      1.1     rmind 	for (u_int i = start; i < end; i++) {
    123      1.1     rmind 		struct bpf_insn *insn = &bp->bf_insns[i];
    124      1.1     rmind 		const u_int fail_off = end - i;
    125      1.1     rmind 
    126      1.1     rmind 		if (fail_off >= JUMP_MAGIC) {
    127      1.1     rmind 			errx(EXIT_FAILURE, "BPF generation error: "
    128      1.1     rmind 			    "the number of instructions is over the limit");
    129      1.1     rmind 		}
    130      1.1     rmind 		if (BPF_CLASS(insn->code) != BPF_JMP) {
    131      1.1     rmind 			continue;
    132      1.1     rmind 		}
    133      1.1     rmind 		if (swap) {
    134      1.1     rmind 			uint8_t jt = insn->jt;
    135      1.1     rmind 			insn->jt = insn->jf;
    136      1.1     rmind 			insn->jf = jt;
    137      1.1     rmind 		}
    138      1.1     rmind 		if (insn->jt == JUMP_MAGIC)
    139      1.1     rmind 			insn->jt = fail_off;
    140      1.1     rmind 		if (insn->jf == JUMP_MAGIC)
    141      1.1     rmind 			insn->jf = fail_off;
    142      1.1     rmind 	}
    143      1.1     rmind }
    144      1.1     rmind 
    145      1.1     rmind static void
    146      1.1     rmind add_insns(npf_bpf_t *ctx, struct bpf_insn *insns, size_t count)
    147      1.1     rmind {
    148      1.1     rmind 	struct bpf_program *bp = &ctx->prog;
    149      1.1     rmind 	size_t offset, len, reqlen;
    150      1.1     rmind 
    151      1.1     rmind 	/* Note: bf_len is the count of instructions. */
    152      1.1     rmind 	offset = bp->bf_len * sizeof(struct bpf_insn);
    153      1.1     rmind 	len = count * sizeof(struct bpf_insn);
    154      1.1     rmind 
    155      1.1     rmind 	/* Ensure the memory buffer for the program. */
    156      1.1     rmind 	reqlen = ALLOC_ROUND(offset + len);
    157      1.1     rmind 	if (reqlen > ctx->alen) {
    158      1.1     rmind 		bp->bf_insns = erealloc(bp->bf_insns, reqlen);
    159      1.1     rmind 		ctx->alen = reqlen;
    160      1.1     rmind 	}
    161      1.1     rmind 
    162      1.1     rmind 	/* Add the code block. */
    163      1.1     rmind 	memcpy((uint8_t *)bp->bf_insns + offset, insns, len);
    164      1.1     rmind 	bp->bf_len += count;
    165      1.1     rmind }
    166      1.1     rmind 
    167      1.1     rmind static void
    168      1.1     rmind done_raw_block(npf_bpf_t *ctx, const uint32_t *m, size_t len)
    169      1.1     rmind {
    170      1.1     rmind 	size_t reqlen, nargs = m[1];
    171      1.1     rmind 
    172      1.1     rmind 	if ((len / sizeof(uint32_t) - 2) != nargs) {
    173      1.1     rmind 		errx(EXIT_FAILURE, "invalid BPF block description");
    174      1.1     rmind 	}
    175      1.1     rmind 	reqlen = ALLOC_ROUND(ctx->mlen + len);
    176      1.1     rmind 	if (reqlen > ctx->malen) {
    177      1.1     rmind 		ctx->marks = erealloc(ctx->marks, reqlen);
    178      1.1     rmind 		ctx->malen = reqlen;
    179      1.1     rmind 	}
    180      1.1     rmind 	memcpy((uint8_t *)ctx->marks + ctx->mlen, m, len);
    181      1.1     rmind 	ctx->mlen += len;
    182      1.1     rmind }
    183      1.1     rmind 
    184      1.1     rmind static void
    185      1.1     rmind done_block(npf_bpf_t *ctx, const uint32_t *m, size_t len)
    186      1.1     rmind {
    187      1.1     rmind 	done_raw_block(ctx, m, len);
    188      1.1     rmind 	ctx->nblocks++;
    189      1.1     rmind }
    190      1.1     rmind 
    191      1.1     rmind struct bpf_program *
    192      1.1     rmind npfctl_bpf_complete(npf_bpf_t *ctx)
    193      1.1     rmind {
    194      1.1     rmind 	struct bpf_program *bp = &ctx->prog;
    195      1.1     rmind 	const u_int retoff = bp->bf_len;
    196      1.1     rmind 
    197      1.8     rmind 	/* No instructions (optimised out). */
    198      1.8     rmind 	if (!bp->bf_len)
    199      1.8     rmind 		return NULL;
    200      1.8     rmind 
    201      1.1     rmind 	/* Add the return fragment (success and failure paths). */
    202      1.1     rmind 	struct bpf_insn insns_ret[] = {
    203      1.1     rmind 		BPF_STMT(BPF_RET+BPF_K, NPF_BPF_SUCCESS),
    204      1.1     rmind 		BPF_STMT(BPF_RET+BPF_K, NPF_BPF_FAILURE),
    205      1.1     rmind 	};
    206      1.1     rmind 	add_insns(ctx, insns_ret, __arraycount(insns_ret));
    207      1.1     rmind 
    208      1.1     rmind 	/* Fixup all jumps to the main failure path. */
    209      1.1     rmind 	fixup_jumps(ctx, 0, retoff, false);
    210      1.1     rmind 
    211      1.1     rmind 	return &ctx->prog;
    212      1.1     rmind }
    213      1.1     rmind 
    214      1.1     rmind const void *
    215      1.1     rmind npfctl_bpf_bmarks(npf_bpf_t *ctx, size_t *len)
    216      1.1     rmind {
    217      1.1     rmind 	*len = ctx->mlen;
    218      1.1     rmind 	return ctx->marks;
    219      1.1     rmind }
    220      1.1     rmind 
    221      1.1     rmind void
    222      1.1     rmind npfctl_bpf_destroy(npf_bpf_t *ctx)
    223      1.1     rmind {
    224      1.1     rmind 	free(ctx->prog.bf_insns);
    225      1.1     rmind 	free(ctx->marks);
    226      1.1     rmind 	free(ctx);
    227      1.1     rmind }
    228      1.1     rmind 
    229      1.1     rmind /*
    230      1.1     rmind  * npfctl_bpf_group: begin a logical group.  It merely uses logical
    231      1.1     rmind  * disjunction (OR) for compares within the group.
    232      1.1     rmind  */
    233      1.1     rmind void
    234      1.1     rmind npfctl_bpf_group(npf_bpf_t *ctx)
    235      1.1     rmind {
    236      1.1     rmind 	struct bpf_program *bp = &ctx->prog;
    237      1.1     rmind 
    238      1.1     rmind 	assert(ctx->goff == 0);
    239      1.1     rmind 	assert(ctx->gblock == 0);
    240      1.1     rmind 
    241      1.1     rmind 	ctx->goff = bp->bf_len;
    242      1.1     rmind 	ctx->gblock = ctx->nblocks;
    243      1.1     rmind 	ctx->ingroup = true;
    244      1.1     rmind }
    245      1.1     rmind 
    246      1.1     rmind void
    247  1.8.2.1  pgoyette npfctl_bpf_endgroup(npf_bpf_t *ctx, bool invert)
    248      1.1     rmind {
    249      1.1     rmind 	struct bpf_program *bp = &ctx->prog;
    250      1.1     rmind 	const size_t curoff = bp->bf_len;
    251      1.1     rmind 
    252      1.1     rmind 	/* If there are no blocks or only one - nothing to do. */
    253  1.8.2.1  pgoyette 	if (!invert && (ctx->nblocks - ctx->gblock) <= 1) {
    254      1.1     rmind 		ctx->goff = ctx->gblock = 0;
    255      1.1     rmind 		return;
    256      1.1     rmind 	}
    257      1.1     rmind 
    258      1.1     rmind 	/*
    259  1.8.2.1  pgoyette 	 * If inverting, then prepend a jump over the statement below.
    260  1.8.2.1  pgoyette 	 * If matching, jump will jump below and the fail will happen.
    261  1.8.2.1  pgoyette 	 */
    262  1.8.2.1  pgoyette 	if (invert) {
    263  1.8.2.1  pgoyette 		struct bpf_insn insns_ret[] = {
    264  1.8.2.1  pgoyette 			BPF_STMT(BPF_JMP+BPF_JA, 1),
    265  1.8.2.1  pgoyette 		};
    266  1.8.2.1  pgoyette 		add_insns(ctx, insns_ret, __arraycount(insns_ret));
    267  1.8.2.1  pgoyette 	}
    268  1.8.2.1  pgoyette 
    269  1.8.2.1  pgoyette 	/*
    270      1.1     rmind 	 * Append a failure return as a fall-through i.e. if there is
    271      1.1     rmind 	 * no match within the group.
    272      1.1     rmind 	 */
    273      1.1     rmind 	struct bpf_insn insns_ret[] = {
    274      1.1     rmind 		BPF_STMT(BPF_RET+BPF_K, NPF_BPF_FAILURE),
    275      1.1     rmind 	};
    276      1.1     rmind 	add_insns(ctx, insns_ret, __arraycount(insns_ret));
    277      1.1     rmind 
    278      1.1     rmind 	/*
    279      1.1     rmind 	 * Adjust jump offsets: on match - jump outside the group i.e.
    280      1.1     rmind 	 * to the current offset.  Otherwise, jump to the next instruction
    281      1.1     rmind 	 * which would lead to the fall-through code above if none matches.
    282      1.1     rmind 	 */
    283      1.1     rmind 	fixup_jumps(ctx, ctx->goff, curoff, true);
    284      1.1     rmind 	ctx->goff = ctx->gblock = 0;
    285      1.1     rmind }
    286      1.1     rmind 
    287      1.1     rmind static void
    288      1.1     rmind fetch_l3(npf_bpf_t *ctx, sa_family_t af, u_int flags)
    289      1.1     rmind {
    290      1.1     rmind 	u_int ver;
    291      1.1     rmind 
    292      1.1     rmind 	switch (af) {
    293      1.1     rmind 	case AF_INET:
    294      1.1     rmind 		ver = IPVERSION;
    295      1.1     rmind 		break;
    296      1.1     rmind 	case AF_INET6:
    297      1.1     rmind 		ver = IPV6_VERSION >> 4;
    298      1.1     rmind 		break;
    299      1.1     rmind 	case AF_UNSPEC:
    300      1.1     rmind 		ver = 0;
    301      1.1     rmind 		break;
    302      1.1     rmind 	default:
    303      1.1     rmind 		abort();
    304      1.1     rmind 	}
    305      1.1     rmind 
    306      1.1     rmind 	/*
    307      1.7     rmind 	 * The memory store is populated with:
    308      1.1     rmind 	 * - BPF_MW_IPVER: IP version (4 or 6).
    309      1.1     rmind 	 * - BPF_MW_L4OFF: L4 header offset.
    310      1.1     rmind 	 * - BPF_MW_L4PROTO: L4 protocol.
    311      1.1     rmind 	 */
    312      1.1     rmind 	if ((ctx->flags & FETCHED_L3) == 0 || (af && ctx->af == 0)) {
    313      1.1     rmind 		const uint8_t jt = ver ? 0 : JUMP_MAGIC;
    314      1.1     rmind 		const uint8_t jf = ver ? JUMP_MAGIC : 0;
    315      1.1     rmind 		bool ingroup = ctx->ingroup;
    316      1.1     rmind 
    317      1.1     rmind 		/*
    318      1.1     rmind 		 * L3 block cannot be inserted in the middle of a group.
    319      1.1     rmind 		 * In fact, it never is.  Check and start the group after.
    320      1.1     rmind 		 */
    321      1.1     rmind 		if (ingroup) {
    322      1.1     rmind 			assert(ctx->nblocks == ctx->gblock);
    323  1.8.2.1  pgoyette 			npfctl_bpf_endgroup(ctx, false);
    324      1.1     rmind 		}
    325      1.1     rmind 
    326      1.1     rmind 		/*
    327      1.1     rmind 		 * A <- IP version; A == expected-version?
    328      1.1     rmind 		 * If no particular version specified, check for non-zero.
    329      1.1     rmind 		 */
    330      1.7     rmind 		struct bpf_insn insns_af[] = {
    331      1.7     rmind 			BPF_STMT(BPF_LD+BPF_W+BPF_MEM, BPF_MW_IPVER),
    332      1.7     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ver, jt, jf),
    333      1.7     rmind 		};
    334      1.7     rmind 		add_insns(ctx, insns_af, __arraycount(insns_af));
    335      1.7     rmind 		ctx->flags |= FETCHED_L3;
    336      1.1     rmind 		ctx->af = af;
    337      1.1     rmind 
    338      1.1     rmind 		if (af) {
    339      1.1     rmind 			uint32_t mwords[] = { BM_IPVER, 1, af };
    340      1.1     rmind 			done_raw_block(ctx, mwords, sizeof(mwords));
    341      1.1     rmind 		}
    342      1.1     rmind 		if (ingroup) {
    343      1.1     rmind 			npfctl_bpf_group(ctx);
    344      1.1     rmind 		}
    345      1.1     rmind 
    346      1.1     rmind 	} else if (af && af != ctx->af) {
    347      1.1     rmind 		errx(EXIT_FAILURE, "address family mismatch");
    348      1.1     rmind 	}
    349      1.1     rmind 
    350      1.1     rmind 	if ((flags & X_EQ_L4OFF) != 0 && (ctx->flags & X_EQ_L4OFF) == 0) {
    351      1.1     rmind 		/* X <- IP header length */
    352      1.1     rmind 		struct bpf_insn insns_hlen[] = {
    353      1.1     rmind 			BPF_STMT(BPF_LDX+BPF_MEM, BPF_MW_L4OFF),
    354      1.1     rmind 		};
    355      1.1     rmind 		add_insns(ctx, insns_hlen, __arraycount(insns_hlen));
    356      1.1     rmind 		ctx->flags |= X_EQ_L4OFF;
    357      1.1     rmind 	}
    358      1.1     rmind }
    359      1.1     rmind 
    360      1.1     rmind /*
    361      1.1     rmind  * npfctl_bpf_proto: code block to match IP version and L4 protocol.
    362      1.1     rmind  */
    363      1.1     rmind void
    364      1.1     rmind npfctl_bpf_proto(npf_bpf_t *ctx, sa_family_t af, int proto)
    365      1.1     rmind {
    366      1.1     rmind 	assert(af != AF_UNSPEC || proto != -1);
    367      1.1     rmind 
    368      1.1     rmind 	/* Note: fails if IP version does not match. */
    369      1.1     rmind 	fetch_l3(ctx, af, 0);
    370      1.1     rmind 	if (proto == -1) {
    371      1.1     rmind 		return;
    372      1.1     rmind 	}
    373      1.1     rmind 
    374      1.1     rmind 	struct bpf_insn insns_proto[] = {
    375      1.1     rmind 		/* A <- L4 protocol; A == expected-protocol? */
    376      1.1     rmind 		BPF_STMT(BPF_LD+BPF_W+BPF_MEM, BPF_MW_L4PROTO),
    377      1.1     rmind 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, proto, 0, JUMP_MAGIC),
    378      1.1     rmind 	};
    379      1.1     rmind 	add_insns(ctx, insns_proto, __arraycount(insns_proto));
    380      1.1     rmind 
    381      1.1     rmind 	uint32_t mwords[] = { BM_PROTO, 1, proto };
    382      1.1     rmind 	done_block(ctx, mwords, sizeof(mwords));
    383      1.6     rmind 	ctx->flags |= CHECKED_L4;
    384      1.1     rmind }
    385      1.1     rmind 
    386      1.1     rmind /*
    387      1.1     rmind  * npfctl_bpf_cidr: code block to match IPv4 or IPv6 CIDR.
    388      1.1     rmind  *
    389      1.1     rmind  * => IP address shall be in the network byte order.
    390      1.1     rmind  */
    391      1.1     rmind void
    392      1.1     rmind npfctl_bpf_cidr(npf_bpf_t *ctx, u_int opts, sa_family_t af,
    393      1.1     rmind     const npf_addr_t *addr, const npf_netmask_t mask)
    394      1.1     rmind {
    395      1.1     rmind 	const uint32_t *awords = (const uint32_t *)addr;
    396      1.1     rmind 	u_int nwords, length, maxmask, off;
    397      1.1     rmind 
    398      1.1     rmind 	assert(((opts & MATCH_SRC) != 0) ^ ((opts & MATCH_DST) != 0));
    399      1.1     rmind 	assert((mask && mask <= NPF_MAX_NETMASK) || mask == NPF_NO_NETMASK);
    400      1.1     rmind 
    401      1.1     rmind 	switch (af) {
    402      1.1     rmind 	case AF_INET:
    403      1.1     rmind 		maxmask = 32;
    404      1.1     rmind 		off = (opts & MATCH_SRC) ?
    405      1.1     rmind 		    offsetof(struct ip, ip_src) :
    406      1.1     rmind 		    offsetof(struct ip, ip_dst);
    407      1.1     rmind 		nwords = sizeof(struct in_addr) / sizeof(uint32_t);
    408      1.1     rmind 		break;
    409      1.1     rmind 	case AF_INET6:
    410      1.1     rmind 		maxmask = 128;
    411      1.1     rmind 		off = (opts & MATCH_SRC) ?
    412      1.1     rmind 		    offsetof(struct ip6_hdr, ip6_src) :
    413      1.1     rmind 		    offsetof(struct ip6_hdr, ip6_dst);
    414      1.1     rmind 		nwords = sizeof(struct in6_addr) / sizeof(uint32_t);
    415      1.1     rmind 		break;
    416      1.1     rmind 	default:
    417      1.1     rmind 		abort();
    418      1.1     rmind 	}
    419      1.1     rmind 
    420      1.1     rmind 	/* Ensure address family. */
    421      1.1     rmind 	fetch_l3(ctx, af, 0);
    422      1.1     rmind 
    423      1.1     rmind 	length = (mask == NPF_NO_NETMASK) ? maxmask : mask;
    424      1.1     rmind 
    425      1.1     rmind 	/* CAUTION: BPF operates in host byte-order. */
    426      1.1     rmind 	for (u_int i = 0; i < nwords; i++) {
    427      1.1     rmind 		const u_int woff = i * sizeof(uint32_t);
    428      1.1     rmind 		uint32_t word = ntohl(awords[i]);
    429      1.1     rmind 		uint32_t wordmask;
    430      1.1     rmind 
    431      1.1     rmind 		if (length >= 32) {
    432      1.1     rmind 			/* The mask is a full word - do not apply it. */
    433      1.1     rmind 			wordmask = 0;
    434      1.1     rmind 			length -= 32;
    435      1.1     rmind 		} else if (length) {
    436      1.4     rmind 			wordmask = 0xffffffff << (32 - length);
    437      1.1     rmind 			length = 0;
    438      1.1     rmind 		} else {
    439      1.3     rmind 			/* The mask became zero - skip the rest. */
    440      1.3     rmind 			break;
    441      1.1     rmind 		}
    442      1.1     rmind 
    443      1.1     rmind 		/* A <- IP address (or one word of it) */
    444      1.1     rmind 		struct bpf_insn insns_ip[] = {
    445      1.1     rmind 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, off + woff),
    446      1.1     rmind 		};
    447      1.1     rmind 		add_insns(ctx, insns_ip, __arraycount(insns_ip));
    448      1.1     rmind 
    449      1.1     rmind 		/* A <- (A & MASK) */
    450      1.1     rmind 		if (wordmask) {
    451      1.1     rmind 			struct bpf_insn insns_mask[] = {
    452      1.1     rmind 				BPF_STMT(BPF_ALU+BPF_AND+BPF_K, wordmask),
    453      1.1     rmind 			};
    454      1.1     rmind 			add_insns(ctx, insns_mask, __arraycount(insns_mask));
    455      1.1     rmind 		}
    456      1.1     rmind 
    457      1.1     rmind 		/* A == expected-IP-word ? */
    458      1.1     rmind 		struct bpf_insn insns_cmp[] = {
    459      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, word, 0, JUMP_MAGIC),
    460      1.1     rmind 		};
    461      1.1     rmind 		add_insns(ctx, insns_cmp, __arraycount(insns_cmp));
    462      1.1     rmind 	}
    463      1.1     rmind 
    464      1.1     rmind 	uint32_t mwords[] = {
    465      1.1     rmind 		(opts & MATCH_SRC) ? BM_SRC_CIDR: BM_DST_CIDR, 6,
    466      1.1     rmind 		af, mask, awords[0], awords[1], awords[2], awords[3],
    467      1.1     rmind 	};
    468      1.1     rmind 	done_block(ctx, mwords, sizeof(mwords));
    469      1.1     rmind }
    470      1.1     rmind 
    471      1.1     rmind /*
    472      1.1     rmind  * npfctl_bpf_ports: code block to match TCP/UDP port range.
    473      1.1     rmind  *
    474      1.1     rmind  * => Port numbers shall be in the network byte order.
    475      1.1     rmind  */
    476      1.1     rmind void
    477      1.1     rmind npfctl_bpf_ports(npf_bpf_t *ctx, u_int opts, in_port_t from, in_port_t to)
    478      1.1     rmind {
    479      1.1     rmind 	const u_int sport_off = offsetof(struct udphdr, uh_sport);
    480      1.1     rmind 	const u_int dport_off = offsetof(struct udphdr, uh_dport);
    481      1.1     rmind 	u_int off;
    482      1.1     rmind 
    483      1.1     rmind 	/* TCP and UDP port offsets are the same. */
    484      1.1     rmind 	assert(sport_off == offsetof(struct tcphdr, th_sport));
    485      1.1     rmind 	assert(dport_off == offsetof(struct tcphdr, th_dport));
    486      1.6     rmind 	assert(ctx->flags & CHECKED_L4);
    487      1.1     rmind 
    488      1.1     rmind 	assert(((opts & MATCH_SRC) != 0) ^ ((opts & MATCH_DST) != 0));
    489      1.1     rmind 	off = (opts & MATCH_SRC) ? sport_off : dport_off;
    490      1.1     rmind 
    491      1.1     rmind 	/* X <- IP header length */
    492      1.2     rmind 	fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
    493      1.1     rmind 
    494      1.1     rmind 	struct bpf_insn insns_fetch[] = {
    495      1.1     rmind 		/* A <- port */
    496      1.1     rmind 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, off),
    497      1.1     rmind 	};
    498      1.1     rmind 	add_insns(ctx, insns_fetch, __arraycount(insns_fetch));
    499      1.1     rmind 
    500      1.1     rmind 	/* CAUTION: BPF operates in host byte-order. */
    501      1.1     rmind 	from = ntohs(from);
    502      1.1     rmind 	to = ntohs(to);
    503      1.1     rmind 
    504      1.1     rmind 	if (from == to) {
    505      1.1     rmind 		/* Single port case. */
    506      1.1     rmind 		struct bpf_insn insns_port[] = {
    507      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, from, 0, JUMP_MAGIC),
    508      1.1     rmind 		};
    509      1.1     rmind 		add_insns(ctx, insns_port, __arraycount(insns_port));
    510      1.1     rmind 	} else {
    511      1.1     rmind 		/* Port range case. */
    512      1.1     rmind 		struct bpf_insn insns_range[] = {
    513      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, from, 0, JUMP_MAGIC),
    514      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, to, JUMP_MAGIC, 0),
    515      1.1     rmind 		};
    516      1.1     rmind 		add_insns(ctx, insns_range, __arraycount(insns_range));
    517      1.1     rmind 	}
    518      1.1     rmind 
    519      1.1     rmind 	uint32_t mwords[] = {
    520      1.1     rmind 		opts & MATCH_SRC ? BM_SRC_PORTS : BM_DST_PORTS, 2, from, to
    521      1.1     rmind 	};
    522      1.1     rmind 	done_block(ctx, mwords, sizeof(mwords));
    523      1.1     rmind }
    524      1.1     rmind 
    525      1.1     rmind /*
    526      1.1     rmind  * npfctl_bpf_tcpfl: code block to match TCP flags.
    527      1.1     rmind  */
    528      1.1     rmind void
    529      1.5     rmind npfctl_bpf_tcpfl(npf_bpf_t *ctx, uint8_t tf, uint8_t tf_mask, bool checktcp)
    530      1.1     rmind {
    531      1.1     rmind 	const u_int tcpfl_off = offsetof(struct tcphdr, th_flags);
    532      1.6     rmind 	const bool usingmask = tf_mask != tf;
    533      1.1     rmind 
    534      1.1     rmind 	/* X <- IP header length */
    535      1.2     rmind 	fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
    536      1.5     rmind 	if (checktcp) {
    537      1.6     rmind 		const u_int jf = usingmask ? 3 : 2;
    538      1.5     rmind 		assert(ctx->ingroup == false);
    539      1.5     rmind 
    540      1.5     rmind 		/* A <- L4 protocol; A == TCP?  If not, jump out. */
    541      1.5     rmind 		struct bpf_insn insns_tcp[] = {
    542      1.5     rmind 			BPF_STMT(BPF_LD+BPF_W+BPF_MEM, BPF_MW_L4PROTO),
    543      1.5     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_TCP, 0, jf),
    544      1.5     rmind 		};
    545      1.5     rmind 		add_insns(ctx, insns_tcp, __arraycount(insns_tcp));
    546      1.6     rmind 	} else {
    547      1.6     rmind 		assert(ctx->flags & CHECKED_L4);
    548      1.5     rmind 	}
    549      1.1     rmind 
    550      1.1     rmind 	struct bpf_insn insns_tf[] = {
    551      1.1     rmind 		/* A <- TCP flags */
    552      1.1     rmind 		BPF_STMT(BPF_LD+BPF_B+BPF_IND, tcpfl_off),
    553      1.1     rmind 	};
    554      1.1     rmind 	add_insns(ctx, insns_tf, __arraycount(insns_tf));
    555      1.1     rmind 
    556      1.6     rmind 	if (usingmask) {
    557      1.1     rmind 		/* A <- (A & mask) */
    558      1.1     rmind 		struct bpf_insn insns_mask[] = {
    559      1.1     rmind 			BPF_STMT(BPF_ALU+BPF_AND+BPF_K, tf_mask),
    560      1.1     rmind 		};
    561      1.1     rmind 		add_insns(ctx, insns_mask, __arraycount(insns_mask));
    562      1.1     rmind 	}
    563      1.1     rmind 
    564      1.1     rmind 	struct bpf_insn insns_cmp[] = {
    565      1.1     rmind 		/* A == expected-TCP-flags? */
    566      1.1     rmind 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, tf, 0, JUMP_MAGIC),
    567      1.1     rmind 	};
    568      1.1     rmind 	add_insns(ctx, insns_cmp, __arraycount(insns_cmp));
    569      1.1     rmind 
    570      1.5     rmind 	if (!checktcp) {
    571      1.5     rmind 		uint32_t mwords[] = { BM_TCPFL, 2, tf, tf_mask};
    572      1.5     rmind 		done_block(ctx, mwords, sizeof(mwords));
    573      1.5     rmind 	}
    574      1.1     rmind }
    575      1.1     rmind 
    576      1.1     rmind /*
    577      1.1     rmind  * npfctl_bpf_icmp: code block to match ICMP type and/or code.
    578      1.1     rmind  * Note: suitable both for the ICMPv4 and ICMPv6.
    579      1.1     rmind  */
    580      1.1     rmind void
    581      1.1     rmind npfctl_bpf_icmp(npf_bpf_t *ctx, int type, int code)
    582      1.1     rmind {
    583      1.1     rmind 	const u_int type_off = offsetof(struct icmp, icmp_type);
    584      1.1     rmind 	const u_int code_off = offsetof(struct icmp, icmp_code);
    585      1.1     rmind 
    586      1.6     rmind 	assert(ctx->flags & CHECKED_L4);
    587      1.1     rmind 	assert(offsetof(struct icmp6_hdr, icmp6_type) == type_off);
    588      1.1     rmind 	assert(offsetof(struct icmp6_hdr, icmp6_code) == code_off);
    589      1.1     rmind 	assert(type != -1 || code != -1);
    590      1.1     rmind 
    591      1.1     rmind 	/* X <- IP header length */
    592      1.2     rmind 	fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
    593      1.1     rmind 
    594      1.1     rmind 	if (type != -1) {
    595      1.1     rmind 		struct bpf_insn insns_type[] = {
    596      1.1     rmind 			BPF_STMT(BPF_LD+BPF_B+BPF_IND, type_off),
    597      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, type, 0, JUMP_MAGIC),
    598      1.1     rmind 		};
    599      1.1     rmind 		add_insns(ctx, insns_type, __arraycount(insns_type));
    600      1.1     rmind 
    601      1.1     rmind 		uint32_t mwords[] = { BM_ICMP_TYPE, 1, type };
    602      1.1     rmind 		done_block(ctx, mwords, sizeof(mwords));
    603      1.1     rmind 	}
    604      1.1     rmind 
    605      1.1     rmind 	if (code != -1) {
    606      1.1     rmind 		struct bpf_insn insns_code[] = {
    607      1.1     rmind 			BPF_STMT(BPF_LD+BPF_B+BPF_IND, code_off),
    608      1.1     rmind 			BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, code, 0, JUMP_MAGIC),
    609      1.1     rmind 		};
    610      1.1     rmind 		add_insns(ctx, insns_code, __arraycount(insns_code));
    611      1.1     rmind 
    612      1.1     rmind 		uint32_t mwords[] = { BM_ICMP_CODE, 1, code };
    613      1.1     rmind 		done_block(ctx, mwords, sizeof(mwords));
    614      1.1     rmind 	}
    615      1.1     rmind }
    616      1.1     rmind 
    617      1.1     rmind #define	SRC_FLAG_BIT	(1U << 31)
    618      1.1     rmind 
    619      1.1     rmind /*
    620      1.1     rmind  * npfctl_bpf_table: code block to match source/destination IP address
    621      1.1     rmind  * against NPF table specified by ID.
    622      1.1     rmind  */
    623      1.1     rmind void
    624      1.1     rmind npfctl_bpf_table(npf_bpf_t *ctx, u_int opts, u_int tid)
    625      1.1     rmind {
    626      1.1     rmind 	const bool src = (opts & MATCH_SRC) != 0;
    627      1.1     rmind 
    628      1.1     rmind 	struct bpf_insn insns_table[] = {
    629      1.1     rmind 		BPF_STMT(BPF_LD+BPF_IMM, (src ? SRC_FLAG_BIT : 0) | tid),
    630      1.1     rmind 		BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_TABLE),
    631      1.1     rmind 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, JUMP_MAGIC, 0),
    632      1.1     rmind 	};
    633      1.1     rmind 	add_insns(ctx, insns_table, __arraycount(insns_table));
    634      1.1     rmind 
    635      1.1     rmind 	uint32_t mwords[] = { src ? BM_SRC_TABLE: BM_DST_TABLE, 1, tid };
    636      1.1     rmind 	done_block(ctx, mwords, sizeof(mwords));
    637      1.1     rmind }
    638