Merge from vendor branch GCC:
[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.8 2005/06/05 12:17:46 corecode 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(__DragonFly__) && defined(_KERNEL)
53 # include <sys/thread2.h>
54 #endif
55 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
56 # include <machine/cpu.h>
57 #endif
58 #include <net/if.h>
59 #ifdef sun
60 # include <net/af.h>
61 #endif
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #ifndef KERNEL
67 # define        KERNEL
68 # define        NOT_KERNEL
69 #endif
70 #ifndef linux
71 # include <netinet/ip_var.h>
72 #endif
73 #ifdef  NOT_KERNEL
74 # undef KERNEL
75 #endif
76 #ifdef __sgi
77 # ifdef IFF_DRVRLOCK /* IRIX6 */
78 #  include <sys/hashing.h>
79 # endif
80 #endif
81 #include <netinet/tcp.h>
82 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
83 extern struct ifqueue   ipintrq;                /* ip packet input queue */
84 #else
85 # ifndef linux
86 #  if defined(__DragonFly__) || __FreeBSD_version >= 300000
87 #   include <net/if_var.h>
88 #  endif
89 #  include <netinet/in_var.h>
90 #  include <netinet/tcp_fsm.h>
91 # endif
92 #endif
93 #include <netinet/udp.h>
94 #include <netinet/ip_icmp.h>
95 #include "ip_compat.h"
96 #include <netinet/tcpip.h>
97 #include "ip_fil.h"
98 #include "ip_auth.h"
99 #if !SOLARIS && !defined(linux)
100 # include <net/netisr.h>
101 # if defined(__DragonFly__) || defined(__FreeBSD__)
102 #  include <machine/cpufunc.h>
103 # endif
104 #endif
105 #if defined(__DragonFly__) || (__FreeBSD_version >= 300000)
106 # include <sys/malloc.h>
107 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
108 #  include <sys/libkern.h>
109 #  include <sys/systm.h>
110 # endif
111 #endif
112
113 #if (SOLARIS || defined(__sgi)) && defined(_KERNEL)
114 extern KRWLOCK_T ipf_auth, ipf_mutex;
115 extern kmutex_t ipf_authmx;
116 # if SOLARIS
117 extern kcondvar_t ipfauthwait;
118 # endif
119 #endif
120 #ifdef linux
121 static struct wait_queue *ipfauthwait = NULL;
122 #endif
123
124 int     fr_authsize = FR_NUMAUTH;
125 int     fr_authused = 0;
126 int     fr_defaultauthage = 600;
127 int     fr_auth_lock = 0;
128 fr_authstat_t   fr_authstats;
129 static frauth_t fr_auth[FR_NUMAUTH];
130 mb_t    *fr_authpkts[FR_NUMAUTH];
131 static int      fr_authstart = 0, fr_authend = 0, fr_authnext = 0;
132 static frauthent_t      *fae_list = NULL;
133 frentry_t       *ipauth = NULL,
134                 *fr_authlist = NULL;
135
136
137 /*
138  * Check if a packet has authorization.  If the packet is found to match an
139  * authorization result and that would result in a feedback loop (i.e. it
140  * will end up returning FR_AUTH) then return FR_BLOCK instead.
141  */
142 u_32_t fr_checkauth(ip, fin)
143 ip_t *ip;
144 fr_info_t *fin;
145 {
146         u_short id = ip->ip_id;
147         frentry_t *fr;
148         frauth_t *fra;
149         u_32_t pass;
150         int i;
151
152         if (fr_auth_lock || !fr_authused)
153                 return 0;
154
155         READ_ENTER(&ipf_auth);
156         for (i = fr_authstart; i != fr_authend; ) {
157                 /*
158                  * index becomes -2 only after an SIOCAUTHW.  Check this in
159                  * case the same packet gets sent again and it hasn't yet been
160                  * auth'd.
161                  */
162                 fra = fr_auth + i;
163                 if ((fra->fra_index == -2) && (id == fra->fra_info.fin_id) &&
164                     !bcmp((char *)fin, (char *)&fra->fra_info, FI_CSIZE)) {
165                         /*
166                          * Avoid feedback loop.
167                          */
168                         if (!(pass = fra->fra_pass) || (pass & FR_AUTH))
169                                 pass = FR_BLOCK;
170                         /*
171                          * Create a dummy rule for the stateful checking to
172                          * use and return.  Zero out any values we don't
173                          * trust from userland!
174                          */
175                         if ((pass & FR_KEEPSTATE) || ((pass & FR_KEEPFRAG) &&
176                              (fin->fin_fi.fi_fl & FI_FRAG))) {
177                                 KMALLOC(fr, frentry_t *);
178                                 if (fr) {
179                                         bcopy((char *)fra->fra_info.fin_fr,
180                                               fr, sizeof(*fr));
181                                         fr->fr_grp = NULL;
182                                         fr->fr_ifa = fin->fin_ifp;
183                                         fr->fr_func = NULL;
184                                         fr->fr_ref = 1;
185                                         fr->fr_flags = pass;
186 #if BSD >= 199306
187                                         fr->fr_oifa = NULL;
188 #endif
189                                 }
190                         } else
191                                 fr = fra->fra_info.fin_fr;
192                         fin->fin_fr = fr;
193                         RWLOCK_EXIT(&ipf_auth);
194                         WRITE_ENTER(&ipf_auth);
195                         if (fr && fr != fra->fra_info.fin_fr) {
196                                 fr->fr_next = fr_authlist;
197                                 fr_authlist = fr;
198                         }
199                         fr_authstats.fas_hits++;
200                         fra->fra_index = -1;
201                         fr_authused--;
202                         if (i == fr_authstart) {
203                                 while (fra->fra_index == -1) {
204                                         i++;
205                                         fra++;
206                                         if (i == FR_NUMAUTH) {
207                                                 i = 0;
208                                                 fra = fr_auth;
209                                         }
210                                         fr_authstart = i;
211                                         if (i == fr_authend)
212                                                 break;
213                                 }
214                                 if (fr_authstart == fr_authend) {
215                                         fr_authnext = 0;
216                                         fr_authstart = fr_authend = 0;
217                                 }
218                         }
219                         RWLOCK_EXIT(&ipf_auth);
220                         return pass;
221                 }
222                 i++;
223                 if (i == FR_NUMAUTH)
224                         i = 0;
225         }
226         fr_authstats.fas_miss++;
227         RWLOCK_EXIT(&ipf_auth);
228         return 0;
229 }
230
231
232 /*
233  * Check if we have room in the auth array to hold details for another packet.
234  * If we do, store it and wake up any user programs which are waiting to
235  * hear about these events.
236  */
237 int fr_newauth(m, fin, ip)
238 mb_t *m;
239 fr_info_t *fin;
240 ip_t *ip;
241 {
242 #if defined(_KERNEL) && SOLARIS
243         qif_t *qif = fin->fin_qif;
244 #endif
245         frauth_t *fra;
246         int i;
247
248         if (fr_auth_lock)
249                 return 0;
250
251         WRITE_ENTER(&ipf_auth);
252         if (fr_authstart > fr_authend) {
253                 fr_authstats.fas_nospace++;
254                 RWLOCK_EXIT(&ipf_auth);
255                 return 0;
256         } else {
257                 if (fr_authused == FR_NUMAUTH) {
258                         fr_authstats.fas_nospace++;
259                         RWLOCK_EXIT(&ipf_auth);
260                         return 0;
261                 }
262         }
263
264         fr_authstats.fas_added++;
265         fr_authused++;
266         i = fr_authend++;
267         if (fr_authend == FR_NUMAUTH)
268                 fr_authend = 0;
269         RWLOCK_EXIT(&ipf_auth);
270         fra = fr_auth + i;
271         fra->fra_index = i;
272         fra->fra_pass = 0;
273         fra->fra_age = fr_defaultauthage;
274         bcopy((char *)fin, (char *)&fra->fra_info, sizeof(*fin));
275 #if SOLARIS && defined(_KERNEL)
276 # if !defined(sparc)
277         /*
278          * No need to copyback here as we want to undo the changes, not keep
279          * them.
280          */
281         if ((ip == (ip_t *)m->b_rptr) && (ip->ip_v == 4))
282         {
283                 u_short bo;
284
285                 bo = ip->ip_len;
286                 ip->ip_len = htons(bo);
287                 bo = ip->ip_off;
288                 ip->ip_off = htons(bo);
289         }
290 # endif
291         m->b_rptr -= qif->qf_off;
292         fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
293         fra->fra_q = qif->qf_q;
294         cv_signal(&ipfauthwait);
295 #else
296 # if defined(BSD) && !defined(sparc) && (BSD >= 199306)
297         if (fin->fin_out == 0) {
298                 ip->ip_len = htons(ip->ip_len);
299                 ip->ip_off = htons(ip->ip_off);
300         }
301 # endif
302         fr_authpkts[i] = m;
303         WAKEUP(&fr_authnext);
304 #endif
305         return 1;
306 }
307
308
309 int fr_auth_ioctl(data, mode, cmd)
310 caddr_t data;
311 int mode;
312 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
313 u_long cmd;
314 #else
315 int cmd;
316 #endif
317 {
318         mb_t *m;
319 #if defined(_KERNEL) && !SOLARIS
320 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
321         struct ifqueue *ifq;
322 #endif
323 #if !defined(__DragonFly__)
324         int s;
325 #endif
326 #endif
327         frauth_t auth, *au = &auth, *fra;
328         int i, error = 0;
329
330         switch (cmd)
331         {
332         case SIOCSTLCK :
333                 if (!(mode & FWRITE)) {
334                         error = EPERM;
335                         break;
336                 }
337                 error = fr_lock(data, &fr_auth_lock);
338                 break;
339         case SIOCINIFR :
340         case SIOCRMIFR :
341         case SIOCADIFR :
342                 error = EINVAL;
343                 break;
344         case SIOCINAFR :
345                 error = EINVAL;
346                 break;
347         case SIOCRMAFR :
348         case SIOCADAFR :
349                 /* These commands go via request to fr_preauthcmd */
350                 error = EINVAL;
351                 break;
352         case SIOCATHST:
353                 fr_authstats.fas_faelist = fae_list;
354                 error = IWCOPYPTR((char *)&fr_authstats, data,
355                                    sizeof(fr_authstats));
356                 break;
357         case SIOCAUTHW:
358                 if (!(mode & FWRITE)) {
359                         error = EPERM;
360                         break;
361                 }
362 fr_authioctlloop:
363                 READ_ENTER(&ipf_auth);
364                 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
365                         error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
366                                           sizeof(frauth_t));
367                         RWLOCK_EXIT(&ipf_auth);
368                         if (error)
369                                 break;
370                         WRITE_ENTER(&ipf_auth);
371                         SPL_NET(s);
372                         fr_authnext++;
373                         if (fr_authnext == FR_NUMAUTH)
374                                 fr_authnext = 0;
375                         SPL_X(s);
376                         RWLOCK_EXIT(&ipf_auth);
377                         return 0;
378                 }
379                 RWLOCK_EXIT(&ipf_auth);
380 #ifdef  _KERNEL
381 # if    SOLARIS
382                 mutex_enter(&ipf_authmx);
383                 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
384                         mutex_exit(&ipf_authmx);
385                         return EINTR;
386                 }
387                 mutex_exit(&ipf_authmx);
388 # else
389                 error = SLEEP(&fr_authnext, "fr_authnext");
390 # endif
391 #endif
392                 if (!error)
393                         goto fr_authioctlloop;
394                 break;
395         case SIOCAUTHR:
396                 if (!(mode & FWRITE)) {
397                         error = EPERM;
398                         break;
399                 }
400                 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
401                 if (error)
402                         return error;
403                 WRITE_ENTER(&ipf_auth);
404                 SPL_NET(s);
405                 i = au->fra_index;
406                 fra = fr_auth + i;
407                 if ((i < 0) || (i > FR_NUMAUTH) ||
408                     (fra->fra_info.fin_id != au->fra_info.fin_id)) {
409                         SPL_X(s);
410                         RWLOCK_EXIT(&ipf_auth);
411                         return EINVAL;
412                 }
413                 m = fr_authpkts[i];
414                 fra->fra_index = -2;
415                 fra->fra_pass = au->fra_pass;
416                 fr_authpkts[i] = NULL;
417                 RWLOCK_EXIT(&ipf_auth);
418 #ifdef  _KERNEL
419                 if (m && au->fra_info.fin_out) {
420 # if SOLARIS
421                         error = (fr_qout(fra->fra_q, m) == 0) ? EINVAL : 0;
422 # else /* SOLARIS */
423                         struct route ro;
424
425                         bzero((char *)&ro, sizeof(ro));
426 #  if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
427        defined(__DragonFly__) || defined(__OpenBSD__) || (defined(IRIX) && (IRIX >= 605)) || \
428        (__FreeBSD_version >= 470102)
429                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
430                                           NULL);
431 #  else
432                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
433 #  endif
434                         if (ro.ro_rt) {
435                                 RTFREE(ro.ro_rt);
436                         }
437 # endif /* SOLARIS */
438                         if (error)
439                                 fr_authstats.fas_sendfail++;
440                         else
441                                 fr_authstats.fas_sendok++;
442                 } else if (m) {
443 # if SOLARIS
444                         error = (fr_qin(fra->fra_q, m) == 0) ? EINVAL : 0;
445 # else /* SOLARIS */
446 # if defined(__DragonFly__) || defined(__FreeBSD__)
447                         error = netisr_queue(NETISR_IP, m);
448 # else
449                         ifq = &ipintrq;
450                         if (IF_QFULL(ifq)) {
451                                 IF_DROP(ifq);
452                                 m_freem(m);
453                                 error = ENOBUFS;
454                         } else {
455                                 IF_ENQUEUE(ifq, m);
456 #  if IRIX < 605
457                                 schednetisr(NETISR_IP);
458 #  endif
459                         }
460 # endif
461 # endif /* !SOLARIS */
462                         if (error)
463                                 fr_authstats.fas_quefail++;
464                         else
465                                 fr_authstats.fas_queok++;
466                 } else
467                         error = EINVAL;
468 # if SOLARIS
469                 if (error)
470                         error = EINVAL;
471 # else
472                 /*
473                  * If we experience an error which will result in the packet
474                  * not being processed, make sure we advance to the next one.
475                  */ 
476                 if (error == ENOBUFS) {
477                         fr_authused--;
478                         fra->fra_index = -1;
479                         fra->fra_pass = 0;
480                         if (i == fr_authstart) {
481                                 while (fra->fra_index == -1) {
482                                         i++;
483                                         if (i == FR_NUMAUTH)
484                                                 i = 0;
485                                         fr_authstart = i;
486                                         if (i == fr_authend)
487                                                 break;
488                                 }
489                                 if (fr_authstart == fr_authend) {
490                                         fr_authnext = 0;
491                                         fr_authstart = fr_authend = 0;
492                                 }
493                         }
494                 }
495 # endif
496 #endif /* _KERNEL */
497                 SPL_X(s);
498                 break;
499         default :
500                 error = EINVAL;
501                 break;
502         }
503         return error;
504 }
505
506
507 /*
508  * Free all network buffer memory used to keep saved packets.
509  */
510 void fr_authunload()
511 {
512         int i;
513         frauthent_t *fae, **faep;
514         frentry_t *fr, **frp;
515         mb_t *m;
516
517         WRITE_ENTER(&ipf_auth);
518         for (i = 0; i < FR_NUMAUTH; i++) {
519                 if ((m = fr_authpkts[i])) {
520                         FREE_MB_T(m);
521                         fr_authpkts[i] = NULL;
522                         fr_auth[i].fra_index = -1;
523                 }
524         }
525
526
527         for (faep = &fae_list; (fae = *faep); ) {
528                 *faep = fae->fae_next;
529                 KFREE(fae);
530         }
531         ipauth = NULL;
532         RWLOCK_EXIT(&ipf_auth);
533
534         if (fr_authlist) {
535                 /*
536                  * We *MuST* reget ipf_auth because otherwise we won't get the
537                  * locks in the right order and risk deadlock.
538                  * We need ipf_mutex here to prevent a rule from using it
539                  * inside fr_check().
540                  */
541                 WRITE_ENTER(&ipf_mutex);
542                 WRITE_ENTER(&ipf_auth);
543                 for (frp = &fr_authlist; (fr = *frp); ) {
544                         if (fr->fr_ref == 1) {
545                                 *frp = fr->fr_next;
546                                 KFREE(fr);
547                         } else
548                                 frp = &fr->fr_next;
549                 }
550                 RWLOCK_EXIT(&ipf_auth);
551                 RWLOCK_EXIT(&ipf_mutex);
552         }
553 }
554
555
556 /*
557  * Slowly expire held auth records.  Timeouts are set
558  * in expectation of this being called twice per second.
559  */
560 void fr_authexpire()
561 {
562         int i;
563         frauth_t *fra;
564         frauthent_t *fae, **faep;
565         frentry_t *fr, **frp;
566         mb_t *m;
567 #if !SOLARIS && defined(_KERNEL) && !defined(__DragonFly__)
568         int s;
569 #endif
570
571         if (fr_auth_lock)
572                 return;
573
574         SPL_NET(s);
575         WRITE_ENTER(&ipf_auth);
576         for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
577                 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
578                         FREE_MB_T(m);
579                         fr_authpkts[i] = NULL;
580                         fr_auth[i].fra_index = -1;
581                         fr_authstats.fas_expire++;
582                         fr_authused--;
583                 }
584         }
585
586         for (faep = &fae_list; (fae = *faep); ) {
587                 if (!--fae->fae_age) {
588                         *faep = fae->fae_next;
589                         KFREE(fae);
590                         fr_authstats.fas_expire++;
591                 } else
592                         faep = &fae->fae_next;
593         }
594         if (fae_list != NULL)
595                 ipauth = &fae_list->fae_fr;
596         else
597                 ipauth = NULL;
598
599         for (frp = &fr_authlist; (fr = *frp); ) {
600                 if (fr->fr_ref == 1) {
601                         *frp = fr->fr_next;
602                         KFREE(fr);
603                 } else
604                         frp = &fr->fr_next;
605         }
606         RWLOCK_EXIT(&ipf_auth);
607         SPL_X(s);
608 }
609
610 int fr_preauthcmd(cmd, fr, frptr)
611 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
612         (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
613 u_long cmd;
614 #else
615 int cmd;
616 #endif                 
617 frentry_t *fr, **frptr;
618 {
619         frauthent_t *fae, **faep;
620         int error = 0;
621 #if defined(KERNEL) && !SOLARIS && !defined(__DragonFly__)
622         int s;
623 #endif
624
625         if ((cmd != SIOCADAFR) && (cmd != SIOCRMAFR)) {
626                 /* Should not happen */
627                 printf("fr_preauthcmd called with bad cmd 0x%lx", (u_long)cmd);
628                 return EIO;
629         }
630         
631         for (faep = &fae_list; (fae = *faep); )
632                 if (&fae->fae_fr == fr)
633                         break;
634                 else
635                         faep = &fae->fae_next;
636         if (cmd == SIOCRMAFR) {
637                 if (!fr || !frptr)
638                         error = EINVAL;
639                 else if (!fae)
640                         error = ESRCH;
641                 else {
642                         WRITE_ENTER(&ipf_auth);
643                         SPL_NET(s);
644                         *faep = fae->fae_next;
645                         *frptr = fr->fr_next;
646                         SPL_X(s);
647                         RWLOCK_EXIT(&ipf_auth);
648                         KFREE(fae);
649                 }
650         } else if (fr && frptr) {
651                 KMALLOC(fae, frauthent_t *);
652                 if (fae != NULL) {
653                         bcopy((char *)fr, (char *)&fae->fae_fr,
654                               sizeof(*fr));
655                         WRITE_ENTER(&ipf_auth);
656                         SPL_NET(s);
657                         fae->fae_age = fr_defaultauthage;
658                         fae->fae_fr.fr_hits = 0;
659                         fae->fae_fr.fr_next = *frptr;
660                         *frptr = &fae->fae_fr;
661                         fae->fae_next = *faep;
662                         *faep = fae;
663                         ipauth = &fae_list->fae_fr;
664                         SPL_X(s);
665                         RWLOCK_EXIT(&ipf_auth);
666                 } else
667                         error = ENOMEM;
668         } else
669                 error = EINVAL;
670         return error;
671 }