Home | History | Annotate | Line # | Download | only in npfctl
npf_scan.l revision 1.26.2.1
      1 /*-
      2  * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Martin Husemann.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 %{
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <err.h>
     34 
     35 #include "npfctl.h"
     36 #include "npf_parse.h"
     37 
     38 int	yycolumn;
     39 
     40 #define	YY_USER_ACTION	yycolumn += yyleng;
     41 
     42 extern int		yyparsetarget;
     43 extern int		yylineno;
     44 extern const char *	yyfilename;
     45 extern int		yyparse(void);
     46 extern void		yyrestart(FILE *);
     47 
     48 void
     49 npfctl_parse_file(const char *name)
     50 {
     51 	FILE *fp;
     52 
     53 	fp = fopen(name, "r");
     54 	if (fp == NULL) {
     55 		err(EXIT_FAILURE, "open '%s'", name);
     56 	}
     57 	yyparsetarget = NPFCTL_PARSE_FILE;
     58 	yyrestart(fp);
     59 	yylineno = 1;
     60 	yycolumn = 0;
     61 	yyfilename = name;
     62 	yyparse();
     63 	fclose(fp);
     64 }
     65 
     66 void
     67 npfctl_parse_string(const char *str)
     68 {
     69 	YY_BUFFER_STATE bs;
     70 
     71 	yyparsetarget = NPFCTL_PARSE_STRING;
     72 	bs = yy_scan_string(str);
     73 	yyfilename = "stdin";
     74 	yyparse();
     75 	yy_delete_buffer(bs);
     76 }
     77 
     78 %}
     79 
     80 %option noyywrap nounput noinput
     81 
     82 ID	[a-zA-Z_][a-zA-Z_0-9]*
     83 DID	[a-zA-Z_][a-zA-Z_0-9-]*
     84 NUMBER	[0-9]+
     85 HEXDIG	[0-9a-fA-F]+
     86 
     87 %%
     88 alg			return ALG;
     89 table			return TABLE;
     90 type			return TYPE;
     91 hash			return HASH;
     92 tree			return TREE;
     93 cdb			return CDB;
     94 static			return TSTATIC;
     95 dynamic			return TDYNAMIC;
     96 file			return TFILE;
     97 map			return MAP;
     98 no-ports		return NO_PORTS;
     99 set			return SET;
    100 "<->"			return ARROWBOTH;
    101 "<-"			return ARROWLEFT;
    102 "->"			return ARROWRIGHT;
    103 algo			return ALGO;
    104 npt66			return NPT66;
    105 "-"			return MINUS;
    106 procedure		return PROCEDURE;
    107 \\\n			yylineno++; yycolumn = 0;
    108 \n			yylineno++; yycolumn = 0; return SEPLINE;
    109 ;			return SEPLINE;
    110 name			return NAME;
    111 group			return GROUP;
    112 default			return DEFAULT;
    113 in			return IN;
    114 out			return OUT;
    115 forw			return FORW;
    116 interface		return INTERFACE;
    117 all			return ALL;
    118 block			return BLOCK;
    119 pass			return PASS;
    120 pcap-filter		return PCAP_FILTER;
    121 stateful		return STATEFUL;
    122 stateful-ends		return STATEFUL_ENDS;
    123 apply			return APPLY;
    124 final			return FINAL;
    125 quick			return FINAL;
    126 on			return ON;
    127 off			return OFF;
    128 bpf.jit			return BPFJIT;
    129 inet6			return INET6;
    130 inet4			return INET4;
    131 ifaddrs			return IFADDRS;
    132 proto			return PROTO;
    133 family			return FAMILY;
    134 tcp			return TCP;
    135 icmp			{ yylval.num = IPPROTO_ICMP; return ICMP; }
    136 ipv6-icmp		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    137 \"ipv6-icmp\"		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    138 return-rst		return RETURNRST;
    139 return-icmp		return RETURNICMP;
    140 return			return RETURN;
    141 ruleset			return RULESET;
    142 from			return FROM;
    143 to			return TO;
    144 port			return PORT;
    145 flags			return FLAGS;
    146 icmp-type		return ICMPTYPE;
    147 code			return CODE;
    148 any			return ANY;
    149 
    150 "/"			return SLASH;
    151 "{"			return CURLY_OPEN;
    152 "}"			return CURLY_CLOSE;
    153 "("			return PAR_OPEN;
    154 ")"			return PAR_CLOSE;
    155 ","			return COMMA;
    156 "="			return EQ;
    157 "!"			return EXCL_MARK;
    158 
    159 "0x"{HEXDIG} {
    160 			char *endp, *buf = ecalloc(1, yyleng + 1);
    161 			buf[yyleng] = 0;
    162 			yylval.num = strtoul(buf+2, &endp, 16);
    163 			free(buf);
    164 			return HEX;
    165 		}
    166 
    167 {NUMBER}"."{NUMBER} {
    168 			char *endp, *buf = estrndup(yytext, yyleng);
    169 			yylval.fpnum = strtod(buf, &endp);
    170 			free(buf);
    171 			return FPNUM;
    172 		}
    173 
    174 {HEXDIG}":"[0-9a-fA-F:]* {
    175 			yylval.str = estrndup(yytext, yyleng);
    176 			return IPV6ADDR;
    177 		}
    178 
    179 "::"{HEXDIG}[0-9a-fA-F:.]* {
    180 			yylval.str = estrndup(yytext, yyleng);
    181 			return IPV6ADDR;
    182 		}
    183 
    184 {NUMBER}"."[0-9][0-9.]* {
    185 			yylval.str = estrndup(yytext, yyleng);
    186 			return IPV4ADDR;
    187 		}
    188 
    189 {NUMBER}	{
    190 			char *endp, *buf = estrndup(yytext, yyleng);
    191 			yylval.num = strtoul(buf, &endp, 10);
    192 			free(buf);
    193 			return NUM;
    194 		}
    195 
    196 "<"{DID}">"	{
    197 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    198 			return TABLE_ID;
    199 		}
    200 
    201 "$"{ID}		{
    202 			yylval.str = estrndup(yytext + 1, yyleng - 1);
    203 			return VAR_ID;
    204 		}
    205 
    206 {ID}		{
    207 			yylval.str = estrndup(yytext, yyleng);
    208 			return IDENTIFIER;
    209 		}
    210 
    211 \"[^\"]*\"	{
    212 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    213 			return STRING;
    214 		}
    215 
    216 #.*$		/* drop comment until end of line */
    217 [ \t]		/* eat whitespace */
    218 
    219 :		return COLON;
    220