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