xref: /freebsd/sys/net/if_mib.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
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 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 
37 #include <net/if.h>
38 #include <net/if_var.h>
39 #include <net/if_private.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 static int
67 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
68 {
69 	int *name = (int *)arg1;
70 	int error;
71 	u_int namelen = arg2;
72 	struct ifnet *ifp;
73 	struct ifmibdata ifmd;
74 	struct epoch_tracker et;
75 	size_t dlen;
76 	char *dbuf;
77 
78 	if (namelen != 2)
79 		return EINVAL;
80 	if (name[0] <= 0)
81 		return (ENOENT);
82 	NET_EPOCH_ENTER(et);
83 	ifp = ifnet_byindex_ref(name[0]);
84 	NET_EPOCH_EXIT(et);
85 	if (ifp == NULL)
86 		return (ENOENT);
87 
88 	switch(name[1]) {
89 	default:
90 		error = ENOENT;
91 		goto out;
92 
93 	case IFDATA_GENERAL:
94 		bzero(&ifmd, sizeof(ifmd));
95 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
96 
97 		ifmd.ifmd_pcount = ifp->if_pcount;
98 		if_data_copy(ifp, &ifmd.ifmd_data);
99 
100 		ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
101 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
102 		ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
103 		ifmd.ifmd_snd_drops =
104 		    ifp->if_get_counter(ifp, IFCOUNTER_OQDROPS);
105 
106 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
107 		if (error)
108 			goto out;
109 		break;
110 
111 	case IFDATA_LINKSPECIFIC:
112 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
113 		if (error || !req->newptr)
114 			goto out;
115 		break;
116 
117 	case IFDATA_DRIVERNAME:
118 		/* 20 is enough for 64bit ints */
119 		dlen = strlen(ifp->if_dname) + 20 + 1;
120 		if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
121 			error = ENOMEM;
122 			goto out;
123 		}
124 		if (ifp->if_dunit == IF_DUNIT_NONE)
125 			strcpy(dbuf, ifp->if_dname);
126 		else
127 			sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
128 
129 		error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
130 		if (error == 0 && req->newptr != NULL)
131 			error = EPERM;
132 		free(dbuf, M_TEMP);
133 		goto out;
134 	}
135 out:
136 	if_rele(ifp);
137 	return error;
138 }
139 
140 SYSCTL_DECL(_net_link_generic);
141 static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata,
142     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ifdata,
143     "Interface table");
144