libnl  3.4.0
addr.c
1 /*
2  * lib/route/addr.c Addresses
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-2012 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2003-2006 Baruch Even <baruch@ev-en.org>,
11  * Mediatrix Telecom, inc. <ericb@mediatrix.com>
12  */
13 
14 /**
15  * @ingroup rtnl
16  * @defgroup rtaddr Addresses
17  * @brief
18  *
19  * @note The maximum size of an address label is IFNAMSIZ.
20  *
21  * @note The address may not contain a prefix length if the peer address
22  * has been specified already.
23  *
24  * @par 1) Address Addition
25  * @code
26  * // Allocate an empty address object to be filled out with the attributes
27  * // of the new address.
28  * struct rtnl_addr *addr = rtnl_addr_alloc();
29  *
30  * // Fill out the mandatory attributes of the new address. Setting the
31  * // local address will automatically set the address family and the
32  * // prefix length to the correct values.
33  * rtnl_addr_set_ifindex(addr, ifindex);
34  * rtnl_addr_set_local(addr, local_addr);
35  *
36  * // The label of the address can be specified, currently only supported
37  * // by IPv4 and DECnet.
38  * rtnl_addr_set_label(addr, "mylabel");
39  *
40  * // The peer address can be specified if necessary, in either case a peer
41  * // address will be sent to the kernel in order to fullfil the interface
42  * // requirements. If none is set, it will equal the local address.
43  * // Note: Real peer addresses are only supported by IPv4 for now.
44  * rtnl_addr_set_peer(addr, peer_addr);
45  *
46  * // In case you want to have the address have a scope other than global
47  * // it may be overwritten using rtnl_addr_set_scope(). The scope currently
48  * // cannot be set for IPv6 addresses.
49  * rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
50  *
51  * // Broadcast address may be specified using the relevant
52  * // functions, the address family will be verified if one of the other
53  * // addresses has been set already. Currently only works for IPv4.
54  * rtnl_addr_set_broadcast(addr, broadcast_addr);
55  *
56  * // Build the netlink message and send it to the kernel, the operation will
57  * // block until the operation has been completed. Alternatively the required
58  * // netlink message can be built using rtnl_addr_build_add_request() to be
59  * // sent out using nl_send_auto_complete().
60  * rtnl_addr_add(sk, addr, 0);
61  *
62  * // Free the memory
63  * rtnl_addr_put(addr);
64  * @endcode
65  *
66  * @par 2) Address Deletion
67  * @code
68  * // Allocate an empty address object to be filled out with the attributes
69  * // matching the address to be deleted. Alternatively a fully equipped
70  * // address object out of a cache can be used instead.
71  * struct rtnl_addr *addr = rtnl_addr_alloc();
72  *
73  * // The only mandatory parameter besides the address family is the interface
74  * // index the address is on, i.e. leaving out all other parameters will
75  * // result in all addresses of the specified address family interface tuple
76  * // to be deleted.
77  * rtnl_addr_set_ifindex(addr, ifindex);
78  *
79  * // Specyfing the address family manually is only required if neither the
80  * // local nor peer address have been specified.
81  * rtnl_addr_set_family(addr, AF_INET);
82  *
83  * // Specyfing the local address is optional but the best choice to delete
84  * // specific addresses.
85  * rtnl_addr_set_local(addr, local_addr);
86  *
87  * // The label of the address can be specified, currently only supported
88  * // by IPv4 and DECnet.
89  * rtnl_addr_set_label(addr, "mylabel");
90  *
91  * // The peer address can be specified if necessary, in either case a peer
92  * // address will be sent to the kernel in order to fullfil the interface
93  * // requirements. If none is set, it will equal the local address.
94  * // Note: Real peer addresses are only supported by IPv4 for now.
95  * rtnl_addr_set_peer(addr, peer_addr);
96  *
97  * // Build the netlink message and send it to the kernel, the operation will
98  * // block until the operation has been completed. Alternatively the required
99  * // netlink message can be built using rtnl_addr_build_delete_request()
100  * // to be sent out using nl_send_auto_complete().
101  * rtnl_addr_delete(sk, addr, 0);
102  *
103  * // Free the memory
104  * rtnl_addr_put(addr);
105  * @endcode
106  * @{
107  */
108 
109 #include <netlink-private/netlink.h>
110 #include <netlink/netlink.h>
111 #include <netlink/route/rtnl.h>
112 #include <netlink/route/addr.h>
113 #include <netlink/route/route.h>
114 #include <netlink/route/link.h>
115 #include <netlink/utils.h>
116 
117 /** @cond SKIP */
118 #define ADDR_ATTR_FAMILY 0x0001
119 #define ADDR_ATTR_PREFIXLEN 0x0002
120 #define ADDR_ATTR_FLAGS 0x0004
121 #define ADDR_ATTR_SCOPE 0x0008
122 #define ADDR_ATTR_IFINDEX 0x0010
123 #define ADDR_ATTR_LABEL 0x0020
124 #define ADDR_ATTR_CACHEINFO 0x0040
125 #define ADDR_ATTR_PEER 0x0080
126 #define ADDR_ATTR_LOCAL 0x0100
127 #define ADDR_ATTR_BROADCAST 0x0200
128 #define ADDR_ATTR_MULTICAST 0x0400
129 #define ADDR_ATTR_ANYCAST 0x0800
130 
131 static struct nl_cache_ops rtnl_addr_ops;
132 static struct nl_object_ops addr_obj_ops;
133 /** @endcond */
134 
135 static void addr_constructor(struct nl_object *obj)
136 {
137  struct rtnl_addr *addr = nl_object_priv(obj);
138 
139  addr->a_scope = RT_SCOPE_NOWHERE;
140 }
141 
142 static void addr_free_data(struct nl_object *obj)
143 {
144  struct rtnl_addr *addr = nl_object_priv(obj);
145 
146  if (!addr)
147  return;
148 
149  nl_addr_put(addr->a_peer);
150  nl_addr_put(addr->a_local);
151  nl_addr_put(addr->a_bcast);
152  nl_addr_put(addr->a_multicast);
153  nl_addr_put(addr->a_anycast);
154  rtnl_link_put(addr->a_link);
155 }
156 
157 static int addr_clone(struct nl_object *_dst, struct nl_object *_src)
158 {
159  struct rtnl_addr *dst = nl_object_priv(_dst);
160  struct rtnl_addr *src = nl_object_priv(_src);
161 
162  if (src->a_link) {
163  nl_object_get(OBJ_CAST(src->a_link));
164  dst->a_link = src->a_link;
165  }
166 
167  if (src->a_peer)
168  if (!(dst->a_peer = nl_addr_clone(src->a_peer)))
169  return -NLE_NOMEM;
170 
171  if (src->a_local)
172  if (!(dst->a_local = nl_addr_clone(src->a_local)))
173  return -NLE_NOMEM;
174 
175  if (src->a_bcast)
176  if (!(dst->a_bcast = nl_addr_clone(src->a_bcast)))
177  return -NLE_NOMEM;
178 
179  if (src->a_multicast)
180  if (!(dst->a_multicast = nl_addr_clone(src->a_multicast)))
181  return -NLE_NOMEM;
182 
183  if (src->a_anycast)
184  if (!(dst->a_anycast = nl_addr_clone(src->a_anycast)))
185  return -NLE_NOMEM;
186 
187  return 0;
188 }
189 
190 static struct nla_policy addr_policy[IFA_MAX+1] = {
191  [IFA_LABEL] = { .type = NLA_STRING,
192  .maxlen = IFNAMSIZ },
193  [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
194 };
195 
196 static int addr_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
197  struct nlmsghdr *nlh, struct nl_parser_param *pp)
198 {
199  struct rtnl_addr *addr;
200  struct ifaddrmsg *ifa;
201  struct nlattr *tb[IFA_MAX+1];
202  int err, family;
203  struct nl_cache *link_cache;
204  struct nl_addr *plen_addr = NULL;
205 
206  addr = rtnl_addr_alloc();
207  if (!addr)
208  return -NLE_NOMEM;
209 
210  addr->ce_msgtype = nlh->nlmsg_type;
211 
212  err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy);
213  if (err < 0)
214  goto errout;
215 
216  ifa = nlmsg_data(nlh);
217  addr->a_family = family = ifa->ifa_family;
218  addr->a_prefixlen = ifa->ifa_prefixlen;
219  addr->a_scope = ifa->ifa_scope;
220  addr->a_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
221  ifa->ifa_flags;
222  addr->a_ifindex = ifa->ifa_index;
223 
224  addr->ce_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN |
225  ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX);
226 
227  if (tb[IFA_LABEL]) {
228  nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ);
229  addr->ce_mask |= ADDR_ATTR_LABEL;
230  }
231 
232  /* IPv6 only */
233  if (tb[IFA_CACHEINFO]) {
234  struct ifa_cacheinfo *ca;
235 
236  ca = nla_data(tb[IFA_CACHEINFO]);
237  addr->a_cacheinfo.aci_prefered = ca->ifa_prefered;
238  addr->a_cacheinfo.aci_valid = ca->ifa_valid;
239  addr->a_cacheinfo.aci_cstamp = ca->cstamp;
240  addr->a_cacheinfo.aci_tstamp = ca->tstamp;
241  addr->ce_mask |= ADDR_ATTR_CACHEINFO;
242  }
243 
244  if (family == AF_INET) {
245  uint32_t null = 0;
246 
247  /* for IPv4/AF_INET, kernel always sets IFA_LOCAL and IFA_ADDRESS, unless it
248  * is effectively 0.0.0.0. */
249  if (tb[IFA_LOCAL])
250  addr->a_local = nl_addr_alloc_attr(tb[IFA_LOCAL], family);
251  else
252  addr->a_local = nl_addr_build(family, &null, sizeof (null));
253  if (!addr->a_local)
254  goto errout_nomem;
255  addr->ce_mask |= ADDR_ATTR_LOCAL;
256 
257  if (tb[IFA_ADDRESS])
258  addr->a_peer = nl_addr_alloc_attr(tb[IFA_ADDRESS], family);
259  else
260  addr->a_peer = nl_addr_build(family, &null, sizeof (null));
261  if (!addr->a_peer)
262  goto errout_nomem;
263 
264  if (!nl_addr_cmp (addr->a_local, addr->a_peer)) {
265  /* having IFA_ADDRESS equal to IFA_LOCAL does not really mean
266  * there is no peer. It means the peer is equal to the local address,
267  * which is the case for "normal" addresses.
268  *
269  * Still, clear the peer and pretend it is unset for backward
270  * compatibility. */
271  nl_addr_put(addr->a_peer);
272  addr->a_peer = NULL;
273  } else
274  addr->ce_mask |= ADDR_ATTR_PEER;
275 
276  plen_addr = addr->a_local;
277  } else {
278  if (tb[IFA_LOCAL]) {
279  addr->a_local = nl_addr_alloc_attr(tb[IFA_LOCAL], family);
280  if (!addr->a_local)
281  goto errout_nomem;
282  addr->ce_mask |= ADDR_ATTR_LOCAL;
283  plen_addr = addr->a_local;
284  }
285 
286  if (tb[IFA_ADDRESS]) {
287  struct nl_addr *a;
288 
289  a = nl_addr_alloc_attr(tb[IFA_ADDRESS], family);
290  if (!a)
291  goto errout_nomem;
292 
293  /* IPv6 sends the local address as IFA_ADDRESS with
294  * no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
295  * with IFA_ADDRESS being the peer address if they differ */
296  if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) {
297  nl_addr_put(addr->a_local);
298  addr->a_local = a;
299  addr->ce_mask |= ADDR_ATTR_LOCAL;
300  } else {
301  addr->a_peer = a;
302  addr->ce_mask |= ADDR_ATTR_PEER;
303  }
304 
305  plen_addr = a;
306  }
307  }
308 
309  if (plen_addr)
310  nl_addr_set_prefixlen(plen_addr, addr->a_prefixlen);
311 
312  /* IPv4 only */
313  if (tb[IFA_BROADCAST]) {
314  addr->a_bcast = nl_addr_alloc_attr(tb[IFA_BROADCAST], family);
315  if (!addr->a_bcast)
316  goto errout_nomem;
317 
318  addr->ce_mask |= ADDR_ATTR_BROADCAST;
319  }
320 
321  /* IPv6 only */
322  if (tb[IFA_MULTICAST]) {
323  addr->a_multicast = nl_addr_alloc_attr(tb[IFA_MULTICAST],
324  family);
325  if (!addr->a_multicast)
326  goto errout_nomem;
327 
328  addr->ce_mask |= ADDR_ATTR_MULTICAST;
329  }
330 
331  /* IPv6 only */
332  if (tb[IFA_ANYCAST]) {
333  addr->a_anycast = nl_addr_alloc_attr(tb[IFA_ANYCAST],
334  family);
335  if (!addr->a_anycast)
336  goto errout_nomem;
337 
338  addr->ce_mask |= ADDR_ATTR_ANYCAST;
339  }
340 
341  if ((link_cache = __nl_cache_mngt_require("route/link"))) {
342  struct rtnl_link *link;
343 
344  if ((link = rtnl_link_get(link_cache, addr->a_ifindex))) {
345  rtnl_addr_set_link(addr, link);
346 
347  /* rtnl_addr_set_link incs refcnt */
348  rtnl_link_put(link);
349  }
350  }
351 
352  err = pp->pp_cb((struct nl_object *) addr, pp);
353 errout:
354  rtnl_addr_put(addr);
355 
356  return err;
357 
358 errout_nomem:
359  err = -NLE_NOMEM;
360  goto errout;
361 }
362 
363 static int addr_request_update(struct nl_cache *cache, struct nl_sock *sk)
364 {
365  return nl_rtgen_request(sk, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP);
366 }
367 
368 static void addr_dump_line(struct nl_object *obj, struct nl_dump_params *p)
369 {
370  struct rtnl_addr *addr = (struct rtnl_addr *) obj;
371  struct nl_cache *link_cache;
372  char buf[128];
373 
374  link_cache = nl_cache_mngt_require_safe("route/link");
375 
376  if (addr->ce_mask & ADDR_ATTR_LOCAL)
377  nl_dump_line(p, "%s",
378  nl_addr2str(addr->a_local, buf, sizeof(buf)));
379  else
380  nl_dump_line(p, "none");
381 
382  if (addr->ce_mask & ADDR_ATTR_PEER)
383  nl_dump(p, " peer %s",
384  nl_addr2str(addr->a_peer, buf, sizeof(buf)));
385 
386  nl_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf)));
387 
388  if (link_cache)
389  nl_dump(p, "dev %s ",
390  rtnl_link_i2name(link_cache, addr->a_ifindex,
391  buf, sizeof(buf)));
392  else
393  nl_dump(p, "dev %d ", addr->a_ifindex);
394 
395  nl_dump(p, "scope %s",
396  rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
397 
398  rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
399  if (buf[0])
400  nl_dump(p, " <%s>", buf);
401 
402  nl_dump(p, "\n");
403 
404  if (link_cache)
405  nl_cache_put(link_cache);
406 }
407 
408 static void addr_dump_details(struct nl_object *obj, struct nl_dump_params *p)
409 {
410  struct rtnl_addr *addr = (struct rtnl_addr *) obj;
411  char buf[128];
412 
413  addr_dump_line(obj, p);
414 
415  if (addr->ce_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST |
416  ADDR_ATTR_MULTICAST)) {
417  nl_dump_line(p, " ");
418 
419  if (addr->ce_mask & ADDR_ATTR_LABEL)
420  nl_dump(p, " label %s", addr->a_label);
421 
422  if (addr->ce_mask & ADDR_ATTR_BROADCAST)
423  nl_dump(p, " broadcast %s",
424  nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
425 
426  if (addr->ce_mask & ADDR_ATTR_MULTICAST)
427  nl_dump(p, " multicast %s",
428  nl_addr2str(addr->a_multicast, buf,
429  sizeof(buf)));
430 
431  if (addr->ce_mask & ADDR_ATTR_ANYCAST)
432  nl_dump(p, " anycast %s",
433  nl_addr2str(addr->a_anycast, buf,
434  sizeof(buf)));
435 
436  nl_dump(p, "\n");
437  }
438 
439  if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
440  struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
441 
442  nl_dump_line(p, " valid-lifetime %s",
443  ci->aci_valid == 0xFFFFFFFFU ? "forever" :
444  nl_msec2str(ci->aci_valid * 1000,
445  buf, sizeof(buf)));
446 
447  nl_dump(p, " preferred-lifetime %s\n",
448  ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
449  nl_msec2str(ci->aci_prefered * 1000,
450  buf, sizeof(buf)));
451 
452  nl_dump_line(p, " created boot-time+%s ",
453  nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
454  buf, sizeof(buf)));
455 
456  nl_dump(p, "last-updated boot-time+%s\n",
457  nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
458  buf, sizeof(buf)));
459  }
460 }
461 
462 static void addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
463 {
464  addr_dump_details(obj, p);
465 }
466 
467 static uint32_t addr_id_attrs_get(struct nl_object *obj)
468 {
469  struct rtnl_addr *addr = (struct rtnl_addr *)obj;
470  uint32_t rv;
471 
472  switch (addr->a_family) {
473  case AF_INET:
474  rv = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
475  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN);
476  if (addr->a_peer)
477  rv |= ADDR_ATTR_PEER;
478  return rv;
479  case AF_INET6:
480  return (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
481  ADDR_ATTR_LOCAL);
482  default:
483  return (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
484  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN);
485  }
486 }
487 
488 static uint64_t addr_compare(struct nl_object *_a, struct nl_object *_b,
489  uint64_t attrs, int flags)
490 {
491  struct rtnl_addr *a = (struct rtnl_addr *) _a;
492  struct rtnl_addr *b = (struct rtnl_addr *) _b;
493  uint64_t diff = 0;
494 
495 #define ADDR_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ADDR_ATTR_##ATTR, a, b, EXPR)
496 
497  diff |= ADDR_DIFF(IFINDEX, a->a_ifindex != b->a_ifindex);
498  diff |= ADDR_DIFF(FAMILY, a->a_family != b->a_family);
499  diff |= ADDR_DIFF(SCOPE, a->a_scope != b->a_scope);
500  diff |= ADDR_DIFF(LABEL, strcmp(a->a_label, b->a_label));
501  if (attrs & ADDR_ATTR_PEER) {
502  if ( (flags & ID_COMPARISON)
503  && a->a_family == AF_INET
504  && b->a_family == AF_INET
505  && a->a_peer
506  && b->a_peer
507  && a->a_prefixlen == b->a_prefixlen) {
508  /* when comparing two IPv4 addresses for id-equality, the network part
509  * of the PEER address shall be compared.
510  */
511  diff |= ADDR_DIFF(PEER, nl_addr_cmp_prefix(a->a_peer, b->a_peer));
512  } else
513  diff |= ADDR_DIFF(PEER, nl_addr_cmp(a->a_peer, b->a_peer));
514  }
515  diff |= ADDR_DIFF(LOCAL, nl_addr_cmp(a->a_local, b->a_local));
516  diff |= ADDR_DIFF(MULTICAST, nl_addr_cmp(a->a_multicast,
517  b->a_multicast));
518  diff |= ADDR_DIFF(BROADCAST, nl_addr_cmp(a->a_bcast, b->a_bcast));
519  diff |= ADDR_DIFF(ANYCAST, nl_addr_cmp(a->a_anycast, b->a_anycast));
520  diff |= ADDR_DIFF(CACHEINFO, memcmp(&a->a_cacheinfo, &b->a_cacheinfo,
521  sizeof (a->a_cacheinfo)));
522 
523  if (flags & LOOSE_COMPARISON)
524  diff |= ADDR_DIFF(FLAGS,
525  (a->a_flags ^ b->a_flags) & b->a_flag_mask);
526  else
527  diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags);
528 
529 #undef ADDR_DIFF
530 
531  return diff;
532 }
533 
534 static const struct trans_tbl addr_attrs[] = {
535  __ADD(ADDR_ATTR_FAMILY, family),
536  __ADD(ADDR_ATTR_PREFIXLEN, prefixlen),
537  __ADD(ADDR_ATTR_FLAGS, flags),
538  __ADD(ADDR_ATTR_SCOPE, scope),
539  __ADD(ADDR_ATTR_IFINDEX, ifindex),
540  __ADD(ADDR_ATTR_LABEL, label),
541  __ADD(ADDR_ATTR_CACHEINFO, cacheinfo),
542  __ADD(ADDR_ATTR_PEER, peer),
543  __ADD(ADDR_ATTR_LOCAL, local),
544  __ADD(ADDR_ATTR_BROADCAST, broadcast),
545  __ADD(ADDR_ATTR_MULTICAST, multicast),
546 };
547 
548 static char *addr_attrs2str(int attrs, char *buf, size_t len)
549 {
550  return __flags2str(attrs, buf, len, addr_attrs,
551  ARRAY_SIZE(addr_attrs));
552 }
553 
554 /**
555  * @name Allocation/Freeing
556  * @{
557  */
558 
559 struct rtnl_addr *rtnl_addr_alloc(void)
560 {
561  return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops);
562 }
563 
564 void rtnl_addr_put(struct rtnl_addr *addr)
565 {
566  nl_object_put((struct nl_object *) addr);
567 }
568 
569 /** @} */
570 
571 /**
572  * @name Cache Management
573  * @{
574  */
575 
576 int rtnl_addr_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
577 {
578  return nl_cache_alloc_and_fill(&rtnl_addr_ops, sk, result);
579 }
580 
581 /**
582  * Search address in cache
583  * @arg cache Address cache
584  * @arg ifindex Interface index of address
585  * @arg addr Local address part
586  *
587  * Searches address cache previously allocated with rtnl_addr_alloc_cache()
588  * for an address with a matching local address.
589  *
590  * The reference counter is incremented before returning the address, therefore
591  * the reference must be given back with rtnl_addr_put() after usage.
592  *
593  * @return Address object or NULL if no match was found.
594  */
595 struct rtnl_addr *rtnl_addr_get(struct nl_cache *cache, int ifindex,
596  struct nl_addr *addr)
597 {
598  struct rtnl_addr *a;
599 
600  if (cache->c_ops != &rtnl_addr_ops)
601  return NULL;
602 
603  nl_list_for_each_entry(a, &cache->c_items, ce_list) {
604  if (ifindex && a->a_ifindex != ifindex)
605  continue;
606 
607  if (a->ce_mask & ADDR_ATTR_LOCAL &&
608  !nl_addr_cmp(a->a_local, addr)) {
609  nl_object_get((struct nl_object *) a);
610  return a;
611  }
612  }
613 
614  return NULL;
615 }
616 
617 /** @} */
618 
619 static int build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags,
620  struct nl_msg **result)
621 {
622  struct nl_msg *msg;
623  struct ifaddrmsg am = {
624  .ifa_family = tmpl->a_family,
625  .ifa_index = tmpl->a_ifindex,
626  .ifa_prefixlen = tmpl->a_prefixlen,
627  .ifa_flags = tmpl->a_flags,
628  };
629 
630  if (tmpl->ce_mask & ADDR_ATTR_SCOPE)
631  am.ifa_scope = tmpl->a_scope;
632  else {
633  /* compatibility hack */
634  if (tmpl->a_family == AF_INET &&
635  tmpl->ce_mask & ADDR_ATTR_LOCAL &&
636  *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
637  am.ifa_scope = RT_SCOPE_HOST;
638  else
639  am.ifa_scope = RT_SCOPE_UNIVERSE;
640  }
641 
642  msg = nlmsg_alloc_simple(cmd, flags);
643  if (!msg)
644  return -NLE_NOMEM;
645 
646  if (nlmsg_append(msg, &am, sizeof(am), NLMSG_ALIGNTO) < 0)
647  goto nla_put_failure;
648 
649  if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
650  NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
651 
652  if (tmpl->ce_mask & ADDR_ATTR_PEER)
653  NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
654  else if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
655  NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
656 
657  if (tmpl->ce_mask & ADDR_ATTR_LABEL)
658  NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
659 
660  if (tmpl->ce_mask & ADDR_ATTR_BROADCAST)
661  NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
662 
663  if (tmpl->ce_mask & ADDR_ATTR_CACHEINFO) {
664  struct ifa_cacheinfo ca = {
665  .ifa_valid = tmpl->a_cacheinfo.aci_valid,
666  .ifa_prefered = tmpl->a_cacheinfo.aci_prefered,
667  };
668 
669  NLA_PUT(msg, IFA_CACHEINFO, sizeof(ca), &ca);
670  }
671 
672  if (tmpl->a_flags & ~0xFF) {
673  /* only set the IFA_FLAGS attribute, if they actually contain additional
674  * flags that are not already set to am.ifa_flags.
675  *
676  * Older kernels refuse RTM_NEWADDR and RTM_NEWROUTE messages with EINVAL
677  * if they contain unknown netlink attributes. See net/core/rtnetlink.c, which
678  * was fixed by kernel commit 661d2967b3f1b34eeaa7e212e7b9bbe8ee072b59.
679  *
680  * With this workaround, libnl will function correctly with older kernels,
681  * unless there is a new libnl user that wants to set these flags. In this
682  * case it's up to the user to workaround this issue. */
683  NLA_PUT_U32(msg, IFA_FLAGS, tmpl->a_flags);
684  }
685 
686  *result = msg;
687  return 0;
688 
689 nla_put_failure:
690  nlmsg_free(msg);
691  return -NLE_MSGSIZE;
692 }
693 
694 /**
695  * @name Addition
696  * @{
697  */
698 
699 /**
700  * Build netlink request message to request addition of new address
701  * @arg addr Address object representing the new address.
702  * @arg flags Additional netlink message flags.
703  * @arg result Pointer to store resulting message.
704  *
705  * Builds a new netlink message requesting the addition of a new
706  * address. The netlink message header isn't fully equipped with
707  * all relevant fields and must thus be sent out via nl_send_auto_complete()
708  * or supplemented as needed.
709  *
710  * Minimal required attributes:
711  * - interface index (rtnl_addr_set_ifindex())
712  * - local address (rtnl_addr_set_local())
713  *
714  * The scope will default to universe except for loopback addresses in
715  * which case a host scope is used if not specified otherwise.
716  *
717  * @note Free the memory after usage using nlmsg_free().
718  *
719  * @return 0 on success or a negative error code.
720  */
721 int rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags,
722  struct nl_msg **result)
723 {
724  uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
725  ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
726 
727  if ((addr->ce_mask & required) != required)
728  return -NLE_MISSING_ATTR;
729 
730  return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags, result);
731 }
732 
733 /**
734  * Request addition of new address
735  * @arg sk Netlink socket.
736  * @arg addr Address object representing the new address.
737  * @arg flags Additional netlink message flags.
738  *
739  * Builds a netlink message by calling rtnl_addr_build_add_request(),
740  * sends the request to the kernel and waits for the next ACK to be
741  * received and thus blocks until the request has been fullfilled.
742  *
743  * @see rtnl_addr_build_add_request()
744  *
745  * @return 0 on sucess or a negative error if an error occured.
746  */
747 int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
748 {
749  struct nl_msg *msg;
750  int err;
751 
752  if ((err = rtnl_addr_build_add_request(addr, flags, &msg)) < 0)
753  return err;
754 
755  err = nl_send_auto_complete(sk, msg);
756  nlmsg_free(msg);
757  if (err < 0)
758  return err;
759 
760  return wait_for_ack(sk);
761 }
762 
763 /** @} */
764 
765 /**
766  * @name Deletion
767  * @{
768  */
769 
770 /**
771  * Build a netlink request message to request deletion of an address
772  * @arg addr Address object to be deleteted.
773  * @arg flags Additional netlink message flags.
774  * @arg result Pointer to store resulting message.
775  *
776  * Builds a new netlink message requesting a deletion of an address.
777  * The netlink message header isn't fully equipped with all relevant
778  * fields and must thus be sent out via nl_send_auto_complete()
779  * or supplemented as needed.
780  *
781  * Minimal required attributes:
782  * - interface index (rtnl_addr_set_ifindex())
783  * - address family (rtnl_addr_set_family())
784  *
785  * Optional attributes:
786  * - local address (rtnl_addr_set_local())
787  * - label (rtnl_addr_set_label(), IPv4/DECnet only)
788  * - peer address (rtnl_addr_set_peer(), IPv4 only)
789  *
790  * @note Free the memory after usage using nlmsg_free().
791  *
792  * @return 0 on success or a negative error code.
793  */
794 int rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags,
795  struct nl_msg **result)
796 {
797  uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
798 
799  if ((addr->ce_mask & required) != required)
800  return -NLE_MISSING_ATTR;
801 
802  return build_addr_msg(addr, RTM_DELADDR, flags, result);
803 }
804 
805 /**
806  * Request deletion of an address
807  * @arg sk Netlink socket.
808  * @arg addr Address object to be deleted.
809  * @arg flags Additional netlink message flags.
810  *
811  * Builds a netlink message by calling rtnl_addr_build_delete_request(),
812  * sends the request to the kernel and waits for the next ACK to be
813  * received and thus blocks until the request has been fullfilled.
814  *
815  * @see rtnl_addr_build_delete_request();
816  *
817  * @return 0 on sucess or a negative error if an error occured.
818  */
819 int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
820 {
821  struct nl_msg *msg;
822  int err;
823 
824  if ((err = rtnl_addr_build_delete_request(addr, flags, &msg)) < 0)
825  return err;
826 
827  err = nl_send_auto_complete(sk, msg);
828  nlmsg_free(msg);
829  if (err < 0)
830  return err;
831 
832  return wait_for_ack(sk);
833 }
834 
835 /** @} */
836 
837 /**
838  * @name Attributes
839  * @{
840  */
841 
842 int rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
843 {
844  if (strlen(label) > sizeof(addr->a_label) - 1)
845  return -NLE_RANGE;
846 
847  strcpy(addr->a_label, label);
848  addr->ce_mask |= ADDR_ATTR_LABEL;
849 
850  return 0;
851 }
852 
853 char *rtnl_addr_get_label(struct rtnl_addr *addr)
854 {
855  if (addr->ce_mask & ADDR_ATTR_LABEL)
856  return addr->a_label;
857  else
858  return NULL;
859 }
860 
861 void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
862 {
863  addr->a_ifindex = ifindex;
864  addr->ce_mask |= ADDR_ATTR_IFINDEX;
865 }
866 
867 int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
868 {
869  return addr->a_ifindex;
870 }
871 
872 void rtnl_addr_set_link(struct rtnl_addr *addr, struct rtnl_link *link)
873 {
874  rtnl_link_put(addr->a_link);
875 
876  if (!link)
877  return;
878 
879  nl_object_get(OBJ_CAST(link));
880  addr->a_link = link;
881  addr->a_ifindex = link->l_index;
882  addr->ce_mask |= ADDR_ATTR_IFINDEX;
883 }
884 
885 struct rtnl_link *rtnl_addr_get_link(struct rtnl_addr *addr)
886 {
887  if (addr->a_link) {
888  nl_object_get(OBJ_CAST(addr->a_link));
889  return addr->a_link;
890  }
891 
892  return NULL;
893 }
894 
895 void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
896 {
897  addr->a_family = family;
898  addr->ce_mask |= ADDR_ATTR_FAMILY;
899 }
900 
901 int rtnl_addr_get_family(struct rtnl_addr *addr)
902 {
903  return addr->a_family;
904 }
905 
906 /**
907  * Set the prefix length / netmask
908  * @arg addr Address
909  * @arg prefixlen Length of prefix (netmask)
910  *
911  * Modifies the length of the prefix. If the address object contains a peer
912  * address the prefix length will apply to it, otherwise the prefix length
913  * will apply to the local address of the address.
914  *
915  * If the address object contains a peer or local address the corresponding
916  * `struct nl_addr` will be updated with the new prefix length.
917  *
918  * @note Specifying a length of 0 will remove the prefix length alltogether.
919  *
920  * @see rtnl_addr_get_prefixlen()
921  */
922 void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen)
923 {
924  addr->a_prefixlen = prefixlen;
925 
926  if (prefixlen)
927  addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
928  else
929  addr->ce_mask &= ~ADDR_ATTR_PREFIXLEN;
930 
931  /*
932  * The prefix length always applies to the peer address if
933  * a peer address is present.
934  */
935  if (addr->a_peer)
936  nl_addr_set_prefixlen(addr->a_peer, prefixlen);
937  else if (addr->a_local)
938  nl_addr_set_prefixlen(addr->a_local, prefixlen);
939 }
940 
941 int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
942 {
943  return addr->a_prefixlen;
944 }
945 
946 void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
947 {
948  addr->a_scope = scope;
949  addr->ce_mask |= ADDR_ATTR_SCOPE;
950 }
951 
952 int rtnl_addr_get_scope(struct rtnl_addr *addr)
953 {
954  return addr->a_scope;
955 }
956 
957 void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
958 {
959  addr->a_flag_mask |= flags;
960  addr->a_flags |= flags;
961  addr->ce_mask |= ADDR_ATTR_FLAGS;
962 }
963 
964 void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
965 {
966  addr->a_flag_mask |= flags;
967  addr->a_flags &= ~flags;
968  addr->ce_mask |= ADDR_ATTR_FLAGS;
969 }
970 
971 unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
972 {
973  return addr->a_flags;
974 }
975 
976 static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
977  struct nl_addr *new, int flag)
978 {
979  if (new) {
980  if (addr->ce_mask & ADDR_ATTR_FAMILY) {
981  if (new->a_family != addr->a_family)
982  return -NLE_AF_MISMATCH;
983  } else
984  addr->a_family = new->a_family;
985 
986  if (*pos)
987  nl_addr_put(*pos);
988 
989  *pos = nl_addr_get(new);
990  addr->ce_mask |= (flag | ADDR_ATTR_FAMILY);
991  } else {
992  if (*pos)
993  nl_addr_put(*pos);
994 
995  *pos = NULL;
996  addr->ce_mask &= ~flag;
997  }
998 
999  return 0;
1000 }
1001 
1002 int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
1003 {
1004  int err;
1005 
1006  /* Prohibit local address with prefix length if peer address is present */
1007  if ((addr->ce_mask & ADDR_ATTR_PEER) && local &&
1008  nl_addr_get_prefixlen(local))
1009  return -NLE_INVAL;
1010 
1011  err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
1012  if (err < 0)
1013  return err;
1014 
1015  /* Never overwrite the prefix length if a peer address is present */
1016  if (!(addr->ce_mask & ADDR_ATTR_PEER))
1017  rtnl_addr_set_prefixlen(addr, local ? nl_addr_get_prefixlen(local) : 0);
1018 
1019  return 0;
1020 }
1021 
1022 struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
1023 {
1024  return addr->a_local;
1025 }
1026 
1027 int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
1028 {
1029  int err;
1030 
1031  if (peer && peer->a_family != AF_INET)
1032  return -NLE_AF_NOSUPPORT;
1033 
1034  err = __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
1035  if (err < 0)
1036  return err;
1037 
1038  rtnl_addr_set_prefixlen(addr, peer ? nl_addr_get_prefixlen(peer) : 0);
1039 
1040  return 0;
1041 }
1042 
1043 struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
1044 {
1045  return addr->a_peer;
1046 }
1047 
1048 int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
1049 {
1050  if (bcast && bcast->a_family != AF_INET)
1051  return -NLE_AF_NOSUPPORT;
1052 
1053  return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
1054 }
1055 
1056 struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
1057 {
1058  return addr->a_bcast;
1059 }
1060 
1061 int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
1062 {
1063  if (multicast && multicast->a_family != AF_INET6)
1064  return -NLE_AF_NOSUPPORT;
1065 
1066  return __assign_addr(addr, &addr->a_multicast, multicast,
1067  ADDR_ATTR_MULTICAST);
1068 }
1069 
1070 struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
1071 {
1072  return addr->a_multicast;
1073 }
1074 
1075 int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
1076 {
1077  if (anycast && anycast->a_family != AF_INET6)
1078  return -NLE_AF_NOSUPPORT;
1079 
1080  return __assign_addr(addr, &addr->a_anycast, anycast,
1081  ADDR_ATTR_ANYCAST);
1082 }
1083 
1084 struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
1085 {
1086  return addr->a_anycast;
1087 }
1088 
1089 uint32_t rtnl_addr_get_valid_lifetime(struct rtnl_addr *addr)
1090 {
1091  if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
1092  return addr->a_cacheinfo.aci_valid;
1093  else
1094  return 0xFFFFFFFFU;
1095 }
1096 
1097 void rtnl_addr_set_valid_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
1098 {
1099  addr->a_cacheinfo.aci_valid = lifetime;
1100  addr->ce_mask |= ADDR_ATTR_CACHEINFO;
1101 }
1102 
1103 uint32_t rtnl_addr_get_preferred_lifetime(struct rtnl_addr *addr)
1104 {
1105  if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
1106  return addr->a_cacheinfo.aci_prefered;
1107  else
1108  return 0xFFFFFFFFU;
1109 }
1110 
1111 void rtnl_addr_set_preferred_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
1112 {
1113  addr->a_cacheinfo.aci_prefered = lifetime;
1114  addr->ce_mask |= ADDR_ATTR_CACHEINFO;
1115 }
1116 
1117 uint32_t rtnl_addr_get_create_time(struct rtnl_addr *addr)
1118 {
1119  return addr->a_cacheinfo.aci_cstamp;
1120 }
1121 
1122 uint32_t rtnl_addr_get_last_update_time(struct rtnl_addr *addr)
1123 {
1124  return addr->a_cacheinfo.aci_tstamp;
1125 }
1126 
1127 /** @} */
1128 
1129 /**
1130  * @name Flags Translations
1131  * @{
1132  */
1133 
1134 static const struct trans_tbl addr_flags[] = {
1135  __ADD(IFA_F_SECONDARY, secondary),
1136  __ADD(IFA_F_NODAD, nodad),
1137  __ADD(IFA_F_OPTIMISTIC, optimistic),
1138  __ADD(IFA_F_HOMEADDRESS, homeaddress),
1139  __ADD(IFA_F_DEPRECATED, deprecated),
1140  __ADD(IFA_F_TENTATIVE, tentative),
1141  __ADD(IFA_F_PERMANENT, permanent),
1142  __ADD(IFA_F_MANAGETEMPADDR, mngtmpaddr),
1143  __ADD(IFA_F_NOPREFIXROUTE, noprefixroute),
1144 };
1145 
1146 char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
1147 {
1148  return __flags2str(flags, buf, size, addr_flags,
1149  ARRAY_SIZE(addr_flags));
1150 }
1151 
1152 int rtnl_addr_str2flags(const char *name)
1153 {
1154  return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
1155 }
1156 
1157 /** @} */
1158 
1159 static struct nl_object_ops addr_obj_ops = {
1160  .oo_name = "route/addr",
1161  .oo_size = sizeof(struct rtnl_addr),
1162  .oo_constructor = addr_constructor,
1163  .oo_free_data = addr_free_data,
1164  .oo_clone = addr_clone,
1165  .oo_dump = {
1166  [NL_DUMP_LINE] = addr_dump_line,
1167  [NL_DUMP_DETAILS] = addr_dump_details,
1168  [NL_DUMP_STATS] = addr_dump_stats,
1169  },
1170  .oo_compare = addr_compare,
1171  .oo_attrs2str = addr_attrs2str,
1172  .oo_id_attrs_get = addr_id_attrs_get,
1173  .oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
1174  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN),
1175 };
1176 
1177 static struct nl_af_group addr_groups[] = {
1178  { AF_INET, RTNLGRP_IPV4_IFADDR },
1179  { AF_INET6, RTNLGRP_IPV6_IFADDR },
1180  { END_OF_GROUP_LIST },
1181 };
1182 
1183 static struct nl_cache_ops rtnl_addr_ops = {
1184  .co_name = "route/addr",
1185  .co_hdrsize = sizeof(struct ifaddrmsg),
1186  .co_msgtypes = {
1187  { RTM_NEWADDR, NL_ACT_NEW, "new" },
1188  { RTM_DELADDR, NL_ACT_DEL, "del" },
1189  { RTM_GETADDR, NL_ACT_GET, "get" },
1190  END_OF_MSGTYPES_LIST,
1191  },
1192  .co_protocol = NETLINK_ROUTE,
1193  .co_groups = addr_groups,
1194  .co_request_update = addr_request_update,
1195  .co_msg_parser = addr_msg_parser,
1196  .co_obj_ops = &addr_obj_ops,
1197 };
1198 
1199 static void __init addr_init(void)
1200 {
1201  nl_cache_mngt_register(&rtnl_addr_ops);
1202 }
1203 
1204 static void __exit addr_exit(void)
1205 {
1206  nl_cache_mngt_unregister(&rtnl_addr_ops);
1207 }
1208 
1209 /** @} */
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1247
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition: addr.c:493
Dump object briefly on one line.
Definition: types.h:22
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
Definition: addr.c:957
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:562
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
Definition: addr.c:585
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition: msg.c:106
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
Definition: attr.h:288
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
Definition: addr.c:968
void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen)
Set the prefix length / netmask.
Definition: addr.c:922
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
Definition: object.c:54
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition: cache_mngt.c:287
Attribute validation policy.
Definition: attr.h:69
int rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags, struct nl_msg **result)
Build netlink request message to request addition of new address.
Definition: addr.c:721
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
Definition: cache_mngt.c:430
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
Definition: object.c:204
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
Definition: addr.c:217
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
Definition: utils.c:594
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:706
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
Definition: msg.c:214
struct rtnl_addr * rtnl_addr_get(struct nl_cache *cache, int ifindex, struct nl_addr *addr)
Search address in cache.
Definition: addr.c:595
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition: addr.c:523
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
Definition: addr.c:262
NUL terminated character string.
Definition: attr.h:45
Dump all attributes but no statistics.
Definition: types.h:23
int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request deletion of an address.
Definition: addr.c:819
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition: cache_mngt.c:252
int nl_rtgen_request(struct nl_sock *sk, int type, int family, int flags)
Send routing netlink request message.
Definition: rtnl.c:41
#define NLA_PUT(msg, attrtype, attrlen, data)
Add unspecific attribute to netlink message.
Definition: attr.h:164
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
int rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags, struct nl_msg **result)
Build a netlink request message to request deletion of an address.
Definition: addr.c:794
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:235
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
Definition: msg.c:446
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:747
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:215
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
Definition: attr.h:262
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition: addr.c:539
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:71
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:347
Dumping parameters.
Definition: types.h:33
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Definition: utils.c:961
int nl_addr_cmp_prefix(const struct nl_addr *a, const struct nl_addr *b)
Compare the prefix of two abstract addresses.
Definition: addr.c:616
Dump all attributes including statistics.
Definition: types.h:24
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
Definition: addr.c:933
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
Definition: cache.c:233
size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
Copy string attribute payload to a buffer.
Definition: attr.c:378
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition: addr.c:991