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