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