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 int nud_state_a2n(unsigned *state, 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 addattr_l(&req.n, sizeof(req), NDA_DST, &dst.data, dst.bytelen);
162 if (lla && strcmp(lla, "null")) {
166 l = ll_addr_a2n(llabuf, sizeof(llabuf), lla);
167 addattr_l(&req.n, sizeof(req), NDA_LLADDR, llabuf, l);
172 if ((req.ndm.ndm_ifindex = ll_name_to_index(d)) == 0) {
173 fprintf(stderr, "Cannot find device \"%s\"\n", d);
177 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
184 int print_neigh(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
186 FILE *fp = (FILE*)arg;
187 struct ndmsg *r = NLMSG_DATA(n);
188 int len = n->nlmsg_len;
189 struct rtattr * tb[NDA_MAX+1];
192 if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH) {
193 fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
194 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
198 len -= NLMSG_LENGTH(sizeof(*r));
200 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
204 if (filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
207 if (filter.family && filter.family != r->ndm_family)
209 if (filter.index && filter.index != r->ndm_ifindex)
211 if (!(filter.state&r->ndm_state) &&
212 (r->ndm_state || !(filter.state&0x100)) &&
213 (r->ndm_family != AF_DECnet))
216 parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));
219 if (filter.pfx.family) {
221 memset(&dst, 0, sizeof(dst));
222 dst.family = r->ndm_family;
223 memcpy(&dst.data, RTA_DATA(tb[NDA_DST]), RTA_PAYLOAD(tb[NDA_DST]));
224 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
228 if (filter.unused_only && tb[NDA_CACHEINFO]) {
229 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
236 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
240 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
241 memcpy(fn, n, n->nlmsg_len);
242 fn->nlmsg_type = RTM_DELNEIGH;
243 fn->nlmsg_flags = NLM_F_REQUEST;
244 fn->nlmsg_seq = ++rth.seq;
245 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
253 format_host(r->ndm_family,
254 RTA_PAYLOAD(tb[NDA_DST]),
255 RTA_DATA(tb[NDA_DST]),
256 abuf, sizeof(abuf)));
258 if (!filter.index && r->ndm_ifindex)
259 fprintf(fp, "dev %s ", ll_index_to_name(r->ndm_ifindex));
260 if (tb[NDA_LLADDR]) {
262 fprintf(fp, "lladdr %s", ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
263 RTA_PAYLOAD(tb[NDA_LLADDR]),
264 ll_index_to_type(r->ndm_ifindex),
267 if (r->ndm_flags & NTF_ROUTER) {
268 fprintf(fp, " router");
270 if (tb[NDA_CACHEINFO] && show_stats) {
271 struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);
272 int hz = get_user_hz();
275 printf(" ref %d", ci->ndm_refcnt);
276 fprintf(fp, " used %d/%d/%d", ci->ndm_used/hz,
277 ci->ndm_confirmed/hz, ci->ndm_updated/hz);
280 if (tb[NDA_PROBES] && show_stats) {
281 __u32 p = *(__u32 *) RTA_DATA(tb[NDA_PROBES]);
282 fprintf(fp, " probes %u", p);
286 int nud = r->ndm_state;
289 #define PRINT_FLAG(f) if (nud & NUD_##f) { \
290 nud &= ~NUD_##f; fprintf(fp, #f "%s", nud ? "," : ""); }
291 PRINT_FLAG(INCOMPLETE);
292 PRINT_FLAG(REACHABLE);
298 PRINT_FLAG(PERMANENT);
307 void ipneigh_reset_filter()
309 memset(&filter, 0, sizeof(filter));
313 int do_show_or_flush(int argc, char **argv, int flush)
315 char *filter_dev = NULL;
318 ipneigh_reset_filter();
321 filter.family = preferred_family;
325 fprintf(stderr, "Flush requires arguments.\n");
328 filter.state = ~(NUD_PERMANENT|NUD_NOARP);
330 filter.state = 0xFF & ~NUD_NOARP;
333 if (strcmp(*argv, "dev") == 0) {
336 duparg("dev", *argv);
338 } else if (strcmp(*argv, "unused") == 0) {
339 filter.unused_only = 1;
340 } else if (strcmp(*argv, "nud") == 0) {
347 if (nud_state_a2n(&state, *argv)) {
348 if (strcmp(*argv, "all") != 0)
349 invarg("nud state is bad", *argv);
356 filter.state |= state;
358 if (strcmp(*argv, "to") == 0) {
361 if (matches(*argv, "help") == 0)
363 get_prefix(&filter.pfx, *argv, filter.family);
364 if (filter.family == AF_UNSPEC)
365 filter.family = filter.pfx.family;
373 if ((filter.index = ll_name_to_index(filter_dev)) == 0) {
374 fprintf(stderr, "Cannot find device \"%s\"\n", filter_dev);
381 char flushb[4096-512];
383 filter.flushb = flushb;
385 filter.flushe = sizeof(flushb);
386 filter.state &= ~NUD_FAILED;
388 while (round < MAX_ROUNDS) {
389 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNEIGH) < 0) {
390 perror("Cannot send dump request");
394 if (rtnl_dump_filter(&rth, print_neigh, stdout, NULL, NULL) < 0) {
395 fprintf(stderr, "Flush terminated\n");
398 if (filter.flushed == 0) {
401 printf("Nothing to flush.\n");
403 printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
409 if (flush_update() < 0)
412 printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
416 printf("*** Flush not complete bailing out after %d rounds\n",
421 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNEIGH) < 0) {
422 perror("Cannot send dump request");
426 if (rtnl_dump_filter(&rth, print_neigh, stdout, NULL, NULL) < 0) {
427 fprintf(stderr, "Dump terminated\n");
434 int do_ipneigh(int argc, char **argv)
437 if (matches(*argv, "add") == 0)
438 return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1);
439 if (matches(*argv, "change") == 0 ||
440 strcmp(*argv, "chg") == 0)
441 return ipneigh_modify(RTM_NEWNEIGH, NLM_F_REPLACE, argc-1, argv+1);
442 if (matches(*argv, "replace") == 0)
443 return ipneigh_modify(RTM_NEWNEIGH, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
444 if (matches(*argv, "delete") == 0)
445 return ipneigh_modify(RTM_DELNEIGH, 0, argc-1, argv+1);
446 if (matches(*argv, "get") == 0) {
447 fprintf(stderr, "Sorry, \"neigh get\" is not implemented :-(\n");
450 if (matches(*argv, "show") == 0 ||
451 matches(*argv, "lst") == 0 ||
452 matches(*argv, "list") == 0)
453 return do_show_or_flush(argc-1, argv+1, 0);
454 if (matches(*argv, "flush") == 0)
455 return do_show_or_flush(argc-1, argv+1, 1);
456 if (matches(*argv, "help") == 0)
459 return do_show_or_flush(0, NULL, 0);
461 fprintf(stderr, "Command \"%s\" is unknown, try \"ip neigh help\".\n", *argv);