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.5 2003/08/07 21:16:48 dillon Exp $
12 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
15 #if defined(KERNEL) && !defined(_KERNEL)
18 #if defined(_KERNEL) && 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 __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 (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 __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(__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 __P((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 <machine/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 __P((caddr_t));
154 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
155 static int frrequest __P((int, u_long, caddr_t, int));
157 static int frrequest __P((int, int, caddr_t, int));
160 static int (*fr_savep) __P((ip_t *, int, void *, int, struct mbuf **));
161 static int send_ip __P((ip_t *, fr_info_t *, struct mbuf **));
163 static int ipfr_fastroute6 __P((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 __P((void));
173 # if defined(__sgi) && (IRIX < 605)
174 static int no_output __P((struct ifnet *, struct mbuf *,
176 static int write_output __P((struct ifnet *, struct mbuf *,
179 static int no_output __P((struct ifnet *, struct mbuf *,
180 struct sockaddr *, struct rtentry *));
181 static int write_output __P((struct ifnet *, struct mbuf *,
182 struct sockaddr *, struct rtentry *));
187 #if (__FreeBSD_version >= 300000) && defined(_KERNEL)
188 struct callout_handle ipfr_slowtimer_ch;
190 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
191 # include <sys/callout.h>
192 struct callout ipfr_slowtimer_ch;
194 #if defined(__OpenBSD__)
195 # include <sys/timeout.h>
196 struct timeout ipfr_slowtimer_ch;
198 #if defined(__sgi) && defined(_KERNEL)
199 toid_t ipfr_slowtimer_ch;
202 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
204 # include <sys/conf.h>
205 const struct cdevsw ipl_cdevsw = {
206 iplopen, iplclose, iplread, nowrite, iplioctl,
207 nostop, notty, nopoll, nommap,
211 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
212 # include <sys/device.h>
213 # include <sys/conf.h>
215 struct cfdriver iplcd = {
216 NULL, "ipl", NULL, NULL, DV_DULL, 0
219 struct devsw iplsw = {
221 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
222 nostrat, nodump, nopsize, 0,
225 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
227 #if defined(__NetBSD__) || defined(__OpenBSD__) || (_BSDI_VERSION >= 199701)
228 # include <sys/conf.h>
229 # if defined(NETBSD_PF)
230 # include <net/pfil.h>
232 * We provide the fr_checkp name just to minimize changes later.
234 int (*fr_checkp) __P((ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
235 # endif /* NETBSD_PF */
236 #endif /* __NetBSD__ */
239 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
241 # include <net/pfil.h>
243 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
245 static int fr_check_wrapper(arg, mp, ifp, dir)
251 struct ip *ip = mtod(*mp, struct ip *);
252 int rv, hlen = ip->ip_hl << 2;
254 #if defined(M_CSUM_TCPv4)
256 * If the packet is out-bound, we can't delay checksums
257 * here. For in-bound, the checksum has already been
260 if (dir == PFIL_OUT) {
261 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
262 in_delayed_cksum(*mp);
263 (*mp)->m_pkthdr.csum_flags &=
264 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
267 #endif /* M_CSUM_TCPv4 */
270 * We get the packet with all fields in network byte
271 * order. We expect ip_len and ip_off to be in host
272 * order. We frob them, call the filter, then frob
275 * Note, we don't need to update the checksum, because
276 * it has already been verified.
281 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
283 if (rv == 0 && *mp != NULL) {
284 ip = mtod(*mp, struct ip *);
293 # include <netinet/ip6.h>
295 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
297 static int fr_check_wrapper6(arg, mp, ifp, dir)
304 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
305 ifp, (dir == PFIL_OUT), mp));
308 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
310 # if defined(IPFILTER_LKM) && !defined(__sgi)
314 if (strcmp(s, "ipl") == 0)
318 # endif /* IPFILTER_LKM */
322 * Try to detect the case when compiling for NetBSD with pseudo-device
324 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
326 ipfilterattach(count)
331 * Do nothing here, really. The filter will be enabled
332 * by the SIOCFRENB ioctl.
338 # if defined(__NetBSD__) || defined(__OpenBSD__)
346 # if defined(__sgi) || (defined(NETBSD_PF) && (__NetBSD_Version__ >= 104200000))
349 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
350 struct pfil_head *ph_inet;
352 struct pfil_head *ph_inet6;
357 if (fr_running || (fr_checkp == fr_check)) {
358 printf("IP Filter: already initialized\n");
366 if (nat_init() == -1) {
370 if (fr_stateinit() == -1) {
374 if (appr_init() == -1) {
380 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011)
381 # if __NetBSD_Version__ >= 105110000
382 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
384 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
394 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
395 PFIL_IN|PFIL_OUT, ph_inet);
399 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
400 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
414 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
417 # if __NetBSD_Version__ >= 105110000
418 if (ph_inet6 != NULL)
419 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
420 PFIL_IN|PFIL_OUT, ph_inet6);
424 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
425 PFIL_IN|PFIL_OUT, ph_inet6);
427 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
428 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
430 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
431 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
444 error = ipfilter_sgi_attach();
454 bzero((char *)frcache, sizeof(frcache));
455 fr_savep = fr_checkp;
456 fr_checkp = fr_check;
460 if (fr_pass & FR_PASS)
462 else if (fr_pass & FR_BLOCK)
465 defpass = "no-match -> block";
467 printf("%s initialized. Default = %s all, Logging = %s\n",
468 ipfilter_version, defpass,
475 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
476 callout_init(&ipfr_slowtimer_ch);
477 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
479 # if defined(__OpenBSD__)
480 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
481 timeout_add(&ipfr_slowtimer_ch, hz/2);
483 # if (__FreeBSD_version >= 300000) || defined(__sgi)
484 ipfr_slowtimer_ch = timeout(ipfr_slowtimer, NULL, hz/2);
486 timeout(ipfr_slowtimer, NULL, hz/2);
496 * Disable the filter by removing the hooks from the IP input/output
499 # if defined(__NetBSD__)
506 #if defined(NETBSD_PF) && \
507 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
509 # if __NetBSD_Version__ >= 105150000
510 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
512 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
518 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
519 callout_stop(&ipfr_slowtimer_ch);
521 # if (__FreeBSD_version >= 300000)
522 untimeout(ipfr_slowtimer, NULL, ipfr_slowtimer_ch);
525 untimeout(ipfr_slowtimer_ch);
527 # if defined(__OpenBSD__)
528 timeout_del(&ipfr_slowtimer_ch);
530 untimeout(ipfr_slowtimer, NULL);
531 # endif /* OpenBSD */
533 # endif /* FreeBSD */
539 printf("IP Filter: not initialized\n");
544 printf("%s unloaded\n", ipfilter_version);
546 fr_checkp = fr_savep;
547 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
548 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
552 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011))
553 # if __NetBSD_Version__ >= 105110000
555 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
556 PFIL_IN|PFIL_OUT, ph_inet);
560 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
561 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
568 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
571 # if __NetBSD_Version__ >= 105110000
572 if (ph_inet6 != NULL)
573 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
574 PFIL_IN|PFIL_OUT, ph_inet6);
578 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
579 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
589 ipfilter_sgi_detach();
604 static int frzerostats(data)
611 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
615 bzero((char *)frstats, sizeof(*frstats) * 2);
622 * Filter ioctl interface.
625 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
627 , cred_t *cp, int *rp
631 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
635 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
636 (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
637 defined(__OpenBSD__)))
644 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
645 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
654 #if defined(_KERNEL) && !SOLARIS
657 int error = 0, unit = 0, tmp;
659 #if (BSD >= 199306) && defined(_KERNEL)
660 if ((securelevel >= 3) && (mode & FWRITE))
664 unit = GET_MINOR(dev);
665 if ((IPL_LOGMAX < unit) || (unit < 0))
671 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
676 if (unit == IPL_LOGNAT) {
678 error = nat_ioctl(data, cmd, mode);
684 if (unit == IPL_LOGSTATE) {
686 error = fr_state_ioctl(data, cmd, mode);
692 if (unit == IPL_LOGAUTH) {
696 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
697 if (!(mode & FWRITE)) {
700 error = frrequest(unit, cmd, data,
704 error = fr_auth_ioctl(data, mode, cmd);
713 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
714 sizeof(iplused[IPL_LOGIPF]));
717 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
722 if (!(mode & FWRITE))
725 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
729 # if defined(__NetBSD__) || defined(__OpenBSD__)
730 error = ipl_enable();
735 # if defined(__NetBSD__)
736 error = ipl_disable();
745 if (!(mode & FWRITE))
748 error = IRCOPY(data, (caddr_t)&fr_flags,
752 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
758 if (!(mode & FWRITE))
761 error = frrequest(unit, cmd, data, fr_active);
766 if (!(mode & FWRITE))
769 error = frrequest(unit, cmd, data, 1 - fr_active);
772 if (!(mode & FWRITE))
775 bzero((char *)frcache, sizeof(frcache[0]) * 2);
776 *(u_int *)data = fr_active;
777 fr_active = 1 - fr_active;
785 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
791 if (!(mode & FWRITE))
794 error = frzerostats(data);
797 if (!(mode & FWRITE))
800 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
802 tmp = frflush(unit, 4, tmp);
803 error = IWCOPY((caddr_t)&tmp, data,
810 if (!(mode & FWRITE))
813 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
815 tmp = frflush(unit, 6, tmp);
816 error = IWCOPY((caddr_t)&tmp, data,
823 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
834 if (!(mode & FWRITE))
837 *(int *)data = ipflog_clear(unit);
839 #endif /* IPFILTER_LOG */
841 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
847 if (!(mode & FWRITE))
850 #if defined(_KERNEL) && defined(__sgi)
865 void fr_forgetifp(ifp)
870 WRITE_ENTER(&ipf_mutex);
871 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
872 if (f->fr_ifa == ifp)
873 f->fr_ifa = (void *)-1;
874 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
875 if (f->fr_ifa == ifp)
876 f->fr_ifa = (void *)-1;
877 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
878 if (f->fr_ifa == ifp)
879 f->fr_ifa = (void *)-1;
880 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
881 if (f->fr_ifa == ifp)
882 f->fr_ifa = (void *)-1;
884 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
885 if (f->fr_ifa == ifp)
886 f->fr_ifa = (void *)-1;
887 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
888 if (f->fr_ifa == ifp)
889 f->fr_ifa = (void *)-1;
890 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
891 if (f->fr_ifa == ifp)
892 f->fr_ifa = (void *)-1;
893 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
894 if (f->fr_ifa == ifp)
895 f->fr_ifa = (void *)-1;
897 RWLOCK_EXIT(&ipf_mutex);
902 static int frrequest(unit, req, data, set)
904 #if defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
912 frentry_t *fp, *f, **fprev;
914 frgroup_t *fg = NULL;
915 int error = 0, in, i;
922 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
926 #if (BSD >= 199306) && defined(_KERNEL)
927 if ((securelevel > 0) && (fp->fr_func != NULL))
932 * Check that the group number does exist and that if a head group
933 * has been specified, doesn't exist.
935 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
936 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
937 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
939 if ((req != SIOCZRLST) && fp->fr_group &&
940 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
943 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
945 if (unit == IPL_LOGAUTH)
946 ftail = fprev = &ipauth;
947 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
948 ftail = fprev = &ipacct[in][set];
949 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
950 ftail = fprev = &ipfilter[in][set];
952 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
953 ftail = fprev = &ipacct6[in][set];
954 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
955 ftail = fprev = &ipfilter6[in][set];
960 if ((group = fp->fr_group)) {
961 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
963 ftail = fprev = fg->fg_start;
966 bzero((char *)frcache, sizeof(frcache[0]) * 2);
968 for (i = 0; i < 4; i++) {
969 if ((fp->fr_ifnames[i][1] == '\0') &&
970 ((fp->fr_ifnames[i][0] == '-') ||
971 (fp->fr_ifnames[i][0] == '*'))) {
972 fp->fr_ifas[i] = NULL;
973 } else if (*fp->fr_ifnames[i]) {
974 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
976 fp->fr_ifas[i] = (void *)-1;
981 fp->fr_flags &= ~FR_DUP;
982 if (*fdp->fd_ifname) {
983 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
985 fdp->fd_ifp = (struct ifnet *)-1;
987 fp->fr_flags |= FR_DUP;
991 if (*fdp->fd_ifname) {
992 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
994 fdp->fd_ifp = (struct ifnet *)-1;
998 * Look for a matching filter rule, but don't include the next or
999 * interface pointer in the comparison (fr_next, fr_ifa).
1001 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1005 for (; (f = *ftail); ftail = &f->fr_next)
1006 if ((fp->fr_cksum == f->fr_cksum) &&
1007 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1011 * If zero'ing statistics, copy current to caller and zero.
1013 if (req == SIOCZRLST) {
1016 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1025 if (req != SIOCINAFR && req != SIOCINIFR)
1026 while ((f = *ftail))
1027 ftail = &f->fr_next;
1031 while (--fp->fr_hits && (f = *ftail))
1032 ftail = &f->fr_next;
1038 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1043 * Only return EBUSY if there is a group list, else
1044 * it's probably just state information referencing
1047 if ((f->fr_ref > 1) && f->fr_grp)
1049 if (fg && fg->fg_head)
1050 fg->fg_head->fr_ref--;
1051 if (unit == IPL_LOGAUTH) {
1052 return fr_preauthcmd(req, f, ftail);
1055 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1057 fixskip(fprev, f, -1);
1058 *ftail = f->fr_next;
1068 if (unit == IPL_LOGAUTH) {
1069 return fr_preauthcmd(req, fp, ftail);
1071 KMALLOC(f, frentry_t *);
1073 if (fg && fg->fg_head)
1074 fg->fg_head->fr_ref++;
1075 bcopy((char *)fp, (char *)f, sizeof(*f));
1078 f->fr_next = *ftail;
1080 if (req == SIOCINIFR || req == SIOCINAFR)
1081 fixskip(fprev, f, 1);
1083 if ((group = f->fr_grhead))
1084 fg = fr_addgroup(group, f, unit, set);
1095 * routines below for saving IP headers to buffer
1099 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1101 int IPL_EXTERN(open)(dev_t dev, int flags)
1104 int IPL_EXTERN(open)(dev, flags
1109 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1110 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1121 # if defined(__sgi) && defined(_KERNEL)
1122 u_int min = geteminor(*pdev);
1124 u_int min = GET_MINOR(dev);
1127 if (IPL_LOGMAX < min)
1136 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1138 int IPL_EXTERN(close)(dev, flags
1143 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1144 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1155 u_int min = GET_MINOR(dev);
1157 if (IPL_LOGMAX < min)
1166 * both of these must operate with at least splnet() lest they be
1167 * called during packet processing and cause an inconsistancy to appear in
1171 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1174 int IPL_EXTERN(read)(dev, uio, ioflag)
1177 int IPL_EXTERN(read)(dev, uio)
1183 # ifdef IPFILTER_LOG
1184 return ipflog_read(GET_MINOR(dev), uio);
1192 * send_reset - this could conceivably be a call to tcp_respond(), but that
1193 * requires a large amount of setting up and isn't any more efficient.
1195 int send_reset(oip, fin)
1199 struct tcphdr *tcp, *tcp2;
1203 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1207 tcp = (struct tcphdr *)fin->fin_dp;
1208 if (tcp->th_flags & TH_RST)
1209 return -1; /* feedback loop */
1210 # if (BSD < 199306) || defined(__sgi)
1211 m = m_get(M_DONTWAIT, MT_HEADER);
1213 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1220 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1221 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1222 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1225 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1227 hlen = sizeof(ip_t);
1229 m->m_len = sizeof(*tcp2) + hlen;
1231 m->m_data += max_linkhdr;
1232 m->m_pkthdr.len = m->m_len;
1233 m->m_pkthdr.rcvif = (struct ifnet *)0;
1235 ip = mtod(m, struct ip *);
1239 bzero((char *)ip, sizeof(*tcp2) + hlen);
1240 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1242 tcp2->th_sport = tcp->th_dport;
1243 tcp2->th_dport = tcp->th_sport;
1244 if (tcp->th_flags & TH_ACK) {
1245 tcp2->th_seq = tcp->th_ack;
1246 tcp2->th_flags = TH_RST;
1248 tcp2->th_ack = ntohl(tcp->th_seq);
1249 tcp2->th_ack += tlen;
1250 tcp2->th_ack = htonl(tcp2->th_ack);
1251 tcp2->th_flags = TH_RST|TH_ACK;
1253 tcp2->th_off = sizeof(*tcp2) >> 2;
1255 if (fin->fin_v == 6) {
1256 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1257 ip6->ip6_nxt = IPPROTO_TCP;
1258 ip6->ip6_src = oip6->ip6_dst;
1259 ip6->ip6_dst = oip6->ip6_src;
1260 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1261 sizeof(*ip6), sizeof(*tcp2));
1262 return send_ip(oip, fin, &m);
1265 ip->ip_p = IPPROTO_TCP;
1266 ip->ip_len = htons(sizeof(struct tcphdr));
1267 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1268 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1269 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1270 ip->ip_len = hlen + sizeof(*tcp2);
1271 return send_ip(oip, fin, &m);
1276 * Send an IP(v4/v6) datagram out into the network
1278 static int send_ip(oip, fin, mp)
1283 struct mbuf *m = *mp;
1288 bzero((char *)&frn, sizeof(frn));
1289 frn.fin_ifp = fin->fin_ifp;
1290 frn.fin_v = fin->fin_v;
1291 frn.fin_out = fin->fin_out;
1292 frn.fin_mp = fin->fin_mp;
1294 ip = mtod(m, ip_t *);
1297 ip->ip_v = fin->fin_v;
1298 if (ip->ip_v == 4) {
1299 ip->ip_hl = (sizeof(*oip) >> 2);
1300 ip->ip_v = IPVERSION;
1301 ip->ip_tos = oip->ip_tos;
1302 ip->ip_id = oip->ip_id;
1304 # if defined(__NetBSD__) || \
1305 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1306 if (ip_mtudisc != 0)
1310 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1315 # if (BSD < 199306) || defined(__sgi)
1316 ip->ip_ttl = tcp_ttl;
1318 ip->ip_ttl = ip_defttl;
1321 frn.fin_dp = (char *)(ip + 1);
1324 else if (ip->ip_v == 6) {
1325 ip6_t *ip6 = (ip6_t *)ip;
1327 hlen = sizeof(*ip6);
1328 ip6->ip6_hlim = 127;
1329 frn.fin_dp = (char *)(ip6 + 1);
1333 m->m_pkthdr.rcvif = NULL;
1336 fr_makefrip(hlen, ip, &frn);
1338 error = ipfr_fastroute(m, mp, &frn, NULL);
1343 int send_icmp_err(oip, type, fin, dst)
1349 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1350 u_short shlen, slen = 0, soff = 0;
1351 struct in_addr dst4;
1356 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1357 struct in6_addr dst6;
1361 if ((type < 0) || (type > ICMP_MAXTYPE))
1364 code = fin->fin_icode;
1366 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1373 if (fin->fin_v == 4) {
1374 if ((oip->ip_p == IPPROTO_ICMP) &&
1375 !(fin->fin_fi.fi_fl & FI_SHORT))
1376 switch (ntohs(fin->fin_data[0]) >> 8)
1387 # if (BSD < 199306) || defined(__sgi)
1389 m = m_get(M_DONTWAIT, MT_HEADER);
1392 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1398 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1401 dst4.s_addr = oip->ip_dst.s_addr;
1403 hlen = sizeof(ip_t);
1404 ohlen = oip->ip_hl << 2;
1409 else if (fin->fin_v == 6) {
1410 hlen = sizeof(ip6_t);
1411 ohlen = sizeof(ip6_t);
1412 type = icmptoicmp6types[type];
1413 if (type == ICMP6_DST_UNREACH)
1414 code = icmptoicmp6unreach[code];
1416 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1420 MCLGET(m, M_DONTWAIT);
1421 if ((m->m_flags & M_EXT) == 0) {
1425 # ifdef M_TRAILINGSPACE
1427 avail = M_TRAILINGSPACE(m);
1429 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1431 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1432 avail - hlen - sizeof(*icmp) - max_linkhdr);
1434 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1437 dst6 = oip6->ip6_dst;
1441 iclen = hlen + sizeof(*icmp);
1443 avail -= (max_linkhdr + iclen);
1444 m->m_data += max_linkhdr;
1445 m->m_pkthdr.rcvif = (struct ifnet *)0;
1449 m->m_pkthdr.len = iclen;
1451 avail -= (m->m_off + iclen);
1457 ip = mtod(m, ip_t *);
1458 icmp = (struct icmp *)((char *)ip + hlen);
1459 bzero((char *)ip, iclen);
1461 icmp->icmp_type = type;
1462 icmp->icmp_code = fin->fin_icode;
1463 icmp->icmp_cksum = 0;
1465 if (type == ICMP_UNREACH &&
1466 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1467 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1471 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1472 avail -= MIN(ohlen, avail);
1477 if (fin->fin_v == 6) {
1479 ip6->ip6_plen = htons(iclen - hlen);
1480 ip6->ip6_nxt = IPPROTO_ICMPV6;
1482 ip6->ip6_src = dst6;
1483 ip6->ip6_dst = oip6->ip6_src;
1485 bcopy((char *)oip + ohlen,
1486 (char *)&icmp->icmp_ip + ohlen, avail);
1487 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1488 sizeof(*ip6), iclen - hlen);
1493 oip->ip_len = htons(oip->ip_len);
1495 oip->ip_off = htons(ip->ip_off);
1497 ip->ip_src.s_addr = dst4.s_addr;
1498 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1503 bcopy((char *)oip + ohlen,
1504 (char *)&icmp->icmp_ip + ohlen, avail);
1505 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1508 ip->ip_p = IPPROTO_ICMP;
1511 shlen = fin->fin_hlen;
1512 fin->fin_hlen = hlen;
1513 err = send_ip(oip, fin, &m);
1514 fin->fin_hlen = shlen;
1516 if (fin->fin_v == 4)
1526 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1527 (!defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1529 int iplinit __P((void));
1533 void iplinit __P((void));
1540 # if defined(__NetBSD__) || defined(__OpenBSD__)
1541 if (ipl_enable() != 0)
1543 if (iplattach() != 0)
1546 printf("IP Filter failed to attach\n");
1550 # endif /* ! __NetBSD__ */
1554 * Return the length of the entire mbuf.
1556 size_t mbufchainlen(m0)
1560 return m0->m_pkthdr.len;
1564 for (; m0; m0 = m0->m_next)
1571 int ipfr_fastroute(m0, mpp, fin, fdp)
1572 struct mbuf *m0, **mpp;
1576 struct ip *ip, *mhip;
1577 struct mbuf *m = m0;
1579 int len, off, error = 0, hlen, code;
1580 struct ifnet *ifp, *sifp;
1581 struct sockaddr_in *dst;
1582 struct route iproute;
1592 if (fin->fin_v == 6) {
1593 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1599 if (fin->fin_v == 6)
1607 * If the mbuf we're about to send is not writable (because of
1608 * a cluster reference, for example) we'll need to make a copy
1609 * of it since this routine modifies the contents.
1611 * If you have non-crappy network hardware that can transmit data
1612 * from the mbuf, rather than making a copy, this is gonna be a
1615 if (M_WRITABLE(m) == 0) {
1616 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1628 hlen = fin->fin_hlen;
1629 ip = mtod(m0, struct ip *);
1631 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1633 * Clear any in-bound checksum flags for this packet.
1635 # if (__NetBSD_Version__ > 105009999)
1636 m0->m_pkthdr.csum_flags = 0;
1638 m0->m_pkthdr.csuminfo = 0;
1640 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1645 #if defined(__sgi) && (IRIX >= 605)
1648 bzero((caddr_t)ro, sizeof (*ro));
1649 dst = (struct sockaddr_in *)&ro->ro_dst;
1650 dst->sin_family = AF_INET;
1651 dst->sin_addr = ip->ip_dst;
1660 * In case we're here due to "to <if>" being used with "keep state",
1661 * check that we're going in the correct direction.
1663 if ((fr != NULL) && (fin->fin_rev != 0)) {
1664 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1666 } else if (fdp != NULL) {
1667 if (fdp->fd_ip.s_addr != 0)
1668 dst->sin_addr = fdp->fd_ip;
1672 dst->sin_len = sizeof(*dst);
1674 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1675 !defined(__OpenBSD__)
1677 rtalloc_ign(ro, RTF_CLONING);
1679 rtalloc_ign(ro, RTF_PRCLONING);
1685 #if defined(__sgi) && (IRIX > 602)
1690 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1696 if ((ifp == NULL) && (ro->ro_rt != NULL))
1697 ifp = ro->ro_rt->rt_ifp;
1699 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1700 if (in_localaddr(ip->ip_dst))
1701 error = EHOSTUNREACH;
1703 error = ENETUNREACH;
1707 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1709 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1711 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1714 ro->ro_rt->rt_use++;
1717 * For input packets which are being "fastrouted", they won't
1718 * go back through output filtering and miss their chance to get
1719 * NAT'd and counted.
1721 if (fin->fin_out == 0) {
1722 sifp = fin->fin_ifp;
1725 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1726 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1727 ATOMIC_INCL(frstats[1].fr_acct);
1730 if (!fr || !(fr->fr_flags & FR_RETMASK))
1731 (void) fr_checkstate(ip, fin);
1732 (void) ip_natout(ip, fin);
1733 fin->fin_ifp = sifp;
1737 * If small enough for interface, can just send directly.
1739 if (ip->ip_len <= ifp->if_mtu) {
1741 # if (!defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1742 !(__NetBSD_Version__ >= 105110000)
1743 ip->ip_id = htons(ip->ip_id);
1745 ip->ip_len = htons(ip->ip_len);
1746 ip->ip_off = htons(ip->ip_off);
1748 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1749 # if (__NetBSD_Version__ > 105009999)
1750 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1751 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1752 else if (ip->ip_sum == 0)
1753 ip->ip_sum = in_cksum(m, hlen);
1755 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1756 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1757 else if (ip->ip_sum == 0)
1758 ip->ip_sum = in_cksum(m, hlen);
1762 ip->ip_sum = in_cksum(m, hlen);
1763 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1764 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1765 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1768 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1774 * Too large for interface; fragment if possible.
1775 * Must be able to put at least 8 bytes per fragment.
1777 if (ip->ip_off & IP_DF) {
1781 len = (ifp->if_mtu - hlen) &~ 7;
1788 int mhlen, firstlen = len;
1789 struct mbuf **mnext = &m->m_act;
1792 * Loop through length of segment after first fragment,
1793 * make new header and copy data of each part and link onto chain.
1796 mhlen = sizeof (struct ip);
1797 for (off = hlen + len; off < ip->ip_len; off += len) {
1799 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1801 MGET(m, M_DONTWAIT, MT_HEADER);
1808 m->m_data += max_linkhdr;
1810 m->m_off = MMAXOFF - hlen;
1812 mhip = mtod(m, struct ip *);
1813 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1814 if (hlen > sizeof (struct ip)) {
1815 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1816 mhip->ip_hl = mhlen >> 2;
1819 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1820 if (ip->ip_off & IP_MF)
1821 mhip->ip_off |= IP_MF;
1822 if (off + len >= ip->ip_len)
1823 len = ip->ip_len - off;
1825 mhip->ip_off |= IP_MF;
1826 mhip->ip_len = htons((u_short)(len + mhlen));
1827 m->m_next = m_copy(m0, off, len);
1828 if (m->m_next == 0) {
1829 error = ENOBUFS; /* ??? */
1833 m->m_pkthdr.len = mhlen + len;
1834 m->m_pkthdr.rcvif = NULL;
1836 mhip->ip_off = htons((u_short)mhip->ip_off);
1838 mhip->ip_sum = in_cksum(m, mhlen);
1843 * Update first fragment by trimming what's been copied out
1844 * and updating header, then send each fragment (in order).
1846 m_adj(m0, hlen + firstlen - ip->ip_len);
1847 ip->ip_len = htons((u_short)(hlen + firstlen));
1848 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1850 ip->ip_sum = in_cksum(m0, hlen);
1852 for (m = m0; m; m = m0) {
1856 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1857 error = (*ifp->if_output)(ifp, m,
1858 (struct sockaddr *)dst, ro->ro_rt);
1860 error = (*ifp->if_output)(ifp, m,
1861 (struct sockaddr *)dst);
1873 if (ro->ro_rt != NULL) {
1879 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
1880 sifp = fin->fin_ifp;
1881 code = fin->fin_icode;
1882 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1884 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
1885 fin->fin_ifp = sifp;
1886 fin->fin_icode = code;
1894 * Return true or false depending on whether the route to the
1895 * given IP address uses the same interface as the one passed.
1897 int fr_verifysrc(ipa, ifp)
1901 struct sockaddr_in *dst;
1902 struct route iproute;
1904 bzero((char *)&iproute, sizeof(iproute));
1905 dst = (struct sockaddr_in *)&iproute.ro_dst;
1906 # if (BSD >= 199306)
1907 dst->sin_len = sizeof(*dst);
1909 dst->sin_family = AF_INET;
1910 dst->sin_addr = ipa;
1911 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1912 !defined(__OpenBSD__)
1914 rtalloc_ign(&iproute, RTF_CLONING);
1916 rtalloc_ign(&iproute, RTF_PRCLONING);
1921 if (iproute.ro_rt == NULL)
1923 return (ifp == iproute.ro_rt->rt_ifp);
1927 # ifdef USE_GETIFNAME
1932 static char workbuf[64];
1934 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
1940 # if defined(USE_INET6)
1942 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1943 * or ensure that it is an IPv6 packet that is being forwarded, those are
1944 * expected to be done by the called (ipfr_fastroute).
1946 static int ipfr_fastroute6(m0, mpp, fin, fdp)
1947 struct mbuf *m0, **mpp;
1951 struct route_in6 ip6route;
1952 struct sockaddr_in6 *dst6;
1953 struct route_in6 *ro;
1956 #if defined(OpenBSD) && (OpenBSD >= 200211)
1957 struct route_in6 *ro_pmtu = NULL;
1958 struct in6_addr finaldst;
1967 bzero((caddr_t)ro, sizeof(*ro));
1968 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
1969 dst6->sin6_family = AF_INET6;
1970 dst6->sin6_len = sizeof(struct sockaddr_in6);
1971 dst6->sin6_addr = fin->fin_fi.fi_src.in6;
1976 if ((fr != NULL) && (fin->fin_rev != 0)) {
1977 if ((ifp != NULL) && (fdp == &fr->fr_tif))
1979 } else if (fdp != NULL) {
1980 if (IP6_NOTZERO(&fdp->fd_ip6))
1981 dst6->sin6_addr = fdp->fd_ip6.in6;
1983 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE)))
1986 rtalloc((struct route *)ro);
1988 if ((ifp == NULL) && (ro->ro_rt != NULL))
1989 ifp = ro->ro_rt->rt_ifp;
1991 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
1992 (ifp != ro->ro_rt->rt_ifp)) {
1993 error = EHOSTUNREACH;
1995 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
1996 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
1997 ro->ro_rt->rt_use++;
1999 #if defined(OpenBSD) && (OpenBSD >= 200211)
2000 ip6 = mtod(m0, ip6_t *);
2002 finaldst = ip6->ip6_dst;
2003 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2006 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2008 if (m0->m_pkthdr.len <= mtu)
2009 error = nd6_output(ifp, fin->fin_ifp, m0,
2013 #if defined(OpenBSD) && (OpenBSD >= 200211)
2018 if (ro->ro_rt != NULL) {
2024 #else /* #ifdef _KERNEL */
2027 # if defined(__sgi) && (IRIX < 605)
2028 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2029 struct sockaddr *s))
2031 static int no_output __P((struct ifnet *ifp, struct mbuf *m,
2032 struct sockaddr *s, struct rtentry *rt))
2040 # if defined(__sgi) && (IRIX < 605)
2041 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2042 struct sockaddr *s))
2044 static int write_output __P((struct ifnet *ifp, struct mbuf *m,
2045 struct sockaddr *s, struct rtentry *rt))
2048 ip_t *ip = (ip_t *)m;
2050 static int write_output(ifp, ip)
2058 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2059 (defined(OpenBSD) && (OpenBSD >= 199603))
2060 sprintf(fname, "%s", ifp->if_xname);
2062 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2064 fd = open(fname, O_WRONLY|O_APPEND);
2069 write(fd, (char *)ip, ntohs(ip->ip_len));
2075 char *get_ifname(ifp)
2078 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2079 (defined(OpenBSD) && (OpenBSD >= 199603))
2080 return ifp->if_xname;
2082 static char fullifname[LIFNAMSIZ];
2084 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2090 struct ifnet *get_unit(ifname, v)
2094 struct ifnet *ifp, **ifa, **old_ifneta;
2096 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2097 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2098 (defined(OpenBSD) && (OpenBSD >= 199603))
2099 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2101 char fullname[LIFNAMSIZ];
2103 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2104 if (!strcmp(ifname, fullname))
2110 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2114 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2121 old_ifneta = ifneta;
2123 ifneta = (struct ifnet **)realloc(ifneta,
2124 (nifs + 1) * sizeof(*ifa));
2130 ifneta[nifs] = NULL;
2131 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2132 if (!ifneta[nifs - 1]) {
2137 ifp = ifneta[nifs - 1];
2139 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2140 (defined(OpenBSD) && (OpenBSD >= 199603))
2141 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2143 ifp->if_name = strdup(ifname);
2145 ifname = ifp->if_name;
2146 while (*ifname && !isdigit(*ifname))
2148 if (*ifname && isdigit(*ifname)) {
2149 ifp->if_unit = atoi(ifname);
2154 ifp->if_output = no_output;
2162 struct ifnet *ifp, **ifa;
2166 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2167 (defined(OpenBSD) && (OpenBSD >= 199603))
2168 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2169 ifp->if_output = write_output;
2170 sprintf(fname, "/tmp/%s", ifp->if_xname);
2171 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2179 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2180 ifp->if_output = write_output;
2181 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2182 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2192 int send_reset(ip, fin)
2196 verbose("- TCP RST sent\n");
2201 int send_icmp_err(ip, code, fin, dst)
2207 verbose("- ICMP UNREACHABLE sent\n");
2217 void m_copydata(m, off, len, cp)
2222 bcopy((char *)m + off, cp, len);
2226 int ipfuiomove(buf, len, rwflag, uio)
2231 int left, ioc, num, offset;
2235 if (rwflag == UIO_READ) {
2239 offset = uio->uio_offset;
2241 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2242 io = uio->uio_iov + ioc;
2246 start = (char *)io->iov_base + offset;
2247 if (start > (char *)io->iov_base + io->iov_len) {
2248 offset -= io->iov_len;
2252 bcopy(buf, start, num);
2253 uio->uio_resid -= num;
2254 uio->uio_offset += num;
2264 #endif /* _KERNEL */