ip_fil_netbsd.c revision 1.22.4.2 1 /* $NetBSD: ip_fil_netbsd.c,v 1.22.4.2 2017/04/29 09:17:59 pgoyette Exp $ */
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 #if defined(__NetBSD__)
10 #include <sys/cdefs.h>
11 __KERNEL_RCSID(0, "$NetBSD: ip_fil_netbsd.c,v 1.22.4.2 2017/04/29 09:17:59 pgoyette Exp $");
12 #else
13 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
14 static const char rcsid[] = "@(#)Id: ip_fil_netbsd.c,v 1.1.1.2 2012/07/22 13:45:17 darrenr Exp";
15 #endif
16 #endif
17
18 #if defined(KERNEL) || defined(_KERNEL)
19 # undef KERNEL
20 # undef _KERNEL
21 # define KERNEL 1
22 # define _KERNEL 1
23 #endif
24 #include <sys/param.h>
25 #if (NetBSD >= 199905) && !defined(IPFILTER_LKM)
26 # if (__NetBSD_Version__ >= 799007100)
27 # include <sys/localcount.h>
28 # endif
29 # if (__NetBSD_Version__ >= 799003000)
30 # ifdef _KERNEL_OPT
31 # include "opt_ipsec.h"
32 # endif
33 # else
34 # include "opt_ipsec.h"
35 # endif
36 #endif
37 #include <sys/errno.h>
38 #include <sys/types.h>
39 #include <sys/file.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
42 #include <sys/systm.h>
43 #include <sys/select.h>
44 #if (NetBSD > 199609)
45 # include <sys/dirent.h>
46 #else
47 # include <sys/dir.h>
48 #endif
49 #if (__NetBSD_Version__ >= 599005900)
50 # include <sys/cprng.h>
51 #endif
52 #include <sys/mbuf.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/poll.h>
56 #if (__NetBSD_Version__ >= 399002000)
57 # include <sys/kauth.h>
58 #endif
59 #if (__NetBSD_Version__ >= 799003000)
60 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #endif
63 #if (__NetBSD_Version__ >= 799003300)
64 #include <sys/localcount.h>
65 #endif
66
67 #include <net/if.h>
68 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/tcp.h>
75 #if __NetBSD_Version__ >= 105190000 /* 1.5T */
76 # include <netinet/tcp_timer.h>
77 # include <netinet/tcp_var.h>
78 #endif
79 #include <netinet/udp.h>
80 #include <netinet/tcpip.h>
81 #include <netinet/ip_icmp.h>
82 #include "netinet/ip_compat.h"
83 #ifdef USE_INET6
84 # include <netinet/icmp6.h>
85 # if (__NetBSD_Version__ >= 106000000)
86 # include <netinet6/nd6.h>
87 # endif
88 #endif
89 #include "netinet/ip_fil.h"
90 #include "netinet/ip_nat.h"
91 #include "netinet/ip_frag.h"
92 #include "netinet/ip_state.h"
93 #include "netinet/ip_proxy.h"
94 #include "netinet/ip_auth.h"
95 #include "netinet/ip_sync.h"
96 #include "netinet/ip_lookup.h"
97 #include "netinet/ip_dstlist.h"
98 #ifdef IPFILTER_SCAN
99 #include "netinet/ip_scan.h"
100 #endif
101 #include <sys/md5.h>
102 #include <sys/kernel.h>
103 #include <sys/conf.h>
104 #ifdef INET
105 extern int ip_optcopy (struct ip *, struct ip *);
106 #endif
107
108 #ifdef IPFILTER_M_IPFILTER
109 MALLOC_DEFINE(M_IPFILTER, "IP Filter", "IP Filter packet filter data structures");
110 #endif
111
112 #if __NetBSD_Version__ >= 105009999
113 # define csuminfo csum_flags
114 #endif
115
116 #if __NetBSD_Version__ < 200000000
117 extern struct protosw inetsw[];
118 #endif
119
120 #if (__NetBSD_Version__ >= 599002000)
121 static kauth_listener_t ipf_listener;
122 #endif
123
124 #if (__NetBSD_Version__ < 399001400)
125 extern int ip6_getpmtu (struct route_in6 *, struct route_in6 *,
126 struct ifnet *, struct in6_addr *, u_long *,
127 int *);
128 #endif
129 #if (NetBSD >= 199511)
130 static int ipfopen(dev_t dev, int flags, int devtype, PROC_T *p);
131 static int ipfclose(dev_t dev, int flags, int devtype, PROC_T *p);
132 #else
133 # if (__NetBSD_Version__ >= 399001400)
134 static int ipfopen(dev_t dev, int flags, struct lwp *);
135 static int ipfclose(dev_t dev, int flags, struct lwp *);
136 # else
137 static int ipfopen(dev_t dev, int flags);
138 static int ipfclose(dev_t dev, int flags);
139 # endif /* __NetBSD_Version__ >= 399001400 */
140 #endif
141 static int ipfread(dev_t, struct uio *, int ioflag);
142 static int ipfwrite(dev_t, struct uio *, int ioflag);
143 static int ipfpoll(dev_t, int events, PROC_T *);
144 static void ipf_timer_func(void *ptr);
145
146 const struct cdevsw ipl_cdevsw = {
147 #if (__NetBSD_Version__ >= 799007100)
148 DEVSW_MODULE_INIT
149 #endif
150 .d_open = ipfopen,
151 .d_close = ipfclose,
152 .d_read = ipfread,
153 .d_write = ipfwrite,
154 .d_ioctl = ipfioctl,
155 .d_stop = nostop,
156 .d_tty = notty,
157 .d_poll = ipfpoll,
158 .d_mmap = nommap,
159 #if (__NetBSD_Version__ >= 200000000)
160 .d_kqfilter = nokqfilter,
161 #endif
162 .d_discard = nodiscard,
163 #ifdef D_OTHER
164 .d_flag = D_OTHER
165 #else
166 .d_flag = 0
167 #endif
168 };
169 #if (__NetBSD_Version__ >= 799003000)
170 kmutex_t ipf_ref_mutex;
171 int ipf_active;
172 #endif
173
174 ipf_main_softc_t ipfmain;
175
176 static u_short ipid = 0;
177 static int (*ipf_savep)(void *, ip_t *, int, void *, int, struct mbuf **);
178 static int ipf_send_ip(fr_info_t *, mb_t *);
179 #ifdef USE_INET6
180 static int ipf_fastroute6(struct mbuf *, struct mbuf **,
181 fr_info_t *, frdest_t *);
182 #endif
183
184 #if defined(NETBSD_PF)
185 # include <net/pfil.h>
186 /*
187 * We provide the ipf_checkp name just to minimize changes later.
188 */
189 int (*ipf_checkp)(void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp);
190 #endif /* NETBSD_PF */
191
192 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
193 # include <net/pfil.h>
194
195 static int ipf_check_wrapper(void *, struct mbuf **, struct ifnet *, int );
196
197 static int
198 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
199 {
200 struct ip *ip;
201 int rv, hlen;
202
203 #if __NetBSD_Version__ >= 200080000
204 /*
205 * ensure that mbufs are writable beforehand
206 * as it's assumed by ipf code.
207 * XXX inefficient
208 */
209 int error = m_makewritable(mp, 0, M_COPYALL, M_DONTWAIT);
210
211 if (error) {
212 m_freem(*mp);
213 *mp = NULL;
214 return error;
215 }
216 #endif
217 ip = mtod(*mp, struct ip *);
218 hlen = ip->ip_hl << 2;
219
220 #ifdef INET
221 #if defined(M_CSUM_TCPv4)
222 /*
223 * If the packet is out-bound, we can't delay checksums
224 * here. For in-bound, the checksum has already been
225 * validated.
226 */
227 if (dir == PFIL_OUT) {
228 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
229 in_delayed_cksum(*mp);
230 (*mp)->m_pkthdr.csum_flags &=
231 ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
232 }
233 }
234 #endif /* M_CSUM_TCPv4 */
235 #endif /* INET */
236
237 /*
238 * Note, we don't need to update the checksum, because
239 * it has already been verified.
240 */
241 rv = ipf_check(&ipfmain, ip, hlen, ifp, (dir == PFIL_OUT), mp);
242
243 return (rv);
244 }
245
246 # ifdef USE_INET6
247 # include <netinet/ip6.h>
248
249 static int ipf_check_wrapper6(void *, struct mbuf **, struct ifnet *, int );
250
251 static int
252 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
253 {
254 #if defined(INET6)
255 # if defined(M_CSUM_TCPv6) && (__NetBSD_Version__ > 200000000)
256 /*
257 * If the packet is out-bound, we can't delay checksums
258 * here. For in-bound, the checksum has already been
259 * validated.
260 */
261 if (dir == PFIL_OUT) {
262 if ((*mp)->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) {
263 # if (__NetBSD_Version__ > 399000600)
264 in6_delayed_cksum(*mp);
265 # endif
266 (*mp)->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv6|
267 M_CSUM_UDPv6);
268 }
269 }
270 # endif
271 #endif /* INET6 */
272
273 return (ipf_check(&ipfmain, mtod(*mp, struct ip *), sizeof(struct ip6_hdr),
274 ifp, (dir == PFIL_OUT), mp));
275 }
276 # endif
277
278
279 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
280
281 # if (__NetBSD_Version__ >= 799000400)
282
283 static void ipf_pfilsync(void *, unsigned long, void *);
284
285 static void
286 ipf_pfilsync(void *hdr, unsigned long cmd, void *arg2)
287 {
288 /*
289 * The interface pointer is useless for create (we have nothing to
290 * compare it to) and at detach, the interface name is still in the
291 * list of active NICs (albeit, down, but that's not any real
292 * indicator) and doing ifunit() on the name will still return the
293 * pointer, so it's not much use then, either.
294 */
295 ipf_sync(&ipfmain, NULL);
296 }
297
298 # else
299
300 static int ipf_pfilsync(void *, struct mbuf **, struct ifnet *, int);
301
302 static int
303 ipf_pfilsync(void *hdr, struct mbuf **mp, struct ifnet *ifp, int dir)
304 {
305 ipf_sync(&ipfmain, NULL);
306 return 0;
307 }
308
309 # endif
310 # endif
311
312 #endif /* __NetBSD_Version__ >= 105110000 */
313
314
315 #if defined(IPFILTER_LKM)
316 int
317 ipf_identify(s)
318 char *s;
319 {
320 if (strcmp(s, "ipl") == 0)
321 return 1;
322 return 0;
323 }
324 #endif /* IPFILTER_LKM */
325
326 #if (__NetBSD_Version__ >= 599002000)
327 static int
328 ipf_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
329 void *arg0, void *arg1, void *arg2, void *arg3)
330 {
331 int result;
332 enum kauth_network_req req;
333
334 result = KAUTH_RESULT_DEFER;
335 req = (enum kauth_network_req)arg0;
336
337 if (action != KAUTH_NETWORK_FIREWALL)
338 return result;
339
340 /* These must have came from device context. */
341 if ((req == KAUTH_REQ_NETWORK_FIREWALL_FW) ||
342 (req == KAUTH_REQ_NETWORK_FIREWALL_NAT))
343 result = KAUTH_RESULT_ALLOW;
344
345 return result;
346 }
347 #endif
348
349 /*
350 * Try to detect the case when compiling for NetBSD with pseudo-device
351 */
352 void
353 ipfilterattach(int count)
354 {
355
356 #if (__NetBSD_Version__ >= 799003000)
357 return;
358 #else
359 #if (__NetBSD_Version__ >= 599002000)
360 ipf_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
361 ipf_listener_cb, NULL);
362 #endif
363
364 if (ipf_load_all() == 0)
365 (void) ipf_create_all(&ipfmain);
366 #endif
367 }
368
369
370 int
371 ipfattach(ipf_main_softc_t *softc)
372 {
373 SPL_INT(s);
374 #if (__NetBSD_Version__ >= 499005500)
375 int i;
376 #endif
377 #if defined(NETBSD_PF) && (__NetBSD_Version__ >= 104200000)
378 int error = 0;
379 # if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 105110000)
380 pfil_head_t *ph_inet;
381 # ifdef USE_INET6
382 pfil_head_t *ph_inet6;
383 # endif
384 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
385 pfil_head_t *ph_ifsync;
386 # endif
387 # endif
388 #endif
389
390 SPL_NET(s);
391 if ((softc->ipf_running > 0) || (ipf_checkp == ipf_check)) {
392 printf("IP Filter: already initialized\n");
393 SPL_X(s);
394 IPFERROR(130017);
395 return EBUSY;
396 }
397
398 if (ipf_init_all(softc) < 0) {
399 SPL_X(s);
400 IPFERROR(130015);
401 return EIO;
402 }
403
404 #ifdef NETBSD_PF
405 # if (__NetBSD_Version__ >= 104200000)
406 # if __NetBSD_Version__ >= 105110000
407 ph_inet = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET);
408 # ifdef USE_INET6
409 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET6);
410 # endif
411 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
412 ph_ifsync = pfil_head_get(PFIL_TYPE_IFNET, 0);
413 # endif
414
415 if (ph_inet == NULL
416 # ifdef USE_INET6
417 && ph_inet6 == NULL
418 # endif
419 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
420 && ph_ifsync == NULL
421 # endif
422 ) {
423 SPL_X(s);
424 IPFERROR(130016);
425 return ENODEV;
426 }
427
428 if (ph_inet != NULL)
429 error = pfil_add_hook((void *)ipf_check_wrapper, NULL,
430 PFIL_IN|PFIL_OUT, ph_inet);
431 else
432 error = 0;
433 # else
434 error = pfil_add_hook((void *)ipf_check, PFIL_IN|PFIL_OUT,
435 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
436 # endif
437 if (error) {
438 IPFERROR(130013);
439 goto pfil_error;
440 }
441 # else
442 pfil_add_hook((void *)ipf_check, PFIL_IN|PFIL_OUT);
443 # endif
444
445 # ifdef USE_INET6
446 # if __NetBSD_Version__ >= 105110000
447 if (ph_inet6 != NULL)
448 error = pfil_add_hook((void *)ipf_check_wrapper6, NULL,
449 PFIL_IN|PFIL_OUT, ph_inet6);
450 else
451 error = 0;
452 if (error) {
453 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
454 PFIL_IN|PFIL_OUT, ph_inet6);
455 ipfmain.ipf_interror = 130014;
456 goto pfil_error;
457 }
458 # else
459 error = pfil_add_hook((void *)ipf_check, PFIL_IN|PFIL_OUT,
460 &inetsw[ip_protox[IPPROTO_IPV6]].pr_pfh);
461 if (error) {
462 pfil_remove_hook((void *)ipf_check, PFIL_IN|PFIL_OUT,
463 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
464 IPFERROR(130014);
465 goto pfil_error;
466 }
467 # endif
468 # endif
469
470 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
471 if (ph_ifsync != NULL)
472 #if (__NetBSD_Version__ >= 799000400)
473 (void) pfil_add_ihook((void *)ipf_pfilsync, NULL,
474 PFIL_IFNET, ph_ifsync);
475 #else
476 (void) pfil_add_hook((void *)ipf_pfilsync, NULL,
477 PFIL_IFNET, ph_ifsync);
478 #endif
479 # endif
480 #endif
481
482 #if (__NetBSD_Version__ >= 499005500)
483 for (i = 0; i < IPL_LOGSIZE; i++)
484 selinit(&ipfmain.ipf_selwait[i]);
485 #else
486 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
487 #endif
488 ipf_savep = ipf_checkp;
489 ipf_checkp = ipf_check;
490
491 #ifdef INET
492 if (softc->ipf_control_forwarding & 1)
493 ipforwarding = 1;
494 #endif
495
496 ipid = 0;
497
498 SPL_X(s);
499
500 #if (__NetBSD_Version__ >= 104010000)
501 # if (__NetBSD_Version__ >= 499002000)
502 callout_init(&softc->ipf_slow_ch, 0);
503 # else
504 callout_init(&softc->ipf_slow_ch);
505 # endif
506 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
507 ipf_timer_func, softc);
508 #else
509 timeout(ipf_timer_func, softc, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
510 #endif
511
512 return 0;
513
514 #if __NetBSD_Version__ >= 105110000
515 pfil_error:
516 SPL_X(s);
517 ipf_fini_all(softc);
518 return error;
519 #endif
520 }
521
522 static void
523 ipf_timer_func(void *ptr)
524 {
525 ipf_main_softc_t *softc = ptr;
526 SPL_INT(s);
527
528 SPL_NET(s);
529 READ_ENTER(&softc->ipf_global);
530
531 if (softc->ipf_running > 0)
532 ipf_slowtimer(softc);
533
534 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
535 #if NETBSD_GE_REV(104240000)
536 callout_reset(&softc->ipf_slow_ch, hz / 2,
537 ipf_timer_func, softc);
538 #else
539 timeout(ipf_timer_func, softc,
540 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
541 #endif
542 }
543 RWLOCK_EXIT(&softc->ipf_global);
544 SPL_X(s);
545 }
546
547
548 /*
549 * Disable the filter by removing the hooks from the IP input/output
550 * stream.
551 */
552 int
553 ipfdetach(ipf_main_softc_t *softc)
554 {
555 SPL_INT(s);
556 #if (__NetBSD_Version__ >= 499005500)
557 int i;
558 #endif
559 #if defined(NETBSD_PF) && (__NetBSD_Version__ >= 104200000)
560 int error = 0;
561 # if __NetBSD_Version__ >= 105150000
562 pfil_head_t *ph_inet = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET);
563 # ifdef USE_INET6
564 pfil_head_t *ph_inet6 = pfil_head_get(PFIL_TYPE_AF, (void *)AF_INET6);
565 # endif
566 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
567 struct pfil_head *ph_ifsync = pfil_head_get(PFIL_TYPE_IFNET, 0);
568 # endif
569 # endif
570 #endif
571
572 SPL_NET(s);
573
574 #if (__NetBSD_Version__ >= 104010000)
575 if (softc->ipf_running > 0)
576 callout_stop(&softc->ipf_slow_ch);
577 #else
578 untimeout(ipf_slowtimer, NULL);
579 #endif /* NetBSD */
580
581 ipf_checkp = ipf_savep;
582 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
583 (void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
584
585 #ifdef INET
586 if (softc->ipf_control_forwarding & 2)
587 ipforwarding = 0;
588 #endif
589
590 #ifdef NETBSD_PF
591 # if (__NetBSD_Version__ >= 104200000)
592 # if __NetBSD_Version__ >= 105110000
593 # if defined(PFIL_TYPE_IFNET) && defined(PFIL_IFNET)
594 (void) pfil_remove_hook((void *)ipf_pfilsync, NULL,
595 PFIL_IFNET, ph_ifsync);
596 # endif
597
598 if (ph_inet != NULL)
599 error = pfil_remove_hook((void *)ipf_check_wrapper, NULL,
600 PFIL_IN|PFIL_OUT, ph_inet);
601 else
602 error = 0;
603 # else
604 error = pfil_remove_hook((void *)ipf_check, PFIL_IN|PFIL_OUT,
605 &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
606 # endif
607 if (error) {
608 SPL_X(s);
609 IPFERROR(130011);
610 return error;
611 }
612 # else
613 pfil_remove_hook((void *)ipf_check, PFIL_IN|PFIL_OUT);
614 # endif
615 # ifdef USE_INET6
616 # if __NetBSD_Version__ >= 105110000
617 if (ph_inet6 != NULL)
618 error = pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
619 PFIL_IN|PFIL_OUT, ph_inet6);
620 else
621 error = 0;
622 # else
623 error = pfil_remove_hook((void *)ipf_check, PFIL_IN|PFIL_OUT,
624 &inetsw[ip_protox[IPPROTO_IPV6]].pr_pfh);
625 # endif
626 if (error) {
627 SPL_X(s);
628 IPFERROR(130012);
629 return error;
630 }
631 # endif
632 #endif
633 SPL_X(s);
634
635 #if (__NetBSD_Version__ >= 499005500)
636 for (i = 0; i < IPL_LOGSIZE; i++)
637 seldestroy(&ipfmain.ipf_selwait[i]);
638 #endif
639
640 ipf_fini_all(softc);
641
642 return 0;
643 }
644
645
646 /*
647 * Filter ioctl interface.
648 */
649 int
650 ipfioctl(dev_t dev, u_long cmd,
651 #if (__NetBSD_Version__ >= 499001000)
652 void *data,
653 #else
654 caddr_t data,
655 #endif
656 int mode
657 #if (NetBSD >= 199511)
658 # if (__NetBSD_Version__ >= 399001400)
659 , struct lwp *p
660 # if (__NetBSD_Version__ >= 399002000)
661 # define UID(l) kauth_cred_getuid((l)->l_cred)
662 # else
663 # define UID(l) ((l)->l_proc->p_cred->p_ruid)
664 # endif
665 # else
666 , struct proc *p
667 # define UID(p) ((p)->p_cred->p_ruid)
668 # endif
669 #endif
670 )
671 {
672 int error = 0, unit = 0;
673 SPL_INT(s);
674
675 #if (__NetBSD_Version__ >= 399002000)
676 if ((mode & FWRITE) &&
677 kauth_authorize_network(p->l_cred, KAUTH_NETWORK_FIREWALL,
678 KAUTH_REQ_NETWORK_FIREWALL_FW, NULL,
679 NULL, NULL)) {
680 ipfmain.ipf_interror = 130005;
681 return EPERM;
682 }
683 #else
684 if ((securelevel >= 2) && (mode & FWRITE)) {
685 ipfmain.ipf_interror = 130001;
686 return EPERM;
687 }
688 #endif
689
690 unit = GET_MINOR(dev);
691 if ((IPL_LOGMAX < unit) || (unit < 0)) {
692 ipfmain.ipf_interror = 130002;
693 return ENXIO;
694 }
695
696 if (ipfmain.ipf_running <= 0) {
697 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
698 ipfmain.ipf_interror = 130003;
699 return EIO;
700 }
701 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
702 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
703 cmd != SIOCGETFS && cmd != SIOCGETFF &&
704 cmd != SIOCIPFINTERROR) {
705 ipfmain.ipf_interror = 130004;
706 return EIO;
707 }
708 }
709
710 SPL_NET(s);
711
712 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, UID(p), p);
713 if (error != -1) {
714 SPL_X(s);
715 return error;
716 }
717
718 SPL_X(s);
719 return error;
720 }
721
722
723 /*
724 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
725 * requires a large amount of setting up and isn't any more efficient.
726 */
727 int
728 ipf_send_reset(fr_info_t *fin)
729 {
730 struct tcphdr *tcp, *tcp2;
731 int tlen = 0, hlen;
732 struct mbuf *m;
733 #ifdef USE_INET6
734 ip6_t *ip6;
735 #endif
736 ip_t *ip;
737
738 tcp = fin->fin_dp;
739 if (tcp->th_flags & TH_RST)
740 return -1; /* feedback loop */
741
742 if (ipf_checkl4sum(fin) == -1)
743 return -1;
744
745 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
746 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
747 ((tcp->th_flags & TH_FIN) ? 1 : 0);
748
749 #ifdef USE_INET6
750 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
751 #else
752 hlen = sizeof(ip_t);
753 #endif
754 #ifdef MGETHDR
755 MGETHDR(m, M_DONTWAIT, MT_HEADER);
756 #else
757 MGET(m, M_DONTWAIT, MT_HEADER);
758 #endif
759 if (m == NULL)
760 return -1;
761 if (sizeof(*tcp2) + hlen > MHLEN) {
762 MCLGET(m, M_DONTWAIT);
763 if (m == NULL)
764 return -1;
765 if ((m->m_flags & M_EXT) == 0) {
766 FREE_MB_T(m);
767 return -1;
768 }
769 }
770
771 m->m_len = sizeof(*tcp2) + hlen;
772 m->m_data += max_linkhdr;
773 m->m_pkthdr.len = m->m_len;
774 m_reset_rcvif(m);
775 ip = mtod(m, struct ip *);
776 bzero((char *)ip, hlen);
777 #ifdef USE_INET6
778 ip6 = (ip6_t *)ip;
779 #endif
780 bzero((char *)ip, sizeof(*tcp2) + hlen);
781 tcp2 = (struct tcphdr *)((char *)ip + hlen);
782 tcp2->th_sport = tcp->th_dport;
783 tcp2->th_dport = tcp->th_sport;
784
785 if (tcp->th_flags & TH_ACK) {
786 tcp2->th_seq = tcp->th_ack;
787 tcp2->th_flags = TH_RST;
788 tcp2->th_ack = 0;
789 } else {
790 tcp2->th_seq = 0;
791 tcp2->th_ack = ntohl(tcp->th_seq);
792 tcp2->th_ack += tlen;
793 tcp2->th_ack = htonl(tcp2->th_ack);
794 tcp2->th_flags = TH_RST|TH_ACK;
795 }
796 tcp2->th_x2 = 0;
797 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
798 tcp2->th_win = tcp->th_win;
799 tcp2->th_sum = 0;
800 tcp2->th_urp = 0;
801
802 #ifdef USE_INET6
803 if (fin->fin_v == 6) {
804 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
805 ip6->ip6_plen = htons(sizeof(struct tcphdr));
806 ip6->ip6_nxt = IPPROTO_TCP;
807 ip6->ip6_hlim = 0;
808 ip6->ip6_src = fin->fin_dst6.in6;
809 ip6->ip6_dst = fin->fin_src6.in6;
810 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
811 sizeof(*ip6), sizeof(*tcp2));
812 return ipf_send_ip(fin, m);
813 }
814 #endif
815 #ifdef INET
816 ip->ip_p = IPPROTO_TCP;
817 ip->ip_len = htons(sizeof(struct tcphdr));
818 ip->ip_src.s_addr = fin->fin_daddr;
819 ip->ip_dst.s_addr = fin->fin_saddr;
820 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
821 ip->ip_len = hlen + sizeof(*tcp2);
822 return ipf_send_ip(fin, m);
823 #else
824 return 0;
825 #endif
826 }
827
828
829 /*
830 * Expects ip_len to be in host byte order when called.
831 */
832 static int
833 ipf_send_ip(fr_info_t *fin, mb_t *m)
834 {
835 fr_info_t fnew;
836 #ifdef INET
837 ip_t *oip;
838 #endif
839 ip_t *ip;
840 int hlen;
841
842 ip = mtod(m, ip_t *);
843 bzero((char *)&fnew, sizeof(fnew));
844 fnew.fin_main_soft = fin->fin_main_soft;
845
846 IP_V_A(ip, fin->fin_v);
847 switch (fin->fin_v)
848 {
849 #ifdef INET
850 case 4 :
851 oip = fin->fin_ip;
852 hlen = sizeof(*oip);
853 fnew.fin_v = 4;
854 fnew.fin_p = ip->ip_p;
855 fnew.fin_plen = ntohs(ip->ip_len);
856 HTONS(ip->ip_len);
857 IP_HL_A(ip, sizeof(*oip) >> 2);
858 ip->ip_tos = oip->ip_tos;
859 ip->ip_id = ipf_nextipid(fin);
860 ip->ip_off = htons(ip_mtudisc ? IP_DF : 0);
861 ip->ip_ttl = ip_defttl;
862 ip->ip_sum = 0;
863 break;
864 #endif
865 #ifdef USE_INET6
866 case 6 :
867 {
868 ip6_t *ip6 = (ip6_t *)ip;
869
870 ip6->ip6_vfc = 0x60;
871 ip6->ip6_hlim = IPDEFTTL;
872
873 hlen = sizeof(*ip6);
874 fnew.fin_p = ip6->ip6_nxt;
875 fnew.fin_v = 6;
876 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
877 break;
878 }
879 #endif
880 default :
881 return EINVAL;
882 }
883 #ifdef KAME_IPSEC
884 m_reset_rcvif(m);
885 #endif
886
887 fnew.fin_ifp = fin->fin_ifp;
888 fnew.fin_flx = FI_NOCKSUM;
889 fnew.fin_m = m;
890 fnew.fin_ip = ip;
891 fnew.fin_mp = &m;
892 fnew.fin_hlen = hlen;
893 fnew.fin_dp = (char *)ip + hlen;
894 (void) ipf_makefrip(hlen, ip, &fnew);
895
896 return ipf_fastroute(m, &m, &fnew, NULL);
897 }
898
899
900 int
901 ipf_send_icmp_err(int type, fr_info_t *fin, int dst)
902 {
903 int err, hlen, xtra, iclen, ohlen, avail;
904 struct in_addr dst4;
905 struct icmp *icmp;
906 struct mbuf *m;
907 i6addr_t dst6;
908 void *ifp;
909 #ifdef USE_INET6
910 int code;
911 ip6_t *ip6;
912 #endif
913 ip_t *ip, *ip2;
914
915 if ((type < 0) || (type > ICMP_MAXTYPE))
916 return -1;
917
918 #ifdef USE_INET6
919 code = fin->fin_icode;
920 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
921 return -1;
922 #endif
923
924 if (ipf_checkl4sum(fin) == -1)
925 return -1;
926 #ifdef MGETHDR
927 MGETHDR(m, M_DONTWAIT, MT_HEADER);
928 #else
929 MGET(m, M_DONTWAIT, MT_HEADER);
930 #endif
931 if (m == NULL)
932 return -1;
933 avail = MHLEN;
934
935 xtra = 0;
936 hlen = 0;
937 ohlen = 0;
938 dst4.s_addr = 0;
939 ifp = fin->fin_ifp;
940 if (fin->fin_v == 4) {
941 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
942 switch (ntohs(fin->fin_data[0]) >> 8)
943 {
944 case ICMP_ECHO :
945 case ICMP_TSTAMP :
946 case ICMP_IREQ :
947 case ICMP_MASKREQ :
948 break;
949 default :
950 FREE_MB_T(m);
951 return 0;
952 }
953
954 if (dst == 0) {
955 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
956 &dst6, NULL) == -1) {
957 FREE_MB_T(m);
958 return -1;
959 }
960 dst4 = dst6.in4;
961 } else
962 dst4.s_addr = fin->fin_daddr;
963
964 hlen = sizeof(ip_t);
965 ohlen = fin->fin_hlen;
966 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
967 if (fin->fin_hlen < fin->fin_plen)
968 xtra = MIN(fin->fin_dlen, 8);
969 else
970 xtra = 0;
971 }
972
973 #ifdef USE_INET6
974 else if (fin->fin_v == 6) {
975 hlen = sizeof(ip6_t);
976 ohlen = sizeof(ip6_t);
977 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
978 type = icmptoicmp6types[type];
979 if (type == ICMP6_DST_UNREACH)
980 code = icmptoicmp6unreach[code];
981
982 if (iclen + max_linkhdr + fin->fin_plen > avail) {
983 MCLGET(m, M_DONTWAIT);
984 if (m == NULL)
985 return -1;
986 if ((m->m_flags & M_EXT) == 0) {
987 FREE_MB_T(m);
988 return -1;
989 }
990 avail = MCLBYTES;
991 }
992 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
993 xtra = MIN(xtra, IPV6_MMTU - iclen);
994 if (dst == 0) {
995 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
996 &dst6, NULL) == -1) {
997 FREE_MB_T(m);
998 return -1;
999 }
1000 } else
1001 dst6 = fin->fin_dst6;
1002 }
1003 #endif
1004 else {
1005 FREE_MB_T(m);
1006 return -1;
1007 }
1008
1009 avail -= (max_linkhdr + iclen);
1010 if (avail < 0) {
1011 FREE_MB_T(m);
1012 return -1;
1013 }
1014 if (xtra > avail)
1015 xtra = avail;
1016 iclen += xtra;
1017 m->m_data += max_linkhdr;
1018 m_reset_rcvif(m);
1019 m->m_pkthdr.len = iclen;
1020 m->m_len = iclen;
1021 ip = mtod(m, ip_t *);
1022 icmp = (struct icmp *)((char *)ip + hlen);
1023 ip2 = (ip_t *)&icmp->icmp_ip;
1024
1025 icmp->icmp_type = type;
1026 icmp->icmp_code = fin->fin_icode;
1027 icmp->icmp_cksum = 0;
1028 #ifdef icmp_nextmtu
1029 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
1030 if (fin->fin_mtu != 0) {
1031 icmp->icmp_nextmtu = htons(fin->fin_mtu);
1032
1033 } else if (ifp != NULL) {
1034 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
1035
1036 } else { /* make up a number... */
1037 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
1038 }
1039 }
1040 #endif
1041
1042 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
1043
1044 #if defined(M_CSUM_IPv4)
1045 /*
1046 * Clear any in-bound checksum flags for this packet.
1047 */
1048 m->m_pkthdr.csuminfo = 0;
1049 #endif /* __NetBSD__ && M_CSUM_IPv4 */
1050
1051 #ifdef USE_INET6
1052 ip6 = (ip6_t *)ip;
1053 if (fin->fin_v == 6) {
1054 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
1055 ip6->ip6_plen = htons(iclen - hlen);
1056 ip6->ip6_nxt = IPPROTO_ICMPV6;
1057 ip6->ip6_hlim = 0;
1058 ip6->ip6_src = dst6.in6;
1059 ip6->ip6_dst = fin->fin_src6.in6;
1060 if (xtra > 0)
1061 bcopy((char *)fin->fin_ip + ohlen,
1062 (char *)&icmp->icmp_ip + ohlen, xtra);
1063 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1064 sizeof(*ip6), iclen - hlen);
1065 } else
1066 #endif
1067 {
1068 ip->ip_p = IPPROTO_ICMP;
1069 ip->ip_src.s_addr = dst4.s_addr;
1070 ip->ip_dst.s_addr = fin->fin_saddr;
1071
1072 if (xtra > 0)
1073 bcopy((char *)fin->fin_ip + ohlen,
1074 (char *)&icmp->icmp_ip + ohlen, xtra);
1075 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
1076 sizeof(*icmp) + 8);
1077 ip->ip_len = iclen;
1078 ip->ip_p = IPPROTO_ICMP;
1079 }
1080 err = ipf_send_ip(fin, m);
1081 return err;
1082 }
1083
1084
1085 /*
1086 * m0 - pointer to mbuf where the IP packet starts
1087 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
1088 */
1089 int
1090 ipf_fastroute(mb_t *m0, mb_t **mpp, fr_info_t *fin, frdest_t *fdp)
1091 {
1092 register struct ip *ip, *mhip;
1093 register struct mbuf *m = *mpp;
1094 register struct route *ro;
1095 int len, off, error = 0, hlen, code;
1096 struct ifnet *ifp, *sifp;
1097 ipf_main_softc_t *softc;
1098 #if __NetBSD_Version__ >= 499001100
1099 union {
1100 struct sockaddr dst;
1101 struct sockaddr_in dst4;
1102 } u;
1103 #else
1104 struct sockaddr_in *dst4;
1105 #endif
1106 struct sockaddr *dst;
1107 u_short ip_off, ip_len;
1108 struct route iproute;
1109 struct rtentry *rt;
1110 frdest_t node;
1111 frentry_t *fr;
1112
1113 if (fin->fin_v == 6) {
1114 #ifdef USE_INET6
1115 error = ipf_fastroute6(m0, mpp, fin, fdp);
1116 #else
1117 error = EPROTONOSUPPORT;
1118 #endif
1119 if ((error != 0) && (*mpp != NULL))
1120 FREE_MB_T(*mpp);
1121 return error;
1122 }
1123 #ifndef INET
1124 FREE_MB_T(*mpp);
1125 return EPROTONOSUPPORT;
1126 #else
1127
1128 hlen = fin->fin_hlen;
1129 ip = mtod(m0, struct ip *);
1130 softc = fin->fin_main_soft;
1131 rt = NULL;
1132 ifp = NULL;
1133
1134 # if defined(M_CSUM_IPv4)
1135 /*
1136 * Clear any in-bound checksum flags for this packet.
1137 */
1138 m0->m_pkthdr.csuminfo = 0;
1139 # endif /* __NetBSD__ && M_CSUM_IPv4 */
1140
1141 /*
1142 * Route packet.
1143 */
1144 ro = &iproute;
1145 memset(ro, 0, sizeof(*ro));
1146 fr = fin->fin_fr;
1147
1148 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
1149 (fdp->fd_type == FRD_DSTLIST)) {
1150 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
1151 fdp = &node;
1152 }
1153 if (fdp != NULL)
1154 ifp = fdp->fd_ptr;
1155 else
1156 ifp = fin->fin_ifp;
1157
1158 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
1159 error = -2;
1160 goto bad;
1161 }
1162
1163 # if __NetBSD_Version__ >= 499001100
1164 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
1165 sockaddr_in_init(&u.dst4, &fdp->fd_ip, 0);
1166 else
1167 sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
1168 dst = &u.dst;
1169 rtcache_setdst(ro, dst);
1170 rt = rtcache_init(ro);
1171 # else
1172 dst4 = (struct sockaddr_in *)&ro->ro_dst;
1173 dst = (struct sockaddr *)dst4;
1174 dst4->sin_family = AF_INET;
1175 dst4->sin_addr = ip->ip_dst;
1176
1177 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
1178 dst4->sin_addr = fdp->fd_ip;
1179
1180 dst4->sin_len = sizeof(*dst);
1181 rtalloc(ro);
1182 rt = ro->ro_rt;
1183 # endif
1184 if ((ifp == NULL) && (rt != NULL))
1185 ifp = rt->rt_ifp;
1186 if ((rt == NULL) || (ifp == NULL)) {
1187 #ifdef INET
1188 if (in_localaddr(ip->ip_dst))
1189 error = EHOSTUNREACH;
1190 else
1191 #endif
1192 error = ENETUNREACH;
1193 goto bad;
1194 }
1195
1196
1197 if (rt->rt_flags & RTF_GATEWAY)
1198 dst = rt->rt_gateway;
1199
1200 rt->rt_use++;
1201
1202 /*
1203 * For input packets which are being "fastrouted", they won't
1204 * go back through output filtering and miss their chance to get
1205 * NAT'd and counted. Duplicated packets aren't considered to be
1206 * part of the normal packet stream, so do not NAT them or pass
1207 * them through stateful checking, etc.
1208 */
1209 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
1210 sifp = fin->fin_ifp;
1211 fin->fin_ifp = ifp;
1212 fin->fin_out = 1;
1213 (void) ipf_acctpkt(fin, NULL);
1214 fin->fin_fr = NULL;
1215 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
1216 u_32_t pass;
1217
1218 (void) ipf_state_check(fin, &pass);
1219 }
1220
1221 switch (ipf_nat_checkout(fin, NULL))
1222 {
1223 case 0 :
1224 break;
1225 case 1 :
1226 ip->ip_sum = 0;
1227 break;
1228 case -1 :
1229 error = -1;
1230 goto bad;
1231 break;
1232 }
1233
1234 fin->fin_ifp = sifp;
1235 fin->fin_out = 0;
1236 } else
1237 ip->ip_sum = 0;
1238 /*
1239 * If small enough for interface, can just send directly.
1240 */
1241 m_set_rcvif(m, ifp);
1242
1243 ip_len = ntohs(ip->ip_len);
1244 if (ip_len <= ifp->if_mtu) {
1245 # if defined(M_CSUM_IPv4)
1246 # if (__NetBSD_Version__ >= 105009999)
1247 if (ifp->if_csum_flags_tx & M_CSUM_IPv4)
1248 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1249 # else
1250 if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
1251 m->m_pkthdr.csuminfo |= M_CSUM_IPv4;
1252 # endif /* (__NetBSD_Version__ >= 105009999) */
1253 else if (ip->ip_sum == 0)
1254 ip->ip_sum = in_cksum(m, hlen);
1255 # else
1256 if (!ip->ip_sum)
1257 ip->ip_sum = in_cksum(m, hlen);
1258 # endif /* M_CSUM_IPv4 */
1259
1260 error = if_output_lock(ifp, ifp, m, dst, rt);
1261 goto done;
1262 }
1263
1264 /*
1265 * Too large for interface; fragment if possible.
1266 * Must be able to put at least 8 bytes per fragment.
1267 */
1268 ip_off = ntohs(ip->ip_off);
1269 if (ip_off & IP_DF) {
1270 error = EMSGSIZE;
1271 goto bad;
1272 }
1273 len = (ifp->if_mtu - hlen) &~ 7;
1274 if (len < 8) {
1275 error = EMSGSIZE;
1276 goto bad;
1277 }
1278
1279 {
1280 int mhlen, firstlen = len;
1281 struct mbuf **mnext = &m->m_act;
1282
1283 /*
1284 * Loop through length of segment after first fragment,
1285 * make new header and copy data of each part and link onto chain.
1286 */
1287 m0 = m;
1288 mhlen = sizeof (struct ip);
1289 for (off = hlen + len; off < ip_len; off += len) {
1290 # ifdef MGETHDR
1291 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1292 # else
1293 MGET(m, M_DONTWAIT, MT_HEADER);
1294 # endif
1295 if (m == 0) {
1296 m = m0;
1297 error = ENOBUFS;
1298 goto bad;
1299 }
1300 m->m_data += max_linkhdr;
1301 mhip = mtod(m, struct ip *);
1302 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
1303 #ifdef INET
1304 if (hlen > sizeof (struct ip)) {
1305 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1306 IP_HL_A(mhip, mhlen >> 2);
1307 }
1308 #endif
1309 m->m_len = mhlen;
1310 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
1311 if (off + len >= ip_len)
1312 len = ip_len - off;
1313 else
1314 mhip->ip_off |= IP_MF;
1315 mhip->ip_len = htons((u_short)(len + mhlen));
1316 m->m_next = m_copy(m0, off, len);
1317 if (m->m_next == 0) {
1318 error = ENOBUFS; /* ??? */
1319 goto sendorfree;
1320 }
1321 m->m_pkthdr.len = mhlen + len;
1322 m_reset_rcvif(m);
1323 mhip->ip_off = htons((u_short)mhip->ip_off);
1324 mhip->ip_sum = 0;
1325 #ifdef INET
1326 mhip->ip_sum = in_cksum(m, mhlen);
1327 #endif
1328 *mnext = m;
1329 mnext = &m->m_act;
1330 }
1331 /*
1332 * Update first fragment by trimming what's been copied out
1333 * and updating header, then send each fragment (in order).
1334 */
1335 m_adj(m0, hlen + firstlen - ip_len);
1336 ip->ip_len = htons((u_short)(hlen + firstlen));
1337 ip->ip_off = htons((u_short)IP_MF);
1338 ip->ip_sum = 0;
1339 #ifdef INET
1340 ip->ip_sum = in_cksum(m0, hlen);
1341 #endif
1342 sendorfree:
1343 for (m = m0; m; m = m0) {
1344 m0 = m->m_act;
1345 m->m_act = 0;
1346 if (error == 0) {
1347 KERNEL_LOCK(1, NULL);
1348 error = (*ifp->if_output)(ifp, m, dst, rt);
1349 KERNEL_UNLOCK_ONE(NULL);
1350 } else {
1351 FREE_MB_T(m);
1352 }
1353 }
1354 }
1355 done:
1356 if (!error)
1357 softc->ipf_frouteok[0]++;
1358 else
1359 softc->ipf_frouteok[1]++;
1360
1361 # if __NetBSD_Version__ >= 499001100
1362 rtcache_unref(rt, ro);
1363 rtcache_free(ro);
1364 # else
1365 if (rt) {
1366 RTFREE(rt);
1367 }
1368 # endif
1369 return error;
1370 bad:
1371 if (error == EMSGSIZE) {
1372 sifp = fin->fin_ifp;
1373 code = fin->fin_icode;
1374 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
1375 fin->fin_ifp = ifp;
1376 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
1377 fin->fin_ifp = sifp;
1378 fin->fin_icode = code;
1379 }
1380 FREE_MB_T(m);
1381 goto done;
1382 #endif /* INET */
1383 }
1384
1385
1386 #if defined(USE_INET6)
1387 /*
1388 * This is the IPv6 specific fastroute code. It doesn't clean up the mbuf's
1389 * or ensure that it is an IPv6 packet that is being forwarded, those are
1390 * expected to be done by the called (ipf_fastroute).
1391 */
1392 static int
1393 ipf_fastroute6(struct mbuf *m0, struct mbuf **mpp, fr_info_t *fin,
1394 frdest_t *fdp)
1395 {
1396 # if __NetBSD_Version__ >= 499001100
1397 struct route ip6route;
1398 const struct sockaddr *dst;
1399 union {
1400 struct sockaddr dst;
1401 struct sockaddr_in6 dst6;
1402 } u;
1403 struct route *ro;
1404 # else
1405 struct route_in6 ip6route;
1406 struct sockaddr_in6 *dst6;
1407 struct route_in6 *ro;
1408 # endif
1409 struct rtentry *rt;
1410 struct ifnet *ifp;
1411 u_long mtu;
1412 int error;
1413
1414 error = 0;
1415 ro = &ip6route;
1416
1417 if (fdp != NULL)
1418 ifp = fdp->fd_ptr;
1419 else
1420 ifp = fin->fin_ifp;
1421 memset(ro, 0, sizeof(*ro));
1422 # if __NetBSD_Version__ >= 499001100
1423 if (fdp != NULL && IP6_NOTZERO(&fdp->fd_ip6))
1424 sockaddr_in6_init(&u.dst6, &fdp->fd_ip6.in6, 0, 0, 0);
1425 else
1426 sockaddr_in6_init(&u.dst6, &fin->fin_fi.fi_dst.in6, 0, 0, 0);
1427 dst = &u.dst;
1428 rtcache_setdst(ro, dst);
1429
1430 rt = rtcache_init(ro);
1431 if ((ifp == NULL) && (rt != NULL))
1432 ifp = rt->rt_ifp;
1433 # else
1434 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
1435 dst6->sin6_family = AF_INET6;
1436 dst6->sin6_len = sizeof(struct sockaddr_in6);
1437 dst6->sin6_addr = fin->fin_fi.fi_dst.in6;
1438
1439 if (fdp != NULL) {
1440 if (IP6_NOTZERO(&fdp->fd_ip6))
1441 dst6->sin6_addr = fdp->fd_ip6.in6;
1442 }
1443
1444 rtalloc((struct route *)ro);
1445
1446 if ((ifp == NULL) && (ro->ro_rt != NULL))
1447 ifp = ro->ro_rt->rt_ifp;
1448 rt = ro->ro_rt;
1449 # endif
1450 if ((rt == NULL) || (ifp == NULL)) {
1451
1452 error = EHOSTUNREACH;
1453 goto bad;
1454 }
1455
1456 /* KAME */
1457 # if __NetBSD_Version__ >= 499001100
1458 if (IN6_IS_ADDR_LINKLOCAL(&u.dst6.sin6_addr))
1459 u.dst6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1460 # else
1461 if (IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr))
1462 dst6->sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1463 # endif
1464
1465 {
1466 # if (__NetBSD_Version__ >= 106010000) && !defined(IN6_LINKMTU)
1467 struct in6_ifextra *ife;
1468 # endif
1469 if (rt->rt_flags & RTF_GATEWAY)
1470 # if __NetBSD_Version__ >= 499001100
1471 dst = rt->rt_gateway;
1472 # else
1473 dst6 = (struct sockaddr_in6 *)rt->rt_gateway;
1474 # endif
1475 rt->rt_use++;
1476
1477 /* Determine path MTU. */
1478 # if (__NetBSD_Version__ <= 106009999)
1479 mtu = nd_ifinfo[ifp->if_index].linkmtu;
1480 # else
1481 # ifdef IN6_LINKMTU
1482 mtu = IN6_LINKMTU(ifp);
1483 # else
1484 ife = (struct in6_ifextra *)(ifp)->if_afdata[AF_INET6];
1485 mtu = ife->nd_ifinfo[ifp->if_index].linkmtu;
1486 # endif
1487 # endif
1488 if ((error == 0) && (m0->m_pkthdr.len <= mtu)) {
1489 # if __NetBSD_Version__ >= 499001100
1490 error = ip6_if_output(ifp, ifp, m0, satocsin6(dst), rt);
1491 # else
1492 error = nd6_output(ifp, ifp, m0, dst6, rt);
1493 # endif
1494 } else {
1495 error = EMSGSIZE;
1496 }
1497 }
1498 bad:
1499 # if __NetBSD_Version__ >= 499001100
1500 rtcache_unref(rt, ro);
1501 rtcache_free(ro);
1502 # else
1503 if (ro->ro_rt != NULL) {
1504 RTFREE(((struct route *)ro)->ro_rt);
1505 }
1506 # endif
1507 return error;
1508 }
1509 #endif /* INET6 */
1510
1511
1512 int
1513 ipf_verifysrc(fr_info_t *fin)
1514 {
1515 #if __NetBSD_Version__ >= 499001100
1516 union {
1517 struct sockaddr dst;
1518 struct sockaddr_in dst4;
1519 } u;
1520 struct rtentry *rt;
1521 #else
1522 struct sockaddr_in *dst;
1523 #endif
1524 struct route iproute;
1525 int rc;
1526
1527 #if __NetBSD_Version__ >= 499001100
1528 sockaddr_in_init(&u.dst4, &fin->fin_src, 0);
1529 rtcache_setdst(&iproute, &u.dst);
1530 rt = rtcache_init(&iproute);
1531 if (rt == NULL)
1532 rc = 0;
1533 else
1534 rc = (fin->fin_ifp == rt->rt_ifp);
1535 rtcache_unref(rt, &iproute);
1536 rtcache_free(&iproute);
1537 #else
1538 dst = (struct sockaddr_in *)&iproute.ro_dst;
1539 dst->sin_len = sizeof(*dst);
1540 dst->sin_family = AF_INET;
1541 dst->sin_addr = fin->fin_src;
1542 rtalloc(&iproute);
1543 if (iproute.ro_rt == NULL)
1544 return 0;
1545 rc = (fin->fin_ifp == iproute.ro_rt->rt_ifp);
1546 RTFREE(iproute.ro_rt);
1547 #endif
1548 return rc;
1549 }
1550
1551
1552 /*
1553 * return the first IP Address associated with an interface
1554 */
1555 int
1556 ipf_ifpaddr(ipf_main_softc_t *softc, int v, int atype, void *ifptr,
1557 i6addr_t *inp, i6addr_t *inpmask)
1558 {
1559 #ifdef USE_INET6
1560 struct in6_addr *inp6 = NULL;
1561 #endif
1562 struct sockaddr *sock, *mask;
1563 struct sockaddr_in *sin;
1564 struct ifaddr *ifa;
1565 struct ifnet *ifp;
1566
1567 if ((ifptr == NULL) || (ifptr == (void *)-1))
1568 return -1;
1569
1570 ifp = ifptr;
1571 mask = NULL;
1572
1573 if (v == 4)
1574 inp->in4.s_addr = 0;
1575 #ifdef USE_INET6
1576 else if (v == 6)
1577 bzero((char *)inp, sizeof(*inp));
1578 #endif
1579
1580 ifa = IFADDR_READER_FIRST(ifp);
1581 sock = ifa ? ifa->ifa_addr : NULL;
1582 while (sock != NULL && ifa != NULL) {
1583 sin = (struct sockaddr_in *)sock;
1584 if ((v == 4) && (sin->sin_family == AF_INET))
1585 break;
1586 #ifdef USE_INET6
1587 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1588 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1589 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1590 !IN6_IS_ADDR_LOOPBACK(inp6))
1591 break;
1592 }
1593 #endif
1594 ifa = IFADDR_READER_NEXT(ifa);
1595 if (ifa != NULL)
1596 sock = ifa->ifa_addr;
1597 }
1598 if (ifa == NULL || sock == NULL)
1599 return -1;
1600
1601 mask = ifa->ifa_netmask;
1602 if (atype == FRI_BROADCAST)
1603 sock = ifa->ifa_broadaddr;
1604 else if (atype == FRI_PEERADDR)
1605 sock = ifa->ifa_dstaddr;
1606
1607 #ifdef USE_INET6
1608 if (v == 6)
1609 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1610 (struct sockaddr_in6 *)mask,
1611 inp, inpmask);
1612 #endif
1613 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1614 (struct sockaddr_in *)mask,
1615 &inp->in4, &inpmask->in4);
1616 }
1617
1618
1619 u_32_t
1620 ipf_newisn(fr_info_t *fin)
1621 {
1622 #if __NetBSD_Version__ >= 105190000 /* 1.5T */
1623 size_t asz;
1624
1625 if (fin->fin_v == 4)
1626 asz = sizeof(struct in_addr);
1627 else if (fin->fin_v == 6)
1628 asz = sizeof(fin->fin_src);
1629 else /* XXX: no way to return error */
1630 return 0;
1631 #ifdef INET
1632 return tcp_new_iss1((void *)&fin->fin_src, (void *)&fin->fin_dst,
1633 fin->fin_sport, fin->fin_dport, asz, 0);
1634 #else
1635 return ENOSYS;
1636 #endif
1637 #else
1638 static int iss_seq_off = 0;
1639 u_char hash[16];
1640 u_32_t newiss;
1641 MD5_CTX ctx;
1642
1643 /*
1644 * Compute the base value of the ISS. It is a hash
1645 * of (saddr, sport, daddr, dport, secret).
1646 */
1647 MD5Init(&ctx);
1648
1649 MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_src,
1650 sizeof(fin->fin_fi.fi_src));
1651 MD5Update(&ctx, (u_char *) &fin->fin_fi.fi_dst,
1652 sizeof(fin->fin_fi.fi_dst));
1653 MD5Update(&ctx, (u_char *) &fin->fin_dat, sizeof(fin->fin_dat));
1654
1655 MD5Update(&ctx, ipf_iss_secret, sizeof(ipf_iss_secret));
1656
1657 MD5Final(hash, &ctx);
1658
1659 memcpy(&newiss, hash, sizeof(newiss));
1660
1661 /*
1662 * Now increment our "timer", and add it in to
1663 * the computed value.
1664 *
1665 * XXX Use `addin'?
1666 * XXX TCP_ISSINCR too large to use?
1667 */
1668 iss_seq_off += 0x00010000;
1669 newiss += iss_seq_off;
1670 return newiss;
1671 #endif
1672 }
1673
1674
1675 /* ------------------------------------------------------------------------ */
1676 /* Function: ipf_nextipid */
1677 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1678 /* Parameters: fin(I) - pointer to packet information */
1679 /* */
1680 /* Returns the next IPv4 ID to use for this packet. */
1681 /* ------------------------------------------------------------------------ */
1682 u_short
1683 ipf_nextipid(fr_info_t *fin)
1684 {
1685 #ifdef USE_MUTEXES
1686 ipf_main_softc_t *softc = fin->fin_main_soft;
1687 #endif
1688 u_short id;
1689
1690 MUTEX_ENTER(&softc->ipf_rw);
1691 id = ipid++;
1692 MUTEX_EXIT(&softc->ipf_rw);
1693
1694 return id;
1695 }
1696
1697
1698 EXTERN_INLINE int
1699 ipf_checkv4sum(fr_info_t *fin)
1700 {
1701 #ifdef M_CSUM_TCP_UDP_BAD
1702 int manual, pflag, cflags, active;
1703 mb_t *m;
1704
1705 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1706 return 0;
1707
1708 if ((fin->fin_flx & FI_SHORT) != 0)
1709 return 1;
1710
1711 if (fin->fin_cksum != FI_CK_NEEDED)
1712 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1713
1714 manual = 0;
1715 m = fin->fin_m;
1716 if (m == NULL) {
1717 manual = 1;
1718 goto skipauto;
1719 }
1720
1721 switch (fin->fin_p)
1722 {
1723 case IPPROTO_UDP :
1724 pflag = M_CSUM_UDPv4;
1725 break;
1726 case IPPROTO_TCP :
1727 pflag = M_CSUM_TCPv4;
1728 break;
1729 default :
1730 pflag = 0;
1731 manual = 1;
1732 break;
1733 }
1734
1735 active = ((struct ifnet *)fin->fin_ifp)->if_csum_flags_rx & pflag;
1736 active |= M_CSUM_TCP_UDP_BAD | M_CSUM_DATA;
1737 cflags = m->m_pkthdr.csum_flags & active;
1738
1739 if (pflag != 0) {
1740 if (cflags == (pflag | M_CSUM_TCP_UDP_BAD)) {
1741 fin->fin_flx |= FI_BAD;
1742 fin->fin_cksum = FI_CK_BAD;
1743 } else if (cflags == (pflag | M_CSUM_DATA)) {
1744 if ((m->m_pkthdr.csum_data ^ 0xffff) != 0) {
1745 fin->fin_flx |= FI_BAD;
1746 fin->fin_cksum = FI_CK_BAD;
1747 } else {
1748 fin->fin_cksum = FI_CK_SUMOK;
1749 }
1750 } else if (cflags == pflag) {
1751 fin->fin_cksum = FI_CK_SUMOK;
1752 } else {
1753 manual = 1;
1754 }
1755 }
1756 skipauto:
1757 if (manual != 0) {
1758 if (ipf_checkl4sum(fin) == -1) {
1759 fin->fin_flx |= FI_BAD;
1760 return -1;
1761 }
1762 }
1763 #else
1764 if (ipf_checkl4sum(fin) == -1) {
1765 fin->fin_flx |= FI_BAD;
1766 return -1;
1767 }
1768 #endif
1769 return 0;
1770 }
1771
1772
1773 #ifdef USE_INET6
1774 EXTERN_INLINE int
1775 ipf_checkv6sum(fr_info_t *fin)
1776 {
1777 # ifdef M_CSUM_TCP_UDP_BAD
1778 int manual, pflag, cflags, active;
1779 mb_t *m;
1780
1781 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1782 return 0;
1783
1784 if ((fin->fin_flx & FI_SHORT) != 0)
1785 return 1;
1786
1787 if (fin->fin_cksum != FI_CK_SUMOK)
1788 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1789
1790
1791 manual = 0;
1792 m = fin->fin_m;
1793
1794 switch (fin->fin_p)
1795 {
1796 case IPPROTO_UDP :
1797 pflag = M_CSUM_UDPv6;
1798 break;
1799 case IPPROTO_TCP :
1800 pflag = M_CSUM_TCPv6;
1801 break;
1802 default :
1803 pflag = 0;
1804 manual = 1;
1805 break;
1806 }
1807
1808 active = ((struct ifnet *)fin->fin_ifp)->if_csum_flags_rx & pflag;
1809 active |= M_CSUM_TCP_UDP_BAD | M_CSUM_DATA;
1810 cflags = m->m_pkthdr.csum_flags & active;
1811
1812 if (pflag != 0) {
1813 if (cflags == (pflag | M_CSUM_TCP_UDP_BAD)) {
1814 fin->fin_flx |= FI_BAD;
1815 } else if (cflags == (pflag | M_CSUM_DATA)) {
1816 if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
1817 fin->fin_flx |= FI_BAD;
1818 } else if (cflags == pflag) {
1819 ;
1820 } else {
1821 manual = 1;
1822 }
1823 }
1824 if (manual != 0) {
1825 if (ipf_checkl4sum(fin) == -1) {
1826 fin->fin_flx |= FI_BAD;
1827 return -1;
1828 }
1829 }
1830 # else
1831 if (ipf_checkl4sum(fin) == -1) {
1832 fin->fin_flx |= FI_BAD;
1833 return -1;
1834 }
1835 # endif
1836 return 0;
1837 }
1838 #endif /* USE_INET6 */
1839
1840
1841 size_t
1842 mbufchainlen(struct mbuf *m0)
1843 {
1844 size_t len;
1845
1846 if ((m0->m_flags & M_PKTHDR) != 0) {
1847 len = m0->m_pkthdr.len;
1848 } else {
1849 struct mbuf *m;
1850
1851 for (m = m0, len = 0; m != NULL; m = m->m_next)
1852 len += m->m_len;
1853 }
1854 return len;
1855 }
1856
1857
1858 /* ------------------------------------------------------------------------ */
1859 /* Function: ipf_pullup */
1860 /* Returns: NULL == pullup failed, else pointer to protocol header */
1861 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1862 /* fin(I) - pointer to packet information */
1863 /* len(I) - number of bytes to pullup */
1864 /* */
1865 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1866 /* single buffer for ease of access. Operating system native functions are */
1867 /* used to manage buffers - if necessary. If the entire packet ends up in */
1868 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1869 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1870 /* and ONLY if the pullup succeeds. */
1871 /* */
1872 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1873 /* of buffers that starts at *fin->fin_mp. */
1874 /* ------------------------------------------------------------------------ */
1875 void *
1876 ipf_pullup(mb_t *xmin, fr_info_t *fin, int len)
1877 {
1878 int dpoff, ipoff;
1879 mb_t *m = xmin;
1880 char *ip;
1881
1882 if (m == NULL)
1883 return NULL;
1884
1885 ip = (char *)fin->fin_ip;
1886 if ((fin->fin_flx & FI_COALESCE) != 0)
1887 return ip;
1888
1889 ipoff = fin->fin_ipoff;
1890 if (fin->fin_dp != NULL)
1891 dpoff = (char *)fin->fin_dp - (char *)ip;
1892 else
1893 dpoff = 0;
1894
1895 if (M_LEN(m) < len) {
1896 mb_t *n = *fin->fin_mp;
1897 /*
1898 * Assume that M_PKTHDR is set and just work with what is left
1899 * rather than check..
1900 * Should not make any real difference, anyway.
1901 */
1902 if (m != n) {
1903 /*
1904 * Record the mbuf that points to the mbuf that we're
1905 * about to go to work on so that we can update the
1906 * m_next appropriately later.
1907 */
1908 for (; n->m_next != m; n = n->m_next)
1909 ;
1910 } else {
1911 n = NULL;
1912 }
1913
1914 #ifdef MHLEN
1915 if (len > MHLEN)
1916 #else
1917 if (len > MLEN)
1918 #endif
1919 {
1920 #ifdef HAVE_M_PULLDOWN
1921 if (m_pulldown(m, 0, len, NULL) == NULL)
1922 m = NULL;
1923 #else
1924 FREE_MB_T(*fin->fin_mp);
1925 m = NULL;
1926 n = NULL;
1927 #endif
1928 } else
1929 {
1930 m = m_pullup(m, len);
1931 }
1932 if (n != NULL)
1933 n->m_next = m;
1934 if (m == NULL) {
1935 /*
1936 * When n is non-NULL, it indicates that m pointed to
1937 * a sub-chain (tail) of the mbuf and that the head
1938 * of this chain has not yet been free'd.
1939 */
1940 if (n != NULL) {
1941 FREE_MB_T(*fin->fin_mp);
1942 }
1943
1944 *fin->fin_mp = NULL;
1945 fin->fin_m = NULL;
1946 return NULL;
1947 }
1948
1949 if (n == NULL)
1950 *fin->fin_mp = m;
1951
1952 while (M_LEN(m) == 0) {
1953 m = m->m_next;
1954 }
1955 fin->fin_m = m;
1956 ip = MTOD(m, char *) + ipoff;
1957
1958 fin->fin_ip = (ip_t *)ip;
1959 if (fin->fin_dp != NULL)
1960 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1961 if (fin->fin_fraghdr != NULL)
1962 fin->fin_fraghdr = (char *)ip +
1963 ((char *)fin->fin_fraghdr -
1964 (char *)fin->fin_ip);
1965 }
1966
1967 if (len == fin->fin_plen)
1968 fin->fin_flx |= FI_COALESCE;
1969 return ip;
1970 }
1971
1972
1973 int
1974 ipf_inject(fr_info_t *fin, mb_t *m)
1975 {
1976 int error;
1977
1978 if (fin->fin_out == 0) {
1979 if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
1980 FREE_MB_T(m);
1981 error = ENOBUFS;
1982 } else {
1983 error = 0;
1984 }
1985 } else {
1986 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1987 }
1988 return error;
1989 }
1990
1991
1992 u_32_t
1993 ipf_random(void)
1994 {
1995 int number;
1996
1997 #ifdef _CPRNG_H
1998 number = cprng_fast32();
1999 #else
2000 number = arc4random();
2001 #endif
2002 return number;
2003 }
2004
2005
2006 /*
2007 * routines below for saving IP headers to buffer
2008 */
2009 static int ipfopen(dev_t dev, int flags
2010 #if (NetBSD >= 199511)
2011 , int devtype, PROC_T *p
2012 #endif
2013 )
2014 {
2015 u_int unit = GET_MINOR(dev);
2016 int error;
2017
2018 if (IPL_LOGMAX < unit) {
2019 error = ENXIO;
2020 } else {
2021 switch (unit)
2022 {
2023 case IPL_LOGIPF :
2024 case IPL_LOGNAT :
2025 case IPL_LOGSTATE :
2026 case IPL_LOGAUTH :
2027 case IPL_LOGLOOKUP :
2028 case IPL_LOGSYNC :
2029 #ifdef IPFILTER_SCAN
2030 case IPL_LOGSCAN :
2031 #endif
2032 error = 0;
2033 break;
2034 default :
2035 error = ENXIO;
2036 break;
2037 }
2038 }
2039 #if (__NetBSD_Version__ >= 799003000)
2040 if (error == 0) {
2041 mutex_enter(&ipf_ref_mutex);
2042 ipf_active = 1;
2043 mutex_exit(&ipf_ref_mutex);
2044 }
2045 #endif
2046 return error;
2047 }
2048
2049
2050 static int ipfclose(dev_t dev, int flags
2051 #if (NetBSD >= 199511)
2052 , int devtype, PROC_T *p
2053 #endif
2054 )
2055 {
2056 u_int unit = GET_MINOR(dev);
2057
2058 if (IPL_LOGMAX < unit)
2059 return ENXIO;
2060 else {
2061 #if (__NetBSD_Version__ >= 799003000)
2062 mutex_enter(&ipf_ref_mutex);
2063 ipf_active = 0;
2064 mutex_exit(&ipf_ref_mutex);
2065 #endif
2066 return 0;
2067 }
2068 }
2069
2070 /*
2071 * ipfread/ipflog
2072 * both of these must operate with at least splnet() lest they be
2073 * called during packet processing and cause an inconsistancy to appear in
2074 * the filter lists.
2075 */
2076 static int ipfread(dev_t dev, struct uio *uio, int ioflag)
2077 {
2078
2079 if (ipfmain.ipf_running < 1) {
2080 ipfmain.ipf_interror = 130006;
2081 return EIO;
2082 }
2083
2084 if (GET_MINOR(dev) == IPL_LOGSYNC)
2085 return ipf_sync_read(&ipfmain, uio);
2086
2087 #ifdef IPFILTER_LOG
2088 return ipf_log_read(&ipfmain, GET_MINOR(dev), uio);
2089 #else
2090 ipfmain.ipf_interror = 130007;
2091 return ENXIO;
2092 #endif
2093 }
2094
2095
2096 /*
2097 * ipfwrite
2098 * both of these must operate with at least splnet() lest they be
2099 * called during packet processing and cause an inconsistancy to appear in
2100 * the filter lists.
2101 */
2102 static int ipfwrite(dev_t dev, struct uio *uio, int ioflag)
2103 {
2104
2105 if (ipfmain.ipf_running < 1) {
2106 ipfmain.ipf_interror = 130008;
2107 return EIO;
2108 }
2109
2110 if (GET_MINOR(dev) == IPL_LOGSYNC)
2111 return ipf_sync_write(&ipfmain, uio);
2112 ipfmain.ipf_interror = 130009;
2113 return ENXIO;
2114 }
2115
2116
2117 static int ipfpoll(dev_t dev, int events, PROC_T *p)
2118 {
2119 u_int unit = GET_MINOR(dev);
2120 int revents = 0;
2121
2122 if (IPL_LOGMAX < unit) {
2123 ipfmain.ipf_interror = 130010;
2124 return ENXIO;
2125 }
2126
2127 switch (unit)
2128 {
2129 case IPL_LOGIPF :
2130 case IPL_LOGNAT :
2131 case IPL_LOGSTATE :
2132 #ifdef IPFILTER_LOG
2133 if ((events & (POLLIN | POLLRDNORM)) &&
2134 ipf_log_canread(&ipfmain, unit))
2135 revents |= events & (POLLIN | POLLRDNORM);
2136 #endif
2137 break;
2138 case IPL_LOGAUTH :
2139 if ((events & (POLLIN | POLLRDNORM)) &&
2140 ipf_auth_waiting(&ipfmain))
2141 revents |= events & (POLLIN | POLLRDNORM);
2142 break;
2143 case IPL_LOGSYNC :
2144 if ((events & (POLLIN | POLLRDNORM)) &&
2145 ipf_sync_canread(&ipfmain))
2146 revents |= events & (POLLIN | POLLRDNORM);
2147 if ((events & (POLLOUT | POLLWRNORM)) &&
2148 ipf_sync_canwrite(&ipfmain))
2149 revents |= events & (POLLOUT | POLLWRNORM);
2150 break;
2151 case IPL_LOGSCAN :
2152 case IPL_LOGLOOKUP :
2153 default :
2154 break;
2155 }
2156
2157 if ((revents == 0) && (((events & (POLLIN|POLLRDNORM)) != 0)))
2158 selrecord(p, &ipfmain.ipf_selwait[unit]);
2159 return revents;
2160 }
2161
2162 u_int
2163 ipf_pcksum(fr_info_t *fin, int hlen, u_int sum)
2164 {
2165 struct mbuf *m;
2166 u_int sum2;
2167 int off;
2168
2169 m = fin->fin_m;
2170 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
2171 m->m_data += hlen;
2172 m->m_len -= hlen;
2173 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
2174 m->m_len += hlen;
2175 m->m_data -= hlen;
2176
2177 /*
2178 * Both sum and sum2 are partial sums, so combine them together.
2179 */
2180 sum += ~sum2 & 0xffff;
2181 while (sum > 0xffff)
2182 sum = (sum & 0xffff) + (sum >> 16);
2183 sum2 = ~sum & 0xffff;
2184 return sum2;
2185 }
2186
2187 #if (__NetBSD_Version__ >= 799003000)
2188
2189 /* NetBSD module interface */
2190
2191 MODULE(MODULE_CLASS_DRIVER, ipl, "bpf_filter");
2192
2193 static int ipl_init(void *);
2194 static int ipl_fini(void *);
2195 static int ipl_modcmd(modcmd_t, void *);
2196
2197 #ifdef _MODULE
2198 static devmajor_t ipl_cmaj = -1, ipl_bmaj = -1;
2199 #endif
2200
2201 static int
2202 ipl_modcmd(modcmd_t cmd, void *opaque)
2203 {
2204
2205 switch (cmd) {
2206 case MODULE_CMD_INIT:
2207 return ipl_init(opaque);
2208 case MODULE_CMD_FINI:
2209 return ipl_fini(opaque);
2210 default:
2211 return ENOTTY;
2212 }
2213 }
2214
2215 static int
2216 ipl_init(void *opaque)
2217 {
2218 int error;
2219
2220 ipf_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
2221 ipf_listener_cb, NULL);
2222
2223 if ((error = ipf_load_all()) != 0)
2224 return error;
2225
2226 if (ipf_create_all(&ipfmain) == NULL) {
2227 ipf_unload_all();
2228 return ENODEV;
2229 }
2230
2231 /* Initialize our mutex and reference count */
2232 mutex_init(&ipf_ref_mutex, MUTEX_DEFAULT, IPL_NONE);
2233 ipf_active = 0;
2234
2235 #ifdef _MODULE
2236 /*
2237 * Insert ourself into the cdevsw list.
2238 */
2239 error = devsw_attach("ipl", NULL, &ipl_bmaj, &ipl_cdevsw, &ipl_cmaj);
2240 if (error)
2241 ipl_fini(opaque);
2242 #endif
2243
2244 return error;
2245 }
2246
2247 static int
2248 ipl_fini(void *opaque)
2249 {
2250
2251 #ifdef _MODULE
2252 (void)devsw_detach(NULL, &ipl_cdevsw);
2253 #endif
2254
2255 /*
2256 * Grab the mutex, verify that there are no references
2257 * and that there are no running filters. If either
2258 * of these exists, reinsert our cdevsw entry and return
2259 * an error.
2260 */
2261 mutex_enter(&ipf_ref_mutex);
2262 if (ipf_active != 0 || ipfmain.ipf_running > 0) {
2263 #ifdef _MODULE
2264 (void)devsw_attach("ipl", NULL, &ipl_bmaj,
2265 &ipl_cdevsw, &ipl_cmaj);
2266 #endif
2267 mutex_exit(&ipf_ref_mutex);
2268 return EBUSY;
2269 }
2270
2271 /* Clean up the rest of our state before being unloaded */
2272
2273 mutex_exit(&ipf_ref_mutex);
2274 mutex_destroy(&ipf_ref_mutex);
2275 ipf_destroy_all(&ipfmain);
2276 ipf_unload_all();
2277 kauth_unlisten_scope(ipf_listener);
2278
2279 return 0;
2280 }
2281 #endif /* (__NetBSD_Version__ >= 799003000) */
2282