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