]>
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)
44 static void usage(void) __attribute__((noreturn
));
46 static void usage(void)
48 fprintf(stderr
, "Usage: ip neigh { add | del | change | replace } { ADDR [ lladdr LLADDR ]\n"
49 " [ nud { permanent | noarp | stale | reachable } ]\n"
50 " | proxy ADDR } [ dev DEV ]\n");
51 fprintf(stderr
, " ip neigh {show|flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]\n");
55 static int nud_state_a2n(unsigned *state
, const char *arg
)
57 if (matches(arg
, "permanent") == 0)
58 *state
= NUD_PERMANENT
;
59 else if (matches(arg
, "reachable") == 0)
60 *state
= NUD_REACHABLE
;
61 else if (strcmp(arg
, "noarp") == 0)
63 else if (strcmp(arg
, "none") == 0)
65 else if (strcmp(arg
, "stale") == 0)
67 else if (strcmp(arg
, "incomplete") == 0)
68 *state
= NUD_INCOMPLETE
;
69 else if (strcmp(arg
, "delay") == 0)
71 else if (strcmp(arg
, "probe") == 0)
73 else if (matches(arg
, "failed") == 0)
76 if (get_unsigned(state
, arg
, 0))
78 if (*state
>=0x100 || (*state
&((*state
)-1)))
84 static int flush_update(void)
86 if (rtnl_send_check(&rth
, filter
.flushb
, filter
.flushp
) < 0) {
87 perror("Failed to send flush request");
95 static int ipneigh_modify(int cmd
, int flags
, int argc
, char **argv
)
108 memset(&req
, 0, sizeof(req
));
110 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
111 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
112 req
.n
.nlmsg_type
= cmd
;
113 req
.ndm
.ndm_family
= preferred_family
;
114 req
.ndm
.ndm_state
= NUD_PERMANENT
;
117 if (matches(*argv
, "lladdr") == 0) {
120 duparg("lladdr", *argv
);
123 } else if (strcmp(*argv
, "nud") == 0) {
126 if (nud_state_a2n(&state
, *argv
))
127 invarg("nud state is bad", *argv
);
128 req
.ndm
.ndm_state
= state
;
129 } else if (matches(*argv
, "proxy") == 0) {
131 if (matches(*argv
, "help") == 0)
134 duparg("address", *argv
);
135 get_addr(&dst
, *argv
, preferred_family
);
137 req
.ndm
.ndm_flags
|= NTF_PROXY
;
138 } else if (strcmp(*argv
, "dev") == 0) {
142 if (strcmp(*argv
, "to") == 0) {
145 if (matches(*argv
, "help") == 0) {
149 duparg2("to", *argv
);
150 get_addr(&dst
, *argv
, preferred_family
);
155 if (d
== NULL
|| !dst_ok
|| dst
.family
== AF_UNSPEC
) {
156 fprintf(stderr
, "Device and destination are required arguments.\n");
159 req
.ndm
.ndm_family
= dst
.family
;
160 if (addattr_l(&req
.n
, sizeof(req
), NDA_DST
, &dst
.data
, dst
.bytelen
) < 0)
163 if (lla
&& strcmp(lla
, "null")) {
167 l
= ll_addr_a2n(llabuf
, sizeof(llabuf
), lla
);
171 if (addattr_l(&req
.n
, sizeof(req
), NDA_LLADDR
, llabuf
, l
) < 0)
177 if ((req
.ndm
.ndm_ifindex
= ll_name_to_index(d
)) == 0) {
178 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
182 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
189 int print_neigh(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
191 FILE *fp
= (FILE*)arg
;
192 struct ndmsg
*r
= NLMSG_DATA(n
);
193 int len
= n
->nlmsg_len
;
194 struct rtattr
* tb
[NDA_MAX
+1];
197 if (n
->nlmsg_type
!= RTM_NEWNEIGH
&& n
->nlmsg_type
!= RTM_DELNEIGH
&&
198 n
->nlmsg_type
!= RTM_GETNEIGH
) {
199 fprintf(stderr
, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
200 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
204 len
-= NLMSG_LENGTH(sizeof(*r
));
206 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
210 if (filter
.flushb
&& n
->nlmsg_type
!= RTM_NEWNEIGH
)
213 if (filter
.family
&& filter
.family
!= r
->ndm_family
)
215 if (filter
.index
&& filter
.index
!= r
->ndm_ifindex
)
217 if (!(filter
.state
&r
->ndm_state
) &&
218 !(r
->ndm_flags
& NTF_PROXY
) &&
219 (r
->ndm_state
|| !(filter
.state
&0x100)) &&
220 (r
->ndm_family
!= AF_DECnet
))
223 parse_rtattr(tb
, NDA_MAX
, NDA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
226 if (filter
.pfx
.family
) {
228 memset(&dst
, 0, sizeof(dst
));
229 dst
.family
= r
->ndm_family
;
230 memcpy(&dst
.data
, RTA_DATA(tb
[NDA_DST
]), RTA_PAYLOAD(tb
[NDA_DST
]));
231 if (inet_addr_match(&dst
, &filter
.pfx
, filter
.pfx
.bitlen
))
235 if (filter
.unused_only
&& tb
[NDA_CACHEINFO
]) {
236 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
243 if (NLMSG_ALIGN(filter
.flushp
) + n
->nlmsg_len
> filter
.flushe
) {
247 fn
= (struct nlmsghdr
*)(filter
.flushb
+ NLMSG_ALIGN(filter
.flushp
));
248 memcpy(fn
, n
, n
->nlmsg_len
);
249 fn
->nlmsg_type
= RTM_DELNEIGH
;
250 fn
->nlmsg_flags
= NLM_F_REQUEST
;
251 fn
->nlmsg_seq
= ++rth
.seq
;
252 filter
.flushp
= (((char*)fn
) + n
->nlmsg_len
) - filter
.flushb
;
258 if (n
->nlmsg_type
== RTM_DELNEIGH
)
259 fprintf(fp
, "delete ");
260 else if (n
->nlmsg_type
== RTM_GETNEIGH
)
261 fprintf(fp
, "miss ");
264 format_host(r
->ndm_family
,
265 RTA_PAYLOAD(tb
[NDA_DST
]),
266 RTA_DATA(tb
[NDA_DST
]),
267 abuf
, sizeof(abuf
)));
269 if (!filter
.index
&& r
->ndm_ifindex
)
270 fprintf(fp
, "dev %s ", ll_index_to_name(r
->ndm_ifindex
));
271 if (tb
[NDA_LLADDR
]) {
273 fprintf(fp
, "lladdr %s", ll_addr_n2a(RTA_DATA(tb
[NDA_LLADDR
]),
274 RTA_PAYLOAD(tb
[NDA_LLADDR
]),
275 ll_index_to_type(r
->ndm_ifindex
),
278 if (r
->ndm_flags
& NTF_ROUTER
) {
279 fprintf(fp
, " router");
281 if (r
->ndm_flags
& NTF_PROXY
) {
282 fprintf(fp
, " proxy");
284 if (tb
[NDA_CACHEINFO
] && show_stats
) {
285 struct nda_cacheinfo
*ci
= RTA_DATA(tb
[NDA_CACHEINFO
]);
286 int hz
= get_user_hz();
289 printf(" ref %d", ci
->ndm_refcnt
);
290 fprintf(fp
, " used %d/%d/%d", ci
->ndm_used
/hz
,
291 ci
->ndm_confirmed
/hz
, ci
->ndm_updated
/hz
);
294 if (tb
[NDA_PROBES
] && show_stats
) {
295 __u32 p
= rta_getattr_u32(tb
[NDA_PROBES
]);
296 fprintf(fp
, " probes %u", p
);
300 int nud
= r
->ndm_state
;
303 #define PRINT_FLAG(f) if (nud & NUD_##f) { \
304 nud &= ~NUD_##f; fprintf(fp, #f "%s", nud ? "," : ""); }
305 PRINT_FLAG(INCOMPLETE
);
306 PRINT_FLAG(REACHABLE
);
312 PRINT_FLAG(PERMANENT
);
321 void ipneigh_reset_filter(int ifindex
)
323 memset(&filter
, 0, sizeof(filter
));
325 filter
.index
= ifindex
;
328 static int do_show_or_flush(int argc
, char **argv
, int flush
)
330 char *filter_dev
= NULL
;
332 struct ndmsg ndm
= { 0 };
334 ipneigh_reset_filter(0);
337 filter
.family
= preferred_family
;
341 fprintf(stderr
, "Flush requires arguments.\n");
344 filter
.state
= ~(NUD_PERMANENT
|NUD_NOARP
);
346 filter
.state
= 0xFF & ~NUD_NOARP
;
349 if (strcmp(*argv
, "dev") == 0) {
352 duparg("dev", *argv
);
354 } else if (strcmp(*argv
, "unused") == 0) {
355 filter
.unused_only
= 1;
356 } else if (strcmp(*argv
, "nud") == 0) {
363 if (nud_state_a2n(&state
, *argv
)) {
364 if (strcmp(*argv
, "all") != 0)
365 invarg("nud state is bad", *argv
);
372 filter
.state
|= state
;
373 } else if (strcmp(*argv
, "proxy") == 0)
374 ndm
.ndm_flags
= NTF_PROXY
;
376 if (strcmp(*argv
, "to") == 0) {
379 if (matches(*argv
, "help") == 0)
381 get_prefix(&filter
.pfx
, *argv
, filter
.family
);
382 if (filter
.family
== AF_UNSPEC
)
383 filter
.family
= filter
.pfx
.family
;
391 if ((filter
.index
= ll_name_to_index(filter_dev
)) == 0) {
392 fprintf(stderr
, "Cannot find device \"%s\"\n", filter_dev
);
399 char flushb
[4096-512];
401 filter
.flushb
= flushb
;
403 filter
.flushe
= sizeof(flushb
);
404 filter
.state
&= ~NUD_FAILED
;
406 while (round
< MAX_ROUNDS
) {
407 if (rtnl_wilddump_request(&rth
, filter
.family
, RTM_GETNEIGH
) < 0) {
408 perror("Cannot send dump request");
412 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
413 fprintf(stderr
, "Flush terminated\n");
416 if (filter
.flushed
== 0) {
419 printf("Nothing to flush.\n");
421 printf("*** Flush is complete after %d round%s ***\n", round
, round
>1?"s":"");
427 if (flush_update() < 0)
430 printf("\n*** Round %d, deleting %d entries ***\n", round
, filter
.flushed
);
434 printf("*** Flush not complete bailing out after %d rounds\n",
439 ndm
.ndm_family
= filter
.family
;
441 if (rtnl_dump_request(&rth
, RTM_GETNEIGH
, &ndm
, sizeof(struct ndmsg
)) < 0) {
442 perror("Cannot send dump request");
446 if (rtnl_dump_filter(&rth
, print_neigh
, stdout
) < 0) {
447 fprintf(stderr
, "Dump terminated\n");
454 int do_ipneigh(int argc
, char **argv
)
457 if (matches(*argv
, "add") == 0)
458 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
459 if (matches(*argv
, "change") == 0 ||
460 strcmp(*argv
, "chg") == 0)
461 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_REPLACE
, argc
-1, argv
+1);
462 if (matches(*argv
, "replace") == 0)
463 return ipneigh_modify(RTM_NEWNEIGH
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
464 if (matches(*argv
, "delete") == 0)
465 return ipneigh_modify(RTM_DELNEIGH
, 0, argc
-1, argv
+1);
466 if (matches(*argv
, "get") == 0) {
467 fprintf(stderr
, "Sorry, \"neigh get\" is not implemented :-(\n");
470 if (matches(*argv
, "show") == 0 ||
471 matches(*argv
, "lst") == 0 ||
472 matches(*argv
, "list") == 0)
473 return do_show_or_flush(argc
-1, argv
+1, 0);
474 if (matches(*argv
, "flush") == 0)
475 return do_show_or_flush(argc
-1, argv
+1, 1);
476 if (matches(*argv
, "help") == 0)
479 return do_show_or_flush(0, NULL
, 0);
481 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv
);