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