BSD 4_3 development
[unix-history] / usr / contrib / enet / enet.4
CommitLineData
720cfaf6
MA
1.TH ENET 4 "17 January 1986" Stanford
2.SH "NAME"
3enet \- ethernet packet filter
4.SH SYNOPSIS
5.B "pseudo-device enetfilter 64"
6.SH "DESCRIPTION"
7The packet filter
8provides a raw interface to Ethernets and similar network data link layers.
9Packets received that are not used by the kernel
10(i.e., to support IP, ARP, and on some systems XNS, protocols)
11are available through this mechanism.
12The packet filter appears as a set of character special files, one
13per hardware interface.
14Each
15.I enet
16file may be opened multiple times, allowing each interface to be used by
17many processes.
18The total number of open ethernet
19files is limited to the value given in the kernel configuration; the
20example given in the SYNOPSIS above sets the limit to 64.
21.PP
22The minor device numbers
23are associated with interfaces when the system is booted.
24Minor device 0
25is associated with the first Ethernet interface ``attached'',
26minor device 1 with the second, and so forth.
27(These character special files are, for historical reasons,
28given the names
29.IR /dev/enet0 ,
30.IR /dev/eneta0 ,
31.IR /dev/enetb0 ,
32etc.)
33.PP
34Associated with each open instance of an
35.I enet
36file is a user-settable packet filter which is used to deliver
37incoming ethernet packets to the appropriate process.
38Whenever a packet is received from the net,
39successive packet filters from the list of filters for
40all open
41.I enet
42files are applied to the packet.
43When a filter accepts the packet,
44it is placed on the packet input queue of the
45associated file.
46If no filters accept the packet, it is discarded.
47The format of a packet filter is described below.
48.PP
49Reads from these files return the next packet
50from a queue of packets that have matched the filter.
51If insufficient buffer space to store the entire packet is specified in the
52read, the packet will be truncated and the trailing contents lost.
53Writes to these devices transmit packets on the
54network, with each write generating exactly one packet.
55.PP
56The packet filter currently supports a variety of different ``Ethernet''
57data-link levels:
58.IP "3mb Ethernet" 1.5i
59packets consist of 4 or more bytes with the first byte
60specifying the source ethernet address, the second
61byte specifying the destination ethernet address,
62and the next two bytes specifying the packet type.
63(Actually, on the network the source and destination addresses
64are in the opposite order.)
65.IP "byte-swapping 3mb Ethernet" 1.5i
66packets consist of 4 or more bytes with the first byte
67specifying the source ethernet address, the second
68byte specifying the destination ethernet address,
69and the next two bytes specifying the packet type.
70\fBEach short word (pair of bytes) is swapped from the network
71byte order\fR; this device type is only provided as a concession
72to backwards-compatibility.
73.IP "10mb Ethernet" 1.5i
74packets consist of 14 or more bytes with the first six
75bytes specifying the destination ethernet address,
76the next six bytes the source ethernet address,
77and the next two bytes specifying the packet type.
78.PP
79The remaining words are interpreted according to the packet type.
80Note that 16-bit and 32-bit quantities may have to be byteswapped
81(and possible short-swapped) to be intelligible on a Vax.
82.PP
83The packet filter mechanism does not know anything about the data portion
84of the packets it sends and receives. The user must supply
85the headers for transmitted packets (although the system makes sure that
86the source address is correct) and the headers of received packets
87are delivered to the user. The packet filters treat the entire packet,
88including headers, as uninterpreted data.
89.SH "IOCTL CALLS"
90In addition to FIONREAD,
91ten special
92.I
93ioctl
94calls may be applied to an open
95.I
96enet
97file.
98The first two set and fetch parameters
99for the file and are of the form:
100.sp
101.nf
102.RS
103.B #include <sys/types.h>
104.B #include <sys/enet.h>
105.B ioctl(fildes, code, param)
106.B
107struct eniocb *param;
108.RE
109.fi
110.sp
111where
112.I
113param
114is defined in
115.I
116<sys/enet.h>
117as:
118.br
119.sp
120.nf
121.RS
122.ta \w'struct 'u +\w'u_char 'u
123.ft B
124struct eniocb
125{
126 u_char en_addr;
127 u_char en_maxfilters;
128 u_char en_maxwaiting;
129 u_char en_maxpriority;
130 long en_rtout;
131};
132.DT
133.RE
134.fi
135.ft R
136.sp
137with the applicable codes being:
138.TP
139EIOCGETP
140Fetch the parameters for this file.
141.TP
142EIOCSETP
143Set the parameters for this file.
144.i0
145.DT
146.PP
147The maximum filter length parameter en_maxfilters indicates
148the maximum possible packet filter command
149list length (see EIOCSETF below).
150The maximum input wait queue size parameter en_maxwaitingindicates
151the maximum number of packets which may be queued for an
152ethernet file at one time (see EIOCSETW below).
153The maximum priority parameter en_maxpriority indicates the highest
154filter priority which may be set for the file (see EIOCSETF below).
155The en_addr field is no longer maintained by the driver; see
156EIOCDEVP below.
157.PP
158The read timeout parameter en_rtout specifies the number of clock ticks to
159wait before timing out on a read request and returning an EOF.
160This parameter is initialized to zero by
161.IR open (2),
162indicating no timeout. If it is negative, then read requests will return an
163EOF immediately if there are no packets in the input queue.
164(Note that all parameters except for the read timeout are read-only
165and are ignored when changed.)
166.PP
167A different
168.I ioctl
169is used to get device parameters of the ethernet underlying the
170minor device. It is of the form:
171.sp
172.nf
173.RS
174.B #include <sys/types.h>
175.br
176.B #include <sys/enet.h>
177.B ioctl(fildes, EIOCDEVP, param)
178.RE
179.fi
180.sp
181where
182.I param
183is defined in
184.I <sys/enet.h>
185as:
186.br
187.sp
188.nf
189.RS
190.ta \w'struct 'u +\w'u_short 'u
191.ft B
192struct endevp {
193 u_char end_dev_type;
194 u_char end_addr_len;
195 u_short end_hdr_len;
196 u_short end_MTU;
197 u_char end_addr[EN_MAX_ADDR_LEN];
198 u_char end_broadaddr[EN_MAX_ADDR_LEN];
199};
200.DT
201.RE
202.fi
203.ft R
204.sp
205The fields are:
206.IP end_dev_type 1.5i
207Specifies the device type; currently one of ENDT_3MB, ENDT_BS3MB or ENDT_10MB.
208.IP end_addr_len 1.5i
209Specifies the address length in bytes (e.g., 1 or 6).
210.IP end_hdr_len 1.5i
211Specifies the total header length in bytes (e.g., 4 or 14).
212.IP end_MTU 1.5i
213Specifies the maximum packet size, including header, in bytes.
214.IP end_addr 1.5i
215The address of this interface; aligned so that the low order
216byte of the address is the first byte in the array.
217.IP end_broadaddr 1.5i
218The hardware destination address for broadcasts on this network.
219.PP
220The next two calls enable and disable the input
221packet signal mechanism
222for the file and are of the form:
223.sp
224.nf
225.RS
226.B #include <sys/types.h>
227.B #include <sys/enet.h>
228.B ioctl(fildes, code, signp)
229.B
230u_int *signp;
231.RE
232.fi
233.sp
234where
235.I
236signp
237is a pointer to a word containing the number
238of the signal
239to be sent when an input packet arrives and
240with the applicable codes being:
241.TP
242EIOCENBS
243Enable the specified signal when an input packet
244is received for this file.
245If the ENHOLDSIG flag (see EIOCMBIS below) is not set,
246further signals are automatically disabled
247whenever a signal is sent to prevent nesting and hence
248must be specifically re-enabled after processing.
249When a signal number of 0 is supplied,
250this call is equivalent to EIOCINHS.
251.TP
252EIOCINHS
253Disable any signal when an input
254packet is received for this file
255(the
256.I signp
257parameter is ignored).
258This is the default when the file is first opened.
259.i0
260.DT
261.PP
262.sp
263The next two calls set and clear ``mode bits'' for the
264for the file and are of the form:
265.sp
266.nf
267.RS
268.B #include <sys/types.h>
269.B #include <sys/enet.h>
270.B ioctl(fildes, code, bits)
271.B
272u_short *bits;
273.RE
274.fi
275.sp
276where
277.I bits
278is a short work bit-mask specifying which bits to set or clear.
279Currently, the only bit mask recognized is ENHOLDSIG, which (if
280.IR clear )
281means that the driver should
282disable the effect of EIOCENBS once it has delivered a signal.
283Setting this bit
284means that you need use EIOCENBS only once. (For historical reasons, the
285default is that ENHOLDSIG is set.)
286The applicable codes are:
287.TP
288EIOCMBIS
289Sets the specified mode bits
290.TP
291EIOCMBIC
292Clears the specified mode bits
293.PP
294Another
295.I
296ioctl
297call is used to set the maximum size
298of the packet input queue for
299an open
300.I
301enet
302file.
303It is of the form:
304.sp
305.nf
306.RS
307.B #include <sys/types.h>
308.B #include <sys/enet.h>
309.B ioctl(fildes, EIOCSETW, maxwaitingp)
310.B u_int *maxwaitingp;
311.RE
312.fi
313.sp
314where
315.I
316maxwaitingp
317is a pointer
318to a word containing
319the input queue size to be set.
320If this is greater than maximum allowable
321size (see EIOCGETP above), it is set to the maximum,
322and if it is zero, it is set to
323a default value.
324.sp
325Another
326.I
327ioctl
328call flushes the queue of incoming packets.
329It is of the
330form:
331.sp
332.nf
333.RS
334.B #include <sys/types.h>
335.B #include <sys/enet.h>
336.B ioctl(fildes, EIOCFLUSH, 0)
337.RE
338.fi
339.sp
340The final
341.I
342ioctl
343call is used to set the packet filter
344for an open
345.I
346enet
347file.
348It is of the form:
349.sp
350.nf
351.RS
352.B #include <sys/types.h>
353.B #include <sys/enet.h>
354.B ioctl(fildes, EIOCSETF, filter)
355.B struct enfilter *filter
356.RE
357.fi
358.sp
359where enfilter is defined in
360.I
361<sys/enet.h>
362as:
363.sp
364.nf
365.RS
366.ft B
367.ta \w'struct 'u \w'struct u_short 'u
368struct enfilter
369{
370 u_char enf_Priority;
371 u_char enf_FilterLen;
372 u_short enf_Filter[ENMAXFILTERS];
373};
374.DT
375.ft R
376.RE
377.fi
378.PP
379A packet filter consists of a priority,
380the filter command list length (in shortwords),
381and the filter command list itself.
382Each filter command list specifies
383a sequence of actions which
384operate on an internal stack.
385Each shortword of the
386command list specifies an action from the set {
387.B
388ENF_PUSHLIT,
389.B
390ENF_PUSHZERO,
391.B
392ENF_PUSHWORD+N
393} which respectively push the next shortword of the
394command list, zero,
395or shortword
396.B
397N
398of the incoming packet on the stack, and a binary operator
399from the set {
400.B
401ENF_EQ,
402.B
403ENF_NEQ,
404.B
405ENF_LT,
406.B
407ENF_LE,
408.B
409ENF_GT,
410.B
411ENF_GE,
412.B
413ENF_AND,
414.B
415ENF_OR,
416.B
417ENF_XOR
418}
419which then operates on the
420top two elements of the stack and replaces them with its result.
421When both an action and operator are specified in the
422same shortword, the action is performed followed by the
423operation.
424.PP
425The binary operator can also be from the set {
426.B
427ENF_COR,
428.B
429ENF_CAND,
430.B
431ENF_CNOR,
432.B
433ENF_CNAND
434}. These are ``short-circuit'' operators, in that they terminate
435the execution of the filter immediately if the condition they are checking
436for is found, and continue otherwise.
437All pop two elements from the stack and compare them for equality;
438.B
439ENF_CAND
440returns false if the result is false;
441.B
442ENF_COR
443returns true if the result is true;
444.B
445ENF_CNAND
446returns true if the result is false;
447.B
448ENF_CNOR
449returns false if the result is true.
450Unlike the other binary operators, these four do not leave a result
451on the stack, even if they continue.
452.PP
453The short-circuit operators should be used when possible, to reduce the
454amount of time spent evaluating filters. When they are used, you should
455also arrange the order of the tests so that the filter will succeed or fail
456as soon as possible; for example, checking the Socket field of a Pup packet
457is more likely to indicate failure than the packet type field.
458.PP
459The
460special action
461.B
462ENF_NOPUSH
463and the special operator
464.B
465ENF_NOP
466can be used to only
467perform the binary operation or to only push a value on the stack.
468Since both are (conveniently) defined to be zero, indicating
469only an action actually specifies the action followed by
470.BR ENF_NOP ,
471and
472indicating only an operation actually specifies
473.B
474ENF_NOPUSH
475followed
476by the operation.
477.PP
478After executing the filter command list, a non-zero value (true)
479left on top of the stack
480(or an empty stack) causes the incoming
481packet to be accepted for the corresponding
482.I
483enet
484file and a zero value (false) causes the packet to
485be passed through the next packet filter.
486(If the filter exits as the result of a short-circuit operator,
487the top-of-stack value is ignored.)
488Specifying an undefined operation or action in the command list
489or performing an illegal operation or action (such as pushing
490a shortword offset
491past the end of the packet or executing a binary operator
492with fewer than two shortwords on the stack) causes a filter to
493reject the packet.
494.sp
495In an attempt to deal with the problem of
496overlapping and/or conflicting packet filters,
497the filters for each open
498.I
499enet
500file are ordered by the driver
501according to their priority
502(lowest
503priority is 0, highest is 255).
504When processing incoming
505ethernet
506packets, filters are applied according to their
507priority (from highest to lowest) and
508for identical priority values according to their
509relative ``busyness'' (the filter that has previously
510matched the most packets is checked first) until one or more filters
511accept the packet or all filters reject it and
512it is discarded.
513.PP
514Filters at a priority of 2 or higher are called "high priority"
515filters.
516Once a packet is delivered to one of these "high priority"
517.I
518enet
519files,
520no further filters are examined,
521i.e.
522the packet is delivered only
523to the first
524.I
525enet
526file with a
527"high priority" filter
528which accepts the packet.
529A packet may be delivered to more than one filter with a priority
530below 2; this might be useful, for example, in building replicated programs.
531However, the use of low-priority filters imposes an additional cost on
532the system, as these filters each must be checked against all packets not
533accepted by a high-priority filter.
534.PP
535The packet filter for an
536.I
537enet
538file is initialized
539with length 0 at priority 0 by
540.IR open (2),
541and hence by default accepts all
542packets which no "high priority" filter
543is interested in.
544.PP
545Priorities should be assigned so that, in general, the more packets a
546filter is expected to match, the higher its priority. This will prevent
547a lot of needless checking of packets against filters that aren't likely
548to match them.
549.SH "FILTER EXAMPLES"
550The following filter would accept all incoming
551.I Pup
552packets on a 3mb ethernet with Pup types in the range 1-0100:
553.sp
554.nf
555.ft B
556.ta \w'stru'u \w'struct ENF_PUSHWORD+8, ENF_PUSHLIT, 2, 'u
557struct enfilter f =
558{
559 10, 19, /* priority and length */
560 ENF_PUSHWORD+1, ENF_PUSHLIT, 2,
561 ENF_EQ, /* packet type == PUP */
562 ENF_PUSHWORD+3, ENF_PUSHLIT,
563 0xFF00, ENF_AND, /* mask high byte */
564 ENF_PUSHZERO, ENF_GT, /* PupType > 0 */
565 ENF_PUSHWORD+3, ENF_PUSHLIT,
566 0xFF00, ENF_AND, /* mask high byte */
567 ENF_PUSHLIT, 0100, ENF_LE, /* PupType <= 0100 */
568 ENF_AND, /* 0 < PupType <= 0100 */
569 ENF_AND /* && packet type == PUP */
570};
571.DT
572.ft R
573.fi
574.sp
575Note that shortwords, such as the packet type field, are byte-swapped
576and so the literals you compare them to must be byte-swapped. Also,
577although for this example the word offsets are constants, code that
578must run with either 3mb or 10mb ethernets must use
579offsets that depend on the device type.
580.PP
581By taking advantage of the ability to
582specify both an action and operation in each word of
583the command list, the filter could be abbreviated to:
584.sp
585.nf
586.ta \w'stru'u \w'struct ENF_PUSHWORD+3, ENF_PUSHLIT | ENF_AND, 'u
587.ft B
588struct enfilter f =
589{
590 10, 14, /* priority and length */
591 ENF_PUSHWORD+1, ENF_PUSHLIT | ENF_EQ, 2, /* packet type == PUP */
592 ENF_PUSHWORD+3, ENF_PUSHLIT | ENF_AND,
593 0xFF00, /* mask high byte */
594 ENF_PUSHZERO | ENF_GT, /* PupType > 0 */
595 ENF_PUSHWORD+3, ENF_PUSHLIT | ENF_AND,
596 0xFF00, /* mask high byte */
597 ENF_PUSHLIT | ENF_LE, 0100, /* PupType <= 0100 */
598 ENF_AND, /* 0 < PupType <= 0100 */
599 ENF_AND /* && packet type == PUP */
600};
601.ft R
602.DT
603.fi
604.sp
605A different example shows the use of "short-circuit" operators to
606create a more efficient filter. This one accepts Pup packets (on a 3Mbit
607ethernet) with a Socket field of 12345. Note that we check the Socket field
608before the packet type field, since in most
609packets the Socket is not likely to match.
610.sp
611.nf
612.ta \w'stru'u \w'struct ENF_PUSHWORD+3, ENF_PUSHLIT | ENF_CAND, 'u
613.ft B
614struct enfilter f =
615{
616 10, 9, /* priority and length */
617 ENF_PUSHWORD+7, ENF_PUSHLIT | ENF_CAND,
618 0, /* High word of socket */
619 ENF_PUSHWORD+8, ENF_PUSHLIT | ENF_CAND,
620 12345, /* Low word of socket */
621 ENF_PUSHWORD+1, ENF_PUSHLIT | ENF_CAND,
622 2 /* packet type == Pup */
623};
624.ft R
625.DT
626.fi
627.SH "SEE ALSO"
628de(4), ec(4), en(4), il(4), enstat(8)
629.SH "FILES"
630/dev/enet{,a,b,c,...}0
631.SH BUGS
632The current implementation can only filter on words within
633the first "mbuf" of the packet; this is around 100 bytes (or
63450 words).
635.PP
636Because packets are streams of bytes, yet the filters operate
637on short words, and standard network byte order is usually opposite
638from Vax byte order, the relational operators
639.B ENF_LT, ENF_LE,
640.B ENF_GT,
641and
642.B ENF_GE
643are not all that useful. Fortunately, they were not often used
644when the packets were treated as streams of shorts, so this is
645probably not a severe problem. If this becomes a severe problem,
646a byte-swapping operator could be added.
647.PP
648Many of the "features" of this driver are there for historical
649reasons; the manual page could be a lot cleaner if these were
650left out.
651.SH "HISTORY"
652.TP
6538-Oct-85 Jeffrey Mogul at Stanford University
654Revised to describe 4.3BSD version of driver.
655.TP
65618-Oct-84 Jeffrey Mogul at Stanford University
657Added short-circuit operators, changed discussion of priorities to
658reflect new arrangement.
659.TP
66018-Jan-84 Jeffrey Mogul at Stanford University
661Updated for 4.2BSD (device-independent) version, including
662documentation of all non-kernel ioctls.
663.TP
66417-Nov-81 Mike Accetta (mja) at Carnegie-Mellon University
665Added mention of <sys/types.h> to include examples.
666.TP
66729-Sep-81 Mike Accetta (mja) at Carnegie-Mellon University
668Changed to describe new EIOCSETW and EIOCFLUSH ioctl
669calls and the new multiple packet queuing features.
670.TP
67112-Nov-80 Mike Accetta (mja) at Carnegie-Mellon University
672Added description of signal mechanism for input packets.
673.TP
67407-Mar-80 Mike Accetta (mja) at Carnegie-Mellon University
675Created.