| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2008 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @ingroup route |
| 8 |
* @defgroup route_obj Route Object |
| 9 |
* |
| 10 |
* @par Attributes |
| 11 |
* @code |
| 12 |
* Name Default |
| 13 |
* ------------------------------------------------------------- |
| 14 |
* routing table RT_TABLE_MAIN |
| 15 |
* scope RT_SCOPE_NOWHERE |
| 16 |
* tos 0 |
| 17 |
* protocol RTPROT_STATIC |
| 18 |
* prio 0 |
| 19 |
* family AF_UNSPEC |
| 20 |
* type RTN_UNICAST |
| 21 |
* iif NULL |
| 22 |
* @endcode |
| 23 |
* |
| 24 |
* @{ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "nl-default.h" |
| 28 |
|
| 29 |
#include <linux/in_route.h> |
| 30 |
|
| 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> |
| 40 |
|
| 41 |
#include "nl-route.h" |
| 42 |
#include "nl-aux-route/nl-route.h" |
| 43 |
#include "nl-priv-dynamic-core/nl-core.h" |
| 44 |
#include "nexthop-encap.h" |
| 45 |
|
| 46 |
/** @cond SKIP */ |
| 47 |
struct rtnl_route { |
| 48 |
NLHDR_COMMON |
| 49 |
|
| 50 |
uint8_t rt_family; |
| 51 |
uint8_t rt_tos; |
| 52 |
uint8_t rt_protocol; |
| 53 |
uint8_t rt_scope; |
| 54 |
uint8_t rt_type; |
| 55 |
uint8_t rt_nmetrics; |
| 56 |
uint8_t rt_ttl_propagate; |
| 57 |
uint32_t rt_flags; |
| 58 |
struct nl_addr *rt_dst; |
| 59 |
struct nl_addr *rt_src; |
| 60 |
uint32_t rt_table; |
| 61 |
uint32_t rt_iif; |
| 62 |
uint32_t rt_prio; |
| 63 |
uint32_t rt_metrics[RTAX_MAX]; |
| 64 |
uint32_t rt_metrics_mask; |
| 65 |
uint32_t rt_nr_nh; |
| 66 |
uint32_t rt_nhid; |
| 67 |
struct nl_addr *rt_pref_src; |
| 68 |
struct nl_list_head rt_nexthops; |
| 69 |
struct rtnl_rtcacheinfo rt_cacheinfo; |
| 70 |
uint32_t rt_flag_mask; |
| 71 |
}; |
| 72 |
|
| 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 |
| 93 |
/** @endcond */ |
| 94 |
|
| 95 |
static void route_constructor(struct nl_object *c) |
| 96 |
{ |
| 97 |
struct rtnl_route *r = (struct rtnl_route *) c; |
| 98 |
|
| 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; |
| 104 |
r->rt_prio = 0; |
| 105 |
|
| 106 |
nl_init_list_head(&r->rt_nexthops); |
| 107 |
} |
| 108 |
|
| 109 |
static void route_free_data(struct nl_object *c) |
| 110 |
{ |
| 111 |
struct rtnl_route *r = (struct rtnl_route *) c; |
| 112 |
struct rtnl_nexthop *nh, *tmp; |
| 113 |
|
| 114 |
if (r == NULL) |
| 115 |
return; |
| 116 |
|
| 117 |
nl_addr_put(r->rt_dst); |
| 118 |
nl_addr_put(r->rt_src); |
| 119 |
nl_addr_put(r->rt_pref_src); |
| 120 |
|
| 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); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
static int route_clone(struct nl_object *_dst, struct nl_object *_src) |
| 128 |
{ |
| 129 |
struct rtnl_route *dst = (struct rtnl_route *) _dst; |
| 130 |
struct rtnl_route *src = (struct rtnl_route *) _src; |
| 131 |
struct rtnl_nexthop *nh, *new; |
| 132 |
|
| 133 |
dst->rt_dst = NULL; |
| 134 |
dst->rt_src = NULL; |
| 135 |
dst->rt_pref_src = NULL; |
| 136 |
nl_init_list_head(&dst->rt_nexthops); |
| 137 |
dst->rt_nr_nh = 0; |
| 138 |
|
| 139 |
if (src->rt_dst) { |
| 140 |
if (!(dst->rt_dst = nl_addr_clone(src->rt_dst))) |
| 141 |
return -NLE_NOMEM; |
| 142 |
} |
| 143 |
|
| 144 |
if (src->rt_src) { |
| 145 |
if (!(dst->rt_src = nl_addr_clone(src->rt_src))) |
| 146 |
return -NLE_NOMEM; |
| 147 |
} |
| 148 |
|
| 149 |
if (src->rt_pref_src) { |
| 150 |
if (!(dst->rt_pref_src = nl_addr_clone(src->rt_pref_src))) |
| 151 |
return -NLE_NOMEM; |
| 152 |
} |
| 153 |
|
| 154 |
nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) { |
| 155 |
new = rtnl_route_nh_clone(nh); |
| 156 |
if (!new) |
| 157 |
return -NLE_NOMEM; |
| 158 |
|
| 159 |
rtnl_route_add_nexthop(dst, new); |
| 160 |
} |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static void route_dump_line(struct nl_object *a, struct nl_dump_params *p) |
| 166 |
{ |
| 167 |
struct rtnl_route *r = (struct rtnl_route *) a; |
| 168 |
int cache = 0, flags; |
| 169 |
char buf[64]; |
| 170 |
|
| 171 |
if (r->rt_flags & RTM_F_CLONED) |
| 172 |
cache = 1; |
| 173 |
|
| 174 |
nl_dump_line(p, "%s ", nl_af2str(r->rt_family, buf, sizeof(buf))); |
| 175 |
|
| 176 |
if (cache) |
| 177 |
nl_dump(p, "cache "); |
| 178 |
|
| 179 |
if (!(r->ce_mask & ROUTE_ATTR_DST) || |
| 180 |
(nl_addr_get_prefixlen(r->rt_dst) == 0 && |
| 181 |
nl_addr_get_len(r->rt_dst) > 0 && nl_addr_iszero(r->rt_dst))) |
| 182 |
nl_dump(p, "default "); |
| 183 |
else |
| 184 |
nl_dump(p, "%s ", nl_addr2str(r->rt_dst, buf, sizeof(buf))); |
| 185 |
|
| 186 |
if (r->ce_mask & ROUTE_ATTR_TABLE && !cache) |
| 187 |
nl_dump(p, "table %s ", |
| 188 |
rtnl_route_table2str(r->rt_table, buf, sizeof(buf))); |
| 189 |
|
| 190 |
if (r->ce_mask & ROUTE_ATTR_TYPE) |
| 191 |
nl_dump(p, "type %s ", |
| 192 |
nl_rtntype2str(r->rt_type, buf, sizeof(buf))); |
| 193 |
|
| 194 |
if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0) |
| 195 |
nl_dump(p, "tos %#x ", r->rt_tos); |
| 196 |
|
| 197 |
if (r->ce_mask & ROUTE_ATTR_NHID) |
| 198 |
nl_dump(p, "nhid %u ", r->rt_nhid); |
| 199 |
|
| 200 |
if (r->ce_mask & ROUTE_ATTR_MULTIPATH) { |
| 201 |
struct rtnl_nexthop *nh; |
| 202 |
|
| 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); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
flags = r->rt_flags & ~(RTM_F_CLONED); |
| 210 |
if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) { |
| 211 |
|
| 212 |
nl_dump(p, "<"); |
| 213 |
|
| 214 |
#define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \ |
| 215 |
flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } |
| 216 |
PRINT_FLAG(DEAD); |
| 217 |
PRINT_FLAG(ONLINK); |
| 218 |
PRINT_FLAG(PERVASIVE); |
| 219 |
#undef PRINT_FLAG |
| 220 |
|
| 221 |
#define PRINT_FLAG(f) if (flags & RTM_F_##f) { \ |
| 222 |
flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); } |
| 223 |
PRINT_FLAG(NOTIFY); |
| 224 |
PRINT_FLAG(EQUALIZE); |
| 225 |
PRINT_FLAG(PREFIX); |
| 226 |
#undef PRINT_FLAG |
| 227 |
|
| 228 |
#define PRINT_FLAG(f) if (flags & RTCF_##f) { \ |
| 229 |
flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); } |
| 230 |
PRINT_FLAG(NOTIFY); |
| 231 |
PRINT_FLAG(REDIRECTED); |
| 232 |
PRINT_FLAG(DOREDIRECT); |
| 233 |
PRINT_FLAG(DIRECTSRC); |
| 234 |
PRINT_FLAG(DNAT); |
| 235 |
PRINT_FLAG(BROADCAST); |
| 236 |
PRINT_FLAG(MULTICAST); |
| 237 |
PRINT_FLAG(LOCAL); |
| 238 |
#undef PRINT_FLAG |
| 239 |
|
| 240 |
nl_dump(p, ">"); |
| 241 |
} |
| 242 |
|
| 243 |
nl_dump(p, "\n"); |
| 244 |
} |
| 245 |
|
| 246 |
static void route_dump_details(struct nl_object *a, struct nl_dump_params *p) |
| 247 |
{ |
| 248 |
_nl_auto_nl_cache struct nl_cache *link_cache = NULL; |
| 249 |
struct rtnl_route *r = (struct rtnl_route *) a; |
| 250 |
char buf[256]; |
| 251 |
int i; |
| 252 |
|
| 253 |
link_cache = nl_cache_mngt_require_safe("route/link"); |
| 254 |
|
| 255 |
route_dump_line(a, p); |
| 256 |
nl_dump_line(p, " "); |
| 257 |
|
| 258 |
if (r->ce_mask & ROUTE_ATTR_PREF_SRC) |
| 259 |
nl_dump(p, "preferred-src %s ", |
| 260 |
nl_addr2str(r->rt_pref_src, buf, sizeof(buf))); |
| 261 |
|
| 262 |
if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE) |
| 263 |
nl_dump(p, "scope %s ", |
| 264 |
rtnl_scope2str(r->rt_scope, buf, sizeof(buf))); |
| 265 |
|
| 266 |
if (r->ce_mask & ROUTE_ATTR_PRIO) |
| 267 |
nl_dump(p, "priority %#x ", r->rt_prio); |
| 268 |
|
| 269 |
if (r->ce_mask & ROUTE_ATTR_PROTOCOL) |
| 270 |
nl_dump(p, "protocol %s ", |
| 271 |
rtnl_route_proto2str(r->rt_protocol, buf, sizeof(buf))); |
| 272 |
|
| 273 |
if (r->ce_mask & ROUTE_ATTR_IIF) { |
| 274 |
if (link_cache) { |
| 275 |
nl_dump(p, "iif %s ", |
| 276 |
rtnl_link_i2name(link_cache, r->rt_iif, |
| 277 |
buf, sizeof(buf))); |
| 278 |
} else |
| 279 |
nl_dump(p, "iif %d ", r->rt_iif); |
| 280 |
} |
| 281 |
|
| 282 |
if (r->ce_mask & ROUTE_ATTR_SRC) |
| 283 |
nl_dump(p, "src %s ", nl_addr2str(r->rt_src, buf, sizeof(buf))); |
| 284 |
|
| 285 |
if (r->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) { |
| 286 |
nl_dump(p, " ttl-propagate %s", |
| 287 |
r->rt_ttl_propagate ? "enabled" : "disabled"); |
| 288 |
} |
| 289 |
|
| 290 |
if (r->ce_mask & ROUTE_ATTR_NHID) |
| 291 |
nl_dump(p, "nhid %u ", r->rt_nhid); |
| 292 |
|
| 293 |
nl_dump(p, "\n"); |
| 294 |
|
| 295 |
if (r->ce_mask & ROUTE_ATTR_MULTIPATH) { |
| 296 |
struct rtnl_nexthop *nh; |
| 297 |
|
| 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); |
| 302 |
nl_dump(p, "\n"); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 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)); |
| 310 |
} |
| 311 |
|
| 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)) |
| 316 |
nl_dump(p, "%s %u ", |
| 317 |
rtnl_route_metric2str(i+1, |
| 318 |
buf, sizeof(buf)), |
| 319 |
r->rt_metrics[i]); |
| 320 |
nl_dump(p, "]\n"); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
static void route_dump_stats(struct nl_object *obj, struct nl_dump_params *p) |
| 325 |
{ |
| 326 |
struct rtnl_route *route = (struct rtnl_route *) obj; |
| 327 |
|
| 328 |
route_dump_details(obj, p); |
| 329 |
|
| 330 |
if (route->ce_mask & ROUTE_ATTR_CACHEINFO) { |
| 331 |
struct rtnl_rtcacheinfo *ci = &route->rt_cacheinfo; |
| 332 |
|
| 333 |
nl_dump_line(p, " used %u refcnt %u last-use %us " |
| 334 |
"expires %us\n", |
| 335 |
ci->rtci_used, ci->rtci_clntref, |
| 336 |
ci->rtci_last_use / nl_get_user_hz(), |
| 337 |
ci->rtci_expires / nl_get_user_hz()); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
static void route_keygen(struct nl_object *obj, uint32_t *hashkey, |
| 342 |
uint32_t table_sz) |
| 343 |
{ |
| 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 { |
| 348 |
uint8_t rt_family; |
| 349 |
uint8_t rt_tos; |
| 350 |
uint32_t rt_table; |
| 351 |
uint32_t rt_prio; |
| 352 |
char rt_addr[0]; |
| 353 |
} _nl_packed *rkey = NULL; |
| 354 |
char buf[INET6_ADDRSTRLEN+5]; |
| 355 |
|
| 356 |
if (route->rt_dst) |
| 357 |
addr = route->rt_dst; |
| 358 |
|
| 359 |
rkey_sz = sizeof(*rkey); |
| 360 |
if (addr) |
| 361 |
rkey_sz += nl_addr_get_len(addr); |
| 362 |
rkey = calloc(1, rkey_sz); |
| 363 |
if (!rkey) { |
| 364 |
NL_DBG(2, "Warning: calloc failed for %d bytes...\n", rkey_sz); |
| 365 |
*hashkey = 0; |
| 366 |
return; |
| 367 |
} |
| 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; |
| 372 |
if (addr) |
| 373 |
memcpy(rkey->rt_addr, nl_addr_get_binary_addr(addr), |
| 374 |
nl_addr_get_len(addr)); |
| 375 |
|
| 376 |
*hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz; |
| 377 |
|
| 378 |
NL_DBG(5, |
| 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, |
| 382 |
*hashkey); |
| 383 |
|
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
static uint32_t route_id_attrs_get(struct nl_object *obj) |
| 388 |
{ |
| 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; |
| 392 |
|
| 393 |
/* MPLS address family does not allow RTA_PRIORITY to be set */ |
| 394 |
if (route->rt_family == AF_MPLS) |
| 395 |
rv &= ~ROUTE_ATTR_PRIO; |
| 396 |
|
| 397 |
return rv; |
| 398 |
} |
| 399 |
|
| 400 |
static uint64_t route_compare(struct nl_object *_a, struct nl_object *_b, |
| 401 |
uint64_t attrs, int flags) |
| 402 |
{ |
| 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; |
| 406 |
int i, found; |
| 407 |
uint64_t diff = 0; |
| 408 |
|
| 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, |
| 421 |
nl_addr_cmp(a->rt_pref_src, b->rt_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); |
| 425 |
|
| 426 |
if (flags & LOOSE_COMPARISON) { |
| 427 |
nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) { |
| 428 |
found = 0; |
| 429 |
nl_list_for_each_entry(nh_a, &a->rt_nexthops, |
| 430 |
rtnh_list) { |
| 431 |
if (!rtnl_route_nh_compare(nh_a, nh_b, |
| 432 |
nh_b->ce_mask, 1)) { |
| 433 |
found = 1; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (!found) |
| 439 |
goto nh_mismatch; |
| 440 |
} |
| 441 |
|
| 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); |
| 447 |
} |
| 448 |
|
| 449 |
diff |= _DIFF(ROUTE_ATTR_FLAGS, |
| 450 |
(a->rt_flags ^ b->rt_flags) & b->rt_flag_mask); |
| 451 |
} else { |
| 452 |
if (a->rt_nr_nh != b->rt_nr_nh) |
| 453 |
goto nh_mismatch; |
| 454 |
|
| 455 |
/* search for a dup in each nh of a */ |
| 456 |
nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) { |
| 457 |
found = 0; |
| 458 |
nl_list_for_each_entry(nh_b, &b->rt_nexthops, |
| 459 |
rtnh_list) { |
| 460 |
if (rtnl_route_nh_identical(nh_a, nh_b)) { |
| 461 |
found = 1; |
| 462 |
break; |
| 463 |
} |
| 464 |
} |
| 465 |
if (!found) |
| 466 |
goto nh_mismatch; |
| 467 |
} |
| 468 |
|
| 469 |
/* search for a dup in each nh of b, covers case where a has |
| 470 |
* dupes itself */ |
| 471 |
nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) { |
| 472 |
found = 0; |
| 473 |
nl_list_for_each_entry(nh_a, &a->rt_nexthops, |
| 474 |
rtnh_list) { |
| 475 |
if (rtnl_route_nh_identical(nh_a, nh_b)) { |
| 476 |
found = 1; |
| 477 |
break; |
| 478 |
} |
| 479 |
} |
| 480 |
if (!found) |
| 481 |
goto nh_mismatch; |
| 482 |
} |
| 483 |
|
| 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); |
| 488 |
else |
| 489 |
diff |= _DIFF(ROUTE_ATTR_METRICS, |
| 490 |
a->rt_metrics[i] != b->rt_metrics[i]); |
| 491 |
} |
| 492 |
|
| 493 |
diff |= _DIFF(ROUTE_ATTR_FLAGS, a->rt_flags != b->rt_flags); |
| 494 |
} |
| 495 |
|
| 496 |
out: |
| 497 |
return diff; |
| 498 |
|
| 499 |
nh_mismatch: |
| 500 |
diff |= _DIFF(ROUTE_ATTR_MULTIPATH, 1); |
| 501 |
goto out; |
| 502 |
#undef _DIFF |
| 503 |
} |
| 504 |
|
| 505 |
static int route_update(struct nl_object *old_obj, struct nl_object *new_obj) |
| 506 |
{ |
| 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]; |
| 512 |
|
| 513 |
/* |
| 514 |
* ipv6 ECMP route notifications from the kernel come as |
| 515 |
* separate notifications, one for every nexthop. This update |
| 516 |
* function collapses such route msgs into a single |
| 517 |
* route with multiple nexthops. The resulting object looks |
| 518 |
* similar to a ipv4 ECMP route |
| 519 |
*/ |
| 520 |
if (new_route->rt_family != AF_INET6 || |
| 521 |
new_route->rt_table == RT_TABLE_LOCAL) |
| 522 |
return -NLE_OPNOTSUPP; |
| 523 |
|
| 524 |
/* |
| 525 |
* For routes that are already multipath, |
| 526 |
* or dont have a nexthop dont do anything |
| 527 |
*/ |
| 528 |
if (rtnl_route_get_nnexthops(new_route) != 1) |
| 529 |
return -NLE_OPNOTSUPP; |
| 530 |
|
| 531 |
/* |
| 532 |
* Get the only nexthop entry from the new route. For |
| 533 |
* IPv6 we always get a route with a 0th NH |
| 534 |
* filled or nothing at all |
| 535 |
*/ |
| 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; |
| 539 |
|
| 540 |
switch(action) { |
| 541 |
case RTM_NEWROUTE : { |
| 542 |
struct rtnl_nexthop *cloned_nh; |
| 543 |
struct rtnl_nexthop *old_nh; |
| 544 |
|
| 545 |
/* |
| 546 |
* Do not add the nexthop to old route if it was already added before |
| 547 |
*/ |
| 548 |
nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, rtnh_list) { |
| 549 |
if (rtnl_route_nh_identical(old_nh, new_nh)) { |
| 550 |
return 0; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
* Add the nexthop to old route |
| 556 |
*/ |
| 557 |
cloned_nh = rtnl_route_nh_clone(new_nh); |
| 558 |
if (!cloned_nh) |
| 559 |
return -NLE_NOMEM; |
| 560 |
rtnl_route_add_nexthop(old_route, cloned_nh); |
| 561 |
|
| 562 |
NL_DBG(2, "Route obj %p updated. Added " |
| 563 |
"nexthop %p via %s\n", old_route, cloned_nh, |
| 564 |
nl_addr2str(cloned_nh->rtnh_gateway, buf, |
| 565 |
sizeof(buf))); |
| 566 |
} |
| 567 |
break; |
| 568 |
case RTM_DELROUTE : { |
| 569 |
struct rtnl_nexthop *old_nh; |
| 570 |
|
| 571 |
/* |
| 572 |
* Only take care of nexthop deletes and not |
| 573 |
* route deletes. So, if there is only one nexthop |
| 574 |
* quite likely we did not update it. So dont do |
| 575 |
* anything and return |
| 576 |
*/ |
| 577 |
if (rtnl_route_get_nnexthops(old_route) <= 1) |
| 578 |
return -NLE_OPNOTSUPP; |
| 579 |
|
| 580 |
/* |
| 581 |
* Find the next hop in old route and delete it |
| 582 |
*/ |
| 583 |
nl_list_for_each_entry(old_nh, &old_route->rt_nexthops, |
| 584 |
rtnh_list) { |
| 585 |
if (rtnl_route_nh_identical(old_nh, new_nh)) { |
| 586 |
|
| 587 |
rtnl_route_remove_nexthop(old_route, old_nh); |
| 588 |
|
| 589 |
NL_DBG(2, "Route obj %p updated. Removed " |
| 590 |
"nexthop %p via %s\n", old_route, |
| 591 |
old_nh, |
| 592 |
nl_addr2str(old_nh->rtnh_gateway, buf, |
| 593 |
sizeof(buf))); |
| 594 |
|
| 595 |
rtnl_route_nh_free(old_nh); |
| 596 |
break; |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
break; |
| 601 |
default: |
| 602 |
NL_DBG(2, "Unknown action associated " |
| 603 |
"to object %p during route update\n", new_obj); |
| 604 |
return -NLE_OPNOTSUPP; |
| 605 |
} |
| 606 |
|
| 607 |
return NLE_SUCCESS; |
| 608 |
} |
| 609 |
|
| 610 |
static 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), |
| 631 |
}; |
| 632 |
|
| 633 |
static char *route_attrs2str(int attrs, char *buf, size_t len) |
| 634 |
{ |
| 635 |
return __flags2str(attrs, buf, len, route_attrs, |
| 636 |
ARRAY_SIZE(route_attrs)); |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* @name Allocation/Freeing |
| 641 |
* @{ |
| 642 |
*/ |
| 643 |
|
| 644 |
struct rtnl_route *rtnl_route_alloc(void) |
| 645 |
{ |
| 646 |
return (struct rtnl_route *) nl_object_alloc(&route_obj_ops); |
| 647 |
} |
| 648 |
|
| 649 |
void rtnl_route_get(struct rtnl_route *route) |
| 650 |
{ |
| 651 |
nl_object_get((struct nl_object *) route); |
| 652 |
} |
| 653 |
|
| 654 |
void rtnl_route_put(struct rtnl_route *route) |
| 655 |
{ |
| 656 |
nl_object_put((struct nl_object *) route); |
| 657 |
} |
| 658 |
|
| 659 |
/** @} */ |
| 660 |
|
| 661 |
/** |
| 662 |
* @name Attributes |
| 663 |
* @{ |
| 664 |
*/ |
| 665 |
|
| 666 |
void rtnl_route_set_table(struct rtnl_route *route, uint32_t table) |
| 667 |
{ |
| 668 |
route->rt_table = table; |
| 669 |
route->ce_mask |= ROUTE_ATTR_TABLE; |
| 670 |
} |
| 671 |
|
| 672 |
uint32_t rtnl_route_get_table(struct rtnl_route *route) |
| 673 |
{ |
| 674 |
return route->rt_table; |
| 675 |
} |
| 676 |
|
| 677 |
void rtnl_route_set_scope(struct rtnl_route *route, uint8_t scope) |
| 678 |
{ |
| 679 |
route->rt_scope = scope; |
| 680 |
route->ce_mask |= ROUTE_ATTR_SCOPE; |
| 681 |
} |
| 682 |
|
| 683 |
uint8_t rtnl_route_get_scope(struct rtnl_route *route) |
| 684 |
{ |
| 685 |
return route->rt_scope; |
| 686 |
} |
| 687 |
|
| 688 |
void rtnl_route_set_tos(struct rtnl_route *route, uint8_t tos) |
| 689 |
{ |
| 690 |
route->rt_tos = tos; |
| 691 |
route->ce_mask |= ROUTE_ATTR_TOS; |
| 692 |
} |
| 693 |
|
| 694 |
uint8_t rtnl_route_get_tos(struct rtnl_route *route) |
| 695 |
{ |
| 696 |
return route->rt_tos; |
| 697 |
} |
| 698 |
|
| 699 |
void rtnl_route_set_protocol(struct rtnl_route *route, uint8_t protocol) |
| 700 |
{ |
| 701 |
route->rt_protocol = protocol; |
| 702 |
route->ce_mask |= ROUTE_ATTR_PROTOCOL; |
| 703 |
} |
| 704 |
|
| 705 |
uint8_t rtnl_route_get_protocol(struct rtnl_route *route) |
| 706 |
{ |
| 707 |
return route->rt_protocol; |
| 708 |
} |
| 709 |
|
| 710 |
void rtnl_route_set_priority(struct rtnl_route *route, uint32_t prio) |
| 711 |
{ |
| 712 |
route->rt_prio = prio; |
| 713 |
route->ce_mask |= ROUTE_ATTR_PRIO; |
| 714 |
} |
| 715 |
|
| 716 |
uint32_t rtnl_route_get_priority(struct rtnl_route *route) |
| 717 |
{ |
| 718 |
return route->rt_prio; |
| 719 |
} |
| 720 |
|
| 721 |
int rtnl_route_set_family(struct rtnl_route *route, uint8_t family) |
| 722 |
{ |
| 723 |
switch(family) { |
| 724 |
case AF_INET: |
| 725 |
case AF_INET6: |
| 726 |
case AF_DECnet: |
| 727 |
case AF_MPLS: |
| 728 |
route->rt_family = family; |
| 729 |
route->ce_mask |= ROUTE_ATTR_FAMILY; |
| 730 |
return 0; |
| 731 |
} |
| 732 |
|
| 733 |
return -NLE_AF_NOSUPPORT; |
| 734 |
} |
| 735 |
|
| 736 |
uint8_t rtnl_route_get_family(struct rtnl_route *route) |
| 737 |
{ |
| 738 |
return route->rt_family; |
| 739 |
} |
| 740 |
|
| 741 |
int rtnl_route_set_dst(struct rtnl_route *route, struct nl_addr *addr) |
| 742 |
{ |
| 743 |
if (route->ce_mask & ROUTE_ATTR_FAMILY) { |
| 744 |
if (addr->a_family != route->rt_family) |
| 745 |
return -NLE_AF_MISMATCH; |
| 746 |
} else |
| 747 |
route->rt_family = addr->a_family; |
| 748 |
|
| 749 |
if (route->rt_dst) |
| 750 |
nl_addr_put(route->rt_dst); |
| 751 |
|
| 752 |
nl_addr_get(addr); |
| 753 |
route->rt_dst = addr; |
| 754 |
|
| 755 |
route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY); |
| 756 |
|
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route) |
| 761 |
{ |
| 762 |
return route->rt_dst; |
| 763 |
} |
| 764 |
|
| 765 |
int rtnl_route_set_src(struct rtnl_route *route, struct nl_addr *addr) |
| 766 |
{ |
| 767 |
if (addr->a_family == AF_INET) |
| 768 |
return -NLE_SRCRT_NOSUPPORT; |
| 769 |
|
| 770 |
if (route->ce_mask & ROUTE_ATTR_FAMILY) { |
| 771 |
if (addr->a_family != route->rt_family) |
| 772 |
return -NLE_AF_MISMATCH; |
| 773 |
} else |
| 774 |
route->rt_family = addr->a_family; |
| 775 |
|
| 776 |
if (route->rt_src) |
| 777 |
nl_addr_put(route->rt_src); |
| 778 |
|
| 779 |
nl_addr_get(addr); |
| 780 |
route->rt_src = addr; |
| 781 |
route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY); |
| 782 |
|
| 783 |
return 0; |
| 784 |
} |
| 785 |
|
| 786 |
struct nl_addr *rtnl_route_get_src(struct rtnl_route *route) |
| 787 |
{ |
| 788 |
return route->rt_src; |
| 789 |
} |
| 790 |
|
| 791 |
int rtnl_route_set_type(struct rtnl_route *route, uint8_t type) |
| 792 |
{ |
| 793 |
if (type > RTN_MAX) |
| 794 |
return -NLE_RANGE; |
| 795 |
|
| 796 |
route->rt_type = type; |
| 797 |
route->ce_mask |= ROUTE_ATTR_TYPE; |
| 798 |
|
| 799 |
return 0; |
| 800 |
} |
| 801 |
|
| 802 |
uint8_t rtnl_route_get_type(struct rtnl_route *route) |
| 803 |
{ |
| 804 |
return route->rt_type; |
| 805 |
} |
| 806 |
|
| 807 |
void rtnl_route_set_flags(struct rtnl_route *route, uint32_t flags) |
| 808 |
{ |
| 809 |
route->rt_flag_mask |= flags; |
| 810 |
route->rt_flags |= flags; |
| 811 |
route->ce_mask |= ROUTE_ATTR_FLAGS; |
| 812 |
} |
| 813 |
|
| 814 |
void rtnl_route_unset_flags(struct rtnl_route *route, uint32_t flags) |
| 815 |
{ |
| 816 |
route->rt_flag_mask |= flags; |
| 817 |
route->rt_flags &= ~flags; |
| 818 |
route->ce_mask |= ROUTE_ATTR_FLAGS; |
| 819 |
} |
| 820 |
|
| 821 |
uint32_t rtnl_route_get_flags(struct rtnl_route *route) |
| 822 |
{ |
| 823 |
return route->rt_flags; |
| 824 |
} |
| 825 |
|
| 826 |
int rtnl_route_set_metric(struct rtnl_route *route, int metric, uint32_t value) |
| 827 |
{ |
| 828 |
if (metric > RTAX_MAX || metric < 1) |
| 829 |
return -NLE_RANGE; |
| 830 |
|
| 831 |
route->rt_metrics[metric - 1] = value; |
| 832 |
|
| 833 |
if (!(route->rt_metrics_mask & (1 << (metric - 1)))) { |
| 834 |
route->rt_nmetrics++; |
| 835 |
route->rt_metrics_mask |= (1 << (metric - 1)); |
| 836 |
} |
| 837 |
|
| 838 |
route->ce_mask |= ROUTE_ATTR_METRICS; |
| 839 |
|
| 840 |
return 0; |
| 841 |
} |
| 842 |
|
| 843 |
int rtnl_route_unset_metric(struct rtnl_route *route, int metric) |
| 844 |
{ |
| 845 |
if (metric > RTAX_MAX || metric < 1) |
| 846 |
return -NLE_RANGE; |
| 847 |
|
| 848 |
if (route->rt_metrics_mask & (1 << (metric - 1))) { |
| 849 |
route->rt_nmetrics--; |
| 850 |
route->rt_metrics_mask &= ~(1 << (metric - 1)); |
| 851 |
} |
| 852 |
|
| 853 |
return 0; |
| 854 |
} |
| 855 |
|
| 856 |
int rtnl_route_get_metric(struct rtnl_route *route, int metric, uint32_t *value) |
| 857 |
{ |
| 858 |
if (metric > RTAX_MAX || metric < 1) |
| 859 |
return -NLE_RANGE; |
| 860 |
|
| 861 |
if (!(route->rt_metrics_mask & (1 << (metric - 1)))) |
| 862 |
return -NLE_OBJ_NOTFOUND; |
| 863 |
|
| 864 |
if (value) |
| 865 |
*value = route->rt_metrics[metric - 1]; |
| 866 |
|
| 867 |
return 0; |
| 868 |
} |
| 869 |
|
| 870 |
int rtnl_route_set_pref_src(struct rtnl_route *route, struct nl_addr *addr) |
| 871 |
{ |
| 872 |
if (route->ce_mask & ROUTE_ATTR_FAMILY) { |
| 873 |
if (addr->a_family != route->rt_family) |
| 874 |
return -NLE_AF_MISMATCH; |
| 875 |
} else |
| 876 |
route->rt_family = addr->a_family; |
| 877 |
|
| 878 |
if (route->rt_pref_src) |
| 879 |
nl_addr_put(route->rt_pref_src); |
| 880 |
|
| 881 |
nl_addr_get(addr); |
| 882 |
route->rt_pref_src = addr; |
| 883 |
route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY); |
| 884 |
|
| 885 |
return 0; |
| 886 |
} |
| 887 |
|
| 888 |
struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route) |
| 889 |
{ |
| 890 |
return route->rt_pref_src; |
| 891 |
} |
| 892 |
|
| 893 |
void rtnl_route_set_iif(struct rtnl_route *route, int ifindex) |
| 894 |
{ |
| 895 |
route->rt_iif = ifindex; |
| 896 |
route->ce_mask |= ROUTE_ATTR_IIF; |
| 897 |
} |
| 898 |
|
| 899 |
int rtnl_route_get_iif(struct rtnl_route *route) |
| 900 |
{ |
| 901 |
return route->rt_iif; |
| 902 |
} |
| 903 |
|
| 904 |
void rtnl_route_add_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh) |
| 905 |
{ |
| 906 |
nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops); |
| 907 |
route->rt_nr_nh++; |
| 908 |
route->ce_mask |= ROUTE_ATTR_MULTIPATH; |
| 909 |
} |
| 910 |
|
| 911 |
void rtnl_route_remove_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh) |
| 912 |
{ |
| 913 |
if (route->ce_mask & ROUTE_ATTR_MULTIPATH) { |
| 914 |
route->rt_nr_nh--; |
| 915 |
nl_list_del(&nh->rtnh_list); |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
struct nl_list_head *rtnl_route_get_nexthops(struct rtnl_route *route) |
| 920 |
{ |
| 921 |
if (route->ce_mask & ROUTE_ATTR_MULTIPATH) |
| 922 |
return &route->rt_nexthops; |
| 923 |
|
| 924 |
return NULL; |
| 925 |
} |
| 926 |
|
| 927 |
int rtnl_route_get_nnexthops(struct rtnl_route *route) |
| 928 |
{ |
| 929 |
if (route->ce_mask & ROUTE_ATTR_MULTIPATH) |
| 930 |
return route->rt_nr_nh; |
| 931 |
|
| 932 |
return 0; |
| 933 |
} |
| 934 |
|
| 935 |
void rtnl_route_foreach_nexthop(struct rtnl_route *r, |
| 936 |
void (*cb)(struct rtnl_nexthop *, void *), |
| 937 |
void *arg) |
| 938 |
{ |
| 939 |
struct rtnl_nexthop *nh; |
| 940 |
|
| 941 |
if (r->ce_mask & ROUTE_ATTR_MULTIPATH) { |
| 942 |
nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) { |
| 943 |
cb(nh, arg); |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
struct rtnl_nexthop *rtnl_route_nexthop_n(struct rtnl_route *r, int n) |
| 949 |
{ |
| 950 |
struct rtnl_nexthop *nh; |
| 951 |
|
| 952 |
if (r->ce_mask & ROUTE_ATTR_MULTIPATH && n >= 0 && |
| 953 |
((unsigned)n) < r->rt_nr_nh) { |
| 954 |
int i; |
| 955 |
|
| 956 |
i = 0; |
| 957 |
nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) { |
| 958 |
if (i == n) |
| 959 |
return nh; |
| 960 |
i++; |
| 961 |
} |
| 962 |
} |
| 963 |
return NULL; |
| 964 |
} |
| 965 |
|
| 966 |
void rtnl_route_set_ttl_propagate(struct rtnl_route *route, uint8_t ttl_prop) |
| 967 |
{ |
| 968 |
route->rt_ttl_propagate = ttl_prop; |
| 969 |
route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE; |
| 970 |
} |
| 971 |
|
| 972 |
int rtnl_route_get_ttl_propagate(struct rtnl_route *route) |
| 973 |
{ |
| 974 |
if (!route) |
| 975 |
return -NLE_INVAL; |
| 976 |
if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)) |
| 977 |
return -NLE_MISSING_ATTR; |
| 978 |
return route->rt_ttl_propagate; |
| 979 |
} |
| 980 |
|
| 981 |
void rtnl_route_set_nhid(struct rtnl_route *route, uint32_t nhid) |
| 982 |
{ |
| 983 |
route->rt_nhid = nhid; |
| 984 |
|
| 985 |
if (nhid > 0) |
| 986 |
route->ce_mask |= ROUTE_ATTR_NHID; |
| 987 |
else |
| 988 |
route->ce_mask &= ~ROUTE_ATTR_NHID; |
| 989 |
} |
| 990 |
|
| 991 |
uint32_t rtnl_route_get_nhid(struct rtnl_route *route) |
| 992 |
{ |
| 993 |
return route->rt_nhid; |
| 994 |
} |
| 995 |
|
| 996 |
/** @} */ |
| 997 |
|
| 998 |
/** |
| 999 |
* @name Utilities |
| 1000 |
* @{ |
| 1001 |
*/ |
| 1002 |
|
| 1003 |
/** |
| 1004 |
* Guess scope of a route object. |
| 1005 |
* @arg route Route object. |
| 1006 |
* |
| 1007 |
* Guesses the scope of a route object, based on the following rules: |
| 1008 |
* @code |
| 1009 |
* 1) Local route -> local scope |
| 1010 |
* 2) At least one nexthop not directly connected -> universe scope |
| 1011 |
* 3) All others -> link scope |
| 1012 |
* @endcode |
| 1013 |
* |
| 1014 |
* @return Scope value. |
| 1015 |
*/ |
| 1016 |
int rtnl_route_guess_scope(struct rtnl_route *route) |
| 1017 |
{ |
| 1018 |
if (route->rt_type == RTN_LOCAL) |
| 1019 |
return RT_SCOPE_HOST; |
| 1020 |
|
| 1021 |
if (route->rt_family == AF_MPLS) |
| 1022 |
return RT_SCOPE_UNIVERSE; |
| 1023 |
|
| 1024 |
if (!nl_list_empty(&route->rt_nexthops)) { |
| 1025 |
struct rtnl_nexthop *nh; |
| 1026 |
|
| 1027 |
/* |
| 1028 |
* Use scope uiniverse if there is at least one nexthop which |
| 1029 |
* is not directly connected |
| 1030 |
*/ |
| 1031 |
nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) { |
| 1032 |
if (nh->rtnh_gateway || nh->rtnh_via) |
| 1033 |
return RT_SCOPE_UNIVERSE; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
return RT_SCOPE_LINK; |
| 1038 |
} |
| 1039 |
|
| 1040 |
/** @} */ |
| 1041 |
|
| 1042 |
static struct nl_addr *rtnl_route_parse_via(struct nlattr *nla) |
| 1043 |
{ |
| 1044 |
int alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr); |
| 1045 |
struct rtvia *via = nla_data(nla); |
| 1046 |
|
| 1047 |
return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen); |
| 1048 |
} |
| 1049 |
|
| 1050 |
static int rtnl_route_put_via(struct nl_msg *msg, struct nl_addr *addr) |
| 1051 |
{ |
| 1052 |
unsigned int alen = nl_addr_get_len(addr); |
| 1053 |
struct nlattr *nla; |
| 1054 |
struct rtvia *via; |
| 1055 |
|
| 1056 |
nla = nla_reserve(msg, RTA_VIA, alen + sizeof(*via)); |
| 1057 |
if (!nla) |
| 1058 |
return -EMSGSIZE; |
| 1059 |
|
| 1060 |
via = nla_data(nla); |
| 1061 |
via->rtvia_family = nl_addr_get_family(addr); |
| 1062 |
memcpy(via->rtvia_addr, nl_addr_get_binary_addr(addr), alen); |
| 1063 |
|
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
|
| 1067 |
static struct nla_policy route_policy[RTA_MAX+1] = { |
| 1068 |
[RTA_IIF] = { .type = NLA_U32 }, |
| 1069 |
[RTA_OIF] = { .type = NLA_U32 }, |
| 1070 |
[RTA_PRIORITY] = { .type = NLA_U32 }, |
| 1071 |
[RTA_FLOW] = { .type = NLA_U32 }, |
| 1072 |
[RTA_CACHEINFO] = { .minlen = sizeof(struct rta_cacheinfo) }, |
| 1073 |
[RTA_METRICS] = { .type = NLA_NESTED }, |
| 1074 |
[RTA_MULTIPATH] = { .type = NLA_NESTED }, |
| 1075 |
[RTA_TTL_PROPAGATE] = { .type = NLA_U8 }, |
| 1076 |
[RTA_ENCAP] = { .type = NLA_NESTED }, |
| 1077 |
[RTA_ENCAP_TYPE] = { .type = NLA_U16 }, |
| 1078 |
[RTA_NH_ID] = { .type = NLA_U32 }, |
| 1079 |
}; |
| 1080 |
|
| 1081 |
static int parse_multipath(struct rtnl_route *route, struct nlattr *attr) |
| 1082 |
{ |
| 1083 |
struct rtnexthop *rtnh = nla_data(attr); |
| 1084 |
size_t tlen = nla_len(attr); |
| 1085 |
int err; |
| 1086 |
|
| 1087 |
while (tlen >= sizeof(*rtnh) && tlen >= rtnh->rtnh_len) { |
| 1088 |
_nl_auto_rtnl_nexthop struct rtnl_nexthop *nh = NULL; |
| 1089 |
|
| 1090 |
nh = rtnl_route_nh_alloc(); |
| 1091 |
if (!nh) |
| 1092 |
return -NLE_NOMEM; |
| 1093 |
|
| 1094 |
rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops); |
| 1095 |
rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex); |
| 1096 |
rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags); |
| 1097 |
|
| 1098 |
if (rtnh->rtnh_len > sizeof(*rtnh)) { |
| 1099 |
struct nlattr *ntb[RTA_MAX + 1]; |
| 1100 |
|
| 1101 |
err = nla_parse(ntb, RTA_MAX, (struct nlattr *) |
| 1102 |
RTNH_DATA(rtnh), |
| 1103 |
rtnh->rtnh_len - sizeof(*rtnh), |
| 1104 |
route_policy); |
| 1105 |
if (err < 0) |
| 1106 |
return err; |
| 1107 |
|
| 1108 |
if (ntb[RTA_GATEWAY]) { |
| 1109 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1110 |
|
| 1111 |
addr = nl_addr_alloc_attr(ntb[RTA_GATEWAY], |
| 1112 |
route->rt_family); |
| 1113 |
if (!addr) |
| 1114 |
return -NLE_NOMEM; |
| 1115 |
|
| 1116 |
rtnl_route_nh_set_gateway(nh, addr); |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (ntb[RTA_FLOW]) { |
| 1120 |
uint32_t realms; |
| 1121 |
|
| 1122 |
realms = nla_get_u32(ntb[RTA_FLOW]); |
| 1123 |
rtnl_route_nh_set_realms(nh, realms); |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (ntb[RTA_NEWDST]) { |
| 1127 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1128 |
|
| 1129 |
addr = nl_addr_alloc_attr(ntb[RTA_NEWDST], |
| 1130 |
route->rt_family); |
| 1131 |
if (!addr) |
| 1132 |
return -NLE_NOMEM; |
| 1133 |
|
| 1134 |
err = rtnl_route_nh_set_newdst(nh, addr); |
| 1135 |
if (err < 0) |
| 1136 |
return err; |
| 1137 |
} |
| 1138 |
|
| 1139 |
if (ntb[RTA_VIA]) { |
| 1140 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1141 |
|
| 1142 |
addr = rtnl_route_parse_via(ntb[RTA_VIA]); |
| 1143 |
if (!addr) |
| 1144 |
return -NLE_NOMEM; |
| 1145 |
|
| 1146 |
err = rtnl_route_nh_set_via(nh, addr); |
| 1147 |
if (err < 0) |
| 1148 |
return err; |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) { |
| 1152 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap |
| 1153 |
*encap = NULL; |
| 1154 |
|
| 1155 |
err = nh_encap_parse_msg(ntb[RTA_ENCAP], |
| 1156 |
ntb[RTA_ENCAP_TYPE], |
| 1157 |
&encap); |
| 1158 |
if (err < 0) |
| 1159 |
return err; |
| 1160 |
|
| 1161 |
err = rtnl_route_nh_set_encap( |
| 1162 |
nh, _nl_steal_pointer(&encap)); |
| 1163 |
if (err < 0) |
| 1164 |
return err; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh)); |
| 1169 |
tlen -= RTNH_ALIGN(rtnh->rtnh_len); |
| 1170 |
rtnh = RTNH_NEXT(rtnh); |
| 1171 |
} |
| 1172 |
|
| 1173 |
return 0; |
| 1174 |
} |
| 1175 |
|
| 1176 |
int rtnl_route_parse(struct nlmsghdr *nlh, struct rtnl_route **result) |
| 1177 |
{ |
| 1178 |
_nl_auto_rtnl_route struct rtnl_route *route = NULL; |
| 1179 |
_nl_auto_rtnl_nexthop struct rtnl_nexthop *old_nh = NULL; |
| 1180 |
_nl_auto_nl_addr struct nl_addr *src = NULL; |
| 1181 |
_nl_auto_nl_addr struct nl_addr *dst = NULL; |
| 1182 |
struct nlattr *tb[RTA_MAX + 1]; |
| 1183 |
struct rtmsg *rtm; |
| 1184 |
int family; |
| 1185 |
int err; |
| 1186 |
|
| 1187 |
route = rtnl_route_alloc(); |
| 1188 |
if (!route) |
| 1189 |
return -NLE_NOMEM; |
| 1190 |
|
| 1191 |
route->ce_msgtype = nlh->nlmsg_type; |
| 1192 |
|
| 1193 |
err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, route_policy); |
| 1194 |
if (err < 0) |
| 1195 |
return err; |
| 1196 |
|
| 1197 |
rtm = nlmsg_data(nlh); |
| 1198 |
route->rt_family = family = rtm->rtm_family; |
| 1199 |
route->rt_tos = rtm->rtm_tos; |
| 1200 |
route->rt_table = rtm->rtm_table; |
| 1201 |
route->rt_type = rtm->rtm_type; |
| 1202 |
route->rt_scope = rtm->rtm_scope; |
| 1203 |
route->rt_protocol = rtm->rtm_protocol; |
| 1204 |
route->rt_flags = rtm->rtm_flags; |
| 1205 |
route->rt_prio = 0; |
| 1206 |
|
| 1207 |
route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS | |
| 1208 |
ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE | |
| 1209 |
ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL | |
| 1210 |
ROUTE_ATTR_FLAGS; |
| 1211 |
|
| 1212 |
/* right now MPLS does not allow rt_prio to be set, so don't |
| 1213 |
* assume it is unless it comes from an attribute |
| 1214 |
*/ |
| 1215 |
if (family != AF_MPLS) |
| 1216 |
route->ce_mask |= ROUTE_ATTR_PRIO; |
| 1217 |
|
| 1218 |
if (tb[RTA_DST]) { |
| 1219 |
if (!(dst = nl_addr_alloc_attr(tb[RTA_DST], family))) |
| 1220 |
return -NLE_NOMEM; |
| 1221 |
} else { |
| 1222 |
int len; |
| 1223 |
|
| 1224 |
switch (family) { |
| 1225 |
case AF_INET: |
| 1226 |
len = 4; |
| 1227 |
break; |
| 1228 |
|
| 1229 |
case AF_INET6: |
| 1230 |
len = 16; |
| 1231 |
break; |
| 1232 |
default: |
| 1233 |
len = 0; |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (!(dst = nl_addr_build(family, NULL, len))) |
| 1238 |
return -NLE_NOMEM; |
| 1239 |
} |
| 1240 |
|
| 1241 |
nl_addr_set_prefixlen(dst, rtm->rtm_dst_len); |
| 1242 |
err = rtnl_route_set_dst(route, dst); |
| 1243 |
if (err < 0) |
| 1244 |
return err; |
| 1245 |
|
| 1246 |
if (tb[RTA_SRC]) { |
| 1247 |
if (!(src = nl_addr_alloc_attr(tb[RTA_SRC], family))) |
| 1248 |
return -NLE_NOMEM; |
| 1249 |
} else if (rtm->rtm_src_len) |
| 1250 |
if (!(src = nl_addr_alloc(0))) |
| 1251 |
return -NLE_NOMEM; |
| 1252 |
|
| 1253 |
if (src) { |
| 1254 |
nl_addr_set_prefixlen(src, rtm->rtm_src_len); |
| 1255 |
rtnl_route_set_src(route, src); |
| 1256 |
} |
| 1257 |
|
| 1258 |
if (tb[RTA_TABLE]) |
| 1259 |
rtnl_route_set_table(route, nla_get_u32(tb[RTA_TABLE])); |
| 1260 |
|
| 1261 |
if (tb[RTA_IIF]) |
| 1262 |
rtnl_route_set_iif(route, nla_get_u32(tb[RTA_IIF])); |
| 1263 |
|
| 1264 |
if (tb[RTA_PRIORITY]) |
| 1265 |
rtnl_route_set_priority(route, nla_get_u32(tb[RTA_PRIORITY])); |
| 1266 |
|
| 1267 |
if (tb[RTA_PREFSRC]) { |
| 1268 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1269 |
|
| 1270 |
if (!(addr = nl_addr_alloc_attr(tb[RTA_PREFSRC], family))) |
| 1271 |
return -NLE_NOMEM; |
| 1272 |
rtnl_route_set_pref_src(route, addr); |
| 1273 |
} |
| 1274 |
|
| 1275 |
if (tb[RTA_METRICS]) { |
| 1276 |
struct nlattr *mtb[RTAX_MAX + 1]; |
| 1277 |
int i; |
| 1278 |
|
| 1279 |
err = nla_parse_nested(mtb, RTAX_MAX, tb[RTA_METRICS], NULL); |
| 1280 |
if (err < 0) |
| 1281 |
return err; |
| 1282 |
|
| 1283 |
for (i = 1; i <= RTAX_MAX; i++) { |
| 1284 |
if (mtb[i] && _nla_len(mtb[i]) >= sizeof(uint32_t)) { |
| 1285 |
uint32_t m = nla_get_u32(mtb[i]); |
| 1286 |
|
| 1287 |
err = rtnl_route_set_metric(route, i, m); |
| 1288 |
if (err < 0) |
| 1289 |
return err; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (tb[RTA_MULTIPATH]) { |
| 1295 |
if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0) |
| 1296 |
return err; |
| 1297 |
} |
| 1298 |
|
| 1299 |
if (tb[RTA_CACHEINFO]) { |
| 1300 |
nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO], |
| 1301 |
sizeof(route->rt_cacheinfo)); |
| 1302 |
route->ce_mask |= ROUTE_ATTR_CACHEINFO; |
| 1303 |
} |
| 1304 |
|
| 1305 |
if (tb[RTA_OIF]) { |
| 1306 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1307 |
return -NLE_NOMEM; |
| 1308 |
|
| 1309 |
rtnl_route_nh_set_ifindex(old_nh, nla_get_u32(tb[RTA_OIF])); |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (tb[RTA_GATEWAY]) { |
| 1313 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1314 |
|
| 1315 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1316 |
return -NLE_NOMEM; |
| 1317 |
|
| 1318 |
if (!(addr = nl_addr_alloc_attr(tb[RTA_GATEWAY], family))) |
| 1319 |
return -NLE_NOMEM; |
| 1320 |
|
| 1321 |
rtnl_route_nh_set_gateway(old_nh, addr); |
| 1322 |
} |
| 1323 |
|
| 1324 |
if (tb[RTA_FLOW]) { |
| 1325 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1326 |
return -NLE_NOMEM; |
| 1327 |
|
| 1328 |
rtnl_route_nh_set_realms(old_nh, nla_get_u32(tb[RTA_FLOW])); |
| 1329 |
} |
| 1330 |
|
| 1331 |
if (tb[RTA_NEWDST]) { |
| 1332 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1333 |
|
| 1334 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1335 |
return -NLE_NOMEM; |
| 1336 |
|
| 1337 |
addr = nl_addr_alloc_attr(tb[RTA_NEWDST], route->rt_family); |
| 1338 |
if (!addr) |
| 1339 |
return -NLE_NOMEM; |
| 1340 |
|
| 1341 |
err = rtnl_route_nh_set_newdst(old_nh, addr); |
| 1342 |
if (err < 0) |
| 1343 |
return err; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if (tb[RTA_VIA]) { |
| 1347 |
int alen = nla_len(tb[RTA_VIA]) - offsetof(struct rtvia, rtvia_addr); |
| 1348 |
_nl_auto_nl_addr struct nl_addr *addr = NULL; |
| 1349 |
struct rtvia *via = nla_data(tb[RTA_VIA]); |
| 1350 |
|
| 1351 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1352 |
return -NLE_NOMEM; |
| 1353 |
|
| 1354 |
addr = nl_addr_build(via->rtvia_family, via->rtvia_addr, alen); |
| 1355 |
if (!addr) |
| 1356 |
return -NLE_NOMEM; |
| 1357 |
|
| 1358 |
err = rtnl_route_nh_set_via(old_nh, addr); |
| 1359 |
if (err < 0) |
| 1360 |
return err; |
| 1361 |
} |
| 1362 |
|
| 1363 |
if (tb[RTA_TTL_PROPAGATE]) { |
| 1364 |
rtnl_route_set_ttl_propagate(route, |
| 1365 |
nla_get_u8(tb[RTA_TTL_PROPAGATE])); |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) { |
| 1369 |
_nl_auto_rtnl_nh_encap struct rtnl_nh_encap *encap = NULL; |
| 1370 |
|
| 1371 |
if (!old_nh && !(old_nh = rtnl_route_nh_alloc())) |
| 1372 |
return -NLE_NOMEM; |
| 1373 |
|
| 1374 |
err = nh_encap_parse_msg(tb[RTA_ENCAP], tb[RTA_ENCAP_TYPE], |
| 1375 |
&encap); |
| 1376 |
if (err < 0) |
| 1377 |
return err; |
| 1378 |
|
| 1379 |
err = rtnl_route_nh_set_encap(old_nh, |
| 1380 |
_nl_steal_pointer(&encap)); |
| 1381 |
if (err < 0) |
| 1382 |
return err; |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (tb[RTA_NH_ID]) { |
| 1386 |
rtnl_route_set_nhid(route, nla_get_u32(tb[RTA_NH_ID])); |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (old_nh) { |
| 1390 |
rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff); |
| 1391 |
if (route->rt_nr_nh == 0) { |
| 1392 |
/* If no nexthops have been provided via RTA_MULTIPATH |
| 1393 |
* we add it as regular nexthop to maintain backwards |
| 1394 |
* compatibility */ |
| 1395 |
rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh)); |
| 1396 |
} else { |
| 1397 |
/* Kernel supports new style nexthop configuration, |
| 1398 |
* verify that it is a duplicate and discard nexthop. */ |
| 1399 |
struct rtnl_nexthop *first; |
| 1400 |
|
| 1401 |
first = nl_list_first_entry(&route->rt_nexthops, |
| 1402 |
struct rtnl_nexthop, |
| 1403 |
rtnh_list); |
| 1404 |
if (!first) |
| 1405 |
BUG(); |
| 1406 |
|
| 1407 |
if (rtnl_route_nh_compare(old_nh, first, |
| 1408 |
old_nh->ce_mask, 0)) { |
| 1409 |
return -NLE_INVAL; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
*result = _nl_steal_pointer(&route); |
| 1415 |
return 0; |
| 1416 |
} |
| 1417 |
|
| 1418 |
int rtnl_route_build_msg(struct nl_msg *msg, struct rtnl_route *route) |
| 1419 |
{ |
| 1420 |
int i; |
| 1421 |
struct nlattr *metrics; |
| 1422 |
struct rtmsg rtmsg = { |
| 1423 |
.rtm_family = route->rt_family, |
| 1424 |
.rtm_tos = route->rt_tos, |
| 1425 |
.rtm_table = route->rt_table, |
| 1426 |
.rtm_protocol = route->rt_protocol, |
| 1427 |
.rtm_scope = route->rt_scope, |
| 1428 |
.rtm_type = route->rt_type, |
| 1429 |
.rtm_flags = route->rt_flags, |
| 1430 |
}; |
| 1431 |
|
| 1432 |
if (route->rt_dst == NULL) |
| 1433 |
return -NLE_MISSING_ATTR; |
| 1434 |
|
| 1435 |
rtmsg.rtm_dst_len = nl_addr_get_prefixlen(route->rt_dst); |
| 1436 |
if (route->rt_src) |
| 1437 |
rtmsg.rtm_src_len = nl_addr_get_prefixlen(route->rt_src); |
| 1438 |
|
| 1439 |
if (!(route->ce_mask & ROUTE_ATTR_SCOPE)) |
| 1440 |
rtmsg.rtm_scope = rtnl_route_guess_scope(route); |
| 1441 |
|
| 1442 |
if (rtnl_route_get_nnexthops(route) == 1) { |
| 1443 |
struct rtnl_nexthop *nh; |
| 1444 |
nh = rtnl_route_nexthop_n(route, 0); |
| 1445 |
rtmsg.rtm_flags |= nh->rtnh_flags; |
| 1446 |
} |
| 1447 |
|
| 1448 |
if (nlmsg_append(msg, &rtmsg, sizeof(rtmsg), NLMSG_ALIGNTO) < 0) |
| 1449 |
goto nla_put_failure; |
| 1450 |
|
| 1451 |
/* Additional table attribute replacing the 8bit in the header, was |
| 1452 |
* required to allow more than 256 tables. MPLS does not allow the |
| 1453 |
* table attribute to be set |
| 1454 |
*/ |
| 1455 |
if (route->rt_family != AF_MPLS) |
| 1456 |
NLA_PUT_U32(msg, RTA_TABLE, route->rt_table); |
| 1457 |
|
| 1458 |
if (nl_addr_get_len(route->rt_dst)) |
| 1459 |
NLA_PUT_ADDR(msg, RTA_DST, route->rt_dst); |
| 1460 |
|
| 1461 |
if (route->ce_mask & ROUTE_ATTR_PRIO) |
| 1462 |
NLA_PUT_U32(msg, RTA_PRIORITY, route->rt_prio); |
| 1463 |
|
| 1464 |
if (route->ce_mask & ROUTE_ATTR_SRC) |
| 1465 |
NLA_PUT_ADDR(msg, RTA_SRC, route->rt_src); |
| 1466 |
|
| 1467 |
if (route->ce_mask & ROUTE_ATTR_PREF_SRC) |
| 1468 |
NLA_PUT_ADDR(msg, RTA_PREFSRC, route->rt_pref_src); |
| 1469 |
|
| 1470 |
if (route->ce_mask & ROUTE_ATTR_IIF) |
| 1471 |
NLA_PUT_U32(msg, RTA_IIF, route->rt_iif); |
| 1472 |
|
| 1473 |
if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE) |
| 1474 |
NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate); |
| 1475 |
|
| 1476 |
if (route->rt_nmetrics > 0) { |
| 1477 |
uint32_t val; |
| 1478 |
|
| 1479 |
metrics = nla_nest_start(msg, RTA_METRICS); |
| 1480 |
if (metrics == NULL) |
| 1481 |
goto nla_put_failure; |
| 1482 |
|
| 1483 |
for (i = 1; i <= RTAX_MAX; i++) { |
| 1484 |
if (!rtnl_route_get_metric(route, i, &val)) |
| 1485 |
NLA_PUT_U32(msg, i, val); |
| 1486 |
} |
| 1487 |
|
| 1488 |
nla_nest_end(msg, metrics); |
| 1489 |
} |
| 1490 |
|
| 1491 |
/* Nexthop specification and nexthop id are mutually exclusive */ |
| 1492 |
if (route->ce_mask & ROUTE_ATTR_NHID) { |
| 1493 |
NLA_PUT_U32(msg, RTA_NH_ID, route->rt_nhid); |
| 1494 |
} else if (rtnl_route_get_nnexthops(route) == 1) { |
| 1495 |
struct rtnl_nexthop *nh; |
| 1496 |
|
| 1497 |
nh = rtnl_route_nexthop_n(route, 0); |
| 1498 |
if (nh->rtnh_gateway) |
| 1499 |
NLA_PUT_ADDR(msg, RTA_GATEWAY, nh->rtnh_gateway); |
| 1500 |
if (nh->rtnh_ifindex) |
| 1501 |
NLA_PUT_U32(msg, RTA_OIF, nh->rtnh_ifindex); |
| 1502 |
if (nh->rtnh_realms) |
| 1503 |
NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms); |
| 1504 |
if (nh->rtnh_newdst) |
| 1505 |
NLA_PUT_ADDR(msg, RTA_NEWDST, nh->rtnh_newdst); |
| 1506 |
if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0) |
| 1507 |
goto nla_put_failure; |
| 1508 |
if (nh->rtnh_encap && |
| 1509 |
nh_encap_build_msg(msg, nh->rtnh_encap) < 0) |
| 1510 |
goto nla_put_failure; |
| 1511 |
} else if (rtnl_route_get_nnexthops(route) > 1) { |
| 1512 |
struct nlattr *multipath; |
| 1513 |
struct rtnl_nexthop *nh; |
| 1514 |
|
| 1515 |
if (!(multipath = nla_nest_start(msg, RTA_MULTIPATH))) |
| 1516 |
goto nla_put_failure; |
| 1517 |
|
| 1518 |
nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) { |
| 1519 |
struct rtnexthop *rtnh; |
| 1520 |
|
| 1521 |
rtnh = nlmsg_reserve(msg, sizeof(*rtnh), NLMSG_ALIGNTO); |
| 1522 |
if (!rtnh) |
| 1523 |
goto nla_put_failure; |
| 1524 |
|
| 1525 |
rtnh->rtnh_flags = nh->rtnh_flags; |
| 1526 |
rtnh->rtnh_hops = nh->rtnh_weight; |
| 1527 |
rtnh->rtnh_ifindex = nh->rtnh_ifindex; |
| 1528 |
|
| 1529 |
if (nh->rtnh_gateway) |
| 1530 |
NLA_PUT_ADDR(msg, RTA_GATEWAY, |
| 1531 |
nh->rtnh_gateway); |
| 1532 |
|
| 1533 |
if (nh->rtnh_newdst) |
| 1534 |
NLA_PUT_ADDR(msg, RTA_NEWDST, nh->rtnh_newdst); |
| 1535 |
|
| 1536 |
if (nh->rtnh_via && |
| 1537 |
rtnl_route_put_via(msg, nh->rtnh_via) < 0) |
| 1538 |
goto nla_put_failure; |
| 1539 |
|
| 1540 |
if (nh->rtnh_realms) |
| 1541 |
NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms); |
| 1542 |
|
| 1543 |
if (nh->rtnh_encap && |
| 1544 |
nh_encap_build_msg(msg, nh->rtnh_encap) < 0) |
| 1545 |
goto nla_put_failure; |
| 1546 |
|
| 1547 |
rtnh->rtnh_len = (char *) nlmsg_tail(msg->nm_nlh) - |
| 1548 |
(char *) rtnh; |
| 1549 |
} |
| 1550 |
|
| 1551 |
nla_nest_end(msg, multipath); |
| 1552 |
} |
| 1553 |
|
| 1554 |
return 0; |
| 1555 |
|
| 1556 |
nla_put_failure: |
| 1557 |
return -NLE_MSGSIZE; |
| 1558 |
} |
| 1559 |
|
| 1560 |
/** @cond SKIP */ |
| 1561 |
struct nl_object_ops route_obj_ops = { |
| 1562 |
.oo_name = "route/route", |
| 1563 |
.oo_size = sizeof(struct rtnl_route), |
| 1564 |
.oo_constructor = route_constructor, |
| 1565 |
.oo_free_data = route_free_data, |
| 1566 |
.oo_clone = route_clone, |
| 1567 |
.oo_dump = { |
| 1568 |
[NL_DUMP_LINE] = route_dump_line, |
| 1569 |
[NL_DUMP_DETAILS] = route_dump_details, |
| 1570 |
[NL_DUMP_STATS] = route_dump_stats, |
| 1571 |
}, |
| 1572 |
.oo_compare = route_compare, |
| 1573 |
.oo_keygen = route_keygen, |
| 1574 |
.oo_update = route_update, |
| 1575 |
.oo_attrs2str = route_attrs2str, |
| 1576 |
.oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS | |
| 1577 |
ROUTE_ATTR_TABLE | ROUTE_ATTR_DST | |
| 1578 |
ROUTE_ATTR_PRIO), |
| 1579 |
.oo_id_attrs_get = route_id_attrs_get, |
| 1580 |
}; |
| 1581 |
/** @endcond */ |
| 1582 |
|
| 1583 |
/** @} */ |