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.21 2006/07/28 02:17:35 dillon 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__) && defined(_KERNEL)
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 #if defined(__DragonFly__)
670 int IPL_EXTERN(ioctl)(struct dev_ioctl_args *ap)
672 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
673 #if (defined(_KERNEL) && defined(__FreeBSD__))
676 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
677 (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
678 defined(__OpenBSD__)))
685 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
686 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
693 #endif /* DragonFly */
696 #if defined(__DragonFly__)
697 dev_t dev = ap->a_head.a_dev;
698 u_long cmd = ap->a_cmd;
699 caddr_t data = ap->a_data;
700 int mode = ap->a_fflag;
702 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
705 int error = 0, unit = 0, tmp;
707 #if (BSD >= 199306) && defined(_KERNEL)
708 if ((securelevel >= 3) && (mode & FWRITE))
712 unit = GET_MINOR(dev);
713 if ((IPL_LOGMAX < unit) || (unit < 0))
719 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
724 if (unit == IPL_LOGNAT) {
726 error = nat_ioctl(data, cmd, mode);
732 if (unit == IPL_LOGSTATE) {
734 error = fr_state_ioctl(data, cmd, mode);
740 if (unit == IPL_LOGAUTH) {
744 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
745 if (!(mode & FWRITE)) {
748 error = frrequest(unit, cmd, data,
752 error = fr_auth_ioctl(data, mode, cmd);
761 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
762 sizeof(iplused[IPL_LOGIPF]));
765 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
770 if (!(mode & FWRITE))
773 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
777 # if defined(__NetBSD__) || defined(__OpenBSD__)
778 error = ipl_enable();
783 # if defined(__NetBSD__)
784 error = ipl_disable();
793 if (!(mode & FWRITE))
796 error = IRCOPY(data, (caddr_t)&fr_flags,
800 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
806 if (!(mode & FWRITE))
809 error = frrequest(unit, cmd, data, fr_active);
814 if (!(mode & FWRITE))
817 error = frrequest(unit, cmd, data, 1 - fr_active);
820 if (!(mode & FWRITE))
823 bzero((char *)frcache, sizeof(frcache[0]) * 2);
824 *(u_int *)data = fr_active;
825 fr_active = 1 - fr_active;
833 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
839 if (!(mode & FWRITE))
842 error = frzerostats(data);
845 if (!(mode & FWRITE))
848 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
850 tmp = frflush(unit, 4, tmp);
851 error = IWCOPY((caddr_t)&tmp, data,
858 if (!(mode & FWRITE))
861 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
863 tmp = frflush(unit, 6, tmp);
864 error = IWCOPY((caddr_t)&tmp, data,
871 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
882 if (!(mode & FWRITE))
885 *(int *)data = ipflog_clear(unit);
887 #endif /* IPFILTER_LOG */
889 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
895 if (!(mode & FWRITE))
898 #if defined(_KERNEL) && defined(__sgi)
913 void fr_forgetifp(ifp)
918 WRITE_ENTER(&ipf_mutex);
919 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
920 if (f->fr_ifa == ifp)
921 f->fr_ifa = (void *)-1;
922 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
923 if (f->fr_ifa == ifp)
924 f->fr_ifa = (void *)-1;
925 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
926 if (f->fr_ifa == ifp)
927 f->fr_ifa = (void *)-1;
928 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
929 if (f->fr_ifa == ifp)
930 f->fr_ifa = (void *)-1;
932 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
933 if (f->fr_ifa == ifp)
934 f->fr_ifa = (void *)-1;
935 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
936 if (f->fr_ifa == ifp)
937 f->fr_ifa = (void *)-1;
938 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
939 if (f->fr_ifa == ifp)
940 f->fr_ifa = (void *)-1;
941 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
942 if (f->fr_ifa == ifp)
943 f->fr_ifa = (void *)-1;
945 RWLOCK_EXIT(&ipf_mutex);
950 static int frrequest(unit, req, data, set)
952 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
960 frentry_t *fp, *f, **fprev;
962 frgroup_t *fg = NULL;
963 int error = 0, in, i;
970 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
974 #if (BSD >= 199306) && defined(_KERNEL)
975 if ((securelevel > 0) && (fp->fr_func != NULL))
980 * Check that the group number does exist and that if a head group
981 * has been specified, doesn't exist.
983 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
984 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
985 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
987 if ((req != SIOCZRLST) && fp->fr_group &&
988 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
991 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
993 if (unit == IPL_LOGAUTH)
994 ftail = fprev = &ipauth;
995 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
996 ftail = fprev = &ipacct[in][set];
997 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
998 ftail = fprev = &ipfilter[in][set];
1000 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
1001 ftail = fprev = &ipacct6[in][set];
1002 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
1003 ftail = fprev = &ipfilter6[in][set];
1008 if ((group = fp->fr_group)) {
1009 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
1011 ftail = fprev = fg->fg_start;
1014 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1016 for (i = 0; i < 4; i++) {
1017 if ((fp->fr_ifnames[i][1] == '\0') &&
1018 ((fp->fr_ifnames[i][0] == '-') ||
1019 (fp->fr_ifnames[i][0] == '*'))) {
1020 fp->fr_ifas[i] = NULL;
1021 } else if (*fp->fr_ifnames[i]) {
1022 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1023 if (!fp->fr_ifas[i])
1024 fp->fr_ifas[i] = (void *)-1;
1029 fp->fr_flags &= ~FR_DUP;
1030 if (*fdp->fd_ifname) {
1031 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1033 fdp->fd_ifp = (struct ifnet *)-1;
1035 fp->fr_flags |= FR_DUP;
1039 if (*fdp->fd_ifname) {
1040 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1042 fdp->fd_ifp = (struct ifnet *)-1;
1046 * Look for a matching filter rule, but don't include the next or
1047 * interface pointer in the comparison (fr_next, fr_ifa).
1049 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1053 for (; (f = *ftail); ftail = &f->fr_next)
1054 if ((fp->fr_cksum == f->fr_cksum) &&
1055 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1059 * If zero'ing statistics, copy current to caller and zero.
1061 if (req == SIOCZRLST) {
1064 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1073 if (req != SIOCINAFR && req != SIOCINIFR)
1074 while ((f = *ftail))
1075 ftail = &f->fr_next;
1079 while (--fp->fr_hits && (f = *ftail))
1080 ftail = &f->fr_next;
1086 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1091 * Only return EBUSY if there is a group list, else
1092 * it's probably just state information referencing
1095 if ((f->fr_ref > 1) && f->fr_grp)
1097 if (fg && fg->fg_head)
1098 fg->fg_head->fr_ref--;
1099 if (unit == IPL_LOGAUTH) {
1100 return fr_preauthcmd(req, f, ftail);
1103 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1105 fixskip(fprev, f, -1);
1106 *ftail = f->fr_next;
1116 if (unit == IPL_LOGAUTH) {
1117 return fr_preauthcmd(req, fp, ftail);
1119 KMALLOC(f, frentry_t *);
1121 if (fg && fg->fg_head)
1122 fg->fg_head->fr_ref++;
1123 bcopy((char *)fp, (char *)f, sizeof(*f));
1126 f->fr_next = *ftail;
1128 if (req == SIOCINIFR || req == SIOCINAFR)
1129 fixskip(fprev, f, 1);
1131 if ((group = f->fr_grhead))
1132 fg = fr_addgroup(group, f, unit, set);
1143 * routines below for saving IP headers to buffer
1145 #ifdef __DragonFly__
1146 int IPL_EXTERN(open)(struct dev_open_args *ap)
1149 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1151 int IPL_EXTERN(open)(dev, flags
1152 #if defined(__FreeBSD__)
1156 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1157 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1167 #endif /* DragonFly */
1169 #ifdef __DragonFly__
1170 dev_t dev = ap->a_head.a_dev;
1172 # if defined(__sgi) && defined(_KERNEL)
1173 u_int min = geteminor(*pdev);
1175 u_int min = GET_MINOR(dev);
1178 if (IPL_LOGMAX < min)
1186 #ifdef __DragonFly__
1187 int IPL_EXTERN(close)(struct dev_close_args *ap)
1190 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1192 int IPL_EXTERN(close)(dev, flags
1193 #if defined(__FreeBSD__)
1197 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1198 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1208 #endif /* DragonFly */
1210 #ifdef __DragonFly__
1211 dev_t dev = ap->a_head.a_dev;
1213 u_int min = GET_MINOR(dev);
1215 if (IPL_LOGMAX < min)
1224 * both of these must operate with at least splnet() lest they be
1225 * called during packet processing and cause an inconsistancy to appear in
1228 #ifdef __DragonFly__
1229 int IPL_EXTERN(read)(struct dev_read_args *ap)
1232 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1235 int IPL_EXTERN(read)(dev, uio, ioflag)
1238 int IPL_EXTERN(read)(dev, uio)
1243 #endif /* DragonFly */
1245 #ifdef __DragonFly__
1246 dev_t dev = ap->a_head.a_dev;
1247 struct uio *uio = ap->a_uio;
1249 # ifdef IPFILTER_LOG
1250 return ipflog_read(GET_MINOR(dev), uio);
1258 * send_reset - this could conceivably be a call to tcp_respond(), but that
1259 * requires a large amount of setting up and isn't any more efficient.
1261 int send_reset(oip, fin)
1265 struct tcphdr *tcp, *tcp2;
1269 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1273 tcp = (struct tcphdr *)fin->fin_dp;
1274 if (tcp->th_flags & TH_RST)
1275 return -1; /* feedback loop */
1276 # if (BSD < 199306) || defined(__sgi)
1277 m = m_get(M_DONTWAIT, MT_HEADER);
1278 # elif defined(__DragonFly__)
1279 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1281 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1288 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1289 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1290 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1293 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1295 hlen = sizeof(ip_t);
1297 m->m_len = sizeof(*tcp2) + hlen;
1299 m->m_data += max_linkhdr;
1300 m->m_pkthdr.len = m->m_len;
1301 m->m_pkthdr.rcvif = (struct ifnet *)0;
1303 ip = mtod(m, struct ip *);
1307 bzero((char *)ip, sizeof(*tcp2) + hlen);
1308 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1310 tcp2->th_sport = tcp->th_dport;
1311 tcp2->th_dport = tcp->th_sport;
1312 if (tcp->th_flags & TH_ACK) {
1313 tcp2->th_seq = tcp->th_ack;
1314 tcp2->th_flags = TH_RST;
1316 tcp2->th_ack = ntohl(tcp->th_seq);
1317 tcp2->th_ack += tlen;
1318 tcp2->th_ack = htonl(tcp2->th_ack);
1319 tcp2->th_flags = TH_RST|TH_ACK;
1321 tcp2->th_off = sizeof(*tcp2) >> 2;
1323 if (fin->fin_v == 6) {
1324 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1325 ip6->ip6_nxt = IPPROTO_TCP;
1326 ip6->ip6_src = oip6->ip6_dst;
1327 ip6->ip6_dst = oip6->ip6_src;
1328 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1329 sizeof(*ip6), sizeof(*tcp2));
1330 return send_ip(oip, fin, &m);
1333 ip->ip_p = IPPROTO_TCP;
1334 ip->ip_len = htons(sizeof(struct tcphdr));
1335 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1336 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1337 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1338 ip->ip_len = hlen + sizeof(*tcp2);
1339 return send_ip(oip, fin, &m);
1344 * Send an IP(v4/v6) datagram out into the network
1346 static int send_ip(oip, fin, mp)
1351 struct mbuf *m = *mp;
1356 bzero((char *)&frn, sizeof(frn));
1357 frn.fin_ifp = fin->fin_ifp;
1358 frn.fin_v = fin->fin_v;
1359 frn.fin_out = fin->fin_out;
1362 ip = mtod(m, ip_t *);
1365 ip->ip_v = fin->fin_v;
1366 if (ip->ip_v == 4) {
1367 ip->ip_hl = (sizeof(*oip) >> 2);
1368 ip->ip_v = IPVERSION;
1369 ip->ip_tos = oip->ip_tos;
1370 ip->ip_id = oip->ip_id;
1372 # if defined(__NetBSD__) || \
1373 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1374 if (ip_mtudisc != 0)
1378 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1383 # if (BSD < 199306) || defined(__sgi)
1384 ip->ip_ttl = tcp_ttl;
1386 ip->ip_ttl = ip_defttl;
1389 frn.fin_dp = (char *)(ip + 1);
1392 else if (ip->ip_v == 6) {
1393 ip6_t *ip6 = (ip6_t *)ip;
1395 hlen = sizeof(*ip6);
1396 ip6->ip6_hlim = 127;
1397 frn.fin_dp = (char *)(ip6 + 1);
1401 m->m_pkthdr.rcvif = NULL;
1404 if (fr_makefrip(hlen, ip, &frn) == 0)
1405 error = ipfr_fastroute(m, mp, &frn, NULL);
1412 int send_icmp_err(oip, type, fin, dst)
1418 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1419 u_short shlen, slen = 0, soff = 0;
1420 struct in_addr dst4;
1425 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1426 struct in6_addr dst6;
1430 if ((type < 0) || (type > ICMP_MAXTYPE))
1433 code = fin->fin_icode;
1435 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1442 if (fin->fin_v == 4) {
1443 if ((oip->ip_p == IPPROTO_ICMP) &&
1444 !(fin->fin_fi.fi_fl & FI_SHORT))
1445 switch (ntohs(fin->fin_data[0]) >> 8)
1456 # if (BSD < 199306) || defined(__sgi)
1458 m = m_get(M_DONTWAIT, MT_HEADER);
1459 # elif defined(__DragonFly__)
1461 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1464 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1470 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1473 dst4.s_addr = oip->ip_dst.s_addr;
1475 hlen = sizeof(ip_t);
1476 ohlen = oip->ip_hl << 2;
1481 else if (fin->fin_v == 6) {
1482 hlen = sizeof(ip6_t);
1483 ohlen = sizeof(ip6_t);
1484 type = icmptoicmp6types[type];
1485 if (type == ICMP6_DST_UNREACH)
1486 code = icmptoicmp6unreach[code];
1488 #ifdef __DragonFly__
1489 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1491 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1496 #ifdef __DragonFly__
1497 MCLGET(m, MB_DONTWAIT);
1499 MCLGET(m, M_DONTWAIT);
1501 if ((m->m_flags & M_EXT) == 0) {
1505 # ifdef M_TRAILINGSPACE
1507 avail = M_TRAILINGSPACE(m);
1509 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1511 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1512 avail - hlen - sizeof(*icmp) - max_linkhdr);
1514 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1517 dst6 = oip6->ip6_dst;
1521 iclen = hlen + sizeof(*icmp);
1523 avail -= (max_linkhdr + iclen);
1524 m->m_data += max_linkhdr;
1525 m->m_pkthdr.rcvif = (struct ifnet *)0;
1529 m->m_pkthdr.len = iclen;
1531 avail -= (m->m_off + iclen);
1537 ip = mtod(m, ip_t *);
1538 icmp = (struct icmp *)((char *)ip + hlen);
1539 bzero((char *)ip, iclen);
1541 icmp->icmp_type = type;
1542 icmp->icmp_code = fin->fin_icode;
1543 icmp->icmp_cksum = 0;
1545 if (type == ICMP_UNREACH &&
1546 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1547 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1552 oip->ip_len = htons(oip->ip_len);
1554 oip->ip_off = htons(oip->ip_off);
1555 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1558 avail -= MIN(ohlen, avail);
1563 if (fin->fin_v == 6) {
1565 ip6->ip6_plen = htons(iclen - hlen);
1566 ip6->ip6_nxt = IPPROTO_ICMPV6;
1568 ip6->ip6_src = dst6;
1569 ip6->ip6_dst = oip6->ip6_src;
1571 bcopy((char *)oip + ohlen,
1572 (char *)&icmp->icmp_ip + ohlen, avail);
1573 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1574 sizeof(*ip6), iclen - hlen);
1579 ip->ip_src.s_addr = dst4.s_addr;
1580 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1585 bcopy((char *)oip + ohlen,
1586 (char *)&icmp->icmp_ip + ohlen, avail);
1587 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1590 ip->ip_p = IPPROTO_ICMP;
1593 shlen = fin->fin_hlen;
1594 fin->fin_hlen = hlen;
1595 err = send_ip(oip, fin, &m);
1596 fin->fin_hlen = shlen;
1602 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1603 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1609 void iplinit (void);
1616 # if defined(__NetBSD__) || defined(__OpenBSD__)
1617 if (ipl_enable() != 0)
1619 if (iplattach() != 0)
1622 printf("IP Filter failed to attach\n");
1626 # endif /* ! __NetBSD__ */
1630 * Return the length of the entire mbuf.
1632 size_t mbufchainlen(m0)
1636 return m0->m_pkthdr.len;
1640 for (; m0; m0 = m0->m_next)
1647 int ipfr_fastroute(m0, mpp, fin, fdp)
1648 struct mbuf *m0, **mpp;
1652 struct ip *ip, *mhip;
1653 struct mbuf *m = m0;
1655 int len, off, error = 0, hlen, code, sout;
1656 struct ifnet *ifp, *sifp;
1657 struct sockaddr_in *dst;
1658 struct route iproute;
1668 if (fin->fin_v == 6) {
1669 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1675 if (fin->fin_v == 6)
1683 * If the mbuf we're about to send is not writable (because of
1684 * a cluster reference, for example) we'll need to make a copy
1685 * of it since this routine modifies the contents.
1687 * If you have non-crappy network hardware that can transmit data
1688 * from the mbuf, rather than making a copy, this is gonna be a
1691 if (M_WRITABLE(m) == 0) {
1692 #ifdef __DragonFly__
1693 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1695 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1708 hlen = fin->fin_hlen;
1709 ip = mtod(m0, struct ip *);
1711 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1713 * Clear any in-bound checksum flags for this packet.
1715 # if (__NetBSD_Version__ > 105009999)
1716 m0->m_pkthdr.csum_flags = 0;
1718 m0->m_pkthdr.csuminfo = 0;
1720 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1725 #if (defined(IRIX) && (IRIX >= 605))
1728 bzero((caddr_t)ro, sizeof (*ro));
1729 dst = (struct sockaddr_in *)&ro->ro_dst;
1730 dst->sin_family = AF_INET;
1731 dst->sin_addr = ip->ip_dst;
1740 * In case we're here due to "to <if>" being used with "keep state",
1741 * check that we're going in the correct direction.
1743 if ((fr != NULL) && (fin->fin_rev != 0)) {
1744 if ((ifp != NULL) && (fdp == &fr->fr_tif)) {
1745 # if (defined(IRIX) && (IRIX >= 605))
1750 } else if (fdp != NULL) {
1751 if (fdp->fd_ip.s_addr != 0)
1752 dst->sin_addr = fdp->fd_ip;
1756 dst->sin_len = sizeof(*dst);
1758 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1759 !defined(__OpenBSD__)
1761 rtalloc_ign(ro, RTF_CLONING);
1763 rtalloc_ign(ro, RTF_PRCLONING);
1770 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1772 # if (defined(IRIX) && (IRIX >= 605))
1779 if ((ifp == NULL) && (ro->ro_rt != NULL))
1780 ifp = ro->ro_rt->rt_ifp;
1782 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1783 if (in_localaddr(ip->ip_dst))
1784 error = EHOSTUNREACH;
1786 error = ENETUNREACH;
1787 # if (defined(IRIX) && (IRIX >= 605))
1793 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1794 #if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1795 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1797 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1800 ro->ro_rt->rt_use++;
1802 #if (defined(IRIX) && (IRIX > 602))
1807 * For input packets which are being "fastrouted", they won't
1808 * go back through output filtering and miss their chance to get
1809 * NAT'd and counted.
1811 if (fin->fin_out == 0) {
1812 sifp = fin->fin_ifp;
1813 sout = fin->fin_out;
1816 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1817 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1818 ATOMIC_INCL(frstats[1].fr_acct);
1821 if (!fr || !(fr->fr_flags & FR_RETMASK))
1822 (void) fr_checkstate(ip, fin);
1824 switch (ip_natout(ip, fin))
1837 fin->fin_ifp = sifp;
1838 fin->fin_out = sout;
1843 * If small enough for interface, can just send directly.
1845 if (ip->ip_len <= ifp->if_mtu) {
1847 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1848 !(__NetBSD_Version__ >= 105110000)
1849 ip->ip_id = htons(ip->ip_id);
1851 ip->ip_len = htons(ip->ip_len);
1852 ip->ip_off = htons(ip->ip_off);
1854 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1855 # if (__NetBSD_Version__ > 105009999)
1856 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1857 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1858 else if (ip->ip_sum == 0)
1859 ip->ip_sum = in_cksum(m, hlen);
1861 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1862 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1863 else if (ip->ip_sum == 0)
1864 ip->ip_sum = in_cksum(m, hlen);
1868 ip->ip_sum = in_cksum(m, hlen);
1869 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1870 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1872 IFNET_UPPERLOCK(ifp);
1874 lwkt_serialize_enter(ifp->if_serializer);
1875 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1877 lwkt_serialize_exit(ifp->if_serializer);
1879 IFNET_UPPERUNLOCK(ifp);
1882 lwkt_serialize_enter(ifp->if_serializer);
1883 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1884 lwkt_serialize_exit(ifp->if_serializer);
1890 * Too large for interface; fragment if possible.
1891 * Must be able to put at least 8 bytes per fragment.
1893 if (ip->ip_off & IP_DF) {
1897 len = (ifp->if_mtu - hlen) &~ 7;
1904 int mhlen, firstlen = len;
1905 struct mbuf **mnext = &m->m_act;
1908 * Loop through length of segment after first fragment,
1909 * make new header and copy data of each part and link onto chain.
1912 mhlen = sizeof (struct ip);
1913 for (off = hlen + len; off < ip->ip_len; off += len) {
1914 # ifdef __DragonFly__
1915 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1916 # elif defined(MGETHDR)
1917 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1919 MGET(m, M_DONTWAIT, MT_HEADER);
1926 m->m_data += max_linkhdr;
1928 m->m_off = MMAXOFF - hlen;
1930 mhip = mtod(m, struct ip *);
1931 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1932 if (hlen > sizeof (struct ip)) {
1933 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1934 mhip->ip_hl = mhlen >> 2;
1937 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1938 if (ip->ip_off & IP_MF)
1939 mhip->ip_off |= IP_MF;
1940 if (off + len >= ip->ip_len)
1941 len = ip->ip_len - off;
1943 mhip->ip_off |= IP_MF;
1944 mhip->ip_len = htons((u_short)(len + mhlen));
1945 m->m_next = m_copy(m0, off, len);
1946 if (m->m_next == 0) {
1947 error = ENOBUFS; /* ??? */
1951 m->m_pkthdr.len = mhlen + len;
1952 m->m_pkthdr.rcvif = NULL;
1954 mhip->ip_off = htons((u_short)mhip->ip_off);
1956 mhip->ip_sum = in_cksum(m, mhlen);
1961 * Update first fragment by trimming what's been copied out
1962 * and updating header, then send each fragment (in order).
1964 m_adj(m0, hlen + firstlen - ip->ip_len);
1965 ip->ip_len = htons((u_short)(hlen + firstlen));
1966 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1968 ip->ip_sum = in_cksum(m0, hlen);
1970 for (m = m0; m; m = m0) {
1974 lwkt_serialize_enter(ifp->if_serializer);
1975 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1976 error = (*ifp->if_output)(ifp, m,
1977 (struct sockaddr *)dst, ro->ro_rt);
1979 error = (*ifp->if_output)(ifp, m,
1980 (struct sockaddr *)dst);
1982 lwkt_serialize_exit(ifp->if_serializer);
1994 if (ro->ro_rt != NULL) {
2000 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
2001 sifp = fin->fin_ifp;
2002 code = fin->fin_icode;
2003 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
2005 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
2006 fin->fin_ifp = sifp;
2007 fin->fin_icode = code;
2015 * Return true or false depending on whether the route to the
2016 * given IP address uses the same interface as the one passed.
2018 int fr_verifysrc(ipa, ifp)
2022 struct sockaddr_in *dst;
2023 struct route iproute;
2025 bzero((char *)&iproute, sizeof(iproute));
2026 dst = (struct sockaddr_in *)&iproute.ro_dst;
2027 # if (BSD >= 199306)
2028 dst->sin_len = sizeof(*dst);
2030 dst->sin_family = AF_INET;
2031 dst->sin_addr = ipa;
2032 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
2033 !defined(__OpenBSD__)
2035 rtalloc_ign(&iproute, RTF_CLONING);
2037 rtalloc_ign(&iproute, RTF_PRCLONING);
2042 if (iproute.ro_rt == NULL)
2044 return (ifp == iproute.ro_rt->rt_ifp);
2048 # ifdef USE_GETIFNAME
2053 static char workbuf[64];
2055 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
2061 # if defined(USE_INET6)
2063 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
2064 * or ensure that it is an IPv6 packet that is being forwarded, those are
2065 * expected to be done by the called (ipfr_fastroute).
2067 static int ipfr_fastroute6(m0, mpp, fin, fdp)
2068 struct mbuf *m0, **mpp;
2072 struct route_in6 ip6route;
2073 struct sockaddr_in6 *dst6;
2074 struct route_in6 *ro;
2077 #if defined(OpenBSD) && (OpenBSD >= 200211)
2078 struct route_in6 *ro_pmtu = NULL;
2079 struct in6_addr finaldst;
2087 bzero((caddr_t)ro, sizeof(*ro));
2088 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2089 dst6->sin6_family = AF_INET6;
2090 dst6->sin6_len = sizeof(struct sockaddr_in6);
2091 dst6->sin6_addr = fin->fin_fi.fi_dst.in6;
2098 if ((fr != NULL) && (fin->fin_rev != 0)) {
2099 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2101 } else if (fdp != NULL) {
2102 if (IP6_NOTZERO(&fdp->fd_ip6))
2103 dst6->sin6_addr = fdp->fd_ip6.in6;
2108 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
2110 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
2111 dst6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2113 rtalloc((struct route *)ro);
2115 if ((ifp == NULL) && (ro->ro_rt != NULL))
2116 ifp = ro->ro_rt->rt_ifp;
2118 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2119 (ifp != ro->ro_rt->rt_ifp)) {
2120 error = EHOSTUNREACH;
2122 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2123 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2124 ro->ro_rt->rt_use++;
2126 #if defined(OpenBSD) && (OpenBSD >= 200211)
2127 ip6 = mtod(m0, ip6_t *);
2129 finaldst = ip6->ip6_dst;
2130 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2134 mtu = ND_IFINFO(ifp)->linkmtu;
2136 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2139 if (m0->m_pkthdr.len <= mtu)
2140 error = nd6_output(ifp, fin->fin_ifp, m0,
2144 #if defined(OpenBSD) && (OpenBSD >= 200211)
2149 if (ro->ro_rt != NULL) {
2155 #else /* #ifdef _KERNEL */
2158 # if defined(__sgi) && (IRIX < 605)
2159 static int no_output (struct ifnet *ifp, struct mbuf *m,
2162 static int no_output (struct ifnet *ifp, struct mbuf *m,
2163 struct sockaddr *s, struct rtentry *rt)
2171 # if defined(__sgi) && (IRIX < 605)
2172 static int write_output (struct ifnet *ifp, struct mbuf *m,
2175 static int write_output (struct ifnet *ifp, struct mbuf *m,
2176 struct sockaddr *s, struct rtentry *rt)
2179 ip_t *ip = (ip_t *)m;
2181 static int write_output(ifp, ip)
2189 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2190 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2191 (defined(__DragonFly__))
2192 sprintf(fname, "%s", ifp->if_xname);
2194 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2196 fd = open(fname, O_WRONLY|O_APPEND);
2201 write(fd, (char *)ip, ntohs(ip->ip_len));
2207 char *get_ifname(ifp)
2210 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2211 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2212 (defined(__DragonFly__))
2213 return ifp->if_xname;
2215 static char fullifname[LIFNAMSIZ];
2217 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2223 struct ifnet *get_unit(ifname, v)
2227 struct ifnet *ifp, **ifa, **old_ifneta;
2229 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2230 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2231 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2232 (defined(__DragonFly__))
2233 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2235 char fullname[LIFNAMSIZ];
2237 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2238 if (!strcmp(ifname, fullname))
2244 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2248 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2255 old_ifneta = ifneta;
2257 ifneta = (struct ifnet **)realloc(ifneta,
2258 (nifs + 1) * sizeof(*ifa));
2264 ifneta[nifs] = NULL;
2265 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2266 if (!ifneta[nifs - 1]) {
2271 ifp = ifneta[nifs - 1];
2273 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2274 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2275 (defined(__DragonFly__))
2276 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2278 ifp->if_name = strdup(ifname);
2280 ifname = ifp->if_name;
2281 while (*ifname && !isdigit(*ifname))
2283 if (*ifname && isdigit(*ifname)) {
2284 ifp->if_unit = atoi(ifname);
2289 ifp->if_output = no_output;
2297 struct ifnet *ifp, **ifa;
2301 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2302 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2303 (defined(__DragonFly__))
2304 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2305 ifp->if_output = write_output;
2306 sprintf(fname, "/tmp/%s", ifp->if_xname);
2307 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2315 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2316 ifp->if_output = write_output;
2317 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2318 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2328 int send_reset(ip, fin)
2332 verbose("- TCP RST sent\n");
2337 int send_icmp_err(ip, code, fin, dst)
2343 verbose("- ICMP UNREACHABLE sent\n");
2353 void m_copydata(m, off, len, cp)
2358 bcopy((char *)m + off, cp, len);
2362 int ipfuiomove(buf, len, rwflag, uio)
2367 int left, ioc, num, offset;
2371 if (rwflag == UIO_READ) {
2375 offset = uio->uio_offset;
2377 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2378 io = uio->uio_iov + ioc;
2382 start = (char *)io->iov_base + offset;
2383 if (start > (char *)io->iov_base + io->iov_len) {
2384 offset -= io->iov_len;
2388 bcopy(buf, start, num);
2389 uio->uio_resid -= num;
2390 uio->uio_offset += num;
2400 #endif /* _KERNEL */