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.11 2004/06/02 14:42:48 eirikn 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"
21 #if defined(__DragonFly_version) && (__DragonFly_version >= 100000)
22 #include "opt_pfil_hooks.h"
25 #include <sys/param.h>
26 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
27 defined(_KERNEL) && !defined(_LKM)
28 # include "opt_ipfilter_log.h"
30 #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
31 # if !defined(_KERNEL) || defined(IPFILTER_LKM)
32 # include <osreldate.h>
35 #if defined(__sgi) && (IRIX > 602)
37 # include <sys/ptimers.h>
46 #include <sys/errno.h>
47 #include <sys/types.h>
49 #if (defined(__DragonFly__) || __FreeBSD_version >= 220000) && defined(_KERNEL)
50 # include <sys/fcntl.h>
51 # include <sys/filio.h>
53 # include <sys/ioctl.h>
57 # include <sys/systm.h>
60 # if defined(__DragonFly__) || (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
61 # include <sys/dirent.h>
65 # include <sys/mbuf.h>
67 # include <sys/filio.h>
69 #include <sys/protosw.h>
70 #include <sys/socket.h>
76 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
77 # include <net/if_var.h>
78 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
79 # include "opt_ipfilter.h"
83 #include <sys/debug.h>
84 # ifdef IFF_DRVRLOCK /* IRIX6 */
85 #include <sys/hashing.h>
88 #include <net/route.h>
89 #include <netinet/in.h>
90 #if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /* IRIX < 6 */
91 # include <netinet/in_var.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/tcp.h>
97 #include <netinet/udp.h>
98 #include <netinet/tcpip.h>
99 #include <netinet/ip_icmp.h>
104 #include "ip_compat.h"
106 # include <netinet/icmp6.h>
108 # include <netinet6/ip6protosw.h>
109 # include <netinet6/nd6.h>
115 #include "ip_state.h"
116 #include "ip_proxy.h"
118 #if defined(__DragonFly__) || (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
119 # include <sys/malloc.h>
122 # define MIN(a,b) (((a)<(b))?(a):(b))
124 #if !SOLARIS && defined(_KERNEL) && !defined(__sgi)
125 # include <sys/kernel.h>
126 extern int ip_optcopy (struct ip *, struct ip *);
128 #if defined(OpenBSD) && (OpenBSD >= 200211) && defined(_KERNEL)
129 extern int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
130 struct ifnet *, struct in6_addr *, u_long *);
133 #include <sys/in_cksum.h>
135 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
137 extern struct protosw inetsw[];
141 static struct ifnet **ifneta = NULL;
144 # if (BSD < 199306) || defined(__sgi)
149 #ifdef ICMP_UNREACH_FILTER_PROHIB
150 int ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
152 int ipl_unreach = ICMP_UNREACH_FILTER;
154 u_long ipl_frouteok[2] = {0, 0};
156 static int frzerostats (caddr_t);
157 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
158 static int frrequest (int, u_long, caddr_t, int);
160 static int frrequest (int, int, caddr_t, int);
163 static int (*fr_savep) (ip_t *, int, void *, int, struct mbuf **);
164 static int send_ip (ip_t *, fr_info_t *, struct mbuf **);
166 static int ipfr_fastroute6 (struct mbuf *, struct mbuf **,
167 fr_info_t *, frdest_t *);
170 extern int tcp_mtudisc;
171 extern kmutex_t ipf_rw;
172 extern KRWLOCK_T ipf_mutex;
175 void init_ifp (void);
176 # if defined(__sgi) && (IRIX < 605)
177 static int no_output (struct ifnet *, struct mbuf *,
179 static int write_output (struct ifnet *, struct mbuf *,
182 static int no_output (struct ifnet *, struct mbuf *,
183 struct sockaddr *, struct rtentry *);
184 static int write_output (struct ifnet *, struct mbuf *,
185 struct sockaddr *, struct rtentry *);
190 #if (defined(__DragonFly__) || __FreeBSD_version >= 300000) && defined(_KERNEL)
191 struct callout_handle ipfr_slowtimer_ch;
193 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
194 # include <sys/callout.h>
195 struct callout ipfr_slowtimer_ch;
197 #if defined(__OpenBSD__)
198 # include <sys/timeout.h>
199 struct timeout ipfr_slowtimer_ch;
201 #if defined(__sgi) && defined(_KERNEL)
202 toid_t ipfr_slowtimer_ch;
205 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
207 # include <sys/conf.h>
208 const struct cdevsw ipl_cdevsw = {
209 iplopen, iplclose, iplread, nowrite, iplioctl,
210 nostop, notty, nopoll, nommap,
214 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
215 # include <sys/device.h>
216 # include <sys/conf.h>
218 struct cfdriver iplcd = {
219 NULL, "ipl", NULL, NULL, DV_DULL, 0
222 struct devsw iplsw = {
224 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
225 nostrat, nodump, nopsize, 0,
228 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
230 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
231 (_BSDI_VERSION >= 199701) || (defined(__DragonFly_version) && \
232 (__DragonFly_version >= 100000))
233 # include <sys/conf.h>
234 # if defined(NETBSD_PF)
235 # include <net/pfil.h>
237 * We provide the fr_checkp name just to minimize changes later.
239 int (*fr_checkp) (ip_t *ip, int hlen, void *ifp, int out, mb_t **mp);
240 # endif /* NETBSD_PF */
241 #endif /* __NetBSD__ */
244 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
246 # include <net/pfil.h>
248 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
250 static int fr_check_wrapper(arg, mp, ifp, dir)
256 struct ip *ip = mtod(*mp, struct ip *);
257 int rv, hlen = ip->ip_hl << 2;
259 #if defined(M_CSUM_TCPv4)
261 * If the packet is out-bound, we can't delay checksums
262 * here. For in-bound, the checksum has already been
265 if (dir == PFIL_OUT) {
266 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
267 in_delayed_cksum(*mp);
268 (*mp)->m_pkthdr.csum_flags &=
269 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
272 #endif /* M_CSUM_TCPv4 */
275 * We get the packet with all fields in network byte
276 * order. We expect ip_len and ip_off to be in host
277 * order. We frob them, call the filter, then frob
280 * Note, we don't need to update the checksum, because
281 * it has already been verified.
286 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
288 if (rv == 0 && *mp != NULL) {
289 ip = mtod(*mp, struct ip *);
298 # include <netinet/ip6.h>
300 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
302 static int fr_check_wrapper6(arg, mp, ifp, dir)
309 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
310 ifp, (dir == PFIL_OUT), mp));
313 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
314 #if defined(__DragonFly_version) && (__DragonFly_version >= 100000) && \
318 fr_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
320 struct ip *ip = mtod(*mp, struct ip *);
321 return fr_check(ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT), mp);
325 # include <netinet/ip6.h>
328 fr_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
330 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
331 ifp, (dir == PFIL_OUT), mp));
333 # endif /* USE_INET6 */
334 #endif /* __DragonFly_version >= 100000 && _KERNEL */
336 # if defined(IPFILTER_LKM) && !defined(__sgi)
340 if (strcmp(s, "ipl") == 0)
344 # endif /* IPFILTER_LKM */
348 * Try to detect the case when compiling for NetBSD with pseudo-device
350 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
352 ipfilterattach(count)
357 * Do nothing here, really. The filter will be enabled
358 * by the SIOCFRENB ioctl.
364 # if defined(__NetBSD__) || defined(__OpenBSD__)
372 # if defined(__sgi) || (defined(NETBSD_PF) && \
373 (__NetBSD_Version__ >= 104200000)) || \
374 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
377 #if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)) || \
378 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
379 struct pfil_head *ph_inet;
381 struct pfil_head *ph_inet6;
386 if (fr_running || (fr_checkp == fr_check)) {
387 printf("IP Filter: already initialized\n");
395 if (nat_init() == -1) {
399 if (fr_stateinit() == -1) {
403 if (appr_init() == -1) {
409 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
410 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
411 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
412 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
414 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
424 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
425 PFIL_IN|PFIL_OUT, ph_inet);
429 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
430 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
444 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
447 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
448 if (ph_inet6 != NULL)
449 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
450 PFIL_IN|PFIL_OUT, ph_inet6);
454 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
455 PFIL_IN|PFIL_OUT, ph_inet6);
457 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
458 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
460 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
461 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
474 error = ipfilter_sgi_attach();
484 bzero((char *)frcache, sizeof(frcache));
485 fr_savep = fr_checkp;
486 fr_checkp = fr_check;
490 if (fr_pass & FR_PASS)
492 else if (fr_pass & FR_BLOCK)
495 defpass = "no-match -> block";
497 printf("%s initialized. Default = %s all, Logging = %s\n",
498 ipfilter_version, defpass,
505 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
506 callout_init(&ipfr_slowtimer_ch);
507 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
509 # if defined(__OpenBSD__)
510 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
511 timeout_add(&ipfr_slowtimer_ch, hz/2);
513 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) || defined(__sgi)
514 ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
516 timeout(ipfr_slowtimer, NULL, hz/2);
526 * Disable the filter by removing the hooks from the IP input/output
529 # if defined(__NetBSD__)
536 #if defined(NETBSD_PF) && \
537 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
538 (defined(__DragonFly_version) && (__DragonFly_version >= 100000)))
540 # if (__NetBSD_Version__ >= 105150000) || (__DragonFly_version >= 100000)
541 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
543 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
549 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
550 callout_stop(&ipfr_slowtimer_ch);
552 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000)
553 untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
556 untimeout(ipfr_slowtimer_ch);
558 # if defined(__OpenBSD__)
559 timeout_del(&ipfr_slowtimer_ch);
561 untimeout(ipfr_slowtimer, NULL);
562 # endif /* OpenBSD */
564 # endif /* FreeBSD */
570 printf("IP Filter: not initialized\n");
575 printf("%s unloaded\n", ipfilter_version);
577 fr_checkp = fr_savep;
578 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
579 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
583 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
584 (__DragonFly_version >= 100000))
585 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
587 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
588 PFIL_IN|PFIL_OUT, ph_inet);
592 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
593 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
600 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
603 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
604 if (ph_inet6 != NULL)
605 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
606 PFIL_IN|PFIL_OUT, ph_inet6);
610 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
611 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
621 ipfilter_sgi_detach();
636 static int frzerostats(data)
643 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
647 bzero((char *)frstats, sizeof(*frstats) * 2);
654 * Filter ioctl interface.
657 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
659 , cred_t *cp, int *rp
663 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
664 #if defined(__DragonFly__) || defined(__FreeBSD__)
667 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
668 (NetBSD >= 199511) || defined(__DragonFly__) || (__FreeBSD_version >= 220000) || \
669 defined(__OpenBSD__)))
676 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
677 (_BSDI_VERSION >= 199701) || (defined(__DragonFly__) || __FreeBSD_version >= 300000)
686 #if defined(_KERNEL) && !SOLARIS
689 int error = 0, unit = 0, tmp;
691 #if (BSD >= 199306) && defined(_KERNEL)
692 if ((securelevel >= 3) && (mode & FWRITE))
696 unit = GET_MINOR(dev);
697 if ((IPL_LOGMAX < unit) || (unit < 0))
703 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
708 if (unit == IPL_LOGNAT) {
710 error = nat_ioctl(data, cmd, mode);
716 if (unit == IPL_LOGSTATE) {
718 error = fr_state_ioctl(data, cmd, mode);
724 if (unit == IPL_LOGAUTH) {
728 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
729 if (!(mode & FWRITE)) {
732 error = frrequest(unit, cmd, data,
736 error = fr_auth_ioctl(data, mode, cmd);
745 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
746 sizeof(iplused[IPL_LOGIPF]));
749 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
754 if (!(mode & FWRITE))
757 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
761 # if defined(__NetBSD__) || defined(__OpenBSD__)
762 error = ipl_enable();
767 # if defined(__NetBSD__)
768 error = ipl_disable();
777 if (!(mode & FWRITE))
780 error = IRCOPY(data, (caddr_t)&fr_flags,
784 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
790 if (!(mode & FWRITE))
793 error = frrequest(unit, cmd, data, fr_active);
798 if (!(mode & FWRITE))
801 error = frrequest(unit, cmd, data, 1 - fr_active);
804 if (!(mode & FWRITE))
807 bzero((char *)frcache, sizeof(frcache[0]) * 2);
808 *(u_int *)data = fr_active;
809 fr_active = 1 - fr_active;
817 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
823 if (!(mode & FWRITE))
826 error = frzerostats(data);
829 if (!(mode & FWRITE))
832 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
834 tmp = frflush(unit, 4, tmp);
835 error = IWCOPY((caddr_t)&tmp, data,
842 if (!(mode & FWRITE))
845 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
847 tmp = frflush(unit, 6, tmp);
848 error = IWCOPY((caddr_t)&tmp, data,
855 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
866 if (!(mode & FWRITE))
869 *(int *)data = ipflog_clear(unit);
871 #endif /* IPFILTER_LOG */
873 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
879 if (!(mode & FWRITE))
882 #if defined(_KERNEL) && defined(__sgi)
897 void fr_forgetifp(ifp)
902 WRITE_ENTER(&ipf_mutex);
903 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
904 if (f->fr_ifa == ifp)
905 f->fr_ifa = (void *)-1;
906 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
907 if (f->fr_ifa == ifp)
908 f->fr_ifa = (void *)-1;
909 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
910 if (f->fr_ifa == ifp)
911 f->fr_ifa = (void *)-1;
912 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
913 if (f->fr_ifa == ifp)
914 f->fr_ifa = (void *)-1;
916 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
917 if (f->fr_ifa == ifp)
918 f->fr_ifa = (void *)-1;
919 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
920 if (f->fr_ifa == ifp)
921 f->fr_ifa = (void *)-1;
922 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
923 if (f->fr_ifa == ifp)
924 f->fr_ifa = (void *)-1;
925 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
926 if (f->fr_ifa == ifp)
927 f->fr_ifa = (void *)-1;
929 RWLOCK_EXIT(&ipf_mutex);
934 static int frrequest(unit, req, data, set)
936 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
944 frentry_t *fp, *f, **fprev;
946 frgroup_t *fg = NULL;
947 int error = 0, in, i;
954 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
958 #if (BSD >= 199306) && defined(_KERNEL)
959 if ((securelevel > 0) && (fp->fr_func != NULL))
964 * Check that the group number does exist and that if a head group
965 * has been specified, doesn't exist.
967 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
968 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
969 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
971 if ((req != SIOCZRLST) && fp->fr_group &&
972 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
975 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
977 if (unit == IPL_LOGAUTH)
978 ftail = fprev = &ipauth;
979 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
980 ftail = fprev = &ipacct[in][set];
981 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
982 ftail = fprev = &ipfilter[in][set];
984 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
985 ftail = fprev = &ipacct6[in][set];
986 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
987 ftail = fprev = &ipfilter6[in][set];
992 if ((group = fp->fr_group)) {
993 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
995 ftail = fprev = fg->fg_start;
998 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1000 for (i = 0; i < 4; i++) {
1001 if ((fp->fr_ifnames[i][1] == '\0') &&
1002 ((fp->fr_ifnames[i][0] == '-') ||
1003 (fp->fr_ifnames[i][0] == '*'))) {
1004 fp->fr_ifas[i] = NULL;
1005 } else if (*fp->fr_ifnames[i]) {
1006 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1007 if (!fp->fr_ifas[i])
1008 fp->fr_ifas[i] = (void *)-1;
1013 fp->fr_flags &= ~FR_DUP;
1014 if (*fdp->fd_ifname) {
1015 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1017 fdp->fd_ifp = (struct ifnet *)-1;
1019 fp->fr_flags |= FR_DUP;
1023 if (*fdp->fd_ifname) {
1024 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1026 fdp->fd_ifp = (struct ifnet *)-1;
1030 * Look for a matching filter rule, but don't include the next or
1031 * interface pointer in the comparison (fr_next, fr_ifa).
1033 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1037 for (; (f = *ftail); ftail = &f->fr_next)
1038 if ((fp->fr_cksum == f->fr_cksum) &&
1039 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1043 * If zero'ing statistics, copy current to caller and zero.
1045 if (req == SIOCZRLST) {
1048 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1057 if (req != SIOCINAFR && req != SIOCINIFR)
1058 while ((f = *ftail))
1059 ftail = &f->fr_next;
1063 while (--fp->fr_hits && (f = *ftail))
1064 ftail = &f->fr_next;
1070 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1075 * Only return EBUSY if there is a group list, else
1076 * it's probably just state information referencing
1079 if ((f->fr_ref > 1) && f->fr_grp)
1081 if (fg && fg->fg_head)
1082 fg->fg_head->fr_ref--;
1083 if (unit == IPL_LOGAUTH) {
1084 return fr_preauthcmd(req, f, ftail);
1087 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1089 fixskip(fprev, f, -1);
1090 *ftail = f->fr_next;
1100 if (unit == IPL_LOGAUTH) {
1101 return fr_preauthcmd(req, fp, ftail);
1103 KMALLOC(f, frentry_t *);
1105 if (fg && fg->fg_head)
1106 fg->fg_head->fr_ref++;
1107 bcopy((char *)fp, (char *)f, sizeof(*f));
1110 f->fr_next = *ftail;
1112 if (req == SIOCINIFR || req == SIOCINAFR)
1113 fixskip(fprev, f, 1);
1115 if ((group = f->fr_grhead))
1116 fg = fr_addgroup(group, f, unit, set);
1127 * routines below for saving IP headers to buffer
1131 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1133 int IPL_EXTERN(open)(dev_t dev, int flags)
1136 int IPL_EXTERN(open)(dev, flags
1137 #if defined(__DragonFly__) || defined(__FreeBSD__)
1141 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1142 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1153 # if defined(__sgi) && defined(_KERNEL)
1154 u_int min = geteminor(*pdev);
1156 u_int min = GET_MINOR(dev);
1159 if (IPL_LOGMAX < min)
1168 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1170 int IPL_EXTERN(close)(dev, flags
1171 #if defined(__DragonFly__) || defined(__FreeBSD__)
1175 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1176 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1187 u_int min = GET_MINOR(dev);
1189 if (IPL_LOGMAX < min)
1198 * both of these must operate with at least splnet() lest they be
1199 * called during packet processing and cause an inconsistancy to appear in
1203 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1206 int IPL_EXTERN(read)(dev, uio, ioflag)
1209 int IPL_EXTERN(read)(dev, uio)
1215 # ifdef IPFILTER_LOG
1216 return ipflog_read(GET_MINOR(dev), uio);
1224 * send_reset - this could conceivably be a call to tcp_respond(), but that
1225 * requires a large amount of setting up and isn't any more efficient.
1227 int send_reset(oip, fin)
1231 struct tcphdr *tcp, *tcp2;
1235 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1239 tcp = (struct tcphdr *)fin->fin_dp;
1240 if (tcp->th_flags & TH_RST)
1241 return -1; /* feedback loop */
1242 # if (BSD < 199306) || defined(__sgi)
1243 m = m_get(M_DONTWAIT, MT_HEADER);
1244 # elif defined(__DragonFly__)
1245 m = m_get(MB_DONTWAIT, MT_HEADER);
1247 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1254 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1255 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1256 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1259 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1261 hlen = sizeof(ip_t);
1263 m->m_len = sizeof(*tcp2) + hlen;
1265 m->m_data += max_linkhdr;
1266 m->m_pkthdr.len = m->m_len;
1267 m->m_pkthdr.rcvif = (struct ifnet *)0;
1269 ip = mtod(m, struct ip *);
1273 bzero((char *)ip, sizeof(*tcp2) + hlen);
1274 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1276 tcp2->th_sport = tcp->th_dport;
1277 tcp2->th_dport = tcp->th_sport;
1278 if (tcp->th_flags & TH_ACK) {
1279 tcp2->th_seq = tcp->th_ack;
1280 tcp2->th_flags = TH_RST;
1282 tcp2->th_ack = ntohl(tcp->th_seq);
1283 tcp2->th_ack += tlen;
1284 tcp2->th_ack = htonl(tcp2->th_ack);
1285 tcp2->th_flags = TH_RST|TH_ACK;
1287 tcp2->th_off = sizeof(*tcp2) >> 2;
1289 if (fin->fin_v == 6) {
1290 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1291 ip6->ip6_nxt = IPPROTO_TCP;
1292 ip6->ip6_src = oip6->ip6_dst;
1293 ip6->ip6_dst = oip6->ip6_src;
1294 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1295 sizeof(*ip6), sizeof(*tcp2));
1296 return send_ip(oip, fin, &m);
1299 ip->ip_p = IPPROTO_TCP;
1300 ip->ip_len = htons(sizeof(struct tcphdr));
1301 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1302 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1303 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1304 ip->ip_len = hlen + sizeof(*tcp2);
1305 return send_ip(oip, fin, &m);
1310 * Send an IP(v4/v6) datagram out into the network
1312 static int send_ip(oip, fin, mp)
1317 struct mbuf *m = *mp;
1322 bzero((char *)&frn, sizeof(frn));
1323 frn.fin_ifp = fin->fin_ifp;
1324 frn.fin_v = fin->fin_v;
1325 frn.fin_out = fin->fin_out;
1326 frn.fin_mp = fin->fin_mp;
1328 ip = mtod(m, ip_t *);
1331 ip->ip_v = fin->fin_v;
1332 if (ip->ip_v == 4) {
1333 ip->ip_hl = (sizeof(*oip) >> 2);
1334 ip->ip_v = IPVERSION;
1335 ip->ip_tos = oip->ip_tos;
1336 ip->ip_id = oip->ip_id;
1338 # if defined(__NetBSD__) || \
1339 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1340 if (ip_mtudisc != 0)
1344 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1349 # if (BSD < 199306) || defined(__sgi)
1350 ip->ip_ttl = tcp_ttl;
1352 ip->ip_ttl = ip_defttl;
1355 frn.fin_dp = (char *)(ip + 1);
1358 else if (ip->ip_v == 6) {
1359 ip6_t *ip6 = (ip6_t *)ip;
1361 hlen = sizeof(*ip6);
1362 ip6->ip6_hlim = 127;
1363 frn.fin_dp = (char *)(ip6 + 1);
1367 m->m_pkthdr.rcvif = NULL;
1370 fr_makefrip(hlen, ip, &frn);
1372 error = ipfr_fastroute(m, mp, &frn, NULL);
1377 int send_icmp_err(oip, type, fin, dst)
1383 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1384 u_short shlen, slen = 0, soff = 0;
1385 struct in_addr dst4;
1390 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1391 struct in6_addr dst6;
1395 if ((type < 0) || (type > ICMP_MAXTYPE))
1398 code = fin->fin_icode;
1400 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1407 if (fin->fin_v == 4) {
1408 if ((oip->ip_p == IPPROTO_ICMP) &&
1409 !(fin->fin_fi.fi_fl & FI_SHORT))
1410 switch (ntohs(fin->fin_data[0]) >> 8)
1421 # if (BSD < 199306) || defined(__sgi)
1423 m = m_get(M_DONTWAIT, MT_HEADER);
1424 # elif defined(__DragonFly__)
1426 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1429 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1435 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1438 dst4.s_addr = oip->ip_dst.s_addr;
1440 hlen = sizeof(ip_t);
1441 ohlen = oip->ip_hl << 2;
1446 else if (fin->fin_v == 6) {
1447 hlen = sizeof(ip6_t);
1448 ohlen = sizeof(ip6_t);
1449 type = icmptoicmp6types[type];
1450 if (type == ICMP6_DST_UNREACH)
1451 code = icmptoicmp6unreach[code];
1453 #ifdef __DragonFly__
1454 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1456 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1461 #ifdef __DragonFly__
1462 MCLGET(m, MB_DONTWAIT);
1464 MCLGET(m, M_DONTWAIT);
1466 if ((m->m_flags & M_EXT) == 0) {
1470 # ifdef M_TRAILINGSPACE
1472 avail = M_TRAILINGSPACE(m);
1474 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1476 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1477 avail - hlen - sizeof(*icmp) - max_linkhdr);
1479 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1482 dst6 = oip6->ip6_dst;
1486 iclen = hlen + sizeof(*icmp);
1488 avail -= (max_linkhdr + iclen);
1489 m->m_data += max_linkhdr;
1490 m->m_pkthdr.rcvif = (struct ifnet *)0;
1494 m->m_pkthdr.len = iclen;
1496 avail -= (m->m_off + iclen);
1502 ip = mtod(m, ip_t *);
1503 icmp = (struct icmp *)((char *)ip + hlen);
1504 bzero((char *)ip, iclen);
1506 icmp->icmp_type = type;
1507 icmp->icmp_code = fin->fin_icode;
1508 icmp->icmp_cksum = 0;
1510 if (type == ICMP_UNREACH &&
1511 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1512 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1516 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1517 avail -= MIN(ohlen, avail);
1522 if (fin->fin_v == 6) {
1524 ip6->ip6_plen = htons(iclen - hlen);
1525 ip6->ip6_nxt = IPPROTO_ICMPV6;
1527 ip6->ip6_src = dst6;
1528 ip6->ip6_dst = oip6->ip6_src;
1530 bcopy((char *)oip + ohlen,
1531 (char *)&icmp->icmp_ip + ohlen, avail);
1532 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1533 sizeof(*ip6), iclen - hlen);
1538 oip->ip_len = htons(oip->ip_len);
1540 oip->ip_off = htons(ip->ip_off);
1542 ip->ip_src.s_addr = dst4.s_addr;
1543 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1548 bcopy((char *)oip + ohlen,
1549 (char *)&icmp->icmp_ip + ohlen, avail);
1550 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1553 ip->ip_p = IPPROTO_ICMP;
1556 shlen = fin->fin_hlen;
1557 fin->fin_hlen = hlen;
1558 err = send_ip(oip, fin, &m);
1559 fin->fin_hlen = shlen;
1561 if (fin->fin_v == 4)
1571 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1572 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1578 void iplinit (void);
1585 # if defined(__NetBSD__) || defined(__OpenBSD__)
1586 if (ipl_enable() != 0)
1588 if (iplattach() != 0)
1591 printf("IP Filter failed to attach\n");
1595 # endif /* ! __NetBSD__ */
1599 * Return the length of the entire mbuf.
1601 size_t mbufchainlen(m0)
1605 return m0->m_pkthdr.len;
1609 for (; m0; m0 = m0->m_next)
1616 int ipfr_fastroute(m0, mpp, fin, fdp)
1617 struct mbuf *m0, **mpp;
1621 struct ip *ip, *mhip;
1622 struct mbuf *m = m0;
1624 int len, off, error = 0, hlen, code;
1625 struct ifnet *ifp, *sifp;
1626 struct sockaddr_in *dst;
1627 struct route iproute;
1637 if (fin->fin_v == 6) {
1638 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1644 if (fin->fin_v == 6)
1652 * If the mbuf we're about to send is not writable (because of
1653 * a cluster reference, for example) we'll need to make a copy
1654 * of it since this routine modifies the contents.
1656 * If you have non-crappy network hardware that can transmit data
1657 * from the mbuf, rather than making a copy, this is gonna be a
1660 if (M_WRITABLE(m) == 0) {
1661 #ifdef __DragonFly__
1662 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1664 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1677 hlen = fin->fin_hlen;
1678 ip = mtod(m0, struct ip *);
1680 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1682 * Clear any in-bound checksum flags for this packet.
1684 # if (__NetBSD_Version__ > 105009999)
1685 m0->m_pkthdr.csum_flags = 0;
1687 m0->m_pkthdr.csuminfo = 0;
1689 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1694 #if defined(__sgi) && (IRIX >= 605)
1697 bzero((caddr_t)ro, sizeof (*ro));
1698 dst = (struct sockaddr_in *)&ro->ro_dst;
1699 dst->sin_family = AF_INET;
1700 dst->sin_addr = ip->ip_dst;
1709 * In case we're here due to "to <if>" being used with "keep state",
1710 * check that we're going in the correct direction.
1712 if ((fr != NULL) && (fin->fin_rev != 0)) {
1713 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1715 } else if (fdp != NULL) {
1716 if (fdp->fd_ip.s_addr != 0)
1717 dst->sin_addr = fdp->fd_ip;
1721 dst->sin_len = sizeof(*dst);
1723 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1724 !defined(__OpenBSD__)
1726 rtalloc_ign(ro, RTF_CLONING);
1728 rtalloc_ign(ro, RTF_PRCLONING);
1734 #if defined(__sgi) && (IRIX > 602)
1739 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1745 if ((ifp == NULL) && (ro->ro_rt != NULL))
1746 ifp = ro->ro_rt->rt_ifp;
1748 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1749 if (in_localaddr(ip->ip_dst))
1750 error = EHOSTUNREACH;
1752 error = ENETUNREACH;
1756 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1758 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1760 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1763 ro->ro_rt->rt_use++;
1766 * For input packets which are being "fastrouted", they won't
1767 * go back through output filtering and miss their chance to get
1768 * NAT'd and counted.
1770 if (fin->fin_out == 0) {
1771 sifp = fin->fin_ifp;
1774 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1775 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1776 ATOMIC_INCL(frstats[1].fr_acct);
1779 if (!fr || !(fr->fr_flags & FR_RETMASK))
1780 (void) fr_checkstate(ip, fin);
1781 (void) ip_natout(ip, fin);
1782 fin->fin_ifp = sifp;
1786 * If small enough for interface, can just send directly.
1788 if (ip->ip_len <= ifp->if_mtu) {
1790 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1791 !(__NetBSD_Version__ >= 105110000)
1792 ip->ip_id = htons(ip->ip_id);
1794 ip->ip_len = htons(ip->ip_len);
1795 ip->ip_off = htons(ip->ip_off);
1797 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1798 # if (__NetBSD_Version__ > 105009999)
1799 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1800 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1801 else if (ip->ip_sum == 0)
1802 ip->ip_sum = in_cksum(m, hlen);
1804 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1805 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1806 else if (ip->ip_sum == 0)
1807 ip->ip_sum = in_cksum(m, hlen);
1811 ip->ip_sum = in_cksum(m, hlen);
1812 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1813 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1814 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1817 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1823 * Too large for interface; fragment if possible.
1824 * Must be able to put at least 8 bytes per fragment.
1826 if (ip->ip_off & IP_DF) {
1830 len = (ifp->if_mtu - hlen) &~ 7;
1837 int mhlen, firstlen = len;
1838 struct mbuf **mnext = &m->m_act;
1841 * Loop through length of segment after first fragment,
1842 * make new header and copy data of each part and link onto chain.
1845 mhlen = sizeof (struct ip);
1846 for (off = hlen + len; off < ip->ip_len; off += len) {
1847 # ifdef __DragonFly__
1848 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1849 # elif defined(MGETHDR)
1850 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1852 MGET(m, M_DONTWAIT, MT_HEADER);
1859 m->m_data += max_linkhdr;
1861 m->m_off = MMAXOFF - hlen;
1863 mhip = mtod(m, struct ip *);
1864 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1865 if (hlen > sizeof (struct ip)) {
1866 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1867 mhip->ip_hl = mhlen >> 2;
1870 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1871 if (ip->ip_off & IP_MF)
1872 mhip->ip_off |= IP_MF;
1873 if (off + len >= ip->ip_len)
1874 len = ip->ip_len - off;
1876 mhip->ip_off |= IP_MF;
1877 mhip->ip_len = htons((u_short)(len + mhlen));
1878 m->m_next = m_copy(m0, off, len);
1879 if (m->m_next == 0) {
1880 error = ENOBUFS; /* ??? */
1884 m->m_pkthdr.len = mhlen + len;
1885 m->m_pkthdr.rcvif = NULL;
1887 mhip->ip_off = htons((u_short)mhip->ip_off);
1889 mhip->ip_sum = in_cksum(m, mhlen);
1894 * Update first fragment by trimming what's been copied out
1895 * and updating header, then send each fragment (in order).
1897 m_adj(m0, hlen + firstlen - ip->ip_len);
1898 ip->ip_len = htons((u_short)(hlen + firstlen));
1899 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1901 ip->ip_sum = in_cksum(m0, hlen);
1903 for (m = m0; m; m = m0) {
1907 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1908 error = (*ifp->if_output)(ifp, m,
1909 (struct sockaddr *)dst, ro->ro_rt);
1911 error = (*ifp->if_output)(ifp, m,
1912 (struct sockaddr *)dst);
1924 if (ro->ro_rt != NULL) {
1930 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1931 sifp = fin->fin_ifp;
1932 code = fin->fin_icode;
1933 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1935 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1936 fin->fin_ifp = sifp;
1937 fin->fin_icode = code;
1945 * Return true or false depending on whether the route to the
1946 * given IP address uses the same interface as the one passed.
1948 int fr_verifysrc(ipa, ifp)
1952 struct sockaddr_in *dst;
1953 struct route iproute;
1955 bzero((char *)&iproute, sizeof(iproute));
1956 dst = (struct sockaddr_in *)&iproute.ro_dst;
1957 # if (BSD >= 199306)
1958 dst->sin_len = sizeof(*dst);
1960 dst->sin_family = AF_INET;
1961 dst->sin_addr = ipa;
1962 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1963 !defined(__OpenBSD__)
1965 rtalloc_ign(&iproute, RTF_CLONING);
1967 rtalloc_ign(&iproute, RTF_PRCLONING);
1972 if (iproute.ro_rt == NULL)
1974 return (ifp == iproute.ro_rt->rt_ifp);
1978 # ifdef USE_GETIFNAME
1983 static char workbuf[64];
1985 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1991 # if defined(USE_INET6)
1993 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1994 * or ensure that it is an IPv6 packet that is being forwarded, those are
1995 * expected to be done by the called (ipfr_fastroute).
1997 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1998 struct mbuf *m0, **mpp;
2002 struct route_in6 ip6route;
2003 struct sockaddr_in6 *dst6;
2004 struct route_in6 *ro;
2007 #if defined(OpenBSD) && (OpenBSD >= 200211)
2008 struct route_in6 *ro_pmtu = NULL;
2009 struct in6_addr finaldst;
2018 bzero((caddr_t)ro, sizeof(*ro));
2019 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2020 dst6->sin6_family = AF_INET6;
2021 dst6->sin6_len = sizeof(struct sockaddr_in6);
2022 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
2027 if ((fr != NULL) && (fin->fin_rev != 0)) {
2028 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2030 } else if (fdp != NULL) {
2031 if (IP6_NOTZERO(&fdp->fd_ip6))
2032 dst6->sin6_addr = fdp->fd_ip6.in6;
2034 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
2037 rtalloc((struct route *)ro);
2039 if ((ifp == NULL) && (ro->ro_rt != NULL))
2040 ifp = ro->ro_rt->rt_ifp;
2042 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2043 (ifp != ro->ro_rt->rt_ifp)) {
2044 error = EHOSTUNREACH;
2046 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2047 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2048 ro->ro_rt->rt_use++;
2050 #if defined(OpenBSD) && (OpenBSD >= 200211)
2051 ip6 = mtod(m0, ip6_t *);
2053 finaldst = ip6->ip6_dst;
2054 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2057 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2059 if (m0->m_pkthdr.len <= mtu)
2060 error = nd6_output(ifp, fin->fin_ifp, m0,
2064 #if defined(OpenBSD) && (OpenBSD >= 200211)
2069 if (ro->ro_rt != NULL) {
2075 #else /* #ifdef _KERNEL */
2078 # if defined(__sgi) && (IRIX < 605)
2079 static int no_output (struct ifnet *ifp, struct mbuf *m,
2082 static int no_output (struct ifnet *ifp, struct mbuf *m,
2083 struct sockaddr *s, struct rtentry *rt)
2091 # if defined(__sgi) && (IRIX < 605)
2092 static int write_output (struct ifnet *ifp, struct mbuf *m,
2095 static int write_output (struct ifnet *ifp, struct mbuf *m,
2096 struct sockaddr *s, struct rtentry *rt)
2099 ip_t *ip = (ip_t *)m;
2101 static int write_output(ifp, ip)
2109 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2110 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2111 (defined(__DragonFly__))
2112 sprintf(fname, "%s", ifp->if_xname);
2114 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2116 fd = open(fname, O_WRONLY|O_APPEND);
2121 write(fd, (char *)ip, ntohs(ip->ip_len));
2127 char *get_ifname(ifp)
2130 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2131 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2132 (defined(__DragonFly__))
2133 return ifp->if_xname;
2135 static char fullifname[LIFNAMSIZ];
2137 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2143 struct ifnet *get_unit(ifname, v)
2147 struct ifnet *ifp, **ifa, **old_ifneta;
2149 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2150 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2151 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2152 (defined(__DragonFly__))
2153 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2155 char fullname[LIFNAMSIZ];
2157 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2158 if (!strcmp(ifname, fullname))
2164 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2168 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2175 old_ifneta = ifneta;
2177 ifneta = (struct ifnet **)realloc(ifneta,
2178 (nifs + 1) * sizeof(*ifa));
2184 ifneta[nifs] = NULL;
2185 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2186 if (!ifneta[nifs - 1]) {
2191 ifp = ifneta[nifs - 1];
2193 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2194 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2195 (defined(__DragonFly__))
2196 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2198 ifp->if_name = strdup(ifname);
2200 ifname = ifp->if_name;
2201 while (*ifname && !isdigit(*ifname))
2203 if (*ifname && isdigit(*ifname)) {
2204 ifp->if_unit = atoi(ifname);
2209 ifp->if_output = no_output;
2217 struct ifnet *ifp, **ifa;
2221 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2222 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2223 (defined(__DragonFly__))
2224 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2225 ifp->if_output = write_output;
2226 sprintf(fname, "/tmp/%s", ifp->if_xname);
2227 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2235 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2236 ifp->if_output = write_output;
2237 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2238 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2248 int send_reset(ip, fin)
2252 verbose("- TCP RST sent\n");
2257 int send_icmp_err(ip, code, fin, dst)
2263 verbose("- ICMP UNREACHABLE sent\n");
2273 void m_copydata(m, off, len, cp)
2278 bcopy((char *)m + off, cp, len);
2282 int ipfuiomove(buf, len, rwflag, uio)
2287 int left, ioc, num, offset;
2291 if (rwflag == UIO_READ) {
2295 offset = uio->uio_offset;
2297 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2298 io = uio->uio_iov + ioc;
2302 start = (char *)io->iov_base + offset;
2303 if (start > (char *)io->iov_base + io->iov_len) {
2304 offset -= io->iov_len;
2308 bcopy(buf, start, num);
2309 uio->uio_resid -= num;
2310 uio->uio_offset += num;
2320 #endif /* _KERNEL */