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);
446 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
447 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
448 struct rtnl_nexthop *new_nh;
449 char buf[INET6_ADDRSTRLEN+5];
450 int action = new_obj->ce_msgtype;
459 if (new_route->rt_family != AF_INET6 ||
460 new_route->rt_table == RT_TABLE_LOCAL)
461 return -NLE_OPNOTSUPP;
467 if (rtnl_route_get_nnexthops(new_route) != 1)
468 return -NLE_OPNOTSUPP;
475 new_nh = rtnl_route_nexthop_n(new_route, 0);
476 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
477 return -NLE_OPNOTSUPP;
480 case RTM_NEWROUTE : {
481 struct rtnl_nexthop *cloned_nh;
486 cloned_nh = rtnl_route_nh_clone(new_nh);
489 rtnl_route_add_nexthop(old_route, cloned_nh);
491 NL_DBG(2,
"Route obj %p updated. Added "
492 "nexthop %p via %s\n", old_route, cloned_nh,
497 case RTM_DELROUTE : {
498 struct rtnl_nexthop *old_nh;
506 if (rtnl_route_get_nnexthops(old_route) <= 1)
507 return -NLE_OPNOTSUPP;
512 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
514 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
516 rtnl_route_remove_nexthop(old_route, old_nh);
518 NL_DBG(2,
"Route obj %p updated. Removed "
519 "nexthop %p via %s\n", old_route,
524 rtnl_route_nh_free(old_nh);
531 NL_DBG(2,
"Unknown action associated "
532 "to object %p during route update\n", new_obj);
533 return -NLE_OPNOTSUPP;
539 static const struct trans_tbl route_attrs[] = {
540 __ADD(ROUTE_ATTR_FAMILY, family)
541 __ADD(ROUTE_ATTR_TOS, tos)
542 __ADD(ROUTE_ATTR_TABLE, table)
543 __ADD(ROUTE_ATTR_PROTOCOL, protocol)
544 __ADD(ROUTE_ATTR_SCOPE, scope)
545 __ADD(ROUTE_ATTR_TYPE, type)
546 __ADD(ROUTE_ATTR_FLAGS, flags)
547 __ADD(ROUTE_ATTR_DST, dst)
548 __ADD(ROUTE_ATTR_SRC, src)
549 __ADD(ROUTE_ATTR_IIF, iif)
550 __ADD(ROUTE_ATTR_OIF, oif)
551 __ADD(ROUTE_ATTR_GATEWAY, gateway)
552 __ADD(ROUTE_ATTR_PRIO, prio)
553 __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
554 __ADD(ROUTE_ATTR_METRICS, metrics)
555 __ADD(ROUTE_ATTR_MULTIPATH, multipath)
556 __ADD(ROUTE_ATTR_REALMS, realms)
557 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
560 static
char *route_attrs2str(
int attrs,
char *buf,
size_t len)
562 return __flags2str(attrs, buf, len, route_attrs,
563 ARRAY_SIZE(route_attrs));
571 struct rtnl_route *rtnl_route_alloc(
void)
576 void rtnl_route_get(
struct rtnl_route *route)
581 void rtnl_route_put(
struct rtnl_route *route)
593 void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
595 route->rt_table = table;
596 route->ce_mask |= ROUTE_ATTR_TABLE;
599 uint32_t rtnl_route_get_table(
struct rtnl_route *route)
601 return route->rt_table;
604 void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
606 route->rt_scope = scope;
607 route->ce_mask |= ROUTE_ATTR_SCOPE;
610 uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
612 return route->rt_scope;
615 void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
618 route->ce_mask |= ROUTE_ATTR_TOS;
621 uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
623 return route->rt_tos;
626 void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
628 route->rt_protocol = protocol;
629 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
632 uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
634 return route->rt_protocol;
637 void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
639 route->rt_prio = prio;
640 route->ce_mask |= ROUTE_ATTR_PRIO;
643 uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
645 return route->rt_prio;
648 int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
650 if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
651 return -NLE_AF_NOSUPPORT;
653 route->rt_family = family;
654 route->ce_mask |= ROUTE_ATTR_FAMILY;
659 uint8_t rtnl_route_get_family(
struct rtnl_route *route)
661 return route->rt_family;
664 int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
666 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
667 if (addr->a_family != route->rt_family)
668 return -NLE_AF_MISMATCH;
670 route->rt_family = addr->a_family;
673 nl_addr_put(route->rt_dst);
676 route->rt_dst = addr;
678 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
683 struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
685 return route->rt_dst;
688 int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
690 if (addr->a_family == AF_INET)
691 return -NLE_SRCRT_NOSUPPORT;
693 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
694 if (addr->a_family != route->rt_family)
695 return -NLE_AF_MISMATCH;
697 route->rt_family = addr->a_family;
700 nl_addr_put(route->rt_src);
703 route->rt_src = addr;
704 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
709 struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
711 return route->rt_src;
714 int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
719 route->rt_type = type;
720 route->ce_mask |= ROUTE_ATTR_TYPE;
725 uint8_t rtnl_route_get_type(
struct rtnl_route *route)
727 return route->rt_type;
730 void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
732 route->rt_flag_mask |= flags;
733 route->rt_flags |= flags;
734 route->ce_mask |= ROUTE_ATTR_FLAGS;
737 void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
739 route->rt_flag_mask |= flags;
740 route->rt_flags &= ~flags;
741 route->ce_mask |= ROUTE_ATTR_FLAGS;
744 uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
746 return route->rt_flags;
749 int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
751 if (metric > RTAX_MAX || metric < 1)
754 route->rt_metrics[metric - 1] = value;
756 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
757 route->rt_nmetrics++;
758 route->rt_metrics_mask |= (1 << (metric - 1));
761 route->ce_mask |= ROUTE_ATTR_METRICS;
766 int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
768 if (metric > RTAX_MAX || metric < 1)
771 if (route->rt_metrics_mask & (1 << (metric - 1))) {
772 route->rt_nmetrics--;
773 route->rt_metrics_mask &= ~(1 << (metric - 1));
779 int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
781 if (metric > RTAX_MAX || metric < 1)
784 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
785 return -NLE_OBJ_NOTFOUND;
788 *value = route->rt_metrics[metric - 1];
793 int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
795 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
796 if (addr->a_family != route->rt_family)
797 return -NLE_AF_MISMATCH;
799 route->rt_family = addr->a_family;
801 if (route->rt_pref_src)
802 nl_addr_put(route->rt_pref_src);
805 route->rt_pref_src = addr;
806 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
811 struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
813 return route->rt_pref_src;
816 void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
818 route->rt_iif = ifindex;
819 route->ce_mask |= ROUTE_ATTR_IIF;
822 int rtnl_route_get_iif(
struct rtnl_route *route)
824 return route->rt_iif;
827 void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
829 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
831 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
834 void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
836 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
838 nl_list_del(&nh->rtnh_list);
842 struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
844 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
845 return &route->rt_nexthops;
850 int rtnl_route_get_nnexthops(
struct rtnl_route *route)
852 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
853 return route->rt_nr_nh;
858 void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
859 void (*cb)(
struct rtnl_nexthop *,
void *),
862 struct rtnl_nexthop *nh;
864 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
865 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
871 struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
873 struct rtnl_nexthop *nh;
876 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
878 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
879 if (i == n)
return nh;
908 if (route->rt_type == RTN_LOCAL)
909 return RT_SCOPE_HOST;
911 if (!nl_list_empty(&route->rt_nexthops)) {
912 struct rtnl_nexthop *nh;
918 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
919 if (nh->rtnh_gateway)
920 return RT_SCOPE_UNIVERSE;
924 return RT_SCOPE_LINK;
929 static struct nla_policy route_policy[RTA_MAX+1] = {
931 [RTA_OIF] = { .type =
NLA_U32 },
932 [RTA_PRIORITY] = { .type =
NLA_U32 },
933 [RTA_FLOW] = { .type =
NLA_U32 },
934 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
939 static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
941 struct rtnl_nexthop *nh = NULL;
942 struct rtnexthop *rtnh =
nla_data(attr);
946 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
947 nh = rtnl_route_nh_alloc();
951 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
952 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
953 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
955 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
956 struct nlattr *ntb[RTA_MAX + 1];
958 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
960 rtnh->rtnh_len -
sizeof(*rtnh),
965 if (ntb[RTA_GATEWAY]) {
966 struct nl_addr *addr;
975 rtnl_route_nh_set_gateway(nh, addr);
983 rtnl_route_nh_set_realms(nh, realms);
987 rtnl_route_add_nexthop(route, nh);
988 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
989 rtnh = RTNH_NEXT(rtnh);
995 rtnl_route_nh_free(nh);
1000 int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1003 struct rtnl_route *route;
1004 struct nlattr *tb[RTA_MAX + 1];
1005 struct nl_addr *src = NULL, *dst = NULL, *addr;
1006 struct rtnl_nexthop *old_nh = NULL;
1009 route = rtnl_route_alloc();
1015 route->ce_msgtype = nlh->nlmsg_type;
1017 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1022 route->rt_family = family = rtm->rtm_family;
1023 route->rt_tos = rtm->rtm_tos;
1024 route->rt_table = rtm->rtm_table;
1025 route->rt_type = rtm->rtm_type;
1026 route->rt_scope = rtm->rtm_scope;
1027 route->rt_protocol = rtm->rtm_protocol;
1028 route->rt_flags = rtm->rtm_flags;
1030 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1031 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1032 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1041 nl_addr_set_family(dst, rtm->rtm_family);
1044 nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
1045 err = rtnl_route_set_dst(route, dst);
1054 }
else if (rtm->rtm_src_len)
1059 nl_addr_set_prefixlen(src, rtm->rtm_src_len);
1060 rtnl_route_set_src(route, src);
1065 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1068 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1070 if (tb[RTA_PRIORITY])
1071 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1073 if (tb[RTA_PREFSRC]) {
1076 rtnl_route_set_pref_src(route, addr);
1080 if (tb[RTA_METRICS]) {
1081 struct nlattr *mtb[RTAX_MAX + 1];
1088 for (i = 1; i <= RTAX_MAX; i++) {
1089 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1091 if (rtnl_route_set_metric(route, i, m) < 0)
1097 if (tb[RTA_MULTIPATH])
1098 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1101 if (tb[RTA_CACHEINFO]) {
1102 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1103 sizeof(route->rt_cacheinfo));
1104 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1108 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1111 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1114 if (tb[RTA_GATEWAY]) {
1115 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1121 rtnl_route_nh_set_gateway(old_nh, addr);
1126 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1129 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1133 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1134 if (route->rt_nr_nh == 0) {
1138 rtnl_route_add_nexthop(route, old_nh);
1142 struct rtnl_nexthop *first;
1144 first = nl_list_first_entry(&route->rt_nexthops,
1145 struct rtnl_nexthop,
1150 if (rtnl_route_nh_compare(old_nh, first,
1151 old_nh->ce_mask, 0)) {
1156 rtnl_route_nh_free(old_nh);
1164 rtnl_route_put(route);
1172 int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1175 struct nlattr *metrics;
1176 struct rtmsg rtmsg = {
1177 .rtm_family = route->rt_family,
1178 .rtm_tos = route->rt_tos,
1179 .rtm_table = route->rt_table,
1180 .rtm_protocol = route->rt_protocol,
1181 .rtm_scope = route->rt_scope,
1182 .rtm_type = route->rt_type,
1183 .rtm_flags = route->rt_flags,
1186 if (route->rt_dst == NULL)
1187 return -NLE_MISSING_ATTR;
1193 if (rtmsg.rtm_scope == RT_SCOPE_NOWHERE)
1196 if (rtnl_route_get_nnexthops(route) == 1) {
1197 struct rtnl_nexthop *nh;
1198 nh = rtnl_route_nexthop_n(route, 0);
1199 rtmsg.rtm_flags |= nh->rtnh_flags;
1202 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1203 goto nla_put_failure;
1213 if (route->ce_mask & ROUTE_ATTR_SRC)
1216 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1219 if (route->ce_mask & ROUTE_ATTR_IIF)
1222 if (route->rt_nmetrics > 0) {
1226 if (metrics == NULL)
1227 goto nla_put_failure;
1229 for (i = 1; i <= RTAX_MAX; i++) {
1230 if (!rtnl_route_get_metric(route, i, &val))
1237 if (rtnl_route_get_nnexthops(route) == 1) {
1238 struct rtnl_nexthop *nh;
1240 nh = rtnl_route_nexthop_n(route, 0);
1241 if (nh->rtnh_gateway)
1243 if (nh->rtnh_ifindex)
1245 if (nh->rtnh_realms)
1247 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1248 struct nlattr *multipath;
1249 struct rtnl_nexthop *nh;
1252 goto nla_put_failure;
1254 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1255 struct rtnexthop *rtnh;
1259 goto nla_put_failure;
1261 rtnh->rtnh_flags = nh->rtnh_flags;
1262 rtnh->rtnh_hops = nh->rtnh_weight;
1263 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1265 if (nh->rtnh_gateway)
1269 if (nh->rtnh_realms)
1272 rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
1282 return -NLE_MSGSIZE;
1288 .oo_size =
sizeof(
struct rtnl_route),
1289 .oo_constructor = route_constructor,
1290 .oo_free_data = route_free_data,
1291 .oo_clone = route_clone,
1297 .oo_compare = route_compare,
1298 .oo_keygen = route_keygen,
1299 .oo_update = route_update,
1300 .oo_attrs2str = route_attrs2str,
1301 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1302 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),