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