uipc_mbuf.c revision 101173
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
34 * $FreeBSD: head/sys/kern/uipc_mbuf.c 101173 2002-08-01 17:47:56Z rwatson $
35 */
36
37#include "opt_mac.h"
38#include "opt_param.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.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
51int	max_linkhdr;
52int	max_protohdr;
53int	max_hdr;
54int	max_datalen;
55
56/*
57 * sysctl(8) exported objects
58 */
59SYSCTL_DECL(_kern_ipc);
60SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
61	   &max_linkhdr, 0, "");
62SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
63	   &max_protohdr, 0, "");
64SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
65SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
66	   &max_datalen, 0, "");
67
68/*
69 * Copy mbuf pkthdr from "from" to "to".
70 * "from" must have M_PKTHDR set, and "to" must be empty.
71 * aux pointer will be moved to "to".
72 */
73void
74m_copy_pkthdr(struct mbuf *to, struct mbuf *from)
75{
76
77#if 0
78	KASSERT(to->m_flags & M_PKTHDR,
79	    ("m_copy_pkthdr() called on non-header"));
80#endif
81#ifdef MAC
82	if (to->m_flags & M_PKTHDR)
83		mac_destroy_mbuf(to);
84#endif
85	to->m_data = to->m_pktdat;
86	to->m_flags = from->m_flags & M_COPYFLAGS;
87	to->m_pkthdr = from->m_pkthdr;
88#ifdef MAC
89	mac_init_mbuf(to, 1);			/* XXXMAC no way to fail */
90	mac_create_mbuf_from_mbuf(from, to);
91#endif
92	from->m_pkthdr.aux = NULL;
93}
94
95/*
96 * Lesser-used path for M_PREPEND:
97 * allocate new mbuf to prepend to chain,
98 * copy junk along.
99 */
100struct mbuf *
101m_prepend(struct mbuf *m, int len, int how)
102{
103	struct mbuf *mn;
104
105	MGET(mn, how, m->m_type);
106	if (mn == NULL) {
107		m_freem(m);
108		return (NULL);
109	}
110	if (m->m_flags & M_PKTHDR) {
111		M_COPY_PKTHDR(mn, m);
112#ifdef MAC
113		mac_destroy_mbuf(m);
114#endif
115		m->m_flags &= ~M_PKTHDR;
116	}
117	mn->m_next = m;
118	m = mn;
119	if (len < MHLEN)
120		MH_ALIGN(m, len);
121	m->m_len = len;
122	return (m);
123}
124
125/*
126 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
127 * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
128 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
129 * Note that the copy is read-only, because clusters are not copied,
130 * only their reference counts are incremented.
131 */
132struct mbuf *
133m_copym(struct mbuf *m, int off0, int len, int wait)
134{
135	struct mbuf *n, **np;
136	int off = off0;
137	struct mbuf *top;
138	int copyhdr = 0;
139
140	KASSERT(off >= 0, ("m_copym, negative off %d", off));
141	KASSERT(len >= 0, ("m_copym, negative len %d", len));
142	if (off == 0 && m->m_flags & M_PKTHDR)
143		copyhdr = 1;
144	while (off > 0) {
145		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
146		if (off < m->m_len)
147			break;
148		off -= m->m_len;
149		m = m->m_next;
150	}
151	np = &top;
152	top = 0;
153	while (len > 0) {
154		if (m == NULL) {
155			KASSERT(len == M_COPYALL,
156			    ("m_copym, length > size of mbuf chain"));
157			break;
158		}
159		MGET(n, wait, m->m_type);
160		*np = n;
161		if (n == NULL)
162			goto nospace;
163		if (copyhdr) {
164			M_COPY_PKTHDR(n, m);
165			if (len == M_COPYALL)
166				n->m_pkthdr.len -= off0;
167			else
168				n->m_pkthdr.len = len;
169			copyhdr = 0;
170		}
171		n->m_len = min(len, m->m_len - off);
172		if (m->m_flags & M_EXT) {
173			n->m_data = m->m_data + off;
174			n->m_ext = m->m_ext;
175			n->m_flags |= M_EXT;
176			MEXT_ADD_REF(m);
177		} else
178			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
179			    (unsigned)n->m_len);
180		if (len != M_COPYALL)
181			len -= n->m_len;
182		off = 0;
183		m = m->m_next;
184		np = &n->m_next;
185	}
186	if (top == NULL)
187		mbstat.m_mcfail++;	/* XXX: No consistency. */
188
189	return (top);
190nospace:
191	m_freem(top);
192	mbstat.m_mcfail++;	/* XXX: No consistency. */
193	return (NULL);
194}
195
196/*
197 * Copy an entire packet, including header (which must be present).
198 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
199 * Note that the copy is read-only, because clusters are not copied,
200 * only their reference counts are incremented.
201 * Preserve alignment of the first mbuf so if the creator has left
202 * some room at the beginning (e.g. for inserting protocol headers)
203 * the copies still have the room available.
204 */
205struct mbuf *
206m_copypacket(struct mbuf *m, int how)
207{
208	struct mbuf *top, *n, *o;
209
210	MGET(n, how, m->m_type);
211	top = n;
212	if (n == NULL)
213		goto nospace;
214
215	M_COPY_PKTHDR(n, m);
216	n->m_len = m->m_len;
217	if (m->m_flags & M_EXT) {
218		n->m_data = m->m_data;
219		n->m_ext = m->m_ext;
220		n->m_flags |= M_EXT;
221		MEXT_ADD_REF(m);
222	} else {
223		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
224		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
225	}
226
227	m = m->m_next;
228	while (m) {
229		MGET(o, how, m->m_type);
230		if (o == NULL)
231			goto nospace;
232
233		n->m_next = o;
234		n = n->m_next;
235
236		n->m_len = m->m_len;
237		if (m->m_flags & M_EXT) {
238			n->m_data = m->m_data;
239			n->m_ext = m->m_ext;
240			n->m_flags |= M_EXT;
241			MEXT_ADD_REF(m);
242		} else {
243			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
244		}
245
246		m = m->m_next;
247	}
248	return top;
249nospace:
250	m_freem(top);
251	mbstat.m_mcfail++;	/* XXX: No consistency. */
252	return (NULL);
253}
254
255/*
256 * Copy data from an mbuf chain starting "off" bytes from the beginning,
257 * continuing for "len" bytes, into the indicated buffer.
258 */
259void
260m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
261{
262	unsigned count;
263
264	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
265	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
266	while (off > 0) {
267		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
268		if (off < m->m_len)
269			break;
270		off -= m->m_len;
271		m = m->m_next;
272	}
273	while (len > 0) {
274		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
275		count = min(m->m_len - off, len);
276		bcopy(mtod(m, caddr_t) + off, cp, count);
277		len -= count;
278		cp += count;
279		off = 0;
280		m = m->m_next;
281	}
282}
283
284/*
285 * Copy a packet header mbuf chain into a completely new chain, including
286 * copying any mbuf clusters.  Use this instead of m_copypacket() when
287 * you need a writable copy of an mbuf chain.
288 */
289struct mbuf *
290m_dup(struct mbuf *m, int how)
291{
292	struct mbuf **p, *top = NULL;
293	int remain, moff, nsize;
294
295	/* Sanity check */
296	if (m == NULL)
297		return (NULL);
298	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
299
300	/* While there's more data, get a new mbuf, tack it on, and fill it */
301	remain = m->m_pkthdr.len;
302	moff = 0;
303	p = &top;
304	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
305		struct mbuf *n;
306
307		/* Get the next new mbuf */
308		MGET(n, how, m->m_type);
309		if (n == NULL)
310			goto nospace;
311		if (top == NULL) {		/* first one, must be PKTHDR */
312			M_COPY_PKTHDR(n, m);
313			nsize = MHLEN;
314		} else				/* not the first one */
315			nsize = MLEN;
316		if (remain >= MINCLSIZE) {
317			MCLGET(n, how);
318			if ((n->m_flags & M_EXT) == 0) {
319				(void)m_free(n);
320				goto nospace;
321			}
322			nsize = MCLBYTES;
323		}
324		n->m_len = 0;
325
326		/* Link it into the new chain */
327		*p = n;
328		p = &n->m_next;
329
330		/* Copy data from original mbuf(s) into new mbuf */
331		while (n->m_len < nsize && m != NULL) {
332			int chunk = min(nsize - n->m_len, m->m_len - moff);
333
334			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
335			moff += chunk;
336			n->m_len += chunk;
337			remain -= chunk;
338			if (moff == m->m_len) {
339				m = m->m_next;
340				moff = 0;
341			}
342		}
343
344		/* Check correct total mbuf length */
345		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
346		    	("%s: bogus m_pkthdr.len", __func__));
347	}
348	return (top);
349
350nospace:
351	m_freem(top);
352	mbstat.m_mcfail++;	/* XXX: No consistency. */
353	return (NULL);
354}
355
356/*
357 * Concatenate mbuf chain n to m.
358 * Both chains must be of the same type (e.g. MT_DATA).
359 * Any m_pkthdr is not updated.
360 */
361void
362m_cat(struct mbuf *m, struct mbuf *n)
363{
364	while (m->m_next)
365		m = m->m_next;
366	while (n) {
367		if (m->m_flags & M_EXT ||
368		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
369			/* just join the two chains */
370			m->m_next = n;
371			return;
372		}
373		/* splat the data from one into the other */
374		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
375		    (u_int)n->m_len);
376		m->m_len += n->m_len;
377		n = m_free(n);
378	}
379}
380
381void
382m_adj(struct mbuf *mp, int req_len)
383{
384	int len = req_len;
385	struct mbuf *m;
386	int count;
387
388	if ((m = mp) == NULL)
389		return;
390	if (len >= 0) {
391		/*
392		 * Trim from head.
393		 */
394		while (m != NULL && len > 0) {
395			if (m->m_len <= len) {
396				len -= m->m_len;
397				m->m_len = 0;
398				m = m->m_next;
399			} else {
400				m->m_len -= len;
401				m->m_data += len;
402				len = 0;
403			}
404		}
405		m = mp;
406		if (mp->m_flags & M_PKTHDR)
407			m->m_pkthdr.len -= (req_len - len);
408	} else {
409		/*
410		 * Trim from tail.  Scan the mbuf chain,
411		 * calculating its length and finding the last mbuf.
412		 * If the adjustment only affects this mbuf, then just
413		 * adjust and return.  Otherwise, rescan and truncate
414		 * after the remaining size.
415		 */
416		len = -len;
417		count = 0;
418		for (;;) {
419			count += m->m_len;
420			if (m->m_next == (struct mbuf *)0)
421				break;
422			m = m->m_next;
423		}
424		if (m->m_len >= len) {
425			m->m_len -= len;
426			if (mp->m_flags & M_PKTHDR)
427				mp->m_pkthdr.len -= len;
428			return;
429		}
430		count -= len;
431		if (count < 0)
432			count = 0;
433		/*
434		 * Correct length for chain is "count".
435		 * Find the mbuf with last data, adjust its length,
436		 * and toss data from remaining mbufs on chain.
437		 */
438		m = mp;
439		if (m->m_flags & M_PKTHDR)
440			m->m_pkthdr.len = count;
441		for (; m; m = m->m_next) {
442			if (m->m_len >= count) {
443				m->m_len = count;
444				break;
445			}
446			count -= m->m_len;
447		}
448		while (m->m_next)
449			(m = m->m_next) ->m_len = 0;
450	}
451}
452
453/*
454 * Rearange an mbuf chain so that len bytes are contiguous
455 * and in the data area of an mbuf (so that mtod and dtom
456 * will work for a structure of size len).  Returns the resulting
457 * mbuf chain on success, frees it and returns null on failure.
458 * If there is room, it will add up to max_protohdr-len extra bytes to the
459 * contiguous region in an attempt to avoid being called next time.
460 */
461struct mbuf *
462m_pullup(struct mbuf *n, int len)
463{
464	struct mbuf *m;
465	int count;
466	int space;
467
468	/*
469	 * If first mbuf has no cluster, and has room for len bytes
470	 * without shifting current data, pullup into it,
471	 * otherwise allocate a new mbuf to prepend to the chain.
472	 */
473	if ((n->m_flags & M_EXT) == 0 &&
474	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
475		if (n->m_len >= len)
476			return (n);
477		m = n;
478		n = n->m_next;
479		len -= m->m_len;
480	} else {
481		if (len > MHLEN)
482			goto bad;
483		MGET(m, M_DONTWAIT, n->m_type);
484		if (m == NULL)
485			goto bad;
486		m->m_len = 0;
487		if (n->m_flags & M_PKTHDR) {
488			M_COPY_PKTHDR(m, n);
489			n->m_flags &= ~M_PKTHDR;
490		}
491	}
492	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
493	do {
494		count = min(min(max(len, max_protohdr), space), n->m_len);
495		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
496		  (unsigned)count);
497		len -= count;
498		m->m_len += count;
499		n->m_len -= count;
500		space -= count;
501		if (n->m_len)
502			n->m_data += count;
503		else
504			n = m_free(n);
505	} while (len > 0 && n);
506	if (len > 0) {
507		(void) m_free(m);
508		goto bad;
509	}
510	m->m_next = n;
511	return (m);
512bad:
513	m_freem(n);
514	mbstat.m_mpfail++;	/* XXX: No consistency. */
515	return (NULL);
516}
517
518/*
519 * Partition an mbuf chain in two pieces, returning the tail --
520 * all but the first len0 bytes.  In case of failure, it returns NULL and
521 * attempts to restore the chain to its original state.
522 *
523 * Note that the resulting mbufs might be read-only, because the new
524 * mbuf can end up sharing an mbuf cluster with the original mbuf if
525 * the "breaking point" happens to lie within a cluster mbuf. Use the
526 * M_WRITABLE() macro to check for this case.
527 */
528struct mbuf *
529m_split(struct mbuf *m0, int len0, int wait)
530{
531	struct mbuf *m, *n;
532	unsigned len = len0, remain;
533
534	for (m = m0; m && len > m->m_len; m = m->m_next)
535		len -= m->m_len;
536	if (m == NULL)
537		return (NULL);
538	remain = m->m_len - len;
539	if (m0->m_flags & M_PKTHDR) {
540		MGETHDR(n, wait, m0->m_type);
541		if (n == NULL)
542			return (NULL);
543		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
544		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
545		m0->m_pkthdr.len = len0;
546		if (m->m_flags & M_EXT)
547			goto extpacket;
548		if (remain > MHLEN) {
549			/* m can't be the lead packet */
550			MH_ALIGN(n, 0);
551			n->m_next = m_split(m, len, wait);
552			if (n->m_next == NULL) {
553				(void) m_free(n);
554				return (NULL);
555			} else {
556				n->m_len = 0;
557				return (n);
558			}
559		} else
560			MH_ALIGN(n, remain);
561	} else if (remain == 0) {
562		n = m->m_next;
563		m->m_next = NULL;
564		return (n);
565	} else {
566		MGET(n, wait, m->m_type);
567		if (n == NULL)
568			return (NULL);
569		M_ALIGN(n, remain);
570	}
571extpacket:
572	if (m->m_flags & M_EXT) {
573		n->m_flags |= M_EXT;
574		n->m_ext = m->m_ext;
575		MEXT_ADD_REF(m);
576		n->m_data = m->m_data + len;
577	} else {
578		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
579	}
580	n->m_len = remain;
581	m->m_len = len;
582	n->m_next = m->m_next;
583	m->m_next = NULL;
584	return (n);
585}
586/*
587 * Routine to copy from device local memory into mbufs.
588 * Note that `off' argument is offset into first mbuf of target chain from
589 * which to begin copying the data to.
590 */
591struct mbuf *
592m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
593	 void (*copy)(char *from, caddr_t to, u_int len))
594{
595	struct mbuf *m;
596	struct mbuf *top = 0, **mp = &top;
597	int len;
598
599	if (off < 0 || off > MHLEN)
600		return (NULL);
601
602	MGETHDR(m, M_DONTWAIT, MT_DATA);
603	if (m == NULL)
604		return (NULL);
605	m->m_pkthdr.rcvif = ifp;
606	m->m_pkthdr.len = totlen;
607	len = MHLEN;
608
609	while (totlen > 0) {
610		if (top) {
611			MGET(m, M_DONTWAIT, MT_DATA);
612			if (m == NULL) {
613				m_freem(top);
614				return (NULL);
615			}
616			len = MLEN;
617		}
618		if (totlen + off >= MINCLSIZE) {
619			MCLGET(m, M_DONTWAIT);
620			if (m->m_flags & M_EXT)
621				len = MCLBYTES;
622		} else {
623			/*
624			 * Place initial small packet/header at end of mbuf.
625			 */
626			if (top == NULL && totlen + off + max_linkhdr <= len) {
627				m->m_data += max_linkhdr;
628				len -= max_linkhdr;
629			}
630		}
631		if (off) {
632			m->m_data += off;
633			len -= off;
634			off = 0;
635		}
636		m->m_len = len = min(totlen, len);
637		if (copy)
638			copy(buf, mtod(m, caddr_t), (unsigned)len);
639		else
640			bcopy(buf, mtod(m, caddr_t), (unsigned)len);
641		buf += len;
642		*mp = m;
643		mp = &m->m_next;
644		totlen -= len;
645	}
646	return (top);
647}
648
649/*
650 * Copy data from a buffer back into the indicated mbuf chain,
651 * starting "off" bytes from the beginning, extending the mbuf
652 * chain if necessary.
653 */
654void
655m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
656{
657	int mlen;
658	struct mbuf *m = m0, *n;
659	int totlen = 0;
660
661	if (m0 == NULL)
662		return;
663	while (off > (mlen = m->m_len)) {
664		off -= mlen;
665		totlen += mlen;
666		if (m->m_next == NULL) {
667			n = m_get_clrd(M_DONTWAIT, m->m_type);
668			if (n == NULL)
669				goto out;
670			n->m_len = min(MLEN, len + off);
671			m->m_next = n;
672		}
673		m = m->m_next;
674	}
675	while (len > 0) {
676		mlen = min (m->m_len - off, len);
677		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
678		cp += mlen;
679		len -= mlen;
680		mlen += off;
681		off = 0;
682		totlen += mlen;
683		if (len == 0)
684			break;
685		if (m->m_next == NULL) {
686			n = m_get(M_DONTWAIT, m->m_type);
687			if (n == NULL)
688				break;
689			n->m_len = min(MLEN, len);
690			m->m_next = n;
691		}
692		m = m->m_next;
693	}
694out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
695		m->m_pkthdr.len = totlen;
696}
697
698void
699m_print(const struct mbuf *m)
700{
701	int len;
702	const struct mbuf *m2;
703
704	len = m->m_pkthdr.len;
705	m2 = m;
706	while (len) {
707		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
708		len -= m2->m_len;
709		m2 = m2->m_next;
710	}
711	return;
712}
713