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