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.13 2004/08/06 08:46:59 brandt 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 <sys/time.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 60 /* 61 * Interface list and flags. 62 */ 63 TAILQ_HEAD(mibif_list, mibif); 64 enum { 65 MIBIF_FOUND = 0x0001, 66 MIBIF_HIGHSPEED = 0x0002, 67 MIBIF_VERYHIGHSPEED = 0x0004, 68 }; 69 #define hc_inoctets mib.ifmd_data.ifi_ibytes 70 #define hc_outoctets mib.ifmd_data.ifi_obytes 71 #define hc_omcasts mib.ifmd_data.ifi_omcasts 72 #define hc_opackets mib.ifmd_data.ifi_opackets 73 #define hc_imcasts mib.ifmd_data.ifi_imcasts 74 #define hc_ipackets mib.ifmd_data.ifi_ipackets 75 76 /* 77 * Interface addresses. 78 */ 79 TAILQ_HEAD(mibifa_list, mibifa); 80 enum { 81 MIBIFA_FOUND = 0x0001, 82 MIBIFA_DESTROYED = 0x0002, 83 }; 84 85 /* 86 * Receive addresses 87 */ 88 TAILQ_HEAD(mibrcvaddr_list, mibrcvaddr); 89 enum { 90 MIBRCVADDR_FOUND = 0x00010000, 91 }; 92 93 /* 94 * Interface index mapping. The problem here is, that if the same interface 95 * is reinstantiated (for examble by unloading and loading the hardware driver) 96 * we must use the same index for this interface. For dynamic interfaces 97 * (clip, lane) we must use a fresh index, each time a new interface is created. 98 * To differentiate between these types of interfaces we use the following table 99 * which contains an entry for each dynamic interface type. All other interface 100 * types are supposed to be static. The mibindexmap contains an entry for 101 * all interfaces. The mibif pointer is NULL, if the interface doesn't exist 102 * anymore. 103 */ 104 struct mibdynif { 105 SLIST_ENTRY(mibdynif) link; 106 char name[IFNAMSIZ]; 107 }; 108 SLIST_HEAD(mibdynif_list, mibdynif); 109 110 struct mibindexmap { 111 STAILQ_ENTRY(mibindexmap) link; 112 u_short sysindex; 113 u_int ifindex; 114 struct mibif *mibif; /* may be NULL */ 115 char name[IFNAMSIZ]; 116 }; 117 STAILQ_HEAD(mibindexmap_list, mibindexmap); 118 119 /* 120 * Interface stacking. The generic code cannot know how the interfaces stack. 121 * For this reason it instantiates only the x.0 and 0.x table elements. All 122 * others have to be instantiated by the interface specific modules. 123 * The table is read-only. 124 */ 125 struct mibifstack { 126 TAILQ_ENTRY(mibifstack) link; 127 struct asn_oid index; 128 }; 129 TAILQ_HEAD(mibifstack_list, mibifstack); 130 131 /* 132 * NetToMediaTable (ArpTable) 133 */ 134 struct mibarp { 135 TAILQ_ENTRY(mibarp) link; 136 struct asn_oid index; /* contains both the ifindex and addr */ 137 u_char phys[128]; /* the physical address */ 138 u_int physlen; /* and its length */ 139 u_int flags; 140 }; 141 TAILQ_HEAD(mibarp_list, mibarp); 142 enum { 143 MIBARP_FOUND = 0x00010000, 144 MIBARP_PERM = 0x00000001, 145 }; 146 147 /* 148 * New if registrations 149 */ 150 struct newifreg { 151 TAILQ_ENTRY(newifreg) link; 152 const struct lmodule *mod; 153 int (*func)(struct mibif *); 154 }; 155 TAILQ_HEAD(newifreg_list, newifreg); 156 157 /* list of all IP addresses */ 158 extern struct mibifa_list mibifa_list; 159 160 /* list of all interfaces */ 161 extern struct mibif_list mibif_list; 162 163 /* list of dynamic interface names */ 164 extern struct mibdynif_list mibdynif_list; 165 166 /* list of all interface index mappings */ 167 extern struct mibindexmap_list mibindexmap_list; 168 169 /* list of all stacking entries */ 170 extern struct mibifstack_list mibifstack_list; 171 172 /* list of all receive addresses */ 173 extern struct mibrcvaddr_list mibrcvaddr_list; 174 175 /* list of all NetToMedia entries */ 176 extern struct mibarp_list mibarp_list; 177 178 /* number of interfaces */ 179 extern int32_t mib_if_number; 180 181 /* last change of interface table */ 182 extern uint32_t mib_iftable_last_change; 183 184 /* last change of stack table */ 185 extern uint32_t mib_ifstack_last_change; 186 187 /* if this is set, one of our lists may be bad. refresh them when idle */ 188 extern int mib_iflist_bad; 189 190 /* last time refreshed */ 191 extern uint32_t mibarpticks; 192 193 /* info on system clocks */ 194 extern struct clockinfo clockinfo; 195 196 /* get interfaces and interface addresses. */ 197 void mib_fetch_interfaces(void); 198 199 /* check whether this interface(type) is dynamic */ 200 int mib_if_is_dyn(const char *name); 201 202 /* destroy an interface address */ 203 int mib_destroy_ifa(struct mibifa *); 204 205 /* restituate a deleted interface address */ 206 void mib_undestroy_ifa(struct mibifa *); 207 208 /* change interface address */ 209 int mib_modify_ifa(struct mibifa *); 210 211 /* undo if address modification */ 212 void mib_unmodify_ifa(struct mibifa *); 213 214 /* create an interface address */ 215 struct mibifa * mib_create_ifa(u_int ifindex, struct in_addr addr, struct in_addr mask, struct in_addr bcast); 216 217 /* delete a freshly created address */ 218 void mib_uncreate_ifa(struct mibifa *); 219 220 /* create/delete arp entries */ 221 struct mibarp *mib_arp_create(const struct mibif *, struct in_addr, const u_char *, size_t); 222 void mib_arp_delete(struct mibarp *); 223 224 /* find arp entry */ 225 struct mibarp *mib_find_arp(const struct mibif *, struct in_addr); 226 227 /* update arp table */ 228 void mib_arp_update(void); 229 230 /* fetch routing table */ 231 u_char *mib_fetch_rtab(int af, int info, int arg, size_t *lenp); 232 233 /* extract addresses from routing message */ 234 void mib_extract_addrs(int, u_char *, struct sockaddr **); 235