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)
116 nl_init_list_head(&dst->rt_nexthops);
117 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
118 new = rtnl_route_nh_clone(nh);
122 rtnl_route_add_nexthop(dst,
new);
130 struct rtnl_route *r = (
struct rtnl_route *) a;
131 int cache = 0, flags;
134 if (r->rt_flags & RTM_F_CLONED)
137 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
142 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
148 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
150 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
152 if (r->ce_mask & ROUTE_ATTR_TYPE)
154 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
156 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
157 nl_dump(p,
"tos %#x ", r->rt_tos);
159 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
160 struct rtnl_nexthop *nh;
162 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
163 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
164 rtnl_route_nh_dump(nh, p);
168 flags = r->rt_flags & ~(RTM_F_CLONED);
169 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
173 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
174 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
177 PRINT_FLAG(PERVASIVE);
180 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
181 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
183 PRINT_FLAG(EQUALIZE);
187 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
188 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
190 PRINT_FLAG(REDIRECTED);
191 PRINT_FLAG(DOREDIRECT);
192 PRINT_FLAG(DIRECTSRC);
194 PRINT_FLAG(BROADCAST);
195 PRINT_FLAG(MULTICAST);
207 struct rtnl_route *r = (
struct rtnl_route *) a;
208 struct nl_cache *link_cache;
214 route_dump_line(a, p);
215 nl_dump_line(p,
" ");
217 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
218 nl_dump(p,
"preferred-src %s ",
221 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
223 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
225 if (r->ce_mask & ROUTE_ATTR_PRIO)
226 nl_dump(p,
"priority %#x ", r->rt_prio);
228 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
230 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
232 if (r->ce_mask & ROUTE_ATTR_IIF) {
238 nl_dump(p,
"iif %d ", r->rt_iif);
241 if (r->ce_mask & ROUTE_ATTR_SRC)
246 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
247 struct rtnl_nexthop *nh;
249 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
250 nl_dump_line(p,
" ");
251 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
252 rtnl_route_nh_dump(nh, p);
257 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
258 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
259 r->rt_cacheinfo.rtci_error,
260 strerror(-r->rt_cacheinfo.rtci_error));
263 if (r->ce_mask & ROUTE_ATTR_METRICS) {
264 nl_dump_line(p,
" metrics [");
265 for (i = 0; i < RTAX_MAX; i++)
266 if (r->rt_metrics_mask & (1 << i))
268 rtnl_route_metric2str(i+1,
277 struct rtnl_route *route = (
struct rtnl_route *) obj;
279 route_dump_details(obj, p);
281 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
284 nl_dump_line(p,
" used %u refcnt %u last-use %us "
286 ci->rtci_used, ci->rtci_clntref,
293 uint32_t attrs,
int flags)
295 struct rtnl_route *a = (
struct rtnl_route *) _a;
296 struct rtnl_route *b = (
struct rtnl_route *) _b;
297 struct rtnl_nexthop *nh_a, *nh_b;
298 int i, diff = 0, found;
300 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
302 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
303 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
304 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
305 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
306 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
307 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
308 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
309 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
310 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
311 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
312 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
315 if (flags & LOOSE_COMPARISON) {
316 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
318 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
320 if (!rtnl_route_nh_compare(nh_a, nh_b,
331 for (i = 0; i < RTAX_MAX - 1; i++) {
332 if (a->rt_metrics_mask & (1 << i) &&
333 (!(b->rt_metrics_mask & (1 << i)) ||
334 a->rt_metrics[i] != b->rt_metrics[i]))
335 ROUTE_DIFF(METRICS, 1);
338 diff |= ROUTE_DIFF(FLAGS,
339 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
341 if (a->rt_nr_nh != b->rt_nr_nh)
345 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
347 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
349 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
360 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
362 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
364 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
373 for (i = 0; i < RTAX_MAX - 1; i++) {
374 if ((a->rt_metrics_mask & (1 << i)) ^
375 (b->rt_metrics_mask & (1 << i)))
376 diff |= ROUTE_DIFF(METRICS, 1);
378 diff |= ROUTE_DIFF(METRICS,
379 a->rt_metrics[i] != b->rt_metrics[i]);
382 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
389 diff |= ROUTE_DIFF(MULTIPATH, 1);
395 static const struct trans_tbl route_attrs[] = {
396 __ADD(ROUTE_ATTR_FAMILY, family)
397 __ADD(ROUTE_ATTR_TOS, tos)
398 __ADD(ROUTE_ATTR_TABLE, table)
399 __ADD(ROUTE_ATTR_PROTOCOL, protocol)
400 __ADD(ROUTE_ATTR_SCOPE, scope)
401 __ADD(ROUTE_ATTR_TYPE, type)
402 __ADD(ROUTE_ATTR_FLAGS, flags)
403 __ADD(ROUTE_ATTR_DST, dst)
404 __ADD(ROUTE_ATTR_SRC, src)
405 __ADD(ROUTE_ATTR_IIF, iif)
406 __ADD(ROUTE_ATTR_OIF, oif)
407 __ADD(ROUTE_ATTR_GATEWAY, gateway)
408 __ADD(ROUTE_ATTR_PRIO, prio)
409 __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
410 __ADD(ROUTE_ATTR_METRICS, metrics)
411 __ADD(ROUTE_ATTR_MULTIPATH, multipath)
412 __ADD(ROUTE_ATTR_REALMS, realms)
413 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
416 static
char *route_attrs2str(
int attrs,
char *buf,
size_t len)
418 return __flags2str(attrs, buf, len, route_attrs,
419 ARRAY_SIZE(route_attrs));
427 struct rtnl_route *rtnl_route_alloc(
void)
432 void rtnl_route_get(
struct rtnl_route *route)
437 void rtnl_route_put(
struct rtnl_route *route)
449 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
451 route->rt_table = table;
452 route->ce_mask |= ROUTE_ATTR_TABLE;
455 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
457 return route->rt_table;
460 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
462 route->rt_scope = scope;
463 route->ce_mask |= ROUTE_ATTR_SCOPE;
466 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
468 return route->rt_scope;
471 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
474 route->ce_mask |= ROUTE_ATTR_TOS;
477 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
479 return route->rt_tos;
482 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
484 route->rt_protocol = protocol;
485 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
488 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
490 return route->rt_protocol;
493 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
495 route->rt_prio = prio;
496 route->ce_mask |= ROUTE_ATTR_PRIO;
499 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
501 return route->rt_prio;
504 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
506 if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
507 return -NLE_AF_NOSUPPORT;
509 route->rt_family = family;
510 route->ce_mask |= ROUTE_ATTR_FAMILY;
515 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
517 return route->rt_family;
520 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
522 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
523 if (addr->a_family != route->rt_family)
524 return -NLE_AF_MISMATCH;
526 route->rt_family = addr->a_family;
529 nl_addr_put(route->rt_dst);
532 route->rt_dst = addr;
534 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
539 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
541 return route->rt_dst;
544 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
546 if (addr->a_family == AF_INET)
547 return -NLE_SRCRT_NOSUPPORT;
549 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
550 if (addr->a_family != route->rt_family)
551 return -NLE_AF_MISMATCH;
553 route->rt_family = addr->a_family;
556 nl_addr_put(route->rt_src);
559 route->rt_src = addr;
560 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
565 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
567 return route->rt_src;
570 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
575 route->rt_type = type;
576 route->ce_mask |= ROUTE_ATTR_TYPE;
581 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
583 return route->rt_type;
586 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
588 route->rt_flag_mask |= flags;
589 route->rt_flags |= flags;
590 route->ce_mask |= ROUTE_ATTR_FLAGS;
593 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
595 route->rt_flag_mask |= flags;
596 route->rt_flags &= ~flags;
597 route->ce_mask |= ROUTE_ATTR_FLAGS;
600 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
602 return route->rt_flags;
605 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
607 if (metric > RTAX_MAX || metric < 1)
610 route->rt_metrics[metric - 1] = value;
612 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
613 route->rt_nmetrics++;
614 route->rt_metrics_mask |= (1 << (metric - 1));
617 route->ce_mask |= ROUTE_ATTR_METRICS;
622 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
624 if (metric > RTAX_MAX || metric < 1)
627 if (route->rt_metrics_mask & (1 << (metric - 1))) {
628 route->rt_nmetrics--;
629 route->rt_metrics_mask &= ~(1 << (metric - 1));
635 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
637 if (metric > RTAX_MAX || metric < 1)
640 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
641 return -NLE_OBJ_NOTFOUND;
644 *value = route->rt_metrics[metric - 1];
649 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
651 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
652 if (addr->a_family != route->rt_family)
653 return -NLE_AF_MISMATCH;
655 route->rt_family = addr->a_family;
657 if (route->rt_pref_src)
658 nl_addr_put(route->rt_pref_src);
661 route->rt_pref_src = addr;
662 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
667 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
669 return route->rt_pref_src;
672 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
674 route->rt_iif = ifindex;
675 route->ce_mask |= ROUTE_ATTR_IIF;
678 int rtnl_route_get_iif(
struct rtnl_route *route)
680 return route->rt_iif;
683 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
685 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
687 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
690 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
692 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
694 nl_list_del(&nh->rtnh_list);
698 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
700 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
701 return &route->rt_nexthops;
706 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
708 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
709 return route->rt_nr_nh;
714 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
715 void (*cb)(
struct rtnl_nexthop *,
void *),
718 struct rtnl_nexthop *nh;
720 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
721 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
727 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
729 struct rtnl_nexthop *nh;
732 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
734 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
735 if (i == n)
return nh;
764 if (route->rt_type == RTN_LOCAL)
765 return RT_SCOPE_HOST;
767 if (!nl_list_empty(&route->rt_nexthops)) {
768 struct rtnl_nexthop *nh;
774 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
775 if (nh->rtnh_gateway)
776 return RT_SCOPE_UNIVERSE;
780 return RT_SCOPE_LINK;
785 static struct nla_policy route_policy[RTA_MAX+1] = {
787 [RTA_OIF] = { .type =
NLA_U32 },
788 [RTA_PRIORITY] = { .type =
NLA_U32 },
789 [RTA_FLOW] = { .type =
NLA_U32 },
790 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
795 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
797 struct rtnl_nexthop *nh = NULL;
798 struct rtnexthop *rtnh =
nla_data(attr);
802 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
803 nh = rtnl_route_nh_alloc();
807 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
808 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
809 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
811 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
812 struct nlattr *ntb[RTA_MAX + 1];
814 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
816 rtnh->rtnh_len -
sizeof(*rtnh),
821 if (ntb[RTA_GATEWAY]) {
822 struct nl_addr *addr;
831 rtnl_route_nh_set_gateway(nh, addr);
839 rtnl_route_nh_set_realms(nh, realms);
843 rtnl_route_add_nexthop(route, nh);
844 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
845 rtnh = RTNH_NEXT(rtnh);
851 rtnl_route_nh_free(nh);
856 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
859 struct rtnl_route *route;
860 struct nlattr *tb[RTA_MAX + 1];
861 struct nl_addr *src = NULL, *dst = NULL, *addr;
862 struct rtnl_nexthop *old_nh = NULL;
865 route = rtnl_route_alloc();
871 route->ce_msgtype = nlh->nlmsg_type;
873 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
878 route->rt_family = family = rtm->rtm_family;
879 route->rt_tos = rtm->rtm_tos;
880 route->rt_table = rtm->rtm_table;
881 route->rt_type = rtm->rtm_type;
882 route->rt_scope = rtm->rtm_scope;
883 route->rt_protocol = rtm->rtm_protocol;
884 route->rt_flags = rtm->rtm_flags;
886 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
887 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
888 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
897 nl_addr_set_family(dst, rtm->rtm_family);
900 nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
901 err = rtnl_route_set_dst(route, dst);
910 }
else if (rtm->rtm_src_len)
915 nl_addr_set_prefixlen(src, rtm->rtm_src_len);
916 rtnl_route_set_src(route, src);
921 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
924 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
926 if (tb[RTA_PRIORITY])
927 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
929 if (tb[RTA_PREFSRC]) {
932 rtnl_route_set_pref_src(route, addr);
936 if (tb[RTA_METRICS]) {
937 struct nlattr *mtb[RTAX_MAX + 1];
944 for (i = 1; i <= RTAX_MAX; i++) {
945 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
947 if (rtnl_route_set_metric(route, i, m) < 0)
953 if (tb[RTA_MULTIPATH])
954 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
957 if (tb[RTA_CACHEINFO]) {
958 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
959 sizeof(route->rt_cacheinfo));
960 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
964 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
967 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
970 if (tb[RTA_GATEWAY]) {
971 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
977 rtnl_route_nh_set_gateway(old_nh, addr);
982 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
985 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
989 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
990 if (route->rt_nr_nh == 0) {
994 rtnl_route_add_nexthop(route, old_nh);
998 struct rtnl_nexthop *first;
1000 first = nl_list_first_entry(&route->rt_nexthops,
1001 struct rtnl_nexthop,
1006 if (rtnl_route_nh_compare(old_nh, first,
1007 old_nh->ce_mask, 0)) {
1012 rtnl_route_nh_free(old_nh);
1020 rtnl_route_put(route);
1028 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1031 struct nlattr *metrics;
1032 struct rtmsg rtmsg = {
1033 .rtm_family = route->rt_family,
1034 .rtm_tos = route->rt_tos,
1035 .rtm_table = route->rt_table,
1036 .rtm_protocol = route->rt_protocol,
1037 .rtm_scope = route->rt_scope,
1038 .rtm_type = route->rt_type,
1039 .rtm_flags = route->rt_flags,
1042 if (route->rt_dst == NULL)
1043 return -NLE_MISSING_ATTR;
1049 if (rtmsg.rtm_scope == RT_SCOPE_NOWHERE)
1052 if (rtnl_route_get_nnexthops(route) == 1) {
1053 struct rtnl_nexthop *nh;
1054 nh = rtnl_route_nexthop_n(route, 0);
1055 rtmsg.rtm_flags |= nh->rtnh_flags;
1058 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1059 goto nla_put_failure;
1069 if (route->ce_mask & ROUTE_ATTR_SRC)
1072 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1075 if (route->ce_mask & ROUTE_ATTR_IIF)
1078 if (route->rt_nmetrics > 0) {
1082 if (metrics == NULL)
1083 goto nla_put_failure;
1085 for (i = 1; i <= RTAX_MAX; i++) {
1086 if (!rtnl_route_get_metric(route, i, &val))
1093 if (rtnl_route_get_nnexthops(route) == 1) {
1094 struct rtnl_nexthop *nh;
1096 nh = rtnl_route_nexthop_n(route, 0);
1097 if (nh->rtnh_gateway)
1099 if (nh->rtnh_ifindex)
1101 if (nh->rtnh_realms)
1103 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1104 struct nlattr *multipath;
1105 struct rtnl_nexthop *nh;
1108 goto nla_put_failure;
1110 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1111 struct rtnexthop *rtnh;
1115 goto nla_put_failure;
1117 rtnh->rtnh_flags = nh->rtnh_flags;
1118 rtnh->rtnh_hops = nh->rtnh_weight;
1119 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1121 if (nh->rtnh_gateway)
1125 if (nh->rtnh_realms)
1128 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1138 return -NLE_MSGSIZE;
1144 .oo_size =
sizeof(
struct rtnl_route),
1145 .oo_constructor = route_constructor,
1146 .oo_free_data = route_free_data,
1147 .oo_clone = route_clone,
1153 .oo_compare = route_compare,
1154 .oo_attrs2str = route_attrs2str,
1155 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1156 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),