]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_mib.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / bsd / net / if_mib.c
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 2000-2011 Apple 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>
d41d1dae 68#include <net/if_var.h>
1c79356b 69
1c79356b
A
70/*
71 * A sysctl(3) MIB for generic interface information. This information
72 * is exported in the net.link.generic branch, which has the following
73 * structure:
74 *
75 * net.link.generic .system - system-wide control variables
76 * and statistics (node)
77 * .ifdata.<ifindex>.general
78 * - what's in `struct ifdata'
79 * plus some other info
80 * .ifdata.<ifindex>.linkspecific
81 * - a link-type-specific data
82 * structure (as might be used
83 * by an SNMP agent
84 *
85 * Perhaps someday we will make addresses accessible via this interface
86 * as well (then there will be four such...). The reason that the
87 * index comes before the last element in the name is because it
88 * seems more orthogonal that way, particularly with the possibility
89 * of other per-interface data living down here as well (e.g., integrated
90 * services stuff).
91 */
92
93SYSCTL_DECL(_net_link_generic);
91447636 94
2d21ac55 95SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RD|CTLFLAG_LOCKED, 0,
1c79356b 96 "Variables global to all interfaces");
91447636 97
6d2010ae 98SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD | CTLFLAG_LOCKED,
1c79356b
A
99 &if_index, 0, "Number of configured interfaces");
100
91447636 101static int sysctl_ifdata SYSCTL_HANDLER_ARGS;
6d2010ae 102SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RD | CTLFLAG_LOCKED,
91447636 103 sysctl_ifdata, "Interface table");
1c79356b 104
91447636 105static int sysctl_ifalldata SYSCTL_HANDLER_ARGS;
6d2010ae 106SYSCTL_NODE(_net_link_generic, IFMIB_IFALLDATA, ifalldata, CTLFLAG_RD | CTLFLAG_LOCKED,
91447636 107 sysctl_ifalldata, "Interface table");
1c79356b 108
91447636 109static int make_ifmibdata(struct ifnet *, int *, struct sysctl_req *);
1c79356b 110
91447636
A
111int
112make_ifmibdata(struct ifnet *ifp, int *name, struct sysctl_req *req)
113{
114 struct ifmibdata ifmd;
115 int error = 0;
116
1c79356b
A
117 switch(name[1]) {
118 default:
91447636
A
119 error = ENOENT;
120 break;
1c79356b
A
121
122 case IFDATA_GENERAL:
3a60a9f5 123 bzero(&ifmd, sizeof(ifmd));
d1ecb069
A
124 /*
125 * Make sure the interface is in use
126 */
6d2010ae 127 if (ifnet_is_attached(ifp, 0)) {
d1ecb069
A
128 snprintf(ifmd.ifmd_name, sizeof(ifmd.ifmd_name), "%s%d",
129 ifp->if_name, ifp->if_unit);
130
1c79356b 131#define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
d1ecb069
A
132 COPY(pcount);
133 COPY(flags);
134 if_data_internal_to_if_data64(ifp, &ifp->if_data, &ifmd.ifmd_data);
1c79356b 135#undef COPY
d1ecb069
A
136 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
137 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
138 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
d1ecb069 139 }
1c79356b
A
140 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
141 if (error || !req->newptr)
91447636 142 break;
1c79356b 143
91447636 144#ifdef IF_MIB_WR
1c79356b
A
145 error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
146 if (error)
91447636 147 break;
1c79356b
A
148
149#define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
150 DONTCOPY(type);
151 DONTCOPY(physical);
152 DONTCOPY(addrlen);
153 DONTCOPY(hdrlen);
154 DONTCOPY(mtu);
155 DONTCOPY(metric);
156 DONTCOPY(baudrate);
157#undef DONTCOPY
158#define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
159 COPY(data);
160 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
161 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
162#undef COPY
91447636 163#endif /* IF_MIB_WR */
1c79356b
A
164 break;
165
166 case IFDATA_LINKSPECIFIC:
167 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
168 if (error || !req->newptr)
91447636 169 break;
1c79356b 170
91447636 171#ifdef IF_MIB_WR
1c79356b
A
172 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
173 if (error)
91447636
A
174 break;
175#endif /* IF_MIB_WR */
176 break;
d41d1dae 177
6d2010ae
A
178 case IFDATA_SUPPLEMENTAL: {
179 struct if_traffic_class if_tc;
180
181 if_copy_traffic_class(ifp, &if_tc);
182
183 error = SYSCTL_OUT(req, &if_tc, sizeof(struct if_traffic_class));
d41d1dae 184 break;
6d2010ae 185 }
1c79356b 186 }
91447636
A
187
188 return error;
189}
190
191int
192sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
193{
d1ecb069 194#pragma unused(oidp)
91447636
A
195 int *name = (int *)arg1;
196 int error = 0;
197 u_int namelen = arg2;
198 struct ifnet *ifp;
199
200 if (namelen != 2)
6d2010ae
A
201 return (EINVAL);
202
b0d623f7 203 ifnet_head_lock_shared();
91447636 204 if (name[0] <= 0 || name[0] > if_index ||
6d2010ae 205 (ifp = ifindex2ifnet[name[0]]) == NULL) {
b0d623f7 206 ifnet_head_done();
6d2010ae 207 return (ENOENT);
b0d623f7 208 }
6d2010ae 209 ifnet_reference(ifp);
b0d623f7 210 ifnet_head_done();
91447636
A
211
212 ifnet_lock_shared(ifp);
91447636 213 error = make_ifmibdata(ifp, name, req);
91447636 214 ifnet_lock_done(ifp);
6d2010ae
A
215
216 ifnet_release(ifp);
217
218 return (error);
1c79356b
A
219}
220
91447636
A
221int
222sysctl_ifalldata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
223{
d1ecb069 224#pragma unused(oidp)
91447636
A
225 int *name = (int *)arg1;
226 int error = 0;
227 u_int namelen = arg2;
228 struct ifnet *ifp;
229
230 if (namelen != 2)
6d2010ae 231 return (EINVAL);
91447636
A
232
233 ifnet_head_lock_shared();
234 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
235 ifnet_lock_shared(ifp);
6d2010ae 236
91447636 237 error = make_ifmibdata(ifp, name, req);
6d2010ae 238
91447636 239 ifnet_lock_done(ifp);
6d2010ae 240 if (error != 0)
91447636
A
241 break;
242 }
243 ifnet_head_done();
244 return error;
245}