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