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