xref: /freebsd/sys/net/if_mib.c (revision caeeeaa7c5db11624a8df5af1f0bfd4fd70bae7e)
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>
358ec07310SGleb Smirnoff #include <sys/malloc.h>
364458ac71SBruce Evans #include <sys/socket.h>
37bbd17bf8SGarrett Wollman #include <sys/sysctl.h>
38bbd17bf8SGarrett Wollman 
39bbd17bf8SGarrett Wollman #include <net/if.h>
4076039bc8SGleb Smirnoff #include <net/if_var.h>
41bbd17bf8SGarrett Wollman #include <net/if_mib.h>
424b79449eSBjoern A. Zeeb #include <net/vnet.h>
43bbd17bf8SGarrett Wollman 
44bbd17bf8SGarrett Wollman /*
45bbd17bf8SGarrett Wollman  * A sysctl(3) MIB for generic interface information.  This information
46bbd17bf8SGarrett Wollman  * is exported in the net.link.generic branch, which has the following
47bbd17bf8SGarrett Wollman  * structure:
48bbd17bf8SGarrett Wollman  *
49bbd17bf8SGarrett Wollman  * net.link.generic	.system			- system-wide control variables
50bbd17bf8SGarrett Wollman  *						  and statistics (node)
51bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.general
52bbd17bf8SGarrett Wollman  *						- what's in `struct ifdata'
53bbd17bf8SGarrett Wollman  *						  plus some other info
54bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.linkspecific
55bbd17bf8SGarrett Wollman  *						- a link-type-specific data
56bbd17bf8SGarrett Wollman  *						  structure (as might be used
57bbd17bf8SGarrett Wollman  *						  by an SNMP agent
58bbd17bf8SGarrett Wollman  *
59bbd17bf8SGarrett Wollman  * Perhaps someday we will make addresses accessible via this interface
60bbd17bf8SGarrett Wollman  * as well (then there will be four such...).  The reason that the
61bbd17bf8SGarrett Wollman  * index comes before the last element in the name is because it
62bbd17bf8SGarrett Wollman  * seems more orthogonal that way, particularly with the possibility
63bbd17bf8SGarrett Wollman  * of other per-interface data living down here as well (e.g., integrated
64bbd17bf8SGarrett Wollman  * services stuff).
65bbd17bf8SGarrett Wollman  */
66bbd17bf8SGarrett Wollman 
67ce02431fSDoug Rabson SYSCTL_DECL(_net_link_generic);
686472ac3dSEd Schouten static SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
69bbd17bf8SGarrett Wollman 	    "Variables global to all interfaces");
708b615593SMarko Zec 
716df8a710SGleb Smirnoff SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
726df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(if_index), 0,
738b615593SMarko Zec 	"Number of configured interfaces");
74bbd17bf8SGarrett Wollman 
75bbd17bf8SGarrett Wollman static int
7682d9ae4eSPoul-Henning Kamp sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
77bbd17bf8SGarrett Wollman {
78bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
799bf40edeSBrooks Davis 	int error;
80bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
81bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
82bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
83*caeeeaa7SGleb Smirnoff 	struct epoch_tracker et;
8415450897SHartmut Brandt 	size_t dlen;
8515450897SHartmut Brandt 	char *dbuf;
86bbd17bf8SGarrett Wollman 
87bbd17bf8SGarrett Wollman 	if (namelen != 2)
88bbd17bf8SGarrett Wollman 		return EINVAL;
8927d37320SRobert Watson 	if (name[0] <= 0)
9027d37320SRobert Watson 		return (ENOENT);
91*caeeeaa7SGleb Smirnoff 	NET_EPOCH_ENTER(et);
9227d37320SRobert Watson 	ifp = ifnet_byindex_ref(name[0]);
93*caeeeaa7SGleb Smirnoff 	NET_EPOCH_EXIT(et);
9427d37320SRobert Watson 	if (ifp == NULL)
9527d37320SRobert Watson 		return (ENOENT);
96bbd17bf8SGarrett Wollman 
97bbd17bf8SGarrett Wollman 	switch(name[1]) {
98bbd17bf8SGarrett Wollman 	default:
9927d37320SRobert Watson 		error = ENOENT;
10027d37320SRobert Watson 		goto out;
101bbd17bf8SGarrett Wollman 
102bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
103fd94099eSColin Percival 		bzero(&ifmd, sizeof(ifmd));
1049bf40edeSBrooks Davis 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
105bbd17bf8SGarrett Wollman 
106e6485f73SGleb Smirnoff 		ifmd.ifmd_pcount = ifp->if_pcount;
107e6485f73SGleb Smirnoff 		if_data_copy(ifp, &ifmd.ifmd_data);
108e6485f73SGleb Smirnoff 
10957c1493bSChristian S.J. Peron 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
110bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
111bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
11256b61ca2SGleb Smirnoff 		ifmd.ifmd_snd_drops =
11356b61ca2SGleb Smirnoff 		    ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
114bbd17bf8SGarrett Wollman 
115bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
116bbd17bf8SGarrett Wollman 		if (error)
11727d37320SRobert Watson 			goto out;
118bbd17bf8SGarrett Wollman 		break;
119bbd17bf8SGarrett Wollman 
120bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
121bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
122bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
12327d37320SRobert Watson 			goto out;
124bbd17bf8SGarrett Wollman 
125bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
126bbd17bf8SGarrett Wollman 		if (error)
12727d37320SRobert Watson 			goto out;
1289d53bbefSBruce M Simpson 		break;
129bbd17bf8SGarrett Wollman 
13015450897SHartmut Brandt 	case IFDATA_DRIVERNAME:
13115450897SHartmut Brandt 		/* 20 is enough for 64bit ints */
13215450897SHartmut Brandt 		dlen = strlen(ifp->if_dname) + 20 + 1;
13327d37320SRobert Watson 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
13427d37320SRobert Watson 			error = ENOMEM;
13527d37320SRobert Watson 			goto out;
13627d37320SRobert Watson 		}
13715450897SHartmut Brandt 		if (ifp->if_dunit == IF_DUNIT_NONE)
13815450897SHartmut Brandt 			strcpy(dbuf, ifp->if_dname);
13915450897SHartmut Brandt 		else
14015450897SHartmut Brandt 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
14115450897SHartmut Brandt 
14215450897SHartmut Brandt 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
14315450897SHartmut Brandt 		if (error == 0 && req->newptr != NULL)
14415450897SHartmut Brandt 			error = EPERM;
14515450897SHartmut Brandt 		free(dbuf, M_TEMP);
14627d37320SRobert Watson 		goto out;
147bbd17bf8SGarrett Wollman 	}
14827d37320SRobert Watson out:
14927d37320SRobert Watson 	if_rele(ifp);
15027d37320SRobert Watson 	return error;
151bbd17bf8SGarrett Wollman }
152bbd17bf8SGarrett Wollman 
1536472ac3dSEd Schouten static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
154bbd17bf8SGarrett Wollman 	    sysctl_ifdata, "Interface table");
155bbd17bf8SGarrett Wollman 
156