]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_mbuf.c
Fix a logic error introduced with mandatory mbuf cluster refcounting and
[FreeBSD/FreeBSD.git] / sys / kern / uipc_mbuf.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1991, 1993
3  *      The Regents of the University of California.  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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_mac.h"
36 #include "opt_param.h"
37 #include "opt_mbuf_stress_test.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/mac.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/uio.h>
51
52 int     max_linkhdr;
53 int     max_protohdr;
54 int     max_hdr;
55 int     max_datalen;
56 #ifdef MBUF_STRESS_TEST
57 int     m_defragpackets;
58 int     m_defragbytes;
59 int     m_defraguseless;
60 int     m_defragfailure;
61 int     m_defragrandomfailures;
62 #endif
63
64 /*
65  * sysctl(8) exported objects
66  */
67 SYSCTL_DECL(_kern_ipc);
68 SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69            &max_linkhdr, 0, "");
70 SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71            &max_protohdr, 0, "");
72 SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73 SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74            &max_datalen, 0, "");
75 #ifdef MBUF_STRESS_TEST
76 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
77            &m_defragpackets, 0, "");
78 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
79            &m_defragbytes, 0, "");
80 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
81            &m_defraguseless, 0, "");
82 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
83            &m_defragfailure, 0, "");
84 SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85            &m_defragrandomfailures, 0, "");
86 #endif
87
88 /*
89  * Allocate a given length worth of mbufs and/or clusters (whatever fits
90  * best) and return a pointer to the top of the allocated chain.  If an
91  * existing mbuf chain is provided, then we will append the new chain
92  * to the existing one but still return the top of the newly allocated
93  * chain.
94  */
95 struct mbuf *
96 m_getm(struct mbuf *m, int len, int how, short type)
97 {
98         struct mbuf *mb, *top, *cur, *mtail;
99         int num, rem;
100         int i;
101
102         KASSERT(len >= 0, ("m_getm(): len is < 0"));
103
104         /* If m != NULL, we will append to the end of that chain. */
105         if (m != NULL)
106                 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
107         else
108                 mtail = NULL;
109
110         /*
111          * Calculate how many mbufs+clusters ("packets") we need and how much
112          * leftover there is after that and allocate the first mbuf+cluster
113          * if required.
114          */
115         num = len / MCLBYTES;
116         rem = len % MCLBYTES;
117         top = cur = NULL;
118         if (num > 0) {
119                 if ((top = cur = m_getcl(how, type, 0)) == NULL)
120                         goto failed;
121                 top->m_len = 0;
122         }
123         num--;
124
125         for (i = 0; i < num; i++) {
126                 mb = m_getcl(how, type, 0);
127                 if (mb == NULL)
128                         goto failed;
129                 mb->m_len = 0;
130                 cur = (cur->m_next = mb);
131         }
132         if (rem > 0) {
133                 mb = (rem > MINCLSIZE) ?
134                     m_getcl(how, type, 0) : m_get(how, type);
135                 if (mb == NULL)
136                         goto failed;
137                 mb->m_len = 0;
138                 if (cur == NULL)
139                         top = mb;
140                 else
141                         cur->m_next = mb;
142         }
143
144         if (mtail != NULL)
145                 mtail->m_next = top;
146         return top;
147 failed:
148         if (top != NULL)
149                 m_freem(top);
150         return NULL;
151 }
152
153 /*
154  * Free an entire chain of mbufs and associated external buffers, if
155  * applicable.
156  */
157 void
158 m_freem(struct mbuf *mb)
159 {
160
161         while (mb != NULL)
162                 mb = m_free(mb);
163 }
164
165 /*-
166  * Configure a provided mbuf to refer to the provided external storage
167  * buffer and setup a reference count for said buffer.  If the setting
168  * up of the reference count fails, the M_EXT bit will not be set.  If
169  * successfull, the M_EXT bit is set in the mbuf's flags.
170  *
171  * Arguments:
172  *    mb     The existing mbuf to which to attach the provided buffer.
173  *    buf    The address of the provided external storage buffer.
174  *    size   The size of the provided buffer.
175  *    freef  A pointer to a routine that is responsible for freeing the
176  *           provided external storage buffer.
177  *    args   A pointer to an argument structure (of any type) to be passed
178  *           to the provided freef routine (may be NULL).
179  *    flags  Any other flags to be passed to the provided mbuf.
180  *    type   The type that the external storage buffer should be
181  *           labeled with.
182  *
183  * Returns:
184  *    Nothing.
185  */
186 void
187 m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
188     void (*freef)(void *, void *), void *args, int flags, int type)
189 {
190         KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
191
192         if (type != EXT_EXTREF)
193                 mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT);
194         if (mb->m_ext.ref_cnt != NULL) {
195                 *(mb->m_ext.ref_cnt) = 1;
196                 mb->m_flags |= (M_EXT | flags);
197                 mb->m_ext.ext_buf = buf;
198                 mb->m_data = mb->m_ext.ext_buf;
199                 mb->m_ext.ext_size = size;
200                 mb->m_ext.ext_free = freef;
201                 mb->m_ext.ext_args = args;
202                 mb->m_ext.ext_type = type;
203         }
204 }
205
206 /*
207  * Non-directly-exported function to clean up after mbufs with M_EXT
208  * storage attached to them if the reference count hits 1.
209  */
210 void
211 mb_free_ext(struct mbuf *m)
212 {
213         KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
214         KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
215
216         /* Free attached storage if this mbuf is the only reference to it. */
217         if (*(m->m_ext.ref_cnt) == 1 ||
218             atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 0) {
219                 switch (m->m_ext.ext_type) {
220                 case EXT_CLUSTER:       /* The packet zone is special. */
221                         if (*(m->m_ext.ref_cnt) == 0)
222                                 *(m->m_ext.ref_cnt) = 1;
223                         uma_zfree(zone_pack, m);
224                         return;         /* Job done. */
225                         break;
226                 case EXT_JUMBO9:
227                         uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
228                         break;
229                 case EXT_JUMBO16:
230                         uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
231                         break;
232                 case EXT_SFBUF:
233                 case EXT_NET_DRV:
234                 case EXT_MOD_TYPE:
235                 case EXT_DISPOSABLE:
236                         *(m->m_ext.ref_cnt) = 0;
237                         uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
238                                 m->m_ext.ref_cnt));
239                         /* FALLTHROUGH */
240                 case EXT_EXTREF:
241                         KASSERT(m->m_ext.ext_free != NULL,
242                                 ("%s: ext_free not set", __func__));
243                         (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
244                             m->m_ext.ext_args);
245                         break;
246                 default:
247                         KASSERT(m->m_ext.ext_type == 0,
248                                 ("%s: unknown ext_type", __func__));
249                 }
250         }
251         /*
252          * Free this mbuf back to the mbuf zone with all m_ext
253          * information purged.
254          */
255         m->m_ext.ext_buf = NULL;
256         m->m_ext.ext_free = NULL;
257         m->m_ext.ext_args = NULL;
258         m->m_ext.ref_cnt = NULL;
259         m->m_ext.ext_size = 0;
260         m->m_ext.ext_type = 0;
261         m->m_flags &= ~M_EXT;
262         uma_zfree(zone_mbuf, m);
263 }
264
265 /*
266  * Attach the the cluster from *m to *n, set up m_ext in *n
267  * and bump the refcount of the cluster.
268  */
269 static void
270 mb_dupcl(struct mbuf *n, struct mbuf *m)
271 {
272         KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
273         KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
274         KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
275
276         if (*(m->m_ext.ref_cnt) == 1)
277                 *(m->m_ext.ref_cnt) += 1;
278         else
279                 atomic_add_int(m->m_ext.ref_cnt, 1);
280         n->m_ext.ext_buf = m->m_ext.ext_buf;
281         n->m_ext.ext_free = m->m_ext.ext_free;
282         n->m_ext.ext_args = m->m_ext.ext_args;
283         n->m_ext.ext_size = m->m_ext.ext_size;
284         n->m_ext.ref_cnt = m->m_ext.ref_cnt;
285         n->m_ext.ext_type = m->m_ext.ext_type;
286         n->m_flags |= M_EXT;
287 }
288
289 /*
290  * Clean up mbuf (chain) from any tags and packet headers.
291  * If "all" is set then the first mbuf in the chain will be
292  * cleaned too.
293  */
294 void
295 m_demote(struct mbuf *m0, int all)
296 {
297         struct mbuf *m;
298
299         for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
300                 if (m->m_flags & M_PKTHDR) {
301                         m_tag_delete_chain(m, NULL);
302                         m->m_flags &= ~M_PKTHDR;
303                         bzero(&m->m_pkthdr, sizeof(struct pkthdr));
304                 }
305                 if (m->m_type == MT_HEADER)
306                         m->m_type = MT_DATA;
307                 if (m != m0 && m->m_nextpkt != NULL)
308                         m->m_nextpkt = NULL;
309                 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST);
310         }
311 }
312
313 /*
314  * Sanity checks on mbuf (chain) for use in KASSERT() and general
315  * debugging.
316  * Returns 0 or panics when bad and 1 on all tests passed.
317  * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
318  * blow up later.
319  */
320 int
321 m_sanity(struct mbuf *m0, int sanitize)
322 {
323         struct mbuf *m;
324         caddr_t a, b;
325         int pktlen = 0;
326
327 #define M_SANITY_ACTION(s)      return (0)
328 /* #define      M_SANITY_ACTION(s)      panic("mbuf %p: " s, m) */
329
330         for (m = m0; m != NULL; m = m->m_next) {
331                 /*
332                  * Basic pointer checks.  If any of these fails then some
333                  * unrelated kernel memory before or after us is trashed.
334                  * No way to recover from that.
335                  */
336                 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
337                         ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
338                          (caddr_t)(&m->m_dat)) );
339                 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
340                         ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
341                 if ((caddr_t)m->m_data < a)
342                         M_SANITY_ACTION("m_data outside mbuf data range left");
343                 if ((caddr_t)m->m_data > b)
344                         M_SANITY_ACTION("m_data outside mbuf data range right");
345                 if ((caddr_t)m->m_data + m->m_len > b)
346                         M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
347                 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
348                         if ((caddr_t)m->m_pkthdr.header < a ||
349                             (caddr_t)m->m_pkthdr.header > b)
350                                 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
351                 }
352
353                 /* m->m_nextpkt may only be set on first mbuf in chain. */
354                 if (m != m0 && m->m_nextpkt != NULL) {
355                         if (sanitize) {
356                                 m_freem(m->m_nextpkt);
357                                 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
358                         } else
359                                 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
360                 }
361
362                 /* correct type correlations. */
363                 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) {
364                         if (sanitize)
365                                 m->m_type = MT_DATA;
366                         else
367                                 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR");
368                 }
369
370                 /* packet length (not mbuf length!) calculation */
371                 if (m0->m_flags & M_PKTHDR)
372                         pktlen += m->m_len;
373
374                 /* m_tags may only be attached to first mbuf in chain. */
375                 if (m != m0 && m->m_flags & M_PKTHDR &&
376                     !SLIST_EMPTY(&m->m_pkthdr.tags)) {
377                         if (sanitize) {
378                                 m_tag_delete_chain(m, NULL);
379                                 /* put in 0xDEADC0DE perhaps? */
380                         } else
381                                 M_SANITY_ACTION("m_tags on in-chain mbuf");
382                 }
383
384                 /* M_PKTHDR may only be set on first mbuf in chain */
385                 if (m != m0 && m->m_flags & M_PKTHDR) {
386                         if (sanitize) {
387                                 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
388                                 m->m_flags &= ~M_PKTHDR;
389                                 /* put in 0xDEADCODE and leave hdr flag in */
390                         } else
391                                 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
392                 }
393         }
394         m = m0;
395         if (pktlen && pktlen != m->m_pkthdr.len) {
396                 if (sanitize)
397                         m->m_pkthdr.len = 0;
398                 else
399                         M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
400         }
401         return 1;
402
403 #undef  M_SANITY_ACTION
404 }
405
406
407 /*
408  * "Move" mbuf pkthdr from "from" to "to".
409  * "from" must have M_PKTHDR set, and "to" must be empty.
410  */
411 void
412 m_move_pkthdr(struct mbuf *to, struct mbuf *from)
413 {
414
415 #if 0
416         /* see below for why these are not enabled */
417         M_ASSERTPKTHDR(to);
418         /* Note: with MAC, this may not be a good assertion. */
419         KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
420             ("m_move_pkthdr: to has tags"));
421 #endif
422 #ifdef MAC
423         /*
424          * XXXMAC: It could be this should also occur for non-MAC?
425          */
426         if (to->m_flags & M_PKTHDR)
427                 m_tag_delete_chain(to, NULL);
428 #endif
429         to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
430         if ((to->m_flags & M_EXT) == 0)
431                 to->m_data = to->m_pktdat;
432         to->m_pkthdr = from->m_pkthdr;          /* especially tags */
433         SLIST_INIT(&from->m_pkthdr.tags);       /* purge tags from src */
434         from->m_flags &= ~M_PKTHDR;
435 }
436
437 /*
438  * Duplicate "from"'s mbuf pkthdr in "to".
439  * "from" must have M_PKTHDR set, and "to" must be empty.
440  * In particular, this does a deep copy of the packet tags.
441  */
442 int
443 m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
444 {
445
446 #if 0
447         /*
448          * The mbuf allocator only initializes the pkthdr
449          * when the mbuf is allocated with MGETHDR. Many users
450          * (e.g. m_copy*, m_prepend) use MGET and then
451          * smash the pkthdr as needed causing these
452          * assertions to trip.  For now just disable them.
453          */
454         M_ASSERTPKTHDR(to);
455         /* Note: with MAC, this may not be a good assertion. */
456         KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
457 #endif
458         MBUF_CHECKSLEEP(how);
459 #ifdef MAC
460         if (to->m_flags & M_PKTHDR)
461                 m_tag_delete_chain(to, NULL);
462 #endif
463         to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
464         if ((to->m_flags & M_EXT) == 0)
465                 to->m_data = to->m_pktdat;
466         to->m_pkthdr = from->m_pkthdr;
467         SLIST_INIT(&to->m_pkthdr.tags);
468         return (m_tag_copy_chain(to, from, MBTOM(how)));
469 }
470
471 /*
472  * Lesser-used path for M_PREPEND:
473  * allocate new mbuf to prepend to chain,
474  * copy junk along.
475  */
476 struct mbuf *
477 m_prepend(struct mbuf *m, int len, int how)
478 {
479         struct mbuf *mn;
480
481         if (m->m_flags & M_PKTHDR)
482                 MGETHDR(mn, how, m->m_type);
483         else
484                 MGET(mn, how, m->m_type);
485         if (mn == NULL) {
486                 m_freem(m);
487                 return (NULL);
488         }
489         if (m->m_flags & M_PKTHDR)
490                 M_MOVE_PKTHDR(mn, m);
491         mn->m_next = m;
492         m = mn;
493         if (len < MHLEN)
494                 MH_ALIGN(m, len);
495         m->m_len = len;
496         return (m);
497 }
498
499 /*
500  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
501  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
502  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
503  * Note that the copy is read-only, because clusters are not copied,
504  * only their reference counts are incremented.
505  */
506 struct mbuf *
507 m_copym(struct mbuf *m, int off0, int len, int wait)
508 {
509         struct mbuf *n, **np;
510         int off = off0;
511         struct mbuf *top;
512         int copyhdr = 0;
513
514         KASSERT(off >= 0, ("m_copym, negative off %d", off));
515         KASSERT(len >= 0, ("m_copym, negative len %d", len));
516         MBUF_CHECKSLEEP(wait);
517         if (off == 0 && m->m_flags & M_PKTHDR)
518                 copyhdr = 1;
519         while (off > 0) {
520                 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
521                 if (off < m->m_len)
522                         break;
523                 off -= m->m_len;
524                 m = m->m_next;
525         }
526         np = &top;
527         top = 0;
528         while (len > 0) {
529                 if (m == NULL) {
530                         KASSERT(len == M_COPYALL, 
531                             ("m_copym, length > size of mbuf chain"));
532                         break;
533                 }
534                 if (copyhdr)
535                         MGETHDR(n, wait, m->m_type);
536                 else
537                         MGET(n, wait, m->m_type);
538                 *np = n;
539                 if (n == NULL)
540                         goto nospace;
541                 if (copyhdr) {
542                         if (!m_dup_pkthdr(n, m, wait))
543                                 goto nospace;
544                         if (len == M_COPYALL)
545                                 n->m_pkthdr.len -= off0;
546                         else
547                                 n->m_pkthdr.len = len;
548                         copyhdr = 0;
549                 }
550                 n->m_len = min(len, m->m_len - off);
551                 if (m->m_flags & M_EXT) {
552                         n->m_data = m->m_data + off;
553                         mb_dupcl(n, m);
554                 } else
555                         bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
556                             (u_int)n->m_len);
557                 if (len != M_COPYALL)
558                         len -= n->m_len;
559                 off = 0;
560                 m = m->m_next;
561                 np = &n->m_next;
562         }
563         if (top == NULL)
564                 mbstat.m_mcfail++;      /* XXX: No consistency. */
565
566         return (top);
567 nospace:
568         m_freem(top);
569         mbstat.m_mcfail++;      /* XXX: No consistency. */
570         return (NULL);
571 }
572
573 /*
574  * Returns mbuf chain with new head for the prepending case.
575  * Copies from mbuf (chain) n from off for len to mbuf (chain) m
576  * either prepending or appending the data.
577  * The resulting mbuf (chain) m is fully writeable.
578  * m is destination (is made writeable)
579  * n is source, off is offset in source, len is len from offset
580  * dir, 0 append, 1 prepend
581  * how, wait or nowait
582  */
583
584 static int
585 m_bcopyxxx(void *s, void *t, u_int len)
586 {
587         bcopy(s, t, (size_t)len);
588         return 0;
589 }
590
591 struct mbuf *
592 m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
593     int prep, int how)
594 {
595         struct mbuf *mm, *x, *z, *prev = NULL;
596         caddr_t p;
597         int i, nlen = 0;
598         caddr_t buf[MLEN];
599
600         KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
601         KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
602         KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
603         KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
604
605         mm = m;
606         if (!prep) {
607                 while(mm->m_next) {
608                         prev = mm;
609                         mm = mm->m_next;
610                 }
611         }
612         for (z = n; z != NULL; z = z->m_next)
613                 nlen += z->m_len;
614         if (len == M_COPYALL)
615                 len = nlen - off;
616         if (off + len > nlen || len < 1)
617                 return NULL;
618
619         if (!M_WRITABLE(mm)) {
620                 /* XXX: Use proper m_xxx function instead. */
621                 x = m_getcl(how, MT_DATA, mm->m_flags);
622                 if (x == NULL)
623                         return NULL;
624                 bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
625                 p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf);
626                 x->m_data = p;
627                 mm->m_next = NULL;
628                 if (mm != m)
629                         prev->m_next = x;
630                 m_free(mm);
631                 mm = x;
632         }
633
634         /*
635          * Append/prepend the data.  Allocating mbufs as necessary.
636          */
637         /* Shortcut if enough free space in first/last mbuf. */
638         if (!prep && M_TRAILINGSPACE(mm) >= len) {
639                 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
640                          mm->m_len);
641                 mm->m_len += len;
642                 mm->m_pkthdr.len += len;
643                 return m;
644         }
645         if (prep && M_LEADINGSPACE(mm) >= len) {
646                 mm->m_data = mtod(mm, caddr_t) - len;
647                 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
648                 mm->m_len += len;
649                 mm->m_pkthdr.len += len;
650                 return mm;
651         }
652
653         /* Expand first/last mbuf to cluster if possible. */
654         if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
655                 bcopy(mm->m_data, &buf, mm->m_len);
656                 m_clget(mm, how);
657                 if (!(mm->m_flags & M_EXT))
658                         return NULL;
659                 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
660                 mm->m_data = mm->m_ext.ext_buf;
661                 mm->m_pkthdr.header = NULL;
662         }
663         if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
664                 bcopy(mm->m_data, &buf, mm->m_len);
665                 m_clget(mm, how);
666                 if (!(mm->m_flags & M_EXT))
667                         return NULL;
668                 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
669                        mm->m_ext.ext_size - mm->m_len, mm->m_len);
670                 mm->m_data = (caddr_t)mm->m_ext.ext_buf +
671                               mm->m_ext.ext_size - mm->m_len;
672                 mm->m_pkthdr.header = NULL;
673         }
674
675         /* Append/prepend as many mbuf (clusters) as necessary to fit len. */
676         if (!prep && len > M_TRAILINGSPACE(mm)) {
677                 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
678                         return NULL;
679         }
680         if (prep && len > M_LEADINGSPACE(mm)) {
681                 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
682                         return NULL;
683                 i = 0;
684                 for (x = z; x != NULL; x = x->m_next) {
685                         i += x->m_flags & M_EXT ? x->m_ext.ext_size :
686                               (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
687                         if (!x->m_next)
688                                 break;
689                 }
690                 z->m_data += i - len;
691                 m_move_pkthdr(mm, z);
692                 x->m_next = mm;
693                 mm = z;
694         }
695
696         /* Seek to start position in source mbuf. Optimization for long chains. */
697         while (off > 0) {
698                 if (off < n->m_len)
699                         break;
700                 off -= n->m_len;
701                 n = n->m_next;
702         }
703
704         /* Copy data into target mbuf. */
705         z = mm;
706         while (len > 0) {
707                 KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
708                 i = M_TRAILINGSPACE(z);
709                 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
710                 z->m_len += i;
711                 /* fixup pkthdr.len if necessary */
712                 if ((prep ? mm : m)->m_flags & M_PKTHDR)
713                         (prep ? mm : m)->m_pkthdr.len += i;
714                 off += i;
715                 len -= i;
716                 z = z->m_next;
717         }
718         return (prep ? mm : m);
719 }
720
721 /*
722  * Copy an entire packet, including header (which must be present).
723  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
724  * Note that the copy is read-only, because clusters are not copied,
725  * only their reference counts are incremented.
726  * Preserve alignment of the first mbuf so if the creator has left
727  * some room at the beginning (e.g. for inserting protocol headers)
728  * the copies still have the room available.
729  */
730 struct mbuf *
731 m_copypacket(struct mbuf *m, int how)
732 {
733         struct mbuf *top, *n, *o;
734
735         MBUF_CHECKSLEEP(how);
736         MGET(n, how, m->m_type);
737         top = n;
738         if (n == NULL)
739                 goto nospace;
740
741         if (!m_dup_pkthdr(n, m, how))
742                 goto nospace;
743         n->m_len = m->m_len;
744         if (m->m_flags & M_EXT) {
745                 n->m_data = m->m_data;
746                 mb_dupcl(n, m);
747         } else {
748                 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
749                 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
750         }
751
752         m = m->m_next;
753         while (m) {
754                 MGET(o, how, m->m_type);
755                 if (o == NULL)
756                         goto nospace;
757
758                 n->m_next = o;
759                 n = n->m_next;
760
761                 n->m_len = m->m_len;
762                 if (m->m_flags & M_EXT) {
763                         n->m_data = m->m_data;
764                         mb_dupcl(n, m);
765                 } else {
766                         bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
767                 }
768
769                 m = m->m_next;
770         }
771         return top;
772 nospace:
773         m_freem(top);
774         mbstat.m_mcfail++;      /* XXX: No consistency. */ 
775         return (NULL);
776 }
777
778 /*
779  * Copy data from an mbuf chain starting "off" bytes from the beginning,
780  * continuing for "len" bytes, into the indicated buffer.
781  */
782 void
783 m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
784 {
785         u_int count;
786
787         KASSERT(off >= 0, ("m_copydata, negative off %d", off));
788         KASSERT(len >= 0, ("m_copydata, negative len %d", len));
789         while (off > 0) {
790                 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
791                 if (off < m->m_len)
792                         break;
793                 off -= m->m_len;
794                 m = m->m_next;
795         }
796         while (len > 0) {
797                 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
798                 count = min(m->m_len - off, len);
799                 bcopy(mtod(m, caddr_t) + off, cp, count);
800                 len -= count;
801                 cp += count;
802                 off = 0;
803                 m = m->m_next;
804         }
805 }
806
807 /*
808  * Copy a packet header mbuf chain into a completely new chain, including
809  * copying any mbuf clusters.  Use this instead of m_copypacket() when
810  * you need a writable copy of an mbuf chain.
811  */
812 struct mbuf *
813 m_dup(struct mbuf *m, int how)
814 {
815         struct mbuf **p, *top = NULL;
816         int remain, moff, nsize;
817
818         MBUF_CHECKSLEEP(how);
819         /* Sanity check */
820         if (m == NULL)
821                 return (NULL);
822         M_ASSERTPKTHDR(m);
823
824         /* While there's more data, get a new mbuf, tack it on, and fill it */
825         remain = m->m_pkthdr.len;
826         moff = 0;
827         p = &top;
828         while (remain > 0 || top == NULL) {     /* allow m->m_pkthdr.len == 0 */
829                 struct mbuf *n;
830
831                 /* Get the next new mbuf */
832                 if (remain >= MINCLSIZE) {
833                         n = m_getcl(how, m->m_type, 0);
834                         nsize = MCLBYTES;
835                 } else {
836                         n = m_get(how, m->m_type);
837                         nsize = MLEN;
838                 }
839                 if (n == NULL)
840                         goto nospace;
841
842                 if (top == NULL) {              /* First one, must be PKTHDR */
843                         if (!m_dup_pkthdr(n, m, how)) {
844                                 m_free(n);
845                                 goto nospace;
846                         }
847                         nsize = MHLEN;
848                 }
849                 n->m_len = 0;
850
851                 /* Link it into the new chain */
852                 *p = n;
853                 p = &n->m_next;
854
855                 /* Copy data from original mbuf(s) into new mbuf */
856                 while (n->m_len < nsize && m != NULL) {
857                         int chunk = min(nsize - n->m_len, m->m_len - moff);
858
859                         bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
860                         moff += chunk;
861                         n->m_len += chunk;
862                         remain -= chunk;
863                         if (moff == m->m_len) {
864                                 m = m->m_next;
865                                 moff = 0;
866                         }
867                 }
868
869                 /* Check correct total mbuf length */
870                 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
871                         ("%s: bogus m_pkthdr.len", __func__));
872         }
873         return (top);
874
875 nospace:
876         m_freem(top);
877         mbstat.m_mcfail++;      /* XXX: No consistency. */
878         return (NULL);
879 }
880
881 /*
882  * Concatenate mbuf chain n to m.
883  * Both chains must be of the same type (e.g. MT_DATA).
884  * Any m_pkthdr is not updated.
885  */
886 void
887 m_cat(struct mbuf *m, struct mbuf *n)
888 {
889         while (m->m_next)
890                 m = m->m_next;
891         while (n) {
892                 if (m->m_flags & M_EXT ||
893                     m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
894                         /* just join the two chains */
895                         m->m_next = n;
896                         return;
897                 }
898                 /* splat the data from one into the other */
899                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
900                     (u_int)n->m_len);
901                 m->m_len += n->m_len;
902                 n = m_free(n);
903         }
904 }
905
906 void
907 m_adj(struct mbuf *mp, int req_len)
908 {
909         int len = req_len;
910         struct mbuf *m;
911         int count;
912
913         if ((m = mp) == NULL)
914                 return;
915         if (len >= 0) {
916                 /*
917                  * Trim from head.
918                  */
919                 while (m != NULL && len > 0) {
920                         if (m->m_len <= len) {
921                                 len -= m->m_len;
922                                 m->m_len = 0;
923                                 m = m->m_next;
924                         } else {
925                                 m->m_len -= len;
926                                 m->m_data += len;
927                                 len = 0;
928                         }
929                 }
930                 m = mp;
931                 if (mp->m_flags & M_PKTHDR)
932                         m->m_pkthdr.len -= (req_len - len);
933         } else {
934                 /*
935                  * Trim from tail.  Scan the mbuf chain,
936                  * calculating its length and finding the last mbuf.
937                  * If the adjustment only affects this mbuf, then just
938                  * adjust and return.  Otherwise, rescan and truncate
939                  * after the remaining size.
940                  */
941                 len = -len;
942                 count = 0;
943                 for (;;) {
944                         count += m->m_len;
945                         if (m->m_next == (struct mbuf *)0)
946                                 break;
947                         m = m->m_next;
948                 }
949                 if (m->m_len >= len) {
950                         m->m_len -= len;
951                         if (mp->m_flags & M_PKTHDR)
952                                 mp->m_pkthdr.len -= len;
953                         return;
954                 }
955                 count -= len;
956                 if (count < 0)
957                         count = 0;
958                 /*
959                  * Correct length for chain is "count".
960                  * Find the mbuf with last data, adjust its length,
961                  * and toss data from remaining mbufs on chain.
962                  */
963                 m = mp;
964                 if (m->m_flags & M_PKTHDR)
965                         m->m_pkthdr.len = count;
966                 for (; m; m = m->m_next) {
967                         if (m->m_len >= count) {
968                                 m->m_len = count;
969                                 if (m->m_next != NULL) {
970                                         m_freem(m->m_next);
971                                         m->m_next = NULL;
972                                 }
973                                 break;
974                         }
975                         count -= m->m_len;
976                 }
977         }
978 }
979
980 /*
981  * Rearange an mbuf chain so that len bytes are contiguous
982  * and in the data area of an mbuf (so that mtod and dtom
983  * will work for a structure of size len).  Returns the resulting
984  * mbuf chain on success, frees it and returns null on failure.
985  * If there is room, it will add up to max_protohdr-len extra bytes to the
986  * contiguous region in an attempt to avoid being called next time.
987  */
988 struct mbuf *
989 m_pullup(struct mbuf *n, int len)
990 {
991         struct mbuf *m;
992         int count;
993         int space;
994
995         /*
996          * If first mbuf has no cluster, and has room for len bytes
997          * without shifting current data, pullup into it,
998          * otherwise allocate a new mbuf to prepend to the chain.
999          */
1000         if ((n->m_flags & M_EXT) == 0 &&
1001             n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
1002                 if (n->m_len >= len)
1003                         return (n);
1004                 m = n;
1005                 n = n->m_next;
1006                 len -= m->m_len;
1007         } else {
1008                 if (len > MHLEN)
1009                         goto bad;
1010                 MGET(m, M_DONTWAIT, n->m_type);
1011                 if (m == NULL)
1012                         goto bad;
1013                 m->m_len = 0;
1014                 if (n->m_flags & M_PKTHDR)
1015                         M_MOVE_PKTHDR(m, n);
1016         }
1017         space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1018         do {
1019                 count = min(min(max(len, max_protohdr), space), n->m_len);
1020                 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1021                   (u_int)count);
1022                 len -= count;
1023                 m->m_len += count;
1024                 n->m_len -= count;
1025                 space -= count;
1026                 if (n->m_len)
1027                         n->m_data += count;
1028                 else
1029                         n = m_free(n);
1030         } while (len > 0 && n);
1031         if (len > 0) {
1032                 (void) m_free(m);
1033                 goto bad;
1034         }
1035         m->m_next = n;
1036         return (m);
1037 bad:
1038         m_freem(n);
1039         mbstat.m_mpfail++;      /* XXX: No consistency. */
1040         return (NULL);
1041 }
1042
1043 /*
1044  * Like m_pullup(), except a new mbuf is always allocated, and we allow
1045  * the amount of empty space before the data in the new mbuf to be specified
1046  * (in the event that the caller expects to prepend later).
1047  */
1048 int MSFail;
1049
1050 struct mbuf *
1051 m_copyup(struct mbuf *n, int len, int dstoff)
1052 {
1053         struct mbuf *m;
1054         int count, space;
1055
1056         if (len > (MHLEN - dstoff))
1057                 goto bad;
1058         MGET(m, M_DONTWAIT, n->m_type);
1059         if (m == NULL)
1060                 goto bad;
1061         m->m_len = 0;
1062         if (n->m_flags & M_PKTHDR)
1063                 M_MOVE_PKTHDR(m, n);
1064         m->m_data += dstoff;
1065         space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1066         do {
1067                 count = min(min(max(len, max_protohdr), space), n->m_len);
1068                 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1069                     (unsigned)count);
1070                 len -= count;
1071                 m->m_len += count;
1072                 n->m_len -= count;
1073                 space -= count;
1074                 if (n->m_len)
1075                         n->m_data += count;
1076                 else
1077                         n = m_free(n);
1078         } while (len > 0 && n);
1079         if (len > 0) {
1080                 (void) m_free(m);
1081                 goto bad;
1082         }
1083         m->m_next = n;
1084         return (m);
1085  bad:
1086         m_freem(n);
1087         MSFail++;
1088         return (NULL);
1089 }
1090
1091 /*
1092  * Partition an mbuf chain in two pieces, returning the tail --
1093  * all but the first len0 bytes.  In case of failure, it returns NULL and
1094  * attempts to restore the chain to its original state.
1095  *
1096  * Note that the resulting mbufs might be read-only, because the new
1097  * mbuf can end up sharing an mbuf cluster with the original mbuf if
1098  * the "breaking point" happens to lie within a cluster mbuf. Use the
1099  * M_WRITABLE() macro to check for this case.
1100  */
1101 struct mbuf *
1102 m_split(struct mbuf *m0, int len0, int wait)
1103 {
1104         struct mbuf *m, *n;
1105         u_int len = len0, remain;
1106
1107         MBUF_CHECKSLEEP(wait);
1108         for (m = m0; m && len > m->m_len; m = m->m_next)
1109                 len -= m->m_len;
1110         if (m == NULL)
1111                 return (NULL);
1112         remain = m->m_len - len;
1113         if (m0->m_flags & M_PKTHDR) {
1114                 MGETHDR(n, wait, m0->m_type);
1115                 if (n == NULL)
1116                         return (NULL);
1117                 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1118                 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1119                 m0->m_pkthdr.len = len0;
1120                 if (m->m_flags & M_EXT)
1121                         goto extpacket;
1122                 if (remain > MHLEN) {
1123                         /* m can't be the lead packet */
1124                         MH_ALIGN(n, 0);
1125                         n->m_next = m_split(m, len, wait);
1126                         if (n->m_next == NULL) {
1127                                 (void) m_free(n);
1128                                 return (NULL);
1129                         } else {
1130                                 n->m_len = 0;
1131                                 return (n);
1132                         }
1133                 } else
1134                         MH_ALIGN(n, remain);
1135         } else if (remain == 0) {
1136                 n = m->m_next;
1137                 m->m_next = NULL;
1138                 return (n);
1139         } else {
1140                 MGET(n, wait, m->m_type);
1141                 if (n == NULL)
1142                         return (NULL);
1143                 M_ALIGN(n, remain);
1144         }
1145 extpacket:
1146         if (m->m_flags & M_EXT) {
1147                 n->m_data = m->m_data + len;
1148                 mb_dupcl(n, m);
1149         } else {
1150                 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1151         }
1152         n->m_len = remain;
1153         m->m_len = len;
1154         n->m_next = m->m_next;
1155         m->m_next = NULL;
1156         return (n);
1157 }
1158 /*
1159  * Routine to copy from device local memory into mbufs.
1160  * Note that `off' argument is offset into first mbuf of target chain from
1161  * which to begin copying the data to.
1162  */
1163 struct mbuf *
1164 m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1165          void (*copy)(char *from, caddr_t to, u_int len))
1166 {
1167         struct mbuf *m;
1168         struct mbuf *top = NULL, **mp = &top;
1169         int len;
1170
1171         if (off < 0 || off > MHLEN)
1172                 return (NULL);
1173
1174         while (totlen > 0) {
1175                 if (top == NULL) {      /* First one, must be PKTHDR */
1176                         if (totlen + off >= MINCLSIZE) {
1177                                 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1178                                 len = MCLBYTES;
1179                         } else {
1180                                 m = m_gethdr(M_DONTWAIT, MT_DATA);
1181                                 len = MHLEN;
1182
1183                                 /* Place initial small packet/header at end of mbuf */
1184                                 if (m && totlen + off + max_linkhdr <= MLEN) {
1185                                         m->m_data += max_linkhdr;
1186                                         len -= max_linkhdr;
1187                                 }
1188                         }
1189                         if (m == NULL)
1190                                 return NULL;
1191                         m->m_pkthdr.rcvif = ifp;
1192                         m->m_pkthdr.len = totlen;
1193                 } else {
1194                         if (totlen + off >= MINCLSIZE) {
1195                                 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1196                                 len = MCLBYTES;
1197                         } else {
1198                                 m = m_get(M_DONTWAIT, MT_DATA);
1199                                 len = MLEN;
1200                         }
1201                         if (m == NULL) {
1202                                 m_freem(top);
1203                                 return NULL;
1204                         }
1205                 }
1206                 if (off) {
1207                         m->m_data += off;
1208                         len -= off;
1209                         off = 0;
1210                 }
1211                 m->m_len = len = min(totlen, len);
1212                 if (copy)
1213                         copy(buf, mtod(m, caddr_t), (u_int)len);
1214                 else
1215                         bcopy(buf, mtod(m, caddr_t), (u_int)len);
1216                 buf += len;
1217                 *mp = m;
1218                 mp = &m->m_next;
1219                 totlen -= len;
1220         }
1221         return (top);
1222 }
1223
1224 /*
1225  * Copy data from a buffer back into the indicated mbuf chain,
1226  * starting "off" bytes from the beginning, extending the mbuf
1227  * chain if necessary.
1228  */
1229 void
1230 m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1231 {
1232         int mlen;
1233         struct mbuf *m = m0, *n;
1234         int totlen = 0;
1235
1236         if (m0 == NULL)
1237                 return;
1238         while (off > (mlen = m->m_len)) {
1239                 off -= mlen;
1240                 totlen += mlen;
1241                 if (m->m_next == NULL) {
1242                         n = m_get(M_DONTWAIT, m->m_type);
1243                         if (n == NULL)
1244                                 goto out;
1245                         bzero(mtod(n, caddr_t), MLEN);
1246                         n->m_len = min(MLEN, len + off);
1247                         m->m_next = n;
1248                 }
1249                 m = m->m_next;
1250         }
1251         while (len > 0) {
1252                 mlen = min (m->m_len - off, len);
1253                 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1254                 cp += mlen;
1255                 len -= mlen;
1256                 mlen += off;
1257                 off = 0;
1258                 totlen += mlen;
1259                 if (len == 0)
1260                         break;
1261                 if (m->m_next == NULL) {
1262                         n = m_get(M_DONTWAIT, m->m_type);
1263                         if (n == NULL)
1264                                 break;
1265                         n->m_len = min(MLEN, len);
1266                         m->m_next = n;
1267                 }
1268                 m = m->m_next;
1269         }
1270 out:    if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1271                 m->m_pkthdr.len = totlen;
1272 }
1273
1274 /*
1275  * Append the specified data to the indicated mbuf chain,
1276  * Extend the mbuf chain if the new data does not fit in
1277  * existing space.
1278  *
1279  * Return 1 if able to complete the job; otherwise 0.
1280  */
1281 int
1282 m_append(struct mbuf *m0, int len, c_caddr_t cp)
1283 {
1284         struct mbuf *m, *n;
1285         int remainder, space;
1286
1287         for (m = m0; m->m_next != NULL; m = m->m_next)
1288                 ;
1289         remainder = len;
1290         space = M_TRAILINGSPACE(m);
1291         if (space > 0) {
1292                 /*
1293                  * Copy into available space.
1294                  */
1295                 if (space > remainder)
1296                         space = remainder;
1297                 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1298                 m->m_len += space;
1299                 cp += space, remainder -= space;
1300         }
1301         while (remainder > 0) {
1302                 /*
1303                  * Allocate a new mbuf; could check space
1304                  * and allocate a cluster instead.
1305                  */
1306                 n = m_get(M_DONTWAIT, m->m_type);
1307                 if (n == NULL)
1308                         break;
1309                 n->m_len = min(MLEN, remainder);
1310                 bcopy(cp, mtod(n, caddr_t), n->m_len);
1311                 cp += n->m_len, remainder -= n->m_len;
1312                 m->m_next = n;
1313                 m = n;
1314         }
1315         if (m0->m_flags & M_PKTHDR)
1316                 m0->m_pkthdr.len += len - remainder;
1317         return (remainder == 0);
1318 }
1319
1320 /*
1321  * Apply function f to the data in an mbuf chain starting "off" bytes from
1322  * the beginning, continuing for "len" bytes.
1323  */
1324 int
1325 m_apply(struct mbuf *m, int off, int len,
1326     int (*f)(void *, void *, u_int), void *arg)
1327 {
1328         u_int count;
1329         int rval;
1330
1331         KASSERT(off >= 0, ("m_apply, negative off %d", off));
1332         KASSERT(len >= 0, ("m_apply, negative len %d", len));
1333         while (off > 0) {
1334                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1335                 if (off < m->m_len)
1336                         break;
1337                 off -= m->m_len;
1338                 m = m->m_next;
1339         }
1340         while (len > 0) {
1341                 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1342                 count = min(m->m_len - off, len);
1343                 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1344                 if (rval)
1345                         return (rval);
1346                 len -= count;
1347                 off = 0;
1348                 m = m->m_next;
1349         }
1350         return (0);
1351 }
1352
1353 /*
1354  * Return a pointer to mbuf/offset of location in mbuf chain.
1355  */
1356 struct mbuf *
1357 m_getptr(struct mbuf *m, int loc, int *off)
1358 {
1359
1360         while (loc >= 0) {
1361                 /* Normal end of search. */
1362                 if (m->m_len > loc) {
1363                         *off = loc;
1364                         return (m);
1365                 } else {
1366                         loc -= m->m_len;
1367                         if (m->m_next == NULL) {
1368                                 if (loc == 0) {
1369                                         /* Point at the end of valid data. */
1370                                         *off = m->m_len;
1371                                         return (m);
1372                                 }
1373                                 return (NULL);
1374                         }
1375                         m = m->m_next;
1376                 }
1377         }
1378         return (NULL);
1379 }
1380
1381 void
1382 m_print(const struct mbuf *m, int maxlen)
1383 {
1384         int len;
1385         int pdata;
1386         const struct mbuf *m2;
1387
1388         if (m->m_flags & M_PKTHDR)
1389                 len = m->m_pkthdr.len;
1390         else
1391                 len = -1;
1392         m2 = m;
1393         while (m2 != NULL && (len == -1 || len)) {
1394                 pdata = m2->m_len;
1395                 if (maxlen != -1 && pdata > maxlen)
1396                         pdata = maxlen;
1397                 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1398                     m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1399                     "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1400                     "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1401                 if (pdata)
1402                         printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-");
1403                 if (len != -1)
1404                         len -= m2->m_len;
1405                 m2 = m2->m_next;
1406         }
1407         if (len > 0)
1408                 printf("%d bytes unaccounted for.\n", len);
1409         return;
1410 }
1411
1412 u_int
1413 m_fixhdr(struct mbuf *m0)
1414 {
1415         u_int len;
1416
1417         len = m_length(m0, NULL);
1418         m0->m_pkthdr.len = len;
1419         return (len);
1420 }
1421
1422 u_int
1423 m_length(struct mbuf *m0, struct mbuf **last)
1424 {
1425         struct mbuf *m;
1426         u_int len;
1427
1428         len = 0;
1429         for (m = m0; m != NULL; m = m->m_next) {
1430                 len += m->m_len;
1431                 if (m->m_next == NULL)
1432                         break;
1433         }
1434         if (last != NULL)
1435                 *last = m;
1436         return (len);
1437 }
1438
1439 /*
1440  * Defragment a mbuf chain, returning the shortest possible
1441  * chain of mbufs and clusters.  If allocation fails and
1442  * this cannot be completed, NULL will be returned, but
1443  * the passed in chain will be unchanged.  Upon success,
1444  * the original chain will be freed, and the new chain
1445  * will be returned.
1446  *
1447  * If a non-packet header is passed in, the original
1448  * mbuf (chain?) will be returned unharmed.
1449  */
1450 struct mbuf *
1451 m_defrag(struct mbuf *m0, int how)
1452 {
1453         struct mbuf *m_new = NULL, *m_final = NULL;
1454         int progress = 0, length;
1455
1456         MBUF_CHECKSLEEP(how);
1457         if (!(m0->m_flags & M_PKTHDR))
1458                 return (m0);
1459
1460         m_fixhdr(m0); /* Needed sanity check */
1461
1462 #ifdef MBUF_STRESS_TEST
1463         if (m_defragrandomfailures) {
1464                 int temp = arc4random() & 0xff;
1465                 if (temp == 0xba)
1466                         goto nospace;
1467         }
1468 #endif
1469         
1470         if (m0->m_pkthdr.len > MHLEN)
1471                 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1472         else
1473                 m_final = m_gethdr(how, MT_DATA);
1474
1475         if (m_final == NULL)
1476                 goto nospace;
1477
1478         if (m_dup_pkthdr(m_final, m0, how) == 0)
1479                 goto nospace;
1480
1481         m_new = m_final;
1482
1483         while (progress < m0->m_pkthdr.len) {
1484                 length = m0->m_pkthdr.len - progress;
1485                 if (length > MCLBYTES)
1486                         length = MCLBYTES;
1487
1488                 if (m_new == NULL) {
1489                         if (length > MLEN)
1490                                 m_new = m_getcl(how, MT_DATA, 0);
1491                         else
1492                                 m_new = m_get(how, MT_DATA);
1493                         if (m_new == NULL)
1494                                 goto nospace;
1495                 }
1496
1497                 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1498                 progress += length;
1499                 m_new->m_len = length;
1500                 if (m_new != m_final)
1501                         m_cat(m_final, m_new);
1502                 m_new = NULL;
1503         }
1504 #ifdef MBUF_STRESS_TEST
1505         if (m0->m_next == NULL)
1506                 m_defraguseless++;
1507 #endif
1508         m_freem(m0);
1509         m0 = m_final;
1510 #ifdef MBUF_STRESS_TEST
1511         m_defragpackets++;
1512         m_defragbytes += m0->m_pkthdr.len;
1513 #endif
1514         return (m0);
1515 nospace:
1516 #ifdef MBUF_STRESS_TEST
1517         m_defragfailure++;
1518 #endif
1519         if (m_final)
1520                 m_freem(m_final);
1521         return (NULL);
1522 }
1523
1524 #ifdef MBUF_STRESS_TEST
1525
1526 /*
1527  * Fragment an mbuf chain.  There's no reason you'd ever want to do
1528  * this in normal usage, but it's great for stress testing various
1529  * mbuf consumers.
1530  *
1531  * If fragmentation is not possible, the original chain will be
1532  * returned.
1533  *
1534  * Possible length values:
1535  * 0     no fragmentation will occur
1536  * > 0  each fragment will be of the specified length
1537  * -1   each fragment will be the same random value in length
1538  * -2   each fragment's length will be entirely random
1539  * (Random values range from 1 to 256)
1540  */
1541 struct mbuf *
1542 m_fragment(struct mbuf *m0, int how, int length)
1543 {
1544         struct mbuf *m_new = NULL, *m_final = NULL;
1545         int progress = 0;
1546
1547         if (!(m0->m_flags & M_PKTHDR))
1548                 return (m0);
1549         
1550         if ((length == 0) || (length < -2))
1551                 return (m0);
1552
1553         m_fixhdr(m0); /* Needed sanity check */
1554
1555         m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1556
1557         if (m_final == NULL)
1558                 goto nospace;
1559
1560         if (m_dup_pkthdr(m_final, m0, how) == 0)
1561                 goto nospace;
1562
1563         m_new = m_final;
1564
1565         if (length == -1)
1566                 length = 1 + (arc4random() & 255);
1567
1568         while (progress < m0->m_pkthdr.len) {
1569                 int fraglen;
1570
1571                 if (length > 0)
1572                         fraglen = length;
1573                 else
1574                         fraglen = 1 + (arc4random() & 255);
1575                 if (fraglen > m0->m_pkthdr.len - progress)
1576                         fraglen = m0->m_pkthdr.len - progress;
1577
1578                 if (fraglen > MCLBYTES)
1579                         fraglen = MCLBYTES;
1580
1581                 if (m_new == NULL) {
1582                         m_new = m_getcl(how, MT_DATA, 0);
1583                         if (m_new == NULL)
1584                                 goto nospace;
1585                 }
1586
1587                 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1588                 progress += fraglen;
1589                 m_new->m_len = fraglen;
1590                 if (m_new != m_final)
1591                         m_cat(m_final, m_new);
1592                 m_new = NULL;
1593         }
1594         m_freem(m0);
1595         m0 = m_final;
1596         return (m0);
1597 nospace:
1598         if (m_final)
1599                 m_freem(m_final);
1600         /* Return the original chain on failure */
1601         return (m0);
1602 }
1603
1604 #endif
1605
1606 struct mbuf *
1607 m_uiotombuf(struct uio *uio, int how, int len, int align)
1608 {
1609         struct mbuf *m_new = NULL, *m_final = NULL;
1610         int progress = 0, error = 0, length, total;
1611
1612         if (len > 0)
1613                 total = min(uio->uio_resid, len);
1614         else
1615                 total = uio->uio_resid;
1616         if (align >= MHLEN)
1617                 goto nospace;
1618         if (total + align > MHLEN)
1619                 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1620         else
1621                 m_final = m_gethdr(how, MT_DATA);
1622         if (m_final == NULL)
1623                 goto nospace;
1624         m_final->m_data += align;
1625         m_new = m_final;
1626         while (progress < total) {
1627                 length = total - progress;
1628                 if (length > MCLBYTES)
1629                         length = MCLBYTES;
1630                 if (m_new == NULL) {
1631                         if (length > MLEN)
1632                                 m_new = m_getcl(how, MT_DATA, 0);
1633                         else
1634                                 m_new = m_get(how, MT_DATA);
1635                         if (m_new == NULL)
1636                                 goto nospace;
1637                 }
1638                 error = uiomove(mtod(m_new, void *), length, uio);
1639                 if (error)
1640                         goto nospace;
1641                 progress += length;
1642                 m_new->m_len = length;
1643                 if (m_new != m_final)
1644                         m_cat(m_final, m_new);
1645                 m_new = NULL;
1646         }
1647         m_fixhdr(m_final);
1648         return (m_final);
1649 nospace:
1650         if (m_new)
1651                 m_free(m_new);
1652         if (m_final)
1653                 m_freem(m_final);
1654         return (NULL);
1655 }
1656
1657 /*
1658  * Set the m_data pointer of a newly-allocated mbuf
1659  * to place an object of the specified size at the
1660  * end of the mbuf, longword aligned.
1661  */
1662 void
1663 m_align(struct mbuf *m, int len)
1664 {
1665         int adjust;
1666
1667         if (m->m_flags & M_EXT)
1668                 adjust = m->m_ext.ext_size - len;
1669         else if (m->m_flags & M_PKTHDR)
1670                 adjust = MHLEN - len;
1671         else
1672                 adjust = MLEN - len;
1673         m->m_data += adjust &~ (sizeof(long)-1);
1674 }