xref: /aosp_15_r20/external/libnl/lib/route/route_obj.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
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 
route_constructor(struct nl_object * c)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 
route_free_data(struct nl_object * c)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 
route_clone(struct nl_object * _dst,struct nl_object * _src)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 
route_dump_line(struct nl_object * a,struct nl_dump_params * p)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 
route_dump_details(struct nl_object * a,struct nl_dump_params * p)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 
route_dump_stats(struct nl_object * obj,struct nl_dump_params * p)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 
route_keygen(struct nl_object * obj,uint32_t * hashkey,uint32_t table_sz)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 
route_id_attrs_get(struct nl_object * obj)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 
route_compare(struct nl_object * _a,struct nl_object * _b,uint64_t attrs,int flags)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 
route_update(struct nl_object * old_obj,struct nl_object * new_obj)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 
route_attrs2str(int attrs,char * buf,size_t len)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 
rtnl_route_alloc(void)644 struct rtnl_route *rtnl_route_alloc(void)
645 {
646 	return (struct rtnl_route *) nl_object_alloc(&route_obj_ops);
647 }
648 
rtnl_route_get(struct rtnl_route * route)649 void rtnl_route_get(struct rtnl_route *route)
650 {
651 	nl_object_get((struct nl_object *) route);
652 }
653 
rtnl_route_put(struct rtnl_route * route)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 
rtnl_route_set_table(struct rtnl_route * route,uint32_t table)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 
rtnl_route_get_table(struct rtnl_route * route)672 uint32_t rtnl_route_get_table(struct rtnl_route *route)
673 {
674 	return route->rt_table;
675 }
676 
rtnl_route_set_scope(struct rtnl_route * route,uint8_t scope)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 
rtnl_route_get_scope(struct rtnl_route * route)683 uint8_t rtnl_route_get_scope(struct rtnl_route *route)
684 {
685 	return route->rt_scope;
686 }
687 
rtnl_route_set_tos(struct rtnl_route * route,uint8_t tos)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 
rtnl_route_get_tos(struct rtnl_route * route)694 uint8_t rtnl_route_get_tos(struct rtnl_route *route)
695 {
696 	return route->rt_tos;
697 }
698 
rtnl_route_set_protocol(struct rtnl_route * route,uint8_t protocol)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 
rtnl_route_get_protocol(struct rtnl_route * route)705 uint8_t rtnl_route_get_protocol(struct rtnl_route *route)
706 {
707 	return route->rt_protocol;
708 }
709 
rtnl_route_set_priority(struct rtnl_route * route,uint32_t prio)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 
rtnl_route_get_priority(struct rtnl_route * route)716 uint32_t rtnl_route_get_priority(struct rtnl_route *route)
717 {
718 	return route->rt_prio;
719 }
720 
rtnl_route_set_family(struct rtnl_route * route,uint8_t family)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 
rtnl_route_get_family(struct rtnl_route * route)736 uint8_t rtnl_route_get_family(struct rtnl_route *route)
737 {
738 	return route->rt_family;
739 }
740 
rtnl_route_set_dst(struct rtnl_route * route,struct nl_addr * addr)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 
rtnl_route_get_dst(struct rtnl_route * route)760 struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route)
761 {
762 	return route->rt_dst;
763 }
764 
rtnl_route_set_src(struct rtnl_route * route,struct nl_addr * addr)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 
rtnl_route_get_src(struct rtnl_route * route)786 struct nl_addr *rtnl_route_get_src(struct rtnl_route *route)
787 {
788 	return route->rt_src;
789 }
790 
rtnl_route_set_type(struct rtnl_route * route,uint8_t type)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 
rtnl_route_get_type(struct rtnl_route * route)802 uint8_t rtnl_route_get_type(struct rtnl_route *route)
803 {
804 	return route->rt_type;
805 }
806 
rtnl_route_set_flags(struct rtnl_route * route,uint32_t flags)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 
rtnl_route_unset_flags(struct rtnl_route * route,uint32_t flags)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 
rtnl_route_get_flags(struct rtnl_route * route)821 uint32_t rtnl_route_get_flags(struct rtnl_route *route)
822 {
823 	return route->rt_flags;
824 }
825 
rtnl_route_set_metric(struct rtnl_route * route,int metric,uint32_t value)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 
rtnl_route_unset_metric(struct rtnl_route * route,int metric)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 
rtnl_route_get_metric(struct rtnl_route * route,int metric,uint32_t * value)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 
rtnl_route_set_pref_src(struct rtnl_route * route,struct nl_addr * addr)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 
rtnl_route_get_pref_src(struct rtnl_route * route)888 struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route)
889 {
890 	return route->rt_pref_src;
891 }
892 
rtnl_route_set_iif(struct rtnl_route * route,int ifindex)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 
rtnl_route_get_iif(struct rtnl_route * route)899 int rtnl_route_get_iif(struct rtnl_route *route)
900 {
901 	return route->rt_iif;
902 }
903 
rtnl_route_add_nexthop(struct rtnl_route * route,struct rtnl_nexthop * nh)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 
rtnl_route_remove_nexthop(struct rtnl_route * route,struct rtnl_nexthop * nh)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 
rtnl_route_get_nexthops(struct rtnl_route * route)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 
rtnl_route_get_nnexthops(struct rtnl_route * route)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 
rtnl_route_foreach_nexthop(struct rtnl_route * r,void (* cb)(struct rtnl_nexthop *,void *),void * arg)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 
rtnl_route_nexthop_n(struct rtnl_route * r,int n)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) return nh;
959 			i++;
960 		}
961 	}
962 	return NULL;
963 }
964 
rtnl_route_set_ttl_propagate(struct rtnl_route * route,uint8_t ttl_prop)965 void rtnl_route_set_ttl_propagate(struct rtnl_route *route, uint8_t ttl_prop)
966 {
967 	route->rt_ttl_propagate = ttl_prop;
968 	route->ce_mask |= ROUTE_ATTR_TTL_PROPAGATE;
969 }
970 
rtnl_route_get_ttl_propagate(struct rtnl_route * route)971 int rtnl_route_get_ttl_propagate(struct rtnl_route *route)
972 {
973 	if (!route)
974 		return -NLE_INVAL;
975 	if (!(route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE))
976 		return -NLE_MISSING_ATTR;
977 	return route->rt_ttl_propagate;
978 }
979 
rtnl_route_set_nhid(struct rtnl_route * route,uint32_t nhid)980 void rtnl_route_set_nhid(struct rtnl_route *route, uint32_t nhid)
981 {
982 	route->rt_nhid = nhid;
983 
984 	if (nhid > 0)
985 		route->ce_mask |= ROUTE_ATTR_NHID;
986 	else
987 		route->ce_mask &= ~ROUTE_ATTR_NHID;
988 }
989 
rtnl_route_get_nhid(struct rtnl_route * route)990 uint32_t rtnl_route_get_nhid(struct rtnl_route *route)
991 {
992 	return route->rt_nhid;
993 }
994 
995 /** @} */
996 
997 /**
998  * @name Utilities
999  * @{
1000  */
1001 
1002 /**
1003  * Guess scope of a route object.
1004  * @arg route		Route object.
1005  *
1006  * Guesses the scope of a route object, based on the following rules:
1007  * @code
1008  *   1) Local route -> local scope
1009  *   2) At least one nexthop not directly connected -> universe scope
1010  *   3) All others -> link scope
1011  * @endcode
1012  *
1013  * @return Scope value.
1014  */
rtnl_route_guess_scope(struct rtnl_route * route)1015 int rtnl_route_guess_scope(struct rtnl_route *route)
1016 {
1017 	if (route->rt_type == RTN_LOCAL)
1018 		return RT_SCOPE_HOST;
1019 
1020 	if (route->rt_family == AF_MPLS)
1021 		return RT_SCOPE_UNIVERSE;
1022 
1023 	if (!nl_list_empty(&route->rt_nexthops)) {
1024 		struct rtnl_nexthop *nh;
1025 
1026 		/*
1027 		 * Use scope uiniverse if there is at least one nexthop which
1028 		 * is not directly connected
1029 		 */
1030 		nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1031 			if (nh->rtnh_gateway || nh->rtnh_via)
1032 				return RT_SCOPE_UNIVERSE;
1033 		}
1034 	}
1035 
1036 	return RT_SCOPE_LINK;
1037 }
1038 
1039 /** @} */
1040 
rtnl_route_parse_via(struct nlattr * nla)1041 static struct nl_addr *rtnl_route_parse_via(struct nlattr *nla)
1042 {
1043 	int alen = nla_len(nla) - offsetof(struct rtvia, rtvia_addr);
1044 	struct rtvia *via = nla_data(nla);
1045 
1046 	return nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1047 }
1048 
rtnl_route_put_via(struct nl_msg * msg,struct nl_addr * addr)1049 static int rtnl_route_put_via(struct nl_msg *msg, struct nl_addr *addr)
1050 {
1051 	unsigned int alen = nl_addr_get_len(addr);
1052 	struct nlattr *nla;
1053 	struct rtvia *via;
1054 
1055 	nla = nla_reserve(msg, RTA_VIA, alen + sizeof(*via));
1056 	if (!nla)
1057 		return -EMSGSIZE;
1058 
1059 	via = nla_data(nla);
1060 	via->rtvia_family = nl_addr_get_family(addr);
1061 	memcpy(via->rtvia_addr, nl_addr_get_binary_addr(addr), alen);
1062 
1063 	return 0;
1064 }
1065 
1066 static struct nla_policy route_policy[RTA_MAX+1] = {
1067 	[RTA_IIF]	= { .type = NLA_U32 },
1068 	[RTA_OIF]	= { .type = NLA_U32 },
1069 	[RTA_PRIORITY]	= { .type = NLA_U32 },
1070 	[RTA_FLOW]	= { .type = NLA_U32 },
1071 	[RTA_CACHEINFO]	= { .minlen = sizeof(struct rta_cacheinfo) },
1072 	[RTA_METRICS]	= { .type = NLA_NESTED },
1073 	[RTA_MULTIPATH]	= { .type = NLA_NESTED },
1074 	[RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
1075 	[RTA_ENCAP]	= { .type = NLA_NESTED },
1076 	[RTA_ENCAP_TYPE] = { .type = NLA_U16 },
1077 	[RTA_NH_ID]	= { .type = NLA_U32 },
1078 };
1079 
parse_multipath(struct rtnl_route * route,struct nlattr * attr)1080 static int parse_multipath(struct rtnl_route *route, struct nlattr *attr)
1081 {
1082 	struct rtnexthop *rtnh = nla_data(attr);
1083 	size_t tlen = nla_len(attr);
1084 	int err;
1085 
1086 	while (tlen >= sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
1087 		_nl_auto_rtnl_nexthop struct rtnl_nexthop *nh = NULL;
1088 
1089 		nh = rtnl_route_nh_alloc();
1090 		if (!nh)
1091 			return -NLE_NOMEM;
1092 
1093 		rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
1094 		rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
1095 		rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
1096 
1097 		if (rtnh->rtnh_len > sizeof(*rtnh)) {
1098 			struct nlattr *ntb[RTA_MAX + 1];
1099 
1100 			err = nla_parse(ntb, RTA_MAX, (struct nlattr *)
1101 					RTNH_DATA(rtnh),
1102 					rtnh->rtnh_len - sizeof(*rtnh),
1103 					route_policy);
1104 			if (err < 0)
1105 				return err;
1106 
1107 			if (ntb[RTA_GATEWAY]) {
1108 				_nl_auto_nl_addr struct nl_addr *addr = NULL;
1109 
1110 				addr = nl_addr_alloc_attr(ntb[RTA_GATEWAY],
1111 							  route->rt_family);
1112 				if (!addr)
1113 					return -NLE_NOMEM;
1114 
1115 				rtnl_route_nh_set_gateway(nh, addr);
1116 			}
1117 
1118 			if (ntb[RTA_FLOW]) {
1119 				uint32_t realms;
1120 
1121 				realms = nla_get_u32(ntb[RTA_FLOW]);
1122 				rtnl_route_nh_set_realms(nh, realms);
1123 			}
1124 
1125 			if (ntb[RTA_NEWDST]) {
1126 				_nl_auto_nl_addr struct nl_addr *addr = NULL;
1127 
1128 				addr = nl_addr_alloc_attr(ntb[RTA_NEWDST],
1129 							  route->rt_family);
1130 				if (!addr)
1131 					return -NLE_NOMEM;
1132 
1133 				err = rtnl_route_nh_set_newdst(nh, addr);
1134 				if (err < 0)
1135 					return err;
1136 			}
1137 
1138 			if (ntb[RTA_VIA]) {
1139 				_nl_auto_nl_addr struct nl_addr *addr = NULL;
1140 
1141 				addr = rtnl_route_parse_via(ntb[RTA_VIA]);
1142 				if (!addr)
1143 					return -NLE_NOMEM;
1144 
1145 				err = rtnl_route_nh_set_via(nh, addr);
1146 				if (err < 0)
1147 					return err;
1148 			}
1149 
1150 			if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1151 				err = nh_encap_parse_msg(ntb[RTA_ENCAP],
1152 							 ntb[RTA_ENCAP_TYPE],
1153 							 nh);
1154 				if (err < 0)
1155 					return err;
1156 			}
1157 		}
1158 
1159 		rtnl_route_add_nexthop(route, _nl_steal_pointer(&nh));
1160 		tlen -= RTNH_ALIGN(rtnh->rtnh_len);
1161 		rtnh = RTNH_NEXT(rtnh);
1162 	}
1163 
1164 	return 0;
1165 }
1166 
rtnl_route_parse(struct nlmsghdr * nlh,struct rtnl_route ** result)1167 int rtnl_route_parse(struct nlmsghdr *nlh, struct rtnl_route **result)
1168 {
1169 	_nl_auto_rtnl_route struct rtnl_route *route = NULL;
1170 	_nl_auto_rtnl_nexthop struct rtnl_nexthop *old_nh = NULL;
1171 	_nl_auto_nl_addr struct nl_addr *src = NULL;
1172 	_nl_auto_nl_addr struct nl_addr *dst = NULL;
1173 	struct nlattr *tb[RTA_MAX + 1];
1174 	struct rtmsg *rtm;
1175 	int family;
1176 	int err;
1177 
1178 	route = rtnl_route_alloc();
1179 	if (!route)
1180 		return -NLE_NOMEM;
1181 
1182 	route->ce_msgtype = nlh->nlmsg_type;
1183 
1184 	err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, route_policy);
1185 	if (err < 0)
1186 		return err;
1187 
1188 	rtm = nlmsg_data(nlh);
1189 	route->rt_family = family = rtm->rtm_family;
1190 	route->rt_tos = rtm->rtm_tos;
1191 	route->rt_table = rtm->rtm_table;
1192 	route->rt_type = rtm->rtm_type;
1193 	route->rt_scope = rtm->rtm_scope;
1194 	route->rt_protocol = rtm->rtm_protocol;
1195 	route->rt_flags = rtm->rtm_flags;
1196 	route->rt_prio = 0;
1197 
1198 	route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1199 			  ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
1200 			  ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
1201 			  ROUTE_ATTR_FLAGS;
1202 
1203 	/* right now MPLS does not allow rt_prio to be set, so don't
1204 	 * assume it is unless it comes from an attribute
1205 	 */
1206 	if (family != AF_MPLS)
1207 		route->ce_mask |= ROUTE_ATTR_PRIO;
1208 
1209 	if (tb[RTA_DST]) {
1210 		if (!(dst = nl_addr_alloc_attr(tb[RTA_DST], family)))
1211 			return -NLE_NOMEM;
1212 	} else {
1213 		int len;
1214 
1215 		switch (family) {
1216 			case AF_INET:
1217 				len = 4;
1218 				break;
1219 
1220 			case AF_INET6:
1221 				len = 16;
1222 				break;
1223 			default:
1224 				len = 0;
1225 				break;
1226 		}
1227 
1228 		if (!(dst = nl_addr_build(family, NULL, len)))
1229 			return -NLE_NOMEM;
1230 	}
1231 
1232 	nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
1233 	err = rtnl_route_set_dst(route, dst);
1234 	if (err < 0)
1235 		return err;
1236 
1237 	if (tb[RTA_SRC]) {
1238 		if (!(src = nl_addr_alloc_attr(tb[RTA_SRC], family)))
1239 			return -NLE_NOMEM;
1240 	} else if (rtm->rtm_src_len)
1241 		if (!(src = nl_addr_alloc(0)))
1242 			return -NLE_NOMEM;
1243 
1244 	if (src) {
1245 		nl_addr_set_prefixlen(src, rtm->rtm_src_len);
1246 		rtnl_route_set_src(route, src);
1247 	}
1248 
1249 	if (tb[RTA_TABLE])
1250 		rtnl_route_set_table(route, nla_get_u32(tb[RTA_TABLE]));
1251 
1252 	if (tb[RTA_IIF])
1253 		rtnl_route_set_iif(route, nla_get_u32(tb[RTA_IIF]));
1254 
1255 	if (tb[RTA_PRIORITY])
1256 		rtnl_route_set_priority(route, nla_get_u32(tb[RTA_PRIORITY]));
1257 
1258 	if (tb[RTA_PREFSRC]) {
1259 		_nl_auto_nl_addr struct nl_addr *addr = NULL;
1260 
1261 		if (!(addr = nl_addr_alloc_attr(tb[RTA_PREFSRC], family)))
1262 			return -NLE_NOMEM;
1263 		rtnl_route_set_pref_src(route, addr);
1264 	}
1265 
1266 	if (tb[RTA_METRICS]) {
1267 		struct nlattr *mtb[RTAX_MAX + 1];
1268 		int i;
1269 
1270 		err = nla_parse_nested(mtb, RTAX_MAX, tb[RTA_METRICS], NULL);
1271 		if (err < 0)
1272 			return err;
1273 
1274 		for (i = 1; i <= RTAX_MAX; i++) {
1275 			if (mtb[i] && _nla_len(mtb[i]) >= sizeof(uint32_t)) {
1276 				uint32_t m = nla_get_u32(mtb[i]);
1277 
1278 				err = rtnl_route_set_metric(route, i, m);
1279 				if (err < 0)
1280 					return err;
1281 			}
1282 		}
1283 	}
1284 
1285 	if (tb[RTA_MULTIPATH]) {
1286 		if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
1287 			return err;
1288 	}
1289 
1290 	if (tb[RTA_CACHEINFO]) {
1291 		nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
1292 			   sizeof(route->rt_cacheinfo));
1293 		route->ce_mask |= ROUTE_ATTR_CACHEINFO;
1294 	}
1295 
1296 	if (tb[RTA_OIF]) {
1297 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1298 			return -NLE_NOMEM;
1299 
1300 		rtnl_route_nh_set_ifindex(old_nh, nla_get_u32(tb[RTA_OIF]));
1301 	}
1302 
1303 	if (tb[RTA_GATEWAY]) {
1304 		_nl_auto_nl_addr struct nl_addr *addr = NULL;
1305 
1306 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1307 			return -NLE_NOMEM;
1308 
1309 		if (!(addr = nl_addr_alloc_attr(tb[RTA_GATEWAY], family)))
1310 			return -NLE_NOMEM;
1311 
1312 		rtnl_route_nh_set_gateway(old_nh, addr);
1313 	}
1314 
1315 	if (tb[RTA_FLOW]) {
1316 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1317 			return -NLE_NOMEM;
1318 
1319 		rtnl_route_nh_set_realms(old_nh, nla_get_u32(tb[RTA_FLOW]));
1320 	}
1321 
1322 	if (tb[RTA_NEWDST]) {
1323 		_nl_auto_nl_addr struct nl_addr *addr = NULL;
1324 
1325 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1326 			return -NLE_NOMEM;
1327 
1328 		addr = nl_addr_alloc_attr(tb[RTA_NEWDST], route->rt_family);
1329 		if (!addr)
1330 			return -NLE_NOMEM;
1331 
1332 		err = rtnl_route_nh_set_newdst(old_nh, addr);
1333 		if (err < 0)
1334 			return err;
1335 	}
1336 
1337 	if (tb[RTA_VIA]) {
1338 		int alen = nla_len(tb[RTA_VIA]) - offsetof(struct rtvia, rtvia_addr);
1339 		_nl_auto_nl_addr struct nl_addr *addr = NULL;
1340 		struct rtvia *via = nla_data(tb[RTA_VIA]);
1341 
1342 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1343 			return -NLE_NOMEM;
1344 
1345 		addr = nl_addr_build(via->rtvia_family, via->rtvia_addr, alen);
1346 		if (!addr)
1347 			return -NLE_NOMEM;
1348 
1349 		err = rtnl_route_nh_set_via(old_nh, addr);
1350 		if (err < 0)
1351 			return err;
1352 	}
1353 
1354 	if (tb[RTA_TTL_PROPAGATE]) {
1355 		rtnl_route_set_ttl_propagate(route,
1356 					     nla_get_u8(tb[RTA_TTL_PROPAGATE]));
1357 	}
1358 
1359 	if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1360 		if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
1361 			return -NLE_NOMEM;
1362 
1363 		err = nh_encap_parse_msg(tb[RTA_ENCAP],
1364 					 tb[RTA_ENCAP_TYPE], old_nh);
1365 		if (err < 0)
1366 			return err;
1367 	}
1368 
1369 	if (tb[RTA_NH_ID]) {
1370 		rtnl_route_set_nhid(route, nla_get_u32(tb[RTA_NH_ID]));
1371 	}
1372 
1373 	if (old_nh) {
1374 		rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
1375 		if (route->rt_nr_nh == 0) {
1376 			/* If no nexthops have been provided via RTA_MULTIPATH
1377 			 * we add it as regular nexthop to maintain backwards
1378 			 * compatibility */
1379 			rtnl_route_add_nexthop(route, _nl_steal_pointer(&old_nh));
1380 		} else {
1381 			/* Kernel supports new style nexthop configuration,
1382 			 * verify that it is a duplicate and discard nexthop. */
1383 			struct rtnl_nexthop *first;
1384 
1385 			first = nl_list_first_entry(&route->rt_nexthops,
1386 						    struct rtnl_nexthop,
1387 						    rtnh_list);
1388 			if (!first)
1389 				BUG();
1390 
1391 			if (rtnl_route_nh_compare(old_nh, first,
1392 						  old_nh->ce_mask, 0)) {
1393 				return -NLE_INVAL;
1394 			}
1395 		}
1396 	}
1397 
1398 	*result = _nl_steal_pointer(&route);
1399 	return 0;
1400 }
1401 
rtnl_route_build_msg(struct nl_msg * msg,struct rtnl_route * route)1402 int rtnl_route_build_msg(struct nl_msg *msg, struct rtnl_route *route)
1403 {
1404 	int i;
1405 	struct nlattr *metrics;
1406 	struct rtmsg rtmsg = {
1407 		.rtm_family = route->rt_family,
1408 		.rtm_tos = route->rt_tos,
1409 		.rtm_table = route->rt_table,
1410 		.rtm_protocol = route->rt_protocol,
1411 		.rtm_scope = route->rt_scope,
1412 		.rtm_type = route->rt_type,
1413 		.rtm_flags = route->rt_flags,
1414 	};
1415 
1416 	if (route->rt_dst == NULL)
1417 		return -NLE_MISSING_ATTR;
1418 
1419 	rtmsg.rtm_dst_len = nl_addr_get_prefixlen(route->rt_dst);
1420 	if (route->rt_src)
1421 		rtmsg.rtm_src_len = nl_addr_get_prefixlen(route->rt_src);
1422 
1423 	if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
1424 		rtmsg.rtm_scope = rtnl_route_guess_scope(route);
1425 
1426 	if (rtnl_route_get_nnexthops(route) == 1) {
1427 		struct rtnl_nexthop *nh;
1428 		nh = rtnl_route_nexthop_n(route, 0);
1429 		rtmsg.rtm_flags |= nh->rtnh_flags;
1430 	}
1431 
1432 	if (nlmsg_append(msg, &rtmsg, sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
1433 		goto nla_put_failure;
1434 
1435 	/* Additional table attribute replacing the 8bit in the header, was
1436 	 * required to allow more than 256 tables. MPLS does not allow the
1437 	 * table attribute to be set
1438 	 */
1439 	if (route->rt_family != AF_MPLS)
1440 		NLA_PUT_U32(msg, RTA_TABLE, route->rt_table);
1441 
1442 	if (nl_addr_get_len(route->rt_dst))
1443 		NLA_PUT_ADDR(msg, RTA_DST, route->rt_dst);
1444 
1445 	if (route->ce_mask & ROUTE_ATTR_PRIO)
1446 		NLA_PUT_U32(msg, RTA_PRIORITY, route->rt_prio);
1447 
1448 	if (route->ce_mask & ROUTE_ATTR_SRC)
1449 		NLA_PUT_ADDR(msg, RTA_SRC, route->rt_src);
1450 
1451 	if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
1452 		NLA_PUT_ADDR(msg, RTA_PREFSRC, route->rt_pref_src);
1453 
1454 	if (route->ce_mask & ROUTE_ATTR_IIF)
1455 		NLA_PUT_U32(msg, RTA_IIF, route->rt_iif);
1456 
1457 	if (route->ce_mask & ROUTE_ATTR_TTL_PROPAGATE)
1458 		NLA_PUT_U8(msg, RTA_TTL_PROPAGATE, route->rt_ttl_propagate);
1459 
1460 	if (route->rt_nmetrics > 0) {
1461 		uint32_t val;
1462 
1463 		metrics = nla_nest_start(msg, RTA_METRICS);
1464 		if (metrics == NULL)
1465 			goto nla_put_failure;
1466 
1467 		for (i = 1; i <= RTAX_MAX; i++) {
1468 			if (!rtnl_route_get_metric(route, i, &val))
1469 				NLA_PUT_U32(msg, i, val);
1470 		}
1471 
1472 		nla_nest_end(msg, metrics);
1473 	}
1474 
1475 	/* Nexthop specification and nexthop id are mutually exclusive */
1476 	if (route->ce_mask & ROUTE_ATTR_NHID) {
1477 		NLA_PUT_U32(msg, RTA_NH_ID, route->rt_nhid);
1478 	} else if (rtnl_route_get_nnexthops(route) == 1) {
1479 		struct rtnl_nexthop *nh;
1480 
1481 		nh = rtnl_route_nexthop_n(route, 0);
1482 		if (nh->rtnh_gateway)
1483 			NLA_PUT_ADDR(msg, RTA_GATEWAY, nh->rtnh_gateway);
1484 		if (nh->rtnh_ifindex)
1485 			NLA_PUT_U32(msg, RTA_OIF, nh->rtnh_ifindex);
1486 		if (nh->rtnh_realms)
1487 			NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
1488 		if (nh->rtnh_newdst)
1489 			NLA_PUT_ADDR(msg, RTA_NEWDST, nh->rtnh_newdst);
1490 		if (nh->rtnh_via && rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1491 			goto nla_put_failure;
1492 		if (nh->rtnh_encap &&
1493 		    nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1494 			goto nla_put_failure;
1495 	} else if (rtnl_route_get_nnexthops(route) > 1) {
1496 		struct nlattr *multipath;
1497 		struct rtnl_nexthop *nh;
1498 
1499 		if (!(multipath = nla_nest_start(msg, RTA_MULTIPATH)))
1500 			goto nla_put_failure;
1501 
1502 		nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
1503 			struct rtnexthop *rtnh;
1504 
1505 			rtnh = nlmsg_reserve(msg, sizeof(*rtnh), NLMSG_ALIGNTO);
1506 			if (!rtnh)
1507 				goto nla_put_failure;
1508 
1509 			rtnh->rtnh_flags = nh->rtnh_flags;
1510 			rtnh->rtnh_hops = nh->rtnh_weight;
1511 			rtnh->rtnh_ifindex = nh->rtnh_ifindex;
1512 
1513 			if (nh->rtnh_gateway)
1514 				NLA_PUT_ADDR(msg, RTA_GATEWAY,
1515 					     nh->rtnh_gateway);
1516 
1517 			if (nh->rtnh_newdst)
1518 				NLA_PUT_ADDR(msg, RTA_NEWDST, nh->rtnh_newdst);
1519 
1520 			if (nh->rtnh_via &&
1521 			    rtnl_route_put_via(msg, nh->rtnh_via) < 0)
1522 				goto nla_put_failure;
1523 
1524 			if (nh->rtnh_realms)
1525 				NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
1526 
1527 			if (nh->rtnh_encap &&
1528 			    nh_encap_build_msg(msg, nh->rtnh_encap) < 0)
1529 				goto nla_put_failure;
1530 
1531 			rtnh->rtnh_len = (char *) nlmsg_tail(msg->nm_nlh) -
1532 						(char *) rtnh;
1533 		}
1534 
1535 		nla_nest_end(msg, multipath);
1536 	}
1537 
1538 	return 0;
1539 
1540 nla_put_failure:
1541 	return -NLE_MSGSIZE;
1542 }
1543 
1544 /** @cond SKIP */
1545 struct nl_object_ops route_obj_ops = {
1546 	.oo_name		= "route/route",
1547 	.oo_size		= sizeof(struct rtnl_route),
1548 	.oo_constructor		= route_constructor,
1549 	.oo_free_data		= route_free_data,
1550 	.oo_clone		= route_clone,
1551 	.oo_dump = {
1552 	    [NL_DUMP_LINE]	= route_dump_line,
1553 	    [NL_DUMP_DETAILS]	= route_dump_details,
1554 	    [NL_DUMP_STATS]	= route_dump_stats,
1555 	},
1556 	.oo_compare		= route_compare,
1557 	.oo_keygen		= route_keygen,
1558 	.oo_update		= route_update,
1559 	.oo_attrs2str		= route_attrs2str,
1560 	.oo_id_attrs		= (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
1561 				   ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
1562 				   ROUTE_ATTR_PRIO),
1563 	.oo_id_attrs_get	= route_id_attrs_get,
1564 };
1565 /** @endcond */
1566 
1567 /** @} */
1568