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