shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2006 USAGI/WIDE Project |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 3 | * |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 4 | * 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 Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 8 | * |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 9 | * 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 Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 13 | * |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
Stephen Hemminger | 4d98ab0 | 2013-12-06 15:05:07 -0800 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 16 | */ |
| 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> |
Strake | 5bd9dd4 | 2012-12-23 08:46:04 -0500 | [diff] [blame] | 29 | #include <sys/socket.h> |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 30 | #include <time.h> |
| 31 | |
| 32 | #include "utils.h" |
| 33 | #include "ip_common.h" |
| 34 | |
| 35 | static struct |
| 36 | { |
| 37 | int family; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 38 | int index; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 39 | #define NONE_DEV (-1) |
| 40 | char name[1024]; |
| 41 | } filter; |
| 42 | |
| 43 | static void usage(void) __attribute__((noreturn)); |
| 44 | |
| 45 | static 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 Sutter | f1fdcfe | 2016-03-02 19:19:53 +0100 | [diff] [blame] | 55 | " [ app_probes VAL ] [ ucast_probes VAL ] [ mcast_probes VAL ]\n" |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 56 | " [ anycast_delay MSEC ] [ proxy_delay MSEC ] [ proxy_queue LEN ]\n" |
| 57 | " [ locktime MSEC ]\n" |
| 58 | ); |
| 59 | |
| 60 | exit(-1); |
| 61 | } |
| 62 | |
| 63 | static int ipntable_modify(int cmd, int flags, int argc, char **argv) |
| 64 | { |
| 65 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 66 | struct nlmsghdr n; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 67 | struct ndtmsg ndtm; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 68 | char buf[1024]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 69 | } 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 | }; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 75 | char *namep = NULL; |
| 76 | char *threshsp = NULL; |
| 77 | char *gc_intp = NULL; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 78 | char parms_buf[1024] = {}; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 79 | struct rtattr *parms_rta = (struct rtattr *)parms_buf; |
| 80 | int parms_change = 0; |
| 81 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 82 | 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 Hemminger | d259f03 | 2013-08-04 15:00:56 -0700 | [diff] [blame] | 300 | fprintf(stderr, "Not enough information: changeable attributes required.\n"); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 301 | 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 Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 309 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 310 | exit(2); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static const char *ntable_strtime_delta(__u32 msec) |
| 316 | { |
| 317 | static char str[32]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 318 | struct timeval now = {}; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 319 | time_t t; |
| 320 | struct tm *tp; |
| 321 | |
| 322 | if (msec == 0) |
| 323 | goto error; |
| 324 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 325 | 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 Sutter | f7b49a3 | 2015-11-06 18:54:08 +0100 | [diff] [blame] | 343 | static int print_ntable(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 344 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 345 | FILE *fp = (FILE *)arg; |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 346 | 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 370 | const char *name = rta_getattr_str(tb[NDTA_NAME]); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 371 | |
| 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 380 | __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 381 | |
| 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 400 | const char *name = rta_getattr_str(tb[NDTA_NAME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 401 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 402 | 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 413 | __u32 thresh1 = rta_getattr_u32(tb[NDTA_THRESH1]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 414 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 415 | fprintf(fp, "thresh1 %u ", thresh1); |
| 416 | } |
| 417 | if (tb[NDTA_THRESH2]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 418 | __u32 thresh2 = rta_getattr_u32(tb[NDTA_THRESH2]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 419 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 420 | fprintf(fp, "thresh2 %u ", thresh2); |
| 421 | } |
| 422 | if (tb[NDTA_THRESH3]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 423 | __u32 thresh3 = rta_getattr_u32(tb[NDTA_THRESH3]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 424 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 425 | fprintf(fp, "thresh3 %u ", thresh3); |
| 426 | } |
| 427 | if (tb[NDTA_GC_INTERVAL]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 428 | unsigned long long gc_int = rta_getattr_u64(tb[NDTA_GC_INTERVAL]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 429 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 430 | fprintf(fp, "gc_int %llu ", gc_int); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 431 | } |
| 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 468 | __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 469 | |
| 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 Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 478 | __u32 refcnt = rta_getattr_u32(tpb[NDTPA_REFCNT]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 479 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 480 | fprintf(fp, "refcnt %u ", refcnt); |
| 481 | } |
| 482 | if (tpb[NDTPA_REACHABLE_TIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 483 | unsigned long long reachable = rta_getattr_u64(tpb[NDTPA_REACHABLE_TIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 484 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 485 | fprintf(fp, "reachable %llu ", reachable); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 486 | } |
| 487 | if (tpb[NDTPA_BASE_REACHABLE_TIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 488 | unsigned long long breachable = rta_getattr_u64(tpb[NDTPA_BASE_REACHABLE_TIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 489 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 490 | fprintf(fp, "base_reachable %llu ", breachable); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 491 | } |
| 492 | if (tpb[NDTPA_RETRANS_TIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 493 | unsigned long long retrans = rta_getattr_u64(tpb[NDTPA_RETRANS_TIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 494 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 495 | fprintf(fp, "retrans %llu ", retrans); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | fprintf(fp, "%s", _SL_); |
| 499 | |
| 500 | fprintf(fp, " "); |
| 501 | |
| 502 | if (tpb[NDTPA_GC_STALETIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 503 | unsigned long long gc_stale = rta_getattr_u64(tpb[NDTPA_GC_STALETIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 504 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 505 | fprintf(fp, "gc_stale %llu ", gc_stale); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 506 | } |
| 507 | if (tpb[NDTPA_DELAY_PROBE_TIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 508 | unsigned long long delay_probe = rta_getattr_u64(tpb[NDTPA_DELAY_PROBE_TIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 509 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 510 | fprintf(fp, "delay_probe %llu ", delay_probe); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 511 | } |
| 512 | if (tpb[NDTPA_QUEUE_LEN]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 513 | __u32 queue = rta_getattr_u32(tpb[NDTPA_QUEUE_LEN]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 514 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 515 | fprintf(fp, "queue %u ", queue); |
| 516 | } |
| 517 | |
| 518 | fprintf(fp, "%s", _SL_); |
| 519 | |
| 520 | fprintf(fp, " "); |
| 521 | |
| 522 | if (tpb[NDTPA_APP_PROBES]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 523 | __u32 aprobe = rta_getattr_u32(tpb[NDTPA_APP_PROBES]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 524 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 525 | fprintf(fp, "app_probes %u ", aprobe); |
| 526 | } |
| 527 | if (tpb[NDTPA_UCAST_PROBES]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 528 | __u32 uprobe = rta_getattr_u32(tpb[NDTPA_UCAST_PROBES]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 529 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 530 | fprintf(fp, "ucast_probes %u ", uprobe); |
| 531 | } |
| 532 | if (tpb[NDTPA_MCAST_PROBES]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 533 | __u32 mprobe = rta_getattr_u32(tpb[NDTPA_MCAST_PROBES]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 534 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 535 | 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 Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 543 | unsigned long long anycast_delay = rta_getattr_u64(tpb[NDTPA_ANYCAST_DELAY]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 544 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 545 | fprintf(fp, "anycast_delay %llu ", anycast_delay); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 546 | } |
| 547 | if (tpb[NDTPA_PROXY_DELAY]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 548 | unsigned long long proxy_delay = rta_getattr_u64(tpb[NDTPA_PROXY_DELAY]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 549 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 550 | fprintf(fp, "proxy_delay %llu ", proxy_delay); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 551 | } |
| 552 | if (tpb[NDTPA_PROXY_QLEN]) { |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 553 | __u32 pqueue = rta_getattr_u32(tpb[NDTPA_PROXY_QLEN]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 554 | |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 555 | fprintf(fp, "proxy_queue %u ", pqueue); |
| 556 | } |
| 557 | if (tpb[NDTPA_LOCKTIME]) { |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 558 | unsigned long long locktime = rta_getattr_u64(tpb[NDTPA_LOCKTIME]); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 559 | |
Stephen Hemminger | a55a8fd | 2013-02-28 08:51:46 -0800 | [diff] [blame] | 560 | fprintf(fp, "locktime %llu ", locktime); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 561 | } |
| 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 Hemminger | ae70d96 | 2013-03-04 13:59:39 -0800 | [diff] [blame] | 572 | 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); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 578 | |
| 579 | fprintf(fp, "%s", _SL_); |
| 580 | fprintf(fp, " "); |
| 581 | |
Stephen Hemminger | ae70d96 | 2013-03-04 13:59:39 -0800 | [diff] [blame] | 582 | 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); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 588 | |
| 589 | fprintf(fp, "%s", _SL_); |
| 590 | fprintf(fp, " "); |
| 591 | |
Stephen Hemminger | ae70d96 | 2013-03-04 13:59:39 -0800 | [diff] [blame] | 592 | 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); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 596 | |
| 597 | fprintf(fp, "%s", _SL_); |
| 598 | fprintf(fp, " "); |
| 599 | |
Stephen Hemminger | ae70d96 | 2013-03-04 13:59:39 -0800 | [diff] [blame] | 600 | 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); |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 604 | |
| 605 | fprintf(fp, "%s", _SL_); |
| 606 | } |
| 607 | |
| 608 | fprintf(fp, "\n"); |
| 609 | |
| 610 | fflush(fp); |
| 611 | return 0; |
| 612 | } |
| 613 | |
Phil Sutter | f7b49a3 | 2015-11-06 18:54:08 +0100 | [diff] [blame] | 614 | static void ipntable_reset_filter(void) |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 615 | { |
| 616 | memset(&filter, 0, sizeof(filter)); |
| 617 | } |
| 618 | |
| 619 | static 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 Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 648 | if (rtnl_dump_filter(&rth, print_ntable, stdout) < 0) { |
shemminger | 143969f | 2006-01-10 18:50:18 +0000 | [diff] [blame] | 649 | fprintf(stderr, "Dump terminated\n"); |
| 650 | exit(1); |
| 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | int 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 | } |