33 #include <netlink-private/netlink.h> 34 #include <netlink-private/utils.h> 35 #include <netlink-private/route/nexthop-encap.h> 36 #include <netlink/netlink.h> 37 #include <netlink/cache.h> 38 #include <netlink/utils.h> 39 #include <netlink/data.h> 40 #include <netlink/hashtable.h> 41 #include <netlink/route/rtnl.h> 42 #include <netlink/route/route.h> 43 #include <netlink/route/link.h> 44 #include <netlink/route/nexthop.h> 45 #include <linux/in_route.h> 48 #define ROUTE_ATTR_FAMILY 0x000001 49 #define ROUTE_ATTR_TOS 0x000002 50 #define ROUTE_ATTR_TABLE 0x000004 51 #define ROUTE_ATTR_PROTOCOL 0x000008 52 #define ROUTE_ATTR_SCOPE 0x000010 53 #define ROUTE_ATTR_TYPE 0x000020 54 #define ROUTE_ATTR_FLAGS 0x000040 55 #define ROUTE_ATTR_DST 0x000080 56 #define ROUTE_ATTR_SRC 0x000100 57 #define ROUTE_ATTR_IIF 0x000200 58 #define ROUTE_ATTR_OIF 0x000400 59 #define ROUTE_ATTR_GATEWAY 0x000800 60 #define ROUTE_ATTR_PRIO 0x001000 61 #define ROUTE_ATTR_PREF_SRC 0x002000 62 #define ROUTE_ATTR_METRICS 0x004000 63 #define ROUTE_ATTR_MULTIPATH 0x008000 64 #define ROUTE_ATTR_REALMS 0x010000 65 #define ROUTE_ATTR_CACHEINFO 0x020000 66 #define ROUTE_ATTR_TTL_PROPAGATE 0x040000 69 static void route_constructor(
struct nl_object *c)
71 struct rtnl_route *r = (
struct rtnl_route *) c;
73 r->rt_family = AF_UNSPEC;
74 r->rt_scope = RT_SCOPE_NOWHERE;
75 r->rt_table = RT_TABLE_MAIN;
76 r->rt_protocol = RTPROT_STATIC;
77 r->rt_type = RTN_UNICAST;
80 nl_init_list_head(&r->rt_nexthops);
83 static void route_free_data(
struct nl_object *c)
85 struct rtnl_route *r = (
struct rtnl_route *) c;
86 struct rtnl_nexthop *nh, *tmp;
95 nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
96 rtnl_route_remove_nexthop(r, nh);
97 rtnl_route_nh_free(nh);
101 static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
103 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
104 struct rtnl_route *src = (
struct rtnl_route *) _src;
105 struct rtnl_nexthop *nh, *
new;
115 if (src->rt_pref_src)
122 nl_init_list_head(&dst->rt_nexthops);
123 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
124 new = rtnl_route_nh_clone(nh);
128 rtnl_route_add_nexthop(dst,
new);
134 static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
136 struct rtnl_route *r = (
struct rtnl_route *) a;
137 int cache = 0, flags;
140 if (r->rt_flags & RTM_F_CLONED)
143 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
148 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
154 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
156 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
158 if (r->ce_mask & ROUTE_ATTR_TYPE)
160 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
162 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
163 nl_dump(p,
"tos %#x ", r->rt_tos);
165 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
166 struct rtnl_nexthop *nh;
168 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
169 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
170 rtnl_route_nh_dump(nh, p);
174 flags = r->rt_flags & ~(RTM_F_CLONED);
175 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
179 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \ 180 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 183 PRINT_FLAG(PERVASIVE);
186 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \ 187 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 189 PRINT_FLAG(EQUALIZE);
193 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \ 194 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 196 PRINT_FLAG(REDIRECTED);
197 PRINT_FLAG(DOREDIRECT);
198 PRINT_FLAG(DIRECTSRC);
200 PRINT_FLAG(BROADCAST);
201 PRINT_FLAG(MULTICAST);
211 static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
213 struct rtnl_route *r = (
struct rtnl_route *) a;
214 struct nl_cache *link_cache;
220 route_dump_line(a, p);
221 nl_dump_line(p,
" ");
223 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
224 nl_dump(p,
"preferred-src %s ",
227 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
229 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
231 if (r->ce_mask & ROUTE_ATTR_PRIO)
232 nl_dump(p,
"priority %#x ", r->rt_prio);
234 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
236 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
238 if (r->ce_mask & ROUTE_ATTR_IIF) {
244 nl_dump(p,
"iif %d ", r->rt_iif);
247 if (r->ce_mask & ROUTE_ATTR_SRC)
250 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
251 nl_dump(p,
" ttl-propagate %s",
252 r->rt_ttl_propagate ?
"enabled" :
"disabled");
257 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
258 struct rtnl_nexthop *nh;
260 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
261 nl_dump_line(p,
" ");
262 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
263 rtnl_route_nh_dump(nh, p);
268 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
269 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
270 r->rt_cacheinfo.rtci_error,
271 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
274 if (r->ce_mask & ROUTE_ATTR_METRICS) {
275 nl_dump_line(p,
" metrics [");
276 for (i = 0; i < RTAX_MAX; i++)
277 if (r->rt_metrics_mask & (1 << i))
279 rtnl_route_metric2str(i+1,
286 nl_cache_put(link_cache);
289 static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
291 struct rtnl_route *route = (
struct rtnl_route *) obj;
293 route_dump_details(obj, p);
295 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
298 nl_dump_line(p,
" used %u refcnt %u last-use %us " 300 ci->rtci_used, ci->rtci_clntref,
306 static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
309 struct rtnl_route *route = (
struct rtnl_route *) obj;
310 unsigned int rkey_sz;
311 struct nl_addr *addr = NULL;
312 struct route_hash_key {
318 } __attribute__((packed)) *rkey;
320 char buf[INET6_ADDRSTRLEN+5];
324 addr = route->rt_dst;
326 rkey_sz =
sizeof(*rkey);
329 rkey = calloc(1, rkey_sz);
331 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
335 rkey->rt_family = route->rt_family;
336 rkey->rt_tos = route->rt_tos;
337 rkey->rt_table = route->rt_table;
338 rkey->rt_prio = route->rt_prio;
343 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
345 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d " 346 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
347 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
355 static uint32_t route_id_attrs_get(
struct nl_object *obj)
357 struct rtnl_route *route = (
struct rtnl_route *)obj;
358 struct nl_object_ops *ops = obj->ce_ops;
359 uint32_t rv = ops->oo_id_attrs;
362 if (route->rt_family == AF_MPLS)
363 rv &= ~ROUTE_ATTR_PRIO;
368 static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
369 uint64_t attrs,
int flags)
371 struct rtnl_route *a = (
struct rtnl_route *) _a;
372 struct rtnl_route *b = (
struct rtnl_route *) _b;
373 struct rtnl_nexthop *nh_a, *nh_b;
377 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR) 379 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
380 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
381 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
382 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
383 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
384 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
385 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
386 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
387 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
388 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
389 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
391 diff |= ROUTE_DIFF(TTL_PROPAGATE,
392 a->rt_ttl_propagate != b->rt_ttl_propagate);
394 if (flags & LOOSE_COMPARISON) {
395 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
397 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
399 if (!rtnl_route_nh_compare(nh_a, nh_b,
410 for (i = 0; i < RTAX_MAX - 1; i++) {
411 if (a->rt_metrics_mask & (1 << i) &&
412 (!(b->rt_metrics_mask & (1 << i)) ||
413 a->rt_metrics[i] != b->rt_metrics[i]))
414 diff |= ROUTE_DIFF(METRICS, 1);
417 diff |= ROUTE_DIFF(FLAGS,
418 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
420 if (a->rt_nr_nh != b->rt_nr_nh)
424 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
426 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
428 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
439 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
441 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
443 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
452 for (i = 0; i < RTAX_MAX - 1; i++) {
453 if ((a->rt_metrics_mask & (1 << i)) ^
454 (b->rt_metrics_mask & (1 << i)))
455 diff |= ROUTE_DIFF(METRICS, 1);
457 diff |= ROUTE_DIFF(METRICS,
458 a->rt_metrics[i] != b->rt_metrics[i]);
461 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
468 diff |= ROUTE_DIFF(MULTIPATH, 1);
474 static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
476 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
477 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
478 struct rtnl_nexthop *new_nh;
479 int action = new_obj->ce_msgtype;
481 char buf[INET6_ADDRSTRLEN+5];
491 if (new_route->rt_family != AF_INET6 ||
492 new_route->rt_table == RT_TABLE_LOCAL)
493 return -NLE_OPNOTSUPP;
499 if (rtnl_route_get_nnexthops(new_route) != 1)
500 return -NLE_OPNOTSUPP;
507 new_nh = rtnl_route_nexthop_n(new_route, 0);
508 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
509 return -NLE_OPNOTSUPP;
512 case RTM_NEWROUTE : {
513 struct rtnl_nexthop *cloned_nh;
518 cloned_nh = rtnl_route_nh_clone(new_nh);
521 rtnl_route_add_nexthop(old_route, cloned_nh);
523 NL_DBG(2,
"Route obj %p updated. Added " 524 "nexthop %p via %s\n", old_route, cloned_nh,
529 case RTM_DELROUTE : {
530 struct rtnl_nexthop *old_nh;
538 if (rtnl_route_get_nnexthops(old_route) <= 1)
539 return -NLE_OPNOTSUPP;
544 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
546 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
548 rtnl_route_remove_nexthop(old_route, old_nh);
550 NL_DBG(2,
"Route obj %p updated. Removed " 551 "nexthop %p via %s\n", old_route,
556 rtnl_route_nh_free(old_nh);
563 NL_DBG(2,
"Unknown action associated " 564 "to object %p during route update\n", new_obj);
565 return -NLE_OPNOTSUPP;
571 static const struct trans_tbl route_attrs[] = {
572 __ADD(ROUTE_ATTR_FAMILY, family),
573 __ADD(ROUTE_ATTR_TOS, tos),
574 __ADD(ROUTE_ATTR_TABLE, table),
575 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
576 __ADD(ROUTE_ATTR_SCOPE, scope),
577 __ADD(ROUTE_ATTR_TYPE, type),
578 __ADD(ROUTE_ATTR_FLAGS, flags),
579 __ADD(ROUTE_ATTR_DST, dst),
580 __ADD(ROUTE_ATTR_SRC, src),
581 __ADD(ROUTE_ATTR_IIF, iif),
582 __ADD(ROUTE_ATTR_OIF, oif),
583 __ADD(ROUTE_ATTR_GATEWAY, gateway),
584 __ADD(ROUTE_ATTR_PRIO, prio),
585 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
586 __ADD(ROUTE_ATTR_METRICS, metrics),
587 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
588 __ADD(ROUTE_ATTR_REALMS, realms),
589 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
590 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
593 static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
595 return __flags2str(attrs, buf, len, route_attrs,
596 ARRAY_SIZE(route_attrs));
604 struct rtnl_route *rtnl_route_alloc(
void)
609 void rtnl_route_get(
struct rtnl_route *route)
614 void rtnl_route_put(
struct rtnl_route *route)
626 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
628 route->rt_table = table;
629 route->ce_mask |= ROUTE_ATTR_TABLE;
632 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
634 return route->rt_table;
637 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
639 route->rt_scope = scope;
640 route->ce_mask |= ROUTE_ATTR_SCOPE;
643 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
645 return route->rt_scope;
648 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
651 route->ce_mask |= ROUTE_ATTR_TOS;
654 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
656 return route->rt_tos;
659 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
661 route->rt_protocol = protocol;
662 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
665 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
667 return route->rt_protocol;
670 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
672 route->rt_prio = prio;
673 route->ce_mask |= ROUTE_ATTR_PRIO;
676 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
678 return route->rt_prio;
681 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
688 route->rt_family = family;
689 route->ce_mask |= ROUTE_ATTR_FAMILY;
693 return -NLE_AF_NOSUPPORT;
696 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
698 return route->rt_family;
701 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
703 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
704 if (addr->a_family != route->rt_family)
705 return -NLE_AF_MISMATCH;
707 route->rt_family = addr->a_family;
713 route->rt_dst = addr;
715 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
720 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
722 return route->rt_dst;
725 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
727 if (addr->a_family == AF_INET)
728 return -NLE_SRCRT_NOSUPPORT;
730 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
731 if (addr->a_family != route->rt_family)
732 return -NLE_AF_MISMATCH;
734 route->rt_family = addr->a_family;
740 route->rt_src = addr;
741 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
746 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
748 return route->rt_src;
751 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
756 route->rt_type = type;
757 route->ce_mask |= ROUTE_ATTR_TYPE;
762 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
764 return route->rt_type;
767 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
769 route->rt_flag_mask |= flags;
770 route->rt_flags |= flags;
771 route->ce_mask |= ROUTE_ATTR_FLAGS;
774 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
776 route->rt_flag_mask |= flags;
777 route->rt_flags &= ~flags;
778 route->ce_mask |= ROUTE_ATTR_FLAGS;
781 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
783 return route->rt_flags;
786 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
788 if (metric > RTAX_MAX || metric < 1)
791 route->rt_metrics[metric - 1] = value;
793 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
794 route->rt_nmetrics++;
795 route->rt_metrics_mask |= (1 << (metric - 1));
798 route->ce_mask |= ROUTE_ATTR_METRICS;
803 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
805 if (metric > RTAX_MAX || metric < 1)
808 if (route->rt_metrics_mask & (1 << (metric - 1))) {
809 route->rt_nmetrics--;
810 route->rt_metrics_mask &= ~(1 << (metric - 1));
816 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
818 if (metric > RTAX_MAX || metric < 1)
821 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
822 return -NLE_OBJ_NOTFOUND;
825 *value = route->rt_metrics[metric - 1];
830 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
832 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
833 if (addr->a_family != route->rt_family)
834 return -NLE_AF_MISMATCH;
836 route->rt_family = addr->a_family;
838 if (route->rt_pref_src)
842 route->rt_pref_src = addr;
843 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
848 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
850 return route->rt_pref_src;
853 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
855 route->rt_iif = ifindex;
856 route->ce_mask |= ROUTE_ATTR_IIF;
859 int rtnl_route_get_iif(
struct rtnl_route *route)
861 return route->rt_iif;
864 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
866 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
868 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
871 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
873 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
875 nl_list_del(&nh->rtnh_list);
879 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
881 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
882 return &route->rt_nexthops;
887 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
889 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
890 return route->rt_nr_nh;
895 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
896 void (*cb)(
struct rtnl_nexthop *,
void *),
899 struct rtnl_nexthop *nh;
901 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
902 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
908 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
910 struct rtnl_nexthop *nh;
913 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
915 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
916 if (i == n)
return nh;
923 void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
925 route->rt_ttl_propagate = ttl_prop;
926 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
929 int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
933 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
934 return -NLE_MISSING_ATTR;
935 return route->rt_ttl_propagate;
960 if (route->rt_type == RTN_LOCAL)
961 return RT_SCOPE_HOST;
963 if (route->rt_family == AF_MPLS)
964 return RT_SCOPE_UNIVERSE;
966 if (!nl_list_empty(&route->rt_nexthops)) {
967 struct rtnl_nexthop *nh;
973 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
974 if (nh->rtnh_gateway)
975 return RT_SCOPE_UNIVERSE;
979 return RT_SCOPE_LINK;
984 static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
986 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
989 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
992 static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
998 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1009 static struct nla_policy route_policy[RTA_MAX+1] = {
1011 [RTA_OIF] = { .type =
NLA_U32 },
1012 [RTA_PRIORITY] = { .type =
NLA_U32 },
1013 [RTA_FLOW] = { .type =
NLA_U32 },
1014 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1017 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1019 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1022 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1024 struct rtnl_nexthop *nh = NULL;
1025 struct rtnexthop *rtnh =
nla_data(attr);
1029 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1030 nh = rtnl_route_nh_alloc();
1034 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1035 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1036 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1038 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1039 struct nlattr *ntb[RTA_MAX + 1];
1041 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1043 rtnh->rtnh_len -
sizeof(*rtnh),
1048 if (ntb[RTA_GATEWAY]) {
1049 struct nl_addr *addr;
1058 rtnl_route_nh_set_gateway(nh, addr);
1062 if (ntb[RTA_FLOW]) {
1066 rtnl_route_nh_set_realms(nh, realms);
1069 if (ntb[RTA_NEWDST]) {
1070 struct nl_addr *addr;
1077 err = rtnl_route_nh_set_newdst(nh, addr);
1084 struct nl_addr *addr;
1086 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1090 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, nh);
1106 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1107 rtnh = RTNH_NEXT(rtnh);
1113 rtnl_route_nh_free(nh);
1118 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1121 struct rtnl_route *route;
1122 struct nlattr *tb[RTA_MAX + 1];
1123 struct nl_addr *src = NULL, *dst = NULL, *addr;
1124 struct rtnl_nexthop *old_nh = NULL;
1127 route = rtnl_route_alloc();
1133 route->ce_msgtype = nlh->nlmsg_type;
1135 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1140 route->rt_family = family = rtm->rtm_family;
1141 route->rt_tos = rtm->rtm_tos;
1142 route->rt_table = rtm->rtm_table;
1143 route->rt_type = rtm->rtm_type;
1144 route->rt_scope = rtm->rtm_scope;
1145 route->rt_protocol = rtm->rtm_protocol;
1146 route->rt_flags = rtm->rtm_flags;
1149 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1150 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1151 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1157 if (family != AF_MPLS)
1158 route->ce_mask |= ROUTE_ATTR_PRIO;
1170 err = rtnl_route_set_dst(route, dst);
1179 }
else if (rtm->rtm_src_len)
1185 rtnl_route_set_src(route, src);
1190 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1193 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1195 if (tb[RTA_PRIORITY])
1196 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1198 if (tb[RTA_PREFSRC]) {
1201 rtnl_route_set_pref_src(route, addr);
1205 if (tb[RTA_METRICS]) {
1206 struct nlattr *mtb[RTAX_MAX + 1];
1213 for (i = 1; i <= RTAX_MAX; i++) {
1214 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1216 if (rtnl_route_set_metric(route, i, m) < 0)
1222 if (tb[RTA_MULTIPATH])
1223 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1226 if (tb[RTA_CACHEINFO]) {
1227 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1228 sizeof(route->rt_cacheinfo));
1229 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1233 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1236 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1239 if (tb[RTA_GATEWAY]) {
1240 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1246 rtnl_route_nh_set_gateway(old_nh, addr);
1251 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1254 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1257 if (tb[RTA_NEWDST]) {
1258 struct nl_addr *addr;
1264 err = rtnl_route_nh_set_newdst(old_nh, addr);
1271 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1272 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1274 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1278 err = rtnl_route_nh_set_via(old_nh, addr);
1284 if (tb[RTA_TTL_PROPAGATE]) {
1285 rtnl_route_set_ttl_propagate(route,
1289 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1290 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1291 tb[RTA_ENCAP_TYPE], old_nh);
1297 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1298 if (route->rt_nr_nh == 0) {
1302 rtnl_route_add_nexthop(route, old_nh);
1306 struct rtnl_nexthop *first;
1308 first = nl_list_first_entry(&route->rt_nexthops,
1309 struct rtnl_nexthop,
1314 if (rtnl_route_nh_compare(old_nh, first,
1315 old_nh->ce_mask, 0)) {
1320 rtnl_route_nh_free(old_nh);
1328 rtnl_route_put(route);
1336 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1339 struct nlattr *metrics;
1340 struct rtmsg rtmsg = {
1341 .rtm_family = route->rt_family,
1342 .rtm_tos = route->rt_tos,
1343 .rtm_table = route->rt_table,
1344 .rtm_protocol = route->rt_protocol,
1345 .rtm_scope = route->rt_scope,
1346 .rtm_type = route->rt_type,
1347 .rtm_flags = route->rt_flags,
1350 if (route->rt_dst == NULL)
1351 return -NLE_MISSING_ATTR;
1357 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1360 if (rtnl_route_get_nnexthops(route) == 1) {
1361 struct rtnl_nexthop *nh;
1362 nh = rtnl_route_nexthop_n(route, 0);
1363 rtmsg.rtm_flags |= nh->rtnh_flags;
1366 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1367 goto nla_put_failure;
1373 if (route->rt_family != AF_MPLS)
1379 if (route->ce_mask & ROUTE_ATTR_PRIO)
1382 if (route->ce_mask & ROUTE_ATTR_SRC)
1385 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1388 if (route->ce_mask & ROUTE_ATTR_IIF)
1391 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1392 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1394 if (route->rt_nmetrics > 0) {
1398 if (metrics == NULL)
1399 goto nla_put_failure;
1401 for (i = 1; i <= RTAX_MAX; i++) {
1402 if (!rtnl_route_get_metric(route, i, &val))
1409 if (rtnl_route_get_nnexthops(route) == 1) {
1410 struct rtnl_nexthop *nh;
1412 nh = rtnl_route_nexthop_n(route, 0);
1413 if (nh->rtnh_gateway)
1415 if (nh->rtnh_ifindex)
1417 if (nh->rtnh_realms)
1419 if (nh->rtnh_newdst)
1421 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1422 goto nla_put_failure;
1423 if (nh->rtnh_encap &&
1424 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1425 goto nla_put_failure;
1426 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1427 struct nlattr *multipath;
1428 struct rtnl_nexthop *nh;
1431 goto nla_put_failure;
1433 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1434 struct rtnexthop *rtnh;
1438 goto nla_put_failure;
1440 rtnh->rtnh_flags = nh->rtnh_flags;
1441 rtnh->rtnh_hops = nh->rtnh_weight;
1442 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1444 if (nh->rtnh_gateway)
1448 if (nh->rtnh_newdst)
1452 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1453 goto nla_put_failure;
1455 if (nh->rtnh_realms)
1458 if (nh->rtnh_encap &&
1459 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1460 goto nla_put_failure;
1462 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1472 return -NLE_MSGSIZE;
1476 struct nl_object_ops route_obj_ops = {
1477 .oo_name =
"route/route",
1478 .oo_size =
sizeof(
struct rtnl_route),
1479 .oo_constructor = route_constructor,
1480 .oo_free_data = route_free_data,
1481 .oo_clone = route_clone,
1487 .oo_compare = route_compare,
1488 .oo_keygen = route_keygen,
1489 .oo_update = route_update,
1490 .oo_attrs2str = route_attrs2str,
1491 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1492 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1494 .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.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
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.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
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_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
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 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.
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.