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