1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2008-2021 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions #include <sys/types.h>
30*1031c584SApple OSS Distributions #include <kern/locks.h>
31*1031c584SApple OSS Distributions #include <kern/zalloc.h>
32*1031c584SApple OSS Distributions #include <sys/errno.h>
33*1031c584SApple OSS Distributions #include <sys/sysctl.h>
34*1031c584SApple OSS Distributions #include <sys/malloc.h>
35*1031c584SApple OSS Distributions #include <sys/socket.h>
36*1031c584SApple OSS Distributions #include <libkern/OSAtomic.h>
37*1031c584SApple OSS Distributions #include <libkern/libkern.h>
38*1031c584SApple OSS Distributions #include <net/if.h>
39*1031c584SApple OSS Distributions #include <net/if_mib.h>
40*1031c584SApple OSS Distributions #include <string.h>
41*1031c584SApple OSS Distributions
42*1031c584SApple OSS Distributions #include "net/net_str_id.h"
43*1031c584SApple OSS Distributions
44*1031c584SApple OSS Distributions #define NET_ID_STR_MAX_LEN 2048
45*1031c584SApple OSS Distributions #define NET_ID_STR_ENTRY_SIZE(__str) \
46*1031c584SApple OSS Distributions (__builtin_offsetof(struct net_str_id_entry, nsi_string[0]) + \
47*1031c584SApple OSS Distributions strlen(__str) + 1)
48*1031c584SApple OSS Distributions
49*1031c584SApple OSS Distributions #define FIRST_NET_STR_ID 1000
50*1031c584SApple OSS Distributions static SLIST_HEAD(, net_str_id_entry) net_str_id_list = {NULL};
51*1031c584SApple OSS Distributions static LCK_GRP_DECLARE(net_str_id_grp, "mbuf_tag_allocate_id");
52*1031c584SApple OSS Distributions static LCK_MTX_DECLARE(net_str_id_lock, &net_str_id_grp);
53*1031c584SApple OSS Distributions
54*1031c584SApple OSS Distributions static u_int32_t nsi_kind_next[NSI_MAX_KIND] = { FIRST_NET_STR_ID, FIRST_NET_STR_ID, FIRST_NET_STR_ID };
55*1031c584SApple OSS Distributions static u_int32_t nsi_next_id = FIRST_NET_STR_ID;
56*1031c584SApple OSS Distributions
57*1031c584SApple OSS Distributions extern int sysctl_if_family_ids SYSCTL_HANDLER_ARGS;
58*1031c584SApple OSS Distributions
59*1031c584SApple OSS Distributions SYSCTL_DECL(_net_link_generic_system);
60*1031c584SApple OSS Distributions
61*1031c584SApple OSS Distributions SYSCTL_PROC(_net_link_generic_system, OID_AUTO, if_family_ids, CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
62*1031c584SApple OSS Distributions 0, 0, sysctl_if_family_ids, "S, if_family_id", "Interface Family ID table");
63*1031c584SApple OSS Distributions
64*1031c584SApple OSS Distributions __private_extern__ void
net_str_id_first_last(u_int32_t * first,u_int32_t * last,u_int32_t kind)65*1031c584SApple OSS Distributions net_str_id_first_last(u_int32_t *first, u_int32_t *last, u_int32_t kind)
66*1031c584SApple OSS Distributions {
67*1031c584SApple OSS Distributions *first = FIRST_NET_STR_ID;
68*1031c584SApple OSS Distributions
69*1031c584SApple OSS Distributions switch (kind) {
70*1031c584SApple OSS Distributions case NSI_MBUF_TAG:
71*1031c584SApple OSS Distributions case NSI_VENDOR_CODE:
72*1031c584SApple OSS Distributions case NSI_IF_FAM_ID:
73*1031c584SApple OSS Distributions *last = nsi_kind_next[kind] - 1;
74*1031c584SApple OSS Distributions break;
75*1031c584SApple OSS Distributions default:
76*1031c584SApple OSS Distributions *last = FIRST_NET_STR_ID - 1;
77*1031c584SApple OSS Distributions break;
78*1031c584SApple OSS Distributions }
79*1031c584SApple OSS Distributions }
80*1031c584SApple OSS Distributions
81*1031c584SApple OSS Distributions __private_extern__ errno_t
net_str_id_find_internal(const char * string,u_int32_t * out_id,u_int32_t kind,int create)82*1031c584SApple OSS Distributions net_str_id_find_internal(const char *string, u_int32_t *out_id,
83*1031c584SApple OSS Distributions u_int32_t kind, int create)
84*1031c584SApple OSS Distributions {
85*1031c584SApple OSS Distributions struct net_str_id_entry *entry = NULL;
86*1031c584SApple OSS Distributions
87*1031c584SApple OSS Distributions
88*1031c584SApple OSS Distributions if (string == NULL || out_id == NULL || kind >= NSI_MAX_KIND) {
89*1031c584SApple OSS Distributions return EINVAL;
90*1031c584SApple OSS Distributions }
91*1031c584SApple OSS Distributions if (strlen(string) > NET_ID_STR_MAX_LEN) {
92*1031c584SApple OSS Distributions return EINVAL;
93*1031c584SApple OSS Distributions }
94*1031c584SApple OSS Distributions
95*1031c584SApple OSS Distributions *out_id = 0;
96*1031c584SApple OSS Distributions
97*1031c584SApple OSS Distributions /* Look for an existing entry */
98*1031c584SApple OSS Distributions lck_mtx_lock(&net_str_id_lock);
99*1031c584SApple OSS Distributions SLIST_FOREACH(entry, &net_str_id_list, nsi_next) {
100*1031c584SApple OSS Distributions if (strcmp(string, entry->nsi_string) == 0) {
101*1031c584SApple OSS Distributions break;
102*1031c584SApple OSS Distributions }
103*1031c584SApple OSS Distributions }
104*1031c584SApple OSS Distributions
105*1031c584SApple OSS Distributions if (entry == NULL) {
106*1031c584SApple OSS Distributions if (create == 0) {
107*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
108*1031c584SApple OSS Distributions return ENOENT;
109*1031c584SApple OSS Distributions }
110*1031c584SApple OSS Distributions
111*1031c584SApple OSS Distributions entry = zalloc_permanent(NET_ID_STR_ENTRY_SIZE(string),
112*1031c584SApple OSS Distributions ZALIGN_PTR);
113*1031c584SApple OSS Distributions if (entry == NULL) {
114*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
115*1031c584SApple OSS Distributions return ENOMEM;
116*1031c584SApple OSS Distributions }
117*1031c584SApple OSS Distributions
118*1031c584SApple OSS Distributions strlcpy(entry->nsi_string, string, strlen(string) + 1);
119*1031c584SApple OSS Distributions entry->nsi_flags = (1 << kind);
120*1031c584SApple OSS Distributions entry->nsi_id = nsi_next_id++;
121*1031c584SApple OSS Distributions nsi_kind_next[kind] = nsi_next_id;
122*1031c584SApple OSS Distributions SLIST_INSERT_HEAD(&net_str_id_list, entry, nsi_next);
123*1031c584SApple OSS Distributions } else if ((entry->nsi_flags & (1 << kind)) == 0) {
124*1031c584SApple OSS Distributions if (create == 0) {
125*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
126*1031c584SApple OSS Distributions return ENOENT;
127*1031c584SApple OSS Distributions }
128*1031c584SApple OSS Distributions entry->nsi_flags |= (1 << kind);
129*1031c584SApple OSS Distributions if (entry->nsi_id >= nsi_kind_next[kind]) {
130*1031c584SApple OSS Distributions nsi_kind_next[kind] = entry->nsi_id + 1;
131*1031c584SApple OSS Distributions }
132*1031c584SApple OSS Distributions }
133*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
134*1031c584SApple OSS Distributions
135*1031c584SApple OSS Distributions *out_id = entry->nsi_id;
136*1031c584SApple OSS Distributions
137*1031c584SApple OSS Distributions return 0;
138*1031c584SApple OSS Distributions }
139*1031c584SApple OSS Distributions
140*1031c584SApple OSS Distributions
141*1031c584SApple OSS Distributions #define ROUNDUP32(a) \
142*1031c584SApple OSS Distributions ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
143*1031c584SApple OSS Distributions
144*1031c584SApple OSS Distributions int
145*1031c584SApple OSS Distributions sysctl_if_family_ids SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
146*1031c584SApple OSS Distributions {
147*1031c584SApple OSS Distributions #pragma unused(oidp)
148*1031c584SApple OSS Distributions #pragma unused(arg1)
149*1031c584SApple OSS Distributions #pragma unused(arg2)
150*1031c584SApple OSS Distributions errno_t error = 0;
151*1031c584SApple OSS Distributions struct net_str_id_entry *entry = NULL;
152*1031c584SApple OSS Distributions struct if_family_id *iffmid = NULL;
153*1031c584SApple OSS Distributions size_t iffmid_allocated_size = 0;
154*1031c584SApple OSS Distributions size_t max_size = 0;
155*1031c584SApple OSS Distributions
156*1031c584SApple OSS Distributions lck_mtx_lock(&net_str_id_lock);
157*1031c584SApple OSS Distributions SLIST_FOREACH(entry, &net_str_id_list, nsi_next) {
158*1031c584SApple OSS Distributions size_t str_size;
159*1031c584SApple OSS Distributions size_t iffmid_size;
160*1031c584SApple OSS Distributions
161*1031c584SApple OSS Distributions if ((entry->nsi_flags & (1 << NSI_IF_FAM_ID)) == 0) {
162*1031c584SApple OSS Distributions continue;
163*1031c584SApple OSS Distributions }
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions str_size = strlen(entry->nsi_string);
166*1031c584SApple OSS Distributions if (str_size > NET_ID_STR_MAX_LEN) {
167*1031c584SApple OSS Distributions str_size = NET_ID_STR_MAX_LEN;
168*1031c584SApple OSS Distributions }
169*1031c584SApple OSS Distributions str_size += 1; // make room for end-of-string
170*1031c584SApple OSS Distributions iffmid_size = ROUNDUP32(offsetof(struct net_str_id_entry, nsi_string) + str_size);
171*1031c584SApple OSS Distributions
172*1031c584SApple OSS Distributions if (iffmid_size > max_size) {
173*1031c584SApple OSS Distributions if (iffmid) {
174*1031c584SApple OSS Distributions kfree_data(iffmid, iffmid->iffmid_len);
175*1031c584SApple OSS Distributions }
176*1031c584SApple OSS Distributions iffmid = (struct if_family_id *)kalloc_data(iffmid_size,
177*1031c584SApple OSS Distributions Z_WAITOK | Z_ZERO);
178*1031c584SApple OSS Distributions if (iffmid == NULL) {
179*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
180*1031c584SApple OSS Distributions error = ENOMEM;
181*1031c584SApple OSS Distributions goto done;
182*1031c584SApple OSS Distributions }
183*1031c584SApple OSS Distributions iffmid_allocated_size = iffmid_size;
184*1031c584SApple OSS Distributions max_size = iffmid_size;
185*1031c584SApple OSS Distributions }
186*1031c584SApple OSS Distributions
187*1031c584SApple OSS Distributions iffmid->iffmid_len = (uint32_t)iffmid_size;
188*1031c584SApple OSS Distributions iffmid->iffmid_id = entry->nsi_id;
189*1031c584SApple OSS Distributions strlcpy(iffmid->iffmid_str, entry->nsi_string, str_size);
190*1031c584SApple OSS Distributions error = SYSCTL_OUT(req, iffmid, iffmid_size);
191*1031c584SApple OSS Distributions if (error) {
192*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
193*1031c584SApple OSS Distributions goto done;
194*1031c584SApple OSS Distributions }
195*1031c584SApple OSS Distributions }
196*1031c584SApple OSS Distributions lck_mtx_unlock(&net_str_id_lock);
197*1031c584SApple OSS Distributions
198*1031c584SApple OSS Distributions done:
199*1031c584SApple OSS Distributions if (iffmid) {
200*1031c584SApple OSS Distributions kfree_data(iffmid, iffmid_allocated_size);
201*1031c584SApple OSS Distributions }
202*1031c584SApple OSS Distributions return error;
203*1031c584SApple OSS Distributions }
204