ipsec_mbuf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
  5. * All rights reserved.
  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. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. /*
  31. * IPsec-specific mbuf routines.
  32. */
  33. #include "opt_ipsec.h"
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/malloc.h>
  37. #include <sys/mbuf.h>
  38. #include <sys/socket.h>
  39. #include <net/vnet.h>
  40. #include <netinet/in.h>
  41. #include <netipsec/ipsec.h>
  42. /*
  43. * Make space for a new header of length hlen at skip bytes
  44. * into the packet. When doing this we allocate new mbufs only
  45. * when absolutely necessary. The mbuf where the new header
  46. * is to go is returned together with an offset into the mbuf.
  47. * If NULL is returned then the mbuf chain may have been modified;
  48. * the caller is assumed to always free the chain.
  49. */
  50. struct mbuf *
  51. m_makespace(struct mbuf *m0, int skip, int hlen, int *off)
  52. {
  53. struct mbuf *m;
  54. unsigned remain;
  55. IPSEC_ASSERT(m0 != NULL, ("null mbuf"));
  56. IPSEC_ASSERT(hlen < MHLEN, ("hlen too big: %u", hlen));
  57. for (m = m0; m && skip > m->m_len; m = m->m_next)
  58. skip -= m->m_len;
  59. if (m == NULL)
  60. return (NULL);
  61. /*
  62. * At this point skip is the offset into the mbuf m
  63. * where the new header should be placed. Figure out
  64. * if there's space to insert the new header. If so,
  65. * and copying the remainder makes sense then do so.
  66. * Otherwise insert a new mbuf in the chain, splitting
  67. * the contents of m as needed.
  68. */
  69. remain = m->m_len - skip; /* data to move */
  70. if (remain > skip &&
  71. hlen + max_linkhdr < M_LEADINGSPACE(m)) {
  72. /*
  73. * mbuf has enough free space at the beginning.
  74. * XXX: which operation is the most heavy - copying of
  75. * possible several hundred of bytes or allocation
  76. * of new mbuf? We can remove max_linkhdr check
  77. * here, but it is possible that this will lead
  78. * to allocation of new mbuf in Layer 2 code.
  79. */
  80. m->m_data -= hlen;
  81. bcopy(mtodo(m, hlen), mtod(m, caddr_t), skip);
  82. m->m_len += hlen;
  83. *off = skip;
  84. } else if (hlen > M_TRAILINGSPACE(m)) {
  85. struct mbuf *n0, *n, **np;
  86. int todo, len, done, alloc;
  87. n0 = NULL;
  88. np = &n0;
  89. alloc = 0;
  90. done = 0;
  91. todo = remain;
  92. while (todo > 0) {
  93. if (todo > MHLEN) {
  94. n = m_getcl(M_NOWAIT, m->m_type, 0);
  95. len = MCLBYTES;
  96. }
  97. else {
  98. n = m_get(M_NOWAIT, m->m_type);
  99. len = MHLEN;
  100. }
  101. if (n == NULL) {
  102. m_freem(n0);
  103. return NULL;
  104. }
  105. *np = n;
  106. np = &n->m_next;
  107. alloc++;
  108. len = min(todo, len);
  109. memcpy(n->m_data, mtod(m, char *) + skip + done, len);
  110. n->m_len = len;
  111. done += len;
  112. todo -= len;
  113. }
  114. if (hlen <= M_TRAILINGSPACE(m) + remain) {
  115. m->m_len = skip + hlen;
  116. *off = skip;
  117. if (n0 != NULL) {
  118. *np = m->m_next;
  119. m->m_next = n0;
  120. }
  121. }
  122. else {
  123. n = m_get(M_NOWAIT, m->m_type);
  124. if (n == NULL) {
  125. m_freem(n0);
  126. return NULL;
  127. }
  128. alloc++;
  129. if ((n->m_next = n0) == NULL)
  130. np = &n->m_next;
  131. n0 = n;
  132. *np = m->m_next;
  133. m->m_next = n0;
  134. n->m_len = hlen;
  135. m->m_len = skip;
  136. m = n; /* header is at front ... */
  137. *off = 0; /* ... of new mbuf */
  138. }
  139. IPSECSTAT_INC(ips_mbinserted);
  140. } else {
  141. /*
  142. * Copy the remainder to the back of the mbuf
  143. * so there's space to write the new header.
  144. */
  145. bcopy(mtod(m, caddr_t) + skip,
  146. mtod(m, caddr_t) + skip + hlen, remain);
  147. m->m_len += hlen;
  148. *off = skip;
  149. }
  150. m0->m_pkthdr.len += hlen; /* adjust packet length */
  151. return m;
  152. }
  153. /*
  154. * m_pad(m, n) pads <m> with <n> bytes at the end. The packet header
  155. * length is updated, and a pointer to the first byte of the padding
  156. * (which is guaranteed to be all in one mbuf) is returned.
  157. */
  158. caddr_t
  159. m_pad(struct mbuf *m, int n)
  160. {
  161. struct mbuf *m0, *m1;
  162. int len, pad;
  163. caddr_t retval;
  164. if (n <= 0) { /* No stupid arguments. */
  165. DPRINTF(("%s: pad length invalid (%d)\n", __func__, n));
  166. m_freem(m);
  167. return NULL;
  168. }
  169. len = m->m_pkthdr.len;
  170. pad = n;
  171. m0 = m;
  172. while (m0->m_len < len) {
  173. len -= m0->m_len;
  174. m0 = m0->m_next;
  175. }
  176. if (m0->m_len != len) {
  177. DPRINTF(("%s: length mismatch (should be %d instead of %d)\n",
  178. __func__, m->m_pkthdr.len,
  179. m->m_pkthdr.len + m0->m_len - len));
  180. m_freem(m);
  181. return NULL;
  182. }
  183. /* Check for zero-length trailing mbufs, and find the last one. */
  184. for (m1 = m0; m1->m_next; m1 = m1->m_next) {
  185. if (m1->m_next->m_len != 0) {
  186. DPRINTF(("%s: length mismatch (should be %d instead "
  187. "of %d)\n", __func__,
  188. m->m_pkthdr.len,
  189. m->m_pkthdr.len + m1->m_next->m_len));
  190. m_freem(m);
  191. return NULL;
  192. }
  193. m0 = m1->m_next;
  194. }
  195. if (pad > M_TRAILINGSPACE(m0)) {
  196. /* Add an mbuf to the chain. */
  197. MGET(m1, M_NOWAIT, MT_DATA);
  198. if (m1 == NULL) {
  199. m_freem(m0);
  200. DPRINTF(("%s: unable to get extra mbuf\n", __func__));
  201. return NULL;
  202. }
  203. m0->m_next = m1;
  204. m0 = m1;
  205. m0->m_len = 0;
  206. }
  207. retval = m0->m_data + m0->m_len;
  208. m0->m_len += pad;
  209. m->m_pkthdr.len += pad;
  210. return retval;
  211. }
  212. /*
  213. * Remove hlen data at offset skip in the packet. This is used by
  214. * the protocols strip protocol headers and associated data (e.g. IV,
  215. * authenticator) on input.
  216. */
  217. int
  218. m_striphdr(struct mbuf *m, int skip, int hlen)
  219. {
  220. struct mbuf *m1;
  221. int roff;
  222. /* Find beginning of header */
  223. m1 = m_getptr(m, skip, &roff);
  224. if (m1 == NULL)
  225. return (EINVAL);
  226. /* Remove the header and associated data from the mbuf. */
  227. if (roff == 0) {
  228. /* The header was at the beginning of the mbuf */
  229. IPSECSTAT_INC(ips_input_front);
  230. m_adj(m1, hlen);
  231. if (m1 != m)
  232. m->m_pkthdr.len -= hlen;
  233. } else if (roff + hlen >= m1->m_len) {
  234. struct mbuf *mo;
  235. int adjlen;
  236. /*
  237. * Part or all of the header is at the end of this mbuf,
  238. * so first let's remove the remainder of the header from
  239. * the beginning of the remainder of the mbuf chain, if any.
  240. */
  241. IPSECSTAT_INC(ips_input_end);
  242. if (roff + hlen > m1->m_len) {
  243. adjlen = roff + hlen - m1->m_len;
  244. /* Adjust the next mbuf by the remainder */
  245. m_adj(m1->m_next, adjlen);
  246. /* The second mbuf is guaranteed not to have a pkthdr... */
  247. m->m_pkthdr.len -= adjlen;
  248. }
  249. /* Now, let's unlink the mbuf chain for a second...*/
  250. mo = m1->m_next;
  251. m1->m_next = NULL;
  252. /* ...and trim the end of the first part of the chain...sick */
  253. adjlen = m1->m_len - roff;
  254. m_adj(m1, -adjlen);
  255. if (m1 != m)
  256. m->m_pkthdr.len -= adjlen;
  257. /* Finally, let's relink */
  258. m1->m_next = mo;
  259. } else {
  260. /*
  261. * The header lies in the "middle" of the mbuf; copy
  262. * the remainder of the mbuf down over the header.
  263. */
  264. IPSECSTAT_INC(ips_input_middle);
  265. bcopy(mtod(m1, u_char *) + roff + hlen,
  266. mtod(m1, u_char *) + roff,
  267. m1->m_len - (roff + hlen));
  268. m1->m_len -= hlen;
  269. m->m_pkthdr.len -= hlen;
  270. }
  271. return (0);
  272. }
  273. /*
  274. * Diagnostic routine to check mbuf alignment as required by the
  275. * crypto device drivers (that use DMA).
  276. */
  277. void
  278. m_checkalignment(const char* where, struct mbuf *m0, int off, int len)
  279. {
  280. int roff;
  281. struct mbuf *m = m_getptr(m0, off, &roff);
  282. caddr_t addr;
  283. if (m == NULL)
  284. return;
  285. printf("%s (off %u len %u): ", where, off, len);
  286. addr = mtod(m, caddr_t) + roff;
  287. do {
  288. int mlen;
  289. if (((uintptr_t) addr) & 3) {
  290. printf("addr misaligned %p,", addr);
  291. break;
  292. }
  293. mlen = m->m_len;
  294. if (mlen > len)
  295. mlen = len;
  296. len -= mlen;
  297. if (len && (mlen & 3)) {
  298. printf("len mismatch %u,", mlen);
  299. break;
  300. }
  301. m = m->m_next;
  302. addr = m ? mtod(m, caddr_t) : NULL;
  303. } while (m && len > 0);
  304. for (m = m0; m; m = m->m_next)
  305. printf(" [%p:%u]", mtod(m, caddr_t), m->m_len);
  306. printf("\n");
  307. }