packetfilter.4
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 4 代码 · 共 1,077 行 · 第 1/2 页
4
1,077 行
.br.ne .5i.IP ens_count.brThe length of the packet in bytes (does not include the.PN enstampheader)..br.ne .5i.IP ens_dropped.brThe number of packets accepted by this filter but dropped because the inputqueue was full; this is a cumulative count since the previous.PN enstampwas read from thispacket filterfile. This count may be completely wrong if the ENNONEXCL modebit is set for this filter..br.ne .5i.IP ens_ifoverflows.brThe total number of input overflows reported by the network interface since thesystem was booted..br.ne .5i.IP ens_tstamp.brThe approximate time the packet was received..br.PPIf the buffer returned by a batched.MS read 2contains more than one packet, the offset from the beginning of the bufferat which each.PN enstampstructure begins is an integer multiple of the word-size of the processor.For example, on a VAX, each.PN enstampis aligned on a longword boundary (provided that the buffer address passed tothe.MS read 2system call is aligned).The alignment (in units of bytes) is given by the constant.PN ENALIGNMENT ,defined in.PN <net/pfilt.h> .If you have an integer.IR x ,you can use the macro\f(CWENALIGN(\fIx\fP)\fRto get the least integer that is a multiple of.PN ENALIGNMENTand not less than\fIx\fR.For example, this code fragment reads and processes one batch:.ne 1i.EXchar *buffer = &(BigBuffer[0]);int buflen;int pktlen, stamplen;struct enstamp *stamp;buflen = read(f, buffer, sizeof(BigBuffer));while (buflen > 0) { stamp = (struct enstamp *)buffer; pktlen = stamp->ens_count; stamplen = stamp->ens_stamplen; ProcessPacket(&(buffer[stamplen]), pktlen); /* your code here */ if (buflen == (pktlen + stamplen)) break; /* last packet in batch */ pktlen = ENALIGN(pktlen); /* account for alignment padding */ buflen -= (pktlen + stamplen); buffer += (pktlen + stamplen); /* move to next stamp */}.EE.PPIf a buffer filled by a batched readcontains more than one packet, the final packet is never truncated.If, however, the entire buffer is not big enough to contain a singlepacket, the packet will be truncated; this is also true for unbatchedreads. Therefore, the buffer passed to the.MS read 2system call should always be big enough to hold the largestpossible packet plus an.PN enstampstructure.(See the .PN EIOCDEVP.PN ioctlrequest later in this reference page for information on how to determine the maximum packetsize. See also the.PN EIOCTRUNCATE.PN ioctlrequest for an example that delivers only the desirednumber of bytes of a packet.).ne 1.5i.PPNormally, a packet filter application blocks in the.PN readsystem call until a received packet is available for reading.There are several ways to avoid blocking indefinitely: an applicationcan use the.MS select 2system call, it can set a ``timeout'' for the packet filter file,or it can request the delivery of a signal (see.MS sigvec 2 )when a packet matches the filter..ne 1i.i0.DT.PP.IP EIOCSETW.brThe packet filter interface limits the number of packets that canbe queued for delivery for a specific packet filter file. Applicationprograms can vary this ``backlog'', if necessary, using thefollowing call:.EXioctl(\fIfildes\fP, EIOCSETW, \fImaxwaitingp\fP)u_int *\fImaxwaitingp\fP;.EEThe argument.I maxwaitingppoints to an integer containingthe input queue size to be set.If this is greater than the maximum allowablesize (see EIOCMAXBACKLOG later), it is set to the maximum.If it is zero, it is set to a default value..br.ne 1i.IP EIOCFLUSH.brAfter changing the packet filter program, the input queue may contain packetsthat were accepted under the old filter.To flush the queue of incoming packets, use the following:.EXioctl(\fIfildes\fP, EIOCFLUSH, 0).EE.ne 1i.IP EIOCTRUNCATE.brAn application, such as a network load monitor, that does not want tosee the entire packet can ask the packet filter to truncatereceived packets at a specified length. This action may improve performanceby reducing data movement. .sp .5To specify truncation, use:.EXioctl(\fIfildes\fP, EIOCTRUNCATE, \fItruncationp\fP)u_int *\fItruncationp\fP;.EEThe argument.I truncationppoints to an integer specifying the truncation length, in bytes.Packets shorter than this length are passed intact..PPThis example, a revision of the previous example, illustratesthe use of EIOCTRUNCATE, which causes the packet filter todeliver only the first \fIn\fR bytes of a packet, not theentire packet..ne 1i.EXchar *buffer = &(BigBuffer[0]);int buflen;int pktlen, stamplen;struct enstamp *stamp;int truncation = SIZE_OF_INTERESTING_PART_OF_PACKET;if (ioctl(f, EIOCTRUNCATE, &truncation) < 0) exit(1); while (1) { buflen = read(f, buffer, sizeof(BigBuffer)); while (buflen > 0) { stamp = (struct enstamp *)buffer; pktlen = stamp->ens_count; /* ens_count is untruncated length */ stamplen = stamp->ens_stamplen; .sp .5 ProcessPacket(&(buffer[stamplen]), pktlen); /* your code here */.sp .5 if (pktlen > truncation) /* truncated portion not in buffer */ pktlen = truncation; if (buflen == (pktlen + stamplen)) break; /* last packet in batch */ pktlen = ENALIGN(pktlen); /* account for alignment padding */ buflen -= (pktlen + stamplen); buffer += (pktlen + stamplen); /* move to next stamp */ }}.EE.PPTwo calls control the timeout mechanism; they are of the followingform:.EX#include <net/time.h>.sp .5ioctl(\fIfildes\fP, \fIcode\fP, \fItvp\fP).sp .5struct timeval *\fItvp\fP;.EE.DT.i0.PPThe.I tvpargument is the address of a.PN struct.PN timevalcontaining the timeout interval (this is a relativevalue, not an absolute time). The codes are:.IP EIOCGRTIMEOUT.brReturns the current timeout value..br.ne 4.IP EIOCSRTIMEOUT.brSets the timeout value.When the value ispositive, a.MS read 2call returns a 0 if no packet arrives during the period.When the timeout value is zero, reads block indefinitely (thisis the default). When the value is negative, a.MS read 2call returns a 0 immediately if there are no queued packets.Note that the largest legal timeout valueis a few million seconds..PPTwo calls control the signal-on-reception mechanism;they are of the following form:.EXioctl(\fIfildes\fP, \fIcode\fP, \fIsignp\fP)u_int *\fIsignp\fP;.EE.PPThe argument.I signpis a pointer to an integer containing the numberof the signalto be sent when an input packet arrives.The applicable.I codesare:.br.ne 6.IP EIOCENBS.brEnables the specified signal when an input packetis received for this file.If the ENHOLDSIG flag (see EIOCMBIS later) is not set,further signals are automatically disabledwhenever a signal is sent to prevent nesting, and hencemust be explicitly re-enabled after processing.When the signal number is 0,this call is equivalent to EIOCINHS..IP EIOCINHS.brDisables signaling on packet reception.The.I signpargumentis ignored.This is the default when the file is first opened..ne 1i.SS Device Configuration ioctl Requests.IP EIOCIFNAME.brEach packet filter file is associated with a specific network interface.To find out the name of the interface underlying thepacket filter file, use the following:.EX#include <net/socket.h>#include <net/if.h>.spioctl(\fIfildes\fP, EIOCIFNAME, \fIifr\fP)struct ifreq *\fIifr\fP;.EEThe interface name (for example, ``de0'') is returned in.I ifr->ifr_name\fR;other fields of the.I struct ifreqare not set..IP EIOCSETIF.brTo set the interface associated with a packet filter file, usethe following:.EXioctl(\fIfildes\fP, EIOCSETIF, \fIifr\fP)struct ifreq *\fIifr\fP;.EE.sp .5The interface name should be passed.I ifr->ifr_name\fR;other fields of the.I struct ifreqare ignored.The name provided may be one of the actual interface names,such as ``de0'' or ``qe1'', or it may be a pseudo-interfacename of the form ``pf\fIn\fP'',used to specify the .I n\fRth\fRinterface attached to the system.For example, ``pf0'' specifiesthe first interface.This is useful for applicationsthat do not know the names of specific interfaces.Pseudo-interface names are never returned by EIOCIFNAME..ne 6.IP EIOCDEVP.brTo get device parameters of the network interface underlying thepacket filter file, use the following:.EXioctl(\fIfildes\fP, EIOCDEVP, \fIparam\fP)struct endevp *\fIparam\fP;.EE.ne 1.75iThe.PN endevpstructure is defined in.PN <net/pfilt.h>as:.EX.ta \w'struct 'u +\w'u_short 'ustruct endevp { u_char end_dev_type; u_char end_addr_len; u_short end_hdr_len; u_short end_MTU; u_char end_addr[EN_MAX_ADDR_LEN]; u_char end_broadaddr[EN_MAX_ADDR_LEN];};.DT.EEThe fields are:.RS.IP end_dev_type 1.5iSpecifies the device type: ENDT_3MB, ENDT_BS3MB, or ENDT_10MB..IP end_addr_len 1.5iSpecifies the address length in bytes (for example, 1 or 6)..IP end_hdr_len 1.5iSpecifies the total header length in bytes (for example, 4 or 14)..IP end_MTU 1.5iSpecifies the maximum packet size, including header, in bytes..IP end_addr 1.5iThe address of this interface; aligned so that the low orderbyte of the address is in.IR end_addr[0] ..IP end_broadaddr 1.5iThe hardware destination address for broadcasts on this network..RE.SS Administrative ioctl Requests.IP EIOCMAXBACKLOG.brThe maximum queue length that can be set using EIOCSETW dependson whether the process is running as the superuser or not. Ifso, the maximum is a kernel constant; otherwise, the maximum is avalue that can be set, by the superuser, for each interface.To set the maximum non-superuser backlog for an interface, useEIOCSETIF to bind to the interface, and then use the following:.EXioctl(\fIfildes\fP, EIOCMAXBACKLOG, \fImaxbacklogp\fP)int *\fImaxbacklogp\fP;.EEThe argument.I maxbacklogppoints to an integer containing the maximum value.(If.I maxbacklogppoints to an integer containing a negative value, it is replacedwith the current backlog value, and no action is taken.).IP EIOCALLOWPROMISC.brCertain kinds of network-monitoring applications need to place theinterface in ``promiscuous mode'', where it receives all packets onthe network. Promiscuous mode can be set by the superuser with the.I /etc/ifconfigcommand, or the superuser can configure an interfaceto go into promiscuous mode automatically if any packet filter applicationshave the ENPROMISC mode bit set. To do so, useEIOCSETIF to bind to the interface, and then use the following:.EXioctl(\fIfildes\fP, EIOCALLOWPROMISC, \fIallowp\fP)int *\fIallowp\fP;.EEThe argument.I allowppoints to an integer containing a Boolean value (nonzeromeans promiscuous mode is set automatically).(If.I allowppoints to an integer containing a negative value, it is replacedwith the current Boolean value, and no action is taken.).IP EIOCALLOWCOPYALL.brCertain promiscuous-mode network-monitoring applications need to seeunicast packets sent or received by the local host. For reasons ofefficiency, these packets are not normally provided to the packet filter,but in ``copy all'' mode they are. The superuser can configure an interface to go into copy-all mode automatically if any packet filterapplications have the ENCOPYALL mode bit set. To do so, use EIOCSETIFto bind to the interface, and then use the following:.EXioctl(\fIfildes\fP, EIOCALLOWCOPYALL, \fIallowp\fP)int *\fIallowp\fP;.EEThe argument.I allowppoints to an integer containing a Boolean value (nonzeromeans copy-all mode is set automatically).(If.I allowppoints to an integer containing a negative value, it is replaced withthe current Boolean value, and no action is taken.).IP EIOCMFREE.brTo find out how many packet filter files remain for opening, use this.PN ioctl ,which places the number in the integer pointed to by\fImfree\fR:.EXioctl(\fIfildes\fP, EIOCMFREE, \fImfree\fP)int *\fImfree\fP;.EE.br.ne 1.5i.SS Miscellaneous ioctl RequestsTwo calls are provided for backwards compatibility and should not be usedin new code. These calls are used to set and fetch parametersof a packet filter file(\fInot\fRthe underlying device; see EIOCDEVP).The form for these calls is:.EX#include <sys/types.h>#include <net/pfilt.h>.sp .5ioctl(\fIfildes\fP, \fIcode\fP, \fIparam\fP).sp .5struct eniocb *\fIparam\fP;.EE.sp .5The structure.PN eniocbis defined in .PN <net/pfilt.h>as:.EX.ta \w'struct 'u +\w'u_char 'ustruct eniocb{ u_char en_addr; u_char en_maxfilters; u_char en_maxwaiting; u_char en_maxpriority; long en_rtout;};.DT.EE.i0.sp .5The applicable.I codesare:.IP EIOCGETP.brFetch the parameters for this file..IP EIOCSETP.brSet the parameters for this file..i0.DT.sp .5All the fields, which are described later, except \fIen_rtout\fR,are read-only..RS.IP en_addr 1.5iNo longer maintained; use EIOCDEVP..IP en_maxfilters 1.5iThe maximum length of a filter command list; see EIOCSETF..br.ne 3.IP en_maxwaiting 1.5iThe maximum number of packets that can be queued for reading on thepacket filterfile; use EIOCMAXBACKLOG..IP en_maxpriority 1.5iThe highest allowable filter priority; see EIOCSETF..IP en_rtout 1.5iThe number of clock ticks towait before timing out on a read request and returning a zero length.If zero, reads block indefinitely until a packet arrives.If negative, read requests return a zero lengthimmediately if there are no packets in the input queue.Initialized to zero by.MS open 2 ,indicating no timeout.(Use EIOCSRTIMEOUT and EIOCGRTIMEOUT.).RE.br.ne 1.5i.SH RestrictionsBecause the packet filter include file .PN <net/pfilt.h>was originally named .PN <sys/enet.h> ,some filter applications may need to be updated..PPA previous restriction against accessing data words past approximatelythe firsthundred bytes in a packet has been removed. However, it becomesslightly more costly to examine words that are not near the beginningof the packet..PPBecause packets are streams of bytes, yet the filters operateon short words, and standard network byte order is usually oppositefrom VAX byte order, the relational operatorsENF_LT, ENF_LE,ENF_GT,andENF_GEare not particularly useful. If this becomes a severe problem,a byte-swapping operator could be added..SH Files.PN /dev/pf/pfilt\fInnn\fR Packet filter special files.SH See Alsoethers(3n), pfopen(3), de(4), ln(4), ni(4), qe(4), xna(4), ifconfig(8),MAKEDEV(8), pfconfig(8c), pfstat(8).br\fIThe Packet Filter: An Efficient Mechanism for User-Level NetworkCode\fR
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?