xref: /freebsd/sys/net/if_mib.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
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 <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 
38 #include <net/if.h>
39 #include <net/if_var.h>
40 #include <net/if_mib.h>
41 #include <net/vnet.h>
42 
43 /*
44  * A sysctl(3) MIB for generic interface information.  This information
45  * is exported in the net.link.generic branch, which has the following
46  * structure:
47  *
48  * net.link.generic	.system			- system-wide control variables
49  *						  and statistics (node)
50  *			.ifdata.<ifindex>.general
51  *						- what's in `struct ifdata'
52  *						  plus some other info
53  *			.ifdata.<ifindex>.linkspecific
54  *						- a link-type-specific data
55  *						  structure (as might be used
56  *						  by an SNMP agent
57  *
58  * Perhaps someday we will make addresses accessible via this interface
59  * as well (then there will be four such...).  The reason that the
60  * index comes before the last element in the name is because it
61  * seems more orthogonal that way, particularly with the possibility
62  * of other per-interface data living down here as well (e.g., integrated
63  * services stuff).
64  */
65 
66 SYSCTL_DECL(_net_link_generic);
67 static SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
68 	    "Variables global to all interfaces");
69 
70 SYSCTL_VNET_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
71 	    &VNET_NAME(if_index), 0,
72 	     "Number of configured interfaces");
73 
74 static int
75 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
76 {
77 	int *name = (int *)arg1;
78 	int error;
79 	u_int namelen = arg2;
80 	struct ifnet *ifp;
81 	struct ifmibdata ifmd;
82 	size_t dlen;
83 	char *dbuf;
84 
85 	if (namelen != 2)
86 		return EINVAL;
87 	if (name[0] <= 0)
88 		return (ENOENT);
89 	ifp = ifnet_byindex_ref(name[0]);
90 	if (ifp == NULL)
91 		return (ENOENT);
92 
93 	switch(name[1]) {
94 	default:
95 		error = ENOENT;
96 		goto out;
97 
98 	case IFDATA_GENERAL:
99 		bzero(&ifmd, sizeof(ifmd));
100 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
101 
102 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
103 		COPY(pcount);
104 		COPY(data);
105 #undef COPY
106 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
107 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
108 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
109 		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
110 
111 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
112 		if (error || !req->newptr)
113 			goto out;
114 
115 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
116 		if (error)
117 			goto out;
118 
119 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
120 		DONTCOPY(type);
121 		DONTCOPY(physical);
122 		DONTCOPY(addrlen);
123 		DONTCOPY(hdrlen);
124 		DONTCOPY(mtu);
125 		DONTCOPY(metric);
126 		DONTCOPY(baudrate);
127 #undef DONTCOPY
128 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
129 		COPY(data);
130 		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
131 		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
132 #undef COPY
133 		break;
134 
135 	case IFDATA_LINKSPECIFIC:
136 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
137 		if (error || !req->newptr)
138 			goto out;
139 
140 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
141 		if (error)
142 			goto out;
143 		break;
144 
145 	case IFDATA_DRIVERNAME:
146 		/* 20 is enough for 64bit ints */
147 		dlen = strlen(ifp->if_dname) + 20 + 1;
148 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
149 			error = ENOMEM;
150 			goto out;
151 		}
152 		if (ifp->if_dunit == IF_DUNIT_NONE)
153 			strcpy(dbuf, ifp->if_dname);
154 		else
155 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
156 
157 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
158 		if (error == 0 && req->newptr != NULL)
159 			error = EPERM;
160 		free(dbuf, M_TEMP);
161 		goto out;
162 	}
163 out:
164 	if_rele(ifp);
165 	return error;
166 }
167 
168 static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
169 	    sysctl_ifdata, "Interface table");
170 
171