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