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