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.3 2004/01/06 03:17:25 dillon 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_mib.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 SYSCTL_NODE(_net_link_generic, IFMIB_SYSTEM, system, CTLFLAG_RW, 0, 67 "Variables global to all interfaces"); 68 SYSCTL_INT(_net_link_generic_system, IFMIB_IFCOUNT, ifcount, CTLFLAG_RD, 69 &if_index, 0, "Number of configured interfaces"); 70 71 static int 72 sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */ 73 { 74 int *name = (int *)arg1; 75 int error; 76 u_int namelen = arg2; 77 struct ifnet *ifp; 78 struct ifmibdata ifmd; 79 80 if (namelen != 2) 81 return EINVAL; 82 83 if (name[0] <= 0 || name[0] > if_index) 84 return ENOENT; 85 86 ifp = ifnet_addrs[name[0] - 1]->ifa_ifp; 87 88 switch(name[1]) { 89 default: 90 return ENOENT; 91 92 case IFDATA_GENERAL: 93 strlcpy(ifmd.ifmd_name, ifp->if_xname, sizeof(ifmd.ifmd_name)); 94 95 #define COPY(fld) ifmd.ifmd_##fld = ifp->if_##fld 96 COPY(pcount); 97 COPY(flags); 98 COPY(data); 99 #undef COPY 100 ifmd.ifmd_snd_len = ifp->if_snd.ifq_len; 101 ifmd.ifmd_snd_maxlen = ifp->if_snd.ifq_maxlen; 102 ifmd.ifmd_snd_drops = ifp->if_snd.ifq_drops; 103 104 error = SYSCTL_OUT(req, &ifmd, sizeof ifmd); 105 if (error || !req->newptr) 106 return error; 107 108 error = SYSCTL_IN(req, &ifmd, sizeof ifmd); 109 if (error) 110 return error; 111 112 #define DONTCOPY(fld) ifmd.ifmd_data.ifi_##fld = ifp->if_data.ifi_##fld 113 DONTCOPY(type); 114 DONTCOPY(physical); 115 DONTCOPY(addrlen); 116 DONTCOPY(hdrlen); 117 DONTCOPY(mtu); 118 DONTCOPY(metric); 119 DONTCOPY(baudrate); 120 #undef DONTCOPY 121 #define COPY(fld) ifp->if_##fld = ifmd.ifmd_##fld 122 COPY(data); 123 ifp->if_snd.ifq_maxlen = ifmd.ifmd_snd_maxlen; 124 ifp->if_snd.ifq_drops = ifmd.ifmd_snd_drops; 125 #undef COPY 126 break; 127 128 case IFDATA_LINKSPECIFIC: 129 error = SYSCTL_OUT(req, ifp->if_linkmib, ifp->if_linkmiblen); 130 if (error || !req->newptr) 131 return error; 132 133 error = SYSCTL_IN(req, ifp->if_linkmib, ifp->if_linkmiblen); 134 if (error) 135 return error; 136 137 } 138 return 0; 139 } 140 141 SYSCTL_NODE(_net_link_generic, IFMIB_IFDATA, ifdata, CTLFLAG_RW, 142 sysctl_ifdata, "Interface table"); 143 144