1/*
2 * Copyright (c) 2016-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef __PBUF_H__
25#define __PBUF_H__
26
27#include <sys/mbuf.h>
28
29enum pbuf_type {
30 PBUF_TYPE_ZOMBIE = 0,
31 PBUF_TYPE_MBUF,
32 PBUF_TYPE_MEMORY
33};
34
35enum pbuf_action {
36 PBUF_ACTION_DESTROY
37};
38
39#define PBUF_ACTION_RV_SUCCESS 0
40#define PBUF_ACTION_RV_FAILURE (-1)
41
42struct pbuf_memory {
43 uint8_t *pm_buffer; // Pointer to start of buffer
44 u_int pm_buffer_len; // Total length of buffer
45 u_int pm_offset; // Offset to start of payload
46 u_int pm_len; // Length of payload
47 uint32_t pm_csum_flags;
48 uint32_t pm_csum_data;
49 uint8_t pm_proto;
50 uint8_t pm_flowsrc;
51 uint32_t pm_flowid;
52 uint32_t pm_flags;
53 struct pf_mtag pm_pftag;
54 int (*pm_action)(struct pbuf_memory *, enum pbuf_action);
55 void *pm_action_cookie;
56};
57
58typedef struct pbuf {
59 enum pbuf_type pb_type;
60 union {
61 struct mbuf *pbu_mbuf;
62 struct pbuf_memory pbu_memory;
63 } pb_u;
64#define pb_mbuf pb_u.pbu_mbuf
65#define pb_memory pb_u.pbu_memory
66
67 void *pb_data;
68 uint32_t pb_packet_len;
69 uint32_t pb_contig_len;
70 uint32_t *pb_csum_flags;
71 uint32_t *pb_csum_data; /* data field used by csum routines */
72 uint8_t *pb_proto;
73 uint8_t *pb_flowsrc;
74 uint32_t *pb_flowid;
75 uint32_t *pb_flags;
76 struct pf_mtag *pb_pftag;
77 struct ifnet *pb_ifp;
78 struct pbuf *pb_next;
79
80} pbuf_t;
81
82#define pbuf_is_valid(pb) (!((pb) == NULL || (pb)->pb_type == PBUF_TYPE_ZOMBIE))
83
84void pbuf_init_mbuf(pbuf_t *, struct mbuf *, struct ifnet *);
85void pbuf_init_memory(pbuf_t *, const struct pbuf_memory *,
86 struct ifnet *);
87void pbuf_destroy(pbuf_t *);
88void pbuf_sync(pbuf_t *);
89
90struct mbuf *pbuf_to_mbuf(pbuf_t *, boolean_t);
91struct mbuf *pbuf_clone_to_mbuf(pbuf_t *);
92
93void * pbuf_ensure_contig(pbuf_t *, size_t);
94void * pbuf_ensure_writable(pbuf_t *, size_t);
95
96void * pbuf_resize_segment(pbuf_t *, int off, int olen, int nlen);
97void * pbuf_contig_segment(pbuf_t *, int off, int len);
98
99void pbuf_copy_data(pbuf_t *, int, int, void *);
100void pbuf_copy_back(pbuf_t *, int, int, void *);
101
102uint16_t pbuf_inet_cksum(const pbuf_t *, uint32_t, uint32_t, uint32_t);
103uint16_t pbuf_inet6_cksum(const pbuf_t *, uint32_t, uint32_t, uint32_t);
104
105mbuf_svc_class_t pbuf_get_service_class(const pbuf_t *);
106
107#endif /* __PBUF_H__ */
108