xref: /freebsd/sys/net/if_mib.c (revision 7e0bba4d8083b6fac71180681b51aee9ed5fb664)
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 
67bbd17bf8SGarrett Wollman static int
6882d9ae4eSPoul-Henning Kamp sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
69bbd17bf8SGarrett Wollman {
70bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
719bf40edeSBrooks Davis 	int error;
72bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
73bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
74bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
75caeeeaa7SGleb Smirnoff 	struct epoch_tracker et;
7615450897SHartmut Brandt 	size_t dlen;
7715450897SHartmut Brandt 	char *dbuf;
78bbd17bf8SGarrett Wollman 
79bbd17bf8SGarrett Wollman 	if (namelen != 2)
80bbd17bf8SGarrett Wollman 		return EINVAL;
8127d37320SRobert Watson 	if (name[0] <= 0)
8227d37320SRobert Watson 		return (ENOENT);
83caeeeaa7SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8427d37320SRobert Watson 	ifp = ifnet_byindex_ref(name[0]);
85caeeeaa7SGleb Smirnoff 	NET_EPOCH_EXIT(et);
8627d37320SRobert Watson 	if (ifp == NULL)
8727d37320SRobert Watson 		return (ENOENT);
88bbd17bf8SGarrett Wollman 
89bbd17bf8SGarrett Wollman 	switch(name[1]) {
90bbd17bf8SGarrett Wollman 	default:
9127d37320SRobert Watson 		error = ENOENT;
9227d37320SRobert Watson 		goto out;
93bbd17bf8SGarrett Wollman 
94bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
95fd94099eSColin Percival 		bzero(&ifmd, sizeof(ifmd));
969bf40edeSBrooks Davis 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
97bbd17bf8SGarrett Wollman 
98e6485f73SGleb Smirnoff 		ifmd.ifmd_pcount = ifp->if_pcount;
99e6485f73SGleb Smirnoff 		if_data_copy(ifp, &ifmd.ifmd_data);
100e6485f73SGleb Smirnoff 
10157c1493bSChristian S.J. Peron 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
102bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
103bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
10456b61ca2SGleb Smirnoff 		ifmd.ifmd_snd_drops =
10556b61ca2SGleb Smirnoff 		    ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
106bbd17bf8SGarrett Wollman 
107bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
108bbd17bf8SGarrett Wollman 		if (error)
10927d37320SRobert Watson 			goto out;
110bbd17bf8SGarrett Wollman 		break;
111bbd17bf8SGarrett Wollman 
112bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
113bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
114bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
11527d37320SRobert Watson 			goto out;
1169d53bbefSBruce M Simpson 		break;
117bbd17bf8SGarrett Wollman 
11815450897SHartmut Brandt 	case IFDATA_DRIVERNAME:
11915450897SHartmut Brandt 		/* 20 is enough for 64bit ints */
12015450897SHartmut Brandt 		dlen = strlen(ifp->if_dname) + 20 + 1;
12127d37320SRobert Watson 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
12227d37320SRobert Watson 			error = ENOMEM;
12327d37320SRobert Watson 			goto out;
12427d37320SRobert Watson 		}
12515450897SHartmut Brandt 		if (ifp->if_dunit == IF_DUNIT_NONE)
12615450897SHartmut Brandt 			strcpy(dbuf, ifp->if_dname);
12715450897SHartmut Brandt 		else
12815450897SHartmut Brandt 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
12915450897SHartmut Brandt 
13015450897SHartmut Brandt 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
13115450897SHartmut Brandt 		if (error == 0 && req->newptr != NULL)
13215450897SHartmut Brandt 			error = EPERM;
13315450897SHartmut Brandt 		free(dbuf, M_TEMP);
13427d37320SRobert Watson 		goto out;
135bbd17bf8SGarrett Wollman 	}
13627d37320SRobert Watson out:
13727d37320SRobert Watson 	if_rele(ifp);
13827d37320SRobert Watson 	return error;
139bbd17bf8SGarrett Wollman }
140bbd17bf8SGarrett Wollman 
141*7e0bba4dSGleb Smirnoff SYSCTL_DECL(_net_link_generic);
1427029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata,
1439daf7154SPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ifdata,
1447029da5cSPawel Biernacki     "Interface table");
145