blob: 879626ee4f491bcdd88c3d88f7f6dd60796a51b5 [file] [log] [blame]
shemminger143969f2006-01-10 18:50:18 +00001/*
2 * Copyright (C)2006 USAGI/WIDE Project
Stephen Hemmingerae665a52006-12-05 10:10:22 -08003 *
shemminger143969f2006-01-10 18:50:18 +00004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
Stephen Hemmingerae665a52006-12-05 10:10:22 -08008 *
shemminger143969f2006-01-10 18:50:18 +00009 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Stephen Hemmingerae665a52006-12-05 10:10:22 -080013 *
shemminger143969f2006-01-10 18:50:18 +000014 * You should have received a copy of the GNU General Public License
Stephen Hemminger4d98ab02013-12-06 15:05:07 -080015 * along with this program; if not, see <http://www.gnu.org/licenses>.
shemminger143969f2006-01-10 18:50:18 +000016 */
17/*
18 * based on ipneigh.c
19 */
20/*
21 * Authors:
22 * Masahide NAKAMURA @USAGI
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/time.h>
Strake5bd9dd42012-12-23 08:46:04 -050029#include <sys/socket.h>
shemminger143969f2006-01-10 18:50:18 +000030#include <time.h>
31
32#include "utils.h"
33#include "ip_common.h"
34
35static struct
36{
37 int family;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070038 int index;
shemminger143969f2006-01-10 18:50:18 +000039#define NONE_DEV (-1)
40 char name[1024];
41} filter;
42
43static void usage(void) __attribute__((noreturn));
44
45static void usage(void)
46{
47 fprintf(stderr,
48 "Usage: ip ntable change name NAME [ dev DEV ]\n"
49 " [ thresh1 VAL ] [ thresh2 VAL ] [ thresh3 VAL ] [ gc_int MSEC ]\n"
50 " [ PARMS ]\n"
51 "Usage: ip ntable show [ dev DEV ] [ name NAME ]\n"
52
53 "PARMS := [ base_reachable MSEC ] [ retrans MSEC ] [ gc_stale MSEC ]\n"
54 " [ delay_probe MSEC ] [ queue LEN ]\n"
Phil Sutterf1fdcfe2016-03-02 19:19:53 +010055 " [ app_probes VAL ] [ ucast_probes VAL ] [ mcast_probes VAL ]\n"
shemminger143969f2006-01-10 18:50:18 +000056 " [ anycast_delay MSEC ] [ proxy_delay MSEC ] [ proxy_queue LEN ]\n"
57 " [ locktime MSEC ]\n"
58 );
59
60 exit(-1);
61}
62
63static int ipntable_modify(int cmd, int flags, int argc, char **argv)
64{
65 struct {
Stephen Hemminger48068672014-02-17 10:56:31 -080066 struct nlmsghdr n;
shemminger143969f2006-01-10 18:50:18 +000067 struct ndtmsg ndtm;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070068 char buf[1024];
Phil Sutterd17b1362016-07-18 16:48:42 +020069 } req = {
70 .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)),
71 .n.nlmsg_flags = NLM_F_REQUEST | flags,
72 .n.nlmsg_type = cmd,
73 .ndtm.ndtm_family = preferred_family,
74 };
shemminger143969f2006-01-10 18:50:18 +000075 char *namep = NULL;
76 char *threshsp = NULL;
77 char *gc_intp = NULL;
Phil Sutterd17b1362016-07-18 16:48:42 +020078 char parms_buf[1024] = {};
shemminger143969f2006-01-10 18:50:18 +000079 struct rtattr *parms_rta = (struct rtattr *)parms_buf;
80 int parms_change = 0;
81
shemminger143969f2006-01-10 18:50:18 +000082 parms_rta->rta_type = NDTA_PARMS;
83 parms_rta->rta_len = RTA_LENGTH(0);
84
85 while (argc > 0) {
86 if (strcmp(*argv, "name") == 0) {
87 int len;
88
89 NEXT_ARG();
90 if (namep)
91 duparg("NAME", *argv);
92
93 namep = *argv;
94 len = strlen(namep) + 1;
95 addattr_l(&req.n, sizeof(req), NDTA_NAME, namep, len);
96 } else if (strcmp(*argv, "thresh1") == 0) {
97 __u32 thresh1;
98
99 NEXT_ARG();
100 threshsp = *argv;
101
102 if (get_u32(&thresh1, *argv, 0))
103 invarg("\"thresh1\" value is invalid", *argv);
104
105 addattr32(&req.n, sizeof(req), NDTA_THRESH1, thresh1);
106 } else if (strcmp(*argv, "thresh2") == 0) {
107 __u32 thresh2;
108
109 NEXT_ARG();
110 threshsp = *argv;
111
112 if (get_u32(&thresh2, *argv, 0))
113 invarg("\"thresh2\" value is invalid", *argv);
114
115 addattr32(&req.n, sizeof(req), NDTA_THRESH2, thresh2);
116 } else if (strcmp(*argv, "thresh3") == 0) {
117 __u32 thresh3;
118
119 NEXT_ARG();
120 threshsp = *argv;
121
122 if (get_u32(&thresh3, *argv, 0))
123 invarg("\"thresh3\" value is invalid", *argv);
124
125 addattr32(&req.n, sizeof(req), NDTA_THRESH3, thresh3);
126 } else if (strcmp(*argv, "gc_int") == 0) {
127 __u64 gc_int;
128
129 NEXT_ARG();
130 gc_intp = *argv;
131
132 if (get_u64(&gc_int, *argv, 0))
133 invarg("\"gc_int\" value is invalid", *argv);
134
135 addattr_l(&req.n, sizeof(req), NDTA_GC_INTERVAL,
136 &gc_int, sizeof(gc_int));
137 } else if (strcmp(*argv, "dev") == 0) {
138 __u32 ifindex;
139
140 NEXT_ARG();
141 ifindex = ll_name_to_index(*argv);
142 if (ifindex == 0) {
143 fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
144 return -1;
145 }
146
147 rta_addattr32(parms_rta, sizeof(parms_buf),
148 NDTPA_IFINDEX, ifindex);
149 } else if (strcmp(*argv, "base_reachable") == 0) {
150 __u64 breachable;
151
152 NEXT_ARG();
153
154 if (get_u64(&breachable, *argv, 0))
155 invarg("\"base_reachable\" value is invalid", *argv);
156
157 rta_addattr_l(parms_rta, sizeof(parms_buf),
158 NDTPA_BASE_REACHABLE_TIME,
159 &breachable, sizeof(breachable));
160 parms_change = 1;
161 } else if (strcmp(*argv, "retrans") == 0) {
162 __u64 retrans;
163
164 NEXT_ARG();
165
166 if (get_u64(&retrans, *argv, 0))
167 invarg("\"retrans\" value is invalid", *argv);
168
169 rta_addattr_l(parms_rta, sizeof(parms_buf),
170 NDTPA_RETRANS_TIME,
171 &retrans, sizeof(retrans));
172 parms_change = 1;
173 } else if (strcmp(*argv, "gc_stale") == 0) {
174 __u64 gc_stale;
175
176 NEXT_ARG();
177
178 if (get_u64(&gc_stale, *argv, 0))
179 invarg("\"gc_stale\" value is invalid", *argv);
180
181 rta_addattr_l(parms_rta, sizeof(parms_buf),
182 NDTPA_GC_STALETIME,
183 &gc_stale, sizeof(gc_stale));
184 parms_change = 1;
185 } else if (strcmp(*argv, "delay_probe") == 0) {
186 __u64 delay_probe;
187
188 NEXT_ARG();
189
190 if (get_u64(&delay_probe, *argv, 0))
191 invarg("\"delay_probe\" value is invalid", *argv);
192
193 rta_addattr_l(parms_rta, sizeof(parms_buf),
194 NDTPA_DELAY_PROBE_TIME,
195 &delay_probe, sizeof(delay_probe));
196 parms_change = 1;
197 } else if (strcmp(*argv, "queue") == 0) {
198 __u32 queue;
199
200 NEXT_ARG();
201
202 if (get_u32(&queue, *argv, 0))
203 invarg("\"queue\" value is invalid", *argv);
204
205 if (!parms_rta)
206 parms_rta = (struct rtattr *)&parms_buf;
207 rta_addattr32(parms_rta, sizeof(parms_buf),
208 NDTPA_QUEUE_LEN, queue);
209 parms_change = 1;
210 } else if (strcmp(*argv, "app_probes") == 0) {
211 __u32 aprobe;
212
213 NEXT_ARG();
214
215 if (get_u32(&aprobe, *argv, 0))
216 invarg("\"app_probes\" value is invalid", *argv);
217
218 rta_addattr32(parms_rta, sizeof(parms_buf),
219 NDTPA_APP_PROBES, aprobe);
220 parms_change = 1;
221 } else if (strcmp(*argv, "ucast_probes") == 0) {
222 __u32 uprobe;
223
224 NEXT_ARG();
225
226 if (get_u32(&uprobe, *argv, 0))
227 invarg("\"ucast_probes\" value is invalid", *argv);
228
229 rta_addattr32(parms_rta, sizeof(parms_buf),
230 NDTPA_UCAST_PROBES, uprobe);
231 parms_change = 1;
232 } else if (strcmp(*argv, "mcast_probes") == 0) {
233 __u32 mprobe;
234
235 NEXT_ARG();
236
237 if (get_u32(&mprobe, *argv, 0))
238 invarg("\"mcast_probes\" value is invalid", *argv);
239
240 rta_addattr32(parms_rta, sizeof(parms_buf),
241 NDTPA_MCAST_PROBES, mprobe);
242 parms_change = 1;
243 } else if (strcmp(*argv, "anycast_delay") == 0) {
244 __u64 anycast_delay;
245
246 NEXT_ARG();
247
248 if (get_u64(&anycast_delay, *argv, 0))
249 invarg("\"anycast_delay\" value is invalid", *argv);
250
251 rta_addattr_l(parms_rta, sizeof(parms_buf),
252 NDTPA_ANYCAST_DELAY,
253 &anycast_delay, sizeof(anycast_delay));
254 parms_change = 1;
255 } else if (strcmp(*argv, "proxy_delay") == 0) {
256 __u64 proxy_delay;
257
258 NEXT_ARG();
259
260 if (get_u64(&proxy_delay, *argv, 0))
261 invarg("\"proxy_delay\" value is invalid", *argv);
262
263 rta_addattr_l(parms_rta, sizeof(parms_buf),
264 NDTPA_PROXY_DELAY,
265 &proxy_delay, sizeof(proxy_delay));
266 parms_change = 1;
267 } else if (strcmp(*argv, "proxy_queue") == 0) {
268 __u32 pqueue;
269
270 NEXT_ARG();
271
272 if (get_u32(&pqueue, *argv, 0))
273 invarg("\"proxy_queue\" value is invalid", *argv);
274
275 rta_addattr32(parms_rta, sizeof(parms_buf),
276 NDTPA_PROXY_QLEN, pqueue);
277 parms_change = 1;
278 } else if (strcmp(*argv, "locktime") == 0) {
279 __u64 locktime;
280
281 NEXT_ARG();
282
283 if (get_u64(&locktime, *argv, 0))
284 invarg("\"locktime\" value is invalid", *argv);
285
286 rta_addattr_l(parms_rta, sizeof(parms_buf),
287 NDTPA_LOCKTIME,
288 &locktime, sizeof(locktime));
289 parms_change = 1;
290 } else {
291 invarg("unknown", *argv);
292 }
293
294 argc--; argv++;
295 }
296
297 if (!namep)
298 missarg("NAME");
299 if (!threshsp && !gc_intp && !parms_change) {
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700300 fprintf(stderr, "Not enough information: changeable attributes required.\n");
shemminger143969f2006-01-10 18:50:18 +0000301 exit(-1);
302 }
303
304 if (parms_rta->rta_len > RTA_LENGTH(0)) {
305 addattr_l(&req.n, sizeof(req), NDTA_PARMS, RTA_DATA(parms_rta),
306 RTA_PAYLOAD(parms_rta));
307 }
308
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700309 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
shemminger143969f2006-01-10 18:50:18 +0000310 exit(2);
311
312 return 0;
313}
314
315static const char *ntable_strtime_delta(__u32 msec)
316{
317 static char str[32];
Phil Sutterd17b1362016-07-18 16:48:42 +0200318 struct timeval now = {};
shemminger143969f2006-01-10 18:50:18 +0000319 time_t t;
320 struct tm *tp;
321
322 if (msec == 0)
323 goto error;
324
shemminger143969f2006-01-10 18:50:18 +0000325 if (gettimeofday(&now, NULL) < 0) {
326 perror("gettimeofday");
327 goto error;
328 }
329
330 t = now.tv_sec - (msec / 1000);
331 tp = localtime(&t);
332 if (!tp)
333 goto error;
334
335 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
336
337 return str;
338 error:
339 strcpy(str, "(error)");
340 return str;
341}
342
Phil Sutterf7b49a32015-11-06 18:54:08 +0100343static int print_ntable(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
shemminger143969f2006-01-10 18:50:18 +0000344{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700345 FILE *fp = (FILE *)arg;
shemminger143969f2006-01-10 18:50:18 +0000346 struct ndtmsg *ndtm = NLMSG_DATA(n);
347 int len = n->nlmsg_len;
348 struct rtattr *tb[NDTA_MAX+1];
349 struct rtattr *tpb[NDTPA_MAX+1];
350 int ret;
351
352 if (n->nlmsg_type != RTM_NEWNEIGHTBL) {
353 fprintf(stderr, "Not NEIGHTBL: %08x %08x %08x\n",
354 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
355 return 0;
356 }
357 len -= NLMSG_LENGTH(sizeof(*ndtm));
358 if (len < 0) {
359 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
360 return -1;
361 }
362
363 if (preferred_family && preferred_family != ndtm->ndtm_family)
364 return 0;
365
366 parse_rtattr(tb, NDTA_MAX, NDTA_RTA(ndtm),
367 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ndtm)));
368
369 if (tb[NDTA_NAME]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700370 const char *name = rta_getattr_str(tb[NDTA_NAME]);
shemminger143969f2006-01-10 18:50:18 +0000371
372 if (strlen(filter.name) > 0 && strcmp(filter.name, name))
373 return 0;
374 }
375 if (tb[NDTA_PARMS]) {
376 parse_rtattr(tpb, NDTPA_MAX, RTA_DATA(tb[NDTA_PARMS]),
377 RTA_PAYLOAD(tb[NDTA_PARMS]));
378
379 if (tpb[NDTPA_IFINDEX]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700380 __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]);
shemminger143969f2006-01-10 18:50:18 +0000381
382 if (filter.index && filter.index != ifindex)
383 return 0;
384 } else {
385 if (filter.index && filter.index != NONE_DEV)
386 return 0;
387 }
388 }
389
390 if (ndtm->ndtm_family == AF_INET)
391 fprintf(fp, "inet ");
392 else if (ndtm->ndtm_family == AF_INET6)
393 fprintf(fp, "inet6 ");
394 else if (ndtm->ndtm_family == AF_DECnet)
395 fprintf(fp, "dnet ");
396 else
397 fprintf(fp, "(%d) ", ndtm->ndtm_family);
398
399 if (tb[NDTA_NAME]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700400 const char *name = rta_getattr_str(tb[NDTA_NAME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700401
shemminger143969f2006-01-10 18:50:18 +0000402 fprintf(fp, "%s ", name);
403 }
404
405 fprintf(fp, "%s", _SL_);
406
407 ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] ||
408 tb[NDTA_GC_INTERVAL]);
409 if (ret)
410 fprintf(fp, " ");
411
412 if (tb[NDTA_THRESH1]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700413 __u32 thresh1 = rta_getattr_u32(tb[NDTA_THRESH1]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700414
shemminger143969f2006-01-10 18:50:18 +0000415 fprintf(fp, "thresh1 %u ", thresh1);
416 }
417 if (tb[NDTA_THRESH2]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700418 __u32 thresh2 = rta_getattr_u32(tb[NDTA_THRESH2]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700419
shemminger143969f2006-01-10 18:50:18 +0000420 fprintf(fp, "thresh2 %u ", thresh2);
421 }
422 if (tb[NDTA_THRESH3]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700423 __u32 thresh3 = rta_getattr_u32(tb[NDTA_THRESH3]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700424
shemminger143969f2006-01-10 18:50:18 +0000425 fprintf(fp, "thresh3 %u ", thresh3);
426 }
427 if (tb[NDTA_GC_INTERVAL]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800428 unsigned long long gc_int = rta_getattr_u64(tb[NDTA_GC_INTERVAL]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700429
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800430 fprintf(fp, "gc_int %llu ", gc_int);
shemminger143969f2006-01-10 18:50:18 +0000431 }
432
433 if (ret)
434 fprintf(fp, "%s", _SL_);
435
436 if (tb[NDTA_CONFIG] && show_stats) {
437 struct ndt_config *ndtc = RTA_DATA(tb[NDTA_CONFIG]);
438
439 fprintf(fp, " ");
440 fprintf(fp, "config ");
441
442 fprintf(fp, "key_len %u ", ndtc->ndtc_key_len);
443 fprintf(fp, "entry_size %u ", ndtc->ndtc_entry_size);
444 fprintf(fp, "entries %u ", ndtc->ndtc_entries);
445
446 fprintf(fp, "%s", _SL_);
447 fprintf(fp, " ");
448
449 fprintf(fp, "last_flush %s ",
450 ntable_strtime_delta(ndtc->ndtc_last_flush));
451 fprintf(fp, "last_rand %s ",
452 ntable_strtime_delta(ndtc->ndtc_last_rand));
453
454 fprintf(fp, "%s", _SL_);
455 fprintf(fp, " ");
456
457 fprintf(fp, "hash_rnd %u ", ndtc->ndtc_hash_rnd);
458 fprintf(fp, "hash_mask %08x ", ndtc->ndtc_hash_mask);
459
460 fprintf(fp, "hash_chain_gc %u ", ndtc->ndtc_hash_chain_gc);
461 fprintf(fp, "proxy_qlen %u ", ndtc->ndtc_proxy_qlen);
462
463 fprintf(fp, "%s", _SL_);
464 }
465
466 if (tb[NDTA_PARMS]) {
467 if (tpb[NDTPA_IFINDEX]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700468 __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]);
shemminger143969f2006-01-10 18:50:18 +0000469
470 fprintf(fp, " ");
471 fprintf(fp, "dev %s ", ll_index_to_name(ifindex));
472 fprintf(fp, "%s", _SL_);
473 }
474
475 fprintf(fp, " ");
476
477 if (tpb[NDTPA_REFCNT]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700478 __u32 refcnt = rta_getattr_u32(tpb[NDTPA_REFCNT]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700479
shemminger143969f2006-01-10 18:50:18 +0000480 fprintf(fp, "refcnt %u ", refcnt);
481 }
482 if (tpb[NDTPA_REACHABLE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800483 unsigned long long reachable = rta_getattr_u64(tpb[NDTPA_REACHABLE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700484
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800485 fprintf(fp, "reachable %llu ", reachable);
shemminger143969f2006-01-10 18:50:18 +0000486 }
487 if (tpb[NDTPA_BASE_REACHABLE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800488 unsigned long long breachable = rta_getattr_u64(tpb[NDTPA_BASE_REACHABLE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700489
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800490 fprintf(fp, "base_reachable %llu ", breachable);
shemminger143969f2006-01-10 18:50:18 +0000491 }
492 if (tpb[NDTPA_RETRANS_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800493 unsigned long long retrans = rta_getattr_u64(tpb[NDTPA_RETRANS_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700494
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800495 fprintf(fp, "retrans %llu ", retrans);
shemminger143969f2006-01-10 18:50:18 +0000496 }
497
498 fprintf(fp, "%s", _SL_);
499
500 fprintf(fp, " ");
501
502 if (tpb[NDTPA_GC_STALETIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800503 unsigned long long gc_stale = rta_getattr_u64(tpb[NDTPA_GC_STALETIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700504
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800505 fprintf(fp, "gc_stale %llu ", gc_stale);
shemminger143969f2006-01-10 18:50:18 +0000506 }
507 if (tpb[NDTPA_DELAY_PROBE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800508 unsigned long long delay_probe = rta_getattr_u64(tpb[NDTPA_DELAY_PROBE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700509
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800510 fprintf(fp, "delay_probe %llu ", delay_probe);
shemminger143969f2006-01-10 18:50:18 +0000511 }
512 if (tpb[NDTPA_QUEUE_LEN]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700513 __u32 queue = rta_getattr_u32(tpb[NDTPA_QUEUE_LEN]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700514
shemminger143969f2006-01-10 18:50:18 +0000515 fprintf(fp, "queue %u ", queue);
516 }
517
518 fprintf(fp, "%s", _SL_);
519
520 fprintf(fp, " ");
521
522 if (tpb[NDTPA_APP_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700523 __u32 aprobe = rta_getattr_u32(tpb[NDTPA_APP_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700524
shemminger143969f2006-01-10 18:50:18 +0000525 fprintf(fp, "app_probes %u ", aprobe);
526 }
527 if (tpb[NDTPA_UCAST_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700528 __u32 uprobe = rta_getattr_u32(tpb[NDTPA_UCAST_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700529
shemminger143969f2006-01-10 18:50:18 +0000530 fprintf(fp, "ucast_probes %u ", uprobe);
531 }
532 if (tpb[NDTPA_MCAST_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700533 __u32 mprobe = rta_getattr_u32(tpb[NDTPA_MCAST_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700534
shemminger143969f2006-01-10 18:50:18 +0000535 fprintf(fp, "mcast_probes %u ", mprobe);
536 }
537
538 fprintf(fp, "%s", _SL_);
539
540 fprintf(fp, " ");
541
542 if (tpb[NDTPA_ANYCAST_DELAY]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800543 unsigned long long anycast_delay = rta_getattr_u64(tpb[NDTPA_ANYCAST_DELAY]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700544
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800545 fprintf(fp, "anycast_delay %llu ", anycast_delay);
shemminger143969f2006-01-10 18:50:18 +0000546 }
547 if (tpb[NDTPA_PROXY_DELAY]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800548 unsigned long long proxy_delay = rta_getattr_u64(tpb[NDTPA_PROXY_DELAY]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700549
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800550 fprintf(fp, "proxy_delay %llu ", proxy_delay);
shemminger143969f2006-01-10 18:50:18 +0000551 }
552 if (tpb[NDTPA_PROXY_QLEN]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700553 __u32 pqueue = rta_getattr_u32(tpb[NDTPA_PROXY_QLEN]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700554
shemminger143969f2006-01-10 18:50:18 +0000555 fprintf(fp, "proxy_queue %u ", pqueue);
556 }
557 if (tpb[NDTPA_LOCKTIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800558 unsigned long long locktime = rta_getattr_u64(tpb[NDTPA_LOCKTIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700559
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800560 fprintf(fp, "locktime %llu ", locktime);
shemminger143969f2006-01-10 18:50:18 +0000561 }
562
563 fprintf(fp, "%s", _SL_);
564 }
565
566 if (tb[NDTA_STATS] && show_stats) {
567 struct ndt_stats *ndts = RTA_DATA(tb[NDTA_STATS]);
568
569 fprintf(fp, " ");
570 fprintf(fp, "stats ");
571
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800572 fprintf(fp, "allocs %llu ",
573 (unsigned long long) ndts->ndts_allocs);
574 fprintf(fp, "destroys %llu ",
575 (unsigned long long) ndts->ndts_destroys);
576 fprintf(fp, "hash_grows %llu ",
577 (unsigned long long) ndts->ndts_hash_grows);
shemminger143969f2006-01-10 18:50:18 +0000578
579 fprintf(fp, "%s", _SL_);
580 fprintf(fp, " ");
581
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800582 fprintf(fp, "res_failed %llu ",
583 (unsigned long long) ndts->ndts_res_failed);
584 fprintf(fp, "lookups %llu ",
585 (unsigned long long) ndts->ndts_lookups);
586 fprintf(fp, "hits %llu ",
587 (unsigned long long) ndts->ndts_hits);
shemminger143969f2006-01-10 18:50:18 +0000588
589 fprintf(fp, "%s", _SL_);
590 fprintf(fp, " ");
591
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800592 fprintf(fp, "rcv_probes_mcast %llu ",
593 (unsigned long long) ndts->ndts_rcv_probes_mcast);
594 fprintf(fp, "rcv_probes_ucast %llu ",
595 (unsigned long long) ndts->ndts_rcv_probes_ucast);
shemminger143969f2006-01-10 18:50:18 +0000596
597 fprintf(fp, "%s", _SL_);
598 fprintf(fp, " ");
599
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800600 fprintf(fp, "periodic_gc_runs %llu ",
601 (unsigned long long) ndts->ndts_periodic_gc_runs);
602 fprintf(fp, "forced_gc_runs %llu ",
603 (unsigned long long) ndts->ndts_forced_gc_runs);
shemminger143969f2006-01-10 18:50:18 +0000604
605 fprintf(fp, "%s", _SL_);
606 }
607
608 fprintf(fp, "\n");
609
610 fflush(fp);
611 return 0;
612}
613
Phil Sutterf7b49a32015-11-06 18:54:08 +0100614static void ipntable_reset_filter(void)
shemminger143969f2006-01-10 18:50:18 +0000615{
616 memset(&filter, 0, sizeof(filter));
617}
618
619static int ipntable_show(int argc, char **argv)
620{
621 ipntable_reset_filter();
622
623 filter.family = preferred_family;
624
625 while (argc > 0) {
626 if (strcmp(*argv, "dev") == 0) {
627 NEXT_ARG();
628
629 if (strcmp("none", *argv) == 0)
630 filter.index = NONE_DEV;
631 else if ((filter.index = ll_name_to_index(*argv)) == 0)
632 invarg("\"DEV\" is invalid", *argv);
633 } else if (strcmp(*argv, "name") == 0) {
634 NEXT_ARG();
635
636 strncpy(filter.name, *argv, sizeof(filter.name));
637 } else
638 invarg("unknown", *argv);
639
640 argc--; argv++;
641 }
642
643 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETNEIGHTBL) < 0) {
644 perror("Cannot send dump request");
645 exit(1);
646 }
647
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800648 if (rtnl_dump_filter(&rth, print_ntable, stdout) < 0) {
shemminger143969f2006-01-10 18:50:18 +0000649 fprintf(stderr, "Dump terminated\n");
650 exit(1);
651 }
652
653 return 0;
654}
655
656int do_ipntable(int argc, char **argv)
657{
658 ll_init_map(&rth);
659
660 if (argc > 0) {
661 if (matches(*argv, "change") == 0 ||
662 matches(*argv, "chg") == 0)
663 return ipntable_modify(RTM_SETNEIGHTBL,
664 NLM_F_REPLACE,
665 argc-1, argv+1);
666 if (matches(*argv, "show") == 0 ||
667 matches(*argv, "lst") == 0 ||
668 matches(*argv, "list") == 0)
669 return ipntable_show(argc-1, argv+1);
670 if (matches(*argv, "help") == 0)
671 usage();
672 } else
673 return ipntable_show(0, NULL);
674
675 fprintf(stderr, "Command \"%s\" is unknown, try \"ip ntable help\".\n", *argv);
676 exit(-1);
677}