]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_fpm_netlink.c
doc: add blurbs on zebra FPM interface and commands
[mirror_frr.git] / zebra / zebra_fpm_netlink.c
CommitLineData
5adc2528
AS
1/*
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"
32
33#include "zebra_fpm_private.h"
34
35/*
36 * addr_to_a
37 *
38 * Returns string representation of an address of the given AF.
39 */
40static inline const char *
41addr_to_a (u_char af, void *addr)
42{
43 if (!addr)
44 return "<No address>";
45
46 switch (af)
47 {
48
49 case AF_INET:
50 return inet_ntoa (*((struct in_addr *) addr));
51
52#ifdef HAVE_IPV6
53 case AF_INET6:
54 return inet6_ntoa (*((struct in6_addr *) addr));
55#endif
56
57 default:
58 return "<Addr in unknown AF>";
59 }
60}
61
62/*
63 * prefix_addr_to_a
64 *
65 * Convience wrapper that returns a human-readable string for the
66 * address in a prefix.
67 */
68static const char *
69prefix_addr_to_a (struct prefix *prefix)
70{
71 if (!prefix)
72 return "<No address>";
73
74 return addr_to_a (prefix->family, &prefix->u.prefix);
75}
76
77/*
78 * af_addr_size
79 *
80 * The size of an address in a given address family.
81 */
82static size_t
83af_addr_size (u_char af)
84{
85 switch (af)
86 {
87
88 case AF_INET:
89 return 4;
90
91#ifdef HAVE_IPV6
92 case AF_INET6:
93 return 16;
94#endif
95
96 default:
97 assert(0);
98 return 16;
99 }
100}
101
102/*
103 * netlink_nh_info_t
104 *
105 * Holds information about a single nexthop for netlink. These info
106 * structures are transient and may contain pointers into rib
107 * data structures for convenience.
108 */
109typedef struct netlink_nh_info_t_
110{
111 uint32_t if_index;
112 union g_addr *gateway;
113
114 /*
115 * Information from the struct nexthop from which this nh was
116 * derived. For debug purposes only.
117 */
118 int recursive;
119 enum nexthop_types_t type;
120} netlink_nh_info_t;
121
122/*
123 * netlink_route_info_t
124 *
125 * A structure for holding information for a netlink route message.
126 */
127typedef struct netlink_route_info_t_
128{
129 uint16_t nlmsg_type;
130 u_char rtm_type;
131 uint32_t rtm_table;
132 u_char rtm_protocol;
133 u_char af;
134 struct prefix *prefix;
135 uint32_t *metric;
136 int num_nhs;
137
138 /*
139 * Nexthop structures. We keep things simple for now by enforcing a
140 * maximum of 64 in case MULTIPATH_NUM is 0;
141 */
142 netlink_nh_info_t nhs[MAX (MULTIPATH_NUM, 64)];
143 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
155netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop)
156{
157 netlink_nh_info_t nhi;
158 union g_addr *src;
159
160 memset (&nhi, 0, sizeof (nhi));
161 src = NULL;
162
163 if (ri->num_nhs >= (int) ZEBRA_NUM_OF (ri->nhs))
164 return 0;
165
166 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
167 {
168 nhi.recursive = 1;
169 nhi.type = nexthop->rtype;
170
171 if (nexthop->rtype == NEXTHOP_TYPE_IPV4
172 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX)
173 {
174 nhi.gateway = &nexthop->rgate;
175 if (nexthop->src.ipv4.s_addr)
176 src = &nexthop->src;
177 }
178
179#ifdef HAVE_IPV6
180 if (nexthop->rtype == NEXTHOP_TYPE_IPV6
181 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
182 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
183 {
184 nhi.gateway = &nexthop->rgate;
185 }
186#endif /* HAVE_IPV6 */
187
188 if (nexthop->rtype == NEXTHOP_TYPE_IFINDEX
189 || nexthop->rtype == NEXTHOP_TYPE_IFNAME
190 || nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
191 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFINDEX
192 || nexthop->rtype == NEXTHOP_TYPE_IPV6_IFNAME)
193 {
194 nhi.if_index = nexthop->rifindex;
195 if ((nexthop->rtype == NEXTHOP_TYPE_IPV4_IFINDEX
196 || nexthop->rtype == NEXTHOP_TYPE_IFINDEX)
197 && nexthop->src.ipv4.s_addr)
198 src = &nexthop->src;
199 }
200
201 goto done;
202 }
203
204 nhi.recursive = 0;
205 nhi.type = nexthop->type;
206
207 if (nexthop->type == NEXTHOP_TYPE_IPV4
208 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
209 {
210 nhi.gateway = &nexthop->gate;
211 if (nexthop->src.ipv4.s_addr)
212 src = &nexthop->src;
213 }
214
215#ifdef HAVE_IPV6
216 if (nexthop->type == NEXTHOP_TYPE_IPV6
217 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
218 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
219 {
220 nhi.gateway = &nexthop->gate;
221 }
222#endif /* HAVE_IPV6 */
223 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
224 || nexthop->type == NEXTHOP_TYPE_IFNAME
225 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
226 {
227 nhi.if_index = nexthop->ifindex;
228
229 if (nexthop->src.ipv4.s_addr)
230 src = &nexthop->src;
231 }
232 else if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX
233 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)
234 {
235 nhi.if_index = nexthop->ifindex;
236 }
237
238 /*
239 * Fall through...
240 */
241
242 done:
243 if (!nhi.gateway && nhi.if_index == 0)
244 return 0;
245
246 /*
247 * We have a valid nhi. Copy the structure over to the route_info.
248 */
249 ri->nhs[ri->num_nhs] = nhi;
250 ri->num_nhs++;
251
252 if (src && !ri->pref_src)
253 ri->pref_src = src;
254
255 return 1;
256}
257
258/*
259 * netlink_proto_from_route_type
260 */
261static u_char
262netlink_proto_from_route_type (int type)
263{
264 switch (type)
265 {
266 case ZEBRA_ROUTE_KERNEL:
267 case ZEBRA_ROUTE_CONNECT:
268 return RTPROT_KERNEL;
269
270 default:
271 return RTPROT_ZEBRA;
272 }
273}
274
275/*
276 * netlink_route_info_fill
277 *
278 * Fill out the route information object from the given route.
279 *
280 * Returns TRUE on success and FALSE on failure.
281 */
282static int
283netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
284 rib_dest_t *dest, struct rib *rib)
285{
286 struct nexthop *nexthop = NULL;
287 int discard;
288
289 memset (ri, 0, sizeof (*ri));
290
291 ri->prefix = rib_dest_prefix (dest);
292 ri->af = rib_dest_af (dest);
293
294 ri->nlmsg_type = cmd;
295 ri->rtm_table = rib_dest_vrf (dest)->id;
296 ri->rtm_protocol = RTPROT_UNSPEC;
297
298 /*
299 * An RTM_DELROUTE need not be accompanied by any nexthops,
300 * particularly in our communication with the FPM.
301 */
302 if (cmd == RTM_DELROUTE && !rib)
303 goto skip;
304
305 if (rib)
306 ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
307
308 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
309 discard = 1;
310 else
311 discard = 0;
312
313 if (cmd == RTM_NEWROUTE)
314 {
315 if (discard)
316 {
317 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
318 ri->rtm_type = RTN_BLACKHOLE;
319 else if (rib->flags & ZEBRA_FLAG_REJECT)
320 ri->rtm_type = RTN_UNREACHABLE;
321 else
322 assert (0);
323 }
324 else
325 ri->rtm_type = RTN_UNICAST;
326 }
327
328 ri->metric = &rib->metric;
329
330 if (discard)
331 {
332 goto skip;
333 }
334
335 /* Multipath case. */
336 if (rib->nexthop_active_num == 1 || MULTIPATH_NUM == 1)
337 {
338 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
339 {
340
341 if ((cmd == RTM_NEWROUTE
342 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
343 || (cmd == RTM_DELROUTE
344 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
345 {
346 netlink_route_info_add_nh (ri, nexthop);
347 break;
348 }
349 }
350 }
351 else
352 {
353 for (nexthop = rib->nexthop;
354 nexthop && (MULTIPATH_NUM == 0 || ri->num_nhs < MULTIPATH_NUM);
355 nexthop = nexthop->next)
356 {
357 if ((cmd == RTM_NEWROUTE
358 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
359 || (cmd == RTM_DELROUTE
360 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
361 {
362 netlink_route_info_add_nh (ri, nexthop);
363 }
364 }
365 }
366
367 /* If there is no useful nexthop then return. */
368 if (ri->num_nhs == 0)
369 {
370 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
371 return 0;
372 }
373
374 skip:
375 return 1;
376}
377
378/*
379 * netlink_route_info_encode
380 *
381 * Returns the number of bytes written to the buffer. 0 or a negative
382 * value indicates an error.
383 */
384static int
385netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
386 size_t in_buf_len)
387{
388 int bytelen;
389 int nexthop_num = 0;
390 size_t buf_offset;
391 netlink_nh_info_t *nhi;
392
393 struct
394 {
395 struct nlmsghdr n;
396 struct rtmsg r;
397 char buf[1];
398 } *req;
399
400 req = (void *) in_buf;
401
402 buf_offset = ((char *) req->buf) - ((char *) req);
403
404 if (in_buf_len < buf_offset) {
405 assert(0);
406 return 0;
407 }
408
409 memset (req, 0, buf_offset);
410
411 bytelen = af_addr_size (ri->af);
412
413 req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
414 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
415 req->n.nlmsg_type = ri->nlmsg_type;
416 req->r.rtm_family = ri->af;
417 req->r.rtm_table = ri->rtm_table;
418 req->r.rtm_dst_len = ri->prefix->prefixlen;
419 req->r.rtm_protocol = ri->rtm_protocol;
420 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
421
422 addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
423
424 req->r.rtm_type = ri->rtm_type;
425
426 /* Metric. */
427 if (ri->metric)
428 addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
429
430 if (ri->num_nhs == 0)
431 goto done;
432
433 if (ri->num_nhs == 1)
434 {
435 nhi = &ri->nhs[0];
436
437 if (nhi->gateway)
438 {
439 addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
440 bytelen);
441 }
442
443 if (nhi->if_index)
444 {
445 addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
446 }
447
448 goto done;
449
450 }
451
452 /*
453 * Multipath case.
454 */
455 char buf[NL_PKT_BUF_SIZE];
456 struct rtattr *rta = (void *) buf;
457 struct rtnexthop *rtnh;
458
459 rta->rta_type = RTA_MULTIPATH;
460 rta->rta_len = RTA_LENGTH (0);
461 rtnh = RTA_DATA (rta);
462
463 for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
464 {
465 nhi = &ri->nhs[nexthop_num];
466
467 rtnh->rtnh_len = sizeof (*rtnh);
468 rtnh->rtnh_flags = 0;
469 rtnh->rtnh_hops = 0;
470 rtnh->rtnh_ifindex = 0;
471 rta->rta_len += rtnh->rtnh_len;
472
473 if (nhi->gateway)
474 {
475 rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
476 rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
477 }
478
479 if (nhi->if_index)
480 {
481 rtnh->rtnh_ifindex = nhi->if_index;
482 }
483
484 rtnh = RTNH_NEXT (rtnh);
485 }
486
487 assert (rta->rta_len > RTA_LENGTH (0));
488 addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
489 RTA_PAYLOAD (rta));
490
491done:
492
493 if (ri->pref_src)
494 {
495 addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
496 }
497
498 assert (req->n.nlmsg_len < in_buf_len);
499 return req->n.nlmsg_len;
500}
501
502/*
503 * zfpm_log_route_info
504 *
505 * Helper function to log the information in a route_info structure.
506 */
507static void
508zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
509{
510 netlink_nh_info_t *nhi;
511 int i;
512
513 zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
514 nl_msg_type_to_str (ri->nlmsg_type),
515 prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
516 nl_rtproto_to_str (ri->rtm_protocol),
517 ri->metric ? *ri->metric : 0);
518
519 for (i = 0; i < ri->num_nhs; i++)
520 {
521 nhi = &ri->nhs[i];
522 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
523 nhi->if_index, addr_to_a (ri->af, nhi->gateway),
524 nhi->recursive ? "yes" : "no",
525 nexthop_type_to_str (nhi->type));
526 }
527}
528
529/*
530 * zfpm_netlink_encode_route
531 *
532 * Create a netlink message corresponding to the given route in the
533 * given buffer space.
534 *
535 * Returns the number of bytes written to the buffer. 0 or a negative
536 * value indicates an error.
537 */
538int
539zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
540 char *in_buf, size_t in_buf_len)
541{
542 netlink_route_info_t ri_space, *ri;
543
544 ri = &ri_space;
545
546 if (!netlink_route_info_fill (ri, cmd, dest, rib))
547 return 0;
548
549 zfpm_log_route_info (ri, __FUNCTION__);
550
551 return netlink_route_info_encode (ri, in_buf, in_buf_len);
552}