]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/net/if_mib.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / net / if_mib.c
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/socket.h>
36 #include <sys/sysctl.h>
37
38 #include <net/if.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, CTLFLAG_RW, 0,
67             "Variables global to all interfaces");
68
69 SYSCTL_VNET_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD,
70             &VNET_NAME(if_index), 0,
71              "Number of configured interfaces");
72
73 static int
74 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
75 {
76         int *name = (int *)arg1;
77         int error;
78         u_int namelen = arg2;
79         struct ifnet *ifp;
80         struct ifmibdata ifmd;
81         size_t dlen;
82         char *dbuf;
83
84         if (namelen != 2)
85                 return EINVAL;
86         if (name[0] <= 0)
87                 return (ENOENT);
88         ifp = ifnet_byindex_ref(name[0]);
89         if (ifp == NULL)
90                 return (ENOENT);
91
92         switch(name[1]) {
93         default:
94                 error = ENOENT;
95                 goto out;
96
97         case IFDATA_GENERAL:
98                 bzero(&ifmd, sizeof(ifmd));
99                 strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name));
100
101 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld
102                 COPY(pcount);
103                 COPY(data);
104 #undef COPY
105                 ifmd.ifmd_flags = ifp->if_flags | ifp->if_drv_flags;
106                 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len;
107                 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen;
108                 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops;
109
110                 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd);
111                 if (error || !req->newptr)
112                         goto out;
113
114                 error = SYSCTL_IN(req, &ifmd, sizeof ifmd);
115                 if (error)
116                         goto out;
117
118 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld
119                 DONTCOPY(type);
120                 DONTCOPY(physical);
121                 DONTCOPY(addrlen);
122                 DONTCOPY(hdrlen);
123                 DONTCOPY(mtu);
124                 DONTCOPY(metric);
125                 DONTCOPY(baudrate);
126 #undef DONTCOPY
127 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld
128                 COPY(data);
129                 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen;
130                 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops;
131 #undef COPY
132                 break;
133
134         case IFDATA_LINKSPECIFIC:
135                 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen);
136                 if (error || !req->newptr)
137                         goto out;
138
139                 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen);
140                 if (error)
141                         goto out;
142                 break;
143
144         case IFDATA_DRIVERNAME:
145                 /* 20 is enough for 64bit ints */
146                 dlen = strlen(ifp->if_dname) + 20 + 1;
147                 if ((dbuf = malloc(dlen, M_TEMP, M_NOWAIT)) == NULL) {
148                         error = ENOMEM;
149                         goto out;
150                 }
151                 if (ifp->if_dunit == IF_DUNIT_NONE)
152                         strcpy(dbuf, ifp->if_dname);
153                 else
154                         sprintf(dbuf, "%s%d", ifp->if_dname, ifp->if_dunit);
155
156                 error = SYSCTL_OUT(req, dbuf, strlen(dbuf) + 1);
157                 if (error == 0 && req->newptr != NULL)
158                         error = EPERM;
159                 free(dbuf, M_TEMP);
160                 goto out;
161         }
162 out:
163         if_rele(ifp);
164         return error;
165 }
166
167 static SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW,
168             sysctl_ifdata, "Interface table");
169