1 /*
2 * We want a reentrant parser.
3 */
4 %pure-parser
5
6 /*
7 * We also want a reentrant scanner, so we have to pass the
8 * handle for the reentrant scanner to the parser, and the
9 * parser has to pass it to the lexical analyzer.
10 *
11 * We use void * rather than yyscan_t because, at least with some
12 * versions of Flex and Bison, if you use yyscan_t in %parse-param and
13 * %lex-param, you have to include scanner.h before grammar.h to get
14 * yyscan_t declared, and you have to include grammar.h before scanner.h
15 * to get YYSTYPE declared. Using void * breaks the cycle; the Flex
16 * documentation says yyscan_t is just a void *.
17 */
18 %parse-param {void *yyscanner}
19 %lex-param {void *yyscanner}
20
21 /*
22 * And we need to pass the compiler state to the scanner.
23 */
24 %parse-param { compiler_state_t *cstate }
25
26 %{
27 /*
28 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
29 * The Regents of the University of California. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that: (1) source code distributions
33 * retain the above copyright notice and this paragraph in its entirety, (2)
34 * distributions including binary code include the above copyright notice and
35 * this paragraph in its entirety in the documentation or other materials
36 * provided with the distribution, and (3) all advertising materials mentioning
37 * features or use of this software display the following acknowledgement:
38 * ``This product includes software developed by the University of California,
39 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
40 * the University nor the names of its contributors may be used to endorse
41 * or promote products derived from this software without specific prior
42 * written permission.
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
44 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 *
47 */
48
49 #ifdef HAVE_CONFIG_H
50 #include <config.h>
51 #endif
52
53 #include <stdlib.h>
54
55 #ifndef _WIN32
56 #include <sys/types.h>
57 #include <sys/socket.h>
58
59 #if __STDC__
60 struct mbuf;
61 struct rtentry;
62 #endif
63
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #endif /* _WIN32 */
67
68 #include <stdio.h>
69
70 #include "diag-control.h"
71
72 #include "pcap-int.h"
73
74 #include "gencode.h"
75 #include "grammar.h"
76 #include "scanner.h"
77
78 #ifdef HAVE_NET_PFVAR_H
79 #include <net/if.h>
80 #include <net/pfvar.h>
81 #include <net/if_pflog.h>
82 #endif
83 #include "llc.h"
84 #include "ieee80211.h"
85 #include <pcap/namedb.h>
86
87 #ifdef HAVE_OS_PROTO_H
88 #include "os-proto.h"
89 #endif
90
91 #ifdef YYBYACC
92 /*
93 * Both Berkeley YACC and Bison define yydebug (under whatever name
94 * it has) as a global, but Bison does so only if YYDEBUG is defined.
95 * Berkeley YACC define it even if YYDEBUG isn't defined; declare it
96 * here to suppress a warning.
97 */
98 #if !defined(YYDEBUG)
99 extern int yydebug;
100 #endif
101
102 /*
103 * In Berkeley YACC, yynerrs (under whatever name it has) is global,
104 * even if it's building a reentrant parser. In Bison, it's local
105 * in reentrant parsers.
106 *
107 * Declare it to squelch a warning.
108 */
109 extern int yynerrs;
110 #endif
111
112 #define QSET(q, p, d, a) (q).proto = (unsigned char)(p),\
113 (q).dir = (unsigned char)(d),\
114 (q).addr = (unsigned char)(a)
115
116 struct tok {
117 int v; /* value */
118 const char *s; /* string */
119 };
120
121 static const struct tok ieee80211_types[] = {
122 { IEEE80211_FC0_TYPE_DATA, "data" },
123 { IEEE80211_FC0_TYPE_MGT, "mgt" },
124 { IEEE80211_FC0_TYPE_MGT, "management" },
125 { IEEE80211_FC0_TYPE_CTL, "ctl" },
126 { IEEE80211_FC0_TYPE_CTL, "control" },
127 { 0, NULL }
128 };
129 static const struct tok ieee80211_mgt_subtypes[] = {
130 { IEEE80211_FC0_SUBTYPE_ASSOC_REQ, "assocreq" },
131 { IEEE80211_FC0_SUBTYPE_ASSOC_REQ, "assoc-req" },
132 { IEEE80211_FC0_SUBTYPE_ASSOC_RESP, "assocresp" },
133 { IEEE80211_FC0_SUBTYPE_ASSOC_RESP, "assoc-resp" },
134 { IEEE80211_FC0_SUBTYPE_REASSOC_REQ, "reassocreq" },
135 { IEEE80211_FC0_SUBTYPE_REASSOC_REQ, "reassoc-req" },
136 { IEEE80211_FC0_SUBTYPE_REASSOC_RESP, "reassocresp" },
137 { IEEE80211_FC0_SUBTYPE_REASSOC_RESP, "reassoc-resp" },
138 { IEEE80211_FC0_SUBTYPE_PROBE_REQ, "probereq" },
139 { IEEE80211_FC0_SUBTYPE_PROBE_REQ, "probe-req" },
140 { IEEE80211_FC0_SUBTYPE_PROBE_RESP, "proberesp" },
141 { IEEE80211_FC0_SUBTYPE_PROBE_RESP, "probe-resp" },
142 { IEEE80211_FC0_SUBTYPE_BEACON, "beacon" },
143 { IEEE80211_FC0_SUBTYPE_ATIM, "atim" },
144 { IEEE80211_FC0_SUBTYPE_DISASSOC, "disassoc" },
145 { IEEE80211_FC0_SUBTYPE_DISASSOC, "disassociation" },
146 { IEEE80211_FC0_SUBTYPE_AUTH, "auth" },
147 { IEEE80211_FC0_SUBTYPE_AUTH, "authentication" },
148 { IEEE80211_FC0_SUBTYPE_DEAUTH, "deauth" },
149 { IEEE80211_FC0_SUBTYPE_DEAUTH, "deauthentication" },
150 { 0, NULL }
151 };
152 static const struct tok ieee80211_ctl_subtypes[] = {
153 { IEEE80211_FC0_SUBTYPE_PS_POLL, "ps-poll" },
154 { IEEE80211_FC0_SUBTYPE_RTS, "rts" },
155 { IEEE80211_FC0_SUBTYPE_CTS, "cts" },
156 { IEEE80211_FC0_SUBTYPE_ACK, "ack" },
157 { IEEE80211_FC0_SUBTYPE_CF_END, "cf-end" },
158 { IEEE80211_FC0_SUBTYPE_CF_END_ACK, "cf-end-ack" },
159 { 0, NULL }
160 };
161 static const struct tok ieee80211_data_subtypes[] = {
162 { IEEE80211_FC0_SUBTYPE_DATA, "data" },
163 { IEEE80211_FC0_SUBTYPE_CF_ACK, "data-cf-ack" },
164 { IEEE80211_FC0_SUBTYPE_CF_POLL, "data-cf-poll" },
165 { IEEE80211_FC0_SUBTYPE_CF_ACPL, "data-cf-ack-poll" },
166 { IEEE80211_FC0_SUBTYPE_NODATA, "null" },
167 { IEEE80211_FC0_SUBTYPE_NODATA_CF_ACK, "cf-ack" },
168 { IEEE80211_FC0_SUBTYPE_NODATA_CF_POLL, "cf-poll" },
169 { IEEE80211_FC0_SUBTYPE_NODATA_CF_ACPL, "cf-ack-poll" },
170 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_DATA, "qos-data" },
171 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_CF_ACK, "qos-data-cf-ack" },
172 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_CF_POLL, "qos-data-cf-poll" },
173 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_CF_ACPL, "qos-data-cf-ack-poll" },
174 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_NODATA, "qos" },
175 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_NODATA_CF_POLL, "qos-cf-poll" },
176 { IEEE80211_FC0_SUBTYPE_QOS|IEEE80211_FC0_SUBTYPE_NODATA_CF_ACPL, "qos-cf-ack-poll" },
177 { 0, NULL }
178 };
179 static const struct tok llc_s_subtypes[] = {
180 { LLC_RR, "rr" },
181 { LLC_RNR, "rnr" },
182 { LLC_REJ, "rej" },
183 { 0, NULL }
184 };
185 static const struct tok llc_u_subtypes[] = {
186 { LLC_UI, "ui" },
187 { LLC_UA, "ua" },
188 { LLC_DISC, "disc" },
189 { LLC_DM, "dm" },
190 { LLC_SABME, "sabme" },
191 { LLC_TEST, "test" },
192 { LLC_XID, "xid" },
193 { LLC_FRMR, "frmr" },
194 { 0, NULL }
195 };
196 struct type2tok {
197 int type;
198 const struct tok *tok;
199 };
200 static const struct type2tok ieee80211_type_subtypes[] = {
201 { IEEE80211_FC0_TYPE_MGT, ieee80211_mgt_subtypes },
202 { IEEE80211_FC0_TYPE_CTL, ieee80211_ctl_subtypes },
203 { IEEE80211_FC0_TYPE_DATA, ieee80211_data_subtypes },
204 { 0, NULL }
205 };
206
207 static int
str2tok(const char * str,const struct tok * toks)208 str2tok(const char *str, const struct tok *toks)
209 {
210 int i;
211
212 for (i = 0; toks[i].s != NULL; i++) {
213 if (pcap_strcasecmp(toks[i].s, str) == 0)
214 return (toks[i].v);
215 }
216 return (-1);
217 }
218
219 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
220
221 static PCAP_NORETURN_DEF void
yyerror(void * yyscanner _U_,compiler_state_t * cstate,const char * msg)222 yyerror(void *yyscanner _U_, compiler_state_t *cstate, const char *msg)
223 {
224 bpf_syntax_error(cstate, msg);
225 /* NOTREACHED */
226 }
227
228 #ifdef HAVE_NET_PFVAR_H
229 static int
pfreason_to_num(compiler_state_t * cstate,const char * reason)230 pfreason_to_num(compiler_state_t *cstate, const char *reason)
231 {
232 const char *reasons[] = PFRES_NAMES;
233 int i;
234
235 for (i = 0; reasons[i]; i++) {
236 if (pcap_strcasecmp(reason, reasons[i]) == 0)
237 return (i);
238 }
239 bpf_error(cstate, "unknown PF reason");
240 /*NOTREACHED*/
241 }
242
243 static int
pfaction_to_num(compiler_state_t * cstate,const char * action)244 pfaction_to_num(compiler_state_t *cstate, const char *action)
245 {
246 if (pcap_strcasecmp(action, "pass") == 0 ||
247 pcap_strcasecmp(action, "accept") == 0)
248 return (PF_PASS);
249 else if (pcap_strcasecmp(action, "drop") == 0 ||
250 pcap_strcasecmp(action, "block") == 0)
251 return (PF_DROP);
252 #if HAVE_PF_NAT_THROUGH_PF_NORDR
253 else if (pcap_strcasecmp(action, "rdr") == 0)
254 return (PF_RDR);
255 else if (pcap_strcasecmp(action, "nat") == 0)
256 return (PF_NAT);
257 else if (pcap_strcasecmp(action, "binat") == 0)
258 return (PF_BINAT);
259 else if (pcap_strcasecmp(action, "nordr") == 0)
260 return (PF_NORDR);
261 #endif
262 else {
263 bpf_error(cstate, "unknown PF action");
264 /*NOTREACHED*/
265 }
266 }
267 #else /* !HAVE_NET_PFVAR_H */
268 static PCAP_NORETURN_DEF int
pfreason_to_num(compiler_state_t * cstate,const char * reason _U_)269 pfreason_to_num(compiler_state_t *cstate, const char *reason _U_)
270 {
271 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
272 /*NOTREACHED*/
273 }
274
275 static PCAP_NORETURN_DEF int
pfaction_to_num(compiler_state_t * cstate,const char * action _U_)276 pfaction_to_num(compiler_state_t *cstate, const char *action _U_)
277 {
278 bpf_error(cstate, "libpcap was compiled on a machine without pf support");
279 /*NOTREACHED*/
280 }
281 #endif /* HAVE_NET_PFVAR_H */
282
283 DIAG_OFF_BISON_BYACC
284 %}
285
286 %union {
287 int i;
288 bpf_u_int32 h;
289 u_char *e;
290 char *s;
291 struct stmt *stmt;
292 struct arth *a;
293 struct {
294 struct qual q;
295 int atmfieldtype;
296 int mtp3fieldtype;
297 struct block *b;
298 } blk;
299 struct block *rblk;
300 }
301
302 %type <blk> expr id nid pid term rterm qid
303 %type <blk> head
304 %type <i> pqual dqual aqual ndaqual
305 %type <a> arth narth
306 %type <i> byteop pname pnum relop irelop
307 %type <blk> and or paren not null prog
308 %type <rblk> other pfvar p80211 pllc
309 %type <i> atmtype atmmultitype
310 %type <blk> atmfield
311 %type <blk> atmfieldvalue atmvalue atmlistvalue
312 %type <i> mtp2type
313 %type <blk> mtp3field
314 %type <blk> mtp3fieldvalue mtp3value mtp3listvalue
315
316
317 %token DST SRC HOST GATEWAY
318 %token NET NETMASK PORT PORTRANGE LESS GREATER PROTO PROTOCHAIN CBYTE
319 %token ARP RARP IP SCTP TCP UDP ICMP IGMP IGRP PIM VRRP CARP
320 %token ATALK AARP DECNET LAT SCA MOPRC MOPDL
321 %token TK_BROADCAST TK_MULTICAST
322 %token NUM INBOUND OUTBOUND
323 %token PF_IFNAME PF_RSET PF_RNR PF_SRNR PF_REASON PF_ACTION
324 %token TYPE SUBTYPE DIR ADDR1 ADDR2 ADDR3 ADDR4 RA TA
325 %token LINK
326 %token GEQ LEQ NEQ
327 %token ID EID HID HID6 AID
328 %token LSH RSH
329 %token LEN
330 %token IPV6 ICMPV6 AH ESP
331 %token VLAN MPLS
332 %token PPPOED PPPOES GENEVE
333 %token ISO ESIS CLNP ISIS L1 L2 IIH LSP SNP CSNP PSNP
334 %token STP
335 %token IPX
336 %token NETBEUI
337 %token LANE LLC METAC BCC SC ILMIC OAMF4EC OAMF4SC
338 %token OAM OAMF4 CONNECTMSG METACONNECT
339 %token VPI VCI
340 %token RADIO
341 %token FISU LSSU MSU HFISU HLSSU HMSU
342 %token SIO OPC DPC SLS HSIO HOPC HDPC HSLS
343
344
345 %type <s> ID
346 %type <e> EID
347 %type <e> AID
348 %type <s> HID HID6
349 %type <i> NUM action reason type subtype type_subtype dir
350
351 %left OR AND
352 %nonassoc '!'
353 %left '|'
354 %left '&'
355 %left LSH RSH
356 %left '+' '-'
357 %left '*' '/'
358 %nonassoc UMINUS
359 %%
360 prog: null expr
361 {
362 finish_parse(cstate, $2.b);
363 }
364 | null
365 ;
366 null: /* null */ { $$.q = qerr; }
367 ;
368 expr: term
369 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
370 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
371 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
372 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
373 ;
374 and: AND { $$ = $<blk>0; }
375 ;
376 or: OR { $$ = $<blk>0; }
377 ;
378 id: nid
379 | pnum { $$.b = gen_ncode(cstate, NULL, (bpf_u_int32)$1,
380 $$.q = $<blk>0.q); }
381 | paren pid ')' { $$ = $2; }
382 ;
383 nid: ID { $$.b = gen_scode(cstate, $1, $$.q = $<blk>0.q); }
384 | HID '/' NUM { $$.b = gen_mcode(cstate, $1, NULL, $3,
385 $$.q = $<blk>0.q); }
386 | HID NETMASK HID { $$.b = gen_mcode(cstate, $1, $3, 0,
387 $$.q = $<blk>0.q); }
388 | HID {
389 /* Decide how to parse HID based on proto */
390 $$.q = $<blk>0.q;
391 if ($$.q.addr == Q_PORT)
392 bpf_error(cstate, "'port' modifier applied to ip host");
393 else if ($$.q.addr == Q_PORTRANGE)
394 bpf_error(cstate, "'portrange' modifier applied to ip host");
395 else if ($$.q.addr == Q_PROTO)
396 bpf_error(cstate, "'proto' modifier applied to ip host");
397 else if ($$.q.addr == Q_PROTOCHAIN)
398 bpf_error(cstate, "'protochain' modifier applied to ip host");
399 $$.b = gen_ncode(cstate, $1, 0, $$.q);
400 }
401 | HID6 '/' NUM {
402 #ifdef INET6
403 $$.b = gen_mcode6(cstate, $1, NULL, $3,
404 $$.q = $<blk>0.q);
405 #else
406 bpf_error(cstate, "'ip6addr/prefixlen' not supported "
407 "in this configuration");
408 #endif /*INET6*/
409 }
410 | HID6 {
411 #ifdef INET6
412 $$.b = gen_mcode6(cstate, $1, 0, 128,
413 $$.q = $<blk>0.q);
414 #else
415 bpf_error(cstate, "'ip6addr' not supported "
416 "in this configuration");
417 #endif /*INET6*/
418 }
419 | EID {
420 $$.b = gen_ecode(cstate, $1, $$.q = $<blk>0.q);
421 /*
422 * $1 was allocated by "pcap_ether_aton()",
423 * so we must free it now that we're done
424 * with it.
425 */
426 free($1);
427 }
428 | AID {
429 $$.b = gen_acode(cstate, $1, $$.q = $<blk>0.q);
430 /*
431 * $1 was allocated by "pcap_ether_aton()",
432 * so we must free it now that we're done
433 * with it.
434 */
435 free($1);
436 }
437 | not id { gen_not($2.b); $$ = $2; }
438 ;
439 not: '!' { $$ = $<blk>0; }
440 ;
441 paren: '(' { $$ = $<blk>0; }
442 ;
443 pid: nid
444 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
445 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
446 ;
447 qid: pnum { $$.b = gen_ncode(cstate, NULL, (bpf_u_int32)$1,
448 $$.q = $<blk>0.q); }
449 | pid
450 ;
451 term: rterm
452 | not term { gen_not($2.b); $$ = $2; }
453 ;
454 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
455 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
456 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
457 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
458 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
459 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
460 ;
461 rterm: head id { $$ = $2; }
462 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
463 | pname { $$.b = gen_proto_abbrev(cstate, $1); $$.q = qerr; }
464 | arth relop arth { $$.b = gen_relation(cstate, $2, $1, $3, 0);
465 $$.q = qerr; }
466 | arth irelop arth { $$.b = gen_relation(cstate, $2, $1, $3, 1);
467 $$.q = qerr; }
468 | other { $$.b = $1; $$.q = qerr; }
469 | atmtype { $$.b = gen_atmtype_abbrev(cstate, $1); $$.q = qerr; }
470 | atmmultitype { $$.b = gen_atmmulti_abbrev(cstate, $1); $$.q = qerr; }
471 | atmfield atmvalue { $$.b = $2.b; $$.q = qerr; }
472 | mtp2type { $$.b = gen_mtp2type_abbrev(cstate, $1); $$.q = qerr; }
473 | mtp3field mtp3value { $$.b = $2.b; $$.q = qerr; }
474 ;
475 /* protocol level qualifiers */
476 pqual: pname
477 | { $$ = Q_DEFAULT; }
478 ;
479 /* 'direction' qualifiers */
480 dqual: SRC { $$ = Q_SRC; }
481 | DST { $$ = Q_DST; }
482 | SRC OR DST { $$ = Q_OR; }
483 | DST OR SRC { $$ = Q_OR; }
484 | SRC AND DST { $$ = Q_AND; }
485 | DST AND SRC { $$ = Q_AND; }
486 | ADDR1 { $$ = Q_ADDR1; }
487 | ADDR2 { $$ = Q_ADDR2; }
488 | ADDR3 { $$ = Q_ADDR3; }
489 | ADDR4 { $$ = Q_ADDR4; }
490 | RA { $$ = Q_RA; }
491 | TA { $$ = Q_TA; }
492 ;
493 /* address type qualifiers */
494 aqual: HOST { $$ = Q_HOST; }
495 | NET { $$ = Q_NET; }
496 | PORT { $$ = Q_PORT; }
497 | PORTRANGE { $$ = Q_PORTRANGE; }
498 ;
499 /* non-directional address type qualifiers */
500 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
501 ;
502 pname: LINK { $$ = Q_LINK; }
503 | IP { $$ = Q_IP; }
504 | ARP { $$ = Q_ARP; }
505 | RARP { $$ = Q_RARP; }
506 | SCTP { $$ = Q_SCTP; }
507 | TCP { $$ = Q_TCP; }
508 | UDP { $$ = Q_UDP; }
509 | ICMP { $$ = Q_ICMP; }
510 | IGMP { $$ = Q_IGMP; }
511 | IGRP { $$ = Q_IGRP; }
512 | PIM { $$ = Q_PIM; }
513 | VRRP { $$ = Q_VRRP; }
514 | CARP { $$ = Q_CARP; }
515 | ATALK { $$ = Q_ATALK; }
516 | AARP { $$ = Q_AARP; }
517 | DECNET { $$ = Q_DECNET; }
518 | LAT { $$ = Q_LAT; }
519 | SCA { $$ = Q_SCA; }
520 | MOPDL { $$ = Q_MOPDL; }
521 | MOPRC { $$ = Q_MOPRC; }
522 | IPV6 { $$ = Q_IPV6; }
523 | ICMPV6 { $$ = Q_ICMPV6; }
524 | AH { $$ = Q_AH; }
525 | ESP { $$ = Q_ESP; }
526 | ISO { $$ = Q_ISO; }
527 | ESIS { $$ = Q_ESIS; }
528 | ISIS { $$ = Q_ISIS; }
529 | L1 { $$ = Q_ISIS_L1; }
530 | L2 { $$ = Q_ISIS_L2; }
531 | IIH { $$ = Q_ISIS_IIH; }
532 | LSP { $$ = Q_ISIS_LSP; }
533 | SNP { $$ = Q_ISIS_SNP; }
534 | PSNP { $$ = Q_ISIS_PSNP; }
535 | CSNP { $$ = Q_ISIS_CSNP; }
536 | CLNP { $$ = Q_CLNP; }
537 | STP { $$ = Q_STP; }
538 | IPX { $$ = Q_IPX; }
539 | NETBEUI { $$ = Q_NETBEUI; }
540 | RADIO { $$ = Q_RADIO; }
541 ;
542 other: pqual TK_BROADCAST { $$ = gen_broadcast(cstate, $1); }
543 | pqual TK_MULTICAST { $$ = gen_multicast(cstate, $1); }
544 | LESS NUM { $$ = gen_less(cstate, $2); }
545 | GREATER NUM { $$ = gen_greater(cstate, $2); }
546 | CBYTE NUM byteop NUM { $$ = gen_byteop(cstate, $3, $2, $4); }
547 | INBOUND { $$ = gen_inbound(cstate, 0); }
548 | OUTBOUND { $$ = gen_inbound(cstate, 1); }
549 | VLAN pnum { $$ = gen_vlan(cstate, $2); }
550 | VLAN { $$ = gen_vlan(cstate, -1); }
551 | MPLS pnum { $$ = gen_mpls(cstate, $2); }
552 | MPLS { $$ = gen_mpls(cstate, -1); }
553 | PPPOED { $$ = gen_pppoed(cstate); }
554 | PPPOES pnum { $$ = gen_pppoes(cstate, $2); }
555 | PPPOES { $$ = gen_pppoes(cstate, -1); }
556 | GENEVE pnum { $$ = gen_geneve(cstate, $2); }
557 | GENEVE { $$ = gen_geneve(cstate, -1); }
558 | pfvar { $$ = $1; }
559 | pqual p80211 { $$ = $2; }
560 | pllc { $$ = $1; }
561 ;
562
563 pfvar: PF_IFNAME ID { $$ = gen_pf_ifname(cstate, $2); }
564 | PF_RSET ID { $$ = gen_pf_ruleset(cstate, $2); }
565 | PF_RNR NUM { $$ = gen_pf_rnr(cstate, $2); }
566 | PF_SRNR NUM { $$ = gen_pf_srnr(cstate, $2); }
567 | PF_REASON reason { $$ = gen_pf_reason(cstate, $2); }
568 | PF_ACTION action { $$ = gen_pf_action(cstate, $2); }
569 ;
570
571 p80211: TYPE type SUBTYPE subtype
572 { $$ = gen_p80211_type(cstate, $2 | $4,
573 IEEE80211_FC0_TYPE_MASK |
574 IEEE80211_FC0_SUBTYPE_MASK);
575 }
576 | TYPE type { $$ = gen_p80211_type(cstate, $2,
577 IEEE80211_FC0_TYPE_MASK);
578 }
579 | SUBTYPE type_subtype { $$ = gen_p80211_type(cstate, $2,
580 IEEE80211_FC0_TYPE_MASK |
581 IEEE80211_FC0_SUBTYPE_MASK);
582 }
583 | DIR dir { $$ = gen_p80211_fcdir(cstate, $2); }
584 ;
585
586 type: NUM
587 | ID { $$ = str2tok($1, ieee80211_types);
588 if ($$ == -1)
589 bpf_error(cstate, "unknown 802.11 type name");
590 }
591 ;
592
593 subtype: NUM
594 | ID { const struct tok *types = NULL;
595 int i;
596 for (i = 0;; i++) {
597 if (ieee80211_type_subtypes[i].tok == NULL) {
598 /* Ran out of types */
599 bpf_error(cstate, "unknown 802.11 type");
600 break;
601 }
602 if ($<i>-1 == ieee80211_type_subtypes[i].type) {
603 types = ieee80211_type_subtypes[i].tok;
604 break;
605 }
606 }
607
608 $$ = str2tok($1, types);
609 if ($$ == -1)
610 bpf_error(cstate, "unknown 802.11 subtype name");
611 }
612 ;
613
614 type_subtype: ID { int i;
615 for (i = 0;; i++) {
616 if (ieee80211_type_subtypes[i].tok == NULL) {
617 /* Ran out of types */
618 bpf_error(cstate, "unknown 802.11 type name");
619 break;
620 }
621 $$ = str2tok($1, ieee80211_type_subtypes[i].tok);
622 if ($$ != -1) {
623 $$ |= ieee80211_type_subtypes[i].type;
624 break;
625 }
626 }
627 }
628 ;
629
630 pllc: LLC { $$ = gen_llc(cstate); }
631 | LLC ID { if (pcap_strcasecmp($2, "i") == 0)
632 $$ = gen_llc_i(cstate);
633 else if (pcap_strcasecmp($2, "s") == 0)
634 $$ = gen_llc_s(cstate);
635 else if (pcap_strcasecmp($2, "u") == 0)
636 $$ = gen_llc_u(cstate);
637 else {
638 int subtype;
639
640 subtype = str2tok($2, llc_s_subtypes);
641 if (subtype != -1)
642 $$ = gen_llc_s_subtype(cstate, subtype);
643 else {
644 subtype = str2tok($2, llc_u_subtypes);
645 if (subtype == -1)
646 bpf_error(cstate, "unknown LLC type name \"%s\"", $2);
647 $$ = gen_llc_u_subtype(cstate, subtype);
648 }
649 }
650 }
651 /* sigh, "rnr" is already a keyword for PF */
652 | LLC PF_RNR { $$ = gen_llc_s_subtype(cstate, LLC_RNR); }
653 ;
654
655 dir: NUM
656 | ID { if (pcap_strcasecmp($1, "nods") == 0)
657 $$ = IEEE80211_FC1_DIR_NODS;
658 else if (pcap_strcasecmp($1, "tods") == 0)
659 $$ = IEEE80211_FC1_DIR_TODS;
660 else if (pcap_strcasecmp($1, "fromds") == 0)
661 $$ = IEEE80211_FC1_DIR_FROMDS;
662 else if (pcap_strcasecmp($1, "dstods") == 0)
663 $$ = IEEE80211_FC1_DIR_DSTODS;
664 else
665 bpf_error(cstate, "unknown 802.11 direction");
666 }
667 ;
668
669 reason: NUM { $$ = $1; }
670 | ID { $$ = pfreason_to_num(cstate, $1); }
671 ;
672
673 action: ID { $$ = pfaction_to_num(cstate, $1); }
674 ;
675
676 relop: '>' { $$ = BPF_JGT; }
677 | GEQ { $$ = BPF_JGE; }
678 | '=' { $$ = BPF_JEQ; }
679 ;
680 irelop: LEQ { $$ = BPF_JGT; }
681 | '<' { $$ = BPF_JGE; }
682 | NEQ { $$ = BPF_JEQ; }
683 ;
684 arth: pnum { $$ = gen_loadi(cstate, $1); }
685 | narth
686 ;
687 narth: pname '[' arth ']' { $$ = gen_load(cstate, $1, $3, 1); }
688 | pname '[' arth ':' NUM ']' { $$ = gen_load(cstate, $1, $3, $5); }
689 | arth '+' arth { $$ = gen_arth(cstate, BPF_ADD, $1, $3); }
690 | arth '-' arth { $$ = gen_arth(cstate, BPF_SUB, $1, $3); }
691 | arth '*' arth { $$ = gen_arth(cstate, BPF_MUL, $1, $3); }
692 | arth '/' arth { $$ = gen_arth(cstate, BPF_DIV, $1, $3); }
693 | arth '%' arth { $$ = gen_arth(cstate, BPF_MOD, $1, $3); }
694 | arth '&' arth { $$ = gen_arth(cstate, BPF_AND, $1, $3); }
695 | arth '|' arth { $$ = gen_arth(cstate, BPF_OR, $1, $3); }
696 | arth '^' arth { $$ = gen_arth(cstate, BPF_XOR, $1, $3); }
697 | arth LSH arth { $$ = gen_arth(cstate, BPF_LSH, $1, $3); }
698 | arth RSH arth { $$ = gen_arth(cstate, BPF_RSH, $1, $3); }
699 | '-' arth %prec UMINUS { $$ = gen_neg(cstate, $2); }
700 | paren narth ')' { $$ = $2; }
701 | LEN { $$ = gen_loadlen(cstate); }
702 ;
703 byteop: '&' { $$ = '&'; }
704 | '|' { $$ = '|'; }
705 | '<' { $$ = '<'; }
706 | '>' { $$ = '>'; }
707 | '=' { $$ = '='; }
708 ;
709 pnum: NUM
710 | paren pnum ')' { $$ = $2; }
711 ;
712 atmtype: LANE { $$ = A_LANE; }
713 | METAC { $$ = A_METAC; }
714 | BCC { $$ = A_BCC; }
715 | OAMF4EC { $$ = A_OAMF4EC; }
716 | OAMF4SC { $$ = A_OAMF4SC; }
717 | SC { $$ = A_SC; }
718 | ILMIC { $$ = A_ILMIC; }
719 ;
720 atmmultitype: OAM { $$ = A_OAM; }
721 | OAMF4 { $$ = A_OAMF4; }
722 | CONNECTMSG { $$ = A_CONNECTMSG; }
723 | METACONNECT { $$ = A_METACONNECT; }
724 ;
725 /* ATM field types quantifier */
726 atmfield: VPI { $$.atmfieldtype = A_VPI; }
727 | VCI { $$.atmfieldtype = A_VCI; }
728 ;
729 atmvalue: atmfieldvalue
730 | relop NUM { $$.b = gen_atmfield_code(cstate, $<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 0); }
731 | irelop NUM { $$.b = gen_atmfield_code(cstate, $<blk>0.atmfieldtype, (bpf_int32)$2, (bpf_u_int32)$1, 1); }
732 | paren atmlistvalue ')' { $$.b = $2.b; $$.q = qerr; }
733 ;
734 atmfieldvalue: NUM {
735 $$.atmfieldtype = $<blk>0.atmfieldtype;
736 if ($$.atmfieldtype == A_VPI ||
737 $$.atmfieldtype == A_VCI)
738 $$.b = gen_atmfield_code(cstate, $$.atmfieldtype, (bpf_int32) $1, BPF_JEQ, 0);
739 }
740 ;
741 atmlistvalue: atmfieldvalue
742 | atmlistvalue or atmfieldvalue { gen_or($1.b, $3.b); $$ = $3; }
743 ;
744 /* MTP2 types quantifier */
745 mtp2type: FISU { $$ = M_FISU; }
746 | LSSU { $$ = M_LSSU; }
747 | MSU { $$ = M_MSU; }
748 | HFISU { $$ = MH_FISU; }
749 | HLSSU { $$ = MH_LSSU; }
750 | HMSU { $$ = MH_MSU; }
751 ;
752 /* MTP3 field types quantifier */
753 mtp3field: SIO { $$.mtp3fieldtype = M_SIO; }
754 | OPC { $$.mtp3fieldtype = M_OPC; }
755 | DPC { $$.mtp3fieldtype = M_DPC; }
756 | SLS { $$.mtp3fieldtype = M_SLS; }
757 | HSIO { $$.mtp3fieldtype = MH_SIO; }
758 | HOPC { $$.mtp3fieldtype = MH_OPC; }
759 | HDPC { $$.mtp3fieldtype = MH_DPC; }
760 | HSLS { $$.mtp3fieldtype = MH_SLS; }
761 ;
762 mtp3value: mtp3fieldvalue
763 | relop NUM { $$.b = gen_mtp3field_code(cstate, $<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 0); }
764 | irelop NUM { $$.b = gen_mtp3field_code(cstate, $<blk>0.mtp3fieldtype, (u_int)$2, (u_int)$1, 1); }
765 | paren mtp3listvalue ')' { $$.b = $2.b; $$.q = qerr; }
766 ;
767 mtp3fieldvalue: NUM {
768 $$.mtp3fieldtype = $<blk>0.mtp3fieldtype;
769 if ($$.mtp3fieldtype == M_SIO ||
770 $$.mtp3fieldtype == M_OPC ||
771 $$.mtp3fieldtype == M_DPC ||
772 $$.mtp3fieldtype == M_SLS ||
773 $$.mtp3fieldtype == MH_SIO ||
774 $$.mtp3fieldtype == MH_OPC ||
775 $$.mtp3fieldtype == MH_DPC ||
776 $$.mtp3fieldtype == MH_SLS)
777 $$.b = gen_mtp3field_code(cstate, $$.mtp3fieldtype, (u_int) $1, BPF_JEQ, 0);
778 }
779 ;
780 mtp3listvalue: mtp3fieldvalue
781 | mtp3listvalue or mtp3fieldvalue { gen_or($1.b, $3.b); $$ = $3; }
782 ;
783 %%
784