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