]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/ipfilter/netinet/ip_auth.c
This commit was generated by cvs2svn to compensate for changes in r90209,
[FreeBSD/FreeBSD.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 #include <sys/errno.h>
7 #include <sys/types.h>
8 #include <sys/param.h>
9 #include <sys/time.h>
10 #include <sys/file.h>
11 #if !defined(_KERNEL) && !defined(KERNEL)
12 # include <stdio.h>
13 # include <stdlib.h>
14 # include <string.h>
15 #endif
16 #if (defined(KERNEL) || defined(_KERNEL)) && (__FreeBSD_version >= 220000)
17 # include <sys/filio.h>
18 # include <sys/fcntl.h>
19 #else
20 # include <sys/ioctl.h>
21 #endif
22 #include <sys/uio.h>
23 #ifndef linux
24 # include <sys/protosw.h>
25 #endif
26 #include <sys/socket.h>
27 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
28 # include <sys/systm.h>
29 #endif
30 #if !defined(__SVR4) && !defined(__svr4__)
31 # ifndef linux
32 #  include <sys/mbuf.h>
33 # endif
34 #else
35 # include <sys/filio.h>
36 # include <sys/byteorder.h>
37 # ifdef _KERNEL
38 #  include <sys/dditypes.h>
39 # endif
40 # include <sys/stream.h>
41 # include <sys/kmem.h>
42 #endif
43 #if (_BSDI_VERSION >= 199802) || (__FreeBSD_version >= 400000)
44 # include <sys/queue.h>
45 #endif
46 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(bsdi)
47 # include <machine/cpu.h>
48 #endif
49 #include <net/if.h>
50 #ifdef sun
51 # include <net/af.h>
52 #endif
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #ifndef KERNEL
58 # define        KERNEL
59 # define        NOT_KERNEL
60 #endif
61 #ifndef linux
62 # include <netinet/ip_var.h>
63 #endif
64 #ifdef  NOT_KERNEL
65 # undef KERNEL
66 #endif
67 #ifdef __sgi
68 # ifdef IFF_DRVRLOCK /* IRIX6 */
69 #  include <sys/hashing.h>
70 # endif
71 #endif
72 #include <netinet/tcp.h>
73 #if defined(__sgi) && !defined(IFF_DRVRLOCK) /* IRIX < 6 */
74 extern struct ifqueue   ipintrq;                /* ip packet input queue */
75 #else
76 # ifndef linux
77 #  if __FreeBSD_version >= 300000
78 #   include <net/if_var.h>
79 #  endif
80 #  include <netinet/in_var.h>
81 #  include <netinet/tcp_fsm.h>
82 # endif
83 #endif
84 #include <netinet/udp.h>
85 #include <netinet/ip_icmp.h>
86 #include "netinet/ip_compat.h"
87 #include <netinet/tcpip.h>
88 #include "netinet/ip_fil.h"
89 #include "netinet/ip_auth.h"
90 #if !SOLARIS && !defined(linux)
91 # include <net/netisr.h>
92 # ifdef __FreeBSD__
93 #  include <machine/cpufunc.h>
94 # endif
95 #endif
96 #if (__FreeBSD_version >= 300000)
97 # include <sys/malloc.h>
98 # if (defined(_KERNEL) || defined(KERNEL)) && !defined(IPFILTER_LKM)
99 #  include <sys/libkern.h>
100 #  include <sys/systm.h>
101 # endif
102 #endif
103
104 #if !defined(lint)
105 /* static const char rcsid[] = "@(#)$Id: ip_auth.c,v 2.11.2.12 2001/07/18 14:57:08 darrenr Exp $"; */
106 static const char rcsid[] = "@(#)$FreeBSD$";
107 #endif
108
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                 register u_short bo;
281
282                 bo = ip->ip_len;
283                 ip->ip_len = htons(bo);
284 # if !SOLARIS && !defined(__NetBSD__) && !defined(__FreeBSD__)
285                 /* 4.4BSD converts this ip_input.c, but I don't in solaris.c */
286                 bo = ip->ip_id;
287                 ip->ip_id = htons(bo);
288 # endif
289                 bo = ip->ip_off;
290                 ip->ip_off = htons(bo);
291         }
292 # endif
293         m->b_rptr -= qif->qf_off;
294         fr_authpkts[i] = *(mblk_t **)fin->fin_mp;
295         fra->fra_q = qif->qf_q;
296         cv_signal(&ipfauthwait);
297 #else
298 # if defined(BSD) && !defined(sparc) && (BSD >= 199306)
299         if (!fin->fin_out) {
300                 HTONS(ip->ip_len);
301                 HTONS(ip->ip_off);
302         }
303 # endif
304         fr_authpkts[i] = m;
305         WAKEUP(&fr_authnext);
306 #endif
307         return 1;
308 }
309
310
311 int fr_auth_ioctl(data, mode, cmd, fr, frptr)
312 caddr_t data;
313 int mode;
314 #if defined(__NetBSD__) || defined(__OpenBSD__) || (FreeBSD_version >= 300003)
315 u_long cmd;
316 #else
317 int cmd;
318 #endif
319 frentry_t *fr, **frptr;
320 {
321         mb_t *m;
322 #if defined(_KERNEL) && !SOLARIS
323         struct ifqueue *ifq;
324         int s;
325 #endif
326         frauth_t auth, *au = &auth, *fra;
327         frauthent_t *fae, **faep;
328         int i, error = 0;
329
330         switch (cmd)
331         {
332         case SIOCSTLCK :
333                 error = fr_lock(data, &fr_auth_lock);
334                 break;
335         case SIOCINIFR :
336         case SIOCRMIFR :
337         case SIOCADIFR :
338                 error = EINVAL;
339                 break;
340         case SIOCINAFR :
341                 error = EINVAL;
342                 break;
343         case SIOCRMAFR :
344         case SIOCADAFR :
345                 for (faep = &fae_list; (fae = *faep); )
346                         if (&fae->fae_fr == fr)
347                                 break;
348                         else
349                                 faep = &fae->fae_next;
350                 if (cmd == SIOCRMAFR) {
351                         if (!fr || !frptr)
352                                 error = EINVAL;
353                         else if (!fae)
354                                 error = ESRCH;
355                         else {
356                                 WRITE_ENTER(&ipf_auth);
357                                 SPL_NET(s);
358                                 *faep = fae->fae_next;
359                                 *frptr = fr->fr_next;
360                                 SPL_X(s);
361                                 RWLOCK_EXIT(&ipf_auth);
362                                 KFREE(fae);
363                         }
364                 } else if (fr && frptr) {
365                         KMALLOC(fae, frauthent_t *);
366                         if (fae != NULL) {
367                                 bcopy((char *)fr, (char *)&fae->fae_fr,
368                                       sizeof(*fr));
369                                 WRITE_ENTER(&ipf_auth);
370                                 SPL_NET(s);
371                                 fae->fae_age = fr_defaultauthage;
372                                 fae->fae_fr.fr_hits = 0;
373                                 fae->fae_fr.fr_next = *frptr;
374                                 *frptr = &fae->fae_fr;
375                                 fae->fae_next = *faep;
376                                 *faep = fae;
377                                 ipauth = &fae_list->fae_fr;
378                                 SPL_X(s);
379                                 RWLOCK_EXIT(&ipf_auth);
380                         } else
381                                 error = ENOMEM;
382                 } else
383                         error = EINVAL;
384                 break;
385         case SIOCATHST:
386                 READ_ENTER(&ipf_auth);
387                 fr_authstats.fas_faelist = fae_list;
388                 RWLOCK_EXIT(&ipf_auth);
389                 error = IWCOPYPTR((char *)&fr_authstats, data,
390                                    sizeof(fr_authstats));
391                 break;
392         case SIOCAUTHW:
393                 if (!(mode & FWRITE)) {
394                         error = EPERM;
395                         break;
396                 }
397 fr_authioctlloop:
398                 READ_ENTER(&ipf_auth);
399                 if ((fr_authnext != fr_authend) && fr_authpkts[fr_authnext]) {
400                         error = IWCOPYPTR((char *)&fr_auth[fr_authnext], data,
401                                           sizeof(frauth_t));
402                         RWLOCK_EXIT(&ipf_auth);
403                         if (error)
404                                 break;
405                         WRITE_ENTER(&ipf_auth);
406                         SPL_NET(s);
407                         fr_authnext++;
408                         if (fr_authnext == FR_NUMAUTH)
409                                 fr_authnext = 0;
410                         SPL_X(s);
411                         RWLOCK_EXIT(&ipf_auth);
412                         return 0;
413                 }
414 #ifdef  _KERNEL
415 # if    SOLARIS
416                 mutex_enter(&ipf_authmx);
417                 if (!cv_wait_sig(&ipfauthwait, &ipf_authmx)) {
418                         mutex_exit(&ipf_authmx);
419                         return EINTR;
420                 }
421                 mutex_exit(&ipf_authmx);
422 # else
423                 error = SLEEP(&fr_authnext, "fr_authnext");
424 # endif
425 #endif
426                 RWLOCK_EXIT(&ipf_auth);
427                 if (!error)
428                         goto fr_authioctlloop;
429                 break;
430         case SIOCAUTHR:
431                 if (!(mode & FWRITE)) {
432                         error = EPERM;
433                         break;
434                 }
435                 error = IRCOPYPTR(data, (caddr_t)&auth, sizeof(auth));
436                 if (error)
437                         return error;
438                 WRITE_ENTER(&ipf_auth);
439                 SPL_NET(s);
440                 i = au->fra_index;
441                 fra = fr_auth + i;
442                 if ((i < 0) || (i > FR_NUMAUTH) ||
443                     (fra->fra_info.fin_id != au->fra_info.fin_id)) {
444                         SPL_X(s);
445                         RWLOCK_EXIT(&ipf_auth);
446                         return EINVAL;
447                 }
448                 m = fr_authpkts[i];
449                 fra->fra_index = -2;
450                 fra->fra_pass = au->fra_pass;
451                 fr_authpkts[i] = NULL;
452                 RWLOCK_EXIT(&ipf_auth);
453 #ifdef  _KERNEL
454                 if (m && au->fra_info.fin_out) {
455 # if SOLARIS
456                         error = fr_qout(fra->fra_q, m);
457 # else /* SOLARIS */
458                         struct route ro;
459
460                         bzero((char *)&ro, sizeof(ro));
461 #  if ((_BSDI_VERSION >= 199802) && (_BSDI_VERSION < 200005)) || \
462        defined(__OpenBSD__)
463                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL,
464                                           NULL);
465 #  else
466                         error = ip_output(m, NULL, &ro, IP_FORWARDING, NULL);
467 #  endif
468                         if (ro.ro_rt) {
469                                 RTFREE(ro.ro_rt);
470                         }
471 # endif /* SOLARIS */
472                         if (error)
473                                 fr_authstats.fas_sendfail++;
474                         else
475                                 fr_authstats.fas_sendok++;
476                 } else if (m) {
477 # if SOLARIS
478                         error = fr_qin(fra->fra_q, m);
479 # else /* SOLARIS */
480                         if (! IF_HANDOFF(&ipintrq, m, NULL))
481                                 error = ENOBUFS;
482                         else
483                                 schednetisr(NETISR_IP);
484 # endif /* SOLARIS */
485                         if (error)
486                                 fr_authstats.fas_quefail++;
487                         else
488                                 fr_authstats.fas_queok++;
489                 } else
490                         error = EINVAL;
491 # if SOLARIS
492                 if (error)
493                         error = EINVAL;
494 # else
495                 /*
496                  * If we experience an error which will result in the packet
497                  * not being processed, make sure we advance to the next one.
498                  */ 
499                 if (error == ENOBUFS) {
500                         fr_authused--;
501                         fra->fra_index = -1;
502                         fra->fra_pass = 0;
503                         if (i == fr_authstart) {
504                                 while (fra->fra_index == -1) {
505                                         i++;
506                                         if (i == FR_NUMAUTH)
507                                                 i = 0;
508                                         fr_authstart = i;
509                                         if (i == fr_authend)
510                                                 break;
511                                 }
512                                 if (fr_authstart == fr_authend) {
513                                         fr_authnext = 0;
514                                         fr_authstart = fr_authend = 0;
515                                 }
516                         }
517                 }
518 # endif
519 #endif /* _KERNEL */
520                 SPL_X(s);
521                 break;
522         default :
523                 error = EINVAL;
524                 break;
525         }
526         return error;
527 }
528
529
530 #ifdef  _KERNEL
531 /*
532  * Free all network buffer memory used to keep saved packets.
533  */
534 void fr_authunload()
535 {
536         register int i;
537         register frauthent_t *fae, **faep;
538         frentry_t *fr, **frp;
539         mb_t *m;
540
541         WRITE_ENTER(&ipf_auth);
542         for (i = 0; i < FR_NUMAUTH; i++) {
543                 if ((m = fr_authpkts[i])) {
544                         FREE_MB_T(m);
545                         fr_authpkts[i] = NULL;
546                         fr_auth[i].fra_index = -1;
547                 }
548         }
549
550
551         for (faep = &fae_list; (fae = *faep); ) {
552                 *faep = fae->fae_next;
553                 KFREE(fae);
554         }
555         ipauth = NULL;
556         RWLOCK_EXIT(&ipf_auth);
557
558         if (fr_authlist) {
559                 /*
560                  * We *MuST* reget ipf_auth because otherwise we won't get the
561                  * locks in the right order and risk deadlock.
562                  * We need ipf_mutex here to prevent a rule from using it
563                  * inside fr_check().
564                  */
565                 WRITE_ENTER(&ipf_mutex);
566                 WRITE_ENTER(&ipf_auth);
567                 for (frp = &fr_authlist; (fr = *frp); ) {
568                         if (fr->fr_ref == 1) {
569                                 *frp = fr->fr_next;
570                                 KFREE(fr);
571                         } else
572                                 frp = &fr->fr_next;
573                 }
574                 RWLOCK_EXIT(&ipf_auth);
575                 RWLOCK_EXIT(&ipf_mutex);
576         }
577 }
578
579
580 /*
581  * Slowly expire held auth records.  Timeouts are set
582  * in expectation of this being called twice per second.
583  */
584 void fr_authexpire()
585 {
586         register int i;
587         register frauth_t *fra;
588         register frauthent_t *fae, **faep;
589         register frentry_t *fr, **frp;
590         mb_t *m;
591 #if !SOLARIS
592         int s;
593 #endif
594
595         if (fr_auth_lock)
596                 return;
597
598         SPL_NET(s);
599         WRITE_ENTER(&ipf_auth);
600         for (i = 0, fra = fr_auth; i < FR_NUMAUTH; i++, fra++) {
601                 if ((!--fra->fra_age) && (m = fr_authpkts[i])) {
602                         FREE_MB_T(m);
603                         fr_authpkts[i] = NULL;
604                         fr_auth[i].fra_index = -1;
605                         fr_authstats.fas_expire++;
606                         fr_authused--;
607                 }
608         }
609
610         for (faep = &fae_list; (fae = *faep); ) {
611                 if (!--fae->fae_age) {
612                         *faep = fae->fae_next;
613                         KFREE(fae);
614                         fr_authstats.fas_expire++;
615                 } else
616                         faep = &fae->fae_next;
617         }
618         ipauth = &fae_list->fae_fr;
619
620         for (frp = &fr_authlist; (fr = *frp); ) {
621                 if (fr->fr_ref == 1) {
622                         *frp = fr->fr_next;
623                         KFREE(fr);
624                 } else
625                         frp = &fr->fr_next;
626         }
627         RWLOCK_EXIT(&ipf_auth);
628         SPL_X(s);
629 }
630 #endif