annotate toys/other/ifconfig.c @ 1133:f8c926309a21

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