]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipneigh.c
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 }\n"
50 " { ADDR [ lladdr LLADDR ] [ nud STATE ] | proxy ADDR } [ dev DEV ]\n");
51 fprintf(stderr
, " ip neigh { show | flush } [ proxy ] [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n");
52 fprintf(stderr
, " [ vrf NAME ]\n\n");
53 fprintf(stderr
, "STATE := { permanent | noarp | stale | reachable | none |\n"
54 " incomplete | delay | probe | failed }\n");
58 static int nud_state_a2n(unsigned int *state
, const char *arg
)
60 if (matches(arg
, "permanent") == 0)
61 *state
= NUD_PERMANENT
;
62 else if (matches(arg
, "reachable") == 0)
63 *state
= NUD_REACHABLE
;
64 else if (strcmp(arg
, "noarp") == 0)
66 else if (strcmp(arg
, "none") == 0)
68 else if (strcmp(arg
, "stale") == 0)
70 else if (strcmp(arg
, "incomplete") == 0)
71 *state
= NUD_INCOMPLETE
;
72 else if (strcmp(arg
, "delay") == 0)
74 else if (strcmp(arg
, "probe") == 0)
76 else if (matches(arg
, "failed") == 0)
79 if (get_unsigned(state
, arg
, 0))
81 if (*state
>= 0x100 || (*state
&((*state
)-1)))
87 static int flush_update(void)
89 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
90 perror("Failed to send flush request");
98 static int ipneigh_modify(int cmd
, int flags
, int argc
, char **argv
)
112 memset(&req
, 0, sizeof(req
));
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
;
121 if (matches(*argv
, "lladdr") == 0) {
124 duparg("lladdr", *argv
);
127 } else if (strcmp(*argv
, "nud") == 0) {
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) {
136 if (matches(*argv
, "help") == 0)
139 duparg("address", *argv
);
140 get_addr(&dst
, *argv
, preferred_family
);
143 req
.ndm
.ndm_flags
|= NTF_PROXY
;
144 } else if (strcmp(*argv
, "dev") == 0) {
149 if (strcmp(*argv
, "to") == 0) {
152 if (matches(*argv
, "help") == 0) {
156 duparg2("to", *argv
);
157 get_addr(&dst
, *argv
, preferred_family
);
162 if (!dev_ok
|| !dst_ok
|| dst
.family
== AF_UNSPEC
) {
163 fprintf(stderr
, "Device and destination are required arguments.\n");
166 req
.ndm
.ndm_family
= dst
.family
;
167 if (addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
) < 0)
170 if (lla
&& strcmp(lla
, "null")) {
174 l
= ll_addr_a2n(llabuf
, sizeof(llabuf
), lla
);
178 if (addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, llabuf
, l
) < 0)
184 if (dev
&& (req
.ndm
.ndm_ifindex
= ll_name_to_index(dev
)) == 0) {
185 fprintf(stderr
, "Cannot find device \"%s\"\n", dev
);
189 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
196 int print_neigh(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
198 FILE *fp
= (FILE *)arg
;
199 struct ndmsg
*r
= NLMSG_DATA(n
);
200 int len
= n
->nlmsg_len
;
201 struct rtattr
*tb
[NDA_MAX
+1];
202 static int logit
= 1;
204 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
&&
205 n
->nlmsg_type
!= RTM_GETNEIGH
) {
206 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
207 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
211 len
-= NLMSG_LENGTH(sizeof(*r
));
213 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
217 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWNEIGH
)
220 if (filter
.family
&& filter
.family
!= r
->ndm_family
)
222 if (filter
.index
&& filter
.index
!= r
->ndm_ifindex
)
224 if (!(filter
.state
&r
->ndm_state
) &&
225 !(r
->ndm_flags
& NTF_PROXY
) &&
226 (r
->ndm_state
|| !(filter
.state
&0x100)) &&
227 (r
->ndm_family
!= AF_DECnet
))
230 if (filter
.master
&& !(n
->nlmsg_flags
& NLM_F_DUMP_FILTERED
)) {
234 "\nWARNING: Kernel does not support filtering by master device\n\n");
238 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
241 if (filter
.pfx
.family
) {
244 memset(&dst
, 0, sizeof(dst
));
245 dst
.family
= r
->ndm_family
;
246 memcpy(&dst
.data
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
247 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
251 if (filter
.unused_only
&& tb
[NDA_CACHEINFO
]) {
252 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
261 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
265 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
266 memcpy(fn
, n
, n
->nlmsg_len
);
267 fn
->nlmsg_type
= RTM_DELNEIGH
;
268 fn
->nlmsg_flags
= NLM_F_REQUEST
;
269 fn
->nlmsg_seq
= ++rth
.seq
;
270 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
276 if (n
->nlmsg_type
== RTM_DELNEIGH
)
277 fprintf(fp
, "Deleted ");
278 else if (n
->nlmsg_type
== RTM_GETNEIGH
)
279 fprintf(fp
, "miss ");
282 format_host_rta(r
->ndm_family
, tb
[NDA_DST
]));
284 if (!filter
.index
&& r
->ndm_ifindex
)
285 fprintf(fp
, "dev %s ", ll_index_to_name(r
->ndm_ifindex
));
286 if (tb
[NDA_LLADDR
]) {
288 fprintf(fp
, "lladdr %s", ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
289 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
290 ll_index_to_type(r
->ndm_ifindex
),
293 if (r
->ndm_flags
& NTF_ROUTER
) {
294 fprintf(fp
, " router");
296 if (r
->ndm_flags
& NTF_PROXY
) {
297 fprintf(fp
, " proxy");
299 if (tb
[NDA_CACHEINFO
] && show_stats
) {
300 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
301 int hz
= get_user_hz();
304 printf(" ref %d", ci
->ndm_refcnt
);
305 fprintf(fp
, " used %d/%d/%d", ci
->ndm_used
/hz
,
306 ci
->ndm_confirmed
/hz
, ci
->ndm_updated
/hz
);
309 if (tb
[NDA_PROBES
] && show_stats
) {
310 __u32 p
= rta_getattr_u32(tb
[NDA_PROBES
]);
312 fprintf(fp
, " probes %u", p
);
316 int nud
= r
->ndm_state
;
320 #define PRINT_FLAG(f) if (nud & NUD_##f) { \
321 nud &= ~NUD_##f; fprintf(fp, #f "%s", nud ? "," : ""); }
322 PRINT_FLAG(INCOMPLETE
);
323 PRINT_FLAG(REACHABLE
);
329 PRINT_FLAG(PERMANENT
);
338 void ipneigh_reset_filter(int ifindex
)
340 memset(&filter
, 0, sizeof(filter
));
342 filter
.index
= ifindex
;
345 static int do_show_or_flush(int argc
, char **argv
, int flush
)
352 char *filter_dev
= NULL
;
355 memset(&req
, 0, sizeof(req
));
357 req
.n
.nlmsg_type
= RTM_GETNEIGH
;
358 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
360 ipneigh_reset_filter(0);
363 filter
.family
= preferred_family
;
367 fprintf(stderr
, "Flush requires arguments.\n");
370 filter
.state
= ~(NUD_PERMANENT
|NUD_NOARP
);
372 filter
.state
= 0xFF & ~NUD_NOARP
;
375 if (strcmp(*argv
, "dev") == 0) {
378 duparg("dev", *argv
);
380 } else if (strcmp(*argv
, "master") == 0) {
384 ifindex
= ll_name_to_index(*argv
);
386 invarg("Device does not exist\n", *argv
);
387 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, ifindex
);
388 filter
.master
= ifindex
;
389 } else if (strcmp(*argv
, "vrf") == 0) {
393 ifindex
= ll_name_to_index(*argv
);
395 invarg("Not a valid VRF name\n", *argv
);
396 if (!name_is_vrf(*argv
))
397 invarg("Not a valid VRF name\n", *argv
);
398 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, ifindex
);
399 filter
.master
= ifindex
;
400 } else if (strcmp(*argv
, "unused") == 0) {
401 filter
.unused_only
= 1;
402 } else if (strcmp(*argv
, "nud") == 0) {
410 if (nud_state_a2n(&state
, *argv
)) {
411 if (strcmp(*argv
, "all") != 0)
412 invarg("nud state is bad", *argv
);
419 filter
.state
|= state
;
420 } else if (strcmp(*argv
, "proxy") == 0)
421 req
.ndm
.ndm_flags
= NTF_PROXY
;
423 if (strcmp(*argv
, "to") == 0) {
426 if (matches(*argv
, "help") == 0)
428 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
429 if (filter
.family
== AF_UNSPEC
)
430 filter
.family
= filter
.pfx
.family
;
438 if ((filter
.index
= ll_name_to_index(filter_dev
)) == 0) {
439 fprintf(stderr
, "Cannot find device \"%s\"\n", filter_dev
);
442 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, filter
.index
);
445 req
.ndm
.ndm_family
= filter
.family
;
449 char flushb
[4096-512];
451 filter
.flushb
= flushb
;
453 filter
.flushe
= sizeof(flushb
);
454 filter
.state
&= ~NUD_FAILED
;
456 while (round
< MAX_ROUNDS
) {
457 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
458 perror("Cannot send dump request");
462 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
463 fprintf(stderr
, "Flush terminated\n");
466 if (filter
.flushed
== 0) {
469 printf("Nothing to flush.\n");
471 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
477 if (flush_update() < 0)
480 printf("\n*** Round %d, deleting %d entries ***\n", round
, filter
.flushed
);
484 printf("*** Flush not complete bailing out after %d rounds\n",
489 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
490 perror("Cannot send dump request");
494 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
495 fprintf(stderr
, "Dump terminated\n");
502 int do_ipneigh(int argc
, char **argv
)
505 if (matches(*argv
, "add") == 0)
506 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
507 if (matches(*argv
, "change") == 0 ||
508 strcmp(*argv
, "chg") == 0)
509 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_REPLACE
, argc
-1, argv
+1);
510 if (matches(*argv
, "replace") == 0)
511 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
512 if (matches(*argv
, "delete") == 0)
513 return ipneigh_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
514 if (matches(*argv
, "get") == 0) {
515 fprintf(stderr
, "Sorry, \"neigh get\" is not implemented :-(\n");
518 if (matches(*argv
, "show") == 0 ||
519 matches(*argv
, "lst") == 0 ||
520 matches(*argv
, "list") == 0)
521 return do_show_or_flush(argc
-1, argv
+1, 0);
522 if (matches(*argv
, "flush") == 0)
523 return do_show_or_flush(argc
-1, argv
+1, 1);
524 if (matches(*argv
, "help") == 0)
527 return do_show_or_flush(0, NULL
, 0);
529 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv
);