1 /* ifconfig.c - Configure network interface. 2 * 3 * Copyright 2012 Ranjan Kumar <ranjankumar.bth (at) gmail.com> 4 * Copyright 2012 Kyungwan Han <asura321 (at) gamil.com> 5 * Reviewed by Kyungsu Kim <kaspyx (at) gmail.com> 6 * 7 * Not in SUSv4. 8 9 USE_IFCONFIG(NEWTOY(ifconfig, "^?a", TOYFLAG_SBIN)) 10 11 config IFCONFIG 12 bool "ifconfig" 13 default y 14 help 15 usage: ifconfig [-a] [INTERFACE [ACTION...]] 16 17 Display or configure network interface. 18 19 With no arguments, display active interfaces. First argument is interface 20 to operate on, one argument by itself displays that interface. 21 22 -a Show all interfaces, not just active ones 23 24 Additional arguments are actions to perform on the interface: 25 26 ADDRESS[/NETMASK] - set IPv4 address (1.2.3.4/5) 27 default - unset ipv4 address 28 add|del ADDRESS[/PREFIXLEN] - add/remove IPv6 address (1111::8888/128) 29 up - enable interface 30 down - disable interface 31 32 netmask|broadcast|pointopoint ADDRESS - set more IPv4 characteristics 33 hw ether|infiniband ADDRESS - set LAN hardware address (AA:BB:CC...) 34 txqueuelen LEN - number of buffered packets before output blocks 35 mtu LEN - size of outgoing packets (Maximum Transmission Unit) 36 37 Flags you can set on an interface (or -remove by prefixing with -): 38 arp - don't use Address Resolution Protocol to map LAN routes 39 promisc - don't discard packets that aren't to this LAN hardware address 40 multicast - force interface into multicast mode if the driver doesn't 41 allmulti - promisc for multicast packets 42 43 Obsolete fields included for historical purposes: 44 irq|io_addr|mem_start ADDR - micromanage obsolete hardware 45 outfill|keepalive INTEGER - SLIP analog dialup line quality monitoring 46 metric INTEGER - added to Linux 0.9.10 with comment "never used", still true 47 */ 48 49 #define FOR_ifconfig 50 #include "toys.h" 51 52 #include <net/if_arp.h> 53 #include <net/ethernet.h> 54 55 GLOBALS( 56 int sockfd; 57 ) 58 59 // Convert hostname to binary address for AF_INET or AF_INET6 60 // return /prefix (or range max if none) 61 int get_addrinfo(char *host, sa_family_t af, void *addr) 62 { 63 struct addrinfo hints, *result, *rp = 0; 64 int status, len; 65 char *from, *slash; 66 67 memset(&hints, 0 , sizeof(struct addrinfo)); 68 hints.ai_family = af; 69 hints.ai_socktype = SOCK_STREAM; 70 71 slash = strchr(host, '/'); 72 if (slash) *slash = 0; 73 74 status = getaddrinfo(host, NULL, &hints, &result); 75 if (!status) 76 for (rp = result; rp; rp = rp->ai_next) 77 if (rp->ai_family == af) break; 78 if (!rp) error_exit("bad address '%s' : %s", host, gai_strerror(status)); 79 80 // ai_addr isn't struct in_addr or in6_addr, it's struct sockaddr. Of course. 81 // You'd think ipv4 and ipv6 would have some basic compatibility, but no. 82 from = ((char *)rp->ai_addr) + 4; 83 if (af == AF_INET6) { 84 len = 16; 85 from += 4; // skip "flowinfo" field ipv6 puts before address 86 } else len = 4; 87 memcpy(addr, from, len); 88 freeaddrinfo(result); 89 90 len = -1; 91 if (slash) len = atolx_range(slash+1, 0, (af == AF_INET) ? 32 : 128); 92 93 return len; 94 } 95 96 static void display_ifconfig(char *name, int always, unsigned long long val[]) 97 { 98 struct ifreq ifre; 99 struct { 100 int type; 101 char *title; 102 } types[] = { 103 {ARPHRD_LOOPBACK, "Local Loopback"}, {ARPHRD_ETHER, "Ethernet"}, 104 {ARPHRD_PPP, "Point-to-Point Protocol"}, {ARPHRD_INFINIBAND, "InfiniBand"}, 105 {ARPHRD_SIT, "IPv6-in-IPv4"}, {-1, "UNSPEC"} 106 }; 107 int i; 108 char *pp; 109 FILE *fp; 110 short flags; 111 112 xstrncpy(ifre.ifr_name, name, IFNAMSIZ); 113 if (ioctl(TT.sockfd, SIOCGIFFLAGS, &ifre)<0) perror_exit_raw(name); 114 flags = ifre.ifr_flags; 115 if (!always && !(flags & IFF_UP)) return; 116 117 // query hardware type and hardware address 118 i = ioctl(TT.sockfd, SIOCGIFHWADDR, &ifre); 119 120 for (i=0; i < (sizeof(types)/sizeof(*types))-1; i++) 121 if (ifre.ifr_hwaddr.sa_family == types[i].type) break; 122 123 xprintf("%-9s Link encap:%s ", name, types[i].title); 124 if(i >= 0 && ifre.ifr_hwaddr.sa_family == ARPHRD_ETHER) { 125 xprintf("HWaddr "); 126 for (i=0; i<6; i++) xprintf(":%02x"+!i, ifre.ifr_hwaddr.sa_data[i]); 127 } 128 xputc('\n'); 129 130 // If an address is assigned record that. 131 132 ifre.ifr_addr.sa_family = AF_INET; 133 memset(&ifre.ifr_addr, 0, sizeof(ifre.ifr_addr)); 134 ioctl(TT.sockfd, SIOCGIFADDR, &ifre); 135 pp = (char *)&ifre.ifr_addr; 136 for (i = 0; i<sizeof(ifre.ifr_addr); i++) if (pp[i]) break; 137 138 if (i != sizeof(ifre.ifr_addr)) { 139 struct sockaddr_in *si = (struct sockaddr_in *)&ifre.ifr_addr; 140 struct { 141 char *name; 142 int flag, ioctl; 143 } addr[] = { 144 {"addr", 0, 0}, 145 {"P-t-P", IFF_POINTOPOINT, SIOCGIFDSTADDR}, 146 {"Bcast", IFF_BROADCAST, SIOCGIFBRDADDR}, 147 {"Mask", 0, SIOCGIFNETMASK} 148 }; 149 150 xprintf("%10c%s", ' ', (si->sin_family == AF_INET) ? "inet" : 151 (si->sin_family == AF_INET6) ? "inet6" : "unspec"); 152 153 for (i=0; i < sizeof(addr)/sizeof(*addr); i++) { 154 if (!addr[i].flag || (flags & addr[i].flag)) { 155 if (addr[i].ioctl && ioctl(TT.sockfd, addr[i].ioctl, &ifre)) 156 si->sin_family = 0; 157 xprintf(" %s:%s ", addr[i].name, 158 (si->sin_family == 0xFFFF || !si->sin_family) 159 ? "[NOT SET]" : inet_ntoa(si->sin_addr)); 160 } 161 } 162 163 xputc('\n'); 164 } 165 166 fp = fopen(pp = "/proc/net/if_inet6", "r"); 167 if (fp) { 168 char iface_name[IFNAMSIZ]; 169 int plen, iscope; 170 171 while (fgets(toybuf, sizeof(toybuf), fp)) { 172 int nitems; 173 char ipv6_addr[40]; 174 175 nitems = sscanf(toybuf, "%32s %*08x %02x %02x %*02x %15s\n", 176 ipv6_addr, &plen, &iscope, iface_name); 177 if (nitems<0 && feof(fp)) break; 178 if (nitems != 4) perror_exit("bad %s", pp); 179 180 if (!strcmp(name, iface_name)) { 181 struct sockaddr_in6 s6; 182 char *ptr = ipv6_addr+sizeof(ipv6_addr)-1; 183 184 // convert giant hex string into colon-spearated ipv6 address by 185 // inserting ':' every 4 characters. 186 for (i = 32; i; i--) 187 if ((*(ptr--) = ipv6_addr[i])) if (!(i&3)) *(ptr--) = ':'; 188 189 // Convert to binary and back to get abbreviated :: version 190 if (inet_pton(AF_INET6, ipv6_addr, (void *)&s6.sin6_addr) > 0) { 191 if (inet_ntop(AF_INET6, &s6.sin6_addr, toybuf, sizeof(toybuf))) { 192 char *scopes[] = {"Global","Host","Link","Site","Compat"}, 193 *scope = "Unknown"; 194 195 for (i=0; i < sizeof(scopes)/sizeof(*scopes); i++) 196 if (iscope == (!!i)<<(i+3)) scope = scopes[i]; 197 xprintf("%10cinet6 addr: %s/%d Scope: %s\n", 198 ' ', toybuf, plen, scope); 199 } 200 } 201 } 202 } 203 fclose(fp); 204 } 205 206 xprintf("%10c", ' '); 207 208 if (flags) { 209 unsigned short mask = 1; 210 char **s, *str[] = { 211 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS", 212 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST", 213 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL 214 }; 215 216 for (s = str; *s; s++) { 217 if (flags & mask) xprintf("%s ", *s); 218 mask = mask << 1; 219 } 220 } else xprintf("[NO FLAGS] "); 221 222 if (ioctl(TT.sockfd, SIOCGIFMTU, &ifre) < 0) ifre.ifr_mtu = 0; 223 xprintf(" MTU:%d", ifre.ifr_mtu); 224 if (ioctl(TT.sockfd, SIOCGIFMETRIC, &ifre) < 0) ifre.ifr_metric = 0; 225 if (!ifre.ifr_metric) ifre.ifr_metric = 1; 226 xprintf(" Metric:%d", ifre.ifr_metric); 227 228 // non-virtual interface 229 230 if (val) { 231 char *label[] = {"RX bytes", "RX packets", "errors", "dropped", "overruns", 232 "frame", 0, 0, "TX bytes", "TX packets", "errors", "dropped", "overruns", 233 "collisions", "carrier", 0, "txqueuelen"}; 234 signed char order[] = {-1, 1, 2, 3, 4, 5, -1, 9, 10, 11, 12, 14, -1, 235 13, 16, -1, 0, 8}; 236 int i; 237 238 // Query txqueuelen 239 if (ioctl(TT.sockfd, SIOCGIFTXQLEN, &ifre) >= 0) val[16] = ifre.ifr_qlen; 240 else val[16] = -1; 241 242 for (i = 0; i < sizeof(order); i++) { 243 int j = order[i]; 244 245 if (j < 0) xprintf("\n%10c", ' '); 246 else xprintf("%s:%llu ", label[j], val[j]); 247 } 248 } 249 xputc('\n'); 250 251 if(!ioctl(TT.sockfd, SIOCGIFMAP, &ifre) && (ifre.ifr_map.irq || 252 ifre.ifr_map.mem_start || ifre.ifr_map.dma || ifre.ifr_map.base_addr)) 253 { 254 xprintf("%10c", ' '); 255 if(ifre.ifr_map.irq) xprintf("Interrupt:%d ", ifre.ifr_map.irq); 256 if(ifre.ifr_map.base_addr >= 0x100) // IO_MAP_INDEX 257 xprintf("Base address:0x%x ", ifre.ifr_map.base_addr); 258 if(ifre.ifr_map.mem_start) 259 xprintf("Memory:%lx-%lx ", ifre.ifr_map.mem_start, ifre.ifr_map.mem_end); 260 if(ifre.ifr_map.dma) xprintf("DMA chan:%x ", ifre.ifr_map.dma); 261 xputc('\n'); 262 } 263 xputc('\n'); 264 } 265 266 static void show_iface(char *iface_name) 267 { 268 char *name; 269 struct string_list *ifaces = 0, *sl; 270 int i, j; 271 FILE *fp; 272 273 fp = xfopen("/proc/net/dev", "r"); 274 275 for (i=0; fgets(toybuf, sizeof(toybuf), fp); i++) { 276 char *buf = toybuf; 277 unsigned long long val[17]; 278 279 if (i<2) continue; 280 281 while (isspace(*buf)) buf++; 282 name = strsep(&buf, ":"); 283 if(!buf) error_exit("bad name %s", name); 284 285 errno = 0; 286 for (j=0; j<16 && !errno; j++) val[j] = strtoll(buf, &buf, 0); 287 if (errno) perror_exit("bad %s at %s", name, buf); 288 289 if (iface_name) { 290 if (!strcmp(iface_name, name)) { 291 display_ifconfig(iface_name, 1, val); 292 293 return; 294 } 295 } else { 296 sl = xmalloc(sizeof(*sl)+strlen(name)+1); 297 strcpy(sl->str, name); 298 sl->next = ifaces; 299 ifaces = sl; 300 301 display_ifconfig(sl->str, toys.optflags & FLAG_a, val); 302 } 303 } 304 fclose(fp); 305 306 if (iface_name) display_ifconfig(iface_name, 1, 0); 307 else { 308 struct ifconf ifcon; 309 struct ifreq *ifre; 310 int num; 311 312 // Loop until buffer's big enough 313 ifcon.ifc_buf = NULL; 314 for (num = 30;;num += 10) { 315 ifcon.ifc_len = sizeof(struct ifreq)*num; 316 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len); 317 xioctl(TT.sockfd, SIOCGIFCONF, &ifcon); 318 if (ifcon.ifc_len != sizeof(struct ifreq)*num) break; 319 } 320 321 ifre = ifcon.ifc_req; 322 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) 323 { 324 // Skip duplicates 325 for(sl = ifaces; sl; sl = sl->next) 326 if(!strcmp(sl->str, ifre->ifr_name)) break; 327 328 if(!sl) display_ifconfig(ifre->ifr_name, toys.optflags & FLAG_a, 0); 329 } 330 331 free(ifcon.ifc_buf); 332 } 333 334 llist_traverse(ifaces, free); 335 } 336 337 // Encode offset and size of field into an int, and make result negative 338 #define IFREQ_OFFSZ(x) -(int)((offsetof(struct ifreq, x)<<16) + sizeof(ifre.x)) 339 340 void ifconfig_main(void) 341 { 342 char **argv = toys.optargs; 343 struct ifreq ifre; 344 int i; 345 346 TT.sockfd = xsocket(AF_INET, SOCK_DGRAM, 0); 347 if(toys.optc < 2) { 348 show_iface(*argv); 349 return; 350 } 351 352 // Open interface 353 memset(&ifre, 0, sizeof(struct ifreq)); 354 xstrncpy(ifre.ifr_name, *argv, IFNAMSIZ); 355 356 // Perform operations on interface 357 while(*++argv) { 358 // Table of known operations 359 struct argh { 360 char *name; 361 int on, off; // set, clear 362 } try[] = { 363 {0, IFF_UP|IFF_RUNNING, SIOCSIFADDR}, 364 {"up", IFF_UP|IFF_RUNNING, 0}, 365 {"down", 0, IFF_UP}, 366 {"arp", 0, IFF_NOARP}, 367 {"promisc", IFF_PROMISC, 0}, 368 {"allmulti", IFF_ALLMULTI, 0}, 369 {"multicast", IFF_MULTICAST, 0}, 370 {"pointopoint", IFF_POINTOPOINT, SIOCSIFDSTADDR}, 371 {"broadcast", IFF_BROADCAST, SIOCSIFBRDADDR}, 372 {"netmask", 0, SIOCSIFNETMASK}, 373 {"dstaddr", 0, SIOCSIFDSTADDR}, 374 {"mtu", IFREQ_OFFSZ(ifr_mtu), SIOCSIFMTU}, 375 {"keepalive", IFREQ_OFFSZ(ifr_data), SIOCDEVPRIVATE}, // SIOCSKEEPALIVE 376 {"outfill", IFREQ_OFFSZ(ifr_data), SIOCDEVPRIVATE+2}, // SIOCSOUTFILL 377 {"metric", IFREQ_OFFSZ(ifr_metric), SIOCSIFMETRIC}, 378 {"txqueuelen", IFREQ_OFFSZ(ifr_qlen), SIOCSIFTXQLEN}, 379 {"mem_start", IFREQ_OFFSZ(ifr_map.mem_start), SIOCSIFMAP}, 380 {"io_addr", IFREQ_OFFSZ(ifr_map.base_addr), SIOCSIFMAP}, 381 {"irq", IFREQ_OFFSZ(ifr_map.irq), SIOCSIFMAP}, 382 {"inet", 0, 0}, 383 {"inet6", 0, 0} 384 }; 385 char *s = *argv; 386 int rev = (*s == '-'); 387 388 s += rev; 389 390 // "set hardware address" is oddball enough to special case 391 if (!strcmp(*argv, "hw")) { 392 char *hw_addr, *ptr, *p; 393 struct sockaddr *sock = &ifre.ifr_hwaddr; 394 int count = 6; 395 396 ptr = p = (char *)sock->sa_data; 397 memset(sock, 0, sizeof(struct sockaddr)); 398 if (argv[1]) { 399 if (!strcmp("ether", *++argv)) sock->sa_family = ARPHRD_ETHER; 400 else if (!strcmp("infiniband", *argv)) { 401 sock->sa_family = ARPHRD_INFINIBAND; 402 count = 20; 403 p = ptr = toybuf; 404 } 405 } 406 if (!sock->sa_family || !argv[1]) help_exit("bad hw '%s'", *argv); 407 hw_addr = *++argv; 408 409 // Parse and verify address. 410 while (*hw_addr && (p-ptr) < count) { 411 int val, len = 0; 412 413 if (*hw_addr == ':') hw_addr++; 414 sscanf(hw_addr, "%2x%n", &val, &len); 415 if (!len || len > 2) break; // 1 nibble can be set e.g. C2:79:38:95:D:A 416 hw_addr += len; 417 *p++ = val; 418 } 419 420 if ((p-ptr) != count || *hw_addr) 421 error_exit("bad hw-addr '%s'", *argv); 422 423 // the linux kernel's "struct sockaddr" (include/linux/socket.h in the 424 // kernel source) only has 14 bytes of sa_data, and an infiniband address 425 // is 20. So if we go through the ioctl, the kernel will truncate 426 // infiniband addresses, meaning we have to go through sysfs instead. 427 if (sock->sa_family == ARPHRD_INFINIBAND && !strchr(ifre.ifr_name, '/')) { 428 int fd; 429 430 sprintf(toybuf, "/sys/class/net/%s/address", ifre.ifr_name); 431 fd = xopen(toybuf, O_RDWR); 432 xwrite(fd, *argv, strlen(*argv)); 433 close(fd); 434 } else xioctl(TT.sockfd, SIOCSIFHWADDR, &ifre); 435 continue; 436 437 // Add/remove ipv6 address to interface 438 439 } else if (!strcmp(*argv, "add") || !strcmp(*argv, "del")) { 440 struct ifreq_inet6 { 441 struct in6_addr addr; 442 unsigned prefix; 443 int index; 444 } ifre6; 445 int plen, fd6 = xsocket(AF_INET6, SOCK_DGRAM, 0); 446 447 if (!argv[1]) help_exit("%s", *argv); 448 449 plen = get_addrinfo(argv[1], AF_INET6, &ifre6.addr); 450 if (plen < 0) plen = 128; 451 xioctl(fd6, SIOCGIFINDEX, &ifre); 452 ifre6.index = ifre.ifr_ifindex; 453 ifre6.prefix = plen; 454 xioctl(fd6, **(argv++)=='a' ? SIOCSIFADDR : SIOCDIFADDR, &ifre6); 455 456 close(fd6); 457 continue; 458 // Iterate through table to find/perform operation 459 } else for (i = 0; i < ARRAY_LEN(try); i++) { 460 struct argh *t = try+i; 461 int on = t->on, off = t->off; 462 463 if (!t->name) { 464 if (isdigit(**argv) || !strcmp(*argv, "default")) argv--; 465 else continue; 466 } else if (strcmp(t->name, s)) continue; 467 468 // Is this an SIOCSI entry? 469 if ((off|0xff) == 0x89ff) { 470 if (!rev) { 471 if (!*++argv) error_exit("%s needs argument", t->name); 472 473 // Assign value to ifre field and call ioctl? (via IFREQ_OFFSZ.) 474 if (on < 0) { 475 long l = strtoul(*argv, 0, 0); 476 477 if (off == SIOCSIFMAP) xioctl(TT.sockfd, SIOCGIFMAP, &ifre); 478 on = -on; 479 poke((on>>16) + (char *)&ifre, l, on&15); 480 xioctl(TT.sockfd, off, &ifre); 481 break; 482 } else { 483 struct sockaddr_in *si = (struct sockaddr_in *)&ifre.ifr_addr; 484 int mask = -1; 485 486 si->sin_family = AF_INET; 487 488 if (!strcmp(*argv, "default")) si->sin_addr.s_addr = INADDR_ANY; 489 else mask = get_addrinfo(*argv, AF_INET, &si->sin_addr); 490 xioctl(TT.sockfd, off, &ifre); 491 492 // Handle /netmask 493 if (mask >= 0) { 494 // sin_addr probably isn't unaligned, but just in case... 495 mask = htonl((~0)<<(32-mask)); 496 memcpy(&si->sin_addr, &mask, 4); 497 xioctl(TT.sockfd, SIOCSIFNETMASK, &ifre); 498 } 499 } 500 } 501 off = 0; 502 } 503 504 // Set flags 505 if (on || off) { 506 xioctl(TT.sockfd, SIOCGIFFLAGS, &ifre); 507 ifre.ifr_flags &= ~(rev ? on : off); 508 ifre.ifr_flags |= (rev ? off : on); 509 xioctl(TT.sockfd, SIOCSIFFLAGS, &ifre); 510 } 511 512 break; 513 } 514 if (i == sizeof(try)/sizeof(*try)) help_exit("bad argument '%s'", *argv); 515 } 516 close(TT.sockfd); 517 } 518