1 /*- 2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: iface.c,v 1.1 1998/10/22 02:32:49 brian Exp $ 27 */ 28 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <net/if.h> 33 #include <net/if_dl.h> 34 #include <net/route.h> 35 #include <arpa/inet.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/ip.h> 38 #include <sys/un.h> 39 40 #include <sys/errno.h> 41 #include <sys/ioctl.h> 42 #include <sys/sysctl.h> 43 #include <string.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <termios.h> 47 #include <unistd.h> 48 49 #include "defs.h" 50 #include "command.h" 51 #include "mbuf.h" 52 #include "log.h" 53 #include "id.h" 54 #include "timer.h" 55 #include "fsm.h" 56 #include "iplist.h" 57 #include "lqr.h" 58 #include "hdlc.h" 59 #include "throughput.h" 60 #include "slcompress.h" 61 #include "filter.h" 62 #include "descriptor.h" 63 #include "ipcp.h" 64 #include "lcp.h" 65 #include "ccp.h" 66 #include "link.h" 67 #include "mp.h" 68 #include "bundle.h" 69 #include "prompt.h" 70 #include "iface.h" 71 72 73 static int 74 bitsinmask(struct in_addr mask) 75 { 76 u_int32_t bitmask, maskaddr; 77 int bits; 78 79 bitmask = 0xffffffff; 80 maskaddr = ntohl(mask.s_addr); 81 for (bits = 32; bits >= 0; bits--) { 82 if (maskaddr == bitmask) 83 break; 84 bitmask &= ~(1 << (32 - bits)); 85 } 86 87 return bits; 88 } 89 90 struct iface * 91 iface_Create(const char *name) 92 { 93 int mib[6], i, s; 94 size_t needed; 95 char *buf, *ptr, *end, *cp, *lim; 96 struct if_msghdr *ifm; 97 struct ifa_msghdr *ifam; 98 struct sockaddr_dl *dl; 99 struct rt_addrinfo rti; 100 struct iface *iface; 101 struct iface_addr *addr; 102 103 s = socket(AF_INET, SOCK_DGRAM, 0); 104 if (s < 0) { 105 fprintf(stderr, "iface_Create: socket(): %s\n", strerror(errno)); 106 return NULL; 107 } 108 109 mib[0] = CTL_NET; 110 mib[1] = PF_ROUTE; 111 mib[2] = 0; 112 mib[3] = 0; 113 mib[4] = NET_RT_IFLIST; 114 mib[5] = 0; 115 116 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 117 fprintf(stderr, "clean: sysctl: estimate: %s\n", 118 strerror(errno)); 119 close(s); 120 return NULL; 121 } 122 123 if ((buf = (char *)malloc(needed)) == NULL) { 124 close(s); 125 return NULL; 126 } 127 128 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 129 free(buf); 130 close(s); 131 return NULL; 132 } 133 134 ptr = buf; 135 end = buf + needed; 136 iface = NULL; 137 138 while (ptr < end && iface == NULL) { 139 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 140 if (ifm->ifm_type != RTM_IFINFO) 141 break; 142 dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 143 if (!strncmp(name, dl->sdl_data, dl->sdl_nlen)) { 144 iface = (struct iface *)malloc(sizeof *iface); 145 if (iface == NULL) { 146 fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno)); 147 return NULL; 148 } 149 iface->name = strdup(name); 150 iface->flags = ifm->ifm_flags; 151 iface->index = ifm->ifm_index; 152 iface->in_addrs = 0; 153 iface->in_addr = NULL; 154 } 155 ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 156 for (; ptr < end; ptr += ifam->ifam_msglen) { 157 ifam = (struct ifa_msghdr *)ptr; /* Next if address */ 158 159 if (ifam->ifam_type != RTM_NEWADDR) /* finished this if */ 160 break; 161 162 if (iface == NULL) /* Keep wading */ 163 continue; 164 165 /* Found an address ! */ 166 167 if (ifam->ifam_addrs & (1 << RTAX_IFA)) { 168 /* *And* it's configured ! */ 169 rti.rti_addrs = ifam->ifam_addrs; 170 lim = (char *)ifam + ifam->ifam_msglen; 171 cp = (char *)(ifam + 1); 172 memset(rti.rti_info, '\0', sizeof(rti.rti_info)); 173 for (i = 0; i < RTAX_MAX && cp < lim; i++) { 174 if ((rti.rti_addrs & (1 << i)) == 0) 175 continue; 176 rti.rti_info[i] = (struct sockaddr *)cp; 177 #define ROUNDUP(x) \ 178 ((x) > 0 ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long)) 179 cp += ROUNDUP(rti.rti_info[i]->sa_len); 180 } 181 182 if (rti.rti_info[RTAX_IFA] && 183 rti.rti_info[RTAX_IFA]->sa_family == AF_INET) { 184 /* Record the iface address rti */ 185 186 addr = (struct iface_addr *)realloc 187 (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]); 188 if (addr == NULL) 189 break; 190 iface->in_addr = addr; 191 192 addr += iface->in_addrs; 193 iface->in_addrs++; 194 195 addr->ifa.s_addr = ((struct sockaddr_in *)rti.rti_info[RTAX_IFA])-> 196 sin_addr.s_addr; 197 addr->brd.s_addr = rti.rti_info[RTAX_BRD] ? 198 ((struct sockaddr_in *)rti.rti_info[RTAX_BRD])->sin_addr.s_addr : 199 INADDR_ANY; 200 addr->mask.s_addr = rti.rti_info[RTAX_NETMASK] ? 201 ((struct sockaddr_in *)rti.rti_info[RTAX_NETMASK])->sin_addr.s_addr: 202 INADDR_ANY; 203 204 addr->bits = bitsinmask(addr->mask); 205 } 206 } 207 } 208 } 209 210 free(buf); 211 close(s); 212 213 return iface; 214 } 215 216 static void 217 iface_addr_Zap(const char *name, struct iface_addr *addr) 218 { 219 struct ifaliasreq ifra; 220 struct sockaddr_in *me, *peer; 221 int s; 222 223 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 224 if (s < 0) 225 log_Printf(LogERROR, "iface_addr_Zap: socket(): %s\n", strerror(errno)); 226 else { 227 memset(&ifra, '\0', sizeof ifra); 228 strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1); 229 me = (struct sockaddr_in *)&ifra.ifra_addr; 230 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 231 me->sin_family = peer->sin_family = AF_INET; 232 me->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 233 me->sin_addr = addr->ifa; 234 peer->sin_addr = addr->brd; 235 log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(addr->ifa)); 236 if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) 237 log_Printf(LogWARN, "iface_addr_Zap: ioctl(SIOCDIFADDR, %s): %s\n", 238 inet_ntoa(addr->ifa), strerror(errno)); 239 close(s); 240 } 241 } 242 243 void 244 iface_inClear(struct iface *iface, int how) 245 { 246 int n, addrs; 247 248 addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1; 249 for (; n < iface->in_addrs; n++) 250 iface_addr_Zap(iface->name, iface->in_addr + n); 251 252 iface->in_addrs = addrs; 253 /* Don't bother realloc()ing - we have little to gain */ 254 } 255 256 int 257 iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask, 258 struct in_addr brd, int how) 259 { 260 int slot, s, chg; 261 struct ifaliasreq ifra; 262 struct sockaddr_in *me, *peer, *msk; 263 struct iface_addr *addr; 264 265 for (slot = 0; slot < iface->in_addrs; slot++) 266 if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) { 267 if (how & IFACE_FORCE_ADD) 268 break; 269 else 270 /* errno = EEXIST; */ 271 return 0; 272 } 273 274 addr = (struct iface_addr *)realloc 275 (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]); 276 if (addr == NULL) { 277 log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno)); 278 return 0; 279 } 280 iface->in_addr = addr; 281 282 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 283 if (s < 0) { 284 log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno)); 285 return 0; 286 } 287 288 /* 289 * We've gotta be careful here. If we try to add an address with the 290 * same destination as an existing interface, nothing will work. 291 * Instead, we tweak all previous address entries that match the 292 * to-be-added destination to 255.255.255.255 (w/ a similar netmask). 293 * There *may* be more than one - if the user has ``iface add''ed 294 * stuff previously. 295 */ 296 for (chg = 0; chg < iface->in_addrs; chg++) { 297 if ((iface->in_addr[chg].brd.s_addr == brd.s_addr && 298 brd.s_addr != INADDR_BROADCAST) || chg == slot) { 299 memset(&ifra, '\0', sizeof ifra); 300 strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1); 301 me = (struct sockaddr_in *)&ifra.ifra_addr; 302 msk = (struct sockaddr_in *)&ifra.ifra_mask; 303 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 304 me->sin_family = msk->sin_family = peer->sin_family = AF_INET; 305 me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 306 me->sin_addr = iface->in_addr[chg].ifa; 307 msk->sin_addr = iface->in_addr[chg].mask; 308 peer->sin_addr = iface->in_addr[chg].brd; 309 log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr)); 310 ID0ioctl(s, SIOCDIFADDR, &ifra); /* Don't care if it fails... */ 311 if (chg != slot) { 312 peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr = 313 msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr = 314 INADDR_BROADCAST; 315 iface->in_addr[chg].bits = 32; 316 log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n", 317 inet_ntoa(me->sin_addr)); 318 if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) { 319 /* Oops - that's bad(ish) news ! We've lost an alias ! */ 320 log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n", 321 inet_ntoa(me->sin_addr), strerror(errno)); 322 iface->in_addrs--; 323 bcopy(iface->in_addr + chg + 1, iface->in_addr + chg, 324 (iface->in_addrs - chg) * sizeof iface->in_addr[0]); 325 if (slot > chg) 326 slot--; 327 chg--; 328 } 329 } 330 } 331 } 332 333 memset(&ifra, '\0', sizeof ifra); 334 strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1); 335 me = (struct sockaddr_in *)&ifra.ifra_addr; 336 msk = (struct sockaddr_in *)&ifra.ifra_mask; 337 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 338 me->sin_family = msk->sin_family = peer->sin_family = AF_INET; 339 me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 340 me->sin_addr = ifa; 341 msk->sin_addr = mask; 342 peer->sin_addr = brd; 343 344 if (log_IsKept(LogDEBUG)) { 345 char buf[16]; 346 347 strncpy(buf, inet_ntoa(brd), sizeof buf-1); 348 buf[sizeof buf - 1] = '\0'; 349 log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf); 350 } 351 352 /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */ 353 if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && 354 (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) { 355 log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n", 356 inet_ntoa(ifa), strerror(errno)); 357 ID0ioctl(s, SIOCDIFADDR, &ifra); /* EEXIST ? */ 358 close(s); 359 return 0; 360 } 361 close(s); 362 363 if (slot == iface->in_addrs) { 364 /* We're adding a new interface address */ 365 366 if (how & IFACE_ADD_FIRST) { 367 /* Stuff it at the start of our list */ 368 slot = 0; 369 bcopy(iface->in_addr, iface->in_addr + 1, 370 iface->in_addrs * sizeof iface->in_addr[0]); 371 } 372 373 iface->in_addrs++; 374 } else if (how & IFACE_ADD_FIRST) { 375 /* Shift it up to the first slot */ 376 bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]); 377 slot = 0; 378 } 379 380 iface->in_addr[slot].ifa = ifa; 381 iface->in_addr[slot].mask = mask; 382 iface->in_addr[slot].brd = brd; 383 iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask); 384 385 return 1; 386 } 387 388 int 389 iface_inDelete(struct iface *iface, struct in_addr ip) 390 { 391 int n; 392 393 for (n = 0; n < iface->in_addrs; n++) 394 if (iface->in_addr[n].ifa.s_addr == ip.s_addr) { 395 iface_addr_Zap(iface->name, iface->in_addr + n); 396 bcopy(iface->in_addr + n + 1, iface->in_addr + n, 397 (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]); 398 iface->in_addrs--; 399 return 1; 400 } 401 402 return 0; 403 } 404 405 void 406 iface_Destroy(struct iface *iface) 407 { 408 /* 409 * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually 410 * if that's what the user wants. It's better to leave the interface 411 * allocated so that existing connections can continue to work. 412 */ 413 414 if (iface != NULL) { 415 free(iface->name); 416 free(iface->in_addr); 417 free(iface); 418 } 419 } 420 421 #define if_entry(x) { IFF_##x, #x } 422 423 struct { 424 int flag; 425 const char *value; 426 } if_flags[] = { 427 if_entry(UP), 428 if_entry(BROADCAST), 429 if_entry(DEBUG), 430 if_entry(LOOPBACK), 431 if_entry(POINTOPOINT), 432 if_entry(RUNNING), 433 if_entry(NOARP), 434 if_entry(PROMISC), 435 if_entry(ALLMULTI), 436 if_entry(OACTIVE), 437 if_entry(SIMPLEX), 438 if_entry(LINK0), 439 if_entry(LINK1), 440 if_entry(LINK2), 441 if_entry(MULTICAST), 442 { 0, "???" } 443 }; 444 445 int 446 iface_Show(struct cmdargs const *arg) 447 { 448 struct iface *iface = arg->bundle->iface, *current; 449 int f, flags; 450 451 current = iface_Create(iface->name); 452 flags = iface->flags = current->flags; 453 iface_Destroy(current); 454 455 prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index); 456 for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++) 457 if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) { 458 prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",", 459 if_flags[f].value); 460 flags &= ~if_flags[f].flag; 461 } 462 prompt_Printf(arg->prompt, "> has %d address%s:\n", iface->in_addrs, 463 iface->in_addrs == 1 ? "" : "es"); 464 465 for (f = 0; f < iface->in_addrs; f++) { 466 prompt_Printf(arg->prompt, " %s", inet_ntoa(iface->in_addr[f].ifa)); 467 if (iface->in_addr[f].bits >= 0) 468 prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits); 469 if (iface->flags & IFF_POINTOPOINT) 470 prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd)); 471 else if (iface->flags & IFF_BROADCAST) 472 prompt_Printf(arg->prompt, " broadcast %s", 473 inet_ntoa(iface->in_addr[f].brd)); 474 if (iface->in_addr[f].bits < 0) 475 prompt_Printf(arg->prompt, " (mask %s)", 476 inet_ntoa(iface->in_addr[f].mask)); 477 prompt_Printf(arg->prompt, "\n"); 478 } 479 480 return 0; 481 } 482