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