]>
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>
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)
47 static void usage(void) __attribute__((noreturn
));
49 static void usage(void)
52 "Usage: ip neigh { add | del | change | replace }\n"
53 " { ADDR [ lladdr LLADDR ] [ nud STATE ] | proxy ADDR } [ dev DEV ]\n"
54 " [ router ] [ extern_learn ] [ protocol PROTO ]\n"
56 " ip neigh { show | flush } [ proxy ] [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n"
59 "STATE := { permanent | noarp | stale | reachable | none |\n"
60 " incomplete | delay | probe | failed }\n");
64 static int nud_state_a2n(unsigned int *state
, const char *arg
)
66 if (matches(arg
, "permanent") == 0)
67 *state
= NUD_PERMANENT
;
68 else if (matches(arg
, "reachable") == 0)
69 *state
= NUD_REACHABLE
;
70 else if (strcmp(arg
, "noarp") == 0)
72 else if (strcmp(arg
, "none") == 0)
74 else if (strcmp(arg
, "stale") == 0)
76 else if (strcmp(arg
, "incomplete") == 0)
77 *state
= NUD_INCOMPLETE
;
78 else if (strcmp(arg
, "delay") == 0)
80 else if (strcmp(arg
, "probe") == 0)
82 else if (matches(arg
, "failed") == 0)
85 if (get_unsigned(state
, arg
, 0))
87 if (*state
>= 0x100 || (*state
&((*state
)-1)))
93 static int flush_update(void)
95 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
96 perror("Failed to send flush request");
104 static int ipneigh_modify(int cmd
, int flags
, int argc
, char **argv
)
111 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
)),
112 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
114 .ndm
.ndm_family
= preferred_family
,
115 .ndm
.ndm_state
= NUD_PERMANENT
,
125 if (matches(*argv
, "lladdr") == 0) {
128 duparg("lladdr", *argv
);
131 } else if (strcmp(*argv
, "nud") == 0) {
135 if (nud_state_a2n(&state
, *argv
))
136 invarg("nud state is bad", *argv
);
137 req
.ndm
.ndm_state
= state
;
138 } else if (matches(*argv
, "proxy") == 0) {
140 if (matches(*argv
, "help") == 0)
143 duparg("address", *argv
);
144 get_addr(&dst
, *argv
, preferred_family
);
147 req
.ndm
.ndm_flags
|= NTF_PROXY
;
148 } else if (strcmp(*argv
, "router") == 0) {
149 req
.ndm
.ndm_flags
|= NTF_ROUTER
;
150 } else if (matches(*argv
, "extern_learn") == 0) {
151 req
.ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
152 } else if (strcmp(*argv
, "dev") == 0) {
156 } else if (matches(*argv
, "protocol") == 0) {
160 if (rtnl_rtprot_a2n(&proto
, *argv
))
161 invarg("\"protocol\" value is invalid\n", *argv
);
162 if (addattr8(&req
.n
, sizeof(req
), NDA_PROTOCOL
, proto
))
165 if (strcmp(*argv
, "to") == 0) {
168 if (matches(*argv
, "help") == 0) {
172 duparg2("to", *argv
);
173 get_addr(&dst
, *argv
, preferred_family
);
178 if (!dev_ok
|| !dst_ok
|| dst
.family
== AF_UNSPEC
) {
179 fprintf(stderr
, "Device and destination are required arguments.\n");
182 req
.ndm
.ndm_family
= dst
.family
;
183 if (addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
) < 0)
186 if (lla
&& strcmp(lla
, "null")) {
190 l
= ll_addr_a2n(llabuf
, sizeof(llabuf
), lla
);
194 if (addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, llabuf
, l
) < 0)
201 req
.ndm
.ndm_ifindex
= ll_name_to_index(dev
);
202 if (!req
.ndm
.ndm_ifindex
)
206 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
212 static void print_cacheinfo(const struct nda_cacheinfo
*ci
)
220 print_uint(PRINT_ANY
, "refcnt",
221 " ref %u", ci
->ndm_refcnt
);
223 print_uint(PRINT_ANY
, "used", " used %u", ci
->ndm_used
/ hz
);
224 print_uint(PRINT_ANY
, "confirmed", "/%u", ci
->ndm_confirmed
/ hz
);
225 print_uint(PRINT_ANY
, "updated", "/%u", ci
->ndm_updated
/ hz
);
228 static void print_neigh_state(unsigned int nud
)
231 open_json_array(PRINT_JSON
,
232 is_json_context() ? "state" : "");
234 #define PRINT_FLAG(f) \
235 if (nud & NUD_##f) { \
237 print_string(PRINT_ANY, NULL, " %s", #f); \
240 PRINT_FLAG(INCOMPLETE
);
241 PRINT_FLAG(REACHABLE
);
247 PRINT_FLAG(PERMANENT
);
250 close_json_array(PRINT_JSON
, NULL
);
253 int print_neigh(struct nlmsghdr
*n
, void *arg
)
255 FILE *fp
= (FILE *)arg
;
256 struct ndmsg
*r
= NLMSG_DATA(n
);
257 int len
= n
->nlmsg_len
;
258 struct rtattr
*tb
[NDA_MAX
+1];
259 static int logit
= 1;
262 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
&&
263 n
->nlmsg_type
!= RTM_GETNEIGH
) {
264 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
265 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
269 len
-= NLMSG_LENGTH(sizeof(*r
));
271 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
275 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWNEIGH
)
278 if (filter
.family
&& filter
.family
!= r
->ndm_family
)
280 if (filter
.index
&& filter
.index
!= r
->ndm_ifindex
)
282 if (!(filter
.state
&r
->ndm_state
) &&
283 !(r
->ndm_flags
& NTF_PROXY
) &&
284 !(r
->ndm_flags
& NTF_EXT_LEARNED
) &&
285 (r
->ndm_state
|| !(filter
.state
&0x100)) &&
286 (r
->ndm_family
!= AF_DECnet
))
289 if (filter
.master
&& !(n
->nlmsg_flags
& NLM_F_DUMP_FILTERED
)) {
293 "\nWARNING: Kernel does not support filtering by master device\n\n");
297 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
299 if (inet_addr_match_rta(&filter
.pfx
, tb
[NDA_DST
]))
302 if (tb
[NDA_PROTOCOL
])
303 protocol
= rta_getattr_u8(tb
[NDA_PROTOCOL
]);
305 if (filter
.protocol
&& filter
.protocol
!= protocol
)
308 if (filter
.unused_only
&& tb
[NDA_CACHEINFO
]) {
309 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
318 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
322 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
323 memcpy(fn
, n
, n
->nlmsg_len
);
324 fn
->nlmsg_type
= RTM_DELNEIGH
;
325 fn
->nlmsg_flags
= NLM_F_REQUEST
;
326 fn
->nlmsg_seq
= ++rth
.seq
;
327 filter
.flushp
= (((char *)fn
) + n
->nlmsg_len
) - filter
.flushb
;
333 open_json_object(NULL
);
334 if (n
->nlmsg_type
== RTM_DELNEIGH
)
335 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
336 else if (n
->nlmsg_type
== RTM_GETNEIGH
)
337 print_null(PRINT_ANY
, "miss", "%s ", "miss");
341 int family
= r
->ndm_family
;
343 if (family
== AF_BRIDGE
) {
344 if (RTA_PAYLOAD(tb
[NDA_DST
]) == sizeof(struct in6_addr
))
350 dst
= format_host_rta(family
, tb
[NDA_DST
]);
351 print_color_string(PRINT_ANY
,
352 ifa_family_color(family
),
356 if (!filter
.index
&& r
->ndm_ifindex
) {
357 if (!is_json_context())
360 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
362 ll_index_to_name(r
->ndm_ifindex
));
365 if (tb
[NDA_LLADDR
]) {
369 lladdr
= ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
370 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
371 ll_index_to_type(r
->ndm_ifindex
),
374 if (!is_json_context())
375 fprintf(fp
, "lladdr ");
377 print_color_string(PRINT_ANY
, COLOR_MAC
,
378 "lladdr", "%s", lladdr
);
381 if (r
->ndm_flags
& NTF_ROUTER
)
382 print_null(PRINT_ANY
, "router", " %s", "router");
384 if (r
->ndm_flags
& NTF_PROXY
)
385 print_null(PRINT_ANY
, "proxy", " %s", "proxy");
387 if (r
->ndm_flags
& NTF_EXT_LEARNED
)
388 print_null(PRINT_ANY
, "extern_learn", " %s ", "extern_learn");
390 if (r
->ndm_flags
& NTF_OFFLOADED
)
391 print_null(PRINT_ANY
, "offload", " %s", "offload");
394 if (tb
[NDA_CACHEINFO
])
395 print_cacheinfo(RTA_DATA(tb
[NDA_CACHEINFO
]));
398 print_uint(PRINT_ANY
, "probes", " probes %u",
399 rta_getattr_u32(tb
[NDA_PROBES
]));
403 print_neigh_state(r
->ndm_state
);
408 print_string(PRINT_ANY
, "protocol", " proto %s ",
409 rtnl_rtprot_n2a(protocol
, b1
, sizeof(b1
)));
412 print_string(PRINT_FP
, NULL
, "\n", "");
419 void ipneigh_reset_filter(int ifindex
)
421 memset(&filter
, 0, sizeof(filter
));
423 filter
.index
= ifindex
;
426 static int ipneigh_dump_filter(struct nlmsghdr
*nlh
, int reqlen
)
428 struct ndmsg
*ndm
= NLMSG_DATA(nlh
);
431 ndm
->ndm_flags
= filter
.ndm_flags
;
434 err
= addattr32(nlh
, reqlen
, NDA_IFINDEX
, filter
.index
);
439 err
= addattr32(nlh
, reqlen
, NDA_MASTER
, filter
.master
);
447 static int do_show_or_flush(int argc
, char **argv
, int flush
)
449 char *filter_dev
= NULL
;
452 ipneigh_reset_filter(0);
455 filter
.family
= preferred_family
;
459 fprintf(stderr
, "Flush requires arguments.\n");
462 filter
.state
= ~(NUD_PERMANENT
|NUD_NOARP
);
464 filter
.state
= 0xFF & ~NUD_NOARP
;
467 if (strcmp(*argv
, "dev") == 0) {
470 duparg("dev", *argv
);
472 } else if (strcmp(*argv
, "master") == 0) {
476 ifindex
= ll_name_to_index(*argv
);
478 invarg("Device does not exist\n", *argv
);
479 filter
.master
= ifindex
;
480 } else if (strcmp(*argv
, "vrf") == 0) {
484 ifindex
= ll_name_to_index(*argv
);
486 invarg("Not a valid VRF name\n", *argv
);
487 if (!name_is_vrf(*argv
))
488 invarg("Not a valid VRF name\n", *argv
);
489 filter
.master
= ifindex
;
490 } else if (strcmp(*argv
, "unused") == 0) {
491 filter
.unused_only
= 1;
492 } else if (strcmp(*argv
, "nud") == 0) {
500 if (nud_state_a2n(&state
, *argv
)) {
501 if (strcmp(*argv
, "all") != 0)
502 invarg("nud state is bad", *argv
);
509 filter
.state
|= state
;
510 } else if (strcmp(*argv
, "proxy") == 0) {
511 filter
.ndm_flags
= NTF_PROXY
;
512 } else if (matches(*argv
, "protocol") == 0) {
516 if (rtnl_rtprot_a2n(&prot
, *argv
)) {
517 if (strcmp(*argv
, "all"))
518 invarg("invalid \"protocol\"\n", *argv
);
521 filter
.protocol
= prot
;
523 if (strcmp(*argv
, "to") == 0) {
526 if (matches(*argv
, "help") == 0)
528 if (get_prefix(&filter
.pfx
, *argv
, filter
.family
))
529 invarg("to value is invalid\n", *argv
);
530 if (filter
.family
== AF_UNSPEC
)
531 filter
.family
= filter
.pfx
.family
;
539 filter
.index
= ll_name_to_index(filter_dev
);
541 return nodev(filter_dev
);
546 char flushb
[4096-512];
548 filter
.flushb
= flushb
;
550 filter
.flushe
= sizeof(flushb
);
552 while (round
< MAX_ROUNDS
) {
553 if (rtnl_neighdump_req(&rth
, filter
.family
,
554 ipneigh_dump_filter
) < 0) {
555 perror("Cannot send dump request");
559 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
560 fprintf(stderr
, "Flush terminated\n");
563 if (filter
.flushed
== 0) {
566 printf("Nothing to flush.\n");
568 printf("*** Flush is complete after %d round%s ***\n", round
, round
> 1?"s":"");
574 if (flush_update() < 0)
577 printf("\n*** Round %d, deleting %d entries ***\n", round
, filter
.flushed
);
580 filter
.state
&= ~NUD_FAILED
;
582 printf("*** Flush not complete bailing out after %d rounds\n",
587 if (rtnl_neighdump_req(&rth
, filter
.family
, ipneigh_dump_filter
) < 0) {
588 perror("Cannot send dump request");
593 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
594 fprintf(stderr
, "Dump terminated\n");
602 int do_ipneigh(int argc
, char **argv
)
605 if (matches(*argv
, "add") == 0)
606 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
607 if (matches(*argv
, "change") == 0 ||
608 strcmp(*argv
, "chg") == 0)
609 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_REPLACE
, argc
-1, argv
+1);
610 if (matches(*argv
, "replace") == 0)
611 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
612 if (matches(*argv
, "delete") == 0)
613 return ipneigh_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
614 if (matches(*argv
, "get") == 0) {
615 fprintf(stderr
, "Sorry, \"neigh get\" is not implemented :-(\n");
618 if (matches(*argv
, "show") == 0 ||
619 matches(*argv
, "lst") == 0 ||
620 matches(*argv
, "list") == 0)
621 return do_show_or_flush(argc
-1, argv
+1, 0);
622 if (matches(*argv
, "flush") == 0)
623 return do_show_or_flush(argc
-1, argv
+1, 1);
624 if (matches(*argv
, "help") == 0)
627 return do_show_or_flush(0, NULL
, 0);
629 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv
);