Home | History | Annotate | Line # | Download | only in npfctl
npf_scan.l revision 1.29.2.2
      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		yystarttoken;
     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 	yystarttoken = 0;
     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, parse_entry_t entry)
     68 {
     69 	YY_BUFFER_STATE bs;
     70 
     71 	switch (entry) {
     72 	case NPFCTL_PARSE_RULE:
     73 		yystarttoken = RULE_ENTRY_TOKEN;
     74 		break;
     75 	case NPFCTL_PARSE_MAP:
     76 		yystarttoken = MAP_ENTRY_TOKEN;
     77 		break;
     78 	default:
     79 		abort();
     80 	}
     81 
     82 	bs = yy_scan_string(str);
     83 	yyfilename = "stdin";
     84 	yyparse();
     85 	yy_delete_buffer(bs);
     86 }
     87 
     88 %}
     89 
     90 %option noyywrap nounput noinput
     91 
     92 ID	[a-zA-Z_][a-zA-Z_0-9]*
     93 DID	[a-zA-Z_][a-zA-Z_0-9-]*
     94 SPID	[a-zA-Z][a-zA-Z_0-9.]*
     95 NUMBER	[0-9]+
     96 HEXDIG	[0-9a-fA-F]+
     97 
     98 %%
     99 %{
    100 	/* This is prepended to yylex(). */
    101 	if (yystarttoken) {
    102 		int token = yystarttoken;
    103 		yystarttoken = 0;
    104 		return token;
    105 	}
    106 %}
    107 
    108 alg			return ALG;
    109 table			return TABLE;
    110 type			return TYPE;
    111 hash			return HASH;
    112 tree			return TREE;
    113 lpm			return LPM;
    114 cdb			return CDB;
    115 const			return CONST;
    116 static			return TSTATIC;
    117 dynamic			return TDYNAMIC;
    118 file			return TFILE;
    119 map			return MAP;
    120 no-ports		return NO_PORTS;
    121 set			return SET;
    122 "<->"			return ARROWBOTH;
    123 "<-"			return ARROWLEFT;
    124 "->"			return ARROWRIGHT;
    125 algo			return ALGO;
    126 netmap			return NETMAP;
    127 ipset			return IPSET;
    128 "ip-hash"		return IPHASH;
    129 "round-robin"		return ROUNDROBIN;
    130 npt66			return NPT66;
    131 "-"			return MINUS;
    132 procedure		return PROCEDURE;
    133 \\\n			yylineno++; yycolumn = 0;
    134 \n			yylineno++; yycolumn = 0; return SEPLINE;
    135 ;			return SEPLINE;
    136 name			return NAME;
    137 group			return GROUP;
    138 default			return DEFAULT;
    139 in			return IN;
    140 out			return OUT;
    141 forw			return FORW;
    142 interface		return INTERFACE;
    143 all			return ALL;
    144 block			return BLOCK;
    145 pass			return PASS;
    146 pcap-filter		return PCAP_FILTER;
    147 stateful		return STATEFUL;
    148 stateful-all		return STATEFUL_ALL;
    149 apply			return APPLY;
    150 final			return FINAL;
    151 quick			return FINAL;
    152 on			return ON;
    153 off			return OFF;
    154 inet6			return INET6;
    155 inet4			return INET4;
    156 ifaddrs			return IFADDRS;
    157 proto			return PROTO;
    158 family			return FAMILY;
    159 tcp			return TCP;
    160 icmp			{ yylval.num = IPPROTO_ICMP; return ICMP; }
    161 ipv6-icmp		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    162 \"ipv6-icmp\"		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
    163 return-rst		return RETURNRST;
    164 return-icmp		return RETURNICMP;
    165 return			return RETURN;
    166 ruleset			return RULESET;
    167 from			return FROM;
    168 to			return TO;
    169 port			return PORT;
    170 flags			return FLAGS;
    171 icmp-type		return ICMPTYPE;
    172 code			return CODE;
    173 any			return ANY;
    174 
    175 "/"			return SLASH;
    176 "{"			return CURLY_OPEN;
    177 "}"			return CURLY_CLOSE;
    178 "("			return PAR_OPEN;
    179 ")"			return PAR_CLOSE;
    180 ","			return COMMA;
    181 "="			return EQ;
    182 "!"			return EXCL_MARK;
    183 
    184 "0x"{HEXDIG} {
    185 			char *endp, *buf = ecalloc(1, yyleng + 1);
    186 			buf[yyleng] = 0;
    187 			yylval.num = strtoul(buf+2, &endp, 16);
    188 			free(buf);
    189 			return HEX;
    190 		}
    191 
    192 {NUMBER}"."{NUMBER} {
    193 			char *endp, *buf = estrndup(yytext, yyleng);
    194 			yylval.fpnum = strtod(buf, &endp);
    195 			free(buf);
    196 			return FPNUM;
    197 		}
    198 
    199 {HEXDIG}":"[0-9a-fA-F:]* {
    200 			yylval.str = estrndup(yytext, yyleng);
    201 			return IPV6ADDR;
    202 		}
    203 
    204 "::"{HEXDIG}[0-9a-fA-F:.]* {
    205 			yylval.str = estrndup(yytext, yyleng);
    206 			return IPV6ADDR;
    207 		}
    208 
    209 {NUMBER}"."[0-9][0-9.]* {
    210 			yylval.str = estrndup(yytext, yyleng);
    211 			return IPV4ADDR;
    212 		}
    213 
    214 {NUMBER}	{
    215 			char *endp, *buf = estrndup(yytext, yyleng);
    216 			yylval.num = strtoul(buf, &endp, 10);
    217 			free(buf);
    218 			return NUM;
    219 		}
    220 
    221 "<"{DID}">"	{
    222 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    223 			return TABLE_ID;
    224 		}
    225 
    226 "$"{ID}		{
    227 			yylval.str = estrndup(yytext + 1, yyleng - 1);
    228 			return VAR_ID;
    229 		}
    230 
    231 {ID}"."{SPID}+	{
    232 			yylval.str = estrndup(yytext, yyleng);
    233 			return PARAM;
    234 		}
    235 
    236 {ID}		{
    237 			yylval.str = estrndup(yytext, yyleng);
    238 			return IDENTIFIER;
    239 		}
    240 
    241 \"[^\"]*\"	{
    242 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    243 			return STRING;
    244 		}
    245 
    246 #.*$		/* drop comment until end of line */
    247 [ \t]		/* eat whitespace */
    248 
    249 :		return COLON;
    250