summaryrefslogtreecommitdiffstats
path: root/contrib/ipfilter/iplang/iplang_l.l
blob: f356d0fb44651e2397d8c160079f8753f8b13830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*	$FreeBSD$	*/

%{
/*
 * Copyright (C) 1997-1998 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 *
 * $Id: iplang_l.l,v 2.8 2003/07/28 01:15:31 darrenr Exp $
 */
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#if defined(__SVR4) || defined(__sysv__)
#include <sys/stream.h>
#endif
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include "iplang_y.h"
#include "ipf.h"

#ifndef	__P
# ifdef __STDC__
#  define	__P(x)	x
# else
#  define	__P(x)	()
# endif
#endif

extern int opts;

int lineNum = 0, ipproto = 0, oldipproto = 0, next = -1, laststate = 0;
int *prstack = NULL, numpr = 0, state = 0, token = 0;

void    yyerror __P((char *));
void	push_proto __P((void));
void	pop_proto __P((void));
int	next_state __P((int, int));
int	next_item __P((int));
int	save_token __P((void));
void	swallow __P((void));
int	yylex __P((void));

struct	lwordtab	{
	char	*word;
	int	state;
	int	next;
};

struct	lwordtab	words[] = {
	{ "interface",	IL_INTERFACE,		-1 },
	{ "iface",	IL_INTERFACE,		-1 },
	{ "name",	IL_IFNAME,		IL_TOKEN },
	{ "ifname",	IL_IFNAME,		IL_TOKEN },
	{ "router",	IL_DEFROUTER,		IL_TOKEN },
	{ "mtu",	IL_MTU,			IL_NUMBER },
	{ "eaddr",	IL_EADDR,		IL_TOKEN },
	{ "v4addr",	IL_V4ADDR,		IL_TOKEN },
	{ "ipv4",	IL_IPV4,		-1 },
	{ "v",		IL_V4V,			IL_TOKEN },
	{ "proto",	IL_V4PROTO,		IL_TOKEN },
	{ "hl",		IL_V4HL,		IL_TOKEN },
	{ "id",		IL_V4ID,		IL_TOKEN },
	{ "ttl",	IL_V4TTL,		IL_TOKEN },
	{ "tos",	IL_V4TOS,		IL_TOKEN },
	{ "src",	IL_V4SRC,		IL_TOKEN },
	{ "dst",	IL_V4DST,		IL_TOKEN },
	{ "opt",	IL_OPT,			-1 },
	{ "len",	IL_LEN,			IL_TOKEN },
	{ "off",	IL_OFF,			IL_TOKEN },
	{ "sum",	IL_SUM,			IL_TOKEN },
	{ "tcp",	IL_TCP,			-1 },
	{ "sport",	IL_SPORT,		IL_TOKEN },
	{ "dport",	IL_DPORT,		IL_TOKEN },
	{ "seq",	IL_TCPSEQ,		IL_TOKEN },
	{ "ack",	IL_TCPACK,		IL_TOKEN },
	{ "flags",	IL_TCPFL,		IL_TOKEN },
	{ "urp",	IL_TCPURP,		IL_TOKEN },
	{ "win",	IL_TCPWIN,		IL_TOKEN },
	{ "udp",	IL_UDP,			-1 },
	{ "send",	IL_SEND,		-1 },
	{ "via",	IL_VIA,			IL_TOKEN },
	{ "arp",	IL_ARP,			-1 },
	{ "data",	IL_DATA,		-1 },
	{ "value",	IL_DVALUE,		IL_TOKEN },
	{ "file",	IL_DFILE,		IL_TOKEN },
	{ "nop",	IL_IPO_NOP,		-1 },
	{ "eol",	IL_IPO_EOL,		-1 },
	{ "rr",		IL_IPO_RR,		-1 },
	{ "zsu",	IL_IPO_ZSU,		-1 },
	{ "mtup",	IL_IPO_MTUP,		-1 },
	{ "mtur",	IL_IPO_MTUR,		-1 },
	{ "encode",	IL_IPO_ENCODE,		-1 },
	{ "ts",		IL_IPO_TS,		-1 },
	{ "tr",		IL_IPO_TR,		-1 },
	{ "sec",	IL_IPO_SEC,		-1 },
	{ "secclass",	IL_IPO_SECCLASS,	IL_TOKEN },
	{ "lsrr",	IL_IPO_LSRR,		-1 },
	{ "esec",	IL_IPO_ESEC,		-1 },
	{ "cipso",	IL_IPO_CIPSO,		-1 },
	{ "satid",	IL_IPO_SATID,		-1 },
	{ "ssrr",	IL_IPO_SSRR,		-1 },
	{ "addext",	IL_IPO_ADDEXT,		-1 },
	{ "visa",	IL_IPO_VISA,		-1 },
	{ "imitd",	IL_IPO_IMITD,		-1 },
	{ "eip",	IL_IPO_EIP,		-1 },
	{ "finn",	IL_IPO_FINN,		-1 },
	{ "mss",	IL_TCPO_MSS,		IL_TOKEN },
	{ "wscale",	IL_TCPO_WSCALE,		IL_TOKEN },
	{ "reserv-4",	IL_IPS_RESERV4,		-1 },
	{ "topsecret",	IL_IPS_TOPSECRET,	-1 },
	{ "secret",	IL_IPS_SECRET,		-1 },
	{ "reserv-3",	IL_IPS_RESERV3,		-1 },
	{ "confid",	IL_IPS_CONFID,		-1 },
	{ "unclass",	IL_IPS_UNCLASS,		-1 },
	{ "reserv-2",	IL_IPS_RESERV2,		-1 },
	{ "reserv-1",	IL_IPS_RESERV1,		-1 },
	{ "icmp",	IL_ICMP,		-1 },
	{ "type",	IL_ICMPTYPE,		-1 },
	{ "code",	IL_ICMPCODE,		-1 },
	{ "echorep",	IL_ICMP_ECHOREPLY,	-1 },
	{ "unreach",	IL_ICMP_UNREACH,	-1 },
	{ "squench",	IL_ICMP_SOURCEQUENCH,	-1 },
	{ "redir",	IL_ICMP_REDIRECT,	-1 },
	{ "echo",	IL_ICMP_ECHO,		-1 },
	{ "routerad",	IL_ICMP_ROUTERADVERT,	-1 },
	{ "routersol",	IL_ICMP_ROUTERSOLICIT,	-1 },
	{ "timex",	IL_ICMP_TIMXCEED,	-1 },
	{ "paramprob",	IL_ICMP_PARAMPROB,	-1 },
	{ "timest",	IL_ICMP_TSTAMP,		-1 },
	{ "timestrep",	IL_ICMP_TSTAMPREPLY,	-1 },
	{ "inforeq",	IL_ICMP_IREQ,		-1 },
	{ "inforep",	IL_ICMP_IREQREPLY,	-1 },
	{ "maskreq",	IL_ICMP_MASKREQ,	-1 },
	{ "maskrep",	IL_ICMP_MASKREPLY,	-1 },
	{ "net-unr",	IL_ICMP_UNREACH_NET,	-1 },
	{ "host-unr",	IL_ICMP_UNREACH_HOST,	-1 },
	{ "proto-unr",	IL_ICMP_UNREACH_PROTOCOL,	-1 },
	{ "port-unr",	IL_ICMP_UNREACH_PORT,	-1 },
	{ "needfrag",	IL_ICMP_UNREACH_NEEDFRAG,	-1 },
	{ "srcfail",	IL_ICMP_UNREACH_SRCFAIL,	-1 },
	{ "net-unk",	IL_ICMP_UNREACH_NET_UNKNOWN,	-1 },
	{ "host-unk",	IL_ICMP_UNREACH_HOST_UNKNOWN,	-1 },
	{ "isolate",	IL_ICMP_UNREACH_ISOLATED,	-1 },
	{ "net-prohib",	IL_ICMP_UNREACH_NET_PROHIB,	-1 },
	{ "host-prohib",	IL_ICMP_UNREACH_HOST_PROHIB,	-1 },
	{ "net-tos",	IL_ICMP_UNREACH_TOSNET,	-1 },
	{ "host-tos",	IL_ICMP_UNREACH_TOSHOST,	-1 },
	{ "filter-prohib",	IL_ICMP_UNREACH_FILTER_PROHIB,	-1 },
	{ "host-preced",	IL_ICMP_UNREACH_HOST_PRECEDENCE,	-1 },
	{ "cutoff-preced",	IL_ICMP_UNREACH_PRECEDENCE_CUTOFF,	-1 },
	{ "net-redir",	IL_ICMP_REDIRECT_NET,	-1 },
	{ "host-redir",	IL_ICMP_REDIRECT_HOST,	-1 },
	{ "tos-net-redir",	IL_ICMP_REDIRECT_TOSNET,	-1 },
	{ "tos-host-redir",	IL_ICMP_REDIRECT_TOSHOST,	-1 },
	{ "intrans",	IL_ICMP_TIMXCEED_INTRANS,	-1 },
	{ "reass",	IL_ICMP_TIMXCEED_REASS,	-1 },
	{ "optabsent",	IL_ICMP_PARAMPROB_OPTABSENT,	-1 },
	{ "otime",	IL_ICMP_OTIME,		-1 },
	{ "rtime",	IL_ICMP_RTIME,		-1 },
	{ "ttime",	IL_ICMP_TTIME,		-1 },
	{ "icmpseq",	IL_ICMP_SEQ,		-1 },
	{ "icmpid",	IL_ICMP_SEQ,		-1 },
	{ ".",		IL_DOT,			-1 },
	{ NULL, 0, 0 }
};
%}
white	[ \t\r]+
%%
{white}	;
\n	{ lineNum++; swallow(); }
\{	{ push_proto(); return next_item('{'); }
\}	{ pop_proto(); return next_item('}'); }
;	{ return next_item(';'); }
[0-9]+	{ return next_item(IL_NUMBER); }
[0-9a-fA-F]	{ return next_item(IL_HEXDIGIT); }
:	{ return next_item(IL_COLON); }
#[^\n]*	{ return next_item(IL_COMMENT); }
[^ \{\}\n\t;:{}]*	{ return next_item(IL_TOKEN); }
\"[^\"]*\"	{ return next_item(IL_TOKEN); }
%%
void    yyerror(msg)
char    *msg;
{
	fprintf(stderr, "%s error at \"%s\", line %d\n", msg, yytext,
		lineNum + 1);
	exit(1);
}


