| 1 | /* |
| 2 | * Copyright (c) 2016-2022 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 | |
| 29 | enum pbuf_type { |
| 30 | PBUF_TYPE_ZOMBIE = 0, |
| 31 | PBUF_TYPE_MBUF, |
| 32 | PBUF_TYPE_MEMORY |
| 33 | }; |
| 34 | |
| 35 | enum pbuf_action { |
| 36 | PBUF_ACTION_DESTROY |
| 37 | }; |
| 38 | |
| 39 | #define PBUF_ACTION_RV_SUCCESS 0 |
| 40 | #define PBUF_ACTION_RV_FAILURE (-1) |
| 41 | |
| 42 | struct 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_flow_gencnt; |
| 53 | uint32_t pm_flags; |
| 54 | struct pf_mtag pm_pftag; |
| 55 | struct pf_fragment_tag pm_pf_fragtag; |
| 56 | int (*pm_action)(struct pbuf_memory *, enum pbuf_action); |
| 57 | void *pm_action_cookie; |
| 58 | }; |
| 59 | |
| 60 | typedef struct pbuf { |
| 61 | enum pbuf_type pb_type; |
| 62 | union { |
| 63 | struct mbuf *pbu_mbuf; |
| 64 | struct pbuf_memory pbu_memory; |
| 65 | } pb_u; |
| 66 | #define pb_mbuf pb_u.pbu_mbuf |
| 67 | #define pb_memory pb_u.pbu_memory |
| 68 | |
| 69 | void *pb_data; |
| 70 | uint32_t pb_packet_len; |
| 71 | uint32_t pb_contig_len; |
| 72 | uint32_t *pb_csum_flags; |
| 73 | uint32_t *pb_csum_data; /* data field used by csum routines */ |
| 74 | uint8_t *pb_proto; |
| 75 | uint8_t *pb_flowsrc; |
| 76 | uint32_t *pb_flowid; |
| 77 | uint32_t *pb_flow_gencnt; |
| 78 | uint32_t *pb_flags; |
| 79 | struct pf_mtag *pb_pftag; |
| 80 | struct pf_fragment_tag *pb_pf_fragtag; |
| 81 | struct ifnet *pb_ifp; |
| 82 | struct pbuf *pb_next; |
| 83 | } pbuf_t; |
| 84 | |
| 85 | #define pbuf_is_valid(pb) (!((pb) == NULL || (pb)->pb_type == PBUF_TYPE_ZOMBIE)) |
| 86 | |
| 87 | void pbuf_init_mbuf(pbuf_t *, struct mbuf *, struct ifnet *); |
| 88 | void pbuf_init_memory(pbuf_t *, const struct pbuf_memory *, |
| 89 | struct ifnet *); |
| 90 | void pbuf_destroy(pbuf_t *); |
| 91 | void pbuf_sync(pbuf_t *); |
| 92 | |
| 93 | struct mbuf *pbuf_to_mbuf(pbuf_t *, boolean_t); |
| 94 | struct mbuf *pbuf_clone_to_mbuf(pbuf_t *); |
| 95 | |
| 96 | void * pbuf_ensure_contig(pbuf_t *, size_t); |
| 97 | void * pbuf_ensure_writable(pbuf_t *, size_t); |
| 98 | |
| 99 | void * pbuf_resize_segment(pbuf_t *, int off, int olen, int nlen); |
| 100 | void * pbuf_contig_segment(pbuf_t *, int off, int len); |
| 101 | |
| 102 | void pbuf_copy_data(pbuf_t *, int, int, void *); |
| 103 | void pbuf_copy_back(pbuf_t *, int, int, void *); |
| 104 | |
| 105 | uint16_t pbuf_inet_cksum(const pbuf_t *, uint32_t, uint32_t, uint32_t); |
| 106 | uint16_t pbuf_inet6_cksum(const pbuf_t *, uint32_t, uint32_t, uint32_t); |
| 107 | |
| 108 | mbuf_svc_class_t pbuf_get_service_class(const pbuf_t *); |
| 109 | void * pbuf_get_packet_buffer_address(const pbuf_t *); |
| 110 | #endif /* __PBUF_H__ */ |
| 111 | |