4 * Copyright (C) 2012 by Darren Reed.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and due credit is given
8 * to the original author and the contributors.
11 #include <sys/param.h>
12 #include <sys/types.h>
14 #include <sys/socket.h>
15 #if defined(__FreeBSD_version)
17 # include <sys/libkern.h>
19 # include <sys/unistd.h>
22 # include <sys/systm.h>
24 #include <sys/errno.h>
25 #include <sys/param.h>
27 # include <sys/mbuf.h>
29 #if defined(__FreeBSD__)
30 # include <sys/sockio.h>
34 #define CURVNET_SET(arg)
35 #define CURVNET_RESTORE()
36 #define VNET_DEFINE(_t, _v) _t _v
37 #define VNET_DECLARE(_t, _v) extern _t _v
41 # include <sys/ioctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/tcp.h>
48 #include "netinet/ip_compat.h"
49 #include "netinet/ip_fil.h"
51 #include "netinet/ip_rules.h"
57 #ifdef IPFILTER_COMPILED
59 VNET_DECLARE(ipf_main_softc_t, ipfmain);
60 #define V_ipfmain VNET(ipfmain)
63 static u_long in_rule__0[] = {
64 0, 0, 0, 0, 0, 0, 0, 0x8070d88, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0x1b0, 0x1, 0, 0, 0, 0x2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40000000, 0x8002, 0, 0, 0, 0xffff, 0, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0
67 static u_long out_rule__0[] = {
68 0, 0, 0, 0, 0, 0, 0, 0x8070d88, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0x1b0, 0x1, 0, 0, 0, 0x3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x40000000, 0x4002, 0, 0, 0, 0xffff, 0, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xffffffff, 0, 0, 0, 0
71 frentry_t *ipf_rules_in_[1] = {
72 (frentry_t *)&in_rule__0
75 /* XXX This file (ip_rules.c) is not part of the ipfilter tarball, it is
76 XXX generated by the ipfilter build process. Unfortunately the build
77 XXX process did not generate the following lines so they are added
78 XXX by hand here. This is a bit of a hack but it works for now. Future
79 XXX imports/merges of ipfilter may generate this so the following will
80 XXX need to be removed following some future merge.
82 frentry_t *ipf_rules_out_[1] = {
83 (frentry_t *)&out_rule__0
86 frentry_t *ipfrule_match_in_(fin, passp)
92 fr = (frentry_t *)&in_rule__0;
96 frentry_t *ipfrule_match_out_(fin, passp)
100 frentry_t *fr = NULL;
102 fr = (frentry_t *)&out_rule__0;
105 static frentry_t ipfrule_out_;
107 int ipfrule_add_out_()
109 int i, j, err = 0, max;
112 max = sizeof(ipf_rules_out_)/sizeof(frentry_t *);
113 for (i = 0; i < max; i++) {
114 fp = ipf_rules_out_[i];
116 for (j = i + 1; j < max; j++)
117 if (strncmp(fp->fr_names + fp->fr_group,
118 ipf_rules_out_[j]->fr_names +
119 ipf_rules_out_[j]->fr_group,
121 if (ipf_rules_out_[j] != NULL)
122 ipf_rules_out_[j]->fr_pnext =
124 fp->fr_pnext = &ipf_rules_out_[j];
125 fp->fr_next = ipf_rules_out_[j];
131 bzero((char *)fp, sizeof(*fp));
132 fp->fr_type = FR_T_CALLFUNC_BUILTIN;
133 fp->fr_flags = FR_OUTQUE|FR_NOMATCH;
134 fp->fr_data = (void *)ipf_rules_out_[0];
135 fp->fr_dsize = sizeof(ipf_rules_out_[0]);
136 fp->fr_family = AF_INET;
137 fp->fr_func = (ipfunc_t)ipfrule_match_out_;
138 err = frrequest(&V_ipfmain, IPL_LOGIPF, SIOCADDFR, (caddr_t)fp,
139 V_ipfmain.ipf_active, 0);
144 int ipfrule_remove_out_()
150 * Try to remove the outbound rule.
152 if (ipfrule_out_.fr_ref > 0) {
155 i = sizeof(ipf_rules_out_)/sizeof(frentry_t *) - 1;
156 for (; i >= 0; i--) {
157 fp = ipf_rules_out_[i];
158 if (fp->fr_ref > 1) {
165 err = frrequest(&V_ipfmain, IPL_LOGIPF, SIOCDELFR,
166 (caddr_t)&ipfrule_out_,
167 V_ipfmain.ipf_active, 0);
174 static frentry_t ipfrule_in_;
176 int ipfrule_add_in_()
178 int i, j, err = 0, max;
181 max = sizeof(ipf_rules_in_)/sizeof(frentry_t *);
182 for (i = 0; i < max; i++) {
183 fp = ipf_rules_in_[i];
185 for (j = i + 1; j < max; j++)
186 if (strncmp(fp->fr_names + fp->fr_group,
187 ipf_rules_in_[j]->fr_names +
188 ipf_rules_in_[j]->fr_group,
190 if (ipf_rules_in_[j] != NULL)
191 ipf_rules_in_[j]->fr_pnext =
193 fp->fr_pnext = &ipf_rules_in_[j];
194 fp->fr_next = ipf_rules_in_[j];
200 bzero((char *)fp, sizeof(*fp));
201 fp->fr_type = FR_T_CALLFUNC_BUILTIN;
202 fp->fr_flags = FR_INQUE|FR_NOMATCH;
203 fp->fr_data = (void *)ipf_rules_in_[0];
204 fp->fr_dsize = sizeof(ipf_rules_in_[0]);
205 fp->fr_family = AF_INET;
206 fp->fr_func = (ipfunc_t)ipfrule_match_in_;
207 err = frrequest(&V_ipfmain, IPL_LOGIPF, SIOCADDFR, (caddr_t)fp,
208 V_ipfmain.ipf_active, 0);
213 int ipfrule_remove_in_()
219 * Try to remove the inbound rule.
221 if (ipfrule_in_.fr_ref > 0) {
224 i = sizeof(ipf_rules_in_)/sizeof(frentry_t *) - 1;
225 for (; i >= 0; i--) {
226 fp = ipf_rules_in_[i];
227 if (fp->fr_ref > 1) {
234 err = frrequest(&V_ipfmain, IPL_LOGIPF, SIOCDELFR,
235 (caddr_t)&ipfrule_in_,
236 V_ipfmain.ipf_active, 0);
248 err = ipfrule_add_out_();
251 err = ipfrule_add_in_();
262 err = ipfrule_remove_out_();
265 err = ipfrule_remove_in_();
270 #endif /* IPFILTER_COMPILED */