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