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