grammar.y 23 KB

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