33 #include <netlink-local.h>
34 #include <netlink/netlink.h>
35 #include <netlink/cache.h>
36 #include <netlink/utils.h>
37 #include <netlink/data.h>
38 #include <netlink/route/rtnl.h>
39 #include <netlink/route/route.h>
40 #include <netlink/route/link.h>
41 #include <netlink/route/nexthop.h>
44 #define ROUTE_ATTR_FAMILY 0x000001
45 #define ROUTE_ATTR_TOS 0x000002
46 #define ROUTE_ATTR_TABLE 0x000004
47 #define ROUTE_ATTR_PROTOCOL 0x000008
48 #define ROUTE_ATTR_SCOPE 0x000010
49 #define ROUTE_ATTR_TYPE 0x000020
50 #define ROUTE_ATTR_FLAGS 0x000040
51 #define ROUTE_ATTR_DST 0x000080
52 #define ROUTE_ATTR_SRC 0x000100
53 #define ROUTE_ATTR_IIF 0x000200
54 #define ROUTE_ATTR_OIF 0x000400
55 #define ROUTE_ATTR_GATEWAY 0x000800
56 #define ROUTE_ATTR_PRIO 0x001000
57 #define ROUTE_ATTR_PREF_SRC 0x002000
58 #define ROUTE_ATTR_METRICS 0x004000
59 #define ROUTE_ATTR_MULTIPATH 0x008000
60 #define ROUTE_ATTR_REALMS 0x010000
61 #define ROUTE_ATTR_CACHEINFO 0x020000
64 static void route_constructor(
struct nl_object *c)
66 struct rtnl_route *r = (
struct rtnl_route *) c;
68 r->rt_family = AF_UNSPEC;
69 r->rt_scope = RT_SCOPE_NOWHERE;
70 r->rt_table = RT_TABLE_MAIN;
71 r->rt_protocol = RTPROT_STATIC;
72 r->rt_type = RTN_UNICAST;
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;
85 nl_addr_put(r->rt_dst);
86 nl_addr_put(r->rt_src);
87 nl_addr_put(r->rt_pref_src);
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);
97 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
98 struct rtnl_route *src = (
struct rtnl_route *) _src;
99 struct rtnl_nexthop *nh, *
new;
109 if (src->rt_pref_src)
113 nl_init_list_head(&dst->rt_nexthops);
114 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
115 new = rtnl_route_nh_clone(nh);
119 rtnl_route_add_nexthop(dst,
new);
127 struct rtnl_route *r = (
struct rtnl_route *) a;
128 int cache = 0, flags;
131 if (r->rt_flags & RTM_F_CLONED)
134 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
139 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
145 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
147 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
149 if (r->ce_mask & ROUTE_ATTR_TYPE)
151 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
153 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
154 nl_dump(p,
"tos %#x ", r->rt_tos);
156 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
157 struct rtnl_nexthop *nh;
159 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
160 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
161 rtnl_route_nh_dump(nh, p);
165 flags = r->rt_flags & ~(RTM_F_CLONED);
166 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
170 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
171 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
174 PRINT_FLAG(PERVASIVE);
177 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
178 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
180 PRINT_FLAG(EQUALIZE);
184 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
185 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
187 PRINT_FLAG(REDIRECTED);
188 PRINT_FLAG(DOREDIRECT);
189 PRINT_FLAG(DIRECTSRC);
191 PRINT_FLAG(BROADCAST);
192 PRINT_FLAG(MULTICAST);
204 struct rtnl_route *r = (
struct rtnl_route *) a;
205 struct nl_cache *link_cache;
211 route_dump_line(a, p);
212 nl_dump_line(p,
" ");
214 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
215 nl_dump(p,
"preferred-src %s ",
218 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
220 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
222 if (r->ce_mask & ROUTE_ATTR_PRIO)
223 nl_dump(p,
"priority %#x ", r->rt_prio);
225 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
227 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
229 if (r->ce_mask & ROUTE_ATTR_IIF) {
235 nl_dump(p,
"iif %d ", r->rt_iif);
238 if (r->ce_mask & ROUTE_ATTR_SRC)
243 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
244 struct rtnl_nexthop *nh;
246 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
247 nl_dump_line(p,
" ");
248 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
249 rtnl_route_nh_dump(nh, p);
254 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
255 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
256 r->rt_cacheinfo.rtci_error,
257 strerror(-r->rt_cacheinfo.rtci_error));
260 if (r->ce_mask & ROUTE_ATTR_METRICS) {
261 nl_dump_line(p,
" metrics [");
262 for (i = 0; i < RTAX_MAX; i++)
263 if (r->rt_metrics_mask & (1 << i))
265 rtnl_route_metric2str(i+1,
274 struct rtnl_route *route = (
struct rtnl_route *) obj;
276 route_dump_details(obj, p);
278 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
281 nl_dump_line(p,
" used %u refcnt %u last-use %us "
283 ci->rtci_used, ci->rtci_clntref,
290 uint32_t attrs,
int flags)
292 struct rtnl_route *a = (
struct rtnl_route *) _a;
293 struct rtnl_route *b = (
struct rtnl_route *) _b;
294 struct rtnl_nexthop *nh_a, *nh_b;
295 int i, diff = 0, found;
297 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
299 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
300 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
301 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
302 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
303 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
304 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
305 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
306 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
307 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
308 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
309 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
312 if (flags & LOOSE_COMPARISON) {
313 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
315 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
317 if (!rtnl_route_nh_compare(nh_a, nh_b,
328 for (i = 0; i < RTAX_MAX - 1; i++) {
329 if (a->rt_metrics_mask & (1 << i) &&
330 (!(b->rt_metrics_mask & (1 << i)) ||
331 a->rt_metrics[i] != b->rt_metrics[i]))
332 ROUTE_DIFF(METRICS, 1);
335 diff |= ROUTE_DIFF(FLAGS,
336 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
338 if (a->rt_nr_nh != a->rt_nr_nh)
342 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
344 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
346 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
357 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
359 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
361 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
370 for (i = 0; i < RTAX_MAX - 1; i++) {
371 if ((a->rt_metrics_mask & (1 << i)) ^
372 (b->rt_metrics_mask & (1 << i)))
373 diff |= ROUTE_DIFF(METRICS, 1);
375 diff |= ROUTE_DIFF(METRICS,
376 a->rt_metrics[i] != b->rt_metrics[i]);
379 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
386 diff |= ROUTE_DIFF(MULTIPATH, 1);
392 static const struct trans_tbl route_attrs[] = {
393 __ADD(ROUTE_ATTR_FAMILY, family)
394 __ADD(ROUTE_ATTR_TOS, tos)
395 __ADD(ROUTE_ATTR_TABLE, table)
396 __ADD(ROUTE_ATTR_PROTOCOL, protocol)
397 __ADD(ROUTE_ATTR_SCOPE, scope)
398 __ADD(ROUTE_ATTR_TYPE, type)
399 __ADD(ROUTE_ATTR_FLAGS, flags)
400 __ADD(ROUTE_ATTR_DST, dst)
401 __ADD(ROUTE_ATTR_SRC, src)
402 __ADD(ROUTE_ATTR_IIF, iif)
403 __ADD(ROUTE_ATTR_OIF, oif)
404 __ADD(ROUTE_ATTR_GATEWAY, gateway)
405 __ADD(ROUTE_ATTR_PRIO, prio)
406 __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
407 __ADD(ROUTE_ATTR_METRICS, metrics)
408 __ADD(ROUTE_ATTR_MULTIPATH, multipath)
409 __ADD(ROUTE_ATTR_REALMS, realms)
410 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
413 static
char *route_attrs2str(
int attrs,
char *buf,
size_t len)
415 return __flags2str(attrs, buf, len, route_attrs,
416 ARRAY_SIZE(route_attrs));
424 struct rtnl_route *rtnl_route_alloc(
void)
429 void rtnl_route_get(
struct rtnl_route *route)
434 void rtnl_route_put(
struct rtnl_route *route)
446 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
448 route->rt_table = table;
449 route->ce_mask |= ROUTE_ATTR_TABLE;
452 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
454 return route->rt_table;
457 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
459 route->rt_scope = scope;
460 route->ce_mask |= ROUTE_ATTR_SCOPE;
463 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
465 return route->rt_scope;
468 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
471 route->ce_mask |= ROUTE_ATTR_TOS;
474 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
476 return route->rt_tos;
479 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
481 route->rt_protocol = protocol;
482 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
485 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
487 return route->rt_protocol;
490 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
492 route->rt_prio = prio;
493 route->ce_mask |= ROUTE_ATTR_PRIO;
496 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
498 return route->rt_prio;
501 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
503 if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
504 return -NLE_AF_NOSUPPORT;
506 route->rt_family = family;
507 route->ce_mask |= ROUTE_ATTR_FAMILY;
512 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
514 return route->rt_family;
517 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
519 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
520 if (addr->a_family != route->rt_family)
521 return -NLE_AF_MISMATCH;
523 route->rt_family = addr->a_family;
526 nl_addr_put(route->rt_dst);
529 route->rt_dst = addr;
531 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
536 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
538 return route->rt_dst;
541 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
543 if (addr->a_family == AF_INET)
544 return -NLE_SRCRT_NOSUPPORT;
546 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
547 if (addr->a_family != route->rt_family)
548 return -NLE_AF_MISMATCH;
550 route->rt_family = addr->a_family;
553 nl_addr_put(route->rt_src);
556 route->rt_src = addr;
557 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
562 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
564 return route->rt_src;
567 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
572 route->rt_type = type;
573 route->ce_mask |= ROUTE_ATTR_TYPE;
578 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
580 return route->rt_type;
583 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
585 route->rt_flag_mask |= flags;
586 route->rt_flags |= flags;
587 route->ce_mask |= ROUTE_ATTR_FLAGS;
590 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
592 route->rt_flag_mask |= flags;
593 route->rt_flags &= ~flags;
594 route->ce_mask |= ROUTE_ATTR_FLAGS;
597 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
599 return route->rt_flags;
602 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
604 if (metric > RTAX_MAX || metric < 1)
607 route->rt_metrics[metric - 1] = value;
609 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
610 route->rt_nmetrics++;
611 route->rt_metrics_mask |= (1 << (metric - 1));
614 route->ce_mask |= ROUTE_ATTR_METRICS;
619 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
621 if (metric > RTAX_MAX || metric < 1)
624 if (route->rt_metrics_mask & (1 << (metric - 1))) {
625 route->rt_nmetrics--;
626 route->rt_metrics_mask &= ~(1 << (metric - 1));
632 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
634 if (metric > RTAX_MAX || metric < 1)
637 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
638 return -NLE_OBJ_NOTFOUND;
641 *value = route->rt_metrics[metric - 1];
646 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
648 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
649 if (addr->a_family != route->rt_family)
650 return -NLE_AF_MISMATCH;
652 route->rt_family = addr->a_family;
654 if (route->rt_pref_src)
655 nl_addr_put(route->rt_pref_src);
658 route->rt_pref_src = addr;
659 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
664 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
666 return route->rt_pref_src;
669 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
671 route->rt_iif = ifindex;
672 route->ce_mask |= ROUTE_ATTR_IIF;
675 int rtnl_route_get_iif(
struct rtnl_route *route)
677 return route->rt_iif;
680 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
682 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
684 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
687 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
689 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
691 nl_list_del(&nh->rtnh_list);
695 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
697 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
698 return &route->rt_nexthops;
703 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
705 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
706 return route->rt_nr_nh;
711 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
712 void (*cb)(
struct rtnl_nexthop *,
void *),
715 struct rtnl_nexthop *nh;
717 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
718 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
724 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
726 struct rtnl_nexthop *nh;
729 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
731 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
732 if (i == n)
return nh;
761 if (route->rt_type == RTN_LOCAL)
762 return RT_SCOPE_HOST;
764 if (!nl_list_empty(&route->rt_nexthops)) {
765 struct rtnl_nexthop *nh;
771 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
772 if (nh->rtnh_gateway)
773 return RT_SCOPE_UNIVERSE;
777 return RT_SCOPE_LINK;
782 static struct nla_policy route_policy[RTA_MAX+1] = {
784 [RTA_OIF] = { .type =
NLA_U32 },
785 [RTA_PRIORITY] = { .type =
NLA_U32 },
786 [RTA_FLOW] = { .type =
NLA_U32 },
787 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
792 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
794 struct rtnl_nexthop *nh = NULL;
795 struct rtnexthop *rtnh =
nla_data(attr);
799 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
800 nh = rtnl_route_nh_alloc();
804 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
805 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
806 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
808 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
809 struct nlattr *ntb[RTA_MAX + 1];
811 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
813 rtnh->rtnh_len -
sizeof(*rtnh),
818 if (ntb[RTA_GATEWAY]) {
819 struct nl_addr *addr;
828 rtnl_route_nh_set_gateway(nh, addr);
836 rtnl_route_nh_set_realms(nh, realms);
840 rtnl_route_add_nexthop(route, nh);
841 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
842 rtnh = RTNH_NEXT(rtnh);
848 rtnl_route_nh_free(nh);
853 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
856 struct rtnl_route *route;
857 struct nlattr *tb[RTA_MAX + 1];
858 struct nl_addr *src = NULL, *dst = NULL, *addr;
859 struct rtnl_nexthop *old_nh = NULL;
862 route = rtnl_route_alloc();
868 route->ce_msgtype = nlh->nlmsg_type;
870 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
875 route->rt_family = family = rtm->rtm_family;
876 route->rt_tos = rtm->rtm_tos;
877 route->rt_table = rtm->rtm_table;
878 route->rt_type = rtm->rtm_type;
879 route->rt_scope = rtm->rtm_scope;
880 route->rt_protocol = rtm->rtm_protocol;
881 route->rt_flags = rtm->rtm_flags;
883 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
884 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
885 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
894 nl_addr_set_family(dst, rtm->rtm_family);
897 nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
898 err = rtnl_route_set_dst(route, dst);
907 }
else if (rtm->rtm_src_len)
912 nl_addr_set_prefixlen(src, rtm->rtm_src_len);
913 rtnl_route_set_src(route, src);
918 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
920 if (tb[RTA_PRIORITY])
921 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
923 if (tb[RTA_PREFSRC]) {
926 rtnl_route_set_pref_src(route, addr);
930 if (tb[RTA_METRICS]) {
931 struct nlattr *mtb[RTAX_MAX + 1];
938 for (i = 1; i <= RTAX_MAX; i++) {
939 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
941 if (rtnl_route_set_metric(route, i, m) < 0)
947 if (tb[RTA_MULTIPATH])
948 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
951 if (tb[RTA_CACHEINFO]) {
952 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
953 sizeof(route->rt_cacheinfo));
954 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
958 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
961 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
964 if (tb[RTA_GATEWAY]) {
965 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
971 rtnl_route_nh_set_gateway(old_nh, addr);
976 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
979 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
983 if (route->rt_nr_nh == 0) {
987 rtnl_route_add_nexthop(route, old_nh);
991 struct rtnl_nexthop *first;
993 first = nl_list_first_entry(&route->rt_nexthops,
999 if (rtnl_route_nh_compare(old_nh, first,
1000 old_nh->ce_mask, 0)) {
1005 rtnl_route_nh_free(old_nh);
1013 rtnl_route_put(route);
1021 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1024 struct nlattr *metrics;
1025 struct rtmsg rtmsg = {
1026 .rtm_family = route->rt_family,
1027 .rtm_tos = route->rt_tos,
1028 .rtm_table = route->rt_table,
1029 .rtm_protocol = route->rt_protocol,
1030 .rtm_scope = route->rt_scope,
1031 .rtm_type = route->rt_type,
1032 .rtm_flags = route->rt_flags,
1035 if (route->rt_dst == NULL)
1036 return -NLE_MISSING_ATTR;
1043 if (rtmsg.rtm_scope == RT_SCOPE_NOWHERE)
1046 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1047 goto nla_put_failure;
1057 if (route->ce_mask & ROUTE_ATTR_SRC)
1060 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1063 if (route->ce_mask & ROUTE_ATTR_IIF)
1066 if (route->rt_nmetrics > 0) {
1070 if (metrics == NULL)
1071 goto nla_put_failure;
1073 for (i = 1; i <= RTAX_MAX; i++) {
1074 if (!rtnl_route_get_metric(route, i, &val))
1081 if (rtnl_route_get_nnexthops(route) == 1) {
1082 struct rtnl_nexthop *nh;
1084 nh = rtnl_route_nexthop_n(route, 0);
1085 if (nh->rtnh_gateway)
1087 if (nh->rtnh_ifindex)
1089 if (nh->rtnh_realms)
1091 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1092 struct nlattr *multipath;
1093 struct rtnl_nexthop *nh;
1096 goto nla_put_failure;
1098 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1099 struct rtnexthop *rtnh;
1103 goto nla_put_failure;
1105 rtnh->rtnh_flags = nh->rtnh_flags;
1106 rtnh->rtnh_hops = nh->rtnh_weight;
1107 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1109 if (nh->rtnh_gateway)
1113 if (nh->rtnh_realms)
1116 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1126 return -NLE_MSGSIZE;
1132 .oo_size =
sizeof(
struct rtnl_route),
1133 .oo_constructor = route_constructor,
1134 .oo_free_data = route_free_data,
1135 .oo_clone = route_clone,
1141 .oo_compare = route_compare,
1142 .oo_attrs2str = route_attrs2str,
1143 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1144 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),