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