xref: /freebsd/sys/net/if_ovpn.c (revision 58d84ef87094691bf1ad9608964ec85f120cd34a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021-2022 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 #include "opt_inet.h"
29 #include "opt_inet6.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/buf_ring.h>
34 #include <sys/epoch.h>
35 #include <sys/file.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/module.h>
41 #include <sys/nv.h>
42 #include <sys/priv.h>
43 #include <sys/protosw.h>
44 #include <sys/rmlock.h>
45 #include <sys/sdt.h>
46 #include <sys/smp.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/time.h>
52 
53 #include <machine/atomic.h>
54 
55 #include <net/bpf.h>
56 #include <net/if.h>
57 #include <net/if_clone.h>
58 #include <net/if_types.h>
59 #include <net/if_var.h>
60 #include <net/netisr.h>
61 #include <net/route/nhop.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/in_fib.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip6.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/in6_fib.h>
73 
74 #include <machine/in_cksum.h>
75 
76 #include <opencrypto/cryptodev.h>
77 
78 #include "if_ovpn.h"
79 
80 struct ovpn_kkey_dir {
81 	int			refcount;
82 	uint8_t			key[32];
83 	uint8_t			keylen;
84 	uint8_t			nonce[8];
85 	uint8_t			noncelen;
86 	enum ovpn_key_cipher	cipher;
87 	crypto_session_t	cryptoid;
88 
89 	struct mtx		replay_mtx;
90 	/*
91 	 * Last seen gapless sequence number. New rx seq numbers must be
92 	 * strictly higher than this.
93 	 */
94 	uint32_t		rx_seq;
95 	/* Seen packets, relative to rx_seq. bit(0) will always be 0. */
96 	uint64_t		rx_window;
97 };
98 
99 struct ovpn_kkey {
100 	struct ovpn_kkey_dir	*encrypt;
101 	struct ovpn_kkey_dir	*decrypt;
102 	uint8_t			 keyid;
103 	uint32_t		 peerid;
104 };
105 
106 struct ovpn_keepalive {
107 	uint32_t	interval;
108 	uint32_t	timeout;
109 };
110 
111 struct ovpn_wire_header {
112 	uint32_t	 opcode; /* opcode, key id, peer id */
113 	uint32_t	 seq;
114 	uint8_t		 auth_tag[16];
115 };
116 
117 struct ovpn_notification {
118 	enum ovpn_notif_type	type;
119 	uint32_t		peerid;
120 };
121 
122 struct ovpn_softc;
123 
124 struct ovpn_kpeer {
125 	int			 refcount;
126 	uint32_t		 peerid;
127 
128 	struct ovpn_softc	*sc;
129 	struct sockaddr_storage	 local;
130 	struct sockaddr_storage	 remote;
131 
132 	struct in_addr		 vpn4;
133 	struct in6_addr		 vpn6;
134 
135 	struct ovpn_kkey	 keys[2];
136 	uint32_t		 tx_seq;
137 
138 	struct ovpn_keepalive	 keepalive;
139 	uint32_t		*last_active;
140 	struct callout		 ping_send;
141 	struct callout		 ping_rcv;
142 };
143 
144 #define OVPN_MAX_PEERS	128
145 
146 struct ovpn_counters {
147 	uint64_t	lost_ctrl_pkts_in;
148 	uint64_t	lost_ctrl_pkts_out;
149 	uint64_t	lost_data_pkts_in;
150 	uint64_t	lost_data_pkts_out;
151 	uint64_t	nomem_data_pkts_in;
152 	uint64_t	nomem_data_pkts_out;
153 	uint64_t	received_ctrl_pkts;
154 	uint64_t	received_data_pkts;
155 	uint64_t	sent_ctrl_pkts;
156 	uint64_t	sent_data_pkts;
157 
158 	uint64_t	transport_bytes_sent;
159 	uint64_t	transport_bytes_received;
160 	uint64_t	tunnel_bytes_sent;
161 	uint64_t	tunnel_bytes_received;
162 };
163 #define OVPN_COUNTER_SIZE (sizeof(struct ovpn_counters)/sizeof(uint64_t))
164 
165 struct ovpn_softc {
166 	int			 refcount;
167 	struct rmlock		 lock;
168 	struct ifnet		*ifp;
169 	struct socket		*so;
170 	int			 peercount;
171 	struct ovpn_kpeer	*peers[OVPN_MAX_PEERS]; /* XXX Hard limit for now? */
172 
173 	/* Pending packets */
174 	struct buf_ring		*rxring;
175 	struct buf_ring		*notifring;
176 
177 	counter_u64_t 		 counters[OVPN_COUNTER_SIZE];
178 
179 	struct epoch_context	 epoch_ctx;
180 };
181 
182 static struct ovpn_kpeer *ovpn_find_peer(struct ovpn_softc *, uint32_t);
183 static bool ovpn_udp_input(struct mbuf *, int, struct inpcb *,
184     const struct sockaddr *, void *);
185 static int ovpn_transmit_to_peer(struct ifnet *, struct mbuf *,
186     struct ovpn_kpeer *, struct rm_priotracker *);
187 static int ovpn_encap(struct ovpn_softc *, uint32_t, struct mbuf *);
188 static int ovpn_get_af(struct mbuf *);
189 static void ovpn_free_kkey_dir(struct ovpn_kkey_dir *);
190 static bool ovpn_check_replay(struct ovpn_kkey_dir *, uint32_t);
191 
192 #define OVPN_MTU_MIN		576
193 #define OVPN_MTU_MAX		(IP_MAXPACKET - sizeof(struct ip) - \
194     sizeof(struct udphdr) - sizeof(struct ovpn_wire_header))
195 
196 #define OVPN_OP_DATA_V2		0x09
197 #define OVPN_OP_SHIFT		3
198 
199 VNET_DEFINE_STATIC(struct if_clone *, ovpn_cloner);
200 #define	V_ovpn_cloner	VNET(ovpn_cloner)
201 
202 #define OVPN_RLOCK_TRACKER	struct rm_priotracker _ovpn_lock_tracker; \
203     struct rm_priotracker *_ovpn_lock_trackerp = &_ovpn_lock_tracker
204 #define OVPN_RLOCK(sc)		rm_rlock(&(sc)->lock, _ovpn_lock_trackerp)
205 #define OVPN_RUNLOCK(sc)	rm_runlock(&(sc)->lock, _ovpn_lock_trackerp)
206 #define OVPN_WLOCK(sc)		rm_wlock(&(sc)->lock)
207 #define OVPN_WUNLOCK(sc)	rm_wunlock(&(sc)->lock)
208 #define OVPN_ASSERT(sc)		rm_assert(&(sc)->lock, RA_LOCKED)
209 #define OVPN_RASSERT(sc)	rm_assert(&(sc)->lock, RA_RLOCKED)
210 #define OVPN_WASSERT(sc)	rm_assert(&(sc)->lock, RA_WLOCKED)
211 #define OVPN_UNLOCK_ASSERT(sc)	rm_assert(&(sc)->lock, RA_UNLOCKED)
212 
213 #define OVPN_COUNTER_ADD(sc, name, val)	\
214 	counter_u64_add(sc->counters[offsetof(struct ovpn_counters, name) / \
215 	    sizeof(uint64_t)], val)
216 
217 #define TO_IN(x)		((struct sockaddr_in *)(x))
218 #define TO_IN6(x)		((struct sockaddr_in6 *)(x))
219 
220 SDT_PROVIDER_DEFINE(if_ovpn);
221 SDT_PROBE_DEFINE1(if_ovpn, tx, transmit, start, "struct mbuf *");
222 SDT_PROBE_DEFINE2(if_ovpn, tx, route, ip4, "struct in_addr *", "struct ovpn_kpeer *");
223 SDT_PROBE_DEFINE2(if_ovpn, tx, route, ip6, "struct in6_addr *", "struct ovpn_kpeer *");
224 
225 static const char ovpnname[] = "ovpn";
226 static const char ovpngroupname[] = "openvpn";
227 
228 static MALLOC_DEFINE(M_OVPN, ovpnname, "OpenVPN DCO Interface");
229 
230 SYSCTL_DECL(_net_link);
231 static SYSCTL_NODE(_net_link, IFT_OTHER, openvpn, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
232     "OpenVPN DCO Interface");
233 VNET_DEFINE_STATIC(int, replay_protection) = 0;
234 #define	V_replay_protection	VNET(replay_protection)
235 SYSCTL_INT(_net_link_openvpn, OID_AUTO, replay_protection, CTLFLAG_VNET | CTLFLAG_RW,
236     &VNET_NAME(replay_protection), 0, "Validate sequence numbers");
237 
238 VNET_DEFINE_STATIC(int, async_crypto);
239 #define	V_async_crypto		VNET(async_crypto)
240 SYSCTL_INT(_net_link_openvpn, OID_AUTO, async_crypto,
241 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(async_crypto), 0,
242 	"Use asynchronous mode to parallelize crypto jobs.");
243 
244 VNET_DEFINE_STATIC(int, async_netisr_queue);
245 #define	V_async_netisr_queue		VNET(async_netisr_queue)
246 SYSCTL_INT(_net_link_openvpn, OID_AUTO, netisr_queue,
247 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(async_netisr_queue), 0,
248 	"Use netisr_queue() rather than netisr_dispatch().");
249 
250 static struct ovpn_kpeer *
251 ovpn_find_peer(struct ovpn_softc *sc, uint32_t peerid)
252 {
253 	struct ovpn_kpeer *p = NULL;
254 
255 	OVPN_ASSERT(sc);
256 
257 	for (int i = 0; i < OVPN_MAX_PEERS; i++) {
258 		p = sc->peers[i];
259 		if (p == NULL)
260 			continue;
261 
262 		if (p->peerid == peerid) {
263 			MPASS(p->sc == sc);
264 			break;
265 		}
266 	}
267 
268 	return (p);
269 }
270 
271 static struct ovpn_kpeer *
272 ovpn_find_only_peer(struct ovpn_softc *sc)
273 {
274 	OVPN_ASSERT(sc);
275 
276 	for (int i = 0; i < OVPN_MAX_PEERS; i++) {
277 		if (sc->peers[i] == NULL)
278 			continue;
279 		return (sc->peers[i]);
280 	}
281 
282 	MPASS(false);
283 
284 	return (NULL);
285 }
286 
287 static uint16_t
288 ovpn_get_port(struct sockaddr_storage *s)
289 {
290 	switch (s->ss_family) {
291 	case AF_INET: {
292 		struct sockaddr_in *in = (struct sockaddr_in *)s;
293 		return (in->sin_port);
294 	}
295 	case AF_INET6: {
296 		struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)s;
297 		return (in6->sin6_port);
298 	}
299 	default:
300 		panic("Unsupported address family %d", s->ss_family);
301 	}
302 }
303 
304 static int
305 ovpn_nvlist_to_sockaddr(const nvlist_t *nvl, struct sockaddr_storage *sa)
306 {
307 	int af;
308 
309 	if (! nvlist_exists_number(nvl, "af"))
310 		return (EINVAL);
311 	if (! nvlist_exists_binary(nvl, "address"))
312 		return (EINVAL);
313 	if (! nvlist_exists_number(nvl, "port"))
314 		return (EINVAL);
315 
316 	af = nvlist_get_number(nvl, "af");
317 
318 	switch (af) {
319 #ifdef INET
320 	case AF_INET: {
321 		struct sockaddr_in *in = (struct sockaddr_in *)sa;
322 		size_t len;
323 		const void *addr = nvlist_get_binary(nvl, "address", &len);
324 		in->sin_family = af;
325 		if (len != sizeof(in->sin_addr))
326 			return (EINVAL);
327 
328 		memcpy(&in->sin_addr, addr, sizeof(in->sin_addr));
329 		in->sin_port = nvlist_get_number(nvl, "port");
330 		break;
331 	}
332 #endif
333 #ifdef INET6
334 	case AF_INET6: {
335 		struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
336 		size_t len;
337 		const void *addr = nvlist_get_binary(nvl, "address", &len);
338 		in6->sin6_family = af;
339 		if (len != sizeof(in6->sin6_addr))
340 			return (EINVAL);
341 
342 		memcpy(&in6->sin6_addr, addr, sizeof(in6->sin6_addr));
343 		in6->sin6_port = nvlist_get_number(nvl, "port");
344 		break;
345 	}
346 #endif
347 	default:
348 		return (EINVAL);
349 	}
350 
351 	return (0);
352 }
353 
354 static bool
355 ovpn_has_peers(struct ovpn_softc *sc)
356 {
357 	OVPN_ASSERT(sc);
358 
359 	return (sc->peercount > 0);
360 }
361 
362 static void
363 ovpn_rele_so(struct ovpn_softc *sc, struct ovpn_kpeer *peer)
364 {
365 	bool has_peers;
366 
367 	OVPN_WASSERT(sc);
368 
369 	if (sc->so == NULL)
370 		return;
371 
372 	has_peers = ovpn_has_peers(sc);
373 
374 	/* Only remove the tunnel function if we're releasing the socket for
375 	 * the last peer. */
376 	if (! has_peers)
377 		(void)udp_set_kernel_tunneling(sc->so, NULL, NULL, NULL);
378 
379 	sorele(sc->so);
380 
381 	if (! has_peers)
382 		sc->so = NULL;
383 }
384 
385 static void
386 ovpn_notify_del_peer(struct ovpn_softc *sc, struct ovpn_kpeer *peer)
387 {
388 	struct ovpn_notification *n;
389 
390 	OVPN_WASSERT(sc);
391 
392 	n = malloc(sizeof(*n), M_OVPN, M_NOWAIT);
393 	if (n == NULL)
394 		return;
395 
396 	n->peerid = peer->peerid;
397 	n->type = OVPN_NOTIF_DEL_PEER;
398 	if (buf_ring_enqueue(sc->notifring, n) != 0) {
399 		free(n, M_OVPN);
400 	} else if (sc->so != NULL) {
401 		/* Wake up userspace */
402 		sc->so->so_error = EAGAIN;
403 		sorwakeup(sc->so);
404 		sowwakeup(sc->so);
405 	}
406 }
407 
408 static void
409 ovpn_peer_release_ref(struct ovpn_kpeer *peer, bool locked)
410 {
411 	struct ovpn_softc *sc;
412 
413 	atomic_add_int(&peer->refcount, -1);
414 
415 	if (atomic_load_int(&peer->refcount) > 0)
416 		return;
417 
418 	sc = peer->sc;
419 
420 	if (! locked) {
421 		OVPN_WLOCK(sc);
422 
423 		/* Might have changed before we acquired the lock. */
424 		if (atomic_load_int(&peer->refcount) > 0) {
425 			OVPN_WUNLOCK(sc);
426 			return;
427 		}
428 	}
429 
430 	/* The peer should have been removed from the list already. */
431 	MPASS(ovpn_find_peer(sc, peer->peerid) == NULL);
432 
433 	ovpn_notify_del_peer(sc, peer);
434 
435 	for (int i = 0; i < 2; i++) {
436 		ovpn_free_kkey_dir(peer->keys[i].encrypt);
437 		ovpn_free_kkey_dir(peer->keys[i].decrypt);
438 	}
439 
440 	ovpn_rele_so(sc, peer);
441 
442 	callout_stop(&peer->ping_send);
443 	callout_stop(&peer->ping_rcv);
444 	uma_zfree_pcpu(pcpu_zone_4, peer->last_active);
445 	free(peer, M_OVPN);
446 
447 	if (! locked)
448 		OVPN_WUNLOCK(sc);
449 }
450 
451 static int
452 ovpn_new_peer(struct ifnet *ifp, const nvlist_t *nvl)
453 {
454 #ifdef INET6
455 	struct epoch_tracker et;
456 #endif
457 	struct sockaddr_storage remote;
458 	struct ovpn_kpeer *peer = NULL;
459 	struct file *fp = NULL;
460 	struct sockaddr *name = NULL;
461 	struct ovpn_softc *sc = ifp->if_softc;
462 	struct thread *td = curthread;
463 	struct socket *so = NULL;
464 	int fd;
465 	uint32_t peerid;
466 	int ret = 0, i;
467 
468 	if (nvl == NULL)
469 		return (EINVAL);
470 
471 	if (! nvlist_exists_number(nvl, "peerid"))
472 		return (EINVAL);
473 
474 	if (! nvlist_exists_number(nvl, "fd"))
475 		return (EINVAL);
476 
477 	if (! nvlist_exists_nvlist(nvl, "remote"))
478 		return (EINVAL);
479 
480 	peerid = nvlist_get_number(nvl, "peerid");
481 
482 	ret = ovpn_nvlist_to_sockaddr(nvlist_get_nvlist(nvl, "remote"),
483 	    &remote);
484 	if (ret != 0)
485 		return (ret);
486 
487 	fd = nvlist_get_number(nvl, "fd");
488 
489 	/* Look up the userspace process and use the fd to find the socket. */
490 	ret = getsock(td, fd, &cap_connect_rights, &fp);
491 	if (ret != 0)
492 		return (ret);
493 
494 	so = fp->f_data;
495 
496 	peer = malloc(sizeof(*peer), M_OVPN, M_WAITOK | M_ZERO);
497 	peer->peerid = peerid;
498 	peer->sc = sc;
499 	peer->tx_seq = 1;
500 	peer->refcount = 1;
501 	peer->last_active = uma_zalloc_pcpu(pcpu_zone_4, M_WAITOK | M_ZERO);
502 
503 	if (nvlist_exists_binary(nvl, "vpn_ipv4")) {
504 		size_t len;
505 		const void *addr = nvlist_get_binary(nvl, "vpn_ipv4", &len);
506 		if (len != sizeof(peer->vpn4)) {
507 			ret = EINVAL;
508 			goto error;
509 		}
510 		memcpy(&peer->vpn4, addr, len);
511 	}
512 
513 	if (nvlist_exists_binary(nvl, "vpn_ipv6")) {
514 		size_t len;
515 		const void *addr = nvlist_get_binary(nvl, "vpn_ipv6", &len);
516 		if (len != sizeof(peer->vpn6)) {
517 			ret = EINVAL;
518 			goto error;
519 		}
520 		memcpy(&peer->vpn6, addr, len);
521 	}
522 
523 	callout_init_rm(&peer->ping_send, &sc->lock, CALLOUT_SHAREDLOCK);
524 	callout_init_rm(&peer->ping_rcv, &sc->lock, 0);
525 
526 	ret = so->so_proto->pr_sockaddr(so, &name);
527 	if (ret)
528 		goto error;
529 
530 	if (ovpn_get_port((struct sockaddr_storage *)name) == 0) {
531 		ret = EINVAL;
532 		goto error;
533 	}
534 	if (name->sa_family != remote.ss_family) {
535 		ret = EINVAL;
536 		goto error;
537 	}
538 
539 	memcpy(&peer->local, name, name->sa_len);
540 	memcpy(&peer->remote, &remote, sizeof(remote));
541 	free(name, M_SONAME);
542 	name = NULL;
543 
544 	if (peer->local.ss_family == AF_INET6 &&
545 	    IN6_IS_ADDR_V4MAPPED(&TO_IN6(&peer->remote)->sin6_addr)) {
546 		/* V4 mapped address, so treat this as v4, not v6. */
547 		in6_sin6_2_sin_in_sock((struct sockaddr *)&peer->local);
548 		in6_sin6_2_sin_in_sock((struct sockaddr *)&peer->remote);
549 	}
550 
551 #ifdef INET6
552 	if (peer->local.ss_family == AF_INET6 &&
553 	    IN6_IS_ADDR_UNSPECIFIED(&TO_IN6(&peer->local)->sin6_addr)) {
554 		NET_EPOCH_ENTER(et);
555 		ret = in6_selectsrc_addr(curthread->td_proc->p_fibnum,
556 		    &TO_IN6(&peer->remote)->sin6_addr,
557 		    0, NULL, &TO_IN6(&peer->local)->sin6_addr, NULL);
558 		NET_EPOCH_EXIT(et);
559 		if (ret != 0) {
560 			goto error;
561 		}
562 	}
563 #endif
564 	OVPN_WLOCK(sc);
565 
566 	/* Disallow peer id re-use. */
567 	if (ovpn_find_peer(sc, peerid) != NULL) {
568 		ret = EEXIST;
569 		goto error_locked;
570 	}
571 
572 	/* Make sure this is really a UDP socket. */
573 	if (so->so_type != SOCK_DGRAM || so->so_proto->pr_type != SOCK_DGRAM) {
574 		ret = EPROTOTYPE;
575 		goto error_locked;
576 	}
577 
578 	/* Must be the same socket as for other peers on this interface. */
579 	if (sc->so != NULL && so != sc->so)
580 		goto error_locked;
581 
582 	if (sc->so == NULL)
583 		sc->so = so;
584 
585 	/* Insert the peer into the list. */
586 	for (i = 0; i < OVPN_MAX_PEERS; i++) {
587 		if (sc->peers[i] != NULL)
588 			continue;
589 
590 		MPASS(sc->peers[i] == NULL);
591 		sc->peers[i] = peer;
592 		sc->peercount++;
593 		soref(sc->so);
594 		break;
595 	}
596 	if (i == OVPN_MAX_PEERS) {
597 		ret = ENOSPC;
598 		goto error_locked;
599 	}
600 
601 	ret = udp_set_kernel_tunneling(sc->so, ovpn_udp_input, NULL, sc);
602 	if (ret == EBUSY) {
603 		/* Fine, another peer already set the input function. */
604 		ret = 0;
605 	}
606 	if (ret != 0) {
607 		sc->peers[i] = NULL;
608 		sc->peercount--;
609 		goto error_locked;
610 	}
611 
612 	OVPN_WUNLOCK(sc);
613 
614 	goto done;
615 
616 error_locked:
617 	OVPN_WUNLOCK(sc);
618 error:
619 	free(name, M_SONAME);
620 	uma_zfree_pcpu(pcpu_zone_4, peer->last_active);
621 	free(peer, M_OVPN);
622 done:
623 	if (fp != NULL)
624 		fdrop(fp, td);
625 
626 	return (ret);
627 }
628 
629 static int
630 _ovpn_del_peer(struct ovpn_softc *sc, uint32_t peerid)
631 {
632 	struct ovpn_kpeer *peer;
633 	int i;
634 
635 	OVPN_WASSERT(sc);
636 
637 	for (i = 0; i < OVPN_MAX_PEERS; i++) {
638 		if (sc->peers[i] == NULL)
639 			continue;
640 		if (sc->peers[i]->peerid != peerid)
641 			continue;
642 
643 		peer = sc->peers[i];
644 		break;
645 	}
646 	if (i == OVPN_MAX_PEERS)
647 		return (ENOENT);
648 
649 	sc->peers[i] = NULL;
650 	sc->peercount--;
651 
652 	ovpn_peer_release_ref(peer, true);
653 
654 	return (0);
655 }
656 
657 static int
658 ovpn_del_peer(struct ifnet *ifp, nvlist_t *nvl)
659 {
660 	struct ovpn_softc *sc = ifp->if_softc;
661 	uint32_t peerid;
662 	int ret;
663 
664 	OVPN_WASSERT(sc);
665 
666 	if (nvl == NULL)
667 		return (EINVAL);
668 
669 	if (! nvlist_exists_number(nvl, "peerid"))
670 		return (EINVAL);
671 
672 	peerid = nvlist_get_number(nvl, "peerid");
673 
674 	ret = _ovpn_del_peer(sc, peerid);
675 
676 	return (ret);
677 }
678 
679 static int
680 ovpn_create_kkey_dir(struct ovpn_kkey_dir **kdirp,
681     const nvlist_t *nvl)
682 {
683 	struct crypto_session_params csp;
684 	struct ovpn_kkey_dir *kdir;
685 	const char *ciphername;
686 	enum ovpn_key_cipher cipher;
687 	const void *key, *iv;
688 	size_t keylen = 0, ivlen = 0;
689 	int error;
690 
691 	if (! nvlist_exists_string(nvl, "cipher"))
692 		return (EINVAL);
693 	ciphername = nvlist_get_string(nvl, "cipher");
694 
695 	if (strcmp(ciphername, "none") == 0)
696 		cipher = OVPN_CIPHER_ALG_NONE;
697 	else if (strcmp(ciphername, "AES-256-GCM") == 0)
698 		cipher = OVPN_CIPHER_ALG_AES_GCM;
699 	else if (strcmp(ciphername, "CHACHA20-POLY1305") == 0)
700 		cipher = OVPN_CIPHER_ALG_CHACHA20_POLY1305;
701 	else
702 		return (EINVAL);
703 
704 	if (cipher != OVPN_CIPHER_ALG_NONE) {
705 		if (! nvlist_exists_binary(nvl, "key"))
706 			return (EINVAL);
707 		key = nvlist_get_binary(nvl, "key", &keylen);
708 		if (keylen > sizeof(kdir->key))
709 			return (E2BIG);
710 
711 		if (! nvlist_exists_binary(nvl, "iv"))
712 			return (EINVAL);
713 		iv = nvlist_get_binary(nvl, "iv", &ivlen);
714 		if (ivlen != 8)
715 			return (E2BIG);
716 	}
717 
718 	kdir = malloc(sizeof(struct ovpn_kkey_dir), M_OVPN,
719 	    M_WAITOK | M_ZERO);
720 
721 	kdir->cipher = cipher;
722 	kdir->keylen = keylen;
723 	memcpy(kdir->key, key, keylen);
724 	kdir->noncelen = ivlen;
725 	memcpy(kdir->nonce, iv, ivlen);
726 
727 	if (kdir->cipher != OVPN_CIPHER_ALG_NONE) {
728 		/* Crypto init */
729 		bzero(&csp, sizeof(csp));
730 		csp.csp_mode = CSP_MODE_AEAD;
731 
732 		if (kdir->cipher == OVPN_CIPHER_ALG_CHACHA20_POLY1305)
733 			csp.csp_cipher_alg = CRYPTO_CHACHA20_POLY1305;
734 		else
735 			csp.csp_cipher_alg = CRYPTO_AES_NIST_GCM_16;
736 
737 		csp.csp_flags |= CSP_F_SEPARATE_AAD;
738 
739 		csp.csp_cipher_klen = kdir->keylen;
740 		csp.csp_cipher_key = kdir->key;
741 		csp.csp_ivlen = 96 / 8;
742 
743 		error = crypto_newsession(&kdir->cryptoid, &csp,
744 		    CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE);
745 		if (error) {
746 			free(kdir, M_OVPN);
747 			return (error);
748 		}
749 	}
750 
751 	mtx_init(&kdir->replay_mtx, "if_ovpn rx replay", NULL, MTX_DEF);
752 	*kdirp = kdir;
753 
754 	return (0);
755 }
756 
757 static void
758 ovpn_free_kkey_dir(struct ovpn_kkey_dir *kdir)
759 {
760 	if (kdir == NULL)
761 		return;
762 
763 	mtx_destroy(&kdir->replay_mtx);
764 
765 	crypto_freesession(kdir->cryptoid);
766 	free(kdir, M_OVPN);
767 }
768 
769 static int
770 ovpn_set_key(struct ifnet *ifp, const nvlist_t *nvl)
771 {
772 	struct ovpn_softc *sc = ifp->if_softc;
773 	struct ovpn_kkey_dir *enc, *dec;
774 	struct ovpn_kpeer *peer;
775 	int slot, keyid, peerid;
776 	int error;
777 
778 	if (nvl == NULL)
779 		return (EINVAL);
780 
781 	if (! nvlist_exists_number(nvl, "slot"))
782 		return (EINVAL);
783 	slot = nvlist_get_number(nvl, "slot");
784 
785 	if (! nvlist_exists_number(nvl, "keyid"))
786 		return (EINVAL);
787 	keyid = nvlist_get_number(nvl, "keyid");
788 
789 	if (! nvlist_exists_number(nvl, "peerid"))
790 		return (EINVAL);
791 	peerid = nvlist_get_number(nvl, "peerid");
792 
793 	if (slot != OVPN_KEY_SLOT_PRIMARY &&
794 	    slot != OVPN_KEY_SLOT_SECONDARY)
795 		return (EINVAL);
796 
797 	if (! nvlist_exists_nvlist(nvl, "encrypt") ||
798 	    ! nvlist_exists_nvlist(nvl, "decrypt"))
799 		return (EINVAL);
800 
801 	error = ovpn_create_kkey_dir(&enc, nvlist_get_nvlist(nvl, "encrypt"));
802 	if (error)
803 		return (error);
804 
805 	error = ovpn_create_kkey_dir(&dec, nvlist_get_nvlist(nvl, "decrypt"));
806 	if (error) {
807 		ovpn_free_kkey_dir(enc);
808 		return (error);
809 	}
810 
811 	OVPN_WLOCK(sc);
812 
813 	peer = ovpn_find_peer(sc, peerid);
814 	if (peer == NULL) {
815 		ovpn_free_kkey_dir(dec);
816 		ovpn_free_kkey_dir(enc);
817 		OVPN_WUNLOCK(sc);
818 		return (ENOENT);
819 	}
820 
821 	ovpn_free_kkey_dir(peer->keys[slot].encrypt);
822 	ovpn_free_kkey_dir(peer->keys[slot].decrypt);
823 
824 	peer->keys[slot].encrypt = enc;
825 	peer->keys[slot].decrypt = dec;
826 
827 	peer->keys[slot].keyid = keyid;
828 	peer->keys[slot].peerid = peerid;
829 
830 	OVPN_WUNLOCK(sc);
831 
832 	return (0);
833 }
834 
835 static int
836 ovpn_check_key(struct ovpn_softc *sc, struct ovpn_kpeer *peer, enum ovpn_key_slot slot)
837 {
838 	OVPN_ASSERT(sc);
839 
840 	if (peer->keys[slot].encrypt == NULL)
841 		return (ENOLINK);
842 
843 	if (peer->keys[slot].decrypt == NULL)
844 		return (ENOLINK);
845 
846 	return (0);
847 }
848 
849 static int
850 ovpn_start(struct ifnet *ifp)
851 {
852 	struct ovpn_softc *sc = ifp->if_softc;
853 
854 	OVPN_WLOCK(sc);
855 
856 	ifp->if_flags |= IFF_UP;
857 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
858 	if_link_state_change(ifp, LINK_STATE_UP);
859 
860 	OVPN_WUNLOCK(sc);
861 
862 	return (0);
863 }
864 
865 static int
866 ovpn_swap_keys(struct ifnet *ifp, nvlist_t *nvl)
867 {
868 	struct ovpn_softc *sc = ifp->if_softc;
869 	struct ovpn_kpeer *peer;
870 	struct ovpn_kkey tmpkey;
871 	int error;
872 
873 	if (nvl == NULL)
874 		return (EINVAL);
875 
876 	if (! nvlist_exists_number(nvl, "peerid"))
877 		return (EINVAL);
878 
879 	OVPN_WLOCK(sc);
880 
881 	peer = ovpn_find_peer(sc, nvlist_get_number(nvl, "peerid"));
882 	if (peer == NULL) {
883 		OVPN_WUNLOCK(sc);
884 		return (ENOENT);
885 	}
886 
887 	/* Check that we have a second key to swap to. */
888 	error = ovpn_check_key(sc, peer, OVPN_KEY_SLOT_SECONDARY);
889 	if (error) {
890 		OVPN_WUNLOCK(sc);
891 		return (error);
892 	}
893 
894 	tmpkey = peer->keys[0];
895 	peer->keys[0] = peer->keys[1];
896 	peer->keys[1] = tmpkey;
897 
898 	OVPN_WUNLOCK(sc);
899 
900 	return (0);
901 }
902 
903 static int
904 ovpn_del_key(struct ifnet *ifp, const nvlist_t *nvl)
905 {
906 	enum ovpn_key_slot slot;
907 	struct ovpn_kpeer *peer;
908 	struct ovpn_softc *sc = ifp->if_softc;
909 
910 	if (nvl == NULL)
911 		return (EINVAL);
912 
913 	if (! nvlist_exists_number(nvl, "peerid"))
914 		return (EINVAL);
915 
916 	if (! nvlist_exists_number(nvl, "slot"))
917 		return (EINVAL);
918 	slot = nvlist_get_number(nvl, "slot");
919 
920 	if (slot != OVPN_KEY_SLOT_PRIMARY &&
921 	    slot != OVPN_KEY_SLOT_SECONDARY)
922 		return (EINVAL);
923 
924 	OVPN_WLOCK(sc);
925 
926 	peer = ovpn_find_peer(sc, nvlist_get_number(nvl, "peerid"));
927 	if (peer == NULL) {
928 		OVPN_WUNLOCK(sc);
929 		return (ENOENT);
930 	}
931 
932 	ovpn_free_kkey_dir(peer->keys[slot].encrypt);
933 	ovpn_free_kkey_dir(peer->keys[slot].decrypt);
934 
935 	peer->keys[slot].encrypt = NULL;
936 	peer->keys[slot].decrypt = NULL;
937 
938 	peer->keys[slot].keyid = 0;
939 	peer->keys[slot].peerid = 0;
940 
941 	OVPN_WUNLOCK(sc);
942 
943 	return (0);
944 }
945 
946 static int
947 ovpn_send_pkt(struct ifnet *ifp, const nvlist_t *nvl)
948 {
949 	struct epoch_tracker et;
950 	struct ovpn_softc *sc = ifp->if_softc;
951 	struct mbuf *m;
952 	const uint8_t *pkt;
953 	size_t pktlen;
954 	uint32_t peerid;
955 	int ret;
956 
957 	if (nvl == NULL)
958 		return (EINVAL);
959 
960 	if (! nvlist_exists_binary(nvl, "packet"))
961 		return (EINVAL);
962 	pkt = nvlist_get_binary(nvl, "packet", &pktlen);
963 
964 	if (! nvlist_exists_number(nvl, "peerid"))
965 		return (EINVAL);
966 
967 	peerid = nvlist_get_number(nvl, "peerid");
968 
969 	/*
970 	 * Check that userspace isn't giving us a data packet. That might lead
971 	 * to IV re-use, which would be bad.
972 	 */
973 	if ((pkt[0] >> OVPN_OP_SHIFT) == OVPN_OP_DATA_V2)
974 		return (EINVAL);
975 
976 	m = m_get2(pktlen, M_WAITOK, MT_DATA, M_PKTHDR);
977 	if (m == NULL)
978 		return (ENOMEM);
979 
980 	m->m_len = m->m_pkthdr.len = pktlen;
981 	m_copyback(m, 0, m->m_len, pkt);
982 
983 	/* Now prepend IP/UDP headers and transmit the mbuf. */
984 	NET_EPOCH_ENTER(et);
985 	ret = ovpn_encap(sc, peerid, m);
986 	NET_EPOCH_EXIT(et);
987 	if (ret == 0)
988 		OVPN_COUNTER_ADD(sc, sent_ctrl_pkts, 1);
989 	else
990 		OVPN_COUNTER_ADD(sc, lost_ctrl_pkts_out, 1);
991 
992 	return (ret);
993 }
994 
995 static void
996 ovpn_send_ping(void *arg)
997 {
998 	static const uint8_t ping_str[] = {
999 		0x2a, 0x18, 0x7b, 0xf3, 0x64, 0x1e, 0xb4, 0xcb,
1000 		0x07, 0xed, 0x2d, 0x0a, 0x98, 0x1f, 0xc7, 0x48
1001 	};
1002 
1003 	struct epoch_tracker et;
1004 	struct ovpn_kpeer *peer = arg;
1005 	struct ovpn_softc *sc = peer->sc;
1006 	struct mbuf *m;
1007 
1008 	OVPN_RASSERT(sc);
1009 
1010 	/* Ensure we repeat! */
1011 	callout_reset(&peer->ping_send, peer->keepalive.interval * hz,
1012 	    ovpn_send_ping, peer);
1013 
1014 	m = m_get2(sizeof(ping_str), M_NOWAIT, MT_DATA, M_PKTHDR);
1015 	if (m == NULL)
1016 		return;
1017 
1018 	m_copyback(m, 0, sizeof(ping_str), ping_str);
1019 	m->m_len = m->m_pkthdr.len = sizeof(ping_str);
1020 
1021 	CURVNET_SET(sc->ifp->if_vnet);
1022 	NET_EPOCH_ENTER(et);
1023 	(void)ovpn_transmit_to_peer(sc->ifp, m, peer, NULL);
1024 	NET_EPOCH_EXIT(et);
1025 	CURVNET_RESTORE();
1026 }
1027 
1028 static void
1029 ovpn_timeout(void *arg)
1030 {
1031 	struct ovpn_kpeer *peer = arg;
1032 	struct ovpn_softc *sc = peer->sc;
1033 	uint32_t last, _last_active;
1034 	int ret __diagused;
1035 	int cpu;
1036 
1037 	OVPN_WASSERT(sc);
1038 
1039 	last = 0;
1040 	CPU_FOREACH(cpu) {
1041 		_last_active = *zpcpu_get_cpu(peer->last_active, cpu);
1042 		if (_last_active > last)
1043 			last = _last_active;
1044 	}
1045 
1046 	if (last + peer->keepalive.timeout > time_uptime) {
1047 		callout_reset(&peer->ping_rcv,
1048 		    (peer->keepalive.timeout - (time_uptime - last)) * hz,
1049 		    ovpn_timeout, peer);
1050 		return;
1051 	}
1052 
1053 	CURVNET_SET(sc->ifp->if_vnet);
1054 	ret = _ovpn_del_peer(sc, peer->peerid);
1055 	MPASS(ret == 0);
1056 	CURVNET_RESTORE();
1057 }
1058 
1059 static int
1060 ovpn_set_peer(struct ifnet *ifp, const nvlist_t *nvl)
1061 {
1062 	struct ovpn_softc *sc = ifp->if_softc;
1063 	struct ovpn_kpeer *peer;
1064 
1065 	if (nvl == NULL)
1066 		return (EINVAL);
1067 
1068 	if (! nvlist_exists_number(nvl, "interval") ||
1069 	    ! nvlist_exists_number(nvl, "timeout") ||
1070 	    ! nvlist_exists_number(nvl, "peerid"))
1071 		return (EINVAL);
1072 
1073 	OVPN_WLOCK(sc);
1074 
1075 	peer = ovpn_find_peer(sc, nvlist_get_number(nvl, "peerid"));
1076 	if (peer == NULL) {
1077 		OVPN_WUNLOCK(sc);
1078 		return (ENOENT);
1079 	}
1080 
1081 	peer->keepalive.interval = nvlist_get_number(nvl, "interval");
1082 	peer->keepalive.timeout = nvlist_get_number(nvl, "timeout");
1083 
1084 	if (peer->keepalive.interval > 0)
1085 		callout_reset(&peer->ping_send, peer->keepalive.interval * hz,
1086 		    ovpn_send_ping, peer);
1087 	if (peer->keepalive.timeout > 0)
1088 		callout_reset(&peer->ping_rcv, peer->keepalive.timeout * hz,
1089 		    ovpn_timeout, peer);
1090 
1091 	OVPN_WUNLOCK(sc);
1092 
1093 	return (0);
1094 }
1095 
1096 static int
1097 ovpn_set_ifmode(struct ifnet *ifp, const nvlist_t *nvl)
1098 {
1099 	struct ovpn_softc *sc = ifp->if_softc;
1100 	int ifmode;
1101 
1102 	if (nvl == NULL)
1103 		return (EINVAL);
1104 
1105 	if (! nvlist_exists_number(nvl, "ifmode") )
1106 		return (EINVAL);
1107 
1108 	ifmode = nvlist_get_number(nvl, "ifmode");
1109 
1110 	OVPN_WLOCK(sc);
1111 
1112 	/* deny this if UP */
1113 	if (ifp->if_flags & IFF_UP) {
1114 		OVPN_WUNLOCK(sc);
1115 		return (EBUSY);
1116 	}
1117 
1118 	switch (ifmode & ~IFF_MULTICAST) {
1119 	case IFF_POINTOPOINT:
1120 	case IFF_BROADCAST:
1121 		ifp->if_flags &=
1122 		    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1123 		ifp->if_flags |= ifmode;
1124 		break;
1125 	default:
1126 		OVPN_WUNLOCK(sc);
1127 		return (EINVAL);
1128 	}
1129 
1130 	OVPN_WUNLOCK(sc);
1131 
1132 	return (0);
1133 }
1134 
1135 static int
1136 ovpn_ioctl_set(struct ifnet *ifp, struct ifdrv *ifd)
1137 {
1138 	struct ovpn_softc *sc = ifp->if_softc;
1139 	uint8_t *buf = NULL;
1140 	nvlist_t *nvl = NULL;
1141 	int ret;
1142 
1143 	if (ifd->ifd_len != 0) {
1144 		if (ifd->ifd_len > OVPN_MAX_REQUEST_SIZE)
1145 			return (E2BIG);
1146 
1147 		buf = malloc(ifd->ifd_len, M_OVPN, M_WAITOK);
1148 
1149 		ret = copyin(ifd->ifd_data, buf, ifd->ifd_len);
1150 		if (ret != 0) {
1151 			free(buf, M_OVPN);
1152 			return (ret);
1153 		}
1154 
1155 		nvl = nvlist_unpack(buf, ifd->ifd_len, 0);
1156 		free(buf, M_OVPN);
1157 		if (nvl == NULL) {
1158 			return (EINVAL);
1159 		}
1160 	}
1161 
1162 	switch (ifd->ifd_cmd) {
1163 	case OVPN_NEW_PEER:
1164 		ret = ovpn_new_peer(ifp, nvl);
1165 		break;
1166 	case OVPN_DEL_PEER:
1167 		OVPN_WLOCK(sc);
1168 		ret = ovpn_del_peer(ifp, nvl);
1169 		OVPN_WUNLOCK(sc);
1170 		break;
1171 	case OVPN_NEW_KEY:
1172 		ret = ovpn_set_key(ifp, nvl);
1173 		break;
1174 	case OVPN_START_VPN:
1175 		ret = ovpn_start(ifp);
1176 		break;
1177 	case OVPN_SWAP_KEYS:
1178 		ret = ovpn_swap_keys(ifp, nvl);
1179 		break;
1180 	case OVPN_DEL_KEY:
1181 		ret = ovpn_del_key(ifp, nvl);
1182 		break;
1183 	case OVPN_SEND_PKT:
1184 		ret = ovpn_send_pkt(ifp, nvl);
1185 		break;
1186 	case OVPN_SET_PEER:
1187 		ret = ovpn_set_peer(ifp, nvl);
1188 		break;
1189 	case OVPN_SET_IFMODE:
1190 		ret = ovpn_set_ifmode(ifp, nvl);
1191 		break;
1192 	default:
1193 		ret = ENOTSUP;
1194 	}
1195 
1196 	nvlist_destroy(nvl);
1197 	return (ret);
1198 }
1199 
1200 static int
1201 ovpn_add_counters(nvlist_t *parent, const char *name, counter_u64_t in,
1202     counter_u64_t out)
1203 {
1204 	nvlist_t *nvl;
1205 
1206 	nvl = nvlist_create(0);
1207 	if (nvl == NULL)
1208 		return (ENOMEM);
1209 
1210 	nvlist_add_number(nvl, "in", counter_u64_fetch(in));
1211 	nvlist_add_number(nvl, "out", counter_u64_fetch(out));
1212 
1213 	nvlist_add_nvlist(parent, name, nvl);
1214 
1215 	nvlist_destroy(nvl);
1216 
1217 	return (0);
1218 }
1219 
1220 static int
1221 ovpn_get_stats(struct ovpn_softc *sc, nvlist_t **onvl)
1222 {
1223 	nvlist_t *nvl;
1224 	int ret;
1225 
1226 	nvl = nvlist_create(0);
1227 	if (nvl == NULL)
1228 		return (ENOMEM);
1229 
1230 #define OVPN_COUNTER_OUT(name, in, out) \
1231 	do { \
1232 		ret = ovpn_add_counters(nvl, name, \
1233 		    sc->counters[offsetof(struct ovpn_counters, in) / \
1234 		    sizeof(uint64_t)], \
1235 		    sc->counters[offsetof(struct ovpn_counters, out) / \
1236 		    sizeof(uint64_t)]); \
1237 		if (ret != 0) \
1238 			goto error; \
1239 	} while(0)
1240 
1241 	OVPN_COUNTER_OUT("lost_ctrl", lost_ctrl_pkts_in, lost_ctrl_pkts_out);
1242 	OVPN_COUNTER_OUT("lost_data", lost_data_pkts_in, lost_data_pkts_out);
1243 	OVPN_COUNTER_OUT("nomem_data", nomem_data_pkts_in,
1244 	    nomem_data_pkts_out);
1245 	OVPN_COUNTER_OUT("data", received_data_pkts, sent_data_pkts);
1246 	OVPN_COUNTER_OUT("ctrl", received_ctrl_pkts, sent_ctrl_pkts);
1247 	OVPN_COUNTER_OUT("tunnel", tunnel_bytes_received,
1248 	    tunnel_bytes_received);
1249 	OVPN_COUNTER_OUT("transport", transport_bytes_received,
1250 	    transport_bytes_received);
1251 #undef OVPN_COUNTER_OUT
1252 
1253 	*onvl = nvl;
1254 
1255 	return (0);
1256 
1257 error:
1258 	nvlist_destroy(nvl);
1259 	return (ret);
1260 }
1261 
1262 static int
1263 ovpn_poll_pkt(struct ovpn_softc *sc, nvlist_t **onvl)
1264 {
1265 	nvlist_t *nvl;
1266 
1267 	nvl = nvlist_create(0);
1268 	if (nvl == NULL)
1269 		return (ENOMEM);
1270 
1271 	nvlist_add_number(nvl, "pending",
1272 	    buf_ring_count(sc->rxring) + buf_ring_count(sc->notifring));
1273 
1274 	*onvl = nvl;
1275 
1276 	return (0);
1277 }
1278 
1279 static int
1280 opvn_get_pkt(struct ovpn_softc *sc, nvlist_t **onvl)
1281 {
1282 	struct ovpn_notification *n;
1283 	struct ovpn_wire_header *ohdr;
1284 	struct mbuf *m;
1285 	uint8_t *buf;
1286 	nvlist_t *nvl;
1287 	uint32_t peerid;
1288 	u_int mlength;
1289 
1290 	/* Check if we have notifications pending. */
1291 	n = buf_ring_dequeue_mc(sc->notifring);
1292 	if (n != NULL) {
1293 		nvl = nvlist_create(0);
1294 		if (nvl == NULL) {
1295 			free(n, M_OVPN);
1296 			return (ENOMEM);
1297 		}
1298 		nvlist_add_number(nvl, "peerid", n->peerid);
1299 		nvlist_add_number(nvl, "notification", n->type);
1300 		free(n, M_OVPN);
1301 
1302 		*onvl = nvl;
1303 
1304 		return (0);
1305 	}
1306 
1307 	/* Queued packet. */
1308 	m = buf_ring_dequeue_mc(sc->rxring);
1309 	if (m == NULL)
1310 		return (ENOENT);
1311 
1312 	mlength = m_length(m, NULL);
1313 	buf = malloc(mlength, M_NVLIST, M_WAITOK);
1314 	m_copydata(m, 0, mlength, buf);
1315 	ohdr = (struct ovpn_wire_header *)buf;
1316 	peerid = ntohl(ohdr->opcode) & 0x00ffffff;
1317 
1318 	nvl = nvlist_create(0);
1319 	if (nvl == NULL) {
1320 		OVPN_COUNTER_ADD(sc, lost_ctrl_pkts_in, 1);
1321 		m_freem(m);
1322 		free(buf, M_NVLIST);
1323 		return (ENOMEM);
1324 	}
1325 
1326 	nvlist_move_binary(nvl, "packet", buf, mlength);
1327 	buf = NULL;
1328 	nvlist_add_number(nvl, "peerid", peerid);
1329 
1330 	*onvl = nvl;
1331 
1332 	m_freem(m);
1333 
1334 	return (0);
1335 }
1336 
1337 static int
1338 ovpn_ioctl_get(struct ifnet *ifp, struct ifdrv *ifd)
1339 {
1340 	struct ovpn_softc *sc = ifp->if_softc;
1341 	nvlist_t *nvl = NULL;
1342 	int error;
1343 
1344 	switch (ifd->ifd_cmd) {
1345 	case OVPN_GET_STATS:
1346 		error = ovpn_get_stats(sc, &nvl);
1347 		break;
1348 	case OVPN_POLL_PKT:
1349 		error = ovpn_poll_pkt(sc, &nvl);
1350 		break;
1351 	case OVPN_GET_PKT:
1352 		error = opvn_get_pkt(sc, &nvl);
1353 		break;
1354 	default:
1355 		error = ENOTSUP;
1356 		break;
1357 	}
1358 
1359 	if (error == 0) {
1360 		void *packed = NULL;
1361 		size_t len;
1362 
1363 		MPASS(nvl != NULL);
1364 
1365 		packed = nvlist_pack(nvl, &len);
1366 		if (! packed) {
1367 			nvlist_destroy(nvl);
1368 			return (ENOMEM);
1369 		}
1370 
1371 		if (len > ifd->ifd_len) {
1372 			free(packed, M_NVLIST);
1373 			nvlist_destroy(nvl);
1374 			return (ENOSPC);
1375 		}
1376 
1377 		error = copyout(packed, ifd->ifd_data, len);
1378 		ifd->ifd_len = len;
1379 
1380 		free(packed, M_NVLIST);
1381 		nvlist_destroy(nvl);
1382 	}
1383 
1384 	return (error);
1385 }
1386 
1387 static int
1388 ovpn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1389 {
1390 	struct ifdrv *ifd;
1391 	int error;
1392 
1393 	switch (cmd) {
1394 	case SIOCSDRVSPEC:
1395 	case SIOCGDRVSPEC:
1396 		error = priv_check(curthread, PRIV_NET_OVPN);
1397 		if (error)
1398 			return (error);
1399 		break;
1400 	}
1401 
1402 	switch (cmd) {
1403 	case SIOCSDRVSPEC:
1404 		ifd = (struct ifdrv *)data;
1405 		error = ovpn_ioctl_set(ifp, ifd);
1406 		break;
1407 	case SIOCGDRVSPEC:
1408 		ifd = (struct ifdrv *)data;
1409 		error = ovpn_ioctl_get(ifp, ifd);
1410 		break;
1411 	case SIOCSIFMTU: {
1412 		struct ifreq *ifr = (struct ifreq *)data;
1413 		if (ifr->ifr_mtu < OVPN_MTU_MIN || ifr->ifr_mtu > OVPN_MTU_MAX)
1414 			return (EINVAL);
1415 
1416 		ifp->if_mtu = ifr->ifr_mtu;
1417 		return (0);
1418 	}
1419 	case SIOCSIFADDR:
1420 	case SIOCADDMULTI:
1421 	case SIOCDELMULTI:
1422 	case SIOCGIFMTU:
1423 	case SIOCSIFFLAGS:
1424 		return (0);
1425 	default:
1426 		error = EINVAL;
1427 	}
1428 
1429 	return (error);
1430 }
1431 
1432 static int
1433 ovpn_encrypt_tx_cb(struct cryptop *crp)
1434 {
1435 	struct epoch_tracker et;
1436 	struct ovpn_kpeer *peer = crp->crp_opaque;
1437 	struct ovpn_softc *sc = peer->sc;
1438 	struct mbuf *m = crp->crp_buf.cb_mbuf;
1439 	int tunnel_len;
1440 	int ret;
1441 
1442 	if (crp->crp_etype != 0) {
1443 		crypto_freereq(crp);
1444 		ovpn_peer_release_ref(peer, false);
1445 		OVPN_COUNTER_ADD(sc, lost_data_pkts_out, 1);
1446 		m_freem(m);
1447 		return (0);
1448 	}
1449 
1450 	NET_EPOCH_ENTER(et);
1451 	CURVNET_SET(sc->ifp->if_vnet);
1452 
1453 	MPASS(crp->crp_buf.cb_type == CRYPTO_BUF_MBUF);
1454 
1455 	tunnel_len = m->m_pkthdr.len - sizeof(struct ovpn_wire_header);
1456 	ret = ovpn_encap(sc, peer->peerid, m);
1457 	if (ret == 0) {
1458 		OVPN_COUNTER_ADD(sc, sent_data_pkts, 1);
1459 		OVPN_COUNTER_ADD(sc, tunnel_bytes_sent, tunnel_len);
1460 	}
1461 
1462 	CURVNET_RESTORE();
1463 	NET_EPOCH_EXIT(et);
1464 
1465 	crypto_freereq(crp);
1466 	ovpn_peer_release_ref(peer, false);
1467 
1468 	return (0);
1469 }
1470 
1471 static void
1472 ovpn_finish_rx(struct ovpn_softc *sc, struct mbuf *m,
1473     struct ovpn_kpeer *peer, struct ovpn_kkey *key, uint32_t seq,
1474     struct rm_priotracker *_ovpn_lock_trackerp)
1475 {
1476 	uint32_t af;
1477 
1478 	OVPN_RASSERT(sc);
1479 	NET_EPOCH_ASSERT();
1480 
1481 	/* Replay protection. */
1482 	if (V_replay_protection && ! ovpn_check_replay(key->decrypt, seq)) {
1483 		OVPN_RUNLOCK(sc);
1484 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
1485 		m_freem(m);
1486 		return;
1487 	}
1488 
1489 	critical_enter();
1490 	*zpcpu_get(peer->last_active) = time_uptime;
1491 	critical_exit();
1492 
1493 	OVPN_RUNLOCK(sc);
1494 
1495 	OVPN_COUNTER_ADD(sc, received_data_pkts, 1);
1496 	OVPN_COUNTER_ADD(sc, tunnel_bytes_received, m->m_pkthdr.len);
1497 
1498 	/* Receive the packet on our interface. */
1499 	m->m_pkthdr.rcvif = sc->ifp;
1500 
1501 	/* Clear checksum flags in case the real hardware set them. */
1502 	m->m_pkthdr.csum_flags = 0;
1503 
1504 	/* Ensure we can read the first byte. */
1505 	m = m_pullup(m, 1);
1506 	if (m == NULL) {
1507 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_in, 1);
1508 		return;
1509 	}
1510 
1511 	/*
1512 	 * Check for address family, and disregard any control packets (e.g.
1513 	 * keepalive).
1514 	 */
1515 	af = ovpn_get_af(m);
1516 	if (af != 0) {
1517 		BPF_MTAP2(sc->ifp, &af, sizeof(af), m);
1518 		if (V_async_netisr_queue)
1519 			netisr_queue(af == AF_INET ? NETISR_IP : NETISR_IPV6, m);
1520 		else
1521 			netisr_dispatch(af == AF_INET ? NETISR_IP : NETISR_IPV6, m);
1522 	} else {
1523 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
1524 		m_freem(m);
1525 	}
1526 }
1527 
1528 static struct ovpn_kkey *
1529 ovpn_find_key(struct ovpn_softc *sc, struct ovpn_kpeer *peer,
1530     const struct ovpn_wire_header *ohdr)
1531 {
1532 	struct ovpn_kkey *key = NULL;
1533 	uint8_t keyid;
1534 
1535 	OVPN_RASSERT(sc);
1536 
1537 	keyid = (ntohl(ohdr->opcode) >> 24) & 0x07;
1538 
1539 	if (peer->keys[0].keyid == keyid)
1540 		key = &peer->keys[0];
1541 	else if (peer->keys[1].keyid == keyid)
1542 		key = &peer->keys[1];
1543 
1544 	return (key);
1545 }
1546 
1547 static int
1548 ovpn_decrypt_rx_cb(struct cryptop *crp)
1549 {
1550 	struct epoch_tracker et;
1551 	struct ovpn_softc *sc = crp->crp_opaque;
1552 	struct mbuf *m = crp->crp_buf.cb_mbuf;
1553 	struct ovpn_kkey *key;
1554 	struct ovpn_kpeer *peer;
1555 	struct ovpn_wire_header *ohdr;
1556 	uint32_t peerid;
1557 
1558 	OVPN_RLOCK_TRACKER;
1559 
1560 	OVPN_RLOCK(sc);
1561 
1562 	MPASS(crp->crp_buf.cb_type == CRYPTO_BUF_MBUF);
1563 
1564 	if (crp->crp_etype != 0) {
1565 		crypto_freereq(crp);
1566 		atomic_add_int(&sc->refcount, -1);
1567 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
1568 		OVPN_RUNLOCK(sc);
1569 		m_freem(m);
1570 		return (0);
1571 	}
1572 
1573 	CURVNET_SET(sc->ifp->if_vnet);
1574 
1575 	ohdr = mtodo(m, sizeof(struct udphdr));
1576 
1577 	peerid = ntohl(ohdr->opcode) & 0x00ffffff;
1578 	peer = ovpn_find_peer(sc, peerid);
1579 	if (peer == NULL) {
1580 		/* No such peer. Drop packet. */
1581 		crypto_freereq(crp);
1582 		atomic_add_int(&sc->refcount, -1);
1583 		OVPN_RUNLOCK(sc);
1584 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
1585 		m_freem(m);
1586 		CURVNET_RESTORE();
1587 		return (0);
1588 	}
1589 
1590 	key = ovpn_find_key(sc, peer, ohdr);
1591 	if (key == NULL) {
1592 		crypto_freereq(crp);
1593 		atomic_add_int(&sc->refcount, -1);
1594 		/*
1595 		 * Has this key been removed between us starting the decrypt
1596 		 * and finishing it?
1597 		 */
1598 		OVPN_RUNLOCK(sc);
1599 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
1600 		m_freem(m);
1601 		CURVNET_RESTORE();
1602 		return (0);
1603 	}
1604 
1605 	/* Now remove the outer headers */
1606 	m_adj_decap(m, sizeof(struct udphdr) +
1607 	    sizeof(struct ovpn_wire_header));
1608 
1609 	NET_EPOCH_ENTER(et);
1610 	ovpn_finish_rx(sc, m, peer, key, ntohl(ohdr->seq), _ovpn_lock_trackerp);
1611 	NET_EPOCH_EXIT(et);
1612 	OVPN_UNLOCK_ASSERT(sc);
1613 
1614 	CURVNET_RESTORE();
1615 
1616 	crypto_freereq(crp);
1617 	atomic_add_int(&sc->refcount, -1);
1618 
1619 	return (0);
1620 }
1621 
1622 static int
1623 ovpn_get_af(struct mbuf *m)
1624 {
1625 	struct ip *ip;
1626 	struct ip6_hdr *ip6;
1627 
1628 	/*
1629 	 * We should pullup, but we're only interested in the first byte, so
1630 	 * that'll always be contiguous.
1631 	 */
1632 	ip = mtod(m, struct ip *);
1633 	if (ip->ip_v == IPVERSION)
1634 		return (AF_INET);
1635 
1636 	ip6 = mtod(m, struct ip6_hdr *);
1637 	if ((ip6->ip6_vfc & IPV6_VERSION_MASK) == IPV6_VERSION)
1638 		return (AF_INET6);
1639 
1640 	return (0);
1641 }
1642 
1643 #ifdef INET
1644 static struct ovpn_kpeer *
1645 ovpn_find_peer_by_ip(struct ovpn_softc *sc, const struct in_addr addr)
1646 {
1647 	struct ovpn_kpeer *peer = NULL;
1648 
1649 	OVPN_ASSERT(sc);
1650 
1651 	for (int i = 0; i < OVPN_MAX_PEERS; i++) {
1652 		if (sc->peers[i] == NULL)
1653 			continue;
1654 		if (addr.s_addr == sc->peers[i]->vpn4.s_addr) {
1655 			peer = sc->peers[i];
1656 			break;
1657 		}
1658 	}
1659 
1660 	return (peer);
1661 }
1662 #endif
1663 
1664 #ifdef INET6
1665 static struct ovpn_kpeer *
1666 ovpn_find_peer_by_ip6(struct ovpn_softc *sc, const struct in6_addr *addr)
1667 {
1668 	struct ovpn_kpeer *peer = NULL;
1669 
1670 	OVPN_ASSERT(sc);
1671 
1672 	for (int i = 0; i < OVPN_MAX_PEERS; i++) {
1673 		if (sc->peers[i] == NULL)
1674 			continue;
1675 		if (memcmp(addr, &sc->peers[i]->vpn6, sizeof(*addr)) == 0) {
1676 			peer = sc->peers[i];
1677 			break;
1678 		}
1679 	}
1680 
1681 	return (peer);
1682 }
1683 #endif
1684 
1685 static struct ovpn_kpeer *
1686 ovpn_route_peer(struct ovpn_softc *sc, struct mbuf **m0,
1687     const struct sockaddr *dst)
1688 {
1689 	struct ovpn_kpeer *peer = NULL;
1690 	int af;
1691 
1692 	NET_EPOCH_ASSERT();
1693 	OVPN_ASSERT(sc);
1694 
1695 	/* Shortcut if we're a client (or are a server and have only one client). */
1696 	if (sc->peercount == 1)
1697 		return (ovpn_find_only_peer(sc));
1698 
1699 	if (dst != NULL)
1700 		af = dst->sa_family;
1701 	else
1702 		af = ovpn_get_af(*m0);
1703 
1704 	switch (af) {
1705 #ifdef INET
1706 	case AF_INET: {
1707 		const struct sockaddr_in *sa = (const struct sockaddr_in *)dst;
1708 		struct nhop_object *nh;
1709 		const struct in_addr *ip_dst;
1710 
1711 		if (sa != NULL) {
1712 			ip_dst = &sa->sin_addr;
1713 		} else {
1714 			struct ip *ip;
1715 
1716 			*m0 = m_pullup(*m0, sizeof(struct ip));
1717 			if (*m0 == NULL)
1718 				return (NULL);
1719 			ip = mtod(*m0, struct ip *);
1720 			ip_dst = &ip->ip_dst;
1721 		}
1722 
1723 		peer = ovpn_find_peer_by_ip(sc, *ip_dst);
1724 		SDT_PROBE2(if_ovpn, tx, route, ip4, ip_dst, peer);
1725 		if (peer == NULL) {
1726 			nh = fib4_lookup(M_GETFIB(*m0), *ip_dst, 0,
1727 			    NHR_NONE, 0);
1728 			if (nh && (nh->nh_flags & NHF_GATEWAY)) {
1729 				peer = ovpn_find_peer_by_ip(sc,
1730 				    nh->gw4_sa.sin_addr);
1731 				SDT_PROBE2(if_ovpn, tx, route, ip4,
1732 				    &nh->gw4_sa.sin_addr, peer);
1733 			}
1734 		}
1735 		break;
1736 	}
1737 #endif
1738 #ifdef INET6
1739 	case AF_INET6: {
1740 		const struct sockaddr_in6 *sa6 =
1741 		    (const struct sockaddr_in6 *)dst;
1742 		struct nhop_object *nh;
1743 		const struct in6_addr *ip6_dst;
1744 
1745 		if (sa6 != NULL) {
1746 			ip6_dst = &sa6->sin6_addr;
1747 		} else {
1748 			struct ip6_hdr *ip6;
1749 
1750 			*m0 = m_pullup(*m0, sizeof(struct ip6_hdr));
1751 			if (*m0 == NULL)
1752 				return (NULL);
1753 			ip6 = mtod(*m0, struct ip6_hdr *);
1754 			ip6_dst = &ip6->ip6_dst;
1755 		}
1756 
1757 		peer = ovpn_find_peer_by_ip6(sc, ip6_dst);
1758 		SDT_PROBE2(if_ovpn, tx, route, ip6, ip6_dst, peer);
1759 		if (peer == NULL) {
1760 			nh = fib6_lookup(M_GETFIB(*m0), ip6_dst, 0,
1761 			    NHR_NONE, 0);
1762 			if (nh && (nh->nh_flags & NHF_GATEWAY)) {
1763 				peer = ovpn_find_peer_by_ip6(sc,
1764 				    &nh->gw6_sa.sin6_addr);
1765 				SDT_PROBE2(if_ovpn, tx, route, ip6,
1766 				    &nh->gw6_sa.sin6_addr, peer);
1767 			}
1768 		}
1769 		break;
1770 	}
1771 #endif
1772 	}
1773 
1774 	return (peer);
1775 }
1776 
1777 static int
1778 ovpn_transmit(struct ifnet *ifp, struct mbuf *m)
1779 {
1780 	return (ifp->if_output(ifp, m, NULL, NULL));
1781 }
1782 
1783 static int
1784 ovpn_transmit_to_peer(struct ifnet *ifp, struct mbuf *m,
1785     struct ovpn_kpeer *peer, struct rm_priotracker *_ovpn_lock_trackerp)
1786 {
1787 	struct ovpn_wire_header *ohdr;
1788 	struct ovpn_kkey *key;
1789 	struct ovpn_softc *sc;
1790 	struct cryptop *crp;
1791 	uint32_t af, seq;
1792 	size_t len, ovpn_hdr_len;
1793 	int tunnel_len;
1794 	int ret;
1795 
1796 	sc = ifp->if_softc;
1797 
1798 	OVPN_RASSERT(sc);
1799 
1800 	tunnel_len = m->m_pkthdr.len;
1801 
1802 	key = &peer->keys[OVPN_KEY_SLOT_PRIMARY];
1803 	if (key->encrypt == NULL) {
1804 		if (_ovpn_lock_trackerp != NULL)
1805 			OVPN_RUNLOCK(sc);
1806 		m_freem(m);
1807 		return (ENOLINK);
1808 	}
1809 
1810 	af = ovpn_get_af(m);
1811 	/* Don't capture control packets. */
1812 	if (af != 0)
1813 		BPF_MTAP2(ifp, &af, sizeof(af), m);
1814 
1815 	len = m->m_pkthdr.len;
1816 	MPASS(len <= ifp->if_mtu);
1817 
1818 	ovpn_hdr_len = sizeof(struct ovpn_wire_header);
1819 	if (key->encrypt->cipher == OVPN_CIPHER_ALG_NONE)
1820 		ovpn_hdr_len -= 16; /* No auth tag. */
1821 
1822 	M_PREPEND(m, ovpn_hdr_len, M_NOWAIT);
1823 	if (m == NULL) {
1824 		if (_ovpn_lock_trackerp != NULL)
1825 			OVPN_RUNLOCK(sc);
1826 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1827 		return (ENOBUFS);
1828 	}
1829 	ohdr = mtod(m, struct ovpn_wire_header *);
1830 	ohdr->opcode = (OVPN_OP_DATA_V2 << OVPN_OP_SHIFT) | key->keyid;
1831 	ohdr->opcode <<= 24;
1832 	ohdr->opcode |= key->peerid;
1833 	ohdr->opcode = htonl(ohdr->opcode);
1834 
1835 	seq = atomic_fetchadd_32(&peer->tx_seq, 1);
1836 	seq = htonl(seq);
1837 	ohdr->seq = seq;
1838 
1839 	if (key->encrypt->cipher == OVPN_CIPHER_ALG_NONE) {
1840 		ret = ovpn_encap(sc, peer->peerid, m);
1841 		if (_ovpn_lock_trackerp != NULL)
1842 			OVPN_RUNLOCK(sc);
1843 		if (ret == 0) {
1844 			OVPN_COUNTER_ADD(sc, sent_data_pkts, 1);
1845 			OVPN_COUNTER_ADD(sc, tunnel_bytes_sent, tunnel_len);
1846 		}
1847 		return (ret);
1848 	}
1849 
1850 	crp = crypto_getreq(key->encrypt->cryptoid, M_NOWAIT);
1851 	if (crp == NULL) {
1852 		if (_ovpn_lock_trackerp != NULL)
1853 			OVPN_RUNLOCK(sc);
1854 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1855 		m_freem(m);
1856 		return (ENOBUFS);
1857 	}
1858 
1859 	/* Encryption covers only the payload, not the header. */
1860 	crp->crp_payload_start = sizeof(*ohdr);
1861 	crp->crp_payload_length = len;
1862 	crp->crp_op = CRYPTO_OP_ENCRYPT;
1863 
1864 	/*
1865 	 * AAD data covers the ovpn_wire_header minus the auth
1866 	 * tag.
1867 	 */
1868 	crp->crp_aad_length = sizeof(*ohdr) - sizeof(ohdr->auth_tag);
1869 	crp->crp_aad = ohdr;
1870 	crp->crp_aad_start = 0;
1871 	crp->crp_op |= CRYPTO_OP_COMPUTE_DIGEST;
1872 	crp->crp_digest_start = offsetof(struct ovpn_wire_header, auth_tag);
1873 
1874 	crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
1875 	memcpy(crp->crp_iv, &seq, sizeof(seq));
1876 	memcpy(crp->crp_iv + sizeof(seq), key->encrypt->nonce,
1877 	    key->encrypt->noncelen);
1878 
1879 	crypto_use_mbuf(crp, m);
1880 	crp->crp_flags |= CRYPTO_F_CBIFSYNC;
1881 	crp->crp_callback = ovpn_encrypt_tx_cb;
1882 	crp->crp_opaque = peer;
1883 
1884 	atomic_add_int(&peer->refcount, 1);
1885 	if (_ovpn_lock_trackerp != NULL)
1886 		OVPN_RUNLOCK(sc);
1887 	if (V_async_crypto)
1888 		ret = crypto_dispatch_async(crp, CRYPTO_ASYNC_ORDERED);
1889 	else
1890 		ret = crypto_dispatch(crp);
1891 	if (ret) {
1892 		OVPN_COUNTER_ADD(sc, lost_data_pkts_out, 1);
1893 	}
1894 
1895 	return (ret);
1896 }
1897 
1898 /*
1899  * Note: Expects to hold the read lock on entry, and will release it itself.
1900  */
1901 static int
1902 ovpn_encap(struct ovpn_softc *sc, uint32_t peerid, struct mbuf *m)
1903 {
1904 	struct udphdr *udp;
1905 	struct ovpn_kpeer *peer;
1906 	int len;
1907 
1908 	OVPN_RLOCK_TRACKER;
1909 
1910 	OVPN_RLOCK(sc);
1911 	NET_EPOCH_ASSERT();
1912 
1913 	peer = ovpn_find_peer(sc, peerid);
1914 	if (peer == NULL || sc->ifp->if_link_state != LINK_STATE_UP) {
1915 		OVPN_RUNLOCK(sc);
1916 		OVPN_COUNTER_ADD(sc, lost_data_pkts_out, 1);
1917 		m_freem(m);
1918 		return (ENETDOWN);
1919 	}
1920 
1921 	len = m->m_pkthdr.len;
1922 
1923 	M_PREPEND(m, sizeof(struct udphdr), M_NOWAIT);
1924 	if (m == NULL) {
1925 		OVPN_RUNLOCK(sc);
1926 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1927 		m_freem(m);
1928 		return (ENOBUFS);
1929 	}
1930 	udp = mtod(m, struct udphdr *);
1931 
1932 	MPASS(peer->local.ss_family == peer->remote.ss_family);
1933 
1934 	udp->uh_sport = ovpn_get_port(&peer->local);
1935 	udp->uh_dport = ovpn_get_port(&peer->remote);
1936 	udp->uh_ulen = htons(sizeof(struct udphdr) + len);
1937 
1938 	switch (peer->remote.ss_family) {
1939 #ifdef INET
1940 	case AF_INET: {
1941 		struct sockaddr_in *in_local = TO_IN(&peer->local);
1942 		struct sockaddr_in *in_remote = TO_IN(&peer->remote);
1943 		struct ip *ip;
1944 
1945 		/*
1946 		 * This requires knowing the source IP, which we don't. Happily
1947 		 * we're allowed to keep this at 0, and the checksum won't do
1948 		 * anything the crypto won't already do.
1949 		 */
1950 		udp->uh_sum = 0;
1951 
1952 		/* Set the checksum flags so we recalculate checksums. */
1953 		m->m_pkthdr.csum_flags |= CSUM_IP;
1954 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1955 
1956 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
1957 		if (m == NULL) {
1958 			OVPN_RUNLOCK(sc);
1959 			OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1960 			return (ENOBUFS);
1961 		}
1962 		ip = mtod(m, struct ip *);
1963 
1964 		ip->ip_tos = 0;
1965 		ip->ip_len = htons(sizeof(struct ip) + sizeof(struct udphdr) +
1966 		   len);
1967 		ip->ip_off = 0;
1968 		ip->ip_ttl = V_ip_defttl;
1969 		ip->ip_p = IPPROTO_UDP;
1970 		ip->ip_sum = 0;
1971 		if (in_local->sin_port != 0)
1972 			ip->ip_src = in_local->sin_addr;
1973 		else
1974 			ip->ip_src.s_addr = INADDR_ANY;
1975 		ip->ip_dst = in_remote->sin_addr;
1976 
1977 		OVPN_RUNLOCK(sc);
1978 		OVPN_COUNTER_ADD(sc, transport_bytes_sent, m->m_pkthdr.len);
1979 
1980 		return (ip_output(m, NULL, NULL, 0, NULL, NULL));
1981 	}
1982 #endif
1983 #ifdef INET6
1984 	case AF_INET6: {
1985 		struct sockaddr_in6 *in6_local = TO_IN6(&peer->local);
1986 		struct sockaddr_in6 *in6_remote = TO_IN6(&peer->remote);
1987 		struct ip6_hdr *ip6;
1988 
1989 		M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
1990 		if (m == NULL) {
1991 			OVPN_RUNLOCK(sc);
1992 			OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1993 			return (ENOBUFS);
1994 		}
1995 		m = m_pullup(m, sizeof(*ip6) + sizeof(*udp));
1996 		if (m == NULL) {
1997 			OVPN_RUNLOCK(sc);
1998 			OVPN_COUNTER_ADD(sc, nomem_data_pkts_out, 1);
1999 			return (ENOBUFS);
2000 		}
2001 
2002 		ip6 = mtod(m, struct ip6_hdr *);
2003 
2004 		ip6->ip6_vfc = IPV6_VERSION;
2005 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
2006 		ip6->ip6_plen = htons(sizeof(*ip6) + sizeof(struct udphdr) +
2007 		    len);
2008 		ip6->ip6_nxt = IPPROTO_UDP;
2009 		ip6->ip6_hlim = V_ip6_defhlim;
2010 
2011 		memcpy(&ip6->ip6_src, &in6_local->sin6_addr,
2012 		    sizeof(ip6->ip6_src));
2013 		memcpy(&ip6->ip6_dst, &in6_remote->sin6_addr,
2014 		    sizeof(ip6->ip6_dst));
2015 
2016 		udp = mtodo(m, sizeof(*ip6));
2017 		udp->uh_sum = in6_cksum_pseudo(ip6,
2018 		    m->m_pkthdr.len - sizeof(struct ip6_hdr),
2019 		    IPPROTO_UDP, 0);
2020 
2021 		m->m_pkthdr.csum_flags |= CSUM_UDP_IPV6;
2022 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2023 
2024 		OVPN_RUNLOCK(sc);
2025 		OVPN_COUNTER_ADD(sc, transport_bytes_sent, m->m_pkthdr.len);
2026 
2027 		return (ip6_output(m, NULL, NULL, IPV6_UNSPECSRC, NULL, NULL,
2028 		    NULL));
2029 	}
2030 #endif
2031 	default:
2032 		panic("Unsupported address family %d",
2033 		    peer->remote.ss_family);
2034 	}
2035 }
2036 
2037 static int
2038 ovpn_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
2039 	struct route *ro)
2040 {
2041 	struct ovpn_softc *sc;
2042 	struct ovpn_kpeer *peer;
2043 
2044 	OVPN_RLOCK_TRACKER;
2045 
2046 	sc = ifp->if_softc;
2047 
2048 	OVPN_RLOCK(sc);
2049 
2050 	SDT_PROBE1(if_ovpn, tx, transmit, start, m);
2051 
2052 	if (__predict_false(ifp->if_link_state != LINK_STATE_UP)) {
2053 		OVPN_COUNTER_ADD(sc, lost_data_pkts_out, 1);
2054 		OVPN_RUNLOCK(sc);
2055 		m_freem(m);
2056 		return (ENETDOWN);
2057 	}
2058 
2059 	/**
2060 	 * Only obey 'dst' (i.e. the gateway) if no route is supplied.
2061 	 * That's our indication that we're being called through pf's route-to,
2062 	 * and we should route according to 'dst' instead. We can't do so
2063 	 * consistently, because the usual openvpn configuration sets the first
2064 	 * non-server IP in the subnet as the gateway. If we always use that
2065 	 * one we'd end up routing all traffic to the first client.
2066 	 * tl;dr: 'ro == NULL' tells us pf is doing a route-to, and then but
2067 	 * only then, we should treat 'dst' as the destination. */
2068 	peer = ovpn_route_peer(sc, &m, ro == NULL ? dst : NULL);
2069 	if (peer == NULL) {
2070 		/* No destination. */
2071 		OVPN_COUNTER_ADD(sc, lost_data_pkts_out, 1);
2072 		OVPN_RUNLOCK(sc);
2073 		m_freem(m);
2074 		return (ENETDOWN);
2075 	}
2076 
2077 	return (ovpn_transmit_to_peer(ifp, m, peer, _ovpn_lock_trackerp));
2078 }
2079 
2080 static void
2081 ovpn_rcv_ctrl(struct ovpn_softc *sc, struct mbuf *m, int off)
2082 {
2083 	/* Lop off the IP and UDP headers */
2084 	m_adj_decap(m, off);
2085 
2086 	/* Keep in the local ring until userspace fetches it. */
2087 	if (buf_ring_enqueue(sc->rxring, m) != 0) {
2088 		OVPN_COUNTER_ADD(sc, lost_ctrl_pkts_in, 1);
2089 		m_freem(m);
2090 		return;
2091 	}
2092 
2093 	OVPN_COUNTER_ADD(sc, received_ctrl_pkts, 1);
2094 }
2095 
2096 static bool
2097 ovpn_check_replay(struct ovpn_kkey_dir *key, uint32_t seq)
2098 {
2099 	uint32_t d;
2100 
2101 	mtx_lock(&key->replay_mtx);
2102 
2103 	/* Sequence number must be strictly greater than rx_seq */
2104 	if (seq <= key->rx_seq) {
2105 		mtx_unlock(&key->replay_mtx);
2106 		return (false);
2107 	}
2108 
2109 	/* Large jump. The packet authenticated okay, so just accept that. */
2110 	if (seq > (key->rx_seq + (sizeof(key->rx_window) * 8))) {
2111 		key->rx_seq = seq;
2112 		key->rx_window = 0;
2113 		mtx_unlock(&key->replay_mtx);
2114 		return (true);
2115 	}
2116 
2117 	/* Happy case. */
2118 	if ((seq == key->rx_seq + 1) && key->rx_window == 0) {
2119 		key->rx_seq++;
2120 		mtx_unlock(&key->replay_mtx);
2121 		return (true);
2122 	}
2123 
2124 	d = seq - key->rx_seq - 1;
2125 
2126 	if (key->rx_window & ((uint64_t)1 << d)) {
2127 		/* Dupe! */
2128 		mtx_unlock(&key->replay_mtx);
2129 		return (false);
2130 	}
2131 
2132 	key->rx_window |= (uint64_t)1 << d;
2133 
2134 	while (key->rx_window & 1) {
2135 		key->rx_seq++;
2136 		key->rx_window >>= 1;
2137 	}
2138 
2139 	mtx_unlock(&key->replay_mtx);
2140 
2141 	return (true);
2142 }
2143 
2144 static struct ovpn_kpeer *
2145 ovpn_peer_from_mbuf(struct ovpn_softc *sc, struct mbuf *m, int off)
2146 {
2147 	struct ovpn_wire_header ohdr;
2148 	uint32_t peerid;
2149 	const size_t hdrlen = sizeof(ohdr) - sizeof(ohdr.auth_tag);
2150 
2151 	OVPN_RASSERT(sc);
2152 
2153 	if (m_length(m, NULL) < (off + sizeof(struct udphdr) + hdrlen))
2154 		return (NULL);
2155 
2156 	m_copydata(m, off + sizeof(struct udphdr), hdrlen, (caddr_t)&ohdr);
2157 
2158 	peerid = ntohl(ohdr.opcode) & 0x00ffffff;
2159 
2160 	return (ovpn_find_peer(sc, peerid));
2161 }
2162 
2163 static bool
2164 ovpn_udp_input(struct mbuf *m, int off, struct inpcb *inp,
2165     const struct sockaddr *sa, void *ctx)
2166 {
2167 	struct ovpn_softc *sc = ctx;
2168 	struct ovpn_wire_header *ohdr;
2169 	struct udphdr *uhdr;
2170 	struct ovpn_kkey *key;
2171 	struct cryptop *crp;
2172 	struct ovpn_kpeer *peer;
2173 	size_t ohdrlen;
2174 	int ret;
2175 	uint8_t op;
2176 
2177 	OVPN_RLOCK_TRACKER;
2178 
2179 	M_ASSERTPKTHDR(m);
2180 
2181 	OVPN_COUNTER_ADD(sc, transport_bytes_received, m->m_pkthdr.len - off);
2182 
2183 	ohdrlen = sizeof(*ohdr) - sizeof(ohdr->auth_tag);
2184 
2185 	OVPN_RLOCK(sc);
2186 
2187 	peer = ovpn_peer_from_mbuf(sc, m, off);
2188 	if (peer == NULL) {
2189 		OVPN_RUNLOCK(sc);
2190 		return (false);
2191 	}
2192 
2193 	m = m_pullup(m, off + sizeof(*uhdr) + ohdrlen);
2194 	if (m == NULL) {
2195 		OVPN_RUNLOCK(sc);
2196 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_in, 1);
2197 		return (true);
2198 	}
2199 	uhdr = mtodo(m, off);
2200 	ohdr = mtodo(m, off + sizeof(*uhdr));
2201 
2202 	op = ntohl(ohdr->opcode) >> 24 >> OVPN_OP_SHIFT;
2203 
2204 	/*
2205 	 * Simplify things by getting rid of the preceding headers, we don't
2206 	 * care about them.
2207 	 */
2208 	m_adj_decap(m, off);
2209 
2210 	uhdr = mtodo(m, 0);
2211 	ohdr = mtodo(m, sizeof(*uhdr));
2212 
2213 	if (op != OVPN_OP_DATA_V2) {
2214 		OVPN_RUNLOCK(sc);
2215 		ovpn_rcv_ctrl(sc, m, sizeof(struct udphdr));
2216 		INP_WLOCK(inp);
2217 		udp_notify(inp, EAGAIN);
2218 		INP_WUNLOCK(inp);
2219 		return (true);
2220 	}
2221 
2222 	key = ovpn_find_key(sc, peer, ohdr);
2223 	if (key == NULL || key->decrypt == NULL) {
2224 		OVPN_RUNLOCK(sc);
2225 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
2226 		m_freem(m);
2227 		return (true);
2228 	}
2229 
2230 	if (key->decrypt->cipher == OVPN_CIPHER_ALG_NONE) {
2231 		/* Now remove the outer headers */
2232 		m_adj_decap(m, sizeof(struct udphdr) + ohdrlen);
2233 
2234 		ohdr = mtodo(m, sizeof(*uhdr));
2235 
2236 		ovpn_finish_rx(sc, m, peer, key, ntohl(ohdr->seq),
2237 		    _ovpn_lock_trackerp);
2238 		OVPN_UNLOCK_ASSERT(sc);
2239 		return (true);
2240 	}
2241 
2242 	ohdrlen += sizeof(ohdr->auth_tag);
2243 
2244 	m = m_pullup(m, sizeof(*uhdr) + ohdrlen);
2245 	if (m == NULL) {
2246 		OVPN_RUNLOCK(sc);
2247 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_in, 1);
2248 		return (true);
2249 	}
2250 	uhdr = mtodo(m, 0);
2251 	ohdr = mtodo(m, sizeof(*uhdr));
2252 
2253 	/* Decrypt */
2254 	crp = crypto_getreq(key->decrypt->cryptoid, M_NOWAIT);
2255 	if (crp == NULL) {
2256 		OVPN_COUNTER_ADD(sc, nomem_data_pkts_in, 1);
2257 		OVPN_RUNLOCK(sc);
2258 		m_freem(m);
2259 		return (true);
2260 	}
2261 
2262 	crp->crp_payload_start = sizeof(struct udphdr) + sizeof(*ohdr);
2263 	crp->crp_payload_length = ntohs(uhdr->uh_ulen) -
2264 	    sizeof(*uhdr) - sizeof(*ohdr);
2265 	crp->crp_op = CRYPTO_OP_DECRYPT;
2266 
2267 	/* AAD validation. */
2268 	crp->crp_aad_length = sizeof(*ohdr) - sizeof(ohdr->auth_tag);
2269 	crp->crp_aad = ohdr;
2270 	crp->crp_aad_start = 0;
2271 	crp->crp_op |= CRYPTO_OP_VERIFY_DIGEST;
2272 	crp->crp_digest_start = sizeof(struct udphdr) +
2273 	    offsetof(struct ovpn_wire_header, auth_tag);
2274 
2275 	crp->crp_flags |= CRYPTO_F_IV_SEPARATE;
2276 	memcpy(crp->crp_iv, &ohdr->seq, sizeof(ohdr->seq));
2277 	memcpy(crp->crp_iv + sizeof(ohdr->seq), key->decrypt->nonce,
2278 	    key->decrypt->noncelen);
2279 
2280 	crypto_use_mbuf(crp, m);
2281 	crp->crp_flags |= CRYPTO_F_CBIFSYNC;
2282 	crp->crp_callback = ovpn_decrypt_rx_cb;
2283 	crp->crp_opaque = sc;
2284 
2285 	atomic_add_int(&sc->refcount, 1);
2286 	OVPN_RUNLOCK(sc);
2287 	if (V_async_crypto)
2288 		ret = crypto_dispatch_async(crp, CRYPTO_ASYNC_ORDERED);
2289 	else
2290 		ret = crypto_dispatch(crp);
2291 	if (ret != 0) {
2292 		OVPN_COUNTER_ADD(sc, lost_data_pkts_in, 1);
2293 	}
2294 
2295 	return (true);
2296 }
2297 
2298 static void
2299 ovpn_qflush(struct ifnet *ifp __unused)
2300 {
2301 
2302 }
2303 
2304 static void
2305 ovpn_flush_rxring(struct ovpn_softc *sc)
2306 {
2307 	struct mbuf *m;
2308 	struct ovpn_notification *n;
2309 
2310 	OVPN_WASSERT(sc);
2311 
2312 	while (! buf_ring_empty(sc->rxring)) {
2313 		m = buf_ring_dequeue_sc(sc->rxring);
2314 		m_freem(m);
2315 	}
2316 
2317 	while (! buf_ring_empty(sc->notifring)) {
2318 		n = buf_ring_dequeue_sc(sc->notifring);
2319 		free(n, M_OVPN);
2320 	}
2321 }
2322 
2323 #ifdef VIMAGE
2324 static void
2325 ovpn_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
2326     char *unused __unused)
2327 {
2328 	struct ovpn_softc *sc = ifp->if_softc;
2329 	int i;
2330 	int ret __diagused;
2331 
2332 	i = 0;
2333 
2334 	OVPN_WLOCK(sc);
2335 
2336 	/* Flush keys & configuration. */
2337 	do {
2338 		if (sc->peers[i] != NULL) {
2339 			ret = _ovpn_del_peer(sc, sc->peers[i]->peerid);
2340 			MPASS(ret == 0);
2341 		}
2342 		i++;
2343 	} while (i < OVPN_MAX_PEERS);
2344 
2345 	ovpn_flush_rxring(sc);
2346 
2347 	OVPN_WUNLOCK(sc);
2348 }
2349 #endif
2350 
2351 static int
2352 ovpn_clone_match(struct if_clone *ifc, const char *name)
2353 {
2354 	/*
2355 	 * Allow all names that start with 'ovpn', specifically because pfSense
2356 	 * uses ovpnc1 / ovpns2
2357 	 */
2358 	return (strncmp(ovpnname, name, strlen(ovpnname)) == 0);
2359 }
2360 
2361 static int
2362 ovpn_clone_create(struct if_clone *ifc, char *name, size_t len,
2363     struct ifc_data *ifd, struct ifnet **ifpp)
2364 {
2365 	struct ovpn_softc *sc;
2366 	struct ifnet *ifp;
2367 	char *dp;
2368 	int error, unit, wildcard;
2369 
2370 	/* Try to see if a special unit was requested. */
2371 	error = ifc_name2unit(name, &unit);
2372 	if (error != 0)
2373 		return (error);
2374 	wildcard = (unit < 0);
2375 
2376 	error = ifc_alloc_unit(ifc, &unit);
2377 	if (error != 0)
2378 		return (error);
2379 
2380 	/*
2381 	 * If no unit had been given, we need to adjust the ifName.
2382 	 */
2383 	for (dp = name; *dp != '\0'; dp++);
2384 	if (wildcard) {
2385 		error = snprintf(dp, len - (dp - name), "%d", unit);
2386 		if (error > len - (dp - name)) {
2387 			/* ifName too long. */
2388 			ifc_free_unit(ifc, unit);
2389 			return (ENOSPC);
2390 		}
2391 		dp += error;
2392 	}
2393 
2394 	/* Make sure it doesn't already exist. */
2395 	if (ifunit(name) != NULL)
2396 		return (EEXIST);
2397 
2398 	sc = malloc(sizeof(struct ovpn_softc), M_OVPN, M_WAITOK | M_ZERO);
2399 	sc->ifp = if_alloc(IFT_ENC);
2400 	rm_init_flags(&sc->lock, "if_ovpn_lock", RM_RECURSE);
2401 	sc->refcount = 0;
2402 
2403 	sc->rxring = buf_ring_alloc(32, M_OVPN, M_WAITOK, NULL);
2404 	sc->notifring = buf_ring_alloc(32, M_OVPN, M_WAITOK, NULL);
2405 
2406 	COUNTER_ARRAY_ALLOC(sc->counters, OVPN_COUNTER_SIZE, M_WAITOK);
2407 
2408 	ifp = sc->ifp;
2409 	ifp->if_softc = sc;
2410 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
2411 	ifp->if_dname = ovpngroupname;
2412 	ifp->if_dunit = unit;
2413 
2414 	ifp->if_addrlen = 0;
2415 	ifp->if_mtu = 1428;
2416 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
2417 	ifp->if_ioctl = ovpn_ioctl;
2418 	ifp->if_transmit = ovpn_transmit;
2419 	ifp->if_output = ovpn_output;
2420 	ifp->if_qflush = ovpn_qflush;
2421 #ifdef VIMAGE
2422 	ifp->if_reassign = ovpn_reassign;
2423 #endif
2424 	ifp->if_capabilities |= IFCAP_LINKSTATE;
2425 	ifp->if_capenable |= IFCAP_LINKSTATE;
2426 
2427 	if_attach(ifp);
2428 	bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
2429 	*ifpp = ifp;
2430 
2431 	return (0);
2432 }
2433 
2434 static void
2435 ovpn_clone_destroy_cb(struct epoch_context *ctx)
2436 {
2437 	struct ovpn_softc *sc;
2438 
2439 	sc = __containerof(ctx, struct ovpn_softc, epoch_ctx);
2440 
2441 	MPASS(sc->peercount == 0);
2442 	for (int i = 0; i < OVPN_MAX_PEERS; i++) {
2443 		MPASS(sc->peers[i] == NULL);
2444 	}
2445 
2446 	COUNTER_ARRAY_FREE(sc->counters, OVPN_COUNTER_SIZE);
2447 
2448 	if_free(sc->ifp);
2449 	free(sc, M_OVPN);
2450 }
2451 
2452 static int
2453 ovpn_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
2454 {
2455 	struct ovpn_softc *sc;
2456 	int unit;
2457 	int i;
2458 	int ret __diagused;
2459 
2460 	sc = ifp->if_softc;
2461 	unit = ifp->if_dunit;
2462 
2463 	OVPN_WLOCK(sc);
2464 
2465 	if (atomic_load_int(&sc->refcount) > 0) {
2466 		OVPN_WUNLOCK(sc);
2467 		return (EBUSY);
2468 	}
2469 
2470 	i = 0;
2471 	do {
2472 		if (sc->peers[i] != NULL) {
2473 			ret = _ovpn_del_peer(sc, sc->peers[i]->peerid);
2474 			MPASS(ret == 0);
2475 		}
2476 		i++;
2477 	} while (i < OVPN_MAX_PEERS);
2478 
2479 	ovpn_flush_rxring(sc);
2480 	buf_ring_free(sc->rxring, M_OVPN);
2481 	buf_ring_free(sc->notifring, M_OVPN);
2482 
2483 	OVPN_WUNLOCK(sc);
2484 
2485 	bpfdetach(ifp);
2486 	if_detach(ifp);
2487 	ifp->if_softc = NULL;
2488 
2489 	NET_EPOCH_CALL(ovpn_clone_destroy_cb, &sc->epoch_ctx);
2490 
2491 	if (unit != IF_DUNIT_NONE)
2492 		ifc_free_unit(ifc, unit);
2493 
2494 	NET_EPOCH_DRAIN_CALLBACKS();
2495 
2496 	return (0);
2497 }
2498 
2499 static void
2500 vnet_ovpn_init(const void *unused __unused)
2501 {
2502 	struct if_clone_addreq req = {
2503 		.match_f = ovpn_clone_match,
2504 		.create_f = ovpn_clone_create,
2505 		.destroy_f = ovpn_clone_destroy,
2506 	};
2507 	V_ovpn_cloner = ifc_attach_cloner(ovpngroupname, &req);
2508 }
2509 VNET_SYSINIT(vnet_ovpn_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
2510     vnet_ovpn_init, NULL);
2511 
2512 static void
2513 vnet_ovpn_uninit(const void *unused __unused)
2514 {
2515 	if_clone_detach(V_ovpn_cloner);
2516 }
2517 VNET_SYSUNINIT(vnet_ovpn_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
2518     vnet_ovpn_uninit, NULL);
2519 
2520 static int
2521 ovpnmodevent(module_t mod, int type, void *data)
2522 {
2523 	switch (type) {
2524 	case MOD_LOAD:
2525 		/* Done in vnet_ovpn_init() */
2526 		break;
2527 	case MOD_UNLOAD:
2528 		/* Done in vnet_ovpn_uninit() */
2529 		break;
2530 	default:
2531 		return (EOPNOTSUPP);
2532 	}
2533 
2534 	return (0);
2535 }
2536 
2537 static moduledata_t ovpn_mod = {
2538 	"if_ovpn",
2539 	ovpnmodevent,
2540 	0
2541 };
2542 
2543 DECLARE_MODULE(if_ovpn, ovpn_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
2544 MODULE_VERSION(if_ovpn, 1);
2545