libnl  3.2.21
neigh.c
1 /*
2  * lib/route/neigh.c Neighbours
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-2008 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup rtnl
14  * @defgroup neigh Neighbours
15  * @brief
16  *
17  * The neighbour table establishes bindings between protocol addresses and
18  * link layer addresses for hosts sharing the same physical link. This
19  * module allows you to access and manipulate the content of these tables.
20  *
21  * @par Neighbour States
22  * @code
23  * NUD_INCOMPLETE
24  * NUD_REACHABLE
25  * NUD_STALE
26  * NUD_DELAY
27  * NUD_PROBE
28  * NUD_FAILED
29  * NUD_NOARP
30  * NUD_PERMANENT
31  * @endcode
32  *
33  * @par Neighbour Flags
34  * @code
35  * NTF_USE
36  * NTF_PROXY
37  * NTF_ROUTER
38  * @endcode
39  *
40  * @par Neighbour Identification
41  * A neighbour is uniquely identified by the attributes listed below, whenever
42  * you refer to an existing neighbour all of the attributes must be set.
43  * Neighbours from caches automatically have all required attributes set.
44  * - interface index (rtnl_neigh_set_ifindex())
45  * - destination address (rtnl_neigh_set_dst())
46  *
47  * @par Changeable Attributes
48  * \anchor neigh_changeable
49  * - state (rtnl_neigh_set_state())
50  * - link layer address (rtnl_neigh_set_lladdr())
51  *
52  * @par Required Caches for Dumping
53  * In order to dump neighbour attributes you must provide the following
54  * caches via nl_cache_provide()
55  * - link cache holding all links
56  *
57  * @par TODO
58  * - Document proxy settings
59  * - Document states and their influence
60  *
61  * @par 1) Retrieving information about configured neighbours
62  * @code
63  * // The first step is to retrieve a list of all available neighbour within
64  * // the kernel and put them into a cache.
65  * struct nl_cache *cache = rtnl_neigh_alloc_cache(sk);
66  *
67  * // Neighbours can then be looked up by the interface and destination
68  * // address:
69  * struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr);
70  *
71  * // After successful usage, the object must be given back to the cache
72  * rtnl_neigh_put(neigh);
73  * @endcode
74  *
75  * @par 2) Adding new neighbours
76  * @code
77  * // Allocate an empty neighbour handle to be filled out with the attributes
78  * // of the new neighbour.
79  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
80  *
81  * // Fill out the attributes of the new neighbour
82  * rtnl_neigh_set_ifindex(neigh, ifindex);
83  * rtnl_neigh_set_dst(neigh, dst_addr);
84  * rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent"));
85  *
86  * // Build the netlink message and send it to the kernel, the operation will
87  * // block until the operation has been completed. Alternatively the required
88  * // netlink message can be built using rtnl_neigh_build_add_request()
89  * // to be sent out using nl_send_auto_complete().
90  * rtnl_neigh_add(sk, neigh, NLM_F_CREATE);
91  *
92  * // Free the memory
93  * rtnl_neigh_put(neigh);
94  * @endcode
95  *
96  * @par 3) Deleting an existing neighbour
97  * @code
98  * // Allocate an empty neighbour object to be filled out with the attributes
99  * // matching the neighbour to be deleted. Alternatively a fully equipped
100  * // neighbour object out of a cache can be used instead.
101  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
102  *
103  * // Neighbours are uniquely identified by their interface index and
104  * // destination address, you may fill out other attributes but they
105  * // will have no influence.
106  * rtnl_neigh_set_ifindex(neigh, ifindex);
107  * rtnl_neigh_set_dst(neigh, dst_addr);
108  *
109  * // Build the netlink message and send it to the kernel, the operation will
110  * // block until the operation has been completed. Alternatively the required
111  * // netlink message can be built using rtnl_neigh_build_delete_request()
112  * // to be sent out using nl_send_auto_complete().
113  * rtnl_neigh_delete(sk, neigh, 0);
114  *
115  * // Free the memory
116  * rtnl_neigh_put(neigh);
117  * @endcode
118  *
119  * @par 4) Changing neighbour attributes
120  * @code
121  * // Allocate an empty neighbour object to be filled out with the attributes
122  * // matching the neighbour to be changed and the new parameters. Alternatively
123  * // a fully equipped modified neighbour object out of a cache can be used.
124  * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
125  *
126  * // Identify the neighbour to be changed by its interface index and
127  * // destination address
128  * rtnl_neigh_set_ifindex(neigh, ifindex);
129  * rtnl_neigh_set_dst(neigh, dst_addr);
130  *
131  * // The link layer address may be modified, if so it is wise to change
132  * // its state to "permanent" in order to avoid having it overwritten.
133  * rtnl_neigh_set_lladdr(neigh, lladdr);
134  *
135  * // Secondly the state can be modified allowing normal neighbours to be
136  * // converted into permanent entries or to manually confirm a neighbour.
137  * rtnl_neigh_set_state(neigh, state);
138  *
139  * // Build the netlink message and send it to the kernel, the operation will
140  * // block until the operation has been completed. Alternatively the required
141  * // netlink message can be built using rtnl_neigh_build_change_request()
142  * // to be sent out using nl_send_auto_complete().
143  * rtnl_neigh_add(sk, neigh, NLM_F_REPLACE);
144  *
145  * // Free the memory
146  * rtnl_neigh_put(neigh);
147  * @endcode
148  * @{
149  */
150 
151 #include <netlink-private/netlink.h>
152 #include <netlink/netlink.h>
153 #include <netlink/utils.h>
154 #include <netlink/hashtable.h>
155 #include <netlink/route/rtnl.h>
156 #include <netlink/route/neighbour.h>
157 #include <netlink/route/link.h>
158 #include <netlink/hashtable.h>
159 
160 /** @cond SKIP */
161 #define NEIGH_ATTR_FLAGS 0x01
162 #define NEIGH_ATTR_STATE 0x02
163 #define NEIGH_ATTR_LLADDR 0x04
164 #define NEIGH_ATTR_DST 0x08
165 #define NEIGH_ATTR_CACHEINFO 0x10
166 #define NEIGH_ATTR_IFINDEX 0x20
167 #define NEIGH_ATTR_FAMILY 0x40
168 #define NEIGH_ATTR_TYPE 0x80
169 #define NEIGH_ATTR_PROBES 0x100
170 #define NEIGH_ATTR_MASTER 0x200
171 
172 static struct nl_cache_ops rtnl_neigh_ops;
173 static struct nl_object_ops neigh_obj_ops;
174 /** @endcond */
175 
176 static void neigh_free_data(struct nl_object *c)
177 {
178  struct rtnl_neigh *neigh = nl_object_priv(c);
179 
180  if (!neigh)
181  return;
182 
183  nl_addr_put(neigh->n_lladdr);
184  nl_addr_put(neigh->n_dst);
185 }
186 
187 static int neigh_clone(struct nl_object *_dst, struct nl_object *_src)
188 {
189  struct rtnl_neigh *dst = nl_object_priv(_dst);
190  struct rtnl_neigh *src = nl_object_priv(_src);
191 
192  if (src->n_lladdr)
193  if (!(dst->n_lladdr = nl_addr_clone(src->n_lladdr)))
194  return -NLE_NOMEM;
195 
196  if (src->n_dst)
197  if (!(dst->n_dst = nl_addr_clone(src->n_dst)))
198  return -NLE_NOMEM;
199 
200  return 0;
201 }
202 
203 static void neigh_keygen(struct nl_object *obj, uint32_t *hashkey,
204  uint32_t table_sz)
205 {
206  struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
207  unsigned int nkey_sz;
208  struct nl_addr *addr = NULL;
209  struct neigh_hash_key {
210  uint32_t n_family;
211  uint32_t n_ifindex;
212  char n_addr[0];
213  } __attribute__((packed)) *nkey;
214  char buf[INET6_ADDRSTRLEN+5];
215 
216  if (neigh->n_family == AF_BRIDGE) {
217  if (neigh->n_lladdr)
218  addr = neigh->n_lladdr;
219  } else if (neigh->n_dst) {
220  addr = neigh->n_dst;
221  }
222 
223  nkey_sz = sizeof(*nkey);
224  if (addr)
225  nkey_sz += nl_addr_get_len(addr);
226 
227  nkey = calloc(1, nkey_sz);
228  if (!nkey) {
229  *hashkey = 0;
230  return;
231  }
232  nkey->n_family = neigh->n_family;
233  if (neigh->n_family == AF_BRIDGE)
234  nkey->n_ifindex = neigh->n_master;
235  else
236  nkey->n_ifindex = neigh->n_ifindex;
237  if (addr)
238  memcpy(nkey->n_addr,
240  nl_addr_get_len(addr));
241 
242  *hashkey = nl_hash(nkey, nkey_sz, 0) % table_sz;
243 
244  NL_DBG(5, "neigh %p key (fam %d dev %d addr %s) keysz %d hash 0x%x\n",
245  neigh, nkey->n_family, nkey->n_ifindex,
246  nl_addr2str(addr, buf, sizeof(buf)),
247  nkey_sz, *hashkey);
248 
249  free(nkey);
250 
251  return;
252 }
253 
254 static int neigh_compare(struct nl_object *_a, struct nl_object *_b,
255  uint32_t attrs, int flags)
256 {
257  struct rtnl_neigh *a = (struct rtnl_neigh *) _a;
258  struct rtnl_neigh *b = (struct rtnl_neigh *) _b;
259  int diff = 0;
260 
261 #define NEIGH_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGH_ATTR_##ATTR, a, b, EXPR)
262 
263  diff |= NEIGH_DIFF(IFINDEX, a->n_ifindex != b->n_ifindex);
264  diff |= NEIGH_DIFF(FAMILY, a->n_family != b->n_family);
265  diff |= NEIGH_DIFF(TYPE, a->n_type != b->n_type);
266  diff |= NEIGH_DIFF(LLADDR, nl_addr_cmp(a->n_lladdr, b->n_lladdr));
267  diff |= NEIGH_DIFF(DST, nl_addr_cmp(a->n_dst, b->n_dst));
268  diff |= NEIGH_DIFF(MASTER, a->n_master != b->n_master);
269 
270  if (flags & LOOSE_COMPARISON) {
271  diff |= NEIGH_DIFF(STATE,
272  (a->n_state ^ b->n_state) & b->n_state_mask);
273  diff |= NEIGH_DIFF(FLAGS,
274  (a->n_flags ^ b->n_flags) & b->n_flag_mask);
275  } else {
276  diff |= NEIGH_DIFF(STATE, a->n_state != b->n_state);
277  diff |= NEIGH_DIFF(FLAGS, a->n_flags != b->n_flags);
278  }
279 
280 #undef NEIGH_DIFF
281 
282  return diff;
283 }
284 
285 static const struct trans_tbl neigh_attrs[] = {
286  __ADD(NEIGH_ATTR_FLAGS, flags)
287  __ADD(NEIGH_ATTR_STATE, state)
288  __ADD(NEIGH_ATTR_LLADDR, lladdr)
289  __ADD(NEIGH_ATTR_DST, dst)
290  __ADD(NEIGH_ATTR_CACHEINFO, cacheinfo)
291  __ADD(NEIGH_ATTR_IFINDEX, ifindex)
292  __ADD(NEIGH_ATTR_FAMILY, family)
293  __ADD(NEIGH_ATTR_TYPE, type)
294  __ADD(NEIGH_ATTR_PROBES, probes)
295 };
296 
297 static char *neigh_attrs2str(int attrs, char *buf, size_t len)
298 {
299  return __flags2str(attrs, buf, len, neigh_attrs,
300  ARRAY_SIZE(neigh_attrs));
301 }
302 
303 static uint32_t neigh_id_attrs_get(struct nl_object *obj)
304 {
305  struct rtnl_neigh *neigh = (struct rtnl_neigh *)obj;
306 
307  if (neigh->n_family == AF_BRIDGE)
308  return (NEIGH_ATTR_LLADDR | NEIGH_ATTR_FAMILY | NEIGH_ATTR_MASTER);
309  else
310  return (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY);
311 }
312 
313 static struct nla_policy neigh_policy[NDA_MAX+1] = {
314  [NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) },
315  [NDA_PROBES] = { .type = NLA_U32 },
316 };
317 
318 static int neigh_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
319  struct nlmsghdr *n, struct nl_parser_param *pp)
320 {
321  struct rtnl_neigh *neigh;
322  int err;
323 
324  if ((err = rtnl_neigh_parse(n, &neigh)) < 0)
325  return err;
326 
327  err = pp->pp_cb((struct nl_object *) neigh, pp);
328 
329  rtnl_neigh_put(neigh);
330  return err;
331 }
332 
333 
334 int rtnl_neigh_parse(struct nlmsghdr *n, struct rtnl_neigh **result)
335 {
336  struct rtnl_neigh *neigh;
337  struct nlattr *tb[NDA_MAX + 1];
338  struct ndmsg *nm;
339  int err;
340 
341  neigh = rtnl_neigh_alloc();
342  if (!neigh) {
343  err = -NLE_NOMEM;
344  goto errout;
345  }
346 
347  neigh->ce_msgtype = n->nlmsg_type;
348  nm = nlmsg_data(n);
349 
350  err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy);
351  if (err < 0)
352  goto errout;
353 
354  neigh->n_family = nm->ndm_family;
355  neigh->n_ifindex = nm->ndm_ifindex;
356  neigh->n_state = nm->ndm_state;
357  neigh->n_flags = nm->ndm_flags;
358  neigh->n_type = nm->ndm_type;
359 
360  neigh->ce_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
361  NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
362  NEIGH_ATTR_TYPE);
363 
364  if (tb[NDA_LLADDR]) {
365  neigh->n_lladdr = nl_addr_alloc_attr(tb[NDA_LLADDR], AF_UNSPEC);
366  if (!neigh->n_lladdr) {
367  err = -NLE_NOMEM;
368  goto errout;
369  }
370  nl_addr_set_family(neigh->n_lladdr,
371  nl_addr_guess_family(neigh->n_lladdr));
372  neigh->ce_mask |= NEIGH_ATTR_LLADDR;
373  }
374 
375  if (tb[NDA_DST]) {
376  neigh->n_dst = nl_addr_alloc_attr(tb[NDA_DST], neigh->n_family);
377  if (!neigh->n_dst) {
378  err = -NLE_NOMEM;
379  goto errout;
380  }
381  neigh->ce_mask |= NEIGH_ATTR_DST;
382  }
383 
384  if (tb[NDA_CACHEINFO]) {
385  struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]);
386 
387  neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
388  neigh->n_cacheinfo.nci_used = ci->ndm_used;
389  neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
390  neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
391 
392  neigh->ce_mask |= NEIGH_ATTR_CACHEINFO;
393  }
394 
395  if (tb[NDA_PROBES]) {
396  neigh->n_probes = nla_get_u32(tb[NDA_PROBES]);
397  neigh->ce_mask |= NEIGH_ATTR_PROBES;
398  }
399 
400  /*
401  * Get the bridge index for AF_BRIDGE family entries
402  */
403  if (neigh->n_family == AF_BRIDGE) {
404  struct nl_cache *lcache = nl_cache_mngt_require_safe("route/link");
405  if (lcache ) {
406  struct rtnl_link *link = rtnl_link_get(lcache,
407  neigh->n_ifindex);
408  if (link) {
409  neigh->n_master = link->l_master;
410  rtnl_link_put(link);
411  neigh->ce_mask |= NEIGH_ATTR_MASTER;
412  }
413 
414  nl_cache_put(lcache);
415  }
416  }
417 
418  *result = neigh;
419  return 0;
420 
421 errout:
422  rtnl_neigh_put(neigh);
423  return err;
424 }
425 
426 static int neigh_request_update(struct nl_cache *c, struct nl_sock *h)
427 {
428  int family = c->c_iarg1;
429 
430  return nl_rtgen_request(h, RTM_GETNEIGH, family, NLM_F_DUMP);
431 }
432 
433 
434 static void neigh_dump_line(struct nl_object *a, struct nl_dump_params *p)
435 {
436  char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
437  struct rtnl_neigh *n = (struct rtnl_neigh *) a;
438  struct nl_cache *link_cache;
439  char state[128], flags[64];
440 
441  link_cache = nl_cache_mngt_require_safe("route/link");
442 
443  if (n->n_family != AF_BRIDGE)
444  nl_dump_line(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst)));
445 
446  if (link_cache)
447  nl_dump(p, "dev %s ",
448  rtnl_link_i2name(link_cache, n->n_ifindex,
449  state, sizeof(state)));
450  else
451  nl_dump(p, "dev %d ", n->n_ifindex);
452 
453  if (n->ce_mask & NEIGH_ATTR_LLADDR)
454  nl_dump(p, "lladdr %s ",
455  nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr)));
456 
457  rtnl_neigh_state2str(n->n_state, state, sizeof(state));
458  rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags));
459 
460  if (state[0])
461  nl_dump(p, "<%s", state);
462  if (flags[0])
463  nl_dump(p, "%s%s", state[0] ? "," : "<", flags);
464  if (state[0] || flags[0])
465  nl_dump(p, ">");
466  nl_dump(p, "\n");
467 
468  if (link_cache)
469  nl_cache_put(link_cache);
470 }
471 
472 static void neigh_dump_details(struct nl_object *a, struct nl_dump_params *p)
473 {
474  char rtn_type[32];
475  struct rtnl_neigh *n = (struct rtnl_neigh *) a;
476  int hz = nl_get_user_hz();
477 
478  neigh_dump_line(a, p);
479 
480  nl_dump_line(p, " refcnt %u type %s confirmed %u used "
481  "%u updated %u\n",
482  n->n_cacheinfo.nci_refcnt,
483  nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)),
484  n->n_cacheinfo.nci_confirmed/hz,
485  n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
486 }
487 
488 static void neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p)
489 {
490  neigh_dump_details(a, p);
491 }
492 
493 /**
494  * @name Neighbour Object Allocation/Freeage
495  * @{
496  */
497 
498 struct rtnl_neigh *rtnl_neigh_alloc(void)
499 {
500  return (struct rtnl_neigh *) nl_object_alloc(&neigh_obj_ops);
501 }
502 
503 void rtnl_neigh_put(struct rtnl_neigh *neigh)
504 {
505  nl_object_put((struct nl_object *) neigh);
506 }
507 
508 /** @} */
509 
510 /**
511  * @name Neighbour Cache Managament
512  * @{
513  */
514 
515 /**
516  * Build a neighbour cache including all neighbours currently configured in the kernel.
517  * @arg sock Netlink socket.
518  * @arg result Pointer to store resulting cache.
519  *
520  * Allocates a new neighbour cache, initializes it properly and updates it
521  * to include all neighbours currently configured in the kernel.
522  *
523  * @return 0 on success or a negative error code.
524  */
525 int rtnl_neigh_alloc_cache(struct nl_sock *sock, struct nl_cache **result)
526 {
527  return nl_cache_alloc_and_fill(&rtnl_neigh_ops, sock, result);
528 }
529 
530 /**
531  * Look up a neighbour by interface index and destination address
532  * @arg cache neighbour cache
533  * @arg ifindex interface index the neighbour is on
534  * @arg dst destination address of the neighbour
535  * @return neighbour handle or NULL if no match was found.
536  */
537 struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex,
538  struct nl_addr *dst)
539 {
540  struct rtnl_neigh *neigh;
541 
542  nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
543  if (neigh->n_family == AF_UNSPEC &&
544  neigh->n_ifindex == ifindex &&
545  !nl_addr_cmp(neigh->n_dst, dst)) {
546  nl_object_get((struct nl_object *) neigh);
547  return neigh;
548  }
549  }
550 
551  return NULL;
552 }
553 
554 /** @} */
555 
556 /**
557  * @name Neighbour Addition
558  * @{
559  */
560 
561 static int build_neigh_msg(struct rtnl_neigh *tmpl, int cmd, int flags,
562  struct nl_msg **result)
563 {
564  struct nl_msg *msg;
565  struct ndmsg nhdr = {
566  .ndm_ifindex = tmpl->n_ifindex,
567  .ndm_state = NUD_PERMANENT,
568  };
569 
570  if (tmpl->n_family != AF_BRIDGE) {
571  if (!(tmpl->ce_mask & NEIGH_ATTR_DST))
572  return -NLE_MISSING_ATTR;
573  nhdr.ndm_family = nl_addr_get_family(tmpl->n_dst);
574  }
575  else
576  nhdr.ndm_family = AF_BRIDGE;
577 
578  if (tmpl->ce_mask & NEIGH_ATTR_FLAGS)
579  nhdr.ndm_flags = tmpl->n_flags;
580 
581  if (tmpl->ce_mask & NEIGH_ATTR_STATE)
582  nhdr.ndm_state = tmpl->n_state;
583 
584  msg = nlmsg_alloc_simple(cmd, flags);
585  if (!msg)
586  return -NLE_NOMEM;
587 
588  if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0)
589  goto nla_put_failure;
590 
591  if (tmpl->n_family != AF_BRIDGE)
592  NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
593 
594  if (tmpl->ce_mask & NEIGH_ATTR_LLADDR)
595  NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
596 
597  *result = msg;
598  return 0;
599 
600 nla_put_failure:
601  nlmsg_free(msg);
602  return -NLE_MSGSIZE;
603 }
604 
605 /**
606  * Build netlink request message to add a new neighbour
607  * @arg tmpl template with data of new neighbour
608  * @arg flags additional netlink message flags
609  * @arg result Pointer to store resulting message.
610  *
611  * Builds a new netlink message requesting a addition of a new
612  * neighbour. The netlink message header isn't fully equipped with
613  * all relevant fields and must thus be sent out via nl_send_auto_complete()
614  * or supplemented as needed. \a tmpl must contain the attributes of the new
615  * neighbour set via \c rtnl_neigh_set_* functions.
616  *
617  * The following attributes must be set in the template:
618  * - Interface index (rtnl_neigh_set_ifindex())
619  * - State (rtnl_neigh_set_state())
620  * - Destination address (rtnl_neigh_set_dst())
621  * - Link layer address (rtnl_neigh_set_lladdr())
622  *
623  * @return 0 on success or a negative error code.
624  */
625 int rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags,
626  struct nl_msg **result)
627 {
628  return build_neigh_msg(tmpl, RTM_NEWNEIGH, flags, result);
629 }
630 
631 /**
632  * Add a new neighbour
633  * @arg sk Netlink socket.
634  * @arg tmpl template with requested changes
635  * @arg flags additional netlink message flags
636  *
637  * Builds a netlink message by calling rtnl_neigh_build_add_request(),
638  * sends the request to the kernel and waits for the next ACK to be
639  * received and thus blocks until the request has been fullfilled.
640  *
641  * The following attributes must be set in the template:
642  * - Interface index (rtnl_neigh_set_ifindex())
643  * - State (rtnl_neigh_set_state())
644  * - Destination address (rtnl_neigh_set_dst())
645  * - Link layer address (rtnl_neigh_set_lladdr())
646  *
647  * @return 0 on sucess or a negative error if an error occured.
648  */
649 int rtnl_neigh_add(struct nl_sock *sk, struct rtnl_neigh *tmpl, int flags)
650 {
651  int err;
652  struct nl_msg *msg;
653 
654  if ((err = rtnl_neigh_build_add_request(tmpl, flags, &msg)) < 0)
655  return err;
656 
657  err = nl_send_auto_complete(sk, msg);
658  nlmsg_free(msg);
659  if (err < 0)
660  return err;
661 
662  return wait_for_ack(sk);
663 }
664 
665 /** @} */
666 
667 /**
668  * @name Neighbour Deletion
669  * @{
670  */
671 
672 /**
673  * Build a netlink request message to delete a neighbour
674  * @arg neigh neighbour to delete
675  * @arg flags additional netlink message flags
676  * @arg result Pointer to store resulting message.
677  *
678  * Builds a new netlink message requesting a deletion of a neighbour.
679  * The netlink message header isn't fully equipped with all relevant
680  * fields and must thus be sent out via nl_send_auto_complete()
681  * or supplemented as needed. \a neigh must point to an existing
682  * neighbour.
683  *
684  * @return 0 on success or a negative error code.
685  */
686 int rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh, int flags,
687  struct nl_msg **result)
688 {
689  return build_neigh_msg(neigh, RTM_DELNEIGH, flags, result);
690 }
691 
692 /**
693  * Delete a neighbour
694  * @arg sk Netlink socket.
695  * @arg neigh neighbour to delete
696  * @arg flags additional netlink message flags
697  *
698  * Builds a netlink message by calling rtnl_neigh_build_delete_request(),
699  * sends the request to the kernel and waits for the next ACK to be
700  * received and thus blocks until the request has been fullfilled.
701  *
702  * @return 0 on sucess or a negative error if an error occured.
703  */
704 int rtnl_neigh_delete(struct nl_sock *sk, struct rtnl_neigh *neigh,
705  int flags)
706 {
707  struct nl_msg *msg;
708  int err;
709 
710  if ((err = rtnl_neigh_build_delete_request(neigh, flags, &msg)) < 0)
711  return err;
712 
713  err = nl_send_auto_complete(sk, msg);
714  nlmsg_free(msg);
715  if (err < 0)
716  return err;
717 
718  return wait_for_ack(sk);
719 }
720 
721 /** @} */
722 
723 /**
724  * @name Neighbour States Translations
725  * @{
726  */
727 
728 static const struct trans_tbl neigh_states[] = {
729  __ADD(NUD_INCOMPLETE, incomplete)
730  __ADD(NUD_REACHABLE, reachable)
731  __ADD(NUD_STALE, stale)
732  __ADD(NUD_DELAY, delay)
733  __ADD(NUD_PROBE, probe)
734  __ADD(NUD_FAILED, failed)
735  __ADD(NUD_NOARP, norarp)
736  __ADD(NUD_PERMANENT, permanent)
737 };
738 
739 char * rtnl_neigh_state2str(int state, char *buf, size_t len)
740 {
741  return __flags2str(state, buf, len, neigh_states,
742  ARRAY_SIZE(neigh_states));
743 }
744 
745 int rtnl_neigh_str2state(const char *name)
746 {
747  return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
748 }
749 
750 /** @} */
751 
752 /**
753  * @name Neighbour Flags Translations
754  * @{
755  */
756 
757 static const struct trans_tbl neigh_flags[] = {
758  __ADD(NTF_USE, use)
759  __ADD(NTF_PROXY, proxy)
760  __ADD(NTF_ROUTER, router)
761 };
762 
763 char * rtnl_neigh_flags2str(int flags, char *buf, size_t len)
764 {
765  return __flags2str(flags, buf, len, neigh_flags,
766  ARRAY_SIZE(neigh_flags));
767 }
768 
769 int rtnl_neigh_str2flag(const char *name)
770 {
771  return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
772 }
773 
774 /** @} */
775 
776 /**
777  * @name Attributes
778  * @{
779  */
780 
781 void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state)
782 {
783  neigh->n_state_mask |= state;
784  neigh->n_state |= state;
785  neigh->ce_mask |= NEIGH_ATTR_STATE;
786 }
787 
788 int rtnl_neigh_get_state(struct rtnl_neigh *neigh)
789 {
790  if (neigh->ce_mask & NEIGH_ATTR_STATE)
791  return neigh->n_state;
792  else
793  return -1;
794 }
795 
796 void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state)
797 {
798  neigh->n_state_mask |= state;
799  neigh->n_state &= ~state;
800  neigh->ce_mask |= NEIGH_ATTR_STATE;
801 }
802 
803 void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags)
804 {
805  neigh->n_flag_mask |= flags;
806  neigh->n_flags |= flags;
807  neigh->ce_mask |= NEIGH_ATTR_FLAGS;
808 }
809 
810 unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh)
811 {
812  return neigh->n_flags;
813 }
814 
815 void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags)
816 {
817  neigh->n_flag_mask |= flags;
818  neigh->n_flags &= ~flags;
819  neigh->ce_mask |= NEIGH_ATTR_FLAGS;
820 }
821 
822 void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex)
823 {
824  neigh->n_ifindex = ifindex;
825  neigh->ce_mask |= NEIGH_ATTR_IFINDEX;
826 }
827 
828 int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh)
829 {
830  return neigh->n_ifindex;
831 }
832 
833 static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos,
834  struct nl_addr *new, int flag, int nocheck)
835 {
836  if (!nocheck) {
837  if (neigh->ce_mask & NEIGH_ATTR_FAMILY) {
838  if (new->a_family != neigh->n_family)
839  return -NLE_AF_MISMATCH;
840  } else {
841  neigh->n_family = new->a_family;
842  neigh->ce_mask |= NEIGH_ATTR_FAMILY;
843  }
844  }
845 
846  if (*pos)
847  nl_addr_put(*pos);
848 
849  nl_addr_get(new);
850  *pos = new;
851 
852  neigh->ce_mask |= flag;
853 
854  return 0;
855 }
856 
857 void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr)
858 {
859  __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
860 }
861 
862 struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh)
863 {
864  if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
865  return neigh->n_lladdr;
866  else
867  return NULL;
868 }
869 
870 int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr)
871 {
872  return __assign_addr(neigh, &neigh->n_dst, addr,
873  NEIGH_ATTR_DST, 0);
874 }
875 
876 struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh)
877 {
878  if (neigh->ce_mask & NEIGH_ATTR_DST)
879  return neigh->n_dst;
880  else
881  return NULL;
882 }
883 
884 void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family)
885 {
886  neigh->n_family = family;
887  neigh->ce_mask |= NEIGH_ATTR_FAMILY;
888 }
889 
890 int rtnl_neigh_get_family(struct rtnl_neigh *neigh)
891 {
892  return neigh->n_family;
893 }
894 
895 void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type)
896 {
897  neigh->n_type = type;
898  neigh->ce_mask = NEIGH_ATTR_TYPE;
899 }
900 
901 int rtnl_neigh_get_type(struct rtnl_neigh *neigh)
902 {
903  if (neigh->ce_mask & NEIGH_ATTR_TYPE)
904  return neigh->n_type;
905  else
906  return -1;
907 }
908 
909 /** @} */
910 
911 static struct nl_object_ops neigh_obj_ops = {
912  .oo_name = "route/neigh",
913  .oo_size = sizeof(struct rtnl_neigh),
914  .oo_free_data = neigh_free_data,
915  .oo_clone = neigh_clone,
916  .oo_dump = {
917  [NL_DUMP_LINE] = neigh_dump_line,
918  [NL_DUMP_DETAILS] = neigh_dump_details,
919  [NL_DUMP_STATS] = neigh_dump_stats,
920  },
921  .oo_compare = neigh_compare,
922  .oo_keygen = neigh_keygen,
923  .oo_attrs2str = neigh_attrs2str,
924  .oo_id_attrs = (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY),
925  .oo_id_attrs_get = neigh_id_attrs_get
926 };
927 
928 static struct nl_af_group neigh_groups[] = {
929  { AF_UNSPEC, RTNLGRP_NEIGH },
930  { AF_BRIDGE, RTNLGRP_NEIGH },
931  { END_OF_GROUP_LIST },
932 };
933 
934 static struct nl_cache_ops rtnl_neigh_ops = {
935  .co_name = "route/neigh",
936  .co_hdrsize = sizeof(struct ndmsg),
937  .co_msgtypes = {
938  { RTM_NEWNEIGH, NL_ACT_NEW, "new" },
939  { RTM_DELNEIGH, NL_ACT_DEL, "del" },
940  { RTM_GETNEIGH, NL_ACT_GET, "get" },
941  END_OF_MSGTYPES_LIST,
942  },
943  .co_protocol = NETLINK_ROUTE,
944  .co_groups = neigh_groups,
945  .co_request_update = neigh_request_update,
946  .co_msg_parser = neigh_msg_parser,
947  .co_obj_ops = &neigh_obj_ops,
948 };
949 
950 static void __init neigh_init(void)
951 {
952  nl_cache_mngt_register(&rtnl_neigh_ops);
953 }
954 
955 static void __exit neigh_exit(void)
956 {
957  nl_cache_mngt_unregister(&rtnl_neigh_ops);
958 }
959 
960 /** @} */