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