1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
2 /*-
3 * SPDX-License-Identifier: BSD-2-Clause
4 *
5 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
6 * All rights reserved.
7 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * BASED ON:
32 * -------------------------------------------------------------------------
33 *
34 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35 * Nottingham University 1987.
36 *
37 * This source may be freely distributed, however I would be interested
38 * in any changes that are made.
39 *
40 * This driver takes packets off the IP i/f and hands them up to a
41 * user process to have its wicked way with. This driver has it's
42 * roots in a similar driver written by Phil Cockcroft (formerly) at
43 * UCL. This driver is based much more on read/write/poll mode of
44 * operation though.
45 */
46
47 #include "opt_inet.h"
48 #include "opt_inet6.h"
49
50 #include <sys/param.h>
51 #include <sys/lock.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/jail.h>
56 #include <sys/mbuf.h>
57 #include <sys/module.h>
58 #include <sys/socket.h>
59 #include <sys/eventhandler.h>
60 #include <sys/fcntl.h>
61 #include <sys/filio.h>
62 #include <sys/sockio.h>
63 #include <sys/sx.h>
64 #include <sys/syslog.h>
65 #include <sys/ttycom.h>
66 #include <sys/poll.h>
67 #include <sys/selinfo.h>
68 #include <sys/signalvar.h>
69 #include <sys/filedesc.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
72 #include <sys/conf.h>
73 #include <sys/uio.h>
74 #include <sys/malloc.h>
75 #include <sys/random.h>
76 #include <sys/ctype.h>
77 #include <sys/osd.h>
78
79 #include <net/ethernet.h>
80 #include <net/if.h>
81 #include <net/if_var.h>
82 #include <net/if_clone.h>
83 #include <net/if_dl.h>
84 #include <net/if_media.h>
85 #include <net/if_private.h>
86 #include <net/if_types.h>
87 #include <net/if_vlan_var.h>
88 #include <net/netisr.h>
89 #include <net/route.h>
90 #include <net/vnet.h>
91 #include <netinet/in.h>
92 #ifdef INET
93 #include <netinet/ip.h>
94 #endif
95 #ifdef INET6
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #endif
99 #include <netinet/udp.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_lro.h>
102 #include <net/bpf.h>
103 #include <net/if_tap.h>
104 #include <net/if_tun.h>
105
106 #include <dev/virtio/network/virtio_net.h>
107
108 #include <sys/queue.h>
109 #include <sys/condvar.h>
110 #include <security/mac/mac_framework.h>
111
112 struct tuntap_driver;
113
114 /*
115 * tun_list is protected by global tunmtx. Other mutable fields are
116 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
117 * static for the duration of a tunnel interface.
118 */
119 struct tuntap_softc {
120 TAILQ_ENTRY(tuntap_softc) tun_list;
121 struct cdev *tun_alias;
122 struct cdev *tun_dev;
123 u_short tun_flags; /* misc flags */
124 #define TUN_OPEN 0x0001
125 #define TUN_INITED 0x0002
126 #define TUN_UNUSED1 0x0008
127 #define TUN_UNUSED2 0x0010
128 #define TUN_LMODE 0x0020
129 #define TUN_RWAIT 0x0040
130 #define TUN_ASYNC 0x0080
131 #define TUN_IFHEAD 0x0100
132 #define TUN_DYING 0x0200
133 #define TUN_L2 0x0400
134 #define TUN_VMNET 0x0800
135
136 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
137 #define TUN_READY (TUN_OPEN | TUN_INITED)
138
139 pid_t tun_pid; /* owning pid */
140 struct ifnet *tun_ifp; /* the interface */
141 struct sigio *tun_sigio; /* async I/O info */
142 struct tuntap_driver *tun_drv; /* appropriate driver */
143 struct selinfo tun_rsel; /* read select */
144 struct mtx tun_mtx; /* softc field mutex */
145 struct cv tun_cv; /* for ref'd dev destroy */
146 struct ether_addr tun_ether; /* remote address */
147 int tun_busy; /* busy count */
148 int tun_vhdrlen; /* virtio-net header length */
149 struct lro_ctrl tun_lro; /* for TCP LRO */
150 bool tun_lro_ready; /* TCP LRO initialized */
151 };
152 #define TUN2IFP(sc) ((sc)->tun_ifp)
153
154 #define TUNDEBUG if (tundebug) if_printf
155
156 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
157 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
158 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
159
160 #define TUN_VMIO_FLAG_MASK 0x0fff
161
162 /*
163 * Interface capabilities of a tap device that supports the virtio-net
164 * header.
165 */
166 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
167 | IFCAP_VLAN_HWCSUM \
168 | IFCAP_TSO | IFCAP_LRO \
169 | IFCAP_VLAN_HWTSO)
170
171 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
172 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
173
174 /*
175 * All mutable global variables in if_tun are locked using tunmtx, with
176 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
177 * which are static after setup.
178 */
179 static struct mtx tunmtx;
180 static eventhandler_tag arrival_tag;
181 static eventhandler_tag clone_tag;
182 static int tuntap_osd_jail_slot;
183 static const char tunname[] = "tun";
184 static const char tapname[] = "tap";
185 static const char vmnetname[] = "vmnet";
186 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
187 static int tundebug = 0;
188 static int tundclone = 1;
189 static int tap_allow_uopen = 0; /* allow user devfs cloning */
190 static int tapuponopen = 0; /* IFF_UP on open() */
191 static int tapdclone = 1; /* enable devfs cloning */
192
193 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
194 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
195
196 static struct sx tun_ioctl_sx;
197 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
198
199 SYSCTL_DECL(_net_link);
200 /* tun */
201 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
202 "IP tunnel software network interface");
203 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
204 "Enable legacy devfs interface creation");
205
206 /* tap */
207 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
208 "Ethernet tunnel software network interface");
209 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
210 "Enable legacy devfs interface creation for all users");
211 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
212 "Bring interface up when /dev/tap is opened");
213 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
214 "Enable legacy devfs interface creation");
215 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
216
217 static int tun_create_device(struct tuntap_driver *drv, int unit,
218 struct ucred *cr, struct cdev **dev, const char *name);
219 static int tun_busy_locked(struct tuntap_softc *tp);
220 static void tun_unbusy_locked(struct tuntap_softc *tp);
221 static int tun_busy(struct tuntap_softc *tp);
222 static void tun_unbusy(struct tuntap_softc *tp);
223
224 static int tuntap_name2info(const char *name, int *unit, int *flags);
225 static void tunclone(void *arg, struct ucred *cred, char *name,
226 int namelen, struct cdev **dev);
227 static void tuncreate(struct cdev *dev);
228 static void tundtor(void *data);
229 static void tunrename(void *arg, struct ifnet *ifp);
230 static int tunifioctl(struct ifnet *, u_long, caddr_t);
231 static void tuninit(struct ifnet *);
232 static void tunifinit(void *xtp);
233 static int tuntapmodevent(module_t, int, void *);
234 static int tunoutput(struct ifnet *, struct mbuf *,
235 const struct sockaddr *, struct route *ro);
236 static void tunstart(struct ifnet *);
237 static void tunstart_l2(struct ifnet *);
238
239 static int tun_clone_match(struct if_clone *ifc, const char *name);
240 static int tap_clone_match(struct if_clone *ifc, const char *name);
241 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
242 static int tun_clone_create(struct if_clone *, char *, size_t,
243 struct ifc_data *, struct ifnet **);
244 static int tun_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
245 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
246
247 static d_open_t tunopen;
248 static d_read_t tunread;
249 static d_write_t tunwrite;
250 static d_ioctl_t tunioctl;
251 static d_poll_t tunpoll;
252 static d_kqfilter_t tunkqfilter;
253
254 static int tunkqread(struct knote *, long);
255 static int tunkqwrite(struct knote *, long);
256 static void tunkqdetach(struct knote *);
257
258 static const struct filterops tun_read_filterops = {
259 .f_isfd = 1,
260 .f_attach = NULL,
261 .f_detach = tunkqdetach,
262 .f_event = tunkqread,
263 };
264
265 static const struct filterops tun_write_filterops = {
266 .f_isfd = 1,
267 .f_attach = NULL,
268 .f_detach = tunkqdetach,
269 .f_event = tunkqwrite,
270 };
271
272 static struct tuntap_driver {
273 struct cdevsw cdevsw;
274 int ident_flags;
275 struct unrhdr *unrhdr;
276 struct clonedevs *clones;
277 ifc_match_f *clone_match_fn;
278 ifc_create_f *clone_create_fn;
279 ifc_destroy_f *clone_destroy_fn;
280 } tuntap_drivers[] = {
281 {
282 .ident_flags = 0,
283 .cdevsw = {
284 .d_version = D_VERSION,
285 .d_flags = D_NEEDMINOR,
286 .d_open = tunopen,
287 .d_read = tunread,
288 .d_write = tunwrite,
289 .d_ioctl = tunioctl,
290 .d_poll = tunpoll,
291 .d_kqfilter = tunkqfilter,
292 .d_name = tunname,
293 },
294 .clone_match_fn = tun_clone_match,
295 .clone_create_fn = tun_clone_create,
296 .clone_destroy_fn = tun_clone_destroy,
297 },
298 {
299 .ident_flags = TUN_L2,
300 .cdevsw = {
301 .d_version = D_VERSION,
302 .d_flags = D_NEEDMINOR,
303 .d_open = tunopen,
304 .d_read = tunread,
305 .d_write = tunwrite,
306 .d_ioctl = tunioctl,
307 .d_poll = tunpoll,
308 .d_kqfilter = tunkqfilter,
309 .d_name = tapname,
310 },
311 .clone_match_fn = tap_clone_match,
312 .clone_create_fn = tun_clone_create,
313 .clone_destroy_fn = tun_clone_destroy,
314 },
315 {
316 .ident_flags = TUN_L2 | TUN_VMNET,
317 .cdevsw = {
318 .d_version = D_VERSION,
319 .d_flags = D_NEEDMINOR,
320 .d_open = tunopen,
321 .d_read = tunread,
322 .d_write = tunwrite,
323 .d_ioctl = tunioctl,
324 .d_poll = tunpoll,
325 .d_kqfilter = tunkqfilter,
326 .d_name = vmnetname,
327 },
328 .clone_match_fn = vmnet_clone_match,
329 .clone_create_fn = tun_clone_create,
330 .clone_destroy_fn = tun_clone_destroy,
331 },
332 };
333 #define NDRV nitems(tuntap_drivers)
334
335 VNET_DEFINE_STATIC(struct if_clone *, tuntap_driver_cloners[NDRV]);
336 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
337
338 /*
339 * Mechanism for marking a tunnel device as busy so that we can safely do some
340 * orthogonal operations (such as operations on devices) without racing against
341 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
342 * open, to be woken up when the condition is alleviated.
343 */
344 static int
tun_busy_locked(struct tuntap_softc * tp)345 tun_busy_locked(struct tuntap_softc *tp)
346 {
347
348 TUN_LOCK_ASSERT(tp);
349 if ((tp->tun_flags & TUN_DYING) != 0) {
350 /*
351 * Perhaps unintuitive, but the device is busy going away.
352 * Other interpretations of EBUSY from tun_busy make little
353 * sense, since making a busy device even more busy doesn't
354 * sound like a problem.
355 */
356 return (EBUSY);
357 }
358
359 ++tp->tun_busy;
360 return (0);
361 }
362
363 static void
tun_unbusy_locked(struct tuntap_softc * tp)364 tun_unbusy_locked(struct tuntap_softc *tp)
365 {
366
367 TUN_LOCK_ASSERT(tp);
368 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
369
370 --tp->tun_busy;
371 /* Wake up anything that may be waiting on our busy tunnel. */
372 if (tp->tun_busy == 0)
373 cv_broadcast(&tp->tun_cv);
374 }
375
376 static int
tun_busy(struct tuntap_softc * tp)377 tun_busy(struct tuntap_softc *tp)
378 {
379 int ret;
380
381 TUN_LOCK(tp);
382 ret = tun_busy_locked(tp);
383 TUN_UNLOCK(tp);
384 return (ret);
385 }
386
387 static void
tun_unbusy(struct tuntap_softc * tp)388 tun_unbusy(struct tuntap_softc *tp)
389 {
390
391 TUN_LOCK(tp);
392 tun_unbusy_locked(tp);
393 TUN_UNLOCK(tp);
394 }
395
396 /*
397 * Sets unit and/or flags given the device name. Must be called with correct
398 * vnet context.
399 */
400 static int
tuntap_name2info(const char * name,int * outunit,int * outflags)401 tuntap_name2info(const char *name, int *outunit, int *outflags)
402 {
403 struct tuntap_driver *drv;
404 char *dname;
405 int flags, unit;
406 bool found;
407
408 if (name == NULL)
409 return (EINVAL);
410
411 /*
412 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
413 * wants to be able to pass back a char * through the second param. We
414 * will always set that as NULL here, so we'll fake it.
415 */
416 dname = __DECONST(char *, name);
417 found = false;
418
419 for (u_int i = 0; i < NDRV; i++) {
420 drv = &tuntap_drivers[i];
421
422 if (strcmp(name, drv->cdevsw.d_name) == 0) {
423 found = true;
424 unit = -1;
425 flags = drv->ident_flags;
426 break;
427 }
428
429 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
430 found = true;
431 flags = drv->ident_flags;
432 break;
433 }
434 }
435
436 if (!found)
437 return (ENXIO);
438
439 if (outunit != NULL)
440 *outunit = unit;
441 if (outflags != NULL)
442 *outflags = flags;
443 return (0);
444 }
445
446 /*
447 * Get driver information from a set of flags specified. Masks the identifying
448 * part of the flags and compares it against all of the available
449 * tuntap_drivers.
450 */
451 static struct tuntap_driver *
tuntap_driver_from_flags(int tun_flags)452 tuntap_driver_from_flags(int tun_flags)
453 {
454
455 for (u_int i = 0; i < NDRV; i++)
456 if ((tun_flags & TUN_DRIVER_IDENT_MASK) ==
457 tuntap_drivers[i].ident_flags)
458 return (&tuntap_drivers[i]);
459
460 return (NULL);
461 }
462
463 static int
tun_clone_match(struct if_clone * ifc,const char * name)464 tun_clone_match(struct if_clone *ifc, const char *name)
465 {
466 int tunflags;
467
468 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
469 if ((tunflags & TUN_L2) == 0)
470 return (1);
471 }
472
473 return (0);
474 }
475
476 static int
tap_clone_match(struct if_clone * ifc,const char * name)477 tap_clone_match(struct if_clone *ifc, const char *name)
478 {
479 int tunflags;
480
481 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
482 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
483 return (1);
484 }
485
486 return (0);
487 }
488
489 static int
vmnet_clone_match(struct if_clone * ifc,const char * name)490 vmnet_clone_match(struct if_clone *ifc, const char *name)
491 {
492 int tunflags;
493
494 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
495 if ((tunflags & TUN_VMNET) != 0)
496 return (1);
497 }
498
499 return (0);
500 }
501
502 /*
503 * Create a clone via the ifnet cloning mechanism. Note that this is invoked
504 * indirectly by tunclone() below.
505 */
506 static int
tun_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)507 tun_clone_create(struct if_clone *ifc, char *name, size_t len,
508 struct ifc_data *ifd, struct ifnet **ifpp)
509 {
510 struct tuntap_driver *drv;
511 struct cdev *dev;
512 int err, i, tunflags, unit;
513
514 tunflags = 0;
515 /* The name here tells us exactly what we're creating */
516 err = tuntap_name2info(name, &unit, &tunflags);
517 if (err != 0)
518 return (err);
519
520 drv = tuntap_driver_from_flags(tunflags);
521 if (drv == NULL)
522 return (ENXIO);
523
524 if (unit != -1) {
525 /* If this unit number is still available that's okay. */
526 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
527 return (EEXIST);
528 } else {
529 unit = alloc_unr(drv->unrhdr);
530 }
531
532 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
533
534 /* find any existing device, or allocate new unit number */
535 dev = NULL;
536 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
537 /* No preexisting struct cdev *, create one */
538 if (i != 0)
539 i = tun_create_device(drv, unit, NULL, &dev, name);
540 if (i == 0) {
541 struct tuntap_softc *tp;
542
543 tuncreate(dev);
544 tp = dev->si_drv1;
545 *ifpp = tp->tun_ifp;
546 }
547
548 return (i);
549 }
550
551 /*
552 * Create a clone via devfs access.
553 */
554 static void
tunclone(void * arg,struct ucred * cred,char * name,int namelen,struct cdev ** dev)555 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
556 struct cdev **dev)
557 {
558 char devname[SPECNAMELEN + 1];
559 struct tuntap_driver *drv;
560 int append_unit, i, u, tunflags;
561 bool mayclone;
562
563 if (*dev != NULL)
564 return;
565
566 tunflags = 0;
567 CURVNET_SET(CRED_TO_VNET(cred));
568 if (tuntap_name2info(name, &u, &tunflags) != 0)
569 goto out; /* Not recognized */
570
571 if (u != -1 && u > IF_MAXUNIT)
572 goto out; /* Unit number too high */
573
574 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
575 if ((tunflags & TUN_L2) != 0) {
576 /* tap/vmnet allow user open with a sysctl */
577 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
578 } else {
579 mayclone = mayclone && tundclone;
580 }
581
582 /*
583 * If tun cloning is enabled, only the superuser can create an
584 * interface.
585 */
586 if (!mayclone)
587 goto out;
588
589 if (u == -1)
590 append_unit = 1;
591 else
592 append_unit = 0;
593
594 drv = tuntap_driver_from_flags(tunflags);
595 if (drv == NULL)
596 goto out;
597
598 /* find any existing device, or allocate new unit number */
599 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
600 if (i) {
601 if (append_unit) {
602 namelen = snprintf(devname, sizeof(devname), "%s%d",
603 name, u);
604 name = devname;
605 }
606
607 i = tun_create_device(drv, u, cred, dev, name);
608 } else {
609 /* Consumed by the dev_clone invoker. */
610 dev_ref(*dev);
611 }
612 if (i == 0)
613 if_clone_create(name, namelen, NULL);
614 out:
615 CURVNET_RESTORE();
616 }
617
618 static void
tun_destroy(struct tuntap_softc * tp)619 tun_destroy(struct tuntap_softc *tp)
620 {
621
622 TUN_LOCK(tp);
623 tp->tun_flags |= TUN_DYING;
624 if (tp->tun_busy != 0)
625 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
626 else
627 TUN_UNLOCK(tp);
628
629 CURVNET_SET(TUN2IFP(tp)->if_vnet);
630
631 /* destroy_dev will take care of any alias. */
632 destroy_dev(tp->tun_dev);
633 seldrain(&tp->tun_rsel);
634 knlist_clear(&tp->tun_rsel.si_note, 0);
635 knlist_destroy(&tp->tun_rsel.si_note);
636 if ((tp->tun_flags & TUN_L2) != 0) {
637 ether_ifdetach(TUN2IFP(tp));
638 } else {
639 bpfdetach(TUN2IFP(tp));
640 if_detach(TUN2IFP(tp));
641 }
642 sx_xlock(&tun_ioctl_sx);
643 TUN2IFP(tp)->if_softc = NULL;
644 sx_xunlock(&tun_ioctl_sx);
645 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
646 if_free(TUN2IFP(tp));
647 mtx_destroy(&tp->tun_mtx);
648 cv_destroy(&tp->tun_cv);
649 free(tp, M_TUN);
650 CURVNET_RESTORE();
651 }
652
653 static int
tun_clone_destroy(struct if_clone * ifc __unused,struct ifnet * ifp,uint32_t flags)654 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp, uint32_t flags)
655 {
656 struct tuntap_softc *tp = ifp->if_softc;
657
658 mtx_lock(&tunmtx);
659 TAILQ_REMOVE(&tunhead, tp, tun_list);
660 mtx_unlock(&tunmtx);
661 tun_destroy(tp);
662
663 return (0);
664 }
665
666 static void
vnet_tun_init(const void * unused __unused)667 vnet_tun_init(const void *unused __unused)
668 {
669
670 for (u_int i = 0; i < NDRV; ++i) {
671 struct if_clone_addreq req = {
672 .match_f = tuntap_drivers[i].clone_match_fn,
673 .create_f = tuntap_drivers[i].clone_create_fn,
674 .destroy_f = tuntap_drivers[i].clone_destroy_fn,
675 };
676 V_tuntap_driver_cloners[i] =
677 ifc_attach_cloner(tuntap_drivers[i].cdevsw.d_name, &req);
678 };
679 }
680 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
681 vnet_tun_init, NULL);
682
683 static void
tun_uninit(const void * unused __unused)684 tun_uninit(const void *unused __unused)
685 {
686 struct tuntap_driver *drv;
687 struct tuntap_softc *tp;
688 int i;
689
690 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
691 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
692
693 CURVNET_SET(vnet0);
694 for (u_int i = 0; i < NDRV; i++) {
695 if_clone_detach(V_tuntap_driver_cloners[i]);
696 V_tuntap_driver_cloners[i] = NULL;
697 }
698 CURVNET_RESTORE();
699
700 if (tuntap_osd_jail_slot != 0)
701 osd_jail_deregister(tuntap_osd_jail_slot);
702
703 mtx_lock(&tunmtx);
704 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
705 TAILQ_REMOVE(&tunhead, tp, tun_list);
706 mtx_unlock(&tunmtx);
707 tun_destroy(tp);
708 mtx_lock(&tunmtx);
709 }
710 mtx_unlock(&tunmtx);
711 for (i = 0; i < nitems(tuntap_drivers); ++i) {
712 drv = &tuntap_drivers[i];
713 delete_unrhdr(drv->unrhdr);
714 clone_cleanup(&drv->clones);
715 }
716 mtx_destroy(&tunmtx);
717 }
718 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
719
720 static struct tuntap_driver *
tuntap_driver_from_ifnet(const struct ifnet * ifp)721 tuntap_driver_from_ifnet(const struct ifnet *ifp)
722 {
723 struct tuntap_driver *drv;
724 int i;
725
726 if (ifp == NULL)
727 return (NULL);
728
729 for (i = 0; i < nitems(tuntap_drivers); ++i) {
730 drv = &tuntap_drivers[i];
731 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
732 return (drv);
733 }
734
735 return (NULL);
736 }
737
738 /*
739 * Remove devices that were created by devfs cloning, as they hold references
740 * which prevent the prison from collapsing, in which state VNET sysuninits will
741 * not be invoked.
742 */
743 static int
tuntap_prison_remove(void * obj,void * data __unused)744 tuntap_prison_remove(void *obj, void *data __unused)
745 {
746 #ifdef VIMAGE
747 struct prison *pr;
748
749 pr = obj;
750 if (prison_owns_vnet(pr)) {
751 CURVNET_SET(pr->pr_vnet);
752 for (u_int i = 0; i < NDRV; i++) {
753 if_clone_detach(V_tuntap_driver_cloners[i]);
754 V_tuntap_driver_cloners[i] = NULL;
755 }
756 CURVNET_RESTORE();
757 }
758 #endif
759 return (0);
760 }
761
762 static int
tuntapmodevent(module_t mod,int type,void * data)763 tuntapmodevent(module_t mod, int type, void *data)
764 {
765 struct tuntap_driver *drv;
766 int i;
767
768 switch (type) {
769 case MOD_LOAD:
770 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
771 for (i = 0; i < nitems(tuntap_drivers); ++i) {
772 drv = &tuntap_drivers[i];
773 clone_setup(&drv->clones);
774 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
775 }
776 osd_method_t methods[PR_MAXMETHOD] = {
777 [PR_METHOD_REMOVE] = tuntap_prison_remove,
778 };
779 tuntap_osd_jail_slot = osd_jail_register(NULL, methods);
780 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
781 tunrename, 0, 1000);
782 if (arrival_tag == NULL)
783 return (ENOMEM);
784 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
785 if (clone_tag == NULL)
786 return (ENOMEM);
787 break;
788 case MOD_UNLOAD:
789 /* See tun_uninit(). */
790 break;
791 default:
792 return EOPNOTSUPP;
793 }
794 return 0;
795 }
796
797 static moduledata_t tuntap_mod = {
798 "if_tuntap",
799 tuntapmodevent,
800 0
801 };
802
803 /* We'll only ever have these two, so no need for a macro. */
804 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
805 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
806
807 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
808 MODULE_VERSION(if_tuntap, 1);
809 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
810 MODULE_VERSION(if_tun, 1);
811 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
812 MODULE_VERSION(if_tap, 1);
813
814 static int
tun_create_device(struct tuntap_driver * drv,int unit,struct ucred * cr,struct cdev ** dev,const char * name)815 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
816 struct cdev **dev, const char *name)
817 {
818 struct make_dev_args args;
819 struct tuntap_softc *tp;
820 int error;
821
822 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
823 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
824 cv_init(&tp->tun_cv, "tun_condvar");
825 tp->tun_flags = drv->ident_flags;
826 tp->tun_drv = drv;
827
828 make_dev_args_init(&args);
829 if (cr != NULL)
830 args.mda_flags = MAKEDEV_REF | MAKEDEV_CHECKNAME;
831 args.mda_devsw = &drv->cdevsw;
832 args.mda_cr = cr;
833 args.mda_uid = UID_UUCP;
834 args.mda_gid = GID_DIALER;
835 args.mda_mode = 0600;
836 args.mda_unit = unit;
837 args.mda_si_drv1 = tp;
838 error = make_dev_s(&args, dev, "%s", name);
839 if (error != 0) {
840 mtx_destroy(&tp->tun_mtx);
841 cv_destroy(&tp->tun_cv);
842 free(tp, M_TUN);
843 return (error);
844 }
845
846 KASSERT((*dev)->si_drv1 != NULL,
847 ("Failed to set si_drv1 at %s creation", name));
848 tp->tun_dev = *dev;
849 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
850 mtx_lock(&tunmtx);
851 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
852 mtx_unlock(&tunmtx);
853 return (0);
854 }
855
856 static void
tunstart(struct ifnet * ifp)857 tunstart(struct ifnet *ifp)
858 {
859 struct tuntap_softc *tp = ifp->if_softc;
860 struct mbuf *m;
861
862 TUNDEBUG(ifp, "starting\n");
863 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
864 IFQ_LOCK(&ifp->if_snd);
865 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
866 if (m == NULL) {
867 IFQ_UNLOCK(&ifp->if_snd);
868 return;
869 }
870 IFQ_UNLOCK(&ifp->if_snd);
871 }
872
873 TUN_LOCK(tp);
874 if (tp->tun_flags & TUN_RWAIT) {
875 tp->tun_flags &= ~TUN_RWAIT;
876 wakeup(tp);
877 }
878 selwakeuppri(&tp->tun_rsel, PZERO);
879 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
880 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
881 TUN_UNLOCK(tp);
882 pgsigio(&tp->tun_sigio, SIGIO, 0);
883 } else
884 TUN_UNLOCK(tp);
885 }
886
887 /*
888 * tunstart_l2
889 *
890 * queue packets from higher level ready to put out
891 */
892 static void
tunstart_l2(struct ifnet * ifp)893 tunstart_l2(struct ifnet *ifp)
894 {
895 struct tuntap_softc *tp = ifp->if_softc;
896
897 TUNDEBUG(ifp, "starting\n");
898
899 /*
900 * do not junk pending output if we are in VMnet mode.
901 * XXX: can this do any harm because of queue overflow?
902 */
903
904 TUN_LOCK(tp);
905 if (((tp->tun_flags & TUN_VMNET) == 0) &&
906 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
907 struct mbuf *m;
908
909 /* Unlocked read. */
910 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
911
912 for (;;) {
913 IF_DEQUEUE(&ifp->if_snd, m);
914 if (m != NULL) {
915 m_freem(m);
916 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
917 } else
918 break;
919 }
920 TUN_UNLOCK(tp);
921
922 return;
923 }
924
925 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
926
927 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
928 if (tp->tun_flags & TUN_RWAIT) {
929 tp->tun_flags &= ~TUN_RWAIT;
930 wakeup(tp);
931 }
932
933 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
934 TUN_UNLOCK(tp);
935 pgsigio(&tp->tun_sigio, SIGIO, 0);
936 TUN_LOCK(tp);
937 }
938
939 selwakeuppri(&tp->tun_rsel, PZERO);
940 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
941 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
942 }
943
944 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
945 TUN_UNLOCK(tp);
946 } /* tunstart_l2 */
947
948 static int
tap_transmit(struct ifnet * ifp,struct mbuf * m)949 tap_transmit(struct ifnet *ifp, struct mbuf *m)
950 {
951 int error;
952
953 BPF_MTAP(ifp, m);
954 IFQ_HANDOFF(ifp, m, error);
955 return (error);
956 }
957
958 static void
tuncreate(struct cdev * dev)959 tuncreate(struct cdev *dev)
960 {
961 struct tuntap_driver *drv;
962 struct tuntap_softc *tp;
963 struct ifnet *ifp;
964 struct ether_addr eaddr;
965 int iflags;
966 u_char type;
967
968 tp = dev->si_drv1;
969 KASSERT(tp != NULL,
970 ("si_drv1 should have been initialized at creation"));
971
972 drv = tp->tun_drv;
973 iflags = IFF_MULTICAST;
974 if ((tp->tun_flags & TUN_L2) != 0) {
975 type = IFT_ETHER;
976 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
977 } else {
978 type = IFT_PPP;
979 iflags |= IFF_POINTOPOINT;
980 }
981 ifp = tp->tun_ifp = if_alloc(type);
982 ifp->if_softc = tp;
983 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
984 ifp->if_ioctl = tunifioctl;
985 ifp->if_flags = iflags;
986 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
987 ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_MEXTPG;
988 if ((tp->tun_flags & TUN_L2) != 0)
989 ifp->if_capabilities |=
990 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_LRO;
991 ifp->if_capenable |= IFCAP_LINKSTATE | IFCAP_MEXTPG;
992
993 if ((tp->tun_flags & TUN_L2) != 0) {
994 ifp->if_init = tunifinit;
995 ifp->if_start = tunstart_l2;
996 ifp->if_transmit = tap_transmit;
997 ifp->if_qflush = if_qflush;
998
999 ether_gen_addr(ifp, &eaddr);
1000 ether_ifattach(ifp, eaddr.octet);
1001 } else {
1002 ifp->if_mtu = TUNMTU;
1003 ifp->if_start = tunstart;
1004 ifp->if_output = tunoutput;
1005
1006 ifp->if_snd.ifq_drv_maxlen = 0;
1007 IFQ_SET_READY(&ifp->if_snd);
1008
1009 if_attach(ifp);
1010 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
1011 }
1012
1013 TUN_LOCK(tp);
1014 tp->tun_flags |= TUN_INITED;
1015 TUN_UNLOCK(tp);
1016
1017 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
1018 ifp->if_xname, dev2unit(dev));
1019 }
1020
1021 static void
tunrename(void * arg __unused,struct ifnet * ifp)1022 tunrename(void *arg __unused, struct ifnet *ifp)
1023 {
1024 struct tuntap_softc *tp;
1025 int error;
1026
1027 if ((ifp->if_flags & IFF_RENAMING) == 0)
1028 return;
1029
1030 if (tuntap_driver_from_ifnet(ifp) == NULL)
1031 return;
1032
1033 /*
1034 * We need to grab the ioctl sx long enough to make sure the softc is
1035 * still there. If it is, we can safely try to busy the tun device.
1036 * The busy may fail if the device is currently dying, in which case
1037 * we do nothing. If it doesn't fail, the busy count stops the device
1038 * from dying until we've created the alias (that will then be
1039 * subsequently destroyed).
1040 */
1041 sx_xlock(&tun_ioctl_sx);
1042 tp = ifp->if_softc;
1043 if (tp == NULL) {
1044 sx_xunlock(&tun_ioctl_sx);
1045 return;
1046 }
1047 error = tun_busy(tp);
1048 sx_xunlock(&tun_ioctl_sx);
1049 if (error != 0)
1050 return;
1051 if (tp->tun_alias != NULL) {
1052 destroy_dev(tp->tun_alias);
1053 tp->tun_alias = NULL;
1054 }
1055
1056 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1057 goto out;
1058
1059 /*
1060 * Failure's ok, aliases are created on a best effort basis. If a
1061 * tun user/consumer decides to rename the interface to conflict with
1062 * another device (non-ifnet) on the system, we will assume they know
1063 * what they are doing. make_dev_alias_p won't touch tun_alias on
1064 * failure, so we use it but ignore the return value.
1065 */
1066 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1067 ifp->if_xname);
1068 out:
1069 tun_unbusy(tp);
1070 }
1071
1072 static int
tunopen(struct cdev * dev,int flag,int mode,struct thread * td)1073 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1074 {
1075 struct ifnet *ifp;
1076 struct tuntap_softc *tp;
1077 int error __diagused, tunflags;
1078
1079 tunflags = 0;
1080 CURVNET_SET(TD_TO_VNET(td));
1081 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1082 if (error != 0) {
1083 CURVNET_RESTORE();
1084 return (error); /* Shouldn't happen */
1085 }
1086
1087 tp = dev->si_drv1;
1088 KASSERT(tp != NULL,
1089 ("si_drv1 should have been initialized at creation"));
1090
1091 TUN_LOCK(tp);
1092 if ((tp->tun_flags & TUN_INITED) == 0) {
1093 TUN_UNLOCK(tp);
1094 CURVNET_RESTORE();
1095 return (ENXIO);
1096 }
1097 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1098 TUN_UNLOCK(tp);
1099 CURVNET_RESTORE();
1100 return (EBUSY);
1101 }
1102
1103 error = tun_busy_locked(tp);
1104 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1105 ifp = TUN2IFP(tp);
1106
1107 if ((tp->tun_flags & TUN_L2) != 0) {
1108 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1109 sizeof(tp->tun_ether.octet));
1110
1111 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1112 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1113
1114 if (tapuponopen)
1115 ifp->if_flags |= IFF_UP;
1116 }
1117
1118 tp->tun_pid = td->td_proc->p_pid;
1119 tp->tun_flags |= TUN_OPEN;
1120
1121 if_link_state_change(ifp, LINK_STATE_UP);
1122 TUNDEBUG(ifp, "open\n");
1123 TUN_UNLOCK(tp);
1124
1125 /*
1126 * This can fail with either ENOENT or EBUSY. This is in the middle of
1127 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1128 * the only cdevpriv dtor being set will be tundtor and the softc being
1129 * passed is constant for a given cdev. We ignore the possible error
1130 * because of this as either "unlikely" or "not actually a problem."
1131 */
1132 (void)devfs_set_cdevpriv(tp, tundtor);
1133 CURVNET_RESTORE();
1134 return (0);
1135 }
1136
1137 /*
1138 * tundtor - tear down the device - mark i/f down & delete
1139 * routing info
1140 */
1141 static void
tundtor(void * data)1142 tundtor(void *data)
1143 {
1144 struct proc *p;
1145 struct tuntap_softc *tp;
1146 struct ifnet *ifp;
1147 bool l2tun;
1148
1149 tp = data;
1150 p = curproc;
1151 ifp = TUN2IFP(tp);
1152
1153 TUN_LOCK(tp);
1154
1155 /*
1156 * Realistically, we can't be obstinate here. This only means that the
1157 * tuntap device was closed out of order, and the last closer wasn't the
1158 * controller. These are still good to know about, though, as software
1159 * should avoid multiple processes with a tuntap device open and
1160 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1161 * parent).
1162 */
1163 if (p->p_pid != tp->tun_pid) {
1164 log(LOG_INFO,
1165 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1166 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1167 }
1168
1169 /*
1170 * junk all pending output
1171 */
1172 CURVNET_SET(ifp->if_vnet);
1173
1174 l2tun = false;
1175 if ((tp->tun_flags & TUN_L2) != 0) {
1176 l2tun = true;
1177 IF_DRAIN(&ifp->if_snd);
1178 } else {
1179 IFQ_PURGE(&ifp->if_snd);
1180 }
1181
1182 /* For vmnet, we won't do most of the address/route bits */
1183 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1184 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1185 goto out;
1186 #if defined(INET) || defined(INET6)
1187 if (l2tun && tp->tun_lro_ready) {
1188 TUNDEBUG (ifp, "LRO disabled\n");
1189 tcp_lro_free(&tp->tun_lro);
1190 tp->tun_lro_ready = false;
1191 }
1192 #endif
1193 if (ifp->if_flags & IFF_UP) {
1194 TUN_UNLOCK(tp);
1195 if_down(ifp);
1196 TUN_LOCK(tp);
1197 }
1198
1199 /* Delete all addresses and routes which reference this interface. */
1200 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1201 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1202 TUN_UNLOCK(tp);
1203 if_purgeaddrs(ifp);
1204 TUN_LOCK(tp);
1205 }
1206
1207 out:
1208 if_link_state_change(ifp, LINK_STATE_DOWN);
1209 CURVNET_RESTORE();
1210
1211 funsetown(&tp->tun_sigio);
1212 selwakeuppri(&tp->tun_rsel, PZERO);
1213 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1214 TUNDEBUG (ifp, "closed\n");
1215 tp->tun_flags &= ~TUN_OPEN;
1216 tp->tun_pid = 0;
1217 tun_vnethdr_set(ifp, 0);
1218
1219 tun_unbusy_locked(tp);
1220 TUN_UNLOCK(tp);
1221 }
1222
1223 static void
tuninit(struct ifnet * ifp)1224 tuninit(struct ifnet *ifp)
1225 {
1226 struct tuntap_softc *tp = ifp->if_softc;
1227
1228 TUNDEBUG(ifp, "tuninit\n");
1229
1230 TUN_LOCK(tp);
1231 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1232 if ((tp->tun_flags & TUN_L2) == 0) {
1233 ifp->if_flags |= IFF_UP;
1234 getmicrotime(&ifp->if_lastchange);
1235 TUN_UNLOCK(tp);
1236 } else {
1237 #if defined(INET) || defined(INET6)
1238 if (tcp_lro_init(&tp->tun_lro) == 0) {
1239 TUNDEBUG(ifp, "LRO enabled\n");
1240 tp->tun_lro.ifp = ifp;
1241 tp->tun_lro_ready = true;
1242 } else {
1243 TUNDEBUG(ifp, "Could not enable LRO\n");
1244 tp->tun_lro_ready = false;
1245 }
1246 #endif
1247 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1248 TUN_UNLOCK(tp);
1249 /* attempt to start output */
1250 tunstart_l2(ifp);
1251 }
1252
1253 }
1254
1255 /*
1256 * Used only for l2 tunnel.
1257 */
1258 static void
tunifinit(void * xtp)1259 tunifinit(void *xtp)
1260 {
1261 struct tuntap_softc *tp;
1262
1263 tp = (struct tuntap_softc *)xtp;
1264 tuninit(tp->tun_ifp);
1265 }
1266
1267 /*
1268 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1269 * current value of ifp->if_capenable.
1270 */
1271 static void
tun_caps_changed(struct ifnet * ifp)1272 tun_caps_changed(struct ifnet *ifp)
1273 {
1274 uint64_t hwassist = 0;
1275
1276 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1277 if (ifp->if_capenable & IFCAP_TXCSUM)
1278 hwassist |= CSUM_TCP | CSUM_UDP;
1279 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1280 hwassist |= CSUM_TCP_IPV6
1281 | CSUM_UDP_IPV6;
1282 if (ifp->if_capenable & IFCAP_TSO4)
1283 hwassist |= CSUM_IP_TSO;
1284 if (ifp->if_capenable & IFCAP_TSO6)
1285 hwassist |= CSUM_IP6_TSO;
1286 ifp->if_hwassist = hwassist;
1287 }
1288
1289 /*
1290 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1291 * if_capabilities and if_capenable as needed.
1292 */
1293 static void
tun_vnethdr_set(struct ifnet * ifp,int vhdrlen)1294 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1295 {
1296 struct tuntap_softc *tp = ifp->if_softc;
1297
1298 TUN_LOCK_ASSERT(tp);
1299
1300 if (tp->tun_vhdrlen == vhdrlen)
1301 return;
1302
1303 /*
1304 * Update if_capabilities to reflect the
1305 * functionalities offered by the virtio-net
1306 * header.
1307 */
1308 if (vhdrlen != 0)
1309 ifp->if_capabilities |=
1310 TAP_VNET_HDR_CAPS;
1311 else
1312 ifp->if_capabilities &=
1313 ~TAP_VNET_HDR_CAPS;
1314 /*
1315 * Disable any capabilities that we don't
1316 * support anymore.
1317 */
1318 ifp->if_capenable &= ifp->if_capabilities;
1319 tun_caps_changed(ifp);
1320 tp->tun_vhdrlen = vhdrlen;
1321
1322 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1323 vhdrlen, ifp->if_capabilities);
1324 }
1325
1326 /*
1327 * Process an ioctl request.
1328 */
1329 static int
tunifioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1330 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1331 {
1332 struct ifreq *ifr = (struct ifreq *)data;
1333 struct tuntap_softc *tp;
1334 struct ifstat *ifs;
1335 struct ifmediareq *ifmr;
1336 int dummy, error = 0;
1337 bool l2tun;
1338
1339 ifmr = NULL;
1340 sx_xlock(&tun_ioctl_sx);
1341 tp = ifp->if_softc;
1342 if (tp == NULL) {
1343 error = ENXIO;
1344 goto bad;
1345 }
1346 l2tun = (tp->tun_flags & TUN_L2) != 0;
1347 switch(cmd) {
1348 case SIOCGIFSTATUS:
1349 ifs = (struct ifstat *)data;
1350 TUN_LOCK(tp);
1351 if (tp->tun_pid)
1352 snprintf(ifs->ascii, sizeof(ifs->ascii),
1353 "\tOpened by PID %d\n", tp->tun_pid);
1354 else
1355 ifs->ascii[0] = '\0';
1356 TUN_UNLOCK(tp);
1357 break;
1358 case SIOCSIFADDR:
1359 if (l2tun)
1360 error = ether_ioctl(ifp, cmd, data);
1361 else
1362 tuninit(ifp);
1363 if (error == 0)
1364 TUNDEBUG(ifp, "address set\n");
1365 break;
1366 case SIOCSIFMTU:
1367 ifp->if_mtu = ifr->ifr_mtu;
1368 TUNDEBUG(ifp, "mtu set\n");
1369 break;
1370 case SIOCSIFFLAGS:
1371 case SIOCADDMULTI:
1372 case SIOCDELMULTI:
1373 break;
1374 case SIOCGIFMEDIA:
1375 if (!l2tun) {
1376 error = EINVAL;
1377 break;
1378 }
1379
1380 ifmr = (struct ifmediareq *)data;
1381 dummy = ifmr->ifm_count;
1382 ifmr->ifm_count = 1;
1383 ifmr->ifm_status = IFM_AVALID;
1384 ifmr->ifm_active = IFM_ETHER | IFM_FDX | IFM_1000_T;
1385 if (tp->tun_flags & TUN_OPEN)
1386 ifmr->ifm_status |= IFM_ACTIVE;
1387 ifmr->ifm_current = ifmr->ifm_active;
1388 if (dummy >= 1) {
1389 int media = IFM_ETHER;
1390 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1391 }
1392 break;
1393 case SIOCSIFCAP:
1394 TUN_LOCK(tp);
1395 ifp->if_capenable = ifr->ifr_reqcap;
1396 tun_caps_changed(ifp);
1397 TUN_UNLOCK(tp);
1398 VLAN_CAPABILITIES(ifp);
1399 break;
1400 default:
1401 if (l2tun) {
1402 error = ether_ioctl(ifp, cmd, data);
1403 } else {
1404 error = EINVAL;
1405 }
1406 }
1407 bad:
1408 sx_xunlock(&tun_ioctl_sx);
1409 return (error);
1410 }
1411
1412 /*
1413 * tunoutput - queue packets from higher level ready to put out.
1414 */
1415 static int
tunoutput(struct ifnet * ifp,struct mbuf * m0,const struct sockaddr * dst,struct route * ro)1416 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1417 struct route *ro)
1418 {
1419 struct tuntap_softc *tp = ifp->if_softc;
1420 u_short cached_tun_flags;
1421 int error;
1422 u_int32_t af;
1423
1424 TUNDEBUG (ifp, "tunoutput\n");
1425
1426 #ifdef MAC
1427 error = mac_ifnet_check_transmit(ifp, m0);
1428 if (error) {
1429 m_freem(m0);
1430 return (error);
1431 }
1432 #endif
1433
1434 /* Could be unlocked read? */
1435 TUN_LOCK(tp);
1436 cached_tun_flags = tp->tun_flags;
1437 TUN_UNLOCK(tp);
1438 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1439 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1440 m_freem (m0);
1441 return (EHOSTDOWN);
1442 }
1443
1444 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1445 m_freem (m0);
1446 return (EHOSTDOWN);
1447 }
1448
1449 /* BPF writes need to be handled specially. */
1450 if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
1451 bcopy(dst->sa_data, &af, sizeof(af));
1452 else
1453 af = RO_GET_FAMILY(ro, dst);
1454
1455 BPF_MTAP2(ifp, &af, sizeof(af), m0);
1456
1457 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1458 if (cached_tun_flags & TUN_LMODE) {
1459 /* allocate space for sockaddr */
1460 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1461
1462 /* if allocation failed drop packet */
1463 if (m0 == NULL) {
1464 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1465 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1466 return (ENOBUFS);
1467 } else {
1468 bcopy(dst, m0->m_data, dst->sa_len);
1469 }
1470 }
1471
1472 if (cached_tun_flags & TUN_IFHEAD) {
1473 /* Prepend the address family */
1474 M_PREPEND(m0, 4, M_NOWAIT);
1475
1476 /* if allocation failed drop packet */
1477 if (m0 == NULL) {
1478 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1479 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1480 return (ENOBUFS);
1481 } else
1482 *(u_int32_t *)m0->m_data = htonl(af);
1483 } else {
1484 #ifdef INET
1485 if (af != AF_INET)
1486 #endif
1487 {
1488 m_freem(m0);
1489 return (EAFNOSUPPORT);
1490 }
1491 }
1492
1493 error = (ifp->if_transmit)(ifp, m0);
1494 if (error)
1495 return (ENOBUFS);
1496 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1497 return (0);
1498 }
1499
1500 /*
1501 * the cdevsw interface is now pretty minimal.
1502 */
1503 static int
tunioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)1504 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1505 struct thread *td)
1506 {
1507 struct ifreq ifr, *ifrp;
1508 struct tuntap_softc *tp = dev->si_drv1;
1509 struct ifnet *ifp = TUN2IFP(tp);
1510 struct tuninfo *tunp;
1511 int error, iflags, ival;
1512 bool l2tun;
1513
1514 l2tun = (tp->tun_flags & TUN_L2) != 0;
1515 if (l2tun) {
1516 /* tap specific ioctls */
1517 switch(cmd) {
1518 /* VMware/VMnet port ioctl's */
1519 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1520 defined(COMPAT_FREEBSD4)
1521 case _IO('V', 0):
1522 ival = IOCPARM_IVAL(data);
1523 data = (caddr_t)&ival;
1524 /* FALLTHROUGH */
1525 #endif
1526 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1527 iflags = *(int *)data;
1528 iflags &= TUN_VMIO_FLAG_MASK;
1529 iflags &= ~IFF_CANTCHANGE;
1530 iflags |= IFF_UP;
1531
1532 TUN_LOCK(tp);
1533 ifp->if_flags = iflags |
1534 (ifp->if_flags & IFF_CANTCHANGE);
1535 TUN_UNLOCK(tp);
1536
1537 return (0);
1538 case SIOCGIFADDR: /* get MAC address of the remote side */
1539 TUN_LOCK(tp);
1540 bcopy(&tp->tun_ether.octet, data,
1541 sizeof(tp->tun_ether.octet));
1542 TUN_UNLOCK(tp);
1543
1544 return (0);
1545 case SIOCSIFADDR: /* set MAC address of the remote side */
1546 TUN_LOCK(tp);
1547 bcopy(data, &tp->tun_ether.octet,
1548 sizeof(tp->tun_ether.octet));
1549 TUN_UNLOCK(tp);
1550
1551 return (0);
1552 case TAPSVNETHDR:
1553 ival = *(int *)data;
1554 if (ival != 0 &&
1555 ival != sizeof(struct virtio_net_hdr) &&
1556 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1557 return (EINVAL);
1558 }
1559 TUN_LOCK(tp);
1560 tun_vnethdr_set(ifp, ival);
1561 TUN_UNLOCK(tp);
1562
1563 return (0);
1564 case TAPGVNETHDR:
1565 TUN_LOCK(tp);
1566 *(int *)data = tp->tun_vhdrlen;
1567 TUN_UNLOCK(tp);
1568
1569 return (0);
1570 }
1571
1572 /* Fall through to the common ioctls if unhandled */
1573 } else {
1574 switch (cmd) {
1575 case TUNSLMODE:
1576 TUN_LOCK(tp);
1577 if (*(int *)data) {
1578 tp->tun_flags |= TUN_LMODE;
1579 tp->tun_flags &= ~TUN_IFHEAD;
1580 } else
1581 tp->tun_flags &= ~TUN_LMODE;
1582 TUN_UNLOCK(tp);
1583
1584 return (0);
1585 case TUNSIFHEAD:
1586 TUN_LOCK(tp);
1587 if (*(int *)data) {
1588 tp->tun_flags |= TUN_IFHEAD;
1589 tp->tun_flags &= ~TUN_LMODE;
1590 } else
1591 tp->tun_flags &= ~TUN_IFHEAD;
1592 TUN_UNLOCK(tp);
1593
1594 return (0);
1595 case TUNGIFHEAD:
1596 TUN_LOCK(tp);
1597 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1598 TUN_UNLOCK(tp);
1599
1600 return (0);
1601 case TUNSIFMODE:
1602 /* deny this if UP */
1603 if (TUN2IFP(tp)->if_flags & IFF_UP)
1604 return (EBUSY);
1605
1606 switch (*(int *)data & ~IFF_MULTICAST) {
1607 case IFF_POINTOPOINT:
1608 case IFF_BROADCAST:
1609 TUN_LOCK(tp);
1610 TUN2IFP(tp)->if_flags &=
1611 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1612 TUN2IFP(tp)->if_flags |= *(int *)data;
1613 TUN_UNLOCK(tp);
1614
1615 break;
1616 default:
1617 return (EINVAL);
1618 }
1619
1620 return (0);
1621 case TUNSIFPID:
1622 TUN_LOCK(tp);
1623 tp->tun_pid = curthread->td_proc->p_pid;
1624 TUN_UNLOCK(tp);
1625
1626 return (0);
1627 }
1628 /* Fall through to the common ioctls if unhandled */
1629 }
1630
1631 switch (cmd) {
1632 case TUNGIFNAME:
1633 ifrp = (struct ifreq *)data;
1634 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1635
1636 return (0);
1637 case TUNSIFINFO:
1638 tunp = (struct tuninfo *)data;
1639 if (TUN2IFP(tp)->if_type != tunp->type)
1640 return (EPROTOTYPE);
1641 TUN_LOCK(tp);
1642 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1643 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1644 ifr.ifr_mtu = tunp->mtu;
1645 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1646 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1647 (caddr_t)&ifr, td);
1648 CURVNET_RESTORE();
1649 if (error) {
1650 TUN_UNLOCK(tp);
1651 return (error);
1652 }
1653 }
1654 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1655 TUN_UNLOCK(tp);
1656 break;
1657 case TUNGIFINFO:
1658 tunp = (struct tuninfo *)data;
1659 TUN_LOCK(tp);
1660 tunp->mtu = TUN2IFP(tp)->if_mtu;
1661 tunp->type = TUN2IFP(tp)->if_type;
1662 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1663 TUN_UNLOCK(tp);
1664 break;
1665 case TUNSDEBUG:
1666 tundebug = *(int *)data;
1667 break;
1668 case TUNGDEBUG:
1669 *(int *)data = tundebug;
1670 break;
1671 case FIONBIO:
1672 break;
1673 case FIOASYNC:
1674 TUN_LOCK(tp);
1675 if (*(int *)data)
1676 tp->tun_flags |= TUN_ASYNC;
1677 else
1678 tp->tun_flags &= ~TUN_ASYNC;
1679 TUN_UNLOCK(tp);
1680 break;
1681 case FIONREAD:
1682 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1683 struct mbuf *mb;
1684 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1685 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1686 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1687 *(int *)data += mb->m_len;
1688 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1689 } else
1690 *(int *)data = 0;
1691 break;
1692 case FIOSETOWN:
1693 return (fsetown(*(int *)data, &tp->tun_sigio));
1694
1695 case FIOGETOWN:
1696 *(int *)data = fgetown(&tp->tun_sigio);
1697 return (0);
1698
1699 /* This is deprecated, FIOSETOWN should be used instead. */
1700 case TIOCSPGRP:
1701 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1702
1703 /* This is deprecated, FIOGETOWN should be used instead. */
1704 case TIOCGPGRP:
1705 *(int *)data = -fgetown(&tp->tun_sigio);
1706 return (0);
1707
1708 default:
1709 return (ENOTTY);
1710 }
1711 return (0);
1712 }
1713
1714 /*
1715 * The cdevsw read interface - reads a packet at a time, or at
1716 * least as much of a packet as can be read.
1717 */
1718 static int
tunread(struct cdev * dev,struct uio * uio,int flag)1719 tunread(struct cdev *dev, struct uio *uio, int flag)
1720 {
1721 struct tuntap_softc *tp = dev->si_drv1;
1722 struct ifnet *ifp = TUN2IFP(tp);
1723 struct mbuf *m;
1724 size_t len;
1725 int error = 0;
1726
1727 TUNDEBUG (ifp, "read\n");
1728 TUN_LOCK(tp);
1729 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1730 TUN_UNLOCK(tp);
1731 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1732 return (EHOSTDOWN);
1733 }
1734
1735 tp->tun_flags &= ~TUN_RWAIT;
1736
1737 for (;;) {
1738 IFQ_DEQUEUE(&ifp->if_snd, m);
1739 if (m != NULL)
1740 break;
1741 if (flag & O_NONBLOCK) {
1742 TUN_UNLOCK(tp);
1743 return (EWOULDBLOCK);
1744 }
1745 tp->tun_flags |= TUN_RWAIT;
1746 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | PZERO,
1747 "tunread", 0);
1748 if (error != 0) {
1749 TUN_UNLOCK(tp);
1750 return (error);
1751 }
1752 }
1753 TUN_UNLOCK(tp);
1754
1755 len = min(tp->tun_vhdrlen, uio->uio_resid);
1756 if (len > 0) {
1757 struct virtio_net_hdr_mrg_rxbuf vhdr;
1758
1759 bzero(&vhdr, sizeof(vhdr));
1760 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1761 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1762 }
1763
1764 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1765 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1766 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1767 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1768 vhdr.hdr.csum_offset);
1769 error = uiomove(&vhdr, len, uio);
1770 }
1771 if (error == 0)
1772 error = m_mbuftouio(uio, m, 0);
1773 m_freem(m);
1774 return (error);
1775 }
1776
1777 static int
tunwrite_l2(struct tuntap_softc * tp,struct mbuf * m,struct virtio_net_hdr_mrg_rxbuf * vhdr)1778 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1779 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1780 {
1781 struct epoch_tracker et;
1782 struct ether_header *eh;
1783 struct ifnet *ifp;
1784
1785 ifp = TUN2IFP(tp);
1786
1787 /*
1788 * Only pass a unicast frame to ether_input(), if it would
1789 * actually have been received by non-virtual hardware.
1790 */
1791 if (m->m_len < sizeof(struct ether_header)) {
1792 m_freem(m);
1793 return (0);
1794 }
1795
1796 eh = mtod(m, struct ether_header *);
1797
1798 if ((ifp->if_flags & IFF_PROMISC) == 0 &&
1799 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1800 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1801 m_freem(m);
1802 return (0);
1803 }
1804
1805 if (vhdr != NULL) {
1806 if (virtio_net_rx_csum(m, &vhdr->hdr)) {
1807 m_freem(m);
1808 return (0);
1809 }
1810 } else {
1811 switch (ntohs(eh->ether_type)) {
1812 #ifdef INET
1813 case ETHERTYPE_IP:
1814 if (ifp->if_capenable & IFCAP_RXCSUM) {
1815 m->m_pkthdr.csum_flags |=
1816 CSUM_IP_CHECKED | CSUM_IP_VALID |
1817 CSUM_DATA_VALID | CSUM_SCTP_VALID |
1818 CSUM_PSEUDO_HDR;
1819 m->m_pkthdr.csum_data = 0xffff;
1820 }
1821 break;
1822 #endif
1823 #ifdef INET6
1824 case ETHERTYPE_IPV6:
1825 if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
1826 m->m_pkthdr.csum_flags |=
1827 CSUM_DATA_VALID_IPV6 | CSUM_SCTP_VALID |
1828 CSUM_PSEUDO_HDR;
1829 m->m_pkthdr.csum_data = 0xffff;
1830 }
1831 break;
1832 #endif
1833 }
1834 }
1835
1836 /* Pass packet up to parent. */
1837 CURVNET_SET(ifp->if_vnet);
1838 NET_EPOCH_ENTER(et);
1839 #if defined(INET) || defined(INET6)
1840 if (tp->tun_lro_ready && ifp->if_capenable & IFCAP_LRO &&
1841 tcp_lro_rx(&tp->tun_lro, m, 0) == 0)
1842 tcp_lro_flush_all(&tp->tun_lro);
1843 else
1844 #endif
1845 (*ifp->if_input)(ifp, m);
1846 NET_EPOCH_EXIT(et);
1847 CURVNET_RESTORE();
1848 /* ibytes are counted in parent */
1849 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1850 return (0);
1851 }
1852
1853 static int
tunwrite_l3(struct tuntap_softc * tp,struct mbuf * m)1854 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1855 {
1856 struct epoch_tracker et;
1857 struct ifnet *ifp;
1858 int family, isr;
1859
1860 ifp = TUN2IFP(tp);
1861 /* Could be unlocked read? */
1862 TUN_LOCK(tp);
1863 if (tp->tun_flags & TUN_IFHEAD) {
1864 TUN_UNLOCK(tp);
1865 if (m->m_len < sizeof(family) &&
1866 (m = m_pullup(m, sizeof(family))) == NULL)
1867 return (ENOBUFS);
1868 family = ntohl(*mtod(m, u_int32_t *));
1869 m_adj(m, sizeof(family));
1870 } else {
1871 TUN_UNLOCK(tp);
1872 family = AF_INET;
1873 }
1874
1875 BPF_MTAP2(ifp, &family, sizeof(family), m);
1876
1877 switch (family) {
1878 #ifdef INET
1879 case AF_INET:
1880 isr = NETISR_IP;
1881 break;
1882 #endif
1883 #ifdef INET6
1884 case AF_INET6:
1885 isr = NETISR_IPV6;
1886 break;
1887 #endif
1888 default:
1889 m_freem(m);
1890 return (EAFNOSUPPORT);
1891 }
1892 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1893 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1894 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1895 CURVNET_SET(ifp->if_vnet);
1896 M_SETFIB(m, ifp->if_fib);
1897 NET_EPOCH_ENTER(et);
1898 netisr_dispatch(isr, m);
1899 NET_EPOCH_EXIT(et);
1900 CURVNET_RESTORE();
1901 return (0);
1902 }
1903
1904 /*
1905 * the cdevsw write interface - an atomic write is a packet - or else!
1906 */
1907 static int
tunwrite(struct cdev * dev,struct uio * uio,int flag)1908 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1909 {
1910 struct virtio_net_hdr_mrg_rxbuf vhdr;
1911 struct tuntap_softc *tp;
1912 struct ifnet *ifp;
1913 struct mbuf *m;
1914 uint32_t mru;
1915 int align, vhdrlen, error;
1916 bool l2tun;
1917
1918 tp = dev->si_drv1;
1919 ifp = TUN2IFP(tp);
1920 TUNDEBUG(ifp, "tunwrite\n");
1921 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1922 /* ignore silently */
1923 return (0);
1924
1925 if (uio->uio_resid == 0)
1926 return (0);
1927
1928 l2tun = (tp->tun_flags & TUN_L2) != 0;
1929 mru = l2tun ? TAPMRU : TUNMRU;
1930 vhdrlen = tp->tun_vhdrlen;
1931 align = 0;
1932 if (l2tun) {
1933 align = ETHER_ALIGN;
1934 mru += vhdrlen;
1935 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1936 mru += sizeof(uint32_t); /* family */
1937 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1938 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1939 return (EIO);
1940 }
1941
1942 if (vhdrlen > 0) {
1943 error = uiomove(&vhdr, vhdrlen, uio);
1944 if (error != 0)
1945 return (error);
1946 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1947 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1948 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1949 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1950 vhdr.hdr.csum_offset);
1951 }
1952
1953 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1954 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1955 return (ENOBUFS);
1956 }
1957
1958 m->m_pkthdr.rcvif = ifp;
1959 #ifdef MAC
1960 mac_ifnet_create_mbuf(ifp, m);
1961 #endif
1962
1963 if (l2tun)
1964 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1965
1966 return (tunwrite_l3(tp, m));
1967 }
1968
1969 /*
1970 * tunpoll - the poll interface, this is only useful on reads
1971 * really. The write detect always returns true, write never blocks
1972 * anyway, it either accepts the packet or drops it.
1973 */
1974 static int
tunpoll(struct cdev * dev,int events,struct thread * td)1975 tunpoll(struct cdev *dev, int events, struct thread *td)
1976 {
1977 struct tuntap_softc *tp = dev->si_drv1;
1978 struct ifnet *ifp = TUN2IFP(tp);
1979 int revents = 0;
1980
1981 TUNDEBUG(ifp, "tunpoll\n");
1982
1983 if (events & (POLLIN | POLLRDNORM)) {
1984 IFQ_LOCK(&ifp->if_snd);
1985 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1986 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1987 revents |= events & (POLLIN | POLLRDNORM);
1988 } else {
1989 TUNDEBUG(ifp, "tunpoll waiting\n");
1990 selrecord(td, &tp->tun_rsel);
1991 }
1992 IFQ_UNLOCK(&ifp->if_snd);
1993 }
1994 revents |= events & (POLLOUT | POLLWRNORM);
1995
1996 return (revents);
1997 }
1998
1999 /*
2000 * tunkqfilter - support for the kevent() system call.
2001 */
2002 static int
tunkqfilter(struct cdev * dev,struct knote * kn)2003 tunkqfilter(struct cdev *dev, struct knote *kn)
2004 {
2005 struct tuntap_softc *tp = dev->si_drv1;
2006 struct ifnet *ifp = TUN2IFP(tp);
2007
2008 switch(kn->kn_filter) {
2009 case EVFILT_READ:
2010 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
2011 ifp->if_xname, dev2unit(dev));
2012 kn->kn_fop = &tun_read_filterops;
2013 break;
2014
2015 case EVFILT_WRITE:
2016 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
2017 ifp->if_xname, dev2unit(dev));
2018 kn->kn_fop = &tun_write_filterops;
2019 break;
2020
2021 default:
2022 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
2023 ifp->if_xname, dev2unit(dev));
2024 return(EINVAL);
2025 }
2026
2027 kn->kn_hook = tp;
2028 knlist_add(&tp->tun_rsel.si_note, kn, 0);
2029
2030 return (0);
2031 }
2032
2033 /*
2034 * Return true of there is data in the interface queue.
2035 */
2036 static int
tunkqread(struct knote * kn,long hint)2037 tunkqread(struct knote *kn, long hint)
2038 {
2039 int ret;
2040 struct tuntap_softc *tp = kn->kn_hook;
2041 struct cdev *dev = tp->tun_dev;
2042 struct ifnet *ifp = TUN2IFP(tp);
2043
2044 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2045 TUNDEBUG(ifp,
2046 "%s have data in the queue. Len = %d, minor = %#x\n",
2047 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2048 ret = 1;
2049 } else {
2050 TUNDEBUG(ifp,
2051 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2052 dev2unit(dev));
2053 ret = 0;
2054 }
2055
2056 return (ret);
2057 }
2058
2059 /*
2060 * Always can write, always return MTU in kn->data.
2061 */
2062 static int
tunkqwrite(struct knote * kn,long hint)2063 tunkqwrite(struct knote *kn, long hint)
2064 {
2065 struct tuntap_softc *tp = kn->kn_hook;
2066 struct ifnet *ifp = TUN2IFP(tp);
2067
2068 kn->kn_data = ifp->if_mtu;
2069
2070 return (1);
2071 }
2072
2073 static void
tunkqdetach(struct knote * kn)2074 tunkqdetach(struct knote *kn)
2075 {
2076 struct tuntap_softc *tp = kn->kn_hook;
2077
2078 knlist_remove(&tp->tun_rsel.si_note, kn, 0);
2079 }
2080