xref: /freebsd/sys/net/if_mib.c (revision bbd17bf8e1085896027c21b166e90eb4f3afbe1f)
1bbd17bf8SGarrett Wollman /*
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  *	$Id$
30bbd17bf8SGarrett Wollman  */
31bbd17bf8SGarrett Wollman 
32bbd17bf8SGarrett Wollman #include <sys/param.h>
33bbd17bf8SGarrett Wollman #include <sys/queue.h>
34bbd17bf8SGarrett Wollman #include <sys/systm.h>
35bbd17bf8SGarrett Wollman #include <sys/kernel.h>
36bbd17bf8SGarrett Wollman #include <sys/errno.h>
37bbd17bf8SGarrett Wollman #include <sys/sysctl.h>
38bbd17bf8SGarrett Wollman 
39bbd17bf8SGarrett Wollman #include <net/if.h>
40bbd17bf8SGarrett Wollman #include <net/if_dl.h>
41bbd17bf8SGarrett Wollman #include <net/if_mib.h>
42bbd17bf8SGarrett Wollman #include <net/if_types.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 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
68bbd17bf8SGarrett Wollman 	    "Variables global to all interfaces");
69bbd17bf8SGarrett Wollman SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
70bbd17bf8SGarrett Wollman 	   &if_index, 0, "Number of configured interfaces");
71bbd17bf8SGarrett Wollman 
72bbd17bf8SGarrett Wollman static int
73bbd17bf8SGarrett Wollman sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */
74bbd17bf8SGarrett Wollman {
75bbd17bf8SGarrett Wollman 	int *name = (int *)arg1;
76bbd17bf8SGarrett Wollman 	int error, ifnlen;
77bbd17bf8SGarrett Wollman 	u_int namelen = arg2;
78bbd17bf8SGarrett Wollman 	struct ifnet *ifp;
79bbd17bf8SGarrett Wollman 	char workbuf[64];
80bbd17bf8SGarrett Wollman 	struct ifmibdata ifmd;
81bbd17bf8SGarrett Wollman 
82bbd17bf8SGarrett Wollman 	if (namelen != 2)
83bbd17bf8SGarrett Wollman 		return EINVAL;
84bbd17bf8SGarrett Wollman 
85bbd17bf8SGarrett Wollman 	if (name[0] <= 0 || name[0] > if_index)
86bbd17bf8SGarrett Wollman 		return ENOENT;
87bbd17bf8SGarrett Wollman 
88bbd17bf8SGarrett Wollman 	ifp = ifnet_addrs[name[0] - 1]->ifa_ifp;
89bbd17bf8SGarrett Wollman 
90bbd17bf8SGarrett Wollman 	switch(name[1]) {
91bbd17bf8SGarrett Wollman 	default:
92bbd17bf8SGarrett Wollman 		return ENOENT;
93bbd17bf8SGarrett Wollman 
94bbd17bf8SGarrett Wollman 	case IFDATA_GENERAL:
95bbd17bf8SGarrett Wollman 		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
96bbd17bf8SGarrett Wollman 		if(ifnlen + 1 > sizeof ifmd.ifmd_name) {
97bbd17bf8SGarrett Wollman 			return ENAMETOOLONG;
98bbd17bf8SGarrett Wollman 		} else {
99bbd17bf8SGarrett Wollman 			strcpy(ifmd.ifmd_name, workbuf);
100bbd17bf8SGarrett Wollman 		}
101bbd17bf8SGarrett Wollman 
102bbd17bf8SGarrett Wollman #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
103bbd17bf8SGarrett Wollman 		COPY(pcount);
104bbd17bf8SGarrett Wollman 		COPY(flags);
105bbd17bf8SGarrett Wollman 		COPY(data);
106bbd17bf8SGarrett Wollman #undef COPY
107bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
108bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
109bbd17bf8SGarrett Wollman 		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
110bbd17bf8SGarrett Wollman 
111bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
112bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
113bbd17bf8SGarrett Wollman 			return error;
114bbd17bf8SGarrett Wollman 
115bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
116bbd17bf8SGarrett Wollman 		if (error)
117bbd17bf8SGarrett Wollman 			return error;
118bbd17bf8SGarrett Wollman 
119bbd17bf8SGarrett Wollman #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
120bbd17bf8SGarrett Wollman 		DONTCOPY(type);
121bbd17bf8SGarrett Wollman 		DONTCOPY(physical);
122bbd17bf8SGarrett Wollman 		DONTCOPY(addrlen);
123bbd17bf8SGarrett Wollman 		DONTCOPY(hdrlen);
124bbd17bf8SGarrett Wollman 		DONTCOPY(mtu);
125bbd17bf8SGarrett Wollman 		DONTCOPY(metric);
126bbd17bf8SGarrett Wollman 		DONTCOPY(baudrate);
127bbd17bf8SGarrett Wollman #undef DONTCOPY
128bbd17bf8SGarrett Wollman #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
129bbd17bf8SGarrett Wollman 		COPY(data);
130bbd17bf8SGarrett Wollman 		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
131bbd17bf8SGarrett Wollman 		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
132bbd17bf8SGarrett Wollman #undef COPY
133bbd17bf8SGarrett Wollman 		break;
134bbd17bf8SGarrett Wollman 
135bbd17bf8SGarrett Wollman 	case IFDATA_LINKSPECIFIC:
136bbd17bf8SGarrett Wollman 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
137bbd17bf8SGarrett Wollman 		if (error || !req->newptr)
138bbd17bf8SGarrett Wollman 			return error;
139bbd17bf8SGarrett Wollman 
140bbd17bf8SGarrett Wollman 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
141bbd17bf8SGarrett Wollman 		if (error)
142bbd17bf8SGarrett Wollman 			return error;
143bbd17bf8SGarrett Wollman 
144bbd17bf8SGarrett Wollman 	}
145bbd17bf8SGarrett Wollman 	return 0;
146bbd17bf8SGarrett Wollman }
147bbd17bf8SGarrett Wollman 
148bbd17bf8SGarrett Wollman SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
149bbd17bf8SGarrett Wollman 	    sysctl_ifdata, "Interface table");
150bbd17bf8SGarrett Wollman 
151