keysock.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* $KAME: keysock.c,v 1.25 2001/08/13 20:07:41 itojun Exp $ */
  2. /*-
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the project nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include "opt_ipsec.h"
  33. /* This code has derived from sys/net/rtsock.c on FreeBSD2.2.5 */
  34. #include <sys/types.h>
  35. #include <sys/param.h>
  36. #include <sys/domain.h>
  37. #include <sys/errno.h>
  38. #include <sys/kernel.h>
  39. #include <sys/lock.h>
  40. #include <sys/malloc.h>
  41. #include <sys/mbuf.h>
  42. #include <sys/mutex.h>
  43. #include <sys/priv.h>
  44. #include <sys/protosw.h>
  45. #include <sys/signalvar.h>
  46. #include <sys/socket.h>
  47. #include <sys/socketvar.h>
  48. #include <sys/sysctl.h>
  49. #include <sys/systm.h>
  50. #include <net/if.h>
  51. #include <net/vnet.h>
  52. #include <netinet/in.h>
  53. #include <net/pfkeyv2.h>
  54. #include <netipsec/key.h>
  55. #include <netipsec/keysock.h>
  56. #include <netipsec/key_debug.h>
  57. #include <netipsec/ipsec.h>
  58. #include <machine/stdarg.h>
  59. static struct mtx keysock_mtx;
  60. MTX_SYSINIT(keysock, &keysock_mtx, "key socket pcb list", MTX_DEF);
  61. #define KEYSOCK_LOCK() mtx_lock(&keysock_mtx)
  62. #define KEYSOCK_UNLOCK() mtx_unlock(&keysock_mtx)
  63. VNET_DEFINE_STATIC(LIST_HEAD(, keycb), keycb_list) =
  64. LIST_HEAD_INITIALIZER(keycb_list);
  65. #define V_keycb_list VNET(keycb_list)
  66. static struct sockaddr key_src = { 2, PF_KEY, };
  67. static int key_sendup0(struct keycb *, struct mbuf *, int);
  68. VNET_PCPUSTAT_DEFINE(struct pfkeystat, pfkeystat);
  69. VNET_PCPUSTAT_SYSINIT(pfkeystat);
  70. #ifdef VIMAGE
  71. VNET_PCPUSTAT_SYSUNINIT(pfkeystat);
  72. #endif /* VIMAGE */
  73. static int
  74. key_send(struct socket *so, int flags, struct mbuf *m,
  75. struct sockaddr *nam, struct mbuf *control, struct thread *td)
  76. {
  77. struct sadb_msg *msg;
  78. int len, error = 0;
  79. if ((flags & PRUS_OOB) || control != NULL) {
  80. m_freem(m);
  81. if (control != NULL)
  82. m_freem(control);
  83. return (EOPNOTSUPP);
  84. }
  85. PFKEYSTAT_INC(out_total);
  86. PFKEYSTAT_ADD(out_bytes, m->m_pkthdr.len);
  87. len = m->m_pkthdr.len;
  88. if (len < sizeof(struct sadb_msg)) {
  89. PFKEYSTAT_INC(out_tooshort);
  90. error = EINVAL;
  91. goto end;
  92. }
  93. if (m->m_len < sizeof(struct sadb_msg)) {
  94. if ((m = m_pullup(m, sizeof(struct sadb_msg))) == NULL) {
  95. PFKEYSTAT_INC(out_nomem);
  96. error = ENOBUFS;
  97. goto end;
  98. }
  99. }
  100. M_ASSERTPKTHDR(m);
  101. KEYDBG(KEY_DUMP, kdebug_mbuf(m));
  102. msg = mtod(m, struct sadb_msg *);
  103. PFKEYSTAT_INC(out_msgtype[msg->sadb_msg_type]);
  104. if (len != PFKEY_UNUNIT64(msg->sadb_msg_len)) {
  105. PFKEYSTAT_INC(out_invlen);
  106. error = EINVAL;
  107. goto end;
  108. }
  109. error = key_parse(m, so);
  110. m = NULL;
  111. end:
  112. if (m != NULL)
  113. m_freem(m);
  114. return (error);
  115. }
  116. /*
  117. * send message to the socket.
  118. */
  119. static int
  120. key_sendup0(struct keycb *kp, struct mbuf *m, int promisc)
  121. {
  122. if (promisc) {
  123. struct sadb_msg *pmsg;
  124. M_PREPEND(m, sizeof(struct sadb_msg), M_NOWAIT);
  125. if (m == NULL) {
  126. PFKEYSTAT_INC(in_nomem);
  127. return (ENOBUFS);
  128. }
  129. pmsg = mtod(m, struct sadb_msg *);
  130. bzero(pmsg, sizeof(*pmsg));
  131. pmsg->sadb_msg_version = PF_KEY_V2;
  132. pmsg->sadb_msg_type = SADB_X_PROMISC;
  133. pmsg->sadb_msg_len = PFKEY_UNIT64(m->m_pkthdr.len);
  134. /* pid and seq? */
  135. PFKEYSTAT_INC(in_msgtype[pmsg->sadb_msg_type]);
  136. }
  137. if (!sbappendaddr(&kp->kp_socket->so_rcv, &key_src, m, NULL)) {
  138. PFKEYSTAT_INC(in_nomem);
  139. m_freem(m);
  140. soroverflow(kp->kp_socket);
  141. return (ENOBUFS);
  142. }
  143. sorwakeup(kp->kp_socket);
  144. return (0);
  145. }
  146. /* so can be NULL if target != KEY_SENDUP_ONE */
  147. int
  148. key_sendup_mbuf(struct socket *so, struct mbuf *m, int target)
  149. {
  150. struct mbuf *n;
  151. struct keycb *kp;
  152. int error = 0;
  153. KASSERT(m != NULL, ("NULL mbuf pointer was passed."));
  154. KASSERT(so != NULL || target != KEY_SENDUP_ONE,
  155. ("NULL socket pointer was passed."));
  156. KASSERT(target == KEY_SENDUP_ONE || target == KEY_SENDUP_ALL ||
  157. target == KEY_SENDUP_REGISTERED, ("Wrong target %d", target));
  158. PFKEYSTAT_INC(in_total);
  159. PFKEYSTAT_ADD(in_bytes, m->m_pkthdr.len);
  160. if (m->m_len < sizeof(struct sadb_msg)) {
  161. m = m_pullup(m, sizeof(struct sadb_msg));
  162. if (m == NULL) {
  163. PFKEYSTAT_INC(in_nomem);
  164. return (ENOBUFS);
  165. }
  166. }
  167. if (m->m_len >= sizeof(struct sadb_msg)) {
  168. struct sadb_msg *msg;
  169. msg = mtod(m, struct sadb_msg *);
  170. PFKEYSTAT_INC(in_msgtype[msg->sadb_msg_type]);
  171. }
  172. KEYSOCK_LOCK();
  173. LIST_FOREACH(kp, &V_keycb_list, kp_next) {
  174. /*
  175. * If you are in promiscuous mode, and when you get broadcasted
  176. * reply, you'll get two PF_KEY messages.
  177. * (based on pf_key@inner.net message on 14 Oct 1998)
  178. */
  179. if (kp->kp_promisc) {
  180. n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
  181. if (n != NULL)
  182. key_sendup0(kp, n, 1);
  183. else
  184. PFKEYSTAT_INC(in_nomem);
  185. }
  186. /* the exact target will be processed later */
  187. if (so != NULL && so->so_pcb == kp)
  188. continue;
  189. if (target == KEY_SENDUP_ONE || (target ==
  190. KEY_SENDUP_REGISTERED && kp->kp_registered == 0))
  191. continue;
  192. /* KEY_SENDUP_ALL + KEY_SENDUP_REGISTERED */
  193. n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
  194. if (n == NULL) {
  195. PFKEYSTAT_INC(in_nomem);
  196. /* Try send to another socket */
  197. continue;
  198. }
  199. if (key_sendup0(kp, n, 0) == 0)
  200. PFKEYSTAT_INC(in_msgtarget[target]);
  201. }
  202. if (so) { /* KEY_SENDUP_ONE */
  203. error = key_sendup0(so->so_pcb, m, 0);
  204. if (error == 0)
  205. PFKEYSTAT_INC(in_msgtarget[KEY_SENDUP_ONE]);
  206. } else {
  207. error = 0;
  208. m_freem(m);
  209. }
  210. KEYSOCK_UNLOCK();
  211. return (error);
  212. }
  213. static u_long key_sendspace = 8192;
  214. SYSCTL_ULONG(_net_key, OID_AUTO, sendspace, CTLFLAG_RW, &key_sendspace, 0,
  215. "Default key socket send space");
  216. static u_long key_recvspace = 8192;
  217. SYSCTL_ULONG(_net_key, OID_AUTO, recvspace, CTLFLAG_RW, &key_recvspace, 0,
  218. "Default key socket receive space");
  219. static int
  220. key_attach(struct socket *so, int proto, struct thread *td)
  221. {
  222. struct keycb *kp;
  223. int error;
  224. KASSERT(so->so_pcb == NULL, ("key_attach: so_pcb != NULL"));
  225. if (td != NULL) {
  226. error = priv_check(td, PRIV_NET_RAW);
  227. if (error != 0)
  228. return (error);
  229. }
  230. error = soreserve(so, key_sendspace, key_recvspace);
  231. if (error != 0)
  232. return (error);
  233. kp = malloc(sizeof(*kp), M_PCB, M_WAITOK);
  234. kp->kp_socket = so;
  235. kp->kp_promisc = kp->kp_registered = 0;
  236. so->so_pcb = kp;
  237. so->so_options |= SO_USELOOPBACK;
  238. KEYSOCK_LOCK();
  239. LIST_INSERT_HEAD(&V_keycb_list, kp, kp_next);
  240. KEYSOCK_UNLOCK();
  241. soisconnected(so);
  242. return (0);
  243. }
  244. static void
  245. key_close(struct socket *so)
  246. {
  247. soisdisconnected(so);
  248. }
  249. static void
  250. key_detach(struct socket *so)
  251. {
  252. struct keycb *kp = so->so_pcb;
  253. key_freereg(so);
  254. KEYSOCK_LOCK();
  255. LIST_REMOVE(kp, kp_next);
  256. KEYSOCK_UNLOCK();
  257. free(kp, M_PCB);
  258. so->so_pcb = NULL;
  259. }
  260. static int
  261. key_shutdown(struct socket *so, enum shutdown_how how)
  262. {
  263. /*
  264. * Note: key socket marks itself as connected through its lifetime.
  265. */
  266. switch (how) {
  267. case SHUT_RD:
  268. socantrcvmore(so);
  269. sbrelease(so, SO_RCV);
  270. break;
  271. case SHUT_RDWR:
  272. socantrcvmore(so);
  273. sbrelease(so, SO_RCV);
  274. /* FALLTHROUGH */
  275. case SHUT_WR:
  276. socantsendmore(so);
  277. }
  278. return (0);
  279. }
  280. SYSCTL_NODE(_net, PF_KEY, key, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  281. "Key Family");
  282. static struct protosw keysw = {
  283. .pr_type = SOCK_RAW,
  284. .pr_protocol = PF_KEY_V2,
  285. .pr_flags = PR_ATOMIC | PR_ADDR,
  286. .pr_abort = key_close,
  287. .pr_attach = key_attach,
  288. .pr_detach = key_detach,
  289. .pr_send = key_send,
  290. .pr_shutdown = key_shutdown,
  291. .pr_close = key_close,
  292. };
  293. static struct domain keydomain = {
  294. .dom_family = PF_KEY,
  295. .dom_name = "key",
  296. .dom_nprotosw = 1,
  297. .dom_protosw = { &keysw },
  298. };
  299. DOMAIN_SET(key);