scanner.l 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. %top {
  2. /* Must come first for _LARGE_FILE_API on AIX. */
  3. #ifdef HAVE_CONFIG_H
  4. #include <config.h>
  5. #endif
  6. /*
  7. * Must come first to avoid warnings on Windows.
  8. *
  9. * Flex-generated scanners may only include <inttypes.h> if __STDC_VERSION__
  10. * is defined with a value >= 199901, meaning "full C99", and MSVC may not
  11. * define it with that value, because it isn't 100% C99-compliant, even
  12. * though it has an <inttypes.h> capable of defining everything the Flex
  13. * scanner needs.
  14. *
  15. * We, however, will include it if we know we have an MSVC version that has
  16. * it; this means that we may define the INTn_MAX and UINTn_MAX values in
  17. * scanner.c, and then include <stdint.h>, which may define them differently
  18. * (same value, but different string of characters), causing compiler warnings.
  19. *
  20. * If we include it here, and they're defined, that'll prevent scanner.c
  21. * from defining them. So we include <pcap/pcap-inttypes.h>, to get
  22. * <inttypes.h> if we have it.
  23. */
  24. #include <pcap/pcap-inttypes.h>
  25. #include "diag-control.h"
  26. }
  27. /*
  28. * We want a reentrant scanner.
  29. */
  30. %option reentrant
  31. /*
  32. * And we need to pass the compiler state to the scanner.
  33. */
  34. %option extra-type="compiler_state_t *"
  35. /*
  36. * We don't use input, so don't generate code for it.
  37. */
  38. %option noinput
  39. /*
  40. * We don't use unput, so don't generate code for it.
  41. */
  42. %option nounput
  43. /*
  44. * We don't read from the terminal.
  45. */
  46. %option never-interactive
  47. /*
  48. * We want to stop processing when we get to the end of the input.
  49. */
  50. %option noyywrap
  51. /*
  52. * We want to generate code that can be used by a reentrant parser
  53. * generated by Bison or Berkeley YACC.
  54. */
  55. %option bison-bridge
  56. %{
  57. /*
  58. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  59. * The Regents of the University of California. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that: (1) source code distributions
  63. * retain the above copyright notice and this paragraph in its entirety, (2)
  64. * distributions including binary code include the above copyright notice and
  65. * this paragraph in its entirety in the documentation or other materials
  66. * provided with the distribution, and (3) all advertising materials mentioning
  67. * features or use of this software display the following acknowledgement:
  68. * ``This product includes software developed by the University of California,
  69. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  70. * the University nor the names of its contributors may be used to endorse
  71. * or promote products derived from this software without specific prior
  72. * written permission.
  73. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  74. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  75. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  76. */
  77. #include <ctype.h>
  78. #include <string.h>
  79. #include "pcap-int.h"
  80. #include "gencode.h"
  81. #include "grammar.h"
  82. /*
  83. * Earlier versions of Flex don't declare these, so we declare them
  84. * ourselves to squelch warnings.
  85. */
  86. int pcap_get_column(yyscan_t);
  87. void pcap_set_column(int, yyscan_t);
  88. #ifdef INET6
  89. #ifdef _WIN32
  90. #include <winsock2.h>
  91. #include <ws2tcpip.h>
  92. /*
  93. * To quote the MSDN page for getaddrinfo() at
  94. *
  95. * https://msdn.microsoft.com/en-us/library/windows/desktop/ms738520(v=vs.85).aspx
  96. *
  97. * "Support for getaddrinfo on Windows 2000 and older versions
  98. * The getaddrinfo function was added to the Ws2_32.dll on Windows XP and
  99. * later. To execute an application that uses this function on earlier
  100. * versions of Windows, then you need to include the Ws2tcpip.h and
  101. * Wspiapi.h files. When the Wspiapi.h include file is added, the
  102. * getaddrinfo function is defined to the WspiapiGetAddrInfo inline
  103. * function in the Wspiapi.h file. At runtime, the WspiapiGetAddrInfo
  104. * function is implemented in such a way that if the Ws2_32.dll or the
  105. * Wship6.dll (the file containing getaddrinfo in the IPv6 Technology
  106. * Preview for Windows 2000) does not include getaddrinfo, then a
  107. * version of getaddrinfo is implemented inline based on code in the
  108. * Wspiapi.h header file. This inline code will be used on older Windows
  109. * platforms that do not natively support the getaddrinfo function."
  110. *
  111. * We use getaddrinfo(), so we include Wspiapi.h here.
  112. */
  113. #include <wspiapi.h>
  114. #else /* _WIN32 */
  115. #include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
  116. #include <netdb.h> /* for "struct addrinfo" */
  117. #endif /* _WIN32 */
  118. /* Workaround for AIX 4.3 */
  119. #if !defined(AI_NUMERICHOST)
  120. #define AI_NUMERICHOST 0x04
  121. #endif
  122. #endif /*INET6*/
  123. #include <pcap/namedb.h>
  124. #include "grammar.h"
  125. #ifdef HAVE_OS_PROTO_H
  126. #include "os-proto.h"
  127. #endif
  128. static int stoi(char *);
  129. static inline int xdtoi(int);
  130. /*
  131. * Disable diagnostics in the code generated by Flex.
  132. */
  133. DIAG_OFF_FLEX
  134. %}
  135. N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
  136. B ([0-9A-Fa-f][0-9A-Fa-f]?)
  137. B2 ([0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f])
  138. W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
  139. %a 18400
  140. %o 21500
  141. %e 7600
  142. %k 4550
  143. %p 27600
  144. %n 2000
  145. V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
  146. V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
  147. V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
  148. V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
  149. V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
  150. V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
  151. V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
  152. V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
  153. V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
  154. V660 ::{W}:{W}:{W}:{W}:{W}:{W}
  155. V661 {W}::{W}:{W}:{W}:{W}:{W}
  156. V662 {W}:{W}::{W}:{W}:{W}:{W}
  157. V663 {W}:{W}:{W}::{W}:{W}:{W}
  158. V664 {W}:{W}:{W}:{W}::{W}:{W}
  159. V665 {W}:{W}:{W}:{W}:{W}::{W}
  160. V666 {W}:{W}:{W}:{W}:{W}:{W}::
  161. V650 ::{W}:{W}:{W}:{W}:{W}
  162. V651 {W}::{W}:{W}:{W}:{W}
  163. V652 {W}:{W}::{W}:{W}:{W}
  164. V653 {W}:{W}:{W}::{W}:{W}
  165. V654 {W}:{W}:{W}:{W}::{W}
  166. V655 {W}:{W}:{W}:{W}:{W}::
  167. V640 ::{W}:{W}:{W}:{W}
  168. V641 {W}::{W}:{W}:{W}
  169. V642 {W}:{W}::{W}:{W}
  170. V643 {W}:{W}:{W}::{W}
  171. V644 {W}:{W}:{W}:{W}::
  172. V630 ::{W}:{W}:{W}
  173. V631 {W}::{W}:{W}
  174. V632 {W}:{W}::{W}
  175. V633 {W}:{W}:{W}::
  176. V620 ::{W}:{W}
  177. V621 {W}::{W}
  178. V622 {W}:{W}::
  179. V610 ::{W}
  180. V611 {W}::
  181. V600 ::
  182. V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  183. V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  184. V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  185. V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  186. V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
  187. V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
  188. V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
  189. V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  190. V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  191. V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
  192. V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
  193. V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
  194. V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
  195. V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
  196. V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
  197. V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
  198. V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
  199. V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
  200. V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
  201. V6104 ::{W}:{N}\.{N}\.{N}\.{N}
  202. V6114 {W}::{N}\.{N}\.{N}\.{N}
  203. V6004 ::{N}\.{N}\.{N}\.{N}
  204. V6 ({V680}|{V670}|{V671}|{V672}|{V673}|{V674}|{V675}|{V676}|{V677}|{V660}|{V661}|{V662}|{V663}|{V664}|{V665}|{V666}|{V650}|{V651}|{V652}|{V653}|{V654}|{V655}|{V640}|{V641}|{V642}|{V643}|{V644}|{V630}|{V631}|{V632}|{V633}|{V620}|{V621}|{V622}|{V610}|{V611}|{V600}|{V6604}|{V6504}|{V6514}|{V6524}|{V6534}|{V6544}|{V6554}|{V6404}|{V6414}|{V6424}|{V6434}|{V6444}|{V6304}|{V6314}|{V6324}|{V6334}|{V6204}|{V6214}|{V6224}|{V6104}|{V6114}|{V6004})
  205. MAC ({B}:{B}:{B}:{B}:{B}:{B}|{B}\-{B}\-{B}\-{B}\-{B}\-{B}|{B}\.{B}\.{B}\.{B}\.{B}\.{B}|{B2}\.{B2}\.{B2}|{B2}{3})
  206. %%
  207. dst return DST;
  208. src return SRC;
  209. link|ether|ppp|slip return LINK;
  210. fddi|tr|wlan return LINK;
  211. arp return ARP;
  212. rarp return RARP;
  213. ip return IP;
  214. sctp return SCTP;
  215. tcp return TCP;
  216. udp return UDP;
  217. icmp return ICMP;
  218. igmp return IGMP;
  219. igrp return IGRP;
  220. pim return PIM;
  221. vrrp return VRRP;
  222. carp return CARP;
  223. radio return RADIO;
  224. ip6 return IPV6;
  225. icmp6 return ICMPV6;
  226. ah return AH;
  227. esp return ESP;
  228. atalk return ATALK;
  229. aarp return AARP;
  230. decnet return DECNET;
  231. lat return LAT;
  232. sca return SCA;
  233. moprc return MOPRC;
  234. mopdl return MOPDL;
  235. iso return ISO;
  236. esis return ESIS;
  237. es-is return ESIS;
  238. isis return ISIS;
  239. is-is return ISIS;
  240. l1 return L1;
  241. l2 return L2;
  242. iih return IIH;
  243. lsp return LSP;
  244. snp return SNP;
  245. csnp return CSNP;
  246. psnp return PSNP;
  247. clnp return CLNP;
  248. stp return STP;
  249. ipx return IPX;
  250. netbeui return NETBEUI;
  251. host return HOST;
  252. net return NET;
  253. mask return NETMASK;
  254. port return PORT;
  255. portrange return PORTRANGE;
  256. proto return PROTO;
  257. protochain {
  258. #ifdef NO_PROTOCHAIN
  259. bpf_error(yyextra, "%s not supported", yytext);
  260. #else
  261. return PROTOCHAIN;
  262. #endif
  263. }
  264. gateway return GATEWAY;
  265. type return TYPE;
  266. subtype return SUBTYPE;
  267. direction|dir return DIR;
  268. address1|addr1 return ADDR1;
  269. address2|addr2 return ADDR2;
  270. address3|addr3 return ADDR3;
  271. address4|addr4 return ADDR4;
  272. ra return RA;
  273. ta return TA;
  274. less return LESS;
  275. greater return GREATER;
  276. byte return CBYTE;
  277. broadcast return TK_BROADCAST;
  278. multicast return TK_MULTICAST;
  279. and|"&&" return AND;
  280. or|"||" return OR;
  281. not return '!';
  282. len|length return LEN;
  283. inbound return INBOUND;
  284. outbound return OUTBOUND;
  285. vlan return VLAN;
  286. mpls return MPLS;
  287. pppoed return PPPOED;
  288. pppoes return PPPOES;
  289. geneve return GENEVE;
  290. lane return LANE;
  291. llc return LLC;
  292. metac return METAC;
  293. bcc return BCC;
  294. oam return OAM;
  295. oamf4 return OAMF4;
  296. oamf4ec return OAMF4EC;
  297. oamf4sc return OAMF4SC;
  298. sc return SC;
  299. ilmic return ILMIC;
  300. vpi return VPI;
  301. vci return VCI;
  302. connectmsg return CONNECTMSG;
  303. metaconnect return METACONNECT;
  304. on|ifname return PF_IFNAME;
  305. rset|ruleset return PF_RSET;
  306. rnr|rulenum return PF_RNR;
  307. srnr|subrulenum return PF_SRNR;
  308. reason return PF_REASON;
  309. action return PF_ACTION;
  310. fisu return FISU;
  311. lssu return LSSU;
  312. lsu return LSSU;
  313. msu return MSU;
  314. hfisu return HFISU;
  315. hlssu return HLSSU;
  316. hmsu return HMSU;
  317. sio return SIO;
  318. opc return OPC;
  319. dpc return DPC;
  320. sls return SLS;
  321. hsio return HSIO;
  322. hopc return HOPC;
  323. hdpc return HDPC;
  324. hsls return HSLS;
  325. [ \r\n\t] ;
  326. [+\-*/%:\[\]!<>()&|\^=] return yytext[0];
  327. ">=" return GEQ;
  328. "<=" return LEQ;
  329. "!=" return NEQ;
  330. "==" return '=';
  331. "<<" return LSH;
  332. ">>" return RSH;
  333. ${B} { yylval->e = pcap_ether_aton(((char *)yytext)+1);
  334. if (yylval->e == NULL)
  335. bpf_error(yyextra, "malloc");
  336. return AID; }
  337. {MAC} { yylval->e = pcap_ether_aton((char *)yytext);
  338. if (yylval->e == NULL)
  339. bpf_error(yyextra, "malloc");
  340. return EID; }
  341. {N} { yylval->i = stoi((char *)yytext); return NUM; }
  342. ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
  343. yylval->s = sdup(yyextra, (char *)yytext); return HID; }
  344. {V6} {
  345. #ifdef INET6
  346. struct addrinfo hints, *res;
  347. memset(&hints, 0, sizeof(hints));
  348. hints.ai_family = AF_INET6;
  349. hints.ai_flags = AI_NUMERICHOST;
  350. if (getaddrinfo(yytext, NULL, &hints, &res))
  351. bpf_error(yyextra, "bogus IPv6 address %s", yytext);
  352. else {
  353. freeaddrinfo(res);
  354. yylval->s = sdup(yyextra, (char *)yytext); return HID6;
  355. }
  356. #else
  357. bpf_error(yyextra, "IPv6 address %s not supported", yytext);
  358. #endif /*INET6*/
  359. }
  360. {B}:+({B}:+)+ { bpf_error(yyextra, "bogus ethernet address %s", yytext); }
  361. icmptype { yylval->i = 0; return NUM; }
  362. icmpcode { yylval->i = 1; return NUM; }
  363. icmp-echoreply { yylval->i = 0; return NUM; }
  364. icmp-unreach { yylval->i = 3; return NUM; }
  365. icmp-sourcequench { yylval->i = 4; return NUM; }
  366. icmp-redirect { yylval->i = 5; return NUM; }
  367. icmp-echo { yylval->i = 8; return NUM; }
  368. icmp-routeradvert { yylval->i = 9; return NUM; }
  369. icmp-routersolicit { yylval->i = 10; return NUM; }
  370. icmp-timxceed { yylval->i = 11; return NUM; }
  371. icmp-paramprob { yylval->i = 12; return NUM; }
  372. icmp-tstamp { yylval->i = 13; return NUM; }
  373. icmp-tstampreply { yylval->i = 14; return NUM; }
  374. icmp-ireq { yylval->i = 15; return NUM; }
  375. icmp-ireqreply { yylval->i = 16; return NUM; }
  376. icmp-maskreq { yylval->i = 17; return NUM; }
  377. icmp-maskreply { yylval->i = 18; return NUM; }
  378. icmp6type { yylval->i = 0; return NUM; }
  379. icmp6code { yylval->i = 1; return NUM; }
  380. icmp6-echo { yylval->i = 128; return NUM; }
  381. icmp6-echoreply { yylval->i = 129; return NUM; }
  382. icmp6-multicastlistenerquery { yylval->i = 130; return NUM; }
  383. icmp6-multicastlistenerreportv1 { yylval->i = 131; return NUM; }
  384. icmp6-multicastlistenerdone { yylval->i = 132; return NUM; }
  385. icmp6-routersolicit { yylval->i = 133; return NUM; }
  386. icmp6-routeradvert { yylval->i = 134; return NUM; }
  387. icmp6-neighborsolicit { yylval->i = 135; return NUM; }
  388. icmp6-neighboradvert { yylval->i = 136; return NUM; }
  389. icmp6-redirect { yylval->i = 137; return NUM; }
  390. icmp6-routerrenum { yylval->i = 138; return NUM; }
  391. icmp6-nodeinformationquery { yylval->i = 139; return NUM; }
  392. icmp6-nodeinformationresponse { yylval->i = 140; return NUM; }
  393. icmp6-ineighbordiscoverysolicit { yylval->i = 141; return NUM; }
  394. icmp6-ineighbordiscoveryadvert { yylval->i = 142; return NUM; }
  395. icmp6-multicastlistenerreportv2 { yylval->i = 143; return NUM; }
  396. icmp6-homeagentdiscoveryrequest { yylval->i = 144; return NUM; }
  397. icmp6-homeagentdiscoveryreply { yylval->i = 145; return NUM; }
  398. icmp6-mobileprefixsolicit { yylval->i = 146; return NUM; }
  399. icmp6-mobileprefixadvert { yylval->i = 147; return NUM; }
  400. icmp6-certpathsolicit { yylval->i = 148; return NUM; }
  401. icmp6-certpathadvert { yylval->i = 149; return NUM; }
  402. icmp6-multicastrouteradvert { yylval->i = 151; return NUM; }
  403. icmp6-multicastroutersolicit { yylval->i = 152; return NUM; }
  404. icmp6-multicastrouterterm { yylval->i = 153; return NUM; }
  405. tcpflags { yylval->i = 13; return NUM; }
  406. tcp-fin { yylval->i = 0x01; return NUM; }
  407. tcp-syn { yylval->i = 0x02; return NUM; }
  408. tcp-rst { yylval->i = 0x04; return NUM; }
  409. tcp-push { yylval->i = 0x08; return NUM; }
  410. tcp-ack { yylval->i = 0x10; return NUM; }
  411. tcp-urg { yylval->i = 0x20; return NUM; }
  412. tcp-ece { yylval->i = 0x40; return NUM; }
  413. tcp-cwr { yylval->i = 0x80; return NUM; }
  414. [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
  415. yylval->s = sdup(yyextra, (char *)yytext); return ID; }
  416. "\\"[^ !()\n\t]+ { yylval->s = sdup(yyextra, (char *)yytext + 1); return ID; }
  417. [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
  418. bpf_error(yyextra, "illegal token: %s", yytext); }
  419. . { bpf_error(yyextra, "illegal char '%c'", *yytext); }
  420. %%
  421. /*
  422. * Turn diagnostics back on, so we check the code that we've written.
  423. */
  424. DIAG_ON_FLEX
  425. /* Hex digit to integer. */
  426. static inline int
  427. xdtoi(int c)
  428. {
  429. if (isdigit(c))
  430. return c - '0';
  431. else if (islower(c))
  432. return c - 'a' + 10;
  433. else
  434. return c - 'A' + 10;
  435. }
  436. /*
  437. * Convert string to integer. Just like atoi(), but checks for
  438. * preceding 0x or 0 and uses hex or octal instead of decimal.
  439. */
  440. static int
  441. stoi(char *s)
  442. {
  443. int base = 10;
  444. int n = 0;
  445. if (*s == '0') {
  446. if (s[1] == 'x' || s[1] == 'X') {
  447. s += 2;
  448. base = 16;
  449. }
  450. else {
  451. base = 8;
  452. s += 1;
  453. }
  454. }
  455. while (*s)
  456. n = n * base + xdtoi(*s++);
  457. return n;
  458. }