if_tun.c revision 1.92 1 /* $NetBSD: if_tun.c,v 1.92 2006/09/07 02:40:33 dogcow Exp $ */
2
3 /*
4 * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
5 * Nottingham University 1987.
6 *
7 * This source may be freely distributed, however I would be interested
8 * in any changes that are made.
9 *
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.
15 */
16
17 #include <sys/cdefs.h>
18 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.92 2006/09/07 02:40:33 dogcow Exp $");
19
20 #include "opt_inet.h"
21
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
39 #include <machine/cpu.h>
40
41 #include <net/if.h>
42 #include <net/if_types.h>
43 #include <net/netisr.h>
44 #include <net/route.h>
45
46
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
54
55
56 #include "bpfilter.h"
57 #if NBPFILTER > 0
58 #include <sys/time.h>
59 #include <net/bpf.h>
60 #endif
61
62 #include <net/if_tun.h>
63
64 #define TUNDEBUG if (tundebug) printf
65 int tundebug = 0;
66
67 extern int ifqmaxlen;
68 void tunattach(int);
69
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;
73
74 static int tun_ioctl(struct ifnet *, u_long, caddr_t);
75 static int tun_output(struct ifnet *, struct mbuf *, struct sockaddr *,
76 struct rtentry *rt);
77 static int tun_clone_create(struct if_clone *, int);
78 static int tun_clone_destroy(struct ifnet *);
79
80 static struct if_clone tun_cloner =
81 IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
82
83 static void tunattach0(struct tun_softc *);
84 static void tuninit(struct tun_softc *);
85 #ifdef ALTQ
86 static void tunstart(struct ifnet *);
87 #endif
88 static struct tun_softc *tun_find_unit(dev_t);
89 static struct tun_softc *tun_find_zunit(int);
90
91 static dev_type_open(tunopen);
92 static dev_type_close(tunclose);
93 static dev_type_read(tunread);
94 static dev_type_write(tunwrite);
95 static dev_type_ioctl(tunioctl);
96 static dev_type_poll(tunpoll);
97 static dev_type_kqfilter(tunkqfilter);
98
99 const struct cdevsw tun_cdevsw = {
100 tunopen, tunclose, tunread, tunwrite, tunioctl,
101 nostop, notty, tunpoll, nommap, tunkqfilter, D_OTHER,
102 };
103
104 void
105 tunattach(int unused)
106 {
107
108 simple_lock_init(&tun_softc_lock);
109 LIST_INIT(&tun_softc_list);
110 LIST_INIT(&tunz_softc_list);
111 if_clone_attach(&tun_cloner);
112 }
113
114 /*
115 * Find driver instance from dev_t.
116 * Call at splnet().
117 * Returns with tp locked (if found).
118 */
119 static struct tun_softc *
120 tun_find_unit(dev_t dev)
121 {
122 struct tun_softc *tp;
123 int unit = minor(dev);
124
125 simple_lock(&tun_softc_lock);
126 LIST_FOREACH(tp, &tun_softc_list, tun_list)
127 if (unit == tp->tun_unit)
128 break;
129 if (tp)
130 simple_lock(&tp->tun_lock);
131 simple_unlock(&tun_softc_lock);
132
133 return (tp);
134 }
135
136 /*
137 * Find zombie driver instance by unit number.
138 * Call at splnet().
139 * Remove tp from list and return it unlocked (if found).
140 */
141 static struct tun_softc *
142 tun_find_zunit(int unit)
143 {
144 struct tun_softc *tp;
145
146 simple_lock(&tun_softc_lock);
147 LIST_FOREACH(tp, &tunz_softc_list, tun_list)
148 if (unit == tp->tun_unit)
149 break;
150 if (tp)
151 LIST_REMOVE(tp, tun_list);
152 simple_unlock(&tun_softc_lock);
153 #ifdef DIAGNOSTIC
154 if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
155 printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
156 #endif
157
158 return (tp);
159 }
160
161 static int
162 tun_clone_create(struct if_clone *ifc, int unit)
163 {
164 struct tun_softc *tp;
165
166 if ((tp = tun_find_zunit(unit)) == NULL) {
167 /* Allocate a new instance */
168 tp = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
169 (void)memset(tp, 0, sizeof(struct tun_softc));
170
171 tp->tun_unit = unit;
172 simple_lock_init(&tp->tun_lock);
173 } else {
174 /* Revive tunnel instance; clear ifp part */
175 (void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
176 }
177
178 (void)snprintf(tp->tun_if.if_xname, sizeof(tp->tun_if.if_xname),
179 "%s%d", ifc->ifc_name, unit);
180 tunattach0(tp);
181 tp->tun_flags |= TUN_INITED;
182
183 simple_lock(&tun_softc_lock);
184 LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
185 simple_unlock(&tun_softc_lock);
186
187 return (0);
188 }
189
190 static void
191 tunattach0(struct tun_softc *tp)
192 {
193 struct ifnet *ifp;
194
195 ifp = &tp->tun_if;
196 ifp->if_softc = tp;
197 ifp->if_mtu = TUNMTU;
198 ifp->if_ioctl = tun_ioctl;
199 ifp->if_output = tun_output;
200 #ifdef ALTQ
201 ifp->if_start = tunstart;
202 #endif
203 ifp->if_flags = IFF_POINTOPOINT;
204 ifp->if_type = IFT_TUNNEL;
205 ifp->if_snd.ifq_maxlen = ifqmaxlen;
206 ifp->if_collisions = 0;
207 ifp->if_ierrors = 0;
208 ifp->if_oerrors = 0;
209 ifp->if_ipackets = 0;
210 ifp->if_opackets = 0;
211 ifp->if_ibytes = 0;
212 ifp->if_obytes = 0;
213 ifp->if_dlt = DLT_NULL;
214 IFQ_SET_READY(&ifp->if_snd);
215 if_attach(ifp);
216 if_alloc_sadl(ifp);
217 #if NBPFILTER > 0
218 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
219 #endif
220 }
221
222 static int
223 tun_clone_destroy(struct ifnet *ifp)
224 {
225 struct tun_softc *tp = (void *)ifp;
226 int s, zombie = 0;
227
228 s = splnet();
229 simple_lock(&tun_softc_lock);
230 simple_lock(&tp->tun_lock);
231 LIST_REMOVE(tp, tun_list);
232 if (tp->tun_flags & TUN_OPEN) {
233 /* Hang on to storage until last close */
234 zombie = 1;
235 tp->tun_flags &= ~TUN_INITED;
236 LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
237 }
238 simple_unlock(&tun_softc_lock);
239
240 IF_PURGE(&ifp->if_snd);
241 ifp->if_flags &= ~IFF_RUNNING;
242
243 if (tp->tun_flags & TUN_RWAIT) {
244 tp->tun_flags &= ~TUN_RWAIT;
245 wakeup((caddr_t)tp);
246 }
247 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
248 fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
249
250 selwakeup(&tp->tun_rsel);
251
252 simple_unlock(&tp->tun_lock);
253 splx(s);
254
255 #if NBPFILTER > 0
256 bpfdetach(ifp);
257 #endif
258 if_detach(ifp);
259
260 if (!zombie)
261 free(tp, M_DEVBUF);
262
263 return (0);
264 }
265
266 /*
267 * tunnel open - must be superuser & the device must be
268 * configured in
269 */
270 static int
271 tunopen(dev_t dev, int flag, int mode, struct lwp *l)
272 {
273 struct ifnet *ifp;
274 struct tun_softc *tp;
275 int s, error;
276
277 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
278 &l->l_acflag)) != 0)
279 return (error);
280
281 s = splnet();
282 tp = tun_find_unit(dev);
283
284 if (tp == NULL) {
285 (void)tun_clone_create(&tun_cloner, minor(dev));
286 tp = tun_find_unit(dev);
287 if (tp == NULL) {
288 error = ENXIO;
289 goto out_nolock;
290 }
291 }
292
293 if (tp->tun_flags & TUN_OPEN) {
294 error = EBUSY;
295 goto out;
296 }
297
298 ifp = &tp->tun_if;
299 tp->tun_flags |= TUN_OPEN;
300 TUNDEBUG("%s: open\n", ifp->if_xname);
301 out:
302 simple_unlock(&tp->tun_lock);
303 out_nolock:
304 splx(s);
305 return (error);
306 }
307
308 /*
309 * tunclose - close the device - mark i/f down & delete
310 * routing info
311 */
312 int
313 tunclose(dev_t dev, int flag, int mode, struct lwp *l)
314 {
315 int s;
316 struct tun_softc *tp;
317 struct ifnet *ifp;
318
319 s = splnet();
320 if ((tp = tun_find_zunit(minor(dev))) != NULL) {
321 /* interface was "destroyed" before the close */
322 free(tp, M_DEVBUF);
323 goto out_nolock;
324 }
325
326 if ((tp = tun_find_unit(dev)) == NULL)
327 goto out_nolock;
328
329 ifp = &tp->tun_if;
330
331 tp->tun_flags &= ~TUN_OPEN;
332
333 /*
334 * junk all pending output
335 */
336 IFQ_PURGE(&ifp->if_snd);
337
338 if (ifp->if_flags & IFF_UP) {
339 if_down(ifp);
340 if (ifp->if_flags & IFF_RUNNING) {
341 /* find internet addresses and delete routes */
342 struct ifaddr *ifa;
343 IFADDR_FOREACH(ifa, ifp) {
344 #if defined(INET) || defined(INET6)
345 if (ifa->ifa_addr->sa_family == AF_INET ||
346 ifa->ifa_addr->sa_family == AF_INET6) {
347 rtinit(ifa, (int)RTM_DELETE,
348 tp->tun_flags & TUN_DSTADDR
349 ? RTF_HOST
350 : 0);
351 }
352 #endif
353 }
354 }
355 }
356 tp->tun_pgid = 0;
357 selnotify(&tp->tun_rsel, 0);
358
359 TUNDEBUG ("%s: closed\n", ifp->if_xname);
360 simple_unlock(&tp->tun_lock);
361 out_nolock:
362 splx(s);
363 return (0);
364 }
365
366 /*
367 * Call at splnet() with tp locked.
368 */
369 static void
370 tuninit(struct tun_softc *tp)
371 {
372 struct ifnet *ifp = &tp->tun_if;
373 struct ifaddr *ifa;
374
375 TUNDEBUG("%s: tuninit\n", ifp->if_xname);
376
377 ifp->if_flags |= IFF_UP | IFF_RUNNING;
378
379 tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
380 IFADDR_FOREACH(ifa, ifp) {
381 #ifdef INET
382 if (ifa->ifa_addr->sa_family == AF_INET) {
383 struct sockaddr_in *sin;
384
385 sin = satosin(ifa->ifa_addr);
386 if (sin && sin->sin_addr.s_addr)
387 tp->tun_flags |= TUN_IASET;
388
389 if (ifp->if_flags & IFF_POINTOPOINT) {
390 sin = satosin(ifa->ifa_dstaddr);
391 if (sin && sin->sin_addr.s_addr)
392 tp->tun_flags |= TUN_DSTADDR;
393 }
394 }
395 #endif
396 #ifdef INET6
397 if (ifa->ifa_addr->sa_family == AF_INET6) {
398 struct sockaddr_in6 *sin;
399
400 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
401 if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
402 tp->tun_flags |= TUN_IASET;
403
404 if (ifp->if_flags & IFF_POINTOPOINT) {
405 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
406 if (sin &&
407 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
408 tp->tun_flags |= TUN_DSTADDR;
409 } else
410 tp->tun_flags &= ~TUN_DSTADDR;
411 }
412 #endif /* INET6 */
413 }
414
415 return;
416 }
417
418 /*
419 * Process an ioctl request.
420 */
421 static int
422 tun_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
423 {
424 int error = 0, s;
425 struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
426
427 s = splnet();
428 simple_lock(&tp->tun_lock);
429
430 switch (cmd) {
431 case SIOCSIFADDR:
432 tuninit(tp);
433 TUNDEBUG("%s: address set\n", ifp->if_xname);
434 break;
435 case SIOCSIFDSTADDR:
436 tuninit(tp);
437 TUNDEBUG("%s: destination address set\n", ifp->if_xname);
438 break;
439 case SIOCSIFBRDADDR:
440 TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
441 break;
442 case SIOCSIFMTU: {
443 struct ifreq *ifr = (struct ifreq *) data;
444 if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
445 error = EINVAL;
446 break;
447 }
448 TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
449 ifp->if_mtu = ifr->ifr_mtu;
450 break;
451 }
452 case SIOCADDMULTI:
453 case SIOCDELMULTI: {
454 struct ifreq *ifr = (struct ifreq *) data;
455 if (ifr == 0) {
456 error = EAFNOSUPPORT; /* XXX */
457 break;
458 }
459 switch (ifr->ifr_addr.sa_family) {
460 #ifdef INET
461 case AF_INET:
462 break;
463 #endif
464 #ifdef INET6
465 case AF_INET6:
466 break;
467 #endif
468 default:
469 error = EAFNOSUPPORT;
470 break;
471 }
472 break;
473 }
474 case SIOCSIFFLAGS:
475 break;
476 default:
477 error = EINVAL;
478 }
479
480 simple_unlock(&tp->tun_lock);
481 splx(s);
482 return (error);
483 }
484
485 /*
486 * tun_output - queue packets from higher level ready to put out.
487 */
488 static int
489 tun_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
490 struct rtentry *rt)
491 {
492 struct tun_softc *tp = ifp->if_softc;
493 int s;
494 int error;
495 #if defined(INET) || defined(INET6)
496 int mlen;
497 uint32_t *af;
498 #endif
499 ALTQ_DECL(struct altq_pktattr pktattr;)
500
501 s = splnet();
502 simple_lock(&tp->tun_lock);
503 TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
504
505 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
506 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
507 tp->tun_flags);
508 m_freem (m0);
509 error = EHOSTDOWN;
510 goto out;
511 }
512
513 /*
514 * if the queueing discipline needs packet classification,
515 * do it before prepending link headers.
516 */
517 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
518
519 #if NBPFILTER > 0
520 if (ifp->if_bpf)
521 bpf_mtap_af(ifp->if_bpf, dst->sa_family, m0);
522 #endif
523
524 switch(dst->sa_family) {
525 #ifdef INET6
526 case AF_INET6:
527 #endif
528 #ifdef INET
529 case AF_INET:
530 #endif
531 #if defined(INET) || defined(INET6)
532 if (tp->tun_flags & TUN_PREPADDR) {
533 /* Simple link-layer header */
534 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
535 if (m0 == NULL) {
536 IF_DROP(&ifp->if_snd);
537 error = ENOBUFS;
538 goto out;
539 }
540 bcopy(dst, mtod(m0, char *), dst->sa_len);
541 }
542
543 if (tp->tun_flags & TUN_IFHEAD) {
544 /* Prepend the address family */
545 M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
546 if (m0 == NULL) {
547 IF_DROP(&ifp->if_snd);
548 error = ENOBUFS;
549 goto out;
550 }
551 af = mtod(m0,uint32_t *);
552 *af = htonl(dst->sa_family);
553 } else {
554 #ifdef INET
555 if (dst->sa_family != AF_INET)
556 #endif
557 {
558 m_freem(m0);
559 error = EAFNOSUPPORT;
560 goto out;
561 }
562 }
563 /* FALLTHROUGH */
564 case AF_UNSPEC:
565 IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
566 if (error) {
567 ifp->if_collisions++;
568 error = EAFNOSUPPORT;
569 goto out;
570 }
571 mlen = m0->m_pkthdr.len;
572 ifp->if_opackets++;
573 ifp->if_obytes += mlen;
574 break;
575 #endif
576 default:
577 m_freem(m0);
578 error = EAFNOSUPPORT;
579 goto out;
580 }
581
582 if (tp->tun_flags & TUN_RWAIT) {
583 tp->tun_flags &= ~TUN_RWAIT;
584 wakeup((caddr_t)tp);
585 }
586 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
587 fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
588 NULL);
589
590 selnotify(&tp->tun_rsel, 0);
591 out:
592 simple_unlock(&tp->tun_lock);
593 splx(s);
594 return (0);
595 }
596
597 /*
598 * the cdevsw interface is now pretty minimal.
599 */
600 int
601 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
602 {
603 struct tun_softc *tp;
604 int s, error = 0;
605
606 s = splnet();
607 tp = tun_find_unit(dev);
608
609 /* interface was "destroyed" already */
610 if (tp == NULL) {
611 error = ENXIO;
612 goto out_nolock;
613 }
614
615 switch (cmd) {
616 case TUNSDEBUG:
617 tundebug = *(int *)data;
618 break;
619
620 case TUNGDEBUG:
621 *(int *)data = tundebug;
622 break;
623
624 case TUNSIFMODE:
625 switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
626 case IFF_POINTOPOINT:
627 case IFF_BROADCAST:
628 if (tp->tun_if.if_flags & IFF_UP) {
629 error = EBUSY;
630 goto out;
631 }
632 tp->tun_if.if_flags &=
633 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
634 tp->tun_if.if_flags |= *(int *)data;
635 break;
636 default:
637 error = EINVAL;
638 goto out;
639 }
640 break;
641
642 case TUNSLMODE:
643 if (*(int *)data) {
644 tp->tun_flags |= TUN_PREPADDR;
645 tp->tun_flags &= ~TUN_IFHEAD;
646 } else
647 tp->tun_flags &= ~TUN_PREPADDR;
648 break;
649
650 case TUNSIFHEAD:
651 if (*(int *)data) {
652 tp->tun_flags |= TUN_IFHEAD;
653 tp->tun_flags &= ~TUN_PREPADDR;
654 } else
655 tp->tun_flags &= ~TUN_IFHEAD;
656 break;
657
658 case TUNGIFHEAD:
659 *(int *)data = (tp->tun_flags & TUN_IFHEAD);
660 break;
661
662 case FIONBIO:
663 if (*(int *)data)
664 tp->tun_flags |= TUN_NBIO;
665 else
666 tp->tun_flags &= ~TUN_NBIO;
667 break;
668
669 case FIOASYNC:
670 if (*(int *)data)
671 tp->tun_flags |= TUN_ASYNC;
672 else
673 tp->tun_flags &= ~TUN_ASYNC;
674 break;
675
676 case FIONREAD:
677 if (tp->tun_if.if_snd.ifq_head)
678 *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
679 else
680 *(int *)data = 0;
681 break;
682
683 case TIOCSPGRP:
684 case FIOSETOWN:
685 error = fsetown(l->l_proc, &tp->tun_pgid, cmd, data);
686 break;
687
688 case TIOCGPGRP:
689 case FIOGETOWN:
690 error = fgetown(l->l_proc, tp->tun_pgid, cmd, data);
691 break;
692
693 default:
694 error = ENOTTY;
695 }
696
697 out:
698 simple_unlock(&tp->tun_lock);
699 out_nolock:
700 splx(s);
701 return (error);
702 }
703
704 /*
705 * The cdevsw read interface - reads a packet at a time, or at
706 * least as much of a packet as can be read.
707 */
708 int
709 tunread(dev_t dev, struct uio *uio, int ioflag)
710 {
711 struct tun_softc *tp;
712 struct ifnet *ifp;
713 struct mbuf *m, *m0;
714 int error = 0, len, s, index;
715
716 s = splnet();
717 tp = tun_find_unit(dev);
718
719 /* interface was "destroyed" already */
720 if (tp == NULL) {
721 error = ENXIO;
722 goto out_nolock;
723 }
724
725 index = tp->tun_if.if_index;
726 ifp = &tp->tun_if;
727
728 TUNDEBUG ("%s: read\n", ifp->if_xname);
729 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
730 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
731 error = EHOSTDOWN;
732 goto out;
733 }
734
735 tp->tun_flags &= ~TUN_RWAIT;
736
737 do {
738 IFQ_DEQUEUE(&ifp->if_snd, m0);
739 if (m0 == 0) {
740 if (tp->tun_flags & TUN_NBIO) {
741 error = EWOULDBLOCK;
742 goto out;
743 }
744 tp->tun_flags |= TUN_RWAIT;
745 if (ltsleep((caddr_t)tp, PZERO|PCATCH|PNORELOCK,
746 "tunread", 0, &tp->tun_lock) != 0) {
747 error = EINTR;
748 goto out_nolock;
749 } else {
750 /*
751 * Maybe the interface was destroyed while
752 * we were sleeping, so let's ensure that
753 * we're looking at the same (valid) tun
754 * interface before looping.
755 */
756 tp = tun_find_unit(dev);
757 if (tp == NULL) {
758 error = ENXIO;
759 goto out_nolock;
760 }
761 if (tp->tun_if.if_index != index) {
762 error = ENXIO;
763 goto out;
764 }
765 }
766 }
767 } while (m0 == 0);
768
769 simple_unlock(&tp->tun_lock);
770 splx(s);
771
772 /* Copy the mbuf chain */
773 while (m0 && uio->uio_resid > 0 && error == 0) {
774 len = min(uio->uio_resid, m0->m_len);
775 if (len != 0)
776 error = uiomove(mtod(m0, caddr_t), len, uio);
777 MFREE(m0, m);
778 m0 = m;
779 }
780
781 if (m0) {
782 TUNDEBUG("Dropping mbuf\n");
783 m_freem(m0);
784 }
785 if (error)
786 ifp->if_ierrors++;
787
788 return (error);
789
790 out:
791 simple_unlock(&tp->tun_lock);
792 out_nolock:
793 splx(s);
794 return (error);
795 }
796
797 /*
798 * the cdevsw write interface - an atomic write is a packet - or else!
799 */
800 int
801 tunwrite(dev_t dev, struct uio *uio, int ioflag)
802 {
803 struct tun_softc *tp;
804 struct ifnet *ifp;
805 struct mbuf *top, **mp, *m;
806 struct ifqueue *ifq;
807 struct sockaddr dst;
808 int isr, error = 0, s, tlen, mlen;
809 uint32_t family;
810
811 s = splnet();
812 tp = tun_find_unit(dev);
813
814 /* interface was "destroyed" already */
815 if (tp == NULL) {
816 error = ENXIO;
817 goto out_nolock;
818 }
819
820 /* Unlock until we've got the data */
821 simple_unlock(&tp->tun_lock);
822 splx(s);
823
824 ifp = &tp->tun_if;
825
826 TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
827
828 if (tp->tun_flags & TUN_PREPADDR) {
829 if (uio->uio_resid < sizeof(dst)) {
830 error = EIO;
831 goto out0;
832 }
833 error = uiomove((caddr_t)&dst, sizeof(dst), uio);
834 if (dst.sa_len > sizeof(dst)) {
835 /* Duh.. */
836 char discard;
837 int n = dst.sa_len - sizeof(dst);
838 while (n--)
839 if ((error = uiomove(&discard, 1, uio)) != 0) {
840 goto out0;
841 }
842 }
843 } else if (tp->tun_flags & TUN_IFHEAD) {
844 if (uio->uio_resid < sizeof(family)){
845 error = EIO;
846 goto out0;
847 }
848 error = uiomove((caddr_t)&family, sizeof(family), uio);
849 dst.sa_family = ntohl(family);
850 } else {
851 #ifdef INET
852 dst.sa_family = AF_INET;
853 #endif
854 }
855
856 if (uio->uio_resid > TUNMTU) {
857 TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
858 (unsigned long)uio->uio_resid);
859 error = EIO;
860 goto out0;
861 }
862
863 switch (dst.sa_family) {
864 #ifdef INET
865 case AF_INET:
866 ifq = &ipintrq;
867 isr = NETISR_IP;
868 break;
869 #endif
870 #ifdef INET6
871 case AF_INET6:
872 ifq = &ip6intrq;
873 isr = NETISR_IPV6;
874 break;
875 #endif
876 default:
877 error = EAFNOSUPPORT;
878 goto out0;
879 }
880
881 tlen = uio->uio_resid;
882
883 /* get a header mbuf */
884 MGETHDR(m, M_DONTWAIT, MT_DATA);
885 if (m == NULL) {
886 error = ENOBUFS;
887 goto out0;
888 }
889 mlen = MHLEN;
890
891 top = NULL;
892 mp = ⊤
893 while (error == 0 && uio->uio_resid > 0) {
894 m->m_len = min(mlen, uio->uio_resid);
895 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
896 *mp = m;
897 mp = &m->m_next;
898 if (error == 0 && uio->uio_resid > 0) {
899 MGET(m, M_DONTWAIT, MT_DATA);
900 if (m == NULL) {
901 error = ENOBUFS;
902 break;
903 }
904 mlen = MLEN;
905 }
906 }
907 if (error) {
908 if (top != NULL)
909 m_freem (top);
910 ifp->if_ierrors++;
911 goto out0;
912 }
913
914 top->m_pkthdr.len = tlen;
915 top->m_pkthdr.rcvif = ifp;
916
917 #if NBPFILTER > 0
918 if (ifp->if_bpf)
919 bpf_mtap_af(ifp->if_bpf, dst.sa_family, top);
920 #endif
921
922 s = splnet();
923 simple_lock(&tp->tun_lock);
924 if ((tp->tun_flags & TUN_INITED) == 0) {
925 /* Interface was destroyed */
926 error = ENXIO;
927 goto out;
928 }
929 if (IF_QFULL(ifq)) {
930 IF_DROP(ifq);
931 ifp->if_collisions++;
932 m_freem(top);
933 error = ENOBUFS;
934 goto out;
935 }
936
937 IF_ENQUEUE(ifq, top);
938 ifp->if_ipackets++;
939 ifp->if_ibytes += tlen;
940 schednetisr(isr);
941 out:
942 simple_unlock(&tp->tun_lock);
943 out_nolock:
944 splx(s);
945 out0:
946 return (error);
947 }
948
949 #ifdef ALTQ
950 /*
951 * Start packet transmission on the interface.
952 * when the interface queue is rate-limited by ALTQ or TBR,
953 * if_start is needed to drain packets from the queue in order
954 * to notify readers when outgoing packets become ready.
955 *
956 * Should be called at splnet.
957 */
958 static void
959 tunstart(struct ifnet *ifp)
960 {
961 struct tun_softc *tp = ifp->if_softc;
962
963 if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
964 return;
965
966 simple_lock(&tp->tun_lock);
967 if (!IF_IS_EMPTY(&ifp->if_snd)) {
968 if (tp->tun_flags & TUN_RWAIT) {
969 tp->tun_flags &= ~TUN_RWAIT;
970 wakeup((caddr_t)tp);
971 }
972 if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
973 fownsignal(tp->tun_pgid, SIGIO, POLL_OUT,
974 POLLOUT|POLLWRNORM, NULL);
975
976 selwakeup(&tp->tun_rsel);
977 }
978 simple_unlock(&tp->tun_lock);
979 }
980 #endif /* ALTQ */
981 /*
982 * tunpoll - the poll interface, this is only useful on reads
983 * really. The write detect always returns true, write never blocks
984 * anyway, it either accepts the packet or drops it.
985 */
986 int
987 tunpoll(dev_t dev, int events, struct lwp *l)
988 {
989 struct tun_softc *tp;
990 struct ifnet *ifp;
991 int s, revents = 0;
992
993 s = splnet();
994 tp = tun_find_unit(dev);
995
996 /* interface was "destroyed" already */
997 if (tp == NULL)
998 goto out_nolock;
999
1000 ifp = &tp->tun_if;
1001
1002 TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1003
1004 if (events & (POLLIN | POLLRDNORM)) {
1005 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1006 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1007 ifp->if_snd.ifq_len);
1008 revents |= events & (POLLIN | POLLRDNORM);
1009 } else {
1010 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1011 selrecord(l, &tp->tun_rsel);
1012 }
1013 }
1014
1015 if (events & (POLLOUT | POLLWRNORM))
1016 revents |= events & (POLLOUT | POLLWRNORM);
1017
1018 simple_unlock(&tp->tun_lock);
1019 out_nolock:
1020 splx(s);
1021 return (revents);
1022 }
1023
1024 static void
1025 filt_tunrdetach(struct knote *kn)
1026 {
1027 struct tun_softc *tp = kn->kn_hook;
1028 int s;
1029
1030 s = splnet();
1031 SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1032 splx(s);
1033 }
1034
1035 static int
1036 filt_tunread(struct knote *kn, long hint)
1037 {
1038 struct tun_softc *tp = kn->kn_hook;
1039 struct ifnet *ifp = &tp->tun_if;
1040 struct mbuf *m;
1041 int s;
1042
1043 s = splnet();
1044 IF_POLL(&ifp->if_snd, m);
1045 if (m == NULL) {
1046 splx(s);
1047 return (0);
1048 }
1049
1050 for (kn->kn_data = 0; m != NULL; m = m->m_next)
1051 kn->kn_data += m->m_len;
1052
1053 splx(s);
1054 return (1);
1055 }
1056
1057 static const struct filterops tunread_filtops =
1058 { 1, NULL, filt_tunrdetach, filt_tunread };
1059
1060 static const struct filterops tun_seltrue_filtops =
1061 { 1, NULL, filt_tunrdetach, filt_seltrue };
1062
1063 int
1064 tunkqfilter(dev_t dev, struct knote *kn)
1065 {
1066 struct tun_softc *tp;
1067 struct klist *klist;
1068 int rv = 0, s;
1069
1070 s = splnet();
1071 tp = tun_find_unit(dev);
1072 if (tp == NULL)
1073 goto out_nolock;
1074
1075 switch (kn->kn_filter) {
1076 case EVFILT_READ:
1077 klist = &tp->tun_rsel.sel_klist;
1078 kn->kn_fop = &tunread_filtops;
1079 break;
1080
1081 case EVFILT_WRITE:
1082 klist = &tp->tun_rsel.sel_klist;
1083 kn->kn_fop = &tun_seltrue_filtops;
1084 break;
1085
1086 default:
1087 rv = 1;
1088 goto out;
1089 }
1090
1091 kn->kn_hook = tp;
1092
1093 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1094
1095 out:
1096 simple_unlock(&tp->tun_lock);
1097 out_nolock:
1098 splx(s);
1099 return (rv);
1100 }
1101