summaryrefslogtreecommitdiff
path: root/usr/src/man/man7m/pfmod.7m
blob: 79a74775e2dc6b40bff9c2af5655c102ada8c63e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'\" te
.\" Copyright (C) 2006, Sun Microsystems, Inc. All Rights Reserved
.\" 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 or http://www.opensolaris.org/os/licensing.  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]
.TH PFMOD 7M "Jun 18, 2006"
.SH NAME
pfmod \- STREAMS Packet Filter Module
.SH SYNOPSIS
.LP
.nf
#include <sys/pfmod.h>
.fi

.LP
.nf
ioctl(\fIfd\fR, IPUSH, "pfmod");
.fi

.SH DESCRIPTION
.sp
.LP
\fBpfmod\fR is a \fBSTREAMS\fR module that subjects messages arriving on its
read queue to a packet filter and passes only those messages that the filter
accepts on to its upstream neighbor. Such filtering can be very useful for
user-level protocol implementations and for networking monitoring programs that
wish to view only specific types of events.
.SS "Read-side Behavior"
.sp
.LP
\fBpfmod\fR applies the current packet filter to all \fBM_DATA\fR and
\fBM_PROTO\fR messages arriving on its read queue. The module prepares these
messages for examination by first skipping over all leading \fBM_PROTO\fR
message blocks to arrive at the beginning of the message's data portion. If
there is no data portion, \fBpfmod\fR accepts the message and passes it along
to its upstream neighbor. Otherwise, the module ensures that the part of the
message's data that the packet filter might examine lies in contiguous memory,
calling the \fBpullupmsg\fR(9F) utility routine if necessary to force
contiguity. (Note: this action destroys any sharing relationships that the
subject message might have had with other messages.) Finally, it applies the
packet filter to the message's data, passing the entire message upstream to the
next module if the filter accepts, and discarding the message otherwise. See
PACKET FILTERS below for details on how the filter works.
.sp
.LP
If there is no packet filter yet in effect, the module acts as if the filter
exists but does nothing, implying that all incoming messages are accepted. The
ioctls section below describes how to associate a packet filter with an
instance of \fBpfmod\fR.
.sp
.LP
\fBpfmod\fR passes all other messages through unaltered to its upper neighbor.
.SS "Write-side Behavior"
.sp
.LP
\fBpfmod\fR intercepts \fBM_IOCTL\fR messages for the \fIioctl\fR described
below. The module passes all other messages through unaltered to its lower
neighbor.
.SH IOCTLS
.sp
.LP
\fBpfmod\fR responds to the following \fIioctl\fR.
.sp
.ne 2
.na
\fB\fBPFIOCSETF\fR \fR
.ad
.RS 14n
This \fIioctl\fR directs the module to replace its current packet filter, if
any, with the filter specified by the \fBstruct packetfilt\fR pointer named by
its final argument. This structure is defined in \fB<sys/pfmod.h>\fR as:
.RE

.sp
.in +2
.nf
struct packetfilt {
 uchar_t	Pf_Priority;             /* priority of filter */
 uchar_t	Pf_FilterLen;            /* length of filter cmd list */
 ushort_t	Pf_Filter[ENMAXFILTERS]; /* filter command list */
};
.fi
.in -2

.sp
.LP
The \fBPf_Priority\fR field is included only for compatibility with other
packet filter implementations and is otherwise ignored. The packet filter
itself is specified in the \fBPf_Filter\fR array as a sequence of two-byte
commands, with the \fBPf_FilterLen\fR field giving the number of commands in
the sequence. This implementation restricts the maximum number of commands in a
filter (\fBENMAXFILTERS\fR) to 255. The next section describes the available
commands and their semantics.
.SH PACKET FILTERS
.sp
.LP
A packet filter consists of the filter command list length (in units of
\fBushort_t\fRs), and the filter command list itself. (The priority field
mentioned above is ignored in this implementation.) Each filter command list
specifies a sequence of actions that operate on an internal stack of ushort_ts
("shortwords") or an offset register. The offset register is  initially zero.
Each shortword of the command  list specifies an action and a binary operator.
Using _n_ as shorthand for the next shortword of the instruction stream and
_%oreg_ for the offset register, the list of actions is:
.sp
.in +2
.nf
       COMMAND        SHORTWORDS  ACTION
       ENF_PUSHLIT             2  Push _n_ on the stack.
       ENF_PUSHZERO            1  Push zero on the stack.
       ENF_PUSHONE             1  Push one on the stack.
       ENF_PUSHFFFF            1  Push 0xFFFF on the stack.
       ENF_PUSHFF00            1  Push 0xFF00 on the stack.
       ENF_PUSH00FF            1  Push 0x00FF on the stack.
       ENF_LOAD_OFFSET         2  Load _n_ into _%oreg_.
       ENF_BRTR                2  Branch forward _n_ shortwords if
                                  the top element of the stack is
                                  non-zero.
       ENF_BRFL                2  Branch forward _n_ shortwords if
                                  the top element of the stack is zero.
       ENF_POP                 1  Pop the top element from the stack.
       ENF_PUSHWORD+m          1  Push the value of shortword (_m_ +
                                  _%oreg_) of the packet onto the stack.
.fi
.in -2

