34 #include <netlink-private/netlink.h> 35 #include <netlink-private/utils.h> 36 #include <netlink-private/route/nexthop-encap.h> 37 #include <netlink/netlink.h> 38 #include <netlink/cache.h> 39 #include <netlink/utils.h> 40 #include <netlink/data.h> 41 #include <netlink/hashtable.h> 42 #include <netlink/route/rtnl.h> 43 #include <netlink/route/route.h> 44 #include <netlink/route/link.h> 45 #include <netlink/route/nexthop.h> 46 #include <linux/in_route.h> 49 #define ROUTE_ATTR_FAMILY 0x000001 50 #define ROUTE_ATTR_TOS 0x000002 51 #define ROUTE_ATTR_TABLE 0x000004 52 #define ROUTE_ATTR_PROTOCOL 0x000008 53 #define ROUTE_ATTR_SCOPE 0x000010 54 #define ROUTE_ATTR_TYPE 0x000020 55 #define ROUTE_ATTR_FLAGS 0x000040 56 #define ROUTE_ATTR_DST 0x000080 57 #define ROUTE_ATTR_SRC 0x000100 58 #define ROUTE_ATTR_IIF 0x000200 59 #define ROUTE_ATTR_OIF 0x000400 60 #define ROUTE_ATTR_GATEWAY 0x000800 61 #define ROUTE_ATTR_PRIO 0x001000 62 #define ROUTE_ATTR_PREF_SRC 0x002000 63 #define ROUTE_ATTR_METRICS 0x004000 64 #define ROUTE_ATTR_MULTIPATH 0x008000 65 #define ROUTE_ATTR_REALMS 0x010000 66 #define ROUTE_ATTR_CACHEINFO 0x020000 67 #define ROUTE_ATTR_TTL_PROPAGATE 0x040000 70 static void route_constructor(
struct nl_object *c)
72 struct rtnl_route *r = (
struct rtnl_route *) c;
74 r->rt_family = AF_UNSPEC;
75 r->rt_scope = RT_SCOPE_NOWHERE;
76 r->rt_table = RT_TABLE_MAIN;
77 r->rt_protocol = RTPROT_STATIC;
78 r->rt_type = RTN_UNICAST;
81 nl_init_list_head(&r->rt_nexthops);
84 static void route_free_data(
struct nl_object *c)
86 struct rtnl_route *r = (
struct rtnl_route *) c;
87 struct rtnl_nexthop *nh, *tmp;
96 nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
97 rtnl_route_remove_nexthop(r, nh);
98 rtnl_route_nh_free(nh);
102 static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
104 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
105 struct rtnl_route *src = (
struct rtnl_route *) _src;
106 struct rtnl_nexthop *nh, *
new;
116 if (src->rt_pref_src)
123 nl_init_list_head(&dst->rt_nexthops);
124 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
125 new = rtnl_route_nh_clone(nh);
129 rtnl_route_add_nexthop(dst,
new);
135 static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
137 struct rtnl_route *r = (
struct rtnl_route *) a;
138 int cache = 0, flags;
141 if (r->rt_flags & RTM_F_CLONED)
144 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
149 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
155 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
157 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
159 if (r->ce_mask & ROUTE_ATTR_TYPE)
161 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
163 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
164 nl_dump(p,
"tos %#x ", r->rt_tos);
166 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
167 struct rtnl_nexthop *nh;
169 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
170 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
171 rtnl_route_nh_dump(nh, p);
175 flags = r->rt_flags & ~(RTM_F_CLONED);
176 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
180 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \ 181 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 184 PRINT_FLAG(PERVASIVE);
187 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \ 188 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 190 PRINT_FLAG(EQUALIZE);
194 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \ 195 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 197 PRINT_FLAG(REDIRECTED);
198 PRINT_FLAG(DOREDIRECT);
199 PRINT_FLAG(DIRECTSRC);
201 PRINT_FLAG(BROADCAST);
202 PRINT_FLAG(MULTICAST);
212 static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
214 struct rtnl_route *r = (
struct rtnl_route *) a;
215 struct nl_cache *link_cache;
221 route_dump_line(a, p);
222 nl_dump_line(p,
" ");
224 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
225 nl_dump(p,
"preferred-src %s ",
228 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
230 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
232 if (r->ce_mask & ROUTE_ATTR_PRIO)
233 nl_dump(p,
"priority %#x ", r->rt_prio);
235 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
237 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
239 if (r->ce_mask & ROUTE_ATTR_IIF) {
245 nl_dump(p,
"iif %d ", r->rt_iif);
248 if (r->ce_mask & ROUTE_ATTR_SRC)
251 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
252 nl_dump(p,
" ttl-propagate %s",
253 r->rt_ttl_propagate ?
"enabled" :
"disabled");
258 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
259 struct rtnl_nexthop *nh;
261 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
262 nl_dump_line(p,
" ");
263 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
264 rtnl_route_nh_dump(nh, p);
269 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
270 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
271 r->rt_cacheinfo.rtci_error,
272 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
275 if (r->ce_mask & ROUTE_ATTR_METRICS) {
276 nl_dump_line(p,
" metrics [");
277 for (i = 0; i < RTAX_MAX; i++)
278 if (r->rt_metrics_mask & (1 << i))
280 rtnl_route_metric2str(i+1,
287 nl_cache_put(link_cache);
290 static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
292 struct rtnl_route *route = (
struct rtnl_route *) obj;
294 route_dump_details(obj, p);
296 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
299 nl_dump_line(p,
" used %u refcnt %u last-use %us " 301 ci->rtci_used, ci->rtci_clntref,
307 static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
310 struct rtnl_route *route = (
struct rtnl_route *) obj;
311 unsigned int rkey_sz;
312 struct nl_addr *addr = NULL;
313 struct route_hash_key {
319 } __attribute__((packed)) *rkey;
321 char buf[INET6_ADDRSTRLEN+5];
325 addr = route->rt_dst;
327 rkey_sz =
sizeof(*rkey);
330 rkey = calloc(1, rkey_sz);
332 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
336 rkey->rt_family = route->rt_family;
337 rkey->rt_tos = route->rt_tos;
338 rkey->rt_table = route->rt_table;
339 rkey->rt_prio = route->rt_prio;
344 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
346 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d " 347 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
348 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
356 static uint32_t route_id_attrs_get(
struct nl_object *obj)
358 struct rtnl_route *route = (
struct rtnl_route *)obj;
359 struct nl_object_ops *ops = obj->ce_ops;
360 uint32_t rv = ops->oo_id_attrs;
363 if (route->rt_family == AF_MPLS)
364 rv &= ~ROUTE_ATTR_PRIO;
369 static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
370 uint64_t attrs,
int flags)
372 struct rtnl_route *a = (
struct rtnl_route *) _a;
373 struct rtnl_route *b = (
struct rtnl_route *) _b;
374 struct rtnl_nexthop *nh_a, *nh_b;
378 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR) 380 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
381 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
382 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
383 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
384 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
385 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
386 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
387 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
388 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
389 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
390 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
392 diff |= ROUTE_DIFF(TTL_PROPAGATE,
393 a->rt_ttl_propagate != b->rt_ttl_propagate);
395 if (flags & LOOSE_COMPARISON) {
396 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
398 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
400 if (!rtnl_route_nh_compare(nh_a, nh_b,
411 for (i = 0; i < RTAX_MAX - 1; i++) {
412 if (a->rt_metrics_mask & (1 << i) &&
413 (!(b->rt_metrics_mask & (1 << i)) ||
414 a->rt_metrics[i] != b->rt_metrics[i]))
415 diff |= ROUTE_DIFF(METRICS, 1);
418 diff |= ROUTE_DIFF(FLAGS,
419 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
421 if (a->rt_nr_nh != b->rt_nr_nh)
425 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
427 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
429 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
440 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
442 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
444 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
453 for (i = 0; i < RTAX_MAX - 1; i++) {
454 if ((a->rt_metrics_mask & (1 << i)) ^
455 (b->rt_metrics_mask & (1 << i)))
456 diff |= ROUTE_DIFF(METRICS, 1);
458 diff |= ROUTE_DIFF(METRICS,
459 a->rt_metrics[i] != b->rt_metrics[i]);
462 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
469 diff |= ROUTE_DIFF(MULTIPATH, 1);
475 static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
477 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
478 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
479 struct rtnl_nexthop *new_nh;
480 int action = new_obj->ce_msgtype;
482 char buf[INET6_ADDRSTRLEN+5];
492 if (new_route->rt_family != AF_INET6 ||
493 new_route->rt_table == RT_TABLE_LOCAL)
494 return -NLE_OPNOTSUPP;
500 if (rtnl_route_get_nnexthops(new_route) != 1)
501 return -NLE_OPNOTSUPP;
508 new_nh = rtnl_route_nexthop_n(new_route, 0);
509 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
510 return -NLE_OPNOTSUPP;
513 case RTM_NEWROUTE : {
514 struct rtnl_nexthop *cloned_nh;
519 cloned_nh = rtnl_route_nh_clone(new_nh);
522 rtnl_route_add_nexthop(old_route, cloned_nh);
524 NL_DBG(2,
"Route obj %p updated. Added " 525 "nexthop %p via %s\n", old_route, cloned_nh,
530 case RTM_DELROUTE : {
531 struct rtnl_nexthop *old_nh;
539 if (rtnl_route_get_nnexthops(old_route) <= 1)
540 return -NLE_OPNOTSUPP;
545 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
547 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
549 rtnl_route_remove_nexthop(old_route, old_nh);
551 NL_DBG(2,
"Route obj %p updated. Removed " 552 "nexthop %p via %s\n", old_route,
557 rtnl_route_nh_free(old_nh);
564 NL_DBG(2,
"Unknown action associated " 565 "to object %p during route update\n", new_obj);
566 return -NLE_OPNOTSUPP;
572 static const struct trans_tbl route_attrs[] = {
573 __ADD(ROUTE_ATTR_FAMILY, family),
574 __ADD(ROUTE_ATTR_TOS, tos),
575 __ADD(ROUTE_ATTR_TABLE, table),
576 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
577 __ADD(ROUTE_ATTR_SCOPE, scope),
578 __ADD(ROUTE_ATTR_TYPE, type),
579 __ADD(ROUTE_ATTR_FLAGS, flags),
580 __ADD(ROUTE_ATTR_DST, dst),
581 __ADD(ROUTE_ATTR_SRC, src),
582 __ADD(ROUTE_ATTR_IIF, iif),
583 __ADD(ROUTE_ATTR_OIF, oif),
584 __ADD(ROUTE_ATTR_GATEWAY, gateway),
585 __ADD(ROUTE_ATTR_PRIO, prio),
586 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
587 __ADD(ROUTE_ATTR_METRICS, metrics),
588 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
589 __ADD(ROUTE_ATTR_REALMS, realms),
590 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
591 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
594 static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
596 return __flags2str(attrs, buf, len, route_attrs,
597 ARRAY_SIZE(route_attrs));
605 struct rtnl_route *rtnl_route_alloc(
void)
610 void rtnl_route_get(
struct rtnl_route *route)
615 void rtnl_route_put(
struct rtnl_route *route)
627 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
629 route->rt_table = table;
630 route->ce_mask |= ROUTE_ATTR_TABLE;
633 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
635 return route->rt_table;
638 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
640 route->rt_scope = scope;
641 route->ce_mask |= ROUTE_ATTR_SCOPE;
644 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
646 return route->rt_scope;
649 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
652 route->ce_mask |= ROUTE_ATTR_TOS;
655 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
657 return route->rt_tos;
660 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
662 route->rt_protocol = protocol;
663 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
666 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
668 return route->rt_protocol;
671 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
673 route->rt_prio = prio;
674 route->ce_mask |= ROUTE_ATTR_PRIO;
677 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
679 return route->rt_prio;
682 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
689 route->rt_family = family;
690 route->ce_mask |= ROUTE_ATTR_FAMILY;
694 return -NLE_AF_NOSUPPORT;
697 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
699 return route->rt_family;
702 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
704 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
705 if (addr->a_family != route->rt_family)
706 return -NLE_AF_MISMATCH;
708 route->rt_family = addr->a_family;
714 route->rt_dst = addr;
716 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
721 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
723 return route->rt_dst;
726 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
728 if (addr->a_family == AF_INET)
729 return -NLE_SRCRT_NOSUPPORT;
731 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
732 if (addr->a_family != route->rt_family)
733 return -NLE_AF_MISMATCH;
735 route->rt_family = addr->a_family;
741 route->rt_src = addr;
742 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
747 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
749 return route->rt_src;
752 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
757 route->rt_type = type;
758 route->ce_mask |= ROUTE_ATTR_TYPE;
763 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
765 return route->rt_type;
768 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
770 route->rt_flag_mask |= flags;
771 route->rt_flags |= flags;
772 route->ce_mask |= ROUTE_ATTR_FLAGS;
775 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
777 route->rt_flag_mask |= flags;
778 route->rt_flags &= ~flags;
779 route->ce_mask |= ROUTE_ATTR_FLAGS;
782 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
784 return route->rt_flags;
787 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
789 if (metric > RTAX_MAX || metric < 1)
792 route->rt_metrics[metric - 1] = value;
794 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
795 route->rt_nmetrics++;
796 route->rt_metrics_mask |= (1 << (metric - 1));
799 route->ce_mask |= ROUTE_ATTR_METRICS;
804 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
806 if (metric > RTAX_MAX || metric < 1)
809 if (route->rt_metrics_mask & (1 << (metric - 1))) {
810 route->rt_nmetrics--;
811 route->rt_metrics_mask &= ~(1 << (metric - 1));
817 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
819 if (metric > RTAX_MAX || metric < 1)
822 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
823 return -NLE_OBJ_NOTFOUND;
826 *value = route->rt_metrics[metric - 1];
831 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
833 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
834 if (addr->a_family != route->rt_family)
835 return -NLE_AF_MISMATCH;
837 route->rt_family = addr->a_family;
839 if (route->rt_pref_src)
843 route->rt_pref_src = addr;
844 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
849 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
851 return route->rt_pref_src;
854 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
856 route->rt_iif = ifindex;
857 route->ce_mask |= ROUTE_ATTR_IIF;
860 int rtnl_route_get_iif(
struct rtnl_route *route)
862 return route->rt_iif;
865 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
867 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
869 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
872 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
874 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
876 nl_list_del(&nh->rtnh_list);
880 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
882 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
883 return &route->rt_nexthops;
888 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
890 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
891 return route->rt_nr_nh;
896 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
897 void (*cb)(
struct rtnl_nexthop *,
void *),
900 struct rtnl_nexthop *nh;
902 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
903 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
909 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
911 struct rtnl_nexthop *nh;
914 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
916 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
917 if (i == n)
return nh;
924 void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
926 route->rt_ttl_propagate = ttl_prop;
927 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
930 int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
934 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
935 return -NLE_MISSING_ATTR;
936 return route->rt_ttl_propagate;
961 if (route->rt_type == RTN_LOCAL)
962 return RT_SCOPE_HOST;
964 if (route->rt_family == AF_MPLS)
965 return RT_SCOPE_UNIVERSE;
967 if (!nl_list_empty(&route->rt_nexthops)) {
968 struct rtnl_nexthop *nh;
974 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
975 if (nh->rtnh_gateway)
976 return RT_SCOPE_UNIVERSE;
980 return RT_SCOPE_LINK;
985 static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
987 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
990 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
993 static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
999 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1010 static struct nla_policy route_policy[RTA_MAX+1] = {
1012 [RTA_OIF] = { .type =
NLA_U32 },
1013 [RTA_PRIORITY] = { .type =
NLA_U32 },
1014 [RTA_FLOW] = { .type =
NLA_U32 },
1015 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1018 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1020 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1023 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1025 struct rtnl_nexthop *nh = NULL;
1026 struct rtnexthop *rtnh =
nla_data(attr);
1030 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1031 nh = rtnl_route_nh_alloc();
1035 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1036 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1037 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1039 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1040 struct nlattr *ntb[RTA_MAX + 1];
1042 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1044 rtnh->rtnh_len -
sizeof(*rtnh),
1049 if (ntb[RTA_GATEWAY]) {
1050 struct nl_addr *addr;
1059 rtnl_route_nh_set_gateway(nh, addr);
1063 if (ntb[RTA_FLOW]) {
1067 rtnl_route_nh_set_realms(nh, realms);
1070 if (ntb[RTA_NEWDST]) {
1071 struct nl_addr *addr;
1078 err = rtnl_route_nh_set_newdst(nh, addr);
1085 struct nl_addr *addr;
1087 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1091 err = rtnl_route_nh_set_via(nh, addr);
1097 if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1098 err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1099 ntb[RTA_ENCAP_TYPE],
1106 rtnl_route_add_nexthop(route, nh);
1107 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1108 rtnh = RTNH_NEXT(rtnh);
1114 rtnl_route_nh_free(nh);
1119 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1122 struct rtnl_route *route;
1123 struct nlattr *tb[RTA_MAX + 1];
1124 struct nl_addr *src = NULL, *dst = NULL, *addr;
1125 struct rtnl_nexthop *old_nh = NULL;
1128 route = rtnl_route_alloc();
1132 route->ce_msgtype = nlh->nlmsg_type;
1134 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1139 route->rt_family = family = rtm->rtm_family;
1140 route->rt_tos = rtm->rtm_tos;
1141 route->rt_table = rtm->rtm_table;
1142 route->rt_type = rtm->rtm_type;
1143 route->rt_scope = rtm->rtm_scope;
1144 route->rt_protocol = rtm->rtm_protocol;
1145 route->rt_flags = rtm->rtm_flags;
1148 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1149 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1150 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1156 if (family != AF_MPLS)
1157 route->ce_mask |= ROUTE_ATTR_PRIO;
1169 err = rtnl_route_set_dst(route, dst);
1178 }
else if (rtm->rtm_src_len)
1184 rtnl_route_set_src(route, src);
1189 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1192 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1194 if (tb[RTA_PRIORITY])
1195 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1197 if (tb[RTA_PREFSRC]) {
1200 rtnl_route_set_pref_src(route, addr);
1204 if (tb[RTA_METRICS]) {
1205 struct nlattr *mtb[RTAX_MAX + 1];
1212 for (i = 1; i <= RTAX_MAX; i++) {
1213 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1216 err = rtnl_route_set_metric(route, i, m);
1223 if (tb[RTA_MULTIPATH])
1224 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1227 if (tb[RTA_CACHEINFO]) {
1228 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1229 sizeof(route->rt_cacheinfo));
1230 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1234 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1237 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1240 if (tb[RTA_GATEWAY]) {
1241 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1247 rtnl_route_nh_set_gateway(old_nh, addr);
1252 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1255 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1258 if (tb[RTA_NEWDST]) {
1259 struct nl_addr *addr;
1261 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1268 err = rtnl_route_nh_set_newdst(old_nh, addr);
1275 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1276 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1278 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1281 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1285 err = rtnl_route_nh_set_via(old_nh, addr);
1291 if (tb[RTA_TTL_PROPAGATE]) {
1292 rtnl_route_set_ttl_propagate(route,
1296 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1297 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1300 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1301 tb[RTA_ENCAP_TYPE], old_nh);
1307 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1308 if (route->rt_nr_nh == 0) {
1312 rtnl_route_add_nexthop(route, old_nh);
1316 struct rtnl_nexthop *first;
1318 first = nl_list_first_entry(&route->rt_nexthops,
1319 struct rtnl_nexthop,
1324 if (rtnl_route_nh_compare(old_nh, first,
1325 old_nh->ce_mask, 0)) {
1330 rtnl_route_nh_free(old_nh);
1340 rtnl_route_nh_free(old_nh);
1341 rtnl_route_put(route);
1349 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1352 struct nlattr *metrics;
1353 struct rtmsg rtmsg = {
1354 .rtm_family = route->rt_family,
1355 .rtm_tos = route->rt_tos,
1356 .rtm_table = route->rt_table,
1357 .rtm_protocol = route->rt_protocol,
1358 .rtm_scope = route->rt_scope,
1359 .rtm_type = route->rt_type,
1360 .rtm_flags = route->rt_flags,
1363 if (route->rt_dst == NULL)
1364 return -NLE_MISSING_ATTR;
1370 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1373 if (rtnl_route_get_nnexthops(route) == 1) {
1374 struct rtnl_nexthop *nh;
1375 nh = rtnl_route_nexthop_n(route, 0);
1376 rtmsg.rtm_flags |= nh->rtnh_flags;
1379 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1380 goto nla_put_failure;
1386 if (route->rt_family != AF_MPLS)
1392 if (route->ce_mask & ROUTE_ATTR_PRIO)
1395 if (route->ce_mask & ROUTE_ATTR_SRC)
1398 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1401 if (route->ce_mask & ROUTE_ATTR_IIF)
1404 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1405 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1407 if (route->rt_nmetrics > 0) {
1411 if (metrics == NULL)
1412 goto nla_put_failure;
1414 for (i = 1; i <= RTAX_MAX; i++) {
1415 if (!rtnl_route_get_metric(route, i, &val))
1422 if (rtnl_route_get_nnexthops(route) == 1) {
1423 struct rtnl_nexthop *nh;
1425 nh = rtnl_route_nexthop_n(route, 0);
1426 if (nh->rtnh_gateway)
1428 if (nh->rtnh_ifindex)
1430 if (nh->rtnh_realms)
1432 if (nh->rtnh_newdst)
1434 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1435 goto nla_put_failure;
1436 if (nh->rtnh_encap &&
1437 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1438 goto nla_put_failure;
1439 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1440 struct nlattr *multipath;
1441 struct rtnl_nexthop *nh;
1444 goto nla_put_failure;
1446 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1447 struct rtnexthop *rtnh;
1451 goto nla_put_failure;
1453 rtnh->rtnh_flags = nh->rtnh_flags;
1454 rtnh->rtnh_hops = nh->rtnh_weight;
1455 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1457 if (nh->rtnh_gateway)
1461 if (nh->rtnh_newdst)
1465 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1466 goto nla_put_failure;
1468 if (nh->rtnh_realms)
1471 if (nh->rtnh_encap &&
1472 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1473 goto nla_put_failure;
1475 rtnh->rtnh_len = (
char *) nlmsg_tail(msg->nm_nlh) -
1485 return -NLE_MSGSIZE;
1489 struct nl_object_ops route_obj_ops = {
1490 .oo_name =
"route/route",
1491 .oo_size =
sizeof(
struct rtnl_route),
1492 .oo_constructor = route_constructor,
1493 .oo_free_data = route_free_data,
1494 .oo_clone = route_clone,
1500 .oo_compare = route_compare,
1501 .oo_keygen = route_keygen,
1502 .oo_update = route_update,
1503 .oo_attrs2str = route_attrs2str,
1504 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1505 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1507 .oo_id_attrs_get = route_id_attrs_get,
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Dump object briefly on one line.
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
int nl_get_user_hz(void)
Return the value of HZ.
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.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
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 nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
Attribute validation policy.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
struct nlattr * nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Reserve space for a attribute.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
void nl_addr_set_family(struct nl_addr *addr, int family)
Set address family.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
Dump all attributes but no statistics.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of 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_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int rtnl_route_guess_scope(struct rtnl_route *route)
Guess scope of a route object.
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad)
Append data to tail of a netlink message.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
uint16_t type
Type of attribute or NLA_UNSPEC.
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
int dp_ivar
PRIVATE Owned by the current caller.
Dump all attributes including statistics.
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.