17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d6f299a8Sdg  * Common Development and Distribution License (the "License").
6d6f299a8Sdg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*2b24ab6bSSebastien Roy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <ctype.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <stddef.h>
35*2b24ab6bSSebastien Roy #include <unistd.h>
36*2b24ab6bSSebastien Roy #include <stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/socket.h>
387c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
39605445d5Sdg #include <sys/vlan.h>
407c478bd9Sstevel@tonic-gate #include <net/if.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
437c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip.h>
457c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
467c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
477c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
487c478bd9Sstevel@tonic-gate #include <netdb.h>
497c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
507c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
517c478bd9Sstevel@tonic-gate #include <rpc/rpcent.h>
52b127ac41SPhilip Kirk #include <sys/dlpi.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <snoop.h>
55605445d5Sdg #include "snoop_vlan.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	IPV4_ONLY	0
587c478bd9Sstevel@tonic-gate #define	IPV6_ONLY	1
597c478bd9Sstevel@tonic-gate #define	IPV4_AND_IPV6	2
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * The following constants represent the offsets in bytes from the beginning
637c478bd9Sstevel@tonic-gate  * of the IP(v6) header of the source and destination IP(v6) addresses.
647c478bd9Sstevel@tonic-gate  * These are useful when generating filter code.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #define	IPV4_SRCADDR_OFFSET	12
677c478bd9Sstevel@tonic-gate #define	IPV4_DSTADDR_OFFSET	16
687c478bd9Sstevel@tonic-gate #define	IPV6_SRCADDR_OFFSET	8
697c478bd9Sstevel@tonic-gate #define	IPV6_DSTADDR_OFFSET	24
707c478bd9Sstevel@tonic-gate #define	IP_VERS(p)	(((*(uchar_t *)p) & 0xf0) >> 4)
717c478bd9Sstevel@tonic-gate #define	MASKED_IPV4_VERS	0x40
727c478bd9Sstevel@tonic-gate #define	MASKED_IPV6_VERS	0x60
737c478bd9Sstevel@tonic-gate #define	IP_HDR_LEN(p)	(((*(uchar_t *)p) & 0xf) * 4)
747c478bd9Sstevel@tonic-gate #define	TCP_HDR_LEN(p)	((((*((uchar_t *)p+12)) >> 4) & 0xf) * 4)
75605445d5Sdg 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * Coding the constant below is tacky, but the compiler won't let us
787c478bd9Sstevel@tonic-gate  * be more clever.  E.g., &((struct ip *)0)->ip_xxx
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate #define	IP_PROTO_OF(p)	(((uchar_t *)p)[9])
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * AppleTalk uses 802.2 Ethernet encapsulation with LLC/SNAP headers,
847c478bd9Sstevel@tonic-gate  * for 8 octets of overhead, and the common AppleTalk DDP Ethernet
857c478bd9Sstevel@tonic-gate  * header is another 4 octets.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * The following constants represents the offsets in bytes from the beginning
887c478bd9Sstevel@tonic-gate  * of the Ethernet payload to various parts of the DDP header.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	AT_DST_NET_OFFSET	12
927c478bd9Sstevel@tonic-gate #define	AT_SRC_NET_OFFSET	14
937c478bd9Sstevel@tonic-gate #define	AT_DST_NODE_OFFSET	16
947c478bd9Sstevel@tonic-gate #define	AT_SRC_NODE_OFFSET	17
957c478bd9Sstevel@tonic-gate 
96b127ac41SPhilip Kirk /*
97b127ac41SPhilip Kirk  * Offset for the source and destination zoneid in the ipnet header.
98b127ac41SPhilip Kirk  */
99b127ac41SPhilip Kirk #define	IPNET_SRCZONE_OFFSET 8
100b127ac41SPhilip Kirk #define	IPNET_DSTZONE_OFFSET 16
101b127ac41SPhilip Kirk 
1027c478bd9Sstevel@tonic-gate int eaddr;	/* need ethernet addr */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate int opstack;	/* operand stack depth */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * These are the operators of the user-level filter.
1087c478bd9Sstevel@tonic-gate  * STOP ends execution of the filter expression and
1097c478bd9Sstevel@tonic-gate  * returns the truth value at the top of the stack.
1107c478bd9Sstevel@tonic-gate  * OP_LOAD_OCTET, OP_LOAD_SHORT and OP_LOAD_LONG pop
1117c478bd9Sstevel@tonic-gate  * an offset value from the stack and load a value of
1127c478bd9Sstevel@tonic-gate  * an appropriate size from the packet (octet, short or
1137c478bd9Sstevel@tonic-gate  * long).  The offset is computed from a base value that
1147c478bd9Sstevel@tonic-gate  * may be set via the OP_OFFSET operators.
1157c478bd9Sstevel@tonic-gate  * OP_EQ, OP_NE, OP_GT, OP_GE, OP_LT, OP_LE pop two values
1167c478bd9Sstevel@tonic-gate  * from the stack and return the result of their comparison.
1177c478bd9Sstevel@tonic-gate  * OP_AND, OP_OR, OP_XOR pop two values from the stack and
1187c478bd9Sstevel@tonic-gate  * do perform a bitwise operation on them - returning a result
1197c478bd9Sstevel@tonic-gate  * to the stack.  OP_NOT inverts the bits of the value on the
1207c478bd9Sstevel@tonic-gate  * stack.
1217c478bd9Sstevel@tonic-gate  * OP_BRFL and OP_BRTR branch to an offset in the code array
1227c478bd9Sstevel@tonic-gate  * depending on the value at the top of the stack: true (not 0)
1237c478bd9Sstevel@tonic-gate  * or false (0).
1247c478bd9Sstevel@tonic-gate  * OP_ADD, OP_SUB, OP_MUL, OP_DIV and OP_REM pop two values
1257c478bd9Sstevel@tonic-gate  * from the stack and perform arithmetic.
1267c478bd9Sstevel@tonic-gate  * The OP_OFFSET operators change the base from which the
1277c478bd9Sstevel@tonic-gate  * OP_LOAD operators compute their offsets.
1287c478bd9Sstevel@tonic-gate  * OP_OFFSET_ZERO sets the offset to zero - beginning of packet.
1297c478bd9Sstevel@tonic-gate  * OP_OFFSET_LINK sets the base to the first octet after
1307c478bd9Sstevel@tonic-gate  * the link (DLC) header.  OP_OFFSET_IP, OP_OFFSET_TCP,
1317c478bd9Sstevel@tonic-gate  * and OP_OFFSET_UDP do the same for those headers - they
1327c478bd9Sstevel@tonic-gate  * set the offset base to the *end* of the header - not the
1337c478bd9Sstevel@tonic-gate  * beginning.  The OP_OFFSET_RPC operator is a bit unusual.
1347c478bd9Sstevel@tonic-gate  * It points the base at the cached RPC header.  For the
1357c478bd9Sstevel@tonic-gate  * purposes of selection, RPC reply headers look like call
1367c478bd9Sstevel@tonic-gate  * headers except for the direction value.
137605445d5Sdg  * OP_OFFSET_ETHERTYPE sets base according to the following
138605445d5Sdg  * algorithm:
139605445d5Sdg  *   if the packet is not VLAN tagged, then set base to
140605445d5Sdg  *         the ethertype field in the ethernet header
141605445d5Sdg  *   else set base to the ethertype field of the VLAN header
1427c478bd9Sstevel@tonic-gate  * OP_OFFSET_POP restores the offset base to the value prior
1437c478bd9Sstevel@tonic-gate  * to the most recent OP_OFFSET call.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate enum optype {
1467c478bd9Sstevel@tonic-gate 	OP_STOP = 0,
1477c478bd9Sstevel@tonic-gate 	OP_LOAD_OCTET,
1487c478bd9Sstevel@tonic-gate 	OP_LOAD_SHORT,
1497c478bd9Sstevel@tonic-gate 	OP_LOAD_LONG,
1507c478bd9Sstevel@tonic-gate 	OP_LOAD_CONST,
1517c478bd9Sstevel@tonic-gate 	OP_LOAD_LENGTH,
1527c478bd9Sstevel@tonic-gate 	OP_EQ,
1537c478bd9Sstevel@tonic-gate 	OP_NE,
1547c478bd9Sstevel@tonic-gate 	OP_GT,
1557c478bd9Sstevel@tonic-gate 	OP_GE,
1567c478bd9Sstevel@tonic-gate 	OP_LT,
1577c478bd9Sstevel@tonic-gate 	OP_LE,
1587c478bd9Sstevel@tonic-gate 	OP_AND,
1597c478bd9Sstevel@tonic-gate 	OP_OR,
1607c478bd9Sstevel@tonic-gate 	OP_XOR,
1617c478bd9Sstevel@tonic-gate 	OP_NOT,
1627c478bd9Sstevel@tonic-gate 	OP_BRFL,
1637c478bd9Sstevel@tonic-gate 	OP_BRTR,
1647c478bd9Sstevel@tonic-gate 	OP_ADD,
1657c478bd9Sstevel@tonic-gate 	OP_SUB,
1667c478bd9Sstevel@tonic-gate 	OP_MUL,
1677c478bd9Sstevel@tonic-gate 	OP_DIV,
1687c478bd9Sstevel@tonic-gate 	OP_REM,
1697c478bd9Sstevel@tonic-gate 	OP_OFFSET_POP,
1707c478bd9Sstevel@tonic-gate 	OP_OFFSET_ZERO,
1717c478bd9Sstevel@tonic-gate 	OP_OFFSET_LINK,
1727c478bd9Sstevel@tonic-gate 	OP_OFFSET_IP,
1737c478bd9Sstevel@tonic-gate 	OP_OFFSET_TCP,
1747c478bd9Sstevel@tonic-gate 	OP_OFFSET_UDP,
1757c478bd9Sstevel@tonic-gate 	OP_OFFSET_RPC,
1767c478bd9Sstevel@tonic-gate 	OP_OFFSET_SLP,
177605445d5Sdg 	OP_OFFSET_ETHERTYPE,
1787c478bd9Sstevel@tonic-gate 	OP_LAST
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static char *opnames[] = {
1827c478bd9Sstevel@tonic-gate 	"STOP",
1837c478bd9Sstevel@tonic-gate 	"LOAD_OCTET",
1847c478bd9Sstevel@tonic-gate 	"LOAD_SHORT",
1857c478bd9Sstevel@tonic-gate 	"LOAD_LONG",
1867c478bd9Sstevel@tonic-gate 	"LOAD_CONST",
1877c478bd9Sstevel@tonic-gate 	"LOAD_LENGTH",
1887c478bd9Sstevel@tonic-gate 	"EQ",
1897c478bd9Sstevel@tonic-gate 	"NE",
1907c478bd9Sstevel@tonic-gate 	"GT",
1917c478bd9Sstevel@tonic-gate 	"GE",
1927c478bd9Sstevel@tonic-gate 	"LT",
1937c478bd9Sstevel@tonic-gate 	"LE",
1947c478bd9Sstevel@tonic-gate 	"AND",
1957c478bd9Sstevel@tonic-gate 	"OR",
1967c478bd9Sstevel@tonic-gate 	"XOR",
1977c478bd9Sstevel@tonic-gate 	"NOT",
1987c478bd9Sstevel@tonic-gate 	"BRFL",
1997c478bd9Sstevel@tonic-gate 	"BRTR",
2007c478bd9Sstevel@tonic-gate 	"ADD",
2017c478bd9Sstevel@tonic-gate 	"SUB",
2027c478bd9Sstevel@tonic-gate 	"MUL",
2037c478bd9Sstevel@tonic-gate 	"DIV",
2047c478bd9Sstevel@tonic-gate 	"REM",
2057c478bd9Sstevel@tonic-gate 	"OFFSET_POP",
2067c478bd9Sstevel@tonic-gate 	"OFFSET_ZERO",
2077c478bd9Sstevel@tonic-gate 	"OFFSET_ETHER",
2087c478bd9Sstevel@tonic-gate 	"OFFSET_IP",
2097c478bd9Sstevel@tonic-gate 	"OFFSET_TCP",
2107c478bd9Sstevel@tonic-gate 	"OFFSET_UDP",
2117c478bd9Sstevel@tonic-gate 	"OFFSET_RPC",
2127c478bd9Sstevel@tonic-gate 	"OP_OFFSET_SLP",
213605445d5Sdg 	"OFFSET_ETHERTYPE",
2147c478bd9Sstevel@tonic-gate 	""
2157c478bd9Sstevel@tonic-gate };
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #define	MAXOPS 1024
2187c478bd9Sstevel@tonic-gate #define	MAXSS	64
2197c478bd9Sstevel@tonic-gate static uint_t oplist[MAXOPS];	/* array of operators */
2207c478bd9Sstevel@tonic-gate static uint_t *curr_op;		/* last op generated */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate extern int valid_slp(uchar_t *, int);	/* decides if a SLP msg is valid */
2237c478bd9Sstevel@tonic-gate extern struct hostent *lgetipnodebyname(const char *, int, int, int *);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static void alternation();
2267c478bd9Sstevel@tonic-gate static uint_t chain();
2277c478bd9Sstevel@tonic-gate static void codeprint();
2287c478bd9Sstevel@tonic-gate static void emitop();
2297c478bd9Sstevel@tonic-gate static void emitval();
2307c478bd9Sstevel@tonic-gate static void expression();
2317c478bd9Sstevel@tonic-gate static struct xid_entry *find_rpc();
2327c478bd9Sstevel@tonic-gate static void optimize();
2337c478bd9Sstevel@tonic-gate static void ethertype_match();
2347c478bd9Sstevel@tonic-gate 
235605445d5Sdg /*
236605445d5Sdg  * Get a ushort from a possibly unaligned character buffer.
237605445d5Sdg  *
238605445d5Sdg  * INPUTS:  buffer - where the data is.  Must be at least
239605445d5Sdg  *          sizeof(uint16_t) bytes long.
240605445d5Sdg  * OUPUTS:  An unsigned short that contains the data at buffer.
241605445d5Sdg  *          No calls to ntohs or htons are done on the data.
242605445d5Sdg  */
243605445d5Sdg static uint16_t
244605445d5Sdg get_u16(uchar_t *buffer)
245605445d5Sdg {
246605445d5Sdg 	uint8_t	*bufraw = buffer;
247605445d5Sdg 
248605445d5Sdg 	/*
249605445d5Sdg 	 * ntohs is used only as a cheap way to flip the bits
250605445d5Sdg 	 * around on a little endian platform.  The value will
251605445d5Sdg 	 * still be in host order or network order, depending on
252605445d5Sdg 	 * the order it was in when it was passed in.
253605445d5Sdg 	 */
254605445d5Sdg 	return (ntohs(bufraw[0] << 8 | bufraw[1]));
255605445d5Sdg }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Returns the ULP for an IPv4 or IPv6 packet
2597c478bd9Sstevel@tonic-gate  * Assumes that the packet has already been checked to verify
2607c478bd9Sstevel@tonic-gate  * that it's either IPv4 or IPv6
2617c478bd9Sstevel@tonic-gate  *
2627c478bd9Sstevel@tonic-gate  * XXX Will need to be updated for AH and ESP
2637c478bd9Sstevel@tonic-gate  * XXX when IPsec is supported for v6.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate static uchar_t
2667c478bd9Sstevel@tonic-gate ip_proto_of(uchar_t *ip)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	uchar_t		nxt;
2697c478bd9Sstevel@tonic-gate 	boolean_t	not_done = B_TRUE;
2707c478bd9Sstevel@tonic-gate 	uchar_t		*ptr = ip;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	switch (IP_VERS(ip)) {
2737c478bd9Sstevel@tonic-gate 	case IPV4_VERSION:
2747c478bd9Sstevel@tonic-gate 		return (IP_PROTO_OF(ip));
2757c478bd9Sstevel@tonic-gate 	case IPV6_VERSION:
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		nxt = ip[6];
2787c478bd9Sstevel@tonic-gate 		ptr += 40;		/* size of ip6 header */
2797c478bd9Sstevel@tonic-gate 		do {
2807c478bd9Sstevel@tonic-gate 			switch (nxt) {
2817c478bd9Sstevel@tonic-gate 			/*
2827c478bd9Sstevel@tonic-gate 			 * XXX Add IPsec headers here when supported for v6
2837c478bd9Sstevel@tonic-gate 			 * XXX (the AH will have a different size...)
2847c478bd9Sstevel@tonic-gate 			 */
2857c478bd9Sstevel@tonic-gate 			case IPPROTO_HOPOPTS:
2867c478bd9Sstevel@tonic-gate 			case IPPROTO_ROUTING:
2877c478bd9Sstevel@tonic-gate 			case IPPROTO_FRAGMENT:
2887c478bd9Sstevel@tonic-gate 			case IPPROTO_DSTOPTS:
2897c478bd9Sstevel@tonic-gate 				ptr += (8 * (ptr[1] + 1));
2907c478bd9Sstevel@tonic-gate 				nxt = *ptr;
2917c478bd9Sstevel@tonic-gate 				break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			default:
2947c478bd9Sstevel@tonic-gate 				not_done = B_FALSE;
2957c478bd9Sstevel@tonic-gate 				break;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 		} while (not_done);
2987c478bd9Sstevel@tonic-gate 		return (nxt);
2997c478bd9Sstevel@tonic-gate 	default:
3007c478bd9Sstevel@tonic-gate 		break;			/* shouldn't get here... */
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	return (0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * Returns the total IP header length.
3077c478bd9Sstevel@tonic-gate  * For v4, this includes any options present.
3087c478bd9Sstevel@tonic-gate  * For v6, this is the length of the IPv6 header plus
3097c478bd9Sstevel@tonic-gate  * any extension headers present.
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  * XXX Will need to be updated for AH and ESP
3127c478bd9Sstevel@tonic-gate  * XXX when IPsec is supported for v6.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate static int
3157c478bd9Sstevel@tonic-gate ip_hdr_len(uchar_t *ip)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	uchar_t		nxt;
3187c478bd9Sstevel@tonic-gate 	int		hdr_len;
3197c478bd9Sstevel@tonic-gate 	boolean_t	not_done = B_TRUE;
3207c478bd9Sstevel@tonic-gate 	int		len = 40;	/* IPv6 header size */
3217c478bd9Sstevel@tonic-gate 	uchar_t		*ptr = ip;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	switch (IP_VERS(ip)) {
3247c478bd9Sstevel@tonic-gate 	case IPV4_VERSION:
3257c478bd9Sstevel@tonic-gate 		return (IP_HDR_LEN(ip));
3267c478bd9Sstevel@tonic-gate 	case IPV6_VERSION:
3277c478bd9Sstevel@tonic-gate 		nxt = ip[6];
3287c478bd9Sstevel@tonic-gate 		ptr += len;
3297c478bd9Sstevel@tonic-gate 		do {
3307c478bd9Sstevel@tonic-gate 			switch (nxt) {
3317c478bd9Sstevel@tonic-gate 			/*
3327c478bd9Sstevel@tonic-gate 			 * XXX Add IPsec headers here when supported for v6
3337c478bd9Sstevel@tonic-gate 			 * XXX (the AH will have a different size...)
3347c478bd9Sstevel@tonic-gate 			 */
3357c478bd9Sstevel@tonic-gate 			case IPPROTO_HOPOPTS:
3367c478bd9Sstevel@tonic-gate 			case IPPROTO_ROUTING:
3377c478bd9Sstevel@tonic-gate 			case IPPROTO_FRAGMENT:
3387c478bd9Sstevel@tonic-gate 			case IPPROTO_DSTOPTS:
3397c478bd9Sstevel@tonic-gate 				hdr_len = (8 * (ptr[1] + 1));
3407c478bd9Sstevel@tonic-gate 				len += hdr_len;
3417c478bd9Sstevel@tonic-gate 				ptr += hdr_len;
3427c478bd9Sstevel@tonic-gate 				nxt = *ptr;
3437c478bd9Sstevel@tonic-gate 				break;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 			default:
3467c478bd9Sstevel@tonic-gate 				not_done = B_FALSE;
3477c478bd9Sstevel@tonic-gate 				break;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 		} while (not_done);
3507c478bd9Sstevel@tonic-gate 		return (len);
3517c478bd9Sstevel@tonic-gate 	default:
3527c478bd9Sstevel@tonic-gate 		break;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	return (0);			/* not IP */
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static void
3587c478bd9Sstevel@tonic-gate codeprint()
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	uint_t *op;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	printf("User filter:\n");
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	for (op = oplist; *op; op++) {
3657c478bd9Sstevel@tonic-gate 		if (*op <= OP_LAST)
3667c478bd9Sstevel@tonic-gate 			printf("\t%2d: %s\n", op - oplist, opnames[*op]);
3677c478bd9Sstevel@tonic-gate 		else
3687c478bd9Sstevel@tonic-gate 			printf("\t%2d: (%d)\n", op - oplist, *op);
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		switch (*op) {
3717c478bd9Sstevel@tonic-gate 		case OP_LOAD_CONST:
3727c478bd9Sstevel@tonic-gate 		case OP_BRTR:
3737c478bd9Sstevel@tonic-gate 		case OP_BRFL:
3747c478bd9Sstevel@tonic-gate 			op++;
3757c478bd9Sstevel@tonic-gate 			if ((int)*op < 0)
3767c478bd9Sstevel@tonic-gate 				printf("\t%2d:   0x%08x (%d)\n",
377b127ac41SPhilip Kirk 				    op - oplist, *op, *op);
3787c478bd9Sstevel@tonic-gate 			else
3797c478bd9Sstevel@tonic-gate 				printf("\t%2d:   %d (0x%08x)\n",
380b127ac41SPhilip Kirk 				    op - oplist, *op, *op);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	printf("\t%2d: STOP\n", op - oplist);
3847c478bd9Sstevel@tonic-gate 	printf("\n");
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Take a pass through the generated code and optimize
3907c478bd9Sstevel@tonic-gate  * branches.  A branch true (BRTR) that has another BRTR
3917c478bd9Sstevel@tonic-gate  * at its destination can use the address of the destination
3927c478bd9Sstevel@tonic-gate  * BRTR.  A BRTR that points to a BRFL (branch false) should
3937c478bd9Sstevel@tonic-gate  * point to the address following the BRFL.
3947c478bd9Sstevel@tonic-gate  * A similar optimization applies to BRFL operators.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate static void
3977c478bd9Sstevel@tonic-gate optimize(uint_t *oplistp)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	uint_t *op;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	for (op = oplistp; *op; op++) {
4027c478bd9Sstevel@tonic-gate 		switch (*op) {
4037c478bd9Sstevel@tonic-gate 		case OP_LOAD_CONST:
4047c478bd9Sstevel@tonic-gate 			op++;
4057c478bd9Sstevel@tonic-gate 			break;
4067c478bd9Sstevel@tonic-gate 		case OP_BRTR:
4077c478bd9Sstevel@tonic-gate 			op++;
4087c478bd9Sstevel@tonic-gate 			optimize(&oplist[*op]);
4097c478bd9Sstevel@tonic-gate 			if (oplist[*op] == OP_BRFL)
4107c478bd9Sstevel@tonic-gate 				*op += 2;
4117c478bd9Sstevel@tonic-gate 			else if (oplist[*op] == OP_BRTR)
4127c478bd9Sstevel@tonic-gate 				*op = oplist[*op + 1];
4137c478bd9Sstevel@tonic-gate 			break;
4147c478bd9Sstevel@tonic-gate 		case OP_BRFL:
4157c478bd9Sstevel@tonic-gate 			op++;
4167c478bd9Sstevel@tonic-gate 			optimize(&oplist[*op]);
4177c478bd9Sstevel@tonic-gate 			if (oplist[*op] == OP_BRTR)
4187c478bd9Sstevel@tonic-gate 				*op += 2;
4197c478bd9Sstevel@tonic-gate 			else if (oplist[*op] == OP_BRFL)
4207c478bd9Sstevel@tonic-gate 				*op = oplist[*op + 1];
4217c478bd9Sstevel@tonic-gate 			break;
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * RPC packets are tough to filter.
4287c478bd9Sstevel@tonic-gate  * While the call packet has all the interesting
4297c478bd9Sstevel@tonic-gate  * info: program number, version, procedure etc,
4307c478bd9Sstevel@tonic-gate  * the reply packet has none of this information.
4317c478bd9Sstevel@tonic-gate  * If we want to do useful filtering based on this
4327c478bd9Sstevel@tonic-gate  * information then we have to stash the information
4337c478bd9Sstevel@tonic-gate  * from the call packet, and use the XID in the reply
4347c478bd9Sstevel@tonic-gate  * to find the stashed info.  The stashed info is
4357c478bd9Sstevel@tonic-gate  * kept in a circular lifo, assuming that a call packet
4367c478bd9Sstevel@tonic-gate  * will be followed quickly by its reply.
4377c478bd9Sstevel@tonic-gate  */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate struct xid_entry {
4407c478bd9Sstevel@tonic-gate 	unsigned	x_xid;		/* The XID (32 bits) */
4417c478bd9Sstevel@tonic-gate 	unsigned	x_dir;		/* CALL or REPLY */
4427c478bd9Sstevel@tonic-gate 	unsigned	x_rpcvers;	/* Protocol version (2) */
4437c478bd9Sstevel@tonic-gate 	unsigned	x_prog;		/* RPC program number */
4447c478bd9Sstevel@tonic-gate 	unsigned	x_vers;		/* RPC version number */
4457c478bd9Sstevel@tonic-gate 	unsigned	x_proc;		/* RPC procedure number */
4467c478bd9Sstevel@tonic-gate };
4477c478bd9Sstevel@tonic-gate static struct xid_entry	xe_table[XID_CACHE_SIZE];
4487c478bd9Sstevel@tonic-gate static struct xid_entry	*xe_first = &xe_table[0];
4497c478bd9Sstevel@tonic-gate static struct xid_entry	*xe	  = &xe_table[0];
4507c478bd9Sstevel@tonic-gate static struct xid_entry	*xe_last  = &xe_table[XID_CACHE_SIZE - 1];
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate static struct xid_entry *
4537c478bd9Sstevel@tonic-gate find_rpc(struct rpc_msg *rpc)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	struct xid_entry *x;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	for (x = xe; x >= xe_first; x--)
4587c478bd9Sstevel@tonic-gate 		if (x->x_xid == rpc->rm_xid)
4597c478bd9Sstevel@tonic-gate 			return (x);
4607c478bd9Sstevel@tonic-gate 	for (x = xe_last; x > xe; x--)
4617c478bd9Sstevel@tonic-gate 		if (x->x_xid == rpc->rm_xid)
4627c478bd9Sstevel@tonic-gate 			return (x);
4637c478bd9Sstevel@tonic-gate 	return (NULL);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate static void
4677c478bd9Sstevel@tonic-gate stash_rpc(struct rpc_msg *rpc)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	struct xid_entry *x;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (find_rpc(rpc))
4727c478bd9Sstevel@tonic-gate 		return;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	x = xe++;
4757c478bd9Sstevel@tonic-gate 	if (xe > xe_last)
4767c478bd9Sstevel@tonic-gate 		xe = xe_first;
4777c478bd9Sstevel@tonic-gate 	x->x_xid  = rpc->rm_xid;
4787c478bd9Sstevel@tonic-gate 	x->x_dir  = htonl(REPLY);
4797c478bd9Sstevel@tonic-gate 	x->x_prog = rpc->rm_call.cb_prog;
4807c478bd9Sstevel@tonic-gate 	x->x_vers = rpc->rm_call.cb_vers;
4817c478bd9Sstevel@tonic-gate 	x->x_proc = rpc->rm_call.cb_proc;
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * SLP can multicast requests, and recieve unicast replies in which
4867c478bd9Sstevel@tonic-gate  * neither the source nor destination port is identifiable as a SLP
4877c478bd9Sstevel@tonic-gate  * port. Hence, we need to do as RPC does, and keep track of packets we
4887c478bd9Sstevel@tonic-gate  * are interested in. For SLP, however, we use ports, not XIDs, and
4897c478bd9Sstevel@tonic-gate  * a smaller cache size is more efficient since every incoming packet
4907c478bd9Sstevel@tonic-gate  * needs to be checked.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate #define	SLP_CACHE_SIZE 64
4947c478bd9Sstevel@tonic-gate static uint_t slp_table[SLP_CACHE_SIZE];
4957c478bd9Sstevel@tonic-gate static int slp_index	= 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Returns the index of dport in the table if found, otherwise -1.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static int
5017c478bd9Sstevel@tonic-gate find_slp(uint_t dport) {
5027c478bd9Sstevel@tonic-gate     int i;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate     if (!dport)
5057c478bd9Sstevel@tonic-gate 	return (0);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate     for (i = slp_index; i >= 0; i--)
5087c478bd9Sstevel@tonic-gate 	if (slp_table[i] == dport) {
5097c478bd9Sstevel@tonic-gate 	    return (i);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate     for (i = SLP_CACHE_SIZE - 1; i > slp_index; i--)
5127c478bd9Sstevel@tonic-gate 	if (slp_table[i] == dport) {
5137c478bd9Sstevel@tonic-gate 	    return (i);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate     return (-1);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate static void stash_slp(uint_t sport) {
5197c478bd9Sstevel@tonic-gate     if (slp_table[slp_index - 1] == sport)
5207c478bd9Sstevel@tonic-gate 	/* avoid redundancy due to multicast retransmissions */
5217c478bd9Sstevel@tonic-gate 	return;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate     slp_table[slp_index++] = sport;
5247c478bd9Sstevel@tonic-gate     if (slp_index == SLP_CACHE_SIZE)
5257c478bd9Sstevel@tonic-gate 	slp_index = 0;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * This routine takes a packet and returns true or false
5307c478bd9Sstevel@tonic-gate  * according to whether the filter expression selects it
5317c478bd9Sstevel@tonic-gate  * or not.
5327c478bd9Sstevel@tonic-gate  * We assume here that offsets for short and long values
5337c478bd9Sstevel@tonic-gate  * are even - we may die with an alignment error if the
5347c478bd9Sstevel@tonic-gate  * CPU doesn't support odd addresses.  Note that long
5357c478bd9Sstevel@tonic-gate  * values are loaded as two shorts so that 32 bit word
5367c478bd9Sstevel@tonic-gate  * alignment isn't important.
5377c478bd9Sstevel@tonic-gate  *
5387c478bd9Sstevel@tonic-gate  * IPv6 is a bit stickier to handle than IPv4...
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate int
5427c478bd9Sstevel@tonic-gate want_packet(uchar_t *pkt, int len, int origlen)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	uint_t stack[MAXSS];	/* operand stack */
5457c478bd9Sstevel@tonic-gate 	uint_t *op;		/* current operator */
5467c478bd9Sstevel@tonic-gate 	uint_t *sp;		/* top of operand stack */
5477c478bd9Sstevel@tonic-gate 	uchar_t *base;		/* base for offsets into packet */
5487c478bd9Sstevel@tonic-gate 	uchar_t *ip;		/* addr of IP header, unaligned */
5497c478bd9Sstevel@tonic-gate 	uchar_t *tcp;		/* addr of TCP header, unaligned */
5507c478bd9Sstevel@tonic-gate 	uchar_t *udp;		/* addr of UDP header, unaligned */
5517c478bd9Sstevel@tonic-gate 	struct rpc_msg rpcmsg;	/* addr of RPC header */
5527c478bd9Sstevel@tonic-gate 	struct rpc_msg *rpc;
5537c478bd9Sstevel@tonic-gate 	int newrpc = 0;
5547c478bd9Sstevel@tonic-gate 	uchar_t *slphdr;		/* beginning of SLP header */
5557c478bd9Sstevel@tonic-gate 	uint_t slp_sport, slp_dport;
5567c478bd9Sstevel@tonic-gate 	int off, header_size;
5577c478bd9Sstevel@tonic-gate 	uchar_t *offstack[MAXSS];	/* offset stack */
5587c478bd9Sstevel@tonic-gate 	uchar_t **offp;		/* current offset */
5597c478bd9Sstevel@tonic-gate 	uchar_t *opkt = NULL;
5607c478bd9Sstevel@tonic-gate 	uint_t olen;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	sp = stack;
5637c478bd9Sstevel@tonic-gate 	*sp = 1;
5647c478bd9Sstevel@tonic-gate 	base = pkt;
5657c478bd9Sstevel@tonic-gate 	offp = offstack;
5667c478bd9Sstevel@tonic-gate 
567*2b24ab6bSSebastien Roy 	header_size = (*interface->header_len)((char *)pkt, len);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	for (op = oplist; *op; op++) {
5707c478bd9Sstevel@tonic-gate 		switch ((enum optype) *op) {
5717c478bd9Sstevel@tonic-gate 		case OP_LOAD_OCTET:
5727c478bd9Sstevel@tonic-gate 			if ((base + *sp) > (pkt + len))
5737c478bd9Sstevel@tonic-gate 				return (0); /* packet too short */
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			*sp = *((uchar_t *)(base + *sp));
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		case OP_LOAD_SHORT:
5787c478bd9Sstevel@tonic-gate 			off = *sp;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 			if ((base + off + sizeof (uint16_t) - 1) > (pkt + len))
5817c478bd9Sstevel@tonic-gate 				return (0); /* packet too short */
5827c478bd9Sstevel@tonic-gate 
583605445d5Sdg 			*sp = ntohs(get_u16((uchar_t *)(base + off)));
5847c478bd9Sstevel@tonic-gate 			break;
5857c478bd9Sstevel@tonic-gate 		case OP_LOAD_LONG:
5867c478bd9Sstevel@tonic-gate 			off = *sp;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 			if ((base + off + sizeof (uint32_t) - 1) > (pkt + len))
5897c478bd9Sstevel@tonic-gate 				return (0); /* packet too short */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 			/*
5927c478bd9Sstevel@tonic-gate 			 * Handle 3 possible alignments
5937c478bd9Sstevel@tonic-gate 			 */
5947c478bd9Sstevel@tonic-gate 			switch ((((unsigned)base) + off) % sizeof (uint_t)) {
5957c478bd9Sstevel@tonic-gate 			case 0:
5967c478bd9Sstevel@tonic-gate 				*sp = *(uint_t *)(base + off);
5977c478bd9Sstevel@tonic-gate 				break;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 			case 2:
6007c478bd9Sstevel@tonic-gate 				*((ushort_t *)(sp)) =
601b127ac41SPhilip Kirk 				    *((ushort_t *)(base + off));
6027c478bd9Sstevel@tonic-gate 				*(((ushort_t *)sp) + 1) =
603b127ac41SPhilip Kirk 				    *((ushort_t *)(base + off) + 1);
6047c478bd9Sstevel@tonic-gate 				break;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			case 1:
6077c478bd9Sstevel@tonic-gate 			case 3:
6087c478bd9Sstevel@tonic-gate 				*((uchar_t *)(sp)) =
609b127ac41SPhilip Kirk 				    *((uchar_t *)(base + off));
6107c478bd9Sstevel@tonic-gate 				*(((uchar_t *)sp) + 1) =
611b127ac41SPhilip Kirk 				    *((uchar_t *)(base + off) + 1);
6127c478bd9Sstevel@tonic-gate 				*(((uchar_t *)sp) + 2) =
613b127ac41SPhilip Kirk 				    *((uchar_t *)(base + off) + 2);
6147c478bd9Sstevel@tonic-gate 				*(((uchar_t *)sp) + 3) =
615b127ac41SPhilip Kirk 				    *((uchar_t *)(base + off) + 3);
6167c478bd9Sstevel@tonic-gate 				break;
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			*sp = ntohl(*sp);
6197c478bd9Sstevel@tonic-gate 			break;
6207c478bd9Sstevel@tonic-gate 		case OP_LOAD_CONST:
6217c478bd9Sstevel@tonic-gate 			if (sp >= &stack[MAXSS])
6227c478bd9Sstevel@tonic-gate 				return (0);
6237c478bd9Sstevel@tonic-gate 			*(++sp) = *(++op);
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 		case OP_LOAD_LENGTH:
6267c478bd9Sstevel@tonic-gate 			if (sp >= &stack[MAXSS])
6277c478bd9Sstevel@tonic-gate 				return (0);
6287c478bd9Sstevel@tonic-gate 			*(++sp) = origlen;
6297c478bd9Sstevel@tonic-gate 			break;
6307c478bd9Sstevel@tonic-gate 		case OP_EQ:
6317c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6327c478bd9Sstevel@tonic-gate 				return (0);
6337c478bd9Sstevel@tonic-gate 			sp--;
6347c478bd9Sstevel@tonic-gate 			*sp = *sp == *(sp + 1);
6357c478bd9Sstevel@tonic-gate 			break;
6367c478bd9Sstevel@tonic-gate 		case OP_NE:
6377c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6387c478bd9Sstevel@tonic-gate 				return (0);
6397c478bd9Sstevel@tonic-gate 			sp--;
6407c478bd9Sstevel@tonic-gate 			*sp = *sp != *(sp + 1);
6417c478bd9Sstevel@tonic-gate 			break;
6427c478bd9Sstevel@tonic-gate 		case OP_GT:
6437c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6447c478bd9Sstevel@tonic-gate 				return (0);
6457c478bd9Sstevel@tonic-gate 			sp--;
6467c478bd9Sstevel@tonic-gate 			*sp = *sp > *(sp + 1);
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		case OP_GE:
6497c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6507c478bd9Sstevel@tonic-gate 				return (0);
6517c478bd9Sstevel@tonic-gate 			sp--;
6527c478bd9Sstevel@tonic-gate 			*sp = *sp >= *(sp + 1);
6537c478bd9Sstevel@tonic-gate 			break;
6547c478bd9Sstevel@tonic-gate 		case OP_LT:
6557c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6567c478bd9Sstevel@tonic-gate 				return (0);
6577c478bd9Sstevel@tonic-gate 			sp--;
6587c478bd9Sstevel@tonic-gate 			*sp = *sp < *(sp + 1);
6597c478bd9Sstevel@tonic-gate 			break;
6607c478bd9Sstevel@tonic-gate 		case OP_LE:
6617c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6627c478bd9Sstevel@tonic-gate 				return (0);
6637c478bd9Sstevel@tonic-gate 			sp--;
6647c478bd9Sstevel@tonic-gate 			*sp = *sp <= *(sp + 1);
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		case OP_AND:
6677c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6687c478bd9Sstevel@tonic-gate 				return (0);
6697c478bd9Sstevel@tonic-gate 			sp--;
6707c478bd9Sstevel@tonic-gate 			*sp &= *(sp + 1);
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 		case OP_OR:
6737c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6747c478bd9Sstevel@tonic-gate 				return (0);
6757c478bd9Sstevel@tonic-gate 			sp--;
6767c478bd9Sstevel@tonic-gate 			*sp |= *(sp + 1);
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		case OP_XOR:
6797c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6807c478bd9Sstevel@tonic-gate 				return (0);
6817c478bd9Sstevel@tonic-gate 			sp--;
6827c478bd9Sstevel@tonic-gate 			*sp ^= *(sp + 1);
6837c478bd9Sstevel@tonic-gate 			break;
6847c478bd9Sstevel@tonic-gate 		case OP_NOT:
6857c478bd9Sstevel@tonic-gate 			*sp = !*sp;
6867c478bd9Sstevel@tonic-gate 			break;
6877c478bd9Sstevel@tonic-gate 		case OP_BRFL:
6887c478bd9Sstevel@tonic-gate 			op++;
6897c478bd9Sstevel@tonic-gate 			if (!*sp)
6907c478bd9Sstevel@tonic-gate 				op = &oplist[*op] - 1;
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		case OP_BRTR:
6937c478bd9Sstevel@tonic-gate 			op++;
6947c478bd9Sstevel@tonic-gate 			if (*sp)
6957c478bd9Sstevel@tonic-gate 				op = &oplist[*op] - 1;
6967c478bd9Sstevel@tonic-gate 			break;
6977c478bd9Sstevel@tonic-gate 		case OP_ADD:
6987c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
6997c478bd9Sstevel@tonic-gate 				return (0);
7007c478bd9Sstevel@tonic-gate 			sp--;
7017c478bd9Sstevel@tonic-gate 			*sp += *(sp + 1);
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 		case OP_SUB:
7047c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
7057c478bd9Sstevel@tonic-gate 				return (0);
7067c478bd9Sstevel@tonic-gate 			sp--;
7077c478bd9Sstevel@tonic-gate 			*sp -= *(sp + 1);
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		case OP_MUL:
7107c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
7117c478bd9Sstevel@tonic-gate 				return (0);
7127c478bd9Sstevel@tonic-gate 			sp--;
7137c478bd9Sstevel@tonic-gate 			*sp *= *(sp + 1);
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 		case OP_DIV:
7167c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
7177c478bd9Sstevel@tonic-gate 				return (0);
7187c478bd9Sstevel@tonic-gate 			sp--;
7197c478bd9Sstevel@tonic-gate 			*sp /= *(sp + 1);
7207c478bd9Sstevel@tonic-gate 			break;
7217c478bd9Sstevel@tonic-gate 		case OP_REM:
7227c478bd9Sstevel@tonic-gate 			if (sp < &stack[1])
7237c478bd9Sstevel@tonic-gate 				return (0);
7247c478bd9Sstevel@tonic-gate 			sp--;
7257c478bd9Sstevel@tonic-gate 			*sp %= *(sp + 1);
7267c478bd9Sstevel@tonic-gate 			break;
7277c478bd9Sstevel@tonic-gate 		case OP_OFFSET_POP:
7287c478bd9Sstevel@tonic-gate 			if (offp < &offstack[0])
7297c478bd9Sstevel@tonic-gate 				return (0);
7307c478bd9Sstevel@tonic-gate 			base = *offp--;
7317c478bd9Sstevel@tonic-gate 			if (opkt != NULL) {
7327c478bd9Sstevel@tonic-gate 				pkt = opkt;
7337c478bd9Sstevel@tonic-gate 				len = olen;
7347c478bd9Sstevel@tonic-gate 				opkt = NULL;
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 		case OP_OFFSET_ZERO:
7387c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
7397c478bd9Sstevel@tonic-gate 				return (0);
7407c478bd9Sstevel@tonic-gate 			*++offp = base;
7417c478bd9Sstevel@tonic-gate 			base = pkt;
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 		case OP_OFFSET_LINK:
7447c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
7457c478bd9Sstevel@tonic-gate 				return (0);
7467c478bd9Sstevel@tonic-gate 			*++offp = base;
7477c478bd9Sstevel@tonic-gate 			base = pkt + header_size;
7487c478bd9Sstevel@tonic-gate 			/*
7497c478bd9Sstevel@tonic-gate 			 * If the offset exceeds the packet length,
7507c478bd9Sstevel@tonic-gate 			 * we should not be interested in this packet...
7517c478bd9Sstevel@tonic-gate 			 * Just return 0.
7527c478bd9Sstevel@tonic-gate 			 */
7537c478bd9Sstevel@tonic-gate 			if (base > pkt + len) {
7547c478bd9Sstevel@tonic-gate 				return (0);
7557c478bd9Sstevel@tonic-gate 			}
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 		case OP_OFFSET_IP:
7587c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
7597c478bd9Sstevel@tonic-gate 				return (0);
7607c478bd9Sstevel@tonic-gate 			*++offp = base;
7617c478bd9Sstevel@tonic-gate 			ip = pkt + header_size;
7627c478bd9Sstevel@tonic-gate 			base = ip + ip_hdr_len(ip);
7637c478bd9Sstevel@tonic-gate 			if (base == ip) {
7647c478bd9Sstevel@tonic-gate 				return (0);			/* not IP */
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 			if (base > pkt + len) {
7677c478bd9Sstevel@tonic-gate 				return (0);			/* bad pkt */
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 		case OP_OFFSET_TCP:
7717c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
7727c478bd9Sstevel@tonic-gate 				return (0);
7737c478bd9Sstevel@tonic-gate 			*++offp = base;
7747c478bd9Sstevel@tonic-gate 			ip = pkt + header_size;
7757c478bd9Sstevel@tonic-gate 			tcp = ip + ip_hdr_len(ip);
7767c478bd9Sstevel@tonic-gate 			if (tcp == ip) {
7777c478bd9Sstevel@tonic-gate 				return (0);			    /* not IP */
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 			base = tcp + TCP_HDR_LEN(tcp);
7807c478bd9Sstevel@tonic-gate 			if (base > pkt + len) {
7817c478bd9Sstevel@tonic-gate 				return (0);
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 			break;
7847c478bd9Sstevel@tonic-gate 		case OP_OFFSET_UDP:
7857c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
7867c478bd9Sstevel@tonic-gate 				return (0);
7877c478bd9Sstevel@tonic-gate 			*++offp = base;
7887c478bd9Sstevel@tonic-gate 			ip = pkt + header_size;
7897c478bd9Sstevel@tonic-gate 			udp = ip + ip_hdr_len(ip);
7907c478bd9Sstevel@tonic-gate 			if (udp == ip) {
7917c478bd9Sstevel@tonic-gate 				return (0);			    /* not IP */
7927c478bd9Sstevel@tonic-gate 			}
7937c478bd9Sstevel@tonic-gate 			base = udp + sizeof (struct udphdr);
7947c478bd9Sstevel@tonic-gate 			if (base > pkt + len) {
7957c478bd9Sstevel@tonic-gate 				return (0);
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 		case OP_OFFSET_RPC:
7997c478bd9Sstevel@tonic-gate 			if (offp >= &offstack[MAXSS])
8007c478bd9Sstevel@tonic-gate 				return (0);
8017c478bd9Sstevel@tonic-gate 			*++offp = base;
8027c478bd9Sstevel@tonic-gate 			ip = pkt + header_size;
8037c478bd9Sstevel@tonic-gate 			rpc = NULL;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 			if (IP_VERS(ip) != IPV4_VERSION &&
8067c478bd9Sstevel@tonic-gate 			    IP_VERS(ip) != IPV6_VERSION) {
8077c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8087c478bd9Sstevel@tonic-gate 					return (0);
8097c478bd9Sstevel@tonic-gate 				*(++sp) = 0;
8107c478bd9Sstevel@tonic-gate 				break;
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 			switch (ip_proto_of(ip)) {
8147c478bd9Sstevel@tonic-gate 			case IPPROTO_UDP:
8157c478bd9Sstevel@tonic-gate 				udp = ip + ip_hdr_len(ip);
8167c478bd9Sstevel@tonic-gate 				rpc = (struct rpc_msg *)(udp +
8177c478bd9Sstevel@tonic-gate 				    sizeof (struct udphdr));
8187c478bd9Sstevel@tonic-gate 				break;
8197c478bd9Sstevel@tonic-gate 			case IPPROTO_TCP:
8207c478bd9Sstevel@tonic-gate 				tcp = ip + ip_hdr_len(ip);
8217c478bd9Sstevel@tonic-gate 				/*
8227c478bd9Sstevel@tonic-gate 				 * Need to skip an extra 4 for the xdr_rec
8237c478bd9Sstevel@tonic-gate 				 * field.
8247c478bd9Sstevel@tonic-gate 				 */
8257c478bd9Sstevel@tonic-gate 				rpc = (struct rpc_msg *)(tcp +
8267c478bd9Sstevel@tonic-gate 				    TCP_HDR_LEN(tcp) + 4);
8277c478bd9Sstevel@tonic-gate 				break;
8287c478bd9Sstevel@tonic-gate 			}
8297c478bd9Sstevel@tonic-gate 			/*
8307c478bd9Sstevel@tonic-gate 			 * We need to have at least 24 bytes of a RPC
8317c478bd9Sstevel@tonic-gate 			 * packet to look at to determine the validity
8327c478bd9Sstevel@tonic-gate 			 * of it.
8337c478bd9Sstevel@tonic-gate 			 */
8347c478bd9Sstevel@tonic-gate 			if (rpc == NULL || (uchar_t *)rpc + 24 > pkt + len) {
8357c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8367c478bd9Sstevel@tonic-gate 					return (0);
8377c478bd9Sstevel@tonic-gate 				*(++sp) = 0;
8387c478bd9Sstevel@tonic-gate 				break;
8397c478bd9Sstevel@tonic-gate 			}
8407c478bd9Sstevel@tonic-gate 			/* align */
8417c478bd9Sstevel@tonic-gate 			(void) memcpy(&rpcmsg, rpc, 24);
842*2b24ab6bSSebastien Roy 			if (!valid_rpc((char *)&rpcmsg, 24)) {
8437c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8447c478bd9Sstevel@tonic-gate 					return (0);
8457c478bd9Sstevel@tonic-gate 				*(++sp) = 0;
8467c478bd9Sstevel@tonic-gate 				break;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			if (ntohl(rpcmsg.rm_direction) == CALL) {
8497c478bd9Sstevel@tonic-gate 				base = (uchar_t *)rpc;
8507c478bd9Sstevel@tonic-gate 				newrpc = 1;
8517c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8527c478bd9Sstevel@tonic-gate 					return (0);
8537c478bd9Sstevel@tonic-gate 				*(++sp) = 1;
8547c478bd9Sstevel@tonic-gate 			} else {
8557c478bd9Sstevel@tonic-gate 				opkt = pkt;
8567c478bd9Sstevel@tonic-gate 				olen = len;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 				pkt = base = (uchar_t *)find_rpc(&rpcmsg);
8597c478bd9Sstevel@tonic-gate 				len = sizeof (struct xid_entry);
8607c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8617c478bd9Sstevel@tonic-gate 					return (0);
8627c478bd9Sstevel@tonic-gate 				*(++sp) = base != NULL;
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 			break;
8657c478bd9Sstevel@tonic-gate 		case OP_OFFSET_SLP:
8667c478bd9Sstevel@tonic-gate 			slphdr = NULL;
8677c478bd9Sstevel@tonic-gate 			ip = pkt + header_size;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 			if (IP_VERS(ip) != IPV4_VERSION &&
8707c478bd9Sstevel@tonic-gate 			    IP_VERS(ip) != IPV6_VERSION) {
8717c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8727c478bd9Sstevel@tonic-gate 					return (0);
8737c478bd9Sstevel@tonic-gate 				*(++sp) = 0;
8747c478bd9Sstevel@tonic-gate 				break;
8757c478bd9Sstevel@tonic-gate 			}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 			switch (ip_proto_of(ip)) {
8787c478bd9Sstevel@tonic-gate 				struct udphdr udp_h;
8797c478bd9Sstevel@tonic-gate 				struct tcphdr tcp_h;
8807c478bd9Sstevel@tonic-gate 			case IPPROTO_UDP:
8817c478bd9Sstevel@tonic-gate 				udp = ip + ip_hdr_len(ip);
8827c478bd9Sstevel@tonic-gate 				/* align */
8837c478bd9Sstevel@tonic-gate 				memcpy(&udp_h, udp, sizeof (udp_h));
8847c478bd9Sstevel@tonic-gate 				slp_sport = ntohs(udp_h.uh_sport);
8857c478bd9Sstevel@tonic-gate 				slp_dport = ntohs(udp_h.uh_dport);
8867c478bd9Sstevel@tonic-gate 				slphdr = udp + sizeof (struct udphdr);
8877c478bd9Sstevel@tonic-gate 				break;
8887c478bd9Sstevel@tonic-gate 			case IPPROTO_TCP:
8897c478bd9Sstevel@tonic-gate 				tcp = ip + ip_hdr_len(ip);
8907c478bd9Sstevel@tonic-gate 				/* align */
8917c478bd9Sstevel@tonic-gate 				memcpy(&tcp_h, tcp, sizeof (tcp_h));
8927c478bd9Sstevel@tonic-gate 				slp_sport = ntohs(tcp_h.th_sport);
8937c478bd9Sstevel@tonic-gate 				slp_dport = ntohs(tcp_h.th_dport);
8947c478bd9Sstevel@tonic-gate 				slphdr = tcp + TCP_HDR_LEN(tcp);
8957c478bd9Sstevel@tonic-gate 				break;
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 			if (slphdr == NULL || slphdr > pkt + len) {
8987c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
8997c478bd9Sstevel@tonic-gate 					return (0);
9007c478bd9Sstevel@tonic-gate 				*(++sp) = 0;
9017c478bd9Sstevel@tonic-gate 				break;
9027c478bd9Sstevel@tonic-gate 			}
9037c478bd9Sstevel@tonic-gate 			if (slp_sport == 427 || slp_dport == 427) {
9047c478bd9Sstevel@tonic-gate 				if (sp >= &stack[MAXSS])
9057c478bd9Sstevel@tonic-gate 					return (0);
9067c478bd9Sstevel@tonic-gate 				*(++sp) = 1;
9077c478bd9Sstevel@tonic-gate 				if (slp_sport != 427 && slp_dport == 427)
9087c478bd9Sstevel@tonic-gate 					stash_slp(slp_sport);
9097c478bd9Sstevel@tonic-gate 				break;
9107c478bd9Sstevel@tonic-gate 			} else if (find_slp(slp_dport) != -1) {
9117c478bd9Sstevel@tonic-gate 				if (valid_slp(slphdr, len)) {
9127c478bd9Sstevel@tonic-gate 					if (sp >= &stack[MAXSS])
9137c478bd9Sstevel@tonic-gate 						return (0);
9147c478bd9Sstevel@tonic-gate 					*(++sp) = 1;
9157c478bd9Sstevel@tonic-gate 					break;
9167c478bd9Sstevel@tonic-gate 				}
9177c478bd9Sstevel@tonic-gate 				/* else fallthrough to reject */
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 			if (sp >= &stack[MAXSS])
9207c478bd9Sstevel@tonic-gate 				return (0);
9217c478bd9Sstevel@tonic-gate 			*(++sp) = 0;
9227c478bd9Sstevel@tonic-gate 			break;
923605445d5Sdg 		case OP_OFFSET_ETHERTYPE:
924605445d5Sdg 			/*
925b127ac41SPhilip Kirk 			 * Set base to the location of the ethertype as
926b127ac41SPhilip Kirk 			 * appropriate for this link type.  Note that it's
927b127ac41SPhilip Kirk 			 * not called "ethertype" for every link type, but
928b127ac41SPhilip Kirk 			 * we need to call it something.
929605445d5Sdg 			 */
930605445d5Sdg 			if (offp >= &offstack[MAXSS])
931605445d5Sdg 				return (0);
932605445d5Sdg 			*++offp = base;
933605445d5Sdg 			base = pkt + interface->network_type_offset;
934605445d5Sdg 
935605445d5Sdg 			/*
936b127ac41SPhilip Kirk 			 * Below, we adjust the offset for unusual
937b127ac41SPhilip Kirk 			 * link-layer headers that may have the protocol
938b127ac41SPhilip Kirk 			 * type in a variable location beyond what was set
939b127ac41SPhilip Kirk 			 * above.
940605445d5Sdg 			 */
941b127ac41SPhilip Kirk 			switch (interface->mac_type) {
942b127ac41SPhilip Kirk 			case DL_ETHER:
943b127ac41SPhilip Kirk 			case DL_CSMACD:
944b127ac41SPhilip Kirk 				/*
945b127ac41SPhilip Kirk 				 * If this is a VLAN-tagged packet, we need
946b127ac41SPhilip Kirk 				 * to point to the ethertype field in the
947b127ac41SPhilip Kirk 				 * VLAN header.  Move past the ethertype
948b127ac41SPhilip Kirk 				 * field in the ethernet header.
949b127ac41SPhilip Kirk 				 */
950b127ac41SPhilip Kirk 				if (ntohs(get_u16(base)) == ETHERTYPE_VLAN)
951605445d5Sdg 					base += (ENCAP_ETHERTYPE_OFF);
952b127ac41SPhilip Kirk 				break;
953b127ac41SPhilip Kirk 			}
954b127ac41SPhilip Kirk 			if (base > pkt + len) {
955b127ac41SPhilip Kirk 				/* Went too far, drop the packet */
956b127ac41SPhilip Kirk 				return (0);
957605445d5Sdg 			}
958605445d5Sdg 			break;
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 	}
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (*sp && newrpc)
9637c478bd9Sstevel@tonic-gate 		stash_rpc(&rpcmsg);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	return (*sp);
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate static void
9697c478bd9Sstevel@tonic-gate load_const(uint_t constval)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate 	emitop(OP_LOAD_CONST);
9727c478bd9Sstevel@tonic-gate 	emitval(constval);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static void
9767c478bd9Sstevel@tonic-gate load_value(int offset, int len)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	if (offset >= 0)
9797c478bd9Sstevel@tonic-gate 		load_const(offset);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	switch (len) {
9827c478bd9Sstevel@tonic-gate 		case 1:
9837c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_OCTET);
9847c478bd9Sstevel@tonic-gate 			break;
9857c478bd9Sstevel@tonic-gate 		case 2:
9867c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_SHORT);
9877c478bd9Sstevel@tonic-gate 			break;
9887c478bd9Sstevel@tonic-gate 		case 4:
9897c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_LONG);
9907c478bd9Sstevel@tonic-gate 			break;
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * Emit code to compare a field in
9967c478bd9Sstevel@tonic-gate  * the packet against a constant value.
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate static void
9997c478bd9Sstevel@tonic-gate compare_value(uint_t offset, uint_t len, uint_t val)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	load_const(val);
10027c478bd9Sstevel@tonic-gate 	load_value(offset, len);
10037c478bd9Sstevel@tonic-gate 	emitop(OP_EQ);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate static void
10077c478bd9Sstevel@tonic-gate compare_addr_v4(uint_t offset, uint_t len, uint_t val)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	load_const(ntohl(val));
10107c478bd9Sstevel@tonic-gate 	load_value(offset, len);
10117c478bd9Sstevel@tonic-gate 	emitop(OP_EQ);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate static void
10157c478bd9Sstevel@tonic-gate compare_addr_v6(uint_t offset, uint_t len, struct in6_addr val)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	int i;
10187c478bd9Sstevel@tonic-gate 	uint32_t value;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += 4) {
10217c478bd9Sstevel@tonic-gate 		value = ntohl(*(uint32_t *)&val.s6_addr[i]);
10227c478bd9Sstevel@tonic-gate 		load_const(value);
10237c478bd9Sstevel@tonic-gate 		load_value(offset + i, 4);
10247c478bd9Sstevel@tonic-gate 		emitop(OP_EQ);
10257c478bd9Sstevel@tonic-gate 		if (i != 0)
10267c478bd9Sstevel@tonic-gate 			emitop(OP_AND);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate /*
10317c478bd9Sstevel@tonic-gate  * Same as above except do the comparison
10327c478bd9Sstevel@tonic-gate  * after and'ing a mask value.  Useful
10337c478bd9Sstevel@tonic-gate  * for comparing IP network numbers
10347c478bd9Sstevel@tonic-gate  */
10357c478bd9Sstevel@tonic-gate static void
10367c478bd9Sstevel@tonic-gate compare_value_mask(uint_t offset, uint_t len, uint_t val, int mask)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	load_value(offset, len);
10397c478bd9Sstevel@tonic-gate 	load_const(mask);
10407c478bd9Sstevel@tonic-gate 	emitop(OP_AND);
10417c478bd9Sstevel@tonic-gate 	load_const(val);
10427c478bd9Sstevel@tonic-gate 	emitop(OP_EQ);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
1045b127ac41SPhilip Kirk /*
1046b127ac41SPhilip Kirk  * Compare two zoneid's. The arg val passed in is stored in network
1047b127ac41SPhilip Kirk  * byte order.
1048b127ac41SPhilip Kirk  */
1049b127ac41SPhilip Kirk static void
1050b127ac41SPhilip Kirk compare_value_zone(uint_t offset, uint64_t val)
1051b127ac41SPhilip Kirk {
1052b127ac41SPhilip Kirk 	int i;
1053b127ac41SPhilip Kirk 
1054b127ac41SPhilip Kirk 	for (i = 0; i < sizeof (uint64_t) / 4; i++) {
1055b127ac41SPhilip Kirk 		load_const(ntohl(((uint32_t *)&val)[i]));
1056b127ac41SPhilip Kirk 		load_value(offset + i * 4, 4);
1057b127ac41SPhilip Kirk 		emitop(OP_EQ);
1058b127ac41SPhilip Kirk 		if (i != 0)
1059b127ac41SPhilip Kirk 			emitop(OP_AND);
1060b127ac41SPhilip Kirk 	}
1061b127ac41SPhilip Kirk }
1062b127ac41SPhilip Kirk 
10637c478bd9Sstevel@tonic-gate /* Emit an operator into the code array */
10647c478bd9Sstevel@tonic-gate static void
10657c478bd9Sstevel@tonic-gate emitop(enum optype opcode)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	if (curr_op >= &oplist[MAXOPS])
10687c478bd9Sstevel@tonic-gate 		pr_err("expression too long");
10697c478bd9Sstevel@tonic-gate 	*curr_op++ = opcode;
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate /*
10737c478bd9Sstevel@tonic-gate  * Remove n operators recently emitted into
10747c478bd9Sstevel@tonic-gate  * the code array.  Used by alternation().
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate static void
10777c478bd9Sstevel@tonic-gate unemit(int numops)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	curr_op -= numops;
10807c478bd9Sstevel@tonic-gate }
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * Same as emitop except that we're emitting
10857c478bd9Sstevel@tonic-gate  * a value that's not an operator.
10867c478bd9Sstevel@tonic-gate  */
10877c478bd9Sstevel@tonic-gate static void
10887c478bd9Sstevel@tonic-gate emitval(uint_t val)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	if (curr_op >= &oplist[MAXOPS])
10917c478bd9Sstevel@tonic-gate 		pr_err("expression too long");
10927c478bd9Sstevel@tonic-gate 	*curr_op++ = val;
10937c478bd9Sstevel@tonic-gate }
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate /*
10967c478bd9Sstevel@tonic-gate  * Used to chain forward branches together
10977c478bd9Sstevel@tonic-gate  * for later resolution by resolve_chain().
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate static uint_t
11007c478bd9Sstevel@tonic-gate chain(int p)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	uint_t pos = curr_op - oplist;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	emitval(p);
11057c478bd9Sstevel@tonic-gate 	return (pos);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * Proceed backward through the code array
11107c478bd9Sstevel@tonic-gate  * following a chain of forward references.
11117c478bd9Sstevel@tonic-gate  * At each reference install the destination
11127c478bd9Sstevel@tonic-gate  * branch offset.
11137c478bd9Sstevel@tonic-gate  */
11147c478bd9Sstevel@tonic-gate static void
11157c478bd9Sstevel@tonic-gate resolve_chain(uint_t p)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	uint_t n;
11187c478bd9Sstevel@tonic-gate 	uint_t pos = curr_op - oplist;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	while (p) {
11217c478bd9Sstevel@tonic-gate 		n = oplist[p];
11227c478bd9Sstevel@tonic-gate 		oplist[p] = pos;
11237c478bd9Sstevel@tonic-gate 		p = n;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate #define	EQ(val) (strcmp(token, val) == 0)
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate char *tkp, *sav_tkp;
11307c478bd9Sstevel@tonic-gate char *token;
11317c478bd9Sstevel@tonic-gate enum { EOL, ALPHA, NUMBER, FIELD, ADDR_IP, ADDR_ETHER, SPECIAL,
11327c478bd9Sstevel@tonic-gate 	ADDR_IP6, ADDR_AT } tokentype;
11337c478bd9Sstevel@tonic-gate uint_t tokenval;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  * This is the scanner.  Each call returns the next
11377c478bd9Sstevel@tonic-gate  * token in the filter expression.  A token is either:
11387c478bd9Sstevel@tonic-gate  * EOL:		The end of the line - no more tokens.
11397c478bd9Sstevel@tonic-gate  * ALPHA:	A name that begins with a letter and contains
11407c478bd9Sstevel@tonic-gate  *		letters or digits, hyphens or underscores.
11417c478bd9Sstevel@tonic-gate  * NUMBER:	A number.  The value can be represented as
11427c478bd9Sstevel@tonic-gate  * 		a decimal value (1234) or an octal value
11437c478bd9Sstevel@tonic-gate  *		that begins with zero (066) or a hex value
11447c478bd9Sstevel@tonic-gate  *		that begins with 0x or 0X (0xff).
11457c478bd9Sstevel@tonic-gate  * FIELD:	A name followed by a left square bracket.
11467c478bd9Sstevel@tonic-gate  * ADDR_IP:	An IP address.  Any sequence of digits
11477c478bd9Sstevel@tonic-gate  *		separated by dots e.g. 109.104.40.13
11487c478bd9Sstevel@tonic-gate  * ADDR_ETHER:	An ethernet address.  Any sequence of hex
11497c478bd9Sstevel@tonic-gate  *		digits separated by colons e.g. 8:0:20:0:76:39
11507c478bd9Sstevel@tonic-gate  * SPECIAL:	A special character e.g. ">" or "(".  The scanner
11517c478bd9Sstevel@tonic-gate  *		correctly handles digraphs - two special characters
11527c478bd9Sstevel@tonic-gate  *		that constitute a single token e.g. "==" or ">=".
11537c478bd9Sstevel@tonic-gate  * ADDR_IP6:    An IPv6 address.
11547c478bd9Sstevel@tonic-gate  *
11557c478bd9Sstevel@tonic-gate  * ADDR_AT:	An AppleTalk Phase II address. A sequence of two numbers
11567c478bd9Sstevel@tonic-gate  *		separated by a dot.
11577c478bd9Sstevel@tonic-gate  *
11587c478bd9Sstevel@tonic-gate  * The current token is maintained in "token" and and its
11597c478bd9Sstevel@tonic-gate  * type in "tokentype".  If tokentype is NUMBER then the
11607c478bd9Sstevel@tonic-gate  * value is held in "tokenval".
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate static const char *namechars =
11647c478bd9Sstevel@tonic-gate 	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.";
11657c478bd9Sstevel@tonic-gate static const char *numchars = "0123456789abcdefABCDEFXx:.";
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate void
11687c478bd9Sstevel@tonic-gate next()
11697c478bd9Sstevel@tonic-gate {
11707c478bd9Sstevel@tonic-gate 	static int savechar;
11717c478bd9Sstevel@tonic-gate 	char *p;
11727c478bd9Sstevel@tonic-gate 	int size, size1;
11737c478bd9Sstevel@tonic-gate 	int base, colons, dots, alphas, double_colon;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	colons = 0;
11767c478bd9Sstevel@tonic-gate 	double_colon = 0;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (*tkp == '\0') {
11797c478bd9Sstevel@tonic-gate 		token = tkp;
11807c478bd9Sstevel@tonic-gate 		*tkp = savechar;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	sav_tkp = tkp;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	while (isspace(*tkp)) tkp++;
11867c478bd9Sstevel@tonic-gate 	token = tkp;
11877c478bd9Sstevel@tonic-gate 	if (*token == '\0') {
11887c478bd9Sstevel@tonic-gate 		tokentype = EOL;
11897c478bd9Sstevel@tonic-gate 		return;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/* A token containing ':' cannot be ALPHA type */
11937c478bd9Sstevel@tonic-gate 	tkp = token + strspn(token, numchars);
11947c478bd9Sstevel@tonic-gate 	for (p = token; p < tkp; p++) {
11957c478bd9Sstevel@tonic-gate 		if (*p == ':') {
11967c478bd9Sstevel@tonic-gate 			colons++;
11977c478bd9Sstevel@tonic-gate 			if (*(p+1) == ':')
11987c478bd9Sstevel@tonic-gate 				double_colon++;
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	tkp = token;
12037c478bd9Sstevel@tonic-gate 	if (isalpha(*tkp) && !colons) {
12047c478bd9Sstevel@tonic-gate 		tokentype = ALPHA;
12057c478bd9Sstevel@tonic-gate 		tkp += strspn(tkp, namechars);
12067c478bd9Sstevel@tonic-gate 		if (*tkp == '[') {
12077c478bd9Sstevel@tonic-gate 			tokentype = FIELD;
12087c478bd9Sstevel@tonic-gate 			*tkp++ = '\0';
12097c478bd9Sstevel@tonic-gate 		}
12107c478bd9Sstevel@tonic-gate 	} else
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	/*
12137c478bd9Sstevel@tonic-gate 	 * RFC1123 states that host names may now start with digits. Need
12147c478bd9Sstevel@tonic-gate 	 * to change parser to account for this. Also, need to distinguish
12157c478bd9Sstevel@tonic-gate 	 * between 1.2.3.4 and 1.2.3.a where the first case is an IP address
12167c478bd9Sstevel@tonic-gate 	 * and the second is a domain name. 333aaa needs to be distinguished
12177c478bd9Sstevel@tonic-gate 	 * from 0x333aaa. The first is a host name and the second is a number.
12187c478bd9Sstevel@tonic-gate 	 *
12197c478bd9Sstevel@tonic-gate 	 * The (colons > 1) conditional differentiates between ethernet
12207c478bd9Sstevel@tonic-gate 	 * and IPv6 addresses, and an expression of the form base[expr:size],
12217c478bd9Sstevel@tonic-gate 	 * which can only contain one ':' character.
12227c478bd9Sstevel@tonic-gate 	 */
12237c478bd9Sstevel@tonic-gate 	if (isdigit(*tkp) || colons > 1) {
12247c478bd9Sstevel@tonic-gate 		tkp = token + strspn(token, numchars);
12257c478bd9Sstevel@tonic-gate 		dots = alphas = 0;
12267c478bd9Sstevel@tonic-gate 		for (p = token; p < tkp; p++) {
12277c478bd9Sstevel@tonic-gate 			if (*p == '.')
12287c478bd9Sstevel@tonic-gate 				dots++;
12297c478bd9Sstevel@tonic-gate 			else if (isalpha(*p))
12307c478bd9Sstevel@tonic-gate 				alphas = 1;
12317c478bd9Sstevel@tonic-gate 		}
12327c478bd9Sstevel@tonic-gate 		if (colons > 1) {
12337c478bd9Sstevel@tonic-gate 			if (colons == 5 && double_colon == 0) {
12347c478bd9Sstevel@tonic-gate 				tokentype = ADDR_ETHER;
12357c478bd9Sstevel@tonic-gate 			} else {
12367c478bd9Sstevel@tonic-gate 				tokentype = ADDR_IP6;
12377c478bd9Sstevel@tonic-gate 			}
12387c478bd9Sstevel@tonic-gate 		} else if (dots) {
12397c478bd9Sstevel@tonic-gate 			size = tkp - token;
12407c478bd9Sstevel@tonic-gate 			size1 = strspn(token, "0123456789.");
12417c478bd9Sstevel@tonic-gate 			if (dots == 1 && size == size1) {
12427c478bd9Sstevel@tonic-gate 				tokentype = ADDR_AT;
12437c478bd9Sstevel@tonic-gate 			} else
12447c478bd9Sstevel@tonic-gate 				if (dots != 3 || size != size1) {
12457c478bd9Sstevel@tonic-gate 					tokentype = ALPHA;
12467c478bd9Sstevel@tonic-gate 					if (*tkp != '\0' && !isspace(*tkp)) {
12477c478bd9Sstevel@tonic-gate 						tkp += strspn(tkp, namechars);
12487c478bd9Sstevel@tonic-gate 						if (*tkp == '[') {
12497c478bd9Sstevel@tonic-gate 							tokentype = FIELD;
12507c478bd9Sstevel@tonic-gate 							*tkp++ = '\0';
12517c478bd9Sstevel@tonic-gate 						}
12527c478bd9Sstevel@tonic-gate 					}
12537c478bd9Sstevel@tonic-gate 				} else
12547c478bd9Sstevel@tonic-gate 					tokentype = ADDR_IP;
12557c478bd9Sstevel@tonic-gate 		} else if (token + strspn(token, namechars) <= tkp) {
12567c478bd9Sstevel@tonic-gate 			/*
12577c478bd9Sstevel@tonic-gate 			 * With the above check, if there are more
12587c478bd9Sstevel@tonic-gate 			 * characters after the last digit, assume
12597c478bd9Sstevel@tonic-gate 			 * that it is not a number.
12607c478bd9Sstevel@tonic-gate 			 */
12617c478bd9Sstevel@tonic-gate 			tokentype = NUMBER;
12627c478bd9Sstevel@tonic-gate 			p = tkp;
12637c478bd9Sstevel@tonic-gate 			tkp = token;
12647c478bd9Sstevel@tonic-gate 			base = 10;
12657c478bd9Sstevel@tonic-gate 			if (*tkp == '0') {
12667c478bd9Sstevel@tonic-gate 				base = 8;
12677c478bd9Sstevel@tonic-gate 				tkp++;
12687c478bd9Sstevel@tonic-gate 				if (*tkp == 'x' || *tkp == 'X')
12697c478bd9Sstevel@tonic-gate 					base = 16;
12707c478bd9Sstevel@tonic-gate 			}
12717c478bd9Sstevel@tonic-gate 			if ((base == 10 || base == 8) && alphas) {
12727c478bd9Sstevel@tonic-gate 				tokentype = ALPHA;
12737c478bd9Sstevel@tonic-gate 				tkp = p;
12747c478bd9Sstevel@tonic-gate 			} else if (base == 16) {
12757c478bd9Sstevel@tonic-gate 				size = 2 + strspn(token+2,
1276b127ac41SPhilip Kirk 				    "0123456789abcdefABCDEF");
12777c478bd9Sstevel@tonic-gate 				size1 = p - token;
12787c478bd9Sstevel@tonic-gate 				if (size != size1) {
12797c478bd9Sstevel@tonic-gate 					tokentype = ALPHA;
12807c478bd9Sstevel@tonic-gate 					tkp = p;
12817c478bd9Sstevel@tonic-gate 				} else
12827c478bd9Sstevel@tonic-gate 				/*
12837c478bd9Sstevel@tonic-gate 				 * handles the case of 0x so an error message
12847c478bd9Sstevel@tonic-gate 				 * is not printed. Treats 0x as 0.
12857c478bd9Sstevel@tonic-gate 				 */
12867c478bd9Sstevel@tonic-gate 				if (size == 2) {
12877c478bd9Sstevel@tonic-gate 					tokenval = 0;
12887c478bd9Sstevel@tonic-gate 					tkp = token +2;
12897c478bd9Sstevel@tonic-gate 				} else {
12907c478bd9Sstevel@tonic-gate 					tokenval = strtoul(token, &tkp, base);
12917c478bd9Sstevel@tonic-gate 				}
12927c478bd9Sstevel@tonic-gate 			} else {
12937c478bd9Sstevel@tonic-gate 				tokenval = strtoul(token, &tkp, base);
12947c478bd9Sstevel@tonic-gate 			}
12957c478bd9Sstevel@tonic-gate 		} else {
12967c478bd9Sstevel@tonic-gate 			tokentype = ALPHA;
12977c478bd9Sstevel@tonic-gate 			tkp += strspn(tkp, namechars);
12987c478bd9Sstevel@tonic-gate 			if (*tkp == '[') {
12997c478bd9Sstevel@tonic-gate 				tokentype = FIELD;
13007c478bd9Sstevel@tonic-gate 				*tkp++ = '\0';
13017c478bd9Sstevel@tonic-gate 			}
13027c478bd9Sstevel@tonic-gate 		}
13037c478bd9Sstevel@tonic-gate 	} else {
13047c478bd9Sstevel@tonic-gate 		tokentype = SPECIAL;
13057c478bd9Sstevel@tonic-gate 		tkp++;
13067c478bd9Sstevel@tonic-gate 		if ((*token == '=' && *tkp == '=') ||
13077c478bd9Sstevel@tonic-gate 		    (*token == '>' && *tkp == '=') ||
13087c478bd9Sstevel@tonic-gate 		    (*token == '<' && *tkp == '=') ||
13097c478bd9Sstevel@tonic-gate 		    (*token == '!' && *tkp == '='))
13107c478bd9Sstevel@tonic-gate 				tkp++;
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	savechar = *tkp;
13147c478bd9Sstevel@tonic-gate 	*tkp = '\0';
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate 
1317b127ac41SPhilip Kirk typedef struct match_type {
13187c478bd9Sstevel@tonic-gate 	char		*m_name;
13197c478bd9Sstevel@tonic-gate 	int		m_offset;
13207c478bd9Sstevel@tonic-gate 	int		m_size;
13217c478bd9Sstevel@tonic-gate 	int		m_value;
13227c478bd9Sstevel@tonic-gate 	int		m_depend;
13237c478bd9Sstevel@tonic-gate 	enum optype	m_optype;
1324b127ac41SPhilip Kirk } match_type_t;
1325b127ac41SPhilip Kirk 
1326b127ac41SPhilip Kirk static match_type_t ether_match_types[] = {
13277c478bd9Sstevel@tonic-gate 	/*
13287c478bd9Sstevel@tonic-gate 	 * Table initialized assuming Ethernet data link headers.
1329605445d5Sdg 	 * m_offset is an offset beyond the offset op, which is why
1330605445d5Sdg 	 * the offset is zero for when snoop needs to check an ethertype.
13317c478bd9Sstevel@tonic-gate 	 */
1332605445d5Sdg 	"ip",		0,  2, ETHERTYPE_IP,	 -1,	OP_OFFSET_ETHERTYPE,
1333605445d5Sdg 	"ip6",		0,  2, ETHERTYPE_IPV6,	 -1,	OP_OFFSET_ETHERTYPE,
1334605445d5Sdg 	"arp",		0,  2, ETHERTYPE_ARP,	 -1,	OP_OFFSET_ETHERTYPE,
1335605445d5Sdg 	"rarp",		0,  2, ETHERTYPE_REVARP, -1,	OP_OFFSET_ETHERTYPE,
1336605445d5Sdg 	"pppoed",	0,  2, ETHERTYPE_PPPOED, -1,	OP_OFFSET_ETHERTYPE,
1337605445d5Sdg 	"pppoes",	0,  2, ETHERTYPE_PPPOES, -1,	OP_OFFSET_ETHERTYPE,
1338605445d5Sdg 	"tcp",		9,  1, IPPROTO_TCP,	 0,	OP_OFFSET_LINK,
1339605445d5Sdg 	"tcp",		6,  1, IPPROTO_TCP,	 1,	OP_OFFSET_LINK,
1340605445d5Sdg 	"udp",		9,  1, IPPROTO_UDP,	 0,	OP_OFFSET_LINK,
1341605445d5Sdg 	"udp",		6,  1, IPPROTO_UDP,	 1,	OP_OFFSET_LINK,
1342605445d5Sdg 	"icmp",		9,  1, IPPROTO_ICMP,	 0,	OP_OFFSET_LINK,
1343605445d5Sdg 	"icmp6",	6,  1, IPPROTO_ICMPV6,	 1,	OP_OFFSET_LINK,
1344605445d5Sdg 	"ospf",		9,  1, IPPROTO_OSPF,	 0,	OP_OFFSET_LINK,
1345605445d5Sdg 	"ospf",		6,  1, IPPROTO_OSPF,	 1,	OP_OFFSET_LINK,
1346605445d5Sdg 	"ip-in-ip",	9,  1, IPPROTO_ENCAP,	 0,	OP_OFFSET_LINK,
1347605445d5Sdg 	"esp",		9,  1, IPPROTO_ESP,	 0,	OP_OFFSET_LINK,
1348605445d5Sdg 	"esp",		6,  1, IPPROTO_ESP,	 1,	OP_OFFSET_LINK,
1349605445d5Sdg 	"ah",		9,  1, IPPROTO_AH,	 0,	OP_OFFSET_LINK,
1350605445d5Sdg 	"ah",		6,  1, IPPROTO_AH,	 1,	OP_OFFSET_LINK,
1351605445d5Sdg 	"sctp",		9,  1, IPPROTO_SCTP,	 0,	OP_OFFSET_LINK,
1352605445d5Sdg 	"sctp",		6,  1, IPPROTO_SCTP,	 1,	OP_OFFSET_LINK,
1353605445d5Sdg 	0,		0,  0, 0,		 0,	0
13547c478bd9Sstevel@tonic-gate };
13557c478bd9Sstevel@tonic-gate 
1356b127ac41SPhilip Kirk static match_type_t ipnet_match_types[] = {
1357b127ac41SPhilip Kirk 	/*
1358b127ac41SPhilip Kirk 	 * Table initialized assuming Ethernet data link headers.
1359b127ac41SPhilip Kirk 	 * m_offset is an offset beyond the offset op, which is why
1360b127ac41SPhilip Kirk 	 * the offset is zero for when snoop needs to check an ethertype.
1361b127ac41SPhilip Kirk 	 */
1362041bde0aSSebastien Roy 	"ip",		0,  1, IPV4_VERSION,    -1,	OP_OFFSET_ETHERTYPE,
1363041bde0aSSebastien Roy 	"ip6",		0,  1, IPV6_VERSION,    -1,	OP_OFFSET_ETHERTYPE,
1364b127ac41SPhilip Kirk 	"tcp",		9,  1, IPPROTO_TCP,	 0,	OP_OFFSET_LINK,
1365b127ac41SPhilip Kirk 	"tcp",		6,  1, IPPROTO_TCP,	 1,	OP_OFFSET_LINK,
1366b127ac41SPhilip Kirk 	"udp",		9,  1, IPPROTO_UDP,	 0,	OP_OFFSET_LINK,
1367b127ac41SPhilip Kirk 	"udp",		6,  1, IPPROTO_UDP,	 1,	OP_OFFSET_LINK,
1368b127ac41SPhilip Kirk 	"icmp",		9,  1, IPPROTO_ICMP,	 0,	OP_OFFSET_LINK,
1369b127ac41SPhilip Kirk 	"icmp6",	6,  1, IPPROTO_ICMPV6,	 1,	OP_OFFSET_LINK,
1370b127ac41SPhilip Kirk 	"ospf",		9,  1, IPPROTO_OSPF,	 0,	OP_OFFSET_LINK,
1371b127ac41SPhilip Kirk 	"ospf",		6,  1, IPPROTO_OSPF,	 1,	OP_OFFSET_LINK,
1372b127ac41SPhilip Kirk 	"ip-in-ip",	9,  1, IPPROTO_ENCAP,	 0,	OP_OFFSET_LINK,
1373b127ac41SPhilip Kirk 	"esp",		9,  1, IPPROTO_ESP,	 0,	OP_OFFSET_LINK,
1374b127ac41SPhilip Kirk 	"esp",		6,  1, IPPROTO_ESP,	 1,	OP_OFFSET_LINK,
1375b127ac41SPhilip Kirk 	"ah",		9,  1, IPPROTO_AH,	 0,	OP_OFFSET_LINK,
1376b127ac41SPhilip Kirk 	"ah",		6,  1, IPPROTO_AH,	 1,	OP_OFFSET_LINK,
1377b127ac41SPhilip Kirk 	"sctp",		9,  1, IPPROTO_SCTP,	 0,	OP_OFFSET_LINK,
1378b127ac41SPhilip Kirk 	"sctp",		6,  1, IPPROTO_SCTP,	 1,	OP_OFFSET_LINK,
1379b127ac41SPhilip Kirk 	0,		0,  0, 0,		 0,	0
1380b127ac41SPhilip Kirk };
1381b127ac41SPhilip Kirk 
1382*2b24ab6bSSebastien Roy static match_type_t iptun_match_types[] = {
1383*2b24ab6bSSebastien Roy 	"ip",		0,  1, IPPROTO_ENCAP,	-1,	OP_OFFSET_ETHERTYPE,
1384*2b24ab6bSSebastien Roy 	"ip6",		0,  1, IPPROTO_IPV6,	-1,	OP_OFFSET_ETHERTYPE,
1385*2b24ab6bSSebastien Roy 	"tcp",		9,  1, IPPROTO_TCP,	0,	OP_OFFSET_LINK,
1386*2b24ab6bSSebastien Roy 	"tcp",		6,  1, IPPROTO_TCP,	1,	OP_OFFSET_LINK,
1387*2b24ab6bSSebastien Roy 	"udp",		9,  1, IPPROTO_UDP,	0,	OP_OFFSET_LINK,
1388*2b24ab6bSSebastien Roy 	"udp",		6,  1, IPPROTO_UDP,	1,	OP_OFFSET_LINK,
1389*2b24ab6bSSebastien Roy 	"icmp",		9,  1, IPPROTO_ICMP,	0,	OP_OFFSET_LINK,
1390*2b24ab6bSSebastien Roy 	"icmp6",	6,  1, IPPROTO_ICMPV6,	1,	OP_OFFSET_LINK,
1391*2b24ab6bSSebastien Roy 	"ospf",		9,  1, IPPROTO_OSPF,	0,	OP_OFFSET_LINK,
1392*2b24ab6bSSebastien Roy 	"ospf",		6,  1, IPPROTO_OSPF,	1,	OP_OFFSET_LINK,
1393*2b24ab6bSSebastien Roy 	"ip-in-ip",	9,  1, IPPROTO_ENCAP,	0,	OP_OFFSET_LINK,
1394*2b24ab6bSSebastien Roy 	"esp",		9,  1, IPPROTO_ESP,	0,	OP_OFFSET_LINK,
1395*2b24ab6bSSebastien Roy 	"esp",		6,  1, IPPROTO_ESP,	1,	OP_OFFSET_LINK,
1396*2b24ab6bSSebastien Roy 	"ah",		9,  1, IPPROTO_AH,	0,	OP_OFFSET_LINK,
1397*2b24ab6bSSebastien Roy 	"ah",		6,  1, IPPROTO_AH,	1,	OP_OFFSET_LINK,
1398*2b24ab6bSSebastien Roy 	"sctp",		9,  1, IPPROTO_SCTP,	0,	OP_OFFSET_LINK,
1399*2b24ab6bSSebastien Roy 	"sctp",		6,  1, IPPROTO_SCTP,	1,	OP_OFFSET_LINK,
1400*2b24ab6bSSebastien Roy 	0,		0,  0, 0,		0,	0
1401*2b24ab6bSSebastien Roy };
1402*2b24ab6bSSebastien Roy 
14037c478bd9Sstevel@tonic-gate static void
1404b127ac41SPhilip Kirk generate_check(match_type_t match_types[], int index, int type)
14057c478bd9Sstevel@tonic-gate {
1406b127ac41SPhilip Kirk 	match_type_t *mtp = &match_types[index];
14077c478bd9Sstevel@tonic-gate 	/*
14087c478bd9Sstevel@tonic-gate 	 * Note: this code assumes the above dependencies are
14097c478bd9Sstevel@tonic-gate 	 * not cyclic.  This *should* always be true.
14107c478bd9Sstevel@tonic-gate 	 */
14117c478bd9Sstevel@tonic-gate 	if (mtp->m_depend != -1)
1412b127ac41SPhilip Kirk 		generate_check(match_types, mtp->m_depend, type);
14137c478bd9Sstevel@tonic-gate 
1414605445d5Sdg 	emitop(mtp->m_optype);
1415605445d5Sdg 	load_value(mtp->m_offset, mtp->m_size);
1416605445d5Sdg 	load_const(mtp->m_value);
1417605445d5Sdg 	emitop(OP_OFFSET_POP);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	emitop(OP_EQ);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (mtp->m_depend != -1)
14227c478bd9Sstevel@tonic-gate 		emitop(OP_AND);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * Generate code based on the keyword argument.
14277c478bd9Sstevel@tonic-gate  * This word is looked up in the match_types table
14287c478bd9Sstevel@tonic-gate  * and checks a field within the packet for a given
14297c478bd9Sstevel@tonic-gate  * value e.g. ether or ip type field.  The match
14307c478bd9Sstevel@tonic-gate  * can also have a dependency on another entry e.g.
14317c478bd9Sstevel@tonic-gate  * "tcp" requires that the packet also be "ip".
14327c478bd9Sstevel@tonic-gate  */
14337c478bd9Sstevel@tonic-gate static int
14347c478bd9Sstevel@tonic-gate comparison(char *s)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	unsigned int	i, n_checks = 0;
1437b127ac41SPhilip Kirk 	match_type_t	*match_types;
14387c478bd9Sstevel@tonic-gate 
1439b127ac41SPhilip Kirk 	switch (interface->mac_type) {
1440b127ac41SPhilip Kirk 	case DL_ETHER:
1441b127ac41SPhilip Kirk 		match_types = ether_match_types;
1442b127ac41SPhilip Kirk 		break;
1443b127ac41SPhilip Kirk 	case DL_IPNET:
1444b127ac41SPhilip Kirk 		match_types = ipnet_match_types;
1445b127ac41SPhilip Kirk 		break;
1446*2b24ab6bSSebastien Roy 	case DL_IPV4:
1447*2b24ab6bSSebastien Roy 	case DL_IPV6:
1448*2b24ab6bSSebastien Roy 	case DL_6TO4:
1449*2b24ab6bSSebastien Roy 		match_types = iptun_match_types;
1450*2b24ab6bSSebastien Roy 		break;
1451b127ac41SPhilip Kirk 	default:
1452b127ac41SPhilip Kirk 		return (0);
1453b127ac41SPhilip Kirk 	}
14547c478bd9Sstevel@tonic-gate 
1455b127ac41SPhilip Kirk 	for (i = 0; match_types[i].m_name != NULL; i++) {
14567c478bd9Sstevel@tonic-gate 		if (strcmp(s, match_types[i].m_name) != 0)
14577c478bd9Sstevel@tonic-gate 			continue;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		n_checks++;
1460b127ac41SPhilip Kirk 		generate_check(match_types, i, interface->mac_type);
14617c478bd9Sstevel@tonic-gate 		if (n_checks > 1)
14627c478bd9Sstevel@tonic-gate 			emitop(OP_OR);
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	return (n_checks > 0);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate enum direction { ANY, TO, FROM };
14697c478bd9Sstevel@tonic-gate enum direction dir;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * Generate code to match an IP address.  The address
14737c478bd9Sstevel@tonic-gate  * may be supplied either as a hostname or in dotted format.
14747c478bd9Sstevel@tonic-gate  * For source packets both the IP source address and ARP
14757c478bd9Sstevel@tonic-gate  * src are checked.
14767c478bd9Sstevel@tonic-gate  * Note: we don't check packet type here - whether IP or ARP.
14777c478bd9Sstevel@tonic-gate  * It's possible that we'll do an improper match.
14787c478bd9Sstevel@tonic-gate  */
14797c478bd9Sstevel@tonic-gate static void
14807c478bd9Sstevel@tonic-gate ipaddr_match(enum direction which, char *hostname, int inet_type)
14817c478bd9Sstevel@tonic-gate {
14827c478bd9Sstevel@tonic-gate 	bool_t found_host;
14837c478bd9Sstevel@tonic-gate 	int m = 0, n = 0;
14847c478bd9Sstevel@tonic-gate 	uint_t *addr4ptr;
14857c478bd9Sstevel@tonic-gate 	uint_t addr4;
14867c478bd9Sstevel@tonic-gate 	struct in6_addr *addr6ptr;
14877c478bd9Sstevel@tonic-gate 	int h_addr_index;
14887c478bd9Sstevel@tonic-gate 	struct hostent *hp = NULL;
14897c478bd9Sstevel@tonic-gate 	int error_num = 0;
14907c478bd9Sstevel@tonic-gate 	boolean_t freehp = B_FALSE;
14917c478bd9Sstevel@tonic-gate 	boolean_t first = B_TRUE;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	/*
14947c478bd9Sstevel@tonic-gate 	 * The addr4offset and addr6offset variables simplify the code which
14957c478bd9Sstevel@tonic-gate 	 * generates the address comparison filter.  With these two variables,
14967c478bd9Sstevel@tonic-gate 	 * duplicate code need not exist for the TO and FROM case.
14977c478bd9Sstevel@tonic-gate 	 * A value of -1 describes the ANY case (TO and FROM).
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	int addr4offset;
15007c478bd9Sstevel@tonic-gate 	int addr6offset;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	found_host = 0;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	if (tokentype == ADDR_IP) {
1505b127ac41SPhilip Kirk 		hp = lgetipnodebyname(hostname, AF_INET, 0, &error_num);
15067c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
1507b127ac41SPhilip Kirk 			hp = getipnodebyname(hostname, AF_INET, 0, &error_num);
15087c478bd9Sstevel@tonic-gate 			freehp = 1;
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
15117c478bd9Sstevel@tonic-gate 			if (error_num == TRY_AGAIN) {
15127c478bd9Sstevel@tonic-gate 				pr_err("couldn't resolve %s (try again later)",
15137c478bd9Sstevel@tonic-gate 				    hostname);
15147c478bd9Sstevel@tonic-gate 			} else {
15157c478bd9Sstevel@tonic-gate 				pr_err("couldn't resolve %s", hostname);
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 		inet_type = IPV4_ONLY;
15197c478bd9Sstevel@tonic-gate 	} else if (tokentype == ADDR_IP6) {
1520b127ac41SPhilip Kirk 		hp = lgetipnodebyname(hostname, AF_INET6, 0, &error_num);
15217c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
1522b127ac41SPhilip Kirk 			hp = getipnodebyname(hostname, AF_INET6, 0, &error_num);
15237c478bd9Sstevel@tonic-gate 			freehp = 1;
15247c478bd9Sstevel@tonic-gate 		}
15257c478bd9Sstevel@tonic-gate 		if (hp == NULL) {
15267c478bd9Sstevel@tonic-gate 			if (error_num == TRY_AGAIN) {
15277c478bd9Sstevel@tonic-gate 				pr_err("couldn't resolve %s (try again later)",
15287c478bd9Sstevel@tonic-gate 				    hostname);
15297c478bd9Sstevel@tonic-gate 			} else {
15307c478bd9Sstevel@tonic-gate 				pr_err("couldn't resolve %s", hostname);
15317c478bd9Sstevel@tonic-gate 			}
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 		inet_type = IPV6_ONLY;
15347c478bd9Sstevel@tonic-gate 	} else {
15357c478bd9Sstevel@tonic-gate 		/* Some hostname i.e. tokentype is ALPHA */
15367c478bd9Sstevel@tonic-gate 		switch (inet_type) {
15377c478bd9Sstevel@tonic-gate 		case IPV4_ONLY:
15387c478bd9Sstevel@tonic-gate 			/* Only IPv4 address is needed */
1539b127ac41SPhilip Kirk 			hp = lgetipnodebyname(hostname, AF_INET, 0, &error_num);
15407c478bd9Sstevel@tonic-gate 			if (hp == NULL) {
1541b127ac41SPhilip Kirk 				hp = getipnodebyname(hostname, AF_INET,	0,
1542b127ac41SPhilip Kirk 				    &error_num);
15437c478bd9Sstevel@tonic-gate 				freehp = 1;
15447c478bd9Sstevel@tonic-gate 			}
15457c478bd9Sstevel@tonic-gate 			if (hp != NULL) {
15467c478bd9Sstevel@tonic-gate 				found_host = 1;
15477c478bd9Sstevel@tonic-gate 			}
15487c478bd9Sstevel@tonic-gate 			break;
15497c478bd9Sstevel@tonic-gate 		case IPV6_ONLY:
15507c478bd9Sstevel@tonic-gate 			/* Only IPv6 address is needed */
1551b127ac41SPhilip Kirk 			hp = lgetipnodebyname(hostname, AF_INET6, 0,
1552b127ac41SPhilip Kirk 			    &error_num);
15537c478bd9Sstevel@tonic-gate 			if (hp == NULL) {
1554b127ac41SPhilip Kirk 				hp = getipnodebyname(hostname, AF_INET6, 0,
1555b127ac41SPhilip Kirk 				    &error_num);
15567c478bd9Sstevel@tonic-gate 				freehp = 1;
15577c478bd9Sstevel@tonic-gate 			}
15587c478bd9Sstevel@tonic-gate 			if (hp != NULL) {
15597c478bd9Sstevel@tonic-gate 				found_host = 1;
15607c478bd9Sstevel@tonic-gate 			}
15617c478bd9Sstevel@tonic-gate 			break;
15627c478bd9Sstevel@tonic-gate 		case IPV4_AND_IPV6:
15637c478bd9Sstevel@tonic-gate 			/* Both IPv4 and IPv6 are needed */
15647c478bd9Sstevel@tonic-gate 			hp = lgetipnodebyname(hostname, AF_INET6,
1565b127ac41SPhilip Kirk 			    AI_ALL | AI_V4MAPPED, &error_num);
15667c478bd9Sstevel@tonic-gate 			if (hp == NULL) {
15677c478bd9Sstevel@tonic-gate 				hp = getipnodebyname(hostname, AF_INET6,
1568b127ac41SPhilip Kirk 				    AI_ALL | AI_V4MAPPED, &error_num);
15697c478bd9Sstevel@tonic-gate 				freehp = 1;
15707c478bd9Sstevel@tonic-gate 			}
15717c478bd9Sstevel@tonic-gate 			if (hp != NULL) {
15727c478bd9Sstevel@tonic-gate 				found_host = 1;
15737c478bd9Sstevel@tonic-gate 			}
15747c478bd9Sstevel@tonic-gate 			break;
15757c478bd9Sstevel@tonic-gate 		default:
15767c478bd9Sstevel@tonic-gate 			found_host = 0;
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		if (!found_host) {
15807c478bd9Sstevel@tonic-gate 			if (error_num == TRY_AGAIN) {
15817c478bd9Sstevel@tonic-gate 				pr_err("could not resolve %s (try again later)",
15827c478bd9Sstevel@tonic-gate 				    hostname);
15837c478bd9Sstevel@tonic-gate 			} else {
15847c478bd9Sstevel@tonic-gate 				pr_err("could not resolve %s", hostname);
15857c478bd9Sstevel@tonic-gate 			}
15867c478bd9Sstevel@tonic-gate 		}
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	switch (which) {
15907c478bd9Sstevel@tonic-gate 	case TO:
15917c478bd9Sstevel@tonic-gate 		addr4offset = IPV4_DSTADDR_OFFSET;
15927c478bd9Sstevel@tonic-gate 		addr6offset = IPV6_DSTADDR_OFFSET;
15937c478bd9Sstevel@tonic-gate 		break;
15947c478bd9Sstevel@tonic-gate 	case FROM:
15957c478bd9Sstevel@tonic-gate 		addr4offset = IPV4_SRCADDR_OFFSET;
15967c478bd9Sstevel@tonic-gate 		addr6offset = IPV6_SRCADDR_OFFSET;
15977c478bd9Sstevel@tonic-gate 		break;
15987c478bd9Sstevel@tonic-gate 	case ANY:
15997c478bd9Sstevel@tonic-gate 		addr4offset = -1;
16007c478bd9Sstevel@tonic-gate 		addr6offset = -1;
16017c478bd9Sstevel@tonic-gate 		break;
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	/*
16057c478bd9Sstevel@tonic-gate 	 * The code below generates the filter.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	if (hp != NULL && hp->h_addrtype == AF_INET) {
1608b127ac41SPhilip Kirk 		ethertype_match(interface->network_type_ip);
16097c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
16107c478bd9Sstevel@tonic-gate 		n = chain(n);
16117c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_LINK);
16127c478bd9Sstevel@tonic-gate 		h_addr_index = 0;
16137c478bd9Sstevel@tonic-gate 		addr4ptr = (uint_t *)hp->h_addr_list[h_addr_index];
16147c478bd9Sstevel@tonic-gate 		while (addr4ptr != NULL) {
16157c478bd9Sstevel@tonic-gate 			if (addr4offset == -1) {
16167c478bd9Sstevel@tonic-gate 				compare_addr_v4(IPV4_SRCADDR_OFFSET, 4,
16177c478bd9Sstevel@tonic-gate 				    *addr4ptr);
16187c478bd9Sstevel@tonic-gate 				emitop(OP_BRTR);
16197c478bd9Sstevel@tonic-gate 				m = chain(m);
16207c478bd9Sstevel@tonic-gate 				compare_addr_v4(IPV4_DSTADDR_OFFSET, 4,
16217c478bd9Sstevel@tonic-gate 				    *addr4ptr);
16227c478bd9Sstevel@tonic-gate 			} else {
16237c478bd9Sstevel@tonic-gate 				compare_addr_v4(addr4offset, 4, *addr4ptr);
16247c478bd9Sstevel@tonic-gate 			}
16257c478bd9Sstevel@tonic-gate 			addr4ptr = (uint_t *)hp->h_addr_list[++h_addr_index];
16267c478bd9Sstevel@tonic-gate 			if (addr4ptr != NULL) {
16277c478bd9Sstevel@tonic-gate 				emitop(OP_BRTR);
16287c478bd9Sstevel@tonic-gate 				m = chain(m);
16297c478bd9Sstevel@tonic-gate 			}
16307c478bd9Sstevel@tonic-gate 		}
16317c478bd9Sstevel@tonic-gate 		if (m != 0) {
16327c478bd9Sstevel@tonic-gate 			resolve_chain(m);
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_POP);
16357c478bd9Sstevel@tonic-gate 		resolve_chain(n);
16367c478bd9Sstevel@tonic-gate 	} else {
16377c478bd9Sstevel@tonic-gate 		/* first pass: IPv4 addresses */
16387c478bd9Sstevel@tonic-gate 		h_addr_index = 0;
16397c478bd9Sstevel@tonic-gate 		addr6ptr = (struct in6_addr *)hp->h_addr_list[h_addr_index];
16407c478bd9Sstevel@tonic-gate 		first = B_TRUE;
16417c478bd9Sstevel@tonic-gate 		while (addr6ptr != NULL) {
16427c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_V4MAPPED(addr6ptr)) {
16437c478bd9Sstevel@tonic-gate 				if (first) {
1644b127ac41SPhilip Kirk 					ethertype_match(
1645b127ac41SPhilip Kirk 					    interface->network_type_ip);
16467c478bd9Sstevel@tonic-gate 					emitop(OP_BRFL);
16477c478bd9Sstevel@tonic-gate 					n = chain(n);
16487c478bd9Sstevel@tonic-gate 					emitop(OP_OFFSET_LINK);
16497c478bd9Sstevel@tonic-gate 					first = B_FALSE;
16507c478bd9Sstevel@tonic-gate 				} else {
16517c478bd9Sstevel@tonic-gate 					emitop(OP_BRTR);
16527c478bd9Sstevel@tonic-gate 					m = chain(m);
16537c478bd9Sstevel@tonic-gate 				}
16547c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_INADDR(addr6ptr,
16557c478bd9Sstevel@tonic-gate 				    (struct in_addr *)&addr4);
16567c478bd9Sstevel@tonic-gate 				if (addr4offset == -1) {
16577c478bd9Sstevel@tonic-gate 					compare_addr_v4(IPV4_SRCADDR_OFFSET, 4,
16587c478bd9Sstevel@tonic-gate 					    addr4);
16597c478bd9Sstevel@tonic-gate 					emitop(OP_BRTR);
16607c478bd9Sstevel@tonic-gate 					m = chain(m);
16617c478bd9Sstevel@tonic-gate 					compare_addr_v4(IPV4_DSTADDR_OFFSET, 4,
16627c478bd9Sstevel@tonic-gate 					    addr4);
16637c478bd9Sstevel@tonic-gate 				} else {
16647c478bd9Sstevel@tonic-gate 					compare_addr_v4(addr4offset, 4, addr4);
16657c478bd9Sstevel@tonic-gate 				}
16667c478bd9Sstevel@tonic-gate 			}
16677c478bd9Sstevel@tonic-gate 			addr6ptr = (struct in6_addr *)
16687c478bd9Sstevel@tonic-gate 			    hp->h_addr_list[++h_addr_index];
16697c478bd9Sstevel@tonic-gate 		}
16707c478bd9Sstevel@tonic-gate 		/* second pass: IPv6 addresses */
16717c478bd9Sstevel@tonic-gate 		h_addr_index = 0;
16727c478bd9Sstevel@tonic-gate 		addr6ptr = (struct in6_addr *)hp->h_addr_list[h_addr_index];
16737c478bd9Sstevel@tonic-gate 		first = B_TRUE;
16747c478bd9Sstevel@tonic-gate 		while (addr6ptr != NULL) {
16757c478bd9Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED(addr6ptr)) {
16767c478bd9Sstevel@tonic-gate 				if (first) {
16777c478bd9Sstevel@tonic-gate 					/*
16787c478bd9Sstevel@tonic-gate 					 * bypass check for IPv6 addresses
16797c478bd9Sstevel@tonic-gate 					 * when we have an IPv4 packet
16807c478bd9Sstevel@tonic-gate 					 */
16817c478bd9Sstevel@tonic-gate 					if (n != 0) {
16827c478bd9Sstevel@tonic-gate 						emitop(OP_BRTR);
16837c478bd9Sstevel@tonic-gate 						m = chain(m);
16847c478bd9Sstevel@tonic-gate 						emitop(OP_BRFL);
16857c478bd9Sstevel@tonic-gate 						m = chain(m);
16867c478bd9Sstevel@tonic-gate 						resolve_chain(n);
16877c478bd9Sstevel@tonic-gate 						n = 0;
16887c478bd9Sstevel@tonic-gate 					}
1689b127ac41SPhilip Kirk 					ethertype_match(
1690b127ac41SPhilip Kirk 					    interface->network_type_ipv6);
16917c478bd9Sstevel@tonic-gate 					emitop(OP_BRFL);
16927c478bd9Sstevel@tonic-gate 					n = chain(n);
16937c478bd9Sstevel@tonic-gate 					emitop(OP_OFFSET_LINK);
16947c478bd9Sstevel@tonic-gate 					first = B_FALSE;
16957c478bd9Sstevel@tonic-gate 				} else {
16967c478bd9Sstevel@tonic-gate 					emitop(OP_BRTR);
16977c478bd9Sstevel@tonic-gate 					m = chain(m);
16987c478bd9Sstevel@tonic-gate 				}
16997c478bd9Sstevel@tonic-gate 				if (addr6offset == -1) {
17007c478bd9Sstevel@tonic-gate 					compare_addr_v6(IPV6_SRCADDR_OFFSET,
17017c478bd9Sstevel@tonic-gate 					    16, *addr6ptr);
17027c478bd9Sstevel@tonic-gate 					emitop(OP_BRTR);
17037c478bd9Sstevel@tonic-gate 					m = chain(m);
17047c478bd9Sstevel@tonic-gate 					compare_addr_v6(IPV6_DSTADDR_OFFSET,
17057c478bd9Sstevel@tonic-gate 					    16, *addr6ptr);
17067c478bd9Sstevel@tonic-gate 				} else {
17077c478bd9Sstevel@tonic-gate 					compare_addr_v6(addr6offset, 16,
17087c478bd9Sstevel@tonic-gate 					    *addr6ptr);
17097c478bd9Sstevel@tonic-gate 				}
17107c478bd9Sstevel@tonic-gate 			}
17117c478bd9Sstevel@tonic-gate 			addr6ptr = (struct in6_addr *)
17127c478bd9Sstevel@tonic-gate 			    hp->h_addr_list[++h_addr_index];
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 		if (m != 0) {
17157c478bd9Sstevel@tonic-gate 			resolve_chain(m);
17167c478bd9Sstevel@tonic-gate 		}
17177c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_POP);
17187c478bd9Sstevel@tonic-gate 		resolve_chain(n);
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	/* only free struct hostent returned by getipnodebyname() */
17227c478bd9Sstevel@tonic-gate 	if (freehp) {
17237c478bd9Sstevel@tonic-gate 		freehostent(hp);
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate 
1727b127ac41SPhilip Kirk /*
1728b127ac41SPhilip Kirk  * Match on zoneid. The arg zone passed in is in network byte order.
1729b127ac41SPhilip Kirk  */
1730b127ac41SPhilip Kirk static void
1731b127ac41SPhilip Kirk zone_match(enum direction which, uint64_t zone)
1732b127ac41SPhilip Kirk {
1733b127ac41SPhilip Kirk 
1734b127ac41SPhilip Kirk 	switch (which) {
1735b127ac41SPhilip Kirk 	case TO:
1736b127ac41SPhilip Kirk 		compare_value_zone(IPNET_DSTZONE_OFFSET, zone);
1737b127ac41SPhilip Kirk 		break;
1738b127ac41SPhilip Kirk 	case FROM:
1739b127ac41SPhilip Kirk 		compare_value_zone(IPNET_SRCZONE_OFFSET, zone);
1740b127ac41SPhilip Kirk 		break;
1741b127ac41SPhilip Kirk 	case ANY:
1742b127ac41SPhilip Kirk 		compare_value_zone(IPNET_SRCZONE_OFFSET, zone);
1743b127ac41SPhilip Kirk 		compare_value_zone(IPNET_DSTZONE_OFFSET, zone);
1744b127ac41SPhilip Kirk 		emitop(OP_OR);
1745b127ac41SPhilip Kirk 	}
1746b127ac41SPhilip Kirk }
1747b127ac41SPhilip Kirk 
17487c478bd9Sstevel@tonic-gate /*
17497c478bd9Sstevel@tonic-gate  * Generate code to match an AppleTalk address.  The address
17507c478bd9Sstevel@tonic-gate  * must be given as two numbers with a dot between
17517c478bd9Sstevel@tonic-gate  *
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate static void
17547c478bd9Sstevel@tonic-gate ataddr_match(enum direction which, char *hostname)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	uint_t net;
17577c478bd9Sstevel@tonic-gate 	uint_t node;
17587c478bd9Sstevel@tonic-gate 	uint_t m, n;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	sscanf(hostname, "%u.%u", &net, &node);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_LINK);
17637c478bd9Sstevel@tonic-gate 	switch (which) {
17647c478bd9Sstevel@tonic-gate 	case TO:
17657c478bd9Sstevel@tonic-gate 		compare_value(AT_DST_NET_OFFSET, 2, net);
17667c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
17677c478bd9Sstevel@tonic-gate 		m = chain(0);
17687c478bd9Sstevel@tonic-gate 		compare_value(AT_DST_NODE_OFFSET, 1, node);
17697c478bd9Sstevel@tonic-gate 		resolve_chain(m);
17707c478bd9Sstevel@tonic-gate 		break;
17717c478bd9Sstevel@tonic-gate 	case FROM:
17727c478bd9Sstevel@tonic-gate 		compare_value(AT_SRC_NET_OFFSET, 2, net);
17737c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
17747c478bd9Sstevel@tonic-gate 		m = chain(0);
17757c478bd9Sstevel@tonic-gate 		compare_value(AT_SRC_NODE_OFFSET, 1, node);
17767c478bd9Sstevel@tonic-gate 		resolve_chain(m);
17777c478bd9Sstevel@tonic-gate 		break;
17787c478bd9Sstevel@tonic-gate 	case ANY:
17797c478bd9Sstevel@tonic-gate 		compare_value(AT_DST_NET_OFFSET, 2, net);
17807c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
17817c478bd9Sstevel@tonic-gate 		m = chain(0);
17827c478bd9Sstevel@tonic-gate 		compare_value(AT_DST_NODE_OFFSET, 1, node);
17837c478bd9Sstevel@tonic-gate 		resolve_chain(m);
17847c478bd9Sstevel@tonic-gate 		emitop(OP_BRTR);
17857c478bd9Sstevel@tonic-gate 		n = chain(0);
17867c478bd9Sstevel@tonic-gate 		compare_value(AT_SRC_NET_OFFSET, 2, net);
17877c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
17887c478bd9Sstevel@tonic-gate 		m = chain(0);
17897c478bd9Sstevel@tonic-gate 		compare_value(AT_SRC_NODE_OFFSET, 1, node);
17907c478bd9Sstevel@tonic-gate 		resolve_chain(m);
17917c478bd9Sstevel@tonic-gate 		resolve_chain(n);
17927c478bd9Sstevel@tonic-gate 		break;
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_POP);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate /*
17987c478bd9Sstevel@tonic-gate  * Compare ethernet addresses. The address may
17997c478bd9Sstevel@tonic-gate  * be provided either as a hostname or as a
18007c478bd9Sstevel@tonic-gate  * 6 octet colon-separated address.
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate static void
18037c478bd9Sstevel@tonic-gate etheraddr_match(enum direction which, char *hostname)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	uint_t addr;
18067c478bd9Sstevel@tonic-gate 	ushort_t *addrp;
18077c478bd9Sstevel@tonic-gate 	int to_offset, from_offset;
18087c478bd9Sstevel@tonic-gate 	struct ether_addr e, *ep = NULL;
18097c478bd9Sstevel@tonic-gate 	int m;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	/*
18127c478bd9Sstevel@tonic-gate 	 * First, check the interface type for whether src/dest address
18137c478bd9Sstevel@tonic-gate 	 * is determinable; if not, retreat early.
18147c478bd9Sstevel@tonic-gate 	 */
18157c478bd9Sstevel@tonic-gate 	switch (interface->mac_type) {
18167c478bd9Sstevel@tonic-gate 	case DL_ETHER:
18177c478bd9Sstevel@tonic-gate 		from_offset = ETHERADDRL;
18187c478bd9Sstevel@tonic-gate 		to_offset = 0;
18197c478bd9Sstevel@tonic-gate 		break;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	case DL_IB:
18227c478bd9Sstevel@tonic-gate 		/*
18237c478bd9Sstevel@tonic-gate 		 * If an ethernet address is attempted to be used
18247c478bd9Sstevel@tonic-gate 		 * on an IPoIB interface, flag error. Link address
18257c478bd9Sstevel@tonic-gate 		 * based filtering is unsupported on IPoIB, so there
18267c478bd9Sstevel@tonic-gate 		 * is no ipibaddr_match() or parsing support for IPoIB
18277c478bd9Sstevel@tonic-gate 		 * 20 byte link addresses.
18287c478bd9Sstevel@tonic-gate 		 */
18297c478bd9Sstevel@tonic-gate 		pr_err("filter option unsupported on media");
18307c478bd9Sstevel@tonic-gate 		break;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	case DL_FDDI:
18337c478bd9Sstevel@tonic-gate 		from_offset = 7;
18347c478bd9Sstevel@tonic-gate 		to_offset = 1;
18357c478bd9Sstevel@tonic-gate 		break;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	default:
18387c478bd9Sstevel@tonic-gate 		/*
18397c478bd9Sstevel@tonic-gate 		 * Where do we find "ether" address for FDDI & TR?
18407c478bd9Sstevel@tonic-gate 		 * XXX can improve?  ~sparker
18417c478bd9Sstevel@tonic-gate 		 */
18427c478bd9Sstevel@tonic-gate 		load_const(1);
18437c478bd9Sstevel@tonic-gate 		return;
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	if (isxdigit(*hostname))
18477c478bd9Sstevel@tonic-gate 		ep = ether_aton(hostname);
18487c478bd9Sstevel@tonic-gate 	if (ep == NULL) {
18497c478bd9Sstevel@tonic-gate 		if (ether_hostton(hostname, &e))
18507c478bd9Sstevel@tonic-gate 			if (!arp_for_ether(hostname, &e))
18517c478bd9Sstevel@tonic-gate 				pr_err("cannot obtain ether addr for %s",
1852b127ac41SPhilip Kirk 				    hostname);
18537c478bd9Sstevel@tonic-gate 		ep = &e;
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 	memcpy(&addr, (ushort_t *)ep, 4);
18567c478bd9Sstevel@tonic-gate 	addrp = (ushort_t *)ep + 2;
18577c478bd9Sstevel@tonic-gate 
1858605445d5Sdg 	emitop(OP_OFFSET_ZERO);
18597c478bd9Sstevel@tonic-gate 	switch (which) {
18607c478bd9Sstevel@tonic-gate 	case TO:
18617c478bd9Sstevel@tonic-gate 		compare_value(to_offset, 4, ntohl(addr));
18627c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
18637c478bd9Sstevel@tonic-gate 		m = chain(0);
18647c478bd9Sstevel@tonic-gate 		compare_value(to_offset + 4, 2, ntohs(*addrp));
18657c478bd9Sstevel@tonic-gate 		resolve_chain(m);
18667c478bd9Sstevel@tonic-gate 		break;
18677c478bd9Sstevel@tonic-gate 	case FROM:
18687c478bd9Sstevel@tonic-gate 		compare_value(from_offset, 4, ntohl(addr));
18697c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
18707c478bd9Sstevel@tonic-gate 		m = chain(0);
18717c478bd9Sstevel@tonic-gate 		compare_value(from_offset + 4, 2, ntohs(*addrp));
18727c478bd9Sstevel@tonic-gate 		resolve_chain(m);
18737c478bd9Sstevel@tonic-gate 		break;
18747c478bd9Sstevel@tonic-gate 	case ANY:
18757c478bd9Sstevel@tonic-gate 		compare_value(to_offset, 4, ntohl(addr));
18767c478bd9Sstevel@tonic-gate 		compare_value(to_offset + 4, 2, ntohs(*addrp));
18777c478bd9Sstevel@tonic-gate 		emitop(OP_AND);
18787c478bd9Sstevel@tonic-gate 		emitop(OP_BRTR);
18797c478bd9Sstevel@tonic-gate 		m = chain(0);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		compare_value(from_offset, 4, ntohl(addr));
18827c478bd9Sstevel@tonic-gate 		compare_value(from_offset + 4, 2, ntohs(*addrp));
18837c478bd9Sstevel@tonic-gate 		emitop(OP_AND);
18847c478bd9Sstevel@tonic-gate 		resolve_chain(m);
18857c478bd9Sstevel@tonic-gate 		break;
18867c478bd9Sstevel@tonic-gate 	}
1887605445d5Sdg 	emitop(OP_OFFSET_POP);
18887c478bd9Sstevel@tonic-gate }
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate static void
18917c478bd9Sstevel@tonic-gate ethertype_match(int val)
18927c478bd9Sstevel@tonic-gate {
1893605445d5Sdg 	int ether_offset = interface->network_type_offset;
18947c478bd9Sstevel@tonic-gate 
1895605445d5Sdg 	/*
1896605445d5Sdg 	 * If the user is interested in ethertype VLAN,
1897605445d5Sdg 	 * then we need to set the offset to the beginning of the packet.
1898605445d5Sdg 	 * But if the user is interested in another ethertype,
1899605445d5Sdg 	 * such as IPv4, then we need to take into consideration
1900605445d5Sdg 	 * the fact that the packet might be VLAN tagged.
1901605445d5Sdg 	 */
1902605445d5Sdg 	if (interface->mac_type == DL_ETHER ||
1903605445d5Sdg 	    interface->mac_type == DL_CSMACD) {
1904605445d5Sdg 		if (val != ETHERTYPE_VLAN) {
1905605445d5Sdg 			/*
1906605445d5Sdg 			 * OP_OFFSET_ETHERTYPE puts us at the ethertype
1907605445d5Sdg 			 * field whether or not there is a VLAN tag,
1908605445d5Sdg 			 * so ether_offset goes to zero if we get here.
1909605445d5Sdg 			 */
1910605445d5Sdg 			emitop(OP_OFFSET_ETHERTYPE);
1911605445d5Sdg 			ether_offset = 0;
1912605445d5Sdg 		} else {
1913605445d5Sdg 			emitop(OP_OFFSET_ZERO);
1914605445d5Sdg 		}
1915605445d5Sdg 	}
1916b127ac41SPhilip Kirk 	compare_value(ether_offset, interface->network_type_len, val);
1917605445d5Sdg 	if (interface->mac_type == DL_ETHER ||
1918605445d5Sdg 	    interface->mac_type == DL_CSMACD) {
1919605445d5Sdg 		emitop(OP_OFFSET_POP);
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /*
19247c478bd9Sstevel@tonic-gate  * Match a network address.  The host part
19257c478bd9Sstevel@tonic-gate  * is masked out.  The network address may
19267c478bd9Sstevel@tonic-gate  * be supplied either as a netname or in
19277c478bd9Sstevel@tonic-gate  * IP dotted format.  The mask to be used
19287c478bd9Sstevel@tonic-gate  * for the comparison is assumed from the
19297c478bd9Sstevel@tonic-gate  * address format (see comment below).
19307c478bd9Sstevel@tonic-gate  */
19317c478bd9Sstevel@tonic-gate static void
19327c478bd9Sstevel@tonic-gate netaddr_match(enum direction which, char *netname)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	uint_t addr;
19357c478bd9Sstevel@tonic-gate 	uint_t mask = 0xff000000;
19367c478bd9Sstevel@tonic-gate 	uint_t m;
19377c478bd9Sstevel@tonic-gate 	struct netent *np;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	if (isdigit(*netname)) {
19407c478bd9Sstevel@tonic-gate 		addr = inet_network(netname);
19417c478bd9Sstevel@tonic-gate 	} else {
19427c478bd9Sstevel@tonic-gate 		np = getnetbyname(netname);
19437c478bd9Sstevel@tonic-gate 		if (np == NULL)
19447c478bd9Sstevel@tonic-gate 			pr_err("net %s not known", netname);
19457c478bd9Sstevel@tonic-gate 		addr = np->n_net;
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/*
19497c478bd9Sstevel@tonic-gate 	 * Left justify the address and figure
19507c478bd9Sstevel@tonic-gate 	 * out a mask based on the supplied address.
19517c478bd9Sstevel@tonic-gate 	 * Set the mask according to the number of zero
19527c478bd9Sstevel@tonic-gate 	 * low-order bytes.
19537c478bd9Sstevel@tonic-gate 	 * Note: this works only for whole octet masks.
19547c478bd9Sstevel@tonic-gate 	 */
19557c478bd9Sstevel@tonic-gate 	if (addr) {
19567c478bd9Sstevel@tonic-gate 		while ((addr & ~mask) != 0) {
19577c478bd9Sstevel@tonic-gate 			mask |= (mask >> 8);
19587c478bd9Sstevel@tonic-gate 		}
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_LINK);
19627c478bd9Sstevel@tonic-gate 	switch (which) {
19637c478bd9Sstevel@tonic-gate 	case TO:
19647c478bd9Sstevel@tonic-gate 		compare_value_mask(16, 4, addr, mask);
19657c478bd9Sstevel@tonic-gate 		break;
19667c478bd9Sstevel@tonic-gate 	case FROM:
19677c478bd9Sstevel@tonic-gate 		compare_value_mask(12, 4, addr, mask);
19687c478bd9Sstevel@tonic-gate 		break;
19697c478bd9Sstevel@tonic-gate 	case ANY:
19707c478bd9Sstevel@tonic-gate 		compare_value_mask(12, 4, addr, mask);
19717c478bd9Sstevel@tonic-gate 		emitop(OP_BRTR);
19727c478bd9Sstevel@tonic-gate 		m = chain(0);
19737c478bd9Sstevel@tonic-gate 		compare_value_mask(16, 4, addr, mask);
19747c478bd9Sstevel@tonic-gate 		resolve_chain(m);
19757c478bd9Sstevel@tonic-gate 		break;
19767c478bd9Sstevel@tonic-gate 	}
19777c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_POP);
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate /*
19817c478bd9Sstevel@tonic-gate  * Match either a UDP or TCP port number.
19827c478bd9Sstevel@tonic-gate  * The port number may be provided either as
19837c478bd9Sstevel@tonic-gate  * port name as listed in /etc/services ("nntp") or as
19847c478bd9Sstevel@tonic-gate  * the port number itself (2049).
19857c478bd9Sstevel@tonic-gate  */
19867c478bd9Sstevel@tonic-gate static void
19877c478bd9Sstevel@tonic-gate port_match(enum direction which, char *portname)
19887c478bd9Sstevel@tonic-gate {
19897c478bd9Sstevel@tonic-gate 	struct servent *sp;
19907c478bd9Sstevel@tonic-gate 	uint_t m, port;
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if (isdigit(*portname)) {
19937c478bd9Sstevel@tonic-gate 		port = atoi(portname);
19947c478bd9Sstevel@tonic-gate 	} else {
19957c478bd9Sstevel@tonic-gate 		sp = getservbyname(portname, NULL);
19967c478bd9Sstevel@tonic-gate 		if (sp == NULL)
1997b127ac41SPhilip Kirk 			pr_err("invalid port number or name: %s", portname);
19987c478bd9Sstevel@tonic-gate 		port = ntohs(sp->s_port);
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_IP);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	switch (which) {
20047c478bd9Sstevel@tonic-gate 	case TO:
20057c478bd9Sstevel@tonic-gate 		compare_value(2, 2, port);
20067c478bd9Sstevel@tonic-gate 		break;
20077c478bd9Sstevel@tonic-gate 	case FROM:
20087c478bd9Sstevel@tonic-gate 		compare_value(0, 2, port);
20097c478bd9Sstevel@tonic-gate 		break;
20107c478bd9Sstevel@tonic-gate 	case ANY:
20117c478bd9Sstevel@tonic-gate 		compare_value(2, 2, port);
20127c478bd9Sstevel@tonic-gate 		emitop(OP_BRTR);
20137c478bd9Sstevel@tonic-gate 		m = chain(0);
20147c478bd9Sstevel@tonic-gate 		compare_value(0, 2, port);
20157c478bd9Sstevel@tonic-gate 		resolve_chain(m);
20167c478bd9Sstevel@tonic-gate 		break;
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_POP);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate  * Generate code to match packets with a specific
20237c478bd9Sstevel@tonic-gate  * RPC program number.  If the progname is a name
20247c478bd9Sstevel@tonic-gate  * it is converted to a number via /etc/rpc.
20257c478bd9Sstevel@tonic-gate  * The program version and/or procedure may be provided
20267c478bd9Sstevel@tonic-gate  * as extra qualifiers.
20277c478bd9Sstevel@tonic-gate  */
20287c478bd9Sstevel@tonic-gate static void
20297c478bd9Sstevel@tonic-gate rpc_match_prog(enum direction which, char *progname, int vers, int proc)
20307c478bd9Sstevel@tonic-gate {
20317c478bd9Sstevel@tonic-gate 	struct rpcent *rpc;
20327c478bd9Sstevel@tonic-gate 	uint_t prog;
20337c478bd9Sstevel@tonic-gate 	uint_t m, n;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if (isdigit(*progname)) {
20367c478bd9Sstevel@tonic-gate 		prog = atoi(progname);
20377c478bd9Sstevel@tonic-gate 	} else {
20387c478bd9Sstevel@tonic-gate 		rpc = (struct rpcent *)getrpcbyname(progname);
20397c478bd9Sstevel@tonic-gate 		if (rpc == NULL)
20407c478bd9Sstevel@tonic-gate 			pr_err("invalid program name: %s", progname);
20417c478bd9Sstevel@tonic-gate 		prog = rpc->r_number;
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_RPC);
20457c478bd9Sstevel@tonic-gate 	emitop(OP_BRFL);
20467c478bd9Sstevel@tonic-gate 	n = chain(0);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	compare_value(12, 4, prog);
20497c478bd9Sstevel@tonic-gate 	emitop(OP_BRFL);
20507c478bd9Sstevel@tonic-gate 	m = chain(0);
20517c478bd9Sstevel@tonic-gate 	if (vers >= 0) {
20527c478bd9Sstevel@tonic-gate 		compare_value(16, 4, vers);
20537c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
20547c478bd9Sstevel@tonic-gate 		m = chain(m);
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 	if (proc >= 0) {
20577c478bd9Sstevel@tonic-gate 		compare_value(20, 4, proc);
20587c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
20597c478bd9Sstevel@tonic-gate 		m = chain(m);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	switch (which) {
20637c478bd9Sstevel@tonic-gate 	case TO:
20647c478bd9Sstevel@tonic-gate 		compare_value(4, 4, CALL);
20657c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
20667c478bd9Sstevel@tonic-gate 		m = chain(m);
20677c478bd9Sstevel@tonic-gate 		break;
20687c478bd9Sstevel@tonic-gate 	case FROM:
20697c478bd9Sstevel@tonic-gate 		compare_value(4, 4, REPLY);
20707c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
20717c478bd9Sstevel@tonic-gate 		m = chain(m);
20727c478bd9Sstevel@tonic-gate 		break;
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 	resolve_chain(m);
20757c478bd9Sstevel@tonic-gate 	resolve_chain(n);
20767c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_POP);
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate /*
20807c478bd9Sstevel@tonic-gate  * Generate code to parse a field specification
20817c478bd9Sstevel@tonic-gate  * and load the value of the field from the packet
20827c478bd9Sstevel@tonic-gate  * onto the operand stack.
20837c478bd9Sstevel@tonic-gate  * The field offset may be specified relative to the
20847c478bd9Sstevel@tonic-gate  * beginning of the ether header, IP header, UDP header,
20857c478bd9Sstevel@tonic-gate  * or TCP header.  An optional size specification may
20867c478bd9Sstevel@tonic-gate  * be provided following a colon.  If no size is given
20877c478bd9Sstevel@tonic-gate  * one byte is assumed e.g.
20887c478bd9Sstevel@tonic-gate  *
20897c478bd9Sstevel@tonic-gate  *	ether[0]	The first byte of the ether header
20907c478bd9Sstevel@tonic-gate  *	ip[2:2]		The second 16 bit field of the IP header
20917c478bd9Sstevel@tonic-gate  */
20927c478bd9Sstevel@tonic-gate static void
20937c478bd9Sstevel@tonic-gate load_field()
20947c478bd9Sstevel@tonic-gate {
20957c478bd9Sstevel@tonic-gate 	int size = 1;
20967c478bd9Sstevel@tonic-gate 	int s;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	if (EQ("ether"))
21007c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_ZERO);
21017c478bd9Sstevel@tonic-gate 	else if (EQ("ip") || EQ("ip6") || EQ("pppoed") || EQ("pppoes"))
21027c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_LINK);
21037c478bd9Sstevel@tonic-gate 	else if (EQ("udp") || EQ("tcp") || EQ("icmp") || EQ("ip-in-ip") ||
21047c478bd9Sstevel@tonic-gate 	    EQ("ah") || EQ("esp"))
21057c478bd9Sstevel@tonic-gate 		emitop(OP_OFFSET_IP);
21067c478bd9Sstevel@tonic-gate 	else
21077c478bd9Sstevel@tonic-gate 		pr_err("invalid field type");
21087c478bd9Sstevel@tonic-gate 	next();
21097c478bd9Sstevel@tonic-gate 	s = opstack;
21107c478bd9Sstevel@tonic-gate 	expression();
21117c478bd9Sstevel@tonic-gate 	if (opstack != s + 1)
21127c478bd9Sstevel@tonic-gate 		pr_err("invalid field offset");
21137c478bd9Sstevel@tonic-gate 	opstack--;
21147c478bd9Sstevel@tonic-gate 	if (*token == ':') {
21157c478bd9Sstevel@tonic-gate 		next();
21167c478bd9Sstevel@tonic-gate 		if (tokentype != NUMBER)
21177c478bd9Sstevel@tonic-gate 			pr_err("field size expected");
21187c478bd9Sstevel@tonic-gate 		size = tokenval;
21197c478bd9Sstevel@tonic-gate 		if (size != 1 && size != 2 && size != 4)
21207c478bd9Sstevel@tonic-gate 			pr_err("field size invalid");
21217c478bd9Sstevel@tonic-gate 		next();
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 	if (*token != ']')
21247c478bd9Sstevel@tonic-gate 		pr_err("right bracket expected");
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	load_value(-1, size);
21277c478bd9Sstevel@tonic-gate 	emitop(OP_OFFSET_POP);
21287c478bd9Sstevel@tonic-gate }
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate /*
21317c478bd9Sstevel@tonic-gate  * Check that the operand stack
21327c478bd9Sstevel@tonic-gate  * contains n arguments
21337c478bd9Sstevel@tonic-gate  */
21347c478bd9Sstevel@tonic-gate static void
21357c478bd9Sstevel@tonic-gate checkstack(int numargs)
21367c478bd9Sstevel@tonic-gate {
21377c478bd9Sstevel@tonic-gate 	if (opstack != numargs)
21387c478bd9Sstevel@tonic-gate 		pr_err("invalid expression at \"%s\".", token);
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate static void
21427c478bd9Sstevel@tonic-gate primary()
21437c478bd9Sstevel@tonic-gate {
2144d04ccbb3Scarlsonj 	int m, m2, s;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	for (;;) {
21477c478bd9Sstevel@tonic-gate 		if (tokentype == FIELD) {
21487c478bd9Sstevel@tonic-gate 			load_field();
21497c478bd9Sstevel@tonic-gate 			opstack++;
21507c478bd9Sstevel@tonic-gate 			next();
21517c478bd9Sstevel@tonic-gate 			break;
21527c478bd9Sstevel@tonic-gate 		}
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		if (comparison(token)) {
21557c478bd9Sstevel@tonic-gate 			opstack++;
21567c478bd9Sstevel@tonic-gate 			next();
21577c478bd9Sstevel@tonic-gate 			break;
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 		if (EQ("not") || EQ("!")) {
21617c478bd9Sstevel@tonic-gate 			next();
21627c478bd9Sstevel@tonic-gate 			s = opstack;
21637c478bd9Sstevel@tonic-gate 			primary();
21647c478bd9Sstevel@tonic-gate 			checkstack(s + 1);
21657c478bd9Sstevel@tonic-gate 			emitop(OP_NOT);
21667c478bd9Sstevel@tonic-gate 			break;
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 		if (EQ("(")) {
21707c478bd9Sstevel@tonic-gate 			next();
21717c478bd9Sstevel@tonic-gate 			s = opstack;
21727c478bd9Sstevel@tonic-gate 			expression();
21737c478bd9Sstevel@tonic-gate 			checkstack(s + 1);
21747c478bd9Sstevel@tonic-gate 			if (!EQ(")"))
21757c478bd9Sstevel@tonic-gate 				pr_err("right paren expected");
21767c478bd9Sstevel@tonic-gate 			next();
21777c478bd9Sstevel@tonic-gate 		}
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 		if (EQ("to") || EQ("dst")) {
21807c478bd9Sstevel@tonic-gate 			dir = TO;
21817c478bd9Sstevel@tonic-gate 			next();
21827c478bd9Sstevel@tonic-gate 			continue;
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 		if (EQ("from") || EQ("src")) {
21867c478bd9Sstevel@tonic-gate 			dir = FROM;
21877c478bd9Sstevel@tonic-gate 			next();
21887c478bd9Sstevel@tonic-gate 			continue;
21897c478bd9Sstevel@tonic-gate 		}
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 		if (EQ("ether")) {
21927c478bd9Sstevel@tonic-gate 			eaddr = 1;
21937c478bd9Sstevel@tonic-gate 			next();
21947c478bd9Sstevel@tonic-gate 			continue;
21957c478bd9Sstevel@tonic-gate 		}
21967c478bd9Sstevel@tonic-gate 
21970990bc30Sdg 		if (EQ("proto")) {
21980990bc30Sdg 			next();
21990990bc30Sdg 			if (tokentype != NUMBER)
22000990bc30Sdg 				pr_err("IP proto type expected");
22010990bc30Sdg 			emitop(OP_OFFSET_LINK);
22020990bc30Sdg 			compare_value(IPV4_TYPE_HEADER_OFFSET, 1, tokenval);
22030990bc30Sdg 			emitop(OP_OFFSET_POP);
22040990bc30Sdg 			opstack++;
22057c478bd9Sstevel@tonic-gate 			next();
22067c478bd9Sstevel@tonic-gate 			continue;
22077c478bd9Sstevel@tonic-gate 		}
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 		if (EQ("broadcast")) {
22107c478bd9Sstevel@tonic-gate 			/*
22117c478bd9Sstevel@tonic-gate 			 * Be tricky: FDDI ether dst address begins at
22127c478bd9Sstevel@tonic-gate 			 * byte one.  Since the address is really six
22137c478bd9Sstevel@tonic-gate 			 * bytes long, this works for FDDI & ethernet.
22147c478bd9Sstevel@tonic-gate 			 * XXX - Token ring?
22157c478bd9Sstevel@tonic-gate 			 */
2216605445d5Sdg 			emitop(OP_OFFSET_ZERO);
22177c478bd9Sstevel@tonic-gate 			if (interface->mac_type == DL_IB)
22187c478bd9Sstevel@tonic-gate 				pr_err("filter option unsupported on media");
22197c478bd9Sstevel@tonic-gate 			compare_value(1, 4, 0xffffffff);
2220605445d5Sdg 			emitop(OP_OFFSET_POP);
22217c478bd9Sstevel@tonic-gate 			opstack++;
22227c478bd9Sstevel@tonic-gate 			next();
22237c478bd9Sstevel@tonic-gate 			break;
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 		if (EQ("multicast")) {
22277c478bd9Sstevel@tonic-gate 			/* XXX Token ring? */
2228605445d5Sdg 			emitop(OP_OFFSET_ZERO);
22297c478bd9Sstevel@tonic-gate 			if (interface->mac_type == DL_FDDI) {
22307c478bd9Sstevel@tonic-gate 				compare_value_mask(1, 1, 0x01, 0x01);
22317c478bd9Sstevel@tonic-gate 			} else if (interface->mac_type == DL_IB) {
22327c478bd9Sstevel@tonic-gate 				pr_err("filter option unsupported on media");
22337c478bd9Sstevel@tonic-gate 			} else {
22347c478bd9Sstevel@tonic-gate 				compare_value_mask(0, 1, 0x01, 0x01);
22357c478bd9Sstevel@tonic-gate 			}
2236605445d5Sdg 			emitop(OP_OFFSET_POP);
22377c478bd9Sstevel@tonic-gate 			opstack++;
22387c478bd9Sstevel@tonic-gate 			next();
22397c478bd9Sstevel@tonic-gate 			break;
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 		if (EQ("decnet")) {
22437c478bd9Sstevel@tonic-gate 			/* XXX Token ring? */
22447c478bd9Sstevel@tonic-gate 			if (interface->mac_type == DL_FDDI) {
22457c478bd9Sstevel@tonic-gate 				load_value(19, 2);	/* ether type */
22467c478bd9Sstevel@tonic-gate 				load_const(0x6000);
22477c478bd9Sstevel@tonic-gate 				emitop(OP_GE);
22487c478bd9Sstevel@tonic-gate 				emitop(OP_BRFL);
22497c478bd9Sstevel@tonic-gate 				m = chain(0);
22507c478bd9Sstevel@tonic-gate 				load_value(19, 2);	/* ether type */
22517c478bd9Sstevel@tonic-gate 				load_const(0x6009);
22527c478bd9Sstevel@tonic-gate 				emitop(OP_LE);
22537c478bd9Sstevel@tonic-gate 				resolve_chain(m);
22547c478bd9Sstevel@tonic-gate 			} else {
2255605445d5Sdg 				emitop(OP_OFFSET_ETHERTYPE);
2256605445d5Sdg 				load_value(0, 2);	/* ether type */
22577c478bd9Sstevel@tonic-gate 				load_const(0x6000);
22587c478bd9Sstevel@tonic-gate 				emitop(OP_GE);
22597c478bd9Sstevel@tonic-gate 				emitop(OP_BRFL);
22607c478bd9Sstevel@tonic-gate 				m = chain(0);
2261605445d5Sdg 				load_value(0, 2);	/* ether type */
22627c478bd9Sstevel@tonic-gate 				load_const(0x6009);
22637c478bd9Sstevel@tonic-gate 				emitop(OP_LE);
22647c478bd9Sstevel@tonic-gate 				resolve_chain(m);
2265605445d5Sdg 				emitop(OP_OFFSET_POP);
22667c478bd9Sstevel@tonic-gate 			}
22677c478bd9Sstevel@tonic-gate 			opstack++;
22687c478bd9Sstevel@tonic-gate 			next();
22697c478bd9Sstevel@tonic-gate 			break;
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 
2272605445d5Sdg 		if (EQ("vlan-id")) {
2273605445d5Sdg 			next();
2274605445d5Sdg 			if (tokentype != NUMBER)
2275605445d5Sdg 				pr_err("vlan id expected");
2276605445d5Sdg 			emitop(OP_OFFSET_ZERO);
2277605445d5Sdg 			ethertype_match(ETHERTYPE_VLAN);
2278605445d5Sdg 			emitop(OP_BRFL);
2279605445d5Sdg 			m = chain(0);
2280605445d5Sdg 			compare_value_mask(VLAN_ID_OFFSET, 2, tokenval,
2281605445d5Sdg 			    VLAN_ID_MASK);
2282605445d5Sdg 			resolve_chain(m);
2283605445d5Sdg 			emitop(OP_OFFSET_POP);
2284605445d5Sdg 			opstack++;
2285605445d5Sdg 			next();
2286605445d5Sdg 			break;
2287605445d5Sdg 		}
2288605445d5Sdg 
22897c478bd9Sstevel@tonic-gate 		if (EQ("apple")) {
22907c478bd9Sstevel@tonic-gate 			/*
22917c478bd9Sstevel@tonic-gate 			 * Appletalk also appears in 802.2
22927c478bd9Sstevel@tonic-gate 			 * packets, so check for the ethertypes
22937c478bd9Sstevel@tonic-gate 			 * at offset 12 and 20 in the MAC header.
22947c478bd9Sstevel@tonic-gate 			 */
22957c478bd9Sstevel@tonic-gate 			ethertype_match(ETHERTYPE_AT);
22967c478bd9Sstevel@tonic-gate 			emitop(OP_BRTR);
22977c478bd9Sstevel@tonic-gate 			m = chain(0);
22987c478bd9Sstevel@tonic-gate 			ethertype_match(ETHERTYPE_AARP);
22997c478bd9Sstevel@tonic-gate 			emitop(OP_BRTR);
23007c478bd9Sstevel@tonic-gate 			m = chain(m);
23017c478bd9Sstevel@tonic-gate 			compare_value(20, 2, ETHERTYPE_AT); /* 802.2 */
23027c478bd9Sstevel@tonic-gate 			emitop(OP_BRTR);
23037c478bd9Sstevel@tonic-gate 			m = chain(m);
23047c478bd9Sstevel@tonic-gate 			compare_value(20, 2, ETHERTYPE_AARP); /* 802.2 */
23057c478bd9Sstevel@tonic-gate 			resolve_chain(m);
23067c478bd9Sstevel@tonic-gate 			opstack++;
23077c478bd9Sstevel@tonic-gate 			next();
23087c478bd9Sstevel@tonic-gate 			break;
23097c478bd9Sstevel@tonic-gate 		}
23107c478bd9Sstevel@tonic-gate 
2311605445d5Sdg 		if (EQ("vlan")) {
2312605445d5Sdg 			ethertype_match(ETHERTYPE_VLAN);
2313605445d5Sdg 			compare_value_mask(VLAN_ID_OFFSET, 2, 0, VLAN_ID_MASK);
2314605445d5Sdg 			emitop(OP_NOT);
2315605445d5Sdg 			emitop(OP_AND);
2316605445d5Sdg 			opstack++;
2317605445d5Sdg 			next();
2318605445d5Sdg 			break;
2319605445d5Sdg 		}
2320605445d5Sdg 
23217c478bd9Sstevel@tonic-gate 		if (EQ("bootp") || EQ("dhcp")) {
2322b127ac41SPhilip Kirk 			ethertype_match(interface->network_type_ip);
2323d04ccbb3Scarlsonj 			emitop(OP_BRFL);
2324d04ccbb3Scarlsonj 			m = chain(0);
23257c478bd9Sstevel@tonic-gate 			emitop(OP_OFFSET_LINK);
2326d04ccbb3Scarlsonj 			compare_value(9, 1, IPPROTO_UDP);
2327d04ccbb3Scarlsonj 			emitop(OP_OFFSET_POP);
2328d04ccbb3Scarlsonj 			emitop(OP_BRFL);
2329d04ccbb3Scarlsonj 			m = chain(m);
23307c478bd9Sstevel@tonic-gate 			emitop(OP_OFFSET_IP);
23317c478bd9Sstevel@tonic-gate 			compare_value(0, 4,
2332d04ccbb3Scarlsonj 			    (IPPORT_BOOTPS << 16) | IPPORT_BOOTPC);
23337c478bd9Sstevel@tonic-gate 			emitop(OP_BRTR);
2334d04ccbb3Scarlsonj 			m2 = chain(0);
23357c478bd9Sstevel@tonic-gate 			compare_value(0, 4,
2336d04ccbb3Scarlsonj 			    (IPPORT_BOOTPC << 16) | IPPORT_BOOTPS);
2337d04ccbb3Scarlsonj 			resolve_chain(m2);
2338d04ccbb3Scarlsonj 			emitop(OP_OFFSET_POP);
23397c478bd9Sstevel@tonic-gate 			resolve_chain(m);
2340d04ccbb3Scarlsonj 			opstack++;
2341d04ccbb3Scarlsonj 			dir = ANY;
2342d04ccbb3Scarlsonj 			next();
2343d04ccbb3Scarlsonj 			break;
2344d04ccbb3Scarlsonj 		}
2345d04ccbb3Scarlsonj 
2346d04ccbb3Scarlsonj 		if (EQ("dhcp6")) {
2347b127ac41SPhilip Kirk 			ethertype_match(interface->network_type_ipv6);
2348d04ccbb3Scarlsonj 			emitop(OP_BRFL);
2349d04ccbb3Scarlsonj 			m = chain(0);
2350d04ccbb3Scarlsonj 			emitop(OP_OFFSET_LINK);
2351d04ccbb3Scarlsonj 			compare_value(6, 1, IPPROTO_UDP);
2352605445d5Sdg 			emitop(OP_OFFSET_POP);
2353d04ccbb3Scarlsonj 			emitop(OP_BRFL);
2354d04ccbb3Scarlsonj 			m = chain(m);
2355d04ccbb3Scarlsonj 			emitop(OP_OFFSET_IP);
2356d04ccbb3Scarlsonj 			compare_value(2, 2, IPPORT_DHCPV6S);
2357d04ccbb3Scarlsonj 			emitop(OP_BRTR);
2358d04ccbb3Scarlsonj 			m2 = chain(0);
2359d04ccbb3Scarlsonj 			compare_value(2, 2, IPPORT_DHCPV6C);
2360d04ccbb3Scarlsonj 			resolve_chain(m2);
2361605445d5Sdg 			emitop(OP_OFFSET_POP);
2362d04ccbb3Scarlsonj 			resolve_chain(m);
23637c478bd9Sstevel@tonic-gate 			opstack++;
23647c478bd9Sstevel@tonic-gate 			dir = ANY;
23657c478bd9Sstevel@tonic-gate 			next();
23667c478bd9Sstevel@tonic-gate 			break;
23677c478bd9Sstevel@tonic-gate 		}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 		if (EQ("ethertype")) {
23707c478bd9Sstevel@tonic-gate 			next();
23717c478bd9Sstevel@tonic-gate 			if (tokentype != NUMBER)
23727c478bd9Sstevel@tonic-gate 				pr_err("ether type expected");
23737c478bd9Sstevel@tonic-gate 			ethertype_match(tokenval);
23747c478bd9Sstevel@tonic-gate 			opstack++;
23757c478bd9Sstevel@tonic-gate 			next();
23767c478bd9Sstevel@tonic-gate 			break;
23777c478bd9Sstevel@tonic-gate 		}
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 		if (EQ("pppoe")) {
23807c478bd9Sstevel@tonic-gate 			ethertype_match(ETHERTYPE_PPPOED);
23817c478bd9Sstevel@tonic-gate 			ethertype_match(ETHERTYPE_PPPOES);
23827c478bd9Sstevel@tonic-gate 			emitop(OP_OR);
23837c478bd9Sstevel@tonic-gate 			opstack++;
23847c478bd9Sstevel@tonic-gate 			next();
23857c478bd9Sstevel@tonic-gate 			break;
23867c478bd9Sstevel@tonic-gate 		}
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 		if (EQ("inet")) {
23897c478bd9Sstevel@tonic-gate 			next();
23907c478bd9Sstevel@tonic-gate 			if (EQ("host"))
23917c478bd9Sstevel@tonic-gate 				next();
23927c478bd9Sstevel@tonic-gate 			if (tokentype != ALPHA && tokentype != ADDR_IP)
23937c478bd9Sstevel@tonic-gate 				pr_err("host/IPv4 addr expected after inet");
23947c478bd9Sstevel@tonic-gate 			ipaddr_match(dir, token, IPV4_ONLY);
23957c478bd9Sstevel@tonic-gate 			opstack++;
23967c478bd9Sstevel@tonic-gate 			next();
23977c478bd9Sstevel@tonic-gate 			break;
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		if (EQ("inet6")) {
24017c478bd9Sstevel@tonic-gate 			next();
24027c478bd9Sstevel@tonic-gate 			if (EQ("host"))
24037c478bd9Sstevel@tonic-gate 				next();
24047c478bd9Sstevel@tonic-gate 			if (tokentype != ALPHA && tokentype != ADDR_IP6)
24057c478bd9Sstevel@tonic-gate 				pr_err("host/IPv6 addr expected after inet6");
24067c478bd9Sstevel@tonic-gate 			ipaddr_match(dir, token, IPV6_ONLY);
24077c478bd9Sstevel@tonic-gate 			opstack++;
24087c478bd9Sstevel@tonic-gate 			next();
24097c478bd9Sstevel@tonic-gate 			break;
24107c478bd9Sstevel@tonic-gate 		}
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 		if (EQ("length")) {
24137c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_LENGTH);
24147c478bd9Sstevel@tonic-gate 			opstack++;
24157c478bd9Sstevel@tonic-gate 			next();
24167c478bd9Sstevel@tonic-gate 			break;
24177c478bd9Sstevel@tonic-gate 		}
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 		if (EQ("less")) {
24207c478bd9Sstevel@tonic-gate 			next();
24217c478bd9Sstevel@tonic-gate 			if (tokentype != NUMBER)
24227c478bd9Sstevel@tonic-gate 				pr_err("packet length expected");
24237c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_LENGTH);
24247c478bd9Sstevel@tonic-gate 			load_const(tokenval);
24257c478bd9Sstevel@tonic-gate 			emitop(OP_LT);
24267c478bd9Sstevel@tonic-gate 			opstack++;
24277c478bd9Sstevel@tonic-gate 			next();
24287c478bd9Sstevel@tonic-gate 			break;
24297c478bd9Sstevel@tonic-gate 		}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 		if (EQ("greater")) {
24327c478bd9Sstevel@tonic-gate 			next();
24337c478bd9Sstevel@tonic-gate 			if (tokentype != NUMBER)
24347c478bd9Sstevel@tonic-gate 				pr_err("packet length expected");
24357c478bd9Sstevel@tonic-gate 			emitop(OP_LOAD_LENGTH);
24367c478bd9Sstevel@tonic-gate 			load_const(tokenval);
24377c478bd9Sstevel@tonic-gate 			emitop(OP_GT);
24387c478bd9Sstevel@tonic-gate 			opstack++;
24397c478bd9Sstevel@tonic-gate 			next();
24407c478bd9Sstevel@tonic-gate 			break;
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 		if (EQ("nofrag")) {
24447c478bd9Sstevel@tonic-gate 			emitop(OP_OFFSET_LINK);
24457c478bd9Sstevel@tonic-gate 			compare_value_mask(6, 2, 0, 0x1fff);
24467c478bd9Sstevel@tonic-gate 			emitop(OP_OFFSET_POP);
24477c478bd9Sstevel@tonic-gate 			emitop(OP_BRFL);
24487c478bd9Sstevel@tonic-gate 			m = chain(0);
2449b127ac41SPhilip Kirk 			ethertype_match(interface->network_type_ip);
24507c478bd9Sstevel@tonic-gate 			resolve_chain(m);
24517c478bd9Sstevel@tonic-gate 			opstack++;
24527c478bd9Sstevel@tonic-gate 			next();
24537c478bd9Sstevel@tonic-gate 			break;
24547c478bd9Sstevel@tonic-gate 		}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 		if (EQ("net") || EQ("dstnet") || EQ("srcnet")) {
24577c478bd9Sstevel@tonic-gate 			if (EQ("dstnet"))
24587c478bd9Sstevel@tonic-gate 				dir = TO;
24597c478bd9Sstevel@tonic-gate 			else if (EQ("srcnet"))
24607c478bd9Sstevel@tonic-gate 				dir = FROM;
24617c478bd9Sstevel@tonic-gate 			next();
24627c478bd9Sstevel@tonic-gate 			netaddr_match(dir, token);
24637c478bd9Sstevel@tonic-gate 			dir = ANY;
24647c478bd9Sstevel@tonic-gate 			opstack++;
24657c478bd9Sstevel@tonic-gate 			next();
24667c478bd9Sstevel@tonic-gate 			break;
24677c478bd9Sstevel@tonic-gate 		}
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 		if (EQ("port") || EQ("srcport") || EQ("dstport")) {
24707c478bd9Sstevel@tonic-gate 			if (EQ("dstport"))
24717c478bd9Sstevel@tonic-gate 				dir = TO;
24727c478bd9Sstevel@tonic-gate 			else if (EQ("srcport"))
24737c478bd9Sstevel@tonic-gate 				dir = FROM;
24747c478bd9Sstevel@tonic-gate 			next();
24757c478bd9Sstevel@tonic-gate 			port_match(dir, token);
24767c478bd9Sstevel@tonic-gate 			dir = ANY;
24777c478bd9Sstevel@tonic-gate 			opstack++;
24787c478bd9Sstevel@tonic-gate 			next();
24797c478bd9Sstevel@tonic-gate 			break;
24807c478bd9Sstevel@tonic-gate 		}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 		if (EQ("rpc")) {
24837c478bd9Sstevel@tonic-gate 			uint_t vers, proc;
24847c478bd9Sstevel@tonic-gate 			char savetoken[32];
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 			vers = proc = -1;
24877c478bd9Sstevel@tonic-gate 			next();
24887c478bd9Sstevel@tonic-gate 			(void) strlcpy(savetoken, token, sizeof (savetoken));
24897c478bd9Sstevel@tonic-gate 			next();
24907c478bd9Sstevel@tonic-gate 			if (*token == ',') {
24917c478bd9Sstevel@tonic-gate 				next();
24927c478bd9Sstevel@tonic-gate 				if (tokentype != NUMBER)
24937c478bd9Sstevel@tonic-gate 					pr_err("version number expected");
24947c478bd9Sstevel@tonic-gate 				vers = tokenval;
24957c478bd9Sstevel@tonic-gate 				next();
24967c478bd9Sstevel@tonic-gate 			}
24977c478bd9Sstevel@tonic-gate 			if (*token == ',') {
24987c478bd9Sstevel@tonic-gate 				next();
24997c478bd9Sstevel@tonic-gate 				if (tokentype != NUMBER)
25007c478bd9Sstevel@tonic-gate 					pr_err("proc number expected");
25017c478bd9Sstevel@tonic-gate 				proc = tokenval;
25027c478bd9Sstevel@tonic-gate 				next();
25037c478bd9Sstevel@tonic-gate 			}
25047c478bd9Sstevel@tonic-gate 			rpc_match_prog(dir, savetoken, vers, proc);
25057c478bd9Sstevel@tonic-gate 			dir = ANY;
25067c478bd9Sstevel@tonic-gate 			opstack++;
25077c478bd9Sstevel@tonic-gate 			break;
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 		if (EQ("slp")) {
2511b127ac41SPhilip Kirk 			/* filter out TCP handshakes */
2512b127ac41SPhilip Kirk 			emitop(OP_OFFSET_LINK);
2513b127ac41SPhilip Kirk 			compare_value(9, 1, IPPROTO_TCP);
2514b127ac41SPhilip Kirk 			emitop(OP_LOAD_CONST);
2515b127ac41SPhilip Kirk 			emitval(52);
2516b127ac41SPhilip Kirk 			emitop(OP_LOAD_CONST);
2517b127ac41SPhilip Kirk 			emitval(2);
2518b127ac41SPhilip Kirk 			emitop(OP_LOAD_SHORT);
2519b127ac41SPhilip Kirk 			emitop(OP_GE);
2520b127ac41SPhilip Kirk 			emitop(OP_AND);	/* proto == TCP && len < 52 */
2521b127ac41SPhilip Kirk 			emitop(OP_NOT);
2522b127ac41SPhilip Kirk 			emitop(OP_BRFL); /* pkt too short to be a SLP call */
2523b127ac41SPhilip Kirk 			m = chain(0);
2524b127ac41SPhilip Kirk 
2525b127ac41SPhilip Kirk 			emitop(OP_OFFSET_POP);
2526b127ac41SPhilip Kirk 			emitop(OP_OFFSET_SLP);
2527b127ac41SPhilip Kirk 			resolve_chain(m);
2528b127ac41SPhilip Kirk 			opstack++;
2529b127ac41SPhilip Kirk 			next();
2530b127ac41SPhilip Kirk 			break;
25317c478bd9Sstevel@tonic-gate 		}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 		if (EQ("ldap")) {
25347c478bd9Sstevel@tonic-gate 			dir = ANY;
25357c478bd9Sstevel@tonic-gate 			port_match(dir, "ldap");
25367c478bd9Sstevel@tonic-gate 			opstack++;
25377c478bd9Sstevel@tonic-gate 			next();
25387c478bd9Sstevel@tonic-gate 			break;
25397c478bd9Sstevel@tonic-gate 		}
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 		if (EQ("and") || EQ("or")) {
25427c478bd9Sstevel@tonic-gate 			break;
25437c478bd9Sstevel@tonic-gate 		}
25447c478bd9Sstevel@tonic-gate 
2545b127ac41SPhilip Kirk 		if (EQ("zone")) {
2546b127ac41SPhilip Kirk 			next();
2547b127ac41SPhilip Kirk 			if (tokentype != NUMBER)
2548b127ac41SPhilip Kirk 				pr_err("zoneid expected");
2549b127ac41SPhilip Kirk 			zone_match(dir, BE_64((uint64_t)(tokenval)));
2550b127ac41SPhilip Kirk 			opstack++;
2551b127ac41SPhilip Kirk 			next();
2552b127ac41SPhilip Kirk 			break;
2553b127ac41SPhilip Kirk 		}
2554b127ac41SPhilip Kirk 
25557c478bd9Sstevel@tonic-gate 		if (EQ("gateway")) {
25567c478bd9Sstevel@tonic-gate 			next();
25577c478bd9Sstevel@tonic-gate 			if (eaddr || tokentype != ALPHA)
25587c478bd9Sstevel@tonic-gate 				pr_err("hostname required: %s", token);
25597c478bd9Sstevel@tonic-gate 			etheraddr_match(dir, token);
25607c478bd9Sstevel@tonic-gate 			dir = ANY;
25617c478bd9Sstevel@tonic-gate 			emitop(OP_BRFL);
25627c478bd9Sstevel@tonic-gate 			m = chain(0);
25637c478bd9Sstevel@tonic-gate 			ipaddr_match(dir, token, IPV4_AND_IPV6);
25647c478bd9Sstevel@tonic-gate 			emitop(OP_NOT);
25657c478bd9Sstevel@tonic-gate 			resolve_chain(m);
25667c478bd9Sstevel@tonic-gate 			opstack++;
25677c478bd9Sstevel@tonic-gate 			next();
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 		if (EQ("host") || EQ("between") ||
25717c478bd9Sstevel@tonic-gate 		    tokentype == ALPHA ||	/* assume its a hostname */
25727c478bd9Sstevel@tonic-gate 		    tokentype == ADDR_IP ||
25737c478bd9Sstevel@tonic-gate 		    tokentype == ADDR_IP6 ||
25747c478bd9Sstevel@tonic-gate 		    tokentype == ADDR_AT ||
25757c478bd9Sstevel@tonic-gate 		    tokentype == ADDR_ETHER) {
25767c478bd9Sstevel@tonic-gate 			if (EQ("host") || EQ("between"))
25777c478bd9Sstevel@tonic-gate 				next();
25787c478bd9Sstevel@tonic-gate 			if (eaddr || tokentype == ADDR_ETHER) {
25797c478bd9Sstevel@tonic-gate 				etheraddr_match(dir, token);
25807c478bd9Sstevel@tonic-gate 			} else if (tokentype == ALPHA) {
25817c478bd9Sstevel@tonic-gate 				ipaddr_match(dir, token, IPV4_AND_IPV6);
25827c478bd9Sstevel@tonic-gate 			} else if (tokentype == ADDR_AT) {
25837c478bd9Sstevel@tonic-gate 				ataddr_match(dir, token);
25847c478bd9Sstevel@tonic-gate 			} else if (tokentype == ADDR_IP) {
25857c478bd9Sstevel@tonic-gate 				ipaddr_match(dir, token, IPV4_ONLY);
25867c478bd9Sstevel@tonic-gate 			} else {
25877c478bd9Sstevel@tonic-gate 				ipaddr_match(dir, token, IPV6_ONLY);
25887c478bd9Sstevel@tonic-gate 			}
25897c478bd9Sstevel@tonic-gate 			dir = ANY;
25907c478bd9Sstevel@tonic-gate 			eaddr = 0;
25917c478bd9Sstevel@tonic-gate 			opstack++;
25927c478bd9Sstevel@tonic-gate 			next();
25937c478bd9Sstevel@tonic-gate 			break;
25947c478bd9Sstevel@tonic-gate 		}
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 		if (tokentype == NUMBER) {
25977c478bd9Sstevel@tonic-gate 			load_const(tokenval);
25987c478bd9Sstevel@tonic-gate 			opstack++;
25997c478bd9Sstevel@tonic-gate 			next();
26007c478bd9Sstevel@tonic-gate 			break;
26017c478bd9Sstevel@tonic-gate 		}
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 		break;	/* unknown token */
26047c478bd9Sstevel@tonic-gate 	}
26057c478bd9Sstevel@tonic-gate }
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate struct optable {
26087c478bd9Sstevel@tonic-gate 	char *op_tok;
26097c478bd9Sstevel@tonic-gate 	enum optype op_type;
26107c478bd9Sstevel@tonic-gate };
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate static struct optable
26137c478bd9Sstevel@tonic-gate mulops[] = {
26147c478bd9Sstevel@tonic-gate 	"*",	OP_MUL,
26157c478bd9Sstevel@tonic-gate 	"/",	OP_DIV,
26167c478bd9Sstevel@tonic-gate 	"%",	OP_REM,
26177c478bd9Sstevel@tonic-gate 	"&",	OP_AND,
26187c478bd9Sstevel@tonic-gate 	"",	OP_STOP,
26197c478bd9Sstevel@tonic-gate };
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate static struct optable
26227c478bd9Sstevel@tonic-gate addops[] = {
26237c478bd9Sstevel@tonic-gate 	"+",	OP_ADD,
26247c478bd9Sstevel@tonic-gate 	"-",	OP_SUB,
26257c478bd9Sstevel@tonic-gate 	"|",	OP_OR,
26267c478bd9Sstevel@tonic-gate 	"^",	OP_XOR,
26277c478bd9Sstevel@tonic-gate 	"",	OP_STOP,
26287c478bd9Sstevel@tonic-gate };
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate static struct optable
26317c478bd9Sstevel@tonic-gate compareops[] = {
26327c478bd9Sstevel@tonic-gate 	"==",	OP_EQ,
26337c478bd9Sstevel@tonic-gate 	"=",	OP_EQ,
26347c478bd9Sstevel@tonic-gate 	"!=",	OP_NE,
26357c478bd9Sstevel@tonic-gate 	">",	OP_GT,
26367c478bd9Sstevel@tonic-gate 	">=",	OP_GE,
26377c478bd9Sstevel@tonic-gate 	"<",	OP_LT,
26387c478bd9Sstevel@tonic-gate 	"<=",	OP_LE,
26397c478bd9Sstevel@tonic-gate 	"",	OP_STOP,
26407c478bd9Sstevel@tonic-gate };
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate /*
26437c478bd9Sstevel@tonic-gate  * Using the table, find the operator
26447c478bd9Sstevel@tonic-gate  * that corresponds to the token.
26457c478bd9Sstevel@tonic-gate  * Return 0 if not found.
26467c478bd9Sstevel@tonic-gate  */
26477c478bd9Sstevel@tonic-gate static int
26487c478bd9Sstevel@tonic-gate find_op(char *tok, struct optable *table)
26497c478bd9Sstevel@tonic-gate {
26507c478bd9Sstevel@tonic-gate 	struct optable *op;
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	for (op = table; *op->op_tok; op++) {
26537c478bd9Sstevel@tonic-gate 		if (strcmp(tok, op->op_tok) == 0)
26547c478bd9Sstevel@tonic-gate 			return (op->op_type);
26557c478bd9Sstevel@tonic-gate 	}
26567c478bd9Sstevel@tonic-gate 
26577c478bd9Sstevel@tonic-gate 	return (0);
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate static void
26617c478bd9Sstevel@tonic-gate expr_mul()
26627c478bd9Sstevel@tonic-gate {
26637c478bd9Sstevel@tonic-gate 	int op;
26647c478bd9Sstevel@tonic-gate 	int s = opstack;
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	primary();
26677c478bd9Sstevel@tonic-gate 	while (op = find_op(token, mulops)) {
26687c478bd9Sstevel@tonic-gate 		next();
26697c478bd9Sstevel@tonic-gate 		primary();
26707c478bd9Sstevel@tonic-gate 		checkstack(s + 2);
26717c478bd9Sstevel@tonic-gate 		emitop(op);
26727c478bd9Sstevel@tonic-gate 		opstack--;
26737c478bd9Sstevel@tonic-gate 	}
26747c478bd9Sstevel@tonic-gate }
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate static void
26777c478bd9Sstevel@tonic-gate expr_add()
26787c478bd9Sstevel@tonic-gate {
26797c478bd9Sstevel@tonic-gate 	int op, s = opstack;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	expr_mul();
26827c478bd9Sstevel@tonic-gate 	while (op = find_op(token, addops)) {
26837c478bd9Sstevel@tonic-gate 		next();
26847c478bd9Sstevel@tonic-gate 		expr_mul();
26857c478bd9Sstevel@tonic-gate 		checkstack(s + 2);
26867c478bd9Sstevel@tonic-gate 		emitop(op);
26877c478bd9Sstevel@tonic-gate 		opstack--;
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate }
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate static void
26927c478bd9Sstevel@tonic-gate expr_compare()
26937c478bd9Sstevel@tonic-gate {
26947c478bd9Sstevel@tonic-gate 	int op, s = opstack;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	expr_add();
26977c478bd9Sstevel@tonic-gate 	while (op = find_op(token, compareops)) {
26987c478bd9Sstevel@tonic-gate 		next();
26997c478bd9Sstevel@tonic-gate 		expr_add();
27007c478bd9Sstevel@tonic-gate 		checkstack(s + 2);
27017c478bd9Sstevel@tonic-gate 		emitop(op);
27027c478bd9Sstevel@tonic-gate 		opstack--;
27037c478bd9Sstevel@tonic-gate 	}
27047c478bd9Sstevel@tonic-gate }
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate /*
27077c478bd9Sstevel@tonic-gate  * Alternation ("and") is difficult because
27087c478bd9Sstevel@tonic-gate  * an implied "and" is acknowledge between
27097c478bd9Sstevel@tonic-gate  * two adjacent primaries.  Just keep calling
27107c478bd9Sstevel@tonic-gate  * the lower-level expression routine until
27117c478bd9Sstevel@tonic-gate  * no value is added to the opstack.
27127c478bd9Sstevel@tonic-gate  */
27137c478bd9Sstevel@tonic-gate static void
27147c478bd9Sstevel@tonic-gate alternation()
27157c478bd9Sstevel@tonic-gate {
27167c478bd9Sstevel@tonic-gate 	int m = 0;
27177c478bd9Sstevel@tonic-gate 	int s = opstack;
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	expr_compare();
27207c478bd9Sstevel@tonic-gate 	checkstack(s + 1);
27217c478bd9Sstevel@tonic-gate 	for (;;) {
27227c478bd9Sstevel@tonic-gate 		if (EQ("and"))
27237c478bd9Sstevel@tonic-gate 			next();
27247c478bd9Sstevel@tonic-gate 		emitop(OP_BRFL);
27257c478bd9Sstevel@tonic-gate 		m = chain(m);
27267c478bd9Sstevel@tonic-gate 		expr_compare();
27277c478bd9Sstevel@tonic-gate 		if (opstack != s + 2)
27287c478bd9Sstevel@tonic-gate 			break;
27297c478bd9Sstevel@tonic-gate 		opstack--;
27307c478bd9Sstevel@tonic-gate 	}
27317c478bd9Sstevel@tonic-gate 	unemit(2);
27327c478bd9Sstevel@tonic-gate 	resolve_chain(m);
27337c478bd9Sstevel@tonic-gate }
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate static void
27367c478bd9Sstevel@tonic-gate expression()
27377c478bd9Sstevel@tonic-gate {
27387c478bd9Sstevel@tonic-gate 	int m = 0;
27397c478bd9Sstevel@tonic-gate 	int s = opstack;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	alternation();
27427c478bd9Sstevel@tonic-gate 	while (EQ("or") || EQ(",")) {
27437c478bd9Sstevel@tonic-gate 		emitop(OP_BRTR);
27447c478bd9Sstevel@tonic-gate 		m = chain(m);
27457c478bd9Sstevel@tonic-gate 		next();
27467c478bd9Sstevel@tonic-gate 		alternation();
27477c478bd9Sstevel@tonic-gate 		checkstack(s + 2);
27487c478bd9Sstevel@tonic-gate 		opstack--;
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 	resolve_chain(m);
27517c478bd9Sstevel@tonic-gate }
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate /*
27547c478bd9Sstevel@tonic-gate  * Take n args from the argv list
27557c478bd9Sstevel@tonic-gate  * and concatenate them into a single string.
27567c478bd9Sstevel@tonic-gate  */
27577c478bd9Sstevel@tonic-gate char *
27587c478bd9Sstevel@tonic-gate concat_args(char **argv, int argc)
27597c478bd9Sstevel@tonic-gate {
27607c478bd9Sstevel@tonic-gate 	int i, len;
27617c478bd9Sstevel@tonic-gate 	char *str, *p;
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	/* First add the lengths of all the strings */
27647c478bd9Sstevel@tonic-gate 	len = 0;
27657c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
27667c478bd9Sstevel@tonic-gate 		len += strlen(argv[i]) + 1;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	/* allocate the big string */
27697c478bd9Sstevel@tonic-gate 	str = (char *)malloc(len);
27707c478bd9Sstevel@tonic-gate 	if (str == NULL)
27717c478bd9Sstevel@tonic-gate 		pr_err("no mem");
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	p = str;
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	/*
27767c478bd9Sstevel@tonic-gate 	 * Concat the strings into the big
27777c478bd9Sstevel@tonic-gate 	 * string using a space as separator
27787c478bd9Sstevel@tonic-gate 	 */
27797c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
27807c478bd9Sstevel@tonic-gate 		strcpy(p, argv[i]);
27817c478bd9Sstevel@tonic-gate 		p += strlen(p);
27827c478bd9Sstevel@tonic-gate 		*p++ = ' ';
27837c478bd9Sstevel@tonic-gate 	}
27847c478bd9Sstevel@tonic-gate 	*--p = '\0';
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	return (str);
27877c478bd9Sstevel@tonic-gate }
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate /*
27907c478bd9Sstevel@tonic-gate  * Take the expression in the string "expr"
27917c478bd9Sstevel@tonic-gate  * and compile it into the code array.
27927c478bd9Sstevel@tonic-gate  * Print the generated code if the print
27937c478bd9Sstevel@tonic-gate  * arg is set.
27947c478bd9Sstevel@tonic-gate  */
27957c478bd9Sstevel@tonic-gate void
27967c478bd9Sstevel@tonic-gate compile(char *expr, int print)
27977c478bd9Sstevel@tonic-gate {
27987c478bd9Sstevel@tonic-gate 	expr = strdup(expr);
27997c478bd9Sstevel@tonic-gate 	if (expr == NULL)
28007c478bd9Sstevel@tonic-gate 		pr_err("no mem");
28017c478bd9Sstevel@tonic-gate 	curr_op = oplist;
28027c478bd9Sstevel@tonic-gate 	tkp = expr;
28037c478bd9Sstevel@tonic-gate 	dir = ANY;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	next();
28067c478bd9Sstevel@tonic-gate 	if (tokentype != EOL)
28077c478bd9Sstevel@tonic-gate 		expression();
28087c478bd9Sstevel@tonic-gate 	emitop(OP_STOP);
28097c478bd9Sstevel@tonic-gate 	if (tokentype != EOL)
28107c478bd9Sstevel@tonic-gate 		pr_err("invalid expression");
28117c478bd9Sstevel@tonic-gate 	optimize(oplist);
28127c478bd9Sstevel@tonic-gate 	if (print)
28137c478bd9Sstevel@tonic-gate 		codeprint();
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate /*
28177c478bd9Sstevel@tonic-gate  * Lookup hostname in the arp cache.
28187c478bd9Sstevel@tonic-gate  */
28197c478bd9Sstevel@tonic-gate boolean_t
28207c478bd9Sstevel@tonic-gate arp_for_ether(char *hostname, struct ether_addr *ep)
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	struct arpreq ar;
28237c478bd9Sstevel@tonic-gate 	struct hostent *hp;
28247c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
28257c478bd9Sstevel@tonic-gate 	int error_num;
28267c478bd9Sstevel@tonic-gate 	int s;
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	memset(&ar, 0, sizeof (ar));
28297c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&ar.arp_pa;
28307c478bd9Sstevel@tonic-gate 	sin->sin_family = AF_INET;
28317c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(hostname, AF_INET, 0, &error_num);
28327c478bd9Sstevel@tonic-gate 	if (hp == NULL) {
28337c478bd9Sstevel@tonic-gate 		return (B_FALSE);
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 	memcpy(&sin->sin_addr, hp->h_addr, sizeof (sin->sin_addr));
28367c478bd9Sstevel@tonic-gate 	s = socket(AF_INET, SOCK_DGRAM, 0);
28377c478bd9Sstevel@tonic-gate 	if (s < 0) {
28387c478bd9Sstevel@tonic-gate 		return (B_FALSE);
28397c478bd9Sstevel@tonic-gate 	}
28407c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGARP, &ar) < 0) {
28417c478bd9Sstevel@tonic-gate 		close(s);
28427c478bd9Sstevel@tonic-gate 		return (B_FALSE);
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 	close(s);
28457c478bd9Sstevel@tonic-gate 	memcpy(ep->ether_addr_octet, ar.arp_ha.sa_data, sizeof (*ep));
28467c478bd9Sstevel@tonic-gate 	return (B_TRUE);
28477c478bd9Sstevel@tonic-gate }
2848