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