2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
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.2 2003/06/17 04:28:20 dillon Exp $
12 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
15 #if defined(KERNEL) && !defined(_KERNEL)
18 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
19 (__FreeBSD_version >= 400000) && !defined(KLD_MODULE)
20 #include "opt_inet6.h"
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"
27 #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
28 # if !defined(_KERNEL) || defined(IPFILTER_LKM)
29 # include <osreldate.h>
32 #if defined(__sgi) && (IRIX > 602)
34 # include <sys/ptimers.h>
43 #include <sys/errno.h>
44 #include <sys/types.h>
46 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
47 # include <sys/fcntl.h>
48 # include <sys/filio.h>
50 # include <sys/ioctl.h>
54 # include <sys/systm.h>
57 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
58 # include <sys/dirent.h>
62 # include <sys/mbuf.h>
64 # include <sys/filio.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
73 #if __FreeBSD_version >= 300000
74 # include <net/if_var.h>
75 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
76 # include "opt_ipfilter.h"
80 #include <sys/debug.h>
81 # ifdef IFF_DRVRLOCK /* IRIX6 */
82 #include <sys/hashing.h>
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>
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>
101 #include "netinet/ip_compat.h"
103 # include <netinet/icmp6.h>
105 # include <netinet6/ip6protosw.h>
106 # include <netinet6/nd6.h>
109 #include "netinet/ip_fil.h"
110 #include "netinet/ip_nat.h"
111 #include "netinet/ip_frag.h"
112 #include "netinet/ip_state.h"
113 #include "netinet/ip_proxy.h"
114 #include "netinet/ip_auth.h"
115 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
116 # include <sys/malloc.h>
119 # define MIN(a,b) (((a)<(b))?(a):(b))
121 #if !SOLARIS && defined(_KERNEL) && !defined(__sgi)
122 # include <sys/kernel.h>
123 extern int ip_optcopy __P((struct ip *, struct ip *));
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 *);
130 #include <machine/in_cksum.h>
132 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
134 extern struct protosw inetsw[];
138 static struct ifnet **ifneta = NULL;
141 # if (BSD < 199306) || defined(__sgi)
146 #ifdef ICMP_UNREACH_FILTER_PROHIB
147 int ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
149 int ipl_unreach = ICMP_UNREACH_FILTER;
151 u_long ipl_frouteok[2] = {0, 0};
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));
157 static int frrequest __P((int, int, caddr_t, int));
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 **));
163 static int ipfr_fastroute6 __P((struct mbuf *, struct mbuf **,
164 fr_info_t *, frdest_t *));
167 extern int tcp_mtudisc;
168 extern kmutex_t ipf_rw;
169 extern KRWLOCK_T ipf_mutex;
172 void init_ifp __P((void));
173 # if defined(__sgi) && (IRIX < 605)
174 static int no_output __P((struct ifnet *, struct mbuf *,
176 static int write_output __P((struct ifnet *, struct mbuf *,
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 *));
187 #if (__FreeBSD_version >= 300000) && defined(_KERNEL)
188 struct callout_handle ipfr_slowtimer_ch;
190 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
191 # include <sys/callout.h>
192 struct callout ipfr_slowtimer_ch;
194 #if defined(__OpenBSD__)
195 # include <sys/timeout.h>
196 struct timeout ipfr_slowtimer_ch;
198 #if defined(__sgi) && defined(_KERNEL)
199 toid_t ipfr_slowtimer_ch;
202 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
204 # include <sys/conf.h>
205 const struct cdevsw ipl_cdevsw = {
206 iplopen, iplclose, iplread, nowrite, iplioctl,
207 nostop, notty, nopoll, nommap,
211 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
212 # include <sys/device.h>
213 # include <sys/conf.h>
215 struct cfdriver iplcd = {
216 NULL, "ipl", NULL, NULL, DV_DULL, 0
219 struct devsw iplsw = {
221 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
222 nostrat, nodump, nopsize, 0,
225 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
227 #if defined(__NetBSD__) || defined(__OpenBSD__) || (_BSDI_VERSION >= 199701)
228 # include <sys/conf.h>
229 # if defined(NETBSD_PF)
230 # include <net/pfil.h>
232 * We provide the fr_checkp name just to minimize changes later.
234 int (*fr_checkp) __P((ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
235 # endif /* NETBSD_PF */
236 #endif /* __NetBSD__ */
239 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
241 # include <net/pfil.h>
243 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
245 static int fr_check_wrapper(arg, mp, ifp, dir)
251 struct ip *ip = mtod(*mp, struct ip *);
252 int rv, hlen = ip->ip_hl << 2;
254 #if defined(M_CSUM_TCPv4)
256 * If the packet is out-bound, we can't delay checksums
257 * here. For in-bound, the checksum has already been
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);
267 #endif /* M_CSUM_TCPv4 */
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
275 * Note, we don't need to update the checksum, because
276 * it has already been verified.
281 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
283 if (rv == 0 && *mp != NULL) {
284 ip = mtod(*mp, struct ip *);
293 # include <netinet/ip6.h>
295 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
297 static int fr_check_wrapper6(arg, mp, ifp, dir)
304 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
305 ifp, (dir == PFIL_OUT), mp));
308 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
310 # if defined(IPFILTER_LKM) && !defined(__sgi)
314 if (strcmp(s, "ipl") == 0)
318 # endif /* IPFILTER_LKM */
322 * Try to detect the case when compiling for NetBSD with pseudo-device
324 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
326 ipfilterattach(count)
331 * Do nothing here, really. The filter will be enabled
332 * by the SIOCFRENB ioctl.
338 # if defined(__NetBSD__) || defined(__OpenBSD__)
346 # if defined(__sgi) || (defined(NETBSD_PF) && (__NetBSD_Version__ >= 104200000))
349 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
350 struct pfil_head *ph_inet;
352 struct pfil_head *ph_inet6;
357 if (fr_running || (fr_checkp == fr_check)) {
358 printf("IP Filter: already initialized\n");
366 if (nat_init() == -1) {
370 if (fr_stateinit() == -1) {
374 if (appr_init() == -1) {
380 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011)
381 # if __NetBSD_Version__ >= 105110000
382 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
384 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
394 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
395 PFIL_IN|PFIL_OUT, ph_inet);
399 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
400 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
414 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
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);
424 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
425 PFIL_IN|PFIL_OUT, ph_inet6);
427 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
428 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
430 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
431 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
444 error = ipfilter_sgi_attach();
454 bzero((char *)frcache, sizeof(frcache));
455 fr_savep = fr_checkp;
456 fr_checkp = fr_check;
460 if (fr_pass & FR_PASS)
462 else if (fr_pass & FR_BLOCK)
465 defpass = "no-match -> block";
467 printf("%s initialized. Default = %s all, Logging = %s\n",
468 ipfilter_version, defpass,
475 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
476 callout_init(&ipfr_slowtimer_ch);
477 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
479 # if defined(__OpenBSD__)
480 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
481 timeout_add(&ipfr_slowtimer_ch, hz/2);
483 # if (__FreeBSD_version >= 300000) || defined(__sgi)
484 ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
486 timeout(ipfr_slowtimer, NULL, hz/2);
496 * Disable the filter by removing the hooks from the IP input/output
499 # if defined(__NetBSD__)
506 #if defined(NETBSD_PF) && \
507 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
509 # if __NetBSD_Version__ >= 105150000
510 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
512 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
518 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
519 callout_stop(&ipfr_slowtimer_ch);
521 # if (__FreeBSD_version >= 300000)
522 untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
525 untimeout(ipfr_slowtimer_ch);
527 # if defined(__OpenBSD__)
528 timeout_del(&ipfr_slowtimer_ch);
530 untimeout(ipfr_slowtimer, NULL);
531 # endif /* OpenBSD */
533 # endif /* FreeBSD */
539 printf("IP Filter: not initialized\n");
544 printf("%s unloaded\n", ipfilter_version);
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);
552 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
553 # if __NetBSD_Version__ >= 105110000
555 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
556 PFIL_IN|PFIL_OUT, ph_inet);
560 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
561 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
568 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
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);
578 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
579 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
589 ipfilter_sgi_detach();
604 static int frzerostats(data)
611 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
615 bzero((char *)frstats, sizeof(*frstats) * 2);
622 * Filter ioctl interface.
625 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
627 , cred_t *cp, int *rp
631 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
632 # if (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
633 (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
634 defined(__OpenBSD__)))
641 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
642 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
651 #if defined(_KERNEL) && !SOLARIS
654 int error = 0, unit = 0, tmp;
656 #if (BSD >= 199306) && defined(_KERNEL)
657 if ((securelevel >= 3) && (mode & FWRITE))
661 unit = GET_MINOR(dev);
662 if ((IPL_LOGMAX < unit) || (unit < 0))
668 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
673 if (unit == IPL_LOGNAT) {
675 error = nat_ioctl(data, cmd, mode);
681 if (unit == IPL_LOGSTATE) {
683 error = fr_state_ioctl(data, cmd, mode);
689 if (unit == IPL_LOGAUTH) {
693 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
694 if (!(mode & FWRITE)) {
697 error = frrequest(unit, cmd, data,
701 error = fr_auth_ioctl(data, mode, cmd);
710 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
711 sizeof(iplused[IPL_LOGIPF]));
714 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
719 if (!(mode & FWRITE))
722 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
726 # if defined(__NetBSD__) || defined(__OpenBSD__)
727 error = ipl_enable();
732 # if defined(__NetBSD__)
733 error = ipl_disable();
742 if (!(mode & FWRITE))
745 error = IRCOPY(data, (caddr_t)&fr_flags,
749 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
755 if (!(mode & FWRITE))
758 error = frrequest(unit, cmd, data, fr_active);
763 if (!(mode & FWRITE))
766 error = frrequest(unit, cmd, data, 1 - fr_active);
769 if (!(mode & FWRITE))
772 bzero((char *)frcache, sizeof(frcache[0]) * 2);
773 *(u_int *)data = fr_active;
774 fr_active = 1 - fr_active;
782 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
788 if (!(mode & FWRITE))
791 error = frzerostats(data);
794 if (!(mode & FWRITE))
797 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
799 tmp = frflush(unit, 4, tmp);
800 error = IWCOPY((caddr_t)&tmp, data,
807 if (!(mode & FWRITE))
810 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
812 tmp = frflush(unit, 6, tmp);
813 error = IWCOPY((caddr_t)&tmp, data,
820 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
831 if (!(mode & FWRITE))
834 *(int *)data = ipflog_clear(unit);
836 #endif /* IPFILTER_LOG */
838 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
844 if (!(mode & FWRITE))
847 #if defined(_KERNEL) && defined(__sgi)
862 void fr_forgetifp(ifp)
865 register frentry_t *f;
867 WRITE_ENTER(&ipf_mutex);
868 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
869 if (f->fr_ifa == ifp)
870 f->fr_ifa = (void *)-1;
871 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
872 if (f->fr_ifa == ifp)
873 f->fr_ifa = (void *)-1;
874 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
875 if (f->fr_ifa == ifp)
876 f->fr_ifa = (void *)-1;
877 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
878 if (f->fr_ifa == ifp)
879 f->fr_ifa = (void *)-1;
881 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
882 if (f->fr_ifa == ifp)
883 f->fr_ifa = (void *)-1;
884 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
885 if (f->fr_ifa == ifp)
886 f->fr_ifa = (void *)-1;
887 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
888 if (f->fr_ifa == ifp)
889 f->fr_ifa = (void *)-1;
890 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
891 if (f->fr_ifa == ifp)
892 f->fr_ifa = (void *)-1;
894 RWLOCK_EXIT(&ipf_mutex);
899 static int frrequest(unit, req, data, set)
901 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
909 register frentry_t *fp, *f, **fprev;
910 register frentry_t **ftail;
911 frgroup_t *fg = NULL;
912 int error = 0, in, i;
919 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
923 #if (BSD >= 199306) && defined(_KERNEL)
924 if ((securelevel > 0) && (fp->fr_func != NULL))
929 * Check that the group number does exist and that if a head group
930 * has been specified, doesn't exist.
932 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
933 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
934 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
936 if ((req != SIOCZRLST) && fp->fr_group &&
937 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
940 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
942 if (unit == IPL_LOGAUTH)
943 ftail = fprev = &ipauth;
944 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
945 ftail = fprev = &ipacct[in][set];
946 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
947 ftail = fprev = &ipfilter[in][set];
949 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
950 ftail = fprev = &ipacct6[in][set];
951 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
952 ftail = fprev = &ipfilter6[in][set];
957 if ((group = fp->fr_group)) {
958 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
960 ftail = fprev = fg->fg_start;
963 bzero((char *)frcache, sizeof(frcache[0]) * 2);
965 for (i = 0; i < 4; i++) {
966 if ((fp->fr_ifnames[i][1] == '\0') &&
967 ((fp->fr_ifnames[i][0] == '-') ||
968 (fp->fr_ifnames[i][0] == '*'))) {
969 fp->fr_ifas[i] = NULL;
970 } else if (*fp->fr_ifnames[i]) {
971 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
973 fp->fr_ifas[i] = (void *)-1;
978 fp->fr_flags &= ~FR_DUP;
979 if (*fdp->fd_ifname) {
980 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
982 fdp->fd_ifp = (struct ifnet *)-1;
984 fp->fr_flags |= FR_DUP;
988 if (*fdp->fd_ifname) {
989 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
991 fdp->fd_ifp = (struct ifnet *)-1;
995 * Look for a matching filter rule, but don't include the next or
996 * interface pointer in the comparison (fr_next, fr_ifa).
998 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1002 for (; (f = *ftail); ftail = &f->fr_next)
1003 if ((fp->fr_cksum == f->fr_cksum) &&
1004 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1008 * If zero'ing statistics, copy current to caller and zero.
1010 if (req == SIOCZRLST) {
1013 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1022 if (req != SIOCINAFR && req != SIOCINIFR)
1023 while ((f = *ftail))
1024 ftail = &f->fr_next;
1028 while (--fp->fr_hits && (f = *ftail))
1029 ftail = &f->fr_next;
1035 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1040 * Only return EBUSY if there is a group list, else
1041 * it's probably just state information referencing
1044 if ((f->fr_ref > 1) && f->fr_grp)
1046 if (fg && fg->fg_head)
1047 fg->fg_head->fr_ref--;
1048 if (unit == IPL_LOGAUTH) {
1049 return fr_preauthcmd(req, f, ftail);
1052 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1054 fixskip(fprev, f, -1);
1055 *ftail = f->fr_next;
1065 if (unit == IPL_LOGAUTH) {
1066 return fr_preauthcmd(req, fp, ftail);
1068 KMALLOC(f, frentry_t *);
1070 if (fg && fg->fg_head)
1071 fg->fg_head->fr_ref++;
1072 bcopy((char *)fp, (char *)f, sizeof(*f));
1075 f->fr_next = *ftail;
1077 if (req == SIOCINIFR || req == SIOCINAFR)
1078 fixskip(fprev, f, 1);
1080 if ((group = f->fr_grhead))
1081 fg = fr_addgroup(group, f, unit, set);
1092 * routines below for saving IP headers to buffer
1096 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1098 int IPL_EXTERN(open)(dev_t dev, int flags)
1101 int IPL_EXTERN(open)(dev, flags
1102 # if ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1103 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1114 # if defined(__sgi) && defined(_KERNEL)
1115 u_int min = geteminor(*pdev);
1117 u_int min = GET_MINOR(dev);
1120 if (IPL_LOGMAX < min)
1129 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1131 int IPL_EXTERN(close)(dev, flags
1132 # if ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1133 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1144 u_int min = GET_MINOR(dev);
1146 if (IPL_LOGMAX < min)
1155 * both of these must operate with at least splnet() lest they be
1156 * called during packet processing and cause an inconsistancy to appear in
1160 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1163 int IPL_EXTERN(read)(dev, uio, ioflag)
1166 int IPL_EXTERN(read)(dev, uio)
1169 register struct uio *uio;
1172 # ifdef IPFILTER_LOG
1173 return ipflog_read(GET_MINOR(dev), uio);
1181 * send_reset - this could conceivably be a call to tcp_respond(), but that
1182 * requires a large amount of setting up and isn't any more efficient.
1184 int send_reset(oip, fin)
1188 struct tcphdr *tcp, *tcp2;
1192 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1196 tcp = (struct tcphdr *)fin->fin_dp;
1197 if (tcp->th_flags & TH_RST)
1198 return -1; /* feedback loop */
1199 # if (BSD < 199306) || defined(__sgi)
1200 m = m_get(M_DONTWAIT, MT_HEADER);
1202 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1209 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1210 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1211 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1214 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1216 hlen = sizeof(ip_t);
1218 m->m_len = sizeof(*tcp2) + hlen;
1220 m->m_data += max_linkhdr;
1221 m->m_pkthdr.len = m->m_len;
1222 m->m_pkthdr.rcvif = (struct ifnet *)0;
1224 ip = mtod(m, struct ip *);
1228 bzero((char *)ip, sizeof(*tcp2) + hlen);
1229 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1231 tcp2->th_sport = tcp->th_dport;
1232 tcp2->th_dport = tcp->th_sport;
1233 if (tcp->th_flags & TH_ACK) {
1234 tcp2->th_seq = tcp->th_ack;
1235 tcp2->th_flags = TH_RST;
1237 tcp2->th_ack = ntohl(tcp->th_seq);
1238 tcp2->th_ack += tlen;
1239 tcp2->th_ack = htonl(tcp2->th_ack);
1240 tcp2->th_flags = TH_RST|TH_ACK;
1242 tcp2->th_off = sizeof(*tcp2) >> 2;
1244 if (fin->fin_v == 6) {
1245 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1246 ip6->ip6_nxt = IPPROTO_TCP;
1247 ip6->ip6_src = oip6->ip6_dst;
1248 ip6->ip6_dst = oip6->ip6_src;
1249 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1250 sizeof(*ip6), sizeof(*tcp2));
1251 return send_ip(oip, fin, &m);
1254 ip->ip_p = IPPROTO_TCP;
1255 ip->ip_len = htons(sizeof(struct tcphdr));
1256 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1257 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1258 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1259 ip->ip_len = hlen + sizeof(*tcp2);
1260 return send_ip(oip, fin, &m);
1265 * Send an IP(v4/v6) datagram out into the network
1267 static int send_ip(oip, fin, mp)
1272 struct mbuf *m = *mp;
1277 bzero((char *)&frn, sizeof(frn));
1278 frn.fin_ifp = fin->fin_ifp;
1279 frn.fin_v = fin->fin_v;
1280 frn.fin_out = fin->fin_out;
1281 frn.fin_mp = fin->fin_mp;
1283 ip = mtod(m, ip_t *);
1286 ip->ip_v = fin->fin_v;
1287 if (ip->ip_v == 4) {
1288 ip->ip_hl = (sizeof(*oip) >> 2);
1289 ip->ip_v = IPVERSION;
1290 ip->ip_tos = oip->ip_tos;
1291 ip->ip_id = oip->ip_id;
1293 # if defined(__NetBSD__) || \
1294 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1295 if (ip_mtudisc != 0)
1299 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1304 # if (BSD < 199306) || defined(__sgi)
1305 ip->ip_ttl = tcp_ttl;
1307 ip->ip_ttl = ip_defttl;
1310 frn.fin_dp = (char *)(ip + 1);
1313 else if (ip->ip_v == 6) {
1314 ip6_t *ip6 = (ip6_t *)ip;
1316 hlen = sizeof(*ip6);
1317 ip6->ip6_hlim = 127;
1318 frn.fin_dp = (char *)(ip6 + 1);
1322 m->m_pkthdr.rcvif = NULL;
1325 fr_makefrip(hlen, ip, &frn);
1327 error = ipfr_fastroute(m, mp, &frn, NULL);
1332 int send_icmp_err(oip, type, fin, dst)
1338 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1339 u_short shlen, slen = 0, soff = 0;
1340 struct in_addr dst4;
1345 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1346 struct in6_addr dst6;
1350 if ((type < 0) || (type > ICMP_MAXTYPE))
1353 code = fin->fin_icode;
1355 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1362 if (fin->fin_v == 4) {
1363 if ((oip->ip_p == IPPROTO_ICMP) &&
1364 !(fin->fin_fi.fi_fl & FI_SHORT))
1365 switch (ntohs(fin->fin_data[0]) >> 8)
1376 # if (BSD < 199306) || defined(__sgi)
1378 m = m_get(M_DONTWAIT, MT_HEADER);
1381 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1387 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1390 dst4.s_addr = oip->ip_dst.s_addr;
1392 hlen = sizeof(ip_t);
1393 ohlen = oip->ip_hl << 2;
1398 else if (fin->fin_v == 6) {
1399 hlen = sizeof(ip6_t);
1400 ohlen = sizeof(ip6_t);
1401 type = icmptoicmp6types[type];
1402 if (type == ICMP6_DST_UNREACH)
1403 code = icmptoicmp6unreach[code];
1405 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1409 MCLGET(m, M_DONTWAIT);
1410 if ((m->m_flags & M_EXT) == 0) {
1414 # ifdef M_TRAILINGSPACE
1416 avail = M_TRAILINGSPACE(m);
1418 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1420 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1421 avail - hlen - sizeof(*icmp) - max_linkhdr);
1423 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1426 dst6 = oip6->ip6_dst;
1430 iclen = hlen + sizeof(*icmp);
1432 avail -= (max_linkhdr + iclen);
1433 m->m_data += max_linkhdr;
1434 m->m_pkthdr.rcvif = (struct ifnet *)0;
1438 m->m_pkthdr.len = iclen;
1440 avail -= (m->m_off + iclen);
1446 ip = mtod(m, ip_t *);
1447 icmp = (struct icmp *)((char *)ip + hlen);
1448 bzero((char *)ip, iclen);
1450 icmp->icmp_type = type;
1451 icmp->icmp_code = fin->fin_icode;
1452 icmp->icmp_cksum = 0;
1454 if (type == ICMP_UNREACH &&
1455 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1456 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1460 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1461 avail -= MIN(ohlen, avail);
1466 if (fin->fin_v == 6) {
1468 ip6->ip6_plen = htons(iclen - hlen);
1469 ip6->ip6_nxt = IPPROTO_ICMPV6;
1471 ip6->ip6_src = dst6;
1472 ip6->ip6_dst = oip6->ip6_src;
1474 bcopy((char *)oip + ohlen,
1475 (char *)&icmp->icmp_ip + ohlen, avail);
1476 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1477 sizeof(*ip6), iclen - hlen);
1482 oip->ip_len = htons(oip->ip_len);
1484 oip->ip_off = htons(ip->ip_off);
1486 ip->ip_src.s_addr = dst4.s_addr;
1487 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1492 bcopy((char *)oip + ohlen,
1493 (char *)&icmp->icmp_ip + ohlen, avail);
1494 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1497 ip->ip_p = IPPROTO_ICMP;
1500 shlen = fin->fin_hlen;
1501 fin->fin_hlen = hlen;
1502 err = send_ip(oip, fin, &m);
1503 fin->fin_hlen = shlen;
1505 if (fin->fin_v == 4)
1515 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1516 (!defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1518 int iplinit __P((void));
1522 void iplinit __P((void));
1529 # if defined(__NetBSD__) || defined(__OpenBSD__)
1530 if (ipl_enable() != 0)
1532 if (iplattach() != 0)
1535 printf("IP Filter failed to attach\n");
1539 # endif /* ! __NetBSD__ */
1543 * Return the length of the entire mbuf.
1545 size_t mbufchainlen(m0)
1546 register struct mbuf *m0;
1549 return m0->m_pkthdr.len;
1551 register size_t len = 0;
1553 for (; m0; m0 = m0->m_next)
1560 int ipfr_fastroute(m0, mpp, fin, fdp)
1561 struct mbuf *m0, **mpp;
1565 register struct ip *ip, *mhip;
1566 register struct mbuf *m = m0;
1567 register struct route *ro;
1568 int len, off, error = 0, hlen, code;
1569 struct ifnet *ifp, *sifp;
1570 struct sockaddr_in *dst;
1571 struct route iproute;
1581 if (fin->fin_v == 6) {
1582 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1588 if (fin->fin_v == 6)
1596 * If the mbuf we're about to send is not writable (because of
1597 * a cluster reference, for example) we'll need to make a copy
1598 * of it since this routine modifies the contents.
1600 * If you have non-crappy network hardware that can transmit data
1601 * from the mbuf, rather than making a copy, this is gonna be a
1604 if (M_WRITABLE(m) == 0) {
1605 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1617 hlen = fin->fin_hlen;
1618 ip = mtod(m0, struct ip *);
1620 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1622 * Clear any in-bound checksum flags for this packet.
1624 # if (__NetBSD_Version__ > 105009999)
1625 m0->m_pkthdr.csum_flags = 0;
1627 m0->m_pkthdr.csuminfo = 0;
1629 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1634 #if defined(__sgi) && (IRIX >= 605)
1637 bzero((caddr_t)ro, sizeof (*ro));
1638 dst = (struct sockaddr_in *)&ro->ro_dst;
1639 dst->sin_family = AF_INET;
1640 dst->sin_addr = ip->ip_dst;
1649 * In case we're here due to "to <if>" being used with "keep state",
1650 * check that we're going in the correct direction.
1652 if ((fr != NULL) && (fin->fin_rev != 0)) {
1653 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1655 } else if (fdp != NULL) {
1656 if (fdp->fd_ip.s_addr != 0)
1657 dst->sin_addr = fdp->fd_ip;
1661 dst->sin_len = sizeof(*dst);
1663 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1664 !defined(__OpenBSD__)
1666 rtalloc_ign(ro, RTF_CLONING);
1668 rtalloc_ign(ro, RTF_PRCLONING);
1674 #if defined(__sgi) && (IRIX > 602)
1679 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1685 if ((ifp == NULL) && (ro->ro_rt != NULL))
1686 ifp = ro->ro_rt->rt_ifp;
1688 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1689 if (in_localaddr(ip->ip_dst))
1690 error = EHOSTUNREACH;
1692 error = ENETUNREACH;
1696 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1698 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1700 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1703 ro->ro_rt->rt_use++;
1706 * For input packets which are being "fastrouted", they won't
1707 * go back through output filtering and miss their chance to get
1708 * NAT'd and counted.
1710 if (fin->fin_out == 0) {
1711 sifp = fin->fin_ifp;
1714 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1715 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1716 ATOMIC_INCL(frstats[1].fr_acct);
1719 if (!fr || !(fr->fr_flags & FR_RETMASK))
1720 (void) fr_checkstate(ip, fin);
1721 (void) ip_natout(ip, fin);
1722 fin->fin_ifp = sifp;
1726 * If small enough for interface, can just send directly.
1728 if (ip->ip_len <= ifp->if_mtu) {
1730 # if (!defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1731 !(__NetBSD_Version__ >= 105110000)
1732 ip->ip_id = htons(ip->ip_id);
1734 ip->ip_len = htons(ip->ip_len);
1735 ip->ip_off = htons(ip->ip_off);
1737 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1738 # if (__NetBSD_Version__ > 105009999)
1739 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1740 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1741 else if (ip->ip_sum == 0)
1742 ip->ip_sum = in_cksum(m, hlen);
1744 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1745 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1746 else if (ip->ip_sum == 0)
1747 ip->ip_sum = in_cksum(m, hlen);
1751 ip->ip_sum = in_cksum(m, hlen);
1752 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1753 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1754 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1757 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1763 * Too large for interface; fragment if possible.
1764 * Must be able to put at least 8 bytes per fragment.
1766 if (ip->ip_off & IP_DF) {
1770 len = (ifp->if_mtu - hlen) &~ 7;
1777 int mhlen, firstlen = len;
1778 struct mbuf **mnext = &m->m_act;
1781 * Loop through length of segment after first fragment,
1782 * make new header and copy data of each part and link onto chain.
1785 mhlen = sizeof (struct ip);
1786 for (off = hlen + len; off < ip->ip_len; off += len) {
1788 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1790 MGET(m, M_DONTWAIT, MT_HEADER);
1797 m->m_data += max_linkhdr;
1799 m->m_off = MMAXOFF - hlen;
1801 mhip = mtod(m, struct ip *);
1802 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1803 if (hlen > sizeof (struct ip)) {
1804 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1805 mhip->ip_hl = mhlen >> 2;
1808 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1809 if (ip->ip_off & IP_MF)
1810 mhip->ip_off |= IP_MF;
1811 if (off + len >= ip->ip_len)
1812 len = ip->ip_len - off;
1814 mhip->ip_off |= IP_MF;
1815 mhip->ip_len = htons((u_short)(len + mhlen));
1816 m->m_next = m_copy(m0, off, len);
1817 if (m->m_next == 0) {
1818 error = ENOBUFS; /* ??? */
1822 m->m_pkthdr.len = mhlen + len;
1823 m->m_pkthdr.rcvif = NULL;
1825 mhip->ip_off = htons((u_short)mhip->ip_off);
1827 mhip->ip_sum = in_cksum(m, mhlen);
1832 * Update first fragment by trimming what's been copied out
1833 * and updating header, then send each fragment (in order).
1835 m_adj(m0, hlen + firstlen - ip->ip_len);
1836 ip->ip_len = htons((u_short)(hlen + firstlen));
1837 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1839 ip->ip_sum = in_cksum(m0, hlen);
1841 for (m = m0; m; m = m0) {
1845 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1846 error = (*ifp->if_output)(ifp, m,
1847 (struct sockaddr *)dst, ro->ro_rt);
1849 error = (*ifp->if_output)(ifp, m,
1850 (struct sockaddr *)dst);
1862 if (ro->ro_rt != NULL) {
1868 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1869 sifp = fin->fin_ifp;
1870 code = fin->fin_icode;
1871 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1873 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1874 fin->fin_ifp = sifp;
1875 fin->fin_icode = code;
1883 * Return true or false depending on whether the route to the
1884 * given IP address uses the same interface as the one passed.
1886 int fr_verifysrc(ipa, ifp)
1890 struct sockaddr_in *dst;
1891 struct route iproute;
1893 bzero((char *)&iproute, sizeof(iproute));
1894 dst = (struct sockaddr_in *)&iproute.ro_dst;
1895 # if (BSD >= 199306)
1896 dst->sin_len = sizeof(*dst);
1898 dst->sin_family = AF_INET;
1899 dst->sin_addr = ipa;
1900 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1901 !defined(__OpenBSD__)
1903 rtalloc_ign(&iproute, RTF_CLONING);
1905 rtalloc_ign(&iproute, RTF_PRCLONING);
1910 if (iproute.ro_rt == NULL)
1912 return (ifp == iproute.ro_rt->rt_ifp);
1916 # ifdef USE_GETIFNAME
1921 static char workbuf[64];
1923 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1929 # if defined(USE_INET6)
1931 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1932 * or ensure that it is an IPv6 packet that is being forwarded, those are
1933 * expected to be done by the called (ipfr_fastroute).
1935 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1936 struct mbuf *m0, **mpp;
1940 struct route_in6 ip6route;
1941 struct sockaddr_in6 *dst6;
1942 struct route_in6 *ro;
1945 #if defined(OpenBSD) && (OpenBSD >= 200211)
1946 struct route_in6 *ro_pmtu = NULL;
1947 struct in6_addr finaldst;
1956 bzero((caddr_t)ro, sizeof(*ro));
1957 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
1958 dst6->sin6_family = AF_INET6;
1959 dst6->sin6_len = sizeof(struct sockaddr_in6);
1960 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
1965 if ((fr != NULL) && (fin->fin_rev != 0)) {
1966 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1968 } else if (fdp != NULL) {
1969 if (IP6_NOTZERO(&fdp->fd_ip6))
1970 dst6->sin6_addr = fdp->fd_ip6.in6;
1972 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
1975 rtalloc((struct route *)ro);
1977 if ((ifp == NULL) && (ro->ro_rt != NULL))
1978 ifp = ro->ro_rt->rt_ifp;
1980 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
1981 (ifp != ro->ro_rt->rt_ifp)) {
1982 error = EHOSTUNREACH;
1984 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
1985 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
1986 ro->ro_rt->rt_use++;
1988 #if defined(OpenBSD) && (OpenBSD >= 200211)
1989 ip6 = mtod(m0, ip6_t *);
1991 finaldst = ip6->ip6_dst;
1992 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
1995 mtu = nd_ifinfo[ifp->if_index].linkmtu;
1997 if (m0->m_pkthdr.len <= mtu)
1998 error = nd6_output(ifp, fin->fin_ifp, m0,
2002 #if defined(OpenBSD) && (OpenBSD >= 200211)
2007 if (ro->ro_rt != NULL) {
2013 #else /* #ifdef _KERNEL */
2016 # if defined(__sgi) && (IRIX < 605)
2017 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2018 struct sockaddr *s))
2020 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2021 struct sockaddr *s, struct rtentry *rt))
2029 # if defined(__sgi) && (IRIX < 605)
2030 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2031 struct sockaddr *s))
2033 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2034 struct sockaddr *s, struct rtentry *rt))
2037 ip_t *ip = (ip_t *)m;
2039 static int write_output(ifp, ip)
2047 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2048 (defined(OpenBSD) && (OpenBSD >= 199603))
2049 sprintf(fname, "%s", ifp->if_xname);
2051 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2053 fd = open(fname, O_WRONLY|O_APPEND);
2058 write(fd, (char *)ip, ntohs(ip->ip_len));
2064 char *get_ifname(ifp)
2067 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2068 (defined(OpenBSD) && (OpenBSD >= 199603))
2069 return ifp->if_xname;
2071 static char fullifname[LIFNAMSIZ];
2073 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2079 struct ifnet *get_unit(ifname, v)
2083 struct ifnet *ifp, **ifa, **old_ifneta;
2085 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2086 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2087 (defined(OpenBSD) && (OpenBSD >= 199603))
2088 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2090 char fullname[LIFNAMSIZ];
2092 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2093 if (!strcmp(ifname, fullname))
2099 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2103 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2110 old_ifneta = ifneta;
2112 ifneta = (struct ifnet **)realloc(ifneta,
2113 (nifs + 1) * sizeof(*ifa));
2119 ifneta[nifs] = NULL;
2120 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2121 if (!ifneta[nifs - 1]) {
2126 ifp = ifneta[nifs - 1];
2128 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2129 (defined(OpenBSD) && (OpenBSD >= 199603))
2130 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2132 ifp->if_name = strdup(ifname);
2134 ifname = ifp->if_name;
2135 while (*ifname && !isdigit(*ifname))
2137 if (*ifname && isdigit(*ifname)) {
2138 ifp->if_unit = atoi(ifname);
2143 ifp->if_output = no_output;
2151 struct ifnet *ifp, **ifa;
2155 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2156 (defined(OpenBSD) && (OpenBSD >= 199603))
2157 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2158 ifp->if_output = write_output;
2159 sprintf(fname, "/tmp/%s", ifp->if_xname);
2160 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2168 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2169 ifp->if_output = write_output;
2170 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2171 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2181 int send_reset(ip, fin)
2185 verbose("- TCP RST sent\n");
2190 int send_icmp_err(ip, code, fin, dst)
2196 verbose("- ICMP UNREACHABLE sent\n");
2206 void m_copydata(m, off, len, cp)
2211 bcopy((char *)m + off, cp, len);
2215 int ipfuiomove(buf, len, rwflag, uio)
2220 int left, ioc, num, offset;
2224 if (rwflag == UIO_READ) {
2228 offset = uio->uio_offset;
2230 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2231 io = uio->uio_iov + ioc;
2235 start = (char *)io->iov_base + offset;
2236 if (start > (char *)io->iov_base + io->iov_len) {
2237 offset -= io->iov_len;
2241 bcopy(buf, start, num);
2242 uio->uio_resid -= num;
2243 uio->uio_offset += num;
2253 #endif /* _KERNEL */