]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipneigh.c
54655842ed38fb7ce20e7d1b4a9325034c590663
2 * ipneigh.c "ip neigh".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
26 #include "ip_common.h"
28 #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
45 static void usage(void) __attribute__((noreturn
));
47 static void usage(void)
49 fprintf(stderr
, "Usage: ip neigh { add | del | change | replace } { ADDR [ lladdr LLADDR ]\n"
50 " [ nud { permanent | noarp | stale | reachable } ]\n"
51 " | proxy ADDR } [ dev DEV ]\n");
52 fprintf(stderr
, " ip neigh {show|flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n");
56 static int nud_state_a2n(unsigned *state
, const char *arg
)
58 if (matches(arg
, "permanent") == 0)
59 *state
= NUD_PERMANENT
;
60 else if (matches(arg
, "reachable") == 0)
61 *state
= NUD_REACHABLE
;
62 else if (strcmp(arg
, "noarp") == 0)
64 else if (strcmp(arg
, "none") == 0)
66 else if (strcmp(arg
, "stale") == 0)
68 else if (strcmp(arg
, "incomplete") == 0)
69 *state
= NUD_INCOMPLETE
;
70 else if (strcmp(arg
, "delay") == 0)
72 else if (strcmp(arg
, "probe") == 0)
74 else if (matches(arg
, "failed") == 0)
77 if (get_unsigned(state
, arg
, 0))
79 if (*state
>=0x100 || (*state
&((*state
)-1)))
85 static int flush_update(void)
87 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
88 perror("Failed to send flush request");
96 static int ipneigh_modify(int cmd
, int flags
, int argc
, char **argv
)
109 memset(&req
, 0, sizeof(req
));
111 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
112 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
113 req
.n
.nlmsg_type
= cmd
;
114 req
.ndm
.ndm_family
= preferred_family
;
115 req
.ndm
.ndm_state
= NUD_PERMANENT
;
118 if (matches(*argv
, "lladdr") == 0) {
121 duparg("lladdr", *argv
);
124 } else if (strcmp(*argv
, "nud") == 0) {
127 if (nud_state_a2n(&state
, *argv
))
128 invarg("nud state is bad", *argv
);
129 req
.ndm
.ndm_state
= state
;
130 } else if (matches(*argv
, "proxy") == 0) {
132 if (matches(*argv
, "help") == 0)
135 duparg("address", *argv
);
136 get_addr(&dst
, *argv
, preferred_family
);
138 req
.ndm
.ndm_flags
|= NTF_PROXY
;
139 } else if (strcmp(*argv
, "dev") == 0) {
143 if (strcmp(*argv
, "to") == 0) {
146 if (matches(*argv
, "help") == 0) {
150 duparg2("to", *argv
);
151 get_addr(&dst
, *argv
, preferred_family
);
156 if (d
== NULL
|| !dst_ok
|| dst
.family
== AF_UNSPEC
) {
157 fprintf(stderr
, "Device and destination are required arguments.\n");
160 req
.ndm
.ndm_family
= dst
.family
;
161 if (addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
) < 0)
164 if (lla
&& strcmp(lla
, "null")) {
168 l
= ll_addr_a2n(llabuf
, sizeof(llabuf
), lla
);
172 if (addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, llabuf
, l
) < 0)
178 if ((req
.ndm
.ndm_ifindex
= ll_name_to_index(d
)) == 0) {
179 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
183 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
190 int print_neigh(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
192 FILE *fp
= (FILE*)arg
;
193 struct ndmsg
*r
= NLMSG_DATA(n
);
194 int len
= n
->nlmsg_len
;
195 struct rtattr
* tb
[NDA_MAX
+1];
197 static int logit
= 1;
199 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
&&
200 n
->nlmsg_type
!= RTM_GETNEIGH
) {
201 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
202 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
206 len
-= NLMSG_LENGTH(sizeof(*r
));
208 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
212 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWNEIGH
)
215 if (filter
.family
&& filter
.family
!= r
->ndm_family
)
217 if (filter
.index
&& filter
.index
!= r
->ndm_ifindex
)
219 if (!(filter
.state
&r
->ndm_state
) &&
220 !(r
->ndm_flags
& NTF_PROXY
) &&
221 (r
->ndm_state
|| !(filter
.state
&0x100)) &&
222 (r
->ndm_family
!= AF_DECnet
))
225 if (filter
.master
&& !(n
->nlmsg_flags
& NLM_F_DUMP_FILTERED
)) {
229 "\nWARNING: Kernel does not support filtering by master device\n\n");
233 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
236 if (filter
.pfx
.family
) {
238 memset(&dst
, 0, sizeof(dst
));
239 dst
.family
= r
->ndm_family
;
240 memcpy(&dst
.data
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
241 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
245 if (filter
.unused_only
&& tb
[NDA_CACHEINFO
]) {
246 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
253 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
257 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
258 memcpy(fn
, n
, n
->nlmsg_len
);
259 fn
->nlmsg_type
= RTM_DELNEIGH
;
260 fn
->nlmsg_flags
= NLM_F_REQUEST
;
261 fn
->nlmsg_seq
= ++rth
.seq
;
262 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
268 if (n
->nlmsg_type
== RTM_DELNEIGH
)
269 fprintf(fp
, "Deleted ");
270 else if (n
->nlmsg_type
== RTM_GETNEIGH
)
271 fprintf(fp
, "miss ");
274 format_host(r
->ndm_family
,
275 RTA_PAYLOAD(tb
[NDA_DST
]),
276 RTA_DATA(tb
[NDA_DST
]),
277 abuf
, sizeof(abuf
)));
279 if (!filter
.index
&& r
->ndm_ifindex
)
280 fprintf(fp
, "dev %s ", ll_index_to_name(r
->ndm_ifindex
));
281 if (tb
[NDA_LLADDR
]) {
283 fprintf(fp
, "lladdr %s", ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
284 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
285 ll_index_to_type(r
->ndm_ifindex
),
288 if (r
->ndm_flags
& NTF_ROUTER
) {
289 fprintf(fp
, " router");
291 if (r
->ndm_flags
& NTF_PROXY
) {
292 fprintf(fp
, " proxy");
294 if (tb
[NDA_CACHEINFO
] && show_stats
) {
295 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
296 int hz
= get_user_hz();
299 printf(" ref %d", ci
->ndm_refcnt
);
300 fprintf(fp
, " used %d/%d/%d", ci
->ndm_used
/hz
,
301 ci
->ndm_confirmed
/hz
, ci
->ndm_updated
/hz
);
304 if (tb
[NDA_PROBES
] && show_stats
) {
305 __u32 p
= rta_getattr_u32(tb
[NDA_PROBES
]);
306 fprintf(fp
, " probes %u", p
);
310 int nud
= r
->ndm_state
;
313 #define PRINT_FLAG(f) if (nud & NUD_##f) { \
314 nud &= ~NUD_##f; fprintf(fp, #f "%s", nud ? "," : ""); }
315 PRINT_FLAG(INCOMPLETE
);
316 PRINT_FLAG(REACHABLE
);
322 PRINT_FLAG(PERMANENT
);
331 void ipneigh_reset_filter(int ifindex
)
333 memset(&filter
, 0, sizeof(filter
));
335 filter
.index
= ifindex
;
338 static int do_show_or_flush(int argc
, char **argv
, int flush
)
345 char *filter_dev
= NULL
;
348 memset(&req
, 0, sizeof(req
));
350 req
.n
.nlmsg_type
= RTM_GETNEIGH
;
351 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
353 ipneigh_reset_filter(0);
356 filter
.family
= preferred_family
;
360 fprintf(stderr
, "Flush requires arguments.\n");
363 filter
.state
= ~(NUD_PERMANENT
|NUD_NOARP
);
365 filter
.state
= 0xFF & ~NUD_NOARP
;
368 if (strcmp(*argv
, "dev") == 0) {
371 duparg("dev", *argv
);
373 } else if (strcmp(*argv
, "master") == 0) {
376 ifindex
= ll_name_to_index(*argv
);
378 invarg("Device does not exist\n", *argv
);
379 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, ifindex
);
380 filter
.master
= ifindex
;
381 } else if (strcmp(*argv
, "unused") == 0) {
382 filter
.unused_only
= 1;
383 } else if (strcmp(*argv
, "nud") == 0) {
390 if (nud_state_a2n(&state
, *argv
)) {
391 if (strcmp(*argv
, "all") != 0)
392 invarg("nud state is bad", *argv
);
399 filter
.state
|= state
;
400 } else if (strcmp(*argv
, "proxy") == 0)
401 req
.ndm
.ndm_flags
= NTF_PROXY
;
403 if (strcmp(*argv
, "to") == 0) {
406 if (matches(*argv
, "help") == 0)
408 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
409 if (filter
.family
== AF_UNSPEC
)
410 filter
.family
= filter
.pfx
.family
;
418 if ((filter
.index
= ll_name_to_index(filter_dev
)) == 0) {
419 fprintf(stderr
, "Cannot find device \"%s\"\n", filter_dev
);
422 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, filter
.index
);
427 char flushb
[4096-512];
429 filter
.flushb
= flushb
;
431 filter
.flushe
= sizeof(flushb
);
432 filter
.state
&= ~NUD_FAILED
;
434 while (round
< MAX_ROUNDS
) {
435 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETNEIGH
) < 0) {
436 perror("Cannot send dump request");
440 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
441 fprintf(stderr
, "Flush terminated\n");
444 if (filter
.flushed
== 0) {
447 printf("Nothing to flush.\n");
449 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
455 if (flush_update() < 0)
458 printf("\n*** Round %d, deleting %d entries ***\n", round
, filter
.flushed
);
462 printf("*** Flush not complete bailing out after %d rounds\n",
467 req
.ndm
.ndm_family
= filter
.family
;
469 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
470 perror("Cannot send dump request");
474 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
475 fprintf(stderr
, "Dump terminated\n");
482 int do_ipneigh(int argc
, char **argv
)
485 if (matches(*argv
, "add") == 0)
486 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
487 if (matches(*argv
, "change") == 0 ||
488 strcmp(*argv
, "chg") == 0)
489 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_REPLACE
, argc
-1, argv
+1);
490 if (matches(*argv
, "replace") == 0)
491 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
492 if (matches(*argv
, "delete") == 0)
493 return ipneigh_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
494 if (matches(*argv
, "get") == 0) {
495 fprintf(stderr
, "Sorry, \"neigh get\" is not implemented :-(\n");
498 if (matches(*argv
, "show") == 0 ||
499 matches(*argv
, "lst") == 0 ||
500 matches(*argv
, "list") == 0)
501 return do_show_or_flush(argc
-1, argv
+1, 0);
502 if (matches(*argv
, "flush") == 0)
503 return do_show_or_flush(argc
-1, argv
+1, 1);
504 if (matches(*argv
, "help") == 0)
507 return do_show_or_flush(0, NULL
, 0);
509 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv
);