toybox/toys/pending/ip.c
<<
>>
Prefs
   1/* ip.c - Show / manipulate routing, devices, policy routing and tunnels.
   2 *
   3 * Copyright 2014 Sameer Prakash Pradhan <sameer.p.pradhan@gmail.com>
   4 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
   5 * Copyright 2014 Rajni Kant <rajnikant12345@gmail.com>
   6 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
   7 *
   8 * No Standard.
   9 *
  10USE_IP(NEWTOY(ip, NULL, TOYFLAG_SBIN))
  11USE_IP(OLDTOY(ipaddr, ip, TOYFLAG_SBIN))
  12USE_IP(OLDTOY(iplink, ip, TOYFLAG_SBIN))
  13USE_IP(OLDTOY(iproute, ip, TOYFLAG_SBIN))
  14USE_IP(OLDTOY(iprule, ip, TOYFLAG_SBIN))
  15USE_IP(OLDTOY(iptunnel, ip, TOYFLAG_SBIN))
  16
  17config IP
  18  bool "ip"
  19  default n
  20  help
  21    usage: ip [ OPTIONS ] OBJECT { COMMAND }
  22
  23    Show / manipulate routing, devices, policy routing and tunnels.
  24
  25    where OBJECT := {address | link | route | rule | tunnel}
  26    OPTIONS := { -f[amily] { inet | inet6 | link } | -o[neline] }
  27*/
  28#define FOR_ip
  29#include "toys.h"
  30#include <linux/netlink.h>
  31#include <linux/rtnetlink.h>
  32#include <linux/if_ether.h>
  33#include <linux/if_addr.h>
  34#include <net/if_arp.h>
  35#include <ifaddrs.h>
  36#include <fnmatch.h>
  37#include <linux/if_tunnel.h>
  38
  39#ifndef IP_DF
  40#define IP_DF 0x4000  /* don't fragment flag. */
  41#endif
  42
  43GLOBALS(
  44  char stats, singleline, flush, *filter_dev, gbuf[8192];
  45  int sockfd, connected, from_ok, route_cmd;
  46  int8_t addressfamily, is_addr;
  47)
  48
  49struct arglist {
  50  char *name;
  51  int idx;
  52};
  53
  54static struct 
  55{
  56  int ifindex, scope, scopemask, up, to;
  57  char *label, *addr;
  58} addrinfo;
  59
  60struct linkdata  {
  61  struct linkdata *next, *prev;
  62  int flags, iface_idx, mtu, txqueuelen, parent,iface_type;
  63  char qdiscpline[IFNAMSIZ+1], state[IFNAMSIZ+1], type[IFNAMSIZ+1],
  64       iface[IFNAMSIZ+1], laddr[64], bcast[64];
  65  struct  rtnl_link_stats rt_stat;
  66}*linfo;
  67
  68typedef int (*cmdobj)(char **argv);
  69
  70#define MESG_LEN 8192
  71
  72// For "/etc/iproute2/RPDB_tables"
  73enum {
  74  RPDB_rtdsfield = 1,
  75  RPDB_rtprotos = 2,
  76  RPDB_rtrealms = 3,
  77  RPDB_rtscopes = 4,
  78  RPDB_rttables = 5
  79};
  80
  81#define RPDB_ENTRIES 256
  82static int8_t rttable_init;
  83static int8_t rtprotos_init;
  84static int8_t rtdsfield_init;
  85static int8_t rtscope_init;
  86static int8_t rtrealms_init;
  87
  88static struct arglist *rt_dsfield[RPDB_ENTRIES];
  89static struct arglist *rt_protos[RPDB_ENTRIES];
  90static struct arglist *rt_tables[RPDB_ENTRIES];
  91static struct arglist *rt_realms[RPDB_ENTRIES];
  92static struct arglist *rt_scope[RPDB_ENTRIES];
  93
  94static struct arglist rtmtypes[] = { {"none", RTN_UNSPEC},
  95  {"unicast", RTN_UNICAST}, {"local", RTN_LOCAL},
  96  {"broadcast", RTN_BROADCAST}, {"anycast", RTN_ANYCAST},
  97  {"multicast", RTN_MULTICAST}, {"blackhole", RTN_BLACKHOLE},
  98  {"unreachable", RTN_UNREACHABLE}, {"prohibit", RTN_PROHIBIT},
  99  {"throw", RTN_THROW}, {"nat", RTN_NAT},
 100  {"xresolve", RTN_XRESOLVE}, {NULL, -1}
 101};
 102
 103static int filter_nlmesg(int (*fun)(struct nlmsghdr *mhdr, char **), char **);
 104static int  ipaddr_print(struct linkdata *, int flg);
 105
 106// extended route attribute metrics.
 107static const char *mx_names[RTAX_MAX + 1] = {
 108    [RTAX_MTU] = "mtu",
 109    [RTAX_WINDOW] = "window",
 110    [RTAX_RTT] = "rtt",
 111    [RTAX_RTTVAR] = "rttvar",
 112    [RTAX_SSTHRESH] = "ssthresh",
 113    [RTAX_CWND] = "cwnd",
 114    [RTAX_ADVMSS] = "advmss",
 115    [RTAX_REORDERING] = "reordering",
 116    [RTAX_HOPLIMIT] = "hoplimit",
 117    [RTAX_INITCWND] = "initcwnd",
 118    [RTAX_FEATURES] = "features",
 119    [RTAX_RTO_MIN] = "rto_min",
 120    [RTAX_INITRWND] = "initrwnd",
 121    [RTAX_QUICKACK] = "quickack",
 122    [RTAX_CC_ALGO] = "congctl"};
 123
 124// ===========================================================================
 125// Common Code for IP Options (like: addr, link, route etc.)
 126// ===========================================================================
 127static int substring_to_idx(char *str, struct arglist *list)
 128{
 129  struct arglist *alist;
 130  int len;
 131
 132  if (!str) return -1;
 133  len = strlen(str);
 134
 135  for (alist = list; alist->name; alist++)
 136    if (!memcmp(str, alist->name, len)) return alist->idx;
 137  return -1;
 138}
 139
 140static int string_to_idx(char *str, struct arglist *list)
 141{
 142  struct arglist *alist;
 143
 144  if (!str) return -1;
 145  for (alist = list; alist->name; alist++)
 146    if (!strcmp(str, alist->name)) return alist->idx;
 147  return -1;
 148}
 149
 150static char *idx_to_string(int idx, struct arglist *list)
 151{
 152  struct arglist *alist;
 153
 154  if (idx < 0) return NULL;
 155  for (alist = list; alist->name; alist++)
 156    if (idx == alist->idx) return alist->name;
 157  return NULL;
 158}
 159
 160static void send_nlmesg(int type, int flags, int family,
 161    void *buf, int blen)
 162{
 163  struct {
 164    struct nlmsghdr nlh;
 165    struct rtgenmsg g;
 166  } req;
 167
 168  if (!buf) {
 169    memset(&req, 0, sizeof(req));
 170    req.nlh.nlmsg_len = sizeof(req);
 171    req.nlh.nlmsg_type = type;
 172    req.nlh.nlmsg_flags = flags;
 173    req.g.rtgen_family = family;
 174    buf = &req;
 175    blen = sizeof(req);
 176  }
 177  if (send(TT.sockfd , (void*)buf, blen, 0) < 0)
 178    perror_exit("Unable to send data on socket.");
 179}
 180
 181// Parse /etc/iproute2/RPDB_tables and prepare list.
 182static void parseRPDB(char *fname, struct arglist **list, int32_t size)
 183{
 184  char *line;
 185  int fd = open(fname, O_RDONLY);
 186
 187  if (fd < 0) return;
 188  for (; (line = get_line(fd)); free(line)) {
 189    char *ptr = line;
 190    int32_t idx;
 191
 192    while (*ptr == ' ' || *ptr == '\t') ptr++;
 193    if (*ptr == 0 || *ptr == '#' || *ptr == '\n') continue;
 194    if ((sscanf(ptr, "0x%x %s\n", &idx, toybuf) != 2) &&
 195        (sscanf(ptr, "0x%x %s #", &idx, toybuf) != 2) &&
 196        (sscanf(ptr, "%d %s\n", &idx, toybuf) != 2) &&
 197        (sscanf(ptr, "%d %s #", &idx, toybuf) != 2)) {
 198      error_msg("Corrupted '%s' file", fname);
 199      xclose(fd);
 200      free(line);
 201      return;
 202    }
 203    if (idx >= 0 && idx < size) {
 204      int index = idx & (size-1);
 205      if (list[index]) free(list[index]->name);
 206      else list[index] = xzalloc(sizeof(struct arglist));
 207      list[index]->idx = idx;
 208      list[index]->name = xstrdup(toybuf);
 209    }
 210  }
 211  xclose(fd);
 212}
 213
 214static void free_alist(struct arglist **list)
 215{
 216  int i;
 217  for (i = 0;i<RPDB_ENTRIES;i++) {
 218    if (list[i]) {
 219      free(list[i]->name);
 220      free(list[i]);
 221    }
 222  }
 223}
 224
 225static void init_arglist(struct arglist **list,int value, char* name)
 226{
 227  if (!list[value]) list[value] =  xzalloc(sizeof(struct arglist));
 228  list[value]->idx = value;
 229  list[value]->name = xstrdup(name);
 230}
 231
 232static struct arglist **getlist(u_int8_t whichDB)
 233{
 234  struct arglist **alist;
 235
 236  switch (whichDB) {
 237    case RPDB_rtdsfield:
 238      alist = rt_dsfield;
 239      if (!rtdsfield_init) {
 240        rtdsfield_init = 1;
 241        parseRPDB("/etc/iproute2/rt_dsfield", alist, ARRAY_LEN(rt_dsfield));
 242      }
 243      break;
 244    case RPDB_rtprotos:
 245      alist = rt_protos;
 246      if (!rttable_init) {
 247        rtprotos_init = 1;
 248        init_arglist(rt_protos,0,"none");
 249        init_arglist(rt_protos,1,"redirect");
 250        init_arglist(rt_protos,2,"kernel");
 251        init_arglist(rt_protos,3,"boot");
 252        init_arglist(rt_protos,4,"static");
 253        init_arglist(rt_protos,8,"gated");
 254        init_arglist(rt_protos,9,"ra");
 255        init_arglist(rt_protos,10,"mrt");
 256        init_arglist(rt_protos,11,"zebra");
 257        init_arglist(rt_protos,12,"bird");
 258        parseRPDB("/etc/iproute2/rt_protos", alist, ARRAY_LEN(rt_protos));
 259      }
 260      break;
 261    case RPDB_rtrealms:
 262      alist = rt_realms;
 263      if (!rtrealms_init) {
 264        rtrealms_init = 1;
 265        init_arglist(rt_realms,0,"unspec");
 266        parseRPDB("/etc/iproute2/rt_realms", alist, ARRAY_LEN(rt_realms));
 267      }
 268      break;
 269    case RPDB_rtscopes:
 270      alist = rt_scope;
 271      if (!rtscope_init) {
 272        rtscope_init = 1;
 273        init_arglist(rt_scope,0,"global");
 274        init_arglist(rt_scope,200,"site");
 275        init_arglist(rt_scope,253,"link");
 276        init_arglist(rt_scope,254,"host");
 277        init_arglist(rt_scope,255,"nowhere");
 278        parseRPDB("/etc/iproute2/rt_scopes", alist, ARRAY_LEN(rt_scope));
 279      }
 280      break;
 281    case RPDB_rttables:
 282      alist = rt_tables;
 283      if (!rttable_init) {
 284        rttable_init = 1;
 285        init_arglist(rt_tables,RT_TABLE_DEFAULT,"default");
 286        init_arglist(rt_tables,RT_TABLE_MAIN,"main");
 287        init_arglist(rt_tables,RT_TABLE_LOCAL,"local");
 288        parseRPDB("/etc/iproute2/rt_tables", alist, ARRAY_LEN(rt_tables));
 289      }
 290      break;
 291    default: 
 292      error_exit("wrong database");
 293      break; // Unreachable code.
 294  }
 295  return alist;
 296}
 297
 298/*
 299 * Parse RPBD tables (if not parsed already).
 300 * return RPDB table name as per idx.
 301 */
 302static char *namefromRPDB(int idx, u_int8_t whichDB)
 303{
 304  struct arglist **alist;
 305
 306  if (idx < 0 || idx >= RPDB_ENTRIES) {
 307    snprintf(toybuf, RPDB_ENTRIES, "%u", idx);
 308    return toybuf;
 309  }
 310
 311  alist = getlist(whichDB);
 312
 313  if (alist[idx] && alist[idx]->name) return alist[idx]->name;
 314
 315  if (whichDB == RPDB_rtdsfield) snprintf(toybuf, RPDB_ENTRIES, "0x%02x", idx);
 316  else snprintf(toybuf, RPDB_ENTRIES, "%u", idx);
 317
 318  return toybuf;
 319}
 320
 321static int idxfromRPDB(char *name, u_int8_t whichDB)
 322{
 323  struct arglist **alist;
 324  long i = 0;
 325  char *ptr = NULL;
 326
 327  for (alist = getlist(whichDB); i < RPDB_ENTRIES; i++) {
 328    if (!alist[i] || !alist[i]->name) continue;
 329    if (!strcmp(alist[i]->name, name)) return i;
 330  }
 331  i = strtol(name, &ptr, 0);
 332  if (errno || (ptr && *ptr) || i < 0 || i > 255)
 333    return -1;
 334  return i;
 335}
 336
 337static char *rtmtype_idx2str(u_int8_t idx)
 338{
 339  char *name = idx_to_string(idx, rtmtypes);
 340
 341  if (!name) snprintf(toybuf, RPDB_ENTRIES, "%u", idx);
 342  else snprintf(toybuf, sizeof(toybuf), "%s", name);
 343  return toybuf;
 344}
 345
 346static int rtmtype_str2idx(char *name)
 347{
 348  int idx = string_to_idx(name, rtmtypes);
 349
 350  if (idx < 0) return atolx_range(name, 0, 255);
 351  return idx;
 352}
 353
 354/*
 355 * Used to get the prefix value in binary form.
 356 * For IPv4: non-standard parsing used; as 10.10 will be treated as 10.10.0.0
 357 * unlike inet_aton which is 10.0.0.10
 358 */
 359static int get_prefix(uint32_t *addr, uint8_t *af, char *name, int family)
 360{
 361  if (family == AF_PACKET) error_exit("'%s' may be inet prefix", name);
 362  if (!memcmp(name, "default", strlen(name))
 363      || !memcmp(name, "all", strlen(name))
 364      || !memcmp(name, "any", strlen(name))) {
 365    *af = family;
 366    return 0;
 367  }
 368  if (strchr(name, ':')) {
 369    *af = AF_INET6;
 370    if (family != AF_UNSPEC && family != AF_INET6) return 1;
 371    if (inet_pton(AF_INET6, name, (void *)addr) != 1) 
 372      return 1;
 373  } else { // for IPv4.
 374    char *ptr = name;
 375    uint8_t count = 0;
 376
 377    *af = AF_INET;
 378    if (family != AF_UNSPEC && family != AF_INET) return 1;
 379    while (*ptr) {
 380      int val, len = 0;
 381
 382      if (*ptr == '.') ptr++;
 383      sscanf(ptr, "%d%n", &val, &len);
 384      if (!len || len > 3 || val < 0 || val > 255 || count > 3) return 1;
 385      ptr += len;
 386      ((uint8_t*)addr)[count++] = val;
 387    }
 388  }
 389  return 0;
 390}
 391
 392/*
 393 * Used to calculate netmask, which can be in the form of
 394 * either 255.255.255.0 or 24 or default or any or all strings.
 395 */
 396static int get_nmask_prefix(uint32_t *netmask, uint8_t af,
 397    char *name, uint8_t family)
 398{
 399  char *ptr;
 400  uint32_t naddr[4] = {0,};
 401  uint64_t plen;
 402  uint8_t naf = AF_UNSPEC;
 403
 404  *netmask = (af == AF_INET6) ? 128 : 32; // set default netmask
 405  plen = strtoul(name, &ptr, 0);
 406
 407  if (!ptr || ptr == name || *ptr || !plen || plen > *netmask) {
 408    if (get_prefix(naddr, &naf, name, family)) return -1;
 409    if (naf == AF_INET) {
 410      uint32_t mask = htonl(*naddr), host = ~mask;
 411      if (host & (host + 1)) return -1;
 412      for (plen = 0; mask; mask <<= 1) ++plen;
 413      if (plen > 32) return -1;
 414    }
 415  }
 416  *netmask = plen;
 417  return 0;
 418}
 419
 420/*
 421 * Parse prefix, which will be in form of
 422 * either default or default/default or default/24 or default/255.255.255.0
 423 * or 10.20.30.40 or 10.20.30.40/default or 10.20.30.40/24
 424 * or 10.20.30.40/255.255.255.0
 425 */
 426static void parse_prefix(uint32_t *addr, uint32_t *netmask, uint8_t *len,
 427    char *name, int family)
 428{
 429  uint8_t af = AF_UNSPEC;
 430  char *slash = strchr(name, '/');
 431
 432  if (slash) *slash = 0;
 433  if (get_prefix(addr, &af, name, family)) error_exit("Invalid prefix");
 434
 435  if (slash) { // grab netmask.
 436    if (get_nmask_prefix(netmask, af, slash+1, family))
 437      error_exit("Invalid prefix");
 438    *slash ='/';
 439  }
 440  else if (af == AF_INET && *addr) *netmask = 32;
 441  else if (af == AF_INET6 && (*addr || *(addr+3))) *netmask = 128;
 442
 443  if (!*addr && !slash && !af) *len = 0;
 444  else *len = (af == AF_INET6) ? 16 : 4;
 445}
 446
 447/*
 448 * Add a route attribute to a buffer; this is primarily used for extended
 449 * attributes which get collected in a separate buffer from the normal route
 450 * attributes and later get added to the main rtm message.
 451 */
 452static void add_varlen_rtattr_to_buffer(struct rtattr *rta, int maxlen,
 453                                        int type, void *data, int alen) {
 454  struct rtattr *subrta;
 455  int len = RTA_LENGTH(alen);
 456  if (RTA_ALIGN(rta->rta_len) + RTA_ALIGN(len) > maxlen) {
 457      error_exit("RTA exceeds max length %d", maxlen);
 458  }
 459  subrta = (struct rtattr*)(((char*)rta) + RTA_ALIGN(rta->rta_len));
 460  subrta->rta_type = type;
 461  subrta->rta_len = len;
 462  if (alen) {
 463    memcpy(RTA_DATA(subrta), data, alen);
 464  }
 465  rta->rta_len = NLMSG_ALIGN(rta->rta_len) + RTA_ALIGN(len);
 466}
 467
 468static void add_uint32_rtattr_to_buffer(struct rtattr *rta, int maxlen,
 469                                        int type, uint32_t attr) {
 470  add_varlen_rtattr_to_buffer(rta, maxlen, type, (char*)&attr, sizeof(attr));
 471}
 472
 473/*
 474 * Add a route attribute to a RTM message.
 475 */
 476static void add_string_to_rtattr(struct nlmsghdr *n, int maxlen,
 477    int type, void *data, int alen)
 478{
 479  int len = RTA_LENGTH(alen);
 480  struct rtattr *rta;
 481
 482  if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) return;
 483  rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len));
 484  rta->rta_type = type;
 485  rta->rta_len = len;
 486  memcpy(RTA_DATA(rta), data, alen);
 487  n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len;
 488}
 489
 490
 491
 492// ===========================================================================
 493// Code for ip link.
 494// ===========================================================================
 495#ifndef NLMSG_TAIL
 496#define NLMSG_TAIL(nmsg) \
 497  ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
 498#endif
 499
 500static uint32_t get_ifaceindex(char *name, int ext)
 501{
 502  struct if_nameindex *if_ni, *i;
 503  int index = -1;
 504
 505  if_ni = if_nameindex();
 506  if (!if_ni) perror_exit("if_nameindex");
 507
 508  for (i = if_ni; i->if_index && i->if_name; i++)
 509    if (!strcmp(name, i->if_name)) { 
 510      index = i->if_index; 
 511      break;
 512    }
 513  if_freenameindex(if_ni);
 514  if (index == -1 && ext) perror_exit("can't find device '%s'", name);
 515  return index;
 516}
 517
 518static void fill_hwaddr(char *arg, int len, unsigned char *address)
 519{
 520  int count = 0, val, length;
 521
 522  while (count < len) {
 523    val = length = 0;
 524    if (!arg) error_exit("bad hw-addr '%s'", "");
 525    if (*arg == ':') arg++, count++;
 526    sscanf(arg, "%2x%n", &val, &length);
 527    if (!length || length > 2)
 528      error_exit("bad hw-addr '%s'", arg);
 529    arg += length;
 530    count += length;
 531    *address++ = val;
 532  }
 533}
 534
 535// Multimach = 1, single match = 0
 536static char *get_flag_string(struct arglist *aflags, int flags, int ismulti)
 537{
 538  struct arglist *p = aflags;
 539  char *out = NULL, *tmp = NULL;
 540
 541  for (; p->name; p++) {
 542    int test = (ismulti ? p->idx & flags : 0) || p->idx == flags;
 543    if (test) { // flags can be zero
 544      tmp = out ? xmprintf("%s,%s", out, p->name) : xmprintf("%s", p->name);
 545      if (out) free(out);
 546      out = tmp;
 547    }
 548  }
 549  return out;
 550}
 551
 552static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
 553{
 554  struct arglist vlan_optlist[] = {{"id", 0}, {"protocol", 1}, 
 555    {"reorder_hdr", 2}, {"gvrp", 3}, {NULL,-1}};
 556  struct arglist vlan_protolist[] = {{"802.1q", 0}, {"802.1ad", 1}, {NULL,-1}};
 557  struct arglist on_off[] = { {"on", 0}, {"off", 1}, {NULL,-1}};
 558  int idx;
 559  struct ifla_vlan_flags flags;
 560
 561  memset(&flags, 0, sizeof(flags));
 562  for (; *argv; argv++) {
 563    int param, proto;
 564
 565    if ((idx = substring_to_idx(*argv++, vlan_optlist)) == -1) help_exit(0);
 566    switch (idx) {
 567      case 0: // ARG_id
 568        if (!*argv) help_exit(0);
 569        param = atolx(*argv);
 570        add_string_to_rtattr(n, size, IFLA_VLAN_ID, &param, sizeof(param));
 571        break;
 572      case 1: // ARG_protocol
 573        if (!*argv) error_exit("Invalid vlan id.");
 574        if ((idx = substring_to_idx(*argv, vlan_protolist)) == -1) help_exit(0);
 575        if (!idx) proto = ETH_P_8021Q; // PROTO_8021Q - 0
 576        else if (idx == 1) proto = 0x88A8; // ETH Protocol - 8021AD
 577        // IFLA VLAN PROTOCOL - 5
 578        add_string_to_rtattr(n, size, 5, &proto, sizeof(proto));
 579        break;
 580      case 2: // ARG_reorder_hdr
 581      case 3: // ARG_gvrp
 582        if ((param = substring_to_idx(*argv, on_off)) == -1) help_exit(0);
 583
 584        flags.mask |= (idx -1); // VLAN FLAG REORDER Header              
 585        flags.flags &= ~(idx -1); // VLAN FLAG REORDER Header            
 586        if (!param) flags.flags |= (idx -1); // VLAN FLAG REORDER Header
 587        break;
 588    }
 589  }
 590  if (flags.mask) 
 591    add_string_to_rtattr(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
 592}
 593
 594static int linkupdate(char **argv)
 595{
 596  struct {
 597    struct nlmsghdr mhdr;
 598    struct ifinfomsg info;
 599    char buf[1024];
 600  } request;  
 601  char *name, *dev, *type, *link, *addr;
 602  struct rtattr *attr = NULL;
 603  int len = 0, add = (*argv[-1] == 'a') ? 1 : 0;
 604
 605  name = dev = type = link = addr = NULL;
 606  for (; *argv; argv++) {
 607    struct arglist objectlist[] = { {"type", 0}, {"name", 1}, {"link", 2}, 
 608      {"address", 3}, {NULL,-1}};
 609    uint8_t idx = substring_to_idx(*argv, objectlist);
 610
 611    if (!idx) {
 612      type = *++argv;
 613      break;
 614    }
 615    else if (idx == 1) dev = name = *++argv;
 616    else if (idx == 2) link = *++argv;
 617    else if (idx == 3) addr = *++argv;
 618    else if (!dev) name = dev = *argv;
 619  }
 620
 621  if (!name && !add)
 622    error_exit("Not enough information: \"dev\" argument is required.\n");
 623  else if (!type  && add)
 624    error_exit("Not enough information: \"type\" argument is required.\n");
 625
 626  memset(&request, 0, sizeof(request));
 627  request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
 628  request.mhdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
 629  if (add) {
 630    request.mhdr.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
 631    request.mhdr.nlmsg_type = RTM_NEWLINK;
 632  } else {
 633    request.mhdr.nlmsg_type = RTM_DELLINK;
 634    request.info.ifi_index = get_ifaceindex(name, 1);
 635  }
 636  request.info.ifi_family = AF_UNSPEC;
 637  attr = NLMSG_TAIL(&request.mhdr);
 638  if (type) {
 639    add_string_to_rtattr(&request.mhdr, sizeof(request),
 640        IFLA_LINKINFO, NULL, 0);
 641    add_string_to_rtattr(&request.mhdr, sizeof(request),
 642        IFLA_INFO_KIND, type, strlen(type));
 643    if (!strcmp(type, "vlan")) {
 644      struct rtattr *data = NLMSG_TAIL(&request.mhdr);
 645      add_string_to_rtattr(&request.mhdr, sizeof(request), 
 646          IFLA_INFO_DATA, NULL, 0);
 647      vlan_parse_opt(++argv, &request.mhdr, sizeof(request));
 648      data->rta_len = (void *)NLMSG_TAIL(&request.mhdr) - (void *)data;
 649    }
 650    attr->rta_len = (void *)NLMSG_TAIL(&request.mhdr) - (void *)attr;
 651  }
 652
 653  if (link) {
 654    uint32_t idx = get_ifaceindex(link, 1);
 655    add_string_to_rtattr(&request.mhdr, sizeof(request), 
 656        IFLA_LINK, &idx, sizeof(uint32_t));
 657  }
 658  if (addr) {
 659    char abuf[IF_NAMESIZE] = {0,};
 660
 661    fill_hwaddr(addr, IF_NAMESIZE, (unsigned char *)abuf);
 662    add_string_to_rtattr(&request.mhdr, sizeof(request), 
 663        IFLA_ADDRESS, abuf, strlen(abuf));
 664  }
 665  if (!name) {
 666    snprintf(toybuf, IFNAMSIZ, "%s%d", type, 0);
 667    for (len = 1; ; len++) {
 668      if (!get_ifaceindex(toybuf, 0)) break;
 669      snprintf(toybuf, IFNAMSIZ, "%s%d", type, len);
 670    }
 671    name = toybuf;
 672  }
 673  len = strlen(name) + 1;
 674  if (len < 2 || len > IFNAMSIZ) error_exit("Invalid device name.");
 675  add_string_to_rtattr(&request.mhdr, sizeof(request), IFLA_IFNAME, name, len);
 676
 677  send_nlmesg(0, 0, 0, (void *)&request, request.mhdr.nlmsg_len);
 678  return (filter_nlmesg(NULL,NULL));
 679}
 680
 681static int link_set(char **argv)
 682{
 683  struct arglist cmd_objectlist[] = {{"up", 0}, {"down", 1}, {"arp", 2}, 
 684    {"multicast", 3}, {"dynamic", 4}, {"name", 5}, {"txqueuelen", 6}, 
 685    {"mtu", 7},{"address", 8}, {"broadcast", 9}, {NULL,-1}};
 686  int case_flags[] = {IFF_NOARP,IFF_MULTICAST,IFF_DYNAMIC};
 687  struct ifreq req;
 688  int idx, flags = 0, masks = 0xffff, fd;
 689
 690  memset(&req, 0, sizeof(req));
 691  if (!*argv) error_exit("\"dev\" missing");
 692  xstrncpy(req.ifr_name, *argv, IF_NAMESIZE);
 693  fd = xsocket(AF_INET, SOCK_DGRAM, 0);
 694  xioctl(fd, SIOCGIFINDEX, &req);
 695  for (++argv; *argv;) {
 696    if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) help_exit(0);
 697    switch(idx) {
 698      case 0:
 699        flags |= IFF_UP; break;
 700      case 1:
 701        masks &= ~IFF_UP; break;
 702      case 2:
 703      case 3:
 704      case 4:
 705        if (!*argv) help_exit(0);
 706        else if (!strcmp(*argv, "on")) {
 707          if (idx == 2) {
 708            masks &= ~case_flags[idx-2];
 709            flags &= ~case_flags[idx-2];
 710          } else flags |= case_flags[idx-2];
 711        } else if (!strcmp(*argv,"off")) {
 712          if (idx == 2) {
 713            masks |= case_flags[idx-2];
 714            flags |= case_flags[idx-2];
 715          } else masks &= ~case_flags[idx-2];
 716        } else help_exit(0);
 717        ++argv;
 718        break;
 719      case 5:
 720        xstrncpy(req.ifr_ifru.ifru_newname, *argv, IF_NAMESIZE);
 721        xioctl(fd, SIOCSIFNAME, &req);
 722        xstrncpy(req.ifr_name, *argv++, IF_NAMESIZE);
 723        xioctl(fd, SIOCGIFINDEX, &req);
 724        break;
 725      case 6:
 726        req.ifr_ifru.ifru_ivalue = atolx(*argv++);
 727        xioctl(fd, SIOCSIFTXQLEN, &req);
 728        break;
 729      case 7:
 730        req.ifr_ifru.ifru_mtu = atolx(*argv++);
 731        xioctl(fd, SIOCSIFMTU, &req);
 732        break;
 733      case 8:
 734        xioctl(fd, SIOCGIFHWADDR, &req);
 735        fill_hwaddr(*argv++, IF_NAMESIZE, 
 736            (unsigned char *)(req.ifr_hwaddr.sa_data));
 737        xioctl(fd, SIOCSIFHWADDR, &req);
 738        break;
 739      case 9:
 740        xioctl(fd, SIOCGIFHWADDR, &req);
 741        fill_hwaddr(*argv++, IF_NAMESIZE,
 742            (unsigned char *)(req.ifr_hwaddr.sa_data));
 743        xioctl(fd, SIOCSIFHWBROADCAST, &req);
 744        break;
 745    }
 746  }
 747  xioctl(fd, SIOCGIFFLAGS, &req);
 748  req.ifr_ifru.ifru_flags |= flags;
 749  req.ifr_ifru.ifru_flags &= masks;
 750  xioctl(fd, SIOCSIFFLAGS, &req);
 751  xclose(fd);
 752  return 0;
 753}
 754
 755static void print_stats(struct  rtnl_link_stats *rtstat)
 756{
 757  char *line_feed = (!TT.singleline ? "\n    " : " ");
 758
 759  if (TT.stats > 0) {
 760    xprintf("    RX: bytes  packets  errors  "
 761        "dropped  overrun  mcast%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
 762        line_feed, rtstat->rx_bytes, rtstat->rx_packets, rtstat->rx_errors,
 763        rtstat->rx_dropped, rtstat->rx_over_errors, rtstat->multicast);
 764    if (TT.stats > 1) {
 765      xprintf("    RX: errors  length  crc  "
 766          "frame  fifo  missed%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
 767          line_feed, rtstat->rx_errors, rtstat->rx_length_errors,
 768          rtstat->rx_crc_errors, rtstat->rx_frame_errors,
 769          rtstat->rx_fifo_errors, rtstat->rx_missed_errors);
 770    }
 771    xprintf("    TX: bytes  packets  errors  "
 772        "dropped  carrier  collsns%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
 773        line_feed, rtstat->tx_bytes, rtstat->tx_packets, rtstat->tx_errors,
 774        rtstat->tx_dropped, rtstat->tx_carrier_errors, rtstat->collisions);
 775    if (TT.stats > 1) {
 776      xprintf("    TX: errors  aborted  fifo  window  "
 777          "heartbeat%s%-10u %-8u %-7u %-8u %-8u\n",
 778          line_feed, rtstat->tx_errors, rtstat->tx_aborted_errors,
 779          rtstat->tx_fifo_errors, rtstat->tx_window_errors, 
 780          rtstat->tx_heartbeat_errors);
 781    }
 782  }
 783}
 784
 785static int print_link_output(struct linkdata *link)
 786{
 787  char *line_feed = " ", *flags,*peer = "brd";
 788  struct arglist iface_flags[] = {{"",0},{"UP", IFF_UP}, 
 789    {"BROADCAST", IFF_BROADCAST}, {"DEBUG", IFF_DEBUG},
 790    {"LOOPBACK", IFF_LOOPBACK}, {"POINTOPOINT", IFF_POINTOPOINT},
 791    {"NOTRAILERS", IFF_NOTRAILERS}, {"RUNNING", IFF_RUNNING},
 792    {"NOARP", IFF_NOARP}, {"PROMISC",IFF_PROMISC},
 793    {"ALLMULTI", IFF_ALLMULTI}, {"MASTER", IFF_MASTER}, {"SLAVE", IFF_SLAVE},
 794    {"MULTICAST", IFF_MULTICAST}, {"PORTSEL", IFF_PORTSEL},
 795    {"AUTOMEDIA", IFF_AUTOMEDIA}, {"DYNAMIC", IFF_DYNAMIC}, {NULL,-1}};
 796
 797  if (link->parent != -1) {
 798    int fd = 0;
 799    struct ifreq req;
 800
 801    memset(&req, 0, sizeof(req));
 802    if_indextoname( link->parent,req.ifr_ifrn.ifrn_name);
 803    fd = xsocket(AF_INET, SOCK_DGRAM, 0);
 804    if (ioctl(fd, SIOCGIFTXQLEN, &req)) perror("");
 805    else link->txqueuelen = req.ifr_ifru.ifru_ivalue;
 806    xclose(fd);
 807  }
 808
 809  if (TT.is_addr && addrinfo.label && fnmatch(addrinfo.label, link->iface, 0))
 810    return 0;
 811
 812
 813  if (!(flags = get_flag_string(iface_flags, link->flags, 1)))
 814    error_exit("Invalid data.");    
 815  if (!TT.singleline) line_feed="\n    ";
 816  if (link->parent != -1) {
 817    char iface[IF_NAMESIZE];
 818
 819    if (!if_indextoname(link->parent, iface)) perror_exit(NULL);
 820    sprintf(toybuf,"%s@%s", link->iface, iface);
 821  }
 822  if (link->flags & IFF_POINTOPOINT) peer = "peer";
 823  if (TT.is_addr && TT.singleline && TT.addressfamily)
 824    xprintf("%d: %s", link->iface_idx,
 825        ((link->parent == -1) ? link->iface : toybuf));
 826  else xprintf("%d: %s: <%s> mtu %d qdisc %s state %s qlen %d",
 827      link->iface_idx, ((link->parent == -1) ? link->iface : toybuf), flags,
 828      link->mtu, link->qdiscpline, link->state, link->txqueuelen);
 829
 830  if (!TT.addressfamily || TT.addressfamily == AF_PACKET)
 831    xprintf("%slink/%s %s %s %s",
 832        line_feed, link->type, link->laddr, peer ,link->bcast);
 833
 834  xputc('\n');
 835
 836  //user can specify stats flag two times
 837  //one for stats and other for erros e.g. -s and -s -s
 838  print_stats(&link->rt_stat);
 839  free(flags);
 840
 841  return 0;
 842}
 843
 844static void fill_address(void *p, char *ip)
 845{
 846  unsigned char *ptr = (unsigned char*)p;
 847  snprintf(ip, 64, " %02x:%02x:%02x:%02x:%02x:%02x",
 848      ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
 849}
 850
 851static int get_link_info(struct nlmsghdr* h,struct linkdata* link,char **argv)
 852{
 853  struct ifinfomsg *iface = NLMSG_DATA(h);
 854  struct rtattr *attr = IFLA_RTA(iface);
 855  int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*iface));
 856  struct arglist hwtypes[]={{"generic",0},{"ether",ARPHRD_ETHER},
 857    {"loopback", ARPHRD_LOOPBACK},{"sit",ARPHRD_SIT},
 858#ifdef ARPHRD_INFINIBAND
 859    {"infiniband",ARPHRD_INFINIBAND},
 860#endif
 861#ifdef ARPHRD_IEEE802_TR
 862    {"ieee802",ARPHRD_IEEE802}, {"tr",ARPHRD_IEEE802_TR},
 863#else
 864    {"tr",ARPHRD_IEEE802},
 865#endif
 866#ifdef ARPHRD_IEEE80211
 867    {"ieee802.11",ARPHRD_IEEE80211},
 868#endif
 869#ifdef ARPHRD_IEEE1394
 870    {"ieee1394",ARPHRD_IEEE1394},
 871#endif
 872    {"irda",ARPHRD_IRDA},{"slip",ARPHRD_SLIP},{"cslip",ARPHRD_CSLIP},
 873    {"slip6",ARPHRD_SLIP6}, {"cslip6",ARPHRD_CSLIP6}, {"ppp",ARPHRD_PPP},
 874    {"ipip",ARPHRD_TUNNEL}, {"tunnel6",ARPHRD_TUNNEL6},
 875    {"gre",ARPHRD_IPGRE},
 876#ifdef ARPHRD_VOID
 877    {"void",ARPHRD_VOID},
 878#endif
 879    {NULL,-1}};
 880  char *lname = get_flag_string(hwtypes, iface->ifi_type, 0);
 881
 882  link->next = link->prev = 0;
 883  link->iface_type = iface->ifi_type;
 884  if (!lname) error_exit("Invalid link.");
 885  xstrncpy(link->type, lname, IFNAMSIZ);
 886  free(lname);
 887  link->iface_idx = iface->ifi_index;
 888  link->flags = iface->ifi_flags;
 889  if (*argv && !strcasecmp("up",*argv) && !(link->flags & IFF_UP)) return 1;
 890  link->parent =  -1;
 891  for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
 892    switch(attr->rta_type) {
 893      case IFLA_IFNAME:
 894        snprintf(link->iface, IFNAMSIZ, "%s",(char *) RTA_DATA(attr));
 895        break;
 896      case IFLA_ADDRESS:
 897        if ( iface->ifi_type== ARPHRD_TUNNEL ||
 898            iface->ifi_type == ARPHRD_SIT ||
 899            iface->ifi_type == ARPHRD_IPGRE)
 900          inet_ntop(AF_INET, RTA_DATA(attr), link->laddr, 64);
 901        else fill_address(RTA_DATA(attr), link->laddr);
 902        break;
 903      case IFLA_BROADCAST:
 904        if (iface->ifi_type== ARPHRD_TUNNEL ||
 905            iface->ifi_type == ARPHRD_SIT ||
 906            iface->ifi_type == ARPHRD_IPGRE)
 907          inet_ntop(AF_INET, RTA_DATA(attr), link->bcast, 64);
 908        else  fill_address(RTA_DATA(attr), link->bcast);
 909        break;
 910      case IFLA_MTU:
 911        link->mtu = *((int*)(RTA_DATA(attr)));
 912        break;
 913      case IFLA_QDISC:
 914        snprintf(link->qdiscpline, IFNAMSIZ, "%s", (char *) RTA_DATA(attr));
 915        break;
 916      case IFLA_STATS  :
 917        link->rt_stat = *((struct rtnl_link_stats*) RTA_DATA(attr));
 918        break;
 919      case IFLA_LINK:
 920        link->parent = *((int*)(RTA_DATA(attr)));
 921        break;
 922      case IFLA_TXQLEN:
 923        link->txqueuelen = *((int*)(RTA_DATA(attr)));
 924        break;
 925      case IFLA_OPERSTATE: 
 926        {
 927          struct arglist flags[]={{"UNKNOWN", 0}, {"NOTPRESENT", 1}, 
 928            {"DOWN", 2}, {"LOWERLAYERDOWN", 3}, {"TESTING", 4}, 
 929            {"DORMANT", 5}, {"UP", 6}, {NULL, -1}};
 930          if (!(lname = get_flag_string(flags, *((int*)(RTA_DATA(attr))), 0)))
 931            error_exit("Invalid state.");
 932          xstrncpy(link->state, lname,IFNAMSIZ);
 933          free(lname);
 934        }
 935        break;
 936      default: break;
 937    }
 938  }
 939  return 0;
 940}
 941
 942static int display_link_info(struct nlmsghdr *mhdr, char **argv)
 943{
 944  struct linkdata link;
 945
 946  if (!get_link_info(mhdr, &link, argv)) {
 947    if (TT.is_addr) {
 948      struct linkdata *lnk = xzalloc(sizeof(struct linkdata));
 949      memcpy(lnk, &link, sizeof(struct linkdata));
 950      dlist_add_nomalloc((struct double_list **)&linfo,
 951          (struct double_list *)lnk);
 952    }
 953    else print_link_output(&link);
 954  }
 955  return 0;
 956}
 957
 958static int link_show(char **argv)
 959{
 960  struct {
 961    struct nlmsghdr mhdr;
 962    struct ifinfomsg info;
 963  } request;
 964  uint32_t index = 0;
 965
 966  if (*argv && strcasecmp("up",*argv)) index = get_ifaceindex(*argv, 1);
 967  memset(&request, 0, sizeof(request));
 968  request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
 969  request.mhdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
 970  if (!index) request.mhdr.nlmsg_flags |= NLM_F_ROOT|NLM_F_MATCH;
 971  else request.info.ifi_change =  0xffffffff; // used in single operation
 972  request.mhdr.nlmsg_type = RTM_GETLINK;
 973  request.info.ifi_index = index;
 974  request.info.ifi_family = AF_UNSPEC;
 975  send_nlmesg(0, 0, 0, (void*)&request, sizeof(request));
 976  return (filter_nlmesg(display_link_info, argv));
 977}
 978
 979static int iplink(char **argv)
 980{
 981  int idx;
 982  cmdobj ipcmd, cmdobjlist[] = {linkupdate, link_set, link_show};
 983  struct arglist cmd_objectlist[] = {{"add", 0}, {"delete", 0},
 984    {"set", 1}, {"show", 2}, {"list", 2}, {"lst", 2}, {NULL,-1}};
 985
 986  if (!*argv) idx = 2;
 987  else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1)
 988    help_exit(0);
 989  ipcmd = cmdobjlist[idx];
 990  return ipcmd(argv);
 991}
 992
 993// ===========================================================================
 994// Code for ip addr.
 995// ===========================================================================
 996
 997static int print_addrinfo(struct nlmsghdr *h, int flag_l)
 998{
 999  struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,};
