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