grammar.y revision 17684
1145524Sdarrenr%{ 2145524Sdarrenr/* 3145524Sdarrenr * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 4162199Sobrien * The Regents of the University of California. All rights reserved. 5255332Scy * 6145524Sdarrenr * Redistribution and use in source and binary forms, with or without 7145524Sdarrenr * modification, are permitted provided that: (1) source code distributions 8145524Sdarrenr * retain the above copyright notice and this paragraph in its entirety, (2) 9255332Scy * distributions including binary code include the above copyright notice and 10145524Sdarrenr * this paragraph in its entirety in the documentation or other materials 11145524Sdarrenr * provided with the distribution, and (3) all advertising materials mentioning 12204585Suqs * features or use of this software display the following acknowledgement: 13145524Sdarrenr * ``This product includes software developed by the University of California, 14255332Scy * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 15145524Sdarrenr * the University nor the names of its contributors may be used to endorse 16255332Scy * or promote products derived from this software without specific prior 17255332Scy * written permission. 18255332Scy * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 19255332Scy * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 20255332Scy * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 21255332Scy * 22145555Sdarrenr */ 23145524Sdarrenr#ifndef lint 24162199Sobrienstatic char rcsid[] = 25162199Sobrien "@(#) $Header: grammar.y,v 1.54 96/07/17 00:11:34 leres Exp $ (LBL)"; 26145524Sdarrenr#endif 27162199Sobrien 28162199Sobrien#include <sys/types.h> 29145524Sdarrenr#include <sys/time.h> 30162199Sobrien#include <sys/socket.h> 31145524Sdarrenr 32162199Sobrien#if __STDC__ 33145524Sdarrenrstruct mbuf; 34145524Sdarrenrstruct rtentry; 35145524Sdarrenr#endif 36145524Sdarrenr 37145524Sdarrenr#include <net/if.h> 38145524Sdarrenr 39145524Sdarrenr#include <netinet/in.h> 40145524Sdarrenr#include <netinet/if_ether.h> 41145524Sdarrenr 42145524Sdarrenr#include <stdio.h> 43145524Sdarrenr 44145524Sdarrenr#include "pcap-int.h" 45145524Sdarrenr 46145524Sdarrenr#include "gencode.h" 47145524Sdarrenr#include <pcap-namedb.h> 48145524Sdarrenr 49145524Sdarrenr#include "gnuc.h" 50145524Sdarrenr#ifdef HAVE_OS_PROTO_H 51145524Sdarrenr#include "os-proto.h" 52145524Sdarrenr#endif 53145524Sdarrenr 54145524Sdarrenr#define QSET(q, p, d, a) (q).proto = (p),\ 55145524Sdarrenr (q).dir = (d),\ 56145524Sdarrenr (q).addr = (a) 57145524Sdarrenr 58145524Sdarrenrint n_errors = 0; 59145524Sdarrenr 60145524Sdarrenrstatic struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF }; 61145524Sdarrenr 62145524Sdarrenrstatic void 63145524Sdarrenryyerror(char *msg) 64145524Sdarrenr{ 65145524Sdarrenr ++n_errors; 66145524Sdarrenr bpf_error(msg); 67145524Sdarrenr /* NOTREACHED */ 68145524Sdarrenr} 69145524Sdarrenr 70145524Sdarrenr#ifndef YYBISON 71145524Sdarrenrint yyparse(void); 72145524Sdarrenr 73145524Sdarrenrint 74145524Sdarrenrpcap_parse() 75145524Sdarrenr{ 76145524Sdarrenr return (yyparse()); 77145524Sdarrenr} 78145524Sdarrenr#endif 79145524Sdarrenr 80145524Sdarrenr%} 81145524Sdarrenr 82145524Sdarrenr%union { 83145524Sdarrenr int i; 84145524Sdarrenr bpf_u_int32 h; 85145524Sdarrenr u_char *e; 86145524Sdarrenr char *s; 87145524Sdarrenr struct stmt *stmt; 88145524Sdarrenr struct arth *a; 89145524Sdarrenr struct { 90145524Sdarrenr struct qual q; 91145524Sdarrenr struct block *b; 92145524Sdarrenr } blk; 93145524Sdarrenr struct block *rblk; 94145524Sdarrenr} 95145524Sdarrenr 96145524Sdarrenr%type <blk> expr id nid pid term rterm qid 97145524Sdarrenr%type <blk> head 98%type <i> pqual dqual aqual ndaqual 99%type <a> arth narth 100%type <i> byteop pname pnum relop irelop 101%type <blk> and or paren not null prog 102%type <rblk> other 103 104%token DST SRC HOST GATEWAY 105%token NET MASK PORT LESS GREATER PROTO BYTE 106%token ARP RARP IP TCP UDP ICMP IGMP IGRP 107%token ATALK DECNET LAT SCA MOPRC MOPDL 108%token TK_BROADCAST TK_MULTICAST 109%token NUM INBOUND OUTBOUND 110%token LINK 111%token GEQ LEQ NEQ 112%token ID EID HID 113%token LSH RSH 114%token LEN 115 116%type <s> ID 117%type <e> EID 118%type <s> HID 119%type <i> NUM 120 121%left OR AND 122%nonassoc '!' 123%left '|' 124%left '&' 125%left LSH RSH 126%left '+' '-' 127%left '*' '/' 128%nonassoc UMINUS 129%% 130prog: null expr 131{ 132 finish_parse($2.b); 133} 134 | null 135 ; 136null: /* null */ { $$.q = qerr; } 137 ; 138expr: term 139 | expr and term { gen_and($1.b, $3.b); $$ = $3; } 140 | expr and id { gen_and($1.b, $3.b); $$ = $3; } 141 | expr or term { gen_or($1.b, $3.b); $$ = $3; } 142 | expr or id { gen_or($1.b, $3.b); $$ = $3; } 143 ; 144and: AND { $$ = $<blk>0; } 145 ; 146or: OR { $$ = $<blk>0; } 147 ; 148id: nid 149 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1, 150 $$.q = $<blk>0.q); } 151 | paren pid ')' { $$ = $2; } 152 ; 153nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); } 154 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3, 155 $$.q = $<blk>0.q); } 156 | HID MASK HID { $$.b = gen_mcode($1, $3, 0, 157 $$.q = $<blk>0.q); } 158 | HID { 159 /* Decide how to parse HID based on proto */ 160 $$.q = $<blk>0.q; 161 switch ($$.q.proto) { 162 case Q_DECNET: 163 $$.b = gen_ncode($1, 0, $$.q); 164 break; 165 default: 166 $$.b = gen_ncode($1, 0, $$.q); 167 break; 168 } 169 } 170 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); } 171 | not id { gen_not($2.b); $$ = $2; } 172 ; 173not: '!' { $$ = $<blk>0; } 174 ; 175paren: '(' { $$ = $<blk>0; } 176 ; 177pid: nid 178 | qid and id { gen_and($1.b, $3.b); $$ = $3; } 179 | qid or id { gen_or($1.b, $3.b); $$ = $3; } 180 ; 181qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1, 182 $$.q = $<blk>0.q); } 183 | pid 184 ; 185term: rterm 186 | not term { gen_not($2.b); $$ = $2; } 187 ; 188head: pqual dqual aqual { QSET($$.q, $1, $2, $3); } 189 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); } 190 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); } 191 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); } 192 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); } 193 ; 194rterm: head id { $$ = $2; } 195 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; } 196 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; } 197 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0); 198 $$.q = qerr; } 199 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1); 200 $$.q = qerr; } 201 | other { $$.b = $1; $$.q = qerr; } 202 ; 203/* protocol level qualifiers */ 204pqual: pname 205 | { $$ = Q_DEFAULT; } 206 ; 207/* 'direction' qualifiers */ 208dqual: SRC { $$ = Q_SRC; } 209 | DST { $$ = Q_DST; } 210 | SRC OR DST { $$ = Q_OR; } 211 | DST OR SRC { $$ = Q_OR; } 212 | SRC AND DST { $$ = Q_AND; } 213 | DST AND SRC { $$ = Q_AND; } 214 ; 215/* address type qualifiers */ 216aqual: HOST { $$ = Q_HOST; } 217 | NET { $$ = Q_NET; } 218 | PORT { $$ = Q_PORT; } 219 ; 220/* non-directional address type qualifiers */ 221ndaqual: GATEWAY { $$ = Q_GATEWAY; } 222 ; 223pname: LINK { $$ = Q_LINK; } 224 | IP { $$ = Q_IP; } 225 | ARP { $$ = Q_ARP; } 226 | RARP { $$ = Q_RARP; } 227 | TCP { $$ = Q_TCP; } 228 | UDP { $$ = Q_UDP; } 229 | ICMP { $$ = Q_ICMP; } 230 | IGMP { $$ = Q_IGMP; } 231 | IGRP { $$ = Q_IGRP; } 232 | ATALK { $$ = Q_ATALK; } 233 | DECNET { $$ = Q_DECNET; } 234 | LAT { $$ = Q_LAT; } 235 | SCA { $$ = Q_SCA; } 236 | MOPDL { $$ = Q_MOPDL; } 237 | MOPRC { $$ = Q_MOPRC; } 238 ; 239other: pqual TK_BROADCAST { $$ = gen_broadcast($1); } 240 | pqual TK_MULTICAST { $$ = gen_multicast($1); } 241 | LESS NUM { $$ = gen_less($2); } 242 | GREATER NUM { $$ = gen_greater($2); } 243 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); } 244 | INBOUND { $$ = gen_inbound(0); } 245 | OUTBOUND { $$ = gen_inbound(1); } 246 ; 247relop: '>' { $$ = BPF_JGT; } 248 | GEQ { $$ = BPF_JGE; } 249 | '=' { $$ = BPF_JEQ; } 250 ; 251irelop: LEQ { $$ = BPF_JGT; } 252 | '<' { $$ = BPF_JGE; } 253 | NEQ { $$ = BPF_JEQ; } 254 ; 255arth: pnum { $$ = gen_loadi($1); } 256 | narth 257 ; 258narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); } 259 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); } 260 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); } 261 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); } 262 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); } 263 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); } 264 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); } 265 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); } 266 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); } 267 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); } 268 | '-' arth %prec UMINUS { $$ = gen_neg($2); } 269 | paren narth ')' { $$ = $2; } 270 | LEN { $$ = gen_loadlen(); } 271 ; 272byteop: '&' { $$ = '&'; } 273 | '|' { $$ = '|'; } 274 | '<' { $$ = '<'; } 275 | '>' { $$ = '>'; } 276 | '=' { $$ = '='; } 277 ; 278pnum: NUM 279 | paren pnum ')' { $$ = $2; } 280 ; 281%% 282