]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_netlink.c
nexthop-tracking.patch
[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
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 #include "nexthop.h"
33
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 */
41 static inline const char *
42 addr_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 */
69 static const char *
70 prefix_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 */
83 static size_t
84 af_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 */
110 typedef 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 */
128 typedef 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 /*
140 * Nexthop structures. We keep things simple for now by enforcing a
141 * maximum of 64 in case MULTIPATH_NUM is 0;
142 */
143 netlink_nh_info_t nhs[MAX (MULTIPATH_NUM, 64)];
144 union g_addr *pref_src;
145 } netlink_route_info_t;
146
147 /*
148 * netlink_route_info_add_nh
149 *
150 * Add information about the given nexthop to the given route info
151 * structure.
152 *
153 * Returns TRUE if a nexthop was added, FALSE otherwise.
154 */
155 static int
156 netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop,
157 int recursive)
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 = recursive;
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 #ifdef HAVE_IPV6
181 if (nexthop->type == NEXTHOP_TYPE_IPV6
182 || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
183 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
184 {
185 nhi.gateway = &nexthop->gate;
186 }
187 #endif /* HAVE_IPV6 */
188
189 if (nexthop->type == NEXTHOP_TYPE_IFINDEX
190 || nexthop->type == NEXTHOP_TYPE_IFNAME)
191 {
192 if (nexthop->src.ipv4.s_addr)
193 src = &nexthop->src;
194 }
195
196 if (!nhi.gateway && nhi.if_index == 0)
197 return 0;
198
199 /*
200 * We have a valid nhi. Copy the structure over to the route_info.
201 */
202 ri->nhs[ri->num_nhs] = nhi;
203 ri->num_nhs++;
204
205 if (src && !ri->pref_src)
206 ri->pref_src = src;
207
208 return 1;
209 }
210
211 /*
212 * netlink_proto_from_route_type
213 */
214 static u_char
215 netlink_proto_from_route_type (int type)
216 {
217 switch (type)
218 {
219 case ZEBRA_ROUTE_KERNEL:
220 case ZEBRA_ROUTE_CONNECT:
221 return RTPROT_KERNEL;
222
223 default:
224 return RTPROT_ZEBRA;
225 }
226 }
227
228 /*
229 * netlink_route_info_fill
230 *
231 * Fill out the route information object from the given route.
232 *
233 * Returns TRUE on success and FALSE on failure.
234 */
235 static int
236 netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
237 rib_dest_t *dest, struct rib *rib)
238 {
239 struct nexthop *nexthop, *tnexthop;
240 int recursing;
241 int discard;
242
243 memset (ri, 0, sizeof (*ri));
244
245 ri->prefix = rib_dest_prefix (dest);
246 ri->af = rib_dest_af (dest);
247
248 ri->nlmsg_type = cmd;
249 ri->rtm_table = rib_dest_vrf (dest)->id;
250 ri->rtm_protocol = RTPROT_UNSPEC;
251
252 /*
253 * An RTM_DELROUTE need not be accompanied by any nexthops,
254 * particularly in our communication with the FPM.
255 */
256 if (cmd == RTM_DELROUTE && !rib)
257 goto skip;
258
259 if (rib)
260 ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
261
262 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->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 (rib->flags & ZEBRA_FLAG_BLACKHOLE)
272 ri->rtm_type = RTN_BLACKHOLE;
273 else if (rib->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 = &rib->metric;
283
284 if (discard)
285 {
286 goto skip;
287 }
288
289 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
290 {
291 if (MULTIPATH_NUM != 0 && ri->num_nhs >= MULTIPATH_NUM)
292 break;
293
294 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
295 continue;
296
297 if ((cmd == RTM_NEWROUTE
298 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
299 || (cmd == RTM_DELROUTE
300 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
301 {
302 netlink_route_info_add_nh (ri, nexthop, recursing);
303 }
304 }
305
306 /* If there is no useful nexthop then return. */
307 if (ri->num_nhs == 0)
308 {
309 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
310 return 0;
311 }
312
313 skip:
314 return 1;
315 }
316
317 /*
318 * netlink_route_info_encode
319 *
320 * Returns the number of bytes written to the buffer. 0 or a negative
321 * value indicates an error.
322 */
323 static int
324 netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
325 size_t in_buf_len)
326 {
327 int bytelen;
328 int nexthop_num = 0;
329 size_t buf_offset;
330 netlink_nh_info_t *nhi;
331
332 struct
333 {
334 struct nlmsghdr n;
335 struct rtmsg r;
336 char buf[1];
337 } *req;
338
339 req = (void *) in_buf;
340
341 buf_offset = ((char *) req->buf) - ((char *) req);
342
343 if (in_buf_len < buf_offset) {
344 assert(0);
345 return 0;
346 }
347
348 memset (req, 0, buf_offset);
349
350 bytelen = af_addr_size (ri->af);
351
352 req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
353 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
354 req->n.nlmsg_type = ri->nlmsg_type;
355 req->r.rtm_family = ri->af;
356 req->r.rtm_table = ri->rtm_table;
357 req->r.rtm_dst_len = ri->prefix->prefixlen;
358 req->r.rtm_protocol = ri->rtm_protocol;
359 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
360
361 addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
362
363 req->r.rtm_type = ri->rtm_type;
364
365 /* Metric. */
366 if (ri->metric)
367 addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
368
369 if (ri->num_nhs == 0)
370 goto done;
371
372 if (ri->num_nhs == 1)
373 {
374 nhi = &ri->nhs[0];
375
376 if (nhi->gateway)
377 {
378 addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
379 bytelen);
380 }
381
382 if (nhi->if_index)
383 {
384 addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
385 }
386
387 goto done;
388
389 }
390
391 /*
392 * Multipath case.
393 */
394 char buf[NL_PKT_BUF_SIZE];
395 struct rtattr *rta = (void *) buf;
396 struct rtnexthop *rtnh;
397
398 rta->rta_type = RTA_MULTIPATH;
399 rta->rta_len = RTA_LENGTH (0);
400 rtnh = RTA_DATA (rta);
401
402 for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
403 {
404 nhi = &ri->nhs[nexthop_num];
405
406 rtnh->rtnh_len = sizeof (*rtnh);
407 rtnh->rtnh_flags = 0;
408 rtnh->rtnh_hops = 0;
409 rtnh->rtnh_ifindex = 0;
410 rta->rta_len += rtnh->rtnh_len;
411
412 if (nhi->gateway)
413 {
414 rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
415 rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
416 }
417
418 if (nhi->if_index)
419 {
420 rtnh->rtnh_ifindex = nhi->if_index;
421 }
422
423 rtnh = RTNH_NEXT (rtnh);
424 }
425
426 assert (rta->rta_len > RTA_LENGTH (0));
427 addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
428 RTA_PAYLOAD (rta));
429
430 done:
431
432 if (ri->pref_src)
433 {
434 addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
435 }
436
437 assert (req->n.nlmsg_len < in_buf_len);
438 return req->n.nlmsg_len;
439 }
440
441 /*
442 * zfpm_log_route_info
443 *
444 * Helper function to log the information in a route_info structure.
445 */
446 static void
447 zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
448 {
449 netlink_nh_info_t *nhi;
450 int i;
451
452 zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
453 nl_msg_type_to_str (ri->nlmsg_type),
454 prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
455 nl_rtproto_to_str (ri->rtm_protocol),
456 ri->metric ? *ri->metric : 0);
457
458 for (i = 0; i < ri->num_nhs; i++)
459 {
460 nhi = &ri->nhs[i];
461 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
462 nhi->if_index, addr_to_a (ri->af, nhi->gateway),
463 nhi->recursive ? "yes" : "no",
464 nexthop_type_to_str (nhi->type));
465 }
466 }
467
468 /*
469 * zfpm_netlink_encode_route
470 *
471 * Create a netlink message corresponding to the given route in the
472 * given buffer space.
473 *
474 * Returns the number of bytes written to the buffer. 0 or a negative
475 * value indicates an error.
476 */
477 int
478 zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
479 char *in_buf, size_t in_buf_len)
480 {
481 netlink_route_info_t ri_space, *ri;
482
483 ri = &ri_space;
484
485 if (!netlink_route_info_fill (ri, cmd, dest, rib))
486 return 0;
487
488 zfpm_log_route_info (ri, __FUNCTION__);
489
490 return netlink_route_info_encode (ri, in_buf, in_buf_len);
491 }