xref: /freebsd/sys/kern/subr_mchain.c (revision 1da7f3f6f72b2245e458fc7195733268ae4a1136)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2000, 2001 Boris Popov
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  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysctl.h>
35 #include <sys/endian.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/module.h>
40 #include <sys/uio.h>
41 
42 #include <sys/mchain.h>
43 
44 FEATURE(libmchain, "mchain library");
45 
46 MODULE_VERSION(libmchain, 1);
47 
48 #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
49 				    __LINE__ , ## __VA_ARGS__)
50 
51 #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
52 				    __LINE__ , ## __VA_ARGS__)
53 
54 /*
55  * Various helper functions
56  */
57 int
58 mb_init(struct mbchain *mbp)
59 {
60 	struct mbuf *m;
61 
62 	m = m_gethdr(M_WAITOK, MT_DATA);
63 	m->m_len = 0;
64 	mb_initm(mbp, m);
65 	return (0);
66 }
67 
68 void
69 mb_initm(struct mbchain *mbp, struct mbuf *m)
70 {
71 	bzero(mbp, sizeof(*mbp));
72 	mbp->mb_top = mbp->mb_cur = m;
73 	mbp->mb_mleft = M_TRAILINGSPACE(m);
74 }
75 
76 void
77 mb_done(struct mbchain *mbp)
78 {
79 	if (mbp->mb_top) {
80 		m_freem(mbp->mb_top);
81 		mbp->mb_top = NULL;
82 	}
83 }
84 
85 struct mbuf *
86 mb_detach(struct mbchain *mbp)
87 {
88 	struct mbuf *m;
89 
90 	m = mbp->mb_top;
91 	mbp->mb_top = NULL;
92 	return (m);
93 }
94 
95 int
96 mb_fixhdr(struct mbchain *mbp)
97 {
98 	return (mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top));
99 }
100 
101 /*
102  * Check if object of size 'size' fit to the current position and
103  * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
104  * Return pointer to the object placeholder or NULL if any error occurred.
105  * Note: size should be <= MLEN
106  */
107 caddr_t
108 mb_reserve(struct mbchain *mbp, int size)
109 {
110 	struct mbuf *m, *mn;
111 	caddr_t bpos;
112 
113 	if (size > MLEN)
114 		panic("mb_reserve: size = %d\n", size);
115 	m = mbp->mb_cur;
116 	if (mbp->mb_mleft < size) {
117 		mn = m_get(M_WAITOK, MT_DATA);
118 		mbp->mb_cur = m->m_next = mn;
119 		m = mn;
120 		m->m_len = 0;
121 		mbp->mb_mleft = M_TRAILINGSPACE(m);
122 	}
123 	mbp->mb_mleft -= size;
124 	mbp->mb_count += size;
125 	bpos = mtod(m, caddr_t) + m->m_len;
126 	m->m_len += size;
127 	return (bpos);
128 }
129 
130 int
131 mb_put_padbyte(struct mbchain *mbp)
132 {
133 	caddr_t dst;
134 	uint8_t x = 0;
135 
136 	dst = mtod(mbp->mb_cur, caddr_t) + mbp->mb_cur->m_len;
137 
138 	/* Only add padding if address is odd */
139 	if ((unsigned long)dst & 1)
140 		return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
141 	else
142 		return (0);
143 }
144 
145 int
146 mb_put_uint8(struct mbchain *mbp, uint8_t x)
147 {
148 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
149 }
150 
151 int
152 mb_put_uint16be(struct mbchain *mbp, uint16_t x)
153 {
154 	x = htobe16(x);
155 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
156 }
157 
158 int
159 mb_put_uint16le(struct mbchain *mbp, uint16_t x)
160 {
161 	x = htole16(x);
162 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
163 }
164 
165 int
166 mb_put_uint32be(struct mbchain *mbp, uint32_t x)
167 {
168 	x = htobe32(x);
169 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
170 }
171 
172 int
173 mb_put_uint32le(struct mbchain *mbp, uint32_t x)
174 {
175 	x = htole32(x);
176 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
177 }
178 
179 int
180 mb_put_int64be(struct mbchain *mbp, int64_t x)
181 {
182 	x = htobe64(x);
183 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
184 }
185 
186 int
187 mb_put_int64le(struct mbchain *mbp, int64_t x)
188 {
189 	x = htole64(x);
190 	return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
191 }
192 
193 int
194 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type)
195 {
196 	struct mbuf *m;
197 	caddr_t dst;
198 	c_caddr_t src;
199 	int cplen, error, mleft, count;
200 	size_t srclen, dstlen;
201 
202 	m = mbp->mb_cur;
203 	mleft = mbp->mb_mleft;
204 
205 	while (size > 0) {
206 		if (mleft == 0) {
207 			if (m->m_next == NULL)
208 				m = m_getm(m, size, M_WAITOK, MT_DATA);
209 			else
210 				m = m->m_next;
211 			mleft = M_TRAILINGSPACE(m);
212 			continue;
213 		}
214 		cplen = mleft > size ? size : mleft;
215 		srclen = dstlen = cplen;
216 		dst = mtod(m, caddr_t) + m->m_len;
217 		switch (type) {
218 		    case MB_MCUSTOM:
219 			srclen = size;
220 			dstlen = mleft;
221 			error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
222 			if (error)
223 				return (error);
224 			break;
225 		    case MB_MINLINE:
226 			for (src = source, count = cplen; count; count--)
227 				*dst++ = *src++;
228 			break;
229 		    case MB_MSYSTEM:
230 			bcopy(source, dst, cplen);
231 			break;
232 		    case MB_MUSER:
233 			error = copyin(source, dst, cplen);
234 			if (error)
235 				return (error);
236 			break;
237 		    case MB_MZERO:
238 			bzero(dst, cplen);
239 			break;
240 		}
241 		size -= srclen;
242 		source += srclen;
243 		m->m_len += dstlen;
244 		mleft -= dstlen;
245 		mbp->mb_count += dstlen;
246 	}
247 	mbp->mb_cur = m;
248 	mbp->mb_mleft = mleft;
249 	return (0);
250 }
251 
252 int
253 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m)
254 {
255 	mbp->mb_cur->m_next = m;
256 	while (m) {
257 		mbp->mb_count += m->m_len;
258 		if (m->m_next == NULL)
259 			break;
260 		m = m->m_next;
261 	}
262 	mbp->mb_mleft = M_TRAILINGSPACE(m);
263 	mbp->mb_cur = m;
264 	return (0);
265 }
266 
267 /*
268  * copies a uio scatter/gather list to an mbuf chain.
269  */
270 int
271 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size)
272 {
273 	long left;
274 	int mtype, error;
275 
276 	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
277 
278 	while (size > 0 && uiop->uio_resid) {
279 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
280 			return (EFBIG);
281 		left = uiop->uio_iov->iov_len;
282 		if (left == 0) {
283 			uiop->uio_iov++;
284 			uiop->uio_iovcnt--;
285 			continue;
286 		}
287 		if (left > size)
288 			left = size;
289 		error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype);
290 		if (error)
291 			return (error);
292 		uiop->uio_offset += left;
293 		uiop->uio_resid -= left;
294 		uiop->uio_iov->iov_base =
295 		    (char *)uiop->uio_iov->iov_base + left;
296 		uiop->uio_iov->iov_len -= left;
297 		size -= left;
298 	}
299 	return (0);
300 }
301 
302 /*
303  * Routines for fetching data from an mbuf chain
304  */
305 int
306 md_init(struct mdchain *mdp)
307 {
308 	struct mbuf *m;
309 
310 	m = m_gethdr(M_WAITOK, MT_DATA);
311 	m->m_len = 0;
312 	md_initm(mdp, m);
313 	return (0);
314 }
315 
316 void
317 md_initm(struct mdchain *mdp, struct mbuf *m)
318 {
319 	bzero(mdp, sizeof(*mdp));
320 	mdp->md_top = mdp->md_cur = m;
321 	mdp->md_pos = mtod(m, u_char*);
322 }
323 
324 void
325 md_done(struct mdchain *mdp)
326 {
327 	if (mdp->md_top) {
328 		m_freem(mdp->md_top);
329 		mdp->md_top = NULL;
330 	}
331 }
332 
333 /*
334  * Append a separate mbuf chain. It is caller responsibility to prevent
335  * multiple calls to fetch/record routines.
336  */
337 void
338 md_append_record(struct mdchain *mdp, struct mbuf *top)
339 {
340 	struct mbuf *m;
341 
342 	if (mdp->md_top == NULL) {
343 		md_initm(mdp, top);
344 		return;
345 	}
346 	m = mdp->md_top;
347 	while (m->m_nextpkt)
348 		m = m->m_nextpkt;
349 	m->m_nextpkt = top;
350 	top->m_nextpkt = NULL;
351 	return;
352 }
353 
354 /*
355  * Put next record in place of existing
356  */
357 int
358 md_next_record(struct mdchain *mdp)
359 {
360 	struct mbuf *m;
361 
362 	if (mdp->md_top == NULL)
363 		return (ENOENT);
364 	m = mdp->md_top->m_nextpkt;
365 	md_done(mdp);
366 	if (m == NULL)
367 		return (ENOENT);
368 	md_initm(mdp, m);
369 	return (0);
370 }
371 
372 int
373 md_get_uint8(struct mdchain *mdp, uint8_t *x)
374 {
375 	return (md_get_mem(mdp, x, 1, MB_MINLINE));
376 }
377 
378 int
379 md_get_uint16(struct mdchain *mdp, uint16_t *x)
380 {
381 	return (md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE));
382 }
383 
384 int
385 md_get_uint16le(struct mdchain *mdp, uint16_t *x)
386 {
387 	uint16_t v;
388 	int error = md_get_uint16(mdp, &v);
389 
390 	if (x != NULL)
391 		*x = le16toh(v);
392 	return (error);
393 }
394 
395 int
396 md_get_uint16be(struct mdchain *mdp, uint16_t *x)
397 {
398 	uint16_t v;
399 	int error = md_get_uint16(mdp, &v);
400 
401 	if (x != NULL)
402 		*x = be16toh(v);
403 	return (error);
404 }
405 
406 int
407 md_get_uint32(struct mdchain *mdp, uint32_t *x)
408 {
409 	return (md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE));
410 }
411 
412 int
413 md_get_uint32be(struct mdchain *mdp, uint32_t *x)
414 {
415 	uint32_t v;
416 	int error;
417 
418 	error = md_get_uint32(mdp, &v);
419 	if (x != NULL)
420 		*x = be32toh(v);
421 	return (error);
422 }
423 
424 int
425 md_get_uint32le(struct mdchain *mdp, uint32_t *x)
426 {
427 	uint32_t v;
428 	int error;
429 
430 	error = md_get_uint32(mdp, &v);
431 	if (x != NULL)
432 		*x = le32toh(v);
433 	return (error);
434 }
435 
436 int
437 md_get_int64(struct mdchain *mdp, int64_t *x)
438 {
439 	return (md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE));
440 }
441 
442 int
443 md_get_int64be(struct mdchain *mdp, int64_t *x)
444 {
445 	int64_t v;
446 	int error;
447 
448 	error = md_get_int64(mdp, &v);
449 	if (x != NULL)
450 		*x = be64toh(v);
451 	return (error);
452 }
453 
454 int
455 md_get_int64le(struct mdchain *mdp, int64_t *x)
456 {
457 	int64_t v;
458 	int error;
459 
460 	error = md_get_int64(mdp, &v);
461 	if (x != NULL)
462 		*x = le64toh(v);
463 	return (error);
464 }
465 
466 int
467 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type)
468 {
469 	struct mbuf *m = mdp->md_cur;
470 	int error;
471 	u_int count;
472 	u_char *s;
473 
474 	while (size > 0) {
475 		if (m == NULL) {
476 			MBERROR("incomplete copy\n");
477 			return (EBADRPC);
478 		}
479 		s = mdp->md_pos;
480 		count = mtod(m, u_char*) + m->m_len - s;
481 		if (count == 0) {
482 			mdp->md_cur = m = m->m_next;
483 			if (m)
484 				s = mdp->md_pos = mtod(m, caddr_t);
485 			continue;
486 		}
487 		if (count > size)
488 			count = size;
489 		size -= count;
490 		mdp->md_pos += count;
491 		if (target == NULL)
492 			continue;
493 		switch (type) {
494 		    case MB_MUSER:
495 			error = copyout(s, target, count);
496 			if (error)
497 				return error;
498 			break;
499 		    case MB_MSYSTEM:
500 			bcopy(s, target, count);
501 			break;
502 		    case MB_MINLINE:
503 			while (count--)
504 				*target++ = *s++;
505 			continue;
506 		}
507 		target += count;
508 	}
509 	return (0);
510 }
511 
512 int
513 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret)
514 {
515 	struct mbuf *m = mdp->md_cur, *rm;
516 
517 	rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAITOK);
518 	md_get_mem(mdp, NULL, size, MB_MZERO);
519 	*ret = rm;
520 	return (0);
521 }
522 
523 int
524 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size)
525 {
526 	char *uiocp;
527 	long left;
528 	int mtype, error;
529 
530 	mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
531 	while (size > 0 && uiop->uio_resid) {
532 		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
533 			return (EFBIG);
534 		left = uiop->uio_iov->iov_len;
535 		if (left == 0) {
536 			uiop->uio_iov++;
537 			uiop->uio_iovcnt--;
538 			continue;
539 		}
540 		uiocp = uiop->uio_iov->iov_base;
541 		if (left > size)
542 			left = size;
543 		error = md_get_mem(mdp, uiocp, left, mtype);
544 		if (error)
545 			return (error);
546 		uiop->uio_offset += left;
547 		uiop->uio_resid -= left;
548 		uiop->uio_iov->iov_base =
549 		    (char *)uiop->uio_iov->iov_base + left;
550 		uiop->uio_iov->iov_len -= left;
551 		size -= left;
552 	}
553 	return (0);
554 }
555