blob: 8e78773d0e4b072ab0ebe6ec38f193ffed4d37a8 [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];
shemminger143969f2006-01-10 18:50:18 +000069 } req;
70 char *namep = NULL;
71 char *threshsp = NULL;
72 char *gc_intp = NULL;
73 char parms_buf[1024];
74 struct rtattr *parms_rta = (struct rtattr *)parms_buf;
75 int parms_change = 0;
76
77 memset(&req, 0, sizeof(req));
78
79 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg));
80 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
81 req.n.nlmsg_type = cmd;
82
83 req.ndtm.ndtm_family = preferred_family;
84 req.ndtm.ndtm_pad1 = 0;
85 req.ndtm.ndtm_pad2 = 0;
86
87 memset(&parms_buf, 0, sizeof(parms_buf));
88
89 parms_rta->rta_type = NDTA_PARMS;
90 parms_rta->rta_len = RTA_LENGTH(0);
91
92 while (argc > 0) {
93 if (strcmp(*argv, "name") == 0) {
94 int len;
95
96 NEXT_ARG();
97 if (namep)
98 duparg("NAME", *argv);
99
100 namep = *argv;
101 len = strlen(namep) + 1;
102 addattr_l(&req.n, sizeof(req), NDTA_NAME, namep, len);
103 } else if (strcmp(*argv, "thresh1") == 0) {
104 __u32 thresh1;
105
106 NEXT_ARG();
107 threshsp = *argv;
108
109 if (get_u32(&thresh1, *argv, 0))
110 invarg("\"thresh1\" value is invalid", *argv);
111
112 addattr32(&req.n, sizeof(req), NDTA_THRESH1, thresh1);
113 } else if (strcmp(*argv, "thresh2") == 0) {
114 __u32 thresh2;
115
116 NEXT_ARG();
117 threshsp = *argv;
118
119 if (get_u32(&thresh2, *argv, 0))
120 invarg("\"thresh2\" value is invalid", *argv);
121
122 addattr32(&req.n, sizeof(req), NDTA_THRESH2, thresh2);
123 } else if (strcmp(*argv, "thresh3") == 0) {
124 __u32 thresh3;
125
126 NEXT_ARG();
127 threshsp = *argv;
128
129 if (get_u32(&thresh3, *argv, 0))
130 invarg("\"thresh3\" value is invalid", *argv);
131
132 addattr32(&req.n, sizeof(req), NDTA_THRESH3, thresh3);
133 } else if (strcmp(*argv, "gc_int") == 0) {
134 __u64 gc_int;
135
136 NEXT_ARG();
137 gc_intp = *argv;
138
139 if (get_u64(&gc_int, *argv, 0))
140 invarg("\"gc_int\" value is invalid", *argv);
141
142 addattr_l(&req.n, sizeof(req), NDTA_GC_INTERVAL,
143 &gc_int, sizeof(gc_int));
144 } else if (strcmp(*argv, "dev") == 0) {
145 __u32 ifindex;
146
147 NEXT_ARG();
148 ifindex = ll_name_to_index(*argv);
149 if (ifindex == 0) {
150 fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
151 return -1;
152 }
153
154 rta_addattr32(parms_rta, sizeof(parms_buf),
155 NDTPA_IFINDEX, ifindex);
156 } else if (strcmp(*argv, "base_reachable") == 0) {
157 __u64 breachable;
158
159 NEXT_ARG();
160
161 if (get_u64(&breachable, *argv, 0))
162 invarg("\"base_reachable\" value is invalid", *argv);
163
164 rta_addattr_l(parms_rta, sizeof(parms_buf),
165 NDTPA_BASE_REACHABLE_TIME,
166 &breachable, sizeof(breachable));
167 parms_change = 1;
168 } else if (strcmp(*argv, "retrans") == 0) {
169 __u64 retrans;
170
171 NEXT_ARG();
172
173 if (get_u64(&retrans, *argv, 0))
174 invarg("\"retrans\" value is invalid", *argv);
175
176 rta_addattr_l(parms_rta, sizeof(parms_buf),
177 NDTPA_RETRANS_TIME,
178 &retrans, sizeof(retrans));
179 parms_change = 1;
180 } else if (strcmp(*argv, "gc_stale") == 0) {
181 __u64 gc_stale;
182
183 NEXT_ARG();
184
185 if (get_u64(&gc_stale, *argv, 0))
186 invarg("\"gc_stale\" value is invalid", *argv);
187
188 rta_addattr_l(parms_rta, sizeof(parms_buf),
189 NDTPA_GC_STALETIME,
190 &gc_stale, sizeof(gc_stale));
191 parms_change = 1;
192 } else if (strcmp(*argv, "delay_probe") == 0) {
193 __u64 delay_probe;
194
195 NEXT_ARG();
196
197 if (get_u64(&delay_probe, *argv, 0))
198 invarg("\"delay_probe\" value is invalid", *argv);
199
200 rta_addattr_l(parms_rta, sizeof(parms_buf),
201 NDTPA_DELAY_PROBE_TIME,
202 &delay_probe, sizeof(delay_probe));
203 parms_change = 1;
204 } else if (strcmp(*argv, "queue") == 0) {
205 __u32 queue;
206
207 NEXT_ARG();
208
209 if (get_u32(&queue, *argv, 0))
210 invarg("\"queue\" value is invalid", *argv);
211
212 if (!parms_rta)
213 parms_rta = (struct rtattr *)&parms_buf;
214 rta_addattr32(parms_rta, sizeof(parms_buf),
215 NDTPA_QUEUE_LEN, queue);
216 parms_change = 1;
217 } else if (strcmp(*argv, "app_probes") == 0) {
218 __u32 aprobe;
219
220 NEXT_ARG();
221
222 if (get_u32(&aprobe, *argv, 0))
223 invarg("\"app_probes\" value is invalid", *argv);
224
225 rta_addattr32(parms_rta, sizeof(parms_buf),
226 NDTPA_APP_PROBES, aprobe);
227 parms_change = 1;
228 } else if (strcmp(*argv, "ucast_probes") == 0) {
229 __u32 uprobe;
230
231 NEXT_ARG();
232
233 if (get_u32(&uprobe, *argv, 0))
234 invarg("\"ucast_probes\" value is invalid", *argv);
235
236 rta_addattr32(parms_rta, sizeof(parms_buf),
237 NDTPA_UCAST_PROBES, uprobe);
238 parms_change = 1;
239 } else if (strcmp(*argv, "mcast_probes") == 0) {
240 __u32 mprobe;
241
242 NEXT_ARG();
243
244 if (get_u32(&mprobe, *argv, 0))
245 invarg("\"mcast_probes\" value is invalid", *argv);
246
247 rta_addattr32(parms_rta, sizeof(parms_buf),
248 NDTPA_MCAST_PROBES, mprobe);
249 parms_change = 1;
250 } else if (strcmp(*argv, "anycast_delay") == 0) {
251 __u64 anycast_delay;
252
253 NEXT_ARG();
254
255 if (get_u64(&anycast_delay, *argv, 0))
256 invarg("\"anycast_delay\" value is invalid", *argv);
257
258 rta_addattr_l(parms_rta, sizeof(parms_buf),
259 NDTPA_ANYCAST_DELAY,
260 &anycast_delay, sizeof(anycast_delay));
261 parms_change = 1;
262 } else if (strcmp(*argv, "proxy_delay") == 0) {
263 __u64 proxy_delay;
264
265 NEXT_ARG();
266
267 if (get_u64(&proxy_delay, *argv, 0))
268 invarg("\"proxy_delay\" value is invalid", *argv);
269
270 rta_addattr_l(parms_rta, sizeof(parms_buf),
271 NDTPA_PROXY_DELAY,
272 &proxy_delay, sizeof(proxy_delay));
273 parms_change = 1;
274 } else if (strcmp(*argv, "proxy_queue") == 0) {
275 __u32 pqueue;
276
277 NEXT_ARG();
278
279 if (get_u32(&pqueue, *argv, 0))
280 invarg("\"proxy_queue\" value is invalid", *argv);
281
282 rta_addattr32(parms_rta, sizeof(parms_buf),
283 NDTPA_PROXY_QLEN, pqueue);
284 parms_change = 1;
285 } else if (strcmp(*argv, "locktime") == 0) {
286 __u64 locktime;
287
288 NEXT_ARG();
289
290 if (get_u64(&locktime, *argv, 0))
291 invarg("\"locktime\" value is invalid", *argv);
292
293 rta_addattr_l(parms_rta, sizeof(parms_buf),
294 NDTPA_LOCKTIME,
295 &locktime, sizeof(locktime));
296 parms_change = 1;
297 } else {
298 invarg("unknown", *argv);
299 }
300
301 argc--; argv++;
302 }
303
304 if (!namep)
305 missarg("NAME");
306 if (!threshsp && !gc_intp && !parms_change) {
Stephen Hemmingerd259f032013-08-04 15:00:56 -0700307 fprintf(stderr, "Not enough information: changeable attributes required.\n");
shemminger143969f2006-01-10 18:50:18 +0000308 exit(-1);
309 }
310
311 if (parms_rta->rta_len > RTA_LENGTH(0)) {
312 addattr_l(&req.n, sizeof(req), NDTA_PARMS, RTA_DATA(parms_rta),
313 RTA_PAYLOAD(parms_rta));
314 }
315
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700316 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
shemminger143969f2006-01-10 18:50:18 +0000317 exit(2);
318
319 return 0;
320}
321
322static const char *ntable_strtime_delta(__u32 msec)
323{
324 static char str[32];
325 struct timeval now;
326 time_t t;
327 struct tm *tp;
328
329 if (msec == 0)
330 goto error;
331
332 memset(&now, 0, sizeof(now));
333
334 if (gettimeofday(&now, NULL) < 0) {
335 perror("gettimeofday");
336 goto error;
337 }
338
339 t = now.tv_sec - (msec / 1000);
340 tp = localtime(&t);
341 if (!tp)
342 goto error;
343
344 strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
345
346 return str;
347 error:
348 strcpy(str, "(error)");
349 return str;
350}
351
Phil Sutterf7b49a32015-11-06 18:54:08 +0100352static int print_ntable(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
shemminger143969f2006-01-10 18:50:18 +0000353{
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700354 FILE *fp = (FILE *)arg;
shemminger143969f2006-01-10 18:50:18 +0000355 struct ndtmsg *ndtm = NLMSG_DATA(n);
356 int len = n->nlmsg_len;
357 struct rtattr *tb[NDTA_MAX+1];
358 struct rtattr *tpb[NDTPA_MAX+1];
359 int ret;
360
361 if (n->nlmsg_type != RTM_NEWNEIGHTBL) {
362 fprintf(stderr, "Not NEIGHTBL: %08x %08x %08x\n",
363 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
364 return 0;
365 }
366 len -= NLMSG_LENGTH(sizeof(*ndtm));
367 if (len < 0) {
368 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
369 return -1;
370 }
371
372 if (preferred_family && preferred_family != ndtm->ndtm_family)
373 return 0;
374
375 parse_rtattr(tb, NDTA_MAX, NDTA_RTA(ndtm),
376 n->nlmsg_len - NLMSG_LENGTH(sizeof(*ndtm)));
377
378 if (tb[NDTA_NAME]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700379 const char *name = rta_getattr_str(tb[NDTA_NAME]);
shemminger143969f2006-01-10 18:50:18 +0000380
381 if (strlen(filter.name) > 0 && strcmp(filter.name, name))
382 return 0;
383 }
384 if (tb[NDTA_PARMS]) {
385 parse_rtattr(tpb, NDTPA_MAX, RTA_DATA(tb[NDTA_PARMS]),
386 RTA_PAYLOAD(tb[NDTA_PARMS]));
387
388 if (tpb[NDTPA_IFINDEX]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700389 __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]);
shemminger143969f2006-01-10 18:50:18 +0000390
391 if (filter.index && filter.index != ifindex)
392 return 0;
393 } else {
394 if (filter.index && filter.index != NONE_DEV)
395 return 0;
396 }
397 }
398
399 if (ndtm->ndtm_family == AF_INET)
400 fprintf(fp, "inet ");
401 else if (ndtm->ndtm_family == AF_INET6)
402 fprintf(fp, "inet6 ");
403 else if (ndtm->ndtm_family == AF_DECnet)
404 fprintf(fp, "dnet ");
405 else
406 fprintf(fp, "(%d) ", ndtm->ndtm_family);
407
408 if (tb[NDTA_NAME]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700409 const char *name = rta_getattr_str(tb[NDTA_NAME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700410
shemminger143969f2006-01-10 18:50:18 +0000411 fprintf(fp, "%s ", name);
412 }
413
414 fprintf(fp, "%s", _SL_);
415
416 ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] ||
417 tb[NDTA_GC_INTERVAL]);
418 if (ret)
419 fprintf(fp, " ");
420
421 if (tb[NDTA_THRESH1]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700422 __u32 thresh1 = rta_getattr_u32(tb[NDTA_THRESH1]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700423
shemminger143969f2006-01-10 18:50:18 +0000424 fprintf(fp, "thresh1 %u ", thresh1);
425 }
426 if (tb[NDTA_THRESH2]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700427 __u32 thresh2 = rta_getattr_u32(tb[NDTA_THRESH2]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700428
shemminger143969f2006-01-10 18:50:18 +0000429 fprintf(fp, "thresh2 %u ", thresh2);
430 }
431 if (tb[NDTA_THRESH3]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700432 __u32 thresh3 = rta_getattr_u32(tb[NDTA_THRESH3]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700433
shemminger143969f2006-01-10 18:50:18 +0000434 fprintf(fp, "thresh3 %u ", thresh3);
435 }
436 if (tb[NDTA_GC_INTERVAL]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800437 unsigned long long gc_int = rta_getattr_u64(tb[NDTA_GC_INTERVAL]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700438
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800439 fprintf(fp, "gc_int %llu ", gc_int);
shemminger143969f2006-01-10 18:50:18 +0000440 }
441
442 if (ret)
443 fprintf(fp, "%s", _SL_);
444
445 if (tb[NDTA_CONFIG] && show_stats) {
446 struct ndt_config *ndtc = RTA_DATA(tb[NDTA_CONFIG]);
447
448 fprintf(fp, " ");
449 fprintf(fp, "config ");
450
451 fprintf(fp, "key_len %u ", ndtc->ndtc_key_len);
452 fprintf(fp, "entry_size %u ", ndtc->ndtc_entry_size);
453 fprintf(fp, "entries %u ", ndtc->ndtc_entries);
454
455 fprintf(fp, "%s", _SL_);
456 fprintf(fp, " ");
457
458 fprintf(fp, "last_flush %s ",
459 ntable_strtime_delta(ndtc->ndtc_last_flush));
460 fprintf(fp, "last_rand %s ",
461 ntable_strtime_delta(ndtc->ndtc_last_rand));
462
463 fprintf(fp, "%s", _SL_);
464 fprintf(fp, " ");
465
466 fprintf(fp, "hash_rnd %u ", ndtc->ndtc_hash_rnd);
467 fprintf(fp, "hash_mask %08x ", ndtc->ndtc_hash_mask);
468
469 fprintf(fp, "hash_chain_gc %u ", ndtc->ndtc_hash_chain_gc);
470 fprintf(fp, "proxy_qlen %u ", ndtc->ndtc_proxy_qlen);
471
472 fprintf(fp, "%s", _SL_);
473 }
474
475 if (tb[NDTA_PARMS]) {
476 if (tpb[NDTPA_IFINDEX]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700477 __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]);
shemminger143969f2006-01-10 18:50:18 +0000478
479 fprintf(fp, " ");
480 fprintf(fp, "dev %s ", ll_index_to_name(ifindex));
481 fprintf(fp, "%s", _SL_);
482 }
483
484 fprintf(fp, " ");
485
486 if (tpb[NDTPA_REFCNT]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700487 __u32 refcnt = rta_getattr_u32(tpb[NDTPA_REFCNT]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700488
shemminger143969f2006-01-10 18:50:18 +0000489 fprintf(fp, "refcnt %u ", refcnt);
490 }
491 if (tpb[NDTPA_REACHABLE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800492 unsigned long long reachable = rta_getattr_u64(tpb[NDTPA_REACHABLE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700493
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800494 fprintf(fp, "reachable %llu ", reachable);
shemminger143969f2006-01-10 18:50:18 +0000495 }
496 if (tpb[NDTPA_BASE_REACHABLE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800497 unsigned long long breachable = rta_getattr_u64(tpb[NDTPA_BASE_REACHABLE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700498
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800499 fprintf(fp, "base_reachable %llu ", breachable);
shemminger143969f2006-01-10 18:50:18 +0000500 }
501 if (tpb[NDTPA_RETRANS_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800502 unsigned long long retrans = rta_getattr_u64(tpb[NDTPA_RETRANS_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700503
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800504 fprintf(fp, "retrans %llu ", retrans);
shemminger143969f2006-01-10 18:50:18 +0000505 }
506
507 fprintf(fp, "%s", _SL_);
508
509 fprintf(fp, " ");
510
511 if (tpb[NDTPA_GC_STALETIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800512 unsigned long long gc_stale = rta_getattr_u64(tpb[NDTPA_GC_STALETIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700513
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800514 fprintf(fp, "gc_stale %llu ", gc_stale);
shemminger143969f2006-01-10 18:50:18 +0000515 }
516 if (tpb[NDTPA_DELAY_PROBE_TIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800517 unsigned long long delay_probe = rta_getattr_u64(tpb[NDTPA_DELAY_PROBE_TIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700518
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800519 fprintf(fp, "delay_probe %llu ", delay_probe);
shemminger143969f2006-01-10 18:50:18 +0000520 }
521 if (tpb[NDTPA_QUEUE_LEN]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700522 __u32 queue = rta_getattr_u32(tpb[NDTPA_QUEUE_LEN]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700523
shemminger143969f2006-01-10 18:50:18 +0000524 fprintf(fp, "queue %u ", queue);
525 }
526
527 fprintf(fp, "%s", _SL_);
528
529 fprintf(fp, " ");
530
531 if (tpb[NDTPA_APP_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700532 __u32 aprobe = rta_getattr_u32(tpb[NDTPA_APP_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700533
shemminger143969f2006-01-10 18:50:18 +0000534 fprintf(fp, "app_probes %u ", aprobe);
535 }
536 if (tpb[NDTPA_UCAST_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700537 __u32 uprobe = rta_getattr_u32(tpb[NDTPA_UCAST_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700538
shemminger143969f2006-01-10 18:50:18 +0000539 fprintf(fp, "ucast_probes %u ", uprobe);
540 }
541 if (tpb[NDTPA_MCAST_PROBES]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700542 __u32 mprobe = rta_getattr_u32(tpb[NDTPA_MCAST_PROBES]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700543
shemminger143969f2006-01-10 18:50:18 +0000544 fprintf(fp, "mcast_probes %u ", mprobe);
545 }
546
547 fprintf(fp, "%s", _SL_);
548
549 fprintf(fp, " ");
550
551 if (tpb[NDTPA_ANYCAST_DELAY]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800552 unsigned long long anycast_delay = rta_getattr_u64(tpb[NDTPA_ANYCAST_DELAY]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700553
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800554 fprintf(fp, "anycast_delay %llu ", anycast_delay);
shemminger143969f2006-01-10 18:50:18 +0000555 }
556 if (tpb[NDTPA_PROXY_DELAY]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800557 unsigned long long proxy_delay = rta_getattr_u64(tpb[NDTPA_PROXY_DELAY]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700558
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800559 fprintf(fp, "proxy_delay %llu ", proxy_delay);
shemminger143969f2006-01-10 18:50:18 +0000560 }
561 if (tpb[NDTPA_PROXY_QLEN]) {
Stephen Hemmingerff247462012-04-10 08:47:55 -0700562 __u32 pqueue = rta_getattr_u32(tpb[NDTPA_PROXY_QLEN]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700563
shemminger143969f2006-01-10 18:50:18 +0000564 fprintf(fp, "proxy_queue %u ", pqueue);
565 }
566 if (tpb[NDTPA_LOCKTIME]) {
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800567 unsigned long long locktime = rta_getattr_u64(tpb[NDTPA_LOCKTIME]);
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700568
Stephen Hemmingera55a8fd2013-02-28 08:51:46 -0800569 fprintf(fp, "locktime %llu ", locktime);
shemminger143969f2006-01-10 18:50:18 +0000570 }
571
572 fprintf(fp, "%s", _SL_);
573 }
574
575 if (tb[NDTA_STATS] && show_stats) {
576 struct ndt_stats *ndts = RTA_DATA(tb[NDTA_STATS]);
577
578 fprintf(fp, " ");
579 fprintf(fp, "stats ");
580
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800581 fprintf(fp, "allocs %llu ",
582 (unsigned long long) ndts->ndts_allocs);
583 fprintf(fp, "destroys %llu ",
584 (unsigned long long) ndts->ndts_destroys);
585 fprintf(fp, "hash_grows %llu ",
586 (unsigned long long) ndts->ndts_hash_grows);
shemminger143969f2006-01-10 18:50:18 +0000587
588 fprintf(fp, "%s", _SL_);
589 fprintf(fp, " ");
590
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800591 fprintf(fp, "res_failed %llu ",
592 (unsigned long long) ndts->ndts_res_failed);
593 fprintf(fp, "lookups %llu ",
594 (unsigned long long) ndts->ndts_lookups);
595 fprintf(fp, "hits %llu ",
596 (unsigned long long) ndts->ndts_hits);
shemminger143969f2006-01-10 18:50:18 +0000597
598 fprintf(fp, "%s", _SL_);
599 fprintf(fp, " ");
600
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800601 fprintf(fp, "rcv_probes_mcast %llu ",
602 (unsigned long long) ndts->ndts_rcv_probes_mcast);
603 fprintf(fp, "rcv_probes_ucast %llu ",
604 (unsigned long long) ndts->ndts_rcv_probes_ucast);
shemminger143969f2006-01-10 18:50:18 +0000605
606 fprintf(fp, "%s", _SL_);
607 fprintf(fp, " ");
608
Stephen Hemmingerae70d962013-03-04 13:59:39 -0800609 fprintf(fp, "periodic_gc_runs %llu ",
610 (unsigned long long) ndts->ndts_periodic_gc_runs);
611 fprintf(fp, "forced_gc_runs %llu ",
612 (unsigned long long) ndts->ndts_forced_gc_runs);
shemminger143969f2006-01-10 18:50:18 +0000613
614 fprintf(fp, "%s", _SL_);
615 }
616
617 fprintf(fp, "\n");
618
619 fflush(fp);
620 return 0;
621}
622
Phil Sutterf7b49a32015-11-06 18:54:08 +0100623static void ipntable_reset_filter(void)
shemminger143969f2006-01-10 18:50:18 +0000624{
625 memset(&filter, 0, sizeof(filter));
626}
627
628static int ipntable_show(int argc, char **argv)
629{
630 ipntable_reset_filter();
631
632 filter.family = preferred_family;
633
634 while (argc > 0) {
635 if (strcmp(*argv, "dev") == 0) {
636 NEXT_ARG();
637
638 if (strcmp("none", *argv) == 0)
639 filter.index = NONE_DEV;
640 else if ((filter.index = ll_name_to_index(*argv)) == 0)
641 invarg("\"DEV\" is invalid", *argv);
642 } else if (strcmp(*argv, "name") == 0) {
643 NEXT_ARG();
644
645 strncpy(filter.name, *argv, sizeof(filter.name));
646 } else
647 invarg("unknown", *argv);
648
649 argc--; argv++;
650 }
651
652 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETNEIGHTBL) < 0) {
653 perror("Cannot send dump request");
654 exit(1);
655 }
656
Stephen Hemmingercd70f3f2011-12-28 10:37:12 -0800657 if (rtnl_dump_filter(&rth, print_ntable, stdout) < 0) {
shemminger143969f2006-01-10 18:50:18 +0000658 fprintf(stderr, "Dump terminated\n");
659 exit(1);
660 }
661
662 return 0;
663}
664
665int do_ipntable(int argc, char **argv)
666{
667 ll_init_map(&rth);
668
669 if (argc > 0) {
670 if (matches(*argv, "change") == 0 ||
671 matches(*argv, "chg") == 0)
672 return ipntable_modify(RTM_SETNEIGHTBL,
673 NLM_F_REPLACE,
674 argc-1, argv+1);
675 if (matches(*argv, "show") == 0 ||
676 matches(*argv, "lst") == 0 ||
677 matches(*argv, "list") == 0)
678 return ipntable_show(argc-1, argv+1);
679 if (matches(*argv, "help") == 0)
680 usage();
681 } else
682 return ipntable_show(0, NULL);
683
684 fprintf(stderr, "Command \"%s\" is unknown, try \"ip ntable help\".\n", *argv);
685 exit(-1);
686}