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.12 2004/06/24 08:15:14 dillon Exp $
12 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
15 #if defined(KERNEL) && !defined(_KERNEL)
18 #if defined(_KERNEL) && (defined(__DragonFly__) || (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 (defined(__DragonFly__) || __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 defined(__DragonFly__) || (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 defined(__DragonFly__) || __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 "ip_compat.h"
103 # include <netinet/icmp6.h>
105 # include <netinet6/ip6protosw.h>
106 # include <netinet6/nd6.h>
112 #include "ip_state.h"
113 #include "ip_proxy.h"
115 #if defined(__DragonFly__) || (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 (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 <sys/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 (caddr_t);
154 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
155 static int frrequest (int, u_long, caddr_t, int);
157 static int frrequest (int, int, caddr_t, int);
160 static int (*fr_savep) (ip_t *, int, void *, int, struct mbuf **);
161 static int send_ip (ip_t *, fr_info_t *, struct mbuf **);
163 static int ipfr_fastroute6 (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 (void);
173 # if defined(__sgi) && (IRIX < 605)
174 static int no_output (struct ifnet *, struct mbuf *,
176 static int write_output (struct ifnet *, struct mbuf *,
179 static int no_output (struct ifnet *, struct mbuf *,
180 struct sockaddr *, struct rtentry *);
181 static int write_output (struct ifnet *, struct mbuf *,
182 struct sockaddr *, struct rtentry *);
187 #if (defined(__DragonFly__) || __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__) || \
228 (_BSDI_VERSION >= 199701) || (defined(__DragonFly_version) && \
229 (__DragonFly_version >= 100000))
230 # include <sys/conf.h>
231 # if defined(NETBSD_PF)
232 # include <net/pfil.h>
234 * We provide the fr_checkp name just to minimize changes later.
236 int (*fr_checkp) (ip_t *ip, int hlen, void *ifp, int out, mb_t **mp);
237 # endif /* NETBSD_PF */
238 #endif /* __NetBSD__ */
241 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
243 # include <net/pfil.h>
245 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
247 static int fr_check_wrapper(arg, mp, ifp, dir)
253 struct ip *ip = mtod(*mp, struct ip *);
254 int rv, hlen = ip->ip_hl << 2;
256 #if defined(M_CSUM_TCPv4)
258 * If the packet is out-bound, we can't delay checksums
259 * here. For in-bound, the checksum has already been
262 if (dir == PFIL_OUT) {
263 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
264 in_delayed_cksum(*mp);
265 (*mp)->m_pkthdr.csum_flags &=
266 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
269 #endif /* M_CSUM_TCPv4 */
272 * We get the packet with all fields in network byte
273 * order. We expect ip_len and ip_off to be in host
274 * order. We frob them, call the filter, then frob
277 * Note, we don't need to update the checksum, because
278 * it has already been verified.
283 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
285 if (rv == 0 && *mp != NULL) {
286 ip = mtod(*mp, struct ip *);
295 # include <netinet/ip6.h>
297 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
299 static int fr_check_wrapper6(arg, mp, ifp, dir)
306 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
307 ifp, (dir == PFIL_OUT), mp));
310 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
311 #if defined(__DragonFly_version) && (__DragonFly_version >= 100000) && \
315 fr_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
317 struct ip *ip = mtod(*mp, struct ip *);
318 return fr_check(ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT), mp);
322 # include <netinet/ip6.h>
325 fr_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
327 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
328 ifp, (dir == PFIL_OUT), mp));
330 # endif /* USE_INET6 */
331 #endif /* __DragonFly_version >= 100000 && _KERNEL */
333 # if defined(IPFILTER_LKM) && !defined(__sgi)
337 if (strcmp(s, "ipl") == 0)
341 # endif /* IPFILTER_LKM */
345 * Try to detect the case when compiling for NetBSD with pseudo-device
347 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
349 ipfilterattach(count)
354 * Do nothing here, really. The filter will be enabled
355 * by the SIOCFRENB ioctl.
361 # if defined(__NetBSD__) || defined(__OpenBSD__)
369 # if defined(__sgi) || (defined(NETBSD_PF) && \
370 (__NetBSD_Version__ >= 104200000)) || \
371 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
374 #if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)) || \
375 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
376 struct pfil_head *ph_inet;
378 struct pfil_head *ph_inet6;
383 if (fr_running || (fr_checkp == fr_check)) {
384 printf("IP Filter: already initialized\n");
392 if (nat_init() == -1) {
396 if (fr_stateinit() == -1) {
400 if (appr_init() == -1) {
406 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
407 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
408 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
409 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
411 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
421 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
422 PFIL_IN|PFIL_OUT, ph_inet);
426 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
427 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
441 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
444 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
445 if (ph_inet6 != NULL)
446 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
447 PFIL_IN|PFIL_OUT, ph_inet6);
451 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
452 PFIL_IN|PFIL_OUT, ph_inet6);
454 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
455 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
457 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
458 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
471 error = ipfilter_sgi_attach();
481 bzero((char *)frcache, sizeof(frcache));
482 fr_savep = fr_checkp;
483 fr_checkp = fr_check;
487 if (fr_pass & FR_PASS)
489 else if (fr_pass & FR_BLOCK)
492 defpass = "no-match -> block";
494 printf("%s initialized. Default = %s all, Logging = %s\n",
495 ipfilter_version, defpass,
502 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
503 callout_init(&ipfr_slowtimer_ch);
504 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
506 # if defined(__OpenBSD__)
507 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
508 timeout_add(&ipfr_slowtimer_ch, hz/2);
510 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) || defined(__sgi)
511 ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
513 timeout(ipfr_slowtimer, NULL, hz/2);
523 * Disable the filter by removing the hooks from the IP input/output
526 # if defined(__NetBSD__)
533 #if defined(NETBSD_PF) && \
534 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
535 (defined(__DragonFly_version) && (__DragonFly_version >= 100000)))
537 # if (__NetBSD_Version__ >= 105150000) || (__DragonFly_version >= 100000)
538 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
540 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
546 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
547 callout_stop(&ipfr_slowtimer_ch);
549 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000)
550 untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
553 untimeout(ipfr_slowtimer_ch);
555 # if defined(__OpenBSD__)
556 timeout_del(&ipfr_slowtimer_ch);
558 untimeout(ipfr_slowtimer, NULL);
559 # endif /* OpenBSD */
561 # endif /* FreeBSD */
567 printf("IP Filter: not initialized\n");
572 printf("%s unloaded\n", ipfilter_version);
574 fr_checkp = fr_savep;
575 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
576 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
580 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
581 (__DragonFly_version >= 100000))
582 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
584 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
585 PFIL_IN|PFIL_OUT, ph_inet);
589 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
590 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
597 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
600 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
601 if (ph_inet6 != NULL)
602 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
603 PFIL_IN|PFIL_OUT, ph_inet6);
607 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
608 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
618 ipfilter_sgi_detach();
633 static int frzerostats(data)
640 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
644 bzero((char *)frstats, sizeof(*frstats) * 2);
651 * Filter ioctl interface.
654 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
656 , cred_t *cp, int *rp
660 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
661 #if defined(__DragonFly__) || defined(__FreeBSD__)
664 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
665 (NetBSD >= 199511) || defined(__DragonFly__) || (__FreeBSD_version >= 220000) || \
666 defined(__OpenBSD__)))
673 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
674 (_BSDI_VERSION >= 199701) || (defined(__DragonFly__) || __FreeBSD_version >= 300000)
683 #if defined(_KERNEL) && !SOLARIS
686 int error = 0, unit = 0, tmp;
688 #if (BSD >= 199306) && defined(_KERNEL)
689 if ((securelevel >= 3) && (mode & FWRITE))
693 unit = GET_MINOR(dev);
694 if ((IPL_LOGMAX < unit) || (unit < 0))
700 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
705 if (unit == IPL_LOGNAT) {
707 error = nat_ioctl(data, cmd, mode);
713 if (unit == IPL_LOGSTATE) {
715 error = fr_state_ioctl(data, cmd, mode);
721 if (unit == IPL_LOGAUTH) {
725 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
726 if (!(mode & FWRITE)) {
729 error = frrequest(unit, cmd, data,
733 error = fr_auth_ioctl(data, mode, cmd);
742 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
743 sizeof(iplused[IPL_LOGIPF]));
746 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
751 if (!(mode & FWRITE))
754 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
758 # if defined(__NetBSD__) || defined(__OpenBSD__)
759 error = ipl_enable();
764 # if defined(__NetBSD__)
765 error = ipl_disable();
774 if (!(mode & FWRITE))
777 error = IRCOPY(data, (caddr_t)&fr_flags,
781 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
787 if (!(mode & FWRITE))
790 error = frrequest(unit, cmd, data, fr_active);
795 if (!(mode & FWRITE))
798 error = frrequest(unit, cmd, data, 1 - fr_active);
801 if (!(mode & FWRITE))
804 bzero((char *)frcache, sizeof(frcache[0]) * 2);
805 *(u_int *)data = fr_active;
806 fr_active = 1 - fr_active;
814 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
820 if (!(mode & FWRITE))
823 error = frzerostats(data);
826 if (!(mode & FWRITE))
829 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
831 tmp = frflush(unit, 4, tmp);
832 error = IWCOPY((caddr_t)&tmp, data,
839 if (!(mode & FWRITE))
842 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
844 tmp = frflush(unit, 6, tmp);
845 error = IWCOPY((caddr_t)&tmp, data,
852 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
863 if (!(mode & FWRITE))
866 *(int *)data = ipflog_clear(unit);
868 #endif /* IPFILTER_LOG */
870 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
876 if (!(mode & FWRITE))
879 #if defined(_KERNEL) && defined(__sgi)
894 void fr_forgetifp(ifp)
899 WRITE_ENTER(&ipf_mutex);
900 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
901 if (f->fr_ifa == ifp)
902 f->fr_ifa = (void *)-1;
903 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
904 if (f->fr_ifa == ifp)
905 f->fr_ifa = (void *)-1;
906 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
907 if (f->fr_ifa == ifp)
908 f->fr_ifa = (void *)-1;
909 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
910 if (f->fr_ifa == ifp)
911 f->fr_ifa = (void *)-1;
913 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
914 if (f->fr_ifa == ifp)
915 f->fr_ifa = (void *)-1;
916 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
917 if (f->fr_ifa == ifp)
918 f->fr_ifa = (void *)-1;
919 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
920 if (f->fr_ifa == ifp)
921 f->fr_ifa = (void *)-1;
922 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
923 if (f->fr_ifa == ifp)
924 f->fr_ifa = (void *)-1;
926 RWLOCK_EXIT(&ipf_mutex);
931 static int frrequest(unit, req, data, set)
933 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
941 frentry_t *fp, *f, **fprev;
943 frgroup_t *fg = NULL;
944 int error = 0, in, i;
951 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
955 #if (BSD >= 199306) && defined(_KERNEL)
956 if ((securelevel > 0) && (fp->fr_func != NULL))
961 * Check that the group number does exist and that if a head group
962 * has been specified, doesn't exist.
964 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
965 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
966 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
968 if ((req != SIOCZRLST) && fp->fr_group &&
969 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
972 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
974 if (unit == IPL_LOGAUTH)
975 ftail = fprev = &ipauth;
976 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
977 ftail = fprev = &ipacct[in][set];
978 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
979 ftail = fprev = &ipfilter[in][set];
981 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
982 ftail = fprev = &ipacct6[in][set];
983 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
984 ftail = fprev = &ipfilter6[in][set];
989 if ((group = fp->fr_group)) {
990 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
992 ftail = fprev = fg->fg_start;
995 bzero((char *)frcache, sizeof(frcache[0]) * 2);
997 for (i = 0; i < 4; i++) {
998 if ((fp->fr_ifnames[i][1] == '\0') &&
999 ((fp->fr_ifnames[i][0] == '-') ||
1000 (fp->fr_ifnames[i][0] == '*'))) {
1001 fp->fr_ifas[i] = NULL;
1002 } else if (*fp->fr_ifnames[i]) {
1003 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1004 if (!fp->fr_ifas[i])
1005 fp->fr_ifas[i] = (void *)-1;
1010 fp->fr_flags &= ~FR_DUP;
1011 if (*fdp->fd_ifname) {
1012 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1014 fdp->fd_ifp = (struct ifnet *)-1;
1016 fp->fr_flags |= FR_DUP;
1020 if (*fdp->fd_ifname) {
1021 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1023 fdp->fd_ifp = (struct ifnet *)-1;
1027 * Look for a matching filter rule, but don't include the next or
1028 * interface pointer in the comparison (fr_next, fr_ifa).
1030 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1034 for (; (f = *ftail); ftail = &f->fr_next)
1035 if ((fp->fr_cksum == f->fr_cksum) &&
1036 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1040 * If zero'ing statistics, copy current to caller and zero.
1042 if (req == SIOCZRLST) {
1045 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1054 if (req != SIOCINAFR && req != SIOCINIFR)
1055 while ((f = *ftail))
1056 ftail = &f->fr_next;
1060 while (--fp->fr_hits && (f = *ftail))
1061 ftail = &f->fr_next;
1067 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1072 * Only return EBUSY if there is a group list, else
1073 * it's probably just state information referencing
1076 if ((f->fr_ref > 1) && f->fr_grp)
1078 if (fg && fg->fg_head)
1079 fg->fg_head->fr_ref--;
1080 if (unit == IPL_LOGAUTH) {
1081 return fr_preauthcmd(req, f, ftail);
1084 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1086 fixskip(fprev, f, -1);
1087 *ftail = f->fr_next;
1097 if (unit == IPL_LOGAUTH) {
1098 return fr_preauthcmd(req, fp, ftail);
1100 KMALLOC(f, frentry_t *);
1102 if (fg && fg->fg_head)
1103 fg->fg_head->fr_ref++;
1104 bcopy((char *)fp, (char *)f, sizeof(*f));
1107 f->fr_next = *ftail;
1109 if (req == SIOCINIFR || req == SIOCINAFR)
1110 fixskip(fprev, f, 1);
1112 if ((group = f->fr_grhead))
1113 fg = fr_addgroup(group, f, unit, set);
1124 * routines below for saving IP headers to buffer
1128 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1130 int IPL_EXTERN(open)(dev_t dev, int flags)
1133 int IPL_EXTERN(open)(dev, flags
1134 #if defined(__DragonFly__) || defined(__FreeBSD__)
1138 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1139 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1150 # if defined(__sgi) && defined(_KERNEL)
1151 u_int min = geteminor(*pdev);
1153 u_int min = GET_MINOR(dev);
1156 if (IPL_LOGMAX < min)
1165 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1167 int IPL_EXTERN(close)(dev, flags
1168 #if defined(__DragonFly__) || defined(__FreeBSD__)
1172 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1173 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1184 u_int min = GET_MINOR(dev);
1186 if (IPL_LOGMAX < min)
1195 * both of these must operate with at least splnet() lest they be
1196 * called during packet processing and cause an inconsistancy to appear in
1200 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1203 int IPL_EXTERN(read)(dev, uio, ioflag)
1206 int IPL_EXTERN(read)(dev, uio)
1212 # ifdef IPFILTER_LOG
1213 return ipflog_read(GET_MINOR(dev), uio);
1221 * send_reset - this could conceivably be a call to tcp_respond(), but that
1222 * requires a large amount of setting up and isn't any more efficient.
1224 int send_reset(oip, fin)
1228 struct tcphdr *tcp, *tcp2;
1232 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1236 tcp = (struct tcphdr *)fin->fin_dp;
1237 if (tcp->th_flags & TH_RST)
1238 return -1; /* feedback loop */
1239 # if (BSD < 199306) || defined(__sgi)
1240 m = m_get(M_DONTWAIT, MT_HEADER);
1241 # elif defined(__DragonFly__)
1242 m = m_get(MB_DONTWAIT, MT_HEADER);
1244 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1251 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1252 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1253 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1256 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1258 hlen = sizeof(ip_t);
1260 m->m_len = sizeof(*tcp2) + hlen;
1262 m->m_data += max_linkhdr;
1263 m->m_pkthdr.len = m->m_len;
1264 m->m_pkthdr.rcvif = (struct ifnet *)0;
1266 ip = mtod(m, struct ip *);
1270 bzero((char *)ip, sizeof(*tcp2) + hlen);
1271 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1273 tcp2->th_sport = tcp->th_dport;
1274 tcp2->th_dport = tcp->th_sport;
1275 if (tcp->th_flags & TH_ACK) {
1276 tcp2->th_seq = tcp->th_ack;
1277 tcp2->th_flags = TH_RST;
1279 tcp2->th_ack = ntohl(tcp->th_seq);
1280 tcp2->th_ack += tlen;
1281 tcp2->th_ack = htonl(tcp2->th_ack);
1282 tcp2->th_flags = TH_RST|TH_ACK;
1284 tcp2->th_off = sizeof(*tcp2) >> 2;
1286 if (fin->fin_v == 6) {
1287 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1288 ip6->ip6_nxt = IPPROTO_TCP;
1289 ip6->ip6_src = oip6->ip6_dst;
1290 ip6->ip6_dst = oip6->ip6_src;
1291 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1292 sizeof(*ip6), sizeof(*tcp2));
1293 return send_ip(oip, fin, &m);
1296 ip->ip_p = IPPROTO_TCP;
1297 ip->ip_len = htons(sizeof(struct tcphdr));
1298 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1299 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1300 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1301 ip->ip_len = hlen + sizeof(*tcp2);
1302 return send_ip(oip, fin, &m);
1307 * Send an IP(v4/v6) datagram out into the network
1309 static int send_ip(oip, fin, mp)
1314 struct mbuf *m = *mp;
1319 bzero((char *)&frn, sizeof(frn));
1320 frn.fin_ifp = fin->fin_ifp;
1321 frn.fin_v = fin->fin_v;
1322 frn.fin_out = fin->fin_out;
1323 frn.fin_mp = fin->fin_mp;
1325 ip = mtod(m, ip_t *);
1328 ip->ip_v = fin->fin_v;
1329 if (ip->ip_v == 4) {
1330 ip->ip_hl = (sizeof(*oip) >> 2);
1331 ip->ip_v = IPVERSION;
1332 ip->ip_tos = oip->ip_tos;
1333 ip->ip_id = oip->ip_id;
1335 # if defined(__NetBSD__) || \
1336 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1337 if (ip_mtudisc != 0)
1341 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1346 # if (BSD < 199306) || defined(__sgi)
1347 ip->ip_ttl = tcp_ttl;
1349 ip->ip_ttl = ip_defttl;
1352 frn.fin_dp = (char *)(ip + 1);
1355 else if (ip->ip_v == 6) {
1356 ip6_t *ip6 = (ip6_t *)ip;
1358 hlen = sizeof(*ip6);
1359 ip6->ip6_hlim = 127;
1360 frn.fin_dp = (char *)(ip6 + 1);
1364 m->m_pkthdr.rcvif = NULL;
1367 fr_makefrip(hlen, ip, &frn);
1369 error = ipfr_fastroute(m, mp, &frn, NULL);
1374 int send_icmp_err(oip, type, fin, dst)
1380 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1381 u_short shlen, slen = 0, soff = 0;
1382 struct in_addr dst4;
1387 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1388 struct in6_addr dst6;
1392 if ((type < 0) || (type > ICMP_MAXTYPE))
1395 code = fin->fin_icode;
1397 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1404 if (fin->fin_v == 4) {
1405 if ((oip->ip_p == IPPROTO_ICMP) &&
1406 !(fin->fin_fi.fi_fl & FI_SHORT))
1407 switch (ntohs(fin->fin_data[0]) >> 8)
1418 # if (BSD < 199306) || defined(__sgi)
1420 m = m_get(M_DONTWAIT, MT_HEADER);
1421 # elif defined(__DragonFly__)
1423 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1426 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1432 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1435 dst4.s_addr = oip->ip_dst.s_addr;
1437 hlen = sizeof(ip_t);
1438 ohlen = oip->ip_hl << 2;
1443 else if (fin->fin_v == 6) {
1444 hlen = sizeof(ip6_t);
1445 ohlen = sizeof(ip6_t);
1446 type = icmptoicmp6types[type];
1447 if (type == ICMP6_DST_UNREACH)
1448 code = icmptoicmp6unreach[code];
1450 #ifdef __DragonFly__
1451 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1453 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1458 #ifdef __DragonFly__
1459 MCLGET(m, MB_DONTWAIT);
1461 MCLGET(m, M_DONTWAIT);
1463 if ((m->m_flags & M_EXT) == 0) {
1467 # ifdef M_TRAILINGSPACE
1469 avail = M_TRAILINGSPACE(m);
1471 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1473 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1474 avail - hlen - sizeof(*icmp) - max_linkhdr);
1476 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1479 dst6 = oip6->ip6_dst;
1483 iclen = hlen + sizeof(*icmp);
1485 avail -= (max_linkhdr + iclen);
1486 m->m_data += max_linkhdr;
1487 m->m_pkthdr.rcvif = (struct ifnet *)0;
1491 m->m_pkthdr.len = iclen;
1493 avail -= (m->m_off + iclen);
1499 ip = mtod(m, ip_t *);
1500 icmp = (struct icmp *)((char *)ip + hlen);
1501 bzero((char *)ip, iclen);
1503 icmp->icmp_type = type;
1504 icmp->icmp_code = fin->fin_icode;
1505 icmp->icmp_cksum = 0;
1507 if (type == ICMP_UNREACH &&
1508 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1509 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1513 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1514 avail -= MIN(ohlen, avail);
1519 if (fin->fin_v == 6) {
1521 ip6->ip6_plen = htons(iclen - hlen);
1522 ip6->ip6_nxt = IPPROTO_ICMPV6;
1524 ip6->ip6_src = dst6;
1525 ip6->ip6_dst = oip6->ip6_src;
1527 bcopy((char *)oip + ohlen,
1528 (char *)&icmp->icmp_ip + ohlen, avail);
1529 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1530 sizeof(*ip6), iclen - hlen);
1535 oip->ip_len = htons(oip->ip_len);
1537 oip->ip_off = htons(ip->ip_off);
1539 ip->ip_src.s_addr = dst4.s_addr;
1540 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1545 bcopy((char *)oip + ohlen,
1546 (char *)&icmp->icmp_ip + ohlen, avail);
1547 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1550 ip->ip_p = IPPROTO_ICMP;
1553 shlen = fin->fin_hlen;
1554 fin->fin_hlen = hlen;
1555 err = send_ip(oip, fin, &m);
1556 fin->fin_hlen = shlen;
1558 if (fin->fin_v == 4)
1568 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1569 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1575 void iplinit (void);
1582 # if defined(__NetBSD__) || defined(__OpenBSD__)
1583 if (ipl_enable() != 0)
1585 if (iplattach() != 0)
1588 printf("IP Filter failed to attach\n");
1592 # endif /* ! __NetBSD__ */
1596 * Return the length of the entire mbuf.
1598 size_t mbufchainlen(m0)
1602 return m0->m_pkthdr.len;
1606 for (; m0; m0 = m0->m_next)
1613 int ipfr_fastroute(m0, mpp, fin, fdp)
1614 struct mbuf *m0, **mpp;
1618 struct ip *ip, *mhip;
1619 struct mbuf *m = m0;
1621 int len, off, error = 0, hlen, code;
1622 struct ifnet *ifp, *sifp;
1623 struct sockaddr_in *dst;
1624 struct route iproute;
1634 if (fin->fin_v == 6) {
1635 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1641 if (fin->fin_v == 6)
1649 * If the mbuf we're about to send is not writable (because of
1650 * a cluster reference, for example) we'll need to make a copy
1651 * of it since this routine modifies the contents.
1653 * If you have non-crappy network hardware that can transmit data
1654 * from the mbuf, rather than making a copy, this is gonna be a
1657 if (M_WRITABLE(m) == 0) {
1658 #ifdef __DragonFly__
1659 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1661 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1674 hlen = fin->fin_hlen;
1675 ip = mtod(m0, struct ip *);
1677 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1679 * Clear any in-bound checksum flags for this packet.
1681 # if (__NetBSD_Version__ > 105009999)
1682 m0->m_pkthdr.csum_flags = 0;
1684 m0->m_pkthdr.csuminfo = 0;
1686 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1691 #if defined(__sgi) && (IRIX >= 605)
1694 bzero((caddr_t)ro, sizeof (*ro));
1695 dst = (struct sockaddr_in *)&ro->ro_dst;
1696 dst->sin_family = AF_INET;
1697 dst->sin_addr = ip->ip_dst;
1706 * In case we're here due to "to <if>" being used with "keep state",
1707 * check that we're going in the correct direction.
1709 if ((fr != NULL) && (fin->fin_rev != 0)) {
1710 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1712 } else if (fdp != NULL) {
1713 if (fdp->fd_ip.s_addr != 0)
1714 dst->sin_addr = fdp->fd_ip;
1718 dst->sin_len = sizeof(*dst);
1720 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1721 !defined(__OpenBSD__)
1723 rtalloc_ign(ro, RTF_CLONING);
1725 rtalloc_ign(ro, RTF_PRCLONING);
1731 #if defined(__sgi) && (IRIX > 602)
1736 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1742 if ((ifp == NULL) && (ro->ro_rt != NULL))
1743 ifp = ro->ro_rt->rt_ifp;
1745 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1746 if (in_localaddr(ip->ip_dst))
1747 error = EHOSTUNREACH;
1749 error = ENETUNREACH;
1753 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1755 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1757 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1760 ro->ro_rt->rt_use++;
1763 * For input packets which are being "fastrouted", they won't
1764 * go back through output filtering and miss their chance to get
1765 * NAT'd and counted.
1767 if (fin->fin_out == 0) {
1768 sifp = fin->fin_ifp;
1771 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1772 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1773 ATOMIC_INCL(frstats[1].fr_acct);
1776 if (!fr || !(fr->fr_flags & FR_RETMASK))
1777 (void) fr_checkstate(ip, fin);
1778 (void) ip_natout(ip, fin);
1779 fin->fin_ifp = sifp;
1783 * If small enough for interface, can just send directly.
1785 if (ip->ip_len <= ifp->if_mtu) {
1787 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1788 !(__NetBSD_Version__ >= 105110000)
1789 ip->ip_id = htons(ip->ip_id);
1791 ip->ip_len = htons(ip->ip_len);
1792 ip->ip_off = htons(ip->ip_off);
1794 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1795 # if (__NetBSD_Version__ > 105009999)
1796 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1797 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1798 else if (ip->ip_sum == 0)
1799 ip->ip_sum = in_cksum(m, hlen);
1801 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1802 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1803 else if (ip->ip_sum == 0)
1804 ip->ip_sum = in_cksum(m, hlen);
1808 ip->ip_sum = in_cksum(m, hlen);
1809 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1810 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1811 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1814 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1820 * Too large for interface; fragment if possible.
1821 * Must be able to put at least 8 bytes per fragment.
1823 if (ip->ip_off & IP_DF) {
1827 len = (ifp->if_mtu - hlen) &~ 7;
1834 int mhlen, firstlen = len;
1835 struct mbuf **mnext = &m->m_act;
1838 * Loop through length of segment after first fragment,
1839 * make new header and copy data of each part and link onto chain.
1842 mhlen = sizeof (struct ip);
1843 for (off = hlen + len; off < ip->ip_len; off += len) {
1844 # ifdef __DragonFly__
1845 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1846 # elif defined(MGETHDR)
1847 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1849 MGET(m, M_DONTWAIT, MT_HEADER);
1856 m->m_data += max_linkhdr;
1858 m->m_off = MMAXOFF - hlen;
1860 mhip = mtod(m, struct ip *);
1861 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1862 if (hlen > sizeof (struct ip)) {
1863 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1864 mhip->ip_hl = mhlen >> 2;
1867 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1868 if (ip->ip_off & IP_MF)
1869 mhip->ip_off |= IP_MF;
1870 if (off + len >= ip->ip_len)
1871 len = ip->ip_len - off;
1873 mhip->ip_off |= IP_MF;
1874 mhip->ip_len = htons((u_short)(len + mhlen));
1875 m->m_next = m_copy(m0, off, len);
1876 if (m->m_next == 0) {
1877 error = ENOBUFS; /* ??? */
1881 m->m_pkthdr.len = mhlen + len;
1882 m->m_pkthdr.rcvif = NULL;
1884 mhip->ip_off = htons((u_short)mhip->ip_off);
1886 mhip->ip_sum = in_cksum(m, mhlen);
1891 * Update first fragment by trimming what's been copied out
1892 * and updating header, then send each fragment (in order).
1894 m_adj(m0, hlen + firstlen - ip->ip_len);
1895 ip->ip_len = htons((u_short)(hlen + firstlen));
1896 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1898 ip->ip_sum = in_cksum(m0, hlen);
1900 for (m = m0; m; m = m0) {
1904 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1905 error = (*ifp->if_output)(ifp, m,
1906 (struct sockaddr *)dst, ro->ro_rt);
1908 error = (*ifp->if_output)(ifp, m,
1909 (struct sockaddr *)dst);
1921 if (ro->ro_rt != NULL) {
1927 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1928 sifp = fin->fin_ifp;
1929 code = fin->fin_icode;
1930 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1932 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1933 fin->fin_ifp = sifp;
1934 fin->fin_icode = code;
1942 * Return true or false depending on whether the route to the
1943 * given IP address uses the same interface as the one passed.
1945 int fr_verifysrc(ipa, ifp)
1949 struct sockaddr_in *dst;
1950 struct route iproute;
1952 bzero((char *)&iproute, sizeof(iproute));
1953 dst = (struct sockaddr_in *)&iproute.ro_dst;
1954 # if (BSD >= 199306)
1955 dst->sin_len = sizeof(*dst);
1957 dst->sin_family = AF_INET;
1958 dst->sin_addr = ipa;
1959 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1960 !defined(__OpenBSD__)
1962 rtalloc_ign(&iproute, RTF_CLONING);
1964 rtalloc_ign(&iproute, RTF_PRCLONING);
1969 if (iproute.ro_rt == NULL)
1971 return (ifp == iproute.ro_rt->rt_ifp);
1975 # ifdef USE_GETIFNAME
1980 static char workbuf[64];
1982 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1988 # if defined(USE_INET6)
1990 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1991 * or ensure that it is an IPv6 packet that is being forwarded, those are
1992 * expected to be done by the called (ipfr_fastroute).
1994 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1995 struct mbuf *m0, **mpp;
1999 struct route_in6 ip6route;
2000 struct sockaddr_in6 *dst6;
2001 struct route_in6 *ro;
2004 #if defined(OpenBSD) && (OpenBSD >= 200211)
2005 struct route_in6 *ro_pmtu = NULL;
2006 struct in6_addr finaldst;
2015 bzero((caddr_t)ro, sizeof(*ro));
2016 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2017 dst6->sin6_family = AF_INET6;
2018 dst6->sin6_len = sizeof(struct sockaddr_in6);
2019 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
2024 if ((fr != NULL) && (fin->fin_rev != 0)) {
2025 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2027 } else if (fdp != NULL) {
2028 if (IP6_NOTZERO(&fdp->fd_ip6))
2029 dst6->sin6_addr = fdp->fd_ip6.in6;
2031 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
2034 rtalloc((struct route *)ro);
2036 if ((ifp == NULL) && (ro->ro_rt != NULL))
2037 ifp = ro->ro_rt->rt_ifp;
2039 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2040 (ifp != ro->ro_rt->rt_ifp)) {
2041 error = EHOSTUNREACH;
2043 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2044 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2045 ro->ro_rt->rt_use++;
2047 #if defined(OpenBSD) && (OpenBSD >= 200211)
2048 ip6 = mtod(m0, ip6_t *);
2050 finaldst = ip6->ip6_dst;
2051 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2054 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2056 if (m0->m_pkthdr.len <= mtu)
2057 error = nd6_output(ifp, fin->fin_ifp, m0,
2061 #if defined(OpenBSD) && (OpenBSD >= 200211)
2066 if (ro->ro_rt != NULL) {
2072 #else /* #ifdef _KERNEL */
2075 # if defined(__sgi) && (IRIX < 605)
2076 static int no_output (struct ifnet *ifp, struct mbuf *m,
2079 static int no_output (struct ifnet *ifp, struct mbuf *m,
2080 struct sockaddr *s, struct rtentry *rt)
2088 # if defined(__sgi) && (IRIX < 605)
2089 static int write_output (struct ifnet *ifp, struct mbuf *m,
2092 static int write_output (struct ifnet *ifp, struct mbuf *m,
2093 struct sockaddr *s, struct rtentry *rt)
2096 ip_t *ip = (ip_t *)m;
2098 static int write_output(ifp, ip)
2106 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2107 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2108 (defined(__DragonFly__))
2109 sprintf(fname, "%s", ifp->if_xname);
2111 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2113 fd = open(fname, O_WRONLY|O_APPEND);
2118 write(fd, (char *)ip, ntohs(ip->ip_len));
2124 char *get_ifname(ifp)
2127 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2128 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2129 (defined(__DragonFly__))
2130 return ifp->if_xname;
2132 static char fullifname[LIFNAMSIZ];
2134 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2140 struct ifnet *get_unit(ifname, v)
2144 struct ifnet *ifp, **ifa, **old_ifneta;
2146 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2147 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2148 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2149 (defined(__DragonFly__))
2150 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2152 char fullname[LIFNAMSIZ];
2154 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2155 if (!strcmp(ifname, fullname))
2161 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2165 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2172 old_ifneta = ifneta;
2174 ifneta = (struct ifnet **)realloc(ifneta,
2175 (nifs + 1) * sizeof(*ifa));
2181 ifneta[nifs] = NULL;
2182 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2183 if (!ifneta[nifs - 1]) {
2188 ifp = ifneta[nifs - 1];
2190 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2191 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2192 (defined(__DragonFly__))
2193 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2195 ifp->if_name = strdup(ifname);
2197 ifname = ifp->if_name;
2198 while (*ifname && !isdigit(*ifname))
2200 if (*ifname && isdigit(*ifname)) {
2201 ifp->if_unit = atoi(ifname);
2206 ifp->if_output = no_output;
2214 struct ifnet *ifp, **ifa;
2218 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2219 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2220 (defined(__DragonFly__))
2221 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2222 ifp->if_output = write_output;
2223 sprintf(fname, "/tmp/%s", ifp->if_xname);
2224 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2232 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2233 ifp->if_output = write_output;
2234 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2235 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2245 int send_reset(ip, fin)
2249 verbose("- TCP RST sent\n");
2254 int send_icmp_err(ip, code, fin, dst)
2260 verbose("- ICMP UNREACHABLE sent\n");
2270 void m_copydata(m, off, len, cp)
2275 bcopy((char *)m + off, cp, len);
2279 int ipfuiomove(buf, len, rwflag, uio)
2284 int left, ioc, num, offset;
2288 if (rwflag == UIO_READ) {
2292 offset = uio->uio_offset;
2294 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2295 io = uio->uio_iov + ioc;
2299 start = (char *)io->iov_base + offset;
2300 if (start > (char *)io->iov_base + io->iov_len) {
2301 offset -= io->iov_len;
2305 bcopy(buf, start, num);
2306 uio->uio_resid -= num;
2307 uio->uio_offset += num;
2317 #endif /* _KERNEL */