No empty .Rs/.Re
[netbsd-mini2440.git] / sys / net / if_tun.c
blobb4cd7aafbf560c88cffd7c30aeba103b318a23ce
1 /* $NetBSD: if_tun.c,v 1.110 2008/11/20 21:55:15 dyoung Exp $ */
3 /*
4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
5 * Nottingham University 1987.
7 * This source may be freely distributed, however I would be interested
8 * in any changes that are made.
10 * This driver takes packets off the IP i/f and hands them up to a
11 * user process to have its wicked way with. This driver has its
12 * roots in a similar driver written by Phil Cockcroft (formerly) at
13 * UCL. This driver is based much more on read/write/poll mode of
14 * operation though.
17 #include <sys/cdefs.h>
18 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.110 2008/11/20 21:55:15 dyoung Exp $");
20 #include "opt_inet.h"
22 #include <sys/param.h>
23 #include <sys/proc.h>
24 #include <sys/systm.h>
25 #include <sys/mbuf.h>
26 #include <sys/buf.h>
27 #include <sys/protosw.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
30 #include <sys/errno.h>
31 #include <sys/syslog.h>
32 #include <sys/select.h>
33 #include <sys/poll.h>
34 #include <sys/file.h>
35 #include <sys/signalvar.h>
36 #include <sys/conf.h>
37 #include <sys/kauth.h>
38 #include <sys/simplelock.h>
39 #include <sys/cpu.h>
41 #include <net/if.h>
42 #include <net/if_types.h>
43 #include <net/netisr.h>
44 #include <net/route.h>
47 #ifdef INET
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/if_inarp.h>
53 #endif
56 #include "bpfilter.h"
57 #if NBPFILTER > 0
58 #include <sys/time.h>
59 #include <net/bpf.h>
60 #endif
62 #include <net/if_tun.h>
64 #define TUNDEBUG if (tundebug) printf
65 int tundebug = 0;
67 extern int ifqmaxlen;
68 void tunattach(int);
70 static LIST_HEAD(, tun_softc) tun_softc_list;
71 static LIST_HEAD(, tun_softc) tunz_softc_list;
72 static struct simplelock tun_softc_lock;
74 static int tun_ioctl(struct ifnet *, u_long, void *);
75 static int tun_output(struct ifnet *, struct mbuf *,
76 const struct sockaddr *, struct rtentry *rt);
77 static int tun_clone_create(struct if_clone *, int);
78 static int tun_clone_destroy(struct ifnet *);
80 static struct if_clone tun_cloner =
81 IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
83 static void tunattach0(struct tun_softc *);
84 static void tuninit(struct tun_softc *);
85 static void tun_i_softintr(void *);
86 static void tun_o_softintr(void *);
87 #ifdef ALTQ
88 static void tunstart(struct ifnet *);
89 #endif
90 static struct tun_softc *tun_find_unit(dev_t);
91 static struct tun_softc *tun_find_zunit(int);
93 static dev_type_open(tunopen);
94 static dev_type_close(tunclose);
95 static dev_type_read(tunread);
96 static dev_type_write(tunwrite);
97 static dev_type_ioctl(tunioctl);
98 static dev_type_poll(tunpoll);
99 static dev_type_kqfilter(tunkqfilter);
101 const struct cdevsw tun_cdevsw = {
102 tunopen, tunclose, tunread, tunwrite, tunioctl,
103 nostop, notty, tunpoll, nommap, tunkqfilter, D_OTHER,
106 void
107 tunattach(int unused)
110 simple_lock_init(&tun_softc_lock);
111 LIST_INIT(&tun_softc_list);
112 LIST_INIT(&tunz_softc_list);
113 if_clone_attach(&tun_cloner);
117 * Find driver instance from dev_t.
118 * Call at splnet().
119 * Returns with tp locked (if found).
121 static struct tun_softc *
122 tun_find_unit(dev_t dev)
124 struct tun_softc *tp;
125 int unit = minor(dev);
127 simple_lock(&tun_softc_lock);
128 LIST_FOREACH(tp, &tun_softc_list, tun_list)
129 if (unit == tp->tun_unit)
130 break;
131 if (tp)
132 simple_lock(&tp->tun_lock);
133 simple_unlock(&tun_softc_lock);
135 return (tp);
139 * Find zombie driver instance by unit number.
140 * Call at splnet().
141 * Remove tp from list and return it unlocked (if found).
143 static struct tun_softc *
144 tun_find_zunit(int unit)
146 struct tun_softc *tp;
148 simple_lock(&tun_softc_lock);
149 LIST_FOREACH(tp, &tunz_softc_list, tun_list)
150 if (unit == tp->tun_unit)
151 break;
152 if (tp)
153 LIST_REMOVE(tp, tun_list);
154 simple_unlock(&tun_softc_lock);
155 #ifdef DIAGNOSTIC
156 if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
157 printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
158 #endif
160 return (tp);
163 static int
164 tun_clone_create(struct if_clone *ifc, int unit)
166 struct tun_softc *tp;
168 if ((tp = tun_find_zunit(unit)) == NULL) {
169 /* Allocate a new instance */
170 tp = malloc(sizeof(*tp), M_DEVBUF, M_WAITOK|M_ZERO);
172 tp->tun_unit = unit;
173 simple_lock_init(&tp->tun_lock);
174 selinit(&tp->tun_rsel);
175 selinit(&tp->tun_wsel);
176 } else {
177 /* Revive tunnel instance; clear ifp part */
178 (void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
181 if_initname(&tp->tun_if, ifc->ifc_name, unit);
182 tunattach0(tp);
183 tp->tun_flags |= TUN_INITED;
184 tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
185 tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
187 simple_lock(&tun_softc_lock);
188 LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
189 simple_unlock(&tun_softc_lock);
191 return (0);
194 static void
195 tunattach0(struct tun_softc *tp)
197 struct ifnet *ifp;
199 ifp = &tp->tun_if;
200 ifp->if_softc = tp;
201 ifp->if_mtu = TUNMTU;
202 ifp->if_ioctl = tun_ioctl;
203 ifp->if_output = tun_output;
204 #ifdef ALTQ
205 ifp->if_start = tunstart;
206 #endif
207 ifp->if_flags = IFF_POINTOPOINT;
208 ifp->if_type = IFT_TUNNEL;
209 ifp->if_snd.ifq_maxlen = ifqmaxlen;
210 ifp->if_collisions = 0;
211 ifp->if_ierrors = 0;
212 ifp->if_oerrors = 0;
213 ifp->if_ipackets = 0;
214 ifp->if_opackets = 0;
215 ifp->if_ibytes = 0;
216 ifp->if_obytes = 0;
217 ifp->if_dlt = DLT_NULL;
218 IFQ_SET_READY(&ifp->if_snd);
219 if_attach(ifp);
220 if_alloc_sadl(ifp);
221 #if NBPFILTER > 0
222 bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
223 #endif
226 static int
227 tun_clone_destroy(struct ifnet *ifp)
229 struct tun_softc *tp = (void *)ifp;
230 int s, zombie = 0;
232 s = splnet();
233 simple_lock(&tun_softc_lock);
234 simple_lock(&tp->tun_lock);
235 LIST_REMOVE(tp, tun_list);
236 if (tp->tun_flags & TUN_OPEN) {
237 /* Hang on to storage until last close */
238 zombie = 1;
239 tp->tun_flags &= ~TUN_INITED;
240 LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
242 simple_unlock(&tun_softc_lock);
244 IF_PURGE(&ifp->if_snd);
245 ifp->if_flags &= ~IFF_RUNNING;
247 if (tp->tun_flags & TUN_RWAIT) {
248 tp->tun_flags &= ~TUN_RWAIT;
249 wakeup((void *)tp);
251 selnotify(&tp->tun_rsel, 0, 0);
253 simple_unlock(&tp->tun_lock);
254 splx(s);
256 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
257 fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
259 #if NBPFILTER > 0
260 bpfdetach(ifp);
261 #endif
262 if_detach(ifp);
264 if (!zombie) {
265 seldestroy(&tp->tun_rsel);
266 seldestroy(&tp->tun_wsel);
267 softint_disestablish(tp->tun_osih);
268 softint_disestablish(tp->tun_isih);
269 free(tp, M_DEVBUF);
272 return (0);
276 * tunnel open - must be superuser & the device must be
277 * configured in
279 static int
280 tunopen(dev_t dev, int flag, int mode, struct lwp *l)
282 struct ifnet *ifp;
283 struct tun_softc *tp;
284 int s, error;
286 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_TUN,
287 KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD, NULL, NULL, NULL);
288 if (error)
289 return (error);
291 s = splnet();
292 tp = tun_find_unit(dev);
294 if (tp == NULL) {
295 (void)tun_clone_create(&tun_cloner, minor(dev));
296 tp = tun_find_unit(dev);
297 if (tp == NULL) {
298 error = ENXIO;
299 goto out_nolock;
303 if (tp->tun_flags & TUN_OPEN) {
304 error = EBUSY;
305 goto out;
308 ifp = &tp->tun_if;
309 tp->tun_flags |= TUN_OPEN;
310 TUNDEBUG("%s: open\n", ifp->if_xname);
311 out:
312 simple_unlock(&tp->tun_lock);
313 out_nolock:
314 splx(s);
315 return (error);
319 * tunclose - close the device - mark i/f down & delete
320 * routing info
323 tunclose(dev_t dev, int flag, int mode,
324 struct lwp *l)
326 int s;
327 struct tun_softc *tp;
328 struct ifnet *ifp;
330 s = splnet();
331 if ((tp = tun_find_zunit(minor(dev))) != NULL) {
332 /* interface was "destroyed" before the close */
333 seldestroy(&tp->tun_rsel);
334 seldestroy(&tp->tun_wsel);
335 softint_disestablish(tp->tun_osih);
336 softint_disestablish(tp->tun_isih);
337 free(tp, M_DEVBUF);
338 goto out_nolock;
341 if ((tp = tun_find_unit(dev)) == NULL)
342 goto out_nolock;
344 ifp = &tp->tun_if;
346 tp->tun_flags &= ~TUN_OPEN;
349 * junk all pending output
351 IFQ_PURGE(&ifp->if_snd);
353 if (ifp->if_flags & IFF_UP) {
354 if_down(ifp);
355 if (ifp->if_flags & IFF_RUNNING) {
356 /* find internet addresses and delete routes */
357 struct ifaddr *ifa;
358 IFADDR_FOREACH(ifa, ifp) {
359 #if defined(INET) || defined(INET6)
360 if (ifa->ifa_addr->sa_family == AF_INET ||
361 ifa->ifa_addr->sa_family == AF_INET6) {
362 rtinit(ifa, (int)RTM_DELETE,
363 tp->tun_flags & TUN_DSTADDR
364 ? RTF_HOST
365 : 0);
367 #endif
371 tp->tun_pgid = 0;
372 selnotify(&tp->tun_rsel, 0, 0);
374 TUNDEBUG ("%s: closed\n", ifp->if_xname);
375 simple_unlock(&tp->tun_lock);
376 out_nolock:
377 splx(s);
378 return (0);
382 * Call at splnet().
384 static void
385 tuninit(struct tun_softc *tp)
387 struct ifnet *ifp = &tp->tun_if;
388 struct ifaddr *ifa;
390 TUNDEBUG("%s: tuninit\n", ifp->if_xname);
392 simple_lock(&tp->tun_lock);
393 ifp->if_flags |= IFF_UP | IFF_RUNNING;
395 tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
396 IFADDR_FOREACH(ifa, ifp) {
397 #ifdef INET
398 if (ifa->ifa_addr->sa_family == AF_INET) {
399 struct sockaddr_in *sin;
401 sin = satosin(ifa->ifa_addr);
402 if (sin && sin->sin_addr.s_addr)
403 tp->tun_flags |= TUN_IASET;
405 if (ifp->if_flags & IFF_POINTOPOINT) {
406 sin = satosin(ifa->ifa_dstaddr);
407 if (sin && sin->sin_addr.s_addr)
408 tp->tun_flags |= TUN_DSTADDR;
411 #endif
412 #ifdef INET6
413 if (ifa->ifa_addr->sa_family == AF_INET6) {
414 struct sockaddr_in6 *sin;
416 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
417 if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
418 tp->tun_flags |= TUN_IASET;
420 if (ifp->if_flags & IFF_POINTOPOINT) {
421 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
422 if (sin &&
423 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
424 tp->tun_flags |= TUN_DSTADDR;
425 } else
426 tp->tun_flags &= ~TUN_DSTADDR;
428 #endif /* INET6 */
431 simple_unlock(&tp->tun_lock);
432 return;
436 * Process an ioctl request.
438 static int
439 tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
441 int error = 0, s;
442 struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
443 struct ifreq *ifr = data;
445 s = splnet();
447 switch (cmd) {
448 case SIOCINITIFADDR:
449 tuninit(tp);
450 TUNDEBUG("%s: address set\n", ifp->if_xname);
451 break;
452 case SIOCSIFDSTADDR:
453 tuninit(tp);
454 TUNDEBUG("%s: destination address set\n", ifp->if_xname);
455 break;
456 case SIOCSIFBRDADDR:
457 TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
458 break;
459 case SIOCSIFMTU:
460 if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
461 error = EINVAL;
462 break;
464 TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
465 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
466 error = 0;
467 break;
468 case SIOCADDMULTI:
469 case SIOCDELMULTI:
470 if (ifr == NULL) {
471 error = EAFNOSUPPORT; /* XXX */
472 break;
474 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
475 #ifdef INET
476 case AF_INET:
477 break;
478 #endif
479 #ifdef INET6
480 case AF_INET6:
481 break;
482 #endif
483 default:
484 error = EAFNOSUPPORT;
485 break;
487 break;
488 default:
489 error = ifioctl_common(ifp, cmd, data);
492 splx(s);
493 return (error);
497 * tun_output - queue packets from higher level ready to put out.
499 static int
500 tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
501 struct rtentry *rt)
503 struct tun_softc *tp = ifp->if_softc;
504 int s;
505 int error;
506 #if defined(INET) || defined(INET6)
507 int mlen;
508 uint32_t *af;
509 #endif
510 ALTQ_DECL(struct altq_pktattr pktattr;)
512 s = splnet();
513 simple_lock(&tp->tun_lock);
514 TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
516 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
517 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
518 tp->tun_flags);
519 m_freem (m0);
520 error = EHOSTDOWN;
521 goto out;
525 * if the queueing discipline needs packet classification,
526 * do it before prepending link headers.
528 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
530 #if NBPFILTER > 0
531 if (ifp->if_bpf)
532 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m0);
533 #endif
535 switch(dst->sa_family) {
536 #ifdef INET6
537 case AF_INET6:
538 #endif
539 #ifdef INET
540 case AF_INET:
541 #endif
542 #if defined(INET) || defined(INET6)
543 if (tp->tun_flags & TUN_PREPADDR) {
544 /* Simple link-layer header */
545 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
546 if (m0 == NULL) {
547 IF_DROP(&ifp->if_snd);
548 error = ENOBUFS;
549 goto out;
551 bcopy(dst, mtod(m0, char *), dst->sa_len);
554 if (tp->tun_flags & TUN_IFHEAD) {
555 /* Prepend the address family */
556 M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
557 if (m0 == NULL) {
558 IF_DROP(&ifp->if_snd);
559 error = ENOBUFS;
560 goto out;
562 af = mtod(m0,uint32_t *);
563 *af = htonl(dst->sa_family);
564 } else {
565 #ifdef INET
566 if (dst->sa_family != AF_INET)
567 #endif
569 m_freem(m0);
570 error = EAFNOSUPPORT;
571 goto out;
574 /* FALLTHROUGH */
575 case AF_UNSPEC:
576 IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
577 if (error) {
578 ifp->if_collisions++;
579 error = EAFNOSUPPORT;
580 goto out;
582 mlen = m0->m_pkthdr.len;
583 ifp->if_opackets++;
584 ifp->if_obytes += mlen;
585 break;
586 #endif
587 default:
588 m_freem(m0);
589 error = EAFNOSUPPORT;
590 goto out;
593 if (tp->tun_flags & TUN_RWAIT) {
594 tp->tun_flags &= ~TUN_RWAIT;
595 wakeup((void *)tp);
597 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
598 softint_schedule(tp->tun_isih);
600 selnotify(&tp->tun_rsel, 0, 0);
601 out:
602 simple_unlock(&tp->tun_lock);
603 splx(s);
604 return (0);
607 static void
608 tun_i_softintr(void *cookie)
610 struct tun_softc *tp = cookie;
612 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
613 fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
614 NULL);
617 static void
618 tun_o_softintr(void *cookie)
620 struct tun_softc *tp = cookie;
622 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
623 fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM,
624 NULL);
628 * the cdevsw interface is now pretty minimal.
631 tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
633 struct tun_softc *tp;
634 int s, error = 0;
636 s = splnet();
637 tp = tun_find_unit(dev);
639 /* interface was "destroyed" already */
640 if (tp == NULL) {
641 error = ENXIO;
642 goto out_nolock;
645 switch (cmd) {
646 case TUNSDEBUG:
647 tundebug = *(int *)data;
648 break;
650 case TUNGDEBUG:
651 *(int *)data = tundebug;
652 break;
654 case TUNSIFMODE:
655 switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
656 case IFF_POINTOPOINT:
657 case IFF_BROADCAST:
658 if (tp->tun_if.if_flags & IFF_UP) {
659 error = EBUSY;
660 goto out;
662 tp->tun_if.if_flags &=
663 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
664 tp->tun_if.if_flags |= *(int *)data;
665 break;
666 default:
667 error = EINVAL;
668 goto out;
670 break;
672 case TUNSLMODE:
673 if (*(int *)data) {
674 tp->tun_flags |= TUN_PREPADDR;
675 tp->tun_flags &= ~TUN_IFHEAD;
676 } else
677 tp->tun_flags &= ~TUN_PREPADDR;
678 break;
680 case TUNSIFHEAD:
681 if (*(int *)data) {
682 tp->tun_flags |= TUN_IFHEAD;
683 tp->tun_flags &= ~TUN_PREPADDR;
684 } else
685 tp->tun_flags &= ~TUN_IFHEAD;
686 break;
688 case TUNGIFHEAD:
689 *(int *)data = (tp->tun_flags & TUN_IFHEAD);
690 break;
692 case FIONBIO:
693 if (*(int *)data)
694 tp->tun_flags |= TUN_NBIO;
695 else
696 tp->tun_flags &= ~TUN_NBIO;
697 break;
699 case FIOASYNC:
700 if (*(int *)data)
701 tp->tun_flags |= TUN_ASYNC;
702 else
703 tp->tun_flags &= ~TUN_ASYNC;
704 break;
706 case FIONREAD:
707 if (tp->tun_if.if_snd.ifq_head)
708 *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
709 else
710 *(int *)data = 0;
711 break;
713 case TIOCSPGRP:
714 case FIOSETOWN:
715 error = fsetown(&tp->tun_pgid, cmd, data);
716 break;
718 case TIOCGPGRP:
719 case FIOGETOWN:
720 error = fgetown(tp->tun_pgid, cmd, data);
721 break;
723 default:
724 error = ENOTTY;
727 out:
728 simple_unlock(&tp->tun_lock);
729 out_nolock:
730 splx(s);
731 return (error);
735 * The cdevsw read interface - reads a packet at a time, or at
736 * least as much of a packet as can be read.
739 tunread(dev_t dev, struct uio *uio, int ioflag)
741 struct tun_softc *tp;
742 struct ifnet *ifp;
743 struct mbuf *m, *m0;
744 int error = 0, len, s, index;
746 s = splnet();
747 tp = tun_find_unit(dev);
749 /* interface was "destroyed" already */
750 if (tp == NULL) {
751 error = ENXIO;
752 goto out_nolock;
755 index = tp->tun_if.if_index;
756 ifp = &tp->tun_if;
758 TUNDEBUG ("%s: read\n", ifp->if_xname);
759 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
760 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
761 error = EHOSTDOWN;
762 goto out;
765 tp->tun_flags &= ~TUN_RWAIT;
767 do {
768 IFQ_DEQUEUE(&ifp->if_snd, m0);
769 if (m0 == 0) {
770 if (tp->tun_flags & TUN_NBIO) {
771 error = EWOULDBLOCK;
772 goto out;
774 tp->tun_flags |= TUN_RWAIT;
775 if (ltsleep((void *)tp, PZERO|PCATCH|PNORELOCK,
776 "tunread", 0, &tp->tun_lock) != 0) {
777 error = EINTR;
778 goto out_nolock;
779 } else {
781 * Maybe the interface was destroyed while
782 * we were sleeping, so let's ensure that
783 * we're looking at the same (valid) tun
784 * interface before looping.
786 tp = tun_find_unit(dev);
787 if (tp == NULL) {
788 error = ENXIO;
789 goto out_nolock;
791 if (tp->tun_if.if_index != index) {
792 error = ENXIO;
793 goto out;
797 } while (m0 == 0);
799 simple_unlock(&tp->tun_lock);
800 splx(s);
802 /* Copy the mbuf chain */
803 while (m0 && uio->uio_resid > 0 && error == 0) {
804 len = min(uio->uio_resid, m0->m_len);
805 if (len != 0)
806 error = uiomove(mtod(m0, void *), len, uio);
807 MFREE(m0, m);
808 m0 = m;
811 if (m0) {
812 TUNDEBUG("Dropping mbuf\n");
813 m_freem(m0);
815 if (error)
816 ifp->if_ierrors++;
818 return (error);
820 out:
821 simple_unlock(&tp->tun_lock);
822 out_nolock:
823 splx(s);
824 return (error);
828 * the cdevsw write interface - an atomic write is a packet - or else!
831 tunwrite(dev_t dev, struct uio *uio, int ioflag)
833 struct tun_softc *tp;
834 struct ifnet *ifp;
835 struct mbuf *top, **mp, *m;
836 struct ifqueue *ifq;
837 struct sockaddr dst;
838 int isr, error = 0, s, tlen, mlen;
839 uint32_t family;
841 s = splnet();
842 tp = tun_find_unit(dev);
844 /* interface was "destroyed" already */
845 if (tp == NULL) {
846 error = ENXIO;
847 goto out_nolock;
850 /* Unlock until we've got the data */
851 simple_unlock(&tp->tun_lock);
852 splx(s);
854 ifp = &tp->tun_if;
856 TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
858 if (tp->tun_flags & TUN_PREPADDR) {
859 if (uio->uio_resid < sizeof(dst)) {
860 error = EIO;
861 goto out0;
863 error = uiomove((void *)&dst, sizeof(dst), uio);
864 if (dst.sa_len > sizeof(dst)) {
865 /* Duh.. */
866 char discard;
867 int n = dst.sa_len - sizeof(dst);
868 while (n--)
869 if ((error = uiomove(&discard, 1, uio)) != 0) {
870 goto out0;
873 } else if (tp->tun_flags & TUN_IFHEAD) {
874 if (uio->uio_resid < sizeof(family)){
875 error = EIO;
876 goto out0;
878 error = uiomove((void *)&family, sizeof(family), uio);
879 dst.sa_family = ntohl(family);
880 } else {
881 #ifdef INET
882 dst.sa_family = AF_INET;
883 #endif
886 if (uio->uio_resid > TUNMTU) {
887 TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
888 (unsigned long)uio->uio_resid);
889 error = EIO;
890 goto out0;
893 switch (dst.sa_family) {
894 #ifdef INET
895 case AF_INET:
896 ifq = &ipintrq;
897 isr = NETISR_IP;
898 break;
899 #endif
900 #ifdef INET6
901 case AF_INET6:
902 ifq = &ip6intrq;
903 isr = NETISR_IPV6;
904 break;
905 #endif
906 default:
907 error = EAFNOSUPPORT;
908 goto out0;
911 tlen = uio->uio_resid;
913 /* get a header mbuf */
914 MGETHDR(m, M_DONTWAIT, MT_DATA);
915 if (m == NULL) {
916 error = ENOBUFS;
917 goto out0;
919 mlen = MHLEN;
921 top = NULL;
922 mp = &top;
923 while (error == 0 && uio->uio_resid > 0) {
924 m->m_len = min(mlen, uio->uio_resid);
925 error = uiomove(mtod(m, void *), m->m_len, uio);
926 *mp = m;
927 mp = &m->m_next;
928 if (error == 0 && uio->uio_resid > 0) {
929 MGET(m, M_DONTWAIT, MT_DATA);
930 if (m == NULL) {
931 error = ENOBUFS;
932 break;
934 mlen = MLEN;
937 if (error) {
938 if (top != NULL)
939 m_freem (top);
940 ifp->if_ierrors++;
941 goto out0;
944 top->m_pkthdr.len = tlen;
945 top->m_pkthdr.rcvif = ifp;
947 #if NBPFILTER > 0
948 if (ifp->if_bpf)
949 bpf_mtap_af(ifp->if_bpf, dst.sa_family, top);
950 #endif
952 s = splnet();
953 simple_lock(&tp->tun_lock);
954 if ((tp->tun_flags & TUN_INITED) == 0) {
955 /* Interface was destroyed */
956 error = ENXIO;
957 goto out;
959 if (IF_QFULL(ifq)) {
960 IF_DROP(ifq);
961 ifp->if_collisions++;
962 m_freem(top);
963 error = ENOBUFS;
964 goto out;
967 IF_ENQUEUE(ifq, top);
968 ifp->if_ipackets++;
969 ifp->if_ibytes += tlen;
970 schednetisr(isr);
971 out:
972 simple_unlock(&tp->tun_lock);
973 out_nolock:
974 splx(s);
975 out0:
976 return (error);
979 #ifdef ALTQ
981 * Start packet transmission on the interface.
982 * when the interface queue is rate-limited by ALTQ or TBR,
983 * if_start is needed to drain packets from the queue in order
984 * to notify readers when outgoing packets become ready.
986 * Should be called at splnet.
988 static void
989 tunstart(struct ifnet *ifp)
991 struct tun_softc *tp = ifp->if_softc;
993 if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
994 return;
996 simple_lock(&tp->tun_lock);
997 if (!IF_IS_EMPTY(&ifp->if_snd)) {
998 if (tp->tun_flags & TUN_RWAIT) {
999 tp->tun_flags &= ~TUN_RWAIT;
1000 wakeup((void *)tp);
1002 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
1003 softint_schedule(tp->tun_osih);
1005 selnotify(&tp->tun_rsel, 0, 0);
1007 simple_unlock(&tp->tun_lock);
1009 #endif /* ALTQ */
1011 * tunpoll - the poll interface, this is only useful on reads
1012 * really. The write detect always returns true, write never blocks
1013 * anyway, it either accepts the packet or drops it.
1016 tunpoll(dev_t dev, int events, struct lwp *l)
1018 struct tun_softc *tp;
1019 struct ifnet *ifp;
1020 int s, revents = 0;
1022 s = splnet();
1023 tp = tun_find_unit(dev);
1025 /* interface was "destroyed" already */
1026 if (tp == NULL)
1027 goto out_nolock;
1029 ifp = &tp->tun_if;
1031 TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1033 if (events & (POLLIN | POLLRDNORM)) {
1034 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1035 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1036 ifp->if_snd.ifq_len);
1037 revents |= events & (POLLIN | POLLRDNORM);
1038 } else {
1039 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1040 selrecord(l, &tp->tun_rsel);
1044 if (events & (POLLOUT | POLLWRNORM))
1045 revents |= events & (POLLOUT | POLLWRNORM);
1047 simple_unlock(&tp->tun_lock);
1048 out_nolock:
1049 splx(s);
1050 return (revents);
1053 static void
1054 filt_tunrdetach(struct knote *kn)
1056 struct tun_softc *tp = kn->kn_hook;
1057 int s;
1059 s = splnet();
1060 SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1061 splx(s);
1064 static int
1065 filt_tunread(struct knote *kn, long hint)
1067 struct tun_softc *tp = kn->kn_hook;
1068 struct ifnet *ifp = &tp->tun_if;
1069 struct mbuf *m;
1070 int s;
1072 s = splnet();
1073 IF_POLL(&ifp->if_snd, m);
1074 if (m == NULL) {
1075 splx(s);
1076 return (0);
1079 for (kn->kn_data = 0; m != NULL; m = m->m_next)
1080 kn->kn_data += m->m_len;
1082 splx(s);
1083 return (1);
1086 static const struct filterops tunread_filtops =
1087 { 1, NULL, filt_tunrdetach, filt_tunread };
1089 static const struct filterops tun_seltrue_filtops =
1090 { 1, NULL, filt_tunrdetach, filt_seltrue };
1093 tunkqfilter(dev_t dev, struct knote *kn)
1095 struct tun_softc *tp;
1096 struct klist *klist;
1097 int rv = 0, s;
1099 s = splnet();
1100 tp = tun_find_unit(dev);
1101 if (tp == NULL)
1102 goto out_nolock;
1104 switch (kn->kn_filter) {
1105 case EVFILT_READ:
1106 klist = &tp->tun_rsel.sel_klist;
1107 kn->kn_fop = &tunread_filtops;
1108 break;
1110 case EVFILT_WRITE:
1111 klist = &tp->tun_rsel.sel_klist;
1112 kn->kn_fop = &tun_seltrue_filtops;
1113 break;
1115 default:
1116 rv = EINVAL;
1117 goto out;
1120 kn->kn_hook = tp;
1122 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1124 out:
1125 simple_unlock(&tp->tun_lock);
1126 out_nolock:
1127 splx(s);
1128 return (rv);