1 /* 2 * Copyright (c) 2001-2003 3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus). 4 * All rights reserved. 5 * 6 * Author: Harti Brandt <harti@freebsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $Begemot: bsnmp/snmp_mibII/mibII.h,v 1.16 2006/02/14 09:04:19 brandt_h Exp $ 30 * 31 * Implementation of the interfaces and IP groups of MIB-II. 32 */ 33 #include <sys/param.h> 34 #include <sys/sysctl.h> 35 #include <sys/socket.h> 36 #include <sys/sockio.h> 37 #include <sys/syslog.h> 38 #include <stdint.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <errno.h> 43 #include <unistd.h> 44 #include <err.h> 45 #include <ctype.h> 46 #include <net/if.h> 47 #include <net/if_dl.h> 48 #include <net/if_mib.h> 49 #include <net/route.h> 50 #include <netinet/in.h> 51 #include <arpa/inet.h> 52 53 #include "asn1.h" 54 #include "snmp.h" 55 #include "snmpmod.h" 56 #include "snmp_mibII.h" 57 #include "mibII_tree.h" 58 59 /* maximum size of interface alias unless overridden with net.ifdescr_maxlen */ 60 #define MIBIF_ALIAS_SIZE (64 + 1) 61 #define MIBIF_ALIAS_SIZE_MAX 1024 62 63 /* 64 * Interface list and flags. 65 */ 66 TAILQ_HEAD(mibif_list, mibif); 67 enum { 68 MIBIF_FOUND = 0x0001, 69 MIBIF_HIGHSPEED = 0x0002, 70 MIBIF_VERYHIGHSPEED = 0x0004, 71 }; 72 73 /* 74 * Private mibif data - hang off from the mibif. 75 */ 76 struct mibif_private { 77 uint64_t hc_inoctets; 78 uint64_t hc_outoctets; 79 uint64_t hc_omcasts; 80 uint64_t hc_opackets; 81 uint64_t hc_imcasts; 82 uint64_t hc_ipackets; 83 }; 84 #define MIBIF_PRIV(IFP) ((struct mibif_private *)((IFP)->private)) 85 86 /* 87 * Interface addresses. 88 */ 89 TAILQ_HEAD(mibifa_list, mibifa); 90 enum { 91 MIBIFA_FOUND = 0x0001, 92 MIBIFA_DESTROYED = 0x0002, 93 }; 94 95 /* 96 * Receive addresses 97 */ 98 TAILQ_HEAD(mibrcvaddr_list, mibrcvaddr); 99 enum { 100 MIBRCVADDR_FOUND = 0x00010000, 101 }; 102 103 /* 104 * Interface index mapping. The problem here is, that if the same interface 105 * is reinstantiated (for examble by unloading and loading the hardware driver) 106 * we must use the same index for this interface. For dynamic interfaces 107 * (clip, lane) we must use a fresh index, each time a new interface is created. 108 * To differentiate between these types of interfaces we use the following table 109 * which contains an entry for each dynamic interface type. All other interface 110 * types are supposed to be static. The mibindexmap contains an entry for 111 * all interfaces. The mibif pointer is NULL, if the interface doesn't exist 112 * anymore. 113 */ 114 struct mibdynif { 115 SLIST_ENTRY(mibdynif) link; 116 char name[IFNAMSIZ]; 117 }; 118 SLIST_HEAD(mibdynif_list, mibdynif); 119 120 struct mibindexmap { 121 STAILQ_ENTRY(mibindexmap) link; 122 u_short sysindex; 123 u_int ifindex; 124 struct mibif *mibif; /* may be NULL */ 125 char name[IFNAMSIZ]; 126 }; 127 STAILQ_HEAD(mibindexmap_list, mibindexmap); 128 129 /* 130 * Interface stacking. The generic code cannot know how the interfaces stack. 131 * For this reason it instantiates only the x.0 and 0.x table elements. All 132 * others have to be instantiated by the interface specific modules. 133 * The table is read-only. 134 */ 135 struct mibifstack { 136 TAILQ_ENTRY(mibifstack) link; 137 struct asn_oid index; 138 }; 139 TAILQ_HEAD(mibifstack_list, mibifstack); 140 141 /* 142 * NetToMediaTable (ArpTable) 143 */ 144 struct mibarp { 145 TAILQ_ENTRY(mibarp) link; 146 struct asn_oid index; /* contains both the ifindex and addr */ 147 u_char phys[128]; /* the physical address */ 148 u_int physlen; /* and its length */ 149 u_int flags; 150 }; 151 TAILQ_HEAD(mibarp_list, mibarp); 152 enum { 153 MIBARP_FOUND = 0x00010000, 154 MIBARP_PERM = 0x00000001, 155 }; 156 157 /* 158 * New if registrations 159 */ 160 struct newifreg { 161 TAILQ_ENTRY(newifreg) link; 162 const struct lmodule *mod; 163 int (*func)(struct mibif *); 164 }; 165 TAILQ_HEAD(newifreg_list, newifreg); 166 167 /* list of all IP addresses */ 168 extern struct mibifa_list mibifa_list; 169 170 /* list of all interfaces */ 171 extern struct mibif_list mibif_list; 172 173 /* list of dynamic interface names */ 174 extern struct mibdynif_list mibdynif_list; 175 176 /* list of all interface index mappings */ 177 extern struct mibindexmap_list mibindexmap_list; 178 179 /* list of all stacking entries */ 180 extern struct mibifstack_list mibifstack_list; 181 182 /* list of all receive addresses */ 183 extern struct mibrcvaddr_list mibrcvaddr_list; 184 185 /* list of all NetToMedia entries */ 186 extern struct mibarp_list mibarp_list; 187 188 /* number of interfaces */ 189 extern int32_t mib_if_number; 190 191 /* last change of interface table */ 192 extern uint64_t mib_iftable_last_change; 193 194 /* last change of stack table */ 195 extern uint64_t mib_ifstack_last_change; 196 197 /* if this is set, one of our lists may be bad. refresh them when idle */ 198 extern int mib_iflist_bad; 199 200 /* last time refreshed */ 201 extern uint64_t mibarpticks; 202 203 /* baud rate of fastest interface */ 204 extern uint64_t mibif_maxspeed; 205 206 /* user-forced update interval */ 207 extern u_int mibif_force_hc_update_interval; 208 209 /* current update interval */ 210 extern u_int mibif_hc_update_interval; 211 212 /* re-compute update interval */ 213 void mibif_reset_hc_timer(void); 214 215 /* interfaces' data poll interval */ 216 extern u_int mibII_poll_ticks; 217 218 /* restart the data poll timer */ 219 void mibif_restart_mibII_poll_timer(void); 220 221 #define MIBII_POLL_TICKS 100 222 223 /* get interfaces and interface addresses. */ 224 void mib_fetch_interfaces(void); 225 226 /* check whether this interface(type) is dynamic */ 227 int mib_if_is_dyn(const char *name); 228 229 /* destroy an interface address */ 230 int mib_destroy_ifa(struct mibifa *); 231 232 /* restituate a deleted interface address */ 233 void mib_undestroy_ifa(struct mibifa *); 234 235 /* change interface address */ 236 int mib_modify_ifa(struct mibifa *); 237 238 /* undo if address modification */ 239 void mib_unmodify_ifa(struct mibifa *); 240 241 /* create an interface address */ 242 struct mibifa * mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask, struct in_addr bcast); 243 244 /* delete a freshly created address */ 245 void mib_uncreate_ifa(struct mibifa *); 246 247 /* create/delete arp entries */ 248 struct mibarp *mib_arp_create(const struct mibif *, struct in_addr, const u_char *, size_t); 249 void mib_arp_delete(struct mibarp *); 250 251 /* find arp entry */ 252 struct mibarp *mib_find_arp(const struct mibif *, struct in_addr); 253 254 /* update arp table */ 255 void mib_arp_update(void); 256 257 /* fetch routing table */ 258 u_char *mib_fetch_rtab(int af, int info, int arg, size_t *lenp); 259 260 /* process routing message */ 261 void mib_sroute_process(struct rt_msghdr *, struct sockaddr *, 262 struct sockaddr *, struct sockaddr *); 263 264 /* send a routing message */ 265 void mib_send_rtmsg(struct rt_msghdr *, struct sockaddr *, 266 struct sockaddr *, struct sockaddr *); 267 268 /* extract addresses from routing message */ 269 void mib_extract_addrs(int, u_char *, struct sockaddr **); 270 271 /* fetch routing table */ 272 int mib_fetch_route(void); 273