2 * Copyright (C) 1997 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 1.4 1998/06/20 18:37:50 peter Exp $";
12 #if defined(KERNEL) && !defined(_KERNEL)
15 #define __FreeBSD_version 300000 /* just a hack - no <sys/osreldate.h> */
17 #if !defined(_KERNEL) && !defined(KERNEL)
21 #include <sys/errno.h>
22 #include <sys/types.h>
23 #include <sys/param.h>
26 #if defined(KERNEL) && (__FreeBSD_version >= 220000)
27 # include <sys/filio.h>
28 # include <sys/fcntl.h>
30 # include <sys/ioctl.h>
34 # include <sys/protosw.h>
36 #include <sys/socket.h>
37 #if defined(_KERNEL) && !defined(linux)
38 # include <sys/systm.h>
40 #if !defined(__SVR4) && !defined(__svr4__)
42 # include <sys/mbuf.h>
45 # include <sys/filio.h>
46 # include <sys/byteorder.h>
47 # include <sys/dditypes.h>
48 # include <sys/stream.h>
49 # include <sys/kmem.h>
51 #if defined(KERNEL) && (__FreeBSD_version >= 300000)
52 # include <sys/malloc.h>
54 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
55 # include <machine/cpu.h>
61 #if !defined(KERNEL) && (__FreeBSD_version >= 300000)
62 # include <net/if_var.h>
64 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/ip.h>
73 # include <netinet/ip_var.h>
79 # ifdef IFF_DRVRLOCK /* IRIX6 */
80 #include <sys/hashing.h>
83 #include <netinet/tcp.h>
84 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
85 extern struct ifqueue ipintrq; /* ip packet input queue */
88 # include <netinet/in_var.h>
89 # include <netinet/tcp_fsm.h>
92 #include <netinet/udp.h>
93 #include <netinet/ip_icmp.h>
94 #include "netinet/ip_compat.h"
95 #include <netinet/tcpip.h>
96 #include "netinet/ip_fil.h"
97 #include "netinet/ip_auth.h"
98 #if !SOLARIS && !defined(linux)
99 # include <net/netisr.h>
101 # include <machine/cpufunc.h>
106 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
107 extern kmutex_t ipf_auth;
109 extern kcondvar_t ipfauthwait;
113 static struct wait_queue *ipfauthwait = NULL;
116 int fr_authsize = FR_NUMAUTH;
118 int fr_defaultauthage = 600;
119 fr_authstat_t fr_authstats;
120 frauth_t fr_auth[FR_NUMAUTH];
121 mb_t *fr_authpkts[FR_NUMAUTH];
122 int fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
123 frauthent_t *fae_list = NULL;
124 frentry_t *ipauth = NULL;
128 * Check if a packet has authorization. If the packet is found to match an
129 * authorization result and that would result in a feedback loop (i.e. it
130 * will end up returning FR_AUTH) then return FR_BLOCK instead.
132 int fr_checkauth(ip, fin)
136 u_short id = ip->ip_id;
140 MUTEX_ENTER(&ipf_auth);
141 for (i = fr_authstart; i != fr_authend; ) {
143 * index becomes -2 only after an SIOCAUTHW. Check this in
144 * case the same packet gets sent again and it hasn't yet been
147 if ((fr_auth[i].fra_index == -2) &&
148 (id == fr_auth[i].fra_info.fin_id) &&
149 !bcmp((char *)fin,(char *)&fr_auth[i].fra_info,FI_CSIZE)) {
151 * Avoid feedback loop.
153 if (!(pass = fr_auth[i].fra_pass) || (pass & FR_AUTH))
155 fr_authstats.fas_hits++;
156 fr_auth[i].fra_index = -1;
158 if (i == fr_authstart) {
159 while (fr_auth[i].fra_index == -1) {
167 if (fr_authstart == fr_authend) {
169 fr_authstart = fr_authend = 0;
172 MUTEX_EXIT(&ipf_auth);
179 fr_authstats.fas_miss++;
180 MUTEX_EXIT(&ipf_auth);
186 * Check if we have room in the auth array to hold details for another packet.
187 * If we do, store it and wake up any user programs which are waiting to
188 * hear about these events.
190 int fr_newauth(m, fin, ip
191 #if defined(_KERNEL) && SOLARIS
203 MUTEX_ENTER(&ipf_auth);
204 if ((fr_authstart > fr_authend) && (fr_authstart - fr_authend == -1)) {
205 fr_authstats.fas_nospace++;
206 MUTEX_EXIT(&ipf_auth);
209 if (fr_authend - fr_authstart == FR_NUMAUTH - 1) {
210 fr_authstats.fas_nospace++;
211 MUTEX_EXIT(&ipf_auth);
215 fr_authstats.fas_added++;
218 if (fr_authend == FR_NUMAUTH)
220 MUTEX_EXIT(&ipf_auth);
221 fr_auth[i].fra_index = i;
222 fr_auth[i].fra_pass = 0;
223 fr_auth[i].fra_age = fr_defaultauthage;
224 bcopy((char *)fin, (char *)&fr_auth[i].fra_info, sizeof(*fin));
225 #if !defined(sparc) && !defined(m68k)
227 * No need to copyback here as we want to undo the changes, not keep
230 # if SOLARIS && defined(_KERNEL)
231 if (ip == (ip_t *)m->b_rptr)
237 ip->ip_len = htons(bo);
238 # if !SOLARIS /* 4.4BSD converts this ip_input.c, but I don't in solaris.c */
240 ip->ip_id = htons(bo);
243 ip->ip_off = htons(bo);
246 #if SOLARIS && defined(_KERNEL)
247 m->b_rptr -= qif->qf_off;
248 fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
249 fr_auth[i].fra_q = qif->qf_q;
250 cv_signal(&ipfauthwait);
253 # if defined(linux) && defined(_KERNEL)
254 wake_up_interruptible(&ipfauthwait);
256 WAKEUP(&fr_authnext);
263 int fr_auth_ioctl(data, cmd, fr, frptr)
265 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
270 frentry_t *fr, **frptr;
279 frauth_t auth, *au = &auth;
280 frauthent_t *fae, **faep;
293 for (faep = &fae_list; (fae = *faep); )
294 if (&fae->fae_fr == fr)
297 faep = &fae->fae_next;
298 if (cmd == SIOCRMAFR) {
302 *faep = fae->fae_next;
303 *frptr = fr->fr_next;
307 KMALLOC(fae, frauthent_t *, sizeof(*fae));
309 IRCOPY((char *)data, (char *)&fae->fae_fr,
310 sizeof(fae->fae_fr));
312 fae->fae_age = fr_defaultauthage;
313 fae->fae_fr.fr_hits = 0;
314 fae->fae_fr.fr_next = *frptr;
315 *frptr = &fae->fae_fr;
316 fae->fae_next = *faep;
323 IWCOPY((char *)&fr_authstats, data, sizeof(fr_authstats));
327 MUTEX_ENTER(&ipf_auth);
328 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
329 IWCOPY((char *)&fr_auth[fr_authnext++], data,
331 if (fr_authnext == FR_NUMAUTH)
333 MUTEX_EXIT(&ipf_auth);
338 if (!cv_wait_sig(&ipfauthwait, &ipf_auth)) {
339 mutex_exit(&ipf_auth);
344 interruptible_sleep_on(&ipfauthwait);
345 if (current->signal & ~current->blocked)
348 error = SLEEP(&fr_authnext, "fr_authnext");
352 MUTEX_EXIT(&ipf_auth);
354 goto fr_authioctlloop;
357 IRCOPY(data, (caddr_t)&auth, sizeof(auth));
358 MUTEX_ENTER(&ipf_auth);
360 if ((i < 0) || (i > FR_NUMAUTH) ||
361 (fr_auth[i].fra_info.fin_id != au->fra_info.fin_id)) {
362 MUTEX_EXIT(&ipf_auth);
366 fr_auth[i].fra_index = -2;
367 fr_auth[i].fra_pass = au->fra_pass;
368 fr_authpkts[i] = NULL;
370 MUTEX_EXIT(&ipf_auth);
373 if (m && au->fra_info.fin_out) {
375 error = fr_qout(fr_auth[i].fra_q, m);
377 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL);
378 # endif /* SOLARIS */
380 fr_authstats.fas_sendfail++;
382 fr_authstats.fas_sendok++;
385 error = fr_qin(fr_auth[i].fra_q, m);
394 schednetisr(NETISR_IP);
396 # endif /* SOLARIS */
398 fr_authstats.fas_quefail++;
400 fr_authstats.fas_queok++;
409 * If we experience an error which will result in the packet
410 * not being processed, make sure we advance to the next one.
412 if (error == ENOBUFS) {
414 fr_auth[i].fra_index = -1;
415 fr_auth[i].fra_pass = 0;
416 if (i == fr_authstart) {
417 while (fr_auth[i].fra_index == -1) {
425 if (fr_authstart == fr_authend) {
427 fr_authstart = fr_authend = 0;
445 * Free all network buffer memory used to keep saved packets.
450 register frauthent_t *fae, **faep;
453 MUTEX_ENTER(&ipf_auth);
454 for (i = 0; i < FR_NUMAUTH; i++) {
455 if ((m = fr_authpkts[i])) {
457 fr_authpkts[i] = NULL;
458 fr_auth[i].fra_index = -1;
463 for (faep = &fae_list; (fae = *faep); ) {
464 *faep = fae->fae_next;
467 MUTEX_EXIT(&ipf_auth);
472 * Slowly expire held auth records. Timeouts are set
473 * in expectation of this being called twice per second.
478 register frauth_t *fra;
479 register frauthent_t *fae, **faep;
486 MUTEX_ENTER(&ipf_auth);
487 for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
488 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
490 fr_authpkts[i] = NULL;
491 fr_auth[i].fra_index = -1;
492 fr_authstats.fas_expire++;
497 for (faep = &fae_list; (fae = *faep); ) {
498 if (!--fra->fra_age) {
499 *faep = fae->fae_next;
501 fr_authstats.fas_expire++;
503 faep = &fae->fae_next;
505 MUTEX_EXIT(&ipf_auth);