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.15 2004/09/16 23:40:24 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"
22 #include <sys/param.h>
23 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
24 defined(_KERNEL) && !defined(_LKM)
25 # include "opt_ipfilter_log.h"
27 #if defined(__FreeBSD__) && !defined(__FreeBSD_version)
28 # if !defined(_KERNEL) || defined(IPFILTER_LKM)
29 # include <osreldate.h>
32 #if defined(__sgi) && (IRIX > 602)
34 # include <sys/ptimers.h>
43 #include <sys/errno.h>
44 #include <sys/types.h>
46 #if (defined(__DragonFly__) || __FreeBSD_version >= 220000) && defined(_KERNEL)
47 # include <sys/fcntl.h>
48 # include <sys/filio.h>
50 # include <sys/ioctl.h>
54 # include <sys/systm.h>
57 # if defined(__DragonFly__) || (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
58 # include <sys/dirent.h>
62 # include <sys/mbuf.h>
64 # include <sys/filio.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
73 #if defined(__DragonFly__) || __FreeBSD_version >= 300000
74 # include <net/if_var.h>
75 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
76 # include "opt_ipfilter.h"
80 #include <sys/debug.h>
81 # ifdef IFF_DRVRLOCK /* IRIX6 */
82 #include <sys/hashing.h>
85 #include <net/route.h>
86 #include <netinet/in.h>
87 #if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /* IRIX < 6 */
88 # include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_var.h>
93 #include <netinet/tcp.h>
94 #include <netinet/udp.h>
95 #include <netinet/tcpip.h>
96 #include <netinet/ip_icmp.h>
101 #include "ip_compat.h"
103 # include <netinet/icmp6.h>
105 # include <netinet6/ip6protosw.h>
106 # include <netinet6/nd6.h>
112 #include "ip_state.h"
113 #include "ip_proxy.h"
115 #if defined(__DragonFly__) || (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
116 # include <sys/malloc.h>
119 # define MIN(a,b) (((a)<(b))?(a):(b))
121 #if !SOLARIS && defined(_KERNEL) && !defined(__sgi)
122 # include <sys/kernel.h>
123 extern int ip_optcopy (struct ip *, struct ip *);
125 #if defined(OpenBSD) && (OpenBSD >= 200211) && defined(_KERNEL)
126 extern int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
127 struct ifnet *, struct in6_addr *, u_long *);
130 #include <sys/in_cksum.h>
132 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
134 extern struct protosw inetsw[];
138 static struct ifnet **ifneta = NULL;
141 # if (BSD < 199306) || defined(__sgi)
146 #ifdef ICMP_UNREACH_FILTER_PROHIB
147 int ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
149 int ipl_unreach = ICMP_UNREACH_FILTER;
151 u_long ipl_frouteok[2] = {0, 0};
153 static int frzerostats (caddr_t);
154 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
155 static int frrequest (int, u_long, caddr_t, int);
157 static int frrequest (int, int, caddr_t, int);
160 static int (*fr_savep) (ip_t *, int, void *, int, struct mbuf **);
161 static int send_ip (ip_t *, fr_info_t *, struct mbuf **);
163 static int ipfr_fastroute6 (struct mbuf *, struct mbuf **,
164 fr_info_t *, frdest_t *);
167 extern int tcp_mtudisc;
168 extern kmutex_t ipf_rw;
169 extern KRWLOCK_T ipf_mutex;
172 void init_ifp (void);
173 # if defined(__sgi) && (IRIX < 605)
174 static int no_output (struct ifnet *, struct mbuf *,
176 static int write_output (struct ifnet *, struct mbuf *,
179 static int no_output (struct ifnet *, struct mbuf *,
180 struct sockaddr *, struct rtentry *);
181 static int write_output (struct ifnet *, struct mbuf *,
182 struct sockaddr *, struct rtentry *);
187 #if (defined(__DragonFly__) || __FreeBSD_version >= 300000) && defined(_KERNEL)
188 struct callout ipfr_slowtimer_ch;
190 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
191 # include <sys/callout.h>
192 struct callout ipfr_slowtimer_ch;
194 #if defined(__OpenBSD__)
195 # include <sys/timeout.h>
196 struct timeout ipfr_slowtimer_ch;
198 #if defined(__sgi) && defined(_KERNEL)
199 toid_t ipfr_slowtimer_ch;
202 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
204 # include <sys/conf.h>
205 const struct cdevsw ipl_cdevsw = {
206 iplopen, iplclose, iplread, nowrite, iplioctl,
207 nostop, notty, nopoll, nommap,
211 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
212 # include <sys/device.h>
213 # include <sys/conf.h>
215 struct cfdriver iplcd = {
216 NULL, "ipl", NULL, NULL, DV_DULL, 0
219 struct devsw iplsw = {
221 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
222 nostrat, nodump, nopsize, 0,
225 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
227 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
228 (_BSDI_VERSION >= 199701) || (defined(__DragonFly_version) && \
229 (__DragonFly_version >= 100000))
230 # include <sys/conf.h>
231 # if defined(NETBSD_PF)
232 # include <net/pfil.h>
234 * We provide the fr_checkp name just to minimize changes later.
236 int (*fr_checkp) (ip_t *ip, int hlen, void *ifp, int out, mb_t **mp);
237 # endif /* NETBSD_PF */
238 #endif /* __NetBSD__ */
241 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
243 # include <net/pfil.h>
245 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
247 static int fr_check_wrapper(arg, mp, ifp, dir)
253 struct ip *ip = mtod(*mp, struct ip *);
254 int rv, hlen = ip->ip_hl << 2;
256 #if defined(M_CSUM_TCPv4)
258 * If the packet is out-bound, we can't delay checksums
259 * here. For in-bound, the checksum has already been
262 if (dir == PFIL_OUT) {
263 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
264 in_delayed_cksum(*mp);
265 (*mp)->m_pkthdr.csum_flags &=
266 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
269 #endif /* M_CSUM_TCPv4 */
272 * We get the packet with all fields in network byte
273 * order. We expect ip_len and ip_off to be in host
274 * order. We frob them, call the filter, then frob
277 * Note, we don't need to update the checksum, because
278 * it has already been verified.
280 ip->ip_len = ntohs(ip->ip_len);
281 ip->ip_off = ntohs(ip->ip_off);
283 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
285 if (rv == 0 && *mp != NULL) {
286 ip = mtod(*mp, struct ip *);
287 ip->ip_len = htons(ip->ip_len);
288 ip->ip_off = htnos(ip->ip_off);
295 # include <netinet/ip6.h>
297 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
299 static int fr_check_wrapper6(arg, mp, ifp, dir)
306 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
307 ifp, (dir == PFIL_OUT), mp));
310 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
311 #if defined(__DragonFly_version) && (__DragonFly_version >= 100000) && \
315 fr_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
317 struct ip *ip = mtod(*mp, struct ip *);
318 return fr_check(ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT), mp);
322 # include <netinet/ip6.h>
325 fr_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
327 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
328 ifp, (dir == PFIL_OUT), mp));
330 # endif /* USE_INET6 */
331 #endif /* __DragonFly_version >= 100000 && _KERNEL */
333 # if defined(IPFILTER_LKM) && !defined(__sgi)
337 if (strcmp(s, "ipl") == 0)
341 # endif /* IPFILTER_LKM */
345 * Try to detect the case when compiling for NetBSD with pseudo-device
347 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
349 ipfilterattach(count)
354 * Do nothing here, really. The filter will be enabled
355 * by the SIOCFRENB ioctl.
361 # if defined(__NetBSD__) || defined(__OpenBSD__)
369 # if defined(__sgi) || (defined(NETBSD_PF) && \
370 (__NetBSD_Version__ >= 104200000)) || \
371 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
374 #if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)) || \
375 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
376 struct pfil_head *ph_inet;
378 struct pfil_head *ph_inet6;
383 if (fr_running || (fr_checkp == fr_check)) {
384 printf("IP Filter: already initialized\n");
392 if (nat_init() == -1) {
396 if (fr_stateinit() == -1) {
400 if (appr_init() == -1) {
406 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
407 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
408 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
409 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
411 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
421 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
422 PFIL_IN|PFIL_OUT, ph_inet);
426 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
427 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
441 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
444 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
445 if (ph_inet6 != NULL)
446 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
447 PFIL_IN|PFIL_OUT, ph_inet6);
451 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
452 PFIL_IN|PFIL_OUT, ph_inet6);
454 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
455 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
457 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
458 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
471 error = ipfilter_sgi_attach();
481 bzero((char *)frcache, sizeof(frcache));
482 fr_savep = fr_checkp;
483 fr_checkp = fr_check;
487 if (fr_pass & FR_PASS)
489 else if (fr_pass & FR_BLOCK)
492 defpass = "no-match -> block";
494 printf("%s initialized. Default = %s all, Logging = %s\n",
495 ipfilter_version, defpass,
502 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
503 callout_init(&ipfr_slowtimer_ch);
504 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
506 # if defined(__OpenBSD__)
507 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
508 timeout_add(&ipfr_slowtimer_ch, hz/2);
510 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) || defined(__sgi)
511 callout_init(&ipfr_slowtimer_ch);
512 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
514 timeout(ipfr_slowtimer, NULL, hz/2);
524 * Disable the filter by removing the hooks from the IP input/output
527 # if defined(__NetBSD__)
534 #if defined(NETBSD_PF) && \
535 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
536 (defined(__DragonFly_version) && (__DragonFly_version >= 100000)))
538 # if (__NetBSD_Version__ >= 105150000) || (__DragonFly_version >= 100000)
539 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
541 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
547 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
548 callout_stop(&ipfr_slowtimer_ch);
550 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000)
551 callout_stop(&ipfr_slowtimer_ch);
554 untimeout(ipfr_slowtimer_ch);
556 # if defined(__OpenBSD__)
557 timeout_del(&ipfr_slowtimer_ch);
559 untimeout(ipfr_slowtimer, NULL);
560 # endif /* OpenBSD */
562 # endif /* FreeBSD */
568 printf("IP Filter: not initialized\n");
573 printf("%s unloaded\n", ipfilter_version);
575 fr_checkp = fr_savep;
576 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
577 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
581 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
582 (__DragonFly_version >= 100000))
583 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
585 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
586 PFIL_IN|PFIL_OUT, ph_inet);
590 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
591 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
598 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
601 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
602 if (ph_inet6 != NULL)
603 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
604 PFIL_IN|PFIL_OUT, ph_inet6);
608 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
609 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
619 ipfilter_sgi_detach();
634 static int frzerostats(data)
641 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
645 bzero((char *)frstats, sizeof(*frstats) * 2);
652 * Filter ioctl interface.
655 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
657 , cred_t *cp, int *rp
661 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
662 #if (defined(_KERNEL) && (defined(__DragonFly__) || defined(__FreeBSD__)))
665 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
666 (NetBSD >= 199511) || defined(__DragonFly__) || (__FreeBSD_version >= 220000) || \
667 defined(__OpenBSD__)))
674 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
675 (_BSDI_VERSION >= 199701) || (defined(__DragonFly__) || __FreeBSD_version >= 300000)
684 #if defined(_KERNEL) && !SOLARIS
687 int error = 0, unit = 0, tmp;
689 #if (BSD >= 199306) && defined(_KERNEL)
690 if ((securelevel >= 3) && (mode & FWRITE))
694 unit = GET_MINOR(dev);
695 if ((IPL_LOGMAX < unit) || (unit < 0))
701 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
706 if (unit == IPL_LOGNAT) {
708 error = nat_ioctl(data, cmd, mode);
714 if (unit == IPL_LOGSTATE) {
716 error = fr_state_ioctl(data, cmd, mode);
722 if (unit == IPL_LOGAUTH) {
726 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
727 if (!(mode & FWRITE)) {
730 error = frrequest(unit, cmd, data,
734 error = fr_auth_ioctl(data, mode, cmd);
743 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
744 sizeof(iplused[IPL_LOGIPF]));
747 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
752 if (!(mode & FWRITE))
755 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
759 # if defined(__NetBSD__) || defined(__OpenBSD__)
760 error = ipl_enable();
765 # if defined(__NetBSD__)
766 error = ipl_disable();
775 if (!(mode & FWRITE))
778 error = IRCOPY(data, (caddr_t)&fr_flags,
782 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
788 if (!(mode & FWRITE))
791 error = frrequest(unit, cmd, data, fr_active);
796 if (!(mode & FWRITE))
799 error = frrequest(unit, cmd, data, 1 - fr_active);
802 if (!(mode & FWRITE))
805 bzero((char *)frcache, sizeof(frcache[0]) * 2);
806 *(u_int *)data = fr_active;
807 fr_active = 1 - fr_active;
815 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
821 if (!(mode & FWRITE))
824 error = frzerostats(data);
827 if (!(mode & FWRITE))
830 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
832 tmp = frflush(unit, 4, tmp);
833 error = IWCOPY((caddr_t)&tmp, data,
840 if (!(mode & FWRITE))
843 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
845 tmp = frflush(unit, 6, tmp);
846 error = IWCOPY((caddr_t)&tmp, data,
853 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
864 if (!(mode & FWRITE))
867 *(int *)data = ipflog_clear(unit);
869 #endif /* IPFILTER_LOG */
871 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
877 if (!(mode & FWRITE))
880 #if defined(_KERNEL) && defined(__sgi)
895 void fr_forgetifp(ifp)
900 WRITE_ENTER(&ipf_mutex);
901 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
902 if (f->fr_ifa == ifp)
903 f->fr_ifa = (void *)-1;
904 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
905 if (f->fr_ifa == ifp)
906 f->fr_ifa = (void *)-1;
907 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
908 if (f->fr_ifa == ifp)
909 f->fr_ifa = (void *)-1;
910 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
911 if (f->fr_ifa == ifp)
912 f->fr_ifa = (void *)-1;
914 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
915 if (f->fr_ifa == ifp)
916 f->fr_ifa = (void *)-1;
917 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
918 if (f->fr_ifa == ifp)
919 f->fr_ifa = (void *)-1;
920 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
921 if (f->fr_ifa == ifp)
922 f->fr_ifa = (void *)-1;
923 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
924 if (f->fr_ifa == ifp)
925 f->fr_ifa = (void *)-1;
927 RWLOCK_EXIT(&ipf_mutex);
932 static int frrequest(unit, req, data, set)
934 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
942 frentry_t *fp, *f, **fprev;
944 frgroup_t *fg = NULL;
945 int error = 0, in, i;
952 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
956 #if (BSD >= 199306) && defined(_KERNEL)
957 if ((securelevel > 0) && (fp->fr_func != NULL))
962 * Check that the group number does exist and that if a head group
963 * has been specified, doesn't exist.
965 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
966 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
967 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
969 if ((req != SIOCZRLST) && fp->fr_group &&
970 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
973 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
975 if (unit == IPL_LOGAUTH)
976 ftail = fprev = &ipauth;
977 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
978 ftail = fprev = &ipacct[in][set];
979 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
980 ftail = fprev = &ipfilter[in][set];
982 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
983 ftail = fprev = &ipacct6[in][set];
984 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
985 ftail = fprev = &ipfilter6[in][set];
990 if ((group = fp->fr_group)) {
991 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
993 ftail = fprev = fg->fg_start;
996 bzero((char *)frcache, sizeof(frcache[0]) * 2);
998 for (i = 0; i < 4; i++) {
999 if ((fp->fr_ifnames[i][1] == '\0') &&
1000 ((fp->fr_ifnames[i][0] == '-') ||
1001 (fp->fr_ifnames[i][0] == '*'))) {
1002 fp->fr_ifas[i] = NULL;
1003 } else if (*fp->fr_ifnames[i]) {
1004 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1005 if (!fp->fr_ifas[i])
1006 fp->fr_ifas[i] = (void *)-1;
1011 fp->fr_flags &= ~FR_DUP;
1012 if (*fdp->fd_ifname) {
1013 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1015 fdp->fd_ifp = (struct ifnet *)-1;
1017 fp->fr_flags |= FR_DUP;
1021 if (*fdp->fd_ifname) {
1022 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1024 fdp->fd_ifp = (struct ifnet *)-1;
1028 * Look for a matching filter rule, but don't include the next or
1029 * interface pointer in the comparison (fr_next, fr_ifa).
1031 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1035 for (; (f = *ftail); ftail = &f->fr_next)
1036 if ((fp->fr_cksum == f->fr_cksum) &&
1037 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1041 * If zero'ing statistics, copy current to caller and zero.
1043 if (req == SIOCZRLST) {
1046 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1055 if (req != SIOCINAFR && req != SIOCINIFR)
1056 while ((f = *ftail))
1057 ftail = &f->fr_next;
1061 while (--fp->fr_hits && (f = *ftail))
1062 ftail = &f->fr_next;
1068 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1073 * Only return EBUSY if there is a group list, else
1074 * it's probably just state information referencing
1077 if ((f->fr_ref > 1) && f->fr_grp)
1079 if (fg && fg->fg_head)
1080 fg->fg_head->fr_ref--;
1081 if (unit == IPL_LOGAUTH) {
1082 return fr_preauthcmd(req, f, ftail);
1085 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1087 fixskip(fprev, f, -1);
1088 *ftail = f->fr_next;
1098 if (unit == IPL_LOGAUTH) {
1099 return fr_preauthcmd(req, fp, ftail);
1101 KMALLOC(f, frentry_t *);
1103 if (fg && fg->fg_head)
1104 fg->fg_head->fr_ref++;
1105 bcopy((char *)fp, (char *)f, sizeof(*f));
1108 f->fr_next = *ftail;
1110 if (req == SIOCINIFR || req == SIOCINAFR)
1111 fixskip(fprev, f, 1);
1113 if ((group = f->fr_grhead))
1114 fg = fr_addgroup(group, f, unit, set);
1125 * routines below for saving IP headers to buffer
1129 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1131 int IPL_EXTERN(open)(dev_t dev, int flags)
1134 int IPL_EXTERN(open)(dev, flags
1135 #if defined(__DragonFly__) || defined(__FreeBSD__)
1139 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1140 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1151 # if defined(__sgi) && defined(_KERNEL)
1152 u_int min = geteminor(*pdev);
1154 u_int min = GET_MINOR(dev);
1157 if (IPL_LOGMAX < min)
1166 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1168 int IPL_EXTERN(close)(dev, flags
1169 #if defined(__DragonFly__) || defined(__FreeBSD__)
1173 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1174 (defined(__DragonFly__) || __FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1185 u_int min = GET_MINOR(dev);
1187 if (IPL_LOGMAX < min)
1196 * both of these must operate with at least splnet() lest they be
1197 * called during packet processing and cause an inconsistancy to appear in
1201 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1204 int IPL_EXTERN(read)(dev, uio, ioflag)
1207 int IPL_EXTERN(read)(dev, uio)
1213 # ifdef IPFILTER_LOG
1214 return ipflog_read(GET_MINOR(dev), uio);
1222 * send_reset - this could conceivably be a call to tcp_respond(), but that
1223 * requires a large amount of setting up and isn't any more efficient.
1225 int send_reset(oip, fin)
1229 struct tcphdr *tcp, *tcp2;
1233 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1237 tcp = (struct tcphdr *)fin->fin_dp;
1238 if (tcp->th_flags & TH_RST)
1239 return -1; /* feedback loop */
1240 # if (BSD < 199306) || defined(__sgi)
1241 m = m_get(M_DONTWAIT, MT_HEADER);
1242 # elif defined(__DragonFly__)
1243 m = m_get(MB_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;
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 if (fr_makefrip(hlen, ip, &frn) == 0)
1369 error = ipfr_fastroute(m, mp, &frn, NULL);
1376 int send_icmp_err(oip, type, fin, dst)
1382 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1383 u_short shlen, slen = 0, soff = 0;
1384 struct in_addr dst4;
1389 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1390 struct in6_addr dst6;
1394 if ((type < 0) || (type > ICMP_MAXTYPE))
1397 code = fin->fin_icode;
1399 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1406 if (fin->fin_v == 4) {
1407 if ((oip->ip_p == IPPROTO_ICMP) &&
1408 !(fin->fin_fi.fi_fl & FI_SHORT))
1409 switch (ntohs(fin->fin_data[0]) >> 8)
1420 # if (BSD < 199306) || defined(__sgi)
1422 m = m_get(M_DONTWAIT, MT_HEADER);
1423 # elif defined(__DragonFly__)
1425 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1428 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1434 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1437 dst4.s_addr = oip->ip_dst.s_addr;
1439 hlen = sizeof(ip_t);
1440 ohlen = oip->ip_hl << 2;
1445 else if (fin->fin_v == 6) {
1446 hlen = sizeof(ip6_t);
1447 ohlen = sizeof(ip6_t);
1448 type = icmptoicmp6types[type];
1449 if (type == ICMP6_DST_UNREACH)
1450 code = icmptoicmp6unreach[code];
1452 #ifdef __DragonFly__
1453 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1455 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1460 #ifdef __DragonFly__
1461 MCLGET(m, MB_DONTWAIT);
1463 MCLGET(m, M_DONTWAIT);
1465 if ((m->m_flags & M_EXT) == 0) {
1469 # ifdef M_TRAILINGSPACE
1471 avail = M_TRAILINGSPACE(m);
1473 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1475 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1476 avail - hlen - sizeof(*icmp) - max_linkhdr);
1478 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1481 dst6 = oip6->ip6_dst;
1485 iclen = hlen + sizeof(*icmp);
1487 avail -= (max_linkhdr + iclen);
1488 m->m_data += max_linkhdr;
1489 m->m_pkthdr.rcvif = (struct ifnet *)0;
1493 m->m_pkthdr.len = iclen;
1495 avail -= (m->m_off + iclen);
1501 ip = mtod(m, ip_t *);
1502 icmp = (struct icmp *)((char *)ip + hlen);
1503 bzero((char *)ip, iclen);
1505 icmp->icmp_type = type;
1506 icmp->icmp_code = fin->fin_icode;
1507 icmp->icmp_cksum = 0;
1509 if (type == ICMP_UNREACH &&
1510 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1511 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1516 oip->ip_len = htons(oip->ip_len);
1518 oip->ip_off = htons(oip->ip_off);
1519 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1522 avail -= MIN(ohlen, avail);
1527 if (fin->fin_v == 6) {
1529 ip6->ip6_plen = htons(iclen - hlen);
1530 ip6->ip6_nxt = IPPROTO_ICMPV6;
1532 ip6->ip6_src = dst6;
1533 ip6->ip6_dst = oip6->ip6_src;
1535 bcopy((char *)oip + ohlen,
1536 (char *)&icmp->icmp_ip + ohlen, avail);
1537 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1538 sizeof(*ip6), iclen - hlen);
1543 ip->ip_src.s_addr = dst4.s_addr;
1544 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1549 bcopy((char *)oip + ohlen,
1550 (char *)&icmp->icmp_ip + ohlen, avail);
1551 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1554 ip->ip_p = IPPROTO_ICMP;
1557 shlen = fin->fin_hlen;
1558 fin->fin_hlen = hlen;
1559 err = send_ip(oip, fin, &m);
1560 fin->fin_hlen = shlen;
1566 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1567 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1573 void iplinit (void);
1580 # if defined(__NetBSD__) || defined(__OpenBSD__)
1581 if (ipl_enable() != 0)
1583 if (iplattach() != 0)
1586 printf("IP Filter failed to attach\n");
1590 # endif /* ! __NetBSD__ */
1594 * Return the length of the entire mbuf.
1596 size_t mbufchainlen(m0)
1600 return m0->m_pkthdr.len;
1604 for (; m0; m0 = m0->m_next)
1611 int ipfr_fastroute(m0, mpp, fin, fdp)
1612 struct mbuf *m0, **mpp;
1616 struct ip *ip, *mhip;
1617 struct mbuf *m = m0;
1619 int len, off, error = 0, hlen, code, sout;
1620 struct ifnet *ifp, *sifp;
1621 struct sockaddr_in *dst;
1622 struct route iproute;
1632 if (fin->fin_v == 6) {
1633 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1639 if (fin->fin_v == 6)
1647 * If the mbuf we're about to send is not writable (because of
1648 * a cluster reference, for example) we'll need to make a copy
1649 * of it since this routine modifies the contents.
1651 * If you have non-crappy network hardware that can transmit data
1652 * from the mbuf, rather than making a copy, this is gonna be a
1655 if (M_WRITABLE(m) == 0) {
1656 #ifdef __DragonFly__
1657 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1659 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1672 hlen = fin->fin_hlen;
1673 ip = mtod(m0, struct ip *);
1675 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1677 * Clear any in-bound checksum flags for this packet.
1679 # if (__NetBSD_Version__ > 105009999)
1680 m0->m_pkthdr.csum_flags = 0;
1682 m0->m_pkthdr.csuminfo = 0;
1684 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1689 #if (defined(IRIX) && (IRIX >= 605))
1692 bzero((caddr_t)ro, sizeof (*ro));
1693 dst = (struct sockaddr_in *)&ro->ro_dst;
1694 dst->sin_family = AF_INET;
1695 dst->sin_addr = ip->ip_dst;
1704 * In case we're here due to "to <if>" being used with "keep state",
1705 * check that we're going in the correct direction.
1707 if ((fr != NULL) && (fin->fin_rev != 0)) {
1708 if ((ifp != NULL) && (fdp == &fr->fr_tif)) {
1709 # if (defined(IRIX) && (IRIX >= 605))
1714 } else if (fdp != NULL) {
1715 if (fdp->fd_ip.s_addr != 0)
1716 dst->sin_addr = fdp->fd_ip;
1720 dst->sin_len = sizeof(*dst);
1722 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1723 !defined(__OpenBSD__)
1725 rtalloc_ign(ro, RTF_CLONING);
1727 rtalloc_ign(ro, RTF_PRCLONING);
1734 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1736 # if (defined(IRIX) && (IRIX >= 605))
1743 if ((ifp == NULL) && (ro->ro_rt != NULL))
1744 ifp = ro->ro_rt->rt_ifp;
1746 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1747 if (in_localaddr(ip->ip_dst))
1748 error = EHOSTUNREACH;
1750 error = ENETUNREACH;
1751 # if (defined(IRIX) && (IRIX >= 605))
1757 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1758 #if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1759 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1761 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1764 ro->ro_rt->rt_use++;
1766 #if (defined(IRIX) && (IRIX > 602))
1771 * For input packets which are being "fastrouted", they won't
1772 * go back through output filtering and miss their chance to get
1773 * NAT'd and counted.
1775 if (fin->fin_out == 0) {
1776 sifp = fin->fin_ifp;
1777 sout = fin->fin_out;
1780 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1781 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1782 ATOMIC_INCL(frstats[1].fr_acct);
1785 if (!fr || !(fr->fr_flags & FR_RETMASK))
1786 (void) fr_checkstate(ip, fin);
1788 switch (ip_natout(ip, fin))
1801 fin->fin_ifp = sifp;
1802 fin->fin_out = sout;
1807 * If small enough for interface, can just send directly.
1809 if (ip->ip_len <= ifp->if_mtu) {
1811 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1812 !(__NetBSD_Version__ >= 105110000)
1813 ip->ip_id = htons(ip->ip_id);
1815 ip->ip_len = htons(ip->ip_len);
1816 ip->ip_off = htons(ip->ip_off);
1818 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1819 # if (__NetBSD_Version__ > 105009999)
1820 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1821 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1822 else if (ip->ip_sum == 0)
1823 ip->ip_sum = in_cksum(m, hlen);
1825 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1826 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1827 else if (ip->ip_sum == 0)
1828 ip->ip_sum = in_cksum(m, hlen);
1832 ip->ip_sum = in_cksum(m, hlen);
1833 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1834 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1836 IFNET_UPPERLOCK(ifp);
1838 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1841 IFNET_UPPERUNLOCK(ifp);
1844 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1850 * Too large for interface; fragment if possible.
1851 * Must be able to put at least 8 bytes per fragment.
1853 if (ip->ip_off & IP_DF) {
1857 len = (ifp->if_mtu - hlen) &~ 7;
1864 int mhlen, firstlen = len;
1865 struct mbuf **mnext = &m->m_act;
1868 * Loop through length of segment after first fragment,
1869 * make new header and copy data of each part and link onto chain.
1872 mhlen = sizeof (struct ip);
1873 for (off = hlen + len; off < ip->ip_len; off += len) {
1874 # ifdef __DragonFly__
1875 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1876 # elif defined(MGETHDR)
1877 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1879 MGET(m, M_DONTWAIT, MT_HEADER);
1886 m->m_data += max_linkhdr;
1888 m->m_off = MMAXOFF - hlen;
1890 mhip = mtod(m, struct ip *);
1891 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1892 if (hlen > sizeof (struct ip)) {
1893 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1894 mhip->ip_hl = mhlen >> 2;
1897 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1898 if (ip->ip_off & IP_MF)
1899 mhip->ip_off |= IP_MF;
1900 if (off + len >= ip->ip_len)
1901 len = ip->ip_len - off;
1903 mhip->ip_off |= IP_MF;
1904 mhip->ip_len = htons((u_short)(len + mhlen));
1905 m->m_next = m_copy(m0, off, len);
1906 if (m->m_next == 0) {
1907 error = ENOBUFS; /* ??? */
1911 m->m_pkthdr.len = mhlen + len;
1912 m->m_pkthdr.rcvif = NULL;
1914 mhip->ip_off = htons((u_short)mhip->ip_off);
1916 mhip->ip_sum = in_cksum(m, mhlen);
1921 * Update first fragment by trimming what's been copied out
1922 * and updating header, then send each fragment (in order).
1924 m_adj(m0, hlen + firstlen - ip->ip_len);
1925 ip->ip_len = htons((u_short)(hlen + firstlen));
1926 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1928 ip->ip_sum = in_cksum(m0, hlen);
1930 for (m = m0; m; m = m0) {
1934 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1935 error = (*ifp->if_output)(ifp, m,
1936 (struct sockaddr *)dst, ro->ro_rt);
1938 error = (*ifp->if_output)(ifp, m,
1939 (struct sockaddr *)dst);
1951 if (ro->ro_rt != NULL) {
1957 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1958 sifp = fin->fin_ifp;
1959 code = fin->fin_icode;
1960 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1962 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1963 fin->fin_ifp = sifp;
1964 fin->fin_icode = code;
1972 * Return true or false depending on whether the route to the
1973 * given IP address uses the same interface as the one passed.
1975 int fr_verifysrc(ipa, ifp)
1979 struct sockaddr_in *dst;
1980 struct route iproute;
1982 bzero((char *)&iproute, sizeof(iproute));
1983 dst = (struct sockaddr_in *)&iproute.ro_dst;
1984 # if (BSD >= 199306)
1985 dst->sin_len = sizeof(*dst);
1987 dst->sin_family = AF_INET;
1988 dst->sin_addr = ipa;
1989 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1990 !defined(__OpenBSD__)
1992 rtalloc_ign(&iproute, RTF_CLONING);
1994 rtalloc_ign(&iproute, RTF_PRCLONING);
1999 if (iproute.ro_rt == NULL)
2001 return (ifp == iproute.ro_rt->rt_ifp);
2005 # ifdef USE_GETIFNAME
2010 static char workbuf[64];
2012 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
2018 # if defined(USE_INET6)
2020 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
2021 * or ensure that it is an IPv6 packet that is being forwarded, those are
2022 * expected to be done by the called (ipfr_fastroute).
2024 static int ipfr_fastroute6(m0, mpp, fin, fdp)
2025 struct mbuf *m0, **mpp;
2029 struct route_in6 ip6route;
2030 struct sockaddr_in6 *dst6;
2031 struct route_in6 *ro;
2034 #if defined(OpenBSD) && (OpenBSD >= 200211)
2035 struct route_in6 *ro_pmtu = NULL;
2036 struct in6_addr finaldst;
2044 bzero((caddr_t)ro, sizeof(*ro));
2045 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2046 dst6->sin6_family = AF_INET6;
2047 dst6->sin6_len = sizeof(struct sockaddr_in6);
2048 dst6->sin6_addr = fin->fin_fi.fi_dst.in6;
2055 if ((fr != NULL) && (fin->fin_rev != 0)) {
2056 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2058 } else if (fdp != NULL) {
2059 if (IP6_NOTZERO(&fdp->fd_ip6))
2060 dst6->sin6_addr = fdp->fd_ip6.in6;
2065 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
2067 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
2068 dst6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2070 rtalloc((struct route *)ro);
2072 if ((ifp == NULL) && (ro->ro_rt != NULL))
2073 ifp = ro->ro_rt->rt_ifp;
2075 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2076 (ifp != ro->ro_rt->rt_ifp)) {
2077 error = EHOSTUNREACH;
2079 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2080 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2081 ro->ro_rt->rt_use++;
2083 #if defined(OpenBSD) && (OpenBSD >= 200211)
2084 ip6 = mtod(m0, ip6_t *);
2086 finaldst = ip6->ip6_dst;
2087 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2090 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2092 if (m0->m_pkthdr.len <= mtu)
2093 error = nd6_output(ifp, fin->fin_ifp, m0,
2097 #if defined(OpenBSD) && (OpenBSD >= 200211)
2102 if (ro->ro_rt != NULL) {
2108 #else /* #ifdef _KERNEL */
2111 # if defined(__sgi) && (IRIX < 605)
2112 static int no_output (struct ifnet *ifp, struct mbuf *m,
2115 static int no_output (struct ifnet *ifp, struct mbuf *m,
2116 struct sockaddr *s, struct rtentry *rt)
2124 # if defined(__sgi) && (IRIX < 605)
2125 static int write_output (struct ifnet *ifp, struct mbuf *m,
2128 static int write_output (struct ifnet *ifp, struct mbuf *m,
2129 struct sockaddr *s, struct rtentry *rt)
2132 ip_t *ip = (ip_t *)m;
2134 static int write_output(ifp, ip)
2142 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2143 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2144 (defined(__DragonFly__))
2145 sprintf(fname, "%s", ifp->if_xname);
2147 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2149 fd = open(fname, O_WRONLY|O_APPEND);
2154 write(fd, (char *)ip, ntohs(ip->ip_len));
2160 char *get_ifname(ifp)
2163 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2164 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2165 (defined(__DragonFly__))
2166 return ifp->if_xname;
2168 static char fullifname[LIFNAMSIZ];
2170 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2176 struct ifnet *get_unit(ifname, v)
2180 struct ifnet *ifp, **ifa, **old_ifneta;
2182 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2183 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2184 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2185 (defined(__DragonFly__))
2186 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2188 char fullname[LIFNAMSIZ];
2190 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2191 if (!strcmp(ifname, fullname))
2197 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2201 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2208 old_ifneta = ifneta;
2210 ifneta = (struct ifnet **)realloc(ifneta,
2211 (nifs + 1) * sizeof(*ifa));
2217 ifneta[nifs] = NULL;
2218 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2219 if (!ifneta[nifs - 1]) {
2224 ifp = ifneta[nifs - 1];
2226 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2227 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2228 (defined(__DragonFly__))
2229 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2231 ifp->if_name = strdup(ifname);
2233 ifname = ifp->if_name;
2234 while (*ifname && !isdigit(*ifname))
2236 if (*ifname && isdigit(*ifname)) {
2237 ifp->if_unit = atoi(ifname);
2242 ifp->if_output = no_output;
2250 struct ifnet *ifp, **ifa;
2254 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2255 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2256 (defined(__DragonFly__))
2257 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2258 ifp->if_output = write_output;
2259 sprintf(fname, "/tmp/%s", ifp->if_xname);
2260 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2268 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2269 ifp->if_output = write_output;
2270 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2271 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2281 int send_reset(ip, fin)
2285 verbose("- TCP RST sent\n");
2290 int send_icmp_err(ip, code, fin, dst)
2296 verbose("- ICMP UNREACHABLE sent\n");
2306 void m_copydata(m, off, len, cp)
2311 bcopy((char *)m + off, cp, len);
2315 int ipfuiomove(buf, len, rwflag, uio)
2320 int left, ioc, num, offset;
2324 if (rwflag == UIO_READ) {
2328 offset = uio->uio_offset;
2330 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2331 io = uio->uio_iov + ioc;
2335 start = (char *)io->iov_base + offset;
2336 if (start > (char *)io->iov_base + io->iov_len) {
2337 offset -= io->iov_len;
2341 bcopy(buf, start, num);
2342 uio->uio_resid -= num;
2343 uio->uio_offset += num;
2353 #endif /* _KERNEL */