]>
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> | |
16 | #include <syslog.h> | |
17 | #include <fcntl.h> | |
18 | #include <string.h> | |
19 | #include <sys/time.h> | |
aba5acdf SH |
20 | #include <sys/socket.h> |
21 | #include <netinet/in.h> | |
22 | #include <netinet/ip.h> | |
23 | ||
24 | #include "rt_names.h" | |
25 | #include "utils.h" | |
26 | #include "ip_common.h" | |
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; | |
34 | int index; | |
35 | int state; | |
36 | int unused_only; | |
37 | inet_prefix pfx; | |
38 | int flushed; | |
39 | char *flushb; | |
40 | int flushp; | |
41 | int flushe; | |
aba5acdf SH |
42 | } filter; |
43 | ||
44 | static void usage(void) __attribute__((noreturn)); | |
45 | ||
46 | static void usage(void) | |
47 | { | |
48 | fprintf(stderr, "Usage: ip neigh { add | del | change | replace } { ADDR [ lladdr LLADDR ]\n" | |
49 | " [ nud { permanent | noarp | stale | reachable } ]\n" | |
50 | " | proxy ADDR } [ dev DEV ]\n"); | |
51 | fprintf(stderr, " ip neigh {show|flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n"); | |
52 | exit(-1); | |
53 | } | |
54 | ||
d1f28cf1 | 55 | static int nud_state_a2n(unsigned *state, const char *arg) |
aba5acdf SH |
56 | { |
57 | if (matches(arg, "permanent") == 0) | |
58 | *state = NUD_PERMANENT; | |
59 | else if (matches(arg, "reachable") == 0) | |
60 | *state = NUD_REACHABLE; | |
61 | else if (strcmp(arg, "noarp") == 0) | |
62 | *state = NUD_NOARP; | |
63 | else if (strcmp(arg, "none") == 0) | |
64 | *state = NUD_NONE; | |
65 | else if (strcmp(arg, "stale") == 0) | |
66 | *state = NUD_STALE; | |
67 | else if (strcmp(arg, "incomplete") == 0) | |
68 | *state = NUD_INCOMPLETE; | |
69 | else if (strcmp(arg, "delay") == 0) | |
70 | *state = NUD_DELAY; | |
71 | else if (strcmp(arg, "probe") == 0) | |
72 | *state = NUD_PROBE; | |
73 | else if (matches(arg, "failed") == 0) | |
74 | *state = NUD_FAILED; | |
75 | else { | |
76 | if (get_unsigned(state, arg, 0)) | |
77 | return -1; | |
78 | if (*state>=0x100 || (*state&((*state)-1))) | |
79 | return -1; | |
80 | } | |
81 | return 0; | |
82 | } | |
83 | ||
aba5acdf SH |
84 | static int flush_update(void) |
85 | { | |
f31a37f7 | 86 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
1fb0a998 | 87 | perror("Failed to send flush request"); |
aba5acdf SH |
88 | return -1; |
89 | } | |
90 | filter.flushp = 0; | |
91 | return 0; | |
92 | } | |
93 | ||
94 | ||
95 | static int ipneigh_modify(int cmd, int flags, int argc, char **argv) | |
96 | { | |
aba5acdf | 97 | struct { |
4806867a SH |
98 | struct nlmsghdr n; |
99 | struct ndmsg ndm; | |
100 | char buf[256]; | |
aba5acdf SH |
101 | } req; |
102 | char *d = NULL; | |
103 | int dst_ok = 0; | |
104 | int lladdr_ok = 0; | |
105 | char * lla = NULL; | |
106 | inet_prefix dst; | |
107 | ||
108 | memset(&req, 0, sizeof(req)); | |
109 | ||
110 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)); | |
111 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
112 | req.n.nlmsg_type = cmd; | |
113 | req.ndm.ndm_family = preferred_family; | |
114 | req.ndm.ndm_state = NUD_PERMANENT; | |
115 | ||
116 | while (argc > 0) { | |
117 | if (matches(*argv, "lladdr") == 0) { | |
118 | NEXT_ARG(); | |
119 | if (lladdr_ok) | |
120 | duparg("lladdr", *argv); | |
121 | lla = *argv; | |
122 | lladdr_ok = 1; | |
123 | } else if (strcmp(*argv, "nud") == 0) { | |
124 | unsigned state; | |
125 | NEXT_ARG(); | |
126 | if (nud_state_a2n(&state, *argv)) | |
127 | invarg("nud state is bad", *argv); | |
128 | req.ndm.ndm_state = state; | |
129 | } else if (matches(*argv, "proxy") == 0) { | |
130 | NEXT_ARG(); | |
131 | if (matches(*argv, "help") == 0) | |
132 | usage(); | |
133 | if (dst_ok) | |
134 | duparg("address", *argv); | |
135 | get_addr(&dst, *argv, preferred_family); | |
136 | dst_ok = 1; | |
137 | req.ndm.ndm_flags |= NTF_PROXY; | |
138 | } else if (strcmp(*argv, "dev") == 0) { | |
139 | NEXT_ARG(); | |
140 | d = *argv; | |
141 | } else { | |
142 | if (strcmp(*argv, "to") == 0) { | |
143 | NEXT_ARG(); | |
144 | } | |
145 | if (matches(*argv, "help") == 0) { | |
146 | NEXT_ARG(); | |
147 | } | |
148 | if (dst_ok) | |
149 | duparg2("to", *argv); | |
150 | get_addr(&dst, *argv, preferred_family); | |
151 | dst_ok = 1; | |
152 | } | |
153 | argc--; argv++; | |
154 | } | |
155 | if (d == NULL || !dst_ok || dst.family == AF_UNSPEC) { | |
156 | fprintf(stderr, "Device and destination are required arguments.\n"); | |
157 | exit(-1); | |
158 | } | |
159 | req.ndm.ndm_family = dst.family; | |
542b0cc7 SH |
160 | if (addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen) < 0) |
161 | return -1; | |
aba5acdf SH |
162 | |
163 | if (lla && strcmp(lla, "null")) { | |
7b565754 | 164 | char llabuf[20]; |
aba5acdf SH |
165 | int l; |
166 | ||
167 | l = ll_addr_a2n(llabuf, sizeof(llabuf), lla); | |
542b0cc7 SH |
168 | if (l < 0) |
169 | return -1; | |
170 | ||
171 | if (addattr_l(&req.n, sizeof(req), NDA_LLADDR, llabuf, l) < 0) | |
172 | return -1; | |
aba5acdf SH |
173 | } |
174 | ||
aba5acdf SH |
175 | ll_init_map(&rth); |
176 | ||
177 | if ((req.ndm.ndm_ifindex = ll_name_to_index(d)) == 0) { | |
178 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
179 | return -1; | |
180 | } | |
181 | ||
c079e121 | 182 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
aba5acdf SH |
183 | exit(2); |
184 | ||
351efcde | 185 | return 0; |
aba5acdf SH |
186 | } |
187 | ||
188 | ||
50772dc5 | 189 | int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
aba5acdf SH |
190 | { |
191 | FILE *fp = (FILE*)arg; | |
192 | struct ndmsg *r = NLMSG_DATA(n); | |
193 | int len = n->nlmsg_len; | |
194 | struct rtattr * tb[NDA_MAX+1]; | |
195 | char abuf[256]; | |
196 | ||
1556e29d DS |
197 | if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH && |
198 | n->nlmsg_type != RTM_GETNEIGH) { | |
aba5acdf SH |
199 | fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n", |
200 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
ae665a52 | 201 | |
aba5acdf SH |
202 | return 0; |
203 | } | |
204 | len -= NLMSG_LENGTH(sizeof(*r)); | |
205 | if (len < 0) { | |
206 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
207 | return -1; | |
208 | } | |
209 | ||
210 | if (filter.flushb && n->nlmsg_type != RTM_NEWNEIGH) | |
211 | return 0; | |
212 | ||
213 | if (filter.family && filter.family != r->ndm_family) | |
214 | return 0; | |
215 | if (filter.index && filter.index != r->ndm_ifindex) | |
216 | return 0; | |
217 | if (!(filter.state&r->ndm_state) && | |
1dac7817 | 218 | !(r->ndm_flags & NTF_PROXY) && |
aba5acdf SH |
219 | (r->ndm_state || !(filter.state&0x100)) && |
220 | (r->ndm_family != AF_DECnet)) | |
221 | return 0; | |
222 | ||
aba5acdf SH |
223 | parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r))); |
224 | ||
225 | if (tb[NDA_DST]) { | |
226 | if (filter.pfx.family) { | |
227 | inet_prefix dst; | |
228 | memset(&dst, 0, sizeof(dst)); | |
229 | dst.family = r->ndm_family; | |
230 | memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST])); | |
231 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) | |
232 | return 0; | |
233 | } | |
234 | } | |
235 | if (filter.unused_only && tb[NDA_CACHEINFO]) { | |
236 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); | |
237 | if (ci->ndm_refcnt) | |
238 | return 0; | |
239 | } | |
240 | ||
241 | if (filter.flushb) { | |
242 | struct nlmsghdr *fn; | |
243 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { | |
244 | if (flush_update()) | |
245 | return -1; | |
246 | } | |
247 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); | |
248 | memcpy(fn, n, n->nlmsg_len); | |
249 | fn->nlmsg_type = RTM_DELNEIGH; | |
250 | fn->nlmsg_flags = NLM_F_REQUEST; | |
351efcde | 251 | fn->nlmsg_seq = ++rth.seq; |
aba5acdf SH |
252 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
253 | filter.flushed++; | |
254 | if (show_stats < 2) | |
255 | return 0; | |
256 | } | |
257 | ||
6ea3ebaf ND |
258 | if (n->nlmsg_type == RTM_DELNEIGH) |
259 | fprintf(fp, "delete "); | |
1556e29d DS |
260 | else if (n->nlmsg_type == RTM_GETNEIGH) |
261 | fprintf(fp, "miss "); | |
aba5acdf | 262 | if (tb[NDA_DST]) { |
ae665a52 | 263 | fprintf(fp, "%s ", |
aba5acdf SH |
264 | format_host(r->ndm_family, |
265 | RTA_PAYLOAD(tb[NDA_DST]), | |
266 | RTA_DATA(tb[NDA_DST]), | |
267 | abuf, sizeof(abuf))); | |
268 | } | |
269 | if (!filter.index && r->ndm_ifindex) | |
270 | fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex)); | |
271 | if (tb[NDA_LLADDR]) { | |
272 | SPRINT_BUF(b1); | |
273 | fprintf(fp, "lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]), | |
274 | RTA_PAYLOAD(tb[NDA_LLADDR]), | |
275 | ll_index_to_type(r->ndm_ifindex), | |
276 | b1, sizeof(b1))); | |
277 | } | |
278 | if (r->ndm_flags & NTF_ROUTER) { | |
279 | fprintf(fp, " router"); | |
280 | } | |
1dac7817 TZ |
281 | if (r->ndm_flags & NTF_PROXY) { |
282 | fprintf(fp, " proxy"); | |
283 | } | |
aba5acdf | 284 | if (tb[NDA_CACHEINFO] && show_stats) { |
aba5acdf | 285 | struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]); |
6a34d291 SH |
286 | int hz = get_user_hz(); |
287 | ||
aba5acdf SH |
288 | if (ci->ndm_refcnt) |
289 | printf(" ref %d", ci->ndm_refcnt); | |
290 | fprintf(fp, " used %d/%d/%d", ci->ndm_used/hz, | |
291 | ci->ndm_confirmed/hz, ci->ndm_updated/hz); | |
292 | } | |
293 | ||
69410a49 | 294 | if (tb[NDA_PROBES] && show_stats) { |
ff24746c | 295 | __u32 p = rta_getattr_u32(tb[NDA_PROBES]); |
69410a49 | 296 | fprintf(fp, " probes %u", p); |
297 | } | |
298 | ||
aba5acdf | 299 | if (r->ndm_state) { |
435ac6b8 | 300 | int nud = r->ndm_state; |
301 | fprintf(fp, " "); | |
302 | ||
303 | #define PRINT_FLAG(f) if (nud & NUD_##f) { \ | |
304 | nud &= ~NUD_##f; fprintf(fp, #f "%s", nud ? "," : ""); } | |
305 | PRINT_FLAG(INCOMPLETE); | |
306 | PRINT_FLAG(REACHABLE); | |
307 | PRINT_FLAG(STALE); | |
308 | PRINT_FLAG(DELAY); | |
309 | PRINT_FLAG(PROBE); | |
310 | PRINT_FLAG(FAILED); | |
311 | PRINT_FLAG(NOARP); | |
312 | PRINT_FLAG(PERMANENT); | |
313 | #undef PRINT_FLAG | |
aba5acdf SH |
314 | } |
315 | fprintf(fp, "\n"); | |
316 | ||
317 | fflush(fp); | |
318 | return 0; | |
319 | } | |
320 | ||
093b7646 | 321 | void ipneigh_reset_filter(int ifindex) |
aba5acdf SH |
322 | { |
323 | memset(&filter, 0, sizeof(filter)); | |
324 | filter.state = ~0; | |
093b7646 | 325 | filter.index = ifindex; |
aba5acdf SH |
326 | } |
327 | ||
d1f28cf1 | 328 | static int do_show_or_flush(int argc, char **argv, int flush) |
aba5acdf SH |
329 | { |
330 | char *filter_dev = NULL; | |
aba5acdf | 331 | int state_given = 0; |
1dac7817 | 332 | struct ndmsg ndm = { 0 }; |
aba5acdf | 333 | |
093b7646 | 334 | ipneigh_reset_filter(0); |
aba5acdf SH |
335 | |
336 | if (!filter.family) | |
337 | filter.family = preferred_family; | |
338 | ||
339 | if (flush) { | |
340 | if (argc <= 0) { | |
341 | fprintf(stderr, "Flush requires arguments.\n"); | |
342 | return -1; | |
343 | } | |
344 | filter.state = ~(NUD_PERMANENT|NUD_NOARP); | |
345 | } else | |
346 | filter.state = 0xFF & ~NUD_NOARP; | |
347 | ||
348 | while (argc > 0) { | |
349 | if (strcmp(*argv, "dev") == 0) { | |
350 | NEXT_ARG(); | |
351 | if (filter_dev) | |
352 | duparg("dev", *argv); | |
353 | filter_dev = *argv; | |
354 | } else if (strcmp(*argv, "unused") == 0) { | |
355 | filter.unused_only = 1; | |
356 | } else if (strcmp(*argv, "nud") == 0) { | |
357 | unsigned state; | |
358 | NEXT_ARG(); | |
359 | if (!state_given) { | |
360 | state_given = 1; | |
361 | filter.state = 0; | |
362 | } | |
363 | if (nud_state_a2n(&state, *argv)) { | |
364 | if (strcmp(*argv, "all") != 0) | |
365 | invarg("nud state is bad", *argv); | |
366 | state = ~0; | |
367 | if (flush) | |
368 | state &= ~NUD_NOARP; | |
369 | } | |
370 | if (state == 0) | |
371 | state = 0x100; | |
372 | filter.state |= state; | |
1dac7817 TZ |
373 | } else if (strcmp(*argv, "proxy") == 0) |
374 | ndm.ndm_flags = NTF_PROXY; | |
375 | else { | |
aba5acdf SH |
376 | if (strcmp(*argv, "to") == 0) { |
377 | NEXT_ARG(); | |
378 | } | |
379 | if (matches(*argv, "help") == 0) | |
380 | usage(); | |
381 | get_prefix(&filter.pfx, *argv, filter.family); | |
382 | if (filter.family == AF_UNSPEC) | |
383 | filter.family = filter.pfx.family; | |
384 | } | |
385 | argc--; argv++; | |
386 | } | |
387 | ||
aba5acdf SH |
388 | ll_init_map(&rth); |
389 | ||
390 | if (filter_dev) { | |
391 | if ((filter.index = ll_name_to_index(filter_dev)) == 0) { | |
392 | fprintf(stderr, "Cannot find device \"%s\"\n", filter_dev); | |
393 | return -1; | |
394 | } | |
395 | } | |
396 | ||
397 | if (flush) { | |
398 | int round = 0; | |
399 | char flushb[4096-512]; | |
400 | ||
401 | filter.flushb = flushb; | |
402 | filter.flushp = 0; | |
403 | filter.flushe = sizeof(flushb); | |
aba5acdf SH |
404 | filter.state &= ~NUD_FAILED; |
405 | ||
66081849 | 406 | while (round < MAX_ROUNDS) { |
aba5acdf SH |
407 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNEIGH) < 0) { |
408 | perror("Cannot send dump request"); | |
409 | exit(1); | |
410 | } | |
411 | filter.flushed = 0; | |
cd70f3f5 | 412 | if (rtnl_dump_filter(&rth, print_neigh, stdout) < 0) { |
aba5acdf SH |
413 | fprintf(stderr, "Flush terminated\n"); |
414 | exit(1); | |
415 | } | |
416 | if (filter.flushed == 0) { | |
f0b34d2d AH |
417 | if (show_stats) { |
418 | if (round == 0) | |
419 | printf("Nothing to flush.\n"); | |
420 | else | |
421 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); | |
422 | } | |
aba5acdf SH |
423 | fflush(stdout); |
424 | return 0; | |
425 | } | |
426 | round++; | |
427 | if (flush_update() < 0) | |
428 | exit(1); | |
429 | if (show_stats) { | |
430 | printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed); | |
431 | fflush(stdout); | |
432 | } | |
433 | } | |
66081849 SH |
434 | printf("*** Flush not complete bailing out after %d rounds\n", |
435 | MAX_ROUNDS); | |
436 | return 1; | |
aba5acdf SH |
437 | } |
438 | ||
1dac7817 TZ |
439 | ndm.ndm_family = filter.family; |
440 | ||
441 | if (rtnl_dump_request(&rth, RTM_GETNEIGH, &ndm, sizeof(struct ndmsg)) < 0) { | |
aba5acdf SH |
442 | perror("Cannot send dump request"); |
443 | exit(1); | |
444 | } | |
445 | ||
cd70f3f5 | 446 | if (rtnl_dump_filter(&rth, print_neigh, stdout) < 0) { |
aba5acdf SH |
447 | fprintf(stderr, "Dump terminated\n"); |
448 | exit(1); | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | int do_ipneigh(int argc, char **argv) | |
455 | { | |
456 | if (argc > 0) { | |
457 | if (matches(*argv, "add") == 0) | |
458 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); | |
459 | if (matches(*argv, "change") == 0 || | |
460 | strcmp(*argv, "chg") == 0) | |
461 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_REPLACE, argc-1, argv+1); | |
462 | if (matches(*argv, "replace") == 0) | |
463 | return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
464 | if (matches(*argv, "delete") == 0) | |
465 | return ipneigh_modify(RTM_DELNEIGH, 0, argc-1, argv+1); | |
466 | if (matches(*argv, "get") == 0) { | |
467 | fprintf(stderr, "Sorry, \"neigh get\" is not implemented :-(\n"); | |
468 | return -1; | |
469 | } | |
470 | if (matches(*argv, "show") == 0 || | |
471 | matches(*argv, "lst") == 0 || | |
472 | matches(*argv, "list") == 0) | |
473 | return do_show_or_flush(argc-1, argv+1, 0); | |
474 | if (matches(*argv, "flush") == 0) | |
475 | return do_show_or_flush(argc-1, argv+1, 1); | |
476 | if (matches(*argv, "help") == 0) | |
477 | usage(); | |
478 | } else | |
479 | return do_show_or_flush(0, NULL, 0); | |
480 | ||
481 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv); | |
482 | exit(-1); | |
483 | } |