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