2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
8 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
11 #if defined(KERNEL) && !defined(_KERNEL)
14 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
15 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
16 #include "opt_inet6.h"
18 #include <sys/param.h>
19 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
20 defined(_KERNEL) && !defined(_LKM)
21 # include "opt_ipfilter_log.h"
23 #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
24 # if !defined(_KERNEL) || defined(IPFILTER_LKM)
25 # include <osreldate.h>
30 # include <sys/ptimers.h>
39 #include <sys/errno.h>
40 #include <sys/types.h>
42 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
43 # include <sys/fcntl.h>
44 # include <sys/filio.h>
46 # include <sys/ioctl.h>
50 # include <sys/systm.h>
53 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
54 # include <sys/dirent.h>
58 # include <sys/mbuf.h>
60 # include <sys/filio.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
69 #if __FreeBSD_version >= 300000
70 # include <net/if_var.h>
71 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
72 # include "opt_ipfilter.h"
76 #include <sys/debug.h>
77 # ifdef IFF_DRVRLOCK /* IRIX6 */
78 #include <sys/hashing.h>
81 #include <net/route.h>
82 #include <netinet/in.h>
83 #if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /* IRIX < 6 */
84 # include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/tcp.h>
90 #include <netinet/udp.h>
91 #include <netinet/tcpip.h>
92 #include <netinet/ip_icmp.h>
97 #include "netinet/ip_compat.h"
99 # include <netinet/icmp6.h>
101 # include <netinet6/ip6protosw.h>
102 # include <netinet6/nd6.h>
105 #include "netinet/ip_fil.h"
106 #include "netinet/ip_nat.h"
107 #include "netinet/ip_frag.h"
108 #include "netinet/ip_state.h"
109 #include "netinet/ip_proxy.h"
110 #include "netinet/ip_auth.h"
111 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
112 # include <sys/malloc.h>
115 # define MIN(a,b) (((a)<(b))?(a):(b))
117 #if !SOLARIS && defined(_KERNEL) && !defined(__sgi)
118 # include <sys/kernel.h>
119 extern int ip_optcopy __P((struct ip *, struct ip *));
122 #include <machine/in_cksum.h>
125 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
126 /* static const char rcsid[] = "@(#)$Id: ip_fil.c,v 2.42.2.34 2001/07/23 13:49:57 darrenr Exp $"; */
127 static const char rcsid[] = "@(#)$FreeBSD$";
130 extern struct protosw inetsw[];
134 static struct ifnet **ifneta = NULL;
137 # if (BSD < 199306) || defined(__sgi)
142 #ifdef ICMP_UNREACH_FILTER_PROHIB
143 int ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
145 int ipl_unreach = ICMP_UNREACH_FILTER;
147 u_long ipl_frouteok[2] = {0, 0};
149 static int frzerostats __P((caddr_t));
150 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
151 static int frrequest __P((int, u_long, caddr_t, int));
153 static int frrequest __P((int, int, caddr_t, int));
156 static int (*fr_savep) __P((ip_t *, int, void *, int, struct mbuf **));
157 static int send_ip __P((ip_t *, fr_info_t *, struct mbuf **));
159 static int ipfr_fastroute6 __P((struct mbuf *, struct mbuf **,
160 fr_info_t *, frdest_t *));
163 extern kmutex_t ipf_rw;
164 extern KRWLOCK_T ipf_mutex;
167 void init_ifp __P((void));
168 # if defined(__sgi) && (IRIX < 605)
169 static int no_output __P((struct ifnet *, struct mbuf *,
171 static int write_output __P((struct ifnet *, struct mbuf *,
174 static int no_output __P((struct ifnet *, struct mbuf *,
175 struct sockaddr *, struct rtentry *));
176 static int write_output __P((struct ifnet *, struct mbuf *,
177 struct sockaddr *, struct rtentry *));
182 #if (__FreeBSD_version >= 300000) && defined(_KERNEL)
183 struct callout_handle ipfr_slowtimer_ch;
185 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
186 # include <sys/callout.h>
187 struct callout ipfr_slowtimer_ch;
189 #if defined(__OpenBSD__)
190 # include <sys/timeout.h>
191 struct timeout ipfr_slowtimer_ch;
193 #if defined(__sgi) && defined(_KERNEL)
194 toid_t ipfr_slowtimer_ch;
197 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
198 # include <sys/device.h>
199 # include <sys/conf.h>
201 struct cfdriver iplcd = {
202 NULL, "ipl", NULL, NULL, DV_DULL, 0
205 struct devsw iplsw = {
207 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
208 nostrat, nodump, nopsize, 0,
211 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
213 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
214 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 500011)
215 # include <sys/conf.h>
216 # if defined(NETBSD_PF)
217 # include <net/pfil.h>
219 * We provide the fr_checkp name just to minimize changes later.
221 int (*fr_checkp) __P((ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
222 # endif /* NETBSD_PF */
223 #endif /* __NetBSD__ */
226 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
228 # include <net/pfil.h>
230 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
232 static int fr_check_wrapper(arg, mp, ifp, dir)
238 struct ip *ip = mtod(*mp, struct ip *);
239 int rv, hlen = ip->ip_hl << 2;
241 #if defined(M_CSUM_TCPv4)
243 * If the packet is out-bound, we can't delay checksums
244 * here. For in-bound, the checksum has already been
247 if (dir == PFIL_OUT) {
248 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
249 in_delayed_cksum(*mp);
250 (*mp)->m_pkthdr.csum_flags &=
251 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
254 #endif /* M_CSUM_TCPv4 */
257 * We get the packet with all fields in network byte
258 * order. We expect ip_len and ip_off to be in host
259 * order. We frob them, call the filter, then frob
262 * Note, we don't need to update the checksum, because
263 * it has already been verified.
268 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
270 if (rv == 0 && *mp != NULL) {
271 ip = mtod(*mp, struct ip *);
280 # include <netinet/ip6.h>
282 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
284 static int fr_check_wrapper6(arg, mp, ifp, dir)
291 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
292 ifp, (dir == PFIL_OUT), mp));
295 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
297 # if defined(IPFILTER_LKM) && !defined(__sgi)
301 if (strcmp(s, "ipl") == 0)
305 # endif /* IPFILTER_LKM */
309 * Try to detect the case when compiling for NetBSD with pseudo-device
311 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
313 ipfilterattach(count)
318 * Do nothing here, really. The filter will be enabled
319 * by the SIOCFRENB ioctl.
325 # if defined(__NetBSD__)
333 # if defined(__sgi) || (defined(NETBSD_PF) && \
334 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011)))
337 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
338 struct pfil_head *ph_inet;
340 struct pfil_head *ph_inet6;
345 if (fr_running || (fr_checkp == fr_check)) {
346 printf("IP Filter: already initialized\n");
354 if (nat_init() == -1) {
358 if (fr_stateinit() == -1) {
362 if (appr_init() == -1) {
368 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011)
369 # if __NetBSD_Version__ >= 105110000
371 !(ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET))
373 && !(ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6))
379 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
380 PFIL_IN|PFIL_OUT, ph_inet);
384 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
385 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
399 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
402 # if __NetBSD_Version__ >= 105110000
403 if (ph_inet6 != NULL)
404 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
405 PFIL_IN|PFIL_OUT, ph_inet6);
409 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
410 PFIL_IN|PFIL_OUT, ph_inet6);
412 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
413 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
415 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
416 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
429 error = ipfilter_sgi_attach();
439 bzero((char *)frcache, sizeof(frcache));
440 fr_savep = fr_checkp;
441 fr_checkp = fr_check;
445 if (fr_pass & FR_PASS)
447 else if (fr_pass & FR_BLOCK)
450 defpass = "no-match -> block";
452 printf("%s initialized. Default = %s all, Logging = %s\n",
453 ipfilter_version, defpass,
460 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
461 callout_init(&ipfr_slowtimer_ch);
462 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
464 # if defined(__OpenBSD__)
465 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
466 timeout_add(&ipfr_slowtimer_ch, hz/2);
468 # if (__FreeBSD_version >= 300000) || defined(__sgi)
469 ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
471 timeout(ipfr_slowtimer, NULL, hz/2);
481 * Disable the filter by removing the hooks from the IP input/output
484 # if defined(__NetBSD__)
490 int s, i = FR_INQUE|FR_OUTQUE;
491 #if defined(NETBSD_PF) && \
492 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
494 # if __NetBSD_Version__ >= 105150000
495 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
497 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
503 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
504 callout_stop(&ipfr_slowtimer_ch);
506 # if (__FreeBSD_version >= 300000)
507 untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
510 untimeout(ipfr_slowtimer_ch);
512 # if defined(__OpenBSD__)
513 timeout_del(&ipfr_slowtimer_ch);
515 untimeout(ipfr_slowtimer, NULL);
516 # endif /* OpenBSD */
518 # endif /* FreeBSD */
524 printf("IP Filter: not initialized\n");
529 printf("%s unloaded\n", ipfilter_version);
531 fr_checkp = fr_savep;
532 i = frflush(IPL_LOGIPF, i);
536 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
537 # if __NetBSD_Version__ >= 105110000
539 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
540 PFIL_IN|PFIL_OUT, ph_inet);
544 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
545 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
552 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
555 # if __NetBSD_Version__ >= 105110000
556 if (ph_inet6 != NULL)
557 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
558 PFIL_IN|PFIL_OUT, ph_inet6);
562 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
563 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
573 ipfilter_sgi_detach();
588 static int frzerostats(data)
595 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
599 bzero((char *)frstats, sizeof(*frstats) * 2);
606 * Filter ioctl interface.
609 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
611 , cred_t *cp, int *rp
615 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
616 # if (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
617 (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
618 defined(__OpenBSD__)))
625 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
626 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
635 #if defined(_KERNEL) && !SOLARIS
638 int error = 0, unit = 0, tmp;
640 #if (BSD >= 199306) && defined(_KERNEL)
641 if ((securelevel >= 3) && (mode & FWRITE))
645 unit = GET_MINOR(dev);
646 if ((IPL_LOGMAX < unit) || (unit < 0))
652 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
657 if (unit == IPL_LOGNAT) {
659 error = nat_ioctl(data, cmd, mode);
665 if (unit == IPL_LOGSTATE) {
667 error = fr_state_ioctl(data, cmd, mode);
673 if (unit == IPL_LOGAUTH) {
677 error = fr_auth_ioctl(data, mode, cmd, NULL, NULL);
685 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
686 sizeof(iplused[IPL_LOGIPF]));
689 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
694 if (!(mode & FWRITE))
697 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
701 # if defined(__NetBSD__)
702 error = ipl_enable();
707 # if defined(__NetBSD__)
708 error = ipl_disable();
717 if (!(mode & FWRITE))
720 error = IRCOPY(data, (caddr_t)&fr_flags,
724 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
730 if (!(mode & FWRITE))
733 error = frrequest(unit, cmd, data, fr_active);
738 if (!(mode & FWRITE))
741 error = frrequest(unit, cmd, data, 1 - fr_active);
744 if (!(mode & FWRITE))
747 bzero((char *)frcache, sizeof(frcache[0]) * 2);
748 *(u_int *)data = fr_active;
749 fr_active = 1 - fr_active;
757 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
763 if (!(mode & FWRITE))
766 error = frzerostats(data);
769 if (!(mode & FWRITE))
772 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
774 tmp = frflush(unit, tmp);
775 error = IWCOPY((caddr_t)&tmp, data,
781 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
792 if (!(mode & FWRITE))
795 *(int *)data = ipflog_clear(unit);
797 #endif /* IPFILTER_LOG */
799 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
806 if (!(mode & FWRITE)) {
811 if (!(mode & FWRITE))
814 #if defined(_KERNEL) && defined(__sgi)
829 void fr_forgetifp(ifp)
832 register frentry_t *f;
834 WRITE_ENTER(&ipf_mutex);
835 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
836 if (f->fr_ifa == ifp)
837 f->fr_ifa = (void *)-1;
838 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
839 if (f->fr_ifa == ifp)
840 f->fr_ifa = (void *)-1;
841 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
842 if (f->fr_ifa == ifp)
843 f->fr_ifa = (void *)-1;
844 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
845 if (f->fr_ifa == ifp)
846 f->fr_ifa = (void *)-1;
848 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
849 if (f->fr_ifa == ifp)
850 f->fr_ifa = (void *)-1;
851 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
852 if (f->fr_ifa == ifp)
853 f->fr_ifa = (void *)-1;
854 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
855 if (f->fr_ifa == ifp)
856 f->fr_ifa = (void *)-1;
857 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
858 if (f->fr_ifa == ifp)
859 f->fr_ifa = (void *)-1;
861 RWLOCK_EXIT(&ipf_mutex);
866 static int frrequest(unit, req, data, set)
868 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
876 register frentry_t *fp, *f, **fprev;
877 register frentry_t **ftail;
878 frgroup_t *fg = NULL;
879 int error = 0, in, i;
886 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
890 #if (BSD >= 199306) && defined(_KERNEL)
891 if ((securelevel > 0) && (fp->fr_func != NULL))
896 * Check that the group number does exist and that if a head group
897 * has been specified, doesn't exist.
899 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
900 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
901 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
903 if ((req != SIOCZRLST) && fp->fr_group &&
904 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
907 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
909 if (unit == IPL_LOGAUTH)
910 ftail = fprev = &ipauth;
911 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
912 ftail = fprev = &ipacct[in][set];
913 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
914 ftail = fprev = &ipfilter[in][set];
916 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
917 ftail = fprev = &ipacct6[in][set];
918 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
919 ftail = fprev = &ipfilter6[in][set];
924 if ((group = fp->fr_group)) {
925 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
927 ftail = fprev = fg->fg_start;
930 bzero((char *)frcache, sizeof(frcache[0]) * 2);
932 for (i = 0; i < 4; i++) {
933 if ((fp->fr_ifnames[i][1] == '\0') &&
934 ((fp->fr_ifnames[i][0] == '-') ||
935 (fp->fr_ifnames[i][0] == '*'))) {
936 fp->fr_ifas[i] = NULL;
937 } else if (*fp->fr_ifnames[i]) {
938 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
940 fp->fr_ifas[i] = (void *)-1;
945 fp->fr_flags &= ~FR_DUP;
946 if (*fdp->fd_ifname) {
947 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
949 fdp->fd_ifp = (struct ifnet *)-1;
951 fp->fr_flags |= FR_DUP;
955 if (*fdp->fd_ifname) {
956 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
958 fdp->fd_ifp = (struct ifnet *)-1;
962 * Look for a matching filter rule, but don't include the next or
963 * interface pointer in the comparison (fr_next, fr_ifa).
965 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
969 for (; (f = *ftail); ftail = &f->fr_next)
970 if ((fp->fr_cksum == f->fr_cksum) &&
971 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
975 * If zero'ing statistics, copy current to caller and zero.
977 if (req == SIOCZRLST) {
980 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
989 if (req != SIOCINAFR && req != SIOCINIFR)
995 while (--fp->fr_hits && (f = *ftail))
1002 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1007 * Only return EBUSY if there is a group list, else
1008 * it's probably just state information referencing
1011 if ((f->fr_ref > 1) && f->fr_grp)
1013 if (fg && fg->fg_head)
1014 fg->fg_head->fr_ref--;
1016 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1018 fixskip(fprev, f, -1);
1019 *ftail = f->fr_next;
1029 KMALLOC(f, frentry_t *);
1031 if (fg && fg->fg_head)
1032 fg->fg_head->fr_ref++;
1033 bcopy((char *)fp, (char *)f, sizeof(*f));
1036 f->fr_next = *ftail;
1038 if (req == SIOCINIFR || req == SIOCINAFR)
1039 fixskip(fprev, f, 1);
1041 if ((group = f->fr_grhead))
1042 fg = fr_addgroup(group, f, unit, set);
1053 * routines below for saving IP headers to buffer
1057 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1059 int IPL_EXTERN(open)(dev_t dev, int flags)
1062 int IPL_EXTERN(open)(dev, flags
1063 # if ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1064 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1075 # if defined(__sgi) && defined(_KERNEL)
1076 u_int min = geteminor(*pdev);
1078 u_int min = GET_MINOR(dev);
1081 if (IPL_LOGMAX < min)
1090 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1092 int IPL_EXTERN(close)(dev, flags
1093 # if ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1094 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1105 u_int min = GET_MINOR(dev);
1107 if (IPL_LOGMAX < min)
1116 * both of these must operate with at least splnet() lest they be
1117 * called during packet processing and cause an inconsistancy to appear in
1121 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1124 int IPL_EXTERN(read)(dev, uio, ioflag)
1127 int IPL_EXTERN(read)(dev, uio)
1130 register struct uio *uio;
1133 # ifdef IPFILTER_LOG
1134 return ipflog_read(GET_MINOR(dev), uio);
1142 * send_reset - this could conceivably be a call to tcp_respond(), but that
1143 * requires a large amount of setting up and isn't any more efficient.
1145 int send_reset(oip, fin)
1149 struct tcphdr *tcp, *tcp2;
1153 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1157 tcp = (struct tcphdr *)fin->fin_dp;
1158 if (tcp->th_flags & TH_RST)
1159 return -1; /* feedback loop */
1160 # if (BSD < 199306) || defined(__sgi)
1161 m = m_get(M_DONTWAIT, MT_HEADER);
1163 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1170 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1171 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1172 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1175 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1177 hlen = sizeof(ip_t);
1179 m->m_len = sizeof(*tcp2) + hlen;
1181 m->m_data += max_linkhdr;
1182 m->m_pkthdr.len = m->m_len;
1183 m->m_pkthdr.rcvif = (struct ifnet *)0;
1185 ip = mtod(m, struct ip *);
1189 bzero((char *)ip, sizeof(*tcp2) + hlen);
1190 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1192 tcp2->th_sport = tcp->th_dport;
1193 tcp2->th_dport = tcp->th_sport;
1194 if (tcp->th_flags & TH_ACK) {
1195 tcp2->th_seq = tcp->th_ack;
1196 tcp2->th_flags = TH_RST;
1198 tcp2->th_ack = ntohl(tcp->th_seq);
1199 tcp2->th_ack += tlen;
1200 tcp2->th_ack = htonl(tcp2->th_ack);
1201 tcp2->th_flags = TH_RST|TH_ACK;
1203 tcp2->th_off = sizeof(*tcp2) >> 2;
1205 if (fin->fin_v == 6) {
1206 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1207 ip6->ip6_nxt = IPPROTO_TCP;
1208 ip6->ip6_src = oip6->ip6_dst;
1209 ip6->ip6_dst = oip6->ip6_src;
1210 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1211 sizeof(*ip6), sizeof(*tcp2));
1212 return send_ip(oip, fin, &m);
1215 ip->ip_p = IPPROTO_TCP;
1216 ip->ip_len = htons(sizeof(struct tcphdr));
1217 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1218 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1219 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1220 ip->ip_len = hlen + sizeof(*tcp2);
1221 return send_ip(oip, fin, &m);
1226 * Send an IP(v4/v6) datagram out into the network
1228 static int send_ip(oip, fin, mp)
1233 struct mbuf *m = *mp;
1238 bzero((char *)&frn, sizeof(frn));
1239 frn.fin_ifp = fin->fin_ifp;
1240 frn.fin_v = fin->fin_v;
1241 frn.fin_out = fin->fin_out;
1242 frn.fin_mp = fin->fin_mp;
1244 ip = mtod(m, ip_t *);
1247 ip->ip_v = fin->fin_v;
1248 if (ip->ip_v == 4) {
1249 ip->ip_hl = (sizeof(*oip) >> 2);
1250 ip->ip_v = IPVERSION;
1251 ip->ip_tos = oip->ip_tos;
1252 ip->ip_id = oip->ip_id;
1254 # if (BSD < 199306) || defined(__sgi)
1255 ip->ip_ttl = tcp_ttl;
1257 ip->ip_ttl = ip_defttl;
1260 frn.fin_dp = (char *)(ip + 1);
1263 else if (ip->ip_v == 6) {
1264 ip6_t *ip6 = (ip6_t *)ip;
1266 hlen = sizeof(*ip6);
1267 ip6->ip6_hlim = 127;
1268 frn.fin_dp = (char *)(ip6 + 1);
1272 m->m_pkthdr.rcvif = NULL;
1275 fr_makefrip(hlen, ip, &frn);
1277 error = ipfr_fastroute(m, mp, &frn, NULL);
1282 int send_icmp_err(oip, type, fin, dst)
1288 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1289 u_short shlen, slen = 0, soff = 0;
1290 struct in_addr dst4;
1295 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1296 struct in6_addr dst6;
1300 if ((type < 0) || (type > ICMP_MAXTYPE))
1303 code = fin->fin_icode;
1305 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1312 if (fin->fin_v == 4) {
1313 if ((oip->ip_p == IPPROTO_ICMP) &&
1314 !(fin->fin_fi.fi_fl & FI_SHORT))
1315 switch (ntohs(fin->fin_data[0]) >> 8)
1326 # if (BSD < 199306) || defined(__sgi)
1328 m = m_get(M_DONTWAIT, MT_HEADER);
1331 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1337 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1340 dst4.s_addr = oip->ip_dst.s_addr;
1342 hlen = sizeof(ip_t);
1343 ohlen = oip->ip_hl << 2;
1348 else if (fin->fin_v == 6) {
1349 hlen = sizeof(ip6_t);
1350 ohlen = sizeof(ip6_t);
1351 type = icmptoicmp6types[type];
1352 if (type == ICMP6_DST_UNREACH)
1353 code = icmptoicmp6unreach[code];
1355 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1359 MCLGET(m, M_DONTWAIT);
1360 if ((m->m_flags & M_EXT) == 0) {
1364 # ifdef M_TRAILINGSPACE
1366 avail = M_TRAILINGSPACE(m);
1370 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1371 avail - hlen - sizeof(*icmp) - max_linkhdr);
1373 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1376 dst6 = oip6->ip6_dst;
1380 iclen = hlen + sizeof(*icmp);
1382 avail -= (max_linkhdr + iclen);
1383 m->m_data += max_linkhdr;
1384 m->m_pkthdr.rcvif = (struct ifnet *)0;
1388 m->m_pkthdr.len = iclen;
1390 avail -= (m->m_off + iclen);
1396 ip = mtod(m, ip_t *);
1397 icmp = (struct icmp *)((char *)ip + hlen);
1398 bzero((char *)ip, iclen);
1400 icmp->icmp_type = type;
1401 icmp->icmp_code = fin->fin_icode;
1402 icmp->icmp_cksum = 0;
1404 if (type == ICMP_UNREACH &&
1405 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1406 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1410 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1411 avail -= MIN(ohlen, avail);
1416 if (fin->fin_v == 6) {
1418 ip6->ip6_plen = htons(iclen - hlen);
1419 ip6->ip6_nxt = IPPROTO_ICMPV6;
1421 ip6->ip6_src = dst6;
1422 ip6->ip6_dst = oip6->ip6_src;
1424 bcopy((char *)oip + ohlen,
1425 (char *)&icmp->icmp_ip + ohlen, avail);
1426 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1427 sizeof(*ip6), iclen - hlen);
1432 oip->ip_len = htons(oip->ip_len);
1434 oip->ip_off = htons(ip->ip_off);
1436 ip->ip_src.s_addr = dst4.s_addr;
1437 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1442 bcopy((char *)oip + ohlen,
1443 (char *)&icmp->icmp_ip + ohlen, avail);
1444 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1447 ip->ip_p = IPPROTO_ICMP;
1450 shlen = fin->fin_hlen;
1451 fin->fin_hlen = hlen;
1452 err = send_ip(oip, fin, &m);
1453 fin->fin_hlen = shlen;
1455 if (fin->fin_v == 4)
1465 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1466 (!defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1468 int iplinit __P((void));
1472 void iplinit __P((void));
1479 # if defined(__NetBSD__)
1480 if (ipl_enable() != 0)
1482 if (iplattach() != 0)
1485 printf("IP Filter failed to attach\n");
1489 # endif /* ! __NetBSD__ */
1493 * Return the length of the entire mbuf.
1495 size_t mbufchainlen(m0)
1496 register struct mbuf *m0;
1499 return m0->m_pkthdr.len;
1501 register size_t len = 0;
1503 for (; m0; m0 = m0->m_next)
1510 int ipfr_fastroute(m0, mpp, fin, fdp)
1511 struct mbuf *m0, **mpp;
1515 register struct ip *ip, *mhip;
1516 register struct mbuf *m = m0;
1517 register struct route *ro;
1518 int len, off, error = 0, hlen, code;
1519 struct ifnet *ifp, *sifp;
1520 struct sockaddr_in *dst;
1521 struct route iproute;
1531 if (fin->fin_v == 6) {
1532 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1538 if (fin->fin_v == 6)
1546 * If the mbuf we're about to send is not writable (because of
1547 * a cluster reference, for example) we'll need to make a copy
1548 * of it since this routine modifies the contents.
1550 * If you have non-crappy network hardware that can transmit data
1551 * from the mbuf, rather than making a copy, this is gonna be a
1554 if (M_WRITABLE(m) == 0) {
1555 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1567 hlen = fin->fin_hlen;
1568 ip = mtod(m0, struct ip *);
1570 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1572 * Clear any in-bound checksum flags for this packet.
1574 # if (__NetBSD_Version__ > 105009999)
1575 m0->m_pkthdr.csum_flags = 0;
1577 m0->m_pkthdr.csuminfo = 0;
1579 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1587 bzero((caddr_t)ro, sizeof (*ro));
1588 dst = (struct sockaddr_in *)&ro->ro_dst;
1589 dst->sin_family = AF_INET;
1590 dst->sin_addr = ip->ip_dst;
1599 * In case we're here due to "to <if>" being used with "keep state",
1600 * check that we're going in the correct direction.
1602 if ((fr != NULL) && (fin->fin_rev != 0)) {
1603 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1605 } else if (fdp != NULL) {
1606 if (fdp->fd_ip.s_addr != 0)
1607 dst->sin_addr = fdp->fd_ip;
1611 dst->sin_len = sizeof(*dst);
1613 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1614 !defined(__OpenBSD__)
1616 rtalloc_ign(ro, RTF_CLONING);
1618 rtalloc_ign(ro, RTF_PRCLONING);
1629 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1635 if ((ifp == NULL) && (ro->ro_rt != NULL))
1636 ifp = ro->ro_rt->rt_ifp;
1638 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1639 if (in_localaddr(ip->ip_dst))
1640 error = EHOSTUNREACH;
1642 error = ENETUNREACH;
1646 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1648 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1650 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1653 ro->ro_rt->rt_use++;
1656 * For input packets which are being "fastrouted", they won't
1657 * go back through output filtering and miss their chance to get
1658 * NAT'd and counted.
1660 if (fin->fin_out == 0) {
1661 sifp = fin->fin_ifp;
1664 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1665 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1666 ATOMIC_INCL(frstats[1].fr_acct);
1669 if (!fr || !(fr->fr_flags & FR_RETMASK))
1670 (void) fr_checkstate(ip, fin);
1671 (void) ip_natout(ip, fin);
1672 fin->fin_ifp = sifp;
1676 * If small enough for interface, can just send directly.
1678 if (ip->ip_len <= ifp->if_mtu) {
1680 # if (!defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510))
1681 ip->ip_id = htons(ip->ip_id);
1683 ip->ip_len = htons(ip->ip_len);
1684 ip->ip_off = htons(ip->ip_off);
1686 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1687 # if (__NetBSD_Version__ > 105009999)
1688 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1689 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1690 else if (ip->ip_sum == 0)
1691 ip->ip_sum = in_cksum(m, hlen);
1693 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1694 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1695 else if (ip->ip_sum == 0)
1696 ip->ip_sum = in_cksum(m, hlen);
1700 ip->ip_sum = in_cksum(m, hlen);
1701 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1702 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1703 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1706 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1712 * Too large for interface; fragment if possible.
1713 * Must be able to put at least 8 bytes per fragment.
1715 if (ip->ip_off & IP_DF) {
1719 len = (ifp->if_mtu - hlen) &~ 7;
1726 int mhlen, firstlen = len;
1727 struct mbuf **mnext = &m->m_act;
1730 * Loop through length of segment after first fragment,
1731 * make new header and copy data of each part and link onto chain.
1734 mhlen = sizeof (struct ip);
1735 for (off = hlen + len; off < ip->ip_len; off += len) {
1737 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1739 MGET(m, M_DONTWAIT, MT_HEADER);
1746 m->m_data += max_linkhdr;
1748 m->m_off = MMAXOFF - hlen;
1750 mhip = mtod(m, struct ip *);
1751 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1752 if (hlen > sizeof (struct ip)) {
1753 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1754 mhip->ip_hl = mhlen >> 2;
1757 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1758 if (ip->ip_off & IP_MF)
1759 mhip->ip_off |= IP_MF;
1760 if (off + len >= ip->ip_len)
1761 len = ip->ip_len - off;
1763 mhip->ip_off |= IP_MF;
1764 mhip->ip_len = htons((u_short)(len + mhlen));
1765 m->m_next = m_copy(m0, off, len);
1766 if (m->m_next == 0) {
1767 error = ENOBUFS; /* ??? */
1771 m->m_pkthdr.len = mhlen + len;
1772 m->m_pkthdr.rcvif = NULL;
1774 mhip->ip_off = htons((u_short)mhip->ip_off);
1776 mhip->ip_sum = in_cksum(m, mhlen);
1781 * Update first fragment by trimming what's been copied out
1782 * and updating header, then send each fragment (in order).
1784 m_adj(m0, hlen + firstlen - ip->ip_len);
1785 ip->ip_len = htons((u_short)(hlen + firstlen));
1786 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1788 ip->ip_sum = in_cksum(m0, hlen);
1790 for (m = m0; m; m = m0) {
1794 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1795 error = (*ifp->if_output)(ifp, m,
1796 (struct sockaddr *)dst, ro->ro_rt);
1798 error = (*ifp->if_output)(ifp, m,
1799 (struct sockaddr *)dst);
1811 if (ro->ro_rt != NULL) {
1817 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1818 sifp = fin->fin_ifp;
1819 code = fin->fin_icode;
1820 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1822 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1823 fin->fin_ifp = sifp;
1824 fin->fin_icode = code;
1832 * Return true or false depending on whether the route to the
1833 * given IP address uses the same interface as the one passed.
1835 int fr_verifysrc(ipa, ifp)
1839 struct sockaddr_in *dst;
1840 struct route iproute;
1842 bzero((char *)&iproute, sizeof(iproute));
1843 dst = (struct sockaddr_in *)&iproute.ro_dst;
1844 # if (BSD >= 199306)
1845 dst->sin_len = sizeof(*dst);
1847 dst->sin_family = AF_INET;
1848 dst->sin_addr = ipa;
1849 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1850 !defined(__OpenBSD__)
1852 rtalloc_ign(&iproute, RTF_CLONING);
1854 rtalloc_ign(&iproute, RTF_PRCLONING);
1859 if (iproute.ro_rt == NULL)
1861 return (ifp == iproute.ro_rt->rt_ifp);
1865 # ifdef USE_GETIFNAME
1870 static char workbuf[64];
1872 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1878 # if defined(USE_INET6)
1880 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1881 * or ensure that it is an IPv6 packet that is being forwarded, those are
1882 * expected to be done by the called (ipfr_fastroute).
1884 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1885 struct mbuf *m0, **mpp;
1889 struct route_in6 ip6route;
1890 struct sockaddr_in6 *dst6;
1891 struct route_in6 *ro;
1899 bzero((caddr_t)ro, sizeof(*ro));
1900 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
1901 dst6->sin6_family = AF_INET6;
1902 dst6->sin6_len = sizeof(struct sockaddr_in6);
1903 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
1908 if ((fr != NULL) && (fin->fin_rev != 0)) {
1909 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1911 } else if (fdp != NULL) {
1912 if (IP6_NOTZERO(&fdp->fd_ip6))
1913 dst6->sin6_addr = fdp->fd_ip6.in6;
1915 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
1918 rtalloc((struct route *)ro);
1920 if ((ifp == NULL) && (ro->ro_rt != NULL))
1921 ifp = ro->ro_rt->rt_ifp;
1923 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
1924 (ifp != ro->ro_rt->rt_ifp)) {
1925 error = EHOSTUNREACH;
1927 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
1928 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
1929 ro->ro_rt->rt_use++;
1931 if (m0->m_pkthdr.len <= nd_ifinfo[ifp->if_index].linkmtu)
1932 error = nd6_output(ifp, fin->fin_ifp, m0, dst6,
1938 if (ro->ro_rt != NULL) {
1944 #else /* #ifdef _KERNEL */
1947 # if defined(__sgi) && (IRIX < 605)
1948 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
1949 struct sockaddr *s))
1951 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
1952 struct sockaddr *s, struct rtentry *rt))
1960 # if defined(__sgi) && (IRIX < 605)
1961 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
1962 struct sockaddr *s))
1964 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
1965 struct sockaddr *s, struct rtentry *rt))
1968 ip_t *ip = (ip_t *)m;
1970 static int write_output(ifp, ip)
1978 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
1979 (defined(OpenBSD) && (OpenBSD >= 199603))
1980 sprintf(fname, "%s", ifp->if_xname);
1982 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
1984 fd = open(fname, O_WRONLY|O_APPEND);
1989 write(fd, (char *)ip, ntohs(ip->ip_len));
1995 char *get_ifname(ifp)
1998 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
1999 (defined(OpenBSD) && (OpenBSD >= 199603))
2000 return ifp->if_xname;
2002 static char fullifname[LIFNAMSIZ];
2004 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2010 struct ifnet *get_unit(ifname, v)
2014 struct ifnet *ifp, **ifa, **old_ifneta;
2016 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2017 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2018 (defined(OpenBSD) && (OpenBSD >= 199603))
2019 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2021 char fullname[LIFNAMSIZ];
2023 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2024 if (!strcmp(ifname, fullname))
2030 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2034 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2041 old_ifneta = ifneta;
2043 ifneta = (struct ifnet **)realloc(ifneta,
2044 (nifs + 1) * sizeof(*ifa));
2050 ifneta[nifs] = NULL;
2051 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2052 if (!ifneta[nifs - 1]) {
2057 ifp = ifneta[nifs - 1];
2059 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2060 (defined(OpenBSD) && (OpenBSD >= 199603))
2061 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2063 ifp->if_name = strdup(ifname);
2065 ifname = ifp->if_name;
2066 while (*ifname && !isdigit(*ifname))
2068 if (*ifname && isdigit(*ifname)) {
2069 ifp->if_unit = atoi(ifname);
2074 ifp->if_output = no_output;
2082 struct ifnet *ifp, **ifa;
2086 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2087 (defined(OpenBSD) && (OpenBSD >= 199603))
2088 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2089 ifp->if_output = write_output;
2090 sprintf(fname, "/tmp/%s", ifp->if_xname);
2091 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2099 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2100 ifp->if_output = write_output;
2101 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2102 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2112 int send_reset(ip, fin)
2116 verbose("- TCP RST sent\n");
2121 int send_icmp_err(ip, code, fin, dst)
2127 verbose("- ICMP UNREACHABLE sent\n");
2137 void m_copydata(m, off, len, cp)
2142 bcopy((char *)m + off, cp, len);
2146 int ipfuiomove(buf, len, rwflag, uio)
2151 int left, ioc, num, offset;
2155 if (rwflag == UIO_READ) {
2159 offset = uio->uio_offset;
2161 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2162 io = uio->uio_iov + ioc;
2166 start = (char *)io->iov_base + offset;
2167 if (start > (char *)io->iov_base + io->iov_len) {
2168 offset -= io->iov_len;
2172 bcopy(buf, start, num);
2173 uio->uio_resid -= num;
2174 uio->uio_offset += num;
2184 #endif /* _KERNEL */