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