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