2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7 * Use is subject to license terms.
22 static char *portcmp
[] = { "*", "==", "!=", "<", ">", "<=", ">=", "**", "***" };
25 int intcmp
__P((const void *, const void *));
26 static void indent
__P((FILE *, int));
27 static void printeq
__P((FILE *, char *, int, int, int));
28 static void printipeq
__P((FILE *, char *, int, int, int));
29 static void addrule
__P((FILE *, frentry_t
*));
30 static void printhooks
__P((FILE *, int, int, frgroup_t
*));
31 static void emitheader
__P((frgroup_t
*, u_int
, u_int
));
32 static void emitGroup
__P((int, int, void *, frentry_t
*, char *,
34 static void emittail
__P((void));
35 static void printCgroup
__P((int, frentry_t
*, mc_t
*, char *));
56 static FILE *cfile
= NULL
;
59 * This is called once per filter rule being loaded to emit data structures
73 if ((fr
->fr_type
!= FR_T_IPF
) && (fr
->fr_type
!= FR_T_NONE
))
75 if ((fr
->fr_type
== FR_T_IPF
) &&
76 ((fr
->fr_datype
!= FRI_NORMAL
) || (fr
->fr_satype
!= FRI_NORMAL
)))
81 cfile
= fopen("ip_rules.c", "w");
87 fprintf(fp
, "* Copyright (C) 1993-2000 by Darren Reed.\n");
89 fprintf(fp
, "* Redistribution and use in source and binary forms are permitted\n");
90 fprintf(fp
, "* provided that this notice is preserved and due credit is given\n");
91 fprintf(fp
, "* to the original author and the contributors.\n");
92 fprintf(fp
, "*/\n\n");
94 fprintf(fp
, "#include <sys/types.h>\n");
95 fprintf(fp
, "#include <sys/time.h>\n");
96 fprintf(fp
, "#include <sys/socket.h>\n");
97 fprintf(fp
, "#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__sgi)\n");
98 fprintf(fp
, "# include <sys/systm.h>\n");
99 fprintf(fp
, "#endif\n");
100 fprintf(fp
, "#include <sys/errno.h>\n");
101 fprintf(fp
, "#include <sys/param.h>\n");
103 "#if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux)\n");
104 fprintf(fp
, "# include <sys/mbuf.h>\n");
105 fprintf(fp
, "#endif\n");
107 "#if defined(__FreeBSD__) && (__FreeBSD_version > 220000)\n");
108 fprintf(fp
, "# include <sys/sockio.h>\n");
109 fprintf(fp
, "#else\n");
110 fprintf(fp
, "# include <sys/ioctl.h>\n");
111 fprintf(fp
, "#endif /* FreeBSD */\n");
112 fprintf(fp
, "#include <net/if.h>\n");
113 fprintf(fp
, "#include <netinet/in.h>\n");
114 fprintf(fp
, "#include <netinet/in_systm.h>\n");
115 fprintf(fp
, "#include <netinet/ip.h>\n");
116 fprintf(fp
, "#include <netinet/tcp.h>\n");
117 fprintf(fp
, "#include \"netinet/ip_compat.h\"\n");
118 fprintf(fp
, "#include \"netinet/ip_fil.h\"\n\n");
119 fprintf(fp
, "#include \"netinet/ip_rules.h\"\n\n");
120 fprintf(fp
, "#ifndef _KERNEL\n");
121 fprintf(fp
, "# include <string.h>\n");
122 fprintf(fp
, "#endif /* _KERNEL */\n");
124 fprintf(fp
, "#ifdef IPFILTER_COMPILED\n");
128 fr
->fr_type
|= FR_T_BUILTIN
;
132 if (i
& -(1 - sizeof(*ulp
)))
134 for (i
/= sizeof(u_long
), ulp
= (u_long
*)fr
; i
> 0; i
--) {
135 fprintf(fp
, "%s%#lx", and, *ulp
++);
138 fprintf(fp
, "\n};\n");
139 fr
->fr_type
&= ~FR_T_BUILTIN
;
147 static frgroup_t
*groups
= NULL
;
150 static void addrule(fp
, fr
)
160 f
= (frentry_t
*)malloc(sizeof(*f
));
162 fprintf(stderr
, "out of memory\n");
165 bcopy((char *)fr
, (char *)f
, sizeof(*fr
));
167 f
->fr_ipf
= (fripf_t
*)malloc(sizeof(*f
->fr_ipf
));
168 if (f
->fr_ipf
== NULL
) {
169 fprintf(stderr
, "out of memory\n");
172 bcopy((char *)fr
->fr_ipf
, (char *)f
->fr_ipf
,
173 sizeof(*fr
->fr_ipf
));
177 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
)
178 if ((strncmp(g
->fg_name
, f
->fr_group
, FR_GROUPLEN
) == 0) &&
179 (g
->fg_flags
== (f
->fr_flags
& FR_INOUT
)))
183 g
= (frgroup_t
*)calloc(1, sizeof(*g
));
185 fprintf(stderr
, "out of memory\n");
191 bcopy(f
->fr_group
, g
->fg_name
, FR_GROUPLEN
);
193 g
->fg_flags
= f
->fr_flags
& FR_INOUT
;
196 for (fpp
= &g
->fg_start
; *fpp
!= NULL
; )
197 fpp
= &((*fpp
)->fr_next
);
200 if (fr
->fr_dsize
> 0) {
202 static u_long ipf%s_rule_data_%s_%u[] = {\n",
203 f
->fr_flags
& FR_INQUE
? "in" : "out",
204 g
->fg_name
, g
->fg_ref
);
208 for (i
/= sizeof(u_long
); i
> 0; i
--) {
209 fprintf(fp
, "%s%#lx", and, *ulp
++);
212 fprintf(fp
, "\n};\n");
215 fprintf(fp
, "\nstatic u_long %s_rule_%s_%d[] = {\n",
216 f
->fr_flags
& FR_INQUE
? "in" : "out", g
->fg_name
, g
->fg_ref
);
220 if (f
->fr_grhead
!= 0) {
221 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
)
222 if ((strncmp(g
->fg_name
, f
->fr_grhead
,
223 FR_GROUPLEN
) == 0) &&
224 g
->fg_flags
== (f
->fr_flags
& FR_INOUT
))
227 g
= (frgroup_t
*)calloc(1, sizeof(*g
));
229 fprintf(stderr
, "out of memory\n");
235 bcopy(f
->fr_grhead
, g
->fg_name
, FR_GROUPLEN
);
237 g
->fg_flags
= f
->fr_flags
& FR_INOUT
;
246 const mc_t
*i1
= (const mc_t
*)c1
, *i2
= (const mc_t
*)c2
;
248 if (i1
->n
== i2
->n
) {
249 return i1
->c
- i2
->c
;
251 return i2
->n
- i1
->n
;
255 static void indent(fp
, in
)
263 static void printeq(fp
, var
, m
, max
, v
)
269 fprintf(fp
, "%s == %#x) {\n", var
, v
);
271 fprintf(fp
, "(%s & %#x) == %#x) {\n", var
, m
, v
);
275 * Parameters: var - IP# being compared
276 * fl - 0 for positive match, 1 for negative match
278 * v - required address
280 static void printipeq(fp
, var
, fl
, m
, v
)
286 fprintf(fp
, "%s ", var
);
288 fprintf(fp
, "(%s & %#x) ", var
, m
);
289 fprintf(fp
, "%c", fl
? '!' : '=');
290 fprintf(fp
, "= %#x) {\n", v
);
294 void emit(num
, dir
, v
, fr
)
303 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
) {
304 if (dir
== 0 || dir
== -1) {
305 if ((g
->fg_flags
& FR_INQUE
) == 0)
307 for (incnt
= 0, f
= g
->fg_start
; f
!= NULL
;
310 emitGroup(num
, dir
, v
, fr
, g
->fg_name
, incnt
, 0);
312 if (dir
== 1 || dir
== -1) {
313 if ((g
->fg_flags
& FR_OUTQUE
) == 0)
315 for (outcnt
= 0, f
= g
->fg_start
; f
!= NULL
;
318 emitGroup(num
, dir
, v
, fr
, g
->fg_name
, 0, outcnt
);
322 if (num
== -1 && dir
== -1) {
323 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
) {
324 if ((g
->fg_flags
& FR_INQUE
) != 0) {
325 for (incnt
= 0, f
= g
->fg_start
; f
!= NULL
;
329 emitheader(g
, incnt
, 0);
331 if ((g
->fg_flags
& FR_OUTQUE
) != 0) {
332 for (outcnt
= 0, f
= g
->fg_start
; f
!= NULL
;
336 emitheader(g
, 0, outcnt
);
340 fprintf(cfile
, "#endif /* IPFILTER_COMPILED */\n");
346 static void emitheader(grp
, incount
, outcount
)
348 u_int incount
, outcount
;
350 static FILE *fph
= NULL
;
354 fph
= fopen("ip_rules.h", "w");
358 fprintf(fph
, "extern int ipfrule_add __P((void));\n");
359 fprintf(fph
, "extern int ipfrule_remove __P((void));\n");
362 printhooks(cfile
, incount
, outcount
, grp
);
366 extern frentry_t *ipfrule_match_in_%s __P((fr_info_t *, u_32_t *));\n\
367 extern frentry_t *ipf_rules_in_%s[%d];\n",
368 grp
->fg_name
, grp
->fg_name
, incount
);
370 for (g
= groups
; g
!= grp
; g
= g
->fg_next
)
371 if ((strncmp(g
->fg_name
, grp
->fg_name
,
372 FR_GROUPLEN
) == 0) &&
373 g
->fg_flags
== grp
->fg_flags
)
377 extern int ipfrule_add_in_%s __P((void));\n\
378 extern int ipfrule_remove_in_%s __P((void));\n", grp
->fg_name
, grp
->fg_name
);
383 extern frentry_t *ipfrule_match_out_%s __P((fr_info_t *, u_32_t *));\n\
384 extern frentry_t *ipf_rules_out_%s[%d];\n",
385 grp
->fg_name
, grp
->fg_name
, outcount
);
387 for (g
= groups
; g
!= grp
; g
= g
->fg_next
)
388 if ((strncmp(g
->fg_name
, grp
->fg_name
,
389 FR_GROUPLEN
) == 0) &&
390 g
->fg_flags
== grp
->fg_flags
)
394 extern int ipfrule_add_out_%s __P((void));\n\
395 extern int ipfrule_remove_out_%s __P((void));\n",
396 grp
->fg_name
, grp
->fg_name
);
401 static void emittail()
410 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
)
412 err = ipfrule_add_%s_%s();\n\
415 (g
->fg_flags
& FR_INQUE
) ? "in" : "out", g
->fg_name
);
422 int ipfrule_remove()\n\
426 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
)
428 err = ipfrule_remove_%s_%s();\n\
431 (g
->fg_flags
& FR_INQUE
) ? "in" : "out", g
->fg_name
);
434 fprintf(cfile
, "}\n");
438 static void emitGroup(num
, dir
, v
, fr
, group
, incount
, outcount
)
443 u_int incount
, outcount
;
445 static FILE *fp
= NULL
;
446 static int header
[2] = { 0, 0 };
447 static char egroup
[FR_GROUPLEN
] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
448 static int openfunc
= 0;
449 static mc_t
*n
= NULL
;
461 if (strncmp(egroup
, group
, FR_GROUPLEN
)) {
462 for (sin
--; sin
> 0; sin
--) {
467 fprintf(fp
, "\treturn fr;\n}\n");
477 strncpy(egroup
, group
, FR_GROUPLEN
);
478 } else if (openfunc
== 1 && num
< 0) {
483 for (sin
--; sin
> 0; sin
--) {
488 fprintf(fp
, "\treturn fr;\n}\n");
496 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
) {
497 if (dir
== 0 && (g
->fg_flags
& FR_INQUE
) == 0)
499 else if (dir
== 1 && (g
->fg_flags
& FR_OUTQUE
) == 0)
501 if (strncmp(g
->fg_name
, group
, FR_GROUPLEN
) != 0)
507 * Output the array of pointers to rules for this group.
509 if (num
== -2 && dir
== 0 && header
[0] == 0 && incount
!= 0) {
510 fprintf(fp
, "\nfrentry_t *ipf_rules_in_%s[%d] = {",
512 for (f
= g
->fg_start
, i
= 0; f
!= NULL
; f
= f
->fr_next
) {
513 if ((f
->fr_flags
& FR_INQUE
) == 0)
519 "(frentry_t *)&in_rule_%s_%d",
525 fprintf(fp
, "\n};\n");
528 if (num
== -2 && dir
== 1 && header
[1] == 0 && outcount
!= 0) {
529 fprintf(fp
, "\nfrentry_t *ipf_rules_out_%s[%d] = {",
531 for (f
= g
->fg_start
, i
= 0; f
!= NULL
; f
= f
->fr_next
) {
532 if ((f
->fr_flags
& FR_OUTQUE
) == 0)
538 "(frentry_t *)&out_rule_%s_%d",
540 if (i
+ 1 < outcount
)
544 fprintf(fp
, "\n};\n");
555 * If the function header has not been printed then print it now.
557 if (header
[dir
] == 0) {
558 int pdst
= 0, psrc
= 0;
561 fprintf(fp
, "\nfrentry_t *ipfrule_match_%s_%s(fin, passp)\n",
562 (dir
== 0) ? "in" : "out", group
);
563 fprintf(fp
, "fr_info_t *fin;\n");
564 fprintf(fp
, "u_32_t *passp;\n");
566 fprintf(fp
, "\tfrentry_t *fr = NULL;\n");
569 * Print out any variables that need to be declared.
571 for (f
= g
->fg_start
, i
= 0; f
!= NULL
; f
= f
->fr_next
) {
572 if (incount
+ outcount
> m
[FRC_SRC
].e
+ 1)
574 if (incount
+ outcount
> m
[FRC_DST
].e
+ 1)
578 fprintf(fp
, "\tu_32_t src = ntohl(%s);\n",
579 "fin->fin_fi.fi_saddr");
581 fprintf(fp
, "\tu_32_t dst = ntohl(%s);\n",
582 "fin->fin_fi.fi_daddr");
585 for (i
= 0; i
< FRC_MAX
; i
++) {
593 if (ipf
!= NULL
&& ipf
->fri_mip
.fi_v
!= 0)
597 if (ipf
!= NULL
&& ipf
->fri_mip
.fi_flx
!= 0)
601 if (ipf
!= NULL
&& ipf
->fri_mip
.fi_p
!= 0)
605 if (ipf
!= NULL
&& ipf
->fri_mip
.fi_ttl
!= 0)
609 if (ipf
!= NULL
&& ipf
->fri_mip
.fi_tos
!= 0)
615 if ((ipf
->fri_ip
.fi_p
== IPPROTO_TCP
) &&
622 if (fr
->fr_scmp
== FR_INRANGE
)
624 else if (fr
->fr_scmp
== FR_OUTRANGE
)
626 else if (fr
->fr_scmp
!= 0)
632 if (fr
->fr_dcmp
== FR_INRANGE
)
634 else if (fr
->fr_dcmp
== FR_OUTRANGE
)
636 else if (fr
->fr_dcmp
!= 0)
642 if (fr
->fr_satype
== FRI_LOOKUP
) {
644 } else if ((fr
->fr_smask
!= 0) ||
645 (fr
->fr_flags
& FR_NOTSRCIP
) != 0)
651 if (fr
->fr_datype
== FRI_LOOKUP
) {
653 } else if ((fr
->fr_dmask
!= 0) ||
654 (fr
->fr_flags
& FR_NOTDSTIP
) != 0)
660 if (fr
->fr_optmask
!= 0)
666 if (fr
->fr_secmask
!= 0)
672 if (fr
->fr_authmask
!= 0)
678 if ((fr
->fr_icmpm
& 0xff00) != 0)
684 if ((fr
->fr_icmpm
& 0xff) != 0)
696 qsort(m
, FRC_MAX
, sizeof(mc_t
), intcmp
);
700 * Calculate the indentation interval upto the last common
701 * common comparison being made.
703 for (i
= 0, in
= 1; i
< FRC_MAX
; i
++) {
704 if (n
[i
].c
!= m
[i
].c
)
706 if (n
[i
].s
!= m
[i
].s
)
709 if (n
[i
].n
&& (n
[i
].n
> n
[i
].e
)) {
721 for (j
= sin
- 1; j
>= in
; j
--) {
732 * print out C code that implements a filter rule.
734 for (; i
< FRC_MAX
; i
++) {
740 fprintf(fp
, "if (fin->fin_ifp == ");
741 fprintf(fp
, "ipf_rules_%s_%s[%d]->fr_ifa) {\n",
742 dir
? "out" : "in", group
, num
);
749 fprintf(fp
, "if (fin->fin_v == %d) {\n",
758 printeq(fp
, "fin->fin_flx",
759 ipf
->fri_mip
.fi_flx
, 0xf,
767 fprintf(fp
, "if (fin->fin_p == %d) {\n",
776 printeq(fp
, "fin->fin_ttl",
777 ipf
->fri_mip
.fi_ttl
, 0xff,
785 fprintf(fp
, "if (fin->fin_tos");
786 printeq(fp
, "fin->fin_tos",
787 ipf
->fri_mip
.fi_tos
, 0xff,
796 printeq(fp
, "fin->fin_tcpf", fr
->fr_tcpfm
,
804 if (fr
->fr_scmp
== FR_INRANGE
) {
806 fprintf(fp
, "if ((fin->fin_data[0] > %d) && ",
808 fprintf(fp
, "(fin->fin_data[0] < %d)",
810 fprintf(fp
, ") {\n");
812 } else if (fr
->fr_scmp
== FR_OUTRANGE
) {
814 fprintf(fp
, "if ((fin->fin_data[0] < %d) || ",
816 fprintf(fp
, "(fin->fin_data[0] > %d)",
818 fprintf(fp
, ") {\n");
820 } else if (fr
->fr_scmp
) {
822 fprintf(fp
, "if (fin->fin_data[0] %s %d)",
823 portcmp
[fr
->fr_scmp
], fr
->fr_sport
);
831 if (fr
->fr_dcmp
== FR_INRANGE
) {
833 fprintf(fp
, "if ((fin->fin_data[1] > %d) && ",
835 fprintf(fp
, "(fin->fin_data[1] < %d)",
837 fprintf(fp
, ") {\n");
839 } else if (fr
->fr_dcmp
== FR_OUTRANGE
) {
841 fprintf(fp
, "if ((fin->fin_data[1] < %d) || ",
843 fprintf(fp
, "(fin->fin_data[1] > %d)",
845 fprintf(fp
, ") {\n");
847 } else if (fr
->fr_dcmp
) {
849 fprintf(fp
, "if (fin->fin_data[1] %s %d)",
850 portcmp
[fr
->fr_dcmp
], fr
->fr_dport
);
858 if (fr
->fr_satype
== FRI_LOOKUP
) {
860 } else if ((fr
->fr_smask
!= 0) ||
861 (fr
->fr_flags
& FR_NOTSRCIP
) != 0) {
865 fr
->fr_flags
& FR_NOTSRCIP
,
866 fr
->fr_smask
, fr
->fr_saddr
);
873 if (fr
->fr_datype
== FRI_LOOKUP
) {
875 } else if ((fr
->fr_dmask
!= 0) ||
876 (fr
->fr_flags
& FR_NOTDSTIP
) != 0) {
880 fr
->fr_flags
& FR_NOTDSTIP
,
881 fr
->fr_dmask
, fr
->fr_daddr
);
889 printeq(fp
, "fin->fin_fi.fi_optmsk",
890 fr
->fr_optmask
, 0xffffffff,
899 printeq(fp
, "fin->fin_fi.fi_secmsk",
900 fr
->fr_secmask
, 0xffff,
909 printeq(fp
, "fin->fin_fi.fi_authmsk",
910 fr
->fr_authmask
, 0xffff,
919 printeq(fp
, "fin->fin_data[0]",
920 fr
->fr_icmpm
& 0xff00, 0xffff,
921 fr
->fr_icmp
& 0xff00);
929 printeq(fp
, "fin->fin_data[0]",
930 fr
->fr_icmpm
& 0xff, 0xffff,
940 if (fr
->fr_flags
& FR_QUICK
) {
941 fprintf(fp
, "return (frentry_t *)&%s_rule_%s_%d;\n",
942 fr
->fr_flags
& FR_INQUE
? "in" : "out",
945 fprintf(fp
, "fr = (frentry_t *)&%s_rule_%s_%d;\n",
946 fr
->fr_flags
& FR_INQUE
? "in" : "out",
950 n
= (mc_t
*)malloc(sizeof(*n
) * FRC_MAX
);
952 fprintf(stderr
, "out of memory\n");
956 bcopy((char *)m
, (char *)n
, sizeof(*n
) * FRC_MAX
);
964 static mc_t
*m
= NULL
;
968 m
= (mc_t
*)calloc(1, sizeof(*m
) * FRC_MAX
);
970 fprintf(stderr
, "out of memory\n");
975 for (g
= groups
; g
!= NULL
; g
= g
->fg_next
) {
976 if ((dir
== 0) && ((g
->fg_flags
& FR_INQUE
) != 0))
977 printCgroup(dir
, g
->fg_start
, m
, g
->fg_name
);
978 if ((dir
== 1) && ((g
->fg_flags
& FR_OUTQUE
) != 0))
979 printCgroup(dir
, g
->fg_start
, m
, g
->fg_name
);
982 emit(-1, dir
, m
, NULL
);
987 * Now print out code to implement all of the rules.
989 static void printCgroup(dir
, top
, m
, group
)
999 for (count
= 0, fr1
= top
; fr1
!= NULL
; fr1
= fr1
->fr_next
) {
1000 if ((dir
== 0) && ((fr1
->fr_flags
& FR_INQUE
) != 0))
1002 else if ((dir
== 1) && ((fr1
->fr_flags
& FR_OUTQUE
) != 0))
1007 emitGroup(-2, dir
, m
, fr1
, group
, count
, 0);
1009 emitGroup(-2, dir
, m
, fr1
, group
, 0, count
);
1012 * Before printing each rule, check to see how many of its fields are
1013 * matched by subsequent rules.
1015 for (fr1
= top
, rn
= 0; fr1
!= NULL
; fr1
= fr1
->fr_next
, rn
++) {
1016 if (!dir
&& !(fr1
->fr_flags
& FR_INQUE
))
1018 if (dir
&& !(fr1
->fr_flags
& FR_OUTQUE
))
1022 for (i
= 0; i
< FRC_MAX
; i
++)
1024 qsort(m
, FRC_MAX
, sizeof(mc_t
), intcmp
);
1026 for (i
= 0; i
< FRC_MAX
; i
++) {
1033 for (fr
= fr1
->fr_next
; fr
; fr
= fr
->fr_next
) {
1034 if (!dir
&& !(fr
->fr_flags
& FR_INQUE
))
1036 if (dir
&& !(fr
->fr_flags
& FR_OUTQUE
))
1040 !strcmp(fr1
->fr_ifname
, fr
->fr_ifname
)) {
1046 if ((n
& 0x0002) && (fr1
->fr_v
== fr
->fr_v
)) {
1053 (fr
->fr_type
== fr1
->fr_type
) &&
1054 (fr
->fr_type
== FR_T_IPF
) &&
1055 (fr1
->fr_mip
.fi_flx
== fr
->fr_mip
.fi_flx
) &&
1056 (fr1
->fr_ip
.fi_flx
== fr
->fr_ip
.fi_flx
)) {
1063 (fr
->fr_type
== fr1
->fr_type
) &&
1064 (fr
->fr_type
== FR_T_IPF
) &&
1065 (fr1
->fr_proto
== fr
->fr_proto
)) {
1072 (fr
->fr_type
== fr1
->fr_type
) &&
1073 (fr
->fr_type
== FR_T_IPF
) &&
1074 (fr1
->fr_ttl
== fr
->fr_ttl
)) {
1081 (fr
->fr_type
== fr1
->fr_type
) &&
1082 (fr
->fr_type
== FR_T_IPF
) &&
1083 (fr1
->fr_tos
== fr
->fr_tos
)) {
1090 (fr
->fr_type
== fr1
->fr_type
) &&
1091 (fr
->fr_type
== FR_T_IPF
) &&
1092 ((fr1
->fr_tcpfm
== fr
->fr_tcpfm
) &&
1093 (fr1
->fr_tcpf
== fr
->fr_tcpf
))) {
1100 (fr
->fr_type
== fr1
->fr_type
) &&
1101 (fr
->fr_type
== FR_T_IPF
) &&
1102 ((fr1
->fr_scmp
== fr
->fr_scmp
) &&
1103 (fr1
->fr_stop
== fr
->fr_stop
) &&
1104 (fr1
->fr_sport
== fr
->fr_sport
))) {
1111 (fr
->fr_type
== fr1
->fr_type
) &&
1112 (fr
->fr_type
== FR_T_IPF
) &&
1113 ((fr1
->fr_dcmp
== fr
->fr_dcmp
) &&
1114 (fr1
->fr_dtop
== fr
->fr_dtop
) &&
1115 (fr1
->fr_dport
== fr
->fr_dport
))) {
1122 (fr
->fr_type
== fr1
->fr_type
) &&
1123 (fr
->fr_type
== FR_T_IPF
) &&
1124 ((fr1
->fr_satype
== FRI_LOOKUP
) &&
1125 (fr
->fr_satype
== FRI_LOOKUP
) &&
1126 (fr1
->fr_srcnum
== fr
->fr_srcnum
))) {
1129 } else if ((n
& 0x0200) &&
1130 (fr
->fr_type
== fr1
->fr_type
) &&
1131 (fr
->fr_type
== FR_T_IPF
) &&
1132 (((fr1
->fr_flags
& FR_NOTSRCIP
) ==
1133 (fr
->fr_flags
& FR_NOTSRCIP
)))) {
1134 if ((fr1
->fr_smask
== fr
->fr_smask
) &&
1135 (fr1
->fr_saddr
== fr
->fr_saddr
))
1139 if (fr1
->fr_smask
&&
1140 (fr1
->fr_saddr
& fr1
->fr_smask
) ==
1141 (fr
->fr_saddr
& fr1
->fr_smask
)) {
1150 (fr
->fr_type
== fr1
->fr_type
) &&
1151 (fr
->fr_type
== FR_T_IPF
) &&
1152 ((fr1
->fr_datype
== FRI_LOOKUP
) &&
1153 (fr
->fr_datype
== FRI_LOOKUP
) &&
1154 (fr1
->fr_dstnum
== fr
->fr_dstnum
))) {
1157 } else if ((n
& 0x0400) &&
1158 (fr
->fr_type
== fr1
->fr_type
) &&
1159 (fr
->fr_type
== FR_T_IPF
) &&
1160 (((fr1
->fr_flags
& FR_NOTDSTIP
) ==
1161 (fr
->fr_flags
& FR_NOTDSTIP
)))) {
1162 if ((fr1
->fr_dmask
== fr
->fr_dmask
) &&
1163 (fr1
->fr_daddr
== fr
->fr_daddr
))
1167 if (fr1
->fr_dmask
&&
1168 (fr1
->fr_daddr
& fr1
->fr_dmask
) ==
1169 (fr
->fr_daddr
& fr1
->fr_dmask
)) {
1178 (fr
->fr_type
== fr1
->fr_type
) &&
1179 (fr
->fr_type
== FR_T_IPF
) &&
1180 (fr1
->fr_optmask
== fr
->fr_optmask
) &&
1181 (fr1
->fr_optbits
== fr
->fr_optbits
)) {
1188 (fr
->fr_type
== fr1
->fr_type
) &&
1189 (fr
->fr_type
== FR_T_IPF
) &&
1190 (fr1
->fr_secmask
== fr
->fr_secmask
) &&
1191 (fr1
->fr_secbits
== fr
->fr_secbits
)) {
1197 if ((n
& 0x10000) &&
1198 (fr
->fr_type
== fr1
->fr_type
) &&
1199 (fr
->fr_type
== FR_T_IPF
) &&
1200 (fr1
->fr_authmask
== fr
->fr_authmask
) &&
1201 (fr1
->fr_authbits
== fr
->fr_authbits
)) {
1207 if ((n
& 0x20000) &&
1208 (fr
->fr_type
== fr1
->fr_type
) &&
1209 (fr
->fr_type
== FR_T_IPF
) &&
1210 ((fr1
->fr_icmpm
& 0xff00) ==
1211 (fr
->fr_icmpm
& 0xff00)) &&
1212 ((fr1
->fr_icmp
& 0xff00) ==
1213 (fr
->fr_icmp
& 0xff00))) {
1219 if ((n
& 0x40000) &&
1220 (fr
->fr_type
== fr1
->fr_type
) &&
1221 (fr
->fr_type
== FR_T_IPF
) &&
1222 ((fr1
->fr_icmpm
& 0xff) == (fr
->fr_icmpm
& 0xff)) &&
1223 ((fr1
->fr_icmp
& 0xff) == (fr
->fr_icmp
& 0xff))) {
1232 emitGroup(rn
, dir
, m
, fr1
, group
, count
, 0);
1234 emitGroup(rn
, dir
, m
, fr1
, group
, 0, count
);
1238 static void printhooks(fp
, in
, out
, grp
)
1249 group
= grp
->fg_name
;
1254 "printhooks called with both in and out set\n");
1265 fprintf(fp
, "static frentry_t ipfrule_%s_%s;\n", instr
, group
);
1269 int ipfrule_add_%s_%s()\n", instr
, group
);
1272 int i, j, err = 0, max;\n\
1281 for (i
= 0, fr
= grp
->fg_start
; fr
!= NULL
; i
++, fr
= fr
->fr_next
)
1282 if (fr
->fr_dsize
> 0) {
1284 ipf_rules_%s_%s[%d]->fr_data = &ipf%s_rule_data_%s_%u;\n",
1285 instr
, grp
->fg_name
, i
,
1286 instr
, grp
->fg_name
, i
);
1289 max = sizeof(ipf_rules_%s_%s)/sizeof(frentry_t *);\n\
1290 for (i = 0; i < max; i++) {\n\
1291 fp = ipf_rules_%s_%s[i];\n\
1292 fp->fr_next = NULL;\n", instr
, group
, instr
, group
);
1295 for (j = i + 1; j < max; j++)\n\
1296 if (strncmp(fp->fr_group,\n\
1297 ipf_rules_%s_%s[j]->fr_group,\n\
1298 FR_GROUPLEN) == 0) {\n\
1299 fp->fr_next = ipf_rules_%s_%s[j];\n\
1301 }\n", instr
, group
, instr
, group
);
1305 if (fp->fr_grhead != 0) {\n\
1306 fg = fr_addgroup(fp->fr_grhead, fp, FR_INQUE,\n\
1309 fp->fr_grp = &fg->fg_start;\n\
1314 fp = &ipfrule_%s_%s;\n", instr
, group
);
1316 bzero((char *)fp, sizeof(*fp));\n\
1317 fp->fr_type = FR_T_CALLFUNC|FR_T_BUILTIN;\n\
1318 fp->fr_flags = FR_%sQUE|FR_NOMATCH;\n\
1319 fp->fr_data = (void *)ipf_rules_%s_%s[0];\n",
1320 (in
!= 0) ? "IN" : "OUT", instr
, group
);
1322 fp->fr_dsize = sizeof(ipf_rules_%s_%s[0]);\n",
1327 fp->fr_func = (ipfunc_t)ipfrule_match_%s_%s;\n\
1328 err = frrequest(IPL_LOGIPF, SIOCADDFR, (caddr_t)fp, fr_active, 0);\n",
1330 fprintf(fp
, "\treturn err;\n}\n");
1333 int ipfrule_remove_%s_%s()\n", instr
, group
);
1340 * Try to remove the %sbound rule.\n", instr
);
1344 if (ipfrule_%s_%s.fr_ref > 0) {\n", instr
, group
);
1351 i = sizeof(ipf_rules_%s_%s)/sizeof(frentry_t *) - 1;\n\
1352 for (; i >= 0; i--) {\n\
1353 fp = ipf_rules_%s_%s[i];\n\
1354 if (fp->fr_ref > 1) {\n\
1361 err = frrequest(IPL_LOGIPF, SIOCDELFR,\n\
1362 (caddr_t)&ipfrule_%s_%s, fr_active, 0);\n",
1363 instr
, group
, instr
, group
, instr
, group
);
1369 fprintf(fp
, "\treturn err;\n}\n");