xref: /freebsd-11-stable/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c (revision 62607e8680e944f89cd7b5b7bca10698c66908b2)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * Copyright (C) 2012 by Darren Reed.
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  */
8 #if !defined(lint)
9 static const char sccsid[] = "@(#)ip_fil.c	2.41 6/5/96 (C) 1993-2000 Darren Reed";
10 static const char rcsid[] = "@(#)$Id$";
11 #endif
12 
13 #if defined(KERNEL) || defined(_KERNEL)
14 # undef KERNEL
15 # undef _KERNEL
16 # define	KERNEL	1
17 # define	_KERNEL	1
18 #endif
19 #if defined(__FreeBSD__) && \
20     !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
21 # include "opt_inet6.h"
22 #endif
23 #include <sys/param.h>
24 #include <sys/errno.h>
25 #include <sys/types.h>
26 #include <sys/file.h>
27 #include <sys/fcntl.h>
28 #include <sys/filio.h>
29 #include <sys/time.h>
30 #include <sys/systm.h>
31 #include <sys/dirent.h>
32 #if defined(__FreeBSD__)
33 # include <sys/jail.h>
34 #endif
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/sockopt.h>
38 #include <sys/socket.h>
39 #include <sys/selinfo.h>
40 #include <netinet/tcp_var.h>
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/netisr.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_fib.h>
47 #include <netinet/in_var.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/tcp.h>
52 #include <net/vnet.h>
53 #include <netinet/udp.h>
54 #include <netinet/tcpip.h>
55 #include <netinet/ip_icmp.h>
56 #include "netinet/ip_compat.h"
57 #ifdef USE_INET6
58 # include <netinet/icmp6.h>
59 #endif
60 #include "netinet/ip_fil.h"
61 #include "netinet/ip_nat.h"
62 #include "netinet/ip_frag.h"
63 #include "netinet/ip_state.h"
64 #include "netinet/ip_proxy.h"
65 #include "netinet/ip_auth.h"
66 #include "netinet/ip_sync.h"
67 #include "netinet/ip_lookup.h"
68 #include "netinet/ip_dstlist.h"
69 #ifdef	IPFILTER_SCAN
70 # include "netinet/ip_scan.h"
71 #endif
72 #include "netinet/ip_pool.h"
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #ifdef CSUM_DATA_VALID
76 # include <machine/in_cksum.h>
77 #endif
78 extern	int	ip_optcopy(struct ip *, struct ip *);
79 
80 #ifdef IPFILTER_M_IPFILTER
81 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
82 #endif
83 
84 
85 static	int	ipf_send_ip(fr_info_t *, mb_t *);
86 static void	ipf_timer_func(void *arg);
87 
88 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
89 	.ipf_running		= -2,
90 };
91 #define	V_ipfmain		VNET(ipfmain)
92 
93 #include <sys/conf.h>
94 #include <net/pfil.h>
95 
96 static eventhandler_tag ipf_arrivetag, ipf_departtag;
97 #if 0
98 /*
99  * Disable the "cloner" event handler;  we are getting interface
100  * events before the firewall is fully initiallized and also no vnet
101  * information thus leading to uninitialised memory accesses.
102  * In addition it is unclear why we need it in first place.
103  * If it turns out to be needed, well need a dedicated event handler
104  * for it to deal with the ifc and the correct vnet.
105  */
106 static eventhandler_tag ipf_clonetag;
107 #endif
108 
109 static void ipf_ifevent(void *arg, struct ifnet *ifp);
110 
ipf_ifevent(arg,ifp)111 static void ipf_ifevent(arg, ifp)
112 	void *arg;
113 	struct ifnet *ifp;
114 {
115 
116 	CURVNET_SET(ifp->if_vnet);
117 	if (V_ipfmain.ipf_running > 0)
118 		ipf_sync(&V_ipfmain, NULL);
119 	CURVNET_RESTORE();
120 }
121 
122 
123 
124 static int
ipf_check_wrapper(void * arg,struct mbuf ** mp,struct ifnet * ifp,int dir)125 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
126 {
127 	struct ip *ip = mtod(*mp, struct ip *);
128 	int rv;
129 
130 	/*
131 	 * IPFilter expects evreything in network byte order
132 	 */
133 #if (__FreeBSD_version < 1000019)
134 	ip->ip_len = htons(ip->ip_len);
135 	ip->ip_off = htons(ip->ip_off);
136 #endif
137 	CURVNET_SET(ifp->if_vnet);
138 	rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
139 		       mp);
140 	CURVNET_RESTORE();
141 #if (__FreeBSD_version < 1000019)
142 	if ((rv == 0) && (*mp != NULL)) {
143 		ip = mtod(*mp, struct ip *);
144 		ip->ip_len = ntohs(ip->ip_len);
145 		ip->ip_off = ntohs(ip->ip_off);
146 	}
147 #endif
148 	return rv;
149 }
150 
151 # ifdef USE_INET6
152 #  include <netinet/ip6.h>
153 
154 static int
ipf_check_wrapper6(void * arg,struct mbuf ** mp,struct ifnet * ifp,int dir)155 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
156 {
157 	int error;
158 
159 	CURVNET_SET(ifp->if_vnet);
160 	error = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
161 			  sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp);
162 	CURVNET_RESTORE();
163 	return (error);
164 }
165 # endif
166 #if	defined(IPFILTER_LKM)
ipf_identify(s)167 int ipf_identify(s)
168 	char *s;
169 {
170 	if (strcmp(s, "ipl") == 0)
171 		return 1;
172 	return 0;
173 }
174 #endif /* IPFILTER_LKM */
175 
176 
177 static void
ipf_timer_func(arg)178 ipf_timer_func(arg)
179 	void *arg;
180 {
181 	ipf_main_softc_t *softc = arg;
182 	SPL_INT(s);
183 
184 	SPL_NET(s);
185 	READ_ENTER(&softc->ipf_global);
186 
187         if (softc->ipf_running > 0)
188 		ipf_slowtimer(softc);
189 
190 	if (softc->ipf_running == -1 || softc->ipf_running == 1) {
191 #if 0
192 		softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
193 #endif
194 		callout_init(&softc->ipf_slow_ch, 1);
195 		callout_reset(&softc->ipf_slow_ch,
196 			(hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
197 			ipf_timer_func, softc);
198 	}
199 	RWLOCK_EXIT(&softc->ipf_global);
200 	SPL_X(s);
201 }
202 
203 
204 int
ipfattach(softc)205 ipfattach(softc)
206 	ipf_main_softc_t *softc;
207 {
208 #ifdef USE_SPL
209 	int s;
210 #endif
211 
212 	SPL_NET(s);
213 	if (softc->ipf_running > 0) {
214 		SPL_X(s);
215 		return EBUSY;
216 	}
217 
218 	if (ipf_init_all(softc) < 0) {
219 		SPL_X(s);
220 		return EIO;
221 	}
222 
223 
224 	bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
225 	softc->ipf_running = 1;
226 
227 	if (softc->ipf_control_forwarding & 1)
228 		V_ipforwarding = 1;
229 
230 	SPL_X(s);
231 #if 0
232 	softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
233 				     (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
234 #endif
235 	callout_init(&softc->ipf_slow_ch, 1);
236 	callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
237 		ipf_timer_func, softc);
238 	return 0;
239 }
240 
241 
242 /*
243  * Disable the filter by removing the hooks from the IP input/output
244  * stream.
245  */
246 int
ipfdetach(softc)247 ipfdetach(softc)
248 	ipf_main_softc_t *softc;
249 {
250 #ifdef USE_SPL
251 	int s;
252 #endif
253 
254 	if (softc->ipf_control_forwarding & 2)
255 		V_ipforwarding = 0;
256 
257 	SPL_NET(s);
258 
259 #if 0
260 	if (softc->ipf_slow_ch.callout != NULL)
261 		untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
262 	bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
263 #endif
264 	callout_drain(&softc->ipf_slow_ch);
265 
266 	ipf_fini_all(softc);
267 
268 	softc->ipf_running = -2;
269 
270 	SPL_X(s);
271 
272 	return 0;
273 }
274 
275 
276 /*
277  * Filter ioctl interface.
278  */
279 int
ipfioctl(dev,cmd,data,mode,p)280 ipfioctl(dev, cmd, data, mode, p)
281 	struct thread *p;
282 #define	p_cred	td_ucred
283 #define	p_uid	td_ucred->cr_ruid
284 	struct cdev *dev;
285 	ioctlcmd_t cmd;
286 	caddr_t data;
287 	int mode;
288 {
289 	int error = 0, unit = 0;
290 	SPL_INT(s);
291 
292 	CURVNET_SET(TD_TO_VNET(p));
293         if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
294 	{
295 		V_ipfmain.ipf_interror = 130001;
296 		CURVNET_RESTORE();
297 		return EPERM;
298 	}
299 
300 	unit = GET_MINOR(dev);
301 	if ((IPL_LOGMAX < unit) || (unit < 0)) {
302 		V_ipfmain.ipf_interror = 130002;
303 		CURVNET_RESTORE();
304 		return ENXIO;
305 	}
306 
307 	if (V_ipfmain.ipf_running <= 0) {
308 		if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
309 			V_ipfmain.ipf_interror = 130003;
310 			CURVNET_RESTORE();
311 			return EIO;
312 		}
313 		if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
314 		    cmd != SIOCIPFSET && cmd != SIOCFRENB &&
315 		    cmd != SIOCGETFS && cmd != SIOCGETFF &&
316 		    cmd != SIOCIPFINTERROR) {
317 			V_ipfmain.ipf_interror = 130004;
318 			CURVNET_RESTORE();
319 			return EIO;
320 		}
321 	}
322 
323 	SPL_NET(s);
324 
325 	error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
326 	CURVNET_RESTORE();
327 	if (error != -1) {
328 		SPL_X(s);
329 		return error;
330 	}
331 
332 	SPL_X(s);
333 
334 	return error;
335 }
336 
337 
338 /*
339  * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
340  * requires a large amount of setting up and isn't any more efficient.
341  */
342 int
ipf_send_reset(fin)343 ipf_send_reset(fin)
344 	fr_info_t *fin;
345 {
346 	struct tcphdr *tcp, *tcp2;
347 	int tlen = 0, hlen;
348 	struct mbuf *m;
349 #ifdef USE_INET6
350 	ip6_t *ip6;
351 #endif
352 	ip_t *ip;
353 
354 	tcp = fin->fin_dp;
355 	if (tcp->th_flags & TH_RST)
356 		return -1;		/* feedback loop */
357 
358 	if (ipf_checkl4sum(fin) == -1)
359 		return -1;
360 
361 	tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
362 			((tcp->th_flags & TH_SYN) ? 1 : 0) +
363 			((tcp->th_flags & TH_FIN) ? 1 : 0);
364 
365 #ifdef USE_INET6
366 	hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
367 #else
368 	hlen = sizeof(ip_t);
369 #endif
370 #ifdef MGETHDR
371 	MGETHDR(m, M_NOWAIT, MT_HEADER);
372 #else
373 	MGET(m, M_NOWAIT, MT_HEADER);
374 #endif
375 	if (m == NULL)
376 		return -1;
377 	if (sizeof(*tcp2) + hlen > MLEN) {
378 		if (!(MCLGET(m, M_NOWAIT))) {
379 			FREE_MB_T(m);
380 			return -1;
381 		}
382 	}
383 
384 	m->m_len = sizeof(*tcp2) + hlen;
385 	m->m_data += max_linkhdr;
386 	m->m_pkthdr.len = m->m_len;
387 	m->m_pkthdr.rcvif = (struct ifnet *)0;
388 	ip = mtod(m, struct ip *);
389 	bzero((char *)ip, hlen);
390 #ifdef USE_INET6
391 	ip6 = (ip6_t *)ip;
392 #endif
393 	tcp2 = (struct tcphdr *)((char *)ip + hlen);
394 	tcp2->th_sport = tcp->th_dport;
395 	tcp2->th_dport = tcp->th_sport;
396 
397 	if (tcp->th_flags & TH_ACK) {
398 		tcp2->th_seq = tcp->th_ack;
399 		tcp2->th_flags = TH_RST;
400 		tcp2->th_ack = 0;
401 	} else {
402 		tcp2->th_seq = 0;
403 		tcp2->th_ack = ntohl(tcp->th_seq);
404 		tcp2->th_ack += tlen;
405 		tcp2->th_ack = htonl(tcp2->th_ack);
406 		tcp2->th_flags = TH_RST|TH_ACK;
407 	}
408 	TCP_X2_A(tcp2, 0);
409 	TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
410 	tcp2->th_win = tcp->th_win;
411 	tcp2->th_sum = 0;
412 	tcp2->th_urp = 0;
413 
414 #ifdef USE_INET6
415 	if (fin->fin_v == 6) {
416 		ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
417 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
418 		ip6->ip6_nxt = IPPROTO_TCP;
419 		ip6->ip6_hlim = 0;
420 		ip6->ip6_src = fin->fin_dst6.in6;
421 		ip6->ip6_dst = fin->fin_src6.in6;
422 		tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
423 					 sizeof(*ip6), sizeof(*tcp2));
424 		return ipf_send_ip(fin, m);
425 	}
426 #endif
427 	ip->ip_p = IPPROTO_TCP;
428 	ip->ip_len = htons(sizeof(struct tcphdr));
429 	ip->ip_src.s_addr = fin->fin_daddr;
430 	ip->ip_dst.s_addr = fin->fin_saddr;
431 	tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
432 	ip->ip_len = htons(hlen + sizeof(*tcp2));
433 	return ipf_send_ip(fin, m);
434 }
435 
436 
437 /*
438  * ip_len must be in network byte order when called.
439  */
440 static int
ipf_send_ip(fin,m)441 ipf_send_ip(fin, m)
442 	fr_info_t *fin;
443 	mb_t *m;
444 {
445 	fr_info_t fnew;
446 	ip_t *ip, *oip;
447 	int hlen;
448 
449 	ip = mtod(m, ip_t *);
450 	bzero((char *)&fnew, sizeof(fnew));
451 	fnew.fin_main_soft = fin->fin_main_soft;
452 
453 	IP_V_A(ip, fin->fin_v);
454 	switch (fin->fin_v)
455 	{
456 	case 4 :
457 		oip = fin->fin_ip;
458 		hlen = sizeof(*oip);
459 		fnew.fin_v = 4;
460 		fnew.fin_p = ip->ip_p;
461 		fnew.fin_plen = ntohs(ip->ip_len);
462 		IP_HL_A(ip, sizeof(*oip) >> 2);
463 		ip->ip_tos = oip->ip_tos;
464 		ip->ip_id = fin->fin_ip->ip_id;
465 		ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
466 		ip->ip_ttl = V_ip_defttl;
467 		ip->ip_sum = 0;
468 		break;
469 #ifdef USE_INET6
470 	case 6 :
471 	{
472 		ip6_t *ip6 = (ip6_t *)ip;
473 
474 		ip6->ip6_vfc = 0x60;
475 		ip6->ip6_hlim = IPDEFTTL;
476 
477 		hlen = sizeof(*ip6);
478 		fnew.fin_p = ip6->ip6_nxt;
479 		fnew.fin_v = 6;
480 		fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
481 		break;
482 	}
483 #endif
484 	default :
485 		return EINVAL;
486 	}
487 #ifdef IPSEC
488 	m->m_pkthdr.rcvif = NULL;
489 #endif
490 
491 	fnew.fin_ifp = fin->fin_ifp;
492 	fnew.fin_flx = FI_NOCKSUM;
493 	fnew.fin_m = m;
494 	fnew.fin_ip = ip;
495 	fnew.fin_mp = &m;
496 	fnew.fin_hlen = hlen;
497 	fnew.fin_dp = (char *)ip + hlen;
498 	(void) ipf_makefrip(hlen, ip, &fnew);
499 
500 	return ipf_fastroute(m, &m, &fnew, NULL);
501 }
502 
503 
504 int
ipf_send_icmp_err(type,fin,dst)505 ipf_send_icmp_err(type, fin, dst)
506 	int type;
507 	fr_info_t *fin;
508 	int dst;
509 {
510 	int err, hlen, xtra, iclen, ohlen, avail, code;
511 	struct in_addr dst4;
512 	struct icmp *icmp;
513 	struct mbuf *m;
514 	i6addr_t dst6;
515 	void *ifp;
516 #ifdef USE_INET6
517 	ip6_t *ip6;
518 #endif
519 	ip_t *ip, *ip2;
520 
521 	if ((type < 0) || (type >= ICMP_MAXTYPE))
522 		return -1;
523 
524 	code = fin->fin_icode;
525 #ifdef USE_INET6
526 	/* See NetBSD ip_fil_netbsd.c r1.4: */
527 	if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
528 		return -1;
529 #endif
530 
531 	if (ipf_checkl4sum(fin) == -1)
532 		return -1;
533 #ifdef MGETHDR
534 	MGETHDR(m, M_NOWAIT, MT_HEADER);
535 #else
536 	MGET(m, M_NOWAIT, MT_HEADER);
537 #endif
538 	if (m == NULL)
539 		return -1;
540 	avail = MHLEN;
541 
542 	xtra = 0;
543 	hlen = 0;
544 	ohlen = 0;
545 	dst4.s_addr = 0;
546 	ifp = fin->fin_ifp;
547 	if (fin->fin_v == 4) {
548 		if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
549 			switch (ntohs(fin->fin_data[0]) >> 8)
550 			{
551 			case ICMP_ECHO :
552 			case ICMP_TSTAMP :
553 			case ICMP_IREQ :
554 			case ICMP_MASKREQ :
555 				break;
556 			default :
557 				FREE_MB_T(m);
558 				return 0;
559 			}
560 
561 		if (dst == 0) {
562 			if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
563 					&dst6, NULL) == -1) {
564 				FREE_MB_T(m);
565 				return -1;
566 			}
567 			dst4 = dst6.in4;
568 		} else
569 			dst4.s_addr = fin->fin_daddr;
570 
571 		hlen = sizeof(ip_t);
572 		ohlen = fin->fin_hlen;
573 		iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
574 		if (fin->fin_hlen < fin->fin_plen)
575 			xtra = MIN(fin->fin_dlen, 8);
576 		else
577 			xtra = 0;
578 	}
579 
580 #ifdef USE_INET6
581 	else if (fin->fin_v == 6) {
582 		hlen = sizeof(ip6_t);
583 		ohlen = sizeof(ip6_t);
584 		iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
585 		type = icmptoicmp6types[type];
586 		if (type == ICMP6_DST_UNREACH)
587 			code = icmptoicmp6unreach[code];
588 
589 		if (iclen + max_linkhdr + fin->fin_plen > avail) {
590 			if (!(MCLGET(m, M_NOWAIT))) {
591 				FREE_MB_T(m);
592 				return -1;
593 			}
594 			avail = MCLBYTES;
595 		}
596 		xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
597 		xtra = MIN(xtra, IPV6_MMTU - iclen);
598 		if (dst == 0) {
599 			if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
600 					&dst6, NULL) == -1) {
601 				FREE_MB_T(m);
602 				return -1;
603 			}
604 		} else
605 			dst6 = fin->fin_dst6;
606 	}
607 #endif
608 	else {
609 		FREE_MB_T(m);
610 		return -1;
611 	}
612 
613 	avail -= (max_linkhdr + iclen);
614 	if (avail < 0) {
615 		FREE_MB_T(m);
616 		return -1;
617 	}
618 	if (xtra > avail)
619 		xtra = avail;
620 	iclen += xtra;
621 	m->m_data += max_linkhdr;
622 	m->m_pkthdr.rcvif = (struct ifnet *)0;
623 	m->m_pkthdr.len = iclen;
624 	m->m_len = iclen;
625 	ip = mtod(m, ip_t *);
626 	icmp = (struct icmp *)((char *)ip + hlen);
627 	ip2 = (ip_t *)&icmp->icmp_ip;
628 
629 	icmp->icmp_type = type;
630 	icmp->icmp_code = fin->fin_icode;
631 	icmp->icmp_cksum = 0;
632 #ifdef icmp_nextmtu
633 	if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
634 		if (fin->fin_mtu != 0) {
635 			icmp->icmp_nextmtu = htons(fin->fin_mtu);
636 
637 		} else if (ifp != NULL) {
638 			icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
639 
640 		} else {	/* make up a number... */
641 			icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
642 		}
643 	}
644 #endif
645 
646 	bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
647 
648 #ifdef USE_INET6
649 	ip6 = (ip6_t *)ip;
650 	if (fin->fin_v == 6) {
651 		ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
652 		ip6->ip6_plen = htons(iclen - hlen);
653 		ip6->ip6_nxt = IPPROTO_ICMPV6;
654 		ip6->ip6_hlim = 0;
655 		ip6->ip6_src = dst6.in6;
656 		ip6->ip6_dst = fin->fin_src6.in6;
657 		if (xtra > 0)
658 			bcopy((char *)fin->fin_ip + ohlen,
659 			      (char *)&icmp->icmp_ip + ohlen, xtra);
660 		icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
661 					     sizeof(*ip6), iclen - hlen);
662 	} else
663 #endif
664 	{
665 		ip->ip_p = IPPROTO_ICMP;
666 		ip->ip_src.s_addr = dst4.s_addr;
667 		ip->ip_dst.s_addr = fin->fin_saddr;
668 
669 		if (xtra > 0)
670 			bcopy((char *)fin->fin_ip + ohlen,
671 			      (char *)&icmp->icmp_ip + ohlen, xtra);
672 		icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
673 					     sizeof(*icmp) + 8);
674 		ip->ip_len = htons(iclen);
675 		ip->ip_p = IPPROTO_ICMP;
676 	}
677 	err = ipf_send_ip(fin, m);
678 	return err;
679 }
680 
681 
682 
683 
684 /*
685  * m0 - pointer to mbuf where the IP packet starts
686  * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
687  */
688 int
ipf_fastroute(m0,mpp,fin,fdp)689 ipf_fastroute(m0, mpp, fin, fdp)
690 	mb_t *m0, **mpp;
691 	fr_info_t *fin;
692 	frdest_t *fdp;
693 {
694 	register struct ip *ip, *mhip;
695 	register struct mbuf *m = *mpp;
696 	int len, off, error = 0, hlen, code;
697 	struct ifnet *ifp, *sifp;
698 	struct sockaddr_in dst;
699 	struct nhop4_extended nh4;
700 	u_long fibnum = 0;
701 	u_short ip_off;
702 	frdest_t node;
703 	frentry_t *fr;
704 
705 #ifdef M_WRITABLE
706 	/*
707 	* HOT FIX/KLUDGE:
708 	*
709 	* If the mbuf we're about to send is not writable (because of
710 	* a cluster reference, for example) we'll need to make a copy
711 	* of it since this routine modifies the contents.
712 	*
713 	* If you have non-crappy network hardware that can transmit data
714 	* from the mbuf, rather than making a copy, this is gonna be a
715 	* problem.
716 	*/
717 	if (M_WRITABLE(m) == 0) {
718 		m0 = m_dup(m, M_NOWAIT);
719 		if (m0 != NULL) {
720 			FREE_MB_T(m);
721 			m = m0;
722 			*mpp = m;
723 		} else {
724 			error = ENOBUFS;
725 			FREE_MB_T(m);
726 			goto done;
727 		}
728 	}
729 #endif
730 
731 #ifdef USE_INET6
732 	if (fin->fin_v == 6) {
733 		/*
734 		 * currently "to <if>" and "to <if>:ip#" are not supported
735 		 * for IPv6
736 		 */
737 		return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
738 	}
739 #endif
740 
741 	hlen = fin->fin_hlen;
742 	ip = mtod(m0, struct ip *);
743 	ifp = NULL;
744 
745 	/*
746 	 * Route packet.
747 	 */
748 	bzero(&dst, sizeof (dst));
749 	dst.sin_family = AF_INET;
750 	dst.sin_addr = ip->ip_dst;
751 	dst.sin_len = sizeof(dst);
752 
753 	fr = fin->fin_fr;
754 	if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
755 	    (fdp->fd_type == FRD_DSTLIST)) {
756 		if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
757 			fdp = &node;
758 	}
759 
760 	if (fdp != NULL)
761 		ifp = fdp->fd_ptr;
762 	else
763 		ifp = fin->fin_ifp;
764 
765 	if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
766 		error = -2;
767 		goto bad;
768 	}
769 
770 	if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
771 		dst.sin_addr = fdp->fd_ip;
772 
773 	fibnum = M_GETFIB(m0);
774 	if (fib4_lookup_nh_ext(fibnum, dst.sin_addr, NHR_REF, 0, &nh4) != 0) {
775 		if (in_localaddr(ip->ip_dst))
776 			error = EHOSTUNREACH;
777 		else
778 			error = ENETUNREACH;
779 		goto bad;
780 	}
781 
782 	if (ifp == NULL)
783 		ifp = nh4.nh_ifp;
784 	if (nh4.nh_flags & NHF_GATEWAY)
785 		dst.sin_addr = nh4.nh_addr;
786 
787 	/*
788 	 * For input packets which are being "fastrouted", they won't
789 	 * go back through output filtering and miss their chance to get
790 	 * NAT'd and counted.  Duplicated packets aren't considered to be
791 	 * part of the normal packet stream, so do not NAT them or pass
792 	 * them through stateful checking, etc.
793 	 */
794 	if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
795 		sifp = fin->fin_ifp;
796 		fin->fin_ifp = ifp;
797 		fin->fin_out = 1;
798 		(void) ipf_acctpkt(fin, NULL);
799 		fin->fin_fr = NULL;
800 		if (!fr || !(fr->fr_flags & FR_RETMASK)) {
801 			u_32_t pass;
802 
803 			(void) ipf_state_check(fin, &pass);
804 		}
805 
806 		switch (ipf_nat_checkout(fin, NULL))
807 		{
808 		case 0 :
809 			break;
810 		case 1 :
811 			ip->ip_sum = 0;
812 			break;
813 		case -1 :
814 			error = -1;
815 			goto bad;
816 			break;
817 		}
818 
819 		fin->fin_ifp = sifp;
820 		fin->fin_out = 0;
821 	} else
822 		ip->ip_sum = 0;
823 	/*
824 	 * If small enough for interface, can just send directly.
825 	 */
826 	if (ntohs(ip->ip_len) <= ifp->if_mtu) {
827 		if (!ip->ip_sum)
828 			ip->ip_sum = in_cksum(m, hlen);
829 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
830 			    NULL
831 			);
832 		goto done;
833 	}
834 	/*
835 	 * Too large for interface; fragment if possible.
836 	 * Must be able to put at least 8 bytes per fragment.
837 	 */
838 	ip_off = ntohs(ip->ip_off);
839 	if (ip_off & IP_DF) {
840 		error = EMSGSIZE;
841 		goto bad;
842 	}
843 	len = (ifp->if_mtu - hlen) &~ 7;
844 	if (len < 8) {
845 		error = EMSGSIZE;
846 		goto bad;
847 	}
848 
849     {
850 	int mhlen, firstlen = len;
851 	struct mbuf **mnext = &m->m_act;
852 
853 	/*
854 	 * Loop through length of segment after first fragment,
855 	 * make new header and copy data of each part and link onto chain.
856 	 */
857 	m0 = m;
858 	mhlen = sizeof (struct ip);
859 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
860 #ifdef MGETHDR
861 		MGETHDR(m, M_NOWAIT, MT_HEADER);
862 #else
863 		MGET(m, M_NOWAIT, MT_HEADER);
864 #endif
865 		if (m == NULL) {
866 			m = m0;
867 			error = ENOBUFS;
868 			goto bad;
869 		}
870 		m->m_data += max_linkhdr;
871 		mhip = mtod(m, struct ip *);
872 		bcopy((char *)ip, (char *)mhip, sizeof(*ip));
873 		if (hlen > sizeof (struct ip)) {
874 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
875 			IP_HL_A(mhip, mhlen >> 2);
876 		}
877 		m->m_len = mhlen;
878 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
879 		if (off + len >= ntohs(ip->ip_len))
880 			len = ntohs(ip->ip_len) - off;
881 		else
882 			mhip->ip_off |= IP_MF;
883 		mhip->ip_len = htons((u_short)(len + mhlen));
884 		*mnext = m;
885 		m->m_next = m_copy(m0, off, len);
886 		if (m->m_next == 0) {
887 			error = ENOBUFS;	/* ??? */
888 			goto sendorfree;
889 		}
890 		m->m_pkthdr.len = mhlen + len;
891 		m->m_pkthdr.rcvif = NULL;
892 		mhip->ip_off = htons((u_short)mhip->ip_off);
893 		mhip->ip_sum = 0;
894 		mhip->ip_sum = in_cksum(m, mhlen);
895 		mnext = &m->m_act;
896 	}
897 	/*
898 	 * Update first fragment by trimming what's been copied out
899 	 * and updating header, then send each fragment (in order).
900 	 */
901 	m_adj(m0, hlen + firstlen - ip->ip_len);
902 	ip->ip_len = htons((u_short)(hlen + firstlen));
903 	ip->ip_off = htons((u_short)IP_MF);
904 	ip->ip_sum = 0;
905 	ip->ip_sum = in_cksum(m0, hlen);
906 sendorfree:
907 	for (m = m0; m; m = m0) {
908 		m0 = m->m_act;
909 		m->m_act = 0;
910 		if (error == 0)
911 			error = (*ifp->if_output)(ifp, m,
912 			    (struct sockaddr *)&dst,
913 			    NULL
914 			    );
915 		else
916 			FREE_MB_T(m);
917 	}
918     }
919 done:
920 	if (!error)
921 		V_ipfmain.ipf_frouteok[0]++;
922 	else
923 		V_ipfmain.ipf_frouteok[1]++;
924 
925 	return 0;
926 bad:
927 	if (error == EMSGSIZE) {
928 		sifp = fin->fin_ifp;
929 		code = fin->fin_icode;
930 		fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
931 		fin->fin_ifp = ifp;
932 		(void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
933 		fin->fin_ifp = sifp;
934 		fin->fin_icode = code;
935 	}
936 	FREE_MB_T(m);
937 	goto done;
938 }
939 
940 
941 int
ipf_verifysrc(fin)942 ipf_verifysrc(fin)
943 	fr_info_t *fin;
944 {
945 	struct nhop4_basic nh4;
946 
947 	if (fib4_lookup_nh_basic(0, fin->fin_src, 0, 0, &nh4) != 0)
948 		return (0);
949 	return (fin->fin_ifp == nh4.nh_ifp);
950 }
951 
952 
953 /*
954  * return the first IP Address associated with an interface
955  */
956 int
ipf_ifpaddr(softc,v,atype,ifptr,inp,inpmask)957 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
958 	ipf_main_softc_t *softc;
959 	int v, atype;
960 	void *ifptr;
961 	i6addr_t *inp, *inpmask;
962 {
963 #ifdef USE_INET6
964 	struct in6_addr *inp6 = NULL;
965 #endif
966 	struct sockaddr *sock, *mask;
967 	struct sockaddr_in *sin;
968 	struct ifaddr *ifa;
969 	struct ifnet *ifp;
970 
971 	if ((ifptr == NULL) || (ifptr == (void *)-1))
972 		return -1;
973 
974 	sin = NULL;
975 	ifp = ifptr;
976 
977 	if (v == 4)
978 		inp->in4.s_addr = 0;
979 #ifdef USE_INET6
980 	else if (v == 6)
981 		bzero((char *)inp, sizeof(*inp));
982 #endif
983 	ifa = TAILQ_FIRST(&ifp->if_addrhead);
984 
985 	sock = ifa->ifa_addr;
986 	while (sock != NULL && ifa != NULL) {
987 		sin = (struct sockaddr_in *)sock;
988 		if ((v == 4) && (sin->sin_family == AF_INET))
989 			break;
990 #ifdef USE_INET6
991 		if ((v == 6) && (sin->sin_family == AF_INET6)) {
992 			inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
993 			if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
994 			    !IN6_IS_ADDR_LOOPBACK(inp6))
995 				break;
996 		}
997 #endif
998 		ifa = TAILQ_NEXT(ifa, ifa_link);
999 		if (ifa != NULL)
1000 			sock = ifa->ifa_addr;
1001 	}
1002 
1003 	if (ifa == NULL || sin == NULL)
1004 		return -1;
1005 
1006 	mask = ifa->ifa_netmask;
1007 	if (atype == FRI_BROADCAST)
1008 		sock = ifa->ifa_broadaddr;
1009 	else if (atype == FRI_PEERADDR)
1010 		sock = ifa->ifa_dstaddr;
1011 
1012 	if (sock == NULL)
1013 		return -1;
1014 
1015 #ifdef USE_INET6
1016 	if (v == 6) {
1017 		return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1018 					 (struct sockaddr_in6 *)mask,
1019 					 inp, inpmask);
1020 	}
1021 #endif
1022 	return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1023 				 (struct sockaddr_in *)mask,
1024 				 &inp->in4, &inpmask->in4);
1025 }
1026 
1027 
1028 u_32_t
ipf_newisn(fin)1029 ipf_newisn(fin)
1030 	fr_info_t *fin;
1031 {
1032 	u_32_t newiss;
1033 	newiss = arc4random();
1034 	return newiss;
1035 }
1036 
1037 
1038 INLINE int
ipf_checkv4sum(fin)1039 ipf_checkv4sum(fin)
1040 	fr_info_t *fin;
1041 {
1042 #ifdef CSUM_DATA_VALID
1043 	int manual = 0;
1044 	u_short sum;
1045 	ip_t *ip;
1046 	mb_t *m;
1047 
1048 	if ((fin->fin_flx & FI_NOCKSUM) != 0)
1049 		return 0;
1050 
1051 	if ((fin->fin_flx & FI_SHORT) != 0)
1052 		return 1;
1053 
1054 	if (fin->fin_cksum != FI_CK_NEEDED)
1055 		return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1056 
1057 	m = fin->fin_m;
1058 	if (m == NULL) {
1059 		manual = 1;
1060 		goto skipauto;
1061 	}
1062 	ip = fin->fin_ip;
1063 
1064 	if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1065 	    CSUM_IP_CHECKED) {
1066 		fin->fin_cksum = FI_CK_BAD;
1067 		fin->fin_flx |= FI_BAD;
1068 		DT2(ipf_fi_bad_checkv4sum_csum_ip_checked, fr_info_t *, fin, u_int, m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID));
1069 		return -1;
1070 	}
1071 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1072 		/* Depending on the driver, UDP may have zero checksum */
1073 		if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1074 		    (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1075 			udphdr_t *udp = fin->fin_dp;
1076 			if (udp->uh_sum == 0) {
1077 				/*
1078 				 * we're good no matter what the hardware
1079 				 * checksum flags and csum_data say (handling
1080 				 * of csum_data for zero UDP checksum is not
1081 				 * consistent across all drivers)
1082 				 */
1083 				fin->fin_cksum = 1;
1084 				return 0;
1085 			}
1086 		}
1087 
1088 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1089 			sum = m->m_pkthdr.csum_data;
1090 		else
1091 			sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1092 					htonl(m->m_pkthdr.csum_data +
1093 					fin->fin_dlen + fin->fin_p));
1094 		sum ^= 0xffff;
1095 		if (sum != 0) {
1096 			fin->fin_cksum = FI_CK_BAD;
1097 			fin->fin_flx |= FI_BAD;
1098 			DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1099 		} else {
1100 			fin->fin_cksum = FI_CK_SUMOK;
1101 			return 0;
1102 		}
1103 	} else {
1104 		if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1105 			fin->fin_cksum = FI_CK_L4FULL;
1106 			return 0;
1107 		} else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1108 			   m->m_pkthdr.csum_flags == CSUM_UDP) {
1109 			fin->fin_cksum = FI_CK_L4PART;
1110 			return 0;
1111 		} else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1112 			fin->fin_cksum = FI_CK_L4PART;
1113 			return 0;
1114 		} else {
1115 			manual = 1;
1116 		}
1117 	}
1118 skipauto:
1119 	if (manual != 0) {
1120 		if (ipf_checkl4sum(fin) == -1) {
1121 			fin->fin_flx |= FI_BAD;
1122 			DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1123 			return -1;
1124 		}
1125 	}
1126 #else
1127 	if (ipf_checkl4sum(fin) == -1) {
1128 		fin->fin_flx |= FI_BAD;
1129 		DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1130 		return -1;
1131 	}
1132 #endif
1133 	return 0;
1134 }
1135 
1136 
1137 #ifdef USE_INET6
1138 INLINE int
ipf_checkv6sum(fin)1139 ipf_checkv6sum(fin)
1140 	fr_info_t *fin;
1141 {
1142 	if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1143 		DT(ipf_checkv6sum_fi_nocksum);
1144 		return 0;
1145 	}
1146 
1147 	if ((fin->fin_flx & FI_SHORT) != 0) {
1148 		DT(ipf_checkv6sum_fi_short);
1149 		return 1;
1150 	}
1151 
1152 	if (fin->fin_cksum != FI_CK_NEEDED) {
1153 		DT(ipf_checkv6sum_fi_ck_needed);
1154 		return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1155 	}
1156 
1157 	if (ipf_checkl4sum(fin) == -1) {
1158 		fin->fin_flx |= FI_BAD;
1159 		DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1160 		return -1;
1161 	}
1162 	return 0;
1163 }
1164 #endif /* USE_INET6 */
1165 
1166 
1167 size_t
mbufchainlen(m0)1168 mbufchainlen(m0)
1169 	struct mbuf *m0;
1170 {
1171 	size_t len;
1172 
1173 	if ((m0->m_flags & M_PKTHDR) != 0) {
1174 		len = m0->m_pkthdr.len;
1175 	} else {
1176 		struct mbuf *m;
1177 
1178 		for (m = m0, len = 0; m != NULL; m = m->m_next)
1179 			len += m->m_len;
1180 	}
1181 	return len;
1182 }
1183 
1184 
1185 /* ------------------------------------------------------------------------ */
1186 /* Function:    ipf_pullup                                                  */
1187 /* Returns:     NULL == pullup failed, else pointer to protocol header      */
1188 /* Parameters:  xmin(I)- pointer to buffer where data packet starts         */
1189 /*              fin(I) - pointer to packet information                      */
1190 /*              len(I) - number of bytes to pullup                          */
1191 /*                                                                          */
1192 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1193 /* single buffer for ease of access.  Operating system native functions are */
1194 /* used to manage buffers - if necessary.  If the entire packet ends up in  */
1195 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1196 /* not been called.  Both fin_ip and fin_dp are updated before exiting _IF_ */
1197 /* and ONLY if the pullup succeeds.                                         */
1198 /*                                                                          */
1199 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1200 /* of buffers that starts at *fin->fin_mp.                                  */
1201 /* ------------------------------------------------------------------------ */
1202 void *
ipf_pullup(xmin,fin,len)1203 ipf_pullup(xmin, fin, len)
1204 	mb_t *xmin;
1205 	fr_info_t *fin;
1206 	int len;
1207 {
1208 	int dpoff, ipoff;
1209 	mb_t *m = xmin;
1210 	char *ip;
1211 
1212 	if (m == NULL)
1213 		return NULL;
1214 
1215 	ip = (char *)fin->fin_ip;
1216 	if ((fin->fin_flx & FI_COALESCE) != 0)
1217 		return ip;
1218 
1219 	ipoff = fin->fin_ipoff;
1220 	if (fin->fin_dp != NULL)
1221 		dpoff = (char *)fin->fin_dp - (char *)ip;
1222 	else
1223 		dpoff = 0;
1224 
1225 	if (M_LEN(m) < len) {
1226 		mb_t *n = *fin->fin_mp;
1227 		/*
1228 		 * Assume that M_PKTHDR is set and just work with what is left
1229 		 * rather than check..
1230 		 * Should not make any real difference, anyway.
1231 		 */
1232 		if (m != n) {
1233 			/*
1234 			 * Record the mbuf that points to the mbuf that we're
1235 			 * about to go to work on so that we can update the
1236 			 * m_next appropriately later.
1237 			 */
1238 			for (; n->m_next != m; n = n->m_next)
1239 				;
1240 		} else {
1241 			n = NULL;
1242 		}
1243 
1244 #ifdef MHLEN
1245 		if (len > MHLEN)
1246 #else
1247 		if (len > MLEN)
1248 #endif
1249 		{
1250 #ifdef HAVE_M_PULLDOWN
1251 			if (m_pulldown(m, 0, len, NULL) == NULL)
1252 				m = NULL;
1253 #else
1254 			FREE_MB_T(*fin->fin_mp);
1255 			m = NULL;
1256 			n = NULL;
1257 #endif
1258 		} else
1259 		{
1260 			m = m_pullup(m, len);
1261 		}
1262 		if (n != NULL)
1263 			n->m_next = m;
1264 		if (m == NULL) {
1265 			/*
1266 			 * When n is non-NULL, it indicates that m pointed to
1267 			 * a sub-chain (tail) of the mbuf and that the head
1268 			 * of this chain has not yet been free'd.
1269 			 */
1270 			if (n != NULL) {
1271 				FREE_MB_T(*fin->fin_mp);
1272 			}
1273 
1274 			*fin->fin_mp = NULL;
1275 			fin->fin_m = NULL;
1276 			return NULL;
1277 		}
1278 
1279 		if (n == NULL)
1280 			*fin->fin_mp = m;
1281 
1282 		while (M_LEN(m) == 0) {
1283 			m = m->m_next;
1284 		}
1285 		fin->fin_m = m;
1286 		ip = MTOD(m, char *) + ipoff;
1287 
1288 		fin->fin_ip = (ip_t *)ip;
1289 		if (fin->fin_dp != NULL)
1290 			fin->fin_dp = (char *)fin->fin_ip + dpoff;
1291 		if (fin->fin_fraghdr != NULL)
1292 			fin->fin_fraghdr = (char *)ip +
1293 					   ((char *)fin->fin_fraghdr -
1294 					    (char *)fin->fin_ip);
1295 	}
1296 
1297 	if (len == fin->fin_plen)
1298 		fin->fin_flx |= FI_COALESCE;
1299 	return ip;
1300 }
1301 
1302 
1303 int
ipf_inject(fin,m)1304 ipf_inject(fin, m)
1305 	fr_info_t *fin;
1306 	mb_t *m;
1307 {
1308 	int error = 0;
1309 
1310 	if (fin->fin_out == 0) {
1311 		netisr_dispatch(NETISR_IP, m);
1312 	} else {
1313 		fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1314 		fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1315 		error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1316 	}
1317 
1318 	return error;
1319 }
1320 
ipf_pfil_unhook(void)1321 int ipf_pfil_unhook(void) {
1322 	struct pfil_head *ph_inet;
1323 #ifdef USE_INET6
1324 	struct pfil_head *ph_inet6;
1325 #endif
1326 
1327 	ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1328 	if (ph_inet != NULL)
1329 		pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1330 		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1331 # ifdef USE_INET6
1332 	ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1333 	if (ph_inet6 != NULL)
1334 		pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1335 		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1336 # endif
1337 
1338 	return (0);
1339 }
1340 
ipf_pfil_hook(void)1341 int ipf_pfil_hook(void) {
1342 	struct pfil_head *ph_inet;
1343 #ifdef USE_INET6
1344 	struct pfil_head *ph_inet6;
1345 #endif
1346 
1347 	ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1348 #    ifdef USE_INET6
1349 	ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1350 #    endif
1351 	if (ph_inet == NULL
1352 #    ifdef USE_INET6
1353 	    && ph_inet6 == NULL
1354 #    endif
1355 	   ) {
1356 		return ENODEV;
1357 	}
1358 
1359 	if (ph_inet != NULL)
1360 		pfil_add_hook((void *)ipf_check_wrapper, NULL,
1361 		    PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1362 #  ifdef USE_INET6
1363 	if (ph_inet6 != NULL)
1364 		pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1365 				      PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1366 #  endif
1367 	return (0);
1368 }
1369 
1370 void
ipf_event_reg(void)1371 ipf_event_reg(void)
1372 {
1373 	ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1374 					       ipf_ifevent, NULL, \
1375 					       EVENTHANDLER_PRI_ANY);
1376 	ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1377 					       ipf_ifevent, NULL, \
1378 					       EVENTHANDLER_PRI_ANY);
1379 #if 0
1380 	ipf_clonetag  = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1381 					       NULL, EVENTHANDLER_PRI_ANY);
1382 #endif
1383 }
1384 
1385 void
ipf_event_dereg(void)1386 ipf_event_dereg(void)
1387 {
1388 	if (ipf_arrivetag != NULL) {
1389 		EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1390 	}
1391 	if (ipf_departtag != NULL) {
1392 		EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1393 	}
1394 #if 0
1395 	if (ipf_clonetag != NULL) {
1396 		EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1397 	}
1398 #endif
1399 }
1400 
1401 
1402 u_32_t
ipf_random()1403 ipf_random()
1404 {
1405 	return arc4random();
1406 }
1407 
1408 
1409 u_int
ipf_pcksum(fin,hlen,sum)1410 ipf_pcksum(fin, hlen, sum)
1411 	fr_info_t *fin;
1412 	int hlen;
1413 	u_int sum;
1414 {
1415 	struct mbuf *m;
1416 	u_int sum2;
1417 	int off;
1418 
1419 	m = fin->fin_m;
1420 	off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1421 	m->m_data += hlen;
1422 	m->m_len -= hlen;
1423 	sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1424 	m->m_len += hlen;
1425 	m->m_data -= hlen;
1426 
1427 	/*
1428 	 * Both sum and sum2 are partial sums, so combine them together.
1429 	 */
1430 	sum += ~sum2 & 0xffff;
1431 	while (sum > 0xffff)
1432 		sum = (sum & 0xffff) + (sum >> 16);
1433 	sum2 = ~sum & 0xffff;
1434 	return sum2;
1435 }
1436 
1437 #ifdef	USE_INET6
1438 u_int
ipf_pcksum6(m,ip6,off,len)1439 ipf_pcksum6(m, ip6, off, len)
1440 	struct mbuf *m;
1441 	ip6_t *ip6;
1442 	u_int32_t off;
1443 	u_int32_t len;
1444 {
1445 #ifdef	_KERNEL
1446 	int sum;
1447 
1448 	if (m->m_len < sizeof(struct ip6_hdr)) {
1449 		return 0xffff;
1450 	}
1451 
1452 	sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1453 	return(sum);
1454 #else
1455 	u_short *sp;
1456 	u_int sum;
1457 
1458 	sp = (u_short *)&ip6->ip6_src;
1459 	sum = *sp++;   /* ip6_src */
1460 	sum += *sp++;
1461 	sum += *sp++;
1462 	sum += *sp++;
1463 	sum += *sp++;
1464 	sum += *sp++;
1465 	sum += *sp++;
1466 	sum += *sp++;
1467 	sum += *sp++;   /* ip6_dst */
1468 	sum += *sp++;
1469 	sum += *sp++;
1470 	sum += *sp++;
1471 	sum += *sp++;
1472 	sum += *sp++;
1473 	sum += *sp++;
1474 	sum += *sp++;
1475 	return(ipf_pcksum(fin, off, sum));
1476 #endif
1477 }
1478 #endif
1479