2 * Code for encoding/decoding FPM messages that are in netlink format.
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")
8 * This file is part of GNU Zebra.
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
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.
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
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"
43 #include "zebra/zebra_fpm_private.h"
44 #include "zebra/zebra_vxlan_private.h"
49 * Returns string representation of an address of the given AF.
51 static inline const char *addr_to_a(uint8_t af
, void *addr
)
54 return "<No address>";
59 return inet_ntoa(*((struct in_addr
*)addr
));
61 return inet6_ntoa(*((struct in6_addr
*)addr
));
63 return "<Addr in unknown AF>";
70 * Convience wrapper that returns a human-readable string for the
71 * address in a prefix.
73 static const char *prefix_addr_to_a(struct prefix
*prefix
)
76 return "<No address>";
78 return addr_to_a(prefix
->family
, &prefix
->u
.prefix
);
84 * The size of an address in a given address family.
86 static size_t af_addr_size(uint8_t af
)
101 * We plan to use RTA_ENCAP_TYPE attribute for VxLAN encap as well.
102 * Currently, values 0 to 8 for this attribute are used by lwtunnel_encap_types
103 * So, we cannot use these values for VxLAN encap.
105 enum fpm_nh_encap_type_t
{
106 FPM_NH_ENCAP_NONE
= 0,
107 FPM_NH_ENCAP_VXLAN
= 100,
112 * fpm_nh_encap_type_to_str
114 static const char *fpm_nh_encap_type_to_str(enum fpm_nh_encap_type_t encap_type
)
116 switch (encap_type
) {
117 case FPM_NH_ENCAP_NONE
:
120 case FPM_NH_ENCAP_VXLAN
:
123 case FPM_NH_ENCAP_MAX
:
130 struct vxlan_encap_info_t
{
134 enum vxlan_encap_info_type_t
{
138 struct fpm_nh_encap_info_t
{
139 enum fpm_nh_encap_type_t encap_type
;
141 struct vxlan_encap_info_t vxlan_encap
;
148 * Holds information about a single nexthop for netlink. These info
149 * structures are transient and may contain pointers into rib
150 * data structures for convenience.
152 struct netlink_nh_info
{
154 union g_addr
*gateway
;
157 * Information from the struct nexthop from which this nh was
158 * derived. For debug purposes only.
161 enum nexthop_types_t type
;
162 struct fpm_nh_encap_info_t encap_info
;
168 * A structure for holding information for a netlink route message.
170 struct netlink_route_info
{
174 uint8_t rtm_protocol
;
176 struct prefix
*prefix
;
178 unsigned int num_nhs
;
183 struct netlink_nh_info nhs
[MULTIPATH_NUM
];
184 union g_addr
*pref_src
;
188 * netlink_route_info_add_nh
190 * Add information about the given nexthop to the given route info
193 * Returns true if a nexthop was added, false otherwise.
195 static int netlink_route_info_add_nh(struct netlink_route_info
*ri
,
196 struct nexthop
*nexthop
,
197 struct route_entry
*re
)
199 struct netlink_nh_info nhi
;
201 zebra_l3vni_t
*zl3vni
= NULL
;
203 memset(&nhi
, 0, sizeof(nhi
));
206 if (ri
->num_nhs
>= (int)array_size(ri
->nhs
))
209 nhi
.recursive
= nexthop
->rparent
? 1 : 0;
210 nhi
.type
= nexthop
->type
;
211 nhi
.if_index
= nexthop
->ifindex
;
213 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
214 || nexthop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
215 nhi
.gateway
= &nexthop
->gate
;
216 if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
220 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
221 || nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
222 nhi
.gateway
= &nexthop
->gate
;
225 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
226 if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
230 if (!nhi
.gateway
&& nhi
.if_index
== 0)
233 if (re
&& CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
234 nhi
.encap_info
.encap_type
= FPM_NH_ENCAP_VXLAN
;
236 zl3vni
= zl3vni_from_vrf(nexthop
->vrf_id
);
237 if (zl3vni
&& is_l3vni_oper_up(zl3vni
)) {
239 /* Add VNI to VxLAN encap info */
240 nhi
.encap_info
.vxlan_encap
.vni
= zl3vni
->vni
;
245 * We have a valid nhi. Copy the structure over to the route_info.
247 ri
->nhs
[ri
->num_nhs
] = nhi
;
250 if (src
&& !ri
->pref_src
)
257 * netlink_proto_from_route_type
259 static uint8_t netlink_proto_from_route_type(int type
)
262 case ZEBRA_ROUTE_KERNEL
:
263 case ZEBRA_ROUTE_CONNECT
:
264 return RTPROT_KERNEL
;
272 * netlink_route_info_fill
274 * Fill out the route information object from the given route.
276 * Returns true on success and false on failure.
278 static int netlink_route_info_fill(struct netlink_route_info
*ri
, int cmd
,
279 rib_dest_t
*dest
, struct route_entry
*re
)
281 struct nexthop
*nexthop
;
283 memset(ri
, 0, sizeof(*ri
));
285 ri
->prefix
= rib_dest_prefix(dest
);
286 ri
->af
= rib_dest_af(dest
);
288 ri
->nlmsg_type
= cmd
;
289 ri
->rtm_table
= rib_table_info(rib_dest_table(dest
))->table_id
;
290 ri
->rtm_protocol
= RTPROT_UNSPEC
;
293 * An RTM_DELROUTE need not be accompanied by any nexthops,
294 * particularly in our communication with the FPM.
296 if (cmd
== RTM_DELROUTE
&& !re
)
300 zfpm_debug("%s: Expected non-NULL re pointer", __func__
);
304 ri
->rtm_protocol
= netlink_proto_from_route_type(re
->type
);
305 ri
->rtm_type
= RTN_UNICAST
;
306 ri
->metric
= &re
->metric
;
308 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
309 if (ri
->num_nhs
>= zrouter
.multipath_num
)
312 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
315 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
316 switch (nexthop
->bh_type
) {
317 case BLACKHOLE_ADMINPROHIB
:
318 ri
->rtm_type
= RTN_PROHIBIT
;
320 case BLACKHOLE_REJECT
:
321 ri
->rtm_type
= RTN_UNREACHABLE
;
325 ri
->rtm_type
= RTN_BLACKHOLE
;
330 if ((cmd
== RTM_NEWROUTE
331 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
332 || (cmd
== RTM_DELROUTE
333 && CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))) {
334 netlink_route_info_add_nh(ri
, nexthop
, re
);
338 if (ri
->num_nhs
== 0) {
339 switch (ri
->rtm_type
) {
341 case RTN_UNREACHABLE
:
345 /* If there is no useful nexthop then return. */
347 "netlink_encode_route(): No useful nexthop.");
356 * netlink_route_info_encode
358 * Returns the number of bytes written to the buffer. 0 or a negative
359 * value indicates an error.
361 static int netlink_route_info_encode(struct netlink_route_info
*ri
,
362 char *in_buf
, size_t in_buf_len
)
365 unsigned int nexthop_num
= 0;
367 struct netlink_nh_info
*nhi
;
368 enum fpm_nh_encap_type_t encap
;
369 struct rtattr
*nest
, *inner_nest
;
370 struct rtnexthop
*rtnh
;
371 struct vxlan_encap_info_t
*vxlan
;
372 struct in6_addr ipv6
;
380 req
= (void *)in_buf
;
382 buf_offset
= ((char *)req
->buf
) - ((char *)req
);
384 if (in_buf_len
< buf_offset
) {
389 memset(req
, 0, buf_offset
);
391 bytelen
= af_addr_size(ri
->af
);
393 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
394 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
395 req
->n
.nlmsg_type
= ri
->nlmsg_type
;
396 req
->r
.rtm_family
= ri
->af
;
399 * rtm_table field is a uchar field which can accomodate table_id less
401 * To support table id greater than 255, if the table_id is greater than
402 * 255, set rtm_table to RT_TABLE_UNSPEC and add RTA_TABLE attribute
403 * with 32 bit value as the table_id.
405 if (ri
->rtm_table
< 256)
406 req
->r
.rtm_table
= ri
->rtm_table
;
408 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
409 nl_attr_put32(&req
->n
, in_buf_len
, RTA_TABLE
, ri
->rtm_table
);
412 req
->r
.rtm_dst_len
= ri
->prefix
->prefixlen
;
413 req
->r
.rtm_protocol
= ri
->rtm_protocol
;
414 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
416 nl_attr_put(&req
->n
, in_buf_len
, RTA_DST
, &ri
->prefix
->u
.prefix
,
419 req
->r
.rtm_type
= ri
->rtm_type
;
423 nl_attr_put32(&req
->n
, in_buf_len
, RTA_PRIORITY
, *ri
->metric
);
425 if (ri
->num_nhs
== 0)
428 if (ri
->num_nhs
== 1) {
432 if (nhi
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
433 && ri
->af
== AF_INET6
) {
434 ipv4_to_ipv4_mapped_ipv6(&ipv6
,
436 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
439 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
440 nhi
->gateway
, bytelen
);
444 nl_attr_put32(&req
->n
, in_buf_len
, RTA_OIF
,
448 encap
= nhi
->encap_info
.encap_type
;
450 case FPM_NH_ENCAP_NONE
:
451 case FPM_NH_ENCAP_MAX
:
453 case FPM_NH_ENCAP_VXLAN
:
454 nl_attr_put16(&req
->n
, in_buf_len
, RTA_ENCAP_TYPE
,
456 vxlan
= &nhi
->encap_info
.vxlan_encap
;
457 nest
= nl_attr_nest(&req
->n
, in_buf_len
, RTA_ENCAP
);
458 nl_attr_put32(&req
->n
, in_buf_len
, VXLAN_VNI
,
460 nl_attr_nest_end(&req
->n
, nest
);
470 nest
= nl_attr_nest(&req
->n
, in_buf_len
, RTA_MULTIPATH
);
472 for (nexthop_num
= 0; nexthop_num
< ri
->num_nhs
; nexthop_num
++) {
473 rtnh
= nl_attr_rtnh(&req
->n
, in_buf_len
);
474 nhi
= &ri
->nhs
[nexthop_num
];
477 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
478 nhi
->gateway
, bytelen
);
481 rtnh
->rtnh_ifindex
= nhi
->if_index
;
484 encap
= nhi
->encap_info
.encap_type
;
486 case FPM_NH_ENCAP_NONE
:
487 case FPM_NH_ENCAP_MAX
:
489 case FPM_NH_ENCAP_VXLAN
:
490 nl_attr_put16(&req
->n
, in_buf_len
, RTA_ENCAP_TYPE
,
492 vxlan
= &nhi
->encap_info
.vxlan_encap
;
494 nl_attr_nest(&req
->n
, in_buf_len
, RTA_ENCAP
);
495 nl_attr_put32(&req
->n
, in_buf_len
, VXLAN_VNI
,
497 nl_attr_nest_end(&req
->n
, inner_nest
);
501 nl_attr_rtnh_end(&req
->n
, rtnh
);
504 nl_attr_nest_end(&req
->n
, nest
);
505 assert(nest
->rta_len
> RTA_LENGTH(0));
510 nl_attr_put(&req
->n
, in_buf_len
, RTA_PREFSRC
, &ri
->pref_src
,
514 assert(req
->n
.nlmsg_len
< in_buf_len
);
515 return req
->n
.nlmsg_len
;
519 * zfpm_log_route_info
521 * Helper function to log the information in a route_info structure.
523 static void zfpm_log_route_info(struct netlink_route_info
*ri
,
526 struct netlink_nh_info
*nhi
;
529 zfpm_debug("%s : %s %s/%d, Proto: %s, Metric: %u", label
,
530 nl_msg_type_to_str(ri
->nlmsg_type
),
531 prefix_addr_to_a(ri
->prefix
), ri
->prefix
->prefixlen
,
532 nl_rtproto_to_str(ri
->rtm_protocol
),
533 ri
->metric
? *ri
->metric
: 0);
535 for (i
= 0; i
< ri
->num_nhs
; i
++) {
537 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s, Encap type: %s",
538 nhi
->if_index
, addr_to_a(ri
->af
, nhi
->gateway
),
539 nhi
->recursive
? "yes" : "no",
540 nexthop_type_to_str(nhi
->type
),
541 fpm_nh_encap_type_to_str(nhi
->encap_info
.encap_type
)
547 * zfpm_netlink_encode_route
549 * Create a netlink message corresponding to the given route in the
550 * given buffer space.
552 * Returns the number of bytes written to the buffer. 0 or a negative
553 * value indicates an error.
555 int zfpm_netlink_encode_route(int cmd
, rib_dest_t
*dest
, struct route_entry
*re
,
556 char *in_buf
, size_t in_buf_len
)
558 struct netlink_route_info ri_space
, *ri
;
562 if (!netlink_route_info_fill(ri
, cmd
, dest
, re
))
565 zfpm_log_route_info(ri
, __func__
);
567 return netlink_route_info_encode(ri
, in_buf
, in_buf_len
);
571 * zfpm_netlink_encode_mac
573 * Create a netlink message corresponding to the given MAC.
575 * Returns the number of bytes written to the buffer. 0 or a negative
576 * value indicates an error.
578 int zfpm_netlink_encode_mac(struct fpm_mac_info_t
*mac
, char *in_buf
,
581 char buf1
[ETHER_ADDR_STRLEN
];
589 req
= (void *)in_buf
;
591 buf_offset
= offsetof(struct macmsg
, buf
);
592 if (in_buf_len
< buf_offset
)
594 memset(req
, 0, buf_offset
);
596 /* Construct nlmsg header */
597 req
->hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
598 req
->hdr
.nlmsg_type
= CHECK_FLAG(mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
) ?
599 RTM_DELNEIGH
: RTM_NEWNEIGH
;
600 req
->hdr
.nlmsg_flags
= NLM_F_REQUEST
;
601 if (req
->hdr
.nlmsg_type
== RTM_NEWNEIGH
)
602 req
->hdr
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
604 /* Construct ndmsg */
605 req
->ndm
.ndm_family
= AF_BRIDGE
;
606 req
->ndm
.ndm_ifindex
= mac
->vxlan_if
;
608 req
->ndm
.ndm_state
= NUD_REACHABLE
;
609 req
->ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
610 if (CHECK_FLAG(mac
->zebra_flags
,
611 (ZEBRA_MAC_STICKY
| ZEBRA_MAC_REMOTE_DEF_GW
)))
612 req
->ndm
.ndm_state
|= NUD_NOARP
;
614 req
->ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
617 nl_attr_put(&req
->hdr
, in_buf_len
, NDA_LLADDR
, &mac
->macaddr
, 6);
618 nl_attr_put(&req
->hdr
, in_buf_len
, NDA_DST
, &mac
->r_vtep_ip
, 4);
619 nl_attr_put32(&req
->hdr
, in_buf_len
, NDA_MASTER
, mac
->svi_if
);
620 nl_attr_put32(&req
->hdr
, in_buf_len
, NDA_VNI
, mac
->vni
);
622 assert(req
->hdr
.nlmsg_len
< in_buf_len
);
624 zfpm_debug("Tx %s family %s ifindex %u MAC %s DEST %s",
625 nl_msg_type_to_str(req
->hdr
.nlmsg_type
),
626 nl_family_to_str(req
->ndm
.ndm_family
), req
->ndm
.ndm_ifindex
,
627 prefix_mac2str(&mac
->macaddr
, buf1
, sizeof(buf1
)),
628 inet_ntoa(mac
->r_vtep_ip
));
630 return req
->hdr
.nlmsg_len
;
633 #endif /* HAVE_NETLINK */