27#include "nl-default.h"
29#include <linux/in_route.h>
31#include <netlink/netlink.h>
32#include <netlink/cache.h>
33#include <netlink/utils.h>
34#include <netlink/data.h>
35#include <netlink/hashtable.h>
36#include <netlink/route/rtnl.h>
37#include <netlink/route/route.h>
38#include <netlink/route/link.h>
39#include <netlink/route/nexthop.h>
42#include "nl-aux-route/nl-route.h"
43#include "nl-priv-dynamic-core/nl-core.h"
44#include "nexthop-encap.h"
56 uint8_t rt_ttl_propagate;
58 struct nl_addr *rt_dst;
59 struct nl_addr *rt_src;
63 uint32_t rt_metrics[RTAX_MAX];
64 uint32_t rt_metrics_mask;
67 struct nl_addr *rt_pref_src;
70 uint32_t rt_flag_mask;
73#define ROUTE_ATTR_FAMILY 0x000001
74#define ROUTE_ATTR_TOS 0x000002
75#define ROUTE_ATTR_TABLE 0x000004
76#define ROUTE_ATTR_PROTOCOL 0x000008
77#define ROUTE_ATTR_SCOPE 0x000010
78#define ROUTE_ATTR_TYPE 0x000020
79#define ROUTE_ATTR_FLAGS 0x000040
80#define ROUTE_ATTR_DST 0x000080
81#define ROUTE_ATTR_SRC 0x000100
82#define ROUTE_ATTR_IIF 0x000200
83#define ROUTE_ATTR_OIF 0x000400
84#define ROUTE_ATTR_GATEWAY 0x000800
85#define ROUTE_ATTR_PRIO 0x001000
86#define ROUTE_ATTR_PREF_SRC 0x002000
87#define ROUTE_ATTR_METRICS 0x004000
88#define ROUTE_ATTR_MULTIPATH 0x008000
89#define ROUTE_ATTR_REALMS 0x010000
90#define ROUTE_ATTR_CACHEINFO 0x020000
91#define ROUTE_ATTR_TTL_PROPAGATE 0x040000
92#define ROUTE_ATTR_NHID 0x080000
95static void route_constructor(
struct nl_object *c)
97 struct rtnl_route *r = (
struct rtnl_route *) c;
99 r->rt_family = AF_UNSPEC;
100 r->rt_scope = RT_SCOPE_NOWHERE;
101 r->rt_table = RT_TABLE_MAIN;
102 r->rt_protocol = RTPROT_STATIC;
103 r->rt_type = RTN_UNICAST;
106 nl_init_list_head(&r->rt_nexthops);
109static void route_free_data(
struct nl_object *c)
111 struct rtnl_route *r = (
struct rtnl_route *) c;
112 struct rtnl_nexthop *nh, *tmp;
121 nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
122 rtnl_route_remove_nexthop(r, nh);
123 rtnl_route_nh_free(nh);
127static int route_clone(
struct nl_object *_dst,
struct nl_object *_src)
129 struct rtnl_route *dst = (
struct rtnl_route *) _dst;
130 struct rtnl_route *src = (
struct rtnl_route *) _src;
131 struct rtnl_nexthop *nh, *
new;
135 dst->rt_pref_src = NULL;
136 nl_init_list_head(&dst->rt_nexthops);
149 if (src->rt_pref_src) {
154 nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
155 new = rtnl_route_nh_clone(nh);
159 rtnl_route_add_nexthop(dst,
new);
165static void route_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
167 struct rtnl_route *r = (
struct rtnl_route *) a;
168 int cache = 0, flags;
171 if (r->rt_flags & RTM_F_CLONED)
174 nl_dump_line(p,
"%s ", nl_af2str(r->rt_family, buf,
sizeof(buf)));
179 if (!(r->ce_mask & ROUTE_ATTR_DST) ||
186 if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
188 rtnl_route_table2str(r->rt_table, buf,
sizeof(buf)));
190 if (r->ce_mask & ROUTE_ATTR_TYPE)
192 nl_rtntype2str(r->rt_type, buf,
sizeof(buf)));
194 if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
195 nl_dump(p,
"tos %#x ", r->rt_tos);
197 if (r->ce_mask & ROUTE_ATTR_NHID)
198 nl_dump(p,
"nhid %u ", r->rt_nhid);
200 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
201 struct rtnl_nexthop *nh;
203 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
204 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
205 rtnl_route_nh_dump(nh, p);
209 flags = r->rt_flags & ~(RTM_F_CLONED);
210 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
214#define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
215 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
218 PRINT_FLAG(PERVASIVE);
221#define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
222 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
224 PRINT_FLAG(EQUALIZE);
228#define PRINT_FLAG(f) if (flags & RTCF_##f) { \
229 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
231 PRINT_FLAG(REDIRECTED);
232 PRINT_FLAG(DOREDIRECT);
233 PRINT_FLAG(DIRECTSRC);
235 PRINT_FLAG(BROADCAST);
236 PRINT_FLAG(MULTICAST);
246static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
248 _nl_auto_nl_cache
struct nl_cache *link_cache = NULL;
249 struct rtnl_route *r = (
struct rtnl_route *) a;
255 route_dump_line(a, p);
256 nl_dump_line(p,
" ");
258 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
259 nl_dump(p,
"preferred-src %s ",
262 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
264 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
266 if (r->ce_mask & ROUTE_ATTR_PRIO)
267 nl_dump(p,
"priority %#x ", r->rt_prio);
269 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
271 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
273 if (r->ce_mask & ROUTE_ATTR_IIF) {
279 nl_dump(p,
"iif %d ", r->rt_iif);
282 if (r->ce_mask & ROUTE_ATTR_SRC)
285 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
286 nl_dump(p,
" ttl-propagate %s",
287 r->rt_ttl_propagate ?
"enabled" :
"disabled");
290 if (r->ce_mask & ROUTE_ATTR_NHID)
291 nl_dump(p,
"nhid %u ", r->rt_nhid);
295 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
296 struct rtnl_nexthop *nh;
298 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
299 nl_dump_line(p,
" ");
300 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
301 rtnl_route_nh_dump(nh, p);
306 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
307 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
308 r->rt_cacheinfo.rtci_error,
309 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
312 if (r->ce_mask & ROUTE_ATTR_METRICS) {
313 nl_dump_line(p,
" metrics [");
314 for (i = 0; i < RTAX_MAX; i++)
315 if (r->rt_metrics_mask & (1 << i))
317 rtnl_route_metric2str(i+1,
324static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
326 struct rtnl_route *route = (
struct rtnl_route *) obj;
328 route_dump_details(obj, p);
330 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
333 nl_dump_line(p,
" used %u refcnt %u last-use %us "
335 ci->rtci_used, ci->rtci_clntref,
341static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
344 struct rtnl_route *route = (
struct rtnl_route *) obj;
345 unsigned int rkey_sz;
346 struct nl_addr *addr = NULL;
347 _nl_auto_free
struct route_hash_key {
353 } _nl_packed *rkey = NULL;
354 char buf[INET6_ADDRSTRLEN+5];
357 addr = route->rt_dst;
359 rkey_sz =
sizeof(*rkey);
362 rkey = calloc(1, rkey_sz);
364 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
368 rkey->rt_family = route->rt_family;
369 rkey->rt_tos = route->rt_tos;
370 rkey->rt_table = route->rt_table;
371 rkey->rt_prio = route->rt_prio;
376 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
379 "route %p key (fam %d tos %d table %d prio %d addr %s) keysz %d hash 0x%x\n",
380 route, rkey->rt_family, rkey->rt_tos, rkey->rt_table,
381 rkey->rt_prio,
nl_addr2str(addr, buf,
sizeof(buf)), rkey_sz,
387static uint32_t route_id_attrs_get(
struct nl_object *obj)
389 struct rtnl_route *route = (
struct rtnl_route *)obj;
390 struct nl_object_ops *ops = obj->ce_ops;
391 uint32_t rv = ops->oo_id_attrs;
394 if (route->rt_family == AF_MPLS)
395 rv &= ~ROUTE_ATTR_PRIO;
400static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
401 uint64_t attrs,
int flags)
403 struct rtnl_route *a = (
struct rtnl_route *) _a;
404 struct rtnl_route *b = (
struct rtnl_route *) _b;
405 struct rtnl_nexthop *nh_a, *nh_b;
409#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
410 diff |= _DIFF(ROUTE_ATTR_FAMILY, a->rt_family != b->rt_family);
411 diff |= _DIFF(ROUTE_ATTR_TOS, a->rt_tos != b->rt_tos);
412 diff |= _DIFF(ROUTE_ATTR_TABLE, a->rt_table != b->rt_table);
413 diff |= _DIFF(ROUTE_ATTR_PROTOCOL, a->rt_protocol != b->rt_protocol);
414 diff |= _DIFF(ROUTE_ATTR_SCOPE, a->rt_scope != b->rt_scope);
415 diff |= _DIFF(ROUTE_ATTR_TYPE, a->rt_type != b->rt_type);
416 diff |= _DIFF(ROUTE_ATTR_PRIO, a->rt_prio != b->rt_prio);
417 diff |= _DIFF(ROUTE_ATTR_DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
418 diff |= _DIFF(ROUTE_ATTR_SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
419 diff |= _DIFF(ROUTE_ATTR_IIF, a->rt_iif != b->rt_iif);
420 diff |= _DIFF(ROUTE_ATTR_PREF_SRC,
422 diff |= _DIFF(ROUTE_ATTR_TTL_PROPAGATE,
423 a->rt_ttl_propagate != b->rt_ttl_propagate);
424 diff |= _DIFF(ROUTE_ATTR_NHID, a->rt_nhid != b->rt_nhid);
426 if (flags & LOOSE_COMPARISON) {
427 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
429 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
431 if (!rtnl_route_nh_compare(nh_a, nh_b,
442 for (i = 0; i < RTAX_MAX - 1; i++) {
443 if (a->rt_metrics_mask & (1 << i) &&
444 (!(b->rt_metrics_mask & (1 << i)) ||
445 a->rt_metrics[i] != b->rt_metrics[i]))
446 diff |= _DIFF(ROUTE_ATTR_METRICS, 1);
449 diff |= _DIFF(ROUTE_ATTR_FLAGS,
450 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
452 if (a->rt_nr_nh != b->rt_nr_nh)
456 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
458 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
471 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
473 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
484 for (i = 0; i < RTAX_MAX - 1; i++) {
485 if ((a->rt_metrics_mask & (1 << i)) ^
486 (b->rt_metrics_mask & (1 << i)))
487 diff |= _DIFF(ROUTE_ATTR_METRICS, 1);
489 diff |= _DIFF(ROUTE_ATTR_METRICS,
490 a->rt_metrics[i] != b->rt_metrics[i]);
493 diff |= _DIFF(ROUTE_ATTR_FLAGS, a->rt_flags != b->rt_flags);
500 diff |= _DIFF(ROUTE_ATTR_MULTIPATH, 1);
505static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
507 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
508 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
509 struct rtnl_nexthop *new_nh;
510 int action = new_obj->ce_msgtype;
511 char buf[INET6_ADDRSTRLEN+5];
520 if (new_route->rt_family != AF_INET6 ||
521 new_route->rt_table == RT_TABLE_LOCAL)
522 return -NLE_OPNOTSUPP;
528 if (rtnl_route_get_nnexthops(new_route) != 1)
529 return -NLE_OPNOTSUPP;
536 new_nh = rtnl_route_nexthop_n(new_route, 0);
537 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
538 return -NLE_OPNOTSUPP;
541 case RTM_NEWROUTE : {
542 struct rtnl_nexthop *cloned_nh;
543 struct rtnl_nexthop *old_nh;
548 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, rtnh_list) {
557 cloned_nh = rtnl_route_nh_clone(new_nh);
560 rtnl_route_add_nexthop(old_route, cloned_nh);
562 NL_DBG(2,
"Route obj %p updated. Added "
563 "nexthop %p via %s\n", old_route, cloned_nh,
568 case RTM_DELROUTE : {
569 struct rtnl_nexthop *old_nh;
577 if (rtnl_route_get_nnexthops(old_route) <= 1)
578 return -NLE_OPNOTSUPP;
583 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
587 rtnl_route_remove_nexthop(old_route, old_nh);
589 NL_DBG(2,
"Route obj %p updated. Removed "
590 "nexthop %p via %s\n", old_route,
595 rtnl_route_nh_free(old_nh);
602 NL_DBG(2,
"Unknown action associated "
603 "to object %p during route update\n", new_obj);
604 return -NLE_OPNOTSUPP;
610static const struct trans_tbl route_attrs[] = {
611 __ADD(ROUTE_ATTR_FAMILY, family),
612 __ADD(ROUTE_ATTR_TOS, tos),
613 __ADD(ROUTE_ATTR_TABLE, table),
614 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
615 __ADD(ROUTE_ATTR_SCOPE, scope),
616 __ADD(ROUTE_ATTR_TYPE, type),
617 __ADD(ROUTE_ATTR_FLAGS, flags),
618 __ADD(ROUTE_ATTR_DST, dst),
619 __ADD(ROUTE_ATTR_SRC, src),
620 __ADD(ROUTE_ATTR_IIF, iif),
621 __ADD(ROUTE_ATTR_OIF, oif),
622 __ADD(ROUTE_ATTR_GATEWAY, gateway),
623 __ADD(ROUTE_ATTR_PRIO, prio),
624 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
625 __ADD(ROUTE_ATTR_METRICS, metrics),
626 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
627 __ADD(ROUTE_ATTR_REALMS, realms),
628 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
629 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
630 __ADD(ROUTE_ATTR_NHID, nhid),
633static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
635 return __flags2str(attrs, buf, len, route_attrs,
636 ARRAY_SIZE(route_attrs));
644struct rtnl_route *rtnl_route_alloc(
void)
649void rtnl_route_get(
struct rtnl_route *route)
654void rtnl_route_put(
struct rtnl_route *route)
666void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
668 route->rt_table = table;
669 route->ce_mask |= ROUTE_ATTR_TABLE;
672uint32_t rtnl_route_get_table(
struct rtnl_route *route)
674 return route->rt_table;
677void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
679 route->rt_scope = scope;
680 route->ce_mask |= ROUTE_ATTR_SCOPE;
683uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
685 return route->rt_scope;
688void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
691 route->ce_mask |= ROUTE_ATTR_TOS;
694uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
696 return route->rt_tos;
699void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
701 route->rt_protocol = protocol;
702 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
705uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
707 return route->rt_protocol;
710void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
712 route->rt_prio = prio;
713 route->ce_mask |= ROUTE_ATTR_PRIO;
716uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
718 return route->rt_prio;
721int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
728 route->rt_family = family;
729 route->ce_mask |= ROUTE_ATTR_FAMILY;
733 return -NLE_AF_NOSUPPORT;
736uint8_t rtnl_route_get_family(
struct rtnl_route *route)
738 return route->rt_family;
741int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
743 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
744 if (addr->a_family != route->rt_family)
745 return -NLE_AF_MISMATCH;
747 route->rt_family = addr->a_family;
753 route->rt_dst = addr;
755 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
760struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
762 return route->rt_dst;
765int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
767 if (addr->a_family == AF_INET)
768 return -NLE_SRCRT_NOSUPPORT;
770 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
771 if (addr->a_family != route->rt_family)
772 return -NLE_AF_MISMATCH;
774 route->rt_family = addr->a_family;
780 route->rt_src = addr;
781 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
786struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
788 return route->rt_src;
791int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
796 route->rt_type = type;
797 route->ce_mask |= ROUTE_ATTR_TYPE;
802uint8_t rtnl_route_get_type(
struct rtnl_route *route)
804 return route->rt_type;
807void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
809 route->rt_flag_mask |= flags;
810 route->rt_flags |= flags;
811 route->ce_mask |= ROUTE_ATTR_FLAGS;
814void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
816 route->rt_flag_mask |= flags;
817 route->rt_flags &= ~flags;
818 route->ce_mask |= ROUTE_ATTR_FLAGS;
821uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
823 return route->rt_flags;
826int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
828 if (metric > RTAX_MAX || metric < 1)
831 route->rt_metrics[metric - 1] = value;
833 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
834 route->rt_nmetrics++;
835 route->rt_metrics_mask |= (1 << (metric - 1));
838 route->ce_mask |= ROUTE_ATTR_METRICS;
843int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
845 if (metric > RTAX_MAX || metric < 1)
848 if (route->rt_metrics_mask & (1 << (metric - 1))) {
849 route->rt_nmetrics--;
850 route->rt_metrics_mask &= ~(1 << (metric - 1));
856int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
858 if (metric > RTAX_MAX || metric < 1)
861 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
862 return -NLE_OBJ_NOTFOUND;
865 *value = route->rt_metrics[metric - 1];
870int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
872 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
873 if (addr->a_family != route->rt_family)
874 return -NLE_AF_MISMATCH;
876 route->rt_family = addr->a_family;
878 if (route->rt_pref_src)
882 route->rt_pref_src = addr;
883 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
888struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
890 return route->rt_pref_src;
893void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
895 route->rt_iif = ifindex;
896 route->ce_mask |= ROUTE_ATTR_IIF;
899int rtnl_route_get_iif(
struct rtnl_route *route)
901 return route->rt_iif;
904void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
906 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
908 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
911void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
913 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
915 nl_list_del(&nh->rtnh_list);
919struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
921 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
922 return &route->rt_nexthops;
927int rtnl_route_get_nnexthops(
struct rtnl_route *route)
929 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
930 return route->rt_nr_nh;
935void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
936 void (*cb)(
struct rtnl_nexthop *,
void *),
939 struct rtnl_nexthop *nh;
941 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
942 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
948struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
950 struct rtnl_nexthop *nh;
952 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && n >= 0 &&
953 ((
unsigned)n) < r->rt_nr_nh) {
957 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
958 if (i == n)
return nh;
965void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
967 route->rt_ttl_propagate = ttl_prop;
968 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
971int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
975 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
976 return -NLE_MISSING_ATTR;
977 return route->rt_ttl_propagate;
980void rtnl_route_set_nhid(
struct rtnl_route *route, uint32_t nhid)
982 route->rt_nhid = nhid;
985 route->ce_mask |= ROUTE_ATTR_NHID;
987 route->ce_mask &= ~ROUTE_ATTR_NHID;
990uint32_t rtnl_route_get_nhid(
struct rtnl_route *route)
992 return route->rt_nhid;
1017 if (route->rt_type == RTN_LOCAL)
1018 return RT_SCOPE_HOST;
1020 if (route->rt_family == AF_MPLS)
1021 return RT_SCOPE_UNIVERSE;
1023 if (!nl_list_empty(&route->rt_nexthops)) {
1024 struct rtnl_nexthop *nh;
1030 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1031 if (nh->rtnh_gateway || nh->rtnh_via)
1032 return RT_SCOPE_UNIVERSE;
1036 return RT_SCOPE_LINK;
1041static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
1043 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
1046 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1049static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
1055 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1071 [
RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1074 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1076 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1077 [RTA_NH_ID] = { .type =
NLA_U32 },
1080static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1082 struct rtnexthop *rtnh =
nla_data(attr);
1086 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1087 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *nh = NULL;
1089 nh = rtnl_route_nh_alloc();
1093 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1094 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1095 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1097 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1098 struct nlattr *ntb[RTA_MAX + 1];
1100 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1102 rtnh->rtnh_len -
sizeof(*rtnh),
1107 if (ntb[RTA_GATEWAY]) {
1108 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1115 rtnl_route_nh_set_gateway(nh, addr);
1118 if (ntb[RTA_FLOW]) {
1122 rtnl_route_nh_set_realms(nh, realms);
1125 if (ntb[RTA_NEWDST]) {
1126 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1133 err = rtnl_route_nh_set_newdst(nh, addr);
1139 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1141 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1145 err = rtnl_route_nh_set_via(nh, addr);
1150 if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1151 err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1152 ntb[RTA_ENCAP_TYPE],
1159 rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh));
1160 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1161 rtnh = RTNH_NEXT(rtnh);
1167int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1169 _nl_auto_rtnl_route
struct rtnl_route *route = NULL;
1170 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *old_nh = NULL;
1171 _nl_auto_nl_addr
struct nl_addr *src = NULL;
1172 _nl_auto_nl_addr
struct nl_addr *dst = NULL;
1173 struct nlattr *tb[RTA_MAX + 1];
1178 route = rtnl_route_alloc();
1182 route->ce_msgtype = nlh->nlmsg_type;
1184 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1189 route->rt_family = family = rtm->rtm_family;
1190 route->rt_tos = rtm->rtm_tos;
1191 route->rt_table = rtm->rtm_table;
1192 route->rt_type = rtm->rtm_type;
1193 route->rt_scope = rtm->rtm_scope;
1194 route->rt_protocol = rtm->rtm_protocol;
1195 route->rt_flags = rtm->rtm_flags;
1198 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1199 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1200 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1206 if (family != AF_MPLS)
1207 route->ce_mask |= ROUTE_ATTR_PRIO;
1233 err = rtnl_route_set_dst(route, dst);
1240 }
else if (rtm->rtm_src_len)
1246 rtnl_route_set_src(route, src);
1250 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1253 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1255 if (tb[RTA_PRIORITY])
1256 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1258 if (tb[RTA_PREFSRC]) {
1259 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1263 rtnl_route_set_pref_src(route, addr);
1266 if (tb[RTA_METRICS]) {
1267 struct nlattr *mtb[RTAX_MAX + 1];
1274 for (i = 1; i <= RTAX_MAX; i++) {
1275 if (mtb[i] && _nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1278 err = rtnl_route_set_metric(route, i, m);
1285 if (tb[RTA_MULTIPATH]) {
1286 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1290 if (tb[RTA_CACHEINFO]) {
1291 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1292 sizeof(route->rt_cacheinfo));
1293 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1297 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1300 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1303 if (tb[RTA_GATEWAY]) {
1304 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1306 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1312 rtnl_route_nh_set_gateway(old_nh, addr);
1316 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1319 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1322 if (tb[RTA_NEWDST]) {
1323 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1325 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1332 err = rtnl_route_nh_set_newdst(old_nh, addr);
1338 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1339 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1340 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1342 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1345 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1349 err = rtnl_route_nh_set_via(old_nh, addr);
1354 if (tb[RTA_TTL_PROPAGATE]) {
1355 rtnl_route_set_ttl_propagate(route,
1359 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1360 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1363 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1364 tb[RTA_ENCAP_TYPE], old_nh);
1369 if (tb[RTA_NH_ID]) {
1370 rtnl_route_set_nhid(route,
nla_get_u32(tb[RTA_NH_ID]));
1374 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1375 if (route->rt_nr_nh == 0) {
1379 rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh));
1383 struct rtnl_nexthop *first;
1385 first = nl_list_first_entry(&route->rt_nexthops,
1386 struct rtnl_nexthop,
1391 if (rtnl_route_nh_compare(old_nh, first,
1392 old_nh->ce_mask, 0)) {
1398 *result = _nl_steal_pointer(&route);
1402int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1405 struct nlattr *metrics;
1406 struct rtmsg rtmsg = {
1407 .rtm_family = route->rt_family,
1408 .rtm_tos = route->rt_tos,
1409 .rtm_table = route->rt_table,
1410 .rtm_protocol = route->rt_protocol,
1411 .rtm_scope = route->rt_scope,
1412 .rtm_type = route->rt_type,
1413 .rtm_flags = route->rt_flags,
1416 if (route->rt_dst == NULL)
1417 return -NLE_MISSING_ATTR;
1423 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1426 if (rtnl_route_get_nnexthops(route) == 1) {
1427 struct rtnl_nexthop *nh;
1428 nh = rtnl_route_nexthop_n(route, 0);
1429 rtmsg.rtm_flags |= nh->rtnh_flags;
1432 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1433 goto nla_put_failure;
1439 if (route->rt_family != AF_MPLS)
1445 if (route->ce_mask & ROUTE_ATTR_PRIO)
1448 if (route->ce_mask & ROUTE_ATTR_SRC)
1451 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1454 if (route->ce_mask & ROUTE_ATTR_IIF)
1457 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1458 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1460 if (route->rt_nmetrics > 0) {
1464 if (metrics == NULL)
1465 goto nla_put_failure;
1467 for (i = 1; i <= RTAX_MAX; i++) {
1468 if (!rtnl_route_get_metric(route, i, &val))
1476 if (route->ce_mask & ROUTE_ATTR_NHID) {
1478 }
else if (rtnl_route_get_nnexthops(route) == 1) {
1479 struct rtnl_nexthop *nh;
1481 nh = rtnl_route_nexthop_n(route, 0);
1482 if (nh->rtnh_gateway)
1484 if (nh->rtnh_ifindex)
1486 if (nh->rtnh_realms)
1488 if (nh->rtnh_newdst)
1490 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1491 goto nla_put_failure;
1492 if (nh->rtnh_encap &&
1493 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1494 goto nla_put_failure;
1495 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1496 struct nlattr *multipath;
1497 struct rtnl_nexthop *nh;
1500 goto nla_put_failure;
1502 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1503 struct rtnexthop *rtnh;
1507 goto nla_put_failure;
1509 rtnh->rtnh_flags = nh->rtnh_flags;
1510 rtnh->rtnh_hops = nh->rtnh_weight;
1511 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1513 if (nh->rtnh_gateway)
1517 if (nh->rtnh_newdst)
1521 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1522 goto nla_put_failure;
1524 if (nh->rtnh_realms)
1527 if (nh->rtnh_encap &&
1528 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1529 goto nla_put_failure;
1531 rtnh->rtnh_len = (
char *) nlmsg_tail(msg->nm_nlh) -
1541 return -NLE_MSGSIZE;
1545struct nl_object_ops route_obj_ops = {
1546 .oo_name =
"route/route",
1547 .oo_size =
sizeof(
struct rtnl_route),
1548 .oo_constructor = route_constructor,
1549 .oo_free_data = route_free_data,
1550 .oo_clone = route_clone,
1556 .oo_compare = route_compare,
1557 .oo_keygen = route_keygen,
1558 .oo_update = route_update,
1559 .oo_attrs2str = route_attrs2str,
1560 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1561 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1563 .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.
int rtnl_route_nh_identical(struct rtnl_nexthop *a, struct rtnl_nexthop *b)
Check if the fixed attributes of two nexthops are identical, and may only differ in flags or weight.
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.