33 #include <netlink-private/netlink.h> 34 #include <netlink-private/utils.h> 35 #include <netlink/netlink.h> 36 #include <netlink/cache.h> 37 #include <netlink/utils.h> 38 #include <netlink/data.h> 39 #include <netlink/hashtable.h> 40 #include <netlink/route/rtnl.h> 41 #include <netlink/route/route.h> 42 #include <netlink/route/link.h> 43 #include <netlink/route/nexthop.h> 44 #include <linux/in_route.h> 47 #define ROUTE_ATTR_FAMILY 0x000001 48 #define ROUTE_ATTR_TOS 0x000002 49 #define ROUTE_ATTR_TABLE 0x000004 50 #define ROUTE_ATTR_PROTOCOL 0x000008 51 #define ROUTE_ATTR_SCOPE 0x000010 52 #define ROUTE_ATTR_TYPE 0x000020 53 #define ROUTE_ATTR_FLAGS 0x000040 54 #define ROUTE_ATTR_DST 0x000080 55 #define ROUTE_ATTR_SRC 0x000100 56 #define ROUTE_ATTR_IIF 0x000200 57 #define ROUTE_ATTR_OIF 0x000400 58 #define ROUTE_ATTR_GATEWAY 0x000800 59 #define ROUTE_ATTR_PRIO 0x001000 60 #define ROUTE_ATTR_PREF_SRC 0x002000 61 #define ROUTE_ATTR_METRICS 0x004000 62 #define ROUTE_ATTR_MULTIPATH 0x008000 63 #define ROUTE_ATTR_REALMS 0x010000 64 #define ROUTE_ATTR_CACHEINFO 0x020000 67 static void route_constructor(
struct nl_object *c)
69 struct rtnl_route *r = (
struct rtnl_route *) c;
71 r->rt_family = AF_UNSPEC;
72 r->rt_scope = RT_SCOPE_NOWHERE;
73 r->rt_table = RT_TABLE_MAIN;
74 r->rt_protocol = RTPROT_STATIC;
75 r->rt_type = RTN_UNICAST;
78 nl_init_list_head(&r->rt_nexthops);
81 static void route_free_data(
struct nl_object *c)
83 struct rtnl_route *r = (
struct rtnl_route *) c;
84 struct rtnl_nexthop *nh, *tmp;
93 nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
94 rtnl_route_remove_nexthop(r, nh);
95 rtnl_route_nh_free(nh);
99 static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
101 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
102 struct rtnl_route *src = (
struct rtnl_route *) _src;
103 struct rtnl_nexthop *nh, *
new;
113 if (src->rt_pref_src)
120 nl_init_list_head(&dst->rt_nexthops);
121 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
122 new = rtnl_route_nh_clone(nh);
126 rtnl_route_add_nexthop(dst,
new);
132 static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
134 struct rtnl_route *r = (
struct rtnl_route *) a;
135 int cache = 0, flags;
138 if (r->rt_flags & RTM_F_CLONED)
141 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
146 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
152 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
154 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
156 if (r->ce_mask & ROUTE_ATTR_TYPE)
158 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
160 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
161 nl_dump(p,
"tos %#x ", r->rt_tos);
163 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
164 struct rtnl_nexthop *nh;
166 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
167 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
168 rtnl_route_nh_dump(nh, p);
172 flags = r->rt_flags & ~(RTM_F_CLONED);
173 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
177 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \ 178 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 181 PRINT_FLAG(PERVASIVE);
184 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \ 185 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 187 PRINT_FLAG(EQUALIZE);
191 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \ 192 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); } 194 PRINT_FLAG(REDIRECTED);
195 PRINT_FLAG(DOREDIRECT);
196 PRINT_FLAG(DIRECTSRC);
198 PRINT_FLAG(BROADCAST);
199 PRINT_FLAG(MULTICAST);
209 static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
211 struct rtnl_route *r = (
struct rtnl_route *) a;
212 struct nl_cache *link_cache;
218 route_dump_line(a, p);
219 nl_dump_line(p,
" ");
221 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
222 nl_dump(p,
"preferred-src %s ",
225 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
227 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
229 if (r->ce_mask & ROUTE_ATTR_PRIO)
230 nl_dump(p,
"priority %#x ", r->rt_prio);
232 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
234 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
236 if (r->ce_mask & ROUTE_ATTR_IIF) {
242 nl_dump(p,
"iif %d ", r->rt_iif);
245 if (r->ce_mask & ROUTE_ATTR_SRC)
250 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
251 struct rtnl_nexthop *nh;
253 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
254 nl_dump_line(p,
" ");
255 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
256 rtnl_route_nh_dump(nh, p);
261 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
262 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
263 r->rt_cacheinfo.rtci_error,
264 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
267 if (r->ce_mask & ROUTE_ATTR_METRICS) {
268 nl_dump_line(p,
" metrics [");
269 for (i = 0; i < RTAX_MAX; i++)
270 if (r->rt_metrics_mask & (1 << i))
272 rtnl_route_metric2str(i+1,
279 nl_cache_put(link_cache);
282 static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
284 struct rtnl_route *route = (
struct rtnl_route *) obj;
286 route_dump_details(obj, p);
288 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
291 nl_dump_line(p,
" used %u refcnt %u last-use %us " 293 ci->rtci_used, ci->rtci_clntref,
299 static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
302 struct rtnl_route *route = (
struct rtnl_route *) obj;
303 unsigned int rkey_sz;
304 struct nl_addr *addr = NULL;
305 struct route_hash_key {
311 } __attribute__((packed)) *rkey;
313 char buf[INET6_ADDRSTRLEN+5];
317 addr = route->rt_dst;
319 rkey_sz =
sizeof(*rkey);
322 rkey = calloc(1, rkey_sz);
324 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
328 rkey->rt_family = route->rt_family;
329 rkey->rt_tos = route->rt_tos;
330 rkey->rt_table = route->rt_table;
331 rkey->rt_prio = route->rt_prio;
336 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
338 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d " 339 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
340 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
348 static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
349 uint64_t attrs,
int flags)
351 struct rtnl_route *a = (
struct rtnl_route *) _a;
352 struct rtnl_route *b = (
struct rtnl_route *) _b;
353 struct rtnl_nexthop *nh_a, *nh_b;
357 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR) 359 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
360 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
361 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
362 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
363 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
364 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
365 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
366 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
367 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
368 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
369 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
372 if (flags & LOOSE_COMPARISON) {
373 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
375 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
377 if (!rtnl_route_nh_compare(nh_a, nh_b,
388 for (i = 0; i < RTAX_MAX - 1; i++) {
389 if (a->rt_metrics_mask & (1 << i) &&
390 (!(b->rt_metrics_mask & (1 << i)) ||
391 a->rt_metrics[i] != b->rt_metrics[i]))
392 diff |= ROUTE_DIFF(METRICS, 1);
395 diff |= ROUTE_DIFF(FLAGS,
396 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
398 if (a->rt_nr_nh != b->rt_nr_nh)
402 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
404 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
406 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
417 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
419 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
421 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
430 for (i = 0; i < RTAX_MAX - 1; i++) {
431 if ((a->rt_metrics_mask & (1 << i)) ^
432 (b->rt_metrics_mask & (1 << i)))
433 diff |= ROUTE_DIFF(METRICS, 1);
435 diff |= ROUTE_DIFF(METRICS,
436 a->rt_metrics[i] != b->rt_metrics[i]);
439 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
446 diff |= ROUTE_DIFF(MULTIPATH, 1);
452 static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
454 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
455 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
456 struct rtnl_nexthop *new_nh;
457 int action = new_obj->ce_msgtype;
459 char buf[INET6_ADDRSTRLEN+5];
469 if (new_route->rt_family != AF_INET6 ||
470 new_route->rt_table == RT_TABLE_LOCAL)
471 return -NLE_OPNOTSUPP;
477 if (rtnl_route_get_nnexthops(new_route) != 1)
478 return -NLE_OPNOTSUPP;
485 new_nh = rtnl_route_nexthop_n(new_route, 0);
486 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
487 return -NLE_OPNOTSUPP;
490 case RTM_NEWROUTE : {
491 struct rtnl_nexthop *cloned_nh;
496 cloned_nh = rtnl_route_nh_clone(new_nh);
499 rtnl_route_add_nexthop(old_route, cloned_nh);
501 NL_DBG(2,
"Route obj %p updated. Added " 502 "nexthop %p via %s\n", old_route, cloned_nh,
507 case RTM_DELROUTE : {
508 struct rtnl_nexthop *old_nh;
516 if (rtnl_route_get_nnexthops(old_route) <= 1)
517 return -NLE_OPNOTSUPP;
522 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
524 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
526 rtnl_route_remove_nexthop(old_route, old_nh);
528 NL_DBG(2,
"Route obj %p updated. Removed " 529 "nexthop %p via %s\n", old_route,
534 rtnl_route_nh_free(old_nh);
541 NL_DBG(2,
"Unknown action associated " 542 "to object %p during route update\n", new_obj);
543 return -NLE_OPNOTSUPP;
549 static const struct trans_tbl route_attrs[] = {
550 __ADD(ROUTE_ATTR_FAMILY, family),
551 __ADD(ROUTE_ATTR_TOS, tos),
552 __ADD(ROUTE_ATTR_TABLE, table),
553 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
554 __ADD(ROUTE_ATTR_SCOPE, scope),
555 __ADD(ROUTE_ATTR_TYPE, type),
556 __ADD(ROUTE_ATTR_FLAGS, flags),
557 __ADD(ROUTE_ATTR_DST, dst),
558 __ADD(ROUTE_ATTR_SRC, src),
559 __ADD(ROUTE_ATTR_IIF, iif),
560 __ADD(ROUTE_ATTR_OIF, oif),
561 __ADD(ROUTE_ATTR_GATEWAY, gateway),
562 __ADD(ROUTE_ATTR_PRIO, prio),
563 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
564 __ADD(ROUTE_ATTR_METRICS, metrics),
565 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
566 __ADD(ROUTE_ATTR_REALMS, realms),
567 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
570 static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
572 return __flags2str(attrs, buf, len, route_attrs,
573 ARRAY_SIZE(route_attrs));
581 struct rtnl_route *rtnl_route_alloc(
void)
586 void rtnl_route_get(
struct rtnl_route *route)
591 void rtnl_route_put(
struct rtnl_route *route)
603 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
605 route->rt_table = table;
606 route->ce_mask |= ROUTE_ATTR_TABLE;
609 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
611 return route->rt_table;
614 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
616 route->rt_scope = scope;
617 route->ce_mask |= ROUTE_ATTR_SCOPE;
620 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
622 return route->rt_scope;
625 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
628 route->ce_mask |= ROUTE_ATTR_TOS;
631 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
633 return route->rt_tos;
636 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
638 route->rt_protocol = protocol;
639 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
642 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
644 return route->rt_protocol;
647 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
649 route->rt_prio = prio;
650 route->ce_mask |= ROUTE_ATTR_PRIO;
653 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
655 return route->rt_prio;
658 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
660 if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
661 return -NLE_AF_NOSUPPORT;
663 route->rt_family = family;
664 route->ce_mask |= ROUTE_ATTR_FAMILY;
669 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
671 return route->rt_family;
674 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
676 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
677 if (addr->a_family != route->rt_family)
678 return -NLE_AF_MISMATCH;
680 route->rt_family = addr->a_family;
686 route->rt_dst = addr;
688 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
693 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
695 return route->rt_dst;
698 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
700 if (addr->a_family == AF_INET)
701 return -NLE_SRCRT_NOSUPPORT;
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_src = addr;
714 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
719 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
721 return route->rt_src;
724 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
729 route->rt_type = type;
730 route->ce_mask |= ROUTE_ATTR_TYPE;
735 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
737 return route->rt_type;
740 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
742 route->rt_flag_mask |= flags;
743 route->rt_flags |= flags;
744 route->ce_mask |= ROUTE_ATTR_FLAGS;
747 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
749 route->rt_flag_mask |= flags;
750 route->rt_flags &= ~flags;
751 route->ce_mask |= ROUTE_ATTR_FLAGS;
754 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
756 return route->rt_flags;
759 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
761 if (metric > RTAX_MAX || metric < 1)
764 route->rt_metrics[metric - 1] = value;
766 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
767 route->rt_nmetrics++;
768 route->rt_metrics_mask |= (1 << (metric - 1));
771 route->ce_mask |= ROUTE_ATTR_METRICS;
776 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
778 if (metric > RTAX_MAX || metric < 1)
781 if (route->rt_metrics_mask & (1 << (metric - 1))) {
782 route->rt_nmetrics--;
783 route->rt_metrics_mask &= ~(1 << (metric - 1));
789 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
791 if (metric > RTAX_MAX || metric < 1)
794 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
795 return -NLE_OBJ_NOTFOUND;
798 *value = route->rt_metrics[metric - 1];
803 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
805 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
806 if (addr->a_family != route->rt_family)
807 return -NLE_AF_MISMATCH;
809 route->rt_family = addr->a_family;
811 if (route->rt_pref_src)
815 route->rt_pref_src = addr;
816 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
821 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
823 return route->rt_pref_src;
826 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
828 route->rt_iif = ifindex;
829 route->ce_mask |= ROUTE_ATTR_IIF;
832 int rtnl_route_get_iif(
struct rtnl_route *route)
834 return route->rt_iif;
837 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
839 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
841 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
844 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
846 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
848 nl_list_del(&nh->rtnh_list);
852 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
854 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
855 return &route->rt_nexthops;
860 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
862 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
863 return route->rt_nr_nh;
868 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
869 void (*cb)(
struct rtnl_nexthop *,
void *),
872 struct rtnl_nexthop *nh;
874 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
875 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
881 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
883 struct rtnl_nexthop *nh;
886 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
888 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
889 if (i == n)
return nh;
918 if (route->rt_type == RTN_LOCAL)
919 return RT_SCOPE_HOST;
921 if (!nl_list_empty(&route->rt_nexthops)) {
922 struct rtnl_nexthop *nh;
928 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
929 if (nh->rtnh_gateway)
930 return RT_SCOPE_UNIVERSE;
934 return RT_SCOPE_LINK;
939 static struct nla_policy route_policy[RTA_MAX+1] = {
941 [RTA_OIF] = { .type =
NLA_U32 },
942 [RTA_PRIORITY] = { .type =
NLA_U32 },
943 [RTA_FLOW] = { .type =
NLA_U32 },
944 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
949 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
951 struct rtnl_nexthop *nh = NULL;
952 struct rtnexthop *rtnh =
nla_data(attr);
956 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
957 nh = rtnl_route_nh_alloc();
961 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
962 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
963 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
965 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
966 struct nlattr *ntb[RTA_MAX + 1];
968 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
970 rtnh->rtnh_len -
sizeof(*rtnh),
975 if (ntb[RTA_GATEWAY]) {
976 struct nl_addr *addr;
985 rtnl_route_nh_set_gateway(nh, addr);
993 rtnl_route_nh_set_realms(nh, realms);
997 rtnl_route_add_nexthop(route, nh);
998 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
999 rtnh = RTNH_NEXT(rtnh);
1005 rtnl_route_nh_free(nh);
1010 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1013 struct rtnl_route *route;
1014 struct nlattr *tb[RTA_MAX + 1];
1015 struct nl_addr *src = NULL, *dst = NULL, *addr;
1016 struct rtnl_nexthop *old_nh = NULL;
1019 route = rtnl_route_alloc();
1025 route->ce_msgtype = nlh->nlmsg_type;
1027 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1032 route->rt_family = family = rtm->rtm_family;
1033 route->rt_tos = rtm->rtm_tos;
1034 route->rt_table = rtm->rtm_table;
1035 route->rt_type = rtm->rtm_type;
1036 route->rt_scope = rtm->rtm_scope;
1037 route->rt_protocol = rtm->rtm_protocol;
1038 route->rt_flags = rtm->rtm_flags;
1041 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1042 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1043 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1044 ROUTE_ATTR_FLAGS | ROUTE_ATTR_PRIO;
1056 err = rtnl_route_set_dst(route, dst);
1065 }
else if (rtm->rtm_src_len)
1071 rtnl_route_set_src(route, src);
1076 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1079 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1081 if (tb[RTA_PRIORITY])
1082 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1084 if (tb[RTA_PREFSRC]) {
1087 rtnl_route_set_pref_src(route, addr);
1091 if (tb[RTA_METRICS]) {
1092 struct nlattr *mtb[RTAX_MAX + 1];
1099 for (i = 1; i <= RTAX_MAX; i++) {
1100 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1102 if (rtnl_route_set_metric(route, i, m) < 0)
1108 if (tb[RTA_MULTIPATH])
1109 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1112 if (tb[RTA_CACHEINFO]) {
1113 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1114 sizeof(route->rt_cacheinfo));
1115 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1119 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1122 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1125 if (tb[RTA_GATEWAY]) {
1126 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1132 rtnl_route_nh_set_gateway(old_nh, addr);
1137 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1140 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1144 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1145 if (route->rt_nr_nh == 0) {
1149 rtnl_route_add_nexthop(route, old_nh);
1153 struct rtnl_nexthop *first;
1155 first = nl_list_first_entry(&route->rt_nexthops,
1156 struct rtnl_nexthop,
1161 if (rtnl_route_nh_compare(old_nh, first,
1162 old_nh->ce_mask, 0)) {
1167 rtnl_route_nh_free(old_nh);
1175 rtnl_route_put(route);
1183 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1186 struct nlattr *metrics;
1187 struct rtmsg rtmsg = {
1188 .rtm_family = route->rt_family,
1189 .rtm_tos = route->rt_tos,
1190 .rtm_table = route->rt_table,
1191 .rtm_protocol = route->rt_protocol,
1192 .rtm_scope = route->rt_scope,
1193 .rtm_type = route->rt_type,
1194 .rtm_flags = route->rt_flags,
1197 if (route->rt_dst == NULL)
1198 return -NLE_MISSING_ATTR;
1204 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1207 if (rtnl_route_get_nnexthops(route) == 1) {
1208 struct rtnl_nexthop *nh;
1209 nh = rtnl_route_nexthop_n(route, 0);
1210 rtmsg.rtm_flags |= nh->rtnh_flags;
1213 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1214 goto nla_put_failure;
1224 if (route->ce_mask & ROUTE_ATTR_SRC)
1227 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1230 if (route->ce_mask & ROUTE_ATTR_IIF)
1233 if (route->rt_nmetrics > 0) {
1237 if (metrics == NULL)
1238 goto nla_put_failure;
1240 for (i = 1; i <= RTAX_MAX; i++) {
1241 if (!rtnl_route_get_metric(route, i, &val))
1248 if (rtnl_route_get_nnexthops(route) == 1) {
1249 struct rtnl_nexthop *nh;
1251 nh = rtnl_route_nexthop_n(route, 0);
1252 if (nh->rtnh_gateway)
1254 if (nh->rtnh_ifindex)
1256 if (nh->rtnh_realms)
1258 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1259 struct nlattr *multipath;
1260 struct rtnl_nexthop *nh;
1263 goto nla_put_failure;
1265 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1266 struct rtnexthop *rtnh;
1270 goto nla_put_failure;
1272 rtnh->rtnh_flags = nh->rtnh_flags;
1273 rtnh->rtnh_hops = nh->rtnh_weight;
1274 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1276 if (nh->rtnh_gateway)
1280 if (nh->rtnh_realms)
1283 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1293 return -NLE_MSGSIZE;
1297 struct nl_object_ops route_obj_ops = {
1298 .oo_name =
"route/route",
1299 .oo_size =
sizeof(
struct rtnl_route),
1300 .oo_constructor = route_constructor,
1301 .oo_free_data = route_free_data,
1302 .oo_clone = route_clone,
1308 .oo_compare = route_compare,
1309 .oo_keygen = route_keygen,
1310 .oo_update = route_update,
1311 .oo_attrs2str = route_attrs2str,
1312 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1313 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
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.
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.
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 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.
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.