if_mib.c revision 22975
1/*
2 * Copyright 1996 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	$Id$
30 */
31
32#include <sys/param.h>
33#include <sys/queue.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/errno.h>
37#include <sys/socket.h>
38#include <sys/sysctl.h>
39
40#include <net/if.h>
41#include <net/if_dl.h>
42#include <net/if_mib.h>
43#include <net/if_types.h>
44
45/*
46 * A sysctl(3) MIB for generic interface information.  This information
47 * is exported in the net.link.generic branch, which has the following
48 * structure:
49 *
50 * net.link.generic	.system			- system-wide control variables
51 *						  and statistics (node)
52 *			.ifdata.<ifindex>.general
53 *						- what's in `struct ifdata'
54 *						  plus some other info
55 *			.ifdata.<ifindex>.linkspecific
56 *						- a link-type-specific data
57 *						  structure (as might be used
58 *						  by an SNMP agent
59 *
60 * Perhaps someday we will make addresses accessible via this interface
61 * as well (then there will be four such...).  The reason that the
62 * index comes before the last element in the name is because it
63 * seems more orthogonal that way, particularly with the possibility
64 * of other per-interface data living down here as well (e.g., integrated
65 * services stuff).
66 */
67
68SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
69	    "Variables global to all interfaces");
70SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
71	   &if_index, 0, "Number of configured interfaces");
72
73static int
74sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
75{
76	int *name = (int *)arg1;
77	int error, ifnlen;
78	u_int namelen = arg2;
79	struct ifnet *ifp;
80	char workbuf[64];
81	struct ifmibdata ifmd;
82
83	if (namelen != 2)
84		return EINVAL;
85
86	if (name[0] <= 0 || name[0] > if_index)
87		return ENOENT;
88
89	ifp = ifnet_addrs[name[0] - 1]->ifa_ifp;
90
91	switch(name[1]) {
92	default:
93		return ENOENT;
94
95	case IFDATA_GENERAL:
96		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
97		if(ifnlen + 1 > sizeof ifmd.ifmd_name) {
98			return ENAMETOOLONG;
99		} else {
100			strcpy(ifmd.ifmd_name, workbuf);
101		}
102
103#define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
104		COPY(pcount);
105		COPY(flags);
106		COPY(data);
107#undef COPY
108		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
109		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
110		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
111
112		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
113		if (error || !req->newptr)
114			return error;
115
116		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
117		if (error)
118			return error;
119
120#define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
121		DONTCOPY(type);
122		DONTCOPY(physical);
123		DONTCOPY(addrlen);
124		DONTCOPY(hdrlen);
125		DONTCOPY(mtu);
126		DONTCOPY(metric);
127		DONTCOPY(baudrate);
128#undef DONTCOPY
129#define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
130		COPY(data);
131		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
132		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
133#undef COPY
134		break;
135
136	case IFDATA_LINKSPECIFIC:
137		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
138		if (error || !req->newptr)
139			return error;
140
141		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
142		if (error)
143			return error;
144
145	}
146	return 0;
147}
148
149SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
150	    sysctl_ifdata, "Interface table");
151
152