1bbd17bf8SGarrett Wollman /* 2bbd17bf8SGarrett Wollman * Copyright 1996 Massachusetts Institute of Technology 3bbd17bf8SGarrett Wollman * 4bbd17bf8SGarrett Wollman * Permission to use, copy, modify, and distribute this software and 5bbd17bf8SGarrett Wollman * its documentation for any purpose and without fee is hereby 6bbd17bf8SGarrett Wollman * granted, provided that both the above copyright notice and this 7bbd17bf8SGarrett Wollman * permission notice appear in all copies, that both the above 8bbd17bf8SGarrett Wollman * copyright notice and this permission notice appear in all 9bbd17bf8SGarrett Wollman * supporting documentation, and that the name of M.I.T. not be used 10bbd17bf8SGarrett Wollman * in advertising or publicity pertaining to distribution of the 11bbd17bf8SGarrett Wollman * software without specific, written prior permission. M.I.T. makes 12bbd17bf8SGarrett Wollman * no representations about the suitability of this software for any 13bbd17bf8SGarrett Wollman * purpose. It is provided "as is" without express or implied 14bbd17bf8SGarrett Wollman * warranty. 15bbd17bf8SGarrett Wollman * 16bbd17bf8SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17bbd17bf8SGarrett Wollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18bbd17bf8SGarrett Wollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19bbd17bf8SGarrett Wollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20bbd17bf8SGarrett Wollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21bbd17bf8SGarrett Wollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22bbd17bf8SGarrett Wollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23bbd17bf8SGarrett Wollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24bbd17bf8SGarrett Wollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25bbd17bf8SGarrett Wollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26bbd17bf8SGarrett Wollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27bbd17bf8SGarrett Wollman * SUCH DAMAGE. 28bbd17bf8SGarrett Wollman * 294458ac71SBruce Evans * $Id: if_mib.c,v 1.1 1996/07/30 19:17:00 wollman Exp $ 30bbd17bf8SGarrett Wollman */ 31bbd17bf8SGarrett Wollman 32bbd17bf8SGarrett Wollman #include <sys/param.h> 33bbd17bf8SGarrett Wollman #include <sys/queue.h> 34bbd17bf8SGarrett Wollman #include <sys/systm.h> 35bbd17bf8SGarrett Wollman #include <sys/kernel.h> 36bbd17bf8SGarrett Wollman #include <sys/errno.h> 374458ac71SBruce Evans #include <sys/socket.h> 38bbd17bf8SGarrett Wollman #include <sys/sysctl.h> 39bbd17bf8SGarrett Wollman 40bbd17bf8SGarrett Wollman #include <net/if.h> 41bbd17bf8SGarrett Wollman #include <net/if_dl.h> 42bbd17bf8SGarrett Wollman #include <net/if_mib.h> 43bbd17bf8SGarrett Wollman #include <net/if_types.h> 44bbd17bf8SGarrett Wollman 45bbd17bf8SGarrett Wollman /* 46bbd17bf8SGarrett Wollman * A sysctl(3) MIB for generic interface information. This information 47bbd17bf8SGarrett Wollman * is exported in the net.link.generic branch, which has the following 48bbd17bf8SGarrett Wollman * structure: 49bbd17bf8SGarrett Wollman * 50bbd17bf8SGarrett Wollman * net.link.generic .system - system-wide control variables 51bbd17bf8SGarrett Wollman * and statistics (node) 52bbd17bf8SGarrett Wollman * .ifdata.<ifindex>.general 53bbd17bf8SGarrett Wollman * - what's in `struct ifdata' 54bbd17bf8SGarrett Wollman * plus some other info 55bbd17bf8SGarrett Wollman * .ifdata.<ifindex>.linkspecific 56bbd17bf8SGarrett Wollman * - a link-type-specific data 57bbd17bf8SGarrett Wollman * structure (as might be used 58bbd17bf8SGarrett Wollman * by an SNMP agent 59bbd17bf8SGarrett Wollman * 60bbd17bf8SGarrett Wollman * Perhaps someday we will make addresses accessible via this interface 61bbd17bf8SGarrett Wollman * as well (then there will be four such...). The reason that the 62bbd17bf8SGarrett Wollman * index comes before the last element in the name is because it 63bbd17bf8SGarrett Wollman * seems more orthogonal that way, particularly with the possibility 64bbd17bf8SGarrett Wollman * of other per-interface data living down here as well (e.g., integrated 65bbd17bf8SGarrett Wollman * services stuff). 66bbd17bf8SGarrett Wollman */ 67bbd17bf8SGarrett Wollman 68bbd17bf8SGarrett Wollman SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0, 69bbd17bf8SGarrett Wollman "Variables global to all interfaces"); 70bbd17bf8SGarrett Wollman SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD, 71bbd17bf8SGarrett Wollman &if_index, 0, "Number of configured interfaces"); 72bbd17bf8SGarrett Wollman 73bbd17bf8SGarrett Wollman static int 74bbd17bf8SGarrett Wollman sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */ 75bbd17bf8SGarrett Wollman { 76bbd17bf8SGarrett Wollman int *name = (int *)arg1; 77bbd17bf8SGarrett Wollman int error, ifnlen; 78bbd17bf8SGarrett Wollman u_int namelen = arg2; 79bbd17bf8SGarrett Wollman struct ifnet *ifp; 80bbd17bf8SGarrett Wollman char workbuf[64]; 81bbd17bf8SGarrett Wollman struct ifmibdata ifmd; 82bbd17bf8SGarrett Wollman 83bbd17bf8SGarrett Wollman if (namelen != 2) 84bbd17bf8SGarrett Wollman return EINVAL; 85bbd17bf8SGarrett Wollman 86bbd17bf8SGarrett Wollman if (name[0] <= 0 || name[0] > if_index) 87bbd17bf8SGarrett Wollman return ENOENT; 88bbd17bf8SGarrett Wollman 89bbd17bf8SGarrett Wollman ifp = ifnet_addrs[name[0] - 1]->ifa_ifp; 90bbd17bf8SGarrett Wollman 91bbd17bf8SGarrett Wollman switch(name[1]) { 92bbd17bf8SGarrett Wollman default: 93bbd17bf8SGarrett Wollman return ENOENT; 94bbd17bf8SGarrett Wollman 95bbd17bf8SGarrett Wollman case IFDATA_GENERAL: 96bbd17bf8SGarrett Wollman ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit); 97bbd17bf8SGarrett Wollman if(ifnlen + 1 > sizeof ifmd.ifmd_name) { 98bbd17bf8SGarrett Wollman return ENAMETOOLONG; 99bbd17bf8SGarrett Wollman } else { 100bbd17bf8SGarrett Wollman strcpy(ifmd.ifmd_name, workbuf); 101bbd17bf8SGarrett Wollman } 102bbd17bf8SGarrett Wollman 103bbd17bf8SGarrett Wollman #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld 104bbd17bf8SGarrett Wollman COPY(pcount); 105bbd17bf8SGarrett Wollman COPY(flags); 106bbd17bf8SGarrett Wollman COPY(data); 107bbd17bf8SGarrett Wollman #undef COPY 108bbd17bf8SGarrett Wollman ifmd.ifmd_snd_len = ifp->if_snd.ifq_len; 109bbd17bf8SGarrett Wollman ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen; 110bbd17bf8SGarrett Wollman ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops; 111bbd17bf8SGarrett Wollman 112bbd17bf8SGarrett Wollman error = SYSCTL_OUT(req, &ifmd, sizeof ifmd); 113bbd17bf8SGarrett Wollman if (error || !req->newptr) 114bbd17bf8SGarrett Wollman return error; 115bbd17bf8SGarrett Wollman 116bbd17bf8SGarrett Wollman error = SYSCTL_IN(req, &ifmd, sizeof ifmd); 117bbd17bf8SGarrett Wollman if (error) 118bbd17bf8SGarrett Wollman return error; 119bbd17bf8SGarrett Wollman 120bbd17bf8SGarrett Wollman #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld 121bbd17bf8SGarrett Wollman DONTCOPY(type); 122bbd17bf8SGarrett Wollman DONTCOPY(physical); 123bbd17bf8SGarrett Wollman DONTCOPY(addrlen); 124bbd17bf8SGarrett Wollman DONTCOPY(hdrlen); 125bbd17bf8SGarrett Wollman DONTCOPY(mtu); 126bbd17bf8SGarrett Wollman DONTCOPY(metric); 127bbd17bf8SGarrett Wollman DONTCOPY(baudrate); 128bbd17bf8SGarrett Wollman #undef DONTCOPY 129bbd17bf8SGarrett Wollman #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld 130bbd17bf8SGarrett Wollman COPY(data); 131bbd17bf8SGarrett Wollman ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen; 132bbd17bf8SGarrett Wollman ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops; 133bbd17bf8SGarrett Wollman #undef COPY 134bbd17bf8SGarrett Wollman break; 135bbd17bf8SGarrett Wollman 136bbd17bf8SGarrett Wollman case IFDATA_LINKSPECIFIC: 137bbd17bf8SGarrett Wollman error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen); 138bbd17bf8SGarrett Wollman if (error || !req->newptr) 139bbd17bf8SGarrett Wollman return error; 140bbd17bf8SGarrett Wollman 141bbd17bf8SGarrett Wollman error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen); 142bbd17bf8SGarrett Wollman if (error) 143bbd17bf8SGarrett Wollman return error; 144bbd17bf8SGarrett Wollman 145bbd17bf8SGarrett Wollman } 146bbd17bf8SGarrett Wollman return 0; 147bbd17bf8SGarrett Wollman } 148bbd17bf8SGarrett Wollman 149bbd17bf8SGarrett Wollman SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW, 150bbd17bf8SGarrett Wollman sysctl_ifdata, "Interface table"); 151bbd17bf8SGarrett Wollman 152