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