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