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