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