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