2 * Copyright (C) 1998-2001 by Darren Reed & Guido van Rooij.
4 * See the IPFILTER.LICENCE file for details on licencing.
7 # include <sys/ptimers.h>
10 #include <sys/types.h>
11 #include <sys/param.h>
14 #if !defined(_KERNEL) && !defined(KERNEL)
19 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
20 # include <sys/filio.h>
21 # include <sys/fcntl.h>
23 # include <sys/ioctl.h>
26 # include <sys/protosw.h>
28 #include <sys/socket.h>
29 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
30 # include <sys/systm.h>
32 #if !defined(__SVR4) && !defined(__svr4__)
34 # include <sys/mbuf.h>
37 # include <sys/filio.h>
38 # include <sys/byteorder.h>
40 # include <sys/dditypes.h>
42 # include <sys/stream.h>
43 # include <sys/kmem.h>
45 #if (_BSDI_VERSION >= 199802) || (__FreeBSD_version >= 400000)
46 # include <sys/queue.h>
48 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
49 # include <machine/cpu.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
64 # include <netinet/ip_var.h>
70 # ifdef IFF_DRVRLOCK /* IRIX6 */
71 # include <sys/hashing.h>
74 #include <netinet/tcp.h>
75 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
76 extern struct ifqueue ipintrq; /* ip packet input queue */
79 # if __FreeBSD_version >= 300000
80 # include <net/if_var.h>
82 # include <netinet/in_var.h>
83 # include <netinet/tcp_fsm.h>
86 #include <netinet/udp.h>
87 #include <netinet/ip_icmp.h>
88 #include "netinet/ip_compat.h"
89 #include <netinet/tcpip.h>
90 #include "netinet/ip_fil.h"
91 #include "netinet/ip_auth.h"
92 #if !SOLARIS && !defined(linux)
93 # include <net/netisr.h>
95 # include <machine/cpufunc.h>
98 #if (__FreeBSD_version >= 300000)
99 # include <sys/malloc.h>
100 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
101 # include <sys/libkern.h>
102 # include <sys/systm.h>
107 /* static const char rcsid[] = "@(#)$Id: ip_auth.c,v 2.11.2.12 2001/07/18 14:57:08 darrenr Exp $"; */
108 static const char rcsid[] = "@(#)$FreeBSD$";
112 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
113 extern KRWLOCK_T ipf_auth, ipf_mutex;
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 int fr_auth_lock = 0;
127 fr_authstat_t fr_authstats;
128 static frauth_t fr_auth[FR_NUMAUTH];
129 mb_t *fr_authpkts[FR_NUMAUTH];
130 static int fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
131 static frauthent_t *fae_list = NULL;
132 frentry_t *ipauth = NULL,
137 * Check if a packet has authorization. If the packet is found to match an
138 * authorization result and that would result in a feedback loop (i.e. it
139 * will end up returning FR_AUTH) then return FR_BLOCK instead.
141 u_32_t fr_checkauth(ip, fin)
145 u_short id = ip->ip_id;
151 if (fr_auth_lock || !fr_authused)
154 READ_ENTER(&ipf_auth);
155 for (i = fr_authstart; i != fr_authend; ) {
157 * index becomes -2 only after an SIOCAUTHW. Check this in
158 * case the same packet gets sent again and it hasn't yet been
162 if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
163 !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
165 * Avoid feedback loop.
167 if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
170 * Create a dummy rule for the stateful checking to
171 * use and return. Zero out any values we don't
172 * trust from userland!
174 if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
175 (fin->fin_fi.fi_fl & FI_FRAG))) {
176 KMALLOC(fr, frentry_t *);
178 bcopy((char *)fra->fra_info.fin_fr,
181 fr->fr_ifa = fin->fin_ifp;
190 fr = fra->fra_info.fin_fr;
192 RWLOCK_EXIT(&ipf_auth);
193 WRITE_ENTER(&ipf_auth);
194 if (fr && fr != fra->fra_info.fin_fr) {
195 fr->fr_next = fr_authlist;
198 fr_authstats.fas_hits++;
201 if (i == fr_authstart) {
202 while (fra->fra_index == -1) {
205 if (i == FR_NUMAUTH) {
213 if (fr_authstart == fr_authend) {
215 fr_authstart = fr_authend = 0;
218 RWLOCK_EXIT(&ipf_auth);
225 fr_authstats.fas_miss++;
226 RWLOCK_EXIT(&ipf_auth);
232 * Check if we have room in the auth array to hold details for another packet.
233 * If we do, store it and wake up any user programs which are waiting to
234 * hear about these events.
236 int fr_newauth(m, fin, ip)
241 #if defined(_KERNEL) && SOLARIS
242 qif_t *qif = fin->fin_qif;
250 WRITE_ENTER(&ipf_auth);
251 if (fr_authstart > fr_authend) {
252 fr_authstats.fas_nospace++;
253 RWLOCK_EXIT(&ipf_auth);
256 if (fr_authused == FR_NUMAUTH) {
257 fr_authstats.fas_nospace++;
258 RWLOCK_EXIT(&ipf_auth);
263 fr_authstats.fas_added++;
266 if (fr_authend == FR_NUMAUTH)
268 RWLOCK_EXIT(&ipf_auth);
272 fra->fra_age = fr_defaultauthage;
273 bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
274 #if SOLARIS && defined(_KERNEL)
277 * No need to copyback here as we want to undo the changes, not keep
280 if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
285 ip->ip_len = htons(bo);
286 # if !SOLARIS && !defined(__NetBSD__) && !defined(__FreeBSD__)
287 /* 4.4BSD converts this ip_input.c, but I don't in solaris.c */
289 ip->ip_id = htons(bo);
292 ip->ip_off = htons(bo);
295 m->b_rptr -= qif->qf_off;
296 fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
297 fra->fra_q = qif->qf_q;
298 cv_signal(&ipfauthwait);
300 # if defined(BSD) && !defined(sparc) && (BSD >= 199306)
302 ip->ip_len = htons(ip->ip_len);
303 ip->ip_off = htons(ip->ip_off);
307 WAKEUP(&fr_authnext);
313 int fr_auth_ioctl(data, mode, cmd, fr, frptr)
316 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
321 frentry_t *fr, **frptr;
324 #if defined(_KERNEL) && !SOLARIS
327 frauth_t auth, *au = &auth, *fra;
328 frauthent_t *fae, **faep;
334 error = fr_lock(data, &fr_auth_lock);
346 for (faep = &fae_list; (fae = *faep); )
347 if (&fae->fae_fr == fr)
350 faep = &fae->fae_next;
351 if (cmd == SIOCRMAFR) {
357 WRITE_ENTER(&ipf_auth);
359 *faep = fae->fae_next;
360 *frptr = fr->fr_next;
362 RWLOCK_EXIT(&ipf_auth);
365 } else if (fr && frptr) {
366 KMALLOC(fae, frauthent_t *);
368 bcopy((char *)fr, (char *)&fae->fae_fr,
370 WRITE_ENTER(&ipf_auth);
372 fae->fae_age = fr_defaultauthage;
373 fae->fae_fr.fr_hits = 0;
374 fae->fae_fr.fr_next = *frptr;
375 *frptr = &fae->fae_fr;
376 fae->fae_next = *faep;
378 ipauth = &fae_list->fae_fr;
380 RWLOCK_EXIT(&ipf_auth);
387 fr_authstats.fas_faelist = fae_list;
388 error = IWCOPYPTR((char *)&fr_authstats, data,
389 sizeof(fr_authstats));
392 if (!(mode & FWRITE)) {
397 READ_ENTER(&ipf_auth);
398 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
399 error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
401 RWLOCK_EXIT(&ipf_auth);
404 WRITE_ENTER(&ipf_auth);
407 if (fr_authnext == FR_NUMAUTH)
410 RWLOCK_EXIT(&ipf_auth);
415 mutex_enter(&ipf_authmx);
416 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
417 mutex_exit(&ipf_authmx);
420 mutex_exit(&ipf_authmx);
422 error = SLEEP(&fr_authnext, "fr_authnext");
425 RWLOCK_EXIT(&ipf_auth);
427 goto fr_authioctlloop;
430 if (!(mode & FWRITE)) {
434 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
437 WRITE_ENTER(&ipf_auth);
441 if ((i < 0) || (i > FR_NUMAUTH) ||
442 (fra->fra_info.fin_id != au->fra_info.fin_id)) {
444 RWLOCK_EXIT(&ipf_auth);
449 fra->fra_pass = au->fra_pass;
450 fr_authpkts[i] = NULL;
451 RWLOCK_EXIT(&ipf_auth);
453 if (m && au->fra_info.fin_out) {
455 error = fr_qout(fra->fra_q, m);
459 bzero((char *)&ro, sizeof(ro));
460 # if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
461 defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605))
462 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
465 error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
470 # endif /* SOLARIS */
472 fr_authstats.fas_sendfail++;
474 fr_authstats.fas_sendok++;
477 error = fr_qin(fra->fra_q, m);
479 if (! IF_HANDOFF(&ipintrq, m, NULL))
482 schednetisr(NETISR_IP);
483 # endif /* SOLARIS */
485 fr_authstats.fas_quefail++;
487 fr_authstats.fas_queok++;
495 * If we experience an error which will result in the packet
496 * not being processed, make sure we advance to the next one.
498 if (error == ENOBUFS) {
502 if (i == fr_authstart) {
503 while (fra->fra_index == -1) {
511 if (fr_authstart == fr_authend) {
513 fr_authstart = fr_authend = 0;
530 * Free all network buffer memory used to keep saved packets.
535 register frauthent_t *fae, **faep;
536 frentry_t *fr, **frp;
539 WRITE_ENTER(&ipf_auth);
540 for (i = 0; i < FR_NUMAUTH; i++) {
541 if ((m = fr_authpkts[i])) {
543 fr_authpkts[i] = NULL;
544 fr_auth[i].fra_index = -1;
549 for (faep = &fae_list; (fae = *faep); ) {
550 *faep = fae->fae_next;
554 RWLOCK_EXIT(&ipf_auth);
558 * We *MuST* reget ipf_auth because otherwise we won't get the
559 * locks in the right order and risk deadlock.
560 * We need ipf_mutex here to prevent a rule from using it
563 WRITE_ENTER(&ipf_mutex);
564 WRITE_ENTER(&ipf_auth);
565 for (frp = &fr_authlist; (fr = *frp); ) {
566 if (fr->fr_ref == 1) {
572 RWLOCK_EXIT(&ipf_auth);
573 RWLOCK_EXIT(&ipf_mutex);
579 * Slowly expire held auth records. Timeouts are set
580 * in expectation of this being called twice per second.
585 register frauth_t *fra;
586 register frauthent_t *fae, **faep;
587 register frentry_t *fr, **frp;
589 #if !SOLARIS && defined(_KERNEL)
597 WRITE_ENTER(&ipf_auth);
598 for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
599 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
601 fr_authpkts[i] = NULL;
602 fr_auth[i].fra_index = -1;
603 fr_authstats.fas_expire++;
608 for (faep = &fae_list; (fae = *faep); ) {
609 if (!--fae->fae_age) {
610 *faep = fae->fae_next;
612 fr_authstats.fas_expire++;
614 faep = &fae->fae_next;
616 ipauth = &fae_list->fae_fr;
618 for (frp = &fr_authlist; (fr = *frp); ) {
619 if (fr->fr_ref == 1) {
625 RWLOCK_EXIT(&ipf_auth);