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 * $Id: if_mib.c,v 1.5 1997/08/02 14:32:38 bde Exp $ 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 41 /* 42 * A sysctl(3) MIB for generic interface information. This information 43 * is exported in the net.link.generic branch, which has the following 44 * structure: 45 * 46 * net.link.generic .system - system-wide control variables 47 * and statistics (node) 48 * .ifdata.<ifindex>.general 49 * - what's in `struct ifdata' 50 * plus some other info 51 * .ifdata.<ifindex>.linkspecific 52 * - a link-type-specific data 53 * structure (as might be used 54 * by an SNMP agent 55 * 56 * Perhaps someday we will make addresses accessible via this interface 57 * as well (then there will be four such...). The reason that the 58 * index comes before the last element in the name is because it 59 * seems more orthogonal that way, particularly with the possibility 60 * of other per-interface data living down here as well (e.g., integrated 61 * services stuff). 62 */ 63 64 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0, 65 "Variables global to all interfaces"); 66 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD, 67 &if_index, 0, "Number of configured interfaces"); 68 69 static int 70 sysctl_ifdata SYSCTL_HANDLER_ARGS /* XXX bad syntax! */ 71 { 72 int *name = (int *)arg1; 73 int error, ifnlen; 74 u_int namelen = arg2; 75 struct ifnet *ifp; 76 char workbuf[64]; 77 struct ifmibdata ifmd; 78 79 if (namelen != 2) 80 return EINVAL; 81 82 if (name[0] <= 0 || name[0] > if_index) 83 return ENOENT; 84 85 ifp = ifnet_addrs[name[0] - 1]->ifa_ifp; 86 87 switch(name[1]) { 88 default: 89 return ENOENT; 90 91 case IFDATA_GENERAL: 92 ifnlen = snprintf(workbuf, sizeof(workbuf), 93 "%s%d", ifp->if_name, ifp->if_unit); 94 if(ifnlen + 1 > sizeof ifmd.ifmd_name) { 95 return ENAMETOOLONG; 96 } else { 97 strcpy(ifmd.ifmd_name, workbuf); 98 } 99 100 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld 101 COPY(pcount); 102 COPY(flags); 103 COPY(data); 104 #undef COPY 105 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len; 106 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen; 107 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops; 108 109 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd); 110 if (error || !req->newptr) 111 return error; 112 113 error = SYSCTL_IN(req, &ifmd, sizeof ifmd); 114 if (error) 115 return error; 116 117 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld 118 DONTCOPY(type); 119 DONTCOPY(physical); 120 DONTCOPY(addrlen); 121 DONTCOPY(hdrlen); 122 DONTCOPY(mtu); 123 DONTCOPY(metric); 124 DONTCOPY(baudrate); 125 #undef DONTCOPY 126 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld 127 COPY(data); 128 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen; 129 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops; 130 #undef COPY 131 break; 132 133 case IFDATA_LINKSPECIFIC: 134 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen); 135 if (error || !req->newptr) 136 return error; 137 138 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen); 139 if (error) 140 return error; 141 142 } 143 return 0; 144 } 145 146 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW, 147 sysctl_ifdata, "Interface table"); 148 149