• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  *  __Userspace__ version of /usr/src/sys/kern/kern_mbuf.c
34  *  We are initializing two zones for Mbufs and Clusters.
35  *
36  */
37 
38 #include <stdio.h>
39 #include <string.h>
40 /* #include <sys/param.h> This defines MSIZE 256 */
41 #if !defined(SCTP_SIMPLE_ALLOCATOR)
42 #include "umem.h"
43 #endif
44 #include "user_mbuf.h"
45 #include "user_environment.h"
46 #include "user_atomic.h"
47 #include "netinet/sctp_pcb.h"
48 
49 #define KIPC_MAX_LINKHDR        4       /* int: max length of link header (see sys/sysclt.h) */
50 #define KIPC_MAX_PROTOHDR	5	/* int: max length of network header (see sys/sysclt.h)*/
51 int max_linkhdr = KIPC_MAX_LINKHDR;
52 int max_protohdr = KIPC_MAX_PROTOHDR; /* Size of largest protocol layer header. */
53 
54 /*
55  * Zones from which we allocate.
56  */
57 sctp_zone_t	zone_mbuf;
58 sctp_zone_t	zone_clust;
59 sctp_zone_t	zone_ext_refcnt;
60 
61 /* __Userspace__ clust_mb_args will be passed as callback data to mb_ctor_clust
62  * and mb_dtor_clust.
63  * Note: I had to use struct clust_args as an encapsulation for an mbuf pointer.
64  * struct mbuf * clust_mb_args; does not work.
65  */
66 struct clust_args clust_mb_args;
67 
68 
69 /* __Userspace__
70  * Local prototypes.
71  */
72 static int	mb_ctor_mbuf(void *, void *, int);
73 static int      mb_ctor_clust(void *, void *, int);
74 static void	mb_dtor_mbuf(void *,  void *);
75 static void	mb_dtor_clust(void *, void *);
76 
77 
78 /***************** Functions taken from user_mbuf.h *************/
79 
mbuf_constructor_dup(struct mbuf * m,int pkthdr,short type)80 static int mbuf_constructor_dup(struct mbuf *m, int pkthdr, short type)
81 {
82 	int flags = pkthdr;
83 	if (type == MT_NOINIT)
84 		return (0);
85 
86 	m->m_next = NULL;
87 	m->m_nextpkt = NULL;
88 	m->m_len = 0;
89 	m->m_flags = flags;
90 	m->m_type = type;
91 	if (flags & M_PKTHDR) {
92 		m->m_data = m->m_pktdat;
93 		m->m_pkthdr.rcvif = NULL;
94 		m->m_pkthdr.len = 0;
95 		m->m_pkthdr.header = NULL;
96 		m->m_pkthdr.csum_flags = 0;
97 		m->m_pkthdr.csum_data = 0;
98 		m->m_pkthdr.tso_segsz = 0;
99 		m->m_pkthdr.ether_vtag = 0;
100 		SLIST_INIT(&m->m_pkthdr.tags);
101 	} else
102 		m->m_data = m->m_dat;
103 
104 	return (0);
105 }
106 
107 /* __Userspace__ */
108 struct mbuf *
m_get(int how,short type)109 m_get(int how, short type)
110 {
111 	struct mbuf *mret;
112 #if defined(SCTP_SIMPLE_ALLOCATOR)
113 	struct mb_args mbuf_mb_args;
114 
115 	/* The following setter function is not yet being enclosed within
116 	 * #if USING_MBUF_CONSTRUCTOR - #endif, until I have thoroughly tested
117 	 * mb_dtor_mbuf. See comment there
118 	 */
119 	mbuf_mb_args.flags = 0;
120 	mbuf_mb_args.type = type;
121 #endif
122 	/* Mbuf master zone, zone_mbuf, has already been
123 	 * created in mbuf_initialize() */
124 	mret = SCTP_ZONE_GET(zone_mbuf, struct mbuf);
125 #if defined(SCTP_SIMPLE_ALLOCATOR)
126 	mb_ctor_mbuf(mret, &mbuf_mb_args, 0);
127 #endif
128 	/*mret =  ((struct mbuf *)umem_cache_alloc(zone_mbuf, UMEM_DEFAULT));*/
129 
130 	/* There are cases when an object available in the current CPU's
131 	 * loaded magazine and in those cases the object's constructor is not applied.
132 	 * If that is the case, then we are duplicating constructor initialization here,
133 	 * so that the mbuf is properly constructed before returning it.
134 	 */
135 	if (mret) {
136 #if USING_MBUF_CONSTRUCTOR
137 		if (! (mret->m_type == type) ) {
138 			mbuf_constructor_dup(mret, 0, type);
139 		}
140 #else
141 		mbuf_constructor_dup(mret, 0, type);
142 #endif
143 
144 	}
145 	return mret;
146 }
147 
148 
149 /* __Userspace__ */
150 struct mbuf *
m_gethdr(int how,short type)151 m_gethdr(int how, short type)
152 {
153 	struct mbuf *mret;
154 #if defined(SCTP_SIMPLE_ALLOCATOR)
155 	struct mb_args mbuf_mb_args;
156 
157 	/* The following setter function is not yet being enclosed within
158 	 * #if USING_MBUF_CONSTRUCTOR - #endif, until I have thoroughly tested
159 	 * mb_dtor_mbuf. See comment there
160 	 */
161 	mbuf_mb_args.flags = M_PKTHDR;
162 	mbuf_mb_args.type = type;
163 #endif
164 	mret = SCTP_ZONE_GET(zone_mbuf, struct mbuf);
165 #if defined(SCTP_SIMPLE_ALLOCATOR)
166 	mb_ctor_mbuf(mret, &mbuf_mb_args, 0);
167 #endif
168 	/*mret = ((struct mbuf *)umem_cache_alloc(zone_mbuf, UMEM_DEFAULT));*/
169 	/* There are cases when an object available in the current CPU's
170 	 * loaded magazine and in those cases the object's constructor is not applied.
171 	 * If that is the case, then we are duplicating constructor initialization here,
172 	 * so that the mbuf is properly constructed before returning it.
173 	 */
174 	if (mret) {
175 #if USING_MBUF_CONSTRUCTOR
176 		if (! ((mret->m_flags & M_PKTHDR) && (mret->m_type == type)) ) {
177 			mbuf_constructor_dup(mret, M_PKTHDR, type);
178 		}
179 #else
180 		mbuf_constructor_dup(mret, M_PKTHDR, type);
181 #endif
182 	}
183 	return mret;
184 }
185 
186 /* __Userspace__ */
187 struct mbuf *
m_free(struct mbuf * m)188 m_free(struct mbuf *m)
189 {
190 
191 	struct mbuf *n = m->m_next;
192 
193 	if (m->m_flags & M_EXT)
194 		mb_free_ext(m);
195 	else if ((m->m_flags & M_NOFREE) == 0) {
196 #if defined(SCTP_SIMPLE_ALLOCATOR)
197 		mb_dtor_mbuf(m, NULL);
198 #endif
199 		SCTP_ZONE_FREE(zone_mbuf, m);
200 	}
201 		/*umem_cache_free(zone_mbuf, m);*/
202 	return (n);
203 }
204 
205 
206 static void
clust_constructor_dup(caddr_t m_clust,struct mbuf * m)207 clust_constructor_dup(caddr_t m_clust, struct mbuf* m)
208 {
209 	u_int *refcnt;
210 	int type, size;
211 
212 	if (m == NULL) {
213 		return;
214 	}
215 	/* Assigning cluster of MCLBYTES. TODO: Add jumbo frame functionality */
216 	type = EXT_CLUSTER;
217 	size = MCLBYTES;
218 
219 	refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int);
220 	/*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/
221 #if !defined(SCTP_SIMPLE_ALLOCATOR)
222 	if (refcnt == NULL) {
223 		umem_reap();
224 		refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int);
225 		/*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/
226 	}
227 #endif
228 	*refcnt = 1;
229 	m->m_ext.ext_buf = (caddr_t)m_clust;
230 	m->m_data = m->m_ext.ext_buf;
231 	m->m_flags |= M_EXT;
232 	m->m_ext.ext_free = NULL;
233 	m->m_ext.ext_args = NULL;
234 	m->m_ext.ext_size = size;
235 	m->m_ext.ext_type = type;
236 	m->m_ext.ref_cnt = refcnt;
237 	return;
238 }
239 
240 
241 /* __Userspace__ */
242 void
m_clget(struct mbuf * m,int how)243 m_clget(struct mbuf *m, int how)
244 {
245 	caddr_t mclust_ret;
246 #if defined(SCTP_SIMPLE_ALLOCATOR)
247 	struct clust_args clust_mb_args_l;
248 #endif
249 	if (m->m_flags & M_EXT) {
250 		SCTPDBG(SCTP_DEBUG_USR, "%s: %p mbuf already has cluster\n", __func__, (void *)m);
251 	}
252 	m->m_ext.ext_buf = (char *)NULL;
253 #if defined(SCTP_SIMPLE_ALLOCATOR)
254 	clust_mb_args_l.parent_mbuf = m;
255 #endif
256 	mclust_ret = SCTP_ZONE_GET(zone_clust, char);
257 #if defined(SCTP_SIMPLE_ALLOCATOR)
258 	mb_ctor_clust(mclust_ret, &clust_mb_args_l, 0);
259 #endif
260 	/*mclust_ret = umem_cache_alloc(zone_clust, UMEM_DEFAULT);*/
261 	/*
262 	 On a cluster allocation failure, call umem_reap() and retry.
263 	 */
264 
265 	if (mclust_ret == NULL) {
266 #if !defined(SCTP_SIMPLE_ALLOCATOR)
267 	/*	mclust_ret = SCTP_ZONE_GET(zone_clust, char);
268 		mb_ctor_clust(mclust_ret, &clust_mb_args, 0);
269 #else*/
270 		umem_reap();
271 		mclust_ret = SCTP_ZONE_GET(zone_clust, char);
272 #endif
273 		/*mclust_ret = umem_cache_alloc(zone_clust, UMEM_DEFAULT);*/
274 		/* if (NULL == mclust_ret) { */
275 		SCTPDBG(SCTP_DEBUG_USR, "Memory allocation failure in %s\n", __func__);
276 		/* } */
277 	}
278 
279 #if USING_MBUF_CONSTRUCTOR
280 	if ((m->m_ext.ext_buf == NULL)) {
281 		clust_constructor_dup(mclust_ret, m);
282 	}
283 #else
284 	clust_constructor_dup(mclust_ret, m);
285 #endif
286 }
287 
288 struct mbuf *
m_getm2(struct mbuf * m,int len,int how,short type,int flags,int allonebuf)289 m_getm2(struct mbuf *m, int len, int how, short type, int flags, int allonebuf)
290 {
291 	struct mbuf *mb, *nm = NULL, *mtail = NULL;
292 	int size, mbuf_threshold, space_needed = len;
293 
294 	KASSERT(len >= 0, ("%s: len is < 0", __func__));
295 
296 	/* Validate flags. */
297 	flags &= (M_PKTHDR | M_EOR);
298 
299 	/* Packet header mbuf must be first in chain. */
300 	if ((flags & M_PKTHDR) && m != NULL) {
301 		flags &= ~M_PKTHDR;
302 	}
303 
304 	if (allonebuf == 0)
305 		mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
306 	else
307 		mbuf_threshold = 1;
308 
309 	/* Loop and append maximum sized mbufs to the chain tail. */
310 	while (len > 0) {
311 		if ((!allonebuf && len >= MCLBYTES) || (len > (int)(((mbuf_threshold - 1) * MLEN) + MHLEN))) {
312 			mb = m_gethdr(how, type);
313 			MCLGET(mb, how);
314 			size = MCLBYTES;
315 			/* SCTP_BUF_LEN(mb) = MCLBYTES; */
316 		} else if (flags & M_PKTHDR) {
317 			mb = m_gethdr(how, type);
318 			if (len < MHLEN) {
319 				size = len;
320 			} else {
321 				size = MHLEN;
322 			}
323 		} else {
324 			mb = m_get(how, type);
325 			if (len < MLEN) {
326 				size = len;
327 			} else {
328 				size = MLEN;
329 			}
330 		}
331 
332 		/* Fail the whole operation if one mbuf can't be allocated. */
333 		if (mb == NULL) {
334 			if (nm != NULL)
335 				m_freem(nm);
336 			return (NULL);
337 		}
338 
339 		if (allonebuf != 0 && size < space_needed) {
340 			m_freem(mb);
341 			return (NULL);
342 		}
343 
344 		/* Book keeping. */
345 		len -= size;
346 		if (mtail != NULL)
347 			mtail->m_next = mb;
348 		else
349 			nm = mb;
350 		mtail = mb;
351 		flags &= ~M_PKTHDR;     /* Only valid on the first mbuf. */
352 	}
353 	if (flags & M_EOR) {
354 		mtail->m_flags |= M_EOR;  /* Only valid on the last mbuf. */
355 	}
356 
357 	/* If mbuf was supplied, append new chain to the end of it. */
358 	if (m != NULL) {
359 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
360 		mtail->m_next = nm;
361 		mtail->m_flags &= ~M_EOR;
362 	} else {
363 		m = nm;
364 	}
365 
366 	return (m);
367 }
368 
369 /*
370  * Copy the contents of uio into a properly sized mbuf chain.
371  */
372 struct mbuf *
m_uiotombuf(struct uio * uio,int how,int len,int align,int flags)373 m_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
374 {
375 	struct mbuf *m, *mb;
376 	int error, length;
377 	ssize_t total;
378 	int progress = 0;
379 
380 	/*
381 	 * len can be zero or an arbitrary large value bound by
382 	 * the total data supplied by the uio.
383 	 */
384 	if (len > 0)
385 		total = min(uio->uio_resid, len);
386 	else
387 		total = uio->uio_resid;
388 	/*
389 	 * The smallest unit returned by m_getm2() is a single mbuf
390 	 * with pkthdr.  We can't align past it.
391 	 */
392 	if (align >= MHLEN)
393 		return (NULL);
394 	/*
395 	 * Give us the full allocation or nothing.
396 	 * If len is zero return the smallest empty mbuf.
397 	 */
398 	m = m_getm2(NULL, (int)max(total + align, 1), how, MT_DATA, flags, 0);
399 	if (m == NULL)
400 		return (NULL);
401 	m->m_data += align;
402 
403 	/* Fill all mbufs with uio data and update header information. */
404 	for (mb = m; mb != NULL; mb = mb->m_next) {
405 		length = (int)min(M_TRAILINGSPACE(mb), total - progress);
406 		error = uiomove(mtod(mb, void *), length, uio);
407 		if (error) {
408 			m_freem(m);
409 			return (NULL);
410 		}
411 
412 		mb->m_len = length;
413 		progress += length;
414 		if (flags & M_PKTHDR)
415 			m->m_pkthdr.len += length;
416 	}
417 	KASSERT(progress == total, ("%s: progress != total", __func__));
418 
419 	return (m);
420 }
421 
422 u_int
m_length(struct mbuf * m0,struct mbuf ** last)423 m_length(struct mbuf *m0, struct mbuf **last)
424 {
425 	struct mbuf *m;
426 	u_int len;
427 
428 	len = 0;
429 	for (m = m0; m != NULL; m = m->m_next) {
430 		len += m->m_len;
431 		if (m->m_next == NULL)
432 			break;
433 	}
434 	if (last != NULL)
435 	*last = m;
436 	return (len);
437 }
438 
439 struct mbuf *
m_last(struct mbuf * m)440 m_last(struct mbuf *m)
441 {
442 	while (m->m_next) {
443 		m = m->m_next;
444 	}
445 	return (m);
446 }
447 
448 /*
449  * Unlink a tag from the list of tags associated with an mbuf.
450  */
451 static __inline void
m_tag_unlink(struct mbuf * m,struct m_tag * t)452 m_tag_unlink(struct mbuf *m, struct m_tag *t)
453 {
454 
455 	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
456 }
457 
458 /*
459  * Reclaim resources associated with a tag.
460  */
461 static __inline void
m_tag_free(struct m_tag * t)462 m_tag_free(struct m_tag *t)
463 {
464 
465 	(*t->m_tag_free)(t);
466 }
467 
468 /*
469  * Set up the contents of a tag.  Note that this does not fill in the free
470  * method; the caller is expected to do that.
471  *
472  * XXX probably should be called m_tag_init, but that was already taken.
473  */
474 static __inline void
m_tag_setup(struct m_tag * t,uint32_t cookie,int type,int len)475 m_tag_setup(struct m_tag *t, uint32_t cookie, int type, int len)
476 {
477 
478 	t->m_tag_id = type;
479 	t->m_tag_len = len;
480 	t->m_tag_cookie = cookie;
481 }
482 
483 /************ End functions from user_mbuf.h  ******************/
484 
485 
486 
487 /************ End functions to substitute umem_cache_alloc and umem_cache_free **************/
488 
489 void
mbuf_initialize(void * dummy)490 mbuf_initialize(void *dummy)
491 {
492 
493 	/*
494 	 * __Userspace__Configure UMA zones for Mbufs and Clusters.
495 	 * (TODO: m_getcl() - using packet secondary zone).
496 	 * There is no provision for trash_init and trash_fini in umem.
497 	 *
498 	 */
499  /* zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0,
500 				mb_ctor_mbuf, mb_dtor_mbuf, NULL,
501 				&mbuf_mb_args,
502 				NULL, 0);
503 	zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0, NULL, NULL, NULL, NULL, NULL, 0);*/
504 #if defined(SCTP_SIMPLE_ALLOCATOR)
505 	SCTP_ZONE_INIT(zone_mbuf, MBUF_MEM_NAME, MSIZE, 0);
506 #else
507 	zone_mbuf = umem_cache_create(MBUF_MEM_NAME, MSIZE, 0,
508 	                              mb_ctor_mbuf, mb_dtor_mbuf, NULL,
509 	                              NUULL,
510 	                              NULL, 0);
511 #endif
512 	/*zone_ext_refcnt = umem_cache_create(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 0,
513 				NULL, NULL, NULL,
514 				NULL,
515 				NULL, 0);*/
516 	SCTP_ZONE_INIT(zone_ext_refcnt, MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int), 0);
517 
518   /*zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0,
519 				 mb_ctor_clust, mb_dtor_clust, NULL,
520 				 &clust_mb_args,
521 				 NULL, 0);
522 	zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0, NULL, NULL, NULL, NULL, NULL,0);*/
523 #if defined(SCTP_SIMPLE_ALLOCATOR)
524 	SCTP_ZONE_INIT(zone_clust, MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0);
525 #else
526 	zone_clust = umem_cache_create(MBUF_CLUSTER_MEM_NAME, MCLBYTES, 0,
527 								   mb_ctor_clust, mb_dtor_clust, NULL,
528 								   &clust_mb_args,
529 								   NULL, 0);
530 #endif
531 
532 	/* uma_prealloc() goes here... */
533 
534 	/* __Userspace__ Add umem_reap here for low memory situation?
535 	 *
536 	 */
537 
538 }
539 
540 
541 
542 /*
543  * __Userspace__
544  *
545  * Constructor for Mbuf master zone. We have a different constructor
546  * for allocating the cluster.
547  *
548  * The 'arg' pointer points to a mb_args structure which
549  * contains call-specific information required to support the
550  * mbuf allocation API.  See user_mbuf.h.
551  *
552  * The flgs parameter below can be UMEM_DEFAULT or UMEM_NOFAIL depending on what
553  * was passed when umem_cache_alloc was called.
554  * TODO: Use UMEM_NOFAIL in umem_cache_alloc and also define a failure handler
555  * and call umem_nofail_callback(my_failure_handler) in the stack initialization routines
556  * The advantage of using UMEM_NOFAIL is that we don't have to check if umem_cache_alloc
557  * was successful or not. The failure handler would take care of it, if we use the UMEM_NOFAIL
558  * flag.
559  *
560  * NOTE Ref: http://docs.sun.com/app/docs/doc/819-2243/6n4i099p2?l=en&a=view&q=umem_zalloc)
561  * The umem_nofail_callback() function sets the **process-wide** UMEM_NOFAIL callback.
562  * It also mentions that umem_nofail_callback is Evolving.
563  *
564  */
565 static int
mb_ctor_mbuf(void * mem,void * arg,int flgs)566 mb_ctor_mbuf(void *mem, void *arg, int flgs)
567 {
568 #if USING_MBUF_CONSTRUCTOR
569 	struct mbuf *m;
570 	struct mb_args *args;
571 
572 	int flags;
573 	short type;
574 
575 	m = (struct mbuf *)mem;
576 	args = (struct mb_args *)arg;
577 	flags = args->flags;
578 	type = args->type;
579 
580 	/*
581 	 * The mbuf is initialized later.
582 	 *
583 	 */
584 	if (type == MT_NOINIT)
585 		return (0);
586 
587 	m->m_next = NULL;
588 	m->m_nextpkt = NULL;
589 	m->m_len = 0;
590 	m->m_flags = flags;
591 	m->m_type = type;
592 	if (flags & M_PKTHDR) {
593 		m->m_data = m->m_pktdat;
594 		m->m_pkthdr.rcvif = NULL;
595 		m->m_pkthdr.len = 0;
596 		m->m_pkthdr.header = NULL;
597 		m->m_pkthdr.csum_flags = 0;
598 		m->m_pkthdr.csum_data = 0;
599 		m->m_pkthdr.tso_segsz = 0;
600 		m->m_pkthdr.ether_vtag = 0;
601 		SLIST_INIT(&m->m_pkthdr.tags);
602 	} else
603 		m->m_data = m->m_dat;
604 #endif
605 	return (0);
606 }
607 
608 
609 /*
610  * __Userspace__
611  * The Mbuf master zone destructor.
612  * This would be called in response to umem_cache_destroy
613  * TODO: Recheck if this is what we want to do in this destructor.
614  * (Note: the number of times mb_dtor_mbuf is called is equal to the
615  * number of individual mbufs allocated from zone_mbuf.
616  */
617 static void
mb_dtor_mbuf(void * mem,void * arg)618 mb_dtor_mbuf(void *mem, void *arg)
619 {
620 	struct mbuf *m;
621 
622 	m = (struct mbuf *)mem;
623 	if ((m->m_flags & M_PKTHDR) != 0) {
624 		m_tag_delete_chain(m, NULL);
625 	}
626 }
627 
628 
629 /* __Userspace__
630  * The Cluster zone constructor.
631  *
632  * Here the 'arg' pointer points to the Mbuf which we
633  * are configuring cluster storage for.  If 'arg' is
634  * empty we allocate just the cluster without setting
635  * the mbuf to it.  See mbuf.h.
636  */
637 static int
mb_ctor_clust(void * mem,void * arg,int flgs)638 mb_ctor_clust(void *mem, void *arg, int flgs)
639 {
640 
641 #if USING_MBUF_CONSTRUCTOR
642 	struct mbuf *m;
643 	struct clust_args * cla;
644 	u_int *refcnt;
645 	int type, size;
646 	sctp_zone_t zone;
647 
648 	/* Assigning cluster of MCLBYTES. TODO: Add jumbo frame functionality */
649 	type = EXT_CLUSTER;
650 	zone = zone_clust;
651 	size = MCLBYTES;
652 
653 	cla = (struct clust_args *)arg;
654 	m = cla->parent_mbuf;
655 
656 	refcnt = SCTP_ZONE_GET(zone_ext_refcnt, u_int);
657 	/*refcnt = (u_int *)umem_cache_alloc(zone_ext_refcnt, UMEM_DEFAULT);*/
658 	*refcnt = 1;
659 
660 	if (m != NULL) {
661 		m->m_ext.ext_buf = (caddr_t)mem;
662 		m->m_data = m->m_ext.ext_buf;
663 		m->m_flags |= M_EXT;
664 		m->m_ext.ext_free = NULL;
665 		m->m_ext.ext_args = NULL;
666 		m->m_ext.ext_size = size;
667 		m->m_ext.ext_type = type;
668 		m->m_ext.ref_cnt = refcnt;
669 	}
670 #endif
671 	return (0);
672 }
673 
674 /* __Userspace__ */
675 static void
mb_dtor_clust(void * mem,void * arg)676 mb_dtor_clust(void *mem, void *arg)
677 {
678 
679   /* mem is of type caddr_t.  In sys/types.h we have typedef char * caddr_t;  */
680   /* mb_dtor_clust is called at time of umem_cache_destroy() (the number of times
681    * mb_dtor_clust is called is equal to the number of individual mbufs allocated
682    * from zone_clust. Similarly for mb_dtor_mbuf).
683    * At this point the following:
684    *  struct mbuf *m;
685    *   m = (struct mbuf *)arg;
686    *  assert (*(m->m_ext.ref_cnt) == 0); is not meaningful since  m->m_ext.ref_cnt = NULL;
687    *  has been done in mb_free_ext().
688    */
689 
690 }
691 
692 
693 
694 
695 /* Unlink and free a packet tag. */
696 void
m_tag_delete(struct mbuf * m,struct m_tag * t)697 m_tag_delete(struct mbuf *m, struct m_tag *t)
698 {
699 	KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", (void *)m, (void *)t));
700 	m_tag_unlink(m, t);
701 	m_tag_free(t);
702 }
703 
704 
705 /* Unlink and free a packet tag chain, starting from given tag. */
706 void
m_tag_delete_chain(struct mbuf * m,struct m_tag * t)707 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
708 {
709 
710 	struct m_tag *p, *q;
711 
712 	KASSERT(m, ("m_tag_delete_chain: null mbuf"));
713 	if (t != NULL)
714 		p = t;
715 	else
716 		p = SLIST_FIRST(&m->m_pkthdr.tags);
717 	if (p == NULL)
718 		return;
719 	while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
720 		m_tag_delete(m, q);
721 	m_tag_delete(m, p);
722 }
723 
724 #if 0
725 static void
726 sctp_print_mbuf_chain(struct mbuf *m)
727 {
728 	SCTP_DEBUG_USR(SCTP_DEBUG_USR, "Printing mbuf chain %p.\n", (void *)m);
729 	for(; m; m=m->m_next) {
730 		SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%p: m_len = %ld, m_type = %x, m_next = %p.\n", (void *)m, m->m_len, m->m_type, (void *)m->m_next);
731 		if (m->m_flags & M_EXT)
732 			SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%p: extend_size = %d, extend_buffer = %p, ref_cnt = %d.\n", (void *)m, m->m_ext.ext_size, (void *)m->m_ext.ext_buf, *(m->m_ext.ref_cnt));
733 	}
734 }
735 #endif
736 
737 /*
738  * Free an entire chain of mbufs and associated external buffers, if
739  * applicable.
740  */
741 void
m_freem(struct mbuf * mb)742 m_freem(struct mbuf *mb)
743 {
744 	while (mb != NULL)
745 		mb = m_free(mb);
746 }
747 
748 /*
749  * __Userspace__
750  * clean mbufs with M_EXT storage attached to them
751  * if the reference count hits 1.
752  */
753 void
mb_free_ext(struct mbuf * m)754 mb_free_ext(struct mbuf *m)
755 {
756 
757 	int skipmbuf;
758 
759 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
760 	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
761 
762 	/*
763 	 * check if the header is embedded in the cluster
764 	 */
765 	skipmbuf = (m->m_flags & M_NOFREE);
766 
767 	/* Free the external attached storage if this
768 	 * mbuf is the only reference to it.
769 	 *__Userspace__ TODO: jumbo frames
770 	 *
771 	*/
772 	/* NOTE: We had the same code that SCTP_DECREMENT_AND_CHECK_REFCOUNT
773 	         reduces to here before but the IPHONE malloc commit had changed
774 	         this to compare to 0 instead of 1 (see next line).  Why?
775 	        . .. this caused a huge memory leak in Linux.
776 	*/
777 #ifdef IPHONE
778 	if (atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 0)
779 #else
780 	if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(m->m_ext.ref_cnt))
781 #endif
782 	{
783 		if (m->m_ext.ext_type == EXT_CLUSTER){
784 #if defined(SCTP_SIMPLE_ALLOCATOR)
785 			mb_dtor_clust(m->m_ext.ext_buf, &clust_mb_args);
786 #endif
787 			SCTP_ZONE_FREE(zone_clust, m->m_ext.ext_buf);
788 			SCTP_ZONE_FREE(zone_ext_refcnt, (u_int*)m->m_ext.ref_cnt);
789 			m->m_ext.ref_cnt = NULL;
790 		}
791 	}
792 
793 	if (skipmbuf)
794 		return;
795 
796 
797 	/* __Userspace__ Also freeing the storage for ref_cnt
798 	 * Free this mbuf back to the mbuf zone with all m_ext
799 	 * information purged.
800 	 */
801 	m->m_ext.ext_buf = NULL;
802 	m->m_ext.ext_free = NULL;
803 	m->m_ext.ext_args = NULL;
804 	m->m_ext.ref_cnt = NULL;
805 	m->m_ext.ext_size = 0;
806 	m->m_ext.ext_type = 0;
807 	m->m_flags &= ~M_EXT;
808 #if defined(SCTP_SIMPLE_ALLOCATOR)
809 	mb_dtor_mbuf(m, NULL);
810 #endif
811 	SCTP_ZONE_FREE(zone_mbuf, m);
812 
813 	/*umem_cache_free(zone_mbuf, m);*/
814 }
815 
816 /*
817  * "Move" mbuf pkthdr from "from" to "to".
818  * "from" must have M_PKTHDR set, and "to" must be empty.
819  */
820 void
m_move_pkthdr(struct mbuf * to,struct mbuf * from)821 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
822 {
823 
824 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
825 	if ((to->m_flags & M_EXT) == 0)
826 		to->m_data = to->m_pktdat;
827 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
828 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
829 	from->m_flags &= ~M_PKTHDR;
830 }
831 
832 
833 /*
834  * Rearange an mbuf chain so that len bytes are contiguous
835  * and in the data area of an mbuf (so that mtod and dtom
836  * will work for a structure of size len).  Returns the resulting
837  * mbuf chain on success, frees it and returns null on failure.
838  * If there is room, it will add up to max_protohdr-len extra bytes to the
839  * contiguous region in an attempt to avoid being called next time.
840  */
841 struct mbuf *
m_pullup(struct mbuf * n,int len)842 m_pullup(struct mbuf *n, int len)
843 {
844 	struct mbuf *m;
845 	int count;
846 	int space;
847 
848 	/*
849 	 * If first mbuf has no cluster, and has room for len bytes
850 	 * without shifting current data, pullup into it,
851 	 * otherwise allocate a new mbuf to prepend to the chain.
852 	 */
853 	if ((n->m_flags & M_EXT) == 0 &&
854 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
855 		if (n->m_len >= len)
856 			return (n);
857 		m = n;
858 		n = n->m_next;
859 		len -= m->m_len;
860 	} else {
861 		if (len > MHLEN)
862 			goto bad;
863 		MGET(m, M_NOWAIT, n->m_type);
864 		if (m == NULL)
865 			goto bad;
866 		m->m_len = 0;
867 		if (n->m_flags & M_PKTHDR)
868 			M_MOVE_PKTHDR(m, n);
869 	}
870 	space = (int)(&m->m_dat[MLEN] - (m->m_data + m->m_len));
871 	do {
872 		count = min(min(max(len, max_protohdr), space), n->m_len);
873 		memcpy(mtod(m, caddr_t) + m->m_len,mtod(n, caddr_t), (u_int)count);
874 		len -= count;
875 		m->m_len += count;
876 		n->m_len -= count;
877 		space -= count;
878 		if (n->m_len)
879 			n->m_data += count;
880 		else
881 			n = m_free(n);
882 	} while (len > 0 && n);
883 	if (len > 0) {
884 		(void) m_free(m);
885 		goto bad;
886 	}
887 	m->m_next = n;
888 	return (m);
889 bad:
890 	m_freem(n);
891 	return (NULL);
892 }
893 
894 
895 static struct mbuf *
m_dup1(struct mbuf * m,int off,int len,int wait)896 m_dup1(struct mbuf *m, int off, int len, int wait)
897 {
898 	struct mbuf *n = NULL;
899 	int copyhdr;
900 
901 	if (len > MCLBYTES)
902 		return NULL;
903 	if (off == 0 && (m->m_flags & M_PKTHDR) != 0)
904 		copyhdr = 1;
905 	else
906 		copyhdr = 0;
907 	if (len >= MINCLSIZE) {
908 		if (copyhdr == 1) {
909 			m_clget(n, wait); /* TODO: include code for copying the header */
910 			m_dup_pkthdr(n, m, wait);
911 		} else
912 			m_clget(n, wait);
913 	} else {
914 		if (copyhdr == 1)
915 			n = m_gethdr(wait, m->m_type);
916 		else
917 			n = m_get(wait, m->m_type);
918 	}
919 	if (!n)
920 		return NULL; /* ENOBUFS */
921 
922 	if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
923 		m_free(n);
924 		return NULL;
925 	}
926 	m_copydata(m, off, len, mtod(n, caddr_t));
927 	n->m_len = len;
928 	return n;
929 }
930 
931 
932 /* Taken from sys/kern/uipc_mbuf2.c */
933 struct mbuf *
m_pulldown(struct mbuf * m,int off,int len,int * offp)934 m_pulldown(struct mbuf *m, int off, int len, int *offp)
935 {
936 	struct mbuf *n, *o;
937 	int hlen, tlen, olen;
938 	int writable;
939 
940 	/* check invalid arguments. */
941 	KASSERT(m, ("m == NULL in m_pulldown()"));
942 	if (len > MCLBYTES) {
943 		m_freem(m);
944 		return NULL;    /* impossible */
945 	}
946 
947 #ifdef PULLDOWN_DEBUG
948 	{
949 		struct mbuf *t;
950 		SCTP_DEBUG_USR(SCTP_DEBUG_USR, "before:");
951 		for (t = m; t; t = t->m_next)
952 			SCTP_DEBUG_USR(SCTP_DEBUG_USR, " %d", t->m_len);
953 		SCTP_DEBUG_USR(SCTP_DEBUG_USR, "\n");
954 	}
955 #endif
956 	n = m;
957 	while (n != NULL && off > 0) {
958 		if (n->m_len > off)
959 			break;
960 		off -= n->m_len;
961 		n = n->m_next;
962 	}
963 	/* be sure to point non-empty mbuf */
964 	while (n != NULL && n->m_len == 0)
965 		n = n->m_next;
966 	if (!n) {
967 		m_freem(m);
968 		return NULL;    /* mbuf chain too short */
969 	}
970 
971 	writable = 0;
972 	if ((n->m_flags & M_EXT) == 0 ||
973 	    (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n)))
974 		writable = 1;
975 
976 	/*
977 	 * the target data is on <n, off>.
978 	 * if we got enough data on the mbuf "n", we're done.
979 	 */
980 	if ((off == 0 || offp) && len <= n->m_len - off && writable)
981 		goto ok;
982 
983 	/*
984 	 * when len <= n->m_len - off and off != 0, it is a special case.
985 	 * len bytes from <n, off> sits in single mbuf, but the caller does
986 	 * not like the starting position (off).
987 	 * chop the current mbuf into two pieces, set off to 0.
988 	 */
989 	if (len <= n->m_len - off) {
990 		o = m_dup1(n, off, n->m_len - off, M_NOWAIT);
991 		if (o == NULL) {
992 			m_freem(m);
993 		return NULL;    /* ENOBUFS */
994 		}
995 		n->m_len = off;
996 		o->m_next = n->m_next;
997 		n->m_next = o;
998 		n = n->m_next;
999 		off = 0;
1000 		goto ok;
1001 	}
1002 	/*
1003 	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
1004 	 * and construct contiguous mbuf with m_len == len.
1005 	 * note that hlen + tlen == len, and tlen > 0.
1006 	 */
1007 	hlen = n->m_len - off;
1008 	tlen = len - hlen;
1009 
1010 	/*
1011 	 * ensure that we have enough trailing data on mbuf chain.
1012 	 * if not, we can do nothing about the chain.
1013 	 */
1014 	olen = 0;
1015 	for (o = n->m_next; o != NULL; o = o->m_next)
1016 		olen += o->m_len;
1017 	if (hlen + olen < len) {
1018 		m_freem(m);
1019 		return NULL;    /* mbuf chain too short */
1020 	}
1021 
1022 	/*
1023 	 * easy cases first.
1024 	 * we need to use m_copydata() to get data from <n->m_next, 0>.
1025 	 */
1026 	if ((off == 0 || offp) && (M_TRAILINGSPACE(n) >= tlen) && writable) {
1027 		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
1028 		n->m_len += tlen;
1029 		m_adj(n->m_next, tlen);
1030 		goto ok;
1031 	}
1032 
1033 	if ((off == 0 || offp) && (M_LEADINGSPACE(n->m_next) >= hlen) && writable) {
1034 		n->m_next->m_data -= hlen;
1035 		n->m_next->m_len += hlen;
1036 		memcpy( mtod(n->m_next, caddr_t), mtod(n, caddr_t) + off,hlen);
1037 		n->m_len -= hlen;
1038 		n = n->m_next;
1039 		off = 0;
1040 		goto ok;
1041 	}
1042 
1043 	/*
1044 	 * now, we need to do the hard way.  don't m_copy as there's no room
1045 	 * on both end.
1046 	 */
1047 	if (len > MLEN)
1048 		m_clget(o, M_NOWAIT);
1049 		/* o = m_getcl(M_NOWAIT, m->m_type, 0);*/
1050 	else
1051 		o = m_get(M_NOWAIT, m->m_type);
1052 	if (!o) {
1053 		m_freem(m);
1054 		return NULL;    /* ENOBUFS */
1055 	}
1056 	/* get hlen from <n, off> into <o, 0> */
1057 	o->m_len = hlen;
1058 	memcpy(mtod(o, caddr_t), mtod(n, caddr_t) + off, hlen);
1059 	n->m_len -= hlen;
1060 	/* get tlen from <n->m_next, 0> into <o, hlen> */
1061 	m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
1062 	o->m_len += tlen;
1063 	m_adj(n->m_next, tlen);
1064 	o->m_next = n->m_next;
1065 	n->m_next = o;
1066 	n = o;
1067 	off = 0;
1068 ok:
1069 #ifdef PULLDOWN_DEBUG
1070 	{
1071 		struct mbuf *t;
1072 		SCTP_DEBUG_USR(SCTP_DEBUG_USR, "after:");
1073 		for (t = m; t; t = t->m_next)
1074 			SCTP_DEBUG_USR(SCTP_DEBUG_USR, "%c%d", t == n ? '*' : ' ', t->m_len);
1075 		SCTP_DEBUG_USR(SCTP_DEBUG_USR, " (off=%d)\n", off);
1076 	}
1077 #endif
1078 	if (offp)
1079 		*offp = off;
1080 	return n;
1081 }
1082 
1083 /*
1084  * Attach the the cluster from *m to *n, set up m_ext in *n
1085  * and bump the refcount of the cluster.
1086  */
1087 static void
mb_dupcl(struct mbuf * n,struct mbuf * m)1088 mb_dupcl(struct mbuf *n, struct mbuf *m)
1089 {
1090 	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
1091 	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
1092 	KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
1093 
1094 	if (*(m->m_ext.ref_cnt) == 1)
1095 		*(m->m_ext.ref_cnt) += 1;
1096 	else
1097 		atomic_add_int(m->m_ext.ref_cnt, 1);
1098 	n->m_ext.ext_buf = m->m_ext.ext_buf;
1099 	n->m_ext.ext_free = m->m_ext.ext_free;
1100 	n->m_ext.ext_args = m->m_ext.ext_args;
1101 	n->m_ext.ext_size = m->m_ext.ext_size;
1102 	n->m_ext.ref_cnt = m->m_ext.ref_cnt;
1103 	n->m_ext.ext_type = m->m_ext.ext_type;
1104 	n->m_flags |= M_EXT;
1105 }
1106 
1107 
1108 /*
1109  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
1110  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
1111  * The wait parameter is a choice of M_TRYWAIT/M_NOWAIT from caller.
1112  * Note that the copy is read-only, because clusters are not copied,
1113  * only their reference counts are incremented.
1114  */
1115 
1116 struct mbuf *
m_copym(struct mbuf * m,int off0,int len,int wait)1117 m_copym(struct mbuf *m, int off0, int len, int wait)
1118 {
1119 	struct mbuf *n, **np;
1120 	int off = off0;
1121 	struct mbuf *top;
1122 	int copyhdr = 0;
1123 
1124 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
1125 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
1126 	KASSERT(m != NULL, ("m_copym, m is NULL"));
1127 
1128 #if !defined(INVARIANTS)
1129 	if (m == NULL) {
1130 		return (NULL);
1131 	}
1132 #endif
1133 	if (off == 0 && m->m_flags & M_PKTHDR)
1134 		copyhdr = 1;
1135 	while (off > 0) {
1136 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
1137 		if (off < m->m_len)
1138 			break;
1139 		off -= m->m_len;
1140 		m = m->m_next;
1141 	}
1142 	np = &top;
1143 	top = 0;
1144 	while (len > 0) {
1145 		if (m == NULL) {
1146 			KASSERT(len == M_COPYALL, ("m_copym, length > size of mbuf chain"));
1147 			break;
1148 		}
1149 		if (copyhdr)
1150 			MGETHDR(n, wait, m->m_type);
1151 		else
1152 			MGET(n, wait, m->m_type);
1153 		*np = n;
1154 		if (n == NULL)
1155 			goto nospace;
1156 		if (copyhdr) {
1157 			if (!m_dup_pkthdr(n, m, wait))
1158 				goto nospace;
1159 			if (len == M_COPYALL)
1160 				n->m_pkthdr.len -= off0;
1161 			else
1162 				n->m_pkthdr.len = len;
1163 			copyhdr = 0;
1164 		}
1165 		n->m_len = min(len, m->m_len - off);
1166 		if (m->m_flags & M_EXT) {
1167 			n->m_data = m->m_data + off;
1168 			mb_dupcl(n, m);
1169 		} else
1170 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, (u_int)n->m_len);
1171 		if (len != M_COPYALL)
1172 			len -= n->m_len;
1173 		off = 0;
1174 		m = m->m_next;
1175 		np = &n->m_next;
1176 	}
1177 
1178 	return (top);
1179 nospace:
1180 	m_freem(top);
1181 	return (NULL);
1182 }
1183 
1184 
1185 int
m_tag_copy_chain(struct mbuf * to,struct mbuf * from,int how)1186 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
1187 {
1188 	struct m_tag *p, *t, *tprev = NULL;
1189 
1190 	KASSERT(to && from, ("m_tag_copy_chain: null argument, to %p from %p", (void *)to, (void *)from));
1191 	m_tag_delete_chain(to, NULL);
1192 	SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
1193 		t = m_tag_copy(p, how);
1194 		if (t == NULL) {
1195 			m_tag_delete_chain(to, NULL);
1196 			return 0;
1197 		}
1198 		if (tprev == NULL)
1199 			SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
1200 		else
1201 			SLIST_INSERT_AFTER(tprev, t, m_tag_link);
1202 		tprev = t;
1203 	}
1204 	return 1;
1205 }
1206 
1207 /*
1208  * Duplicate "from"'s mbuf pkthdr in "to".
1209  * "from" must have M_PKTHDR set, and "to" must be empty.
1210  * In particular, this does a deep copy of the packet tags.
1211  */
1212 int
m_dup_pkthdr(struct mbuf * to,struct mbuf * from,int how)1213 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
1214 {
1215 
1216 	KASSERT(to, ("m_dup_pkthdr: to is NULL"));
1217 	KASSERT(from, ("m_dup_pkthdr: from is NULL"));
1218 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
1219 	if ((to->m_flags & M_EXT) == 0)
1220 		to->m_data = to->m_pktdat;
1221 	to->m_pkthdr = from->m_pkthdr;
1222 	SLIST_INIT(&to->m_pkthdr.tags);
1223 	return (m_tag_copy_chain(to, from, MBTOM(how)));
1224 }
1225 
1226 /* Copy a single tag. */
1227 struct m_tag *
m_tag_copy(struct m_tag * t,int how)1228 m_tag_copy(struct m_tag *t, int how)
1229 {
1230 	struct m_tag *p;
1231 
1232 	KASSERT(t, ("m_tag_copy: null tag"));
1233 	p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
1234 	if (p == NULL)
1235 		return (NULL);
1236 	memcpy(p + 1, t + 1, t->m_tag_len); /* Copy the data */
1237 	return p;
1238 }
1239 
1240 /* Get a packet tag structure along with specified data following. */
1241 struct m_tag *
m_tag_alloc(uint32_t cookie,int type,int len,int wait)1242 m_tag_alloc(uint32_t cookie, int type, int len, int wait)
1243 {
1244 	struct m_tag *t;
1245 
1246 	if (len < 0)
1247 		return NULL;
1248 	t = malloc(len + sizeof(struct m_tag));
1249 	if (t == NULL)
1250 		return NULL;
1251 	m_tag_setup(t, cookie, type, len);
1252 	t->m_tag_free = m_tag_free_default;
1253 	return t;
1254 }
1255 
1256 /* Free a packet tag. */
1257 void
m_tag_free_default(struct m_tag * t)1258 m_tag_free_default(struct m_tag *t)
1259 {
1260   free(t);
1261 }
1262 
1263 /*
1264  * Copy data from a buffer back into the indicated mbuf chain,
1265  * starting "off" bytes from the beginning, extending the mbuf
1266  * chain if necessary.
1267  */
1268 void
m_copyback(struct mbuf * m0,int off,int len,caddr_t cp)1269 m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
1270 {
1271 	int mlen;
1272 	struct mbuf *m = m0, *n;
1273 	int totlen = 0;
1274 
1275 	if (m0 == NULL)
1276 		return;
1277 	while (off > (mlen = m->m_len)) {
1278 		off -= mlen;
1279 		totlen += mlen;
1280 		if (m->m_next == NULL) {
1281 			n = m_get(M_NOWAIT, m->m_type);
1282 			if (n == NULL)
1283 				goto out;
1284 			memset(mtod(n, caddr_t), 0, MLEN);
1285 			n->m_len = min(MLEN, len + off);
1286 			m->m_next = n;
1287 		}
1288 		m = m->m_next;
1289 	}
1290 	while (len > 0) {
1291 		mlen = min (m->m_len - off, len);
1292 		memcpy(off + mtod(m, caddr_t), cp, (u_int)mlen);
1293 		cp += mlen;
1294 		len -= mlen;
1295 		mlen += off;
1296 		off = 0;
1297 		totlen += mlen;
1298 		if (len == 0)
1299 			break;
1300 		if (m->m_next == NULL) {
1301 			n = m_get(M_NOWAIT, m->m_type);
1302 			if (n == NULL)
1303 				break;
1304 			n->m_len = min(MLEN, len);
1305 			m->m_next = n;
1306 		}
1307 		m = m->m_next;
1308 	}
1309 out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1310 		m->m_pkthdr.len = totlen;
1311 }
1312 
1313 
1314 /*
1315  * Lesser-used path for M_PREPEND:
1316  * allocate new mbuf to prepend to chain,
1317  * copy junk along.
1318  */
1319 struct mbuf *
m_prepend(struct mbuf * m,int len,int how)1320 m_prepend(struct mbuf *m, int len, int how)
1321 {
1322 	struct mbuf *mn;
1323 
1324 	if (m->m_flags & M_PKTHDR)
1325 		MGETHDR(mn, how, m->m_type);
1326 	else
1327 		MGET(mn, how, m->m_type);
1328 	if (mn == NULL) {
1329 		m_freem(m);
1330 		return (NULL);
1331 	}
1332 	if (m->m_flags & M_PKTHDR)
1333 		M_MOVE_PKTHDR(mn, m);
1334 	mn->m_next = m;
1335 	m = mn;
1336 	if (m->m_flags & M_PKTHDR) {
1337 		if (len < MHLEN)
1338 			MH_ALIGN(m, len);
1339 	} else {
1340 		if (len < MLEN)
1341 			M_ALIGN(m, len);
1342 	}
1343 	m->m_len = len;
1344 	return (m);
1345 }
1346 
1347 /*
1348  * Copy data from an mbuf chain starting "off" bytes from the beginning,
1349  * continuing for "len" bytes, into the indicated buffer.
1350  */
1351 void
m_copydata(const struct mbuf * m,int off,int len,caddr_t cp)1352 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
1353 {
1354 	u_int count;
1355 
1356 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
1357 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
1358 	while (off > 0) {
1359 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
1360 		if (off < m->m_len)
1361 			break;
1362 		off -= m->m_len;
1363 		m = m->m_next;
1364 	}
1365 	while (len > 0) {
1366 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
1367 		count = min(m->m_len - off, len);
1368 		memcpy(cp, mtod(m, caddr_t) + off, count);
1369 		len -= count;
1370 		cp += count;
1371 		off = 0;
1372 		m = m->m_next;
1373 	}
1374 }
1375 
1376 
1377 /*
1378  * Concatenate mbuf chain n to m.
1379  * Both chains must be of the same type (e.g. MT_DATA).
1380  * Any m_pkthdr is not updated.
1381  */
1382 void
m_cat(struct mbuf * m,struct mbuf * n)1383 m_cat(struct mbuf *m, struct mbuf *n)
1384 {
1385 	while (m->m_next)
1386 		m = m->m_next;
1387 	while (n) {
1388 		if (m->m_flags & M_EXT ||
1389 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
1390 			/* just join the two chains */
1391 			m->m_next = n;
1392 			return;
1393 		}
1394 		/* splat the data from one into the other */
1395 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), (u_int)n->m_len);
1396 		m->m_len += n->m_len;
1397 		n = m_free(n);
1398 	}
1399 }
1400 
1401 
1402 void
m_adj(struct mbuf * mp,int req_len)1403 m_adj(struct mbuf *mp, int req_len)
1404 {
1405 	int len = req_len;
1406 	struct mbuf *m;
1407 	int count;
1408 
1409 	if ((m = mp) == NULL)
1410 		return;
1411 	if (len >= 0) {
1412 		/*
1413 		 * Trim from head.
1414 		 */
1415 		while (m != NULL && len > 0) {
1416 			if (m->m_len <= len) {
1417 				len -= m->m_len;
1418 				m->m_len = 0;
1419 				m = m->m_next;
1420 			} else {
1421 				m->m_len -= len;
1422 				m->m_data += len;
1423 				len = 0;
1424 			}
1425 		}
1426 		m = mp;
1427 		if (mp->m_flags & M_PKTHDR)
1428 			m->m_pkthdr.len -= (req_len - len);
1429 	} else {
1430 		/*
1431 		 * Trim from tail.  Scan the mbuf chain,
1432 		 * calculating its length and finding the last mbuf.
1433 		 * If the adjustment only affects this mbuf, then just
1434 		 * adjust and return.  Otherwise, rescan and truncate
1435 		 * after the remaining size.
1436 		 */
1437 		len = -len;
1438 		count = 0;
1439 		for (;;) {
1440 			count += m->m_len;
1441 			if (m->m_next == (struct mbuf *)0)
1442 				break;
1443 			m = m->m_next;
1444 		}
1445 		if (m->m_len >= len) {
1446 			m->m_len -= len;
1447 			if (mp->m_flags & M_PKTHDR)
1448 				mp->m_pkthdr.len -= len;
1449 			return;
1450 		}
1451 		count -= len;
1452 		if (count < 0)
1453 			count = 0;
1454 		/*
1455 		 * Correct length for chain is "count".
1456 		 * Find the mbuf with last data, adjust its length,
1457 		 * and toss data from remaining mbufs on chain.
1458 		 */
1459 		m = mp;
1460 		if (m->m_flags & M_PKTHDR)
1461 			m->m_pkthdr.len = count;
1462 		for (; m; m = m->m_next) {
1463 			if (m->m_len >= count) {
1464 				m->m_len = count;
1465 				if (m->m_next != NULL) {
1466 					m_freem(m->m_next);
1467 					m->m_next = NULL;
1468 				}
1469 				break;
1470 			}
1471 			count -= m->m_len;
1472 		}
1473 	}
1474 }
1475 
1476 
1477 /* m_split is used within sctp_handle_cookie_echo. */
1478 
1479 /*
1480  * Partition an mbuf chain in two pieces, returning the tail --
1481  * all but the first len0 bytes.  In case of failure, it returns NULL and
1482  * attempts to restore the chain to its original state.
1483  *
1484  * Note that the resulting mbufs might be read-only, because the new
1485  * mbuf can end up sharing an mbuf cluster with the original mbuf if
1486  * the "breaking point" happens to lie within a cluster mbuf. Use the
1487  * M_WRITABLE() macro to check for this case.
1488  */
1489 struct mbuf *
m_split(struct mbuf * m0,int len0,int wait)1490 m_split(struct mbuf *m0, int len0, int wait)
1491 {
1492 	struct mbuf *m, *n;
1493 	u_int len = len0, remain;
1494 
1495 	/* MBUF_CHECKSLEEP(wait); */
1496 	for (m = m0; m && (int)len > m->m_len; m = m->m_next)
1497 		len -= m->m_len;
1498 	if (m == NULL)
1499 		return (NULL);
1500 	remain = m->m_len - len;
1501 	if (m0->m_flags & M_PKTHDR) {
1502 		MGETHDR(n, wait, m0->m_type);
1503 		if (n == NULL)
1504 			return (NULL);
1505 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1506 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1507 		m0->m_pkthdr.len = len0;
1508 		if (m->m_flags & M_EXT)
1509 			goto extpacket;
1510 		if (remain > MHLEN) {
1511 			/* m can't be the lead packet */
1512 			MH_ALIGN(n, 0);
1513 			n->m_next = m_split(m, len, wait);
1514 			if (n->m_next == NULL) {
1515 				(void) m_free(n);
1516 				return (NULL);
1517 			} else {
1518 				n->m_len = 0;
1519 				return (n);
1520 			}
1521 		} else
1522 			MH_ALIGN(n, remain);
1523 	} else if (remain == 0) {
1524 		n = m->m_next;
1525 		m->m_next = NULL;
1526 		return (n);
1527 	} else {
1528 		MGET(n, wait, m->m_type);
1529 		if (n == NULL)
1530 			return (NULL);
1531 		M_ALIGN(n, remain);
1532 	}
1533 extpacket:
1534 	if (m->m_flags & M_EXT) {
1535 		n->m_data = m->m_data + len;
1536 		mb_dupcl(n, m);
1537 	} else {
1538 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
1539 	}
1540 	n->m_len = remain;
1541 	m->m_len = len;
1542 	n->m_next = m->m_next;
1543 	m->m_next = NULL;
1544 	return (n);
1545 }
1546 
1547 
1548 
1549 
1550 int
pack_send_buffer(caddr_t buffer,struct mbuf * mb)1551 pack_send_buffer(caddr_t buffer, struct mbuf* mb){
1552 
1553 	int count_to_copy;
1554 	int total_count_copied = 0;
1555 	int offset = 0;
1556 
1557 	do {
1558 		count_to_copy = mb->m_len;
1559 		memcpy(buffer+offset, mtod(mb, caddr_t), count_to_copy);
1560 		offset += count_to_copy;
1561 		total_count_copied += count_to_copy;
1562 		mb = mb->m_next;
1563 	} while(mb);
1564 
1565 	return (total_count_copied);
1566 }
1567