10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2760Sdg199075 * Common Development and Distribution License (the "License").
6*2760Sdg199075 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*2760Sdg199075 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * STREAMS Packet Filter Module
300Sstevel@tonic-gate *
310Sstevel@tonic-gate * This module applies a filter to messages arriving on its read
320Sstevel@tonic-gate * queue, passing on messages that the filter accepts adn discarding
330Sstevel@tonic-gate * the others. It supports ioctls for setting the filter.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * On the write side, the module simply passes everything through
360Sstevel@tonic-gate * unchanged.
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * Based on SunOS 4.x version. This version has minor changes:
390Sstevel@tonic-gate * - general SVR4 porting stuff
400Sstevel@tonic-gate * - change name and prefixes from "nit" buffer to streams buffer
410Sstevel@tonic-gate * - multithreading assumes configured as D_MTQPAIR
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/sysmacros.h>
460Sstevel@tonic-gate #include <sys/errno.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/time.h>
490Sstevel@tonic-gate #include <sys/stropts.h>
500Sstevel@tonic-gate #include <sys/stream.h>
510Sstevel@tonic-gate #include <sys/conf.h>
520Sstevel@tonic-gate #include <sys/ddi.h>
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <sys/kmem.h>
550Sstevel@tonic-gate #include <sys/strsun.h>
560Sstevel@tonic-gate #include <sys/pfmod.h>
570Sstevel@tonic-gate #include <sys/modctl.h>
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * Expanded version of the Packetfilt structure that includes
610Sstevel@tonic-gate * some additional fields that aid filter execution efficiency.
620Sstevel@tonic-gate */
630Sstevel@tonic-gate struct epacketfilt {
640Sstevel@tonic-gate struct Pf_ext_packetfilt pf;
650Sstevel@tonic-gate #define pf_Priority pf.Pf_Priority
660Sstevel@tonic-gate #define pf_FilterLen pf.Pf_FilterLen
670Sstevel@tonic-gate #define pf_Filter pf.Pf_Filter
680Sstevel@tonic-gate /* pointer to word immediately past end of filter */
690Sstevel@tonic-gate ushort_t *pf_FilterEnd;
700Sstevel@tonic-gate /* length in bytes of packet prefix the filter examines */
710Sstevel@tonic-gate ushort_t pf_PByteLen;
720Sstevel@tonic-gate };
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * (Internal) packet descriptor for FilterPacket
760Sstevel@tonic-gate */
770Sstevel@tonic-gate struct packdesc {
780Sstevel@tonic-gate ushort_t *pd_hdr; /* header starting address */
790Sstevel@tonic-gate uint_t pd_hdrlen; /* header length in shorts */
800Sstevel@tonic-gate ushort_t *pd_body; /* body starting address */
810Sstevel@tonic-gate uint_t pd_bodylen; /* body length in shorts */
820Sstevel@tonic-gate };
830Sstevel@tonic-gate
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Function prototypes.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate static int pfopen(queue_t *, dev_t *, int, int, cred_t *);
890Sstevel@tonic-gate static int pfclose(queue_t *);
900Sstevel@tonic-gate static void pfioctl(queue_t *wq, mblk_t *mp);
910Sstevel@tonic-gate static int FilterPacket(struct packdesc *, struct epacketfilt *);
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate * To save instructions, since STREAMS ignores the return value
940Sstevel@tonic-gate * from these functions, they are defined as void here. Kind of icky, but...
950Sstevel@tonic-gate */
960Sstevel@tonic-gate static void pfwput(queue_t *, mblk_t *);
970Sstevel@tonic-gate static void pfrput(queue_t *, mblk_t *);
980Sstevel@tonic-gate
990Sstevel@tonic-gate static struct module_info pf_minfo = {
1000Sstevel@tonic-gate 22, /* mi_idnum */
1010Sstevel@tonic-gate "pfmod", /* mi_idname */
1020Sstevel@tonic-gate 0, /* mi_minpsz */
1030Sstevel@tonic-gate INFPSZ, /* mi_maxpsz */
1040Sstevel@tonic-gate 0, /* mi_hiwat */
1050Sstevel@tonic-gate 0 /* mi_lowat */
1060Sstevel@tonic-gate };
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate static struct qinit pf_rinit = {
1090Sstevel@tonic-gate (int (*)())pfrput, /* qi_putp */
1100Sstevel@tonic-gate NULL,
1110Sstevel@tonic-gate pfopen, /* qi_qopen */
1120Sstevel@tonic-gate pfclose, /* qi_qclose */
1130Sstevel@tonic-gate NULL, /* qi_qadmin */
1140Sstevel@tonic-gate &pf_minfo, /* qi_minfo */
1150Sstevel@tonic-gate NULL /* qi_mstat */
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static struct qinit pf_winit = {
1190Sstevel@tonic-gate (int (*)())pfwput, /* qi_putp */
1200Sstevel@tonic-gate NULL, /* qi_srvp */
1210Sstevel@tonic-gate NULL, /* qi_qopen */
1220Sstevel@tonic-gate NULL, /* qi_qclose */
1230Sstevel@tonic-gate NULL, /* qi_qadmin */
1240Sstevel@tonic-gate &pf_minfo, /* qi_minfo */
1250Sstevel@tonic-gate NULL /* qi_mstat */
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate static struct streamtab pf_info = {
1290Sstevel@tonic-gate &pf_rinit, /* st_rdinit */
1300Sstevel@tonic-gate &pf_winit, /* st_wrinit */
1310Sstevel@tonic-gate NULL, /* st_muxrinit */
1320Sstevel@tonic-gate NULL /* st_muxwinit */
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static struct fmodsw fsw = {
1360Sstevel@tonic-gate "pfmod",
1370Sstevel@tonic-gate &pf_info,
1380Sstevel@tonic-gate D_MTQPAIR | D_MP
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static struct modlstrmod modlstrmod = {
1420Sstevel@tonic-gate &mod_strmodops, "streams packet filter module", &fsw
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate static struct modlinkage modlinkage = {
1460Sstevel@tonic-gate MODREV_1, &modlstrmod, NULL
1470Sstevel@tonic-gate };
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate int
_init(void)1500Sstevel@tonic-gate _init(void)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate return (mod_install(&modlinkage));
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate int
_fini(void)1560Sstevel@tonic-gate _fini(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate return (mod_remove(&modlinkage));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1620Sstevel@tonic-gate _info(struct modinfo *modinfop)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*ARGSUSED*/
1680Sstevel@tonic-gate static int
pfopen(queue_t * rq,dev_t * dev,int oflag,int sflag,cred_t * crp)1690Sstevel@tonic-gate pfopen(queue_t *rq, dev_t *dev, int oflag, int sflag, cred_t *crp)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate struct epacketfilt *pfp;
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate ASSERT(rq);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (sflag != MODOPEN)
1760Sstevel@tonic-gate return (EINVAL);
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if (rq->q_ptr)
1790Sstevel@tonic-gate return (0);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * Allocate and initialize per-Stream structure.
1830Sstevel@tonic-gate */
1840Sstevel@tonic-gate pfp = kmem_alloc(sizeof (struct epacketfilt), KM_SLEEP);
1850Sstevel@tonic-gate rq->q_ptr = WR(rq)->q_ptr = (char *)pfp;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate qprocson(rq);
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate return (0);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate static int
pfclose(queue_t * rq)1930Sstevel@tonic-gate pfclose(queue_t *rq)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate struct epacketfilt *pfp = (struct epacketfilt *)rq->q_ptr;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate ASSERT(pfp);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate qprocsoff(rq);
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate kmem_free(pfp, sizeof (struct epacketfilt));
2020Sstevel@tonic-gate rq->q_ptr = WR(rq)->q_ptr = NULL;
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate return (0);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * Write-side put procedure. Its main task is to detect ioctls.
2090Sstevel@tonic-gate * Other message types are passed on through.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate static void
pfwput(queue_t * wq,mblk_t * mp)2120Sstevel@tonic-gate pfwput(queue_t *wq, mblk_t *mp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate switch (mp->b_datap->db_type) {
2150Sstevel@tonic-gate case M_IOCTL:
2160Sstevel@tonic-gate pfioctl(wq, mp);
2170Sstevel@tonic-gate break;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate default:
2200Sstevel@tonic-gate putnext(wq, mp);
2210Sstevel@tonic-gate break;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * Read-side put procedure. It's responsible for applying the
2270Sstevel@tonic-gate * packet filter and passing upstream message on or discarding it
2280Sstevel@tonic-gate * depending upon the results.
2290Sstevel@tonic-gate *
2300Sstevel@tonic-gate * Upstream messages can start with zero or more M_PROTO mblks
2310Sstevel@tonic-gate * which are skipped over before executing the packet filter
2320Sstevel@tonic-gate * on any remaining M_DATA mblks.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate static void
pfrput(queue_t * rq,mblk_t * mp)2350Sstevel@tonic-gate pfrput(queue_t *rq, mblk_t *mp)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate struct epacketfilt *pfp = (struct epacketfilt *)rq->q_ptr;
2380Sstevel@tonic-gate mblk_t *mbp, *mpp;
2390Sstevel@tonic-gate struct packdesc pd;
2400Sstevel@tonic-gate int need;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate ASSERT(pfp);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate switch (DB_TYPE(mp)) {
2450Sstevel@tonic-gate case M_PROTO:
2460Sstevel@tonic-gate case M_DATA:
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * Skip over protocol information and find the start
2490Sstevel@tonic-gate * of the message body, saving the overall message
2500Sstevel@tonic-gate * start in mpp.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate for (mpp = mp; mp && (DB_TYPE(mp) == M_PROTO); mp = mp->b_cont)
2530Sstevel@tonic-gate ;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate * Null body (exclusive of M_PROTO blocks) ==> accept.
2570Sstevel@tonic-gate * Note that a null body is not the same as an empty body.
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate if (mp == NULL) {
2600Sstevel@tonic-gate putnext(rq, mpp);
2610Sstevel@tonic-gate break;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate * Pull the packet up to the length required by
2660Sstevel@tonic-gate * the filter. Note that doing so destroys sharing
2670Sstevel@tonic-gate * relationships, which is unfortunate, since the
2680Sstevel@tonic-gate * results of pulling up here are likely to be useful
2690Sstevel@tonic-gate * for shared messages applied to a filter on a sibling
2700Sstevel@tonic-gate * stream.
2710Sstevel@tonic-gate *
2720Sstevel@tonic-gate * Most packet sources will provide the packet in two
2730Sstevel@tonic-gate * logical pieces: an initial header in a single mblk,
2740Sstevel@tonic-gate * and a body in a sequence of mblks hooked to the
2750Sstevel@tonic-gate * header. We're prepared to deal with variant forms,
2760Sstevel@tonic-gate * but in any case, the pullup applies only to the body
2770Sstevel@tonic-gate * part.
2780Sstevel@tonic-gate */
2790Sstevel@tonic-gate mbp = mp->b_cont;
2800Sstevel@tonic-gate need = pfp->pf_PByteLen;
2810Sstevel@tonic-gate if (mbp && (MBLKL(mbp) < need)) {
2820Sstevel@tonic-gate int len = msgdsize(mbp);
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /* XXX discard silently on pullupmsg failure */
2850Sstevel@tonic-gate if (pullupmsg(mbp, MIN(need, len)) == 0) {
2860Sstevel@tonic-gate freemsg(mpp);
2870Sstevel@tonic-gate break;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate * Misalignment (not on short boundary) ==> reject.
2930Sstevel@tonic-gate */
2940Sstevel@tonic-gate if (((uintptr_t)mp->b_rptr & (sizeof (ushort_t) - 1)) ||
2950Sstevel@tonic-gate (mbp != NULL &&
2960Sstevel@tonic-gate ((uintptr_t)mbp->b_rptr & (sizeof (ushort_t) - 1)))) {
2970Sstevel@tonic-gate freemsg(mpp);
2980Sstevel@tonic-gate break;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * These assignments are distasteful, but necessary,
3030Sstevel@tonic-gate * since the packet filter wants to work in terms of
3040Sstevel@tonic-gate * shorts. Odd bytes at the end of header or data can't
3050Sstevel@tonic-gate * participate in the filtering operation.
3060Sstevel@tonic-gate */
3070Sstevel@tonic-gate pd.pd_hdr = (ushort_t *)mp->b_rptr;
3080Sstevel@tonic-gate pd.pd_hdrlen = (mp->b_wptr - mp->b_rptr) / sizeof (ushort_t);
3090Sstevel@tonic-gate if (mbp) {
3100Sstevel@tonic-gate pd.pd_body = (ushort_t *)mbp->b_rptr;
3110Sstevel@tonic-gate pd.pd_bodylen = (mbp->b_wptr - mbp->b_rptr) /
3120Sstevel@tonic-gate sizeof (ushort_t);
3130Sstevel@tonic-gate } else {
3140Sstevel@tonic-gate pd.pd_body = NULL;
3150Sstevel@tonic-gate pd.pd_bodylen = 0;
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate * Apply the filter.
3200Sstevel@tonic-gate */
3210Sstevel@tonic-gate if (FilterPacket(&pd, pfp))
3220Sstevel@tonic-gate putnext(rq, mpp);
3230Sstevel@tonic-gate else
3240Sstevel@tonic-gate freemsg(mpp);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate break;
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate default:
3290Sstevel@tonic-gate putnext(rq, mp);
3300Sstevel@tonic-gate break;
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate * Handle write-side M_IOCTL messages.
3370Sstevel@tonic-gate */
3380Sstevel@tonic-gate static void
pfioctl(queue_t * wq,mblk_t * mp)3390Sstevel@tonic-gate pfioctl(queue_t *wq, mblk_t *mp)
3400Sstevel@tonic-gate {
3410Sstevel@tonic-gate struct epacketfilt *pfp = (struct epacketfilt *)wq->q_ptr;
3420Sstevel@tonic-gate struct Pf_ext_packetfilt *upfp;
3430Sstevel@tonic-gate struct packetfilt *opfp;
3440Sstevel@tonic-gate ushort_t *fwp;
345*2760Sdg199075 int arg;
346*2760Sdg199075 int maxoff = 0;
347*2760Sdg199075 int maxoffreg = 0;
3480Sstevel@tonic-gate struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
3490Sstevel@tonic-gate int error;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate switch (iocp->ioc_cmd) {
3520Sstevel@tonic-gate case PFIOCSETF:
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate * Verify argument length. Since the size of packet filter
3550Sstevel@tonic-gate * got increased (ENMAXFILTERS was bumped up to 2047), to
3560Sstevel@tonic-gate * maintain backwards binary compatibility, we need to
3570Sstevel@tonic-gate * check for both possible sizes.
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate switch (iocp->ioc_count) {
3600Sstevel@tonic-gate case sizeof (struct Pf_ext_packetfilt):
3610Sstevel@tonic-gate error = miocpullup(mp,
3620Sstevel@tonic-gate sizeof (struct Pf_ext_packetfilt));
3630Sstevel@tonic-gate if (error != 0) {
3640Sstevel@tonic-gate miocnak(wq, mp, 0, error);
3650Sstevel@tonic-gate return;
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate upfp = (struct Pf_ext_packetfilt *)mp->b_cont->b_rptr;
3680Sstevel@tonic-gate if (upfp->Pf_FilterLen > PF_MAXFILTERS) {
3690Sstevel@tonic-gate miocnak(wq, mp, 0, EINVAL);
3700Sstevel@tonic-gate return;
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate bcopy(upfp, pfp, sizeof (struct Pf_ext_packetfilt));
3740Sstevel@tonic-gate pfp->pf_FilterEnd = &pfp->pf_Filter[pfp->pf_FilterLen];
3750Sstevel@tonic-gate break;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate case sizeof (struct packetfilt):
3780Sstevel@tonic-gate error = miocpullup(mp, sizeof (struct packetfilt));
3790Sstevel@tonic-gate if (error != 0) {
3800Sstevel@tonic-gate miocnak(wq, mp, 0, error);
3810Sstevel@tonic-gate return;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate opfp = (struct packetfilt *)mp->b_cont->b_rptr;
3840Sstevel@tonic-gate /* this strange comparison keeps gcc from complaining */
3850Sstevel@tonic-gate if (opfp->Pf_FilterLen - 1 >= ENMAXFILTERS) {
3860Sstevel@tonic-gate miocnak(wq, mp, 0, EINVAL);
3870Sstevel@tonic-gate return;
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate pfp->pf.Pf_Priority = opfp->Pf_Priority;
3910Sstevel@tonic-gate pfp->pf.Pf_FilterLen = (unsigned int)opfp->Pf_FilterLen;
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate bcopy(opfp->Pf_Filter, pfp->pf.Pf_Filter,
3940Sstevel@tonic-gate sizeof (opfp->Pf_Filter));
3950Sstevel@tonic-gate pfp->pf_FilterEnd = &pfp->pf_Filter[pfp->pf_FilterLen];
3960Sstevel@tonic-gate break;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate default:
3990Sstevel@tonic-gate miocnak(wq, mp, 0, EINVAL);
4000Sstevel@tonic-gate return;
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate * Find and record maximum byte offset that the
4050Sstevel@tonic-gate * filter users. We use this when executing the
4060Sstevel@tonic-gate * filter to determine how much of the packet
4070Sstevel@tonic-gate * body to pull up. This code depends on the
4080Sstevel@tonic-gate * filter encoding.
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate for (fwp = pfp->pf_Filter; fwp < pfp->pf_FilterEnd; fwp++) {
4110Sstevel@tonic-gate arg = *fwp & ((1 << ENF_NBPA) - 1);
4120Sstevel@tonic-gate switch (arg) {
4130Sstevel@tonic-gate default:
4140Sstevel@tonic-gate if ((arg -= ENF_PUSHWORD) > maxoff)
4150Sstevel@tonic-gate maxoff = arg;
4160Sstevel@tonic-gate break;
4170Sstevel@tonic-gate
418*2760Sdg199075 case ENF_LOAD_OFFSET:
419*2760Sdg199075 /* Point to the offset */
420*2760Sdg199075 fwp++;
421*2760Sdg199075 if (*fwp > maxoffreg)
422*2760Sdg199075 maxoffreg = *fwp;
423*2760Sdg199075 break;
424*2760Sdg199075
4250Sstevel@tonic-gate case ENF_PUSHLIT:
426*2760Sdg199075 case ENF_BRTR:
427*2760Sdg199075 case ENF_BRFL:
4280Sstevel@tonic-gate /* Skip over the literal. */
4290Sstevel@tonic-gate fwp++;
4300Sstevel@tonic-gate break;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate case ENF_PUSHZERO:
4330Sstevel@tonic-gate case ENF_PUSHONE:
4340Sstevel@tonic-gate case ENF_PUSHFFFF:
4350Sstevel@tonic-gate case ENF_PUSHFF00:
4360Sstevel@tonic-gate case ENF_PUSH00FF:
4370Sstevel@tonic-gate case ENF_NOPUSH:
438*2760Sdg199075 case ENF_POP:
4390Sstevel@tonic-gate break;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate * Convert word offset to length in bytes.
4450Sstevel@tonic-gate */
446*2760Sdg199075 pfp->pf_PByteLen = (maxoff + maxoffreg + 1) * sizeof (ushort_t);
4470Sstevel@tonic-gate miocack(wq, mp, 0, 0);
4480Sstevel@tonic-gate break;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate default:
4510Sstevel@tonic-gate putnext(wq, mp);
4520Sstevel@tonic-gate break;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /* #define DEBUG 1 */
4570Sstevel@tonic-gate /* #define INNERDEBUG 1 */
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate #ifdef INNERDEBUG
460*2760Sdg199075 #define enprintf(a) printf a
461*2760Sdg199075 #else
462*2760Sdg199075 #define enprintf(a)
463*2760Sdg199075 #endif
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate * Apply the packet filter given by pfp to the packet given by
4670Sstevel@tonic-gate * pp. Return nonzero iff the filter accepts the packet.
4680Sstevel@tonic-gate *
4690Sstevel@tonic-gate * The packet comes in two pieces, a header and a body, since
4700Sstevel@tonic-gate * that's the most convenient form for our caller. The header
4710Sstevel@tonic-gate * is in contiguous memory, whereas the body is in a mbuf.
4720Sstevel@tonic-gate * Our caller will have adjusted the mbuf chain so that its first
4730Sstevel@tonic-gate * min(MLEN, length(body)) bytes are guaranteed contiguous. For
4740Sstevel@tonic-gate * the sake of efficiency (and some laziness) the filter is prepared
4750Sstevel@tonic-gate * to examine only these two contiguous pieces. Furthermore, it
4760Sstevel@tonic-gate * assumes that the header length is even, so that there's no need
4770Sstevel@tonic-gate * to glue the last byte of header to the first byte of data.
4780Sstevel@tonic-gate */
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate #define opx(i) ((i) >> ENF_NBPA)
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate static int
FilterPacket(struct packdesc * pp,struct epacketfilt * pfp)4830Sstevel@tonic-gate FilterPacket(struct packdesc *pp, struct epacketfilt *pfp)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate int maxhdr = pp->pd_hdrlen;
4860Sstevel@tonic-gate int maxword = maxhdr + pp->pd_bodylen;
4870Sstevel@tonic-gate ushort_t *sp;
4880Sstevel@tonic-gate ushort_t *fp;
4890Sstevel@tonic-gate ushort_t *fpe;
4900Sstevel@tonic-gate unsigned op;
4910Sstevel@tonic-gate unsigned arg;
492*2760Sdg199075 unsigned offreg = 0;
4930Sstevel@tonic-gate ushort_t stack[ENMAXFILTERS+1];
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate fp = &pfp->pf_Filter[0];
4960Sstevel@tonic-gate fpe = pfp->pf_FilterEnd;
4970Sstevel@tonic-gate
498*2760Sdg199075 enprintf(("FilterPacket(%p, %p, %p, %p):\n", pp, pfp, fp, fpe));
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate * Push TRUE on stack to start. The stack size is chosen such
5020Sstevel@tonic-gate * that overflow can't occur -- each operation can push at most
5030Sstevel@tonic-gate * one item on the stack, and the stack size equals the maximum
5040Sstevel@tonic-gate * program length.
5050Sstevel@tonic-gate */
5060Sstevel@tonic-gate sp = &stack[ENMAXFILTERS];
5070Sstevel@tonic-gate *sp = 1;
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate while (fp < fpe) {
5100Sstevel@tonic-gate op = *fp >> ENF_NBPA;
5110Sstevel@tonic-gate arg = *fp & ((1 << ENF_NBPA) - 1);
5120Sstevel@tonic-gate fp++;
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate switch (arg) {
5150Sstevel@tonic-gate default:
5160Sstevel@tonic-gate arg -= ENF_PUSHWORD;
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate * Since arg is unsigned,
5190Sstevel@tonic-gate * if it were less than ENF_PUSHWORD before,
5200Sstevel@tonic-gate * it would now be huge.
5210Sstevel@tonic-gate */
522*2760Sdg199075 if (arg + offreg < maxhdr)
523*2760Sdg199075 *--sp = pp->pd_hdr[arg + offreg];
524*2760Sdg199075 else if (arg + offreg < maxword)
525*2760Sdg199075 *--sp = pp->pd_body[arg - maxhdr + offreg];
5260Sstevel@tonic-gate else {
527*2760Sdg199075 enprintf(("=>0(len)\n"));
5280Sstevel@tonic-gate return (0);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate break;
5310Sstevel@tonic-gate case ENF_PUSHLIT:
5320Sstevel@tonic-gate *--sp = *fp++;
5330Sstevel@tonic-gate break;
5340Sstevel@tonic-gate case ENF_PUSHZERO:
5350Sstevel@tonic-gate *--sp = 0;
5360Sstevel@tonic-gate break;
5370Sstevel@tonic-gate case ENF_PUSHONE:
5380Sstevel@tonic-gate *--sp = 1;
5390Sstevel@tonic-gate break;
5400Sstevel@tonic-gate case ENF_PUSHFFFF:
5410Sstevel@tonic-gate *--sp = 0xffff;
5420Sstevel@tonic-gate break;
5430Sstevel@tonic-gate case ENF_PUSHFF00:
5440Sstevel@tonic-gate *--sp = 0xff00;
5450Sstevel@tonic-gate break;
5460Sstevel@tonic-gate case ENF_PUSH00FF:
5470Sstevel@tonic-gate *--sp = 0x00ff;
5480Sstevel@tonic-gate break;
549*2760Sdg199075 case ENF_LOAD_OFFSET:
550*2760Sdg199075 offreg = *fp++;
551*2760Sdg199075 break;
552*2760Sdg199075 case ENF_BRTR:
553*2760Sdg199075 if (*sp != 0)
554*2760Sdg199075 fp += *fp;
555*2760Sdg199075 else
556*2760Sdg199075 fp++;
557*2760Sdg199075 if (fp >= fpe) {
558*2760Sdg199075 enprintf(("BRTR: fp>=fpe\n"));
559*2760Sdg199075 return (0);
560*2760Sdg199075 }
561*2760Sdg199075 break;
562*2760Sdg199075 case ENF_BRFL:
563*2760Sdg199075 if (*sp == 0)
564*2760Sdg199075 fp += *fp;
565*2760Sdg199075 else
566*2760Sdg199075 fp++;
567*2760Sdg199075 if (fp >= fpe) {
568*2760Sdg199075 enprintf(("BRFL: fp>=fpe\n"));
569*2760Sdg199075 return (0);
570*2760Sdg199075 }
571*2760Sdg199075 break;
572*2760Sdg199075 case ENF_POP:
573*2760Sdg199075 ++sp;
574*2760Sdg199075 if (sp > &stack[ENMAXFILTERS]) {
575*2760Sdg199075 enprintf(("stack underflow\n"));
576*2760Sdg199075 return (0);
577*2760Sdg199075 }
578*2760Sdg199075 break;
5790Sstevel@tonic-gate case ENF_NOPUSH:
5800Sstevel@tonic-gate break;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate if (sp < &stack[2]) { /* check stack overflow: small yellow zone */
584*2760Sdg199075 enprintf(("=>0(--sp)\n"));
5850Sstevel@tonic-gate return (0);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (op == ENF_NOP)
5890Sstevel@tonic-gate continue;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * all non-NOP operators binary, must have at least two operands
5930Sstevel@tonic-gate * on stack to evaluate.
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate if (sp > &stack[ENMAXFILTERS-2]) {
596*2760Sdg199075 enprintf(("=>0(sp++)\n"));
5970Sstevel@tonic-gate return (0);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate arg = *sp++;
6010Sstevel@tonic-gate switch (op) {
6020Sstevel@tonic-gate default:
603*2760Sdg199075 enprintf(("=>0(def)\n"));
6040Sstevel@tonic-gate return (0);
6050Sstevel@tonic-gate case opx(ENF_AND):
6060Sstevel@tonic-gate *sp &= arg;
6070Sstevel@tonic-gate break;
6080Sstevel@tonic-gate case opx(ENF_OR):
6090Sstevel@tonic-gate *sp |= arg;
6100Sstevel@tonic-gate break;
6110Sstevel@tonic-gate case opx(ENF_XOR):
6120Sstevel@tonic-gate *sp ^= arg;
6130Sstevel@tonic-gate break;
6140Sstevel@tonic-gate case opx(ENF_EQ):
6150Sstevel@tonic-gate *sp = (*sp == arg);
6160Sstevel@tonic-gate break;
6170Sstevel@tonic-gate case opx(ENF_NEQ):
6180Sstevel@tonic-gate *sp = (*sp != arg);
6190Sstevel@tonic-gate break;
6200Sstevel@tonic-gate case opx(ENF_LT):
6210Sstevel@tonic-gate *sp = (*sp < arg);
6220Sstevel@tonic-gate break;
6230Sstevel@tonic-gate case opx(ENF_LE):
6240Sstevel@tonic-gate *sp = (*sp <= arg);
6250Sstevel@tonic-gate break;
6260Sstevel@tonic-gate case opx(ENF_GT):
6270Sstevel@tonic-gate *sp = (*sp > arg);
6280Sstevel@tonic-gate break;
6290Sstevel@tonic-gate case opx(ENF_GE):
6300Sstevel@tonic-gate *sp = (*sp >= arg);
6310Sstevel@tonic-gate break;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate /* short-circuit operators */
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate case opx(ENF_COR):
6360Sstevel@tonic-gate if (*sp++ == arg) {
637*2760Sdg199075 enprintf(("=>COR %x\n", *sp));
6380Sstevel@tonic-gate return (1);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate break;
6410Sstevel@tonic-gate case opx(ENF_CAND):
6420Sstevel@tonic-gate if (*sp++ != arg) {
643*2760Sdg199075 enprintf(("=>CAND %x\n", *sp));
6440Sstevel@tonic-gate return (0);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate break;
6470Sstevel@tonic-gate case opx(ENF_CNOR):
6480Sstevel@tonic-gate if (*sp++ == arg) {
649*2760Sdg199075 enprintf(("=>COR %x\n", *sp));
6500Sstevel@tonic-gate return (0);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate break;
6530Sstevel@tonic-gate case opx(ENF_CNAND):
6540Sstevel@tonic-gate if (*sp++ != arg) {
655*2760Sdg199075 enprintf(("=>CNAND %x\n", *sp));
6560Sstevel@tonic-gate return (1);
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate break;
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate }
661*2760Sdg199075 enprintf(("=>%x\n", *sp));
6620Sstevel@tonic-gate return (*sp);
6630Sstevel@tonic-gate }
664