blob: 175d351a13b02982ee356027b8a14a1c6b5e196b [file] [log] [blame]
Avneesh Sachdev5adc2522012-11-13 22:48:59 +00001/*
2 * Code for encoding/decoding FPM messages that are in netlink format.
3 *
4 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
7 *
8 * This file is part of GNU Zebra.
9 *
10 * GNU Zebra is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * GNU Zebra is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with GNU Zebra; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26#include <zebra.h>
27
28#include "log.h"
29#include "rib.h"
30
31#include "rt_netlink.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050032#include "nexthop.h"
Avneesh Sachdev5adc2522012-11-13 22:48:59 +000033
34#include "zebra_fpm_private.h"
35
36/*
37 * addr_to_a
38 *
39 * Returns string representation of an address of the given AF.
40 */
41static inline const char *
42addr_to_a (u_char af, void *addr)
43{
44 if (!addr)
45 return "<No address>";
46
47 switch (af)
48 {
49
50 case AF_INET:
51 return inet_ntoa (*((struct in_addr *) addr));
52
53#ifdef HAVE_IPV6
54 case AF_INET6:
55 return inet6_ntoa (*((struct in6_addr *) addr));
56#endif
57
58 default:
59 return "<Addr in unknown AF>";
60 }
61}
62
63/*
64 * prefix_addr_to_a
65 *
66 * Convience wrapper that returns a human-readable string for the
67 * address in a prefix.
68 */
69static const char *
70prefix_addr_to_a (struct prefix *prefix)
71{
72 if (!prefix)
73 return "<No address>";
74
75 return addr_to_a (prefix->family, &prefix->u.prefix);
76}
77
78/*
79 * af_addr_size
80 *
81 * The size of an address in a given address family.
82 */
83static size_t
84af_addr_size (u_char af)
85{
86 switch (af)
87 {
88
89 case AF_INET:
90 return 4;
91
92#ifdef HAVE_IPV6
93 case AF_INET6:
94 return 16;
95#endif
96
97 default:
98 assert(0);
99 return 16;
100 }
101}
102
103/*
104 * netlink_nh_info_t
105 *
106 * Holds information about a single nexthop for netlink. These info
107 * structures are transient and may contain pointers into rib
108 * data structures for convenience.
109 */
110typedef struct netlink_nh_info_t_
111{
112 uint32_t if_index;
113 union g_addr *gateway;
114
115 /*
116 * Information from the struct nexthop from which this nh was
117 * derived. For debug purposes only.
118 */
119 int recursive;
120 enum nexthop_types_t type;
121} netlink_nh_info_t;
122
123/*
124 * netlink_route_info_t
125 *
126 * A structure for holding information for a netlink route message.
127 */
128typedef struct netlink_route_info_t_
129{
130 uint16_t nlmsg_type;
131 u_char rtm_type;
132 uint32_t rtm_table;
133 u_char rtm_protocol;
134 u_char af;
135 struct prefix *prefix;
136 uint32_t *metric;
137 int num_nhs;
138
139 /*
Donald Sharp91ce87a2015-12-09 08:24:45 -0500140 * Nexthop structures
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000141 */
Donald Sharp91ce87a2015-12-09 08:24:45 -0500142 netlink_nh_info_t nhs[MULTIPATH_NUM];
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000143 union g_addr *pref_src;
144} netlink_route_info_t;
145
146/*
147 * netlink_route_info_add_nh
148 *
149 * Add information about the given nexthop to the given route info
150 * structure.
151 *
152 * Returns TRUE if a nexthop was added, FALSE otherwise.
153 */
154static int
Christian Frankefa713d92013-07-05 15:35:37 +0000155netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop,
156 int recursive)
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000157{
158 netlink_nh_info_t nhi;
159 union g_addr *src;
160
161 memset (&nhi, 0, sizeof (nhi));
162 src = NULL;
163
164 if (ri->num_nhs >= (int) ZEBRA_NUM_OF (ri->nhs))
165 return 0;
166
Christian Frankefa713d92013-07-05 15:35:37 +0000167 nhi.recursive = recursive;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000168 nhi.type = nexthop->type;
Avneesh Sachdev324ed1f2012-11-13 22:49:01 +0000169 nhi.if_index = nexthop->ifindex;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000170
171 if (nexthop->type == NEXTHOP_TYPE_IPV4
172 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
173 {
174 nhi.gateway = &nexthop->gate;
175 if (nexthop->src.ipv4.s_addr)
176 src = &nexthop->src;
177 }
178
179#ifdef HAVE_IPV6
180 if (nexthop->type == NEXTHOP_TYPE_IPV6
181 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
182 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
183 {
184 nhi.gateway = &nexthop->gate;
185 }
186#endif /* HAVE_IPV6 */
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000187
Avneesh Sachdev324ed1f2012-11-13 22:49:01 +0000188 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
189 || nexthop->type == NEXTHOP_TYPE_IFNAME)
190 {
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000191 if (nexthop->src.ipv4.s_addr)
192 src = &nexthop->src;
193 }
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000194
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000195 if (!nhi.gateway && nhi.if_index == 0)
196 return 0;
197
198 /*
199 * We have a valid nhi. Copy the structure over to the route_info.
200 */
201 ri->nhs[ri->num_nhs] = nhi;
202 ri->num_nhs++;
203
204 if (src && !ri->pref_src)
205 ri->pref_src = src;
206
207 return 1;
208}
209
210/*
211 * netlink_proto_from_route_type
212 */
213static u_char
214netlink_proto_from_route_type (int type)
215{
216 switch (type)
217 {
218 case ZEBRA_ROUTE_KERNEL:
219 case ZEBRA_ROUTE_CONNECT:
220 return RTPROT_KERNEL;
221
222 default:
223 return RTPROT_ZEBRA;
224 }
225}
226
227/*
228 * netlink_route_info_fill
229 *
230 * Fill out the route information object from the given route.
231 *
232 * Returns TRUE on success and FALSE on failure.
233 */
234static int
235netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
236 rib_dest_t *dest, struct rib *rib)
237{
Christian Frankefa713d92013-07-05 15:35:37 +0000238 struct nexthop *nexthop, *tnexthop;
239 int recursing;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000240 int discard;
241
242 memset (ri, 0, sizeof (*ri));
243
244 ri->prefix = rib_dest_prefix (dest);
245 ri->af = rib_dest_af (dest);
246
247 ri->nlmsg_type = cmd;
Feng Lu41f44a22015-05-22 11:39:56 +0200248 ri->rtm_table = rib_dest_vrf (dest)->vrf_id;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000249 ri->rtm_protocol = RTPROT_UNSPEC;
250
251 /*
252 * An RTM_DELROUTE need not be accompanied by any nexthops,
253 * particularly in our communication with the FPM.
254 */
Christian Franke51b45a52016-06-06 22:04:30 +0200255 if (cmd == RTM_DELROUTE)
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000256 goto skip;
257
Christian Franke51b45a52016-06-06 22:04:30 +0200258 if (!rib)
259 {
260 zlog_err("netlink_route_info_fill RTM_ADDROUTE called without rib info");
261 return 0;
262 }
263
264 ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000265
266 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
267 discard = 1;
268 else
269 discard = 0;
270
271 if (cmd == RTM_NEWROUTE)
272 {
273 if (discard)
274 {
275 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
276 ri->rtm_type = RTN_BLACKHOLE;
277 else if (rib->flags & ZEBRA_FLAG_REJECT)
278 ri->rtm_type = RTN_UNREACHABLE;
279 else
280 assert (0);
281 }
282 else
283 ri->rtm_type = RTN_UNICAST;
284 }
285
286 ri->metric = &rib->metric;
287
288 if (discard)
289 {
290 goto skip;
291 }
292
Christian Frankefa713d92013-07-05 15:35:37 +0000293 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000294 {
Donald Sharp91ce87a2015-12-09 08:24:45 -0500295 if (ri->num_nhs >= MULTIPATH_NUM)
Christian Frankefa713d92013-07-05 15:35:37 +0000296 break;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000297
Christian Frankefa713d92013-07-05 15:35:37 +0000298 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
299 continue;
300
301 if ((cmd == RTM_NEWROUTE
302 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
303 || (cmd == RTM_DELROUTE
304 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000305 {
Christian Frankefa713d92013-07-05 15:35:37 +0000306 netlink_route_info_add_nh (ri, nexthop, recursing);
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000307 }
308 }
309
310 /* If there is no useful nexthop then return. */
311 if (ri->num_nhs == 0)
312 {
313 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
314 return 0;
315 }
316
317 skip:
318 return 1;
319}
320
321/*
322 * netlink_route_info_encode
323 *
324 * Returns the number of bytes written to the buffer. 0 or a negative
325 * value indicates an error.
326 */
327static int
328netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
329 size_t in_buf_len)
330{
Paul Jakma6f235412016-04-05 13:21:20 +0100331 size_t bytelen;
Avneesh Sachdev5adc2522012-11-13 22:48:59 +0000332 int nexthop_num = 0;
333 size_t buf_offset;
334 netlink_nh_info_t *nhi;
335
336 struct
337 {
338 struct nlmsghdr n;
339 struct rtmsg r;
340 char buf[1];
341 } *req;
342
343 req = (void *) in_buf;
344
345 buf_offset = ((char *) req->buf) - ((char *) req);
346
347 if (in_buf_len < buf_offset) {
348 assert(0);
349 return 0;
350 }
351
352 memset (req, 0, buf_offset);
353
354 bytelen = af_addr_size (ri->af);
355
356 req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
357 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
358 req->n.nlmsg_type = ri->nlmsg_type;
359 req->r.rtm_family = ri->af;
360 req->r.rtm_table = ri->rtm_table;
361 req->r.rtm_dst_len = ri->prefix->prefixlen;
362 req->r.rtm_protocol = ri->rtm_protocol;
363 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
364
365 addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
366
367 req->r.rtm_type = ri->rtm_type;
368
369 /* Metric. */
370 if (ri->metric)
371 addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
372
373 if (ri->num_nhs == 0)
374 goto done;
375
376 if (ri->num_nhs == 1)
377 {
378 nhi = &ri->nhs[0];
379
380 if (nhi->gateway)
381 {
382 addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
383 bytelen);
384 }
385
386 if (nhi->if_index)
387 {
388 addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
389 }
390
391 goto done;
392
393 }
394
395 /*
396 * Multipath case.
397 */
398 char buf[NL_PKT_BUF_SIZE];
399 struct rtattr *rta = (void *) buf;
400 struct rtnexthop *rtnh;
401
402 rta->rta_type = RTA_MULTIPATH;
403 rta->rta_len = RTA_LENGTH (0);
404 rtnh = RTA_DATA (rta);
405
406 for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
407 {
408 nhi = &ri->nhs[nexthop_num];
409
410 rtnh->rtnh_len = sizeof (*rtnh);
411 rtnh->rtnh_flags = 0;
412 rtnh->rtnh_hops = 0;
413 rtnh->rtnh_ifindex = 0;
414 rta->rta_len += rtnh->rtnh_len;
415
416 if (nhi->gateway)
417 {
418 rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
419 rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
420 }
421
422 if (nhi->if_index)
423 {
424 rtnh->rtnh_ifindex = nhi->if_index;
425 }
426
427 rtnh = RTNH_NEXT (rtnh);
428 }
429
430 assert (rta->rta_len > RTA_LENGTH (0));
431 addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
432 RTA_PAYLOAD (rta));
433
434done:
435
436 if (ri->pref_src)
437 {
438 addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
439 }
440
441 assert (req->n.nlmsg_len < in_buf_len);
442 return req->n.nlmsg_len;
443}
444
445/*
446 * zfpm_log_route_info
447 *
448 * Helper function to log the information in a route_info structure.
449 */
450static void
451zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
452{
453 netlink_nh_info_t *nhi;
454 int i;
455
456 zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
457 nl_msg_type_to_str (ri->nlmsg_type),
458 prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
459 nl_rtproto_to_str (ri->rtm_protocol),
460 ri->metric ? *ri->metric : 0);
461
462 for (i = 0; i < ri->num_nhs; i++)
463 {
464 nhi = &ri->nhs[i];
465 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
466 nhi->if_index, addr_to_a (ri->af, nhi->gateway),
467 nhi->recursive ? "yes" : "no",
468 nexthop_type_to_str (nhi->type));
469 }
470}
471
472/*
473 * zfpm_netlink_encode_route
474 *
475 * Create a netlink message corresponding to the given route in the
476 * given buffer space.
477 *
478 * Returns the number of bytes written to the buffer. 0 or a negative
479 * value indicates an error.
480 */
481int
482zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
483 char *in_buf, size_t in_buf_len)
484{
485 netlink_route_info_t ri_space, *ri;
486
487 ri = &ri_space;
488
489 if (!netlink_route_info_fill (ri, cmd, dest, rib))
490 return 0;
491
492 zfpm_log_route_info (ri, __FUNCTION__);
493
494 return netlink_route_info_encode (ri, in_buf, in_buf_len);
495}