xform_tcp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2003 Bruce M. Simpson <bms@spc.org>
  5. * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  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. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /* TCP MD5 Signature Option (RFC2385) */
  31. #include <sys/cdefs.h>
  32. #include "opt_inet.h"
  33. #include "opt_inet6.h"
  34. #include "opt_ipsec.h"
  35. #include <sys/param.h>
  36. #include <sys/systm.h>
  37. #include <sys/mbuf.h>
  38. #include <sys/lock.h>
  39. #include <sys/md5.h>
  40. #include <sys/rmlock.h>
  41. #include <sys/socket.h>
  42. #include <sys/sockopt.h>
  43. #include <sys/kernel.h>
  44. #include <sys/module.h>
  45. #include <sys/protosw.h>
  46. #include <netinet/in.h>
  47. #include <netinet/in_pcb.h>
  48. #include <netinet/in_systm.h>
  49. #include <netinet/ip.h>
  50. #include <netinet/ip_var.h>
  51. #include <netinet/tcp.h>
  52. #include <netinet/tcp_var.h>
  53. #include <netinet/udp.h>
  54. #include <net/vnet.h>
  55. #include <netipsec/ipsec.h>
  56. #include <netipsec/ipsec_support.h>
  57. #include <netipsec/xform.h>
  58. #ifdef INET6
  59. #include <netinet/ip6.h>
  60. #include <netipsec/ipsec6.h>
  61. #endif
  62. #include <netipsec/key.h>
  63. #include <netipsec/key_debug.h>
  64. #define TCP_SIGLEN 16 /* length of computed digest in bytes */
  65. #define TCP_KEYLEN_MIN 1 /* minimum length of TCP-MD5 key */
  66. #define TCP_KEYLEN_MAX 80 /* maximum length of TCP-MD5 key */
  67. static int
  68. tcp_ipsec_pcbctl(struct inpcb *inp, struct sockopt *sopt)
  69. {
  70. struct tcpcb *tp;
  71. int error, optval;
  72. if (sopt->sopt_name != TCP_MD5SIG) {
  73. return (ENOPROTOOPT);
  74. }
  75. if (sopt->sopt_dir == SOPT_GET) {
  76. INP_RLOCK(inp);
  77. if (inp->inp_flags & INP_DROPPED) {
  78. INP_RUNLOCK(inp);
  79. return (ECONNRESET);
  80. }
  81. tp = intotcpcb(inp);
  82. optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
  83. INP_RUNLOCK(inp);
  84. /* On success return with released INP_WLOCK */
  85. return (sooptcopyout(sopt, &optval, sizeof(optval)));
  86. }
  87. error = sooptcopyin(sopt, &optval, sizeof(optval), sizeof(optval));
  88. if (error != 0)
  89. return (error);
  90. /* INP_WLOCK_RECHECK */
  91. INP_WLOCK(inp);
  92. if (inp->inp_flags & INP_DROPPED) {
  93. INP_WUNLOCK(inp);
  94. return (ECONNRESET);
  95. }
  96. tp = intotcpcb(inp);
  97. if (optval > 0)
  98. tp->t_flags |= TF_SIGNATURE;
  99. else
  100. tp->t_flags &= ~TF_SIGNATURE;
  101. INP_WUNLOCK(inp);
  102. return (error);
  103. }
  104. /*
  105. * Callback function invoked by m_apply() to digest TCP segment data
  106. * contained within an mbuf chain.
  107. */
  108. static int
  109. tcp_signature_apply(void *fstate, void *data, u_int len)
  110. {
  111. MD5Update(fstate, (u_char *)data, len);
  112. return (0);
  113. }
  114. #ifdef INET
  115. static int
  116. ip_pseudo_compute(struct mbuf *m, MD5_CTX *ctx)
  117. {
  118. struct ippseudo ipp;
  119. struct ip *ip;
  120. int hdr_len;
  121. ip = mtod(m, struct ip *);
  122. ipp.ippseudo_src.s_addr = ip->ip_src.s_addr;
  123. ipp.ippseudo_dst.s_addr = ip->ip_dst.s_addr;
  124. ipp.ippseudo_p = IPPROTO_TCP;
  125. ipp.ippseudo_pad = 0;
  126. hdr_len = ip->ip_hl << 2;
  127. if (ip->ip_p == IPPROTO_UDP)
  128. /* TCP over UDP */
  129. hdr_len += sizeof(struct udphdr);
  130. ipp.ippseudo_len = htons(m->m_pkthdr.len - hdr_len);
  131. MD5Update(ctx, (char *)&ipp, sizeof(ipp));
  132. return (hdr_len);
  133. }
  134. #endif
  135. #ifdef INET6
  136. static int
  137. ip6_pseudo_compute(struct mbuf *m, MD5_CTX *ctx)
  138. {
  139. struct ip6_pseudo {
  140. struct in6_addr src, dst;
  141. uint32_t len;
  142. uint32_t nxt;
  143. } ip6p __aligned(4);
  144. struct ip6_hdr *ip6;
  145. int hdr_len;
  146. ip6 = mtod(m, struct ip6_hdr *);
  147. ip6p.src = ip6->ip6_src;
  148. ip6p.dst = ip6->ip6_dst;
  149. hdr_len = sizeof(struct ip6_hdr);
  150. if (ip6->ip6_nxt == IPPROTO_UDP)
  151. /* TCP over UDP */
  152. hdr_len += sizeof(struct udphdr);
  153. /* XXX: ext headers */
  154. ip6p.len = htonl(m->m_pkthdr.len - hdr_len);
  155. ip6p.nxt = htonl(IPPROTO_TCP);
  156. MD5Update(ctx, (char *)&ip6p, sizeof(ip6p));
  157. return (hdr_len);
  158. }
  159. #endif
  160. static int
  161. tcp_signature_compute(struct mbuf *m, struct tcphdr *th,
  162. struct secasvar *sav, u_char *buf)
  163. {
  164. MD5_CTX ctx;
  165. int len;
  166. u_short csum;
  167. MD5Init(&ctx);
  168. /* Step 1: Update MD5 hash with IP(v6) pseudo-header. */
  169. switch (sav->sah->saidx.dst.sa.sa_family) {
  170. #ifdef INET
  171. case AF_INET:
  172. len = ip_pseudo_compute(m, &ctx);
  173. break;
  174. #endif
  175. #ifdef INET6
  176. case AF_INET6:
  177. len = ip6_pseudo_compute(m, &ctx);
  178. break;
  179. #endif
  180. default:
  181. return (EAFNOSUPPORT);
  182. }
  183. /*
  184. * Step 2: Update MD5 hash with TCP header, excluding options.
  185. * The TCP checksum must be set to zero.
  186. */
  187. csum = th->th_sum;
  188. th->th_sum = 0;
  189. MD5Update(&ctx, (char *)th, sizeof(struct tcphdr));
  190. th->th_sum = csum;
  191. /*
  192. * Step 3: Update MD5 hash with TCP segment data.
  193. * Use m_apply() to avoid an early m_pullup().
  194. */
  195. len += (th->th_off << 2);
  196. if (m->m_pkthdr.len - len > 0)
  197. m_apply(m, len, m->m_pkthdr.len - len,
  198. tcp_signature_apply, &ctx);
  199. /*
  200. * Step 4: Update MD5 hash with shared secret.
  201. */
  202. MD5Update(&ctx, sav->key_auth->key_data, _KEYLEN(sav->key_auth));
  203. MD5Final(buf, &ctx);
  204. key_sa_recordxfer(sav, m);
  205. return (0);
  206. }
  207. static void
  208. setsockaddrs(const struct mbuf *m, union sockaddr_union *src,
  209. union sockaddr_union *dst)
  210. {
  211. struct ip *ip;
  212. IPSEC_ASSERT(m->m_len >= sizeof(*ip), ("unexpected mbuf len"));
  213. ip = mtod(m, struct ip *);
  214. switch (ip->ip_v) {
  215. #ifdef INET
  216. case IPVERSION:
  217. ipsec4_setsockaddrs(m, src, dst);
  218. break;
  219. #endif
  220. #ifdef INET6
  221. case (IPV6_VERSION >> 4):
  222. ipsec6_setsockaddrs(m, src, dst);
  223. break;
  224. #endif
  225. default:
  226. bzero(src, sizeof(*src));
  227. bzero(dst, sizeof(*dst));
  228. }
  229. }
  230. /*
  231. * Compute TCP-MD5 hash of an *INBOUND* TCP segment.
  232. * Parameters:
  233. * m pointer to head of mbuf chain
  234. * th pointer to TCP header
  235. * buf pointer to storage for computed MD5 digest
  236. *
  237. * Return 0 if successful, otherwise return error code.
  238. */
  239. static int
  240. tcp_ipsec_input(struct mbuf *m, struct tcphdr *th, u_char *buf)
  241. {
  242. char tmpdigest[TCP_SIGLEN];
  243. struct secasindex saidx;
  244. struct secasvar *sav;
  245. setsockaddrs(m, &saidx.src, &saidx.dst);
  246. saidx.proto = IPPROTO_TCP;
  247. saidx.mode = IPSEC_MODE_TCPMD5;
  248. saidx.reqid = 0;
  249. sav = key_allocsa_tcpmd5(&saidx);
  250. if (sav == NULL) {
  251. KMOD_TCPSTAT_INC(tcps_sig_err_buildsig);
  252. return (ENOENT);
  253. }
  254. if (buf == NULL) {
  255. key_freesav(&sav);
  256. KMOD_TCPSTAT_INC(tcps_sig_err_nosigopt);
  257. return (EACCES);
  258. }
  259. /*
  260. * tcp_input() operates with TCP header fields in host
  261. * byte order. We expect them in network byte order.
  262. */
  263. tcp_fields_to_net(th);
  264. tcp_signature_compute(m, th, sav, tmpdigest);
  265. tcp_fields_to_host(th);
  266. key_freesav(&sav);
  267. if (bcmp(buf, tmpdigest, TCP_SIGLEN) != 0) {
  268. KMOD_TCPSTAT_INC(tcps_sig_rcvbadsig);
  269. return (EACCES);
  270. }
  271. KMOD_TCPSTAT_INC(tcps_sig_rcvgoodsig);
  272. return (0);
  273. }
  274. /*
  275. * Compute TCP-MD5 hash of an *OUTBOUND* TCP segment.
  276. * Parameters:
  277. * m pointer to head of mbuf chain
  278. * th pointer to TCP header
  279. * buf pointer to storage for computed MD5 digest
  280. *
  281. * Return 0 if successful, otherwise return error code.
  282. */
  283. static int
  284. tcp_ipsec_output(struct mbuf *m, struct tcphdr *th, u_char *buf)
  285. {
  286. struct secasindex saidx;
  287. struct secasvar *sav;
  288. setsockaddrs(m, &saidx.src, &saidx.dst);
  289. saidx.proto = IPPROTO_TCP;
  290. saidx.mode = IPSEC_MODE_TCPMD5;
  291. saidx.reqid = 0;
  292. sav = key_allocsa_tcpmd5(&saidx);
  293. if (sav == NULL) {
  294. KMOD_TCPSTAT_INC(tcps_sig_err_buildsig);
  295. return (ENOENT);
  296. }
  297. tcp_signature_compute(m, th, sav, buf);
  298. key_freesav(&sav);
  299. return (0);
  300. }
  301. /*
  302. * Initialize a TCP-MD5 SA. Called when the SA is being set up.
  303. *
  304. * We don't need to set up the tdb prefixed fields, as we don't use the
  305. * opencrypto code; we just perform a key length check.
  306. *
  307. * XXX: Currently we have used single 'magic' SPI and need to still
  308. * support this.
  309. *
  310. * This allows per-host granularity without affecting the userland
  311. * interface, which is a simple socket option toggle switch,
  312. * TCP_SIGNATURE_ENABLE.
  313. *
  314. * To allow per-service granularity requires that we have a means
  315. * of mapping port to SPI. The mandated way of doing this is to
  316. * use SPD entries to specify packet flows which get the TCP-MD5
  317. * treatment, however the code to do this is currently unstable
  318. * and unsuitable for production use.
  319. *
  320. * Therefore we use this compromise in the meantime.
  321. */
  322. static int
  323. tcpsignature_init(struct secasvar *sav, struct xformsw *xsp)
  324. {
  325. int keylen;
  326. if (sav->alg_auth != SADB_X_AALG_TCP_MD5) {
  327. DPRINTF(("%s: unsupported authentication algorithm %u\n",
  328. __func__, sav->alg_auth));
  329. return (EINVAL);
  330. }
  331. if (sav->key_auth == NULL) {
  332. DPRINTF(("%s: no authentication key present\n", __func__));
  333. return (EINVAL);
  334. }
  335. keylen = _KEYLEN(sav->key_auth);
  336. if ((keylen < TCP_KEYLEN_MIN) || (keylen > TCP_KEYLEN_MAX)) {
  337. DPRINTF(("%s: invalid key length %u\n", __func__, keylen));
  338. return (EINVAL);
  339. }
  340. sav->tdb_xform = xsp;
  341. return (0);
  342. }
  343. /*
  344. * Called when the SA is deleted.
  345. */
  346. static void
  347. tcpsignature_cleanup(struct secasvar *sav)
  348. {
  349. }
  350. static struct xformsw tcpsignature_xformsw = {
  351. .xf_type = XF_TCPSIGNATURE,
  352. .xf_name = "TCP-MD5",
  353. .xf_init = tcpsignature_init,
  354. .xf_cleanup = tcpsignature_cleanup,
  355. };
  356. static const struct tcpmd5_methods tcpmd5_methods = {
  357. .input = tcp_ipsec_input,
  358. .output = tcp_ipsec_output,
  359. .pcbctl = tcp_ipsec_pcbctl,
  360. };
  361. #ifndef KLD_MODULE
  362. /* TCP-MD5 support is build in the kernel */
  363. static const struct tcpmd5_support tcpmd5_ipsec = {
  364. .enabled = IPSEC_MODULE_ENABLED,
  365. .methods = &tcpmd5_methods
  366. };
  367. const struct tcpmd5_support * const tcp_ipsec_support = &tcpmd5_ipsec;
  368. #endif /* !KLD_MODULE */
  369. static int
  370. tcpmd5_modevent(module_t mod, int type, void *data)
  371. {
  372. switch (type) {
  373. case MOD_LOAD:
  374. xform_attach(&tcpsignature_xformsw);
  375. #ifdef KLD_MODULE
  376. tcpmd5_support_enable(&tcpmd5_methods);
  377. #endif
  378. break;
  379. case MOD_UNLOAD:
  380. #ifdef KLD_MODULE
  381. tcpmd5_support_disable();
  382. #endif
  383. xform_detach(&tcpsignature_xformsw);
  384. break;
  385. default:
  386. return (EOPNOTSUPP);
  387. }
  388. return (0);
  389. }
  390. static moduledata_t tcpmd5_mod = {
  391. "tcpmd5",
  392. tcpmd5_modevent,
  393. 0
  394. };
  395. DECLARE_MODULE(tcpmd5, tcpmd5_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY);
  396. MODULE_VERSION(tcpmd5, 1);
  397. #ifdef KLD_MODULE
  398. MODULE_DEPEND(tcpmd5, ipsec_support, 1, 1, 1);
  399. #endif