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