.sp
.LP
The binary operators can be from the set {\fBENF_EQ\fR, \fBENF_NEQ\fR,
\fBENF_LT\fR, \fBENF_LE\fR, \fBENF_GT\fR,\fBENF_GE\fR, \fBENF_AND\fR,
\fBENF_OR\fR, \fBENF_XOR\fR} which operate on the top two elements of the stack
and replace them with its result.
.sp
.LP
When both an action and operator are specified in the same shortword, the
action is performed followed by the operation.
.sp
.LP
The binary operator can also be from the set {\fBENF_COR\fR, \fBENF_CAND\fR,
\fBENF_CNOR\fR, \fBENF_CNAND\fR}. These are "short-circuit" operators, in that
they terminate the execution of the filter immediately if the condition they
are checking for is found, and continue otherwise. All pop two elements from
the stack and compare them for equality; \fBENF_CAND\fR returns false if the
result is false; \fBENF_COR\fR returns true if the result is true;
\fBENF_CNAND\fR returns true if the result is false; \fBENF_CNOR\fR returns
false if the result is true. Unlike the other binary operators, these four do
not leave a result on the stack, even if they continue.
.sp
.LP
The short-circuit operators should be used when possible, to reduce the amount
of time spent evaluating filters. When they are used, you should also arrange
the order of the tests so that the filter will succeed or fail as soon as
possible; for example, checking the \fBIP\fR destination field of a \fBUDP\fR
packet is more likely to indicate failure than the packet type field.
.sp
.LP
The special action \fBENF_NOPUSH\fR and the special operator \fBENF_NOP\fR can
be used to only perform the binary operation or to only push a value on the
stack. Since both are (conveniently) defined to be zero, indicating only an
action actually specifies the action followed by \fBENF_NOP\fR, and indicating
only an operation actually specifies \fBENF_NOPUSH\fR followed by the
operation.
.sp
.LP
After executing the filter command list, a non-zero value (true) left on top of
the stack (or an empty stack) causes the incoming packet to be accepted and a
zero value (false) causes the packet to be rejected. (If the filter exits as
the result of a short-circuit operator, the top-of-stack value is ignored.)
Specifying an undefined operation or action in the command list or performing
an illegal operation or action (such as pushing a shortword offset past the end
of the packet or executing a binary operator with fewer than two shortwords on
the stack) causes a filter to reject the packet.
.SH EXAMPLES
.sp
.LP
The packet filter module is not dependent on any particular device driver or
module but is commonly used with datalink drivers such as the Ethernet driver.
If the underlying datalink driver supports the Data Link Provider Interface
(DLPI) message set, the appropriate \fBSTREAMS DLPI\fR messages must be issued
to attach the stream to a particular hardware device and bind a datalink
address to the stream before the underlying driver will route received packets
upstream. Refer to the \fBDLPI\fR Version 2 specification for details on this
interface.
.sp
.LP
The reverse \fBARP\fR daemon program may use code similar to the following
fragment  to construct a filter that rejects all but \fBRARP\fR packets. That
is, it accepts only packets whose Ethernet type field has the value
\fBETHERTYPE_REVARP\fR. The filter works whether a VLAN is configured or not.
.sp
.in +2
.nf
struct ether_header eh;		/* used only for offset values */
struct packetfilt pf;
register ushort_t *fwp = pf.Pf_Filter;
ushort_t offset;
int	fd;
/*
 * Push packet filter streams module.
 */
if (ioctl(fd, I_PUSH, "pfmod") < 0)
	syserr("pfmod");

/*
 * Set up filter.  Offset is the displacement of the Ethernet
 * type field from the beginning of the packet in units of
 * ushort_ts.
 */
offset = ((uint_t) &eh.ether_type - (uint_t) &eh.ether_dhost) /
	            sizeof (us_short);
       *fwp++ = ENF_PUSHWORD + offset;
       *fwp++ = ENF_PUSHLIT;
       *fwp++ = htons(ETHERTYPE_VLAN);
       *fwp++ = ENF_EQ;
       *fwp++ = ENF_BRFL;
       *fwp++ = 3;  /* If this isn't ethertype VLAN, don't change oreg */
       *fwp++ = ENF_LOAD_OFFSET;
       *fwp++ = 2;  /* size of the VLAN tag in words */
       *fwp++ = ENF_POP;
       *fwp++ = ENF_PUSHWORD + offset;
       *fwp++ = ENF_PUSHLIT;
       *fwp++ = htons(ETHERTYPE_REVARP);
       *fwp++ = ENF_EQ;
       pf.Pf_FilterLen = fwp - &pf.PF_Filter[0];
.fi
.in -2

.sp
.LP
This filter can be abbreviated by taking advantage of the ability to combine
actions and operations:
.sp
.in +2
.nf
       *fwp++ = ENF_PUSHWORD + offset;
       *fwp++ = ENF_PUSHLIT | ENF_EQ;
       *fwp++ = htons(ETHERTYPE_REVARP);
       *fwp++ = htons(ETHERTYPE_VLAN);
       *fwp++ = ENF_BRFL | ENF_NOP;
       *fwp++ = 3;
       *fwp++ = ENF_LOAD_OFFSET | ENF_NOP;
       *fwp++ = 2;
       *fwp++ = ENF_POP | ENF_NOP;
       *fwp++ = ENF_PUSHWORD + offset;
       *fwp++ = ENF_PUSHLIT | ENF_EQ;
       *fwp++ = htons(ETHERTYPE_REVARP);
.fi
.in -2

.SH SEE ALSO
.sp
.LP
\fBbufmod\fR(7M), \fBdlpi\fR(7P), \fBpullupmsg\fR(9F)