xref: /freebsd/sys/net/if_mib.c (revision 8655c70597b0e0918c82114b1186df5669b83eb6)
1 /*-
2  * Copyright 1996 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include "opt_route.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/vimage.h>
40 
41 #include <net/if.h>
42 #include <net/if_mib.h>
43 #include <net/route.h>
44 #include <net/vnet.h>
45 
46 /*
47  * A sysctl(3) MIB for generic interface information.  This information
48  * is exported in the net.link.generic branch, which has the following
49  * structure:
50  *
51  * net.link.generic	.system			- system-wide control variables
52  *						  and statistics (node)
53  *			.ifdata.<ifindex>.general
54  *						- what's in `struct ifdata'
55  *						  plus some other info
56  *			.ifdata.<ifindex>.linkspecific
57  *						- a link-type-specific data
58  *						  structure (as might be used
59  *						  by an SNMP agent
60  *
61  * Perhaps someday we will make addresses accessible via this interface
62  * as well (then there will be four such...).  The reason that the
63  * index comes before the last element in the name is because it
64  * seems more orthogonal that way, particularly with the possibility
65  * of other per-interface data living down here as well (e.g., integrated
66  * services stuff).
67  */
68 
69 SYSCTL_DECL(_net_link_generic);
70 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
71 	    "Variables global to all interfaces");
72 
73 SYSCTL_V_INT(V_NET, vnet_net, _net_link_generic_system, IFMIB_IFCOUNT,
74 	     ifcount, CTLFLAG_RD, if_index, 0,
75 	     "Number of configured interfaces");
76 
77 static int
78 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
79 {
80 	INIT_VNET_NET(curvnet);
81 	int *name = (int *)arg1;
82 	int error;
83 	u_int namelen = arg2;
84 	struct ifnet *ifp;
85 	struct ifmibdata ifmd;
86 	size_t dlen;
87 	char *dbuf;
88 
89 	if (namelen != 2)
90 		return EINVAL;
91 
92 	if (name[0] <= 0 || name[0] > V_if_index ||
93 	    ifnet_byindex(name[0]) == NULL)
94 		return ENOENT;
95 
96 	ifp = ifnet_byindex(name[0]);
97 
98 	switch(name[1]) {
99 	default:
100 		return ENOENT;
101 
102 	case IFDATA_GENERAL:
103 		bzero(&ifmd, sizeof(ifmd));
104 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
105 
106 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
107 		COPY(pcount);
108 		COPY(data);
109 #undef COPY
110 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
111 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
112 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
113 		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
114 
115 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
116 		if (error || !req->newptr)
117 			return error;
118 
119 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
120 		if (error)
121 			return error;
122 
123 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
124 		DONTCOPY(type);
125 		DONTCOPY(physical);
126 		DONTCOPY(addrlen);
127 		DONTCOPY(hdrlen);
128 		DONTCOPY(mtu);
129 		DONTCOPY(metric);
130 		DONTCOPY(baudrate);
131 #undef DONTCOPY
132 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
133 		COPY(data);
134 		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
135 		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
136 #undef COPY
137 		break;
138 
139 	case IFDATA_LINKSPECIFIC:
140 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
141 		if (error || !req->newptr)
142 			return error;
143 
144 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
145 		if (error)
146 			return error;
147 		break;
148 
149 	case IFDATA_DRIVERNAME:
150 		/* 20 is enough for 64bit ints */
151 		dlen = strlen(ifp->if_dname) + 20 + 1;
152 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL)
153 			return (ENOMEM);
154 		if (ifp->if_dunit == IF_DUNIT_NONE)
155 			strcpy(dbuf, ifp->if_dname);
156 		else
157 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
158 
159 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
160 		if (error == 0 && req->newptr != NULL)
161 			error = EPERM;
162 		free(dbuf, M_TEMP);
163 		return (error);
164 	}
165 	return 0;
166 }
167 
168 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
169 	    sysctl_ifdata, "Interface table");
170 
171