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.24 2006/09/30 20:03:44 swildner 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";
139 static struct ifnet **ifneta = NULL;
142 # if (BSD < 199306) || defined(__sgi)
147 #ifdef ICMP_UNREACH_FILTER_PROHIB
148 int ipl_unreach = ICMP_UNREACH_FILTER_PROHIB;
150 int ipl_unreach = ICMP_UNREACH_FILTER;
152 u_long ipl_frouteok[2] = {0, 0};
154 static int frzerostats (caddr_t);
155 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
156 static int frrequest (int, u_long, caddr_t, int);
158 static int frrequest (int, int, caddr_t, int);
161 static int (*fr_savep) (ip_t *, int, void *, int, struct mbuf **);
162 static int send_ip (ip_t *, fr_info_t *, struct mbuf **);
164 static int ipfr_fastroute6 (struct mbuf *, struct mbuf **,
165 fr_info_t *, frdest_t *);
168 extern int tcp_mtudisc;
169 extern kmutex_t ipf_rw;
170 extern KRWLOCK_T ipf_mutex;
173 void init_ifp (void);
174 # if defined(__sgi) && (IRIX < 605)
175 static int no_output (struct ifnet *, struct mbuf *,
177 static int write_output (struct ifnet *, struct mbuf *,
180 static int no_output (struct ifnet *, struct mbuf *,
181 struct sockaddr *, struct rtentry *);
182 static int write_output (struct ifnet *, struct mbuf *,
183 struct sockaddr *, struct rtentry *);
188 #if (defined(__DragonFly__) || __FreeBSD_version >= 300000) && defined(_KERNEL)
189 struct callout ipfr_slowtimer_ch;
191 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
192 # include <sys/callout.h>
193 struct callout ipfr_slowtimer_ch;
195 #if defined(__OpenBSD__)
196 # include <sys/timeout.h>
197 struct timeout ipfr_slowtimer_ch;
199 #if defined(__sgi) && defined(_KERNEL)
200 toid_t ipfr_slowtimer_ch;
203 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 106080000) && \
205 # include <sys/conf.h>
206 const struct cdevsw ipl_cdevsw = {
207 iplopen, iplclose, iplread, nowrite, iplioctl,
208 nostop, notty, nopoll, nommap,
212 #if (_BSDI_VERSION >= 199510) && defined(_KERNEL)
213 # include <sys/device.h>
214 # include <sys/conf.h>
216 struct cfdriver iplcd = {
217 NULL, "ipl", NULL, NULL, DV_DULL, 0
220 struct devsw iplsw = {
222 iplopen, iplclose, iplread, nowrite, iplioctl, noselect, nommap,
223 nostrat, nodump, nopsize, 0,
226 #endif /* _BSDI_VERSION >= 199510 && _KERNEL */
228 #if defined(__NetBSD__) || defined(__OpenBSD__) || \
229 (_BSDI_VERSION >= 199701) || (defined(__DragonFly_version) && \
230 (__DragonFly_version >= 100000))
231 # include <sys/conf.h>
232 # if defined(NETBSD_PF)
233 # include <net/pfil.h>
235 * We provide the fr_checkp name just to minimize changes later.
237 int (*fr_checkp) (ip_t *ip, int hlen, void *ifp, int out, mb_t **mp);
238 # endif /* NETBSD_PF */
239 #endif /* __NetBSD__ */
242 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000) && \
244 # include <net/pfil.h>
246 static int fr_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
248 static int fr_check_wrapper(arg, mp, ifp, dir)
254 struct ip *ip = mtod(*mp, struct ip *);
255 int rv, hlen = ip->ip_hl << 2;
257 #if defined(M_CSUM_TCPv4)
259 * If the packet is out-bound, we can't delay checksums
260 * here. For in-bound, the checksum has already been
263 if (dir == PFIL_OUT) {
264 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
265 in_delayed_cksum(*mp);
266 (*mp)->m_pkthdr.csum_flags &=
267 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
270 #endif /* M_CSUM_TCPv4 */
273 * We get the packet with all fields in network byte
274 * order. We expect ip_len and ip_off to be in host
275 * order. We frob them, call the filter, then frob
278 * Note, we don't need to update the checksum, because
279 * it has already been verified.
281 ip->ip_len = ntohs(ip->ip_len);
282 ip->ip_off = ntohs(ip->ip_off);
284 rv = fr_check(ip, hlen, ifp, (dir == PFIL_OUT), mp);
286 if (rv == 0 && *mp != NULL) {
287 ip = mtod(*mp, struct ip *);
288 ip->ip_len = htons(ip->ip_len);
289 ip->ip_off = htnos(ip->ip_off);
296 # include <netinet/ip6.h>
298 static int fr_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
300 static int fr_check_wrapper6(arg, mp, ifp, dir)
307 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
308 ifp, (dir == PFIL_OUT), mp));
311 #endif /* __NetBSD_Version >= 105110000 && _KERNEL */
312 #if defined(__DragonFly_version) && (__DragonFly_version >= 100000) && \
316 fr_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
318 struct ip *ip = mtod(*mp, struct ip *);
319 return fr_check(ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT), mp);
323 # include <netinet/ip6.h>
326 fr_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
328 return (fr_check(mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
329 ifp, (dir == PFIL_OUT), mp));
331 # endif /* USE_INET6 */
332 #endif /* __DragonFly_version >= 100000 && _KERNEL */
334 # if defined(IPFILTER_LKM) && !defined(__sgi)
338 if (strcmp(s, "ipl") == 0)
342 # endif /* IPFILTER_LKM */
346 * Try to detect the case when compiling for NetBSD with pseudo-device
348 # if defined(__NetBSD__) && defined(PFIL_HOOKS)
350 ipfilterattach(count)
355 * Do nothing here, really. The filter will be enabled
356 * by the SIOCFRENB ioctl.
362 # if defined(__NetBSD__) || defined(__OpenBSD__)
369 # if !defined(__DragonFly__)
372 # if defined(__sgi) || (defined(NETBSD_PF) && \
373 (__NetBSD_Version__ >= 104200000)) || \
374 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
377 #if (defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)) || \
378 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
379 struct pfil_head *ph_inet;
381 struct pfil_head *ph_inet6;
386 if (fr_running || (fr_checkp == fr_check)) {
387 printf("IP Filter: already initialized\n");
395 if (nat_init() == -1) {
399 if (fr_stateinit() == -1) {
403 if (appr_init() == -1) {
409 # if (__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
410 (defined(__DragonFly_version) && (__DragonFly_version >= 100000))
411 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
412 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
414 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
424 error = pfil_add_hook((void *)fr_check_wrapper, NULL,
425 PFIL_IN|PFIL_OUT, ph_inet);
429 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
430 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
444 pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
447 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
448 if (ph_inet6 != NULL)
449 error = pfil_add_hook((void *)fr_check_wrapper6, NULL,
450 PFIL_IN|PFIL_OUT, ph_inet6);
454 pfil_remove_hook((void *)fr_check_wrapper6, NULL,
455 PFIL_IN|PFIL_OUT, ph_inet6);
457 error = pfil_add_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
458 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
460 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
461 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
474 error = ipfilter_sgi_attach();
484 bzero((char *)frcache, sizeof(frcache));
485 fr_savep = fr_checkp;
486 fr_checkp = fr_check;
490 if (fr_pass & FR_PASS)
492 else if (fr_pass & FR_BLOCK)
495 defpass = "no-match -> block";
497 printf("%s initialized. Default = %s all, Logging = %s\n",
498 ipfilter_version, defpass,
505 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
506 callout_init(&ipfr_slowtimer_ch);
507 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
509 # if defined(__OpenBSD__)
510 timeout_set(&ipfr_slowtimer_ch, ipfr_slowtimer, NULL);
511 timeout_add(&ipfr_slowtimer_ch, hz/2);
513 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000) || defined(__sgi)
514 callout_init(&ipfr_slowtimer_ch);
515 callout_reset(&ipfr_slowtimer_ch, hz / 2, ipfr_slowtimer, NULL);
517 timeout(ipfr_slowtimer, NULL, hz/2);
527 * Disable the filter by removing the hooks from the IP input/output
530 # if defined(__NetBSD__)
536 # if !defined(__DragonFly__)
540 #if defined(NETBSD_PF) && \
541 ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
542 (defined(__DragonFly_version) && (__DragonFly_version >= 100000)))
544 # if (__NetBSD_Version__ >= 105150000) || (__DragonFly_version >= 100000)
545 struct pfil_head *ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
547 struct pfil_head *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
553 # if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
554 callout_stop(&ipfr_slowtimer_ch);
556 # if (defined(__DragonFly__) || __FreeBSD_version >= 300000)
557 callout_stop(&ipfr_slowtimer_ch);
560 untimeout(ipfr_slowtimer_ch);
562 # if defined(__OpenBSD__)
563 timeout_del(&ipfr_slowtimer_ch);
565 untimeout(ipfr_slowtimer, NULL);
566 # endif /* OpenBSD */
568 # endif /* FreeBSD */
574 printf("IP Filter: not initialized\n");
579 printf("%s unloaded\n", ipfilter_version);
581 fr_checkp = fr_savep;
582 i = frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
583 i += frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE);
587 # if ((__NetBSD_Version__ >= 104200000) || (__FreeBSD_version >= 500011) || \
588 (__DragonFly_version >= 100000))
589 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
591 error = pfil_remove_hook((void *)fr_check_wrapper, NULL,
592 PFIL_IN|PFIL_OUT, ph_inet);
596 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
597 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
604 pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT);
607 # if (__NetBSD_Version__ >= 105110000) || (__DragonFly_version >= 100000)
608 if (ph_inet6 != NULL)
609 error = pfil_remove_hook((void *)fr_check_wrapper6, NULL,
610 PFIL_IN|PFIL_OUT, ph_inet6);
614 error = pfil_remove_hook((void *)fr_check, PFIL_IN|PFIL_OUT,
615 &inet6sw[ip6_protox[IPPROTO_IPV6]].pr_pfh);
625 ipfilter_sgi_detach();
640 static int frzerostats(data)
647 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
651 bzero((char *)frstats, sizeof(*frstats) * 2);
658 * Filter ioctl interface.
661 int IPL_EXTERN(ioctl)(dev_t dev, int cmd, caddr_t data, int mode
663 , cred_t *cp, int *rp
667 #if defined(__DragonFly__)
668 # if defined(_KERNEL)
669 int IPL_EXTERN(ioctl)(struct dev_ioctl_args *ap)
671 int IPL_EXTERN(ioctl)(struct cdev *dev, u_long cmd, caddr_t data, int mode)
674 int IPL_EXTERN(ioctl)(dev, cmd, data, mode
675 #if (defined(_KERNEL) && defined(__FreeBSD__))
678 # elif (defined(_KERNEL) && ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || \
679 (NetBSD >= 199511) || (__FreeBSD_version >= 220000) || \
680 defined(__OpenBSD__)))
687 # if defined(__NetBSD__) || defined(__OpenBSD__) || \
688 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000)
695 #endif /* DragonFly */
698 #if (defined(_KERNEL) && defined(__DragonFly__))
699 struct cdev *dev = ap->a_head.a_dev;
700 u_long cmd = ap->a_cmd;
701 caddr_t data = ap->a_data;
702 int mode = ap->a_fflag;
704 #if defined(_KERNEL) && !SOLARIS && !defined(__DragonFly__)
707 int error = 0, unit = 0, tmp;
709 #if (BSD >= 199306) && defined(_KERNEL)
710 if ((securelevel >= 3) && (mode & FWRITE))
714 unit = GET_MINOR(dev);
715 if ((IPL_LOGMAX < unit) || (unit < 0))
721 if (fr_running == 0 && (cmd != SIOCFRENB || unit != IPL_LOGIPF))
726 if (unit == IPL_LOGNAT) {
728 error = nat_ioctl(data, cmd, mode);
734 if (unit == IPL_LOGSTATE) {
736 error = fr_state_ioctl(data, cmd, mode);
742 if (unit == IPL_LOGAUTH) {
746 if ((cmd == SIOCADAFR) || (cmd == SIOCRMAFR)) {
747 if (!(mode & FWRITE)) {
750 error = frrequest(unit, cmd, data,
754 error = fr_auth_ioctl(data, mode, cmd);
763 error = IWCOPY((caddr_t)&iplused[IPL_LOGIPF], (caddr_t)data,
764 sizeof(iplused[IPL_LOGIPF]));
767 #if (!defined(IPFILTER_LKM) || defined(__NetBSD__)) && defined(_KERNEL)
772 if (!(mode & FWRITE))
775 error = IRCOPY(data, (caddr_t)&enable, sizeof(enable));
779 # if defined(__NetBSD__) || defined(__OpenBSD__)
780 error = ipl_enable();
785 # if defined(__NetBSD__)
786 error = ipl_disable();
795 if (!(mode & FWRITE))
798 error = IRCOPY(data, (caddr_t)&fr_flags,
802 error = IWCOPY((caddr_t)&fr_flags, data, sizeof(fr_flags));
808 if (!(mode & FWRITE))
811 error = frrequest(unit, cmd, data, fr_active);
816 if (!(mode & FWRITE))
819 error = frrequest(unit, cmd, data, 1 - fr_active);
822 if (!(mode & FWRITE))
825 bzero((char *)frcache, sizeof(frcache[0]) * 2);
826 *(u_int *)data = fr_active;
827 fr_active = 1 - fr_active;
835 error = IWCOPYPTR((caddr_t)&fio, data, sizeof(fio));
841 if (!(mode & FWRITE))
844 error = frzerostats(data);
847 if (!(mode & FWRITE))
850 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
852 tmp = frflush(unit, 4, tmp);
853 error = IWCOPY((caddr_t)&tmp, data,
860 if (!(mode & FWRITE))
863 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
865 tmp = frflush(unit, 6, tmp);
866 error = IWCOPY((caddr_t)&tmp, data,
873 error = IRCOPY(data, (caddr_t)&tmp, sizeof(tmp));
884 if (!(mode & FWRITE))
887 *(int *)data = ipflog_clear(unit);
889 #endif /* IPFILTER_LOG */
891 error = IWCOPYPTR((caddr_t)ipfr_fragstats(), data,
897 if (!(mode & FWRITE))
900 #if defined(_KERNEL) && defined(__sgi)
915 void fr_forgetifp(ifp)
920 WRITE_ENTER(&ipf_mutex);
921 for (f = ipacct[0][fr_active]; (f != NULL); f = f->fr_next)
922 if (f->fr_ifa == ifp)
923 f->fr_ifa = (void *)-1;
924 for (f = ipacct[1][fr_active]; (f != NULL); f = f->fr_next)
925 if (f->fr_ifa == ifp)
926 f->fr_ifa = (void *)-1;
927 for (f = ipfilter[0][fr_active]; (f != NULL); f = f->fr_next)
928 if (f->fr_ifa == ifp)
929 f->fr_ifa = (void *)-1;
930 for (f = ipfilter[1][fr_active]; (f != NULL); f = f->fr_next)
931 if (f->fr_ifa == ifp)
932 f->fr_ifa = (void *)-1;
934 for (f = ipacct6[0][fr_active]; (f != NULL); f = f->fr_next)
935 if (f->fr_ifa == ifp)
936 f->fr_ifa = (void *)-1;
937 for (f = ipacct6[1][fr_active]; (f != NULL); f = f->fr_next)
938 if (f->fr_ifa == ifp)
939 f->fr_ifa = (void *)-1;
940 for (f = ipfilter6[0][fr_active]; (f != NULL); f = f->fr_next)
941 if (f->fr_ifa == ifp)
942 f->fr_ifa = (void *)-1;
943 for (f = ipfilter6[1][fr_active]; (f != NULL); f = f->fr_next)
944 if (f->fr_ifa == ifp)
945 f->fr_ifa = (void *)-1;
947 RWLOCK_EXIT(&ipf_mutex);
952 static int frrequest(unit, req, data, set)
954 #if defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) || (__FreeBSD_version >= 300003)
962 frentry_t *fp, *f, **fprev;
964 frgroup_t *fg = NULL;
965 int error = 0, in, i;
972 error = IRCOPYPTR(data, (caddr_t)fp, sizeof(*fp));
976 #if (BSD >= 199306) && defined(_KERNEL)
977 if ((securelevel > 0) && (fp->fr_func != NULL))
982 * Check that the group number does exist and that if a head group
983 * has been specified, doesn't exist.
985 if ((req != SIOCZRLST) && ((req == SIOCINAFR) || (req == SIOCINIFR) ||
986 (req == SIOCADAFR) || (req == SIOCADIFR)) && fp->fr_grhead &&
987 fr_findgroup((u_int)fp->fr_grhead, fp->fr_flags, unit, set, NULL))
989 if ((req != SIOCZRLST) && fp->fr_group &&
990 !fr_findgroup((u_int)fp->fr_group, fp->fr_flags, unit, set, NULL))
993 in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
995 if (unit == IPL_LOGAUTH)
996 ftail = fprev = &ipauth;
997 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 4))
998 ftail = fprev = &ipacct[in][set];
999 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 4))
1000 ftail = fprev = &ipfilter[in][set];
1002 else if ((fp->fr_flags & FR_ACCOUNT) && (fp->fr_v == 6))
1003 ftail = fprev = &ipacct6[in][set];
1004 else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) && (fp->fr_v == 6))
1005 ftail = fprev = &ipfilter6[in][set];
1010 if ((group = fp->fr_group)) {
1011 if (!(fg = fr_findgroup(group, fp->fr_flags, unit, set, NULL)))
1013 ftail = fprev = fg->fg_start;
1016 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1018 for (i = 0; i < 4; i++) {
1019 if ((fp->fr_ifnames[i][1] == '\0') &&
1020 ((fp->fr_ifnames[i][0] == '-') ||
1021 (fp->fr_ifnames[i][0] == '*'))) {
1022 fp->fr_ifas[i] = NULL;
1023 } else if (*fp->fr_ifnames[i]) {
1024 fp->fr_ifas[i] = GETUNIT(fp->fr_ifnames[i], fp->fr_v);
1025 if (!fp->fr_ifas[i])
1026 fp->fr_ifas[i] = (void *)-1;
1031 fp->fr_flags &= ~FR_DUP;
1032 if (*fdp->fd_ifname) {
1033 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1035 fdp->fd_ifp = (struct ifnet *)-1;
1037 fp->fr_flags |= FR_DUP;
1041 if (*fdp->fd_ifname) {
1042 fdp->fd_ifp = GETUNIT(fdp->fd_ifname, fp->fr_v);
1044 fdp->fd_ifp = (struct ifnet *)-1;
1048 * Look for a matching filter rule, but don't include the next or
1049 * interface pointer in the comparison (fr_next, fr_ifa).
1051 for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_ip, pp = &fp->fr_cksum;
1055 for (; (f = *ftail); ftail = &f->fr_next)
1056 if ((fp->fr_cksum == f->fr_cksum) &&
1057 !bcmp((char *)&f->fr_ip, (char *)&fp->fr_ip, FR_CMPSIZ))
1061 * If zero'ing statistics, copy current to caller and zero.
1063 if (req == SIOCZRLST) {
1066 error = IWCOPYPTR((caddr_t)f, data, sizeof(*f));
1075 if (req != SIOCINAFR && req != SIOCINIFR)
1076 while ((f = *ftail))
1077 ftail = &f->fr_next;
1081 while (--fp->fr_hits && (f = *ftail))
1082 ftail = &f->fr_next;
1088 if (req == SIOCRMAFR || req == SIOCRMIFR) {
1093 * Only return EBUSY if there is a group list, else
1094 * it's probably just state information referencing
1097 if ((f->fr_ref > 1) && f->fr_grp)
1099 if (fg && fg->fg_head)
1100 fg->fg_head->fr_ref--;
1101 if (unit == IPL_LOGAUTH) {
1102 return fr_preauthcmd(req, f, ftail);
1105 fr_delgroup((u_int)f->fr_grhead, fp->fr_flags,
1107 fixskip(fprev, f, -1);
1108 *ftail = f->fr_next;
1118 if (unit == IPL_LOGAUTH) {
1119 return fr_preauthcmd(req, fp, ftail);
1121 KMALLOC(f, frentry_t *);
1123 if (fg && fg->fg_head)
1124 fg->fg_head->fr_ref++;
1125 bcopy((char *)fp, (char *)f, sizeof(*f));
1128 f->fr_next = *ftail;
1130 if (req == SIOCINIFR || req == SIOCINAFR)
1131 fixskip(fprev, f, 1);
1133 if ((group = f->fr_grhead))
1134 fg = fr_addgroup(group, f, unit, set);
1145 * routines below for saving IP headers to buffer
1147 #ifdef __DragonFly__
1148 int IPL_EXTERN(open)(struct dev_open_args *ap)
1151 int IPL_EXTERN(open)(dev_t *pdev, int flags, int devtype, cred_t *cp)
1153 int IPL_EXTERN(open)(dev, flags
1154 #if defined(__FreeBSD__)
1158 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1159 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1169 #endif /* DragonFly */
1171 #ifdef __DragonFly__
1172 struct cdev *dev = ap->a_head.a_dev;
1174 # if defined(__sgi) && defined(_KERNEL)
1175 u_int min = geteminor(*pdev);
1177 u_int min = GET_MINOR(dev);
1180 if (IPL_LOGMAX < min)
1188 #ifdef __DragonFly__
1189 int IPL_EXTERN(close)(struct dev_close_args *ap)
1192 int IPL_EXTERN(close)(dev_t dev, int flags, int devtype, cred_t *cp)
1194 int IPL_EXTERN(close)(dev, flags
1195 #if defined(__FreeBSD__)
1199 #elif ((_BSDI_VERSION >= 199510) || (BSD >= 199506) || (NetBSD >= 199511) || \
1200 (__FreeBSD_version >= 220000) || defined(__OpenBSD__)) && defined(_KERNEL)
1210 #endif /* DragonFly */
1212 #ifdef __DragonFly__
1213 struct cdev *dev = ap->a_head.a_dev;
1215 u_int min = GET_MINOR(dev);
1217 if (IPL_LOGMAX < min)
1226 * both of these must operate with at least splnet() lest they be
1227 * called during packet processing and cause an inconsistancy to appear in
1230 #ifdef __DragonFly__
1231 int IPL_EXTERN(read)(struct dev_read_args *ap)
1234 int IPL_EXTERN(read)(dev_t dev, uio_t *uio, cred_t *crp)
1237 int IPL_EXTERN(read)(dev, uio, ioflag)
1240 int IPL_EXTERN(read)(dev, uio)
1245 #endif /* DragonFly */
1247 #ifdef __DragonFly__
1248 struct cdev *dev = ap->a_head.a_dev;
1249 struct uio *uio = ap->a_uio;
1251 # ifdef IPFILTER_LOG
1252 return ipflog_read(GET_MINOR(dev), uio);
1260 * send_reset - this could conceivably be a call to tcp_respond(), but that
1261 * requires a large amount of setting up and isn't any more efficient.
1263 int send_reset(oip, fin)
1267 struct tcphdr *tcp, *tcp2;
1271 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1275 tcp = (struct tcphdr *)fin->fin_dp;
1276 if (tcp->th_flags & TH_RST)
1277 return -1; /* feedback loop */
1278 # if (BSD < 199306) || defined(__sgi)
1279 m = m_get(M_DONTWAIT, MT_HEADER);
1280 # elif defined(__DragonFly__)
1281 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1283 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1290 tlen = fin->fin_dlen - (tcp->th_off << 2) +
1291 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
1292 ((tcp->th_flags & TH_FIN) ? 1 : 0);
1295 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
1297 hlen = sizeof(ip_t);
1299 m->m_len = sizeof(*tcp2) + hlen;
1301 m->m_data += max_linkhdr;
1302 m->m_pkthdr.len = m->m_len;
1303 m->m_pkthdr.rcvif = (struct ifnet *)0;
1305 ip = mtod(m, struct ip *);
1309 bzero((char *)ip, sizeof(*tcp2) + hlen);
1310 tcp2 = (struct tcphdr *)((char *)ip + hlen);
1312 tcp2->th_sport = tcp->th_dport;
1313 tcp2->th_dport = tcp->th_sport;
1314 if (tcp->th_flags & TH_ACK) {
1315 tcp2->th_seq = tcp->th_ack;
1316 tcp2->th_flags = TH_RST;
1318 tcp2->th_ack = ntohl(tcp->th_seq);
1319 tcp2->th_ack += tlen;
1320 tcp2->th_ack = htonl(tcp2->th_ack);
1321 tcp2->th_flags = TH_RST|TH_ACK;
1323 tcp2->th_off = sizeof(*tcp2) >> 2;
1325 if (fin->fin_v == 6) {
1326 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1327 ip6->ip6_nxt = IPPROTO_TCP;
1328 ip6->ip6_src = oip6->ip6_dst;
1329 ip6->ip6_dst = oip6->ip6_src;
1330 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
1331 sizeof(*ip6), sizeof(*tcp2));
1332 return send_ip(oip, fin, &m);
1335 ip->ip_p = IPPROTO_TCP;
1336 ip->ip_len = htons(sizeof(struct tcphdr));
1337 ip->ip_src.s_addr = oip->ip_dst.s_addr;
1338 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1339 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
1340 ip->ip_len = hlen + sizeof(*tcp2);
1341 return send_ip(oip, fin, &m);
1346 * Send an IP(v4/v6) datagram out into the network
1348 static int send_ip(oip, fin, mp)
1353 struct mbuf *m = *mp;
1358 bzero((char *)&frn, sizeof(frn));
1359 frn.fin_ifp = fin->fin_ifp;
1360 frn.fin_v = fin->fin_v;
1361 frn.fin_out = fin->fin_out;
1364 ip = mtod(m, ip_t *);
1367 ip->ip_v = fin->fin_v;
1368 if (ip->ip_v == 4) {
1369 ip->ip_hl = (sizeof(*oip) >> 2);
1370 ip->ip_v = IPVERSION;
1371 ip->ip_tos = oip->ip_tos;
1372 ip->ip_id = oip->ip_id;
1374 # if defined(__NetBSD__) || \
1375 (defined(__OpenBSD__) && (OpenBSD >= 200012))
1376 if (ip_mtudisc != 0)
1380 if (ip->ip_p == IPPROTO_TCP && tcp_mtudisc != 0)
1385 # if (BSD < 199306) || defined(__sgi)
1386 ip->ip_ttl = tcp_ttl;
1388 ip->ip_ttl = ip_defttl;
1391 frn.fin_dp = (char *)(ip + 1);
1394 else if (ip->ip_v == 6) {
1395 ip6_t *ip6 = (ip6_t *)ip;
1397 hlen = sizeof(*ip6);
1398 ip6->ip6_hlim = 127;
1399 frn.fin_dp = (char *)(ip6 + 1);
1403 m->m_pkthdr.rcvif = NULL;
1406 if (fr_makefrip(hlen, ip, &frn) == 0)
1407 error = ipfr_fastroute(m, mp, &frn, NULL);
1414 int send_icmp_err(oip, type, fin, dst)
1420 int err, hlen = 0, xtra = 0, iclen, ohlen = 0, avail, code;
1421 u_short shlen, slen = 0, soff = 0;
1422 struct in_addr dst4;
1427 ip6_t *ip6, *oip6 = (ip6_t *)oip;
1428 struct in6_addr dst6;
1432 if ((type < 0) || (type > ICMP_MAXTYPE))
1435 code = fin->fin_icode;
1437 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
1444 if (fin->fin_v == 4) {
1445 if ((oip->ip_p == IPPROTO_ICMP) &&
1446 !(fin->fin_fi.fi_fl & FI_SHORT))
1447 switch (ntohs(fin->fin_data[0]) >> 8)
1458 # if (BSD < 199306) || defined(__sgi)
1460 m = m_get(M_DONTWAIT, MT_HEADER);
1461 # elif defined(__DragonFly__)
1463 m = m_gethdr(MB_DONTWAIT, MT_HEADER);
1466 m = m_gethdr(M_DONTWAIT, MT_HEADER);
1472 if (fr_ifpaddr(4, ifp, &dst4) == -1)
1475 dst4.s_addr = oip->ip_dst.s_addr;
1477 hlen = sizeof(ip_t);
1478 ohlen = oip->ip_hl << 2;
1483 else if (fin->fin_v == 6) {
1484 hlen = sizeof(ip6_t);
1485 ohlen = sizeof(ip6_t);
1486 type = icmptoicmp6types[type];
1487 if (type == ICMP6_DST_UNREACH)
1488 code = icmptoicmp6unreach[code];
1490 #ifdef __DragonFly__
1491 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1493 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1498 #ifdef __DragonFly__
1499 MCLGET(m, MB_DONTWAIT);
1501 MCLGET(m, M_DONTWAIT);
1503 if ((m->m_flags & M_EXT) == 0) {
1507 # ifdef M_TRAILINGSPACE
1509 avail = M_TRAILINGSPACE(m);
1511 avail = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
1513 xtra = MIN(ntohs(oip6->ip6_plen) + sizeof(ip6_t),
1514 avail - hlen - sizeof(*icmp) - max_linkhdr);
1516 if (fr_ifpaddr(6, ifp, (struct in_addr *)&dst6) == -1)
1519 dst6 = oip6->ip6_dst;
1523 iclen = hlen + sizeof(*icmp);
1525 avail -= (max_linkhdr + iclen);
1526 m->m_data += max_linkhdr;
1527 m->m_pkthdr.rcvif = (struct ifnet *)0;
1531 m->m_pkthdr.len = iclen;
1533 avail -= (m->m_off + iclen);
1539 ip = mtod(m, ip_t *);
1540 icmp = (struct icmp *)((char *)ip + hlen);
1541 bzero((char *)ip, iclen);
1543 icmp->icmp_type = type;
1544 icmp->icmp_code = fin->fin_icode;
1545 icmp->icmp_cksum = 0;
1547 if (type == ICMP_UNREACH &&
1548 fin->fin_icode == ICMP_UNREACH_NEEDFRAG && ifp)
1549 icmp->icmp_nextmtu = htons(((struct ifnet *) ifp)->if_mtu);
1554 oip->ip_len = htons(oip->ip_len);
1556 oip->ip_off = htons(oip->ip_off);
1557 bcopy((char *)oip, (char *)&icmp->icmp_ip, MIN(ohlen, avail));
1560 avail -= MIN(ohlen, avail);
1565 if (fin->fin_v == 6) {
1567 ip6->ip6_plen = htons(iclen - hlen);
1568 ip6->ip6_nxt = IPPROTO_ICMPV6;
1570 ip6->ip6_src = dst6;
1571 ip6->ip6_dst = oip6->ip6_src;
1573 bcopy((char *)oip + ohlen,
1574 (char *)&icmp->icmp_ip + ohlen, avail);
1575 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1576 sizeof(*ip6), iclen - hlen);
1581 ip->ip_src.s_addr = dst4.s_addr;
1582 ip->ip_dst.s_addr = oip->ip_src.s_addr;
1587 bcopy((char *)oip + ohlen,
1588 (char *)&icmp->icmp_ip + ohlen, avail);
1589 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1592 ip->ip_p = IPPROTO_ICMP;
1595 shlen = fin->fin_hlen;
1596 fin->fin_hlen = hlen;
1597 err = send_ip(oip, fin, &m);
1598 fin->fin_hlen = shlen;
1604 # if !defined(IPFILTER_LKM) && !defined(__sgi) && \
1605 (!defined(__DragonFly__) || !defined(__FreeBSD_version) || (__FreeBSD_version < 300000))
1611 void iplinit (void);
1618 # if defined(__NetBSD__) || defined(__OpenBSD__)
1619 if (ipl_enable() != 0)
1621 if (iplattach() != 0)
1624 printf("IP Filter failed to attach\n");
1628 # endif /* ! __NetBSD__ */
1632 * Return the length of the entire mbuf.
1634 size_t mbufchainlen(m0)
1638 return m0->m_pkthdr.len;
1642 for (; m0; m0 = m0->m_next)
1649 int ipfr_fastroute(m0, mpp, fin, fdp)
1650 struct mbuf *m0, **mpp;
1654 struct ip *ip, *mhip;
1655 struct mbuf *m = m0;
1657 int len, off, error = 0, hlen, code, sout;
1658 struct ifnet *ifp, *sifp;
1659 struct sockaddr_in *dst;
1660 struct route iproute;
1670 if (fin->fin_v == 6) {
1671 error = ipfr_fastroute6(m0, mpp, fin, fdp);
1677 if (fin->fin_v == 6)
1685 * If the mbuf we're about to send is not writable (because of
1686 * a cluster reference, for example) we'll need to make a copy
1687 * of it since this routine modifies the contents.
1689 * If you have non-crappy network hardware that can transmit data
1690 * from the mbuf, rather than making a copy, this is gonna be a
1693 if (M_WRITABLE(m) == 0) {
1694 #ifdef __DragonFly__
1695 if ((m0 = m_dup(m, MB_DONTWAIT)) != NULL) {
1697 if ((m0 = m_dup(m, M_DONTWAIT)) != NULL) {
1710 hlen = fin->fin_hlen;
1711 ip = mtod(m0, struct ip *);
1713 #if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1715 * Clear any in-bound checksum flags for this packet.
1717 # if (__NetBSD_Version__ > 105009999)
1718 m0->m_pkthdr.csum_flags = 0;
1720 m0->m_pkthdr.csuminfo = 0;
1722 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1727 #if (defined(IRIX) && (IRIX >= 605))
1730 bzero((caddr_t)ro, sizeof (*ro));
1731 dst = (struct sockaddr_in *)&ro->ro_dst;
1732 dst->sin_family = AF_INET;
1733 dst->sin_addr = ip->ip_dst;
1742 * In case we're here due to "to <if>" being used with "keep state",
1743 * check that we're going in the correct direction.
1745 if ((fr != NULL) && (fin->fin_rev != 0)) {
1746 if ((ifp != NULL) && (fdp == &fr->fr_tif)) {
1747 # if (defined(IRIX) && (IRIX >= 605))
1752 } else if (fdp != NULL) {
1753 if (fdp->fd_ip.s_addr != 0)
1754 dst->sin_addr = fdp->fd_ip;
1758 dst->sin_len = sizeof(*dst);
1760 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
1761 !defined(__OpenBSD__)
1763 rtalloc_ign(ro, RTF_CLONING);
1765 rtalloc_ign(ro, RTF_PRCLONING);
1772 if (!fr || !(fr->fr_flags & FR_FASTROUTE)) {
1774 # if (defined(IRIX) && (IRIX >= 605))
1781 if ((ifp == NULL) && (ro->ro_rt != NULL))
1782 ifp = ro->ro_rt->rt_ifp;
1784 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
1785 if (in_localaddr(ip->ip_dst))
1786 error = EHOSTUNREACH;
1788 error = ENETUNREACH;
1789 # if (defined(IRIX) && (IRIX >= 605))
1795 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
1796 #if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1797 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1799 dst = (struct sockaddr_in *)&ro->ro_rt->rt_gateway;
1802 ro->ro_rt->rt_use++;
1804 #if (defined(IRIX) && (IRIX > 602))
1809 * For input packets which are being "fastrouted", they won't
1810 * go back through output filtering and miss their chance to get
1811 * NAT'd and counted.
1813 if (fin->fin_out == 0) {
1814 sifp = fin->fin_ifp;
1815 sout = fin->fin_out;
1818 if ((fin->fin_fr = ipacct[1][fr_active]) &&
1819 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1820 ATOMIC_INCL(frstats[1].fr_acct);
1823 if (!fr || !(fr->fr_flags & FR_RETMASK))
1824 (void) fr_checkstate(ip, fin);
1826 switch (ip_natout(ip, fin))
1839 fin->fin_ifp = sifp;
1840 fin->fin_out = sout;
1845 * If small enough for interface, can just send directly.
1847 if (ip->ip_len <= ifp->if_mtu) {
1849 # if (!defined(__DragonFly__) && !defined(__FreeBSD__) && !(_BSDI_VERSION >= 199510)) && \
1850 !(__NetBSD_Version__ >= 105110000)
1851 ip->ip_id = htons(ip->ip_id);
1853 ip->ip_len = htons(ip->ip_len);
1854 ip->ip_off = htons(ip->ip_off);
1856 # if defined(__NetBSD__) && defined(M_CSUM_IPv4)
1857 # if (__NetBSD_Version__ > 105009999)
1858 if (ifp->if_csum_flags_tx & IFCAP_CSUM_IPv4)
1859 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1860 else if (ip->ip_sum == 0)
1861 ip->ip_sum = in_cksum(m, hlen);
1863 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1864 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1865 else if (ip->ip_sum == 0)
1866 ip->ip_sum = in_cksum(m, hlen);
1870 ip->ip_sum = in_cksum(m, hlen);
1871 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1872 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1874 IFNET_UPPERLOCK(ifp);
1876 lwkt_serialize_enter(ifp->if_serializer);
1877 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
1879 lwkt_serialize_exit(ifp->if_serializer);
1881 IFNET_UPPERUNLOCK(ifp);
1884 lwkt_serialize_enter(ifp->if_serializer);
1885 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst);
1886 lwkt_serialize_exit(ifp->if_serializer);
1892 * Too large for interface; fragment if possible.
1893 * Must be able to put at least 8 bytes per fragment.
1895 if (ip->ip_off & IP_DF) {
1899 len = (ifp->if_mtu - hlen) &~ 7;
1906 int mhlen, firstlen = len;
1907 struct mbuf **mnext = &m->m_act;
1910 * Loop through length of segment after first fragment,
1911 * make new header and copy data of each part and link onto chain.
1914 mhlen = sizeof (struct ip);
1915 for (off = hlen + len; off < ip->ip_len; off += len) {
1916 # ifdef __DragonFly__
1917 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1918 # elif defined(MGETHDR)
1919 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1921 MGET(m, M_DONTWAIT, MT_HEADER);
1928 m->m_data += max_linkhdr;
1930 m->m_off = MMAXOFF - hlen;
1932 mhip = mtod(m, struct ip *);
1933 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1934 if (hlen > sizeof (struct ip)) {
1935 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1936 mhip->ip_hl = mhlen >> 2;
1939 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
1940 if (ip->ip_off & IP_MF)
1941 mhip->ip_off |= IP_MF;
1942 if (off + len >= ip->ip_len)
1943 len = ip->ip_len - off;
1945 mhip->ip_off |= IP_MF;
1946 mhip->ip_len = htons((u_short)(len + mhlen));
1947 m->m_next = m_copy(m0, off, len);
1948 if (m->m_next == 0) {
1949 error = ENOBUFS; /* ??? */
1953 m->m_pkthdr.len = mhlen + len;
1954 m->m_pkthdr.rcvif = NULL;
1956 mhip->ip_off = htons((u_short)mhip->ip_off);
1958 mhip->ip_sum = in_cksum(m, mhlen);
1963 * Update first fragment by trimming what's been copied out
1964 * and updating header, then send each fragment (in order).
1966 m_adj(m0, hlen + firstlen - ip->ip_len);
1967 ip->ip_len = htons((u_short)(hlen + firstlen));
1968 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
1970 ip->ip_sum = in_cksum(m0, hlen);
1972 for (m = m0; m; m = m0) {
1976 lwkt_serialize_enter(ifp->if_serializer);
1977 # if (BSD >= 199306) || (defined(IRIX) && (IRIX >= 605))
1978 error = (*ifp->if_output)(ifp, m,
1979 (struct sockaddr *)dst, ro->ro_rt);
1981 error = (*ifp->if_output)(ifp, m,
1982 (struct sockaddr *)dst);
1984 lwkt_serialize_exit(ifp->if_serializer);
1996 if (ro->ro_rt != NULL) {
2002 if ((error == EMSGSIZE) && (fin->fin_v == 4)) {
2003 sifp = fin->fin_ifp;
2004 code = fin->fin_icode;
2005 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
2007 (void) send_icmp_err(ip, ICMP_UNREACH, fin, 1);
2008 fin->fin_ifp = sifp;
2009 fin->fin_icode = code;
2017 * Return true or false depending on whether the route to the
2018 * given IP address uses the same interface as the one passed.
2020 int fr_verifysrc(ipa, ifp)
2024 struct sockaddr_in *dst;
2025 struct route iproute;
2027 bzero((char *)&iproute, sizeof(iproute));
2028 dst = (struct sockaddr_in *)&iproute.ro_dst;
2029 # if (BSD >= 199306)
2030 dst->sin_len = sizeof(*dst);
2032 dst->sin_family = AF_INET;
2033 dst->sin_addr = ipa;
2034 # if (BSD >= 199306) && !defined(__NetBSD__) && !defined(__bsdi__) && \
2035 !defined(__OpenBSD__)
2037 rtalloc_ign(&iproute, RTF_CLONING);
2039 rtalloc_ign(&iproute, RTF_PRCLONING);
2044 if (iproute.ro_rt == NULL)
2046 return (ifp == iproute.ro_rt->rt_ifp);
2050 # ifdef USE_GETIFNAME
2055 static char workbuf[64];
2057 sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
2063 # if defined(USE_INET6)
2065 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
2066 * or ensure that it is an IPv6 packet that is being forwarded, those are
2067 * expected to be done by the called (ipfr_fastroute).
2069 static int ipfr_fastroute6(m0, mpp, fin, fdp)
2070 struct mbuf *m0, **mpp;
2074 struct route_in6 ip6route;
2075 struct sockaddr_in6 *dst6;
2076 struct route_in6 *ro;
2079 #if defined(OpenBSD) && (OpenBSD >= 200211)
2080 struct route_in6 *ro_pmtu = NULL;
2081 struct in6_addr finaldst;
2089 bzero((caddr_t)ro, sizeof(*ro));
2090 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
2091 dst6->sin6_family = AF_INET6;
2092 dst6->sin6_len = sizeof(struct sockaddr_in6);
2093 dst6->sin6_addr = fin->fin_fi.fi_dst.in6;
2100 if ((fr != NULL) && (fin->fin_rev != 0)) {
2101 if ((ifp != NULL) && (fdp == &fr->fr_tif))
2103 } else if (fdp != NULL) {
2104 if (IP6_NOTZERO(&fdp->fd_ip6))
2105 dst6->sin6_addr = fdp->fd_ip6.in6;
2110 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__)
2112 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
2113 dst6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
2115 rtalloc((struct route *)ro);
2117 if ((ifp == NULL) && (ro->ro_rt != NULL))
2118 ifp = ro->ro_rt->rt_ifp;
2120 if ((ro->ro_rt == NULL) || (ifp == NULL) ||
2121 (ifp != ro->ro_rt->rt_ifp)) {
2122 error = EHOSTUNREACH;
2124 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2125 dst6 = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
2126 ro->ro_rt->rt_use++;
2128 #if defined(OpenBSD) && (OpenBSD >= 200211)
2129 ip6 = mtod(m0, ip6_t *);
2131 finaldst = ip6->ip6_dst;
2132 error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu);
2136 mtu = ND_IFINFO(ifp)->linkmtu;
2138 mtu = nd_ifinfo[ifp->if_index].linkmtu;
2141 if (m0->m_pkthdr.len <= mtu)
2142 error = nd6_output(ifp, fin->fin_ifp, m0,
2146 #if defined(OpenBSD) && (OpenBSD >= 200211)
2151 if (ro->ro_rt != NULL) {
2157 #else /* #ifdef _KERNEL */
2160 # if defined(__sgi) && (IRIX < 605)
2161 static int no_output (struct ifnet *ifp, struct mbuf *m,
2164 static int no_output (struct ifnet *ifp, struct mbuf *m,
2165 struct sockaddr *s, struct rtentry *rt)
2173 # if defined(__sgi) && (IRIX < 605)
2174 static int write_output (struct ifnet *ifp, struct mbuf *m,
2177 static int write_output (struct ifnet *ifp, struct mbuf *m,
2178 struct sockaddr *s, struct rtentry *rt)
2181 ip_t *ip = (ip_t *)m;
2183 static int write_output(ifp, ip)
2191 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2192 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2193 (defined(__DragonFly__))
2194 sprintf(fname, "%s", ifp->if_xname);
2196 sprintf(fname, "%s%d", ifp->if_name, ifp->if_unit);
2198 fd = open(fname, O_WRONLY|O_APPEND);
2203 write(fd, (char *)ip, ntohs(ip->ip_len));
2209 char *get_ifname(ifp)
2212 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2213 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2214 (defined(__DragonFly__))
2215 return ifp->if_xname;
2217 static char fullifname[LIFNAMSIZ];
2219 sprintf(fullifname, "%s%d", ifp->if_name, ifp->if_unit);
2225 struct ifnet *get_unit(ifname, v)
2229 struct ifnet *ifp, **ifa, **old_ifneta;
2231 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2232 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2233 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2234 (defined(__DragonFly__))
2235 if (!strncmp(ifname, ifp->if_xname, sizeof(ifp->if_xname)))
2237 char fullname[LIFNAMSIZ];
2239 sprintf(fullname, "%s%d", ifp->if_name, ifp->if_unit);
2240 if (!strcmp(ifname, fullname))
2246 ifneta = (struct ifnet **)malloc(sizeof(ifp) * 2);
2250 ifneta[0] = (struct ifnet *)calloc(1, sizeof(*ifp));
2257 old_ifneta = ifneta;
2259 ifneta = (struct ifnet **)realloc(ifneta,
2260 (nifs + 1) * sizeof(*ifa));
2266 ifneta[nifs] = NULL;
2267 ifneta[nifs - 1] = (struct ifnet *)malloc(sizeof(*ifp));
2268 if (!ifneta[nifs - 1]) {
2273 ifp = ifneta[nifs - 1];
2275 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2276 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2277 (defined(__DragonFly__))
2278 strncpy(ifp->if_xname, ifname, sizeof(ifp->if_xname));
2280 ifp->if_name = strdup(ifname);
2282 ifname = ifp->if_name;
2283 while (*ifname && !isdigit(*ifname))
2285 if (*ifname && isdigit(*ifname)) {
2286 ifp->if_unit = atoi(ifname);
2291 ifp->if_output = no_output;
2299 struct ifnet *ifp, **ifa;
2303 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
2304 (defined(OpenBSD) && (OpenBSD >= 199603)) || \
2305 (defined(__DragonFly__))
2306 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2307 ifp->if_output = write_output;
2308 sprintf(fname, "/tmp/%s", ifp->if_xname);
2309 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2317 for (ifa = ifneta; ifa && (ifp = *ifa); ifa++) {
2318 ifp->if_output = write_output;
2319 sprintf(fname, "/tmp/%s%d", ifp->if_name, ifp->if_unit);
2320 fd = open(fname, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0600);
2330 int send_reset(ip, fin)
2334 verbose("- TCP RST sent\n");
2339 int send_icmp_err(ip, code, fin, dst)
2345 verbose("- ICMP UNREACHABLE sent\n");
2355 void m_copydata(m, off, len, cp)
2360 bcopy((char *)m + off, cp, len);
2364 int ipfuiomove(buf, len, rwflag, uio)
2369 int left, ioc, num, offset;
2373 if (rwflag == UIO_READ) {
2377 offset = uio->uio_offset;
2379 while ((left > 0) && (ioc < uio->uio_iovcnt)) {
2380 io = uio->uio_iov + ioc;
2384 start = (char *)io->iov_base + offset;
2385 if (start > (char *)io->iov_base + io->iov_len) {
2386 offset -= io->iov_len;
2390 bcopy(buf, start, num);
2391 uio->uio_resid -= num;
2392 uio->uio_offset += num;
2402 #endif /* _KERNEL */