1 /* 2 * Copyright (c) 2022 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 29 #ifndef _SKYWALK_NEXUS_TRAFFIC_RULE_H_ 30 #define _SKYWALK_NEXUS_TRAFFIC_RULE_H_ 31 32 #include <skywalk/os_skywalk_private.h> 33 34 __BEGIN_DECLS 35 struct nxctl_traffic_rule; 36 37 /* 38 * These callbacks need to be implemented for each rule type. 39 */ 40 41 /* Validate user provided parameters. */ 42 typedef int (nxctl_traffic_rule_validate_cb_t)( 43 const char *ifname, 44 struct ifnet_traffic_descriptor_common *td, 45 struct ifnet_traffic_rule_action *ra); 46 /* 47 * Each rule type has its own global structure for storing rules. 48 * These callbacks access this global structure. 49 */ 50 #define NTR_FIND_FLAG_EXACT 0x0001 51 typedef int (nxctl_traffic_rule_find_cb_t)( 52 const char *ifname, 53 struct ifnet_traffic_descriptor_common *td, 54 uint32_t flags, 55 struct nxctl_traffic_rule **ntrp); 56 57 typedef int (nxctl_traffic_rule_find_by_uuid_cb_t)( 58 uuid_t uuid, 59 struct nxctl_traffic_rule **ntrp); 60 61 typedef void (nxctl_traffic_rule_link_cb_t)( 62 struct nxctl_traffic_rule *ntr); 63 64 typedef void (nxctl_traffic_rule_unlink_cb_t)( 65 struct nxctl_traffic_rule *ntr); 66 67 /* 68 * Notifies lower layers of the addition/removal of a rule. 69 * This is called outside of nxctl_traffic_rule_lock to avoid potential 70 * locking issues. 71 */ 72 #define NTR_NOTIFY_FLAG_ADD 0x0001 73 #define NTR_NOTIFY_FLAG_REMOVE 0x0002 74 typedef int (nxctl_traffic_rule_notify_cb_t)( 75 struct nxctl_traffic_rule *ntr, 76 uint32_t flags); 77 78 /* 79 * Callback for a rule type to get rule count. 80 */ 81 typedef int (nxctl_traffic_rule_get_count_cb_t)( 82 const char *ifname, uint32_t *count); 83 84 /* 85 * Create/Destroy callbacks for a rule type. 86 */ 87 typedef int (nxctl_traffic_rule_create_cb_t)( 88 const char *ifname, 89 struct ifnet_traffic_descriptor_common *td, 90 struct ifnet_traffic_rule_action *ra, 91 uint32_t flags, 92 struct nxctl_traffic_rule **ntrp); 93 94 typedef void (nxctl_traffic_rule_destroy_cb_t)( 95 struct nxctl_traffic_rule *ntr); 96 97 /* 98 * This is used for copying all rules for a type (including generic 99 * and type-specific info) to userspace. 100 */ 101 typedef int (nxctl_traffic_rule_get_all_cb_t)( 102 uint32_t size, 103 uint32_t *count, 104 user_addr_t uaddr); 105 106 struct nxctl_traffic_rule_type { 107 uint8_t ntrt_type; 108 nxctl_traffic_rule_validate_cb_t *ntrt_validate; 109 nxctl_traffic_rule_find_cb_t *ntrt_find; 110 nxctl_traffic_rule_find_by_uuid_cb_t *ntrt_find_by_uuid; 111 nxctl_traffic_rule_link_cb_t *ntrt_link; 112 nxctl_traffic_rule_unlink_cb_t *ntrt_unlink; 113 nxctl_traffic_rule_notify_cb_t *ntrt_notify; 114 nxctl_traffic_rule_create_cb_t *ntrt_create; 115 nxctl_traffic_rule_destroy_cb_t *ntrt_destroy; 116 nxctl_traffic_rule_get_all_cb_t *ntrt_get_all; 117 nxctl_traffic_rule_get_count_cb_t *ntrt_get_count; 118 }; 119 120 /* 121 * Generic traffic rule. 122 * Contains fields common to all traffic rules. 123 */ 124 #define NTR_FLAG_PERSIST 0x0001 125 #define NTR_FLAG_ON_NXCTL_LIST 0x0002 126 struct nxctl_traffic_rule { 127 uint8_t ntrt_type; 128 uint32_t ntr_flags; 129 os_refcnt_t ntr_refcnt; 130 uuid_t ntr_uuid; 131 char ntr_procname[NTR_PROCNAME_SZ]; 132 char ntr_ifname[IFNAMSIZ]; 133 SLIST_ENTRY(nxctl_traffic_rule) ntr_storage_link; 134 }; 135 136 #define ITDBIT(set, bit) (((set) != 0) ? (bit) : 0) 137 138 139 void nxtr_wlock(void); 140 void nxtr_wunlock(void); 141 void nxtr_rlock(void); 142 void nxtr_runlock(void); 143 144 #define NXTR_WLOCK() nxtr_wlock() 145 #define NXTR_WUNLOCK() nxtr_wunlock() 146 #define NXTR_RLOCK() nxtr_rlock() 147 #define NXTR_RUNLOCK() nxtr_runlock() 148 149 void retain_traffic_rule(struct nxctl_traffic_rule *ntr); 150 void release_traffic_rule(struct nxctl_traffic_rule *ntr); 151 152 __END_DECLS 153 154 #endif /* _SKYWALK_NEXUS_TRAFFIC_RULE_H_ */ 155