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