1 /* $OpenBSD: device.c,v 1.14 2009/10/27 23:59:52 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1993-95 Mats O Jansson. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "os.h" 28 #include "common/common.h" 29 #include "common/mopdef.h" 30 31 struct if_info *iflist; /* Interface List */ 32 33 void mopReadDL(void); 34 void mopReadRC(void); 35 int mopOpenDL(struct if_info *, int); 36 int mopOpenRC(struct if_info *, int); 37 int pfTrans(); 38 int pfInit(); 39 int pfWrite(); 40 41 #ifdef DEV_NEW_CONF 42 /* 43 * Return ethernet address for interface 44 */ 45 46 void 47 deviceEthAddr(char *ifname, u_char *eaddr) 48 { 49 struct sockaddr_dl *sdl; 50 struct ifaddrs *ifap, *ifa; 51 52 if (getifaddrs(&ifap) != 0) { 53 syslog(LOG_ERR, "deviceEthAddr: getifaddrs: %m"); 54 exit(1); 55 } 56 57 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 58 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 59 if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || 60 sdl->sdl_alen != 6) 61 continue; 62 if (!strcmp(ifa->ifa_name, ifname)) { 63 bcopy(LLADDR(sdl), eaddr, 6); 64 freeifaddrs(ifap); 65 return; 66 } 67 } 68 69 syslog(LOG_ERR, "deviceEthAddr: Never saw interface `%s'!", ifname); 70 exit(1); 71 } 72 #endif /* DEV_NEW_CONF */ 73 74 void 75 deviceOpen(char *ifname, u_short proto, int trans) 76 { 77 struct if_info *p, tmp; 78 79 strncpy(tmp.if_name, ifname, sizeof(tmp.if_name) - 1); 80 tmp.if_name[sizeof(tmp.if_name) - 1] = 0; 81 tmp.iopen = pfInit; 82 83 switch (proto) { 84 case MOP_K_PROTO_RC: 85 tmp.read = mopReadRC; 86 tmp.fd = mopOpenRC(&tmp, trans); 87 break; 88 case MOP_K_PROTO_DL: 89 tmp.read = mopReadDL; 90 tmp.fd = mopOpenDL(&tmp, trans); 91 break; 92 default: 93 break; 94 } 95 96 if (tmp.fd != -1) { 97 p = malloc(sizeof(*p)); 98 if (p == 0) { 99 syslog(LOG_ERR, "deviceOpen: malloc: %m"); 100 exit(1); 101 } 102 103 p->next = iflist; 104 iflist = p; 105 106 strlcpy(p->if_name, tmp.if_name, IFNAME_SIZE); 107 p->iopen = tmp.iopen; 108 p->write = pfWrite; 109 p->read = tmp.read; 110 bzero(p->eaddr, sizeof(p->eaddr)); 111 p->fd = tmp.fd; 112 113 #ifdef DEV_NEW_CONF 114 deviceEthAddr(p->if_name, &p->eaddr[0]); 115 #else 116 p->eaddr[0] = tmp.eaddr[0]; 117 p->eaddr[1] = tmp.eaddr[1]; 118 p->eaddr[2] = tmp.eaddr[2]; 119 p->eaddr[3] = tmp.eaddr[3]; 120 p->eaddr[4] = tmp.eaddr[4]; 121 p->eaddr[5] = tmp.eaddr[5]; 122 #endif /* DEV_NEW_CONF */ 123 124 #ifdef LINUX2_PF 125 { 126 int s; 127 128 s = socket(AF_INET, SOCK_DGRAM, 0); 129 pfEthAddr(s, p->if_name, &p->eaddr[0]); 130 close(s); 131 } 132 #endif 133 } 134 } 135 136 void 137 deviceInitOne(char *ifname) 138 { 139 char interface[IFNAME_SIZE]; 140 struct if_info *p; 141 int trans; 142 #ifdef _AIX 143 char dev[IFNAME_SIZE]; 144 int unit,j; 145 146 unit = 0; 147 for (j = 0; j < strlen(ifname); j++) { 148 if (isalpha(ifname[j])) 149 dev[j] = ifname[j]; 150 else 151 if (isdigit(ifname[j])) { 152 unit = unit * 10 + ifname[j] - '0'; 153 dev[j] = '\0'; 154 } 155 } 156 157 if ((strlen(dev) == 2) && (dev[0] == 'e') && 158 ((dev[1] == 'n') || (dev[1] == 't'))) 159 snprintf(interface, sizeof(interface), "ent%d\0", unit); 160 else 161 snprintf(interface, sizeof(interface), "%s%d\0", dev, unit); 162 #else 163 snprintf(interface, sizeof(interface), "%s", ifname); 164 #endif /* _AIX */ 165 166 /* Ok, init it just once */ 167 p = iflist; 168 for (p = iflist; p; p = p->next) 169 if (strcmp(p->if_name, interface) == 0) 170 return; 171 172 syslog(LOG_INFO, "Initialized %s", interface); 173 174 /* Ok, get transport information */ 175 trans = pfTrans(interface); 176 177 #ifndef NORC 178 /* Start with MOP Remote Console */ 179 switch (trans) { 180 case TRANS_ETHER: 181 deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER); 182 break; 183 case TRANS_8023: 184 deviceOpen(interface, MOP_K_PROTO_RC, TRANS_8023); 185 break; 186 case TRANS_ETHER + TRANS_8023: 187 deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER); 188 deviceOpen(interface, MOP_K_PROTO_RC, TRANS_8023); 189 break; 190 case TRANS_ETHER + TRANS_8023 + TRANS_AND: 191 deviceOpen(interface, MOP_K_PROTO_RC, TRANS_ETHER + TRANS_8023); 192 break; 193 } 194 #endif 195 196 #ifndef NODL 197 /* and next MOP Dump/Load */ 198 switch (trans) { 199 case TRANS_ETHER: 200 deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER); 201 break; 202 case TRANS_8023: 203 deviceOpen(interface, MOP_K_PROTO_DL, TRANS_8023); 204 break; 205 case TRANS_ETHER + TRANS_8023: 206 deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER); 207 deviceOpen(interface, MOP_K_PROTO_DL, TRANS_8023); 208 break; 209 case TRANS_ETHER + TRANS_8023 + TRANS_AND: 210 deviceOpen(interface, MOP_K_PROTO_DL, TRANS_ETHER + TRANS_8023); 211 break; 212 } 213 #endif 214 215 } 216 217 /* 218 * Initialize all "candidate" interfaces that are in the system 219 * configuration list. A "candidate" is up, not loopback and not 220 * point to point. 221 */ 222 void 223 deviceInitAll() 224 { 225 #ifdef DEV_NEW_CONF 226 struct sockaddr_dl *sdl; 227 struct ifaddrs *ifap, *ifa; 228 229 if (getifaddrs(&ifap) != 0) { 230 syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m"); 231 exit(1); 232 } 233 234 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 235 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 236 if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || 237 sdl->sdl_alen != 6) 238 continue; 239 if ((ifa->ifa_flags & 240 (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP) 241 continue; 242 deviceInitOne(ifa->ifa_name); 243 } 244 freeifaddrs(ifap); 245 #else 246 struct ifaddrs *ifap, *ifa; 247 248 if (getifaddrs(&ifap) != 0) { 249 syslog(LOG_ERR, "deviceInitAll: getifaddrs: %m"); 250 exit(1); 251 } 252 253 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 254 if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/ 255 ifa->ifa_flags & IFF_LOOPBACK || 256 ifa->ifa_flags & IFF_POINTOPOINT) 257 continue; 258 deviceInitOne(ifa->ifa_name); 259 } 260 freeifaddrs(ifap); 261 #endif 262 } 263