10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (C) 1993-2001 by Darren Reed.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
5*2934Sjojemann *
6*2934Sjojemann * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
7*2934Sjojemann * Use is subject to license terms.
80Sstevel@tonic-gate */
9*2934Sjojemann
10*2934Sjojemann #pragma ident "%Z%%M% %I% %E% SMI"
11*2934Sjojemann
120Sstevel@tonic-gate #if !defined(lint)
130Sstevel@tonic-gate static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
142393Syz155240 static const char rcsid[] = "@(#)$Id: ipfcomp.c,v 1.24.2.2 2004/04/28 10:34:44 darrenr Exp $";
150Sstevel@tonic-gate #endif
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include "ipf.h"
180Sstevel@tonic-gate
190Sstevel@tonic-gate
200Sstevel@tonic-gate typedef struct {
210Sstevel@tonic-gate int c;
220Sstevel@tonic-gate int e;
230Sstevel@tonic-gate int n;
240Sstevel@tonic-gate int p;
250Sstevel@tonic-gate int s;
260Sstevel@tonic-gate } mc_t;
270Sstevel@tonic-gate
280Sstevel@tonic-gate
290Sstevel@tonic-gate static char *portcmp[] = { "*", "==", "!=", "<", ">", "<=", ">=", "**", "***" };
300Sstevel@tonic-gate static int count = 0;
310Sstevel@tonic-gate
320Sstevel@tonic-gate int intcmp __P((const void *, const void *));
330Sstevel@tonic-gate static void indent __P((FILE *, int));
340Sstevel@tonic-gate static void printeq __P((FILE *, char *, int, int, int));
350Sstevel@tonic-gate static void printipeq __P((FILE *, char *, int, int, int));
360Sstevel@tonic-gate static void addrule __P((FILE *, frentry_t *));
370Sstevel@tonic-gate static void printhooks __P((FILE *, int, int, frgroup_t *));
380Sstevel@tonic-gate static void emitheader __P((frgroup_t *, u_int, u_int));
390Sstevel@tonic-gate static void emitGroup __P((int, int, void *, frentry_t *, char *,
400Sstevel@tonic-gate u_int, u_int));
410Sstevel@tonic-gate static void emittail __P((void));
420Sstevel@tonic-gate static void printCgroup __P((int, frentry_t *, mc_t *, char *));
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define FRC_IFN 0
450Sstevel@tonic-gate #define FRC_V 1
460Sstevel@tonic-gate #define FRC_P 2
470Sstevel@tonic-gate #define FRC_FL 3
480Sstevel@tonic-gate #define FRC_TOS 4
490Sstevel@tonic-gate #define FRC_TTL 5
500Sstevel@tonic-gate #define FRC_SRC 6
510Sstevel@tonic-gate #define FRC_DST 7
520Sstevel@tonic-gate #define FRC_TCP 8
530Sstevel@tonic-gate #define FRC_SP 9
540Sstevel@tonic-gate #define FRC_DP 10
550Sstevel@tonic-gate #define FRC_OPT 11
560Sstevel@tonic-gate #define FRC_SEC 12
570Sstevel@tonic-gate #define FRC_ATH 13
580Sstevel@tonic-gate #define FRC_ICT 14
590Sstevel@tonic-gate #define FRC_ICC 15
600Sstevel@tonic-gate #define FRC_MAX 16
610Sstevel@tonic-gate
620Sstevel@tonic-gate
630Sstevel@tonic-gate static FILE *cfile = NULL;
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * This is called once per filter rule being loaded to emit data structures
670Sstevel@tonic-gate * required.
680Sstevel@tonic-gate */
printc(fr)690Sstevel@tonic-gate void printc(fr)
700Sstevel@tonic-gate frentry_t *fr;
710Sstevel@tonic-gate {
720Sstevel@tonic-gate fripf_t *ipf;
730Sstevel@tonic-gate u_long *ulp;
740Sstevel@tonic-gate char *and;
750Sstevel@tonic-gate FILE *fp;
760Sstevel@tonic-gate int i;
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (fr->fr_v != 4)
790Sstevel@tonic-gate return;
800Sstevel@tonic-gate if ((fr->fr_type != FR_T_IPF) && (fr->fr_type != FR_T_NONE))
810Sstevel@tonic-gate return;
820Sstevel@tonic-gate if ((fr->fr_type == FR_T_IPF) &&
830Sstevel@tonic-gate ((fr->fr_datype != FRI_NORMAL) || (fr->fr_satype != FRI_NORMAL)))
840Sstevel@tonic-gate return;
850Sstevel@tonic-gate ipf = fr->fr_ipf;
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (cfile == NULL)
880Sstevel@tonic-gate cfile = fopen("ip_rules.c", "w");
890Sstevel@tonic-gate if (cfile == NULL)
900Sstevel@tonic-gate return;
910Sstevel@tonic-gate fp = cfile;
920Sstevel@tonic-gate if (count == 0) {
930Sstevel@tonic-gate fprintf(fp, "/*\n");
940Sstevel@tonic-gate fprintf(fp, "* Copyright (C) 1993-2000 by Darren Reed.\n");
950Sstevel@tonic-gate fprintf(fp, "*\n");
960Sstevel@tonic-gate fprintf(fp, "* Redistribution and use in source and binary forms are permitted\n");
970Sstevel@tonic-gate fprintf(fp, "* provided that this notice is preserved and due credit is given\n");
980Sstevel@tonic-gate fprintf(fp, "* to the original author and the contributors.\n");
990Sstevel@tonic-gate fprintf(fp, "*/\n\n");
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate fprintf(fp, "#include <sys/types.h>\n");
1020Sstevel@tonic-gate fprintf(fp, "#include <sys/time.h>\n");
1030Sstevel@tonic-gate fprintf(fp, "#include <sys/socket.h>\n");
1042393Syz155240 fprintf(fp, "#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__sgi)\n");
1052393Syz155240 fprintf(fp, "# include <sys/systm.h>\n");
1062393Syz155240 fprintf(fp, "#endif\n");
1070Sstevel@tonic-gate fprintf(fp, "#include <sys/errno.h>\n");
1080Sstevel@tonic-gate fprintf(fp, "#include <sys/param.h>\n");
1090Sstevel@tonic-gate fprintf(fp,
1100Sstevel@tonic-gate "#if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux)\n");
1110Sstevel@tonic-gate fprintf(fp, "# include <sys/mbuf.h>\n");
1120Sstevel@tonic-gate fprintf(fp, "#endif\n");
1130Sstevel@tonic-gate fprintf(fp,
1140Sstevel@tonic-gate "#if defined(__FreeBSD__) && (__FreeBSD_version > 220000)\n");
1150Sstevel@tonic-gate fprintf(fp, "# include <sys/sockio.h>\n");
1160Sstevel@tonic-gate fprintf(fp, "#else\n");
1170Sstevel@tonic-gate fprintf(fp, "# include <sys/ioctl.h>\n");
1180Sstevel@tonic-gate fprintf(fp, "#endif /* FreeBSD */\n");
1190Sstevel@tonic-gate fprintf(fp, "#include <net/if.h>\n");
1200Sstevel@tonic-gate fprintf(fp, "#include <netinet/in.h>\n");
1210Sstevel@tonic-gate fprintf(fp, "#include <netinet/in_systm.h>\n");
1220Sstevel@tonic-gate fprintf(fp, "#include <netinet/ip.h>\n");
1230Sstevel@tonic-gate fprintf(fp, "#include <netinet/tcp.h>\n");
1242393Syz155240 fprintf(fp, "#include \"netinet/ip_compat.h\"\n");
1252393Syz155240 fprintf(fp, "#include \"netinet/ip_fil.h\"\n\n");
1262393Syz155240 fprintf(fp, "#include \"netinet/ip_rules.h\"\n\n");
1272393Syz155240 fprintf(fp, "#ifndef _KERNEL\n");
1282393Syz155240 fprintf(fp, "# include <string.h>\n");
1292393Syz155240 fprintf(fp, "#endif /* _KERNEL */\n");
1302393Syz155240 fprintf(fp, "\n");
1312393Syz155240 fprintf(fp, "#ifdef IPFILTER_COMPILED\n");
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate addrule(fp, fr);
1350Sstevel@tonic-gate fr->fr_type |= FR_T_BUILTIN;
1360Sstevel@tonic-gate and = "";
1370Sstevel@tonic-gate fr->fr_ref = 1;
1380Sstevel@tonic-gate i = sizeof(*fr);
1390Sstevel@tonic-gate if (i & -(1 - sizeof(*ulp)))
1400Sstevel@tonic-gate i += sizeof(u_long);
1410Sstevel@tonic-gate for (i /= sizeof(u_long), ulp = (u_long *)fr; i > 0; i--) {
1420Sstevel@tonic-gate fprintf(fp, "%s%#lx", and, *ulp++);
1430Sstevel@tonic-gate and = ", ";
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate fprintf(fp, "\n};\n");
1460Sstevel@tonic-gate fr->fr_type &= ~FR_T_BUILTIN;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate count++;
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate fflush(fp);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate static frgroup_t *groups = NULL;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate
addrule(fp,fr)1570Sstevel@tonic-gate static void addrule(fp, fr)
1580Sstevel@tonic-gate FILE *fp;
1590Sstevel@tonic-gate frentry_t *fr;
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate frentry_t *f, **fpp;
1620Sstevel@tonic-gate frgroup_t *g;
1630Sstevel@tonic-gate u_long *ulp;
1640Sstevel@tonic-gate char *and;
1650Sstevel@tonic-gate int i;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate f = (frentry_t *)malloc(sizeof(*f));
168*2934Sjojemann if (f == NULL) {
169*2934Sjojemann fprintf(stderr, "out of memory\n");
170*2934Sjojemann exit(1);
171*2934Sjojemann }
1720Sstevel@tonic-gate bcopy((char *)fr, (char *)f, sizeof(*fr));
1730Sstevel@tonic-gate if (fr->fr_ipf) {
1740Sstevel@tonic-gate f->fr_ipf = (fripf_t *)malloc(sizeof(*f->fr_ipf));
175*2934Sjojemann if (f->fr_ipf == NULL) {
176*2934Sjojemann fprintf(stderr, "out of memory\n");
177*2934Sjojemann exit(1);
178*2934Sjojemann }
1790Sstevel@tonic-gate bcopy((char *)fr->fr_ipf, (char *)f->fr_ipf,
1800Sstevel@tonic-gate sizeof(*fr->fr_ipf));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate f->fr_next = NULL;
1840Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next)
1850Sstevel@tonic-gate if ((strncmp(g->fg_name, f->fr_group, FR_GROUPLEN) == 0) &&
1860Sstevel@tonic-gate (g->fg_flags == (f->fr_flags & FR_INOUT)))
1870Sstevel@tonic-gate break;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate if (g == NULL) {
1900Sstevel@tonic-gate g = (frgroup_t *)calloc(1, sizeof(*g));
191*2934Sjojemann if (g == NULL) {
192*2934Sjojemann fprintf(stderr, "out of memory\n");
193*2934Sjojemann exit(1);
194*2934Sjojemann }
1950Sstevel@tonic-gate g->fg_next = groups;
1960Sstevel@tonic-gate groups = g;
1970Sstevel@tonic-gate g->fg_head = f;
1980Sstevel@tonic-gate bcopy(f->fr_group, g->fg_name, FR_GROUPLEN);
1990Sstevel@tonic-gate g->fg_ref = 0;
2000Sstevel@tonic-gate g->fg_flags = f->fr_flags & FR_INOUT;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate for (fpp = &g->fg_start; *fpp != NULL; )
2040Sstevel@tonic-gate fpp = &((*fpp)->fr_next);
2050Sstevel@tonic-gate *fpp = f;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (fr->fr_dsize > 0) {
2080Sstevel@tonic-gate fprintf(fp, "\
2090Sstevel@tonic-gate static u_long ipf%s_rule_data_%s_%u[] = {\n",
2100Sstevel@tonic-gate f->fr_flags & FR_INQUE ? "in" : "out",
2110Sstevel@tonic-gate g->fg_name, g->fg_ref);
2120Sstevel@tonic-gate and = "";
2130Sstevel@tonic-gate i = fr->fr_dsize;
2140Sstevel@tonic-gate ulp = fr->fr_data;
2150Sstevel@tonic-gate for (i /= sizeof(u_long); i > 0; i--) {
2160Sstevel@tonic-gate fprintf(fp, "%s%#lx", and, *ulp++);
2170Sstevel@tonic-gate and = ", ";
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate fprintf(fp, "\n};\n");
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate fprintf(fp, "\nstatic u_long %s_rule_%s_%d[] = {\n",
2230Sstevel@tonic-gate f->fr_flags & FR_INQUE ? "in" : "out", g->fg_name, g->fg_ref);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate g->fg_ref++;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (f->fr_grhead != 0) {
2280Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next)
2290Sstevel@tonic-gate if ((strncmp(g->fg_name, f->fr_grhead,
2300Sstevel@tonic-gate FR_GROUPLEN) == 0) &&
2310Sstevel@tonic-gate g->fg_flags == (f->fr_flags & FR_INOUT))
2320Sstevel@tonic-gate break;
2330Sstevel@tonic-gate if (g == NULL) {
2340Sstevel@tonic-gate g = (frgroup_t *)calloc(1, sizeof(*g));
235*2934Sjojemann if (g == NULL) {
236*2934Sjojemann fprintf(stderr, "out of memory\n");
237*2934Sjojemann exit(1);
238*2934Sjojemann }
2390Sstevel@tonic-gate g->fg_next = groups;
2400Sstevel@tonic-gate groups = g;
2410Sstevel@tonic-gate g->fg_head = f;
2420Sstevel@tonic-gate bcopy(f->fr_grhead, g->fg_name, FR_GROUPLEN);
2430Sstevel@tonic-gate g->fg_ref = 0;
2440Sstevel@tonic-gate g->fg_flags = f->fr_flags & FR_INOUT;
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate
intcmp(c1,c2)2500Sstevel@tonic-gate int intcmp(c1, c2)
2510Sstevel@tonic-gate const void *c1, *c2;
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate const mc_t *i1 = (const mc_t *)c1, *i2 = (const mc_t *)c2;
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (i1->n == i2->n) {
2560Sstevel@tonic-gate return i1->c - i2->c;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate return i2->n - i1->n;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate
indent(fp,in)2620Sstevel@tonic-gate static void indent(fp, in)
2630Sstevel@tonic-gate FILE *fp;
2640Sstevel@tonic-gate int in;
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate for (; in; in--)
2670Sstevel@tonic-gate fputc('\t', fp);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
printeq(fp,var,m,max,v)2700Sstevel@tonic-gate static void printeq(fp, var, m, max, v)
2710Sstevel@tonic-gate FILE *fp;
2720Sstevel@tonic-gate char *var;
2730Sstevel@tonic-gate int m, max, v;
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate if (m == max)
2760Sstevel@tonic-gate fprintf(fp, "%s == %#x) {\n", var, v);
2770Sstevel@tonic-gate else
2780Sstevel@tonic-gate fprintf(fp, "(%s & %#x) == %#x) {\n", var, m, v);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate * Parameters: var - IP# being compared
2830Sstevel@tonic-gate * fl - 0 for positive match, 1 for negative match
2840Sstevel@tonic-gate * m - netmask
2850Sstevel@tonic-gate * v - required address
2860Sstevel@tonic-gate */
printipeq(fp,var,fl,m,v)2870Sstevel@tonic-gate static void printipeq(fp, var, fl, m, v)
2880Sstevel@tonic-gate FILE *fp;
2890Sstevel@tonic-gate char *var;
2900Sstevel@tonic-gate int fl, m, v;
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate if (m == 0xffffffff)
2930Sstevel@tonic-gate fprintf(fp, "%s ", var);
2940Sstevel@tonic-gate else
2950Sstevel@tonic-gate fprintf(fp, "(%s & %#x) ", var, m);
2960Sstevel@tonic-gate fprintf(fp, "%c", fl ? '!' : '=');
2970Sstevel@tonic-gate fprintf(fp, "= %#x) {\n", v);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate
emit(num,dir,v,fr)3010Sstevel@tonic-gate void emit(num, dir, v, fr)
3020Sstevel@tonic-gate int num, dir;
3030Sstevel@tonic-gate void *v;
3040Sstevel@tonic-gate frentry_t *fr;
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate u_int incnt, outcnt;
3070Sstevel@tonic-gate frgroup_t *g;
3080Sstevel@tonic-gate frentry_t *f;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next) {
3110Sstevel@tonic-gate if (dir == 0 || dir == -1) {
3120Sstevel@tonic-gate if ((g->fg_flags & FR_INQUE) == 0)
3130Sstevel@tonic-gate continue;
3140Sstevel@tonic-gate for (incnt = 0, f = g->fg_start; f != NULL;
3150Sstevel@tonic-gate f = f->fr_next)
3160Sstevel@tonic-gate incnt++;
3170Sstevel@tonic-gate emitGroup(num, dir, v, fr, g->fg_name, incnt, 0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate if (dir == 1 || dir == -1) {
3200Sstevel@tonic-gate if ((g->fg_flags & FR_OUTQUE) == 0)
3210Sstevel@tonic-gate continue;
3220Sstevel@tonic-gate for (outcnt = 0, f = g->fg_start; f != NULL;
3230Sstevel@tonic-gate f = f->fr_next)
3240Sstevel@tonic-gate outcnt++;
3250Sstevel@tonic-gate emitGroup(num, dir, v, fr, g->fg_name, 0, outcnt);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate if (num == -1 && dir == -1) {
3300Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next) {
3310Sstevel@tonic-gate if ((g->fg_flags & FR_INQUE) != 0) {
3320Sstevel@tonic-gate for (incnt = 0, f = g->fg_start; f != NULL;
3330Sstevel@tonic-gate f = f->fr_next)
3340Sstevel@tonic-gate incnt++;
3350Sstevel@tonic-gate if (incnt > 0)
3360Sstevel@tonic-gate emitheader(g, incnt, 0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate if ((g->fg_flags & FR_OUTQUE) != 0) {
3390Sstevel@tonic-gate for (outcnt = 0, f = g->fg_start; f != NULL;
3400Sstevel@tonic-gate f = f->fr_next)
3410Sstevel@tonic-gate outcnt++;
3420Sstevel@tonic-gate if (outcnt > 0)
3430Sstevel@tonic-gate emitheader(g, 0, outcnt);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate emittail();
3472393Syz155240 fprintf(cfile, "#endif /* IPFILTER_COMPILED */\n");
3480Sstevel@tonic-gate }
3492393Syz155240
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate
emitheader(grp,incount,outcount)3530Sstevel@tonic-gate static void emitheader(grp, incount, outcount)
3540Sstevel@tonic-gate frgroup_t *grp;
3550Sstevel@tonic-gate u_int incount, outcount;
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate static FILE *fph = NULL;
3580Sstevel@tonic-gate frgroup_t *g;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (fph == NULL) {
3610Sstevel@tonic-gate fph = fopen("ip_rules.h", "w");
3620Sstevel@tonic-gate if (fph == NULL)
3630Sstevel@tonic-gate return;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate fprintf(fph, "extern int ipfrule_add __P((void));\n");
3660Sstevel@tonic-gate fprintf(fph, "extern int ipfrule_remove __P((void));\n");
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate printhooks(cfile, incount, outcount, grp);
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate if (incount) {
3720Sstevel@tonic-gate fprintf(fph, "\n\
3730Sstevel@tonic-gate extern frentry_t *ipfrule_match_in_%s __P((fr_info_t *, u_32_t *));\n\
3740Sstevel@tonic-gate extern frentry_t *ipf_rules_in_%s[%d];\n",
3750Sstevel@tonic-gate grp->fg_name, grp->fg_name, incount);
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate for (g = groups; g != grp; g = g->fg_next)
3780Sstevel@tonic-gate if ((strncmp(g->fg_name, grp->fg_name,
3790Sstevel@tonic-gate FR_GROUPLEN) == 0) &&
3800Sstevel@tonic-gate g->fg_flags == grp->fg_flags)
3810Sstevel@tonic-gate break;
3820Sstevel@tonic-gate if (g == grp) {
3830Sstevel@tonic-gate fprintf(fph, "\n\
3840Sstevel@tonic-gate extern int ipfrule_add_in_%s __P((void));\n\
3850Sstevel@tonic-gate extern int ipfrule_remove_in_%s __P((void));\n", grp->fg_name, grp->fg_name);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate if (outcount) {
3890Sstevel@tonic-gate fprintf(fph, "\n\
3900Sstevel@tonic-gate extern frentry_t *ipfrule_match_out_%s __P((fr_info_t *, u_32_t *));\n\
3910Sstevel@tonic-gate extern frentry_t *ipf_rules_out_%s[%d];\n",
3920Sstevel@tonic-gate grp->fg_name, grp->fg_name, outcount);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate for (g = groups; g != g; g = g->fg_next)
3950Sstevel@tonic-gate if ((strncmp(g->fg_name, grp->fg_name,
3960Sstevel@tonic-gate FR_GROUPLEN) == 0) &&
3970Sstevel@tonic-gate g->fg_flags == grp->fg_flags)
3980Sstevel@tonic-gate break;
3990Sstevel@tonic-gate if (g == grp) {
4000Sstevel@tonic-gate fprintf(fph, "\n\
4010Sstevel@tonic-gate extern int ipfrule_add_out_%s __P((void));\n\
4020Sstevel@tonic-gate extern int ipfrule_remove_out_%s __P((void));\n",
4030Sstevel@tonic-gate grp->fg_name, grp->fg_name);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate
emittail()4080Sstevel@tonic-gate static void emittail()
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate frgroup_t *g;
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate fprintf(cfile, "\n\
4130Sstevel@tonic-gate int ipfrule_add()\n\
4140Sstevel@tonic-gate {\n\
4150Sstevel@tonic-gate int err;\n\
4160Sstevel@tonic-gate \n");
4170Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next)
4180Sstevel@tonic-gate fprintf(cfile, "\
4190Sstevel@tonic-gate err = ipfrule_add_%s_%s();\n\
4200Sstevel@tonic-gate if (err != 0)\n\
4210Sstevel@tonic-gate return err;\n",
4220Sstevel@tonic-gate (g->fg_flags & FR_INQUE) ? "in" : "out", g->fg_name);
4230Sstevel@tonic-gate fprintf(cfile, "\
4240Sstevel@tonic-gate return 0;\n");
4250Sstevel@tonic-gate fprintf(cfile, "}\n\
4260Sstevel@tonic-gate \n");
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate fprintf(cfile, "\n\
4290Sstevel@tonic-gate int ipfrule_remove()\n\
4300Sstevel@tonic-gate {\n\
4310Sstevel@tonic-gate int err;\n\
4320Sstevel@tonic-gate \n");
4330Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next)
4340Sstevel@tonic-gate fprintf(cfile, "\
4350Sstevel@tonic-gate err = ipfrule_remove_%s_%s();\n\
4360Sstevel@tonic-gate if (err != 0)\n\
4370Sstevel@tonic-gate return err;\n",
4380Sstevel@tonic-gate (g->fg_flags & FR_INQUE) ? "in" : "out", g->fg_name);
4390Sstevel@tonic-gate fprintf(cfile, "\
4400Sstevel@tonic-gate return 0;\n");
4410Sstevel@tonic-gate fprintf(cfile, "}\n");
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate
emitGroup(num,dir,v,fr,group,incount,outcount)4450Sstevel@tonic-gate static void emitGroup(num, dir, v, fr, group, incount, outcount)
4460Sstevel@tonic-gate int num, dir;
4470Sstevel@tonic-gate void *v;
4480Sstevel@tonic-gate frentry_t *fr;
4490Sstevel@tonic-gate char *group;
4500Sstevel@tonic-gate u_int incount, outcount;
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate static FILE *fp = NULL;
4530Sstevel@tonic-gate static int header[2] = { 0, 0 };
4540Sstevel@tonic-gate static char egroup[FR_GROUPLEN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
4550Sstevel@tonic-gate static int openfunc = 0;
4560Sstevel@tonic-gate static mc_t *n = NULL;
4570Sstevel@tonic-gate static int sin = 0;
4580Sstevel@tonic-gate frentry_t *f;
4590Sstevel@tonic-gate frgroup_t *g;
4600Sstevel@tonic-gate fripf_t *ipf;
4610Sstevel@tonic-gate int i, in, j;
4620Sstevel@tonic-gate mc_t *m = v;
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate if (fp == NULL)
4650Sstevel@tonic-gate fp = cfile;
4660Sstevel@tonic-gate if (fp == NULL)
4670Sstevel@tonic-gate return;
4680Sstevel@tonic-gate if (strncmp(egroup, group, FR_GROUPLEN)) {
4690Sstevel@tonic-gate for (sin--; sin > 0; sin--) {
4700Sstevel@tonic-gate indent(fp, sin);
4710Sstevel@tonic-gate fprintf(fp, "}\n");
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate if (openfunc == 1) {
4740Sstevel@tonic-gate fprintf(fp, "\treturn fr;\n}\n");
4750Sstevel@tonic-gate openfunc = 0;
4760Sstevel@tonic-gate if (n != NULL) {
4770Sstevel@tonic-gate free(n);
4780Sstevel@tonic-gate n = NULL;
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate sin = 0;
4820Sstevel@tonic-gate header[0] = 0;
4830Sstevel@tonic-gate header[1] = 0;
4840Sstevel@tonic-gate strncpy(egroup, group, FR_GROUPLEN);
4850Sstevel@tonic-gate } else if (openfunc == 1 && num < 0) {
4860Sstevel@tonic-gate if (n != NULL) {
4870Sstevel@tonic-gate free(n);
4880Sstevel@tonic-gate n = NULL;
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate for (sin--; sin > 0; sin--) {
4910Sstevel@tonic-gate indent(fp, sin);
4920Sstevel@tonic-gate fprintf(fp, "}\n");
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate if (openfunc == 1) {
4950Sstevel@tonic-gate fprintf(fp, "\treturn fr;\n}\n");
4960Sstevel@tonic-gate openfunc = 0;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate if (dir == -1)
5010Sstevel@tonic-gate return;
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next) {
5040Sstevel@tonic-gate if (dir == 0 && (g->fg_flags & FR_INQUE) == 0)
5050Sstevel@tonic-gate continue;
5060Sstevel@tonic-gate else if (dir == 1 && (g->fg_flags & FR_OUTQUE) == 0)
5070Sstevel@tonic-gate continue;
5080Sstevel@tonic-gate if (strncmp(g->fg_name, group, FR_GROUPLEN) != 0)
5090Sstevel@tonic-gate continue;
5100Sstevel@tonic-gate break;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate * Output the array of pointers to rules for this group.
5150Sstevel@tonic-gate */
5160Sstevel@tonic-gate if (num == -2 && dir == 0 && header[0] == 0 && incount != 0) {
5170Sstevel@tonic-gate fprintf(fp, "\nfrentry_t *ipf_rules_in_%s[%d] = {",
5180Sstevel@tonic-gate group, incount);
5190Sstevel@tonic-gate for (f = g->fg_start, i = 0; f != NULL; f = f->fr_next) {
5200Sstevel@tonic-gate if ((f->fr_flags & FR_INQUE) == 0)
5210Sstevel@tonic-gate continue;
5220Sstevel@tonic-gate if ((i & 1) == 0) {
5230Sstevel@tonic-gate fprintf(fp, "\n\t");
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate fprintf(fp,
5260Sstevel@tonic-gate "(frentry_t *)&in_rule_%s_%d",
5270Sstevel@tonic-gate f->fr_group, i);
5280Sstevel@tonic-gate if (i + 1 < incount)
5290Sstevel@tonic-gate fprintf(fp, ", ");
5300Sstevel@tonic-gate i++;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate fprintf(fp, "\n};\n");
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate if (num == -2 && dir == 1 && header[1] == 0 && outcount != 0) {
5360Sstevel@tonic-gate fprintf(fp, "\nfrentry_t *ipf_rules_out_%s[%d] = {",
5370Sstevel@tonic-gate group, outcount);
5380Sstevel@tonic-gate for (f = g->fg_start, i = 0; f != NULL; f = f->fr_next) {
5390Sstevel@tonic-gate if ((f->fr_flags & FR_OUTQUE) == 0)
5400Sstevel@tonic-gate continue;
5410Sstevel@tonic-gate if ((i & 1) == 0) {
5420Sstevel@tonic-gate fprintf(fp, "\n\t");
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate fprintf(fp,
5450Sstevel@tonic-gate "(frentry_t *)&out_rule_%s_%d",
5460Sstevel@tonic-gate f->fr_group, i);
5470Sstevel@tonic-gate if (i + 1 < outcount)
5480Sstevel@tonic-gate fprintf(fp, ", ");
5490Sstevel@tonic-gate i++;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate fprintf(fp, "\n};\n");
5520Sstevel@tonic-gate fp = NULL;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate if (num < 0)
5560Sstevel@tonic-gate return;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate in = 0;
5590Sstevel@tonic-gate ipf = fr->fr_ipf;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate * If the function header has not been printed then print it now.
5630Sstevel@tonic-gate */
5640Sstevel@tonic-gate if (header[dir] == 0) {
5650Sstevel@tonic-gate int pdst = 0, psrc = 0;
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate openfunc = 1;
5680Sstevel@tonic-gate fprintf(fp, "\nfrentry_t *ipfrule_match_%s_%s(fin, passp)\n",
5690Sstevel@tonic-gate (dir == 0) ? "in" : "out", group);
5700Sstevel@tonic-gate fprintf(fp, "fr_info_t *fin;\n");
5710Sstevel@tonic-gate fprintf(fp, "u_32_t *passp;\n");
5720Sstevel@tonic-gate fprintf(fp, "{\n");
5730Sstevel@tonic-gate fprintf(fp, "\tfrentry_t *fr = NULL;\n");
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate * Print out any variables that need to be declared.
5770Sstevel@tonic-gate */
5780Sstevel@tonic-gate for (f = g->fg_start, i = 0; f != NULL; f = f->fr_next) {
5790Sstevel@tonic-gate if (incount + outcount > m[FRC_SRC].e + 1)
5800Sstevel@tonic-gate psrc = 1;
5810Sstevel@tonic-gate if (incount + outcount > m[FRC_DST].e + 1)
5820Sstevel@tonic-gate pdst = 1;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate if (psrc == 1)
5850Sstevel@tonic-gate fprintf(fp, "\tu_32_t src = ntohl(%s);\n",
5860Sstevel@tonic-gate "fin->fin_fi.fi_saddr");
5870Sstevel@tonic-gate if (pdst == 1)
5880Sstevel@tonic-gate fprintf(fp, "\tu_32_t dst = ntohl(%s);\n",
5890Sstevel@tonic-gate "fin->fin_fi.fi_daddr");
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate for (i = 0; i < FRC_MAX; i++) {
5930Sstevel@tonic-gate switch(m[i].c)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate case FRC_IFN :
5960Sstevel@tonic-gate if (*fr->fr_ifname)
5970Sstevel@tonic-gate m[i].s = 1;
5980Sstevel@tonic-gate break;
5990Sstevel@tonic-gate case FRC_V :
6000Sstevel@tonic-gate if (ipf != NULL && ipf->fri_mip.fi_v != 0)
6010Sstevel@tonic-gate m[i].s = 1;
6020Sstevel@tonic-gate break;
6030Sstevel@tonic-gate case FRC_FL :
6040Sstevel@tonic-gate if (ipf != NULL && ipf->fri_mip.fi_flx != 0)
6050Sstevel@tonic-gate m[i].s = 1;
6060Sstevel@tonic-gate break;
6070Sstevel@tonic-gate case FRC_P :
6080Sstevel@tonic-gate if (ipf != NULL && ipf->fri_mip.fi_p != 0)
6090Sstevel@tonic-gate m[i].s = 1;
6100Sstevel@tonic-gate break;
6110Sstevel@tonic-gate case FRC_TTL :
6120Sstevel@tonic-gate if (ipf != NULL && ipf->fri_mip.fi_ttl != 0)
6130Sstevel@tonic-gate m[i].s = 1;
6140Sstevel@tonic-gate break;
6150Sstevel@tonic-gate case FRC_TOS :
6160Sstevel@tonic-gate if (ipf != NULL && ipf->fri_mip.fi_tos != 0)
6170Sstevel@tonic-gate m[i].s = 1;
6180Sstevel@tonic-gate break;
6190Sstevel@tonic-gate case FRC_TCP :
6200Sstevel@tonic-gate if (ipf == NULL)
6210Sstevel@tonic-gate break;
6220Sstevel@tonic-gate if ((ipf->fri_ip.fi_p == IPPROTO_TCP) &&
6230Sstevel@tonic-gate fr->fr_tcpfm != 0)
6240Sstevel@tonic-gate m[i].s = 1;
6250Sstevel@tonic-gate break;
6260Sstevel@tonic-gate case FRC_SP :
6270Sstevel@tonic-gate if (ipf == NULL)
6280Sstevel@tonic-gate break;
6290Sstevel@tonic-gate if (fr->fr_scmp == FR_INRANGE)
6300Sstevel@tonic-gate m[i].s = 1;
6310Sstevel@tonic-gate else if (fr->fr_scmp == FR_OUTRANGE)
6320Sstevel@tonic-gate m[i].s = 1;
6330Sstevel@tonic-gate else if (fr->fr_scmp != 0)
6340Sstevel@tonic-gate m[i].s = 1;
6350Sstevel@tonic-gate break;
6360Sstevel@tonic-gate case FRC_DP :
6370Sstevel@tonic-gate if (ipf == NULL)
6380Sstevel@tonic-gate break;
6390Sstevel@tonic-gate if (fr->fr_dcmp == FR_INRANGE)
6400Sstevel@tonic-gate m[i].s = 1;
6410Sstevel@tonic-gate else if (fr->fr_dcmp == FR_OUTRANGE)
6420Sstevel@tonic-gate m[i].s = 1;
6430Sstevel@tonic-gate else if (fr->fr_dcmp != 0)
6440Sstevel@tonic-gate m[i].s = 1;
6450Sstevel@tonic-gate break;
6460Sstevel@tonic-gate case FRC_SRC :
6470Sstevel@tonic-gate if (ipf == NULL)
6480Sstevel@tonic-gate break;
6490Sstevel@tonic-gate if (fr->fr_satype == FRI_LOOKUP) {
6500Sstevel@tonic-gate ;
6510Sstevel@tonic-gate } else if ((fr->fr_smask != 0) ||
6520Sstevel@tonic-gate (fr->fr_flags & FR_NOTSRCIP) != 0)
6530Sstevel@tonic-gate m[i].s = 1;
6540Sstevel@tonic-gate break;
6550Sstevel@tonic-gate case FRC_DST :
6560Sstevel@tonic-gate if (ipf == NULL)
6570Sstevel@tonic-gate break;
6580Sstevel@tonic-gate if (fr->fr_datype == FRI_LOOKUP) {
6590Sstevel@tonic-gate ;
6600Sstevel@tonic-gate } else if ((fr->fr_dmask != 0) ||
6610Sstevel@tonic-gate (fr->fr_flags & FR_NOTDSTIP) != 0)
6620Sstevel@tonic-gate m[i].s = 1;
6630Sstevel@tonic-gate break;
6640Sstevel@tonic-gate case FRC_OPT :
6650Sstevel@tonic-gate if (ipf == NULL)
6660Sstevel@tonic-gate break;
6670Sstevel@tonic-gate if (fr->fr_optmask != 0)
6680Sstevel@tonic-gate m[i].s = 1;
6690Sstevel@tonic-gate break;
6700Sstevel@tonic-gate case FRC_SEC :
6710Sstevel@tonic-gate if (ipf == NULL)
6720Sstevel@tonic-gate break;
6730Sstevel@tonic-gate if (fr->fr_secmask != 0)
6740Sstevel@tonic-gate m[i].s = 1;
6750Sstevel@tonic-gate break;
6760Sstevel@tonic-gate case FRC_ATH :
6770Sstevel@tonic-gate if (ipf == NULL)
6780Sstevel@tonic-gate break;
6790Sstevel@tonic-gate if (fr->fr_authmask != 0)
6800Sstevel@tonic-gate m[i].s = 1;
6810Sstevel@tonic-gate break;
6820Sstevel@tonic-gate case FRC_ICT :
6830Sstevel@tonic-gate if (ipf == NULL)
6840Sstevel@tonic-gate break;
6850Sstevel@tonic-gate if ((fr->fr_icmpm & 0xff00) != 0)
6860Sstevel@tonic-gate m[i].s = 1;
6870Sstevel@tonic-gate break;
6880Sstevel@tonic-gate case FRC_ICC :
6890Sstevel@tonic-gate if (ipf == NULL)
6900Sstevel@tonic-gate break;
6910Sstevel@tonic-gate if ((fr->fr_icmpm & 0xff) != 0)
6920Sstevel@tonic-gate m[i].s = 1;
6930Sstevel@tonic-gate break;
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate if (!header[dir]) {
6980Sstevel@tonic-gate fprintf(fp, "\n");
6990Sstevel@tonic-gate header[dir] = 1;
7000Sstevel@tonic-gate sin = 0;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate qsort(m, FRC_MAX, sizeof(mc_t), intcmp);
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate if (n) {
7060Sstevel@tonic-gate /*
7070Sstevel@tonic-gate * Calculate the indentation interval upto the last common
7080Sstevel@tonic-gate * common comparison being made.
7090Sstevel@tonic-gate */
7100Sstevel@tonic-gate for (i = 0, in = 1; i < FRC_MAX; i++) {
7110Sstevel@tonic-gate if (n[i].c != m[i].c)
7120Sstevel@tonic-gate break;
7130Sstevel@tonic-gate if (n[i].s != m[i].s)
7140Sstevel@tonic-gate break;
7150Sstevel@tonic-gate if (n[i].s) {
7160Sstevel@tonic-gate if (n[i].n && (n[i].n > n[i].e)) {
7170Sstevel@tonic-gate m[i].p++;
7180Sstevel@tonic-gate in += m[i].p;
7190Sstevel@tonic-gate break;
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate if (n[i].e > 0) {
7220Sstevel@tonic-gate in++;
7230Sstevel@tonic-gate } else
7240Sstevel@tonic-gate break;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate if (sin != in) {
7280Sstevel@tonic-gate for (j = sin - 1; j >= in; j--) {
7290Sstevel@tonic-gate indent(fp, j);
7300Sstevel@tonic-gate fprintf(fp, "}\n");
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate } else {
7340Sstevel@tonic-gate in = 1;
7350Sstevel@tonic-gate i = 0;
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate * print out C code that implements a filter rule.
7402393Syz155240 */
7410Sstevel@tonic-gate for (; i < FRC_MAX; i++) {
7420Sstevel@tonic-gate switch(m[i].c)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate case FRC_IFN :
7450Sstevel@tonic-gate if (m[i].s) {
7460Sstevel@tonic-gate indent(fp, in);
7470Sstevel@tonic-gate fprintf(fp, "if (fin->fin_ifp == ");
7480Sstevel@tonic-gate fprintf(fp, "ipf_rules_%s_%s[%d]->fr_ifa) {\n",
7490Sstevel@tonic-gate dir ? "out" : "in", group, num);
7500Sstevel@tonic-gate in++;
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate break;
7530Sstevel@tonic-gate case FRC_V :
7540Sstevel@tonic-gate if (m[i].s) {
7550Sstevel@tonic-gate indent(fp, in);
7560Sstevel@tonic-gate fprintf(fp, "if (fin->fin_v == %d) {\n",
7570Sstevel@tonic-gate ipf->fri_ip.fi_v);
7580Sstevel@tonic-gate in++;
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate break;
7610Sstevel@tonic-gate case FRC_FL :
7620Sstevel@tonic-gate if (m[i].s) {
7630Sstevel@tonic-gate indent(fp, in);
7640Sstevel@tonic-gate fprintf(fp, "if (");
7650Sstevel@tonic-gate printeq(fp, "fin->fin_flx",
7660Sstevel@tonic-gate ipf->fri_mip.fi_flx, 0xf,
7670Sstevel@tonic-gate ipf->fri_ip.fi_flx);
7680Sstevel@tonic-gate in++;
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate break;
7710Sstevel@tonic-gate case FRC_P :
7720Sstevel@tonic-gate if (m[i].s) {
7730Sstevel@tonic-gate indent(fp, in);
7740Sstevel@tonic-gate fprintf(fp, "if (fin->fin_p == %d) {\n",
7750Sstevel@tonic-gate ipf->fri_ip.fi_p);
7760Sstevel@tonic-gate in++;
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate break;
7790Sstevel@tonic-gate case FRC_TTL :
7800Sstevel@tonic-gate if (m[i].s) {
7810Sstevel@tonic-gate indent(fp, in);
7820Sstevel@tonic-gate fprintf(fp, "if (");
7830Sstevel@tonic-gate printeq(fp, "fin->fin_ttl",
7840Sstevel@tonic-gate ipf->fri_mip.fi_ttl, 0xff,
7850Sstevel@tonic-gate ipf->fri_ip.fi_ttl);
7860Sstevel@tonic-gate in++;
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate break;
7890Sstevel@tonic-gate case FRC_TOS :
7900Sstevel@tonic-gate if (m[i].s) {
7910Sstevel@tonic-gate indent(fp, in);
7920Sstevel@tonic-gate fprintf(fp, "if (fin->fin_tos");
7930Sstevel@tonic-gate printeq(fp, "fin->fin_tos",
7940Sstevel@tonic-gate ipf->fri_mip.fi_tos, 0xff,
7950Sstevel@tonic-gate ipf->fri_ip.fi_tos);
7960Sstevel@tonic-gate in++;
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate break;
7990Sstevel@tonic-gate case FRC_TCP :
8000Sstevel@tonic-gate if (m[i].s) {
8010Sstevel@tonic-gate indent(fp, in);
8020Sstevel@tonic-gate fprintf(fp, "if (");
8030Sstevel@tonic-gate printeq(fp, "fin->fin_tcpf", fr->fr_tcpfm,
8040Sstevel@tonic-gate 0xff, fr->fr_tcpf);
8050Sstevel@tonic-gate in++;
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate break;
8080Sstevel@tonic-gate case FRC_SP :
8090Sstevel@tonic-gate if (!m[i].s)
8100Sstevel@tonic-gate break;
8110Sstevel@tonic-gate if (fr->fr_scmp == FR_INRANGE) {
8120Sstevel@tonic-gate indent(fp, in);
8130Sstevel@tonic-gate fprintf(fp, "if ((fin->fin_data[0] > %d) && ",
8140Sstevel@tonic-gate fr->fr_sport);
8150Sstevel@tonic-gate fprintf(fp, "(fin->fin_data[0] < %d)",
8160Sstevel@tonic-gate fr->fr_stop);
8170Sstevel@tonic-gate fprintf(fp, ") {\n");
8180Sstevel@tonic-gate in++;
8190Sstevel@tonic-gate } else if (fr->fr_scmp == FR_OUTRANGE) {
8200Sstevel@tonic-gate indent(fp, in);
8210Sstevel@tonic-gate fprintf(fp, "if ((fin->fin_data[0] < %d) || ",
8220Sstevel@tonic-gate fr->fr_sport);
8230Sstevel@tonic-gate fprintf(fp, "(fin->fin_data[0] > %d)",
8240Sstevel@tonic-gate fr->fr_stop);
8250Sstevel@tonic-gate fprintf(fp, ") {\n");
8260Sstevel@tonic-gate in++;
8270Sstevel@tonic-gate } else if (fr->fr_scmp) {
8280Sstevel@tonic-gate indent(fp, in);
8290Sstevel@tonic-gate fprintf(fp, "if (fin->fin_data[0] %s %d)",
8300Sstevel@tonic-gate portcmp[fr->fr_scmp], fr->fr_sport);
8310Sstevel@tonic-gate fprintf(fp, " {\n");
8320Sstevel@tonic-gate in++;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate break;
8350Sstevel@tonic-gate case FRC_DP :
8360Sstevel@tonic-gate if (!m[i].s)
8370Sstevel@tonic-gate break;
8380Sstevel@tonic-gate if (fr->fr_dcmp == FR_INRANGE) {
8390Sstevel@tonic-gate indent(fp, in);
8400Sstevel@tonic-gate fprintf(fp, "if ((fin->fin_data[1] > %d) && ",
8410Sstevel@tonic-gate fr->fr_dport);
8420Sstevel@tonic-gate fprintf(fp, "(fin->fin_data[1] < %d)",
8430Sstevel@tonic-gate fr->fr_dtop);
8440Sstevel@tonic-gate fprintf(fp, ") {\n");
8450Sstevel@tonic-gate in++;
8460Sstevel@tonic-gate } else if (fr->fr_dcmp == FR_OUTRANGE) {
8470Sstevel@tonic-gate indent(fp, in);
8480Sstevel@tonic-gate fprintf(fp, "if ((fin->fin_data[1] < %d) || ",
8490Sstevel@tonic-gate fr->fr_dport);
8500Sstevel@tonic-gate fprintf(fp, "(fin->fin_data[1] > %d)",
8510Sstevel@tonic-gate fr->fr_dtop);
8520Sstevel@tonic-gate fprintf(fp, ") {\n");
8530Sstevel@tonic-gate in++;
8540Sstevel@tonic-gate } else if (fr->fr_dcmp) {
8550Sstevel@tonic-gate indent(fp, in);
8560Sstevel@tonic-gate fprintf(fp, "if (fin->fin_data[1] %s %d)",
8570Sstevel@tonic-gate portcmp[fr->fr_dcmp], fr->fr_dport);
8580Sstevel@tonic-gate fprintf(fp, " {\n");
8590Sstevel@tonic-gate in++;
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate break;
8620Sstevel@tonic-gate case FRC_SRC :
8630Sstevel@tonic-gate if (!m[i].s)
8640Sstevel@tonic-gate break;
8650Sstevel@tonic-gate if (fr->fr_satype == FRI_LOOKUP) {
8660Sstevel@tonic-gate ;
8670Sstevel@tonic-gate } else if ((fr->fr_smask != 0) ||
8680Sstevel@tonic-gate (fr->fr_flags & FR_NOTSRCIP) != 0) {
8690Sstevel@tonic-gate indent(fp, in);
8700Sstevel@tonic-gate fprintf(fp, "if (");
8710Sstevel@tonic-gate printipeq(fp, "src",
8720Sstevel@tonic-gate fr->fr_flags & FR_NOTSRCIP,
8730Sstevel@tonic-gate fr->fr_smask, fr->fr_saddr);
8740Sstevel@tonic-gate in++;
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate break;
8770Sstevel@tonic-gate case FRC_DST :
8780Sstevel@tonic-gate if (!m[i].s)
8790Sstevel@tonic-gate break;
8800Sstevel@tonic-gate if (fr->fr_datype == FRI_LOOKUP) {
8810Sstevel@tonic-gate ;
8820Sstevel@tonic-gate } else if ((fr->fr_dmask != 0) ||
8830Sstevel@tonic-gate (fr->fr_flags & FR_NOTDSTIP) != 0) {
8840Sstevel@tonic-gate indent(fp, in);
8850Sstevel@tonic-gate fprintf(fp, "if (");
8860Sstevel@tonic-gate printipeq(fp, "dst",
8870Sstevel@tonic-gate fr->fr_flags & FR_NOTDSTIP,
8880Sstevel@tonic-gate fr->fr_dmask, fr->fr_daddr);
8890Sstevel@tonic-gate in++;
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate break;
8920Sstevel@tonic-gate case FRC_OPT :
8930Sstevel@tonic-gate if (m[i].s) {
8940Sstevel@tonic-gate indent(fp, in);
8950Sstevel@tonic-gate fprintf(fp, "if (");
8960Sstevel@tonic-gate printeq(fp, "fin->fin_fi.fi_optmsk",
8970Sstevel@tonic-gate fr->fr_optmask, 0xffffffff,
8980Sstevel@tonic-gate fr->fr_optbits);
8990Sstevel@tonic-gate in++;
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate break;
9020Sstevel@tonic-gate case FRC_SEC :
9030Sstevel@tonic-gate if (m[i].s) {
9040Sstevel@tonic-gate indent(fp, in);
9050Sstevel@tonic-gate fprintf(fp, "if (");
9060Sstevel@tonic-gate printeq(fp, "fin->fin_fi.fi_secmsk",
9070Sstevel@tonic-gate fr->fr_secmask, 0xffff,
9080Sstevel@tonic-gate fr->fr_secbits);
9090Sstevel@tonic-gate in++;
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate break;
9120Sstevel@tonic-gate case FRC_ATH :
9130Sstevel@tonic-gate if (m[i].s) {
9140Sstevel@tonic-gate indent(fp, in);
9150Sstevel@tonic-gate fprintf(fp, "if (");
9160Sstevel@tonic-gate printeq(fp, "fin->fin_fi.fi_authmsk",
9170Sstevel@tonic-gate fr->fr_authmask, 0xffff,
9180Sstevel@tonic-gate fr->fr_authbits);
9190Sstevel@tonic-gate in++;
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate break;
9220Sstevel@tonic-gate case FRC_ICT :
9230Sstevel@tonic-gate if (m[i].s) {
9240Sstevel@tonic-gate indent(fp, in);
9250Sstevel@tonic-gate fprintf(fp, "if (");
9260Sstevel@tonic-gate printeq(fp, "fin->fin_data[0]",
9270Sstevel@tonic-gate fr->fr_icmpm & 0xff00, 0xffff,
9280Sstevel@tonic-gate fr->fr_icmp & 0xff00);
9290Sstevel@tonic-gate in++;
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate break;
9320Sstevel@tonic-gate case FRC_ICC :
9330Sstevel@tonic-gate if (m[i].s) {
9340Sstevel@tonic-gate indent(fp, in);
9350Sstevel@tonic-gate fprintf(fp, "if (");
9360Sstevel@tonic-gate printeq(fp, "fin->fin_data[0]",
9370Sstevel@tonic-gate fr->fr_icmpm & 0xff, 0xffff,
9380Sstevel@tonic-gate fr->fr_icmp & 0xff);
9390Sstevel@tonic-gate in++;
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate break;
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate indent(fp, in);
9470Sstevel@tonic-gate if (fr->fr_flags & FR_QUICK) {
9480Sstevel@tonic-gate fprintf(fp, "return (frentry_t *)&%s_rule_%s_%d;\n",
9490Sstevel@tonic-gate fr->fr_flags & FR_INQUE ? "in" : "out",
9500Sstevel@tonic-gate fr->fr_group, num);
9510Sstevel@tonic-gate } else {
9520Sstevel@tonic-gate fprintf(fp, "fr = (frentry_t *)&%s_rule_%s_%d;\n",
9530Sstevel@tonic-gate fr->fr_flags & FR_INQUE ? "in" : "out",
9540Sstevel@tonic-gate fr->fr_group, num);
9550Sstevel@tonic-gate }
956*2934Sjojemann if (n == NULL) {
9570Sstevel@tonic-gate n = (mc_t *)malloc(sizeof(*n) * FRC_MAX);
958*2934Sjojemann if (n == NULL) {
959*2934Sjojemann fprintf(stderr, "out of memory\n");
960*2934Sjojemann exit(1);
961*2934Sjojemann }
962*2934Sjojemann }
9630Sstevel@tonic-gate bcopy((char *)m, (char *)n, sizeof(*n) * FRC_MAX);
9640Sstevel@tonic-gate sin = in;
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate
9670Sstevel@tonic-gate
printC(dir)9680Sstevel@tonic-gate void printC(dir)
9690Sstevel@tonic-gate int dir;
9700Sstevel@tonic-gate {
9710Sstevel@tonic-gate static mc_t *m = NULL;
9720Sstevel@tonic-gate frgroup_t *g;
9730Sstevel@tonic-gate
974*2934Sjojemann if (m == NULL) {
9750Sstevel@tonic-gate m = (mc_t *)calloc(1, sizeof(*m) * FRC_MAX);
976*2934Sjojemann if (m == NULL) {
977*2934Sjojemann fprintf(stderr, "out of memory\n");
978*2934Sjojemann exit(1);
979*2934Sjojemann }
980*2934Sjojemann }
9810Sstevel@tonic-gate
9820Sstevel@tonic-gate for (g = groups; g != NULL; g = g->fg_next) {
9830Sstevel@tonic-gate if ((dir == 0) && ((g->fg_flags & FR_INQUE) != 0))
9840Sstevel@tonic-gate printCgroup(dir, g->fg_start, m, g->fg_name);
9850Sstevel@tonic-gate if ((dir == 1) && ((g->fg_flags & FR_OUTQUE) != 0))
9860Sstevel@tonic-gate printCgroup(dir, g->fg_start, m, g->fg_name);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate emit(-1, dir, m, NULL);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate /*
9940Sstevel@tonic-gate * Now print out code to implement all of the rules.
9950Sstevel@tonic-gate */
printCgroup(dir,top,m,group)9960Sstevel@tonic-gate static void printCgroup(dir, top, m, group)
9970Sstevel@tonic-gate int dir;
9980Sstevel@tonic-gate frentry_t *top;
9990Sstevel@tonic-gate mc_t *m;
10000Sstevel@tonic-gate char *group;
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate frentry_t *fr, *fr1;
10030Sstevel@tonic-gate int i, n, rn;
10040Sstevel@tonic-gate u_int count;
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate for (count = 0, fr1 = top; fr1 != NULL; fr1 = fr1->fr_next) {
10070Sstevel@tonic-gate if ((dir == 0) && ((fr1->fr_flags & FR_INQUE) != 0))
10080Sstevel@tonic-gate count++;
10090Sstevel@tonic-gate else if ((dir == 1) && ((fr1->fr_flags & FR_OUTQUE) != 0))
10100Sstevel@tonic-gate count++;
10110Sstevel@tonic-gate }
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate if (dir == 0)
10140Sstevel@tonic-gate emitGroup(-2, dir, m, fr1, group, count, 0);
10150Sstevel@tonic-gate else if (dir == 1)
10160Sstevel@tonic-gate emitGroup(-2, dir, m, fr1, group, 0, count);
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate /*
10190Sstevel@tonic-gate * Before printing each rule, check to see how many of its fields are
10200Sstevel@tonic-gate * matched by subsequent rules.
10210Sstevel@tonic-gate */
10220Sstevel@tonic-gate for (fr1 = top, rn = 0; fr1 != NULL; fr1 = fr1->fr_next, rn++) {
10230Sstevel@tonic-gate if (!dir && !(fr1->fr_flags & FR_INQUE))
10240Sstevel@tonic-gate continue;
10250Sstevel@tonic-gate if (dir && !(fr1->fr_flags & FR_OUTQUE))
10260Sstevel@tonic-gate continue;
10270Sstevel@tonic-gate n = 0xfffffff;
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate for (i = 0; i < FRC_MAX; i++)
10300Sstevel@tonic-gate m[i].e = 0;
10310Sstevel@tonic-gate qsort(m, FRC_MAX, sizeof(mc_t), intcmp);
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate for (i = 0; i < FRC_MAX; i++) {
10340Sstevel@tonic-gate m[i].c = i;
10350Sstevel@tonic-gate m[i].e = 0;
10360Sstevel@tonic-gate m[i].n = 0;
10370Sstevel@tonic-gate m[i].s = 0;
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate for (fr = fr1->fr_next; fr; fr = fr->fr_next) {
10410Sstevel@tonic-gate if (!dir && !(fr->fr_flags & FR_INQUE))
10420Sstevel@tonic-gate continue;
10430Sstevel@tonic-gate if (dir && !(fr->fr_flags & FR_OUTQUE))
10440Sstevel@tonic-gate continue;
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate if ((n & 0x0001) &&
10470Sstevel@tonic-gate !strcmp(fr1->fr_ifname, fr->fr_ifname)) {
10480Sstevel@tonic-gate m[FRC_IFN].e++;
10490Sstevel@tonic-gate m[FRC_IFN].n++;
10500Sstevel@tonic-gate } else
10510Sstevel@tonic-gate n &= ~0x0001;
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate if ((n & 0x0002) && (fr1->fr_v == fr->fr_v)) {
10540Sstevel@tonic-gate m[FRC_V].e++;
10550Sstevel@tonic-gate m[FRC_V].n++;
10560Sstevel@tonic-gate } else
10570Sstevel@tonic-gate n &= ~0x0002;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate if ((n & 0x0004) &&
10600Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
10610Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
10620Sstevel@tonic-gate (fr1->fr_mip.fi_flx == fr->fr_mip.fi_flx) &&
10630Sstevel@tonic-gate (fr1->fr_ip.fi_flx == fr->fr_ip.fi_flx)) {
10640Sstevel@tonic-gate m[FRC_FL].e++;
10650Sstevel@tonic-gate m[FRC_FL].n++;
10660Sstevel@tonic-gate } else
10670Sstevel@tonic-gate n &= ~0x0004;
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate if ((n & 0x0008) &&
10700Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
10710Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
10720Sstevel@tonic-gate (fr1->fr_proto == fr->fr_proto)) {
10730Sstevel@tonic-gate m[FRC_P].e++;
10740Sstevel@tonic-gate m[FRC_P].n++;
10750Sstevel@tonic-gate } else
10760Sstevel@tonic-gate n &= ~0x0008;
10770Sstevel@tonic-gate
10780Sstevel@tonic-gate if ((n & 0x0010) &&
10790Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
10800Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
10810Sstevel@tonic-gate (fr1->fr_ttl == fr->fr_ttl)) {
10820Sstevel@tonic-gate m[FRC_TTL].e++;
10830Sstevel@tonic-gate m[FRC_TTL].n++;
10840Sstevel@tonic-gate } else
10850Sstevel@tonic-gate n &= ~0x0010;
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate if ((n & 0x0020) &&
10880Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
10890Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
10900Sstevel@tonic-gate (fr1->fr_tos == fr->fr_tos)) {
10910Sstevel@tonic-gate m[FRC_TOS].e++;
10920Sstevel@tonic-gate m[FRC_TOS].n++;
10930Sstevel@tonic-gate } else
10940Sstevel@tonic-gate n &= ~0x0020;
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate if ((n & 0x0040) &&
10970Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
10980Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
10990Sstevel@tonic-gate ((fr1->fr_tcpfm == fr->fr_tcpfm) &&
11000Sstevel@tonic-gate (fr1->fr_tcpf == fr->fr_tcpf))) {
11010Sstevel@tonic-gate m[FRC_TCP].e++;
11020Sstevel@tonic-gate m[FRC_TCP].n++;
11030Sstevel@tonic-gate } else
11040Sstevel@tonic-gate n &= ~0x0040;
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate if ((n & 0x0080) &&
11070Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11080Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11090Sstevel@tonic-gate ((fr1->fr_scmp == fr->fr_scmp) &&
11100Sstevel@tonic-gate (fr1->fr_stop == fr->fr_stop) &&
11110Sstevel@tonic-gate (fr1->fr_sport == fr->fr_sport))) {
11120Sstevel@tonic-gate m[FRC_SP].e++;
11130Sstevel@tonic-gate m[FRC_SP].n++;
11140Sstevel@tonic-gate } else
11150Sstevel@tonic-gate n &= ~0x0080;
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate if ((n & 0x0100) &&
11180Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11190Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11200Sstevel@tonic-gate ((fr1->fr_dcmp == fr->fr_dcmp) &&
11210Sstevel@tonic-gate (fr1->fr_dtop == fr->fr_dtop) &&
11220Sstevel@tonic-gate (fr1->fr_dport == fr->fr_dport))) {
11230Sstevel@tonic-gate m[FRC_DP].e++;
11240Sstevel@tonic-gate m[FRC_DP].n++;
11250Sstevel@tonic-gate } else
11260Sstevel@tonic-gate n &= ~0x0100;
11270Sstevel@tonic-gate
11280Sstevel@tonic-gate if ((n & 0x0200) &&
11290Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11300Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11310Sstevel@tonic-gate ((fr1->fr_satype == FRI_LOOKUP) &&
11320Sstevel@tonic-gate (fr->fr_satype == FRI_LOOKUP) &&
11330Sstevel@tonic-gate (fr1->fr_srcnum == fr->fr_srcnum))) {
11340Sstevel@tonic-gate m[FRC_SRC].e++;
11350Sstevel@tonic-gate m[FRC_SRC].n++;
11360Sstevel@tonic-gate } else if ((n & 0x0200) &&
11370Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11380Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11390Sstevel@tonic-gate (((fr1->fr_flags & FR_NOTSRCIP) ==
11400Sstevel@tonic-gate (fr->fr_flags & FR_NOTSRCIP)))) {
11410Sstevel@tonic-gate if ((fr1->fr_smask == fr->fr_smask) &&
11420Sstevel@tonic-gate (fr1->fr_saddr == fr->fr_saddr))
11430Sstevel@tonic-gate m[FRC_SRC].e++;
11440Sstevel@tonic-gate else
11450Sstevel@tonic-gate n &= ~0x0200;
11460Sstevel@tonic-gate if (fr1->fr_smask &&
11470Sstevel@tonic-gate (fr1->fr_saddr & fr1->fr_smask) ==
11480Sstevel@tonic-gate (fr->fr_saddr & fr1->fr_smask)) {
11490Sstevel@tonic-gate m[FRC_SRC].n++;
11500Sstevel@tonic-gate n |= 0x0200;
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate } else {
11530Sstevel@tonic-gate n &= ~0x0200;
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate
11560Sstevel@tonic-gate if ((n & 0x0400) &&
11570Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11580Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11590Sstevel@tonic-gate ((fr1->fr_datype == FRI_LOOKUP) &&
11600Sstevel@tonic-gate (fr->fr_datype == FRI_LOOKUP) &&
11610Sstevel@tonic-gate (fr1->fr_dstnum == fr->fr_dstnum))) {
11620Sstevel@tonic-gate m[FRC_DST].e++;
11630Sstevel@tonic-gate m[FRC_DST].n++;
11640Sstevel@tonic-gate } else if ((n & 0x0400) &&
11650Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11660Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11670Sstevel@tonic-gate (((fr1->fr_flags & FR_NOTDSTIP) ==
11680Sstevel@tonic-gate (fr->fr_flags & FR_NOTDSTIP)))) {
11690Sstevel@tonic-gate if ((fr1->fr_dmask == fr->fr_dmask) &&
11700Sstevel@tonic-gate (fr1->fr_daddr == fr->fr_daddr))
11710Sstevel@tonic-gate m[FRC_DST].e++;
11720Sstevel@tonic-gate else
11730Sstevel@tonic-gate n &= ~0x0400;
11740Sstevel@tonic-gate if (fr1->fr_dmask &&
11750Sstevel@tonic-gate (fr1->fr_daddr & fr1->fr_dmask) ==
11760Sstevel@tonic-gate (fr->fr_daddr & fr1->fr_dmask)) {
11770Sstevel@tonic-gate m[FRC_DST].n++;
11780Sstevel@tonic-gate n |= 0x0400;
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate } else {
11810Sstevel@tonic-gate n &= ~0x0400;
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate
11840Sstevel@tonic-gate if ((n & 0x0800) &&
11850Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11860Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11870Sstevel@tonic-gate (fr1->fr_optmask == fr->fr_optmask) &&
11880Sstevel@tonic-gate (fr1->fr_optbits == fr->fr_optbits)) {
11890Sstevel@tonic-gate m[FRC_OPT].e++;
11900Sstevel@tonic-gate m[FRC_OPT].n++;
11910Sstevel@tonic-gate } else
11920Sstevel@tonic-gate n &= ~0x0800;
11930Sstevel@tonic-gate
11940Sstevel@tonic-gate if ((n & 0x1000) &&
11950Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
11960Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
11970Sstevel@tonic-gate (fr1->fr_secmask == fr->fr_secmask) &&
11980Sstevel@tonic-gate (fr1->fr_secbits == fr->fr_secbits)) {
11990Sstevel@tonic-gate m[FRC_SEC].e++;
12000Sstevel@tonic-gate m[FRC_SEC].n++;
12010Sstevel@tonic-gate } else
12020Sstevel@tonic-gate n &= ~0x1000;
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate if ((n & 0x10000) &&
12050Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
12060Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
12070Sstevel@tonic-gate (fr1->fr_authmask == fr->fr_authmask) &&
12080Sstevel@tonic-gate (fr1->fr_authbits == fr->fr_authbits)) {
12090Sstevel@tonic-gate m[FRC_ATH].e++;
12100Sstevel@tonic-gate m[FRC_ATH].n++;
12110Sstevel@tonic-gate } else
12120Sstevel@tonic-gate n &= ~0x10000;
12130Sstevel@tonic-gate
12140Sstevel@tonic-gate if ((n & 0x20000) &&
12150Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
12160Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
12170Sstevel@tonic-gate ((fr1->fr_icmpm & 0xff00) ==
12180Sstevel@tonic-gate (fr->fr_icmpm & 0xff00)) &&
12190Sstevel@tonic-gate ((fr1->fr_icmp & 0xff00) ==
12200Sstevel@tonic-gate (fr->fr_icmp & 0xff00))) {
12210Sstevel@tonic-gate m[FRC_ICT].e++;
12220Sstevel@tonic-gate m[FRC_ICT].n++;
12230Sstevel@tonic-gate } else
12240Sstevel@tonic-gate n &= ~0x20000;
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate if ((n & 0x40000) &&
12270Sstevel@tonic-gate (fr->fr_type == fr1->fr_type) &&
12280Sstevel@tonic-gate (fr->fr_type == FR_T_IPF) &&
12290Sstevel@tonic-gate ((fr1->fr_icmpm & 0xff) == (fr->fr_icmpm & 0xff)) &&
12300Sstevel@tonic-gate ((fr1->fr_icmp & 0xff) == (fr->fr_icmp & 0xff))) {
12310Sstevel@tonic-gate m[FRC_ICC].e++;
12320Sstevel@tonic-gate m[FRC_ICC].n++;
12330Sstevel@tonic-gate } else
12340Sstevel@tonic-gate n &= ~0x40000;
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate /*msort(m);*/
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate if (dir == 0)
12390Sstevel@tonic-gate emitGroup(rn, dir, m, fr1, group, count, 0);
12400Sstevel@tonic-gate else if (dir == 1)
12410Sstevel@tonic-gate emitGroup(rn, dir, m, fr1, group, 0, count);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate
printhooks(fp,in,out,grp)12450Sstevel@tonic-gate static void printhooks(fp, in, out, grp)
12460Sstevel@tonic-gate FILE *fp;
12470Sstevel@tonic-gate int in;
12480Sstevel@tonic-gate int out;
12490Sstevel@tonic-gate frgroup_t *grp;
12500Sstevel@tonic-gate {
12510Sstevel@tonic-gate frentry_t *fr;
12520Sstevel@tonic-gate char *group;
12530Sstevel@tonic-gate int dogrp, i;
12540Sstevel@tonic-gate char *instr;
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate group = grp->fg_name;
12570Sstevel@tonic-gate dogrp = 0;
12580Sstevel@tonic-gate
12590Sstevel@tonic-gate if (in && out) {
12600Sstevel@tonic-gate fprintf(stderr,
12610Sstevel@tonic-gate "printhooks called with both in and out set\n");
12620Sstevel@tonic-gate exit(1);
12630Sstevel@tonic-gate }
12640Sstevel@tonic-gate
12650Sstevel@tonic-gate if (in) {
12660Sstevel@tonic-gate instr = "in";
12670Sstevel@tonic-gate } else if (out) {
12680Sstevel@tonic-gate instr = "out";
12690Sstevel@tonic-gate } else {
12700Sstevel@tonic-gate instr = "???";
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate fprintf(fp, "static frentry_t ipfrule_%s_%s;\n", instr, group);
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate fprintf(fp, "\
12750Sstevel@tonic-gate \n\
12760Sstevel@tonic-gate int ipfrule_add_%s_%s()\n", instr, group);
12770Sstevel@tonic-gate fprintf(fp, "\
12780Sstevel@tonic-gate {\n\
12790Sstevel@tonic-gate int i, j, err = 0, max;\n\
12800Sstevel@tonic-gate frentry_t *fp;\n");
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate if (dogrp)
12830Sstevel@tonic-gate fprintf(fp, "\
12840Sstevel@tonic-gate frgroup_t *fg;\n");
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate fprintf(fp, "\n");
12870Sstevel@tonic-gate
12880Sstevel@tonic-gate for (i = 0, fr = grp->fg_start; fr != NULL; i++, fr = fr->fr_next)
12890Sstevel@tonic-gate if (fr->fr_dsize > 0) {
12900Sstevel@tonic-gate fprintf(fp, "\
12910Sstevel@tonic-gate ipf_rules_%s_%s[%d]->fr_data = &ipf%s_rule_data_%s_%u;\n",
12920Sstevel@tonic-gate instr, grp->fg_name, i,
12930Sstevel@tonic-gate instr, grp->fg_name, i);
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate fprintf(fp, "\
12960Sstevel@tonic-gate max = sizeof(ipf_rules_%s_%s)/sizeof(frentry_t *);\n\
12970Sstevel@tonic-gate for (i = 0; i < max; i++) {\n\
12980Sstevel@tonic-gate fp = ipf_rules_%s_%s[i];\n\
12990Sstevel@tonic-gate fp->fr_next = NULL;\n", instr, group, instr, group);
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate fprintf(fp, "\
13020Sstevel@tonic-gate for (j = i + 1; j < max; j++)\n\
13030Sstevel@tonic-gate if (strncmp(fp->fr_group,\n\
13040Sstevel@tonic-gate ipf_rules_%s_%s[j]->fr_group,\n\
13050Sstevel@tonic-gate FR_GROUPLEN) == 0) {\n\
13060Sstevel@tonic-gate fp->fr_next = ipf_rules_%s_%s[j];\n\
13070Sstevel@tonic-gate break;\n\
13080Sstevel@tonic-gate }\n", instr, group, instr, group);
13090Sstevel@tonic-gate if (dogrp)
13100Sstevel@tonic-gate fprintf(fp, "\
13110Sstevel@tonic-gate \n\
13120Sstevel@tonic-gate if (fp->fr_grhead != 0) {\n\
13130Sstevel@tonic-gate fg = fr_addgroup(fp->fr_grhead, fp, FR_INQUE,\n\
13140Sstevel@tonic-gate IPL_LOGIPF, 0);\n\
13150Sstevel@tonic-gate if (fg != NULL)\n\
13160Sstevel@tonic-gate fp->fr_grp = &fg->fg_start;\n\
13170Sstevel@tonic-gate }\n");
13180Sstevel@tonic-gate fprintf(fp, "\
13190Sstevel@tonic-gate }\n\
13200Sstevel@tonic-gate \n\
13210Sstevel@tonic-gate fp = &ipfrule_%s_%s;\n", instr, group);
13220Sstevel@tonic-gate fprintf(fp, "\
13230Sstevel@tonic-gate bzero((char *)fp, sizeof(*fp));\n\
13240Sstevel@tonic-gate fp->fr_type = FR_T_CALLFUNC|FR_T_BUILTIN;\n\
13250Sstevel@tonic-gate fp->fr_flags = FR_%sQUE|FR_NOMATCH;\n\
13260Sstevel@tonic-gate fp->fr_data = (void *)ipf_rules_%s_%s[0];\n",
13270Sstevel@tonic-gate (in != 0) ? "IN" : "OUT", instr, group);
13282393Syz155240 fprintf(fp, "\
13292393Syz155240 fp->fr_dsize = sizeof(ipf_rules_%s_%s[0]);\n",
13302393Syz155240 instr, group);
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate fprintf(fp, "\
13330Sstevel@tonic-gate fp->fr_v = 4;\n\
13340Sstevel@tonic-gate fp->fr_func = (ipfunc_t)ipfrule_match_%s_%s;\n\
13350Sstevel@tonic-gate err = frrequest(IPL_LOGIPF, SIOCADDFR, (caddr_t)fp, fr_active, 0);\n",
13360Sstevel@tonic-gate instr, group);
13370Sstevel@tonic-gate fprintf(fp, "\treturn err;\n}\n");
13380Sstevel@tonic-gate
13390Sstevel@tonic-gate fprintf(fp, "\n\n\
13400Sstevel@tonic-gate int ipfrule_remove_%s_%s()\n", instr, group);
13410Sstevel@tonic-gate fprintf(fp, "\
13420Sstevel@tonic-gate {\n\
13430Sstevel@tonic-gate int err = 0, i;\n\
13440Sstevel@tonic-gate frentry_t *fp;\n\
13450Sstevel@tonic-gate \n\
13460Sstevel@tonic-gate /*\n\
13470Sstevel@tonic-gate * Try to remove the %sbound rule.\n", instr);
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate fprintf(fp, "\
13500Sstevel@tonic-gate */\n\
13510Sstevel@tonic-gate if (ipfrule_%s_%s.fr_ref > 0) {\n", instr, group);
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate fprintf(fp, "\
13540Sstevel@tonic-gate err = EBUSY;\n\
13550Sstevel@tonic-gate } else {\n");
13560Sstevel@tonic-gate
13570Sstevel@tonic-gate fprintf(fp, "\
13580Sstevel@tonic-gate i = sizeof(ipf_rules_%s_%s)/sizeof(frentry_t *) - 1;\n\
13590Sstevel@tonic-gate for (; i >= 0; i--) {\n\
13600Sstevel@tonic-gate fp = ipf_rules_%s_%s[i];\n\
13610Sstevel@tonic-gate if (fp->fr_ref > 1) {\n\
13620Sstevel@tonic-gate err = EBUSY;\n\
13630Sstevel@tonic-gate break;\n\
13640Sstevel@tonic-gate }\n\
13650Sstevel@tonic-gate }\n\
13660Sstevel@tonic-gate }\n\
13670Sstevel@tonic-gate if (err == 0)\n\
13680Sstevel@tonic-gate err = frrequest(IPL_LOGIPF, SIOCDELFR,\n\
13690Sstevel@tonic-gate (caddr_t)&ipfrule_%s_%s, fr_active, 0);\n",
13700Sstevel@tonic-gate instr, group, instr, group, instr, group);
13710Sstevel@tonic-gate fprintf(fp, "\
13720Sstevel@tonic-gate if (err)\n\
13730Sstevel@tonic-gate return err;\n\
13740Sstevel@tonic-gate \n\n");
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate fprintf(fp, "\treturn err;\n}\n");
13770Sstevel@tonic-gate }
1378