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