1000  char *family = toybuf, *scope = toybuf+256, *label = toybuf+512,
1001       *brd = toybuf+768, *peer = toybuf+1024, *any = toybuf+1280,
1002       lbuf[INET6_ADDRSTRLEN] = {0,}, lbuf_ifa[INET6_ADDRSTRLEN] = {0,};
1003  struct ifaddrmsg *ifa = NLMSG_DATA(h);
1004  int len;
1005
1006  if ((len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))) < 0) {
1007    error_msg("wrong nlmsg len %d", len);
1008    return 0;
1009  }
1010
1011  for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta=RTA_NEXT(rta, len))
1012    if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta;
1013
1014  if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1015  if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
1016  if ((addrinfo.scope ^ ifa->ifa_scope)&addrinfo.scopemask) return 0;
1017  if (addrinfo.ifindex && addrinfo.ifindex != ifa->ifa_index) return 0;
1018
1019  if (flag_l && addrinfo.label && ifa->ifa_family == AF_INET6) return 0;
1020  if ((rta_tb[IFA_LABEL])) {
1021    xstrncpy(label, RTA_DATA(rta_tb[IFA_LABEL]), 256);
1022    label[255] = '\0';
1023    if (addrinfo.label && fnmatch(addrinfo.label, label, 0))
1024      return 0;
1025  }
1026
1027  if (TT.flush) {
1028    if (ifa->ifa_index == addrinfo.ifindex) {
1029      h->nlmsg_type = RTM_DELADDR;
1030      h->nlmsg_flags = NLM_F_REQUEST;
1031      send_nlmesg(RTM_DELADDR, 0, 0, h, h->nlmsg_len);
1032      return 0;
1033    }
1034  }
1035
1036  if (h->nlmsg_type == RTM_DELADDR) printf("Deleted ");
1037
1038  if (TT.singleline) {
1039    if (!if_indextoname(ifa->ifa_index, lbuf)) perror_exit(NULL);
1040    printf("%u: %s",ifa->ifa_index, lbuf);
1041  }
1042
1043  sprintf(scope, " scope %s ", namefromRPDB(ifa->ifa_scope, RPDB_rtscopes));
1044
1045  if (ifa->ifa_family == AF_INET) strcpy(family, "    inet ");
1046  else if (ifa->ifa_family == AF_INET6) strcpy(family, "    inet6 ");
1047  else sprintf(family, "    family %d", ifa->ifa_family);
1048
1049  if (rta_tb[IFA_LOCAL]) {
1050    if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL]),
1051          lbuf, sizeof(lbuf))) perror_exit("inet");
1052
1053    sprintf(family+strlen(family), lbuf, strlen(lbuf));
1054    if (!rta_tb[IFA_ADDRESS] || !memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
1055          RTA_DATA(rta_tb[IFA_LOCAL]), 4))
1056      sprintf(family+strlen(family), "/%d ", ifa->ifa_prefixlen);
1057    else {
1058      if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS]),
1059            lbuf_ifa, sizeof(lbuf_ifa))) perror_exit("inet");
1060      sprintf(peer, " peer %s/%d ", lbuf_ifa, ifa->ifa_prefixlen);
1061    }
1062  }
1063
1064  if (addrinfo.to && strcmp(addrinfo.addr, lbuf))
1065    return 0;
1066
1067  if (rta_tb[IFA_BROADCAST]) {
1068    if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_BROADCAST]),
1069          lbuf, sizeof(lbuf))) perror_exit("inet");
1070    sprintf(brd, " brd %s", lbuf);
1071  }else brd = "";
1072
1073  if (rta_tb[IFA_ANYCAST]) {
1074    if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ANYCAST]),
1075          lbuf, sizeof(lbuf))) perror_exit("inet");
1076    sprintf(any, " any %s", lbuf);
1077  }
1078
1079  if (ifa->ifa_family == AF_INET)
1080    printf("%s%s%s%s%s %c", family, brd, peer, scope, label,
1081        (TT.singleline? '\0' : '\n'));
1082  else printf("%s%s %c", family, scope, (TT.singleline? '\0' : '\n'));
1083  if (TT.singleline && (ifa->ifa_family == AF_INET)) xputc('\n');
1084
1085  if (rta_tb[IFA_CACHEINFO]) {
1086    struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
1087
1088    printf("%c      valid_lft ", (TT.singleline? '\\' : '\0'));
1089    if (ci->ifa_valid ==  0xFFFFFFFFU) printf("forever");
1090    else printf("%usec", ci->ifa_valid);
1091    printf(" preferred_lft ");
1092    if (ci->ifa_prefered ==  0xFFFFFFFFU) printf("forever");
1093    else printf("%dsec", ci->ifa_prefered);
1094    xputc('\n');
1095  }
1096  return 0;
1097}
1098
1099static int ipaddrupdate(char **argv)
1100{
1101  int length, cmd = !memcmp("add", argv[-1], strlen(argv[-1]))
1102    ? RTM_NEWADDR: RTM_DELADDR;
1103  int idx = 0,length_brd = 0, length_peer = 0,length_any = 0,length_local = 0,
1104      scoped = 0;
1105  char *dev = NULL,*label = NULL, reply[8192];
1106
1107  struct nlmsghdr *addr_ptr = NULL;
1108  struct nlmsgerr *err = NULL;
1109  struct arglist cmd_objectlist[] = {{"dev",0}, {"peer", 1},
1110    {"remote", 2}, {"broadcast", 3}, {"brd", 4}, {"label", 5},
1111    {"anycast", 6},{"scope", 7}, {"local", 8}, {NULL, -1}};
1112  struct {
1113    struct nlmsghdr nlm;
1114    struct ifaddrmsg ifadd;
1115    char buf[256];
1116  } req;
1117  typedef struct {
1118    int family, bytelen, bitlen;
1119    __u32  data[8];
1120  } option_data;
1121  option_data local;
1122
1123  memset(&req, 0, sizeof(req));
1124  req.nlm.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1125  req.nlm.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
1126  req.nlm.nlmsg_type = cmd;
1127  req.ifadd.ifa_family = TT.addressfamily;
1128
1129  while (*argv) {
1130    idx = substring_to_idx(*argv, cmd_objectlist);
1131    if (idx >= 0)
1132      if (!*++argv)
1133        error_exit("Incomplete Command line");
1134    switch(idx) {
1135      case 0:
1136        dev = *argv;
1137        break;
1138      case 1:
1139      case 2:
1140        {
1141          uint32_t addr[4] = {0,}, netmask = 0;
1142          uint8_t len = 0;
1143          parse_prefix(addr, &netmask, &len, *argv,
1144              req.ifadd.ifa_family);
1145          if (len)
1146            req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6);
1147          length_peer = len;
1148          add_string_to_rtattr(&req.nlm, sizeof(req),
1149              IFA_ADDRESS, addr, len);
1150          req.ifadd.ifa_prefixlen = netmask;
1151        }
1152        break;
1153      case 3:
1154      case 4:
1155        if (*argv[0] == '+') {
1156          length_brd = -1;
1157        } else if (*argv[0] == '-') {
1158          length_brd = -2;
1159        } else {
1160          uint32_t addr[4] = {0,};
1161          uint8_t af = AF_UNSPEC;
1162
1163          if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family))
1164            error_exit("Invalid prefix");
1165
1166          length_brd = ((af == AF_INET6) ? 16 : 4);
1167          if (req.ifadd.ifa_family == AF_UNSPEC)
1168            req.ifadd.ifa_family = af;
1169          add_string_to_rtattr(&req.nlm, sizeof(req),
1170              IFA_BROADCAST, &addr, length_brd);
1171        }
1172        break;
1173      case 5:
1174        label = *argv;
1175        add_string_to_rtattr(&req.nlm, sizeof(req),
1176            IFA_LABEL, label, strlen(label) + 1);
1177        break;
1178      case 6:
1179        {
1180          uint32_t addr[4] = {0,};
1181          uint8_t af = AF_UNSPEC;
1182
1183          if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family))
1184            error_exit("Invalid prefix");
1185          length_any = ((af == AF_INET6) ? 16 : 4);
1186          if (req.ifadd.ifa_family == AF_UNSPEC)
1187            req.ifadd.ifa_family = af;
1188          add_string_to_rtattr(&req.nlm, sizeof(req),
1189              IFA_ANYCAST, &addr, length_any);
1190        }
1191        break;
1192      case 7:
1193        {
1194          int scope = idxfromRPDB(*argv, RPDB_rtscopes);
1195          if (scope < 0) error_exit("wrong scope '%s'", *argv);
1196          req.ifadd.ifa_scope = scope;
1197          scoped = 1;
1198        }
1199        break;
1200      default:
1201        {
1202          //local is by default
1203          uint32_t addr[8] = {0,}, netmask = 0;
1204          uint8_t len = 0;
1205
1206          parse_prefix(addr, &netmask, &len, *argv,
1207              req.ifadd.ifa_family);
1208          if (len)
1209            req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6);
1210          length_local = len;
1211          local.bitlen = netmask;
1212          local.bytelen = len;
1213          memcpy(local.data, addr, sizeof(local.data));
1214          local.family = req.ifadd.ifa_family;
1215          add_string_to_rtattr(&req.nlm, sizeof(req),
1216              IFA_LOCAL, &local.data, local.bytelen);
1217        }
1218        break;
1219    }
1220    argv++;
1221  }
1222  if (!dev) error_exit("need \"dev \" argument");
1223  if (label && strncmp(dev, label, strlen(dev)) != 0)
1224    error_exit("\"dev\" (%s) must match \"label\" (%s)", dev, label);
1225
1226  if (length_peer == 0 && length_local && cmd != RTM_DELADDR){
1227    add_string_to_rtattr(&req.nlm, sizeof(req),
1228        IFA_ADDRESS, &local.data, local.bytelen);
1229  }
1230
1231  if (length_brd < 0 && cmd != RTM_DELADDR){
1232    int i;
1233
1234    if (req.ifadd.ifa_family != AF_INET)
1235      error_exit("broadcast can be set only for IPv4 addresses");
1236
1237    if (local.bitlen <= 30) {
1238      for (i = 31; i >= local.bitlen; i--) {
1239        if (length_brd == -1)
1240          local.data[0] |= htonl(1<<(31-i));
1241        else
1242          local.data[0] &= ~htonl(1<<(31-i));
1243      }
1244      add_string_to_rtattr(&req.nlm, sizeof(req),
1245          IFA_BROADCAST, &local.data, local.bytelen);
1246      length_brd = local.bytelen;
1247    }
1248  }
1249  if (req.ifadd.ifa_prefixlen == 0)
1250    req.ifadd.ifa_prefixlen = local.bitlen;
1251  if (!scoped && (cmd != RTM_DELADDR) && (local.family == AF_INET)
1252      && (local.bytelen >= 1 && *(uint8_t*)&local.data == 127))
1253    req.ifadd.ifa_scope = RT_SCOPE_HOST;
1254  req.ifadd.ifa_index = get_ifaceindex(dev, 1);
1255
1256  send_nlmesg(RTM_NEWADDR, 0, AF_UNSPEC, (void *)&req, req.nlm.nlmsg_len);
1257  length = recv(TT.sockfd, reply, sizeof(reply), 0);
1258  addr_ptr = (struct nlmsghdr *) reply;
1259  for (; NLMSG_OK(addr_ptr, length); addr_ptr = NLMSG_NEXT(addr_ptr, length)) {
1260    if (addr_ptr->nlmsg_type == NLMSG_DONE)
1261      return 1;
1262    if (addr_ptr->nlmsg_type == NLMSG_ERROR)
1263      err = (struct nlmsgerr*) NLMSG_DATA(addr_ptr);
1264    if (err && err->error) {
1265      errno = -err->error;
1266      perror_exit("RTNETLINK answers:");
1267    }
1268  }
1269  return 0;
1270}
1271
1272static int ipaddr_listflush(char **argv)
1273{
1274  int idx; uint32_t netmask = 0, found = 0;
1275  char *tmp = NULL, *name = NULL;
1276  struct double_list *dlist;
1277  struct arglist cmd_objectlist[] = {{"to", 0}, {"scope", 1}, {"up", 2},
1278    {"label", 3}, {"dev", 4}, {NULL, -1}};
1279
1280  TT.flush = *argv[-1] == 'f' ? 1 : 0;
1281  memset(&addrinfo, 0, sizeof(addrinfo));
1282
1283  if (TT.flush) {
1284    if (!*argv)
1285      error_exit("Incomplete command for \"flush\"");
1286    if (TT.addressfamily == AF_PACKET)
1287      error_exit("Can't flush link Addressess");
1288  }
1289  addrinfo.scope = -1;
1290  while (*argv) {
1291    switch (idx = substring_to_idx(*argv, cmd_objectlist)) {
1292      case 0: 
1293        {// ADDR_TO
1294          if (!*++argv) error_exit("Incomplete Command line");
1295          else if (!strcmp(*argv, "0")) return 0;
1296          uint32_t addr[4] = {0,};
1297          uint8_t len = 0;
1298
1299          addrinfo.to = 1;
1300          parse_prefix(addr, &netmask, &len, *argv, TT.addressfamily);
1301          if (len)
1302            TT.addressfamily = ((len == 4) ? AF_INET : AF_INET6);
1303          addrinfo.addr  = strtok(*argv, "/");
1304        }
1305        break;
1306      case 1: // ADDR_SCOPE
1307        {
1308          int scope = 0;
1309          if (!*++argv) error_exit("Incomplete Command line");
1310          name = *argv;
1311
1312          addrinfo.scopemask = -1;
1313          if (isdigit(**argv)) {
1314            int idx = atolx(*argv);
1315
1316            name = xstrdup(namefromRPDB(idx, RPDB_rtscopes));
1317          }
1318          if ((scope = idxfromRPDB(name, RPDB_rtscopes)) < 0) {
1319            if (strcmp(name, "all"))
1320              error_exit("wrong scope '%s'", name);
1321            scope = RT_SCOPE_NOWHERE;
1322            addrinfo.scopemask = 0;
1323          }
1324
1325          if (isdigit(**argv))
1326            free(name);
1327          addrinfo.scope = scope;
1328        }
1329        break;       
1330      case 2: // ADDR_UP
1331        addrinfo.up = 1;
1332        break;            
1333      case 3: // ADDR_LABEL
1334        if (!*++argv) error_exit("Incomplete Command line");
1335        addrinfo.label = *argv;
1336        break;
1337      case 4: // ADDR_DEV
1338        if (!*++argv) error_exit("Incomplete Command line");
1339
1340      default:                               
1341        if (TT.filter_dev)
1342          error_exit("Either \"dev\" is duplicate or %s is garbage",
1343              *argv);
1344        TT.filter_dev = *argv;
1345        break;
1346    }
1347    argv++;
1348  }
1349
1350  link_show(&tmp);
1351  while ( linfo && (dlist = dlist_pop(&linfo))){    
1352    struct linkdata *tmp  = (struct linkdata*) dlist;
1353    char *temp = &tmp->iface[0];
1354
1355    if (TT.filter_dev && strcmp(TT.filter_dev, temp))
1356      continue;
1357    found = 1;
1358    if (TT.flush && addrinfo.label) ipaddr_print( tmp, 0);
1359    if (addrinfo.up && !(tmp->flags & IFF_UP)){
1360      ipaddr_print(tmp, 0);
1361      continue;
1362    }
1363    if (addrinfo.label){
1364      if ( fnmatch(addrinfo.label, temp, 0)) {
1365        ipaddr_print(tmp, 1);
1366        continue;
1367      }      
1368    }
1369    if (!TT.addressfamily && ! TT.flush ) print_link_output(tmp);
1370
1371    ipaddr_print(tmp, 0);
1372    free(tmp);
1373  }
1374  if (TT.filter_dev && !found)
1375    error_exit("Device \"%s\" doesn't exist. \n", TT.filter_dev);
1376  return 0;
1377}
1378
1379static int ipaddr_print( struct linkdata *link, int flag_l)
1380{
1381  struct nlmsghdr *addr_ptr;
1382  int ip_match = 0;
1383
1384  addrinfo.ifindex = link->iface_idx;
1385  send_nlmesg(RTM_GETADDR, NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
1386      AF_UNSPEC, NULL, 0);
1387  if (TT.addressfamily == AF_PACKET) print_link_output(link);
1388
1389  if (addrinfo.label){
1390    char *col = strchr(addrinfo.label, ':');
1391    if (!col && (fnmatch(addrinfo.label, &link->iface[0], 0)))
1392      return 0;
1393  }
1394
1395  while (1){
1396    int len = recv(TT.sockfd, TT.gbuf, sizeof(TT.gbuf), 0);
1397    addr_ptr = (struct nlmsghdr *)TT.gbuf;
1398    struct ifaddrmsg *addressInfo = NLMSG_DATA(addr_ptr);
1399    char lbuf[INET6_ADDRSTRLEN];
1400    struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,};
1401
1402    int len1 = addr_ptr->nlmsg_len - NLMSG_LENGTH(sizeof(*addressInfo));
1403    if (len1 > 0) {
1404      for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) {
1405        addressInfo = NLMSG_DATA(addr_ptr);
1406        if (TT.addressfamily && TT.addressfamily != addressInfo->ifa_family)
1407          continue;
1408        if (addrinfo.ifindex && addrinfo.ifindex != addressInfo->ifa_index)
1409          continue;
1410
1411        if (addrinfo.to) {        
1412          memset(rta_tb, 0, sizeof(rta_tb));
1413          int rt_len = IFA_PAYLOAD(addr_ptr);
1414          for (rta = IFA_RTA(addressInfo); RTA_OK(rta, rt_len); rta=RTA_NEXT(rta, rt_len)) {
1415            if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta;
1416          }
1417          if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1418          if (rta_tb[IFA_LOCAL]) {
1419            if (!inet_ntop(TT.addressfamily, RTA_DATA(rta_tb[IFA_LOCAL]),
1420                  lbuf, sizeof(lbuf))) perror_exit("inet");
1421            if (strcmp(addrinfo.addr, lbuf))
1422              continue;
1423            ip_match=1;
1424          }
1425          if (!ip_match)
1426            continue;
1427        }
1428
1429        if (!TT.flush){
1430          if (addrinfo.scope != -1 && TT.addressfamily && TT.addressfamily ==
1431              addressInfo->ifa_family &&
1432              (addrinfo.ifindex == addressInfo->ifa_index)) {
1433            if ((addrinfo.scope ^ addressInfo->ifa_scope) & addrinfo.scopemask)
1434              continue;
1435            else if (addrinfo.up && (link->flags & IFF_UP))
1436              print_link_output(link);
1437            else if (!addrinfo.up) print_link_output(link);
1438          }
1439          if (TT.addressfamily &&
1440              (addrinfo.ifindex == addressInfo->ifa_index) &&
1441              (addrinfo.scope == -1)){
1442            if (addrinfo.up && (link->flags & IFF_UP))
1443              print_link_output(link);
1444            else if (!addrinfo.up) print_link_output(link);
1445          }
1446        }
1447
1448        for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) {
1449          if ((addr_ptr->nlmsg_type == RTM_NEWADDR))
1450            print_addrinfo(addr_ptr, flag_l);
1451          if ((addr_ptr->nlmsg_type == NLMSG_DONE) ||
1452              (addr_ptr->nlmsg_type == NLMSG_ERROR) ||
1453              (TT.flush && addrinfo.to))
1454            goto ret_stop;          
1455        }
1456        if ((addr_ptr->nlmsg_type == NLMSG_DONE) ||
1457            (addr_ptr->nlmsg_type == NLMSG_ERROR))
1458          break;
1459      }
1460    }
1461    else
1462      return 0;
1463  }
1464
1465ret_stop:
1466  return 0;
1467}
1468
1469static int ipaddr(char **argv)
1470{
1471  int    idx;
1472  cmdobj ipcmd, cmdobjlist[] = {ipaddrupdate, ipaddr_listflush};
1473  struct arglist cmd_objectlist[] = { {"add", 0}, {"delete", 0},
1474    {"list", 1},{"show", 1},{"lst", 1}, {"flush", 1}, {NULL,-1}};
1475
1476  TT.is_addr++;
1477  if (!*argv) idx = 1;
1478  else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1)
1479    help_exit(0);
1480
1481  ipcmd = cmdobjlist[idx];
1482  return ipcmd(argv);
1483}
1484
1485// ===========================================================================
1486// code for ip route
1487// ===========================================================================
1488struct I_data {
1489  unsigned char family;
1490  uint32_t addr[8] , netmask ;
1491  uint8_t len ;
1492};
1493
1494struct {
1495  int tb,idev,odev,proto;
1496  struct I_data rvia, rdst, mdst, rsrc, msrc;
1497} gfilter;
1498
1499static void show_iproute_help(void)
1500{
1501  char *errmsg = "\n\n" \
1502       "iproute { list | flush } SELECTOR\n" \
1503       "iproute get ADDRESS [from ADDRESS iif STRING]\n" \
1504       "        [oif STRING]\n" \
1505       "iproute { add | del | change | append | replace | test } ROUTE\n" \
1506       "        SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n" \
1507       "        ROUTE := [TYPE] PREFIX [proto RTPROTO] [metric METRIC]";
1508
1509  error_exit(errmsg);
1510}
1511
1512static void print_rta_metrics(char* out, const struct rtattr *mxattr)
1513{
1514  int32_t tvar = RTA_PAYLOAD(mxattr);
1515  struct rtattr *rta, *mxrta[RTAX_MAX+1] = {0,};
1516  unsigned int mxlock = 0;
1517  int i;
1518
1519  for (rta = RTA_DATA(mxattr); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1520    if (rta->rta_type <= RTA_MAX) mxrta[rta->rta_type] = rta;
1521
1522  if (mxrta[RTAX_LOCK])
1523    mxlock = *(u_int32_t *)RTA_DATA(mxrta[RTAX_LOCK]);
1524
1525  for (i = 2; i <= RTAX_MAX; i++) {
1526    uint32_t val = 0;
1527
1528    if (mxrta[i] == NULL && !(mxlock & (1 << i)))
1529      continue;
1530
1531    if (mxrta[i] != NULL && i != RTAX_CC_ALGO)
1532      val = *(u_int32_t *)RTA_DATA(mxrta[i]);
1533
1534    if (i == RTAX_HOPLIMIT && (int)val == -1)
1535      continue;
1536
1537    if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
1538      sprintf(out, "%s%s ", out, mx_names[i]);
1539    else
1540      sprintf(out, "%smetric %d ", out, i);
1541
1542    if (mxlock & (1<<i))
1543      sprintf(out, "%slock ", out);
1544
1545    switch (i) {
1546      case RTAX_RTT:
1547      case RTAX_RTTVAR:
1548      case RTAX_RTO_MIN:
1549        if (i == RTAX_RTT)
1550          val /= 8;
1551        else if (i == RTAX_RTTVAR)
1552          val /= 4;
1553
1554        if (val >= 1000)
1555          sprintf(out, "%s%gs ", out, val / 1e3);
1556        else
1557          sprintf(out, "%s%ums ", out, val);
1558        break;
1559
1560      case RTAX_CC_ALGO:
1561        sprintf(out, "%scongestion %s ", out, (const char*)RTA_DATA(mxrta[i]));
1562        break;
1563
1564      default:
1565        sprintf(out, "%s%u ", out, val);
1566        break;
1567    }
1568  }
1569}
1570
1571static int display_route_info(struct nlmsghdr *mhdr, char **argv)
1572{
1573  char *inetval = NULL, out[1024] = {0};
1574  struct rtmsg *msg = NLMSG_DATA(mhdr);
1575  struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1576  int32_t tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1577  int hlen = ((msg->rtm_family == AF_INET) ? 32
1578      : ((msg->rtm_family == AF_INET6) ? 128 : -1));
1579
1580  if (mhdr->nlmsg_type != RTM_NEWROUTE) return 0;
1581  if (msglen < 0) return 1;
1582
1583  if (msg->rtm_family == AF_INET6) {
1584    if (gfilter.tb) {
1585      if (gfilter.tb < 0) {
1586        if (!(msg->rtm_flags & RTM_F_CLONED)) return 0;
1587      } else {
1588        if (msg->rtm_flags & RTM_F_CLONED) return 0;
1589        if (gfilter.tb == RT_TABLE_LOCAL && msg->rtm_type != RTN_LOCAL)
1590          return 0;
1591        else if (gfilter.tb == RT_TABLE_MAIN && msg->rtm_type == RTN_LOCAL)
1592          return 0;
1593      }
1594    }
1595  }
1596  else if (gfilter.tb > 0 && gfilter.tb != msg->rtm_table) return 0;
1597
1598  if (gfilter.proto && (msg->rtm_protocol != gfilter.proto)) return 0;
1599
1600
1601  if (gfilter.rdst.family && (msg->rtm_family != gfilter.rdst.family ||
1602        gfilter.rdst.netmask > msg->rtm_dst_len)) return 0;
1603  if (gfilter.mdst.family && (msg->rtm_family != gfilter.mdst.family
1604        || (gfilter.mdst.netmask < msg->rtm_dst_len))) return 0;
1605  if (gfilter.rsrc.family && (msg->rtm_family != gfilter.rsrc.family
1606        || gfilter.rsrc.netmask > msg->rtm_src_len)) return 0;
1607  if (gfilter.msrc.family && (msg->rtm_family != gfilter.msrc.family
1608        || (gfilter.msrc.netmask < msg->rtm_src_len))) return 0;
1609  tvar = msglen;
1610
1611  for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1612    if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1613
1614  if (msg->rtm_type != RTN_UNICAST)
1615    sprintf(out,"%s%s ", out,rtmtype_idx2str(msg->rtm_type));
1616  if (attr[RTA_DST]) {
1617    inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_DST]),
1618        toybuf, sizeof(toybuf));
1619    if (gfilter.rdst.family &&
1620        memcmp(RTA_DATA(attr[RTA_DST]), &gfilter.rdst.addr, gfilter.rdst.len))
1621      return 0;
1622    if (gfilter.mdst.family &&
1623        memcmp(RTA_DATA(attr[RTA_DST]), &gfilter.mdst.addr, gfilter.mdst.len))
1624      return 0;
1625    sprintf(out,"%s%s",out,inetval);
1626  }
1627  if (msg->rtm_dst_len) sprintf(out,"%s/%d ", out,msg->rtm_dst_len);
1628  else sprintf(out,"%s%s",out,"default ");
1629
1630  if (attr[RTA_SRC]) {
1631    inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_SRC]),
1632        toybuf, sizeof(toybuf));
1633    if (gfilter.rsrc.family &&
1634        memcmp(RTA_DATA(attr[RTA_SRC]), &gfilter.rsrc.addr, gfilter.rsrc.len))
1635      return 0;
1636    if (gfilter.msrc.family &&
1637        memcmp(RTA_DATA(attr[RTA_SRC]), &gfilter.msrc.addr, gfilter.msrc.len))
1638      return 0;
1639    sprintf(out, "%s from %s", out, inetval);
1640  }
1641  if (msg->rtm_src_len) sprintf(out, "%s/%d ", out, msg->rtm_src_len);
1642
1643  if (attr[RTA_GATEWAY]) {
1644    inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_GATEWAY]),
1645        toybuf, sizeof(toybuf));
1646    sprintf(out, "%s via %s ", out, inetval);
1647  }
1648  if (gfilter.rvia.family) {
1649    char tmp[256];
1650
1651    if (!attr[RTA_GATEWAY]) return 0;
1652    if (strcmp((char *)inet_ntop(msg->rtm_family, gfilter.rvia.addr,
1653            tmp, sizeof(tmp)), inetval)) return 0;
1654  }
1655
1656  if (gfilter.odev != 0) if (!attr[RTA_OIF]) return 0;
1657  if (attr[RTA_OIF]) {
1658    if (gfilter.odev !=0 && gfilter.odev != *(int*)RTA_DATA(attr[RTA_OIF]))
1659      return 0;
1660    sprintf(out, "%s dev %s ", out, 
1661        if_indextoname(*(int*)RTA_DATA(attr[RTA_OIF]), toybuf));
1662  }
1663
1664  if (attr[RTA_PREFSRC] && hlen) {
1665    inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_PREFSRC]),
1666        toybuf, sizeof(toybuf));
1667    sprintf(out, "%s src %s ", out, inetval);
1668  }
1669  if (attr[RTA_PRIORITY])
1670    sprintf(out, "%s metric %d ", out, *(uint32_t*)RTA_DATA(attr[RTA_PRIORITY]));
1671  if (msg->rtm_family == AF_INET6) {
1672    struct rta_cacheinfo *ci = NULL;
1673    if (attr[RTA_CACHEINFO]) ci = RTA_DATA(attr[RTA_CACHEINFO]);
1674    if ((msg->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
1675      if (msg->rtm_flags & RTM_F_CLONED) sprintf(out, "%s%s    cache ",
1676          out, (!TT.singleline ? "\n" : " "));
1677      if (ci && ci->rta_expires) {
1678        int hz = 0;
1679        FILE *fp = xfopen("/proc/net/psched","r");
1680
1681        if (fp) {
1682          unsigned int nom, denom;
1683
1684          if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
1685            if (nom == 1000000)
1686              hz = denom;
1687          fclose(fp);
1688        }
1689        if (!hz) hz = sysconf(_SC_CLK_TCK);
1690        sprintf(out, "%s expires %dsec", out, ci->rta_expires /hz);
1691      }
1692      if (ci && ci->rta_error) sprintf(out, "%s error %d", out, ci->rta_error);
1693    }
1694    else if (ci && ci->rta_error)
1695      sprintf(out, "%s error %d", out, ci->rta_error);
1696  }
1697  if (attr[RTA_IIF] && !gfilter.idev)
1698    sprintf(out, "%s iif %s", out, 
1699        if_indextoname(*(int*)RTA_DATA(attr[RTA_IIF]), toybuf));
1700
1701  if (attr[RTA_METRICS])
1702    print_rta_metrics(out, attr[RTA_METRICS]);
1703
1704  if (TT.flush || (TT.connected && !TT.from_ok)) 
1705    memcpy(toybuf, (void*)mhdr,mhdr->nlmsg_len);
1706
1707  if (TT.flush) {
1708    int sockfd = 0;
1709    struct nlmsghdr* mhdr = (struct nlmsghdr*)toybuf;
1710    struct rtmsg *msg = NLMSG_DATA(mhdr);
1711    int tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1712    struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1713
1714    tvar = msglen;
1715    for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1716      if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1717
1718    if (msg->rtm_family == AF_INET6
1719        && !msg->rtm_dst_len
1720        && msg->rtm_type == RTN_UNREACHABLE
1721        && attr[RTA_PRIORITY]
1722        && *(int*)RTA_DATA(attr[RTA_PRIORITY]) == -1)
1723      return 0;
1724
1725    mhdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1726    mhdr->nlmsg_type  = RTM_DELROUTE;
1727    mhdr->nlmsg_pid = 0;
1728    sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1729    if (send(sockfd , (void*)mhdr, mhdr->nlmsg_len, 0) < 0)
1730      perror_exit("Unable to send data on socket.");
1731
1732    while (1) {
1733      struct nlmsghdr *mhdr;
1734      int msglen = recv(sockfd, toybuf, sizeof(toybuf), 0);
1735
1736      if ((msglen < 0) && (errno == EINTR || errno == EAGAIN)) continue;
1737      else if (msglen < 0) {
1738        error_msg("netlink receive error %s", strerror(errno));
1739        xclose(sockfd);
1740        return 1;
1741      } else if (!msglen) {
1742        error_msg("EOF on netlink");
1743        xclose(sockfd);
1744        return 1;
1745      }
1746
1747      for (mhdr = (struct nlmsghdr*)toybuf; NLMSG_OK(mhdr, msglen);
1748          mhdr = NLMSG_NEXT(mhdr, msglen)) {
1749        switch (mhdr->nlmsg_type) {
1750          case NLMSG_DONE:
1751            xclose(sockfd);
1752            return 0;
1753          case NLMSG_ERROR:
1754            {
1755              struct nlmsgerr *merr = (struct nlmsgerr*)NLMSG_DATA(mhdr);
1756
1757              if (merr->error == 0)  { xclose(sockfd); return 0; }
1758              if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
1759                error_msg("ERROR truncated");
1760              else {
1761                errno = -merr->error;
1762                perror_msg("RTNETLINK answers");
1763              }
1764              xclose(sockfd);
1765              return 1;
1766            }
1767          default:
1768            break;
1769        }
1770      } // End of for loop.
1771    } // End of while loop.
1772
1773    xclose(sockfd);
1774  } else printf("%s\n",out);
1775  return 0;
1776}
1777
1778static int route_get(char **argv)
1779{
1780  int idx, flag;
1781  struct arglist cmd_objectlist[] = {{"from", 0}, {"iif", 1}, {"oif", 2}, 
1782    {"dev", 3}, {"notify", 4}, {"connected", 5}, {"to", 6}, {NULL, -1}};
1783  char *idev = NULL, *odev = NULL;
1784  struct {
1785    struct nlmsghdr mhdr;
1786    struct rtmsg msg;
1787    char buf[1024];
1788  } request;
1789
1790  memset(&request, 0, sizeof(request));
1791  request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1792  request.mhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1793  request.mhdr.nlmsg_type = RTM_GETROUTE;
1794  request.msg.rtm_family = AF_UNSPEC;
1795
1796  for (; *argv; argv++) {
1797    switch(idx = substring_to_idx(*argv, cmd_objectlist)) {
1798      case 0: TT.from_ok = 1; // dst address
1799      case 6: argv++; //fallthrough
1800      default: 
1801              {
1802                uint32_t addr[8] = {0,}, netmask = 0;
1803                uint8_t len = 0;
1804
1805                if (!*argv) error_exit("'%s': Missing Prefix", argv[-1]);
1806                parse_prefix(addr, &netmask, &len, *argv, request.msg.rtm_family);
1807                if (len) request.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
1808                netmask = (request.msg.rtm_family == AF_INET6) ? 128 : 32;
1809                if (!idx) request.msg.rtm_src_len = netmask;
1810                else request.msg.rtm_dst_len = netmask;
1811                add_string_to_rtattr(&request.mhdr, sizeof(request),
1812                    (!idx ? RTA_SRC : RTA_DST), addr, len);
1813                break;
1814              }
1815      case 1:
1816      case 2:
1817      case 3:
1818              if (!*++argv) show_iproute_help();
1819              if (idx == 1) idev = *argv, flag = RTA_IIF;
1820              else odev = *argv, flag = RTA_OIF;
1821              idx = get_ifaceindex(*argv, 1);
1822              add_string_to_rtattr(&request.mhdr, sizeof(request),
1823                  flag, (char*)&idx, sizeof(idx));
1824              break;
1825      case 4:
1826              request.msg.rtm_flags |= RTM_F_NOTIFY;
1827              break;
1828      case 5:
1829              TT.connected = 1;
1830              break;
1831    }
1832  }
1833  if (!request.msg.rtm_dst_len) 
1834    error_exit("need at least destination address");
1835
1836  send_nlmesg(0, 0, 0, &request, sizeof(request));
1837  filter_nlmesg(display_route_info, NULL);
1838
1839  if (TT.connected && !TT.from_ok) {
1840    struct nlmsghdr *mhdr = (struct nlmsghdr*)toybuf;
1841    struct rtmsg *msg = NLMSG_DATA(mhdr);
1842    int tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1843    struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1844
1845    if (mhdr->nlmsg_type != RTM_NEWROUTE) error_exit("not a route?");
1846    if (msglen < 0) error_exit("wrong len %d", msglen);
1847
1848    tvar = msglen;
1849    for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1850      if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1851
1852    if (attr[RTA_PREFSRC]) {
1853      attr[RTA_PREFSRC]->rta_type = RTA_SRC;
1854      msg->rtm_src_len = 8*RTA_PAYLOAD(attr[RTA_PREFSRC]);
1855    } else if (!attr[RTA_SRC]) error_exit("can't connect the route");
1856
1857    if (!odev && attr[RTA_OIF]) attr[RTA_OIF]->rta_type = 0;
1858    if (attr[RTA_GATEWAY]) attr[RTA_GATEWAY]->rta_type = 0;
1859    if (!idev && attr[RTA_IIF]) attr[RTA_IIF]->rta_type = 0;
1860    mhdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1861    mhdr->nlmsg_type  = RTM_GETROUTE;
1862    mhdr->nlmsg_pid = 0;
1863    xclose(TT.sockfd);
1864    TT.sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1865    send_nlmesg(0, 0, 0, mhdr, mhdr->nlmsg_len);
1866    filter_nlmesg(display_route_info, NULL);
1867  }
1868  return 0;
1869}
1870
1871static int route_show_flush(char **argv)
1872{
1873  struct arglist cmd_objectlist[] = {{"protocol", 0}, {"dev", 1}, {"oif", 2},
1874    {"iif", 3}, {"via", 4}, {"table", 5}, {"cache", 6}, {"from", 7}, 
1875    {"to", 8}, {"all", 9}, {"root", 10}, {"match", 11}, {"exact", 12}, 
1876    {"main", 13}, {NULL,-1}};
1877  int family = TT.addressfamily, idx;
1878  struct {
1879    struct nlmsghdr mhdr;
1880    struct rtmsg msg;
1881  } request;
1882
1883  if (*argv[-1] == 'f') TT.flush = 1;
1884  if (TT.flush && !*argv) show_iproute_help();
1885
1886  gfilter.tb = RT_TABLE_MAIN;
1887  for (; *argv; argv++) {
1888    switch (idx = substring_to_idx(*argv, cmd_objectlist)) {
1889      case 0:
1890        if (!*++argv) show_iproute_help();
1891        if ((idx = idxfromRPDB(*argv,RPDB_rtprotos)) < 0)
1892          error_exit("Invalid argument protocol.");
1893        gfilter.proto = idx;
1894        break;
1895      case 1:
1896      case 2:
1897      case 3:
1898        {
1899          if (!*++argv) show_iproute_help();
1900          int dev = get_ifaceindex(*argv, 1);
1901
1902          if (idx == 3) gfilter.idev = dev;
1903          else gfilter.odev = dev;        
1904        }
1905        break;
1906      case 4:
1907        if (!*++argv) show_iproute_help();
1908        parse_prefix(gfilter.rvia.addr, &gfilter.rvia.netmask,
1909            &gfilter.rvia.len, *argv, gfilter.rvia.family);
1910        if (gfilter.rvia.len)
1911          gfilter.rvia.family = ((gfilter.rvia.len == 4) ?
1912              AF_INET : AF_INET6);
1913        break;
1914      case 5:
1915        if (!*++argv) show_iproute_help();
1916        idx = substring_to_idx(*argv, cmd_objectlist);
1917        if (idx == 6) gfilter.tb = -1;
1918        else if (idx == 9) gfilter.tb = 0;
1919        else if (idx != 13) {
1920          if ((gfilter.tb = idxfromRPDB(*argv, RPDB_rttables)) < 0)
1921            error_exit("table %s is invalid.", *argv);
1922        }
1923        break;
1924      case 6:
1925        gfilter.tb = -1;
1926        break;
1927      case 7:
1928        if (!*++argv) show_iproute_help();
1929        idx = substring_to_idx(*argv, cmd_objectlist);
1930        if (idx < 0)  if (!*++argv) show_iproute_help();
1931        if (idx == 10)
1932           if (!*++argv) show_iproute_help();
1933          parse_prefix(gfilter.rsrc.addr, &gfilter.rsrc.netmask,
1934              &gfilter.rsrc.len, *argv, gfilter.rsrc.family);
1935        if (gfilter.rsrc.len)
1936          gfilter.rsrc.family = ((gfilter.rsrc.len == 4) ?
1937              AF_INET : AF_INET6);
1938        else {
1939          if ((idx == 12 ||idx == 11) && !*++argv) show_iproute_help();
1940          parse_prefix(gfilter.msrc.addr, &gfilter.msrc.netmask,
1941              &gfilter.msrc.len, *argv, gfilter.msrc.family);
1942          if (gfilter.msrc.len)
1943            gfilter.msrc.family = ((gfilter.msrc.len == 4) ?
1944                AF_INET : AF_INET6);
1945          if (idx != 11) gfilter.rsrc = gfilter.msrc;
1946        }
1947        break;
1948      case 8:
1949        idx = substring_to_idx(*argv, cmd_objectlist);
1950        if (idx != -1 && !*++argv) show_iproute_help();
1951      default: // fallthrough
1952        if (idx == 10) {
1953          if (!*++argv) show_iproute_help();
1954          parse_prefix(gfilter.rdst.addr, &gfilter.rdst.netmask,
1955              &gfilter.rdst.len, *argv, gfilter.rdst.family);
1956        if (gfilter.rdst.len)
1957          gfilter.rdst.family = ((gfilter.rdst.len == 4) ?
1958              AF_INET : AF_INET6);
1959        }
1960        else {
1961          if ((idx == 12 ||idx == 11) && !*++argv) show_iproute_help();
1962          parse_prefix(gfilter.mdst.addr, &gfilter.mdst.netmask,
1963              &gfilter.mdst.len, *argv, gfilter.mdst.family);
1964          if (gfilter.mdst.len)
1965            gfilter.mdst.family = ((gfilter.mdst.len == 4) ?
1966                AF_INET : AF_INET6);
1967          if (idx != 11) gfilter.rdst = gfilter.mdst;
1968        }
1969        break;
1970    }
1971  }
1972  if (family == AF_UNSPEC && gfilter.tb) family = AF_INET;
1973
1974  if (TT.flush) {
1975    if (gfilter.tb < 0) { // flush table cache
1976      if (family != AF_INET6) {
1977        FILE *fp = xfopen("/proc/sys/net/ipv4/route/flush", "w");
1978
1979        if (fwrite("-1",1,2,fp) < 2) error_exit("can't flush routing cache");
1980        fclose(fp);
1981      }
1982      if (family == AF_INET) return 0;
1983    }
1984  }
1985
1986  memset(&request, 0, sizeof (request));
1987  request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof (struct rtmsg));
1988  request.mhdr.nlmsg_flags = NLM_F_REQUEST;
1989  request.mhdr.nlmsg_flags |= NLM_F_ROOT | NLM_F_MATCH;
1990  request.mhdr.nlmsg_type = RTM_GETROUTE;
1991  request.msg.rtm_family = family;
1992  if (gfilter.tb < 0) request.msg.rtm_flags = RTM_F_CLONED;
1993  send_nlmesg(0, 0, 0, (void*)&request, sizeof (request));
1994  return (filter_nlmesg(display_route_info, NULL));
1995}
1996
1997static int route_update(char **argv, unsigned int route_flags)
1998{
1999  char mxbuf[256], *d = NULL;
2000  struct rtattr *mxrta = (void*)mxbuf;
2001  unsigned mxlock = 0, ok = 0;
2002  int idx;
2003  uint32_t addr[8] = {0,}, netmask = 0;
2004  uint8_t len = 0;
2005
2006  struct arglist cmd_objectlist[] = {{"src", 0}, {"via", 1}, {"mtu", 2},
2007    {"lock", 3}, {"protocol", 4}, {"table", 5}, {"dev", 6}, {"oif", 7},
2008    {"to", 8}, {"metric", 9}, {NULL,-1}
2009  };
2010  enum {
2011    gtwy_ok = 1,
2012    dst_ok = 2,
2013    proto_ok = 4,
2014    type_ok = 8
2015  };
2016  struct {
2017    struct nlmsghdr hdr;
2018    struct rtmsg msg;
2019    char buf[1024];
2020  } req;
2021
2022  memset(&req, 0, sizeof(req));
2023  req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2024  req.hdr.nlmsg_flags = NLM_F_ACK| NLM_F_REQUEST | route_flags;
2025  req.hdr.nlmsg_type = TT.route_cmd;
2026  req.msg.rtm_family = AF_UNSPEC;
2027  req.msg.rtm_table = RT_TABLE_MAIN;
2028  req.msg.rtm_scope = RT_SCOPE_NOWHERE;
2029
2030  if (TT.route_cmd != RTM_DELROUTE) {
2031    req.msg.rtm_protocol = RTPROT_BOOT;
2032    req.msg.rtm_scope = RT_SCOPE_UNIVERSE;
2033    req.msg.rtm_type = RTN_UNICAST;
2034  }
2035
2036  mxrta->rta_type = RTA_METRICS;
2037  mxrta->rta_len = RTA_LENGTH(0);
2038
2039  for (; *argv; argv++) {
2040    idx = substring_to_idx(*argv, cmd_objectlist);
2041    if (!idx) {
2042      if (!*++argv) show_iproute_help();
2043      parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2044      if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2045      add_string_to_rtattr(&req.hdr, sizeof(req), RTA_PREFSRC, addr, len);
2046    } else if (idx == 1) {
2047      ok |= gtwy_ok;
2048      if (!*++argv) show_iproute_help();
2049      parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2050      if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2051      add_string_to_rtattr(&req.hdr, sizeof(req),RTA_GATEWAY, addr, len);
2052    } else if (idx == 2) {
2053      if (!*++argv) show_iproute_help();
2054      if (substring_to_idx(*argv, cmd_objectlist ) == 3) {
2055        mxlock |= (1 << RTAX_MTU);
2056        if (!*++argv) show_iproute_help();
2057      }
2058      idx = atolx(*argv);
2059      add_uint32_rtattr_to_buffer(mxrta, sizeof(mxbuf), RTAX_MTU, idx);
2060    } else if (idx == 4) {
2061      if (!*++argv) show_iproute_help();
2062      if ((idx = idxfromRPDB(*argv,RPDB_rtprotos)) < 0)
2063      error_exit("Invalid argument protocol %s.",*argv);
2064      req.msg.rtm_protocol = idx;
2065      ok |= proto_ok;
2066    } else if (idx == 5) {
2067      if (!*++argv) show_iproute_help();
2068      req.msg.rtm_table = idxfromRPDB(*argv, RPDB_rttables);
2069    } else if (idx == 6 || idx == 7) {
2070      if (!*++argv) show_iproute_help();
2071      d = *argv;
2072    } else if (idx == 9) {
2073      unsigned long metric;
2074      unsigned int res;
2075      char* ptr;
2076      if (!*++argv) show_iproute_help();
2077      metric = strtoul(*argv, &ptr, 0);
2078      if (!(!*ptr && metric <= 0xFFFFFFFFUL)) 
2079        error_exit("Invalid argument metric %s.",*argv);
2080      else
2081        res = metric;
2082      add_string_to_rtattr(&req.hdr, sizeof(req),
2083          RTA_PRIORITY, (char*)&res, sizeof(res));
2084    } else {
2085      if (idx == 8)
2086        if (!*++argv) show_iproute_help();
2087      idx = substring_to_idx(*argv,rtmtypes);
2088      if (idx != -1) {
2089        if (!*++argv) show_iproute_help();
2090        req.msg.rtm_type = idx;
2091        ok |= type_ok;
2092      }
2093      if (ok & dst_ok) error_exit("Duplicate argument 'to'");
2094      parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2095      if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2096      req.msg.rtm_dst_len = netmask;
2097      ok |= dst_ok;
2098      if (len) add_string_to_rtattr(&req.hdr, sizeof(req),RTA_DST, addr, len);
2099    }
2100  }
2101
2102  if (d) {
2103    idx = get_ifaceindex(d,1);
2104    add_string_to_rtattr(&req.hdr, sizeof(req),
2105        RTA_OIF, (char*)&idx, sizeof(idx));
2106  }
2107  if (mxrta->rta_len > RTA_LENGTH(0)) {
2108    if (mxlock)
2109      add_uint32_rtattr_to_buffer(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
2110    add_string_to_rtattr(&req.hdr, sizeof(req),
2111        RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
2112  }
2113
2114  if (req.msg.rtm_type == RTN_LOCAL || req.msg.rtm_type == RTN_NAT)
2115    req.msg.rtm_scope = RT_SCOPE_HOST;
2116  else if (req.msg.rtm_type == RTN_BROADCAST||req.msg.rtm_type == RTN_MULTICAST
2117      || req.msg.rtm_type == RTN_ANYCAST)
2118    req.msg.rtm_scope = RT_SCOPE_LINK;
2119  else if (req.msg.rtm_type == RTN_UNICAST || req.msg.rtm_type == RTN_UNSPEC) {
2120    if (TT.route_cmd == RTM_DELROUTE)
2121      req.msg.rtm_scope = RT_SCOPE_NOWHERE;
2122    else if (!(ok & gtwy_ok))
2123      req.msg.rtm_scope = RT_SCOPE_LINK;
2124  }
2125  if (req.msg.rtm_family == AF_UNSPEC) req.msg.rtm_family = AF_INET;
2126  send_nlmesg(0, 0, 0, &req, sizeof(req));
2127  filter_nlmesg(NULL, NULL);
2128  return 0;
2129}
2130
2131static int iproute(char **argv)
2132{
2133  int idx = 1;
2134  struct arglist cmd_objectlist1[] = {{"add", 0}, {"append", 1},{"change", 2},
2135    {"chg", 3},{"delete",4}, {"get", 5}, {"list", 6}, {"show", 7},
2136    {"prepend", 8},{"replace", 9},{"test", 10}, {"flush", 11},{NULL,-1}};
2137
2138  TT.route_cmd = RTM_NEWROUTE;
2139  switch (idx = substring_to_idx(*argv , cmd_objectlist1)) {
2140    case 0: // add
2141      return route_update(++argv , NLM_F_CREATE|NLM_F_EXCL);
2142    case 1: // append
2143      return route_update(++argv , NLM_F_CREATE|NLM_F_APPEND);
2144    case 2: // change
2145    case 3: // chg
2146      return route_update(++argv , NLM_F_REPLACE);
2147    case 4: // delete
2148      TT.route_cmd = RTM_DELROUTE;
2149      return route_update(++argv , RTM_DELROUTE);
2150    case 5:
2151      return route_get(++argv);
2152    case 6:
2153    case 7:
2154      return route_show_flush(++argv);
2155    case 8: // prepend
2156      return route_update(++argv , NLM_F_CREATE);
2157    case 9: // replace
2158      return route_update(++argv ,  NLM_F_CREATE|NLM_F_REPLACE);
2159    case 10: // test
2160      return route_update(++argv , NLM_F_EXCL);
2161    case 11: // flush
2162      return route_show_flush(++argv);
2163    default:
2164      if (!*argv) return route_show_flush(argv);
2165      else show_iproute_help();
2166  }
2167  return 0; // non reachable code.
2168}
2169
2170
2171// ===========================================================================
2172// code for ip rule.
2173// ===========================================================================
2174static void show_iprule_help(void)
2175{
2176  char *errmsg = "usage: ip rule [ list | add | del ] SELECTOR ACTION\n"
2177    "SELECTOR := [ from PREFIX ] [ to PREFIX ] [pref NUMBER] [ tos TOS ]\n"
2178    "            [ fwmark FWMARK] [ dev/iif STRING ] [type TYPE]\n"
2179    "ACTION := [ table TABLE_ID ] [ realms [SRCREALM/]DSTREALM ]";
2180
2181  error_exit(errmsg);
2182}
2183
2184static int ruleupdate(char **argv)
2185{
2186  int8_t idx, tflag = 0, opt = (*argv[-1] == 'a') ? RTM_NEWRULE : RTM_DELRULE;
2187  struct arglist options[] = {{"from", 0}, {"to", 1}, {"preference", 2},
2188    {"order", 2}, {"priority", 2}, {"tos", 3}, {"dsfield", 3}, {"fwmark", 4},
2189    {"realms", 5}, {"table", 6}, {"lookup", 6}, {"dev", 7}, {"iif", 7},
2190    {"nat", 8}, {"map-to", 8}, {"type", 9}, {"help", 10}, {NULL, -1}};
2191  struct {
2192    struct nlmsghdr mhdr;
2193    struct rtmsg    msg;
2194    char buf[1024];
2195  } request;
2196
2197  memset(&request, 0, sizeof(request));
2198  request.mhdr.nlmsg_type = opt;
2199  request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2200  request.mhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK |
2201    ((opt == RTM_DELRULE) ? 0 : NLM_F_CREATE | NLM_F_EXCL);
2202  request.msg.rtm_family = TT.addressfamily;
2203  request.msg.rtm_protocol = RTPROT_BOOT;
2204  request.msg.rtm_scope = RT_SCOPE_UNIVERSE;
2205  request.msg.rtm_table = 0;
2206  request.msg.rtm_type = ((opt == RTM_DELRULE) ? RTN_UNSPEC : RTN_UNICAST);
2207
2208  for (; *argv; argv++) {
2209    switch ((idx = substring_to_idx(*argv, options))) {
2210      case 0:
2211      case 1: 
2212        { // e.g. from IP/Netmask and to IP/Netmask.
2213          uint32_t addr[4] = {0,}, netmask = 0;
2214          uint8_t len = 0, *tmp;
2215
2216          if (!*++argv) error_exit("'%s': Missing Prefix", argv[-1]);
2217          parse_prefix(addr, &netmask, &len, *argv, request.msg.rtm_family);
2218
2219          tmp = idx ? &request.msg.rtm_dst_len : &request.msg.rtm_src_len;
2220          if (!netmask) *tmp = 0;
2221          else *tmp = netmask;
2222
2223          add_string_to_rtattr(&request.mhdr, sizeof(request),
2224              (idx ? RTA_DST : RTA_SRC), addr, len);
2225        }
2226        break;
2227      case 2:
2228      case 4: 
2229        { // e.g. Preference p# and fwmark MARK
2230          uint32_t pref;
2231          char *ptr;
2232
2233          if (!*++argv)
2234            error_exit("Missing %s", (idx == 2) ? "Preference" : "fwmark");
2235          pref = strtoul(*argv, &ptr, 0);
2236          if (!ptr || (ptr == *argv) || *ptr  || pref > 0xFFFFFFFFUL)
2237            error_exit("Invalid %s",  (idx == 2) ? "Preference" : "fwmark");
2238          add_string_to_rtattr(&request.mhdr, sizeof(request),
2239              ((idx == 2) ? RTA_PRIORITY : RTA_PROTOINFO),
2240              (void *)&pref, sizeof(uint32_t));
2241        }
2242        break;
2243      case 3:
2244        {
2245          uint32_t tos;
2246          if (!*++argv) error_exit("Missing TOS key");
2247          if ((tos = idxfromRPDB(*argv, RPDB_rtdsfield)) < 0)
2248            error_exit("Invalid TOS");
2249          request.msg.rtm_tos = tos;
2250        }
2251        break;
2252      case 5:
2253        { // e.g. realms FROM_realm/TO_realm
2254          uint32_t realms = 0;
2255          int ret;
2256          char *ptr;
2257
2258          if (!*++argv) error_exit("Missing REALMSID");
2259          if ((ptr = strchr(*argv, '/'))) {
2260            *ptr = 0;
2261            if ((ret = idxfromRPDB(*argv, RPDB_rtrealms)) < 0)
2262              error_exit("Invalid realms");
2263            realms = ret;
2264            realms <<= 16;
2265            *ptr++ = '/';
2266          } else ptr = *argv;
2267          if ((ret = idxfromRPDB(ptr, RPDB_rtrealms)) < 0)
2268            error_exit("Invalid realms");
2269          realms |= ret;
2270          add_string_to_rtattr(&request.mhdr, sizeof(request),
2271              RTA_FLOW, (void *)&realms, sizeof(uint32_t));
2272        }
2273        break;
2274      case 6:
2275        { // e.g. table tid/tableName
2276          int tid;
2277          if (!*++argv) error_exit("Missing TableID");
2278          if ((tid = idxfromRPDB(*argv, RPDB_rttables)) < 0)
2279            error_exit("Invalid TID");
2280          request.msg.rtm_table = tid;
2281          tflag = 1;
2282        }
2283        break;
2284      case 7:
2285        {
2286          if (!*++argv) error_exit("Missing dev/iif NAME");
2287          add_string_to_rtattr(&request.mhdr, sizeof(request),
2288              RTA_IIF, *argv, strlen(*argv)+1);
2289        }
2290        break;
2291      case 8:
2292        {
2293          uint32_t addr[4] = {0,};
2294          uint8_t af = AF_UNSPEC;
2295
2296          if (!*++argv) error_exit("Missing nat/map-to ADDRESS");
2297          if (get_prefix(addr, &af /* Un-used variable */, *argv, AF_INET))
2298            error_exit("Invalid mapping Address");
2299
2300          add_string_to_rtattr(&request.mhdr, sizeof(request),
2301              RTA_GATEWAY, addr, sizeof(uint32_t));
2302          request.msg.rtm_type = RTN_NAT;
2303        }
2304        break;
2305      case 9:
2306        {
2307          if (!*++argv) error_exit("TYPE Missing");
2308          request.msg.rtm_type = rtmtype_str2idx(*argv);
2309        }
2310        break;
2311      case 10: 
2312        show_iprule_help();
2313        break; // Unreachable code.
2314      default: 
2315        error_exit("Invalid argument '%s'", *argv);
2316        break; // Unreachable code.
2317    }
2318  }
2319
2320  if (!request.msg.rtm_family) request.msg.rtm_family = AF_INET;
2321  if (!tflag && opt == RTM_NEWRULE) request.msg.rtm_table = RT_TABLE_MAIN;
2322
2323  send_nlmesg(0, 0, 0, &request, sizeof(request));
2324  return (filter_nlmesg(NULL, NULL));
2325}
2326
2327static int show_rules(struct nlmsghdr *mhdr,
2328    char **argv __attribute__ ((__unused__)))
2329{
2330  struct rtmsg *msg = NLMSG_DATA(mhdr);
2331  struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
2332  int32_t tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
2333  int hlen = ((msg->rtm_family == AF_INET) ? 32
2334      : ((msg->rtm_family == AF_INET6) ? 128 : -1));
2335
2336  if (mhdr->nlmsg_type != RTM_NEWRULE) return 0;
2337  if (msglen < 0) return 1;
2338
2339  tvar = msglen;
2340  for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
2341    if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
2342
2343  if (tvar) error_msg("deficit %d, rtalen = %d!", tvar, rta->rta_len);
2344
2345  printf("%u:\tfrom ", attr[RTA_PRIORITY] ?
2346      *(unsigned *)RTA_DATA(attr[RTA_PRIORITY]) : 0);
2347
2348  if (attr[RTA_SRC]) {
2349    printf("%s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6)
2350        ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_SRC]),
2351          toybuf, sizeof(toybuf))
2352        : "???");
2353    (msg->rtm_src_len != hlen) ? printf("/%u", msg->rtm_src_len) : 0;
2354  } else msg->rtm_src_len ? printf("0/%d", msg->rtm_src_len) : printf("all");
2355
2356  xputc(' ');
2357  if (attr[RTA_DST]) {
2358    printf("to %s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6)
2359        ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_DST]),
2360          toybuf, sizeof(toybuf))  : "???");
2361    (msg->rtm_dst_len != hlen) ? printf("/%u", msg->rtm_dst_len) : xputc(' ');
2362  } else if (msg->rtm_dst_len)
2363    printf("to 0/%d ", msg->rtm_dst_len);
2364
2365  if (msg->rtm_tos)
2366    printf("tos %s ", namefromRPDB(msg->rtm_tos, RPDB_rtdsfield));
2367
2368  if (attr[RTA_PROTOINFO])
2369    printf("fwmark %#x ", *(uint32_t*)RTA_DATA(attr[RTA_PROTOINFO]));
2370
2371  if (attr[RTA_IIF]) printf("iif %s ", (char*)RTA_DATA(attr[RTA_IIF]));
2372
2373  if (msg->rtm_table)
2374    printf("lookup %s ", namefromRPDB(msg->rtm_table, RPDB_rttables));
2375
2376  if (attr[RTA_FLOW]) {
2377    u_int32_t from, to = *(u_int32_t *)RTA_DATA(attr[RTA_FLOW]);
2378    char *format = "realms %s/";
2379
2380    to = (from = (to >> 16)) & 0xFFFF;
2381    format = (from ? format: "%s");
2382    printf(format, namefromRPDB((from ? from : to), RPDB_rtrealms));
2383  }
2384
2385  if (msg->rtm_type == RTN_NAT) {
2386    if (!attr[RTA_GATEWAY]) printf("masquerade");
2387    else printf("map-to %s ", inet_ntop(msg->rtm_family,
2388          RTA_DATA(attr[RTA_GATEWAY]), toybuf, sizeof(toybuf)));
2389  } else if (msg->rtm_type != RTN_UNICAST)
2390    printf("%s", rtmtype_idx2str(msg->rtm_type));
2391
2392  xputc('\n');
2393  return 0;
2394}
2395
2396static int rulelist(char **argv)
2397{
2398  if (*argv) {
2399    error_msg("'ip rule show' does not take any arguments.");
2400    return 1;
2401  }
2402  send_nlmesg(RTM_GETRULE, NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
2403      ((TT.addressfamily != AF_UNSPEC) ? TT.addressfamily : AF_INET), NULL, 0);
2404  return filter_nlmesg(show_rules, argv);
2405}
2406
2407static int iprule(char **argv)
2408{
2409  int idx;
2410  struct arglist options[] = {{"add", 0}, {"delete", 0}, {"list", 1},
2411    {"show", 1}, {NULL, -1}};
2412  cmdobj ipcmd, cmdobjlist[] = {ruleupdate, rulelist};
2413
2414  if (!*argv) idx = 1;
2415  else if ((idx = substring_to_idx(*argv++, options)) == -1)
2416    show_iprule_help();
2417  ipcmd = cmdobjlist[idx];
2418  return ipcmd(argv);
2419}
2420//============================================================================
2421// code for ip tunnel.
2422//============================================================================
2423static void show_iptunnel_help(void)
2424{
2425  char *errmsg = "usage: iptunnel { add | change | del | show } [NAME]\n"
2426    "           [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n"
2427    "           [[i|o]seq] [[i|o]key KEY] [[i|o]csum] [ttl TTL]\n"
2428    "           [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]";
2429
2430  error_exit(errmsg);
2431}
2432
2433static int tnl_ioctl(char *dev, int rtype, struct ip_tunnel_parm *ptnl)
2434{
2435  struct ifreq req;
2436  int fd, ret = 0;
2437
2438  if ((rtype == SIOCCHGTUNNEL || rtype == SIOCDELTUNNEL) && *ptnl->name)
2439    xstrncpy(req.ifr_name, ptnl->name, IF_NAMESIZE);
2440  else xstrncpy(req.ifr_name, dev, IF_NAMESIZE);
2441
2442  if (rtype != SIOCGIFHWADDR) req.ifr_ifru.ifru_data = (void*)ptnl;
2443  fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2444
2445  if (rtype == SIOCGETTUNNEL) ret = ioctl(fd, rtype, &req);
2446  else if (rtype == SIOCGIFHWADDR)
2447    ret = (ioctl(fd, rtype, &req) < 0) ? -1 : req.ifr_addr.sa_family;
2448  else xioctl(fd, rtype, &req);
2449
2450  close(fd);
2451  return ret;
2452}
2453
2454static int display_tunnel(struct ip_tunnel_parm *ptnl)
2455{
2456  char rmt_addr[64], lcl_addr[64], ikey_str[64], okey_str[64];
2457
2458  printf("%s: %s/ip", ptnl->name, ptnl->iph.protocol == IPPROTO_IPIP ? "ip" :
2459      (ptnl->iph.protocol == IPPROTO_GRE ? "gre" :
2460       (ptnl->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")));
2461  printf("  remote %s  local %s ", ptnl->iph.daddr ?
2462      inet_ntop(AF_INET, &ptnl->iph.daddr, rmt_addr, sizeof(rmt_addr)) : "any",
2463      ptnl->iph.saddr ? inet_ntop(AF_INET, &ptnl->iph.saddr, lcl_addr,
2464        sizeof(lcl_addr)) : "any");
2465  if (ptnl->link) {
2466    struct ifreq req;
2467    int fd;
2468
2469    req.ifr_ifindex = ptnl->link;
2470    fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2471    if (ioctl(fd, SIOCGIFNAME, &req) < 0) perror_msg("SIOCGIFNAME");
2472    else printf(" dev %s ", req.ifr_name);
2473    close(fd);
2474  }
2475  if (ptnl->iph.ttl) printf(" ttl %d ", ptnl->iph.ttl);
2476  else printf(" ttl inherit ");
2477
2478  if (ptnl->iph.tos) {
2479    printf(" tos");
2480    if (ptnl->iph.tos & 1) printf(" inherit");
2481    if (ptnl->iph.tos & ~1) printf("%c%s ", ptnl->iph.tos & 1 ? '/' : ' ',
2482        namefromRPDB((ptnl->iph.tos & ~1), RPDB_rtdsfield));
2483  }
2484  if (!(ptnl->iph.frag_off & htons(IP_DF))) printf(" nopmtudisc");
2485  inet_ntop(AF_INET, &ptnl->i_key, ikey_str, sizeof(ikey_str));
2486  if ((ptnl->i_flags & GRE_KEY) && (ptnl->o_flags & GRE_KEY)
2487      && ptnl->o_key == ptnl->i_key) printf(" key %s", ikey_str);
2488  else if ((ptnl->i_flags | ptnl->o_flags) & GRE_KEY) {
2489    inet_ntop(AF_INET, &ptnl->o_key, okey_str, sizeof(okey_str));
2490    if (ptnl->i_flags & GRE_KEY) printf(" ikey %s ", ikey_str);
2491    if (ptnl->o_flags & GRE_KEY) printf(" okey %s ", okey_str);
2492  }
2493  if (ptnl->i_flags & GRE_SEQ) printf("\n  Drop packets out of sequence.\n");
2494  if (ptnl->i_flags & GRE_CSUM)
2495    printf("\n  Checksum in received packet is required.");
2496  if (ptnl->o_flags & GRE_SEQ) printf("\n  Sequence packets on output.");
2497  if (ptnl->o_flags & GRE_CSUM) printf("\n  Checksum output packets.");
2498  xputc('\n');
2499  return 0;
2500}
2501
2502static int read_tunnel(struct ip_tunnel_parm *ptnl)
2503{
2504  int count = 0;
2505  char iface[IF_NAMESIZE];
2506  struct ip_tunnel_parm iptnl;
2507  FILE *fp = xfopen("/proc/net/dev", "r");
2508
2509  while (fgets(toybuf, sizeof(toybuf), fp)) {
2510    char *ptr;
2511    int ret;
2512
2513    if (count++ < 2) continue; // 1st two lines are header.
2514
2515    ptr = strchr(toybuf, ':');
2516    if (!ptr || (*ptr++ = 0, sscanf(toybuf, "%s", iface) != 1))
2517      error_exit("invalid format of '/proc/net/dev'");
2518    if (*ptnl->name && strcmp(ptnl->name, iface)) continue;
2519    if ((ret = tnl_ioctl(iface, SIOCGIFHWADDR, &iptnl)) < 0) {
2520      error_msg("failed to get type of '%s'", iface);
2521      continue;
2522    }
2523    if (ret != ARPHRD_TUNNEL && ret !=  ARPHRD_SIT &&
2524        ret != ARPHRD_IPGRE) continue;
2525
2526    memset(&iptnl, 0, sizeof(iptnl));
2527    if (tnl_ioctl(iface, SIOCGETTUNNEL, &iptnl) < 0) continue;
2528    if ((ptnl->link && iptnl.link != ptnl->link) || (*ptnl->name &&
2529          strcmp(iptnl.name, ptnl->name)) || (ptnl->iph.daddr &&
2530          iptnl.iph.daddr != ptnl->iph.daddr) || (ptnl->iph.saddr &&
2531            iptnl.iph.saddr != ptnl->iph.saddr) || (ptnl->i_key &&
2532              iptnl.i_key != ptnl->i_key)) continue;
2533    display_tunnel(&iptnl);
2534  }
2535  fclose(fp);
2536  return 0;
2537}
2538
2539static void parse_iptunnel_args(struct ip_tunnel_parm *ptnl, char **argv, 
2540    int ipt_opt_idx)
2541{
2542  int idx;
2543  uint8_t af = AF_INET;
2544  uint32_t addr = 0;
2545  struct arglist opts[] = { {"mode", 0}, {"key", 1}, {"ikey", 2},
2546    {"okey", 3}, {"seq", 4}, {"iseq", 5}, {"oseq", 6}, {"csum", 7},
2547    {"icsum", 8}, {"ocsum", 9}, {"nopmtudisc", 10}, {"pmtudisc", 11},
2548    {"remote", 12}, {"local", 13},{"dev", 14}, {"ttl", 15}, {"tos", 16},
2549    {"dsfield", 17}, {"name", 18}, {NULL, -1} 
2550  };
2551
2552  ptnl->iph.version = 4; // The value indicates the version of IP (4 or 6)
2553  ptnl->iph.ihl = 5; // Minimum Internet Header Length
2554  // frag_off is measured in units of 8 octets (64 bits)
2555  ptnl->iph.frag_off = htons(IP_DF);
2556  if (*argv && ipt_opt_idx <= 2 && string_to_idx(*argv, opts) == -1) {
2557    xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2558    if (ipt_opt_idx == 1) {
2559      struct ip_tunnel_parm iptnl_old;
2560
2561      memset(&iptnl_old, 0, sizeof(iptnl_old));
2562      tnl_ioctl(ptnl->name, SIOCGETTUNNEL, &iptnl_old);
2563      *ptnl = iptnl_old;
2564    }
2565    argv++;
2566  }
2567  for (; *argv; argv++, addr = 0) {
2568    switch (idx = string_to_idx(*argv, opts)) {
2569      case 0:
2570        if (!*++argv) error_exit("mode is missing");
2571        if ((!strcmp("ipip", *argv) || !strcmp("ip/ip", *argv)))
2572          ptnl->iph.protocol = IPPROTO_IPIP;
2573        else if ((!strcmp("gre", *argv) || !strcmp("gre/ip", *argv)))
2574          ptnl->iph.protocol = IPPROTO_GRE;
2575        else if ((!strcmp("sit", *argv) || !strcmp("ipv6/ip", *argv)))
2576          ptnl->iph.protocol = IPPROTO_IPV6;
2577        else show_iptunnel_help();
2578        break;
2579      case 1:
2580      case 2:
2581      case 3:
2582        {
2583          struct addrinfo *info, hint;
2584          int ret;
2585
2586          if (!*++argv) error_exit("key value is missing");
2587          memset(&hint, 0, sizeof(hint));
2588          hint.ai_family = AF_INET;
2589          ret = getaddrinfo(*argv, NULL, &hint, &info);
2590          if (ret || !info) error_exit("invalid argument to key");
2591          freeaddrinfo(info);
2592
2593          if (strchr(*argv, '.')) {
2594            if (get_prefix(&addr, &af, *argv, AF_INET))
2595              error_exit("invalid key '%s'", *argv);
2596          } else {
2597            unsigned key_val;
2598
2599            sscanf(*argv, "%u", &key_val);
2600            addr = htonl(key_val);
2601          }
2602          if (idx == 1) {
2603            ptnl->i_flags |= GRE_KEY;
2604            ptnl->o_flags |= GRE_KEY;
2605            ptnl->i_key = ptnl->o_key = addr;
2606          } else if (idx == 2) {
2607            ptnl->i_flags |= GRE_KEY;
2608            ptnl->i_key = addr;
2609          } else {
2610            ptnl->o_flags |= GRE_KEY;
2611            ptnl->o_key = addr;
2612          }
2613        }
2614        break;
2615      case 4:
2616        ptnl->i_flags |= GRE_SEQ;
2617        ptnl->o_flags |= GRE_SEQ;
2618        break;
2619      case 5:
2620        ptnl->i_flags |= GRE_SEQ;
2621        break;
2622      case 6:
2623        ptnl->o_flags |= GRE_SEQ;
2624        break;
2625      case 7:
2626        ptnl->i_flags |= GRE_CSUM;
2627        ptnl->o_flags |= GRE_CSUM;
2628        break;
2629      case 8:
2630        ptnl->i_flags |= GRE_CSUM;
2631        break;
2632      case 9:
2633        ptnl->o_flags |= GRE_CSUM;
2634        break;
2635      case 10:
2636        ptnl->iph.frag_off = 0;
2637        break;
2638      case 11:
2639        ptnl->iph.frag_off = htons(IP_DF);
2640        break;
2641      case 12:
2642      case 13:
2643        if (!*++argv) error_exit("remote/local address is missing");
2644        if (get_prefix(&addr, &af, *argv, AF_INET))
2645          error_exit("invalid remote/local address '%s'", *argv);
2646        (idx == 12) ? (ptnl->iph.daddr = addr) : (ptnl->iph.saddr = addr);
2647        break;
2648      case 14:
2649        if (!*++argv) error_exit("device name is missing");
2650        else {
2651          struct ifreq req;
2652          int fd;
2653
2654          xstrncpy(req.ifr_name, *argv, IFNAMSIZ);
2655          fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2656          xioctl(fd, SIOCGIFINDEX, &req);
2657          close(fd);
2658          ptnl->link = req.ifr_ifindex;
2659        }
2660        break;
2661      case 15:
2662        if (!*++argv) error_exit("ttl value is missing");
2663        if (strcmp(*argv, "inherit"))
2664          ptnl->iph.ttl = atolx_range(*argv, 0, 255);
2665        break;
2666      case 16:
2667      case 17:
2668        if (!*++argv) error_exit("tos value is missing");
2669        if (strcmp(*argv, "inherit")) {
2670          char *ptr;
2671          unsigned long tval = strtoul(*argv, &ptr, 16);
2672
2673          if (tval > 255) error_exit("invalid tos value '%s'", *argv);
2674          if (*ptr) {
2675            int ret;
2676
2677            if ((ret = idxfromRPDB(*argv, RPDB_rtdsfield)) < 0)
2678              error_exit("invalid tos value");
2679            ptnl->iph.tos = ret;
2680          } else ptnl->iph.tos = tval;
2681        } else ptnl->iph.tos = 1;
2682        break;
2683      case 18:
2684        if (*ptnl->name) error_exit("invalid tunnel");
2685        else {
2686          if (!*++argv) error_exit("name is missing");
2687          xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2688        }
2689        break;
2690      default:
2691        if (*ptnl->name) error_exit("invalid tunnel");
2692        xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2693        break;
2694    }
2695  }
2696  if (ptnl->iph.protocol == IPPROTO_IPIP ||
2697      ptnl->iph.protocol == IPPROTO_IPV6) {
2698    if ((ptnl->i_flags & GRE_KEY) || (ptnl->o_flags & GRE_KEY))
2699      error_exit("[i|o]key is allowed with gre only");
2700    if ((ptnl->i_flags & GRE_SEQ) || (ptnl->o_flags & GRE_SEQ))
2701      error_exit("[i|o]seq is allowed with gre only");
2702    if ((ptnl->i_flags & GRE_CSUM) || (ptnl->o_flags & GRE_CSUM))
2703      error_exit("[i|o]csum is allowed with gre only");
2704  }
2705  if (!ptnl->i_key && IN_MULTICAST(ntohl(ptnl->iph.daddr))) {
2706    ptnl->i_key = ptnl->iph.daddr;
2707    ptnl->i_flags |= GRE_KEY;
2708  }
2709  if (!ptnl->o_key && IN_MULTICAST(ntohl(ptnl->iph.daddr))) {
2710    ptnl->o_key = ptnl->iph.daddr;
2711    ptnl->o_flags |= GRE_KEY;
2712  }
2713  if (IN_MULTICAST(ntohl(ptnl->iph.daddr)) && !ptnl->iph.saddr)
2714    error_exit("broadcast tunnel requires a source address");
2715}
2716
2717static int tunnellist(char **argv)
2718{
2719  struct ip_tunnel_parm iptnl;
2720  int ret = 0;
2721
2722  memset(&iptnl, 0, sizeof(iptnl));
2723  parse_iptunnel_args(&iptnl, argv, 3);
2724
2725  if (iptnl.iph.protocol == IPPROTO_IPIP) 
2726    ret = tnl_ioctl(*iptnl.name ? iptnl.name : "tunl0", SIOCGETTUNNEL, &iptnl);
2727  else if (iptnl.iph.protocol == IPPROTO_GRE) 
2728    ret = tnl_ioctl(*iptnl.name ? iptnl.name : "gre0", SIOCGETTUNNEL, &iptnl);
2729  else if (iptnl.iph.protocol == IPPROTO_IPV6) 
2730    ret = tnl_ioctl(*iptnl.name ? iptnl.name : "sit0", SIOCGETTUNNEL, &iptnl);
2731  else return read_tunnel(&iptnl);
2732
2733  if (ret < 0) {
2734    perror_msg("SIOCGETTUNNEL");
2735    return ret;
2736  } else return display_tunnel(&iptnl);
2737}
2738
2739// Performing add, change, & delete tunnel action, according to passed req_type
2740static int tunnelupdate(char **argv)
2741{
2742  struct ip_tunnel_parm iptnl;
2743  int idx = 2, rtype = SIOCDELTUNNEL;
2744
2745  if (*argv[-1] == 'a') {
2746    idx = 0;
2747    rtype = SIOCADDTUNNEL;
2748  } else if (*argv[-1] == 'c') {
2749    idx = 1;
2750    rtype = SIOCCHGTUNNEL;
2751  }
2752
2753  memset(&iptnl, 0, sizeof(iptnl));
2754  parse_iptunnel_args(&iptnl, argv, idx);
2755  if (idx != 2 && iptnl.iph.ttl && !(iptnl.iph.frag_off))
2756    error_exit("ttl > 0 and nopmtudisc are incompatible");
2757  if (iptnl.iph.protocol == IPPROTO_IPIP)
2758    return (tnl_ioctl("tunl0", rtype, &iptnl) < 0) ? 1 : 0;
2759  else if (iptnl.iph.protocol == IPPROTO_GRE)
2760    return (tnl_ioctl("gre0", rtype, &iptnl) < 0) ? 1 : 0;
2761  else if (iptnl.iph.protocol == IPPROTO_IPV6)
2762    return (tnl_ioctl("sit0", rtype, &iptnl) < 0) ? 1 : 0;
2763  else {
2764    if (idx != 2) error_exit("invalid tunnel mode");
2765    return (tnl_ioctl(iptnl.name, rtype, &iptnl) < 0) ? 1 : 0;
2766  }
2767}
2768
2769static int iptunnel(char **argv)
2770{
2771  int idx;
2772  struct arglist opts[] = {{"add", 0}, {"change", 0}, {"del", 0},
2773    {"delete", 0}, {"show", 1}, {"list", 1}, {"lst", 1}, {NULL, -1}
2774  };
2775  cmdobj ipcmd, cmdobjlist[] = {tunnelupdate, tunnellist};
2776
2777  if (!*argv) idx = 1;
2778  else if ((idx = substring_to_idx(*argv++, opts)) == -1)
2779    show_iptunnel_help();
2780  ipcmd = cmdobjlist[idx];
2781  return ipcmd(argv);
2782}
2783
2784// ===========================================================================
2785// Common code, which is used for all ip options.
2786// ===========================================================================
2787
2788// Parse netlink messages and call input callback handler for action
2789static int filter_nlmesg(int (*fun)(struct nlmsghdr *mhdr, char **argv),
2790    char **argv)
2791{
2792  while (1) {
2793    struct nlmsghdr *mhdr;
2794    int msglen = recv(TT.sockfd, TT.gbuf, MESG_LEN, 0);
2795
2796    if ((msglen < 0) && (errno == EINTR || errno == EAGAIN)) continue;
2797    else if (msglen < 0) {
2798      error_msg("netlink receive error %s", strerror(errno));
2799      return 1;
2800    } else if (!msglen) {
2801      error_msg("EOF on netlink");
2802      return 1;
2803    }
2804
2805    for (mhdr = (struct nlmsghdr*)TT.gbuf; NLMSG_OK(mhdr, msglen);
2806        mhdr = NLMSG_NEXT(mhdr, msglen)) {
2807      int err;
2808      if (mhdr->nlmsg_pid != getpid())
2809        continue;
2810      switch (mhdr->nlmsg_type) {
2811        case NLMSG_DONE:
2812          return 0;
2813        case NLMSG_ERROR:
2814          {
2815            struct nlmsgerr *merr = (struct nlmsgerr*)NLMSG_DATA(mhdr);
2816
2817            if (merr->error == 0) return 0;
2818            if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
2819              error_msg("ERROR truncated");
2820            else {
2821              errno = -merr->error;
2822              perror_msg("RTNETLINK answers");
2823            }
2824            return 1;
2825          }
2826        default:
2827          if (fun && (err = fun(mhdr, argv))) return err;
2828          break;
2829      }
2830    } // End of for loop.
2831  } // End of while loop.
2832  return 0;
2833}
2834
2835void ip_main(void)
2836{
2837  char **optargv = toys.argv;
2838  int idx, isip = !(toys.which->name[2]); //1 -> if only ip
2839  cmdobj ipcmd, cmdobjlist[] = {ipaddr, iplink, iproute, iprule, iptunnel};
2840
2841  for (++optargv; *optargv; ++optargv) {
2842    char *ptr = *optargv;
2843    struct arglist ip_options[] = {{"oneline", 0}, {"family",  1},
2844      {"4", 1}, {"6", 1}, {"0", 1}, {"stats", 2}, {NULL, -1}};
2845
2846    if (*ptr != '-') break;
2847    else if ((*(ptr+1) == '-') && (*(ptr+2))) ptr +=2;
2848    //escape "--" and stop ip arg parsing.
2849    else if ((*(ptr+1) == '-') && (!*(ptr+2))) {
2850      *ptr +=1;
2851      break;
2852    } else ptr +=1;
2853    switch (substring_to_idx(ptr, ip_options)) {
2854      case 0: TT.singleline = 1;
2855              break;
2856      case 1: {
2857                if (isdigit(*ptr)) {
2858                  long num = atolx(ptr);
2859                  if (num == 4) TT.addressfamily  = AF_INET;
2860                  else if (num == 6) TT.addressfamily  = AF_INET6;
2861                  else TT.addressfamily = AF_PACKET;
2862                } else {
2863                  struct arglist ip_aflist[] = {{"inet", AF_INET},
2864                    {"inet6", AF_INET6}, {"link", AF_PACKET}, {NULL, -1}};
2865
2866                  if (!*++optargv) help_exit(0);
2867                  if ((TT.addressfamily = string_to_idx(*optargv, ip_aflist)) == -1)
2868                    error_exit("wrong family '%s'", *optargv);
2869                }
2870              }
2871              break;
2872      case 2:
2873              TT.stats++;
2874              break;
2875      default: help_exit(0);
2876               break; // unreachable code.
2877    }
2878  }
2879
2880  TT.sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2881
2882  if (isip) {// only for ip
2883    if (*optargv) {
2884      struct arglist ip_objectlist[] = { {"address", 0}, {"link", 1},
2885        {"route", 2}, {"rule", 3}, {"tunnel", 4}, {"tunl", 4}, {NULL, -1}};
2886
2887      if ((idx = substring_to_idx(*optargv, ip_objectlist)) == -1) help_exit(0);
2888      ipcmd = cmdobjlist[idx];
2889      toys.exitval = ipcmd(++optargv);
2890    } else help_exit(0);
2891  } else {
2892    struct arglist ip_objectlist[] = { {"ipaddr", 0}, {"iplink", 1},
2893      {"iproute", 2}, {"iprule", 3}, {"iptunnel", 4}, {NULL, -1}};
2894    if ((idx = string_to_idx(toys.which->name, ip_objectlist)) == -1)
2895      help_exit(0);
2896    ipcmd = cmdobjlist[idx];
2897    toys.exitval = ipcmd(optargv);
2898  }
2899  xclose(TT.sockfd);
2900  if (rtdsfield_init) free_alist(rt_dsfield);
2901  if (rtrealms_init) free_alist(rt_realms);
2902  if (rtscope_init) free_alist(rt_scope);
2903  if (rttable_init) free_alist(rt_tables);
2904  if (rtprotos_init) free_alist(rt_protos);
2905}
2906