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