More register axe work (remove the register keyword)
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_auth.c
1 /*
2  * Copyright (C) 1998-2001 by Darren Reed & Guido van Rooij.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
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 $
9  */
10 #if defined(__sgi) && (IRIX > 602)
11 # include <sys/ptimers.h>
12 #endif
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
16 #include <sys/time.h>
17 #include <sys/file.h>
18 #if !defined(_KERNEL) && !defined(KERNEL)
19 # include <stdio.h>
20 # include <stdlib.h>
21 # include <string.h>
22 #endif
23 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
24 # include <sys/filio.h>
25 # include <sys/fcntl.h>
26 #else
27 # include <sys/ioctl.h>
28 #endif
29 #ifndef linux
30 # include <sys/protosw.h>
31 #endif
32 #include <sys/socket.h>
33 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
34 # include <sys/systm.h>
35 #endif
36 #if !defined(__SVR4) && !defined(__svr4__)
37 # ifndef linux
38 #  include <sys/mbuf.h>
39 # endif
40 #else
41 # include <sys/filio.h>
42 # include <sys/byteorder.h>
43 # ifdef _KERNEL
44 #  include <sys/dditypes.h>
45 # endif
46 # include <sys/stream.h>
47 # include <sys/kmem.h>
48 #endif
49 #if (_BSDI_VERSION >= 199802) || (__FreeBSD_version >= 400000)
50 # include <sys/queue.h>
51 #endif
52 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
53 # include <machine/cpu.h>
54 #endif
55 #include <net/if.h>
56 #ifdef sun
57 # include <net/af.h>
58 #endif
59 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #ifndef KERNEL
64 # define        KERNEL
65 # define        NOT_KERNEL
66 #endif
67 #ifndef linux
68 # include <netinet/ip_var.h>
69 #endif
70 #ifdef  NOT_KERNEL
71 # undef KERNEL
72 #endif
73 #ifdef __sgi
74 # ifdef IFF_DRVRLOCK /* IRIX6 */
75 #  include <sys/hashing.h>
76 # endif
77 #endif
78 #include <netinet/tcp.h>
79 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
80 extern struct ifqueue   ipintrq;                /* ip packet input queue */
81 #else
82 # ifndef linux
83 #  if __FreeBSD_version >= 300000
84 #   include <net/if_var.h>
85 #  endif
86 #  include <netinet/in_var.h>
87 #  include <netinet/tcp_fsm.h>
88 # endif
89 #endif
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>
98 # ifdef __FreeBSD__
99 #  include <machine/cpufunc.h>
100 # endif
101 #endif
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>
107 # endif
108 #endif
109
110 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
111 extern KRWLOCK_T ipf_auth, ipf_mutex;
112 extern kmutex_t ipf_authmx;
113 # if SOLARIS
114 extern kcondvar_t ipfauthwait;
115 # endif
116 #endif
117 #ifdef linux
118 static struct wait_queue *ipfauthwait = NULL;
119 #endif
120
121 int     fr_authsize = FR_NUMAUTH;
122 int     fr_authused = 0;
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,
131                 *fr_authlist = NULL;
132
133
134 /*
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.
138  */
139 u_32_t fr_checkauth(ip, fin)
140 ip_t *ip;
141 fr_info_t *fin;
142 {
143         u_short id = ip->ip_id;
144         frentry_t *fr;
145         frauth_t *fra;
146         u_32_t pass;
147         int i;
148
149         if (fr_auth_lock || !fr_authused)
150                 return 0;
151
152         READ_ENTER(&ipf_auth);
153         for (i = fr_authstart; i != fr_authend; ) {
154                 /*
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
157                  * auth'd.
158                  */
159                 fra = fr_auth + i;
160                 if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
161                     !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
162                         /*
163                          * Avoid feedback loop.
164                          */
165                         if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
166                                 pass = FR_BLOCK;
167                         /*
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!
171                          */
172                         if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
173                              (fin->fin_fi.fi_fl & FI_FRAG))) {
174                                 KMALLOC(fr, frentry_t *);
175                                 if (fr) {
176                                         bcopy((char *)fra->fra_info.fin_fr,
177                                               fr, sizeof(*fr));
178                                         fr->fr_grp = NULL;
179                                         fr->fr_ifa = fin->fin_ifp;
180                                         fr->fr_func = NULL;
181                                         fr->fr_ref = 1;
182                                         fr->fr_flags = pass;
183 #if BSD >= 199306
184                                         fr->fr_oifa = NULL;
185 #endif
186                                 }
187                         } else
188                                 fr = fra->fra_info.fin_fr;
189                         fin->fin_fr = 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;
194                                 fr_authlist = fr;
195                         }
196                         fr_authstats.fas_hits++;
197                         fra->fra_index = -1;
198                         fr_authused--;
199                         if (i == fr_authstart) {
200                                 while (fra->fra_index == -1) {
201                                         i++;
202                                         fra++;
203                                         if (i == FR_NUMAUTH) {
204                                                 i = 0;
205                                                 fra = fr_auth;
206                                         }
207                                         fr_authstart = i;
208                                         if (i == fr_authend)
209                                                 break;
210                                 }
211                                 if (fr_authstart == fr_authend) {
212                                         fr_authnext = 0;
213                                         fr_authstart = fr_authend = 0;
214                                 }
215                         }
216                         RWLOCK_EXIT(&ipf_auth);
217                         return pass;
218                 }
219                 i++;
220                 if (i == FR_NUMAUTH)
221                         i = 0;
222         }
223         fr_authstats.fas_miss++;
224         RWLOCK_EXIT(&ipf_auth);
225         return 0;
226 }
227
228
229 /*
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.
233  */
234 int fr_newauth(m, fin, ip)
235 mb_t *m;
236 fr_info_t *fin;
237 ip_t *ip;
238 {
239 #if defined(_KERNEL) && SOLARIS
240         qif_t *qif = fin->fin_qif;
241 #endif
242         frauth_t *fra;
243         int i;
244
245         if (fr_auth_lock)
246                 return 0;
247
248         WRITE_ENTER(&ipf_auth);
249         if (fr_authstart > fr_authend) {
250                 fr_authstats.fas_nospace++;
251                 RWLOCK_EXIT(&ipf_auth);
252                 return 0;
253         } else {
254                 if (fr_authused == FR_NUMAUTH) {
255                         fr_authstats.fas_nospace++;
256                         RWLOCK_EXIT(&ipf_auth);
257                         return 0;
258                 }
259         }
260
261         fr_authstats.fas_added++;
262         fr_authused++;
263         i = fr_authend++;
264         if (fr_authend == FR_NUMAUTH)
265                 fr_authend = 0;
266         RWLOCK_EXIT(&ipf_auth);
267         fra = fr_auth + i;
268         fra->fra_index = i;
269         fra->fra_pass = 0;
270         fra->fra_age = fr_defaultauthage;
271         bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
272 #if SOLARIS && defined(_KERNEL)
273 # if !defined(sparc)
274         /*
275          * No need to copyback here as we want to undo the changes, not keep
276          * them.
277          */
278         if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
279         {
280                 u_short bo;
281
282                 bo = ip->ip_len;
283                 ip->ip_len = htons(bo);
284                 bo = ip->ip_off;
285                 ip->ip_off = htons(bo);
286         }
287 # endif
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);
292 #else
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);
297         }
298 # endif
299         fr_authpkts[i] = m;
300         WAKEUP(&fr_authnext);
301 #endif
302         return 1;
303 }
304
305
306 int fr_auth_ioctl(data, mode, cmd)
307 caddr_t data;
308 int mode;
309 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
310 u_long cmd;
311 #else
312 int cmd;
313 #endif
314 {
315         mb_t *m;
316 #if defined(_KERNEL) && !SOLARIS
317         struct ifqueue *ifq;
318         int s;
319 #endif
320         frauth_t auth, *au = &auth, *fra;
321         int i, error = 0;
322
323         switch (cmd)
324         {
325         case SIOCSTLCK :
326                 if (!(mode & FWRITE)) {
327                         error = EPERM;
328                         break;
329                 }
330                 error = fr_lock(data, &fr_auth_lock);
331                 break;
332         case SIOCINIFR :
333         case SIOCRMIFR :
334         case SIOCADIFR :
335                 error = EINVAL;
336                 break;
337         case SIOCINAFR :
338                 error = EINVAL;
339                 break;
340         case SIOCRMAFR :
341         case SIOCADAFR :
342                 /* These commands go via request to fr_preauthcmd */
343                 error = EINVAL;
344                 break;
345         case SIOCATHST:
346                 fr_authstats.fas_faelist = fae_list;
347                 error = IWCOPYPTR((char *)&fr_authstats, data,
348                                    sizeof(fr_authstats));
349                 break;
350         case SIOCAUTHW:
351                 if (!(mode & FWRITE)) {
352                         error = EPERM;
353                         break;
354                 }
355 fr_authioctlloop:
356                 READ_ENTER(&ipf_auth);
357                 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
358                         error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
359                                           sizeof(frauth_t));
360                         RWLOCK_EXIT(&ipf_auth);
361                         if (error)
362                                 break;
363                         WRITE_ENTER(&ipf_auth);
364                         SPL_NET(s);
365                         fr_authnext++;
366                         if (fr_authnext == FR_NUMAUTH)
367                                 fr_authnext = 0;
368                         SPL_X(s);
369                         RWLOCK_EXIT(&ipf_auth);
370                         return 0;
371                 }
372                 RWLOCK_EXIT(&ipf_auth);
373 #ifdef  _KERNEL
374 # if    SOLARIS
375                 mutex_enter(&ipf_authmx);
376                 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
377                         mutex_exit(&ipf_authmx);
378                         return EINTR;
379                 }
380                 mutex_exit(&ipf_authmx);
381 # else
382                 error = SLEEP(&fr_authnext, "fr_authnext");
383 # endif
384 #endif
385                 if (!error)
386                         goto fr_authioctlloop;
387                 break;
388         case SIOCAUTHR:
389                 if (!(mode & FWRITE)) {
390                         error = EPERM;
391                         break;
392                 }
393                 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
394                 if (error)
395                         return error;
396                 WRITE_ENTER(&ipf_auth);
397                 SPL_NET(s);
398                 i = au->fra_index;
399                 fra = fr_auth + i;
400                 if ((i < 0) || (i > FR_NUMAUTH) ||
401                     (fra->fra_info.fin_id != au->fra_info.fin_id)) {
402                         SPL_X(s);
403                         RWLOCK_EXIT(&ipf_auth);
404                         return EINVAL;
405                 }
406                 m = fr_authpkts[i];
407                 fra->fra_index = -2;
408                 fra->fra_pass = au->fra_pass;
409                 fr_authpkts[i] = NULL;
410                 RWLOCK_EXIT(&ipf_auth);
411 #ifdef  _KERNEL
412                 if (m && au->fra_info.fin_out) {
413 # if SOLARIS
414                         error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
415 # else /* SOLARIS */
416                         struct route ro;
417
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,
423                                           NULL);
424 #  else
425                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
426 #  endif
427                         if (ro.ro_rt) {
428                                 RTFREE(ro.ro_rt);
429                         }
430 # endif /* SOLARIS */
431                         if (error)
432                                 fr_authstats.fas_sendfail++;
433                         else
434                                 fr_authstats.fas_sendok++;
435                 } else if (m) {
436 # if SOLARIS
437                         error = (fr_qin(fra->fra_q, m) == 0) ? EINVAL : 0;
438 # else /* SOLARIS */
439                         ifq = &ipintrq;
440                         if (IF_QFULL(ifq)) {
441                                 IF_DROP(ifq);
442                                 m_freem(m);
443                                 error = ENOBUFS;
444                         } else {
445                                 IF_ENQUEUE(ifq, m);
446 #  if IRIX < 605
447                                 schednetisr(NETISR_IP);
448 #  endif
449                         }
450 # endif /* SOLARIS */
451                         if (error)
452                                 fr_authstats.fas_quefail++;
453                         else
454                                 fr_authstats.fas_queok++;
455                 } else
456                         error = EINVAL;
457 # if SOLARIS
458                 if (error)
459                         error = EINVAL;
460 # else
461                 /*
462                  * If we experience an error which will result in the packet
463                  * not being processed, make sure we advance to the next one.
464                  */ 
465                 if (error == ENOBUFS) {
466                         fr_authused--;
467                         fra->fra_index = -1;
468                         fra->fra_pass = 0;
469                         if (i == fr_authstart) {
470                                 while (fra->fra_index == -1) {
471                                         i++;
472                                         if (i == FR_NUMAUTH)
473                                                 i = 0;
474                                         fr_authstart = i;
475                                         if (i == fr_authend)
476                                                 break;
477                                 }
478                                 if (fr_authstart == fr_authend) {
479                                         fr_authnext = 0;
480                                         fr_authstart = fr_authend = 0;
481                                 }
482                         }
483                 }
484 # endif
485 #endif /* _KERNEL */
486                 SPL_X(s);
487                 break;
488         default :
489                 error = EINVAL;
490                 break;
491         }
492         return error;
493 }
494
495
496 /*
497  * Free all network buffer memory used to keep saved packets.
498  */
499 void fr_authunload()
500 {
501         int i;
502         frauthent_t *fae, **faep;
503         frentry_t *fr, **frp;
504         mb_t *m;
505
506         WRITE_ENTER(&ipf_auth);
507         for (i = 0; i < FR_NUMAUTH; i++) {
508                 if ((m = fr_authpkts[i])) {
509                         FREE_MB_T(m);
510                         fr_authpkts[i] = NULL;
511                         fr_auth[i].fra_index = -1;
512                 }
513         }
514
515
516         for (faep = &fae_list; (fae = *faep); ) {
517                 *faep = fae->fae_next;
518                 KFREE(fae);
519         }
520         ipauth = NULL;
521         RWLOCK_EXIT(&ipf_auth);
522
523         if (fr_authlist) {
524                 /*
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
528                  * inside fr_check().
529                  */
530                 WRITE_ENTER(&ipf_mutex);
531                 WRITE_ENTER(&ipf_auth);
532                 for (frp = &fr_authlist; (fr = *frp); ) {
533                         if (fr->fr_ref == 1) {
534                                 *frp = fr->fr_next;
535                                 KFREE(fr);
536                         } else
537                                 frp = &fr->fr_next;
538                 }
539                 RWLOCK_EXIT(&ipf_auth);
540                 RWLOCK_EXIT(&ipf_mutex);
541         }
542 }
543
544
545 /*
546  * Slowly expire held auth records.  Timeouts are set
547  * in expectation of this being called twice per second.
548  */
549 void fr_authexpire()
550 {
551         int i;
552         frauth_t *fra;
553         frauthent_t *fae, **faep;
554         frentry_t *fr, **frp;
555         mb_t *m;
556 #if !SOLARIS && defined(_KERNEL)
557         int s;
558 #endif
559
560         if (fr_auth_lock)
561                 return;
562
563         SPL_NET(s);
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])) {
567                         FREE_MB_T(m);
568                         fr_authpkts[i] = NULL;
569                         fr_auth[i].fra_index = -1;
570                         fr_authstats.fas_expire++;
571                         fr_authused--;
572                 }
573         }
574
575         for (faep = &fae_list; (fae = *faep); ) {
576                 if (!--fae->fae_age) {
577                         *faep = fae->fae_next;
578                         KFREE(fae);
579                         fr_authstats.fas_expire++;
580                 } else
581                         faep = &fae->fae_next;
582         }
583         if (fae_list != NULL)
584                 ipauth = &fae_list->fae_fr;
585         else
586                 ipauth = NULL;
587
588         for (frp = &fr_authlist; (fr = *frp); ) {
589                 if (fr->fr_ref == 1) {
590                         *frp = fr->fr_next;
591                         KFREE(fr);
592                 } else
593                         frp = &fr->fr_next;
594         }
595         RWLOCK_EXIT(&ipf_auth);
596         SPL_X(s);
597 }
598
599 int fr_preauthcmd(cmd, fr, frptr)
600 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
601         (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
602 u_long cmd;
603 #else
604 int cmd;
605 #endif                 
606 frentry_t *fr, **frptr;
607 {
608         frauthent_t *fae, **faep;
609         int error = 0;
610 #if defined(KERNEL) && !SOLARIS
611         int s;
612 #endif
613
614         if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
615                 /* Should not happen */
616                 printf("fr_preauthcmd called with bad cmd 0x%lx", (u_long)cmd);
617                 return EIO;
618         }
619         
620         for (faep = &fae_list; (fae = *faep); )
621                 if (&fae->fae_fr == fr)
622                         break;
623                 else
624                         faep = &fae->fae_next;
625         if (cmd == SIOCRMAFR) {
626                 if (!fr || !frptr)
627                         error = EINVAL;
628                 else if (!fae)
629                         error = ESRCH;
630                 else {
631                         WRITE_ENTER(&ipf_auth);
632                         SPL_NET(s);
633                         *faep = fae->fae_next;
634                         *frptr = fr->fr_next;
635                         SPL_X(s);
636                         RWLOCK_EXIT(&ipf_auth);
637                         KFREE(fae);
638                 }
639         } else if (fr && frptr) {
640                 KMALLOC(fae, frauthent_t *);
641                 if (fae != NULL) {
642                         bcopy((char *)fr, (char *)&fae->fae_fr,
643                               sizeof(*fr));
644                         WRITE_ENTER(&ipf_auth);
645                         SPL_NET(s);
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;
651                         *faep = fae;
652                         ipauth = &fae_list->fae_fr;
653                         SPL_X(s);
654                         RWLOCK_EXIT(&ipf_auth);
655                 } else
656                         error = ENOMEM;
657         } else
658                 error = EINVAL;
659         return error;
660 }