]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_mib.c
xnu-1504.9.26.tar.gz
[apple/xnu.git] / bsd / net / if_mib.c
CommitLineData
1c79356b 1/*
d1ecb069 2 * Copyright (c) 2000-2009 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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 *
9bccf70c 56 * $FreeBSD: src/sys/net/if_mib.c,v 1.8.2.1 2000/08/03 00:09:34 ps Exp $
1c79356b
A
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>
91447636 64#include <sys/systm.h>
1c79356b
A
65
66#include <net/if.h>
67#include <net/if_mib.h>
68
69#if NETMIBS
70
71/*
72 * A sysctl(3) MIB for generic interface information. This information
73 * is exported in the net.link.generic branch, which has the following
74 * structure:
75 *
76 * net.link.generic .system - system-wide control variables
77 * and statistics (node)
78 * .ifdata.<ifindex>.general
79 * - what's in `struct ifdata'
80 * plus some other info
81 * .ifdata.<ifindex>.linkspecific
82 * - a link-type-specific data
83 * structure (as might be used
84 * by an SNMP agent
85 *
86 * Perhaps someday we will make addresses accessible via this interface
87 * as well (then there will be four such...). The reason that the
88 * index comes before the last element in the name is because it
89 * seems more orthogonal that way, particularly with the possibility
90 * of other per-interface data living down here as well (e.g., integrated
91 * services stuff).
92 */
93
94SYSCTL_DECL(_net_link_generic);
91447636 95
2d21ac55 96SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RD|CTLFLAG_LOCKED, 0,
1c79356b 97 "Variables global to all interfaces");
91447636 98
1c79356b
A
99SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
100 &if_index, 0, "Number of configured interfaces");
101
91447636
A
102static int sysctl_ifdata SYSCTL_HANDLER_ARGS;
103SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RD,
104 sysctl_ifdata, "Interface table");
1c79356b 105
91447636
A
106static int sysctl_ifalldata SYSCTL_HANDLER_ARGS;
107SYSCTL_NODE(_net_link_generic, IFMIB_IFALLDATA, ifalldata, CTLFLAG_RD,
108 sysctl_ifalldata, "Interface table");
1c79356b 109
2d21ac55
A
110extern int dlil_multithreaded_input;
111SYSCTL_INT(_net_link_generic_system, OID_AUTO, multi_threaded_input, CTLFLAG_RW,
112 &dlil_multithreaded_input , 0, "Uses multiple input thread for DLIL input");
113#ifdef IFNET_INPUT_SANITY_CHK
114extern int dlil_input_sanity_check;
115SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_input_sanity_check, CTLFLAG_RW,
116 &dlil_input_sanity_check , 0, "Turn on sanity checking in DLIL input");
117#endif
118
b0d623f7
A
119extern int dlil_verbose;
120SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_verbose, CTLFLAG_RW,
121 &dlil_verbose, 0, "Log DLIL error messages");
122
123
91447636 124static int make_ifmibdata(struct ifnet *, int *, struct sysctl_req *);
1c79356b 125
91447636
A
126int
127make_ifmibdata(struct ifnet *ifp, int *name, struct sysctl_req *req)
128{
129 struct ifmibdata ifmd;
130 int error = 0;
131
1c79356b
A
132 switch(name[1]) {
133 default:
91447636
A
134 error = ENOENT;
135 break;
1c79356b
A
136
137 case IFDATA_GENERAL:
3a60a9f5 138 bzero(&ifmd, sizeof(ifmd));
d1ecb069
A
139 /*
140 * Make sure the interface is in use
141 */
142 if (ifp->if_refcnt > 0) {
143 snprintf(ifmd.ifmd_name, sizeof(ifmd.ifmd_name), "%s%d",
144 ifp->if_name, ifp->if_unit);
145
1c79356b 146#define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
d1ecb069
A
147 COPY(pcount);
148 COPY(flags);
149 if_data_internal_to_if_data64(ifp, &ifp->if_data, &ifmd.ifmd_data);
1c79356b 150#undef COPY
d1ecb069
A
151 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
152 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
153 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
154#if PKT_PRIORITY
155 /* stuff these into unused fields for now */
156 ifmd.ifmd_filler[0] = ifp->if_obgpackets;
157 ifmd.ifmd_filler[1] = ifp->if_obgbytes;
158#endif /* PKT_PRIORITY */
159 }
1c79356b
A
160 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
161 if (error || !req->newptr)
91447636 162 break;
1c79356b 163
91447636 164#ifdef IF_MIB_WR
1c79356b
A
165 error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
166 if (error)
91447636 167 break;
1c79356b
A
168
169#define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
170 DONTCOPY(type);
171 DONTCOPY(physical);
172 DONTCOPY(addrlen);
173 DONTCOPY(hdrlen);
174 DONTCOPY(mtu);
175 DONTCOPY(metric);
176 DONTCOPY(baudrate);
177#undef DONTCOPY
178#define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
179 COPY(data);
180 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
181 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
182#undef COPY
91447636 183#endif /* IF_MIB_WR */
1c79356b
A
184 break;
185
186 case IFDATA_LINKSPECIFIC:
187 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
188 if (error || !req->newptr)
91447636 189 break;
1c79356b 190
91447636 191#ifdef IF_MIB_WR
1c79356b
A
192 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
193 if (error)
91447636
A
194 break;
195#endif /* IF_MIB_WR */
196 break;
1c79356b 197 }
91447636
A
198
199 return error;
200}
201
202int
203sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
204{
d1ecb069 205#pragma unused(oidp)
91447636
A
206 int *name = (int *)arg1;
207 int error = 0;
208 u_int namelen = arg2;
209 struct ifnet *ifp;
210
211 if (namelen != 2)
212 return EINVAL;
b0d623f7 213 ifnet_head_lock_shared();
91447636 214 if (name[0] <= 0 || name[0] > if_index ||
d1ecb069
A
215 (ifp = ifindex2ifnet[name[0]]) == NULL ||
216 ifp->if_refcnt == 0) {
b0d623f7 217 ifnet_head_done();
91447636 218 return ENOENT;
b0d623f7
A
219 }
220 ifnet_head_done();
91447636
A
221
222 ifnet_lock_shared(ifp);
223
224 error = make_ifmibdata(ifp, name, req);
225
226 ifnet_lock_done(ifp);
227
228 return error;
1c79356b
A
229}
230
91447636
A
231int
232sysctl_ifalldata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
233{
d1ecb069 234#pragma unused(oidp)
91447636
A
235 int *name = (int *)arg1;
236 int error = 0;
237 u_int namelen = arg2;
238 struct ifnet *ifp;
239
240 if (namelen != 2)
241 return EINVAL;
242
243 ifnet_head_lock_shared();
244 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
245 ifnet_lock_shared(ifp);
246
247 error = make_ifmibdata(ifp, name, req);
248
249 ifnet_lock_done(ifp);
250 if (error)
251 break;
252 }
253 ifnet_head_done();
254 return error;
255}
1c79356b
A
256
257#endif