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