FreeBSD/Linux Kernel Cross Reference
sys/net/if_tun.c
1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1988, Julian Onions <jpo@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 it's
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 * $FreeBSD: releng/7.4/sys/net/if_tun.c 213895 2010-10-15 15:06:32Z bz $
17 */
18
19 #include "opt_atalk.h"
20 #include "opt_inet.h"
21 #include "opt_inet6.h"
22 #include "opt_ipx.h"
23 #include "opt_mac.h"
24
25 #include <sys/param.h>
26 #include <sys/priv.h>
27 #include <sys/proc.h>
28 #include <sys/systm.h>
29 #include <sys/mbuf.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
32 #include <sys/fcntl.h>
33 #include <sys/filio.h>
34 #include <sys/sockio.h>
35 #include <sys/ttycom.h>
36 #include <sys/poll.h>
37 #include <sys/selinfo.h>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/conf.h>
43 #include <sys/uio.h>
44 #include <sys/malloc.h>
45 #include <sys/random.h>
46
47 #include <net/if.h>
48 #include <net/if_clone.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
52 #ifdef INET
53 #include <netinet/in.h>
54 #endif
55 #include <net/bpf.h>
56 #include <net/if_tun.h>
57
58 #include <sys/queue.h>
59 #include <sys/condvar.h>
60
61 #include <security/mac/mac_framework.h>
62
63 /*
64 * tun_list is protected by global tunmtx. Other mutable fields are
65 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
66 * static for the duration of a tunnel interface.
67 */
68 struct tun_softc {
69 TAILQ_ENTRY(tun_softc) tun_list;
70 struct cdev *tun_dev;
71 u_short tun_flags; /* misc flags */
72 #define TUN_OPEN 0x0001
73 #define TUN_INITED 0x0002
74 #define TUN_RCOLL 0x0004
75 #define TUN_IASET 0x0008
76 #define TUN_DSTADDR 0x0010
77 #define TUN_LMODE 0x0020
78 #define TUN_RWAIT 0x0040
79 #define TUN_ASYNC 0x0080
80 #define TUN_IFHEAD 0x0100
81
82 #define TUN_READY (TUN_OPEN | TUN_INITED)
83
84 /*
85 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
86 * actually needed? Can we just return EBUSY if already open?
87 * Problem is that this involved inherent races when a tun device
88 * is handed off from one process to another, as opposed to just
89 * being slightly stale informationally.
90 */
91 pid_t tun_pid; /* owning pid */
92 struct ifnet *tun_ifp; /* the interface */
93 struct sigio *tun_sigio; /* information for async I/O */
94 struct selinfo tun_rsel; /* read select */
95 struct mtx tun_mtx; /* protect mutable softc fields */
96 struct cv tun_cv; /* protect against ref'd dev destroy */
97 };
98 #define TUN2IFP(sc) ((sc)->tun_ifp)
99
100 #define TUNDEBUG if (tundebug) if_printf
101 #define TUNNAME "tun"
102
103 /*
104 * All mutable global variables in if_tun are locked using tunmtx, with
105 * the exception of tundebug, which is used unlocked, and tunclones,
106 * which is static after setup.
107 */
108 static struct mtx tunmtx;
109 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
110 static int tundebug = 0;
111 static int tundclone = 1;
112 static struct clonedevs *tunclones;
113 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
114 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
115
116 SYSCTL_DECL(_net_link);
117 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
118 "IP tunnel software network interface.");
119 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
120 "Enable legacy devfs interface creation.");
121
122 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
123
124 static void tunclone(void *arg, struct ucred *cred, char *name,
125 int namelen, struct cdev **dev);
126 static void tuncreate(const char *name, struct cdev *dev);
127 static int tunifioctl(struct ifnet *, u_long, caddr_t);
128 static int tuninit(struct ifnet *);
129 static int tunmodevent(module_t, int, void *);
130 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
131 struct rtentry *rt);
132 static void tunstart(struct ifnet *);
133
134 static int tun_clone_create(struct if_clone *, int, caddr_t);
135 static void tun_clone_destroy(struct ifnet *);
136
137 IFC_SIMPLE_DECLARE(tun, 0);
138
139 static d_open_t tunopen;
140 static d_close_t tunclose;
141 static d_read_t tunread;
142 static d_write_t tunwrite;
143 static d_ioctl_t tunioctl;
144 static d_poll_t tunpoll;
145 static d_kqfilter_t tunkqfilter;
146
147 static int tunkqread(struct knote *, long);
148 static int tunkqwrite(struct knote *, long);
149 static void tunkqdetach(struct knote *);
150
151 static struct filterops tun_read_filterops = {
152 .f_isfd = 1,
153 .f_attach = NULL,
154 .f_detach = tunkqdetach,
155 .f_event = tunkqread,
156 };
157
158 static struct filterops tun_write_filterops = {
159 .f_isfd = 1,
160 .f_attach = NULL,
161 .f_detach = tunkqdetach,
162 .f_event = tunkqwrite,
163 };
164
165 static struct cdevsw tun_cdevsw = {
166 .d_version = D_VERSION,
167 .d_flags = D_PSEUDO,
168 .d_open = tunopen,
169 .d_close = tunclose,
170 .d_read = tunread,
171 .d_write = tunwrite,
172 .d_ioctl = tunioctl,
173 .d_poll = tunpoll,
174 .d_kqfilter = tunkqfilter,
175 .d_name = TUNNAME,
176 };
177
178 static int
179 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
180 {
181 struct cdev *dev;
182 int i;
183
184 /* find any existing device, or allocate new unit number */
185 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
186 if (i) {
187 /* No preexisting struct cdev *, create one */
188 dev = make_dev(&tun_cdevsw, unit2minor(unit),
189 UID_UUCP, GID_DIALER, 0600, "%s%d", ifc->ifc_name, unit);
190 }
191 tuncreate(ifc->ifc_name, dev);
192
193 return (0);
194 }
195
196 static void
197 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
198 struct cdev **dev)
199 {
200 char devname[SPECNAMELEN + 1];
201 int u, i, append_unit;
202
203 if (*dev != NULL)
204 return;
205
206 /*
207 * If tun cloning is enabled, only the superuser can create an
208 * interface.
209 */
210 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
211 return;
212
213 if (strcmp(name, TUNNAME) == 0) {
214 u = -1;
215 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
216 return; /* Don't recognise the name */
217 if (u != -1 && u > IF_MAXUNIT)
218 return; /* Unit number too high */
219
220 if (u == -1)
221 append_unit = 1;
222 else
223 append_unit = 0;
224
225 /* find any existing device, or allocate new unit number */
226 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
227 if (i) {
228 if (append_unit) {
229 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
230 u);
231 name = devname;
232 }
233 /* No preexisting struct cdev *, create one */
234 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, unit2minor(u),
235 cred, UID_UUCP, GID_DIALER, 0600, "%s", name);
236 }
237
238 if_clone_create(name, namelen, NULL);
239 }
240
241 static void
242 tun_destroy(struct tun_softc *tp)
243 {
244 struct cdev *dev;
245
246 /* Unlocked read. */
247 mtx_lock(&tp->tun_mtx);
248 if ((tp->tun_flags & TUN_OPEN) != 0)
249 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
250 else
251 mtx_unlock(&tp->tun_mtx);
252
253 dev = tp->tun_dev;
254 bpfdetach(TUN2IFP(tp));
255 if_detach(TUN2IFP(tp));
256 if_free(TUN2IFP(tp));
257 destroy_dev(dev);
258 knlist_destroy(&tp->tun_rsel.si_note);
259 mtx_destroy(&tp->tun_mtx);
260 cv_destroy(&tp->tun_cv);
261 free(tp, M_TUN);
262 }
263
264 static void
265 tun_clone_destroy(struct ifnet *ifp)
266 {
267 struct tun_softc *tp = ifp->if_softc;
268
269 mtx_lock(&tunmtx);
270 TAILQ_REMOVE(&tunhead, tp, tun_list);
271 mtx_unlock(&tunmtx);
272 tun_destroy(tp);
273 }
274
275 static int
276 tunmodevent(module_t mod, int type, void *data)
277 {
278 static eventhandler_tag tag;
279 struct tun_softc *tp;
280
281 switch (type) {
282 case MOD_LOAD:
283 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
284 clone_setup(&tunclones);
285 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
286 if (tag == NULL)
287 return (ENOMEM);
288 if_clone_attach(&tun_cloner);
289 break;
290 case MOD_UNLOAD:
291 if_clone_detach(&tun_cloner);
292 EVENTHANDLER_DEREGISTER(dev_clone, tag);
293 drain_dev_clone_events();
294
295 mtx_lock(&tunmtx);
296 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
297 TAILQ_REMOVE(&tunhead, tp, tun_list);
298 mtx_unlock(&tunmtx);
299 tun_destroy(tp);
300 mtx_lock(&tunmtx);
301 }
302 mtx_unlock(&tunmtx);
303 clone_cleanup(&tunclones);
304 mtx_destroy(&tunmtx);
305 break;
306 default:
307 return EOPNOTSUPP;
308 }
309 return 0;
310 }
311
312 static moduledata_t tun_mod = {
313 "if_tun",
314 tunmodevent,
315 0
316 };
317
318 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
319
320 static void
321 tunstart(struct ifnet *ifp)
322 {
323 struct tun_softc *tp = ifp->if_softc;
324 struct mbuf *m;
325
326 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
327 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
328 IFQ_LOCK(&ifp->if_snd);
329 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
330 if (m == NULL) {
331 IFQ_UNLOCK(&ifp->if_snd);
332 return;
333 }
334 IFQ_UNLOCK(&ifp->if_snd);
335 }
336
337 mtx_lock(&tp->tun_mtx);
338 if (tp->tun_flags & TUN_RWAIT) {
339 tp->tun_flags &= ~TUN_RWAIT;
340 wakeup(tp);
341 }
342 selwakeuppri(&tp->tun_rsel, PZERO + 1);
343 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
344 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
345 mtx_unlock(&tp->tun_mtx);
346 pgsigio(&tp->tun_sigio, SIGIO, 0);
347 } else
348 mtx_unlock(&tp->tun_mtx);
349 }
350
351 /* XXX: should return an error code so it can fail. */
352 static void
353 tuncreate(const char *name, struct cdev *dev)
354 {
355 struct tun_softc *sc;
356 struct ifnet *ifp;
357
358 dev->si_flags &= ~SI_CHEAPCLONE;
359
360 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
361 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
362 cv_init(&sc->tun_cv, "tun_condvar");
363 sc->tun_flags = TUN_INITED;
364 sc->tun_dev = dev;
365 mtx_lock(&tunmtx);
366 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
367 mtx_unlock(&tunmtx);
368
369 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
370 if (ifp == NULL)
371 panic("%s%d: failed to if_alloc() interface.\n",
372 name, dev2unit(dev));
373 if_initname(ifp, name, dev2unit(dev));
374 ifp->if_mtu = TUNMTU;
375 ifp->if_ioctl = tunifioctl;
376 ifp->if_output = tunoutput;
377 ifp->if_start = tunstart;
378 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
379 ifp->if_softc = sc;
380 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
381 ifp->if_snd.ifq_drv_maxlen = 0;
382 IFQ_SET_READY(&ifp->if_snd);
383 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
384
385 if_attach(ifp);
386 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
387 dev->si_drv1 = sc;
388 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
389 ifp->if_xname, minor(dev));
390 }
391
392 static int
393 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
394 {
395 struct ifnet *ifp;
396 struct tun_softc *tp;
397
398 /*
399 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
400 * synchronization.
401 */
402 tp = dev->si_drv1;
403 if (!tp) {
404 tuncreate(TUNNAME, dev);
405 tp = dev->si_drv1;
406 }
407
408 /*
409 * XXXRW: This use of tun_pid is subject to error due to the
410 * fact that a reference to the tunnel can live beyond the
411 * death of the process that created it. Can we replace this
412 * with a simple busy flag?
413 */
414 mtx_lock(&tp->tun_mtx);
415 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
416 mtx_unlock(&tp->tun_mtx);
417 return (EBUSY);
418 }
419 tp->tun_pid = td->td_proc->p_pid;
420
421 tp->tun_flags |= TUN_OPEN;
422 ifp = TUN2IFP(tp);
423 if_link_state_change(ifp, LINK_STATE_UP);
424 TUNDEBUG(ifp, "open\n");
425 mtx_unlock(&tp->tun_mtx);
426
427 return (0);
428 }
429
430 /*
431 * tunclose - close the device - mark i/f down & delete
432 * routing info
433 */
434 static int
435 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
436 {
437 struct tun_softc *tp;
438 struct ifnet *ifp;
439
440 tp = dev->si_drv1;
441 ifp = TUN2IFP(tp);
442
443 mtx_lock(&tp->tun_mtx);
444 tp->tun_flags &= ~TUN_OPEN;
445 tp->tun_pid = 0;
446
447 /*
448 * junk all pending output
449 */
450 IFQ_PURGE(&ifp->if_snd);
451
452 if (ifp->if_flags & IFF_UP) {
453 mtx_unlock(&tp->tun_mtx);
454 if_down(ifp);
455 mtx_lock(&tp->tun_mtx);
456 }
457
458 /* Delete all addresses and routes which reference this interface. */
459 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
460 struct ifaddr *ifa;
461
462 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
463 mtx_unlock(&tp->tun_mtx);
464 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
465 /* deal w/IPv4 PtP destination; unlocked read */
466 if (ifa->ifa_addr->sa_family == AF_INET) {
467 rtinit(ifa, (int)RTM_DELETE,
468 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
469 } else {
470 rtinit(ifa, (int)RTM_DELETE, 0);
471 }
472 }
473 if_purgeaddrs(ifp);
474 mtx_lock(&tp->tun_mtx);
475 }
476 if_link_state_change(ifp, LINK_STATE_DOWN);
477
478 funsetown(&tp->tun_sigio);
479 selwakeuppri(&tp->tun_rsel, PZERO + 1);
480 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
481 TUNDEBUG (ifp, "closed\n");
482
483 cv_broadcast(&tp->tun_cv);
484 mtx_unlock(&tp->tun_mtx);
485 return (0);
486 }
487
488 static int
489 tuninit(struct ifnet *ifp)
490 {
491 struct tun_softc *tp = ifp->if_softc;
492 #ifdef INET
493 struct ifaddr *ifa;
494 #endif
495 int error = 0;
496
497 TUNDEBUG(ifp, "tuninit\n");
498
499 mtx_lock(&tp->tun_mtx);
500 ifp->if_flags |= IFF_UP;
501 ifp->if_drv_flags |= IFF_DRV_RUNNING;
502 getmicrotime(&ifp->if_lastchange);
503
504 #ifdef INET
505 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
506 if (ifa->ifa_addr->sa_family == AF_INET) {
507 struct sockaddr_in *si;
508
509 si = (struct sockaddr_in *)ifa->ifa_addr;
510 if (si->sin_addr.s_addr)
511 tp->tun_flags |= TUN_IASET;
512
513 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
514 if (si && si->sin_addr.s_addr)
515 tp->tun_flags |= TUN_DSTADDR;
516 }
517 }
518 #endif
519 mtx_unlock(&tp->tun_mtx);
520 return (error);
521 }
522
523 /*
524 * Process an ioctl request.
525 */
526 static int
527 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
528 {
529 struct ifreq *ifr = (struct ifreq *)data;
530 struct tun_softc *tp = ifp->if_softc;
531 struct ifstat *ifs;
532 int error = 0;
533
534 switch(cmd) {
535 case SIOCGIFSTATUS:
536 ifs = (struct ifstat *)data;
537 mtx_lock(&tp->tun_mtx);
538 if (tp->tun_pid)
539 sprintf(ifs->ascii + strlen(ifs->ascii),
540 "\tOpened by PID %d\n", tp->tun_pid);
541 mtx_unlock(&tp->tun_mtx);
542 break;
543 case SIOCSIFADDR:
544 error = tuninit(ifp);
545 TUNDEBUG(ifp, "address set, error=%d\n", error);
546 break;
547 case SIOCSIFDSTADDR:
548 error = tuninit(ifp);
549 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
550 break;
551 case SIOCSIFMTU:
552 ifp->if_mtu = ifr->ifr_mtu;
553 TUNDEBUG(ifp, "mtu set\n");
554 break;
555 case SIOCSIFFLAGS:
556 case SIOCADDMULTI:
557 case SIOCDELMULTI:
558 break;
559 default:
560 error = EINVAL;
561 }
562 return (error);
563 }
564
565 /*
566 * tunoutput - queue packets from higher level ready to put out.
567 */
568 static int
569 tunoutput(
570 struct ifnet *ifp,
571 struct mbuf *m0,
572 struct sockaddr *dst,
573 struct rtentry *rt)
574 {
575 struct tun_softc *tp = ifp->if_softc;
576 u_short cached_tun_flags;
577 int error;
578 u_int32_t af;
579
580 TUNDEBUG (ifp, "tunoutput\n");
581
582 #ifdef MAC
583 error = mac_check_ifnet_transmit(ifp, m0);
584 if (error) {
585 m_freem(m0);
586 return (error);
587 }
588 #endif
589
590 /* Could be unlocked read? */
591 mtx_lock(&tp->tun_mtx);
592 cached_tun_flags = tp->tun_flags;
593 mtx_unlock(&tp->tun_mtx);
594 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
595 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
596 m_freem (m0);
597 return (EHOSTDOWN);
598 }
599
600 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
601 m_freem (m0);
602 return (EHOSTDOWN);
603 }
604
605 /* BPF writes need to be handled specially. */
606 if (dst->sa_family == AF_UNSPEC) {
607 bcopy(dst->sa_data, &af, sizeof(af));
608 dst->sa_family = af;
609 }
610
611 if (bpf_peers_present(ifp->if_bpf)) {
612 af = dst->sa_family;
613 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
614 }
615
616 /* prepend sockaddr? this may abort if the mbuf allocation fails */
617 if (cached_tun_flags & TUN_LMODE) {
618 /* allocate space for sockaddr */
619 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
620
621 /* if allocation failed drop packet */
622 if (m0 == NULL) {
623 ifp->if_iqdrops++;
624 ifp->if_oerrors++;
625 return (ENOBUFS);
626 } else {
627 bcopy(dst, m0->m_data, dst->sa_len);
628 }
629 }
630
631 if (cached_tun_flags & TUN_IFHEAD) {
632 /* Prepend the address family */
633 M_PREPEND(m0, 4, M_DONTWAIT);
634
635 /* if allocation failed drop packet */
636 if (m0 == NULL) {
637 ifp->if_iqdrops++;
638 ifp->if_oerrors++;
639 return (ENOBUFS);
640 } else
641 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
642 } else {
643 #ifdef INET
644 if (dst->sa_family != AF_INET)
645 #endif
646 {
647 m_freem(m0);
648 return (EAFNOSUPPORT);
649 }
650 }
651
652 IFQ_HANDOFF(ifp, m0, error);
653 if (error) {
654 ifp->if_collisions++;
655 return (ENOBUFS);
656 }
657 ifp->if_opackets++;
658 return (0);
659 }
660
661 /*
662 * the cdevsw interface is now pretty minimal.
663 */
664 static int
665 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
666 {
667 int error;
668 struct tun_softc *tp = dev->si_drv1;
669 struct tuninfo *tunp;
670
671 switch (cmd) {
672 case TUNSIFINFO:
673 tunp = (struct tuninfo *)data;
674 if (tunp->mtu < IF_MINMTU)
675 return (EINVAL);
676 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
677 error = priv_check(td, PRIV_NET_SETIFMTU);
678 if (error)
679 return (error);
680 }
681 mtx_lock(&tp->tun_mtx);
682 TUN2IFP(tp)->if_mtu = tunp->mtu;
683 TUN2IFP(tp)->if_type = tunp->type;
684 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
685 mtx_unlock(&tp->tun_mtx);
686 break;
687 case TUNGIFINFO:
688 tunp = (struct tuninfo *)data;
689 mtx_lock(&tp->tun_mtx);
690 tunp->mtu = TUN2IFP(tp)->if_mtu;
691 tunp->type = TUN2IFP(tp)->if_type;
692 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
693 mtx_unlock(&tp->tun_mtx);
694 break;
695 case TUNSDEBUG:
696 tundebug = *(int *)data;
697 break;
698 case TUNGDEBUG:
699 *(int *)data = tundebug;
700 break;
701 case TUNSLMODE:
702 mtx_lock(&tp->tun_mtx);
703 if (*(int *)data) {
704 tp->tun_flags |= TUN_LMODE;
705 tp->tun_flags &= ~TUN_IFHEAD;
706 } else
707 tp->tun_flags &= ~TUN_LMODE;
708 mtx_unlock(&tp->tun_mtx);
709 break;
710 case TUNSIFHEAD:
711 mtx_lock(&tp->tun_mtx);
712 if (*(int *)data) {
713 tp->tun_flags |= TUN_IFHEAD;
714 tp->tun_flags &= ~TUN_LMODE;
715 } else
716 tp->tun_flags &= ~TUN_IFHEAD;
717 mtx_unlock(&tp->tun_mtx);
718 break;
719 case TUNGIFHEAD:
720 mtx_lock(&tp->tun_mtx);
721 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
722 mtx_unlock(&tp->tun_mtx);
723 break;
724 case TUNSIFMODE:
725 /* deny this if UP */
726 if (TUN2IFP(tp)->if_flags & IFF_UP)
727 return(EBUSY);
728
729 switch (*(int *)data & ~IFF_MULTICAST) {
730 case IFF_POINTOPOINT:
731 case IFF_BROADCAST:
732 mtx_lock(&tp->tun_mtx);
733 TUN2IFP(tp)->if_flags &=
734 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
735 TUN2IFP(tp)->if_flags |= *(int *)data;
736 mtx_unlock(&tp->tun_mtx);
737 break;
738 default:
739 return(EINVAL);
740 }
741 break;
742 case TUNSIFPID:
743 mtx_lock(&tp->tun_mtx);
744 tp->tun_pid = curthread->td_proc->p_pid;
745 mtx_unlock(&tp->tun_mtx);
746 break;
747 case FIONBIO:
748 break;
749 case FIOASYNC:
750 mtx_lock(&tp->tun_mtx);
751 if (*(int *)data)
752 tp->tun_flags |= TUN_ASYNC;
753 else
754 tp->tun_flags &= ~TUN_ASYNC;
755 mtx_unlock(&tp->tun_mtx);
756 break;
757 case FIONREAD:
758 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
759 struct mbuf *mb;
760 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
761 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
762 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
763 *(int *)data += mb->m_len;
764 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
765 } else
766 *(int *)data = 0;
767 break;
768 case FIOSETOWN:
769 return (fsetown(*(int *)data, &tp->tun_sigio));
770
771 case FIOGETOWN:
772 *(int *)data = fgetown(&tp->tun_sigio);
773 return (0);
774
775 /* This is deprecated, FIOSETOWN should be used instead. */
776 case TIOCSPGRP:
777 return (fsetown(-(*(int *)data), &tp->tun_sigio));
778
779 /* This is deprecated, FIOGETOWN should be used instead. */
780 case TIOCGPGRP:
781 *(int *)data = -fgetown(&tp->tun_sigio);
782 return (0);
783
784 default:
785 return (ENOTTY);
786 }
787 return (0);
788 }
789
790 /*
791 * The cdevsw read interface - reads a packet at a time, or at
792 * least as much of a packet as can be read.
793 */
794 static int
795 tunread(struct cdev *dev, struct uio *uio, int flag)
796 {
797 struct tun_softc *tp = dev->si_drv1;
798 struct ifnet *ifp = TUN2IFP(tp);
799 struct mbuf *m;
800 int error=0, len;
801
802 TUNDEBUG (ifp, "read\n");
803 mtx_lock(&tp->tun_mtx);
804 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
805 mtx_unlock(&tp->tun_mtx);
806 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
807 return (EHOSTDOWN);
808 }
809
810 tp->tun_flags &= ~TUN_RWAIT;
811
812 do {
813 IFQ_DEQUEUE(&ifp->if_snd, m);
814 if (m == NULL) {
815 if (flag & O_NONBLOCK) {
816 mtx_unlock(&tp->tun_mtx);
817 return (EWOULDBLOCK);
818 }
819 tp->tun_flags |= TUN_RWAIT;
820 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
821 "tunread", 0);
822 if (error != 0) {
823 mtx_unlock(&tp->tun_mtx);
824 return (error);
825 }
826 }
827 } while (m == NULL);
828 mtx_unlock(&tp->tun_mtx);
829
830 while (m && uio->uio_resid > 0 && error == 0) {
831 len = min(uio->uio_resid, m->m_len);
832 if (len != 0)
833 error = uiomove(mtod(m, void *), len, uio);
834 m = m_free(m);
835 }
836
837 if (m) {
838 TUNDEBUG(ifp, "Dropping mbuf\n");
839 m_freem(m);
840 }
841 return (error);
842 }
843
844 /*
845 * the cdevsw write interface - an atomic write is a packet - or else!
846 */
847 static int
848 tunwrite(struct cdev *dev, struct uio *uio, int flag)
849 {
850 struct tun_softc *tp = dev->si_drv1;
851 struct ifnet *ifp = TUN2IFP(tp);
852 struct mbuf *m;
853 int error = 0;
854 uint32_t family;
855 int isr;
856
857 TUNDEBUG(ifp, "tunwrite\n");
858
859 if ((ifp->if_flags & IFF_UP) != IFF_UP)
860 /* ignore silently */
861 return (0);
862
863 if (uio->uio_resid == 0)
864 return (0);
865
866 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
867 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
868 return (EIO);
869 }
870
871 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
872 ifp->if_ierrors++;
873 return (error);
874 }
875
876 m->m_pkthdr.rcvif = ifp;
877 #ifdef MAC
878 mac_create_mbuf_from_ifnet(ifp, m);
879 #endif
880
881 /* Could be unlocked read? */
882 mtx_lock(&tp->tun_mtx);
883 if (tp->tun_flags & TUN_IFHEAD) {
884 mtx_unlock(&tp->tun_mtx);
885 if (m->m_len < sizeof(family) &&
886 (m = m_pullup(m, sizeof(family))) == NULL)
887 return (ENOBUFS);
888 family = ntohl(*mtod(m, u_int32_t *));
889 m_adj(m, sizeof(family));
890 } else {
891 mtx_unlock(&tp->tun_mtx);
892 family = AF_INET;
893 }
894
895 BPF_MTAP2(ifp, &family, sizeof(family), m);
896
897 switch (family) {
898 #ifdef INET
899 case AF_INET:
900 isr = NETISR_IP;
901 break;
902 #endif
903 #ifdef INET6
904 case AF_INET6:
905 isr = NETISR_IPV6;
906 break;
907 #endif
908 #ifdef IPX
909 case AF_IPX:
910 isr = NETISR_IPX;
911 break;
912 #endif
913 #ifdef NETATALK
914 case AF_APPLETALK:
915 isr = NETISR_ATALK2;
916 break;
917 #endif
918 default:
919 m_freem(m);
920 return (EAFNOSUPPORT);
921 }
922 /* First chunk of an mbuf contains good junk */
923 if (harvest.point_to_point)
924 random_harvest(m, 16, 3, 0, RANDOM_NET);
925 ifp->if_ibytes += m->m_pkthdr.len;
926 ifp->if_ipackets++;
927 netisr_dispatch(isr, m);
928 return (0);
929 }
930
931 /*
932 * tunpoll - the poll interface, this is only useful on reads
933 * really. The write detect always returns true, write never blocks
934 * anyway, it either accepts the packet or drops it.
935 */
936 static int
937 tunpoll(struct cdev *dev, int events, struct thread *td)
938 {
939 struct tun_softc *tp = dev->si_drv1;
940 struct ifnet *ifp = TUN2IFP(tp);
941 int revents = 0;
942 struct mbuf *m;
943
944 TUNDEBUG(ifp, "tunpoll\n");
945
946 if (events & (POLLIN | POLLRDNORM)) {
947 IFQ_LOCK(&ifp->if_snd);
948 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
949 if (m != NULL) {
950 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
951 revents |= events & (POLLIN | POLLRDNORM);
952 } else {
953 TUNDEBUG(ifp, "tunpoll waiting\n");
954 selrecord(td, &tp->tun_rsel);
955 }
956 IFQ_UNLOCK(&ifp->if_snd);
957 }
958 if (events & (POLLOUT | POLLWRNORM))
959 revents |= events & (POLLOUT | POLLWRNORM);
960
961 return (revents);
962 }
963
964 /*
965 * tunkqfilter - support for the kevent() system call.
966 */
967 static int
968 tunkqfilter(struct cdev *dev, struct knote *kn)
969 {
970 struct tun_softc *tp = dev->si_drv1;
971 struct ifnet *ifp = TUN2IFP(tp);
972
973 switch(kn->kn_filter) {
974 case EVFILT_READ:
975 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
976 ifp->if_xname, minor(dev));
977 kn->kn_fop = &tun_read_filterops;
978 break;
979
980 case EVFILT_WRITE:
981 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
982 ifp->if_xname, minor(dev));
983 kn->kn_fop = &tun_write_filterops;
984 break;
985
986 default:
987 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
988 ifp->if_xname, minor(dev));
989 return(EINVAL);
990 }
991
992 kn->kn_hook = tp;
993 knlist_add(&tp->tun_rsel.si_note, kn, 0);
994
995 return (0);
996 }
997
998 /*
999 * Return true of there is data in the interface queue.
1000 */
1001 static int
1002 tunkqread(struct knote *kn, long hint)
1003 {
1004 int ret;
1005 struct tun_softc *tp = kn->kn_hook;
1006 struct cdev *dev = tp->tun_dev;
1007 struct ifnet *ifp = TUN2IFP(tp);
1008
1009 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1010 TUNDEBUG(ifp,
1011 "%s have data in the queue. Len = %d, minor = %#x\n",
1012 ifp->if_xname, ifp->if_snd.ifq_len, minor(dev));
1013 ret = 1;
1014 } else {
1015 TUNDEBUG(ifp,
1016 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1017 minor(dev));
1018 ret = 0;
1019 }
1020
1021 return (ret);
1022 }
1023
1024 /*
1025 * Always can write, always return MTU in kn->data.
1026 */
1027 static int
1028 tunkqwrite(struct knote *kn, long hint)
1029 {
1030 struct tun_softc *tp = kn->kn_hook;
1031 struct ifnet *ifp = TUN2IFP(tp);
1032
1033 kn->kn_data = ifp->if_mtu;
1034
1035 return (1);
1036 }
1037
1038 static void
1039 tunkqdetach(struct knote *kn)
1040 {
1041 struct tun_softc *tp = kn->kn_hook;
1042
1043 knlist_remove(&tp->tun_rsel.si_note, kn, 0);
1044 }
Cache object: 6ba32e133d10107ea296b4946372f676
|