kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_fil.c
1 /*
2  * Copyright (C) 1993-2001 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * @(#)ip_fil.c     2.41 6/5/96 (C) 1993-2000 Darren Reed
7  * @(#)$Id: ip_fil.c,v 2.42.2.60 2002/08/28 12:40:39 darrenr Exp $
8  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_fil.c,v 1.25.2.6 2003/03/01 03:55:54 darrenr Exp $
9  * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_fil.c,v 1.5 2003/08/07 21:16:48 dillon Exp $
10  */
11 #ifndef SOLARIS
12 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
13 #endif
14
15 #if defined(KERNEL) && !defined(_KERNEL)
16 # define        _KERNEL
17 #endif
18 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
19     (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
20 #include "opt_inet6.h"
21 #endif
22 #include <sys/param.h>
23 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
24     defined(_KERNEL)  && !defined(_LKM)
25 # include "opt_ipfilter_log.h"
26 #endif
27 #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
28 # if !defined(_KERNEL) || defined(IPFILTER_LKM)
29 #  include <osreldate.h>
30 # endif
31 #endif
32 #if defined(__sgi) && (IRIX > 602)
33 # define _KMEMUSER
34 # include <sys/ptimers.h>
35 #endif
36 #ifndef _KERNEL
37 # include <stdio.h>
38 # include <string.h>
39 # include <stdlib.h>
40 # include <ctype.h>
41 # include <fcntl.h>
42 #endif
43 #include <sys/errno.h>
44 #include <sys/types.h>
45 #include <sys/file.h>
46 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
47 # include <sys/fcntl.h>
48 # include <sys/filio.h>
49 #else
50 # include <sys/ioctl.h>
51 #endif
52 #include <sys/time.h>
53 #ifdef  _KERNEL
54 # include <sys/systm.h>
55 #endif
56 #if !SOLARIS
57 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
58 #  include <sys/dirent.h>
59 # else
60 #  include <sys/dir.h>
61 # endif
62 # include <sys/mbuf.h>
63 #else
64 # include <sys/filio.h>
65 #endif
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68
69 #include <net/if.h>
70 #ifdef sun
71 # include <net/af.h>
72 #endif
73 #if __FreeBSD_version >= 300000
74 # include <net/if_var.h>
75 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
76 #  include "opt_ipfilter.h"
77 # endif
78 #endif
79 #ifdef __sgi
80 #include <sys/debug.h>
81 # ifdef IFF_DRVRLOCK /* IRIX6 */
82 #include <sys/hashing.h>
83 # endif
84 #endif
85 #include <net/route.h>
86 #include <netinet/in.h>
87 #if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /* IRIX < 6 */
88 # include <netinet/in_var.h>
89 #endif
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_var.h>
93 #include <netinet/tcp.h>
94 #include <netinet/udp.h>
95 #include <netinet/tcpip.h>
96 #include <netinet/ip_icmp.h>
97 #ifndef _KERNEL
98 # include <unistd.h>
99 # include <syslog.h>
100 #endif
101 #include "ip_compat.h"
102 #ifdef USE_INET6
103 # include <netinet/icmp6.h>
104 # if !SOLARIS
105 #  include <netinet6/ip6protosw.h>
106 #  include <netinet6/nd6.h>
107 # endif
108 #endif
109 #include "ip_fil.h"
110 #include "ip_nat.h"
111 #include "ip_frag.h"
112 #include "ip_state.h"
113 #include "ip_proxy.h"
114 #include "ip_auth.h"
115 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
116 # include <sys/malloc.h>
117 #endif
118 #ifndef MIN
119 # define        MIN(a,b)        (((a)<(b))?(a):(b))
120 #endif
121 #if !SOLARIS && defined(_KERNEL) && !defined(__sgi)
122 # include <sys/kernel.h>
123 extern  int     ip_optcopy __P((struct ip *, struct ip *));
124 #endif
125 #if defined(OpenBSD) && (OpenBSD >= 200211) && defined(_KERNEL)
126 extern  int     ip6_getpmtu(struct route_in6 *, struct route_in6 *,
127                             struct ifnet *, struct in6_addr *, u_long *);
128 #endif
129
130 #include <machine/in_cksum.h>
131
132 static const char sccsid[] = "@(#)ip_fil.c     2.41 6/5/96 (C) 1993-2000 Darren Reed";
133
134 extern  struct  protosw inetsw[];
135
136 #ifndef _KERNEL
137 # include "ipt.h"
138 static  struct  ifnet **ifneta = NULL;
139 static  int     nifs = 0;
140 #else
141 # if    (BSD < 199306) || defined(__sgi)
142 extern  int     tcp_ttl;
143 # endif
144 #endif
145
146 #ifdef  ICMP_UNREACH_FILTER_PROHIB
147 int     ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
148 #else
149 int     ipl_unreach = ICMP_UNREACH_FILTER;
150 #endif
151 u_long  ipl_frouteok[2] = {0, 0};
152
153 static  int     frzerostats __P((caddr_t));
154 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
155 static  int     frrequest __P((int, u_long, caddr_t, int));
156 #else
157 static  int     frrequest __P((int, int, caddr_t, int));
158 #endif
159 #ifdef  _KERNEL
160 static  int     (*fr_savep) __P((ip_t *, int, void *, int, struct mbuf **));
161 static  int     send_ip __P((ip_t *, fr_info_t *, struct mbuf **));
162 # ifdef USE_INET6
163 static  int     ipfr_fastroute6 __P((struct mbuf *, struct mbuf **,
164                                      fr_info_t *, frdest_t *));
165 # endif
166 # ifdef __sgi
167 extern  int             tcp_mtudisc;
168 extern  kmutex_t        ipf_rw;
169 extern  KRWLOCK_T       ipf_mutex;
170 # endif
171 #else
172 void    init_ifp __P((void));
173 # if defined(__sgi) && (IRIX < 605)
174 static int      no_output __P((struct ifnet *, struct mbuf *,
175                                struct sockaddr *));
176 static int      write_output __P((struct ifnet *, struct mbuf *,
177                                   struct sockaddr *));
178 # else
179 static int      no_output __P((struct ifnet *, struct mbuf *,
180                                struct sockaddr *, struct rtentry *));
181 static int      write_output __P((struct ifnet *, struct mbuf *,
182                                   struct sockaddr *, struct rtentry *));
183 # endif
184 #endif
185 int     fr_running = 0;
186
187 #if (__FreeBSD_version >= 300000) && defined(_KERNEL)
188 struct callout_handle ipfr_slowtimer_ch;
189 #endif
190 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
191 # include <sys/callout.h>
192 struct callout ipfr_slowtimer_ch;
193 #endif
194 #if defined(__OpenBSD__)
195 # include <sys/timeout.h>
196 struct timeout ipfr_slowtimer_ch;
197 #endif
198 #if defined(__sgi) && defined(_KERNEL)
199 toid_t ipfr_slowtimer_ch;
200 #endif
201
202 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
203     defined(_KERNEL)
204 # include <sys/conf.h>
205 const struct cdevsw ipl_cdevsw = {
206         iplopen, iplclose, iplread, nowrite, iplioctl,
207         nostop, notty, nopoll, nommap,
208 };
209 #endif
210
211 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
212 # include <sys/device.h>
213 # include <sys/conf.h>
214
215 struct cfdriver iplcd = {
216         NULL, "ipl", NULL, NULL, DV_DULL, 0
217 };
218
219 struct devsw iplsw = {
220         &iplcd,
221         iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
222         nostrat, nodump, nopsize, 0,
223         nostop
224 };
225 #endif /* _BSDI_VERSION >= 199510  && _KERNEL */
226
227 #if defined(__NetBSD__) || defined(__OpenBSD__)  || (_BSDI_VERSION >= 199701)
228 # include <sys/conf.h>
229 # if defined(NETBSD_PF)
230 #  include <net/pfil.h>
231 /*
232  * We provide the fr_checkp name just to minimize changes later.
233  */
234 int (*fr_checkp) __P((ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
235 # endif /* NETBSD_PF */
236 #endif /* __NetBSD__ */
237
238
239 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
240     defined(_KERNEL)
241 # include <net/pfil.h>
242
243 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
244
245 static int fr_check_wrapper(arg, mp, ifp, dir)
246 void *arg;
247 struct mbuf **mp;
248 struct ifnet *ifp;
249 int dir;
250 {
251         struct ip *ip = mtod(*mp, struct ip *);
252         int rv, hlen = ip->ip_hl << 2;
253
254 #if defined(M_CSUM_TCPv4)
255         /*
256          * If the packet is out-bound, we can't delay checksums
257          * here.  For in-bound, the checksum has already been
258          * validated.
259          */
260         if (dir == PFIL_OUT) {
261                 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
262                         in_delayed_cksum(*mp);
263                         (*mp)->m_pkthdr.csum_flags &=
264                             ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
265                 }
266         }
267 #endif /* M_CSUM_TCPv4 */
268
269         /*
270          * We get the packet with all fields in network byte
271          * order.  We expect ip_len and ip_off to be in host
272          * order.  We frob them, call the filter, then frob
273          * them back.
274          *
275          * Note, we don't need to update the checksum, because
276          * it has already been verified.
277          */
278         NTOHS(ip->ip_len);
279         NTOHS(ip->ip_off);
280
281         rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
282
283         if (rv == 0 && *mp != NULL) {
284                 ip = mtod(*mp, struct ip *);
285                 HTONS(ip->ip_len);
286                 HTONS(ip->ip_off);
287         }
288
289         return (rv);
290 }
291
292 # ifdef USE_INET6
293 #  include <netinet/ip6.h>
294
295 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
296
297 static int fr_check_wrapper6(arg, mp, ifp, dir)
298 void *arg;
299 struct mbuf **mp;
300 struct ifnet *ifp;
301 int dir;
302 {
303         
304         return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
305             ifp, (dir == PFIL_OUT), mp));
306 }
307 # endif
308 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
309 #ifdef  _KERNEL
310 # if    defined(IPFILTER_LKM) && !defined(__sgi)
311 int iplidentify(s)
312 char *s;
313 {
314         if (strcmp(s, "ipl") == 0)
315                 return 1;
316         return 0;
317 }
318 # endif /* IPFILTER_LKM */
319
320
321 /*
322  * Try to detect the case when compiling for NetBSD with pseudo-device
323  */
324 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
325 void
326 ipfilterattach(count)
327 int count;
328 {
329
330         /*
331          * Do nothing here, really.  The filter will be enabled
332          * by the SIOCFRENB ioctl.
333          */
334 }
335 # endif
336
337
338 # if defined(__NetBSD__) || defined(__OpenBSD__)
339 int ipl_enable()
340 # else
341 int iplattach()
342 # endif
343 {
344         char *defpass;
345         int s;
346 # if defined(__sgi) || (defined(NETBSD_PF) && (__NetBSD_Version__ >= 104200000))
347         int error = 0;
348 # endif
349 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
350         struct pfil_head *ph_inet;
351 # ifdef USE_INET6
352         struct pfil_head *ph_inet6;
353 # endif
354 #endif
355
356         SPL_NET(s);
357         if (fr_running || (fr_checkp == fr_check)) {
358                 printf("IP Filter: already initialized\n");
359                 SPL_X(s);
360                 return EBUSY;
361         }
362
363 # ifdef IPFILTER_LOG
364         ipflog_init();
365 # endif
366         if (nat_init() == -1) {
367                 SPL_X(s);
368                 return EIO;
369         }
370         if (fr_stateinit() == -1) {
371                 SPL_X(s);
372                 return EIO;
373         }
374         if (appr_init() == -1) {
375                 SPL_X(s);
376                 return EIO;
377         }
378
379 # ifdef NETBSD_PF
380 #  if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011)
381 #   if __NetBSD_Version__ >= 105110000
382         ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
383 #    ifdef USE_INET6
384         ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
385 #    endif
386         if (ph_inet == NULL
387 #    ifdef USE_INET6
388             && ph_inet6 == NULL
389 #    endif
390            )
391                 return ENODEV;
392
393         if (ph_inet != NULL)
394                 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
395                                       PFIL_IN|PFIL_OUT, ph_inet);
396         else
397                 error = 0;
398 #  else
399         error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
400                               &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
401 #  endif
402         if (error) {
403 #   ifdef USE_INET6
404                 goto pfil_error;
405 #   else
406                 SPL_X(s);
407                 appr_unload();
408                 ip_natunload();
409                 fr_stateunload();
410                 return error;
411 #   endif
412         }
413 #  else
414         pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
415 #  endif
416 #  ifdef USE_INET6
417 #   if __NetBSD_Version__ >= 105110000
418         if (ph_inet6 != NULL)
419                 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
420                                       PFIL_IN|PFIL_OUT, ph_inet6);
421         else
422                 error = 0;
423         if (error) {
424                 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
425                                  PFIL_IN|PFIL_OUT, ph_inet6);
426 #   else
427         error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
428                               &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
429         if (error) {
430                 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
431                                  &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
432 #   endif
433 pfil_error:
434                 SPL_X(s);
435                 appr_unload();
436                 ip_natunload();
437                 fr_stateunload();
438                 return error;
439         }
440 #  endif
441 # endif
442
443 # ifdef __sgi
444         error = ipfilter_sgi_attach();
445         if (error) {
446                 SPL_X(s);
447                 appr_unload();
448                 ip_natunload();
449                 fr_stateunload();
450                 return error;
451         }
452 # endif
453
454         bzero((char *)frcache, sizeof(frcache));
455         fr_savep = fr_checkp;
456         fr_checkp = fr_check;
457         fr_running = 1;
458
459         SPL_X(s);
460         if (fr_pass & FR_PASS)
461                 defpass = "pass";
462         else if (fr_pass & FR_BLOCK)
463                 defpass = "block";
464         else
465                 defpass = "no-match -> block";
466
467         printf("%s initialized.  Default = %s all, Logging = %s\n",
468                 ipfilter_version, defpass,
469 # ifdef IPFILTER_LOG
470                 "enabled");
471 # else
472                 "disabled");
473 # endif
474 #ifdef  _KERNEL
475 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
476         callout_init(&ipfr_slowtimer_ch);
477         callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
478 # else
479 #  if defined(__OpenBSD__)
480         timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
481         timeout_add(&ipfr_slowtimer_ch, hz/2);
482 #  else
483 #   if (__FreeBSD_version >= 300000) || defined(__sgi)
484         ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
485 #   else
486         timeout(ipfr_slowtimer, NULL, hz/2);
487 #   endif
488 #  endif
489 # endif
490 #endif
491         return 0;
492 }
493
494
495 /*
496  * Disable the filter by removing the hooks from the IP input/output
497  * stream.
498  */
499 # if defined(__NetBSD__)
500 int ipl_disable()
501 # else
502 int ipldetach()
503 # endif
504 {
505         int s, i;
506 #if defined(NETBSD_PF) && \
507     ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
508         int error = 0;
509 # if __NetBSD_Version__ >= 105150000
510         struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
511 #  ifdef USE_INET6
512         struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
513 #  endif
514 # endif
515 #endif
516
517 #ifdef  _KERNEL
518 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
519         callout_stop(&ipfr_slowtimer_ch);
520 # else
521 #  if (__FreeBSD_version >= 300000)
522         untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
523 #  else
524 #  ifdef __sgi
525         untimeout(ipfr_slowtimer_ch);
526 #   else
527 #    if defined(__OpenBSD__)
528         timeout_del(&ipfr_slowtimer_ch);
529 #    else
530         untimeout(ipfr_slowtimer, NULL);
531 #    endif /* OpenBSD */
532 #   endif /* __sgi */
533 #  endif /* FreeBSD */
534 # endif /* NetBSD */
535 #endif
536         SPL_NET(s);
537         if (!fr_running)
538         {
539                 printf("IP Filter: not initialized\n");
540                 SPL_X(s);
541                 return 0;
542         }
543
544         printf("%s unloaded\n", ipfilter_version);
545
546         fr_checkp = fr_savep;
547         i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
548         i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
549         fr_running = 0;
550
551 # ifdef NETBSD_PF
552 #  if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
553 #   if __NetBSD_Version__ >= 105110000
554         if (ph_inet != NULL)
555                 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
556                                          PFIL_IN|PFIL_OUT, ph_inet);
557         else
558                 error = 0;
559 #   else
560         error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
561                                  &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
562 #   endif
563         if (error) {
564                 SPL_X(s);
565                 return error;
566         }
567 #  else
568         pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
569 #  endif
570 #  ifdef USE_INET6
571 #   if __NetBSD_Version__ >= 105110000
572         if (ph_inet6 != NULL)
573                 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
574                                          PFIL_IN|PFIL_OUT, ph_inet6);
575         else
576                 error = 0;
577 #   else
578         error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
579                                  &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
580 #   endif
581         if (error) {
582                 SPL_X(s);
583                 return error;
584         }
585 #  endif
586 # endif
587
588 # ifdef __sgi
589         ipfilter_sgi_detach();
590 # endif
591
592         appr_unload();
593         ipfr_unload();
594         ip_natunload();
595         fr_stateunload();
596         fr_authunload();
597
598         SPL_X(s);
599         return 0;
600 }
601 #endif /* _KERNEL */
602
603
604 static  int     frzerostats(data)
605 caddr_t data;
606 {
607         friostat_t fio;
608         int error;
609
610         fr_getstat(&fio);
611         error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
612         if (error)
613                 return EFAULT;
614
615         bzero((char *)frstats, sizeof(*frstats) * 2);
616
617         return 0;
618 }
619
620
621 /*
622  * Filter ioctl interface.
623  */
624 #ifdef __sgi
625 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
626 # ifdef _KERNEL
627         , cred_t *cp, int *rp
628 # endif
629 )
630 #else
631 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
632 #ifdef __FreeBSD__
633 , td)
634 struct thread *td;
635 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
636        (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
637        defined(__OpenBSD__)))
638 , p)
639 struct proc *p;
640 # else
641 )
642 # endif
643 dev_t dev;
644 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
645         (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
646 u_long cmd;
647 # else
648 int cmd;
649 # endif
650 caddr_t data;
651 int mode;
652 #endif /* __sgi */
653 {
654 #if defined(_KERNEL) && !SOLARIS
655         int s;
656 #endif
657         int error = 0, unit = 0, tmp;
658
659 #if (BSD >= 199306) && defined(_KERNEL)
660         if ((securelevel >= 3) && (mode & FWRITE))
661                 return EPERM;
662 #endif
663 #ifdef  _KERNEL
664         unit = GET_MINOR(dev);
665         if ((IPL_LOGMAX < unit) || (unit < 0))
666                 return ENXIO;
667 #else
668         unit = dev;
669 #endif
670
671         if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
672                 return ENODEV;
673
674         SPL_NET(s);
675
676         if (unit == IPL_LOGNAT) {
677                 if (fr_running)
678                         error = nat_ioctl(data, cmd, mode);
679                 else
680                         error = EIO;
681                 SPL_X(s);
682                 return error;
683         }
684         if (unit == IPL_LOGSTATE) {
685                 if (fr_running)
686                         error = fr_state_ioctl(data, cmd, mode);
687                 else
688                         error = EIO;
689                 SPL_X(s);
690                 return error;
691         }
692         if (unit == IPL_LOGAUTH) {
693                 if (!fr_running)
694                         error = EIO;
695                 else
696                         if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
697                                 if (!(mode & FWRITE))  {
698                                         error = EPERM;
699                                 } else {
700                                         error = frrequest(unit, cmd, data,
701                                                           fr_active);
702                                 }
703                         } else {
704                                 error = fr_auth_ioctl(data, mode, cmd);
705                         }
706                 SPL_X(s);
707                 return error;
708         }
709
710         switch (cmd) {
711         case FIONREAD :
712 #ifdef IPFILTER_LOG
713                 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
714                                sizeof(iplused[IPL_LOGIPF]));
715 #endif
716                 break;
717 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
718         case SIOCFRENB :
719         {
720                 u_int   enable;
721
722                 if (!(mode & FWRITE))
723                         error = EPERM;
724                 else {
725                         error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
726                         if (error)
727                                 break;
728                         if (enable)
729 # if defined(__NetBSD__) || defined(__OpenBSD__)
730                                 error = ipl_enable();
731 # else
732                                 error = iplattach();
733 # endif
734                         else
735 # if defined(__NetBSD__)
736                                 error = ipl_disable();
737 # else
738                                 error = ipldetach();
739 # endif
740                 }
741                 break;
742         }
743 #endif
744         case SIOCSETFF :
745                 if (!(mode & FWRITE))
746                         error = EPERM;
747                 else
748                         error = IRCOPY(data, (caddr_t)&fr_flags,
749                                        sizeof(fr_flags));
750                 break;
751         case SIOCGETFF :
752                 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
753                 break;
754         case SIOCINAFR :
755         case SIOCRMAFR :
756         case SIOCADAFR :
757         case SIOCZRLST :
758                 if (!(mode & FWRITE))
759                         error = EPERM;
760                 else
761                         error = frrequest(unit, cmd, data, fr_active);
762                 break;
763         case SIOCINIFR :
764         case SIOCRMIFR :
765         case SIOCADIFR :
766                 if (!(mode & FWRITE))
767                         error = EPERM;
768                 else
769                         error = frrequest(unit, cmd, data, 1 - fr_active);
770                 break;
771         case SIOCSWAPA :
772                 if (!(mode & FWRITE))
773                         error = EPERM;
774                 else {
775                         bzero((char *)frcache, sizeof(frcache[0]) * 2);
776                         *(u_int *)data = fr_active;
777                         fr_active = 1 - fr_active;
778                 }
779                 break;
780         case SIOCGETFS :
781         {
782                 friostat_t      fio;
783
784                 fr_getstat(&fio);
785                 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
786                 if (error)
787                         error = EFAULT;
788                 break;
789         }
790         case    SIOCFRZST :
791                 if (!(mode & FWRITE))
792                         error = EPERM;
793                 else
794                         error = frzerostats(data);
795                 break;
796         case    SIOCIPFFL :
797                 if (!(mode & FWRITE))
798                         error = EPERM;
799                 else {
800                         error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
801                         if (!error) {
802                                 tmp = frflush(unit, 4, tmp);
803                                 error = IWCOPY((caddr_t)&tmp, data,
804                                                sizeof(tmp));
805                         }
806                 }
807                 break;
808 #ifdef  USE_INET6
809         case    SIOCIPFL6 :
810                 if (!(mode & FWRITE))
811                         error = EPERM;
812                 else {
813                         error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
814                         if (!error) {
815                                 tmp = frflush(unit, 6, tmp);
816                                 error = IWCOPY((caddr_t)&tmp, data,
817                                                sizeof(tmp));
818                         }
819                 }
820                 break;
821 #endif
822         case SIOCSTLCK :
823                 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
824                 if (!error) {
825                         fr_state_lock = tmp;
826                         fr_nat_lock = tmp;
827                         fr_frag_lock = tmp;
828                         fr_auth_lock = tmp;
829                 } else
830                         error = EFAULT;
831                 break;
832 #ifdef  IPFILTER_LOG
833         case    SIOCIPFFB :
834                 if (!(mode & FWRITE))
835                         error = EPERM;
836                 else
837                         *(int *)data = ipflog_clear(unit);
838                 break;
839 #endif /* IPFILTER_LOG */
840         case SIOCGFRST :
841                 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
842                                   sizeof(ipfrstat_t));
843                 if (error)
844                         error = EFAULT;
845                 break;
846         case SIOCFRSYN :
847                 if (!(mode & FWRITE))
848                         error = EPERM;
849                 else {
850 #if defined(_KERNEL) && defined(__sgi)
851                         ipfsync();
852 #endif
853                         frsync();
854                 }
855                 break;
856         default :
857                 error = EINVAL;
858                 break;
859         }
860         SPL_X(s);
861         return error;
862 }
863
864
865 void fr_forgetifp(ifp)
866 void *ifp;
867 {
868         frentry_t *f;
869
870         WRITE_ENTER(&ipf_mutex);
871         for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
872                 if (f->fr_ifa == ifp)
873                         f->fr_ifa = (void *)-1;
874         for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
875                 if (f->fr_ifa == ifp)
876                         f->fr_ifa = (void *)-1;
877         for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
878                 if (f->fr_ifa == ifp)
879                         f->fr_ifa = (void *)-1;
880         for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
881                 if (f->fr_ifa == ifp)
882                         f->fr_ifa = (void *)-1;
883 #ifdef  USE_INET6
884         for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
885                 if (f->fr_ifa == ifp)
886                         f->fr_ifa = (void *)-1;
887         for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
888                 if (f->fr_ifa == ifp)
889                         f->fr_ifa = (void *)-1;
890         for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
891                 if (f->fr_ifa == ifp)
892                         f->fr_ifa = (void *)-1;
893         for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
894                 if (f->fr_ifa == ifp)
895                         f->fr_ifa = (void *)-1;
896 #endif
897         RWLOCK_EXIT(&ipf_mutex);
898         ip_natsync(ifp);
899 }
900
901
902 static int frrequest(unit, req, data, set)
903 int unit;
904 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
905 u_long req;
906 #else
907 int req;
908 #endif
909 int set;
910 caddr_t data;
911 {
912         frentry_t *fp, *f, **fprev;
913         frentry_t **ftail;
914         frgroup_t *fg = NULL;
915         int error = 0, in, i;
916         u_int   *p, *pp;
917         frentry_t frd;
918         frdest_t *fdp;
919         u_int group;
920
921         fp = &frd;
922         error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
923         if (error)
924                 return EFAULT;
925         fp->fr_ref = 0;
926 #if (BSD >= 199306) && defined(_KERNEL)
927         if ((securelevel > 0) && (fp->fr_func != NULL))
928                 return EPERM;
929 #endif
930
931         /*
932          * Check that the group number does exist and that if a head group
933          * has been specified, doesn't exist.
934          */
935         if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
936              (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
937             fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
938                 return EEXIST;
939         if ((req != SIOCZRLST) && fp->fr_group &&
940             !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
941                 return ESRCH;
942
943         in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
944
945         if (unit == IPL_LOGAUTH)
946                 ftail = fprev = &ipauth;
947         else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
948                 ftail = fprev = &ipacct[in][set];
949         else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
950                 ftail = fprev = &ipfilter[in][set];
951 #ifdef  USE_INET6
952         else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
953                 ftail = fprev = &ipacct6[in][set];
954         else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
955                 ftail = fprev = &ipfilter6[in][set];
956 #endif
957         else
958                 return ESRCH;
959
960         if ((group = fp->fr_group)) {
961                 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
962                         return ESRCH;
963                 ftail = fprev = fg->fg_start;
964         }
965
966         bzero((char *)frcache, sizeof(frcache[0]) * 2);
967
968         for (i = 0; i < 4; i++) {
969                 if ((fp->fr_ifnames[i][1] == '\0') &&
970                     ((fp->fr_ifnames[i][0] == '-') ||
971                      (fp->fr_ifnames[i][0] == '*'))) {
972                         fp->fr_ifas[i] = NULL;
973                 } else if (*fp->fr_ifnames[i]) {
974                         fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
975                         if (!fp->fr_ifas[i])
976                                 fp->fr_ifas[i] = (void *)-1;
977                 }
978         }
979
980         fdp = &fp->fr_dif;
981         fp->fr_flags &= ~FR_DUP;
982         if (*fdp->fd_ifname) {
983                 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
984                 if (!fdp->fd_ifp)
985                         fdp->fd_ifp = (struct ifnet *)-1;
986                 else
987                         fp->fr_flags |= FR_DUP;
988         }
989
990         fdp = &fp->fr_tif;
991         if (*fdp->fd_ifname) {
992                 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
993                 if (!fdp->fd_ifp)
994                         fdp->fd_ifp = (struct ifnet *)-1;
995         }
996
997         /*
998          * Look for a matching filter rule, but don't include the next or
999          * interface pointer in the comparison (fr_next, fr_ifa).
1000          */
1001         for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1002              p < pp; p++)
1003                 fp->fr_cksum += *p;
1004
1005         for (; (f = *ftail); ftail = &f->fr_next)
1006                 if ((fp->fr_cksum == f->fr_cksum) &&
1007                     !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1008                         break;
1009
1010         /*
1011          * If zero'ing statistics, copy current to caller and zero.
1012          */
1013         if (req == SIOCZRLST) {
1014                 if (!f)
1015                         return ESRCH;
1016                 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1017                 if (error)
1018                         return EFAULT;
1019                 f->fr_hits = 0;
1020                 f->fr_bytes = 0;
1021                 return 0;
1022         }
1023
1024         if (!f) {
1025                 if (req != SIOCINAFR && req != SIOCINIFR)
1026                         while ((f = *ftail))
1027                                 ftail = &f->fr_next;
1028                 else {
1029                         if (fp->fr_hits) {
1030                                 ftail = fprev;
1031                                 while (--fp->fr_hits && (f = *ftail))
1032                                         ftail = &f->fr_next;
1033                         }
1034                         f = NULL;
1035                 }
1036         }
1037
1038         if (req == SIOCRMAFR || req == SIOCRMIFR) {
1039                 if (!f)
1040                         error = ESRCH;
1041                 else {
1042                         /*
1043                          * Only return EBUSY if there is a group list, else
1044                          * it's probably just state information referencing
1045                          * the rule.
1046                          */
1047                         if ((f->fr_ref > 1) && f->fr_grp)
1048                                 return EBUSY;
1049                         if (fg && fg->fg_head)
1050                                 fg->fg_head->fr_ref--;
1051                         if (unit == IPL_LOGAUTH) {
1052                                 return fr_preauthcmd(req, f, ftail);
1053                         }
1054                         if (f->fr_grhead)
1055                                 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1056                                             unit, set);
1057                         fixskip(fprev, f, -1);
1058                         *ftail = f->fr_next;
1059                         f->fr_next = NULL;
1060                         f->fr_ref--;
1061                         if (f->fr_ref == 0)
1062                                 KFREE(f);
1063                 }
1064         } else {
1065                 if (f)
1066                         error = EEXIST;
1067                 else {
1068                         if (unit == IPL_LOGAUTH) {
1069                                 return fr_preauthcmd(req, fp, ftail);
1070                         }
1071                         KMALLOC(f, frentry_t *);
1072                         if (f != NULL) {
1073                                 if (fg && fg->fg_head)
1074                                         fg->fg_head->fr_ref++;
1075                                 bcopy((char *)fp, (char *)f, sizeof(*f));
1076                                 f->fr_ref = 1;
1077                                 f->fr_hits = 0;
1078                                 f->fr_next = *ftail;
1079                                 *ftail = f;
1080                                 if (req == SIOCINIFR || req == SIOCINAFR)
1081                                         fixskip(fprev, f, 1);
1082                                 f->fr_grp = NULL;
1083                                 if ((group = f->fr_grhead))
1084                                         fg = fr_addgroup(group, f, unit, set);
1085                         } else
1086                                 error = ENOMEM;
1087                 }
1088         }
1089         return (error);
1090 }
1091
1092
1093 #ifdef  _KERNEL
1094 /*
1095  * routines below for saving IP headers to buffer
1096  */
1097 # ifdef __sgi
1098 #  ifdef _KERNEL
1099 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1100 #  else
1101 int IPL_EXTERN(open)(dev_t dev, int flags)
1102 #  endif
1103 # else
1104 int IPL_EXTERN(open)(dev, flags
1105 #ifdef __FreeBSD__
1106 , devtype, td)
1107 int devtype;
1108 struct thread *td;
1109 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1110      (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1111 , devtype, p)
1112 int devtype;
1113 struct proc *p;
1114 #  else
1115 )
1116 #  endif
1117 dev_t dev;
1118 int flags;
1119 # endif /* __sgi */
1120 {
1121 # if defined(__sgi) && defined(_KERNEL)
1122         u_int min = geteminor(*pdev);
1123 # else
1124         u_int min = GET_MINOR(dev);
1125 # endif
1126
1127         if (IPL_LOGMAX < min)
1128                 min = ENXIO;
1129         else
1130                 min = 0;
1131         return min;
1132 }
1133
1134
1135 # ifdef __sgi
1136 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1137 #else
1138 int IPL_EXTERN(close)(dev, flags
1139 #ifdef __FreeBSD__
1140 , devtype, td)
1141 int devtype;
1142 struct thread *td;
1143 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1144      (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1145 , devtype, p)
1146 int devtype;
1147 struct proc *p;
1148 #  else
1149 )
1150 #  endif
1151 dev_t dev;
1152 int flags;
1153 # endif /* __sgi */
1154 {
1155         u_int   min = GET_MINOR(dev);
1156
1157         if (IPL_LOGMAX < min)
1158                 min = ENXIO;
1159         else
1160                 min = 0;
1161         return min;
1162 }
1163
1164 /*
1165  * iplread/ipllog
1166  * both of these must operate with at least splnet() lest they be
1167  * called during packet processing and cause an inconsistancy to appear in
1168  * the filter lists.
1169  */
1170 # ifdef __sgi
1171 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1172 # else
1173 #  if BSD >= 199306
1174 int IPL_EXTERN(read)(dev, uio, ioflag)
1175 int ioflag;
1176 #  else
1177 int IPL_EXTERN(read)(dev, uio)
1178 #  endif
1179 dev_t dev;
1180 struct uio *uio;
1181 # endif /* __sgi */
1182 {
1183 # ifdef IPFILTER_LOG
1184         return ipflog_read(GET_MINOR(dev), uio);
1185 # else
1186         return ENXIO;
1187 # endif
1188 }
1189
1190
1191 /*
1192  * send_reset - this could conceivably be a call to tcp_respond(), but that
1193  * requires a large amount of setting up and isn't any more efficient.
1194  */
1195 int send_reset(oip, fin)
1196 struct ip *oip;
1197 fr_info_t *fin;
1198 {
1199         struct tcphdr *tcp, *tcp2;
1200         int tlen = 0, hlen;
1201         struct mbuf *m;
1202 #ifdef  USE_INET6
1203         ip6_t *ip6, *oip6 = (ip6_t *)oip;
1204 #endif
1205         ip_t *ip;
1206
1207         tcp = (struct tcphdr *)fin->fin_dp;
1208         if (tcp->th_flags & TH_RST)
1209                 return -1;              /* feedback loop */
1210 # if    (BSD < 199306) || defined(__sgi)
1211         m = m_get(M_DONTWAIT, MT_HEADER);
1212 # else
1213         m = m_gethdr(M_DONTWAIT, MT_HEADER);
1214 # endif
1215         if (m == NULL)
1216                 return ENOBUFS;
1217         if (m == NULL)
1218                 return -1;
1219
1220         tlen = fin->fin_dlen - (tcp->th_off << 2) +
1221                         ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1222                         ((tcp->th_flags & TH_FIN) ? 1 : 0);
1223
1224 #ifdef  USE_INET6
1225         hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1226 #else
1227         hlen = sizeof(ip_t);
1228 #endif
1229         m->m_len = sizeof(*tcp2) + hlen;
1230 # if    BSD >= 199306
1231         m->m_data += max_linkhdr;
1232         m->m_pkthdr.len = m->m_len;
1233         m->m_pkthdr.rcvif = (struct ifnet *)0;
1234 # endif
1235         ip = mtod(m, struct ip *);
1236 # ifdef USE_INET6
1237         ip6 = (ip6_t *)ip;
1238 # endif
1239         bzero((char *)ip, sizeof(*tcp2) + hlen);
1240         tcp2 = (struct tcphdr *)((char *)ip + hlen);
1241
1242         tcp2->th_sport = tcp->th_dport;
1243         tcp2->th_dport = tcp->th_sport;
1244         if (tcp->th_flags & TH_ACK) {
1245                 tcp2->th_seq = tcp->th_ack;
1246                 tcp2->th_flags = TH_RST;
1247         } else {
1248                 tcp2->th_ack = ntohl(tcp->th_seq);
1249                 tcp2->th_ack += tlen;
1250                 tcp2->th_ack = htonl(tcp2->th_ack);
1251                 tcp2->th_flags = TH_RST|TH_ACK;
1252         }
1253         tcp2->th_off = sizeof(*tcp2) >> 2;
1254 # ifdef USE_INET6
1255         if (fin->fin_v == 6) {
1256                 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1257                 ip6->ip6_nxt = IPPROTO_TCP;
1258                 ip6->ip6_src = oip6->ip6_dst;
1259                 ip6->ip6_dst = oip6->ip6_src;
1260                 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1261                                          sizeof(*ip6), sizeof(*tcp2));
1262                 return send_ip(oip, fin, &m);
1263         }
1264 # endif
1265         ip->ip_p = IPPROTO_TCP;
1266         ip->ip_len = htons(sizeof(struct tcphdr));
1267         ip->ip_src.s_addr = oip->ip_dst.s_addr;
1268         ip->ip_dst.s_addr = oip->ip_src.s_addr;
1269         tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1270         ip->ip_len = hlen + sizeof(*tcp2);
1271         return send_ip(oip, fin, &m);
1272 }
1273
1274
1275 /*
1276  * Send an IP(v4/v6) datagram out into the network
1277  */
1278 static int send_ip(oip, fin, mp)
1279 ip_t *oip;
1280 fr_info_t *fin;
1281 struct mbuf **mp;
1282 {
1283         struct mbuf *m = *mp;
1284         int error, hlen;
1285         fr_info_t frn;
1286         ip_t *ip;
1287
1288         bzero((char *)&frn, sizeof(frn));
1289         frn.fin_ifp = fin->fin_ifp;
1290         frn.fin_v = fin->fin_v;
1291         frn.fin_out = fin->fin_out;
1292         frn.fin_mp = fin->fin_mp;
1293
1294         ip = mtod(m, ip_t *);
1295         hlen = sizeof(*ip);
1296
1297         ip->ip_v = fin->fin_v;
1298         if (ip->ip_v == 4) {
1299                 ip->ip_hl = (sizeof(*oip) >> 2);
1300                 ip->ip_v = IPVERSION;
1301                 ip->ip_tos = oip->ip_tos;
1302                 ip->ip_id = oip->ip_id;
1303
1304 # if defined(__NetBSD__) || \
1305      (defined(__OpenBSD__) && (OpenBSD >= 200012))
1306                 if (ip_mtudisc != 0)
1307                         ip->ip_off = IP_DF;
1308 # else
1309 #  if defined(__sgi)
1310                 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1311                         ip->ip_off = IP_DF;
1312 #  endif
1313 # endif
1314
1315 # if (BSD < 199306) || defined(__sgi)
1316                 ip->ip_ttl = tcp_ttl;
1317 # else
1318                 ip->ip_ttl = ip_defttl;
1319 # endif
1320                 ip->ip_sum = 0;
1321                 frn.fin_dp = (char *)(ip + 1);
1322         }
1323 # ifdef USE_INET6
1324         else if (ip->ip_v == 6) {
1325                 ip6_t *ip6 = (ip6_t *)ip;
1326
1327                 hlen = sizeof(*ip6);
1328                 ip6->ip6_hlim = 127;
1329                 frn.fin_dp = (char *)(ip6 + 1);
1330         }
1331 # endif
1332 # ifdef IPSEC
1333         m->m_pkthdr.rcvif = NULL;
1334 # endif
1335
1336         fr_makefrip(hlen, ip, &frn);
1337
1338         error = ipfr_fastroute(m, mp, &frn, NULL);
1339         return error;
1340 }
1341
1342
1343 int send_icmp_err(oip, type, fin, dst)
1344 ip_t *oip;
1345 int type;
1346 fr_info_t *fin;
1347 int dst;
1348 {
1349         int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1350         u_short shlen, slen = 0, soff = 0;
1351         struct in_addr dst4;
1352         struct icmp *icmp;
1353         struct mbuf *m;
1354         void *ifp;
1355 #ifdef USE_INET6
1356         ip6_t *ip6, *oip6 = (ip6_t *)oip;
1357         struct in6_addr dst6;
1358 #endif
1359         ip_t *ip;
1360
1361         if ((type < 0) || (type > ICMP_MAXTYPE))
1362                 return -1;
1363
1364         code = fin->fin_icode;
1365 #ifdef USE_INET6
1366         if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1367                 return -1;
1368 #endif
1369
1370         avail = 0;
1371         m = NULL;
1372         ifp = fin->fin_ifp;
1373         if (fin->fin_v == 4) {
1374                 if ((oip->ip_p == IPPROTO_ICMP) &&
1375                     !(fin->fin_fi.fi_fl & FI_SHORT))
1376                         switch (ntohs(fin->fin_data[0]) >> 8)
1377                         {
1378                         case ICMP_ECHO :
1379                         case ICMP_TSTAMP :
1380                         case ICMP_IREQ :
1381                         case ICMP_MASKREQ :
1382                                 break;
1383                         default :
1384                                 return 0;
1385                         }
1386
1387 # if    (BSD < 199306) || defined(__sgi)
1388                 avail = MLEN;
1389                 m = m_get(M_DONTWAIT, MT_HEADER);
1390 # else
1391                 avail = MHLEN;
1392                 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1393 # endif
1394                 if (m == NULL)
1395                         return ENOBUFS;
1396
1397                 if (dst == 0) {
1398                         if (fr_ifpaddr(4, ifp, &dst4) == -1)
1399                                 return -1;
1400                 } else
1401                         dst4.s_addr = oip->ip_dst.s_addr;
1402
1403                 hlen = sizeof(ip_t);
1404                 ohlen = oip->ip_hl << 2;
1405                 xtra = 8;
1406         }
1407
1408 #ifdef  USE_INET6
1409         else if (fin->fin_v == 6) {
1410                 hlen = sizeof(ip6_t);
1411                 ohlen = sizeof(ip6_t);
1412                 type = icmptoicmp6types[type];
1413                 if (type == ICMP6_DST_UNREACH)
1414                         code = icmptoicmp6unreach[code];
1415
1416                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1417                 if (!m)
1418                         return ENOBUFS;
1419
1420                 MCLGET(m, M_DONTWAIT);
1421                 if ((m->m_flags & M_EXT) == 0) {
1422                         m_freem(m);
1423                         return ENOBUFS;
1424                 }
1425 # ifdef M_TRAILINGSPACE
1426                 m->m_len = 0;
1427                 avail = M_TRAILINGSPACE(m);
1428 # else
1429                 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1430 # endif
1431                 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1432                            avail - hlen - sizeof(*icmp) - max_linkhdr);
1433                 if (dst == 0) {
1434                         if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1435                                 return -1;
1436                 } else
1437                         dst6 = oip6->ip6_dst;
1438         }
1439 #endif
1440
1441         iclen = hlen + sizeof(*icmp);
1442 # if    BSD >= 199306
1443         avail -= (max_linkhdr + iclen);
1444         m->m_data += max_linkhdr;
1445         m->m_pkthdr.rcvif = (struct ifnet *)0;
1446         if (xtra > avail)
1447                 xtra = avail;
1448         iclen += xtra;
1449         m->m_pkthdr.len = iclen;
1450 #else
1451         avail -= (m->m_off + iclen);
1452         if (xtra > avail)
1453                 xtra = avail;
1454         iclen += xtra;
1455 #endif
1456         m->m_len = iclen;
1457         ip = mtod(m, ip_t *);
1458         icmp = (struct icmp *)((char *)ip + hlen);
1459         bzero((char *)ip, iclen);
1460
1461         icmp->icmp_type = type;
1462         icmp->icmp_code = fin->fin_icode;
1463         icmp->icmp_cksum = 0;
1464 #ifdef  icmp_nextmtu
1465         if (type == ICMP_UNREACH &&
1466             fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1467                 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1468 #endif
1469
1470         if (avail) {
1471                 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1472                 avail -= MIN(ohlen, avail);
1473         }
1474
1475 #ifdef  USE_INET6
1476         ip6 = (ip6_t *)ip;
1477         if (fin->fin_v == 6) {
1478                 ip6->ip6_flow = 0;
1479                 ip6->ip6_plen = htons(iclen - hlen);
1480                 ip6->ip6_nxt = IPPROTO_ICMPV6;
1481                 ip6->ip6_hlim = 0;
1482                 ip6->ip6_src = dst6;
1483                 ip6->ip6_dst = oip6->ip6_src;
1484                 if (avail)
1485                         bcopy((char *)oip + ohlen,
1486                               (char *)&icmp->icmp_ip + ohlen, avail);
1487                 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1488                                              sizeof(*ip6), iclen - hlen);
1489         } else
1490 #endif
1491         {
1492                 slen = oip->ip_len;
1493                 oip->ip_len = htons(oip->ip_len);
1494                 soff = oip->ip_off;
1495                 oip->ip_off = htons(ip->ip_off);
1496
1497                 ip->ip_src.s_addr = dst4.s_addr;
1498                 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1499
1500                 if (avail > 8)
1501                         avail = 8;
1502                 if (avail)
1503                         bcopy((char *)oip + ohlen,
1504                               (char *)&icmp->icmp_ip + ohlen, avail);
1505                 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1506                                              sizeof(*icmp) + 8);
1507                 ip->ip_len = iclen;
1508                 ip->ip_p = IPPROTO_ICMP;
1509         }
1510
1511         shlen = fin->fin_hlen;
1512         fin->fin_hlen = hlen;
1513         err = send_ip(oip, fin, &m);
1514         fin->fin_hlen = shlen;
1515 #ifdef  USE_INET6
1516         if (fin->fin_v == 4)
1517 #endif
1518         {
1519                 oip->ip_len = slen;
1520                 oip->ip_off = soff;
1521         }
1522         return err;
1523 }
1524
1525
1526 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1527      (!defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1528 #  if   (BSD < 199306)
1529 int iplinit __P((void));
1530
1531 int
1532 #  else
1533 void iplinit __P((void));
1534
1535 void
1536 #  endif
1537 iplinit()
1538 {
1539
1540 #  if defined(__NetBSD__) || defined(__OpenBSD__)
1541         if (ipl_enable() != 0)
1542 #  else
1543         if (iplattach() != 0)
1544 #  endif
1545         {
1546                 printf("IP Filter failed to attach\n");
1547         }
1548         ip_init();
1549 }
1550 # endif /* ! __NetBSD__ */
1551
1552
1553 /*
1554  * Return the length of the entire mbuf.
1555  */
1556 size_t mbufchainlen(m0)
1557 struct mbuf *m0;
1558 {
1559 #if BSD >= 199306
1560         return m0->m_pkthdr.len;
1561 #else
1562         size_t len = 0;
1563
1564         for (; m0; m0 = m0->m_next)
1565                 len += m0->m_len;
1566         return len;
1567 #endif
1568 }
1569
1570
1571 int ipfr_fastroute(m0, mpp, fin, fdp)
1572 struct mbuf *m0, **mpp;
1573 fr_info_t *fin;
1574 frdest_t *fdp;
1575 {
1576         struct ip *ip, *mhip;
1577         struct mbuf *m = m0;
1578         struct route *ro;
1579         int len, off, error = 0, hlen, code;
1580         struct ifnet *ifp, *sifp;
1581         struct sockaddr_in *dst;
1582         struct route iproute;
1583         frentry_t *fr;
1584
1585         ip = NULL;
1586         ro = NULL;
1587         ifp = NULL;
1588         ro = &iproute;
1589         ro->ro_rt = NULL;
1590
1591 #ifdef  USE_INET6
1592         if (fin->fin_v == 6) {
1593                 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1594                 if (error != 0)
1595                         goto bad;
1596                 goto done;
1597         }
1598 #else
1599         if (fin->fin_v == 6)
1600                 goto bad;
1601 #endif
1602
1603 #ifdef  M_WRITABLE
1604         /*
1605          * HOT FIX/KLUDGE:
1606          *
1607          * If the mbuf we're about to send is not writable (because of
1608          * a cluster reference, for example) we'll need to make a copy
1609          * of it since this routine modifies the contents.
1610          *
1611          * If you have non-crappy network hardware that can transmit data
1612          * from the mbuf, rather than making a copy, this is gonna be a
1613          * problem.
1614          */
1615         if (M_WRITABLE(m) == 0) {
1616                 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1617                         m_freem(*mpp);
1618                         *mpp = m0;
1619                         m = m0;
1620                 } else {
1621                         error = ENOBUFS;
1622                         m_freem(*mpp);
1623                         goto done;
1624                 }
1625         }
1626 #endif
1627
1628         hlen = fin->fin_hlen;
1629         ip = mtod(m0, struct ip *);
1630
1631 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1632         /*
1633          * Clear any in-bound checksum flags for this packet.
1634          */
1635 # if (__NetBSD_Version__ > 105009999)
1636         m0->m_pkthdr.csum_flags = 0;
1637 # else
1638         m0->m_pkthdr.csuminfo = 0;
1639 # endif
1640 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1641
1642         /*
1643          * Route packet.
1644          */
1645 #if defined(__sgi) && (IRIX >= 605)
1646         ROUTE_RDLOCK();
1647 #endif
1648         bzero((caddr_t)ro, sizeof (*ro));
1649         dst = (struct sockaddr_in *)&ro->ro_dst;
1650         dst->sin_family = AF_INET;
1651         dst->sin_addr = ip->ip_dst;
1652
1653         fr = fin->fin_fr;
1654         if (fdp != NULL)
1655                 ifp = fdp->fd_ifp;
1656         else
1657                 ifp = fin->fin_ifp;
1658
1659         /*
1660          * In case we're here due to "to <if>" being used with "keep state",
1661          * check that we're going in the correct direction.
1662          */
1663         if ((fr != NULL) && (fin->fin_rev != 0)) {
1664                 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1665                         return 0;
1666         } else if (fdp != NULL) {
1667                 if (fdp->fd_ip.s_addr != 0)
1668                         dst->sin_addr = fdp->fd_ip;
1669         }
1670
1671 # if BSD >= 199306
1672         dst->sin_len = sizeof(*dst);
1673 # endif
1674 # if    (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1675         !defined(__OpenBSD__)
1676 #  ifdef        RTF_CLONING
1677         rtalloc_ign(ro, RTF_CLONING);
1678 #  else
1679         rtalloc_ign(ro, RTF_PRCLONING);
1680 #  endif
1681 # else
1682         rtalloc(ro);
1683 # endif
1684
1685 #if defined(__sgi) && (IRIX > 602)
1686         ROUTE_UNLOCK();
1687 #endif
1688
1689         if (!ifp) {
1690                 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1691                         error = -2;
1692                         goto bad;
1693                 }
1694         }
1695
1696         if ((ifp == NULL) && (ro->ro_rt != NULL))
1697                 ifp = ro->ro_rt->rt_ifp;
1698
1699         if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1700                 if (in_localaddr(ip->ip_dst))
1701                         error = EHOSTUNREACH;
1702                 else
1703                         error = ENETUNREACH;
1704                 goto bad;
1705         }
1706
1707         if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1708 #if BSD >= 199306
1709                 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1710 #else
1711                 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1712 #endif
1713         }
1714         ro->ro_rt->rt_use++;
1715
1716         /*
1717          * For input packets which are being "fastrouted", they won't
1718          * go back through output filtering and miss their chance to get
1719          * NAT'd and counted.
1720          */
1721         if (fin->fin_out == 0) {
1722                 sifp = fin->fin_ifp;
1723                 fin->fin_ifp = ifp;
1724                 fin->fin_out = 1;
1725                 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1726                     (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1727                         ATOMIC_INCL(frstats[1].fr_acct);
1728                 }
1729                 fin->fin_fr = NULL;
1730                 if (!fr || !(fr->fr_flags & FR_RETMASK))
1731                         (void) fr_checkstate(ip, fin);
1732                 (void) ip_natout(ip, fin);
1733                 fin->fin_ifp = sifp;
1734         } else
1735                 ip->ip_sum = 0;
1736         /*
1737          * If small enough for interface, can just send directly.
1738          */
1739         if (ip->ip_len <= ifp->if_mtu) {
1740 # ifndef sparc
1741 #  if (!defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1742       !(__NetBSD_Version__ >= 105110000)
1743                 ip->ip_id = htons(ip->ip_id);
1744 #  endif
1745                 ip->ip_len = htons(ip->ip_len);
1746                 ip->ip_off = htons(ip->ip_off);
1747 # endif
1748 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1749 #  if (__NetBSD_Version__ > 105009999)
1750                 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1751                         m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1752                 else if (ip->ip_sum == 0)
1753                         ip->ip_sum = in_cksum(m, hlen);
1754 #  else
1755                 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1756                         m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1757                 else if (ip->ip_sum == 0)
1758                         ip->ip_sum = in_cksum(m, hlen);
1759 #  endif
1760 # else
1761                 if (!ip->ip_sum)
1762                         ip->ip_sum = in_cksum(m, hlen);
1763 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1764 # if    (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1765                 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1766                                           ro->ro_rt);
1767 # else
1768                 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1769 # endif
1770                 goto done;
1771         }
1772
1773         /*
1774          * Too large for interface; fragment if possible.
1775          * Must be able to put at least 8 bytes per fragment.
1776          */
1777         if (ip->ip_off & IP_DF) {
1778                 error = EMSGSIZE;
1779                 goto bad;
1780         }
1781         len = (ifp->if_mtu - hlen) &~ 7;
1782         if (len < 8) {
1783                 error = EMSGSIZE;
1784                 goto bad;
1785         }
1786
1787     {
1788         int mhlen, firstlen = len;
1789         struct mbuf **mnext = &m->m_act;
1790
1791         /*
1792          * Loop through length of segment after first fragment,
1793          * make new header and copy data of each part and link onto chain.
1794          */
1795         m0 = m;
1796         mhlen = sizeof (struct ip);
1797         for (off = hlen + len; off < ip->ip_len; off += len) {
1798 # ifdef MGETHDR
1799                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1800 # else
1801                 MGET(m, M_DONTWAIT, MT_HEADER);
1802 # endif
1803                 if (m == 0) {
1804                         error = ENOBUFS;
1805                         goto bad;
1806                 }
1807 # if BSD >= 199306
1808                 m->m_data += max_linkhdr;
1809 # else
1810                 m->m_off = MMAXOFF - hlen;
1811 # endif
1812                 mhip = mtod(m, struct ip *);
1813                 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1814                 if (hlen > sizeof (struct ip)) {
1815                         mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1816                         mhip->ip_hl = mhlen >> 2;
1817                 }
1818                 m->m_len = mhlen;
1819                 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1820                 if (ip->ip_off & IP_MF)
1821                         mhip->ip_off |= IP_MF;
1822                 if (off + len >= ip->ip_len)
1823                         len = ip->ip_len - off;
1824                 else
1825                         mhip->ip_off |= IP_MF;
1826                 mhip->ip_len = htons((u_short)(len + mhlen));
1827                 m->m_next = m_copy(m0, off, len);
1828                 if (m->m_next == 0) {
1829                         error = ENOBUFS;        /* ??? */
1830                         goto sendorfree;
1831                 }
1832 # if BSD >= 199306
1833                 m->m_pkthdr.len = mhlen + len;
1834                 m->m_pkthdr.rcvif = NULL;
1835 # endif
1836                 mhip->ip_off = htons((u_short)mhip->ip_off);
1837                 mhip->ip_sum = 0;
1838                 mhip->ip_sum = in_cksum(m, mhlen);
1839                 *mnext = m;
1840                 mnext = &m->m_act;
1841         }
1842         /*
1843          * Update first fragment by trimming what's been copied out
1844          * and updating header, then send each fragment (in order).
1845          */
1846         m_adj(m0, hlen + firstlen - ip->ip_len);
1847         ip->ip_len = htons((u_short)(hlen + firstlen));
1848         ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1849         ip->ip_sum = 0;
1850         ip->ip_sum = in_cksum(m0, hlen);
1851 sendorfree:
1852         for (m = m0; m; m = m0) {
1853                 m0 = m->m_act;
1854                 m->m_act = 0;
1855                 if (error == 0)
1856 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1857                         error = (*ifp->if_output)(ifp, m,
1858                             (struct sockaddr *)dst, ro->ro_rt);
1859 # else
1860                         error = (*ifp->if_output)(ifp, m,
1861                             (struct sockaddr *)dst);
1862 # endif
1863                 else
1864                         m_freem(m);
1865         }
1866     }   
1867 done:
1868         if (!error)
1869                 ipl_frouteok[0]++;
1870         else
1871                 ipl_frouteok[1]++;
1872
1873         if (ro->ro_rt != NULL) {
1874                 RTFREE(ro->ro_rt);
1875         }
1876         *mpp = NULL;
1877         return error;
1878 bad:
1879         if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1880                 sifp = fin->fin_ifp;
1881                 code = fin->fin_icode;
1882                 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1883                 fin->fin_ifp = ifp;
1884                 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1885                 fin->fin_ifp = sifp;
1886                 fin->fin_icode = code;
1887         }
1888         m_freem(m);
1889         goto done;
1890 }
1891
1892
1893 /*
1894  * Return true or false depending on whether the route to the
1895  * given IP address uses the same interface as the one passed.
1896  */
1897 int fr_verifysrc(ipa, ifp)
1898 struct in_addr ipa;
1899 void *ifp;
1900 {
1901         struct sockaddr_in *dst;
1902         struct route iproute;
1903
1904         bzero((char *)&iproute, sizeof(iproute));
1905         dst = (struct sockaddr_in *)&iproute.ro_dst;
1906 # if    (BSD >= 199306)
1907         dst->sin_len = sizeof(*dst);
1908 # endif
1909         dst->sin_family = AF_INET;
1910         dst->sin_addr = ipa;
1911 # if    (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1912         !defined(__OpenBSD__)
1913 #  ifdef        RTF_CLONING
1914         rtalloc_ign(&iproute, RTF_CLONING);
1915 #  else
1916         rtalloc_ign(&iproute, RTF_PRCLONING);
1917 #  endif
1918 # else
1919         rtalloc(&iproute);
1920 # endif
1921         if (iproute.ro_rt == NULL)
1922                 return 0;
1923         return (ifp == iproute.ro_rt->rt_ifp);
1924 }
1925
1926
1927 # ifdef USE_GETIFNAME
1928 char *
1929 get_ifname(ifp)
1930 struct ifnet *ifp;
1931 {
1932         static char workbuf[64];
1933
1934         sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1935         return workbuf;
1936 }
1937 # endif
1938
1939
1940 # if defined(USE_INET6)
1941 /*
1942  * This is the IPv6 specific fastroute code.  It doesn't clean up the mbuf's
1943  * or ensure that it is an IPv6 packet that is being forwarded, those are
1944  * expected to be done by the called (ipfr_fastroute).
1945  */
1946 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1947 struct mbuf *m0, **mpp;
1948 fr_info_t *fin;
1949 frdest_t *fdp;
1950 {
1951         struct route_in6 ip6route;
1952         struct sockaddr_in6 *dst6;
1953         struct route_in6 *ro;
1954         struct ifnet *ifp;
1955         frentry_t *fr;
1956 #if defined(OpenBSD) && (OpenBSD >= 200211)
1957         struct route_in6 *ro_pmtu = NULL;
1958         struct in6_addr finaldst;
1959         ip6_t *ip6;
1960 #endif
1961         u_long mtu;
1962         int error;
1963
1964         ifp = NULL;
1965         ro = &ip6route;
1966         fr = fin->fin_fr;
1967         bzero((caddr_t)ro, sizeof(*ro));
1968         dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
1969         dst6->sin6_family = AF_INET6;
1970         dst6->sin6_len = sizeof(struct sockaddr_in6);
1971         dst6->sin6_addr = fin->fin_fi.fi_src.in6;
1972
1973         if (fdp != NULL)
1974                 ifp = fdp->fd_ifp;
1975
1976         if ((fr != NULL) && (fin->fin_rev != 0)) {
1977                 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1978                         return 0;
1979         } else if (fdp != NULL) {
1980                 if (IP6_NOTZERO(&fdp->fd_ip6))
1981                         dst6->sin6_addr = fdp->fd_ip6.in6;
1982         }
1983         if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
1984                 return -2;
1985
1986         rtalloc((struct route *)ro);
1987
1988         if ((ifp == NULL) && (ro->ro_rt != NULL))
1989                 ifp = ro->ro_rt->rt_ifp;
1990
1991         if ((ro->ro_rt == NULL) || (ifp == NULL) ||
1992             (ifp != ro->ro_rt->rt_ifp)) {
1993                 error = EHOSTUNREACH;
1994         } else {
1995                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
1996                         dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
1997                 ro->ro_rt->rt_use++;
1998
1999 #if defined(OpenBSD) && (OpenBSD >= 200211)
2000                 ip6 = mtod(m0, ip6_t *);
2001                 ro_pmtu = ro;
2002                 finaldst = ip6->ip6_dst;
2003                 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2004                 if (error == 0) {
2005 #else
2006                         mtu = nd_ifinfo[ifp->if_index].linkmtu;
2007 #endif
2008                         if (m0->m_pkthdr.len <= mtu)
2009                                 error = nd6_output(ifp, fin->fin_ifp, m0,
2010                                                    dst6, ro->ro_rt);
2011                         else
2012                                 error = EMSGSIZE;
2013 #if defined(OpenBSD) && (OpenBSD >= 200211)
2014                 }
2015 #endif
2016         }
2017
2018         if (ro->ro_rt != NULL) {
2019                 RTFREE(ro->ro_rt);
2020         }
2021         return error;
2022 }
2023 # endif
2024 #else /* #ifdef _KERNEL */
2025
2026
2027 # if defined(__sgi) && (IRIX < 605)
2028 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2029                            struct sockaddr *s))
2030 # else
2031 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2032                            struct sockaddr *s, struct rtentry *rt))
2033 # endif
2034 {
2035         return 0;
2036 }
2037
2038
2039 # ifdef __STDC__
2040 #  if defined(__sgi) && (IRIX < 605)
2041 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2042                              struct sockaddr *s))
2043 #  else
2044 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2045                              struct sockaddr *s, struct rtentry *rt))
2046 #  endif
2047 {
2048         ip_t *ip = (ip_t *)m;
2049 # else
2050 static int write_output(ifp, ip)
2051 struct ifnet *ifp;
2052 ip_t *ip;
2053 {
2054 # endif
2055         char fname[32];
2056         int fd;
2057
2058 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2059         (defined(OpenBSD) && (OpenBSD >= 199603))
2060         sprintf(fname, "%s", ifp->if_xname);
2061 # else
2062         sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2063 # endif
2064         fd = open(fname, O_WRONLY|O_APPEND);
2065         if (fd == -1) {
2066                 perror("open");
2067                 return -1;
2068         }
2069         write(fd, (char *)ip, ntohs(ip->ip_len));
2070         close(fd);
2071         return 0;
2072 }
2073
2074
2075 char *get_ifname(ifp)
2076 struct ifnet *ifp;
2077 {
2078 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2079      (defined(OpenBSD) && (OpenBSD >= 199603))
2080         return ifp->if_xname;
2081 # else
2082         static char fullifname[LIFNAMSIZ];
2083
2084         sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2085         return fullifname;
2086 # endif
2087 }
2088
2089
2090 struct ifnet *get_unit(ifname, v)
2091 char *ifname;
2092 int v;
2093 {
2094         struct ifnet *ifp, **ifa, **old_ifneta;
2095
2096         for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2097 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2098      (defined(OpenBSD) && (OpenBSD >= 199603))
2099                 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2100 # else
2101                 char fullname[LIFNAMSIZ];
2102
2103                 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2104                 if (!strcmp(ifname, fullname))
2105 # endif
2106                         return ifp;
2107         }
2108
2109         if (!ifneta) {
2110                 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2111                 if (!ifneta)
2112                         return NULL;
2113                 ifneta[1] = NULL;
2114                 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2115                 if (!ifneta[0]) {
2116                         free(ifneta);
2117                         return NULL;
2118                 }
2119                 nifs = 1;
2120         } else {
2121                 old_ifneta = ifneta;
2122                 nifs++;
2123                 ifneta = (struct ifnet **)realloc(ifneta,
2124                                                   (nifs + 1) * sizeof(*ifa));
2125                 if (!ifneta) {
2126                         free(old_ifneta);
2127                         nifs = 0;
2128                         return NULL;
2129                 }
2130                 ifneta[nifs] = NULL;
2131                 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2132                 if (!ifneta[nifs - 1]) {
2133                         nifs--;
2134                         return NULL;
2135                 }
2136         }
2137         ifp = ifneta[nifs - 1];
2138
2139 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2140      (defined(OpenBSD) && (OpenBSD >= 199603))
2141         strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2142 # else
2143         ifp->if_name = strdup(ifname);
2144
2145         ifname = ifp->if_name;
2146         while (*ifname && !isdigit(*ifname))
2147                 ifname++;
2148         if (*ifname && isdigit(*ifname)) {
2149                 ifp->if_unit = atoi(ifname);
2150                 *ifname = '\0';
2151         } else
2152                 ifp->if_unit = -1;
2153 # endif
2154         ifp->if_output = no_output;
2155         return ifp;
2156 }
2157
2158
2159
2160 void init_ifp()
2161 {
2162         struct ifnet *ifp, **ifa;
2163         char fname[32];
2164         int fd;
2165
2166 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2167         (defined(OpenBSD) && (OpenBSD >= 199603))
2168         for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2169                 ifp->if_output = write_output;
2170                 sprintf(fname, "/tmp/%s", ifp->if_xname);
2171                 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2172                 if (fd == -1)
2173                         perror("open");
2174                 else
2175                         close(fd);
2176         }
2177 # else
2178
2179         for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2180                 ifp->if_output = write_output;
2181                 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2182                 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2183                 if (fd == -1)
2184                         perror("open");
2185                 else
2186                         close(fd);
2187         }
2188 # endif
2189 }
2190
2191
2192 int send_reset(ip, fin)
2193 ip_t *ip;
2194 fr_info_t *fin;
2195 {
2196         verbose("- TCP RST sent\n");
2197         return 0;
2198 }
2199
2200
2201 int send_icmp_err(ip, code, fin, dst)
2202 ip_t *ip;
2203 int code;
2204 fr_info_t *fin;
2205 int dst;
2206 {
2207         verbose("- ICMP UNREACHABLE sent\n");
2208         return 0;
2209 }
2210
2211
2212 void frsync()
2213 {
2214         return;
2215 }
2216
2217 void m_copydata(m, off, len, cp)
2218 mb_t *m;
2219 int off, len;
2220 caddr_t cp;
2221 {
2222         bcopy((char *)m + off, cp, len);
2223 }
2224
2225
2226 int ipfuiomove(buf, len, rwflag, uio)
2227 caddr_t buf;
2228 int len, rwflag;
2229 struct uio *uio;
2230 {
2231         int left, ioc, num, offset;
2232         struct iovec *io;
2233         char *start;
2234
2235         if (rwflag == UIO_READ) {
2236                 left = len;
2237                 ioc = 0;
2238
2239                 offset = uio->uio_offset;
2240
2241                 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2242                         io = uio->uio_iov + ioc;
2243                         num = io->iov_len;
2244                         if (num > left)
2245                                 num = left;
2246                         start = (char *)io->iov_base + offset;
2247                         if (start > (char *)io->iov_base + io->iov_len) {
2248                                 offset -= io->iov_len;
2249                                 ioc++;
2250                                 continue;
2251                         }
2252                         bcopy(buf, start, num);
2253                         uio->uio_resid -= num;
2254                         uio->uio_offset += num;
2255                         left -= num;
2256                         if (left > 0)
2257                                 ioc++;
2258                 }
2259                 if (left > 0)
2260                         return EFAULT;
2261         }
2262         return 0;
2263 }
2264 #endif /* _KERNEL */