xref: /freebsd/sys/net/if_mib.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
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);
68*7029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system,
69*7029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
70bbd17bf8SGarrett Wollman     "Variables global to all interfaces");
718b615593SMarko Zec 
726df8a710SGleb Smirnoff SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount,
736df8a710SGleb Smirnoff 	CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(if_index), 0,
748b615593SMarko Zec 	"Number of configured interfaces");
75bbd17bf8SGarrett Wollman 
76bbd17bf8SGarrett Wollman static int
7782d9ae4eSPoul-Henning Kamp sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
78bbd17bf8SGarrett Wollman {
79bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
809bf40edeSBrooks Davis 	int error;
81bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
82bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
83bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
84caeeeaa7SGleb Smirnoff 	struct epoch_tracker et;
8515450897SHartmut Brandt 	size_t dlen;
8615450897SHartmut Brandt 	char *dbuf;
87bbd17bf8SGarrett Wollman 
88bbd17bf8SGarrett Wollman 	if (namelen != 2)
89bbd17bf8SGarrett Wollman 		return EINVAL;
9027d37320SRobert Watson 	if (name[0] <= 0)
9127d37320SRobert Watson 		return (ENOENT);
92caeeeaa7SGleb Smirnoff 	NET_EPOCH_ENTER(et);
9327d37320SRobert Watson 	ifp = ifnet_byindex_ref(name[0]);
94caeeeaa7SGleb Smirnoff 	NET_EPOCH_EXIT(et);
9527d37320SRobert Watson 	if (ifp == NULL)
9627d37320SRobert Watson 		return (ENOENT);
97bbd17bf8SGarrett Wollman 
98bbd17bf8SGarrett Wollman 	switch(name[1]) {
99bbd17bf8SGarrett Wollman 	default:
10027d37320SRobert Watson 		error = ENOENT;
10127d37320SRobert Watson 		goto out;
102bbd17bf8SGarrett Wollman 
103bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
104fd94099eSColin Percival 		bzero(&ifmd, sizeof(ifmd));
1059bf40edeSBrooks Davis 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
106bbd17bf8SGarrett Wollman 
107e6485f73SGleb Smirnoff 		ifmd.ifmd_pcount = ifp->if_pcount;
108e6485f73SGleb Smirnoff 		if_data_copy(ifp, &ifmd.ifmd_data);
109e6485f73SGleb Smirnoff 
11057c1493bSChristian S.J. Peron 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
111bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
112bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
11356b61ca2SGleb Smirnoff 		ifmd.ifmd_snd_drops =
11456b61ca2SGleb Smirnoff 		    ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
115bbd17bf8SGarrett Wollman 
116bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
117bbd17bf8SGarrett Wollman 		if (error)
11827d37320SRobert Watson 			goto out;
119bbd17bf8SGarrett Wollman 		break;
120bbd17bf8SGarrett Wollman 
121bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
122bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
123bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
12427d37320SRobert Watson 			goto out;
125bbd17bf8SGarrett Wollman 
126bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
127bbd17bf8SGarrett Wollman 		if (error)
12827d37320SRobert Watson 			goto out;
1299d53bbefSBruce M Simpson 		break;
130bbd17bf8SGarrett Wollman 
13115450897SHartmut Brandt 	case IFDATA_DRIVERNAME:
13215450897SHartmut Brandt 		/* 20 is enough for 64bit ints */
13315450897SHartmut Brandt 		dlen = strlen(ifp->if_dname) + 20 + 1;
13427d37320SRobert Watson 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
13527d37320SRobert Watson 			error = ENOMEM;
13627d37320SRobert Watson 			goto out;
13727d37320SRobert Watson 		}
13815450897SHartmut Brandt 		if (ifp->if_dunit == IF_DUNIT_NONE)
13915450897SHartmut Brandt 			strcpy(dbuf, ifp->if_dname);
14015450897SHartmut Brandt 		else
14115450897SHartmut Brandt 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
14215450897SHartmut Brandt 
14315450897SHartmut Brandt 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
14415450897SHartmut Brandt 		if (error == 0 && req->newptr != NULL)
14515450897SHartmut Brandt 			error = EPERM;
14615450897SHartmut Brandt 		free(dbuf, M_TEMP);
14727d37320SRobert Watson 		goto out;
148bbd17bf8SGarrett Wollman 	}
14927d37320SRobert Watson out:
15027d37320SRobert Watson 	if_rele(ifp);
15127d37320SRobert Watson 	return error;
152bbd17bf8SGarrett Wollman }
153bbd17bf8SGarrett Wollman 
154*7029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata,
155*7029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_NEEDGIANT, sysctl_ifdata,
156*7029da5cSPawel Biernacki     "Interface table");
157bbd17bf8SGarrett Wollman 
158