Mercurial > hg > toybox
comparison toys/pending/ip.c @ 1510:4f3a5d600803 draft
Updated version, having complete support for ip _link_, _addr_, _rule_, _route_ and _tunnel_ options.
author | Ashwini Sharma <ak.ashwini1981@gmail.com> |
---|---|
date | Sun, 28 Sep 2014 19:34:53 -0500 |
parents | |
children | 2ce0f52103a9 |
comparison
equal
deleted
inserted
replaced
1509:22691dfb17b9 | 1510:4f3a5d600803 |
---|---|
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 * | |
7 * No Standard. | |
8 * | |
9 USE_IP(NEWTOY(ip, NULL, TOYFLAG_SBIN)) | |
10 USE_IP(OLDTOY(ipaddr, ip, NULL, TOYFLAG_SBIN)) | |
11 USE_IP(OLDTOY(iplink, ip, NULL, TOYFLAG_SBIN)) | |
12 USE_IP(OLDTOY(iproute, ip, NULL, TOYFLAG_SBIN)) | |
13 USE_IP(OLDTOY(iprule, ip, NULL, TOYFLAG_SBIN)) | |
14 USE_IP(OLDTOY(iptunnel, ip, NULL, TOYFLAG_SBIN)) | |
15 | |
16 config IP | |
17 bool "ip" | |
18 default n | |
19 help | |
20 usage: ip [ OPTIONS ] OBJECT { COMMAND } | |
21 | |
22 Show / manipulate routing, devices, policy routing and tunnels. | |
23 | |
24 where OBJECT := {address | link | route | rule | tunnel} | |
25 OPTIONS := { -f[amily] { inet | inet6 | link } | -o[neline] } | |
26 */ | |
27 #define FOR_ip | |
28 #include "toys.h" | |
29 #include <linux/netlink.h> | |
30 #include <linux/rtnetlink.h> | |
31 #include <linux/if_ether.h> | |
32 #include <linux/if_addr.h> | |
33 #include <net/if_arp.h> | |
34 #include <ifaddrs.h> | |
35 #include <fnmatch.h> | |
36 //============================================================================= | |
37 | |
38 // TODO List | |
39 /* | |
40 * 1. At this point of time, not clear about using hashing technique | |
41 * is useful to improve performance. | |
42 * 2. Pending decision: | |
43 * a. rt_realms, rt_dsfield and rt_tables will get free or not. | |
44 * b. rt_realms, rt_dsfield and rt_tables will be global or local. | |
45 * 3. Use atolx_range instead of strtoul. | |
46 * 4. substring_to_idx for multiple match cases. | |
47 */ | |
48 | |
49 GLOBALS( | |
50 char stats; | |
51 int sockfd; | |
52 char gbuf[8192]; | |
53 int8_t addressfamily; | |
54 int8_t is_addr; | |
55 char singleline; | |
56 char flush; | |
57 char *filter_dev; | |
58 ) | |
59 | |
60 struct arglist { | |
61 char *name; | |
62 int idx; | |
63 }; | |
64 | |
65 static struct | |
66 { | |
67 int ifindex, scope, scopemask, up, to; | |
68 char *label, *addr; | |
69 } addrinfo; | |
70 | |
71 struct linkdata { | |
72 struct linkdata *next, *prev; | |
73 int flags, iface_idx, mtu, txqueuelen, parent,iface_type; | |
74 char qdiscpline[IFNAMSIZ+1], state[IFNAMSIZ+1], type[IFNAMSIZ+1], | |
75 iface[IFNAMSIZ+1], laddr[64], bcast[64]; | |
76 struct rtnl_link_stats rt_stat; | |
77 }*linfo; | |
78 | |
79 typedef int (*cmdobj)(char **argv); | |
80 | |
81 #define MESG_LEN 8192 | |
82 | |
83 // For "/etc/iproute2/RPDB_tables" | |
84 enum { | |
85 RPDB_rtdsfield = 1, | |
86 RPDB_rtrealms = 3, | |
87 RPDB_rtscopes = 4, | |
88 RPDB_rttables = 5 | |
89 }; | |
90 | |
91 #define RPDB_ENTRIES 256 | |
92 static int8_t rttable_init; | |
93 static int8_t rtdsfield_init; | |
94 static int8_t rtscope_init; | |
95 static int8_t rtrealms_init; | |
96 | |
97 static struct arglist default_table = {"default", RT_TABLE_DEFAULT}; | |
98 static struct arglist main_table = {"main", RT_TABLE_MAIN}; | |
99 static struct arglist local_table = {"local", RT_TABLE_LOCAL}; | |
100 static struct arglist unspec_realms = {"unspec", RT_TABLE_UNSPEC}; | |
101 | |
102 static struct arglist *rt_dsfield[RPDB_ENTRIES] = {0}; | |
103 static struct arglist *rt_tables[RPDB_ENTRIES] = { | |
104 [RT_TABLE_DEFAULT] = &default_table, | |
105 [RT_TABLE_MAIN] = &main_table, | |
106 [RT_TABLE_LOCAL] = &local_table, | |
107 }; | |
108 static struct arglist *rt_realms[RPDB_ENTRIES] = { | |
109 [0] = &unspec_realms, | |
110 }; | |
111 | |
112 // default values from /etc/iproute2/rt_scope | |
113 static struct arglist global_table = {"global", 0}; | |
114 static struct arglist site_table = {"site", 200}; | |
115 static struct arglist link_table = {"link", 253}; | |
116 static struct arglist host_table = {"host", 254}; | |
117 static struct arglist nowhere_table = {"nowhere", 255}; | |
118 static struct arglist *rt_scope[256] = { | |
119 [RT_SCOPE_UNIVERSE] = &global_table, | |
120 [RT_SCOPE_SITE] = &site_table, | |
121 [RT_SCOPE_LINK] = &link_table, | |
122 [RT_SCOPE_HOST] = &host_table, | |
123 [RT_SCOPE_NOWHERE] = &nowhere_table, | |
124 }; | |
125 | |
126 static struct arglist rtmtypes[] = { {"none", RTN_UNSPEC}, | |
127 {"unicast", RTN_UNICAST}, {"local", RTN_LOCAL}, | |
128 {"broadcast", RTN_BROADCAST}, {"anycast", RTN_ANYCAST}, | |
129 {"multicast", RTN_MULTICAST}, {"blackhole", RTN_BLACKHOLE}, | |
130 {"unreachable", RTN_UNREACHABLE}, {"prohibit", RTN_PROHIBIT}, | |
131 {"throw", RTN_THROW}, {"nat", RTN_NAT}, | |
132 {"xresolve", RTN_XRESOLVE}, {NULL, -1} | |
133 }; | |
134 | |
135 static int filter_nlmesg(int (*fun)(struct nlmsghdr *mhdr, char **), char **); | |
136 static int ipaddr_print(struct linkdata *, int flg); | |
137 | |
138 | |
139 // =========================================================================== | |
140 // Common Code for IP Options (like: addr, link, route etc.) | |
141 // =========================================================================== | |
142 static int substring_to_idx(char *str, struct arglist *list) | |
143 { | |
144 struct arglist *alist; | |
145 int idx = -1, mcount = 0, len; | |
146 | |
147 if (!str) return -1; | |
148 len = strlen(str); | |
149 | |
150 for (alist = list; alist->name; alist++) { | |
151 if (!memcmp(str, alist->name, len)) { | |
152 if (!*(alist->name + len)) | |
153 return alist->idx; | |
154 idx = alist->idx; | |
155 ++mcount; // No of matches. | |
156 } | |
157 } | |
158 if (mcount > 1) return -1; // Multiple match found. | |
159 return idx; | |
160 } | |
161 | |
162 static int string_to_idx(char *str, struct arglist *list) | |
163 { | |
164 struct arglist *alist; | |
165 | |
166 if (!str) return -1; | |
167 for (alist = list; alist->name; alist++) | |
168 if (!strcmp(str, alist->name)) return alist->idx; | |
169 return -1; | |
170 } | |
171 | |
172 static char *idx_to_string(int idx, struct arglist *list) | |
173 { | |
174 struct arglist *alist; | |
175 | |
176 if (idx < 0) return NULL; | |
177 for (alist = list; alist->name; alist++) | |
178 if (idx == alist->idx) return alist->name; | |
179 return NULL; | |
180 } | |
181 | |
182 static void iphelp(void) | |
183 { | |
184 toys.exithelp = 1; | |
185 error_exit(NULL); | |
186 } | |
187 | |
188 static void send_nlmesg(int type, int flags, int family, | |
189 void *buf, int blen) | |
190 { | |
191 struct { | |
192 struct nlmsghdr nlh; | |
193 struct rtgenmsg g; | |
194 } req; | |
195 | |
196 if (!buf) { | |
197 memset(&req, 0, sizeof(req)); | |
198 req.nlh.nlmsg_len = sizeof(req); | |
199 req.nlh.nlmsg_type = type; | |
200 req.nlh.nlmsg_flags = flags; | |
201 req.g.rtgen_family = family; | |
202 buf = &req; | |
203 blen = sizeof(req); | |
204 } | |
205 if (send(TT.sockfd , (void*)buf, blen, 0) < 0) | |
206 perror_exit("Unable to send data on socket."); | |
207 } | |
208 | |
209 // Parse /etc/iproute2/RPDB_tables and prepare list. | |
210 static void parseRPDB(char *fname, struct arglist **list, int32_t size) | |
211 { | |
212 char *line; | |
213 int fd = open(fname, O_RDONLY); | |
214 | |
215 if (fd < 0) return; | |
216 for (; (line = get_line(fd)); free(line)) { | |
217 char *ptr = line; | |
218 int32_t idx; | |
219 | |
220 while (*ptr == ' ' || *ptr == '\t') ptr++; | |
221 if (*ptr == 0 || *ptr == '#' || *ptr == '\n') continue; | |
222 if ((sscanf(ptr, "0x%x %s\n", &idx, toybuf) != 2) && | |
223 (sscanf(ptr, "0x%x %s #", &idx, toybuf) != 2) && | |
224 (sscanf(ptr, "%d %s\n", &idx, toybuf) != 2) && | |
225 (sscanf(ptr, "%d %s #", &idx, toybuf) != 2)) { | |
226 error_msg("Corrupted '%s' file", fname); | |
227 xclose(fd); | |
228 free(line); | |
229 return; | |
230 } | |
231 if (idx >= 0 && idx < size) { | |
232 int index = idx & (size-1); | |
233 list[index] = xzalloc(sizeof(struct arglist)); | |
234 list[index]->idx = idx; | |
235 list[index]->name = xstrdup(toybuf); | |
236 } | |
237 } | |
238 xclose(fd); | |
239 } | |
240 | |
241 static struct arglist **getlist(u_int8_t whichDB) | |
242 { | |
243 struct arglist **alist; | |
244 | |
245 switch (whichDB) { | |
246 case RPDB_rtdsfield: | |
247 alist = rt_dsfield; | |
248 if (!rtdsfield_init) { | |
249 rtdsfield_init = 1; | |
250 parseRPDB("/etc/iproute2/rt_dsfield", alist, ARRAY_LEN(rt_dsfield)); | |
251 } | |
252 break; | |
253 case RPDB_rtrealms: | |
254 alist = rt_realms; | |
255 if (!rtrealms_init) { | |
256 rtrealms_init = 1; | |
257 parseRPDB("/etc/iproute2/rt_realms", alist, ARRAY_LEN(rt_realms)); | |
258 } | |
259 break; | |
260 case RPDB_rtscopes: | |
261 alist = rt_scope; | |
262 if (!rtscope_init) { | |
263 rtscope_init = 1; | |
264 parseRPDB("/etc/iproute2/rt_scopes", alist, ARRAY_LEN(rt_scope)); | |
265 } | |
266 break; | |
267 case RPDB_rttables: | |
268 alist = rt_tables; | |
269 if (!rttable_init) { | |
270 rttable_init = 1; | |
271 parseRPDB("/etc/iproute2/rt_tables", alist, ARRAY_LEN(rt_tables)); | |
272 } | |
273 break; | |
274 default: | |
275 error_exit("wrong database"); | |
276 break; // Unreachable code. | |
277 } | |
278 return alist; | |
279 } | |
280 | |
281 /* | |
282 * Parse RPBD tables (if not parsed already). | |
283 * return RPDB table name as per idx. | |
284 */ | |
285 static char *namefromRPDB(int idx, u_int8_t whichDB) | |
286 { | |
287 struct arglist **alist; | |
288 | |
289 if (idx < RT_TABLE_UNSPEC || idx >= RPDB_ENTRIES) { | |
290 snprintf(toybuf, RPDB_ENTRIES, "%u", idx); | |
291 return toybuf; | |
292 } | |
293 | |
294 alist = getlist(whichDB); | |
295 | |
296 if (alist[idx] && alist[idx]->name) return alist[idx]->name; | |
297 if (whichDB == RPDB_rtdsfield) snprintf(toybuf, RPDB_ENTRIES, "0x%02x", idx); | |
298 else snprintf(toybuf, RPDB_ENTRIES, "%u", idx); | |
299 | |
300 return toybuf; | |
301 } | |
302 | |
303 static int idxfromRPDB(char *name, u_int8_t whichDB) | |
304 { | |
305 struct arglist **alist; | |
306 int i = 0; | |
307 | |
308 for (alist = getlist(whichDB); i < RPDB_ENTRIES; i++) { | |
309 if (!alist[i] || !alist[i]->name) continue; | |
310 if (!strcmp(alist[i]->name, name)) return i; | |
311 } | |
312 return (atolx_range(name, 0, 255)); | |
313 } | |
314 | |
315 /* | |
316 * TODO: | |
317 * rtmtype_idx2str and rtmtype_str2idx interfaces may be used in IPROUTE too. | |
318 */ | |
319 static char *rtmtype_idx2str(u_int8_t idx) | |
320 { | |
321 char *name = idx_to_string(idx, rtmtypes); | |
322 | |
323 if (!name) snprintf(toybuf, RPDB_ENTRIES, "%u", idx); | |
324 else strcpy(toybuf, name); | |
325 | |
326 return toybuf; | |
327 } | |
328 | |
329 static int rtmtype_str2idx(char *name) | |
330 { | |
331 int idx = string_to_idx(name, rtmtypes); | |
332 | |
333 if (idx < 0) return atolx_range(name, 0, 255); | |
334 return idx; | |
335 } | |
336 | |
337 /* | |
338 * Used to get the prefix value in binary form. | |
339 * For IPv4: non-standard parsing used; as 10.10 will be treated as 10.10.0.0 | |
340 * unlike inet_aton which is 10.0.0.10 | |
341 */ | |
342 static int get_prefix(uint32_t *addr, uint8_t *af, char *name, int family) | |
343 { | |
344 if (family == AF_PACKET) error_exit("'%s' may be inet prefix", name); | |
345 if (!memcmp(name, "default", strlen(name)) | |
346 || !memcmp(name, "all", strlen(name)) | |
347 || !memcmp(name, "any", strlen(name))) { | |
348 *af = family; | |
349 return 0; | |
350 } | |
351 if (strchr(name, ':')) { | |
352 *af = AF_INET6; | |
353 if (family != AF_UNSPEC && family != AF_INET6) return 1; | |
354 if (inet_pton(AF_INET6, name, (void *)addr) != 1) | |
355 return 1; | |
356 } else { // for IPv4. | |
357 char *ptr = name; | |
358 uint8_t count = 0; | |
359 | |
360 *af = AF_INET; | |
361 if (family != AF_UNSPEC && family != AF_INET) return 1; | |
362 while (*ptr) { | |
363 int val, len = 0; | |
364 | |
365 if (*ptr == '.') ptr++; | |
366 sscanf(ptr, "%d%n", &val, &len); | |
367 if (!len || len > 3 || val < 0 || val > 255 || count > 3) return 1; | |
368 ptr += len; | |
369 ((uint8_t*)addr)[count++] = val; | |
370 } | |
371 } | |
372 return 0; | |
373 } | |
374 | |
375 /* | |
376 * Used to calculate netmask, which can be in the form of | |
377 * either 255.255.255.0 or 24 or default or any or all strings. | |
378 */ | |
379 static int get_nmask_prefix(uint32_t *netmask, uint8_t af, | |
380 char *name, uint8_t family) | |
381 { | |
382 char *ptr; | |
383 uint32_t naddr[4] = {0,}; | |
384 uint64_t plen; | |
385 uint8_t naf = AF_UNSPEC; | |
386 | |
387 *netmask = (af == AF_INET6) ? 128 : 32; // set default netmask | |
388 plen = strtoul(name, &ptr, 0); | |
389 | |
390 if (!ptr || ptr == name || *ptr || !plen || plen > *netmask) { | |
391 if (get_prefix(naddr, &naf, name, family)) return -1; | |
392 if (naf == AF_INET) { | |
393 uint32_t mask = htonl(*naddr), host = ~mask; | |
394 if (host & (host + 1)) return -1; | |
395 for (plen = 0; mask; mask <<= 1) ++plen; | |
396 if (plen > 32) return -1; | |
397 } | |
398 } | |
399 *netmask = plen; | |
400 return 0; | |
401 } | |
402 | |
403 /* | |
404 * Parse prefix, which will be in form of | |
405 * either default or default/default or default/24 or default/255.255.255.0 | |
406 * or 10.20.30.40 or 10.20.30.40/default or 10.20.30.40/24 | |
407 * or 10.20.30.40/255.255.255.0 | |
408 */ | |
409 static void parse_prefix(uint32_t *addr, uint32_t *netmask, uint8_t *len, | |
410 char *name, int family) | |
411 { | |
412 uint8_t af = AF_UNSPEC; | |
413 char *slash = strchr(name, '/'); | |
414 | |
415 if (slash) *slash = 0; | |
416 if (get_prefix(addr, &af, name, family)) error_exit("Invalid prefix"); | |
417 | |
418 if (slash) { // grab netmask. | |
419 if (get_nmask_prefix(netmask, af, slash+1, family)) | |
420 error_exit("Invalid prefix"); | |
421 *slash ='/'; | |
422 } | |
423 else if (af == AF_INET && *addr) *netmask = 32; | |
424 else if (af == AF_INET6 && (*addr || *(addr+3))) *netmask = 128; | |
425 | |
426 if (!*addr && !slash && !af) *len = 0; | |
427 else *len = (af == AF_INET6) ? 16 : 4; | |
428 } | |
429 | |
430 static void add_string_to_rtattr(struct nlmsghdr *n, int maxlen, | |
431 int type, void *data, int alen) | |
432 { | |
433 int len = RTA_LENGTH(alen); | |
434 struct rtattr *rta; | |
435 | |
436 if ((int)(NLMSG_ALIGN(n->nlmsg_len) + len) > maxlen) return; | |
437 rta = (struct rtattr*)(((char*)n) + NLMSG_ALIGN(n->nlmsg_len)); | |
438 rta->rta_type = type; | |
439 rta->rta_len = len; | |
440 memcpy(RTA_DATA(rta), data, alen); | |
441 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + len; | |
442 } | |
443 | |
444 | |
445 | |
446 // =========================================================================== | |
447 // Code for ip link. | |
448 // =========================================================================== | |
449 #ifndef NLMSG_TAIL | |
450 #define NLMSG_TAIL(nmsg) \ | |
451 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
452 #endif | |
453 | |
454 static uint32_t get_ifaceindex(char *name, int ext) | |
455 { | |
456 struct if_nameindex *if_ni, *i; | |
457 int index = -1; | |
458 if_ni = if_nameindex(); | |
459 if (if_ni == NULL) { | |
460 perror("if_nameindex"); | |
461 exit(EXIT_FAILURE); | |
462 } | |
463 | |
464 for (i = if_ni; ! (i->if_index == 0 && i->if_name == NULL); i++) | |
465 if(!strcmp(name,i->if_name)) { index =i->if_index; break; } | |
466 if_freenameindex(if_ni); | |
467 if (index == -1 && ext) perror_exit("can't find device '%s'", name); | |
468 return index; | |
469 } | |
470 | |
471 static void fill_hwaddr(char *arg, int len, unsigned char *address) | |
472 { | |
473 int count = 0, val, length; | |
474 | |
475 while (count < len) { | |
476 val = length = 0; | |
477 if (*arg == ':') arg++, count++; | |
478 sscanf(arg, "%2x%n", &val, &length); | |
479 if (!length || length > 2) | |
480 error_exit("bad hw-addr '%s'", arg ? arg : ""); | |
481 arg += length; | |
482 count += length; | |
483 *address++ = val; | |
484 } | |
485 } | |
486 | |
487 // Multimach = 1, single match = 0 | |
488 static char *get_flag_string(struct arglist *aflags, int flags, int ismulti) | |
489 { | |
490 struct arglist *p = aflags; | |
491 char *out = NULL, *tmp = NULL; | |
492 | |
493 for (; p->name; p++) { | |
494 int test = (ismulti ? p->idx & flags : 0) || p->idx == flags; | |
495 if (test) { // flags can be zero | |
496 tmp = out ? xmprintf("%s,%s", out, p->name) : xmprintf("%s", p->name); | |
497 if (out) free(out); | |
498 out = tmp; | |
499 } | |
500 } | |
501 return out; | |
502 } | |
503 | |
504 static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size) | |
505 { | |
506 struct arglist vlan_optlist[] = {{"id", 0}, {"protocol", 1}, | |
507 {"reorder_hdr", 2}, {"gvrp", 3}, {NULL,-1}}; | |
508 struct arglist vlan_protolist[] = {{"802.1q", 0}, {"802.1ad", 1}, {NULL,-1}}; | |
509 struct arglist on_off[] = { {"on", 0}, {"off", 1}, {NULL,-1}}; | |
510 int idx; | |
511 struct ifla_vlan_flags flags; | |
512 | |
513 memset(&flags, 0, sizeof(flags)); | |
514 for (; *argv; argv++) { | |
515 int param, proto; | |
516 | |
517 if ((idx = substring_to_idx(*argv++, vlan_optlist)) == -1) iphelp(); | |
518 switch (idx) { | |
519 case 0: // ARG_id | |
520 if (!*argv) iphelp(); | |
521 param = atolx(*argv); | |
522 add_string_to_rtattr(n, size, IFLA_VLAN_ID, ¶m, sizeof(param)); | |
523 break; | |
524 case 1: // ARG_protocol | |
525 if (!*argv) error_exit("Invalid vlan id."); | |
526 if ((idx = substring_to_idx(*argv, vlan_protolist)) == -1) iphelp(); | |
527 if (!idx) proto = ETH_P_8021Q; // PROTO_8021Q - 0 | |
528 else if (idx == 1) proto = 0x88A8; // ETH Protocol - 8021AD | |
529 // IFLA VLAN PROTOCOL - 5 | |
530 add_string_to_rtattr(n, size, 5, &proto, sizeof(proto)); | |
531 break; | |
532 case 2: // ARG_reorder_hdr | |
533 case 3: // ARG_gvrp | |
534 if ((param = substring_to_idx(*argv, on_off)) == -1) iphelp(); | |
535 | |
536 flags.mask |= (idx -1); // VLAN FLAG REORDER Header | |
537 flags.flags &= ~(idx -1); // VLAN FLAG REORDER Header | |
538 if (!param) flags.flags |= (idx -1); // VLAN FLAG REORDER Header | |
539 break; | |
540 } | |
541 } | |
542 if (flags.mask) | |
543 add_string_to_rtattr(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags)); | |
544 } | |
545 | |
546 static int linkupdate(char **argv) | |
547 { | |
548 struct { | |
549 struct nlmsghdr mhdr; | |
550 struct ifinfomsg info; | |
551 char buf[1024]; | |
552 } request; | |
553 char *name, *dev, *type, *link, *addr; | |
554 struct rtattr *attr = NULL; | |
555 int len = 0, add = (*argv[-1] == 'a') ? 1 : 0; | |
556 | |
557 name = dev = type = link = addr = NULL; | |
558 for (; *argv; argv++) { | |
559 struct arglist objectlist[] = { {"type", 0}, {"name", 1}, {"link", 2}, | |
560 {"address", 3}, {NULL,-1}}; | |
561 uint8_t idx = substring_to_idx(*argv, objectlist); | |
562 | |
563 if (!idx) { | |
564 type = *++argv; | |
565 break; | |
566 } | |
567 else if (idx == 1) dev = name = *++argv; | |
568 else if (idx == 2) link = *++argv; | |
569 else if (idx == 3) addr = *++argv; | |
570 else if (!dev) name = dev = *argv; | |
571 } | |
572 | |
573 if (!name && !add) | |
574 error_exit("Not enough information: \"dev\" argument is required.\n"); | |
575 else if (!type && add) | |
576 error_exit("Not enough information: \"type\" argument is required.\n"); | |
577 | |
578 memset(&request, 0, sizeof(request)); | |
579 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
580 request.mhdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; | |
581 if (add) { | |
582 request.mhdr.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; | |
583 request.mhdr.nlmsg_type = RTM_NEWLINK; | |
584 } else { | |
585 request.mhdr.nlmsg_type = RTM_DELLINK; | |
586 request.info.ifi_index = get_ifaceindex(name, 1); | |
587 } | |
588 request.info.ifi_family = AF_UNSPEC; | |
589 attr = NLMSG_TAIL(&request.mhdr); | |
590 if (type) { | |
591 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
592 IFLA_LINKINFO, NULL, 0); | |
593 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
594 IFLA_INFO_KIND, type, strlen(type)); | |
595 if (!strcmp(type, "vlan")) { | |
596 struct rtattr *data = NLMSG_TAIL(&request.mhdr); | |
597 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
598 IFLA_INFO_DATA, NULL, 0); | |
599 vlan_parse_opt(++argv, &request.mhdr, sizeof(request)); | |
600 data->rta_len = (void *)NLMSG_TAIL(&request.mhdr) - (void *)data; | |
601 } | |
602 attr->rta_len = (void *)NLMSG_TAIL(&request.mhdr) - (void *)attr; | |
603 } | |
604 | |
605 if (link) { | |
606 uint32_t idx = get_ifaceindex(link, 1); | |
607 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
608 IFLA_LINK, &idx, sizeof(uint32_t)); | |
609 } | |
610 if (addr) { | |
611 char abuf[IF_NAMESIZE] = {0,}; | |
612 | |
613 fill_hwaddr(addr, IF_NAMESIZE, (unsigned char *)abuf); | |
614 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
615 IFLA_ADDRESS, abuf, strlen(abuf)); | |
616 } | |
617 if (!name) { | |
618 snprintf(toybuf, IFNAMSIZ, "%s%d", type, 0); | |
619 for (len = 1; ; len++) { | |
620 if (!get_ifaceindex(toybuf, 0)) break; | |
621 snprintf(toybuf, IFNAMSIZ, "%s%d", type, len); | |
622 } | |
623 name = toybuf; | |
624 } | |
625 len = strlen(name) + 1; | |
626 if (len < 2 || len > IFNAMSIZ) error_exit("Invalid device name."); | |
627 add_string_to_rtattr(&request.mhdr, sizeof(request), IFLA_IFNAME, name, len); | |
628 | |
629 send_nlmesg(0, 0, 0, (void *)&request, request.mhdr.nlmsg_len); | |
630 return (filter_nlmesg(NULL,NULL)); | |
631 } | |
632 | |
633 static int link_set(char **argv) | |
634 { | |
635 struct arglist cmd_objectlist[] = {{"up", 0}, {"down", 1}, {"arp", 2}, | |
636 {"multicast", 3}, {"dynamic", 4}, {"name", 5}, {"txqueuelen", 6}, | |
637 {"mtu", 7},{"address", 8}, {"broadcast", 9}, {NULL,-1}}; | |
638 int case_flags[] = {IFF_NOARP,IFF_MULTICAST,IFF_DYNAMIC}; | |
639 struct ifreq req; | |
640 int idx, flags = 0, masks = 0xffff, fd; | |
641 | |
642 memset(&req, 0, sizeof(req)); | |
643 if (!*argv) error_exit("\"dev\" missing"); | |
644 strncpy(req.ifr_name, *argv, IF_NAMESIZE); | |
645 fd = xsocket(AF_INET, SOCK_DGRAM, 0); | |
646 xioctl(fd, SIOCGIFINDEX, &req); | |
647 for (++argv; *argv;) { | |
648 if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); | |
649 switch(idx) { | |
650 case 0: | |
651 flags |= IFF_UP; break; | |
652 case 1: | |
653 masks &= ~IFF_UP; break; | |
654 case 2: | |
655 case 3: | |
656 case 4: | |
657 if (!*argv) iphelp(); | |
658 else if (!strcmp(*argv, "on")) { | |
659 if (idx == 2) { | |
660 masks &= ~case_flags[idx-2]; | |
661 flags &= ~case_flags[idx-2]; | |
662 } else flags |= case_flags[idx-2]; | |
663 } else if (!strcmp(*argv,"off")) { | |
664 if (idx == 2) { | |
665 masks |= case_flags[idx-2]; | |
666 flags |= case_flags[idx-2]; | |
667 } else masks &= ~case_flags[idx-2]; | |
668 } else iphelp(); | |
669 ++argv; | |
670 break; | |
671 case 5: | |
672 strncpy(req.ifr_ifru.ifru_newname, *argv, IF_NAMESIZE); | |
673 xioctl(fd, SIOCSIFNAME, &req); | |
674 strncpy(req.ifr_name, *argv++, IF_NAMESIZE); | |
675 xioctl(fd, SIOCGIFINDEX, &req); | |
676 break; | |
677 case 6: | |
678 req.ifr_ifru.ifru_ivalue = atolx(*argv++); | |
679 xioctl(fd, SIOCSIFTXQLEN, &req); | |
680 break; | |
681 case 7: | |
682 req.ifr_ifru.ifru_mtu = atolx(*argv++); | |
683 xioctl(fd, SIOCSIFMTU, &req); | |
684 break; | |
685 case 8: | |
686 xioctl(fd, SIOCGIFHWADDR, &req); | |
687 fill_hwaddr(*argv++, IF_NAMESIZE, | |
688 (unsigned char *)(req.ifr_hwaddr.sa_data)); | |
689 xioctl(fd, SIOCSIFHWADDR, &req); | |
690 break; | |
691 case 9: | |
692 xioctl(fd, SIOCGIFHWADDR, &req); | |
693 fill_hwaddr(*argv++, IF_NAMESIZE, | |
694 (unsigned char *)(req.ifr_hwaddr.sa_data)); | |
695 xioctl(fd, SIOCSIFHWBROADCAST, &req); | |
696 break; | |
697 } | |
698 } | |
699 xioctl(fd, SIOCGIFFLAGS, &req); | |
700 req.ifr_ifru.ifru_flags |= flags; | |
701 req.ifr_ifru.ifru_flags &= masks; | |
702 xioctl(fd, SIOCSIFFLAGS, &req); | |
703 xclose(fd); | |
704 return 0; | |
705 } | |
706 | |
707 static void print_stats(struct rtnl_link_stats *rtstat) | |
708 { | |
709 char *line_feed = (!TT.singleline ? "\n " : " "); | |
710 | |
711 if (TT.stats > 0) { | |
712 xprintf(" RX: bytes packets errors " | |
713 "dropped overrun mcast%s%-10u %-8u %-7u %-8u %-8u %-8u\n", | |
714 line_feed, rtstat->rx_bytes, rtstat->rx_packets, rtstat->rx_errors, | |
715 rtstat->rx_dropped, rtstat->rx_over_errors, rtstat->multicast); | |
716 if (TT.stats > 1) { | |
717 xprintf(" RX: errors length crc " | |
718 "frame fifo missed%s%-10u %-8u %-7u %-8u %-8u %-8u\n", | |
719 line_feed, rtstat->rx_errors, rtstat->rx_length_errors, | |
720 rtstat->rx_crc_errors, rtstat->rx_frame_errors, | |
721 rtstat->rx_fifo_errors, rtstat->rx_missed_errors); | |
722 } | |
723 xprintf(" TX: bytes packets errors " | |
724 "dropped carrier collsns%s%-10u %-8u %-7u %-8u %-8u %-8u\n", | |
725 line_feed, rtstat->tx_bytes, rtstat->tx_packets, rtstat->tx_errors, | |
726 rtstat->tx_dropped, rtstat->tx_carrier_errors, rtstat->collisions); | |
727 if (TT.stats > 1) { | |
728 xprintf(" TX: errors aborted fifo window " | |
729 "heartbeat%s%-10u %-8u %-7u %-8u %-8u\n", | |
730 line_feed, rtstat->tx_errors, rtstat->tx_aborted_errors, | |
731 rtstat->tx_fifo_errors, rtstat->tx_window_errors, | |
732 rtstat->tx_heartbeat_errors); | |
733 } | |
734 } | |
735 } | |
736 | |
737 static int print_link_output(struct linkdata *link) | |
738 { | |
739 char *line_feed = " ", *flags,*peer = "brd"; | |
740 struct arglist iface_flags[] = {{"",0},{"UP", IFF_UP}, | |
741 {"BROADCAST", IFF_BROADCAST}, {"DEBUG", IFF_DEBUG}, | |
742 {"LOOPBACK", IFF_LOOPBACK}, {"POINTOPOINT", IFF_POINTOPOINT}, | |
743 {"NOTRAILERS", IFF_NOTRAILERS}, {"RUNNING", IFF_RUNNING}, | |
744 {"NOARP", IFF_NOARP}, {"PROMISC",IFF_PROMISC}, | |
745 {"ALLMULTI", IFF_ALLMULTI}, {"MASTER", IFF_MASTER}, {"SLAVE", IFF_SLAVE}, | |
746 {"MULTICAST", IFF_MULTICAST}, {"PORTSEL", IFF_PORTSEL}, | |
747 {"AUTOMEDIA", IFF_AUTOMEDIA}, {"DYNAMIC", IFF_DYNAMIC}, {NULL,-1}}; | |
748 | |
749 if (link->parent != -1) { | |
750 int fd = 0; | |
751 struct ifreq req; | |
752 | |
753 memset(&req, 0, sizeof(req)); | |
754 if_indextoname( link->parent,req.ifr_ifrn.ifrn_name); | |
755 fd = xsocket(AF_INET, SOCK_DGRAM, 0); | |
756 if (ioctl(fd, SIOCGIFTXQLEN, &req)) perror(""); | |
757 else link->txqueuelen = req.ifr_ifru.ifru_ivalue; | |
758 xclose(fd); | |
759 } | |
760 | |
761 if(TT.is_addr && addrinfo.label && fnmatch(addrinfo.label, link->iface, 0)) | |
762 return 0; | |
763 | |
764 | |
765 if (!(flags = get_flag_string(iface_flags, link->flags, 1))) | |
766 error_exit("Invalid data."); | |
767 if (!TT.singleline) line_feed="\n "; | |
768 if (link->parent != -1) { | |
769 char iface[IF_NAMESIZE]; | |
770 | |
771 if (!if_indextoname(link->parent, iface)) perror_exit(NULL); | |
772 sprintf(toybuf,"%s@%s", link->iface, iface); | |
773 } | |
774 if(link->flags & IFF_POINTOPOINT) peer = "peer"; | |
775 if (TT.is_addr && TT.singleline && TT.addressfamily) | |
776 xprintf("%d: %s", link->iface_idx, | |
777 ((link->parent == -1) ? link->iface : toybuf)); | |
778 else xprintf("%d: %s: <%s> mtu %d qdisc %s state %s qlen %d", | |
779 link->iface_idx, ((link->parent == -1) ? link->iface : toybuf), flags, | |
780 link->mtu, link->qdiscpline, link->state, link->txqueuelen); | |
781 | |
782 if (!TT.addressfamily || TT.addressfamily == AF_PACKET) | |
783 xprintf("%slink/%s %s %s %s", | |
784 line_feed, link->type, link->laddr, peer ,link->bcast); | |
785 | |
786 xputc('\n'); | |
787 | |
788 //user can specify stats flag two times | |
789 //one for stats and other for erros e.g. -s and -s -s | |
790 print_stats(&link->rt_stat); | |
791 free(flags); | |
792 | |
793 return 0; | |
794 } | |
795 | |
796 static void fill_address(void *p, char *ip) | |
797 { | |
798 unsigned char *ptr = (unsigned char*)p; | |
799 snprintf(ip, 64, " %02x:%02x:%02x:%02x:%02x:%02x", | |
800 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); | |
801 } | |
802 | |
803 static int get_link_info(struct nlmsghdr* h,struct linkdata* link,char **argv) | |
804 { | |
805 struct ifinfomsg *iface = NLMSG_DATA(h); | |
806 struct rtattr *attr = IFLA_RTA(iface); | |
807 int len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*iface)); | |
808 struct arglist hwtypes[]={{"generic",0},{"ether",ARPHRD_ETHER}, | |
809 {"loopback", ARPHRD_LOOPBACK},{"sit",ARPHRD_SIT}, | |
810 #ifdef ARPHRD_INFINIBAND | |
811 {"infiniband",ARPHRD_INFINIBAND}, | |
812 #endif | |
813 #ifdef ARPHRD_IEEE802_TR | |
814 {"ieee802",ARPHRD_IEEE802}, {"tr",ARPHRD_IEEE802_TR}, | |
815 #else | |
816 {"tr",ARPHRD_IEEE802}, | |
817 #endif | |
818 #ifdef ARPHRD_IEEE80211 | |
819 {"ieee802.11",ARPHRD_IEEE80211}, | |
820 #endif | |
821 #ifdef ARPHRD_IEEE1394 | |
822 {"ieee1394",ARPHRD_IEEE1394}, | |
823 #endif | |
824 {"irda",ARPHRD_IRDA},{"slip",ARPHRD_SLIP},{"cslip",ARPHRD_CSLIP}, | |
825 {"slip6",ARPHRD_SLIP6}, {"cslip6",ARPHRD_CSLIP6}, {"ppp",ARPHRD_PPP}, | |
826 {"ipip",ARPHRD_TUNNEL}, {"tunnel6",ARPHRD_TUNNEL6}, | |
827 {"gre",ARPHRD_IPGRE}, | |
828 #ifdef ARPHRD_VOID | |
829 {"void",ARPHRD_VOID}, | |
830 #endif | |
831 {NULL,-1}}; | |
832 char *lname = get_flag_string(hwtypes, iface->ifi_type, 0); | |
833 | |
834 link->next = link->prev = 0; | |
835 link->iface_type = iface->ifi_type; | |
836 if (!lname) error_exit("Invalid link."); | |
837 strncpy(link->type, lname, IFNAMSIZ); | |
838 free(lname); | |
839 link->iface_idx = iface->ifi_index; | |
840 link->flags = iface->ifi_flags; | |
841 if (*argv && !strcasecmp("up",*argv) && !(link->flags & IFF_UP)) return 1; | |
842 link->parent = -1; | |
843 for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) { | |
844 switch(attr->rta_type) { | |
845 case IFLA_IFNAME: | |
846 snprintf(link->iface, IFNAMSIZ, "%s",(char *) RTA_DATA(attr)); | |
847 break; | |
848 case IFLA_ADDRESS: | |
849 if ( iface->ifi_type== ARPHRD_TUNNEL || | |
850 iface->ifi_type == ARPHRD_SIT || | |
851 iface->ifi_type == ARPHRD_IPGRE) | |
852 inet_ntop(AF_INET, RTA_DATA(attr), link->laddr, 64); | |
853 else fill_address(RTA_DATA(attr), link->laddr); | |
854 break; | |
855 case IFLA_BROADCAST: | |
856 if (iface->ifi_type== ARPHRD_TUNNEL || | |
857 iface->ifi_type == ARPHRD_SIT || | |
858 iface->ifi_type == ARPHRD_IPGRE) | |
859 inet_ntop(AF_INET, RTA_DATA(attr), link->bcast, 64); | |
860 else fill_address(RTA_DATA(attr), link->bcast); | |
861 break; | |
862 case IFLA_MTU: | |
863 link->mtu = *((int*)(RTA_DATA(attr))); | |
864 break; | |
865 case IFLA_QDISC: | |
866 snprintf(link->qdiscpline, IFNAMSIZ, "%s", (char *) RTA_DATA(attr)); | |
867 break; | |
868 case IFLA_STATS : | |
869 link->rt_stat = *((struct rtnl_link_stats*) RTA_DATA(attr)); | |
870 break; | |
871 case IFLA_LINK: | |
872 link->parent = *((int*)(RTA_DATA(attr))); | |
873 break; | |
874 case IFLA_TXQLEN: | |
875 link->txqueuelen = *((int*)(RTA_DATA(attr))); | |
876 break; | |
877 case IFLA_OPERSTATE: | |
878 { | |
879 struct arglist flags[]={{"UNKNOWN", 0}, {"NOTPRESENT", 1}, | |
880 {"DOWN", 2}, {"LOWERLAYERDOWN", 3}, {"TESTING", 4}, | |
881 {"DORMANT", 5}, {"UP", 6}, {NULL, -1}}; | |
882 if (!(lname = get_flag_string(flags, *((int*)(RTA_DATA(attr))), 0))) | |
883 error_exit("Invalid state."); | |
884 strncpy(link->state, lname,IFNAMSIZ); | |
885 free(lname); | |
886 } | |
887 break; | |
888 default: break; | |
889 } | |
890 } | |
891 return 0; | |
892 } | |
893 | |
894 static int display_link_info(struct nlmsghdr *mhdr, char **argv) | |
895 { | |
896 struct linkdata link; | |
897 | |
898 if(!get_link_info(mhdr, &link, argv)){ | |
899 if(TT.is_addr) { | |
900 struct linkdata *lnk = xzalloc(sizeof(struct linkdata)); | |
901 memcpy(lnk, &link, sizeof(struct linkdata)); | |
902 dlist_add_nomalloc((struct double_list **)&linfo, | |
903 (struct double_list *)lnk); | |
904 } | |
905 else print_link_output(&link); | |
906 } | |
907 return 0; | |
908 } | |
909 | |
910 static int link_show(char **argv) | |
911 { | |
912 struct { | |
913 struct nlmsghdr mhdr; | |
914 struct ifinfomsg info; | |
915 } request; | |
916 uint32_t index = 0; | |
917 | |
918 if (*argv && strcasecmp("up",*argv)) index = get_ifaceindex(*argv, 1); | |
919 memset(&request, 0, sizeof(request)); | |
920 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | |
921 request.mhdr.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; | |
922 if (!index) request.mhdr.nlmsg_flags |= NLM_F_ROOT|NLM_F_MATCH; | |
923 else request.info.ifi_change = 0xffffffff; // used in single operation | |
924 request.mhdr.nlmsg_type = RTM_GETLINK; | |
925 request.info.ifi_index = index; | |
926 request.info.ifi_family = AF_UNSPEC; | |
927 send_nlmesg(0, 0, 0, (void*)&request, sizeof(request)); | |
928 return (filter_nlmesg(display_link_info, argv)); | |
929 } | |
930 | |
931 static int iplink(char **argv) | |
932 { | |
933 int idx; | |
934 cmdobj ipcmd, cmdobjlist[] = {linkupdate, link_set, link_show}; | |
935 struct arglist cmd_objectlist[] = {{"add", 0}, {"delete", 0}, | |
936 {"set", 1}, {"show", 2}, {"list", 2}, {"lst", 2}, {NULL,-1}}; | |
937 | |
938 if (!*argv) idx = 2; | |
939 else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); | |
940 ipcmd = cmdobjlist[idx]; | |
941 return ipcmd(argv); | |
942 } | |
943 | |
944 // =========================================================================== | |
945 // Code for ip addr. | |
946 // =========================================================================== | |
947 | |
948 static int print_addrinfo(struct nlmsghdr *h, int flag_l) | |
949 { | |
950 struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,}; | |
951 char *family = toybuf, *scope = toybuf+256, *label = toybuf+512, | |
952 *brd = toybuf+768, *peer = toybuf+1024, *any = toybuf+1280, | |
953 lbuf[INET6_ADDRSTRLEN] = {0,}, lbuf_ifa[INET6_ADDRSTRLEN] = {0,}; | |
954 struct ifaddrmsg *ifa = NLMSG_DATA(h); | |
955 int len; | |
956 | |
957 if ((len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))) < 0) { | |
958 error_msg("wrong nlmsg len %d", len); | |
959 return 0; | |
960 } | |
961 | |
962 for (rta = IFA_RTA(ifa); RTA_OK(rta, len); rta=RTA_NEXT(rta, len)) | |
963 if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta; | |
964 | |
965 if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; | |
966 if (!rta_tb[IFA_ADDRESS]) rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; | |
967 if ((addrinfo.scope ^ ifa->ifa_scope)&addrinfo.scopemask) return 0; | |
968 if (addrinfo.ifindex && addrinfo.ifindex != ifa->ifa_index) return 0; | |
969 | |
970 if (flag_l && addrinfo.label && ifa->ifa_family == AF_INET6) return 0; | |
971 if ((rta_tb[IFA_LABEL])) { | |
972 strcpy(label, RTA_DATA(rta_tb[IFA_LABEL])); | |
973 if(addrinfo.label && fnmatch(addrinfo.label, label, 0)) | |
974 return 0; | |
975 } | |
976 | |
977 if (TT.flush) { | |
978 if (ifa->ifa_index == addrinfo.ifindex) { | |
979 h->nlmsg_type = RTM_DELADDR; | |
980 h->nlmsg_flags = NLM_F_REQUEST; | |
981 send_nlmesg(RTM_DELADDR, 0, 0, h, h->nlmsg_len); | |
982 return 0; | |
983 } | |
984 } | |
985 | |
986 if (h->nlmsg_type == RTM_DELADDR) printf("Deleted "); | |
987 | |
988 if (TT.singleline) { | |
989 if (!if_indextoname(ifa->ifa_index, lbuf)) perror_exit(NULL); | |
990 printf("%u: %s",ifa->ifa_index, lbuf); | |
991 } | |
992 | |
993 sprintf(scope, " scope %s ", namefromRPDB(ifa->ifa_scope, RPDB_rtscopes)); | |
994 | |
995 if (ifa->ifa_family == AF_INET) strcpy(family, " inet "); | |
996 else if (ifa->ifa_family == AF_INET6) strcpy(family, " inet6 "); | |
997 else sprintf(family, " family %d", ifa->ifa_family); | |
998 | |
999 if (rta_tb[IFA_LOCAL]) { | |
1000 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL]), | |
1001 lbuf, sizeof(lbuf))) perror_exit("inet"); | |
1002 | |
1003 sprintf(family+strlen(family), lbuf, strlen(lbuf)); | |
1004 if (!rta_tb[IFA_ADDRESS] || !memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), | |
1005 RTA_DATA(rta_tb[IFA_LOCAL]), 4)) | |
1006 sprintf(family+strlen(family), "/%d ", ifa->ifa_prefixlen); | |
1007 else { | |
1008 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS]), | |
1009 lbuf_ifa, sizeof(lbuf_ifa))) perror_exit("inet"); | |
1010 sprintf(peer, " peer %s/%d ", lbuf_ifa, ifa->ifa_prefixlen); | |
1011 } | |
1012 } | |
1013 | |
1014 if (addrinfo.to && strcmp(addrinfo.addr, lbuf)) | |
1015 return 0; | |
1016 | |
1017 if (rta_tb[IFA_BROADCAST]) { | |
1018 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_BROADCAST]), | |
1019 lbuf, sizeof(lbuf))) perror_exit("inet"); | |
1020 sprintf(brd, " brd %s", lbuf); | |
1021 }else brd = ""; | |
1022 | |
1023 if (rta_tb[IFA_ANYCAST]) { | |
1024 if (!inet_ntop(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ANYCAST]), | |
1025 lbuf, sizeof(lbuf))) perror_exit("inet"); | |
1026 sprintf(any, " any %s", lbuf); | |
1027 } | |
1028 | |
1029 if (ifa->ifa_family == AF_INET) | |
1030 printf("%s%s%s%s%s %c", family, brd, peer, scope, label, | |
1031 (TT.singleline? '\0' : '\n')); | |
1032 else printf("%s%s %c", family, scope, (TT.singleline? '\0' : '\n')); | |
1033 if (TT.singleline && (ifa->ifa_family == AF_INET)) xputc('\n'); | |
1034 | |
1035 if (rta_tb[IFA_CACHEINFO]) { | |
1036 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); | |
1037 | |
1038 printf("%c valid_lft ", (TT.singleline? '\\' : '\0')); | |
1039 if (ci->ifa_valid == 0xFFFFFFFFU) printf("forever"); | |
1040 else printf("%usec", ci->ifa_valid); | |
1041 printf(" preferred_lft "); | |
1042 if (ci->ifa_prefered == 0xFFFFFFFFU) printf("forever"); | |
1043 else printf("%dsec", ci->ifa_prefered); | |
1044 xputc('\n'); | |
1045 } | |
1046 return 0; | |
1047 } | |
1048 | |
1049 static int ipaddrupdate(char **argv) | |
1050 { | |
1051 int length, cmd = !memcmp("add", argv[-1], strlen(argv[-1])) | |
1052 ? RTM_NEWADDR: RTM_DELADDR; | |
1053 int idx = 0,length_brd = 0, length_peer = 0,length_any = 0,length_local = 0, | |
1054 scoped = 0; | |
1055 char *dev = NULL,*label = NULL, reply[8192]; | |
1056 | |
1057 unsigned scope; | |
1058 struct nlmsghdr *addr_ptr = NULL; | |
1059 struct nlmsgerr *err = NULL; | |
1060 struct arglist cmd_objectlist[] = {{"dev",0}, {"peer", 1}, | |
1061 {"remote", 2}, {"broadcast", 3}, {"brd", 4}, {"label", 5}, | |
1062 {"anycast", 6},{"scope", 7}, {"local", 8}, {NULL, -1}}; | |
1063 struct{ | |
1064 struct nlmsghdr nlm; | |
1065 struct ifaddrmsg ifadd; | |
1066 char buf[256]; | |
1067 } req; | |
1068 typedef struct{ | |
1069 int family, bytelen, bitlen; | |
1070 __u32 data[8]; | |
1071 }option_data; | |
1072 option_data local; | |
1073 | |
1074 memset(&req, 0, sizeof(req)); | |
1075 req.nlm.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
1076 req.nlm.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; | |
1077 req.nlm.nlmsg_type = cmd; | |
1078 req.ifadd.ifa_family = TT.addressfamily; | |
1079 | |
1080 while(*argv) { | |
1081 idx = substring_to_idx(*argv, cmd_objectlist); | |
1082 if (idx >= 0) | |
1083 if (!*++argv) | |
1084 error_exit("Incomplete Command line"); | |
1085 switch(idx) { | |
1086 case 0: | |
1087 dev = *argv; | |
1088 break; | |
1089 case 1: | |
1090 case 2: | |
1091 { | |
1092 uint32_t addr[4] = {0,}, netmask = 0; | |
1093 uint8_t len = 0; | |
1094 parse_prefix(addr, &netmask, &len, *argv, | |
1095 req.ifadd.ifa_family); | |
1096 if (len) | |
1097 req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6); | |
1098 length_peer = len; | |
1099 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1100 IFA_ADDRESS, addr, len); | |
1101 req.ifadd.ifa_prefixlen = netmask; | |
1102 } | |
1103 break; | |
1104 case 3: | |
1105 case 4: | |
1106 if (*argv[0] == '+') { | |
1107 length_brd = -1; | |
1108 } else if (*argv[0] == '-') { | |
1109 length_brd = -2; | |
1110 } else { | |
1111 uint32_t addr[4] = {0,}; | |
1112 uint8_t af = AF_UNSPEC; | |
1113 | |
1114 if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family)) | |
1115 error_exit("Invalid prefix"); | |
1116 | |
1117 length_brd = ((af == AF_INET6) ? 16 : 4); | |
1118 if (req.ifadd.ifa_family == AF_UNSPEC) | |
1119 req.ifadd.ifa_family = af; | |
1120 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1121 IFA_BROADCAST, &addr, length_brd); | |
1122 } | |
1123 break; | |
1124 case 5: | |
1125 label = *argv; | |
1126 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1127 IFA_LABEL, label, strlen(label) + 1); | |
1128 break; | |
1129 case 6: | |
1130 { | |
1131 uint32_t addr[4] = {0,}; | |
1132 uint8_t af = AF_UNSPEC; | |
1133 | |
1134 if (get_prefix(addr, &af, *argv, req.ifadd.ifa_family)) | |
1135 error_exit("Invalid prefix"); | |
1136 length_any = ((af == AF_INET6) ? 16 : 4); | |
1137 if (req.ifadd.ifa_family == AF_UNSPEC) | |
1138 req.ifadd.ifa_family = af; | |
1139 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1140 IFA_ANYCAST, &addr, length_brd); | |
1141 } | |
1142 break; | |
1143 case 7: | |
1144 scope = idxfromRPDB(*argv, RPDB_rtscopes); | |
1145 if (scope){ | |
1146 if (!strcmp(*argv, "scope")) | |
1147 error_exit("wrong scope '%s'", *argv); | |
1148 } | |
1149 req.ifadd.ifa_scope = scope; | |
1150 scoped =1; | |
1151 break; | |
1152 default: | |
1153 { | |
1154 //local is by default | |
1155 uint32_t addr[8] = {0,}, netmask = 0; | |
1156 uint8_t len = 0; | |
1157 | |
1158 parse_prefix(addr, &netmask, &len, *argv, | |
1159 req.ifadd.ifa_family); | |
1160 if (len) | |
1161 req.ifadd.ifa_family = ((len == 4) ? AF_INET : AF_INET6); | |
1162 length_local = len; | |
1163 local.bitlen = netmask; | |
1164 local.bytelen = len; | |
1165 memcpy(local.data, addr, sizeof(local.data)); | |
1166 local.family = req.ifadd.ifa_family; | |
1167 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1168 IFA_LOCAL, &local.data, local.bytelen); | |
1169 } | |
1170 break; | |
1171 } | |
1172 argv++; | |
1173 } | |
1174 if (!dev) error_exit("need \"dev \" argument"); | |
1175 if (label && strncmp(dev, label, strlen(dev)) != 0) | |
1176 error_exit("\"dev\" (%s) must match \"label\" (%s)", dev, label); | |
1177 | |
1178 if (length_peer == 0 && length_local && cmd != RTM_DELADDR){ | |
1179 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1180 IFA_ADDRESS, &local.data, local.bytelen); | |
1181 } | |
1182 | |
1183 if (length_brd < 0 && cmd != RTM_DELADDR){ | |
1184 int i; | |
1185 | |
1186 if (req.ifadd.ifa_family != AF_INET) | |
1187 error_exit("broadcast can be set only for IPv4 addresses"); | |
1188 | |
1189 if (local.bitlen <= 30) { | |
1190 for (i = 31; i >= local.bitlen; i--) { | |
1191 if (length_brd == -1) | |
1192 local.data[0] |= htonl(1<<(31-i)); | |
1193 else | |
1194 local.data[0] &= ~htonl(1<<(31-i)); | |
1195 } | |
1196 add_string_to_rtattr(&req.nlm, sizeof(req), | |
1197 IFA_BROADCAST, &local.data, local.bytelen); | |
1198 length_brd = local.bytelen; | |
1199 } | |
1200 } | |
1201 if (req.ifadd.ifa_prefixlen == 0) | |
1202 req.ifadd.ifa_prefixlen = local.bitlen; | |
1203 if (!scoped && (cmd != RTM_DELADDR) && (local.family == AF_INET) | |
1204 && (local.bytelen >= 1 && *(uint8_t*)&local.data == 127)) | |
1205 req.ifadd.ifa_scope = RT_SCOPE_HOST; | |
1206 req.ifadd.ifa_index = get_ifaceindex(dev, 1); | |
1207 | |
1208 send_nlmesg(RTM_NEWADDR, 0, AF_UNSPEC, (void *)&req, req.nlm.nlmsg_len); | |
1209 length = recv(TT.sockfd, reply, sizeof(reply), 0); | |
1210 addr_ptr = (struct nlmsghdr *) reply; | |
1211 for (; NLMSG_OK(addr_ptr, length); addr_ptr = NLMSG_NEXT(addr_ptr, length)) { | |
1212 if (addr_ptr->nlmsg_type == NLMSG_DONE) | |
1213 return 1; | |
1214 if (addr_ptr->nlmsg_type == NLMSG_ERROR) | |
1215 err = (struct nlmsgerr*) NLMSG_DATA(addr_ptr); | |
1216 if (err->error != 0){ | |
1217 errno = -err->error; | |
1218 perror_exit("RTNETLINK answers:"); | |
1219 } | |
1220 } | |
1221 return 0; | |
1222 } | |
1223 | |
1224 static int ipaddr_listflush(char **argv) | |
1225 { | |
1226 int idx; uint32_t netmask = 0, found = 0; | |
1227 char *tmp = NULL, *name = NULL; | |
1228 struct double_list *dlist; | |
1229 struct arglist cmd_objectlist[] = {{"to", 0}, {"scope", 1}, {"up", 2}, | |
1230 {"label", 3}, {"dev", 4}, {NULL, -1}}; | |
1231 | |
1232 TT.flush = *argv[-1] == 'f' ? 1 : 0; | |
1233 memset(&addrinfo, 0, sizeof(addrinfo)); | |
1234 | |
1235 if (TT.flush) { | |
1236 if (!*argv) | |
1237 error_exit("Incomplete command for \"flush\""); | |
1238 if (TT.addressfamily == AF_PACKET) | |
1239 error_exit("Can't flush link Addressess"); | |
1240 } | |
1241 addrinfo.scope = -1; | |
1242 while (*argv) { | |
1243 unsigned scope = 0; | |
1244 | |
1245 switch (idx = substring_to_idx(*argv, cmd_objectlist)) { | |
1246 case 0: | |
1247 {// ADDR_TO | |
1248 if (!*++argv) error_exit("Incomplete Command line"); | |
1249 else if(!strcmp(*argv, "0")) return 0; | |
1250 uint32_t addr[4] = {0,}; | |
1251 uint8_t len = 0; | |
1252 | |
1253 addrinfo.to = 1; | |
1254 parse_prefix(addr, &netmask, &len, *argv, TT.addressfamily); | |
1255 if (len) | |
1256 TT.addressfamily = ((len == 4) ? AF_INET : AF_INET6); | |
1257 addrinfo.addr = strtok(*argv, "/"); | |
1258 } | |
1259 break; | |
1260 case 1: // ADDR_SCOPE | |
1261 if (!*++argv) error_exit("Incomplete Command line"); | |
1262 name = *argv; | |
1263 | |
1264 addrinfo.scopemask = -1; | |
1265 if (isdigit(**argv)) { | |
1266 int idx = atolx(*argv); | |
1267 | |
1268 name = xstrdup(namefromRPDB(idx, RPDB_rtscopes)); | |
1269 } | |
1270 scope = idxfromRPDB(name, RPDB_rtscopes); | |
1271 | |
1272 if (!scope && strcmp(name, "global")) { | |
1273 if (strcmp(name, "all")) | |
1274 error_exit("wrong scope '%s'", name); | |
1275 scope = RT_SCOPE_NOWHERE; | |
1276 addrinfo.scopemask = 0; | |
1277 } | |
1278 if (isdigit(**argv)) | |
1279 free(name); | |
1280 addrinfo.scope = scope; | |
1281 break; | |
1282 case 2: // ADDR_UP | |
1283 addrinfo.up = 1; | |
1284 break; | |
1285 case 3: // ADDR_LABEL | |
1286 if (!*++argv) error_exit("Incomplete Command line"); | |
1287 addrinfo.label = *argv; | |
1288 break; | |
1289 case 4: // ADDR_DEV | |
1290 if (!*++argv) error_exit("Incomplete Command line"); | |
1291 | |
1292 default: | |
1293 if (TT.filter_dev) | |
1294 error_exit("Either \"dev\" is duplicate or %s is garbage", | |
1295 *argv); | |
1296 TT.filter_dev = *argv; | |
1297 break; | |
1298 } | |
1299 argv++; | |
1300 } | |
1301 | |
1302 link_show(&tmp); | |
1303 while ( linfo && (dlist = dlist_pop(&linfo))){ | |
1304 struct linkdata *tmp = (struct linkdata*) dlist; | |
1305 char *temp = &tmp->iface[0]; | |
1306 | |
1307 if (TT.filter_dev && strcmp(TT.filter_dev, temp)) | |
1308 continue; | |
1309 found = 1; | |
1310 if (TT.flush && addrinfo.label) ipaddr_print( tmp, 0); | |
1311 if (addrinfo.up && !(tmp->flags & IFF_UP)){ | |
1312 ipaddr_print(tmp, 0); | |
1313 continue; | |
1314 } | |
1315 if (addrinfo.label){ | |
1316 if ( fnmatch(addrinfo.label, temp, 0)) { | |
1317 ipaddr_print(tmp, 1); | |
1318 continue; | |
1319 } | |
1320 } | |
1321 if (!TT.addressfamily && ! TT.flush ) print_link_output(tmp); | |
1322 | |
1323 ipaddr_print(tmp, 0); | |
1324 free(tmp); | |
1325 } | |
1326 if (TT.filter_dev && !found) | |
1327 error_exit("Device \"%s\" doesn't exist. \n", TT.filter_dev); | |
1328 return 0; | |
1329 } | |
1330 | |
1331 static int ipaddr_print( struct linkdata *link, int flag_l) | |
1332 { | |
1333 struct nlmsghdr *addr_ptr; | |
1334 int ip_match = 0; | |
1335 | |
1336 addrinfo.ifindex = link->iface_idx; | |
1337 send_nlmesg(RTM_GETADDR, NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST, | |
1338 AF_UNSPEC, NULL, 0); | |
1339 if (TT.addressfamily == AF_PACKET) print_link_output(link); | |
1340 | |
1341 if(addrinfo.label){ | |
1342 char *col = strchr(addrinfo.label, ':'); | |
1343 if (!col && (fnmatch(addrinfo.label, &link->iface[0], 0))) | |
1344 return 0; | |
1345 } | |
1346 | |
1347 while (1){ | |
1348 int len = recv(TT.sockfd, TT.gbuf, sizeof(TT.gbuf), 0); | |
1349 addr_ptr = (struct nlmsghdr *)TT.gbuf; | |
1350 struct ifaddrmsg *addressInfo = NLMSG_DATA(addr_ptr); | |
1351 char lbuf[INET6_ADDRSTRLEN]; | |
1352 struct rtattr *rta, *rta_tb[IFA_MAX+1] = {0,}; | |
1353 | |
1354 int len1 = addr_ptr->nlmsg_len - NLMSG_LENGTH(sizeof(*addressInfo)); | |
1355 if (len1 > 0) { | |
1356 for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) { | |
1357 addressInfo = NLMSG_DATA(addr_ptr); | |
1358 if (TT.addressfamily && TT.addressfamily != addressInfo->ifa_family) | |
1359 continue; | |
1360 if (addrinfo.ifindex && addrinfo.ifindex != addressInfo->ifa_index) | |
1361 continue; | |
1362 | |
1363 if(addrinfo.to){ | |
1364 memset(rta_tb, 0, sizeof(rta_tb)); | |
1365 int rt_len = IFA_PAYLOAD(addr_ptr); | |
1366 for (rta = IFA_RTA(addressInfo); RTA_OK(rta, rt_len); rta=RTA_NEXT(rta, rt_len)) { | |
1367 if (rta->rta_type <= IFA_MAX) rta_tb[rta->rta_type] = rta; | |
1368 } | |
1369 if (!rta_tb[IFA_LOCAL]) rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; | |
1370 if (rta_tb[IFA_LOCAL]) { | |
1371 if (!inet_ntop(TT.addressfamily, RTA_DATA(rta_tb[IFA_LOCAL]), | |
1372 lbuf, sizeof(lbuf))) perror_exit("inet"); | |
1373 if (strcmp(addrinfo.addr, lbuf)) | |
1374 continue; | |
1375 ip_match=1; | |
1376 } | |
1377 if(!ip_match) | |
1378 continue; | |
1379 } | |
1380 | |
1381 if(!TT.flush){ | |
1382 if (addrinfo.scope != -1 && TT.addressfamily && TT.addressfamily == | |
1383 addressInfo->ifa_family && | |
1384 (addrinfo.ifindex == addressInfo->ifa_index)) { | |
1385 if(addrinfo.scope != addressInfo->ifa_scope) | |
1386 continue; | |
1387 else if (addrinfo.up && (link->flags & IFF_UP)) | |
1388 print_link_output(link); | |
1389 else if (!addrinfo.up) print_link_output(link); | |
1390 } | |
1391 if (TT.addressfamily && | |
1392 (addrinfo.ifindex == addressInfo->ifa_index) && | |
1393 (addrinfo.scope == -1)){ | |
1394 if (addrinfo.up && (link->flags & IFF_UP)) | |
1395 print_link_output(link); | |
1396 else if (!addrinfo.up) print_link_output(link); | |
1397 } | |
1398 } | |
1399 for (; NLMSG_OK(addr_ptr, len); addr_ptr = NLMSG_NEXT(addr_ptr, len)) { | |
1400 if ((addr_ptr->nlmsg_type == RTM_NEWADDR)) | |
1401 print_addrinfo(addr_ptr, flag_l); | |
1402 if ((addr_ptr->nlmsg_type == NLMSG_DONE) || | |
1403 (addr_ptr->nlmsg_type == NLMSG_ERROR) || | |
1404 (TT.flush && addrinfo.to)) | |
1405 goto ret_stop; | |
1406 } | |
1407 } | |
1408 } | |
1409 else | |
1410 return 0; | |
1411 } | |
1412 | |
1413 ret_stop: | |
1414 return 0; | |
1415 } | |
1416 | |
1417 static int ipaddr(char **argv) | |
1418 { | |
1419 int idx; | |
1420 cmdobj ipcmd, cmdobjlist[] = {ipaddrupdate, ipaddr_listflush}; | |
1421 struct arglist cmd_objectlist[] = { {"add", 0}, {"delete", 0}, | |
1422 {"list", 1},{"show", 1},{"lst", 1}, {"flush", 1}, {NULL,-1}}; | |
1423 | |
1424 TT.is_addr++; | |
1425 if (!*argv) idx = 1; | |
1426 else if ((idx = substring_to_idx(*argv++, cmd_objectlist)) == -1) iphelp(); | |
1427 | |
1428 ipcmd = cmdobjlist[idx]; | |
1429 return ipcmd(argv); | |
1430 } | |
1431 | |
1432 // =========================================================================== | |
1433 // TODO: code for ip route. | |
1434 // =========================================================================== | |
1435 static int iproute(char **argv) | |
1436 { | |
1437 printf("__FUNCTION__ = %s\n", __FUNCTION__); | |
1438 return 0; | |
1439 } | |
1440 | |
1441 // =========================================================================== | |
1442 // code for ip rule. | |
1443 // =========================================================================== | |
1444 static void show_iprule_help(void) | |
1445 { | |
1446 char *errmsg = "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n" | |
1447 "SELECTOR := [ from PREFIX ] [ to PREFIX ] [pref NUMBER] [ tos TOS ]\n" | |
1448 " [ fwmark FWMARK] [ dev/iif STRING ] [type TYPE]\n" | |
1449 "ACTION := [ table TABLE_ID ] [ realms [SRCREALM/]DSTREALM ]"; | |
1450 | |
1451 error_exit(errmsg); | |
1452 } | |
1453 | |
1454 static int ruleupdate(char **argv) | |
1455 { | |
1456 int8_t idx, tflag = 0, opt = (*argv[-1] == 'a') ? RTM_NEWRULE : RTM_DELRULE; | |
1457 struct arglist options[] = {{"from", 0}, {"to", 1}, {"preference", 2}, | |
1458 {"order", 2}, {"priority", 2}, {"tos", 3}, {"dsfield", 3}, {"fwmark", 4}, | |
1459 {"realms", 5}, {"table", 6}, {"lookup", 6}, {"dev", 7}, {"iif", 7}, | |
1460 {"nat", 8}, {"map-to", 8}, {"type", 9}, {"help", 10}, {NULL, -1}}; | |
1461 struct { | |
1462 struct nlmsghdr mhdr; | |
1463 struct rtmsg msg; | |
1464 char buf[1024]; | |
1465 } request; | |
1466 | |
1467 memset(&request, 0, sizeof(request)); | |
1468 request.mhdr.nlmsg_type = opt; | |
1469 request.mhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
1470 request.mhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | | |
1471 ((opt == RTM_DELRULE) ? 0 : NLM_F_CREATE | NLM_F_EXCL); | |
1472 request.msg.rtm_family = TT.addressfamily; | |
1473 request.msg.rtm_protocol = RTPROT_BOOT; | |
1474 request.msg.rtm_scope = RT_SCOPE_UNIVERSE; | |
1475 request.msg.rtm_table = 0; | |
1476 request.msg.rtm_type = ((opt == RTM_DELRULE) ? RTN_UNSPEC : RTN_UNICAST); | |
1477 | |
1478 for (; *argv; argv++) { | |
1479 switch ((idx = substring_to_idx(*argv, options))) { | |
1480 case 0: | |
1481 case 1: | |
1482 { // e.g. from IP/Netmask and to IP/Netmask. | |
1483 uint32_t addr[4] = {0,}, netmask = 0; | |
1484 uint8_t len = 0, *tmp; | |
1485 | |
1486 if (!*++argv) error_exit("'%s': Missing Prefix", argv[-1]); | |
1487 parse_prefix(addr, &netmask, &len, *argv, request.msg.rtm_family); | |
1488 | |
1489 tmp = idx ? &request.msg.rtm_dst_len : &request.msg.rtm_src_len; | |
1490 if (!netmask) *tmp = 0; | |
1491 else *tmp = netmask; | |
1492 | |
1493 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
1494 (idx ? RTA_DST : RTA_SRC), addr, len); | |
1495 } | |
1496 break; | |
1497 case 2: | |
1498 case 4: | |
1499 { // e.g. Preference p# and fwmark MARK | |
1500 uint32_t pref; | |
1501 char *ptr; | |
1502 | |
1503 if (!*++argv) | |
1504 error_exit("Missing %s", (idx == 2) ? "Preference" : "fwmark"); | |
1505 pref = strtoul(*argv, &ptr, 0); | |
1506 if (!ptr || (ptr == *argv) || *ptr || pref > 0xFFFFFFFFUL) | |
1507 error_exit("Invalid %s", (idx == 2) ? "Preference" : "fwmark"); | |
1508 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
1509 ((idx == 2) ? RTA_PRIORITY : RTA_PROTOINFO), | |
1510 (void *)&pref, sizeof(uint32_t)); | |
1511 } | |
1512 break; | |
1513 case 3: | |
1514 { | |
1515 if (!*++argv) error_exit("Missing TOS key"); | |
1516 request.msg.rtm_tos = idxfromRPDB(*argv, RPDB_rtdsfield); | |
1517 } | |
1518 break; | |
1519 case 5: | |
1520 { // e.g. realms FROM_realm/TO_realm | |
1521 uint32_t realms = 0; | |
1522 char *ptr; | |
1523 | |
1524 if (!*++argv) error_exit("Missing REALMSID"); | |
1525 if ((ptr = strchr(*argv, '/'))) { | |
1526 *ptr = 0; | |
1527 realms = idxfromRPDB(*argv, RPDB_rtrealms); | |
1528 realms <<= 16; | |
1529 *ptr++ = '/'; | |
1530 } else ptr = *argv; | |
1531 realms |= idxfromRPDB(ptr, RPDB_rtrealms); | |
1532 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
1533 RTA_FLOW, (void *)&realms, sizeof(uint32_t)); | |
1534 } | |
1535 break; | |
1536 case 6: | |
1537 { // e.g. table tid/tableName | |
1538 if (!*++argv) error_exit("Missing TableID"); | |
1539 request.msg.rtm_table = idxfromRPDB(*argv, RPDB_rttables); | |
1540 tflag = 1; | |
1541 } | |
1542 break; | |
1543 case 7: | |
1544 { | |
1545 if (!*++argv) error_exit("Missing dev/iif NAME"); | |
1546 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
1547 RTA_IIF, *argv, strlen(*argv)+1); | |
1548 } | |
1549 break; | |
1550 case 8: | |
1551 { | |
1552 uint32_t addr[4] = {0,}; | |
1553 uint8_t af = AF_UNSPEC; | |
1554 | |
1555 if (!*++argv) error_exit("Missing nat/map-to ADDRESS"); | |
1556 if (get_prefix(addr, &af /* Un-used variable */, *argv, AF_INET)) | |
1557 error_exit("Invalid mapping Address"); | |
1558 | |
1559 add_string_to_rtattr(&request.mhdr, sizeof(request), | |
1560 RTA_GATEWAY, addr, sizeof(uint32_t)); | |
1561 request.msg.rtm_type = RTN_NAT; | |
1562 } | |
1563 break; | |
1564 case 9: | |
1565 { | |
1566 if (!*++argv) error_exit("TYPE Missing"); | |
1567 request.msg.rtm_type = rtmtype_str2idx(*argv); | |
1568 } | |
1569 break; | |
1570 case 10: | |
1571 show_iprule_help(); | |
1572 break; // Unreachable code. | |
1573 default: | |
1574 error_exit("Invalid argument '%s'", *argv); | |
1575 break; // Unreachable code. | |
1576 } | |
1577 } | |
1578 | |
1579 if (!request.msg.rtm_family) request.msg.rtm_family = AF_INET; | |
1580 if (!tflag && opt == RTM_NEWRULE) request.msg.rtm_table = RT_TABLE_MAIN; | |
1581 | |
1582 send_nlmesg(0, 0, 0, &request, sizeof(request)); | |
1583 return (filter_nlmesg(NULL, NULL)); | |
1584 } | |
1585 | |
1586 static int show_rules(struct nlmsghdr *mhdr, | |
1587 char **argv __attribute__ ((__unused__))) | |
1588 { | |
1589 struct rtmsg *msg = NLMSG_DATA(mhdr); | |
1590 struct rtattr *rta, *attr[RTA_MAX+1] = {0,}; | |
1591 int32_t tvar, msglen = mhdr->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg)); | |
1592 int hlen = ((msg->rtm_family == AF_INET) ? 32 | |
1593 : ((msg->rtm_family == AF_INET6) ? 128 : -1)); | |
1594 | |
1595 if (mhdr->nlmsg_type != RTM_NEWRULE) return 0; | |
1596 if (msglen < 0) return 1; | |
1597 | |
1598 tvar = msglen; | |
1599 for (rta = RTM_RTA(msg); RTA_OK(rta, tvar); rta=RTA_NEXT(rta, tvar)) | |
1600 if (rta->rta_type <= RTA_MAX) attr[rta->rta_type] = rta; | |
1601 | |
1602 if (tvar) error_msg("deficit %d, rtalen = %d!", tvar, rta->rta_len); | |
1603 | |
1604 printf("%u:\tfrom ", attr[RTA_PRIORITY] ? | |
1605 *(unsigned *)RTA_DATA(attr[RTA_PRIORITY]) : 0); | |
1606 | |
1607 if (attr[RTA_SRC]) { | |
1608 printf("%s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6) | |
1609 ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_SRC]), | |
1610 toybuf, sizeof(toybuf)) | |
1611 : "???"); | |
1612 (msg->rtm_src_len != hlen) ? printf("/%u", msg->rtm_src_len) : 0; | |
1613 } else msg->rtm_src_len ? printf("0/%d", msg->rtm_src_len) : printf("all"); | |
1614 | |
1615 xputc(' '); | |
1616 if (attr[RTA_DST]) { | |
1617 printf("to %s", (msg->rtm_family == AF_INET || msg->rtm_family == AF_INET6) | |
1618 ? inet_ntop(msg->rtm_family, RTA_DATA(attr[RTA_DST]), | |
1619 toybuf, sizeof(toybuf)) : "???"); | |
1620 (msg->rtm_dst_len != hlen) ? printf("/%u", msg->rtm_dst_len) : xputc(' '); | |
1621 } else if (msg->rtm_dst_len) | |
1622 printf("to 0/%d ", msg->rtm_dst_len); | |
1623 | |
1624 if (msg->rtm_tos) | |
1625 printf("tos %s ", namefromRPDB(msg->rtm_tos, RPDB_rtdsfield)); | |
1626 | |
1627 if (attr[RTA_PROTOINFO]) | |
1628 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(attr[RTA_PROTOINFO])); | |
1629 | |
1630 if (attr[RTA_IIF]) printf("iif %s ", (char*)RTA_DATA(attr[RTA_IIF])); | |
1631 | |
1632 if (msg->rtm_table) | |
1633 printf("lookup %s ", namefromRPDB(msg->rtm_table, RPDB_rttables)); | |
1634 | |
1635 if (attr[RTA_FLOW]) { | |
1636 u_int32_t from, to = *(u_int32_t *)RTA_DATA(attr[RTA_FLOW]); | |
1637 char *format = "realms %s/"; | |
1638 | |
1639 to = (from = (to >> 16)) & 0xFFFF; | |
1640 format = (from ? format: "%s"); | |
1641 printf(format, namefromRPDB((from ? from : to), RPDB_rtrealms)); | |
1642 } | |
1643 | |
1644 if (msg->rtm_type == RTN_NAT) { | |
1645 if (!attr[RTA_GATEWAY]) printf("masquerade"); | |
1646 else printf("map-to %s ", inet_ntop(msg->rtm_family, | |
1647 RTA_DATA(attr[RTA_GATEWAY]), toybuf, sizeof(toybuf))); | |
1648 } else if (msg->rtm_type != RTN_UNICAST) | |
1649 printf("%s", rtmtype_idx2str(msg->rtm_type)); | |
1650 | |
1651 xputc('\n'); | |
1652 return 0; | |
1653 } | |
1654 | |
1655 static int rulelist(char **argv) | |
1656 { | |
1657 if (*argv) { | |
1658 error_msg("'ip rule show' does not take any arguments."); | |
1659 return 1; | |
1660 } | |
1661 send_nlmesg(RTM_GETRULE, NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST, | |
1662 ((TT.addressfamily != AF_UNSPEC) ? TT.addressfamily : AF_INET), NULL, 0); | |
1663 return filter_nlmesg(show_rules, argv); | |
1664 } | |
1665 | |
1666 static int iprule(char **argv) | |
1667 { | |
1668 int idx; | |
1669 struct arglist options[] = {{"add", 0}, {"delete", 0}, {"list", 1}, | |
1670 {"show", 1}, {NULL, -1}}; | |
1671 cmdobj ipcmd, cmdobjlist[] = {ruleupdate, rulelist}; | |
1672 | |
1673 if (!*argv) idx = 1; | |
1674 else if ((idx = substring_to_idx(*argv++, options)) == -1) | |
1675 show_iprule_help(); | |
1676 ipcmd = cmdobjlist[idx]; | |
1677 return ipcmd(argv); | |
1678 } | |
1679 //============================================================================ | |
1680 // TODO: code for ip tunnel. | |
1681 //============================================================================ | |
1682 static int iptunnel(char **argv) | |
1683 { | |
1684 printf("__FUNCTION__ = %s\n", __FUNCTION__); | |
1685 return 0; | |
1686 } | |
1687 | |
1688 // =========================================================================== | |
1689 // Common code, which is used for all ip options. | |
1690 // =========================================================================== | |
1691 | |
1692 // Parse netlink messages and call input callback handler for action | |
1693 static int filter_nlmesg(int (*fun)(struct nlmsghdr *mhdr, char **argv), | |
1694 char **argv) | |
1695 { | |
1696 while (1) { | |
1697 struct nlmsghdr *mhdr; | |
1698 int msglen = recv(TT.sockfd, TT.gbuf, MESG_LEN, 0); | |
1699 | |
1700 if ((msglen < 0) && (errno == EINTR || errno == EAGAIN)) continue; | |
1701 else if (msglen < 0) { | |
1702 error_msg("netlink receive error %s", strerror(errno)); | |
1703 return 1; | |
1704 } else if (!msglen) { | |
1705 error_msg("EOF on netlink"); | |
1706 return 1; | |
1707 } | |
1708 | |
1709 for (mhdr = (struct nlmsghdr*)TT.gbuf; NLMSG_OK(mhdr, msglen); | |
1710 mhdr = NLMSG_NEXT(mhdr, msglen)) { | |
1711 int err; | |
1712 if (mhdr->nlmsg_pid != getpid()) | |
1713 continue; | |
1714 switch (mhdr->nlmsg_type) { | |
1715 case NLMSG_DONE: | |
1716 return 0; | |
1717 case NLMSG_ERROR: | |
1718 { | |
1719 struct nlmsgerr *merr = (struct nlmsgerr*)NLMSG_DATA(mhdr); | |
1720 | |
1721 if (merr->error == 0) return 0; | |
1722 if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) | |
1723 error_msg("ERROR truncated"); | |
1724 else { | |
1725 errno = -merr->error; | |
1726 perror_msg("RTNETLINK answers"); | |
1727 } | |
1728 return 1; | |
1729 } | |
1730 default: | |
1731 if (fun && (err = fun(mhdr, argv))) return err; | |
1732 break; | |
1733 } | |
1734 } // End of for loop. | |
1735 } // End of while loop. | |
1736 return 0; | |
1737 } | |
1738 | |
1739 void ip_main(void) | |
1740 { | |
1741 char **optargv = toys.argv; | |
1742 //TT.addressfamily = AF_UNSPEC; | |
1743 int idx, isip = !(toys.which->name[2]); //1 -> if only ip | |
1744 cmdobj ipcmd, cmdobjlist[] = {ipaddr, iplink, iproute, iprule, iptunnel}; | |
1745 | |
1746 for (++optargv; *optargv; ++optargv) { | |
1747 char *ptr = *optargv; | |
1748 struct arglist ip_options[] = {{"oneline", 0}, {"family", 1}, | |
1749 {"4", 1}, {"6", 1}, {"0", 1}, {"stats", 2}, {NULL, -1}}; | |
1750 | |
1751 if (*ptr != '-') break; | |
1752 else if ((*(ptr+1) == '-') && (*(ptr+2))) ptr +=2; | |
1753 //escape "--" and stop ip arg parsing. | |
1754 else if ((*(ptr+1) == '-') && (!*(ptr+2))) { | |
1755 *ptr +=1; | |
1756 break; | |
1757 } else ptr +=1; | |
1758 switch (substring_to_idx(ptr, ip_options)) { | |
1759 case 0: TT.singleline = 1; | |
1760 break; | |
1761 case 1: { | |
1762 if (isdigit(*ptr)) { | |
1763 long num = atolx(ptr); | |
1764 if (num == 4) TT.addressfamily = AF_INET; | |
1765 else if (num == 6) TT.addressfamily = AF_INET6; | |
1766 else TT.addressfamily = AF_PACKET; | |
1767 } else { | |
1768 struct arglist ip_aflist[] = {{"inet", AF_INET}, | |
1769 {"inet6", AF_INET6}, {"link", AF_PACKET}, {NULL, -1}}; | |
1770 | |
1771 if (!*++optargv) iphelp(); | |
1772 if ((TT.addressfamily = string_to_idx(*optargv, ip_aflist)) == -1) | |
1773 error_exit("wrong family '%s'", *optargv); | |
1774 } | |
1775 } | |
1776 break; | |
1777 case 2: | |
1778 TT.stats++; | |
1779 break; | |
1780 default: iphelp(); | |
1781 break; // unreachable code. | |
1782 } | |
1783 } | |
1784 | |
1785 TT.sockfd = xsocket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | |
1786 | |
1787 if (isip) {// only for ip | |
1788 if (*optargv) { | |
1789 struct arglist ip_objectlist[] = { {"address", 0}, {"link", 1}, | |
1790 {"route", 2}, {"rule", 3}, {"tunnel", 4}, {"tunl", 4}, {NULL, -1}}; | |
1791 | |
1792 if ((idx = substring_to_idx(*optargv, ip_objectlist)) == -1) iphelp(); | |
1793 ipcmd = cmdobjlist[idx]; | |
1794 toys.exitval = ipcmd(++optargv); | |
1795 } else iphelp(); | |
1796 } else { | |
1797 struct arglist ip_objectlist[] = { {"ipaddr", 0}, {"iplink", 1}, | |
1798 {"iproute", 2}, {"iprule", 3}, {"iptunnel", 4}, {NULL, -1}}; | |
1799 if ((idx = string_to_idx(toys.which->name, ip_objectlist)) == -1) | |
1800 iphelp(); | |
1801 ipcmd = cmdobjlist[idx]; | |
1802 toys.exitval = ipcmd(optargv); | |
1803 } | |
1804 xclose(TT.sockfd); | |
1805 } |