packetfilter.4

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 4 代码 · 共 1,077 行 · 第 1/2 页

4
1,077
字号
.TH packetfilter 4.SH Namepacketfilter \- Ethernet packet filter.SH Syntax.B "options	PACKETFILTER".br.B "pseudo-device	packetfilter".SH Description.NXR "packetfilter"The packet filter pseudo-device driverprovides a raw interface to Ethernets and similar network data link layers.Packets received that are not used by the kernel(for example, to support the IP and DECnet protocol families)are available through this mechanism.The packet filter driver is kernel-resident code provided bythe ULTRIX operating system.  The driver appears toapplications as a set of character special files, onefor each open packet filter application.(Throughout this reference page, the word.I filerefers to such a character special file.).PPYou create the minor device fileswith the.MS MAKEDEV 8script usingthese commands:.EX# \f(CBcd /dev\fP# \f(CBMAKEDEV pfilt.EEA single call to.PN MAKEDEVwith an argument of.PN pfiltcreates 64 character special files in .PN /dev/pf ,which are named.PN pfilt\fInnn ,\fRwhere \fInnn\fP isthe unit number.Successive calls to.PN MAKEDEVwith arguments of.PN pfilt1 ,.PN pfilt2 ,and.PN pfilt3make additional sets of 64 sequentially numbered packet filtersto a maximum of 256.The maximum number of packet filter special filesis limited to 256, which is the maximum number ofminor device numbers allowed for each major device number.(See .MS MAKEDEV 8 for more information on making system special files.).PPFor opening these special files, the ULTRIX operating systemprovides the.MS pfopen 3 library routine..PPAssociated with each open instance of apacket filter specialfile is a user-settable packet filter ``program''that is used to select which incoming packets are delivered by thatpacket filter special file.Whenever a packet is received from the net,the packet filter driver successively applies the filterprograms of each of the open packet filter files to thepacket, until one filter program ``accepts'' the packet.When a filter accepts the packet,it is placed on the packet input queue of theassociated special file.If no filters accept the packet, it is discarded.The format of a packet filter is described later..PPReads from these files return the next packetfrom a queue of packets that have matched the filter.If the read operation specifies insufficient buffer spaceto store the entire packet,the packet is truncated and the trailing contents lost.Writes to these files transmit packets on thenetwork, with each write operation generating exactly one packet..PPThe packet filter supports a variety of different Ethernetdata-link levels:.IP "10Mb Ethernet".brPackets consist of fourteen or more bytes, with the first sixbytes specifying the destination Ethernet address,the next six bytes the source Ethernet address,and the next two bytes specifying the packet type.(This is the Standard Ethernet.).IP "3Mb Ethernet".brPackets consist of four or more bytes, with the first bytespecifying the source Ethernet address, the secondbyte specifying the destination Ethernet address,and the next two bytes specifying the packet type.(On the network, the source and destination addressesare in the opposite order.).br.ne 6.IP "Byte-swapping 3Mb Ethernet".brPackets consist of four or more bytes, with the first bytespecifying the source Ethernet address, the secondbyte specifying the destination Ethernet address,and the next two bytes specifying the packet type.Each short word (pair of bytes) is swapped from the networkbyte order.  This device type is provided only forbackwards-compatibility..PPThe remaining words are interpreted according to the packet type.Note that 16-bit and 32-bit quantities may have to be byteswapped(and possibly short-swapped) to be intelligible on an ULTRIX system..PPThe packet filters treat the entire packet,including headers, as uninterpreted data.The user must supplythe headers for transmitted packets (although the system makes sure thatthe source address is correct) and the headers of received packetsare delivered to the user.The packet filter mechanism does not know anything about the dataportion of the packets it sends and receives..PPIn addition to the FIONREAD.PN ioctlrequest (described in the .MS tty 4reference page), the application can apply several special.PN ioctlrequests to an openpacket filter file.  The calls are divided into five categories:packet-filter specifying,packet handling,device configuration,administrative,and miscellaneous..br.ne 1i.SS Packet-filter Specification ioctl RequestThe.PN EIOCSETF.PN ioctlis central to the operation of the packet filter interface, becauseit specifies which packets the application wishes to receive.It is used to set the packet filter ``program''for an openpacket filter file, and is of the form:.EXioctl(\fIfildes\fP, EIOCSETF, \fIfilter\fP)struct enfilter *\fIfilter\fP.EEThe.PN enfilterstructure is defined in .PN <net/pfilt.h>as:.EX.ta \w'struct 'u \w'struct u_short  'ustruct enfilter{	u_char	enf_Priority;	u_char	enf_FilterLen;	u_short	enf_Filter[ENMAXFILTERS];};.DT.EE.PPA packet filter consists of a priority,the filter command list length (in shortwords),and the filter command list itself.Each filter command list specifiesa sequence of actions thatoperate on an internal stack.Each shortword of thecommand list specifies an action and a binary operator.  .SS Command List ActionsThe actioncan be one of the following:.IP ENF_PUSHLIT.brPushes the next shortword of the command list on the stack..IP ENF_PUSHWORD+N.brPushes shortword N of the incoming packet on the stack..br.ne 3.IP ENF_PUSHZERO.brPushes a zero.  Is slightly faster than ENF_PUSHLIT with anexplicit literal..IP ENF_PUSHONE.brPushes a one.  Is slightly faster than ENF_PUSHLIT with an explicit literal..IP ENF_PUSHFFFF.brPushes 0xFFFF.  Is slightly faster than ENF_PUSHLIT with anexplicit literal..IP ENF_PUSH00FF.brPushes  0x00FF.  Is slightly faster than ENF_PUSHLIT with anexplicit literal..IP ENF_PUSHFF00.brPushes 0xFF00.  Is slightly faster than ENF_PUSHLIT with anexplicit literal..IP ENF_NOPUSH.brDefined as zero..SS Binary OperatorsWhen both an action and an operator are specified in thesame shortword, the action is performed, followed by theoperation.You can combine an action with an operator using bitwise OR;for example,.EX((ENF_PUSHWORD+3) | ENF_EQ).EE.PPThe binary operator, which can be one of the following, operates onthe top two elements of the stack and replaces them with itsresult:.IP ENF_EQ 15Returns true if the result is equal..IP ENF_NEQReturns true if the result is not equal..IP ENF_LTReturns true if the result is less than..IP ENF_LEReturns true if the result is less than or equal..IP ENF_GTReturns true if the result is greater than..IP ENF_GEReturns true if the result is greater than or equal..IP ENF_ANDReturns the result of the binary AND operation..IP ENF_ORReturns the result of the binary OR operation..IP ENF_XORReturns the result of the binary XOR operation..IP ENF_NOPDefined as zero..IP ENF_CANDReturns false immediately if the result is false,and continues execution of the filter otherwise.(Short-circuit operator).IP ENF_CORReturns true immediately if the result is true,and continues execution of the filter otherwise.(Short-circuit operator).IP ENF_CNANDReturns true immediately if the result is false,and continues execution of the filter otherwise.(Short-circuit operator).IP ENF_CNORReturns false immediately if the result is true,and continues execution of the filter otherwise.(Short-circuit operator).PPThe short-circuit operators are so called because they terminatethe execution of the filter immediately if the condition they are checking for is found, and continue otherwise.All the short-circuit operators pop two elements from the stack and compare them for equality.Unlike the other binary operators, these four operators do not leave a resulton the stack, even if they continue..PPUse the short-circuit operators whenever possible, to reduce theamount of time spent evaluating filters.  When you use them, you shouldalso arrange the order of the tests so that the filter will succeed or failas soon as possible. For example, checking a word inan address field of an Ethernet packet is more likely to indicate failure than the Ethernet type field..PPThespecial actionENF_NOPUSHand the special operatorENF_NOPcan be used to only perform the binary operation orto only push a value on the stack.Because both are defined to be zero, specifyingonly an action actually specifies the action followed byENF_NOP, and specifying only an operation actually specifiesENF_NOPUSHfollowedby the operation..PPAfter executing the filter command list, a nonzero value (true)left on top of the stack(or an empty stack) causes the incomingpacket to be accepted for the correspondingpacket filter file and a zero value (false) causes the packet tobe passed through the next packet filter.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 listor performing an illegal operation or action (such as pushinga shortword offsetpast the end of the packet or executing a binary operatorwith fewer than two shortwords on the stack) causes a filter toreject the packet..PPTo resolve problems withoverlapping or conflicting packet filters,the filters for each openpacket filter file are ordered by the driveraccording to their priority(lowestpriority is 0, highest is 255).When processing incomingpackets, filters are applied according to theirpriority (from highest to lowest) andfor identical priority values according to theirrelative ``busyness'' (the filter that has previouslymatched the most packets is checked first), until one or more filtersaccept the packet or all filters reject it andit is discarded..PPNormally once a packet is delivered to a filter, it is not presented to anyother filters.  However, if the packet is accepted by a filter innonexclusive mode (ENNONEXCL set using EIOCMBIS,described in the following section), the packet ispassed along to lower-priority filters and may be delivered more thanonce.The use of nonexclusive filters imposes an additional cost onthe system, because it increases the average number of filters applied to eachpacket..PPThe packet filter for a packet filter file is initializedwith length 0 at priority 0 by.MS open 2 ,and hence, by default, accepts allpackets in which no higher-priority filteris interested..PPPriorities should be assigned so that, in general, the more packets afilter is expected to match, the higher its priority.  This preventsa lot of checking of packets against filters that are unlikelyto match them..br.ne 3i.PPThe filter in this example accepts incomingRARP (Reverse Address Resolution Protocol) broadcast packets..PPThe filter first checks the Ethernet type of the packet.If it is not a RARP (Reverse ARP) packet, it is discarded. Then, the RARP type field is checked for a reverse request (type 3),followed by a check for a broadcast destination address.Note that the packet type field is checked before the destination address,because the total number of broadcast packets on the network is largerthan the number of RARP packets.  Thus, the filter isordered with a minimum amount of processing overhead..EX.ta \w'stru'u \w'struct ENF_PUSHWORD,      'ustruct enfilter f ={	36, 0,	/* priority and length */	ENF_PUSHWORD + 6,	ENF_PUSHLIT, 0x3580,	ENF_CAND,	/* Ethernet type == 0x8035 (RARP) */	ENF_PUSHWORD + 10,	ENF_PUSHLIT, 0x0300,	ENF_CAND,	/* reverse request type = 0003 */	ENF_PUSHWORD + 0,	ENF_PUSHLIT, 0xFFFF,	ENF_CAND,	/* dest addr = FF-FF */	ENF_PUSHWORD + 1,	ENF_PUSHLIT, 0xFFFF,	ENF_CAND,	/* dest addr = FF-FF */	ENF_PUSHWORD + 2,	ENF_PUSHLIT, 0xFFFF,	ENF_EQ	/* dest addr = FF-FF */};.DT.EE.PPNote that shortwords, such as the packet type field, are in networkbyte-order.The literals you compare them to may have to be byte-swappedon machines like the VAX..br.ne 3i.PPBy taking advantage of the ability tospecify both an action and operation in each word ofthe command list, you could abbreviate the filter to thefollowing:.EX.ta \w'stru'u \w'struct ENF_PUSHLIT | ENF_CAND,  'ustruct enfilter f ={	36, 0,	/* priority and length */	ENF_PUSHWORD + 6,	ENF_PUSHLIT | ENF_CAND,	0x3580,	/* Ethernet type == 0x8035 (RARP) */	ENF_PUSHWORD + 10,	ENF_PUSHLIT | ENF_CAND,	0x0300,	/* reverse request type = 0003 */	ENF_PUSHWORD + 0,	ENF_PUSHFFFF | ENF_CAND,	/* dest addr = FF-FF */	ENF_PUSHWORD + 1,	ENF_PUSHFFFF | ENF_CAND,	/* dest addr = FF-FF */	ENF_PUSHWORD + 2,	ENF_PUSHFFFF | ENF_EQ	/* dest addr = FF-FF */};.DT.EE.ne 1i.SS Packet-Handling ioctl RequestsThese.PN ioctlrequests control how the packet filter processes input packetsand returns them to the application process.The most useful of these requests set and clear so-called``mode bits'' for the file and are of this form:.EXioctl(\fIfildes\fP, \fIcode\fP, \fIbits\fP)u_short *\fIbits\fP;.DT.EE.i0.PPIn these calls,.I bitsis a bitmask specifying which bits to set or clear.  The applicable.I codesare:.IP EIOCMBIS.brSets the specified mode bits..IP EIOCMBIC.brClears the specified mode bits..br.ne 1i.PPThe bits are:.IP ENTSTAMP.brIf set, a received packet is preceded by a header structure (see thedescription of.PN enstampfollowing) that includes a time stamp and other information..IP ENBATCH.brIf clear, each.MS read 2system call returns at most one packet.  If set, a.PN readcall might return more than one packet, each of which is preceded by an.PN enstampheader..IP ENPROMISC.brIf set, this filter will be applied to promiscuously-received packets.This puts the interface into ``promiscuous mode'' only if thishas been allowed by the superuser using the EIOCALLOWPROMISC.PN ioctlcall (described later)..IP ENCOPYALL.brIf set, this filter will see packets sent and received by thekernel-resident protocols of the local host.  (Normally, these packetsare not copied to the packet filter.)  This mode takes effect only if thishas been allowed by the superuser using the EIOCALLOWCOPYALL.PN ioctlcall (described later)..br.ne 4.IP ENNONEXCL.brIf set, packets accepted by this filter will be available toany lower-priority filters.  If clear, no lower-priority filter will seepackets accepted by this filter..IP ENHOLDSIG.brIf clear,means that the driver shoulddisable the effect of EIOCENBS (described later)once it has delivered a signal.If set (the default), the effect of EIOCENBS persists..PPThe.PN enstampstructure contains useful information about the packet that immediatelyfollows it; in ENBATCH mode, it also allows the reader to separate thepackets in a batch.  It is defined in.PN <net/pfilt.h>as:.EX.ta \w'struct 'u +\w'struct timeval  'ustruct enstamp {	u_short	ens_stamplen;	u_short	ens_flags;	u_short	ens_count;	u_short	ens_dropped;	u_long	ens_ifoverflows;	struct	timeval	ens_tstamp;};.EE.i0.DT.PPThe fields are:.IP ens_stamplen.brThe length of .PN enstampstructure in bytes.  The packet data follows immediately..br.ne 1.25i.IP ens_flags.brIndicates how the packet was received. The bits are:.RS.IP ENSF_PROMISC.brReceived promiscuously (unicast to some other host)..IP ENSF_BROADCAST.brReceived as a broadcast..IP ENSF_MULTICAST.brReceived as a multicast..IP ENSF_TRAILER.brReceived in a trailer encapsulation.  The packet has been rearranged intoheader format..RE

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?