/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* STREAMS Packet Filter Module
*
* This module applies a filter to messages arriving on its read
* queue, passing on messages that the filter accepts adn discarding
* the others. It supports ioctls for setting the filter.
*
* On the write side, the module simply passes everything through
* unchanged.
*
* Based on SunOS 4.x version. This version has minor changes:
* - general SVR4 porting stuff
* - change name and prefixes from "nit" buffer to streams buffer
* - multithreading assumes configured as D_MTQPAIR
*/
#include <sys/sysmacros.h>
/*
* Expanded version of the Packetfilt structure that includes
* some additional fields that aid filter execution efficiency.
*/
struct epacketfilt {
/* pointer to word immediately past end of filter */
/* length in bytes of packet prefix the filter examines */
};
/*
* (Internal) packet descriptor for FilterPacket
*/
struct packdesc {
};
/*
* Function prototypes.
*/
/*
* To save instructions, since STREAMS ignores the return value
* from these functions, they are defined as void here. Kind of icky, but...
*/
22, /* mi_idnum */
"pfmod", /* mi_idname */
0, /* mi_minpsz */
INFPSZ, /* mi_maxpsz */
0, /* mi_hiwat */
0 /* mi_lowat */
};
(int (*)())pfrput, /* qi_putp */
NULL,
pfopen, /* qi_qopen */
pfclose, /* qi_qclose */
NULL, /* qi_qadmin */
&pf_minfo, /* qi_minfo */
NULL /* qi_mstat */
};
(int (*)())pfwput, /* qi_putp */
NULL, /* qi_srvp */
NULL, /* qi_qopen */
NULL, /* qi_qclose */
NULL, /* qi_qadmin */
&pf_minfo, /* qi_minfo */
NULL /* qi_mstat */
};
&pf_rinit, /* st_rdinit */
&pf_winit, /* st_wrinit */
NULL, /* st_muxrinit */
NULL /* st_muxwinit */
};
"pfmod",
&pf_info,
};
};
};
int
_init(void)
{
return (mod_install(&modlinkage));
}
int
_fini(void)
{
return (mod_remove(&modlinkage));
}
int
{
}
/*ARGSUSED*/
static int
{
return (EINVAL);
return (0);
/*
* Allocate and initialize per-Stream structure.
*/
return (0);
}
static int
{
return (0);
}
/*
* Write-side put procedure. Its main task is to detect ioctls.
* Other message types are passed on through.
*/
static void
{
case M_IOCTL:
break;
default:
break;
}
}
/*
* Read-side put procedure. It's responsible for applying the
* packet filter and passing upstream message on or discarding it
* depending upon the results.
*
* Upstream messages can start with zero or more M_PROTO mblks
* which are skipped over before executing the packet filter
* on any remaining M_DATA mblks.
*/
static void
{
int need;
case M_PROTO:
case M_DATA:
/*
* Skip over protocol information and find the start
* of the message body, saving the overall message
* start in mpp.
*/
;
/*
* Null body (exclusive of M_PROTO blocks) ==> accept.
* Note that a null body is not the same as an empty body.
*/
break;
}
/*
* Pull the packet up to the length required by
* the filter. Note that doing so destroys sharing
* relationships, which is unfortunate, since the
* results of pulling up here are likely to be useful
* for shared messages applied to a filter on a sibling
* stream.
*
* Most packet sources will provide the packet in two
* logical pieces: an initial header in a single mblk,
* and a body in a sequence of mblks hooked to the
* header. We're prepared to deal with variant forms,
* but in any case, the pullup applies only to the body
* part.
*/
/* XXX discard silently on pullupmsg failure */
break;
}
}
/*
* Misalignment (not on short boundary) ==> reject.
*/
break;
}
/*
* These assignments are distasteful, but necessary,
* since the packet filter wants to work in terms of
* shorts. Odd bytes at the end of header or data can't
* participate in the filtering operation.
*/
if (mbp) {
sizeof (ushort_t);
} else {
pd.pd_bodylen = 0;
}
/*
* Apply the filter.
*/
else
break;
default:
break;
}
}
/*
* Handle write-side M_IOCTL messages.
*/
static void
{
int arg;
int maxoff = 0;
int maxoffreg = 0;
int error;
case PFIOCSETF:
/*
* Verify argument length. Since the size of packet filter
* got increased (ENMAXFILTERS was bumped up to 2047), to
* maintain backwards binary compatibility, we need to
* check for both possible sizes.
*/
case sizeof (struct Pf_ext_packetfilt):
sizeof (struct Pf_ext_packetfilt));
if (error != 0) {
return;
}
return;
}
break;
case sizeof (struct packetfilt):
if (error != 0) {
return;
}
/* this strange comparison keeps gcc from complaining */
return;
}
break;
default:
return;
}
/*
* Find and record maximum byte offset that the
* filter users. We use this when executing the
* filter to determine how much of the packet
* body to pull up. This code depends on the
* filter encoding.
*/
switch (arg) {
default:
break;
case ENF_LOAD_OFFSET:
/* Point to the offset */
fwp++;
break;
case ENF_PUSHLIT:
case ENF_BRTR:
case ENF_BRFL:
/* Skip over the literal. */
fwp++;
break;
case ENF_PUSHZERO:
case ENF_PUSHONE:
case ENF_PUSHFFFF:
case ENF_PUSHFF00:
case ENF_PUSH00FF:
case ENF_NOPUSH:
case ENF_POP:
break;
}
}
/*
* Convert word offset to length in bytes.
*/
break;
default:
break;
}
}
/* #define DEBUG 1 */
/* #define INNERDEBUG 1 */
#ifdef INNERDEBUG
#else
#define enprintf(a)
#endif
/*
* Apply the packet filter given by pfp to the packet given by
* pp. Return nonzero iff the filter accepts the packet.
*
* The packet comes in two pieces, a header and a body, since
* that's the most convenient form for our caller. The header
* is in contiguous memory, whereas the body is in a mbuf.
* Our caller will have adjusted the mbuf chain so that its first
* min(MLEN, length(body)) bytes are guaranteed contiguous. For
* the sake of efficiency (and some laziness) the filter is prepared
* to examine only these two contiguous pieces. Furthermore, it
* assumes that the header length is even, so that there's no need
* to glue the last byte of header to the first byte of data.
*/
static int
{
unsigned op;
unsigned arg;
unsigned offreg = 0;
/*
* Push TRUE on stack to start. The stack size is chosen such
* that overflow can't occur -- each operation can push at most
* one item on the stack, and the stack size equals the maximum
* program length.
*/
*sp = 1;
fp++;
switch (arg) {
default:
arg -= ENF_PUSHWORD;
/*
* Since arg is unsigned,
* if it were less than ENF_PUSHWORD before,
* it would now be huge.
*/
else {
enprintf(("=>0(len)\n"));
return (0);
}
break;
case ENF_PUSHLIT:
break;
case ENF_PUSHZERO:
*--sp = 0;
break;
case ENF_PUSHONE:
*--sp = 1;
break;
case ENF_PUSHFFFF:
*--sp = 0xffff;
break;
case ENF_PUSHFF00:
*--sp = 0xff00;
break;
case ENF_PUSH00FF:
*--sp = 0x00ff;
break;
case ENF_LOAD_OFFSET:
break;
case ENF_BRTR:
if (*sp != 0)
else
fp++;
enprintf(("BRTR: fp>=fpe\n"));
return (0);
}
break;
case ENF_BRFL:
if (*sp == 0)
else
fp++;
enprintf(("BRFL: fp>=fpe\n"));
return (0);
}
break;
case ENF_POP:
++sp;
enprintf(("stack underflow\n"));
return (0);
}
break;
case ENF_NOPUSH:
break;
}
enprintf(("=>0(--sp)\n"));
return (0);
}
continue;
/*
* all non-NOP operators binary, must have at least two operands
* on stack to evaluate.
*/
enprintf(("=>0(sp++)\n"));
return (0);
}
switch (op) {
default:
enprintf(("=>0(def)\n"));
return (0);
break;
break;
break;
break;
break;
break;
break;
break;
break;
/* short-circuit operators */
return (1);
}
break;
return (0);
}
break;
return (0);
}
break;
return (1);
}
break;
}
}
return (*sp);
}