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"
58 uint8_t rt_ttl_propagate;
60 struct nl_addr *rt_dst;
61 struct nl_addr *rt_src;
65 uint32_t rt_metrics[RTAX_MAX];
66 uint32_t rt_metrics_mask;
68 struct nl_addr *rt_pref_src;
71 uint32_t rt_flag_mask;
74#define ROUTE_ATTR_FAMILY 0x000001
75#define ROUTE_ATTR_TOS 0x000002
76#define ROUTE_ATTR_TABLE 0x000004
77#define ROUTE_ATTR_PROTOCOL 0x000008
78#define ROUTE_ATTR_SCOPE 0x000010
79#define ROUTE_ATTR_TYPE 0x000020
80#define ROUTE_ATTR_FLAGS 0x000040
81#define ROUTE_ATTR_DST 0x000080
82#define ROUTE_ATTR_SRC 0x000100
83#define ROUTE_ATTR_IIF 0x000200
84#define ROUTE_ATTR_OIF 0x000400
85#define ROUTE_ATTR_GATEWAY 0x000800
86#define ROUTE_ATTR_PRIO 0x001000
87#define ROUTE_ATTR_PREF_SRC 0x002000
88#define ROUTE_ATTR_METRICS 0x004000
89#define ROUTE_ATTR_MULTIPATH 0x008000
90#define ROUTE_ATTR_REALMS 0x010000
91#define ROUTE_ATTR_CACHEINFO 0x020000
92#define ROUTE_ATTR_TTL_PROPAGATE 0x040000
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;
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;
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_MULTIPATH) {
200 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
201 p->
dp_ivar = NH_DUMP_FROM_ONELINE;
202 rtnl_route_nh_dump(nh, p);
206 flags = r->rt_flags & ~(RTM_F_CLONED);
207 if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
211#define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
212 flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
215 PRINT_FLAG(PERVASIVE);
218#define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
219 flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
221 PRINT_FLAG(EQUALIZE);
225#define PRINT_FLAG(f) if (flags & RTCF_##f) { \
226 flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
228 PRINT_FLAG(REDIRECTED);
229 PRINT_FLAG(DOREDIRECT);
230 PRINT_FLAG(DIRECTSRC);
232 PRINT_FLAG(BROADCAST);
233 PRINT_FLAG(MULTICAST);
243static void route_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
245 _nl_auto_nl_cache
struct nl_cache *link_cache = NULL;
246 struct rtnl_route *r = (
struct rtnl_route *) a;
252 route_dump_line(a, p);
253 nl_dump_line(p,
" ");
255 if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
256 nl_dump(p,
"preferred-src %s ",
259 if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
261 rtnl_scope2str(r->rt_scope, buf,
sizeof(buf)));
263 if (r->ce_mask & ROUTE_ATTR_PRIO)
264 nl_dump(p,
"priority %#x ", r->rt_prio);
266 if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
268 rtnl_route_proto2str(r->rt_protocol, buf,
sizeof(buf)));
270 if (r->ce_mask & ROUTE_ATTR_IIF) {
276 nl_dump(p,
"iif %d ", r->rt_iif);
279 if (r->ce_mask & ROUTE_ATTR_SRC)
282 if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) {
283 nl_dump(p,
" ttl-propagate %s",
284 r->rt_ttl_propagate ?
"enabled" :
"disabled");
289 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
292 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
293 nl_dump_line(p,
" ");
294 p->
dp_ivar = NH_DUMP_FROM_DETAILS;
295 rtnl_route_nh_dump(nh, p);
300 if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
301 nl_dump_line(p,
" cacheinfo error %d (%s)\n",
302 r->rt_cacheinfo.rtci_error,
303 nl_strerror_l(-r->rt_cacheinfo.rtci_error));
306 if (r->ce_mask & ROUTE_ATTR_METRICS) {
307 nl_dump_line(p,
" metrics [");
308 for (i = 0; i < RTAX_MAX; i++)
309 if (r->rt_metrics_mask & (1 << i))
311 rtnl_route_metric2str(i+1,
318static void route_dump_stats(
struct nl_object *obj,
struct nl_dump_params *p)
320 struct rtnl_route *route = (
struct rtnl_route *) obj;
322 route_dump_details(obj, p);
324 if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
327 nl_dump_line(p,
" used %u refcnt %u last-use %us "
329 ci->rtci_used, ci->rtci_clntref,
335static void route_keygen(
struct nl_object *obj, uint32_t *hashkey,
338 struct rtnl_route *route = (
struct rtnl_route *) obj;
339 unsigned int rkey_sz;
340 struct nl_addr *addr = NULL;
341 _nl_auto_free
struct route_hash_key {
347 } _nl_packed *rkey = NULL;
349 char buf[INET6_ADDRSTRLEN+5];
353 addr = route->rt_dst;
355 rkey_sz =
sizeof(*rkey);
358 rkey = calloc(1, rkey_sz);
360 NL_DBG(2,
"Warning: calloc failed for %d bytes...\n", rkey_sz);
364 rkey->rt_family = route->rt_family;
365 rkey->rt_tos = route->rt_tos;
366 rkey->rt_table = route->rt_table;
367 rkey->rt_prio = route->rt_prio;
372 *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
374 NL_DBG(5,
"route %p key (fam %d tos %d table %d addr %s) keysz %d "
375 "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
376 rkey->rt_table,
nl_addr2str(addr, buf,
sizeof(buf)),
382static uint32_t route_id_attrs_get(
struct nl_object *obj)
384 struct rtnl_route *route = (
struct rtnl_route *)obj;
385 struct nl_object_ops *ops = obj->ce_ops;
386 uint32_t rv = ops->oo_id_attrs;
389 if (route->rt_family == AF_MPLS)
390 rv &= ~ROUTE_ATTR_PRIO;
395static uint64_t route_compare(
struct nl_object *_a,
struct nl_object *_b,
396 uint64_t attrs,
int flags)
398 struct rtnl_route *a = (
struct rtnl_route *) _a;
399 struct rtnl_route *b = (
struct rtnl_route *) _b;
404#define _DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ATTR, a, b, EXPR)
405 diff |= _DIFF(ROUTE_ATTR_FAMILY, a->rt_family != b->rt_family);
406 diff |= _DIFF(ROUTE_ATTR_TOS, a->rt_tos != b->rt_tos);
407 diff |= _DIFF(ROUTE_ATTR_TABLE, a->rt_table != b->rt_table);
408 diff |= _DIFF(ROUTE_ATTR_PROTOCOL, a->rt_protocol != b->rt_protocol);
409 diff |= _DIFF(ROUTE_ATTR_SCOPE, a->rt_scope != b->rt_scope);
410 diff |= _DIFF(ROUTE_ATTR_TYPE, a->rt_type != b->rt_type);
411 diff |= _DIFF(ROUTE_ATTR_PRIO, a->rt_prio != b->rt_prio);
412 diff |= _DIFF(ROUTE_ATTR_DST,
nl_addr_cmp(a->rt_dst, b->rt_dst));
413 diff |= _DIFF(ROUTE_ATTR_SRC,
nl_addr_cmp(a->rt_src, b->rt_src));
414 diff |= _DIFF(ROUTE_ATTR_IIF, a->rt_iif != b->rt_iif);
415 diff |= _DIFF(ROUTE_ATTR_PREF_SRC,
417 diff |= _DIFF(ROUTE_ATTR_TTL_PROPAGATE,
418 a->rt_ttl_propagate != b->rt_ttl_propagate);
420 if (flags & LOOSE_COMPARISON) {
421 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
423 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
425 if (!rtnl_route_nh_compare(nh_a, nh_b,
436 for (i = 0; i < RTAX_MAX - 1; i++) {
437 if (a->rt_metrics_mask & (1 << i) &&
438 (!(b->rt_metrics_mask & (1 << i)) ||
439 a->rt_metrics[i] != b->rt_metrics[i]))
440 diff |= _DIFF(ROUTE_ATTR_METRICS, 1);
443 diff |= _DIFF(ROUTE_ATTR_FLAGS,
444 (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
446 if (a->rt_nr_nh != b->rt_nr_nh)
450 nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
452 nl_list_for_each_entry(nh_b, &b->rt_nexthops,
454 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
465 nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
467 nl_list_for_each_entry(nh_a, &a->rt_nexthops,
469 if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
478 for (i = 0; i < RTAX_MAX - 1; i++) {
479 if ((a->rt_metrics_mask & (1 << i)) ^
480 (b->rt_metrics_mask & (1 << i)))
481 diff |= _DIFF(ROUTE_ATTR_METRICS, 1);
483 diff |= _DIFF(ROUTE_ATTR_METRICS,
484 a->rt_metrics[i] != b->rt_metrics[i]);
487 diff |= _DIFF(ROUTE_ATTR_FLAGS, a->rt_flags != b->rt_flags);
494 diff |= _DIFF(ROUTE_ATTR_MULTIPATH, 1);
499static int route_update(
struct nl_object *old_obj,
struct nl_object *new_obj)
501 struct rtnl_route *new_route = (
struct rtnl_route *) new_obj;
502 struct rtnl_route *old_route = (
struct rtnl_route *) old_obj;
504 int action = new_obj->ce_msgtype;
506 char buf[INET6_ADDRSTRLEN+5];
516 if (new_route->rt_family != AF_INET6 ||
517 new_route->rt_table == RT_TABLE_LOCAL)
518 return -NLE_OPNOTSUPP;
524 if (rtnl_route_get_nnexthops(new_route) != 1)
525 return -NLE_OPNOTSUPP;
532 new_nh = rtnl_route_nexthop_n(new_route, 0);
533 if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
534 return -NLE_OPNOTSUPP;
537 case RTM_NEWROUTE : {
544 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, rtnh_list) {
545 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
553 cloned_nh = rtnl_route_nh_clone(new_nh);
556 rtnl_route_add_nexthop(old_route, cloned_nh);
558 NL_DBG(2,
"Route obj %p updated. Added "
559 "nexthop %p via %s\n", old_route, cloned_nh,
564 case RTM_DELROUTE : {
573 if (rtnl_route_get_nnexthops(old_route) <= 1)
574 return -NLE_OPNOTSUPP;
579 nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
581 if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
583 rtnl_route_remove_nexthop(old_route, old_nh);
585 NL_DBG(2,
"Route obj %p updated. Removed "
586 "nexthop %p via %s\n", old_route,
591 rtnl_route_nh_free(old_nh);
598 NL_DBG(2,
"Unknown action associated "
599 "to object %p during route update\n", new_obj);
600 return -NLE_OPNOTSUPP;
606static const struct trans_tbl route_attrs[] = {
607 __ADD(ROUTE_ATTR_FAMILY, family),
608 __ADD(ROUTE_ATTR_TOS, tos),
609 __ADD(ROUTE_ATTR_TABLE, table),
610 __ADD(ROUTE_ATTR_PROTOCOL, protocol),
611 __ADD(ROUTE_ATTR_SCOPE, scope),
612 __ADD(ROUTE_ATTR_TYPE, type),
613 __ADD(ROUTE_ATTR_FLAGS, flags),
614 __ADD(ROUTE_ATTR_DST, dst),
615 __ADD(ROUTE_ATTR_SRC, src),
616 __ADD(ROUTE_ATTR_IIF, iif),
617 __ADD(ROUTE_ATTR_OIF, oif),
618 __ADD(ROUTE_ATTR_GATEWAY, gateway),
619 __ADD(ROUTE_ATTR_PRIO, prio),
620 __ADD(ROUTE_ATTR_PREF_SRC, pref_src),
621 __ADD(ROUTE_ATTR_METRICS, metrics),
622 __ADD(ROUTE_ATTR_MULTIPATH, multipath),
623 __ADD(ROUTE_ATTR_REALMS, realms),
624 __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo),
625 __ADD(ROUTE_ATTR_TTL_PROPAGATE, ttl_propagate),
628static char *route_attrs2str(
int attrs,
char *buf,
size_t len)
630 return __flags2str(attrs, buf, len, route_attrs,
631 ARRAY_SIZE(route_attrs));
639struct rtnl_route *rtnl_route_alloc(
void)
644void rtnl_route_get(
struct rtnl_route *route)
649void rtnl_route_put(
struct rtnl_route *route)
661void rtnl_route_set_table(
struct rtnl_route *route, uint32_t table)
663 route->rt_table = table;
664 route->ce_mask |= ROUTE_ATTR_TABLE;
667uint32_t rtnl_route_get_table(
struct rtnl_route *route)
669 return route->rt_table;
672void rtnl_route_set_scope(
struct rtnl_route *route, uint8_t scope)
674 route->rt_scope = scope;
675 route->ce_mask |= ROUTE_ATTR_SCOPE;
678uint8_t rtnl_route_get_scope(
struct rtnl_route *route)
680 return route->rt_scope;
683void rtnl_route_set_tos(
struct rtnl_route *route, uint8_t tos)
686 route->ce_mask |= ROUTE_ATTR_TOS;
689uint8_t rtnl_route_get_tos(
struct rtnl_route *route)
691 return route->rt_tos;
694void rtnl_route_set_protocol(
struct rtnl_route *route, uint8_t protocol)
696 route->rt_protocol = protocol;
697 route->ce_mask |= ROUTE_ATTR_PROTOCOL;
700uint8_t rtnl_route_get_protocol(
struct rtnl_route *route)
702 return route->rt_protocol;
705void rtnl_route_set_priority(
struct rtnl_route *route, uint32_t prio)
707 route->rt_prio = prio;
708 route->ce_mask |= ROUTE_ATTR_PRIO;
711uint32_t rtnl_route_get_priority(
struct rtnl_route *route)
713 return route->rt_prio;
716int rtnl_route_set_family(
struct rtnl_route *route, uint8_t family)
723 route->rt_family = family;
724 route->ce_mask |= ROUTE_ATTR_FAMILY;
728 return -NLE_AF_NOSUPPORT;
731uint8_t rtnl_route_get_family(
struct rtnl_route *route)
733 return route->rt_family;
736int rtnl_route_set_dst(
struct rtnl_route *route,
struct nl_addr *addr)
738 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
739 if (addr->a_family != route->rt_family)
740 return -NLE_AF_MISMATCH;
742 route->rt_family = addr->a_family;
748 route->rt_dst = addr;
750 route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
755struct nl_addr *rtnl_route_get_dst(
struct rtnl_route *route)
757 return route->rt_dst;
760int rtnl_route_set_src(
struct rtnl_route *route,
struct nl_addr *addr)
762 if (addr->a_family == AF_INET)
763 return -NLE_SRCRT_NOSUPPORT;
765 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
766 if (addr->a_family != route->rt_family)
767 return -NLE_AF_MISMATCH;
769 route->rt_family = addr->a_family;
775 route->rt_src = addr;
776 route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
781struct nl_addr *rtnl_route_get_src(
struct rtnl_route *route)
783 return route->rt_src;
786int rtnl_route_set_type(
struct rtnl_route *route, uint8_t type)
791 route->rt_type = type;
792 route->ce_mask |= ROUTE_ATTR_TYPE;
797uint8_t rtnl_route_get_type(
struct rtnl_route *route)
799 return route->rt_type;
802void rtnl_route_set_flags(
struct rtnl_route *route, uint32_t flags)
804 route->rt_flag_mask |= flags;
805 route->rt_flags |= flags;
806 route->ce_mask |= ROUTE_ATTR_FLAGS;
809void rtnl_route_unset_flags(
struct rtnl_route *route, uint32_t flags)
811 route->rt_flag_mask |= flags;
812 route->rt_flags &= ~flags;
813 route->ce_mask |= ROUTE_ATTR_FLAGS;
816uint32_t rtnl_route_get_flags(
struct rtnl_route *route)
818 return route->rt_flags;
821int rtnl_route_set_metric(
struct rtnl_route *route,
int metric, uint32_t value)
823 if (metric > RTAX_MAX || metric < 1)
826 route->rt_metrics[metric - 1] = value;
828 if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
829 route->rt_nmetrics++;
830 route->rt_metrics_mask |= (1 << (metric - 1));
833 route->ce_mask |= ROUTE_ATTR_METRICS;
838int rtnl_route_unset_metric(
struct rtnl_route *route,
int metric)
840 if (metric > RTAX_MAX || metric < 1)
843 if (route->rt_metrics_mask & (1 << (metric - 1))) {
844 route->rt_nmetrics--;
845 route->rt_metrics_mask &= ~(1 << (metric - 1));
851int rtnl_route_get_metric(
struct rtnl_route *route,
int metric, uint32_t *value)
853 if (metric > RTAX_MAX || metric < 1)
856 if (!(route->rt_metrics_mask & (1 << (metric - 1))))
857 return -NLE_OBJ_NOTFOUND;
860 *value = route->rt_metrics[metric - 1];
865int rtnl_route_set_pref_src(
struct rtnl_route *route,
struct nl_addr *addr)
867 if (route->ce_mask & ROUTE_ATTR_FAMILY) {
868 if (addr->a_family != route->rt_family)
869 return -NLE_AF_MISMATCH;
871 route->rt_family = addr->a_family;
873 if (route->rt_pref_src)
877 route->rt_pref_src = addr;
878 route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
883struct nl_addr *rtnl_route_get_pref_src(
struct rtnl_route *route)
885 return route->rt_pref_src;
888void rtnl_route_set_iif(
struct rtnl_route *route,
int ifindex)
890 route->rt_iif = ifindex;
891 route->ce_mask |= ROUTE_ATTR_IIF;
894int rtnl_route_get_iif(
struct rtnl_route *route)
896 return route->rt_iif;
899void rtnl_route_add_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
901 nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
903 route->ce_mask |= ROUTE_ATTR_MULTIPATH;
906void rtnl_route_remove_nexthop(
struct rtnl_route *route,
struct rtnl_nexthop *nh)
908 if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
910 nl_list_del(&nh->rtnh_list);
914struct nl_list_head *rtnl_route_get_nexthops(
struct rtnl_route *route)
916 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
917 return &route->rt_nexthops;
922int rtnl_route_get_nnexthops(
struct rtnl_route *route)
924 if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
925 return route->rt_nr_nh;
930void rtnl_route_foreach_nexthop(
struct rtnl_route *r,
936 if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
937 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
943struct rtnl_nexthop *rtnl_route_nexthop_n(
struct rtnl_route *r,
int n)
948 if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
950 nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
951 if (i == n)
return nh;
958void rtnl_route_set_ttl_propagate(
struct rtnl_route *route, uint8_t ttl_prop)
960 route->rt_ttl_propagate = ttl_prop;
961 route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
964int rtnl_route_get_ttl_propagate(
struct rtnl_route *route)
968 if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
969 return -NLE_MISSING_ATTR;
970 return route->rt_ttl_propagate;
995 if (route->rt_type == RTN_LOCAL)
996 return RT_SCOPE_HOST;
998 if (route->rt_family == AF_MPLS)
999 return RT_SCOPE_UNIVERSE;
1001 if (!nl_list_empty(&route->rt_nexthops)) {
1008 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1009 if (nh->rtnh_gateway)
1010 return RT_SCOPE_UNIVERSE;
1014 return RT_SCOPE_LINK;
1019static struct nl_addr *rtnl_route_parse_via(
struct nlattr *nla)
1021 int alen =
nla_len(nla) - offsetof(
struct rtvia, rtvia_addr);
1024 return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1027static int rtnl_route_put_via(
struct nl_msg *msg,
struct nl_addr *addr)
1033 nla =
nla_reserve(msg, RTA_VIA, alen +
sizeof(*via));
1044static struct nla_policy route_policy[RTA_MAX+1] = {
1046 [RTA_OIF] = { .type =
NLA_U32 },
1047 [RTA_PRIORITY] = { .type =
NLA_U32 },
1048 [RTA_FLOW] = { .type =
NLA_U32 },
1049 [RTA_CACHEINFO] = { .minlen =
sizeof(
struct rta_cacheinfo) },
1052 [RTA_TTL_PROPAGATE] = { .type =
NLA_U8 },
1054 [RTA_ENCAP_TYPE] = { .type =
NLA_U16 },
1057static int parse_multipath(
struct rtnl_route *route,
struct nlattr *attr)
1059 struct rtnexthop *rtnh =
nla_data(attr);
1063 while (tlen >=
sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1066 nh = rtnl_route_nh_alloc();
1070 rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1071 rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1072 rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1074 if (rtnh->rtnh_len >
sizeof(*rtnh)) {
1075 struct nlattr *ntb[RTA_MAX + 1];
1077 err =
nla_parse(ntb, RTA_MAX, (
struct nlattr *)
1079 rtnh->rtnh_len -
sizeof(*rtnh),
1084 if (ntb[RTA_GATEWAY]) {
1085 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1092 rtnl_route_nh_set_gateway(nh, addr);
1095 if (ntb[RTA_FLOW]) {
1099 rtnl_route_nh_set_realms(nh, realms);
1102 if (ntb[RTA_NEWDST]) {
1103 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1110 err = rtnl_route_nh_set_newdst(nh, addr);
1116 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1118 addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1122 err = rtnl_route_nh_set_via(nh, addr);
1127 if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1128 err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1129 ntb[RTA_ENCAP_TYPE],
1136 rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh));
1137 tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1138 rtnh = RTNH_NEXT(rtnh);
1144int rtnl_route_parse(
struct nlmsghdr *nlh,
struct rtnl_route **result)
1146 _nl_auto_rtnl_route
struct rtnl_route *route = NULL;
1147 _nl_auto_rtnl_nexthop
struct rtnl_nexthop *old_nh = NULL;
1148 _nl_auto_nl_addr
struct nl_addr *src = NULL;
1149 _nl_auto_nl_addr
struct nl_addr *dst = NULL;
1150 struct nlattr *tb[RTA_MAX + 1];
1155 route = rtnl_route_alloc();
1159 route->ce_msgtype = nlh->nlmsg_type;
1161 err =
nlmsg_parse(nlh,
sizeof(
struct rtmsg), tb, RTA_MAX, route_policy);
1166 route->rt_family = family = rtm->rtm_family;
1167 route->rt_tos = rtm->rtm_tos;
1168 route->rt_table = rtm->rtm_table;
1169 route->rt_type = rtm->rtm_type;
1170 route->rt_scope = rtm->rtm_scope;
1171 route->rt_protocol = rtm->rtm_protocol;
1172 route->rt_flags = rtm->rtm_flags;
1175 route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1176 ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1177 ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1183 if (family != AF_MPLS)
1184 route->ce_mask |= ROUTE_ATTR_PRIO;
1210 err = rtnl_route_set_dst(route, dst);
1217 }
else if (rtm->rtm_src_len)
1223 rtnl_route_set_src(route, src);
1227 rtnl_route_set_table(route,
nla_get_u32(tb[RTA_TABLE]));
1230 rtnl_route_set_iif(route,
nla_get_u32(tb[RTA_IIF]));
1232 if (tb[RTA_PRIORITY])
1233 rtnl_route_set_priority(route,
nla_get_u32(tb[RTA_PRIORITY]));
1235 if (tb[RTA_PREFSRC]) {
1236 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1240 rtnl_route_set_pref_src(route, addr);
1243 if (tb[RTA_METRICS]) {
1244 struct nlattr *mtb[RTAX_MAX + 1];
1251 for (i = 1; i <= RTAX_MAX; i++) {
1252 if (mtb[i] &&
nla_len(mtb[i]) >=
sizeof(uint32_t)) {
1255 err = rtnl_route_set_metric(route, i, m);
1262 if (tb[RTA_MULTIPATH]) {
1263 if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1267 if (tb[RTA_CACHEINFO]) {
1268 nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1269 sizeof(route->rt_cacheinfo));
1270 route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1274 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1277 rtnl_route_nh_set_ifindex(old_nh,
nla_get_u32(tb[RTA_OIF]));
1280 if (tb[RTA_GATEWAY]) {
1281 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1283 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1289 rtnl_route_nh_set_gateway(old_nh, addr);
1293 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1296 rtnl_route_nh_set_realms(old_nh,
nla_get_u32(tb[RTA_FLOW]));
1299 if (tb[RTA_NEWDST]) {
1300 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1302 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1309 err = rtnl_route_nh_set_newdst(old_nh, addr);
1315 int alen =
nla_len(tb[RTA_VIA]) - offsetof(
struct rtvia, rtvia_addr);
1316 _nl_auto_nl_addr
struct nl_addr *addr = NULL;
1317 struct rtvia *via =
nla_data(tb[RTA_VIA]);
1319 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1322 addr =
nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1326 err = rtnl_route_nh_set_via(old_nh, addr);
1331 if (tb[RTA_TTL_PROPAGATE]) {
1332 rtnl_route_set_ttl_propagate(route,
1336 if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1337 if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1340 err = nh_encap_parse_msg(tb[RTA_ENCAP],
1341 tb[RTA_ENCAP_TYPE], old_nh);
1347 rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1348 if (route->rt_nr_nh == 0) {
1352 rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh));
1358 first = nl_list_first_entry(&route->rt_nexthops,
1364 if (rtnl_route_nh_compare(old_nh, first,
1365 old_nh->ce_mask, 0)) {
1371 *result = _nl_steal_pointer(&route);
1375int rtnl_route_build_msg(
struct nl_msg *msg,
struct rtnl_route *route)
1378 struct nlattr *metrics;
1379 struct rtmsg rtmsg = {
1380 .rtm_family = route->rt_family,
1381 .rtm_tos = route->rt_tos,
1382 .rtm_table = route->rt_table,
1383 .rtm_protocol = route->rt_protocol,
1384 .rtm_scope = route->rt_scope,
1385 .rtm_type = route->rt_type,
1386 .rtm_flags = route->rt_flags,
1389 if (route->rt_dst == NULL)
1390 return -NLE_MISSING_ATTR;
1396 if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1399 if (rtnl_route_get_nnexthops(route) == 1) {
1401 nh = rtnl_route_nexthop_n(route, 0);
1402 rtmsg.rtm_flags |= nh->rtnh_flags;
1405 if (
nlmsg_append(msg, &rtmsg,
sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1406 goto nla_put_failure;
1412 if (route->rt_family != AF_MPLS)
1418 if (route->ce_mask & ROUTE_ATTR_PRIO)
1421 if (route->ce_mask & ROUTE_ATTR_SRC)
1424 if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1427 if (route->ce_mask & ROUTE_ATTR_IIF)
1430 if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1431 NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1433 if (route->rt_nmetrics > 0) {
1437 if (metrics == NULL)
1438 goto nla_put_failure;
1440 for (i = 1; i <= RTAX_MAX; i++) {
1441 if (!rtnl_route_get_metric(route, i, &val))
1448 if (rtnl_route_get_nnexthops(route) == 1) {
1451 nh = rtnl_route_nexthop_n(route, 0);
1452 if (nh->rtnh_gateway)
1454 if (nh->rtnh_ifindex)
1456 if (nh->rtnh_realms)
1458 if (nh->rtnh_newdst)
1460 if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1461 goto nla_put_failure;
1462 if (nh->rtnh_encap &&
1463 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1464 goto nla_put_failure;
1465 }
else if (rtnl_route_get_nnexthops(route) > 1) {
1466 struct nlattr *multipath;
1470 goto nla_put_failure;
1472 nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1473 struct rtnexthop *rtnh;
1477 goto nla_put_failure;
1479 rtnh->rtnh_flags = nh->rtnh_flags;
1480 rtnh->rtnh_hops = nh->rtnh_weight;
1481 rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1483 if (nh->rtnh_gateway)
1487 if (nh->rtnh_newdst)
1491 rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1492 goto nla_put_failure;
1494 if (nh->rtnh_realms)
1497 if (nh->rtnh_encap &&
1498 nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1499 goto nla_put_failure;
1501 rtnh->rtnh_len = (
char *) nlmsg_tail(msg->nm_nlh) -
1511 return -NLE_MSGSIZE;
1515struct nl_object_ops route_obj_ops = {
1516 .oo_name =
"route/route",
1517 .oo_size =
sizeof(
struct rtnl_route),
1518 .oo_constructor = route_constructor,
1519 .oo_free_data = route_free_data,
1520 .oo_clone = route_clone,
1526 .oo_compare = route_compare,
1527 .oo_keygen = route_keygen,
1528 .oo_update = route_update,
1529 .oo_attrs2str = route_attrs2str,
1530 .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1531 ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1533 .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.