libnl 3.9.0
neightbl.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
4 */
5
6/**
7 * @ingroup rtnl
8 * @defgroup neightbl Neighbour Tables
9 * @brief
10 * @{
11 */
12
13#include "nl-default.h"
14
15#include <netlink/netlink.h>
16#include <netlink/utils.h>
17#include <netlink/route/rtnl.h>
18#include <netlink/route/neightbl.h>
19#include <netlink/route/link.h>
20
21#include "nl-route.h"
22#include "nl-priv-dynamic-core/nl-core.h"
23#include "nl-priv-dynamic-core/cache-api.h"
24
26 /**
27 * Interface index of the device this parameter set is assigned
28 * to or 0 for the default set.
29 */
30 uint32_t ntp_ifindex;
31
32 /**
33 * Number of references to this parameter set.
34 */
35 uint32_t ntp_refcnt;
36
37 /**
38 * Queue length for pending arp requests, i.e. the number of
39 * packets which are accepted from other layers while the
40 * neighbour address is still being resolved
41 */
42 uint32_t ntp_queue_len;
43
44 /**
45 * Number of requests to send to the user level ARP daemon.
46 * Specify 0 to disable.
47 */
49
50 /**
51 * Maximum number of retries for unicast solicitation.
52 */
54
55 /**
56 * Maximum number of retries for multicast solicitation.
57 */
59
60 /**
61 * Base value in milliseconds to ompute reachable time, see RFC2461.
62 */
64
65 /**
66 * Actual reachable time (read-only)
67 */
68 uint64_t ntp_reachable_time; /* secs */
69
70 /**
71 * The time in milliseconds between retransmitted Neighbor
72 * Solicitation messages.
73 */
75
76 /**
77 * Interval in milliseconds to check for stale neighbour
78 * entries.
79 */
80 uint64_t ntp_gc_stale_time; /* secs */
81
82 /**
83 * Delay in milliseconds for the first time probe if
84 * the neighbour is reachable.
85 */
86 uint64_t ntp_probe_delay; /* secs */
87
88 /**
89 * Maximum delay in milliseconds of an answer to a neighbour
90 * solicitation message.
91 */
93
94 /**
95 * Minimum age in milliseconds before a neighbour entry
96 * may be replaced.
97 */
98 uint64_t ntp_locktime;
99
100 /**
101 * Delay in milliseconds before answering to an ARP request
102 * for which a proxy ARP entry exists.
103 */
105
106 /**
107 * Queue length for the delayed proxy arp requests.
108 */
110
111 /**
112 * Mask of available parameter attributes
113 */
114 uint32_t ntp_mask;
115};
116
117#define NTBLNAMSIZ 32
118
119/**
120 * Neighbour table
121 * @ingroup neightbl
122 */
124 NLHDR_COMMON
125
126 char nt_name[NTBLNAMSIZ];
127 uint32_t nt_family;
128 uint32_t nt_gc_thresh1;
129 uint32_t nt_gc_thresh2;
130 uint32_t nt_gc_thresh3;
131 uint64_t nt_gc_interval;
132 struct ndt_config nt_config;
133 struct rtnl_neightbl_parms nt_parms;
134 struct ndt_stats nt_stats;
135};
136
137/** @cond SKIP */
138#define NEIGHTBL_ATTR_FAMILY 0x001
139#define NEIGHTBL_ATTR_STATS 0x002
140#define NEIGHTBL_ATTR_NAME 0x004
141#define NEIGHTBL_ATTR_THRESH1 0x008
142#define NEIGHTBL_ATTR_THRESH2 0x010
143#define NEIGHTBL_ATTR_THRESH3 0x020
144#define NEIGHTBL_ATTR_CONFIG 0x040
145#define NEIGHTBL_ATTR_PARMS 0x080
146#define NEIGHTBL_ATTR_GC_INTERVAL 0x100
147
148#define NEIGHTBLPARM_ATTR_IFINDEX 0x0001
149#define NEIGHTBLPARM_ATTR_REFCNT 0x0002
150#define NEIGHTBLPARM_ATTR_QUEUE_LEN 0x0004
151#define NEIGHTBLPARM_ATTR_APP_PROBES 0x0008
152#define NEIGHTBLPARM_ATTR_UCAST_PROBES 0x0010
153#define NEIGHTBLPARM_ATTR_MCAST_PROBES 0x0020
154#define NEIGHTBLPARM_ATTR_PROXY_QLEN 0x0040
155#define NEIGHTBLPARM_ATTR_REACHABLE_TIME 0x0080
156#define NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME 0x0100
157#define NEIGHTBLPARM_ATTR_RETRANS_TIME 0x0200
158#define NEIGHTBLPARM_ATTR_GC_STALETIME 0x0400
159#define NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME 0x0800
160#define NEIGHTBLPARM_ATTR_ANYCAST_DELAY 0x1000
161#define NEIGHTBLPARM_ATTR_PROXY_DELAY 0x2000
162#define NEIGHTBLPARM_ATTR_LOCKTIME 0x4000
163
164static struct nl_cache_ops rtnl_neightbl_ops;
165static struct nl_object_ops neightbl_obj_ops;
166/** @endcond */
167
168static uint64_t neightbl_compare(struct nl_object *_a, struct nl_object *_b,
169 uint64_t attrs, int flags)
170{
171 struct rtnl_neightbl *a = (struct rtnl_neightbl *)_a;
172 struct rtnl_neightbl *b = (struct rtnl_neightbl *)_b;
173 uint64_t diff = 0;
174
175#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
176 diff |= _DIFF(NEIGHTBL_ATTR_FAMILY, a->nt_family != b->nt_family);
177 diff |= _DIFF(NEIGHTBL_ATTR_NAME, strcmp(a->nt_name, b->nt_name));
178 diff |= _DIFF(NEIGHTBL_ATTR_THRESH1,
179 a->nt_gc_thresh1 != b->nt_gc_thresh1);
180 diff |= _DIFF(NEIGHTBL_ATTR_THRESH2,
181 a->nt_gc_thresh2 != b->nt_gc_thresh2);
182 diff |= _DIFF(NEIGHTBL_ATTR_THRESH3,
183 a->nt_gc_thresh3 != b->nt_gc_thresh3);
184 diff |= _DIFF(NEIGHTBL_ATTR_GC_INTERVAL,
185 a->nt_gc_interval != b->nt_gc_interval);
186#undef _DIFF
187
188 if (!(a->ce_mask & NEIGHTBL_ATTR_PARMS) &&
189 !(b->ce_mask & NEIGHTBL_ATTR_PARMS))
190 return diff;
191
192 /* XXX: FIXME: Compare parameter table */
193
194#if 0
195#define REQ(F) (fp->ntp_mask & NEIGHTBLPARM_ATTR_##F)
196#define AVAIL(F) (op->ntp_mask & NEIGHTBLPARM_ATTR_##F)
197#define _C(F, N) (REQ(F) && (!AVAIL(F) || (op->N != fp->N)))
198 if (_C(IFINDEX, ntp_ifindex) ||
199 _C(QUEUE_LEN, ntp_queue_len) ||
200 _C(APP_PROBES, ntp_app_probes) ||
201 _C(UCAST_PROBES, ntp_ucast_probes) ||
202 _C(MCAST_PROBES, ntp_mcast_probes) ||
203 _C(PROXY_QLEN, ntp_proxy_qlen) ||
204 _C(LOCKTIME, ntp_locktime) ||
205 _C(RETRANS_TIME, ntp_retrans_time) ||
206 _C(BASE_REACHABLE_TIME, ntp_base_reachable_time) ||
207 _C(GC_STALETIME, ntp_gc_stale_time) ||
208 _C(DELAY_PROBE_TIME, ntp_probe_delay) ||
209 _C(ANYCAST_DELAY, ntp_anycast_delay) ||
210 _C(PROXY_DELAY, ntp_proxy_delay))
211 return 0;
212#undef REQ
213#undef AVAIL
214#undef _C
215#endif
216
217 return diff;
218}
219
220static struct nla_policy neightbl_policy[NDTA_MAX + 1] = {
221 [NDTA_NAME] = { .type = NLA_STRING, .maxlen = NTBLNAMSIZ },
222 [NDTA_THRESH1] = { .type = NLA_U32 },
223 [NDTA_THRESH2] = { .type = NLA_U32 },
224 [NDTA_THRESH3] = { .type = NLA_U32 },
225 [NDTA_GC_INTERVAL] = { .type = NLA_U32 },
226 [NDTA_CONFIG] = { .minlen = sizeof(struct ndt_config) },
227 [NDTA_STATS] = { .minlen = sizeof(struct ndt_stats) },
228 [NDTA_PARMS] = { .type = NLA_NESTED },
229};
230
231static int neightbl_msg_parser(struct nl_cache_ops *ops,
232 struct sockaddr_nl *who, struct nlmsghdr *n,
233 struct nl_parser_param *pp)
234{
235 struct rtnl_neightbl *ntbl;
236 struct nlattr *tb[NDTA_MAX + 1];
237 struct rtgenmsg *rtmsg;
238 int err;
239
240 ntbl = rtnl_neightbl_alloc();
241 if (!ntbl) {
242 err = -NLE_NOMEM;
243 goto errout;
244 }
245
246 ntbl->ce_msgtype = n->nlmsg_type;
247 rtmsg = nlmsg_data(n);
248
249 err = nlmsg_parse(n, sizeof(*rtmsg), tb, NDTA_MAX, neightbl_policy);
250 if (err < 0)
251 goto errout;
252
253 ntbl->nt_family = rtmsg->rtgen_family;
254
255 if (tb[NDTA_NAME] == NULL) {
256 err = -NLE_MISSING_ATTR;
257 goto errout;
258 }
259
260 nla_strlcpy(ntbl->nt_name, tb[NDTA_NAME], NTBLNAMSIZ);
261 ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
262
263 if (tb[NDTA_THRESH1]) {
264 ntbl->nt_gc_thresh1 = nla_get_u32(tb[NDTA_THRESH1]);
265 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
266 }
267
268 if (tb[NDTA_THRESH2]) {
269 ntbl->nt_gc_thresh2 = nla_get_u32(tb[NDTA_THRESH2]);
270 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
271 }
272
273 if (tb[NDTA_THRESH3]) {
274 ntbl->nt_gc_thresh3 = nla_get_u32(tb[NDTA_THRESH3]);
275 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
276 }
277
278 if (tb[NDTA_GC_INTERVAL]) {
279 ntbl->nt_gc_interval = nla_get_u32(tb[NDTA_GC_INTERVAL]);
280 ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
281 }
282
283 if (tb[NDTA_CONFIG]) {
284 nla_memcpy(&ntbl->nt_config, tb[NDTA_CONFIG],
285 sizeof(ntbl->nt_config));
286 ntbl->ce_mask |= NEIGHTBL_ATTR_CONFIG;
287 }
288
289 if (tb[NDTA_STATS]) {
290 nla_memcpy(&ntbl->nt_stats, tb[NDTA_STATS],
291 sizeof(ntbl->nt_stats));
292 ntbl->ce_mask |= NEIGHTBL_ATTR_STATS;
293 }
294
295 if (tb[NDTA_PARMS]) {
296 struct nlattr *tbp[NDTPA_MAX + 1];
297 struct rtnl_neightbl_parms *p = &ntbl->nt_parms;
298
299 err = nla_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS], NULL);
300 if (err < 0)
301 goto errout;
302
303#define COPY_ENTRY(name, var) \
304 if (tbp[NDTPA_##name]) { \
305 p->ntp_##var = nla_get_u32(tbp[NDTPA_##name]); \
306 p->ntp_mask |= NEIGHTBLPARM_ATTR_##name; \
307 }
308
309 COPY_ENTRY(IFINDEX, ifindex);
310 COPY_ENTRY(REFCNT, refcnt);
311 COPY_ENTRY(QUEUE_LEN, queue_len);
312 COPY_ENTRY(APP_PROBES, app_probes);
313 COPY_ENTRY(UCAST_PROBES, ucast_probes);
314 COPY_ENTRY(MCAST_PROBES, mcast_probes);
315 COPY_ENTRY(PROXY_QLEN, proxy_qlen);
316 COPY_ENTRY(PROXY_DELAY, proxy_delay);
317 COPY_ENTRY(ANYCAST_DELAY, anycast_delay);
318 COPY_ENTRY(LOCKTIME, locktime);
319 COPY_ENTRY(REACHABLE_TIME, reachable_time);
320 COPY_ENTRY(BASE_REACHABLE_TIME, base_reachable_time);
321 COPY_ENTRY(RETRANS_TIME, retrans_time);
322 COPY_ENTRY(GC_STALETIME, gc_stale_time);
323 COPY_ENTRY(DELAY_PROBE_TIME, probe_delay);
324#undef COPY_ENTRY
325
326 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
327 }
328
329 err = pp->pp_cb((struct nl_object *)ntbl, pp);
330errout:
331 rtnl_neightbl_put(ntbl);
332 return err;
333}
334
335static int neightbl_request_update(struct nl_cache *c, struct nl_sock *h)
336{
337 return nl_rtgen_request(h, RTM_GETNEIGHTBL, AF_UNSPEC, NLM_F_DUMP);
338}
339
340static void neightbl_dump_line(struct nl_object *arg, struct nl_dump_params *p)
341{
342 struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *)arg;
343
344 nl_dump_line(p, "%s", ntbl->nt_name);
345
346 if (ntbl->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX) {
347 struct nl_cache *link_cache;
348
349 link_cache = nl_cache_mngt_require_safe("route/link");
350
351 if (link_cache) {
352 char buf[32];
353 nl_dump(p, "<%s> ",
354 rtnl_link_i2name(link_cache,
355 ntbl->nt_parms.ntp_ifindex,
356 buf, sizeof(buf)));
357 nl_cache_put(link_cache);
358 } else
359 nl_dump(p, "<%u> ", ntbl->nt_parms.ntp_ifindex);
360 } else
361 nl_dump(p, " ");
362
363 if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG)
364 nl_dump(p, "entries %u ", ntbl->nt_config.ndtc_entries);
365
366 if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
367 char rt[32], rt2[32];
368 struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
369
370 nl_dump(p, "reachable-time %s retransmit-time %s",
371 nl_msec2str(pa->ntp_reachable_time, rt, sizeof(rt)),
372 nl_msec2str(pa->ntp_retrans_time, rt2, sizeof(rt2)));
373 }
374
375 nl_dump(p, "\n");
376}
377
378static void neightbl_dump_details(struct nl_object *arg,
379 struct nl_dump_params *p)
380{
381 char x[32], y[32], z[32];
382 struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *)arg;
383
384 neightbl_dump_line(arg, p);
385
386 if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG) {
387 nl_dump_line(p, " key-len %u entry-size %u last-flush %s\n",
388 ntbl->nt_config.ndtc_key_len,
389 ntbl->nt_config.ndtc_entry_size,
390 nl_msec2str(ntbl->nt_config.ndtc_last_flush, x,
391 sizeof(x)));
392
393 nl_dump_line(p,
394 " gc threshold %u/%u/%u interval %s "
395 "chain-position %u\n",
396 ntbl->nt_gc_thresh1, ntbl->nt_gc_thresh2,
397 ntbl->nt_gc_thresh3,
398 nl_msec2str(ntbl->nt_gc_interval, x, sizeof(x)),
399 ntbl->nt_config.ndtc_hash_chain_gc);
400
401 nl_dump_line(p, " hash-rand 0x%08X/0x%08X last-rand %s\n",
402 ntbl->nt_config.ndtc_hash_rnd,
403 ntbl->nt_config.ndtc_hash_mask,
404 nl_msec2str(ntbl->nt_config.ndtc_last_rand, x,
405 sizeof(x)));
406 }
407
408 if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
409 struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
410
411 nl_dump_line(p,
412 " refcnt %u pending-queue-limit %u "
413 "proxy-delayed-queue-limit %u\n",
414 pa->ntp_refcnt, pa->ntp_queue_len,
415 pa->ntp_proxy_qlen);
416
417 nl_dump_line(p,
418 " num-userspace-probes %u num-unicast-probes "
419 "%u num-multicast-probes %u\n",
421 pa->ntp_mcast_probes);
422
423 nl_dump_line(p,
424 " min-age %s base-reachable-time %s "
425 "stale-check-interval %s\n",
426 nl_msec2str(pa->ntp_locktime, x, sizeof(x)),
428 sizeof(y)),
429 nl_msec2str(pa->ntp_gc_stale_time, z, sizeof(z)));
430
431 nl_dump_line(p,
432 " initial-probe-delay %s answer-delay %s "
433 "proxy-answer-delay %s\n",
434 nl_msec2str(pa->ntp_probe_delay, x, sizeof(x)),
435 nl_msec2str(pa->ntp_anycast_delay, y, sizeof(y)),
436 nl_msec2str(pa->ntp_proxy_delay, z, sizeof(z)));
437 }
438}
439
440static void neightbl_dump_stats(struct nl_object *arg, struct nl_dump_params *p)
441{
442 struct rtnl_neightbl *ntbl = (struct rtnl_neightbl *)arg;
443
444 neightbl_dump_details(arg, p);
445
446 if (!(ntbl->ce_mask & NEIGHTBL_ATTR_STATS))
447 return;
448
449 nl_dump_line(p,
450 " "
451 " lookups %llu hits %llu failed %llu"
452 " allocations %llu destroys %llu\n",
453 (long long unsigned)ntbl->nt_stats.ndts_lookups,
454 (long long unsigned)ntbl->nt_stats.ndts_hits,
455 (long long unsigned)ntbl->nt_stats.ndts_res_failed,
456 (long long unsigned)ntbl->nt_stats.ndts_allocs,
457 (long long unsigned)ntbl->nt_stats.ndts_destroys);
458
459 nl_dump_line(p,
460 " "
461 " hash-grows %llu forced-gc-runs %llu"
462 " periodic-gc-runs %llu\n",
463 (long long unsigned)ntbl->nt_stats.ndts_hash_grows,
464 (long long unsigned)ntbl->nt_stats.ndts_forced_gc_runs,
465 (long long unsigned)ntbl->nt_stats.ndts_periodic_gc_runs);
466
467 nl_dump_line(p,
468 " "
469 " rcv-unicast-probes %llu"
470 " rcv-multicast-probes %llu"
471 "\n",
472 (long long unsigned)ntbl->nt_stats.ndts_rcv_probes_ucast,
473 (long long unsigned)ntbl->nt_stats.ndts_rcv_probes_mcast);
474}
475
476/**
477 * @name Allocation/Freeing
478 * @{
479 */
480
481struct rtnl_neightbl *rtnl_neightbl_alloc(void)
482{
483 return (struct rtnl_neightbl *)nl_object_alloc(&neightbl_obj_ops);
484}
485
486void rtnl_neightbl_put(struct rtnl_neightbl *neightbl)
487{
488 nl_object_put((struct nl_object *)neightbl);
489}
490
491/** @} */
492
493/**
494 * @name Neighbour Table Cache Management
495 * @{
496 */
497
498/**
499 * Build a neighbour table cache including all neighbour tables currently configured in the kernel.
500 * @arg sk Netlink socket.
501 * @arg result Pointer to store resulting cache.
502 *
503 * Allocates a new neighbour table cache, initializes it properly and
504 * updates it to include all neighbour tables currently configured in
505 * the kernel.
506 *
507 * @return 0 on success or a negative error code.
508 */
509int rtnl_neightbl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
510{
511 return nl_cache_alloc_and_fill(&rtnl_neightbl_ops, sk, result);
512}
513
514/**
515 * Lookup neighbour table by name and optional interface index
516 * @arg cache neighbour table cache
517 * @arg name name of table
518 * @arg ifindex optional interface index
519 *
520 * Looks up the neighbour table matching the specified name and
521 * optionally the specified ifindex to retrieve device specific
522 * parameter sets.
523 *
524 * @return ptr to neighbour table inside the cache or NULL if no
525 * match was found.
526 */
527struct rtnl_neightbl *rtnl_neightbl_get(struct nl_cache *cache,
528 const char *name, int ifindex)
529{
530 struct rtnl_neightbl *nt;
531
532 if (cache->c_ops != &rtnl_neightbl_ops)
533 return NULL;
534
535 nl_list_for_each_entry(nt, &cache->c_items, ce_list) {
536 if (!strcasecmp(nt->nt_name, name) &&
537 ((!ifindex && !nt->nt_parms.ntp_ifindex) ||
538 (ifindex && ifindex == nt->nt_parms.ntp_ifindex))) {
539 nl_object_get((struct nl_object *)nt);
540 return nt;
541 }
542 }
543
544 return NULL;
545}
546
547/** @} */
548
549/**
550 * @name Neighbour Table Modifications
551 * @{
552 */
553
554/**
555 * Builds a netlink change request message to change neighbour table attributes
556 * @arg old neighbour table to change
557 * @arg tmpl template with requested changes
558 * @arg result Pointer to store resulting message.
559 *
560 * Builds a new netlink message requesting a change of neighbour table
561 * attributes. The netlink message header isn't fully equipped with all
562 * relevant fields and must be sent out via nl_send_auto_complete() or
563 * supplemented as needed.
564 * \a old must point to a neighbour table currently configured in the
565 * kernel and \a tmpl must contain the attributes to be changed set via
566 * \c rtnl_neightbl_set_* functions.
567 *
568 * @return 0 on success or a negative error code.
569 */
571 struct rtnl_neightbl *tmpl,
572 struct nl_msg **result)
573{
574 struct nl_msg *m, *parms = NULL;
575 struct ndtmsg ndt = {
576 .ndtm_family = old->nt_family,
577 };
578
579 m = nlmsg_alloc_simple(RTM_SETNEIGHTBL, 0);
580 if (!m)
581 return -NLE_NOMEM;
582
583 if (nlmsg_append(m, &ndt, sizeof(ndt), NLMSG_ALIGNTO) < 0)
584 goto nla_put_failure;
585
586 NLA_PUT_STRING(m, NDTA_NAME, old->nt_name);
587
588 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH1)
589 NLA_PUT_U32(m, NDTA_THRESH1, tmpl->nt_gc_thresh1);
590
591 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
592 NLA_PUT_U32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
593
594 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
595 NLA_PUT_U32(m, NDTA_THRESH2, tmpl->nt_gc_thresh2);
596
597 if (tmpl->ce_mask & NEIGHTBL_ATTR_GC_INTERVAL)
598 NLA_PUT_U64(m, NDTA_GC_INTERVAL, tmpl->nt_gc_interval);
599
600 if (tmpl->ce_mask & NEIGHTBL_ATTR_PARMS) {
601 struct rtnl_neightbl_parms *p = &tmpl->nt_parms;
602
603 parms = nlmsg_alloc();
604 if (!parms)
605 goto nla_put_failure;
606
607 if (old->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX)
608 NLA_PUT_U32(parms, NDTPA_IFINDEX,
609 old->nt_parms.ntp_ifindex);
610
611 if (p->ntp_mask & NEIGHTBLPARM_ATTR_QUEUE_LEN)
612 NLA_PUT_U32(parms, NDTPA_QUEUE_LEN, p->ntp_queue_len);
613
614 if (p->ntp_mask & NEIGHTBLPARM_ATTR_APP_PROBES)
615 NLA_PUT_U32(parms, NDTPA_APP_PROBES, p->ntp_app_probes);
616
617 if (p->ntp_mask & NEIGHTBLPARM_ATTR_UCAST_PROBES)
618 NLA_PUT_U32(parms, NDTPA_UCAST_PROBES,
620
621 if (p->ntp_mask & NEIGHTBLPARM_ATTR_MCAST_PROBES)
622 NLA_PUT_U32(parms, NDTPA_MCAST_PROBES,
624
625 if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_QLEN)
626 NLA_PUT_U32(parms, NDTPA_PROXY_QLEN, p->ntp_proxy_qlen);
627
628 if (p->ntp_mask & NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME)
629 NLA_PUT_U64(parms, NDTPA_BASE_REACHABLE_TIME,
631
632 if (p->ntp_mask & NEIGHTBLPARM_ATTR_RETRANS_TIME)
633 NLA_PUT_U64(parms, NDTPA_RETRANS_TIME,
635
636 if (p->ntp_mask & NEIGHTBLPARM_ATTR_GC_STALETIME)
637 NLA_PUT_U64(parms, NDTPA_GC_STALETIME,
639
640 if (p->ntp_mask & NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME)
641 NLA_PUT_U64(parms, NDTPA_DELAY_PROBE_TIME,
642 p->ntp_proxy_delay);
643
644 if (p->ntp_mask & NEIGHTBLPARM_ATTR_ANYCAST_DELAY)
645 NLA_PUT_U64(parms, NDTPA_ANYCAST_DELAY,
647
648 if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_DELAY)
649 NLA_PUT_U64(parms, NDTPA_PROXY_DELAY,
650 p->ntp_proxy_delay);
651
652 if (p->ntp_mask & NEIGHTBLPARM_ATTR_LOCKTIME)
653 NLA_PUT_U64(parms, NDTPA_LOCKTIME, p->ntp_locktime);
654
655 if (nla_put_nested(m, NDTA_PARMS, parms) < 0)
656 goto nla_put_failure;
657
658 nlmsg_free(parms);
659 }
660
661 *result = m;
662 return 0;
663
664nla_put_failure:
665 if (parms)
666 nlmsg_free(parms);
667 nlmsg_free(m);
668 return -NLE_MSGSIZE;
669}
670
671/**
672 * Change neighbour table attributes
673 * @arg sk Netlink socket.
674 * @arg old neighbour table to be changed
675 * @arg tmpl template with requested changes
676 *
677 * Builds a new netlink message by calling
678 * rtnl_neightbl_build_change_request(), sends the request to the
679 * kernel and waits for the next ACK to be received, i.e. blocks
680 * until the request has been processed.
681 *
682 * @return 0 on success or a negative error code
683 */
684int rtnl_neightbl_change(struct nl_sock *sk, struct rtnl_neightbl *old,
685 struct rtnl_neightbl *tmpl)
686{
687 struct nl_msg *msg;
688 int err;
689
690 if ((err = rtnl_neightbl_build_change_request(old, tmpl, &msg)) < 0)
691 return err;
692
693 err = nl_send_auto_complete(sk, msg);
694 nlmsg_free(msg);
695 if (err < 0)
696 return err;
697
698 return wait_for_ack(sk);
699}
700
701/** @} */
702
703/**
704 * @name Attribute Modification
705 * @{
706 */
707
708void rtnl_neightbl_set_family(struct rtnl_neightbl *ntbl, int family)
709{
710 ntbl->nt_family = family;
711 ntbl->ce_mask |= NEIGHTBL_ATTR_FAMILY;
712}
713
714void rtnl_neightbl_set_gc_interval(struct rtnl_neightbl *ntbl, uint64_t ms)
715{
716 ntbl->nt_gc_interval = ms;
717 ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
718}
719
720void rtnl_neightbl_set_gc_tresh1(struct rtnl_neightbl *ntbl, int thresh)
721{
722 ntbl->nt_gc_thresh1 = thresh;
723 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
724}
725
726void rtnl_neightbl_set_gc_tresh2(struct rtnl_neightbl *ntbl, int thresh)
727{
728 ntbl->nt_gc_thresh2 = thresh;
729 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
730}
731
732void rtnl_neightbl_set_gc_tresh3(struct rtnl_neightbl *ntbl, int thresh)
733{
734 ntbl->nt_gc_thresh3 = thresh;
735 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
736}
737
738void rtnl_neightbl_set_name(struct rtnl_neightbl *ntbl, const char *name)
739{
740 _nl_strncpy_trunc(ntbl->nt_name, name, sizeof(ntbl->nt_name));
741 ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
742}
743
744void rtnl_neightbl_set_dev(struct rtnl_neightbl *ntbl, int ifindex)
745{
746 ntbl->nt_parms.ntp_ifindex = ifindex;
747 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_IFINDEX;
748 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
749}
750
751/**
752 * Set the queue length for pending requests of a neighbour table to the specified value
753 * @arg ntbl neighbour table to change
754 * @arg len new queue len
755 */
757{
758 ntbl->nt_parms.ntp_queue_len = len;
759 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_QUEUE_LEN;
760 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
761}
762
763/**
764 * Set the queue length for delay proxy arp requests of a neighbour table to the specified value
765 * @arg ntbl neighbour table to change
766 * @arg len new queue len
767 */
769{
770 ntbl->nt_parms.ntp_proxy_qlen = len;
771 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_QLEN;
772 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
773}
774
775/**
776 * Set the number of application probes of a neighbour table to the specified value
777 * @arg ntbl neighbour table to change
778 * @arg probes new probes value
779 */
780void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *ntbl, int probes)
781{
782 ntbl->nt_parms.ntp_app_probes = probes;
783 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_APP_PROBES;
784 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
785}
786
787/**
788 * Set the number of unicast probes of a neighbour table to the specified value
789 * @arg ntbl neighbour table to change
790 * @arg probes new probes value
791 */
792void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *ntbl, int probes)
793{
794 ntbl->nt_parms.ntp_ucast_probes = probes;
795 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_UCAST_PROBES;
796 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
797}
798
799/**
800 * Set the number of multicast probes of a neighbour table to the specified value
801 * @arg ntbl neighbour table to change
802 * @arg probes new probes value
803 */
804void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *ntbl, int probes)
805{
806 ntbl->nt_parms.ntp_mcast_probes = probes;
807 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_MCAST_PROBES;
808 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
809}
810
811/**
812 * Set the base reachable time of a neighbour table to the specified value
813 * @arg ntbl neighbour table to change
814 * @arg ms new base reachable time in milliseconds
815 */
817 uint64_t ms)
818{
819 ntbl->nt_parms.ntp_base_reachable_time = ms;
820 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME;
821 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
822}
823
824/**
825 * Set the retransmit time of a neighbour table to the specified value
826 * @arg ntbl neighbour table to change
827 * @arg ms new retransmit time
828 */
829void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *ntbl, uint64_t ms)
830{
831 ntbl->nt_parms.ntp_retrans_time = ms;
832 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_RETRANS_TIME;
833 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
834}
835
836/**
837 * Set the gc stale time of a neighbour table to the specified value
838 * @arg ntbl neighbour table to change
839 * @arg ms new gc stale time in milliseconds
840 */
841void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *ntbl, uint64_t ms)
842{
843 ntbl->nt_parms.ntp_gc_stale_time = ms;
844 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_GC_STALETIME;
845 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
846}
847
848/**
849 * Set the first probe delay time of a neighbour table to the specified value
850 * @arg ntbl neighbour table to change
851 * @arg ms new first probe delay time in milliseconds
852 */
854{
855 ntbl->nt_parms.ntp_probe_delay = ms;
856 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME;
857 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
858}
859
860/**
861 * Set the anycast delay of a neighbour table to the specified value
862 * @arg ntbl neighbour table to change
863 * @arg ms new anycast delay in milliseconds
864 */
865void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
866{
867 ntbl->nt_parms.ntp_anycast_delay = ms;
868 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_ANYCAST_DELAY;
869 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
870}
871
872/**
873 * Set the proxy delay of a neighbour table to the specified value
874 * @arg ntbl neighbour table to change
875 * @arg ms new proxy delay in milliseconds
876 */
877void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
878{
879 ntbl->nt_parms.ntp_proxy_delay = ms;
880 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_DELAY;
881 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
882}
883
884/**
885 * Set the locktime of a neighbour table to the specified value
886 * @arg ntbl neighbour table to change
887 * @arg ms new locktime in milliseconds
888 */
889void rtnl_neightbl_set_locktime(struct rtnl_neightbl *ntbl, uint64_t ms)
890{
891 ntbl->nt_parms.ntp_locktime = ms;
892 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_LOCKTIME;
893 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
894}
895
896/** @} */
897
898static struct nl_object_ops neightbl_obj_ops = {
899 .oo_name = "route/neightbl",
900 .oo_size = sizeof(struct rtnl_neightbl),
901 .oo_dump = {
902 [NL_DUMP_LINE] = neightbl_dump_line,
903 [NL_DUMP_DETAILS] = neightbl_dump_details,
904 [NL_DUMP_STATS] = neightbl_dump_stats,
905 },
906 .oo_compare = neightbl_compare,
907};
908
909static struct nl_cache_ops rtnl_neightbl_ops = {
910 .co_name = "route/neightbl",
911 .co_hdrsize = sizeof(struct rtgenmsg),
912 .co_msgtypes = {
913 { RTM_NEWNEIGHTBL, NL_ACT_NEW, "new" },
914 { RTM_SETNEIGHTBL, NL_ACT_SET, "set" },
915 { RTM_GETNEIGHTBL, NL_ACT_GET, "get" },
916 END_OF_MSGTYPES_LIST,
917 },
918 .co_protocol = NETLINK_ROUTE,
919 .co_request_update = neightbl_request_update,
920 .co_msg_parser = neightbl_msg_parser,
921 .co_obj_ops = &neightbl_obj_ops,
922};
923
924static void _nl_init neightbl_init(void)
925{
926 nl_cache_mngt_register(&rtnl_neightbl_ops);
927}
928
929static void _nl_exit neightbl_exit(void)
930{
931 nl_cache_mngt_unregister(&rtnl_neightbl_ops);
932}
933
934/** @} */
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition attr.c:710
int nla_put_nested(struct nl_msg *msg, int attrtype, const struct nl_msg *nested)
Add nested attributes to netlink message.
Definition attr.c:888
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition attr.h:230
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition attr.c:351
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition attr.c:379
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition attr.c:1033
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
Definition attr.h:257
#define NLA_PUT_U64(msg, attrtype, value)
Add 64 bit integer attribute to netlink message.
Definition attr.h:248
@ NLA_STRING
NUL terminated character string.
Definition attr.h:39
@ NLA_NESTED
Nested attributes.
Definition attr.h:42
@ NLA_U32
32 bit integer
Definition attr.h:37
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition cache_mngt.c:287
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition cache_mngt.c:252
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
Definition cache_mngt.c:430
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
Definition cache.c:234
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition msg.c:349
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition msg.c:108
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition msg.c:566
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
Definition msg.c:216
struct nl_msg * nlmsg_alloc(void)
Allocate a new netlink message with the default maximum payload size.
Definition msg.c:302
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition msg.c:450
void rtnl_neightbl_set_proxy_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the proxy delay of a neighbour table to the specified value.
Definition neightbl.c:877
int rtnl_neightbl_change(struct nl_sock *sk, struct rtnl_neightbl *old, struct rtnl_neightbl *tmpl)
Change neighbour table attributes.
Definition neightbl.c:684
struct rtnl_neightbl * rtnl_neightbl_get(struct nl_cache *cache, const char *name, int ifindex)
Lookup neighbour table by name and optional interface index.
Definition neightbl.c:527
void rtnl_neightbl_set_locktime(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the locktime of a neighbour table to the specified value.
Definition neightbl.c:889
int rtnl_neightbl_build_change_request(struct rtnl_neightbl *old, struct rtnl_neightbl *tmpl, struct nl_msg **result)
Builds a netlink change request message to change neighbour table attributes.
Definition neightbl.c:570
void rtnl_neightbl_set_gc_stale_time(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the gc stale time of a neighbour table to the specified value.
Definition neightbl.c:841
void rtnl_neightbl_set_queue_len(struct rtnl_neightbl *ntbl, int len)
Set the queue length for pending requests of a neighbour table to the specified value.
Definition neightbl.c:756
int rtnl_neightbl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Build a neighbour table cache including all neighbour tables currently configured in the kernel.
Definition neightbl.c:509
void rtnl_neightbl_set_app_probes(struct rtnl_neightbl *ntbl, int probes)
Set the number of application probes of a neighbour table to the specified value.
Definition neightbl.c:780
void rtnl_neightbl_set_anycast_delay(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the anycast delay of a neighbour table to the specified value.
Definition neightbl.c:865
void rtnl_neightbl_set_delay_probe_time(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the first probe delay time of a neighbour table to the specified value.
Definition neightbl.c:853
void rtnl_neightbl_set_retrans_time(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the retransmit time of a neighbour table to the specified value.
Definition neightbl.c:829
void rtnl_neightbl_set_mcast_probes(struct rtnl_neightbl *ntbl, int probes)
Set the number of multicast probes of a neighbour table to the specified value.
Definition neightbl.c:804
void rtnl_neightbl_set_proxy_queue_len(struct rtnl_neightbl *ntbl, int len)
Set the queue length for delay proxy arp requests of a neighbour table to the specified value.
Definition neightbl.c:768
void rtnl_neightbl_set_base_reachable_time(struct rtnl_neightbl *ntbl, uint64_t ms)
Set the base reachable time of a neighbour table to the specified value.
Definition neightbl.c:816
void rtnl_neightbl_set_ucast_probes(struct rtnl_neightbl *ntbl, int probes)
Set the number of unicast probes of a neighbour table to the specified value.
Definition neightbl.c:792
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition object.c:221
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition object.c:210
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition object.c:55
int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
Send routing netlink request message.
Definition rtnl.c:38
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition nl.c:1246
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition utils.c:1017
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
Definition utils.c:650
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition types.h:22
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition types.h:20
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
Dumping parameters.
Definition types.h:32
Attribute validation policy.
Definition attr.h:63
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition attr.h:65
uint64_t ntp_probe_delay
Delay in milliseconds for the first time probe if the neighbour is reachable.
Definition neightbl.c:86
uint64_t ntp_retrans_time
The time in milliseconds between retransmitted Neighbor Solicitation messages.
Definition neightbl.c:74
uint64_t ntp_reachable_time
Actual reachable time (read-only)
Definition neightbl.c:68
uint64_t ntp_proxy_delay
Delay in milliseconds before answering to an ARP request for which a proxy ARP entry exists.
Definition neightbl.c:104
uint32_t ntp_mcast_probes
Maximum number of retries for multicast solicitation.
Definition neightbl.c:58
uint32_t ntp_ifindex
Interface index of the device this parameter set is assigned to or 0 for the default set.
Definition neightbl.c:30
uint32_t ntp_proxy_qlen
Queue length for the delayed proxy arp requests.
Definition neightbl.c:109
uint64_t ntp_anycast_delay
Maximum delay in milliseconds of an answer to a neighbour solicitation message.
Definition neightbl.c:92
uint32_t ntp_app_probes
Number of requests to send to the user level ARP daemon.
Definition neightbl.c:48
uint32_t ntp_refcnt
Number of references to this parameter set.
Definition neightbl.c:35
uint32_t ntp_ucast_probes
Maximum number of retries for unicast solicitation.
Definition neightbl.c:53
uint64_t ntp_base_reachable_time
Base value in milliseconds to ompute reachable time, see RFC2461.
Definition neightbl.c:63
uint32_t ntp_mask
Mask of available parameter attributes.
Definition neightbl.c:114
uint32_t ntp_queue_len
Queue length for pending arp requests, i.e.
Definition neightbl.c:42
uint64_t ntp_gc_stale_time
Interval in milliseconds to check for stale neighbour entries.
Definition neightbl.c:80
uint64_t ntp_locktime
Minimum age in milliseconds before a neighbour entry may be replaced.
Definition neightbl.c:98
Neighbour table.
Definition neightbl.c:123