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