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