2 * lib/route/neigh.c Neighbours
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup neigh Neighbours
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.
21 * @par Neighbour States
33 * @par Neighbour Flags
39 * @par Neighbour Identification
40 * A neighbour is uniquely identified by the attributes listed below, whenever
41 * you refer to an existing neighbour all of the attributes must be set.
42 * Neighbours from caches automatically have all required attributes set.
43 * - interface index (rtnl_neigh_set_ifindex())
44 * - destination address (rtnl_neigh_set_dst())
46 * @par Changeable Attributes
47 * \anchor neigh_changeable
48 * - state (rtnl_neigh_set_state())
49 * - link layer address (rtnl_neigh_set_lladdr())
51 * @par Required Caches for Dumping
52 * In order to dump neighbour attributes you must provide the following
53 * caches via nl_cache_provide()
54 * - link cache holding all links
57 * - Document proxy settings
58 * - Document states and their influence
60 * @par 1) Retrieving information about configured neighbours
62 * // The first step is to retrieve a list of all available neighbour within
63 * // the kernel and put them into a cache.
64 * struct nl_cache *cache = rtnl_neigh_alloc_cache(handle);
66 * // Neighbours can then be looked up by the interface and destination
68 * struct rtnl_neigh *neigh = rtnl_neigh_get(cache, ifindex, dst_addr);
70 * // After successful usage, the object must be given back to the cache
71 * rtnl_neigh_put(neigh);
74 * @par 2) Adding new neighbours
76 * // Allocate an empty neighbour handle to be filled out with the attributes
77 * // of the new neighbour.
78 * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
80 * // Fill out the attributes of the new neighbour
81 * rtnl_neigh_set_ifindex(neigh, ifindex);
82 * rtnl_neigh_set_dst(neigh, dst_addr);
83 * rtnl_neigh_set_state(neigh, rtnl_neigh_str2state("permanent"));
85 * // Build the netlink message and send it to the kernel, the operation will
86 * // block until the operation has been completed. Alternatively the required
87 * // netlink message can be built using rtnl_neigh_build_add_request()
88 * // to be sent out using nl_send_auto_complete().
89 * rtnl_neigh_add(nl_handle, neigh, NLM_F_REPLACE);
92 * rtnl_neigh_put(neigh);
95 * @par 3) Deleting an existing neighbour
97 * // Allocate an empty neighbour object to be filled out with the attributes
98 * // matching the neighbour to be deleted. Alternatively a fully equipped
99 * // neighbour object out of a cache can be used instead.
100 * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
102 * // Neighbours are uniquely identified by their interface index and
103 * // destination address, you may fill out other attributes but they
104 * // will have no influence.
105 * rtnl_neigh_set_ifindex(neigh, ifindex);
106 * rtnl_neigh_set_dst(neigh, dst_addr);
108 * // Build the netlink message and send it to the kernel, the operation will
109 * // block until the operation has been completed. Alternatively the required
110 * // netlink message can be built using rtnl_neigh_build_delete_request()
111 * // to be sent out using nl_send_auto_complete().
112 * rtnl_neigh_delete(handle, neigh, 0);
115 * rtnl_neigh_put(neigh);
118 * @par 4) Changing neighbour attributes
120 * // Allocate an empty neighbour object to be filled out with the attributes
121 * // matching the neighbour to be changed and the new parameters. Alternatively
122 * // a fully equipped modified neighbour object out of a cache can be used.
123 * struct rtnl_neigh *neigh = rtnl_neigh_alloc();
125 * // Identify the neighbour to be changed by its interface index and
126 * // destination address
127 * rtnl_neigh_set_ifindex(neigh, ifindex);
128 * rtnl_neigh_set_dst(neigh, dst_addr);
130 * // The link layer address may be modified, if so it is wise to change
131 * // its state to "permanent" in order to avoid having it overwritten.
132 * rtnl_neigh_set_lladdr(neigh, lladdr);
134 * // Secondly the state can be modified allowing normal neighbours to be
135 * // converted into permanent entries or to manually confirm a neighbour.
136 * rtnl_neigh_set_state(neigh, state);
138 * // Build the netlink message and send it to the kernel, the operation will
139 * // block until the operation has been completed. Alternatively the required
140 * // netlink message can be built using rtnl_neigh_build_change_request()
141 * // to be sent out using nl_send_auto_complete().
142 * rtnl_neigh_change(handle, neigh, 0);
145 * rtnl_neigh_put(neigh);
150 #include <netlink-local.h>
151 #include <netlink/netlink.h>
152 #include <netlink/utils.h>
153 #include <netlink/route/rtnl.h>
154 #include <netlink/route/neighbour.h>
155 #include <netlink/route/link.h>
158 #define NEIGH_ATTR_FLAGS 0x01
159 #define NEIGH_ATTR_STATE 0x02
160 #define NEIGH_ATTR_LLADDR 0x04
161 #define NEIGH_ATTR_DST 0x08
162 #define NEIGH_ATTR_CACHEINFO 0x10
163 #define NEIGH_ATTR_IFINDEX 0x20
164 #define NEIGH_ATTR_FAMILY 0x40
165 #define NEIGH_ATTR_TYPE 0x80
166 #define NEIGH_ATTR_PROBES 0x100
168 static struct nl_cache_ops rtnl_neigh_ops;
169 static struct nl_object_ops neigh_obj_ops;
172 static void neigh_free_data(struct nl_object *c)
174 struct rtnl_neigh *neigh = nl_object_priv(c);
179 nl_addr_put(neigh->n_lladdr);
180 nl_addr_put(neigh->n_dst);
183 static int neigh_clone(struct nl_object *_dst, struct nl_object *_src)
185 struct rtnl_neigh *dst = nl_object_priv(_dst);
186 struct rtnl_neigh *src = nl_object_priv(_src);
189 if (!(dst->n_lladdr = nl_addr_clone(src->n_lladdr)))
193 if (!(dst->n_dst = nl_addr_clone(src->n_dst)))
198 return nl_get_errno();
201 static int neigh_compare(struct nl_object *_a, struct nl_object *_b,
202 uint32_t attrs, int flags)
204 struct rtnl_neigh *a = (struct rtnl_neigh *) _a;
205 struct rtnl_neigh *b = (struct rtnl_neigh *) _b;
208 #define NEIGH_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGH_ATTR_##ATTR, a, b, EXPR)
210 diff |= NEIGH_DIFF(IFINDEX, a->n_ifindex != b->n_ifindex);
211 diff |= NEIGH_DIFF(FAMILY, a->n_family != b->n_family);
212 diff |= NEIGH_DIFF(TYPE, a->n_type != b->n_type);
213 diff |= NEIGH_DIFF(LLADDR, nl_addr_cmp(a->n_lladdr, b->n_lladdr));
214 diff |= NEIGH_DIFF(DST, nl_addr_cmp(a->n_dst, b->n_dst));
216 if (flags & LOOSE_FLAG_COMPARISON) {
217 diff |= NEIGH_DIFF(STATE,
218 (a->n_state ^ b->n_state) & b->n_state_mask);
219 diff |= NEIGH_DIFF(FLAGS,
220 (a->n_flags ^ b->n_flags) & b->n_flag_mask);
222 diff |= NEIGH_DIFF(STATE, a->n_state != b->n_state);
223 diff |= NEIGH_DIFF(FLAGS, a->n_flags != b->n_flags);
231 static struct trans_tbl neigh_attrs[] = {
232 __ADD(NEIGH_ATTR_FLAGS, flags)
233 __ADD(NEIGH_ATTR_STATE, state)
234 __ADD(NEIGH_ATTR_LLADDR, lladdr)
235 __ADD(NEIGH_ATTR_DST, dst)
236 __ADD(NEIGH_ATTR_CACHEINFO, cacheinfo)
237 __ADD(NEIGH_ATTR_IFINDEX, ifindex)
238 __ADD(NEIGH_ATTR_FAMILY, family)
239 __ADD(NEIGH_ATTR_TYPE, type)
240 __ADD(NEIGH_ATTR_PROBES, probes)
243 static char *neigh_attrs2str(int attrs, char *buf, size_t len)
245 return __flags2str(attrs, buf, len, neigh_attrs,
246 ARRAY_SIZE(neigh_attrs));
249 static struct nla_policy neigh_policy[NDA_MAX+1] = {
250 [NDA_CACHEINFO] = { .minlen = sizeof(struct nda_cacheinfo) },
251 [NDA_PROBES] = { .type = NLA_U32 },
254 static int neigh_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
255 struct nlmsghdr *n, struct nl_parser_param *pp)
257 struct rtnl_neigh *neigh;
258 struct nlattr *tb[NDA_MAX + 1];
262 neigh = rtnl_neigh_alloc();
264 err = nl_errno(ENOMEM);
268 neigh->ce_msgtype = n->nlmsg_type;
271 err = nlmsg_parse(n, sizeof(*nm), tb, NDA_MAX, neigh_policy);
275 neigh->n_family = nm->ndm_family;
276 neigh->n_ifindex = nm->ndm_ifindex;
277 neigh->n_state = nm->ndm_state;
278 neigh->n_flags = nm->ndm_flags;
279 neigh->n_type = nm->ndm_type;
281 neigh->ce_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
282 NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
285 if (tb[NDA_LLADDR]) {
286 neigh->n_lladdr = nla_get_addr(tb[NDA_LLADDR], AF_UNSPEC);
287 if (!neigh->n_lladdr)
289 nl_addr_set_family(neigh->n_lladdr,
290 nl_addr_guess_family(neigh->n_lladdr));
291 neigh->ce_mask |= NEIGH_ATTR_LLADDR;
295 neigh->n_dst = nla_get_addr(tb[NDA_DST], neigh->n_family);
298 neigh->ce_mask |= NEIGH_ATTR_DST;
301 if (tb[NDA_CACHEINFO]) {
302 struct nda_cacheinfo *ci = nla_data(tb[NDA_CACHEINFO]);
304 neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
305 neigh->n_cacheinfo.nci_used = ci->ndm_used;
306 neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
307 neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
309 neigh->ce_mask |= NEIGH_ATTR_CACHEINFO;
312 if (tb[NDA_PROBES]) {
313 neigh->n_probes = nla_get_u32(tb[NDA_PROBES]);
314 neigh->ce_mask |= NEIGH_ATTR_PROBES;
317 err = pp->pp_cb((struct nl_object *) neigh, pp);
324 rtnl_neigh_put(neigh);
328 static int neigh_request_update(struct nl_cache *c, struct nl_handle *h)
330 return nl_rtgen_request(h, RTM_GETNEIGH, AF_UNSPEC, NLM_F_DUMP);
334 static int neigh_dump_brief(struct nl_object *a, struct nl_dump_params *p)
336 char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
337 struct rtnl_neigh *n = (struct rtnl_neigh *) a;
338 struct nl_cache *link_cache;
339 char state[128], flags[64];
341 link_cache = nl_cache_mngt_require("route/link");
343 dp_dump(p, "%s ", nl_addr2str(n->n_dst, dst, sizeof(dst)));
346 dp_dump(p, "dev %s ",
347 rtnl_link_i2name(link_cache, n->n_ifindex,
348 state, sizeof(state)));
350 dp_dump(p, "dev %d ", n->n_ifindex);
352 if (n->ce_mask & NEIGH_ATTR_LLADDR)
353 dp_dump(p, "lladdr %s ",
354 nl_addr2str(n->n_lladdr, lladdr, sizeof(lladdr)));
356 rtnl_neigh_state2str(n->n_state, state, sizeof(state));
357 rtnl_neigh_flags2str(n->n_flags, flags, sizeof(flags));
360 dp_dump(p, "<%s", state);
362 dp_dump(p, "%s%s", state[0] ? "," : "<", flags);
363 if (state[0] || flags[0])
370 static int neigh_dump_full(struct nl_object *a, struct nl_dump_params *p)
373 struct rtnl_neigh *n = (struct rtnl_neigh *) a;
374 int hz = nl_get_hz();
376 int line = neigh_dump_brief(a, p);
378 dp_dump_line(p, line++, " refcnt %u type %s confirmed %u used "
380 n->n_cacheinfo.nci_refcnt,
381 nl_rtntype2str(n->n_type, rtn_type, sizeof(rtn_type)),
382 n->n_cacheinfo.nci_confirmed/hz,
383 n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
388 static int neigh_dump_stats(struct nl_object *a, struct nl_dump_params *p)
390 return neigh_dump_full(a, p);
393 static int neigh_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
395 struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
399 dp_dump_line(p, line++, "<neighbour>\n");
400 dp_dump_line(p, line++, " <family>%s</family>\n",
401 nl_af2str(neigh->n_family, buf, sizeof(buf)));
403 if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
404 dp_dump_line(p, line++, " <lladdr>%s</lladdr>\n",
405 nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
407 if (neigh->ce_mask & NEIGH_ATTR_DST)
408 dp_dump_line(p, line++, " <dst>%s</dst>\n",
409 nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
411 if (neigh->ce_mask & NEIGH_ATTR_IFINDEX) {
412 struct nl_cache *link_cache;
414 link_cache = nl_cache_mngt_require("route/link");
417 dp_dump_line(p, line++, " <device>%s</device>\n",
418 rtnl_link_i2name(link_cache,
422 dp_dump_line(p, line++, " <device>%u</device>\n",
426 if (neigh->ce_mask & NEIGH_ATTR_PROBES)
427 dp_dump_line(p, line++, " <probes>%u</probes>\n",
430 if (neigh->ce_mask & NEIGH_ATTR_TYPE)
431 dp_dump_line(p, line++, " <type>%s</type>\n",
432 nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
434 rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
436 dp_dump_line(p, line++, " <flags>%s</flags>\n", buf);
438 rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
440 dp_dump_line(p, line++, " <state>%s</state>\n", buf);
442 dp_dump_line(p, line++, "</neighbour>\n");
445 struct rtnl_ncacheinfo n_cacheinfo;
451 static int neigh_dump_env(struct nl_object *obj, struct nl_dump_params *p)
453 struct rtnl_neigh *neigh = (struct rtnl_neigh *) obj;
457 dp_dump_line(p, line++, "NEIGH_FAMILY=%s\n",
458 nl_af2str(neigh->n_family, buf, sizeof(buf)));
460 if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
461 dp_dump_line(p, line++, "NEIGHT_LLADDR=%s\n",
462 nl_addr2str(neigh->n_lladdr, buf, sizeof(buf)));
464 if (neigh->ce_mask & NEIGH_ATTR_DST)
465 dp_dump_line(p, line++, "NEIGH_DST=%s\n",
466 nl_addr2str(neigh->n_dst, buf, sizeof(buf)));
468 if (neigh->ce_mask & NEIGH_ATTR_IFINDEX) {
469 struct nl_cache *link_cache;
471 dp_dump_line(p, line++, "NEIGH_IFINDEX=%u\n",
474 link_cache = nl_cache_mngt_require("route/link");
476 dp_dump_line(p, line++, "NEIGH_IFNAME=%s\n",
477 rtnl_link_i2name(link_cache,
482 if (neigh->ce_mask & NEIGH_ATTR_PROBES)
483 dp_dump_line(p, line++, "NEIGH_PROBES=%u\n",
486 if (neigh->ce_mask & NEIGH_ATTR_TYPE)
487 dp_dump_line(p, line++, "NEIGH_TYPE=%s\n",
488 nl_rtntype2str(neigh->n_type, buf, sizeof(buf)));
490 rtnl_neigh_flags2str(neigh->n_flags, buf, sizeof(buf));
492 dp_dump_line(p, line++, "NEIGH_FLAGS=%s\n", buf);
494 rtnl_neigh_state2str(neigh->n_state, buf, sizeof(buf));
496 dp_dump_line(p, line++, "NEIGH_STATE=%s\n", buf);
502 * @name Neighbour Object Allocation/Freeage
506 struct rtnl_neigh *rtnl_neigh_alloc(void)
508 return (struct rtnl_neigh *) nl_object_alloc(&neigh_obj_ops);
511 void rtnl_neigh_put(struct rtnl_neigh *neigh)
513 nl_object_put((struct nl_object *) neigh);
519 * @name Neighbour Cache Managament
524 * Build a neighbour cache including all neighbours currently configured in the kernel.
525 * @arg handle netlink handle
527 * Allocates a new neighbour cache, initializes it properly and updates it
528 * to include all neighbours currently configured in the kernel.
530 * @note The caller is responsible for destroying and freeing the
531 * cache after using it.
532 * @return The new cache or NULL if an error occured.
534 struct nl_cache *rtnl_neigh_alloc_cache(struct nl_handle *handle)
536 struct nl_cache *cache;
538 cache = nl_cache_alloc(&rtnl_neigh_ops);
542 if (handle && nl_cache_refill(handle, cache) < 0) {
543 nl_cache_free(cache);
547 NL_DBG(2, "Returning new cache %p\n", cache);
553 * Look up a neighbour by interface index and destination address
554 * @arg cache neighbour cache
555 * @arg ifindex interface index the neighbour is on
556 * @arg dst destination address of the neighbour
557 * @return neighbour handle or NULL if no match was found.
559 struct rtnl_neigh * rtnl_neigh_get(struct nl_cache *cache, int ifindex,
562 struct rtnl_neigh *neigh;
564 nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
565 if (neigh->n_ifindex == ifindex &&
566 !nl_addr_cmp(neigh->n_dst, dst)) {
567 nl_object_get((struct nl_object *) neigh);
578 * @name Neighbour Addition
582 static struct nl_msg * build_neigh_msg(struct rtnl_neigh *tmpl, int cmd,
586 struct ndmsg nhdr = {
587 .ndm_ifindex = tmpl->n_ifindex,
588 .ndm_family = nl_addr_get_family(tmpl->n_dst),
589 .ndm_state = NUD_PERMANENT,
592 if (tmpl->ce_mask & NEIGH_ATTR_STATE)
593 nhdr.ndm_state = tmpl->n_state;
595 msg = nlmsg_alloc_simple(cmd, flags);
599 if (nlmsg_append(msg, &nhdr, sizeof(nhdr), NLMSG_ALIGNTO) < 0)
600 goto nla_put_failure;
602 NLA_PUT_ADDR(msg, NDA_DST, tmpl->n_dst);
604 if (tmpl->ce_mask & NEIGH_ATTR_LLADDR)
605 NLA_PUT_ADDR(msg, NDA_LLADDR, tmpl->n_lladdr);
615 * Build netlink request message to add a new neighbour
616 * @arg tmpl template with data of new neighbour
617 * @arg flags additional netlink message flags
619 * Builds a new netlink message requesting a addition of a new
620 * neighbour. The netlink message header isn't fully equipped with
621 * all relevant fields and must thus be sent out via nl_send_auto_complete()
622 * or supplemented as needed. \a tmpl must contain the attributes of the new
623 * neighbour set via \c rtnl_neigh_set_* functions.
625 * The following attributes must be set in the template:
626 * - Interface index (rtnl_neigh_set_ifindex())
627 * - State (rtnl_neigh_set_state())
628 * - Destination address (rtnl_neigh_set_dst())
629 * - Link layer address (rtnl_neigh_set_lladdr())
631 * @return The netlink message
633 struct nl_msg * rtnl_neigh_build_add_request(struct rtnl_neigh *tmpl, int flags)
635 return build_neigh_msg(tmpl, RTM_NEWNEIGH, NLM_F_CREATE | flags);
639 * Add a new neighbour
640 * @arg handle netlink handle
641 * @arg tmpl template with requested changes
642 * @arg flags additional netlink message flags
644 * Builds a netlink message by calling rtnl_neigh_build_add_request(),
645 * sends the request to the kernel and waits for the next ACK to be
646 * received and thus blocks until the request has been fullfilled.
648 * The following attributes must be set in the template:
649 * - Interface index (rtnl_neigh_set_ifindex())
650 * - State (rtnl_neigh_set_state())
651 * - Destination address (rtnl_neigh_set_dst())
652 * - Link layer address (rtnl_neigh_set_lladdr())
654 * @return 0 on sucess or a negative error if an error occured.
656 int rtnl_neigh_add(struct nl_handle *handle, struct rtnl_neigh *tmpl, int flags)
661 msg = rtnl_neigh_build_add_request(tmpl, flags);
663 return nl_errno(ENOMEM);
665 err = nl_send_auto_complete(handle, msg);
670 return nl_wait_for_ack(handle);
676 * @name Neighbour Deletion
681 * Build a netlink request message to delete a neighbour
682 * @arg neigh neighbour to delete
683 * @arg flags additional netlink message flags
685 * Builds a new netlink message requesting a deletion of a neighbour.
686 * The netlink message header isn't fully equipped with all relevant
687 * fields and must thus be sent out via nl_send_auto_complete()
688 * or supplemented as needed. \a neigh must point to an existing
691 * @return The netlink message
693 struct nl_msg *rtnl_neigh_build_delete_request(struct rtnl_neigh *neigh,
696 return build_neigh_msg(neigh, RTM_DELNEIGH, flags);
701 * @arg handle netlink handle
702 * @arg neigh neighbour to delete
703 * @arg flags additional netlink message flags
705 * Builds a netlink message by calling rtnl_neigh_build_delete_request(),
706 * sends the request to the kernel and waits for the next ACK to be
707 * received and thus blocks until the request has been fullfilled.
709 * @return 0 on sucess or a negative error if an error occured.
711 int rtnl_neigh_delete(struct nl_handle *handle, struct rtnl_neigh *neigh,
717 msg = rtnl_neigh_build_delete_request(neigh, flags);
719 return nl_errno(ENOMEM);
721 err = nl_send_auto_complete(handle, msg);
726 return nl_wait_for_ack(handle);
732 * @name Neighbour Modification
737 * Build a netlink request message to change neighbour attributes
738 * @arg neigh the neighbour to change
739 * @arg flags additional netlink message flags
741 * Builds a new netlink message requesting a change of a neigh
742 * attributes. The netlink message header isn't fully equipped with
743 * all relevant fields and must thus be sent out via nl_send_auto_complete()
744 * or supplemented as needed.
746 * @return The netlink message
747 * @note Not all attributes can be changed, see
748 * \ref neigh_changeable "Changeable Attributes" for a list.
750 struct nl_msg *rtnl_neigh_build_change_request(struct rtnl_neigh *neigh,
753 return build_neigh_msg(neigh, RTM_NEWNEIGH, NLM_F_REPLACE | flags);
757 * Change neighbour attributes
758 * @arg handle netlink handle
759 * @arg neigh neighbour to be changed
760 * @arg flags additional netlink message flags
762 * Builds a netlink message by calling rtnl_neigh_build_change_request(),
763 * sends the request to the kernel and waits for the next ACK to be
764 * received and thus blocks until the request has been fullfilled.
766 * @return 0 on sucess or a negative error if an error occured.
767 * @note Not all attributes can be changed, see
768 * \ref neigh_changeable "Changeable Attributes" for a list.
770 int rtnl_neigh_change(struct nl_handle *handle, struct rtnl_neigh *neigh,
776 msg = rtnl_neigh_build_change_request(neigh, flags);
778 return nl_errno(ENOMEM);
780 err = nl_send_auto_complete(handle, msg);
785 return nl_wait_for_ack(handle);
791 * @name Neighbour States Translations
795 static struct trans_tbl neigh_states[] = {
796 __ADD(NUD_INCOMPLETE, incomplete)
797 __ADD(NUD_REACHABLE, reachable)
798 __ADD(NUD_STALE, stale)
799 __ADD(NUD_DELAY, delay)
800 __ADD(NUD_PROBE, probe)
801 __ADD(NUD_FAILED, failed)
802 __ADD(NUD_NOARP, norarp)
803 __ADD(NUD_PERMANENT, permanent)
806 char * rtnl_neigh_state2str(int state, char *buf, size_t len)
808 return __flags2str(state, buf, len, neigh_states,
809 ARRAY_SIZE(neigh_states));
812 int rtnl_neigh_str2state(const char *name)
814 return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
820 * @name Neighbour Flags Translations
824 static struct trans_tbl neigh_flags[] = {
825 __ADD(NTF_PROXY, proxy)
826 __ADD(NTF_ROUTER, router)
829 char * rtnl_neigh_flags2str(int flags, char *buf, size_t len)
831 return __flags2str(flags, buf, len, neigh_flags,
832 ARRAY_SIZE(neigh_flags));
835 int rtnl_neigh_str2flag(const char *name)
837 return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
847 void rtnl_neigh_set_state(struct rtnl_neigh *neigh, int state)
849 neigh->n_state_mask |= state;
850 neigh->n_state |= state;
851 neigh->ce_mask |= NEIGH_ATTR_STATE;
854 int rtnl_neigh_get_state(struct rtnl_neigh *neigh)
856 if (neigh->ce_mask & NEIGH_ATTR_STATE)
857 return neigh->n_state;
862 void rtnl_neigh_unset_state(struct rtnl_neigh *neigh, int state)
864 neigh->n_state_mask |= state;
865 neigh->n_state &= ~state;
866 neigh->ce_mask |= NEIGH_ATTR_STATE;
869 void rtnl_neigh_set_flags(struct rtnl_neigh *neigh, unsigned int flags)
871 neigh->n_flag_mask |= flags;
872 neigh->n_flags |= flags;
873 neigh->ce_mask |= NEIGH_ATTR_FLAGS;
876 unsigned int rtnl_neigh_get_flags(struct rtnl_neigh *neigh)
878 return neigh->n_flags;
881 void rtnl_neigh_unset_flags(struct rtnl_neigh *neigh, unsigned int flags)
883 neigh->n_flag_mask |= flags;
884 neigh->n_flags &= ~flags;
885 neigh->ce_mask |= NEIGH_ATTR_FLAGS;
888 void rtnl_neigh_set_ifindex(struct rtnl_neigh *neigh, int ifindex)
890 neigh->n_ifindex = ifindex;
891 neigh->ce_mask |= NEIGH_ATTR_IFINDEX;
894 int rtnl_neigh_get_ifindex(struct rtnl_neigh *neigh)
896 if (neigh->ce_mask & NEIGH_ATTR_IFINDEX)
897 return neigh->n_ifindex;
899 return RTNL_LINK_NOT_FOUND;
902 static inline int __assign_addr(struct rtnl_neigh *neigh, struct nl_addr **pos,
903 struct nl_addr *new, int flag, int nocheck)
906 if (neigh->ce_mask & NEIGH_ATTR_FAMILY) {
907 if (new->a_family != neigh->n_family)
908 return nl_error(EINVAL,
909 "Address family mismatch");
911 neigh->n_family = new->a_family;
912 neigh->ce_mask |= NEIGH_ATTR_FAMILY;
922 neigh->ce_mask |= flag;
927 void rtnl_neigh_set_lladdr(struct rtnl_neigh *neigh, struct nl_addr *addr)
929 __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
932 struct nl_addr *rtnl_neigh_get_lladdr(struct rtnl_neigh *neigh)
934 if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
935 return neigh->n_lladdr;
940 int rtnl_neigh_set_dst(struct rtnl_neigh *neigh, struct nl_addr *addr)
942 return __assign_addr(neigh, &neigh->n_dst, addr,
946 struct nl_addr *rtnl_neigh_get_dst(struct rtnl_neigh *neigh)
948 if (neigh->ce_mask & NEIGH_ATTR_DST)
954 void rtnl_neigh_set_family(struct rtnl_neigh *neigh, int family)
956 neigh->n_family = family;
957 neigh->ce_mask |= NEIGH_ATTR_FAMILY;
960 void rtnl_neigh_set_type(struct rtnl_neigh *neigh, int type)
962 neigh->n_type = type;
963 neigh->ce_mask = NEIGH_ATTR_TYPE;
966 int rtnl_neigh_get_type(struct rtnl_neigh *neigh)
968 if (neigh->ce_mask & NEIGH_ATTR_TYPE)
969 return neigh->n_type;
976 static struct nl_object_ops neigh_obj_ops = {
977 .oo_name = "route/neigh",
978 .oo_size = sizeof(struct rtnl_neigh),
979 .oo_free_data = neigh_free_data,
980 .oo_clone = neigh_clone,
981 .oo_dump[NL_DUMP_BRIEF] = neigh_dump_brief,
982 .oo_dump[NL_DUMP_FULL] = neigh_dump_full,
983 .oo_dump[NL_DUMP_STATS] = neigh_dump_stats,
984 .oo_dump[NL_DUMP_XML] = neigh_dump_xml,
985 .oo_dump[NL_DUMP_ENV] = neigh_dump_env,
986 .oo_compare = neigh_compare,
987 .oo_attrs2str = neigh_attrs2str,
988 .oo_id_attrs = (NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY),
991 static struct nl_af_group neigh_groups[] = {
992 { AF_UNSPEC, RTNLGRP_NEIGH },
993 { END_OF_GROUP_LIST },
996 static struct nl_cache_ops rtnl_neigh_ops = {
997 .co_name = "route/neigh",
998 .co_hdrsize = sizeof(struct ndmsg),
1000 { RTM_NEWNEIGH, NL_ACT_NEW, "new" },
1001 { RTM_DELNEIGH, NL_ACT_DEL, "del" },
1002 { RTM_GETNEIGH, NL_ACT_GET, "get" },
1003 END_OF_MSGTYPES_LIST,
1005 .co_protocol = NETLINK_ROUTE,
1006 .co_groups = neigh_groups,
1007 .co_request_update = neigh_request_update,
1008 .co_msg_parser = neigh_msg_parser,
1009 .co_obj_ops = &neigh_obj_ops,
1012 static void __init neigh_init(void)
1014 nl_cache_mngt_register(&rtnl_neigh_ops);
1017 static void __exit neigh_exit(void)
1019 nl_cache_mngt_unregister(&rtnl_neigh_ops);