]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * ipneigh.c "ip neigh". | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
aba5acdf SH |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
aba5acdf SH |
16 | #include <fcntl.h> |
17 | #include <string.h> | |
18 | #include <sys/time.h> | |
aba5acdf SH |
19 | #include <sys/socket.h> |
20 | #include <netinet/in.h> | |
21 | #include <netinet/ip.h> | |
22 | ||
23 | #include "rt_names.h" | |
24 | #include "utils.h" | |
25 | #include "ip_common.h" | |
aac7f725 | 26 | #include "json_print.h" |
aba5acdf SH |
27 | |
28 | #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY) | |
66081849 | 29 | #define MAX_ROUNDS 10 |
aba5acdf SH |
30 | |
31 | static struct | |
32 | { | |
33 | int family; | |
56f5daac | 34 | int index; |
aba5acdf SH |
35 | int state; |
36 | int unused_only; | |
37 | inet_prefix pfx; | |
38 | int flushed; | |
39 | char *flushb; | |
40 | int flushp; | |
41 | int flushe; | |
0d238ca2 | 42 | int master; |
aba5acdf SH |
43 | } filter; |
44 | ||
45 | static void usage(void) __attribute__((noreturn)); | |
46 | ||
47 | static void usage(void) | |
48 | { | |
03a0cf20 PS |
49 | fprintf(stderr, "Usage: ip neigh { add | del | change | replace }\n" |
50 | " { ADDR [ lladdr LLADDR ] [ nud STATE ] | proxy ADDR } [ dev DEV ]\n"); | |
4c45b684 | 51 | fprintf(stderr, " [ router ] [ extern_learn ]\n\n"); |
5db1adae DA |
52 | fprintf(stderr, " ip neigh { show | flush } [ proxy ] [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n"); |
53 | fprintf(stderr, " [ vrf NAME ]\n\n"); | |
03a0cf20 | 54 | fprintf(stderr, "STATE := { permanent | noarp | stale | reachable | none |\n" |
56f5daac | 55 | " incomplete | delay | probe | failed }\n"); |
aba5acdf SH |
56 | exit(-1); |
57 | } | |
58 | ||
56f5daac | 59 | static int nud_state_a2n(unsigned int *state, const char *arg) |
aba5acdf SH |
60 | { |
61 | if (matches(arg, "permanent") == 0) | |
62 | *state = NUD_PERMANENT; | |
63 | else if (matches(arg, "reachable") == 0) | |
64 | *state = NUD_REACHABLE; | |
65 | else if (strcmp(arg, "noarp") == 0) | |
66 | *state = NUD_NOARP; | |
67 | else if (strcmp(arg, "none") == 0) | |
68 | *state = NUD_NONE; | |
69 | else if (strcmp(arg, "stale") == 0) | |
70 | *state = NUD_STALE; | |
71 | else if (strcmp(arg, "incomplete") == 0) | |
72 | *state = NUD_INCOMPLETE; | |
73 | else if (strcmp(arg, "delay") == 0) | |
74 | *state = NUD_DELAY; | |
75 | else if (strcmp(arg, "probe") == 0) | |
76 | *state = NUD_PROBE; | |
77 | else if (matches(arg, "failed") == 0) | |
78 | *state = NUD_FAILED; | |
79 | else { | |
80 | if (get_unsigned(state, arg, 0)) | |
81 | return -1; | |
56f5daac | 82 | if (*state >= 0x100 || (*state&((*state)-1))) |
aba5acdf SH |
83 | return -1; |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
aba5acdf SH |
88 | static int flush_update(void) |
89 | { | |
f31a37f7 | 90 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
1fb0a998 | 91 | perror("Failed to send flush request"); |
aba5acdf SH |
92 | return -1; |
93 | } | |
94 | filter.flushp = 0; | |
95 | return 0; | |
96 | } | |
97 | ||
98 | ||
99 | static int ipneigh_modify(int cmd, int flags, int argc, char **argv) | |
100 | { | |
aba5acdf | 101 | struct { |
4806867a SH |
102 | struct nlmsghdr n; |
103 | struct ndmsg ndm; | |
56f5daac | 104 | char buf[256]; |
d17b136f PS |
105 | } req = { |
106 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)), | |
107 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
108 | .n.nlmsg_type = cmd, | |
109 | .ndm.ndm_family = preferred_family, | |
110 | .ndm.ndm_state = NUD_PERMANENT, | |
111 | }; | |
e834eb8e | 112 | char *dev = NULL; |
aba5acdf | 113 | int dst_ok = 0; |
e834eb8e | 114 | int dev_ok = 0; |
aba5acdf | 115 | int lladdr_ok = 0; |
56f5daac | 116 | char *lla = NULL; |
aba5acdf SH |
117 | inet_prefix dst; |
118 | ||
aba5acdf SH |
119 | while (argc > 0) { |
120 | if (matches(*argv, "lladdr") == 0) { | |
121 | NEXT_ARG(); | |
122 | if (lladdr_ok) | |
123 | duparg("lladdr", *argv); | |
124 | lla = *argv; | |
125 | lladdr_ok = 1; | |
126 | } else if (strcmp(*argv, "nud") == 0) { | |
56f5daac SH |
127 | unsigned int state; |
128 | ||
aba5acdf SH |
129 | NEXT_ARG(); |
130 | if (nud_state_a2n(&state, *argv)) | |
131 | invarg("nud state is bad", *argv); | |
132 | req.ndm.ndm_state = state; | |
133 | } else if (matches(*argv, "proxy") == 0) { | |
134 | NEXT_ARG(); | |
135 | if (matches(*argv, "help") == 0) | |
136 | usage(); | |
137 | if (dst_ok) | |
138 | duparg("address", *argv); | |
139 | get_addr(&dst, *argv, preferred_family); | |
140 | dst_ok = 1; | |
e834eb8e | 141 | dev_ok = 1; |
aba5acdf | 142 | req.ndm.ndm_flags |= NTF_PROXY; |
c2cd14ac RP |
143 | } else if (strcmp(*argv, "router") == 0) { |
144 | req.ndm.ndm_flags |= NTF_ROUTER; | |
4c45b684 RP |
145 | } else if (matches(*argv, "extern_learn") == 0) { |
146 | req.ndm.ndm_flags |= NTF_EXT_LEARNED; | |
aba5acdf SH |
147 | } else if (strcmp(*argv, "dev") == 0) { |
148 | NEXT_ARG(); | |
e834eb8e KK |
149 | dev = *argv; |
150 | dev_ok = 1; | |
aba5acdf SH |
151 | } else { |
152 | if (strcmp(*argv, "to") == 0) { | |
153 | NEXT_ARG(); | |
154 | } | |
155 | if (matches(*argv, "help") == 0) { | |
156 | NEXT_ARG(); | |
157 | } | |
158 | if (dst_ok) | |
159 | duparg2("to", *argv); | |
160 | get_addr(&dst, *argv, preferred_family); | |
161 | dst_ok = 1; | |
162 | } | |
163 | argc--; argv++; | |
164 | } | |
e834eb8e | 165 | if (!dev_ok || !dst_ok || dst.family == AF_UNSPEC) { |
aba5acdf SH |
166 | fprintf(stderr, "Device and destination are required arguments.\n"); |
167 | exit(-1); | |
168 | } | |
169 | req.ndm.ndm_family = dst.family; | |
542b0cc7 SH |
170 | if (addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen) < 0) |
171 | return -1; | |
aba5acdf SH |
172 | |
173 | if (lla && strcmp(lla, "null")) { | |
7b565754 | 174 | char llabuf[20]; |
aba5acdf SH |
175 | int l; |
176 | ||
177 | l = ll_addr_a2n(llabuf, sizeof(llabuf), lla); | |
542b0cc7 SH |
178 | if (l < 0) |
179 | return -1; | |
180 | ||
181 | if (addattr_l(&req.n, sizeof(req), NDA_LLADDR, llabuf, l) < 0) | |
182 | return -1; | |
aba5acdf SH |
183 | } |
184 | ||
aba5acdf SH |
185 | ll_init_map(&rth); |
186 | ||
fe99adbc SP |
187 | if (dev) { |
188 | req.ndm.ndm_ifindex = ll_name_to_index(dev); | |
189 | if (!req.ndm.ndm_ifindex) | |
190 | return nodev(dev); | |
aba5acdf SH |
191 | } |
192 | ||
86bf43c7 | 193 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
aba5acdf SH |
194 | exit(2); |
195 | ||
351efcde | 196 | return 0; |
aba5acdf SH |
197 | } |
198 | ||
aac7f725 SH |
199 | static void print_cacheinfo(const struct nda_cacheinfo *ci) |
200 | { | |
201 | static int hz; | |
202 | ||
203 | if (!hz) | |
204 | hz = get_user_hz(); | |
205 | ||
206 | if (ci->ndm_refcnt) | |
207 | print_uint(PRINT_ANY, "refcnt", | |
208 | " ref %u", ci->ndm_refcnt); | |
209 | ||
210 | print_uint(PRINT_ANY, "used", " used %u", ci->ndm_used / hz); | |
211 | print_uint(PRINT_ANY, "confirmed", "/%u", ci->ndm_confirmed / hz); | |
7cd3f08b | 212 | print_uint(PRINT_ANY, "updated", "/%u", ci->ndm_updated / hz); |
aac7f725 SH |
213 | } |
214 | ||
215 | static void print_neigh_state(unsigned int nud) | |
216 | { | |
217 | ||
218 | open_json_array(PRINT_JSON, | |
219 | is_json_context() ? "state" : ""); | |
220 | ||
221 | #define PRINT_FLAG(f) \ | |
222 | if (nud & NUD_##f) { \ | |
223 | nud &= ~NUD_##f; \ | |
224 | print_string(PRINT_ANY, NULL, " %s", #f); \ | |
225 | } | |
226 | ||
227 | PRINT_FLAG(INCOMPLETE); | |
228 | PRINT_FLAG(REACHABLE); | |
229 | PRINT_FLAG(STALE); | |
230 | PRINT_FLAG(DELAY); | |
231 | PRINT_FLAG(PROBE); | |
232 | PRINT_FLAG(FAILED); | |
233 | PRINT_FLAG(NOARP); | |
234 | PRINT_FLAG(PERMANENT); | |
235 | #undef PRINT_FLAG | |
236 | ||
237 | close_json_array(PRINT_JSON, NULL); | |
238 | } | |
aba5acdf | 239 | |
cd554f2c | 240 | int print_neigh(struct nlmsghdr *n, void *arg) |
aba5acdf | 241 | { |
56f5daac | 242 | FILE *fp = (FILE *)arg; |
aba5acdf SH |
243 | struct ndmsg *r = NLMSG_DATA(n); |
244 | int len = n->nlmsg_len; | |
56f5daac | 245 | struct rtattr *tb[NDA_MAX+1]; |
0d238ca2 | 246 | static int logit = 1; |
aba5acdf | 247 | |
1556e29d DS |
248 | if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH && |
249 | n->nlmsg_type != RTM_GETNEIGH) { | |
aba5acdf SH |
250 | fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", |
251 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
ae665a52 | 252 | |
aba5acdf SH |
253 | return 0; |
254 | } | |
255 | len -= NLMSG_LENGTH(sizeof(*r)); | |
256 | if (len < 0) { | |
257 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
258 | return -1; | |
259 | } | |
260 | ||
261 | if (filter.flushb && n->nlmsg_type != RTM_NEWNEIGH) | |
262 | return 0; | |
263 | ||
264 | if (filter.family && filter.family != r->ndm_family) | |
265 | return 0; | |
266 | if (filter.index && filter.index != r->ndm_ifindex) | |
267 | return 0; | |
268 | if (!(filter.state&r->ndm_state) && | |
1dac7817 | 269 | !(r->ndm_flags & NTF_PROXY) && |
9c6a6d84 | 270 | !(r->ndm_flags & NTF_EXT_LEARNED) && |
aba5acdf | 271 | (r->ndm_state || !(filter.state&0x100)) && |
aac7f725 | 272 | (r->ndm_family != AF_DECnet)) |
aba5acdf SH |
273 | return 0; |
274 | ||
0d238ca2 DA |
275 | if (filter.master && !(n->nlmsg_flags & NLM_F_DUMP_FILTERED)) { |
276 | if (logit) { | |
277 | logit = 0; | |
278 | fprintf(fp, | |
279 | "\nWARNING: Kernel does not support filtering by master device\n\n"); | |
280 | } | |
281 | } | |
282 | ||
aba5acdf SH |
283 | parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
284 | ||
a4270fd8 SP |
285 | if (inet_addr_match_rta(&filter.pfx, tb[NDA_DST])) |
286 | return 0; | |
56f5daac | 287 | |
aba5acdf SH |
288 | if (filter.unused_only && tb[NDA_CACHEINFO]) { |
289 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); | |
56f5daac | 290 | |
aba5acdf SH |
291 | if (ci->ndm_refcnt) |
292 | return 0; | |
293 | } | |
294 | ||
295 | if (filter.flushb) { | |
296 | struct nlmsghdr *fn; | |
56f5daac | 297 | |
aba5acdf SH |
298 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
299 | if (flush_update()) | |
300 | return -1; | |
301 | } | |
56f5daac | 302 | fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
aba5acdf SH |
303 | memcpy(fn, n, n->nlmsg_len); |
304 | fn->nlmsg_type = RTM_DELNEIGH; | |
305 | fn->nlmsg_flags = NLM_F_REQUEST; | |
351efcde | 306 | fn->nlmsg_seq = ++rth.seq; |
56f5daac | 307 | filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb; |
aba5acdf SH |
308 | filter.flushed++; |
309 | if (show_stats < 2) | |
310 | return 0; | |
311 | } | |
312 | ||
aac7f725 | 313 | open_json_object(NULL); |
6ea3ebaf | 314 | if (n->nlmsg_type == RTM_DELNEIGH) |
aac7f725 | 315 | print_bool(PRINT_ANY, "deleted", "Deleted ", true); |
1556e29d | 316 | else if (n->nlmsg_type == RTM_GETNEIGH) |
aac7f725 SH |
317 | print_null(PRINT_ANY, "miss", "%s ", "miss"); |
318 | ||
aba5acdf | 319 | if (tb[NDA_DST]) { |
aac7f725 SH |
320 | const char *dst; |
321 | ||
322 | dst = format_host_rta(r->ndm_family, tb[NDA_DST]); | |
323 | print_color_string(PRINT_ANY, | |
324 | ifa_family_color(r->ndm_family), | |
325 | "dst", "%s ", dst); | |
aba5acdf | 326 | } |
aac7f725 SH |
327 | |
328 | if (!filter.index && r->ndm_ifindex) { | |
329 | if (!is_json_context()) | |
330 | fprintf(fp, "dev "); | |
331 | ||
332 | print_color_string(PRINT_ANY, COLOR_IFNAME, | |
333 | "dev", "%s ", | |
334 | ll_index_to_name(r->ndm_ifindex)); | |
335 | } | |
336 | ||
aba5acdf | 337 | if (tb[NDA_LLADDR]) { |
aac7f725 | 338 | const char *lladdr; |
aba5acdf | 339 | SPRINT_BUF(b1); |
6a34d291 | 340 | |
aac7f725 SH |
341 | lladdr = ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), |
342 | RTA_PAYLOAD(tb[NDA_LLADDR]), | |
343 | ll_index_to_type(r->ndm_ifindex), | |
344 | b1, sizeof(b1)); | |
aba5acdf | 345 | |
aac7f725 SH |
346 | if (!is_json_context()) |
347 | fprintf(fp, "lladdr "); | |
56f5daac | 348 | |
aac7f725 SH |
349 | print_color_string(PRINT_ANY, COLOR_MAC, |
350 | "lladdr", "%s", lladdr); | |
69410a49 | 351 | } |
352 | ||
aac7f725 SH |
353 | if (r->ndm_flags & NTF_ROUTER) |
354 | print_null(PRINT_ANY, "router", " %s", "router"); | |
355 | ||
356 | if (r->ndm_flags & NTF_PROXY) | |
357 | print_null(PRINT_ANY, "proxy", " %s", "proxy"); | |
358 | ||
4c45b684 RP |
359 | if (r->ndm_flags & NTF_EXT_LEARNED) |
360 | print_null(PRINT_ANY, "extern_learn", " %s ", "extern_learn"); | |
361 | ||
aac7f725 SH |
362 | if (show_stats) { |
363 | if (tb[NDA_CACHEINFO]) | |
364 | print_cacheinfo(RTA_DATA(tb[NDA_CACHEINFO])); | |
365 | ||
366 | if (tb[NDA_PROBES]) | |
367 | print_uint(PRINT_ANY, "probes", " probes %u", | |
368 | rta_getattr_u32(tb[NDA_PROBES])); | |
aba5acdf | 369 | } |
aba5acdf | 370 | |
aac7f725 SH |
371 | if (r->ndm_state) |
372 | print_neigh_state(r->ndm_state); | |
373 | ||
374 | print_string(PRINT_FP, NULL, "\n", ""); | |
375 | close_json_object(); | |
376 | fflush(stdout); | |
377 | ||
aba5acdf SH |
378 | return 0; |
379 | } | |
380 | ||
093b7646 | 381 | void ipneigh_reset_filter(int ifindex) |
aba5acdf SH |
382 | { |
383 | memset(&filter, 0, sizeof(filter)); | |
384 | filter.state = ~0; | |
093b7646 | 385 | filter.index = ifindex; |
aba5acdf SH |
386 | } |
387 | ||
d1f28cf1 | 388 | static int do_show_or_flush(int argc, char **argv, int flush) |
aba5acdf | 389 | { |
0d238ca2 DA |
390 | struct { |
391 | struct nlmsghdr n; | |
392 | struct ndmsg ndm; | |
56f5daac | 393 | char buf[256]; |
d17b136f PS |
394 | } req = { |
395 | .n.nlmsg_type = RTM_GETNEIGH, | |
396 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)), | |
397 | }; | |
aba5acdf | 398 | char *filter_dev = NULL; |
aba5acdf | 399 | int state_given = 0; |
0d238ca2 | 400 | |
093b7646 | 401 | ipneigh_reset_filter(0); |
aba5acdf SH |
402 | |
403 | if (!filter.family) | |
404 | filter.family = preferred_family; | |
405 | ||
406 | if (flush) { | |
407 | if (argc <= 0) { | |
408 | fprintf(stderr, "Flush requires arguments.\n"); | |
409 | return -1; | |
410 | } | |
411 | filter.state = ~(NUD_PERMANENT|NUD_NOARP); | |
412 | } else | |
413 | filter.state = 0xFF & ~NUD_NOARP; | |
414 | ||
415 | while (argc > 0) { | |
416 | if (strcmp(*argv, "dev") == 0) { | |
417 | NEXT_ARG(); | |
418 | if (filter_dev) | |
419 | duparg("dev", *argv); | |
420 | filter_dev = *argv; | |
0d238ca2 DA |
421 | } else if (strcmp(*argv, "master") == 0) { |
422 | int ifindex; | |
56f5daac | 423 | |
0d238ca2 DA |
424 | NEXT_ARG(); |
425 | ifindex = ll_name_to_index(*argv); | |
426 | if (!ifindex) | |
427 | invarg("Device does not exist\n", *argv); | |
428 | addattr32(&req.n, sizeof(req), NDA_MASTER, ifindex); | |
429 | filter.master = ifindex; | |
5db1adae DA |
430 | } else if (strcmp(*argv, "vrf") == 0) { |
431 | int ifindex; | |
432 | ||
433 | NEXT_ARG(); | |
434 | ifindex = ll_name_to_index(*argv); | |
435 | if (!ifindex) | |
436 | invarg("Not a valid VRF name\n", *argv); | |
437 | if (!name_is_vrf(*argv)) | |
438 | invarg("Not a valid VRF name\n", *argv); | |
439 | addattr32(&req.n, sizeof(req), NDA_MASTER, ifindex); | |
440 | filter.master = ifindex; | |
aba5acdf SH |
441 | } else if (strcmp(*argv, "unused") == 0) { |
442 | filter.unused_only = 1; | |
443 | } else if (strcmp(*argv, "nud") == 0) { | |
56f5daac SH |
444 | unsigned int state; |
445 | ||
aba5acdf SH |
446 | NEXT_ARG(); |
447 | if (!state_given) { | |
448 | state_given = 1; | |
449 | filter.state = 0; | |
450 | } | |
451 | if (nud_state_a2n(&state, *argv)) { | |
452 | if (strcmp(*argv, "all") != 0) | |
453 | invarg("nud state is bad", *argv); | |
454 | state = ~0; | |
455 | if (flush) | |
456 | state &= ~NUD_NOARP; | |
457 | } | |
458 | if (state == 0) | |
459 | state = 0x100; | |
460 | filter.state |= state; | |
1dac7817 | 461 | } else if (strcmp(*argv, "proxy") == 0) |
0d238ca2 | 462 | req.ndm.ndm_flags = NTF_PROXY; |
1dac7817 | 463 | else { |
aba5acdf SH |
464 | if (strcmp(*argv, "to") == 0) { |
465 | NEXT_ARG(); | |
466 | } | |
467 | if (matches(*argv, "help") == 0) | |
468 | usage(); | |
a4270fd8 SP |
469 | if (get_prefix(&filter.pfx, *argv, filter.family)) |
470 | invarg("to value is invalid\n", *argv); | |
aba5acdf SH |
471 | if (filter.family == AF_UNSPEC) |
472 | filter.family = filter.pfx.family; | |
473 | } | |
474 | argc--; argv++; | |
475 | } | |
476 | ||
aba5acdf SH |
477 | ll_init_map(&rth); |
478 | ||
479 | if (filter_dev) { | |
fe99adbc SP |
480 | filter.index = ll_name_to_index(filter_dev); |
481 | if (!filter.index) | |
482 | return nodev(filter_dev); | |
b8c75324 | 483 | addattr32(&req.n, sizeof(req), NDA_IFINDEX, filter.index); |
aba5acdf SH |
484 | } |
485 | ||
1c346dcc JH |
486 | req.ndm.ndm_family = filter.family; |
487 | ||
aba5acdf SH |
488 | if (flush) { |
489 | int round = 0; | |
490 | char flushb[4096-512]; | |
491 | ||
492 | filter.flushb = flushb; | |
493 | filter.flushp = 0; | |
494 | filter.flushe = sizeof(flushb); | |
aba5acdf | 495 | |
66081849 | 496 | while (round < MAX_ROUNDS) { |
1c346dcc | 497 | if (rtnl_dump_request_n(&rth, &req.n) < 0) { |
aba5acdf SH |
498 | perror("Cannot send dump request"); |
499 | exit(1); | |
500 | } | |
501 | filter.flushed = 0; | |
cd70f3f5 | 502 | if (rtnl_dump_filter(&rth, print_neigh, stdout) < 0) { |
aba5acdf SH |
503 | fprintf(stderr, "Flush terminated\n"); |
504 | exit(1); | |
505 | } | |
506 | if (filter.flushed == 0) { | |
f0b34d2d AH |
507 | if (show_stats) { |
508 | if (round == 0) | |
509 | printf("Nothing to flush.\n"); | |
510 | else | |
56f5daac | 511 | printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":""); |
f0b34d2d | 512 | } |
aba5acdf SH |
513 | fflush(stdout); |
514 | return 0; | |
515 | } | |
516 | round++; | |
517 | if (flush_update() < 0) | |
518 | exit(1); | |
519 | if (show_stats) { | |
520 | printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed); | |
521 | fflush(stdout); | |
522 | } | |
ad0a6a2c | 523 | filter.state &= ~NUD_FAILED; |
aba5acdf | 524 | } |
66081849 SH |
525 | printf("*** Flush not complete bailing out after %d rounds\n", |
526 | MAX_ROUNDS); | |
527 | return 1; | |
aba5acdf SH |
528 | } |
529 | ||
0d238ca2 | 530 | if (rtnl_dump_request_n(&rth, &req.n) < 0) { |
aba5acdf SH |
531 | perror("Cannot send dump request"); |
532 | exit(1); | |
533 | } | |
534 | ||
aac7f725 | 535 | new_json_obj(json); |
cd70f3f5 | 536 | if (rtnl_dump_filter(&rth, print_neigh, stdout) < 0) { |
aba5acdf SH |
537 | fprintf(stderr, "Dump terminated\n"); |
538 | exit(1); | |
539 | } | |
aac7f725 | 540 | delete_json_obj(); |
aba5acdf SH |
541 | |
542 | return 0; | |
543 | } | |
544 | ||
545 | int do_ipneigh(int argc, char **argv) | |
546 | { | |
547 | if (argc > 0) { | |
548 | if (matches(*argv, "add") == 0) | |
549 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
550 | if (matches(*argv, "change") == 0 || | |
551 | strcmp(*argv, "chg") == 0) | |
552 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_REPLACE, argc-1, argv+1); | |
553 | if (matches(*argv, "replace") == 0) | |
554 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
555 | if (matches(*argv, "delete") == 0) | |
556 | return ipneigh_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
557 | if (matches(*argv, "get") == 0) { | |
558 | fprintf(stderr, "Sorry, \"neigh get\" is not implemented :-(\n"); | |
559 | return -1; | |
560 | } | |
561 | if (matches(*argv, "show") == 0 || | |
562 | matches(*argv, "lst") == 0 || | |
563 | matches(*argv, "list") == 0) | |
564 | return do_show_or_flush(argc-1, argv+1, 0); | |
565 | if (matches(*argv, "flush") == 0) | |
566 | return do_show_or_flush(argc-1, argv+1, 1); | |
567 | if (matches(*argv, "help") == 0) | |
568 | usage(); | |
569 | } else | |
570 | return do_show_or_flush(0, NULL, 0); | |
571 | ||
572 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv); | |
573 | exit(-1); | |
574 | } |