xref: /xnu-10002.61.3/bsd/net/if_mib.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2000-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  * Copyright 1996 Massachusetts Institute of Technology
30  *
31  * Permission to use, copy, modify, and distribute this software and
32  * its documentation for any purpose and without fee is hereby
33  * granted, provided that both the above copyright notice and this
34  * permission notice appear in all copies, that both the above
35  * copyright notice and this permission notice appear in all
36  * supporting documentation, and that the name of M.I.T. not be used
37  * in advertising or publicity pertaining to distribution of the
38  * software without specific, written prior permission.  M.I.T. makes
39  * no representations about the suitability of this software for any
40  * purpose.  It is provided "as is" without express or implied
41  * warranty.
42  *
43  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
44  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
45  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
46  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
47  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
50  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
53  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  *
56  * $FreeBSD: src/sys/net/if_mib.c,v 1.8.2.1 2000/08/03 00:09:34 ps Exp $
57  */
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/socket.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
65 
66 #include <net/if.h>
67 #include <net/if_mib.h>
68 #include <net/if_var.h>
69 
70 #include <os/log.h>
71 
72 /*
73  * A sysctl(3) MIB for generic interface information.  This information
74  * is exported in the net.link.generic branch, which has the following
75  * structure:
76  *
77  * net.link.generic	.system			- system-wide control variables
78  *						  and statistics (node)
79  *			.ifdata.<ifindex>.general
80  *						- what's in `struct ifdata'
81  *						  plus some other info
82  *			.ifdata.<ifindex>.linkspecific
83  *						- a link-type-specific data
84  *						  structure (as might be used
85  *						  by an SNMP agent
86  *
87  * Perhaps someday we will make addresses accessible via this interface
88  * as well (then there will be four such...).  The reason that the
89  * index comes before the last element in the name is because it
90  * seems more orthogonal that way, particularly with the possibility
91  * of other per-interface data living down here as well (e.g., integrated
92  * services stuff).
93  */
94 
95 SYSCTL_DECL(_net_link_generic);
96 
97 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RD | CTLFLAG_LOCKED, 0,
98     "Variables global to all interfaces");
99 
100 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD | CTLFLAG_LOCKED,
101     &if_index, 0, "Number of configured interfaces");
102 
103 static int sysctl_ifdata SYSCTL_HANDLER_ARGS;
104 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RD | CTLFLAG_LOCKED,
105     sysctl_ifdata, "Interface table");
106 
107 static int sysctl_ifalldata SYSCTL_HANDLER_ARGS;
108 SYSCTL_NODE(_net_link_generic, IFMIB_IFALLDATA, ifalldata, CTLFLAG_RD | CTLFLAG_LOCKED,
109     sysctl_ifalldata, "Interface table");
110 
111 static int make_ifmibdata(struct ifnet *, int *, struct sysctl_req *);
112 
113 int
make_ifmibdata(struct ifnet * ifp,int * name,struct sysctl_req * req)114 make_ifmibdata(struct ifnet *ifp, int *name, struct sysctl_req *req)
115 {
116 	struct ifmibdata        ifmd;
117 	int error = 0;
118 
119 	switch (name[1]) {
120 	default:
121 		error = ENOENT;
122 		break;
123 
124 	case IFDATA_GENERAL:
125 		bzero(&ifmd, sizeof(ifmd));
126 		/*
127 		 * Make sure the interface is in use
128 		 */
129 		if (ifnet_is_attached(ifp, 0)) {
130 			snprintf(ifmd.ifmd_name, sizeof(ifmd.ifmd_name), "%s",
131 			    if_name(ifp));
132 
133 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
134 			COPY(pcount);
135 			COPY(flags);
136 			if_data_internal_to_if_data64(ifp, &ifp->if_data, &ifmd.ifmd_data);
137 #undef COPY
138 			ifmd.ifmd_snd_len = IFCQ_LEN(ifp->if_snd);
139 			ifmd.ifmd_snd_maxlen = IFCQ_MAXLEN(ifp->if_snd);
140 			ifmd.ifmd_snd_drops =
141 			    (unsigned int)ifp->if_snd->ifcq_dropcnt.packets;
142 		}
143 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
144 		if (error || !req->newptr) {
145 			break;
146 		}
147 
148 #ifdef IF_MIB_WR
149 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
150 		if (error) {
151 			break;
152 		}
153 
154 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
155 		DONTCOPY(type);
156 		DONTCOPY(physical);
157 		DONTCOPY(addrlen);
158 		DONTCOPY(hdrlen);
159 		DONTCOPY(mtu);
160 		DONTCOPY(metric);
161 		DONTCOPY(baudrate);
162 #undef DONTCOPY
163 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
164 		COPY(data);
165 		ifp->if_snd->ifq_maxlen = ifmd.ifmd_snd_maxlen;
166 		ifp->if_snd->ifq_drops = ifmd.ifmd_snd_drops;
167 #undef COPY
168 #endif /* IF_MIB_WR */
169 		break;
170 
171 	case IFDATA_LINKSPECIFIC:
172 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
173 		if (error || !req->newptr) {
174 			break;
175 		}
176 
177 #ifdef IF_MIB_WR
178 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
179 		if (error) {
180 			break;
181 		}
182 #endif /* IF_MIB_WR */
183 		break;
184 
185 	case IFDATA_SUPPLEMENTAL: {
186 		struct ifmibdata_supplemental *ifmd_supp;
187 
188 		ifmd_supp = kalloc_type(struct ifmibdata_supplemental,
189 		    Z_WAITOK | Z_ZERO | Z_NOFAIL);
190 		if_copy_traffic_class(ifp, &ifmd_supp->ifmd_traffic_class);
191 		if_copy_data_extended(ifp, &ifmd_supp->ifmd_data_extended);
192 		if_copy_packet_stats(ifp, &ifmd_supp->ifmd_packet_stats);
193 		if_copy_rxpoll_stats(ifp, &ifmd_supp->ifmd_rxpoll_stats);
194 		if_copy_netif_stats(ifp, &ifmd_supp->ifmd_netif_stats);
195 
196 		if (req->oldptr == USER_ADDR_NULL) {
197 			req->oldlen = sizeof(*ifmd_supp);
198 		}
199 
200 		error = SYSCTL_OUT(req, ifmd_supp, MIN(sizeof(*ifmd_supp),
201 		    req->oldlen));
202 #if DEVELOPMENT || DEBUG
203 		if (error != 0) {
204 			os_log(OS_LOG_DEFAULT, "%s: IFDATA_SUPPLEMENTAL SYSCTL_OUT(MIN(%lu, %lu) failed with error %d",
205 			    __func__, sizeof(*ifmd_supp), req->oldlen, error);
206 		}
207 #endif /* DEVELOPMENT || DEBUG */
208 
209 		kfree_type(struct ifmibdata_supplemental, ifmd_supp);
210 		break;
211 	}
212 	}
213 
214 	return error;
215 }
216 
217 int
218 sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
219 {
220 #pragma unused(oidp)
221 	int *name = (int *)arg1;
222 	int error = 0;
223 	u_int namelen = arg2;
224 	struct ifnet *ifp;
225 
226 	if (namelen != 2) {
227 		return EINVAL;
228 	}
229 
230 	ifnet_head_lock_shared();
231 	if (name[0] <= 0 || name[0] > if_index ||
232 	    (ifp = ifindex2ifnet[name[0]]) == NULL) {
233 		ifnet_head_done();
234 		return ENOENT;
235 	}
236 	ifnet_reference(ifp);
237 	ifnet_head_done();
238 
239 	ifnet_lock_shared(ifp);
240 	error = make_ifmibdata(ifp, name, req);
241 	ifnet_lock_done(ifp);
242 
243 	ifnet_release(ifp);
244 
245 	return error;
246 }
247 
248 int
249 sysctl_ifalldata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
250 {
251 #pragma unused(oidp)
252 	int *name = (int *)arg1;
253 	int error = 0;
254 	u_int namelen = arg2;
255 	struct ifnet *ifp;
256 
257 	if (namelen != 2) {
258 		return EINVAL;
259 	}
260 
261 	ifnet_head_lock_shared();
262 	TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
263 		ifnet_lock_shared(ifp);
264 
265 		error = make_ifmibdata(ifp, name, req);
266 
267 		ifnet_lock_done(ifp);
268 		if (error != 0) {
269 			break;
270 		}
271 	}
272 	ifnet_head_done();
273 	return error;
274 }
275