]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_fpm_netlink.c
Merge pull request #8317 from mjstapp/fix_short_printfrr_buf
[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 #include "zebra/zebra_vxlan_private.h"
45
46 /*
47 * af_addr_size
48 *
49 * The size of an address in a given address family.
50 */
51 static size_t af_addr_size(uint8_t af)
52 {
53 switch (af) {
54
55 case AF_INET:
56 return 4;
57 case AF_INET6:
58 return 16;
59 default:
60 assert(0);
61 return 16;
62 }
63 }
64
65 /*
66 * We plan to use RTA_ENCAP_TYPE attribute for VxLAN encap as well.
67 * Currently, values 0 to 8 for this attribute are used by lwtunnel_encap_types
68 * So, we cannot use these values for VxLAN encap.
69 */
70 enum fpm_nh_encap_type_t {
71 FPM_NH_ENCAP_NONE = 0,
72 FPM_NH_ENCAP_VXLAN = 100,
73 FPM_NH_ENCAP_MAX,
74 };
75
76 /*
77 * fpm_nh_encap_type_to_str
78 */
79 static const char *fpm_nh_encap_type_to_str(enum fpm_nh_encap_type_t encap_type)
80 {
81 switch (encap_type) {
82 case FPM_NH_ENCAP_NONE:
83 return "none";
84
85 case FPM_NH_ENCAP_VXLAN:
86 return "VxLAN";
87
88 case FPM_NH_ENCAP_MAX:
89 return "invalid";
90 }
91
92 return "invalid";
93 }
94
95 struct vxlan_encap_info_t {
96 vni_t vni;
97 };
98
99 enum vxlan_encap_info_type_t {
100 VXLAN_VNI = 0,
101 };
102
103 struct fpm_nh_encap_info_t {
104 enum fpm_nh_encap_type_t encap_type;
105 union {
106 struct vxlan_encap_info_t vxlan_encap;
107 };
108 };
109
110 /*
111 * netlink_nh_info
112 *
113 * Holds information about a single nexthop for netlink. These info
114 * structures are transient and may contain pointers into rib
115 * data structures for convenience.
116 */
117 struct netlink_nh_info {
118 uint32_t if_index;
119 union g_addr *gateway;
120
121 /*
122 * Information from the struct nexthop from which this nh was
123 * derived. For debug purposes only.
124 */
125 int recursive;
126 enum nexthop_types_t type;
127 struct fpm_nh_encap_info_t encap_info;
128 };
129
130 /*
131 * netlink_route_info
132 *
133 * A structure for holding information for a netlink route message.
134 */
135 struct netlink_route_info {
136 uint32_t nlmsg_pid;
137 uint16_t nlmsg_type;
138 uint8_t rtm_type;
139 uint32_t rtm_table;
140 uint8_t rtm_protocol;
141 uint8_t af;
142 struct prefix *prefix;
143 uint32_t *metric;
144 unsigned int num_nhs;
145
146 /*
147 * Nexthop structures
148 */
149 struct netlink_nh_info nhs[MULTIPATH_NUM];
150 union g_addr *pref_src;
151 };
152
153 /*
154 * netlink_route_info_add_nh
155 *
156 * Add information about the given nexthop to the given route info
157 * structure.
158 *
159 * Returns true if a nexthop was added, false otherwise.
160 */
161 static int netlink_route_info_add_nh(struct netlink_route_info *ri,
162 struct nexthop *nexthop,
163 struct route_entry *re)
164 {
165 struct netlink_nh_info nhi;
166 union g_addr *src;
167 zebra_l3vni_t *zl3vni = NULL;
168
169 memset(&nhi, 0, sizeof(nhi));
170 src = NULL;
171
172 if (ri->num_nhs >= (int)array_size(ri->nhs))
173 return 0;
174
175 nhi.recursive = nexthop->rparent ? 1 : 0;
176 nhi.type = nexthop->type;
177 nhi.if_index = nexthop->ifindex;
178
179 if (nexthop->type == NEXTHOP_TYPE_IPV4
180 || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) {
181 nhi.gateway = &nexthop->gate;
182 if (nexthop->src.ipv4.s_addr != INADDR_ANY)
183 src = &nexthop->src;
184 }
185
186 if (nexthop->type == NEXTHOP_TYPE_IPV6
187 || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX) {
188 nhi.gateway = &nexthop->gate;
189 }
190
191 if (nexthop->type == NEXTHOP_TYPE_IFINDEX) {
192 if (nexthop->src.ipv4.s_addr != INADDR_ANY)
193 src = &nexthop->src;
194 }
195
196 if (!nhi.gateway && nhi.if_index == 0)
197 return 0;
198
199 if (re && CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)) {
200 nhi.encap_info.encap_type = FPM_NH_ENCAP_VXLAN;
201
202 zl3vni = zl3vni_from_vrf(nexthop->vrf_id);
203 if (zl3vni && is_l3vni_oper_up(zl3vni)) {
204
205 /* Add VNI to VxLAN encap info */
206 nhi.encap_info.vxlan_encap.vni = zl3vni->vni;
207 }
208 }
209
210 /*
211 * We have a valid nhi. Copy the structure over to the route_info.
212 */
213 ri->nhs[ri->num_nhs] = nhi;
214 ri->num_nhs++;
215
216 if (src && !ri->pref_src)
217 ri->pref_src = src;
218
219 return 1;
220 }
221
222 /*
223 * netlink_proto_from_route_type
224 */
225 static uint8_t netlink_proto_from_route_type(int type)
226 {
227 switch (type) {
228 case ZEBRA_ROUTE_KERNEL:
229 case ZEBRA_ROUTE_CONNECT:
230 return RTPROT_KERNEL;
231
232 default:
233 return RTPROT_ZEBRA;
234 }
235 }
236
237 /*
238 * netlink_route_info_fill
239 *
240 * Fill out the route information object from the given route.
241 *
242 * Returns true on success and false on failure.
243 */
244 static int netlink_route_info_fill(struct netlink_route_info *ri, int cmd,
245 rib_dest_t *dest, struct route_entry *re)
246 {
247 struct nexthop *nexthop;
248 struct rib_table_info *table_info =
249 rib_table_info(rib_dest_table(dest));
250 struct zebra_vrf *zvrf = table_info->zvrf;
251
252 memset(ri, 0, sizeof(*ri));
253
254 ri->prefix = rib_dest_prefix(dest);
255 ri->af = rib_dest_af(dest);
256
257 if (zvrf && zvrf->zns)
258 ri->nlmsg_pid = zvrf->zns->netlink_dplane.snl.nl_pid;
259
260 ri->nlmsg_type = cmd;
261 ri->rtm_table = table_info->table_id;
262 ri->rtm_protocol = RTPROT_UNSPEC;
263
264 /*
265 * An RTM_DELROUTE need not be accompanied by any nexthops,
266 * particularly in our communication with the FPM.
267 */
268 if (cmd == RTM_DELROUTE && !re)
269 return 1;
270
271 if (!re) {
272 zfpm_debug("%s: Expected non-NULL re pointer", __func__);
273 return 0;
274 }
275
276 ri->rtm_protocol = netlink_proto_from_route_type(re->type);
277 ri->rtm_type = RTN_UNICAST;
278 ri->metric = &re->metric;
279
280 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) {
281 if (ri->num_nhs >= zrouter.multipath_num)
282 break;
283
284 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
285 continue;
286
287 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
288 switch (nexthop->bh_type) {
289 case BLACKHOLE_ADMINPROHIB:
290 ri->rtm_type = RTN_PROHIBIT;
291 break;
292 case BLACKHOLE_REJECT:
293 ri->rtm_type = RTN_UNREACHABLE;
294 break;
295 case BLACKHOLE_NULL:
296 default:
297 ri->rtm_type = RTN_BLACKHOLE;
298 break;
299 }
300 }
301
302 if ((cmd == RTM_NEWROUTE
303 && CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
304 || (cmd == RTM_DELROUTE
305 && CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))) {
306 netlink_route_info_add_nh(ri, nexthop, re);
307 }
308 }
309
310 if (ri->num_nhs == 0) {
311 switch (ri->rtm_type) {
312 case RTN_PROHIBIT:
313 case RTN_UNREACHABLE:
314 case RTN_BLACKHOLE:
315 break;
316 default:
317 /* If there is no useful nexthop then return. */
318 zfpm_debug(
319 "netlink_encode_route(): No useful nexthop.");
320 return 0;
321 }
322 }
323
324 return 1;
325 }
326
327 /*
328 * netlink_route_info_encode
329 *
330 * Returns the number of bytes written to the buffer. 0 or a negative
331 * value indicates an error.
332 */
333 static int netlink_route_info_encode(struct netlink_route_info *ri,
334 char *in_buf, size_t in_buf_len)
335 {
336 size_t bytelen;
337 unsigned int nexthop_num = 0;
338 size_t buf_offset;
339 struct netlink_nh_info *nhi;
340 enum fpm_nh_encap_type_t encap;
341 struct rtattr *nest, *inner_nest;
342 struct rtnexthop *rtnh;
343 struct vxlan_encap_info_t *vxlan;
344 struct in6_addr ipv6;
345
346 struct {
347 struct nlmsghdr n;
348 struct rtmsg r;
349 char buf[1];
350 } * req;
351
352 req = (void *)in_buf;
353
354 buf_offset = ((char *)req->buf) - ((char *)req);
355
356 if (in_buf_len < buf_offset) {
357 assert(0);
358 return 0;
359 }
360
361 memset(req, 0, buf_offset);
362
363 bytelen = af_addr_size(ri->af);
364
365 req->n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
366 req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
367 req->n.nlmsg_pid = ri->nlmsg_pid;
368 req->n.nlmsg_type = ri->nlmsg_type;
369 req->r.rtm_family = ri->af;
370
371 /*
372 * rtm_table field is a uchar field which can accomodate table_id less
373 * than 256.
374 * To support table id greater than 255, if the table_id is greater than
375 * 255, set rtm_table to RT_TABLE_UNSPEC and add RTA_TABLE attribute
376 * with 32 bit value as the table_id.
377 */
378 if (ri->rtm_table < 256)
379 req->r.rtm_table = ri->rtm_table;
380 else {
381 req->r.rtm_table = RT_TABLE_UNSPEC;
382 nl_attr_put32(&req->n, in_buf_len, RTA_TABLE, ri->rtm_table);
383 }
384
385 req->r.rtm_dst_len = ri->prefix->prefixlen;
386 req->r.rtm_protocol = ri->rtm_protocol;
387 req->r.rtm_scope = RT_SCOPE_UNIVERSE;
388
389 nl_attr_put(&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix,
390 bytelen);
391
392 req->r.rtm_type = ri->rtm_type;
393
394 /* Metric. */
395 if (ri->metric)
396 nl_attr_put32(&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
397
398 if (ri->num_nhs == 0)
399 goto done;
400
401 if (ri->num_nhs == 1) {
402 nhi = &ri->nhs[0];
403
404 if (nhi->gateway) {
405 if (nhi->type == NEXTHOP_TYPE_IPV4_IFINDEX
406 && ri->af == AF_INET6) {
407 ipv4_to_ipv4_mapped_ipv6(&ipv6,
408 nhi->gateway->ipv4);
409 nl_attr_put(&req->n, in_buf_len, RTA_GATEWAY,
410 &ipv6, bytelen);
411 } else
412 nl_attr_put(&req->n, in_buf_len, RTA_GATEWAY,
413 nhi->gateway, bytelen);
414 }
415
416 if (nhi->if_index) {
417 nl_attr_put32(&req->n, in_buf_len, RTA_OIF,
418 nhi->if_index);
419 }
420
421 encap = nhi->encap_info.encap_type;
422 switch (encap) {
423 case FPM_NH_ENCAP_NONE:
424 case FPM_NH_ENCAP_MAX:
425 break;
426 case FPM_NH_ENCAP_VXLAN:
427 nl_attr_put16(&req->n, in_buf_len, RTA_ENCAP_TYPE,
428 encap);
429 vxlan = &nhi->encap_info.vxlan_encap;
430 nest = nl_attr_nest(&req->n, in_buf_len, RTA_ENCAP);
431 nl_attr_put32(&req->n, in_buf_len, VXLAN_VNI,
432 vxlan->vni);
433 nl_attr_nest_end(&req->n, nest);
434 break;
435 }
436
437 goto done;
438 }
439
440 /*
441 * Multipath case.
442 */
443 nest = nl_attr_nest(&req->n, in_buf_len, RTA_MULTIPATH);
444
445 for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++) {
446 rtnh = nl_attr_rtnh(&req->n, in_buf_len);
447 nhi = &ri->nhs[nexthop_num];
448
449 if (nhi->gateway)
450 nl_attr_put(&req->n, in_buf_len, RTA_GATEWAY,
451 nhi->gateway, bytelen);
452
453 if (nhi->if_index) {
454 rtnh->rtnh_ifindex = nhi->if_index;
455 }
456
457 encap = nhi->encap_info.encap_type;
458 switch (encap) {
459 case FPM_NH_ENCAP_NONE:
460 case FPM_NH_ENCAP_MAX:
461 break;
462 case FPM_NH_ENCAP_VXLAN:
463 nl_attr_put16(&req->n, in_buf_len, RTA_ENCAP_TYPE,
464 encap);
465 vxlan = &nhi->encap_info.vxlan_encap;
466 inner_nest =
467 nl_attr_nest(&req->n, in_buf_len, RTA_ENCAP);
468 nl_attr_put32(&req->n, in_buf_len, VXLAN_VNI,
469 vxlan->vni);
470 nl_attr_nest_end(&req->n, inner_nest);
471 break;
472 }
473
474 nl_attr_rtnh_end(&req->n, rtnh);
475 }
476
477 nl_attr_nest_end(&req->n, nest);
478 assert(nest->rta_len > RTA_LENGTH(0));
479
480 done:
481
482 if (ri->pref_src) {
483 nl_attr_put(&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src,
484 bytelen);
485 }
486
487 assert(req->n.nlmsg_len < in_buf_len);
488 return req->n.nlmsg_len;
489 }
490
491 /*
492 * zfpm_log_route_info
493 *
494 * Helper function to log the information in a route_info structure.
495 */
496 static void zfpm_log_route_info(struct netlink_route_info *ri,
497 const char *label)
498 {
499 struct netlink_nh_info *nhi;
500 unsigned int i;
501 char buf[PREFIX_STRLEN];
502
503 zfpm_debug("%s : %s %pFX, Proto: %s, Metric: %u", label,
504 nl_msg_type_to_str(ri->nlmsg_type), ri->prefix,
505 nl_rtproto_to_str(ri->rtm_protocol),
506 ri->metric ? *ri->metric : 0);
507
508 for (i = 0; i < ri->num_nhs; i++) {
509 nhi = &ri->nhs[i];
510
511 if (ri->af == AF_INET)
512 inet_ntop(AF_INET, &nhi->gateway, buf, sizeof(buf));
513 else
514 inet_ntop(AF_INET6, &nhi->gateway, buf, sizeof(buf));
515
516 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s, Encap type: %s",
517 nhi->if_index, buf, nhi->recursive ? "yes" : "no",
518 nexthop_type_to_str(nhi->type),
519 fpm_nh_encap_type_to_str(nhi->encap_info.encap_type)
520 );
521 }
522 }
523
524 /*
525 * zfpm_netlink_encode_route
526 *
527 * Create a netlink message corresponding to the given route in the
528 * given buffer space.
529 *
530 * Returns the number of bytes written to the buffer. 0 or a negative
531 * value indicates an error.
532 */
533 int zfpm_netlink_encode_route(int cmd, rib_dest_t *dest, struct route_entry *re,
534 char *in_buf, size_t in_buf_len)
535 {
536 struct netlink_route_info ri_space, *ri;
537
538 ri = &ri_space;
539
540 if (!netlink_route_info_fill(ri, cmd, dest, re))
541 return 0;
542
543 zfpm_log_route_info(ri, __func__);
544
545 return netlink_route_info_encode(ri, in_buf, in_buf_len);
546 }
547
548 /*
549 * zfpm_netlink_encode_mac
550 *
551 * Create a netlink message corresponding to the given MAC.
552 *
553 * Returns the number of bytes written to the buffer. 0 or a negative
554 * value indicates an error.
555 */
556 int zfpm_netlink_encode_mac(struct fpm_mac_info_t *mac, char *in_buf,
557 size_t in_buf_len)
558 {
559 size_t buf_offset;
560
561 struct macmsg {
562 struct nlmsghdr hdr;
563 struct ndmsg ndm;
564 char buf[0];
565 } *req;
566 req = (void *)in_buf;
567
568 buf_offset = offsetof(struct macmsg, buf);
569 if (in_buf_len < buf_offset)
570 return 0;
571 memset(req, 0, buf_offset);
572
573 /* Construct nlmsg header */
574 req->hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg));
575 req->hdr.nlmsg_type = CHECK_FLAG(mac->fpm_flags, ZEBRA_MAC_DELETE_FPM) ?
576 RTM_DELNEIGH : RTM_NEWNEIGH;
577 req->hdr.nlmsg_flags = NLM_F_REQUEST;
578 if (req->hdr.nlmsg_type == RTM_NEWNEIGH)
579 req->hdr.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
580
581 /* Construct ndmsg */
582 req->ndm.ndm_family = AF_BRIDGE;
583 req->ndm.ndm_ifindex = mac->vxlan_if;
584
585 req->ndm.ndm_state = NUD_REACHABLE;
586 req->ndm.ndm_flags |= NTF_SELF | NTF_MASTER;
587 if (CHECK_FLAG(mac->zebra_flags,
588 (ZEBRA_MAC_STICKY | ZEBRA_MAC_REMOTE_DEF_GW)))
589 req->ndm.ndm_state |= NUD_NOARP;
590 else
591 req->ndm.ndm_flags |= NTF_EXT_LEARNED;
592
593 /* Add attributes */
594 nl_attr_put(&req->hdr, in_buf_len, NDA_LLADDR, &mac->macaddr, 6);
595 nl_attr_put(&req->hdr, in_buf_len, NDA_DST, &mac->r_vtep_ip, 4);
596 nl_attr_put32(&req->hdr, in_buf_len, NDA_MASTER, mac->svi_if);
597 nl_attr_put32(&req->hdr, in_buf_len, NDA_VNI, mac->vni);
598
599 assert(req->hdr.nlmsg_len < in_buf_len);
600
601 zfpm_debug("Tx %s family %s ifindex %u MAC %pEA DEST %pI4",
602 nl_msg_type_to_str(req->hdr.nlmsg_type),
603 nl_family_to_str(req->ndm.ndm_family), req->ndm.ndm_ifindex,
604 &mac->macaddr, &mac->r_vtep_ip);
605
606 return req->hdr.nlmsg_len;
607 }
608
609 #endif /* HAVE_NETLINK */