proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / net / tun / if_tun.c
CommitLineData
984263bc
MD
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: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
dadab5e9 17 * $DragonFly: src/sys/net/tun/if_tun.c,v 1.4 2003/06/25 03:56:02 dillon Exp $
984263bc
MD
18 */
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/socket.h>
27#include <sys/filio.h>
28#include <sys/sockio.h>
29#include <sys/ttycom.h>
30#include <sys/poll.h>
31#include <sys/signalvar.h>
32#include <sys/filedesc.h>
33#include <sys/kernel.h>
34#include <sys/sysctl.h>
35#include <sys/conf.h>
36#include <sys/uio.h>
37#include <sys/vnode.h>
38#include <sys/malloc.h>
39
40#include <net/if.h>
41#include <net/if_types.h>
42#include <net/route.h>
43#include <net/intrq.h>
44
45#ifdef INET
46#include <netinet/in.h>
47#endif
48
49#include <net/bpf.h>
50
51#include <net/if_tunvar.h>
52#include <net/if_tun.h>
53
54static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
55
56static void tunattach __P((void *));
57PSEUDO_SET(tunattach, if_tun);
58
59static void tuncreate __P((dev_t dev));
60
61#define TUNDEBUG if (tundebug) printf
62static int tundebug = 0;
63SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
64
65static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
66 struct rtentry *rt));
67static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
68static int tuninit __P((struct ifnet *));
69
70static d_open_t tunopen;
71static d_close_t tunclose;
72static d_read_t tunread;
73static d_write_t tunwrite;
74static d_ioctl_t tunioctl;
75static d_poll_t tunpoll;
76
77#define CDEV_MAJOR 52
78static struct cdevsw tun_cdevsw = {
79 /* open */ tunopen,
80 /* close */ tunclose,
81 /* read */ tunread,
82 /* write */ tunwrite,
83 /* ioctl */ tunioctl,
84 /* poll */ tunpoll,
85 /* mmap */ nommap,
86 /* strategy */ nostrategy,
87 /* name */ "tun",
88 /* maj */ CDEV_MAJOR,
89 /* dump */ nodump,
90 /* psize */ nopsize,
91 /* flags */ 0,
92 /* bmaj */ -1
93};
94
95static void
96tunattach(dummy)
97 void *dummy;
98{
99
100 cdevsw_add(&tun_cdevsw);
101}
102
103static void
104tuncreate(dev)
105 dev_t dev;
106{
107 struct tun_softc *sc;
108 struct ifnet *ifp;
109
110 dev = make_dev(&tun_cdevsw, minor(dev),
111 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
112
113 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
114 bzero(sc, sizeof *sc);
115 sc->tun_flags = TUN_INITED;
116
117 ifp = &sc->tun_if;
118 ifp->if_unit = lminor(dev);
119 ifp->if_name = "tun";
120 ifp->if_mtu = TUNMTU;
121 ifp->if_ioctl = tunifioctl;
122 ifp->if_output = tunoutput;
123 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
124 ifp->if_type = IFT_PPP;
125 ifp->if_snd.ifq_maxlen = ifqmaxlen;
126 ifp->if_softc = sc;
127 if_attach(ifp);
128 bpfattach(ifp, DLT_NULL, sizeof(u_int));
129 dev->si_drv1 = sc;
130}
131
132/*
133 * tunnel open - must be superuser & the device must be
134 * configured in
135 */
136static int
41c20dac 137tunopen(dev_t dev, int flag, int mode, struct thread *td)
984263bc
MD
138{
139 struct ifnet *ifp;
140 struct tun_softc *tp;
141 register int error;
142
41c20dac 143 KKASSERT(td->td_proc);
dadab5e9 144 if ((error = suser(td)) != NULL)
984263bc
MD
145 return (error);
146
147 tp = dev->si_drv1;
148 if (!tp) {
149 tuncreate(dev);
150 tp = dev->si_drv1;
151 }
152 if (tp->tun_flags & TUN_OPEN)
153 return EBUSY;
41c20dac 154 tp->tun_pid = td->td_proc->p_pid;
984263bc
MD
155 ifp = &tp->tun_if;
156 tp->tun_flags |= TUN_OPEN;
157 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
158 return (0);
159}
160
161/*
162 * tunclose - close the device - mark i/f down & delete
163 * routing info
164 */
165static int
41c20dac 166tunclose(dev_t dev, int foo, int bar, struct thread *td)
984263bc
MD
167{
168 register int s;
169 struct tun_softc *tp;
170 struct ifnet *ifp;
171 struct mbuf *m;
172
173 tp = dev->si_drv1;
174 ifp = &tp->tun_if;
175
176 tp->tun_flags &= ~TUN_OPEN;
177 tp->tun_pid = 0;
178
179 /*
180 * junk all pending output
181 */
182 do {
183 s = splimp();
184 IF_DEQUEUE(&ifp->if_snd, m);
185 splx(s);
186 if (m)
187 m_freem(m);
188 } while (m);
189
190 if (ifp->if_flags & IFF_UP) {
191 s = splimp();
192 if_down(ifp);
193 splx(s);
194 }
195
196 if (ifp->if_flags & IFF_RUNNING) {
197 register struct ifaddr *ifa;
198
199 s = splimp();
200 /* find internet addresses and delete routes */
201 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
202 if (ifa->ifa_addr->sa_family == AF_INET)
203 rtinit(ifa, (int)RTM_DELETE,
204 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
205 ifp->if_flags &= ~IFF_RUNNING;
206 splx(s);
207 }
208
209 funsetown(tp->tun_sigio);
210 selwakeup(&tp->tun_rsel);
211
212 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
213 return (0);
214}
215
216static int
217tuninit(ifp)
218 struct ifnet *ifp;
219{
220 struct tun_softc *tp = ifp->if_softc;
221 register struct ifaddr *ifa;
222 int error = 0;
223
224 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
225
226 ifp->if_flags |= IFF_UP | IFF_RUNNING;
227 getmicrotime(&ifp->if_lastchange);
228
229 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
230 ifa = TAILQ_NEXT(ifa, ifa_link)) {
231 if (ifa->ifa_addr == NULL)
232 error = EFAULT;
233 /* XXX: Should maybe return straight off? */
234 else {
235#ifdef INET
236 if (ifa->ifa_addr->sa_family == AF_INET) {
237 struct sockaddr_in *si;
238
239 si = (struct sockaddr_in *)ifa->ifa_addr;
240 if (si->sin_addr.s_addr)
241 tp->tun_flags |= TUN_IASET;
242
243 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
244 if (si && si->sin_addr.s_addr)
245 tp->tun_flags |= TUN_DSTADDR;
246 }
247#endif
248 }
249 }
250 return (error);
251}
252
253/*
254 * Process an ioctl request.
255 */
256int
257tunifioctl(ifp, cmd, data)
258 struct ifnet *ifp;
259 u_long cmd;
260 caddr_t data;
261{
262 struct ifreq *ifr = (struct ifreq *)data;
263 struct tun_softc *tp = ifp->if_softc;
264 struct ifstat *ifs;
265 int error = 0, s;
266
267 s = splimp();
268 switch(cmd) {
269 case SIOCGIFSTATUS:
270 ifs = (struct ifstat *)data;
271 if (tp->tun_pid)
272 sprintf(ifs->ascii + strlen(ifs->ascii),
273 "\tOpened by PID %d\n", tp->tun_pid);
274 break;
275 case SIOCSIFADDR:
276 error = tuninit(ifp);
277 TUNDEBUG("%s%d: address set, error=%d\n",
278 ifp->if_name, ifp->if_unit, error);
279 break;
280 case SIOCSIFDSTADDR:
281 error = tuninit(ifp);
282 TUNDEBUG("%s%d: destination address set, error=%d\n",
283 ifp->if_name, ifp->if_unit, error);
284 break;
285 case SIOCSIFMTU:
286 ifp->if_mtu = ifr->ifr_mtu;
287 TUNDEBUG("%s%d: mtu set\n",
288 ifp->if_name, ifp->if_unit);
289 break;
290 case SIOCSIFFLAGS:
291 case SIOCADDMULTI:
292 case SIOCDELMULTI:
293 break;
294 default:
295 error = EINVAL;
296 }
297 splx(s);
298 return (error);
299}
300
301/*
302 * tunoutput - queue packets from higher level ready to put out.
303 */
304int
305tunoutput(ifp, m0, dst, rt)
306 struct ifnet *ifp;
307 struct mbuf *m0;
308 struct sockaddr *dst;
309 struct rtentry *rt;
310{
311 struct tun_softc *tp = ifp->if_softc;
312 int s;
313
314 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
315
316 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
317 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
318 ifp->if_unit, tp->tun_flags);
319 m_freem (m0);
320 return EHOSTDOWN;
321 }
322
323 /* BPF write needs to be handled specially */
324 if (dst->sa_family == AF_UNSPEC) {
325 dst->sa_family = *(mtod(m0, int *));
326 m0->m_len -= sizeof(int);
327 m0->m_pkthdr.len -= sizeof(int);
328 m0->m_data += sizeof(int);
329 }
330
331 if (ifp->if_bpf) {
332 /*
333 * We need to prepend the address family as
334 * a four byte field. Cons up a dummy header
335 * to pacify bpf. This is safe because bpf
336 * will only read from the mbuf (i.e., it won't
337 * try to free it or keep a pointer to it).
338 */
339 struct mbuf m;
340 uint32_t af = dst->sa_family;
341
342 m.m_next = m0;
343 m.m_len = 4;
344 m.m_data = (char *)&af;
345
346 bpf_mtap(ifp, &m);
347 }
348
349 /* prepend sockaddr? this may abort if the mbuf allocation fails */
350 if (tp->tun_flags & TUN_LMODE) {
351 /* allocate space for sockaddr */
352 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
353
354 /* if allocation failed drop packet */
355 if (m0 == NULL){
356 s = splimp(); /* spl on queue manipulation */
357 IF_DROP(&ifp->if_snd);
358 splx(s);
359 ifp->if_oerrors++;
360 return (ENOBUFS);
361 } else {
362 bcopy(dst, m0->m_data, dst->sa_len);
363 }
364 }
365
366 if (tp->tun_flags & TUN_IFHEAD) {
367 /* Prepend the address family */
368 M_PREPEND(m0, 4, M_DONTWAIT);
369
370 /* if allocation failed drop packet */
371 if (m0 == NULL){
372 s = splimp(); /* spl on queue manipulation */
373 IF_DROP(&ifp->if_snd);
374 splx(s);
375 ifp->if_oerrors++;
376 return ENOBUFS;
377 } else
378 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
379 } else {
380#ifdef INET
381 if (dst->sa_family != AF_INET)
382#endif
383 {
384 m_freem(m0);
385 return EAFNOSUPPORT;
386 }
387 }
388
389 s = splimp();
390 if (IF_QFULL(&ifp->if_snd)) {
391 IF_DROP(&ifp->if_snd);
392 m_freem(m0);
393 splx(s);
394 ifp->if_collisions++;
395 return ENOBUFS;
396 }
397 ifp->if_obytes += m0->m_pkthdr.len;
398 IF_ENQUEUE(&ifp->if_snd, m0);
399 splx(s);
400 ifp->if_opackets++;
401
402 if (tp->tun_flags & TUN_RWAIT) {
403 tp->tun_flags &= ~TUN_RWAIT;
404 wakeup((caddr_t)tp);
405 }
406 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
407 pgsigio(tp->tun_sigio, SIGIO, 0);
408 selwakeup(&tp->tun_rsel);
409 return 0;
410}
411
412/*
413 * the cdevsw interface is now pretty minimal.
414 */
415static int
41c20dac 416tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
984263bc
MD
417{
418 int s;
419 struct tun_softc *tp = dev->si_drv1;
420 struct tuninfo *tunp;
421
422 switch (cmd) {
423 case TUNSIFINFO:
424 tunp = (struct tuninfo *)data;
425 if (tunp->mtu < IF_MINMTU)
426 return (EINVAL);
427 tp->tun_if.if_mtu = tunp->mtu;
428 tp->tun_if.if_type = tunp->type;
429 tp->tun_if.if_baudrate = tunp->baudrate;
430 break;
431 case TUNGIFINFO:
432 tunp = (struct tuninfo *)data;
433 tunp->mtu = tp->tun_if.if_mtu;
434 tunp->type = tp->tun_if.if_type;
435 tunp->baudrate = tp->tun_if.if_baudrate;
436 break;
437 case TUNSDEBUG:
438 tundebug = *(int *)data;
439 break;
440 case TUNGDEBUG:
441 *(int *)data = tundebug;
442 break;
443 case TUNSLMODE:
444 if (*(int *)data) {
445 tp->tun_flags |= TUN_LMODE;
446 tp->tun_flags &= ~TUN_IFHEAD;
447 } else
448 tp->tun_flags &= ~TUN_LMODE;
449 break;
450 case TUNSIFHEAD:
451 if (*(int *)data) {
452 tp->tun_flags |= TUN_IFHEAD;
453 tp->tun_flags &= ~TUN_LMODE;
454 } else
455 tp->tun_flags &= ~TUN_IFHEAD;
456 break;
457 case TUNGIFHEAD:
458 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
459 break;
460 case TUNSIFMODE:
461 /* deny this if UP */
462 if (tp->tun_if.if_flags & IFF_UP)
463 return(EBUSY);
464
465 switch (*(int *)data & ~IFF_MULTICAST) {
466 case IFF_POINTOPOINT:
467 case IFF_BROADCAST:
468 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
469 tp->tun_if.if_flags |= *(int *)data;
470 break;
471 default:
472 return(EINVAL);
473 }
474 break;
475 case TUNSIFPID:
476 tp->tun_pid = curproc->p_pid;
477 break;
478 case FIONBIO:
479 break;
480 case FIOASYNC:
481 if (*(int *)data)
482 tp->tun_flags |= TUN_ASYNC;
483 else
484 tp->tun_flags &= ~TUN_ASYNC;
485 break;
486 case FIONREAD:
487 s = splimp();
488 if (tp->tun_if.if_snd.ifq_head) {
489 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
490 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
491 *(int *)data += mb->m_len;
492 } else
493 *(int *)data = 0;
494 splx(s);
495 break;
496 case FIOSETOWN:
497 return (fsetown(*(int *)data, &tp->tun_sigio));
498
499 case FIOGETOWN:
500 *(int *)data = fgetown(tp->tun_sigio);
501 return (0);
502
503 /* This is deprecated, FIOSETOWN should be used instead. */
504 case TIOCSPGRP:
505 return (fsetown(-(*(int *)data), &tp->tun_sigio));
506
507 /* This is deprecated, FIOGETOWN should be used instead. */
508 case TIOCGPGRP:
509 *(int *)data = -fgetown(tp->tun_sigio);
510 return (0);
511
512 default:
513 return (ENOTTY);
514 }
515 return (0);
516}
517
518/*
519 * The cdevsw read interface - reads a packet at a time, or at
520 * least as much of a packet as can be read.
521 */
522static int
523tunread(dev, uio, flag)
524 dev_t dev;
525 struct uio *uio;
526 int flag;
527{
528 struct tun_softc *tp = dev->si_drv1;
529 struct ifnet *ifp = &tp->tun_if;
530 struct mbuf *m0;
531 int error=0, len, s;
532
533 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
534 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
535 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
536 ifp->if_unit, tp->tun_flags);
537 return EHOSTDOWN;
538 }
539
540 tp->tun_flags &= ~TUN_RWAIT;
541
542 s = splimp();
543 do {
544 IF_DEQUEUE(&ifp->if_snd, m0);
545 if (m0 == 0) {
546 if (flag & IO_NDELAY) {
547 splx(s);
548 return EWOULDBLOCK;
549 }
550 tp->tun_flags |= TUN_RWAIT;
551 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
552 "tunread", 0)) != 0) {
553 splx(s);
554 return error;
555 }
556 }
557 } while (m0 == 0);
558 splx(s);
559
560 while (m0 && uio->uio_resid > 0 && error == 0) {
561 len = min(uio->uio_resid, m0->m_len);
562 if (len != 0)
563 error = uiomove(mtod(m0, caddr_t), len, uio);
564 m0 = m_free(m0);
565 }
566
567 if (m0) {
568 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
569 m_freem(m0);
570 }
571 return error;
572}
573
574/*
575 * the cdevsw write interface - an atomic write is a packet - or else!
576 */
577static int
578tunwrite(dev, uio, flag)
579 dev_t dev;
580 struct uio *uio;
581 int flag;
582{
583 struct tun_softc *tp = dev->si_drv1;
584 struct ifnet *ifp = &tp->tun_if;
585 struct mbuf *top, **mp, *m;
586 int error=0, tlen, mlen;
587 uint32_t family;
588
589 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
590
591 if (uio->uio_resid == 0)
592 return 0;
593
594 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
595 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
596 uio->uio_resid);
597 return EIO;
598 }
599 tlen = uio->uio_resid;
600
601 /* get a header mbuf */
602 MGETHDR(m, M_DONTWAIT, MT_DATA);
603 if (m == NULL)
604 return ENOBUFS;
605 mlen = MHLEN;
606
607 top = 0;
608 mp = &top;
609 while (error == 0 && uio->uio_resid > 0) {
610 m->m_len = min(mlen, uio->uio_resid);
611 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
612 *mp = m;
613 mp = &m->m_next;
614 if (uio->uio_resid > 0) {
615 MGET (m, M_DONTWAIT, MT_DATA);
616 if (m == 0) {
617 error = ENOBUFS;
618 break;
619 }
620 mlen = MLEN;
621 }
622 }
623 if (error) {
624 if (top)
625 m_freem (top);
626 ifp->if_ierrors++;
627 return error;
628 }
629
630 top->m_pkthdr.len = tlen;
631 top->m_pkthdr.rcvif = ifp;
632
633 if (ifp->if_bpf) {
634 if (tp->tun_flags & TUN_IFHEAD) {
635 /*
636 * Conveniently, we already have a 4-byte address
637 * family prepended to our packet !
638 * Inconveniently, it's in the wrong byte order !
639 */
640 if ((top = m_pullup(top, sizeof(family))) == NULL)
641 return ENOBUFS;
642 *mtod(top, u_int32_t *) =
643 ntohl(*mtod(top, u_int32_t *));
644 bpf_mtap(ifp, top);
645 *mtod(top, u_int32_t *) =
646 htonl(*mtod(top, u_int32_t *));
647 } else {
648 /*
649 * We need to prepend the address family as
650 * a four byte field. Cons up a dummy header
651 * to pacify bpf. This is safe because bpf
652 * will only read from the mbuf (i.e., it won't
653 * try to free it or keep a pointer to it).
654 */
655 struct mbuf m;
656 uint32_t af = AF_INET;
657
658 m.m_next = top;
659 m.m_len = 4;
660 m.m_data = (char *)&af;
661
662 bpf_mtap(ifp, &m);
663 }
664 }
665
666 if (tp->tun_flags & TUN_IFHEAD) {
667 if (top->m_len < sizeof(family) &&
668 (top = m_pullup(top, sizeof(family))) == NULL)
669 return ENOBUFS;
670 family = ntohl(*mtod(top, u_int32_t *));
671 m_adj(top, sizeof(family));
672 } else
673 family = AF_INET;
674
675 ifp->if_ibytes += top->m_pkthdr.len;
676 ifp->if_ipackets++;
677
678 return family_enqueue(family, top);
679}
680
681/*
682 * tunpoll - the poll interface, this is only useful on reads
683 * really. The write detect always returns true, write never blocks
684 * anyway, it either accepts the packet or drops it.
685 */
686static int
41c20dac 687tunpoll(dev_t dev, int events, struct thread *td)
984263bc
MD
688{
689 int s;
690 struct tun_softc *tp = dev->si_drv1;
691 struct ifnet *ifp = &tp->tun_if;
692 int revents = 0;
693
694 s = splimp();
695 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
696
697 if (events & (POLLIN | POLLRDNORM)) {
698 if (ifp->if_snd.ifq_len > 0) {
699 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
700 ifp->if_unit, ifp->if_snd.ifq_len);
701 revents |= events & (POLLIN | POLLRDNORM);
702 } else {
703 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
704 ifp->if_unit);
41c20dac 705 selrecord(td, &tp->tun_rsel);
984263bc
MD
706 }
707 }
708 if (events & (POLLOUT | POLLWRNORM))
709 revents |= events & (POLLOUT | POLLWRNORM);
710
711 splx(s);
712 return (revents);
713}