xref: /freebsd/sys/sys/mbuf.h (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 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. 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  *	@(#)mbuf.h	8.5 (Berkeley) 2/19/95
30  * $FreeBSD$
31  */
32 
33 #ifndef _SYS_MBUF_H_
34 #define	_SYS_MBUF_H_
35 
36 /* XXX: These includes suck. Sorry! */
37 #include <sys/queue.h>
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <vm/uma.h>
41 #endif
42 
43 /*
44  * Mbufs are of a single size, MSIZE (sys/param.h), which
45  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
46  * MCLBYTES (also in sys/param.h), which has no additional overhead
47  * and is used instead of the internal data area; this is done when
48  * at least MINCLSIZE of data must be stored.  Additionally, it is possible
49  * to allocate a separate buffer externally and attach it to the mbuf in
50  * a way similar to that of mbuf clusters.
51  */
52 #define	MLEN		(MSIZE - sizeof(struct m_hdr))	/* normal data len */
53 #define	MHLEN		(MLEN - sizeof(struct pkthdr))	/* data len w/pkthdr */
54 #define	MINCLSIZE	(MHLEN + 1)	/* smallest amount to put in cluster */
55 #define	M_MAXCOMPRESS	(MHLEN / 2)	/* max amount to copy for compression */
56 
57 #ifdef _KERNEL
58 /*-
59  * Macros for type conversion:
60  * mtod(m, t)	-- Convert mbuf pointer to data pointer of correct type.
61  * dtom(x)	-- Convert data pointer within mbuf to mbuf pointer (XXX).
62  */
63 #define	mtod(m, t)	((t)((m)->m_data))
64 #define	dtom(x)		((struct mbuf *)((intptr_t)(x) & ~(MSIZE-1)))
65 
66 /*
67  * Argument structure passed to UMA routines during mbuf and packet
68  * allocations.
69  */
70 struct mb_args {
71 	int	flags;	/* Flags for mbuf being allocated */
72 	int	how;	/* How to allocate: M_WAITOK or M_DONTWAIT */
73 	short	type;	/* Type of mbuf being allocated */
74 };
75 #endif /* _KERNEL */
76 
77 /*
78  * Header present at the beginning of every mbuf.
79  */
80 struct m_hdr {
81 	struct	mbuf *mh_next;		/* next buffer in chain */
82 	struct	mbuf *mh_nextpkt;	/* next chain in queue/record */
83 	caddr_t	mh_data;		/* location of data */
84 	int	mh_len;			/* amount of data in this mbuf */
85 	int	mh_flags;		/* flags; see below */
86 	short	mh_type;		/* type of data in this mbuf */
87 };
88 
89 /*
90  * Packet tag structure (see below for details).
91  */
92 struct m_tag {
93 	SLIST_ENTRY(m_tag)	m_tag_link;	/* List of packet tags */
94 	u_int16_t		m_tag_id;	/* Tag ID */
95 	u_int16_t		m_tag_len;	/* Length of data */
96 	u_int32_t		m_tag_cookie;	/* ABI/Module ID */
97 	void			(*m_tag_free)(struct m_tag *);
98 };
99 
100 /*
101  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
102  */
103 struct pkthdr {
104 	struct	ifnet *rcvif;		/* rcv interface */
105 	int	len;			/* total packet length */
106 	/* variables for ip and tcp reassembly */
107 	void	*header;		/* pointer to packet header */
108 	/* variables for hardware checksum */
109 	int	csum_flags;		/* flags regarding checksum */
110 	int	csum_data;		/* data field used by csum routines */
111 	SLIST_HEAD(packet_tags, m_tag) tags; /* list of packet tags */
112 };
113 
114 /*
115  * Description of external storage mapped into mbuf; valid only if M_EXT is set.
116  */
117 struct m_ext {
118 	caddr_t	ext_buf;		/* start of buffer */
119 	void	(*ext_free)		/* free routine if not the usual */
120 		    (void *, void *);
121 	void	*ext_args;		/* optional argument pointer */
122 	u_int	ext_size;		/* size of buffer, for ext_free */
123 	u_int	*ref_cnt;		/* pointer to ref count info */
124 	int	ext_type;		/* type of external storage */
125 };
126 
127 /*
128  * The core of the mbuf object along with some shortcut defines for
129  * practical purposes.
130  */
131 struct mbuf {
132 	struct	m_hdr m_hdr;
133 	union {
134 		struct {
135 			struct	pkthdr MH_pkthdr;	/* M_PKTHDR set */
136 			union {
137 				struct	m_ext MH_ext;	/* M_EXT set */
138 				char	MH_databuf[MHLEN];
139 			} MH_dat;
140 		} MH;
141 		char	M_databuf[MLEN];		/* !M_PKTHDR, !M_EXT */
142 	} M_dat;
143 };
144 #define	m_next		m_hdr.mh_next
145 #define	m_len		m_hdr.mh_len
146 #define	m_data		m_hdr.mh_data
147 #define	m_type		m_hdr.mh_type
148 #define	m_flags		m_hdr.mh_flags
149 #define	m_nextpkt	m_hdr.mh_nextpkt
150 #define	m_act		m_nextpkt
151 #define	m_pkthdr	M_dat.MH.MH_pkthdr
152 #define	m_ext		M_dat.MH.MH_dat.MH_ext
153 #define	m_pktdat	M_dat.MH.MH_dat.MH_databuf
154 #define	m_dat		M_dat.M_databuf
155 
156 /*
157  * mbuf flags.
158  */
159 #define	M_EXT		0x0001	/* has associated external storage */
160 #define	M_PKTHDR	0x0002	/* start of record */
161 #define	M_EOR		0x0004	/* end of record */
162 #define	M_RDONLY	0x0008	/* associated data is marked read-only */
163 #define	M_PROTO1	0x0010	/* protocol-specific */
164 #define	M_PROTO2	0x0020	/* protocol-specific */
165 #define	M_PROTO3	0x0040	/* protocol-specific */
166 #define	M_PROTO4	0x0080	/* protocol-specific */
167 #define	M_PROTO5	0x0100	/* protocol-specific */
168 #define	M_PROTO6	0x4000	/* protocol-specific (avoid M_BCAST conflict) */
169 #define	M_FREELIST	0x8000	/* mbuf is on the free list */
170 
171 /*
172  * mbuf pkthdr flags (also stored in m_flags).
173  */
174 #define	M_BCAST		0x0200	/* send/received as link-level broadcast */
175 #define	M_MCAST		0x0400	/* send/received as link-level multicast */
176 #define	M_FRAG		0x0800	/* packet is a fragment of a larger packet */
177 #define	M_FIRSTFRAG	0x1000	/* packet is first fragment */
178 #define	M_LASTFRAG	0x2000	/* packet is last fragment */
179 
180 /*
181  * External buffer types: identify ext_buf type.
182  */
183 #define	EXT_CLUSTER	1	/* mbuf cluster */
184 #define	EXT_SFBUF	2	/* sendfile(2)'s sf_bufs */
185 #define	EXT_PACKET	3	/* came out of Packet zone */
186 #define	EXT_NET_DRV	100	/* custom ext_buf provided by net driver(s) */
187 #define	EXT_MOD_TYPE	200	/* custom module's ext_buf type */
188 #define	EXT_DISPOSABLE	300	/* can throw this buffer away w/page flipping */
189 #define	EXT_EXTREF	400	/* has externally maintained ref_cnt ptr */
190 
191 /*
192  * Flags copied when copying m_pkthdr.
193  */
194 #define	M_COPYFLAGS	(M_PKTHDR|M_EOR|M_RDONLY|M_PROTO1|M_PROTO1|M_PROTO2|\
195 			    M_PROTO3|M_PROTO4|M_PROTO5|M_PROTO6|\
196 			    M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG)
197 
198 /*
199  * Flags indicating hw checksum support and sw checksum requirements.
200  */
201 #define	CSUM_IP			0x0001		/* will csum IP */
202 #define	CSUM_TCP		0x0002		/* will csum TCP */
203 #define	CSUM_UDP		0x0004		/* will csum UDP */
204 #define	CSUM_IP_FRAGS		0x0008		/* will csum IP fragments */
205 #define	CSUM_FRAGMENT		0x0010		/* will do IP fragmentation */
206 
207 #define	CSUM_IP_CHECKED		0x0100		/* did csum IP */
208 #define	CSUM_IP_VALID		0x0200		/*   ... the csum is valid */
209 #define	CSUM_DATA_VALID		0x0400		/* csum_data field is valid */
210 #define	CSUM_PSEUDO_HDR		0x0800		/* csum_data has pseudo hdr */
211 
212 #define	CSUM_DELAY_DATA		(CSUM_TCP | CSUM_UDP)
213 #define	CSUM_DELAY_IP		(CSUM_IP)	/* XXX add ipv6 here too? */
214 
215 /*
216  * mbuf types.
217  */
218 #define	MT_NOTMBUF	0	/* USED INTERNALLY ONLY! Object is not mbuf */
219 #define	MT_DATA		1	/* dynamic (data) allocation */
220 #define	MT_HEADER	2	/* packet header */
221 #if 0
222 #define	MT_SOCKET	3	/* socket structure */
223 #define	MT_PCB		4	/* protocol control block */
224 #define	MT_RTABLE	5	/* routing tables */
225 #define	MT_HTABLE	6	/* IMP host tables */
226 #define	MT_ATABLE	7	/* address resolution tables */
227 #endif
228 #define	MT_SONAME	8	/* socket name */
229 #if 0
230 #define	MT_SOOPTS	10	/* socket options */
231 #endif
232 #define	MT_FTABLE	11	/* fragment reassembly header */
233 #if 0
234 #define	MT_RIGHTS	12	/* access rights */
235 #define	MT_IFADDR	13	/* interface address */
236 #endif
237 #define	MT_CONTROL	14	/* extra-data protocol message */
238 #define	MT_OOBDATA	15	/* expedited data  */
239 #define	MT_NTYPES	16	/* number of mbuf types for mbtypes[] */
240 
241 /*
242  * General mbuf allocator statistics structure.
243  */
244 struct mbstat {
245 	u_long	m_mbufs;	/* XXX */
246 	u_long	m_mclusts;	/* XXX */
247 
248 	u_long	m_drain;	/* times drained protocols for space */
249 	u_long	m_mcfail;	/* XXX: times m_copym failed */
250 	u_long	m_mpfail;	/* XXX: times m_pullup failed */
251 	u_long	m_msize;	/* length of an mbuf */
252 	u_long	m_mclbytes;	/* length of an mbuf cluster */
253 	u_long	m_minclsize;	/* min length of data to allocate a cluster */
254 	u_long	m_mlen;		/* length of data in an mbuf */
255 	u_long	m_mhlen;	/* length of data in a header mbuf */
256 
257 	/* Number of mbtypes (gives # elems in mbtypes[] array: */
258 	short	m_numtypes;
259 
260 	/* XXX: Sendfile stats should eventually move to their own struct */
261 	u_long	sf_iocnt;	/* times sendfile had to do disk I/O */
262 	u_long	sf_allocfail;	/* times sfbuf allocation failed */
263 	u_long	sf_allocwait;	/* times sfbuf allocation had to wait */
264 };
265 
266 /*
267  * Flags specifying how an allocation should be made.
268  *
269  * The flag to use is as follows:
270  * - M_DONTWAIT or M_NOWAIT from an interrupt handler to not block allocation.
271  * - M_WAIT or M_WAITOK or M_TRYWAIT from wherever it is safe to block.
272  *
273  * M_DONTWAIT/M_NOWAIT means that we will not block the thread explicitly
274  * and if we cannot allocate immediately we may return NULL,
275  * whereas M_WAIT/M_WAITOK/M_TRYWAIT means that if we cannot allocate
276  * resources we will block until they are available, and thus never
277  * return NULL.
278  *
279  * XXX Eventually just phase this out to use M_WAITOK/M_NOWAIT.
280  */
281 #define	MBTOM(how)	(how)
282 #define	M_DONTWAIT	M_NOWAIT
283 #define	M_TRYWAIT	M_WAITOK
284 #define	M_WAIT		M_WAITOK
285 
286 #ifdef _KERNEL
287 /*-
288  * mbuf external reference count management macros.
289  *
290  * MEXT_IS_REF(m): true if (m) is not the only mbuf referencing
291  *     the external buffer ext_buf.
292  *
293  * MEXT_REM_REF(m): remove reference to m_ext object.
294  *
295  * MEXT_ADD_REF(m): add reference to m_ext object already
296  *     referred to by (m).
297  */
298 #define	MEXT_IS_REF(m)	(*((m)->m_ext.ref_cnt) > 1)
299 
300 #define	MEXT_REM_REF(m) do {						\
301 	KASSERT(*((m)->m_ext.ref_cnt) > 0, ("m_ext refcnt < 0"));	\
302 	atomic_subtract_int((m)->m_ext.ref_cnt, 1);			\
303 } while(0)
304 
305 #define	MEXT_ADD_REF(m)	atomic_add_int((m)->m_ext.ref_cnt, 1)
306 
307 /*
308  * Network buffer allocation API
309  *
310  * The rest of it is defined in kern/subr_mbuf.c
311  */
312 
313 extern uma_zone_t	zone_mbuf;
314 extern uma_zone_t	zone_clust;
315 extern uma_zone_t	zone_pack;
316 
317 static __inline struct mbuf	*m_get(int how, short type);
318 static __inline struct mbuf	*m_gethdr(int how, short type);
319 static __inline struct mbuf	*m_getcl(int how, short type, int flags);
320 static __inline struct mbuf	*m_getclr(int how, short type);	/* XXX */
321 static __inline struct mbuf	*m_free(struct mbuf *m);
322 static __inline void		 m_clget(struct mbuf *m, int how);
323 static __inline void		 m_chtype(struct mbuf *m, short new_type);
324 void				 mb_free_ext(struct mbuf *);
325 
326 static __inline
327 struct mbuf *
328 m_get(int how, short type)
329 {
330 	struct mb_args args;
331 
332 	args.flags = 0;
333 	args.how = how;
334 	args.type = type;
335 	return (uma_zalloc_arg(zone_mbuf, &args, how));
336 }
337 
338 /* XXX This should be depracated, very little use */
339 static __inline
340 struct mbuf *
341 m_getclr(int how, short type)
342 {
343 	struct mbuf *m;
344 	struct mb_args args;
345 
346 	args.flags = 0;
347 	args.how = how;
348 	args.type = type;
349 	m = uma_zalloc_arg(zone_mbuf, &args, how);
350 	if (m != NULL)
351 		bzero(m->m_data, MLEN);
352 	return m;
353 }
354 
355 static __inline
356 struct mbuf *
357 m_gethdr(int how, short type)
358 {
359 	struct mb_args args;
360 
361 	args.flags = M_PKTHDR;
362 	args.how = how;
363 	args.type = type;
364 	return (uma_zalloc_arg(zone_mbuf, &args, how));
365 }
366 
367 static __inline
368 struct mbuf *
369 m_getcl(int how, short type, int flags)
370 {
371 	struct mb_args args;
372 
373 	args.flags = flags;
374 	args.how = how;
375 	args.type = type;
376 	return (uma_zalloc_arg(zone_pack, &args, how));
377 }
378 
379 static __inline
380 struct mbuf *
381 m_free(struct mbuf *m)
382 {
383 	struct mbuf *n = m->m_next;
384 
385 #ifdef INVARIANTS
386 	m->m_flags |= M_FREELIST;
387 #endif
388 	if (m->m_flags & M_EXT)
389 		mb_free_ext(m);
390 	else
391 		uma_zfree(zone_mbuf, m);
392 	return n;
393 }
394 
395 static __inline
396 void
397 m_clget(struct mbuf *m, int how)
398 {
399 	m->m_ext.ext_buf = NULL;
400 	uma_zalloc_arg(zone_clust, m, how);
401 }
402 
403 static __inline
404 void
405 m_chtype(struct mbuf *m, short new_type)
406 {
407 	m->m_type = new_type;
408 }
409 
410 /*
411  * mbuf, cluster, and external object allocation macros
412  * (for compatibility purposes).
413  */
414 /* NB: M_COPY_PKTHDR is deprecated.  Use M_MOVE_PKTHDR or m_dup_pktdr. */
415 #define	M_MOVE_PKTHDR(to, from)	m_move_pkthdr((to), (from))
416 #define	MGET(m, how, type)	((m) = m_get((how), (type)))
417 #define	MGETHDR(m, how, type)	((m) = m_gethdr((how), (type)))
418 #define	MCLGET(m, how)		m_clget((m), (how))
419 #define	MEXTADD(m, buf, size, free, args, flags, type) 			\
420     m_extadd((m), (caddr_t)(buf), (size), (free), (args), (flags), (type))
421 
422 /*
423  * Evaluate TRUE if it's safe to write to the mbuf m's data region (this
424  * can be both the local data payload, or an external buffer area,
425  * depending on whether M_EXT is set).
426  */
427 #define	M_WRITABLE(m)	(!((m)->m_flags & M_RDONLY) && (!((m)->m_flags  \
428 			    & M_EXT) || !MEXT_IS_REF(m)))
429 
430 /* Check if the supplied mbuf has a packet header, or else panic. */
431 #define	M_ASSERTPKTHDR(m)						\
432 	KASSERT(m != NULL && m->m_flags & M_PKTHDR,			\
433 	    ("%s: no mbuf packet header!", __func__))
434 
435 /* Ensure that the supplied mbuf is a valid, non-free mbuf. */
436 #define	M_ASSERTVALID(m)						\
437 	KASSERT((((struct mbuf *)m)->m_flags & M_FREELIST) == 0,	\
438 	    ("%s: attempted use of a free mbuf!", __func__))
439 
440 /*
441  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
442  * an object of the specified size at the end of the mbuf, longword aligned.
443  */
444 #define	M_ALIGN(m, len) do {						\
445 	(m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);		\
446 } while (0)
447 
448 /*
449  * As above, for mbufs allocated with m_gethdr/MGETHDR
450  * or initialized by M_COPY_PKTHDR.
451  */
452 #define	MH_ALIGN(m, len) do {						\
453 	(m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);		\
454 } while (0)
455 
456 /*
457  * Compute the amount of space available
458  * before the current start of data in an mbuf.
459  *
460  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
461  * of checking writability of the mbuf data area rests solely with the caller.
462  */
463 #define	M_LEADINGSPACE(m)						\
464 	((m)->m_flags & M_EXT ?						\
465 	    (M_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0):	\
466 	    (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :	\
467 	    (m)->m_data - (m)->m_dat)
468 
469 /*
470  * Compute the amount of space available
471  * after the end of data in an mbuf.
472  *
473  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
474  * of checking writability of the mbuf data area rests solely with the caller.
475  */
476 #define	M_TRAILINGSPACE(m)						\
477 	((m)->m_flags & M_EXT ?						\
478 	    (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size	\
479 		- ((m)->m_data + (m)->m_len) : 0) :			\
480 	    &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
481 
482 /*
483  * Arrange to prepend space of size plen to mbuf m.
484  * If a new mbuf must be allocated, how specifies whether to wait.
485  * If the allocation fails, the original mbuf chain is freed and m is
486  * set to NULL.
487  */
488 #define	M_PREPEND(m, plen, how) do {					\
489 	struct mbuf **_mmp = &(m);					\
490 	struct mbuf *_mm = *_mmp;					\
491 	int _mplen = (plen);						\
492 	int __mhow = (how);						\
493 									\
494 	if (M_LEADINGSPACE(_mm) >= _mplen) {				\
495 		_mm->m_data -= _mplen;					\
496 		_mm->m_len += _mplen;					\
497 	} else								\
498 		_mm = m_prepend(_mm, _mplen, __mhow);			\
499 	if (_mm != NULL && _mm->m_flags & M_PKTHDR)			\
500 		_mm->m_pkthdr.len += _mplen;				\
501 	*_mmp = _mm;							\
502 } while (0)
503 
504 /*
505  * Change mbuf to new type.
506  * This is a relatively expensive operation and should be avoided.
507  */
508 #define	MCHTYPE(m, t)	m_chtype((m), (t))
509 
510 /* Length to m_copy to copy all. */
511 #define	M_COPYALL	1000000000
512 
513 /* Compatibility with 4.3. */
514 #define	m_copy(m, o, l)	m_copym((m), (o), (l), M_DONTWAIT)
515 
516 extern	int max_datalen;		/* MHLEN - max_hdr */
517 extern	int max_hdr;			/* Largest link + protocol header */
518 extern	int max_linkhdr;		/* Largest link-level header */
519 extern	int max_protohdr;		/* Largest protocol header */
520 extern	struct mbstat mbstat;		/* General mbuf stats/infos */
521 extern	int nmbclusters;		/* Maximum number of clusters */
522 
523 struct uio;
524 
525 void		 m_adj(struct mbuf *, int);
526 int		 m_apply(struct mbuf *, int, int,
527 		    int (*)(void *, void *, u_int), void *);
528 void		 m_cat(struct mbuf *, struct mbuf *);
529 void		 m_extadd(struct mbuf *, caddr_t, u_int,
530 		    void (*)(void *, void *), void *, int, int);
531 void		 m_copyback(struct mbuf *, int, int, c_caddr_t);
532 void		 m_copydata(const struct mbuf *, int, int, caddr_t);
533 struct	mbuf	*m_copym(struct mbuf *, int, int, int);
534 struct	mbuf	*m_copypacket(struct mbuf *, int);
535 void		 m_copy_pkthdr(struct mbuf *, struct mbuf *);
536 struct	mbuf	*m_defrag(struct mbuf *, int);
537 struct	mbuf	*m_devget(char *, int, int, struct ifnet *,
538 		    void (*)(char *, caddr_t, u_int));
539 struct	mbuf	*m_dup(struct mbuf *, int);
540 int		 m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
541 u_int		 m_fixhdr(struct mbuf *);
542 struct	mbuf	*m_fragment(struct mbuf *, int, int);
543 void		 m_freem(struct mbuf *);
544 struct	mbuf	*m_getm(struct mbuf *, int, int, short);
545 struct	mbuf	*m_getptr(struct mbuf *, int, int *);
546 u_int		 m_length(struct mbuf *, struct mbuf **);
547 void		 m_move_pkthdr(struct mbuf *, struct mbuf *);
548 struct	mbuf	*m_prepend(struct mbuf *, int, int);
549 void		 m_print(const struct mbuf *);
550 struct	mbuf	*m_pulldown(struct mbuf *, int, int, int *);
551 struct	mbuf	*m_pullup(struct mbuf *, int);
552 struct	mbuf	*m_split(struct mbuf *, int, int);
553 struct	mbuf	*m_uiotombuf(struct uio *, int, int);
554 
555 /*-
556  * Network packets may have annotations attached by affixing a list
557  * of "packet tags" to the pkthdr structure.  Packet tags are
558  * dynamically allocated semi-opaque data structures that have
559  * a fixed header (struct m_tag) that specifies the size of the
560  * memory block and a <cookie,type> pair that identifies it.
561  * The cookie is a 32-bit unique unsigned value used to identify
562  * a module or ABI.  By convention this value is chose as the
563  * date+time that the module is created, expressed as the number of
564  * seconds since the epoch (e.g., using date -u +'%s').  The type value
565  * is an ABI/module-specific value that identifies a particular annotation
566  * and is private to the module.  For compatibility with systems
567  * like OpenBSD that define packet tags w/o an ABI/module cookie,
568  * the value PACKET_ABI_COMPAT is used to implement m_tag_get and
569  * m_tag_find compatibility shim functions and several tag types are
570  * defined below.  Users that do not require compatibility should use
571  * a private cookie value so that packet tag-related definitions
572  * can be maintained privately.
573  *
574  * Note that the packet tag returned by m_tag_alloc has the default
575  * memory alignment implemented by malloc.  To reference private data
576  * one can use a construct like:
577  *
578  *	struct m_tag *mtag = m_tag_alloc(...);
579  *	struct foo *p = (struct foo *)(mtag+1);
580  *
581  * if the alignment of struct m_tag is sufficient for referencing members
582  * of struct foo.  Otherwise it is necessary to embed struct m_tag within
583  * the private data structure to insure proper alignment; e.g.,
584  *
585  *	struct foo {
586  *		struct m_tag	tag;
587  *		...
588  *	};
589  *	struct foo *p = (struct foo *) m_tag_alloc(...);
590  *	struct m_tag *mtag = &p->tag;
591  */
592 
593 /*
594  * Persistent tags stay with an mbuf until the mbuf is reclaimed.
595  * Otherwise tags are expected to ``vanish'' when they pass through
596  * a network interface.  For most interfaces this happens normally
597  * as the tags are reclaimed when the mbuf is free'd.  However in
598  * some special cases reclaiming must be done manually.  An example
599  * is packets that pass through the loopback interface.  Also, one
600  * must be careful to do this when ``turning around'' packets (e.g.,
601  * icmp_reflect).
602  *
603  * To mark a tag persistent bit-or this flag in when defining the
604  * tag id.  The tag will then be treated as described above.
605  */
606 #define	MTAG_PERSISTENT				0x800
607 
608 #define	PACKET_TAG_NONE				0  /* Nadda */
609 
610 /* Packet tags for use with PACKET_ABI_COMPAT. */
611 #define	PACKET_TAG_IPSEC_IN_DONE		1  /* IPsec applied, in */
612 #define	PACKET_TAG_IPSEC_OUT_DONE		2  /* IPsec applied, out */
613 #define	PACKET_TAG_IPSEC_IN_CRYPTO_DONE		3  /* NIC IPsec crypto done */
614 #define	PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED	4  /* NIC IPsec crypto req'ed */
615 #define	PACKET_TAG_IPSEC_IN_COULD_DO_CRYPTO	5  /* NIC notifies IPsec */
616 #define	PACKET_TAG_IPSEC_PENDING_TDB		6  /* Reminder to do IPsec */
617 #define	PACKET_TAG_BRIDGE			7  /* Bridge processing done */
618 #define	PACKET_TAG_GIF				8  /* GIF processing done */
619 #define	PACKET_TAG_GRE				9  /* GRE processing done */
620 #define	PACKET_TAG_IN_PACKET_CHECKSUM		10 /* NIC checksumming done */
621 #define	PACKET_TAG_ENCAP			11 /* Encap.  processing */
622 #define	PACKET_TAG_IPSEC_SOCKET			12 /* IPSEC socket ref */
623 #define	PACKET_TAG_IPSEC_HISTORY		13 /* IPSEC history */
624 #define	PACKET_TAG_IPV6_INPUT			14 /* IPV6 input processing */
625 #define	PACKET_TAG_DUMMYNET			15 /* dummynet info */
626 #define	PACKET_TAG_DIVERT			17 /* divert info */
627 #define	PACKET_TAG_IPFORWARD			18 /* ipforward info */
628 #define	PACKET_TAG_MACLABEL	(19 | MTAG_PERSISTENT) /* MAC label */
629 #define	PACKET_TAG_PF_GENERATED	(20 | MTAG_PERSISTENT) /* PF, pass always */
630 #define	PACKET_TAG_PF_ROUTED			21 /* PF routed, avoid loops */
631 #define	PACKET_TAG_PF_FRAGCACHE			22 /* PF fragment cached */
632 #define	PACKET_TAG_PF_QID			23 /* PF ALTQ queue id */
633 #define	PACKET_TAG_PF_TAG			24 /* PF tagged */
634 #define	PACKET_TAG_RTSOCKFAM			25 /* rtsock sa family */
635 #define	PACKET_TAG_PF_TRANSLATE_LOCALHOST	26 /* PF translate localhost */
636 
637 /* Packet tag routines. */
638 struct	m_tag	*m_tag_alloc(u_int32_t, int, int, int);
639 void		 m_tag_delete(struct mbuf *, struct m_tag *);
640 void		 m_tag_delete_chain(struct mbuf *, struct m_tag *);
641 struct	m_tag	*m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
642 struct	m_tag	*m_tag_copy(struct m_tag *, int);
643 int		 m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
644 void		 m_tag_delete_nonpersistent(struct mbuf *);
645 
646 /*
647  * Initialize the list of tags associated with an mbuf.
648  */
649 static __inline void
650 m_tag_init(struct mbuf *m)
651 {
652 	SLIST_INIT(&m->m_pkthdr.tags);
653 }
654 
655 /*
656  * Set up the contents of a tag.  Note that this does not
657  * fill in the free method; the caller is expected to do that.
658  *
659  * XXX probably should be called m_tag_init, but that was
660  * already taken.
661  */
662 static __inline void
663 m_tag_setup(struct m_tag *t, u_int32_t cookie, int type, int len)
664 {
665 	t->m_tag_id = type;
666 	t->m_tag_len = len;
667 	t->m_tag_cookie = cookie;
668 }
669 
670 /*
671  * Reclaim resources associated with a tag.
672  */
673 static __inline void
674 m_tag_free(struct m_tag *t)
675 {
676 	(*t->m_tag_free)(t);
677 }
678 
679 /*
680  * Return the first tag associated with an mbuf.
681  */
682 static __inline struct m_tag *
683 m_tag_first(struct mbuf *m)
684 {
685 	return (SLIST_FIRST(&m->m_pkthdr.tags));
686 }
687 
688 /*
689  * Return the next tag in the list of tags associated with an mbuf.
690  */
691 static __inline struct m_tag *
692 m_tag_next(struct mbuf *m, struct m_tag *t)
693 {
694 	return (SLIST_NEXT(t, m_tag_link));
695 }
696 
697 /*
698  * Prepend a tag to the list of tags associated with an mbuf.
699  */
700 static __inline void
701 m_tag_prepend(struct mbuf *m, struct m_tag *t)
702 {
703 	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
704 }
705 
706 /*
707  * Unlink a tag from the list of tags associated with an mbuf.
708  */
709 static __inline void
710 m_tag_unlink(struct mbuf *m, struct m_tag *t)
711 {
712 	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
713 }
714 
715 /* These are for OpenBSD compatibility. */
716 #define	MTAG_ABI_COMPAT		0		/* compatibility ABI */
717 
718 static __inline struct m_tag *
719 m_tag_get(int type, int length, int wait)
720 {
721 	return (m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait));
722 }
723 
724 static __inline struct m_tag *
725 m_tag_find(struct mbuf *m, int type, struct m_tag *start)
726 {
727 	return (SLIST_EMPTY(&m->m_pkthdr.tags) ?
728 	    NULL : m_tag_locate(m, MTAG_ABI_COMPAT, type, start));
729 }
730 
731 /*
732  * Obtain next_hop information associated with the mbuf, if any.
733  * If a tag is present devalidate it also.
734  */
735 static __inline struct sockaddr_in *
736 m_claim_next(struct mbuf *m, int type)
737 {
738 	struct m_tag *mtag = m_tag_find(m, type, NULL);
739 	if (mtag) {
740 		struct sockaddr_in *sin = *(struct sockaddr_in **)(mtag + 1);
741 		mtag->m_tag_id = PACKET_TAG_NONE;
742 		return (sin);
743 	}
744 	return (NULL);
745 }
746 #endif /* _KERNEL */
747 
748 #endif /* !_SYS_MBUF_H_ */
749