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