if_ovpn.c 58 KB

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