xref: /freebsd/sys/net/if_mib.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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  */
29bbd17bf8SGarrett Wollman 
30bbd17bf8SGarrett Wollman #include <sys/param.h>
31bbd17bf8SGarrett Wollman #include <sys/systm.h>
32bbd17bf8SGarrett Wollman #include <sys/kernel.h>
338ec07310SGleb Smirnoff #include <sys/malloc.h>
344458ac71SBruce Evans #include <sys/socket.h>
35bbd17bf8SGarrett Wollman #include <sys/sysctl.h>
36bbd17bf8SGarrett Wollman 
37bbd17bf8SGarrett Wollman #include <net/if.h>
3876039bc8SGleb Smirnoff #include <net/if_var.h>
39*2c2b37adSJustin Hibbits #include <net/if_private.h>
40bbd17bf8SGarrett Wollman #include <net/if_mib.h>
414b79449eSBjoern A. Zeeb #include <net/vnet.h>
42bbd17bf8SGarrett Wollman 
43bbd17bf8SGarrett Wollman /*
44bbd17bf8SGarrett Wollman  * A sysctl(3) MIB for generic interface information.  This information
45bbd17bf8SGarrett Wollman  * is exported in the net.link.generic branch, which has the following
46bbd17bf8SGarrett Wollman  * structure:
47bbd17bf8SGarrett Wollman  *
48bbd17bf8SGarrett Wollman  * net.link.generic	.system			- system-wide control variables
49bbd17bf8SGarrett Wollman  *						  and statistics (node)
50bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.general
51bbd17bf8SGarrett Wollman  *						- what's in `struct ifdata'
52bbd17bf8SGarrett Wollman  *						  plus some other info
53bbd17bf8SGarrett Wollman  *			.ifdata.<ifindex>.linkspecific
54bbd17bf8SGarrett Wollman  *						- a link-type-specific data
55bbd17bf8SGarrett Wollman  *						  structure (as might be used
56bbd17bf8SGarrett Wollman  *						  by an SNMP agent
57bbd17bf8SGarrett Wollman  *
58bbd17bf8SGarrett Wollman  * Perhaps someday we will make addresses accessible via this interface
59bbd17bf8SGarrett Wollman  * as well (then there will be four such...).  The reason that the
60bbd17bf8SGarrett Wollman  * index comes before the last element in the name is because it
61bbd17bf8SGarrett Wollman  * seems more orthogonal that way, particularly with the possibility
62bbd17bf8SGarrett Wollman  * of other per-interface data living down here as well (e.g., integrated
63bbd17bf8SGarrett Wollman  * services stuff).
64bbd17bf8SGarrett Wollman  */
65bbd17bf8SGarrett Wollman 
66bbd17bf8SGarrett Wollman static int
sysctl_ifdata(SYSCTL_HANDLER_ARGS)6782d9ae4eSPoul-Henning Kamp sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
68bbd17bf8SGarrett Wollman {
69bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
709bf40edeSBrooks Davis 	int error;
71bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
72bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
73bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
74caeeeaa7SGleb Smirnoff 	struct epoch_tracker et;
7515450897SHartmut Brandt 	size_t dlen;
7615450897SHartmut Brandt 	char *dbuf;
77bbd17bf8SGarrett Wollman 
78bbd17bf8SGarrett Wollman 	if (namelen != 2)
79bbd17bf8SGarrett Wollman 		return EINVAL;
8027d37320SRobert Watson 	if (name[0] <= 0)
8127d37320SRobert Watson 		return (ENOENT);
82caeeeaa7SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8327d37320SRobert Watson 	ifp = ifnet_byindex_ref(name[0]);
84caeeeaa7SGleb Smirnoff 	NET_EPOCH_EXIT(et);
8527d37320SRobert Watson 	if (ifp == NULL)
8627d37320SRobert Watson 		return (ENOENT);
87bbd17bf8SGarrett Wollman 
88bbd17bf8SGarrett Wollman 	switch(name[1]) {
89bbd17bf8SGarrett Wollman 	default:
9027d37320SRobert Watson 		error = ENOENT;
9127d37320SRobert Watson 		goto out;
92bbd17bf8SGarrett Wollman 
93bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
94fd94099eSColin Percival 		bzero(&ifmd, sizeof(ifmd));
959bf40edeSBrooks Davis 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
96bbd17bf8SGarrett Wollman 
97e6485f73SGleb Smirnoff 		ifmd.ifmd_pcount = ifp->if_pcount;
98e6485f73SGleb Smirnoff 		if_data_copy(ifp, &ifmd.ifmd_data);
99e6485f73SGleb Smirnoff 
10057c1493bSChristian S.J. Peron 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
101bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
102bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
10356b61ca2SGleb Smirnoff 		ifmd.ifmd_snd_drops =
10456b61ca2SGleb Smirnoff 		    ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
105bbd17bf8SGarrett Wollman 
106bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
107bbd17bf8SGarrett Wollman 		if (error)
10827d37320SRobert Watson 			goto out;
109bbd17bf8SGarrett Wollman 		break;
110bbd17bf8SGarrett Wollman 
111bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
112bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
113bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
11427d37320SRobert Watson 			goto out;
1159d53bbefSBruce M Simpson 		break;
116bbd17bf8SGarrett Wollman 
11715450897SHartmut Brandt 	case IFDATA_DRIVERNAME:
11815450897SHartmut Brandt 		/* 20 is enough for 64bit ints */
11915450897SHartmut Brandt 		dlen = strlen(ifp->if_dname) + 20 + 1;
12027d37320SRobert Watson 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
12127d37320SRobert Watson 			error = ENOMEM;
12227d37320SRobert Watson 			goto out;
12327d37320SRobert Watson 		}
12415450897SHartmut Brandt 		if (ifp->if_dunit == IF_DUNIT_NONE)
12515450897SHartmut Brandt 			strcpy(dbuf, ifp->if_dname);
12615450897SHartmut Brandt 		else
12715450897SHartmut Brandt 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
12815450897SHartmut Brandt 
12915450897SHartmut Brandt 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
13015450897SHartmut Brandt 		if (error == 0 && req->newptr != NULL)
13115450897SHartmut Brandt 			error = EPERM;
13215450897SHartmut Brandt 		free(dbuf, M_TEMP);
13327d37320SRobert Watson 		goto out;
134bbd17bf8SGarrett Wollman 	}
13527d37320SRobert Watson out:
13627d37320SRobert Watson 	if_rele(ifp);
13727d37320SRobert Watson 	return error;
138bbd17bf8SGarrett Wollman }
139bbd17bf8SGarrett Wollman 
1407e0bba4dSGleb Smirnoff SYSCTL_DECL(_net_link_generic);
1417029da5cSPawel Biernacki static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata,
1429daf7154SPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ifdata,
1437029da5cSPawel Biernacki     "Interface table");
144