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/hashtable.h>
39 #include <netlink/route/rtnl.h>
40 #include <netlink/route/route.h>
41 #include <netlink/route/link.h>
42 #include <netlink/route/nexthop.h>
45 #define ROUTE_ATTR_FAMILY 0x000001
46 #define ROUTE_ATTR_TOS 0x000002
47 #define ROUTE_ATTR_TABLE 0x000004
48 #define ROUTE_ATTR_PROTOCOL 0x000008
49 #define ROUTE_ATTR_SCOPE 0x000010
50 #define ROUTE_ATTR_TYPE 0x000020
51 #define ROUTE_ATTR_FLAGS 0x000040
52 #define ROUTE_ATTR_DST 0x000080
53 #define ROUTE_ATTR_SRC 0x000100
54 #define ROUTE_ATTR_IIF 0x000200
55 #define ROUTE_ATTR_OIF 0x000400
56 #define ROUTE_ATTR_GATEWAY 0x000800
57 #define ROUTE_ATTR_PRIO 0x001000
58 #define ROUTE_ATTR_PREF_SRC 0x002000
59 #define ROUTE_ATTR_METRICS 0x004000
60 #define ROUTE_ATTR_MULTIPATH 0x008000
61 #define ROUTE_ATTR_REALMS 0x010000
62 #define ROUTE_ATTR_CACHEINFO 0x020000
65 static void route_constructor(
struct nl_object *c)
67 struct rtnl_route *r = (
struct rtnl_route *) c;
69 r->rt_family = AF_UNSPEC;
70 r->rt_scope = RT_SCOPE_NOWHERE;
71 r->rt_table = RT_TABLE_MAIN;
72 r->rt_protocol = RTPROT_STATIC;
73 r->rt_type = RTN_UNICAST;
75 nl_init_list_head(&r->rt_nexthops);
78 static void route_free_data(
struct nl_object *c)
80 struct rtnl_route *r = (
struct rtnl_route *) c;
81 struct rtnl_nexthop *nh, *tmp;
86 nl_addr_put(r->rt_dst);
87 nl_addr_put(r->rt_src);
88 nl_addr_put(r->rt_pref_src);
90 nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
91 rtnl_route_remove_nexthop(r, nh);
92 rtnl_route_nh_free(nh);
98 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
99 struct rtnl_route *src = (
struct rtnl_route *) _src;
100 struct rtnl_nexthop *nh, *
new;
110 if (src->rt_pref_src)
117 nl_init_list_head(&dst->rt_nexthops);
118 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
119 new = rtnl_route_nh_clone(nh);
123 rtnl_route_add_nexthop(dst,
new);
131 struct rtnl_route *r = (
struct rtnl_route *) a;
132 int cache = 0, flags;
135 if (r->rt_flags & RTM_F_CLONED)
138 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
143 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
149 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
151 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
153 if (r->ce_mask & ROUTE_ATTR_TYPE)
155 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
157 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
158 nl_dump(p,
"tos %#x ", r->rt_tos);
160 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
161 struct rtnl_nexthop *nh;
163 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
164 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
165 rtnl_route_nh_dump(nh, p);
169 flags = r->rt_flags & ~(RTM_F_CLONED);
170 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
174 #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
175 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
178 PRINT_FLAG(PERVASIVE);
181 #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
182 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
184 PRINT_FLAG(EQUALIZE);
188 #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
189 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
191 PRINT_FLAG(REDIRECTED);
192 PRINT_FLAG(DOREDIRECT);
193 PRINT_FLAG(DIRECTSRC);
195 PRINT_FLAG(BROADCAST);
196 PRINT_FLAG(MULTICAST);
208 struct rtnl_route *r = (
struct rtnl_route *) a;
209 struct nl_cache *link_cache;
215 route_dump_line(a, p);
216 nl_dump_line(p,
" ");
218 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
219 nl_dump(p,
"preferred-src %s ",
222 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
224 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
226 if (r->ce_mask & ROUTE_ATTR_PRIO)
227 nl_dump(p,
"priority %#x ", r->rt_prio);
229 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
231 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
233 if (r->ce_mask & ROUTE_ATTR_IIF) {
239 nl_dump(p,
"iif %d ", r->rt_iif);
242 if (r->ce_mask & ROUTE_ATTR_SRC)
247 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
248 struct rtnl_nexthop *nh;
250 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
251 nl_dump_line(p,
" ");
252 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
253 rtnl_route_nh_dump(nh, p);
258 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
259 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
260 r->rt_cacheinfo.rtci_error,
261 strerror(-r->rt_cacheinfo.rtci_error));
264 if (r->ce_mask & ROUTE_ATTR_METRICS) {
265 nl_dump_line(p,
" metrics [");
266 for (i = 0; i < RTAX_MAX; i++)
267 if (r->rt_metrics_mask & (1 << i))
269 rtnl_route_metric2str(i+1,
276 nl_cache_put(link_cache);
281 struct rtnl_route *route = (
struct rtnl_route *) obj;
283 route_dump_details(obj, p);
285 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
288 nl_dump_line(p,
" used %u refcnt %u last-use %us "
290 ci->rtci_used, ci->rtci_clntref,
296 static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
299 struct rtnl_route *route = (
struct rtnl_route *) obj;
300 unsigned int rkey_sz;
301 struct nl_addr *addr = NULL;
302 struct route_hash_key {
307 } __attribute__((packed)) *rkey;
308 char buf[INET6_ADDRSTRLEN+5];
311 addr = route->rt_dst;
313 rkey_sz =
sizeof(*rkey);
316 rkey = calloc(1, rkey_sz);
318 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
322 rkey->rt_family = route->rt_family;
323 rkey->rt_tos = route->rt_tos;
324 rkey->rt_table = route->rt_table;
329 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
331 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d "
332 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
333 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
342 uint32_t attrs,
int flags)
344 struct rtnl_route *a = (
struct rtnl_route *) _a;
345 struct rtnl_route *b = (
struct rtnl_route *) _b;
346 struct rtnl_nexthop *nh_a, *nh_b;
347 int i, diff = 0, found;
349 #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
351 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
352 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
353 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
354 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
355 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
356 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
357 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
358 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
359 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
360 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
361 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
364 if (flags & LOOSE_COMPARISON) {
365 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
367 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
369 if (!rtnl_route_nh_compare(nh_a, nh_b,
380 for (i = 0; i < RTAX_MAX - 1; i++) {
381 if (a->rt_metrics_mask & (1 << i) &&
382 (!(b->rt_metrics_mask & (1 << i)) ||
383 a->rt_metrics[i] != b->rt_metrics[i]))
384 diff |= ROUTE_DIFF(METRICS, 1);
387 diff |= ROUTE_DIFF(FLAGS,
388 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
390 if (a->rt_nr_nh != b->rt_nr_nh)
394 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
396 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
398 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
409 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
411 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
413 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
422 for (i = 0; i < RTAX_MAX - 1; i++) {
423 if ((a->rt_metrics_mask & (1 << i)) ^
424 (b->rt_metrics_mask & (1 << i)))
425 diff |= ROUTE_DIFF(METRICS, 1);
427 diff |= ROUTE_DIFF(METRICS,
428 a->rt_metrics[i] != b->rt_metrics[i]);
431 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
438 diff |= ROUTE_DIFF(MULTIPATH, 1);
444 static const struct trans_tbl route_attrs[] = {
445 __ADD(ROUTE_ATTR_FAMILY, family)
446 __ADD(ROUTE_ATTR_TOS, tos)
447 __ADD(ROUTE_ATTR_TABLE, table)
448 __ADD(ROUTE_ATTR_PROTOCOL, protocol)
449 __ADD(ROUTE_ATTR_SCOPE, scope)
450 __ADD(ROUTE_ATTR_TYPE, type)
451 __ADD(ROUTE_ATTR_FLAGS, flags)
452 __ADD(ROUTE_ATTR_DST, dst)
453 __ADD(ROUTE_ATTR_SRC, src)
454 __ADD(ROUTE_ATTR_IIF, iif)
455 __ADD(ROUTE_ATTR_OIF, oif)
456 __ADD(ROUTE_ATTR_GATEWAY, gateway)
457 __ADD(ROUTE_ATTR_PRIO, prio)
458 __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
459 __ADD(ROUTE_ATTR_METRICS, metrics)
460 __ADD(ROUTE_ATTR_MULTIPATH, multipath)
461 __ADD(ROUTE_ATTR_REALMS, realms)
462 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
465 static
char *route_attrs2str(
int attrs,
char *buf,
size_t len)
467 return __flags2str(attrs, buf, len, route_attrs,
468 ARRAY_SIZE(route_attrs));
476 struct rtnl_route *rtnl_route_alloc(
void)
481 void rtnl_route_get(
struct rtnl_route *route)
486 void rtnl_route_put(
struct rtnl_route *route)
498 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
500 route->rt_table = table;
501 route->ce_mask |= ROUTE_ATTR_TABLE;
504 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
506 return route->rt_table;
509 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
511 route->rt_scope = scope;
512 route->ce_mask |= ROUTE_ATTR_SCOPE;
515 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
517 return route->rt_scope;
520 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
523 route->ce_mask |= ROUTE_ATTR_TOS;
526 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
528 return route->rt_tos;
531 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
533 route->rt_protocol = protocol;
534 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
537 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
539 return route->rt_protocol;
542 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
544 route->rt_prio = prio;
545 route->ce_mask |= ROUTE_ATTR_PRIO;
548 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
550 return route->rt_prio;
553 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
555 if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
556 return -NLE_AF_NOSUPPORT;
558 route->rt_family = family;
559 route->ce_mask |= ROUTE_ATTR_FAMILY;
564 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
566 return route->rt_family;
569 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
571 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
572 if (addr->a_family != route->rt_family)
573 return -NLE_AF_MISMATCH;
575 route->rt_family = addr->a_family;
578 nl_addr_put(route->rt_dst);
581 route->rt_dst = addr;
583 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
588 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
590 return route->rt_dst;
593 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
595 if (addr->a_family == AF_INET)
596 return -NLE_SRCRT_NOSUPPORT;
598 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
599 if (addr->a_family != route->rt_family)
600 return -NLE_AF_MISMATCH;
602 route->rt_family = addr->a_family;
605 nl_addr_put(route->rt_src);
608 route->rt_src = addr;
609 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
614 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
616 return route->rt_src;
619 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
624 route->rt_type = type;
625 route->ce_mask |= ROUTE_ATTR_TYPE;
630 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
632 return route->rt_type;
635 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
637 route->rt_flag_mask |= flags;
638 route->rt_flags |= flags;
639 route->ce_mask |= ROUTE_ATTR_FLAGS;
642 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
644 route->rt_flag_mask |= flags;
645 route->rt_flags &= ~flags;
646 route->ce_mask |= ROUTE_ATTR_FLAGS;
649 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
651 return route->rt_flags;
654 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
656 if (metric > RTAX_MAX || metric < 1)
659 route->rt_metrics[metric - 1] = value;
661 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
662 route->rt_nmetrics++;
663 route->rt_metrics_mask |= (1 << (metric - 1));
666 route->ce_mask |= ROUTE_ATTR_METRICS;
671 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
673 if (metric > RTAX_MAX || metric < 1)
676 if (route->rt_metrics_mask & (1 << (metric - 1))) {
677 route->rt_nmetrics--;
678 route->rt_metrics_mask &= ~(1 << (metric - 1));
684 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
686 if (metric > RTAX_MAX || metric < 1)
689 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
690 return -NLE_OBJ_NOTFOUND;
693 *value = route->rt_metrics[metric - 1];
698 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
700 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
701 if (addr->a_family != route->rt_family)
702 return -NLE_AF_MISMATCH;
704 route->rt_family = addr->a_family;
706 if (route->rt_pref_src)
707 nl_addr_put(route->rt_pref_src);
710 route->rt_pref_src = addr;
711 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
716 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
718 return route->rt_pref_src;
721 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
723 route->rt_iif = ifindex;
724 route->ce_mask |= ROUTE_ATTR_IIF;
727 int rtnl_route_get_iif(
struct rtnl_route *route)
729 return route->rt_iif;
732 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
734 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
736 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
739 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
741 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
743 nl_list_del(&nh->rtnh_list);
747 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
749 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
750 return &route->rt_nexthops;
755 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
757 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
758 return route->rt_nr_nh;
763 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
764 void (*cb)(
struct rtnl_nexthop *,
void *),
767 struct rtnl_nexthop *nh;
769 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
770 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
776 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
778 struct rtnl_nexthop *nh;
781 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
783 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
784 if (i == n)
return nh;
813 if (route->rt_type == RTN_LOCAL)
814 return RT_SCOPE_HOST;
816 if (!nl_list_empty(&route->rt_nexthops)) {
817 struct rtnl_nexthop *nh;
823 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
824 if (nh->rtnh_gateway)
825 return RT_SCOPE_UNIVERSE;
829 return RT_SCOPE_LINK;
834 static struct nla_policy route_policy[RTA_MAX+1] = {
836 [RTA_OIF] = { .type =
NLA_U32 },
837 [RTA_PRIORITY] = { .type =
NLA_U32 },
838 [RTA_FLOW] = { .type =
NLA_U32 },
839 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
844 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
846 struct rtnl_nexthop *nh = NULL;
847 struct rtnexthop *rtnh =
nla_data(attr);
851 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
852 nh = rtnl_route_nh_alloc();
856 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
857 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
858 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
860 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
861 struct nlattr *ntb[RTA_MAX + 1];
863 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
865 rtnh->rtnh_len -
sizeof(*rtnh),
870 if (ntb[RTA_GATEWAY]) {
871 struct nl_addr *addr;
880 rtnl_route_nh_set_gateway(nh, addr);
888 rtnl_route_nh_set_realms(nh, realms);
892 rtnl_route_add_nexthop(route, nh);
893 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
894 rtnh = RTNH_NEXT(rtnh);
900 rtnl_route_nh_free(nh);
905 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
908 struct rtnl_route *route;
909 struct nlattr *tb[RTA_MAX + 1];
910 struct nl_addr *src = NULL, *dst = NULL, *addr;
911 struct rtnl_nexthop *old_nh = NULL;
914 route = rtnl_route_alloc();
920 route->ce_msgtype = nlh->nlmsg_type;
922 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
927 route->rt_family = family = rtm->rtm_family;
928 route->rt_tos = rtm->rtm_tos;
929 route->rt_table = rtm->rtm_table;
930 route->rt_type = rtm->rtm_type;
931 route->rt_scope = rtm->rtm_scope;
932 route->rt_protocol = rtm->rtm_protocol;
933 route->rt_flags = rtm->rtm_flags;
935 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
936 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
937 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
946 nl_addr_set_family(dst, rtm->rtm_family);
949 nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
950 err = rtnl_route_set_dst(route, dst);
959 }
else if (rtm->rtm_src_len)
964 nl_addr_set_prefixlen(src, rtm->rtm_src_len);
965 rtnl_route_set_src(route, src);
970 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
973 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
975 if (tb[RTA_PRIORITY])
976 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
978 if (tb[RTA_PREFSRC]) {
981 rtnl_route_set_pref_src(route, addr);
985 if (tb[RTA_METRICS]) {
986 struct nlattr *mtb[RTAX_MAX + 1];
993 for (i = 1; i <= RTAX_MAX; i++) {
994 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
996 if (rtnl_route_set_metric(route, i, m) < 0)
1002 if (tb[RTA_MULTIPATH])
1003 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1006 if (tb[RTA_CACHEINFO]) {
1007 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1008 sizeof(route->rt_cacheinfo));
1009 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1013 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1016 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1019 if (tb[RTA_GATEWAY]) {
1020 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1026 rtnl_route_nh_set_gateway(old_nh, addr);
1031 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1034 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1038 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1039 if (route->rt_nr_nh == 0) {
1043 rtnl_route_add_nexthop(route, old_nh);
1047 struct rtnl_nexthop *first;
1049 first = nl_list_first_entry(&route->rt_nexthops,
1050 struct rtnl_nexthop,
1055 if (rtnl_route_nh_compare(old_nh, first,
1056 old_nh->ce_mask, 0)) {
1061 rtnl_route_nh_free(old_nh);
1069 rtnl_route_put(route);
1077 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1080 struct nlattr *metrics;
1081 struct rtmsg rtmsg = {
1082 .rtm_family = route->rt_family,
1083 .rtm_tos = route->rt_tos,
1084 .rtm_table = route->rt_table,
1085 .rtm_protocol = route->rt_protocol,
1086 .rtm_scope = route->rt_scope,
1087 .rtm_type = route->rt_type,
1088 .rtm_flags = route->rt_flags,
1091 if (route->rt_dst == NULL)
1092 return -NLE_MISSING_ATTR;
1098 if (rtmsg.rtm_scope == RT_SCOPE_NOWHERE)
1101 if (rtnl_route_get_nnexthops(route) == 1) {
1102 struct rtnl_nexthop *nh;
1103 nh = rtnl_route_nexthop_n(route, 0);
1104 rtmsg.rtm_flags |= nh->rtnh_flags;
1107 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1108 goto nla_put_failure;
1118 if (route->ce_mask & ROUTE_ATTR_SRC)
1121 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1124 if (route->ce_mask & ROUTE_ATTR_IIF)
1127 if (route->rt_nmetrics > 0) {
1131 if (metrics == NULL)
1132 goto nla_put_failure;
1134 for (i = 1; i <= RTAX_MAX; i++) {
1135 if (!rtnl_route_get_metric(route, i, &val))
1142 if (rtnl_route_get_nnexthops(route) == 1) {
1143 struct rtnl_nexthop *nh;
1145 nh = rtnl_route_nexthop_n(route, 0);
1146 if (nh->rtnh_gateway)
1148 if (nh->rtnh_ifindex)
1150 if (nh->rtnh_realms)
1152 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1153 struct nlattr *multipath;
1154 struct rtnl_nexthop *nh;
1157 goto nla_put_failure;
1159 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1160 struct rtnexthop *rtnh;
1164 goto nla_put_failure;
1166 rtnh->rtnh_flags = nh->rtnh_flags;
1167 rtnh->rtnh_hops = nh->rtnh_weight;
1168 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1170 if (nh->rtnh_gateway)
1174 if (nh->rtnh_realms)
1177 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1187 return -NLE_MSGSIZE;
1193 .oo_size =
sizeof(
struct rtnl_route),
1194 .oo_constructor = route_constructor,
1195 .oo_free_data = route_free_data,
1196 .oo_clone = route_clone,
1202 .oo_compare = route_compare,
1203 .oo_keygen = route_keygen,
1204 .oo_attrs2str = route_attrs2str,
1205 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1206 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),