xref: /dragonfly/sys/net/if_mib.c (revision 7ff0fc30)
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: src/sys/net/if_mib.c,v 1.8.2.1 2000/08/03 00:09:34 ps Exp $
30  * $DragonFly: src/sys/net/if_mib.c,v 1.7 2005/06/15 19:29:30 joerg Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.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 
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 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0,
68 	    "Variables global to all interfaces");
69 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
70 	   &if_index, 0, "Number of configured interfaces");
71 
72 static int
73 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
74 {
75 	int *name = (int *)arg1;
76 	int error;
77 	u_int namelen = arg2;
78 	struct ifnet *ifp;
79 	struct ifmibdata ifmd;
80 
81 	if (namelen != 2)
82 		return EINVAL;
83 
84 	/*
85 	 * Hold the ifnet lock while we are accessing the ifp
86 	 * obtained through ifindex2ifnet.
87 	 */
88 	ifnet_lock();
89 
90 	if (name[0] <= 0 || name[0] > if_index ||
91 	    ifindex2ifnet[name[0]] == NULL) {
92 		ifnet_unlock();
93 		return ENOENT;
94 	}
95 
96 	ifp = ifindex2ifnet[name[0]];
97 
98 	switch(name[1]) {
99 	default:
100 		ifnet_unlock();
101 		return ENOENT;
102 
103 	case IFDATA_GENERAL:
104 		bzero(&ifmd, sizeof(ifmd));
105 		strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
106 
107 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
108 		COPY(pcount);
109 		COPY(flags);
110 		COPY(data);
111 #undef COPY
112 #define COPY_DATA(name)	IFNET_STAT_GET(ifp, name, ifmd.ifmd_data.ifi_##name)
113 		COPY_DATA(ipackets);
114 		COPY_DATA(ierrors);
115 		COPY_DATA(opackets);
116 		COPY_DATA(oerrors);
117 		COPY_DATA(collisions);
118 		COPY_DATA(ibytes);
119 		COPY_DATA(obytes);
120 		COPY_DATA(imcasts);
121 		COPY_DATA(omcasts);
122 		COPY_DATA(iqdrops);
123 		COPY_DATA(noproto);
124 		COPY_DATA(oqdrops);
125 #undef COPY_DATA
126 
127 		ifmd.ifmd_snd_maxlen = ifp->if_snd.altq_maxlen;
128 #ifdef notyet
129 		ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
130 		ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
131 #endif
132 
133 		error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
134 		if (error || !req->newptr) {
135 			ifnet_unlock();
136 			return error;
137 		}
138 
139 		error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
140 		if (error) {
141 			ifnet_unlock();
142 			return error;
143 		}
144 
145 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
146 		DONTCOPY(type);
147 		DONTCOPY(physical);
148 		DONTCOPY(addrlen);
149 		DONTCOPY(hdrlen);
150 		DONTCOPY(mtu);
151 		DONTCOPY(metric);
152 		DONTCOPY(baudrate);
153 #undef DONTCOPY
154 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
155 		COPY(data);
156 #undef COPY
157 #define COPY_DATA(name)	IFNET_STAT_SET(ifp, name, ifmd.ifmd_data.ifi_##name)
158 		COPY_DATA(ipackets);
159 		COPY_DATA(ierrors);
160 		COPY_DATA(opackets);
161 		COPY_DATA(oerrors);
162 		COPY_DATA(collisions);
163 		COPY_DATA(ibytes);
164 		COPY_DATA(obytes);
165 		COPY_DATA(imcasts);
166 		COPY_DATA(omcasts);
167 		COPY_DATA(iqdrops);
168 		COPY_DATA(noproto);
169 		COPY_DATA(oqdrops);
170 #undef COPY_DATA
171 
172 #ifdef notyet
173 		ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
174 		ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
175 #endif
176 		break;
177 
178 	case IFDATA_LINKSPECIFIC:
179 		error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
180 		if (error || !req->newptr) {
181 			ifnet_unlock();
182 			return error;
183 		}
184 
185 		error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
186 		if (error) {
187 			ifnet_unlock();
188 			return error;
189 		}
190 	}
191 
192 	ifnet_unlock();
193 	return 0;
194 }
195 
196 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
197 	    sysctl_ifdata, "Interface table");
198 
199