]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_netlink.c
Merge branch stable/2.0 into stable/3.0
[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 #include "vty.h"
31 #include "prefix.h"
32
33 #include "zebra/zserv.h"
34 #include "zebra/zebra_ns.h"
35 #include "zebra/zebra_vrf.h"
36 #include "zebra/kernel_netlink.h"
37 #include "zebra/rt_netlink.h"
38 #include "nexthop.h"
39
40 #include "zebra/zebra_fpm_private.h"
41
42 /*
43 * addr_to_a
44 *
45 * Returns string representation of an address of the given AF.
46 */
47 static inline const char *
48 addr_to_a (u_char af, void *addr)
49 {
50 if (!addr)
51 return "<No address>";
52
53 switch (af)
54 {
55
56 case AF_INET:
57 return inet_ntoa (*((struct in_addr *) addr));
58 break;
59 case AF_INET6:
60 return inet6_ntoa (*((struct in6_addr *) addr));
61 break;
62 default:
63 return "<Addr in unknown AF>";
64 break;
65 }
66 }
67
68 /*
69 * prefix_addr_to_a
70 *
71 * Convience wrapper that returns a human-readable string for the
72 * address in a prefix.
73 */
74 static const char *
75 prefix_addr_to_a (struct prefix *prefix)
76 {
77 if (!prefix)
78 return "<No address>";
79
80 return addr_to_a (prefix->family, &prefix->u.prefix);
81 }
82
83 /*
84 * af_addr_size
85 *
86 * The size of an address in a given address family.
87 */
88 static size_t
89 af_addr_size (u_char af)
90 {
91 switch (af)
92 {
93
94 case AF_INET:
95 return 4;
96 break;
97 case AF_INET6:
98 return 16;
99 break;
100 default:
101 assert(0);
102 return 16;
103 }
104 }
105
106 /*
107 * netlink_nh_info_t
108 *
109 * Holds information about a single nexthop for netlink. These info
110 * structures are transient and may contain pointers into rib
111 * data structures for convenience.
112 */
113 typedef struct netlink_nh_info_t_
114 {
115 uint32_t if_index;
116 union g_addr *gateway;
117
118 /*
119 * Information from the struct nexthop from which this nh was
120 * derived. For debug purposes only.
121 */
122 int recursive;
123 enum nexthop_types_t type;
124 } netlink_nh_info_t;
125
126 /*
127 * netlink_route_info_t
128 *
129 * A structure for holding information for a netlink route message.
130 */
131 typedef struct netlink_route_info_t_
132 {
133 uint16_t nlmsg_type;
134 u_char rtm_type;
135 uint32_t rtm_table;
136 u_char rtm_protocol;
137 u_char af;
138 struct prefix *prefix;
139 uint32_t *metric;
140 unsigned int num_nhs;
141
142 /*
143 * Nexthop structures
144 */
145 netlink_nh_info_t nhs[MULTIPATH_NUM];
146 union g_addr *pref_src;
147 } netlink_route_info_t;
148
149 /*
150 * netlink_route_info_add_nh
151 *
152 * Add information about the given nexthop to the given route info
153 * structure.
154 *
155 * Returns TRUE if a nexthop was added, FALSE otherwise.
156 */
157 static int
158 netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop,
159 int recursive)
160 {
161 netlink_nh_info_t nhi;
162 union g_addr *src;
163
164 memset (&nhi, 0, sizeof (nhi));
165 src = NULL;
166
167 if (ri->num_nhs >= (int) ZEBRA_NUM_OF (ri->nhs))
168 return 0;
169
170 nhi.recursive = recursive;
171 nhi.type = nexthop->type;
172 nhi.if_index = nexthop->ifindex;
173
174 if (nexthop->type == NEXTHOP_TYPE_IPV4
175 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
176 {
177 nhi.gateway = &nexthop->gate;
178 if (nexthop->src.ipv4.s_addr)
179 src = &nexthop->src;
180 }
181
182 if (nexthop->type == NEXTHOP_TYPE_IPV6
183 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
184 {
185 nhi.gateway = &nexthop->gate;
186 }
187
188 if (nexthop->type == NEXTHOP_TYPE_IFINDEX)
189 {
190 if (nexthop->src.ipv4.s_addr)
191 src = &nexthop->src;
192 }
193
194 if (!nhi.gateway && nhi.if_index == 0)
195 return 0;
196
197 /*
198 * We have a valid nhi. Copy the structure over to the route_info.
199 */
200 ri->nhs[ri->num_nhs] = nhi;
201 ri->num_nhs++;
202
203 if (src && !ri->pref_src)
204 ri->pref_src = src;
205
206 return 1;
207 }
208
209 /*
210 * netlink_proto_from_route_type
211 */
212 static u_char
213 netlink_proto_from_route_type (int type)
214 {
215 switch (type)
216 {
217 case ZEBRA_ROUTE_KERNEL:
218 case ZEBRA_ROUTE_CONNECT:
219 return RTPROT_KERNEL;
220
221 default:
222 return RTPROT_ZEBRA;
223 }
224 }
225
226 /*
227 * netlink_route_info_fill
228 *
229 * Fill out the route information object from the given route.
230 *
231 * Returns TRUE on success and FALSE on failure.
232 */
233 static int
234 netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
235 rib_dest_t *dest, struct rib *rib)
236 {
237 struct nexthop *nexthop, *tnexthop;
238 int recursing;
239 int discard;
240
241 memset (ri, 0, sizeof (*ri));
242
243 ri->prefix = rib_dest_prefix (dest);
244 ri->af = rib_dest_af (dest);
245
246 ri->nlmsg_type = cmd;
247 ri->rtm_table = zvrf_id (rib_dest_vrf (dest));
248 ri->rtm_protocol = RTPROT_UNSPEC;
249
250 /*
251 * An RTM_DELROUTE need not be accompanied by any nexthops,
252 * particularly in our communication with the FPM.
253 */
254 if (cmd == RTM_DELROUTE && !rib)
255 return 1;
256
257 if (!rib)
258 {
259 zfpm_debug ("%s: Expected non-NULL rib pointer", __PRETTY_FUNCTION__);
260 return 0;
261 }
262
263 ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
264
265 if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
266 discard = 1;
267 else
268 discard = 0;
269
270 if (cmd == RTM_NEWROUTE)
271 {
272 if (discard)
273 {
274 if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
275 ri->rtm_type = RTN_BLACKHOLE;
276 else if (rib->flags & ZEBRA_FLAG_REJECT)
277 ri->rtm_type = RTN_UNREACHABLE;
278 else
279 assert (0);
280 }
281 else
282 ri->rtm_type = RTN_UNICAST;
283 }
284
285 ri->metric = &rib->metric;
286
287 if (discard)
288 return 1;
289
290 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
291 {
292 if (ri->num_nhs >= multipath_num)
293 break;
294
295 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
296 continue;
297
298 if ((cmd == RTM_NEWROUTE
299 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
300 || (cmd == RTM_DELROUTE
301 && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
302 {
303 netlink_route_info_add_nh (ri, nexthop, recursing);
304 }
305 }
306
307 /* If there is no useful nexthop then return. */
308 if (ri->num_nhs == 0)
309 {
310 zfpm_debug ("netlink_encode_route(): No useful nexthop.");
311 return 0;
312 }
313
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 size_t bytelen;
328 unsigned 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 unsigned 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 }