xref: /illumos-gate/usr/src/uts/common/io/pfmod.c (revision d9c462b2dd4afc98da9074ea6ace57ecd8035001)
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
5605445d5Sdg199075  * Common Development and Distribution License (the "License").
6605445d5Sdg199075  * 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 /*
22605445d5Sdg199075  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * STREAMS Packet Filter Module
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * This module applies a filter to messages arriving on its read
307c478bd9Sstevel@tonic-gate  * queue, passing on messages that the filter accepts adn discarding
317c478bd9Sstevel@tonic-gate  * the others.  It supports ioctls for setting the filter.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * On the write side, the module simply passes everything through
347c478bd9Sstevel@tonic-gate  * unchanged.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Based on SunOS 4.x version.  This version has minor changes:
377c478bd9Sstevel@tonic-gate  *	- general SVR4 porting stuff
387c478bd9Sstevel@tonic-gate  *	- change name and prefixes from "nit" buffer to streams buffer
397c478bd9Sstevel@tonic-gate  *	- multithreading assumes configured as D_MTQPAIR
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/time.h>
477c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/conf.h>
507c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
537c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
547c478bd9Sstevel@tonic-gate #include <sys/pfmod.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Expanded version of the Packetfilt structure that includes
597c478bd9Sstevel@tonic-gate  * some additional fields that aid filter execution efficiency.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate struct epacketfilt {
627c478bd9Sstevel@tonic-gate 	struct Pf_ext_packetfilt	pf;
637c478bd9Sstevel@tonic-gate #define	pf_Priority	pf.Pf_Priority
647c478bd9Sstevel@tonic-gate #define	pf_FilterLen	pf.Pf_FilterLen
657c478bd9Sstevel@tonic-gate #define	pf_Filter	pf.Pf_Filter
667c478bd9Sstevel@tonic-gate 	/* pointer to word immediately past end of filter */
677c478bd9Sstevel@tonic-gate 	ushort_t		*pf_FilterEnd;
687c478bd9Sstevel@tonic-gate 	/* length in bytes of packet prefix the filter examines */
697c478bd9Sstevel@tonic-gate 	ushort_t		pf_PByteLen;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * (Internal) packet descriptor for FilterPacket
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate struct packdesc {
767c478bd9Sstevel@tonic-gate 	ushort_t	*pd_hdr;	/* header starting address */
777c478bd9Sstevel@tonic-gate 	uint_t		pd_hdrlen;	/* header length in shorts */
787c478bd9Sstevel@tonic-gate 	ushort_t	*pd_body;	/* body starting address */
797c478bd9Sstevel@tonic-gate 	uint_t		pd_bodylen;	/* body length in shorts */
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Function prototypes.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate static	int	pfopen(queue_t *, dev_t *, int, int, cred_t *);
875e1743f0SToomas Soome static	int	pfclose(queue_t *, int, cred_t *);
887c478bd9Sstevel@tonic-gate static void	pfioctl(queue_t *wq, mblk_t *mp);
897c478bd9Sstevel@tonic-gate static	int	FilterPacket(struct packdesc *, struct epacketfilt *);
90*d9c462b2SToomas Soome static int	pfwput(queue_t *, mblk_t *);
91*d9c462b2SToomas Soome static int	pfrput(queue_t *, mblk_t *);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static struct module_info pf_minfo = {
947c478bd9Sstevel@tonic-gate 	22,		/* mi_idnum */
957c478bd9Sstevel@tonic-gate 	"pfmod",	/* mi_idname */
967c478bd9Sstevel@tonic-gate 	0,		/* mi_minpsz */
977c478bd9Sstevel@tonic-gate 	INFPSZ,		/* mi_maxpsz */
987c478bd9Sstevel@tonic-gate 	0,		/* mi_hiwat */
997c478bd9Sstevel@tonic-gate 	0		/* mi_lowat */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static struct qinit pf_rinit = {
103*d9c462b2SToomas Soome 	pfrput,			/* qi_putp */
1047c478bd9Sstevel@tonic-gate 	NULL,
1057c478bd9Sstevel@tonic-gate 	pfopen,			/* qi_qopen */
1067c478bd9Sstevel@tonic-gate 	pfclose,		/* qi_qclose */
1077c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1087c478bd9Sstevel@tonic-gate 	&pf_minfo,		/* qi_minfo */
1097c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct qinit pf_winit = {
113*d9c462b2SToomas Soome 	pfwput,			/* qi_putp */
1147c478bd9Sstevel@tonic-gate 	NULL,			/* qi_srvp */
1157c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qopen */
1167c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qclose */
1177c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
1187c478bd9Sstevel@tonic-gate 	&pf_minfo,		/* qi_minfo */
1197c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
1207c478bd9Sstevel@tonic-gate };
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static struct streamtab pf_info = {
1237c478bd9Sstevel@tonic-gate 	&pf_rinit,	/* st_rdinit */
1247c478bd9Sstevel@tonic-gate 	&pf_winit,	/* st_wrinit */
1257c478bd9Sstevel@tonic-gate 	NULL,		/* st_muxrinit */
1267c478bd9Sstevel@tonic-gate 	NULL		/* st_muxwinit */
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static struct fmodsw fsw = {
1307c478bd9Sstevel@tonic-gate 	"pfmod",
1317c478bd9Sstevel@tonic-gate 	&pf_info,
1327c478bd9Sstevel@tonic-gate 	D_MTQPAIR | D_MP
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
1367c478bd9Sstevel@tonic-gate 	&mod_strmodops, "streams packet filter module", &fsw
1377c478bd9Sstevel@tonic-gate };
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1407c478bd9Sstevel@tonic-gate 	MODREV_1, &modlstrmod, NULL
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
_init(void)1447c478bd9Sstevel@tonic-gate _init(void)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int
_fini(void)1507c478bd9Sstevel@tonic-gate _fini(void)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1567c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1627c478bd9Sstevel@tonic-gate static int
pfopen(queue_t * rq,dev_t * dev,int oflag,int sflag,cred_t * crp)1637c478bd9Sstevel@tonic-gate pfopen(queue_t *rq, dev_t *dev, int oflag, int sflag, cred_t *crp)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct epacketfilt	*pfp;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	ASSERT(rq);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (sflag != MODOPEN)
1707c478bd9Sstevel@tonic-gate 		return (EINVAL);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (rq->q_ptr)
1737c478bd9Sstevel@tonic-gate 		return (0);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Allocate and initialize per-Stream structure.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	pfp = kmem_alloc(sizeof (struct epacketfilt), KM_SLEEP);
1797c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = (char *)pfp;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	qprocson(rq);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (0);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1865e1743f0SToomas Soome /* ARGSUSED */
1877c478bd9Sstevel@tonic-gate static int
pfclose(queue_t * rq,int flags __unused,cred_t * credp __unused)1885e1743f0SToomas Soome pfclose(queue_t	*rq, int flags __unused, cred_t *credp __unused)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	struct	epacketfilt	*pfp = (struct epacketfilt *)rq->q_ptr;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	ASSERT(pfp);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	qprocsoff(rq);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	kmem_free(pfp, sizeof (struct epacketfilt));
1977c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = NULL;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Write-side put procedure.  Its main task is to detect ioctls.
2047c478bd9Sstevel@tonic-gate  * Other message types are passed on through.
2057c478bd9Sstevel@tonic-gate  */
206*d9c462b2SToomas Soome static int
pfwput(queue_t * wq,mblk_t * mp)2077c478bd9Sstevel@tonic-gate pfwput(queue_t *wq, mblk_t *mp)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
2107c478bd9Sstevel@tonic-gate 	case M_IOCTL:
2117c478bd9Sstevel@tonic-gate 		pfioctl(wq, mp);
2127c478bd9Sstevel@tonic-gate 		break;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	default:
2157c478bd9Sstevel@tonic-gate 		putnext(wq, mp);
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 	}
218*d9c462b2SToomas Soome 	return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * Read-side put procedure.  It's responsible for applying the
2237c478bd9Sstevel@tonic-gate  * packet filter and passing upstream message on or discarding it
2247c478bd9Sstevel@tonic-gate  * depending upon the results.
2257c478bd9Sstevel@tonic-gate  *
2267c478bd9Sstevel@tonic-gate  * Upstream messages can start with zero or more M_PROTO mblks
2277c478bd9Sstevel@tonic-gate  * which are skipped over before executing the packet filter
2287c478bd9Sstevel@tonic-gate  * on any remaining M_DATA mblks.
2297c478bd9Sstevel@tonic-gate  */
230*d9c462b2SToomas Soome static int
pfrput(queue_t * rq,mblk_t * mp)2317c478bd9Sstevel@tonic-gate pfrput(queue_t *rq, mblk_t *mp)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	struct	epacketfilt	*pfp = (struct epacketfilt *)rq->q_ptr;
2347c478bd9Sstevel@tonic-gate 	mblk_t	*mbp, *mpp;
2357c478bd9Sstevel@tonic-gate 	struct	packdesc	pd;
2367c478bd9Sstevel@tonic-gate 	int	need;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	ASSERT(pfp);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
2417c478bd9Sstevel@tonic-gate 	case M_PROTO:
2427c478bd9Sstevel@tonic-gate 	case M_DATA:
2437c478bd9Sstevel@tonic-gate 		/*
2447c478bd9Sstevel@tonic-gate 		 * Skip over protocol information and find the start
2457c478bd9Sstevel@tonic-gate 		 * of the message body, saving the overall message
2467c478bd9Sstevel@tonic-gate 		 * start in mpp.
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		for (mpp = mp; mp && (DB_TYPE(mp) == M_PROTO); mp = mp->b_cont)
2497c478bd9Sstevel@tonic-gate 			;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		/*
2527c478bd9Sstevel@tonic-gate 		 * Null body (exclusive of M_PROTO blocks) ==> accept.
2537c478bd9Sstevel@tonic-gate 		 * Note that a null body is not the same as an empty body.
2547c478bd9Sstevel@tonic-gate 		 */
2557c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
2567c478bd9Sstevel@tonic-gate 			putnext(rq, mpp);
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		/*
2617c478bd9Sstevel@tonic-gate 		 * Pull the packet up to the length required by
2627c478bd9Sstevel@tonic-gate 		 * the filter.  Note that doing so destroys sharing
2637c478bd9Sstevel@tonic-gate 		 * relationships, which is unfortunate, since the
2647c478bd9Sstevel@tonic-gate 		 * results of pulling up here are likely to be useful
2657c478bd9Sstevel@tonic-gate 		 * for shared messages applied to a filter on a sibling
2667c478bd9Sstevel@tonic-gate 		 * stream.
2677c478bd9Sstevel@tonic-gate 		 *
2687c478bd9Sstevel@tonic-gate 		 * Most packet sources will provide the packet in two
2697c478bd9Sstevel@tonic-gate 		 * logical pieces: an initial header in a single mblk,
2707c478bd9Sstevel@tonic-gate 		 * and a body in a sequence of mblks hooked to the
2717c478bd9Sstevel@tonic-gate 		 * header.  We're prepared to deal with variant forms,
2727c478bd9Sstevel@tonic-gate 		 * but in any case, the pullup applies only to the body
2737c478bd9Sstevel@tonic-gate 		 * part.
2747c478bd9Sstevel@tonic-gate 		 */
2757c478bd9Sstevel@tonic-gate 		mbp = mp->b_cont;
2767c478bd9Sstevel@tonic-gate 		need = pfp->pf_PByteLen;
2777c478bd9Sstevel@tonic-gate 		if (mbp && (MBLKL(mbp) < need)) {
2787c478bd9Sstevel@tonic-gate 			int len = msgdsize(mbp);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			/* XXX discard silently on pullupmsg failure */
2817c478bd9Sstevel@tonic-gate 			if (pullupmsg(mbp, MIN(need, len)) == 0) {
2827c478bd9Sstevel@tonic-gate 				freemsg(mpp);
2837c478bd9Sstevel@tonic-gate 				break;
2847c478bd9Sstevel@tonic-gate 			}
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/*
2887c478bd9Sstevel@tonic-gate 		 * Misalignment (not on short boundary) ==> reject.
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		if (((uintptr_t)mp->b_rptr & (sizeof (ushort_t) - 1)) ||
2917c478bd9Sstevel@tonic-gate 		    (mbp != NULL &&
2927c478bd9Sstevel@tonic-gate 		    ((uintptr_t)mbp->b_rptr & (sizeof (ushort_t) - 1)))) {
2937c478bd9Sstevel@tonic-gate 			freemsg(mpp);
2947c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * These assignments are distasteful, but necessary,
2997c478bd9Sstevel@tonic-gate 		 * since the packet filter wants to work in terms of
3007c478bd9Sstevel@tonic-gate 		 * shorts.  Odd bytes at the end of header or data can't
3017c478bd9Sstevel@tonic-gate 		 * participate in the filtering operation.
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		pd.pd_hdr = (ushort_t *)mp->b_rptr;
3047c478bd9Sstevel@tonic-gate 		pd.pd_hdrlen = (mp->b_wptr - mp->b_rptr) / sizeof (ushort_t);
3057c478bd9Sstevel@tonic-gate 		if (mbp) {
3067c478bd9Sstevel@tonic-gate 			pd.pd_body = (ushort_t *)mbp->b_rptr;
3077c478bd9Sstevel@tonic-gate 			pd.pd_bodylen = (mbp->b_wptr - mbp->b_rptr) /
3087c478bd9Sstevel@tonic-gate 			    sizeof (ushort_t);
3097c478bd9Sstevel@tonic-gate 		} else {
3107c478bd9Sstevel@tonic-gate 			pd.pd_body = NULL;
3117c478bd9Sstevel@tonic-gate 			pd.pd_bodylen = 0;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Apply the filter.
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 		if (FilterPacket(&pd, pfp))
3187c478bd9Sstevel@tonic-gate 			putnext(rq, mpp);
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 			freemsg(mpp);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		break;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	default:
3257c478bd9Sstevel@tonic-gate 		putnext(rq, mp);
3267c478bd9Sstevel@tonic-gate 		break;
3277c478bd9Sstevel@tonic-gate 	}
328*d9c462b2SToomas Soome 	return (0);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * Handle write-side M_IOCTL messages.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate static void
pfioctl(queue_t * wq,mblk_t * mp)3357c478bd9Sstevel@tonic-gate pfioctl(queue_t *wq, mblk_t *mp)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	struct	epacketfilt	*pfp = (struct epacketfilt *)wq->q_ptr;
3387c478bd9Sstevel@tonic-gate 	struct	Pf_ext_packetfilt	*upfp;
3397c478bd9Sstevel@tonic-gate 	struct	packetfilt	*opfp;
3407c478bd9Sstevel@tonic-gate 	ushort_t	*fwp;
341605445d5Sdg199075 	int	arg;
342605445d5Sdg199075 	int	maxoff = 0;
343605445d5Sdg199075 	int	maxoffreg = 0;
3447c478bd9Sstevel@tonic-gate 	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
3457c478bd9Sstevel@tonic-gate 	int	error;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	switch (iocp->ioc_cmd) {
3487c478bd9Sstevel@tonic-gate 	case PFIOCSETF:
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * Verify argument length. Since the size of packet filter
3517c478bd9Sstevel@tonic-gate 		 * got increased (ENMAXFILTERS was bumped up to 2047), to
3527c478bd9Sstevel@tonic-gate 		 * maintain backwards binary compatibility, we need to
3537c478bd9Sstevel@tonic-gate 		 * check for both possible sizes.
3547c478bd9Sstevel@tonic-gate 		 */
3557c478bd9Sstevel@tonic-gate 		switch (iocp->ioc_count) {
3567c478bd9Sstevel@tonic-gate 		case sizeof (struct Pf_ext_packetfilt):
3577c478bd9Sstevel@tonic-gate 			error = miocpullup(mp,
3587c478bd9Sstevel@tonic-gate 			    sizeof (struct Pf_ext_packetfilt));
3597c478bd9Sstevel@tonic-gate 			if (error != 0) {
3607c478bd9Sstevel@tonic-gate 				miocnak(wq, mp, 0, error);
3617c478bd9Sstevel@tonic-gate 				return;
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 			upfp = (struct Pf_ext_packetfilt *)mp->b_cont->b_rptr;
3647c478bd9Sstevel@tonic-gate 			if (upfp->Pf_FilterLen > PF_MAXFILTERS) {
3657c478bd9Sstevel@tonic-gate 				miocnak(wq, mp, 0, EINVAL);
3667c478bd9Sstevel@tonic-gate 				return;
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 			bcopy(upfp, pfp, sizeof (struct Pf_ext_packetfilt));
3707c478bd9Sstevel@tonic-gate 			pfp->pf_FilterEnd = &pfp->pf_Filter[pfp->pf_FilterLen];
3717c478bd9Sstevel@tonic-gate 			break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		case sizeof (struct packetfilt):
3747c478bd9Sstevel@tonic-gate 			error = miocpullup(mp, sizeof (struct packetfilt));
3757c478bd9Sstevel@tonic-gate 			if (error != 0) {
3767c478bd9Sstevel@tonic-gate 				miocnak(wq, mp, 0, error);
3777c478bd9Sstevel@tonic-gate 				return;
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 			opfp = (struct packetfilt *)mp->b_cont->b_rptr;
3807c478bd9Sstevel@tonic-gate 			/* this strange comparison keeps gcc from complaining */
3817c478bd9Sstevel@tonic-gate 			if (opfp->Pf_FilterLen - 1 >= ENMAXFILTERS) {
3827c478bd9Sstevel@tonic-gate 				miocnak(wq, mp, 0, EINVAL);
3837c478bd9Sstevel@tonic-gate 				return;
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 			pfp->pf.Pf_Priority = opfp->Pf_Priority;
3877c478bd9Sstevel@tonic-gate 			pfp->pf.Pf_FilterLen = (unsigned int)opfp->Pf_FilterLen;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			bcopy(opfp->Pf_Filter, pfp->pf.Pf_Filter,
3907c478bd9Sstevel@tonic-gate 			    sizeof (opfp->Pf_Filter));
3917c478bd9Sstevel@tonic-gate 			pfp->pf_FilterEnd = &pfp->pf_Filter[pfp->pf_FilterLen];
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		default:
3957c478bd9Sstevel@tonic-gate 			miocnak(wq, mp, 0, EINVAL);
3967c478bd9Sstevel@tonic-gate 			return;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		/*
4007c478bd9Sstevel@tonic-gate 		 * Find and record maximum byte offset that the
4017c478bd9Sstevel@tonic-gate 		 * filter users.  We use this when executing the
4027c478bd9Sstevel@tonic-gate 		 * filter to determine how much of the packet
4037c478bd9Sstevel@tonic-gate 		 * body to pull up.  This code depends on the
4047c478bd9Sstevel@tonic-gate 		 * filter encoding.
4057c478bd9Sstevel@tonic-gate 		 */
4067c478bd9Sstevel@tonic-gate 		for (fwp = pfp->pf_Filter; fwp < pfp->pf_FilterEnd; fwp++) {
4077c478bd9Sstevel@tonic-gate 			arg = *fwp & ((1 << ENF_NBPA) - 1);
4087c478bd9Sstevel@tonic-gate 			switch (arg) {
4097c478bd9Sstevel@tonic-gate 			default:
4107c478bd9Sstevel@tonic-gate 				if ((arg -= ENF_PUSHWORD) > maxoff)
4117c478bd9Sstevel@tonic-gate 					maxoff = arg;
4127c478bd9Sstevel@tonic-gate 				break;
4137c478bd9Sstevel@tonic-gate 
414605445d5Sdg199075 			case ENF_LOAD_OFFSET:
415605445d5Sdg199075 				/* Point to the offset */
416605445d5Sdg199075 				fwp++;
417605445d5Sdg199075 				if (*fwp > maxoffreg)
418605445d5Sdg199075 					maxoffreg = *fwp;
419605445d5Sdg199075 				break;
420605445d5Sdg199075 
4217c478bd9Sstevel@tonic-gate 			case ENF_PUSHLIT:
422605445d5Sdg199075 			case ENF_BRTR:
423605445d5Sdg199075 			case ENF_BRFL:
4247c478bd9Sstevel@tonic-gate 				/* Skip over the literal. */
4257c478bd9Sstevel@tonic-gate 				fwp++;
4267c478bd9Sstevel@tonic-gate 				break;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 			case ENF_PUSHZERO:
4297c478bd9Sstevel@tonic-gate 			case ENF_PUSHONE:
4307c478bd9Sstevel@tonic-gate 			case ENF_PUSHFFFF:
4317c478bd9Sstevel@tonic-gate 			case ENF_PUSHFF00:
4327c478bd9Sstevel@tonic-gate 			case ENF_PUSH00FF:
4337c478bd9Sstevel@tonic-gate 			case ENF_NOPUSH:
434605445d5Sdg199075 			case ENF_POP:
4357c478bd9Sstevel@tonic-gate 				break;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		/*
4407c478bd9Sstevel@tonic-gate 		 * Convert word offset to length in bytes.
4417c478bd9Sstevel@tonic-gate 		 */
442605445d5Sdg199075 		pfp->pf_PByteLen = (maxoff + maxoffreg + 1) * sizeof (ushort_t);
4437c478bd9Sstevel@tonic-gate 		miocack(wq, mp, 0, 0);
4447c478bd9Sstevel@tonic-gate 		break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	default:
4477c478bd9Sstevel@tonic-gate 		putnext(wq, mp);
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /* #define	DEBUG	1 */
4537c478bd9Sstevel@tonic-gate /* #define	INNERDEBUG	1 */
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate #ifdef	INNERDEBUG
456605445d5Sdg199075 #define	enprintf(a)	printf a
457605445d5Sdg199075 #else
458605445d5Sdg199075 #define	enprintf(a)
459605445d5Sdg199075 #endif
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * Apply the packet filter given by pfp to the packet given by
4637c478bd9Sstevel@tonic-gate  * pp.  Return nonzero iff the filter accepts the packet.
4647c478bd9Sstevel@tonic-gate  *
4657c478bd9Sstevel@tonic-gate  * The packet comes in two pieces, a header and a body, since
4667c478bd9Sstevel@tonic-gate  * that's the most convenient form for our caller.  The header
4677c478bd9Sstevel@tonic-gate  * is in contiguous memory, whereas the body is in a mbuf.
4687c478bd9Sstevel@tonic-gate  * Our caller will have adjusted the mbuf chain so that its first
4697c478bd9Sstevel@tonic-gate  * min(MLEN, length(body)) bytes are guaranteed contiguous.  For
4707c478bd9Sstevel@tonic-gate  * the sake of efficiency (and some laziness) the filter is prepared
4717c478bd9Sstevel@tonic-gate  * to examine only these two contiguous pieces.  Furthermore, it
4727c478bd9Sstevel@tonic-gate  * assumes that the header length is even, so that there's no need
4737c478bd9Sstevel@tonic-gate  * to glue the last byte of header to the first byte of data.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate #define	opx(i)	((i) >> ENF_NBPA)
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static int
FilterPacket(struct packdesc * pp,struct epacketfilt * pfp)4797c478bd9Sstevel@tonic-gate FilterPacket(struct packdesc *pp, struct epacketfilt *pfp)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	int		maxhdr = pp->pd_hdrlen;
4827c478bd9Sstevel@tonic-gate 	int		maxword = maxhdr + pp->pd_bodylen;
4837c478bd9Sstevel@tonic-gate 	ushort_t	*sp;
4847c478bd9Sstevel@tonic-gate 	ushort_t	*fp;
4857c478bd9Sstevel@tonic-gate 	ushort_t	*fpe;
4867c478bd9Sstevel@tonic-gate 	unsigned	op;
4877c478bd9Sstevel@tonic-gate 	unsigned	arg;
488605445d5Sdg199075 	unsigned	offreg = 0;
4897c478bd9Sstevel@tonic-gate 	ushort_t	stack[ENMAXFILTERS+1];
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	fp = &pfp->pf_Filter[0];
4927c478bd9Sstevel@tonic-gate 	fpe = pfp->pf_FilterEnd;
4937c478bd9Sstevel@tonic-gate 
494605445d5Sdg199075 	enprintf(("FilterPacket(%p, %p, %p, %p):\n", pp, pfp, fp, fpe));
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * Push TRUE on stack to start.  The stack size is chosen such
4987c478bd9Sstevel@tonic-gate 	 * that overflow can't occur -- each operation can push at most
4997c478bd9Sstevel@tonic-gate 	 * one item on the stack, and the stack size equals the maximum
5007c478bd9Sstevel@tonic-gate 	 * program length.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	sp = &stack[ENMAXFILTERS];
5037c478bd9Sstevel@tonic-gate 	*sp = 1;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	while (fp < fpe) {
5067c478bd9Sstevel@tonic-gate 	op = *fp >> ENF_NBPA;
5077c478bd9Sstevel@tonic-gate 	arg = *fp & ((1 << ENF_NBPA) - 1);
5087c478bd9Sstevel@tonic-gate 	fp++;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	switch (arg) {
5117c478bd9Sstevel@tonic-gate 	default:
5127c478bd9Sstevel@tonic-gate 		arg -= ENF_PUSHWORD;
5137c478bd9Sstevel@tonic-gate 		/*
5147c478bd9Sstevel@tonic-gate 		 * Since arg is unsigned,
5157c478bd9Sstevel@tonic-gate 		 * if it were less than ENF_PUSHWORD before,
5167c478bd9Sstevel@tonic-gate 		 * it would now be huge.
5177c478bd9Sstevel@tonic-gate 		 */
518605445d5Sdg199075 		if (arg + offreg < maxhdr)
519605445d5Sdg199075 			*--sp = pp->pd_hdr[arg + offreg];
520605445d5Sdg199075 		else if (arg + offreg < maxword)
521605445d5Sdg199075 			*--sp = pp->pd_body[arg - maxhdr + offreg];
5227c478bd9Sstevel@tonic-gate 		else {
523605445d5Sdg199075 			enprintf(("=>0(len)\n"));
5247c478bd9Sstevel@tonic-gate 			return (0);
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		break;
5277c478bd9Sstevel@tonic-gate 	case ENF_PUSHLIT:
5287c478bd9Sstevel@tonic-gate 		*--sp = *fp++;
5297c478bd9Sstevel@tonic-gate 		break;
5307c478bd9Sstevel@tonic-gate 	case ENF_PUSHZERO:
5317c478bd9Sstevel@tonic-gate 		*--sp = 0;
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 	case ENF_PUSHONE:
5347c478bd9Sstevel@tonic-gate 		*--sp = 1;
5357c478bd9Sstevel@tonic-gate 		break;
5367c478bd9Sstevel@tonic-gate 	case ENF_PUSHFFFF:
5377c478bd9Sstevel@tonic-gate 		*--sp = 0xffff;
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 	case ENF_PUSHFF00:
5407c478bd9Sstevel@tonic-gate 		*--sp = 0xff00;
5417c478bd9Sstevel@tonic-gate 		break;
5427c478bd9Sstevel@tonic-gate 	case ENF_PUSH00FF:
5437c478bd9Sstevel@tonic-gate 		*--sp = 0x00ff;
5447c478bd9Sstevel@tonic-gate 		break;
545605445d5Sdg199075 	case ENF_LOAD_OFFSET:
546605445d5Sdg199075 		offreg = *fp++;
547605445d5Sdg199075 		break;
548605445d5Sdg199075 	case ENF_BRTR:
549605445d5Sdg199075 		if (*sp != 0)
550605445d5Sdg199075 			fp += *fp;
551605445d5Sdg199075 		else
552605445d5Sdg199075 			fp++;
553605445d5Sdg199075 		if (fp >= fpe) {
554605445d5Sdg199075 			enprintf(("BRTR: fp>=fpe\n"));
555605445d5Sdg199075 			return (0);
556605445d5Sdg199075 		}
557605445d5Sdg199075 		break;
558605445d5Sdg199075 	case ENF_BRFL:
559605445d5Sdg199075 		if (*sp == 0)
560605445d5Sdg199075 			fp += *fp;
561605445d5Sdg199075 		else
562605445d5Sdg199075 			fp++;
563605445d5Sdg199075 		if (fp >= fpe) {
564605445d5Sdg199075 			enprintf(("BRFL: fp>=fpe\n"));
565605445d5Sdg199075 			return (0);
566605445d5Sdg199075 		}
567605445d5Sdg199075 		break;
568605445d5Sdg199075 	case ENF_POP:
569605445d5Sdg199075 		++sp;
570605445d5Sdg199075 		if (sp > &stack[ENMAXFILTERS]) {
571605445d5Sdg199075 			enprintf(("stack underflow\n"));
572605445d5Sdg199075 			return (0);
573605445d5Sdg199075 		}
574605445d5Sdg199075 		break;
5757c478bd9Sstevel@tonic-gate 	case ENF_NOPUSH:
5767c478bd9Sstevel@tonic-gate 		break;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if (sp < &stack[2]) {	/* check stack overflow: small yellow zone */
580605445d5Sdg199075 		enprintf(("=>0(--sp)\n"));
5817c478bd9Sstevel@tonic-gate 		return (0);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (op == ENF_NOP)
5857c478bd9Sstevel@tonic-gate 		continue;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	/*
5887c478bd9Sstevel@tonic-gate 	 * all non-NOP operators binary, must have at least two operands
5897c478bd9Sstevel@tonic-gate 	 * on stack to evaluate.
5907c478bd9Sstevel@tonic-gate 	 */
5917c478bd9Sstevel@tonic-gate 	if (sp > &stack[ENMAXFILTERS-2]) {
592605445d5Sdg199075 		enprintf(("=>0(sp++)\n"));
5937c478bd9Sstevel@tonic-gate 		return (0);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	arg = *sp++;
5977c478bd9Sstevel@tonic-gate 	switch (op) {
5987c478bd9Sstevel@tonic-gate 	default:
599605445d5Sdg199075 		enprintf(("=>0(def)\n"));
6007c478bd9Sstevel@tonic-gate 		return (0);
6017c478bd9Sstevel@tonic-gate 	case opx(ENF_AND):
6027c478bd9Sstevel@tonic-gate 		*sp &= arg;
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	case opx(ENF_OR):
6057c478bd9Sstevel@tonic-gate 		*sp |= arg;
6067c478bd9Sstevel@tonic-gate 		break;
6077c478bd9Sstevel@tonic-gate 	case opx(ENF_XOR):
6087c478bd9Sstevel@tonic-gate 		*sp ^= arg;
6097c478bd9Sstevel@tonic-gate 		break;
6107c478bd9Sstevel@tonic-gate 	case opx(ENF_EQ):
6117c478bd9Sstevel@tonic-gate 		*sp = (*sp == arg);
6127c478bd9Sstevel@tonic-gate 		break;
6137c478bd9Sstevel@tonic-gate 	case opx(ENF_NEQ):
6147c478bd9Sstevel@tonic-gate 		*sp = (*sp != arg);
6157c478bd9Sstevel@tonic-gate 		break;
6167c478bd9Sstevel@tonic-gate 	case opx(ENF_LT):
6177c478bd9Sstevel@tonic-gate 		*sp = (*sp < arg);
6187c478bd9Sstevel@tonic-gate 		break;
6197c478bd9Sstevel@tonic-gate 	case opx(ENF_LE):
6207c478bd9Sstevel@tonic-gate 		*sp = (*sp <= arg);
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 	case opx(ENF_GT):
6237c478bd9Sstevel@tonic-gate 		*sp = (*sp > arg);
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 	case opx(ENF_GE):
6267c478bd9Sstevel@tonic-gate 		*sp = (*sp >= arg);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/* short-circuit operators */
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	case opx(ENF_COR):
6327c478bd9Sstevel@tonic-gate 		if (*sp++ == arg) {
633605445d5Sdg199075 			enprintf(("=>COR %x\n", *sp));
6347c478bd9Sstevel@tonic-gate 			return (1);
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 		break;
6377c478bd9Sstevel@tonic-gate 	case opx(ENF_CAND):
6387c478bd9Sstevel@tonic-gate 		if (*sp++ != arg) {
639605445d5Sdg199075 			enprintf(("=>CAND %x\n", *sp));
6407c478bd9Sstevel@tonic-gate 			return (0);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 		break;
6437c478bd9Sstevel@tonic-gate 	case opx(ENF_CNOR):
6447c478bd9Sstevel@tonic-gate 		if (*sp++ == arg) {
645605445d5Sdg199075 			enprintf(("=>COR %x\n", *sp));
6467c478bd9Sstevel@tonic-gate 			return (0);
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 		break;
6497c478bd9Sstevel@tonic-gate 	case opx(ENF_CNAND):
6507c478bd9Sstevel@tonic-gate 		if (*sp++ != arg) {
651605445d5Sdg199075 			enprintf(("=>CNAND %x\n", *sp));
6527c478bd9Sstevel@tonic-gate 			return (1);
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 		break;
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 	}
657605445d5Sdg199075 	enprintf(("=>%x\n", *sp));
6587c478bd9Sstevel@tonic-gate 	return (*sp);
6597c478bd9Sstevel@tonic-gate }
660