]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ipfilter/netinet/mlfk_ipl.c
Merge IPFilter 4.1.23 back to HEAD
[FreeBSD/FreeBSD.git] / sys / contrib / ipfilter / netinet / mlfk_ipl.c
1 /*      $FreeBSD$       */
2
3 /*
4  * Copyright (C) 2000 by Darren Reed.
5  *
6  * $FreeBSD$
7  * See the IPFILTER.LICENCE file for details on licencing.
8  */
9
10
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/kernel.h>
14 #include <sys/module.h>
15 #include <sys/conf.h>
16 #include <sys/socket.h>
17 #include <sys/sysctl.h>
18 #include <sys/select.h>
19 #if __FreeBSD_version >= 500000
20 # include <sys/selinfo.h>
21 #endif                  
22 #include <net/if.h>
23 #include <netinet/in_systm.h>
24 #include <netinet/in.h>
25
26
27 #include <netinet/ipl.h>
28 #include <netinet/ip_compat.h>
29 #include <netinet/ip_fil.h>
30 #include <netinet/ip_state.h>
31 #include <netinet/ip_nat.h>
32 #include <netinet/ip_auth.h>
33 #include <netinet/ip_frag.h>
34 #include <netinet/ip_sync.h>
35
36 #if __FreeBSD_version >= 502116
37 static struct cdev *ipf_devs[IPL_LOGSIZE];
38 #else
39 static dev_t ipf_devs[IPL_LOGSIZE];
40 #endif
41
42 static int sysctl_ipf_int ( SYSCTL_HANDLER_ARGS );
43 static int ipf_modload(void);
44 static int ipf_modunload(void);
45
46 SYSCTL_DECL(_net_inet);
47 #define SYSCTL_IPF(parent, nbr, name, access, ptr, val, descr) \
48         SYSCTL_OID(parent, nbr, name, CTLTYPE_INT|access, \
49                    ptr, val, sysctl_ipf_int, "I", descr);
50 #define CTLFLAG_OFF     0x00800000      /* IPFilter must be disabled */
51 #define CTLFLAG_RWO     (CTLFLAG_RW|CTLFLAG_OFF)
52 SYSCTL_NODE(_net_inet, OID_AUTO, ipf, CTLFLAG_RW, 0, "IPF");
53 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_flags, CTLFLAG_RW, &fr_flags, 0, "");
54 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_pass, CTLFLAG_RW, &fr_pass, 0, "");
55 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_active, CTLFLAG_RD, &fr_active, 0, "");
56 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcpidletimeout, CTLFLAG_RWO,
57            &fr_tcpidletimeout, 0, "");
58 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcphalfclosed, CTLFLAG_RWO,
59            &fr_tcphalfclosed, 0, "");
60 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcpclosewait, CTLFLAG_RWO,
61            &fr_tcpclosewait, 0, "");
62 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcplastack, CTLFLAG_RWO,
63            &fr_tcplastack, 0, "");
64 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcptimeout, CTLFLAG_RWO,
65            &fr_tcptimeout, 0, "");
66 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_tcpclosed, CTLFLAG_RWO,
67            &fr_tcpclosed, 0, "");
68 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_udptimeout, CTLFLAG_RWO,
69            &fr_udptimeout, 0, "");
70 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_udpacktimeout, CTLFLAG_RWO,
71            &fr_udpacktimeout, 0, "");
72 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_icmptimeout, CTLFLAG_RWO,
73            &fr_icmptimeout, 0, "");
74 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_defnatage, CTLFLAG_RWO,
75            &fr_defnatage, 0, "");
76 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_ipfrttl, CTLFLAG_RW,
77            &fr_ipfrttl, 0, "");
78 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_running, CTLFLAG_RD,
79            &fr_running, 0, "");
80 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_statesize, CTLFLAG_RWO,
81            &fr_statesize, 0, "");
82 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_statemax, CTLFLAG_RWO,
83            &fr_statemax, 0, "");
84 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, ipf_nattable_sz, CTLFLAG_RWO,
85            &ipf_nattable_sz, 0, "");
86 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, ipf_natrules_sz, CTLFLAG_RWO,
87            &ipf_natrules_sz, 0, "");
88 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, ipf_rdrrules_sz, CTLFLAG_RWO,
89            &ipf_rdrrules_sz, 0, "");
90 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, ipf_hostmap_sz, CTLFLAG_RWO,
91            &ipf_hostmap_sz, 0, "");
92 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_authsize, CTLFLAG_RWO,
93            &fr_authsize, 0, "");
94 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_authused, CTLFLAG_RD,
95            &fr_authused, 0, "");
96 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_defaultauthage, CTLFLAG_RW,
97            &fr_defaultauthage, 0, "");
98 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_chksrc, CTLFLAG_RW, &fr_chksrc, 0, "");
99 SYSCTL_IPF(_net_inet_ipf, OID_AUTO, fr_minttl, CTLFLAG_RW, &fr_minttl, 0, "");
100
101 #define CDEV_MAJOR 79
102 #include <sys/poll.h>
103 #if __FreeBSD_version >= 500043
104 # include <sys/select.h>
105 static int iplpoll(struct cdev *dev, int events, struct thread *td);
106
107 static struct cdevsw ipl_cdevsw = {
108 # if __FreeBSD_version >= 502103
109         .d_version =    D_VERSION,
110         .d_flags =      0,      /* D_NEEDGIANT - Should be SMP safe */
111 # endif
112         .d_open =       iplopen,
113         .d_close =      iplclose,
114         .d_read =       iplread,
115         .d_write =      iplwrite,
116         .d_ioctl =      iplioctl,
117         .d_name =       "ipl",
118 # if __FreeBSD_version >= 500043
119         .d_poll =       iplpoll,
120 # endif
121 # if __FreeBSD_version < 600000
122         .d_maj =        CDEV_MAJOR,
123 # endif
124 };
125 #else
126 static int iplpoll(dev_t dev, int events, struct proc *p);
127
128 static struct cdevsw ipl_cdevsw = {
129         /* open */      iplopen,
130         /* close */     iplclose,
131         /* read */      iplread,
132         /* write */     iplwrite,
133         /* ioctl */     iplioctl,
134         /* poll */      iplpoll,
135         /* mmap */      nommap,
136         /* strategy */  nostrategy,
137         /* name */      "ipl",
138         /* maj */       CDEV_MAJOR,
139         /* dump */      nodump,
140         /* psize */     nopsize,
141         /* flags */     0,
142 # if (__FreeBSD_version < 500043)
143         /* bmaj */      -1,
144 # endif
145 # if (__FreeBSD_version > 430000)
146         /* kqfilter */  NULL
147 # endif
148 };
149 #endif
150
151 static char *ipf_devfiles[] = { IPL_NAME, IPNAT_NAME, IPSTATE_NAME, IPAUTH_NAME,
152                                 IPSYNC_NAME, IPSCAN_NAME, IPLOOKUP_NAME, NULL };
153
154
155 static int
156 ipfilter_modevent(module_t mod, int type, void *unused)
157 {
158         int error = 0;
159
160         switch (type)
161         {
162         case MOD_LOAD :
163                 error = ipf_modload();
164                 break;
165
166         case MOD_UNLOAD :
167                 error = ipf_modunload();
168                 break;
169         default:
170                 error = EINVAL;
171                 break;
172         }
173         return error;
174 }
175
176
177 static int
178 ipf_modload()
179 {
180         char *defpass, *c, *str;
181         int i, j, error;
182
183         RWLOCK_INIT(&ipf_global, "ipf filter load/unload mutex");
184         RWLOCK_INIT(&ipf_mutex, "ipf filter rwlock");
185         RWLOCK_INIT(&ipf_frcache, "ipf cache rwlock");
186
187         error = ipfattach();
188         if (error) {
189                 RW_DESTROY(&ipf_global);
190                 RW_DESTROY(&ipf_mutex);
191                 RW_DESTROY(&ipf_frcache);
192                 return error;
193         }
194
195         for (i = 0; i < IPL_LOGSIZE; i++)
196                 ipf_devs[i] = NULL;
197
198         for (i = 0; (str = ipf_devfiles[i]); i++) {
199                 c = NULL;
200                 for(j = strlen(str); j > 0; j--)
201                         if (str[j] == '/') {
202                                 c = str + j + 1;
203                                 break;
204                         }
205                 if (!c)
206                         c = str;
207                 ipf_devs[i] = make_dev(&ipl_cdevsw, i, 0, 0, 0600, c);
208         }
209
210         if (FR_ISPASS(fr_pass))
211                 defpass = "pass";
212         else if (FR_ISBLOCK(fr_pass))
213                 defpass = "block";
214         else          
215                 defpass = "no-match -> block";
216
217         printf("%s initialized.  Default = %s all, Logging = %s%s\n",
218                 ipfilter_version, defpass,                
219 #ifdef IPFILTER_LOG
220                 "enabled",
221 #else
222                 "disabled",
223 #endif
224 #ifdef IPFILTER_COMPILED
225                 " (COMPILED)"
226 #else
227                 ""
228 #endif
229                 );         
230         return 0;
231 }
232
233
234 static int
235 ipf_modunload()
236 {
237         int error, i;
238
239         if (fr_refcnt)
240                 return EBUSY;
241
242         if (fr_running >= 0) {
243                 error = ipfdetach();
244                 if (error != 0)
245                         return error;
246         } else
247                 error = 0;
248
249         RW_DESTROY(&ipf_global);
250         RW_DESTROY(&ipf_mutex);
251         RW_DESTROY(&ipf_frcache);
252
253         fr_running = -2;
254
255         for (i = 0; ipf_devfiles[i]; i++) {
256                 if (ipf_devs[i] != NULL)
257                         destroy_dev(ipf_devs[i]);
258         }
259
260         printf("%s unloaded\n", ipfilter_version);
261
262         return error;
263 }
264
265
266 static moduledata_t ipfiltermod = {
267         "ipfilter",
268         ipfilter_modevent,
269         0
270 };
271
272
273 DECLARE_MODULE(ipfilter, ipfiltermod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
274 #ifdef  MODULE_VERSION
275 MODULE_VERSION(ipfilter, 1);
276 #endif
277
278
279 #ifdef SYSCTL_IPF
280 int
281 sysctl_ipf_int ( SYSCTL_HANDLER_ARGS )
282 {
283         int error = 0;
284
285         if (arg1)
286                 error = SYSCTL_OUT(req, arg1, sizeof(int));
287         else
288                 error = SYSCTL_OUT(req, &arg2, sizeof(int));
289
290         if (error || !req->newptr)
291                 return (error);
292
293         if (!arg1)
294                 error = EPERM;
295         else {
296                 if ((oidp->oid_kind & CTLFLAG_OFF) && (fr_running > 0))
297                         error = EBUSY;
298                 else
299                         error = SYSCTL_IN(req, arg1, sizeof(int));
300         }
301         return (error);
302 }
303 #endif
304
305
306 static int
307 #if __FreeBSD_version >= 500043
308 iplpoll(struct cdev *dev, int events, struct thread *td)
309 #else
310 iplpoll(dev_t dev, int events, struct proc *td)
311 #endif
312 {
313         u_int xmin = GET_MINOR(dev);
314         int revents;
315
316         if (xmin < 0 || xmin > IPL_LOGMAX)
317                 return 0;
318
319         revents = 0;
320
321         switch (xmin) 
322         {
323         case IPL_LOGIPF :
324         case IPL_LOGNAT :
325         case IPL_LOGSTATE :
326 #ifdef IPFILTER_LOG
327                 if ((events & (POLLIN | POLLRDNORM)) && ipflog_canread(xmin))
328                         revents |= events & (POLLIN | POLLRDNORM);
329 #endif  
330                 break;
331         case IPL_LOGAUTH :
332                 if ((events & (POLLIN | POLLRDNORM)) && fr_auth_waiting())
333                         revents |= events & (POLLIN | POLLRDNORM);
334                 break; 
335         case IPL_LOGSYNC :
336 #ifdef IPFILTER_SYNC
337                 if ((events & (POLLIN | POLLRDNORM)) && ipfsync_canread())
338                         revents |= events & (POLLIN | POLLRDNORM);
339                 if ((events & (POLLOUT | POLLWRNORM)) && ipfsync_canwrite())
340                         revents |= events & (POLLOUT | POLLWRNORM);
341 #endif
342                 break;
343         case IPL_LOGSCAN :
344         case IPL_LOGLOOKUP :
345         default :
346                 break;
347         }
348
349         if ((revents == 0) && ((events & (POLLIN|POLLRDNORM)) != 0))
350                 selrecord(td, &ipfselwait[xmin]);
351
352         return revents;
353 }