]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ipfilter/ip_sfil.c
This commit was generated by cvs2svn to compensate for changes in r96539,
[FreeBSD/FreeBSD.git] / contrib / ipfilter / ip_sfil.c
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * I hate legaleese, don't you ?
7  */
8 #if !defined(lint)
9 static const char sccsid[] = "%W% %G% (C) 1993-2000 Darren Reed";
10 static const char rcsid[] = "@(#)$Id: ip_sfil.c,v 2.23.2.16 2002/04/05 08:43:25 darrenr Exp $";
11 #endif
12
13 #include <sys/types.h>
14 #include <sys/errno.h>
15 #include <sys/param.h>
16 #include <sys/cpuvar.h>
17 #include <sys/open.h>
18 #include <sys/ioctl.h>
19 #include <sys/filio.h>
20 #include <sys/systm.h>
21 #include <sys/cred.h>
22 #include <sys/ddi.h>
23 #include <sys/sunddi.h>
24 #include <sys/ksynch.h>
25 #include <sys/kmem.h>
26 #include <sys/mkdev.h>
27 #include <sys/protosw.h>
28 #include <sys/socket.h>
29 #include <sys/dditypes.h>
30 #include <sys/cmn_err.h>
31 #include <net/if.h>
32 #include <net/af.h>
33 #include <net/route.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <netinet/ip_var.h>
38 #include <netinet/tcp.h>
39 #include <netinet/udp.h>
40 #include <netinet/tcpip.h>
41 #include <netinet/ip_icmp.h>
42 #include "ip_compat.h"
43 #ifdef  USE_INET6
44 # include <netinet/icmp6.h>
45 #endif
46 #include "ip_fil.h"
47 #include "ip_state.h"
48 #include "ip_nat.h"
49 #include "ip_frag.h"
50 #include "ip_auth.h"
51 #include "ip_proxy.h"
52 #include <inet/ip_ire.h>
53 #ifndef MIN
54 #define MIN(a,b)        (((a)<(b))?(a):(b))
55 #endif
56
57
58 extern  fr_flags, fr_active;
59
60 int     fr_running = 0;
61 int     ipl_unreach = ICMP_UNREACH_HOST;
62 u_long  ipl_frouteok[2] = {0, 0};
63 static  int     frzerostats __P((caddr_t));
64 static  u_long  *ip_ttl_ptr;
65
66 static  int     frrequest __P((minor_t, int, caddr_t, int));
67 static  int     send_ip __P((fr_info_t *fin, mblk_t *m));
68 kmutex_t        ipl_mutex, ipf_authmx, ipf_rw;
69 KRWLOCK_T       ipf_mutex, ipfs_mutex, ipf_solaris;
70 KRWLOCK_T       ipf_frag, ipf_state, ipf_nat, ipf_natfrag, ipf_auth;
71 kcondvar_t      iplwait, ipfauthwait;
72
73
74 int ipldetach()
75 {
76         int     i;
77
78 #ifdef  IPFDEBUG
79         cmn_err(CE_CONT, "ipldetach()\n");
80 #endif
81 #ifdef  IPFILTER_LOG
82         for (i = IPL_LOGMAX; i >= 0; i--)
83                 ipflog_clear(i);
84 #endif
85         i = FR_INQUE|FR_OUTQUE;
86         (void) frflush(IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
87         ipfr_unload();
88         fr_stateunload();
89         ip_natunload();
90         cv_destroy(&iplwait);
91         cv_destroy(&ipfauthwait);
92         mutex_destroy(&ipf_authmx);
93         mutex_destroy(&ipl_mutex);
94         mutex_destroy(&ipf_rw);
95         RW_DESTROY(&ipf_mutex);
96         RW_DESTROY(&ipf_frag);
97         RW_DESTROY(&ipf_state);
98         RW_DESTROY(&ipf_natfrag);
99         RW_DESTROY(&ipf_nat);
100         RW_DESTROY(&ipf_auth);
101         RW_DESTROY(&ipfs_mutex);
102         /* NOTE: This lock is acquired in ipf_detach */
103         RWLOCK_EXIT(&ipf_solaris);
104         RW_DESTROY(&ipf_solaris);
105         return 0;
106 }
107
108
109 int iplattach __P((void))
110 {
111         int i;
112
113 #ifdef  IPFDEBUG
114         cmn_err(CE_CONT, "iplattach()\n");
115 #endif
116         bzero((char *)frcache, sizeof(frcache));
117         mutex_init(&ipf_rw, "ipf rw mutex", MUTEX_DRIVER, NULL);
118         mutex_init(&ipl_mutex, "ipf log mutex", MUTEX_DRIVER, NULL);
119         mutex_init(&ipf_authmx, "ipf auth log mutex", MUTEX_DRIVER, NULL);
120         RWLOCK_INIT(&ipf_solaris, "ipf filter load/unload mutex", NULL);
121         RWLOCK_INIT(&ipf_mutex, "ipf filter rwlock", NULL);
122         RWLOCK_INIT(&ipfs_mutex, "ipf solaris mutex", NULL);
123         RWLOCK_INIT(&ipf_frag, "ipf fragment rwlock", NULL);
124         RWLOCK_INIT(&ipf_state, "ipf IP state rwlock", NULL);
125         RWLOCK_INIT(&ipf_nat, "ipf IP NAT rwlock", NULL);
126         RWLOCK_INIT(&ipf_natfrag, "ipf IP NAT-Frag rwlock", NULL);
127         RWLOCK_INIT(&ipf_auth, "ipf IP User-Auth rwlock", NULL);
128         cv_init(&iplwait, "ipl condvar", CV_DRIVER, NULL);
129         cv_init(&ipfauthwait, "ipf auth condvar", CV_DRIVER, NULL);
130 #ifdef  IPFILTER_LOG
131         ipflog_init();
132 #endif
133         if (nat_init() == -1)
134                 return -1;
135         if (fr_stateinit() == -1)
136                 return -1;
137         if (appr_init() == -1)
138                 return -1;
139
140         ip_ttl_ptr = NULL;
141         /*
142          * XXX - There is no terminator for this array, so it is not possible
143          * to tell if what we are looking for is missing and go off the end
144          * of the array.
145          */
146         for (i = 0; ; i++) {
147                 if (!strcmp(ip_param_arr[i].ip_param_name, "ip_def_ttl")) {
148                         ip_ttl_ptr = &ip_param_arr[i].ip_param_value;
149                         break;
150                 }
151         }
152         return 0;
153 }
154
155
156 static  int     frzerostats(data)
157 caddr_t data;
158 {
159         friostat_t fio;
160         int error;
161
162         fr_getstat(&fio);
163         error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
164         if (error)
165                 return error;
166
167         bzero((char *)frstats, sizeof(*frstats) * 2);
168
169         return 0;
170 }
171
172
173 /*
174  * Filter ioctl interface.
175  */
176 int iplioctl(dev, cmd, data, mode, cp, rp)
177 dev_t dev;
178 int cmd;
179 #if SOLARIS2 >= 7
180 intptr_t data;
181 #else
182 int *data;
183 #endif
184 int mode;
185 cred_t *cp;
186 int *rp;
187 {
188         int error = 0, tmp;
189         minor_t unit;
190
191 #ifdef  IPFDEBUG
192         cmn_err(CE_CONT, "iplioctl(%x,%x,%x,%d,%x,%d)\n",
193                 dev, cmd, data, mode, cp, rp);
194 #endif
195         unit = getminor(dev);
196         if (IPL_LOGMAX < unit)
197                 return ENXIO;
198
199         if (fr_running <= 0)
200                 return 0;
201
202         READ_ENTER(&ipf_solaris);
203         if (unit == IPL_LOGNAT) {
204                 error = nat_ioctl((caddr_t)data, cmd, mode);
205                 RWLOCK_EXIT(&ipf_solaris);
206                 return error;
207         }
208         if (unit == IPL_LOGSTATE) {
209                 error = fr_state_ioctl((caddr_t)data, cmd, mode);
210                 RWLOCK_EXIT(&ipf_solaris);
211                 return error;
212         }
213         if (unit == IPL_LOGAUTH) {
214                 error = fr_auth_ioctl((caddr_t)data, mode, cmd, NULL, NULL);
215                 RWLOCK_EXIT(&ipf_solaris);
216                 return error;
217         }
218
219         switch (cmd) {
220         case SIOCFRENB :
221         {
222                 u_int   enable;
223
224                 if (!(mode & FWRITE))
225                         error = EPERM;
226                 else
227                         error = IRCOPY((caddr_t)data, (caddr_t)&enable,
228                                        sizeof(enable));
229                 break;
230         }
231         case SIOCSETFF :
232                 if (!(mode & FWRITE))
233                         error = EPERM;
234                 else {
235                         WRITE_ENTER(&ipf_mutex);
236                         error = IRCOPY((caddr_t)data, (caddr_t)&fr_flags,
237                                sizeof(fr_flags));
238                         RWLOCK_EXIT(&ipf_mutex);
239                 }
240                 break;
241         case SIOCGETFF :
242                 error = IWCOPY((caddr_t)&fr_flags, (caddr_t)data,
243                                sizeof(fr_flags));
244                 if (error)
245                         error = EFAULT;
246                 break;
247         case SIOCINAFR :
248         case SIOCRMAFR :
249         case SIOCADAFR :
250         case SIOCZRLST :
251                 if (!(mode & FWRITE))
252                         error = EPERM;
253                 else
254                         error = frrequest(unit, cmd, (caddr_t)data, fr_active);
255                 break;
256         case SIOCINIFR :
257         case SIOCRMIFR :
258         case SIOCADIFR :
259                 if (!(mode & FWRITE))
260                         error = EPERM;
261                 else
262                         error = frrequest(unit, cmd, (caddr_t)data,
263                                           1 - fr_active);
264                 break;
265         case SIOCSWAPA :
266                 if (!(mode & FWRITE))
267                         error = EPERM;
268                 else {
269                         WRITE_ENTER(&ipf_mutex);
270                         bzero((char *)frcache, sizeof(frcache[0]) * 2);
271                         error = IWCOPY((caddr_t)&fr_active, (caddr_t)data,
272                                        sizeof(fr_active));
273                         if (error)
274                                 error = EFAULT;
275                         fr_active = 1 - fr_active;
276                         RWLOCK_EXIT(&ipf_mutex);
277                 }
278                 break;
279         case SIOCGETFS :
280         {
281                 friostat_t      fio;
282
283                 READ_ENTER(&ipf_mutex);
284                 fr_getstat(&fio);
285                 RWLOCK_EXIT(&ipf_mutex);
286                 error = IWCOPYPTR((caddr_t)&fio, (caddr_t)data, sizeof(fio));
287                 if (error)
288                         error = EFAULT;
289                 break;
290         }
291         case SIOCFRZST :
292                 if (!(mode & FWRITE))
293                         error = EPERM;
294                 else
295                         error = frzerostats((caddr_t)data);
296                 break;
297         case    SIOCIPFFL :
298                 if (!(mode & FWRITE))
299                         error = EPERM;
300                 else {
301                         error = IRCOPY((caddr_t)data, (caddr_t)&tmp,
302                                        sizeof(tmp));
303                         if (!error) {
304                                 tmp = frflush(unit, tmp);
305                                 error = IWCOPY((caddr_t)&tmp, (caddr_t)data,
306                                                sizeof(tmp));
307                                 if (error)
308                                         error = EFAULT;
309                         }
310                 }
311                 break;
312         case SIOCSTLCK :
313                 error = IRCOPY((caddr_t)data, (caddr_t)&tmp, sizeof(tmp));
314                 if (!error) {
315                         fr_state_lock = tmp;
316                         fr_nat_lock = tmp;
317                         fr_frag_lock = tmp;
318                         fr_auth_lock = tmp;
319                 } else
320                         error = EFAULT;
321         break;
322 #ifdef  IPFILTER_LOG
323         case    SIOCIPFFB :
324                 if (!(mode & FWRITE))
325                         error = EPERM;
326                 else {
327                         tmp = ipflog_clear(unit);
328                         error = IWCOPY((caddr_t)&tmp, (caddr_t)data,
329                                        sizeof(tmp));
330                         if (error)
331                                 error = EFAULT;
332                 }
333                 break;
334 #endif /* IPFILTER_LOG */
335         case SIOCFRSYN :
336                 if (!(mode & FWRITE))
337                         error = EPERM;
338                 else
339                         error = ipfsync();
340                 break;
341         case SIOCGFRST :
342                 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), (caddr_t)data,
343                                   sizeof(ipfrstat_t));
344                 break;
345         case FIONREAD :
346         {
347 #ifdef  IPFILTER_LOG
348                 int copy = (int)iplused[IPL_LOGIPF];
349
350                 error = IWCOPY((caddr_t)&copy, (caddr_t)data, sizeof(copy));
351                 if (error)
352                         error = EFAULT;
353 #endif
354                 break;
355         }
356         default :
357                 error = EINVAL;
358                 break;
359         }
360         RWLOCK_EXIT(&ipf_solaris);
361         return error;
362 }
363
364
365 ill_t   *get_unit(name, v)
366 char    *name;
367 int     v;
368 {
369         size_t len = strlen(name) + 1;  /* includes \0 */
370         ill_t *il;
371         int sap;
372
373         if (v == 4)
374                 sap = 0x0800;
375         else if (v == 6)
376                 sap = 0x86dd;
377         else
378                 return NULL;
379         for (il = ill_g_head; il; il = il->ill_next)
380                 if ((len == il->ill_name_length) && (il->ill_sap == sap) &&
381                     !strncmp(il->ill_name, name, len))
382                         return il;
383         return NULL;
384 }
385
386
387 static int frrequest(unit, req, data, set)
388 minor_t unit;
389 int req, set;
390 caddr_t data;
391 {
392         register frentry_t *fp, *f, **fprev;
393         register frentry_t **ftail;
394         frgroup_t *fg = NULL;
395         int error = 0, in, i;
396         u_int *p, *pp;
397         frdest_t *fdp;
398         frentry_t fr;
399         u_32_t group;
400         ipif_t *ipif;
401         ill_t *ill;
402         ire_t *ire;
403
404         fp = &fr;
405         error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
406         if (error)
407                 return EFAULT;
408         fp->fr_ref = 0;
409 #if SOLARIS2 >= 8
410         if (fp->fr_v == 4)
411                 fp->fr_sap = IP_DL_SAP;
412         else if (fp->fr_v == 6)
413                 fp->fr_sap = IP6_DL_SAP;
414         else
415                 return EINVAL;
416 #else
417         fp->fr_sap = 0;
418 #endif
419
420         WRITE_ENTER(&ipf_mutex);
421         /*
422          * Check that the group number does exist and that if a head group
423          * has been specified, doesn't exist.
424          */
425         if ((req != SIOCZRLST) && fp->fr_grhead &&
426             fr_findgroup(fp->fr_grhead, fp->fr_flags, unit, set, NULL)) {
427                 error = EEXIST;
428                 goto out;
429         }
430         if ((req != SIOCZRLST) && fp->fr_group &&
431             !fr_findgroup(fp->fr_group, fp->fr_flags, unit, set, NULL)) {
432                 error = ESRCH;
433                 goto out;
434         }
435
436         in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
437
438         if (unit == IPL_LOGAUTH)
439                 ftail = fprev = &ipauth;
440         else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
441                 ftail = fprev = &ipacct[in][set];
442         else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
443                 ftail = fprev = &ipfilter[in][set];
444 #ifdef  USE_INET6
445         else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
446                 ftail = fprev = &ipacct6[in][set];
447         else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
448                 ftail = fprev = &ipfilter6[in][set];
449 #endif
450         else {
451                 error = ESRCH;
452                 goto out;
453         }
454
455         group = fp->fr_group;
456         if (group != 0) {
457                 fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL);
458                 if (fg == NULL) {
459                         error = ESRCH;
460                         goto out;
461                 }
462                 ftail = fprev = fg->fg_start;
463         }
464
465         bzero((char *)frcache, sizeof(frcache[0]) * 2);
466
467         for (i = 0; i < 4; i++) {
468                 if ((fp->fr_ifnames[i][1] == '\0') &&
469                     ((fp->fr_ifnames[i][0] == '-') ||
470                      (fp->fr_ifnames[i][0] == '*'))) {
471                         fp->fr_ifas[i] = NULL;
472                 } else if (*fp->fr_ifnames[i]) {
473                         fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
474                         if (!fp->fr_ifas[i])
475                                 fp->fr_ifas[i] = (void *)-1;
476                 }
477         }
478
479         fdp = &fp->fr_dif;
480         fdp->fd_mp = NULL;
481         fp->fr_flags &= ~FR_DUP;
482         if (*fdp->fd_ifname) {
483                 ill = get_unit(fdp->fd_ifname, (int)fp->fr_v);
484                 if (!ill)
485                         ire = (ire_t *)-1;
486                 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 4)) {
487 #if SOLARIS2 > 5
488                         ire = ire_ctable_lookup(ipif->ipif_local_addr, 0,
489                                                 IRE_LOCAL, NULL, NULL,
490                                                 MATCH_IRE_TYPE);
491 #else
492                         ire = ire_lookup_myaddr(ipif->ipif_local_addr);
493 #endif
494                         if (!ire)
495                                 ire = (ire_t *)-1;
496                         else
497                                 fp->fr_flags |= FR_DUP;
498                 }
499 #ifdef  USE_INET6
500                 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 6)) {
501                         ire = ire_ctable_lookup_v6(&ipif->ipif_v6lcl_addr, 0,
502                                                    IRE_LOCAL, NULL, NULL,
503                                                    MATCH_IRE_TYPE);
504                         if (!ire)
505                                 ire = (ire_t *)-1;
506                         else
507                                 fp->fr_flags |= FR_DUP;
508                 }
509 #endif
510                 fdp->fd_ifp = (struct ifnet *)ire;
511         }
512
513         fdp = &fp->fr_tif;
514         fdp->fd_mp = NULL;
515         if (*fdp->fd_ifname) {
516                 ill = get_unit(fdp->fd_ifname, (int)fp->fr_v);
517                 if (!ill)
518                         ire = (ire_t *)-1;
519                 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 4)) {
520 #if SOLARIS2 > 5
521                         ire = ire_ctable_lookup(ipif->ipif_local_addr, 0,
522                                                 IRE_LOCAL, NULL, NULL,
523                                                 MATCH_IRE_TYPE);
524 #else
525                         ire = ire_lookup_myaddr(ipif->ipif_local_addr);
526 #endif
527                         if (!ire)
528                                 ire = (ire_t *)-1;
529                 }
530 #ifdef  USE_INET6
531                 else if ((ipif = ill->ill_ipif) && (fp->fr_v == 6)) {
532                         ire = ire_ctable_lookup_v6(&ipif->ipif_v6lcl_addr, 0,
533                                                    IRE_LOCAL, NULL, NULL,
534                                                    MATCH_IRE_TYPE);
535                         if (!ire)
536                                 ire = (ire_t *)-1;
537                 }
538 #endif
539                 fdp->fd_ifp = (struct ifnet *)ire;
540         }
541
542         /*
543          * Look for a matching filter rule, but don't include the next or
544          * interface pointer in the comparison (fr_next, fr_ifa).
545          */
546         for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
547              p < pp; p++)
548                 fp->fr_cksum += *p;
549
550         for (; (f = *ftail); ftail = &f->fr_next)
551                 if ((fp->fr_cksum == f->fr_cksum) &&
552                     !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
553                         break;
554
555         /*
556          * If zero'ing statistics, copy current to caller and zero.
557          */
558         if (req == SIOCZRLST) {
559                 if (!f) {
560                         error = ESRCH;
561                         goto out;
562                 }
563                 MUTEX_DOWNGRADE(&ipf_mutex);
564                 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
565                 if (error)
566                         goto out;
567                 f->fr_hits = 0;
568                 f->fr_bytes = 0;
569                 goto out;
570         }
571
572         if (!f) {
573                 if (req != SIOCINAFR && req != SIOCINIFR)
574                         while ((f = *ftail))
575                                 ftail = &f->fr_next;
576                 else {
577                         if (fp->fr_hits) {
578                                 ftail = fprev;
579                                 while (--fp->fr_hits && (f = *ftail))
580                                         ftail = &f->fr_next;
581                         }
582                         f = NULL;
583                 }
584         }
585
586         if (req == SIOCRMAFR || req == SIOCRMIFR) {
587                 if (!f)
588                         error = ESRCH;
589                 else {
590                         /*
591                          * Only return EBUSY if there is a group list, else
592                          * it's probably just state information referencing
593                          * the rule.
594                          */
595                         if ((f->fr_ref > 1) && f->fr_grp) {
596                                 error = EBUSY;
597                                 goto out;
598                         }
599                         if (fg && fg->fg_head)
600                                 fg->fg_head->fr_ref--;
601                         if (f->fr_grhead)
602                                 fr_delgroup(f->fr_grhead, fp->fr_flags,
603                                             unit, set);
604                         fixskip(fprev, f, -1);
605                         *ftail = f->fr_next;
606                         f->fr_next = NULL;
607                         f->fr_ref--;
608                         if (f->fr_ref == 0)
609                                 KFREE(f);
610                 }
611         } else {
612                 if (f) {
613                         error = EEXIST;
614                 } else {
615                         KMALLOC(f, frentry_t *);
616                         if (f != NULL) {
617                                 if (fg && fg->fg_head)
618                                         fg->fg_head->fr_ref++;
619                                 bcopy((char *)fp, (char *)f, sizeof(*f));
620                                 f->fr_ref = 1;
621                                 f->fr_hits = 0;
622                                 f->fr_next = *ftail;
623                                 *ftail = f;
624                                 if (req == SIOCINIFR || req == SIOCINAFR)
625                                         fixskip(fprev, f, 1);
626                                 f->fr_grp = NULL;
627                                 group = f->fr_grhead;
628                                 if (group != 0)
629                                         fg = fr_addgroup(group, f, unit, set);
630                         } else
631                                 error = ENOMEM;
632                 }
633         }
634 out:
635         RWLOCK_EXIT(&ipf_mutex);
636         return (error);
637 }
638
639
640 /*
641  * routines below for saving IP headers to buffer
642  */
643 int iplopen(devp, flags, otype, cred)
644 dev_t *devp;
645 int flags, otype;
646 cred_t *cred;
647 {
648         minor_t min = getminor(*devp);
649
650 #ifdef  IPFDEBUG
651         cmn_err(CE_CONT, "iplopen(%x,%x,%x,%x)\n", devp, flags, otype, cred);
652 #endif
653         if ((fr_running <= 0) || !(otype & OTYP_CHR))
654                 return ENXIO;
655         min = (IPL_LOGMAX < min) ? ENXIO : 0;
656         return min;
657 }
658
659
660 int iplclose(dev, flags, otype, cred)
661 dev_t dev;
662 int flags, otype;
663 cred_t *cred;
664 {
665         minor_t min = getminor(dev);
666
667 #ifdef  IPFDEBUG
668         cmn_err(CE_CONT, "iplclose(%x,%x,%x,%x)\n", dev, flags, otype, cred);
669 #endif
670         min = (IPL_LOGMAX < min) ? ENXIO : 0;
671         return min;
672 }
673
674 #ifdef  IPFILTER_LOG
675 /*
676  * iplread/ipllog
677  * both of these must operate with at least splnet() lest they be
678  * called during packet processing and cause an inconsistancy to appear in
679  * the filter lists.
680  */
681 int iplread(dev, uio, cp)
682 dev_t dev;
683 register struct uio *uio;
684 cred_t *cp;
685 {
686 #ifdef  IPFDEBUG
687         cmn_err(CE_CONT, "iplread(%x,%x,%x)\n", dev, uio, cp);
688 #endif
689         return ipflog_read(getminor(dev), uio);
690 }
691 #endif /* IPFILTER_LOG */
692
693
694 /*
695  * send_reset - this could conceivably be a call to tcp_respond(), but that
696  * requires a large amount of setting up and isn't any more efficient.
697  */
698 int send_reset(oip, fin)
699 ip_t *oip;
700 fr_info_t *fin;
701 {
702         tcphdr_t *tcp, *tcp2;
703         int tlen, hlen;
704         mblk_t *m;
705 #ifdef  USE_INET6
706         ip6_t *ip6, *oip6 = (ip6_t *)oip;
707 #endif
708         ip_t *ip;
709
710         tcp = (struct tcphdr *)fin->fin_dp;
711         if (tcp->th_flags & TH_RST)
712                 return -1;
713         tlen = (tcp->th_flags & (TH_SYN|TH_FIN)) ? 1 : 0;
714 #ifdef  USE_INET6
715         if (fin->fin_v == 6)
716                 hlen = sizeof(ip6_t);
717         else
718 #endif
719                 hlen = sizeof(ip_t);
720         hlen += sizeof(*tcp2);
721         if ((m = (mblk_t *)allocb(hlen + 16, BPRI_HI)) == NULL)
722                 return -1;
723
724         m->b_rptr += 16;
725         MTYPE(m) = M_DATA;
726         m->b_wptr = m->b_rptr + hlen;
727         bzero((char *)m->b_rptr, hlen);
728         tcp2 = (struct tcphdr *)(m->b_rptr + hlen - sizeof(*tcp2));
729         tcp2->th_dport = tcp->th_sport;
730         tcp2->th_sport = tcp->th_dport;
731         if (tcp->th_flags & TH_ACK) {
732                 tcp2->th_seq = tcp->th_ack;
733                 tcp2->th_flags = TH_RST|TH_ACK;
734         } else {
735                 tcp2->th_ack = ntohl(tcp->th_seq);
736                 tcp2->th_ack += tlen;
737                 tcp2->th_ack = htonl(tcp2->th_ack);
738                 tcp2->th_flags = TH_RST;
739         }
740         tcp2->th_off = sizeof(struct tcphdr) >> 2;
741         tcp2->th_flags = TH_RST|TH_ACK;
742
743         /*
744          * This is to get around a bug in the Solaris 2.4/2.5 TCP checksum
745          * computation that is done by their put routine.
746          */
747         tcp2->th_sum = htons(0x14);
748 #ifdef  USE_INET6
749         if (fin->fin_v == 6) {
750                 ip6 = (ip6_t *)m->b_rptr;
751                 ip6->ip6_src = oip6->ip6_dst;
752                 ip6->ip6_dst = oip6->ip6_src;
753                 ip6->ip6_plen = htons(sizeof(*tcp));
754                 ip6->ip6_nxt = IPPROTO_TCP;
755         } else
756 #endif
757         {
758                 ip = (ip_t *)m->b_rptr;
759                 ip->ip_src.s_addr = oip->ip_dst.s_addr;
760                 ip->ip_dst.s_addr = oip->ip_src.s_addr;
761                 ip->ip_hl = sizeof(*ip) >> 2;
762                 ip->ip_p = IPPROTO_TCP;
763                 ip->ip_len = htons(sizeof(*ip) + sizeof(*tcp));
764                 ip->ip_tos = oip->ip_tos;
765         }
766         return send_ip(fin, m);
767 }
768
769
770 int static send_ip(fin, m)
771 fr_info_t *fin;
772 mblk_t *m;
773 {
774         RWLOCK_EXIT(&ipfs_mutex);
775         RWLOCK_EXIT(&ipf_solaris);
776 #ifdef  USE_INET6
777         if (fin->fin_v == 6) {
778                 extern void ip_wput_v6 __P((queue_t *, mblk_t *));
779                 ip6_t *ip6;
780
781                 ip6 = (ip6_t *)m->b_rptr;
782                 ip6->ip6_flow = 0;
783                 ip6->ip6_vfc = 0x60;
784                 ip6->ip6_hlim = 127;
785                 ip_wput_v6(((qif_t *)fin->fin_qif)->qf_ill->ill_wq, m);
786         } else
787 #endif
788         {
789                 ip_t *ip;
790
791                 ip = (ip_t *)m->b_rptr;
792                 ip->ip_v = IPVERSION;
793                 ip->ip_ttl = (u_char)(*ip_ttl_ptr);
794                 ip_wput(((qif_t *)fin->fin_qif)->qf_ill->ill_wq, m);
795         }
796         READ_ENTER(&ipf_solaris);
797         READ_ENTER(&ipfs_mutex);
798         return 0;
799 }
800
801
802 int send_icmp_err(oip, type, fin, dst)
803 ip_t *oip;
804 int type;
805 fr_info_t *fin;
806 int dst;
807 {
808         struct in_addr dst4;
809         struct icmp *icmp;
810         mblk_t *m, *mb;
811         int hlen, code;
812         qif_t *qif;
813         u_short sz;
814         ill_t *il;
815 #ifdef  USE_INET6
816         ip6_t *ip6, *oip6;
817 #endif
818         ip_t *ip;
819
820         if ((type < 0) || (type > ICMP_MAXTYPE))
821                 return -1;
822
823         code = fin->fin_icode;
824 #ifdef USE_INET6
825         if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
826                 return -1;
827 #endif
828
829         qif = fin->fin_qif;
830         m = fin->fin_qfm;
831
832 #ifdef  USE_INET6
833         if (oip->ip_v == 6) {
834                 oip6 = (ip6_t *)oip;
835                 sz = sizeof(ip6_t);
836                 sz += MIN(m->b_wptr - m->b_rptr, 512);
837                 hlen = sizeof(ip6_t);
838                 type = icmptoicmp6types[type];
839                 if (type == ICMP6_DST_UNREACH)
840                         code = icmptoicmp6unreach[code];
841         } else
842 #endif
843         {
844                 if ((oip->ip_p == IPPROTO_ICMP) &&
845                     !(fin->fin_fi.fi_fl & FI_SHORT))
846                         switch (ntohs(fin->fin_data[0]) >> 8)
847                         {
848                         case ICMP_ECHO :
849                         case ICMP_TSTAMP :
850                         case ICMP_IREQ :
851                         case ICMP_MASKREQ :
852                                 break;
853                         default :
854                                 return 0;
855                         }
856
857                 sz = sizeof(ip_t) * 2;
858                 sz += 8;                /* 64 bits of data */
859                 hlen = sz;
860         }
861
862         sz += offsetof(struct icmp, icmp_ip);
863         if ((mb = (mblk_t *)allocb((size_t)sz + 16, BPRI_HI)) == NULL)
864                 return -1;
865         MTYPE(mb) = M_DATA;
866         mb->b_rptr += 16;
867         mb->b_wptr = mb->b_rptr + sz;
868         bzero((char *)mb->b_rptr, (size_t)sz);
869         icmp = (struct icmp *)(mb->b_rptr + sizeof(*ip));
870         icmp->icmp_type = type;
871         icmp->icmp_code = code;
872         icmp->icmp_cksum = 0;
873 #ifdef  icmp_nextmtu
874         if (type == ICMP_UNREACH && (il = qif->qf_ill) &&
875             fin->fin_icode == ICMP_UNREACH_NEEDFRAG)
876                 icmp->icmp_nextmtu = htons(il->ill_max_frag);
877 #endif
878
879 #ifdef  USE_INET6
880         if (oip->ip_v == 6) {
881                 struct in6_addr dst6;
882                 int csz;
883
884                 if (dst == 0) {
885                         if (fr_ifpaddr(6, ((qif_t *)fin->fin_qif)->qf_ill,
886                                        (struct in_addr *)&dst6) == -1)
887                                 return -1;
888                 } else
889                         dst6 = oip6->ip6_dst;
890
891                 csz = sz;
892                 sz -= sizeof(ip6_t);
893                 ip6 = (ip6_t *)mb->b_rptr;
894                 ip6->ip6_flow = 0;
895                 ip6->ip6_vfc = 0x60;
896                 ip6->ip6_hlim = 127;
897                 ip6->ip6_plen = htons(sz);
898                 ip6->ip6_nxt = IPPROTO_ICMPV6;
899                 ip6->ip6_src = dst6;
900                 ip6->ip6_dst = oip6->ip6_src;
901                 sz -= offsetof(struct icmp, icmp_ip);
902                 bcopy((char *)m->b_rptr, (char *)&icmp->icmp_ip, sz);
903                 icmp->icmp_cksum = csz - sizeof(ip6_t);
904         } else
905 #endif
906         {
907                 ip = (ip_t *)mb->b_rptr;
908                 ip->ip_v = IPVERSION;
909                 ip->ip_hl = (sizeof(*ip) >> 2);
910                 ip->ip_p = IPPROTO_ICMP;
911                 ip->ip_id = oip->ip_id;
912                 ip->ip_sum = 0;
913                 ip->ip_ttl = (u_char)(*ip_ttl_ptr);
914                 ip->ip_tos = oip->ip_tos;
915                 ip->ip_len = (u_short)htons(sz);
916                 if (dst == 0) {
917                         if (fr_ifpaddr(4, ((qif_t *)fin->fin_qif)->qf_ill,
918                                        &dst4) == -1)
919                                 return -1;
920                 } else
921                         dst4 = oip->ip_dst;
922                 ip->ip_src = dst4;
923                 ip->ip_dst = oip->ip_src;
924                 bcopy((char *)oip, (char *)&icmp->icmp_ip, sizeof(*oip));
925                 bcopy((char *)oip + (oip->ip_hl << 2),
926                       (char *)&icmp->icmp_ip + sizeof(*oip), 8);
927                 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
928                                              sizeof(*icmp) + 8);
929         }
930
931         /*
932          * Need to exit out of these so we don't recursively call rw_enter
933          * from fr_qout.
934          */
935         return send_ip(fin, mb);
936 }