Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998  | 
3  |  |  *  The Regents of the University of California.  All rights reserved.  | 
4  |  |  *  | 
5  |  |  * Redistribution and use in source and binary forms, with or without  | 
6  |  |  * modification, are permitted provided that: (1) source code distributions  | 
7  |  |  * retain the above copyright notice and this paragraph in its entirety, (2)  | 
8  |  |  * distributions including binary code include the above copyright notice and  | 
9  |  |  * this paragraph in its entirety in the documentation or other materials  | 
10  |  |  * provided with the distribution, and (3) all advertising materials mentioning  | 
11  |  |  * features or use of this software display the following acknowledgement:  | 
12  |  |  * ``This product includes software developed by the University of California,  | 
13  |  |  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of  | 
14  |  |  * the University nor the names of its contributors may be used to endorse  | 
15  |  |  * or promote products derived from this software without specific prior  | 
16  |  |  * written permission.  | 
17  |  |  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED  | 
18  |  |  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF  | 
19  |  |  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  | 
20  |  |  */  | 
21  |  |  | 
22  |  | #ifdef HAVE_CONFIG_H  | 
23  |  | #include <config.h>  | 
24  |  | #endif  | 
25  |  |  | 
26  |  | #ifdef _WIN32  | 
27  |  |   #include <ws2tcpip.h>  | 
28  |  | #else  | 
29  |  |   #include <sys/socket.h>  | 
30  |  |  | 
31  |  |   #ifdef __NetBSD__  | 
32  |  |     #include <sys/param.h>  | 
33  |  |   #endif  | 
34  |  |  | 
35  |  |   #include <netinet/in.h>  | 
36  |  |   #include <arpa/inet.h>  | 
37  |  | #endif /* _WIN32 */  | 
38  |  |  | 
39  |  | #include <stdlib.h>  | 
40  |  | #include <string.h>  | 
41  |  | #include <memory.h>  | 
42  |  | #include <setjmp.h>  | 
43  |  | #include <stdarg.h>  | 
44  |  | #include <stdio.h>  | 
45  |  |  | 
46  |  | #ifdef MSDOS  | 
47  |  | #include "pcap-dos.h"  | 
48  |  | #endif  | 
49  |  |  | 
50  |  | #include "pcap-int.h"  | 
51  |  |  | 
52  |  | #include "extract.h"  | 
53  |  |  | 
54  |  | #include "ethertype.h"  | 
55  |  | #include "nlpid.h"  | 
56  |  | #include "llc.h"  | 
57  |  | #include "gencode.h"  | 
58  |  | #include "ieee80211.h"  | 
59  |  | #include "atmuni31.h"  | 
60  |  | #include "sunatmpos.h"  | 
61  |  | #include "pflog.h"  | 
62  |  | #include "ppp.h"  | 
63  |  | #include "pcap/sll.h"  | 
64  |  | #include "pcap/ipnet.h"  | 
65  |  | #include "arcnet.h"  | 
66  |  | #include "diag-control.h"  | 
67  |  |  | 
68  |  | #include "scanner.h"  | 
69  |  |  | 
70  |  | #if defined(linux)  | 
71  |  | #include <linux/types.h>  | 
72  |  | #include <linux/if_packet.h>  | 
73  |  | #include <linux/filter.h>  | 
74  |  | #endif  | 
75  |  |  | 
76  |  | #ifndef offsetof  | 
77  |  | #define offsetof(s, e) ((size_t)&((s *)0)->e)  | 
78  |  | #endif  | 
79  |  |  | 
80  |  | #ifdef _WIN32  | 
81  |  |   #ifdef INET6  | 
82  |  |     #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)  | 
83  |  | /* IPv6 address */  | 
84  |  | struct in6_addr  | 
85  |  |   { | 
86  |  |     union  | 
87  |  |       { | 
88  |  |   uint8_t   u6_addr8[16];  | 
89  |  |   uint16_t  u6_addr16[8];  | 
90  |  |   uint32_t  u6_addr32[4];  | 
91  |  |       } in6_u;  | 
92  |  | #define s6_addr     in6_u.u6_addr8  | 
93  |  | #define s6_addr16   in6_u.u6_addr16  | 
94  |  | #define s6_addr32   in6_u.u6_addr32  | 
95  |  | #define s6_addr64   in6_u.u6_addr64  | 
96  |  |   };  | 
97  |  |  | 
98  |  | typedef unsigned short  sa_family_t;  | 
99  |  |  | 
100  |  | #define __SOCKADDR_COMMON(sa_prefix) \  | 
101  |  |   sa_family_t sa_prefix##family  | 
102  |  |  | 
103  |  | /* Ditto, for IPv6.  */  | 
104  |  | struct sockaddr_in6  | 
105  |  |   { | 
106  |  |     __SOCKADDR_COMMON (sin6_);  | 
107  |  |     uint16_t sin6_port;   /* Transport layer port # */  | 
108  |  |     uint32_t sin6_flowinfo; /* IPv6 flow information */  | 
109  |  |     struct in6_addr sin6_addr;  /* IPv6 address */  | 
110  |  |   };  | 
111  |  |  | 
112  |  |       #ifndef EAI_ADDRFAMILY  | 
113  |  | struct addrinfo { | 
114  |  |   int ai_flags; /* AI_PASSIVE, AI_CANONNAME */  | 
115  |  |   int ai_family;  /* PF_xxx */  | 
116  |  |   int ai_socktype;  /* SOCK_xxx */  | 
117  |  |   int ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */  | 
118  |  |   size_t  ai_addrlen; /* length of ai_addr */  | 
119  |  |   char  *ai_canonname;  /* canonical name for hostname */  | 
120  |  |   struct sockaddr *ai_addr; /* binary address */  | 
121  |  |   struct addrinfo *ai_next; /* next structure in linked list */  | 
122  |  | };  | 
123  |  |       #endif /* EAI_ADDRFAMILY */  | 
124  |  |     #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */  | 
125  |  |   #endif /* INET6 */  | 
126  |  | #else /* _WIN32 */  | 
127  |  |   #include <netdb.h>  /* for "struct addrinfo" */  | 
128  |  | #endif /* _WIN32 */  | 
129  |  | #include <pcap/namedb.h>  | 
130  |  |  | 
131  |  | #include "nametoaddr.h"  | 
132  |  |  | 
133  | 305  | #define ETHERMTU  1500  | 
134  |  |  | 
135  |  | #ifndef IPPROTO_HOPOPTS  | 
136  |  | #define IPPROTO_HOPOPTS 0  | 
137  |  | #endif  | 
138  |  | #ifndef IPPROTO_ROUTING  | 
139  |  | #define IPPROTO_ROUTING 43  | 
140  |  | #endif  | 
141  |  | #ifndef IPPROTO_FRAGMENT  | 
142  |  | #define IPPROTO_FRAGMENT 44  | 
143  |  | #endif  | 
144  |  | #ifndef IPPROTO_DSTOPTS  | 
145  |  | #define IPPROTO_DSTOPTS 60  | 
146  |  | #endif  | 
147  |  | #ifndef IPPROTO_SCTP  | 
148  |  | #define IPPROTO_SCTP 132  | 
149  |  | #endif  | 
150  |  |  | 
151  | 0  | #define GENEVE_PORT 6081  | 
152  |  |  | 
153  |  | #ifdef HAVE_OS_PROTO_H  | 
154  |  | #include "os-proto.h"  | 
155  |  | #endif  | 
156  |  |  | 
157  | 10.3k  | #define JMP(c) ((c)|BPF_JMP|BPF_K)  | 
158  |  |  | 
159  |  | /*  | 
160  |  |  * "Push" the current value of the link-layer header type and link-layer  | 
161  |  |  * header offset onto a "stack", and set a new value.  (It's not a  | 
162  |  |  * full-blown stack; we keep only the top two items.)  | 
163  |  |  */  | 
164  | 0  | #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \  | 
165  | 0  | { \ | 
166  | 0  |   (cs)->prevlinktype = (cs)->linktype; \  | 
167  | 0  |   (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \  | 
168  | 0  |   (cs)->linktype = (new_linktype); \  | 
169  | 0  |   (cs)->off_linkhdr.is_variable = (new_is_variable); \  | 
170  | 0  |   (cs)->off_linkhdr.constant_part = (new_constant_part); \  | 
171  | 0  |   (cs)->off_linkhdr.reg = (new_reg); \  | 
172  | 0  |   (cs)->is_geneve = 0; \  | 
173  | 0  | }  | 
174  |  |  | 
175  |  | /*  | 
176  |  |  * Offset "not set" value.  | 
177  |  |  */  | 
178  | 41.1k  | #define OFFSET_NOT_SET  0xffffffffU  | 
179  |  |  | 
180  |  | /*  | 
181  |  |  * Absolute offsets, which are offsets from the beginning of the raw  | 
182  |  |  * packet data, are, in the general case, the sum of a variable value  | 
183  |  |  * and a constant value; the variable value may be absent, in which  | 
184  |  |  * case the offset is only the constant value, and the constant value  | 
185  |  |  * may be zero, in which case the offset is only the variable value.  | 
186  |  |  *  | 
187  |  |  * bpf_abs_offset is a structure containing all that information:  | 
188  |  |  *  | 
189  |  |  *   is_variable is 1 if there's a variable part.  | 
190  |  |  *  | 
191  |  |  *   constant_part is the constant part of the value, possibly zero;  | 
192  |  |  *  | 
193  |  |  *   if is_variable is 1, reg is the register number for a register  | 
194  |  |  *   containing the variable value if the register has been assigned,  | 
195  |  |  *   and -1 otherwise.  | 
196  |  |  */  | 
197  |  | typedef struct { | 
198  |  |   int is_variable;  | 
199  |  |   u_int constant_part;  | 
200  |  |   int reg;  | 
201  |  | } bpf_abs_offset;  | 
202  |  |  | 
203  |  | /*  | 
204  |  |  * Value passed to gen_load_a() to indicate what the offset argument  | 
205  |  |  * is relative to the beginning of.  | 
206  |  |  */  | 
207  |  | enum e_offrel { | 
208  |  |   OR_PACKET,    /* full packet data */  | 
209  |  |   OR_LINKHDR,   /* link-layer header */  | 
210  |  |   OR_PREVLINKHDR,   /* previous link-layer header */  | 
211  |  |   OR_LLC,     /* 802.2 LLC header */  | 
212  |  |   OR_PREVMPLSHDR,   /* previous MPLS header */  | 
213  |  |   OR_LINKTYPE,    /* link-layer type */  | 
214  |  |   OR_LINKPL,    /* link-layer payload */  | 
215  |  |   OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */  | 
216  |  |   OR_TRAN_IPV4,   /* transport-layer header, with IPv4 network layer */  | 
217  |  |   OR_TRAN_IPV6    /* transport-layer header, with IPv6 network layer */  | 
218  |  | };  | 
219  |  |  | 
220  |  | /*  | 
221  |  |  * We divy out chunks of memory rather than call malloc each time so  | 
222  |  |  * we don't have to worry about leaking memory.  It's probably  | 
223  |  |  * not a big deal if all this memory was wasted but if this ever  | 
224  |  |  * goes into a library that would probably not be a good idea.  | 
225  |  |  *  | 
226  |  |  * XXX - this *is* in a library....  | 
227  |  |  */  | 
228  | 131k  | #define NCHUNKS 16  | 
229  | 4.58k  | #define CHUNK0SIZE 1024  | 
230  |  | struct chunk { | 
231  |  |   size_t n_left;  | 
232  |  |   void *m;  | 
233  |  | };  | 
234  |  |  | 
235  |  | /*  | 
236  |  |  * A chunk can store any of:  | 
237  |  |  *  - a string (guaranteed alignment 1 but present for completeness)  | 
238  |  |  *  - a block  | 
239  |  |  *  - an slist  | 
240  |  |  *  - an arth  | 
241  |  |  * For this simple allocator every allocated chunk gets rounded up to the  | 
242  |  |  * alignment needed for any chunk.  | 
243  |  |  */  | 
244  |  | struct chunk_align { | 
245  |  |   char dummy;  | 
246  |  |   union { | 
247  |  |     char c;  | 
248  |  |     struct block b;  | 
249  |  |     struct slist s;  | 
250  |  |     struct arth a;  | 
251  |  |   } u;  | 
252  |  | };  | 
253  | 115k  | #define CHUNK_ALIGN (offsetof(struct chunk_align, u))  | 
254  |  |  | 
255  |  | /* Code generator state */  | 
256  |  |  | 
257  |  | struct _compiler_state { | 
258  |  |   jmp_buf top_ctx;  | 
259  |  |   pcap_t *bpf_pcap;  | 
260  |  |   int error_set;  | 
261  |  |  | 
262  |  |   struct icode ic;  | 
263  |  |  | 
264  |  |   int snaplen;  | 
265  |  |  | 
266  |  |   int linktype;  | 
267  |  |   int prevlinktype;  | 
268  |  |   int outermostlinktype;  | 
269  |  |  | 
270  |  |   bpf_u_int32 netmask;  | 
271  |  |   int no_optimize;  | 
272  |  |  | 
273  |  |   /* Hack for handling VLAN and MPLS stacks. */  | 
274  |  |   u_int label_stack_depth;  | 
275  |  |   u_int vlan_stack_depth;  | 
276  |  |  | 
277  |  |   /* XXX */  | 
278  |  |   u_int pcap_fddipad;  | 
279  |  |  | 
280  |  |   /*  | 
281  |  |    * As errors are handled by a longjmp, anything allocated must  | 
282  |  |    * be freed in the longjmp handler, so it must be reachable  | 
283  |  |    * from that handler.  | 
284  |  |    *  | 
285  |  |    * One thing that's allocated is the result of pcap_nametoaddrinfo();  | 
286  |  |    * it must be freed with freeaddrinfo().  This variable points to  | 
287  |  |    * any addrinfo structure that would need to be freed.  | 
288  |  |    */  | 
289  |  |   struct addrinfo *ai;  | 
290  |  |  | 
291  |  |   /*  | 
292  |  |    * Another thing that's allocated is the result of pcap_ether_aton();  | 
293  |  |    * it must be freed with free().  This variable points to any  | 
294  |  |    * address that would need to be freed.  | 
295  |  |    */  | 
296  |  |   u_char *e;  | 
297  |  |  | 
298  |  |   /*  | 
299  |  |    * Various code constructs need to know the layout of the packet.  | 
300  |  |    * These values give the necessary offsets from the beginning  | 
301  |  |    * of the packet data.  | 
302  |  |    */  | 
303  |  |  | 
304  |  |   /*  | 
305  |  |    * Absolute offset of the beginning of the link-layer header.  | 
306  |  |    */  | 
307  |  |   bpf_abs_offset off_linkhdr;  | 
308  |  |  | 
309  |  |   /*  | 
310  |  |    * If we're checking a link-layer header for a packet encapsulated  | 
311  |  |    * in another protocol layer, this is the equivalent information  | 
312  |  |    * for the previous layers' link-layer header from the beginning  | 
313  |  |    * of the raw packet data.  | 
314  |  |    */  | 
315  |  |   bpf_abs_offset off_prevlinkhdr;  | 
316  |  |  | 
317  |  |   /*  | 
318  |  |    * This is the equivalent information for the outermost layers'  | 
319  |  |    * link-layer header.  | 
320  |  |    */  | 
321  |  |   bpf_abs_offset off_outermostlinkhdr;  | 
322  |  |  | 
323  |  |   /*  | 
324  |  |    * Absolute offset of the beginning of the link-layer payload.  | 
325  |  |    */  | 
326  |  |   bpf_abs_offset off_linkpl;  | 
327  |  |  | 
328  |  |   /*  | 
329  |  |    * "off_linktype" is the offset to information in the link-layer  | 
330  |  |    * header giving the packet type. This is an absolute offset  | 
331  |  |    * from the beginning of the packet.  | 
332  |  |    *  | 
333  |  |    * For Ethernet, it's the offset of the Ethernet type field; this  | 
334  |  |    * means that it must have a value that skips VLAN tags.  | 
335  |  |    *  | 
336  |  |    * For link-layer types that always use 802.2 headers, it's the  | 
337  |  |    * offset of the LLC header; this means that it must have a value  | 
338  |  |    * that skips VLAN tags.  | 
339  |  |    *  | 
340  |  |    * For PPP, it's the offset of the PPP type field.  | 
341  |  |    *  | 
342  |  |    * For Cisco HDLC, it's the offset of the CHDLC type field.  | 
343  |  |    *  | 
344  |  |    * For BSD loopback, it's the offset of the AF_ value.  | 
345  |  |    *  | 
346  |  |    * For Linux cooked sockets, it's the offset of the type field.  | 
347  |  |    *  | 
348  |  |    * off_linktype.constant_part is set to OFFSET_NOT_SET for no  | 
349  |  |    * encapsulation, in which case, IP is assumed.  | 
350  |  |    */  | 
351  |  |   bpf_abs_offset off_linktype;  | 
352  |  |  | 
353  |  |   /*  | 
354  |  |    * TRUE if the link layer includes an ATM pseudo-header.  | 
355  |  |    */  | 
356  |  |   int is_atm;  | 
357  |  |  | 
358  |  |   /*  | 
359  |  |    * TRUE if "geneve" appeared in the filter; it causes us to  | 
360  |  |    * generate code that checks for a Geneve header and assume  | 
361  |  |    * that later filters apply to the encapsulated payload.  | 
362  |  |    */  | 
363  |  |   int is_geneve;  | 
364  |  |  | 
365  |  |   /*  | 
366  |  |    * TRUE if we need variable length part of VLAN offset  | 
367  |  |    */  | 
368  |  |   int is_vlan_vloffset;  | 
369  |  |  | 
370  |  |   /*  | 
371  |  |    * These are offsets for the ATM pseudo-header.  | 
372  |  |    */  | 
373  |  |   u_int off_vpi;  | 
374  |  |   u_int off_vci;  | 
375  |  |   u_int off_proto;  | 
376  |  |  | 
377  |  |   /*  | 
378  |  |    * These are offsets for the MTP2 fields.  | 
379  |  |    */  | 
380  |  |   u_int off_li;  | 
381  |  |   u_int off_li_hsl;  | 
382  |  |  | 
383  |  |   /*  | 
384  |  |    * These are offsets for the MTP3 fields.  | 
385  |  |    */  | 
386  |  |   u_int off_sio;  | 
387  |  |   u_int off_opc;  | 
388  |  |   u_int off_dpc;  | 
389  |  |   u_int off_sls;  | 
390  |  |  | 
391  |  |   /*  | 
392  |  |    * This is the offset of the first byte after the ATM pseudo_header,  | 
393  |  |    * or -1 if there is no ATM pseudo-header.  | 
394  |  |    */  | 
395  |  |   u_int off_payload;  | 
396  |  |  | 
397  |  |   /*  | 
398  |  |    * These are offsets to the beginning of the network-layer header.  | 
399  |  |    * They are relative to the beginning of the link-layer payload  | 
400  |  |    * (i.e., they don't include off_linkhdr.constant_part or  | 
401  |  |    * off_linkpl.constant_part).  | 
402  |  |    *  | 
403  |  |    * If the link layer never uses 802.2 LLC:  | 
404  |  |    *  | 
405  |  |    *  "off_nl" and "off_nl_nosnap" are the same.  | 
406  |  |    *  | 
407  |  |    * If the link layer always uses 802.2 LLC:  | 
408  |  |    *  | 
409  |  |    *  "off_nl" is the offset if there's a SNAP header following  | 
410  |  |    *  the 802.2 header;  | 
411  |  |    *  | 
412  |  |    *  "off_nl_nosnap" is the offset if there's no SNAP header.  | 
413  |  |    *  | 
414  |  |    * If the link layer is Ethernet:  | 
415  |  |    *  | 
416  |  |    *  "off_nl" is the offset if the packet is an Ethernet II packet  | 
417  |  |    *  (we assume no 802.3+802.2+SNAP);  | 
418  |  |    *  | 
419  |  |    *  "off_nl_nosnap" is the offset if the packet is an 802.3 packet  | 
420  |  |    *  with an 802.2 header following it.  | 
421  |  |    */  | 
422  |  |   u_int off_nl;  | 
423  |  |   u_int off_nl_nosnap;  | 
424  |  |  | 
425  |  |   /*  | 
426  |  |    * Here we handle simple allocation of the scratch registers.  | 
427  |  |    * If too many registers are alloc'd, the allocator punts.  | 
428  |  |    */  | 
429  |  |   int regused[BPF_MEMWORDS];  | 
430  |  |   int curreg;  | 
431  |  |  | 
432  |  |   /*  | 
433  |  |    * Memory chunks.  | 
434  |  |    */  | 
435  |  |   struct chunk chunks[NCHUNKS];  | 
436  |  |   int cur_chunk;  | 
437  |  | };  | 
438  |  |  | 
439  |  | /*  | 
440  |  |  * For use by routines outside this file.  | 
441  |  |  */  | 
442  |  | /* VARARGS */  | 
443  |  | void  | 
444  |  | bpf_set_error(compiler_state_t *cstate, const char *fmt, ...)  | 
445  | 657  | { | 
446  | 657  |   va_list ap;  | 
447  |  |  | 
448  |  |   /*  | 
449  |  |    * If we've already set an error, don't override it.  | 
450  |  |    * The lexical analyzer reports some errors by setting  | 
451  |  |    * the error and then returning a LEX_ERROR token, which  | 
452  |  |    * is not recognized by any grammar rule, and thus forces  | 
453  |  |    * the parse to stop.  We don't want the error reported  | 
454  |  |    * by the lexical analyzer to be overwritten by the syntax  | 
455  |  |    * error.  | 
456  |  |    */  | 
457  | 657  |   if (!cstate->error_set) { | 
458  | 642  |     va_start(ap, fmt);  | 
459  | 642  |     (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,  | 
460  | 642  |         fmt, ap);  | 
461  | 642  |     va_end(ap);  | 
462  | 642  |     cstate->error_set = 1;  | 
463  | 642  |   }  | 
464  | 657  | }  | 
465  |  |  | 
466  |  | /*  | 
467  |  |  * For use *ONLY* in routines in this file.  | 
468  |  |  */  | 
469  |  | static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...)  | 
470  |  |     PCAP_PRINTFLIKE(2, 3);  | 
471  |  |  | 
472  |  | /* VARARGS */  | 
473  |  | static void PCAP_NORETURN  | 
474  |  | bpf_error(compiler_state_t *cstate, const char *fmt, ...)  | 
475  | 171  | { | 
476  | 171  |   va_list ap;  | 
477  |  |  | 
478  | 171  |   va_start(ap, fmt);  | 
479  | 171  |   (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE,  | 
480  | 171  |       fmt, ap);  | 
481  | 171  |   va_end(ap);  | 
482  | 171  |   longjmp(cstate->top_ctx, 1);  | 
483  |  |   /*NOTREACHED*/  | 
484  |  | #ifdef _AIX  | 
485  |  |   PCAP_UNREACHABLE  | 
486  |  | #endif /* _AIX */  | 
487  | 171  | }  | 
488  |  |  | 
489  |  | static int init_linktype(compiler_state_t *, pcap_t *);  | 
490  |  |  | 
491  |  | static void init_regs(compiler_state_t *);  | 
492  |  | static int alloc_reg(compiler_state_t *);  | 
493  |  | static void free_reg(compiler_state_t *, int);  | 
494  |  |  | 
495  |  | static void initchunks(compiler_state_t *cstate);  | 
496  |  | static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t);  | 
497  |  | static void *newchunk(compiler_state_t *cstate, size_t);  | 
498  |  | static void freechunks(compiler_state_t *cstate);  | 
499  |  | static inline struct block *new_block(compiler_state_t *cstate, int);  | 
500  |  | static inline struct slist *new_stmt(compiler_state_t *cstate, int);  | 
501  |  | static struct block *gen_retblk(compiler_state_t *cstate, int);  | 
502  |  | static inline void syntax(compiler_state_t *cstate);  | 
503  |  |  | 
504  |  | static void backpatch(struct block *, struct block *);  | 
505  |  | static void merge(struct block *, struct block *);  | 
506  |  | static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int,  | 
507  |  |     u_int, bpf_u_int32);  | 
508  |  | static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int,  | 
509  |  |     u_int, bpf_u_int32);  | 
510  |  | static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int,  | 
511  |  |     u_int, bpf_u_int32);  | 
512  |  | static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int,  | 
513  |  |     u_int, bpf_u_int32);  | 
514  |  | static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int,  | 
515  |  |     u_int, bpf_u_int32);  | 
516  |  | static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int,  | 
517  |  |     u_int, bpf_u_int32, bpf_u_int32);  | 
518  |  | static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int,  | 
519  |  |     u_int, const u_char *);  | 
520  |  | static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int,  | 
521  |  |     u_int, bpf_u_int32, int, int, bpf_u_int32);  | 
522  |  | static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *,  | 
523  |  |     u_int, u_int);  | 
524  |  | static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int,  | 
525  |  |     u_int);  | 
526  |  | static struct slist *gen_loadx_iphdrlen(compiler_state_t *);  | 
527  |  | static struct block *gen_uncond(compiler_state_t *, int);  | 
528  |  | static inline struct block *gen_true(compiler_state_t *);  | 
529  |  | static inline struct block *gen_false(compiler_state_t *);  | 
530  |  | static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32);  | 
531  |  | static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32);  | 
532  |  | static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32);  | 
533  |  | static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *);  | 
534  |  | static struct slist *gen_load_prism_llprefixlen(compiler_state_t *);  | 
535  |  | static struct slist *gen_load_avs_llprefixlen(compiler_state_t *);  | 
536  |  | static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *);  | 
537  |  | static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *);  | 
538  |  | static void insert_compute_vloffsets(compiler_state_t *, struct block *);  | 
539  |  | static struct slist *gen_abs_offset_varpart(compiler_state_t *,  | 
540  |  |     bpf_abs_offset *);  | 
541  |  | static bpf_u_int32 ethertype_to_ppptype(bpf_u_int32);  | 
542  |  | static struct block *gen_linktype(compiler_state_t *, bpf_u_int32);  | 
543  |  | static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32);  | 
544  |  | static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32);  | 
545  |  | static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32,  | 
546  |  |     int, bpf_u_int32, u_int, u_int);  | 
547  |  | #ifdef INET6  | 
548  |  | static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *,  | 
549  |  |     struct in6_addr *, int, bpf_u_int32, u_int, u_int);  | 
550  |  | #endif  | 
551  |  | static struct block *gen_ahostop(compiler_state_t *, const u_char *, int);  | 
552  |  | static struct block *gen_ehostop(compiler_state_t *, const u_char *, int);  | 
553  |  | static struct block *gen_fhostop(compiler_state_t *, const u_char *, int);  | 
554  |  | static struct block *gen_thostop(compiler_state_t *, const u_char *, int);  | 
555  |  | static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int);  | 
556  |  | static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int);  | 
557  |  | static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int);  | 
558  |  | static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32);  | 
559  |  | static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32,  | 
560  |  |     int, int, int);  | 
561  |  | #ifdef INET6  | 
562  |  | static struct block *gen_host6(compiler_state_t *, struct in6_addr *,  | 
563  |  |     struct in6_addr *, int, int, int);  | 
564  |  | #endif  | 
565  |  | #ifndef INET6  | 
566  |  | static struct block *gen_gateway(compiler_state_t *, const u_char *,  | 
567  |  |     struct addrinfo *, int, int);  | 
568  |  | #endif  | 
569  |  | static struct block *gen_ipfrag(compiler_state_t *);  | 
570  |  | static struct block *gen_portatom(compiler_state_t *, int, bpf_u_int32);  | 
571  |  | static struct block *gen_portrangeatom(compiler_state_t *, u_int, bpf_u_int32,  | 
572  |  |     bpf_u_int32);  | 
573  |  | static struct block *gen_portatom6(compiler_state_t *, int, bpf_u_int32);  | 
574  |  | static struct block *gen_portrangeatom6(compiler_state_t *, u_int, bpf_u_int32,  | 
575  |  |     bpf_u_int32);  | 
576  |  | static struct block *gen_portop(compiler_state_t *, u_int, u_int, int);  | 
577  |  | static struct block *gen_port(compiler_state_t *, u_int, int, int);  | 
578  |  | static struct block *gen_portrangeop(compiler_state_t *, u_int, u_int,  | 
579  |  |     bpf_u_int32, int);  | 
580  |  | static struct block *gen_portrange(compiler_state_t *, u_int, u_int, int, int);  | 
581  |  | struct block *gen_portop6(compiler_state_t *, u_int, u_int, int);  | 
582  |  | static struct block *gen_port6(compiler_state_t *, u_int, int, int);  | 
583  |  | static struct block *gen_portrangeop6(compiler_state_t *, u_int, u_int,  | 
584  |  |     bpf_u_int32, int);  | 
585  |  | static struct block *gen_portrange6(compiler_state_t *, u_int, u_int, int, int);  | 
586  |  | static int lookup_proto(compiler_state_t *, const char *, int);  | 
587  |  | #if !defined(NO_PROTOCHAIN)  | 
588  |  | static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int);  | 
589  |  | #endif /* !defined(NO_PROTOCHAIN) */  | 
590  |  | static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int, int);  | 
591  |  | static struct slist *xfer_to_x(compiler_state_t *, struct arth *);  | 
592  |  | static struct slist *xfer_to_a(compiler_state_t *, struct arth *);  | 
593  |  | static struct block *gen_mac_multicast(compiler_state_t *, int);  | 
594  |  | static struct block *gen_len(compiler_state_t *, int, int);  | 
595  |  | static struct block *gen_check_802_11_data_frame(compiler_state_t *);  | 
596  |  | static struct block *gen_geneve_ll_check(compiler_state_t *cstate);  | 
597  |  |  | 
598  |  | static struct block *gen_ppi_dlt_check(compiler_state_t *);  | 
599  |  | static struct block *gen_atmfield_code_internal(compiler_state_t *, int,  | 
600  |  |     bpf_u_int32, int, int);  | 
601  |  | static struct block *gen_atmtype_llc(compiler_state_t *);  | 
602  |  | static struct block *gen_msg_abbrev(compiler_state_t *, int type);  | 
603  |  |  | 
604  |  | static void  | 
605  |  | initchunks(compiler_state_t *cstate)  | 
606  | 3.72k  | { | 
607  | 3.72k  |   int i;  | 
608  |  |  | 
609  | 63.3k  |   for (i = 0; i < NCHUNKS; i++) { | 
610  | 59.6k  |     cstate->chunks[i].n_left = 0;  | 
611  | 59.6k  |     cstate->chunks[i].m = NULL;  | 
612  | 59.6k  |   }  | 
613  | 3.72k  |   cstate->cur_chunk = 0;  | 
614  | 3.72k  | }  | 
615  |  |  | 
616  |  | static void *  | 
617  |  | newchunk_nolongjmp(compiler_state_t *cstate, size_t n)  | 
618  | 57.6k  | { | 
619  | 57.6k  |   struct chunk *cp;  | 
620  | 57.6k  |   int k;  | 
621  | 57.6k  |   size_t size;  | 
622  |  |  | 
623  |  |   /* Round up to chunk alignment. */  | 
624  | 57.6k  |   n = (n + CHUNK_ALIGN - 1) & ~(CHUNK_ALIGN - 1);  | 
625  |  |  | 
626  | 57.6k  |   cp = &cstate->chunks[cstate->cur_chunk];  | 
627  | 57.6k  |   if (n > cp->n_left) { | 
628  | 4.58k  |     ++cp;  | 
629  | 4.58k  |     k = ++cstate->cur_chunk;  | 
630  | 4.58k  |     if (k >= NCHUNKS) { | 
631  | 0  |       bpf_set_error(cstate, "out of memory");  | 
632  | 0  |       return (NULL);  | 
633  | 0  |     }  | 
634  | 4.58k  |     size = CHUNK0SIZE << k;  | 
635  | 4.58k  |     cp->m = (void *)malloc(size);  | 
636  | 4.58k  |     if (cp->m == NULL) { | 
637  | 0  |       bpf_set_error(cstate, "out of memory");  | 
638  | 0  |       return (NULL);  | 
639  | 0  |     }  | 
640  | 4.58k  |     memset((char *)cp->m, 0, size);  | 
641  | 4.58k  |     cp->n_left = size;  | 
642  | 4.58k  |     if (n > size) { | 
643  | 0  |       bpf_set_error(cstate, "out of memory");  | 
644  | 0  |       return (NULL);  | 
645  | 0  |     }  | 
646  | 4.58k  |   }  | 
647  | 57.6k  |   cp->n_left -= n;  | 
648  | 57.6k  |   return (void *)((char *)cp->m + cp->n_left);  | 
649  | 57.6k  | }  | 
650  |  |  | 
651  |  | static void *  | 
652  |  | newchunk(compiler_state_t *cstate, size_t n)  | 
653  | 57.5k  | { | 
654  | 57.5k  |   void *p;  | 
655  |  |  | 
656  | 57.5k  |   p = newchunk_nolongjmp(cstate, n);  | 
657  | 57.5k  |   if (p == NULL) { | 
658  | 0  |     longjmp(cstate->top_ctx, 1);  | 
659  |  |     /*NOTREACHED*/  | 
660  | 0  |   }  | 
661  | 57.5k  |   return (p);  | 
662  | 57.5k  | }  | 
663  |  |  | 
664  |  | static void  | 
665  |  | freechunks(compiler_state_t *cstate)  | 
666  | 3.72k  | { | 
667  | 3.72k  |   int i;  | 
668  |  |  | 
669  | 63.3k  |   for (i = 0; i < NCHUNKS; ++i)  | 
670  | 59.6k  |     if (cstate->chunks[i].m != NULL)  | 
671  | 4.58k  |       free(cstate->chunks[i].m);  | 
672  | 3.72k  | }  | 
673  |  |  | 
674  |  | /*  | 
675  |  |  * A strdup whose allocations are freed after code generation is over.  | 
676  |  |  * This is used by the lexical analyzer, so it can't longjmp; it just  | 
677  |  |  * returns NULL on an allocation error, and the callers must check  | 
678  |  |  * for it.  | 
679  |  |  */  | 
680  |  | char *  | 
681  |  | sdup(compiler_state_t *cstate, const char *s)  | 
682  | 181  | { | 
683  | 181  |   size_t n = strlen(s) + 1;  | 
684  | 181  |   char *cp = newchunk_nolongjmp(cstate, n);  | 
685  |  |  | 
686  | 181  |   if (cp == NULL)  | 
687  | 0  |     return (NULL);  | 
688  | 181  |   pcap_strlcpy(cp, s, n);  | 
689  | 181  |   return (cp);  | 
690  | 181  | }  | 
691  |  |  | 
692  |  | static inline struct block *  | 
693  |  | new_block(compiler_state_t *cstate, int code)  | 
694  | 14.5k  | { | 
695  | 14.5k  |   struct block *p;  | 
696  |  |  | 
697  | 14.5k  |   p = (struct block *)newchunk(cstate, sizeof(*p));  | 
698  | 14.5k  |   p->s.code = code;  | 
699  | 14.5k  |   p->head = p;  | 
700  |  |  | 
701  | 14.5k  |   return p;  | 
702  | 14.5k  | }  | 
703  |  |  | 
704  |  | static inline struct slist *  | 
705  |  | new_stmt(compiler_state_t *cstate, int code)  | 
706  | 39.4k  | { | 
707  | 39.4k  |   struct slist *p;  | 
708  |  |  | 
709  | 39.4k  |   p = (struct slist *)newchunk(cstate, sizeof(*p));  | 
710  | 39.4k  |   p->s.code = code;  | 
711  |  |  | 
712  | 39.4k  |   return p;  | 
713  | 39.4k  | }  | 
714  |  |  | 
715  |  | static struct block *  | 
716  |  | gen_retblk(compiler_state_t *cstate, int v)  | 
717  | 5.00k  | { | 
718  | 5.00k  |   struct block *b = new_block(cstate, BPF_RET|BPF_K);  | 
719  |  |  | 
720  | 5.00k  |   b->s.k = v;  | 
721  | 5.00k  |   return b;  | 
722  | 5.00k  | }  | 
723  |  |  | 
724  |  | static inline PCAP_NORETURN_DEF void  | 
725  |  | syntax(compiler_state_t *cstate)  | 
726  | 3  | { | 
727  | 3  |   bpf_error(cstate, "syntax error in filter expression");  | 
728  | 3  | }  | 
729  |  |  | 
730  |  | int  | 
731  |  | pcap_compile(pcap_t *p, struct bpf_program *program,  | 
732  |  |        const char *buf, int optimize, bpf_u_int32 mask)  | 
733  | 3.72k  | { | 
734  |  | #ifdef _WIN32  | 
735  |  |   static int done = 0;  | 
736  |  | #endif  | 
737  | 3.72k  |   compiler_state_t cstate;  | 
738  | 3.72k  |   const char * volatile xbuf = buf;  | 
739  | 3.72k  |   yyscan_t scanner = NULL;  | 
740  | 3.72k  |   volatile YY_BUFFER_STATE in_buffer = NULL;  | 
741  | 3.72k  |   u_int len;  | 
742  | 3.72k  |   int  rc;  | 
743  |  |  | 
744  |  |   /*  | 
745  |  |    * If this pcap_t hasn't been activated, it doesn't have a  | 
746  |  |    * link-layer type, so we can't use it.  | 
747  |  |    */  | 
748  | 3.72k  |   if (!p->activated) { | 
749  | 0  |     snprintf(p->errbuf, PCAP_ERRBUF_SIZE,  | 
750  | 0  |         "not-yet-activated pcap_t passed to pcap_compile");  | 
751  | 0  |     return (PCAP_ERROR);  | 
752  | 0  |   }  | 
753  |  |  | 
754  |  | #ifdef _WIN32  | 
755  |  |   if (!done)  | 
756  |  |     pcap_wsockinit();  | 
757  |  |   done = 1;  | 
758  |  | #endif  | 
759  |  |  | 
760  |  | #ifdef ENABLE_REMOTE  | 
761  |  |   /*  | 
762  |  |    * If the device on which we're capturing need to be notified  | 
763  |  |    * that a new filter is being compiled, do so.  | 
764  |  |    *  | 
765  |  |    * This allows them to save a copy of it, in case, for example,  | 
766  |  |    * they're implementing a form of remote packet capture, and  | 
767  |  |    * want the remote machine to filter out the packets in which  | 
768  |  |    * it's sending the packets it's captured.  | 
769  |  |    *  | 
770  |  |    * XXX - the fact that we happen to be compiling a filter  | 
771  |  |    * doesn't necessarily mean we'll be installing it as the  | 
772  |  |    * filter for this pcap_t; we might be running it from userland  | 
773  |  |    * on captured packets to do packet classification.  We really  | 
774  |  |    * need a better way of handling this, but this is all that  | 
775  |  |    * the WinPcap remote capture code did.  | 
776  |  |    */  | 
777  |  |   if (p->save_current_filter_op != NULL)  | 
778  |  |     (p->save_current_filter_op)(p, buf);  | 
779  |  | #endif  | 
780  |  |  | 
781  | 3.72k  |   initchunks(&cstate);  | 
782  | 3.72k  |   cstate.no_optimize = 0;  | 
783  | 3.72k  | #ifdef INET6  | 
784  | 3.72k  |   cstate.ai = NULL;  | 
785  | 3.72k  | #endif  | 
786  | 3.72k  |   cstate.e = NULL;  | 
787  | 3.72k  |   cstate.ic.root = NULL;  | 
788  | 3.72k  |   cstate.ic.cur_mark = 0;  | 
789  | 3.72k  |   cstate.bpf_pcap = p;  | 
790  | 3.72k  |   cstate.error_set = 0;  | 
791  | 3.72k  |   init_regs(&cstate);  | 
792  |  |  | 
793  | 3.72k  |   cstate.netmask = mask;  | 
794  |  |  | 
795  | 3.72k  |   cstate.snaplen = pcap_snapshot(p);  | 
796  | 3.72k  |   if (cstate.snaplen == 0) { | 
797  | 0  |     snprintf(p->errbuf, PCAP_ERRBUF_SIZE,  | 
798  | 0  |        "snaplen of 0 rejects all packets");  | 
799  | 0  |     rc = PCAP_ERROR;  | 
800  | 0  |     goto quit;  | 
801  | 0  |   }  | 
802  |  |  | 
803  | 3.72k  |   if (pcap_lex_init(&scanner) != 0)  | 
804  | 0  |     pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,  | 
805  | 0  |         errno, "can't initialize scanner");  | 
806  | 3.72k  |   in_buffer = pcap__scan_string(xbuf ? xbuf : "", scanner);  | 
807  |  |  | 
808  |  |   /*  | 
809  |  |    * Associate the compiler state with the lexical analyzer  | 
810  |  |    * state.  | 
811  |  |    */  | 
812  | 3.72k  |   pcap_set_extra(&cstate, scanner);  | 
813  |  |  | 
814  | 3.72k  |   if (init_linktype(&cstate, p) == -1) { | 
815  | 27  |     rc = PCAP_ERROR;  | 
816  | 27  |     goto quit;  | 
817  | 27  |   }  | 
818  | 3.70k  |   if (pcap_parse(scanner, &cstate) != 0) { | 
819  | 786  | #ifdef INET6  | 
820  | 786  |     if (cstate.ai != NULL)  | 
821  | 0  |       freeaddrinfo(cstate.ai);  | 
822  | 786  | #endif  | 
823  | 786  |     if (cstate.e != NULL)  | 
824  | 0  |       free(cstate.e);  | 
825  | 786  |     rc = PCAP_ERROR;  | 
826  | 786  |     goto quit;  | 
827  | 786  |   }  | 
828  |  |  | 
829  | 2.91k  |   if (cstate.ic.root == NULL) { | 
830  |  |     /*  | 
831  |  |      * Catch errors reported by gen_retblk().  | 
832  |  |      */  | 
833  | 930  |     if (setjmp(cstate.top_ctx)) { | 
834  | 0  |       rc = PCAP_ERROR;  | 
835  | 0  |       goto quit;  | 
836  | 0  |     }  | 
837  | 930  |     cstate.ic.root = gen_retblk(&cstate, cstate.snaplen);  | 
838  | 930  |   }  | 
839  |  |  | 
840  | 2.91k  |   if (optimize && !cstate.no_optimize) { | 
841  | 2.43k  |     if (bpf_optimize(&cstate.ic, p->errbuf) == -1) { | 
842  |  |       /* Failure */  | 
843  | 14  |       rc = PCAP_ERROR;  | 
844  | 14  |       goto quit;  | 
845  | 14  |     }  | 
846  | 2.42k  |     if (cstate.ic.root == NULL ||  | 
847  | 2.42k  |         (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) { | 
848  | 340  |       (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,  | 
849  | 340  |           "expression rejects all packets");  | 
850  | 340  |       rc = PCAP_ERROR;  | 
851  | 340  |       goto quit;  | 
852  | 340  |     }  | 
853  | 2.42k  |   }  | 
854  | 2.56k  |   program->bf_insns = icode_to_fcode(&cstate.ic,  | 
855  | 2.56k  |       cstate.ic.root, &len, p->errbuf);  | 
856  | 2.56k  |   if (program->bf_insns == NULL) { | 
857  |  |     /* Failure */  | 
858  | 0  |     rc = PCAP_ERROR;  | 
859  | 0  |     goto quit;  | 
860  | 0  |   }  | 
861  | 2.56k  |   program->bf_len = len;  | 
862  |  |  | 
863  | 2.56k  |   rc = 0;  /* We're all okay */  | 
864  |  |  | 
865  | 3.72k  | quit:  | 
866  |  |   /*  | 
867  |  |    * Clean up everything for the lexical analyzer.  | 
868  |  |    */  | 
869  | 3.72k  |   if (in_buffer != NULL)  | 
870  | 3.72k  |     pcap__delete_buffer(in_buffer, scanner);  | 
871  | 3.72k  |   if (scanner != NULL)  | 
872  | 3.72k  |     pcap_lex_destroy(scanner);  | 
873  |  |  | 
874  |  |   /*  | 
875  |  |    * Clean up our own allocated memory.  | 
876  |  |    */  | 
877  | 3.72k  |   freechunks(&cstate);  | 
878  |  |  | 
879  | 3.72k  |   return (rc);  | 
880  | 2.56k  | }  | 
881  |  |  | 
882  |  | /*  | 
883  |  |  * entry point for using the compiler with no pcap open  | 
884  |  |  * pass in all the stuff that is needed explicitly instead.  | 
885  |  |  */  | 
886  |  | int  | 
887  |  | pcap_compile_nopcap(int snaplen_arg, int linktype_arg,  | 
888  |  |         struct bpf_program *program,  | 
889  |  |        const char *buf, int optimize, bpf_u_int32 mask)  | 
890  | 0  | { | 
891  | 0  |   pcap_t *p;  | 
892  | 0  |   int ret;  | 
893  |  | 
  | 
894  | 0  |   p = pcap_open_dead(linktype_arg, snaplen_arg);  | 
895  | 0  |   if (p == NULL)  | 
896  | 0  |     return (PCAP_ERROR);  | 
897  | 0  |   ret = pcap_compile(p, program, buf, optimize, mask);  | 
898  | 0  |   pcap_close(p);  | 
899  | 0  |   return (ret);  | 
900  | 0  | }  | 
901  |  |  | 
902  |  | /*  | 
903  |  |  * Clean up a "struct bpf_program" by freeing all the memory allocated  | 
904  |  |  * in it.  | 
905  |  |  */  | 
906  |  | void  | 
907  |  | pcap_freecode(struct bpf_program *program)  | 
908  | 6.28k  | { | 
909  | 6.28k  |   program->bf_len = 0;  | 
910  | 6.28k  |   if (program->bf_insns != NULL) { | 
911  | 2.56k  |     free((char *)program->bf_insns);  | 
912  | 2.56k  |     program->bf_insns = NULL;  | 
913  | 2.56k  |   }  | 
914  | 6.28k  | }  | 
915  |  |  | 
916  |  | /*  | 
917  |  |  * Backpatch the blocks in 'list' to 'target'.  The 'sense' field indicates  | 
918  |  |  * which of the jt and jf fields has been resolved and which is a pointer  | 
919  |  |  * back to another unresolved block (or nil).  At least one of the fields  | 
920  |  |  * in each block is already resolved.  | 
921  |  |  */  | 
922  |  | static void  | 
923  |  | backpatch(struct block *list, struct block *target)  | 
924  | 11.5k  | { | 
925  | 11.5k  |   struct block *next;  | 
926  |  |  | 
927  | 30.5k  |   while (list) { | 
928  | 19.0k  |     if (!list->sense) { | 
929  | 9.51k  |       next = JT(list);  | 
930  | 9.51k  |       JT(list) = target;  | 
931  | 9.51k  |     } else { | 
932  | 9.51k  |       next = JF(list);  | 
933  | 9.51k  |       JF(list) = target;  | 
934  | 9.51k  |     }  | 
935  | 19.0k  |     list = next;  | 
936  | 19.0k  |   }  | 
937  | 11.5k  | }  | 
938  |  |  | 
939  |  | /*  | 
940  |  |  * Merge the lists in b0 and b1, using the 'sense' field to indicate  | 
941  |  |  * which of jt and jf is the link.  | 
942  |  |  */  | 
943  |  | static void  | 
944  |  | merge(struct block *b0, struct block *b1)  | 
945  | 7.48k  | { | 
946  | 7.48k  |   register struct block **p = &b0;  | 
947  |  |  | 
948  |  |   /* Find end of list. */  | 
949  | 16.6k  |   while (*p)  | 
950  | 9.20k  |     p = !((*p)->sense) ? &JT(*p) : &JF(*p);  | 
951  |  |  | 
952  |  |   /* Concatenate the lists. */  | 
953  | 7.48k  |   *p = b1;  | 
954  | 7.48k  | }  | 
955  |  |  | 
956  |  | int  | 
957  |  | finish_parse(compiler_state_t *cstate, struct block *p)  | 
958  | 2.03k  | { | 
959  | 2.03k  |   struct block *ppi_dlt_check;  | 
960  |  |  | 
961  |  |   /*  | 
962  |  |    * Catch errors reported by us and routines below us, and return -1  | 
963  |  |    * on an error.  | 
964  |  |    */  | 
965  | 2.03k  |   if (setjmp(cstate->top_ctx))  | 
966  | 0  |     return (-1);  | 
967  |  |  | 
968  |  |   /*  | 
969  |  |    * Insert before the statements of the first (root) block any  | 
970  |  |    * statements needed to load the lengths of any variable-length  | 
971  |  |    * headers into registers.  | 
972  |  |    *  | 
973  |  |    * XXX - a fancier strategy would be to insert those before the  | 
974  |  |    * statements of all blocks that use those lengths and that  | 
975  |  |    * have no predecessors that use them, so that we only compute  | 
976  |  |    * the lengths if we need them.  There might be even better  | 
977  |  |    * approaches than that.  | 
978  |  |    *  | 
979  |  |    * However, those strategies would be more complicated, and  | 
980  |  |    * as we don't generate code to compute a length if the  | 
981  |  |    * program has no tests that use the length, and as most  | 
982  |  |    * tests will probably use those lengths, we would just  | 
983  |  |    * postpone computing the lengths so that it's not done  | 
984  |  |    * for tests that fail early, and it's not clear that's  | 
985  |  |    * worth the effort.  | 
986  |  |    */  | 
987  | 2.03k  |   insert_compute_vloffsets(cstate, p->head);  | 
988  |  |  | 
989  |  |   /*  | 
990  |  |    * For DLT_PPI captures, generate a check of the per-packet  | 
991  |  |    * DLT value to make sure it's DLT_IEEE802_11.  | 
992  |  |    *  | 
993  |  |    * XXX - TurboCap cards use DLT_PPI for Ethernet.  | 
994  |  |    * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header  | 
995  |  |    * with appropriate Ethernet information and use that rather  | 
996  |  |    * than using something such as DLT_PPI where you don't know  | 
997  |  |    * the link-layer header type until runtime, which, in the  | 
998  |  |    * general case, would force us to generate both Ethernet *and*  | 
999  |  |    * 802.11 code (*and* anything else for which PPI is used)  | 
1000  |  |    * and choose between them early in the BPF program?  | 
1001  |  |    */  | 
1002  | 2.03k  |   ppi_dlt_check = gen_ppi_dlt_check(cstate);  | 
1003  | 2.03k  |   if (ppi_dlt_check != NULL)  | 
1004  | 184  |     gen_and(ppi_dlt_check, p);  | 
1005  |  |  | 
1006  | 2.03k  |   backpatch(p, gen_retblk(cstate, cstate->snaplen));  | 
1007  | 2.03k  |   p->sense = !p->sense;  | 
1008  | 2.03k  |   backpatch(p, gen_retblk(cstate, 0));  | 
1009  | 2.03k  |   cstate->ic.root = p->head;  | 
1010  | 2.03k  |   return (0);  | 
1011  | 2.03k  | }  | 
1012  |  |  | 
1013  |  | void  | 
1014  |  | gen_and(struct block *b0, struct block *b1)  | 
1015  | 5.73k  | { | 
1016  | 5.73k  |   backpatch(b0, b1->head);  | 
1017  | 5.73k  |   b0->sense = !b0->sense;  | 
1018  | 5.73k  |   b1->sense = !b1->sense;  | 
1019  | 5.73k  |   merge(b1, b0);  | 
1020  | 5.73k  |   b1->sense = !b1->sense;  | 
1021  | 5.73k  |   b1->head = b0->head;  | 
1022  | 5.73k  | }  | 
1023  |  |  | 
1024  |  | void  | 
1025  |  | gen_or(struct block *b0, struct block *b1)  | 
1026  | 1.75k  | { | 
1027  | 1.75k  |   b0->sense = !b0->sense;  | 
1028  | 1.75k  |   backpatch(b0, b1->head);  | 
1029  | 1.75k  |   b0->sense = !b0->sense;  | 
1030  | 1.75k  |   merge(b1, b0);  | 
1031  | 1.75k  |   b1->head = b0->head;  | 
1032  | 1.75k  | }  | 
1033  |  |  | 
1034  |  | void  | 
1035  |  | gen_not(struct block *b)  | 
1036  | 978  | { | 
1037  | 978  |   b->sense = !b->sense;  | 
1038  | 978  | }  | 
1039  |  |  | 
1040  |  | static struct block *  | 
1041  |  | gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1042  |  |     u_int size, bpf_u_int32 v)  | 
1043  | 6.35k  | { | 
1044  | 6.35k  |   return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);  | 
1045  | 6.35k  | }  | 
1046  |  |  | 
1047  |  | static struct block *  | 
1048  |  | gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1049  |  |     u_int size, bpf_u_int32 v)  | 
1050  | 154  | { | 
1051  | 154  |   return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);  | 
1052  | 154  | }  | 
1053  |  |  | 
1054  |  | static struct block *  | 
1055  |  | gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1056  |  |     u_int size, bpf_u_int32 v)  | 
1057  | 0  | { | 
1058  | 0  |   return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);  | 
1059  | 0  | }  | 
1060  |  |  | 
1061  |  | static struct block *  | 
1062  |  | gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1063  |  |     u_int size, bpf_u_int32 v)  | 
1064  | 0  | { | 
1065  | 0  |   return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);  | 
1066  | 0  | }  | 
1067  |  |  | 
1068  |  | static struct block *  | 
1069  |  | gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1070  |  |     u_int size, bpf_u_int32 v)  | 
1071  | 0  | { | 
1072  | 0  |   return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);  | 
1073  | 0  | }  | 
1074  |  |  | 
1075  |  | static struct block *  | 
1076  |  | gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1077  |  |     u_int size, bpf_u_int32 v, bpf_u_int32 mask)  | 
1078  | 736  | { | 
1079  | 736  |   return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);  | 
1080  | 736  | }  | 
1081  |  |  | 
1082  |  | static struct block *  | 
1083  |  | gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1084  |  |     u_int size, const u_char *v)  | 
1085  | 0  | { | 
1086  | 0  |   register struct block *b, *tmp;  | 
1087  |  | 
  | 
1088  | 0  |   b = NULL;  | 
1089  | 0  |   while (size >= 4) { | 
1090  | 0  |     register const u_char *p = &v[size - 4];  | 
1091  |  | 
  | 
1092  | 0  |     tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W,  | 
1093  | 0  |         EXTRACT_BE_U_4(p));  | 
1094  | 0  |     if (b != NULL)  | 
1095  | 0  |       gen_and(b, tmp);  | 
1096  | 0  |     b = tmp;  | 
1097  | 0  |     size -= 4;  | 
1098  | 0  |   }  | 
1099  | 0  |   while (size >= 2) { | 
1100  | 0  |     register const u_char *p = &v[size - 2];  | 
1101  |  | 
  | 
1102  | 0  |     tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H,  | 
1103  | 0  |         EXTRACT_BE_U_2(p));  | 
1104  | 0  |     if (b != NULL)  | 
1105  | 0  |       gen_and(b, tmp);  | 
1106  | 0  |     b = tmp;  | 
1107  | 0  |     size -= 2;  | 
1108  | 0  |   }  | 
1109  | 0  |   if (size > 0) { | 
1110  | 0  |     tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]);  | 
1111  | 0  |     if (b != NULL)  | 
1112  | 0  |       gen_and(b, tmp);  | 
1113  | 0  |     b = tmp;  | 
1114  | 0  |   }  | 
1115  | 0  |   return b;  | 
1116  | 0  | }  | 
1117  |  |  | 
1118  |  | /*  | 
1119  |  |  * AND the field of size "size" at offset "offset" relative to the header  | 
1120  |  |  * specified by "offrel" with "mask", and compare it with the value "v"  | 
1121  |  |  * with the test specified by "jtype"; if "reverse" is true, the test  | 
1122  |  |  * should test the opposite of "jtype".  | 
1123  |  |  */  | 
1124  |  | static struct block *  | 
1125  |  | gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1126  |  |     u_int size, bpf_u_int32 mask, int jtype, int reverse,  | 
1127  |  |     bpf_u_int32 v)  | 
1128  | 7.32k  | { | 
1129  | 7.32k  |   struct slist *s, *s2;  | 
1130  | 7.32k  |   struct block *b;  | 
1131  |  |  | 
1132  | 7.32k  |   s = gen_load_a(cstate, offrel, offset, size);  | 
1133  |  |  | 
1134  | 7.32k  |   if (mask != 0xffffffff) { | 
1135  | 819  |     s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
1136  | 819  |     s2->s.k = mask;  | 
1137  | 819  |     sappend(s, s2);  | 
1138  | 819  |   }  | 
1139  |  |  | 
1140  | 7.32k  |   b = new_block(cstate, JMP(jtype));  | 
1141  | 7.32k  |   b->stmts = s;  | 
1142  | 7.32k  |   b->s.k = v;  | 
1143  | 7.32k  |   if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))  | 
1144  | 6  |     gen_not(b);  | 
1145  | 7.32k  |   return b;  | 
1146  | 7.32k  | }  | 
1147  |  |  | 
1148  |  | static int  | 
1149  |  | init_linktype(compiler_state_t *cstate, pcap_t *p)  | 
1150  | 3.72k  | { | 
1151  | 3.72k  |   cstate->pcap_fddipad = p->fddipad;  | 
1152  |  |  | 
1153  |  |   /*  | 
1154  |  |    * We start out with only one link-layer header.  | 
1155  |  |    */  | 
1156  | 3.72k  |   cstate->outermostlinktype = pcap_datalink(p);  | 
1157  | 3.72k  |   cstate->off_outermostlinkhdr.constant_part = 0;  | 
1158  | 3.72k  |   cstate->off_outermostlinkhdr.is_variable = 0;  | 
1159  | 3.72k  |   cstate->off_outermostlinkhdr.reg = -1;  | 
1160  |  |  | 
1161  | 3.72k  |   cstate->prevlinktype = cstate->outermostlinktype;  | 
1162  | 3.72k  |   cstate->off_prevlinkhdr.constant_part = 0;  | 
1163  | 3.72k  |   cstate->off_prevlinkhdr.is_variable = 0;  | 
1164  | 3.72k  |   cstate->off_prevlinkhdr.reg = -1;  | 
1165  |  |  | 
1166  | 3.72k  |   cstate->linktype = cstate->outermostlinktype;  | 
1167  | 3.72k  |   cstate->off_linkhdr.constant_part = 0;  | 
1168  | 3.72k  |   cstate->off_linkhdr.is_variable = 0;  | 
1169  | 3.72k  |   cstate->off_linkhdr.reg = -1;  | 
1170  |  |  | 
1171  |  |   /*  | 
1172  |  |    * XXX  | 
1173  |  |    */  | 
1174  | 3.72k  |   cstate->off_linkpl.constant_part = 0;  | 
1175  | 3.72k  |   cstate->off_linkpl.is_variable = 0;  | 
1176  | 3.72k  |   cstate->off_linkpl.reg = -1;  | 
1177  |  |  | 
1178  | 3.72k  |   cstate->off_linktype.constant_part = 0;  | 
1179  | 3.72k  |   cstate->off_linktype.is_variable = 0;  | 
1180  | 3.72k  |   cstate->off_linktype.reg = -1;  | 
1181  |  |  | 
1182  |  |   /*  | 
1183  |  |    * Assume it's not raw ATM with a pseudo-header, for now.  | 
1184  |  |    */  | 
1185  | 3.72k  |   cstate->is_atm = 0;  | 
1186  | 3.72k  |   cstate->off_vpi = OFFSET_NOT_SET;  | 
1187  | 3.72k  |   cstate->off_vci = OFFSET_NOT_SET;  | 
1188  | 3.72k  |   cstate->off_proto = OFFSET_NOT_SET;  | 
1189  | 3.72k  |   cstate->off_payload = OFFSET_NOT_SET;  | 
1190  |  |  | 
1191  |  |   /*  | 
1192  |  |    * And not Geneve.  | 
1193  |  |    */  | 
1194  | 3.72k  |   cstate->is_geneve = 0;  | 
1195  |  |  | 
1196  |  |   /*  | 
1197  |  |    * No variable length VLAN offset by default  | 
1198  |  |    */  | 
1199  | 3.72k  |   cstate->is_vlan_vloffset = 0;  | 
1200  |  |  | 
1201  |  |   /*  | 
1202  |  |    * And assume we're not doing SS7.  | 
1203  |  |    */  | 
1204  | 3.72k  |   cstate->off_li = OFFSET_NOT_SET;  | 
1205  | 3.72k  |   cstate->off_li_hsl = OFFSET_NOT_SET;  | 
1206  | 3.72k  |   cstate->off_sio = OFFSET_NOT_SET;  | 
1207  | 3.72k  |   cstate->off_opc = OFFSET_NOT_SET;  | 
1208  | 3.72k  |   cstate->off_dpc = OFFSET_NOT_SET;  | 
1209  | 3.72k  |   cstate->off_sls = OFFSET_NOT_SET;  | 
1210  |  |  | 
1211  | 3.72k  |   cstate->label_stack_depth = 0;  | 
1212  | 3.72k  |   cstate->vlan_stack_depth = 0;  | 
1213  |  |  | 
1214  | 3.72k  |   switch (cstate->linktype) { | 
1215  |  |  | 
1216  | 20  |   case DLT_ARCNET:  | 
1217  | 20  |     cstate->off_linktype.constant_part = 2;  | 
1218  | 20  |     cstate->off_linkpl.constant_part = 6;  | 
1219  | 20  |     cstate->off_nl = 0;   /* XXX in reality, variable! */  | 
1220  | 20  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1221  | 20  |     break;  | 
1222  |  |  | 
1223  | 24  |   case DLT_ARCNET_LINUX:  | 
1224  | 24  |     cstate->off_linktype.constant_part = 4;  | 
1225  | 24  |     cstate->off_linkpl.constant_part = 8;  | 
1226  | 24  |     cstate->off_nl = 0;   /* XXX in reality, variable! */  | 
1227  | 24  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1228  | 24  |     break;  | 
1229  |  |  | 
1230  | 87  |   case DLT_EN10MB:  | 
1231  | 87  |     cstate->off_linktype.constant_part = 12;  | 
1232  | 87  |     cstate->off_linkpl.constant_part = 14;  /* Ethernet header length */  | 
1233  | 87  |     cstate->off_nl = 0;   /* Ethernet II */  | 
1234  | 87  |     cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */  | 
1235  | 87  |     break;  | 
1236  |  |  | 
1237  | 18  |   case DLT_SLIP:  | 
1238  |  |     /*  | 
1239  |  |      * SLIP doesn't have a link level type.  The 16 byte  | 
1240  |  |      * header is hacked into our SLIP driver.  | 
1241  |  |      */  | 
1242  | 18  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1243  | 18  |     cstate->off_linkpl.constant_part = 16;  | 
1244  | 18  |     cstate->off_nl = 0;  | 
1245  | 18  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1246  | 18  |     break;  | 
1247  |  |  | 
1248  | 24  |   case DLT_SLIP_BSDOS:  | 
1249  |  |     /* XXX this may be the same as the DLT_PPP_BSDOS case */  | 
1250  | 24  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1251  |  |     /* XXX end */  | 
1252  | 24  |     cstate->off_linkpl.constant_part = 24;  | 
1253  | 24  |     cstate->off_nl = 0;  | 
1254  | 24  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1255  | 24  |     break;  | 
1256  |  |  | 
1257  | 412  |   case DLT_NULL:  | 
1258  | 433  |   case DLT_LOOP:  | 
1259  | 433  |     cstate->off_linktype.constant_part = 0;  | 
1260  | 433  |     cstate->off_linkpl.constant_part = 4;  | 
1261  | 433  |     cstate->off_nl = 0;  | 
1262  | 433  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1263  | 433  |     break;  | 
1264  |  |  | 
1265  | 38  |   case DLT_ENC:  | 
1266  | 38  |     cstate->off_linktype.constant_part = 0;  | 
1267  | 38  |     cstate->off_linkpl.constant_part = 12;  | 
1268  | 38  |     cstate->off_nl = 0;  | 
1269  | 38  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1270  | 38  |     break;  | 
1271  |  |  | 
1272  | 22  |   case DLT_PPP:  | 
1273  | 38  |   case DLT_PPP_PPPD:  | 
1274  | 63  |   case DLT_C_HDLC:   /* BSD/OS Cisco HDLC */  | 
1275  | 86  |   case DLT_HDLC:     /* NetBSD (Cisco) HDLC */  | 
1276  | 105  |   case DLT_PPP_SERIAL:   /* NetBSD sync/async serial PPP */  | 
1277  | 105  |     cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */  | 
1278  | 105  |     cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */  | 
1279  | 105  |     cstate->off_nl = 0;  | 
1280  | 105  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1281  | 105  |     break;  | 
1282  |  |  | 
1283  | 18  |   case DLT_PPP_ETHER:  | 
1284  |  |     /*  | 
1285  |  |      * This does no include the Ethernet header, and  | 
1286  |  |      * only covers session state.  | 
1287  |  |      */  | 
1288  | 18  |     cstate->off_linktype.constant_part = 6;  | 
1289  | 18  |     cstate->off_linkpl.constant_part = 8;  | 
1290  | 18  |     cstate->off_nl = 0;  | 
1291  | 18  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1292  | 18  |     break;  | 
1293  |  |  | 
1294  | 40  |   case DLT_PPP_BSDOS:  | 
1295  | 40  |     cstate->off_linktype.constant_part = 5;  | 
1296  | 40  |     cstate->off_linkpl.constant_part = 24;  | 
1297  | 40  |     cstate->off_nl = 0;  | 
1298  | 40  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1299  | 40  |     break;  | 
1300  |  |  | 
1301  | 25  |   case DLT_FDDI:  | 
1302  |  |     /*  | 
1303  |  |      * FDDI doesn't really have a link-level type field.  | 
1304  |  |      * We set "off_linktype" to the offset of the LLC header.  | 
1305  |  |      *  | 
1306  |  |      * To check for Ethernet types, we assume that SSAP = SNAP  | 
1307  |  |      * is being used and pick out the encapsulated Ethernet type.  | 
1308  |  |      * XXX - should we generate code to check for SNAP?  | 
1309  |  |      */  | 
1310  | 25  |     cstate->off_linktype.constant_part = 13;  | 
1311  | 25  |     cstate->off_linktype.constant_part += cstate->pcap_fddipad;  | 
1312  | 25  |     cstate->off_linkpl.constant_part = 13;  /* FDDI MAC header length */  | 
1313  | 25  |     cstate->off_linkpl.constant_part += cstate->pcap_fddipad;  | 
1314  | 25  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1315  | 25  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1316  | 25  |     break;  | 
1317  |  |  | 
1318  | 34  |   case DLT_IEEE802:  | 
1319  |  |     /*  | 
1320  |  |      * Token Ring doesn't really have a link-level type field.  | 
1321  |  |      * We set "off_linktype" to the offset of the LLC header.  | 
1322  |  |      *  | 
1323  |  |      * To check for Ethernet types, we assume that SSAP = SNAP  | 
1324  |  |      * is being used and pick out the encapsulated Ethernet type.  | 
1325  |  |      * XXX - should we generate code to check for SNAP?  | 
1326  |  |      *  | 
1327  |  |      * XXX - the header is actually variable-length.  | 
1328  |  |      * Some various Linux patched versions gave 38  | 
1329  |  |      * as "off_linktype" and 40 as "off_nl"; however,  | 
1330  |  |      * if a token ring packet has *no* routing  | 
1331  |  |      * information, i.e. is not source-routed, the correct  | 
1332  |  |      * values are 20 and 22, as they are in the vanilla code.  | 
1333  |  |      *  | 
1334  |  |      * A packet is source-routed iff the uppermost bit  | 
1335  |  |      * of the first byte of the source address, at an  | 
1336  |  |      * offset of 8, has the uppermost bit set.  If the  | 
1337  |  |      * packet is source-routed, the total number of bytes  | 
1338  |  |      * of routing information is 2 plus bits 0x1F00 of  | 
1339  |  |      * the 16-bit value at an offset of 14 (shifted right  | 
1340  |  |      * 8 - figure out which byte that is).  | 
1341  |  |      */  | 
1342  | 34  |     cstate->off_linktype.constant_part = 14;  | 
1343  | 34  |     cstate->off_linkpl.constant_part = 14;  /* Token Ring MAC header length */  | 
1344  | 34  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1345  | 34  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1346  | 34  |     break;  | 
1347  |  |  | 
1348  | 289  |   case DLT_PRISM_HEADER:  | 
1349  | 315  |   case DLT_IEEE802_11_RADIO_AVS:  | 
1350  | 431  |   case DLT_IEEE802_11_RADIO:  | 
1351  | 431  |     cstate->off_linkhdr.is_variable = 1;  | 
1352  |  |     /* Fall through, 802.11 doesn't have a variable link  | 
1353  |  |      * prefix but is otherwise the same. */  | 
1354  |  |     /* FALLTHROUGH */  | 
1355  |  |  | 
1356  | 695  |   case DLT_IEEE802_11:  | 
1357  |  |     /*  | 
1358  |  |      * 802.11 doesn't really have a link-level type field.  | 
1359  |  |      * We set "off_linktype.constant_part" to the offset of  | 
1360  |  |      * the LLC header.  | 
1361  |  |      *  | 
1362  |  |      * To check for Ethernet types, we assume that SSAP = SNAP  | 
1363  |  |      * is being used and pick out the encapsulated Ethernet type.  | 
1364  |  |      * XXX - should we generate code to check for SNAP?  | 
1365  |  |      *  | 
1366  |  |      * We also handle variable-length radio headers here.  | 
1367  |  |      * The Prism header is in theory variable-length, but in  | 
1368  |  |      * practice it's always 144 bytes long.  However, some  | 
1369  |  |      * drivers on Linux use ARPHRD_IEEE80211_PRISM, but  | 
1370  |  |      * sometimes or always supply an AVS header, so we  | 
1371  |  |      * have to check whether the radio header is a Prism  | 
1372  |  |      * header or an AVS header, so, in practice, it's  | 
1373  |  |      * variable-length.  | 
1374  |  |      */  | 
1375  | 695  |     cstate->off_linktype.constant_part = 24;  | 
1376  | 695  |     cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */  | 
1377  | 695  |     cstate->off_linkpl.is_variable = 1;  | 
1378  | 695  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1379  | 695  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1380  | 695  |     break;  | 
1381  |  |  | 
1382  | 212  |   case DLT_PPI:  | 
1383  |  |     /*  | 
1384  |  |      * At the moment we treat PPI the same way that we treat  | 
1385  |  |      * normal Radiotap encoded packets. The difference is in  | 
1386  |  |      * the function that generates the code at the beginning  | 
1387  |  |      * to compute the header length.  Since this code generator  | 
1388  |  |      * of PPI supports bare 802.11 encapsulation only (i.e.  | 
1389  |  |      * the encapsulated DLT should be DLT_IEEE802_11) we  | 
1390  |  |      * generate code to check for this too.  | 
1391  |  |      */  | 
1392  | 212  |     cstate->off_linktype.constant_part = 24;  | 
1393  | 212  |     cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */  | 
1394  | 212  |     cstate->off_linkpl.is_variable = 1;  | 
1395  | 212  |     cstate->off_linkhdr.is_variable = 1;  | 
1396  | 212  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1397  | 212  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1398  | 212  |     break;  | 
1399  |  |  | 
1400  | 39  |   case DLT_ATM_RFC1483:  | 
1401  | 58  |   case DLT_ATM_CLIP: /* Linux ATM defines this */  | 
1402  |  |     /*  | 
1403  |  |      * assume routed, non-ISO PDUs  | 
1404  |  |      * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)  | 
1405  |  |      *  | 
1406  |  |      * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,  | 
1407  |  |      * or PPP with the PPP NLPID (e.g., PPPoA)?  The  | 
1408  |  |      * latter would presumably be treated the way PPPoE  | 
1409  |  |      * should be, so you can do "pppoe and udp port 2049"  | 
1410  |  |      * or "pppoa and tcp port 80" and have it check for  | 
1411  |  |      * PPPo{A,E} and a PPP protocol of IP and.... | 
1412  |  |      */  | 
1413  | 58  |     cstate->off_linktype.constant_part = 0;  | 
1414  | 58  |     cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */  | 
1415  | 58  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1416  | 58  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1417  | 58  |     break;  | 
1418  |  |  | 
1419  | 45  |   case DLT_SUNATM:  | 
1420  |  |     /*  | 
1421  |  |      * Full Frontal ATM; you get AALn PDUs with an ATM  | 
1422  |  |      * pseudo-header.  | 
1423  |  |      */  | 
1424  | 45  |     cstate->is_atm = 1;  | 
1425  | 45  |     cstate->off_vpi = SUNATM_VPI_POS;  | 
1426  | 45  |     cstate->off_vci = SUNATM_VCI_POS;  | 
1427  | 45  |     cstate->off_proto = PROTO_POS;  | 
1428  | 45  |     cstate->off_payload = SUNATM_PKT_BEGIN_POS;  | 
1429  | 45  |     cstate->off_linktype.constant_part = cstate->off_payload;  | 
1430  | 45  |     cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */  | 
1431  | 45  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1432  | 45  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1433  | 45  |     break;  | 
1434  |  |  | 
1435  | 25  |   case DLT_RAW:  | 
1436  | 54  |   case DLT_IPV4:  | 
1437  | 75  |   case DLT_IPV6:  | 
1438  | 75  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1439  | 75  |     cstate->off_linkpl.constant_part = 0;  | 
1440  | 75  |     cstate->off_nl = 0;  | 
1441  | 75  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1442  | 75  |     break;  | 
1443  |  |  | 
1444  | 202  |   case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */  | 
1445  | 202  |     cstate->off_linktype.constant_part = 14;  | 
1446  | 202  |     cstate->off_linkpl.constant_part = 16;  | 
1447  | 202  |     cstate->off_nl = 0;  | 
1448  | 202  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1449  | 202  |     break;  | 
1450  |  |  | 
1451  | 46  |   case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */  | 
1452  | 46  |     cstate->off_linktype.constant_part = 0;  | 
1453  | 46  |     cstate->off_linkpl.constant_part = 20;  | 
1454  | 46  |     cstate->off_nl = 0;  | 
1455  | 46  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1456  | 46  |     break;  | 
1457  |  |  | 
1458  | 20  |   case DLT_LTALK:  | 
1459  |  |     /*  | 
1460  |  |      * LocalTalk does have a 1-byte type field in the LLAP header,  | 
1461  |  |      * but really it just indicates whether there is a "short" or  | 
1462  |  |      * "long" DDP packet following.  | 
1463  |  |      */  | 
1464  | 20  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1465  | 20  |     cstate->off_linkpl.constant_part = 0;  | 
1466  | 20  |     cstate->off_nl = 0;  | 
1467  | 20  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1468  | 20  |     break;  | 
1469  |  |  | 
1470  | 23  |   case DLT_IP_OVER_FC:  | 
1471  |  |     /*  | 
1472  |  |      * RFC 2625 IP-over-Fibre-Channel doesn't really have a  | 
1473  |  |      * link-level type field.  We set "off_linktype" to the  | 
1474  |  |      * offset of the LLC header.  | 
1475  |  |      *  | 
1476  |  |      * To check for Ethernet types, we assume that SSAP = SNAP  | 
1477  |  |      * is being used and pick out the encapsulated Ethernet type.  | 
1478  |  |      * XXX - should we generate code to check for SNAP? RFC  | 
1479  |  |      * 2625 says SNAP should be used.  | 
1480  |  |      */  | 
1481  | 23  |     cstate->off_linktype.constant_part = 16;  | 
1482  | 23  |     cstate->off_linkpl.constant_part = 16;  | 
1483  | 23  |     cstate->off_nl = 8;   /* 802.2+SNAP */  | 
1484  | 23  |     cstate->off_nl_nosnap = 3;  /* 802.2 */  | 
1485  | 23  |     break;  | 
1486  |  |  | 
1487  | 34  |   case DLT_FRELAY:  | 
1488  |  |     /*  | 
1489  |  |      * XXX - we should set this to handle SNAP-encapsulated  | 
1490  |  |      * frames (NLPID of 0x80).  | 
1491  |  |      */  | 
1492  | 34  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1493  | 34  |     cstate->off_linkpl.constant_part = 0;  | 
1494  | 34  |     cstate->off_nl = 0;  | 
1495  | 34  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1496  | 34  |     break;  | 
1497  |  |  | 
1498  |  |                 /*  | 
1499  |  |                  * the only BPF-interesting FRF.16 frames are non-control frames;  | 
1500  |  |                  * Frame Relay has a variable length link-layer  | 
1501  |  |                  * so lets start with offset 4 for now and increments later on (FIXME);  | 
1502  |  |                  */  | 
1503  | 8  |   case DLT_MFR:  | 
1504  | 8  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1505  | 8  |     cstate->off_linkpl.constant_part = 0;  | 
1506  | 8  |     cstate->off_nl = 4;  | 
1507  | 8  |     cstate->off_nl_nosnap = 0;  /* XXX - for now -> no 802.2 LLC */  | 
1508  | 8  |     break;  | 
1509  |  |  | 
1510  | 9  |   case DLT_APPLE_IP_OVER_IEEE1394:  | 
1511  | 9  |     cstate->off_linktype.constant_part = 16;  | 
1512  | 9  |     cstate->off_linkpl.constant_part = 18;  | 
1513  | 9  |     cstate->off_nl = 0;  | 
1514  | 9  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1515  | 9  |     break;  | 
1516  |  |  | 
1517  | 68  |   case DLT_SYMANTEC_FIREWALL:  | 
1518  | 68  |     cstate->off_linktype.constant_part = 6;  | 
1519  | 68  |     cstate->off_linkpl.constant_part = 44;  | 
1520  | 68  |     cstate->off_nl = 0;   /* Ethernet II */  | 
1521  | 68  |     cstate->off_nl_nosnap = 0;  /* XXX - what does it do with 802.3 packets? */  | 
1522  | 68  |     break;  | 
1523  |  |  | 
1524  | 124  |   case DLT_PFLOG:  | 
1525  | 124  |     cstate->off_linktype.constant_part = 0;  | 
1526  | 124  |     cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */  | 
1527  | 124  |     cstate->off_linkpl.is_variable = 1;  | 
1528  | 124  |     cstate->off_nl = 0;  | 
1529  | 124  |     cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
1530  | 124  |     break;  | 
1531  |  |  | 
1532  | 15  |         case DLT_JUNIPER_MFR:  | 
1533  | 36  |         case DLT_JUNIPER_MLFR:  | 
1534  | 56  |         case DLT_JUNIPER_MLPPP:  | 
1535  | 73  |         case DLT_JUNIPER_PPP:  | 
1536  | 89  |         case DLT_JUNIPER_CHDLC:  | 
1537  | 108  |         case DLT_JUNIPER_FRELAY:  | 
1538  | 108  |     cstate->off_linktype.constant_part = 4;  | 
1539  | 108  |     cstate->off_linkpl.constant_part = 4;  | 
1540  | 108  |     cstate->off_nl = 0;  | 
1541  | 108  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1542  | 108  |                 break;  | 
1543  |  |  | 
1544  | 17  |   case DLT_JUNIPER_ATM1:  | 
1545  | 17  |     cstate->off_linktype.constant_part = 4;   /* in reality variable between 4-8 */  | 
1546  | 17  |     cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */  | 
1547  | 17  |     cstate->off_nl = 0;  | 
1548  | 17  |     cstate->off_nl_nosnap = 10;  | 
1549  | 17  |     break;  | 
1550  |  |  | 
1551  | 19  |   case DLT_JUNIPER_ATM2:  | 
1552  | 19  |     cstate->off_linktype.constant_part = 8;   /* in reality variable between 8-12 */  | 
1553  | 19  |     cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */  | 
1554  | 19  |     cstate->off_nl = 0;  | 
1555  | 19  |     cstate->off_nl_nosnap = 10;  | 
1556  | 19  |     break;  | 
1557  |  |  | 
1558  |  |     /* frames captured on a Juniper PPPoE service PIC  | 
1559  |  |      * contain raw ethernet frames */  | 
1560  | 17  |   case DLT_JUNIPER_PPPOE:  | 
1561  | 36  |         case DLT_JUNIPER_ETHER:  | 
1562  | 36  |     cstate->off_linkpl.constant_part = 14;  | 
1563  | 36  |     cstate->off_linktype.constant_part = 16;  | 
1564  | 36  |     cstate->off_nl = 18;    /* Ethernet II */  | 
1565  | 36  |     cstate->off_nl_nosnap = 21; /* 802.3+802.2 */  | 
1566  | 36  |     break;  | 
1567  |  |  | 
1568  | 43  |   case DLT_JUNIPER_PPPOE_ATM:  | 
1569  | 43  |     cstate->off_linktype.constant_part = 4;  | 
1570  | 43  |     cstate->off_linkpl.constant_part = 6;  | 
1571  | 43  |     cstate->off_nl = 0;  | 
1572  | 43  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1573  | 43  |     break;  | 
1574  |  |  | 
1575  | 30  |   case DLT_JUNIPER_GGSN:  | 
1576  | 30  |     cstate->off_linktype.constant_part = 6;  | 
1577  | 30  |     cstate->off_linkpl.constant_part = 12;  | 
1578  | 30  |     cstate->off_nl = 0;  | 
1579  | 30  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1580  | 30  |     break;  | 
1581  |  |  | 
1582  | 19  |   case DLT_JUNIPER_ES:  | 
1583  | 19  |     cstate->off_linktype.constant_part = 6;  | 
1584  | 19  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */  | 
1585  | 19  |     cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */  | 
1586  | 19  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1587  | 19  |     break;  | 
1588  |  |  | 
1589  | 28  |   case DLT_JUNIPER_MONITOR:  | 
1590  | 28  |     cstate->off_linktype.constant_part = 12;  | 
1591  | 28  |     cstate->off_linkpl.constant_part = 12;  | 
1592  | 28  |     cstate->off_nl = 0;     /* raw IP/IP6 header */  | 
1593  | 28  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1594  | 28  |     break;  | 
1595  |  |  | 
1596  | 18  |   case DLT_BACNET_MS_TP:  | 
1597  | 18  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1598  | 18  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1599  | 18  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1600  | 18  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1601  | 18  |     break;  | 
1602  |  |  | 
1603  | 17  |   case DLT_JUNIPER_SERVICES:  | 
1604  | 17  |     cstate->off_linktype.constant_part = 12;  | 
1605  | 17  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */  | 
1606  | 17  |     cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */  | 
1607  | 17  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1608  | 17  |     break;  | 
1609  |  |  | 
1610  | 17  |   case DLT_JUNIPER_VP:  | 
1611  | 17  |     cstate->off_linktype.constant_part = 18;  | 
1612  | 17  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1613  | 17  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1614  | 17  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1615  | 17  |     break;  | 
1616  |  |  | 
1617  | 16  |   case DLT_JUNIPER_ST:  | 
1618  | 16  |     cstate->off_linktype.constant_part = 18;  | 
1619  | 16  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1620  | 16  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1621  | 16  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1622  | 16  |     break;  | 
1623  |  |  | 
1624  | 15  |   case DLT_JUNIPER_ISM:  | 
1625  | 15  |     cstate->off_linktype.constant_part = 8;  | 
1626  | 15  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1627  | 15  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1628  | 15  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1629  | 15  |     break;  | 
1630  |  |  | 
1631  | 17  |   case DLT_JUNIPER_VS:  | 
1632  | 37  |   case DLT_JUNIPER_SRX_E2E:  | 
1633  | 61  |   case DLT_JUNIPER_FIBRECHANNEL:  | 
1634  | 77  |   case DLT_JUNIPER_ATM_CEMIC:  | 
1635  | 77  |     cstate->off_linktype.constant_part = 8;  | 
1636  | 77  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1637  | 77  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1638  | 77  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1639  | 77  |     break;  | 
1640  |  |  | 
1641  | 15  |   case DLT_MTP2:  | 
1642  | 15  |     cstate->off_li = 2;  | 
1643  | 15  |     cstate->off_li_hsl = 4;  | 
1644  | 15  |     cstate->off_sio = 3;  | 
1645  | 15  |     cstate->off_opc = 4;  | 
1646  | 15  |     cstate->off_dpc = 4;  | 
1647  | 15  |     cstate->off_sls = 7;  | 
1648  | 15  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1649  | 15  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1650  | 15  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1651  | 15  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1652  | 15  |     break;  | 
1653  |  |  | 
1654  | 11  |   case DLT_MTP2_WITH_PHDR:  | 
1655  | 11  |     cstate->off_li = 6;  | 
1656  | 11  |     cstate->off_li_hsl = 8;  | 
1657  | 11  |     cstate->off_sio = 7;  | 
1658  | 11  |     cstate->off_opc = 8;  | 
1659  | 11  |     cstate->off_dpc = 8;  | 
1660  | 11  |     cstate->off_sls = 11;  | 
1661  | 11  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1662  | 11  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1663  | 11  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1664  | 11  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1665  | 11  |     break;  | 
1666  |  |  | 
1667  | 16  |   case DLT_ERF:  | 
1668  | 16  |     cstate->off_li = 22;  | 
1669  | 16  |     cstate->off_li_hsl = 24;  | 
1670  | 16  |     cstate->off_sio = 23;  | 
1671  | 16  |     cstate->off_opc = 24;  | 
1672  | 16  |     cstate->off_dpc = 24;  | 
1673  | 16  |     cstate->off_sls = 27;  | 
1674  | 16  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1675  | 16  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1676  | 16  |     cstate->off_nl = OFFSET_NOT_SET;  | 
1677  | 16  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1678  | 16  |     break;  | 
1679  |  |  | 
1680  | 9  |   case DLT_PFSYNC:  | 
1681  | 9  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1682  | 9  |     cstate->off_linkpl.constant_part = 4;  | 
1683  | 9  |     cstate->off_nl = 0;  | 
1684  | 9  |     cstate->off_nl_nosnap = 0;  | 
1685  | 9  |     break;  | 
1686  |  |  | 
1687  | 7  |   case DLT_AX25_KISS:  | 
1688  |  |     /*  | 
1689  |  |      * Currently, only raw "link[N:M]" filtering is supported.  | 
1690  |  |      */  | 
1691  | 7  |     cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */  | 
1692  | 7  |     cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1693  | 7  |     cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */  | 
1694  | 7  |     cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */  | 
1695  | 7  |     break;  | 
1696  |  |  | 
1697  | 20  |   case DLT_IPNET:  | 
1698  | 20  |     cstate->off_linktype.constant_part = 1;  | 
1699  | 20  |     cstate->off_linkpl.constant_part = 24;  /* ipnet header length */  | 
1700  | 20  |     cstate->off_nl = 0;  | 
1701  | 20  |     cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1702  | 20  |     break;  | 
1703  |  |  | 
1704  | 28  |   case DLT_NETANALYZER:  | 
1705  | 28  |     cstate->off_linkhdr.constant_part = 4;  /* Ethernet header is past 4-byte pseudo-header */  | 
1706  | 28  |     cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;  | 
1707  | 28  |     cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* pseudo-header+Ethernet header length */  | 
1708  | 28  |     cstate->off_nl = 0;   /* Ethernet II */  | 
1709  | 28  |     cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */  | 
1710  | 28  |     break;  | 
1711  |  |  | 
1712  | 20  |   case DLT_NETANALYZER_TRANSPARENT:  | 
1713  | 20  |     cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */  | 
1714  | 20  |     cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;  | 
1715  | 20  |     cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* pseudo-header+preamble+SFD+Ethernet header length */  | 
1716  | 20  |     cstate->off_nl = 0;   /* Ethernet II */  | 
1717  | 20  |     cstate->off_nl_nosnap = 3;  /* 802.3+802.2 */  | 
1718  | 20  |     break;  | 
1719  |  |  | 
1720  | 657  |   default:  | 
1721  |  |     /*  | 
1722  |  |      * For values in the range in which we've assigned new  | 
1723  |  |      * DLT_ values, only raw "link[N:M]" filtering is supported.  | 
1724  |  |      */  | 
1725  | 657  |     if (cstate->linktype >= DLT_MATCHING_MIN &&  | 
1726  | 657  |         cstate->linktype <= DLT_MATCHING_MAX) { | 
1727  | 630  |       cstate->off_linktype.constant_part = OFFSET_NOT_SET;  | 
1728  | 630  |       cstate->off_linkpl.constant_part = OFFSET_NOT_SET;  | 
1729  | 630  |       cstate->off_nl = OFFSET_NOT_SET;  | 
1730  | 630  |       cstate->off_nl_nosnap = OFFSET_NOT_SET;  | 
1731  | 630  |     } else { | 
1732  | 27  |       bpf_set_error(cstate, "unknown data link type %d (min %d, max %d)",  | 
1733  | 27  |           cstate->linktype, DLT_MATCHING_MIN, DLT_MATCHING_MAX);  | 
1734  | 27  |       return (-1);  | 
1735  | 27  |     }  | 
1736  | 630  |     break;  | 
1737  | 3.72k  |   }  | 
1738  |  |  | 
1739  | 3.70k  |   cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr;  | 
1740  | 3.70k  |   return (0);  | 
1741  | 3.72k  | }  | 
1742  |  |  | 
1743  |  | /*  | 
1744  |  |  * Load a value relative to the specified absolute offset.  | 
1745  |  |  */  | 
1746  |  | static struct slist *  | 
1747  |  | gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset,  | 
1748  |  |     u_int offset, u_int size)  | 
1749  | 8.21k  | { | 
1750  | 8.21k  |   struct slist *s, *s2;  | 
1751  |  |  | 
1752  | 8.21k  |   s = gen_abs_offset_varpart(cstate, abs_offset);  | 
1753  |  |  | 
1754  |  |   /*  | 
1755  |  |    * If "s" is non-null, it has code to arrange that the X register  | 
1756  |  |    * contains the variable part of the absolute offset, so we  | 
1757  |  |    * generate a load relative to that, with an offset of  | 
1758  |  |    * abs_offset->constant_part + offset.  | 
1759  |  |    *  | 
1760  |  |    * Otherwise, we can do an absolute load with an offset of  | 
1761  |  |    * abs_offset->constant_part + offset.  | 
1762  |  |    */  | 
1763  | 8.21k  |   if (s != NULL) { | 
1764  |  |     /*  | 
1765  |  |      * "s" points to a list of statements that puts the  | 
1766  |  |      * variable part of the absolute offset into the X register.  | 
1767  |  |      * Do an indirect load, to use the X register as an offset.  | 
1768  |  |      */  | 
1769  | 1.99k  |     s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);  | 
1770  | 1.99k  |     s2->s.k = abs_offset->constant_part + offset;  | 
1771  | 1.99k  |     sappend(s, s2);  | 
1772  | 6.22k  |   } else { | 
1773  |  |     /*  | 
1774  |  |      * There is no variable part of the absolute offset, so  | 
1775  |  |      * just do an absolute load.  | 
1776  |  |      */  | 
1777  | 6.22k  |     s = new_stmt(cstate, BPF_LD|BPF_ABS|size);  | 
1778  | 6.22k  |     s->s.k = abs_offset->constant_part + offset;  | 
1779  | 6.22k  |   }  | 
1780  | 8.21k  |   return s;  | 
1781  | 8.21k  | }  | 
1782  |  |  | 
1783  |  | /*  | 
1784  |  |  * Load a value relative to the beginning of the specified header.  | 
1785  |  |  */  | 
1786  |  | static struct slist *  | 
1787  |  | gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,  | 
1788  |  |     u_int size)  | 
1789  | 8.23k  | { | 
1790  | 8.23k  |   struct slist *s, *s2;  | 
1791  |  |  | 
1792  |  |   /*  | 
1793  |  |    * Squelch warnings from compilers that *don't* assume that  | 
1794  |  |    * offrel always has a valid enum value and therefore don't  | 
1795  |  |    * assume that we'll always go through one of the case arms.  | 
1796  |  |    *  | 
1797  |  |    * If we have a default case, compilers that *do* assume that  | 
1798  |  |    * will then complain about the default case code being  | 
1799  |  |    * unreachable.  | 
1800  |  |    *  | 
1801  |  |    * Damned if you do, damned if you don't.  | 
1802  |  |    */  | 
1803  | 8.23k  |   s = NULL;  | 
1804  |  |  | 
1805  | 8.23k  |   switch (offrel) { | 
1806  |  |  | 
1807  | 17  |   case OR_PACKET:  | 
1808  | 17  |                 s = new_stmt(cstate, BPF_LD|BPF_ABS|size);  | 
1809  | 17  |                 s->s.k = offset;  | 
1810  | 17  |     break;  | 
1811  |  |  | 
1812  | 2.04k  |   case OR_LINKHDR:  | 
1813  | 2.04k  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size);  | 
1814  | 2.04k  |     break;  | 
1815  |  |  | 
1816  | 0  |   case OR_PREVLINKHDR:  | 
1817  | 0  |     s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size);  | 
1818  | 0  |     break;  | 
1819  |  |  | 
1820  | 907  |   case OR_LLC:  | 
1821  | 907  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size);  | 
1822  | 907  |     break;  | 
1823  |  |  | 
1824  | 0  |   case OR_PREVMPLSHDR:  | 
1825  | 0  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size);  | 
1826  | 0  |     break;  | 
1827  |  |  | 
1828  | 1.18k  |   case OR_LINKPL:  | 
1829  | 1.18k  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size);  | 
1830  | 1.18k  |     break;  | 
1831  |  |  | 
1832  | 3.46k  |   case OR_LINKPL_NOSNAP:  | 
1833  | 3.46k  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size);  | 
1834  | 3.46k  |     break;  | 
1835  |  |  | 
1836  | 603  |   case OR_LINKTYPE:  | 
1837  | 603  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size);  | 
1838  | 603  |     break;  | 
1839  |  |  | 
1840  | 0  |   case OR_TRAN_IPV4:  | 
1841  |  |     /*  | 
1842  |  |      * Load the X register with the length of the IPv4 header  | 
1843  |  |      * (plus the offset of the link-layer header, if it's  | 
1844  |  |      * preceded by a variable-length header such as a radio  | 
1845  |  |      * header), in bytes.  | 
1846  |  |      */  | 
1847  | 0  |     s = gen_loadx_iphdrlen(cstate);  | 
1848  |  |  | 
1849  |  |     /*  | 
1850  |  |      * Load the item at {offset of the link-layer payload} + | 
1851  |  |      * {offset, relative to the start of the link-layer | 
1852  |  |      * paylod, of the IPv4 header} + {length of the IPv4 header} + | 
1853  |  |      * {specified offset}. | 
1854  |  |      *  | 
1855  |  |      * If the offset of the link-layer payload is variable,  | 
1856  |  |      * the variable part of that offset is included in the  | 
1857  |  |      * value in the X register, and we include the constant  | 
1858  |  |      * part in the offset of the load.  | 
1859  |  |      */  | 
1860  | 0  |     s2 = new_stmt(cstate, BPF_LD|BPF_IND|size);  | 
1861  | 0  |     s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset;  | 
1862  | 0  |     sappend(s, s2);  | 
1863  | 0  |     break;  | 
1864  |  |  | 
1865  | 0  |   case OR_TRAN_IPV6:  | 
1866  | 0  |     s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size);  | 
1867  | 0  |     break;  | 
1868  | 8.23k  |   }  | 
1869  | 8.23k  |   return s;  | 
1870  | 8.23k  | }  | 
1871  |  |  | 
1872  |  | /*  | 
1873  |  |  * Generate code to load into the X register the sum of the length of  | 
1874  |  |  * the IPv4 header and the variable part of the offset of the link-layer  | 
1875  |  |  * payload.  | 
1876  |  |  */  | 
1877  |  | static struct slist *  | 
1878  |  | gen_loadx_iphdrlen(compiler_state_t *cstate)  | 
1879  | 0  | { | 
1880  | 0  |   struct slist *s, *s2;  | 
1881  |  | 
  | 
1882  | 0  |   s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);  | 
1883  | 0  |   if (s != NULL) { | 
1884  |  |     /*  | 
1885  |  |      * The offset of the link-layer payload has a variable  | 
1886  |  |      * part.  "s" points to a list of statements that put  | 
1887  |  |      * the variable part of that offset into the X register.  | 
1888  |  |      *  | 
1889  |  |      * The 4*([k]&0xf) addressing mode can't be used, as we  | 
1890  |  |      * don't have a constant offset, so we have to load the  | 
1891  |  |      * value in question into the A register and add to it  | 
1892  |  |      * the value from the X register.  | 
1893  |  |      */  | 
1894  | 0  |     s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
1895  | 0  |     s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
1896  | 0  |     sappend(s, s2);  | 
1897  | 0  |     s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
1898  | 0  |     s2->s.k = 0xf;  | 
1899  | 0  |     sappend(s, s2);  | 
1900  | 0  |     s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);  | 
1901  | 0  |     s2->s.k = 2;  | 
1902  | 0  |     sappend(s, s2);  | 
1903  |  |  | 
1904  |  |     /*  | 
1905  |  |      * The A register now contains the length of the IP header.  | 
1906  |  |      * We need to add to it the variable part of the offset of  | 
1907  |  |      * the link-layer payload, which is still in the X  | 
1908  |  |      * register, and move the result into the X register.  | 
1909  |  |      */  | 
1910  | 0  |     sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));  | 
1911  | 0  |     sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));  | 
1912  | 0  |   } else { | 
1913  |  |     /*  | 
1914  |  |      * The offset of the link-layer payload is a constant,  | 
1915  |  |      * so no code was generated to load the (non-existent)  | 
1916  |  |      * variable part of that offset.  | 
1917  |  |      *  | 
1918  |  |      * This means we can use the 4*([k]&0xf) addressing  | 
1919  |  |      * mode.  Load the length of the IPv4 header, which  | 
1920  |  |      * is at an offset of cstate->off_nl from the beginning of  | 
1921  |  |      * the link-layer payload, and thus at an offset of  | 
1922  |  |      * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning  | 
1923  |  |      * of the raw packet data, using that addressing mode.  | 
1924  |  |      */  | 
1925  | 0  |     s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);  | 
1926  | 0  |     s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
1927  | 0  |   }  | 
1928  | 0  |   return s;  | 
1929  | 0  | }  | 
1930  |  |  | 
1931  |  |  | 
1932  |  | static struct block *  | 
1933  |  | gen_uncond(compiler_state_t *cstate, int rsense)  | 
1934  | 435  | { | 
1935  | 435  |   struct block *b;  | 
1936  | 435  |   struct slist *s;  | 
1937  |  |  | 
1938  | 435  |   s = new_stmt(cstate, BPF_LD|BPF_IMM);  | 
1939  | 435  |   s->s.k = !rsense;  | 
1940  | 435  |   b = new_block(cstate, JMP(BPF_JEQ));  | 
1941  | 435  |   b->stmts = s;  | 
1942  |  |  | 
1943  | 435  |   return b;  | 
1944  | 435  | }  | 
1945  |  |  | 
1946  |  | static inline struct block *  | 
1947  |  | gen_true(compiler_state_t *cstate)  | 
1948  | 18  | { | 
1949  | 18  |   return gen_uncond(cstate, 1);  | 
1950  | 18  | }  | 
1951  |  |  | 
1952  |  | static inline struct block *  | 
1953  |  | gen_false(compiler_state_t *cstate)  | 
1954  | 417  | { | 
1955  | 417  |   return gen_uncond(cstate, 0);  | 
1956  | 417  | }  | 
1957  |  |  | 
1958  |  | /*  | 
1959  |  |  * Byte-swap a 32-bit number.  | 
1960  |  |  * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on | 
1961  |  |  * big-endian platforms.)  | 
1962  |  |  */  | 
1963  | 618  | #define SWAPLONG(y) \  | 
1964  | 618  | ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))  | 
1965  |  |  | 
1966  |  | /*  | 
1967  |  |  * Generate code to match a particular packet type.  | 
1968  |  |  *  | 
1969  |  |  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP  | 
1970  |  |  * value, if <= ETHERMTU.  We use that to determine whether to  | 
1971  |  |  * match the type/length field or to check the type/length field for  | 
1972  |  |  * a value <= ETHERMTU to see whether it's a type field and then do  | 
1973  |  |  * the appropriate test.  | 
1974  |  |  */  | 
1975  |  | static struct block *  | 
1976  |  | gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
1977  | 177  | { | 
1978  | 177  |   struct block *b0, *b1;  | 
1979  |  |  | 
1980  | 177  |   switch (ll_proto) { | 
1981  |  |  | 
1982  | 150  |   case LLCSAP_ISONS:  | 
1983  | 150  |   case LLCSAP_IP:  | 
1984  | 150  |   case LLCSAP_NETBEUI:  | 
1985  |  |     /*  | 
1986  |  |      * OSI protocols and NetBEUI always use 802.2 encapsulation,  | 
1987  |  |      * so we check the DSAP and SSAP.  | 
1988  |  |      *  | 
1989  |  |      * LLCSAP_IP checks for IP-over-802.2, rather  | 
1990  |  |      * than IP-over-Ethernet or IP-over-SNAP.  | 
1991  |  |      *  | 
1992  |  |      * XXX - should we check both the DSAP and the  | 
1993  |  |      * SSAP, like this, or should we check just the  | 
1994  |  |      * DSAP, as we do for other types <= ETHERMTU  | 
1995  |  |      * (i.e., other SAP values)?  | 
1996  |  |      */  | 
1997  | 150  |     b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);  | 
1998  | 150  |     gen_not(b0);  | 
1999  | 150  |     b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);  | 
2000  | 150  |     gen_and(b0, b1);  | 
2001  | 150  |     return b1;  | 
2002  |  |  | 
2003  | 0  |   case LLCSAP_IPX:  | 
2004  |  |     /*  | 
2005  |  |      * Check for;  | 
2006  |  |      *  | 
2007  |  |      *  Ethernet_II frames, which are Ethernet  | 
2008  |  |      *  frames with a frame type of ETHERTYPE_IPX;  | 
2009  |  |      *  | 
2010  |  |      *  Ethernet_802.3 frames, which are 802.3  | 
2011  |  |      *  frames (i.e., the type/length field is  | 
2012  |  |      *  a length field, <= ETHERMTU, rather than  | 
2013  |  |      *  a type field) with the first two bytes  | 
2014  |  |      *  after the Ethernet/802.3 header being  | 
2015  |  |      *  0xFFFF;  | 
2016  |  |      *  | 
2017  |  |      *  Ethernet_802.2 frames, which are 802.3  | 
2018  |  |      *  frames with an 802.2 LLC header and  | 
2019  |  |      *  with the IPX LSAP as the DSAP in the LLC  | 
2020  |  |      *  header;  | 
2021  |  |      *  | 
2022  |  |      *  Ethernet_SNAP frames, which are 802.3  | 
2023  |  |      *  frames with an LLC header and a SNAP  | 
2024  |  |      *  header and with an OUI of 0x000000  | 
2025  |  |      *  (encapsulated Ethernet) and a protocol  | 
2026  |  |      *  ID of ETHERTYPE_IPX in the SNAP header.  | 
2027  |  |      *  | 
2028  |  |      * XXX - should we generate the same code both  | 
2029  |  |      * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?  | 
2030  |  |      */  | 
2031  |  |  | 
2032  |  |     /*  | 
2033  |  |      * This generates code to check both for the  | 
2034  |  |      * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.  | 
2035  |  |      */  | 
2036  | 0  |     b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);  | 
2037  | 0  |     b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);  | 
2038  | 0  |     gen_or(b0, b1);  | 
2039  |  |  | 
2040  |  |     /*  | 
2041  |  |      * Now we add code to check for SNAP frames with  | 
2042  |  |      * ETHERTYPE_IPX, i.e. Ethernet_SNAP.  | 
2043  |  |      */  | 
2044  | 0  |     b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);  | 
2045  | 0  |     gen_or(b0, b1);  | 
2046  |  |  | 
2047  |  |     /*  | 
2048  |  |      * Now we generate code to check for 802.3  | 
2049  |  |      * frames in general.  | 
2050  |  |      */  | 
2051  | 0  |     b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);  | 
2052  | 0  |     gen_not(b0);  | 
2053  |  |  | 
2054  |  |     /*  | 
2055  |  |      * Now add the check for 802.3 frames before the  | 
2056  |  |      * check for Ethernet_802.2 and Ethernet_802.3,  | 
2057  |  |      * as those checks should only be done on 802.3  | 
2058  |  |      * frames, not on Ethernet frames.  | 
2059  |  |      */  | 
2060  | 0  |     gen_and(b0, b1);  | 
2061  |  |  | 
2062  |  |     /*  | 
2063  |  |      * Now add the check for Ethernet_II frames, and  | 
2064  |  |      * do that before checking for the other frame  | 
2065  |  |      * types.  | 
2066  |  |      */  | 
2067  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);  | 
2068  | 0  |     gen_or(b0, b1);  | 
2069  | 0  |     return b1;  | 
2070  |  |  | 
2071  | 0  |   case ETHERTYPE_ATALK:  | 
2072  | 0  |   case ETHERTYPE_AARP:  | 
2073  |  |     /*  | 
2074  |  |      * EtherTalk (AppleTalk protocols on Ethernet link  | 
2075  |  |      * layer) may use 802.2 encapsulation.  | 
2076  |  |      */  | 
2077  |  |  | 
2078  |  |     /*  | 
2079  |  |      * Check for 802.2 encapsulation (EtherTalk phase 2?);  | 
2080  |  |      * we check for an Ethernet type field less than  | 
2081  |  |      * 1500, which means it's an 802.3 length field.  | 
2082  |  |      */  | 
2083  | 0  |     b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);  | 
2084  | 0  |     gen_not(b0);  | 
2085  |  |  | 
2086  |  |     /*  | 
2087  |  |      * 802.2-encapsulated ETHERTYPE_ATALK packets are  | 
2088  |  |      * SNAP packets with an organization code of  | 
2089  |  |      * 0x080007 (Apple, for Appletalk) and a protocol  | 
2090  |  |      * type of ETHERTYPE_ATALK (Appletalk).  | 
2091  |  |      *  | 
2092  |  |      * 802.2-encapsulated ETHERTYPE_AARP packets are  | 
2093  |  |      * SNAP packets with an organization code of  | 
2094  |  |      * 0x000000 (encapsulated Ethernet) and a protocol  | 
2095  |  |      * type of ETHERTYPE_AARP (Appletalk ARP).  | 
2096  |  |      */  | 
2097  | 0  |     if (ll_proto == ETHERTYPE_ATALK)  | 
2098  | 0  |       b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);  | 
2099  | 0  |     else  /* ll_proto == ETHERTYPE_AARP */  | 
2100  | 0  |       b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);  | 
2101  | 0  |     gen_and(b0, b1);  | 
2102  |  |  | 
2103  |  |     /*  | 
2104  |  |      * Check for Ethernet encapsulation (Ethertalk  | 
2105  |  |      * phase 1?); we just check for the Ethernet  | 
2106  |  |      * protocol type.  | 
2107  |  |      */  | 
2108  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
2109  |  | 
  | 
2110  | 0  |     gen_or(b0, b1);  | 
2111  | 0  |     return b1;  | 
2112  |  |  | 
2113  | 27  |   default:  | 
2114  | 27  |     if (ll_proto <= ETHERMTU) { | 
2115  |  |       /*  | 
2116  |  |        * This is an LLC SAP value, so the frames  | 
2117  |  |        * that match would be 802.2 frames.  | 
2118  |  |        * Check that the frame is an 802.2 frame  | 
2119  |  |        * (i.e., that the length/type field is  | 
2120  |  |        * a length field, <= ETHERMTU) and  | 
2121  |  |        * then check the DSAP.  | 
2122  |  |        */  | 
2123  | 3  |       b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);  | 
2124  | 3  |       gen_not(b0);  | 
2125  | 3  |       b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto);  | 
2126  | 3  |       gen_and(b0, b1);  | 
2127  | 3  |       return b1;  | 
2128  | 24  |     } else { | 
2129  |  |       /*  | 
2130  |  |        * This is an Ethernet type, so compare  | 
2131  |  |        * the length/type field with it (if  | 
2132  |  |        * the frame is an 802.2 frame, the length  | 
2133  |  |        * field will be <= ETHERMTU, and, as  | 
2134  |  |        * "ll_proto" is > ETHERMTU, this test  | 
2135  |  |        * will fail and the frame won't match,  | 
2136  |  |        * which is what we want).  | 
2137  |  |        */  | 
2138  | 24  |       return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
2139  | 24  |     }  | 
2140  | 177  |   }  | 
2141  | 177  | }  | 
2142  |  |  | 
2143  |  | static struct block *  | 
2144  |  | gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
2145  | 187  | { | 
2146  |  |   /*  | 
2147  |  |    * For DLT_NULL, the link-layer header is a 32-bit word  | 
2148  |  |    * containing an AF_ value in *host* byte order, and for  | 
2149  |  |    * DLT_ENC, the link-layer header begins with a 32-bit  | 
2150  |  |    * word containing an AF_ value in host byte order.  | 
2151  |  |    *  | 
2152  |  |    * In addition, if we're reading a saved capture file,  | 
2153  |  |    * the host byte order in the capture may not be the  | 
2154  |  |    * same as the host byte order on this machine.  | 
2155  |  |    *  | 
2156  |  |    * For DLT_LOOP, the link-layer header is a 32-bit  | 
2157  |  |    * word containing an AF_ value in *network* byte order.  | 
2158  |  |    */  | 
2159  | 187  |   if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) { | 
2160  |  |     /*  | 
2161  |  |      * The AF_ value is in host byte order, but the BPF  | 
2162  |  |      * interpreter will convert it to network byte order.  | 
2163  |  |      *  | 
2164  |  |      * If this is a save file, and it's from a machine  | 
2165  |  |      * with the opposite byte order to ours, we byte-swap  | 
2166  |  |      * the AF_ value.  | 
2167  |  |      *  | 
2168  |  |      * Then we run it through "htonl()", and generate  | 
2169  |  |      * code to compare against the result.  | 
2170  |  |      */  | 
2171  | 162  |     if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped)  | 
2172  | 152  |       ll_proto = SWAPLONG(ll_proto);  | 
2173  | 162  |     ll_proto = htonl(ll_proto);  | 
2174  | 162  |   }  | 
2175  | 187  |   return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto));  | 
2176  | 187  | }  | 
2177  |  |  | 
2178  |  | /*  | 
2179  |  |  * "proto" is an Ethernet type value and for IPNET, if it is not IPv4  | 
2180  |  |  * or IPv6 then we have an error.  | 
2181  |  |  */  | 
2182  |  | static struct block *  | 
2183  |  | gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
2184  | 37  | { | 
2185  | 37  |   switch (ll_proto) { | 
2186  |  |  | 
2187  | 4  |   case ETHERTYPE_IP:  | 
2188  | 4  |     return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET);  | 
2189  |  |     /*NOTREACHED*/  | 
2190  |  |  | 
2191  | 4  |   case ETHERTYPE_IPV6:  | 
2192  | 4  |     return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6);  | 
2193  |  |     /*NOTREACHED*/  | 
2194  |  |  | 
2195  | 29  |   default:  | 
2196  | 29  |     break;  | 
2197  | 37  |   }  | 
2198  |  |  | 
2199  | 29  |   return gen_false(cstate);  | 
2200  | 37  | }  | 
2201  |  |  | 
2202  |  | /*  | 
2203  |  |  * Generate code to match a particular packet type.  | 
2204  |  |  *  | 
2205  |  |  * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP  | 
2206  |  |  * value, if <= ETHERMTU.  We use that to determine whether to  | 
2207  |  |  * match the type field or to check the type field for the special  | 
2208  |  |  * LINUX_SLL_P_802_2 value and then do the appropriate test.  | 
2209  |  |  */  | 
2210  |  | static struct block *  | 
2211  |  | gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
2212  | 85  | { | 
2213  | 85  |   struct block *b0, *b1;  | 
2214  |  |  | 
2215  | 85  |   switch (ll_proto) { | 
2216  |  |  | 
2217  | 68  |   case LLCSAP_ISONS:  | 
2218  | 68  |   case LLCSAP_IP:  | 
2219  | 68  |   case LLCSAP_NETBEUI:  | 
2220  |  |     /*  | 
2221  |  |      * OSI protocols and NetBEUI always use 802.2 encapsulation,  | 
2222  |  |      * so we check the DSAP and SSAP.  | 
2223  |  |      *  | 
2224  |  |      * LLCSAP_IP checks for IP-over-802.2, rather  | 
2225  |  |      * than IP-over-Ethernet or IP-over-SNAP.  | 
2226  |  |      *  | 
2227  |  |      * XXX - should we check both the DSAP and the  | 
2228  |  |      * SSAP, like this, or should we check just the  | 
2229  |  |      * DSAP, as we do for other types <= ETHERMTU  | 
2230  |  |      * (i.e., other SAP values)?  | 
2231  |  |      */  | 
2232  | 68  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);  | 
2233  | 68  |     b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto);  | 
2234  | 68  |     gen_and(b0, b1);  | 
2235  | 68  |     return b1;  | 
2236  |  |  | 
2237  | 0  |   case LLCSAP_IPX:  | 
2238  |  |     /*  | 
2239  |  |      *  Ethernet_II frames, which are Ethernet  | 
2240  |  |      *  frames with a frame type of ETHERTYPE_IPX;  | 
2241  |  |      *  | 
2242  |  |      *  Ethernet_802.3 frames, which have a frame  | 
2243  |  |      *  type of LINUX_SLL_P_802_3;  | 
2244  |  |      *  | 
2245  |  |      *  Ethernet_802.2 frames, which are 802.3  | 
2246  |  |      *  frames with an 802.2 LLC header (i.e, have  | 
2247  |  |      *  a frame type of LINUX_SLL_P_802_2) and  | 
2248  |  |      *  with the IPX LSAP as the DSAP in the LLC  | 
2249  |  |      *  header;  | 
2250  |  |      *  | 
2251  |  |      *  Ethernet_SNAP frames, which are 802.3  | 
2252  |  |      *  frames with an LLC header and a SNAP  | 
2253  |  |      *  header and with an OUI of 0x000000  | 
2254  |  |      *  (encapsulated Ethernet) and a protocol  | 
2255  |  |      *  ID of ETHERTYPE_IPX in the SNAP header.  | 
2256  |  |      *  | 
2257  |  |      * First, do the checks on LINUX_SLL_P_802_2  | 
2258  |  |      * frames; generate the check for either  | 
2259  |  |      * Ethernet_802.2 or Ethernet_SNAP frames, and  | 
2260  |  |      * then put a check for LINUX_SLL_P_802_2 frames  | 
2261  |  |      * before it.  | 
2262  |  |      */  | 
2263  | 0  |     b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);  | 
2264  | 0  |     b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX);  | 
2265  | 0  |     gen_or(b0, b1);  | 
2266  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);  | 
2267  | 0  |     gen_and(b0, b1);  | 
2268  |  |  | 
2269  |  |     /*  | 
2270  |  |      * Now check for 802.3 frames and OR that with  | 
2271  |  |      * the previous test.  | 
2272  |  |      */  | 
2273  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3);  | 
2274  | 0  |     gen_or(b0, b1);  | 
2275  |  |  | 
2276  |  |     /*  | 
2277  |  |      * Now add the check for Ethernet_II frames, and  | 
2278  |  |      * do that before checking for the other frame  | 
2279  |  |      * types.  | 
2280  |  |      */  | 
2281  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX);  | 
2282  | 0  |     gen_or(b0, b1);  | 
2283  | 0  |     return b1;  | 
2284  |  |  | 
2285  | 0  |   case ETHERTYPE_ATALK:  | 
2286  | 0  |   case ETHERTYPE_AARP:  | 
2287  |  |     /*  | 
2288  |  |      * EtherTalk (AppleTalk protocols on Ethernet link  | 
2289  |  |      * layer) may use 802.2 encapsulation.  | 
2290  |  |      */  | 
2291  |  |  | 
2292  |  |     /*  | 
2293  |  |      * Check for 802.2 encapsulation (EtherTalk phase 2?);  | 
2294  |  |      * we check for the 802.2 protocol type in the  | 
2295  |  |      * "Ethernet type" field.  | 
2296  |  |      */  | 
2297  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);  | 
2298  |  |  | 
2299  |  |     /*  | 
2300  |  |      * 802.2-encapsulated ETHERTYPE_ATALK packets are  | 
2301  |  |      * SNAP packets with an organization code of  | 
2302  |  |      * 0x080007 (Apple, for Appletalk) and a protocol  | 
2303  |  |      * type of ETHERTYPE_ATALK (Appletalk).  | 
2304  |  |      *  | 
2305  |  |      * 802.2-encapsulated ETHERTYPE_AARP packets are  | 
2306  |  |      * SNAP packets with an organization code of  | 
2307  |  |      * 0x000000 (encapsulated Ethernet) and a protocol  | 
2308  |  |      * type of ETHERTYPE_AARP (Appletalk ARP).  | 
2309  |  |      */  | 
2310  | 0  |     if (ll_proto == ETHERTYPE_ATALK)  | 
2311  | 0  |       b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);  | 
2312  | 0  |     else  /* ll_proto == ETHERTYPE_AARP */  | 
2313  | 0  |       b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP);  | 
2314  | 0  |     gen_and(b0, b1);  | 
2315  |  |  | 
2316  |  |     /*  | 
2317  |  |      * Check for Ethernet encapsulation (Ethertalk  | 
2318  |  |      * phase 1?); we just check for the Ethernet  | 
2319  |  |      * protocol type.  | 
2320  |  |      */  | 
2321  | 0  |     b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
2322  |  | 
  | 
2323  | 0  |     gen_or(b0, b1);  | 
2324  | 0  |     return b1;  | 
2325  |  |  | 
2326  | 17  |   default:  | 
2327  | 17  |     if (ll_proto <= ETHERMTU) { | 
2328  |  |       /*  | 
2329  |  |        * This is an LLC SAP value, so the frames  | 
2330  |  |        * that match would be 802.2 frames.  | 
2331  |  |        * Check for the 802.2 protocol type  | 
2332  |  |        * in the "Ethernet type" field, and  | 
2333  |  |        * then check the DSAP.  | 
2334  |  |        */  | 
2335  | 3  |       b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2);  | 
2336  | 3  |       b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B,  | 
2337  | 3  |            ll_proto);  | 
2338  | 3  |       gen_and(b0, b1);  | 
2339  | 3  |       return b1;  | 
2340  | 14  |     } else { | 
2341  |  |       /*  | 
2342  |  |        * This is an Ethernet type, so compare  | 
2343  |  |        * the length/type field with it (if  | 
2344  |  |        * the frame is an 802.2 frame, the length  | 
2345  |  |        * field will be <= ETHERMTU, and, as  | 
2346  |  |        * "ll_proto" is > ETHERMTU, this test  | 
2347  |  |        * will fail and the frame won't match,  | 
2348  |  |        * which is what we want).  | 
2349  |  |        */  | 
2350  | 14  |       return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
2351  | 14  |     }  | 
2352  | 85  |   }  | 
2353  | 85  | }  | 
2354  |  |  | 
2355  |  | /*  | 
2356  |  |  * Load a value relative to the beginning of the link-layer header after the  | 
2357  |  |  * pflog header.  | 
2358  |  |  */  | 
2359  |  | static struct slist *  | 
2360  |  | gen_load_pflog_llprefixlen(compiler_state_t *cstate)  | 
2361  | 71  | { | 
2362  | 71  |   struct slist *s1, *s2;  | 
2363  |  |  | 
2364  |  |   /*  | 
2365  |  |    * Generate code to load the length of the pflog header into  | 
2366  |  |    * the register assigned to hold that length, if one has been  | 
2367  |  |    * assigned.  (If one hasn't been assigned, no code we've  | 
2368  |  |    * generated uses that prefix, so we don't need to generate any  | 
2369  |  |    * code to load it.)  | 
2370  |  |    */  | 
2371  | 71  |   if (cstate->off_linkpl.reg != -1) { | 
2372  |  |     /*  | 
2373  |  |      * The length is in the first byte of the header.  | 
2374  |  |      */  | 
2375  | 63  |     s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
2376  | 63  |     s1->s.k = 0;  | 
2377  |  |  | 
2378  |  |     /*  | 
2379  |  |      * Round it up to a multiple of 4.  | 
2380  |  |      * Add 3, and clear the lower 2 bits.  | 
2381  |  |      */  | 
2382  | 63  |     s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
2383  | 63  |     s2->s.k = 3;  | 
2384  | 63  |     sappend(s1, s2);  | 
2385  | 63  |     s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
2386  | 63  |     s2->s.k = 0xfffffffc;  | 
2387  | 63  |     sappend(s1, s2);  | 
2388  |  |  | 
2389  |  |     /*  | 
2390  |  |      * Now allocate a register to hold that value and store  | 
2391  |  |      * it.  | 
2392  |  |      */  | 
2393  | 63  |     s2 = new_stmt(cstate, BPF_ST);  | 
2394  | 63  |     s2->s.k = cstate->off_linkpl.reg;  | 
2395  | 63  |     sappend(s1, s2);  | 
2396  |  |  | 
2397  |  |     /*  | 
2398  |  |      * Now move it into the X register.  | 
2399  |  |      */  | 
2400  | 63  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2401  | 63  |     sappend(s1, s2);  | 
2402  |  |  | 
2403  | 63  |     return (s1);  | 
2404  | 63  |   } else  | 
2405  | 8  |     return (NULL);  | 
2406  | 71  | }  | 
2407  |  |  | 
2408  |  | static struct slist *  | 
2409  |  | gen_load_prism_llprefixlen(compiler_state_t *cstate)  | 
2410  | 257  | { | 
2411  | 257  |   struct slist *s1, *s2;  | 
2412  | 257  |   struct slist *sjeq_avs_cookie;  | 
2413  | 257  |   struct slist *sjcommon;  | 
2414  |  |  | 
2415  |  |   /*  | 
2416  |  |    * This code is not compatible with the optimizer, as  | 
2417  |  |    * we are generating jmp instructions within a normal  | 
2418  |  |    * slist of instructions  | 
2419  |  |    */  | 
2420  | 257  |   cstate->no_optimize = 1;  | 
2421  |  |  | 
2422  |  |   /*  | 
2423  |  |    * Generate code to load the length of the radio header into  | 
2424  |  |    * the register assigned to hold that length, if one has been  | 
2425  |  |    * assigned.  (If one hasn't been assigned, no code we've  | 
2426  |  |    * generated uses that prefix, so we don't need to generate any  | 
2427  |  |    * code to load it.)  | 
2428  |  |    *  | 
2429  |  |    * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes  | 
2430  |  |    * or always use the AVS header rather than the Prism header.  | 
2431  |  |    * We load a 4-byte big-endian value at the beginning of the  | 
2432  |  |    * raw packet data, and see whether, when masked with 0xFFFFF000,  | 
2433  |  |    * it's equal to 0x80211000.  If so, that indicates that it's  | 
2434  |  |    * an AVS header (the masked-out bits are the version number).  | 
2435  |  |    * Otherwise, it's a Prism header.  | 
2436  |  |    *  | 
2437  |  |    * XXX - the Prism header is also, in theory, variable-length,  | 
2438  |  |    * but no known software generates headers that aren't 144  | 
2439  |  |    * bytes long.  | 
2440  |  |    */  | 
2441  | 257  |   if (cstate->off_linkhdr.reg != -1) { | 
2442  |  |     /*  | 
2443  |  |      * Load the cookie.  | 
2444  |  |      */  | 
2445  | 28  |     s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);  | 
2446  | 28  |     s1->s.k = 0;  | 
2447  |  |  | 
2448  |  |     /*  | 
2449  |  |      * AND it with 0xFFFFF000.  | 
2450  |  |      */  | 
2451  | 28  |     s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
2452  | 28  |     s2->s.k = 0xFFFFF000;  | 
2453  | 28  |     sappend(s1, s2);  | 
2454  |  |  | 
2455  |  |     /*  | 
2456  |  |      * Compare with 0x80211000.  | 
2457  |  |      */  | 
2458  | 28  |     sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ));  | 
2459  | 28  |     sjeq_avs_cookie->s.k = 0x80211000;  | 
2460  | 28  |     sappend(s1, sjeq_avs_cookie);  | 
2461  |  |  | 
2462  |  |     /*  | 
2463  |  |      * If it's AVS:  | 
2464  |  |      *  | 
2465  |  |      * The 4 bytes at an offset of 4 from the beginning of  | 
2466  |  |      * the AVS header are the length of the AVS header.  | 
2467  |  |      * That field is big-endian.  | 
2468  |  |      */  | 
2469  | 28  |     s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);  | 
2470  | 28  |     s2->s.k = 4;  | 
2471  | 28  |     sappend(s1, s2);  | 
2472  | 28  |     sjeq_avs_cookie->s.jt = s2;  | 
2473  |  |  | 
2474  |  |     /*  | 
2475  |  |      * Now jump to the code to allocate a register  | 
2476  |  |      * into which to save the header length and  | 
2477  |  |      * store the length there.  (The "jump always"  | 
2478  |  |      * instruction needs to have the k field set;  | 
2479  |  |      * it's added to the PC, so, as we're jumping  | 
2480  |  |      * over a single instruction, it should be 1.)  | 
2481  |  |      */  | 
2482  | 28  |     sjcommon = new_stmt(cstate, JMP(BPF_JA));  | 
2483  | 28  |     sjcommon->s.k = 1;  | 
2484  | 28  |     sappend(s1, sjcommon);  | 
2485  |  |  | 
2486  |  |     /*  | 
2487  |  |      * Now for the code that handles the Prism header.  | 
2488  |  |      * Just load the length of the Prism header (144)  | 
2489  |  |      * into the A register.  Have the test for an AVS  | 
2490  |  |      * header branch here if we don't have an AVS header.  | 
2491  |  |      */  | 
2492  | 28  |     s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);  | 
2493  | 28  |     s2->s.k = 144;  | 
2494  | 28  |     sappend(s1, s2);  | 
2495  | 28  |     sjeq_avs_cookie->s.jf = s2;  | 
2496  |  |  | 
2497  |  |     /*  | 
2498  |  |      * Now allocate a register to hold that value and store  | 
2499  |  |      * it.  The code for the AVS header will jump here after  | 
2500  |  |      * loading the length of the AVS header.  | 
2501  |  |      */  | 
2502  | 28  |     s2 = new_stmt(cstate, BPF_ST);  | 
2503  | 28  |     s2->s.k = cstate->off_linkhdr.reg;  | 
2504  | 28  |     sappend(s1, s2);  | 
2505  | 28  |     sjcommon->s.jf = s2;  | 
2506  |  |  | 
2507  |  |     /*  | 
2508  |  |      * Now move it into the X register.  | 
2509  |  |      */  | 
2510  | 28  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2511  | 28  |     sappend(s1, s2);  | 
2512  |  |  | 
2513  | 28  |     return (s1);  | 
2514  | 28  |   } else  | 
2515  | 229  |     return (NULL);  | 
2516  | 257  | }  | 
2517  |  |  | 
2518  |  | static struct slist *  | 
2519  |  | gen_load_avs_llprefixlen(compiler_state_t *cstate)  | 
2520  | 20  | { | 
2521  | 20  |   struct slist *s1, *s2;  | 
2522  |  |  | 
2523  |  |   /*  | 
2524  |  |    * Generate code to load the length of the AVS header into  | 
2525  |  |    * the register assigned to hold that length, if one has been  | 
2526  |  |    * assigned.  (If one hasn't been assigned, no code we've  | 
2527  |  |    * generated uses that prefix, so we don't need to generate any  | 
2528  |  |    * code to load it.)  | 
2529  |  |    */  | 
2530  | 20  |   if (cstate->off_linkhdr.reg != -1) { | 
2531  |  |     /*  | 
2532  |  |      * The 4 bytes at an offset of 4 from the beginning of  | 
2533  |  |      * the AVS header are the length of the AVS header.  | 
2534  |  |      * That field is big-endian.  | 
2535  |  |      */  | 
2536  | 16  |     s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);  | 
2537  | 16  |     s1->s.k = 4;  | 
2538  |  |  | 
2539  |  |     /*  | 
2540  |  |      * Now allocate a register to hold that value and store  | 
2541  |  |      * it.  | 
2542  |  |      */  | 
2543  | 16  |     s2 = new_stmt(cstate, BPF_ST);  | 
2544  | 16  |     s2->s.k = cstate->off_linkhdr.reg;  | 
2545  | 16  |     sappend(s1, s2);  | 
2546  |  |  | 
2547  |  |     /*  | 
2548  |  |      * Now move it into the X register.  | 
2549  |  |      */  | 
2550  | 16  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2551  | 16  |     sappend(s1, s2);  | 
2552  |  |  | 
2553  | 16  |     return (s1);  | 
2554  | 16  |   } else  | 
2555  | 4  |     return (NULL);  | 
2556  | 20  | }  | 
2557  |  |  | 
2558  |  | static struct slist *  | 
2559  |  | gen_load_radiotap_llprefixlen(compiler_state_t *cstate)  | 
2560  | 102  | { | 
2561  | 102  |   struct slist *s1, *s2;  | 
2562  |  |  | 
2563  |  |   /*  | 
2564  |  |    * Generate code to load the length of the radiotap header into  | 
2565  |  |    * the register assigned to hold that length, if one has been  | 
2566  |  |    * assigned.  (If one hasn't been assigned, no code we've  | 
2567  |  |    * generated uses that prefix, so we don't need to generate any  | 
2568  |  |    * code to load it.)  | 
2569  |  |    */  | 
2570  | 102  |   if (cstate->off_linkhdr.reg != -1) { | 
2571  |  |     /*  | 
2572  |  |      * The 2 bytes at offsets of 2 and 3 from the beginning  | 
2573  |  |      * of the radiotap header are the length of the radiotap  | 
2574  |  |      * header; unfortunately, it's little-endian, so we have  | 
2575  |  |      * to load it a byte at a time and construct the value.  | 
2576  |  |      */  | 
2577  |  |  | 
2578  |  |     /*  | 
2579  |  |      * Load the high-order byte, at an offset of 3, shift it  | 
2580  |  |      * left a byte, and put the result in the X register.  | 
2581  |  |      */  | 
2582  | 96  |     s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
2583  | 96  |     s1->s.k = 3;  | 
2584  | 96  |     s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);  | 
2585  | 96  |     sappend(s1, s2);  | 
2586  | 96  |     s2->s.k = 8;  | 
2587  | 96  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2588  | 96  |     sappend(s1, s2);  | 
2589  |  |  | 
2590  |  |     /*  | 
2591  |  |      * Load the next byte, at an offset of 2, and OR the  | 
2592  |  |      * value from the X register into it.  | 
2593  |  |      */  | 
2594  | 96  |     s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
2595  | 96  |     sappend(s1, s2);  | 
2596  | 96  |     s2->s.k = 2;  | 
2597  | 96  |     s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);  | 
2598  | 96  |     sappend(s1, s2);  | 
2599  |  |  | 
2600  |  |     /*  | 
2601  |  |      * Now allocate a register to hold that value and store  | 
2602  |  |      * it.  | 
2603  |  |      */  | 
2604  | 96  |     s2 = new_stmt(cstate, BPF_ST);  | 
2605  | 96  |     s2->s.k = cstate->off_linkhdr.reg;  | 
2606  | 96  |     sappend(s1, s2);  | 
2607  |  |  | 
2608  |  |     /*  | 
2609  |  |      * Now move it into the X register.  | 
2610  |  |      */  | 
2611  | 96  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2612  | 96  |     sappend(s1, s2);  | 
2613  |  |  | 
2614  | 96  |     return (s1);  | 
2615  | 96  |   } else  | 
2616  | 6  |     return (NULL);  | 
2617  | 102  | }  | 
2618  |  |  | 
2619  |  | /*  | 
2620  |  |  * At the moment we treat PPI as normal Radiotap encoded  | 
2621  |  |  * packets. The difference is in the function that generates  | 
2622  |  |  * the code at the beginning to compute the header length.  | 
2623  |  |  * Since this code generator of PPI supports bare 802.11  | 
2624  |  |  * encapsulation only (i.e. the encapsulated DLT should be  | 
2625  |  |  * DLT_IEEE802_11) we generate code to check for this too;  | 
2626  |  |  * that's done in finish_parse().  | 
2627  |  |  */  | 
2628  |  | static struct slist *  | 
2629  |  | gen_load_ppi_llprefixlen(compiler_state_t *cstate)  | 
2630  | 184  | { | 
2631  | 184  |   struct slist *s1, *s2;  | 
2632  |  |  | 
2633  |  |   /*  | 
2634  |  |    * Generate code to load the length of the radiotap header  | 
2635  |  |    * into the register assigned to hold that length, if one has  | 
2636  |  |    * been assigned.  | 
2637  |  |    */  | 
2638  | 184  |   if (cstate->off_linkhdr.reg != -1) { | 
2639  |  |     /*  | 
2640  |  |      * The 2 bytes at offsets of 2 and 3 from the beginning  | 
2641  |  |      * of the radiotap header are the length of the radiotap  | 
2642  |  |      * header; unfortunately, it's little-endian, so we have  | 
2643  |  |      * to load it a byte at a time and construct the value.  | 
2644  |  |      */  | 
2645  |  |  | 
2646  |  |     /*  | 
2647  |  |      * Load the high-order byte, at an offset of 3, shift it  | 
2648  |  |      * left a byte, and put the result in the X register.  | 
2649  |  |      */  | 
2650  | 25  |     s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
2651  | 25  |     s1->s.k = 3;  | 
2652  | 25  |     s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K);  | 
2653  | 25  |     sappend(s1, s2);  | 
2654  | 25  |     s2->s.k = 8;  | 
2655  | 25  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2656  | 25  |     sappend(s1, s2);  | 
2657  |  |  | 
2658  |  |     /*  | 
2659  |  |      * Load the next byte, at an offset of 2, and OR the  | 
2660  |  |      * value from the X register into it.  | 
2661  |  |      */  | 
2662  | 25  |     s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
2663  | 25  |     sappend(s1, s2);  | 
2664  | 25  |     s2->s.k = 2;  | 
2665  | 25  |     s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X);  | 
2666  | 25  |     sappend(s1, s2);  | 
2667  |  |  | 
2668  |  |     /*  | 
2669  |  |      * Now allocate a register to hold that value and store  | 
2670  |  |      * it.  | 
2671  |  |      */  | 
2672  | 25  |     s2 = new_stmt(cstate, BPF_ST);  | 
2673  | 25  |     s2->s.k = cstate->off_linkhdr.reg;  | 
2674  | 25  |     sappend(s1, s2);  | 
2675  |  |  | 
2676  |  |     /*  | 
2677  |  |      * Now move it into the X register.  | 
2678  |  |      */  | 
2679  | 25  |     s2 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
2680  | 25  |     sappend(s1, s2);  | 
2681  |  |  | 
2682  | 25  |     return (s1);  | 
2683  | 25  |   } else  | 
2684  | 159  |     return (NULL);  | 
2685  | 184  | }  | 
2686  |  |  | 
2687  |  | /*  | 
2688  |  |  * Load a value relative to the beginning of the link-layer header after the 802.11  | 
2689  |  |  * header, i.e. LLC_SNAP.  | 
2690  |  |  * The link-layer header doesn't necessarily begin at the beginning  | 
2691  |  |  * of the packet data; there might be a variable-length prefix containing  | 
2692  |  |  * radio information.  | 
2693  |  |  */  | 
2694  |  | static struct slist *  | 
2695  |  | gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext)  | 
2696  | 779  | { | 
2697  | 779  |   struct slist *s2;  | 
2698  | 779  |   struct slist *sjset_data_frame_1;  | 
2699  | 779  |   struct slist *sjset_data_frame_2;  | 
2700  | 779  |   struct slist *sjset_qos;  | 
2701  | 779  |   struct slist *sjset_radiotap_flags_present;  | 
2702  | 779  |   struct slist *sjset_radiotap_ext_present;  | 
2703  | 779  |   struct slist *sjset_radiotap_tsft_present;  | 
2704  | 779  |   struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;  | 
2705  | 779  |   struct slist *s_roundup;  | 
2706  |  |  | 
2707  | 779  |   if (cstate->off_linkpl.reg == -1) { | 
2708  |  |     /*  | 
2709  |  |      * No register has been assigned to the offset of  | 
2710  |  |      * the link-layer payload, which means nobody needs  | 
2711  |  |      * it; don't bother computing it - just return  | 
2712  |  |      * what we already have.  | 
2713  |  |      */  | 
2714  | 521  |     return (s);  | 
2715  | 521  |   }  | 
2716  |  |  | 
2717  |  |   /*  | 
2718  |  |    * This code is not compatible with the optimizer, as  | 
2719  |  |    * we are generating jmp instructions within a normal  | 
2720  |  |    * slist of instructions  | 
2721  |  |    */  | 
2722  | 258  |   cstate->no_optimize = 1;  | 
2723  |  |  | 
2724  |  |   /*  | 
2725  |  |    * If "s" is non-null, it has code to arrange that the X register  | 
2726  |  |    * contains the length of the prefix preceding the link-layer  | 
2727  |  |    * header.  | 
2728  |  |    *  | 
2729  |  |    * Otherwise, the length of the prefix preceding the link-layer  | 
2730  |  |    * header is "off_outermostlinkhdr.constant_part".  | 
2731  |  |    */  | 
2732  | 258  |   if (s == NULL) { | 
2733  |  |     /*  | 
2734  |  |      * There is no variable-length header preceding the  | 
2735  |  |      * link-layer header.  | 
2736  |  |      *  | 
2737  |  |      * Load the length of the fixed-length prefix preceding  | 
2738  |  |      * the link-layer header (if any) into the X register,  | 
2739  |  |      * and store it in the cstate->off_linkpl.reg register.  | 
2740  |  |      * That length is off_outermostlinkhdr.constant_part.  | 
2741  |  |      */  | 
2742  | 100  |     s = new_stmt(cstate, BPF_LDX|BPF_IMM);  | 
2743  | 100  |     s->s.k = cstate->off_outermostlinkhdr.constant_part;  | 
2744  | 100  |   }  | 
2745  |  |  | 
2746  |  |   /*  | 
2747  |  |    * The X register contains the offset of the beginning of the  | 
2748  |  |    * link-layer header; add 24, which is the minimum length  | 
2749  |  |    * of the MAC header for a data frame, to that, and store it  | 
2750  |  |    * in cstate->off_linkpl.reg, and then load the Frame Control field,  | 
2751  |  |    * which is at the offset in the X register, with an indexed load.  | 
2752  |  |    */  | 
2753  | 258  |   s2 = new_stmt(cstate, BPF_MISC|BPF_TXA);  | 
2754  | 258  |   sappend(s, s2);  | 
2755  | 258  |   s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
2756  | 258  |   s2->s.k = 24;  | 
2757  | 258  |   sappend(s, s2);  | 
2758  | 258  |   s2 = new_stmt(cstate, BPF_ST);  | 
2759  | 258  |   s2->s.k = cstate->off_linkpl.reg;  | 
2760  | 258  |   sappend(s, s2);  | 
2761  |  |  | 
2762  | 258  |   s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
2763  | 258  |   s2->s.k = 0;  | 
2764  | 258  |   sappend(s, s2);  | 
2765  |  |  | 
2766  |  |   /*  | 
2767  |  |    * Check the Frame Control field to see if this is a data frame;  | 
2768  |  |    * a data frame has the 0x08 bit (b3) in that field set and the  | 
2769  |  |    * 0x04 bit (b2) clear.  | 
2770  |  |    */  | 
2771  | 258  |   sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET));  | 
2772  | 258  |   sjset_data_frame_1->s.k = 0x08;  | 
2773  | 258  |   sappend(s, sjset_data_frame_1);  | 
2774  |  |  | 
2775  |  |   /*  | 
2776  |  |    * If b3 is set, test b2, otherwise go to the first statement of  | 
2777  |  |    * the rest of the program.  | 
2778  |  |    */  | 
2779  | 258  |   sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET));  | 
2780  | 258  |   sjset_data_frame_2->s.k = 0x04;  | 
2781  | 258  |   sappend(s, sjset_data_frame_2);  | 
2782  | 258  |   sjset_data_frame_1->s.jf = snext;  | 
2783  |  |  | 
2784  |  |   /*  | 
2785  |  |    * If b2 is not set, this is a data frame; test the QoS bit.  | 
2786  |  |    * Otherwise, go to the first statement of the rest of the  | 
2787  |  |    * program.  | 
2788  |  |    */  | 
2789  | 258  |   sjset_data_frame_2->s.jt = snext;  | 
2790  | 258  |   sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET));  | 
2791  | 258  |   sjset_qos->s.k = 0x80;  /* QoS bit */  | 
2792  | 258  |   sappend(s, sjset_qos);  | 
2793  |  |  | 
2794  |  |   /*  | 
2795  |  |    * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS  | 
2796  |  |    * field.  | 
2797  |  |    * Otherwise, go to the first statement of the rest of the  | 
2798  |  |    * program.  | 
2799  |  |    */  | 
2800  | 258  |   sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
2801  | 258  |   s2->s.k = cstate->off_linkpl.reg;  | 
2802  | 258  |   sappend(s, s2);  | 
2803  | 258  |   s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);  | 
2804  | 258  |   s2->s.k = 2;  | 
2805  | 258  |   sappend(s, s2);  | 
2806  | 258  |   s2 = new_stmt(cstate, BPF_ST);  | 
2807  | 258  |   s2->s.k = cstate->off_linkpl.reg;  | 
2808  | 258  |   sappend(s, s2);  | 
2809  |  |  | 
2810  |  |   /*  | 
2811  |  |    * If we have a radiotap header, look at it to see whether  | 
2812  |  |    * there's Atheros padding between the MAC-layer header  | 
2813  |  |    * and the payload.  | 
2814  |  |    *  | 
2815  |  |    * Note: all of the fields in the radiotap header are  | 
2816  |  |    * little-endian, so we byte-swap all of the values  | 
2817  |  |    * we test against, as they will be loaded as big-endian  | 
2818  |  |    * values.  | 
2819  |  |    *  | 
2820  |  |    * XXX - in the general case, we would have to scan through  | 
2821  |  |    * *all* the presence bits, if there's more than one word of  | 
2822  |  |    * presence bits.  That would require a loop, meaning that  | 
2823  |  |    * we wouldn't be able to run the filter in the kernel.  | 
2824  |  |    *  | 
2825  |  |    * We assume here that the Atheros adapters that insert the  | 
2826  |  |    * annoying padding don't have multiple antennae and therefore  | 
2827  |  |    * do not generate radiotap headers with multiple presence words.  | 
2828  |  |    */  | 
2829  | 258  |   if (cstate->linktype == DLT_IEEE802_11_RADIO) { | 
2830  |  |     /*  | 
2831  |  |      * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set  | 
2832  |  |      * in the first presence flag word?  | 
2833  |  |      */  | 
2834  | 94  |     sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W);  | 
2835  | 94  |     s2->s.k = 4;  | 
2836  | 94  |     sappend(s, s2);  | 
2837  |  |  | 
2838  | 94  |     sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET));  | 
2839  | 94  |     sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002);  | 
2840  | 94  |     sappend(s, sjset_radiotap_flags_present);  | 
2841  |  |  | 
2842  |  |     /*  | 
2843  |  |      * If not, skip all of this.  | 
2844  |  |      */  | 
2845  | 94  |     sjset_radiotap_flags_present->s.jf = snext;  | 
2846  |  |  | 
2847  |  |     /*  | 
2848  |  |      * Otherwise, is the "extension" bit set in that word?  | 
2849  |  |      */  | 
2850  | 94  |     sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET));  | 
2851  | 94  |     sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000);  | 
2852  | 94  |     sappend(s, sjset_radiotap_ext_present);  | 
2853  | 94  |     sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present;  | 
2854  |  |  | 
2855  |  |     /*  | 
2856  |  |      * If so, skip all of this.  | 
2857  |  |      */  | 
2858  | 94  |     sjset_radiotap_ext_present->s.jt = snext;  | 
2859  |  |  | 
2860  |  |     /*  | 
2861  |  |      * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?  | 
2862  |  |      */  | 
2863  | 94  |     sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET));  | 
2864  | 94  |     sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001);  | 
2865  | 94  |     sappend(s, sjset_radiotap_tsft_present);  | 
2866  | 94  |     sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present;  | 
2867  |  |  | 
2868  |  |     /*  | 
2869  |  |      * If IEEE80211_RADIOTAP_TSFT is set, the flags field is  | 
2870  |  |      * at an offset of 16 from the beginning of the raw packet  | 
2871  |  |      * data (8 bytes for the radiotap header and 8 bytes for  | 
2872  |  |      * the TSFT field).  | 
2873  |  |      *  | 
2874  |  |      * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)  | 
2875  |  |      * is set.  | 
2876  |  |      */  | 
2877  | 94  |     s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);  | 
2878  | 94  |     s2->s.k = 16;  | 
2879  | 94  |     sappend(s, s2);  | 
2880  | 94  |     sjset_radiotap_tsft_present->s.jt = s2;  | 
2881  |  |  | 
2882  | 94  |     sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET));  | 
2883  | 94  |     sjset_tsft_datapad->s.k = 0x20;  | 
2884  | 94  |     sappend(s, sjset_tsft_datapad);  | 
2885  |  |  | 
2886  |  |     /*  | 
2887  |  |      * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is  | 
2888  |  |      * at an offset of 8 from the beginning of the raw packet  | 
2889  |  |      * data (8 bytes for the radiotap header).  | 
2890  |  |      *  | 
2891  |  |      * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)  | 
2892  |  |      * is set.  | 
2893  |  |      */  | 
2894  | 94  |     s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);  | 
2895  | 94  |     s2->s.k = 8;  | 
2896  | 94  |     sappend(s, s2);  | 
2897  | 94  |     sjset_radiotap_tsft_present->s.jf = s2;  | 
2898  |  |  | 
2899  | 94  |     sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET));  | 
2900  | 94  |     sjset_notsft_datapad->s.k = 0x20;  | 
2901  | 94  |     sappend(s, sjset_notsft_datapad);  | 
2902  |  |  | 
2903  |  |     /*  | 
2904  |  |      * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is  | 
2905  |  |      * set, round the length of the 802.11 header to  | 
2906  |  |      * a multiple of 4.  Do that by adding 3 and then  | 
2907  |  |      * dividing by and multiplying by 4, which we do by  | 
2908  |  |      * ANDing with ~3.  | 
2909  |  |      */  | 
2910  | 94  |     s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
2911  | 94  |     s_roundup->s.k = cstate->off_linkpl.reg;  | 
2912  | 94  |     sappend(s, s_roundup);  | 
2913  | 94  |     s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);  | 
2914  | 94  |     s2->s.k = 3;  | 
2915  | 94  |     sappend(s, s2);  | 
2916  | 94  |     s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM);  | 
2917  | 94  |     s2->s.k = (bpf_u_int32)~3;  | 
2918  | 94  |     sappend(s, s2);  | 
2919  | 94  |     s2 = new_stmt(cstate, BPF_ST);  | 
2920  | 94  |     s2->s.k = cstate->off_linkpl.reg;  | 
2921  | 94  |     sappend(s, s2);  | 
2922  |  |  | 
2923  | 94  |     sjset_tsft_datapad->s.jt = s_roundup;  | 
2924  | 94  |     sjset_tsft_datapad->s.jf = snext;  | 
2925  | 94  |     sjset_notsft_datapad->s.jt = s_roundup;  | 
2926  | 94  |     sjset_notsft_datapad->s.jf = snext;  | 
2927  | 94  |   } else  | 
2928  | 164  |     sjset_qos->s.jf = snext;  | 
2929  |  |  | 
2930  | 258  |   return s;  | 
2931  | 779  | }  | 
2932  |  |  | 
2933  |  | static void  | 
2934  |  | insert_compute_vloffsets(compiler_state_t *cstate, struct block *b)  | 
2935  | 2.03k  | { | 
2936  | 2.03k  |   struct slist *s;  | 
2937  |  |  | 
2938  |  |   /* There is an implicit dependency between the link  | 
2939  |  |    * payload and link header since the payload computation  | 
2940  |  |    * includes the variable part of the header. Therefore,  | 
2941  |  |    * if nobody else has allocated a register for the link  | 
2942  |  |    * header and we need it, do it now. */  | 
2943  | 2.03k  |   if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable &&  | 
2944  | 2.03k  |       cstate->off_linkhdr.reg == -1)  | 
2945  | 0  |     cstate->off_linkhdr.reg = alloc_reg(cstate);  | 
2946  |  |  | 
2947  |  |   /*  | 
2948  |  |    * For link-layer types that have a variable-length header  | 
2949  |  |    * preceding the link-layer header, generate code to load  | 
2950  |  |    * the offset of the link-layer header into the register  | 
2951  |  |    * assigned to that offset, if any.  | 
2952  |  |    *  | 
2953  |  |    * XXX - this, and the next switch statement, won't handle  | 
2954  |  |    * encapsulation of 802.11 or 802.11+radio information in  | 
2955  |  |    * some other protocol stack.  That's significantly more  | 
2956  |  |    * complicated.  | 
2957  |  |    */  | 
2958  | 2.03k  |   switch (cstate->outermostlinktype) { | 
2959  |  |  | 
2960  | 257  |   case DLT_PRISM_HEADER:  | 
2961  | 257  |     s = gen_load_prism_llprefixlen(cstate);  | 
2962  | 257  |     break;  | 
2963  |  |  | 
2964  | 20  |   case DLT_IEEE802_11_RADIO_AVS:  | 
2965  | 20  |     s = gen_load_avs_llprefixlen(cstate);  | 
2966  | 20  |     break;  | 
2967  |  |  | 
2968  | 102  |   case DLT_IEEE802_11_RADIO:  | 
2969  | 102  |     s = gen_load_radiotap_llprefixlen(cstate);  | 
2970  | 102  |     break;  | 
2971  |  |  | 
2972  | 184  |   case DLT_PPI:  | 
2973  | 184  |     s = gen_load_ppi_llprefixlen(cstate);  | 
2974  | 184  |     break;  | 
2975  |  |  | 
2976  | 1.47k  |   default:  | 
2977  | 1.47k  |     s = NULL;  | 
2978  | 1.47k  |     break;  | 
2979  | 2.03k  |   }  | 
2980  |  |  | 
2981  |  |   /*  | 
2982  |  |    * For link-layer types that have a variable-length link-layer  | 
2983  |  |    * header, generate code to load the offset of the link-layer  | 
2984  |  |    * payload into the register assigned to that offset, if any.  | 
2985  |  |    */  | 
2986  | 2.03k  |   switch (cstate->outermostlinktype) { | 
2987  |  |  | 
2988  | 216  |   case DLT_IEEE802_11:  | 
2989  | 473  |   case DLT_PRISM_HEADER:  | 
2990  | 493  |   case DLT_IEEE802_11_RADIO_AVS:  | 
2991  | 595  |   case DLT_IEEE802_11_RADIO:  | 
2992  | 779  |   case DLT_PPI:  | 
2993  | 779  |     s = gen_load_802_11_header_len(cstate, s, b->stmts);  | 
2994  | 779  |     break;  | 
2995  |  |  | 
2996  | 71  |   case DLT_PFLOG:  | 
2997  | 71  |     s = gen_load_pflog_llprefixlen(cstate);  | 
2998  | 71  |     break;  | 
2999  | 2.03k  |   }  | 
3000  |  |  | 
3001  |  |   /*  | 
3002  |  |    * If there is no initialization yet and we need variable  | 
3003  |  |    * length offsets for VLAN, initialize them to zero  | 
3004  |  |    */  | 
3005  | 2.03k  |   if (s == NULL && cstate->is_vlan_vloffset) { | 
3006  | 0  |     struct slist *s2;  | 
3007  |  | 
  | 
3008  | 0  |     if (cstate->off_linkpl.reg == -1)  | 
3009  | 0  |       cstate->off_linkpl.reg = alloc_reg(cstate);  | 
3010  | 0  |     if (cstate->off_linktype.reg == -1)  | 
3011  | 0  |       cstate->off_linktype.reg = alloc_reg(cstate);  | 
3012  |  | 
  | 
3013  | 0  |     s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM);  | 
3014  | 0  |     s->s.k = 0;  | 
3015  | 0  |     s2 = new_stmt(cstate, BPF_ST);  | 
3016  | 0  |     s2->s.k = cstate->off_linkpl.reg;  | 
3017  | 0  |     sappend(s, s2);  | 
3018  | 0  |     s2 = new_stmt(cstate, BPF_ST);  | 
3019  | 0  |     s2->s.k = cstate->off_linktype.reg;  | 
3020  | 0  |     sappend(s, s2);  | 
3021  | 0  |   }  | 
3022  |  |  | 
3023  |  |   /*  | 
3024  |  |    * If we have any offset-loading code, append all the  | 
3025  |  |    * existing statements in the block to those statements,  | 
3026  |  |    * and make the resulting list the list of statements  | 
3027  |  |    * for the block.  | 
3028  |  |    */  | 
3029  | 2.03k  |   if (s != NULL) { | 
3030  | 328  |     sappend(s, b->stmts);  | 
3031  | 328  |     b->stmts = s;  | 
3032  | 328  |   }  | 
3033  | 2.03k  | }  | 
3034  |  |  | 
3035  |  | static struct block *  | 
3036  |  | gen_ppi_dlt_check(compiler_state_t *cstate)  | 
3037  | 2.03k  | { | 
3038  | 2.03k  |   struct slist *s_load_dlt;  | 
3039  | 2.03k  |   struct block *b;  | 
3040  |  |  | 
3041  | 2.03k  |   if (cstate->linktype == DLT_PPI)  | 
3042  | 184  |   { | 
3043  |  |     /* Create the statements that check for the DLT  | 
3044  |  |      */  | 
3045  | 184  |     s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS);  | 
3046  | 184  |     s_load_dlt->s.k = 4;  | 
3047  |  |  | 
3048  | 184  |     b = new_block(cstate, JMP(BPF_JEQ));  | 
3049  |  |  | 
3050  | 184  |     b->stmts = s_load_dlt;  | 
3051  | 184  |     b->s.k = SWAPLONG(DLT_IEEE802_11);  | 
3052  | 184  |   }  | 
3053  | 1.85k  |   else  | 
3054  | 1.85k  |   { | 
3055  | 1.85k  |     b = NULL;  | 
3056  | 1.85k  |   }  | 
3057  |  |  | 
3058  | 2.03k  |   return b;  | 
3059  | 2.03k  | }  | 
3060  |  |  | 
3061  |  | /*  | 
3062  |  |  * Take an absolute offset, and:  | 
3063  |  |  *  | 
3064  |  |  *    if it has no variable part, return NULL;  | 
3065  |  |  *  | 
3066  |  |  *    if it has a variable part, generate code to load the register  | 
3067  |  |  *    containing that variable part into the X register, returning  | 
3068  |  |  *    a pointer to that code - if no register for that offset has  | 
3069  |  |  *    been allocated, allocate it first.  | 
3070  |  |  *  | 
3071  |  |  * (The code to set that register will be generated later, but will  | 
3072  |  |  * be placed earlier in the code sequence.)  | 
3073  |  |  */  | 
3074  |  | static struct slist *  | 
3075  |  | gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off)  | 
3076  | 8.21k  | { | 
3077  | 8.21k  |   struct slist *s;  | 
3078  |  |  | 
3079  | 8.21k  |   if (off->is_variable) { | 
3080  | 1.99k  |     if (off->reg == -1) { | 
3081  |  |       /*  | 
3082  |  |        * We haven't yet assigned a register for the  | 
3083  |  |        * variable part of the offset of the link-layer  | 
3084  |  |        * header; allocate one.  | 
3085  |  |        */  | 
3086  | 490  |       off->reg = alloc_reg(cstate);  | 
3087  | 490  |     }  | 
3088  |  |  | 
3089  |  |     /*  | 
3090  |  |      * Load the register containing the variable part of the  | 
3091  |  |      * offset of the link-layer header into the X register.  | 
3092  |  |      */  | 
3093  | 1.99k  |     s = new_stmt(cstate, BPF_LDX|BPF_MEM);  | 
3094  | 1.99k  |     s->s.k = off->reg;  | 
3095  | 1.99k  |     return s;  | 
3096  | 6.22k  |   } else { | 
3097  |  |     /*  | 
3098  |  |      * That offset isn't variable, there's no variable part,  | 
3099  |  |      * so we don't need to generate any code.  | 
3100  |  |      */  | 
3101  | 6.22k  |     return NULL;  | 
3102  | 6.22k  |   }  | 
3103  | 8.21k  | }  | 
3104  |  |  | 
3105  |  | /*  | 
3106  |  |  * Map an Ethernet type to the equivalent PPP type.  | 
3107  |  |  */  | 
3108  |  | static bpf_u_int32  | 
3109  |  | ethertype_to_ppptype(bpf_u_int32 ll_proto)  | 
3110  | 172  | { | 
3111  | 172  |   switch (ll_proto) { | 
3112  |  |  | 
3113  | 11  |   case ETHERTYPE_IP:  | 
3114  | 11  |     ll_proto = PPP_IP;  | 
3115  | 11  |     break;  | 
3116  |  |  | 
3117  | 17  |   case ETHERTYPE_IPV6:  | 
3118  | 17  |     ll_proto = PPP_IPV6;  | 
3119  | 17  |     break;  | 
3120  |  |  | 
3121  | 0  |   case ETHERTYPE_DN:  | 
3122  | 0  |     ll_proto = PPP_DECNET;  | 
3123  | 0  |     break;  | 
3124  |  |  | 
3125  | 0  |   case ETHERTYPE_ATALK:  | 
3126  | 0  |     ll_proto = PPP_APPLE;  | 
3127  | 0  |     break;  | 
3128  |  |  | 
3129  | 0  |   case ETHERTYPE_NS:  | 
3130  | 0  |     ll_proto = PPP_NS;  | 
3131  | 0  |     break;  | 
3132  |  |  | 
3133  | 136  |   case LLCSAP_ISONS:  | 
3134  | 136  |     ll_proto = PPP_OSI;  | 
3135  | 136  |     break;  | 
3136  |  |  | 
3137  | 6  |   case LLCSAP_8021D:  | 
3138  |  |     /*  | 
3139  |  |      * I'm assuming the "Bridging PDU"s that go  | 
3140  |  |      * over PPP are Spanning Tree Protocol  | 
3141  |  |      * Bridging PDUs.  | 
3142  |  |      */  | 
3143  | 6  |     ll_proto = PPP_BRPDU;  | 
3144  | 6  |     break;  | 
3145  |  |  | 
3146  | 0  |   case LLCSAP_IPX:  | 
3147  | 0  |     ll_proto = PPP_IPX;  | 
3148  | 0  |     break;  | 
3149  | 172  |   }  | 
3150  | 172  |   return (ll_proto);  | 
3151  | 172  | }  | 
3152  |  |  | 
3153  |  | /*  | 
3154  |  |  * Generate any tests that, for encapsulation of a link-layer packet  | 
3155  |  |  * inside another protocol stack, need to be done to check for those  | 
3156  |  |  * link-layer packets (and that haven't already been done by a check  | 
3157  |  |  * for that encapsulation).  | 
3158  |  |  */  | 
3159  |  | static struct block *  | 
3160  |  | gen_prevlinkhdr_check(compiler_state_t *cstate)  | 
3161  | 177  | { | 
3162  | 177  |   struct block *b0;  | 
3163  |  |  | 
3164  | 177  |   if (cstate->is_geneve)  | 
3165  | 0  |     return gen_geneve_ll_check(cstate);  | 
3166  |  |  | 
3167  | 177  |   switch (cstate->prevlinktype) { | 
3168  |  |  | 
3169  | 0  |   case DLT_SUNATM:  | 
3170  |  |     /*  | 
3171  |  |      * This is LANE-encapsulated Ethernet; check that the LANE  | 
3172  |  |      * packet doesn't begin with an LE Control marker, i.e.  | 
3173  |  |      * that it's data, not a control message.  | 
3174  |  |      *  | 
3175  |  |      * (We've already generated a test for LANE.)  | 
3176  |  |      */  | 
3177  | 0  |     b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);  | 
3178  | 0  |     gen_not(b0);  | 
3179  | 0  |     return b0;  | 
3180  |  |  | 
3181  | 177  |   default:  | 
3182  |  |     /*  | 
3183  |  |      * No such tests are necessary.  | 
3184  |  |      */  | 
3185  | 177  |     return NULL;  | 
3186  | 177  |   }  | 
3187  |  |   /*NOTREACHED*/  | 
3188  | 177  | }  | 
3189  |  |  | 
3190  |  | /*  | 
3191  |  |  * The three different values we should check for when checking for an  | 
3192  |  |  * IPv6 packet with DLT_NULL.  | 
3193  |  |  */  | 
3194  | 46  | #define BSD_AFNUM_INET6_BSD 24  /* NetBSD, OpenBSD, BSD/OS, Npcap */  | 
3195  | 46  | #define BSD_AFNUM_INET6_FREEBSD 28  /* FreeBSD */  | 
3196  | 46  | #define BSD_AFNUM_INET6_DARWIN  30  /* macOS, iOS, other Darwin-based OSes */  | 
3197  |  |  | 
3198  |  | /*  | 
3199  |  |  * Generate code to match a particular packet type by matching the  | 
3200  |  |  * link-layer type field or fields in the 802.2 LLC header.  | 
3201  |  |  *  | 
3202  |  |  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP  | 
3203  |  |  * value, if <= ETHERMTU.  | 
3204  |  |  */  | 
3205  |  | static struct block *  | 
3206  |  | gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
3207  | 2.76k  | { | 
3208  | 2.76k  |   struct block *b0, *b1, *b2;  | 
3209  | 2.76k  |   const char *description;  | 
3210  |  |  | 
3211  |  |   /* are we checking MPLS-encapsulated packets? */  | 
3212  | 2.76k  |   if (cstate->label_stack_depth > 0)  | 
3213  | 0  |     return gen_mpls_linktype(cstate, ll_proto);  | 
3214  |  |  | 
3215  | 2.76k  |   switch (cstate->linktype) { | 
3216  |  |  | 
3217  | 104  |   case DLT_EN10MB:  | 
3218  | 144  |   case DLT_NETANALYZER:  | 
3219  | 177  |   case DLT_NETANALYZER_TRANSPARENT:  | 
3220  |  |     /* Geneve has an EtherType regardless of whether there is an  | 
3221  |  |      * L2 header. */  | 
3222  | 177  |     if (!cstate->is_geneve)  | 
3223  | 177  |       b0 = gen_prevlinkhdr_check(cstate);  | 
3224  | 0  |     else  | 
3225  | 0  |       b0 = NULL;  | 
3226  |  |  | 
3227  | 177  |     b1 = gen_ether_linktype(cstate, ll_proto);  | 
3228  | 177  |     if (b0 != NULL)  | 
3229  | 0  |       gen_and(b0, b1);  | 
3230  | 177  |     return b1;  | 
3231  |  |     /*NOTREACHED*/  | 
3232  |  |  | 
3233  | 36  |   case DLT_C_HDLC:  | 
3234  | 73  |   case DLT_HDLC:  | 
3235  | 73  |     switch (ll_proto) { | 
3236  |  |  | 
3237  | 3  |     case LLCSAP_ISONS:  | 
3238  | 3  |       ll_proto = (ll_proto << 8 | LLCSAP_ISONS);  | 
3239  |  |       /* fall through */  | 
3240  |  |  | 
3241  | 73  |     default:  | 
3242  | 73  |       return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
3243  |  |       /*NOTREACHED*/  | 
3244  | 73  |     }  | 
3245  |  |  | 
3246  | 153  |   case DLT_IEEE802_11:  | 
3247  | 202  |   case DLT_PRISM_HEADER:  | 
3248  | 235  |   case DLT_IEEE802_11_RADIO_AVS:  | 
3249  | 393  |   case DLT_IEEE802_11_RADIO:  | 
3250  | 443  |   case DLT_PPI:  | 
3251  |  |     /*  | 
3252  |  |      * Check that we have a data frame.  | 
3253  |  |      */  | 
3254  | 443  |     b0 = gen_check_802_11_data_frame(cstate);  | 
3255  |  |  | 
3256  |  |     /*  | 
3257  |  |      * Now check for the specified link-layer type.  | 
3258  |  |      */  | 
3259  | 443  |     b1 = gen_llc_linktype(cstate, ll_proto);  | 
3260  | 443  |     gen_and(b0, b1);  | 
3261  | 443  |     return b1;  | 
3262  |  |     /*NOTREACHED*/  | 
3263  |  |  | 
3264  | 29  |   case DLT_FDDI:  | 
3265  |  |     /*  | 
3266  |  |      * XXX - check for LLC frames.  | 
3267  |  |      */  | 
3268  | 29  |     return gen_llc_linktype(cstate, ll_proto);  | 
3269  |  |     /*NOTREACHED*/  | 
3270  |  |  | 
3271  | 40  |   case DLT_IEEE802:  | 
3272  |  |     /*  | 
3273  |  |      * XXX - check for LLC PDUs, as per IEEE 802.5.  | 
3274  |  |      */  | 
3275  | 40  |     return gen_llc_linktype(cstate, ll_proto);  | 
3276  |  |     /*NOTREACHED*/  | 
3277  |  |  | 
3278  | 47  |   case DLT_ATM_RFC1483:  | 
3279  | 76  |   case DLT_ATM_CLIP:  | 
3280  | 111  |   case DLT_IP_OVER_FC:  | 
3281  | 111  |     return gen_llc_linktype(cstate, ll_proto);  | 
3282  |  |     /*NOTREACHED*/  | 
3283  |  |  | 
3284  | 65  |   case DLT_SUNATM:  | 
3285  |  |     /*  | 
3286  |  |      * Check for an LLC-encapsulated version of this protocol;  | 
3287  |  |      * if we were checking for LANE, linktype would no longer  | 
3288  |  |      * be DLT_SUNATM.  | 
3289  |  |      *  | 
3290  |  |      * Check for LLC encapsulation and then check the protocol.  | 
3291  |  |      */  | 
3292  | 65  |     b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);  | 
3293  | 65  |     b1 = gen_llc_linktype(cstate, ll_proto);  | 
3294  | 65  |     gen_and(b0, b1);  | 
3295  | 65  |     return b1;  | 
3296  |  |     /*NOTREACHED*/  | 
3297  |  |  | 
3298  | 85  |   case DLT_LINUX_SLL:  | 
3299  | 85  |     return gen_linux_sll_linktype(cstate, ll_proto);  | 
3300  |  |     /*NOTREACHED*/  | 
3301  |  |  | 
3302  | 31  |   case DLT_SLIP:  | 
3303  | 72  |   case DLT_SLIP_BSDOS:  | 
3304  | 113  |   case DLT_RAW:  | 
3305  |  |     /*  | 
3306  |  |      * These types don't provide any type field; packets  | 
3307  |  |      * are always IPv4 or IPv6.  | 
3308  |  |      *  | 
3309  |  |      * XXX - for IPv4, check for a version number of 4, and,  | 
3310  |  |      * for IPv6, check for a version number of 6?  | 
3311  |  |      */  | 
3312  | 113  |     switch (ll_proto) { | 
3313  |  |  | 
3314  | 16  |     case ETHERTYPE_IP:  | 
3315  |  |       /* Check for a version number of 4. */  | 
3316  | 16  |       return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0);  | 
3317  |  |  | 
3318  | 16  |     case ETHERTYPE_IPV6:  | 
3319  |  |       /* Check for a version number of 6. */  | 
3320  | 16  |       return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0);  | 
3321  |  |  | 
3322  | 81  |     default:  | 
3323  | 81  |       return gen_false(cstate); /* always false */  | 
3324  | 113  |     }  | 
3325  |  |     /*NOTREACHED*/  | 
3326  |  |  | 
3327  | 46  |   case DLT_IPV4:  | 
3328  |  |     /*  | 
3329  |  |      * Raw IPv4, so no type field.  | 
3330  |  |      */  | 
3331  | 46  |     if (ll_proto == ETHERTYPE_IP)  | 
3332  | 9  |       return gen_true(cstate);  /* always true */  | 
3333  |  |  | 
3334  |  |     /* Checking for something other than IPv4; always false */  | 
3335  | 37  |     return gen_false(cstate);  | 
3336  |  |     /*NOTREACHED*/  | 
3337  |  |  | 
3338  | 40  |   case DLT_IPV6:  | 
3339  |  |     /*  | 
3340  |  |      * Raw IPv6, so no type field.  | 
3341  |  |      */  | 
3342  | 40  |     if (ll_proto == ETHERTYPE_IPV6)  | 
3343  | 6  |       return gen_true(cstate);  /* always true */  | 
3344  |  |  | 
3345  |  |     /* Checking for something other than IPv6; always false */  | 
3346  | 34  |     return gen_false(cstate);  | 
3347  |  |     /*NOTREACHED*/  | 
3348  |  |  | 
3349  | 35  |   case DLT_PPP:  | 
3350  | 62  |   case DLT_PPP_PPPD:  | 
3351  | 99  |   case DLT_PPP_SERIAL:  | 
3352  | 128  |   case DLT_PPP_ETHER:  | 
3353  |  |     /*  | 
3354  |  |      * We use Ethernet protocol types inside libpcap;  | 
3355  |  |      * map them to the corresponding PPP protocol types.  | 
3356  |  |      */  | 
3357  | 128  |     return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,  | 
3358  | 128  |         ethertype_to_ppptype(ll_proto));  | 
3359  |  |     /*NOTREACHED*/  | 
3360  |  |  | 
3361  | 50  |   case DLT_PPP_BSDOS:  | 
3362  |  |     /*  | 
3363  |  |      * We use Ethernet protocol types inside libpcap;  | 
3364  |  |      * map them to the corresponding PPP protocol types.  | 
3365  |  |      */  | 
3366  | 50  |     switch (ll_proto) { | 
3367  |  |  | 
3368  | 6  |     case ETHERTYPE_IP:  | 
3369  |  |       /*  | 
3370  |  |        * Also check for Van Jacobson-compressed IP.  | 
3371  |  |        * XXX - do this for other forms of PPP?  | 
3372  |  |        */  | 
3373  | 6  |       b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP);  | 
3374  | 6  |       b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC);  | 
3375  | 6  |       gen_or(b0, b1);  | 
3376  | 6  |       b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC);  | 
3377  | 6  |       gen_or(b1, b0);  | 
3378  | 6  |       return b0;  | 
3379  |  |  | 
3380  | 44  |     default:  | 
3381  | 44  |       return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H,  | 
3382  | 44  |           ethertype_to_ppptype(ll_proto));  | 
3383  | 50  |     }  | 
3384  |  |     /*NOTREACHED*/  | 
3385  |  |  | 
3386  | 121  |   case DLT_NULL:  | 
3387  | 159  |   case DLT_LOOP:  | 
3388  | 193  |   case DLT_ENC:  | 
3389  | 193  |     switch (ll_proto) { | 
3390  |  |  | 
3391  | 49  |     case ETHERTYPE_IP:  | 
3392  | 49  |       return (gen_loopback_linktype(cstate, AF_INET));  | 
3393  |  |  | 
3394  | 46  |     case ETHERTYPE_IPV6:  | 
3395  |  |       /*  | 
3396  |  |        * AF_ values may, unfortunately, be platform-  | 
3397  |  |        * dependent; AF_INET isn't, because everybody  | 
3398  |  |        * used 4.2BSD's value, but AF_INET6 is, because  | 
3399  |  |        * 4.2BSD didn't have a value for it (given that  | 
3400  |  |        * IPv6 didn't exist back in the early 1980's),  | 
3401  |  |        * and they all picked their own values.  | 
3402  |  |        *  | 
3403  |  |        * This means that, if we're reading from a  | 
3404  |  |        * savefile, we need to check for all the  | 
3405  |  |        * possible values.  | 
3406  |  |        *  | 
3407  |  |        * If we're doing a live capture, we only need  | 
3408  |  |        * to check for this platform's value; however,  | 
3409  |  |        * Npcap uses 24, which isn't Windows's AF_INET6  | 
3410  |  |        * value.  (Given the multiple different values,  | 
3411  |  |        * programs that read pcap files shouldn't be  | 
3412  |  |        * checking for their platform's AF_INET6 value  | 
3413  |  |        * anyway, they should check for all of the  | 
3414  |  |        * possible values. and they might as well do  | 
3415  |  |        * that even for live captures.)  | 
3416  |  |        */  | 
3417  | 46  |       if (cstate->bpf_pcap->rfile != NULL) { | 
3418  |  |         /*  | 
3419  |  |          * Savefile - check for all three  | 
3420  |  |          * possible IPv6 values.  | 
3421  |  |          */  | 
3422  | 46  |         b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD);  | 
3423  | 46  |         b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD);  | 
3424  | 46  |         gen_or(b0, b1);  | 
3425  | 46  |         b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN);  | 
3426  | 46  |         gen_or(b0, b1);  | 
3427  | 46  |         return (b1);  | 
3428  | 46  |       } else { | 
3429  |  |         /*  | 
3430  |  |          * Live capture, so we only need to  | 
3431  |  |          * check for the value used on this  | 
3432  |  |          * platform.  | 
3433  |  |          */  | 
3434  |  | #ifdef _WIN32  | 
3435  |  |         /*  | 
3436  |  |          * Npcap doesn't use Windows's AF_INET6,  | 
3437  |  |          * as that collides with AF_IPX on  | 
3438  |  |          * some BSDs (both have the value 23).  | 
3439  |  |          * Instead, it uses 24.  | 
3440  |  |          */  | 
3441  |  |         return (gen_loopback_linktype(cstate, 24));  | 
3442  |  | #else /* _WIN32 */  | 
3443  | 0  | #ifdef AF_INET6  | 
3444  | 0  |         return (gen_loopback_linktype(cstate, AF_INET6));  | 
3445  |  | #else /* AF_INET6 */  | 
3446  |  |         /*  | 
3447  |  |          * I guess this platform doesn't support  | 
3448  |  |          * IPv6, so we just reject all packets.  | 
3449  |  |          */  | 
3450  |  |         return gen_false(cstate);  | 
3451  |  | #endif /* AF_INET6 */  | 
3452  | 0  | #endif /* _WIN32 */  | 
3453  | 0  |       }  | 
3454  |  |  | 
3455  | 98  |     default:  | 
3456  |  |       /*  | 
3457  |  |        * Not a type on which we support filtering.  | 
3458  |  |        * XXX - support those that have AF_ values  | 
3459  |  |        * #defined on this platform, at least?  | 
3460  |  |        */  | 
3461  | 98  |       return gen_false(cstate);  | 
3462  | 193  |     }  | 
3463  |  |  | 
3464  | 129  |   case DLT_PFLOG:  | 
3465  |  |     /*  | 
3466  |  |      * af field is host byte order in contrast to the rest of  | 
3467  |  |      * the packet.  | 
3468  |  |      */  | 
3469  | 129  |     if (ll_proto == ETHERTYPE_IP)  | 
3470  | 45  |       return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),  | 
3471  | 45  |           BPF_B, AF_INET));  | 
3472  | 84  |     else if (ll_proto == ETHERTYPE_IPV6)  | 
3473  | 45  |       return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af),  | 
3474  | 45  |           BPF_B, AF_INET6));  | 
3475  | 39  |     else  | 
3476  | 39  |       return gen_false(cstate);  | 
3477  |  |     /*NOTREACHED*/  | 
3478  |  |  | 
3479  | 37  |   case DLT_ARCNET:  | 
3480  | 75  |   case DLT_ARCNET_LINUX:  | 
3481  |  |     /*  | 
3482  |  |      * XXX should we check for first fragment if the protocol  | 
3483  |  |      * uses PHDS?  | 
3484  |  |      */  | 
3485  | 75  |     switch (ll_proto) { | 
3486  |  |  | 
3487  | 61  |     default:  | 
3488  | 61  |       return gen_false(cstate);  | 
3489  |  |  | 
3490  | 7  |     case ETHERTYPE_IPV6:  | 
3491  | 7  |       return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3492  | 7  |         ARCTYPE_INET6));  | 
3493  |  |  | 
3494  | 7  |     case ETHERTYPE_IP:  | 
3495  | 7  |       b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3496  | 7  |           ARCTYPE_IP);  | 
3497  | 7  |       b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3498  | 7  |           ARCTYPE_IP_OLD);  | 
3499  | 7  |       gen_or(b0, b1);  | 
3500  | 7  |       return (b1);  | 
3501  |  |  | 
3502  | 0  |     case ETHERTYPE_ARP:  | 
3503  | 0  |       b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3504  | 0  |           ARCTYPE_ARP);  | 
3505  | 0  |       b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3506  | 0  |           ARCTYPE_ARP_OLD);  | 
3507  | 0  |       gen_or(b0, b1);  | 
3508  | 0  |       return (b1);  | 
3509  |  |  | 
3510  | 0  |     case ETHERTYPE_REVARP:  | 
3511  | 0  |       return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3512  | 0  |           ARCTYPE_REVARP));  | 
3513  |  |  | 
3514  | 0  |     case ETHERTYPE_ATALK:  | 
3515  | 0  |       return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B,  | 
3516  | 0  |           ARCTYPE_ATALK));  | 
3517  | 75  |     }  | 
3518  |  |     /*NOTREACHED*/  | 
3519  |  |  | 
3520  | 35  |   case DLT_LTALK:  | 
3521  | 35  |     switch (ll_proto) { | 
3522  | 0  |     case ETHERTYPE_ATALK:  | 
3523  | 0  |       return gen_true(cstate);  | 
3524  | 35  |     default:  | 
3525  | 35  |       return gen_false(cstate);  | 
3526  | 35  |     }  | 
3527  |  |     /*NOTREACHED*/  | 
3528  |  |  | 
3529  | 14  |   case DLT_FRELAY:  | 
3530  |  |     /*  | 
3531  |  |      * XXX - assumes a 2-byte Frame Relay header with  | 
3532  |  |      * DLCI and flags.  What if the address is longer?  | 
3533  |  |      */  | 
3534  | 14  |     switch (ll_proto) { | 
3535  |  |  | 
3536  | 5  |     case ETHERTYPE_IP:  | 
3537  |  |       /*  | 
3538  |  |        * Check for the special NLPID for IP.  | 
3539  |  |        */  | 
3540  | 5  |       return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc);  | 
3541  |  |  | 
3542  | 5  |     case ETHERTYPE_IPV6:  | 
3543  |  |       /*  | 
3544  |  |        * Check for the special NLPID for IPv6.  | 
3545  |  |        */  | 
3546  | 5  |       return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e);  | 
3547  |  |  | 
3548  | 1  |     case LLCSAP_ISONS:  | 
3549  |  |       /*  | 
3550  |  |        * Check for several OSI protocols.  | 
3551  |  |        *  | 
3552  |  |        * Frame Relay packets typically have an OSI  | 
3553  |  |        * NLPID at the beginning; we check for each  | 
3554  |  |        * of them.  | 
3555  |  |        *  | 
3556  |  |        * What we check for is the NLPID and a frame  | 
3557  |  |        * control field of UI, i.e. 0x03 followed  | 
3558  |  |        * by the NLPID.  | 
3559  |  |        */  | 
3560  | 1  |       b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);  | 
3561  | 1  |       b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);  | 
3562  | 1  |       b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);  | 
3563  | 1  |       gen_or(b1, b2);  | 
3564  | 1  |       gen_or(b0, b2);  | 
3565  | 1  |       return b2;  | 
3566  |  |  | 
3567  | 3  |     default:  | 
3568  | 3  |       return gen_false(cstate);  | 
3569  | 14  |     }  | 
3570  |  |     /*NOTREACHED*/  | 
3571  |  |  | 
3572  | 4  |   case DLT_MFR:  | 
3573  | 4  |     bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented");  | 
3574  |  |  | 
3575  | 29  |         case DLT_JUNIPER_MFR:  | 
3576  | 59  |         case DLT_JUNIPER_MLFR:  | 
3577  | 93  |         case DLT_JUNIPER_MLPPP:  | 
3578  | 122  |   case DLT_JUNIPER_ATM1:  | 
3579  | 153  |   case DLT_JUNIPER_ATM2:  | 
3580  | 182  |   case DLT_JUNIPER_PPPOE:  | 
3581  | 207  |   case DLT_JUNIPER_PPPOE_ATM:  | 
3582  | 250  |         case DLT_JUNIPER_GGSN:  | 
3583  | 283  |         case DLT_JUNIPER_ES:  | 
3584  | 321  |         case DLT_JUNIPER_MONITOR:  | 
3585  | 351  |         case DLT_JUNIPER_SERVICES:  | 
3586  | 380  |         case DLT_JUNIPER_ETHER:  | 
3587  | 409  |         case DLT_JUNIPER_PPP:  | 
3588  | 438  |         case DLT_JUNIPER_FRELAY:  | 
3589  | 467  |         case DLT_JUNIPER_CHDLC:  | 
3590  | 491  |         case DLT_JUNIPER_VP:  | 
3591  | 515  |         case DLT_JUNIPER_ST:  | 
3592  | 540  |         case DLT_JUNIPER_ISM:  | 
3593  | 571  |         case DLT_JUNIPER_VS:  | 
3594  | 604  |         case DLT_JUNIPER_SRX_E2E:  | 
3595  | 640  |         case DLT_JUNIPER_FIBRECHANNEL:  | 
3596  | 669  |   case DLT_JUNIPER_ATM_CEMIC:  | 
3597  |  |  | 
3598  |  |     /* just lets verify the magic number for now -  | 
3599  |  |      * on ATM we may have up to 6 different encapsulations on the wire  | 
3600  |  |      * and need a lot of heuristics to figure out that the payload  | 
3601  |  |      * might be;  | 
3602  |  |      *  | 
3603  |  |      * FIXME encapsulation specific BPF_ filters  | 
3604  |  |      */  | 
3605  | 669  |     return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */  | 
3606  |  |  | 
3607  | 35  |   case DLT_BACNET_MS_TP:  | 
3608  | 35  |     return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000);  | 
3609  |  |  | 
3610  | 37  |   case DLT_IPNET:  | 
3611  | 37  |     return gen_ipnet_linktype(cstate, ll_proto);  | 
3612  |  |  | 
3613  | 4  |   case DLT_LINUX_IRDA:  | 
3614  | 4  |     bpf_error(cstate, "IrDA link-layer type filtering not implemented");  | 
3615  |  |  | 
3616  | 4  |   case DLT_DOCSIS:  | 
3617  | 4  |     bpf_error(cstate, "DOCSIS link-layer type filtering not implemented");  | 
3618  |  |  | 
3619  | 5  |   case DLT_MTP2:  | 
3620  | 9  |   case DLT_MTP2_WITH_PHDR:  | 
3621  | 9  |     bpf_error(cstate, "MTP2 link-layer type filtering not implemented");  | 
3622  |  |  | 
3623  | 4  |   case DLT_ERF:  | 
3624  | 4  |     bpf_error(cstate, "ERF link-layer type filtering not implemented");  | 
3625  |  |  | 
3626  | 4  |   case DLT_PFSYNC:  | 
3627  | 4  |     bpf_error(cstate, "PFSYNC link-layer type filtering not implemented");  | 
3628  |  |  | 
3629  | 4  |   case DLT_LINUX_LAPD:  | 
3630  | 4  |     bpf_error(cstate, "LAPD link-layer type filtering not implemented");  | 
3631  |  |  | 
3632  | 4  |   case DLT_USB_FREEBSD:  | 
3633  | 8  |   case DLT_USB_LINUX:  | 
3634  | 12  |   case DLT_USB_LINUX_MMAPPED:  | 
3635  | 16  |   case DLT_USBPCAP:  | 
3636  | 16  |     bpf_error(cstate, "USB link-layer type filtering not implemented");  | 
3637  |  |  | 
3638  | 4  |   case DLT_BLUETOOTH_HCI_H4:  | 
3639  | 8  |   case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:  | 
3640  | 8  |     bpf_error(cstate, "Bluetooth link-layer type filtering not implemented");  | 
3641  |  |  | 
3642  | 4  |   case DLT_CAN20B:  | 
3643  | 8  |   case DLT_CAN_SOCKETCAN:  | 
3644  | 8  |     bpf_error(cstate, "CAN link-layer type filtering not implemented");  | 
3645  |  |  | 
3646  | 4  |   case DLT_IEEE802_15_4:  | 
3647  | 8  |   case DLT_IEEE802_15_4_LINUX:  | 
3648  | 12  |   case DLT_IEEE802_15_4_NONASK_PHY:  | 
3649  | 16  |   case DLT_IEEE802_15_4_NOFCS:  | 
3650  | 20  |   case DLT_IEEE802_15_4_TAP:  | 
3651  | 20  |     bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented");  | 
3652  |  |  | 
3653  | 4  |   case DLT_IEEE802_16_MAC_CPS_RADIO:  | 
3654  | 4  |     bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented");  | 
3655  |  |  | 
3656  | 4  |   case DLT_SITA:  | 
3657  | 4  |     bpf_error(cstate, "SITA link-layer type filtering not implemented");  | 
3658  |  |  | 
3659  | 4  |   case DLT_RAIF1:  | 
3660  | 4  |     bpf_error(cstate, "RAIF1 link-layer type filtering not implemented");  | 
3661  |  |  | 
3662  | 4  |   case DLT_IPMB_KONTRON:  | 
3663  | 8  |   case DLT_IPMB_LINUX:  | 
3664  | 8  |     bpf_error(cstate, "IPMB link-layer type filtering not implemented");  | 
3665  |  |  | 
3666  | 4  |   case DLT_AX25_KISS:  | 
3667  | 4  |     bpf_error(cstate, "AX.25 link-layer type filtering not implemented");  | 
3668  |  |  | 
3669  | 4  |   case DLT_NFLOG:  | 
3670  |  |     /* Using the fixed-size NFLOG header it is possible to tell only  | 
3671  |  |      * the address family of the packet, other meaningful data is  | 
3672  |  |      * either missing or behind TLVs.  | 
3673  |  |      */  | 
3674  | 4  |     bpf_error(cstate, "NFLOG link-layer type filtering not implemented");  | 
3675  |  |  | 
3676  | 67  |   default:  | 
3677  |  |     /*  | 
3678  |  |      * Does this link-layer header type have a field  | 
3679  |  |      * indicating the type of the next protocol?  If  | 
3680  |  |      * so, off_linktype.constant_part will be the offset of that  | 
3681  |  |      * field in the packet; if not, it will be OFFSET_NOT_SET.  | 
3682  |  |      */  | 
3683  | 67  |     if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) { | 
3684  |  |       /*  | 
3685  |  |        * Yes; assume it's an Ethernet type.  (If  | 
3686  |  |        * it's not, it needs to be handled specially  | 
3687  |  |        * above.)  | 
3688  |  |        */  | 
3689  | 45  |       return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto);  | 
3690  |  |       /*NOTREACHED */  | 
3691  | 45  |     } else { | 
3692  |  |       /*  | 
3693  |  |        * No; report an error.  | 
3694  |  |        */  | 
3695  | 22  |       description = pcap_datalink_val_to_description_or_dlt(cstate->linktype);  | 
3696  | 22  |       bpf_error(cstate, "%s link-layer type filtering not implemented",  | 
3697  | 22  |           description);  | 
3698  |  |       /*NOTREACHED */  | 
3699  | 22  |     }  | 
3700  | 2.76k  |   }  | 
3701  | 2.76k  | }  | 
3702  |  |  | 
3703  |  | /*  | 
3704  |  |  * Check for an LLC SNAP packet with a given organization code and  | 
3705  |  |  * protocol type; we check the entire contents of the 802.2 LLC and  | 
3706  |  |  * snap headers, checking for DSAP and SSAP of SNAP and a control  | 
3707  |  |  * field of 0x03 in the LLC header, and for the specified organization  | 
3708  |  |  * code and protocol type in the SNAP header.  | 
3709  |  |  */  | 
3710  |  | static struct block *  | 
3711  |  | gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype)  | 
3712  | 0  | { | 
3713  | 0  |   u_char snapblock[8];  | 
3714  |  | 
  | 
3715  | 0  |   snapblock[0] = LLCSAP_SNAP;   /* DSAP = SNAP */  | 
3716  | 0  |   snapblock[1] = LLCSAP_SNAP;   /* SSAP = SNAP */  | 
3717  | 0  |   snapblock[2] = 0x03;      /* control = UI */  | 
3718  | 0  |   snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */  | 
3719  | 0  |   snapblock[4] = (u_char)(orgcode >> 8);  /* middle 8 bits of organization code */  | 
3720  | 0  |   snapblock[5] = (u_char)(orgcode >> 0);  /* lower 8 bits of organization code */  | 
3721  | 0  |   snapblock[6] = (u_char)(ptype >> 8);  /* upper 8 bits of protocol type */  | 
3722  | 0  |   snapblock[7] = (u_char)(ptype >> 0);  /* lower 8 bits of protocol type */  | 
3723  | 0  |   return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock);  | 
3724  | 0  | }  | 
3725  |  |  | 
3726  |  | /*  | 
3727  |  |  * Generate code to match frames with an LLC header.  | 
3728  |  |  */  | 
3729  |  | static struct block *  | 
3730  |  | gen_llc_internal(compiler_state_t *cstate)  | 
3731  | 19  | { | 
3732  | 19  |   struct block *b0, *b1;  | 
3733  |  |  | 
3734  | 19  |   switch (cstate->linktype) { | 
3735  |  |  | 
3736  | 1  |   case DLT_EN10MB:  | 
3737  |  |     /*  | 
3738  |  |      * We check for an Ethernet type field less than  | 
3739  |  |      * 1500, which means it's an 802.3 length field.  | 
3740  |  |      */  | 
3741  | 1  |     b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU);  | 
3742  | 1  |     gen_not(b0);  | 
3743  |  |  | 
3744  |  |     /*  | 
3745  |  |      * Now check for the purported DSAP and SSAP not being  | 
3746  |  |      * 0xFF, to rule out NetWare-over-802.3.  | 
3747  |  |      */  | 
3748  | 1  |     b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF);  | 
3749  | 1  |     gen_not(b1);  | 
3750  | 1  |     gen_and(b0, b1);  | 
3751  | 1  |     return b1;  | 
3752  |  |  | 
3753  | 1  |   case DLT_SUNATM:  | 
3754  |  |     /*  | 
3755  |  |      * We check for LLC traffic.  | 
3756  |  |      */  | 
3757  | 1  |     b0 = gen_atmtype_llc(cstate);  | 
3758  | 1  |     return b0;  | 
3759  |  |  | 
3760  | 1  |   case DLT_IEEE802: /* Token Ring */  | 
3761  |  |     /*  | 
3762  |  |      * XXX - check for LLC frames.  | 
3763  |  |      */  | 
3764  | 1  |     return gen_true(cstate);  | 
3765  |  |  | 
3766  | 1  |   case DLT_FDDI:  | 
3767  |  |     /*  | 
3768  |  |      * XXX - check for LLC frames.  | 
3769  |  |      */  | 
3770  | 1  |     return gen_true(cstate);  | 
3771  |  |  | 
3772  | 1  |   case DLT_ATM_RFC1483:  | 
3773  |  |     /*  | 
3774  |  |      * For LLC encapsulation, these are defined to have an  | 
3775  |  |      * 802.2 LLC header.  | 
3776  |  |      *  | 
3777  |  |      * For VC encapsulation, they don't, but there's no  | 
3778  |  |      * way to check for that; the protocol used on the VC  | 
3779  |  |      * is negotiated out of band.  | 
3780  |  |      */  | 
3781  | 1  |     return gen_true(cstate);  | 
3782  |  |  | 
3783  | 2  |   case DLT_IEEE802_11:  | 
3784  | 3  |   case DLT_PRISM_HEADER:  | 
3785  | 5  |   case DLT_IEEE802_11_RADIO:  | 
3786  | 6  |   case DLT_IEEE802_11_RADIO_AVS:  | 
3787  | 9  |   case DLT_PPI:  | 
3788  |  |     /*  | 
3789  |  |      * Check that we have a data frame.  | 
3790  |  |      */  | 
3791  | 9  |     b0 = gen_check_802_11_data_frame(cstate);  | 
3792  | 9  |     return b0;  | 
3793  |  |  | 
3794  | 5  |   default:  | 
3795  | 5  |     bpf_error(cstate, "'llc' not supported for %s",  | 
3796  | 5  |         pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
3797  |  |     /*NOTREACHED*/  | 
3798  | 19  |   }  | 
3799  | 19  | }  | 
3800  |  |  | 
3801  |  | struct block *  | 
3802  |  | gen_llc(compiler_state_t *cstate)  | 
3803  | 19  | { | 
3804  |  |   /*  | 
3805  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3806  |  |    * on an error.  | 
3807  |  |    */  | 
3808  | 19  |   if (setjmp(cstate->top_ctx))  | 
3809  | 5  |     return (NULL);  | 
3810  |  |  | 
3811  | 14  |   return gen_llc_internal(cstate);  | 
3812  | 19  | }  | 
3813  |  |  | 
3814  |  | struct block *  | 
3815  |  | gen_llc_i(compiler_state_t *cstate)  | 
3816  | 0  | { | 
3817  | 0  |   struct block *b0, *b1;  | 
3818  | 0  |   struct slist *s;  | 
3819  |  |  | 
3820  |  |   /*  | 
3821  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3822  |  |    * on an error.  | 
3823  |  |    */  | 
3824  | 0  |   if (setjmp(cstate->top_ctx))  | 
3825  | 0  |     return (NULL);  | 
3826  |  |  | 
3827  |  |   /*  | 
3828  |  |    * Check whether this is an LLC frame.  | 
3829  |  |    */  | 
3830  | 0  |   b0 = gen_llc_internal(cstate);  | 
3831  |  |  | 
3832  |  |   /*  | 
3833  |  |    * Load the control byte and test the low-order bit; it must  | 
3834  |  |    * be clear for I frames.  | 
3835  |  |    */  | 
3836  | 0  |   s = gen_load_a(cstate, OR_LLC, 2, BPF_B);  | 
3837  | 0  |   b1 = new_block(cstate, JMP(BPF_JSET));  | 
3838  | 0  |   b1->s.k = 0x01;  | 
3839  | 0  |   b1->stmts = s;  | 
3840  | 0  |   gen_not(b1);  | 
3841  | 0  |   gen_and(b0, b1);  | 
3842  | 0  |   return b1;  | 
3843  | 0  | }  | 
3844  |  |  | 
3845  |  | struct block *  | 
3846  |  | gen_llc_s(compiler_state_t *cstate)  | 
3847  | 0  | { | 
3848  | 0  |   struct block *b0, *b1;  | 
3849  |  |  | 
3850  |  |   /*  | 
3851  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3852  |  |    * on an error.  | 
3853  |  |    */  | 
3854  | 0  |   if (setjmp(cstate->top_ctx))  | 
3855  | 0  |     return (NULL);  | 
3856  |  |  | 
3857  |  |   /*  | 
3858  |  |    * Check whether this is an LLC frame.  | 
3859  |  |    */  | 
3860  | 0  |   b0 = gen_llc_internal(cstate);  | 
3861  |  |  | 
3862  |  |   /*  | 
3863  |  |    * Now compare the low-order 2 bit of the control byte against  | 
3864  |  |    * the appropriate value for S frames.  | 
3865  |  |    */  | 
3866  | 0  |   b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03);  | 
3867  | 0  |   gen_and(b0, b1);  | 
3868  | 0  |   return b1;  | 
3869  | 0  | }  | 
3870  |  |  | 
3871  |  | struct block *  | 
3872  |  | gen_llc_u(compiler_state_t *cstate)  | 
3873  | 0  | { | 
3874  | 0  |   struct block *b0, *b1;  | 
3875  |  |  | 
3876  |  |   /*  | 
3877  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3878  |  |    * on an error.  | 
3879  |  |    */  | 
3880  | 0  |   if (setjmp(cstate->top_ctx))  | 
3881  | 0  |     return (NULL);  | 
3882  |  |  | 
3883  |  |   /*  | 
3884  |  |    * Check whether this is an LLC frame.  | 
3885  |  |    */  | 
3886  | 0  |   b0 = gen_llc_internal(cstate);  | 
3887  |  |  | 
3888  |  |   /*  | 
3889  |  |    * Now compare the low-order 2 bit of the control byte against  | 
3890  |  |    * the appropriate value for U frames.  | 
3891  |  |    */  | 
3892  | 0  |   b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03);  | 
3893  | 0  |   gen_and(b0, b1);  | 
3894  | 0  |   return b1;  | 
3895  | 0  | }  | 
3896  |  |  | 
3897  |  | struct block *  | 
3898  |  | gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)  | 
3899  | 0  | { | 
3900  | 0  |   struct block *b0, *b1;  | 
3901  |  |  | 
3902  |  |   /*  | 
3903  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3904  |  |    * on an error.  | 
3905  |  |    */  | 
3906  | 0  |   if (setjmp(cstate->top_ctx))  | 
3907  | 0  |     return (NULL);  | 
3908  |  |  | 
3909  |  |   /*  | 
3910  |  |    * Check whether this is an LLC frame.  | 
3911  |  |    */  | 
3912  | 0  |   b0 = gen_llc_internal(cstate);  | 
3913  |  |  | 
3914  |  |   /*  | 
3915  |  |    * Now check for an S frame with the appropriate type.  | 
3916  |  |    */  | 
3917  | 0  |   b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK);  | 
3918  | 0  |   gen_and(b0, b1);  | 
3919  | 0  |   return b1;  | 
3920  | 0  | }  | 
3921  |  |  | 
3922  |  | struct block *  | 
3923  |  | gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype)  | 
3924  | 0  | { | 
3925  | 0  |   struct block *b0, *b1;  | 
3926  |  |  | 
3927  |  |   /*  | 
3928  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
3929  |  |    * on an error.  | 
3930  |  |    */  | 
3931  | 0  |   if (setjmp(cstate->top_ctx))  | 
3932  | 0  |     return (NULL);  | 
3933  |  |  | 
3934  |  |   /*  | 
3935  |  |    * Check whether this is an LLC frame.  | 
3936  |  |    */  | 
3937  | 0  |   b0 = gen_llc_internal(cstate);  | 
3938  |  |  | 
3939  |  |   /*  | 
3940  |  |    * Now check for a U frame with the appropriate type.  | 
3941  |  |    */  | 
3942  | 0  |   b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK);  | 
3943  | 0  |   gen_and(b0, b1);  | 
3944  | 0  |   return b1;  | 
3945  | 0  | }  | 
3946  |  |  | 
3947  |  | /*  | 
3948  |  |  * Generate code to match a particular packet type, for link-layer types  | 
3949  |  |  * using 802.2 LLC headers.  | 
3950  |  |  *  | 
3951  |  |  * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used  | 
3952  |  |  * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.  | 
3953  |  |  *  | 
3954  |  |  * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP  | 
3955  |  |  * value, if <= ETHERMTU.  We use that to determine whether to  | 
3956  |  |  * match the DSAP or both DSAP and LSAP or to check the OUI and  | 
3957  |  |  * protocol ID in a SNAP header.  | 
3958  |  |  */  | 
3959  |  | static struct block *  | 
3960  |  | gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
3961  | 688  | { | 
3962  |  |   /*  | 
3963  |  |    * XXX - handle token-ring variable-length header.  | 
3964  |  |    */  | 
3965  | 688  |   switch (ll_proto) { | 
3966  |  |  | 
3967  | 0  |   case LLCSAP_IP:  | 
3968  | 581  |   case LLCSAP_ISONS:  | 
3969  | 581  |   case LLCSAP_NETBEUI:  | 
3970  |  |     /*  | 
3971  |  |      * XXX - should we check both the DSAP and the  | 
3972  |  |      * SSAP, like this, or should we check just the  | 
3973  |  |      * DSAP, as we do for other SAP values?  | 
3974  |  |      */  | 
3975  | 581  |     return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32)  | 
3976  | 581  |            ((ll_proto << 8) | ll_proto));  | 
3977  |  |  | 
3978  | 0  |   case LLCSAP_IPX:  | 
3979  |  |     /*  | 
3980  |  |      * XXX - are there ever SNAP frames for IPX on  | 
3981  |  |      * non-Ethernet 802.x networks?  | 
3982  |  |      */  | 
3983  | 0  |     return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX);  | 
3984  |  |  | 
3985  | 0  |   case ETHERTYPE_ATALK:  | 
3986  |  |     /*  | 
3987  |  |      * 802.2-encapsulated ETHERTYPE_ATALK packets are  | 
3988  |  |      * SNAP packets with an organization code of  | 
3989  |  |      * 0x080007 (Apple, for Appletalk) and a protocol  | 
3990  |  |      * type of ETHERTYPE_ATALK (Appletalk).  | 
3991  |  |      *  | 
3992  |  |      * XXX - check for an organization code of  | 
3993  |  |      * encapsulated Ethernet as well?  | 
3994  |  |      */  | 
3995  | 0  |     return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK);  | 
3996  |  |  | 
3997  | 107  |   default:  | 
3998  |  |     /*  | 
3999  |  |      * XXX - we don't have to check for IPX 802.3  | 
4000  |  |      * here, but should we check for the IPX Ethertype?  | 
4001  |  |      */  | 
4002  | 107  |     if (ll_proto <= ETHERMTU) { | 
4003  |  |       /*  | 
4004  |  |        * This is an LLC SAP value, so check  | 
4005  |  |        * the DSAP.  | 
4006  |  |        */  | 
4007  | 8  |       return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto);  | 
4008  | 99  |     } else { | 
4009  |  |       /*  | 
4010  |  |        * This is an Ethernet type; we assume that it's  | 
4011  |  |        * unlikely that it'll appear in the right place  | 
4012  |  |        * at random, and therefore check only the  | 
4013  |  |        * location that would hold the Ethernet type  | 
4014  |  |        * in a SNAP frame with an organization code of  | 
4015  |  |        * 0x000000 (encapsulated Ethernet).  | 
4016  |  |        *  | 
4017  |  |        * XXX - if we were to check for the SNAP DSAP and  | 
4018  |  |        * LSAP, as per XXX, and were also to check for an  | 
4019  |  |        * organization code of 0x000000 (encapsulated  | 
4020  |  |        * Ethernet), we'd do  | 
4021  |  |        *  | 
4022  |  |        *  return gen_snap(cstate, 0x000000, ll_proto);  | 
4023  |  |        *  | 
4024  |  |        * here; for now, we don't, as per the above.  | 
4025  |  |        * I don't know whether it's worth the extra CPU  | 
4026  |  |        * time to do the right check or not.  | 
4027  |  |        */  | 
4028  | 99  |       return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto);  | 
4029  | 99  |     }  | 
4030  | 688  |   }  | 
4031  | 688  | }  | 
4032  |  |  | 
4033  |  | static struct block *  | 
4034  |  | gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,  | 
4035  |  |     int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off)  | 
4036  | 0  | { | 
4037  | 0  |   struct block *b0, *b1;  | 
4038  | 0  |   u_int offset;  | 
4039  |  | 
  | 
4040  | 0  |   switch (dir) { | 
4041  |  |  | 
4042  | 0  |   case Q_SRC:  | 
4043  | 0  |     offset = src_off;  | 
4044  | 0  |     break;  | 
4045  |  |  | 
4046  | 0  |   case Q_DST:  | 
4047  | 0  |     offset = dst_off;  | 
4048  | 0  |     break;  | 
4049  |  |  | 
4050  | 0  |   case Q_AND:  | 
4051  | 0  |     b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);  | 
4052  | 0  |     b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);  | 
4053  | 0  |     gen_and(b0, b1);  | 
4054  | 0  |     return b1;  | 
4055  |  |  | 
4056  | 0  |   case Q_DEFAULT:  | 
4057  | 0  |   case Q_OR:  | 
4058  | 0  |     b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);  | 
4059  | 0  |     b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);  | 
4060  | 0  |     gen_or(b0, b1);  | 
4061  | 0  |     return b1;  | 
4062  |  |  | 
4063  | 0  |   case Q_ADDR1:  | 
4064  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4065  |  |     /*NOTREACHED*/  | 
4066  |  |  | 
4067  | 0  |   case Q_ADDR2:  | 
4068  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4069  |  |     /*NOTREACHED*/  | 
4070  |  |  | 
4071  | 0  |   case Q_ADDR3:  | 
4072  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4073  |  |     /*NOTREACHED*/  | 
4074  |  |  | 
4075  | 0  |   case Q_ADDR4:  | 
4076  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4077  |  |     /*NOTREACHED*/  | 
4078  |  |  | 
4079  | 0  |   case Q_RA:  | 
4080  | 0  |     bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4081  |  |     /*NOTREACHED*/  | 
4082  |  |  | 
4083  | 0  |   case Q_TA:  | 
4084  | 0  |     bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4085  |  |     /*NOTREACHED*/  | 
4086  |  |  | 
4087  | 0  |   default:  | 
4088  | 0  |     abort();  | 
4089  |  |     /*NOTREACHED*/  | 
4090  | 0  |   }  | 
4091  | 0  |   b0 = gen_linktype(cstate, ll_proto);  | 
4092  | 0  |   b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask);  | 
4093  | 0  |   gen_and(b0, b1);  | 
4094  | 0  |   return b1;  | 
4095  | 0  | }  | 
4096  |  |  | 
4097  |  | #ifdef INET6  | 
4098  |  | static struct block *  | 
4099  |  | gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr,  | 
4100  |  |     struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off,  | 
4101  |  |     u_int dst_off)  | 
4102  | 0  | { | 
4103  | 0  |   struct block *b0, *b1;  | 
4104  | 0  |   u_int offset;  | 
4105  | 0  |   uint32_t *a, *m;  | 
4106  |  | 
  | 
4107  | 0  |   switch (dir) { | 
4108  |  |  | 
4109  | 0  |   case Q_SRC:  | 
4110  | 0  |     offset = src_off;  | 
4111  | 0  |     break;  | 
4112  |  |  | 
4113  | 0  |   case Q_DST:  | 
4114  | 0  |     offset = dst_off;  | 
4115  | 0  |     break;  | 
4116  |  |  | 
4117  | 0  |   case Q_AND:  | 
4118  | 0  |     b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);  | 
4119  | 0  |     b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);  | 
4120  | 0  |     gen_and(b0, b1);  | 
4121  | 0  |     return b1;  | 
4122  |  |  | 
4123  | 0  |   case Q_DEFAULT:  | 
4124  | 0  |   case Q_OR:  | 
4125  | 0  |     b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off);  | 
4126  | 0  |     b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off);  | 
4127  | 0  |     gen_or(b0, b1);  | 
4128  | 0  |     return b1;  | 
4129  |  |  | 
4130  | 0  |   case Q_ADDR1:  | 
4131  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4132  |  |     /*NOTREACHED*/  | 
4133  |  |  | 
4134  | 0  |   case Q_ADDR2:  | 
4135  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4136  |  |     /*NOTREACHED*/  | 
4137  |  |  | 
4138  | 0  |   case Q_ADDR3:  | 
4139  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4140  |  |     /*NOTREACHED*/  | 
4141  |  |  | 
4142  | 0  |   case Q_ADDR4:  | 
4143  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4144  |  |     /*NOTREACHED*/  | 
4145  |  |  | 
4146  | 0  |   case Q_RA:  | 
4147  | 0  |     bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4148  |  |     /*NOTREACHED*/  | 
4149  |  |  | 
4150  | 0  |   case Q_TA:  | 
4151  | 0  |     bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4152  |  |     /*NOTREACHED*/  | 
4153  |  |  | 
4154  | 0  |   default:  | 
4155  | 0  |     abort();  | 
4156  |  |     /*NOTREACHED*/  | 
4157  | 0  |   }  | 
4158  |  |   /* this order is important */  | 
4159  | 0  |   a = (uint32_t *)addr;  | 
4160  | 0  |   m = (uint32_t *)mask;  | 
4161  | 0  |   b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));  | 
4162  | 0  |   b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));  | 
4163  | 0  |   gen_and(b0, b1);  | 
4164  | 0  |   b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));  | 
4165  | 0  |   gen_and(b0, b1);  | 
4166  | 0  |   b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));  | 
4167  | 0  |   gen_and(b0, b1);  | 
4168  | 0  |   b0 = gen_linktype(cstate, ll_proto);  | 
4169  | 0  |   gen_and(b0, b1);  | 
4170  | 0  |   return b1;  | 
4171  | 0  | }  | 
4172  |  | #endif  | 
4173  |  |  | 
4174  |  | static struct block *  | 
4175  |  | gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
4176  | 0  | { | 
4177  | 0  |   register struct block *b0, *b1;  | 
4178  |  | 
  | 
4179  | 0  |   switch (dir) { | 
4180  | 0  |   case Q_SRC:  | 
4181  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr);  | 
4182  |  |  | 
4183  | 0  |   case Q_DST:  | 
4184  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr);  | 
4185  |  |  | 
4186  | 0  |   case Q_AND:  | 
4187  | 0  |     b0 = gen_ehostop(cstate, eaddr, Q_SRC);  | 
4188  | 0  |     b1 = gen_ehostop(cstate, eaddr, Q_DST);  | 
4189  | 0  |     gen_and(b0, b1);  | 
4190  | 0  |     return b1;  | 
4191  |  |  | 
4192  | 0  |   case Q_DEFAULT:  | 
4193  | 0  |   case Q_OR:  | 
4194  | 0  |     b0 = gen_ehostop(cstate, eaddr, Q_SRC);  | 
4195  | 0  |     b1 = gen_ehostop(cstate, eaddr, Q_DST);  | 
4196  | 0  |     gen_or(b0, b1);  | 
4197  | 0  |     return b1;  | 
4198  |  |  | 
4199  | 0  |   case Q_ADDR1:  | 
4200  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");  | 
4201  |  |     /*NOTREACHED*/  | 
4202  |  |  | 
4203  | 0  |   case Q_ADDR2:  | 
4204  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");  | 
4205  |  |     /*NOTREACHED*/  | 
4206  |  |  | 
4207  | 0  |   case Q_ADDR3:  | 
4208  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");  | 
4209  |  |     /*NOTREACHED*/  | 
4210  |  |  | 
4211  | 0  |   case Q_ADDR4:  | 
4212  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");  | 
4213  |  |     /*NOTREACHED*/  | 
4214  |  |  | 
4215  | 0  |   case Q_RA:  | 
4216  | 0  |     bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers");  | 
4217  |  |     /*NOTREACHED*/  | 
4218  |  |  | 
4219  | 0  |   case Q_TA:  | 
4220  | 0  |     bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers");  | 
4221  |  |     /*NOTREACHED*/  | 
4222  | 0  |   }  | 
4223  | 0  |   abort();  | 
4224  |  |   /*NOTREACHED*/  | 
4225  | 0  | }  | 
4226  |  |  | 
4227  |  | /*  | 
4228  |  |  * Like gen_ehostop, but for DLT_FDDI  | 
4229  |  |  */  | 
4230  |  | static struct block *  | 
4231  |  | gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
4232  | 0  | { | 
4233  | 0  |   struct block *b0, *b1;  | 
4234  |  | 
  | 
4235  | 0  |   switch (dir) { | 
4236  | 0  |   case Q_SRC:  | 
4237  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr);  | 
4238  |  |  | 
4239  | 0  |   case Q_DST:  | 
4240  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr);  | 
4241  |  |  | 
4242  | 0  |   case Q_AND:  | 
4243  | 0  |     b0 = gen_fhostop(cstate, eaddr, Q_SRC);  | 
4244  | 0  |     b1 = gen_fhostop(cstate, eaddr, Q_DST);  | 
4245  | 0  |     gen_and(b0, b1);  | 
4246  | 0  |     return b1;  | 
4247  |  |  | 
4248  | 0  |   case Q_DEFAULT:  | 
4249  | 0  |   case Q_OR:  | 
4250  | 0  |     b0 = gen_fhostop(cstate, eaddr, Q_SRC);  | 
4251  | 0  |     b1 = gen_fhostop(cstate, eaddr, Q_DST);  | 
4252  | 0  |     gen_or(b0, b1);  | 
4253  | 0  |     return b1;  | 
4254  |  |  | 
4255  | 0  |   case Q_ADDR1:  | 
4256  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");  | 
4257  |  |     /*NOTREACHED*/  | 
4258  |  |  | 
4259  | 0  |   case Q_ADDR2:  | 
4260  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");  | 
4261  |  |     /*NOTREACHED*/  | 
4262  |  |  | 
4263  | 0  |   case Q_ADDR3:  | 
4264  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");  | 
4265  |  |     /*NOTREACHED*/  | 
4266  |  |  | 
4267  | 0  |   case Q_ADDR4:  | 
4268  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");  | 
4269  |  |     /*NOTREACHED*/  | 
4270  |  |  | 
4271  | 0  |   case Q_RA:  | 
4272  | 0  |     bpf_error(cstate, "'ra' is only supported on 802.11");  | 
4273  |  |     /*NOTREACHED*/  | 
4274  |  |  | 
4275  | 0  |   case Q_TA:  | 
4276  | 0  |     bpf_error(cstate, "'ta' is only supported on 802.11");  | 
4277  |  |     /*NOTREACHED*/  | 
4278  | 0  |   }  | 
4279  | 0  |   abort();  | 
4280  |  |   /*NOTREACHED*/  | 
4281  | 0  | }  | 
4282  |  |  | 
4283  |  | /*  | 
4284  |  |  * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)  | 
4285  |  |  */  | 
4286  |  | static struct block *  | 
4287  |  | gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
4288  | 0  | { | 
4289  | 0  |   register struct block *b0, *b1;  | 
4290  |  | 
  | 
4291  | 0  |   switch (dir) { | 
4292  | 0  |   case Q_SRC:  | 
4293  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr);  | 
4294  |  |  | 
4295  | 0  |   case Q_DST:  | 
4296  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);  | 
4297  |  |  | 
4298  | 0  |   case Q_AND:  | 
4299  | 0  |     b0 = gen_thostop(cstate, eaddr, Q_SRC);  | 
4300  | 0  |     b1 = gen_thostop(cstate, eaddr, Q_DST);  | 
4301  | 0  |     gen_and(b0, b1);  | 
4302  | 0  |     return b1;  | 
4303  |  |  | 
4304  | 0  |   case Q_DEFAULT:  | 
4305  | 0  |   case Q_OR:  | 
4306  | 0  |     b0 = gen_thostop(cstate, eaddr, Q_SRC);  | 
4307  | 0  |     b1 = gen_thostop(cstate, eaddr, Q_DST);  | 
4308  | 0  |     gen_or(b0, b1);  | 
4309  | 0  |     return b1;  | 
4310  |  |  | 
4311  | 0  |   case Q_ADDR1:  | 
4312  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");  | 
4313  |  |     /*NOTREACHED*/  | 
4314  |  |  | 
4315  | 0  |   case Q_ADDR2:  | 
4316  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");  | 
4317  |  |     /*NOTREACHED*/  | 
4318  |  |  | 
4319  | 0  |   case Q_ADDR3:  | 
4320  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");  | 
4321  |  |     /*NOTREACHED*/  | 
4322  |  |  | 
4323  | 0  |   case Q_ADDR4:  | 
4324  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");  | 
4325  |  |     /*NOTREACHED*/  | 
4326  |  |  | 
4327  | 0  |   case Q_RA:  | 
4328  | 0  |     bpf_error(cstate, "'ra' is only supported on 802.11");  | 
4329  |  |     /*NOTREACHED*/  | 
4330  |  |  | 
4331  | 0  |   case Q_TA:  | 
4332  | 0  |     bpf_error(cstate, "'ta' is only supported on 802.11");  | 
4333  |  |     /*NOTREACHED*/  | 
4334  | 0  |   }  | 
4335  | 0  |   abort();  | 
4336  |  |   /*NOTREACHED*/  | 
4337  | 0  | }  | 
4338  |  |  | 
4339  |  | /*  | 
4340  |  |  * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and  | 
4341  |  |  * various 802.11 + radio headers.  | 
4342  |  |  */  | 
4343  |  | static struct block *  | 
4344  |  | gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
4345  | 0  | { | 
4346  | 0  |   register struct block *b0, *b1, *b2;  | 
4347  | 0  |   register struct slist *s;  | 
4348  |  | 
  | 
4349  |  | #ifdef ENABLE_WLAN_FILTERING_PATCH  | 
4350  |  |   /*  | 
4351  |  |    * TODO GV 20070613  | 
4352  |  |    * We need to disable the optimizer because the optimizer is buggy  | 
4353  |  |    * and wipes out some LD instructions generated by the below  | 
4354  |  |    * code to validate the Frame Control bits  | 
4355  |  |    */  | 
4356  |  |   cstate->no_optimize = 1;  | 
4357  |  | #endif /* ENABLE_WLAN_FILTERING_PATCH */  | 
4358  |  | 
  | 
4359  | 0  |   switch (dir) { | 
4360  | 0  |   case Q_SRC:  | 
4361  |  |     /*  | 
4362  |  |      * Oh, yuk.  | 
4363  |  |      *  | 
4364  |  |      *  For control frames, there is no SA.  | 
4365  |  |      *  | 
4366  |  |      *  For management frames, SA is at an  | 
4367  |  |      *  offset of 10 from the beginning of  | 
4368  |  |      *  the packet.  | 
4369  |  |      *  | 
4370  |  |      *  For data frames, SA is at an offset  | 
4371  |  |      *  of 10 from the beginning of the packet  | 
4372  |  |      *  if From DS is clear, at an offset of  | 
4373  |  |      *  16 from the beginning of the packet  | 
4374  |  |      *  if From DS is set and To DS is clear,  | 
4375  |  |      *  and an offset of 24 from the beginning  | 
4376  |  |      *  of the packet if From DS is set and To DS  | 
4377  |  |      *  is set.  | 
4378  |  |      */  | 
4379  |  |  | 
4380  |  |     /*  | 
4381  |  |      * Generate the tests to be done for data frames  | 
4382  |  |      * with From DS set.  | 
4383  |  |      *  | 
4384  |  |      * First, check for To DS set, i.e. check "link[1] & 0x01".  | 
4385  |  |      */  | 
4386  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4387  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4388  | 0  |     b1->s.k = 0x01; /* To DS */  | 
4389  | 0  |     b1->stmts = s;  | 
4390  |  |  | 
4391  |  |     /*  | 
4392  |  |      * If To DS is set, the SA is at 24.  | 
4393  |  |      */  | 
4394  | 0  |     b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);  | 
4395  | 0  |     gen_and(b1, b0);  | 
4396  |  |  | 
4397  |  |     /*  | 
4398  |  |      * Now, check for To DS not set, i.e. check  | 
4399  |  |      * "!(link[1] & 0x01)".  | 
4400  |  |      */  | 
4401  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4402  | 0  |     b2 = new_block(cstate, JMP(BPF_JSET));  | 
4403  | 0  |     b2->s.k = 0x01; /* To DS */  | 
4404  | 0  |     b2->stmts = s;  | 
4405  | 0  |     gen_not(b2);  | 
4406  |  |  | 
4407  |  |     /*  | 
4408  |  |      * If To DS is not set, the SA is at 16.  | 
4409  |  |      */  | 
4410  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);  | 
4411  | 0  |     gen_and(b2, b1);  | 
4412  |  |  | 
4413  |  |     /*  | 
4414  |  |      * Now OR together the last two checks.  That gives  | 
4415  |  |      * the complete set of checks for data frames with  | 
4416  |  |      * From DS set.  | 
4417  |  |      */  | 
4418  | 0  |     gen_or(b1, b0);  | 
4419  |  |  | 
4420  |  |     /*  | 
4421  |  |      * Now check for From DS being set, and AND that with  | 
4422  |  |      * the ORed-together checks.  | 
4423  |  |      */  | 
4424  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4425  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4426  | 0  |     b1->s.k = 0x02; /* From DS */  | 
4427  | 0  |     b1->stmts = s;  | 
4428  | 0  |     gen_and(b1, b0);  | 
4429  |  |  | 
4430  |  |     /*  | 
4431  |  |      * Now check for data frames with From DS not set.  | 
4432  |  |      */  | 
4433  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4434  | 0  |     b2 = new_block(cstate, JMP(BPF_JSET));  | 
4435  | 0  |     b2->s.k = 0x02; /* From DS */  | 
4436  | 0  |     b2->stmts = s;  | 
4437  | 0  |     gen_not(b2);  | 
4438  |  |  | 
4439  |  |     /*  | 
4440  |  |      * If From DS isn't set, the SA is at 10.  | 
4441  |  |      */  | 
4442  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);  | 
4443  | 0  |     gen_and(b2, b1);  | 
4444  |  |  | 
4445  |  |     /*  | 
4446  |  |      * Now OR together the checks for data frames with  | 
4447  |  |      * From DS not set and for data frames with From DS  | 
4448  |  |      * set; that gives the checks done for data frames.  | 
4449  |  |      */  | 
4450  | 0  |     gen_or(b1, b0);  | 
4451  |  |  | 
4452  |  |     /*  | 
4453  |  |      * Now check for a data frame.  | 
4454  |  |      * I.e, check "link[0] & 0x08".  | 
4455  |  |      */  | 
4456  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4457  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4458  | 0  |     b1->s.k = 0x08;  | 
4459  | 0  |     b1->stmts = s;  | 
4460  |  |  | 
4461  |  |     /*  | 
4462  |  |      * AND that with the checks done for data frames.  | 
4463  |  |      */  | 
4464  | 0  |     gen_and(b1, b0);  | 
4465  |  |  | 
4466  |  |     /*  | 
4467  |  |      * If the high-order bit of the type value is 0, this  | 
4468  |  |      * is a management frame.  | 
4469  |  |      * I.e, check "!(link[0] & 0x08)".  | 
4470  |  |      */  | 
4471  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4472  | 0  |     b2 = new_block(cstate, JMP(BPF_JSET));  | 
4473  | 0  |     b2->s.k = 0x08;  | 
4474  | 0  |     b2->stmts = s;  | 
4475  | 0  |     gen_not(b2);  | 
4476  |  |  | 
4477  |  |     /*  | 
4478  |  |      * For management frames, the SA is at 10.  | 
4479  |  |      */  | 
4480  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);  | 
4481  | 0  |     gen_and(b2, b1);  | 
4482  |  |  | 
4483  |  |     /*  | 
4484  |  |      * OR that with the checks done for data frames.  | 
4485  |  |      * That gives the checks done for management and  | 
4486  |  |      * data frames.  | 
4487  |  |      */  | 
4488  | 0  |     gen_or(b1, b0);  | 
4489  |  |  | 
4490  |  |     /*  | 
4491  |  |      * If the low-order bit of the type value is 1,  | 
4492  |  |      * this is either a control frame or a frame  | 
4493  |  |      * with a reserved type, and thus not a  | 
4494  |  |      * frame with an SA.  | 
4495  |  |      *  | 
4496  |  |      * I.e., check "!(link[0] & 0x04)".  | 
4497  |  |      */  | 
4498  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4499  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4500  | 0  |     b1->s.k = 0x04;  | 
4501  | 0  |     b1->stmts = s;  | 
4502  | 0  |     gen_not(b1);  | 
4503  |  |  | 
4504  |  |     /*  | 
4505  |  |      * AND that with the checks for data and management  | 
4506  |  |      * frames.  | 
4507  |  |      */  | 
4508  | 0  |     gen_and(b1, b0);  | 
4509  | 0  |     return b0;  | 
4510  |  |  | 
4511  | 0  |   case Q_DST:  | 
4512  |  |     /*  | 
4513  |  |      * Oh, yuk.  | 
4514  |  |      *  | 
4515  |  |      *  For control frames, there is no DA.  | 
4516  |  |      *  | 
4517  |  |      *  For management frames, DA is at an  | 
4518  |  |      *  offset of 4 from the beginning of  | 
4519  |  |      *  the packet.  | 
4520  |  |      *  | 
4521  |  |      *  For data frames, DA is at an offset  | 
4522  |  |      *  of 4 from the beginning of the packet  | 
4523  |  |      *  if To DS is clear and at an offset of  | 
4524  |  |      *  16 from the beginning of the packet  | 
4525  |  |      *  if To DS is set.  | 
4526  |  |      */  | 
4527  |  |  | 
4528  |  |     /*  | 
4529  |  |      * Generate the tests to be done for data frames.  | 
4530  |  |      *  | 
4531  |  |      * First, check for To DS set, i.e. "link[1] & 0x01".  | 
4532  |  |      */  | 
4533  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4534  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4535  | 0  |     b1->s.k = 0x01; /* To DS */  | 
4536  | 0  |     b1->stmts = s;  | 
4537  |  |  | 
4538  |  |     /*  | 
4539  |  |      * If To DS is set, the DA is at 16.  | 
4540  |  |      */  | 
4541  | 0  |     b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);  | 
4542  | 0  |     gen_and(b1, b0);  | 
4543  |  |  | 
4544  |  |     /*  | 
4545  |  |      * Now, check for To DS not set, i.e. check  | 
4546  |  |      * "!(link[1] & 0x01)".  | 
4547  |  |      */  | 
4548  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
4549  | 0  |     b2 = new_block(cstate, JMP(BPF_JSET));  | 
4550  | 0  |     b2->s.k = 0x01; /* To DS */  | 
4551  | 0  |     b2->stmts = s;  | 
4552  | 0  |     gen_not(b2);  | 
4553  |  |  | 
4554  |  |     /*  | 
4555  |  |      * If To DS is not set, the DA is at 4.  | 
4556  |  |      */  | 
4557  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);  | 
4558  | 0  |     gen_and(b2, b1);  | 
4559  |  |  | 
4560  |  |     /*  | 
4561  |  |      * Now OR together the last two checks.  That gives  | 
4562  |  |      * the complete set of checks for data frames.  | 
4563  |  |      */  | 
4564  | 0  |     gen_or(b1, b0);  | 
4565  |  |  | 
4566  |  |     /*  | 
4567  |  |      * Now check for a data frame.  | 
4568  |  |      * I.e, check "link[0] & 0x08".  | 
4569  |  |      */  | 
4570  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4571  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4572  | 0  |     b1->s.k = 0x08;  | 
4573  | 0  |     b1->stmts = s;  | 
4574  |  |  | 
4575  |  |     /*  | 
4576  |  |      * AND that with the checks done for data frames.  | 
4577  |  |      */  | 
4578  | 0  |     gen_and(b1, b0);  | 
4579  |  |  | 
4580  |  |     /*  | 
4581  |  |      * If the high-order bit of the type value is 0, this  | 
4582  |  |      * is a management frame.  | 
4583  |  |      * I.e, check "!(link[0] & 0x08)".  | 
4584  |  |      */  | 
4585  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4586  | 0  |     b2 = new_block(cstate, JMP(BPF_JSET));  | 
4587  | 0  |     b2->s.k = 0x08;  | 
4588  | 0  |     b2->stmts = s;  | 
4589  | 0  |     gen_not(b2);  | 
4590  |  |  | 
4591  |  |     /*  | 
4592  |  |      * For management frames, the DA is at 4.  | 
4593  |  |      */  | 
4594  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);  | 
4595  | 0  |     gen_and(b2, b1);  | 
4596  |  |  | 
4597  |  |     /*  | 
4598  |  |      * OR that with the checks done for data frames.  | 
4599  |  |      * That gives the checks done for management and  | 
4600  |  |      * data frames.  | 
4601  |  |      */  | 
4602  | 0  |     gen_or(b1, b0);  | 
4603  |  |  | 
4604  |  |     /*  | 
4605  |  |      * If the low-order bit of the type value is 1,  | 
4606  |  |      * this is either a control frame or a frame  | 
4607  |  |      * with a reserved type, and thus not a  | 
4608  |  |      * frame with an SA.  | 
4609  |  |      *  | 
4610  |  |      * I.e., check "!(link[0] & 0x04)".  | 
4611  |  |      */  | 
4612  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4613  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4614  | 0  |     b1->s.k = 0x04;  | 
4615  | 0  |     b1->stmts = s;  | 
4616  | 0  |     gen_not(b1);  | 
4617  |  |  | 
4618  |  |     /*  | 
4619  |  |      * AND that with the checks for data and management  | 
4620  |  |      * frames.  | 
4621  |  |      */  | 
4622  | 0  |     gen_and(b1, b0);  | 
4623  | 0  |     return b0;  | 
4624  |  |  | 
4625  | 0  |   case Q_AND:  | 
4626  | 0  |     b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);  | 
4627  | 0  |     b1 = gen_wlanhostop(cstate, eaddr, Q_DST);  | 
4628  | 0  |     gen_and(b0, b1);  | 
4629  | 0  |     return b1;  | 
4630  |  |  | 
4631  | 0  |   case Q_DEFAULT:  | 
4632  | 0  |   case Q_OR:  | 
4633  | 0  |     b0 = gen_wlanhostop(cstate, eaddr, Q_SRC);  | 
4634  | 0  |     b1 = gen_wlanhostop(cstate, eaddr, Q_DST);  | 
4635  | 0  |     gen_or(b0, b1);  | 
4636  | 0  |     return b1;  | 
4637  |  |  | 
4638  |  |   /*  | 
4639  |  |    * XXX - add BSSID keyword?  | 
4640  |  |    */  | 
4641  | 0  |   case Q_ADDR1:  | 
4642  | 0  |     return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr));  | 
4643  |  |  | 
4644  | 0  |   case Q_ADDR2:  | 
4645  |  |     /*  | 
4646  |  |      * Not present in CTS or ACK control frames.  | 
4647  |  |      */  | 
4648  | 0  |     b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,  | 
4649  | 0  |       IEEE80211_FC0_TYPE_MASK);  | 
4650  | 0  |     gen_not(b0);  | 
4651  | 0  |     b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,  | 
4652  | 0  |       IEEE80211_FC0_SUBTYPE_MASK);  | 
4653  | 0  |     gen_not(b1);  | 
4654  | 0  |     b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,  | 
4655  | 0  |       IEEE80211_FC0_SUBTYPE_MASK);  | 
4656  | 0  |     gen_not(b2);  | 
4657  | 0  |     gen_and(b1, b2);  | 
4658  | 0  |     gen_or(b0, b2);  | 
4659  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);  | 
4660  | 0  |     gen_and(b2, b1);  | 
4661  | 0  |     return b1;  | 
4662  |  |  | 
4663  | 0  |   case Q_ADDR3:  | 
4664  |  |     /*  | 
4665  |  |      * Not present in control frames.  | 
4666  |  |      */  | 
4667  | 0  |     b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,  | 
4668  | 0  |       IEEE80211_FC0_TYPE_MASK);  | 
4669  | 0  |     gen_not(b0);  | 
4670  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr);  | 
4671  | 0  |     gen_and(b0, b1);  | 
4672  | 0  |     return b1;  | 
4673  |  |  | 
4674  | 0  |   case Q_ADDR4:  | 
4675  |  |     /*  | 
4676  |  |      * Present only if the direction mask has both "From DS"  | 
4677  |  |      * and "To DS" set.  Neither control frames nor management  | 
4678  |  |      * frames should have both of those set, so we don't  | 
4679  |  |      * check the frame type.  | 
4680  |  |      */  | 
4681  | 0  |     b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B,  | 
4682  | 0  |       IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);  | 
4683  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr);  | 
4684  | 0  |     gen_and(b0, b1);  | 
4685  | 0  |     return b1;  | 
4686  |  |  | 
4687  | 0  |   case Q_RA:  | 
4688  |  |     /*  | 
4689  |  |      * Not present in management frames; addr1 in other  | 
4690  |  |      * frames.  | 
4691  |  |      */  | 
4692  |  |  | 
4693  |  |     /*  | 
4694  |  |      * If the high-order bit of the type value is 0, this  | 
4695  |  |      * is a management frame.  | 
4696  |  |      * I.e, check "(link[0] & 0x08)".  | 
4697  |  |      */  | 
4698  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4699  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4700  | 0  |     b1->s.k = 0x08;  | 
4701  | 0  |     b1->stmts = s;  | 
4702  |  |  | 
4703  |  |     /*  | 
4704  |  |      * Check addr1.  | 
4705  |  |      */  | 
4706  | 0  |     b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr);  | 
4707  |  |  | 
4708  |  |     /*  | 
4709  |  |      * AND that with the check of addr1.  | 
4710  |  |      */  | 
4711  | 0  |     gen_and(b1, b0);  | 
4712  | 0  |     return (b0);  | 
4713  |  |  | 
4714  | 0  |   case Q_TA:  | 
4715  |  |     /*  | 
4716  |  |      * Not present in management frames; addr2, if present,  | 
4717  |  |      * in other frames.  | 
4718  |  |      */  | 
4719  |  |  | 
4720  |  |     /*  | 
4721  |  |      * Not present in CTS or ACK control frames.  | 
4722  |  |      */  | 
4723  | 0  |     b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,  | 
4724  | 0  |       IEEE80211_FC0_TYPE_MASK);  | 
4725  | 0  |     gen_not(b0);  | 
4726  | 0  |     b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,  | 
4727  | 0  |       IEEE80211_FC0_SUBTYPE_MASK);  | 
4728  | 0  |     gen_not(b1);  | 
4729  | 0  |     b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,  | 
4730  | 0  |       IEEE80211_FC0_SUBTYPE_MASK);  | 
4731  | 0  |     gen_not(b2);  | 
4732  | 0  |     gen_and(b1, b2);  | 
4733  | 0  |     gen_or(b0, b2);  | 
4734  |  |  | 
4735  |  |     /*  | 
4736  |  |      * If the high-order bit of the type value is 0, this  | 
4737  |  |      * is a management frame.  | 
4738  |  |      * I.e, check "(link[0] & 0x08)".  | 
4739  |  |      */  | 
4740  | 0  |     s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
4741  | 0  |     b1 = new_block(cstate, JMP(BPF_JSET));  | 
4742  | 0  |     b1->s.k = 0x08;  | 
4743  | 0  |     b1->stmts = s;  | 
4744  |  |  | 
4745  |  |     /*  | 
4746  |  |      * AND that with the check for frames other than  | 
4747  |  |      * CTS and ACK frames.  | 
4748  |  |      */  | 
4749  | 0  |     gen_and(b1, b2);  | 
4750  |  |  | 
4751  |  |     /*  | 
4752  |  |      * Check addr2.  | 
4753  |  |      */  | 
4754  | 0  |     b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);  | 
4755  | 0  |     gen_and(b2, b1);  | 
4756  | 0  |     return b1;  | 
4757  | 0  |   }  | 
4758  | 0  |   abort();  | 
4759  |  |   /*NOTREACHED*/  | 
4760  | 0  | }  | 
4761  |  |  | 
4762  |  | /*  | 
4763  |  |  * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.  | 
4764  |  |  * (We assume that the addresses are IEEE 48-bit MAC addresses,  | 
4765  |  |  * as the RFC states.)  | 
4766  |  |  */  | 
4767  |  | static struct block *  | 
4768  |  | gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
4769  | 0  | { | 
4770  | 0  |   register struct block *b0, *b1;  | 
4771  |  | 
  | 
4772  | 0  |   switch (dir) { | 
4773  | 0  |   case Q_SRC:  | 
4774  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr);  | 
4775  |  |  | 
4776  | 0  |   case Q_DST:  | 
4777  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr);  | 
4778  |  |  | 
4779  | 0  |   case Q_AND:  | 
4780  | 0  |     b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);  | 
4781  | 0  |     b1 = gen_ipfchostop(cstate, eaddr, Q_DST);  | 
4782  | 0  |     gen_and(b0, b1);  | 
4783  | 0  |     return b1;  | 
4784  |  |  | 
4785  | 0  |   case Q_DEFAULT:  | 
4786  | 0  |   case Q_OR:  | 
4787  | 0  |     b0 = gen_ipfchostop(cstate, eaddr, Q_SRC);  | 
4788  | 0  |     b1 = gen_ipfchostop(cstate, eaddr, Q_DST);  | 
4789  | 0  |     gen_or(b0, b1);  | 
4790  | 0  |     return b1;  | 
4791  |  |  | 
4792  | 0  |   case Q_ADDR1:  | 
4793  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");  | 
4794  |  |     /*NOTREACHED*/  | 
4795  |  |  | 
4796  | 0  |   case Q_ADDR2:  | 
4797  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");  | 
4798  |  |     /*NOTREACHED*/  | 
4799  |  |  | 
4800  | 0  |   case Q_ADDR3:  | 
4801  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");  | 
4802  |  |     /*NOTREACHED*/  | 
4803  |  |  | 
4804  | 0  |   case Q_ADDR4:  | 
4805  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");  | 
4806  |  |     /*NOTREACHED*/  | 
4807  |  |  | 
4808  | 0  |   case Q_RA:  | 
4809  | 0  |     bpf_error(cstate, "'ra' is only supported on 802.11");  | 
4810  |  |     /*NOTREACHED*/  | 
4811  |  |  | 
4812  | 0  |   case Q_TA:  | 
4813  | 0  |     bpf_error(cstate, "'ta' is only supported on 802.11");  | 
4814  |  |     /*NOTREACHED*/  | 
4815  | 0  |   }  | 
4816  | 0  |   abort();  | 
4817  |  |   /*NOTREACHED*/  | 
4818  | 0  | }  | 
4819  |  |  | 
4820  |  | /*  | 
4821  |  |  * This is quite tricky because there may be pad bytes in front of the  | 
4822  |  |  * DECNET header, and then there are two possible data packet formats that  | 
4823  |  |  * carry both src and dst addresses, plus 5 packet types in a format that  | 
4824  |  |  * carries only the src node, plus 2 types that use a different format and  | 
4825  |  |  * also carry just the src node.  | 
4826  |  |  *  | 
4827  |  |  * Yuck.  | 
4828  |  |  *  | 
4829  |  |  * Instead of doing those all right, we just look for data packets with  | 
4830  |  |  * 0 or 1 bytes of padding.  If you want to look at other packets, that  | 
4831  |  |  * will require a lot more hacking.  | 
4832  |  |  *  | 
4833  |  |  * To add support for filtering on DECNET "areas" (network numbers)  | 
4834  |  |  * one would want to add a "mask" argument to this routine.  That would  | 
4835  |  |  * make the filter even more inefficient, although one could be clever  | 
4836  |  |  * and not generate masking instructions if the mask is 0xFFFF.  | 
4837  |  |  */  | 
4838  |  | static struct block *  | 
4839  |  | gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir)  | 
4840  | 0  | { | 
4841  | 0  |   struct block *b0, *b1, *b2, *tmp;  | 
4842  | 0  |   u_int offset_lh;  /* offset if long header is received */  | 
4843  | 0  |   u_int offset_sh;  /* offset if short header is received */  | 
4844  |  | 
  | 
4845  | 0  |   switch (dir) { | 
4846  |  |  | 
4847  | 0  |   case Q_DST:  | 
4848  | 0  |     offset_sh = 1;  /* follows flags */  | 
4849  | 0  |     offset_lh = 7;  /* flgs,darea,dsubarea,HIORD */  | 
4850  | 0  |     break;  | 
4851  |  |  | 
4852  | 0  |   case Q_SRC:  | 
4853  | 0  |     offset_sh = 3;  /* follows flags, dstnode */  | 
4854  | 0  |     offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */  | 
4855  | 0  |     break;  | 
4856  |  |  | 
4857  | 0  |   case Q_AND:  | 
4858  |  |     /* Inefficient because we do our Calvinball dance twice */  | 
4859  | 0  |     b0 = gen_dnhostop(cstate, addr, Q_SRC);  | 
4860  | 0  |     b1 = gen_dnhostop(cstate, addr, Q_DST);  | 
4861  | 0  |     gen_and(b0, b1);  | 
4862  | 0  |     return b1;  | 
4863  |  |  | 
4864  | 0  |   case Q_DEFAULT:  | 
4865  | 0  |   case Q_OR:  | 
4866  |  |     /* Inefficient because we do our Calvinball dance twice */  | 
4867  | 0  |     b0 = gen_dnhostop(cstate, addr, Q_SRC);  | 
4868  | 0  |     b1 = gen_dnhostop(cstate, addr, Q_DST);  | 
4869  | 0  |     gen_or(b0, b1);  | 
4870  | 0  |     return b1;  | 
4871  |  |  | 
4872  | 0  |   case Q_ADDR1:  | 
4873  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4874  |  |     /*NOTREACHED*/  | 
4875  |  |  | 
4876  | 0  |   case Q_ADDR2:  | 
4877  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4878  |  |     /*NOTREACHED*/  | 
4879  |  |  | 
4880  | 0  |   case Q_ADDR3:  | 
4881  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4882  |  |     /*NOTREACHED*/  | 
4883  |  |  | 
4884  | 0  |   case Q_ADDR4:  | 
4885  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");  | 
4886  |  |     /*NOTREACHED*/  | 
4887  |  |  | 
4888  | 0  |   case Q_RA:  | 
4889  | 0  |     bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4890  |  |     /*NOTREACHED*/  | 
4891  |  |  | 
4892  | 0  |   case Q_TA:  | 
4893  | 0  |     bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");  | 
4894  |  |     /*NOTREACHED*/  | 
4895  |  |  | 
4896  | 0  |   default:  | 
4897  | 0  |     abort();  | 
4898  |  |     /*NOTREACHED*/  | 
4899  | 0  |   }  | 
4900  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_DN);  | 
4901  |  |   /* Check for pad = 1, long header case */  | 
4902  | 0  |   tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,  | 
4903  | 0  |       (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF));  | 
4904  | 0  |   b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh,  | 
4905  | 0  |       BPF_H, (bpf_u_int32)ntohs((u_short)addr));  | 
4906  | 0  |   gen_and(tmp, b1);  | 
4907  |  |   /* Check for pad = 0, long header case */  | 
4908  | 0  |   tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06,  | 
4909  | 0  |       (bpf_u_int32)0x7);  | 
4910  | 0  |   b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H,  | 
4911  | 0  |       (bpf_u_int32)ntohs((u_short)addr));  | 
4912  | 0  |   gen_and(tmp, b2);  | 
4913  | 0  |   gen_or(b2, b1);  | 
4914  |  |   /* Check for pad = 1, short header case */  | 
4915  | 0  |   tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H,  | 
4916  | 0  |       (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF));  | 
4917  | 0  |   b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H,  | 
4918  | 0  |       (bpf_u_int32)ntohs((u_short)addr));  | 
4919  | 0  |   gen_and(tmp, b2);  | 
4920  | 0  |   gen_or(b2, b1);  | 
4921  |  |   /* Check for pad = 0, short header case */  | 
4922  | 0  |   tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02,  | 
4923  | 0  |       (bpf_u_int32)0x7);  | 
4924  | 0  |   b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H,  | 
4925  | 0  |       (bpf_u_int32)ntohs((u_short)addr));  | 
4926  | 0  |   gen_and(tmp, b2);  | 
4927  | 0  |   gen_or(b2, b1);  | 
4928  |  |  | 
4929  |  |   /* Combine with test for cstate->linktype */  | 
4930  | 0  |   gen_and(b0, b1);  | 
4931  | 0  |   return b1;  | 
4932  | 0  | }  | 
4933  |  |  | 
4934  |  | /*  | 
4935  |  |  * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;  | 
4936  |  |  * test the bottom-of-stack bit, and then check the version number  | 
4937  |  |  * field in the IP header.  | 
4938  |  |  */  | 
4939  |  | static struct block *  | 
4940  |  | gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto)  | 
4941  | 0  | { | 
4942  | 0  |   struct block *b0, *b1;  | 
4943  |  | 
  | 
4944  | 0  |         switch (ll_proto) { | 
4945  |  |  | 
4946  | 0  |         case ETHERTYPE_IP:  | 
4947  |  |                 /* match the bottom-of-stack bit */  | 
4948  | 0  |                 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);  | 
4949  |  |                 /* match the IPv4 version number */  | 
4950  | 0  |                 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0);  | 
4951  | 0  |                 gen_and(b0, b1);  | 
4952  | 0  |                 return b1;  | 
4953  |  |  | 
4954  | 0  |         case ETHERTYPE_IPV6:  | 
4955  |  |                 /* match the bottom-of-stack bit */  | 
4956  | 0  |                 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01);  | 
4957  |  |                 /* match the IPv4 version number */  | 
4958  | 0  |                 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0);  | 
4959  | 0  |                 gen_and(b0, b1);  | 
4960  | 0  |                 return b1;  | 
4961  |  |  | 
4962  | 0  |         default:  | 
4963  |  |                /* FIXME add other L3 proto IDs */  | 
4964  | 0  |                bpf_error(cstate, "unsupported protocol over mpls");  | 
4965  |  |                /*NOTREACHED*/  | 
4966  | 0  |         }  | 
4967  | 0  | }  | 
4968  |  |  | 
4969  |  | static struct block *  | 
4970  |  | gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask,  | 
4971  |  |     int proto, int dir, int type)  | 
4972  | 0  | { | 
4973  | 0  |   struct block *b0, *b1;  | 
4974  | 0  |   const char *typestr;  | 
4975  |  | 
  | 
4976  | 0  |   if (type == Q_NET)  | 
4977  | 0  |     typestr = "net";  | 
4978  | 0  |   else  | 
4979  | 0  |     typestr = "host";  | 
4980  |  | 
  | 
4981  | 0  |   switch (proto) { | 
4982  |  |  | 
4983  | 0  |   case Q_DEFAULT:  | 
4984  | 0  |     b0 = gen_host(cstate, addr, mask, Q_IP, dir, type);  | 
4985  |  |     /*  | 
4986  |  |      * Only check for non-IPv4 addresses if we're not  | 
4987  |  |      * checking MPLS-encapsulated packets.  | 
4988  |  |      */  | 
4989  | 0  |     if (cstate->label_stack_depth == 0) { | 
4990  | 0  |       b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type);  | 
4991  | 0  |       gen_or(b0, b1);  | 
4992  | 0  |       b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type);  | 
4993  | 0  |       gen_or(b1, b0);  | 
4994  | 0  |     }  | 
4995  | 0  |     return b0;  | 
4996  |  |  | 
4997  | 0  |   case Q_LINK:  | 
4998  | 0  |     bpf_error(cstate, "link-layer modifier applied to %s", typestr);  | 
4999  |  |  | 
5000  | 0  |   case Q_IP:  | 
5001  | 0  |     return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16);  | 
5002  |  |  | 
5003  | 0  |   case Q_RARP:  | 
5004  | 0  |     return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24);  | 
5005  |  |  | 
5006  | 0  |   case Q_ARP:  | 
5007  | 0  |     return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24);  | 
5008  |  |  | 
5009  | 0  |   case Q_SCTP:  | 
5010  | 0  |     bpf_error(cstate, "'sctp' modifier applied to %s", typestr);  | 
5011  |  |  | 
5012  | 0  |   case Q_TCP:  | 
5013  | 0  |     bpf_error(cstate, "'tcp' modifier applied to %s", typestr);  | 
5014  |  |  | 
5015  | 0  |   case Q_UDP:  | 
5016  | 0  |     bpf_error(cstate, "'udp' modifier applied to %s", typestr);  | 
5017  |  |  | 
5018  | 0  |   case Q_ICMP:  | 
5019  | 0  |     bpf_error(cstate, "'icmp' modifier applied to %s", typestr);  | 
5020  |  |  | 
5021  | 0  |   case Q_IGMP:  | 
5022  | 0  |     bpf_error(cstate, "'igmp' modifier applied to %s", typestr);  | 
5023  |  |  | 
5024  | 0  |   case Q_IGRP:  | 
5025  | 0  |     bpf_error(cstate, "'igrp' modifier applied to %s", typestr);  | 
5026  |  |  | 
5027  | 0  |   case Q_ATALK:  | 
5028  | 0  |     bpf_error(cstate, "AppleTalk host filtering not implemented");  | 
5029  |  |  | 
5030  | 0  |   case Q_DECNET:  | 
5031  | 0  |     return gen_dnhostop(cstate, addr, dir);  | 
5032  |  |  | 
5033  | 0  |   case Q_LAT:  | 
5034  | 0  |     bpf_error(cstate, "LAT host filtering not implemented");  | 
5035  |  |  | 
5036  | 0  |   case Q_SCA:  | 
5037  | 0  |     bpf_error(cstate, "SCA host filtering not implemented");  | 
5038  |  |  | 
5039  | 0  |   case Q_MOPRC:  | 
5040  | 0  |     bpf_error(cstate, "MOPRC host filtering not implemented");  | 
5041  |  |  | 
5042  | 0  |   case Q_MOPDL:  | 
5043  | 0  |     bpf_error(cstate, "MOPDL host filtering not implemented");  | 
5044  |  |  | 
5045  | 0  |   case Q_IPV6:  | 
5046  | 0  |     bpf_error(cstate, "'ip6' modifier applied to ip host");  | 
5047  |  |  | 
5048  | 0  |   case Q_ICMPV6:  | 
5049  | 0  |     bpf_error(cstate, "'icmp6' modifier applied to %s", typestr);  | 
5050  |  |  | 
5051  | 0  |   case Q_AH:  | 
5052  | 0  |     bpf_error(cstate, "'ah' modifier applied to %s", typestr);  | 
5053  |  |  | 
5054  | 0  |   case Q_ESP:  | 
5055  | 0  |     bpf_error(cstate, "'esp' modifier applied to %s", typestr);  | 
5056  |  |  | 
5057  | 0  |   case Q_PIM:  | 
5058  | 0  |     bpf_error(cstate, "'pim' modifier applied to %s", typestr);  | 
5059  |  |  | 
5060  | 0  |   case Q_VRRP:  | 
5061  | 0  |     bpf_error(cstate, "'vrrp' modifier applied to %s", typestr);  | 
5062  |  |  | 
5063  | 0  |   case Q_AARP:  | 
5064  | 0  |     bpf_error(cstate, "AARP host filtering not implemented");  | 
5065  |  |  | 
5066  | 0  |   case Q_ISO:  | 
5067  | 0  |     bpf_error(cstate, "ISO host filtering not implemented");  | 
5068  |  |  | 
5069  | 0  |   case Q_ESIS:  | 
5070  | 0  |     bpf_error(cstate, "'esis' modifier applied to %s", typestr);  | 
5071  |  |  | 
5072  | 0  |   case Q_ISIS:  | 
5073  | 0  |     bpf_error(cstate, "'isis' modifier applied to %s", typestr);  | 
5074  |  |  | 
5075  | 0  |   case Q_CLNP:  | 
5076  | 0  |     bpf_error(cstate, "'clnp' modifier applied to %s", typestr);  | 
5077  |  |  | 
5078  | 0  |   case Q_STP:  | 
5079  | 0  |     bpf_error(cstate, "'stp' modifier applied to %s", typestr);  | 
5080  |  |  | 
5081  | 0  |   case Q_IPX:  | 
5082  | 0  |     bpf_error(cstate, "IPX host filtering not implemented");  | 
5083  |  |  | 
5084  | 0  |   case Q_NETBEUI:  | 
5085  | 0  |     bpf_error(cstate, "'netbeui' modifier applied to %s", typestr);  | 
5086  |  |  | 
5087  | 0  |   case Q_ISIS_L1:  | 
5088  | 0  |     bpf_error(cstate, "'l1' modifier applied to %s", typestr);  | 
5089  |  |  | 
5090  | 0  |   case Q_ISIS_L2:  | 
5091  | 0  |     bpf_error(cstate, "'l2' modifier applied to %s", typestr);  | 
5092  |  |  | 
5093  | 0  |   case Q_ISIS_IIH:  | 
5094  | 0  |     bpf_error(cstate, "'iih' modifier applied to %s", typestr);  | 
5095  |  |  | 
5096  | 0  |   case Q_ISIS_SNP:  | 
5097  | 0  |     bpf_error(cstate, "'snp' modifier applied to %s", typestr);  | 
5098  |  |  | 
5099  | 0  |   case Q_ISIS_CSNP:  | 
5100  | 0  |     bpf_error(cstate, "'csnp' modifier applied to %s", typestr);  | 
5101  |  |  | 
5102  | 0  |   case Q_ISIS_PSNP:  | 
5103  | 0  |     bpf_error(cstate, "'psnp' modifier applied to %s", typestr);  | 
5104  |  |  | 
5105  | 0  |   case Q_ISIS_LSP:  | 
5106  | 0  |     bpf_error(cstate, "'lsp' modifier applied to %s", typestr);  | 
5107  |  |  | 
5108  | 0  |   case Q_RADIO:  | 
5109  | 0  |     bpf_error(cstate, "'radio' modifier applied to %s", typestr);  | 
5110  |  |  | 
5111  | 0  |   case Q_CARP:  | 
5112  | 0  |     bpf_error(cstate, "'carp' modifier applied to %s", typestr);  | 
5113  |  |  | 
5114  | 0  |   default:  | 
5115  | 0  |     abort();  | 
5116  | 0  |   }  | 
5117  |  |   /*NOTREACHED*/  | 
5118  | 0  | }  | 
5119  |  |  | 
5120  |  | #ifdef INET6  | 
5121  |  | static struct block *  | 
5122  |  | gen_host6(compiler_state_t *cstate, struct in6_addr *addr,  | 
5123  |  |     struct in6_addr *mask, int proto, int dir, int type)  | 
5124  | 0  | { | 
5125  | 0  |   const char *typestr;  | 
5126  |  | 
  | 
5127  | 0  |   if (type == Q_NET)  | 
5128  | 0  |     typestr = "net";  | 
5129  | 0  |   else  | 
5130  | 0  |     typestr = "host";  | 
5131  |  | 
  | 
5132  | 0  |   switch (proto) { | 
5133  |  |  | 
5134  | 0  |   case Q_DEFAULT:  | 
5135  | 0  |     return gen_host6(cstate, addr, mask, Q_IPV6, dir, type);  | 
5136  |  |  | 
5137  | 0  |   case Q_LINK:  | 
5138  | 0  |     bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr);  | 
5139  |  |  | 
5140  | 0  |   case Q_IP:  | 
5141  | 0  |     bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr);  | 
5142  |  |  | 
5143  | 0  |   case Q_RARP:  | 
5144  | 0  |     bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr);  | 
5145  |  |  | 
5146  | 0  |   case Q_ARP:  | 
5147  | 0  |     bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr);  | 
5148  |  |  | 
5149  | 0  |   case Q_SCTP:  | 
5150  | 0  |     bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr);  | 
5151  |  |  | 
5152  | 0  |   case Q_TCP:  | 
5153  | 0  |     bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr);  | 
5154  |  |  | 
5155  | 0  |   case Q_UDP:  | 
5156  | 0  |     bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr);  | 
5157  |  |  | 
5158  | 0  |   case Q_ICMP:  | 
5159  | 0  |     bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr);  | 
5160  |  |  | 
5161  | 0  |   case Q_IGMP:  | 
5162  | 0  |     bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr);  | 
5163  |  |  | 
5164  | 0  |   case Q_IGRP:  | 
5165  | 0  |     bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr);  | 
5166  |  |  | 
5167  | 0  |   case Q_ATALK:  | 
5168  | 0  |     bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr);  | 
5169  |  |  | 
5170  | 0  |   case Q_DECNET:  | 
5171  | 0  |     bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr);  | 
5172  |  |  | 
5173  | 0  |   case Q_LAT:  | 
5174  | 0  |     bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr);  | 
5175  |  |  | 
5176  | 0  |   case Q_SCA:  | 
5177  | 0  |     bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr);  | 
5178  |  |  | 
5179  | 0  |   case Q_MOPRC:  | 
5180  | 0  |     bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr);  | 
5181  |  |  | 
5182  | 0  |   case Q_MOPDL:  | 
5183  | 0  |     bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr);  | 
5184  |  |  | 
5185  | 0  |   case Q_IPV6:  | 
5186  | 0  |     return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24);  | 
5187  |  |  | 
5188  | 0  |   case Q_ICMPV6:  | 
5189  | 0  |     bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr);  | 
5190  |  |  | 
5191  | 0  |   case Q_AH:  | 
5192  | 0  |     bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr);  | 
5193  |  |  | 
5194  | 0  |   case Q_ESP:  | 
5195  | 0  |     bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr);  | 
5196  |  |  | 
5197  | 0  |   case Q_PIM:  | 
5198  | 0  |     bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr);  | 
5199  |  |  | 
5200  | 0  |   case Q_VRRP:  | 
5201  | 0  |     bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr);  | 
5202  |  |  | 
5203  | 0  |   case Q_AARP:  | 
5204  | 0  |     bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr);  | 
5205  |  |  | 
5206  | 0  |   case Q_ISO:  | 
5207  | 0  |     bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr);  | 
5208  |  |  | 
5209  | 0  |   case Q_ESIS:  | 
5210  | 0  |     bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr);  | 
5211  |  |  | 
5212  | 0  |   case Q_ISIS:  | 
5213  | 0  |     bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr);  | 
5214  |  |  | 
5215  | 0  |   case Q_CLNP:  | 
5216  | 0  |     bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr);  | 
5217  |  |  | 
5218  | 0  |   case Q_STP:  | 
5219  | 0  |     bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr);  | 
5220  |  |  | 
5221  | 0  |   case Q_IPX:  | 
5222  | 0  |     bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr);  | 
5223  |  |  | 
5224  | 0  |   case Q_NETBEUI:  | 
5225  | 0  |     bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr);  | 
5226  |  |  | 
5227  | 0  |   case Q_ISIS_L1:  | 
5228  | 0  |     bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr);  | 
5229  |  |  | 
5230  | 0  |   case Q_ISIS_L2:  | 
5231  | 0  |     bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr);  | 
5232  |  |  | 
5233  | 0  |   case Q_ISIS_IIH:  | 
5234  | 0  |     bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr);  | 
5235  |  |  | 
5236  | 0  |   case Q_ISIS_SNP:  | 
5237  | 0  |     bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr);  | 
5238  |  |  | 
5239  | 0  |   case Q_ISIS_CSNP:  | 
5240  | 0  |     bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr);  | 
5241  |  |  | 
5242  | 0  |   case Q_ISIS_PSNP:  | 
5243  | 0  |     bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr);  | 
5244  |  |  | 
5245  | 0  |   case Q_ISIS_LSP:  | 
5246  | 0  |     bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr);  | 
5247  |  |  | 
5248  | 0  |   case Q_RADIO:  | 
5249  | 0  |     bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr);  | 
5250  |  |  | 
5251  | 0  |   case Q_CARP:  | 
5252  | 0  |     bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr);  | 
5253  |  |  | 
5254  | 0  |   default:  | 
5255  | 0  |     abort();  | 
5256  | 0  |   }  | 
5257  |  |   /*NOTREACHED*/  | 
5258  | 0  | }  | 
5259  |  | #endif  | 
5260  |  |  | 
5261  |  | #ifndef INET6  | 
5262  |  | static struct block *  | 
5263  |  | gen_gateway(compiler_state_t *cstate, const u_char *eaddr,  | 
5264  |  |     struct addrinfo *alist, int proto, int dir)  | 
5265  |  | { | 
5266  |  |   struct block *b0, *b1, *tmp;  | 
5267  |  |   struct addrinfo *ai;  | 
5268  |  |   struct sockaddr_in *sin;  | 
5269  |  |  | 
5270  |  |   if (dir != 0)  | 
5271  |  |     bpf_error(cstate, "direction applied to 'gateway'");  | 
5272  |  |  | 
5273  |  |   switch (proto) { | 
5274  |  |   case Q_DEFAULT:  | 
5275  |  |   case Q_IP:  | 
5276  |  |   case Q_ARP:  | 
5277  |  |   case Q_RARP:  | 
5278  |  |     switch (cstate->linktype) { | 
5279  |  |     case DLT_EN10MB:  | 
5280  |  |     case DLT_NETANALYZER:  | 
5281  |  |     case DLT_NETANALYZER_TRANSPARENT:  | 
5282  |  |       b1 = gen_prevlinkhdr_check(cstate);  | 
5283  |  |       b0 = gen_ehostop(cstate, eaddr, Q_OR);  | 
5284  |  |       if (b1 != NULL)  | 
5285  |  |         gen_and(b1, b0);  | 
5286  |  |       break;  | 
5287  |  |     case DLT_FDDI:  | 
5288  |  |       b0 = gen_fhostop(cstate, eaddr, Q_OR);  | 
5289  |  |       break;  | 
5290  |  |     case DLT_IEEE802:  | 
5291  |  |       b0 = gen_thostop(cstate, eaddr, Q_OR);  | 
5292  |  |       break;  | 
5293  |  |     case DLT_IEEE802_11:  | 
5294  |  |     case DLT_PRISM_HEADER:  | 
5295  |  |     case DLT_IEEE802_11_RADIO_AVS:  | 
5296  |  |     case DLT_IEEE802_11_RADIO:  | 
5297  |  |     case DLT_PPI:  | 
5298  |  |       b0 = gen_wlanhostop(cstate, eaddr, Q_OR);  | 
5299  |  |       break;  | 
5300  |  |     case DLT_SUNATM:  | 
5301  |  |       /*  | 
5302  |  |        * This is LLC-multiplexed traffic; if it were  | 
5303  |  |        * LANE, cstate->linktype would have been set to  | 
5304  |  |        * DLT_EN10MB.  | 
5305  |  |        */  | 
5306  |  |       bpf_error(cstate,  | 
5307  |  |           "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");  | 
5308  |  |       break;  | 
5309  |  |     case DLT_IP_OVER_FC:  | 
5310  |  |       b0 = gen_ipfchostop(cstate, eaddr, Q_OR);  | 
5311  |  |       break;  | 
5312  |  |     default:  | 
5313  |  |       bpf_error(cstate,  | 
5314  |  |           "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");  | 
5315  |  |     }  | 
5316  |  |     b1 = NULL;  | 
5317  |  |     for (ai = alist; ai != NULL; ai = ai->ai_next) { | 
5318  |  |       /*  | 
5319  |  |        * Does it have an address?  | 
5320  |  |        */  | 
5321  |  |       if (ai->ai_addr != NULL) { | 
5322  |  |         /*  | 
5323  |  |          * Yes.  Is it an IPv4 address?  | 
5324  |  |          */  | 
5325  |  |         if (ai->ai_addr->sa_family == AF_INET) { | 
5326  |  |           /*  | 
5327  |  |            * Generate an entry for it.  | 
5328  |  |            */  | 
5329  |  |           sin = (struct sockaddr_in *)ai->ai_addr;  | 
5330  |  |           tmp = gen_host(cstate,  | 
5331  |  |               ntohl(sin->sin_addr.s_addr),  | 
5332  |  |               0xffffffff, proto, Q_OR, Q_HOST);  | 
5333  |  |           /*  | 
5334  |  |            * Is it the *first* IPv4 address?  | 
5335  |  |            */  | 
5336  |  |           if (b1 == NULL) { | 
5337  |  |             /*  | 
5338  |  |              * Yes, so start with it.  | 
5339  |  |              */  | 
5340  |  |             b1 = tmp;  | 
5341  |  |           } else { | 
5342  |  |             /*  | 
5343  |  |              * No, so OR it into the  | 
5344  |  |              * existing set of  | 
5345  |  |              * addresses.  | 
5346  |  |              */  | 
5347  |  |             gen_or(b1, tmp);  | 
5348  |  |             b1 = tmp;  | 
5349  |  |           }  | 
5350  |  |         }  | 
5351  |  |       }  | 
5352  |  |     }  | 
5353  |  |     if (b1 == NULL) { | 
5354  |  |       /*  | 
5355  |  |        * No IPv4 addresses found.  | 
5356  |  |        */  | 
5357  |  |       return (NULL);  | 
5358  |  |     }  | 
5359  |  |     gen_not(b1);  | 
5360  |  |     gen_and(b0, b1);  | 
5361  |  |     return b1;  | 
5362  |  |   }  | 
5363  |  |   bpf_error(cstate, "illegal modifier of 'gateway'");  | 
5364  |  |   /*NOTREACHED*/  | 
5365  |  | }  | 
5366  |  | #endif  | 
5367  |  |  | 
5368  |  | static struct block *  | 
5369  |  | gen_proto_abbrev_internal(compiler_state_t *cstate, int proto)  | 
5370  | 1.47k  | { | 
5371  | 1.47k  |   struct block *b0;  | 
5372  | 1.47k  |   struct block *b1;  | 
5373  |  |  | 
5374  | 1.47k  |   switch (proto) { | 
5375  |  |  | 
5376  | 0  |   case Q_SCTP:  | 
5377  | 0  |     b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT);  | 
5378  | 0  |     break;  | 
5379  |  |  | 
5380  | 125  |   case Q_TCP:  | 
5381  | 125  |     b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT);  | 
5382  | 125  |     break;  | 
5383  |  |  | 
5384  | 0  |   case Q_UDP:  | 
5385  | 0  |     b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT);  | 
5386  | 0  |     break;  | 
5387  |  |  | 
5388  | 0  |   case Q_ICMP:  | 
5389  | 0  |     b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT);  | 
5390  | 0  |     break;  | 
5391  |  |  | 
5392  |  | #ifndef IPPROTO_IGMP  | 
5393  |  | #define IPPROTO_IGMP  2  | 
5394  |  | #endif  | 
5395  |  |  | 
5396  | 0  |   case Q_IGMP:  | 
5397  | 0  |     b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT);  | 
5398  | 0  |     break;  | 
5399  |  |  | 
5400  | 0  | #ifndef IPPROTO_IGRP  | 
5401  | 0  | #define IPPROTO_IGRP  9  | 
5402  | 0  | #endif  | 
5403  | 0  |   case Q_IGRP:  | 
5404  | 0  |     b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT);  | 
5405  | 0  |     break;  | 
5406  |  |  | 
5407  |  | #ifndef IPPROTO_PIM  | 
5408  |  | #define IPPROTO_PIM 103  | 
5409  |  | #endif  | 
5410  |  |  | 
5411  | 0  |   case Q_PIM:  | 
5412  | 0  |     b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT);  | 
5413  | 0  |     break;  | 
5414  |  |  | 
5415  | 0  | #ifndef IPPROTO_VRRP  | 
5416  | 0  | #define IPPROTO_VRRP  112  | 
5417  | 0  | #endif  | 
5418  |  |  | 
5419  | 0  |   case Q_VRRP:  | 
5420  | 0  |     b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT);  | 
5421  | 0  |     break;  | 
5422  |  |  | 
5423  | 0  | #ifndef IPPROTO_CARP  | 
5424  | 0  | #define IPPROTO_CARP  112  | 
5425  | 0  | #endif  | 
5426  |  |  | 
5427  | 0  |   case Q_CARP:  | 
5428  | 0  |     b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT);  | 
5429  | 0  |     break;  | 
5430  |  |  | 
5431  | 7  |   case Q_IP:  | 
5432  | 7  |     b1 = gen_linktype(cstate, ETHERTYPE_IP);  | 
5433  | 7  |     break;  | 
5434  |  |  | 
5435  | 0  |   case Q_ARP:  | 
5436  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_ARP);  | 
5437  | 0  |     break;  | 
5438  |  |  | 
5439  | 0  |   case Q_RARP:  | 
5440  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_REVARP);  | 
5441  | 0  |     break;  | 
5442  |  |  | 
5443  | 2  |   case Q_LINK:  | 
5444  | 2  |     bpf_error(cstate, "link layer applied in wrong context");  | 
5445  |  |  | 
5446  | 0  |   case Q_ATALK:  | 
5447  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_ATALK);  | 
5448  | 0  |     break;  | 
5449  |  |  | 
5450  | 0  |   case Q_AARP:  | 
5451  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_AARP);  | 
5452  | 0  |     break;  | 
5453  |  |  | 
5454  | 0  |   case Q_DECNET:  | 
5455  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_DN);  | 
5456  | 0  |     break;  | 
5457  |  |  | 
5458  | 10  |   case Q_SCA:  | 
5459  | 10  |     b1 = gen_linktype(cstate, ETHERTYPE_SCA);  | 
5460  | 10  |     break;  | 
5461  |  |  | 
5462  | 0  |   case Q_LAT:  | 
5463  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_LAT);  | 
5464  | 0  |     break;  | 
5465  |  |  | 
5466  | 0  |   case Q_MOPDL:  | 
5467  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_MOPDL);  | 
5468  | 0  |     break;  | 
5469  |  |  | 
5470  | 0  |   case Q_MOPRC:  | 
5471  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_MOPRC);  | 
5472  | 0  |     break;  | 
5473  |  |  | 
5474  | 0  |   case Q_IPV6:  | 
5475  | 0  |     b1 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
5476  | 0  |     break;  | 
5477  |  |  | 
5478  |  | #ifndef IPPROTO_ICMPV6  | 
5479  |  | #define IPPROTO_ICMPV6  58  | 
5480  |  | #endif  | 
5481  | 0  |   case Q_ICMPV6:  | 
5482  | 0  |     b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);  | 
5483  | 0  |     break;  | 
5484  |  |  | 
5485  |  | #ifndef IPPROTO_AH  | 
5486  |  | #define IPPROTO_AH  51  | 
5487  |  | #endif  | 
5488  | 0  |   case Q_AH:  | 
5489  | 0  |     b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT);  | 
5490  | 0  |     break;  | 
5491  |  |  | 
5492  |  | #ifndef IPPROTO_ESP  | 
5493  |  | #define IPPROTO_ESP 50  | 
5494  |  | #endif  | 
5495  | 203  |   case Q_ESP:  | 
5496  | 203  |     b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT);  | 
5497  | 203  |     break;  | 
5498  |  |  | 
5499  | 14  |   case Q_ISO:  | 
5500  | 14  |     b1 = gen_linktype(cstate, LLCSAP_ISONS);  | 
5501  | 14  |     break;  | 
5502  |  |  | 
5503  | 430  |   case Q_ESIS:  | 
5504  | 430  |     b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT);  | 
5505  | 430  |     break;  | 
5506  |  |  | 
5507  | 187  |   case Q_ISIS:  | 
5508  | 187  |     b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);  | 
5509  | 187  |     break;  | 
5510  |  |  | 
5511  | 53  |   case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */  | 
5512  | 53  |     b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);  | 
5513  | 53  |     b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */  | 
5514  | 53  |     gen_or(b0, b1);  | 
5515  | 53  |     b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);  | 
5516  | 53  |     gen_or(b0, b1);  | 
5517  | 53  |     b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);  | 
5518  | 53  |     gen_or(b0, b1);  | 
5519  | 53  |     b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);  | 
5520  | 53  |     gen_or(b0, b1);  | 
5521  | 53  |     break;  | 
5522  |  |  | 
5523  | 9  |   case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */  | 
5524  | 9  |     b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);  | 
5525  | 9  |     b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */  | 
5526  | 9  |     gen_or(b0, b1);  | 
5527  | 9  |     b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);  | 
5528  | 9  |     gen_or(b0, b1);  | 
5529  | 9  |     b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);  | 
5530  | 9  |     gen_or(b0, b1);  | 
5531  | 9  |     b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);  | 
5532  | 9  |     gen_or(b0, b1);  | 
5533  | 9  |     break;  | 
5534  |  |  | 
5535  | 0  |   case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */  | 
5536  | 0  |     b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);  | 
5537  | 0  |     b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);  | 
5538  | 0  |     gen_or(b0, b1);  | 
5539  | 0  |     b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);  | 
5540  | 0  |     gen_or(b0, b1);  | 
5541  | 0  |     break;  | 
5542  |  |  | 
5543  | 65  |   case Q_ISIS_LSP:  | 
5544  | 65  |     b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);  | 
5545  | 65  |     b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);  | 
5546  | 65  |     gen_or(b0, b1);  | 
5547  | 65  |     break;  | 
5548  |  |  | 
5549  | 210  |   case Q_ISIS_SNP:  | 
5550  | 210  |     b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);  | 
5551  | 210  |     b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);  | 
5552  | 210  |     gen_or(b0, b1);  | 
5553  | 210  |     b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);  | 
5554  | 210  |     gen_or(b0, b1);  | 
5555  | 210  |     b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);  | 
5556  | 210  |     gen_or(b0, b1);  | 
5557  | 210  |     break;  | 
5558  |  |  | 
5559  | 80  |   case Q_ISIS_CSNP:  | 
5560  | 80  |     b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);  | 
5561  | 80  |     b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);  | 
5562  | 80  |     gen_or(b0, b1);  | 
5563  | 80  |     break;  | 
5564  |  |  | 
5565  | 30  |   case Q_ISIS_PSNP:  | 
5566  | 30  |     b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);  | 
5567  | 30  |     b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);  | 
5568  | 30  |     gen_or(b0, b1);  | 
5569  | 30  |     break;  | 
5570  |  |  | 
5571  | 24  |   case Q_CLNP:  | 
5572  | 24  |     b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT);  | 
5573  | 24  |     break;  | 
5574  |  |  | 
5575  | 26  |   case Q_STP:  | 
5576  | 26  |     b1 = gen_linktype(cstate, LLCSAP_8021D);  | 
5577  | 26  |     break;  | 
5578  |  |  | 
5579  | 0  |   case Q_IPX:  | 
5580  | 0  |     b1 = gen_linktype(cstate, LLCSAP_IPX);  | 
5581  | 0  |     break;  | 
5582  |  |  | 
5583  | 0  |   case Q_NETBEUI:  | 
5584  | 0  |     b1 = gen_linktype(cstate, LLCSAP_NETBEUI);  | 
5585  | 0  |     break;  | 
5586  |  |  | 
5587  | 0  |   case Q_RADIO:  | 
5588  | 0  |     bpf_error(cstate, "'radio' is not a valid protocol type");  | 
5589  |  |  | 
5590  | 0  |   default:  | 
5591  | 0  |     abort();  | 
5592  | 1.47k  |   }  | 
5593  | 1.33k  |   return b1;  | 
5594  | 1.47k  | }  | 
5595  |  |  | 
5596  |  | struct block *  | 
5597  |  | gen_proto_abbrev(compiler_state_t *cstate, int proto)  | 
5598  | 1.47k  | { | 
5599  |  |   /*  | 
5600  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
5601  |  |    * on an error.  | 
5602  |  |    */  | 
5603  | 1.47k  |   if (setjmp(cstate->top_ctx))  | 
5604  | 137  |     return (NULL);  | 
5605  |  |  | 
5606  | 1.33k  |   return gen_proto_abbrev_internal(cstate, proto);  | 
5607  | 1.47k  | }  | 
5608  |  |  | 
5609  |  | static struct block *  | 
5610  |  | gen_ipfrag(compiler_state_t *cstate)  | 
5611  | 0  | { | 
5612  | 0  |   struct slist *s;  | 
5613  | 0  |   struct block *b;  | 
5614  |  |  | 
5615  |  |   /* not IPv4 frag other than the first frag */  | 
5616  | 0  |   s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H);  | 
5617  | 0  |   b = new_block(cstate, JMP(BPF_JSET));  | 
5618  | 0  |   b->s.k = 0x1fff;  | 
5619  | 0  |   b->stmts = s;  | 
5620  | 0  |   gen_not(b);  | 
5621  |  | 
  | 
5622  | 0  |   return b;  | 
5623  | 0  | }  | 
5624  |  |  | 
5625  |  | /*  | 
5626  |  |  * Generate a comparison to a port value in the transport-layer header  | 
5627  |  |  * at the specified offset from the beginning of that header.  | 
5628  |  |  *  | 
5629  |  |  * XXX - this handles a variable-length prefix preceding the link-layer  | 
5630  |  |  * header, such as the radiotap or AVS radio prefix, but doesn't handle  | 
5631  |  |  * variable-length link-layer headers (such as Token Ring or 802.11  | 
5632  |  |  * headers).  | 
5633  |  |  */  | 
5634  |  | static struct block *  | 
5635  |  | gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v)  | 
5636  | 0  | { | 
5637  | 0  |   return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v);  | 
5638  | 0  | }  | 
5639  |  |  | 
5640  |  | static struct block *  | 
5641  |  | gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v)  | 
5642  | 0  | { | 
5643  | 0  |   return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v);  | 
5644  | 0  | }  | 
5645  |  |  | 
5646  |  | static struct block *  | 
5647  |  | gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir)  | 
5648  | 0  | { | 
5649  | 0  |   struct block *b0, *b1, *tmp;  | 
5650  |  |  | 
5651  |  |   /* ip proto 'proto' and not a fragment other than the first fragment */  | 
5652  | 0  |   tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);  | 
5653  | 0  |   b0 = gen_ipfrag(cstate);  | 
5654  | 0  |   gen_and(tmp, b0);  | 
5655  |  | 
  | 
5656  | 0  |   switch (dir) { | 
5657  | 0  |   case Q_SRC:  | 
5658  | 0  |     b1 = gen_portatom(cstate, 0, port);  | 
5659  | 0  |     break;  | 
5660  |  |  | 
5661  | 0  |   case Q_DST:  | 
5662  | 0  |     b1 = gen_portatom(cstate, 2, port);  | 
5663  | 0  |     break;  | 
5664  |  |  | 
5665  | 0  |   case Q_AND:  | 
5666  | 0  |     tmp = gen_portatom(cstate, 0, port);  | 
5667  | 0  |     b1 = gen_portatom(cstate, 2, port);  | 
5668  | 0  |     gen_and(tmp, b1);  | 
5669  | 0  |     break;  | 
5670  |  |  | 
5671  | 0  |   case Q_DEFAULT:  | 
5672  | 0  |   case Q_OR:  | 
5673  | 0  |     tmp = gen_portatom(cstate, 0, port);  | 
5674  | 0  |     b1 = gen_portatom(cstate, 2, port);  | 
5675  | 0  |     gen_or(tmp, b1);  | 
5676  | 0  |     break;  | 
5677  |  |  | 
5678  | 0  |   case Q_ADDR1:  | 
5679  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports");  | 
5680  |  |     /*NOTREACHED*/  | 
5681  |  |  | 
5682  | 0  |   case Q_ADDR2:  | 
5683  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports");  | 
5684  |  |     /*NOTREACHED*/  | 
5685  |  |  | 
5686  | 0  |   case Q_ADDR3:  | 
5687  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports");  | 
5688  |  |     /*NOTREACHED*/  | 
5689  |  |  | 
5690  | 0  |   case Q_ADDR4:  | 
5691  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports");  | 
5692  |  |     /*NOTREACHED*/  | 
5693  |  |  | 
5694  | 0  |   case Q_RA:  | 
5695  | 0  |     bpf_error(cstate, "'ra' is not a valid qualifier for ports");  | 
5696  |  |     /*NOTREACHED*/  | 
5697  |  |  | 
5698  | 0  |   case Q_TA:  | 
5699  | 0  |     bpf_error(cstate, "'ta' is not a valid qualifier for ports");  | 
5700  |  |     /*NOTREACHED*/  | 
5701  |  |  | 
5702  | 0  |   default:  | 
5703  | 0  |     abort();  | 
5704  |  |     /*NOTREACHED*/  | 
5705  | 0  |   }  | 
5706  | 0  |   gen_and(b0, b1);  | 
5707  |  | 
  | 
5708  | 0  |   return b1;  | 
5709  | 0  | }  | 
5710  |  |  | 
5711  |  | static struct block *  | 
5712  |  | gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir)  | 
5713  | 0  | { | 
5714  | 0  |   struct block *b0, *b1, *tmp;  | 
5715  |  |  | 
5716  |  |   /*  | 
5717  |  |    * ether proto ip  | 
5718  |  |    *  | 
5719  |  |    * For FDDI, RFC 1188 says that SNAP encapsulation is used,  | 
5720  |  |    * not LLC encapsulation with LLCSAP_IP.  | 
5721  |  |    *  | 
5722  |  |    * For IEEE 802 networks - which includes 802.5 token ring  | 
5723  |  |    * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042  | 
5724  |  |    * says that SNAP encapsulation is used, not LLC encapsulation  | 
5725  |  |    * with LLCSAP_IP.  | 
5726  |  |    *  | 
5727  |  |    * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and  | 
5728  |  |    * RFC 2225 say that SNAP encapsulation is used, not LLC  | 
5729  |  |    * encapsulation with LLCSAP_IP.  | 
5730  |  |    *  | 
5731  |  |    * So we always check for ETHERTYPE_IP.  | 
5732  |  |    */  | 
5733  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
5734  |  | 
  | 
5735  | 0  |   switch (ip_proto) { | 
5736  | 0  |   case IPPROTO_UDP:  | 
5737  | 0  |   case IPPROTO_TCP:  | 
5738  | 0  |   case IPPROTO_SCTP:  | 
5739  | 0  |     b1 = gen_portop(cstate, port, (u_int)ip_proto, dir);  | 
5740  | 0  |     break;  | 
5741  |  |  | 
5742  | 0  |   case PROTO_UNDEF:  | 
5743  | 0  |     tmp = gen_portop(cstate, port, IPPROTO_TCP, dir);  | 
5744  | 0  |     b1 = gen_portop(cstate, port, IPPROTO_UDP, dir);  | 
5745  | 0  |     gen_or(tmp, b1);  | 
5746  | 0  |     tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir);  | 
5747  | 0  |     gen_or(tmp, b1);  | 
5748  | 0  |     break;  | 
5749  |  |  | 
5750  | 0  |   default:  | 
5751  | 0  |     abort();  | 
5752  | 0  |   }  | 
5753  | 0  |   gen_and(b0, b1);  | 
5754  | 0  |   return b1;  | 
5755  | 0  | }  | 
5756  |  |  | 
5757  |  | struct block *  | 
5758  |  | gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir)  | 
5759  | 0  | { | 
5760  | 0  |   struct block *b0, *b1, *tmp;  | 
5761  |  |  | 
5762  |  |   /* ip6 proto 'proto' */  | 
5763  |  |   /* XXX - catch the first fragment of a fragmented packet? */  | 
5764  | 0  |   b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);  | 
5765  |  | 
  | 
5766  | 0  |   switch (dir) { | 
5767  | 0  |   case Q_SRC:  | 
5768  | 0  |     b1 = gen_portatom6(cstate, 0, port);  | 
5769  | 0  |     break;  | 
5770  |  |  | 
5771  | 0  |   case Q_DST:  | 
5772  | 0  |     b1 = gen_portatom6(cstate, 2, port);  | 
5773  | 0  |     break;  | 
5774  |  |  | 
5775  | 0  |   case Q_AND:  | 
5776  | 0  |     tmp = gen_portatom6(cstate, 0, port);  | 
5777  | 0  |     b1 = gen_portatom6(cstate, 2, port);  | 
5778  | 0  |     gen_and(tmp, b1);  | 
5779  | 0  |     break;  | 
5780  |  |  | 
5781  | 0  |   case Q_DEFAULT:  | 
5782  | 0  |   case Q_OR:  | 
5783  | 0  |     tmp = gen_portatom6(cstate, 0, port);  | 
5784  | 0  |     b1 = gen_portatom6(cstate, 2, port);  | 
5785  | 0  |     gen_or(tmp, b1);  | 
5786  | 0  |     break;  | 
5787  |  |  | 
5788  | 0  |   default:  | 
5789  | 0  |     abort();  | 
5790  | 0  |   }  | 
5791  | 0  |   gen_and(b0, b1);  | 
5792  |  | 
  | 
5793  | 0  |   return b1;  | 
5794  | 0  | }  | 
5795  |  |  | 
5796  |  | static struct block *  | 
5797  |  | gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir)  | 
5798  | 0  | { | 
5799  | 0  |   struct block *b0, *b1, *tmp;  | 
5800  |  |  | 
5801  |  |   /* link proto ip6 */  | 
5802  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
5803  |  | 
  | 
5804  | 0  |   switch (ip_proto) { | 
5805  | 0  |   case IPPROTO_UDP:  | 
5806  | 0  |   case IPPROTO_TCP:  | 
5807  | 0  |   case IPPROTO_SCTP:  | 
5808  | 0  |     b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir);  | 
5809  | 0  |     break;  | 
5810  |  |  | 
5811  | 0  |   case PROTO_UNDEF:  | 
5812  | 0  |     tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir);  | 
5813  | 0  |     b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir);  | 
5814  | 0  |     gen_or(tmp, b1);  | 
5815  | 0  |     tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir);  | 
5816  | 0  |     gen_or(tmp, b1);  | 
5817  | 0  |     break;  | 
5818  |  |  | 
5819  | 0  |   default:  | 
5820  | 0  |     abort();  | 
5821  | 0  |   }  | 
5822  | 0  |   gen_and(b0, b1);  | 
5823  | 0  |   return b1;  | 
5824  | 0  | }  | 
5825  |  |  | 
5826  |  | /* gen_portrange code */  | 
5827  |  | static struct block *  | 
5828  |  | gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,  | 
5829  |  |     bpf_u_int32 v2)  | 
5830  | 0  | { | 
5831  | 0  |   struct block *b1, *b2;  | 
5832  |  | 
  | 
5833  | 0  |   if (v1 > v2) { | 
5834  |  |     /*  | 
5835  |  |      * Reverse the order of the ports, so v1 is the lower one.  | 
5836  |  |      */  | 
5837  | 0  |     bpf_u_int32 vtemp;  | 
5838  |  | 
  | 
5839  | 0  |     vtemp = v1;  | 
5840  | 0  |     v1 = v2;  | 
5841  | 0  |     v2 = vtemp;  | 
5842  | 0  |   }  | 
5843  |  | 
  | 
5844  | 0  |   b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1);  | 
5845  | 0  |   b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2);  | 
5846  |  | 
  | 
5847  | 0  |   gen_and(b1, b2);  | 
5848  |  | 
  | 
5849  | 0  |   return b2;  | 
5850  | 0  | }  | 
5851  |  |  | 
5852  |  | static struct block *  | 
5853  |  | gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2,  | 
5854  |  |     bpf_u_int32 proto, int dir)  | 
5855  | 0  | { | 
5856  | 0  |   struct block *b0, *b1, *tmp;  | 
5857  |  |  | 
5858  |  |   /* ip proto 'proto' and not a fragment other than the first fragment */  | 
5859  | 0  |   tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto);  | 
5860  | 0  |   b0 = gen_ipfrag(cstate);  | 
5861  | 0  |   gen_and(tmp, b0);  | 
5862  |  | 
  | 
5863  | 0  |   switch (dir) { | 
5864  | 0  |   case Q_SRC:  | 
5865  | 0  |     b1 = gen_portrangeatom(cstate, 0, port1, port2);  | 
5866  | 0  |     break;  | 
5867  |  |  | 
5868  | 0  |   case Q_DST:  | 
5869  | 0  |     b1 = gen_portrangeatom(cstate, 2, port1, port2);  | 
5870  | 0  |     break;  | 
5871  |  |  | 
5872  | 0  |   case Q_AND:  | 
5873  | 0  |     tmp = gen_portrangeatom(cstate, 0, port1, port2);  | 
5874  | 0  |     b1 = gen_portrangeatom(cstate, 2, port1, port2);  | 
5875  | 0  |     gen_and(tmp, b1);  | 
5876  | 0  |     break;  | 
5877  |  |  | 
5878  | 0  |   case Q_DEFAULT:  | 
5879  | 0  |   case Q_OR:  | 
5880  | 0  |     tmp = gen_portrangeatom(cstate, 0, port1, port2);  | 
5881  | 0  |     b1 = gen_portrangeatom(cstate, 2, port1, port2);  | 
5882  | 0  |     gen_or(tmp, b1);  | 
5883  | 0  |     break;  | 
5884  |  |  | 
5885  | 0  |   case Q_ADDR1:  | 
5886  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges");  | 
5887  |  |     /*NOTREACHED*/  | 
5888  |  |  | 
5889  | 0  |   case Q_ADDR2:  | 
5890  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges");  | 
5891  |  |     /*NOTREACHED*/  | 
5892  |  |  | 
5893  | 0  |   case Q_ADDR3:  | 
5894  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges");  | 
5895  |  |     /*NOTREACHED*/  | 
5896  |  |  | 
5897  | 0  |   case Q_ADDR4:  | 
5898  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges");  | 
5899  |  |     /*NOTREACHED*/  | 
5900  |  |  | 
5901  | 0  |   case Q_RA:  | 
5902  | 0  |     bpf_error(cstate, "'ra' is not a valid qualifier for port ranges");  | 
5903  |  |     /*NOTREACHED*/  | 
5904  |  |  | 
5905  | 0  |   case Q_TA:  | 
5906  | 0  |     bpf_error(cstate, "'ta' is not a valid qualifier for port ranges");  | 
5907  |  |     /*NOTREACHED*/  | 
5908  |  |  | 
5909  | 0  |   default:  | 
5910  | 0  |     abort();  | 
5911  |  |     /*NOTREACHED*/  | 
5912  | 0  |   }  | 
5913  | 0  |   gen_and(b0, b1);  | 
5914  |  | 
  | 
5915  | 0  |   return b1;  | 
5916  | 0  | }  | 
5917  |  |  | 
5918  |  | static struct block *  | 
5919  |  | gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,  | 
5920  |  |     int dir)  | 
5921  | 0  | { | 
5922  | 0  |   struct block *b0, *b1, *tmp;  | 
5923  |  |  | 
5924  |  |   /* link proto ip */  | 
5925  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
5926  |  | 
  | 
5927  | 0  |   switch (ip_proto) { | 
5928  | 0  |   case IPPROTO_UDP:  | 
5929  | 0  |   case IPPROTO_TCP:  | 
5930  | 0  |   case IPPROTO_SCTP:  | 
5931  | 0  |     b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto,  | 
5932  | 0  |         dir);  | 
5933  | 0  |     break;  | 
5934  |  |  | 
5935  | 0  |   case PROTO_UNDEF:  | 
5936  | 0  |     tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir);  | 
5937  | 0  |     b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir);  | 
5938  | 0  |     gen_or(tmp, b1);  | 
5939  | 0  |     tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir);  | 
5940  | 0  |     gen_or(tmp, b1);  | 
5941  | 0  |     break;  | 
5942  |  |  | 
5943  | 0  |   default:  | 
5944  | 0  |     abort();  | 
5945  | 0  |   }  | 
5946  | 0  |   gen_and(b0, b1);  | 
5947  | 0  |   return b1;  | 
5948  | 0  | }  | 
5949  |  |  | 
5950  |  | static struct block *  | 
5951  |  | gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1,  | 
5952  |  |     bpf_u_int32 v2)  | 
5953  | 0  | { | 
5954  | 0  |   struct block *b1, *b2;  | 
5955  |  | 
  | 
5956  | 0  |   if (v1 > v2) { | 
5957  |  |     /*  | 
5958  |  |      * Reverse the order of the ports, so v1 is the lower one.  | 
5959  |  |      */  | 
5960  | 0  |     bpf_u_int32 vtemp;  | 
5961  |  | 
  | 
5962  | 0  |     vtemp = v1;  | 
5963  | 0  |     v1 = v2;  | 
5964  | 0  |     v2 = vtemp;  | 
5965  | 0  |   }  | 
5966  |  | 
  | 
5967  | 0  |   b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1);  | 
5968  | 0  |   b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2);  | 
5969  |  | 
  | 
5970  | 0  |   gen_and(b1, b2);  | 
5971  |  | 
  | 
5972  | 0  |   return b2;  | 
5973  | 0  | }  | 
5974  |  |  | 
5975  |  | static struct block *  | 
5976  |  | gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2,  | 
5977  |  |     bpf_u_int32 proto, int dir)  | 
5978  | 0  | { | 
5979  | 0  |   struct block *b0, *b1, *tmp;  | 
5980  |  |  | 
5981  |  |   /* ip6 proto 'proto' */  | 
5982  |  |   /* XXX - catch the first fragment of a fragmented packet? */  | 
5983  | 0  |   b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto);  | 
5984  |  | 
  | 
5985  | 0  |   switch (dir) { | 
5986  | 0  |   case Q_SRC:  | 
5987  | 0  |     b1 = gen_portrangeatom6(cstate, 0, port1, port2);  | 
5988  | 0  |     break;  | 
5989  |  |  | 
5990  | 0  |   case Q_DST:  | 
5991  | 0  |     b1 = gen_portrangeatom6(cstate, 2, port1, port2);  | 
5992  | 0  |     break;  | 
5993  |  |  | 
5994  | 0  |   case Q_AND:  | 
5995  | 0  |     tmp = gen_portrangeatom6(cstate, 0, port1, port2);  | 
5996  | 0  |     b1 = gen_portrangeatom6(cstate, 2, port1, port2);  | 
5997  | 0  |     gen_and(tmp, b1);  | 
5998  | 0  |     break;  | 
5999  |  |  | 
6000  | 0  |   case Q_DEFAULT:  | 
6001  | 0  |   case Q_OR:  | 
6002  | 0  |     tmp = gen_portrangeatom6(cstate, 0, port1, port2);  | 
6003  | 0  |     b1 = gen_portrangeatom6(cstate, 2, port1, port2);  | 
6004  | 0  |     gen_or(tmp, b1);  | 
6005  | 0  |     break;  | 
6006  |  |  | 
6007  | 0  |   default:  | 
6008  | 0  |     abort();  | 
6009  | 0  |   }  | 
6010  | 0  |   gen_and(b0, b1);  | 
6011  |  | 
  | 
6012  | 0  |   return b1;  | 
6013  | 0  | }  | 
6014  |  |  | 
6015  |  | static struct block *  | 
6016  |  | gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto,  | 
6017  |  |     int dir)  | 
6018  | 0  | { | 
6019  | 0  |   struct block *b0, *b1, *tmp;  | 
6020  |  |  | 
6021  |  |   /* link proto ip6 */  | 
6022  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
6023  |  | 
  | 
6024  | 0  |   switch (ip_proto) { | 
6025  | 0  |   case IPPROTO_UDP:  | 
6026  | 0  |   case IPPROTO_TCP:  | 
6027  | 0  |   case IPPROTO_SCTP:  | 
6028  | 0  |     b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto,  | 
6029  | 0  |         dir);  | 
6030  | 0  |     break;  | 
6031  |  |  | 
6032  | 0  |   case PROTO_UNDEF:  | 
6033  | 0  |     tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir);  | 
6034  | 0  |     b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir);  | 
6035  | 0  |     gen_or(tmp, b1);  | 
6036  | 0  |     tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir);  | 
6037  | 0  |     gen_or(tmp, b1);  | 
6038  | 0  |     break;  | 
6039  |  |  | 
6040  | 0  |   default:  | 
6041  | 0  |     abort();  | 
6042  | 0  |   }  | 
6043  | 0  |   gen_and(b0, b1);  | 
6044  | 0  |   return b1;  | 
6045  | 0  | }  | 
6046  |  |  | 
6047  |  | static int  | 
6048  |  | lookup_proto(compiler_state_t *cstate, const char *name, int proto)  | 
6049  | 0  | { | 
6050  | 0  |   register int v;  | 
6051  |  | 
  | 
6052  | 0  |   switch (proto) { | 
6053  |  |  | 
6054  | 0  |   case Q_DEFAULT:  | 
6055  | 0  |   case Q_IP:  | 
6056  | 0  |   case Q_IPV6:  | 
6057  | 0  |     v = pcap_nametoproto(name);  | 
6058  | 0  |     if (v == PROTO_UNDEF)  | 
6059  | 0  |       bpf_error(cstate, "unknown ip proto '%s'", name);  | 
6060  | 0  |     break;  | 
6061  |  |  | 
6062  | 0  |   case Q_LINK:  | 
6063  |  |     /* XXX should look up h/w protocol type based on cstate->linktype */  | 
6064  | 0  |     v = pcap_nametoeproto(name);  | 
6065  | 0  |     if (v == PROTO_UNDEF) { | 
6066  | 0  |       v = pcap_nametollc(name);  | 
6067  | 0  |       if (v == PROTO_UNDEF)  | 
6068  | 0  |         bpf_error(cstate, "unknown ether proto '%s'", name);  | 
6069  | 0  |     }  | 
6070  | 0  |     break;  | 
6071  |  |  | 
6072  | 0  |   case Q_ISO:  | 
6073  | 0  |     if (strcmp(name, "esis") == 0)  | 
6074  | 0  |       v = ISO9542_ESIS;  | 
6075  | 0  |     else if (strcmp(name, "isis") == 0)  | 
6076  | 0  |       v = ISO10589_ISIS;  | 
6077  | 0  |     else if (strcmp(name, "clnp") == 0)  | 
6078  | 0  |       v = ISO8473_CLNP;  | 
6079  | 0  |     else  | 
6080  | 0  |       bpf_error(cstate, "unknown osi proto '%s'", name);  | 
6081  | 0  |     break;  | 
6082  |  |  | 
6083  | 0  |   default:  | 
6084  | 0  |     v = PROTO_UNDEF;  | 
6085  | 0  |     break;  | 
6086  | 0  |   }  | 
6087  | 0  |   return v;  | 
6088  | 0  | }  | 
6089  |  |  | 
6090  |  | #if !defined(NO_PROTOCHAIN)  | 
6091  |  | static struct block *  | 
6092  |  | gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto)  | 
6093  | 0  | { | 
6094  | 0  |   struct block *b0, *b;  | 
6095  | 0  |   struct slist *s[100];  | 
6096  | 0  |   int fix2, fix3, fix4, fix5;  | 
6097  | 0  |   int ahcheck, again, end;  | 
6098  | 0  |   int i, max;  | 
6099  | 0  |   int reg2 = alloc_reg(cstate);  | 
6100  |  | 
  | 
6101  | 0  |   memset(s, 0, sizeof(s));  | 
6102  | 0  |   fix3 = fix4 = fix5 = 0;  | 
6103  |  | 
  | 
6104  | 0  |   switch (proto) { | 
6105  | 0  |   case Q_IP:  | 
6106  | 0  |   case Q_IPV6:  | 
6107  | 0  |     break;  | 
6108  | 0  |   case Q_DEFAULT:  | 
6109  | 0  |     b0 = gen_protochain(cstate, v, Q_IP);  | 
6110  | 0  |     b = gen_protochain(cstate, v, Q_IPV6);  | 
6111  | 0  |     gen_or(b0, b);  | 
6112  | 0  |     return b;  | 
6113  | 0  |   default:  | 
6114  | 0  |     bpf_error(cstate, "bad protocol applied for 'protochain'");  | 
6115  |  |     /*NOTREACHED*/  | 
6116  | 0  |   }  | 
6117  |  |  | 
6118  |  |   /*  | 
6119  |  |    * We don't handle variable-length prefixes before the link-layer  | 
6120  |  |    * header, or variable-length link-layer headers, here yet.  | 
6121  |  |    * We might want to add BPF instructions to do the protochain  | 
6122  |  |    * work, to simplify that and, on platforms that have a BPF  | 
6123  |  |    * interpreter with the new instructions, let the filtering  | 
6124  |  |    * be done in the kernel.  (We already require a modified BPF  | 
6125  |  |    * engine to do the protochain stuff, to support backward  | 
6126  |  |    * branches, and backward branch support is unlikely to appear  | 
6127  |  |    * in kernel BPF engines.)  | 
6128  |  |    */  | 
6129  | 0  |   if (cstate->off_linkpl.is_variable)  | 
6130  | 0  |     bpf_error(cstate, "'protochain' not supported with variable length headers");  | 
6131  |  |  | 
6132  |  |   /*  | 
6133  |  |    * To quote a comment in optimize.c:  | 
6134  |  |    *  | 
6135  |  |    * "These data structures are used in a Cocke and Shwarz style  | 
6136  |  |    * value numbering scheme.  Since the flowgraph is acyclic,  | 
6137  |  |    * exit values can be propagated from a node's predecessors  | 
6138  |  |    * provided it is uniquely defined."  | 
6139  |  |    *  | 
6140  |  |    * "Acyclic" means "no backward branches", which means "no  | 
6141  |  |    * loops", so we have to turn the optimizer off.  | 
6142  |  |    */  | 
6143  | 0  |   cstate->no_optimize = 1;  | 
6144  |  |  | 
6145  |  |   /*  | 
6146  |  |    * s[0] is a dummy entry to protect other BPF insn from damage  | 
6147  |  |    * by s[fix] = foo with uninitialized variable "fix".  It is somewhat  | 
6148  |  |    * hard to find interdependency made by jump table fixup.  | 
6149  |  |    */  | 
6150  | 0  |   i = 0;  | 
6151  | 0  |   s[i] = new_stmt(cstate, 0); /*dummy*/  | 
6152  | 0  |   i++;  | 
6153  |  | 
  | 
6154  | 0  |   switch (proto) { | 
6155  | 0  |   case Q_IP:  | 
6156  | 0  |     b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
6157  |  |  | 
6158  |  |     /* A = ip->ip_p */  | 
6159  | 0  |     s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);  | 
6160  | 0  |     s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9;  | 
6161  | 0  |     i++;  | 
6162  |  |     /* X = ip->ip_hl << 2 */  | 
6163  | 0  |     s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B);  | 
6164  | 0  |     s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
6165  | 0  |     i++;  | 
6166  | 0  |     break;  | 
6167  |  |  | 
6168  | 0  |   case Q_IPV6:  | 
6169  | 0  |     b0 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
6170  |  |  | 
6171  |  |     /* A = ip6->ip_nxt */  | 
6172  | 0  |     s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B);  | 
6173  | 0  |     s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6;  | 
6174  | 0  |     i++;  | 
6175  |  |     /* X = sizeof(struct ip6_hdr) */  | 
6176  | 0  |     s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM);  | 
6177  | 0  |     s[i]->s.k = 40;  | 
6178  | 0  |     i++;  | 
6179  | 0  |     break;  | 
6180  |  |  | 
6181  | 0  |   default:  | 
6182  | 0  |     bpf_error(cstate, "unsupported proto to gen_protochain");  | 
6183  |  |     /*NOTREACHED*/  | 
6184  | 0  |   }  | 
6185  |  |  | 
6186  |  |   /* again: if (A == v) goto end; else fall through; */  | 
6187  | 0  |   again = i;  | 
6188  | 0  |   s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6189  | 0  |   s[i]->s.k = v;  | 
6190  | 0  |   s[i]->s.jt = NULL;    /*later*/  | 
6191  | 0  |   s[i]->s.jf = NULL;    /*update in next stmt*/  | 
6192  | 0  |   fix5 = i;  | 
6193  | 0  |   i++;  | 
6194  |  | 
  | 
6195  |  | #ifndef IPPROTO_NONE  | 
6196  |  | #define IPPROTO_NONE  59  | 
6197  |  | #endif  | 
6198  |  |   /* if (A == IPPROTO_NONE) goto end */  | 
6199  | 0  |   s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6200  | 0  |   s[i]->s.jt = NULL;  /*later*/  | 
6201  | 0  |   s[i]->s.jf = NULL;  /*update in next stmt*/  | 
6202  | 0  |   s[i]->s.k = IPPROTO_NONE;  | 
6203  | 0  |   s[fix5]->s.jf = s[i];  | 
6204  | 0  |   fix2 = i;  | 
6205  | 0  |   i++;  | 
6206  |  | 
  | 
6207  | 0  |   if (proto == Q_IPV6) { | 
6208  | 0  |     int v6start, v6end, v6advance, j;  | 
6209  |  | 
  | 
6210  | 0  |     v6start = i;  | 
6211  |  |     /* if (A == IPPROTO_HOPOPTS) goto v6advance */  | 
6212  | 0  |     s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6213  | 0  |     s[i]->s.jt = NULL;  /*later*/  | 
6214  | 0  |     s[i]->s.jf = NULL;  /*update in next stmt*/  | 
6215  | 0  |     s[i]->s.k = IPPROTO_HOPOPTS;  | 
6216  | 0  |     s[fix2]->s.jf = s[i];  | 
6217  | 0  |     i++;  | 
6218  |  |     /* if (A == IPPROTO_DSTOPTS) goto v6advance */  | 
6219  | 0  |     s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6220  | 0  |     s[i]->s.jt = NULL;  /*later*/  | 
6221  | 0  |     s[i]->s.jf = NULL;  /*update in next stmt*/  | 
6222  | 0  |     s[i]->s.k = IPPROTO_DSTOPTS;  | 
6223  | 0  |     i++;  | 
6224  |  |     /* if (A == IPPROTO_ROUTING) goto v6advance */  | 
6225  | 0  |     s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6226  | 0  |     s[i]->s.jt = NULL;  /*later*/  | 
6227  | 0  |     s[i]->s.jf = NULL;  /*update in next stmt*/  | 
6228  | 0  |     s[i]->s.k = IPPROTO_ROUTING;  | 
6229  | 0  |     i++;  | 
6230  |  |     /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */  | 
6231  | 0  |     s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6232  | 0  |     s[i]->s.jt = NULL;  /*later*/  | 
6233  | 0  |     s[i]->s.jf = NULL;  /*later*/  | 
6234  | 0  |     s[i]->s.k = IPPROTO_FRAGMENT;  | 
6235  | 0  |     fix3 = i;  | 
6236  | 0  |     v6end = i;  | 
6237  | 0  |     i++;  | 
6238  |  |  | 
6239  |  |     /* v6advance: */  | 
6240  | 0  |     v6advance = i;  | 
6241  |  |  | 
6242  |  |     /*  | 
6243  |  |      * in short,  | 
6244  |  |      * A = P[X + packet head];  | 
6245  |  |      * X = X + (P[X + packet head + 1] + 1) * 8;  | 
6246  |  |      */  | 
6247  |  |     /* A = P[X + packet head] */  | 
6248  | 0  |     s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
6249  | 0  |     s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
6250  | 0  |     i++;  | 
6251  |  |     /* MEM[reg2] = A */  | 
6252  | 0  |     s[i] = new_stmt(cstate, BPF_ST);  | 
6253  | 0  |     s[i]->s.k = reg2;  | 
6254  | 0  |     i++;  | 
6255  |  |     /* A = P[X + packet head + 1]; */  | 
6256  | 0  |     s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
6257  | 0  |     s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1;  | 
6258  | 0  |     i++;  | 
6259  |  |     /* A += 1 */  | 
6260  | 0  |     s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
6261  | 0  |     s[i]->s.k = 1;  | 
6262  | 0  |     i++;  | 
6263  |  |     /* A *= 8 */  | 
6264  | 0  |     s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);  | 
6265  | 0  |     s[i]->s.k = 8;  | 
6266  | 0  |     i++;  | 
6267  |  |     /* A += X */  | 
6268  | 0  |     s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);  | 
6269  | 0  |     s[i]->s.k = 0;  | 
6270  | 0  |     i++;  | 
6271  |  |     /* X = A; */  | 
6272  | 0  |     s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
6273  | 0  |     i++;  | 
6274  |  |     /* A = MEM[reg2] */  | 
6275  | 0  |     s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
6276  | 0  |     s[i]->s.k = reg2;  | 
6277  | 0  |     i++;  | 
6278  |  |  | 
6279  |  |     /* goto again; (must use BPF_JA for backward jump) */  | 
6280  | 0  |     s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);  | 
6281  | 0  |     s[i]->s.k = again - i - 1;  | 
6282  | 0  |     s[i - 1]->s.jf = s[i];  | 
6283  | 0  |     i++;  | 
6284  |  |  | 
6285  |  |     /* fixup */  | 
6286  | 0  |     for (j = v6start; j <= v6end; j++)  | 
6287  | 0  |       s[j]->s.jt = s[v6advance];  | 
6288  | 0  |   } else { | 
6289  |  |     /* nop */  | 
6290  | 0  |     s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
6291  | 0  |     s[i]->s.k = 0;  | 
6292  | 0  |     s[fix2]->s.jf = s[i];  | 
6293  | 0  |     i++;  | 
6294  | 0  |   }  | 
6295  |  |  | 
6296  |  |   /* ahcheck: */  | 
6297  | 0  |   ahcheck = i;  | 
6298  |  |   /* if (A == IPPROTO_AH) then fall through; else goto end; */  | 
6299  | 0  |   s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K);  | 
6300  | 0  |   s[i]->s.jt = NULL;  /*later*/  | 
6301  | 0  |   s[i]->s.jf = NULL;  /*later*/  | 
6302  | 0  |   s[i]->s.k = IPPROTO_AH;  | 
6303  | 0  |   if (fix3)  | 
6304  | 0  |     s[fix3]->s.jf = s[ahcheck];  | 
6305  | 0  |   fix4 = i;  | 
6306  | 0  |   i++;  | 
6307  |  |  | 
6308  |  |   /*  | 
6309  |  |    * in short,  | 
6310  |  |    * A = P[X];  | 
6311  |  |    * X = X + (P[X + 1] + 2) * 4;  | 
6312  |  |    */  | 
6313  |  |   /* A = X */  | 
6314  | 0  |   s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);  | 
6315  | 0  |   i++;  | 
6316  |  |   /* A = P[X + packet head]; */  | 
6317  | 0  |   s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
6318  | 0  |   s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
6319  | 0  |   i++;  | 
6320  |  |   /* MEM[reg2] = A */  | 
6321  | 0  |   s[i] = new_stmt(cstate, BPF_ST);  | 
6322  | 0  |   s[i]->s.k = reg2;  | 
6323  | 0  |   i++;  | 
6324  |  |   /* A = X */  | 
6325  | 0  |   s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA);  | 
6326  | 0  |   i++;  | 
6327  |  |   /* A += 1 */  | 
6328  | 0  |   s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
6329  | 0  |   s[i]->s.k = 1;  | 
6330  | 0  |   i++;  | 
6331  |  |   /* X = A */  | 
6332  | 0  |   s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
6333  | 0  |   i++;  | 
6334  |  |   /* A = P[X + packet head] */  | 
6335  | 0  |   s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
6336  | 0  |   s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
6337  | 0  |   i++;  | 
6338  |  |   /* A += 2 */  | 
6339  | 0  |   s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
6340  | 0  |   s[i]->s.k = 2;  | 
6341  | 0  |   i++;  | 
6342  |  |   /* A *= 4 */  | 
6343  | 0  |   s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);  | 
6344  | 0  |   s[i]->s.k = 4;  | 
6345  | 0  |   i++;  | 
6346  |  |   /* X = A; */  | 
6347  | 0  |   s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
6348  | 0  |   i++;  | 
6349  |  |   /* A = MEM[reg2] */  | 
6350  | 0  |   s[i] = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
6351  | 0  |   s[i]->s.k = reg2;  | 
6352  | 0  |   i++;  | 
6353  |  |  | 
6354  |  |   /* goto again; (must use BPF_JA for backward jump) */  | 
6355  | 0  |   s[i] = new_stmt(cstate, BPF_JMP|BPF_JA);  | 
6356  | 0  |   s[i]->s.k = again - i - 1;  | 
6357  | 0  |   i++;  | 
6358  |  |  | 
6359  |  |   /* end: nop */  | 
6360  | 0  |   end = i;  | 
6361  | 0  |   s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
6362  | 0  |   s[i]->s.k = 0;  | 
6363  | 0  |   s[fix2]->s.jt = s[end];  | 
6364  | 0  |   s[fix4]->s.jf = s[end];  | 
6365  | 0  |   s[fix5]->s.jt = s[end];  | 
6366  | 0  |   i++;  | 
6367  |  |  | 
6368  |  |   /*  | 
6369  |  |    * make slist chain  | 
6370  |  |    */  | 
6371  | 0  |   max = i;  | 
6372  | 0  |   for (i = 0; i < max - 1; i++)  | 
6373  | 0  |     s[i]->next = s[i + 1];  | 
6374  | 0  |   s[max - 1]->next = NULL;  | 
6375  |  |  | 
6376  |  |   /*  | 
6377  |  |    * emit final check  | 
6378  |  |    */  | 
6379  | 0  |   b = new_block(cstate, JMP(BPF_JEQ));  | 
6380  | 0  |   b->stmts = s[1];  /*remember, s[0] is dummy*/  | 
6381  | 0  |   b->s.k = v;  | 
6382  |  | 
  | 
6383  | 0  |   free_reg(cstate, reg2);  | 
6384  |  | 
  | 
6385  | 0  |   gen_and(b0, b);  | 
6386  | 0  |   return b;  | 
6387  | 0  | }  | 
6388  |  | #endif /* !defined(NO_PROTOCHAIN) */  | 
6389  |  |  | 
6390  |  | static struct block *  | 
6391  |  | gen_check_802_11_data_frame(compiler_state_t *cstate)  | 
6392  | 452  | { | 
6393  | 452  |   struct slist *s;  | 
6394  | 452  |   struct block *b0, *b1;  | 
6395  |  |  | 
6396  |  |   /*  | 
6397  |  |    * A data frame has the 0x08 bit (b3) in the frame control field set  | 
6398  |  |    * and the 0x04 bit (b2) clear.  | 
6399  |  |    */  | 
6400  | 452  |   s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
6401  | 452  |   b0 = new_block(cstate, JMP(BPF_JSET));  | 
6402  | 452  |   b0->s.k = 0x08;  | 
6403  | 452  |   b0->stmts = s;  | 
6404  |  |  | 
6405  | 452  |   s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
6406  | 452  |   b1 = new_block(cstate, JMP(BPF_JSET));  | 
6407  | 452  |   b1->s.k = 0x04;  | 
6408  | 452  |   b1->stmts = s;  | 
6409  | 452  |   gen_not(b1);  | 
6410  |  |  | 
6411  | 452  |   gen_and(b1, b0);  | 
6412  |  |  | 
6413  | 452  |   return b0;  | 
6414  | 452  | }  | 
6415  |  |  | 
6416  |  | /*  | 
6417  |  |  * Generate code that checks whether the packet is a packet for protocol  | 
6418  |  |  * <proto> and whether the type field in that protocol's header has  | 
6419  |  |  * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an  | 
6420  |  |  * IP packet and checks the protocol number in the IP header against <v>.  | 
6421  |  |  *  | 
6422  |  |  * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks  | 
6423  |  |  * against Q_IP and Q_IPV6.  | 
6424  |  |  */  | 
6425  |  | static struct block *  | 
6426  |  | gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir)  | 
6427  | 4.57k  | { | 
6428  | 4.57k  |   struct block *b0, *b1;  | 
6429  | 4.57k  |   struct block *b2;  | 
6430  |  |  | 
6431  | 4.57k  |   if (dir != Q_DEFAULT)  | 
6432  | 0  |     bpf_error(cstate, "direction applied to 'proto'");  | 
6433  |  |  | 
6434  | 4.57k  |   switch (proto) { | 
6435  | 328  |   case Q_DEFAULT:  | 
6436  | 328  |     b0 = gen_proto(cstate, v, Q_IP, dir);  | 
6437  | 328  |     b1 = gen_proto(cstate, v, Q_IPV6, dir);  | 
6438  | 328  |     gen_or(b0, b1);  | 
6439  | 328  |     return b1;  | 
6440  |  |  | 
6441  | 0  |   case Q_LINK:  | 
6442  | 0  |     return gen_linktype(cstate, v);  | 
6443  |  |  | 
6444  | 328  |   case Q_IP:  | 
6445  |  |     /*  | 
6446  |  |      * For FDDI, RFC 1188 says that SNAP encapsulation is used,  | 
6447  |  |      * not LLC encapsulation with LLCSAP_IP.  | 
6448  |  |      *  | 
6449  |  |      * For IEEE 802 networks - which includes 802.5 token ring  | 
6450  |  |      * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042  | 
6451  |  |      * says that SNAP encapsulation is used, not LLC encapsulation  | 
6452  |  |      * with LLCSAP_IP.  | 
6453  |  |      *  | 
6454  |  |      * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and  | 
6455  |  |      * RFC 2225 say that SNAP encapsulation is used, not LLC  | 
6456  |  |      * encapsulation with LLCSAP_IP.  | 
6457  |  |      *  | 
6458  |  |      * So we always check for ETHERTYPE_IP.  | 
6459  |  |      */  | 
6460  | 328  |     b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
6461  | 328  |     b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v);  | 
6462  | 328  |     gen_and(b0, b1);  | 
6463  | 328  |     return b1;  | 
6464  |  |  | 
6465  | 0  |   case Q_ARP:  | 
6466  | 0  |     bpf_error(cstate, "arp does not encapsulate another protocol");  | 
6467  |  |     /*NOTREACHED*/  | 
6468  |  |  | 
6469  | 0  |   case Q_RARP:  | 
6470  | 0  |     bpf_error(cstate, "rarp does not encapsulate another protocol");  | 
6471  |  |     /*NOTREACHED*/  | 
6472  |  |  | 
6473  | 0  |   case Q_SCTP:  | 
6474  | 0  |     bpf_error(cstate, "'sctp proto' is bogus");  | 
6475  |  |     /*NOTREACHED*/  | 
6476  |  |  | 
6477  | 0  |   case Q_TCP:  | 
6478  | 0  |     bpf_error(cstate, "'tcp proto' is bogus");  | 
6479  |  |     /*NOTREACHED*/  | 
6480  |  |  | 
6481  | 0  |   case Q_UDP:  | 
6482  | 0  |     bpf_error(cstate, "'udp proto' is bogus");  | 
6483  |  |     /*NOTREACHED*/  | 
6484  |  |  | 
6485  | 0  |   case Q_ICMP:  | 
6486  | 0  |     bpf_error(cstate, "'icmp proto' is bogus");  | 
6487  |  |     /*NOTREACHED*/  | 
6488  |  |  | 
6489  | 0  |   case Q_IGMP:  | 
6490  | 0  |     bpf_error(cstate, "'igmp proto' is bogus");  | 
6491  |  |     /*NOTREACHED*/  | 
6492  |  |  | 
6493  | 0  |   case Q_IGRP:  | 
6494  | 0  |     bpf_error(cstate, "'igrp proto' is bogus");  | 
6495  |  |     /*NOTREACHED*/  | 
6496  |  |  | 
6497  | 0  |   case Q_ATALK:  | 
6498  | 0  |     bpf_error(cstate, "AppleTalk encapsulation is not specifiable");  | 
6499  |  |     /*NOTREACHED*/  | 
6500  |  |  | 
6501  | 0  |   case Q_DECNET:  | 
6502  | 0  |     bpf_error(cstate, "DECNET encapsulation is not specifiable");  | 
6503  |  |     /*NOTREACHED*/  | 
6504  |  |  | 
6505  | 0  |   case Q_LAT:  | 
6506  | 0  |     bpf_error(cstate, "LAT does not encapsulate another protocol");  | 
6507  |  |     /*NOTREACHED*/  | 
6508  |  |  | 
6509  | 0  |   case Q_SCA:  | 
6510  | 0  |     bpf_error(cstate, "SCA does not encapsulate another protocol");  | 
6511  |  |     /*NOTREACHED*/  | 
6512  |  |  | 
6513  | 0  |   case Q_MOPRC:  | 
6514  | 0  |     bpf_error(cstate, "MOPRC does not encapsulate another protocol");  | 
6515  |  |     /*NOTREACHED*/  | 
6516  |  |  | 
6517  | 0  |   case Q_MOPDL:  | 
6518  | 0  |     bpf_error(cstate, "MOPDL does not encapsulate another protocol");  | 
6519  |  |     /*NOTREACHED*/  | 
6520  |  |  | 
6521  | 297  |   case Q_IPV6:  | 
6522  | 297  |     b0 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
6523  |  |     /*  | 
6524  |  |      * Also check for a fragment header before the final  | 
6525  |  |      * header.  | 
6526  |  |      */  | 
6527  | 297  |     b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT);  | 
6528  | 297  |     b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v);  | 
6529  | 297  |     gen_and(b2, b1);  | 
6530  | 297  |     b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v);  | 
6531  | 297  |     gen_or(b2, b1);  | 
6532  | 297  |     gen_and(b0, b1);  | 
6533  | 297  |     return b1;  | 
6534  |  |  | 
6535  | 0  |   case Q_ICMPV6:  | 
6536  | 0  |     bpf_error(cstate, "'icmp6 proto' is bogus");  | 
6537  |  |     /*NOTREACHED*/  | 
6538  |  |  | 
6539  | 0  |   case Q_AH:  | 
6540  | 0  |     bpf_error(cstate, "'ah proto' is bogus");  | 
6541  |  |     /*NOTREACHED*/  | 
6542  |  |  | 
6543  | 0  |   case Q_ESP:  | 
6544  | 0  |     bpf_error(cstate, "'esp proto' is bogus");  | 
6545  |  |     /*NOTREACHED*/  | 
6546  |  |  | 
6547  | 0  |   case Q_PIM:  | 
6548  | 0  |     bpf_error(cstate, "'pim proto' is bogus");  | 
6549  |  |     /*NOTREACHED*/  | 
6550  |  |  | 
6551  | 0  |   case Q_VRRP:  | 
6552  | 0  |     bpf_error(cstate, "'vrrp proto' is bogus");  | 
6553  |  |     /*NOTREACHED*/  | 
6554  |  |  | 
6555  | 0  |   case Q_AARP:  | 
6556  | 0  |     bpf_error(cstate, "'aarp proto' is bogus");  | 
6557  |  |     /*NOTREACHED*/  | 
6558  |  |  | 
6559  | 2.13k  |   case Q_ISO:  | 
6560  | 2.13k  |     switch (cstate->linktype) { | 
6561  |  |  | 
6562  | 48  |     case DLT_FRELAY:  | 
6563  |  |       /*  | 
6564  |  |        * Frame Relay packets typically have an OSI  | 
6565  |  |        * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"  | 
6566  |  |        * generates code to check for all the OSI  | 
6567  |  |        * NLPIDs, so calling it and then adding a check  | 
6568  |  |        * for the particular NLPID for which we're  | 
6569  |  |        * looking is bogus, as we can just check for  | 
6570  |  |        * the NLPID.  | 
6571  |  |        *  | 
6572  |  |        * What we check for is the NLPID and a frame  | 
6573  |  |        * control field value of UI, i.e. 0x03 followed  | 
6574  |  |        * by the NLPID.  | 
6575  |  |        *  | 
6576  |  |        * XXX - assumes a 2-byte Frame Relay header with  | 
6577  |  |        * DLCI and flags.  What if the address is longer?  | 
6578  |  |        *  | 
6579  |  |        * XXX - what about SNAP-encapsulated frames?  | 
6580  |  |        */  | 
6581  | 48  |       return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v);  | 
6582  |  |       /*NOTREACHED*/  | 
6583  |  |  | 
6584  | 30  |     case DLT_C_HDLC:  | 
6585  | 59  |     case DLT_HDLC:  | 
6586  |  |       /*  | 
6587  |  |        * Cisco uses an Ethertype lookalike - for OSI,  | 
6588  |  |        * it's 0xfefe.  | 
6589  |  |        */  | 
6590  | 59  |       b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS);  | 
6591  |  |       /* OSI in C-HDLC is stuffed with a fudge byte */  | 
6592  | 59  |       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v);  | 
6593  | 59  |       gen_and(b0, b1);  | 
6594  | 59  |       return b1;  | 
6595  |  |  | 
6596  | 2.02k  |     default:  | 
6597  | 2.02k  |       b0 = gen_linktype(cstate, LLCSAP_ISONS);  | 
6598  | 2.02k  |       b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v);  | 
6599  | 2.02k  |       gen_and(b0, b1);  | 
6600  | 2.02k  |       return b1;  | 
6601  | 2.13k  |     }  | 
6602  |  |  | 
6603  | 0  |   case Q_ESIS:  | 
6604  | 0  |     bpf_error(cstate, "'esis proto' is bogus");  | 
6605  |  |     /*NOTREACHED*/  | 
6606  |  |  | 
6607  | 1.49k  |   case Q_ISIS:  | 
6608  | 1.49k  |     b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT);  | 
6609  |  |     /*  | 
6610  |  |      * 4 is the offset of the PDU type relative to the IS-IS  | 
6611  |  |      * header.  | 
6612  |  |      */  | 
6613  | 1.49k  |     b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v);  | 
6614  | 1.49k  |     gen_and(b0, b1);  | 
6615  | 1.49k  |     return b1;  | 
6616  |  |  | 
6617  | 0  |   case Q_CLNP:  | 
6618  | 0  |     bpf_error(cstate, "'clnp proto' is not supported");  | 
6619  |  |     /*NOTREACHED*/  | 
6620  |  |  | 
6621  | 0  |   case Q_STP:  | 
6622  | 0  |     bpf_error(cstate, "'stp proto' is bogus");  | 
6623  |  |     /*NOTREACHED*/  | 
6624  |  |  | 
6625  | 0  |   case Q_IPX:  | 
6626  | 0  |     bpf_error(cstate, "'ipx proto' is bogus");  | 
6627  |  |     /*NOTREACHED*/  | 
6628  |  |  | 
6629  | 0  |   case Q_NETBEUI:  | 
6630  | 0  |     bpf_error(cstate, "'netbeui proto' is bogus");  | 
6631  |  |     /*NOTREACHED*/  | 
6632  |  |  | 
6633  | 0  |   case Q_ISIS_L1:  | 
6634  | 0  |     bpf_error(cstate, "'l1 proto' is bogus");  | 
6635  |  |     /*NOTREACHED*/  | 
6636  |  |  | 
6637  | 0  |   case Q_ISIS_L2:  | 
6638  | 0  |     bpf_error(cstate, "'l2 proto' is bogus");  | 
6639  |  |     /*NOTREACHED*/  | 
6640  |  |  | 
6641  | 0  |   case Q_ISIS_IIH:  | 
6642  | 0  |     bpf_error(cstate, "'iih proto' is bogus");  | 
6643  |  |     /*NOTREACHED*/  | 
6644  |  |  | 
6645  | 0  |   case Q_ISIS_SNP:  | 
6646  | 0  |     bpf_error(cstate, "'snp proto' is bogus");  | 
6647  |  |     /*NOTREACHED*/  | 
6648  |  |  | 
6649  | 0  |   case Q_ISIS_CSNP:  | 
6650  | 0  |     bpf_error(cstate, "'csnp proto' is bogus");  | 
6651  |  |     /*NOTREACHED*/  | 
6652  |  |  | 
6653  | 0  |   case Q_ISIS_PSNP:  | 
6654  | 0  |     bpf_error(cstate, "'psnp proto' is bogus");  | 
6655  |  |     /*NOTREACHED*/  | 
6656  |  |  | 
6657  | 0  |   case Q_ISIS_LSP:  | 
6658  | 0  |     bpf_error(cstate, "'lsp proto' is bogus");  | 
6659  |  |     /*NOTREACHED*/  | 
6660  |  |  | 
6661  | 0  |   case Q_RADIO:  | 
6662  | 0  |     bpf_error(cstate, "'radio proto' is bogus");  | 
6663  |  |     /*NOTREACHED*/  | 
6664  |  |  | 
6665  | 0  |   case Q_CARP:  | 
6666  | 0  |     bpf_error(cstate, "'carp proto' is bogus");  | 
6667  |  |     /*NOTREACHED*/  | 
6668  |  |  | 
6669  | 0  |   default:  | 
6670  | 0  |     abort();  | 
6671  |  |     /*NOTREACHED*/  | 
6672  | 4.57k  |   }  | 
6673  |  |   /*NOTREACHED*/  | 
6674  | 4.57k  | }  | 
6675  |  |  | 
6676  |  | struct block *  | 
6677  |  | gen_scode(compiler_state_t *cstate, const char *name, struct qual q)  | 
6678  | 2  | { | 
6679  | 2  |   int proto = q.proto;  | 
6680  | 2  |   int dir = q.dir;  | 
6681  | 2  |   int tproto;  | 
6682  | 2  |   u_char *eaddr;  | 
6683  | 2  |   bpf_u_int32 mask, addr;  | 
6684  | 2  |   struct addrinfo *res, *res0;  | 
6685  | 2  |   struct sockaddr_in *sin4;  | 
6686  | 2  | #ifdef INET6  | 
6687  | 2  |   int tproto6;  | 
6688  | 2  |   struct sockaddr_in6 *sin6;  | 
6689  | 2  |   struct in6_addr mask128;  | 
6690  | 2  | #endif /*INET6*/  | 
6691  | 2  |   struct block *b, *tmp;  | 
6692  | 2  |   int port, real_proto;  | 
6693  | 2  |   int port1, port2;  | 
6694  |  |  | 
6695  |  |   /*  | 
6696  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
6697  |  |    * on an error.  | 
6698  |  |    */  | 
6699  | 2  |   if (setjmp(cstate->top_ctx))  | 
6700  | 2  |     return (NULL);  | 
6701  |  |  | 
6702  | 0  |   switch (q.addr) { | 
6703  |  |  | 
6704  | 0  |   case Q_NET:  | 
6705  | 0  |     addr = pcap_nametonetaddr(name);  | 
6706  | 0  |     if (addr == 0)  | 
6707  | 0  |       bpf_error(cstate, "unknown network '%s'", name);  | 
6708  |  |     /* Left justify network addr and calculate its network mask */  | 
6709  | 0  |     mask = 0xffffffff;  | 
6710  | 0  |     while (addr && (addr & 0xff000000) == 0) { | 
6711  | 0  |       addr <<= 8;  | 
6712  | 0  |       mask <<= 8;  | 
6713  | 0  |     }  | 
6714  | 0  |     return gen_host(cstate, addr, mask, proto, dir, q.addr);  | 
6715  |  |  | 
6716  | 0  |   case Q_DEFAULT:  | 
6717  | 0  |   case Q_HOST:  | 
6718  | 0  |     if (proto == Q_LINK) { | 
6719  | 0  |       switch (cstate->linktype) { | 
6720  |  |  | 
6721  | 0  |       case DLT_EN10MB:  | 
6722  | 0  |       case DLT_NETANALYZER:  | 
6723  | 0  |       case DLT_NETANALYZER_TRANSPARENT:  | 
6724  | 0  |         eaddr = pcap_ether_hostton(name);  | 
6725  | 0  |         if (eaddr == NULL)  | 
6726  | 0  |           bpf_error(cstate,  | 
6727  | 0  |               "unknown ether host '%s'", name);  | 
6728  | 0  |         tmp = gen_prevlinkhdr_check(cstate);  | 
6729  | 0  |         b = gen_ehostop(cstate, eaddr, dir);  | 
6730  | 0  |         if (tmp != NULL)  | 
6731  | 0  |           gen_and(tmp, b);  | 
6732  | 0  |         free(eaddr);  | 
6733  | 0  |         return b;  | 
6734  |  |  | 
6735  | 0  |       case DLT_FDDI:  | 
6736  | 0  |         eaddr = pcap_ether_hostton(name);  | 
6737  | 0  |         if (eaddr == NULL)  | 
6738  | 0  |           bpf_error(cstate,  | 
6739  | 0  |               "unknown FDDI host '%s'", name);  | 
6740  | 0  |         b = gen_fhostop(cstate, eaddr, dir);  | 
6741  | 0  |         free(eaddr);  | 
6742  | 0  |         return b;  | 
6743  |  |  | 
6744  | 0  |       case DLT_IEEE802:  | 
6745  | 0  |         eaddr = pcap_ether_hostton(name);  | 
6746  | 0  |         if (eaddr == NULL)  | 
6747  | 0  |           bpf_error(cstate,  | 
6748  | 0  |               "unknown token ring host '%s'", name);  | 
6749  | 0  |         b = gen_thostop(cstate, eaddr, dir);  | 
6750  | 0  |         free(eaddr);  | 
6751  | 0  |         return b;  | 
6752  |  |  | 
6753  | 0  |       case DLT_IEEE802_11:  | 
6754  | 0  |       case DLT_PRISM_HEADER:  | 
6755  | 0  |       case DLT_IEEE802_11_RADIO_AVS:  | 
6756  | 0  |       case DLT_IEEE802_11_RADIO:  | 
6757  | 0  |       case DLT_PPI:  | 
6758  | 0  |         eaddr = pcap_ether_hostton(name);  | 
6759  | 0  |         if (eaddr == NULL)  | 
6760  | 0  |           bpf_error(cstate,  | 
6761  | 0  |               "unknown 802.11 host '%s'", name);  | 
6762  | 0  |         b = gen_wlanhostop(cstate, eaddr, dir);  | 
6763  | 0  |         free(eaddr);  | 
6764  | 0  |         return b;  | 
6765  |  |  | 
6766  | 0  |       case DLT_IP_OVER_FC:  | 
6767  | 0  |         eaddr = pcap_ether_hostton(name);  | 
6768  | 0  |         if (eaddr == NULL)  | 
6769  | 0  |           bpf_error(cstate,  | 
6770  | 0  |               "unknown Fibre Channel host '%s'", name);  | 
6771  | 0  |         b = gen_ipfchostop(cstate, eaddr, dir);  | 
6772  | 0  |         free(eaddr);  | 
6773  | 0  |         return b;  | 
6774  | 0  |       }  | 
6775  |  |  | 
6776  | 0  |       bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");  | 
6777  | 0  |     } else if (proto == Q_DECNET) { | 
6778  | 0  |       unsigned short dn_addr;  | 
6779  |  | 
  | 
6780  | 0  |       if (!__pcap_nametodnaddr(name, &dn_addr)) { | 
6781  |  | #ifdef  DECNETLIB  | 
6782  |  |         bpf_error(cstate, "unknown decnet host name '%s'\n", name);  | 
6783  |  | #else  | 
6784  | 0  |         bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n",  | 
6785  | 0  |           name);  | 
6786  | 0  | #endif  | 
6787  | 0  |       }  | 
6788  |  |       /*  | 
6789  |  |        * I don't think DECNET hosts can be multihomed, so  | 
6790  |  |        * there is no need to build up a list of addresses  | 
6791  |  |        */  | 
6792  | 0  |       return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr));  | 
6793  | 0  |     } else { | 
6794  | 0  | #ifdef INET6  | 
6795  | 0  |       memset(&mask128, 0xff, sizeof(mask128));  | 
6796  | 0  | #endif  | 
6797  | 0  |       res0 = res = pcap_nametoaddrinfo(name);  | 
6798  | 0  |       if (res == NULL)  | 
6799  | 0  |         bpf_error(cstate, "unknown host '%s'", name);  | 
6800  | 0  |       cstate->ai = res;  | 
6801  | 0  |       b = tmp = NULL;  | 
6802  | 0  |       tproto = proto;  | 
6803  | 0  | #ifdef INET6  | 
6804  | 0  |       tproto6 = proto;  | 
6805  | 0  | #endif  | 
6806  | 0  |       if (cstate->off_linktype.constant_part == OFFSET_NOT_SET &&  | 
6807  | 0  |           tproto == Q_DEFAULT) { | 
6808  | 0  |         tproto = Q_IP;  | 
6809  | 0  | #ifdef INET6  | 
6810  | 0  |         tproto6 = Q_IPV6;  | 
6811  | 0  | #endif  | 
6812  | 0  |       }  | 
6813  | 0  |       for (res = res0; res; res = res->ai_next) { | 
6814  | 0  |         switch (res->ai_family) { | 
6815  | 0  |         case AF_INET:  | 
6816  | 0  | #ifdef INET6  | 
6817  | 0  |           if (tproto == Q_IPV6)  | 
6818  | 0  |             continue;  | 
6819  | 0  | #endif  | 
6820  |  |  | 
6821  | 0  |           sin4 = (struct sockaddr_in *)  | 
6822  | 0  |             res->ai_addr;  | 
6823  | 0  |           tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr),  | 
6824  | 0  |             0xffffffff, tproto, dir, q.addr);  | 
6825  | 0  |           break;  | 
6826  | 0  | #ifdef INET6  | 
6827  | 0  |         case AF_INET6:  | 
6828  | 0  |           if (tproto6 == Q_IP)  | 
6829  | 0  |             continue;  | 
6830  |  |  | 
6831  | 0  |           sin6 = (struct sockaddr_in6 *)  | 
6832  | 0  |             res->ai_addr;  | 
6833  | 0  |           tmp = gen_host6(cstate, &sin6->sin6_addr,  | 
6834  | 0  |             &mask128, tproto6, dir, q.addr);  | 
6835  | 0  |           break;  | 
6836  | 0  | #endif  | 
6837  | 0  |         default:  | 
6838  | 0  |           continue;  | 
6839  | 0  |         }  | 
6840  | 0  |         if (b)  | 
6841  | 0  |           gen_or(b, tmp);  | 
6842  | 0  |         b = tmp;  | 
6843  | 0  |       }  | 
6844  | 0  |       cstate->ai = NULL;  | 
6845  | 0  |       freeaddrinfo(res0);  | 
6846  | 0  |       if (b == NULL) { | 
6847  | 0  |         bpf_error(cstate, "unknown host '%s'%s", name,  | 
6848  | 0  |             (proto == Q_DEFAULT)  | 
6849  | 0  |           ? ""  | 
6850  | 0  |           : " for specified address family");  | 
6851  | 0  |       }  | 
6852  | 0  |       return b;  | 
6853  | 0  |     }  | 
6854  |  |  | 
6855  | 0  |   case Q_PORT:  | 
6856  | 0  |     if (proto != Q_DEFAULT &&  | 
6857  | 0  |         proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)  | 
6858  | 0  |       bpf_error(cstate, "illegal qualifier of 'port'");  | 
6859  | 0  |     if (pcap_nametoport(name, &port, &real_proto) == 0)  | 
6860  | 0  |       bpf_error(cstate, "unknown port '%s'", name);  | 
6861  | 0  |     if (proto == Q_UDP) { | 
6862  | 0  |       if (real_proto == IPPROTO_TCP)  | 
6863  | 0  |         bpf_error(cstate, "port '%s' is tcp", name);  | 
6864  | 0  |       else if (real_proto == IPPROTO_SCTP)  | 
6865  | 0  |         bpf_error(cstate, "port '%s' is sctp", name);  | 
6866  | 0  |       else  | 
6867  |  |         /* override PROTO_UNDEF */  | 
6868  | 0  |         real_proto = IPPROTO_UDP;  | 
6869  | 0  |     }  | 
6870  | 0  |     if (proto == Q_TCP) { | 
6871  | 0  |       if (real_proto == IPPROTO_UDP)  | 
6872  | 0  |         bpf_error(cstate, "port '%s' is udp", name);  | 
6873  |  |  | 
6874  | 0  |       else if (real_proto == IPPROTO_SCTP)  | 
6875  | 0  |         bpf_error(cstate, "port '%s' is sctp", name);  | 
6876  | 0  |       else  | 
6877  |  |         /* override PROTO_UNDEF */  | 
6878  | 0  |         real_proto = IPPROTO_TCP;  | 
6879  | 0  |     }  | 
6880  | 0  |     if (proto == Q_SCTP) { | 
6881  | 0  |       if (real_proto == IPPROTO_UDP)  | 
6882  | 0  |         bpf_error(cstate, "port '%s' is udp", name);  | 
6883  |  |  | 
6884  | 0  |       else if (real_proto == IPPROTO_TCP)  | 
6885  | 0  |         bpf_error(cstate, "port '%s' is tcp", name);  | 
6886  | 0  |       else  | 
6887  |  |         /* override PROTO_UNDEF */  | 
6888  | 0  |         real_proto = IPPROTO_SCTP;  | 
6889  | 0  |     }  | 
6890  | 0  |     if (port < 0)  | 
6891  | 0  |       bpf_error(cstate, "illegal port number %d < 0", port);  | 
6892  | 0  |     if (port > 65535)  | 
6893  | 0  |       bpf_error(cstate, "illegal port number %d > 65535", port);  | 
6894  | 0  |     b = gen_port(cstate, port, real_proto, dir);  | 
6895  | 0  |     gen_or(gen_port6(cstate, port, real_proto, dir), b);  | 
6896  | 0  |     return b;  | 
6897  |  |  | 
6898  | 0  |   case Q_PORTRANGE:  | 
6899  | 0  |     if (proto != Q_DEFAULT &&  | 
6900  | 0  |         proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)  | 
6901  | 0  |       bpf_error(cstate, "illegal qualifier of 'portrange'");  | 
6902  | 0  |     if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)  | 
6903  | 0  |       bpf_error(cstate, "unknown port in range '%s'", name);  | 
6904  | 0  |     if (proto == Q_UDP) { | 
6905  | 0  |       if (real_proto == IPPROTO_TCP)  | 
6906  | 0  |         bpf_error(cstate, "port in range '%s' is tcp", name);  | 
6907  | 0  |       else if (real_proto == IPPROTO_SCTP)  | 
6908  | 0  |         bpf_error(cstate, "port in range '%s' is sctp", name);  | 
6909  | 0  |       else  | 
6910  |  |         /* override PROTO_UNDEF */  | 
6911  | 0  |         real_proto = IPPROTO_UDP;  | 
6912  | 0  |     }  | 
6913  | 0  |     if (proto == Q_TCP) { | 
6914  | 0  |       if (real_proto == IPPROTO_UDP)  | 
6915  | 0  |         bpf_error(cstate, "port in range '%s' is udp", name);  | 
6916  | 0  |       else if (real_proto == IPPROTO_SCTP)  | 
6917  | 0  |         bpf_error(cstate, "port in range '%s' is sctp", name);  | 
6918  | 0  |       else  | 
6919  |  |         /* override PROTO_UNDEF */  | 
6920  | 0  |         real_proto = IPPROTO_TCP;  | 
6921  | 0  |     }  | 
6922  | 0  |     if (proto == Q_SCTP) { | 
6923  | 0  |       if (real_proto == IPPROTO_UDP)  | 
6924  | 0  |         bpf_error(cstate, "port in range '%s' is udp", name);  | 
6925  | 0  |       else if (real_proto == IPPROTO_TCP)  | 
6926  | 0  |         bpf_error(cstate, "port in range '%s' is tcp", name);  | 
6927  | 0  |       else  | 
6928  |  |         /* override PROTO_UNDEF */  | 
6929  | 0  |         real_proto = IPPROTO_SCTP;  | 
6930  | 0  |     }  | 
6931  | 0  |     if (port1 < 0)  | 
6932  | 0  |       bpf_error(cstate, "illegal port number %d < 0", port1);  | 
6933  | 0  |     if (port1 > 65535)  | 
6934  | 0  |       bpf_error(cstate, "illegal port number %d > 65535", port1);  | 
6935  | 0  |     if (port2 < 0)  | 
6936  | 0  |       bpf_error(cstate, "illegal port number %d < 0", port2);  | 
6937  | 0  |     if (port2 > 65535)  | 
6938  | 0  |       bpf_error(cstate, "illegal port number %d > 65535", port2);  | 
6939  |  |  | 
6940  | 0  |     b = gen_portrange(cstate, port1, port2, real_proto, dir);  | 
6941  | 0  |     gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b);  | 
6942  | 0  |     return b;  | 
6943  |  |  | 
6944  | 0  |   case Q_GATEWAY:  | 
6945  |  | #ifndef INET6  | 
6946  |  |     eaddr = pcap_ether_hostton(name);  | 
6947  |  |     if (eaddr == NULL)  | 
6948  |  |       bpf_error(cstate, "unknown ether host: %s", name);  | 
6949  |  |  | 
6950  |  |     res = pcap_nametoaddrinfo(name);  | 
6951  |  |     cstate->ai = res;  | 
6952  |  |     if (res == NULL)  | 
6953  |  |       bpf_error(cstate, "unknown host '%s'", name);  | 
6954  |  |     b = gen_gateway(cstate, eaddr, res, proto, dir);  | 
6955  |  |     cstate->ai = NULL;  | 
6956  |  |     freeaddrinfo(res);  | 
6957  |  |     if (b == NULL)  | 
6958  |  |       bpf_error(cstate, "unknown host '%s'", name);  | 
6959  |  |     return b;  | 
6960  |  | #else  | 
6961  | 0  |     bpf_error(cstate, "'gateway' not supported in this configuration");  | 
6962  | 0  | #endif /*INET6*/  | 
6963  |  |  | 
6964  | 0  |   case Q_PROTO:  | 
6965  | 0  |     real_proto = lookup_proto(cstate, name, proto);  | 
6966  | 0  |     if (real_proto >= 0)  | 
6967  | 0  |       return gen_proto(cstate, real_proto, proto, dir);  | 
6968  | 0  |     else  | 
6969  | 0  |       bpf_error(cstate, "unknown protocol: %s", name);  | 
6970  |  |  | 
6971  | 0  | #if !defined(NO_PROTOCHAIN)  | 
6972  | 0  |   case Q_PROTOCHAIN:  | 
6973  | 0  |     real_proto = lookup_proto(cstate, name, proto);  | 
6974  | 0  |     if (real_proto >= 0)  | 
6975  | 0  |       return gen_protochain(cstate, real_proto, proto);  | 
6976  | 0  |     else  | 
6977  | 0  |       bpf_error(cstate, "unknown protocol: %s", name);  | 
6978  | 0  | #endif /* !defined(NO_PROTOCHAIN) */  | 
6979  |  |  | 
6980  | 2  |   case Q_UNDEF:  | 
6981  | 2  |     syntax(cstate);  | 
6982  |  |     /*NOTREACHED*/  | 
6983  | 0  |   }  | 
6984  | 0  |   abort();  | 
6985  |  |   /*NOTREACHED*/  | 
6986  | 0  | }  | 
6987  |  |  | 
6988  |  | struct block *  | 
6989  |  | gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,  | 
6990  |  |     bpf_u_int32 masklen, struct qual q)  | 
6991  | 0  | { | 
6992  | 0  |   register int nlen, mlen;  | 
6993  | 0  |   bpf_u_int32 n, m;  | 
6994  |  |  | 
6995  |  |   /*  | 
6996  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
6997  |  |    * on an error.  | 
6998  |  |    */  | 
6999  | 0  |   if (setjmp(cstate->top_ctx))  | 
7000  | 0  |     return (NULL);  | 
7001  |  |  | 
7002  | 0  |   nlen = __pcap_atoin(s1, &n);  | 
7003  | 0  |   if (nlen < 0)  | 
7004  | 0  |     bpf_error(cstate, "invalid IPv4 address '%s'", s1);  | 
7005  |  |   /* Promote short ipaddr */  | 
7006  | 0  |   n <<= 32 - nlen;  | 
7007  |  | 
  | 
7008  | 0  |   if (s2 != NULL) { | 
7009  | 0  |     mlen = __pcap_atoin(s2, &m);  | 
7010  | 0  |     if (mlen < 0)  | 
7011  | 0  |       bpf_error(cstate, "invalid IPv4 address '%s'", s2);  | 
7012  |  |     /* Promote short ipaddr */  | 
7013  | 0  |     m <<= 32 - mlen;  | 
7014  | 0  |     if ((n & ~m) != 0)  | 
7015  | 0  |       bpf_error(cstate, "non-network bits set in \"%s mask %s\"",  | 
7016  | 0  |           s1, s2);  | 
7017  | 0  |   } else { | 
7018  |  |     /* Convert mask len to mask */  | 
7019  | 0  |     if (masklen > 32)  | 
7020  | 0  |       bpf_error(cstate, "mask length must be <= 32");  | 
7021  | 0  |     if (masklen == 0) { | 
7022  |  |       /*  | 
7023  |  |        * X << 32 is not guaranteed by C to be 0; it's  | 
7024  |  |        * undefined.  | 
7025  |  |        */  | 
7026  | 0  |       m = 0;  | 
7027  | 0  |     } else  | 
7028  | 0  |       m = 0xffffffff << (32 - masklen);  | 
7029  | 0  |     if ((n & ~m) != 0)  | 
7030  | 0  |       bpf_error(cstate, "non-network bits set in \"%s/%d\"",  | 
7031  | 0  |           s1, masklen);  | 
7032  | 0  |   }  | 
7033  |  |  | 
7034  | 0  |   switch (q.addr) { | 
7035  |  |  | 
7036  | 0  |   case Q_NET:  | 
7037  | 0  |     return gen_host(cstate, n, m, q.proto, q.dir, q.addr);  | 
7038  |  |  | 
7039  | 0  |   default:  | 
7040  | 0  |     bpf_error(cstate, "Mask syntax for networks only");  | 
7041  |  |     /*NOTREACHED*/  | 
7042  | 0  |   }  | 
7043  |  |   /*NOTREACHED*/  | 
7044  | 0  | }  | 
7045  |  |  | 
7046  |  | struct block *  | 
7047  |  | gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)  | 
7048  | 1  | { | 
7049  | 1  |   bpf_u_int32 mask;  | 
7050  | 1  |   int proto;  | 
7051  | 1  |   int dir;  | 
7052  | 1  |   register int vlen;  | 
7053  |  |  | 
7054  |  |   /*  | 
7055  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7056  |  |    * on an error.  | 
7057  |  |    */  | 
7058  | 1  |   if (setjmp(cstate->top_ctx))  | 
7059  | 1  |     return (NULL);  | 
7060  |  |  | 
7061  | 0  |   proto = q.proto;  | 
7062  | 0  |   dir = q.dir;  | 
7063  | 0  |   if (s == NULL)  | 
7064  | 1  |     vlen = 32;  | 
7065  | 18.4E  |   else if (q.proto == Q_DECNET) { | 
7066  | 0  |     vlen = __pcap_atodn(s, &v);  | 
7067  | 0  |     if (vlen == 0)  | 
7068  | 0  |       bpf_error(cstate, "malformed decnet address '%s'", s);  | 
7069  | 18.4E  |   } else { | 
7070  | 18.4E  |     vlen = __pcap_atoin(s, &v);  | 
7071  | 18.4E  |     if (vlen < 0)  | 
7072  | 0  |       bpf_error(cstate, "invalid IPv4 address '%s'", s);  | 
7073  | 18.4E  |   }  | 
7074  |  |  | 
7075  | 0  |   switch (q.addr) { | 
7076  |  |  | 
7077  | 0  |   case Q_DEFAULT:  | 
7078  | 0  |   case Q_HOST:  | 
7079  | 0  |   case Q_NET:  | 
7080  | 0  |     if (proto == Q_DECNET)  | 
7081  | 0  |       return gen_host(cstate, v, 0, proto, dir, q.addr);  | 
7082  | 0  |     else if (proto == Q_LINK) { | 
7083  | 0  |       bpf_error(cstate, "illegal link layer address");  | 
7084  | 0  |     } else { | 
7085  | 0  |       mask = 0xffffffff;  | 
7086  | 0  |       if (s == NULL && q.addr == Q_NET) { | 
7087  |  |         /* Promote short net number */  | 
7088  | 0  |         while (v && (v & 0xff000000) == 0) { | 
7089  | 0  |           v <<= 8;  | 
7090  | 0  |           mask <<= 8;  | 
7091  | 0  |         }  | 
7092  | 0  |       } else { | 
7093  |  |         /* Promote short ipaddr */  | 
7094  | 0  |         v <<= 32 - vlen;  | 
7095  | 0  |         mask <<= 32 - vlen ;  | 
7096  | 0  |       }  | 
7097  | 0  |       return gen_host(cstate, v, mask, proto, dir, q.addr);  | 
7098  | 0  |     }  | 
7099  |  |  | 
7100  | 0  |   case Q_PORT:  | 
7101  | 0  |     if (proto == Q_UDP)  | 
7102  | 0  |       proto = IPPROTO_UDP;  | 
7103  | 0  |     else if (proto == Q_TCP)  | 
7104  | 0  |       proto = IPPROTO_TCP;  | 
7105  | 0  |     else if (proto == Q_SCTP)  | 
7106  | 0  |       proto = IPPROTO_SCTP;  | 
7107  | 0  |     else if (proto == Q_DEFAULT)  | 
7108  | 0  |       proto = PROTO_UNDEF;  | 
7109  | 0  |     else  | 
7110  | 0  |       bpf_error(cstate, "illegal qualifier of 'port'");  | 
7111  |  |  | 
7112  | 0  |     if (v > 65535)  | 
7113  | 0  |       bpf_error(cstate, "illegal port number %u > 65535", v);  | 
7114  |  |  | 
7115  | 0  |       { | 
7116  | 0  |     struct block *b;  | 
7117  | 0  |     b = gen_port(cstate, v, proto, dir);  | 
7118  | 0  |     gen_or(gen_port6(cstate, v, proto, dir), b);  | 
7119  | 0  |     return b;  | 
7120  | 0  |       }  | 
7121  |  |  | 
7122  | 0  |   case Q_PORTRANGE:  | 
7123  | 0  |     if (proto == Q_UDP)  | 
7124  | 0  |       proto = IPPROTO_UDP;  | 
7125  | 0  |     else if (proto == Q_TCP)  | 
7126  | 0  |       proto = IPPROTO_TCP;  | 
7127  | 0  |     else if (proto == Q_SCTP)  | 
7128  | 0  |       proto = IPPROTO_SCTP;  | 
7129  | 0  |     else if (proto == Q_DEFAULT)  | 
7130  | 0  |       proto = PROTO_UNDEF;  | 
7131  | 0  |     else  | 
7132  | 0  |       bpf_error(cstate, "illegal qualifier of 'portrange'");  | 
7133  |  |  | 
7134  | 0  |     if (v > 65535)  | 
7135  | 0  |       bpf_error(cstate, "illegal port number %u > 65535", v);  | 
7136  |  |  | 
7137  | 0  |       { | 
7138  | 0  |     struct block *b;  | 
7139  | 0  |     b = gen_portrange(cstate, v, v, proto, dir);  | 
7140  | 0  |     gen_or(gen_portrange6(cstate, v, v, proto, dir), b);  | 
7141  | 0  |     return b;  | 
7142  | 0  |       }  | 
7143  |  |  | 
7144  | 0  |   case Q_GATEWAY:  | 
7145  | 0  |     bpf_error(cstate, "'gateway' requires a name");  | 
7146  |  |     /*NOTREACHED*/  | 
7147  |  |  | 
7148  | 0  |   case Q_PROTO:  | 
7149  | 0  |     return gen_proto(cstate, v, proto, dir);  | 
7150  |  |  | 
7151  | 0  | #if !defined(NO_PROTOCHAIN)  | 
7152  | 0  |   case Q_PROTOCHAIN:  | 
7153  | 0  |     return gen_protochain(cstate, v, proto);  | 
7154  | 0  | #endif  | 
7155  |  |  | 
7156  | 1  |   case Q_UNDEF:  | 
7157  | 1  |     syntax(cstate);  | 
7158  |  |     /*NOTREACHED*/  | 
7159  |  |  | 
7160  | 0  |   default:  | 
7161  | 0  |     abort();  | 
7162  |  |     /*NOTREACHED*/  | 
7163  | 0  |   }  | 
7164  |  |   /*NOTREACHED*/  | 
7165  | 0  | }  | 
7166  |  |  | 
7167  |  | #ifdef INET6  | 
7168  |  | struct block *  | 
7169  |  | gen_mcode6(compiler_state_t *cstate, const char *s1, const char *s2,  | 
7170  |  |     bpf_u_int32 masklen, struct qual q)  | 
7171  | 0  | { | 
7172  | 0  |   struct addrinfo *res;  | 
7173  | 0  |   struct in6_addr *addr;  | 
7174  | 0  |   struct in6_addr mask;  | 
7175  | 0  |   struct block *b;  | 
7176  | 0  |   uint32_t *a, *m;  | 
7177  |  |  | 
7178  |  |   /*  | 
7179  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7180  |  |    * on an error.  | 
7181  |  |    */  | 
7182  | 0  |   if (setjmp(cstate->top_ctx))  | 
7183  | 0  |     return (NULL);  | 
7184  |  |  | 
7185  | 0  |   if (s2)  | 
7186  | 0  |     bpf_error(cstate, "no mask %s supported", s2);  | 
7187  |  |  | 
7188  | 0  |   res = pcap_nametoaddrinfo(s1);  | 
7189  | 0  |   if (!res)  | 
7190  | 0  |     bpf_error(cstate, "invalid ip6 address %s", s1);  | 
7191  | 0  |   cstate->ai = res;  | 
7192  | 0  |   if (res->ai_next)  | 
7193  | 0  |     bpf_error(cstate, "%s resolved to multiple address", s1);  | 
7194  | 0  |   addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;  | 
7195  |  | 
  | 
7196  | 0  |   if (masklen > sizeof(mask.s6_addr) * 8)  | 
7197  | 0  |     bpf_error(cstate, "mask length must be <= %u", (unsigned int)(sizeof(mask.s6_addr) * 8));  | 
7198  | 0  |   memset(&mask, 0, sizeof(mask));  | 
7199  | 0  |   memset(&mask.s6_addr, 0xff, masklen / 8);  | 
7200  | 0  |   if (masklen % 8) { | 
7201  | 0  |     mask.s6_addr[masklen / 8] =  | 
7202  | 0  |       (0xff << (8 - masklen % 8)) & 0xff;  | 
7203  | 0  |   }  | 
7204  |  | 
  | 
7205  | 0  |   a = (uint32_t *)addr;  | 
7206  | 0  |   m = (uint32_t *)&mask;  | 
7207  | 0  |   if ((a[0] & ~m[0]) || (a[1] & ~m[1])  | 
7208  | 0  |    || (a[2] & ~m[2]) || (a[3] & ~m[3])) { | 
7209  | 0  |     bpf_error(cstate, "non-network bits set in \"%s/%d\"", s1, masklen);  | 
7210  | 0  |   }  | 
7211  |  |  | 
7212  | 0  |   switch (q.addr) { | 
7213  |  |  | 
7214  | 0  |   case Q_DEFAULT:  | 
7215  | 0  |   case Q_HOST:  | 
7216  | 0  |     if (masklen != 128)  | 
7217  | 0  |       bpf_error(cstate, "Mask syntax for networks only");  | 
7218  |  |     /* FALLTHROUGH */  | 
7219  |  |  | 
7220  | 0  |   case Q_NET:  | 
7221  | 0  |     b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr);  | 
7222  | 0  |     cstate->ai = NULL;  | 
7223  | 0  |     freeaddrinfo(res);  | 
7224  | 0  |     return b;  | 
7225  |  |  | 
7226  | 0  |   default:  | 
7227  | 0  |     bpf_error(cstate, "invalid qualifier against IPv6 address");  | 
7228  |  |     /*NOTREACHED*/  | 
7229  | 0  |   }  | 
7230  | 0  | }  | 
7231  |  | #endif /*INET6*/  | 
7232  |  |  | 
7233  |  | struct block *  | 
7234  |  | gen_ecode(compiler_state_t *cstate, const char *s, struct qual q)  | 
7235  | 0  | { | 
7236  | 0  |   struct block *b, *tmp;  | 
7237  |  |  | 
7238  |  |   /*  | 
7239  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7240  |  |    * on an error.  | 
7241  |  |    */  | 
7242  | 0  |   if (setjmp(cstate->top_ctx))  | 
7243  | 0  |     return (NULL);  | 
7244  |  |  | 
7245  | 0  |   if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) { | 
7246  | 0  |     cstate->e = pcap_ether_aton(s);  | 
7247  | 0  |     if (cstate->e == NULL)  | 
7248  | 0  |       bpf_error(cstate, "malloc");  | 
7249  | 0  |     switch (cstate->linktype) { | 
7250  | 0  |     case DLT_EN10MB:  | 
7251  | 0  |     case DLT_NETANALYZER:  | 
7252  | 0  |     case DLT_NETANALYZER_TRANSPARENT:  | 
7253  | 0  |       tmp = gen_prevlinkhdr_check(cstate);  | 
7254  | 0  |       b = gen_ehostop(cstate, cstate->e, (int)q.dir);  | 
7255  | 0  |       if (tmp != NULL)  | 
7256  | 0  |         gen_and(tmp, b);  | 
7257  | 0  |       break;  | 
7258  | 0  |     case DLT_FDDI:  | 
7259  | 0  |       b = gen_fhostop(cstate, cstate->e, (int)q.dir);  | 
7260  | 0  |       break;  | 
7261  | 0  |     case DLT_IEEE802:  | 
7262  | 0  |       b = gen_thostop(cstate, cstate->e, (int)q.dir);  | 
7263  | 0  |       break;  | 
7264  | 0  |     case DLT_IEEE802_11:  | 
7265  | 0  |     case DLT_PRISM_HEADER:  | 
7266  | 0  |     case DLT_IEEE802_11_RADIO_AVS:  | 
7267  | 0  |     case DLT_IEEE802_11_RADIO:  | 
7268  | 0  |     case DLT_PPI:  | 
7269  | 0  |       b = gen_wlanhostop(cstate, cstate->e, (int)q.dir);  | 
7270  | 0  |       break;  | 
7271  | 0  |     case DLT_IP_OVER_FC:  | 
7272  | 0  |       b = gen_ipfchostop(cstate, cstate->e, (int)q.dir);  | 
7273  | 0  |       break;  | 
7274  | 0  |     default:  | 
7275  | 0  |       free(cstate->e);  | 
7276  | 0  |       cstate->e = NULL;  | 
7277  | 0  |       bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");  | 
7278  |  |       /*NOTREACHED*/  | 
7279  | 0  |     }  | 
7280  | 0  |     free(cstate->e);  | 
7281  | 0  |     cstate->e = NULL;  | 
7282  | 0  |     return (b);  | 
7283  | 0  |   }  | 
7284  | 0  |   bpf_error(cstate, "ethernet address used in non-ether expression");  | 
7285  |  |   /*NOTREACHED*/  | 
7286  | 0  | }  | 
7287  |  |  | 
7288  |  | void  | 
7289  |  | sappend(struct slist *s0, struct slist *s1)  | 
7290  | 26.1k  | { | 
7291  |  |   /*  | 
7292  |  |    * This is definitely not the best way to do this, but the  | 
7293  |  |    * lists will rarely get long.  | 
7294  |  |    */  | 
7295  | 348k  |   while (s0->next)  | 
7296  | 321k  |     s0 = s0->next;  | 
7297  | 26.1k  |   s0->next = s1;  | 
7298  | 26.1k  | }  | 
7299  |  |  | 
7300  |  | static struct slist *  | 
7301  |  | xfer_to_x(compiler_state_t *cstate, struct arth *a)  | 
7302  | 2.53k  | { | 
7303  | 2.53k  |   struct slist *s;  | 
7304  |  |  | 
7305  | 2.53k  |   s = new_stmt(cstate, BPF_LDX|BPF_MEM);  | 
7306  | 2.53k  |   s->s.k = a->regno;  | 
7307  | 2.53k  |   return s;  | 
7308  | 2.53k  | }  | 
7309  |  |  | 
7310  |  | static struct slist *  | 
7311  |  | xfer_to_a(compiler_state_t *cstate, struct arth *a)  | 
7312  | 4.74k  | { | 
7313  | 4.74k  |   struct slist *s;  | 
7314  |  |  | 
7315  | 4.74k  |   s = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
7316  | 4.74k  |   s->s.k = a->regno;  | 
7317  | 4.74k  |   return s;  | 
7318  | 4.74k  | }  | 
7319  |  |  | 
7320  |  | /*  | 
7321  |  |  * Modify "index" to use the value stored into its register as an  | 
7322  |  |  * offset relative to the beginning of the header for the protocol  | 
7323  |  |  * "proto", and allocate a register and put an item "size" bytes long  | 
7324  |  |  * (1, 2, or 4) at that offset into that register, making it the register  | 
7325  |  |  * for "index".  | 
7326  |  |  */  | 
7327  |  | static struct arth *  | 
7328  |  | gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst,  | 
7329  |  |     bpf_u_int32 size)  | 
7330  | 0  | { | 
7331  | 0  |   int size_code;  | 
7332  | 0  |   struct slist *s, *tmp;  | 
7333  | 0  |   struct block *b;  | 
7334  | 0  |   int regno = alloc_reg(cstate);  | 
7335  |  | 
  | 
7336  | 0  |   free_reg(cstate, inst->regno);  | 
7337  | 0  |   switch (size) { | 
7338  |  |  | 
7339  | 0  |   default:  | 
7340  | 0  |     bpf_error(cstate, "data size must be 1, 2, or 4");  | 
7341  |  |     /*NOTREACHED*/  | 
7342  |  |  | 
7343  | 0  |   case 1:  | 
7344  | 0  |     size_code = BPF_B;  | 
7345  | 0  |     break;  | 
7346  |  |  | 
7347  | 0  |   case 2:  | 
7348  | 0  |     size_code = BPF_H;  | 
7349  | 0  |     break;  | 
7350  |  |  | 
7351  | 0  |   case 4:  | 
7352  | 0  |     size_code = BPF_W;  | 
7353  | 0  |     break;  | 
7354  | 0  |   }  | 
7355  | 0  |   switch (proto) { | 
7356  | 0  |   default:  | 
7357  | 0  |     bpf_error(cstate, "unsupported index operation");  | 
7358  |  |  | 
7359  | 0  |   case Q_RADIO:  | 
7360  |  |     /*  | 
7361  |  |      * The offset is relative to the beginning of the packet  | 
7362  |  |      * data, if we have a radio header.  (If we don't, this  | 
7363  |  |      * is an error.)  | 
7364  |  |      */  | 
7365  | 0  |     if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS &&  | 
7366  | 0  |         cstate->linktype != DLT_IEEE802_11_RADIO &&  | 
7367  | 0  |         cstate->linktype != DLT_PRISM_HEADER)  | 
7368  | 0  |       bpf_error(cstate, "radio information not present in capture");  | 
7369  |  |  | 
7370  |  |     /*  | 
7371  |  |      * Load into the X register the offset computed into the  | 
7372  |  |      * register specified by "index".  | 
7373  |  |      */  | 
7374  | 0  |     s = xfer_to_x(cstate, inst);  | 
7375  |  |  | 
7376  |  |     /*  | 
7377  |  |      * Load the item at that offset.  | 
7378  |  |      */  | 
7379  | 0  |     tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);  | 
7380  | 0  |     sappend(s, tmp);  | 
7381  | 0  |     sappend(inst->s, s);  | 
7382  | 0  |     break;  | 
7383  |  |  | 
7384  | 0  |   case Q_LINK:  | 
7385  |  |     /*  | 
7386  |  |      * The offset is relative to the beginning of  | 
7387  |  |      * the link-layer header.  | 
7388  |  |      *  | 
7389  |  |      * XXX - what about ATM LANE?  Should the index be  | 
7390  |  |      * relative to the beginning of the AAL5 frame, so  | 
7391  |  |      * that 0 refers to the beginning of the LE Control  | 
7392  |  |      * field, or relative to the beginning of the LAN  | 
7393  |  |      * frame, so that 0 refers, for Ethernet LANE, to  | 
7394  |  |      * the beginning of the destination address?  | 
7395  |  |      */  | 
7396  | 0  |     s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr);  | 
7397  |  |  | 
7398  |  |     /*  | 
7399  |  |      * If "s" is non-null, it has code to arrange that the  | 
7400  |  |      * X register contains the length of the prefix preceding  | 
7401  |  |      * the link-layer header.  Add to it the offset computed  | 
7402  |  |      * into the register specified by "index", and move that  | 
7403  |  |      * into the X register.  Otherwise, just load into the X  | 
7404  |  |      * register the offset computed into the register specified  | 
7405  |  |      * by "index".  | 
7406  |  |      */  | 
7407  | 0  |     if (s != NULL) { | 
7408  | 0  |       sappend(s, xfer_to_a(cstate, inst));  | 
7409  | 0  |       sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));  | 
7410  | 0  |       sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));  | 
7411  | 0  |     } else  | 
7412  | 0  |       s = xfer_to_x(cstate, inst);  | 
7413  |  |  | 
7414  |  |     /*  | 
7415  |  |      * Load the item at the sum of the offset we've put in the  | 
7416  |  |      * X register and the offset of the start of the link  | 
7417  |  |      * layer header (which is 0 if the radio header is  | 
7418  |  |      * variable-length; that header length is what we put  | 
7419  |  |      * into the X register and then added to the index).  | 
7420  |  |      */  | 
7421  | 0  |     tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);  | 
7422  | 0  |     tmp->s.k = cstate->off_linkhdr.constant_part;  | 
7423  | 0  |     sappend(s, tmp);  | 
7424  | 0  |     sappend(inst->s, s);  | 
7425  | 0  |     break;  | 
7426  |  |  | 
7427  | 0  |   case Q_IP:  | 
7428  | 0  |   case Q_ARP:  | 
7429  | 0  |   case Q_RARP:  | 
7430  | 0  |   case Q_ATALK:  | 
7431  | 0  |   case Q_DECNET:  | 
7432  | 0  |   case Q_SCA:  | 
7433  | 0  |   case Q_LAT:  | 
7434  | 0  |   case Q_MOPRC:  | 
7435  | 0  |   case Q_MOPDL:  | 
7436  | 0  |   case Q_IPV6:  | 
7437  |  |     /*  | 
7438  |  |      * The offset is relative to the beginning of  | 
7439  |  |      * the network-layer header.  | 
7440  |  |      * XXX - are there any cases where we want  | 
7441  |  |      * cstate->off_nl_nosnap?  | 
7442  |  |      */  | 
7443  | 0  |     s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);  | 
7444  |  |  | 
7445  |  |     /*  | 
7446  |  |      * If "s" is non-null, it has code to arrange that the  | 
7447  |  |      * X register contains the variable part of the offset  | 
7448  |  |      * of the link-layer payload.  Add to it the offset  | 
7449  |  |      * computed into the register specified by "index",  | 
7450  |  |      * and move that into the X register.  Otherwise, just  | 
7451  |  |      * load into the X register the offset computed into  | 
7452  |  |      * the register specified by "index".  | 
7453  |  |      */  | 
7454  | 0  |     if (s != NULL) { | 
7455  | 0  |       sappend(s, xfer_to_a(cstate, inst));  | 
7456  | 0  |       sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));  | 
7457  | 0  |       sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));  | 
7458  | 0  |     } else  | 
7459  | 0  |       s = xfer_to_x(cstate, inst);  | 
7460  |  |  | 
7461  |  |     /*  | 
7462  |  |      * Load the item at the sum of the offset we've put in the  | 
7463  |  |      * X register, the offset of the start of the network  | 
7464  |  |      * layer header from the beginning of the link-layer  | 
7465  |  |      * payload, and the constant part of the offset of the  | 
7466  |  |      * start of the link-layer payload.  | 
7467  |  |      */  | 
7468  | 0  |     tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);  | 
7469  | 0  |     tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
7470  | 0  |     sappend(s, tmp);  | 
7471  | 0  |     sappend(inst->s, s);  | 
7472  |  |  | 
7473  |  |     /*  | 
7474  |  |      * Do the computation only if the packet contains  | 
7475  |  |      * the protocol in question.  | 
7476  |  |      */  | 
7477  | 0  |     b = gen_proto_abbrev_internal(cstate, proto);  | 
7478  | 0  |     if (inst->b)  | 
7479  | 0  |       gen_and(inst->b, b);  | 
7480  | 0  |     inst->b = b;  | 
7481  | 0  |     break;  | 
7482  |  |  | 
7483  | 0  |   case Q_SCTP:  | 
7484  | 0  |   case Q_TCP:  | 
7485  | 0  |   case Q_UDP:  | 
7486  | 0  |   case Q_ICMP:  | 
7487  | 0  |   case Q_IGMP:  | 
7488  | 0  |   case Q_IGRP:  | 
7489  | 0  |   case Q_PIM:  | 
7490  | 0  |   case Q_VRRP:  | 
7491  | 0  |   case Q_CARP:  | 
7492  |  |     /*  | 
7493  |  |      * The offset is relative to the beginning of  | 
7494  |  |      * the transport-layer header.  | 
7495  |  |      *  | 
7496  |  |      * Load the X register with the length of the IPv4 header  | 
7497  |  |      * (plus the offset of the link-layer header, if it's  | 
7498  |  |      * a variable-length header), in bytes.  | 
7499  |  |      *  | 
7500  |  |      * XXX - are there any cases where we want  | 
7501  |  |      * cstate->off_nl_nosnap?  | 
7502  |  |      * XXX - we should, if we're built with  | 
7503  |  |      * IPv6 support, generate code to load either  | 
7504  |  |      * IPv4, IPv6, or both, as appropriate.  | 
7505  |  |      */  | 
7506  | 0  |     s = gen_loadx_iphdrlen(cstate);  | 
7507  |  |  | 
7508  |  |     /*  | 
7509  |  |      * The X register now contains the sum of the variable  | 
7510  |  |      * part of the offset of the link-layer payload and the  | 
7511  |  |      * length of the network-layer header.  | 
7512  |  |      *  | 
7513  |  |      * Load into the A register the offset relative to  | 
7514  |  |      * the beginning of the transport layer header,  | 
7515  |  |      * add the X register to that, move that to the  | 
7516  |  |      * X register, and load with an offset from the  | 
7517  |  |      * X register equal to the sum of the constant part of  | 
7518  |  |      * the offset of the link-layer payload and the offset,  | 
7519  |  |      * relative to the beginning of the link-layer payload,  | 
7520  |  |      * of the network-layer header.  | 
7521  |  |      */  | 
7522  | 0  |     sappend(s, xfer_to_a(cstate, inst));  | 
7523  | 0  |     sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));  | 
7524  | 0  |     sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));  | 
7525  | 0  |     sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code));  | 
7526  | 0  |     tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl;  | 
7527  | 0  |     sappend(inst->s, s);  | 
7528  |  |  | 
7529  |  |     /*  | 
7530  |  |      * Do the computation only if the packet contains  | 
7531  |  |      * the protocol in question - which is true only  | 
7532  |  |      * if this is an IP datagram and is the first or  | 
7533  |  |      * only fragment of that datagram.  | 
7534  |  |      */  | 
7535  | 0  |     gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate));  | 
7536  | 0  |     if (inst->b)  | 
7537  | 0  |       gen_and(inst->b, b);  | 
7538  | 0  |     gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b);  | 
7539  | 0  |     inst->b = b;  | 
7540  | 0  |     break;  | 
7541  | 0  |   case Q_ICMPV6:  | 
7542  |  |         /*  | 
7543  |  |         * Do the computation only if the packet contains  | 
7544  |  |         * the protocol in question.  | 
7545  |  |         */  | 
7546  | 0  |         b = gen_proto_abbrev_internal(cstate, Q_IPV6);  | 
7547  | 0  |         if (inst->b) { | 
7548  | 0  |             gen_and(inst->b, b);  | 
7549  | 0  |         }  | 
7550  | 0  |         inst->b = b;  | 
7551  |  |  | 
7552  |  |         /*  | 
7553  |  |         * Check if we have an icmp6 next header  | 
7554  |  |         */  | 
7555  | 0  |         b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58);  | 
7556  | 0  |         if (inst->b) { | 
7557  | 0  |             gen_and(inst->b, b);  | 
7558  | 0  |         }  | 
7559  | 0  |         inst->b = b;  | 
7560  |  |  | 
7561  |  | 
  | 
7562  | 0  |         s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);  | 
7563  |  |         /*  | 
7564  |  |         * If "s" is non-null, it has code to arrange that the  | 
7565  |  |         * X register contains the variable part of the offset  | 
7566  |  |         * of the link-layer payload.  Add to it the offset  | 
7567  |  |         * computed into the register specified by "index",  | 
7568  |  |         * and move that into the X register.  Otherwise, just  | 
7569  |  |         * load into the X register the offset computed into  | 
7570  |  |         * the register specified by "index".  | 
7571  |  |         */  | 
7572  | 0  |         if (s != NULL) { | 
7573  | 0  |             sappend(s, xfer_to_a(cstate, inst));  | 
7574  | 0  |             sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X));  | 
7575  | 0  |             sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX));  | 
7576  | 0  |         } else { | 
7577  | 0  |             s = xfer_to_x(cstate, inst);  | 
7578  | 0  |         }  | 
7579  |  |  | 
7580  |  |         /*  | 
7581  |  |         * Load the item at the sum of the offset we've put in the  | 
7582  |  |         * X register, the offset of the start of the network  | 
7583  |  |         * layer header from the beginning of the link-layer  | 
7584  |  |         * payload, and the constant part of the offset of the  | 
7585  |  |         * start of the link-layer payload.  | 
7586  |  |         */  | 
7587  | 0  |         tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code);  | 
7588  | 0  |         tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40;  | 
7589  |  | 
  | 
7590  | 0  |         sappend(s, tmp);  | 
7591  | 0  |         sappend(inst->s, s);  | 
7592  |  | 
  | 
7593  | 0  |         break;  | 
7594  | 0  |   }  | 
7595  | 0  |   inst->regno = regno;  | 
7596  | 0  |   s = new_stmt(cstate, BPF_ST);  | 
7597  | 0  |   s->s.k = regno;  | 
7598  | 0  |   sappend(inst->s, s);  | 
7599  |  | 
  | 
7600  | 0  |   return inst;  | 
7601  | 0  | }  | 
7602  |  |  | 
7603  |  | struct arth *  | 
7604  |  | gen_load(compiler_state_t *cstate, int proto, struct arth *inst,  | 
7605  |  |     bpf_u_int32 size)  | 
7606  | 0  | { | 
7607  |  |   /*  | 
7608  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7609  |  |    * on an error.  | 
7610  |  |    */  | 
7611  | 0  |   if (setjmp(cstate->top_ctx))  | 
7612  | 0  |     return (NULL);  | 
7613  |  |  | 
7614  | 0  |   return gen_load_internal(cstate, proto, inst, size);  | 
7615  | 0  | }  | 
7616  |  |  | 
7617  |  | static struct block *  | 
7618  |  | gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0,  | 
7619  |  |     struct arth *a1, int reversed)  | 
7620  | 680  | { | 
7621  | 680  |   struct slist *s0, *s1, *s2;  | 
7622  | 680  |   struct block *b, *tmp;  | 
7623  |  |  | 
7624  | 680  |   s0 = xfer_to_x(cstate, a1);  | 
7625  | 680  |   s1 = xfer_to_a(cstate, a0);  | 
7626  | 680  |   if (code == BPF_JEQ) { | 
7627  | 192  |     s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X);  | 
7628  | 192  |     b = new_block(cstate, JMP(code));  | 
7629  | 192  |     sappend(s1, s2);  | 
7630  | 192  |   }  | 
7631  | 488  |   else  | 
7632  | 488  |     b = new_block(cstate, BPF_JMP|code|BPF_X);  | 
7633  | 680  |   if (reversed)  | 
7634  | 330  |     gen_not(b);  | 
7635  |  |  | 
7636  | 680  |   sappend(s0, s1);  | 
7637  | 680  |   sappend(a1->s, s0);  | 
7638  | 680  |   sappend(a0->s, a1->s);  | 
7639  |  |  | 
7640  | 680  |   b->stmts = a0->s;  | 
7641  |  |  | 
7642  | 680  |   free_reg(cstate, a0->regno);  | 
7643  | 680  |   free_reg(cstate, a1->regno);  | 
7644  |  |  | 
7645  |  |   /* 'and' together protocol checks */  | 
7646  | 680  |   if (a0->b) { | 
7647  | 0  |     if (a1->b) { | 
7648  | 0  |       gen_and(a0->b, tmp = a1->b);  | 
7649  | 0  |     }  | 
7650  | 0  |     else  | 
7651  | 0  |       tmp = a0->b;  | 
7652  | 0  |   } else  | 
7653  | 680  |     tmp = a1->b;  | 
7654  |  |  | 
7655  | 680  |   if (tmp)  | 
7656  | 0  |     gen_and(tmp, b);  | 
7657  |  |  | 
7658  | 680  |   return b;  | 
7659  | 680  | }  | 
7660  |  |  | 
7661  |  | struct block *  | 
7662  |  | gen_relation(compiler_state_t *cstate, int code, struct arth *a0,  | 
7663  |  |     struct arth *a1, int reversed)  | 
7664  | 680  | { | 
7665  |  |   /*  | 
7666  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7667  |  |    * on an error.  | 
7668  |  |    */  | 
7669  | 680  |   if (setjmp(cstate->top_ctx))  | 
7670  | 0  |     return (NULL);  | 
7671  |  |  | 
7672  | 680  |   return gen_relation_internal(cstate, code, a0, a1, reversed);  | 
7673  | 680  | }  | 
7674  |  |  | 
7675  |  | struct arth *  | 
7676  |  | gen_loadlen(compiler_state_t *cstate)  | 
7677  | 0  | { | 
7678  | 0  |   int regno;  | 
7679  | 0  |   struct arth *a;  | 
7680  | 0  |   struct slist *s;  | 
7681  |  |  | 
7682  |  |   /*  | 
7683  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7684  |  |    * on an error.  | 
7685  |  |    */  | 
7686  | 0  |   if (setjmp(cstate->top_ctx))  | 
7687  | 0  |     return (NULL);  | 
7688  |  |  | 
7689  | 0  |   regno = alloc_reg(cstate);  | 
7690  | 0  |   a = (struct arth *)newchunk(cstate, sizeof(*a));  | 
7691  | 0  |   s = new_stmt(cstate, BPF_LD|BPF_LEN);  | 
7692  | 0  |   s->next = new_stmt(cstate, BPF_ST);  | 
7693  | 0  |   s->next->s.k = regno;  | 
7694  | 0  |   a->s = s;  | 
7695  | 0  |   a->regno = regno;  | 
7696  |  | 
  | 
7697  | 0  |   return a;  | 
7698  | 0  | }  | 
7699  |  |  | 
7700  |  | static struct arth *  | 
7701  |  | gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val)  | 
7702  | 3.49k  | { | 
7703  | 3.49k  |   struct arth *a;  | 
7704  | 3.49k  |   struct slist *s;  | 
7705  | 3.49k  |   int reg;  | 
7706  |  |  | 
7707  | 3.49k  |   a = (struct arth *)newchunk(cstate, sizeof(*a));  | 
7708  |  |  | 
7709  | 3.49k  |   reg = alloc_reg(cstate);  | 
7710  |  |  | 
7711  | 3.49k  |   s = new_stmt(cstate, BPF_LD|BPF_IMM);  | 
7712  | 3.49k  |   s->s.k = val;  | 
7713  | 3.49k  |   s->next = new_stmt(cstate, BPF_ST);  | 
7714  | 3.49k  |   s->next->s.k = reg;  | 
7715  | 3.49k  |   a->s = s;  | 
7716  | 3.49k  |   a->regno = reg;  | 
7717  |  |  | 
7718  | 3.49k  |   return a;  | 
7719  | 3.49k  | }  | 
7720  |  |  | 
7721  |  | struct arth *  | 
7722  |  | gen_loadi(compiler_state_t *cstate, bpf_u_int32 val)  | 
7723  | 3.49k  | { | 
7724  |  |   /*  | 
7725  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7726  |  |    * on an error.  | 
7727  |  |    */  | 
7728  | 3.49k  |   if (setjmp(cstate->top_ctx))  | 
7729  | 0  |     return (NULL);  | 
7730  |  |  | 
7731  | 3.49k  |   return gen_loadi_internal(cstate, val);  | 
7732  | 3.49k  | }  | 
7733  |  |  | 
7734  |  | /*  | 
7735  |  |  * The a_arg dance is to avoid annoying whining by compilers that  | 
7736  |  |  * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?  | 
7737  |  |  * It's not *used* after setjmp returns.  | 
7738  |  |  */  | 
7739  |  | struct arth *  | 
7740  |  | gen_neg(compiler_state_t *cstate, struct arth *a_arg)  | 
7741  | 2.20k  | { | 
7742  | 2.20k  |   struct arth *a = a_arg;  | 
7743  | 2.20k  |   struct slist *s;  | 
7744  |  |  | 
7745  |  |   /*  | 
7746  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7747  |  |    * on an error.  | 
7748  |  |    */  | 
7749  | 2.20k  |   if (setjmp(cstate->top_ctx))  | 
7750  | 0  |     return (NULL);  | 
7751  |  |  | 
7752  | 2.20k  |   s = xfer_to_a(cstate, a);  | 
7753  | 2.20k  |   sappend(a->s, s);  | 
7754  | 2.20k  |   s = new_stmt(cstate, BPF_ALU|BPF_NEG);  | 
7755  | 2.20k  |   s->s.k = 0;  | 
7756  | 2.20k  |   sappend(a->s, s);  | 
7757  | 2.20k  |   s = new_stmt(cstate, BPF_ST);  | 
7758  | 2.20k  |   s->s.k = a->regno;  | 
7759  | 2.20k  |   sappend(a->s, s);  | 
7760  |  |  | 
7761  | 2.20k  |   return a;  | 
7762  | 2.20k  | }  | 
7763  |  |  | 
7764  |  | /*  | 
7765  |  |  * The a0_arg dance is to avoid annoying whining by compilers that  | 
7766  |  |  * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?  | 
7767  |  |  * It's not *used* after setjmp returns.  | 
7768  |  |  */  | 
7769  |  | struct arth *  | 
7770  |  | gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg,  | 
7771  |  |     struct arth *a1)  | 
7772  | 1.87k  | { | 
7773  | 1.87k  |   struct arth *a0 = a0_arg;  | 
7774  | 1.87k  |   struct slist *s0, *s1, *s2;  | 
7775  |  |  | 
7776  |  |   /*  | 
7777  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7778  |  |    * on an error.  | 
7779  |  |    */  | 
7780  | 1.87k  |   if (setjmp(cstate->top_ctx))  | 
7781  | 18  |     return (NULL);  | 
7782  |  |  | 
7783  |  |   /*  | 
7784  |  |    * Disallow division by, or modulus by, zero; we do this here  | 
7785  |  |    * so that it gets done even if the optimizer is disabled.  | 
7786  |  |    *  | 
7787  |  |    * Also disallow shifts by a value greater than 31; we do this  | 
7788  |  |    * here, for the same reason.  | 
7789  |  |    */  | 
7790  | 1.85k  |   if (code == BPF_DIV) { | 
7791  | 390  |     if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)  | 
7792  | 9  |       bpf_error(cstate, "division by zero");  | 
7793  | 1.46k  |   } else if (code == BPF_MOD) { | 
7794  | 88  |     if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0)  | 
7795  | 4  |       bpf_error(cstate, "modulus by zero");  | 
7796  | 1.38k  |   } else if (code == BPF_LSH || code == BPF_RSH) { | 
7797  | 26  |     if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31)  | 
7798  | 5  |       bpf_error(cstate, "shift by more than 31 bits");  | 
7799  | 26  |   }  | 
7800  | 1.83k  |   s0 = xfer_to_x(cstate, a1);  | 
7801  | 1.83k  |   s1 = xfer_to_a(cstate, a0);  | 
7802  | 1.83k  |   s2 = new_stmt(cstate, BPF_ALU|BPF_X|code);  | 
7803  |  |  | 
7804  | 1.83k  |   sappend(s1, s2);  | 
7805  | 1.83k  |   sappend(s0, s1);  | 
7806  | 1.83k  |   sappend(a1->s, s0);  | 
7807  | 1.83k  |   sappend(a0->s, a1->s);  | 
7808  |  |  | 
7809  | 1.83k  |   free_reg(cstate, a0->regno);  | 
7810  | 1.83k  |   free_reg(cstate, a1->regno);  | 
7811  |  |  | 
7812  | 1.83k  |   s0 = new_stmt(cstate, BPF_ST);  | 
7813  | 1.83k  |   a0->regno = s0->s.k = alloc_reg(cstate);  | 
7814  | 1.83k  |   sappend(a0->s, s0);  | 
7815  |  |  | 
7816  | 1.83k  |   return a0;  | 
7817  | 1.85k  | }  | 
7818  |  |  | 
7819  |  | /*  | 
7820  |  |  * Initialize the table of used registers and the current register.  | 
7821  |  |  */  | 
7822  |  | static void  | 
7823  |  | init_regs(compiler_state_t *cstate)  | 
7824  | 3.72k  | { | 
7825  | 3.72k  |   cstate->curreg = 0;  | 
7826  | 3.72k  |   memset(cstate->regused, 0, sizeof cstate->regused);  | 
7827  | 3.72k  | }  | 
7828  |  |  | 
7829  |  | /*  | 
7830  |  |  * Return the next free register.  | 
7831  |  |  */  | 
7832  |  | static int  | 
7833  |  | alloc_reg(compiler_state_t *cstate)  | 
7834  | 5.83k  | { | 
7835  | 5.83k  |   int n = BPF_MEMWORDS;  | 
7836  |  |  | 
7837  | 8.59k  |   while (--n >= 0) { | 
7838  | 8.59k  |     if (cstate->regused[cstate->curreg])  | 
7839  | 2.75k  |       cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS;  | 
7840  | 5.83k  |     else { | 
7841  | 5.83k  |       cstate->regused[cstate->curreg] = 1;  | 
7842  | 5.83k  |       return cstate->curreg;  | 
7843  | 5.83k  |     }  | 
7844  | 8.59k  |   }  | 
7845  | 0  |   bpf_error(cstate, "too many registers needed to evaluate expression");  | 
7846  |  |   /*NOTREACHED*/  | 
7847  | 5.83k  | }  | 
7848  |  |  | 
7849  |  | /*  | 
7850  |  |  * Return a register to the table so it can  | 
7851  |  |  * be used later.  | 
7852  |  |  */  | 
7853  |  | static void  | 
7854  |  | free_reg(compiler_state_t *cstate, int n)  | 
7855  | 5.06k  | { | 
7856  | 5.06k  |   cstate->regused[n] = 0;  | 
7857  | 5.06k  | }  | 
7858  |  |  | 
7859  |  | static struct block *  | 
7860  |  | gen_len(compiler_state_t *cstate, int jmp, int n)  | 
7861  | 0  | { | 
7862  | 0  |   struct slist *s;  | 
7863  | 0  |   struct block *b;  | 
7864  |  | 
  | 
7865  | 0  |   s = new_stmt(cstate, BPF_LD|BPF_LEN);  | 
7866  | 0  |   b = new_block(cstate, JMP(jmp));  | 
7867  | 0  |   b->stmts = s;  | 
7868  | 0  |   b->s.k = n;  | 
7869  |  | 
  | 
7870  | 0  |   return b;  | 
7871  | 0  | }  | 
7872  |  |  | 
7873  |  | struct block *  | 
7874  |  | gen_greater(compiler_state_t *cstate, int n)  | 
7875  | 0  | { | 
7876  |  |   /*  | 
7877  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7878  |  |    * on an error.  | 
7879  |  |    */  | 
7880  | 0  |   if (setjmp(cstate->top_ctx))  | 
7881  | 0  |     return (NULL);  | 
7882  |  |  | 
7883  | 0  |   return gen_len(cstate, BPF_JGE, n);  | 
7884  | 0  | }  | 
7885  |  |  | 
7886  |  | /*  | 
7887  |  |  * Actually, this is less than or equal.  | 
7888  |  |  */  | 
7889  |  | struct block *  | 
7890  |  | gen_less(compiler_state_t *cstate, int n)  | 
7891  | 0  | { | 
7892  | 0  |   struct block *b;  | 
7893  |  |  | 
7894  |  |   /*  | 
7895  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7896  |  |    * on an error.  | 
7897  |  |    */  | 
7898  | 0  |   if (setjmp(cstate->top_ctx))  | 
7899  | 0  |     return (NULL);  | 
7900  |  |  | 
7901  | 0  |   b = gen_len(cstate, BPF_JGT, n);  | 
7902  | 0  |   gen_not(b);  | 
7903  |  | 
  | 
7904  | 0  |   return b;  | 
7905  | 0  | }  | 
7906  |  |  | 
7907  |  | /*  | 
7908  |  |  * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to | 
7909  |  |  * the beginning of the link-layer header.  | 
7910  |  |  * XXX - that means you can't test values in the radiotap header, but  | 
7911  |  |  * as that header is difficult if not impossible to parse generally  | 
7912  |  |  * without a loop, that might not be a severe problem.  A new keyword  | 
7913  |  |  * "radio" could be added for that, although what you'd really want  | 
7914  |  |  * would be a way of testing particular radio header values, which  | 
7915  |  |  * would generate code appropriate to the radio header in question.  | 
7916  |  |  */  | 
7917  |  | struct block *  | 
7918  |  | gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val)  | 
7919  | 0  | { | 
7920  | 0  |   struct block *b;  | 
7921  | 0  |   struct slist *s;  | 
7922  |  |  | 
7923  |  |   /*  | 
7924  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7925  |  |    * on an error.  | 
7926  |  |    */  | 
7927  | 0  |   if (setjmp(cstate->top_ctx))  | 
7928  | 0  |     return (NULL);  | 
7929  |  |  | 
7930  | 0  |   switch (op) { | 
7931  | 0  |   default:  | 
7932  | 0  |     abort();  | 
7933  |  |  | 
7934  | 0  |   case '=':  | 
7935  | 0  |     return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);  | 
7936  |  |  | 
7937  | 0  |   case '<':  | 
7938  | 0  |     b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);  | 
7939  | 0  |     return b;  | 
7940  |  |  | 
7941  | 0  |   case '>':  | 
7942  | 0  |     b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val);  | 
7943  | 0  |     return b;  | 
7944  |  |  | 
7945  | 0  |   case '|':  | 
7946  | 0  |     s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K);  | 
7947  | 0  |     break;  | 
7948  |  |  | 
7949  | 0  |   case '&':  | 
7950  | 0  |     s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
7951  | 0  |     break;  | 
7952  | 0  |   }  | 
7953  | 0  |   s->s.k = val;  | 
7954  | 0  |   b = new_block(cstate, JMP(BPF_JEQ));  | 
7955  | 0  |   b->stmts = s;  | 
7956  | 0  |   gen_not(b);  | 
7957  |  | 
  | 
7958  | 0  |   return b;  | 
7959  | 0  | }  | 
7960  |  |  | 
7961  |  | static const u_char abroadcast[] = { 0x0 }; | 
7962  |  |  | 
7963  |  | struct block *  | 
7964  |  | gen_broadcast(compiler_state_t *cstate, int proto)  | 
7965  | 0  | { | 
7966  | 0  |   bpf_u_int32 hostmask;  | 
7967  | 0  |   struct block *b0, *b1, *b2;  | 
7968  | 0  |   static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; | 
7969  |  |  | 
7970  |  |   /*  | 
7971  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
7972  |  |    * on an error.  | 
7973  |  |    */  | 
7974  | 0  |   if (setjmp(cstate->top_ctx))  | 
7975  | 0  |     return (NULL);  | 
7976  |  |  | 
7977  | 0  |   switch (proto) { | 
7978  |  |  | 
7979  | 0  |   case Q_DEFAULT:  | 
7980  | 0  |   case Q_LINK:  | 
7981  | 0  |     switch (cstate->linktype) { | 
7982  | 0  |     case DLT_ARCNET:  | 
7983  | 0  |     case DLT_ARCNET_LINUX:  | 
7984  | 0  |       return gen_ahostop(cstate, abroadcast, Q_DST);  | 
7985  | 0  |     case DLT_EN10MB:  | 
7986  | 0  |     case DLT_NETANALYZER:  | 
7987  | 0  |     case DLT_NETANALYZER_TRANSPARENT:  | 
7988  | 0  |       b1 = gen_prevlinkhdr_check(cstate);  | 
7989  | 0  |       b0 = gen_ehostop(cstate, ebroadcast, Q_DST);  | 
7990  | 0  |       if (b1 != NULL)  | 
7991  | 0  |         gen_and(b1, b0);  | 
7992  | 0  |       return b0;  | 
7993  | 0  |     case DLT_FDDI:  | 
7994  | 0  |       return gen_fhostop(cstate, ebroadcast, Q_DST);  | 
7995  | 0  |     case DLT_IEEE802:  | 
7996  | 0  |       return gen_thostop(cstate, ebroadcast, Q_DST);  | 
7997  | 0  |     case DLT_IEEE802_11:  | 
7998  | 0  |     case DLT_PRISM_HEADER:  | 
7999  | 0  |     case DLT_IEEE802_11_RADIO_AVS:  | 
8000  | 0  |     case DLT_IEEE802_11_RADIO:  | 
8001  | 0  |     case DLT_PPI:  | 
8002  | 0  |       return gen_wlanhostop(cstate, ebroadcast, Q_DST);  | 
8003  | 0  |     case DLT_IP_OVER_FC:  | 
8004  | 0  |       return gen_ipfchostop(cstate, ebroadcast, Q_DST);  | 
8005  | 0  |     default:  | 
8006  | 0  |       bpf_error(cstate, "not a broadcast link");  | 
8007  | 0  |     }  | 
8008  |  |     /*NOTREACHED*/  | 
8009  |  |  | 
8010  | 0  |   case Q_IP:  | 
8011  |  |     /*  | 
8012  |  |      * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)  | 
8013  |  |      * as an indication that we don't know the netmask, and fail  | 
8014  |  |      * in that case.  | 
8015  |  |      */  | 
8016  | 0  |     if (cstate->netmask == PCAP_NETMASK_UNKNOWN)  | 
8017  | 0  |       bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported");  | 
8018  | 0  |     b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
8019  | 0  |     hostmask = ~cstate->netmask;  | 
8020  | 0  |     b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask);  | 
8021  | 0  |     b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W,  | 
8022  | 0  |             ~0 & hostmask, hostmask);  | 
8023  | 0  |     gen_or(b1, b2);  | 
8024  | 0  |     gen_and(b0, b2);  | 
8025  | 0  |     return b2;  | 
8026  | 0  |   }  | 
8027  | 0  |   bpf_error(cstate, "only link-layer/IP broadcast filters supported");  | 
8028  |  |   /*NOTREACHED*/  | 
8029  | 0  | }  | 
8030  |  |  | 
8031  |  | /*  | 
8032  |  |  * Generate code to test the low-order bit of a MAC address (that's  | 
8033  |  |  * the bottom bit of the *first* byte).  | 
8034  |  |  */  | 
8035  |  | static struct block *  | 
8036  |  | gen_mac_multicast(compiler_state_t *cstate, int offset)  | 
8037  | 0  | { | 
8038  | 0  |   register struct block *b0;  | 
8039  | 0  |   register struct slist *s;  | 
8040  |  |  | 
8041  |  |   /* link[offset] & 1 != 0 */  | 
8042  | 0  |   s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B);  | 
8043  | 0  |   b0 = new_block(cstate, JMP(BPF_JSET));  | 
8044  | 0  |   b0->s.k = 1;  | 
8045  | 0  |   b0->stmts = s;  | 
8046  | 0  |   return b0;  | 
8047  | 0  | }  | 
8048  |  |  | 
8049  |  | struct block *  | 
8050  |  | gen_multicast(compiler_state_t *cstate, int proto)  | 
8051  | 0  | { | 
8052  | 0  |   register struct block *b0, *b1, *b2;  | 
8053  | 0  |   register struct slist *s;  | 
8054  |  |  | 
8055  |  |   /*  | 
8056  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8057  |  |    * on an error.  | 
8058  |  |    */  | 
8059  | 0  |   if (setjmp(cstate->top_ctx))  | 
8060  | 0  |     return (NULL);  | 
8061  |  |  | 
8062  | 0  |   switch (proto) { | 
8063  |  |  | 
8064  | 0  |   case Q_DEFAULT:  | 
8065  | 0  |   case Q_LINK:  | 
8066  | 0  |     switch (cstate->linktype) { | 
8067  | 0  |     case DLT_ARCNET:  | 
8068  | 0  |     case DLT_ARCNET_LINUX:  | 
8069  |  |       /* all ARCnet multicasts use the same address */  | 
8070  | 0  |       return gen_ahostop(cstate, abroadcast, Q_DST);  | 
8071  | 0  |     case DLT_EN10MB:  | 
8072  | 0  |     case DLT_NETANALYZER:  | 
8073  | 0  |     case DLT_NETANALYZER_TRANSPARENT:  | 
8074  | 0  |       b1 = gen_prevlinkhdr_check(cstate);  | 
8075  |  |       /* ether[0] & 1 != 0 */  | 
8076  | 0  |       b0 = gen_mac_multicast(cstate, 0);  | 
8077  | 0  |       if (b1 != NULL)  | 
8078  | 0  |         gen_and(b1, b0);  | 
8079  | 0  |       return b0;  | 
8080  | 0  |     case DLT_FDDI:  | 
8081  |  |       /*  | 
8082  |  |        * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX  | 
8083  |  |        *  | 
8084  |  |        * XXX - was that referring to bit-order issues?  | 
8085  |  |        */  | 
8086  |  |       /* fddi[1] & 1 != 0 */  | 
8087  | 0  |       return gen_mac_multicast(cstate, 1);  | 
8088  | 0  |     case DLT_IEEE802:  | 
8089  |  |       /* tr[2] & 1 != 0 */  | 
8090  | 0  |       return gen_mac_multicast(cstate, 2);  | 
8091  | 0  |     case DLT_IEEE802_11:  | 
8092  | 0  |     case DLT_PRISM_HEADER:  | 
8093  | 0  |     case DLT_IEEE802_11_RADIO_AVS:  | 
8094  | 0  |     case DLT_IEEE802_11_RADIO:  | 
8095  | 0  |     case DLT_PPI:  | 
8096  |  |       /*  | 
8097  |  |        * Oh, yuk.  | 
8098  |  |        *  | 
8099  |  |        *  For control frames, there is no DA.  | 
8100  |  |        *  | 
8101  |  |        *  For management frames, DA is at an  | 
8102  |  |        *  offset of 4 from the beginning of  | 
8103  |  |        *  the packet.  | 
8104  |  |        *  | 
8105  |  |        *  For data frames, DA is at an offset  | 
8106  |  |        *  of 4 from the beginning of the packet  | 
8107  |  |        *  if To DS is clear and at an offset of  | 
8108  |  |        *  16 from the beginning of the packet  | 
8109  |  |        *  if To DS is set.  | 
8110  |  |        */  | 
8111  |  |  | 
8112  |  |       /*  | 
8113  |  |        * Generate the tests to be done for data frames.  | 
8114  |  |        *  | 
8115  |  |        * First, check for To DS set, i.e. "link[1] & 0x01".  | 
8116  |  |        */  | 
8117  | 0  |       s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
8118  | 0  |       b1 = new_block(cstate, JMP(BPF_JSET));  | 
8119  | 0  |       b1->s.k = 0x01; /* To DS */  | 
8120  | 0  |       b1->stmts = s;  | 
8121  |  |  | 
8122  |  |       /*  | 
8123  |  |        * If To DS is set, the DA is at 16.  | 
8124  |  |        */  | 
8125  | 0  |       b0 = gen_mac_multicast(cstate, 16);  | 
8126  | 0  |       gen_and(b1, b0);  | 
8127  |  |  | 
8128  |  |       /*  | 
8129  |  |        * Now, check for To DS not set, i.e. check  | 
8130  |  |        * "!(link[1] & 0x01)".  | 
8131  |  |        */  | 
8132  | 0  |       s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B);  | 
8133  | 0  |       b2 = new_block(cstate, JMP(BPF_JSET));  | 
8134  | 0  |       b2->s.k = 0x01; /* To DS */  | 
8135  | 0  |       b2->stmts = s;  | 
8136  | 0  |       gen_not(b2);  | 
8137  |  |  | 
8138  |  |       /*  | 
8139  |  |        * If To DS is not set, the DA is at 4.  | 
8140  |  |        */  | 
8141  | 0  |       b1 = gen_mac_multicast(cstate, 4);  | 
8142  | 0  |       gen_and(b2, b1);  | 
8143  |  |  | 
8144  |  |       /*  | 
8145  |  |        * Now OR together the last two checks.  That gives  | 
8146  |  |        * the complete set of checks for data frames.  | 
8147  |  |        */  | 
8148  | 0  |       gen_or(b1, b0);  | 
8149  |  |  | 
8150  |  |       /*  | 
8151  |  |        * Now check for a data frame.  | 
8152  |  |        * I.e, check "link[0] & 0x08".  | 
8153  |  |        */  | 
8154  | 0  |       s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
8155  | 0  |       b1 = new_block(cstate, JMP(BPF_JSET));  | 
8156  | 0  |       b1->s.k = 0x08;  | 
8157  | 0  |       b1->stmts = s;  | 
8158  |  |  | 
8159  |  |       /*  | 
8160  |  |        * AND that with the checks done for data frames.  | 
8161  |  |        */  | 
8162  | 0  |       gen_and(b1, b0);  | 
8163  |  |  | 
8164  |  |       /*  | 
8165  |  |        * If the high-order bit of the type value is 0, this  | 
8166  |  |        * is a management frame.  | 
8167  |  |        * I.e, check "!(link[0] & 0x08)".  | 
8168  |  |        */  | 
8169  | 0  |       s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
8170  | 0  |       b2 = new_block(cstate, JMP(BPF_JSET));  | 
8171  | 0  |       b2->s.k = 0x08;  | 
8172  | 0  |       b2->stmts = s;  | 
8173  | 0  |       gen_not(b2);  | 
8174  |  |  | 
8175  |  |       /*  | 
8176  |  |        * For management frames, the DA is at 4.  | 
8177  |  |        */  | 
8178  | 0  |       b1 = gen_mac_multicast(cstate, 4);  | 
8179  | 0  |       gen_and(b2, b1);  | 
8180  |  |  | 
8181  |  |       /*  | 
8182  |  |        * OR that with the checks done for data frames.  | 
8183  |  |        * That gives the checks done for management and  | 
8184  |  |        * data frames.  | 
8185  |  |        */  | 
8186  | 0  |       gen_or(b1, b0);  | 
8187  |  |  | 
8188  |  |       /*  | 
8189  |  |        * If the low-order bit of the type value is 1,  | 
8190  |  |        * this is either a control frame or a frame  | 
8191  |  |        * with a reserved type, and thus not a  | 
8192  |  |        * frame with an SA.  | 
8193  |  |        *  | 
8194  |  |        * I.e., check "!(link[0] & 0x04)".  | 
8195  |  |        */  | 
8196  | 0  |       s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B);  | 
8197  | 0  |       b1 = new_block(cstate, JMP(BPF_JSET));  | 
8198  | 0  |       b1->s.k = 0x04;  | 
8199  | 0  |       b1->stmts = s;  | 
8200  | 0  |       gen_not(b1);  | 
8201  |  |  | 
8202  |  |       /*  | 
8203  |  |        * AND that with the checks for data and management  | 
8204  |  |        * frames.  | 
8205  |  |        */  | 
8206  | 0  |       gen_and(b1, b0);  | 
8207  | 0  |       return b0;  | 
8208  | 0  |     case DLT_IP_OVER_FC:  | 
8209  | 0  |       b0 = gen_mac_multicast(cstate, 2);  | 
8210  | 0  |       return b0;  | 
8211  | 0  |     default:  | 
8212  | 0  |       break;  | 
8213  | 0  |     }  | 
8214  |  |     /* Link not known to support multicasts */  | 
8215  | 0  |     break;  | 
8216  |  |  | 
8217  | 0  |   case Q_IP:  | 
8218  | 0  |     b0 = gen_linktype(cstate, ETHERTYPE_IP);  | 
8219  | 0  |     b1 = gen_cmp_ge(cstate, OR_LINKPL, 16, BPF_B, 224);  | 
8220  | 0  |     gen_and(b0, b1);  | 
8221  | 0  |     return b1;  | 
8222  |  |  | 
8223  | 0  |   case Q_IPV6:  | 
8224  | 0  |     b0 = gen_linktype(cstate, ETHERTYPE_IPV6);  | 
8225  | 0  |     b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255);  | 
8226  | 0  |     gen_and(b0, b1);  | 
8227  | 0  |     return b1;  | 
8228  | 0  |   }  | 
8229  | 0  |   bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");  | 
8230  |  |   /*NOTREACHED*/  | 
8231  | 0  | }  | 
8232  |  |  | 
8233  |  | struct block *  | 
8234  |  | gen_ifindex(compiler_state_t *cstate, int ifindex)  | 
8235  | 0  | { | 
8236  | 0  |   register struct block *b0;  | 
8237  |  |  | 
8238  |  |   /*  | 
8239  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8240  |  |    * on an error.  | 
8241  |  |    */  | 
8242  | 0  |   if (setjmp(cstate->top_ctx))  | 
8243  | 0  |     return (NULL);  | 
8244  |  |  | 
8245  |  |   /*  | 
8246  |  |    * Only some data link types support ifindex qualifiers.  | 
8247  |  |    */  | 
8248  | 0  |   switch (cstate->linktype) { | 
8249  | 0  |   case DLT_LINUX_SLL2:  | 
8250  |  |     /* match packets on this interface */  | 
8251  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex);  | 
8252  | 0  |     break;  | 
8253  | 0  |         default:  | 
8254  | 0  | #if defined(linux)  | 
8255  |  |     /*  | 
8256  |  |      * This is Linux; we require PF_PACKET support.  | 
8257  |  |      * If this is a *live* capture, we can look at  | 
8258  |  |      * special meta-data in the filter expression;  | 
8259  |  |      * if it's a savefile, we can't.  | 
8260  |  |      */  | 
8261  | 0  |     if (cstate->bpf_pcap->rfile != NULL) { | 
8262  |  |       /* We have a FILE *, so this is a savefile */  | 
8263  | 0  |       bpf_error(cstate, "ifindex not supported on %s when reading savefiles",  | 
8264  | 0  |           pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
8265  | 0  |       b0 = NULL;  | 
8266  |  |       /*NOTREACHED*/  | 
8267  | 0  |     }  | 
8268  |  |     /* match ifindex */  | 
8269  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W,  | 
8270  | 0  |                  ifindex);  | 
8271  |  | #else /* defined(linux) */  | 
8272  |  |     bpf_error(cstate, "ifindex not supported on %s",  | 
8273  |  |         pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
8274  |  |     /*NOTREACHED*/  | 
8275  |  | #endif /* defined(linux) */  | 
8276  | 0  |   }  | 
8277  | 0  |   return (b0);  | 
8278  | 0  | }  | 
8279  |  |  | 
8280  |  | /*  | 
8281  |  |  * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.  | 
8282  |  |  * Outbound traffic is sent by this machine, while inbound traffic is  | 
8283  |  |  * sent by a remote machine (and may include packets destined for a  | 
8284  |  |  * unicast or multicast link-layer address we are not subscribing to).  | 
8285  |  |  * These are the same definitions implemented by pcap_setdirection().  | 
8286  |  |  * Capturing only unicast traffic destined for this host is probably  | 
8287  |  |  * better accomplished using a higher-layer filter.  | 
8288  |  |  */  | 
8289  |  | struct block *  | 
8290  |  | gen_inbound(compiler_state_t *cstate, int dir)  | 
8291  | 0  | { | 
8292  | 0  |   register struct block *b0;  | 
8293  |  |  | 
8294  |  |   /*  | 
8295  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8296  |  |    * on an error.  | 
8297  |  |    */  | 
8298  | 0  |   if (setjmp(cstate->top_ctx))  | 
8299  | 0  |     return (NULL);  | 
8300  |  |  | 
8301  |  |   /*  | 
8302  |  |    * Only some data link types support inbound/outbound qualifiers.  | 
8303  |  |    */  | 
8304  | 0  |   switch (cstate->linktype) { | 
8305  | 0  |   case DLT_SLIP:  | 
8306  | 0  |     b0 = gen_relation_internal(cstate, BPF_JEQ,  | 
8307  | 0  |         gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1),  | 
8308  | 0  |         gen_loadi_internal(cstate, 0),  | 
8309  | 0  |         dir);  | 
8310  | 0  |     break;  | 
8311  |  |  | 
8312  | 0  |   case DLT_IPNET:  | 
8313  | 0  |     if (dir) { | 
8314  |  |       /* match outgoing packets */  | 
8315  | 0  |       b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND);  | 
8316  | 0  |     } else { | 
8317  |  |       /* match incoming packets */  | 
8318  | 0  |       b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND);  | 
8319  | 0  |     }  | 
8320  | 0  |     break;  | 
8321  |  |  | 
8322  | 0  |   case DLT_LINUX_SLL:  | 
8323  |  |     /* match outgoing packets */  | 
8324  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING);  | 
8325  | 0  |     if (!dir) { | 
8326  |  |       /* to filter on inbound traffic, invert the match */  | 
8327  | 0  |       gen_not(b0);  | 
8328  | 0  |     }  | 
8329  | 0  |     break;  | 
8330  |  |  | 
8331  | 0  |   case DLT_LINUX_SLL2:  | 
8332  |  |     /* match outgoing packets */  | 
8333  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING);  | 
8334  | 0  |     if (!dir) { | 
8335  |  |       /* to filter on inbound traffic, invert the match */  | 
8336  | 0  |       gen_not(b0);  | 
8337  | 0  |     }  | 
8338  | 0  |     break;  | 
8339  |  |  | 
8340  | 0  |   case DLT_PFLOG:  | 
8341  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B,  | 
8342  | 0  |         ((dir == 0) ? PF_IN : PF_OUT));  | 
8343  | 0  |     break;  | 
8344  |  |  | 
8345  | 0  |   case DLT_PPP_PPPD:  | 
8346  | 0  |     if (dir) { | 
8347  |  |       /* match outgoing packets */  | 
8348  | 0  |       b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT);  | 
8349  | 0  |     } else { | 
8350  |  |       /* match incoming packets */  | 
8351  | 0  |       b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN);  | 
8352  | 0  |     }  | 
8353  | 0  |     break;  | 
8354  |  |  | 
8355  | 0  |         case DLT_JUNIPER_MFR:  | 
8356  | 0  |         case DLT_JUNIPER_MLFR:  | 
8357  | 0  |         case DLT_JUNIPER_MLPPP:  | 
8358  | 0  |   case DLT_JUNIPER_ATM1:  | 
8359  | 0  |   case DLT_JUNIPER_ATM2:  | 
8360  | 0  |   case DLT_JUNIPER_PPPOE:  | 
8361  | 0  |   case DLT_JUNIPER_PPPOE_ATM:  | 
8362  | 0  |         case DLT_JUNIPER_GGSN:  | 
8363  | 0  |         case DLT_JUNIPER_ES:  | 
8364  | 0  |         case DLT_JUNIPER_MONITOR:  | 
8365  | 0  |         case DLT_JUNIPER_SERVICES:  | 
8366  | 0  |         case DLT_JUNIPER_ETHER:  | 
8367  | 0  |         case DLT_JUNIPER_PPP:  | 
8368  | 0  |         case DLT_JUNIPER_FRELAY:  | 
8369  | 0  |         case DLT_JUNIPER_CHDLC:  | 
8370  | 0  |         case DLT_JUNIPER_VP:  | 
8371  | 0  |         case DLT_JUNIPER_ST:  | 
8372  | 0  |         case DLT_JUNIPER_ISM:  | 
8373  | 0  |         case DLT_JUNIPER_VS:  | 
8374  | 0  |         case DLT_JUNIPER_SRX_E2E:  | 
8375  | 0  |         case DLT_JUNIPER_FIBRECHANNEL:  | 
8376  | 0  |   case DLT_JUNIPER_ATM_CEMIC:  | 
8377  |  |  | 
8378  |  |     /* juniper flags (including direction) are stored  | 
8379  |  |      * the byte after the 3-byte magic number */  | 
8380  | 0  |     if (dir) { | 
8381  |  |       /* match outgoing packets */  | 
8382  | 0  |       b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01);  | 
8383  | 0  |     } else { | 
8384  |  |       /* match incoming packets */  | 
8385  | 0  |       b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01);  | 
8386  | 0  |     }  | 
8387  | 0  |     break;  | 
8388  |  |  | 
8389  | 0  |   default:  | 
8390  |  |     /*  | 
8391  |  |      * If we have packet meta-data indicating a direction,  | 
8392  |  |      * and that metadata can be checked by BPF code, check  | 
8393  |  |      * it.  Otherwise, give up, as this link-layer type has  | 
8394  |  |      * nothing in the packet data.  | 
8395  |  |      *  | 
8396  |  |      * Currently, the only platform where a BPF filter can  | 
8397  |  |      * check that metadata is Linux with the in-kernel  | 
8398  |  |      * BPF interpreter.  If other packet capture mechanisms  | 
8399  |  |      * and BPF filters also supported this, it would be  | 
8400  |  |      * nice.  It would be even better if they made that  | 
8401  |  |      * metadata available so that we could provide it  | 
8402  |  |      * with newer capture APIs, allowing it to be saved  | 
8403  |  |      * in pcapng files.  | 
8404  |  |      */  | 
8405  | 0  | #if defined(linux)  | 
8406  |  |     /*  | 
8407  |  |      * This is Linux; we require PF_PACKET support.  | 
8408  |  |      * If this is a *live* capture, we can look at  | 
8409  |  |      * special meta-data in the filter expression;  | 
8410  |  |      * if it's a savefile, we can't.  | 
8411  |  |      */  | 
8412  | 0  |     if (cstate->bpf_pcap->rfile != NULL) { | 
8413  |  |       /* We have a FILE *, so this is a savefile */  | 
8414  | 0  |       bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles",  | 
8415  | 0  |           pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
8416  |  |       /*NOTREACHED*/  | 
8417  | 0  |     }  | 
8418  |  |     /* match outgoing packets */  | 
8419  | 0  |     b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,  | 
8420  | 0  |                  PACKET_OUTGOING);  | 
8421  | 0  |     if (!dir) { | 
8422  |  |       /* to filter on inbound traffic, invert the match */  | 
8423  | 0  |       gen_not(b0);  | 
8424  | 0  |     }  | 
8425  |  | #else /* defined(linux) */  | 
8426  |  |     bpf_error(cstate, "inbound/outbound not supported on %s",  | 
8427  |  |         pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
8428  |  |     /*NOTREACHED*/  | 
8429  |  | #endif /* defined(linux) */  | 
8430  | 0  |   }  | 
8431  | 0  |   return (b0);  | 
8432  | 0  | }  | 
8433  |  |  | 
8434  |  | /* PF firewall log matched interface */  | 
8435  |  | struct block *  | 
8436  |  | gen_pf_ifname(compiler_state_t *cstate, const char *ifname)  | 
8437  | 0  | { | 
8438  | 0  |   struct block *b0;  | 
8439  | 0  |   u_int len, off;  | 
8440  |  |  | 
8441  |  |   /*  | 
8442  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8443  |  |    * on an error.  | 
8444  |  |    */  | 
8445  | 0  |   if (setjmp(cstate->top_ctx))  | 
8446  | 0  |     return (NULL);  | 
8447  |  |  | 
8448  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8449  | 0  |     bpf_error(cstate, "ifname supported only on PF linktype");  | 
8450  |  |     /*NOTREACHED*/  | 
8451  | 0  |   }  | 
8452  | 0  |   len = sizeof(((struct pfloghdr *)0)->ifname);  | 
8453  | 0  |   off = offsetof(struct pfloghdr, ifname);  | 
8454  | 0  |   if (strlen(ifname) >= len) { | 
8455  | 0  |     bpf_error(cstate, "ifname interface names can only be %d characters",  | 
8456  | 0  |         len-1);  | 
8457  |  |     /*NOTREACHED*/  | 
8458  | 0  |   }  | 
8459  | 0  |   b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname),  | 
8460  | 0  |       (const u_char *)ifname);  | 
8461  | 0  |   return (b0);  | 
8462  | 0  | }  | 
8463  |  |  | 
8464  |  | /* PF firewall log ruleset name */  | 
8465  |  | struct block *  | 
8466  |  | gen_pf_ruleset(compiler_state_t *cstate, char *ruleset)  | 
8467  | 0  | { | 
8468  | 0  |   struct block *b0;  | 
8469  |  |  | 
8470  |  |   /*  | 
8471  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8472  |  |    * on an error.  | 
8473  |  |    */  | 
8474  | 0  |   if (setjmp(cstate->top_ctx))  | 
8475  | 0  |     return (NULL);  | 
8476  |  |  | 
8477  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8478  | 0  |     bpf_error(cstate, "ruleset supported only on PF linktype");  | 
8479  |  |     /*NOTREACHED*/  | 
8480  | 0  |   }  | 
8481  |  |  | 
8482  | 0  |   if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { | 
8483  | 0  |     bpf_error(cstate, "ruleset names can only be %ld characters",  | 
8484  | 0  |         (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));  | 
8485  |  |     /*NOTREACHED*/  | 
8486  | 0  |   }  | 
8487  |  |  | 
8488  | 0  |   b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset),  | 
8489  | 0  |       (u_int)strlen(ruleset), (const u_char *)ruleset);  | 
8490  | 0  |   return (b0);  | 
8491  | 0  | }  | 
8492  |  |  | 
8493  |  | /* PF firewall log rule number */  | 
8494  |  | struct block *  | 
8495  |  | gen_pf_rnr(compiler_state_t *cstate, int rnr)  | 
8496  | 0  | { | 
8497  | 0  |   struct block *b0;  | 
8498  |  |  | 
8499  |  |   /*  | 
8500  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8501  |  |    * on an error.  | 
8502  |  |    */  | 
8503  | 0  |   if (setjmp(cstate->top_ctx))  | 
8504  | 0  |     return (NULL);  | 
8505  |  |  | 
8506  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8507  | 0  |     bpf_error(cstate, "rnr supported only on PF linktype");  | 
8508  |  |     /*NOTREACHED*/  | 
8509  | 0  |   }  | 
8510  |  |  | 
8511  | 0  |   b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W,  | 
8512  | 0  |      (bpf_u_int32)rnr);  | 
8513  | 0  |   return (b0);  | 
8514  | 0  | }  | 
8515  |  |  | 
8516  |  | /* PF firewall log sub-rule number */  | 
8517  |  | struct block *  | 
8518  |  | gen_pf_srnr(compiler_state_t *cstate, int srnr)  | 
8519  | 0  | { | 
8520  | 0  |   struct block *b0;  | 
8521  |  |  | 
8522  |  |   /*  | 
8523  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8524  |  |    * on an error.  | 
8525  |  |    */  | 
8526  | 0  |   if (setjmp(cstate->top_ctx))  | 
8527  | 0  |     return (NULL);  | 
8528  |  |  | 
8529  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8530  | 0  |     bpf_error(cstate, "srnr supported only on PF linktype");  | 
8531  |  |     /*NOTREACHED*/  | 
8532  | 0  |   }  | 
8533  |  |  | 
8534  | 0  |   b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W,  | 
8535  | 0  |       (bpf_u_int32)srnr);  | 
8536  | 0  |   return (b0);  | 
8537  | 0  | }  | 
8538  |  |  | 
8539  |  | /* PF firewall log reason code */  | 
8540  |  | struct block *  | 
8541  |  | gen_pf_reason(compiler_state_t *cstate, int reason)  | 
8542  | 0  | { | 
8543  | 0  |   struct block *b0;  | 
8544  |  |  | 
8545  |  |   /*  | 
8546  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8547  |  |    * on an error.  | 
8548  |  |    */  | 
8549  | 0  |   if (setjmp(cstate->top_ctx))  | 
8550  | 0  |     return (NULL);  | 
8551  |  |  | 
8552  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8553  | 0  |     bpf_error(cstate, "reason supported only on PF linktype");  | 
8554  |  |     /*NOTREACHED*/  | 
8555  | 0  |   }  | 
8556  |  |  | 
8557  | 0  |   b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B,  | 
8558  | 0  |       (bpf_u_int32)reason);  | 
8559  | 0  |   return (b0);  | 
8560  | 0  | }  | 
8561  |  |  | 
8562  |  | /* PF firewall log action */  | 
8563  |  | struct block *  | 
8564  |  | gen_pf_action(compiler_state_t *cstate, int action)  | 
8565  | 0  | { | 
8566  | 0  |   struct block *b0;  | 
8567  |  |  | 
8568  |  |   /*  | 
8569  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8570  |  |    * on an error.  | 
8571  |  |    */  | 
8572  | 0  |   if (setjmp(cstate->top_ctx))  | 
8573  | 0  |     return (NULL);  | 
8574  |  |  | 
8575  | 0  |   if (cstate->linktype != DLT_PFLOG) { | 
8576  | 0  |     bpf_error(cstate, "action supported only on PF linktype");  | 
8577  |  |     /*NOTREACHED*/  | 
8578  | 0  |   }  | 
8579  |  |  | 
8580  | 0  |   b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B,  | 
8581  | 0  |       (bpf_u_int32)action);  | 
8582  | 0  |   return (b0);  | 
8583  | 0  | }  | 
8584  |  |  | 
8585  |  | /* IEEE 802.11 wireless header */  | 
8586  |  | struct block *  | 
8587  |  | gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask)  | 
8588  | 0  | { | 
8589  | 0  |   struct block *b0;  | 
8590  |  |  | 
8591  |  |   /*  | 
8592  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8593  |  |    * on an error.  | 
8594  |  |    */  | 
8595  | 0  |   if (setjmp(cstate->top_ctx))  | 
8596  | 0  |     return (NULL);  | 
8597  |  |  | 
8598  | 0  |   switch (cstate->linktype) { | 
8599  |  |  | 
8600  | 0  |   case DLT_IEEE802_11:  | 
8601  | 0  |   case DLT_PRISM_HEADER:  | 
8602  | 0  |   case DLT_IEEE802_11_RADIO_AVS:  | 
8603  | 0  |   case DLT_IEEE802_11_RADIO:  | 
8604  | 0  |     b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask);  | 
8605  | 0  |     break;  | 
8606  |  |  | 
8607  | 0  |   default:  | 
8608  | 0  |     bpf_error(cstate, "802.11 link-layer types supported only on 802.11");  | 
8609  |  |     /*NOTREACHED*/  | 
8610  | 0  |   }  | 
8611  |  |  | 
8612  | 0  |   return (b0);  | 
8613  | 0  | }  | 
8614  |  |  | 
8615  |  | struct block *  | 
8616  |  | gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir)  | 
8617  | 0  | { | 
8618  | 0  |   struct block *b0;  | 
8619  |  |  | 
8620  |  |   /*  | 
8621  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8622  |  |    * on an error.  | 
8623  |  |    */  | 
8624  | 0  |   if (setjmp(cstate->top_ctx))  | 
8625  | 0  |     return (NULL);  | 
8626  |  |  | 
8627  | 0  |   switch (cstate->linktype) { | 
8628  |  |  | 
8629  | 0  |   case DLT_IEEE802_11:  | 
8630  | 0  |   case DLT_PRISM_HEADER:  | 
8631  | 0  |   case DLT_IEEE802_11_RADIO_AVS:  | 
8632  | 0  |   case DLT_IEEE802_11_RADIO:  | 
8633  | 0  |     break;  | 
8634  |  |  | 
8635  | 0  |   default:  | 
8636  | 0  |     bpf_error(cstate, "frame direction supported only with 802.11 headers");  | 
8637  |  |     /*NOTREACHED*/  | 
8638  | 0  |   }  | 
8639  |  |  | 
8640  | 0  |   b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir,  | 
8641  | 0  |       IEEE80211_FC1_DIR_MASK);  | 
8642  |  | 
  | 
8643  | 0  |   return (b0);  | 
8644  | 0  | }  | 
8645  |  |  | 
8646  |  | struct block *  | 
8647  |  | gen_acode(compiler_state_t *cstate, const char *s, struct qual q)  | 
8648  | 0  | { | 
8649  | 0  |   struct block *b;  | 
8650  |  |  | 
8651  |  |   /*  | 
8652  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8653  |  |    * on an error.  | 
8654  |  |    */  | 
8655  | 0  |   if (setjmp(cstate->top_ctx))  | 
8656  | 0  |     return (NULL);  | 
8657  |  |  | 
8658  | 0  |   switch (cstate->linktype) { | 
8659  |  |  | 
8660  | 0  |   case DLT_ARCNET:  | 
8661  | 0  |   case DLT_ARCNET_LINUX:  | 
8662  | 0  |     if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&  | 
8663  | 0  |         q.proto == Q_LINK) { | 
8664  | 0  |       cstate->e = pcap_ether_aton(s);  | 
8665  | 0  |       if (cstate->e == NULL)  | 
8666  | 0  |         bpf_error(cstate, "malloc");  | 
8667  | 0  |       b = gen_ahostop(cstate, cstate->e, (int)q.dir);  | 
8668  | 0  |       free(cstate->e);  | 
8669  | 0  |       cstate->e = NULL;  | 
8670  | 0  |       return (b);  | 
8671  | 0  |     } else  | 
8672  | 0  |       bpf_error(cstate, "ARCnet address used in non-arc expression");  | 
8673  |  |     /*NOTREACHED*/  | 
8674  |  |  | 
8675  | 0  |   default:  | 
8676  | 0  |     bpf_error(cstate, "aid supported only on ARCnet");  | 
8677  |  |     /*NOTREACHED*/  | 
8678  | 0  |   }  | 
8679  | 0  | }  | 
8680  |  |  | 
8681  |  | static struct block *  | 
8682  |  | gen_ahostop(compiler_state_t *cstate, const u_char *eaddr, int dir)  | 
8683  | 0  | { | 
8684  | 0  |   register struct block *b0, *b1;  | 
8685  |  | 
  | 
8686  | 0  |   switch (dir) { | 
8687  |  |   /* src comes first, different from Ethernet */  | 
8688  | 0  |   case Q_SRC:  | 
8689  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 0, 1, eaddr);  | 
8690  |  |  | 
8691  | 0  |   case Q_DST:  | 
8692  | 0  |     return gen_bcmp(cstate, OR_LINKHDR, 1, 1, eaddr);  | 
8693  |  |  | 
8694  | 0  |   case Q_AND:  | 
8695  | 0  |     b0 = gen_ahostop(cstate, eaddr, Q_SRC);  | 
8696  | 0  |     b1 = gen_ahostop(cstate, eaddr, Q_DST);  | 
8697  | 0  |     gen_and(b0, b1);  | 
8698  | 0  |     return b1;  | 
8699  |  |  | 
8700  | 0  |   case Q_DEFAULT:  | 
8701  | 0  |   case Q_OR:  | 
8702  | 0  |     b0 = gen_ahostop(cstate, eaddr, Q_SRC);  | 
8703  | 0  |     b1 = gen_ahostop(cstate, eaddr, Q_DST);  | 
8704  | 0  |     gen_or(b0, b1);  | 
8705  | 0  |     return b1;  | 
8706  |  |  | 
8707  | 0  |   case Q_ADDR1:  | 
8708  | 0  |     bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11");  | 
8709  |  |     /*NOTREACHED*/  | 
8710  |  |  | 
8711  | 0  |   case Q_ADDR2:  | 
8712  | 0  |     bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11");  | 
8713  |  |     /*NOTREACHED*/  | 
8714  |  |  | 
8715  | 0  |   case Q_ADDR3:  | 
8716  | 0  |     bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11");  | 
8717  |  |     /*NOTREACHED*/  | 
8718  |  |  | 
8719  | 0  |   case Q_ADDR4:  | 
8720  | 0  |     bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11");  | 
8721  |  |     /*NOTREACHED*/  | 
8722  |  |  | 
8723  | 0  |   case Q_RA:  | 
8724  | 0  |     bpf_error(cstate, "'ra' is only supported on 802.11");  | 
8725  |  |     /*NOTREACHED*/  | 
8726  |  |  | 
8727  | 0  |   case Q_TA:  | 
8728  | 0  |     bpf_error(cstate, "'ta' is only supported on 802.11");  | 
8729  |  |     /*NOTREACHED*/  | 
8730  | 0  |   }  | 
8731  | 0  |   abort();  | 
8732  |  |   /*NOTREACHED*/  | 
8733  | 0  | }  | 
8734  |  |  | 
8735  |  | static struct block *  | 
8736  |  | gen_vlan_tpid_test(compiler_state_t *cstate)  | 
8737  | 0  | { | 
8738  | 0  |   struct block *b0, *b1;  | 
8739  |  |  | 
8740  |  |   /* check for VLAN, including 802.1ad and QinQ */  | 
8741  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_8021Q);  | 
8742  | 0  |   b1 = gen_linktype(cstate, ETHERTYPE_8021AD);  | 
8743  | 0  |   gen_or(b0,b1);  | 
8744  | 0  |   b0 = b1;  | 
8745  | 0  |   b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ);  | 
8746  | 0  |   gen_or(b0,b1);  | 
8747  |  | 
  | 
8748  | 0  |   return b1;  | 
8749  | 0  | }  | 
8750  |  |  | 
8751  |  | static struct block *  | 
8752  |  | gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num)  | 
8753  | 0  | { | 
8754  | 0  |   if (vlan_num > 0x0fff) { | 
8755  | 0  |     bpf_error(cstate, "VLAN tag %u greater than maximum %u",  | 
8756  | 0  |         vlan_num, 0x0fff);  | 
8757  | 0  |   }  | 
8758  | 0  |   return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff);  | 
8759  | 0  | }  | 
8760  |  |  | 
8761  |  | static struct block *  | 
8762  |  | gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,  | 
8763  |  |     int has_vlan_tag)  | 
8764  | 0  | { | 
8765  | 0  |   struct block *b0, *b1;  | 
8766  |  | 
  | 
8767  | 0  |   b0 = gen_vlan_tpid_test(cstate);  | 
8768  |  | 
  | 
8769  | 0  |   if (has_vlan_tag) { | 
8770  | 0  |     b1 = gen_vlan_vid_test(cstate, vlan_num);  | 
8771  | 0  |     gen_and(b0, b1);  | 
8772  | 0  |     b0 = b1;  | 
8773  | 0  |   }  | 
8774  |  |  | 
8775  |  |   /*  | 
8776  |  |    * Both payload and link header type follow the VLAN tags so that  | 
8777  |  |    * both need to be updated.  | 
8778  |  |    */  | 
8779  | 0  |   cstate->off_linkpl.constant_part += 4;  | 
8780  | 0  |   cstate->off_linktype.constant_part += 4;  | 
8781  |  | 
  | 
8782  | 0  |   return b0;  | 
8783  | 0  | }  | 
8784  |  |  | 
8785  |  | #if defined(SKF_AD_VLAN_TAG_PRESENT)  | 
8786  |  | /* add v to variable part of off */  | 
8787  |  | static void  | 
8788  |  | gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off,  | 
8789  |  |     bpf_u_int32 v, struct slist *s)  | 
8790  | 0  | { | 
8791  | 0  |   struct slist *s2;  | 
8792  |  | 
  | 
8793  | 0  |   if (!off->is_variable)  | 
8794  | 0  |     off->is_variable = 1;  | 
8795  | 0  |   if (off->reg == -1)  | 
8796  | 0  |     off->reg = alloc_reg(cstate);  | 
8797  |  | 
  | 
8798  | 0  |   s2 = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
8799  | 0  |   s2->s.k = off->reg;  | 
8800  | 0  |   sappend(s, s2);  | 
8801  | 0  |   s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM);  | 
8802  | 0  |   s2->s.k = v;  | 
8803  | 0  |   sappend(s, s2);  | 
8804  | 0  |   s2 = new_stmt(cstate, BPF_ST);  | 
8805  | 0  |   s2->s.k = off->reg;  | 
8806  | 0  |   sappend(s, s2);  | 
8807  | 0  | }  | 
8808  |  |  | 
8809  |  | /*  | 
8810  |  |  * patch block b_tpid (VLAN TPID test) to update variable parts of link payload  | 
8811  |  |  * and link type offsets first  | 
8812  |  |  */  | 
8813  |  | static void  | 
8814  |  | gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid)  | 
8815  | 0  | { | 
8816  | 0  |   struct slist s;  | 
8817  |  |  | 
8818  |  |   /* offset determined at run time, shift variable part */  | 
8819  | 0  |   s.next = NULL;  | 
8820  | 0  |   cstate->is_vlan_vloffset = 1;  | 
8821  | 0  |   gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s);  | 
8822  | 0  |   gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s);  | 
8823  |  |  | 
8824  |  |   /* we get a pointer to a chain of or-ed blocks, patch first of them */  | 
8825  | 0  |   sappend(s.next, b_tpid->head->stmts);  | 
8826  | 0  |   b_tpid->head->stmts = s.next;  | 
8827  | 0  | }  | 
8828  |  |  | 
8829  |  | /*  | 
8830  |  |  * patch block b_vid (VLAN id test) to load VID value either from packet  | 
8831  |  |  * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true  | 
8832  |  |  */  | 
8833  |  | static void  | 
8834  |  | gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid)  | 
8835  | 0  | { | 
8836  | 0  |   struct slist *s, *s2, *sjeq;  | 
8837  | 0  |   unsigned cnt;  | 
8838  |  | 
  | 
8839  | 0  |   s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
8840  | 0  |   s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;  | 
8841  |  |  | 
8842  |  |   /* true -> next instructions, false -> beginning of b_vid */  | 
8843  | 0  |   sjeq = new_stmt(cstate, JMP(BPF_JEQ));  | 
8844  | 0  |   sjeq->s.k = 1;  | 
8845  | 0  |   sjeq->s.jf = b_vid->stmts;  | 
8846  | 0  |   sappend(s, sjeq);  | 
8847  |  | 
  | 
8848  | 0  |   s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
8849  | 0  |   s2->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG;  | 
8850  | 0  |   sappend(s, s2);  | 
8851  | 0  |   sjeq->s.jt = s2;  | 
8852  |  |  | 
8853  |  |   /* Jump to the test in b_vid. We need to jump one instruction before  | 
8854  |  |    * the end of the b_vid block so that we only skip loading the TCI  | 
8855  |  |    * from packet data and not the 'and' instruction extractging VID.  | 
8856  |  |    */  | 
8857  | 0  |   cnt = 0;  | 
8858  | 0  |   for (s2 = b_vid->stmts; s2; s2 = s2->next)  | 
8859  | 0  |     cnt++;  | 
8860  | 0  |   s2 = new_stmt(cstate, JMP(BPF_JA));  | 
8861  | 0  |   s2->s.k = cnt - 1;  | 
8862  | 0  |   sappend(s, s2);  | 
8863  |  |  | 
8864  |  |   /* insert our statements at the beginning of b_vid */  | 
8865  | 0  |   sappend(s, b_vid->stmts);  | 
8866  | 0  |   b_vid->stmts = s;  | 
8867  | 0  | }  | 
8868  |  |  | 
8869  |  | /*  | 
8870  |  |  * Generate check for "vlan" or "vlan <id>" on systems with support for BPF  | 
8871  |  |  * extensions.  Even if kernel supports VLAN BPF extensions, (outermost) VLAN  | 
8872  |  |  * tag can be either in metadata or in packet data; therefore if the  | 
8873  |  |  * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link  | 
8874  |  |  * header for VLAN tag. As the decision is done at run time, we need  | 
8875  |  |  * update variable part of the offsets  | 
8876  |  |  */  | 
8877  |  | static struct block *  | 
8878  |  | gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num,  | 
8879  |  |     int has_vlan_tag)  | 
8880  | 0  | { | 
8881  | 0  |         struct block *b0, *b_tpid, *b_vid = NULL;  | 
8882  | 0  |         struct slist *s;  | 
8883  |  |  | 
8884  |  |         /* generate new filter code based on extracting packet  | 
8885  |  |          * metadata */  | 
8886  | 0  |         s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS);  | 
8887  | 0  |         s->s.k = SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT;  | 
8888  |  | 
  | 
8889  | 0  |         b0 = new_block(cstate, JMP(BPF_JEQ));  | 
8890  | 0  |         b0->stmts = s;  | 
8891  | 0  |         b0->s.k = 1;  | 
8892  |  |  | 
8893  |  |   /*  | 
8894  |  |    * This is tricky. We need to insert the statements updating variable  | 
8895  |  |    * parts of offsets before the traditional TPID and VID tests so  | 
8896  |  |    * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but  | 
8897  |  |    * we do not want this update to affect those checks. That's why we  | 
8898  |  |    * generate both test blocks first and insert the statements updating  | 
8899  |  |    * variable parts of both offsets after that. This wouldn't work if  | 
8900  |  |    * there already were variable length link header when entering this  | 
8901  |  |    * function but gen_vlan_bpf_extensions() isn't called in that case.  | 
8902  |  |    */  | 
8903  | 0  |   b_tpid = gen_vlan_tpid_test(cstate);  | 
8904  | 0  |   if (has_vlan_tag)  | 
8905  | 0  |     b_vid = gen_vlan_vid_test(cstate, vlan_num);  | 
8906  |  | 
  | 
8907  | 0  |   gen_vlan_patch_tpid_test(cstate, b_tpid);  | 
8908  | 0  |   gen_or(b0, b_tpid);  | 
8909  | 0  |   b0 = b_tpid;  | 
8910  |  | 
  | 
8911  | 0  |   if (has_vlan_tag) { | 
8912  | 0  |     gen_vlan_patch_vid_test(cstate, b_vid);  | 
8913  | 0  |     gen_and(b0, b_vid);  | 
8914  | 0  |     b0 = b_vid;  | 
8915  | 0  |   }  | 
8916  |  | 
  | 
8917  | 0  |         return b0;  | 
8918  | 0  | }  | 
8919  |  | #endif  | 
8920  |  |  | 
8921  |  | /*  | 
8922  |  |  * support IEEE 802.1Q VLAN trunk over ethernet  | 
8923  |  |  */  | 
8924  |  | struct block *  | 
8925  |  | gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag)  | 
8926  | 0  | { | 
8927  | 0  |   struct  block *b0;  | 
8928  |  |  | 
8929  |  |   /*  | 
8930  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
8931  |  |    * on an error.  | 
8932  |  |    */  | 
8933  | 0  |   if (setjmp(cstate->top_ctx))  | 
8934  | 0  |     return (NULL);  | 
8935  |  |  | 
8936  |  |   /* can't check for VLAN-encapsulated packets inside MPLS */  | 
8937  | 0  |   if (cstate->label_stack_depth > 0)  | 
8938  | 0  |     bpf_error(cstate, "no VLAN match after MPLS");  | 
8939  |  |  | 
8940  |  |   /*  | 
8941  |  |    * Check for a VLAN packet, and then change the offsets to point  | 
8942  |  |    * to the type and data fields within the VLAN packet.  Just  | 
8943  |  |    * increment the offsets, so that we can support a hierarchy, e.g.  | 
8944  |  |    * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within  | 
8945  |  |    * VLAN 100.  | 
8946  |  |    *  | 
8947  |  |    * XXX - this is a bit of a kludge.  If we were to split the  | 
8948  |  |    * compiler into a parser that parses an expression and  | 
8949  |  |    * generates an expression tree, and a code generator that  | 
8950  |  |    * takes an expression tree (which could come from our  | 
8951  |  |    * parser or from some other parser) and generates BPF code,  | 
8952  |  |    * we could perhaps make the offsets parameters of routines  | 
8953  |  |    * and, in the handler for an "AND" node, pass to subnodes  | 
8954  |  |    * other than the VLAN node the adjusted offsets.  | 
8955  |  |    *  | 
8956  |  |    * This would mean that "vlan" would, instead of changing the  | 
8957  |  |    * behavior of *all* tests after it, change only the behavior  | 
8958  |  |    * of tests ANDed with it.  That would change the documented  | 
8959  |  |    * semantics of "vlan", which might break some expressions.  | 
8960  |  |    * However, it would mean that "(vlan and ip) or ip" would check  | 
8961  |  |    * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than  | 
8962  |  |    * checking only for VLAN-encapsulated IP, so that could still  | 
8963  |  |    * be considered worth doing; it wouldn't break expressions  | 
8964  |  |    * that are of the form "vlan and ..." or "vlan N and ...",  | 
8965  |  |    * which I suspect are the most common expressions involving  | 
8966  |  |    * "vlan".  "vlan or ..." doesn't necessarily do what the user  | 
8967  |  |    * would really want, now, as all the "or ..." tests would  | 
8968  |  |    * be done assuming a VLAN, even though the "or" could be viewed  | 
8969  |  |    * as meaning "or, if this isn't a VLAN packet...".  | 
8970  |  |    */  | 
8971  | 0  |   switch (cstate->linktype) { | 
8972  |  |  | 
8973  | 0  |   case DLT_EN10MB:  | 
8974  | 0  |   case DLT_NETANALYZER:  | 
8975  | 0  |   case DLT_NETANALYZER_TRANSPARENT:  | 
8976  | 0  | #if defined(SKF_AD_VLAN_TAG_PRESENT)  | 
8977  |  |     /* Verify that this is the outer part of the packet and  | 
8978  |  |      * not encapsulated somehow. */  | 
8979  | 0  |     if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable &&  | 
8980  | 0  |         cstate->off_linkhdr.constant_part ==  | 
8981  | 0  |         cstate->off_outermostlinkhdr.constant_part) { | 
8982  |  |       /*  | 
8983  |  |        * Do we need special VLAN handling?  | 
8984  |  |        */  | 
8985  | 0  |       if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING)  | 
8986  | 0  |         b0 = gen_vlan_bpf_extensions(cstate, vlan_num,  | 
8987  | 0  |             has_vlan_tag);  | 
8988  | 0  |       else  | 
8989  | 0  |         b0 = gen_vlan_no_bpf_extensions(cstate,  | 
8990  | 0  |             vlan_num, has_vlan_tag);  | 
8991  | 0  |     } else  | 
8992  | 0  | #endif  | 
8993  | 0  |       b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num,  | 
8994  | 0  |           has_vlan_tag);  | 
8995  | 0  |                 break;  | 
8996  |  |  | 
8997  | 0  |   case DLT_IEEE802_11:  | 
8998  | 0  |   case DLT_PRISM_HEADER:  | 
8999  | 0  |   case DLT_IEEE802_11_RADIO_AVS:  | 
9000  | 0  |   case DLT_IEEE802_11_RADIO:  | 
9001  | 0  |     b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag);  | 
9002  | 0  |     break;  | 
9003  |  |  | 
9004  | 0  |   default:  | 
9005  | 0  |     bpf_error(cstate, "no VLAN support for %s",  | 
9006  | 0  |           pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
9007  |  |     /*NOTREACHED*/  | 
9008  | 0  |   }  | 
9009  |  |  | 
9010  | 0  |         cstate->vlan_stack_depth++;  | 
9011  |  | 
  | 
9012  | 0  |   return (b0);  | 
9013  | 0  | }  | 
9014  |  |  | 
9015  |  | /*  | 
9016  |  |  * support for MPLS  | 
9017  |  |  *  | 
9018  |  |  * The label_num_arg dance is to avoid annoying whining by compilers that  | 
9019  |  |  * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?  | 
9020  |  |  * It's not *used* after setjmp returns.  | 
9021  |  |  */  | 
9022  |  | struct block *  | 
9023  |  | gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num_arg,  | 
9024  |  |     int has_label_num)  | 
9025  | 0  | { | 
9026  | 0  |   volatile bpf_u_int32 label_num = label_num_arg;  | 
9027  | 0  |   struct  block *b0, *b1;  | 
9028  |  |  | 
9029  |  |   /*  | 
9030  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9031  |  |    * on an error.  | 
9032  |  |    */  | 
9033  | 0  |   if (setjmp(cstate->top_ctx))  | 
9034  | 0  |     return (NULL);  | 
9035  |  |  | 
9036  | 0  |         if (cstate->label_stack_depth > 0) { | 
9037  |  |             /* just match the bottom-of-stack bit clear */  | 
9038  | 0  |             b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01);  | 
9039  | 0  |         } else { | 
9040  |  |             /*  | 
9041  |  |              * We're not in an MPLS stack yet, so check the link-layer  | 
9042  |  |              * type against MPLS.  | 
9043  |  |              */  | 
9044  | 0  |             switch (cstate->linktype) { | 
9045  |  |  | 
9046  | 0  |             case DLT_C_HDLC: /* fall through */  | 
9047  | 0  |             case DLT_HDLC:  | 
9048  | 0  |             case DLT_EN10MB:  | 
9049  | 0  |             case DLT_NETANALYZER:  | 
9050  | 0  |             case DLT_NETANALYZER_TRANSPARENT:  | 
9051  | 0  |                     b0 = gen_linktype(cstate, ETHERTYPE_MPLS);  | 
9052  | 0  |                     break;  | 
9053  |  |  | 
9054  | 0  |             case DLT_PPP:  | 
9055  | 0  |                     b0 = gen_linktype(cstate, PPP_MPLS_UCAST);  | 
9056  | 0  |                     break;  | 
9057  |  |  | 
9058  |  |                     /* FIXME add other DLT_s ...  | 
9059  |  |                      * for Frame-Relay/and ATM this may get messy due to SNAP headers  | 
9060  |  |                      * leave it for now */  | 
9061  |  |  | 
9062  | 0  |             default:  | 
9063  | 0  |                     bpf_error(cstate, "no MPLS support for %s",  | 
9064  | 0  |                           pcap_datalink_val_to_description_or_dlt(cstate->linktype));  | 
9065  |  |                     /*NOTREACHED*/  | 
9066  | 0  |             }  | 
9067  | 0  |         }  | 
9068  |  |  | 
9069  |  |   /* If a specific MPLS label is requested, check it */  | 
9070  | 0  |   if (has_label_num) { | 
9071  | 0  |     if (label_num > 0xFFFFF) { | 
9072  | 0  |       bpf_error(cstate, "MPLS label %u greater than maximum %u",  | 
9073  | 0  |           label_num, 0xFFFFF);  | 
9074  | 0  |     }  | 
9075  | 0  |     label_num = label_num << 12; /* label is shifted 12 bits on the wire */  | 
9076  | 0  |     b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num,  | 
9077  | 0  |         0xfffff000); /* only compare the first 20 bits */  | 
9078  | 0  |     gen_and(b0, b1);  | 
9079  | 0  |     b0 = b1;  | 
9080  | 0  |   }  | 
9081  |  |  | 
9082  |  |         /*  | 
9083  |  |          * Change the offsets to point to the type and data fields within  | 
9084  |  |          * the MPLS packet.  Just increment the offsets, so that we  | 
9085  |  |          * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to  | 
9086  |  |          * capture packets with an outer label of 100000 and an inner  | 
9087  |  |          * label of 1024.  | 
9088  |  |          *  | 
9089  |  |          * Increment the MPLS stack depth as well; this indicates that  | 
9090  |  |          * we're checking MPLS-encapsulated headers, to make sure higher  | 
9091  |  |          * level code generators don't try to match against IP-related  | 
9092  |  |          * protocols such as Q_ARP, Q_RARP etc.  | 
9093  |  |          *  | 
9094  |  |          * XXX - this is a bit of a kludge.  See comments in gen_vlan().  | 
9095  |  |          */  | 
9096  | 0  |         cstate->off_nl_nosnap += 4;  | 
9097  | 0  |         cstate->off_nl += 4;  | 
9098  | 0  |         cstate->label_stack_depth++;  | 
9099  | 0  |   return (b0);  | 
9100  | 0  | }  | 
9101  |  |  | 
9102  |  | /*  | 
9103  |  |  * Support PPPOE discovery and session.  | 
9104  |  |  */  | 
9105  |  | struct block *  | 
9106  |  | gen_pppoed(compiler_state_t *cstate)  | 
9107  | 0  | { | 
9108  |  |   /*  | 
9109  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9110  |  |    * on an error.  | 
9111  |  |    */  | 
9112  | 0  |   if (setjmp(cstate->top_ctx))  | 
9113  | 0  |     return (NULL);  | 
9114  |  |  | 
9115  |  |   /* check for PPPoE discovery */  | 
9116  | 0  |   return gen_linktype(cstate, ETHERTYPE_PPPOED);  | 
9117  | 0  | }  | 
9118  |  |  | 
9119  |  | struct block *  | 
9120  |  | gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num)  | 
9121  | 0  | { | 
9122  | 0  |   struct block *b0, *b1;  | 
9123  |  |  | 
9124  |  |   /*  | 
9125  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9126  |  |    * on an error.  | 
9127  |  |    */  | 
9128  | 0  |   if (setjmp(cstate->top_ctx))  | 
9129  | 0  |     return (NULL);  | 
9130  |  |  | 
9131  |  |   /*  | 
9132  |  |    * Test against the PPPoE session link-layer type.  | 
9133  |  |    */  | 
9134  | 0  |   b0 = gen_linktype(cstate, ETHERTYPE_PPPOES);  | 
9135  |  |  | 
9136  |  |   /* If a specific session is requested, check PPPoE session id */  | 
9137  | 0  |   if (has_sess_num) { | 
9138  | 0  |     if (sess_num > 0x0000ffff) { | 
9139  | 0  |       bpf_error(cstate, "PPPoE session number %u greater than maximum %u",  | 
9140  | 0  |           sess_num, 0x0000ffff);  | 
9141  | 0  |     }  | 
9142  | 0  |     b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff);  | 
9143  | 0  |     gen_and(b0, b1);  | 
9144  | 0  |     b0 = b1;  | 
9145  | 0  |   }  | 
9146  |  |  | 
9147  |  |   /*  | 
9148  |  |    * Change the offsets to point to the type and data fields within  | 
9149  |  |    * the PPP packet, and note that this is PPPoE rather than  | 
9150  |  |    * raw PPP.  | 
9151  |  |    *  | 
9152  |  |    * XXX - this is a bit of a kludge.  See the comments in  | 
9153  |  |    * gen_vlan().  | 
9154  |  |    *  | 
9155  |  |    * The "network-layer" protocol is PPPoE, which has a 6-byte  | 
9156  |  |    * PPPoE header, followed by a PPP packet.  | 
9157  |  |    *  | 
9158  |  |    * There is no HDLC encapsulation for the PPP packet (it's  | 
9159  |  |    * encapsulated in PPPoES instead), so the link-layer type  | 
9160  |  |    * starts at the first byte of the PPP packet.  For PPPoE,  | 
9161  |  |    * that offset is relative to the beginning of the total  | 
9162  |  |    * link-layer payload, including any 802.2 LLC header, so  | 
9163  |  |    * it's 6 bytes past cstate->off_nl.  | 
9164  |  |    */  | 
9165  | 0  |   PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable,  | 
9166  | 0  |       cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */  | 
9167  | 0  |       cstate->off_linkpl.reg);  | 
9168  |  | 
  | 
9169  | 0  |   cstate->off_linktype = cstate->off_linkhdr;  | 
9170  | 0  |   cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2;  | 
9171  |  | 
  | 
9172  | 0  |   cstate->off_nl = 0;  | 
9173  | 0  |   cstate->off_nl_nosnap = 0;  /* no 802.2 LLC */  | 
9174  |  | 
  | 
9175  | 0  |   return b0;  | 
9176  | 0  | }  | 
9177  |  |  | 
9178  |  | /* Check that this is Geneve and the VNI is correct if  | 
9179  |  |  * specified. Parameterized to handle both IPv4 and IPv6. */  | 
9180  |  | static struct block *  | 
9181  |  | gen_geneve_check(compiler_state_t *cstate,  | 
9182  |  |     struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int),  | 
9183  |  |     enum e_offrel offrel, bpf_u_int32 vni, int has_vni)  | 
9184  | 0  | { | 
9185  | 0  |   struct block *b0, *b1;  | 
9186  |  | 
  | 
9187  | 0  |   b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST);  | 
9188  |  |  | 
9189  |  |   /* Check that we are operating on version 0. Otherwise, we  | 
9190  |  |    * can't decode the rest of the fields. The version is 2 bits  | 
9191  |  |    * in the first byte of the Geneve header. */  | 
9192  | 0  |   b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0);  | 
9193  | 0  |   gen_and(b0, b1);  | 
9194  | 0  |   b0 = b1;  | 
9195  |  | 
  | 
9196  | 0  |   if (has_vni) { | 
9197  | 0  |     if (vni > 0xffffff) { | 
9198  | 0  |       bpf_error(cstate, "Geneve VNI %u greater than maximum %u",  | 
9199  | 0  |           vni, 0xffffff);  | 
9200  | 0  |     }  | 
9201  | 0  |     vni <<= 8; /* VNI is in the upper 3 bytes */  | 
9202  | 0  |     b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00);  | 
9203  | 0  |     gen_and(b0, b1);  | 
9204  | 0  |     b0 = b1;  | 
9205  | 0  |   }  | 
9206  |  |  | 
9207  | 0  |   return b0;  | 
9208  | 0  | }  | 
9209  |  |  | 
9210  |  | /* The IPv4 and IPv6 Geneve checks need to do two things:  | 
9211  |  |  * - Verify that this actually is Geneve with the right VNI.  | 
9212  |  |  * - Place the IP header length (plus variable link prefix if  | 
9213  |  |  *   needed) into register A to be used later to compute  | 
9214  |  |  *   the inner packet offsets. */  | 
9215  |  | static struct block *  | 
9216  |  | gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)  | 
9217  | 0  | { | 
9218  | 0  |   struct block *b0, *b1;  | 
9219  | 0  |   struct slist *s, *s1;  | 
9220  |  | 
  | 
9221  | 0  |   b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni);  | 
9222  |  |  | 
9223  |  |   /* Load the IP header length into A. */  | 
9224  | 0  |   s = gen_loadx_iphdrlen(cstate);  | 
9225  |  | 
  | 
9226  | 0  |   s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);  | 
9227  | 0  |   sappend(s, s1);  | 
9228  |  |  | 
9229  |  |   /* Forcibly append these statements to the true condition  | 
9230  |  |    * of the protocol check by creating a new block that is  | 
9231  |  |    * always true and ANDing them. */  | 
9232  | 0  |   b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);  | 
9233  | 0  |   b1->stmts = s;  | 
9234  | 0  |   b1->s.k = 0;  | 
9235  |  | 
  | 
9236  | 0  |   gen_and(b0, b1);  | 
9237  |  | 
  | 
9238  | 0  |   return b1;  | 
9239  | 0  | }  | 
9240  |  |  | 
9241  |  | static struct block *  | 
9242  |  | gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)  | 
9243  | 0  | { | 
9244  | 0  |   struct block *b0, *b1;  | 
9245  | 0  |   struct slist *s, *s1;  | 
9246  |  | 
  | 
9247  | 0  |   b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni);  | 
9248  |  |  | 
9249  |  |   /* Load the IP header length. We need to account for a  | 
9250  |  |    * variable length link prefix if there is one. */  | 
9251  | 0  |   s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl);  | 
9252  | 0  |   if (s) { | 
9253  | 0  |     s1 = new_stmt(cstate, BPF_LD|BPF_IMM);  | 
9254  | 0  |     s1->s.k = 40;  | 
9255  | 0  |     sappend(s, s1);  | 
9256  |  | 
  | 
9257  | 0  |     s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);  | 
9258  | 0  |     s1->s.k = 0;  | 
9259  | 0  |     sappend(s, s1);  | 
9260  | 0  |   } else { | 
9261  | 0  |     s = new_stmt(cstate, BPF_LD|BPF_IMM);  | 
9262  | 0  |     s->s.k = 40;  | 
9263  | 0  |   }  | 
9264  |  |  | 
9265  |  |   /* Forcibly append these statements to the true condition  | 
9266  |  |    * of the protocol check by creating a new block that is  | 
9267  |  |    * always true and ANDing them. */  | 
9268  | 0  |   s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
9269  | 0  |   sappend(s, s1);  | 
9270  |  | 
  | 
9271  | 0  |   b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);  | 
9272  | 0  |   b1->stmts = s;  | 
9273  | 0  |   b1->s.k = 0;  | 
9274  |  | 
  | 
9275  | 0  |   gen_and(b0, b1);  | 
9276  |  | 
  | 
9277  | 0  |   return b1;  | 
9278  | 0  | }  | 
9279  |  |  | 
9280  |  | /* We need to store three values based on the Geneve header::  | 
9281  |  |  * - The offset of the linktype.  | 
9282  |  |  * - The offset of the end of the Geneve header.  | 
9283  |  |  * - The offset of the end of the encapsulated MAC header. */  | 
9284  |  | static struct slist *  | 
9285  |  | gen_geneve_offsets(compiler_state_t *cstate)  | 
9286  | 0  | { | 
9287  | 0  |   struct slist *s, *s1, *s_proto;  | 
9288  |  |  | 
9289  |  |   /* First we need to calculate the offset of the Geneve header  | 
9290  |  |    * itself. This is composed of the IP header previously calculated  | 
9291  |  |    * (include any variable link prefix) and stored in A plus the  | 
9292  |  |    * fixed sized headers (fixed link prefix, MAC length, and UDP  | 
9293  |  |    * header). */  | 
9294  | 0  |   s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
9295  | 0  |   s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8;  | 
9296  |  |  | 
9297  |  |   /* Stash this in X since we'll need it later. */  | 
9298  | 0  |   s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
9299  | 0  |   sappend(s, s1);  | 
9300  |  |  | 
9301  |  |   /* The EtherType in Geneve is 2 bytes in. Calculate this and  | 
9302  |  |    * store it. */  | 
9303  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
9304  | 0  |   s1->s.k = 2;  | 
9305  | 0  |   sappend(s, s1);  | 
9306  |  | 
  | 
9307  | 0  |   cstate->off_linktype.reg = alloc_reg(cstate);  | 
9308  | 0  |   cstate->off_linktype.is_variable = 1;  | 
9309  | 0  |   cstate->off_linktype.constant_part = 0;  | 
9310  |  | 
  | 
9311  | 0  |   s1 = new_stmt(cstate, BPF_ST);  | 
9312  | 0  |   s1->s.k = cstate->off_linktype.reg;  | 
9313  | 0  |   sappend(s, s1);  | 
9314  |  |  | 
9315  |  |   /* Load the Geneve option length and mask and shift to get the  | 
9316  |  |    * number of bytes. It is stored in the first byte of the Geneve  | 
9317  |  |    * header. */  | 
9318  | 0  |   s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B);  | 
9319  | 0  |   s1->s.k = 0;  | 
9320  | 0  |   sappend(s, s1);  | 
9321  |  | 
  | 
9322  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K);  | 
9323  | 0  |   s1->s.k = 0x3f;  | 
9324  | 0  |   sappend(s, s1);  | 
9325  |  | 
  | 
9326  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K);  | 
9327  | 0  |   s1->s.k = 4;  | 
9328  | 0  |   sappend(s, s1);  | 
9329  |  |  | 
9330  |  |   /* Add in the rest of the Geneve base header. */  | 
9331  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
9332  | 0  |   s1->s.k = 8;  | 
9333  | 0  |   sappend(s, s1);  | 
9334  |  |  | 
9335  |  |   /* Add the Geneve header length to its offset and store. */  | 
9336  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X);  | 
9337  | 0  |   s1->s.k = 0;  | 
9338  | 0  |   sappend(s, s1);  | 
9339  |  |  | 
9340  |  |   /* Set the encapsulated type as Ethernet. Even though we may  | 
9341  |  |    * not actually have Ethernet inside there are two reasons this  | 
9342  |  |    * is useful:  | 
9343  |  |    * - The linktype field is always in EtherType format regardless  | 
9344  |  |    *   of whether it is in Geneve or an inner Ethernet frame.  | 
9345  |  |    * - The only link layer that we have specific support for is  | 
9346  |  |    *   Ethernet. We will confirm that the packet actually is  | 
9347  |  |    *   Ethernet at runtime before executing these checks. */  | 
9348  | 0  |   PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate));  | 
9349  |  | 
  | 
9350  | 0  |   s1 = new_stmt(cstate, BPF_ST);  | 
9351  | 0  |   s1->s.k = cstate->off_linkhdr.reg;  | 
9352  | 0  |   sappend(s, s1);  | 
9353  |  |  | 
9354  |  |   /* Calculate whether we have an Ethernet header or just raw IP/  | 
9355  |  |    * MPLS/etc. If we have Ethernet, advance the end of the MAC offset  | 
9356  |  |    * and linktype by 14 bytes so that the network header can be found  | 
9357  |  |    * seamlessly. Otherwise, keep what we've calculated already. */  | 
9358  |  |  | 
9359  |  |   /* We have a bare jmp so we can't use the optimizer. */  | 
9360  | 0  |   cstate->no_optimize = 1;  | 
9361  |  |  | 
9362  |  |   /* Load the EtherType in the Geneve header, 2 bytes in. */  | 
9363  | 0  |   s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H);  | 
9364  | 0  |   s1->s.k = 2;  | 
9365  | 0  |   sappend(s, s1);  | 
9366  |  |  | 
9367  |  |   /* Load X with the end of the Geneve header. */  | 
9368  | 0  |   s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);  | 
9369  | 0  |   s1->s.k = cstate->off_linkhdr.reg;  | 
9370  | 0  |   sappend(s, s1);  | 
9371  |  |  | 
9372  |  |   /* Check if the EtherType is Transparent Ethernet Bridging. At the  | 
9373  |  |    * end of this check, we should have the total length in X. In  | 
9374  |  |    * the non-Ethernet case, it's already there. */  | 
9375  | 0  |   s_proto = new_stmt(cstate, JMP(BPF_JEQ));  | 
9376  | 0  |   s_proto->s.k = ETHERTYPE_TEB;  | 
9377  | 0  |   sappend(s, s_proto);  | 
9378  |  | 
  | 
9379  | 0  |   s1 = new_stmt(cstate, BPF_MISC|BPF_TXA);  | 
9380  | 0  |   sappend(s, s1);  | 
9381  | 0  |   s_proto->s.jt = s1;  | 
9382  |  |  | 
9383  |  |   /* Since this is Ethernet, use the EtherType of the payload  | 
9384  |  |    * directly as the linktype. Overwrite what we already have. */  | 
9385  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
9386  | 0  |   s1->s.k = 12;  | 
9387  | 0  |   sappend(s, s1);  | 
9388  |  | 
  | 
9389  | 0  |   s1 = new_stmt(cstate, BPF_ST);  | 
9390  | 0  |   s1->s.k = cstate->off_linktype.reg;  | 
9391  | 0  |   sappend(s, s1);  | 
9392  |  |  | 
9393  |  |   /* Advance two bytes further to get the end of the Ethernet  | 
9394  |  |    * header. */  | 
9395  | 0  |   s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K);  | 
9396  | 0  |   s1->s.k = 2;  | 
9397  | 0  |   sappend(s, s1);  | 
9398  |  |  | 
9399  |  |   /* Move the result to X. */  | 
9400  | 0  |   s1 = new_stmt(cstate, BPF_MISC|BPF_TAX);  | 
9401  | 0  |   sappend(s, s1);  | 
9402  |  |  | 
9403  |  |   /* Store the final result of our linkpl calculation. */  | 
9404  | 0  |   cstate->off_linkpl.reg = alloc_reg(cstate);  | 
9405  | 0  |   cstate->off_linkpl.is_variable = 1;  | 
9406  | 0  |   cstate->off_linkpl.constant_part = 0;  | 
9407  |  | 
  | 
9408  | 0  |   s1 = new_stmt(cstate, BPF_STX);  | 
9409  | 0  |   s1->s.k = cstate->off_linkpl.reg;  | 
9410  | 0  |   sappend(s, s1);  | 
9411  | 0  |   s_proto->s.jf = s1;  | 
9412  |  | 
  | 
9413  | 0  |   cstate->off_nl = 0;  | 
9414  |  | 
  | 
9415  | 0  |   return s;  | 
9416  | 0  | }  | 
9417  |  |  | 
9418  |  | /* Check to see if this is a Geneve packet. */  | 
9419  |  | struct block *  | 
9420  |  | gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni)  | 
9421  | 0  | { | 
9422  | 0  |   struct block *b0, *b1;  | 
9423  | 0  |   struct slist *s;  | 
9424  |  |  | 
9425  |  |   /*  | 
9426  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9427  |  |    * on an error.  | 
9428  |  |    */  | 
9429  | 0  |   if (setjmp(cstate->top_ctx))  | 
9430  | 0  |     return (NULL);  | 
9431  |  |  | 
9432  | 0  |   b0 = gen_geneve4(cstate, vni, has_vni);  | 
9433  | 0  |   b1 = gen_geneve6(cstate, vni, has_vni);  | 
9434  |  | 
  | 
9435  | 0  |   gen_or(b0, b1);  | 
9436  | 0  |   b0 = b1;  | 
9437  |  |  | 
9438  |  |   /* Later filters should act on the payload of the Geneve frame,  | 
9439  |  |    * update all of the header pointers. Attach this code so that  | 
9440  |  |    * it gets executed in the event that the Geneve filter matches. */  | 
9441  | 0  |   s = gen_geneve_offsets(cstate);  | 
9442  |  | 
  | 
9443  | 0  |   b1 = gen_true(cstate);  | 
9444  | 0  |   sappend(s, b1->stmts);  | 
9445  | 0  |   b1->stmts = s;  | 
9446  |  | 
  | 
9447  | 0  |   gen_and(b0, b1);  | 
9448  |  | 
  | 
9449  | 0  |   cstate->is_geneve = 1;  | 
9450  |  | 
  | 
9451  | 0  |   return b1;  | 
9452  | 0  | }  | 
9453  |  |  | 
9454  |  | /* Check that the encapsulated frame has a link layer header  | 
9455  |  |  * for Ethernet filters. */  | 
9456  |  | static struct block *  | 
9457  |  | gen_geneve_ll_check(compiler_state_t *cstate)  | 
9458  | 0  | { | 
9459  | 0  |   struct block *b0;  | 
9460  | 0  |   struct slist *s, *s1;  | 
9461  |  |  | 
9462  |  |   /* The easiest way to see if there is a link layer present  | 
9463  |  |    * is to check if the link layer header and payload are not  | 
9464  |  |    * the same. */  | 
9465  |  |  | 
9466  |  |   /* Geneve always generates pure variable offsets so we can  | 
9467  |  |    * compare only the registers. */  | 
9468  | 0  |   s = new_stmt(cstate, BPF_LD|BPF_MEM);  | 
9469  | 0  |   s->s.k = cstate->off_linkhdr.reg;  | 
9470  |  | 
  | 
9471  | 0  |   s1 = new_stmt(cstate, BPF_LDX|BPF_MEM);  | 
9472  | 0  |   s1->s.k = cstate->off_linkpl.reg;  | 
9473  | 0  |   sappend(s, s1);  | 
9474  |  | 
  | 
9475  | 0  |   b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X);  | 
9476  | 0  |   b0->stmts = s;  | 
9477  | 0  |   b0->s.k = 0;  | 
9478  | 0  |   gen_not(b0);  | 
9479  |  | 
  | 
9480  | 0  |   return b0;  | 
9481  | 0  | }  | 
9482  |  |  | 
9483  |  | static struct block *  | 
9484  |  | gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield,  | 
9485  |  |     bpf_u_int32 jvalue, int jtype, int reverse)  | 
9486  | 66  | { | 
9487  | 66  |   struct block *b0;  | 
9488  |  |  | 
9489  | 66  |   switch (atmfield) { | 
9490  |  |  | 
9491  | 0  |   case A_VPI:  | 
9492  | 0  |     if (!cstate->is_atm)  | 
9493  | 0  |       bpf_error(cstate, "'vpi' supported only on raw ATM");  | 
9494  | 0  |     if (cstate->off_vpi == OFFSET_NOT_SET)  | 
9495  | 0  |       abort();  | 
9496  | 0  |     b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B,  | 
9497  | 0  |         0xffffffffU, jtype, reverse, jvalue);  | 
9498  | 0  |     break;  | 
9499  |  |  | 
9500  | 0  |   case A_VCI:  | 
9501  | 0  |     if (!cstate->is_atm)  | 
9502  | 0  |       bpf_error(cstate, "'vci' supported only on raw ATM");  | 
9503  | 0  |     if (cstate->off_vci == OFFSET_NOT_SET)  | 
9504  | 0  |       abort();  | 
9505  | 0  |     b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H,  | 
9506  | 0  |         0xffffffffU, jtype, reverse, jvalue);  | 
9507  | 0  |     break;  | 
9508  |  |  | 
9509  | 66  |   case A_PROTOTYPE:  | 
9510  | 66  |     if (cstate->off_proto == OFFSET_NOT_SET)  | 
9511  | 0  |       abort(); /* XXX - this isn't on FreeBSD */  | 
9512  | 66  |     b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,  | 
9513  | 66  |         0x0fU, jtype, reverse, jvalue);  | 
9514  | 66  |     break;  | 
9515  |  |  | 
9516  | 0  |   case A_MSGTYPE:  | 
9517  | 0  |     if (cstate->off_payload == OFFSET_NOT_SET)  | 
9518  | 0  |       abort();  | 
9519  | 0  |     b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B,  | 
9520  | 0  |         0xffffffffU, jtype, reverse, jvalue);  | 
9521  | 0  |     break;  | 
9522  |  |  | 
9523  | 0  |   case A_CALLREFTYPE:  | 
9524  | 0  |     if (!cstate->is_atm)  | 
9525  | 0  |       bpf_error(cstate, "'callref' supported only on raw ATM");  | 
9526  | 0  |     if (cstate->off_proto == OFFSET_NOT_SET)  | 
9527  | 0  |       abort();  | 
9528  | 0  |     b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B,  | 
9529  | 0  |         0xffffffffU, jtype, reverse, jvalue);  | 
9530  | 0  |     break;  | 
9531  |  |  | 
9532  | 0  |   default:  | 
9533  | 0  |     abort();  | 
9534  | 66  |   }  | 
9535  | 66  |   return b0;  | 
9536  | 66  | }  | 
9537  |  |  | 
9538  |  | static struct block *  | 
9539  |  | gen_atmtype_metac(compiler_state_t *cstate)  | 
9540  | 0  | { | 
9541  | 0  |   struct block *b0, *b1;  | 
9542  |  | 
  | 
9543  | 0  |   b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9544  | 0  |   b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0);  | 
9545  | 0  |   gen_and(b0, b1);  | 
9546  | 0  |   return b1;  | 
9547  | 0  | }  | 
9548  |  |  | 
9549  |  | static struct block *  | 
9550  |  | gen_atmtype_sc(compiler_state_t *cstate)  | 
9551  | 0  | { | 
9552  | 0  |   struct block *b0, *b1;  | 
9553  |  | 
  | 
9554  | 0  |   b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9555  | 0  |   b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0);  | 
9556  | 0  |   gen_and(b0, b1);  | 
9557  | 0  |   return b1;  | 
9558  | 0  | }  | 
9559  |  |  | 
9560  |  | static struct block *  | 
9561  |  | gen_atmtype_llc(compiler_state_t *cstate)  | 
9562  | 1  | { | 
9563  | 1  |   struct block *b0;  | 
9564  |  |  | 
9565  | 1  |   b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);  | 
9566  | 1  |   cstate->linktype = cstate->prevlinktype;  | 
9567  | 1  |   return b0;  | 
9568  | 1  | }  | 
9569  |  |  | 
9570  |  | struct block *  | 
9571  |  | gen_atmfield_code(compiler_state_t *cstate, int atmfield,  | 
9572  |  |     bpf_u_int32 jvalue, int jtype, int reverse)  | 
9573  | 0  | { | 
9574  |  |   /*  | 
9575  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9576  |  |    * on an error.  | 
9577  |  |    */  | 
9578  | 0  |   if (setjmp(cstate->top_ctx))  | 
9579  | 0  |     return (NULL);  | 
9580  |  |  | 
9581  | 0  |   return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype,  | 
9582  | 0  |       reverse);  | 
9583  | 0  | }  | 
9584  |  |  | 
9585  |  | struct block *  | 
9586  |  | gen_atmtype_abbrev(compiler_state_t *cstate, int type)  | 
9587  | 2  | { | 
9588  | 2  |   struct block *b0, *b1;  | 
9589  |  |  | 
9590  |  |   /*  | 
9591  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9592  |  |    * on an error.  | 
9593  |  |    */  | 
9594  | 2  |   if (setjmp(cstate->top_ctx))  | 
9595  | 2  |     return (NULL);  | 
9596  |  |  | 
9597  | 0  |   switch (type) { | 
9598  |  |  | 
9599  | 0  |   case A_METAC:  | 
9600  |  |     /* Get all packets in Meta signalling Circuit */  | 
9601  | 0  |     if (!cstate->is_atm)  | 
9602  | 0  |       bpf_error(cstate, "'metac' supported only on raw ATM");  | 
9603  | 0  |     b1 = gen_atmtype_metac(cstate);  | 
9604  | 0  |     break;  | 
9605  |  |  | 
9606  | 0  |   case A_BCC:  | 
9607  |  |     /* Get all packets in Broadcast Circuit*/  | 
9608  | 0  |     if (!cstate->is_atm)  | 
9609  | 0  |       bpf_error(cstate, "'bcc' supported only on raw ATM");  | 
9610  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9611  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0);  | 
9612  | 0  |     gen_and(b0, b1);  | 
9613  | 0  |     break;  | 
9614  |  |  | 
9615  | 0  |   case A_OAMF4SC:  | 
9616  |  |     /* Get all cells in Segment OAM F4 circuit*/  | 
9617  | 0  |     if (!cstate->is_atm)  | 
9618  | 0  |       bpf_error(cstate, "'oam4sc' supported only on raw ATM");  | 
9619  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9620  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);  | 
9621  | 0  |     gen_and(b0, b1);  | 
9622  | 0  |     break;  | 
9623  |  |  | 
9624  | 0  |   case A_OAMF4EC:  | 
9625  |  |     /* Get all cells in End-to-End OAM F4 Circuit*/  | 
9626  | 0  |     if (!cstate->is_atm)  | 
9627  | 0  |       bpf_error(cstate, "'oam4ec' supported only on raw ATM");  | 
9628  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9629  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);  | 
9630  | 0  |     gen_and(b0, b1);  | 
9631  | 0  |     break;  | 
9632  |  |  | 
9633  | 2  |   case A_SC:  | 
9634  |  |     /*  Get all packets in connection Signalling Circuit */  | 
9635  | 2  |     if (!cstate->is_atm)  | 
9636  | 2  |       bpf_error(cstate, "'sc' supported only on raw ATM");  | 
9637  | 0  |     b1 = gen_atmtype_sc(cstate);  | 
9638  | 0  |     break;  | 
9639  |  |  | 
9640  | 0  |   case A_ILMIC:  | 
9641  |  |     /* Get all packets in ILMI Circuit */  | 
9642  | 0  |     if (!cstate->is_atm)  | 
9643  | 0  |       bpf_error(cstate, "'ilmic' supported only on raw ATM");  | 
9644  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9645  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0);  | 
9646  | 0  |     gen_and(b0, b1);  | 
9647  | 0  |     break;  | 
9648  |  |  | 
9649  | 0  |   case A_LANE:  | 
9650  |  |     /* Get all LANE packets */  | 
9651  | 0  |     if (!cstate->is_atm)  | 
9652  | 0  |       bpf_error(cstate, "'lane' supported only on raw ATM");  | 
9653  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);  | 
9654  |  |  | 
9655  |  |     /*  | 
9656  |  |      * Arrange that all subsequent tests assume LANE  | 
9657  |  |      * rather than LLC-encapsulated packets, and set  | 
9658  |  |      * the offsets appropriately for LANE-encapsulated  | 
9659  |  |      * Ethernet.  | 
9660  |  |      *  | 
9661  |  |      * We assume LANE means Ethernet, not Token Ring.  | 
9662  |  |      */  | 
9663  | 0  |     PUSH_LINKHDR(cstate, DLT_EN10MB, 0,  | 
9664  | 0  |         cstate->off_payload + 2,  /* Ethernet header */  | 
9665  | 0  |         -1);  | 
9666  | 0  |     cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12;  | 
9667  | 0  |     cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14;  /* Ethernet */  | 
9668  | 0  |     cstate->off_nl = 0;     /* Ethernet II */  | 
9669  | 0  |     cstate->off_nl_nosnap = 3;    /* 802.3+802.2 */  | 
9670  | 0  |     break;  | 
9671  |  |  | 
9672  | 0  |   case A_LLC:  | 
9673  |  |     /* Get all LLC-encapsulated packets */  | 
9674  | 0  |     if (!cstate->is_atm)  | 
9675  | 0  |       bpf_error(cstate, "'llc' supported only on raw ATM");  | 
9676  | 0  |     b1 = gen_atmtype_llc(cstate);  | 
9677  | 0  |     break;  | 
9678  |  |  | 
9679  | 0  |   default:  | 
9680  | 0  |     abort();  | 
9681  | 0  |   }  | 
9682  | 0  |   return b1;  | 
9683  | 0  | }  | 
9684  |  |  | 
9685  |  | /*  | 
9686  |  |  * Filtering for MTP2 messages based on li value  | 
9687  |  |  * FISU, length is null  | 
9688  |  |  * LSSU, length is 1 or 2  | 
9689  |  |  * MSU, length is 3 or more  | 
9690  |  |  * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits  | 
9691  |  |  */  | 
9692  |  | struct block *  | 
9693  |  | gen_mtp2type_abbrev(compiler_state_t *cstate, int type)  | 
9694  | 17  | { | 
9695  | 17  |   struct block *b0, *b1;  | 
9696  |  |  | 
9697  |  |   /*  | 
9698  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9699  |  |    * on an error.  | 
9700  |  |    */  | 
9701  | 17  |   if (setjmp(cstate->top_ctx))  | 
9702  | 6  |     return (NULL);  | 
9703  |  |  | 
9704  | 11  |   switch (type) { | 
9705  |  |  | 
9706  | 0  |   case M_FISU:  | 
9707  | 0  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9708  | 0  |          (cstate->linktype != DLT_ERF) &&  | 
9709  | 0  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9710  | 0  |       bpf_error(cstate, "'fisu' supported only on MTP2");  | 
9711  |  |     /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */  | 
9712  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,  | 
9713  | 0  |         0x3fU, BPF_JEQ, 0, 0U);  | 
9714  | 0  |     break;  | 
9715  |  |  | 
9716  | 9  |   case M_LSSU:  | 
9717  | 9  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9718  | 9  |          (cstate->linktype != DLT_ERF) &&  | 
9719  | 9  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9720  | 3  |       bpf_error(cstate, "'lssu' supported only on MTP2");  | 
9721  | 6  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,  | 
9722  | 6  |         0x3fU, BPF_JGT, 1, 2U);  | 
9723  | 6  |     b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,  | 
9724  | 6  |         0x3fU, BPF_JGT, 0, 0U);  | 
9725  | 6  |     gen_and(b1, b0);  | 
9726  | 6  |     break;  | 
9727  |  |  | 
9728  | 8  |   case M_MSU:  | 
9729  | 8  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9730  | 8  |          (cstate->linktype != DLT_ERF) &&  | 
9731  | 8  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9732  | 3  |       bpf_error(cstate, "'msu' supported only on MTP2");  | 
9733  | 5  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B,  | 
9734  | 5  |         0x3fU, BPF_JGT, 0, 2U);  | 
9735  | 5  |     break;  | 
9736  |  |  | 
9737  | 0  |   case MH_FISU:  | 
9738  | 0  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9739  | 0  |          (cstate->linktype != DLT_ERF) &&  | 
9740  | 0  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9741  | 0  |       bpf_error(cstate, "'hfisu' supported only on MTP2_HSL");  | 
9742  |  |     /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */  | 
9743  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,  | 
9744  | 0  |         0xff80U, BPF_JEQ, 0, 0U);  | 
9745  | 0  |     break;  | 
9746  |  |  | 
9747  | 0  |   case MH_LSSU:  | 
9748  | 0  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9749  | 0  |          (cstate->linktype != DLT_ERF) &&  | 
9750  | 0  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9751  | 0  |       bpf_error(cstate, "'hlssu' supported only on MTP2_HSL");  | 
9752  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,  | 
9753  | 0  |         0xff80U, BPF_JGT, 1, 0x0100U);  | 
9754  | 0  |     b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,  | 
9755  | 0  |         0xff80U, BPF_JGT, 0, 0U);  | 
9756  | 0  |     gen_and(b1, b0);  | 
9757  | 0  |     break;  | 
9758  |  |  | 
9759  | 0  |   case MH_MSU:  | 
9760  | 0  |     if ( (cstate->linktype != DLT_MTP2) &&  | 
9761  | 0  |          (cstate->linktype != DLT_ERF) &&  | 
9762  | 0  |          (cstate->linktype != DLT_MTP2_WITH_PHDR) )  | 
9763  | 0  |       bpf_error(cstate, "'hmsu' supported only on MTP2_HSL");  | 
9764  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H,  | 
9765  | 0  |         0xff80U, BPF_JGT, 0, 0x0100U);  | 
9766  | 0  |     break;  | 
9767  |  |  | 
9768  | 0  |   default:  | 
9769  | 0  |     abort();  | 
9770  | 11  |   }  | 
9771  | 11  |   return b0;  | 
9772  | 11  | }  | 
9773  |  |  | 
9774  |  | /*  | 
9775  |  |  * The jvalue_arg dance is to avoid annoying whining by compilers that  | 
9776  |  |  * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?  | 
9777  |  |  * It's not *used* after setjmp returns.  | 
9778  |  |  */  | 
9779  |  | struct block *  | 
9780  |  | gen_mtp3field_code(compiler_state_t *cstate, int mtp3field,  | 
9781  |  |     bpf_u_int32 jvalue_arg, int jtype, int reverse)  | 
9782  | 0  | { | 
9783  | 0  |   volatile bpf_u_int32 jvalue = jvalue_arg;  | 
9784  | 0  |   struct block *b0;  | 
9785  | 0  |   bpf_u_int32 val1 , val2 , val3;  | 
9786  | 0  |   u_int newoff_sio;  | 
9787  | 0  |   u_int newoff_opc;  | 
9788  | 0  |   u_int newoff_dpc;  | 
9789  | 0  |   u_int newoff_sls;  | 
9790  |  |  | 
9791  |  |   /*  | 
9792  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9793  |  |    * on an error.  | 
9794  |  |    */  | 
9795  | 0  |   if (setjmp(cstate->top_ctx))  | 
9796  | 0  |     return (NULL);  | 
9797  |  |  | 
9798  | 0  |   newoff_sio = cstate->off_sio;  | 
9799  | 0  |   newoff_opc = cstate->off_opc;  | 
9800  | 0  |   newoff_dpc = cstate->off_dpc;  | 
9801  | 0  |   newoff_sls = cstate->off_sls;  | 
9802  | 0  |   switch (mtp3field) { | 
9803  |  |  | 
9804  | 0  |   case MH_SIO:  | 
9805  | 0  |     newoff_sio += 3; /* offset for MTP2_HSL */  | 
9806  |  |     /* FALLTHROUGH */  | 
9807  |  | 
  | 
9808  | 0  |   case M_SIO:  | 
9809  | 0  |     if (cstate->off_sio == OFFSET_NOT_SET)  | 
9810  | 0  |       bpf_error(cstate, "'sio' supported only on SS7");  | 
9811  |  |     /* sio coded on 1 byte so max value 255 */  | 
9812  | 0  |     if(jvalue > 255)  | 
9813  | 0  |             bpf_error(cstate, "sio value %u too big; max value = 255",  | 
9814  | 0  |                 jvalue);  | 
9815  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU,  | 
9816  | 0  |         jtype, reverse, jvalue);  | 
9817  | 0  |     break;  | 
9818  |  |  | 
9819  | 0  |   case MH_OPC:  | 
9820  | 0  |     newoff_opc += 3;  | 
9821  |  |  | 
9822  |  |     /* FALLTHROUGH */  | 
9823  | 0  |         case M_OPC:  | 
9824  | 0  |           if (cstate->off_opc == OFFSET_NOT_SET)  | 
9825  | 0  |       bpf_error(cstate, "'opc' supported only on SS7");  | 
9826  |  |     /* opc coded on 14 bits so max value 16383 */  | 
9827  | 0  |     if (jvalue > 16383)  | 
9828  | 0  |             bpf_error(cstate, "opc value %u too big; max value = 16383",  | 
9829  | 0  |                 jvalue);  | 
9830  |  |     /* the following instructions are made to convert jvalue  | 
9831  |  |      * to the form used to write opc in an ss7 message*/  | 
9832  | 0  |     val1 = jvalue & 0x00003c00;  | 
9833  | 0  |     val1 = val1 >>10;  | 
9834  | 0  |     val2 = jvalue & 0x000003fc;  | 
9835  | 0  |     val2 = val2 <<6;  | 
9836  | 0  |     val3 = jvalue & 0x00000003;  | 
9837  | 0  |     val3 = val3 <<22;  | 
9838  | 0  |     jvalue = val1 + val2 + val3;  | 
9839  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU,  | 
9840  | 0  |         jtype, reverse, jvalue);  | 
9841  | 0  |     break;  | 
9842  |  |  | 
9843  | 0  |   case MH_DPC:  | 
9844  | 0  |     newoff_dpc += 3;  | 
9845  |  |     /* FALLTHROUGH */  | 
9846  |  | 
  | 
9847  | 0  |   case M_DPC:  | 
9848  | 0  |           if (cstate->off_dpc == OFFSET_NOT_SET)  | 
9849  | 0  |       bpf_error(cstate, "'dpc' supported only on SS7");  | 
9850  |  |     /* dpc coded on 14 bits so max value 16383 */  | 
9851  | 0  |     if (jvalue > 16383)  | 
9852  | 0  |             bpf_error(cstate, "dpc value %u too big; max value = 16383",  | 
9853  | 0  |                 jvalue);  | 
9854  |  |     /* the following instructions are made to convert jvalue  | 
9855  |  |      * to the forme used to write dpc in an ss7 message*/  | 
9856  | 0  |     val1 = jvalue & 0x000000ff;  | 
9857  | 0  |     val1 = val1 << 24;  | 
9858  | 0  |     val2 = jvalue & 0x00003f00;  | 
9859  | 0  |     val2 = val2 << 8;  | 
9860  | 0  |     jvalue = val1 + val2;  | 
9861  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U,  | 
9862  | 0  |         jtype, reverse, jvalue);  | 
9863  | 0  |     break;  | 
9864  |  |  | 
9865  | 0  |   case MH_SLS:  | 
9866  | 0  |     newoff_sls += 3;  | 
9867  |  |     /* FALLTHROUGH */  | 
9868  |  | 
  | 
9869  | 0  |   case M_SLS:  | 
9870  | 0  |           if (cstate->off_sls == OFFSET_NOT_SET)  | 
9871  | 0  |       bpf_error(cstate, "'sls' supported only on SS7");  | 
9872  |  |     /* sls coded on 4 bits so max value 15 */  | 
9873  | 0  |     if (jvalue > 15)  | 
9874  | 0  |              bpf_error(cstate, "sls value %u too big; max value = 15",  | 
9875  | 0  |                  jvalue);  | 
9876  |  |     /* the following instruction is made to convert jvalue  | 
9877  |  |      * to the forme used to write sls in an ss7 message*/  | 
9878  | 0  |     jvalue = jvalue << 4;  | 
9879  | 0  |     b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U,  | 
9880  | 0  |         jtype, reverse, jvalue);  | 
9881  | 0  |     break;  | 
9882  |  |  | 
9883  | 0  |   default:  | 
9884  | 0  |     abort();  | 
9885  | 0  |   }  | 
9886  | 0  |   return b0;  | 
9887  | 0  | }  | 
9888  |  |  | 
9889  |  | static struct block *  | 
9890  |  | gen_msg_abbrev(compiler_state_t *cstate, int type)  | 
9891  | 0  | { | 
9892  | 0  |   struct block *b1;  | 
9893  |  |  | 
9894  |  |   /*  | 
9895  |  |    * Q.2931 signalling protocol messages for handling virtual circuits  | 
9896  |  |    * establishment and teardown  | 
9897  |  |    */  | 
9898  | 0  |   switch (type) { | 
9899  |  |  | 
9900  | 0  |   case A_SETUP:  | 
9901  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0);  | 
9902  | 0  |     break;  | 
9903  |  |  | 
9904  | 0  |   case A_CALLPROCEED:  | 
9905  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);  | 
9906  | 0  |     break;  | 
9907  |  |  | 
9908  | 0  |   case A_CONNECT:  | 
9909  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0);  | 
9910  | 0  |     break;  | 
9911  |  |  | 
9912  | 0  |   case A_CONNECTACK:  | 
9913  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);  | 
9914  | 0  |     break;  | 
9915  |  |  | 
9916  | 0  |   case A_RELEASE:  | 
9917  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0);  | 
9918  | 0  |     break;  | 
9919  |  |  | 
9920  | 0  |   case A_RELEASE_DONE:  | 
9921  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);  | 
9922  | 0  |     break;  | 
9923  |  |  | 
9924  | 0  |   default:  | 
9925  | 0  |     abort();  | 
9926  | 0  |   }  | 
9927  | 0  |   return b1;  | 
9928  | 0  | }  | 
9929  |  |  | 
9930  |  | struct block *  | 
9931  |  | gen_atmmulti_abbrev(compiler_state_t *cstate, int type)  | 
9932  | 0  | { | 
9933  | 0  |   struct block *b0, *b1;  | 
9934  |  |  | 
9935  |  |   /*  | 
9936  |  |    * Catch errors reported by us and routines below us, and return NULL  | 
9937  |  |    * on an error.  | 
9938  |  |    */  | 
9939  | 0  |   if (setjmp(cstate->top_ctx))  | 
9940  | 0  |     return (NULL);  | 
9941  |  |  | 
9942  | 0  |   switch (type) { | 
9943  |  |  | 
9944  | 0  |   case A_OAM:  | 
9945  | 0  |     if (!cstate->is_atm)  | 
9946  | 0  |       bpf_error(cstate, "'oam' supported only on raw ATM");  | 
9947  |  |     /* OAM F4 type */  | 
9948  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);  | 
9949  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);  | 
9950  | 0  |     gen_or(b0, b1);  | 
9951  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9952  | 0  |     gen_and(b0, b1);  | 
9953  | 0  |     break;  | 
9954  |  |  | 
9955  | 0  |   case A_OAMF4:  | 
9956  | 0  |     if (!cstate->is_atm)  | 
9957  | 0  |       bpf_error(cstate, "'oamf4' supported only on raw ATM");  | 
9958  |  |     /* OAM F4 type */  | 
9959  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0);  | 
9960  | 0  |     b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0);  | 
9961  | 0  |     gen_or(b0, b1);  | 
9962  | 0  |     b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0);  | 
9963  | 0  |     gen_and(b0, b1);  | 
9964  | 0  |     break;  | 
9965  |  |  | 
9966  | 0  |   case A_CONNECTMSG:  | 
9967  |  |     /*  | 
9968  |  |      * Get Q.2931 signalling messages for switched  | 
9969  |  |      * virtual connection  | 
9970  |  |      */  | 
9971  | 0  |     if (!cstate->is_atm)  | 
9972  | 0  |       bpf_error(cstate, "'connectmsg' supported only on raw ATM");  | 
9973  | 0  |     b0 = gen_msg_abbrev(cstate, A_SETUP);  | 
9974  | 0  |     b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);  | 
9975  | 0  |     gen_or(b0, b1);  | 
9976  | 0  |     b0 = gen_msg_abbrev(cstate, A_CONNECT);  | 
9977  | 0  |     gen_or(b0, b1);  | 
9978  | 0  |     b0 = gen_msg_abbrev(cstate, A_CONNECTACK);  | 
9979  | 0  |     gen_or(b0, b1);  | 
9980  | 0  |     b0 = gen_msg_abbrev(cstate, A_RELEASE);  | 
9981  | 0  |     gen_or(b0, b1);  | 
9982  | 0  |     b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);  | 
9983  | 0  |     gen_or(b0, b1);  | 
9984  | 0  |     b0 = gen_atmtype_sc(cstate);  | 
9985  | 0  |     gen_and(b0, b1);  | 
9986  | 0  |     break;  | 
9987  |  |  | 
9988  | 0  |   case A_METACONNECT:  | 
9989  | 0  |     if (!cstate->is_atm)  | 
9990  | 0  |       bpf_error(cstate, "'metaconnect' supported only on raw ATM");  | 
9991  | 0  |     b0 = gen_msg_abbrev(cstate, A_SETUP);  | 
9992  | 0  |     b1 = gen_msg_abbrev(cstate, A_CALLPROCEED);  | 
9993  | 0  |     gen_or(b0, b1);  | 
9994  | 0  |     b0 = gen_msg_abbrev(cstate, A_CONNECT);  | 
9995  | 0  |     gen_or(b0, b1);  | 
9996  | 0  |     b0 = gen_msg_abbrev(cstate, A_RELEASE);  | 
9997  | 0  |     gen_or(b0, b1);  | 
9998  | 0  |     b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE);  | 
9999  | 0  |     gen_or(b0, b1);  | 
10000  | 0  |     b0 = gen_atmtype_metac(cstate);  | 
10001  | 0  |     gen_and(b0, b1);  | 
10002  | 0  |     break;  | 
10003  |  |  | 
10004  | 0  |   default:  | 
10005  | 0  |     abort();  | 
10006  | 0  |   }  | 
10007  | 0  |   return b1;  | 
10008  | 0  | }  |