Merge from vendor branch HEIMDAL:
[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.6 2004/02/12 22:35:47 joerg 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)) && (defined(__DragonFly__) || __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 defined(__DragonFly__) || (_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 defined(__DragonFly__) || __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 "ip_compat.h"
93 #include <netinet/tcpip.h>
94 #include "ip_fil.h"
95 #include "ip_auth.h"
96 #if !SOLARIS && !defined(linux)
97 # include <net/netisr.h>
98 # if defined(__DragonFly__) || defined(__FreeBSD__)
99 #  include <machine/cpufunc.h>
100 # endif
101 #endif
102 #if defined(__DragonFly__) || (__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(__DragonFly__) || 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 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
318         struct ifqueue *ifq;
319 #endif
320         int s;
321 #endif
322         frauth_t auth, *au = &auth, *fra;
323         int i, error = 0;
324
325         switch (cmd)
326         {
327         case SIOCSTLCK :
328                 if (!(mode & FWRITE)) {
329                         error = EPERM;
330                         break;
331                 }
332                 error = fr_lock(data, &fr_auth_lock);
333                 break;
334         case SIOCINIFR :
335         case SIOCRMIFR :
336         case SIOCADIFR :
337                 error = EINVAL;
338                 break;
339         case SIOCINAFR :
340                 error = EINVAL;
341                 break;
342         case SIOCRMAFR :
343         case SIOCADAFR :
344                 /* These commands go via request to fr_preauthcmd */
345                 error = EINVAL;
346                 break;
347         case SIOCATHST:
348                 fr_authstats.fas_faelist = fae_list;
349                 error = IWCOPYPTR((char *)&fr_authstats, data,
350                                    sizeof(fr_authstats));
351                 break;
352         case SIOCAUTHW:
353                 if (!(mode & FWRITE)) {
354                         error = EPERM;
355                         break;
356                 }
357 fr_authioctlloop:
358                 READ_ENTER(&ipf_auth);
359                 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
360                         error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
361                                           sizeof(frauth_t));
362                         RWLOCK_EXIT(&ipf_auth);
363                         if (error)
364                                 break;
365                         WRITE_ENTER(&ipf_auth);
366                         SPL_NET(s);
367                         fr_authnext++;
368                         if (fr_authnext == FR_NUMAUTH)
369                                 fr_authnext = 0;
370                         SPL_X(s);
371                         RWLOCK_EXIT(&ipf_auth);
372                         return 0;
373                 }
374                 RWLOCK_EXIT(&ipf_auth);
375 #ifdef  _KERNEL
376 # if    SOLARIS
377                 mutex_enter(&ipf_authmx);
378                 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
379                         mutex_exit(&ipf_authmx);
380                         return EINTR;
381                 }
382                 mutex_exit(&ipf_authmx);
383 # else
384                 error = SLEEP(&fr_authnext, "fr_authnext");
385 # endif
386 #endif
387                 if (!error)
388                         goto fr_authioctlloop;
389                 break;
390         case SIOCAUTHR:
391                 if (!(mode & FWRITE)) {
392                         error = EPERM;
393                         break;
394                 }
395                 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
396                 if (error)
397                         return error;
398                 WRITE_ENTER(&ipf_auth);
399                 SPL_NET(s);
400                 i = au->fra_index;
401                 fra = fr_auth + i;
402                 if ((i < 0) || (i > FR_NUMAUTH) ||
403                     (fra->fra_info.fin_id != au->fra_info.fin_id)) {
404                         SPL_X(s);
405                         RWLOCK_EXIT(&ipf_auth);
406                         return EINVAL;
407                 }
408                 m = fr_authpkts[i];
409                 fra->fra_index = -2;
410                 fra->fra_pass = au->fra_pass;
411                 fr_authpkts[i] = NULL;
412                 RWLOCK_EXIT(&ipf_auth);
413 #ifdef  _KERNEL
414                 if (m && au->fra_info.fin_out) {
415 # if SOLARIS
416                         error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
417 # else /* SOLARIS */
418                         struct route ro;
419
420                         bzero((char *)&ro, sizeof(ro));
421 #  if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
422        defined(__DragonFly__) || defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605)) || \
423        (__FreeBSD_version >= 470102)
424                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
425                                           NULL);
426 #  else
427                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
428 #  endif
429                         if (ro.ro_rt) {
430                                 RTFREE(ro.ro_rt);
431                         }
432 # endif /* SOLARIS */
433                         if (error)
434                                 fr_authstats.fas_sendfail++;
435                         else
436                                 fr_authstats.fas_sendok++;
437                 } else if (m) {
438 # if SOLARIS
439                         error = (fr_qin(fra->fra_q, m) == 0) ? EINVAL : 0;
440 # else /* SOLARIS */
441 # if defined(__DragonFly__) || defined(__FreeBSD__)
442                         error = netisr_queue(NETISR_IP, m);
443 # else
444                         ifq = &ipintrq;
445                         if (IF_QFULL(ifq)) {
446                                 IF_DROP(ifq);
447                                 m_freem(m);
448                                 error = ENOBUFS;
449                         } else {
450                                 IF_ENQUEUE(ifq, m);
451 #  if IRIX < 605
452                                 schednetisr(NETISR_IP);
453 #  endif
454                         }
455 # endif
456 # endif /* !SOLARIS */
457                         if (error)
458                                 fr_authstats.fas_quefail++;
459                         else
460                                 fr_authstats.fas_queok++;
461                 } else
462                         error = EINVAL;
463 # if SOLARIS
464                 if (error)
465                         error = EINVAL;
466 # else
467                 /*
468                  * If we experience an error which will result in the packet
469                  * not being processed, make sure we advance to the next one.
470                  */ 
471                 if (error == ENOBUFS) {
472                         fr_authused--;
473                         fra->fra_index = -1;
474                         fra->fra_pass = 0;
475                         if (i == fr_authstart) {
476                                 while (fra->fra_index == -1) {
477                                         i++;
478                                         if (i == FR_NUMAUTH)
479                                                 i = 0;
480                                         fr_authstart = i;
481                                         if (i == fr_authend)
482                                                 break;
483                                 }
484                                 if (fr_authstart == fr_authend) {
485                                         fr_authnext = 0;
486                                         fr_authstart = fr_authend = 0;
487                                 }
488                         }
489                 }
490 # endif
491 #endif /* _KERNEL */
492                 SPL_X(s);
493                 break;
494         default :
495                 error = EINVAL;
496                 break;
497         }
498         return error;
499 }
500
501
502 /*
503  * Free all network buffer memory used to keep saved packets.
504  */
505 void fr_authunload()
506 {
507         int i;
508         frauthent_t *fae, **faep;
509         frentry_t *fr, **frp;
510         mb_t *m;
511
512         WRITE_ENTER(&ipf_auth);
513         for (i = 0; i < FR_NUMAUTH; i++) {
514                 if ((m = fr_authpkts[i])) {
515                         FREE_MB_T(m);
516                         fr_authpkts[i] = NULL;
517                         fr_auth[i].fra_index = -1;
518                 }
519         }
520
521
522         for (faep = &fae_list; (fae = *faep); ) {
523                 *faep = fae->fae_next;
524                 KFREE(fae);
525         }
526         ipauth = NULL;
527         RWLOCK_EXIT(&ipf_auth);
528
529         if (fr_authlist) {
530                 /*
531                  * We *MuST* reget ipf_auth because otherwise we won't get the
532                  * locks in the right order and risk deadlock.
533                  * We need ipf_mutex here to prevent a rule from using it
534                  * inside fr_check().
535                  */
536                 WRITE_ENTER(&ipf_mutex);
537                 WRITE_ENTER(&ipf_auth);
538                 for (frp = &fr_authlist; (fr = *frp); ) {
539                         if (fr->fr_ref == 1) {
540                                 *frp = fr->fr_next;
541                                 KFREE(fr);
542                         } else
543                                 frp = &fr->fr_next;
544                 }
545                 RWLOCK_EXIT(&ipf_auth);
546                 RWLOCK_EXIT(&ipf_mutex);
547         }
548 }
549
550
551 /*
552  * Slowly expire held auth records.  Timeouts are set
553  * in expectation of this being called twice per second.
554  */
555 void fr_authexpire()
556 {
557         int i;
558         frauth_t *fra;
559         frauthent_t *fae, **faep;
560         frentry_t *fr, **frp;
561         mb_t *m;
562 #if !SOLARIS && defined(_KERNEL)
563         int s;
564 #endif
565
566         if (fr_auth_lock)
567                 return;
568
569         SPL_NET(s);
570         WRITE_ENTER(&ipf_auth);
571         for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
572                 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
573                         FREE_MB_T(m);
574                         fr_authpkts[i] = NULL;
575                         fr_auth[i].fra_index = -1;
576                         fr_authstats.fas_expire++;
577                         fr_authused--;
578                 }
579         }
580
581         for (faep = &fae_list; (fae = *faep); ) {
582                 if (!--fae->fae_age) {
583                         *faep = fae->fae_next;
584                         KFREE(fae);
585                         fr_authstats.fas_expire++;
586                 } else
587                         faep = &fae->fae_next;
588         }
589         if (fae_list != NULL)
590                 ipauth = &fae_list->fae_fr;
591         else
592                 ipauth = NULL;
593
594         for (frp = &fr_authlist; (fr = *frp); ) {
595                 if (fr->fr_ref == 1) {
596                         *frp = fr->fr_next;
597                         KFREE(fr);
598                 } else
599                         frp = &fr->fr_next;
600         }
601         RWLOCK_EXIT(&ipf_auth);
602         SPL_X(s);
603 }
604
605 int fr_preauthcmd(cmd, fr, frptr)
606 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
607         (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
608 u_long cmd;
609 #else
610 int cmd;
611 #endif                 
612 frentry_t *fr, **frptr;
613 {
614         frauthent_t *fae, **faep;
615         int error = 0;
616 #if defined(KERNEL) && !SOLARIS
617         int s;
618 #endif
619
620         if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
621                 /* Should not happen */
622                 printf("fr_preauthcmd called with bad cmd 0x%lx", (u_long)cmd);
623                 return EIO;
624         }
625         
626         for (faep = &fae_list; (fae = *faep); )
627                 if (&fae->fae_fr == fr)
628                         break;
629                 else
630                         faep = &fae->fae_next;
631         if (cmd == SIOCRMAFR) {
632                 if (!fr || !frptr)
633                         error = EINVAL;
634                 else if (!fae)
635                         error = ESRCH;
636                 else {
637                         WRITE_ENTER(&ipf_auth);
638                         SPL_NET(s);
639                         *faep = fae->fae_next;
640                         *frptr = fr->fr_next;
641                         SPL_X(s);
642                         RWLOCK_EXIT(&ipf_auth);
643                         KFREE(fae);
644                 }
645         } else if (fr && frptr) {
646                 KMALLOC(fae, frauthent_t *);
647                 if (fae != NULL) {
648                         bcopy((char *)fr, (char *)&fae->fae_fr,
649                               sizeof(*fr));
650                         WRITE_ENTER(&ipf_auth);
651                         SPL_NET(s);
652                         fae->fae_age = fr_defaultauthage;
653                         fae->fae_fr.fr_hits = 0;
654                         fae->fae_fr.fr_next = *frptr;
655                         *frptr = &fae->fae_fr;
656                         fae->fae_next = *faep;
657                         *faep = fae;
658                         ipauth = &fae_list->fae_fr;
659                         SPL_X(s);
660                         RWLOCK_EXIT(&ipf_auth);
661                 } else
662                         error = ENOMEM;
663         } else
664                 error = EINVAL;
665         return error;
666 }