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