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