1 /* ip.c - Show / manipulate routing, devices, policy routing and tunnels.
2 *
3 * Copyright 2014 Sameer Prakash Pradhan <sameer.p.pradhan@gmail.com>
4 * Copyright 2014 Ranjan Kumar <ranjankumar.bth@gmail.com>
5 * Copyright 2014 Rajni Kant <rajnikant12345@gmail.com>
6 * Copyright 2014 Bilal Qureshi <bilal.jmi@gmail.com>
7 *
8 * No Standard.
9 *
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, ¶m, 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}, {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
692 memset(&req, 0, sizeof(req));
693 if (!*argv) error_exit("\"dev\" missing");
694 xstrncpy(req.ifr_name, *argv, IF_NAMESIZE);
695 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
696 xioctl(fd, SIOCGIFINDEX, &req);
697 for (++argv; *argv;) {
698 if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) help_exit(0);
699 switch(idx) {
700 case 0:
701 flags |= IFF_UP; break;
702 case 1:
703 masks &= ~IFF_UP; break;
704 case 2:
705 case 3:
706 case 4:
707 if (!*argv) help_exit(0);
708 else if (!strcmp(*argv, "on")) {
709 if (idx == 2) {
710 masks &= ~case_flags[idx-2];
711 flags &= ~case_flags[idx-2];
712 } else flags |= case_flags[idx-2];
713 } else if (!strcmp(*argv,"off")) {
714 if (idx == 2) {
715 masks |= case_flags[idx-2];
716 flags |= case_flags[idx-2];
717 } else masks &= ~case_flags[idx-2];
718 } else help_exit(0);
719 ++argv;
720 break;
721 case 5:
722 xstrncpy(req.ifr_ifru.ifru_newname, *argv, IF_NAMESIZE);
723 xioctl(fd, SIOCSIFNAME, &req);
724 xstrncpy(req.ifr_name, *argv++, IF_NAMESIZE);
725 xioctl(fd, SIOCGIFINDEX, &req);
726 break;
727 case 6:
728 req.ifr_ifru.ifru_ivalue = atolx(*argv++);
729 xioctl(fd, SIOCSIFTXQLEN, &req);
730 break;
731 case 7:
732 req.ifr_ifru.ifru_mtu = atolx(*argv++);
733 xioctl(fd, SIOCSIFMTU, &req);
734 break;
735 case 8:
736 xioctl(fd, SIOCGIFHWADDR, &req);
737 fill_hwaddr(*argv++, IF_NAMESIZE,
738 (unsigned char *)(req.ifr_hwaddr.sa_data));
739 xioctl(fd, SIOCSIFHWADDR, &req);
740 break;
741 case 9:
742 xioctl(fd, SIOCGIFHWADDR, &req);
743 fill_hwaddr(*argv++, IF_NAMESIZE,
744 (unsigned char *)(req.ifr_hwaddr.sa_data));
745 xioctl(fd, SIOCSIFHWBROADCAST, &req);
746 break;
747 }
748 }
749 xioctl(fd, SIOCGIFFLAGS, &req);
750 req.ifr_ifru.ifru_flags |= flags;
751 req.ifr_ifru.ifru_flags &= masks;
752 xioctl(fd, SIOCSIFFLAGS, &req);
753 xclose(fd);
754 return 0;
755 }
756
print_stats(struct rtnl_link_stats * rtstat)757 static void print_stats(struct rtnl_link_stats *rtstat)
758 {
759 char *line_feed = (!TT.singleline ? "\n " : " ");
760
761 if (TT.stats > 0) {
762 xprintf(" RX: bytes packets errors "
763 "dropped overrun mcast%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
764 line_feed, rtstat->rx_bytes, rtstat->rx_packets, rtstat->rx_errors,
765 rtstat->rx_dropped, rtstat->rx_over_errors, rtstat->multicast);
766 if (TT.stats > 1) {
767 xprintf(" RX: errors length crc "
768 "frame fifo missed%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
769 line_feed, rtstat->rx_errors, rtstat->rx_length_errors,
770 rtstat->rx_crc_errors, rtstat->rx_frame_errors,
771 rtstat->rx_fifo_errors, rtstat->rx_missed_errors);
772 }
773 xprintf(" TX: bytes packets errors "
774 "dropped carrier collsns%s%-10u %-8u %-7u %-8u %-8u %-8u\n",
775 line_feed, rtstat->tx_bytes, rtstat->tx_packets, rtstat->tx_errors,
776 rtstat->tx_dropped, rtstat->tx_carrier_errors, rtstat->collisions);
777 if (TT.stats > 1) {
778 xprintf(" TX: errors aborted fifo window "
779 "heartbeat%s%-10u %-8u %-7u %-8u %-8u\n",
780 line_feed, rtstat->tx_errors, rtstat->tx_aborted_errors,
781 rtstat->tx_fifo_errors, rtstat->tx_window_errors,
782 rtstat->tx_heartbeat_errors);
783 }
784 }
785 }
786
print_link_output(struct linkdata * link)787 static int print_link_output(struct linkdata *link)
788 {
789 char *line_feed = " ", *flags,*peer = "brd";
790 struct arglist iface_flags[] = {{"",0},{"UP", IFF_UP},
791 {"BROADCAST", IFF_BROADCAST}, {"DEBUG", IFF_DEBUG},
792 {"LOOPBACK", IFF_LOOPBACK}, {"POINTOPOINT", IFF_POINTOPOINT},
793 {"NOTRAILERS", IFF_NOTRAILERS}, {"RUNNING", IFF_RUNNING},
794 {"NOARP", IFF_NOARP}, {"PROMISC",IFF_PROMISC},
795 {"ALLMULTI", IFF_ALLMULTI}, {"MASTER", IFF_MASTER}, {"SLAVE", IFF_SLAVE},
796 {"MULTICAST", IFF_MULTICAST}, {"PORTSEL", IFF_PORTSEL},
797 {"AUTOMEDIA", IFF_AUTOMEDIA}, {"DYNAMIC", IFF_DYNAMIC}, {NULL,-1}};
798
799 if (link->parent != -1) {
800 int fd = 0;
801 struct ifreq req;
802
803 memset(&req, 0, sizeof(req));
804 if_indextoname( link->parent,req.ifr_ifrn.ifrn_name);
805 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
806 if (ioctl(fd, SIOCGIFTXQLEN, &req)) perror("");
807 else link->txqueuelen = req.ifr_ifru.ifru_ivalue;
808 xclose(fd);
809 }
810
811 if (TT.is_addr && addrinfo.label && fnmatch(addrinfo.label, link->iface, 0))
812 return 0;
813
814
815 if (!(flags = get_flag_string(iface_flags, link->flags, 1)))
816 error_exit("Invalid data.");
817 if (!TT.singleline) line_feed="\n ";
818 if (link->parent != -1) {
819 char iface[IF_NAMESIZE];
820
821 if (!if_indextoname(link->parent, iface)) perror_exit(NULL);
822 sprintf(toybuf,"%s@%s", link->iface, iface);
823 }
824 if (link->flags & IFF_POINTOPOINT) peer = "peer";
825 if (TT.is_addr && TT.singleline && TT.addressfamily)
826 xprintf("%d: %s", link->iface_idx,
827 ((link->parent == -1) ? link->iface : toybuf));
828 else xprintf("%d: %s: <%s> mtu %d qdisc %s state %s qlen %d",
829 link->iface_idx, ((link->parent == -1) ? link->iface : toybuf), flags,
830 link->mtu, link->qdiscpline, link->state, link->txqueuelen);
831
832 if (!TT.addressfamily || TT.addressfamily == AF_PACKET)
833 xprintf("%slink/%s %s %s %s",
834 line_feed, link->type, link->laddr, peer ,link->bcast);
835
836 xputc('\n');
837
838 //user can specify stats flag two times
839 //one for stats and other for erros e.g. -s and -s -s
840 print_stats(&link->rt_stat);
841 free(flags);
842
843 return 0;
844 }
845
fill_address(void * p,char * ip)846 static void fill_address(void *p, char *ip)
847 {
848 unsigned char *ptr = (unsigned char*)p;
849 snprintf(ip, 64, " %02x:%02x:%02x:%02x:%02x:%02x",
850 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
851 }
852
get_link_info(struct nlmsghdr * h,struct linkdata * link,char ** argv)853 static int get_link_info(struct nlmsghdr* h,struct linkdata* link,char **argv)
854 {
855 struct ifinfomsg *iface = NLMSG_DATA(h);
856 struct rtattr *attr = IFLA_RTA(iface);
857 int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*iface));
858 struct arglist hwtypes[]={{"generic",0},{"ether",ARPHRD_ETHER},
859 {"loopback", ARPHRD_LOOPBACK},{"sit",ARPHRD_SIT},
860 #ifdef ARPHRD_INFINIBAND
861 {"infiniband",ARPHRD_INFINIBAND},
862 #endif
863 #ifdef ARPHRD_IEEE802_TR
864 {"ieee802",ARPHRD_IEEE802}, {"tr",ARPHRD_IEEE802_TR},
865 #else
866 {"tr",ARPHRD_IEEE802},
867 #endif
868 #ifdef ARPHRD_IEEE80211
869 {"ieee802.11",ARPHRD_IEEE80211},
870 #endif
871 #ifdef ARPHRD_IEEE1394
872 {"ieee1394",ARPHRD_IEEE1394},
873 #endif
874 {"irda",ARPHRD_IRDA},{"slip",ARPHRD_SLIP},{"cslip",ARPHRD_CSLIP},
875 {"slip6",ARPHRD_SLIP6}, {"cslip6",ARPHRD_CSLIP6}, {"ppp",ARPHRD_PPP},
876 {"ipip",ARPHRD_TUNNEL}, {"tunnel6",ARPHRD_TUNNEL6},
877 {"gre",ARPHRD_IPGRE},
878 #ifdef ARPHRD_VOID
879 {"void",ARPHRD_VOID},
880 #endif
881 {NULL,-1}};
882 char *lname = get_flag_string(hwtypes, iface->ifi_type, 0);
883
884 link->next = link->prev = 0;
885 link->iface_type = iface->ifi_type;
886 if (!lname) error_exit("Invalid link.");
887 xstrncpy(link->type, lname, IFNAMSIZ);
888 free(lname);
889 link->iface_idx = iface->ifi_index;
890 link->flags = iface->ifi_flags;
891 if (*argv && !strcasecmp("up",*argv) && !(link->flags & IFF_UP)) return 1;
892 link->parent = -1;
893 for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) {
894 switch(attr->rta_type) {
895 case IFLA_IFNAME:
896 snprintf(link->iface, IFNAMSIZ, "%s",(char *) RTA_DATA(attr));
897 break;
898 case IFLA_ADDRESS:
899 if ( iface->ifi_type== ARPHRD_TUNNEL ||
900 iface->ifi_type == ARPHRD_SIT ||
901 iface->ifi_type == ARPHRD_IPGRE)
902 inet_ntop(AF_INET, RTA_DATA(attr), link->laddr, 64);
903 else fill_address(RTA_DATA(attr), link->laddr);
904 break;
905 case IFLA_BROADCAST:
906 if (iface->ifi_type== ARPHRD_TUNNEL ||
907 iface->ifi_type == ARPHRD_SIT ||
908 iface->ifi_type == ARPHRD_IPGRE)
909 inet_ntop(AF_INET, RTA_DATA(attr), link->bcast, 64);
910 else fill_address(RTA_DATA(attr), link->bcast);
911 break;
912 case IFLA_MTU:
913 link->mtu = *((int*)(RTA_DATA(attr)));
914 break;
915 case IFLA_QDISC:
916 snprintf(link->qdiscpline, IFNAMSIZ, "%s", (char *) RTA_DATA(attr));
917 break;
918 case IFLA_STATS :
919 link->rt_stat = *((struct rtnl_link_stats*) RTA_DATA(attr));
920 break;
921 case IFLA_LINK:
922 link->parent = *((int*)(RTA_DATA(attr)));
923 break;
924 case IFLA_TXQLEN:
925 link->txqueuelen = *((int*)(RTA_DATA(attr)));
926 break;
927 case IFLA_OPERSTATE:
928 {
929 struct arglist flags[]={{"UNKNOWN", 0}, {"NOTPRESENT", 1},
930 {"DOWN", 2}, {"LOWERLAYERDOWN", 3}, {"TESTING", 4},
931 {"DORMANT", 5}, {"UP", 6}, {NULL, -1}};
932 if (!(lname = get_flag_string(flags, *((int*)(RTA_DATA(attr))), 0)))
933 error_exit("Invalid state.");
934 xstrncpy(link->state, lname,IFNAMSIZ);
935 free(lname);
936 }
937 break;
938 default: break;
939 }
940 }
941 return 0;
942 }
943
display_link_info(struct nlmsghdr * mhdr,char ** argv)944 static int display_link_info(struct nlmsghdr *mhdr, char **argv)
945 {
946 struct linkdata link;
947
948 if (!get_link_info(mhdr, &link, argv)) {
949 if (TT.is_addr) {
950 struct linkdata *lnk = xzalloc(sizeof(struct linkdata));
951 memcpy(lnk, &link, sizeof(struct linkdata));
952 dlist_add_nomalloc((struct double_list **)&linfo,
953 (struct double_list *)lnk);
954 }
955 else print_link_output(&link);
956 }
957 return 0;
958 }
959
link_show(char ** argv)960 static int link_show(char **argv)
961 {
962 struct {
963 struct nlmsghdr mhdr;
964 struct ifinfomsg info;
965 } request;
966 uint32_t index = 0;
967
968 if (*argv && strcasecmp("up",*argv)) index = get_ifaceindex(*argv, 1);
969 memset(&request, 0, sizeof(request));
970 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
971 request.mhdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
972 if (!index) request.mhdr.nlmsg_flags |= NLM_F_ROOT|NLM_F_MATCH;
973 else request.info.ifi_change = 0xffffffff; // used in single operation
974 request.mhdr.nlmsg_type = RTM_GETLINK;
975 request.info.ifi_index = index;
976 request.info.ifi_family = AF_UNSPEC;
977 send_nlmesg(0, 0, 0, (void*)&request, sizeof(request));
978 return (filter_nlmesg(display_link_info, argv));
979 }
980
iplink(char ** argv)981 static int iplink(char **argv)
982 {
983 int idx;
984 cmdobj ipcmd, cmdobjlist[] = {linkupdate, link_set, link_show};
985 struct arglist cmd_objectlist[] = {{"add", 0}, {"delete", 0},
986 {"set", 1}, {"show", 2}, {"list", 2}, {"lst", 2}, {NULL,-1}};
987
988 if (!*argv) idx = 2;
989 else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1)
990 help_exit(0);
991 ipcmd = cmdobjlist[idx];
992 return ipcmd(argv);
993 }
994
995 // ===========================================================================
996 // Code for ip addr.
997 // ===========================================================================
998
print_addrinfo(struct nlmsghdr * h,int flag_l)999 static int print_addrinfo(struct nlmsghdr *h, int flag_l)
1000 {
1001 struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,};
1002 char *family = toybuf, *scope = toybuf+256, *label = toybuf+512,
1003 *brd = toybuf+768, *peer = toybuf+1024, *any = toybuf+1280,
1004 lbuf[INET6_ADDRSTRLEN] = {0,}, lbuf_ifa[INET6_ADDRSTRLEN] = {0,};
1005 struct ifaddrmsg *ifa = NLMSG_DATA(h);
1006 int len;
1007
1008 if ((len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))) < 0) {
1009 error_msg("wrong nlmsg len %d", len);
1010 return 0;
1011 }
1012
1013 for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta=RTA_NEXT(rta, len))
1014 if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta;
1015
1016 if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1017 if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
1018 if ((addrinfo.scope ^ ifa->ifa_scope)&addrinfo.scopemask) return 0;
1019 if (addrinfo.ifindex && addrinfo.ifindex != ifa->ifa_index) return 0;
1020
1021 if (flag_l && addrinfo.label && ifa->ifa_family == AF_INET6) return 0;
1022 if ((rta_tb[IFA_LABEL])) {
1023 xstrncpy(label, RTA_DATA(rta_tb[IFA_LABEL]), 256);
1024 label[255] = '\0';
1025 if (addrinfo.label && fnmatch(addrinfo.label, label, 0))
1026 return 0;
1027 }
1028
1029 if (TT.flush) {
1030 if (ifa->ifa_index == addrinfo.ifindex) {
1031 h->nlmsg_type = RTM_DELADDR;
1032 h->nlmsg_flags = NLM_F_REQUEST;
1033 send_nlmesg(RTM_DELADDR, 0, 0, h, h->nlmsg_len);
1034 return 0;
1035 }
1036 }
1037
1038 if (h->nlmsg_type == RTM_DELADDR) printf("Deleted ");
1039
1040 if (TT.singleline) {
1041 if (!if_indextoname(ifa->ifa_index, lbuf)) perror_exit(NULL);
1042 printf("%u: %s",ifa->ifa_index, lbuf);
1043 }
1044
1045 sprintf(scope, " scope %s ", namefromRPDB(ifa->ifa_scope, RPDB_rtscopes));
1046
1047 if (ifa->ifa_family == AF_INET) strcpy(family, " inet ");
1048 else if (ifa->ifa_family == AF_INET6) strcpy(family, " inet6 ");
1049 else sprintf(family, " family %d", ifa->ifa_family);
1050
1051 if (rta_tb[IFA_LOCAL]) {
1052 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL]),
1053 lbuf, sizeof(lbuf))) perror_exit("inet");
1054
1055 sprintf(family+strlen(family), lbuf, strlen(lbuf));
1056 if (!rta_tb[IFA_ADDRESS] || !memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
1057 RTA_DATA(rta_tb[IFA_LOCAL]), 4))
1058 sprintf(family+strlen(family), "/%d ", ifa->ifa_prefixlen);
1059 else {
1060 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS]),
1061 lbuf_ifa, sizeof(lbuf_ifa))) perror_exit("inet");
1062 sprintf(peer, " peer %s/%d ", lbuf_ifa, ifa->ifa_prefixlen);
1063 }
1064 }
1065
1066 if (addrinfo.to && strcmp(addrinfo.addr, lbuf))
1067 return 0;
1068
1069 if (rta_tb[IFA_BROADCAST]) {
1070 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_BROADCAST]),
1071 lbuf, sizeof(lbuf))) perror_exit("inet");
1072 sprintf(brd, " brd %s", lbuf);
1073 }else brd = "";
1074
1075 if (rta_tb[IFA_ANYCAST]) {
1076 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ANYCAST]),
1077 lbuf, sizeof(lbuf))) perror_exit("inet");
1078 sprintf(any, " any %s", lbuf);
1079 }
1080
1081 if (ifa->ifa_family == AF_INET)
1082 printf("%s%s%s%s%s %c", family, brd, peer, scope, label,
1083 (TT.singleline? '\0' : '\n'));
1084 else printf("%s%s %c", family, scope, (TT.singleline? '\0' : '\n'));
1085 if (TT.singleline && (ifa->ifa_family == AF_INET)) xputc('\n');
1086
1087 if (rta_tb[IFA_CACHEINFO]) {
1088 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
1089
1090 printf("%c valid_lft ", (TT.singleline? '\\' : '\0'));
1091 if (ci->ifa_valid == 0xFFFFFFFFU) printf("forever");
1092 else printf("%usec", ci->ifa_valid);
1093 printf(" preferred_lft ");
1094 if (ci->ifa_prefered == 0xFFFFFFFFU) printf("forever");
1095 else printf("%dsec", ci->ifa_prefered);
1096 xputc('\n');
1097 }
1098 return 0;
1099 }
1100
ipaddrupdate(char ** argv)1101 static int ipaddrupdate(char **argv)
1102 {
1103 int length, cmd = !memcmp("add", argv[-1], strlen(argv[-1]))
1104 ? RTM_NEWADDR: RTM_DELADDR;
1105 int idx = 0,length_brd = 0, length_peer = 0,length_any = 0,length_local = 0,
1106 scoped = 0;
1107 char *dev = NULL,*label = NULL, reply[8192];
1108
1109 struct nlmsghdr *addr_ptr = NULL;
1110 struct nlmsgerr *err = NULL;
1111 struct arglist cmd_objectlist[] = {{"dev",0}, {"peer", 1},
1112 {"remote", 2}, {"broadcast", 3}, {"brd", 4}, {"label", 5},
1113 {"anycast", 6},{"scope", 7}, {"local", 8}, {NULL, -1}};
1114 struct {
1115 struct nlmsghdr nlm;
1116 struct ifaddrmsg ifadd;
1117 char buf[256];
1118 } req;
1119 typedef struct {
1120 int family, bytelen, bitlen;
1121 __u32 data[8];
1122 } option_data;
1123 option_data local;
1124
1125 memset(&req, 0, sizeof(req));
1126 req.nlm.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1127 req.nlm.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK;
1128 req.nlm.nlmsg_type = cmd;
1129 req.ifadd.ifa_family = TT.addressfamily;
1130
1131 while (*argv) {
1132 idx = substring_to_idx(*argv, cmd_objectlist);
1133 if (idx >= 0)
1134 if (!*++argv)
1135 error_exit("Incomplete Command line");
1136 switch(idx) {
1137 case 0:
1138 dev = *argv;
1139 break;
1140 case 1:
1141 case 2:
1142 {
1143 uint32_t addr[4] = {0,}, netmask = 0;
1144 uint8_t len = 0;
1145 parse_prefix(addr, &netmask, &len, *argv,
1146 req.ifadd.ifa_family);
1147 if (len)
1148 req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6);
1149 length_peer = len;
1150 add_string_to_rtattr(&req.nlm, sizeof(req),
1151 IFA_ADDRESS, addr, len);
1152 req.ifadd.ifa_prefixlen = netmask;
1153 }
1154 break;
1155 case 3:
1156 case 4:
1157 if (*argv[0] == '+') {
1158 length_brd = -1;
1159 } else if (*argv[0] == '-') {
1160 length_brd = -2;
1161 } else {
1162 uint32_t addr[4] = {0,};
1163 uint8_t af = AF_UNSPEC;
1164
1165 if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family))
1166 error_exit("Invalid prefix");
1167
1168 length_brd = ((af == AF_INET6) ? 16 : 4);
1169 if (req.ifadd.ifa_family == AF_UNSPEC)
1170 req.ifadd.ifa_family = af;
1171 add_string_to_rtattr(&req.nlm, sizeof(req),
1172 IFA_BROADCAST, &addr, length_brd);
1173 }
1174 break;
1175 case 5:
1176 label = *argv;
1177 add_string_to_rtattr(&req.nlm, sizeof(req),
1178 IFA_LABEL, label, strlen(label) + 1);
1179 break;
1180 case 6:
1181 {
1182 uint32_t addr[4] = {0,};
1183 uint8_t af = AF_UNSPEC;
1184
1185 if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family))
1186 error_exit("Invalid prefix");
1187 length_any = ((af == AF_INET6) ? 16 : 4);
1188 if (req.ifadd.ifa_family == AF_UNSPEC)
1189 req.ifadd.ifa_family = af;
1190 add_string_to_rtattr(&req.nlm, sizeof(req),
1191 IFA_ANYCAST, &addr, length_any);
1192 }
1193 break;
1194 case 7:
1195 {
1196 int scope = idxfromRPDB(*argv, RPDB_rtscopes);
1197 if (scope < 0) error_exit("wrong scope '%s'", *argv);
1198 req.ifadd.ifa_scope = scope;
1199 scoped = 1;
1200 }
1201 break;
1202 default:
1203 {
1204 //local is by default
1205 uint32_t addr[8] = {0,}, netmask = 0;
1206 uint8_t len = 0;
1207
1208 parse_prefix(addr, &netmask, &len, *argv,
1209 req.ifadd.ifa_family);
1210 if (len)
1211 req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6);
1212 length_local = len;
1213 local.bitlen = netmask;
1214 local.bytelen = len;
1215 memcpy(local.data, addr, sizeof(local.data));
1216 local.family = req.ifadd.ifa_family;
1217 add_string_to_rtattr(&req.nlm, sizeof(req),
1218 IFA_LOCAL, &local.data, local.bytelen);
1219 }
1220 break;
1221 }
1222 argv++;
1223 }
1224 if (!dev) error_exit("need \"dev \" argument");
1225 if (label && strncmp(dev, label, strlen(dev)) != 0)
1226 error_exit("\"dev\" (%s) must match \"label\" (%s)", dev, label);
1227
1228 if (length_peer == 0 && length_local && cmd != RTM_DELADDR){
1229 add_string_to_rtattr(&req.nlm, sizeof(req),
1230 IFA_ADDRESS, &local.data, local.bytelen);
1231 }
1232
1233 if (length_brd < 0 && cmd != RTM_DELADDR){
1234 int i;
1235
1236 if (req.ifadd.ifa_family != AF_INET)
1237 error_exit("broadcast can be set only for IPv4 addresses");
1238
1239 if (local.bitlen <= 30) {
1240 for (i = 31; i >= local.bitlen; i--) {
1241 if (length_brd == -1)
1242 local.data[0] |= htonl(1<<(31-i));
1243 else
1244 local.data[0] &= ~htonl(1<<(31-i));
1245 }
1246 add_string_to_rtattr(&req.nlm, sizeof(req),
1247 IFA_BROADCAST, &local.data, local.bytelen);
1248 length_brd = local.bytelen;
1249 }
1250 }
1251 if (req.ifadd.ifa_prefixlen == 0)
1252 req.ifadd.ifa_prefixlen = local.bitlen;
1253 if (!scoped && (cmd != RTM_DELADDR) && (local.family == AF_INET)
1254 && (local.bytelen >= 1 && *(uint8_t*)&local.data == 127))
1255 req.ifadd.ifa_scope = RT_SCOPE_HOST;
1256 req.ifadd.ifa_index = get_ifaceindex(dev, 1);
1257
1258 send_nlmesg(RTM_NEWADDR, 0, AF_UNSPEC, (void *)&req, req.nlm.nlmsg_len);
1259 length = recv(TT.sockfd, reply, sizeof(reply), 0);
1260 addr_ptr = (struct nlmsghdr *) reply;
1261 for (; NLMSG_OK(addr_ptr, length); addr_ptr = NLMSG_NEXT(addr_ptr, length)) {
1262 if (addr_ptr->nlmsg_type == NLMSG_DONE)
1263 return 1;
1264 if (addr_ptr->nlmsg_type == NLMSG_ERROR)
1265 err = (struct nlmsgerr*) NLMSG_DATA(addr_ptr);
1266 if (err && err->error) {
1267 errno = -err->error;
1268 perror_exit("RTNETLINK answers:");
1269 }
1270 }
1271 return 0;
1272 }
1273
ipaddr_listflush(char ** argv)1274 static int ipaddr_listflush(char **argv)
1275 {
1276 int idx; uint32_t netmask = 0, found = 0;
1277 char *tmp = NULL, *name = NULL;
1278 struct double_list *dlist;
1279 struct arglist cmd_objectlist[] = {{"to", 0}, {"scope", 1}, {"up", 2},
1280 {"label", 3}, {"dev", 4}, {NULL, -1}};
1281
1282 TT.flush = *argv[-1] == 'f' ? 1 : 0;
1283 memset(&addrinfo, 0, sizeof(addrinfo));
1284
1285 if (TT.flush) {
1286 if (!*argv)
1287 error_exit("Incomplete command for \"flush\"");
1288 if (TT.addressfamily == AF_PACKET)
1289 error_exit("Can't flush link Addresses");
1290 }
1291 addrinfo.scope = -1;
1292 while (*argv) {
1293 switch (idx = substring_to_idx(*argv, cmd_objectlist)) {
1294 case 0:
1295 {// ADDR_TO
1296 if (!*++argv) error_exit("Incomplete Command line");
1297 else if (!strcmp(*argv, "0")) return 0;
1298 uint32_t addr[4] = {0,};
1299 uint8_t len = 0;
1300
1301 addrinfo.to = 1;
1302 parse_prefix(addr, &netmask, &len, *argv, TT.addressfamily);
1303 if (len)
1304 TT.addressfamily = ((len == 4) ? AF_INET : AF_INET6);
1305 addrinfo.addr = strtok(*argv, "/");
1306 }
1307 break;
1308 case 1: // ADDR_SCOPE
1309 {
1310 int scope = 0;
1311 if (!*++argv) error_exit("Incomplete Command line");
1312 name = *argv;
1313
1314 addrinfo.scopemask = -1;
1315 if (isdigit(**argv)) {
1316 int idx = atolx(*argv);
1317
1318 name = xstrdup(namefromRPDB(idx, RPDB_rtscopes));
1319 }
1320 if ((scope = idxfromRPDB(name, RPDB_rtscopes)) < 0) {
1321 if (strcmp(name, "all"))
1322 error_exit("wrong scope '%s'", name);
1323 scope = RT_SCOPE_NOWHERE;
1324 addrinfo.scopemask = 0;
1325 }
1326
1327 if (isdigit(**argv))
1328 free(name);
1329 addrinfo.scope = scope;
1330 }
1331 break;
1332 case 2: // ADDR_UP
1333 addrinfo.up = 1;
1334 break;
1335 case 3: // ADDR_LABEL
1336 if (!*++argv) error_exit("Incomplete Command line");
1337 addrinfo.label = *argv;
1338 break;
1339 case 4: // ADDR_DEV
1340 if (!*++argv) error_exit("Incomplete Command line");
1341
1342 default:
1343 if (TT.filter_dev)
1344 error_exit("Either \"dev\" is duplicate or %s is garbage",
1345 *argv);
1346 TT.filter_dev = *argv;
1347 break;
1348 }
1349 argv++;
1350 }
1351
1352 link_show(&tmp);
1353 while ( linfo && (dlist = dlist_pop(&linfo))){
1354 struct linkdata *tmp = (struct linkdata*) dlist;
1355 char *temp = &tmp->iface[0];
1356
1357 if (TT.filter_dev && strcmp(TT.filter_dev, temp))
1358 continue;
1359 found = 1;
1360 if (TT.flush && addrinfo.label) ipaddr_print( tmp, 0);
1361 if (addrinfo.up && !(tmp->flags & IFF_UP)){
1362 ipaddr_print(tmp, 0);
1363 continue;
1364 }
1365 if (addrinfo.label){
1366 if ( fnmatch(addrinfo.label, temp, 0)) {
1367 ipaddr_print(tmp, 1);
1368 continue;
1369 }
1370 }
1371 if (!TT.addressfamily && ! TT.flush ) print_link_output(tmp);
1372
1373 ipaddr_print(tmp, 0);
1374 free(tmp);
1375 }
1376 if (TT.filter_dev && !found)
1377 error_exit("Device \"%s\" doesn't exist. \n", TT.filter_dev);
1378 return 0;
1379 }
1380
ipaddr_print(struct linkdata * link,int flag_l)1381 static int ipaddr_print( struct linkdata *link, int flag_l)
1382 {
1383 struct nlmsghdr *addr_ptr;
1384 int ip_match = 0;
1385
1386 addrinfo.ifindex = link->iface_idx;
1387 send_nlmesg(RTM_GETADDR, NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
1388 AF_UNSPEC, NULL, 0);
1389 if (TT.addressfamily == AF_PACKET) print_link_output(link);
1390
1391 if (addrinfo.label){
1392 char *col = strchr(addrinfo.label, ':');
1393 if (!col && (fnmatch(addrinfo.label, &link->iface[0], 0)))
1394 return 0;
1395 }
1396
1397 while (1){
1398 int len = recv(TT.sockfd, TT.gbuf, sizeof(TT.gbuf), 0);
1399 addr_ptr = (struct nlmsghdr *)TT.gbuf;
1400 struct ifaddrmsg *addressInfo = NLMSG_DATA(addr_ptr);
1401 char lbuf[INET6_ADDRSTRLEN];
1402 struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,};
1403
1404 int len1 = addr_ptr->nlmsg_len - NLMSG_LENGTH(sizeof(*addressInfo));
1405 if (len1 > 0) {
1406 for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) {
1407 addressInfo = NLMSG_DATA(addr_ptr);
1408 if (TT.addressfamily && TT.addressfamily != addressInfo->ifa_family)
1409 continue;
1410 if (addrinfo.ifindex && addrinfo.ifindex != addressInfo->ifa_index)
1411 continue;
1412
1413 if (addrinfo.to) {
1414 memset(rta_tb, 0, sizeof(rta_tb));
1415 int rt_len = IFA_PAYLOAD(addr_ptr);
1416 for (rta = IFA_RTA(addressInfo); RTA_OK(rta, rt_len); rta=RTA_NEXT(rta, rt_len)) {
1417 if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta;
1418 }
1419 if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
1420 if (rta_tb[IFA_LOCAL]) {
1421 if (!inet_ntop(TT.addressfamily, RTA_DATA(rta_tb[IFA_LOCAL]),
1422 lbuf, sizeof(lbuf))) perror_exit("inet");
1423 if (strcmp(addrinfo.addr, lbuf))
1424 continue;
1425 ip_match=1;
1426 }
1427 if (!ip_match)
1428 continue;
1429 }
1430
1431 if (!TT.flush){
1432 if (addrinfo.scope != -1 && TT.addressfamily && TT.addressfamily ==
1433 addressInfo->ifa_family &&
1434 (addrinfo.ifindex == addressInfo->ifa_index)) {
1435 if ((addrinfo.scope ^ addressInfo->ifa_scope) & addrinfo.scopemask)
1436 continue;
1437 else if (addrinfo.up && (link->flags & IFF_UP))
1438 print_link_output(link);
1439 else if (!addrinfo.up) print_link_output(link);
1440 }
1441 if (TT.addressfamily &&
1442 (addrinfo.ifindex == addressInfo->ifa_index) &&
1443 (addrinfo.scope == -1)){
1444 if (addrinfo.up && (link->flags & IFF_UP))
1445 print_link_output(link);
1446 else if (!addrinfo.up) print_link_output(link);
1447 }
1448 }
1449
1450 for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) {
1451 if (addr_ptr->nlmsg_type == RTM_NEWADDR)
1452 print_addrinfo(addr_ptr, flag_l);
1453 if ((addr_ptr->nlmsg_type == NLMSG_DONE) ||
1454 (addr_ptr->nlmsg_type == NLMSG_ERROR) ||
1455 (TT.flush && addrinfo.to))
1456 goto ret_stop;
1457 }
1458 if ((addr_ptr->nlmsg_type == NLMSG_DONE) ||
1459 (addr_ptr->nlmsg_type == NLMSG_ERROR))
1460 break;
1461 }
1462 }
1463 else
1464 return 0;
1465 }
1466
1467 ret_stop:
1468 return 0;
1469 }
1470
ipaddr(char ** argv)1471 static int ipaddr(char **argv)
1472 {
1473 int idx;
1474 cmdobj ipcmd, cmdobjlist[] = {ipaddrupdate, ipaddr_listflush};
1475 struct arglist cmd_objectlist[] = { {"add", 0}, {"delete", 0},
1476 {"list", 1},{"show", 1},{"lst", 1}, {"flush", 1}, {NULL,-1}};
1477
1478 TT.is_addr++;
1479 if (!*argv) idx = 1;
1480 else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1)
1481 help_exit(0);
1482
1483 ipcmd = cmdobjlist[idx];
1484 return ipcmd(argv);
1485 }
1486
1487 // ===========================================================================
1488 // code for ip route
1489 // ===========================================================================
1490 struct I_data {
1491 unsigned char family;
1492 uint32_t addr[8] , netmask ;
1493 uint8_t len ;
1494 };
1495
1496 struct {
1497 int tb,idev,odev,proto;
1498 struct I_data rvia, rdst, mdst, rsrc, msrc;
1499 } gfilter;
1500
show_iproute_help(void)1501 static void show_iproute_help(void)
1502 {
1503 error_exit("\n\n" \
1504 "iproute { list | flush } SELECTOR\n" \
1505 "iproute get ADDRESS [from ADDRESS iif STRING]\n" \
1506 " [oif STRING]\n" \
1507 "iproute { add | del | change | append | replace | test } ROUTE\n" \
1508 " SELECTOR := [root PREFIX] [match PREFIX] [proto RTPROTO]\n" \
1509 " ROUTE := [TYPE] PREFIX [proto RTPROTO] [metric METRIC]");
1510 }
1511
print_rta_metrics(char * out,const struct rtattr * mxattr)1512 static void print_rta_metrics(char* out, const struct rtattr *mxattr)
1513 {
1514 int32_t tvar = RTA_PAYLOAD(mxattr);
1515 struct rtattr *rta, *mxrta[RTAX_MAX+1] = {0,};
1516 unsigned int mxlock = 0;
1517 int i;
1518
1519 for (rta = RTA_DATA(mxattr); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1520 if (rta->rta_type <= RTA_MAX) mxrta[rta->rta_type] = rta;
1521
1522 if (mxrta[RTAX_LOCK])
1523 mxlock = *(u_int32_t *)RTA_DATA(mxrta[RTAX_LOCK]);
1524
1525 for (i = 2; i <= RTAX_MAX; i++) {
1526 uint32_t val = 0;
1527
1528 if (mxrta[i] == NULL && !(mxlock & (1 << i)))
1529 continue;
1530
1531 if (mxrta[i] != NULL && i != RTAX_CC_ALGO)
1532 val = *(u_int32_t *)RTA_DATA(mxrta[i]);
1533
1534 if (i == RTAX_HOPLIMIT && (int)val == -1)
1535 continue;
1536
1537 if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
1538 sprintf(out, "%s%s ", out, mx_names[i]);
1539 else
1540 sprintf(out, "%smetric %d ", out, i);
1541
1542 if (mxlock & (1<<i))
1543 sprintf(out, "%slock ", out);
1544
1545 switch (i) {
1546 case RTAX_RTT:
1547 case RTAX_RTTVAR:
1548 case RTAX_RTO_MIN:
1549 if (i == RTAX_RTT)
1550 val /= 8;
1551 else if (i == RTAX_RTTVAR)
1552 val /= 4;
1553
1554 if (val >= 1000)
1555 sprintf(out, "%s%gs ", out, val / 1e3);
1556 else
1557 sprintf(out, "%s%ums ", out, val);
1558 break;
1559
1560 case RTAX_CC_ALGO:
1561 sprintf(out, "%scongestion %s ", out, (const char*)RTA_DATA(mxrta[i]));
1562 break;
1563
1564 default:
1565 sprintf(out, "%s%u ", out, val);
1566 break;
1567 }
1568 }
1569 }
1570
display_route_info(struct nlmsghdr * mhdr,char ** argv)1571 static int display_route_info(struct nlmsghdr *mhdr, char **argv)
1572 {
1573 char *inetval = NULL, out[1024] = {0};
1574 struct rtmsg *msg = NLMSG_DATA(mhdr);
1575 struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1576 int32_t tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1577 int hlen = ((msg->rtm_family == AF_INET) ? 32
1578 : ((msg->rtm_family == AF_INET6) ? 128 : -1));
1579
1580 if (mhdr->nlmsg_type != RTM_NEWROUTE) return 0;
1581 if (msglen < 0) return 1;
1582
1583 if (msg->rtm_family == AF_INET6) {
1584 if (gfilter.tb) {
1585 if (gfilter.tb < 0) {
1586 if (!(msg->rtm_flags & RTM_F_CLONED)) return 0;
1587 } else {
1588 if (msg->rtm_flags & RTM_F_CLONED) return 0;
1589 if (gfilter.tb == RT_TABLE_LOCAL && msg->rtm_type != RTN_LOCAL)
1590 return 0;
1591 else if (gfilter.tb == RT_TABLE_MAIN && msg->rtm_type == RTN_LOCAL)
1592 return 0;
1593 }
1594 }
1595 }
1596 else if (gfilter.tb > 0 && gfilter.tb != msg->rtm_table) return 0;
1597
1598 if (gfilter.proto && (msg->rtm_protocol != gfilter.proto)) return 0;
1599
1600
1601 if (gfilter.rdst.family && (msg->rtm_family != gfilter.rdst.family ||
1602 gfilter.rdst.netmask > msg->rtm_dst_len)) return 0;
1603 if (gfilter.mdst.family && (msg->rtm_family != gfilter.mdst.family
1604 || (gfilter.mdst.netmask < msg->rtm_dst_len))) return 0;
1605 if (gfilter.rsrc.family && (msg->rtm_family != gfilter.rsrc.family
1606 || gfilter.rsrc.netmask > msg->rtm_src_len)) return 0;
1607 if (gfilter.msrc.family && (msg->rtm_family != gfilter.msrc.family
1608 || (gfilter.msrc.netmask < msg->rtm_src_len))) return 0;
1609 tvar = msglen;
1610
1611 for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1612 if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1613
1614 if (msg->rtm_type != RTN_UNICAST)
1615 sprintf(out,"%s%s ", out,rtmtype_idx2str(msg->rtm_type));
1616 if (attr[RTA_DST]) {
1617 inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_DST]),
1618 toybuf, sizeof(toybuf));
1619 if (gfilter.rdst.family &&
1620 memcmp(RTA_DATA(attr[RTA_DST]), &gfilter.rdst.addr, gfilter.rdst.len))
1621 return 0;
1622 if (gfilter.mdst.family &&
1623 memcmp(RTA_DATA(attr[RTA_DST]), &gfilter.mdst.addr, gfilter.mdst.len))
1624 return 0;
1625 sprintf(out,"%s%s",out,inetval);
1626 }
1627 if (msg->rtm_dst_len) sprintf(out,"%s/%d ", out,msg->rtm_dst_len);
1628 else sprintf(out,"%s%s",out,"default ");
1629
1630 if (attr[RTA_SRC]) {
1631 inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_SRC]),
1632 toybuf, sizeof(toybuf));
1633 if (gfilter.rsrc.family &&
1634 memcmp(RTA_DATA(attr[RTA_SRC]), &gfilter.rsrc.addr, gfilter.rsrc.len))
1635 return 0;
1636 if (gfilter.msrc.family &&
1637 memcmp(RTA_DATA(attr[RTA_SRC]), &gfilter.msrc.addr, gfilter.msrc.len))
1638 return 0;
1639 sprintf(out, "%s from %s", out, inetval);
1640 }
1641 if (msg->rtm_src_len) sprintf(out, "%s/%d ", out, msg->rtm_src_len);
1642
1643 if (attr[RTA_GATEWAY]) {
1644 inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_GATEWAY]),
1645 toybuf, sizeof(toybuf));
1646 sprintf(out, "%s via %s ", out, inetval);
1647 }
1648 if (gfilter.rvia.family) {
1649 char tmp[256];
1650
1651 if (!attr[RTA_GATEWAY]) return 0;
1652 if (strcmp((char *)inet_ntop(msg->rtm_family, gfilter.rvia.addr,
1653 tmp, sizeof(tmp)), inetval)) return 0;
1654 }
1655
1656 if (gfilter.odev != 0) if (!attr[RTA_OIF]) return 0;
1657 if (attr[RTA_OIF]) {
1658 if (gfilter.odev !=0 && gfilter.odev != *(int*)RTA_DATA(attr[RTA_OIF]))
1659 return 0;
1660 sprintf(out, "%s dev %s ", out,
1661 if_indextoname(*(int*)RTA_DATA(attr[RTA_OIF]), toybuf));
1662 }
1663
1664 if (attr[RTA_PREFSRC] && hlen) {
1665 inetval = (char *)inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_PREFSRC]),
1666 toybuf, sizeof(toybuf));
1667 sprintf(out, "%s src %s ", out, inetval);
1668 }
1669 if (attr[RTA_PRIORITY])
1670 sprintf(out, "%s metric %d ", out, *(uint32_t*)RTA_DATA(attr[RTA_PRIORITY]));
1671 if (msg->rtm_family == AF_INET6) {
1672 struct rta_cacheinfo *ci = NULL;
1673 if (attr[RTA_CACHEINFO]) ci = RTA_DATA(attr[RTA_CACHEINFO]);
1674 if ((msg->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
1675 if (msg->rtm_flags & RTM_F_CLONED) sprintf(out, "%s%s cache ",
1676 out, (!TT.singleline ? "\n" : " "));
1677 if (ci && ci->rta_expires) {
1678 int hz = 0;
1679 FILE *fp = xfopen("/proc/net/psched","r");
1680
1681 if (fp) {
1682 unsigned int nom, denom;
1683
1684 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
1685 if (nom == 1000000)
1686 hz = denom;
1687 fclose(fp);
1688 }
1689 if (!hz) hz = sysconf(_SC_CLK_TCK);
1690 sprintf(out, "%s expires %dsec", out, ci->rta_expires /hz);
1691 }
1692 if (ci && ci->rta_error) sprintf(out, "%s error %d", out, ci->rta_error);
1693 }
1694 else if (ci && ci->rta_error)
1695 sprintf(out, "%s error %d", out, ci->rta_error);
1696 }
1697 if (attr[RTA_IIF] && !gfilter.idev)
1698 sprintf(out, "%s iif %s", out,
1699 if_indextoname(*(int*)RTA_DATA(attr[RTA_IIF]), toybuf));
1700
1701 if (attr[RTA_METRICS])
1702 print_rta_metrics(out, attr[RTA_METRICS]);
1703
1704 if (TT.flush || (TT.connected && !TT.from_ok))
1705 memcpy(toybuf, (void*)mhdr,mhdr->nlmsg_len);
1706
1707 if (TT.flush) {
1708 int sockfd = 0;
1709 struct nlmsghdr* mhdr = (struct nlmsghdr*)toybuf;
1710 struct rtmsg *msg = NLMSG_DATA(mhdr);
1711 int tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1712 struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1713
1714 tvar = msglen;
1715 for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1716 if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1717
1718 if (msg->rtm_family == AF_INET6
1719 && !msg->rtm_dst_len
1720 && msg->rtm_type == RTN_UNREACHABLE
1721 && attr[RTA_PRIORITY]
1722 && *(int*)RTA_DATA(attr[RTA_PRIORITY]) == -1)
1723 return 0;
1724
1725 mhdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1726 mhdr->nlmsg_type = RTM_DELROUTE;
1727 mhdr->nlmsg_pid = 0;
1728 sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1729 if (send(sockfd , (void*)mhdr, mhdr->nlmsg_len, 0) < 0)
1730 perror_exit("Unable to send data on socket.");
1731
1732 while (1) {
1733 struct nlmsghdr *mhdr;
1734 int msglen = recv(sockfd, toybuf, sizeof(toybuf), 0);
1735
1736 if ((msglen < 0) && (errno == EINTR || errno == EAGAIN)) continue;
1737 else if (msglen < 0) {
1738 error_msg("netlink receive error %s", strerror(errno));
1739 xclose(sockfd);
1740 return 1;
1741 } else if (!msglen) {
1742 error_msg("EOF on netlink");
1743 xclose(sockfd);
1744 return 1;
1745 }
1746
1747 for (mhdr = (struct nlmsghdr*)toybuf; NLMSG_OK(mhdr, msglen);
1748 mhdr = NLMSG_NEXT(mhdr, msglen)) {
1749 switch (mhdr->nlmsg_type) {
1750 case NLMSG_DONE:
1751 xclose(sockfd);
1752 return 0;
1753 case NLMSG_ERROR:
1754 {
1755 struct nlmsgerr *merr = (struct nlmsgerr*)NLMSG_DATA(mhdr);
1756
1757 if (merr->error == 0) { xclose(sockfd); return 0; }
1758 if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
1759 error_msg("ERROR truncated");
1760 else {
1761 errno = -merr->error;
1762 perror_msg("RTNETLINK answers");
1763 }
1764 xclose(sockfd);
1765 return 1;
1766 }
1767 default:
1768 break;
1769 }
1770 } // End of for loop.
1771 } // End of while loop.
1772
1773 xclose(sockfd);
1774 } else printf("%s\n",out);
1775 return 0;
1776 }
1777
route_get(char ** argv)1778 static int route_get(char **argv)
1779 {
1780 int idx, flag;
1781 struct arglist cmd_objectlist[] = {{"from", 0}, {"iif", 1}, {"oif", 2},
1782 {"dev", 3}, {"notify", 4}, {"connected", 5}, {"to", 6}, {NULL, -1}};
1783 char *idev = NULL, *odev = NULL;
1784 struct {
1785 struct nlmsghdr mhdr;
1786 struct rtmsg msg;
1787 char buf[1024];
1788 } request;
1789
1790 memset(&request, 0, sizeof(request));
1791 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
1792 request.mhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1793 request.mhdr.nlmsg_type = RTM_GETROUTE;
1794 request.msg.rtm_family = AF_UNSPEC;
1795
1796 for (; *argv; argv++) {
1797 switch(idx = substring_to_idx(*argv, cmd_objectlist)) {
1798 case 0: TT.from_ok = 1; // dst address
1799 case 6: argv++; //fallthrough
1800 default:
1801 {
1802 uint32_t addr[8] = {0,}, netmask = 0;
1803 uint8_t len = 0;
1804
1805 if (!*argv) error_exit("'%s': Missing Prefix", argv[-1]);
1806 parse_prefix(addr, &netmask, &len, *argv, request.msg.rtm_family);
1807 if (len) request.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
1808 netmask = (request.msg.rtm_family == AF_INET6) ? 128 : 32;
1809 if (!idx) request.msg.rtm_src_len = netmask;
1810 else request.msg.rtm_dst_len = netmask;
1811 add_string_to_rtattr(&request.mhdr, sizeof(request),
1812 (!idx ? RTA_SRC : RTA_DST), addr, len);
1813 break;
1814 }
1815 case 1:
1816 case 2:
1817 case 3:
1818 if (!*++argv) show_iproute_help();
1819 if (idx == 1) idev = *argv, flag = RTA_IIF;
1820 else odev = *argv, flag = RTA_OIF;
1821 idx = get_ifaceindex(*argv, 1);
1822 add_string_to_rtattr(&request.mhdr, sizeof(request),
1823 flag, (char*)&idx, sizeof(idx));
1824 break;
1825 case 4:
1826 request.msg.rtm_flags |= RTM_F_NOTIFY;
1827 break;
1828 case 5:
1829 TT.connected = 1;
1830 break;
1831 }
1832 }
1833 if (!request.msg.rtm_dst_len)
1834 error_exit("need at least destination address");
1835
1836 send_nlmesg(0, 0, 0, &request, sizeof(request));
1837 filter_nlmesg(display_route_info, NULL);
1838
1839 if (TT.connected && !TT.from_ok) {
1840 struct nlmsghdr *mhdr = (struct nlmsghdr*)toybuf;
1841 struct rtmsg *msg = NLMSG_DATA(mhdr);
1842 int tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
1843 struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
1844
1845 if (mhdr->nlmsg_type != RTM_NEWROUTE) error_exit("not a route?");
1846 if (msglen < 0) error_exit("wrong len %d", msglen);
1847
1848 tvar = msglen;
1849 for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
1850 if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
1851
1852 if (attr[RTA_PREFSRC]) {
1853 attr[RTA_PREFSRC]->rta_type = RTA_SRC;
1854 msg->rtm_src_len = 8*RTA_PAYLOAD(attr[RTA_PREFSRC]);
1855 } else if (!attr[RTA_SRC]) error_exit("can't connect the route");
1856
1857 if (!odev && attr[RTA_OIF]) attr[RTA_OIF]->rta_type = 0;
1858 if (attr[RTA_GATEWAY]) attr[RTA_GATEWAY]->rta_type = 0;
1859 if (!idev && attr[RTA_IIF]) attr[RTA_IIF]->rta_type = 0;
1860 mhdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1861 mhdr->nlmsg_type = RTM_GETROUTE;
1862 mhdr->nlmsg_pid = 0;
1863 xclose(TT.sockfd);
1864 TT.sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1865 send_nlmesg(0, 0, 0, mhdr, mhdr->nlmsg_len);
1866 filter_nlmesg(display_route_info, NULL);
1867 }
1868 return 0;
1869 }
1870
route_show_flush(char ** argv)1871 static int route_show_flush(char **argv)
1872 {
1873 struct arglist cmd_objectlist[] = {{"protocol", 0}, {"dev", 1}, {"oif", 2},
1874 {"iif", 3}, {"via", 4}, {"table", 5}, {"cache", 6}, {"from", 7},
1875 {"to", 8}, {"all", 9}, {"root", 10}, {"match", 11}, {"exact", 12},
1876 {"main", 13}, {NULL,-1}};
1877 int family = TT.addressfamily, idx;
1878 struct {
1879 struct nlmsghdr mhdr;
1880 struct rtmsg msg;
1881 } request;
1882
1883 if (*argv[-1] == 'f') TT.flush = 1;
1884 if (TT.flush && !*argv) show_iproute_help();
1885
1886 gfilter.tb = RT_TABLE_MAIN;
1887 for (; *argv; argv++) {
1888 switch (idx = substring_to_idx(*argv, cmd_objectlist)) {
1889 case 0:
1890 if (!*++argv) show_iproute_help();
1891 if ((idx = idxfromRPDB(*argv,RPDB_rtprotos)) < 0)
1892 error_exit("Invalid argument protocol.");
1893 gfilter.proto = idx;
1894 break;
1895 case 1:
1896 case 2:
1897 case 3:
1898 {
1899 if (!*++argv) show_iproute_help();
1900 int dev = get_ifaceindex(*argv, 1);
1901
1902 if (idx == 3) gfilter.idev = dev;
1903 else gfilter.odev = dev;
1904 }
1905 break;
1906 case 4:
1907 if (!*++argv) show_iproute_help();
1908 parse_prefix(gfilter.rvia.addr, &gfilter.rvia.netmask,
1909 &gfilter.rvia.len, *argv, gfilter.rvia.family);
1910 if (gfilter.rvia.len)
1911 gfilter.rvia.family = ((gfilter.rvia.len == 4) ?
1912 AF_INET : AF_INET6);
1913 break;
1914 case 5:
1915 if (!*++argv) show_iproute_help();
1916 idx = substring_to_idx(*argv, cmd_objectlist);
1917 if (idx == 6) gfilter.tb = -1;
1918 else if (idx == 9) gfilter.tb = 0;
1919 else if (idx != 13) {
1920 if ((gfilter.tb = idxfromRPDB(*argv, RPDB_rttables)) < 0)
1921 error_exit("table %s is invalid.", *argv);
1922 }
1923 break;
1924 case 6:
1925 gfilter.tb = -1;
1926 break;
1927 case 7:
1928 if (!*++argv) show_iproute_help();
1929 idx = substring_to_idx(*argv, cmd_objectlist);
1930 if (idx < 0) if (!*++argv) show_iproute_help();
1931 if (idx == 10)
1932 if (!*++argv) show_iproute_help();
1933 parse_prefix(gfilter.rsrc.addr, &gfilter.rsrc.netmask,
1934 &gfilter.rsrc.len, *argv, gfilter.rsrc.family);
1935 if (gfilter.rsrc.len)
1936 gfilter.rsrc.family = ((gfilter.rsrc.len == 4) ?
1937 AF_INET : AF_INET6);
1938 else {
1939 if ((idx == 12 ||idx == 11) && !*++argv) show_iproute_help();
1940 parse_prefix(gfilter.msrc.addr, &gfilter.msrc.netmask,
1941 &gfilter.msrc.len, *argv, gfilter.msrc.family);
1942 if (gfilter.msrc.len)
1943 gfilter.msrc.family = ((gfilter.msrc.len == 4) ?
1944 AF_INET : AF_INET6);
1945 if (idx != 11) gfilter.rsrc = gfilter.msrc;
1946 }
1947 break;
1948 case 8:
1949 idx = substring_to_idx(*argv, cmd_objectlist);
1950 if (idx != -1 && !*++argv) show_iproute_help();
1951 default: // fallthrough
1952 if (idx == 10) {
1953 if (!*++argv) show_iproute_help();
1954 parse_prefix(gfilter.rdst.addr, &gfilter.rdst.netmask,
1955 &gfilter.rdst.len, *argv, gfilter.rdst.family);
1956 if (gfilter.rdst.len)
1957 gfilter.rdst.family = ((gfilter.rdst.len == 4) ?
1958 AF_INET : AF_INET6);
1959 }
1960 else {
1961 if ((idx == 12 ||idx == 11) && !*++argv) show_iproute_help();
1962 parse_prefix(gfilter.mdst.addr, &gfilter.mdst.netmask,
1963 &gfilter.mdst.len, *argv, gfilter.mdst.family);
1964 if (gfilter.mdst.len)
1965 gfilter.mdst.family = ((gfilter.mdst.len == 4) ?
1966 AF_INET : AF_INET6);
1967 if (idx != 11) gfilter.rdst = gfilter.mdst;
1968 }
1969 break;
1970 }
1971 }
1972 if (family == AF_UNSPEC && gfilter.tb) family = AF_INET;
1973
1974 if (TT.flush) {
1975 if (gfilter.tb < 0) { // flush table cache
1976 if (family != AF_INET6) {
1977 FILE *fp = xfopen("/proc/sys/net/ipv4/route/flush", "w");
1978
1979 if (fwrite("-1",1,2,fp) < 2) error_exit("can't flush routing cache");
1980 fclose(fp);
1981 }
1982 if (family == AF_INET) return 0;
1983 }
1984 }
1985
1986 memset(&request, 0, sizeof (request));
1987 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof (struct rtmsg));
1988 request.mhdr.nlmsg_flags = NLM_F_REQUEST;
1989 request.mhdr.nlmsg_flags |= NLM_F_ROOT | NLM_F_MATCH;
1990 request.mhdr.nlmsg_type = RTM_GETROUTE;
1991 request.msg.rtm_family = family;
1992 if (gfilter.tb < 0) request.msg.rtm_flags = RTM_F_CLONED;
1993 send_nlmesg(0, 0, 0, (void*)&request, sizeof (request));
1994 return (filter_nlmesg(display_route_info, NULL));
1995 }
1996
route_update(char ** argv,unsigned int route_flags)1997 static int route_update(char **argv, unsigned int route_flags)
1998 {
1999 char mxbuf[256], *d = NULL;
2000 struct rtattr *mxrta = (void*)mxbuf;
2001 unsigned mxlock = 0, ok = 0;
2002 int idx;
2003 uint32_t addr[8] = {0,}, netmask = 0;
2004 uint8_t len = 0;
2005
2006 struct arglist cmd_objectlist[] = {{"src", 0}, {"via", 1}, {"mtu", 2},
2007 {"lock", 3}, {"protocol", 4}, {"table", 5}, {"dev", 6}, {"oif", 7},
2008 {"to", 8}, {"metric", 9}, {NULL,-1}
2009 };
2010 enum {
2011 gtwy_ok = 1,
2012 dst_ok = 2,
2013 proto_ok = 4,
2014 type_ok = 8
2015 };
2016 struct {
2017 struct nlmsghdr hdr;
2018 struct rtmsg msg;
2019 char buf[1024];
2020 } req;
2021
2022 memset(&req, 0, sizeof(req));
2023 req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2024 req.hdr.nlmsg_flags = NLM_F_ACK| NLM_F_REQUEST | route_flags;
2025 req.hdr.nlmsg_type = TT.route_cmd;
2026 req.msg.rtm_family = AF_UNSPEC;
2027 req.msg.rtm_table = RT_TABLE_MAIN;
2028 req.msg.rtm_scope = RT_SCOPE_NOWHERE;
2029
2030 if (TT.route_cmd != RTM_DELROUTE) {
2031 req.msg.rtm_protocol = RTPROT_BOOT;
2032 req.msg.rtm_scope = RT_SCOPE_UNIVERSE;
2033 req.msg.rtm_type = RTN_UNICAST;
2034 }
2035
2036 mxrta->rta_type = RTA_METRICS;
2037 mxrta->rta_len = RTA_LENGTH(0);
2038
2039 for (; *argv; argv++) {
2040 idx = substring_to_idx(*argv, cmd_objectlist);
2041 if (!idx) {
2042 if (!*++argv) show_iproute_help();
2043 parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2044 if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2045 add_string_to_rtattr(&req.hdr, sizeof(req), RTA_PREFSRC, addr, len);
2046 } else if (idx == 1) {
2047 ok |= gtwy_ok;
2048 if (!*++argv) show_iproute_help();
2049 parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2050 if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2051 add_string_to_rtattr(&req.hdr, sizeof(req),RTA_GATEWAY, addr, len);
2052 } else if (idx == 2) {
2053 if (!*++argv) show_iproute_help();
2054 if (substring_to_idx(*argv, cmd_objectlist ) == 3) {
2055 mxlock |= (1 << RTAX_MTU);
2056 if (!*++argv) show_iproute_help();
2057 }
2058 idx = atolx(*argv);
2059 add_uint32_rtattr_to_buffer(mxrta, sizeof(mxbuf), RTAX_MTU, idx);
2060 } else if (idx == 4) {
2061 if (!*++argv) show_iproute_help();
2062 if ((idx = idxfromRPDB(*argv,RPDB_rtprotos)) < 0)
2063 error_exit("Invalid argument protocol %s.",*argv);
2064 req.msg.rtm_protocol = idx;
2065 ok |= proto_ok;
2066 } else if (idx == 5) {
2067 if (!*++argv) show_iproute_help();
2068 req.msg.rtm_table = idxfromRPDB(*argv, RPDB_rttables);
2069 } else if (idx == 6 || idx == 7) {
2070 if (!*++argv) show_iproute_help();
2071 d = *argv;
2072 } else if (idx == 9) {
2073 unsigned long metric;
2074 unsigned int res;
2075 char* ptr;
2076 if (!*++argv) show_iproute_help();
2077 metric = strtoul(*argv, &ptr, 0);
2078 if (!(!*ptr && metric <= 0xFFFFFFFFUL))
2079 error_exit("Invalid argument metric %s.",*argv);
2080 else
2081 res = metric;
2082 add_string_to_rtattr(&req.hdr, sizeof(req),
2083 RTA_PRIORITY, (char*)&res, sizeof(res));
2084 } else {
2085 if (idx == 8)
2086 if (!*++argv) show_iproute_help();
2087 idx = substring_to_idx(*argv,rtmtypes);
2088 if (idx != -1) {
2089 if (!*++argv) show_iproute_help();
2090 req.msg.rtm_type = idx;
2091 ok |= type_ok;
2092 }
2093 if (ok & dst_ok) error_exit("Duplicate argument 'to'");
2094 parse_prefix(addr, &netmask, &len, *argv, req.msg.rtm_family);
2095 if (len) req.msg.rtm_family = ((len == 4) ? AF_INET : AF_INET6);
2096 req.msg.rtm_dst_len = netmask;
2097 ok |= dst_ok;
2098 if (len) add_string_to_rtattr(&req.hdr, sizeof(req),RTA_DST, addr, len);
2099 }
2100 }
2101
2102 if (d) {
2103 idx = get_ifaceindex(d,1);
2104 add_string_to_rtattr(&req.hdr, sizeof(req),
2105 RTA_OIF, (char*)&idx, sizeof(idx));
2106 }
2107 if (mxrta->rta_len > RTA_LENGTH(0)) {
2108 if (mxlock)
2109 add_uint32_rtattr_to_buffer(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
2110 add_string_to_rtattr(&req.hdr, sizeof(req),
2111 RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
2112 }
2113
2114 if (req.msg.rtm_type == RTN_LOCAL || req.msg.rtm_type == RTN_NAT)
2115 req.msg.rtm_scope = RT_SCOPE_HOST;
2116 else if (req.msg.rtm_type == RTN_BROADCAST||req.msg.rtm_type == RTN_MULTICAST
2117 || req.msg.rtm_type == RTN_ANYCAST)
2118 req.msg.rtm_scope = RT_SCOPE_LINK;
2119 else if (req.msg.rtm_type == RTN_UNICAST || req.msg.rtm_type == RTN_UNSPEC) {
2120 if (TT.route_cmd == RTM_DELROUTE)
2121 req.msg.rtm_scope = RT_SCOPE_NOWHERE;
2122 else if (!(ok & gtwy_ok))
2123 req.msg.rtm_scope = RT_SCOPE_LINK;
2124 }
2125 if (req.msg.rtm_family == AF_UNSPEC) req.msg.rtm_family = AF_INET;
2126 send_nlmesg(0, 0, 0, &req, sizeof(req));
2127 filter_nlmesg(NULL, NULL);
2128 return 0;
2129 }
2130
iproute(char ** argv)2131 static int iproute(char **argv)
2132 {
2133 int idx = 1;
2134 struct arglist cmd_objectlist1[] = {{"add", 0}, {"append", 1},{"change", 2},
2135 {"chg", 3},{"delete",4}, {"get", 5}, {"list", 6}, {"show", 7},
2136 {"prepend", 8},{"replace", 9},{"test", 10}, {"flush", 11},{NULL,-1}};
2137
2138 TT.route_cmd = RTM_NEWROUTE;
2139 switch (idx = substring_to_idx(*argv , cmd_objectlist1)) {
2140 case 0: // add
2141 return route_update(++argv , NLM_F_CREATE|NLM_F_EXCL);
2142 case 1: // append
2143 return route_update(++argv , NLM_F_CREATE|NLM_F_APPEND);
2144 case 2: // change
2145 case 3: // chg
2146 return route_update(++argv , NLM_F_REPLACE);
2147 case 4: // delete
2148 TT.route_cmd = RTM_DELROUTE;
2149 return route_update(++argv , RTM_DELROUTE);
2150 case 5:
2151 return route_get(++argv);
2152 case 6:
2153 case 7:
2154 return route_show_flush(++argv);
2155 case 8: // prepend
2156 return route_update(++argv , NLM_F_CREATE);
2157 case 9: // replace
2158 return route_update(++argv , NLM_F_CREATE|NLM_F_REPLACE);
2159 case 10: // test
2160 return route_update(++argv , NLM_F_EXCL);
2161 case 11: // flush
2162 return route_show_flush(++argv);
2163 default:
2164 if (!*argv) return route_show_flush(argv);
2165 else show_iproute_help();
2166 }
2167 return 0; // non reachable code.
2168 }
2169
2170
2171 // ===========================================================================
2172 // code for ip rule.
2173 // ===========================================================================
show_iprule_help(void)2174 static void show_iprule_help(void)
2175 {
2176 error_exit("usage: ip rule [ list | add | del ] SELECTOR ACTION\n"
2177 "SELECTOR := [ from PREFIX ] [ to PREFIX ] [pref NUMBER] [ tos TOS ]\n"
2178 " [ fwmark FWMARK] [ dev/iif STRING ] [type TYPE]\n"
2179 "ACTION := [ table TABLE_ID ] [ realms [SRCREALM/]DSTREALM ]");
2180 }
2181
ruleupdate(char ** argv)2182 static int ruleupdate(char **argv)
2183 {
2184 int8_t idx, tflag = 0, opt = (*argv[-1] == 'a') ? RTM_NEWRULE : RTM_DELRULE;
2185 struct arglist options[] = {{"from", 0}, {"to", 1}, {"preference", 2},
2186 {"order", 2}, {"priority", 2}, {"tos", 3}, {"dsfield", 3}, {"fwmark", 4},
2187 {"realms", 5}, {"table", 6}, {"lookup", 6}, {"dev", 7}, {"iif", 7},
2188 {"nat", 8}, {"map-to", 8}, {"type", 9}, {"help", 10}, {NULL, -1}};
2189 struct {
2190 struct nlmsghdr mhdr;
2191 struct rtmsg msg;
2192 char buf[1024];
2193 } request;
2194
2195 memset(&request, 0, sizeof(request));
2196 request.mhdr.nlmsg_type = opt;
2197 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
2198 request.mhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK |
2199 ((opt == RTM_DELRULE) ? 0 : NLM_F_CREATE | NLM_F_EXCL);
2200 request.msg.rtm_family = TT.addressfamily;
2201 request.msg.rtm_protocol = RTPROT_BOOT;
2202 request.msg.rtm_scope = RT_SCOPE_UNIVERSE;
2203 request.msg.rtm_table = 0;
2204 request.msg.rtm_type = ((opt == RTM_DELRULE) ? RTN_UNSPEC : RTN_UNICAST);
2205
2206 for (; *argv; argv++) {
2207 switch ((idx = substring_to_idx(*argv, options))) {
2208 case 0:
2209 case 1:
2210 { // e.g. from IP/Netmask and to IP/Netmask.
2211 uint32_t addr[4] = {0,}, netmask = 0;
2212 uint8_t len = 0, *tmp;
2213
2214 if (!*++argv) error_exit("'%s': Missing Prefix", argv[-1]);
2215 parse_prefix(addr, &netmask, &len, *argv, request.msg.rtm_family);
2216
2217 tmp = idx ? &request.msg.rtm_dst_len : &request.msg.rtm_src_len;
2218 if (!netmask) *tmp = 0;
2219 else *tmp = netmask;
2220
2221 add_string_to_rtattr(&request.mhdr, sizeof(request),
2222 (idx ? RTA_DST : RTA_SRC), addr, len);
2223 }
2224 break;
2225 case 2:
2226 case 4:
2227 { // e.g. Preference p# and fwmark MARK
2228 uint32_t pref;
2229 char *ptr;
2230
2231 if (!*++argv)
2232 error_exit("Missing %s", (idx == 2) ? "Preference" : "fwmark");
2233 pref = strtoul(*argv, &ptr, 0);
2234 if (!ptr || (ptr == *argv) || *ptr || pref > 0xFFFFFFFFUL)
2235 error_exit("Invalid %s", (idx == 2) ? "Preference" : "fwmark");
2236 add_string_to_rtattr(&request.mhdr, sizeof(request),
2237 ((idx == 2) ? RTA_PRIORITY : RTA_PROTOINFO),
2238 (void *)&pref, sizeof(uint32_t));
2239 }
2240 break;
2241 case 3:
2242 {
2243 uint32_t tos;
2244 if (!*++argv) error_exit("Missing TOS key");
2245 if ((tos = idxfromRPDB(*argv, RPDB_rtdsfield)) < 0)
2246 error_exit("Invalid TOS");
2247 request.msg.rtm_tos = tos;
2248 }
2249 break;
2250 case 5:
2251 { // e.g. realms FROM_realm/TO_realm
2252 uint32_t realms = 0;
2253 int ret;
2254 char *ptr;
2255
2256 if (!*++argv) error_exit("Missing REALMSID");
2257 if ((ptr = strchr(*argv, '/'))) {
2258 *ptr = 0;
2259 if ((ret = idxfromRPDB(*argv, RPDB_rtrealms)) < 0)
2260 error_exit("Invalid realms");
2261 realms = ret;
2262 realms <<= 16;
2263 *ptr++ = '/';
2264 } else ptr = *argv;
2265 if ((ret = idxfromRPDB(ptr, RPDB_rtrealms)) < 0)
2266 error_exit("Invalid realms");
2267 realms |= ret;
2268 add_string_to_rtattr(&request.mhdr, sizeof(request),
2269 RTA_FLOW, (void *)&realms, sizeof(uint32_t));
2270 }
2271 break;
2272 case 6:
2273 { // e.g. table tid/tableName
2274 int tid;
2275 if (!*++argv) error_exit("Missing TableID");
2276 if ((tid = idxfromRPDB(*argv, RPDB_rttables)) < 0)
2277 error_exit("Invalid TID");
2278 request.msg.rtm_table = tid;
2279 tflag = 1;
2280 }
2281 break;
2282 case 7:
2283 {
2284 if (!*++argv) error_exit("Missing dev/iif NAME");
2285 add_string_to_rtattr(&request.mhdr, sizeof(request),
2286 RTA_IIF, *argv, strlen(*argv)+1);
2287 }
2288 break;
2289 case 8:
2290 {
2291 uint32_t addr[4] = {0,};
2292 uint8_t af = AF_UNSPEC;
2293
2294 if (!*++argv) error_exit("Missing nat/map-to ADDRESS");
2295 if (get_prefix(addr, &af /* Un-used variable */, *argv, AF_INET))
2296 error_exit("Invalid mapping Address");
2297
2298 add_string_to_rtattr(&request.mhdr, sizeof(request),
2299 RTA_GATEWAY, addr, sizeof(uint32_t));
2300 request.msg.rtm_type = RTN_NAT;
2301 }
2302 break;
2303 case 9:
2304 {
2305 if (!*++argv) error_exit("TYPE Missing");
2306 request.msg.rtm_type = rtmtype_str2idx(*argv);
2307 }
2308 break;
2309 case 10:
2310 show_iprule_help();
2311 break; // Unreachable code.
2312 default:
2313 error_exit("Invalid argument '%s'", *argv);
2314 break; // Unreachable code.
2315 }
2316 }
2317
2318 if (!request.msg.rtm_family) request.msg.rtm_family = AF_INET;
2319 if (!tflag && opt == RTM_NEWRULE) request.msg.rtm_table = RT_TABLE_MAIN;
2320
2321 send_nlmesg(0, 0, 0, &request, sizeof(request));
2322 return (filter_nlmesg(NULL, NULL));
2323 }
2324
show_rules(struct nlmsghdr * mhdr,char ** argv)2325 static int show_rules(struct nlmsghdr *mhdr,
2326 char **argv __attribute__ ((__unused__)))
2327 {
2328 struct rtmsg *msg = NLMSG_DATA(mhdr);
2329 struct rtattr *rta, *attr[RTA_MAX+1] = {0,};
2330 int32_t tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
2331 int hlen = ((msg->rtm_family == AF_INET) ? 32
2332 : ((msg->rtm_family == AF_INET6) ? 128 : -1));
2333
2334 if (mhdr->nlmsg_type != RTM_NEWRULE) return 0;
2335 if (msglen < 0) return 1;
2336
2337 tvar = msglen;
2338 for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar))
2339 if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta;
2340
2341 if (tvar) error_msg("deficit %d, rtalen = %d!", tvar, rta->rta_len);
2342
2343 printf("%u:\tfrom ", attr[RTA_PRIORITY] ?
2344 *(unsigned *)RTA_DATA(attr[RTA_PRIORITY]) : 0);
2345
2346 if (attr[RTA_SRC]) {
2347 printf("%s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6)
2348 ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_SRC]),
2349 toybuf, sizeof(toybuf))
2350 : "???");
2351 (msg->rtm_src_len != hlen) ? printf("/%u", msg->rtm_src_len) : 0;
2352 } else msg->rtm_src_len ? printf("0/%d", msg->rtm_src_len) : printf("all");
2353
2354 xputc(' ');
2355 if (attr[RTA_DST]) {
2356 printf("to %s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6)
2357 ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_DST]),
2358 toybuf, sizeof(toybuf)) : "???");
2359 (msg->rtm_dst_len != hlen) ? printf("/%u", msg->rtm_dst_len) : xputc(' ');
2360 } else if (msg->rtm_dst_len)
2361 printf("to 0/%d ", msg->rtm_dst_len);
2362
2363 if (msg->rtm_tos)
2364 printf("tos %s ", namefromRPDB(msg->rtm_tos, RPDB_rtdsfield));
2365
2366 if (attr[RTA_PROTOINFO])
2367 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(attr[RTA_PROTOINFO]));
2368
2369 if (attr[RTA_IIF]) printf("iif %s ", (char*)RTA_DATA(attr[RTA_IIF]));
2370
2371 if (msg->rtm_table)
2372 printf("lookup %s ", namefromRPDB(msg->rtm_table, RPDB_rttables));
2373
2374 if (attr[RTA_FLOW]) {
2375 u_int32_t from, to = *(u_int32_t *)RTA_DATA(attr[RTA_FLOW]);
2376 char *format = "realms %s/";
2377
2378 to = (from = (to >> 16)) & 0xFFFF;
2379 format = (from ? format: "%s");
2380 printf(format, namefromRPDB((from ? from : to), RPDB_rtrealms));
2381 }
2382
2383 if (msg->rtm_type == RTN_NAT) {
2384 if (!attr[RTA_GATEWAY]) printf("masquerade");
2385 else printf("map-to %s ", inet_ntop(msg->rtm_family,
2386 RTA_DATA(attr[RTA_GATEWAY]), toybuf, sizeof(toybuf)));
2387 } else if (msg->rtm_type != RTN_UNICAST)
2388 printf("%s", rtmtype_idx2str(msg->rtm_type));
2389
2390 xputc('\n');
2391 return 0;
2392 }
2393
rulelist(char ** argv)2394 static int rulelist(char **argv)
2395 {
2396 if (*argv) {
2397 error_msg("'ip rule show' does not take any arguments.");
2398 return 1;
2399 }
2400 send_nlmesg(RTM_GETRULE, NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST,
2401 ((TT.addressfamily != AF_UNSPEC) ? TT.addressfamily : AF_INET), NULL, 0);
2402 return filter_nlmesg(show_rules, argv);
2403 }
2404
iprule(char ** argv)2405 static int iprule(char **argv)
2406 {
2407 int idx;
2408 struct arglist options[] = {{"add", 0}, {"delete", 0}, {"list", 1},
2409 {"show", 1}, {NULL, -1}};
2410 cmdobj ipcmd, cmdobjlist[] = {ruleupdate, rulelist};
2411
2412 if (!*argv) idx = 1;
2413 else if ((idx = substring_to_idx(*argv++, options)) == -1)
2414 show_iprule_help();
2415 ipcmd = cmdobjlist[idx];
2416 return ipcmd(argv);
2417 }
2418 //============================================================================
2419 // code for ip tunnel.
2420 //============================================================================
show_iptunnel_help(void)2421 static void show_iptunnel_help(void)
2422 {
2423 error_exit("usage: iptunnel { add | change | del | show } [NAME]\n"
2424 " [mode { ipip | gre | sit }] [remote ADDR] [local ADDR]\n"
2425 " [[i|o]seq] [[i|o]key KEY] [[i|o]csum] [ttl TTL]\n"
2426 " [tos TOS] [[no]pmtudisc] [dev PHYS_DEV]");
2427 }
2428
tnl_ioctl(char * dev,int rtype,struct ip_tunnel_parm * ptnl)2429 static int tnl_ioctl(char *dev, int rtype, struct ip_tunnel_parm *ptnl)
2430 {
2431 struct ifreq req;
2432 int fd, ret = 0;
2433
2434 if ((rtype == SIOCCHGTUNNEL || rtype == SIOCDELTUNNEL) && *ptnl->name)
2435 xstrncpy(req.ifr_name, ptnl->name, IF_NAMESIZE);
2436 else xstrncpy(req.ifr_name, dev, IF_NAMESIZE);
2437
2438 if (rtype != SIOCGIFHWADDR) req.ifr_ifru.ifru_data = (void*)ptnl;
2439 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2440
2441 if (rtype == SIOCGETTUNNEL) ret = ioctl(fd, rtype, &req);
2442 else if (rtype == SIOCGIFHWADDR)
2443 ret = (ioctl(fd, rtype, &req) < 0) ? -1 : req.ifr_addr.sa_family;
2444 else xioctl(fd, rtype, &req);
2445
2446 close(fd);
2447 return ret;
2448 }
2449
display_tunnel(struct ip_tunnel_parm * ptnl)2450 static int display_tunnel(struct ip_tunnel_parm *ptnl)
2451 {
2452 char rmt_addr[64], lcl_addr[64], ikey_str[64], okey_str[64];
2453
2454 printf("%s: %s/ip", ptnl->name, ptnl->iph.protocol == IPPROTO_IPIP ? "ip" :
2455 (ptnl->iph.protocol == IPPROTO_GRE ? "gre" :
2456 (ptnl->iph.protocol == IPPROTO_IPV6 ? "ipv6" : "unknown")));
2457 printf(" remote %s local %s ", ptnl->iph.daddr ?
2458 inet_ntop(AF_INET, &ptnl->iph.daddr, rmt_addr, sizeof(rmt_addr)) : "any",
2459 ptnl->iph.saddr ? inet_ntop(AF_INET, &ptnl->iph.saddr, lcl_addr,
2460 sizeof(lcl_addr)) : "any");
2461 if (ptnl->link) {
2462 struct ifreq req;
2463 int fd;
2464
2465 req.ifr_ifindex = ptnl->link;
2466 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2467 if (ioctl(fd, SIOCGIFNAME, &req) < 0) perror_msg("SIOCGIFNAME");
2468 else printf(" dev %s ", req.ifr_name);
2469 close(fd);
2470 }
2471 if (ptnl->iph.ttl) printf(" ttl %d ", ptnl->iph.ttl);
2472 else printf(" ttl inherit ");
2473
2474 if (ptnl->iph.tos) {
2475 printf(" tos");
2476 if (ptnl->iph.tos & 1) printf(" inherit");
2477 if (ptnl->iph.tos & ~1) printf("%c%s ", ptnl->iph.tos & 1 ? '/' : ' ',
2478 namefromRPDB((ptnl->iph.tos & ~1), RPDB_rtdsfield));
2479 }
2480 if (!(ptnl->iph.frag_off & htons(IP_DF))) printf(" nopmtudisc");
2481 inet_ntop(AF_INET, &ptnl->i_key, ikey_str, sizeof(ikey_str));
2482 if ((ptnl->i_flags & GRE_KEY) && (ptnl->o_flags & GRE_KEY)
2483 && ptnl->o_key == ptnl->i_key) printf(" key %s", ikey_str);
2484 else if ((ptnl->i_flags | ptnl->o_flags) & GRE_KEY) {
2485 inet_ntop(AF_INET, &ptnl->o_key, okey_str, sizeof(okey_str));
2486 if (ptnl->i_flags & GRE_KEY) printf(" ikey %s ", ikey_str);
2487 if (ptnl->o_flags & GRE_KEY) printf(" okey %s ", okey_str);
2488 }
2489 if (ptnl->i_flags & GRE_SEQ) printf("\n Drop packets out of sequence.\n");
2490 if (ptnl->i_flags & GRE_CSUM)
2491 printf("\n Checksum in received packet is required.");
2492 if (ptnl->o_flags & GRE_SEQ) printf("\n Sequence packets on output.");
2493 if (ptnl->o_flags & GRE_CSUM) printf("\n Checksum output packets.");
2494 xputc('\n');
2495 return 0;
2496 }
2497
read_tunnel(struct ip_tunnel_parm * ptnl)2498 static int read_tunnel(struct ip_tunnel_parm *ptnl)
2499 {
2500 int count = 0;
2501 char iface[IF_NAMESIZE];
2502 struct ip_tunnel_parm iptnl;
2503 FILE *fp = xfopen("/proc/net/dev", "r");
2504
2505 while (fgets(toybuf, sizeof(toybuf), fp)) {
2506 char *ptr;
2507 int ret;
2508
2509 if (count++ < 2) continue; // 1st two lines are header.
2510
2511 ptr = strchr(toybuf, ':');
2512 if (!ptr || (*ptr++ = 0, sscanf(toybuf, "%s", iface) != 1))
2513 error_exit("invalid format of '/proc/net/dev'");
2514 if (*ptnl->name && strcmp(ptnl->name, iface)) continue;
2515 if ((ret = tnl_ioctl(iface, SIOCGIFHWADDR, &iptnl)) < 0) {
2516 error_msg("failed to get type of '%s'", iface);
2517 continue;
2518 }
2519 if (ret != ARPHRD_TUNNEL && ret != ARPHRD_SIT &&
2520 ret != ARPHRD_IPGRE) continue;
2521
2522 memset(&iptnl, 0, sizeof(iptnl));
2523 if (tnl_ioctl(iface, SIOCGETTUNNEL, &iptnl) < 0) continue;
2524 if ((ptnl->link && iptnl.link != ptnl->link) || (*ptnl->name &&
2525 strcmp(iptnl.name, ptnl->name)) || (ptnl->iph.daddr &&
2526 iptnl.iph.daddr != ptnl->iph.daddr) || (ptnl->iph.saddr &&
2527 iptnl.iph.saddr != ptnl->iph.saddr) || (ptnl->i_key &&
2528 iptnl.i_key != ptnl->i_key)) continue;
2529 display_tunnel(&iptnl);
2530 }
2531 fclose(fp);
2532 return 0;
2533 }
2534
parse_iptunnel_args(struct ip_tunnel_parm * ptnl,char ** argv,int ipt_opt_idx)2535 static void parse_iptunnel_args(struct ip_tunnel_parm *ptnl, char **argv,
2536 int ipt_opt_idx)
2537 {
2538 int idx;
2539 uint8_t af = AF_INET;
2540 uint32_t addr = 0;
2541 struct arglist opts[] = { {"mode", 0}, {"key", 1}, {"ikey", 2},
2542 {"okey", 3}, {"seq", 4}, {"iseq", 5}, {"oseq", 6}, {"csum", 7},
2543 {"icsum", 8}, {"ocsum", 9}, {"nopmtudisc", 10}, {"pmtudisc", 11},
2544 {"remote", 12}, {"local", 13},{"dev", 14}, {"ttl", 15}, {"tos", 16},
2545 {"dsfield", 17}, {"name", 18}, {NULL, -1}
2546 };
2547
2548 ptnl->iph.version = 4; // The value indicates the version of IP (4 or 6)
2549 ptnl->iph.ihl = 5; // Minimum Internet Header Length
2550 // frag_off is measured in units of 8 octets (64 bits)
2551 ptnl->iph.frag_off = htons(IP_DF);
2552 if (*argv && ipt_opt_idx <= 2 && string_to_idx(*argv, opts) == -1) {
2553 xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2554 if (ipt_opt_idx == 1) {
2555 struct ip_tunnel_parm iptnl_old;
2556
2557 memset(&iptnl_old, 0, sizeof(iptnl_old));
2558 tnl_ioctl(ptnl->name, SIOCGETTUNNEL, &iptnl_old);
2559 *ptnl = iptnl_old;
2560 }
2561 argv++;
2562 }
2563 for (; *argv; argv++, addr = 0) {
2564 switch (idx = string_to_idx(*argv, opts)) {
2565 case 0:
2566 if (!*++argv) error_exit("mode is missing");
2567 if ((!strcmp("ipip", *argv) || !strcmp("ip/ip", *argv)))
2568 ptnl->iph.protocol = IPPROTO_IPIP;
2569 else if ((!strcmp("gre", *argv) || !strcmp("gre/ip", *argv)))
2570 ptnl->iph.protocol = IPPROTO_GRE;
2571 else if ((!strcmp("sit", *argv) || !strcmp("ipv6/ip", *argv)))
2572 ptnl->iph.protocol = IPPROTO_IPV6;
2573 else show_iptunnel_help();
2574 break;
2575 case 1:
2576 case 2:
2577 case 3:
2578 {
2579 struct addrinfo *info, hint;
2580 int ret;
2581
2582 if (!*++argv) error_exit("key value is missing");
2583 memset(&hint, 0, sizeof(hint));
2584 hint.ai_family = AF_INET;
2585 ret = getaddrinfo(*argv, NULL, &hint, &info);
2586 if (ret || !info) error_exit("invalid argument to key");
2587 freeaddrinfo(info);
2588
2589 if (strchr(*argv, '.')) {
2590 if (get_prefix(&addr, &af, *argv, AF_INET))
2591 error_exit("invalid key '%s'", *argv);
2592 } else {
2593 unsigned key_val;
2594
2595 sscanf(*argv, "%u", &key_val);
2596 addr = htonl(key_val);
2597 }
2598 if (idx == 1) {
2599 ptnl->i_flags |= GRE_KEY;
2600 ptnl->o_flags |= GRE_KEY;
2601 ptnl->i_key = ptnl->o_key = addr;
2602 } else if (idx == 2) {
2603 ptnl->i_flags |= GRE_KEY;
2604 ptnl->i_key = addr;
2605 } else {
2606 ptnl->o_flags |= GRE_KEY;
2607 ptnl->o_key = addr;
2608 }
2609 }
2610 break;
2611 case 4:
2612 ptnl->i_flags |= GRE_SEQ;
2613 ptnl->o_flags |= GRE_SEQ;
2614 break;
2615 case 5:
2616 ptnl->i_flags |= GRE_SEQ;
2617 break;
2618 case 6:
2619 ptnl->o_flags |= GRE_SEQ;
2620 break;
2621 case 7:
2622 ptnl->i_flags |= GRE_CSUM;
2623 ptnl->o_flags |= GRE_CSUM;
2624 break;
2625 case 8:
2626 ptnl->i_flags |= GRE_CSUM;
2627 break;
2628 case 9:
2629 ptnl->o_flags |= GRE_CSUM;
2630 break;
2631 case 10:
2632 ptnl->iph.frag_off = 0;
2633 break;
2634 case 11:
2635 ptnl->iph.frag_off = htons(IP_DF);
2636 break;
2637 case 12:
2638 case 13:
2639 if (!*++argv) error_exit("remote/local address is missing");
2640 if (get_prefix(&addr, &af, *argv, AF_INET))
2641 error_exit("invalid remote/local address '%s'", *argv);
2642 (idx == 12) ? (ptnl->iph.daddr = addr) : (ptnl->iph.saddr = addr);
2643 break;
2644 case 14:
2645 if (!*++argv) error_exit("device name is missing");
2646 else {
2647 struct ifreq req;
2648 int fd;
2649
2650 xstrncpy(req.ifr_name, *argv, IFNAMSIZ);
2651 fd = xsocket(AF_INET, SOCK_DGRAM, 0);
2652 xioctl(fd, SIOCGIFINDEX, &req);
2653 close(fd);
2654 ptnl->link = req.ifr_ifindex;
2655 }
2656 break;
2657 case 15:
2658 if (!*++argv) error_exit("ttl value is missing");
2659 if (strcmp(*argv, "inherit"))
2660 ptnl->iph.ttl = atolx_range(*argv, 0, 255);
2661 break;
2662 case 16:
2663 case 17:
2664 if (!*++argv) error_exit("tos value is missing");
2665 if (strcmp(*argv, "inherit")) {
2666 char *ptr;
2667 unsigned long tval = strtoul(*argv, &ptr, 16);
2668
2669 if (tval > 255) error_exit("invalid tos value '%s'", *argv);
2670 if (*ptr) {
2671 int ret;
2672
2673 if ((ret = idxfromRPDB(*argv, RPDB_rtdsfield)) < 0)
2674 error_exit("invalid tos value");
2675 ptnl->iph.tos = ret;
2676 } else ptnl->iph.tos = tval;
2677 } else ptnl->iph.tos = 1;
2678 break;
2679 case 18:
2680 if (*ptnl->name) error_exit("invalid tunnel");
2681 else {
2682 if (!*++argv) error_exit("name is missing");
2683 xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2684 }
2685 break;
2686 default:
2687 if (*ptnl->name) error_exit("invalid tunnel");
2688 xstrncpy(ptnl->name, *argv, IF_NAMESIZE);
2689 break;
2690 }
2691 }
2692 if (ptnl->iph.protocol == IPPROTO_IPIP ||
2693 ptnl->iph.protocol == IPPROTO_IPV6) {
2694 if ((ptnl->i_flags & GRE_KEY) || (ptnl->o_flags & GRE_KEY))
2695 error_exit("[i|o]key is allowed with gre only");
2696 if ((ptnl->i_flags & GRE_SEQ) || (ptnl->o_flags & GRE_SEQ))
2697 error_exit("[i|o]seq is allowed with gre only");
2698 if ((ptnl->i_flags & GRE_CSUM) || (ptnl->o_flags & GRE_CSUM))
2699 error_exit("[i|o]csum is allowed with gre only");
2700 }
2701 if (!ptnl->i_key && IN_MULTICAST(ntohl(ptnl->iph.daddr))) {
2702 ptnl->i_key = ptnl->iph.daddr;
2703 ptnl->i_flags |= GRE_KEY;
2704 }
2705 if (!ptnl->o_key && IN_MULTICAST(ntohl(ptnl->iph.daddr))) {
2706 ptnl->o_key = ptnl->iph.daddr;
2707 ptnl->o_flags |= GRE_KEY;
2708 }
2709 if (IN_MULTICAST(ntohl(ptnl->iph.daddr)) && !ptnl->iph.saddr)
2710 error_exit("broadcast tunnel requires a source address");
2711 }
2712
tunnellist(char ** argv)2713 static int tunnellist(char **argv)
2714 {
2715 struct ip_tunnel_parm iptnl;
2716 int ret = 0;
2717
2718 memset(&iptnl, 0, sizeof(iptnl));
2719 parse_iptunnel_args(&iptnl, argv, 3);
2720
2721 if (iptnl.iph.protocol == IPPROTO_IPIP)
2722 ret = tnl_ioctl(*iptnl.name ? iptnl.name : "tunl0", SIOCGETTUNNEL, &iptnl);
2723 else if (iptnl.iph.protocol == IPPROTO_GRE)
2724 ret = tnl_ioctl(*iptnl.name ? iptnl.name : "gre0", SIOCGETTUNNEL, &iptnl);
2725 else if (iptnl.iph.protocol == IPPROTO_IPV6)
2726 ret = tnl_ioctl(*iptnl.name ? iptnl.name : "sit0", SIOCGETTUNNEL, &iptnl);
2727 else return read_tunnel(&iptnl);
2728
2729 if (ret < 0) {
2730 perror_msg("SIOCGETTUNNEL");
2731 return ret;
2732 } else return display_tunnel(&iptnl);
2733 }
2734
2735 // Performing add, change, & delete tunnel action, according to passed req_type
tunnelupdate(char ** argv)2736 static int tunnelupdate(char **argv)
2737 {
2738 struct ip_tunnel_parm iptnl;
2739 int idx = 2, rtype = SIOCDELTUNNEL;
2740
2741 if (*argv[-1] == 'a') {
2742 idx = 0;
2743 rtype = SIOCADDTUNNEL;
2744 } else if (*argv[-1] == 'c') {
2745 idx = 1;
2746 rtype = SIOCCHGTUNNEL;
2747 }
2748
2749 memset(&iptnl, 0, sizeof(iptnl));
2750 parse_iptunnel_args(&iptnl, argv, idx);
2751 if (idx != 2 && iptnl.iph.ttl && !(iptnl.iph.frag_off))
2752 error_exit("ttl > 0 and nopmtudisc are incompatible");
2753 if (iptnl.iph.protocol == IPPROTO_IPIP)
2754 return (tnl_ioctl("tunl0", rtype, &iptnl) < 0) ? 1 : 0;
2755 else if (iptnl.iph.protocol == IPPROTO_GRE)
2756 return (tnl_ioctl("gre0", rtype, &iptnl) < 0) ? 1 : 0;
2757 else if (iptnl.iph.protocol == IPPROTO_IPV6)
2758 return (tnl_ioctl("sit0", rtype, &iptnl) < 0) ? 1 : 0;
2759 else {
2760 if (idx != 2) error_exit("invalid tunnel mode");
2761 return (tnl_ioctl(iptnl.name, rtype, &iptnl) < 0) ? 1 : 0;
2762 }
2763 }
2764
iptunnel(char ** argv)2765 static int iptunnel(char **argv)
2766 {
2767 int idx;
2768 struct arglist opts[] = {{"add", 0}, {"change", 0}, {"del", 0},
2769 {"delete", 0}, {"show", 1}, {"list", 1}, {"lst", 1}, {NULL, -1}
2770 };
2771 cmdobj ipcmd, cmdobjlist[] = {tunnelupdate, tunnellist};
2772
2773 if (!*argv) idx = 1;
2774 else if ((idx = substring_to_idx(*argv++, opts)) == -1)
2775 show_iptunnel_help();
2776 ipcmd = cmdobjlist[idx];
2777 return ipcmd(argv);
2778 }
2779
2780 // ===========================================================================
2781 // Common code, which is used for all ip options.
2782 // ===========================================================================
2783
2784 // Parse netlink messages and call input callback handler for action
filter_nlmesg(int (* fun)(struct nlmsghdr * mhdr,char ** argv),char ** argv)2785 static int filter_nlmesg(int (*fun)(struct nlmsghdr *mhdr, char **argv),
2786 char **argv)
2787 {
2788 while (1) {
2789 struct nlmsghdr *mhdr;
2790 int msglen = recv(TT.sockfd, TT.gbuf, MESG_LEN, 0);
2791
2792 if ((msglen < 0) && (errno == EINTR || errno == EAGAIN)) continue;
2793 else if (msglen < 0) {
2794 error_msg("netlink receive error %s", strerror(errno));
2795 return 1;
2796 } else if (!msglen) {
2797 error_msg("EOF on netlink");
2798 return 1;
2799 }
2800
2801 for (mhdr = (struct nlmsghdr*)TT.gbuf; NLMSG_OK(mhdr, msglen);
2802 mhdr = NLMSG_NEXT(mhdr, msglen)) {
2803 int err;
2804 if (mhdr->nlmsg_pid != getpid())
2805 continue;
2806 switch (mhdr->nlmsg_type) {
2807 case NLMSG_DONE:
2808 return 0;
2809 case NLMSG_ERROR:
2810 {
2811 struct nlmsgerr *merr = (struct nlmsgerr*)NLMSG_DATA(mhdr);
2812
2813 if (merr->error == 0) return 0;
2814 if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr)))
2815 error_msg("ERROR truncated");
2816 else {
2817 errno = -merr->error;
2818 perror_msg("RTNETLINK answers");
2819 }
2820 return 1;
2821 }
2822 default:
2823 if (fun && (err = fun(mhdr, argv))) return err;
2824 break;
2825 }
2826 } // End of for loop.
2827 } // End of while loop.
2828 return 0;
2829 }
2830
ip_main(void)2831 void ip_main(void)
2832 {
2833 char **optargv = toys.argv;
2834 int idx, isip = !(toys.which->name[2]); //1 -> if only ip
2835 cmdobj ipcmd, cmdobjlist[] = {ipaddr, iplink, iproute, iprule, iptunnel};
2836
2837 for (++optargv; *optargv; ++optargv) {
2838 char *ptr = *optargv;
2839 struct arglist ip_options[] = {{"oneline", 0}, {"family", 1},
2840 {"4", 1}, {"6", 1}, {"0", 1}, {"stats", 2}, {NULL, -1}};
2841
2842 if (*ptr != '-') break;
2843 else if ((*(ptr+1) == '-') && (*(ptr+2))) ptr +=2;
2844 //escape "--" and stop ip arg parsing.
2845 else if ((*(ptr+1) == '-') && (!*(ptr+2))) {
2846 *ptr +=1;
2847 break;
2848 } else ptr +=1;
2849 switch (substring_to_idx(ptr, ip_options)) {
2850 case 0: TT.singleline = 1;
2851 break;
2852 case 1: {
2853 if (isdigit(*ptr)) {
2854 long num = atolx(ptr);
2855 if (num == 4) TT.addressfamily = AF_INET;
2856 else if (num == 6) TT.addressfamily = AF_INET6;
2857 else TT.addressfamily = AF_PACKET;
2858 } else {
2859 struct arglist ip_aflist[] = {{"inet", AF_INET},
2860 {"inet6", AF_INET6}, {"link", AF_PACKET}, {NULL, -1}};
2861
2862 if (!*++optargv) help_exit(0);
2863 if ((TT.addressfamily = string_to_idx(*optargv, ip_aflist)) == -1)
2864 error_exit("wrong family '%s'", *optargv);
2865 }
2866 }
2867 break;
2868 case 2:
2869 TT.stats++;
2870 break;
2871 default: help_exit(0);
2872 break; // unreachable code.
2873 }
2874 }
2875
2876 TT.sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2877
2878 if (isip) {// only for ip
2879 if (*optargv) {
2880 struct arglist ip_objectlist[] = { {"address", 0}, {"link", 1},
2881 {"route", 2}, {"rule", 3}, {"tunnel", 4}, {"tunl", 4}, {NULL, -1}};
2882
2883 if ((idx = substring_to_idx(*optargv, ip_objectlist)) == -1) help_exit(0);
2884 ipcmd = cmdobjlist[idx];
2885 toys.exitval = ipcmd(++optargv);
2886 } else help_exit(0);
2887 } else {
2888 struct arglist ip_objectlist[] = { {"ipaddr", 0}, {"iplink", 1},
2889 {"iproute", 2}, {"iprule", 3}, {"iptunnel", 4}, {NULL, -1}};
2890 if ((idx = string_to_idx(toys.which->name, ip_objectlist)) == -1)
2891 help_exit(0);
2892 ipcmd = cmdobjlist[idx];
2893 toys.exitval = ipcmd(optargv);
2894 }
2895 xclose(TT.sockfd);
2896 if (rtdsfield_init) free_alist(rt_dsfield);
2897 if (rtrealms_init) free_alist(rt_realms);
2898 if (rtscope_init) free_alist(rt_scope);
2899 if (rttable_init) free_alist(rt_tables);
2900 if (rtprotos_init) free_alist(rt_protos);
2901 }
2902