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