libnl  3.2.29-rc1
route_obj.c
1 /*
2  * lib/route/route_obj.c Route Object
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup route
14  * @defgroup route_obj Route Object
15  *
16  * @par Attributes
17  * @code
18  * Name Default
19  * -------------------------------------------------------------
20  * routing table RT_TABLE_MAIN
21  * scope RT_SCOPE_NOWHERE
22  * tos 0
23  * protocol RTPROT_STATIC
24  * prio 0
25  * family AF_UNSPEC
26  * type RTN_UNICAST
27  * iif NULL
28  * @endcode
29  *
30  * @{
31  */
32 
33 #include <netlink-private/netlink.h>
34 #include <netlink-private/utils.h>
35 #include <netlink/netlink.h>
36 #include <netlink/cache.h>
37 #include <netlink/utils.h>
38 #include <netlink/data.h>
39 #include <netlink/hashtable.h>
40 #include <netlink/route/rtnl.h>
41 #include <netlink/route/route.h>
42 #include <netlink/route/link.h>
43 #include <netlink/route/nexthop.h>
44 
45 /** @cond SKIP */
46 #define ROUTE_ATTR_FAMILY 0x000001
47 #define ROUTE_ATTR_TOS 0x000002
48 #define ROUTE_ATTR_TABLE 0x000004
49 #define ROUTE_ATTR_PROTOCOL 0x000008
50 #define ROUTE_ATTR_SCOPE 0x000010
51 #define ROUTE_ATTR_TYPE 0x000020
52 #define ROUTE_ATTR_FLAGS 0x000040
53 #define ROUTE_ATTR_DST 0x000080
54 #define ROUTE_ATTR_SRC 0x000100
55 #define ROUTE_ATTR_IIF 0x000200
56 #define ROUTE_ATTR_OIF 0x000400
57 #define ROUTE_ATTR_GATEWAY 0x000800
58 #define ROUTE_ATTR_PRIO 0x001000
59 #define ROUTE_ATTR_PREF_SRC 0x002000
60 #define ROUTE_ATTR_METRICS 0x004000
61 #define ROUTE_ATTR_MULTIPATH 0x008000
62 #define ROUTE_ATTR_REALMS 0x010000
63 #define ROUTE_ATTR_CACHEINFO 0x020000
64 /** @endcond */
65 
66 static void route_constructor(struct nl_object *c)
67 {
68  struct rtnl_route *r = (struct rtnl_route *) c;
69 
70  r->rt_family = AF_UNSPEC;
71  r->rt_scope = RT_SCOPE_NOWHERE;
72  r->rt_table = RT_TABLE_MAIN;
73  r->rt_protocol = RTPROT_STATIC;
74  r->rt_type = RTN_UNICAST;
75  r->rt_prio = 0;
76 
77  nl_init_list_head(&r->rt_nexthops);
78 }
79 
80 static void route_free_data(struct nl_object *c)
81 {
82  struct rtnl_route *r = (struct rtnl_route *) c;
83  struct rtnl_nexthop *nh, *tmp;
84 
85  if (r == NULL)
86  return;
87 
88  nl_addr_put(r->rt_dst);
89  nl_addr_put(r->rt_src);
90  nl_addr_put(r->rt_pref_src);
91 
92  nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
93  rtnl_route_remove_nexthop(r, nh);
94  rtnl_route_nh_free(nh);
95  }
96 }
97 
98 static int route_clone(struct nl_object *_dst, struct nl_object *_src)
99 {
100  struct rtnl_route *dst = (struct rtnl_route *) _dst;
101  struct rtnl_route *src = (struct rtnl_route *) _src;
102  struct rtnl_nexthop *nh, *new;
103 
104  if (src->rt_dst)
105  if (!(dst->rt_dst = nl_addr_clone(src->rt_dst)))
106  return -NLE_NOMEM;
107 
108  if (src->rt_src)
109  if (!(dst->rt_src = nl_addr_clone(src->rt_src)))
110  return -NLE_NOMEM;
111 
112  if (src->rt_pref_src)
113  if (!(dst->rt_pref_src = nl_addr_clone(src->rt_pref_src)))
114  return -NLE_NOMEM;
115 
116  /* Will be inc'ed again while adding the nexthops of the source */
117  dst->rt_nr_nh = 0;
118 
119  nl_init_list_head(&dst->rt_nexthops);
120  nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
121  new = rtnl_route_nh_clone(nh);
122  if (!new)
123  return -NLE_NOMEM;
124 
125  rtnl_route_add_nexthop(dst, new);
126  }
127 
128  return 0;
129 }
130 
131 static void route_dump_line(struct nl_object *a, struct nl_dump_params *p)
132 {
133  struct rtnl_route *r = (struct rtnl_route *) a;
134  int cache = 0, flags;
135  char buf[64];
136 
137  if (r->rt_flags & RTM_F_CLONED)
138  cache = 1;
139 
140  nl_dump_line(p, "%s ", nl_af2str(r->rt_family, buf, sizeof(buf)));
141 
142  if (cache)
143  nl_dump(p, "cache ");
144 
145  if (!(r->ce_mask & ROUTE_ATTR_DST) ||
146  nl_addr_get_len(r->rt_dst) == 0)
147  nl_dump(p, "default ");
148  else
149  nl_dump(p, "%s ", nl_addr2str(r->rt_dst, buf, sizeof(buf)));
150 
151  if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
152  nl_dump(p, "table %s ",
153  rtnl_route_table2str(r->rt_table, buf, sizeof(buf)));
154 
155  if (r->ce_mask & ROUTE_ATTR_TYPE)
156  nl_dump(p, "type %s ",
157  nl_rtntype2str(r->rt_type, buf, sizeof(buf)));
158 
159  if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
160  nl_dump(p, "tos %#x ", r->rt_tos);
161 
162  if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
163  struct rtnl_nexthop *nh;
164 
165  nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
166  p->dp_ivar = NH_DUMP_FROM_ONELINE;
167  rtnl_route_nh_dump(nh, p);
168  }
169  }
170 
171  flags = r->rt_flags & ~(RTM_F_CLONED);
172  if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
173 
174  nl_dump(p, "<");
175 
176 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
177  flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
178  PRINT_FLAG(DEAD);
179  PRINT_FLAG(ONLINK);
180  PRINT_FLAG(PERVASIVE);
181 #undef PRINT_FLAG
182 
183 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
184  flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
185  PRINT_FLAG(NOTIFY);
186  PRINT_FLAG(EQUALIZE);
187  PRINT_FLAG(PREFIX);
188 #undef PRINT_FLAG
189 
190 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
191  flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
192  PRINT_FLAG(NOTIFY);
193  PRINT_FLAG(REDIRECTED);
194  PRINT_FLAG(DOREDIRECT);
195  PRINT_FLAG(DIRECTSRC);
196  PRINT_FLAG(DNAT);
197  PRINT_FLAG(BROADCAST);
198  PRINT_FLAG(MULTICAST);
199  PRINT_FLAG(LOCAL);
200 #undef PRINT_FLAG
201 
202  nl_dump(p, ">");
203  }
204 
205  nl_dump(p, "\n");
206 }
207 
208 static void route_dump_details(struct nl_object *a, struct nl_dump_params *p)
209 {
210  struct rtnl_route *r = (struct rtnl_route *) a;
211  struct nl_cache *link_cache;
212  char buf[256];
213  int i;
214 
215  link_cache = nl_cache_mngt_require_safe("route/link");
216 
217  route_dump_line(a, p);
218  nl_dump_line(p, " ");
219 
220  if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
221  nl_dump(p, "preferred-src %s ",
222  nl_addr2str(r->rt_pref_src, buf, sizeof(buf)));
223 
224  if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
225  nl_dump(p, "scope %s ",
226  rtnl_scope2str(r->rt_scope, buf, sizeof(buf)));
227 
228  if (r->ce_mask & ROUTE_ATTR_PRIO)
229  nl_dump(p, "priority %#x ", r->rt_prio);
230 
231  if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
232  nl_dump(p, "protocol %s ",
233  rtnl_route_proto2str(r->rt_protocol, buf, sizeof(buf)));
234 
235  if (r->ce_mask & ROUTE_ATTR_IIF) {
236  if (link_cache) {
237  nl_dump(p, "iif %s ",
238  rtnl_link_i2name(link_cache, r->rt_iif,
239  buf, sizeof(buf)));
240  } else
241  nl_dump(p, "iif %d ", r->rt_iif);
242  }
243 
244  if (r->ce_mask & ROUTE_ATTR_SRC)
245  nl_dump(p, "src %s ", nl_addr2str(r->rt_src, buf, sizeof(buf)));
246 
247  nl_dump(p, "\n");
248 
249  if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
250  struct rtnl_nexthop *nh;
251 
252  nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
253  nl_dump_line(p, " ");
254  p->dp_ivar = NH_DUMP_FROM_DETAILS;
255  rtnl_route_nh_dump(nh, p);
256  nl_dump(p, "\n");
257  }
258  }
259 
260  if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
261  nl_dump_line(p, " cacheinfo error %d (%s)\n",
262  r->rt_cacheinfo.rtci_error,
263  nl_strerror_l(-r->rt_cacheinfo.rtci_error));
264  }
265 
266  if (r->ce_mask & ROUTE_ATTR_METRICS) {
267  nl_dump_line(p, " metrics [");
268  for (i = 0; i < RTAX_MAX; i++)
269  if (r->rt_metrics_mask & (1 << i))
270  nl_dump(p, "%s %u ",
271  rtnl_route_metric2str(i+1,
272  buf, sizeof(buf)),
273  r->rt_metrics[i]);
274  nl_dump(p, "]\n");
275  }
276 
277  if (link_cache)
278  nl_cache_put(link_cache);
279 }
280 
281 static void route_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
282 {
283  struct rtnl_route *route = (struct rtnl_route *) obj;
284 
285  route_dump_details(obj, p);
286 
287  if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
288  struct rtnl_rtcacheinfo *ci = &route->rt_cacheinfo;
289 
290  nl_dump_line(p, " used %u refcnt %u last-use %us "
291  "expires %us\n",
292  ci->rtci_used, ci->rtci_clntref,
293  ci->rtci_last_use / nl_get_user_hz(),
294  ci->rtci_expires / nl_get_user_hz());
295  }
296 }
297 
298 static void route_keygen(struct nl_object *obj, uint32_t *hashkey,
299  uint32_t table_sz)
300 {
301  struct rtnl_route *route = (struct rtnl_route *) obj;
302  unsigned int rkey_sz;
303  struct nl_addr *addr = NULL;
304  struct route_hash_key {
305  uint8_t rt_family;
306  uint8_t rt_tos;
307  uint32_t rt_table;
308  uint32_t rt_prio;
309  char rt_addr[0];
310  } __attribute__((packed)) *rkey;
311 #ifdef NL_DEBUG
312  char buf[INET6_ADDRSTRLEN+5];
313 #endif
314 
315  if (route->rt_dst)
316  addr = route->rt_dst;
317 
318  rkey_sz = sizeof(*rkey);
319  if (addr)
320  rkey_sz += nl_addr_get_len(addr);
321  rkey = calloc(1, rkey_sz);
322  if (!rkey) {
323  NL_DBG(2, "Warning: calloc failed for %d bytes...\n", rkey_sz);
324  *hashkey = 0;
325  return;
326  }
327  rkey->rt_family = route->rt_family;
328  rkey->rt_tos = route->rt_tos;
329  rkey->rt_table = route->rt_table;
330  rkey->rt_prio = route->rt_prio;
331  if (addr)
332  memcpy(rkey->rt_addr, nl_addr_get_binary_addr(addr),
333  nl_addr_get_len(addr));
334 
335  *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
336 
337  NL_DBG(5, "route %p key (fam %d tos %d table %d addr %s) keysz %d "
338  "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
339  rkey->rt_table, nl_addr2str(addr, buf, sizeof(buf)),
340  rkey_sz, *hashkey);
341 
342  free(rkey);
343 
344  return;
345 }
346 
347 static uint64_t route_compare(struct nl_object *_a, struct nl_object *_b,
348  uint64_t attrs, int flags)
349 {
350  struct rtnl_route *a = (struct rtnl_route *) _a;
351  struct rtnl_route *b = (struct rtnl_route *) _b;
352  struct rtnl_nexthop *nh_a, *nh_b;
353  int i, found;
354  uint64_t diff = 0;
355 
356 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
357 
358  diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
359  diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
360  diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
361  diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
362  diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
363  diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
364  diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
365  diff |= ROUTE_DIFF(DST, nl_addr_cmp(a->rt_dst, b->rt_dst));
366  diff |= ROUTE_DIFF(SRC, nl_addr_cmp(a->rt_src, b->rt_src));
367  diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
368  diff |= ROUTE_DIFF(PREF_SRC, nl_addr_cmp(a->rt_pref_src,
369  b->rt_pref_src));
370 
371  if (flags & LOOSE_COMPARISON) {
372  nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
373  found = 0;
374  nl_list_for_each_entry(nh_a, &a->rt_nexthops,
375  rtnh_list) {
376  if (!rtnl_route_nh_compare(nh_a, nh_b,
377  nh_b->ce_mask, 1)) {
378  found = 1;
379  break;
380  }
381  }
382 
383  if (!found)
384  goto nh_mismatch;
385  }
386 
387  for (i = 0; i < RTAX_MAX - 1; i++) {
388  if (a->rt_metrics_mask & (1 << i) &&
389  (!(b->rt_metrics_mask & (1 << i)) ||
390  a->rt_metrics[i] != b->rt_metrics[i]))
391  diff |= ROUTE_DIFF(METRICS, 1);
392  }
393 
394  diff |= ROUTE_DIFF(FLAGS,
395  (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
396  } else {
397  if (a->rt_nr_nh != b->rt_nr_nh)
398  goto nh_mismatch;
399 
400  /* search for a dup in each nh of a */
401  nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
402  found = 0;
403  nl_list_for_each_entry(nh_b, &b->rt_nexthops,
404  rtnh_list) {
405  if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
406  found = 1;
407  break;
408  }
409  }
410  if (!found)
411  goto nh_mismatch;
412  }
413 
414  /* search for a dup in each nh of b, covers case where a has
415  * dupes itself */
416  nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
417  found = 0;
418  nl_list_for_each_entry(nh_a, &a->rt_nexthops,
419  rtnh_list) {
420  if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
421  found = 1;
422  break;
423  }
424  }
425  if (!found)
426  goto nh_mismatch;
427  }
428 
429  for (i = 0; i < RTAX_MAX - 1; i++) {
430  if ((a->rt_metrics_mask & (1 << i)) ^
431  (b->rt_metrics_mask & (1 << i)))
432  diff |= ROUTE_DIFF(METRICS, 1);
433  else
434  diff |= ROUTE_DIFF(METRICS,
435  a->rt_metrics[i] != b->rt_metrics[i]);
436  }
437 
438  diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
439  }
440 
441 out:
442  return diff;
443 
444 nh_mismatch:
445  diff |= ROUTE_DIFF(MULTIPATH, 1);
446  goto out;
447 
448 #undef ROUTE_DIFF
449 }
450 
451 static int route_update(struct nl_object *old_obj, struct nl_object *new_obj)
452 {
453  struct rtnl_route *new_route = (struct rtnl_route *) new_obj;
454  struct rtnl_route *old_route = (struct rtnl_route *) old_obj;
455  struct rtnl_nexthop *new_nh;
456  int action = new_obj->ce_msgtype;
457 #ifdef NL_DEBUG
458  char buf[INET6_ADDRSTRLEN+5];
459 #endif
460 
461  /*
462  * ipv6 ECMP route notifications from the kernel come as
463  * separate notifications, one for every nexthop. This update
464  * function collapses such route msgs into a single
465  * route with multiple nexthops. The resulting object looks
466  * similar to a ipv4 ECMP route
467  */
468  if (new_route->rt_family != AF_INET6 ||
469  new_route->rt_table == RT_TABLE_LOCAL)
470  return -NLE_OPNOTSUPP;
471 
472  /*
473  * For routes that are already multipath,
474  * or dont have a nexthop dont do anything
475  */
476  if (rtnl_route_get_nnexthops(new_route) != 1)
477  return -NLE_OPNOTSUPP;
478 
479  /*
480  * Get the only nexthop entry from the new route. For
481  * IPv6 we always get a route with a 0th NH
482  * filled or nothing at all
483  */
484  new_nh = rtnl_route_nexthop_n(new_route, 0);
485  if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
486  return -NLE_OPNOTSUPP;
487 
488  switch(action) {
489  case RTM_NEWROUTE : {
490  struct rtnl_nexthop *cloned_nh;
491 
492  /*
493  * Add the nexthop to old route
494  */
495  cloned_nh = rtnl_route_nh_clone(new_nh);
496  if (!cloned_nh)
497  return -NLE_NOMEM;
498  rtnl_route_add_nexthop(old_route, cloned_nh);
499 
500  NL_DBG(2, "Route obj %p updated. Added "
501  "nexthop %p via %s\n", old_route, cloned_nh,
502  nl_addr2str(cloned_nh->rtnh_gateway, buf,
503  sizeof(buf)));
504  }
505  break;
506  case RTM_DELROUTE : {
507  struct rtnl_nexthop *old_nh;
508 
509  /*
510  * Only take care of nexthop deletes and not
511  * route deletes. So, if there is only one nexthop
512  * quite likely we did not update it. So dont do
513  * anything and return
514  */
515  if (rtnl_route_get_nnexthops(old_route) <= 1)
516  return -NLE_OPNOTSUPP;
517 
518  /*
519  * Find the next hop in old route and delete it
520  */
521  nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
522  rtnh_list) {
523  if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
524 
525  rtnl_route_remove_nexthop(old_route, old_nh);
526 
527  NL_DBG(2, "Route obj %p updated. Removed "
528  "nexthop %p via %s\n", old_route,
529  old_nh,
530  nl_addr2str(old_nh->rtnh_gateway, buf,
531  sizeof(buf)));
532 
533  rtnl_route_nh_free(old_nh);
534  break;
535  }
536  }
537  }
538  break;
539  default:
540  NL_DBG(2, "Unknown action associated "
541  "to object %p during route update\n", new_obj);
542  return -NLE_OPNOTSUPP;
543  }
544 
545  return NLE_SUCCESS;
546 }
547 
548 static const struct trans_tbl route_attrs[] = {
549  __ADD(ROUTE_ATTR_FAMILY, family),
550  __ADD(ROUTE_ATTR_TOS, tos),
551  __ADD(ROUTE_ATTR_TABLE, table),
552  __ADD(ROUTE_ATTR_PROTOCOL, protocol),
553  __ADD(ROUTE_ATTR_SCOPE, scope),
554  __ADD(ROUTE_ATTR_TYPE, type),
555  __ADD(ROUTE_ATTR_FLAGS, flags),
556  __ADD(ROUTE_ATTR_DST, dst),
557  __ADD(ROUTE_ATTR_SRC, src),
558  __ADD(ROUTE_ATTR_IIF, iif),
559  __ADD(ROUTE_ATTR_OIF, oif),
560  __ADD(ROUTE_ATTR_GATEWAY, gateway),
561  __ADD(ROUTE_ATTR_PRIO, prio),
562  __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
563  __ADD(ROUTE_ATTR_METRICS, metrics),
564  __ADD(ROUTE_ATTR_MULTIPATH, multipath),
565  __ADD(ROUTE_ATTR_REALMS, realms),
566  __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
567 };
568 
569 static char *route_attrs2str(int attrs, char *buf, size_t len)
570 {
571  return __flags2str(attrs, buf, len, route_attrs,
572  ARRAY_SIZE(route_attrs));
573 }
574 
575 /**
576  * @name Allocation/Freeing
577  * @{
578  */
579 
580 struct rtnl_route *rtnl_route_alloc(void)
581 {
582  return (struct rtnl_route *) nl_object_alloc(&route_obj_ops);
583 }
584 
585 void rtnl_route_get(struct rtnl_route *route)
586 {
587  nl_object_get((struct nl_object *) route);
588 }
589 
590 void rtnl_route_put(struct rtnl_route *route)
591 {
592  nl_object_put((struct nl_object *) route);
593 }
594 
595 /** @} */
596 
597 /**
598  * @name Attributes
599  * @{
600  */
601 
602 void rtnl_route_set_table(struct rtnl_route *route, uint32_t table)
603 {
604  route->rt_table = table;
605  route->ce_mask |= ROUTE_ATTR_TABLE;
606 }
607 
608 uint32_t rtnl_route_get_table(struct rtnl_route *route)
609 {
610  return route->rt_table;
611 }
612 
613 void rtnl_route_set_scope(struct rtnl_route *route, uint8_t scope)
614 {
615  route->rt_scope = scope;
616  route->ce_mask |= ROUTE_ATTR_SCOPE;
617 }
618 
619 uint8_t rtnl_route_get_scope(struct rtnl_route *route)
620 {
621  return route->rt_scope;
622 }
623 
624 void rtnl_route_set_tos(struct rtnl_route *route, uint8_t tos)
625 {
626  route->rt_tos = tos;
627  route->ce_mask |= ROUTE_ATTR_TOS;
628 }
629 
630 uint8_t rtnl_route_get_tos(struct rtnl_route *route)
631 {
632  return route->rt_tos;
633 }
634 
635 void rtnl_route_set_protocol(struct rtnl_route *route, uint8_t protocol)
636 {
637  route->rt_protocol = protocol;
638  route->ce_mask |= ROUTE_ATTR_PROTOCOL;
639 }
640 
641 uint8_t rtnl_route_get_protocol(struct rtnl_route *route)
642 {
643  return route->rt_protocol;
644 }
645 
646 void rtnl_route_set_priority(struct rtnl_route *route, uint32_t prio)
647 {
648  route->rt_prio = prio;
649  route->ce_mask |= ROUTE_ATTR_PRIO;
650 }
651 
652 uint32_t rtnl_route_get_priority(struct rtnl_route *route)
653 {
654  return route->rt_prio;
655 }
656 
657 int rtnl_route_set_family(struct rtnl_route *route, uint8_t family)
658 {
659  if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
660  return -NLE_AF_NOSUPPORT;
661 
662  route->rt_family = family;
663  route->ce_mask |= ROUTE_ATTR_FAMILY;
664 
665  return 0;
666 }
667 
668 uint8_t rtnl_route_get_family(struct rtnl_route *route)
669 {
670  return route->rt_family;
671 }
672 
673 int rtnl_route_set_dst(struct rtnl_route *route, struct nl_addr *addr)
674 {
675  if (route->ce_mask & ROUTE_ATTR_FAMILY) {
676  if (addr->a_family != route->rt_family)
677  return -NLE_AF_MISMATCH;
678  } else
679  route->rt_family = addr->a_family;
680 
681  if (route->rt_dst)
682  nl_addr_put(route->rt_dst);
683 
684  nl_addr_get(addr);
685  route->rt_dst = addr;
686 
687  route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
688 
689  return 0;
690 }
691 
692 struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route)
693 {
694  return route->rt_dst;
695 }
696 
697 int rtnl_route_set_src(struct rtnl_route *route, struct nl_addr *addr)
698 {
699  if (addr->a_family == AF_INET)
700  return -NLE_SRCRT_NOSUPPORT;
701 
702  if (route->ce_mask & ROUTE_ATTR_FAMILY) {
703  if (addr->a_family != route->rt_family)
704  return -NLE_AF_MISMATCH;
705  } else
706  route->rt_family = addr->a_family;
707 
708  if (route->rt_src)
709  nl_addr_put(route->rt_src);
710 
711  nl_addr_get(addr);
712  route->rt_src = addr;
713  route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
714 
715  return 0;
716 }
717 
718 struct nl_addr *rtnl_route_get_src(struct rtnl_route *route)
719 {
720  return route->rt_src;
721 }
722 
723 int rtnl_route_set_type(struct rtnl_route *route, uint8_t type)
724 {
725  if (type > RTN_MAX)
726  return -NLE_RANGE;
727 
728  route->rt_type = type;
729  route->ce_mask |= ROUTE_ATTR_TYPE;
730 
731  return 0;
732 }
733 
734 uint8_t rtnl_route_get_type(struct rtnl_route *route)
735 {
736  return route->rt_type;
737 }
738 
739 void rtnl_route_set_flags(struct rtnl_route *route, uint32_t flags)
740 {
741  route->rt_flag_mask |= flags;
742  route->rt_flags |= flags;
743  route->ce_mask |= ROUTE_ATTR_FLAGS;
744 }
745 
746 void rtnl_route_unset_flags(struct rtnl_route *route, uint32_t flags)
747 {
748  route->rt_flag_mask |= flags;
749  route->rt_flags &= ~flags;
750  route->ce_mask |= ROUTE_ATTR_FLAGS;
751 }
752 
753 uint32_t rtnl_route_get_flags(struct rtnl_route *route)
754 {
755  return route->rt_flags;
756 }
757 
758 int rtnl_route_set_metric(struct rtnl_route *route, int metric, uint32_t value)
759 {
760  if (metric > RTAX_MAX || metric < 1)
761  return -NLE_RANGE;
762 
763  route->rt_metrics[metric - 1] = value;
764 
765  if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
766  route->rt_nmetrics++;
767  route->rt_metrics_mask |= (1 << (metric - 1));
768  }
769 
770  route->ce_mask |= ROUTE_ATTR_METRICS;
771 
772  return 0;
773 }
774 
775 int rtnl_route_unset_metric(struct rtnl_route *route, int metric)
776 {
777  if (metric > RTAX_MAX || metric < 1)
778  return -NLE_RANGE;
779 
780  if (route->rt_metrics_mask & (1 << (metric - 1))) {
781  route->rt_nmetrics--;
782  route->rt_metrics_mask &= ~(1 << (metric - 1));
783  }
784 
785  return 0;
786 }
787 
788 int rtnl_route_get_metric(struct rtnl_route *route, int metric, uint32_t *value)
789 {
790  if (metric > RTAX_MAX || metric < 1)
791  return -NLE_RANGE;
792 
793  if (!(route->rt_metrics_mask & (1 << (metric - 1))))
794  return -NLE_OBJ_NOTFOUND;
795 
796  if (value)
797  *value = route->rt_metrics[metric - 1];
798 
799  return 0;
800 }
801 
802 int rtnl_route_set_pref_src(struct rtnl_route *route, struct nl_addr *addr)
803 {
804  if (route->ce_mask & ROUTE_ATTR_FAMILY) {
805  if (addr->a_family != route->rt_family)
806  return -NLE_AF_MISMATCH;
807  } else
808  route->rt_family = addr->a_family;
809 
810  if (route->rt_pref_src)
811  nl_addr_put(route->rt_pref_src);
812 
813  nl_addr_get(addr);
814  route->rt_pref_src = addr;
815  route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
816 
817  return 0;
818 }
819 
820 struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route)
821 {
822  return route->rt_pref_src;
823 }
824 
825 void rtnl_route_set_iif(struct rtnl_route *route, int ifindex)
826 {
827  route->rt_iif = ifindex;
828  route->ce_mask |= ROUTE_ATTR_IIF;
829 }
830 
831 int rtnl_route_get_iif(struct rtnl_route *route)
832 {
833  return route->rt_iif;
834 }
835 
836 void rtnl_route_add_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
837 {
838  nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
839  route->rt_nr_nh++;
840  route->ce_mask |= ROUTE_ATTR_MULTIPATH;
841 }
842 
843 void rtnl_route_remove_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
844 {
845  if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
846  route->rt_nr_nh--;
847  nl_list_del(&nh->rtnh_list);
848  }
849 }
850 
851 struct nl_list_head *rtnl_route_get_nexthops(struct rtnl_route *route)
852 {
853  if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
854  return &route->rt_nexthops;
855 
856  return NULL;
857 }
858 
859 int rtnl_route_get_nnexthops(struct rtnl_route *route)
860 {
861  if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
862  return route->rt_nr_nh;
863 
864  return 0;
865 }
866 
867 void rtnl_route_foreach_nexthop(struct rtnl_route *r,
868  void (*cb)(struct rtnl_nexthop *, void *),
869  void *arg)
870 {
871  struct rtnl_nexthop *nh;
872 
873  if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
874  nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
875  cb(nh, arg);
876  }
877  }
878 }
879 
880 struct rtnl_nexthop *rtnl_route_nexthop_n(struct rtnl_route *r, int n)
881 {
882  struct rtnl_nexthop *nh;
883  uint32_t i;
884 
885  if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
886  i = 0;
887  nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
888  if (i == n) return nh;
889  i++;
890  }
891  }
892  return NULL;
893 }
894 
895 /** @} */
896 
897 /**
898  * @name Utilities
899  * @{
900  */
901 
902 /**
903  * Guess scope of a route object.
904  * @arg route Route object.
905  *
906  * Guesses the scope of a route object, based on the following rules:
907  * @code
908  * 1) Local route -> local scope
909  * 2) At least one nexthop not directly connected -> universe scope
910  * 3) All others -> link scope
911  * @endcode
912  *
913  * @return Scope value.
914  */
915 int rtnl_route_guess_scope(struct rtnl_route *route)
916 {
917  if (route->rt_type == RTN_LOCAL)
918  return RT_SCOPE_HOST;
919 
920  if (!nl_list_empty(&route->rt_nexthops)) {
921  struct rtnl_nexthop *nh;
922 
923  /*
924  * Use scope uiniverse if there is at least one nexthop which
925  * is not directly connected
926  */
927  nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
928  if (nh->rtnh_gateway)
929  return RT_SCOPE_UNIVERSE;
930  }
931  }
932 
933  return RT_SCOPE_LINK;
934 }
935 
936 /** @} */
937 
938 static struct nla_policy route_policy[RTA_MAX+1] = {
939  [RTA_IIF] = { .type = NLA_U32 },
940  [RTA_OIF] = { .type = NLA_U32 },
941  [RTA_PRIORITY] = { .type = NLA_U32 },
942  [RTA_FLOW] = { .type = NLA_U32 },
943  [RTA_CACHEINFO] = { .minlen = sizeof(struct rta_cacheinfo) },
944  [RTA_METRICS] = { .type = NLA_NESTED },
945  [RTA_MULTIPATH] = { .type = NLA_NESTED },
946 };
947 
948 static int parse_multipath(struct rtnl_route *route, struct nlattr *attr)
949 {
950  struct rtnl_nexthop *nh = NULL;
951  struct rtnexthop *rtnh = nla_data(attr);
952  size_t tlen = nla_len(attr);
953  int err;
954 
955  while (tlen >= sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
956  nh = rtnl_route_nh_alloc();
957  if (!nh)
958  return -NLE_NOMEM;
959 
960  rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
961  rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
962  rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
963 
964  if (rtnh->rtnh_len > sizeof(*rtnh)) {
965  struct nlattr *ntb[RTA_MAX + 1];
966 
967  err = nla_parse(ntb, RTA_MAX, (struct nlattr *)
968  RTNH_DATA(rtnh),
969  rtnh->rtnh_len - sizeof(*rtnh),
970  route_policy);
971  if (err < 0)
972  goto errout;
973 
974  if (ntb[RTA_GATEWAY]) {
975  struct nl_addr *addr;
976 
977  addr = nl_addr_alloc_attr(ntb[RTA_GATEWAY],
978  route->rt_family);
979  if (!addr) {
980  err = -NLE_NOMEM;
981  goto errout;
982  }
983 
984  rtnl_route_nh_set_gateway(nh, addr);
985  nl_addr_put(addr);
986  }
987 
988  if (ntb[RTA_FLOW]) {
989  uint32_t realms;
990 
991  realms = nla_get_u32(ntb[RTA_FLOW]);
992  rtnl_route_nh_set_realms(nh, realms);
993  }
994  }
995 
996  rtnl_route_add_nexthop(route, nh);
997  tlen -= RTNH_ALIGN(rtnh->rtnh_len);
998  rtnh = RTNH_NEXT(rtnh);
999  }
1000 
1001  err = 0;
1002 errout:
1003  if (err && nh)
1004  rtnl_route_nh_free(nh);
1005 
1006  return err;
1007 }
1008 
1009 int rtnl_route_parse(struct nlmsghdr *nlh, struct rtnl_route **result)
1010 {
1011  struct rtmsg *rtm;
1012  struct rtnl_route *route;
1013  struct nlattr *tb[RTA_MAX + 1];
1014  struct nl_addr *src = NULL, *dst = NULL, *addr;
1015  struct rtnl_nexthop *old_nh = NULL;
1016  int err, family;
1017 
1018  route = rtnl_route_alloc();
1019  if (!route) {
1020  err = -NLE_NOMEM;
1021  goto errout;
1022  }
1023 
1024  route->ce_msgtype = nlh->nlmsg_type;
1025 
1026  err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, route_policy);
1027  if (err < 0)
1028  goto errout;
1029 
1030  rtm = nlmsg_data(nlh);
1031  route->rt_family = family = rtm->rtm_family;
1032  route->rt_tos = rtm->rtm_tos;
1033  route->rt_table = rtm->rtm_table;
1034  route->rt_type = rtm->rtm_type;
1035  route->rt_scope = rtm->rtm_scope;
1036  route->rt_protocol = rtm->rtm_protocol;
1037  route->rt_flags = rtm->rtm_flags;
1038  route->rt_prio = 0;
1039 
1040  route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1041  ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1042  ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1043  ROUTE_ATTR_FLAGS | ROUTE_ATTR_PRIO;
1044 
1045  if (tb[RTA_DST]) {
1046  if (!(dst = nl_addr_alloc_attr(tb[RTA_DST], family)))
1047  goto errout_nomem;
1048  } else {
1049  if (!(dst = nl_addr_alloc(0)))
1050  goto errout_nomem;
1051  nl_addr_set_family(dst, rtm->rtm_family);
1052  }
1053 
1054  nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
1055  err = rtnl_route_set_dst(route, dst);
1056  if (err < 0)
1057  goto errout;
1058 
1059  nl_addr_put(dst);
1060 
1061  if (tb[RTA_SRC]) {
1062  if (!(src = nl_addr_alloc_attr(tb[RTA_SRC], family)))
1063  goto errout_nomem;
1064  } else if (rtm->rtm_src_len)
1065  if (!(src = nl_addr_alloc(0)))
1066  goto errout_nomem;
1067 
1068  if (src) {
1069  nl_addr_set_prefixlen(src, rtm->rtm_src_len);
1070  rtnl_route_set_src(route, src);
1071  nl_addr_put(src);
1072  }
1073 
1074  if (tb[RTA_TABLE])
1075  rtnl_route_set_table(route, nla_get_u32(tb[RTA_TABLE]));
1076 
1077  if (tb[RTA_IIF])
1078  rtnl_route_set_iif(route, nla_get_u32(tb[RTA_IIF]));
1079 
1080  if (tb[RTA_PRIORITY])
1081  rtnl_route_set_priority(route, nla_get_u32(tb[RTA_PRIORITY]));
1082 
1083  if (tb[RTA_PREFSRC]) {
1084  if (!(addr = nl_addr_alloc_attr(tb[RTA_PREFSRC], family)))
1085  goto errout_nomem;
1086  rtnl_route_set_pref_src(route, addr);
1087  nl_addr_put(addr);
1088  }
1089 
1090  if (tb[RTA_METRICS]) {
1091  struct nlattr *mtb[RTAX_MAX + 1];
1092  int i;
1093 
1094  err = nla_parse_nested(mtb, RTAX_MAX, tb[RTA_METRICS], NULL);
1095  if (err < 0)
1096  goto errout;
1097 
1098  for (i = 1; i <= RTAX_MAX; i++) {
1099  if (mtb[i] && nla_len(mtb[i]) >= sizeof(uint32_t)) {
1100  uint32_t m = nla_get_u32(mtb[i]);
1101  if (rtnl_route_set_metric(route, i, m) < 0)
1102  goto errout;
1103  }
1104  }
1105  }
1106 
1107  if (tb[RTA_MULTIPATH])
1108  if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1109  goto errout;
1110 
1111  if (tb[RTA_CACHEINFO]) {
1112  nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1113  sizeof(route->rt_cacheinfo));
1114  route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1115  }
1116 
1117  if (tb[RTA_OIF]) {
1118  if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1119  goto errout;
1120 
1121  rtnl_route_nh_set_ifindex(old_nh, nla_get_u32(tb[RTA_OIF]));
1122  }
1123 
1124  if (tb[RTA_GATEWAY]) {
1125  if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1126  goto errout;
1127 
1128  if (!(addr = nl_addr_alloc_attr(tb[RTA_GATEWAY], family)))
1129  goto errout_nomem;
1130 
1131  rtnl_route_nh_set_gateway(old_nh, addr);
1132  nl_addr_put(addr);
1133  }
1134 
1135  if (tb[RTA_FLOW]) {
1136  if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1137  goto errout;
1138 
1139  rtnl_route_nh_set_realms(old_nh, nla_get_u32(tb[RTA_FLOW]));
1140  }
1141 
1142  if (old_nh) {
1143  rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1144  if (route->rt_nr_nh == 0) {
1145  /* If no nexthops have been provided via RTA_MULTIPATH
1146  * we add it as regular nexthop to maintain backwards
1147  * compatibility */
1148  rtnl_route_add_nexthop(route, old_nh);
1149  } else {
1150  /* Kernel supports new style nexthop configuration,
1151  * verify that it is a duplicate and discard nexthop. */
1152  struct rtnl_nexthop *first;
1153 
1154  first = nl_list_first_entry(&route->rt_nexthops,
1155  struct rtnl_nexthop,
1156  rtnh_list);
1157  if (!first)
1158  BUG();
1159 
1160  if (rtnl_route_nh_compare(old_nh, first,
1161  old_nh->ce_mask, 0)) {
1162  err = -NLE_INVAL;
1163  goto errout;
1164  }
1165 
1166  rtnl_route_nh_free(old_nh);
1167  }
1168  }
1169 
1170  *result = route;
1171  return 0;
1172 
1173 errout:
1174  rtnl_route_put(route);
1175  return err;
1176 
1177 errout_nomem:
1178  err = -NLE_NOMEM;
1179  goto errout;
1180 }
1181 
1182 int rtnl_route_build_msg(struct nl_msg *msg, struct rtnl_route *route)
1183 {
1184  int i;
1185  struct nlattr *metrics;
1186  struct rtmsg rtmsg = {
1187  .rtm_family = route->rt_family,
1188  .rtm_tos = route->rt_tos,
1189  .rtm_table = route->rt_table,
1190  .rtm_protocol = route->rt_protocol,
1191  .rtm_scope = route->rt_scope,
1192  .rtm_type = route->rt_type,
1193  .rtm_flags = route->rt_flags,
1194  };
1195 
1196  if (route->rt_dst == NULL)
1197  return -NLE_MISSING_ATTR;
1198 
1199  rtmsg.rtm_dst_len = nl_addr_get_prefixlen(route->rt_dst);
1200  if (route->rt_src)
1201  rtmsg.rtm_src_len = nl_addr_get_prefixlen(route->rt_src);
1202 
1203  if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1204  rtmsg.rtm_scope = rtnl_route_guess_scope(route);
1205 
1206  if (rtnl_route_get_nnexthops(route) == 1) {
1207  struct rtnl_nexthop *nh;
1208  nh = rtnl_route_nexthop_n(route, 0);
1209  rtmsg.rtm_flags |= nh->rtnh_flags;
1210  }
1211 
1212  if (nlmsg_append(msg, &rtmsg, sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1213  goto nla_put_failure;
1214 
1215  /* Additional table attribute replacing the 8bit in the header, was
1216  * required to allow more than 256 tables. */
1217  NLA_PUT_U32(msg, RTA_TABLE, route->rt_table);
1218 
1219  if (nl_addr_get_len(route->rt_dst))
1220  NLA_PUT_ADDR(msg, RTA_DST, route->rt_dst);
1221  NLA_PUT_U32(msg, RTA_PRIORITY, route->rt_prio);
1222 
1223  if (route->ce_mask & ROUTE_ATTR_SRC)
1224  NLA_PUT_ADDR(msg, RTA_SRC, route->rt_src);
1225 
1226  if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1227  NLA_PUT_ADDR(msg, RTA_PREFSRC, route->rt_pref_src);
1228 
1229  if (route->ce_mask & ROUTE_ATTR_IIF)
1230  NLA_PUT_U32(msg, RTA_IIF, route->rt_iif);
1231 
1232  if (route->rt_nmetrics > 0) {
1233  uint32_t val;
1234 
1235  metrics = nla_nest_start(msg, RTA_METRICS);
1236  if (metrics == NULL)
1237  goto nla_put_failure;
1238 
1239  for (i = 1; i <= RTAX_MAX; i++) {
1240  if (!rtnl_route_get_metric(route, i, &val))
1241  NLA_PUT_U32(msg, i, val);
1242  }
1243 
1244  nla_nest_end(msg, metrics);
1245  }
1246 
1247  if (rtnl_route_get_nnexthops(route) == 1) {
1248  struct rtnl_nexthop *nh;
1249 
1250  nh = rtnl_route_nexthop_n(route, 0);
1251  if (nh->rtnh_gateway)
1252  NLA_PUT_ADDR(msg, RTA_GATEWAY, nh->rtnh_gateway);
1253  if (nh->rtnh_ifindex)
1254  NLA_PUT_U32(msg, RTA_OIF, nh->rtnh_ifindex);
1255  if (nh->rtnh_realms)
1256  NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
1257  } else if (rtnl_route_get_nnexthops(route) > 1) {
1258  struct nlattr *multipath;
1259  struct rtnl_nexthop *nh;
1260 
1261  if (!(multipath = nla_nest_start(msg, RTA_MULTIPATH)))
1262  goto nla_put_failure;
1263 
1264  nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1265  struct rtnexthop *rtnh;
1266 
1267  rtnh = nlmsg_reserve(msg, sizeof(*rtnh), NLMSG_ALIGNTO);
1268  if (!rtnh)
1269  goto nla_put_failure;
1270 
1271  rtnh->rtnh_flags = nh->rtnh_flags;
1272  rtnh->rtnh_hops = nh->rtnh_weight;
1273  rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1274 
1275  if (nh->rtnh_gateway)
1276  NLA_PUT_ADDR(msg, RTA_GATEWAY,
1277  nh->rtnh_gateway);
1278 
1279  if (nh->rtnh_realms)
1280  NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
1281 
1282  rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1283  (void *) rtnh;
1284  }
1285 
1286  nla_nest_end(msg, multipath);
1287  }
1288 
1289  return 0;
1290 
1291 nla_put_failure:
1292  return -NLE_MSGSIZE;
1293 }
1294 
1295 /** @cond SKIP */
1296 struct nl_object_ops route_obj_ops = {
1297  .oo_name = "route/route",
1298  .oo_size = sizeof(struct rtnl_route),
1299  .oo_constructor = route_constructor,
1300  .oo_free_data = route_free_data,
1301  .oo_clone = route_clone,
1302  .oo_dump = {
1303  [NL_DUMP_LINE] = route_dump_line,
1304  [NL_DUMP_DETAILS] = route_dump_details,
1305  [NL_DUMP_STATS] = route_dump_stats,
1306  },
1307  .oo_compare = route_compare,
1308  .oo_keygen = route_keygen,
1309  .oo_update = route_update,
1310  .oo_attrs2str = route_attrs2str,
1311  .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1312  ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1313  ROUTE_ATTR_PRIO),
1314 };
1315 /** @endcond */
1316 
1317 /** @} */
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
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
Definition: addr.c:185
int nl_get_user_hz(void)
Return the value of HZ.
Definition: utils.c:489
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
Definition: addr.c:929
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:106
#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:940
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
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
Definition: msg.c:408
Attribute validation policy.
Definition: attr.h:67
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
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:214
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Definition: addr.c:501
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
Definition: addr.c:844
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
Definition: addr.c:255
Dump all attributes but no statistics.
Definition: types.h:23
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
Definition: attr.c:917
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
Definition: attr.c:353
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
Definition: attr.c:992
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
Definition: attr.h:233
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition: attr.c:131
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, struct nla_policy *policy)
Create attribute index based on a stream of attributes.
Definition: attr.c:242
int rtnl_route_guess_scope(struct rtnl_route *route)
Guess scope of a route object.
Definition: route_obj.c:915
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:443
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
Definition: object.c:215
Nested attributes.
Definition: attr.h:46
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
32 bit integer
Definition: attr.h:41
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:938
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
Definition: addr.c:917
int dp_ivar
PRIVATE Owned by the current caller.
Definition: types.h:105
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:905
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
Definition: attr.c:895
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
Definition: addr.c:963