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.7 2004/07/04 09:24:38 darrenr Exp $
9 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_fil.c,v 1.17 2005/06/04 14:24:33 corecode 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>
68 #if defined(__DragonFly__)
69 # include <sys/thread2.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 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.
283 ip->ip_len = ntohs(ip->ip_len);
284 ip->ip_off = ntohs(ip->ip_off);
286 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
288 if (rv == 0 && *mp != NULL) {
289 ip = mtod(*mp, struct ip *);
290 ip->ip_len = htons(ip->ip_len);
291 ip->ip_off = htnos(ip->ip_off);
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__)
371 # if !defined(__DragonFly__)
374 # if defined(__sgi) || (defined(NETBSD_PF) && \
375 (__NetBSD_Version__ >= 104200000)) || \
376 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
379 #if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)) || \
380 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
381 struct pfil_head *ph_inet;
383 struct pfil_head *ph_inet6;
388 if (fr_running || (fr_checkp == fr_check)) {
389 printf("IP Filter: already initialized\n");
397 if (nat_init() == -1) {
401 if (fr_stateinit() == -1) {
405 if (appr_init() == -1) {
411 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
412 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
413 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
414 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
416 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
426 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
427 PFIL_IN|PFIL_OUT, ph_inet);
431 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
432 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
446 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
449 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
450 if (ph_inet6 != NULL)
451 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
452 PFIL_IN|PFIL_OUT, ph_inet6);
456 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
457 PFIL_IN|PFIL_OUT, ph_inet6);
459 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
460 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
462 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
463 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
476 error = ipfilter_sgi_attach();
486 bzero((char *)frcache, sizeof(frcache));
487 fr_savep = fr_checkp;
488 fr_checkp = fr_check;
492 if (fr_pass & FR_PASS)
494 else if (fr_pass & FR_BLOCK)
497 defpass = "no-match -> block";
499 printf("%s initialized. Default = %s all, Logging = %s\n",
500 ipfilter_version, defpass,
507 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
508 callout_init(&ipfr_slowtimer_ch);
509 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
511 # if defined(__OpenBSD__)
512 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
513 timeout_add(&ipfr_slowtimer_ch, hz/2);
515 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) || defined(__sgi)
516 callout_init(&ipfr_slowtimer_ch);
517 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
519 timeout(ipfr_slowtimer, NULL, hz/2);
529 * Disable the filter by removing the hooks from the IP input/output
532 # if defined(__NetBSD__)
538 # if !defined(__DragonFly__)
542 #if defined(NETBSD_PF) && \
543 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
544 (defined(__DragonFly_version) && (__DragonFly_version >= 100000)))
546 # if (__NetBSD_Version__ >= 105150000) || (__DragonFly_version >= 100000)
547 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
549 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
555 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
556 callout_stop(&ipfr_slowtimer_ch);
558 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000)
559 callout_stop(&ipfr_slowtimer_ch);
562 untimeout(ipfr_slowtimer_ch);
564 # if defined(__OpenBSD__)
565 timeout_del(&ipfr_slowtimer_ch);
567 untimeout(ipfr_slowtimer, NULL);
568 # endif /* OpenBSD */
570 # endif /* FreeBSD */
576 printf("IP Filter: not initialized\n");
581 printf("%s unloaded\n", ipfilter_version);
583 fr_checkp = fr_savep;
584 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
585 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
589 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
590 (__DragonFly_version >= 100000))
591 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
593 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
594 PFIL_IN|PFIL_OUT, ph_inet);
598 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
599 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
606 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
609 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
610 if (ph_inet6 != NULL)
611 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
612 PFIL_IN|PFIL_OUT, ph_inet6);
616 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
617 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
627 ipfilter_sgi_detach();
642 static int frzerostats(data)
649 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
653 bzero((char *)frstats, sizeof(*frstats) * 2);
660 * Filter ioctl interface.
663 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
665 , cred_t *cp, int *rp
669 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
670 #if (defined(_KERNEL) && (defined(__DragonFly__) || defined(__FreeBSD__)))
673 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
674 (NetBSD >= 199511) || defined(__DragonFly__) || (__FreeBSD_version >= 220000) || \
675 defined(__OpenBSD__)))
682 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
683 (_BSDI_VERSION >= 199701) || (defined(__DragonFly__) || __FreeBSD_version >= 300000)
692 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
695 int error = 0, unit = 0, tmp;
697 #if (BSD >= 199306) && defined(_KERNEL)
698 if ((securelevel >= 3) && (mode & FWRITE))
702 unit = GET_MINOR(dev);
703 if ((IPL_LOGMAX < unit) || (unit < 0))
709 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
714 if (unit == IPL_LOGNAT) {
716 error = nat_ioctl(data, cmd, mode);
722 if (unit == IPL_LOGSTATE) {
724 error = fr_state_ioctl(data, cmd, mode);
730 if (unit == IPL_LOGAUTH) {
734 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
735 if (!(mode & FWRITE)) {
738 error = frrequest(unit, cmd, data,
742 error = fr_auth_ioctl(data, mode, cmd);
751 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
752 sizeof(iplused[IPL_LOGIPF]));
755 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
760 if (!(mode & FWRITE))
763 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
767 # if defined(__NetBSD__) || defined(__OpenBSD__)
768 error = ipl_enable();
773 # if defined(__NetBSD__)
774 error = ipl_disable();
783 if (!(mode & FWRITE))
786 error = IRCOPY(data, (caddr_t)&fr_flags,
790 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
796 if (!(mode & FWRITE))
799 error = frrequest(unit, cmd, data, fr_active);
804 if (!(mode & FWRITE))
807 error = frrequest(unit, cmd, data, 1 - fr_active);
810 if (!(mode & FWRITE))
813 bzero((char *)frcache, sizeof(frcache[0]) * 2);
814 *(u_int *)data = fr_active;
815 fr_active = 1 - fr_active;
823 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
829 if (!(mode & FWRITE))
832 error = frzerostats(data);
835 if (!(mode & FWRITE))
838 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
840 tmp = frflush(unit, 4, tmp);
841 error = IWCOPY((caddr_t)&tmp, data,
848 if (!(mode & FWRITE))
851 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
853 tmp = frflush(unit, 6, tmp);
854 error = IWCOPY((caddr_t)&tmp, data,
861 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
872 if (!(mode & FWRITE))
875 *(int *)data = ipflog_clear(unit);
877 #endif /* IPFILTER_LOG */
879 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
885 if (!(mode & FWRITE))
888 #if defined(_KERNEL) && defined(__sgi)
903 void fr_forgetifp(ifp)
908 WRITE_ENTER(&ipf_mutex);
909 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
910 if (f->fr_ifa == ifp)
911 f->fr_ifa = (void *)-1;
912 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
913 if (f->fr_ifa == ifp)
914 f->fr_ifa = (void *)-1;
915 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
916 if (f->fr_ifa == ifp)
917 f->fr_ifa = (void *)-1;
918 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
919 if (f->fr_ifa == ifp)
920 f->fr_ifa = (void *)-1;
922 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
923 if (f->fr_ifa == ifp)
924 f->fr_ifa = (void *)-1;
925 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
926 if (f->fr_ifa == ifp)
927 f->fr_ifa = (void *)-1;
928 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
929 if (f->fr_ifa == ifp)
930 f->fr_ifa = (void *)-1;
931 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
932 if (f->fr_ifa == ifp)
933 f->fr_ifa = (void *)-1;
935 RWLOCK_EXIT(&ipf_mutex);
940 static int frrequest(unit, req, data, set)
942 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
950 frentry_t *fp, *f, **fprev;
952 frgroup_t *fg = NULL;
953 int error = 0, in, i;
960 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
964 #if (BSD >= 199306) && defined(_KERNEL)
965 if ((securelevel > 0) && (fp->fr_func != NULL))
970 * Check that the group number does exist and that if a head group
971 * has been specified, doesn't exist.
973 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
974 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
975 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
977 if ((req != SIOCZRLST) && fp->fr_group &&
978 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
981 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
983 if (unit == IPL_LOGAUTH)
984 ftail = fprev = &ipauth;
985 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
986 ftail = fprev = &ipacct[in][set];
987 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
988 ftail = fprev = &ipfilter[in][set];
990 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
991 ftail = fprev = &ipacct6[in][set];
992 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
993 ftail = fprev = &ipfilter6[in][set];
998 if ((group = fp->fr_group)) {
999 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
1001 ftail = fprev = fg->fg_start;
1004 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1006 for (i = 0; i < 4; i++) {
1007 if ((fp->fr_ifnames[i][1] == '\0') &&
1008 ((fp->fr_ifnames[i][0] == '-') ||
1009 (fp->fr_ifnames[i][0] == '*'))) {
1010 fp->fr_ifas[i] = NULL;
1011 } else if (*fp->fr_ifnames[i]) {
1012 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1013 if (!fp->fr_ifas[i])
1014 fp->fr_ifas[i] = (void *)-1;
1019 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;
1025 fp->fr_flags |= FR_DUP;
1029 if (*fdp->fd_ifname) {
1030 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1032 fdp->fd_ifp = (struct ifnet *)-1;
1036 * Look for a matching filter rule, but don't include the next or
1037 * interface pointer in the comparison (fr_next, fr_ifa).
1039 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1043 for (; (f = *ftail); ftail = &f->fr_next)
1044 if ((fp->fr_cksum == f->fr_cksum) &&
1045 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1049 * If zero'ing statistics, copy current to caller and zero.
1051 if (req == SIOCZRLST) {
1054 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1063 if (req != SIOCINAFR && req != SIOCINIFR)
1064 while ((f = *ftail))
1065 ftail = &f->fr_next;
1069 while (--fp->fr_hits && (f = *ftail))
1070 ftail = &f->fr_next;
1076 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1081 * Only return EBUSY if there is a group list, else
1082 * it's probably just state information referencing
1085 if ((f->fr_ref > 1) && f->fr_grp)
1087 if (fg && fg->fg_head)
1088 fg->fg_head->fr_ref--;
1089 if (unit == IPL_LOGAUTH) {
1090 return fr_preauthcmd(req, f, ftail);
1093 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1095 fixskip(fprev, f, -1);
1096 *ftail = f->fr_next;
1106 if (unit == IPL_LOGAUTH) {
1107 return fr_preauthcmd(req, fp, ftail);
1109 KMALLOC(f, frentry_t *);
1111 if (fg && fg->fg_head)
1112 fg->fg_head->fr_ref++;
1113 bcopy((char *)fp, (char *)f, sizeof(*f));
1116 f->fr_next = *ftail;
1118 if (req == SIOCINIFR || req == SIOCINAFR)
1119 fixskip(fprev, f, 1);
1121 if ((group = f->fr_grhead))
1122 fg = fr_addgroup(group, f, unit, set);
1133 * routines below for saving IP headers to buffer
1137 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1139 int IPL_EXTERN(open)(dev_t dev, int flags)
1142 int IPL_EXTERN(open)(dev, flags
1143 #if defined(__DragonFly__) || defined(__FreeBSD__)
1147 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1148 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1159 # if defined(__sgi) && defined(_KERNEL)
1160 u_int min = geteminor(*pdev);
1162 u_int min = GET_MINOR(dev);
1165 if (IPL_LOGMAX < min)
1174 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1176 int IPL_EXTERN(close)(dev, flags
1177 #if defined(__DragonFly__) || defined(__FreeBSD__)
1181 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1182 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1193 u_int min = GET_MINOR(dev);
1195 if (IPL_LOGMAX < min)
1204 * both of these must operate with at least splnet() lest they be
1205 * called during packet processing and cause an inconsistancy to appear in
1209 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1212 int IPL_EXTERN(read)(dev, uio, ioflag)
1215 int IPL_EXTERN(read)(dev, uio)
1221 # ifdef IPFILTER_LOG
1222 return ipflog_read(GET_MINOR(dev), uio);
1230 * send_reset - this could conceivably be a call to tcp_respond(), but that
1231 * requires a large amount of setting up and isn't any more efficient.
1233 int send_reset(oip, fin)
1237 struct tcphdr *tcp, *tcp2;
1241 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1245 tcp = (struct tcphdr *)fin->fin_dp;
1246 if (tcp->th_flags & TH_RST)
1247 return -1; /* feedback loop */
1248 # if (BSD < 199306) || defined(__sgi)
1249 m = m_get(M_DONTWAIT, MT_HEADER);
1250 # elif defined(__DragonFly__)
1251 m = m_get(MB_DONTWAIT, MT_HEADER);
1253 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1260 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1261 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1262 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1265 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1267 hlen = sizeof(ip_t);
1269 m->m_len = sizeof(*tcp2) + hlen;
1271 m->m_data += max_linkhdr;
1272 m->m_pkthdr.len = m->m_len;
1273 m->m_pkthdr.rcvif = (struct ifnet *)0;
1275 ip = mtod(m, struct ip *);
1279 bzero((char *)ip, sizeof(*tcp2) + hlen);
1280 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1282 tcp2->th_sport = tcp->th_dport;
1283 tcp2->th_dport = tcp->th_sport;
1284 if (tcp->th_flags & TH_ACK) {
1285 tcp2->th_seq = tcp->th_ack;
1286 tcp2->th_flags = TH_RST;
1288 tcp2->th_ack = ntohl(tcp->th_seq);
1289 tcp2->th_ack += tlen;
1290 tcp2->th_ack = htonl(tcp2->th_ack);
1291 tcp2->th_flags = TH_RST|TH_ACK;
1293 tcp2->th_off = sizeof(*tcp2) >> 2;
1295 if (fin->fin_v == 6) {
1296 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1297 ip6->ip6_nxt = IPPROTO_TCP;
1298 ip6->ip6_src = oip6->ip6_dst;
1299 ip6->ip6_dst = oip6->ip6_src;
1300 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1301 sizeof(*ip6), sizeof(*tcp2));
1302 return send_ip(oip, fin, &m);
1305 ip->ip_p = IPPROTO_TCP;
1306 ip->ip_len = htons(sizeof(struct tcphdr));
1307 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1308 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1309 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1310 ip->ip_len = hlen + sizeof(*tcp2);
1311 return send_ip(oip, fin, &m);
1316 * Send an IP(v4/v6) datagram out into the network
1318 static int send_ip(oip, fin, mp)
1323 struct mbuf *m = *mp;
1328 bzero((char *)&frn, sizeof(frn));
1329 frn.fin_ifp = fin->fin_ifp;
1330 frn.fin_v = fin->fin_v;
1331 frn.fin_out = fin->fin_out;
1334 ip = mtod(m, ip_t *);
1337 ip->ip_v = fin->fin_v;
1338 if (ip->ip_v == 4) {
1339 ip->ip_hl = (sizeof(*oip) >> 2);
1340 ip->ip_v = IPVERSION;
1341 ip->ip_tos = oip->ip_tos;
1342 ip->ip_id = oip->ip_id;
1344 # if defined(__NetBSD__) || \
1345 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1346 if (ip_mtudisc != 0)
1350 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1355 # if (BSD < 199306) || defined(__sgi)
1356 ip->ip_ttl = tcp_ttl;
1358 ip->ip_ttl = ip_defttl;
1361 frn.fin_dp = (char *)(ip + 1);
1364 else if (ip->ip_v == 6) {
1365 ip6_t *ip6 = (ip6_t *)ip;
1367 hlen = sizeof(*ip6);
1368 ip6->ip6_hlim = 127;
1369 frn.fin_dp = (char *)(ip6 + 1);
1373 m->m_pkthdr.rcvif = NULL;
1376 if (fr_makefrip(hlen, ip, &frn) == 0)
1377 error = ipfr_fastroute(m, mp, &frn, NULL);
1384 int send_icmp_err(oip, type, fin, dst)
1390 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1391 u_short shlen, slen = 0, soff = 0;
1392 struct in_addr dst4;
1397 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1398 struct in6_addr dst6;
1402 if ((type < 0) || (type > ICMP_MAXTYPE))
1405 code = fin->fin_icode;
1407 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1414 if (fin->fin_v == 4) {
1415 if ((oip->ip_p == IPPROTO_ICMP) &&
1416 !(fin->fin_fi.fi_fl & FI_SHORT))
1417 switch (ntohs(fin->fin_data[0]) >> 8)
1428 # if (BSD < 199306) || defined(__sgi)
1430 m = m_get(M_DONTWAIT, MT_HEADER);
1431 # elif defined(__DragonFly__)
1433 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1436 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1442 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1445 dst4.s_addr = oip->ip_dst.s_addr;
1447 hlen = sizeof(ip_t);
1448 ohlen = oip->ip_hl << 2;
1453 else if (fin->fin_v == 6) {
1454 hlen = sizeof(ip6_t);
1455 ohlen = sizeof(ip6_t);
1456 type = icmptoicmp6types[type];
1457 if (type == ICMP6_DST_UNREACH)
1458 code = icmptoicmp6unreach[code];
1460 #ifdef __DragonFly__
1461 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1463 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1468 #ifdef __DragonFly__
1469 MCLGET(m, MB_DONTWAIT);
1471 MCLGET(m, M_DONTWAIT);
1473 if ((m->m_flags & M_EXT) == 0) {
1477 # ifdef M_TRAILINGSPACE
1479 avail = M_TRAILINGSPACE(m);
1481 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1483 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1484 avail - hlen - sizeof(*icmp) - max_linkhdr);
1486 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1489 dst6 = oip6->ip6_dst;
1493 iclen = hlen + sizeof(*icmp);
1495 avail -= (max_linkhdr + iclen);
1496 m->m_data += max_linkhdr;
1497 m->m_pkthdr.rcvif = (struct ifnet *)0;
1501 m->m_pkthdr.len = iclen;
1503 avail -= (m->m_off + iclen);
1509 ip = mtod(m, ip_t *);
1510 icmp = (struct icmp *)((char *)ip + hlen);
1511 bzero((char *)ip, iclen);
1513 icmp->icmp_type = type;
1514 icmp->icmp_code = fin->fin_icode;
1515 icmp->icmp_cksum = 0;
1517 if (type == ICMP_UNREACH &&
1518 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1519 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1524 oip->ip_len = htons(oip->ip_len);
1526 oip->ip_off = htons(oip->ip_off);
1527 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1530 avail -= MIN(ohlen, avail);
1535 if (fin->fin_v == 6) {
1537 ip6->ip6_plen = htons(iclen - hlen);
1538 ip6->ip6_nxt = IPPROTO_ICMPV6;
1540 ip6->ip6_src = dst6;
1541 ip6->ip6_dst = oip6->ip6_src;
1543 bcopy((char *)oip + ohlen,
1544 (char *)&icmp->icmp_ip + ohlen, avail);
1545 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1546 sizeof(*ip6), iclen - hlen);
1551 ip->ip_src.s_addr = dst4.s_addr;
1552 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1557 bcopy((char *)oip + ohlen,
1558 (char *)&icmp->icmp_ip + ohlen, avail);
1559 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1562 ip->ip_p = IPPROTO_ICMP;
1565 shlen = fin->fin_hlen;
1566 fin->fin_hlen = hlen;
1567 err = send_ip(oip, fin, &m);
1568 fin->fin_hlen = shlen;
1574 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1575 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1581 void iplinit (void);
1588 # if defined(__NetBSD__) || defined(__OpenBSD__)
1589 if (ipl_enable() != 0)
1591 if (iplattach() != 0)
1594 printf("IP Filter failed to attach\n");
1598 # endif /* ! __NetBSD__ */
1602 * Return the length of the entire mbuf.
1604 size_t mbufchainlen(m0)
1608 return m0->m_pkthdr.len;
1612 for (; m0; m0 = m0->m_next)
1619 int ipfr_fastroute(m0, mpp, fin, fdp)
1620 struct mbuf *m0, **mpp;
1624 struct ip *ip, *mhip;
1625 struct mbuf *m = m0;
1627 int len, off, error = 0, hlen, code, sout;
1628 struct ifnet *ifp, *sifp;
1629 struct sockaddr_in *dst;
1630 struct route iproute;
1640 if (fin->fin_v == 6) {
1641 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1647 if (fin->fin_v == 6)
1655 * If the mbuf we're about to send is not writable (because of
1656 * a cluster reference, for example) we'll need to make a copy
1657 * of it since this routine modifies the contents.
1659 * If you have non-crappy network hardware that can transmit data
1660 * from the mbuf, rather than making a copy, this is gonna be a
1663 if (M_WRITABLE(m) == 0) {
1664 #ifdef __DragonFly__
1665 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1667 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1680 hlen = fin->fin_hlen;
1681 ip = mtod(m0, struct ip *);
1683 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1685 * Clear any in-bound checksum flags for this packet.
1687 # if (__NetBSD_Version__ > 105009999)
1688 m0->m_pkthdr.csum_flags = 0;
1690 m0->m_pkthdr.csuminfo = 0;
1692 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1697 #if (defined(IRIX) && (IRIX >= 605))
1700 bzero((caddr_t)ro, sizeof (*ro));
1701 dst = (struct sockaddr_in *)&ro->ro_dst;
1702 dst->sin_family = AF_INET;
1703 dst->sin_addr = ip->ip_dst;
1712 * In case we're here due to "to <if>" being used with "keep state",
1713 * check that we're going in the correct direction.
1715 if ((fr != NULL) && (fin->fin_rev != 0)) {
1716 if ((ifp != NULL) && (fdp == &fr->fr_tif)) {
1717 # if (defined(IRIX) && (IRIX >= 605))
1722 } else if (fdp != NULL) {
1723 if (fdp->fd_ip.s_addr != 0)
1724 dst->sin_addr = fdp->fd_ip;
1728 dst->sin_len = sizeof(*dst);
1730 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1731 !defined(__OpenBSD__)
1733 rtalloc_ign(ro, RTF_CLONING);
1735 rtalloc_ign(ro, RTF_PRCLONING);
1742 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1744 # if (defined(IRIX) && (IRIX >= 605))
1751 if ((ifp == NULL) && (ro->ro_rt != NULL))
1752 ifp = ro->ro_rt->rt_ifp;
1754 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1755 if (in_localaddr(ip->ip_dst))
1756 error = EHOSTUNREACH;
1758 error = ENETUNREACH;
1759 # if (defined(IRIX) && (IRIX >= 605))
1765 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1766 #if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1767 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1769 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1772 ro->ro_rt->rt_use++;
1774 #if (defined(IRIX) && (IRIX > 602))
1779 * For input packets which are being "fastrouted", they won't
1780 * go back through output filtering and miss their chance to get
1781 * NAT'd and counted.
1783 if (fin->fin_out == 0) {
1784 sifp = fin->fin_ifp;
1785 sout = fin->fin_out;
1788 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1789 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1790 ATOMIC_INCL(frstats[1].fr_acct);
1793 if (!fr || !(fr->fr_flags & FR_RETMASK))
1794 (void) fr_checkstate(ip, fin);
1796 switch (ip_natout(ip, fin))
1809 fin->fin_ifp = sifp;
1810 fin->fin_out = sout;
1815 * If small enough for interface, can just send directly.
1817 if (ip->ip_len <= ifp->if_mtu) {
1819 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1820 !(__NetBSD_Version__ >= 105110000)
1821 ip->ip_id = htons(ip->ip_id);
1823 ip->ip_len = htons(ip->ip_len);
1824 ip->ip_off = htons(ip->ip_off);
1826 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1827 # if (__NetBSD_Version__ > 105009999)
1828 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1829 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1830 else if (ip->ip_sum == 0)
1831 ip->ip_sum = in_cksum(m, hlen);
1833 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1834 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1835 else if (ip->ip_sum == 0)
1836 ip->ip_sum = in_cksum(m, hlen);
1840 ip->ip_sum = in_cksum(m, hlen);
1841 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1842 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1844 IFNET_UPPERLOCK(ifp);
1846 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1849 IFNET_UPPERUNLOCK(ifp);
1852 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1858 * Too large for interface; fragment if possible.
1859 * Must be able to put at least 8 bytes per fragment.
1861 if (ip->ip_off & IP_DF) {
1865 len = (ifp->if_mtu - hlen) &~ 7;
1872 int mhlen, firstlen = len;
1873 struct mbuf **mnext = &m->m_act;
1876 * Loop through length of segment after first fragment,
1877 * make new header and copy data of each part and link onto chain.
1880 mhlen = sizeof (struct ip);
1881 for (off = hlen + len; off < ip->ip_len; off += len) {
1882 # ifdef __DragonFly__
1883 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1884 # elif defined(MGETHDR)
1885 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1887 MGET(m, M_DONTWAIT, MT_HEADER);
1894 m->m_data += max_linkhdr;
1896 m->m_off = MMAXOFF - hlen;
1898 mhip = mtod(m, struct ip *);
1899 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1900 if (hlen > sizeof (struct ip)) {
1901 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1902 mhip->ip_hl = mhlen >> 2;
1905 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1906 if (ip->ip_off & IP_MF)
1907 mhip->ip_off |= IP_MF;
1908 if (off + len >= ip->ip_len)
1909 len = ip->ip_len - off;
1911 mhip->ip_off |= IP_MF;
1912 mhip->ip_len = htons((u_short)(len + mhlen));
1913 m->m_next = m_copy(m0, off, len);
1914 if (m->m_next == 0) {
1915 error = ENOBUFS; /* ??? */
1919 m->m_pkthdr.len = mhlen + len;
1920 m->m_pkthdr.rcvif = NULL;
1922 mhip->ip_off = htons((u_short)mhip->ip_off);
1924 mhip->ip_sum = in_cksum(m, mhlen);
1929 * Update first fragment by trimming what's been copied out
1930 * and updating header, then send each fragment (in order).
1932 m_adj(m0, hlen + firstlen - ip->ip_len);
1933 ip->ip_len = htons((u_short)(hlen + firstlen));
1934 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1936 ip->ip_sum = in_cksum(m0, hlen);
1938 for (m = m0; m; m = m0) {
1942 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1943 error = (*ifp->if_output)(ifp, m,
1944 (struct sockaddr *)dst, ro->ro_rt);
1946 error = (*ifp->if_output)(ifp, m,
1947 (struct sockaddr *)dst);
1959 if (ro->ro_rt != NULL) {
1965 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1966 sifp = fin->fin_ifp;
1967 code = fin->fin_icode;
1968 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1970 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1971 fin->fin_ifp = sifp;
1972 fin->fin_icode = code;
1980 * Return true or false depending on whether the route to the
1981 * given IP address uses the same interface as the one passed.
1983 int fr_verifysrc(ipa, ifp)
1987 struct sockaddr_in *dst;
1988 struct route iproute;
1990 bzero((char *)&iproute, sizeof(iproute));
1991 dst = (struct sockaddr_in *)&iproute.ro_dst;
1992 # if (BSD >= 199306)
1993 dst->sin_len = sizeof(*dst);
1995 dst->sin_family = AF_INET;
1996 dst->sin_addr = ipa;
1997 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1998 !defined(__OpenBSD__)
2000 rtalloc_ign(&iproute, RTF_CLONING);
2002 rtalloc_ign(&iproute, RTF_PRCLONING);
2007 if (iproute.ro_rt == NULL)
2009 return (ifp == iproute.ro_rt->rt_ifp);
2013 # ifdef USE_GETIFNAME
2018 static char workbuf[64];
2020 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
2026 # if defined(USE_INET6)
2028 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
2029 * or ensure that it is an IPv6 packet that is being forwarded, those are
2030 * expected to be done by the called (ipfr_fastroute).
2032 static int ipfr_fastroute6(m0, mpp, fin, fdp)
2033 struct mbuf *m0, **mpp;
2037 struct route_in6 ip6route;
2038 struct sockaddr_in6 *dst6;
2039 struct route_in6 *ro;
2042 #if defined(OpenBSD) && (OpenBSD >= 200211)
2043 struct route_in6 *ro_pmtu = NULL;
2044 struct in6_addr finaldst;
2052 bzero((caddr_t)ro, sizeof(*ro));
2053 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2054 dst6->sin6_family = AF_INET6;
2055 dst6->sin6_len = sizeof(struct sockaddr_in6);
2056 dst6->sin6_addr = fin->fin_fi.fi_dst.in6;
2063 if ((fr != NULL) && (fin->fin_rev != 0)) {
2064 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2066 } else if (fdp != NULL) {
2067 if (IP6_NOTZERO(&fdp->fd_ip6))
2068 dst6->sin6_addr = fdp->fd_ip6.in6;
2073 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
2075 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
2076 dst6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2078 rtalloc((struct route *)ro);
2080 if ((ifp == NULL) && (ro->ro_rt != NULL))
2081 ifp = ro->ro_rt->rt_ifp;
2083 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2084 (ifp != ro->ro_rt->rt_ifp)) {
2085 error = EHOSTUNREACH;
2087 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2088 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2089 ro->ro_rt->rt_use++;
2091 #if defined(OpenBSD) && (OpenBSD >= 200211)
2092 ip6 = mtod(m0, ip6_t *);
2094 finaldst = ip6->ip6_dst;
2095 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2099 mtu = ND_IFINFO(ifp)->linkmtu;
2101 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2104 if (m0->m_pkthdr.len <= mtu)
2105 error = nd6_output(ifp, fin->fin_ifp, m0,
2109 #if defined(OpenBSD) && (OpenBSD >= 200211)
2114 if (ro->ro_rt != NULL) {
2120 #else /* #ifdef _KERNEL */
2123 # if defined(__sgi) && (IRIX < 605)
2124 static int no_output (struct ifnet *ifp, struct mbuf *m,
2127 static int no_output (struct ifnet *ifp, struct mbuf *m,
2128 struct sockaddr *s, struct rtentry *rt)
2136 # if defined(__sgi) && (IRIX < 605)
2137 static int write_output (struct ifnet *ifp, struct mbuf *m,
2140 static int write_output (struct ifnet *ifp, struct mbuf *m,
2141 struct sockaddr *s, struct rtentry *rt)
2144 ip_t *ip = (ip_t *)m;
2146 static int write_output(ifp, ip)
2154 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2155 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2156 (defined(__DragonFly__))
2157 sprintf(fname, "%s", ifp->if_xname);
2159 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2161 fd = open(fname, O_WRONLY|O_APPEND);
2166 write(fd, (char *)ip, ntohs(ip->ip_len));
2172 char *get_ifname(ifp)
2175 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2176 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2177 (defined(__DragonFly__))
2178 return ifp->if_xname;
2180 static char fullifname[LIFNAMSIZ];
2182 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2188 struct ifnet *get_unit(ifname, v)
2192 struct ifnet *ifp, **ifa, **old_ifneta;
2194 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2195 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2196 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2197 (defined(__DragonFly__))
2198 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2200 char fullname[LIFNAMSIZ];
2202 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2203 if (!strcmp(ifname, fullname))
2209 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2213 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2220 old_ifneta = ifneta;
2222 ifneta = (struct ifnet **)realloc(ifneta,
2223 (nifs + 1) * sizeof(*ifa));
2229 ifneta[nifs] = NULL;
2230 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2231 if (!ifneta[nifs - 1]) {
2236 ifp = ifneta[nifs - 1];
2238 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2239 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2240 (defined(__DragonFly__))
2241 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2243 ifp->if_name = strdup(ifname);
2245 ifname = ifp->if_name;
2246 while (*ifname && !isdigit(*ifname))
2248 if (*ifname && isdigit(*ifname)) {
2249 ifp->if_unit = atoi(ifname);
2254 ifp->if_output = no_output;
2262 struct ifnet *ifp, **ifa;
2266 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2267 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2268 (defined(__DragonFly__))
2269 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2270 ifp->if_output = write_output;
2271 sprintf(fname, "/tmp/%s", ifp->if_xname);
2272 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2280 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2281 ifp->if_output = write_output;
2282 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2283 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2293 int send_reset(ip, fin)
2297 verbose("- TCP RST sent\n");
2302 int send_icmp_err(ip, code, fin, dst)
2308 verbose("- ICMP UNREACHABLE sent\n");
2318 void m_copydata(m, off, len, cp)
2323 bcopy((char *)m + off, cp, len);
2327 int ipfuiomove(buf, len, rwflag, uio)
2332 int left, ioc, num, offset;
2336 if (rwflag == UIO_READ) {
2340 offset = uio->uio_offset;
2342 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2343 io = uio->uio_iov + ioc;
2347 start = (char *)io->iov_base + offset;
2348 if (start > (char *)io->iov_base + io->iov_len) {
2349 offset -= io->iov_len;
2353 bcopy(buf, start, num);
2354 uio->uio_resid -= num;
2355 uio->uio_offset += num;
2365 #endif /* _KERNEL */