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