Home | History | Annotate | Line # | Download | only in npfctl
npf_scan.l revision 1.1.4.4
      1 /*	$NetBSD: npf_scan.l,v 1.1.4.4 2013/01/16 05:34:10 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2011-2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Martin Husemann.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 %{
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <err.h>
     36 
     37 #include "npfctl.h"
     38 #include "npf_parse.h"
     39 
     40 int	yycolumn;
     41 
     42 #define	YY_USER_ACTION	yycolumn += yyleng;
     43 
     44 %}
     45 
     46 %option noyywrap nounput noinput
     47 
     48 ID	[a-zA-Z_][a-zA-Z_0-9]*
     49 NID	[a-zA-Z_0-9]+
     50 NUMBER	[0-9]+
     51 
     52 %%
     53 table			return TABLE;
     54 type			return TYPE;
     55 hash			return HASH;
     56 tree			return TREE;
     57 static			return TSTATIC;
     58 dynamic			return TDYNAMIC;
     59 file			return TFILE;
     60 map			return MAP;
     61 "<->"			return ARROWBOTH;
     62 "<-"			return ARROWLEFT;
     63 "->"			return ARROWRIGHT;
     64 "-"			return MINUS;
     65 procedure		return PROCEDURE;
     66 \\\n			yylineno++; yycolumn = 0;
     67 \n			yylineno++; yycolumn = 0; return SEPLINE;
     68 ;			return SEPLINE;
     69 name			return NAME;
     70 group			return GROUP;
     71 default			return DEFAULT;
     72 in			return IN;
     73 out			return OUT;
     74 interface		return INTERFACE;
     75 all			return ALL;
     76 block			return BLOCK;
     77 pass			return PASS;
     78 stateful		return STATEFUL;
     79 apply			return APPLY;
     80 final			return FINAL;
     81 quick			return FINAL;
     82 on			return ON;
     83 ifnet			return IFNET;
     84 inet6			return INET6;
     85 inet4			return INET;
     86 inet			return INET;
     87 proto			return PROTO;
     88 family			return FAMILY;
     89 tcp			return TCP;
     90 icmp			{ yylval.num = IPPROTO_ICMP; return ICMP; }
     91 ipv6-icmp		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
     92 \"ipv6-icmp\"		{ yylval.num = IPPROTO_ICMPV6; return ICMP6; }
     93 return-rst		return RETURNRST;
     94 return-icmp		return RETURNICMP;
     95 return			return RETURN;
     96 from			return FROM;
     97 to			return TO;
     98 port			return PORT;
     99 flags			return FLAGS;
    100 icmp-type		return ICMPTYPE;
    101 code			return CODE;
    102 any			return ANY;
    103 
    104 "/"			return SLASH;
    105 "{"			return CURLY_OPEN;
    106 "}"			return CURLY_CLOSE;
    107 "("			return PAR_OPEN;
    108 ")"			return PAR_CLOSE;
    109 ","			return COMMA;
    110 "="			return EQ;
    111 
    112 "0x"[0-9a-fA-F]+ {
    113 			char *endp, *buf = ecalloc(1, yyleng + 1);
    114 			buf[yyleng] = 0;
    115 			yylval.num = strtoul(buf+2, &endp, 16);
    116 			free(buf);
    117 			return HEX;
    118 		}
    119 
    120 {NUMBER}"."{NUMBER} {
    121 			char *endp, *buf = estrndup(yytext, yyleng);
    122 			yylval.fpnum = strtod(buf, &endp);
    123 			free(buf);
    124 			return FPNUM;
    125 		}
    126 
    127 [0-9a-fA-F]+":"[0-9a-fA-F:]* {
    128 			yylval.str = estrndup(yytext, yyleng);
    129 			return IPV6ADDR;
    130 		}
    131 
    132 {NUMBER}"."[0-9][0-9.]* {
    133 			yylval.str = estrndup(yytext, yyleng);
    134 			return IPV4ADDR;
    135 		}
    136 
    137 {NUMBER}	{
    138 			char *endp, *buf = estrndup(yytext, yyleng);
    139 			yylval.num = strtoul(buf, &endp, 10);
    140 			free(buf);
    141 			return NUM;
    142 		}
    143 
    144 "<"{NID}">"	{
    145 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    146 			return TABLE_ID;
    147 		}
    148 
    149 "$"{NID}	{
    150 			yylval.str = estrndup(yytext + 1, yyleng - 1);
    151 			return VAR_ID;
    152 		}
    153 
    154 {ID}		{
    155 			yylval.str = estrndup(yytext, yyleng);
    156 			return IDENTIFIER;
    157 		}
    158 
    159 \"[^\"]*\"	{
    160 			yylval.str = estrndup(yytext + 1, yyleng - 2);
    161 			return STRING;
    162 		}
    163 
    164 #.*$		/* drop comment until end of line */
    165 [ \t]		/* eat whitespace */
    166 
    167 :		return COLON;
    168