void push_proto()
{
	numpr++;
	if (!prstack)
		prstack = (int *)malloc(sizeof(int));
	else
		prstack = (int *)realloc((char *)prstack, numpr * sizeof(int));
	prstack[numpr - 1] = oldipproto;
}


void pop_proto()
{
	numpr--;
	ipproto = prstack[numpr];
	if (!numpr) {
		free(prstack);
		prstack = NULL;
		return;
	}
	prstack = (int *)realloc((char *)prstack, numpr * sizeof(int));
}


int save_token()
{

	yylval.str = strdup((char *)yytext);
	return IL_TOKEN;
}


int next_item(nstate)
int nstate;
{
	struct	lwordtab	*wt;

	if (opts & OPT_DEBUG)
		printf("text=[%s] id=%d next=%d\n", yytext, nstate, next);
	if (next == IL_TOKEN) {
		next = -1;
		return save_token();
	}
	token++;

	for (wt = words; wt->word; wt++)
		if (!strcasecmp(wt->word, (char *)yytext))
			return next_state(wt->state, wt->next);
	if (opts & OPT_DEBUG)
		printf("unknown keyword=[%s]\n", yytext);
	next = -1;
	if (nstate == IL_NUMBER)
		yylval.num = atoi((char *)yytext);
	token++;
	return nstate;
}


