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