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