]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_mib.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / net / if_mib.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright 1996 Massachusetts Institute of Technology
32 *
33 * Permission to use, copy, modify, and distribute this software and
34 * its documentation for any purpose and without fee is hereby
35 * granted, provided that both the above copyright notice and this
36 * permission notice appear in all copies, that both the above
37 * copyright notice and this permission notice appear in all
38 * supporting documentation, and that the name of M.I.T. not be used
39 * in advertising or publicity pertaining to distribution of the
40 * software without specific, written prior permission. M.I.T. makes
41 * no representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied
43 * warranty.
44 *
45 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
46 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
47 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
48 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
49 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * $FreeBSD: src/sys/net/if_mib.c,v 1.8.2.1 2000/08/03 00:09:34 ps Exp $
59 */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
67
68 #include <net/if.h>
69 #include <net/if_mib.h>
70
71 #if NETMIBS
72
73 /*
74 * A sysctl(3) MIB for generic interface information. This information
75 * is exported in the net.link.generic branch, which has the following
76 * structure:
77 *
78 * net.link.generic .system - system-wide control variables
79 * and statistics (node)
80 * .ifdata.<ifindex>.general
81 * - what's in `struct ifdata'
82 * plus some other info
83 * .ifdata.<ifindex>.linkspecific
84 * - a link-type-specific data
85 * structure (as might be used
86 * by an SNMP agent
87 *
88 * Perhaps someday we will make addresses accessible via this interface
89 * as well (then there will be four such...). The reason that the
90 * index comes before the last element in the name is because it
91 * seems more orthogonal that way, particularly with the possibility
92 * of other per-interface data living down here as well (e.g., integrated
93 * services stuff).
94 */
95
96 SYSCTL_DECL(_net_link_generic);
97
98 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RD, 0,
99 "Variables global to all interfaces");
100
101 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
102 &if_index, 0, "Number of configured interfaces");
103
104 static int sysctl_ifdata SYSCTL_HANDLER_ARGS;
105 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RD,
106 sysctl_ifdata, "Interface table");
107
108 static int sysctl_ifalldata SYSCTL_HANDLER_ARGS;
109 SYSCTL_NODE(_net_link_generic, IFMIB_IFALLDATA, ifalldata, CTLFLAG_RD,
110 sysctl_ifalldata, "Interface table");
111
112 static int make_ifmibdata(struct ifnet *, int *, struct sysctl_req *);
113
114
115 int
116 make_ifmibdata(struct ifnet *ifp, int *name, struct sysctl_req *req)
117 {
118 struct ifmibdata ifmd;
119 int error = 0;
120
121 switch(name[1]) {
122 default:
123 error = ENOENT;
124 break;
125
126 case IFDATA_GENERAL:
127
128 bzero(&ifmd, sizeof(ifmd));
129 snprintf(ifmd.ifmd_name, sizeof(ifmd.ifmd_name), "%s%d",
130 ifp->if_name, ifp->if_unit);
131
132 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
133 COPY(pcount);
134 COPY(flags);
135 if_data_internal_to_if_data64(&ifp->if_data, &ifmd.ifmd_data);
136 #undef COPY
137 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
138 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
139 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
140
141 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
142 if (error || !req->newptr)
143 break;
144
145 #ifdef IF_MIB_WR
146 error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
147 if (error)
148 break;
149
150 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
151 DONTCOPY(type);
152 DONTCOPY(physical);
153 DONTCOPY(addrlen);
154 DONTCOPY(hdrlen);
155 DONTCOPY(mtu);
156 DONTCOPY(metric);
157 DONTCOPY(baudrate);
158 #undef DONTCOPY
159 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
160 COPY(data);
161 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
162 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
163 #undef COPY
164 #endif /* IF_MIB_WR */
165 break;
166
167 case IFDATA_LINKSPECIFIC:
168 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
169 if (error || !req->newptr)
170 break;
171
172 #ifdef IF_MIB_WR
173 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
174 if (error)
175 break;
176 #endif /* IF_MIB_WR */
177 break;
178 }
179
180 return error;
181 }
182
183 int
184 sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
185 {
186 int *name = (int *)arg1;
187 int error = 0;
188 u_int namelen = arg2;
189 struct ifnet *ifp;
190
191 if (namelen != 2)
192 return EINVAL;
193
194 if (name[0] <= 0 || name[0] > if_index ||
195 (ifp = ifindex2ifnet[name[0]]) == NULL)
196 return ENOENT;
197
198 ifnet_lock_shared(ifp);
199
200 error = make_ifmibdata(ifp, name, req);
201
202 ifnet_lock_done(ifp);
203
204 return error;
205 }
206
207 int
208 sysctl_ifalldata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
209 {
210 int *name = (int *)arg1;
211 int error = 0;
212 u_int namelen = arg2;
213 struct ifnet *ifp;
214
215 if (namelen != 2)
216 return EINVAL;
217
218 ifnet_head_lock_shared();
219 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
220 ifnet_lock_shared(ifp);
221
222 error = make_ifmibdata(ifp, name, req);
223
224 ifnet_lock_done(ifp);
225 if (error)
226 break;
227 }
228 ifnet_head_done();
229 return error;
230 }
231
232 #endif