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>
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
172 static struct nl_cache_ops rtnl_neigh_ops;
173 static struct nl_object_ops neigh_obj_ops;
176 static void neigh_free_data(
struct nl_object *c)
178 struct rtnl_neigh *neigh = nl_object_priv(c);
187 static int neigh_clone(
struct nl_object *_dst,
struct nl_object *_src)
189 struct rtnl_neigh *dst = nl_object_priv(_dst);
190 struct rtnl_neigh *src = nl_object_priv(_src);
203 static void neigh_keygen(
struct nl_object *obj, uint32_t *hashkey,
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 {
213 } __attribute__((packed)) *nkey;
215 char buf[INET6_ADDRSTRLEN+5];
218 if (neigh->n_family == AF_BRIDGE) {
220 addr = neigh->n_lladdr;
221 }
else if (neigh->n_dst) {
225 nkey_sz =
sizeof(*nkey);
229 nkey = calloc(1, nkey_sz);
234 nkey->n_family = neigh->n_family;
235 if (neigh->n_family == AF_BRIDGE)
236 nkey->n_ifindex = neigh->n_master;
238 nkey->n_ifindex = neigh->n_ifindex;
244 *hashkey = nl_hash(nkey, nkey_sz, 0) % table_sz;
246 NL_DBG(5,
"neigh %p key (fam %d dev %d addr %s) keysz %d hash 0x%x\n",
247 neigh, nkey->n_family, nkey->n_ifindex,
256 static int neigh_compare(
struct nl_object *_a,
struct nl_object *_b,
257 uint32_t attrs,
int flags)
259 struct rtnl_neigh *a = (
struct rtnl_neigh *) _a;
260 struct rtnl_neigh *b = (
struct rtnl_neigh *) _b;
263 #define NEIGH_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGH_ATTR_##ATTR, a, b, EXPR)
265 diff |= NEIGH_DIFF(IFINDEX, a->n_ifindex != b->n_ifindex);
266 diff |= NEIGH_DIFF(FAMILY, a->n_family != b->n_family);
267 diff |= NEIGH_DIFF(TYPE, a->n_type != b->n_type);
268 diff |= NEIGH_DIFF(LLADDR,
nl_addr_cmp(a->n_lladdr, b->n_lladdr));
269 diff |= NEIGH_DIFF(DST,
nl_addr_cmp(a->n_dst, b->n_dst));
270 diff |= NEIGH_DIFF(MASTER, a->n_master != b->n_master);
272 if (flags & LOOSE_COMPARISON) {
273 diff |= NEIGH_DIFF(STATE,
274 (a->n_state ^ b->n_state) & b->n_state_mask);
275 diff |= NEIGH_DIFF(FLAGS,
276 (a->n_flags ^ b->n_flags) & b->n_flag_mask);
278 diff |= NEIGH_DIFF(STATE, a->n_state != b->n_state);
279 diff |= NEIGH_DIFF(FLAGS, a->n_flags != b->n_flags);
287 static const struct trans_tbl neigh_attrs[] = {
288 __ADD(NEIGH_ATTR_FLAGS, flags)
289 __ADD(NEIGH_ATTR_STATE, state)
290 __ADD(NEIGH_ATTR_LLADDR, lladdr)
291 __ADD(NEIGH_ATTR_DST, dst)
292 __ADD(NEIGH_ATTR_CACHEINFO, cacheinfo)
293 __ADD(NEIGH_ATTR_IFINDEX, ifindex)
294 __ADD(NEIGH_ATTR_FAMILY, family)
295 __ADD(NEIGH_ATTR_TYPE, type)
296 __ADD(NEIGH_ATTR_PROBES, probes)
299 static
char *neigh_attrs2str(
int attrs,
char *buf,
size_t len)
301 return __flags2str(attrs, buf, len, neigh_attrs,
302 ARRAY_SIZE(neigh_attrs));
305 static uint32_t neigh_id_attrs_get(
struct nl_object *obj)
307 struct rtnl_neigh *neigh = (
struct rtnl_neigh *)obj;
309 if (neigh->n_family == AF_BRIDGE)
310 return (NEIGH_ATTR_LLADDR | NEIGH_ATTR_FAMILY | NEIGH_ATTR_MASTER);
312 return (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY);
315 static struct nla_policy neigh_policy[NDA_MAX+1] = {
316 [NDA_CACHEINFO] = { .
minlen =
sizeof(
struct nda_cacheinfo) },
317 [NDA_PROBES] = { .type =
NLA_U32 },
320 static int neigh_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
321 struct nlmsghdr *n,
struct nl_parser_param *pp)
323 struct rtnl_neigh *neigh;
326 if ((err = rtnl_neigh_parse(n, &neigh)) < 0)
329 err = pp->pp_cb((
struct nl_object *) neigh, pp);
331 rtnl_neigh_put(neigh);
336 int rtnl_neigh_parse(
struct nlmsghdr *n,
struct rtnl_neigh **result)
338 struct rtnl_neigh *neigh;
339 struct nlattr *tb[NDA_MAX + 1];
343 neigh = rtnl_neigh_alloc();
349 neigh->ce_msgtype = n->nlmsg_type;
352 err =
nlmsg_parse(n,
sizeof(*nm), tb, NDA_MAX, neigh_policy);
356 neigh->n_family = nm->ndm_family;
357 neigh->n_ifindex = nm->ndm_ifindex;
358 neigh->n_state = nm->ndm_state;
359 neigh->n_flags = nm->ndm_flags;
360 neigh->n_type = nm->ndm_type;
362 neigh->ce_mask |= (NEIGH_ATTR_FAMILY | NEIGH_ATTR_IFINDEX |
363 NEIGH_ATTR_STATE | NEIGH_ATTR_FLAGS |
366 if (tb[NDA_LLADDR]) {
368 if (!neigh->n_lladdr) {
374 neigh->ce_mask |= NEIGH_ATTR_LLADDR;
383 neigh->ce_mask |= NEIGH_ATTR_DST;
386 if (tb[NDA_CACHEINFO]) {
387 struct nda_cacheinfo *ci =
nla_data(tb[NDA_CACHEINFO]);
389 neigh->n_cacheinfo.nci_confirmed = ci->ndm_confirmed;
390 neigh->n_cacheinfo.nci_used = ci->ndm_used;
391 neigh->n_cacheinfo.nci_updated = ci->ndm_updated;
392 neigh->n_cacheinfo.nci_refcnt = ci->ndm_refcnt;
394 neigh->ce_mask |= NEIGH_ATTR_CACHEINFO;
397 if (tb[NDA_PROBES]) {
399 neigh->ce_mask |= NEIGH_ATTR_PROBES;
405 if (neigh->n_family == AF_BRIDGE) {
411 neigh->n_master = link->l_master;
413 neigh->ce_mask |= NEIGH_ATTR_MASTER;
416 nl_cache_put(lcache);
424 rtnl_neigh_put(neigh);
428 static int neigh_request_update(
struct nl_cache *c,
struct nl_sock *h)
430 int family = c->c_iarg1;
436 static void neigh_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
438 char dst[INET6_ADDRSTRLEN+5], lladdr[INET6_ADDRSTRLEN+5];
439 struct rtnl_neigh *n = (
struct rtnl_neigh *) a;
440 struct nl_cache *link_cache;
441 char state[128], flags[64];
445 if (n->n_family != AF_BRIDGE)
446 nl_dump_line(p,
"%s ",
nl_addr2str(n->n_dst, dst,
sizeof(dst)));
451 state,
sizeof(state)));
453 nl_dump(p,
"dev %d ", n->n_ifindex);
455 if (n->ce_mask & NEIGH_ATTR_LLADDR)
459 rtnl_neigh_state2str(n->n_state, state,
sizeof(state));
460 rtnl_neigh_flags2str(n->n_flags, flags,
sizeof(flags));
465 nl_dump(p,
"%s%s", state[0] ?
"," :
"<", flags);
466 if (state[0] || flags[0])
471 nl_cache_put(link_cache);
474 static void neigh_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
477 struct rtnl_neigh *n = (
struct rtnl_neigh *) a;
480 neigh_dump_line(a, p);
482 nl_dump_line(p,
" refcnt %u type %s confirmed %u used "
484 n->n_cacheinfo.nci_refcnt,
485 nl_rtntype2str(n->n_type, rtn_type,
sizeof(rtn_type)),
486 n->n_cacheinfo.nci_confirmed/hz,
487 n->n_cacheinfo.nci_used/hz, n->n_cacheinfo.nci_updated/hz);
490 static void neigh_dump_stats(
struct nl_object *a,
struct nl_dump_params *p)
492 neigh_dump_details(a, p);
500 struct rtnl_neigh *rtnl_neigh_alloc(
void)
505 void rtnl_neigh_put(
struct rtnl_neigh *neigh)
543 struct rtnl_neigh *neigh;
545 nl_list_for_each_entry(neigh, &cache->c_items, ce_list) {
546 if (neigh->n_ifindex == ifindex &&
563 static int build_neigh_msg(
struct rtnl_neigh *tmpl,
int cmd,
int flags,
564 struct nl_msg **result)
567 struct ndmsg nhdr = {
568 .ndm_ifindex = tmpl->n_ifindex,
569 .ndm_state = NUD_PERMANENT,
572 if (tmpl->n_family != AF_BRIDGE) {
573 if (!(tmpl->ce_mask & NEIGH_ATTR_DST))
574 return -NLE_MISSING_ATTR;
578 nhdr.ndm_family = AF_BRIDGE;
580 if (tmpl->ce_mask & NEIGH_ATTR_FLAGS)
581 nhdr.ndm_flags = tmpl->n_flags;
583 if (tmpl->ce_mask & NEIGH_ATTR_STATE)
584 nhdr.ndm_state = tmpl->n_state;
590 if (
nlmsg_append(msg, &nhdr,
sizeof(nhdr), NLMSG_ALIGNTO) < 0)
591 goto nla_put_failure;
593 if (tmpl->n_family != AF_BRIDGE)
596 if (tmpl->ce_mask & NEIGH_ATTR_LLADDR)
628 struct nl_msg **result)
630 return build_neigh_msg(tmpl, RTM_NEWNEIGH, flags, result);
664 return wait_for_ack(sk);
689 struct nl_msg **result)
691 return build_neigh_msg(neigh, RTM_DELNEIGH, flags, result);
720 return wait_for_ack(sk);
730 static const struct trans_tbl neigh_states[] = {
731 __ADD(NUD_INCOMPLETE, incomplete)
732 __ADD(NUD_REACHABLE, reachable)
733 __ADD(NUD_STALE, stale)
734 __ADD(NUD_DELAY, delay)
735 __ADD(NUD_PROBE, probe)
736 __ADD(NUD_FAILED, failed)
737 __ADD(NUD_NOARP, norarp)
738 __ADD(NUD_PERMANENT, permanent)
741 char * rtnl_neigh_state2str(
int state,
char *buf,
size_t len)
743 return __flags2str(state, buf, len, neigh_states,
744 ARRAY_SIZE(neigh_states));
747 int rtnl_neigh_str2state(
const char *name)
749 return __str2type(name, neigh_states, ARRAY_SIZE(neigh_states));
759 static const struct trans_tbl neigh_flags[] = {
761 __ADD(NTF_PROXY, proxy)
762 __ADD(NTF_ROUTER, router)
765 char * rtnl_neigh_flags2str(
int flags,
char *buf,
size_t len)
767 return __flags2str(flags, buf, len, neigh_flags,
768 ARRAY_SIZE(neigh_flags));
771 int rtnl_neigh_str2flag(
const char *name)
773 return __str2type(name, neigh_flags, ARRAY_SIZE(neigh_flags));
783 void rtnl_neigh_set_state(
struct rtnl_neigh *neigh,
int state)
785 neigh->n_state_mask |= state;
786 neigh->n_state |= state;
787 neigh->ce_mask |= NEIGH_ATTR_STATE;
790 int rtnl_neigh_get_state(
struct rtnl_neigh *neigh)
792 if (neigh->ce_mask & NEIGH_ATTR_STATE)
793 return neigh->n_state;
798 void rtnl_neigh_unset_state(
struct rtnl_neigh *neigh,
int state)
800 neigh->n_state_mask |= state;
801 neigh->n_state &= ~state;
802 neigh->ce_mask |= NEIGH_ATTR_STATE;
805 void rtnl_neigh_set_flags(
struct rtnl_neigh *neigh,
unsigned int flags)
807 neigh->n_flag_mask |= flags;
808 neigh->n_flags |= flags;
809 neigh->ce_mask |= NEIGH_ATTR_FLAGS;
812 unsigned int rtnl_neigh_get_flags(
struct rtnl_neigh *neigh)
814 return neigh->n_flags;
817 void rtnl_neigh_unset_flags(
struct rtnl_neigh *neigh,
unsigned int flags)
819 neigh->n_flag_mask |= flags;
820 neigh->n_flags &= ~flags;
821 neigh->ce_mask |= NEIGH_ATTR_FLAGS;
824 void rtnl_neigh_set_ifindex(
struct rtnl_neigh *neigh,
int ifindex)
826 neigh->n_ifindex = ifindex;
827 neigh->ce_mask |= NEIGH_ATTR_IFINDEX;
830 int rtnl_neigh_get_ifindex(
struct rtnl_neigh *neigh)
832 return neigh->n_ifindex;
835 static inline int __assign_addr(
struct rtnl_neigh *neigh,
struct nl_addr **pos,
836 struct nl_addr *
new,
int flag,
int nocheck)
839 if (neigh->ce_mask & NEIGH_ATTR_FAMILY) {
840 if (new->a_family != neigh->n_family)
841 return -NLE_AF_MISMATCH;
843 neigh->n_family =
new->a_family;
844 neigh->ce_mask |= NEIGH_ATTR_FAMILY;
854 neigh->ce_mask |= flag;
859 void rtnl_neigh_set_lladdr(
struct rtnl_neigh *neigh,
struct nl_addr *addr)
861 __assign_addr(neigh, &neigh->n_lladdr, addr, NEIGH_ATTR_LLADDR, 1);
864 struct nl_addr *rtnl_neigh_get_lladdr(
struct rtnl_neigh *neigh)
866 if (neigh->ce_mask & NEIGH_ATTR_LLADDR)
867 return neigh->n_lladdr;
872 int rtnl_neigh_set_dst(
struct rtnl_neigh *neigh,
struct nl_addr *addr)
874 return __assign_addr(neigh, &neigh->n_dst, addr,
878 struct nl_addr *rtnl_neigh_get_dst(
struct rtnl_neigh *neigh)
880 if (neigh->ce_mask & NEIGH_ATTR_DST)
886 void rtnl_neigh_set_family(
struct rtnl_neigh *neigh,
int family)
888 neigh->n_family = family;
889 neigh->ce_mask |= NEIGH_ATTR_FAMILY;
892 int rtnl_neigh_get_family(
struct rtnl_neigh *neigh)
894 return neigh->n_family;
897 void rtnl_neigh_set_type(
struct rtnl_neigh *neigh,
int type)
899 neigh->n_type = type;
900 neigh->ce_mask = NEIGH_ATTR_TYPE;
903 int rtnl_neigh_get_type(
struct rtnl_neigh *neigh)
905 if (neigh->ce_mask & NEIGH_ATTR_TYPE)
906 return neigh->n_type;
913 static struct nl_object_ops neigh_obj_ops = {
914 .oo_name =
"route/neigh",
915 .oo_size =
sizeof(
struct rtnl_neigh),
916 .oo_free_data = neigh_free_data,
917 .oo_clone = neigh_clone,
923 .oo_compare = neigh_compare,
924 .oo_keygen = neigh_keygen,
925 .oo_attrs2str = neigh_attrs2str,
926 .oo_id_attrs = (NEIGH_ATTR_IFINDEX | NEIGH_ATTR_DST | NEIGH_ATTR_FAMILY),
927 .oo_id_attrs_get = neigh_id_attrs_get
930 static struct nl_af_group neigh_groups[] = {
931 { AF_UNSPEC, RTNLGRP_NEIGH },
932 { AF_BRIDGE, RTNLGRP_NEIGH },
933 { END_OF_GROUP_LIST },
936 static struct nl_cache_ops rtnl_neigh_ops = {
937 .co_name =
"route/neigh",
938 .co_hdrsize =
sizeof(
struct ndmsg),
940 { RTM_NEWNEIGH, NL_ACT_NEW,
"new" },
941 { RTM_DELNEIGH, NL_ACT_DEL,
"del" },
942 { RTM_GETNEIGH, NL_ACT_GET,
"get" },
943 END_OF_MSGTYPES_LIST,
945 .co_protocol = NETLINK_ROUTE,
946 .co_groups = neigh_groups,
947 .co_request_update = neigh_request_update,
948 .co_msg_parser = neigh_msg_parser,
949 .co_obj_ops = &neigh_obj_ops,
952 static void __init neigh_init(
void)
957 static void __exit neigh_exit(
void)