1 %{
2 /*
3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 */
22
23 #ifndef lint
24 static const char rcsid[] _U_ =
25 "@(#) $Header: /tcpdump/master/libpcap/scanner.l,v 1.99.2.9 2007/06/11 09:52:05 guy Exp $ (LBL)";
26 #endif
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <ctype.h>
33 #include <string.h>
34
35 #include "pcap-int.h"
36
37 #include "gencode.h"
38 #ifdef INET6
39 #ifdef WIN32
40 #include <pcap-stdinc.h>
41
42 #ifdef __MINGW32__
43 #include "IP6_misc.h"
44 #endif
45 #else /* WIN32 */
46 #include <sys/socket.h> /* for "struct sockaddr" in "struct addrinfo" */
47 #include <netdb.h> /* for "struct addrinfo" */
48 #endif /* WIN32 */
49
50 /* Workaround for AIX 4.3 */
51 #if !defined(AI_NUMERICHOST)
52 #define AI_NUMERICHOST 0x04
53 #endif
54 #endif /*INET6*/
55 #include <pcap-namedb.h>
56 #include "tokdefs.h"
57
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61
62 static int stoi(char *);
63 static inline int xdtoi(int);
64
65 #ifdef FLEX_SCANNER
66 #define YY_NO_UNPUT
67 static YY_BUFFER_STATE in_buffer;
68 #else
69 static char *in_buffer;
70
71 #undef getc
72 #define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++)
73 #endif
74
75 #define yylval pcap_lval
76 extern YYSTYPE yylval;
77
78 %}
79
80 N ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
81 B ([0-9A-Fa-f][0-9A-Fa-f]?)
82 W ([0-9A-Fa-f][0-9A-Fa-f]?[0-9A-Fa-f]?[0-9A-Fa-f]?)
83
84 %a 18400
85 %o 21500
86 %e 7600
87 %k 4550
88 %p 27600
89 %n 2000
90
91 V680 {W}:{W}:{W}:{W}:{W}:{W}:{W}:{W}
92
93 V670 ::{W}:{W}:{W}:{W}:{W}:{W}:{W}
94 V671 {W}::{W}:{W}:{W}:{W}:{W}:{W}
95 V672 {W}:{W}::{W}:{W}:{W}:{W}:{W}
96 V673 {W}:{W}:{W}::{W}:{W}:{W}:{W}
97 V674 {W}:{W}:{W}:{W}::{W}:{W}:{W}
98 V675 {W}:{W}:{W}:{W}:{W}::{W}:{W}
99 V676 {W}:{W}:{W}:{W}:{W}:{W}::{W}
100 V677 {W}:{W}:{W}:{W}:{W}:{W}:{W}::
101
102 V660 ::{W}:{W}:{W}:{W}:{W}:{W}
103 V661 {W}::{W}:{W}:{W}:{W}:{W}
104 V662 {W}:{W}::{W}:{W}:{W}:{W}
105 V663 {W}:{W}:{W}::{W}:{W}:{W}
106 V664 {W}:{W}:{W}:{W}::{W}:{W}
107 V665 {W}:{W}:{W}:{W}:{W}::{W}
108 V666 {W}:{W}:{W}:{W}:{W}:{W}::
109
110 V650 ::{W}:{W}:{W}:{W}:{W}
111 V651 {W}::{W}:{W}:{W}:{W}
112 V652 {W}:{W}::{W}:{W}:{W}
113 V653 {W}:{W}:{W}::{W}:{W}
114 V654 {W}:{W}:{W}:{W}::{W}
115 V655 {W}:{W}:{W}:{W}:{W}::
116
117 V640 ::{W}:{W}:{W}:{W}
118 V641 {W}::{W}:{W}:{W}
119 V642 {W}:{W}::{W}:{W}
120 V643 {W}:{W}:{W}::{W}
121 V644 {W}:{W}:{W}:{W}::
122
123 V630 ::{W}:{W}:{W}
124 V631 {W}::{W}:{W}
125 V632 {W}:{W}::{W}
126 V633 {W}:{W}:{W}::
127
128 V620 ::{W}:{W}
129 V621 {W}::{W}
130 V622 {W}:{W}::
131
132 V610 ::{W}
133 V611 {W}::
134
135 V600 ::
136
137 V6604 {W}:{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
138
139 V6504 ::{W}:{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
140 V6514 {W}::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
141 V6524 {W}:{W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
142 V6534 {W}:{W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
143 V6544 {W}:{W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
144 V6554 {W}:{W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
145
146 V6404 ::{W}:{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
147 V6414 {W}::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
148 V6424 {W}:{W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
149 V6434 {W}:{W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
150 V6444 {W}:{W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
151
152 V6304 ::{W}:{W}:{W}:{N}\.{N}\.{N}\.{N}
153 V6314 {W}::{W}:{W}:{N}\.{N}\.{N}\.{N}
154 V6324 {W}:{W}::{W}:{N}\.{N}\.{N}\.{N}
155 V6334 {W}:{W}:{W}::{N}\.{N}\.{N}\.{N}
156
157 V6204 ::{W}:{W}:{N}\.{N}\.{N}\.{N}
158 V6214 {W}::{W}:{N}\.{N}\.{N}\.{N}
159 V6224 {W}:{W}::{N}\.{N}\.{N}\.{N}
160
161 V6104 ::{W}:{N}\.{N}\.{N}\.{N}
162 V6114 {W}::{N}\.{N}\.{N}\.{N}
163
164 V6004 ::{N}\.{N}\.{N}\.{N}
165
166
167 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})
168
169 %%
170 dst return DST;
171 src return SRC;
172
173 link|ether|ppp|slip return LINK;
174 fddi|tr|wlan return LINK;
175 arp return ARP;
176 rarp return RARP;
177 ip return IP;
178 sctp return SCTP;
179 tcp return TCP;
180 udp return UDP;
181 icmp return ICMP;
182 igmp return IGMP;
183 igrp return IGRP;
184 pim return PIM;
185 vrrp return VRRP;
186 radio return RADIO;
187
188 ip6 {
189 #ifdef INET6
190 return IPV6;
191 #else
192 bpf_error("%s not supported", yytext);
193 #endif
194 }
195 icmp6 {
196 #ifdef INET6
197 return ICMPV6;
198 #else
199 bpf_error("%s not supported", yytext);
200 #endif
201 }
202 ah return AH;
203 esp return ESP;
204
205 atalk return ATALK;
206 aarp return AARP;
207 decnet return DECNET;
208 lat return LAT;
209 sca return SCA;
210 moprc return MOPRC;
211 mopdl return MOPDL;
212
213 iso return ISO;
214 esis return ESIS;
215 es-is return ESIS;
216 isis return ISIS;
217 is-is return ISIS;
218 l1 return L1;
219 l2 return L2;
220 iih return IIH;
221 lsp return LSP;
222 snp return SNP;
223 csnp return CSNP;
224 psnp return PSNP;
225
226 clnp return CLNP;
227
228 stp return STP;
229
230 ipx return IPX;
231
232 netbeui return NETBEUI;
233
234 host return HOST;
235 net return NET;
236 mask return NETMASK;
237 port return PORT;
238 portrange return PORTRANGE;
239 proto return PROTO;
240 protochain {
241 #ifdef NO_PROTOCHAIN
242 bpf_error("%s not supported", yytext);
243 #else
244 return PROTOCHAIN;
245 #endif
246 }
247
248 gateway return GATEWAY;
249
250 less return LESS;
251 greater return GREATER;
252 byte return CBYTE;
253 broadcast return TK_BROADCAST;
254 multicast return TK_MULTICAST;
255
256 and|"&&" return AND;
257 or|"||" return OR;
258 not return '!';
259
260 len|length return LEN;
261 inbound return INBOUND;
262 outbound return OUTBOUND;
263
264 vlan return VLAN;
265 mpls return MPLS;
266 pppoed return PPPOED;
267 pppoes return PPPOES;
268
269 lane return LANE;
270 llc return LLC;
271 metac return METAC;
272 bcc return BCC;
273 oam return OAM;
274 oamf4 return OAMF4;
275 oamf4ec return OAMF4EC;
276 oamf4sc return OAMF4SC;
277 sc return SC;
278 ilmic return ILMIC;
279 vpi return VPI;
280 vci return VCI;
281 connectmsg return CONNECTMSG;
282 metaconnect return METACONNECT;
283
284 on|ifname return PF_IFNAME;
285 rset|ruleset return PF_RSET;
286 rnr|rulenum return PF_RNR;
287 srnr|subrulenum return PF_SRNR;
288 reason return PF_REASON;
289 action return PF_ACTION;
290
291 fisu return FISU;
292 lssu return LSSU;
293 lsu return LSSU;
294 msu return MSU;
295 sio return SIO;
296 opc return OPC;
297 dpc return DPC;
298 sls return SLS;
299
300 [ \r\n\t] ;
301 [+\-*/:\[\]!<>()&|=] return yytext[0];
302 ">=" return GEQ;
303 "<=" return LEQ;
304 "!=" return NEQ;
305 "==" return '=';
306 "<<" return LSH;
307 ">>" return RSH;
308 ${B} { yylval.e = pcap_ether_aton(((char *)yytext)+1);
309 return AID; }
310 {N} { yylval.i = stoi((char *)yytext); return NUM; }
311 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) {
312 yylval.s = sdup((char *)yytext); return HID; }
313 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
314 return EID; }
315 {V6} {
316 #ifdef INET6
317 struct addrinfo hints, *res;
318 memset(&hints, 0, sizeof(hints));
319 hints.ai_family = AF_INET6;
320 hints.ai_flags = AI_NUMERICHOST;
321 if (getaddrinfo(yytext, NULL, &hints, &res))
322 bpf_error("bogus IPv6 address %s", yytext);
323 else {
324 yylval.s = sdup((char *)yytext); return HID6;
325 }
326 #else
327 bpf_error("IPv6 address %s not supported", yytext);
328 #endif /*INET6*/
329 }
330 {B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); }
331 icmptype { yylval.i = 0; return NUM; }
332 icmpcode { yylval.i = 1; return NUM; }
333 icmp-echoreply { yylval.i = 0; return NUM; }
334 icmp-unreach { yylval.i = 3; return NUM; }
335 icmp-sourcequench { yylval.i = 4; return NUM; }
336 icmp-redirect { yylval.i = 5; return NUM; }
337 icmp-echo { yylval.i = 8; return NUM; }
338 icmp-routeradvert { yylval.i = 9; return NUM; }
339 icmp-routersolicit { yylval.i = 10; return NUM; }
340 icmp-timxceed { yylval.i = 11; return NUM; }
341 icmp-paramprob { yylval.i = 12; return NUM; }
342 icmp-tstamp { yylval.i = 13; return NUM; }
343 icmp-tstampreply { yylval.i = 14; return NUM; }
344 icmp-ireq { yylval.i = 15; return NUM; }
345 icmp-ireqreply { yylval.i = 16; return NUM; }
346 icmp-maskreq { yylval.i = 17; return NUM; }
347 icmp-maskreply { yylval.i = 18; return NUM; }
348 tcpflags { yylval.i = 13; return NUM; }
349 tcp-fin { yylval.i = 0x01; return NUM; }
350 tcp-syn { yylval.i = 0x02; return NUM; }
351 tcp-rst { yylval.i = 0x04; return NUM; }
352 tcp-push { yylval.i = 0x08; return NUM; }
353 tcp-ack { yylval.i = 0x10; return NUM; }
354 tcp-urg { yylval.i = 0x20; return NUM; }
355 [A-Za-z0-9]([-_.A-Za-z0-9]*[.A-Za-z0-9])? {
356 yylval.s = sdup((char *)yytext); return ID; }
357 "\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; }
358 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ {
359 bpf_error("illegal token: %s", yytext); }
360 . { bpf_error("illegal char '%c'", *yytext); }
361 %%
362 void
lex_init(buf)363 lex_init(buf)
364 const char *buf;
365 {
366 #ifdef FLEX_SCANNER
367 in_buffer = yy_scan_string(buf);
368 #else
369 in_buffer = buf;
370 #endif
371 }
372
373 /*
374 * Do any cleanup necessary after parsing.
375 */
376 void
lex_cleanup()377 lex_cleanup()
378 {
379 #ifdef FLEX_SCANNER
380 if (in_buffer != NULL)
381 yy_delete_buffer(in_buffer);
382 in_buffer = NULL;
383 #endif
384 }
385
386 /*
387 * Also define a yywrap. Note that if we're using flex, it will
388 * define a macro to map this identifier to pcap_wrap.
389 */
390 int
yywrap()391 yywrap()
392 {
393 return 1;
394 }
395
396 /* Hex digit to integer. */
397 static inline int
xdtoi(c)398 xdtoi(c)
399 register int c;
400 {
401 if (isdigit(c))
402 return c - '0';
403 else if (islower(c))
404 return c - 'a' + 10;
405 else
406 return c - 'A' + 10;
407 }
408
409 /*
410 * Convert string to integer. Just like atoi(), but checks for
411 * preceding 0x or 0 and uses hex or octal instead of decimal.
412 */
413 static int
stoi(s)414 stoi(s)
415 char *s;
416 {
417 int base = 10;
418 int n = 0;
419
420 if (*s == '0') {
421 if (s[1] == 'x' || s[1] == 'X') {
422 s += 2;
423 base = 16;
424 }
425 else {
426 base = 8;
427 s += 1;
428 }
429 }
430 while (*s)
431 n = n * base + xdtoi(*s++);
432
433 return n;
434 }
435