xref: /xnu-11215.41.3/bsd/net/net_str_id.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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 // TODO: -fbounds-safety increases the alignment and we have
43 //       no control over the alignment. (rdar://118519573)
44 #pragma clang diagnostic ignored "-Wcast-align"
45 #include "net/net_str_id.h"
46 
47 #define NET_ID_STR_MAX_LEN 2048
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 __private_extern__ void
net_str_id_first_last(u_int32_t * first,u_int32_t * last,u_int32_t kind)58 net_str_id_first_last(u_int32_t *first, u_int32_t *last, u_int32_t kind)
59 {
60 	*first = FIRST_NET_STR_ID;
61 
62 	switch (kind) {
63 	case NSI_MBUF_TAG:
64 	case NSI_VENDOR_CODE:
65 	case NSI_IF_FAM_ID:
66 		*last = nsi_kind_next[kind] - 1;
67 		break;
68 	default:
69 		*last = FIRST_NET_STR_ID - 1;
70 		break;
71 	}
72 }
73 
74 __private_extern__ errno_t
net_str_id_find_internal(const char * string,u_int32_t * out_id,u_int32_t kind,int create)75 net_str_id_find_internal(const char *string, u_int32_t *out_id,
76     u_int32_t kind, int create)
77 {
78 	struct net_str_id_entry                 *entry = NULL;
79 
80 
81 	if (string == NULL || out_id == NULL || kind >= NSI_MAX_KIND) {
82 		return EINVAL;
83 	}
84 	if (strlen(string) > NET_ID_STR_MAX_LEN) {
85 		return EINVAL;
86 	}
87 
88 	*out_id = 0;
89 
90 	/* Look for an existing entry */
91 	lck_mtx_lock(&net_str_id_lock);
92 	SLIST_FOREACH(entry, &net_str_id_list, nsi_next) {
93 		if (strlcmp(entry->nsi_string, string, entry->nsi_length) == 0) {
94 			break;
95 		}
96 	}
97 
98 	if (entry == NULL) {
99 		if (create == 0) {
100 			lck_mtx_unlock(&net_str_id_lock);
101 			return ENOENT;
102 		}
103 
104 		const uint32_t string_length = (uint32_t)strlen(string) + 1;
105 		entry = zalloc_permanent(sizeof(*entry) + string_length,
106 		    ZALIGN_PTR);
107 		if (entry == NULL) {
108 			lck_mtx_unlock(&net_str_id_lock);
109 			return ENOMEM;
110 		}
111 
112 		strlcpy(entry->nsi_string, string, string_length);
113 		entry->nsi_length = string_length;
114 		entry->nsi_flags = (1 << kind);
115 		entry->nsi_id = nsi_next_id++;
116 		nsi_kind_next[kind] = nsi_next_id;
117 		SLIST_INSERT_HEAD(&net_str_id_list, entry, nsi_next);
118 	} else if ((entry->nsi_flags & (1 << kind)) == 0) {
119 		if (create == 0) {
120 			lck_mtx_unlock(&net_str_id_lock);
121 			return ENOENT;
122 		}
123 		entry->nsi_flags |= (1 << kind);
124 		if (entry->nsi_id >= nsi_kind_next[kind]) {
125 			nsi_kind_next[kind] = entry->nsi_id + 1;
126 		}
127 	}
128 	lck_mtx_unlock(&net_str_id_lock);
129 
130 	*out_id = entry->nsi_id;
131 
132 	return 0;
133 }
134