2 * Copyright (C) 1998 by Darren Reed & Guido van Rooij.
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
9 static const char rcsid[] = "@(#)$Id: ip_auth.c,v 2.1.2.1 1999/09/28 11:44:04 darrenr Exp $";
12 #include <sys/errno.h>
13 #include <sys/types.h>
14 #include <sys/param.h>
17 #if !defined(_KERNEL) && !defined(KERNEL)
22 #if defined(KERNEL) && (__FreeBSD_version >= 220000)
23 # include <sys/filio.h>
24 # include <sys/fcntl.h>
26 # include <sys/ioctl.h>
30 # include <sys/protosw.h>
32 #include <sys/socket.h>
33 #if defined(_KERNEL) && !defined(linux)
34 # include <sys/systm.h>
36 #if !defined(__SVR4) && !defined(__svr4__)
38 # include <sys/mbuf.h>
41 # include <sys/filio.h>
42 # include <sys/byteorder.h>
44 # include <sys/dditypes.h>
46 # include <sys/stream.h>
47 # include <sys/kmem.h>
49 #if _BSDI_VERSION >= 199802
50 # include <sys/queue.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
53 # include <machine/cpu.h>
59 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
68 # include <netinet/ip_var.h>
74 # ifdef IFF_DRVRLOCK /* IRIX6 */
75 # include <sys/hashing.h>
78 #include <netinet/tcp.h>
79 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
80 extern struct ifqueue ipintrq; /* ip packet input queue */
83 # if __FreeBSD_version >= 300000
84 # include <net/if_var.h>
86 # include <netinet/in_var.h>
87 # include <netinet/tcp_fsm.h>
90 #include <netinet/udp.h>
91 #include <netinet/ip_icmp.h>
92 #include "netinet/ip_compat.h"
93 #include <netinet/tcpip.h>
94 #include "netinet/ip_fil.h"
95 #include "netinet/ip_auth.h"
96 #if !SOLARIS && !defined(linux)
97 # include <net/netisr.h>
99 # include <machine/cpufunc.h>
102 #if (__FreeBSD_version >= 300000)
103 # include <sys/malloc.h>
104 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
105 # include <sys/libkern.h>
106 # include <sys/systm.h>
112 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
113 extern KRWLOCK_T ipf_auth;
114 extern kmutex_t ipf_authmx;
116 extern kcondvar_t ipfauthwait;
120 static struct wait_queue *ipfauthwait = NULL;
123 int fr_authsize = FR_NUMAUTH;
125 int fr_defaultauthage = 600;
126 fr_authstat_t fr_authstats;
127 frauth_t fr_auth[FR_NUMAUTH];
128 mb_t *fr_authpkts[FR_NUMAUTH];
129 int fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
130 frauthent_t *fae_list = NULL;
131 frentry_t *ipauth = NULL;
135 * Check if a packet has authorization. If the packet is found to match an
136 * authorization result and that would result in a feedback loop (i.e. it
137 * will end up returning FR_AUTH) then return FR_BLOCK instead.
139 u_32_t fr_checkauth(ip, fin)
143 u_short id = ip->ip_id;
147 READ_ENTER(&ipf_auth);
148 for (i = fr_authstart; i != fr_authend; ) {
150 * index becomes -2 only after an SIOCAUTHW. Check this in
151 * case the same packet gets sent again and it hasn't yet been
154 if ((fr_auth[i].fra_index == -2) &&
155 (id == fr_auth[i].fra_info.fin_id) &&
156 !bcmp((char *)fin,(char *)&fr_auth[i].fra_info,FI_CSIZE)) {
158 * Avoid feedback loop.
160 if (!(pass = fr_auth[i].fra_pass) || (pass & FR_AUTH))
162 RWLOCK_EXIT(&ipf_auth);
163 WRITE_ENTER(&ipf_auth);
164 fr_authstats.fas_hits++;
165 fr_auth[i].fra_index = -1;
167 if (i == fr_authstart) {
168 while (fr_auth[i].fra_index == -1) {
176 if (fr_authstart == fr_authend) {
178 fr_authstart = fr_authend = 0;
181 RWLOCK_EXIT(&ipf_auth);
188 fr_authstats.fas_miss++;
189 RWLOCK_EXIT(&ipf_auth);
195 * Check if we have room in the auth array to hold details for another packet.
196 * If we do, store it and wake up any user programs which are waiting to
197 * hear about these events.
199 int fr_newauth(m, fin, ip
200 #if defined(_KERNEL) && SOLARIS
212 WRITE_ENTER(&ipf_auth);
213 if (fr_authstart > fr_authend) {
214 fr_authstats.fas_nospace++;
215 RWLOCK_EXIT(&ipf_auth);
218 if ((fr_authstart == 0) && (fr_authend == FR_NUMAUTH - 1)) {
219 fr_authstats.fas_nospace++;
220 RWLOCK_EXIT(&ipf_auth);
225 fr_authstats.fas_added++;
228 if (fr_authend == FR_NUMAUTH)
230 RWLOCK_EXIT(&ipf_auth);
231 fr_auth[i].fra_index = i;
232 fr_auth[i].fra_pass = 0;
233 fr_auth[i].fra_age = fr_defaultauthage;
234 bcopy((char *)fin, (char *)&fr_auth[i].fra_info, sizeof(*fin));
235 #if !defined(sparc) && !defined(m68k)
237 * No need to copyback here as we want to undo the changes, not keep
240 # if SOLARIS && defined(_KERNEL)
241 if (ip == (ip_t *)m->b_rptr)
247 ip->ip_len = htons(bo);
248 # if !SOLARIS /* 4.4BSD converts this ip_input.c, but I don't in solaris.c */
250 ip->ip_id = htons(bo);
253 ip->ip_off = htons(bo);
256 #if SOLARIS && defined(_KERNEL)
257 m->b_rptr -= qif->qf_off;
258 fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
259 fr_auth[i].fra_q = qif->qf_q;
260 cv_signal(&ipfauthwait);
263 # if defined(linux) && defined(_KERNEL)
264 wake_up_interruptible(&ipfauthwait);
266 WAKEUP(&fr_authnext);
273 int fr_auth_ioctl(data, cmd, fr, frptr)
275 #if defined(__NetBSD__) || defined(__OpenBSD__)
280 frentry_t *fr, **frptr;
289 frauth_t auth, *au = &auth;
290 frauthent_t *fae, **faep;
303 for (faep = &fae_list; (fae = *faep); )
304 if (&fae->fae_fr == fr)
307 faep = &fae->fae_next;
308 if (cmd == SIOCRMAFR) {
312 WRITE_ENTER(&ipf_auth);
313 *faep = fae->fae_next;
314 *frptr = fr->fr_next;
315 RWLOCK_EXIT(&ipf_auth);
319 KMALLOC(fae, frauthent_t *);
321 IRCOPY((char *)data, (char *)&fae->fae_fr,
322 sizeof(fae->fae_fr));
323 WRITE_ENTER(&ipf_auth);
324 fae->fae_age = fr_defaultauthage;
325 fae->fae_fr.fr_hits = 0;
326 fae->fae_fr.fr_next = *frptr;
327 *frptr = &fae->fae_fr;
328 fae->fae_next = *faep;
330 ipauth = &fae_list->fae_fr;
331 RWLOCK_EXIT(&ipf_auth);
337 READ_ENTER(&ipf_auth);
338 fr_authstats.fas_faelist = fae_list;
339 RWLOCK_EXIT(&ipf_auth);
340 IWCOPY((char *)&fr_authstats, data, sizeof(fr_authstats));
344 READ_ENTER(&ipf_auth);
345 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
346 IWCOPY((char *)&fr_auth[fr_authnext], data,
348 RWLOCK_EXIT(&ipf_auth);
349 WRITE_ENTER(&ipf_auth);
351 if (fr_authnext == FR_NUMAUTH)
353 RWLOCK_EXIT(&ipf_auth);
358 mutex_enter(&ipf_authmx);
359 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
360 mutex_exit(&ipf_authmx);
363 mutex_exit(&ipf_authmx);
366 interruptible_sleep_on(&ipfauthwait);
367 if (current->signal & ~current->blocked)
370 error = SLEEP(&fr_authnext, "fr_authnext");
374 RWLOCK_EXIT(&ipf_auth);
376 goto fr_authioctlloop;
379 IRCOPY(data, (caddr_t)&auth, sizeof(auth));
380 WRITE_ENTER(&ipf_auth);
382 if ((i < 0) || (i > FR_NUMAUTH) ||
383 (fr_auth[i].fra_info.fin_id != au->fra_info.fin_id)) {
384 RWLOCK_EXIT(&ipf_auth);
388 fr_auth[i].fra_index = -2;
389 fr_auth[i].fra_pass = au->fra_pass;
390 fr_authpkts[i] = NULL;
392 RWLOCK_EXIT(&ipf_auth);
395 if (m && au->fra_info.fin_out) {
397 error = fr_qout(fr_auth[i].fra_q, m);
399 # if _BSDI_VERSION >= 199802
400 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL,
403 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL);
405 # endif /* SOLARIS */
407 fr_authstats.fas_sendfail++;
409 fr_authstats.fas_sendok++;
412 error = fr_qin(fr_auth[i].fra_q, m);
421 schednetisr(NETISR_IP);
423 # endif /* SOLARIS */
425 fr_authstats.fas_quefail++;
427 fr_authstats.fas_queok++;
436 * If we experience an error which will result in the packet
437 * not being processed, make sure we advance to the next one.
439 if (error == ENOBUFS) {
441 fr_auth[i].fra_index = -1;
442 fr_auth[i].fra_pass = 0;
443 if (i == fr_authstart) {
444 while (fr_auth[i].fra_index == -1) {
452 if (fr_authstart == fr_authend) {
454 fr_authstart = fr_authend = 0;
472 * Free all network buffer memory used to keep saved packets.
477 register frauthent_t *fae, **faep;
480 WRITE_ENTER(&ipf_auth);
481 for (i = 0; i < FR_NUMAUTH; i++) {
482 if ((m = fr_authpkts[i])) {
484 fr_authpkts[i] = NULL;
485 fr_auth[i].fra_index = -1;
490 for (faep = &fae_list; (fae = *faep); ) {
491 *faep = fae->fae_next;
495 RWLOCK_EXIT(&ipf_auth);
500 * Slowly expire held auth records. Timeouts are set
501 * in expectation of this being called twice per second.
506 register frauth_t *fra;
507 register frauthent_t *fae, **faep;
514 WRITE_ENTER(&ipf_auth);
515 for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
516 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
518 fr_authpkts[i] = NULL;
519 fr_auth[i].fra_index = -1;
520 fr_authstats.fas_expire++;
525 for (faep = &fae_list; (fae = *faep); ) {
526 if (!--fae->fae_age) {
527 *faep = fae->fae_next;
529 fr_authstats.fas_expire++;
531 faep = &fae->fae_next;
533 ipauth = &fae_list->fae_fr;
534 RWLOCK_EXIT(&ipf_auth);