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