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