2 * Copyright (C) 1998-2001 by Darren Reed & Guido van Rooij.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * @(#)$Id: ip_auth.c,v 2.11.2.20 2002/06/04 14:40:42 darrenr Exp $
7 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_auth.c,v 1.21.2.7 2003/03/01 03:55:54 darrenr Exp $
8 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_auth.c,v 1.3 2003/07/26 14:07:18 rob Exp $
10 #if defined(__sgi) && (IRIX > 602)
11 # include <sys/ptimers.h>
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
18 #if !defined(_KERNEL) && !defined(KERNEL)
23 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
24 # include <sys/filio.h>
25 # include <sys/fcntl.h>
27 # include <sys/ioctl.h>
30 # include <sys/protosw.h>
32 #include <sys/socket.h>
33 #if (defined(_KERNEL) || 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) || (__FreeBSD_version >= 400000)
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>
110 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
111 extern KRWLOCK_T ipf_auth, ipf_mutex;
112 extern kmutex_t ipf_authmx;
114 extern kcondvar_t ipfauthwait;
118 static struct wait_queue *ipfauthwait = NULL;
121 int fr_authsize = FR_NUMAUTH;
123 int fr_defaultauthage = 600;
124 int fr_auth_lock = 0;
125 fr_authstat_t fr_authstats;
126 static frauth_t fr_auth[FR_NUMAUTH];
127 mb_t *fr_authpkts[FR_NUMAUTH];
128 static int fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
129 static frauthent_t *fae_list = NULL;
130 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;
149 if (fr_auth_lock || !fr_authused)
152 READ_ENTER(&ipf_auth);
153 for (i = fr_authstart; i != fr_authend; ) {
155 * index becomes -2 only after an SIOCAUTHW. Check this in
156 * case the same packet gets sent again and it hasn't yet been
160 if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
161 !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
163 * Avoid feedback loop.
165 if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
168 * Create a dummy rule for the stateful checking to
169 * use and return. Zero out any values we don't
170 * trust from userland!
172 if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
173 (fin->fin_fi.fi_fl & FI_FRAG))) {
174 KMALLOC(fr, frentry_t *);
176 bcopy((char *)fra->fra_info.fin_fr,
179 fr->fr_ifa = fin->fin_ifp;
188 fr = fra->fra_info.fin_fr;
190 RWLOCK_EXIT(&ipf_auth);
191 WRITE_ENTER(&ipf_auth);
192 if (fr && fr != fra->fra_info.fin_fr) {
193 fr->fr_next = fr_authlist;
196 fr_authstats.fas_hits++;
199 if (i == fr_authstart) {
200 while (fra->fra_index == -1) {
203 if (i == FR_NUMAUTH) {
211 if (fr_authstart == fr_authend) {
213 fr_authstart = fr_authend = 0;
216 RWLOCK_EXIT(&ipf_auth);
223 fr_authstats.fas_miss++;
224 RWLOCK_EXIT(&ipf_auth);
230 * Check if we have room in the auth array to hold details for another packet.
231 * If we do, store it and wake up any user programs which are waiting to
232 * hear about these events.
234 int fr_newauth(m, fin, ip)
239 #if defined(_KERNEL) && SOLARIS
240 qif_t *qif = fin->fin_qif;
248 WRITE_ENTER(&ipf_auth);
249 if (fr_authstart > fr_authend) {
250 fr_authstats.fas_nospace++;
251 RWLOCK_EXIT(&ipf_auth);
254 if (fr_authused == FR_NUMAUTH) {
255 fr_authstats.fas_nospace++;
256 RWLOCK_EXIT(&ipf_auth);
261 fr_authstats.fas_added++;
264 if (fr_authend == FR_NUMAUTH)
266 RWLOCK_EXIT(&ipf_auth);
270 fra->fra_age = fr_defaultauthage;
271 bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
272 #if SOLARIS && defined(_KERNEL)
275 * No need to copyback here as we want to undo the changes, not keep
278 if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
283 ip->ip_len = htons(bo);
285 ip->ip_off = htons(bo);
288 m->b_rptr -= qif->qf_off;
289 fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
290 fra->fra_q = qif->qf_q;
291 cv_signal(&ipfauthwait);
293 # if defined(BSD) && !defined(sparc) && (BSD >= 199306)
294 if (fin->fin_out == 0) {
295 ip->ip_len = htons(ip->ip_len);
296 ip->ip_off = htons(ip->ip_off);
300 WAKEUP(&fr_authnext);
306 int fr_auth_ioctl(data, mode, cmd)
309 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
316 #if defined(_KERNEL) && !SOLARIS
320 frauth_t auth, *au = &auth, *fra;
326 if (!(mode & FWRITE)) {
330 error = fr_lock(data, &fr_auth_lock);
342 /* These commands go via request to fr_preauthcmd */
346 fr_authstats.fas_faelist = fae_list;
347 error = IWCOPYPTR((char *)&fr_authstats, data,
348 sizeof(fr_authstats));
351 if (!(mode & FWRITE)) {
356 READ_ENTER(&ipf_auth);
357 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
358 error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
360 RWLOCK_EXIT(&ipf_auth);
363 WRITE_ENTER(&ipf_auth);
366 if (fr_authnext == FR_NUMAUTH)
369 RWLOCK_EXIT(&ipf_auth);
372 RWLOCK_EXIT(&ipf_auth);
375 mutex_enter(&ipf_authmx);
376 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
377 mutex_exit(&ipf_authmx);
380 mutex_exit(&ipf_authmx);
382 error = SLEEP(&fr_authnext, "fr_authnext");
386 goto fr_authioctlloop;
389 if (!(mode & FWRITE)) {
393 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
396 WRITE_ENTER(&ipf_auth);
400 if ((i < 0) || (i > FR_NUMAUTH) ||
401 (fra->fra_info.fin_id != au->fra_info.fin_id)) {
403 RWLOCK_EXIT(&ipf_auth);
408 fra->fra_pass = au->fra_pass;
409 fr_authpkts[i] = NULL;
410 RWLOCK_EXIT(&ipf_auth);
412 if (m && au->fra_info.fin_out) {
414 error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
418 bzero((char *)&ro, sizeof(ro));
419 # if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
420 defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605)) || \
421 (__FreeBSD_version >= 470102)
422 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
425 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
430 # endif /* SOLARIS */
432 fr_authstats.fas_sendfail++;
434 fr_authstats.fas_sendok++;
437 error = (fr_qin(fra->fra_q, m) == 0) ? EINVAL : 0;
447 schednetisr(NETISR_IP);
450 # endif /* SOLARIS */
452 fr_authstats.fas_quefail++;
454 fr_authstats.fas_queok++;
462 * If we experience an error which will result in the packet
463 * not being processed, make sure we advance to the next one.
465 if (error == ENOBUFS) {
469 if (i == fr_authstart) {
470 while (fra->fra_index == -1) {
478 if (fr_authstart == fr_authend) {
480 fr_authstart = fr_authend = 0;
497 * Free all network buffer memory used to keep saved packets.
502 frauthent_t *fae, **faep;
503 frentry_t *fr, **frp;
506 WRITE_ENTER(&ipf_auth);
507 for (i = 0; i < FR_NUMAUTH; i++) {
508 if ((m = fr_authpkts[i])) {
510 fr_authpkts[i] = NULL;
511 fr_auth[i].fra_index = -1;
516 for (faep = &fae_list; (fae = *faep); ) {
517 *faep = fae->fae_next;
521 RWLOCK_EXIT(&ipf_auth);
525 * We *MuST* reget ipf_auth because otherwise we won't get the
526 * locks in the right order and risk deadlock.
527 * We need ipf_mutex here to prevent a rule from using it
530 WRITE_ENTER(&ipf_mutex);
531 WRITE_ENTER(&ipf_auth);
532 for (frp = &fr_authlist; (fr = *frp); ) {
533 if (fr->fr_ref == 1) {
539 RWLOCK_EXIT(&ipf_auth);
540 RWLOCK_EXIT(&ipf_mutex);
546 * Slowly expire held auth records. Timeouts are set
547 * in expectation of this being called twice per second.
553 frauthent_t *fae, **faep;
554 frentry_t *fr, **frp;
556 #if !SOLARIS && defined(_KERNEL)
564 WRITE_ENTER(&ipf_auth);
565 for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
566 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
568 fr_authpkts[i] = NULL;
569 fr_auth[i].fra_index = -1;
570 fr_authstats.fas_expire++;
575 for (faep = &fae_list; (fae = *faep); ) {
576 if (!--fae->fae_age) {
577 *faep = fae->fae_next;
579 fr_authstats.fas_expire++;
581 faep = &fae->fae_next;
583 if (fae_list != NULL)
584 ipauth = &fae_list->fae_fr;
588 for (frp = &fr_authlist; (fr = *frp); ) {
589 if (fr->fr_ref == 1) {
595 RWLOCK_EXIT(&ipf_auth);
599 int fr_preauthcmd(cmd, fr, frptr)
600 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
601 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
606 frentry_t *fr, **frptr;
608 frauthent_t *fae, **faep;
610 #if defined(KERNEL) && !SOLARIS
614 if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
615 /* Should not happen */
616 printf("fr_preauthcmd called with bad cmd 0x%lx", (u_long)cmd);
620 for (faep = &fae_list; (fae = *faep); )
621 if (&fae->fae_fr == fr)
624 faep = &fae->fae_next;
625 if (cmd == SIOCRMAFR) {
631 WRITE_ENTER(&ipf_auth);
633 *faep = fae->fae_next;
634 *frptr = fr->fr_next;
636 RWLOCK_EXIT(&ipf_auth);
639 } else if (fr && frptr) {
640 KMALLOC(fae, frauthent_t *);
642 bcopy((char *)fr, (char *)&fae->fae_fr,
644 WRITE_ENTER(&ipf_auth);
646 fae->fae_age = fr_defaultauthage;
647 fae->fae_fr.fr_hits = 0;
648 fae->fae_fr.fr_next = *frptr;
649 *frptr = &fae->fae_fr;
650 fae->fae_next = *faep;
652 ipauth = &fae_list->fae_fr;
654 RWLOCK_EXIT(&ipf_auth);