]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipneigh.c
925494dbe3249d0c6c4961ead9431dac554cf83a
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>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netinet/ip.h>
25 #include "ip_common.h"
26 #include "json_print.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
)
105 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
106 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
108 .ndm
.ndm_family
= preferred_family
,
109 .ndm
.ndm_state
= NUD_PERMANENT
,
119 if (matches(*argv
, "lladdr") == 0) {
122 duparg("lladdr", *argv
);
125 } else if (strcmp(*argv
, "nud") == 0) {
129 if (nud_state_a2n(&state
, *argv
))
130 invarg("nud state is bad", *argv
);
131 req
.ndm
.ndm_state
= state
;
132 } else if (matches(*argv
, "proxy") == 0) {
134 if (matches(*argv
, "help") == 0)
137 duparg("address", *argv
);
138 get_addr(&dst
, *argv
, preferred_family
);
141 req
.ndm
.ndm_flags
|= NTF_PROXY
;
142 } else if (strcmp(*argv
, "dev") == 0) {
147 if (strcmp(*argv
, "to") == 0) {
150 if (matches(*argv
, "help") == 0) {
154 duparg2("to", *argv
);
155 get_addr(&dst
, *argv
, preferred_family
);
160 if (!dev_ok
|| !dst_ok
|| dst
.family
== AF_UNSPEC
) {
161 fprintf(stderr
, "Device and destination are required arguments.\n");
164 req
.ndm
.ndm_family
= dst
.family
;
165 if (addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
) < 0)
168 if (lla
&& strcmp(lla
, "null")) {
172 l
= ll_addr_a2n(llabuf
, sizeof(llabuf
), lla
);
176 if (addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, llabuf
, l
) < 0)
182 if (dev
&& (req
.ndm
.ndm_ifindex
= ll_name_to_index(dev
)) == 0) {
183 fprintf(stderr
, "Cannot find device \"%s\"\n", dev
);
187 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
193 static void print_cacheinfo(const struct nda_cacheinfo
*ci
)
201 print_uint(PRINT_ANY
, "refcnt",
202 " ref %u", ci
->ndm_refcnt
);
204 print_uint(PRINT_ANY
, "used", " used %u", ci
->ndm_used
/ hz
);
205 print_uint(PRINT_ANY
, "confirmed", "/%u", ci
->ndm_confirmed
/ hz
);
206 print_uint(PRINT_ANY
, "updated", "/u", ci
->ndm_updated
/ hz
);
209 static void print_neigh_state(unsigned int nud
)
212 open_json_array(PRINT_JSON
,
213 is_json_context() ? "state" : "");
215 #define PRINT_FLAG(f) \
216 if (nud & NUD_##f) { \
218 print_string(PRINT_ANY, NULL, " %s", #f); \
221 PRINT_FLAG(INCOMPLETE
);
222 PRINT_FLAG(REACHABLE
);
228 PRINT_FLAG(PERMANENT
);
231 close_json_array(PRINT_JSON
, NULL
);
234 int print_neigh(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
236 FILE *fp
= (FILE *)arg
;
237 struct ndmsg
*r
= NLMSG_DATA(n
);
238 int len
= n
->nlmsg_len
;
239 struct rtattr
*tb
[NDA_MAX
+1];
240 static int logit
= 1;
242 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
&&
243 n
->nlmsg_type
!= RTM_GETNEIGH
) {
244 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
245 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
249 len
-= NLMSG_LENGTH(sizeof(*r
));
251 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
255 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWNEIGH
)
258 if (filter
.family
&& filter
.family
!= r
->ndm_family
)
260 if (filter
.index
&& filter
.index
!= r
->ndm_ifindex
)
262 if (!(filter
.state
&r
->ndm_state
) &&
263 !(r
->ndm_flags
& NTF_PROXY
) &&
264 (r
->ndm_state
|| !(filter
.state
&0x100)) &&
265 (r
->ndm_family
!= AF_DECnet
))
268 if (filter
.master
&& !(n
->nlmsg_flags
& NLM_F_DUMP_FILTERED
)) {
272 "\nWARNING: Kernel does not support filtering by master device\n\n");
276 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
278 if (inet_addr_match_rta(&filter
.pfx
, tb
[NDA_DST
]))
281 if (filter
.unused_only
&& tb
[NDA_CACHEINFO
]) {
282 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
291 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
295 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
296 memcpy(fn
, n
, n
->nlmsg_len
);
297 fn
->nlmsg_type
= RTM_DELNEIGH
;
298 fn
->nlmsg_flags
= NLM_F_REQUEST
;
299 fn
->nlmsg_seq
= ++rth
.seq
;
300 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
306 open_json_object(NULL
);
307 if (n
->nlmsg_type
== RTM_DELNEIGH
)
308 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
309 else if (n
->nlmsg_type
== RTM_GETNEIGH
)
310 print_null(PRINT_ANY
, "miss", "%s ", "miss");
315 dst
= format_host_rta(r
->ndm_family
, tb
[NDA_DST
]);
316 print_color_string(PRINT_ANY
,
317 ifa_family_color(r
->ndm_family
),
321 if (!filter
.index
&& r
->ndm_ifindex
) {
322 if (!is_json_context())
325 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
327 ll_index_to_name(r
->ndm_ifindex
));
330 if (tb
[NDA_LLADDR
]) {
334 lladdr
= ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
335 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
336 ll_index_to_type(r
->ndm_ifindex
),
339 if (!is_json_context())
340 fprintf(fp
, "lladdr ");
342 print_color_string(PRINT_ANY
, COLOR_MAC
,
343 "lladdr", "%s", lladdr
);
346 if (r
->ndm_flags
& NTF_ROUTER
)
347 print_null(PRINT_ANY
, "router", " %s", "router");
349 if (r
->ndm_flags
& NTF_PROXY
)
350 print_null(PRINT_ANY
, "proxy", " %s", "proxy");
353 if (tb
[NDA_CACHEINFO
])
354 print_cacheinfo(RTA_DATA(tb
[NDA_CACHEINFO
]));
357 print_uint(PRINT_ANY
, "probes", " probes %u",
358 rta_getattr_u32(tb
[NDA_PROBES
]));
362 print_neigh_state(r
->ndm_state
);
364 print_string(PRINT_FP
, NULL
, "\n", "");
371 void ipneigh_reset_filter(int ifindex
)
373 memset(&filter
, 0, sizeof(filter
));
375 filter
.index
= ifindex
;
378 static int do_show_or_flush(int argc
, char **argv
, int flush
)
385 .n
.nlmsg_type
= RTM_GETNEIGH
,
386 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
388 char *filter_dev
= NULL
;
391 ipneigh_reset_filter(0);
394 filter
.family
= preferred_family
;
398 fprintf(stderr
, "Flush requires arguments.\n");
401 filter
.state
= ~(NUD_PERMANENT
|NUD_NOARP
);
403 filter
.state
= 0xFF & ~NUD_NOARP
;
406 if (strcmp(*argv
, "dev") == 0) {
409 duparg("dev", *argv
);
411 } else if (strcmp(*argv
, "master") == 0) {
415 ifindex
= ll_name_to_index(*argv
);
417 invarg("Device does not exist\n", *argv
);
418 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, ifindex
);
419 filter
.master
= ifindex
;
420 } else if (strcmp(*argv
, "vrf") == 0) {
424 ifindex
= ll_name_to_index(*argv
);
426 invarg("Not a valid VRF name\n", *argv
);
427 if (!name_is_vrf(*argv
))
428 invarg("Not a valid VRF name\n", *argv
);
429 addattr32(&req
.n
, sizeof(req
), NDA_MASTER
, ifindex
);
430 filter
.master
= ifindex
;
431 } else if (strcmp(*argv
, "unused") == 0) {
432 filter
.unused_only
= 1;
433 } else if (strcmp(*argv
, "nud") == 0) {
441 if (nud_state_a2n(&state
, *argv
)) {
442 if (strcmp(*argv
, "all") != 0)
443 invarg("nud state is bad", *argv
);
450 filter
.state
|= state
;
451 } else if (strcmp(*argv
, "proxy") == 0)
452 req
.ndm
.ndm_flags
= NTF_PROXY
;
454 if (strcmp(*argv
, "to") == 0) {
457 if (matches(*argv
, "help") == 0)
459 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
460 invarg("to value is invalid\n", *argv
);
461 if (filter
.family
== AF_UNSPEC
)
462 filter
.family
= filter
.pfx
.family
;
470 if ((filter
.index
= ll_name_to_index(filter_dev
)) == 0) {
471 fprintf(stderr
, "Cannot find device \"%s\"\n", filter_dev
);
474 addattr32(&req
.n
, sizeof(req
), NDA_IFINDEX
, filter
.index
);
477 req
.ndm
.ndm_family
= filter
.family
;
481 char flushb
[4096-512];
483 filter
.flushb
= flushb
;
485 filter
.flushe
= sizeof(flushb
);
487 while (round
< MAX_ROUNDS
) {
488 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
489 perror("Cannot send dump request");
493 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
494 fprintf(stderr
, "Flush terminated\n");
497 if (filter
.flushed
== 0) {
500 printf("Nothing to flush.\n");
502 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
508 if (flush_update() < 0)
511 printf("\n*** Round %d, deleting %d entries ***\n", round
, filter
.flushed
);
514 filter
.state
&= ~NUD_FAILED
;
516 printf("*** Flush not complete bailing out after %d rounds\n",
521 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
522 perror("Cannot send dump request");
527 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
528 fprintf(stderr
, "Dump terminated\n");
536 int do_ipneigh(int argc
, char **argv
)
539 if (matches(*argv
, "add") == 0)
540 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
541 if (matches(*argv
, "change") == 0 ||
542 strcmp(*argv
, "chg") == 0)
543 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_REPLACE
, argc
-1, argv
+1);
544 if (matches(*argv
, "replace") == 0)
545 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
546 if (matches(*argv
, "delete") == 0)
547 return ipneigh_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
548 if (matches(*argv
, "get") == 0) {
549 fprintf(stderr
, "Sorry, \"neigh get\" is not implemented :-(\n");
552 if (matches(*argv
, "show") == 0 ||
553 matches(*argv
, "lst") == 0 ||
554 matches(*argv
, "list") == 0)
555 return do_show_or_flush(argc
-1, argv
+1, 0);
556 if (matches(*argv
, "flush") == 0)
557 return do_show_or_flush(argc
-1, argv
+1, 1);
558 if (matches(*argv
, "help") == 0)
561 return do_show_or_flush(0, NULL
, 0);
563 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv
);