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