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