27#include <netlink-private/netlink.h>
28#include <netlink-private/utils.h>
29#include <netlink-private/route/nexthop-encap.h>
30#include <netlink/netlink.h>
31#include <netlink/cache.h>
32#include <netlink/utils.h>
33#include <netlink/data.h>
34#include <netlink/hashtable.h>
35#include <netlink/route/rtnl.h>
36#include <netlink/route/route.h>
37#include <netlink/route/link.h>
38#include <netlink/route/nexthop.h>
39#include <linux/in_route.h>
42#define ROUTE_ATTR_FAMILY 0x000001
43#define ROUTE_ATTR_TOS 0x000002
44#define ROUTE_ATTR_TABLE 0x000004
45#define ROUTE_ATTR_PROTOCOL 0x000008
46#define ROUTE_ATTR_SCOPE 0x000010
47#define ROUTE_ATTR_TYPE 0x000020
48#define ROUTE_ATTR_FLAGS 0x000040
49#define ROUTE_ATTR_DST 0x000080
50#define ROUTE_ATTR_SRC 0x000100
51#define ROUTE_ATTR_IIF 0x000200
52#define ROUTE_ATTR_OIF 0x000400
53#define ROUTE_ATTR_GATEWAY 0x000800
54#define ROUTE_ATTR_PRIO 0x001000
55#define ROUTE_ATTR_PREF_SRC 0x002000
56#define ROUTE_ATTR_METRICS 0x004000
57#define ROUTE_ATTR_MULTIPATH 0x008000
58#define ROUTE_ATTR_REALMS 0x010000
59#define ROUTE_ATTR_CACHEINFO 0x020000
60#define ROUTE_ATTR_TTL_PROPAGATE 0x040000
63static void route_constructor(
struct nl_object *c)
65 struct rtnl_route *r = (
struct rtnl_route *) c;
67 r->rt_family = AF_UNSPEC;
68 r->rt_scope = RT_SCOPE_NOWHERE;
69 r->rt_table = RT_TABLE_MAIN;
70 r->rt_protocol = RTPROT_STATIC;
71 r->rt_type = RTN_UNICAST;
74 nl_init_list_head(&r->rt_nexthops);
77static void route_free_data(
struct nl_object *c)
79 struct rtnl_route *r = (
struct rtnl_route *) c;
80 struct rtnl_nexthop *nh, *tmp;
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);
95static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
97 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
98 struct rtnl_route *src = (
struct rtnl_route *) _src;
99 struct rtnl_nexthop *nh, *
new;
103 dst->rt_pref_src = NULL;
104 nl_init_list_head(&dst->rt_nexthops);
117 if (src->rt_pref_src) {
122 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
123 new = rtnl_route_nh_clone(nh);
127 rtnl_route_add_nexthop(dst,
new);
133static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
135 struct rtnl_route *r = (
struct rtnl_route *) a;
136 int cache = 0, flags;
139 if (r->rt_flags & RTM_F_CLONED)
142 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
147 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
154 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
156 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
158 if (r->ce_mask & ROUTE_ATTR_TYPE)
160 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
162 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
163 nl_dump(p,
"tos %#x ", r->rt_tos);
165 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
166 struct rtnl_nexthop *nh;
168 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
169 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
170 rtnl_route_nh_dump(nh, p);
174 flags = r->rt_flags & ~(RTM_F_CLONED);
175 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
179#define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
180 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
183 PRINT_FLAG(PERVASIVE);
186#define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
187 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
189 PRINT_FLAG(EQUALIZE);
193#define PRINT_FLAG(f) if (flags & RTCF_##f) { \
194 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
196 PRINT_FLAG(REDIRECTED);
197 PRINT_FLAG(DOREDIRECT);
198 PRINT_FLAG(DIRECTSRC);
200 PRINT_FLAG(BROADCAST);
201 PRINT_FLAG(MULTICAST);
211static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
213 _nl_auto_nl_cache
struct nl_cache *link_cache = NULL;
214 struct rtnl_route *r = (
struct rtnl_route *) a;
220 route_dump_line(a, p);
221 nl_dump_line(p,
" ");
223 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
224 nl_dump(p,
"preferred-src %s ",
227 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
229 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
231 if (r->ce_mask & ROUTE_ATTR_PRIO)
232 nl_dump(p,
"priority %#x ", r->rt_prio);
234 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
236 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
238 if (r->ce_mask & ROUTE_ATTR_IIF) {
244 nl_dump(p,
"iif %d ", r->rt_iif);
247 if (r->ce_mask & ROUTE_ATTR_SRC)
250 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
251 nl_dump(p,
" ttl-propagate %s",
252 r->rt_ttl_propagate ?
"enabled" :
"disabled");
257 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
258 struct rtnl_nexthop *nh;
260 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
261 nl_dump_line(p,
" ");
262 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
263 rtnl_route_nh_dump(nh, p);
268 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
269 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
270 r->rt_cacheinfo.rtci_error,
271 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
274 if (r->ce_mask & ROUTE_ATTR_METRICS) {
275 nl_dump_line(p,
" metrics [");
276 for (i = 0; i < RTAX_MAX; i++)
277 if (r->rt_metrics_mask & (1 << i))
279 rtnl_route_metric2str(i+1,
286static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
288 struct rtnl_route *route = (
struct rtnl_route *) obj;
290 route_dump_details(obj, p);
292 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
295 nl_dump_line(p,
" used %u refcnt %u last-use %us "
297 ci->rtci_used, ci->rtci_clntref,
303static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
306 struct rtnl_route *route = (
struct rtnl_route *) obj;
307 unsigned int rkey_sz;
308 struct nl_addr *addr = NULL;
309 _nl_auto_free
struct route_hash_key {
315 } __attribute__((packed)) *rkey = NULL;
317 char buf[INET6_ADDRSTRLEN+5];
321 addr = route->rt_dst;
323 rkey_sz =
sizeof(*rkey);
326 rkey = calloc(1, rkey_sz);
328 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
332 rkey->rt_family = route->rt_family;
333 rkey->rt_tos = route->rt_tos;
334 rkey->rt_table = route->rt_table;
335 rkey->rt_prio = route->rt_prio;
340 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
342 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d "
343 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
344 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
350static uint32_t route_id_attrs_get(
struct nl_object *obj)
352 struct rtnl_route *route = (
struct rtnl_route *)obj;
353 struct nl_object_ops *ops = obj->ce_ops;
354 uint32_t rv = ops->oo_id_attrs;
357 if (route->rt_family == AF_MPLS)
358 rv &= ~ROUTE_ATTR_PRIO;
363static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
364 uint64_t attrs,
int flags)
366 struct rtnl_route *a = (
struct rtnl_route *) _a;
367 struct rtnl_route *b = (
struct rtnl_route *) _b;
368 struct rtnl_nexthop *nh_a, *nh_b;
372#define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
374 diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
375 diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
376 diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
377 diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
378 diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
379 diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
380 diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
381 diff |= ROUTE_DIFF(DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
382 diff |= ROUTE_DIFF(SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
383 diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
384 diff |= ROUTE_DIFF(PREF_SRC,
nl_addr_cmp(a->rt_pref_src,
386 diff |= ROUTE_DIFF(TTL_PROPAGATE,
387 a->rt_ttl_propagate != b->rt_ttl_propagate);
389 if (flags & LOOSE_COMPARISON) {
390 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
392 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
394 if (!rtnl_route_nh_compare(nh_a, nh_b,
405 for (i = 0; i < RTAX_MAX - 1; i++) {
406 if (a->rt_metrics_mask & (1 << i) &&
407 (!(b->rt_metrics_mask & (1 << i)) ||
408 a->rt_metrics[i] != b->rt_metrics[i]))
409 diff |= ROUTE_DIFF(METRICS, 1);
412 diff |= ROUTE_DIFF(FLAGS,
413 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
415 if (a->rt_nr_nh != b->rt_nr_nh)
419 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
421 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
423 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
434 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
436 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
438 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
447 for (i = 0; i < RTAX_MAX - 1; i++) {
448 if ((a->rt_metrics_mask & (1 << i)) ^
449 (b->rt_metrics_mask & (1 << i)))
450 diff |= ROUTE_DIFF(METRICS, 1);
452 diff |= ROUTE_DIFF(METRICS,
453 a->rt_metrics[i] != b->rt_metrics[i]);
456 diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
463 diff |= ROUTE_DIFF(MULTIPATH, 1);
469static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
471 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
472 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
473 struct rtnl_nexthop *new_nh;
474 int action = new_obj->ce_msgtype;
476 char buf[INET6_ADDRSTRLEN+5];
486 if (new_route->rt_family != AF_INET6 ||
487 new_route->rt_table == RT_TABLE_LOCAL)
488 return -NLE_OPNOTSUPP;
494 if (rtnl_route_get_nnexthops(new_route) != 1)
495 return -NLE_OPNOTSUPP;
502 new_nh = rtnl_route_nexthop_n(new_route, 0);
503 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
504 return -NLE_OPNOTSUPP;
507 case RTM_NEWROUTE : {
508 struct rtnl_nexthop *cloned_nh;
509 struct rtnl_nexthop *old_nh;
514 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, rtnh_list) {
515 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
523 cloned_nh = rtnl_route_nh_clone(new_nh);
526 rtnl_route_add_nexthop(old_route, cloned_nh);
528 NL_DBG(2,
"Route obj %p updated. Added "
529 "nexthop %p via %s\n", old_route, cloned_nh,
534 case RTM_DELROUTE : {
535 struct rtnl_nexthop *old_nh;
543 if (rtnl_route_get_nnexthops(old_route) <= 1)
544 return -NLE_OPNOTSUPP;
549 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
551 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
553 rtnl_route_remove_nexthop(old_route, old_nh);
555 NL_DBG(2,
"Route obj %p updated. Removed "
556 "nexthop %p via %s\n", old_route,
561 rtnl_route_nh_free(old_nh);
568 NL_DBG(2,
"Unknown action associated "
569 "to object %p during route update\n", new_obj);
570 return -NLE_OPNOTSUPP;
576static const struct trans_tbl route_attrs[] = {
577 __ADD(ROUTE_ATTR_FAMILY, family),
578 __ADD(ROUTE_ATTR_TOS, tos),
579 __ADD(ROUTE_ATTR_TABLE, table),
580 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
581 __ADD(ROUTE_ATTR_SCOPE, scope),
582 __ADD(ROUTE_ATTR_TYPE, type),
583 __ADD(ROUTE_ATTR_FLAGS, flags),
584 __ADD(ROUTE_ATTR_DST, dst),
585 __ADD(ROUTE_ATTR_SRC, src),
586 __ADD(ROUTE_ATTR_IIF, iif),
587 __ADD(ROUTE_ATTR_OIF, oif),
588 __ADD(ROUTE_ATTR_GATEWAY, gateway),
589 __ADD(ROUTE_ATTR_PRIO, prio),
590 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
591 __ADD(ROUTE_ATTR_METRICS, metrics),
592 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
593 __ADD(ROUTE_ATTR_REALMS, realms),
594 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
595 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
598static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
600 return __flags2str(attrs, buf, len, route_attrs,
601 ARRAY_SIZE(route_attrs));
609struct rtnl_route *rtnl_route_alloc(
void)
614void rtnl_route_get(
struct rtnl_route *route)
619void rtnl_route_put(
struct rtnl_route *route)
631void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
633 route->rt_table = table;
634 route->ce_mask |= ROUTE_ATTR_TABLE;
637uint32_t rtnl_route_get_table(
struct rtnl_route *route)
639 return route->rt_table;
642void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
644 route->rt_scope = scope;
645 route->ce_mask |= ROUTE_ATTR_SCOPE;
648uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
650 return route->rt_scope;
653void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
656 route->ce_mask |= ROUTE_ATTR_TOS;
659uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
661 return route->rt_tos;
664void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
666 route->rt_protocol = protocol;
667 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
670uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
672 return route->rt_protocol;
675void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
677 route->rt_prio = prio;
678 route->ce_mask |= ROUTE_ATTR_PRIO;
681uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
683 return route->rt_prio;
686int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
693 route->rt_family = family;
694 route->ce_mask |= ROUTE_ATTR_FAMILY;
698 return -NLE_AF_NOSUPPORT;
701uint8_t rtnl_route_get_family(
struct rtnl_route *route)
703 return route->rt_family;
706int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
708 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
709 if (addr->a_family != route->rt_family)
710 return -NLE_AF_MISMATCH;
712 route->rt_family = addr->a_family;
718 route->rt_dst = addr;
720 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
725struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
727 return route->rt_dst;
730int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
732 if (addr->a_family == AF_INET)
733 return -NLE_SRCRT_NOSUPPORT;
735 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
736 if (addr->a_family != route->rt_family)
737 return -NLE_AF_MISMATCH;
739 route->rt_family = addr->a_family;
745 route->rt_src = addr;
746 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
751struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
753 return route->rt_src;
756int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
761 route->rt_type = type;
762 route->ce_mask |= ROUTE_ATTR_TYPE;
767uint8_t rtnl_route_get_type(
struct rtnl_route *route)
769 return route->rt_type;
772void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
774 route->rt_flag_mask |= flags;
775 route->rt_flags |= flags;
776 route->ce_mask |= ROUTE_ATTR_FLAGS;
779void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
781 route->rt_flag_mask |= flags;
782 route->rt_flags &= ~flags;
783 route->ce_mask |= ROUTE_ATTR_FLAGS;
786uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
788 return route->rt_flags;
791int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
793 if (metric > RTAX_MAX || metric < 1)
796 route->rt_metrics[metric - 1] = value;
798 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
799 route->rt_nmetrics++;
800 route->rt_metrics_mask |= (1 << (metric - 1));
803 route->ce_mask |= ROUTE_ATTR_METRICS;
808int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
810 if (metric > RTAX_MAX || metric < 1)
813 if (route->rt_metrics_mask & (1 << (metric - 1))) {
814 route->rt_nmetrics--;
815 route->rt_metrics_mask &= ~(1 << (metric - 1));
821int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
823 if (metric > RTAX_MAX || metric < 1)
826 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
827 return -NLE_OBJ_NOTFOUND;
830 *value = route->rt_metrics[metric - 1];
835int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
837 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
838 if (addr->a_family != route->rt_family)
839 return -NLE_AF_MISMATCH;
841 route->rt_family = addr->a_family;
843 if (route->rt_pref_src)
847 route->rt_pref_src = addr;
848 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
853struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
855 return route->rt_pref_src;
858void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
860 route->rt_iif = ifindex;
861 route->ce_mask |= ROUTE_ATTR_IIF;
864int rtnl_route_get_iif(
struct rtnl_route *route)
866 return route->rt_iif;
869void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
871 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
873 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
876void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
878 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
880 nl_list_del(&nh->rtnh_list);
884struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
886 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
887 return &route->rt_nexthops;
892int rtnl_route_get_nnexthops(
struct rtnl_route *route)
894 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
895 return route->rt_nr_nh;
900void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
901 void (*cb)(
struct rtnl_nexthop *,
void *),
904 struct rtnl_nexthop *nh;
906 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
907 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
913struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
915 struct rtnl_nexthop *nh;
918 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
920 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
921 if (i == n)
return nh;
928void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
930 route->rt_ttl_propagate = ttl_prop;
931 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
934int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
938 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
939 return -NLE_MISSING_ATTR;
940 return route->rt_ttl_propagate;
965 if (route->rt_type == RTN_LOCAL)
966 return RT_SCOPE_HOST;
968 if (route->rt_family == AF_MPLS)
969 return RT_SCOPE_UNIVERSE;
971 if (!nl_list_empty(&route->rt_nexthops)) {
972 struct rtnl_nexthop *nh;
978 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
979 if (nh->rtnh_gateway)
980 return RT_SCOPE_UNIVERSE;
984 return RT_SCOPE_LINK;
989static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
991 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
994 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
997static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
1003 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1014static struct nla_policy route_policy[RTA_MAX+1] = {
1016 [RTA_OIF] = { .type =
NLA_U32 },
1017 [RTA_PRIORITY] = { .type =
NLA_U32 },
1018 [RTA_FLOW] = { .type =
NLA_U32 },
1019 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1022 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1024 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1027static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1029 struct rtnexthop *rtnh =
nla_data(attr);
1033 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1034 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *nh = NULL;
1036 nh = rtnl_route_nh_alloc();
1040 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1041 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1042 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1044 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1045 struct nlattr *ntb[RTA_MAX + 1];
1047 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1049 rtnh->rtnh_len -
sizeof(*rtnh),
1054 if (ntb[RTA_GATEWAY]) {
1055 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1062 rtnl_route_nh_set_gateway(nh, addr);
1065 if (ntb[RTA_FLOW]) {
1069 rtnl_route_nh_set_realms(nh, realms);
1072 if (ntb[RTA_NEWDST]) {
1073 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1080 err = rtnl_route_nh_set_newdst(nh, addr);
1086 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1088 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1092 err = rtnl_route_nh_set_via(nh, addr);
1097 if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1098 err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1099 ntb[RTA_ENCAP_TYPE],
1106 rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh));
1107 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1108 rtnh = RTNH_NEXT(rtnh);
1114int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1116 _nl_auto_rtnl_route
struct rtnl_route *route = NULL;
1117 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *old_nh = NULL;
1118 _nl_auto_nl_addr
struct nl_addr *src = NULL;
1119 _nl_auto_nl_addr
struct nl_addr *dst = NULL;
1120 struct nlattr *tb[RTA_MAX + 1];
1125 route = rtnl_route_alloc();
1129 route->ce_msgtype = nlh->nlmsg_type;
1131 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1136 route->rt_family = family = rtm->rtm_family;
1137 route->rt_tos = rtm->rtm_tos;
1138 route->rt_table = rtm->rtm_table;
1139 route->rt_type = rtm->rtm_type;
1140 route->rt_scope = rtm->rtm_scope;
1141 route->rt_protocol = rtm->rtm_protocol;
1142 route->rt_flags = rtm->rtm_flags;
1145 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1146 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1147 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1153 if (family != AF_MPLS)
1154 route->ce_mask |= ROUTE_ATTR_PRIO;
1180 err = rtnl_route_set_dst(route, dst);
1187 }
else if (rtm->rtm_src_len)
1193 rtnl_route_set_src(route, src);
1197 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1200 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1202 if (tb[RTA_PRIORITY])
1203 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1205 if (tb[RTA_PREFSRC]) {
1206 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1210 rtnl_route_set_pref_src(route, addr);
1213 if (tb[RTA_METRICS]) {
1214 struct nlattr *mtb[RTAX_MAX + 1];
1221 for (i = 1; i <= RTAX_MAX; i++) {
1222 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1225 err = rtnl_route_set_metric(route, i, m);
1232 if (tb[RTA_MULTIPATH]) {
1233 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1237 if (tb[RTA_CACHEINFO]) {
1238 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1239 sizeof(route->rt_cacheinfo));
1240 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1244 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1247 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1250 if (tb[RTA_GATEWAY]) {
1251 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1253 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1259 rtnl_route_nh_set_gateway(old_nh, addr);
1263 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1266 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1269 if (tb[RTA_NEWDST]) {
1270 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1272 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1279 err = rtnl_route_nh_set_newdst(old_nh, addr);
1285 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1286 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1287 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1289 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1292 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1296 err = rtnl_route_nh_set_via(old_nh, addr);
1301 if (tb[RTA_TTL_PROPAGATE]) {
1302 rtnl_route_set_ttl_propagate(route,
1306 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1307 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1310 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1311 tb[RTA_ENCAP_TYPE], old_nh);
1317 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1318 if (route->rt_nr_nh == 0) {
1322 rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh));
1326 struct rtnl_nexthop *first;
1328 first = nl_list_first_entry(&route->rt_nexthops,
1329 struct rtnl_nexthop,
1334 if (rtnl_route_nh_compare(old_nh, first,
1335 old_nh->ce_mask, 0)) {
1341 *result = _nl_steal_pointer(&route);
1345int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1348 struct nlattr *metrics;
1349 struct rtmsg rtmsg = {
1350 .rtm_family = route->rt_family,
1351 .rtm_tos = route->rt_tos,
1352 .rtm_table = route->rt_table,
1353 .rtm_protocol = route->rt_protocol,
1354 .rtm_scope = route->rt_scope,
1355 .rtm_type = route->rt_type,
1356 .rtm_flags = route->rt_flags,
1359 if (route->rt_dst == NULL)
1360 return -NLE_MISSING_ATTR;
1366 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1369 if (rtnl_route_get_nnexthops(route) == 1) {
1370 struct rtnl_nexthop *nh;
1371 nh = rtnl_route_nexthop_n(route, 0);
1372 rtmsg.rtm_flags |= nh->rtnh_flags;
1375 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1376 goto nla_put_failure;
1382 if (route->rt_family != AF_MPLS)
1388 if (route->ce_mask & ROUTE_ATTR_PRIO)
1391 if (route->ce_mask & ROUTE_ATTR_SRC)
1394 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1397 if (route->ce_mask & ROUTE_ATTR_IIF)
1400 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1401 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1403 if (route->rt_nmetrics > 0) {
1407 if (metrics == NULL)
1408 goto nla_put_failure;
1410 for (i = 1; i <= RTAX_MAX; i++) {
1411 if (!rtnl_route_get_metric(route, i, &val))
1418 if (rtnl_route_get_nnexthops(route) == 1) {
1419 struct rtnl_nexthop *nh;
1421 nh = rtnl_route_nexthop_n(route, 0);
1422 if (nh->rtnh_gateway)
1424 if (nh->rtnh_ifindex)
1426 if (nh->rtnh_realms)
1428 if (nh->rtnh_newdst)
1430 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1431 goto nla_put_failure;
1432 if (nh->rtnh_encap &&
1433 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1434 goto nla_put_failure;
1435 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1436 struct nlattr *multipath;
1437 struct rtnl_nexthop *nh;
1440 goto nla_put_failure;
1442 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1443 struct rtnexthop *rtnh;
1447 goto nla_put_failure;
1449 rtnh->rtnh_flags = nh->rtnh_flags;
1450 rtnh->rtnh_hops = nh->rtnh_weight;
1451 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1453 if (nh->rtnh_gateway)
1457 if (nh->rtnh_newdst)
1461 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1462 goto nla_put_failure;
1464 if (nh->rtnh_realms)
1467 if (nh->rtnh_encap &&
1468 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1469 goto nla_put_failure;
1471 rtnh->rtnh_len = (
char *) nlmsg_tail(msg->nm_nlh) -
1481 return -NLE_MSGSIZE;
1485struct nl_object_ops route_obj_ops = {
1486 .oo_name =
"route/route",
1487 .oo_size =
sizeof(
struct rtnl_route),
1488 .oo_constructor = route_constructor,
1489 .oo_free_data = route_free_data,
1490 .oo_clone = route_clone,
1496 .oo_compare = route_compare,
1497 .oo_keygen = route_keygen,
1498 .oo_update = route_update,
1499 .oo_attrs2str = route_attrs2str,
1500 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1501 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1503 .oo_id_attrs_get = route_id_attrs_get,
int nl_addr_iszero(const struct nl_addr *addr)
Returns true if the address consists of all zeros.
void nl_addr_set_prefixlen(struct nl_addr *addr, int prefixlen)
Set the prefix length of an abstract address.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
struct nl_addr * nl_addr_build(int family, const void *buf, size_t size)
Allocate abstract address based on a binary address.
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
void * nl_addr_get_binary_addr(const struct nl_addr *addr)
Get binary address of abstract address object.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_addr * nl_addr_alloc(size_t maxsize)
Allocate empty abstract address.
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
int nl_addr_get_family(const struct nl_addr *addr)
Return address family.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.
unsigned int nl_addr_get_prefixlen(const struct nl_addr *addr)
Return prefix length of abstract address object.
unsigned int nl_addr_get_len(const struct nl_addr *addr)
Get length of binary address of abstract address object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len, const struct nla_policy *policy)
Create attribute index based on a stream of attributes.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
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.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
int nla_memcpy(void *dest, const struct nlattr *src, int count)
Copy attribute payload to another memory area.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_len(const struct nlattr *nla)
Return length of the payload .
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
struct nlattr * nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
Reserve space for a attribute.
@ NLA_NESTED
Nested attributes.
struct nl_cache * nl_cache_mngt_require_safe(const char *name)
Return cache previously provided via nl_cache_mngt_provide()
char * rtnl_link_i2name(struct nl_cache *cache, int ifindex, char *dst, size_t len)
Translate interface index to corresponding link name.
void * nlmsg_data(const struct nlmsghdr *nlh)
Return pointer to message payload.
void * nlmsg_reserve(struct nl_msg *n, size_t len, int pad)
Reserve room for additional data in a netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
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_object_get(struct nl_object *obj)
Acquire a reference on a object.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
int rtnl_route_guess_scope(struct rtnl_route *route)
Guess scope of a route object.
int nl_get_user_hz(void)
Return the value of HZ.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
@ NL_DUMP_STATS
Dump all attributes including statistics.
@ NL_DUMP_LINE
Dump object briefly on one line.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
int dp_ivar
PRIVATE Owned by the current caller.
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.