libnl  3.2.28
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 
471  switch (addr->a_family) {
472  case AF_INET:
473  return (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
474  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN |
475  ADDR_ATTR_PEER);
476  case AF_INET6:
477  return (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
478  ADDR_ATTR_LOCAL);
479  default:
480  return (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
481  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN);
482  }
483 }
484 
485 static uint64_t addr_compare(struct nl_object *_a, struct nl_object *_b,
486  uint64_t attrs, int flags)
487 {
488  struct rtnl_addr *a = (struct rtnl_addr *) _a;
489  struct rtnl_addr *b = (struct rtnl_addr *) _b;
490  uint64_t diff = 0;
491 
492 #define ADDR_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ADDR_ATTR_##ATTR, a, b, EXPR)
493 
494  diff |= ADDR_DIFF(IFINDEX, a->a_ifindex != b->a_ifindex);
495  diff |= ADDR_DIFF(FAMILY, a->a_family != b->a_family);
496  diff |= ADDR_DIFF(SCOPE, a->a_scope != b->a_scope);
497  diff |= ADDR_DIFF(LABEL, strcmp(a->a_label, b->a_label));
498  if (attrs & ADDR_ATTR_PEER) {
499  if ( (flags & ID_COMPARISON)
500  && a->a_family == AF_INET
501  && b->a_family == AF_INET
502  && a->a_peer
503  && b->a_peer
504  && a->a_prefixlen == b->a_prefixlen) {
505  /* when comparing two IPv4 addresses for id-equality, the network part
506  * of the PEER address shall be compared.
507  */
508  diff |= ADDR_DIFF(PEER, nl_addr_cmp_prefix(a->a_peer, b->a_peer));
509  } else
510  diff |= ADDR_DIFF(PEER, nl_addr_cmp(a->a_peer, b->a_peer));
511  }
512  diff |= ADDR_DIFF(LOCAL, nl_addr_cmp(a->a_local, b->a_local));
513  diff |= ADDR_DIFF(MULTICAST, nl_addr_cmp(a->a_multicast,
514  b->a_multicast));
515  diff |= ADDR_DIFF(BROADCAST, nl_addr_cmp(a->a_bcast, b->a_bcast));
516  diff |= ADDR_DIFF(ANYCAST, nl_addr_cmp(a->a_anycast, b->a_anycast));
517  diff |= ADDR_DIFF(CACHEINFO, memcmp(&a->a_cacheinfo, &b->a_cacheinfo,
518  sizeof (a->a_cacheinfo)));
519 
520  if (flags & LOOSE_COMPARISON)
521  diff |= ADDR_DIFF(FLAGS,
522  (a->a_flags ^ b->a_flags) & b->a_flag_mask);
523  else
524  diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags);
525 
526 #undef ADDR_DIFF
527 
528  return diff;
529 }
530 
531 static const struct trans_tbl addr_attrs[] = {
532  __ADD(ADDR_ATTR_FAMILY, family),
533  __ADD(ADDR_ATTR_PREFIXLEN, prefixlen),
534  __ADD(ADDR_ATTR_FLAGS, flags),
535  __ADD(ADDR_ATTR_SCOPE, scope),
536  __ADD(ADDR_ATTR_IFINDEX, ifindex),
537  __ADD(ADDR_ATTR_LABEL, label),
538  __ADD(ADDR_ATTR_CACHEINFO, cacheinfo),
539  __ADD(ADDR_ATTR_PEER, peer),
540  __ADD(ADDR_ATTR_LOCAL, local),
541  __ADD(ADDR_ATTR_BROADCAST, broadcast),
542  __ADD(ADDR_ATTR_MULTICAST, multicast),
543 };
544 
545 static char *addr_attrs2str(int attrs, char *buf, size_t len)
546 {
547  return __flags2str(attrs, buf, len, addr_attrs,
548  ARRAY_SIZE(addr_attrs));
549 }
550 
551 /**
552  * @name Allocation/Freeing
553  * @{
554  */
555 
556 struct rtnl_addr *rtnl_addr_alloc(void)
557 {
558  return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops);
559 }
560 
561 void rtnl_addr_put(struct rtnl_addr *addr)
562 {
563  nl_object_put((struct nl_object *) addr);
564 }
565 
566 /** @} */
567 
568 /**
569  * @name Cache Management
570  * @{
571  */
572 
573 int rtnl_addr_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
574 {
575  return nl_cache_alloc_and_fill(&rtnl_addr_ops, sk, result);
576 }
577 
578 /**
579  * Search address in cache
580  * @arg cache Address cache
581  * @arg ifindex Interface index of address
582  * @arg addr Local address part
583  *
584  * Searches address cache previously allocated with rtnl_addr_alloc_cache()
585  * for an address with a matching local address.
586  *
587  * The reference counter is incremented before returning the address, therefore
588  * the reference must be given back with rtnl_addr_put() after usage.
589  *
590  * @return Address object or NULL if no match was found.
591  */
592 struct rtnl_addr *rtnl_addr_get(struct nl_cache *cache, int ifindex,
593  struct nl_addr *addr)
594 {
595  struct rtnl_addr *a;
596 
597  if (cache->c_ops != &rtnl_addr_ops)
598  return NULL;
599 
600  nl_list_for_each_entry(a, &cache->c_items, ce_list) {
601  if (ifindex && a->a_ifindex != ifindex)
602  continue;
603 
604  if (a->ce_mask & ADDR_ATTR_LOCAL &&
605  !nl_addr_cmp(a->a_local, addr)) {
606  nl_object_get((struct nl_object *) a);
607  return a;
608  }
609  }
610 
611  return NULL;
612 }
613 
614 /** @} */
615 
616 static int build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags,
617  struct nl_msg **result)
618 {
619  struct nl_msg *msg;
620  struct ifaddrmsg am = {
621  .ifa_family = tmpl->a_family,
622  .ifa_index = tmpl->a_ifindex,
623  .ifa_prefixlen = tmpl->a_prefixlen,
624  .ifa_flags = tmpl->a_flags,
625  };
626 
627  if (tmpl->ce_mask & ADDR_ATTR_SCOPE)
628  am.ifa_scope = tmpl->a_scope;
629  else {
630  /* compatibility hack */
631  if (tmpl->a_family == AF_INET &&
632  tmpl->ce_mask & ADDR_ATTR_LOCAL &&
633  *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
634  am.ifa_scope = RT_SCOPE_HOST;
635  else
636  am.ifa_scope = RT_SCOPE_UNIVERSE;
637  }
638 
639  msg = nlmsg_alloc_simple(cmd, flags);
640  if (!msg)
641  return -NLE_NOMEM;
642 
643  if (nlmsg_append(msg, &am, sizeof(am), NLMSG_ALIGNTO) < 0)
644  goto nla_put_failure;
645 
646  if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
647  NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
648 
649  if (tmpl->ce_mask & ADDR_ATTR_PEER)
650  NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
651  else if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
652  NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
653 
654  if (tmpl->ce_mask & ADDR_ATTR_LABEL)
655  NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
656 
657  if (tmpl->ce_mask & ADDR_ATTR_BROADCAST)
658  NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
659 
660  if (tmpl->ce_mask & ADDR_ATTR_CACHEINFO) {
661  struct ifa_cacheinfo ca = {
662  .ifa_valid = tmpl->a_cacheinfo.aci_valid,
663  .ifa_prefered = tmpl->a_cacheinfo.aci_prefered,
664  };
665 
666  NLA_PUT(msg, IFA_CACHEINFO, sizeof(ca), &ca);
667  }
668 
669  if (tmpl->a_flags & ~0xFF) {
670  /* only set the IFA_FLAGS attribute, if they actually contain additional
671  * flags that are not already set to am.ifa_flags.
672  *
673  * Older kernels refuse RTM_NEWADDR and RTM_NEWROUTE messages with EINVAL
674  * if they contain unknown netlink attributes. See net/core/rtnetlink.c, which
675  * was fixed by kernel commit 661d2967b3f1b34eeaa7e212e7b9bbe8ee072b59.
676  *
677  * With this workaround, libnl will function correctly with older kernels,
678  * unless there is a new libnl user that wants to set these flags. In this
679  * case it's up to the user to workaround this issue. */
680  NLA_PUT_U32(msg, IFA_FLAGS, tmpl->a_flags);
681  }
682 
683  *result = msg;
684  return 0;
685 
686 nla_put_failure:
687  nlmsg_free(msg);
688  return -NLE_MSGSIZE;
689 }
690 
691 /**
692  * @name Addition
693  * @{
694  */
695 
696 /**
697  * Build netlink request message to request addition of new address
698  * @arg addr Address object representing the new address.
699  * @arg flags Additional netlink message flags.
700  * @arg result Pointer to store resulting message.
701  *
702  * Builds a new netlink message requesting the addition of a new
703  * address. The netlink message header isn't fully equipped with
704  * all relevant fields and must thus be sent out via nl_send_auto_complete()
705  * or supplemented as needed.
706  *
707  * Minimal required attributes:
708  * - interface index (rtnl_addr_set_ifindex())
709  * - local address (rtnl_addr_set_local())
710  *
711  * The scope will default to universe except for loopback addresses in
712  * which case a host scope is used if not specified otherwise.
713  *
714  * @note Free the memory after usage using nlmsg_free().
715  *
716  * @return 0 on success or a negative error code.
717  */
718 int rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags,
719  struct nl_msg **result)
720 {
721  uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
722  ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
723 
724  if ((addr->ce_mask & required) != required)
725  return -NLE_MISSING_ATTR;
726 
727  return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags, result);
728 }
729 
730 /**
731  * Request addition of new address
732  * @arg sk Netlink socket.
733  * @arg addr Address object representing the new address.
734  * @arg flags Additional netlink message flags.
735  *
736  * Builds a netlink message by calling rtnl_addr_build_add_request(),
737  * sends the request to the kernel and waits for the next ACK to be
738  * received and thus blocks until the request has been fullfilled.
739  *
740  * @see rtnl_addr_build_add_request()
741  *
742  * @return 0 on sucess or a negative error if an error occured.
743  */
744 int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
745 {
746  struct nl_msg *msg;
747  int err;
748 
749  if ((err = rtnl_addr_build_add_request(addr, flags, &msg)) < 0)
750  return err;
751 
752  err = nl_send_auto_complete(sk, msg);
753  nlmsg_free(msg);
754  if (err < 0)
755  return err;
756 
757  return wait_for_ack(sk);
758 }
759 
760 /** @} */
761 
762 /**
763  * @name Deletion
764  * @{
765  */
766 
767 /**
768  * Build a netlink request message to request deletion of an address
769  * @arg addr Address object to be deleteted.
770  * @arg flags Additional netlink message flags.
771  * @arg result Pointer to store resulting message.
772  *
773  * Builds a new netlink message requesting a deletion of an address.
774  * The netlink message header isn't fully equipped with all relevant
775  * fields and must thus be sent out via nl_send_auto_complete()
776  * or supplemented as needed.
777  *
778  * Minimal required attributes:
779  * - interface index (rtnl_addr_set_ifindex())
780  * - address family (rtnl_addr_set_family())
781  *
782  * Optional attributes:
783  * - local address (rtnl_addr_set_local())
784  * - label (rtnl_addr_set_label(), IPv4/DECnet only)
785  * - peer address (rtnl_addr_set_peer(), IPv4 only)
786  *
787  * @note Free the memory after usage using nlmsg_free().
788  *
789  * @return 0 on success or a negative error code.
790  */
791 int rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags,
792  struct nl_msg **result)
793 {
794  uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
795 
796  if ((addr->ce_mask & required) != required)
797  return -NLE_MISSING_ATTR;
798 
799  return build_addr_msg(addr, RTM_DELADDR, flags, result);
800 }
801 
802 /**
803  * Request deletion of an address
804  * @arg sk Netlink socket.
805  * @arg addr Address object to be deleted.
806  * @arg flags Additional netlink message flags.
807  *
808  * Builds a netlink message by calling rtnl_addr_build_delete_request(),
809  * sends the request to the kernel and waits for the next ACK to be
810  * received and thus blocks until the request has been fullfilled.
811  *
812  * @see rtnl_addr_build_delete_request();
813  *
814  * @return 0 on sucess or a negative error if an error occured.
815  */
816 int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
817 {
818  struct nl_msg *msg;
819  int err;
820 
821  if ((err = rtnl_addr_build_delete_request(addr, flags, &msg)) < 0)
822  return err;
823 
824  err = nl_send_auto_complete(sk, msg);
825  nlmsg_free(msg);
826  if (err < 0)
827  return err;
828 
829  return wait_for_ack(sk);
830 }
831 
832 /** @} */
833 
834 /**
835  * @name Attributes
836  * @{
837  */
838 
839 int rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
840 {
841  if (strlen(label) > sizeof(addr->a_label) - 1)
842  return -NLE_RANGE;
843 
844  strcpy(addr->a_label, label);
845  addr->ce_mask |= ADDR_ATTR_LABEL;
846 
847  return 0;
848 }
849 
850 char *rtnl_addr_get_label(struct rtnl_addr *addr)
851 {
852  if (addr->ce_mask & ADDR_ATTR_LABEL)
853  return addr->a_label;
854  else
855  return NULL;
856 }
857 
858 void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
859 {
860  addr->a_ifindex = ifindex;
861  addr->ce_mask |= ADDR_ATTR_IFINDEX;
862 }
863 
864 int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
865 {
866  return addr->a_ifindex;
867 }
868 
869 void rtnl_addr_set_link(struct rtnl_addr *addr, struct rtnl_link *link)
870 {
871  rtnl_link_put(addr->a_link);
872 
873  if (!link)
874  return;
875 
876  nl_object_get(OBJ_CAST(link));
877  addr->a_link = link;
878  addr->a_ifindex = link->l_index;
879  addr->ce_mask |= ADDR_ATTR_IFINDEX;
880 }
881 
882 struct rtnl_link *rtnl_addr_get_link(struct rtnl_addr *addr)
883 {
884  if (addr->a_link) {
885  nl_object_get(OBJ_CAST(addr->a_link));
886  return addr->a_link;
887  }
888 
889  return NULL;
890 }
891 
892 void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
893 {
894  addr->a_family = family;
895  addr->ce_mask |= ADDR_ATTR_FAMILY;
896 }
897 
898 int rtnl_addr_get_family(struct rtnl_addr *addr)
899 {
900  return addr->a_family;
901 }
902 
903 /**
904  * Set the prefix length / netmask
905  * @arg addr Address
906  * @arg prefixlen Length of prefix (netmask)
907  *
908  * Modifies the length of the prefix. If the address object contains a peer
909  * address the prefix length will apply to it, otherwise the prefix length
910  * will apply to the local address of the address.
911  *
912  * If the address object contains a peer or local address the corresponding
913  * `struct nl_addr` will be updated with the new prefix length.
914  *
915  * @note Specifying a length of 0 will remove the prefix length alltogether.
916  *
917  * @see rtnl_addr_get_prefixlen()
918  */
919 void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen)
920 {
921  addr->a_prefixlen = prefixlen;
922 
923  if (prefixlen)
924  addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
925  else
926  addr->ce_mask &= ~ADDR_ATTR_PREFIXLEN;
927 
928  /*
929  * The prefix length always applies to the peer address if
930  * a peer address is present.
931  */
932  if (addr->a_peer)
933  nl_addr_set_prefixlen(addr->a_peer, prefixlen);
934  else if (addr->a_local)
935  nl_addr_set_prefixlen(addr->a_local, prefixlen);
936 }
937 
938 int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
939 {
940  return addr->a_prefixlen;
941 }
942 
943 void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
944 {
945  addr->a_scope = scope;
946  addr->ce_mask |= ADDR_ATTR_SCOPE;
947 }
948 
949 int rtnl_addr_get_scope(struct rtnl_addr *addr)
950 {
951  return addr->a_scope;
952 }
953 
954 void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
955 {
956  addr->a_flag_mask |= flags;
957  addr->a_flags |= flags;
958  addr->ce_mask |= ADDR_ATTR_FLAGS;
959 }
960 
961 void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
962 {
963  addr->a_flag_mask |= flags;
964  addr->a_flags &= ~flags;
965  addr->ce_mask |= ADDR_ATTR_FLAGS;
966 }
967 
968 unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
969 {
970  return addr->a_flags;
971 }
972 
973 static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
974  struct nl_addr *new, int flag)
975 {
976  if (new) {
977  if (addr->ce_mask & ADDR_ATTR_FAMILY) {
978  if (new->a_family != addr->a_family)
979  return -NLE_AF_MISMATCH;
980  } else
981  addr->a_family = new->a_family;
982 
983  if (*pos)
984  nl_addr_put(*pos);
985 
986  *pos = nl_addr_get(new);
987  addr->ce_mask |= (flag | ADDR_ATTR_FAMILY);
988  } else {
989  if (*pos)
990  nl_addr_put(*pos);
991 
992  *pos = NULL;
993  addr->ce_mask &= ~flag;
994  }
995 
996  return 0;
997 }
998 
999 int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
1000 {
1001  int err;
1002 
1003  /* Prohibit local address with prefix length if peer address is present */
1004  if ((addr->ce_mask & ADDR_ATTR_PEER) && local &&
1005  nl_addr_get_prefixlen(local))
1006  return -NLE_INVAL;
1007 
1008  err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
1009  if (err < 0)
1010  return err;
1011 
1012  /* Never overwrite the prefix length if a peer address is present */
1013  if (!(addr->ce_mask & ADDR_ATTR_PEER))
1014  rtnl_addr_set_prefixlen(addr, local ? nl_addr_get_prefixlen(local) : 0);
1015 
1016  return 0;
1017 }
1018 
1019 struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
1020 {
1021  return addr->a_local;
1022 }
1023 
1024 int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
1025 {
1026  int err;
1027 
1028  if (peer && peer->a_family != AF_INET)
1029  return -NLE_AF_NOSUPPORT;
1030 
1031  err = __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
1032  if (err < 0)
1033  return err;
1034 
1035  rtnl_addr_set_prefixlen(addr, peer ? nl_addr_get_prefixlen(peer) : 0);
1036 
1037  return 0;
1038 }
1039 
1040 struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
1041 {
1042  return addr->a_peer;
1043 }
1044 
1045 int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
1046 {
1047  if (bcast && bcast->a_family != AF_INET)
1048  return -NLE_AF_NOSUPPORT;
1049 
1050  return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
1051 }
1052 
1053 struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
1054 {
1055  return addr->a_bcast;
1056 }
1057 
1058 int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
1059 {
1060  if (multicast && multicast->a_family != AF_INET6)
1061  return -NLE_AF_NOSUPPORT;
1062 
1063  return __assign_addr(addr, &addr->a_multicast, multicast,
1064  ADDR_ATTR_MULTICAST);
1065 }
1066 
1067 struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
1068 {
1069  return addr->a_multicast;
1070 }
1071 
1072 int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
1073 {
1074  if (anycast && anycast->a_family != AF_INET6)
1075  return -NLE_AF_NOSUPPORT;
1076 
1077  return __assign_addr(addr, &addr->a_anycast, anycast,
1078  ADDR_ATTR_ANYCAST);
1079 }
1080 
1081 struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
1082 {
1083  return addr->a_anycast;
1084 }
1085 
1086 uint32_t rtnl_addr_get_valid_lifetime(struct rtnl_addr *addr)
1087 {
1088  if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
1089  return addr->a_cacheinfo.aci_valid;
1090  else
1091  return 0xFFFFFFFFU;
1092 }
1093 
1094 void rtnl_addr_set_valid_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
1095 {
1096  addr->a_cacheinfo.aci_valid = lifetime;
1097  addr->ce_mask |= ADDR_ATTR_CACHEINFO;
1098 }
1099 
1100 uint32_t rtnl_addr_get_preferred_lifetime(struct rtnl_addr *addr)
1101 {
1102  if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
1103  return addr->a_cacheinfo.aci_prefered;
1104  else
1105  return 0xFFFFFFFFU;
1106 }
1107 
1108 void rtnl_addr_set_preferred_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
1109 {
1110  addr->a_cacheinfo.aci_prefered = lifetime;
1111  addr->ce_mask |= ADDR_ATTR_CACHEINFO;
1112 }
1113 
1114 uint32_t rtnl_addr_get_create_time(struct rtnl_addr *addr)
1115 {
1116  return addr->a_cacheinfo.aci_cstamp;
1117 }
1118 
1119 uint32_t rtnl_addr_get_last_update_time(struct rtnl_addr *addr)
1120 {
1121  return addr->a_cacheinfo.aci_tstamp;
1122 }
1123 
1124 /** @} */
1125 
1126 /**
1127  * @name Flags Translations
1128  * @{
1129  */
1130 
1131 static const struct trans_tbl addr_flags[] = {
1132  __ADD(IFA_F_SECONDARY, secondary),
1133  __ADD(IFA_F_NODAD, nodad),
1134  __ADD(IFA_F_OPTIMISTIC, optimistic),
1135  __ADD(IFA_F_HOMEADDRESS, homeaddress),
1136  __ADD(IFA_F_DEPRECATED, deprecated),
1137  __ADD(IFA_F_TENTATIVE, tentative),
1138  __ADD(IFA_F_PERMANENT, permanent),
1139  __ADD(IFA_F_MANAGETEMPADDR, mngtmpaddr),
1140  __ADD(IFA_F_NOPREFIXROUTE, noprefixroute),
1141 };
1142 
1143 char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
1144 {
1145  return __flags2str(flags, buf, size, addr_flags,
1146  ARRAY_SIZE(addr_flags));
1147 }
1148 
1149 int rtnl_addr_str2flags(const char *name)
1150 {
1151  return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
1152 }
1153 
1154 /** @} */
1155 
1156 static struct nl_object_ops addr_obj_ops = {
1157  .oo_name = "route/addr",
1158  .oo_size = sizeof(struct rtnl_addr),
1159  .oo_constructor = addr_constructor,
1160  .oo_free_data = addr_free_data,
1161  .oo_clone = addr_clone,
1162  .oo_dump = {
1163  [NL_DUMP_LINE] = addr_dump_line,
1164  [NL_DUMP_DETAILS] = addr_dump_details,
1165  [NL_DUMP_STATS] = addr_dump_stats,
1166  },
1167  .oo_compare = addr_compare,
1168  .oo_attrs2str = addr_attrs2str,
1169  .oo_id_attrs_get = addr_id_attrs_get,
1170  .oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
1171  ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN),
1172 };
1173 
1174 static struct nl_af_group addr_groups[] = {
1175  { AF_INET, RTNLGRP_IPV4_IFADDR },
1176  { AF_INET6, RTNLGRP_IPV6_IFADDR },
1177  { END_OF_GROUP_LIST },
1178 };
1179 
1180 static struct nl_cache_ops rtnl_addr_ops = {
1181  .co_name = "route/addr",
1182  .co_hdrsize = sizeof(struct ifaddrmsg),
1183  .co_msgtypes = {
1184  { RTM_NEWADDR, NL_ACT_NEW, "new" },
1185  { RTM_DELADDR, NL_ACT_DEL, "del" },
1186  { RTM_GETADDR, NL_ACT_GET, "get" },
1187  END_OF_MSGTYPES_LIST,
1188  },
1189  .co_protocol = NETLINK_ROUTE,
1190  .co_groups = addr_groups,
1191  .co_request_update = addr_request_update,
1192  .co_msg_parser = addr_msg_parser,
1193  .co_obj_ops = &addr_obj_ops,
1194 };
1195 
1196 static void __init addr_init(void)
1197 {
1198  nl_cache_mngt_register(&rtnl_addr_ops);
1199 }
1200 
1201 static void __exit addr_exit(void)
1202 {
1203  nl_cache_mngt_unregister(&rtnl_addr_ops);
1204 }
1205 
1206 /** @} */
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Definition: nl.c:1252
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Definition: addr.c:471
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:917
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
Definition: msg.c:558
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
Definition: addr.c:563
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
Definition: msg.c:105
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
Definition: attr.h:286
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
Definition: addr.c:928
void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen)
Set the prefix length / netmask.
Definition: addr.c:919
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:67
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:718
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:216
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
Definition: utils.c:547
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:699
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:213
struct rtnl_addr * rtnl_addr_get(struct nl_cache *cache, int ifindex, struct nl_addr *addr)
Search address in cache.
Definition: addr.c:592
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition: addr.c:501
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
Definition: addr.c:255
NUL terminated character string.
Definition: attr.h:43
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:816
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:162
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:791
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:233
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:442
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
Request addition of new address.
Definition: addr.c:744
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:260
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
Definition: addr.c:517
uint16_t type
Type of attribute or NLA_UNSPEC.
Definition: attr.h:69
struct nl_msg * nlmsg_alloc_simple(int nlmsgtype, int flags)
Allocate a new netlink message.
Definition: msg.c:346
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:914
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:594
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:893
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:951