xref: /freebsd/sys/net/if_mib.c (revision 9d53bbefbade7b04b3c3ac0a95499b700bdcfef7)
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>
37603724d3SBjoern A. Zeeb #include <sys/vimage.h>
38bbd17bf8SGarrett Wollman 
39bbd17bf8SGarrett Wollman #include <net/if.h>
40bbd17bf8SGarrett Wollman #include <net/if_mib.h>
41bbd17bf8SGarrett Wollman 
42bbd17bf8SGarrett Wollman /*
43bbd17bf8SGarrett Wollman  * A sysctl(3) MIB for generic interface information.  This information
44bbd17bf8SGarrett Wollman  * is exported in the net.link.generic branch, which has the following
45bbd17bf8SGarrett Wollman  * structure:
46bbd17bf8SGarrett Wollman  *
47bbd17bf8SGarrett Wollman  * net.link.generic	.system			- system-wide control variables
48bbd17bf8SGarrett Wollman  *						  and statistics (node)
49bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.general
50bbd17bf8SGarrett Wollman  *						- what's in `struct ifdata'
51bbd17bf8SGarrett Wollman  *						  plus some other info
52bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.linkspecific
53bbd17bf8SGarrett Wollman  *						- a link-type-specific data
54bbd17bf8SGarrett Wollman  *						  structure (as might be used
55bbd17bf8SGarrett Wollman  *						  by an SNMP agent
56bbd17bf8SGarrett Wollman  *
57bbd17bf8SGarrett Wollman  * Perhaps someday we will make addresses accessible via this interface
58bbd17bf8SGarrett Wollman  * as well (then there will be four such...).  The reason that the
59bbd17bf8SGarrett Wollman  * index comes before the last element in the name is because it
60bbd17bf8SGarrett Wollman  * seems more orthogonal that way, particularly with the possibility
61bbd17bf8SGarrett Wollman  * of other per-interface data living down here as well (e.g., integrated
62bbd17bf8SGarrett Wollman  * services stuff).
63bbd17bf8SGarrett Wollman  */
64bbd17bf8SGarrett Wollman 
65ce02431fSDoug Rabson SYSCTL_DECL(_net_link_generic);
66bbd17bf8SGarrett Wollman SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
67bbd17bf8SGarrett Wollman 	    "Variables global to all interfaces");
68bbd17bf8SGarrett Wollman SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
69bbd17bf8SGarrett Wollman 	   &if_index, 0, "Number of configured interfaces");
70bbd17bf8SGarrett Wollman 
71bbd17bf8SGarrett Wollman static int
7282d9ae4eSPoul-Henning Kamp sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
73bbd17bf8SGarrett Wollman {
74bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
759bf40edeSBrooks Davis 	int error;
76bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
77bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
78bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
7915450897SHartmut Brandt 	size_t dlen;
8015450897SHartmut Brandt 	char *dbuf;
81bbd17bf8SGarrett Wollman 
82bbd17bf8SGarrett Wollman 	if (namelen != 2)
83bbd17bf8SGarrett Wollman 		return EINVAL;
84bbd17bf8SGarrett Wollman 
85603724d3SBjoern A. Zeeb 	if (name[0] <= 0 || name[0] > V_if_index ||
86f0a2ef48SRuslan Ermilov 	    ifnet_byindex(name[0]) == NULL)
87bbd17bf8SGarrett Wollman 		return ENOENT;
88bbd17bf8SGarrett Wollman 
89f0a2ef48SRuslan Ermilov 	ifp = ifnet_byindex(name[0]);
90bbd17bf8SGarrett Wollman 
91bbd17bf8SGarrett Wollman 	switch(name[1]) {
92bbd17bf8SGarrett Wollman 	default:
93bbd17bf8SGarrett Wollman 		return ENOENT;
94bbd17bf8SGarrett Wollman 
95bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
96fd94099eSColin Percival 		bzero(&ifmd, sizeof(ifmd));
979bf40edeSBrooks Davis 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
98bbd17bf8SGarrett Wollman 
99bbd17bf8SGarrett Wollman #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
100bbd17bf8SGarrett Wollman 		COPY(pcount);
101bbd17bf8SGarrett Wollman 		COPY(data);
102bbd17bf8SGarrett Wollman #undef COPY
10357c1493bSChristian S.J. Peron 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
104bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
105bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
106bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
107bbd17bf8SGarrett Wollman 
108bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
109bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
110bbd17bf8SGarrett Wollman 			return error;
111bbd17bf8SGarrett Wollman 
112bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
113bbd17bf8SGarrett Wollman 		if (error)
114bbd17bf8SGarrett Wollman 			return error;
115bbd17bf8SGarrett Wollman 
116bbd17bf8SGarrett Wollman #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
117bbd17bf8SGarrett Wollman 		DONTCOPY(type);
118bbd17bf8SGarrett Wollman 		DONTCOPY(physical);
119bbd17bf8SGarrett Wollman 		DONTCOPY(addrlen);
120bbd17bf8SGarrett Wollman 		DONTCOPY(hdrlen);
121bbd17bf8SGarrett Wollman 		DONTCOPY(mtu);
122bbd17bf8SGarrett Wollman 		DONTCOPY(metric);
123bbd17bf8SGarrett Wollman 		DONTCOPY(baudrate);
124bbd17bf8SGarrett Wollman #undef DONTCOPY
125bbd17bf8SGarrett Wollman #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
126bbd17bf8SGarrett Wollman 		COPY(data);
127bbd17bf8SGarrett Wollman 		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
128bbd17bf8SGarrett Wollman 		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
129bbd17bf8SGarrett Wollman #undef COPY
130bbd17bf8SGarrett Wollman 		break;
131bbd17bf8SGarrett Wollman 
132bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
133bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
134bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
135bbd17bf8SGarrett Wollman 			return error;
136bbd17bf8SGarrett Wollman 
137bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
138bbd17bf8SGarrett Wollman 		if (error)
139bbd17bf8SGarrett Wollman 			return error;
1409d53bbefSBruce M Simpson 		break;
141bbd17bf8SGarrett Wollman 
14215450897SHartmut Brandt 	case IFDATA_DRIVERNAME:
14315450897SHartmut Brandt 		/* 20 is enough for 64bit ints */
14415450897SHartmut Brandt 		dlen = strlen(ifp->if_dname) + 20 + 1;
14515450897SHartmut Brandt 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL)
14615450897SHartmut Brandt 			return (ENOMEM);
14715450897SHartmut Brandt 		if (ifp->if_dunit == IF_DUNIT_NONE)
14815450897SHartmut Brandt 			strcpy(dbuf, ifp->if_dname);
14915450897SHartmut Brandt 		else
15015450897SHartmut Brandt 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
15115450897SHartmut Brandt 
15215450897SHartmut Brandt 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
15315450897SHartmut Brandt 		if (error == 0 && req->newptr != NULL)
15415450897SHartmut Brandt 			error = EPERM;
15515450897SHartmut Brandt 		free(dbuf, M_TEMP);
15615450897SHartmut Brandt 		return (error);
157bbd17bf8SGarrett Wollman 	}
158bbd17bf8SGarrett Wollman 	return 0;
159bbd17bf8SGarrett Wollman }
160bbd17bf8SGarrett Wollman 
161bbd17bf8SGarrett Wollman SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
162bbd17bf8SGarrett Wollman 	    sysctl_ifdata, "Interface table");
163bbd17bf8SGarrett Wollman 
164