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.9 2004/02/12 22:35:47 joerg 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 <machine/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);
1245 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1252 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1253 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1254 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1257 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1259 hlen = sizeof(ip_t);
1261 m->m_len = sizeof(*tcp2) + hlen;
1263 m->m_data += max_linkhdr;
1264 m->m_pkthdr.len = m->m_len;
1265 m->m_pkthdr.rcvif = (struct ifnet *)0;
1267 ip = mtod(m, struct ip *);
1271 bzero((char *)ip, sizeof(*tcp2) + hlen);
1272 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1274 tcp2->th_sport = tcp->th_dport;
1275 tcp2->th_dport = tcp->th_sport;
1276 if (tcp->th_flags & TH_ACK) {
1277 tcp2->th_seq = tcp->th_ack;
1278 tcp2->th_flags = TH_RST;
1280 tcp2->th_ack = ntohl(tcp->th_seq);
1281 tcp2->th_ack += tlen;
1282 tcp2->th_ack = htonl(tcp2->th_ack);
1283 tcp2->th_flags = TH_RST|TH_ACK;
1285 tcp2->th_off = sizeof(*tcp2) >> 2;
1287 if (fin->fin_v == 6) {
1288 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1289 ip6->ip6_nxt = IPPROTO_TCP;
1290 ip6->ip6_src = oip6->ip6_dst;
1291 ip6->ip6_dst = oip6->ip6_src;
1292 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1293 sizeof(*ip6), sizeof(*tcp2));
1294 return send_ip(oip, fin, &m);
1297 ip->ip_p = IPPROTO_TCP;
1298 ip->ip_len = htons(sizeof(struct tcphdr));
1299 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1300 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1301 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1302 ip->ip_len = hlen + sizeof(*tcp2);
1303 return send_ip(oip, fin, &m);
1308 * Send an IP(v4/v6) datagram out into the network
1310 static int send_ip(oip, fin, mp)
1315 struct mbuf *m = *mp;
1320 bzero((char *)&frn, sizeof(frn));
1321 frn.fin_ifp = fin->fin_ifp;
1322 frn.fin_v = fin->fin_v;
1323 frn.fin_out = fin->fin_out;
1324 frn.fin_mp = fin->fin_mp;
1326 ip = mtod(m, ip_t *);
1329 ip->ip_v = fin->fin_v;
1330 if (ip->ip_v == 4) {
1331 ip->ip_hl = (sizeof(*oip) >> 2);
1332 ip->ip_v = IPVERSION;
1333 ip->ip_tos = oip->ip_tos;
1334 ip->ip_id = oip->ip_id;
1336 # if defined(__NetBSD__) || \
1337 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1338 if (ip_mtudisc != 0)
1342 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1347 # if (BSD < 199306) || defined(__sgi)
1348 ip->ip_ttl = tcp_ttl;
1350 ip->ip_ttl = ip_defttl;
1353 frn.fin_dp = (char *)(ip + 1);
1356 else if (ip->ip_v == 6) {
1357 ip6_t *ip6 = (ip6_t *)ip;
1359 hlen = sizeof(*ip6);
1360 ip6->ip6_hlim = 127;
1361 frn.fin_dp = (char *)(ip6 + 1);
1365 m->m_pkthdr.rcvif = NULL;
1368 fr_makefrip(hlen, ip, &frn);
1370 error = ipfr_fastroute(m, mp, &frn, NULL);
1375 int send_icmp_err(oip, type, fin, dst)
1381 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1382 u_short shlen, slen = 0, soff = 0;
1383 struct in_addr dst4;
1388 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1389 struct in6_addr dst6;
1393 if ((type < 0) || (type > ICMP_MAXTYPE))
1396 code = fin->fin_icode;
1398 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1405 if (fin->fin_v == 4) {
1406 if ((oip->ip_p == IPPROTO_ICMP) &&
1407 !(fin->fin_fi.fi_fl & FI_SHORT))
1408 switch (ntohs(fin->fin_data[0]) >> 8)
1419 # if (BSD < 199306) || defined(__sgi)
1421 m = m_get(M_DONTWAIT, MT_HEADER);
1424 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1430 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1433 dst4.s_addr = oip->ip_dst.s_addr;
1435 hlen = sizeof(ip_t);
1436 ohlen = oip->ip_hl << 2;
1441 else if (fin->fin_v == 6) {
1442 hlen = sizeof(ip6_t);
1443 ohlen = sizeof(ip6_t);
1444 type = icmptoicmp6types[type];
1445 if (type == ICMP6_DST_UNREACH)
1446 code = icmptoicmp6unreach[code];
1448 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1452 MCLGET(m, M_DONTWAIT);
1453 if ((m->m_flags & M_EXT) == 0) {
1457 # ifdef M_TRAILINGSPACE
1459 avail = M_TRAILINGSPACE(m);
1461 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1463 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1464 avail - hlen - sizeof(*icmp) - max_linkhdr);
1466 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1469 dst6 = oip6->ip6_dst;
1473 iclen = hlen + sizeof(*icmp);
1475 avail -= (max_linkhdr + iclen);
1476 m->m_data += max_linkhdr;
1477 m->m_pkthdr.rcvif = (struct ifnet *)0;
1481 m->m_pkthdr.len = iclen;
1483 avail -= (m->m_off + iclen);
1489 ip = mtod(m, ip_t *);
1490 icmp = (struct icmp *)((char *)ip + hlen);
1491 bzero((char *)ip, iclen);
1493 icmp->icmp_type = type;
1494 icmp->icmp_code = fin->fin_icode;
1495 icmp->icmp_cksum = 0;
1497 if (type == ICMP_UNREACH &&
1498 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1499 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1503 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1504 avail -= MIN(ohlen, avail);
1509 if (fin->fin_v == 6) {
1511 ip6->ip6_plen = htons(iclen - hlen);
1512 ip6->ip6_nxt = IPPROTO_ICMPV6;
1514 ip6->ip6_src = dst6;
1515 ip6->ip6_dst = oip6->ip6_src;
1517 bcopy((char *)oip + ohlen,
1518 (char *)&icmp->icmp_ip + ohlen, avail);
1519 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1520 sizeof(*ip6), iclen - hlen);
1525 oip->ip_len = htons(oip->ip_len);
1527 oip->ip_off = htons(ip->ip_off);
1529 ip->ip_src.s_addr = dst4.s_addr;
1530 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1535 bcopy((char *)oip + ohlen,
1536 (char *)&icmp->icmp_ip + ohlen, avail);
1537 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1540 ip->ip_p = IPPROTO_ICMP;
1543 shlen = fin->fin_hlen;
1544 fin->fin_hlen = hlen;
1545 err = send_ip(oip, fin, &m);
1546 fin->fin_hlen = shlen;
1548 if (fin->fin_v == 4)
1558 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1559 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1565 void iplinit (void);
1572 # if defined(__NetBSD__) || defined(__OpenBSD__)
1573 if (ipl_enable() != 0)
1575 if (iplattach() != 0)
1578 printf("IP Filter failed to attach\n");
1582 # endif /* ! __NetBSD__ */
1586 * Return the length of the entire mbuf.
1588 size_t mbufchainlen(m0)
1592 return m0->m_pkthdr.len;
1596 for (; m0; m0 = m0->m_next)
1603 int ipfr_fastroute(m0, mpp, fin, fdp)
1604 struct mbuf *m0, **mpp;
1608 struct ip *ip, *mhip;
1609 struct mbuf *m = m0;
1611 int len, off, error = 0, hlen, code;
1612 struct ifnet *ifp, *sifp;
1613 struct sockaddr_in *dst;
1614 struct route iproute;
1624 if (fin->fin_v == 6) {
1625 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1631 if (fin->fin_v == 6)
1639 * If the mbuf we're about to send is not writable (because of
1640 * a cluster reference, for example) we'll need to make a copy
1641 * of it since this routine modifies the contents.
1643 * If you have non-crappy network hardware that can transmit data
1644 * from the mbuf, rather than making a copy, this is gonna be a
1647 if (M_WRITABLE(m) == 0) {
1648 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1660 hlen = fin->fin_hlen;
1661 ip = mtod(m0, struct ip *);
1663 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1665 * Clear any in-bound checksum flags for this packet.
1667 # if (__NetBSD_Version__ > 105009999)
1668 m0->m_pkthdr.csum_flags = 0;
1670 m0->m_pkthdr.csuminfo = 0;
1672 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1677 #if defined(__sgi) && (IRIX >= 605)
1680 bzero((caddr_t)ro, sizeof (*ro));
1681 dst = (struct sockaddr_in *)&ro->ro_dst;
1682 dst->sin_family = AF_INET;
1683 dst->sin_addr = ip->ip_dst;
1692 * In case we're here due to "to <if>" being used with "keep state",
1693 * check that we're going in the correct direction.
1695 if ((fr != NULL) && (fin->fin_rev != 0)) {
1696 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1698 } else if (fdp != NULL) {
1699 if (fdp->fd_ip.s_addr != 0)
1700 dst->sin_addr = fdp->fd_ip;
1704 dst->sin_len = sizeof(*dst);
1706 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1707 !defined(__OpenBSD__)
1709 rtalloc_ign(ro, RTF_CLONING);
1711 rtalloc_ign(ro, RTF_PRCLONING);
1717 #if defined(__sgi) && (IRIX > 602)
1722 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1728 if ((ifp == NULL) && (ro->ro_rt != NULL))
1729 ifp = ro->ro_rt->rt_ifp;
1731 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1732 if (in_localaddr(ip->ip_dst))
1733 error = EHOSTUNREACH;
1735 error = ENETUNREACH;
1739 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1741 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1743 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1746 ro->ro_rt->rt_use++;
1749 * For input packets which are being "fastrouted", they won't
1750 * go back through output filtering and miss their chance to get
1751 * NAT'd and counted.
1753 if (fin->fin_out == 0) {
1754 sifp = fin->fin_ifp;
1757 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1758 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1759 ATOMIC_INCL(frstats[1].fr_acct);
1762 if (!fr || !(fr->fr_flags & FR_RETMASK))
1763 (void) fr_checkstate(ip, fin);
1764 (void) ip_natout(ip, fin);
1765 fin->fin_ifp = sifp;
1769 * If small enough for interface, can just send directly.
1771 if (ip->ip_len <= ifp->if_mtu) {
1773 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1774 !(__NetBSD_Version__ >= 105110000)
1775 ip->ip_id = htons(ip->ip_id);
1777 ip->ip_len = htons(ip->ip_len);
1778 ip->ip_off = htons(ip->ip_off);
1780 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1781 # if (__NetBSD_Version__ > 105009999)
1782 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1783 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1784 else if (ip->ip_sum == 0)
1785 ip->ip_sum = in_cksum(m, hlen);
1787 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1788 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1789 else if (ip->ip_sum == 0)
1790 ip->ip_sum = in_cksum(m, hlen);
1794 ip->ip_sum = in_cksum(m, hlen);
1795 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1796 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1797 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1800 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1806 * Too large for interface; fragment if possible.
1807 * Must be able to put at least 8 bytes per fragment.
1809 if (ip->ip_off & IP_DF) {
1813 len = (ifp->if_mtu - hlen) &~ 7;
1820 int mhlen, firstlen = len;
1821 struct mbuf **mnext = &m->m_act;
1824 * Loop through length of segment after first fragment,
1825 * make new header and copy data of each part and link onto chain.
1828 mhlen = sizeof (struct ip);
1829 for (off = hlen + len; off < ip->ip_len; off += len) {
1831 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1833 MGET(m, M_DONTWAIT, MT_HEADER);
1840 m->m_data += max_linkhdr;
1842 m->m_off = MMAXOFF - hlen;
1844 mhip = mtod(m, struct ip *);
1845 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1846 if (hlen > sizeof (struct ip)) {
1847 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1848 mhip->ip_hl = mhlen >> 2;
1851 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1852 if (ip->ip_off & IP_MF)
1853 mhip->ip_off |= IP_MF;
1854 if (off + len >= ip->ip_len)
1855 len = ip->ip_len - off;
1857 mhip->ip_off |= IP_MF;
1858 mhip->ip_len = htons((u_short)(len + mhlen));
1859 m->m_next = m_copy(m0, off, len);
1860 if (m->m_next == 0) {
1861 error = ENOBUFS; /* ??? */
1865 m->m_pkthdr.len = mhlen + len;
1866 m->m_pkthdr.rcvif = NULL;
1868 mhip->ip_off = htons((u_short)mhip->ip_off);
1870 mhip->ip_sum = in_cksum(m, mhlen);
1875 * Update first fragment by trimming what's been copied out
1876 * and updating header, then send each fragment (in order).
1878 m_adj(m0, hlen + firstlen - ip->ip_len);
1879 ip->ip_len = htons((u_short)(hlen + firstlen));
1880 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1882 ip->ip_sum = in_cksum(m0, hlen);
1884 for (m = m0; m; m = m0) {
1888 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1889 error = (*ifp->if_output)(ifp, m,
1890 (struct sockaddr *)dst, ro->ro_rt);
1892 error = (*ifp->if_output)(ifp, m,
1893 (struct sockaddr *)dst);
1905 if (ro->ro_rt != NULL) {
1911 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1912 sifp = fin->fin_ifp;
1913 code = fin->fin_icode;
1914 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1916 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1917 fin->fin_ifp = sifp;
1918 fin->fin_icode = code;
1926 * Return true or false depending on whether the route to the
1927 * given IP address uses the same interface as the one passed.
1929 int fr_verifysrc(ipa, ifp)
1933 struct sockaddr_in *dst;
1934 struct route iproute;
1936 bzero((char *)&iproute, sizeof(iproute));
1937 dst = (struct sockaddr_in *)&iproute.ro_dst;
1938 # if (BSD >= 199306)
1939 dst->sin_len = sizeof(*dst);
1941 dst->sin_family = AF_INET;
1942 dst->sin_addr = ipa;
1943 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1944 !defined(__OpenBSD__)
1946 rtalloc_ign(&iproute, RTF_CLONING);
1948 rtalloc_ign(&iproute, RTF_PRCLONING);
1953 if (iproute.ro_rt == NULL)
1955 return (ifp == iproute.ro_rt->rt_ifp);
1959 # ifdef USE_GETIFNAME
1964 static char workbuf[64];
1966 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1972 # if defined(USE_INET6)
1974 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1975 * or ensure that it is an IPv6 packet that is being forwarded, those are
1976 * expected to be done by the called (ipfr_fastroute).
1978 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1979 struct mbuf *m0, **mpp;
1983 struct route_in6 ip6route;
1984 struct sockaddr_in6 *dst6;
1985 struct route_in6 *ro;
1988 #if defined(OpenBSD) && (OpenBSD >= 200211)
1989 struct route_in6 *ro_pmtu = NULL;
1990 struct in6_addr finaldst;
1999 bzero((caddr_t)ro, sizeof(*ro));
2000 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2001 dst6->sin6_family = AF_INET6;
2002 dst6->sin6_len = sizeof(struct sockaddr_in6);
2003 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
2008 if ((fr != NULL) && (fin->fin_rev != 0)) {
2009 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2011 } else if (fdp != NULL) {
2012 if (IP6_NOTZERO(&fdp->fd_ip6))
2013 dst6->sin6_addr = fdp->fd_ip6.in6;
2015 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
2018 rtalloc((struct route *)ro);
2020 if ((ifp == NULL) && (ro->ro_rt != NULL))
2021 ifp = ro->ro_rt->rt_ifp;
2023 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2024 (ifp != ro->ro_rt->rt_ifp)) {
2025 error = EHOSTUNREACH;
2027 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2028 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2029 ro->ro_rt->rt_use++;
2031 #if defined(OpenBSD) && (OpenBSD >= 200211)
2032 ip6 = mtod(m0, ip6_t *);
2034 finaldst = ip6->ip6_dst;
2035 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2038 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2040 if (m0->m_pkthdr.len <= mtu)
2041 error = nd6_output(ifp, fin->fin_ifp, m0,
2045 #if defined(OpenBSD) && (OpenBSD >= 200211)
2050 if (ro->ro_rt != NULL) {
2056 #else /* #ifdef _KERNEL */
2059 # if defined(__sgi) && (IRIX < 605)
2060 static int no_output (struct ifnet *ifp, struct mbuf *m,
2063 static int no_output (struct ifnet *ifp, struct mbuf *m,
2064 struct sockaddr *s, struct rtentry *rt)
2072 # if defined(__sgi) && (IRIX < 605)
2073 static int write_output (struct ifnet *ifp, struct mbuf *m,
2076 static int write_output (struct ifnet *ifp, struct mbuf *m,
2077 struct sockaddr *s, struct rtentry *rt)
2080 ip_t *ip = (ip_t *)m;
2082 static int write_output(ifp, ip)
2090 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2091 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2092 (defined(__DragonFly__))
2093 sprintf(fname, "%s", ifp->if_xname);
2095 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2097 fd = open(fname, O_WRONLY|O_APPEND);
2102 write(fd, (char *)ip, ntohs(ip->ip_len));
2108 char *get_ifname(ifp)
2111 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2112 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2113 (defined(__DragonFly__))
2114 return ifp->if_xname;
2116 static char fullifname[LIFNAMSIZ];
2118 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2124 struct ifnet *get_unit(ifname, v)
2128 struct ifnet *ifp, **ifa, **old_ifneta;
2130 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2131 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2132 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2133 (defined(__DragonFly__))
2134 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2136 char fullname[LIFNAMSIZ];
2138 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2139 if (!strcmp(ifname, fullname))
2145 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2149 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2156 old_ifneta = ifneta;
2158 ifneta = (struct ifnet **)realloc(ifneta,
2159 (nifs + 1) * sizeof(*ifa));
2165 ifneta[nifs] = NULL;
2166 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2167 if (!ifneta[nifs - 1]) {
2172 ifp = ifneta[nifs - 1];
2174 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2175 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2176 (defined(__DragonFly__))
2177 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2179 ifp->if_name = strdup(ifname);
2181 ifname = ifp->if_name;
2182 while (*ifname && !isdigit(*ifname))
2184 if (*ifname && isdigit(*ifname)) {
2185 ifp->if_unit = atoi(ifname);
2190 ifp->if_output = no_output;
2198 struct ifnet *ifp, **ifa;
2202 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2203 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2204 (defined(__DragonFly__))
2205 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2206 ifp->if_output = write_output;
2207 sprintf(fname, "/tmp/%s", ifp->if_xname);
2208 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2216 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2217 ifp->if_output = write_output;
2218 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2219 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2229 int send_reset(ip, fin)
2233 verbose("- TCP RST sent\n");
2238 int send_icmp_err(ip, code, fin, dst)
2244 verbose("- ICMP UNREACHABLE sent\n");
2254 void m_copydata(m, off, len, cp)
2259 bcopy((char *)m + off, cp, len);
2263 int ipfuiomove(buf, len, rwflag, uio)
2268 int left, ioc, num, offset;
2272 if (rwflag == UIO_READ) {
2276 offset = uio->uio_offset;
2278 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2279 io = uio->uio_iov + ioc;
2283 start = (char *)io->iov_base + offset;
2284 if (start > (char *)io->iov_base + io->iov_len) {
2285 offset -= io->iov_len;
2289 bcopy(buf, start, num);
2290 uio->uio_resid -= num;
2291 uio->uio_offset += num;
2301 #endif /* _KERNEL */