xref: /xnu-8792.61.2/bsd/sys/mbuf.h (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1 /*
2  * Copyright (c) 1999-2021 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
29 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 /*
31  * Mach Operating System
32  * Copyright (c) 1987 Carnegie-Mellon University
33  * All rights reserved.  The CMU software License Agreement specifies
34  * the terms and conditions for use and redistribution.
35  */
36 /*
37  * Copyright (c) 1994 NeXT Computer, Inc. All rights reserved.
38  *
39  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *      This product includes software developed by the University of
53  *      California, Berkeley and its contributors.
54  * 4. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)mbuf.h	8.3 (Berkeley) 1/21/94
71  */
72 /*
73  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
74  * support for mandatory and extensible security protections.  This notice
75  * is included in support of clause 2.2 (b) of the Apple Public License,
76  * Version 2.0.
77  */
78 
79 #ifndef _SYS_MBUF_H_
80 #define _SYS_MBUF_H_
81 
82 #include <sys/appleapiopts.h>
83 #include <sys/cdefs.h>
84 #include <sys/_types/_u_int32_t.h> /* u_int32_t */
85 #include <sys/_types/_u_int64_t.h> /* u_int64_t */
86 #include <sys/_types/_u_short.h> /* u_short */
87 
88 #ifdef KERNEL
89 #include <sys/kpi_mbuf.h>
90 #endif
91 
92 #ifdef XNU_KERNEL_PRIVATE
93 #include <sys/lock.h>
94 #include <sys/queue.h>
95 #include <machine/endian.h>
96 /*
97  * Mbufs are of a single size, MSIZE (machine/param.h), which
98  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
99  * MCLBYTES/MBIGCLBYTES/M16KCLBYTES (also in machine/param.h), which has
100  * no additional overhead and is used instead of the internal data area;
101  * this is done when at least MINCLSIZE of data must be stored.
102  */
103 
104 /*
105  * The following _MLEN and _MHLEN macros are private to xnu.  Private code
106  * that are outside of xnu must use the mbuf_get_{mlen,mhlen} routines since
107  * the sizes of the structures are dependent upon specific xnu configs.
108  */
109 #define _MLEN           (MSIZE - sizeof(struct m_hdr))  /* normal data len */
110 #define _MHLEN          (_MLEN - sizeof(struct pkthdr)) /* data len w/pkthdr */
111 
112 #define NMBPGSHIFT      (PAGE_SHIFT - MSIZESHIFT)
113 #define NMBPG           (1 << NMBPGSHIFT)       /* # of mbufs per page */
114 
115 #define NCLPGSHIFT      (PAGE_SHIFT - MCLSHIFT)
116 #define NCLPG           (1 << NCLPGSHIFT)       /* # of cl per page */
117 
118 #define NBCLPGSHIFT     (PAGE_SHIFT - MBIGCLSHIFT)
119 #define NBCLPG          (1 << NBCLPGSHIFT)      /* # of big cl per page */
120 
121 #define NMBPCLSHIFT     (MCLSHIFT - MSIZESHIFT)
122 #define NMBPCL          (1 << NMBPCLSHIFT)      /* # of mbufs per cl */
123 
124 #define NCLPJCLSHIFT    (M16KCLSHIFT - MCLSHIFT)
125 #define NCLPJCL         (1 << NCLPJCLSHIFT)     /* # of cl per jumbo cl */
126 
127 #define NCLPBGSHIFT     (MBIGCLSHIFT - MCLSHIFT)
128 #define NCLPBG          (1 << NCLPBGSHIFT)      /* # of cl per big cl */
129 
130 #define NMBPBGSHIFT     (MBIGCLSHIFT - MSIZESHIFT)
131 #define NMBPBG          (1 << NMBPBGSHIFT)      /* # of mbufs per big cl */
132 
133 /*
134  * Macros for type conversion
135  * mtod(m,t) -	convert mbuf pointer to data pointer of correct type
136  * mtodo(m, o) -- Same as above but with offset 'o' into data.
137  * dtom(x) -	convert data pointer within mbuf to mbuf pointer (XXX)
138  */
139 #define mtod(m, t)      ((t)m_mtod_indexable(m))
140 #define mtodo(m, o)     ((void *)(mtod(m, uint8_t *) + (o)))
141 #define dtom(x)         m_dtom(x)
142 
143 /* header at beginning of each mbuf: */
144 struct m_hdr {
145 	struct mbuf                *mh_next;       /* next buffer in chain */
146 	struct mbuf                *mh_nextpkt;    /* next chain in queue/record */
147 	caddr_t __unsafe_indexable mh_data;        /* location of data */
148 	int32_t                    mh_len;         /* amount of data in this mbuf */
149 	u_int16_t                  mh_type;        /* type of data in this mbuf */
150 	u_int16_t                  mh_flags;       /* flags; see below */
151 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
152 /* This is needed because of how _MLEN is defined and used. Ideally, _MLEN
153  * should be defined using the offsetof(struct mbuf, M_dat), since there is
154  * no guarantee that mbuf.M_dat will start where mbuf.m_hdr ends. The compiler
155  * may (and does in the armv7k case) insert padding between m_hdr and M_dat in
156  * mbuf. We cannot easily use offsetof, however, since _MLEN is referenced
157  * in the definition of mbuf.
158  */
159 } __attribute__((aligned(8)));
160 #else
161 };
162 #endif
163 
164 /*
165  * Packet tag structure (see below for details).
166  */
167 struct m_tag {
168 	uint64_t               m_tag_cookie;   /* Error checking */
169 #ifndef __LP64__
170 	uint32_t               pad;            /* For structure alignment */
171 #endif /* !__LP64__ */
172 	SLIST_ENTRY(m_tag)      m_tag_link;     /* List of packet tags */
173 	uint16_t               m_tag_type;     /* Module specific type */
174 	uint16_t               m_tag_len;      /* Length of data */
175 	uint32_t               m_tag_id;       /* Module ID */
176 };
177 
178 #define M_TAG_ALIGN(len) \
179 	(P2ROUNDUP(len, sizeof (u_int64_t)) + sizeof (struct m_tag))
180 
181 #define M_TAG_VALID_PATTERN     0xfeedfacefeedfaceULL
182 #define M_TAG_FREE_PATTERN      0xdeadbeefdeadbeefULL
183 
184 /*
185  * Packet tag header structure (at the top of mbuf).  Pointers are
186  * 32-bit in ILP32; m_tag needs 64-bit alignment, hence padded.
187  */
188 struct m_taghdr {
189 #ifndef __LP64__
190 	u_int32_t               pad;            /* For structure alignment */
191 #endif /* !__LP64__ */
192 	u_int64_t               refcnt;         /* Number of tags in this mbuf */
193 };
194 
195 /*
196  * Driver auxiliary metadata tag (KERNEL_TAG_TYPE_DRVAUX).
197  */
198 struct m_drvaux_tag {
199 	u_int32_t       da_family;      /* IFNET_FAMILY values */
200 	u_int32_t       da_subfamily;   /* IFNET_SUBFAMILY values */
201 	u_int32_t       da_reserved;    /* for future */
202 	u_int32_t       da_length;      /* length of following data */
203 };
204 
205 /* Values for pftag_flags (16-bit wide) */
206 #define PF_TAG_GENERATED                0x1     /* pkt generated by PF */
207 #define PF_TAG_FRAGCACHE                0x2
208 #define PF_TAG_TRANSLATE_LOCALHOST      0x4
209 #if PF_ECN
210 #define PF_TAG_HDR_INET                 0x8     /* hdr points to IPv4 */
211 #define PF_TAG_HDR_INET6                0x10    /* hdr points to IPv6 */
212 #endif /* PF_ECN */
213 #define PF_TAG_REASSEMBLED              0x20    /* pkt reassembled by PF */
214 #define PF_TAG_REFRAGMENTED             0x40    /* pkt refragmented by PF */
215 /*
216  * PF mbuf tag
217  */
218 struct pf_mtag {
219 	u_int16_t       pftag_flags;    /* PF_TAG flags */
220 	u_int16_t       pftag_rtableid; /* alternate routing table id */
221 	u_int16_t       pftag_tag;
222 	u_int16_t       pftag_routed;
223 #if PF_ECN
224 	void            *pftag_hdr;     /* saved hdr pos in mbuf, for ECN */
225 #endif /* PF_ECN */
226 };
227 
228 /* System reserved PF tags */
229 #define PF_TAG_ID_SYSTEM_SERVICE        0xff00
230 #define PF_TAG_ID_STACK_DROP            0xff01
231 
232 /*
233  * PF fragment tag
234  */
235 struct pf_fragment_tag {
236 	uint32_t ft_id;     /* fragment id */
237 	uint16_t ft_hdrlen; /* header length of reassembled pkt */
238 	uint16_t ft_unfragpartlen; /* length of the per-fragment headers */
239 	uint16_t ft_extoff; /* last extension header offset or 0 */
240 	uint16_t ft_maxlen; /* maximum fragment payload length */
241 };
242 
243 /*
244  * TCP mbuf tag
245  */
246 struct tcp_pktinfo {
247 	union {
248 		struct {
249 			uint32_t segsz;        /* segment size (actual MSS) */
250 			uint32_t start_seq;    /* start seq of this packet */
251 			pid_t     pid;
252 			pid_t     e_pid;
253 		} __tx;
254 		struct {
255 			uint8_t  seg_cnt;    /* # of coalesced TCP pkts */
256 		} __rx;
257 	} __offload;
258 #define tso_segsz       proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.segsz
259 #define tx_start_seq    proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.start_seq
260 #define tx_tcp_pid      proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.pid
261 #define tx_tcp_e_pid    proto_mtag.__pr_u.tcp.tm_tcp.__offload.__tx.e_pid
262 #define seg_cnt         proto_mtag.__pr_u.tcp.tm_tcp.__offload.__rx.seg_cnt
263 };
264 
265 /*
266  * MPTCP mbuf tag
267  */
268 struct mptcp_pktinfo {
269 	uint64_t       mtpi_dsn;       /* MPTCP Data Sequence Number */
270 	uint32_t       mtpi_rel_seq;   /* Relative Seq Number */
271 	uint16_t       mtpi_length;    /* Length of mapping */
272 	uint16_t       mtpi_csum;
273 #define mp_dsn          proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_dsn
274 #define mp_rseq         proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_rel_seq
275 #define mp_rlen         proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_length
276 #define mp_csum         proto_mtag.__pr_u.tcp.tm_mptcp.mtpi_csum
277 };
278 
279 /*
280  * TCP specific mbuf tag.  Note that the current implementation uses
281  * MPTCP metadata strictly between MPTCP and the TCP subflow layers,
282  * hence tm_tcp and tm_mptcp are mutually exclusive.  This also means
283  * that TCP messages functionality is currently incompatible with MPTCP.
284  */
285 struct tcp_mtag {
286 	union {
287 		struct tcp_pktinfo      tm_tcp;         /* TCP and below */
288 		struct mptcp_pktinfo    tm_mptcp;       /* MPTCP-TCP only */
289 	};
290 };
291 
292 struct udp_mtag {
293 	pid_t     _pid;
294 	pid_t     _e_pid;
295 #define tx_udp_pid      proto_mtag.__pr_u.udp._pid
296 #define tx_udp_e_pid    proto_mtag.__pr_u.udp._e_pid
297 };
298 
299 struct rawip_mtag {
300 	pid_t     _pid;
301 	pid_t     _e_pid;
302 #define tx_rawip_pid    proto_mtag.__pr_u.rawip._pid
303 #define tx_rawip_e_pid  proto_mtag.__pr_u.rawip._e_pid
304 };
305 
306 struct driver_mtag_ {
307 	uintptr_t               _drv_tx_compl_arg;
308 	uintptr_t               _drv_tx_compl_data;
309 	kern_return_t           _drv_tx_status;
310 	uint16_t                _drv_flowid;
311 #define drv_tx_compl_arg        builtin_mtag._drv_mtag._drv_tx_compl_arg
312 #define drv_tx_compl_data       builtin_mtag._drv_mtag._drv_tx_compl_data
313 #define drv_tx_status           builtin_mtag._drv_mtag._drv_tx_status
314 #define drv_flowid              builtin_mtag._drv_mtag._drv_flowid
315 };
316 
317 /*
318  * Protocol specific mbuf tag (at most one protocol metadata per mbuf).
319  *
320  * Care must be taken to ensure that they are mutually exclusive, e.g.
321  * IPsec policy ID implies no TCP segment offload (which is fine given
322  * that the former is used on the virtual ipsec interface that does
323  * not advertise the TSO capability.)
324  */
325 struct proto_mtag_ {
326 	union {
327 		struct tcp_mtag tcp;            /* TCP specific */
328 		struct udp_mtag         udp;    /* UDP specific */
329 		struct rawip_mtag       rawip;  /* raw IPv4/IPv6 specific */
330 	} __pr_u;
331 };
332 
333 /*
334  * NECP specific mbuf tag.
335  */
336 struct necp_mtag_ {
337 	u_int32_t       necp_policy_id;
338 	u_int32_t       necp_skip_policy_id;
339 	u_int32_t       necp_route_rule_id;
340 	u_int16_t       necp_last_interface_index;
341 	u_int16_t       necp_app_id;
342 };
343 
344 union builtin_mtag {
345 	struct {
346 		struct proto_mtag_ _proto_mtag; /* built-in protocol-specific tag */
347 		struct pf_mtag  _pf_mtag;       /* built-in PF tag */
348 		struct necp_mtag_ _necp_mtag; /* built-in NECP tag */
349 	} _net_mtag;
350 	struct driver_mtag_ _drv_mtag;
351 #define necp_mtag builtin_mtag._net_mtag._necp_mtag
352 #define proto_mtag builtin_mtag._net_mtag._proto_mtag
353 #define driver_mtag builtin_mtag._drv_mtag
354 };
355 
356 /*
357  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR set.
358  */
359 struct pkthdr {
360 	struct ifnet *rcvif;            /* rcv interface */
361 	/* variables for ip and tcp reassembly */
362 	void    *pkt_hdr;               /* pointer to packet header */
363 	int32_t len;                    /* total packet length */
364 	/* variables for hardware checksum */
365 	/* Note: csum_flags is used for hardware checksum and VLAN */
366 	u_int32_t csum_flags;           /* flags regarding checksum */
367 	union {
368 		struct {
369 			u_int16_t val;   /* checksum value */
370 			u_int16_t start; /* checksum start offset */
371 		} _csum_rx;
372 #define csum_rx_val     _csum_rx.val
373 #define csum_rx_start   _csum_rx.start
374 		struct {
375 			u_int16_t start; /* checksum start offset */
376 			u_int16_t stuff; /* checksum stuff offset */
377 		} _csum_tx;
378 #define csum_tx_start   _csum_tx.start
379 #define csum_tx_stuff   _csum_tx.stuff
380 		/*
381 		 * Generic data field used by csum routines.
382 		 * It gets used differently in different contexts.
383 		 */
384 		u_int32_t csum_data;
385 	};
386 	u_int16_t vlan_tag;             /* VLAN tag, host byte order */
387 	/*
388 	 * Packet classifier info
389 	 *
390 	 * PKTF_FLOW_ID set means valid flow ID.  A non-zero flow ID value
391 	 * means the packet has been classified by one of the flow sources.
392 	 * It is also a prerequisite for flow control advisory, which is
393 	 * enabled by additionally setting PKTF_FLOW_ADV.
394 	 *
395 	 * The protocol value is a best-effort representation of the payload.
396 	 * It is opportunistically updated and used only for optimization.
397 	 * It is not a substitute for parsing the protocol header(s); use it
398 	 * only as a hint.
399 	 *
400 	 * If PKTF_IFAINFO is set, pkt_ifainfo contains one or both of the
401 	 * indices of interfaces which own the source and/or destination
402 	 * addresses of the packet.  For the local/loopback case (PKTF_LOOP),
403 	 * both should be valid, and thus allows for the receiving end to
404 	 * quickly determine the actual interfaces used by the the addresses;
405 	 * they may not necessarily be the same or refer to the loopback
406 	 * interface.  Otherwise, in the non-local/loopback case, the indices
407 	 * are opportunistically set, and because of that only one may be set
408 	 * (0 means the index has not been determined.)  In addition, the
409 	 * interface address flags are also recorded.  This allows us to avoid
410 	 * storing the corresponding {in,in6}_ifaddr in an mbuf tag.  Ideally
411 	 * this would be a superset of {ia,ia6}_flags, but the namespaces are
412 	 * overlapping at present, so we'll need a new set of values in future
413 	 * to achieve this.  For now, we will just rely on the address family
414 	 * related code paths examining this mbuf to interpret the flags.
415 	 */
416 	u_int8_t pkt_proto;             /* IPPROTO value */
417 	u_int8_t pkt_flowsrc;           /* FLOWSRC values */
418 	u_int32_t pkt_flowid;           /* flow ID */
419 	u_int32_t pkt_flags;            /* PKTF flags (see below) */
420 	u_int32_t pkt_svc;              /* MBUF_SVC value */
421 
422 	u_int32_t pkt_compl_context;            /* Packet completion context */
423 
424 	union {
425 		struct {
426 			u_int16_t src;          /* ifindex of src addr i/f */
427 			u_int16_t src_flags;    /* src PKT_IFAIFF flags */
428 			u_int16_t dst;          /* ifindex of dst addr i/f */
429 			u_int16_t dst_flags;    /* dst PKT_IFAIFF flags */
430 		} _pkt_iaif;
431 #define src_ifindex     _pkt_iaif.src
432 #define src_iff         _pkt_iaif.src_flags
433 #define dst_ifindex     _pkt_iaif.dst
434 #define dst_iff         _pkt_iaif.dst_flags
435 		u_int64_t pkt_ifainfo;  /* data field used by ifainfo */
436 		struct {
437 			u_int32_t if_data; /* bytes in interface queue */
438 			u_int32_t sndbuf_data; /* bytes in socket buffer */
439 		} _pkt_bsr;     /* Buffer status report used by cellular interface */
440 #define bufstatus_if    _pkt_bsr.if_data
441 #define bufstatus_sndbuf        _pkt_bsr.sndbuf_data
442 	};
443 	u_int64_t pkt_timestamp;        /* TX: enqueue time, RX: receive timestamp */
444 
445 	/*
446 	 * Tags (external and built-in)
447 	 */
448 	SLIST_HEAD(packet_tags, m_tag) tags; /* list of external tags */
449 	union builtin_mtag builtin_mtag;
450 
451 	uint32_t comp_gencnt;
452 	uint16_t pkt_ext_flags;
453 	uint16_t pkt_crumbs;
454 	/*
455 	 * Module private scratch space (32-bit aligned), currently 16-bytes
456 	 * large. Anything stored here is not guaranteed to survive across
457 	 * modules.  The AQM layer (outbound) uses all 16-bytes for both
458 	 * packet scheduling and flow advisory information.
459 	 */
460 	struct {
461 		union {
462 			u_int8_t        __mpriv8[16];
463 			u_int16_t       __mpriv16[8];
464 			struct {
465 				union {
466 					u_int8_t        __val8[4];
467 					u_int16_t       __val16[2];
468 					u_int32_t       __val32;
469 				} __mpriv32_u;
470 			}               __mpriv32[4];
471 			u_int64_t       __mpriv64[2];
472 		} __mpriv_u;
473 	} pkt_mpriv __attribute__((aligned(4)));
474 #define pkt_mpriv_hash  pkt_mpriv.__mpriv_u.__mpriv32[0].__mpriv32_u.__val32
475 #define pkt_mpriv_flags pkt_mpriv.__mpriv_u.__mpriv32[1].__mpriv32_u.__val32
476 #define pkt_mpriv_srcid pkt_mpriv.__mpriv_u.__mpriv32[2].__mpriv32_u.__val32
477 #define pkt_mpriv_fidx  pkt_mpriv.__mpriv_u.__mpriv32[3].__mpriv32_u.__val32
478 
479 	u_int32_t redzone;              /* red zone */
480 	u_int32_t pkt_compl_callbacks;  /* Packet completion callbacks */
481 };
482 
483 /*
484  * Flow data source type.  A data source module is responsible for generating
485  * a unique flow ID and associating it to each data flow as pkt_flowid.
486  * This is required for flow control/advisory, as it allows the output queue
487  * to identify the data source object and inform that it can resume its
488  * transmission (in the event it was flow controlled.)
489  */
490 #define FLOWSRC_INPCB           1       /* flow ID generated by INPCB */
491 #define FLOWSRC_IFNET           2       /* flow ID generated by interface */
492 #define FLOWSRC_PF              3       /* flow ID generated by PF */
493 #define FLOWSRC_CHANNEL         4       /* flow ID generated by channel */
494 
495 /*
496  * FLOWSRC_MPKL_INPUT is not a true flow data source and is used for
497  * multi-layer packet logging. We're usurping the pkt_flowsrc field because
498  * the mbuf packet header ran out of space and pkt_flowsrc is normally
499  * used on output so we assume we can safely overwrite the normal semantic of
500  * the field.
501  * This value is meant to be used on incoming packet from a lower level protocol
502  * to pass information to some upper level protocol. When FLOWSRC_MPKL_INPUT
503  * is set, the following fields are used:
504  * - pkt_proto: the IP protocol ID of the lower level protocol
505  * - pkt_flowid: the identifier of the packet at the lower protocol.
506  * For example ESP would set pkt_proto to IPPROTO_ESP and pkt_flowid to the SPI.
507  */
508 
509 /*
510  * Packet flags.  Unlike m_flags, all packet flags are copied along when
511  * copying m_pkthdr, i.e. no equivalent of M_COPYFLAGS here.  These flags
512  * (and other classifier info) will be cleared during DLIL input.
513  *
514  * Some notes about M_LOOP and PKTF_LOOP:
515  *
516  *    - M_LOOP flag is overloaded, and its use is discouraged.  Historically,
517  *	that flag was used by the KAME implementation for allowing certain
518  *	certain exceptions to be made in the IP6_EXTHDR_CHECK() logic; this
519  *	was originally meant to be set as the packet is looped back to the
520  *	system, and in some circumstances temporarily set in ip6_output().
521  *	Over time, this flag was used by the pre-output routines to indicate
522  *	to the DLIL frameout and output routines, that the packet may be
523  *	looped back to the system under the right conditions.  In addition,
524  *	this is an mbuf flag rather than an mbuf packet header flag.
525  *
526  *    - PKTF_LOOP is an mbuf packet header flag, which is set if and only
527  *	if the packet was looped back to the system.  This flag should be
528  *	used instead for newer code.
529  */
530 #define PKTF_FLOW_ID            0x1     /* pkt has valid flowid value */
531 #define PKTF_FLOW_ADV           0x2     /* pkt triggers local flow advisory */
532 #define PKTF_FLOW_LOCALSRC      0x4     /* pkt is locally originated  */
533 #define PKTF_FLOW_RAWSOCK       0x8     /* pkt locally generated by raw sock */
534 #define PKTF_PRIO_PRIVILEGED    0x10    /* packet priority is privileged */
535 #define PKTF_PROXY_DST          0x20    /* processed but not locally destined */
536 #define PKTF_INET_RESOLVE       0x40    /* IPv4 resolver packet */
537 #define PKTF_INET6_RESOLVE      0x80    /* IPv6 resolver packet */
538 #define PKTF_RESOLVE_RTR        0x100   /* pkt is for resolving router */
539 #define PKTF_SKIP_PKTAP         0x200   /* pkt has already passed through pktap */
540 #define PKTF_WAKE_PKT           0x400   /* packet caused system to wake from sleep */
541 #define PKTF_MPTCP              0x800   /* TCP with MPTCP metadata */
542 #define PKTF_MPSO               0x1000  /* MPTCP socket meta data */
543 #define PKTF_LOOP               0x2000  /* loopbacked packet */
544 #define PKTF_IFAINFO            0x4000  /* pkt has valid interface addr info */
545 #define PKTF_SO_BACKGROUND      0x8000  /* data is from background source */
546 #define PKTF_FORWARDED          0x10000 /* pkt was forwarded from another i/f */
547 #define PKTF_PRIV_GUARDED       0x20000 /* pkt_mpriv area guard enabled */
548 #define PKTF_KEEPALIVE          0x40000 /* pkt is kernel-generated keepalive */
549 #define PKTF_SO_REALTIME        0x80000 /* data is realtime traffic */
550 #define PKTF_VALID_UNSENT_DATA  0x100000 /* unsent data is valid */
551 #define PKTF_TCP_REXMT          0x200000 /* packet is TCP retransmission */
552 #define PKTF_REASSEMBLED        0x400000 /* Packet was reassembled */
553 #define PKTF_TX_COMPL_TS_REQ    0x800000 /* tx completion timestamp requested */
554 #define PKTF_TS_VALID           0x1000000 /* pkt timestamp is valid */
555 #define PKTF_DRIVER_MTAG        0x2000000 /* driver mbuf tags fields inited */
556 #define PKTF_NEW_FLOW           0x4000000 /* Data from a new flow */
557 #define PKTF_START_SEQ          0x8000000 /* valid start sequence */
558 #define PKTF_LAST_PKT           0x10000000 /* last packet in the flow */
559 #define PKTF_MPTCP_REINJ        0x20000000 /* Packet has been reinjected for MPTCP */
560 #define PKTF_MPTCP_DFIN         0x40000000 /* Packet is a data-fin */
561 #define PKTF_HBH_CHKED          0x80000000 /* HBH option is checked */
562 
563 #define PKTF_EXT_OUTPUT_SCOPE   0x1     /* outgoing packet has ipv6 address scope id */
564 #define PKTF_EXT_L4S            0x2     /* pkts is from a L4S connection */
565 
566 #define PKT_CRUMB_TS_COMP_REQ   0x0001 /* timestamp completion requested */
567 #define PKT_CRUMB_TS_COMP_CB    0x0002 /* timestamp callback called */
568 #define PKT_CRUMB_DLIL_OUTPUT   0x0004 /* dlil_output called */
569 #define PKT_CRUMB_FLOW_TX       0x0008 /* dp_flow_tx_process called */
570 #define PKT_CRUMB_FQ_ENQUEUE    0x0010 /* fq_enqueue called */
571 #define PKT_CRUMB_FQ_DEQUEUE    0x0020 /* fq_dequeue called */
572 #define PKT_CRUMB_SK_PKT_COPY   0x0040 /* copy from mbuf to skywalk packet */
573 #define PKT_CRUMB_TCP_OUTPUT    0x0080
574 #define PKT_CRUMB_UDP_OUTPUT    0x0100
575 #define PKT_CRUMB_SOSEND        0x0200
576 #define PKT_CRUMB_DLIL_INPUT    0x0400
577 #define PKT_CRUMB_IP_INPUT      0x0800
578 #define PKT_CRUMB_TCP_INPUT     0x1000
579 #define PKT_CRUMB_UDP_INPUT     0x2000
580 
581 /* flags related to flow control/advisory and identification */
582 #define PKTF_FLOW_MASK  \
583 	(PKTF_FLOW_ID | PKTF_FLOW_ADV | PKTF_FLOW_LOCALSRC | PKTF_FLOW_RAWSOCK)
584 
585 /*
586  * Description of external storage mapped into mbuf, valid only if M_EXT set.
587  */
588 typedef void (*m_ext_free_func_t)(caddr_t, u_int, caddr_t);
589 struct m_ext {
590 	caddr_t ext_buf;                /* start of buffer */
591 	m_ext_free_func_t ext_free;     /* free routine if not the usual */
592 	u_int   ext_size;               /* size of buffer, for ext_free */
593 	caddr_t ext_arg;                /* additional ext_free argument */
594 	struct ext_ref {
595 		struct mbuf *paired;
596 		u_int16_t minref;
597 		u_int16_t refcnt;
598 		u_int16_t prefcnt;
599 		u_int16_t flags;
600 		u_int32_t priv;
601 		uintptr_t ext_token;
602 	} *ext_refflags;
603 };
604 
605 /* define m_ext to a type since it gets redefined below */
606 typedef struct m_ext _m_ext_t;
607 
608 /*
609  * The mbuf object
610  */
611 struct mbuf {
612 	struct m_hdr m_hdr;
613 	union {
614 		struct {
615 			struct pkthdr MH_pkthdr;        /* M_PKTHDR set */
616 			union {
617 				struct m_ext MH_ext;    /* M_EXT set */
618 				char    MH_databuf[_MHLEN];
619 			} MH_dat;
620 		} MH;
621 		char    M_databuf[_MLEN];               /* !M_PKTHDR, !M_EXT */
622 	} M_dat;
623 };
624 
625 #define m_next          m_hdr.mh_next
626 #define m_len           m_hdr.mh_len
627 #define m_data          m_hdr.mh_data
628 #define m_type          m_hdr.mh_type
629 #define m_flags         m_hdr.mh_flags
630 #define m_nextpkt       m_hdr.mh_nextpkt
631 #define m_act           m_nextpkt
632 #define m_pkthdr        M_dat.MH.MH_pkthdr
633 #define m_ext           M_dat.MH.MH_dat.MH_ext
634 #define m_pktdat        M_dat.MH.MH_dat.MH_databuf
635 #define m_dat           M_dat.M_databuf
636 #define m_pktlen(_m)    ((_m)->m_pkthdr.len)
637 #define m_pftag(_m)     (&(_m)->m_pkthdr.builtin_mtag._net_mtag._pf_mtag)
638 #define m_necptag(_m)   (&(_m)->m_pkthdr.builtin_mtag._net_mtag._necp_mtag)
639 
640 /* mbuf flags (private) */
641 #define M_EXT           0x0001  /* has associated external storage */
642 #define M_PKTHDR        0x0002  /* start of record */
643 #define M_EOR           0x0004  /* end of record */
644 #define M_PROTO1        0x0008  /* protocol-specific */
645 #define M_PROTO2        0x0010  /* protocol-specific */
646 #define M_PROTO3        0x0020  /* protocol-specific */
647 #define M_LOOP          0x0040  /* packet is looped back (also see PKTF_LOOP) */
648 #define M_PROTO5        0x0080  /* protocol-specific */
649 
650 /* mbuf pkthdr flags, also in m_flags (private) */
651 #define M_BCAST         0x0100  /* send/received as link-level broadcast */
652 #define M_MCAST         0x0200  /* send/received as link-level multicast */
653 #define M_FRAG          0x0400  /* packet is a fragment of a larger packet */
654 #define M_FIRSTFRAG     0x0800  /* packet is first fragment */
655 #define M_LASTFRAG      0x1000  /* packet is last fragment */
656 #define M_PROMISC       0x2000  /* packet is promiscuous (shouldn't go to stack) */
657 #define M_HASFCS        0x4000  /* packet has FCS */
658 #define M_TAGHDR        0x8000  /* m_tag hdr structure at top of mbuf data */
659 
660 /*
661  * Flags to purge when crossing layers.
662  */
663 #define M_PROTOFLAGS \
664 	(M_PROTO1|M_PROTO2|M_PROTO3|M_PROTO5)
665 
666 /* flags copied when copying m_pkthdr */
667 #define M_COPYFLAGS                                                     \
668 	(M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 |                    \
669 	M_LOOP|M_PROTO5|M_BCAST|M_MCAST|M_FRAG |                        \
670 	M_FIRSTFRAG|M_LASTFRAG|M_PROMISC|M_HASFCS)
671 
672 /* flags indicating hw checksum support and sw checksum requirements */
673 #define CSUM_IP                 0x0001          /* will csum IP */
674 #define CSUM_TCP                0x0002          /* will csum TCP */
675 #define CSUM_UDP                0x0004          /* will csum UDP */
676 #define CSUM_IP_FRAGS           0x0008          /* will csum IP fragments */
677 #define CSUM_FRAGMENT           0x0010          /* will do IP fragmentation */
678 #define CSUM_TCPIPV6            0x0020          /* will csum TCP for IPv6 */
679 #define CSUM_UDPIPV6            0x0040          /* will csum UDP for IPv6 */
680 #define CSUM_FRAGMENT_IPV6      0x0080          /* will do IPv6 fragmentation */
681 
682 #define CSUM_IP_CHECKED         0x0100          /* did csum IP */
683 #define CSUM_IP_VALID           0x0200          /*   ... the csum is valid */
684 #define CSUM_DATA_VALID         0x0400          /* csum_data field is valid */
685 #define CSUM_PSEUDO_HDR         0x0800          /* csum_data has pseudo hdr */
686 #define CSUM_PARTIAL            0x1000          /* simple Sum16 computation */
687 #define CSUM_ZERO_INVERT        0x2000          /* invert 0 to -0 (0xffff) */
688 
689 #define CSUM_DELAY_DATA         (CSUM_TCP | CSUM_UDP)
690 #define CSUM_DELAY_IP           (CSUM_IP)       /* IPv4 only: no IPv6 IP cksum */
691 #define CSUM_DELAY_IPV6_DATA    (CSUM_TCPIPV6 | CSUM_UDPIPV6)
692 #define CSUM_DATA_IPV6_VALID    CSUM_DATA_VALID /* csum_data field is valid */
693 
694 #define CSUM_TX_FLAGS                                                   \
695 	(CSUM_DELAY_IP | CSUM_DELAY_DATA | CSUM_DELAY_IPV6_DATA |       \
696 	CSUM_DATA_VALID | CSUM_PARTIAL | CSUM_ZERO_INVERT)
697 
698 #define CSUM_RX_FULL_FLAGS                                              \
699 	(CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_PSEUDO_HDR |            \
700 	CSUM_DATA_VALID)
701 
702 #define CSUM_RX_FLAGS                                                   \
703 	(CSUM_RX_FULL_FLAGS | CSUM_PARTIAL)
704 
705 
706 
707 /*
708  * Note: see also IF_HWASSIST_CSUM defined in <net/if_var.h>
709  */
710 
711 /* VLAN tag present */
712 #define CSUM_VLAN_TAG_VALID     0x00010000      /* vlan_tag field is valid */
713 
714 /* checksum start adjustment has been done */
715 #define CSUM_ADJUST_DONE        0x00020000
716 
717 /* VLAN encapsulation present */
718 #define CSUM_VLAN_ENCAP_PRESENT    0x00040000      /* mbuf has vlan encapsulation */
719 
720 /* TCP Segment Offloading requested on this mbuf */
721 #define CSUM_TSO_IPV4           0x00100000      /* This mbuf needs to be segmented by the NIC */
722 #define CSUM_TSO_IPV6           0x00200000      /* This mbuf needs to be segmented by the NIC */
723 
724 #define TSO_IPV4_OK(_ifp, _m)                                           \
725     (((_ifp)->if_hwassist & IFNET_TSO_IPV4) &&                          \
726     ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4))                        \
727 
728 #define TSO_IPV4_NOTOK(_ifp, _m)                                        \
729     (!((_ifp)->if_hwassist & IFNET_TSO_IPV4) &&                         \
730     ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV4))                        \
731 
732 #define TSO_IPV6_OK(_ifp, _m)                                           \
733     (((_ifp)->if_hwassist & IFNET_TSO_IPV6) &&                          \
734     ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6))                        \
735 
736 #define TSO_IPV6_NOTOK(_ifp, _m)                                        \
737     (!((_ifp)->if_hwassist & IFNET_TSO_IPV6) &&                         \
738     ((_m)->m_pkthdr.csum_flags & CSUM_TSO_IPV6))                        \
739 
740 #endif /* XNU_KERNEL_PRIVATE */
741 
742 /* mbuf types */
743 #define MT_FREE         0       /* should be on free list */
744 #define MT_DATA         1       /* dynamic (data) allocation */
745 #define MT_HEADER       2       /* packet header */
746 #define MT_SOCKET       3       /* socket structure */
747 #define MT_PCB          4       /* protocol control block */
748 #define MT_RTABLE       5       /* routing tables */
749 #define MT_HTABLE       6       /* IMP host tables */
750 #define MT_ATABLE       7       /* address resolution tables */
751 #define MT_SONAME       8       /* socket name */
752 #define MT_SOOPTS       10      /* socket options */
753 #define MT_FTABLE       11      /* fragment reassembly header */
754 #define MT_RIGHTS       12      /* access rights */
755 #define MT_IFADDR       13      /* interface address */
756 #define MT_CONTROL      14      /* extra-data protocol message */
757 #define MT_OOBDATA      15      /* expedited data  */
758 #define MT_TAG          16      /* volatile metadata associated to pkts */
759 #define MT_MAX          32      /* enough? */
760 
761 #ifdef XNU_KERNEL_PRIVATE
762 /*
763  * mbuf allocation/deallocation macros:
764  *
765  *	MGET(struct mbuf *m, int how, int type)
766  * allocates an mbuf and initializes it to contain internal data.
767  *
768  *	MGETHDR(struct mbuf *m, int how, int type)
769  * allocates an mbuf and initializes it to contain a packet header
770  * and internal data.
771  */
772 
773 #if 1
774 #define MCHECK(m) m_mcheck(m)
775 #else
776 #define MCHECK(m)
777 #endif
778 
779 #define MGET(m, how, type) ((m) = m_get((how), (type)))
780 
781 #define MGETHDR(m, how, type)   ((m) = m_gethdr((how), (type)))
782 
783 /*
784  * Mbuf cluster macros.
785  * MCLALLOC(caddr_t p, int how) allocates an mbuf cluster.
786  * MCLGET adds such clusters to a normal mbuf;
787  * the flag M_EXT is set upon success.
788  * MCLFREE releases a reference to a cluster allocated by MCLALLOC,
789  * freeing the cluster if the reference count has reached 0.
790  *
791  * Normal mbuf clusters are normally treated as character arrays
792  * after allocation, but use the first word of the buffer as a free list
793  * pointer while on the free list.
794  */
795 union mcluster {
796 	union   mcluster *mcl_next;
797 	char    mcl_buf[MCLBYTES];
798 };
799 
800 #define MCLALLOC(p, how)        ((p) = m_mclalloc(how))
801 
802 #define MCLFREE(p)              m_mclfree(p)
803 
804 #define MCLGET(m, how)          ((m) = m_mclget(m, how))
805 
806 /*
807  * Mbuf big cluster
808  */
809 union mbigcluster {
810 	union mbigcluster       *mbc_next;
811 	char                    mbc_buf[MBIGCLBYTES];
812 };
813 
814 /*
815  * Mbuf jumbo cluster
816  */
817 union m16kcluster {
818 	union m16kcluster       *m16kcl_next;
819 	char                    m16kcl_buf[M16KCLBYTES];
820 };
821 
822 #define MCLHASREFERENCE(m)      m_mclhasreference(m)
823 
824 /*
825  * MFREE(struct mbuf *m, struct mbuf *n)
826  * Free a single mbuf and associated external storage.
827  * Place the successor, if any, in n.
828  */
829 
830 #define MFREE(m, n) ((n) = m_free(m))
831 
832 /*
833  * Copy mbuf pkthdr from from to to.
834  * from must have M_PKTHDR set, and to must be empty.
835  * aux pointer will be moved to `to'.
836  */
837 #define M_COPY_PKTHDR(to, from)         m_copy_pkthdr(to, from)
838 
839 #define M_COPY_PFTAG(to, from)          m_copy_pftag(to, from)
840 
841 #define M_COPY_NECPTAG(to, from)        m_copy_necptag(to, from)
842 
843 #define M_COPY_CLASSIFIER(to, from)     m_copy_classifier(to, from)
844 
845 /*
846  * Evaluate TRUE if it's safe to write to the mbuf m's data region (this can
847  * be both the local data payload, or an external buffer area, depending on
848  * whether M_EXT is set).
849  */
850 #define M_WRITABLE(m)   (((m)->m_flags & M_EXT) == 0 || !MCLHASREFERENCE(m))
851 
852 /*
853  * These macros are mapped to the appropriate KPIs, so that private code
854  * can be simply recompiled in order to be forward-compatible with future
855  * changes toward the struture sizes.
856  */
857 #ifdef XNU_KERNEL_PRIVATE
858 #define MLEN            _MLEN
859 #define MHLEN           _MHLEN
860 #define MINCLSIZE       (MLEN + MHLEN)
861 #else
862 #define MLEN            mbuf_get_mlen()         /* normal mbuf data len */
863 #define MHLEN           mbuf_get_mhlen()        /* data len in an mbuf w/pkthdr */
864 #define MINCLSIZE       mbuf_get_minclsize()    /* cluster usage threshold */
865 #endif
866 /*
867  * Return the address of the start of the buffer associated with an mbuf,
868  * handling external storage, packet-header mbufs, and regular data mbufs.
869  */
870 #define M_START(m)                                                      \
871 	(((m)->m_flags & M_EXT) ? (m)->m_ext.ext_buf :                  \
872 	 ((m)->m_flags & M_PKTHDR) ? &(m)->m_pktdat[0] :                \
873 	 &(m)->m_dat[0])
874 
875 /*
876  * Return the size of the buffer associated with an mbuf, handling external
877  * storage, packet-header mbufs, and regular data mbufs.
878  */
879 #define M_SIZE(m)                                                       \
880 	(((m)->m_flags & M_EXT) ? (m)->m_ext.ext_size :                 \
881 	 ((m)->m_flags & M_PKTHDR) ? MHLEN :                            \
882 	 MLEN)
883 
884 #define M_ALIGN(m, len)         m_align(m, len)
885 #define MH_ALIGN(m, len)        m_align(m, len)
886 #define MEXT_ALIGN(m, len)      m_align(m, len)
887 
888 /*
889  * Compute the amount of space available before the current start of data in
890  * an mbuf.
891  *
892  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
893  * of checking writability of the mbuf data area rests solely with the caller.
894  */
895 #define M_LEADINGSPACE(m)                                               \
896 	(M_WRITABLE(m) ? ((m)->m_data - M_START(m)) : 0)
897 
898 /*
899  * Compute the amount of space available after the end of data in an mbuf.
900  *
901  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
902  * of checking writability of the mbuf data area rests solely with the caller.
903  */
904 #define M_TRAILINGSPACE(m)                                              \
905 	(M_WRITABLE(m) ?                                                \
906 	    ((M_START(m) + M_SIZE(m)) - ((m)->m_data + (m)->m_len)) : 0)
907 
908 /*
909  * Arrange to prepend space of size plen to mbuf m.
910  * If a new mbuf must be allocated, how specifies whether to wait.
911  * If how is M_DONTWAIT and allocation fails, the original mbuf chain
912  * is freed and m is set to NULL.
913  */
914 #define M_PREPEND(m, plen, how, align)  \
915     ((m) = m_prepend_2((m), (plen), (how), (align)))
916 
917 /* change mbuf to new type */
918 #define MCHTYPE(m, t)           m_mchtype(m, t)
919 
920 /* compatiblity with 4.3 */
921 #define m_copy(m, o, l)         m_copym((m), (o), (l), M_DONTWAIT)
922 
923 #define MBSHIFT         20                              /* 1MB */
924 #define MBSIZE          (1 << MBSHIFT)
925 #define GBSHIFT         30                              /* 1GB */
926 #define GBSIZE          (1 << GBSHIFT)
927 
928 /*
929  * M_STRUCT_GET ensures that intermediate protocol header (from "off" to
930  * "off+len") is located in single mbuf, on contiguous memory region.
931  * The pointer to the region will be returned to pointer variable "val",
932  * with type "typ".
933  *
934  * M_STRUCT_GET0 does the same, except that it aligns the structure at
935  * very top of mbuf.  GET0 is likely to make memory copy than GET.
936  */
937 #define M_STRUCT_GET(val, typ, m, off, len)                             \
938 do {                                                                    \
939 	struct mbuf *t;                                                 \
940 	int tmp;                                                        \
941                                                                         \
942 	if ((m)->m_len >= (off) + (len)) {                              \
943 	        (val) = (typ)(mtod((m), caddr_t) + (off));              \
944 	} else {                                                        \
945 	        t = m_pulldown((m), (off), (len), &tmp);                \
946 	        if (t != NULL) {                                        \
947 	                if (t->m_len < tmp + (len))                     \
948 	                        panic("m_pulldown malfunction");        \
949 	                (val) = (typ)(mtod(t, caddr_t) + tmp);          \
950 	        } else {                                                \
951 	                (val) = (typ)NULL;                              \
952 	                (m) = NULL;                                     \
953 	        }                                                       \
954 	}                                                               \
955 } while (0)
956 
957 #define M_STRUCT_GET0(val, typ, m, off, len)                            \
958 do {                                                                    \
959 	struct mbuf *t;                                                 \
960                                                                         \
961 	if ((off) == 0 && ((m)->m_len >= (len))) {                      \
962 	        (val) = (typ)(void *)mtod(m, caddr_t);                  \
963 	} else {                                                        \
964 	        t = m_pulldown((m), (off), (len), NULL);                \
965 	        if (t != NULL) {                                        \
966 	                if (t->m_len < (len))                           \
967 	                        panic("m_pulldown malfunction");        \
968 	                (val) = (typ)(void *)mtod(t, caddr_t);          \
969 	        } else {                                                \
970 	                (val) = (typ)NULL;                              \
971 	                (m) = NULL;                                     \
972 	        }                                                       \
973 	}                                                               \
974 } while (0)
975 
976 #define MBUF_INPUT_CHECK(m, rcvif)                                      \
977 do {                                                                    \
978 	if (!(m->m_flags & MBUF_PKTHDR) ||                              \
979 	    m->m_len < 0 ||                                             \
980 	    m->m_len > ((njcl > 0) ? njclbytes : MBIGCLBYTES) ||        \
981 	    m->m_type == MT_FREE ||                                     \
982 	    ((m->m_flags & M_EXT) != 0 && m->m_ext.ext_buf == NULL)) {  \
983 	        panic_plain("Failed mbuf validity check: mbuf %p len %d "  \
984 	            "type %d flags 0x%x data %p rcvif %s ifflags 0x%x",  \
985 	            m, m->m_len, m->m_type, m->m_flags,                    \
986 	            ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : m->m_data), \
987 	            if_name(rcvif),                                     \
988 	            (rcvif->if_flags & 0xffff));                        \
989 	}                                                               \
990 } while (0)
991 
992 /*
993  * Simple mbuf queueing system
994  *
995  * This is basically a SIMPLEQ adapted to mbuf use (i.e. using
996  * m_nextpkt instead of field.sqe_next).
997  *
998  * m_next is ignored, so queueing chains of mbufs is possible
999  */
1000 #define MBUFQ_HEAD(name)                                        \
1001 struct name {                                                   \
1002 	struct mbuf *mq_first;  /* first packet */              \
1003 	struct mbuf **mq_last;  /* addr of last next packet */  \
1004 }
1005 
1006 #define MBUFQ_INIT(q)           do {                            \
1007 	MBUFQ_FIRST(q) = NULL;                                  \
1008 	(q)->mq_last = &MBUFQ_FIRST(q);                         \
1009 } while (0)
1010 
1011 #define MBUFQ_PREPEND(q, m)     do {                            \
1012 	if ((MBUFQ_NEXT(m) = MBUFQ_FIRST(q)) == NULL)           \
1013 	        (q)->mq_last = &MBUFQ_NEXT(m);                  \
1014 	MBUFQ_FIRST(q) = (m);                                   \
1015 } while (0)
1016 
1017 #define MBUFQ_ENQUEUE(q, m)     do {                            \
1018 	MBUFQ_NEXT(m) = NULL;                                   \
1019 	*(q)->mq_last = (m);                                    \
1020 	(q)->mq_last = &MBUFQ_NEXT(m);                          \
1021 } while (0)
1022 
1023 #define MBUFQ_ENQUEUE_MULTI(q, m, n)    do {                    \
1024 	MBUFQ_NEXT(n) = NULL;                                   \
1025 	*(q)->mq_last = (m);                                    \
1026 	(q)->mq_last = &MBUFQ_NEXT(n);                          \
1027 } while (0)
1028 
1029 #define MBUFQ_DEQUEUE(q, m)     do {                            \
1030 	if (((m) = MBUFQ_FIRST(q)) != NULL) {                   \
1031 	        if ((MBUFQ_FIRST(q) = MBUFQ_NEXT(m)) == NULL)   \
1032 	                (q)->mq_last = &MBUFQ_FIRST(q);         \
1033 	        else                                            \
1034 	                MBUFQ_NEXT(m) = NULL;                   \
1035 	}                                                       \
1036 } while (0)
1037 
1038 #define MBUFQ_REMOVE(q, m)      do {                            \
1039 	if (MBUFQ_FIRST(q) == (m)) {                            \
1040 	        MBUFQ_DEQUEUE(q, m);                            \
1041 	} else {                                                \
1042 	        struct mbuf *_m = MBUFQ_FIRST(q);               \
1043 	        while (MBUFQ_NEXT(_m) != (m))                   \
1044 	                _m = MBUFQ_NEXT(_m);                    \
1045 	        if ((MBUFQ_NEXT(_m) =                           \
1046 	            MBUFQ_NEXT(MBUFQ_NEXT(_m))) == NULL)        \
1047 	                (q)->mq_last = &MBUFQ_NEXT(_m);         \
1048 	}                                                       \
1049 } while (0)
1050 
1051 #define MBUFQ_DRAIN(q)          do {                            \
1052 	struct mbuf *__m0;                                      \
1053 	while ((__m0 = MBUFQ_FIRST(q)) != NULL) {               \
1054 	        MBUFQ_FIRST(q) = MBUFQ_NEXT(__m0);              \
1055 	        MBUFQ_NEXT(__m0) = NULL;                        \
1056 	        m_freem(__m0);                                  \
1057 	}                                                       \
1058 	(q)->mq_last = &MBUFQ_FIRST(q);                         \
1059 } while (0)
1060 
1061 #define MBUFQ_FOREACH(m, q)                                     \
1062 	for ((m) = MBUFQ_FIRST(q);                              \
1063 	    (m);                                                \
1064 	    (m) = MBUFQ_NEXT(m))
1065 
1066 #define MBUFQ_FOREACH_SAFE(m, q, tvar)                          \
1067 	for ((m) = MBUFQ_FIRST(q);                              \
1068 	    (m) && ((tvar) = MBUFQ_NEXT(m), 1);                 \
1069 	    (m) = (tvar))
1070 
1071 #define MBUFQ_EMPTY(q)          ((q)->mq_first == NULL)
1072 #define MBUFQ_FIRST(q)          ((q)->mq_first)
1073 #define MBUFQ_NEXT(m)           ((m)->m_nextpkt)
1074 /*
1075  * mq_last is initialized to point to mq_first, so check if they're
1076  * equal and return NULL when the list is empty.  Otherwise, we need
1077  * to subtract the offset of MBUQ_NEXT (i.e. m_nextpkt field) to get
1078  * to the base mbuf address to return to caller.
1079  */
1080 #define MBUFQ_LAST(head)                                        \
1081 	(((head)->mq_last == &MBUFQ_FIRST(head)) ? NULL :       \
1082 	((struct mbuf *)(void *)((char *)(head)->mq_last -      \
1083 	     __builtin_offsetof(struct mbuf, m_nextpkt))))
1084 
1085 #if (DEBUG || DEVELOPMENT)
1086 #define MBUFQ_ADD_CRUMB_MULTI(_q, _h, _t, _f) do {              \
1087 	struct mbuf * _saved = (_t)->m_nextpkt;                 \
1088 	struct mbuf * _m;                                       \
1089 	for (_m = (_h); _m != NULL; _m = MBUFQ_NEXT(_m)) {      \
1090 	        m_add_crumb((_m), (_f));                        \
1091 	}                                                       \
1092 	(_t)->m_nextpkt = _saved;                               \
1093 } while (0)
1094 
1095 #define MBUFQ_ADD_CRUMB(_q, _m, _f) do {                \
1096 	m_add_crumb((_m), (_f));                        \
1097 } while (0)
1098 #else
1099 #define MBUFQ_ADD_CRUMB_MULTI(_q, _h, _t, _f)
1100 #define MBUFQ_ADD_CRUMB(_q, _m, _f)
1101 #endif /* (DEBUG || DEVELOPMENT) */
1102 
1103 #endif /* XNU_KERNEL_PRIVATE */
1104 
1105 /*
1106  * Mbuf statistics (legacy).
1107  */
1108 struct mbstat {
1109 	u_int32_t       m_mbufs;        /* mbufs obtained from page pool */
1110 	u_int32_t       m_clusters;     /* clusters obtained from page pool */
1111 	u_int32_t       m_spare;        /* spare field */
1112 	u_int32_t       m_clfree;       /* free clusters */
1113 	u_int32_t       m_drops;        /* times failed to find space */
1114 	u_int32_t       m_wait;         /* times waited for space */
1115 	u_int32_t       m_drain;        /* times drained protocols for space */
1116 	u_short         m_mtypes[256];  /* type specific mbuf allocations */
1117 	u_int32_t       m_mcfail;       /* times m_copym failed */
1118 	u_int32_t       m_mpfail;       /* times m_pullup failed */
1119 	u_int32_t       m_msize;        /* length of an mbuf */
1120 	u_int32_t       m_mclbytes;     /* length of an mbuf cluster */
1121 	u_int32_t       m_minclsize;    /* min length of data to allocate a cluster */
1122 	u_int32_t       m_mlen;         /* length of data in an mbuf */
1123 	u_int32_t       m_mhlen;        /* length of data in a header mbuf */
1124 	u_int32_t       m_bigclusters;  /* clusters obtained from page pool */
1125 	u_int32_t       m_bigclfree;    /* free clusters */
1126 	u_int32_t       m_bigmclbytes;  /* length of an mbuf cluster */
1127 	u_int32_t       m_forcedefunct; /* times we force defunct'ed an app's sockets */
1128 };
1129 
1130 /* Compatibillity with 10.3 */
1131 struct ombstat {
1132 	u_int32_t       m_mbufs;        /* mbufs obtained from page pool */
1133 	u_int32_t       m_clusters;     /* clusters obtained from page pool */
1134 	u_int32_t       m_spare;        /* spare field */
1135 	u_int32_t       m_clfree;       /* free clusters */
1136 	u_int32_t       m_drops;        /* times failed to find space */
1137 	u_int32_t       m_wait;         /* times waited for space */
1138 	u_int32_t       m_drain;        /* times drained protocols for space */
1139 	u_short         m_mtypes[256];  /* type specific mbuf allocations */
1140 	u_int32_t       m_mcfail;       /* times m_copym failed */
1141 	u_int32_t       m_mpfail;       /* times m_pullup failed */
1142 	u_int32_t       m_msize;        /* length of an mbuf */
1143 	u_int32_t       m_mclbytes;     /* length of an mbuf cluster */
1144 	u_int32_t       m_minclsize;    /* min length of data to allocate a cluster */
1145 	u_int32_t       m_mlen;         /* length of data in an mbuf */
1146 	u_int32_t       m_mhlen;        /* length of data in a header mbuf */
1147 };
1148 
1149 /*
1150  * mbuf class statistics.
1151  */
1152 #define MAX_MBUF_CNAME  15
1153 
1154 #if defined(XNU_KERNEL_PRIVATE)
1155 /* For backwards compatibility with 32-bit userland process */
1156 struct omb_class_stat {
1157 	char            mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1158 	u_int32_t       mbcl_size;      /* buffer size */
1159 	u_int32_t       mbcl_total;     /* # of buffers created */
1160 	u_int32_t       mbcl_active;    /* # of active buffers */
1161 	u_int32_t       mbcl_infree;    /* # of available buffers */
1162 	u_int32_t       mbcl_slab_cnt;  /* # of available slabs */
1163 	u_int32_t       mbcl_pad;       /* padding */
1164 	u_int64_t       mbcl_alloc_cnt; /* # of times alloc is called */
1165 	u_int64_t       mbcl_free_cnt;  /* # of times free is called */
1166 	u_int64_t       mbcl_notified;  /* # of notified wakeups */
1167 	u_int64_t       mbcl_purge_cnt; /* # of purges so far */
1168 	u_int64_t       mbcl_fail_cnt;  /* # of allocation failures */
1169 	u_int32_t       mbcl_ctotal;    /* total only for this class */
1170 	u_int32_t       mbcl_release_cnt; /* amount of memory returned */
1171 	/*
1172 	 * Cache layer statistics
1173 	 */
1174 	u_int32_t       mbcl_mc_state;  /* cache state (see below) */
1175 	u_int32_t       mbcl_mc_cached; /* # of cached buffers */
1176 	u_int32_t       mbcl_mc_waiter_cnt;  /* # waiters on the cache */
1177 	u_int32_t       mbcl_mc_wretry_cnt;  /* # of wait retries */
1178 	u_int32_t       mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
1179 	u_int32_t       mbcl_peak_reported; /* last usage peak reported */
1180 	u_int32_t       mbcl_reserved[7];    /* for future use */
1181 	u_int32_t       mbcl_pad2;       /* padding */
1182 } __attribute__((__packed__));
1183 #endif /* XNU_KERNEL_PRIVATE */
1184 
1185 typedef struct mb_class_stat {
1186 	char            mbcl_cname[MAX_MBUF_CNAME + 1]; /* class name */
1187 	u_int32_t       mbcl_size;      /* buffer size */
1188 	u_int32_t       mbcl_total;     /* # of buffers created */
1189 	u_int32_t       mbcl_active;    /* # of active buffers */
1190 	u_int32_t       mbcl_infree;    /* # of available buffers */
1191 	u_int32_t       mbcl_slab_cnt;  /* # of available slabs */
1192 #if defined(KERNEL) || defined(__LP64__)
1193 	u_int32_t       mbcl_pad;       /* padding */
1194 #endif /* KERNEL || __LP64__ */
1195 	u_int64_t       mbcl_alloc_cnt; /* # of times alloc is called */
1196 	u_int64_t       mbcl_free_cnt;  /* # of times free is called */
1197 	u_int64_t       mbcl_notified;  /* # of notified wakeups */
1198 	u_int64_t       mbcl_purge_cnt; /* # of purges so far */
1199 	u_int64_t       mbcl_fail_cnt;  /* # of allocation failures */
1200 	u_int32_t       mbcl_ctotal;    /* total only for this class */
1201 	u_int32_t       mbcl_release_cnt; /* amount of memory returned */
1202 	/*
1203 	 * Cache layer statistics
1204 	 */
1205 	u_int32_t       mbcl_mc_state;  /* cache state (see below) */
1206 	u_int32_t       mbcl_mc_cached; /* # of cached buffers */
1207 	u_int32_t       mbcl_mc_waiter_cnt;  /* # waiters on the cache */
1208 	u_int32_t       mbcl_mc_wretry_cnt;  /* # of wait retries */
1209 	u_int32_t       mbcl_mc_nwretry_cnt; /* # of no-wait retry attempts */
1210 	u_int32_t       mbcl_peak_reported; /* last usage peak reported */
1211 	u_int32_t       mbcl_reserved[7];    /* for future use */
1212 } mb_class_stat_t;
1213 
1214 #define MCS_DISABLED    0       /* cache is permanently disabled */
1215 #define MCS_ONLINE      1       /* cache is online */
1216 #define MCS_PURGING     2       /* cache is being purged */
1217 #define MCS_OFFLINE     3       /* cache is offline (resizing) */
1218 
1219 #if defined(XNU_KERNEL_PRIVATE)
1220 /* For backwards compatibility with 32-bit userland process */
1221 struct omb_stat {
1222 	u_int32_t               mbs_cnt;        /* number of classes */
1223 	u_int32_t               mbs_pad;        /* padding */
1224 	struct omb_class_stat   mbs_class[1];   /* class array */
1225 } __attribute__((__packed__));
1226 #endif /* XNU_KERNEL_PRIVATE */
1227 
1228 typedef struct mb_stat {
1229 	u_int32_t       mbs_cnt;        /* number of classes */
1230 #if defined(KERNEL) || defined(__LP64__)
1231 	u_int32_t       mbs_pad;        /* padding */
1232 #endif /* KERNEL || __LP64__ */
1233 	mb_class_stat_t mbs_class[1];   /* class array */
1234 } mb_stat_t;
1235 
1236 #ifdef PRIVATE
1237 #define MLEAK_STACK_DEPTH       16      /* Max PC stack depth */
1238 
1239 typedef struct mleak_trace_stat {
1240 	u_int64_t       mltr_collisions;
1241 	u_int64_t       mltr_hitcount;
1242 	u_int64_t       mltr_allocs;
1243 	u_int64_t       mltr_depth;
1244 	u_int64_t       mltr_addr[MLEAK_STACK_DEPTH];
1245 } mleak_trace_stat_t;
1246 
1247 typedef struct mleak_stat {
1248 	u_int32_t               ml_isaddr64;    /* 64-bit KVA? */
1249 	u_int32_t               ml_cnt;         /* number of traces */
1250 	mleak_trace_stat_t      ml_trace[1];    /* trace array */
1251 } mleak_stat_t;
1252 
1253 struct mleak_table {
1254 	u_int32_t mleak_capture;        /* sampling capture counter */
1255 	u_int32_t mleak_sample_factor;  /* sample factor */
1256 
1257 	/* Times two active records want to occupy the same spot */
1258 	u_int64_t alloc_collisions;
1259 	u_int64_t trace_collisions;
1260 
1261 	/* Times new record lands on spot previously occupied by freed alloc */
1262 	u_int64_t alloc_overwrites;
1263 	u_int64_t trace_overwrites;
1264 
1265 	/* Times a new alloc or trace is put into the hash table */
1266 	u_int64_t alloc_recorded;
1267 	u_int64_t trace_recorded;
1268 
1269 	/* Total number of outstanding allocs */
1270 	u_int64_t outstanding_allocs;
1271 
1272 	/* Times mleak_log returned false because couldn't acquire the lock */
1273 	u_int64_t total_conflicts;
1274 };
1275 #endif /* PRIVATE */
1276 
1277 #ifdef KERNEL_PRIVATE
1278 __BEGIN_DECLS
1279 
1280 /*
1281  * Exported (private)
1282  */
1283 
1284 extern struct mbstat mbstat;                    /* statistics */
1285 
1286 __END_DECLS
1287 #endif /* KERNEL_PRIVATE */
1288 
1289 #ifdef XNU_KERNEL_PRIVATE
1290 __BEGIN_DECLS
1291 
1292 /*
1293  * Not exported (xnu private)
1294  */
1295 
1296 /* flags to m_get/MGET */
1297 /* Need to include malloc.h to get right options for malloc  */
1298 #include        <sys/malloc.h>
1299 
1300 struct mbuf;
1301 
1302 /* length to m_copy to copy all */
1303 #define M_COPYALL       1000000000
1304 
1305 #define M_DONTWAIT      M_NOWAIT
1306 #define M_WAIT          M_WAITOK
1307 
1308 /* modes for m_copym and variants */
1309 #define M_COPYM_NOOP_HDR        0       /* don't copy/move pkthdr contents */
1310 #define M_COPYM_COPY_HDR        1       /* copy pkthdr from old to new */
1311 #define M_COPYM_MOVE_HDR        2       /* move pkthdr from old to new */
1312 #define M_COPYM_MUST_COPY_HDR   3       /* MUST copy pkthdr from old to new */
1313 #define M_COPYM_MUST_MOVE_HDR   4       /* MUST move pkthdr from old to new */
1314 
1315 extern void m_freem(struct mbuf *) __XNU_INTERNAL(m_freem);
1316 extern u_int64_t mcl_to_paddr(char *);
1317 extern void m_adj(struct mbuf *, int);
1318 extern void m_cat(struct mbuf *, struct mbuf *);
1319 extern void m_copydata(struct mbuf *, int, int, void *);
1320 extern struct mbuf *m_copym(struct mbuf *, int, int, int);
1321 extern struct mbuf *m_copym_mode(struct mbuf *, int, int, int, uint32_t);
1322 extern struct mbuf *m_get(int, int);
1323 extern struct mbuf *m_gethdr(int, int);
1324 extern struct mbuf *m_getpacket(void);
1325 extern struct mbuf *m_getpackets(int, int, int);
1326 extern struct mbuf *m_mclget(struct mbuf *, int);
1327 extern void *__unsafe_indexable m_mtod(struct mbuf *);
1328 extern struct mbuf *m_prepend_2(struct mbuf *, int, int, int);
1329 extern struct mbuf *m_pullup(struct mbuf *, int);
1330 extern struct mbuf *m_split(struct mbuf *, int, int);
1331 extern void m_mclfree(caddr_t p);
1332 extern int mbuf_get_class(struct mbuf *m);
1333 extern bool mbuf_class_under_pressure(struct mbuf *m);
1334 
1335 static inline void *__header_indexable
m_mtod_indexable(struct mbuf * m)1336 m_mtod_indexable(struct mbuf *m)
1337 {
1338 	return __unsafe_forge_bidi_indexable(void *, m_mtod(m), m->m_len);
1339 }
1340 
1341 /*
1342  * On platforms which require strict alignment (currently for anything but
1343  * i386 or x86_64), this macro checks whether the data pointer of an mbuf
1344  * is 32-bit aligned (this is the expected minimum alignment for protocol
1345  * headers), and assert otherwise.
1346  */
1347 #if defined(__i386__) || defined(__x86_64__)
1348 #define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m)
1349 #else /* !__i386__ && !__x86_64__ */
1350 #define MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(_m) do {                    \
1351 	if (!IS_P2ALIGNED((_m)->m_data, sizeof (u_int32_t))) {          \
1352 	        if (((_m)->m_flags & M_PKTHDR) &&                       \
1353 	            (_m)->m_pkthdr.rcvif != NULL) {                     \
1354 	                panic_plain("\n%s: mbuf %p data ptr %p is not " \
1355 	                    "32-bit aligned [%s: alignerrs=%lld]\n",    \
1356 	                    __func__, (_m), (_m)->m_data,               \
1357 	                    if_name((_m)->m_pkthdr.rcvif),              \
1358 	                    (_m)->m_pkthdr.rcvif->if_alignerrs);        \
1359 	        } else {                                                \
1360 	                panic_plain("\n%s: mbuf %p data ptr %p is not " \
1361 	                    "32-bit aligned\n",                         \
1362 	                    __func__, (_m), (_m)->m_data);              \
1363 	        }                                                       \
1364 	}                                                               \
1365 } while (0)
1366 #endif /* !__i386__ && !__x86_64__ */
1367 
1368 /* Maximum number of MBUF_SC values (excluding MBUF_SC_UNSPEC) */
1369 #define MBUF_SC_MAX_CLASSES     10
1370 
1371 /*
1372  * These conversion macros rely on the corresponding MBUF_SC and
1373  * MBUF_TC values in order to establish the following mapping:
1374  *
1375  *	MBUF_SC_BK_SYS	] ==>	MBUF_TC_BK
1376  *	MBUF_SC_BK	]
1377  *
1378  *	MBUF_SC_BE	] ==>	MBUF_TC_BE
1379  *	MBUF_SC_RD	]
1380  *	MBUF_SC_OAM	]
1381  *
1382  *	MBUF_SC_AV	] ==>	MBUF_TC_VI
1383  *	MBUF_SC_RV	]
1384  *	MBUF_SC_VI	]
1385  *	MBUF_SC_SIG	]
1386  *
1387  *	MBUF_SC_VO	] ==>	MBUF_TC_VO
1388  *	MBUF_SC_CTL	]
1389  *
1390  * The values assigned to each service class allows for a fast mapping to
1391  * the corresponding MBUF_TC traffic class values, as well as to retrieve the
1392  * assigned index; therefore care must be taken when comparing against these
1393  * values.  Use the corresponding class and index macros to retrieve the
1394  * corresponding portion, and never assume that a higher class corresponds
1395  * to a higher index.
1396  */
1397 #define MBUF_SCVAL(x)           ((x) & 0xffff)
1398 #define MBUF_SCIDX(x)           ((((x) >> 16) & 0xff) >> 3)
1399 #define MBUF_SC2TC(_sc)         (MBUF_SCVAL(_sc) >> 7)
1400 #define MBUF_TC2SCVAL(_tc)      ((_tc) << 7)
1401 #define IS_MBUF_SC_BACKGROUND(_sc) (((_sc) == MBUF_SC_BK_SYS) || \
1402 	((_sc) == MBUF_SC_BK))
1403 #define IS_MBUF_SC_REALTIME(_sc)        ((_sc) >= MBUF_SC_AV && (_sc) <= MBUF_SC_VO)
1404 #define IS_MBUF_SC_BESTEFFORT(_sc)      ((_sc) == MBUF_SC_BE || \
1405     (_sc) == MBUF_SC_RD || (_sc) == MBUF_SC_OAM)
1406 
1407 #define SCIDX_BK_SYS            MBUF_SCIDX(MBUF_SC_BK_SYS)
1408 #define SCIDX_BK                MBUF_SCIDX(MBUF_SC_BK)
1409 #define SCIDX_BE                MBUF_SCIDX(MBUF_SC_BE)
1410 #define SCIDX_RD                MBUF_SCIDX(MBUF_SC_RD)
1411 #define SCIDX_OAM               MBUF_SCIDX(MBUF_SC_OAM)
1412 #define SCIDX_AV                MBUF_SCIDX(MBUF_SC_AV)
1413 #define SCIDX_RV                MBUF_SCIDX(MBUF_SC_RV)
1414 #define SCIDX_VI                MBUF_SCIDX(MBUF_SC_VI)
1415 #define SCIDX_SIG               MBUF_SCIDX(MBUF_SC_SIG)
1416 #define SCIDX_VO                MBUF_SCIDX(MBUF_SC_VO)
1417 #define SCIDX_CTL               MBUF_SCIDX(MBUF_SC_CTL)
1418 
1419 #define SCVAL_BK_SYS            MBUF_SCVAL(MBUF_SC_BK_SYS)
1420 #define SCVAL_BK                MBUF_SCVAL(MBUF_SC_BK)
1421 #define SCVAL_BE                MBUF_SCVAL(MBUF_SC_BE)
1422 #define SCVAL_RD                MBUF_SCVAL(MBUF_SC_RD)
1423 #define SCVAL_OAM               MBUF_SCVAL(MBUF_SC_OAM)
1424 #define SCVAL_AV                MBUF_SCVAL(MBUF_SC_AV)
1425 #define SCVAL_RV                MBUF_SCVAL(MBUF_SC_RV)
1426 #define SCVAL_VI                MBUF_SCVAL(MBUF_SC_VI)
1427 #define SCVAL_SIG               MBUF_SCVAL(MBUF_SC_SIG)
1428 #define SCVAL_VO                MBUF_SCVAL(MBUF_SC_VO)
1429 #define SCVAL_CTL               MBUF_SCVAL(MBUF_SC_CTL)
1430 
1431 #define MBUF_VALID_SC(c)                                                \
1432 	(c == MBUF_SC_BK_SYS || c == MBUF_SC_BK || c == MBUF_SC_BE ||   \
1433 	c == MBUF_SC_RD || c == MBUF_SC_OAM || c == MBUF_SC_AV ||       \
1434 	c == MBUF_SC_RV || c == MBUF_SC_VI || c == MBUF_SC_SIG ||       \
1435 	c == MBUF_SC_VO || c == MBUF_SC_CTL)
1436 
1437 #define MBUF_VALID_SCIDX(c)                                             \
1438 	(c == SCIDX_BK_SYS || c == SCIDX_BK || c == SCIDX_BE ||         \
1439 	c == SCIDX_RD || c == SCIDX_OAM || c == SCIDX_AV ||             \
1440 	c == SCIDX_RV || c == SCIDX_VI || c == SCIDX_SIG ||             \
1441 	c == SCIDX_VO || c == SCIDX_CTL)
1442 
1443 #define MBUF_VALID_SCVAL(c)                                             \
1444 	(c == SCVAL_BK_SYS || c == SCVAL_BK || c == SCVAL_BE ||         \
1445 	c == SCVAL_RD || c == SCVAL_OAM || c == SCVAL_AV ||             \
1446 	c == SCVAL_RV || c == SCVAL_VI || c == SCVAL_SIG ||             \
1447 	c == SCVAL_VO || SCVAL_CTL)
1448 
1449 extern unsigned char *mbutl;    /* start VA of mbuf pool */
1450 extern unsigned char *embutl;   /* end VA of mbuf pool */
1451 extern unsigned int nmbclusters;        /* number of mapped clusters */
1452 extern int njcl;                /* # of jumbo clusters  */
1453 extern int njclbytes;   /* size of a jumbo cluster */
1454 extern int max_hdr;             /* largest link+protocol header */
1455 extern int max_datalen; /* MHLEN - max_hdr */
1456 
1457 extern int max_linkhdr;        /* largest link-level header */
1458 
1459 /* Use max_protohdr instead of _max_protohdr */
1460 extern int max_protohdr;       /* largest protocol header */
1461 
1462 __private_extern__ unsigned int mbuf_default_ncl(uint64_t);
1463 __private_extern__ void mbinit(void);
1464 __private_extern__ struct mbuf *m_clattach(struct mbuf *, int, caddr_t,
1465     void (*)(caddr_t, u_int, caddr_t), size_t, caddr_t, int, int);
1466 __private_extern__ caddr_t m_bigalloc(int);
1467 __private_extern__ void m_bigfree(caddr_t, u_int, caddr_t);
1468 __private_extern__ struct mbuf *m_mbigget(struct mbuf *, int);
1469 __private_extern__ caddr_t m_16kalloc(int);
1470 __private_extern__ void m_16kfree(caddr_t, u_int, caddr_t);
1471 __private_extern__ struct mbuf *m_m16kget(struct mbuf *, int);
1472 __private_extern__ int m_reinit(struct mbuf *, int);
1473 __private_extern__ struct mbuf *m_free(struct mbuf *) __XNU_INTERNAL(m_free);
1474 __private_extern__ struct mbuf *m_getclr(int, int);
1475 __private_extern__ struct mbuf *m_getptr(struct mbuf *, int, int *);
1476 __private_extern__ unsigned int m_length(struct mbuf *);
1477 __private_extern__ unsigned int m_length2(struct mbuf *, struct mbuf **);
1478 __private_extern__ unsigned int m_fixhdr(struct mbuf *);
1479 __private_extern__ struct mbuf *m_defrag(struct mbuf *, int);
1480 __private_extern__ struct mbuf *m_defrag_offset(struct mbuf *, u_int32_t, int);
1481 __private_extern__ struct mbuf *m_prepend(struct mbuf *, int, int);
1482 __private_extern__ struct mbuf *m_copyup(struct mbuf *, int, int);
1483 __private_extern__ struct mbuf *m_retry(int, int);
1484 __private_extern__ struct mbuf *m_retryhdr(int, int);
1485 __private_extern__ int m_freem_list(struct mbuf *);
1486 __private_extern__ int m_append(struct mbuf *, int, caddr_t);
1487 __private_extern__ struct mbuf *m_last(struct mbuf *);
1488 __private_extern__ struct mbuf *m_devget(char *, int, int, struct ifnet *,
1489     void (*)(const void *, void *, size_t));
1490 __private_extern__ struct mbuf *m_pulldown(struct mbuf *, int, int, int *);
1491 
1492 __private_extern__ struct mbuf *m_getcl(int, int, int);
1493 __private_extern__ caddr_t m_mclalloc(int);
1494 __private_extern__ int m_mclhasreference(struct mbuf *);
1495 __private_extern__ void m_copy_pkthdr(struct mbuf *, struct mbuf *);
1496 __private_extern__ int m_dup_pkthdr(struct mbuf *, struct mbuf *, int);
1497 __private_extern__ void m_copy_pftag(struct mbuf *, struct mbuf *);
1498 __private_extern__ void m_copy_necptag(struct mbuf *, struct mbuf *);
1499 __private_extern__ void m_copy_classifier(struct mbuf *, struct mbuf *);
1500 
1501 __private_extern__ struct mbuf *m_dtom(void *);
1502 __private_extern__ int m_mtocl(void *);
1503 __private_extern__ union mcluster *m_cltom(int);
1504 
1505 __private_extern__ void m_align(struct mbuf *, int);
1506 
1507 __private_extern__ struct mbuf *m_normalize(struct mbuf *m);
1508 __private_extern__ void m_mchtype(struct mbuf *m, int t);
1509 __private_extern__ void m_mcheck(struct mbuf *);
1510 
1511 __private_extern__ void m_copyback(struct mbuf *, int, int, const void *);
1512 __private_extern__ struct mbuf *m_copyback_cow(struct mbuf *, int, int,
1513     const void *, int);
1514 __private_extern__ int m_makewritable(struct mbuf **, int, int, int);
1515 __private_extern__ struct mbuf *m_dup(struct mbuf *m, int how);
1516 __private_extern__ struct mbuf *m_copym_with_hdrs(struct mbuf *, int, int, int,
1517     struct mbuf **, int *, uint32_t);
1518 __private_extern__ struct mbuf *m_getpackethdrs(int, int);
1519 __private_extern__ struct mbuf *m_getpacket_how(int);
1520 __private_extern__ struct mbuf *m_getpackets_internal(unsigned int *, int,
1521     int, int, size_t);
1522 __private_extern__ struct mbuf *m_allocpacket_internal(unsigned int *, size_t,
1523     unsigned int *, int, int, size_t);
1524 
1525 __private_extern__ int m_ext_set_prop(struct mbuf *, uint32_t, uint32_t);
1526 __private_extern__ uint32_t m_ext_get_prop(struct mbuf *);
1527 __private_extern__ int m_ext_paired_is_active(struct mbuf *);
1528 __private_extern__ void m_ext_paired_activate(struct mbuf *);
1529 
1530 __private_extern__ void m_add_crumb(struct mbuf *, uint16_t);
1531 
1532 __private_extern__ void mbuf_drain(boolean_t);
1533 
1534 /*
1535  * Packets may have annotations attached by affixing a list of "packet
1536  * tags" to the pkthdr structure.  Packet tags are dynamically allocated
1537  * semi-opaque data structures that have a fixed header (struct m_tag)
1538  * that specifies the size of the memory block and an <id,type> pair that
1539  * identifies it. The id identifies the module and the type identifies the
1540  * type of data for that module. The id of zero is reserved for the kernel.
1541  *
1542  * Note that the packet tag returned by m_tag_allocate has the default
1543  * memory alignment implemented by malloc.  To reference private data one
1544  * can use a construct like:
1545  *
1546  *      struct m_tag *mtag = m_tag_allocate(...);
1547  *      struct foo *p = (struct foo *)(mtag+1);
1548  *
1549  * if the alignment of struct m_tag is sufficient for referencing members
1550  * of struct foo.  Otherwise it is necessary to embed struct m_tag within
1551  * the private data structure to insure proper alignment; e.g.
1552  *
1553  *      struct foo {
1554  *              struct m_tag    tag;
1555  *              ...
1556  *      };
1557  *      struct foo *p = (struct foo *) m_tag_allocate(...);
1558  *      struct m_tag *mtag = &p->tag;
1559  */
1560 
1561 #define KERNEL_MODULE_TAG_ID    0
1562 
1563 enum {
1564 	KERNEL_TAG_TYPE_NONE                    = 0,
1565 	KERNEL_TAG_TYPE_DUMMYNET                = 1,
1566 	KERNEL_TAG_TYPE_DIVERT                  = 2,
1567 	KERNEL_TAG_TYPE_IPFORWARD               = 3,
1568 	KERNEL_TAG_TYPE_IPFILT                  = 4,
1569 	KERNEL_TAG_TYPE_MACLABEL                = 5,
1570 	KERNEL_TAG_TYPE_MAC_POLICY_LABEL        = 6,
1571 	KERNEL_TAG_TYPE_ENCAP                   = 8,
1572 	KERNEL_TAG_TYPE_INET6                   = 9,
1573 	KERNEL_TAG_TYPE_IPSEC                   = 10,
1574 	KERNEL_TAG_TYPE_DRVAUX                  = 11,
1575 #if SKYWALK
1576 	KERNEL_TAG_TYPE_PKT_FLOWADV             = 12,
1577 #endif /* SKYWALK */
1578 	KERNEL_TAG_TYPE_CFIL_UDP                = 13,
1579 	KERNEL_TAG_TYPE_PF_REASS                = 14,
1580 };
1581 
1582 /* Packet tag routines */
1583 __private_extern__ struct  m_tag *m_tag_alloc(u_int32_t, u_int16_t, int, int);
1584 __private_extern__ struct  m_tag *m_tag_create(u_int32_t, u_int16_t, int, int,
1585     struct mbuf *);
1586 __private_extern__ void m_tag_free(struct m_tag *);
1587 __private_extern__ void m_tag_prepend(struct mbuf *, struct m_tag *);
1588 __private_extern__ void m_tag_unlink(struct mbuf *, struct m_tag *);
1589 __private_extern__ void m_tag_delete(struct mbuf *, struct m_tag *);
1590 __private_extern__ void m_tag_delete_chain(struct mbuf *, struct m_tag *);
1591 __private_extern__ struct m_tag *m_tag_locate(struct mbuf *, u_int32_t,
1592     u_int16_t, struct m_tag *);
1593 __private_extern__ struct m_tag *m_tag_copy(struct m_tag *, int);
1594 __private_extern__ int m_tag_copy_chain(struct mbuf *, struct mbuf *, int);
1595 __private_extern__ void m_tag_init(struct mbuf *, int);
1596 __private_extern__ struct  m_tag *m_tag_first(struct mbuf *);
1597 __private_extern__ struct  m_tag *m_tag_next(struct mbuf *, struct m_tag *);
1598 
1599 __private_extern__ void m_scratch_init(struct mbuf *);
1600 __private_extern__ u_int32_t m_scratch_get(struct mbuf *, u_int8_t **);
1601 
1602 __private_extern__ void m_classifier_init(struct mbuf *, uint32_t);
1603 
1604 __private_extern__ int m_set_service_class(struct mbuf *, mbuf_svc_class_t);
1605 __private_extern__ mbuf_svc_class_t m_get_service_class(struct mbuf *);
1606 __private_extern__ mbuf_svc_class_t m_service_class_from_idx(u_int32_t);
1607 __private_extern__ mbuf_svc_class_t m_service_class_from_val(u_int32_t);
1608 __private_extern__ int m_set_traffic_class(struct mbuf *, mbuf_traffic_class_t);
1609 __private_extern__ mbuf_traffic_class_t m_get_traffic_class(struct mbuf *);
1610 
1611 #define ADDCARRY(_x)  do {                                              \
1612 	while (((_x) >> 16) != 0)                                       \
1613 	        (_x) = ((_x) >> 16) + ((_x) & 0xffff);                  \
1614 } while (0)
1615 
1616 __private_extern__ u_int16_t m_adj_sum16(struct mbuf *, u_int32_t,
1617     u_int32_t, u_int32_t, u_int32_t);
1618 __private_extern__ u_int16_t m_sum16(struct mbuf *, u_int32_t, u_int32_t);
1619 
1620 __private_extern__ void m_set_ext(struct mbuf *, struct ext_ref *,
1621     m_ext_free_func_t, caddr_t);
1622 __private_extern__ struct ext_ref *m_get_rfa(struct mbuf *);
1623 __private_extern__ m_ext_free_func_t m_get_ext_free(struct mbuf *);
1624 __private_extern__ caddr_t m_get_ext_arg(struct mbuf *);
1625 
1626 __private_extern__ void m_do_tx_compl_callback(struct mbuf *, struct ifnet *);
1627 __private_extern__ mbuf_tx_compl_func m_get_tx_compl_callback(u_int32_t);
1628 
1629 __END_DECLS
1630 #endif /* XNU_KERNEL_PRIVATE */
1631 #endif  /* !_SYS_MBUF_H_ */
1632