27 #include <netlink-private/netlink.h>
28 #include <netlink-private/utils.h>
29 #include <netlink-private/route/nexthop-encap.h>
30 #include <netlink/netlink.h>
31 #include <netlink/cache.h>
32 #include <netlink/utils.h>
33 #include <netlink/data.h>
34 #include <netlink/hashtable.h>
35 #include <netlink/route/rtnl.h>
36 #include <netlink/route/route.h>
37 #include <netlink/route/link.h>
38 #include <netlink/route/nexthop.h>
39 #include <linux/in_route.h>
42 #define ROUTE_ATTR_FAMILY 0x000001
43 #define ROUTE_ATTR_TOS 0x000002
44 #define ROUTE_ATTR_TABLE 0x000004
45 #define ROUTE_ATTR_PROTOCOL 0x000008
46 #define ROUTE_ATTR_SCOPE 0x000010
47 #define ROUTE_ATTR_TYPE 0x000020
48 #define ROUTE_ATTR_FLAGS 0x000040
49 #define ROUTE_ATTR_DST 0x000080
50 #define ROUTE_ATTR_SRC 0x000100
51 #define ROUTE_ATTR_IIF 0x000200
52 #define ROUTE_ATTR_OIF 0x000400
53 #define ROUTE_ATTR_GATEWAY 0x000800
54 #define ROUTE_ATTR_PRIO 0x001000
55 #define ROUTE_ATTR_PREF_SRC 0x002000
56 #define ROUTE_ATTR_METRICS 0x004000
57 #define ROUTE_ATTR_MULTIPATH 0x008000
58 #define ROUTE_ATTR_REALMS 0x010000
59 #define ROUTE_ATTR_CACHEINFO 0x020000
60 #define ROUTE_ATTR_TTL_PROPAGATE 0x040000
63 static void route_constructor(
struct nl_object *c)
65 struct rtnl_route *r = (
struct rtnl_route *) c;
67 r->rt_family = AF_UNSPEC;
68 r->rt_scope = RT_SCOPE_NOWHERE;
69 r->rt_table = RT_TABLE_MAIN;
70 r->rt_protocol = RTPROT_STATIC;
71 r->rt_type = RTN_UNICAST;
74 nl_init_list_head(&r->rt_nexthops);
77 static void route_free_data(
struct nl_object *c)
79 struct rtnl_route *r = (
struct rtnl_route *) c;
80 struct rtnl_nexthop *nh, *tmp;
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);
95 static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
97 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
98 struct rtnl_route *src = (
struct rtnl_route *) _src;
99 struct rtnl_nexthop *nh, *
new;
103 dst->rt_pref_src = NULL;
104 nl_init_list_head(&dst->rt_nexthops);
117 if (src->rt_pref_src) {
122 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
123 new = rtnl_route_nh_clone(nh);
127 rtnl_route_add_nexthop(dst,
new);
133 static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
135 struct rtnl_route *r = (
struct rtnl_route *) a;
136 int cache = 0, flags;
139 if (r->rt_flags & RTM_F_CLONED)
142 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
147 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
153 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
155 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
157 if (r->ce_mask & ROUTE_ATTR_TYPE)
159 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
161 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
162 nl_dump(p,
"tos %#x ", r->rt_tos);
164 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
165 struct rtnl_nexthop *nh;
167 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
168 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
169 rtnl_route_nh_dump(nh, p);
173 flags = r->rt_flags & ~(RTM_F_CLONED);
174 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
178 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
179 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
182 PRINT_FLAG(PERVASIVE);
185 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
186 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
188 PRINT_FLAG(EQUALIZE);
192 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
193 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
195 PRINT_FLAG(REDIRECTED);
196 PRINT_FLAG(DOREDIRECT);
197 PRINT_FLAG(DIRECTSRC);
199 PRINT_FLAG(BROADCAST);
200 PRINT_FLAG(MULTICAST);
210 static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
212 _nl_auto_nl_cache
struct nl_cache *link_cache = NULL;
213 struct rtnl_route *r = (
struct rtnl_route *) a;
219 route_dump_line(a, p);
220 nl_dump_line(p,
" ");
222 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
223 nl_dump(p,
"preferred-src %s ",
226 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
228 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
230 if (r->ce_mask & ROUTE_ATTR_PRIO)
231 nl_dump(p,
"priority %#x ", r->rt_prio);
233 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
235 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
237 if (r->ce_mask & ROUTE_ATTR_IIF) {
243 nl_dump(p,
"iif %d ", r->rt_iif);
246 if (r->ce_mask & ROUTE_ATTR_SRC)
249 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
250 nl_dump(p,
" ttl-propagate %s",
251 r->rt_ttl_propagate ?
"enabled" :
"disabled");
256 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
257 struct rtnl_nexthop *nh;
259 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
260 nl_dump_line(p,
" ");
261 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
262 rtnl_route_nh_dump(nh, p);
267 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
268 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
269 r->rt_cacheinfo.rtci_error,
270 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
273 if (r->ce_mask & ROUTE_ATTR_METRICS) {
274 nl_dump_line(p,
" metrics [");
275 for (i = 0; i < RTAX_MAX; i++)
276 if (r->rt_metrics_mask & (1 << i))
278 rtnl_route_metric2str(i+1,
285 static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
287 struct rtnl_route *route = (
struct rtnl_route *) obj;
289 route_dump_details(obj, p);
291 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
294 nl_dump_line(p,
" used %u refcnt %u last-use %us "
296 ci->rtci_used, ci->rtci_clntref,
302 static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
305 struct rtnl_route *route = (
struct rtnl_route *) obj;
306 unsigned int rkey_sz;
307 struct nl_addr *addr = NULL;
308 _nl_auto_free
struct route_hash_key {
314 } __attribute__((packed)) *rkey = NULL;
316 char buf[INET6_ADDRSTRLEN+5];
320 addr = route->rt_dst;
322 rkey_sz =
sizeof(*rkey);
325 rkey = calloc(1, rkey_sz);
327 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
331 rkey->rt_family = route->rt_family;
332 rkey->rt_tos = route->rt_tos;
333 rkey->rt_table = route->rt_table;
334 rkey->rt_prio = route->rt_prio;
339 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
341 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d "
342 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
343 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
349 static uint32_t route_id_attrs_get(
struct nl_object *obj)
351 struct rtnl_route *route = (
struct rtnl_route *)obj;
352 struct nl_object_ops *ops = obj->ce_ops;
353 uint32_t rv = ops->oo_id_attrs;
356 if (route->rt_family == AF_MPLS)
357 rv &= ~ROUTE_ATTR_PRIO;
362 static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
363 uint64_t attrs,
int flags)
365 struct rtnl_route *a = (
struct rtnl_route *) _a;
366 struct rtnl_route *b = (
struct rtnl_route *) _b;
367 struct rtnl_nexthop *nh_a, *nh_b;
371 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
373 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
374 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
375 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
376 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
377 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
378 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
379 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
380 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
381 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
382 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
383 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
385 diff |= ROUTE_DIFF(TTL_PROPAGATE,
386 a->rt_ttl_propagate != b->rt_ttl_propagate);
388 if (flags & LOOSE_COMPARISON) {
389 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
391 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
393 if (!rtnl_route_nh_compare(nh_a, nh_b,
404 for (i = 0; i < RTAX_MAX - 1; i++) {
405 if (a->rt_metrics_mask & (1 << i) &&
406 (!(b->rt_metrics_mask & (1 << i)) ||
407 a->rt_metrics[i] != b->rt_metrics[i]))
408 diff |= ROUTE_DIFF(METRICS, 1);
411 diff |= ROUTE_DIFF(FLAGS,
412 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
414 if (a->rt_nr_nh != b->rt_nr_nh)
418 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
420 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
422 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
433 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
435 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
437 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
446 for (i = 0; i < RTAX_MAX - 1; i++) {
447 if ((a->rt_metrics_mask & (1 << i)) ^
448 (b->rt_metrics_mask & (1 << i)))
449 diff |= ROUTE_DIFF(METRICS, 1);
451 diff |= ROUTE_DIFF(METRICS,
452 a->rt_metrics[i] != b->rt_metrics[i]);
455 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
462 diff |= ROUTE_DIFF(MULTIPATH, 1);
468 static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
470 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
471 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
472 struct rtnl_nexthop *new_nh;
473 int action = new_obj->ce_msgtype;
475 char buf[INET6_ADDRSTRLEN+5];
485 if (new_route->rt_family != AF_INET6 ||
486 new_route->rt_table == RT_TABLE_LOCAL)
487 return -NLE_OPNOTSUPP;
493 if (rtnl_route_get_nnexthops(new_route) != 1)
494 return -NLE_OPNOTSUPP;
501 new_nh = rtnl_route_nexthop_n(new_route, 0);
502 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
503 return -NLE_OPNOTSUPP;
506 case RTM_NEWROUTE : {
507 struct rtnl_nexthop *cloned_nh;
508 struct rtnl_nexthop *old_nh;
513 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, rtnh_list) {
514 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
522 cloned_nh = rtnl_route_nh_clone(new_nh);
525 rtnl_route_add_nexthop(old_route, cloned_nh);
527 NL_DBG(2,
"Route obj %p updated. Added "
528 "nexthop %p via %s\n", old_route, cloned_nh,
533 case RTM_DELROUTE : {
534 struct rtnl_nexthop *old_nh;
542 if (rtnl_route_get_nnexthops(old_route) <= 1)
543 return -NLE_OPNOTSUPP;
548 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
550 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
552 rtnl_route_remove_nexthop(old_route, old_nh);
554 NL_DBG(2,
"Route obj %p updated. Removed "
555 "nexthop %p via %s\n", old_route,
560 rtnl_route_nh_free(old_nh);
567 NL_DBG(2,
"Unknown action associated "
568 "to object %p during route update\n", new_obj);
569 return -NLE_OPNOTSUPP;
575 static const struct trans_tbl route_attrs[] = {
576 __ADD(ROUTE_ATTR_FAMILY, family),
577 __ADD(ROUTE_ATTR_TOS, tos),
578 __ADD(ROUTE_ATTR_TABLE, table),
579 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
580 __ADD(ROUTE_ATTR_SCOPE, scope),
581 __ADD(ROUTE_ATTR_TYPE, type),
582 __ADD(ROUTE_ATTR_FLAGS, flags),
583 __ADD(ROUTE_ATTR_DST, dst),
584 __ADD(ROUTE_ATTR_SRC, src),
585 __ADD(ROUTE_ATTR_IIF, iif),
586 __ADD(ROUTE_ATTR_OIF, oif),
587 __ADD(ROUTE_ATTR_GATEWAY, gateway),
588 __ADD(ROUTE_ATTR_PRIO, prio),
589 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
590 __ADD(ROUTE_ATTR_METRICS, metrics),
591 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
592 __ADD(ROUTE_ATTR_REALMS, realms),
593 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
594 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
597 static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
599 return __flags2str(attrs, buf, len, route_attrs,
600 ARRAY_SIZE(route_attrs));
608 struct rtnl_route *rtnl_route_alloc(
void)
613 void rtnl_route_get(
struct rtnl_route *route)
618 void rtnl_route_put(
struct rtnl_route *route)
630 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
632 route->rt_table = table;
633 route->ce_mask |= ROUTE_ATTR_TABLE;
636 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
638 return route->rt_table;
641 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
643 route->rt_scope = scope;
644 route->ce_mask |= ROUTE_ATTR_SCOPE;
647 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
649 return route->rt_scope;
652 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
655 route->ce_mask |= ROUTE_ATTR_TOS;
658 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
660 return route->rt_tos;
663 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
665 route->rt_protocol = protocol;
666 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
669 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
671 return route->rt_protocol;
674 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
676 route->rt_prio = prio;
677 route->ce_mask |= ROUTE_ATTR_PRIO;
680 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
682 return route->rt_prio;
685 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
692 route->rt_family = family;
693 route->ce_mask |= ROUTE_ATTR_FAMILY;
697 return -NLE_AF_NOSUPPORT;
700 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
702 return route->rt_family;
705 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
707 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
708 if (addr->a_family != route->rt_family)
709 return -NLE_AF_MISMATCH;
711 route->rt_family = addr->a_family;
717 route->rt_dst = addr;
719 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
724 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
726 return route->rt_dst;
729 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
731 if (addr->a_family == AF_INET)
732 return -NLE_SRCRT_NOSUPPORT;
734 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
735 if (addr->a_family != route->rt_family)
736 return -NLE_AF_MISMATCH;
738 route->rt_family = addr->a_family;
744 route->rt_src = addr;
745 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
750 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
752 return route->rt_src;
755 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
760 route->rt_type = type;
761 route->ce_mask |= ROUTE_ATTR_TYPE;
766 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
768 return route->rt_type;
771 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
773 route->rt_flag_mask |= flags;
774 route->rt_flags |= flags;
775 route->ce_mask |= ROUTE_ATTR_FLAGS;
778 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
780 route->rt_flag_mask |= flags;
781 route->rt_flags &= ~flags;
782 route->ce_mask |= ROUTE_ATTR_FLAGS;
785 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
787 return route->rt_flags;
790 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
792 if (metric > RTAX_MAX || metric < 1)
795 route->rt_metrics[metric - 1] = value;
797 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
798 route->rt_nmetrics++;
799 route->rt_metrics_mask |= (1 << (metric - 1));
802 route->ce_mask |= ROUTE_ATTR_METRICS;
807 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
809 if (metric > RTAX_MAX || metric < 1)
812 if (route->rt_metrics_mask & (1 << (metric - 1))) {
813 route->rt_nmetrics--;
814 route->rt_metrics_mask &= ~(1 << (metric - 1));
820 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
822 if (metric > RTAX_MAX || metric < 1)
825 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
826 return -NLE_OBJ_NOTFOUND;
829 *value = route->rt_metrics[metric - 1];
834 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
836 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
837 if (addr->a_family != route->rt_family)
838 return -NLE_AF_MISMATCH;
840 route->rt_family = addr->a_family;
842 if (route->rt_pref_src)
846 route->rt_pref_src = addr;
847 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
852 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
854 return route->rt_pref_src;
857 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
859 route->rt_iif = ifindex;
860 route->ce_mask |= ROUTE_ATTR_IIF;
863 int rtnl_route_get_iif(
struct rtnl_route *route)
865 return route->rt_iif;
868 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
870 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
872 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
875 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
877 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
879 nl_list_del(&nh->rtnh_list);
883 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
885 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
886 return &route->rt_nexthops;
891 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
893 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
894 return route->rt_nr_nh;
899 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
900 void (*cb)(
struct rtnl_nexthop *,
void *),
903 struct rtnl_nexthop *nh;
905 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
906 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
912 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
914 struct rtnl_nexthop *nh;
917 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
919 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
920 if (i == n)
return nh;
927 void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
929 route->rt_ttl_propagate = ttl_prop;
930 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
933 int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
937 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
938 return -NLE_MISSING_ATTR;
939 return route->rt_ttl_propagate;
964 if (route->rt_type == RTN_LOCAL)
965 return RT_SCOPE_HOST;
967 if (route->rt_family == AF_MPLS)
968 return RT_SCOPE_UNIVERSE;
970 if (!nl_list_empty(&route->rt_nexthops)) {
971 struct rtnl_nexthop *nh;
977 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
978 if (nh->rtnh_gateway)
979 return RT_SCOPE_UNIVERSE;
983 return RT_SCOPE_LINK;
988 static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
990 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
993 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
996 static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
1002 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1013 static struct nla_policy route_policy[RTA_MAX+1] = {
1015 [RTA_OIF] = { .type =
NLA_U32 },
1016 [RTA_PRIORITY] = { .type =
NLA_U32 },
1017 [RTA_FLOW] = { .type =
NLA_U32 },
1018 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1021 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1023 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1026 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1028 struct rtnexthop *rtnh =
nla_data(attr);
1032 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1033 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *nh = NULL;
1035 nh = rtnl_route_nh_alloc();
1039 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1040 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1041 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1043 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1044 struct nlattr *ntb[RTA_MAX + 1];
1046 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1048 rtnh->rtnh_len -
sizeof(*rtnh),
1053 if (ntb[RTA_GATEWAY]) {
1054 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1061 rtnl_route_nh_set_gateway(nh, addr);
1064 if (ntb[RTA_FLOW]) {
1068 rtnl_route_nh_set_realms(nh, realms);
1071 if (ntb[RTA_NEWDST]) {
1072 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1079 err = rtnl_route_nh_set_newdst(nh, addr);
1085 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1087 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1091 err = rtnl_route_nh_set_via(nh, addr);
1096 if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1097 err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1098 ntb[RTA_ENCAP_TYPE],
1105 rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh));
1106 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1107 rtnh = RTNH_NEXT(rtnh);
1113 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1115 _nl_auto_rtnl_route
struct rtnl_route *route = NULL;
1116 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *old_nh = NULL;
1117 _nl_auto_nl_addr
struct nl_addr *src = NULL;
1118 _nl_auto_nl_addr
struct nl_addr *dst = NULL;
1119 struct nlattr *tb[RTA_MAX + 1];
1124 route = rtnl_route_alloc();
1128 route->ce_msgtype = nlh->nlmsg_type;
1130 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1135 route->rt_family = family = rtm->rtm_family;
1136 route->rt_tos = rtm->rtm_tos;
1137 route->rt_table = rtm->rtm_table;
1138 route->rt_type = rtm->rtm_type;
1139 route->rt_scope = rtm->rtm_scope;
1140 route->rt_protocol = rtm->rtm_protocol;
1141 route->rt_flags = rtm->rtm_flags;
1144 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1145 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1146 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1152 if (family != AF_MPLS)
1153 route->ce_mask |= ROUTE_ATTR_PRIO;
1165 err = rtnl_route_set_dst(route, dst);
1172 }
else if (rtm->rtm_src_len)
1178 rtnl_route_set_src(route, src);
1182 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1185 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1187 if (tb[RTA_PRIORITY])
1188 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1190 if (tb[RTA_PREFSRC]) {
1191 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1195 rtnl_route_set_pref_src(route, addr);
1198 if (tb[RTA_METRICS]) {
1199 struct nlattr *mtb[RTAX_MAX + 1];
1206 for (i = 1; i <= RTAX_MAX; i++) {
1207 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1210 err = rtnl_route_set_metric(route, i, m);
1217 if (tb[RTA_MULTIPATH]) {
1218 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1222 if (tb[RTA_CACHEINFO]) {
1223 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1224 sizeof(route->rt_cacheinfo));
1225 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1229 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1232 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1235 if (tb[RTA_GATEWAY]) {
1236 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1238 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1244 rtnl_route_nh_set_gateway(old_nh, addr);
1248 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1251 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1254 if (tb[RTA_NEWDST]) {
1255 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1257 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1264 err = rtnl_route_nh_set_newdst(old_nh, addr);
1270 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1271 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1272 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1274 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1277 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1281 err = rtnl_route_nh_set_via(old_nh, addr);
1286 if (tb[RTA_TTL_PROPAGATE]) {
1287 rtnl_route_set_ttl_propagate(route,
1291 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1292 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1295 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1296 tb[RTA_ENCAP_TYPE], old_nh);
1302 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1303 if (route->rt_nr_nh == 0) {
1307 rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh));
1311 struct rtnl_nexthop *first;
1313 first = nl_list_first_entry(&route->rt_nexthops,
1314 struct rtnl_nexthop,
1319 if (rtnl_route_nh_compare(old_nh, first,
1320 old_nh->ce_mask, 0)) {
1326 *result = _nl_steal_pointer(&route);
1330 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1333 struct nlattr *metrics;
1334 struct rtmsg rtmsg = {
1335 .rtm_family = route->rt_family,
1336 .rtm_tos = route->rt_tos,
1337 .rtm_table = route->rt_table,
1338 .rtm_protocol = route->rt_protocol,
1339 .rtm_scope = route->rt_scope,
1340 .rtm_type = route->rt_type,
1341 .rtm_flags = route->rt_flags,
1344 if (route->rt_dst == NULL)
1345 return -NLE_MISSING_ATTR;
1351 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1354 if (rtnl_route_get_nnexthops(route) == 1) {
1355 struct rtnl_nexthop *nh;
1356 nh = rtnl_route_nexthop_n(route, 0);
1357 rtmsg.rtm_flags |= nh->rtnh_flags;
1360 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1361 goto nla_put_failure;
1367 if (route->rt_family != AF_MPLS)
1373 if (route->ce_mask & ROUTE_ATTR_PRIO)
1376 if (route->ce_mask & ROUTE_ATTR_SRC)
1379 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1382 if (route->ce_mask & ROUTE_ATTR_IIF)
1385 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1386 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1388 if (route->rt_nmetrics > 0) {
1392 if (metrics == NULL)
1393 goto nla_put_failure;
1395 for (i = 1; i <= RTAX_MAX; i++) {
1396 if (!rtnl_route_get_metric(route, i, &val))
1403 if (rtnl_route_get_nnexthops(route) == 1) {
1404 struct rtnl_nexthop *nh;
1406 nh = rtnl_route_nexthop_n(route, 0);
1407 if (nh->rtnh_gateway)
1409 if (nh->rtnh_ifindex)
1411 if (nh->rtnh_realms)
1413 if (nh->rtnh_newdst)
1415 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1416 goto nla_put_failure;
1417 if (nh->rtnh_encap &&
1418 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1419 goto nla_put_failure;
1420 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1421 struct nlattr *multipath;
1422 struct rtnl_nexthop *nh;
1425 goto nla_put_failure;
1427 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1428 struct rtnexthop *rtnh;
1432 goto nla_put_failure;
1434 rtnh->rtnh_flags = nh->rtnh_flags;
1435 rtnh->rtnh_hops = nh->rtnh_weight;
1436 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1438 if (nh->rtnh_gateway)
1442 if (nh->rtnh_newdst)
1446 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1447 goto nla_put_failure;
1449 if (nh->rtnh_realms)
1452 if (nh->rtnh_encap &&
1453 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1454 goto nla_put_failure;
1456 rtnh->rtnh_len = (
char *) nlmsg_tail(msg->nm_nlh) -
1466 return -NLE_MSGSIZE;
1470 struct nl_object_ops route_obj_ops = {
1471 .oo_name =
"route/route",
1472 .oo_size =
sizeof(
struct rtnl_route),
1473 .oo_constructor = route_constructor,
1474 .oo_free_data = route_free_data,
1475 .oo_clone = route_clone,
1481 .oo_compare = route_compare,
1482 .oo_keygen = route_keygen,
1483 .oo_update = route_update,
1484 .oo_attrs2str = route_attrs2str,
1485 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1486 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1488 .oo_id_attrs_get = route_id_attrs_get,
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
struct nlattr * nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Reserve space for a attribute.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
@ NLA_NESTED
Nested attributes.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
int rtnl_route_guess_scope(struct rtnl_route *route)
Guess scope of a route object.
int nl_get_user_hz(void)
Return the value of HZ.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
int dp_ivar
PRIVATE Owned by the current caller.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.