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