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