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