xref: /titanic_50/usr/src/cmd/ipf/tools/ipf_y.y (revision df4cb6e0bcfc660e38e1e96ccd7b794c0466228a)
1 %{
2 /*
3  * Copyright (C) 2003 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
8  * Use is subject to license terms.
9  */
10 
11 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include "ipf.h"
14 #include <sys/ioctl.h>
15 #include <syslog.h>
16 #ifdef IPFILTER_BPF
17 # include "pcap-bpf.h"
18 # define _NET_BPF_H_
19 # include <pcap.h>
20 #endif
21 #include "netinet/ip_pool.h"
22 #include "netinet/ip_htable.h"
23 #include "netinet/ipl.h"
24 #include "ipf_l.h"
25 
26 #define	YYDEBUG	1
27 #define	DOALL(x)	for (fr = frc; fr != NULL; fr = fr->fr_next) { x }
28 #define	DOREM(x)	for (; fr != NULL; fr = fr->fr_next) { x }
29 
30 #define OPTION_LOG              0x1
31 #define OPTION_QUICK            0x2
32 #define OPTION_DUP              0x4
33 #define OPTION_PROUTE           0x8
34 #define OPTION_ON               0x10
35 #define OPTION_REPLYTO          0x20
36 #define OPTION_FROUTE           0x40
37 
38 extern	void	yyerror __P((char *));
39 extern	int	yyparse __P((void));
40 extern	int	yylex __P((void));
41 extern	int	yydebug;
42 extern	FILE	*yyin;
43 extern	int	yylineNum;
44 
45 static	void	newrule __P((void));
46 static	void	setipftype __P((void));
47 static	u_32_t	lookuphost __P((char *));
48 static	void	dobpf __P((int, char *));
49 static	void	resetaddr __P((void));
50 static	struct	alist_s	*newalist __P((struct alist_s *));
51 static	u_int	makehash __P((struct alist_s *));
52 static	int	makepool __P((struct alist_s *));
53 static	frentry_t *addrule __P((void));
54 static	void	setsyslog __P((void));
55 static	void	unsetsyslog __P((void));
56 static	void	fillgroup __P((frentry_t *));
57 
58 frentry_t	*fr = NULL, *frc = NULL, *frtop = NULL, *frold = NULL;
59 
60 static	int		ifpflag = 0;
61 static	int		nowith = 0;
62 static	int		dynamic = -1;
63 static	int		pooled = 0;
64 static	int		hashed = 0;
65 static	int		nrules = 0;
66 static	int		newlist = 0;
67 static	int		added = 0;
68 static	int		ipffd = -1;
69 static  int             ruleopts = 0;
70 static	int		*yycont = 0;
71 static	ioctlfunc_t	ipfioctl[IPL_LOGSIZE];
72 static	addfunc_t	ipfaddfunc = NULL;
73 static	struct	wordtab ipfwords[95];
74 static	struct	wordtab	addrwords[4];
75 static	struct	wordtab	maskwords[5];
76 static	struct	wordtab icmpcodewords[17];
77 static	struct	wordtab icmptypewords[16];
78 static	struct	wordtab ipv4optwords[25];
79 static	struct	wordtab ipv4secwords[9];
80 static	struct	wordtab ipv6optwords[8];
81 static	struct	wordtab logwords[33];
82 static  int             set_ipv6_addr = 0;
83 
84 %}
85 %union	{
86 	char	*str;
87 	u_32_t	num;
88 	struct	in_addr	ipa;
89 	frentry_t	fr;
90 	frtuc_t	*frt;
91 	struct	alist_s	*alist;
92 	u_short	port;
93 	struct	{
94 		u_short	p1;
95 		u_short	p2;
96 		int	pc;
97 	} pc;
98 	struct	{
99 		union	i6addr	a;
100 		union	i6addr	m;
101 	} ipp;
102 	union	i6addr	ip6;
103 };
104 
105 %type	<port>	portnum
106 %type	<num>	facility priority icmpcode seclevel secname icmptype
107 %type	<num>	opt compare range opttype flagset optlist ipv6hdrlist ipv6hdr
108 %type	<num>	portc porteq
109 %type	<ipa>	hostname ipv4 ipv4mask ipv4_16 ipv4_24
110 %type	<ip6>	ipv6mask
111 %type	<ipp>	addr ipaddr
112 %type	<str>	servicename name interfacename
113 %type	<pc>	portrange portcomp
114 %type	<alist>	addrlist poollist
115 
116 %token	<num>	YY_NUMBER YY_HEX
117 %token	<str>	YY_STR
118 %token		YY_COMMENT
119 %token		YY_CMP_EQ YY_CMP_NE YY_CMP_LE YY_CMP_GE YY_CMP_LT YY_CMP_GT
120 %token		YY_RANGE_OUT YY_RANGE_IN
121 %token	<ip6>	YY_IPV6
122 
123 %token	IPFY_PASS IPFY_BLOCK IPFY_COUNT IPFY_CALL
124 %token	IPFY_RETICMP IPFY_RETRST IPFY_RETICMPASDST
125 %token	IPFY_IN IPFY_OUT
126 %token	IPFY_QUICK IPFY_ON IPFY_OUTVIA IPFY_INVIA
127 %token	IPFY_DUPTO IPFY_TO IPFY_FROUTE IPFY_REPLY_TO IPFY_ROUTETO
128 %token	IPFY_TOS IPFY_TTL IPFY_PROTO
129 %token	IPFY_HEAD IPFY_GROUP
130 %token	IPFY_AUTH IPFY_PREAUTH
131 %token	IPFY_LOG IPFY_BODY IPFY_FIRST IPFY_LEVEL IPFY_ORBLOCK
132 %token	IPFY_LOGTAG IPFY_MATCHTAG IPFY_SETTAG IPFY_SKIP
133 %token	IPFY_FROM IPFY_ALL IPFY_ANY IPFY_BPFV4 IPFY_BPFV6 IPFY_POOL IPFY_HASH
134 %token	IPFY_PPS
135 %token	IPFY_ESP IPFY_AH
136 %token	IPFY_WITH IPFY_AND IPFY_NOT IPFY_NO IPFY_OPT
137 %token	IPFY_TCPUDP IPFY_TCP IPFY_UDP
138 %token	IPFY_FLAGS IPFY_MULTICAST
139 %token	IPFY_MASK IPFY_BROADCAST IPFY_NETWORK IPFY_NETMASKED IPFY_PEER
140 %token	IPFY_PORT
141 %token	IPFY_NOW
142 %token	IPFY_ICMP IPFY_ICMPTYPE IPFY_ICMPCODE
143 %token	IPFY_IPOPTS IPFY_SHORT IPFY_NAT IPFY_BADSRC IPFY_LOWTTL IPFY_FRAG
144 %token	IPFY_MBCAST IPFY_BAD IPFY_BADNAT IPFY_OOW IPFY_NEWISN IPFY_NOICMPERR
145 %token	IPFY_KEEP IPFY_STATE IPFY_FRAGS IPFY_LIMIT IPFY_STRICT IPFY_AGE
146 %token	IPFY_SYNC IPFY_FRAGBODY
147 %token	IPFY_IPOPT_NOP IPFY_IPOPT_RR IPFY_IPOPT_ZSU IPFY_IPOPT_MTUP
148 %token	IPFY_IPOPT_MTUR IPFY_IPOPT_ENCODE IPFY_IPOPT_TS IPFY_IPOPT_TR
149 %token	IPFY_IPOPT_SEC IPFY_IPOPT_LSRR IPFY_IPOPT_ESEC IPFY_IPOPT_CIPSO
150 %token	IPFY_IPOPT_SATID IPFY_IPOPT_SSRR IPFY_IPOPT_ADDEXT IPFY_IPOPT_VISA
151 %token	IPFY_IPOPT_IMITD IPFY_IPOPT_EIP IPFY_IPOPT_FINN IPFY_IPOPT_DPS
152 %token	IPFY_IPOPT_SDB IPFY_IPOPT_NSAPA IPFY_IPOPT_RTRALRT IPFY_IPOPT_UMP
153 %token	IPFY_SECCLASS IPFY_SEC_UNC IPFY_SEC_CONF IPFY_SEC_RSV1 IPFY_SEC_RSV2
154 %token	IPFY_SEC_RSV4 IPFY_SEC_SEC IPFY_SEC_TS IPFY_SEC_RSV3
155 
156 %token	IPF6_V6HDRS IPFY_IPV6OPT IPFY_IPV6OPT_DSTOPTS IPFY_IPV6OPT_HOPOPTS
157 %token	IPFY_IPV6OPT_IPV6 IPFY_IPV6OPT_NONE IPFY_IPV6OPT_ROUTING
158 
159 %token	IPFY_ICMPT_UNR IPFY_ICMPT_ECHO IPFY_ICMPT_ECHOR IPFY_ICMPT_SQUENCH
160 %token	IPFY_ICMPT_REDIR IPFY_ICMPT_TIMEX IPFY_ICMPT_PARAMP IPFY_ICMPT_TIMEST
161 %token	IPFY_ICMPT_TIMESTREP IPFY_ICMPT_INFOREQ IPFY_ICMPT_INFOREP
162 %token	IPFY_ICMPT_MASKREQ IPFY_ICMPT_MASKREP IPFY_ICMPT_ROUTERAD
163 %token	IPFY_ICMPT_ROUTERSOL
164 
165 %token	IPFY_ICMPC_NETUNR IPFY_ICMPC_HSTUNR IPFY_ICMPC_PROUNR IPFY_ICMPC_PORUNR
166 %token	IPFY_ICMPC_NEEDF IPFY_ICMPC_SRCFAIL IPFY_ICMPC_NETUNK IPFY_ICMPC_HSTUNK
167 %token	IPFY_ICMPC_ISOLATE IPFY_ICMPC_NETPRO IPFY_ICMPC_HSTPRO
168 %token	IPFY_ICMPC_NETTOS IPFY_ICMPC_HSTTOS IPFY_ICMPC_FLTPRO IPFY_ICMPC_HSTPRE
169 %token	IPFY_ICMPC_CUTPRE
170 
171 %token	IPFY_FAC_KERN IPFY_FAC_USER IPFY_FAC_MAIL IPFY_FAC_DAEMON IPFY_FAC_AUTH
172 %token	IPFY_FAC_SYSLOG IPFY_FAC_LPR IPFY_FAC_NEWS IPFY_FAC_UUCP IPFY_FAC_CRON
173 %token	IPFY_FAC_LOCAL0 IPFY_FAC_LOCAL1 IPFY_FAC_LOCAL2 IPFY_FAC_LOCAL3
174 %token	IPFY_FAC_LOCAL4 IPFY_FAC_LOCAL5 IPFY_FAC_LOCAL6 IPFY_FAC_LOCAL7
175 %token	IPFY_FAC_SECURITY IPFY_FAC_FTP IPFY_FAC_AUTHPRIV IPFY_FAC_AUDIT
176 %token	IPFY_FAC_LFMT IPFY_FAC_CONSOLE
177 
178 %token	IPFY_PRI_EMERG IPFY_PRI_ALERT IPFY_PRI_CRIT IPFY_PRI_ERR IPFY_PRI_WARN
179 %token	IPFY_PRI_NOTICE IPFY_PRI_INFO IPFY_PRI_DEBUG
180 %%
181 file:	line
182 	| assign
183 	| file line
184 	| file assign
185 	;
186 
187 line:	xx rule		{ while ((fr = frtop) != NULL) {
188 				frtop = fr->fr_next;
189 				fr->fr_next = NULL;
190 				(*ipfaddfunc)(ipffd, ipfioctl[IPL_LOGIPF], fr);
191 				fr->fr_next = frold;
192 				frold = fr;
193 			  }
194 			  resetlexer();
195 			}
196 	| YY_COMMENT
197 	;
198 
199 xx:	{ newrule(); }
200 	;
201 
202 assign:	YY_STR assigning YY_STR ';'	{ set_variable($1, $3);
203 					  resetlexer();
204 					  free($1);
205 					  free($3);
206 					}
207 	;
208 
209 assigning:
210 	'='				{ yyvarnext = 1; }
211 	;
212 
213 rule:	inrule eol
214 	| outrule eol
215 	;
216 
217 eol:	| ';'
218 	;
219 
220 inrule:
221 	rulehead markin { ruleopts = 0; } inopts rulemain ruletail intag ruletail2
222 	;
223 
224 outrule:
225 	rulehead markout { ruleopts = 0; } outopts rulemain ruletail outtag ruletail2
226 	;
227 
228 rulehead:
229 	collection action
230 	| insert collection action
231 	;
232 
233 markin:	IPFY_IN				{ fr->fr_flags |= FR_INQUE; }
234 	;
235 
236 markout:
237 	IPFY_OUT			{ fr->fr_flags |= FR_OUTQUE; }
238 	;
239 
240 rulemain:
241 	ipfrule
242 	| bpfrule
243 	;
244 
245 ipfrule:
246 	tos ttl proto ip
247 	;
248 
249 bpfrule:
250 	IPFY_BPFV4 '{' YY_STR '}' 	{ dobpf(4, $3); free($3); }
251 	| IPFY_BPFV6 '{' YY_STR '}' 	{ dobpf(6, $3); free($3); }
252 	;
253 
254 ruletail:
255 	with keep head group
256 	;
257 
258 ruletail2:
259 	pps age new
260 	;
261 
262 intag:	settagin matchtagin
263 	;
264 
265 outtag:	settagout matchtagout
266 	;
267 
268 insert:
269 	'@' YY_NUMBER			{ fr->fr_hits = (U_QUAD_T)$2 + 1; }
270 	;
271 
272 collection:
273 	| YY_NUMBER			{ fr->fr_collect = $1; }
274 	;
275 
276 action:	block
277 	| IPFY_PASS			{ fr->fr_flags |= FR_PASS; }
278 	| log
279 	| IPFY_COUNT			{ fr->fr_flags |= FR_ACCOUNT; }
280 	| auth
281 	| IPFY_SKIP YY_NUMBER		{ fr->fr_flags |= FR_SKIP;
282 					  fr->fr_arg = $2; }
283 	| IPFY_CALL func
284 	| IPFY_CALL IPFY_NOW func	{ fr->fr_flags |= FR_CALLNOW; }
285 	;
286 
287 block:	blocked
288 	| blocked blockreturn
289 	;
290 
291 blocked:
292 	IPFY_BLOCK			{ fr->fr_flags = FR_BLOCK; }
293 	;
294 blockreturn:
295 	IPFY_RETICMP			{ fr->fr_flags |= FR_RETICMP; }
296 	| IPFY_RETICMP returncode	{ fr->fr_flags |= FR_RETICMP; }
297 	| IPFY_RETICMPASDST		{ fr->fr_flags |= FR_FAKEICMP; }
298 	| IPFY_RETICMPASDST returncode	{ fr->fr_flags |= FR_FAKEICMP; }
299 	| IPFY_RETRST			{ fr->fr_flags |= FR_RETRST; }
300 	;
301 
302 log:	IPFY_LOG			{ fr->fr_flags |= FR_LOG; }
303 	| IPFY_LOG logoptions		{ fr->fr_flags |= FR_LOG; }
304 	;
305 
306 auth:	IPFY_AUTH			{ fr->fr_flags |= FR_AUTH; }
307 	| IPFY_AUTH IPFY_RETRST		{ fr->fr_flags |= (FR_AUTH|FR_RETRST);}
308 	| IPFY_PREAUTH			{ fr->fr_flags |= FR_PREAUTH; }
309 	;
310 
311 func:	YY_STR '/' YY_NUMBER	{ fr->fr_func = nametokva($1,
312 							  ipfioctl[IPL_LOGIPF]);
313 				  fr->fr_arg = $3;
314 				  free($1); }
315 	;
316 
317 inopts:
318 	| inopts inopt
319 	;
320 
321 inopt:
322 	logopt
323 	{
324 		if ( ruleopts & OPTION_LOG )
325 			yyerror("Duplicate log option");
326 		ruleopts |= OPTION_LOG;
327 	}
328 	| quick
329 	{
330 		if ( ruleopts & OPTION_QUICK )
331 			yyerror("Duplicate quick option");
332 		ruleopts |= OPTION_QUICK;
333 	}
334 	| on
335 	{
336 		if ( ruleopts & OPTION_ON )
337 			yyerror("Duplicate on option");
338 		ruleopts |= OPTION_ON;
339 	}
340 	| dup
341 	{
342 		if ( ruleopts & OPTION_DUP )
343 			yyerror("Duplicate dup option");
344 		ruleopts |= OPTION_DUP;
345 	}
346 	| froute
347 	{
348 		if ( ruleopts & OPTION_FROUTE )
349 			yyerror("Duplicate froute option");
350 		ruleopts |= OPTION_FROUTE;
351 	}
352 	| proute
353 	{
354 		if ( ruleopts & OPTION_PROUTE )
355 			yyerror("Duplicate proute option");
356 		ruleopts |= OPTION_PROUTE;
357 	}
358 	| replyto
359 	{
360 		if ( ruleopts & OPTION_REPLYTO )
361 			yyerror("Duplicate replyto option");
362 		ruleopts |= OPTION_REPLYTO;
363 	}
364 	;
365 
366 outopts:
367 	| outopts outopt
368 	;
369 
370 outopt:
371 	logopt
372 	{
373 		if ( ruleopts & OPTION_LOG )
374 			yyerror("Duplicate log option");
375 		ruleopts |= OPTION_LOG;
376 	}
377 	| quick
378 	{
379 		if ( ruleopts & OPTION_QUICK )
380 			yyerror("Duplicate quick option");
381 		ruleopts |= OPTION_QUICK;
382 	}
383 	| on
384 	{
385 		if ( ruleopts & OPTION_ON )
386 			yyerror("Duplicate on option");
387 		ruleopts |= OPTION_ON;
388 	}
389 	| dup
390 	{
391 		if ( ruleopts & OPTION_DUP )
392 			yyerror("Duplicate dup option");
393 		ruleopts |= OPTION_DUP;
394 	}
395 	| proute
396 	{
397 		if ( ruleopts & OPTION_PROUTE )
398 			yyerror("Duplicate proute option");
399 		ruleopts |= OPTION_PROUTE;
400 	}
401 	| replyto
402 	{
403 		if ( ruleopts & OPTION_REPLYTO )
404 			yyerror("Duplicate replyto option");
405 		ruleopts |= OPTION_REPLYTO;
406 	}
407 	;
408 
409 tos:	| settos YY_NUMBER	{ DOALL(fr->fr_tos = $2; fr->fr_mtos = 0xff;) }
410 	| settos YY_HEX	{ DOALL(fr->fr_tos = $2; fr->fr_mtos = 0xff;) }
411 	| settos lstart toslist lend
412 	;
413 
414 settos:	IPFY_TOS			{ setipftype(); }
415 	;
416 
417 toslist:
418 	YY_NUMBER	{ DOALL(fr->fr_tos = $1; fr->fr_mtos = 0xff;) }
419 	| YY_HEX	{ DOREM(fr->fr_tos = $1; fr->fr_mtos = 0xff;) }
420 	| toslist lmore YY_NUMBER
421 			{ DOREM(fr->fr_tos = $3; fr->fr_mtos = 0xff;) }
422 	| toslist lmore YY_HEX
423 			{ DOREM(fr->fr_tos = $3; fr->fr_mtos = 0xff;) }
424 	;
425 
426 ttl:	| setttl YY_NUMBER
427 			{ DOALL(fr->fr_ttl = $2; fr->fr_mttl = 0xff;) }
428 	| setttl lstart ttllist lend
429 	;
430 
431 lstart:	'('				{ newlist = 1; fr = frc; added = 0; }
432 	;
433 
434 lend:	')'				{ nrules += added; }
435 	;
436 
437 lmore:	lanother			{ if (newlist == 1) {
438 						newlist = 0;
439 					  }
440 					  fr = addrule();
441 					  if (yycont != NULL)
442 						*yycont = 1;
443 					}
444 	;
445 
446 lanother:
447 	| ','
448 	;
449 
450 setttl:	IPFY_TTL			{ setipftype(); }
451 	;
452 
453 ttllist:
454 	YY_NUMBER	{ DOREM(fr->fr_ttl = $1; fr->fr_mttl = 0xff;) }
455 	| ttllist lmore YY_NUMBER
456 			{ DOREM(fr->fr_ttl = $3; fr->fr_mttl = 0xff;) }
457 	;
458 
459 proto:	| protox protocol		{ yyresetdict(); }
460 	;
461 
462 protox:	IPFY_PROTO			{ setipftype();
463 					  fr = frc;
464 					  yysetdict(NULL); }
465 	;
466 
467 ip:	srcdst flags icmp
468 	;
469 
470 group:	| IPFY_GROUP YY_STR		{ DOALL(strncpy(fr->fr_group, $2, \
471 							FR_GROUPLEN); \
472 							fillgroup(fr););
473 					  free($2); }
474 	| IPFY_GROUP YY_NUMBER		{ DOALL(sprintf(fr->fr_group, "%d", \
475 							$2); \
476 							fillgroup(fr);) }
477 	;
478 
479 head:	| IPFY_HEAD YY_STR		{ DOALL(strncpy(fr->fr_grhead, $2, \
480 							FR_GROUPLEN););
481 					  free($2); }
482 	| IPFY_HEAD YY_NUMBER		{ DOALL(sprintf(fr->fr_grhead, "%d", \
483 							$2);) }
484 	;
485 
486 settagin:
487 	| IPFY_SETTAG '(' taginlist ')'
488 	;
489 
490 taginlist:
491 	taginspec
492 	| taginlist ',' taginspec
493 	;
494 
495 taginspec:
496 	logtag
497 	|nattag
498 	;
499 
500 nattag:	IPFY_NAT '=' YY_STR		{ DOALL(strncpy(fr->fr_nattag.ipt_tag,\
501 						$3, IPFTAG_LEN););
502 					  free($3); }
503 	| IPFY_NAT '=' YY_NUMBER	{ DOALL(sprintf(fr->fr_nattag.ipt_tag,\
504 						"%d", $3 & 0xffffffff);) }
505 	;
506 
507 logtag:	IPFY_LOG '=' YY_NUMBER		{ DOALL(fr->fr_logtag = $3;) }
508 	;
509 
510 settagout:
511 	| IPFY_SETTAG '(' tagoutlist ')'
512 	;
513 
514 tagoutlist:
515 	tagoutspec
516 	| tagoutlist ',' tagoutspec
517 	;
518 
519 tagoutspec:
520 	logtag
521 	| nattag
522 	;
523 
524 matchtagin:
525 	| IPFY_MATCHTAG '(' tagoutlist ')'
526 	;
527 
528 matchtagout:
529 	| IPFY_MATCHTAG '(' taginlist ')'
530 	;
531 
532 pps:	| IPFY_PPS YY_NUMBER		{ DOALL(fr->fr_pps = $2;) }
533 	;
534 
535 new:	| savegroup file restoregroup
536 	;
537 
538 savegroup:
539 	'{'
540 	;
541 
542 restoregroup:
543 	'}'
544 	;
545 
546 logopt:	log
547 	;
548 
549 quick:
550 	IPFY_QUICK			{ fr->fr_flags |= FR_QUICK; }
551 	;
552 
553 on:	IPFY_ON onname
554 	| IPFY_ON onname IPFY_INVIA vianame
555 	| IPFY_ON onname IPFY_OUTVIA vianame
556 	;
557 
558 onname:	interfacename
559 		{ strncpy(fr->fr_ifnames[0], $1, sizeof(fr->fr_ifnames[0]));
560 		  free($1);
561 		}
562 	| interfacename ',' interfacename
563 		{ strncpy(fr->fr_ifnames[0], $1, sizeof(fr->fr_ifnames[0]));
564 		  free($1);
565 		  strncpy(fr->fr_ifnames[1], $3, sizeof(fr->fr_ifnames[1]));
566 		  free($3);
567 		}
568 	;
569 
570 vianame:
571 	name
572 		{ strncpy(fr->fr_ifnames[2], $1, sizeof(fr->fr_ifnames[2]));
573 		  free($1);
574 		}
575 	| name ',' name
576 		{ strncpy(fr->fr_ifnames[2], $1, sizeof(fr->fr_ifnames[2]));
577 		  free($1);
578 		  strncpy(fr->fr_ifnames[3], $3, sizeof(fr->fr_ifnames[3]));
579 		  free($3);
580 		}
581 	;
582 
583 dup:	IPFY_DUPTO name
584 	{ strncpy(fr->fr_dif.fd_ifname, $2, sizeof(fr->fr_dif.fd_ifname));
585 	  free($2);
586 	}
587 	| IPFY_DUPTO name duptoseparator hostname
588 	{ strncpy(fr->fr_dif.fd_ifname, $2, sizeof(fr->fr_dif.fd_ifname));
589 	  fr->fr_dif.fd_ip = $4;
590 	  yyexpectaddr = 0;
591 	  free($2);
592 	}
593 	| IPFY_DUPTO name duptoseparator YY_IPV6
594 	{ strncpy(fr->fr_dif.fd_ifname, $2, sizeof(fr->fr_dif.fd_ifname));
595 	  bcopy(&$4, &fr->fr_dif.fd_ip6, sizeof(fr->fr_dif.fd_ip6));
596 	  yyexpectaddr = 0;
597 	  free($2);
598 	}
599 	;
600 
601 duptoseparator:
602 	':'	{ yyexpectaddr = 1; yycont = &yyexpectaddr; resetaddr(); }
603 	;
604 
605 froute:	IPFY_FROUTE			{ fr->fr_flags |= FR_FASTROUTE; }
606 	;
607 
608 proute:	routeto name
609 	{ strncpy(fr->fr_tif.fd_ifname, $2, sizeof(fr->fr_tif.fd_ifname));
610 	  free($2);
611 	}
612 	| routeto name duptoseparator hostname
613 	{ strncpy(fr->fr_tif.fd_ifname, $2, sizeof(fr->fr_tif.fd_ifname));
614 	  fr->fr_tif.fd_ip = $4;
615 	  yyexpectaddr = 0;
616 	  free($2);
617 	}
618 	| routeto name duptoseparator YY_IPV6
619 	{ strncpy(fr->fr_tif.fd_ifname, $2, sizeof(fr->fr_tif.fd_ifname));
620 	  bcopy(&$4, &fr->fr_tif.fd_ip6, sizeof(fr->fr_tif.fd_ip6));
621 	  yyexpectaddr = 0;
622 	  free($2);
623 	}
624 	;
625 
626 routeto:
627 	IPFY_TO
628 	| IPFY_ROUTETO
629 	;
630 
631 replyto:
632 	IPFY_REPLY_TO name
633 	{ strncpy(fr->fr_rif.fd_ifname, $2, sizeof(fr->fr_rif.fd_ifname));
634 	  free($2);
635 	}
636 	| IPFY_REPLY_TO name duptoseparator hostname
637 	{ strncpy(fr->fr_rif.fd_ifname, $2, sizeof(fr->fr_rif.fd_ifname));
638 	  fr->fr_rif.fd_ip = $4;
639 	  free($2);
640 	}
641 	;
642 
643 logoptions:
644 	logoption
645 	| logoptions logoption
646 	;
647 
648 logoption:
649 	IPFY_BODY			{ fr->fr_flags |= FR_LOGBODY; }
650 	| IPFY_FIRST			{ fr->fr_flags |= FR_LOGFIRST; }
651 	| IPFY_ORBLOCK			{ fr->fr_flags |= FR_LOGORBLOCK; }
652 	| level loglevel		{ unsetsyslog(); }
653 	;
654 
655 returncode:
656 	starticmpcode icmpcode ')'	{ fr->fr_icode = $2; yyresetdict(); }
657 	;
658 
659 starticmpcode:
660 	'('				{ yysetdict(icmpcodewords); }
661 	;
662 
663 srcdst:	| IPFY_ALL
664 	| fromto
665 	;
666 
667 protocol:
668 	YY_NUMBER		{ DOREM(fr->fr_proto = $1; \
669 					fr->fr_mproto = 0xff;) }
670 	| YY_STR		{ if (!strcmp($1, "tcp-udp")) {
671 					DOREM(fr->fr_flx |= FI_TCPUDP; \
672 					      fr->fr_mflx |= FI_TCPUDP;)
673 				  } else {
674 					int p = getproto($1);
675 					if (p == -1)
676 						yyerror("protocol unknown");
677 					DOREM(fr->fr_proto = p; \
678 						fr->fr_mproto = 0xff;)
679 				  }
680 				  free($1);
681 				}
682 	| YY_STR nextstring YY_STR
683 				{ if (!strcmp($1, "tcp") &&
684 				      !strcmp($3, "udp")) {
685 					DOREM(fr->fr_flx |= FI_TCPUDP; \
686 					      fr->fr_mflx |= FI_TCPUDP;)
687 				  } else
688 					YYERROR;
689 				  free($1);
690 				  free($3);
691 				}
692 	;
693 
694 nextstring:
695 	'/'			{ yysetdict(NULL); }
696 	;
697 
698 fromto:	from srcobject to dstobject	{ yyexpectaddr = 0; yycont = NULL; }
699 	| to dstobject			{ yyexpectaddr = 0; yycont = NULL; }
700 	| from srcobject		{ yyexpectaddr = 0; yycont = NULL; }
701 	;
702 
703 from:	IPFY_FROM			{ setipftype();
704 					  if (fr == NULL)
705 						fr = frc;
706 					  yyexpectaddr = 1;
707 					  if (yydebug)
708 						printf("set yyexpectaddr\n");
709 					  yycont = &yyexpectaddr;
710 					  yysetdict(addrwords);
711 					  resetaddr(); }
712 	;
713 
714 to:	IPFY_TO				{ if (fr == NULL)
715 						fr = frc;
716 					  yyexpectaddr = 1;
717 					  if (yydebug)
718 						printf("set yyexpectaddr\n");
719 					  yycont = &yyexpectaddr;
720 					  yysetdict(addrwords);
721 					  resetaddr(); }
722 	;
723 
724 with:	| andwith withlist
725 	;
726 
727 andwith:
728 	IPFY_WITH			{ nowith = 0; setipftype(); }
729 	| IPFY_AND			{ nowith = 0; setipftype(); }
730 	;
731 
732 flags:	| startflags flagset
733 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = FR_TCPFMAX;) }
734 	| startflags flagset '/' flagset
735 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = $4;) }
736 	| startflags '/' flagset
737 		{ DOALL(fr->fr_tcpf = 0; fr->fr_tcpfm = $3;) }
738 	| startflags YY_NUMBER
739 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = FR_TCPFMAX;) }
740 	| startflags '/' YY_NUMBER
741 		{ DOALL(fr->fr_tcpf = 0; fr->fr_tcpfm = $3;) }
742 	| startflags YY_NUMBER '/' YY_NUMBER
743 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = $4;) }
744 	| startflags flagset '/' YY_NUMBER
745 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = $4;) }
746 	| startflags YY_NUMBER '/' flagset
747 		{ DOALL(fr->fr_tcpf = $2; fr->fr_tcpfm = $4;) }
748 	;
749 
750 startflags:
751 	IPFY_FLAGS	{ if (frc->fr_type != FR_T_IPF)
752 				yyerror("flags with non-ipf type rule");
753 			  if (frc->fr_proto != IPPROTO_TCP)
754 				yyerror("flags with non-TCP rule");
755 			}
756 	;
757 
758 flagset:
759 	YY_STR				{ $$ = tcpflags($1); free($1); }
760 	| YY_HEX			{ $$ = $1; }
761 	;
762 
763 srcobject:
764 	{ yyresetdict(); } fromport
765 	| srcaddr srcport
766 	| '!' srcaddr srcport
767 		{ DOALL(fr->fr_flags |= FR_NOTSRCIP;) }
768 	;
769 
770 srcaddr:
771 	addr	{ DOREM(bcopy(&($1.a), &fr->fr_ip.fi_src, sizeof($1.a)); \
772 			bcopy(&($1.m), &fr->fr_mip.fi_src, sizeof($1.m)); \
773 			if (dynamic != -1) { \
774 				fr->fr_satype = ifpflag; \
775 				fr->fr_ipf->fri_sifpidx = dynamic; \
776 			} else if (pooled || hashed) \
777 				fr->fr_satype = FRI_LOOKUP;)
778 		}
779 	| lstart srcaddrlist lend
780 	;
781 
782 srcaddrlist:
783 	addr	{ DOREM(bcopy(&($1.a), &fr->fr_ip.fi_src, sizeof($1.a)); \
784 			bcopy(&($1.m), &fr->fr_mip.fi_src, sizeof($1.m)); \
785 			if (dynamic != -1) { \
786 				fr->fr_satype = ifpflag; \
787 				fr->fr_ipf->fri_sifpidx = dynamic; \
788 			} else if (pooled || hashed) \
789 				fr->fr_satype = FRI_LOOKUP;)
790 		}
791 	| srcaddrlist lmore addr
792 		{ DOREM(bcopy(&($3.a), &fr->fr_ip.fi_src, sizeof($3.a)); \
793 			bcopy(&($3.m), &fr->fr_mip.fi_src, sizeof($3.m)); \
794 			if (dynamic != -1) { \
795 				fr->fr_satype = ifpflag; \
796 				fr->fr_ipf->fri_sifpidx = dynamic; \
797 			} else if (pooled || hashed) \
798 				fr->fr_satype = FRI_LOOKUP;)
799 		}
800 	;
801 
802 srcport:
803 	| portcomp
804 		{ DOALL(fr->fr_scmp = $1.pc; fr->fr_sport = $1.p1;) }
805 	| portrange
806 		{ DOALL(fr->fr_scmp = $1.pc; fr->fr_sport = $1.p1; \
807 			fr->fr_stop = $1.p2;) }
808 	| porteq lstart srcportlist lend
809 		{ yyresetdict(); }
810 	;
811 
812 fromport:
813 	portcomp
814 		{ DOALL(fr->fr_scmp = $1.pc; fr->fr_sport = $1.p1;) }
815 	| portrange
816 		{ DOALL(fr->fr_scmp = $1.pc; fr->fr_sport = $1.p1; \
817 			fr->fr_stop = $1.p2;) }
818 	| porteq lstart srcportlist lend
819 		{ yyresetdict(); }
820 	;
821 
822 srcportlist:
823 	portnum		{ DOREM(fr->fr_scmp = FR_EQUAL; fr->fr_sport = $1;) }
824 	| srcportlist lmore portnum
825 			{ DOREM(fr->fr_scmp = FR_EQUAL; fr->fr_sport = $3;) }
826 	;
827 
828 dstobject:
829 	{ yyresetdict(); } toport
830 	| dstaddr dstport
831 	| '!' dstaddr dstport
832 			{ DOALL(fr->fr_flags |= FR_NOTDSTIP;) }
833 	;
834 
835 dstaddr:
836 	addr	{ DOREM(bcopy(&($1.a), &fr->fr_ip.fi_dst, sizeof($1.a)); \
837 			bcopy(&($1.m), &fr->fr_mip.fi_dst, sizeof($1.m)); \
838 			if (dynamic != -1) { \
839 				fr->fr_datype = ifpflag; \
840 				fr->fr_ipf->fri_difpidx = dynamic; \
841 			  } else if (pooled || hashed) \
842 				fr->fr_datype = FRI_LOOKUP;)
843 		}
844 	| lstart dstaddrlist lend
845 	;
846 
847 dstaddrlist:
848 	addr	{ DOREM(bcopy(&($1.a), &fr->fr_ip.fi_dst, sizeof($1.a)); \
849 			bcopy(&($1.m), &fr->fr_mip.fi_dst, sizeof($1.m)); \
850 			if (dynamic != -1) { \
851 				fr->fr_datype = ifpflag; \
852 				fr->fr_ipf->fri_difpidx = dynamic; \
853 			} else if (pooled || hashed) \
854 				fr->fr_datype = FRI_LOOKUP;)
855 		}
856 	| dstaddrlist lmore addr
857 		{ DOREM(bcopy(&($3.a), &fr->fr_ip.fi_dst, sizeof($3.a)); \
858 			bcopy(&($3.m), &fr->fr_mip.fi_dst, sizeof($3.m)); \
859 			if (dynamic != -1) { \
860 				fr->fr_datype = ifpflag; \
861 				fr->fr_ipf->fri_difpidx = dynamic; \
862 			} else if (pooled || hashed) \
863 				fr->fr_datype = FRI_LOOKUP;)
864 		}
865 	;
866 
867 
868 dstport:
869 	| portcomp
870 		{ DOALL(fr->fr_dcmp = $1.pc; fr->fr_dport = $1.p1;) }
871 	| portrange
872 		{ DOALL(fr->fr_dcmp = $1.pc; fr->fr_dport = $1.p1; \
873 			fr->fr_dtop = $1.p2;) }
874 	| porteq lstart dstportlist lend
875 		{ yyresetdict(); }
876 	;
877 
878 toport:
879 	portcomp
880 		{ DOALL(fr->fr_dcmp = $1.pc; fr->fr_dport = $1.p1;) }
881 	| portrange
882 		{ DOALL(fr->fr_dcmp = $1.pc; fr->fr_dport = $1.p1; \
883 			fr->fr_dtop = $1.p2;) }
884 	| porteq lstart dstportlist lend
885 		{ yyresetdict(); }
886 	;
887 
888 dstportlist:
889 	portnum		{ DOREM(fr->fr_dcmp = FR_EQUAL; fr->fr_dport = $1;) }
890 	| dstportlist lmore portnum
891 			{ DOREM(fr->fr_dcmp = FR_EQUAL; fr->fr_dport = $3;) }
892 	;
893 
894 addr:	pool '/' YY_NUMBER		{ pooled = 1;
895 					  yyexpectaddr = 0;
896 					  $$.a.iplookuptype = IPLT_POOL;
897 					  $$.a.iplookupnum = $3; }
898 	| pool '=' '(' poollist ')'	{ pooled = 1;
899 					  yyexpectaddr = 0;
900 					  $$.a.iplookuptype = IPLT_POOL;
901 					  $$.a.iplookupnum = makepool($4); }
902 	| hash '/' YY_NUMBER		{ hashed = 1;
903 					  yyexpectaddr = 0;
904 					  $$.a.iplookuptype = IPLT_HASH;
905 					  $$.a.iplookupnum = $3; }
906 	| hash '=' '(' addrlist ')'	{ hashed = 1;
907 					  yyexpectaddr = 0;
908 					  $$.a.iplookuptype = IPLT_HASH;
909 					  $$.a.iplookupnum = makehash($4); }
910 	| ipaddr			{ bcopy(&$1, &$$, sizeof($$));
911 					  yyexpectaddr = 0; }
912 	;
913 
914 ipaddr:	IPFY_ANY			{ bzero(&($$), sizeof($$));
915 					  yyresetdict();
916 					  yyexpectaddr = 0; }
917 	| hostname			{ $$.a.in4 = $1;
918 					  $$.m.in4_addr = 0xffffffff;
919 					  yyexpectaddr = 0; }
920 	| hostname			{ yyresetdict();
921 					  $$.a.in4_addr = $1.s_addr; }
922 		maskspace		{ yysetdict(maskwords); }
923 		ipv4mask		{ $$.m.in4_addr = $5.s_addr;
924 					  $$.a.in4_addr &= $5.s_addr;
925 					  yyresetdict();
926 					  yyexpectaddr = 0; }
927 	| YY_IPV6			{ set_ipv6_addr = 1;
928 					  bcopy(&$1, &$$.a, sizeof($$.a));
929 					  fill6bits(128, (u_32_t *)&$$.m);
930 					  yyresetdict();
931 					  yyexpectaddr = 0; }
932 	| YY_IPV6			{ set_ipv6_addr = 1;
933 					  yyresetdict();
934 					  bcopy(&$1, &$$.a, sizeof($$.a)); }
935 		maskspace		{ yysetdict(maskwords); }
936 		ipv6mask		{ bcopy(&$5, &$$.m, sizeof($$.m));
937 					  yyresetdict();
938 					  yyexpectaddr = 0; }
939 	;
940 
941 maskspace:
942 	'/'
943 	| IPFY_MASK
944 	;
945 
946 ipv4mask:
947 	ipv4				{ $$ = $1; }
948 	| YY_HEX			{ $$.s_addr = htonl($1); }
949 	| YY_NUMBER			{ if (($1 >= 0) && ($1 <= 32)) {
950 						ntomask(4, $1, (u_32_t *)&$$);
951 					  } else
952 						yyerror("invalid mask");
953 					}
954 	| IPFY_BROADCAST		{ if (ifpflag == FRI_DYNAMIC) {
955 						$$.s_addr = 0;
956 						ifpflag = FRI_BROADCAST;
957 					  } else
958 						YYERROR;
959 					}
960 	| IPFY_NETWORK			{ if (ifpflag == FRI_DYNAMIC) {
961 						$$.s_addr = 0;
962 						ifpflag = FRI_NETWORK;
963 					  } else
964 						YYERROR;
965 					}
966 	| IPFY_NETMASKED		{ if (ifpflag == FRI_DYNAMIC) {
967 						$$.s_addr = 0;
968 						ifpflag = FRI_NETMASKED;
969 					  } else
970 						YYERROR;
971 					}
972 	| IPFY_PEER			{ if (ifpflag == FRI_DYNAMIC) {
973 						$$.s_addr = 0;
974 						ifpflag = FRI_PEERADDR;
975 					  } else
976 						YYERROR;
977 					}
978 	;
979 
980 ipv6mask:
981 	YY_NUMBER			{ if (($1 >= 0) && ($1 <= 128)) {
982 						ntomask(6, $1, $$.i6);
983 					  } else
984 						yyerror("invalid mask");
985 					}
986 	| IPFY_BROADCAST		{ if (ifpflag == FRI_DYNAMIC) {
987 						bzero(&$$, sizeof($$));
988 						ifpflag = FRI_BROADCAST;
989 					  } else
990 						YYERROR;
991 					}
992 	| IPFY_NETWORK			{ if (ifpflag == FRI_DYNAMIC) {
993 						bzero(&$$, sizeof($$));
994 						ifpflag = FRI_BROADCAST;
995 					  } else
996 						YYERROR;
997 					}
998 	| IPFY_NETMASKED		{ if (ifpflag == FRI_DYNAMIC) {
999 						bzero(&$$, sizeof($$));
1000 						ifpflag = FRI_BROADCAST;
1001 					  } else
1002 						YYERROR;
1003 					}
1004 	| IPFY_PEER			{ if (ifpflag == FRI_DYNAMIC) {
1005 						bzero(&$$, sizeof($$));
1006 						ifpflag = FRI_BROADCAST;
1007 					  } else
1008 						YYERROR;
1009 					}
1010 	;
1011 
1012 hostname:
1013 	ipv4				{ $$ = $1; }
1014 	| YY_NUMBER			{ $$.s_addr = $1; }
1015 	| YY_HEX			{ $$.s_addr = $1; }
1016 	| YY_STR			{ $$.s_addr = lookuphost($1);
1017 					  free($1);
1018 					  if ($$.s_addr == 0 &&
1019 						ifpflag != FRI_DYNAMIC)
1020 						yyerror("Unknown hostname");
1021 					}
1022 	;
1023 
1024 addrlist:
1025 	ipaddr		{ $$ = newalist(NULL);
1026 			  if (set_ipv6_addr)
1027 				  $$->al_family = AF_INET6;
1028 			  else
1029 				  $$->al_family = AF_INET;
1030 			  set_ipv6_addr = 0;
1031 			  bcopy(&($1.a), &($$->al_i6addr), sizeof($1.a));
1032 			  bcopy(&($1.m), &($$->al_i6mask), sizeof($1.m)); }
1033 	| addrlist ',' ipaddr
1034 			{ $$ = newalist($1);
1035 			  if (set_ipv6_addr)
1036 				  $$->al_family = AF_INET6;
1037 			  else
1038 				  $$->al_family = AF_INET;
1039 			  set_ipv6_addr = 0;
1040 			  bcopy(&($3.a), &($$->al_i6addr), sizeof($3.a));
1041 			  bcopy(&($3.m), &($$->al_i6mask), sizeof($3.m)); }
1042 	;
1043 
1044 pool:	IPFY_POOL	{ yyexpectaddr = 0; yycont = NULL; yyresetdict(); }
1045 	;
1046 
1047 hash:	IPFY_HASH	{ yyexpectaddr = 0; yycont = NULL; yyresetdict(); }
1048 	;
1049 
1050 poollist:
1051 	ipaddr		{ $$ = newalist(NULL);
1052 			  if (set_ipv6_addr)
1053 				  $$->al_family = AF_INET6;
1054 			  else
1055 				  $$->al_family = AF_INET;
1056 			  set_ipv6_addr = 0;
1057 			  bcopy(&($1.a), &($$->al_i6addr), sizeof($1.a));
1058 			  bcopy(&($1.m), &($$->al_i6mask), sizeof($1.m)); }
1059 	| '!' ipaddr	{ $$ = newalist(NULL);
1060 			  $$->al_not = 1;
1061 			  if (set_ipv6_addr)
1062 				  $$->al_family = AF_INET6;
1063 			  else
1064 				  $$->al_family = AF_INET;
1065 			  set_ipv6_addr = 0;
1066 			  bcopy(&($2.a), &($$->al_i6addr), sizeof($2.a));
1067 			  bcopy(&($2.m), &($$->al_i6mask), sizeof($2.m)); }
1068 	| poollist ',' ipaddr
1069 			{ $$ = newalist($1);
1070 			  if (set_ipv6_addr)
1071 				  $$->al_family = AF_INET6;
1072 			  else
1073 				  $$->al_family = AF_INET;
1074 			  set_ipv6_addr = 0;
1075 			  bcopy(&($3.a), &($$->al_i6addr), sizeof($3.a));
1076 			  bcopy(&($3.m), &($$->al_i6mask), sizeof($3.m)); }
1077 	| poollist ',' '!' ipaddr
1078 			{ $$ = newalist($1);
1079 			  $$->al_not = 1;
1080 			  if (set_ipv6_addr)
1081 				  $$->al_family = AF_INET6;
1082 			  else
1083 				  $$->al_family = AF_INET;
1084 			  set_ipv6_addr = 0;
1085 			  bcopy(&($4.a), &($$->al_i6addr), sizeof($4.a));
1086 			  bcopy(&($4.m), &($$->al_i6mask), sizeof($4.m)); }
1087 	;
1088 
1089 port:	IPFY_PORT			{ yyexpectaddr = 0;
1090 					  yycont = NULL;
1091 					}
1092 	;
1093 
1094 portc:	port compare			{ $$ = $2;
1095 					  yysetdict(NULL); }
1096 	| porteq			{ $$ = $1; }
1097 	;
1098 
1099 porteq:	port '='			{ $$ = FR_EQUAL;
1100 					  yysetdict(NULL); }
1101 	;
1102 
1103 portr:	IPFY_PORT			{ yyexpectaddr = 0;
1104 					  yycont = NULL;
1105 					  yysetdict(NULL); }
1106 	;
1107 
1108 portcomp:
1109 	portc portnum			{ $$.pc = $1;
1110 					  $$.p1 = $2;
1111 					  yyresetdict(); }
1112 	;
1113 
1114 portrange:
1115 	portr portnum range portnum	{ $$.p1 = $2;
1116 					  $$.pc = $3;
1117 					  $$.p2 = $4;
1118 					  yyresetdict(); }
1119 	;
1120 
1121 icmp:	| itype icode
1122 	;
1123 
1124 itype:	seticmptype icmptype
1125 	{ DOALL(fr->fr_icmp = htons($2 << 8); fr->fr_icmpm = htons(0xff00););
1126 	  yyresetdict();
1127 	}
1128 	| seticmptype lstart typelist lend	{ yyresetdict(); }
1129 	;
1130 
1131 seticmptype:
1132 	IPFY_ICMPTYPE				{ setipftype();
1133 						  yysetdict(icmptypewords); }
1134 	;
1135 
1136 icode:	| seticmpcode icmpcode
1137 	{ DOALL(fr->fr_icmp |= htons($2); fr->fr_icmpm |= htons(0xff););
1138 	  yyresetdict();
1139 	}
1140 	| seticmpcode lstart codelist lend	{ yyresetdict(); }
1141 	;
1142 
1143 seticmpcode:
1144 	IPFY_ICMPCODE				{ yysetdict(icmpcodewords); }
1145 	;
1146 
1147 typelist:
1148 	icmptype
1149 	{ DOREM(fr->fr_icmp = htons($1 << 8); fr->fr_icmpm = htons(0xff00);) }
1150 	| typelist lmore icmptype
1151 	{ DOREM(fr->fr_icmp = htons($3 << 8); fr->fr_icmpm = htons(0xff00);) }
1152 	;
1153 
1154 codelist:
1155 	icmpcode
1156 	{ DOREM(fr->fr_icmp |= htons($1); fr->fr_icmpm |= htons(0xff);) }
1157 	| codelist lmore icmpcode
1158 	{ DOREM(fr->fr_icmp |= htons($3); fr->fr_icmpm |= htons(0xff);) }
1159 	;
1160 
1161 age:	| IPFY_AGE YY_NUMBER		{ DOALL(fr->fr_age[0] = $2; \
1162 						fr->fr_age[1] = $2;) }
1163 	| IPFY_AGE YY_NUMBER '/' YY_NUMBER
1164 					{ DOALL(fr->fr_age[0] = $2; \
1165 						fr->fr_age[1] = $4;) }
1166 	;
1167 
1168 keep:	| IPFY_KEEP keepstate
1169 	| IPFY_KEEP keepfrag
1170 	| IPFY_KEEP keepstate IPFY_KEEP keepfrag
1171 	| IPFY_KEEP keepfrag IPFY_KEEP keepstate
1172 	;
1173 
1174 keepstate:
1175 	IPFY_STATE stateoptlist		{ DOALL(fr->fr_flags |= FR_KEEPSTATE;)}
1176 	;
1177 
1178 keepfrag:
1179 	IPFY_FRAGS fragoptlist		{ DOALL(fr->fr_flags |= FR_KEEPFRAG;) }
1180 	| IPFY_FRAG fragoptlist		{ DOALL(fr->fr_flags |= FR_KEEPFRAG;) }
1181 	;
1182 
1183 fragoptlist:
1184 	| '(' fragopts ')'
1185 	;
1186 
1187 fragopts:
1188 	fragopt lanother fragopts
1189 	| fragopt
1190 	;
1191 
1192 fragopt:
1193 	IPFY_STRICT			{ DOALL(fr->fr_flags |= FR_FRSTRICT;) }
1194 	;
1195 
1196 stateoptlist:
1197 	| '(' stateopts ')'
1198 	;
1199 
1200 stateopts:
1201 	stateopt lanother stateopts
1202 	| stateopt
1203 	;
1204 
1205 stateopt:
1206 	IPFY_LIMIT YY_NUMBER	{ DOALL(fr->fr_statemax = $2;) }
1207 	| IPFY_STRICT		{ DOALL(if (fr->fr_proto != IPPROTO_TCP) { \
1208 						YYERROR; \
1209 					  } else \
1210 						fr->fr_flags |= FR_STSTRICT;)
1211 				}
1212 	| IPFY_NEWISN		{ DOALL(if (fr->fr_proto != IPPROTO_TCP) { \
1213 						YYERROR; \
1214 					  } else \
1215 						fr->fr_flags |= FR_NEWISN;)
1216 				}
1217 	| IPFY_NOICMPERR	{ DOALL(fr->fr_flags |= FR_NOICMPERR;) }
1218 
1219 	| IPFY_SYNC		{ DOALL(fr->fr_flags |= FR_STATESYNC;) }
1220 	;
1221 
1222 portnum:
1223 	servicename			{ if (getport(frc, $1, &($$)) == -1)
1224 						yyerror("service unknown");
1225 					  else
1226 						$$ = ntohs($$);
1227 					  free($1);
1228 					}
1229 	| YY_NUMBER			{ if ($1 > 65535)	/* Unsigned */
1230 						yyerror("invalid port number");
1231 					  else
1232 						$$ = $1;
1233 					}
1234 	;
1235 
1236 withlist:
1237 	withopt
1238 	| withlist withopt
1239 	| withlist ',' withopt
1240 	;
1241 
1242 withopt:
1243 	opttype		{ DOALL(fr->fr_flx |= $1; fr->fr_mflx |= $1;) }
1244 	| notwith opttype
1245 					{ DOALL(fr->fr_mflx |= $2;) }
1246 	| ipopt ipopts			{ yyresetdict(); }
1247 	| notwith ipopt ipopts		{ yyresetdict(); }
1248 	| startv6hdrs ipv6hdrs		{ yyresetdict(); }
1249 	;
1250 
1251 ipopt:	IPFY_OPT			{ yysetdict(ipv4optwords); }
1252 	;
1253 
1254 startv6hdrs:
1255 	IPF6_V6HDRS	{ if (use_inet6 == 0)
1256 				yyerror("only available with IPv6");
1257 			  yysetdict(ipv6optwords);
1258 			}
1259 	;
1260 
1261 notwith:
1262 	IPFY_NOT			{ nowith = 1; }
1263 	| IPFY_NO			{ nowith = 1; }
1264 	;
1265 
1266 opttype:
1267 	IPFY_IPOPTS			{ $$ = FI_OPTIONS; }
1268 	| IPFY_SHORT			{ $$ = FI_SHORT; }
1269 	| IPFY_NAT			{ $$ = FI_NATED; }
1270 	| IPFY_BAD			{ $$ = FI_BAD; }
1271 	| IPFY_BADNAT			{ $$ = FI_BADNAT; }
1272 	| IPFY_BADSRC			{ $$ = FI_BADSRC; }
1273 	| IPFY_LOWTTL			{ $$ = FI_LOWTTL; }
1274 	| IPFY_FRAG			{ $$ = FI_FRAG; }
1275 	| IPFY_FRAGBODY			{ $$ = FI_FRAGBODY; }
1276 	| IPFY_FRAGS			{ $$ = FI_FRAG; }
1277 	| IPFY_MBCAST			{ $$ = FI_MBCAST; }
1278 	| IPFY_MULTICAST		{ $$ = FI_MULTICAST; }
1279 	| IPFY_BROADCAST		{ $$ = FI_BROADCAST; }
1280 	| IPFY_STATE			{ $$ = FI_STATE; }
1281 	| IPFY_OOW			{ $$ = FI_OOW; }
1282 	;
1283 
1284 ipopts:	optlist		{ DOALL(fr->fr_mip.fi_optmsk |= $1;
1285 				if (!nowith)
1286 					fr->fr_ip.fi_optmsk |= $1;)
1287 			}
1288 	;
1289 
1290 optlist:
1291 	opt				{ $$ |= $1; }
1292 	| optlist ',' opt		{ $$ |= $1 | $3; }
1293 	;
1294 
1295 ipv6hdrs:
1296 	ipv6hdrlist	{ DOALL(fr->fr_mip.fi_optmsk |= $1;
1297 				if (!nowith)
1298 					fr->fr_ip.fi_optmsk |= $1;)
1299 			}
1300 	;
1301 
1302 ipv6hdrlist:
1303 	ipv6hdr				{ $$ |= $1; }
1304 	| ipv6hdrlist ',' ipv6hdr	{ $$ |= $1 | $3; }
1305 	;
1306 
1307 secname:
1308 	seclevel			{ $$ |= $1; }
1309 	| secname ',' seclevel		{ $$ |= $1 | $3; }
1310 	;
1311 
1312 seclevel:
1313 	IPFY_SEC_UNC			{ $$ = secbit(IPSO_CLASS_UNCL); }
1314 	| IPFY_SEC_CONF			{ $$ = secbit(IPSO_CLASS_CONF); }
1315 	| IPFY_SEC_RSV1			{ $$ = secbit(IPSO_CLASS_RES1); }
1316 	| IPFY_SEC_RSV2			{ $$ = secbit(IPSO_CLASS_RES2); }
1317 	| IPFY_SEC_RSV3			{ $$ = secbit(IPSO_CLASS_RES3); }
1318 	| IPFY_SEC_RSV4			{ $$ = secbit(IPSO_CLASS_RES4); }
1319 	| IPFY_SEC_SEC			{ $$ = secbit(IPSO_CLASS_SECR); }
1320 	| IPFY_SEC_TS			{ $$ = secbit(IPSO_CLASS_TOPS); }
1321 	;
1322 
1323 icmptype:
1324 	YY_NUMBER			{ $$ = $1; }
1325 	| IPFY_ICMPT_UNR		{ $$ = ICMP_UNREACH; }
1326 	| IPFY_ICMPT_ECHO		{ $$ = ICMP_ECHO; }
1327 	| IPFY_ICMPT_ECHOR		{ $$ = ICMP_ECHOREPLY; }
1328 	| IPFY_ICMPT_SQUENCH		{ $$ = ICMP_SOURCEQUENCH; }
1329 	| IPFY_ICMPT_REDIR		{ $$ = ICMP_REDIRECT; }
1330 	| IPFY_ICMPT_TIMEX		{ $$ = ICMP_TIMXCEED; }
1331 	| IPFY_ICMPT_PARAMP		{ $$ = ICMP_PARAMPROB; }
1332 	| IPFY_ICMPT_TIMEST		{ $$ = ICMP_TSTAMP; }
1333 	| IPFY_ICMPT_TIMESTREP		{ $$ = ICMP_TSTAMPREPLY; }
1334 	| IPFY_ICMPT_INFOREQ		{ $$ = ICMP_IREQ; }
1335 	| IPFY_ICMPT_INFOREP		{ $$ = ICMP_IREQREPLY; }
1336 	| IPFY_ICMPT_MASKREQ		{ $$ = ICMP_MASKREQ; }
1337 	| IPFY_ICMPT_MASKREP		{ $$ = ICMP_MASKREPLY; }
1338 	| IPFY_ICMPT_ROUTERAD		{ $$ = ICMP_ROUTERADVERT; }
1339 	| IPFY_ICMPT_ROUTERSOL		{ $$ = ICMP_ROUTERSOLICIT; }
1340 	;
1341 
1342 icmpcode:
1343 	YY_NUMBER			{ $$ = $1; }
1344 	| IPFY_ICMPC_NETUNR		{ $$ = ICMP_UNREACH_NET; }
1345 	| IPFY_ICMPC_HSTUNR		{ $$ = ICMP_UNREACH_HOST; }
1346 	| IPFY_ICMPC_PROUNR		{ $$ = ICMP_UNREACH_PROTOCOL; }
1347 	| IPFY_ICMPC_PORUNR		{ $$ = ICMP_UNREACH_PORT; }
1348 	| IPFY_ICMPC_NEEDF		{ $$ = ICMP_UNREACH_NEEDFRAG; }
1349 	| IPFY_ICMPC_SRCFAIL		{ $$ = ICMP_UNREACH_SRCFAIL; }
1350 	| IPFY_ICMPC_NETUNK		{ $$ = ICMP_UNREACH_NET_UNKNOWN; }
1351 	| IPFY_ICMPC_HSTUNK		{ $$ = ICMP_UNREACH_HOST_UNKNOWN; }
1352 	| IPFY_ICMPC_ISOLATE		{ $$ = ICMP_UNREACH_ISOLATED; }
1353 	| IPFY_ICMPC_NETPRO		{ $$ = ICMP_UNREACH_NET_PROHIB; }
1354 	| IPFY_ICMPC_HSTPRO		{ $$ = ICMP_UNREACH_HOST_PROHIB; }
1355 	| IPFY_ICMPC_NETTOS		{ $$ = ICMP_UNREACH_TOSNET; }
1356 	| IPFY_ICMPC_HSTTOS		{ $$ = ICMP_UNREACH_TOSHOST; }
1357 	| IPFY_ICMPC_FLTPRO		{ $$ = ICMP_UNREACH_ADMIN_PROHIBIT; }
1358 	| IPFY_ICMPC_HSTPRE		{ $$ = 14; }
1359 	| IPFY_ICMPC_CUTPRE		{ $$ = 15; }
1360 	;
1361 
1362 opt:
1363 	IPFY_IPOPT_NOP			{ $$ = getoptbyvalue(IPOPT_NOP); }
1364 	| IPFY_IPOPT_RR			{ $$ = getoptbyvalue(IPOPT_RR); }
1365 	| IPFY_IPOPT_ZSU		{ $$ = getoptbyvalue(IPOPT_ZSU); }
1366 	| IPFY_IPOPT_MTUP		{ $$ = getoptbyvalue(IPOPT_MTUP); }
1367 	| IPFY_IPOPT_MTUR		{ $$ = getoptbyvalue(IPOPT_MTUR); }
1368 	| IPFY_IPOPT_ENCODE		{ $$ = getoptbyvalue(IPOPT_ENCODE); }
1369 	| IPFY_IPOPT_TS			{ $$ = getoptbyvalue(IPOPT_TS); }
1370 	| IPFY_IPOPT_TR			{ $$ = getoptbyvalue(IPOPT_TR); }
1371 	| IPFY_IPOPT_SEC		{ $$ = getoptbyvalue(IPOPT_SECURITY); }
1372 	| IPFY_IPOPT_LSRR		{ $$ = getoptbyvalue(IPOPT_LSRR); }
1373 	| IPFY_IPOPT_ESEC		{ $$ = getoptbyvalue(IPOPT_E_SEC); }
1374 	| IPFY_IPOPT_CIPSO		{ $$ = getoptbyvalue(IPOPT_CIPSO); }
1375 	| IPFY_IPOPT_SATID		{ $$ = getoptbyvalue(IPOPT_SATID); }
1376 	| IPFY_IPOPT_SSRR		{ $$ = getoptbyvalue(IPOPT_SSRR); }
1377 	| IPFY_IPOPT_ADDEXT		{ $$ = getoptbyvalue(IPOPT_ADDEXT); }
1378 	| IPFY_IPOPT_VISA		{ $$ = getoptbyvalue(IPOPT_VISA); }
1379 	| IPFY_IPOPT_IMITD		{ $$ = getoptbyvalue(IPOPT_IMITD); }
1380 	| IPFY_IPOPT_EIP		{ $$ = getoptbyvalue(IPOPT_EIP); }
1381 	| IPFY_IPOPT_FINN		{ $$ = getoptbyvalue(IPOPT_FINN); }
1382 	| IPFY_IPOPT_DPS		{ $$ = getoptbyvalue(IPOPT_DPS); }
1383 	| IPFY_IPOPT_SDB		{ $$ = getoptbyvalue(IPOPT_SDB); }
1384 	| IPFY_IPOPT_NSAPA		{ $$ = getoptbyvalue(IPOPT_NSAPA); }
1385 	| IPFY_IPOPT_RTRALRT		{ $$ = getoptbyvalue(IPOPT_RTRALRT); }
1386 	| IPFY_IPOPT_UMP		{ $$ = getoptbyvalue(IPOPT_UMP); }
1387 	| setsecclass secname
1388 			{ DOALL(fr->fr_mip.fi_secmsk |= $2;
1389 				if (!nowith)
1390 					fr->fr_ip.fi_secmsk |= $2;)
1391 			  $$ = 0;
1392 			  yyresetdict();
1393 			}
1394 	;
1395 
1396 setsecclass:
1397 	IPFY_SECCLASS	{ yysetdict(ipv4secwords); }
1398 	;
1399 
1400 ipv6hdr:
1401 	IPFY_AH			{ $$ = getv6optbyvalue(IPPROTO_AH); }
1402 	| IPFY_IPV6OPT_DSTOPTS	{ $$ = getv6optbyvalue(IPPROTO_DSTOPTS); }
1403 	| IPFY_ESP		{ $$ = getv6optbyvalue(IPPROTO_ESP); }
1404 	| IPFY_IPV6OPT_HOPOPTS	{ $$ = getv6optbyvalue(IPPROTO_HOPOPTS); }
1405 	| IPFY_IPV6OPT_IPV6	{ $$ = getv6optbyvalue(IPPROTO_IPV6); }
1406 	| IPFY_IPV6OPT_NONE	{ $$ = getv6optbyvalue(IPPROTO_NONE); }
1407 	| IPFY_IPV6OPT_ROUTING	{ $$ = getv6optbyvalue(IPPROTO_ROUTING); }
1408 	| IPFY_FRAG		{ $$ = getv6optbyvalue(IPPROTO_FRAGMENT); }
1409 	;
1410 
1411 level:	IPFY_LEVEL			{ setsyslog(); }
1412 	;
1413 
1414 loglevel:
1415 	priority			{ fr->fr_loglevel = LOG_LOCAL0|$1; }
1416 	| facility '.' priority		{ fr->fr_loglevel = $1 | $3; }
1417 	;
1418 
1419 facility:
1420 	IPFY_FAC_KERN			{ $$ = LOG_KERN; }
1421 	| IPFY_FAC_USER			{ $$ = LOG_USER; }
1422 	| IPFY_FAC_MAIL			{ $$ = LOG_MAIL; }
1423 	| IPFY_FAC_DAEMON		{ $$ = LOG_DAEMON; }
1424 	| IPFY_FAC_AUTH			{ $$ = LOG_AUTH; }
1425 	| IPFY_FAC_SYSLOG		{ $$ = LOG_SYSLOG; }
1426 	| IPFY_FAC_LPR			{ $$ = LOG_LPR; }
1427 	| IPFY_FAC_NEWS			{ $$ = LOG_NEWS; }
1428 	| IPFY_FAC_UUCP			{ $$ = LOG_UUCP; }
1429 	| IPFY_FAC_CRON			{ $$ = LOG_CRON; }
1430 	| IPFY_FAC_FTP			{ $$ = LOG_FTP; }
1431 	| IPFY_FAC_AUTHPRIV		{ $$ = LOG_AUTHPRIV; }
1432 	| IPFY_FAC_AUDIT		{ $$ = LOG_AUDIT; }
1433 	| IPFY_FAC_LFMT			{ $$ = LOG_LFMT; }
1434 	| IPFY_FAC_LOCAL0		{ $$ = LOG_LOCAL0; }
1435 	| IPFY_FAC_LOCAL1		{ $$ = LOG_LOCAL1; }
1436 	| IPFY_FAC_LOCAL2		{ $$ = LOG_LOCAL2; }
1437 	| IPFY_FAC_LOCAL3		{ $$ = LOG_LOCAL3; }
1438 	| IPFY_FAC_LOCAL4		{ $$ = LOG_LOCAL4; }
1439 	| IPFY_FAC_LOCAL5		{ $$ = LOG_LOCAL5; }
1440 	| IPFY_FAC_LOCAL6		{ $$ = LOG_LOCAL6; }
1441 	| IPFY_FAC_LOCAL7		{ $$ = LOG_LOCAL7; }
1442 	| IPFY_FAC_SECURITY		{ $$ = LOG_SECURITY; }
1443 	;
1444 
1445 priority:
1446 	IPFY_PRI_EMERG			{ $$ = LOG_EMERG; }
1447 	| IPFY_PRI_ALERT		{ $$ = LOG_ALERT; }
1448 	| IPFY_PRI_CRIT			{ $$ = LOG_CRIT; }
1449 	| IPFY_PRI_ERR			{ $$ = LOG_ERR; }
1450 	| IPFY_PRI_WARN			{ $$ = LOG_WARNING; }
1451 	| IPFY_PRI_NOTICE		{ $$ = LOG_NOTICE; }
1452 	| IPFY_PRI_INFO			{ $$ = LOG_INFO; }
1453 	| IPFY_PRI_DEBUG		{ $$ = LOG_DEBUG; }
1454 	;
1455 
1456 compare:
1457 	YY_CMP_EQ			{ $$ = FR_EQUAL; }
1458 	| YY_CMP_NE			{ $$ = FR_NEQUAL; }
1459 	| YY_CMP_LT			{ $$ = FR_LESST; }
1460 	| YY_CMP_LE			{ $$ = FR_LESSTE; }
1461 	| YY_CMP_GT			{ $$ = FR_GREATERT; }
1462 	| YY_CMP_GE			{ $$ = FR_GREATERTE; }
1463 	;
1464 
1465 range:	YY_RANGE_IN			{ $$ = FR_INRANGE; }
1466 	| YY_RANGE_OUT			{ $$ = FR_OUTRANGE; }
1467 	| ':'				{ $$ = FR_INCRANGE; }
1468 	;
1469 
1470 servicename:
1471 	YY_STR				{ $$ = $1; }
1472 	;
1473 
1474 interfacename:	YY_STR			{ $$ = $1; }
1475 	| YY_STR ':' YY_NUMBER
1476 		{ $$ = $1;
1477 		  fprintf(stderr, "%d: Logical interface %s:%d unsupported, "
1478 			  "use the physical interface %s instead.\n",
1479 			  yylineNum, $1, $3, $1);
1480 		}
1481 	;
1482 
1483 name:	YY_STR				{ $$ = $1; }
1484 	;
1485 
1486 ipv4_16:
1487 	YY_NUMBER '.' YY_NUMBER
1488 		{ if ($1 > 255 || $3 > 255) {
1489 			yyerror("Invalid octet string for IP address");
1490 			return 0;
1491 		  }
1492 		  $$.s_addr = ($1 << 24) | ($3 << 16);
1493 		  $$.s_addr = htonl($$.s_addr);
1494 		}
1495 	;
1496 
1497 ipv4_24:
1498 	ipv4_16 '.' YY_NUMBER
1499 		{ if ($3 > 255) {
1500 			yyerror("Invalid octet string for IP address");
1501 			return 0;
1502 		  }
1503 		  $$.s_addr |= htonl($3 << 8);
1504 		}
1505 	;
1506 
1507 ipv4:	ipv4_24 '.' YY_NUMBER
1508 		{ if ($3 > 255) {
1509 			yyerror("Invalid octet string for IP address");
1510 			return 0;
1511 		  }
1512 		  $$.s_addr |= htonl($3);
1513 		}
1514 	| ipv4_24
1515 	| ipv4_16
1516 	;
1517 
1518 %%
1519 
1520 
1521 static	struct	wordtab ipfwords[95] = {
1522 	{ "age",			IPFY_AGE },
1523 	{ "ah",				IPFY_AH },
1524 	{ "all",			IPFY_ALL },
1525 	{ "and",			IPFY_AND },
1526 	{ "auth",			IPFY_AUTH },
1527 	{ "bad",			IPFY_BAD },
1528 	{ "bad-nat",			IPFY_BADNAT },
1529 	{ "bad-src",			IPFY_BADSRC },
1530 	{ "bcast",			IPFY_BROADCAST },
1531 	{ "block",			IPFY_BLOCK },
1532 	{ "body",			IPFY_BODY },
1533 	{ "bpf-v4",			IPFY_BPFV4 },
1534 #ifdef USE_INET6
1535 	{ "bpf-v6",			IPFY_BPFV6 },
1536 #endif
1537 	{ "call",			IPFY_CALL },
1538 	{ "code",			IPFY_ICMPCODE },
1539 	{ "count",			IPFY_COUNT },
1540 	{ "dup-to",			IPFY_DUPTO },
1541 	{ "eq",				YY_CMP_EQ },
1542 	{ "esp",			IPFY_ESP },
1543 	{ "fastroute",			IPFY_FROUTE },
1544 	{ "first",			IPFY_FIRST },
1545 	{ "flags",			IPFY_FLAGS },
1546 	{ "frag",			IPFY_FRAG },
1547 	{ "frag-body",			IPFY_FRAGBODY },
1548 	{ "frags",			IPFY_FRAGS },
1549 	{ "from",			IPFY_FROM },
1550 	{ "ge",				YY_CMP_GE },
1551 	{ "group",			IPFY_GROUP },
1552 	{ "gt",				YY_CMP_GT },
1553 	{ "head",			IPFY_HEAD },
1554 	{ "icmp",			IPFY_ICMP },
1555 	{ "icmp-type",			IPFY_ICMPTYPE },
1556 	{ "in",				IPFY_IN },
1557 	{ "in-via",			IPFY_INVIA },
1558 	{ "ipopt",			IPFY_IPOPTS },
1559 	{ "ipopts",			IPFY_IPOPTS },
1560 	{ "keep",			IPFY_KEEP },
1561 	{ "le",				YY_CMP_LE },
1562 	{ "level",			IPFY_LEVEL },
1563 	{ "limit",			IPFY_LIMIT },
1564 	{ "log",			IPFY_LOG },
1565 	{ "lowttl",			IPFY_LOWTTL },
1566 	{ "lt",				YY_CMP_LT },
1567 	{ "mask",			IPFY_MASK },
1568 	{ "match-tag",			IPFY_MATCHTAG },
1569 	{ "mbcast",			IPFY_MBCAST },
1570 	{ "multicast",			IPFY_MULTICAST },
1571 	{ "nat",			IPFY_NAT },
1572 	{ "ne",				YY_CMP_NE },
1573 	{ "net",			IPFY_NETWORK },
1574 	{ "newisn",			IPFY_NEWISN },
1575 	{ "no",				IPFY_NO },
1576 	{ "no-icmp-err",		IPFY_NOICMPERR },
1577 	{ "now",			IPFY_NOW },
1578 	{ "not",			IPFY_NOT },
1579 	{ "oow",			IPFY_OOW },
1580 	{ "on",				IPFY_ON },
1581 	{ "opt",			IPFY_OPT },
1582 	{ "or-block",			IPFY_ORBLOCK },
1583 	{ "out",			IPFY_OUT },
1584 	{ "out-via",			IPFY_OUTVIA },
1585 	{ "pass",			IPFY_PASS },
1586 	{ "port",			IPFY_PORT },
1587 	{ "pps",			IPFY_PPS },
1588 	{ "preauth",			IPFY_PREAUTH },
1589 	{ "proto",			IPFY_PROTO },
1590 	{ "quick",			IPFY_QUICK },
1591 	{ "reply-to",			IPFY_REPLY_TO },
1592 	{ "return-icmp",		IPFY_RETICMP },
1593 	{ "return-icmp-as-dest",	IPFY_RETICMPASDST },
1594 	{ "return-rst",			IPFY_RETRST },
1595 	{ "route-to",			IPFY_ROUTETO },
1596 	{ "sec-class",			IPFY_SECCLASS },
1597 	{ "set-tag",			IPFY_SETTAG },
1598 	{ "skip",			IPFY_SKIP },
1599 	{ "short",			IPFY_SHORT },
1600 	{ "state",			IPFY_STATE },
1601 	{ "state-age",			IPFY_AGE },
1602 	{ "strict",			IPFY_STRICT },
1603 	{ "sync",			IPFY_SYNC },
1604 	{ "tcp",			IPFY_TCP },
1605 	{ "tcp-udp",			IPFY_TCPUDP },
1606 	{ "tos",			IPFY_TOS },
1607 	{ "to",				IPFY_TO },
1608 	{ "ttl",			IPFY_TTL },
1609 	{ "udp",			IPFY_UDP },
1610 	{ "v6hdrs",			IPF6_V6HDRS },
1611 	{ "with",			IPFY_WITH },
1612 	{ NULL,				0 }
1613 };
1614 
1615 static	struct	wordtab	addrwords[4] = {
1616 	{ "any",			IPFY_ANY },
1617 	{ "hash",			IPFY_HASH },
1618 	{ "pool",			IPFY_POOL },
1619 	{ NULL,				0 }
1620 };
1621 
1622 static	struct	wordtab	maskwords[5] = {
1623 	{ "broadcast",			IPFY_BROADCAST },
1624 	{ "netmasked",			IPFY_NETMASKED },
1625 	{ "network",			IPFY_NETWORK },
1626 	{ "peer",			IPFY_PEER },
1627 	{ NULL,				0 }
1628 };
1629 
1630 static	struct	wordtab icmptypewords[16] = {
1631 	{ "echo",			IPFY_ICMPT_ECHO },
1632 	{ "echorep",			IPFY_ICMPT_ECHOR },
1633 	{ "inforeq",			IPFY_ICMPT_INFOREQ },
1634 	{ "inforep",			IPFY_ICMPT_INFOREP },
1635 	{ "maskrep",			IPFY_ICMPT_MASKREP },
1636 	{ "maskreq",			IPFY_ICMPT_MASKREQ },
1637 	{ "paramprob",			IPFY_ICMPT_PARAMP },
1638 	{ "redir",			IPFY_ICMPT_REDIR },
1639 	{ "unreach",			IPFY_ICMPT_UNR },
1640 	{ "routerad",			IPFY_ICMPT_ROUTERAD },
1641 	{ "routersol",			IPFY_ICMPT_ROUTERSOL },
1642 	{ "squench",			IPFY_ICMPT_SQUENCH },
1643 	{ "timest",			IPFY_ICMPT_TIMEST },
1644 	{ "timestrep",			IPFY_ICMPT_TIMESTREP },
1645 	{ "timex",			IPFY_ICMPT_TIMEX },
1646 	{ NULL,				0 },
1647 };
1648 
1649 static	struct	wordtab icmpcodewords[17] = {
1650 	{ "cutoff-preced",		IPFY_ICMPC_CUTPRE },
1651 	{ "filter-prohib",		IPFY_ICMPC_FLTPRO },
1652 	{ "isolate",			IPFY_ICMPC_ISOLATE },
1653 	{ "needfrag",			IPFY_ICMPC_NEEDF },
1654 	{ "net-prohib",			IPFY_ICMPC_NETPRO },
1655 	{ "net-tos",			IPFY_ICMPC_NETTOS },
1656 	{ "host-preced",		IPFY_ICMPC_HSTPRE },
1657 	{ "host-prohib",		IPFY_ICMPC_HSTPRO },
1658 	{ "host-tos",			IPFY_ICMPC_HSTTOS },
1659 	{ "host-unk",			IPFY_ICMPC_HSTUNK },
1660 	{ "host-unr",			IPFY_ICMPC_HSTUNR },
1661 	{ "net-unk",			IPFY_ICMPC_NETUNK },
1662 	{ "net-unr",			IPFY_ICMPC_NETUNR },
1663 	{ "port-unr",			IPFY_ICMPC_PORUNR },
1664 	{ "proto-unr",			IPFY_ICMPC_PROUNR },
1665 	{ "srcfail",			IPFY_ICMPC_SRCFAIL },
1666 	{ NULL,				0 },
1667 };
1668 
1669 static	struct	wordtab ipv4optwords[25] = {
1670 	{ "addext",			IPFY_IPOPT_ADDEXT },
1671 	{ "cipso",			IPFY_IPOPT_CIPSO },
1672 	{ "dps",			IPFY_IPOPT_DPS },
1673 	{ "e-sec",			IPFY_IPOPT_ESEC },
1674 	{ "eip",			IPFY_IPOPT_EIP },
1675 	{ "encode",			IPFY_IPOPT_ENCODE },
1676 	{ "finn",			IPFY_IPOPT_FINN },
1677 	{ "imitd",			IPFY_IPOPT_IMITD },
1678 	{ "lsrr",			IPFY_IPOPT_LSRR },
1679 	{ "mtup",			IPFY_IPOPT_MTUP },
1680 	{ "mtur",			IPFY_IPOPT_MTUR },
1681 	{ "nop",			IPFY_IPOPT_NOP },
1682 	{ "nsapa",			IPFY_IPOPT_NSAPA },
1683 	{ "rr",				IPFY_IPOPT_RR },
1684 	{ "rtralrt",			IPFY_IPOPT_RTRALRT },
1685 	{ "satid",			IPFY_IPOPT_SATID },
1686 	{ "sdb",			IPFY_IPOPT_SDB },
1687 	{ "sec",			IPFY_IPOPT_SEC },
1688 	{ "ssrr",			IPFY_IPOPT_SSRR },
1689 	{ "tr",				IPFY_IPOPT_TR },
1690 	{ "ts",				IPFY_IPOPT_TS },
1691 	{ "ump",			IPFY_IPOPT_UMP },
1692 	{ "visa",			IPFY_IPOPT_VISA },
1693 	{ "zsu",			IPFY_IPOPT_ZSU },
1694 	{ NULL,				0 },
1695 };
1696 
1697 static	struct	wordtab ipv4secwords[9] = {
1698 	{ "confid",			IPFY_SEC_CONF },
1699 	{ "reserv-1",			IPFY_SEC_RSV1 },
1700 	{ "reserv-2",			IPFY_SEC_RSV2 },
1701 	{ "reserv-3",			IPFY_SEC_RSV3 },
1702 	{ "reserv-4",			IPFY_SEC_RSV4 },
1703 	{ "secret",			IPFY_SEC_SEC },
1704 	{ "topsecret",			IPFY_SEC_TS },
1705 	{ "unclass",			IPFY_SEC_UNC },
1706 	{ NULL,				0 },
1707 };
1708 
1709 static	struct	wordtab ipv6optwords[8] = {
1710 	{ "dstopts",			IPFY_IPV6OPT_DSTOPTS },
1711 	{ "esp",			IPFY_ESP },
1712 	{ "frag",			IPFY_FRAG },
1713 	{ "hopopts",			IPFY_IPV6OPT_HOPOPTS },
1714 	{ "ipv6",			IPFY_IPV6OPT_IPV6 },
1715 	{ "none",			IPFY_IPV6OPT_NONE },
1716 	{ "routing",			IPFY_IPV6OPT_ROUTING },
1717 	{ NULL,				0 },
1718 };
1719 
1720 static	struct	wordtab logwords[33] = {
1721 	{ "kern",			IPFY_FAC_KERN },
1722 	{ "user",			IPFY_FAC_USER },
1723 	{ "mail",			IPFY_FAC_MAIL },
1724 	{ "daemon",			IPFY_FAC_DAEMON },
1725 	{ "auth",			IPFY_FAC_AUTH },
1726 	{ "syslog",			IPFY_FAC_SYSLOG },
1727 	{ "lpr",			IPFY_FAC_LPR },
1728 	{ "news",			IPFY_FAC_NEWS },
1729 	{ "uucp",			IPFY_FAC_UUCP },
1730 	{ "cron",			IPFY_FAC_CRON },
1731 	{ "ftp",			IPFY_FAC_FTP },
1732 	{ "authpriv",			IPFY_FAC_AUTHPRIV },
1733 	{ "audit",			IPFY_FAC_AUDIT },
1734 	{ "logalert",			IPFY_FAC_LFMT },
1735 	{ "console",			IPFY_FAC_CONSOLE },
1736 	{ "security",			IPFY_FAC_SECURITY },
1737 	{ "local0",			IPFY_FAC_LOCAL0 },
1738 	{ "local1",			IPFY_FAC_LOCAL1 },
1739 	{ "local2",			IPFY_FAC_LOCAL2 },
1740 	{ "local3",			IPFY_FAC_LOCAL3 },
1741 	{ "local4",			IPFY_FAC_LOCAL4 },
1742 	{ "local5",			IPFY_FAC_LOCAL5 },
1743 	{ "local6",			IPFY_FAC_LOCAL6 },
1744 	{ "local7",			IPFY_FAC_LOCAL7 },
1745 	{ "emerg",			IPFY_PRI_EMERG },
1746 	{ "alert",			IPFY_PRI_ALERT },
1747 	{ "crit",			IPFY_PRI_CRIT },
1748 	{ "err",			IPFY_PRI_ERR },
1749 	{ "warn",			IPFY_PRI_WARN },
1750 	{ "notice",			IPFY_PRI_NOTICE },
1751 	{ "info",			IPFY_PRI_INFO },
1752 	{ "debug",			IPFY_PRI_DEBUG },
1753 	{ NULL,				0 },
1754 };
1755 
1756 
1757 
1758 
1759 int ipf_parsefile(fd, addfunc, iocfuncs, filename)
1760 int fd;
1761 addfunc_t addfunc;
1762 ioctlfunc_t *iocfuncs;
1763 char *filename;
1764 {
1765 	FILE *fp = NULL;
1766 	char *s;
1767 
1768 	yylineNum = 1;
1769 	yysettab(ipfwords);
1770 
1771 	s = getenv("YYDEBUG");
1772 	if (s != NULL)
1773 		yydebug = atoi(s);
1774 	else
1775 		yydebug = 0;
1776 
1777 	if (strcmp(filename, "-")) {
1778 		fp = fopen(filename, "r");
1779 		if (fp == NULL) {
1780 			fprintf(stderr, "fopen(%s) failed: %s\n", filename,
1781 				STRERROR(errno));
1782 			return -1;
1783 		}
1784 	} else
1785 		fp = stdin;
1786 
1787 	while (ipf_parsesome(fd, addfunc, iocfuncs, fp) == 1)
1788 		;
1789 	if (fp != NULL)
1790 		fclose(fp);
1791 	return 0;
1792 }
1793 
1794 
1795 int ipf_parsesome(fd, addfunc, iocfuncs, fp)
1796 int fd;
1797 addfunc_t addfunc;
1798 ioctlfunc_t *iocfuncs;
1799 FILE *fp;
1800 {
1801 	char *s;
1802 	int i;
1803 
1804 	ipffd = fd;
1805 	for (i = 0; i <= IPL_LOGMAX; i++)
1806 		ipfioctl[i] = iocfuncs[i];
1807 	ipfaddfunc = addfunc;
1808 
1809 	if (feof(fp))
1810 		return 0;
1811 	i = fgetc(fp);
1812 	if (i == EOF)
1813 		return 0;
1814 	if (ungetc(i, fp) == 0)
1815 		return 0;
1816 	if (feof(fp))
1817 		return 0;
1818 	s = getenv("YYDEBUG");
1819 	if (s != NULL)
1820 		yydebug = atoi(s);
1821 	else
1822 		yydebug = 0;
1823 
1824 	yyin = fp;
1825 	yyparse();
1826 	return 1;
1827 }
1828 
1829 
1830 static void newrule()
1831 {
1832 	frentry_t *frn;
1833 
1834 	frn = (frentry_t *)calloc(1, sizeof(frentry_t));
1835 	if (frn == NULL)
1836 		yyerror("sorry, out of memory");
1837 	for (fr = frtop; fr != NULL && fr->fr_next != NULL; fr = fr->fr_next)
1838 		;
1839 	if (fr != NULL)
1840 		fr->fr_next = frn;
1841 	if (frtop == NULL)
1842 		frtop = frn;
1843 	fr = frn;
1844 	frc = frn;
1845 	fr->fr_loglevel = 0xffff;
1846 	fr->fr_isc = (void *)-1;
1847 	fr->fr_logtag = FR_NOLOGTAG;
1848 	fr->fr_type = FR_T_NONE;
1849 	if (use_inet6 != 0)
1850 		fr->fr_v = 6;
1851 	else
1852 		fr->fr_v = 4;
1853 
1854 	nrules = 1;
1855 }
1856 
1857 
1858 static void setipftype()
1859 {
1860 	for (fr = frc; fr != NULL; fr = fr->fr_next) {
1861 		if (fr->fr_type == FR_T_NONE) {
1862 			fr->fr_type = FR_T_IPF;
1863 			fr->fr_data = (void *)calloc(sizeof(fripf_t), 1);
1864 			if (fr->fr_data == NULL)
1865 				yyerror("sorry, out of memory");
1866 			fr->fr_dsize = sizeof(fripf_t);
1867 			fr->fr_ip.fi_v = frc->fr_v;
1868 			fr->fr_mip.fi_v = 0xf;
1869 			fr->fr_ipf->fri_sifpidx = -1;
1870 			fr->fr_ipf->fri_difpidx = -1;
1871 		}
1872 		if (fr->fr_type != FR_T_IPF) {
1873 			fprintf(stderr, "IPF Type not set\n");
1874 		}
1875 	}
1876 }
1877 
1878 
1879 static frentry_t *addrule()
1880 {
1881 	frentry_t *f, *f1, *f2;
1882 	int count;
1883 
1884 	for (f2 = frc; f2->fr_next != NULL; f2 = f2->fr_next)
1885 		;
1886 
1887 	count = nrules;
1888 	if (count == 0) {
1889 		f = (frentry_t *)calloc(sizeof(*f), 1);
1890 		if (f == NULL)
1891 			yyerror("sorry, out of memory");
1892 		added++;
1893 		f2->fr_next = f;
1894 		bcopy(f2, f, sizeof(*f));
1895 		if (f2->fr_caddr != NULL) {
1896 			f->fr_caddr = malloc(f->fr_dsize);
1897 			if (f->fr_caddr == NULL)
1898 				yyerror("sorry, out of memory");
1899 			bcopy(f2->fr_caddr, f->fr_caddr, f->fr_dsize);
1900 		}
1901 		f->fr_next = NULL;
1902 		return f;
1903 	}
1904 	f = f2;
1905 	for (f1 = frc; count > 0; count--, f1 = f1->fr_next) {
1906 		f->fr_next = (frentry_t *)calloc(sizeof(*f), 1);
1907 		if (f->fr_next == NULL)
1908 			yyerror("sorry, out of memory");
1909 		added++;
1910 		f = f->fr_next;
1911 		bcopy(f1, f, sizeof(*f));
1912 		f->fr_next = NULL;
1913 		if (f->fr_caddr != NULL) {
1914 			f->fr_caddr = malloc(f->fr_dsize);
1915 			if (f->fr_caddr == NULL)
1916 				yyerror("sorry, out of memory");
1917 			bcopy(f1->fr_caddr, f->fr_caddr, f->fr_dsize);
1918 		}
1919 	}
1920 
1921 	return f2->fr_next;
1922 }
1923 
1924 
1925 static u_32_t lookuphost(name)
1926 char *name;
1927 {
1928 	u_32_t addr;
1929 	int i;
1930 
1931 	hashed = 0;
1932 	pooled = 0;
1933 	dynamic = -1;
1934 
1935 	for (i = 0; i < 4; i++) {
1936 		if (strncmp(name, frc->fr_ifnames[i],
1937 			    sizeof(frc->fr_ifnames[i])) == 0) {
1938 			ifpflag = FRI_DYNAMIC;
1939 			dynamic = i;
1940 			return 0;
1941 		}
1942 	}
1943 
1944 	if (gethost(name, &addr) == -1) {
1945 		fprintf(stderr, "unknown name \"%s\"\n", name);
1946 		return 0;
1947 	}
1948 	return addr;
1949 }
1950 
1951 
1952 static void dobpf(v, phrase)
1953 int v;
1954 char *phrase;
1955 {
1956 #ifdef IPFILTER_BPF
1957 	struct bpf_program bpf;
1958 	struct pcap *p;
1959 #endif
1960 	fakebpf_t *fb;
1961 	u_32_t l;
1962 	char *s;
1963 	int i;
1964 
1965 	for (fr = frc; fr != NULL; fr = fr->fr_next) {
1966 		if (fr->fr_type != FR_T_NONE) {
1967 			fprintf(stderr, "cannot mix IPF and BPF matching\n");
1968 			return;
1969 		}
1970 		fr->fr_v = v;
1971 		fr->fr_type = FR_T_BPFOPC;
1972 
1973 		if (!strncmp(phrase, "\"0x", 2)) {
1974 			phrase++;
1975 			fb = malloc(sizeof(fakebpf_t));
1976 			if (fb == NULL)
1977 				yyerror("sorry, out of memory");
1978 
1979 			for (i = 0, s = strtok(phrase, " \r\n\t"); s != NULL;
1980 			     s = strtok(NULL, " \r\n\t"), i++) {
1981 				fb = realloc(fb, (i / 4 + 1) * sizeof(*fb));
1982 				if (fb == NULL)
1983 					yyerror("sorry, out of memory");
1984 				l = (u_32_t)strtol(s, NULL, 0);
1985 				switch (i & 3)
1986 				{
1987 				case 0 :
1988 					fb[i / 4].fb_c = l & 0xffff;
1989 					break;
1990 				case 1 :
1991 					fb[i / 4].fb_t = l & 0xff;
1992 					break;
1993 				case 2 :
1994 					fb[i / 4].fb_f = l & 0xff;
1995 					break;
1996 				case 3 :
1997 					fb[i / 4].fb_k = l;
1998 					break;
1999 				}
2000 			}
2001 			if ((i & 3) != 0) {
2002 				fprintf(stderr,
2003 					"Odd number of bytes in BPF code\n");
2004 				exit(1);
2005 			}
2006 			i--;
2007 			fr->fr_dsize = (i / 4 + 1) * sizeof(*fb);
2008 			fr->fr_data = fb;
2009 			return;
2010 		}
2011 
2012 #ifdef IPFILTER_BPF
2013 		bzero((char *)&bpf, sizeof(bpf));
2014 		p = pcap_open_dead(DLT_RAW, 1);
2015 		if (!p) {
2016 			fprintf(stderr, "pcap_open_dead failed\n");
2017 			return;
2018 		}
2019 
2020 		if (pcap_compile(p, &bpf, phrase, 1, 0xffffffff)) {
2021 			pcap_perror(p, "ipf");
2022 			pcap_close(p);
2023 			fprintf(stderr, "pcap parsing failed (%s)\n", phrase);
2024 			return;
2025 		}
2026 		pcap_close(p);
2027 
2028 		fr->fr_dsize = bpf.bf_len * sizeof(struct bpf_insn);
2029 		fr->fr_data = malloc(fr->fr_dsize);
2030 		if (fr->fr_data == NULL)
2031 			yyerror("sorry, out of memory");
2032 		bcopy((char *)bpf.bf_insns, fr->fr_data, fr->fr_dsize);
2033 		if (!bpf_validate(fr->fr_data, bpf.bf_len)) {
2034 			fprintf(stderr, "BPF validation failed\n");
2035 			return;
2036 		}
2037 #endif
2038 	}
2039 
2040 #ifdef IPFILTER_BPF
2041 	if (opts & OPT_DEBUG)
2042 		bpf_dump(&bpf, 0);
2043 #else
2044 	fprintf(stderr, "BPF filter expressions not supported\n");
2045 	exit(1);
2046 #endif
2047 }
2048 
2049 
2050 static void resetaddr()
2051 {
2052 	hashed = 0;
2053 	pooled = 0;
2054 	dynamic = -1;
2055 }
2056 
2057 
2058 static alist_t *newalist(ptr)
2059 alist_t *ptr;
2060 {
2061 	alist_t *al;
2062 
2063 	al = malloc(sizeof(*al));
2064 	if (al == NULL)
2065 		return NULL;
2066 	al->al_not = 0;
2067 	al->al_next = ptr;
2068 	return al;
2069 }
2070 
2071 
2072 static int makepool(list)
2073 alist_t *list;
2074 {
2075 	ip_pool_node_t *n, *top;
2076 	ip_pool_t pool;
2077 	alist_t *a;
2078 	int num;
2079 
2080 	if (list == NULL)
2081 		return 0;
2082 	top = calloc(1, sizeof(*top));
2083 	if (top == NULL)
2084 		return 0;
2085 
2086 	for (n = top, a = list; (n != NULL) && (a != NULL); a = a->al_next) {
2087 		n->ipn_addr.adf_family = a->al_family;
2088 		n->ipn_mask.adf_family = a->al_family;
2089 		(void *)bcopy((void *)&a->al_i6addr,
2090 			      (void *)&n->ipn_addr.adf_addr,
2091 			      sizeof(n->ipn_addr.adf_addr));
2092 		(void *)bcopy((void *)&a->al_i6mask,
2093 			      (void *)&n->ipn_mask.adf_addr,
2094 			      sizeof(n->ipn_mask.adf_addr));
2095 		n->ipn_info = a->al_not;
2096 		if (a->al_next != NULL) {
2097 			n->ipn_next = calloc(1, sizeof(*n));
2098 			if (n->ipn_next == NULL)
2099 				yyerror("sorry, out of memory");
2100 			n = n->ipn_next;
2101 		}
2102 	}
2103 
2104 	bzero((char *)&pool, sizeof(pool));
2105 	pool.ipo_unit = IPL_LOGIPF;
2106 	pool.ipo_list = top;
2107 	num = load_pool(&pool, ipfioctl[IPL_LOGLOOKUP]);
2108 
2109 	while ((n = top) != NULL) {
2110 		top = n->ipn_next;
2111 		free(n);
2112 	}
2113 	return num;
2114 }
2115 
2116 
2117 static u_int makehash(list)
2118 alist_t *list;
2119 {
2120 	iphtent_t *n, *top;
2121 	iphtable_t iph;
2122 	alist_t *a;
2123 	int num;
2124 
2125 	if (list == NULL)
2126 		return 0;
2127 	top = calloc(1, sizeof(*top));
2128 	if (top == NULL)
2129 		return 0;
2130 
2131 	for (n = top, a = list; (n != NULL) && (a != NULL); a = a->al_next) {
2132 		n->ipe_family = a->al_family;
2133 		(void *)bcopy((void *)&a->al_i6addr,
2134 			      (void *)&n->ipe_addr,
2135 			      sizeof(n->ipe_addr));
2136 		(void *)bcopy((void *)&a->al_i6mask,
2137 			      (void *)&n->ipe_mask,
2138 			      sizeof(n->ipe_mask));
2139 		n->ipe_value = 0;
2140 		if (a->al_next != NULL) {
2141 			n->ipe_next = calloc(1, sizeof(*n));
2142 			if (n->ipe_next == NULL)
2143 				yyerror("sorry, out of memory");
2144 			n = n->ipe_next;
2145 		}
2146 	}
2147 
2148 	bzero((char *)&iph, sizeof(iph));
2149 	iph.iph_unit = IPL_LOGIPF;
2150 	iph.iph_type = IPHASH_LOOKUP;
2151 	*iph.iph_name = '\0';
2152 
2153 	if (load_hash(&iph, top, ipfioctl[IPL_LOGLOOKUP]) == 0)
2154 		sscanf(iph.iph_name, "%u", &num);
2155 	else
2156 		num = 0;
2157 
2158 	while ((n = top) != NULL) {
2159 		top = n->ipe_next;
2160 		free(n);
2161 	}
2162 	return num;
2163 }
2164 
2165 
2166 void ipf_addrule(fd, ioctlfunc, ptr)
2167 int fd;
2168 ioctlfunc_t ioctlfunc;
2169 void *ptr;
2170 {
2171 	ioctlcmd_t add, del;
2172 	frentry_t *fr;
2173 	ipfobj_t obj;
2174 
2175 	fr = ptr;
2176 	add = 0;
2177 	del = 0;
2178 
2179 	bzero((char *)&obj, sizeof(obj));
2180 	obj.ipfo_rev = IPFILTER_VERSION;
2181 	obj.ipfo_size = sizeof(*fr);
2182 	obj.ipfo_type = IPFOBJ_FRENTRY;
2183 	obj.ipfo_ptr = ptr;
2184 
2185 	if ((opts & OPT_DONOTHING) != 0)
2186 		fd = -1;
2187 
2188 	if (opts & OPT_ZERORULEST) {
2189 		add = SIOCZRLST;
2190 	} else if (opts & OPT_INACTIVE) {
2191 		add = (u_int)fr->fr_hits ? SIOCINIFR :
2192 					   SIOCADIFR;
2193 		del = SIOCRMIFR;
2194 	} else {
2195 		add = (u_int)fr->fr_hits ? SIOCINAFR :
2196 					   SIOCADAFR;
2197 		del = SIOCRMAFR;
2198 	}
2199 
2200 	if (fr && (opts & OPT_OUTQUE))
2201 		fr->fr_flags |= FR_OUTQUE;
2202 	if (fr->fr_hits)
2203 		fr->fr_hits--;
2204 	if (fr && (opts & OPT_VERBOSE))
2205 		printfr(fr, ioctlfunc);
2206 
2207 	if (opts & OPT_DEBUG) {
2208 		binprint(fr, sizeof(*fr));
2209 		if (fr->fr_data != NULL)
2210 			binprint(fr->fr_data, fr->fr_dsize);
2211 	}
2212 
2213 	if ((opts & OPT_ZERORULEST) != 0) {
2214 		if ((*ioctlfunc)(fd, add, (void *)&obj) == -1) {
2215 			if ((opts & OPT_DONOTHING) == 0) {
2216 				fprintf(stderr, "%d:", yylineNum);
2217 				perror("ioctl(SIOCZRLST)");
2218 			}
2219 		} else {
2220 #ifdef	USE_QUAD_T
2221 			printf("hits %qd bytes %qd ",
2222 				(long long)fr->fr_hits,
2223 				(long long)fr->fr_bytes);
2224 #else
2225 			printf("hits %ld bytes %ld ",
2226 				fr->fr_hits, fr->fr_bytes);
2227 #endif
2228 			printfr(fr, ioctlfunc);
2229 		}
2230 	} else if ((opts & OPT_REMOVE) != 0) {
2231 		if ((*ioctlfunc)(fd, del, (void *)&obj) == -1) {
2232 			if ((opts & OPT_DONOTHING) != 0) {
2233 				fprintf(stderr, "%d:", yylineNum);
2234 				perror("ioctl(delete rule)");
2235 			}
2236 		}
2237 	} else {
2238 		if ((*ioctlfunc)(fd, add, (void *)&obj) == -1) {
2239 			if (!(opts & OPT_DONOTHING)) {
2240 				fprintf(stderr, "%d:", yylineNum);
2241 				perror("ioctl(add/insert rule)");
2242 			}
2243 		}
2244 	}
2245 }
2246 
2247 static void setsyslog()
2248 {
2249 	yysetdict(logwords);
2250 	yybreakondot = 1;
2251 }
2252 
2253 
2254 static void unsetsyslog()
2255 {
2256 	yyresetdict();
2257 	yybreakondot = 0;
2258 }
2259 
2260 
2261 static void fillgroup(fr)
2262 frentry_t *fr;
2263 {
2264 	frentry_t *f;
2265 
2266 	for (f = frold; f != NULL; f = f->fr_next)
2267 		if (strncmp(f->fr_grhead, fr->fr_group, FR_GROUPLEN) == 0)
2268 			break;
2269 	if (f == NULL)
2270 		return;
2271 
2272 	/*
2273 	 * Only copy down matching fields if the rules are of the same type
2274 	 * and are of ipf type.   The only fields that are copied are those
2275 	 * that impact the rule parsing itself, eg. need for knowing what the
2276 	 * protocol should be for rules with port comparisons in them.
2277 	 */
2278 	if (f->fr_type != fr->fr_type || f->fr_type != FR_T_IPF)
2279 		return;
2280 
2281 	if (fr->fr_v == 0 && f->fr_v != 0)
2282 		fr->fr_v = f->fr_v;
2283 
2284 	if (fr->fr_mproto == 0 && f->fr_mproto != 0)
2285 		fr->fr_mproto = f->fr_mproto;
2286 	if (fr->fr_proto == 0 && f->fr_proto != 0)
2287 		fr->fr_proto = f->fr_proto;
2288 
2289 	if ((fr->fr_mproto == 0) && ((fr->fr_flx & FI_TCPUDP) == 0) &&
2290 	    ((f->fr_flx & FI_TCPUDP) != 0))
2291 		fr->fr_flx |= FI_TCPUDP;
2292 }
2293