| 1 | /* |
| 2 | * Copyright (c) 2008-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 | |
| 29 | #include <sys/types.h> |
| 30 | #include <kern/locks.h> |
| 31 | #include <kern/zalloc.h> |
| 32 | #include <sys/errno.h> |
| 33 | #include <sys/sysctl.h> |
| 34 | #include <sys/malloc.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <libkern/OSAtomic.h> |
| 37 | #include <libkern/libkern.h> |
| 38 | #include <net/if.h> |
| 39 | #include <net/if_mib.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include "net/net_str_id.h" |
| 43 | |
| 44 | #define NET_ID_STR_MAX_LEN 2048 |
| 45 | #define NET_ID_STR_ENTRY_SIZE(__str) \ |
| 46 | (__builtin_offsetof(struct net_str_id_entry, nsi_string[0]) + \ |
| 47 | strlen(__str) + 1) |
| 48 | |
| 49 | #define FIRST_NET_STR_ID 1000 |
| 50 | static SLIST_HEAD(, net_str_id_entry) net_str_id_list = {NULL}; |
| 51 | static LCK_GRP_DECLARE(net_str_id_grp, "mbuf_tag_allocate_id" ); |
| 52 | static LCK_MTX_DECLARE(net_str_id_lock, &net_str_id_grp); |
| 53 | |
| 54 | static u_int32_t nsi_kind_next[NSI_MAX_KIND] = { FIRST_NET_STR_ID, FIRST_NET_STR_ID, FIRST_NET_STR_ID }; |
| 55 | static u_int32_t nsi_next_id = FIRST_NET_STR_ID; |
| 56 | |
| 57 | extern int sysctl_if_family_ids SYSCTL_HANDLER_ARGS; |
| 58 | |
| 59 | SYSCTL_DECL(_net_link_generic_system); |
| 60 | |
| 61 | SYSCTL_PROC(_net_link_generic_system, OID_AUTO, if_family_ids, CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
| 62 | 0, 0, sysctl_if_family_ids, "S, if_family_id" , "Interface Family ID table" ); |
| 63 | |
| 64 | __private_extern__ void |
| 65 | net_str_id_first_last(u_int32_t *first, u_int32_t *last, u_int32_t kind) |
| 66 | { |
| 67 | *first = FIRST_NET_STR_ID; |
| 68 | |
| 69 | switch (kind) { |
| 70 | case NSI_MBUF_TAG: |
| 71 | case NSI_VENDOR_CODE: |
| 72 | case NSI_IF_FAM_ID: |
| 73 | *last = nsi_kind_next[kind] - 1; |
| 74 | break; |
| 75 | default: |
| 76 | *last = FIRST_NET_STR_ID - 1; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | __private_extern__ errno_t |
| 82 | net_str_id_find_internal(const char *string, u_int32_t *out_id, |
| 83 | u_int32_t kind, int create) |
| 84 | { |
| 85 | struct net_str_id_entry *entry = NULL; |
| 86 | |
| 87 | |
| 88 | if (string == NULL || out_id == NULL || kind >= NSI_MAX_KIND) { |
| 89 | return EINVAL; |
| 90 | } |
| 91 | if (strlen(s: string) > NET_ID_STR_MAX_LEN) { |
| 92 | return EINVAL; |
| 93 | } |
| 94 | |
| 95 | *out_id = 0; |
| 96 | |
| 97 | /* Look for an existing entry */ |
| 98 | lck_mtx_lock(lck: &net_str_id_lock); |
| 99 | SLIST_FOREACH(entry, &net_str_id_list, nsi_next) { |
| 100 | if (strcmp(s1: string, s2: entry->nsi_string) == 0) { |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (entry == NULL) { |
| 106 | if (create == 0) { |
| 107 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 108 | return ENOENT; |
| 109 | } |
| 110 | |
| 111 | entry = zalloc_permanent(NET_ID_STR_ENTRY_SIZE(string), |
| 112 | ZALIGN_PTR); |
| 113 | if (entry == NULL) { |
| 114 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 115 | return ENOMEM; |
| 116 | } |
| 117 | |
| 118 | strlcpy(dst: entry->nsi_string, src: string, n: strlen(s: string) + 1); |
| 119 | entry->nsi_flags = (1 << kind); |
| 120 | entry->nsi_id = nsi_next_id++; |
| 121 | nsi_kind_next[kind] = nsi_next_id; |
| 122 | SLIST_INSERT_HEAD(&net_str_id_list, entry, nsi_next); |
| 123 | } else if ((entry->nsi_flags & (1 << kind)) == 0) { |
| 124 | if (create == 0) { |
| 125 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 126 | return ENOENT; |
| 127 | } |
| 128 | entry->nsi_flags |= (1 << kind); |
| 129 | if (entry->nsi_id >= nsi_kind_next[kind]) { |
| 130 | nsi_kind_next[kind] = entry->nsi_id + 1; |
| 131 | } |
| 132 | } |
| 133 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 134 | |
| 135 | *out_id = entry->nsi_id; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | #define ROUNDUP32(a) \ |
| 142 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t)) |
| 143 | |
| 144 | int |
| 145 | sysctl_if_family_ids SYSCTL_HANDLER_ARGS /* XXX bad syntax! */ |
| 146 | { |
| 147 | #pragma unused(oidp) |
| 148 | #pragma unused(arg1) |
| 149 | #pragma unused(arg2) |
| 150 | errno_t error = 0; |
| 151 | struct net_str_id_entry *entry = NULL; |
| 152 | struct if_family_id *iffmid = NULL; |
| 153 | size_t iffmid_allocated_size = 0; |
| 154 | size_t max_size = 0; |
| 155 | |
| 156 | lck_mtx_lock(lck: &net_str_id_lock); |
| 157 | SLIST_FOREACH(entry, &net_str_id_list, nsi_next) { |
| 158 | size_t str_size; |
| 159 | size_t iffmid_size; |
| 160 | |
| 161 | if ((entry->nsi_flags & (1 << NSI_IF_FAM_ID)) == 0) { |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | str_size = strlen(s: entry->nsi_string); |
| 166 | if (str_size > NET_ID_STR_MAX_LEN) { |
| 167 | str_size = NET_ID_STR_MAX_LEN; |
| 168 | } |
| 169 | str_size += 1; // make room for end-of-string |
| 170 | iffmid_size = ROUNDUP32(offsetof(struct net_str_id_entry, nsi_string) + str_size); |
| 171 | |
| 172 | if (iffmid_size > max_size) { |
| 173 | if (iffmid) { |
| 174 | kfree_data(iffmid, iffmid->iffmid_len); |
| 175 | } |
| 176 | iffmid = (struct if_family_id *)kalloc_data(iffmid_size, |
| 177 | Z_WAITOK | Z_ZERO); |
| 178 | if (iffmid == NULL) { |
| 179 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 180 | error = ENOMEM; |
| 181 | goto done; |
| 182 | } |
| 183 | iffmid_allocated_size = iffmid_size; |
| 184 | max_size = iffmid_size; |
| 185 | } |
| 186 | |
| 187 | iffmid->iffmid_len = (uint32_t)iffmid_size; |
| 188 | iffmid->iffmid_id = entry->nsi_id; |
| 189 | strlcpy(dst: iffmid->iffmid_str, src: entry->nsi_string, n: str_size); |
| 190 | error = SYSCTL_OUT(req, iffmid, iffmid_size); |
| 191 | if (error) { |
| 192 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 193 | goto done; |
| 194 | } |
| 195 | } |
| 196 | lck_mtx_unlock(lck: &net_str_id_lock); |
| 197 | |
| 198 | done: |
| 199 | if (iffmid) { |
| 200 | kfree_data(iffmid, iffmid_allocated_size); |
| 201 | } |
| 202 | return error; |
| 203 | } |
| 204 | |