iface.c revision 74916
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 * $FreeBSD: head/usr.sbin/ppp/iface.c 74916 2001-03-28 09:45:27Z brian $ 27 */ 28 29#include <sys/param.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 <errno.h> 41#include <string.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <sys/ioctl.h> 45#include <sys/sysctl.h> 46#include <termios.h> 47#include <unistd.h> 48 49#include "layer.h" 50#include "defs.h" 51#include "command.h" 52#include "mbuf.h" 53#include "log.h" 54#include "id.h" 55#include "timer.h" 56#include "fsm.h" 57#include "iplist.h" 58#include "lqr.h" 59#include "hdlc.h" 60#include "throughput.h" 61#include "slcompress.h" 62#include "descriptor.h" 63#include "ipcp.h" 64#include "filter.h" 65#include "lcp.h" 66#include "ccp.h" 67#include "link.h" 68#include "mp.h" 69#ifndef NORADIUS 70#include "radius.h" 71#endif 72#include "bundle.h" 73#include "prompt.h" 74#include "iface.h" 75 76 77static int 78bitsinmask(struct in_addr mask) 79{ 80 u_int32_t bitmask, maskaddr; 81 int bits; 82 83 bitmask = 0xffffffff; 84 maskaddr = ntohl(mask.s_addr); 85 for (bits = 32; bits >= 0; bits--) { 86 if (maskaddr == bitmask) 87 break; 88 bitmask &= ~(1 << (32 - bits)); 89 } 90 91 return bits; 92} 93 94struct iface * 95iface_Create(const char *name) 96{ 97 int mib[6], s; 98 size_t needed, namelen; 99 char *buf, *ptr, *end; 100 struct if_msghdr *ifm; 101 struct ifa_msghdr *ifam; 102 struct sockaddr_dl *dl; 103 struct sockaddr *sa[RTAX_MAX]; 104 struct iface *iface; 105 struct iface_addr *addr; 106 107 s = socket(AF_INET, SOCK_DGRAM, 0); 108 if (s < 0) { 109 fprintf(stderr, "iface_Create: socket(): %s\n", strerror(errno)); 110 return NULL; 111 } 112 113 mib[0] = CTL_NET; 114 mib[1] = PF_ROUTE; 115 mib[2] = 0; 116 mib[3] = 0; 117 mib[4] = NET_RT_IFLIST; 118 mib[5] = 0; 119 120 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { 121 fprintf(stderr, "iface_Create: sysctl: estimate: %s\n", 122 strerror(errno)); 123 close(s); 124 return NULL; 125 } 126 127 if ((buf = (char *)malloc(needed)) == NULL) { 128 fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno)); 129 close(s); 130 return NULL; 131 } 132 133 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { 134 fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(errno)); 135 free(buf); 136 close(s); 137 return NULL; 138 } 139 140 ptr = buf; 141 end = buf + needed; 142 iface = NULL; 143 namelen = strlen(name); 144 145 while (ptr < end && iface == NULL) { 146 ifm = (struct if_msghdr *)ptr; /* On if_msghdr */ 147 if (ifm->ifm_type != RTM_IFINFO) 148 break; 149 dl = (struct sockaddr_dl *)(ifm + 1); /* Single _dl at end */ 150 if (dl->sdl_nlen == namelen && !strncmp(name, dl->sdl_data, namelen)) { 151 iface = (struct iface *)malloc(sizeof *iface); 152 if (iface == NULL) { 153 fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno)); 154 return NULL; 155 } 156 iface->name = strdup(name); 157 iface->flags = ifm->ifm_flags; 158 iface->index = ifm->ifm_index; 159 iface->in_addrs = 0; 160 iface->in_addr = NULL; 161 } 162 ptr += ifm->ifm_msglen; /* First ifa_msghdr */ 163 for (; ptr < end; ptr += ifam->ifam_msglen) { 164 ifam = (struct ifa_msghdr *)ptr; /* Next if address */ 165 166 if (ifam->ifam_type != RTM_NEWADDR) /* finished this if */ 167 break; 168 169 if (iface != NULL && ifam->ifam_addrs & RTA_IFA) { 170 /* Found a configured interface ! */ 171 iface_ParseHdr(ifam, sa); 172 173 if (sa[RTAX_IFA] && sa[RTAX_IFA]->sa_family == AF_INET) { 174 /* Record the address */ 175 176 addr = (struct iface_addr *)realloc 177 (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]); 178 if (addr == NULL) 179 break; 180 iface->in_addr = addr; 181 182 addr += iface->in_addrs; 183 iface->in_addrs++; 184 185 addr->ifa = ((struct sockaddr_in *)sa[RTAX_IFA])->sin_addr; 186 187 if (sa[RTAX_BRD]) 188 addr->brd = ((struct sockaddr_in *)sa[RTAX_BRD])->sin_addr; 189 else 190 addr->brd.s_addr = INADDR_ANY; 191 192 if (sa[RTAX_NETMASK]) 193 addr->mask = ((struct sockaddr_in *)sa[RTAX_NETMASK])->sin_addr; 194 else 195 addr->mask.s_addr = INADDR_ANY; 196 197 addr->bits = bitsinmask(addr->mask); 198 } 199 } 200 } 201 } 202 203 free(buf); 204 close(s); 205 206 return iface; 207} 208 209static void 210iface_addr_Zap(const char *name, struct iface_addr *addr) 211{ 212 struct ifaliasreq ifra; 213 struct sockaddr_in *me, *peer; 214 int s; 215 216 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 217 if (s < 0) 218 log_Printf(LogERROR, "iface_addr_Zap: socket(): %s\n", strerror(errno)); 219 else { 220 memset(&ifra, '\0', sizeof ifra); 221 strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1); 222 me = (struct sockaddr_in *)&ifra.ifra_addr; 223 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 224 me->sin_family = peer->sin_family = AF_INET; 225 me->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 226 me->sin_addr = addr->ifa; 227 peer->sin_addr = addr->brd; 228 log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(addr->ifa)); 229 if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0) 230 log_Printf(LogWARN, "iface_addr_Zap: ioctl(SIOCDIFADDR, %s): %s\n", 231 inet_ntoa(addr->ifa), strerror(errno)); 232 close(s); 233 } 234} 235 236void 237iface_inClear(struct iface *iface, int how) 238{ 239 int n, addrs; 240 241 if (iface->in_addrs) { 242 addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1; 243 for (; n < iface->in_addrs; n++) 244 iface_addr_Zap(iface->name, iface->in_addr + n); 245 246 iface->in_addrs = addrs; 247 /* Don't bother realloc()ing - we have little to gain */ 248 } 249} 250 251int 252iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask, 253 struct in_addr brd, int how) 254{ 255 int slot, s, chg, nochange; 256 struct ifaliasreq ifra; 257 struct sockaddr_in *me, *peer, *msk; 258 struct iface_addr *addr; 259 260 for (slot = 0; slot < iface->in_addrs; slot++) 261 if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) { 262 if (how & IFACE_FORCE_ADD) 263 break; 264 else 265 /* errno = EEXIST; */ 266 return 0; 267 } 268 269 addr = (struct iface_addr *)realloc 270 (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]); 271 if (addr == NULL) { 272 log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno)); 273 return 0; 274 } 275 iface->in_addr = addr; 276 277 /* 278 * We've gotta be careful here. If we try to add an address with the 279 * same destination as an existing interface, nothing will work. 280 * Instead, we tweak all previous address entries that match the 281 * to-be-added destination to 255.255.255.255 (w/ a similar netmask). 282 * There *may* be more than one - if the user has ``iface add''ed 283 * stuff previously. 284 */ 285 nochange = 0; 286 s = -1; 287 for (chg = 0; chg < iface->in_addrs; chg++) { 288 if ((iface->in_addr[chg].brd.s_addr == brd.s_addr && 289 brd.s_addr != INADDR_BROADCAST) || chg == slot) { 290 /* 291 * If we've found an entry that exactly matches what we want to add, 292 * don't remove it and then add it again. If we do, it's possible 293 * that the kernel will (correctly) ``tidy up'' any routes that use 294 * the IP number as a destination. 295 */ 296 if (chg == slot && iface->in_addr[chg].mask.s_addr == mask.s_addr) { 297 nochange = 1; 298 continue; 299 } 300 if (s == -1 && (s = ID0socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 301 log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno)); 302 return 0; 303 } 304 305 memset(&ifra, '\0', sizeof ifra); 306 strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1); 307 me = (struct sockaddr_in *)&ifra.ifra_addr; 308 msk = (struct sockaddr_in *)&ifra.ifra_mask; 309 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 310 me->sin_family = msk->sin_family = peer->sin_family = AF_INET; 311 me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 312 me->sin_addr = iface->in_addr[chg].ifa; 313 msk->sin_addr = iface->in_addr[chg].mask; 314 peer->sin_addr = iface->in_addr[chg].brd; 315 log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr)); 316 ID0ioctl(s, SIOCDIFADDR, &ifra); /* Don't care if it fails... */ 317 if (chg != slot) { 318 peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr = 319 msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr = 320 INADDR_BROADCAST; 321 iface->in_addr[chg].bits = 32; 322 log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n", 323 inet_ntoa(me->sin_addr)); 324 if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) { 325 /* Oops - that's bad(ish) news ! We've lost an alias ! */ 326 log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n", 327 inet_ntoa(me->sin_addr), strerror(errno)); 328 iface->in_addrs--; 329 bcopy(iface->in_addr + chg + 1, iface->in_addr + chg, 330 (iface->in_addrs - chg) * sizeof iface->in_addr[0]); 331 if (slot > chg) 332 slot--; 333 chg--; 334 } 335 } 336 } 337 } 338 339 if (!nochange) { 340 if (s == -1 && (s = ID0socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 341 log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno)); 342 return 0; 343 } 344 memset(&ifra, '\0', sizeof ifra); 345 strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1); 346 me = (struct sockaddr_in *)&ifra.ifra_addr; 347 msk = (struct sockaddr_in *)&ifra.ifra_mask; 348 peer = (struct sockaddr_in *)&ifra.ifra_broadaddr; 349 me->sin_family = msk->sin_family = peer->sin_family = AF_INET; 350 me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in); 351 me->sin_addr = ifa; 352 msk->sin_addr = mask; 353 peer->sin_addr = brd; 354 355 if (log_IsKept(LogDEBUG)) { 356 char buf[16]; 357 358 strncpy(buf, inet_ntoa(brd), sizeof buf-1); 359 buf[sizeof buf - 1] = '\0'; 360 log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf); 361 } 362 363 /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */ 364 if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && 365 (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) { 366 log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n", 367 inet_ntoa(ifa), strerror(errno)); 368 ID0ioctl(s, SIOCDIFADDR, &ifra); /* EEXIST ? */ 369 close(s); 370 return 0; 371 } 372 } 373 374 if (s != -1) 375 close(s); 376 377 if (slot == iface->in_addrs) { 378 /* We're adding a new interface address */ 379 380 if (how & IFACE_ADD_FIRST) { 381 /* Stuff it at the start of our list */ 382 slot = 0; 383 bcopy(iface->in_addr, iface->in_addr + 1, 384 iface->in_addrs * sizeof iface->in_addr[0]); 385 } 386 387 iface->in_addrs++; 388 } else if (how & IFACE_ADD_FIRST) { 389 /* Shift it up to the first slot */ 390 bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]); 391 slot = 0; 392 } 393 394 iface->in_addr[slot].ifa = ifa; 395 iface->in_addr[slot].mask = mask; 396 iface->in_addr[slot].brd = brd; 397 iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask); 398 399 return 1; 400} 401 402int 403iface_inDelete(struct iface *iface, struct in_addr ip) 404{ 405 int n; 406 407 for (n = 0; n < iface->in_addrs; n++) 408 if (iface->in_addr[n].ifa.s_addr == ip.s_addr) { 409 iface_addr_Zap(iface->name, iface->in_addr + n); 410 bcopy(iface->in_addr + n + 1, iface->in_addr + n, 411 (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]); 412 iface->in_addrs--; 413 return 1; 414 } 415 416 return 0; 417} 418 419#define IFACE_ADDFLAGS 1 420#define IFACE_DELFLAGS 2 421 422static int 423iface_ChangeFlags(const char *ifname, int flags, int how) 424{ 425 struct ifreq ifrq; 426 int s; 427 428 s = ID0socket(AF_INET, SOCK_DGRAM, 0); 429 if (s < 0) { 430 log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno)); 431 return 0; 432 } 433 434 memset(&ifrq, '\0', sizeof ifrq); 435 strncpy(ifrq.ifr_name, ifname, sizeof ifrq.ifr_name - 1); 436 ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0'; 437 if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) { 438 log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n", 439 strerror(errno)); 440 close(s); 441 return 0; 442 } 443 444 if (how == IFACE_ADDFLAGS) 445 ifrq.ifr_flags |= flags; 446 else 447 ifrq.ifr_flags &= ~flags; 448 449 if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) { 450 log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n", 451 strerror(errno)); 452 close(s); 453 return 0; 454 } 455 close(s); 456 457 return 1; /* Success */ 458} 459 460int 461iface_SetFlags(const char *ifname, int flags) 462{ 463 return iface_ChangeFlags(ifname, flags, IFACE_ADDFLAGS); 464} 465 466int 467iface_ClearFlags(const char *ifname, int flags) 468{ 469 return iface_ChangeFlags(ifname, flags, IFACE_DELFLAGS); 470} 471 472void 473iface_Destroy(struct iface *iface) 474{ 475 /* 476 * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually 477 * if that's what the user wants. It's better to leave the interface 478 * allocated so that existing connections can continue to work. 479 */ 480 481 if (iface != NULL) { 482 free(iface->name); 483 free(iface->in_addr); 484 free(iface); 485 } 486} 487 488#define if_entry(x) { IFF_##x, #x } 489 490struct { 491 int flag; 492 const char *value; 493} if_flags[] = { 494 if_entry(UP), 495 if_entry(BROADCAST), 496 if_entry(DEBUG), 497 if_entry(LOOPBACK), 498 if_entry(POINTOPOINT), 499 if_entry(RUNNING), 500 if_entry(NOARP), 501 if_entry(PROMISC), 502 if_entry(ALLMULTI), 503 if_entry(OACTIVE), 504 if_entry(SIMPLEX), 505 if_entry(LINK0), 506 if_entry(LINK1), 507 if_entry(LINK2), 508 if_entry(MULTICAST), 509 { 0, "???" } 510}; 511 512int 513iface_Show(struct cmdargs const *arg) 514{ 515 struct iface *iface = arg->bundle->iface, *current; 516 int f, flags; 517 518 current = iface_Create(iface->name); 519 flags = iface->flags = current->flags; 520 iface_Destroy(current); 521 522 prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index); 523 for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++) 524 if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) { 525 prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",", 526 if_flags[f].value); 527 flags &= ~if_flags[f].flag; 528 } 529 prompt_Printf(arg->prompt, "> mtu %d has %d address%s:\n", arg->bundle->mtu, 530 iface->in_addrs, iface->in_addrs == 1 ? "" : "es"); 531 532 for (f = 0; f < iface->in_addrs; f++) { 533 prompt_Printf(arg->prompt, " %s", inet_ntoa(iface->in_addr[f].ifa)); 534 if (iface->in_addr[f].bits >= 0) 535 prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits); 536 if (iface->flags & IFF_POINTOPOINT) 537 prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd)); 538 else if (iface->flags & IFF_BROADCAST) 539 prompt_Printf(arg->prompt, " broadcast %s", 540 inet_ntoa(iface->in_addr[f].brd)); 541 if (iface->in_addr[f].bits < 0) 542 prompt_Printf(arg->prompt, " (mask %s)", 543 inet_ntoa(iface->in_addr[f].mask)); 544 prompt_Printf(arg->prompt, "\n"); 545 } 546 547 return 0; 548} 549 550void 551iface_ParseHdr(struct ifa_msghdr *ifam, struct sockaddr *sa[RTAX_MAX]) 552{ 553 char *wp; 554 int rtax; 555 556 wp = (char *)(ifam + 1); 557 558 for (rtax = 0; rtax < RTAX_MAX; rtax++) 559 if (ifam->ifam_addrs & (1 << rtax)) { 560 sa[rtax] = (struct sockaddr *)wp; 561 wp += ROUNDUP(sa[rtax]->sa_len); 562 } else 563 sa[rtax] = NULL; 564} 565