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