Initial import from FreeBSD RELENG_4:
[games.git] / contrib / ipfilter / 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 #if defined(__sgi) && (IRIX > 602)
7 # include <sys/ptimers.h>
8 #endif
9 #include <sys/errno.h>
10 #include <sys/types.h>
11 #include <sys/param.h>
12 #include <sys/time.h>
13 #include <sys/file.h>
14 #if !defined(_KERNEL) && !defined(KERNEL)
15 # include <stdio.h>
16 # include <stdlib.h>
17 # include <string.h>
18 #endif
19 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
20 # include <sys/filio.h>
21 # include <sys/fcntl.h>
22 #else
23 # include <sys/ioctl.h>
24 #endif
25 #ifndef linux
26 # include <sys/protosw.h>
27 #endif
28 #include <sys/socket.h>
29 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
30 # include <sys/systm.h>
31 #endif
32 #if !defined(__SVR4) && !defined(__svr4__)
33 # ifndef linux
34 #  include <sys/mbuf.h>
35 # endif
36 #else
37 # include <sys/filio.h>
38 # include <sys/byteorder.h>
39 # ifdef _KERNEL
40 #  include <sys/dditypes.h>
41 # endif
42 # include <sys/stream.h>
43 # include <sys/kmem.h>
44 #endif
45 #if (_BSDI_VERSION >= 199802) || (__FreeBSD_version >= 400000)
46 # include <sys/queue.h>
47 #endif
48 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
49 # include <machine/cpu.h>
50 #endif
51 #include <net/if.h>
52 #ifdef sun
53 # include <net/af.h>
54 #endif
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #ifndef KERNEL
60 # define        KERNEL
61 # define        NOT_KERNEL
62 #endif
63 #ifndef linux
64 # include <netinet/ip_var.h>
65 #endif
66 #ifdef  NOT_KERNEL
67 # undef KERNEL
68 #endif
69 #ifdef __sgi
70 # ifdef IFF_DRVRLOCK /* IRIX6 */
71 #  include <sys/hashing.h>
72 # endif
73 #endif
74 #include <netinet/tcp.h>
75 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
76 extern struct ifqueue   ipintrq;                /* ip packet input queue */
77 #else
78 # ifndef linux
79 #  if __FreeBSD_version >= 300000
80 #   include <net/if_var.h>
81 #  endif
82 #  include <netinet/in_var.h>
83 #  include <netinet/tcp_fsm.h>
84 # endif
85 #endif
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>
94 # ifdef __FreeBSD__
95 #  include <machine/cpufunc.h>
96 # endif
97 #endif
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>
103 # endif
104 #endif
105
106 #if !defined(lint)
107 static const char rcsid[] = "@(#)$Id: ip_auth.c,v 2.11.2.24 2002/12/06 11:40:21 darrenr Exp $";
108 #endif
109
110
111 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
112 extern KRWLOCK_T ipf_auth, ipf_mutex;
113 extern kmutex_t ipf_authmx;
114 # if SOLARIS
115 extern kcondvar_t ipfauthwait;
116 # endif
117 #endif
118 #ifdef linux
119 static struct wait_queue *ipfauthwait = NULL;
120 #endif
121
122 int     fr_authsize = FR_NUMAUTH;
123 int     fr_authused = 0;
124 int     fr_defaultauthage = 600;
125 int     fr_auth_lock = 0;
126 fr_authstat_t   fr_authstats;
127 static frauth_t fr_auth[FR_NUMAUTH];
128 mb_t    *fr_authpkts[FR_NUMAUTH];
129 static int      fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
130 static frauthent_t      *fae_list = NULL;
131 frentry_t       *ipauth = NULL,
132                 *fr_authlist = NULL;
133
134
135 /*
136  * Check if a packet has authorization.  If the packet is found to match an
137  * authorization result and that would result in a feedback loop (i.e. it
138  * will end up returning FR_AUTH) then return FR_BLOCK instead.
139  */
140 u_32_t fr_checkauth(ip, fin)
141 ip_t *ip;
142 fr_info_t *fin;
143 {
144         u_short id = ip->ip_id;
145         frentry_t *fr;
146         frauth_t *fra;
147         u_32_t pass;
148         int i;
149
150         if (fr_auth_lock || !fr_authused)
151                 return 0;
152
153         READ_ENTER(&ipf_auth);
154         for (i = fr_authstart; i != fr_authend; ) {
155                 /*
156                  * index becomes -2 only after an SIOCAUTHW.  Check this in
157                  * case the same packet gets sent again and it hasn't yet been
158                  * auth'd.
159                  */
160                 fra = fr_auth + i;
161                 if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
162                     !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
163                         /*
164                          * Avoid feedback loop.
165                          */
166                         if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
167                                 pass = FR_BLOCK;
168                         /*
169                          * Create a dummy rule for the stateful checking to
170                          * use and return.  Zero out any values we don't
171                          * trust from userland!
172                          */
173                         if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
174                              (fin->fin_fi.fi_fl & FI_FRAG))) {
175                                 KMALLOC(fr, frentry_t *);
176                                 if (fr) {
177                                         bcopy((char *)fra->fra_info.fin_fr,
178                                               fr, sizeof(*fr));
179                                         fr->fr_grp = NULL;
180                                         fr->fr_ifa = fin->fin_ifp;
181                                         fr->fr_func = NULL;
182                                         fr->fr_ref = 1;
183                                         fr->fr_flags = pass;
184 #if BSD >= 199306
185                                         fr->fr_oifa = NULL;
186 #endif
187                                 }
188                         } else
189                                 fr = fra->fra_info.fin_fr;
190                         fin->fin_fr = fr;
191                         RWLOCK_EXIT(&ipf_auth);
192                         WRITE_ENTER(&ipf_auth);
193                         if (fr && fr != fra->fra_info.fin_fr) {
194                                 fr->fr_next = fr_authlist;
195                                 fr_authlist = fr;
196                         }
197                         fr_authstats.fas_hits++;
198                         fra->fra_index = -1;
199                         fr_authused--;
200                         if (i == fr_authstart) {
201                                 while (fra->fra_index == -1) {
202                                         i++;
203                                         fra++;
204                                         if (i == FR_NUMAUTH) {
205                                                 i = 0;
206                                                 fra = fr_auth;
207                                         }
208                                         fr_authstart = i;
209                                         if (i == fr_authend)
210                                                 break;
211                                 }
212                                 if (fr_authstart == fr_authend) {
213                                         fr_authnext = 0;
214                                         fr_authstart = fr_authend = 0;
215                                 }
216                         }
217                         RWLOCK_EXIT(&ipf_auth);
218                         return pass;
219                 }
220                 i++;
221                 if (i == FR_NUMAUTH)
222                         i = 0;
223         }
224         fr_authstats.fas_miss++;
225         RWLOCK_EXIT(&ipf_auth);
226         return 0;
227 }
228
229
230 /*
231  * Check if we have room in the auth array to hold details for another packet.
232  * If we do, store it and wake up any user programs which are waiting to
233  * hear about these events.
234  */
235 int fr_newauth(m, fin, ip)
236 mb_t *m;
237 fr_info_t *fin;
238 ip_t *ip;
239 {
240 #if defined(_KERNEL) && SOLARIS
241         qif_t *qif = fin->fin_qif;
242 #endif
243         frauth_t *fra;
244         int i;
245
246         if (fr_auth_lock)
247                 return 0;
248
249         WRITE_ENTER(&ipf_auth);
250         if (fr_authstart > fr_authend) {
251                 fr_authstats.fas_nospace++;
252                 RWLOCK_EXIT(&ipf_auth);
253                 return 0;
254         } else {
255                 if (fr_authused == FR_NUMAUTH) {
256                         fr_authstats.fas_nospace++;
257                         RWLOCK_EXIT(&ipf_auth);
258                         return 0;
259                 }
260         }
261
262         fr_authstats.fas_added++;
263         fr_authused++;
264         i = fr_authend++;
265         if (fr_authend == FR_NUMAUTH)
266                 fr_authend = 0;
267         RWLOCK_EXIT(&ipf_auth);
268         fra = fr_auth + i;
269         fra->fra_index = i;
270         fra->fra_pass = 0;
271         fra->fra_age = fr_defaultauthage;
272         bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
273 #if SOLARIS && defined(_KERNEL)
274 # if !defined(sparc)
275         /*
276          * No need to copyback here as we want to undo the changes, not keep
277          * them.
278          */
279         if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
280         {
281                 register u_short bo;
282
283                 bo = ip->ip_len;
284                 ip->ip_len = htons(bo);
285                 bo = ip->ip_off;
286                 ip->ip_off = htons(bo);
287         }
288 # endif
289         m->b_rptr -= qif->qf_off;
290         fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
291         fra->fra_q = qif->qf_q;
292         cv_signal(&ipfauthwait);
293 #else
294 # if defined(BSD) && !defined(sparc) && (BSD >= 199306)
295         if (fin->fin_out == 0) {
296                 ip->ip_len = htons(ip->ip_len);
297                 ip->ip_off = htons(ip->ip_off);
298         }
299 # endif
300         fr_authpkts[i] = m;
301         WAKEUP(&fr_authnext);
302 #endif
303         return 1;
304 }
305
306
307 int fr_auth_ioctl(data, mode, cmd)
308 caddr_t data;
309 int mode;
310 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
311 u_long cmd;
312 #else
313 int cmd;
314 #endif
315 {
316         mb_t *m;
317 #if defined(_KERNEL) && !SOLARIS
318         struct ifqueue *ifq;
319         int s;
320 #endif
321         frauth_t auth, *au = &auth, *fra;
322         int i, error = 0;
323
324         switch (cmd)
325         {
326         case SIOCSTLCK :
327                 if (!(mode & FWRITE)) {
328                         error = EPERM;
329                         break;
330                 }
331                 error = fr_lock(data, &fr_auth_lock);
332                 break;
333         case SIOCINIFR :
334         case SIOCRMIFR :
335         case SIOCADIFR :
336                 error = EINVAL;
337                 break;
338         case SIOCINAFR :
339                 error = EINVAL;
340                 break;
341         case SIOCRMAFR :
342         case SIOCADAFR :
343                 /* These commands go via request to fr_preauthcmd */
344                 error = EINVAL;
345                 break;
346         case SIOCATHST:
347                 fr_authstats.fas_faelist = fae_list;
348                 error = IWCOPYPTR((char *)&fr_authstats, data,
349                                    sizeof(fr_authstats));
350                 break;
351         case SIOCAUTHW:
352                 if (!(mode & FWRITE)) {
353                         error = EPERM;
354                         break;
355                 }
356 fr_authioctlloop:
357                 READ_ENTER(&ipf_auth);
358                 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
359                         error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
360                                           sizeof(frauth_t));
361                         RWLOCK_EXIT(&ipf_auth);
362                         if (error)
363                                 break;
364                         WRITE_ENTER(&ipf_auth);
365                         SPL_NET(s);
366                         fr_authnext++;
367                         if (fr_authnext == FR_NUMAUTH)
368                                 fr_authnext = 0;
369                         SPL_X(s);
370                         RWLOCK_EXIT(&ipf_auth);
371                         return 0;
372                 }
373                 RWLOCK_EXIT(&ipf_auth);
374 #ifdef  _KERNEL
375 # if    SOLARIS
376                 mutex_enter(&ipf_authmx);
377                 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
378                         mutex_exit(&ipf_authmx);
379                         return EINTR;
380                 }
381                 mutex_exit(&ipf_authmx);
382 # else
383                 error = SLEEP(&fr_authnext, "fr_authnext");
384 # endif
385 #endif
386                 if (!error)
387                         goto fr_authioctlloop;
388                 break;
389         case SIOCAUTHR:
390                 if (!(mode & FWRITE)) {
391                         error = EPERM;
392                         break;
393                 }
394                 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
395                 if (error)
396                         return error;
397                 WRITE_ENTER(&ipf_auth);
398                 SPL_NET(s);
399                 i = au->fra_index;
400                 fra = fr_auth + i;
401                 if ((i < 0) || (i > FR_NUMAUTH) ||
402                     (fra->fra_info.fin_id != au->fra_info.fin_id)) {
403                         SPL_X(s);
404                         RWLOCK_EXIT(&ipf_auth);
405                         return EINVAL;
406                 }
407                 m = fr_authpkts[i];
408                 fra->fra_index = -2;
409                 fra->fra_pass = au->fra_pass;
410                 fr_authpkts[i] = NULL;
411                 RWLOCK_EXIT(&ipf_auth);
412 #ifdef  _KERNEL
413                 if (m && au->fra_info.fin_out) {
414 # if SOLARIS
415                         error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
416 # else /* SOLARIS */
417                         struct route ro;
418
419                         bzero((char *)&ro, sizeof(ro));
420 #  if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
421        defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605))
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         register int i;
502         register 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         register int i;
552         register frauth_t *fra;
553         register frauthent_t *fae, **faep;
554         register 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 }