int next_state(nstate, fornext)
int nstate, fornext;
{
	next = fornext;

	switch (nstate)
	{
	case IL_IPV4 :
	case IL_TCP :
	case IL_UDP :
	case IL_ICMP :
	case IL_DATA :
	case IL_INTERFACE :
	case IL_ARP :
		oldipproto = ipproto;
		ipproto = nstate;
		break;
	case IL_SUM :
		if (ipproto == IL_IPV4)
			nstate = IL_V4SUM;
		else if (ipproto == IL_TCP)
			nstate = IL_TCPSUM;
		else if (ipproto == IL_UDP)
			nstate = IL_UDPSUM;
		break;
	case IL_OPT :
		if (ipproto == IL_IPV4)
			nstate = IL_V4OPT;
		else if (ipproto == IL_TCP)
			nstate = IL_TCPOPT;
		break;
	case IL_IPO_NOP :
		if (ipproto == IL_TCP)
			nstate = IL_TCPO_NOP;
		break;
	case IL_IPO_EOL :
		if (ipproto == IL_TCP)
			nstate = IL_TCPO_EOL;
		break;
	case IL_IPO_TS :
		if (ipproto == IL_TCP)
			nstate = IL_TCPO_TS;
		break;
	case IL_OFF :
		if (ipproto == IL_IPV4)
			nstate = IL_V4OFF;
		else if (ipproto == IL_TCP)
			nstate = IL_TCPOFF;
		break;
	case IL_LEN :
		if (ipproto == IL_IPV4)
			nstate = IL_V4LEN;
		else if (ipproto == IL_UDP)
			nstate = IL_UDPLEN;
		break;
	}
	return nstate;
}


void swallow()
{
	int c;

	c = input();

	if (c == '#') {
		while ((c != '\n') && (c != EOF))
			c = input();
	}
	if (c != EOF)
		unput(c);
}
OpenPOWER on IntegriCloud