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