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"
45 #include "zebra/interface.h"
50 * The size of an address in a given address family.
52 static size_t af_addr_size(uint8_t af
)
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.
71 enum fpm_nh_encap_type_t
{
72 FPM_NH_ENCAP_NONE
= 0,
73 FPM_NH_ENCAP_VXLAN
= 100,
78 * fpm_nh_encap_type_to_str
80 static const char *fpm_nh_encap_type_to_str(enum fpm_nh_encap_type_t encap_type
)
83 case FPM_NH_ENCAP_NONE
:
86 case FPM_NH_ENCAP_VXLAN
:
89 case FPM_NH_ENCAP_MAX
:
96 struct vxlan_encap_info_t
{
100 enum vxlan_encap_info_type_t
{
104 struct fpm_nh_encap_info_t
{
105 enum fpm_nh_encap_type_t encap_type
;
107 struct vxlan_encap_info_t vxlan_encap
;
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.
118 struct netlink_nh_info
{
119 /* Weight of the nexthop ( for unequal cost ECMP ) */
122 union g_addr
*gateway
;
125 * Information from the struct nexthop from which this nh was
126 * derived. For debug purposes only.
129 enum nexthop_types_t type
;
130 struct fpm_nh_encap_info_t encap_info
;
136 * A structure for holding information for a netlink route message.
138 struct netlink_route_info
{
143 uint8_t rtm_protocol
;
145 struct prefix
*prefix
;
147 unsigned int num_nhs
;
152 struct netlink_nh_info nhs
[MULTIPATH_NUM
];
153 union g_addr
*pref_src
;
157 * netlink_route_info_add_nh
159 * Add information about the given nexthop to the given route info
162 * Returns true if a nexthop was added, false otherwise.
164 static int netlink_route_info_add_nh(struct netlink_route_info
*ri
,
165 struct nexthop
*nexthop
,
166 struct route_entry
*re
)
168 struct netlink_nh_info nhi
;
170 struct zebra_vrf
*zvrf
= NULL
;
171 struct interface
*ifp
= NULL
, *link_if
= NULL
;
172 struct zebra_if
*zif
= NULL
;
175 memset(&nhi
, 0, sizeof(nhi
));
178 if (ri
->num_nhs
>= (int)array_size(ri
->nhs
))
181 nhi
.recursive
= nexthop
->rparent
? 1 : 0;
182 nhi
.type
= nexthop
->type
;
183 nhi
.if_index
= nexthop
->ifindex
;
184 nhi
.weight
= nexthop
->weight
;
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
)
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
197 if (ri
->af
== AF_INET
)
198 nhi
.gateway
= &ipv4ll_gateway
;
200 nhi
.gateway
= &nexthop
->gate
;
203 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
) {
204 if (nexthop
->src
.ipv4
.s_addr
!= INADDR_ANY
)
208 if (!nhi
.gateway
&& nhi
.if_index
== 0)
211 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN
)) {
212 nhi
.encap_info
.encap_type
= FPM_NH_ENCAP_VXLAN
;
214 /* Extract VNI id for the nexthop SVI interface */
215 zvrf
= zebra_vrf_lookup_by_id(nexthop
->vrf_id
);
217 ifp
= if_lookup_by_index_per_ns(zvrf
->zns
,
220 zif
= (struct zebra_if
*)ifp
->info
;
222 if (IS_ZEBRA_IF_BRIDGE(ifp
))
224 else if (IS_ZEBRA_IF_VLAN(ifp
))
226 if_lookup_by_index_per_ns(
230 vni
= vni_id_from_svi(ifp
,
236 nhi
.encap_info
.vxlan_encap
.vni
= vni
;
240 * We have a valid nhi. Copy the structure over to the route_info.
242 ri
->nhs
[ri
->num_nhs
] = nhi
;
245 if (src
&& !ri
->pref_src
)
252 * netlink_proto_from_route_type
254 static uint8_t netlink_proto_from_route_type(int type
)
256 return zebra2proto(type
);
260 * netlink_route_info_fill
262 * Fill out the route information object from the given route.
264 * Returns true on success and false on failure.
266 static int netlink_route_info_fill(struct netlink_route_info
*ri
, int cmd
,
267 rib_dest_t
*dest
, struct route_entry
*re
)
269 struct nexthop
*nexthop
;
270 struct rib_table_info
*table_info
=
271 rib_table_info(rib_dest_table(dest
));
272 struct zebra_vrf
*zvrf
= table_info
->zvrf
;
274 memset(ri
, 0, sizeof(*ri
));
276 ri
->prefix
= rib_dest_prefix(dest
);
277 ri
->af
= rib_dest_af(dest
);
279 if (zvrf
&& zvrf
->zns
)
280 ri
->nlmsg_pid
= zvrf
->zns
->netlink_dplane_out
.snl
.nl_pid
;
282 ri
->nlmsg_type
= cmd
;
283 ri
->rtm_table
= table_info
->table_id
;
284 ri
->rtm_protocol
= RTPROT_UNSPEC
;
287 * An RTM_DELROUTE need not be accompanied by any nexthops,
288 * particularly in our communication with the FPM.
290 if (cmd
== RTM_DELROUTE
&& !re
)
294 zfpm_debug("%s: Expected non-NULL re pointer", __func__
);
298 ri
->rtm_protocol
= netlink_proto_from_route_type(re
->type
);
299 ri
->rtm_type
= RTN_UNICAST
;
300 ri
->metric
= &re
->metric
;
302 for (ALL_NEXTHOPS(re
->nhe
->nhg
, nexthop
)) {
303 if (ri
->num_nhs
>= zrouter
.multipath_num
)
306 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
309 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
310 switch (nexthop
->bh_type
) {
311 case BLACKHOLE_ADMINPROHIB
:
312 ri
->rtm_type
= RTN_PROHIBIT
;
314 case BLACKHOLE_REJECT
:
315 ri
->rtm_type
= RTN_UNREACHABLE
;
318 case BLACKHOLE_UNSPEC
:
319 ri
->rtm_type
= RTN_BLACKHOLE
;
324 if ((cmd
== RTM_NEWROUTE
325 && CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
326 || (cmd
== RTM_DELROUTE
327 && CHECK_FLAG(re
->status
, ROUTE_ENTRY_INSTALLED
))) {
328 netlink_route_info_add_nh(ri
, nexthop
, re
);
332 if (ri
->num_nhs
== 0) {
333 switch (ri
->rtm_type
) {
335 case RTN_UNREACHABLE
:
339 /* If there is no useful nexthop then return. */
341 "netlink_encode_route(): No useful nexthop.");
350 * netlink_route_info_encode
352 * Returns the number of bytes written to the buffer. 0 or a negative
353 * value indicates an error.
355 static int netlink_route_info_encode(struct netlink_route_info
*ri
,
356 char *in_buf
, size_t in_buf_len
)
359 unsigned int nexthop_num
= 0;
361 struct netlink_nh_info
*nhi
;
362 enum fpm_nh_encap_type_t encap
;
363 struct rtattr
*nest
, *inner_nest
;
364 struct rtnexthop
*rtnh
;
365 struct vxlan_encap_info_t
*vxlan
;
366 struct in6_addr ipv6
;
374 req
= (void *)in_buf
;
376 buf_offset
= ((char *)req
->buf
) - ((char *)req
);
378 if (in_buf_len
< buf_offset
) {
383 memset(req
, 0, buf_offset
);
385 bytelen
= af_addr_size(ri
->af
);
387 req
->n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct rtmsg
));
388 req
->n
.nlmsg_flags
= NLM_F_CREATE
| NLM_F_REQUEST
;
389 req
->n
.nlmsg_pid
= ri
->nlmsg_pid
;
390 req
->n
.nlmsg_type
= ri
->nlmsg_type
;
391 req
->r
.rtm_family
= ri
->af
;
394 * rtm_table field is a uchar field which can accommodate table_id less
396 * To support table id greater than 255, if the table_id is greater than
397 * 255, set rtm_table to RT_TABLE_UNSPEC and add RTA_TABLE attribute
398 * with 32 bit value as the table_id.
400 if (ri
->rtm_table
< 256)
401 req
->r
.rtm_table
= ri
->rtm_table
;
403 req
->r
.rtm_table
= RT_TABLE_UNSPEC
;
404 nl_attr_put32(&req
->n
, in_buf_len
, RTA_TABLE
, ri
->rtm_table
);
407 req
->r
.rtm_dst_len
= ri
->prefix
->prefixlen
;
408 req
->r
.rtm_protocol
= ri
->rtm_protocol
;
409 req
->r
.rtm_scope
= RT_SCOPE_UNIVERSE
;
411 nl_attr_put(&req
->n
, in_buf_len
, RTA_DST
, &ri
->prefix
->u
.prefix
,
414 req
->r
.rtm_type
= ri
->rtm_type
;
418 nl_attr_put32(&req
->n
, in_buf_len
, RTA_PRIORITY
, *ri
->metric
);
420 if (ri
->num_nhs
== 0)
423 if (ri
->num_nhs
== 1) {
427 if (nhi
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
428 && ri
->af
== AF_INET6
) {
429 ipv4_to_ipv4_mapped_ipv6(&ipv6
,
431 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
434 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
435 nhi
->gateway
, bytelen
);
439 nl_attr_put32(&req
->n
, in_buf_len
, RTA_OIF
,
443 encap
= nhi
->encap_info
.encap_type
;
445 case FPM_NH_ENCAP_NONE
:
446 case FPM_NH_ENCAP_MAX
:
448 case FPM_NH_ENCAP_VXLAN
:
449 nl_attr_put16(&req
->n
, in_buf_len
, RTA_ENCAP_TYPE
,
451 vxlan
= &nhi
->encap_info
.vxlan_encap
;
452 nest
= nl_attr_nest(&req
->n
, in_buf_len
, RTA_ENCAP
);
453 nl_attr_put32(&req
->n
, in_buf_len
, VXLAN_VNI
,
455 nl_attr_nest_end(&req
->n
, nest
);
465 nest
= nl_attr_nest(&req
->n
, in_buf_len
, RTA_MULTIPATH
);
467 for (nexthop_num
= 0; nexthop_num
< ri
->num_nhs
; nexthop_num
++) {
468 rtnh
= nl_attr_rtnh(&req
->n
, in_buf_len
);
469 nhi
= &ri
->nhs
[nexthop_num
];
472 nl_attr_put(&req
->n
, in_buf_len
, RTA_GATEWAY
,
473 nhi
->gateway
, bytelen
);
476 rtnh
->rtnh_ifindex
= nhi
->if_index
;
479 rtnh
->rtnh_hops
= nhi
->weight
;
481 encap
= nhi
->encap_info
.encap_type
;
483 case FPM_NH_ENCAP_NONE
:
484 case FPM_NH_ENCAP_MAX
:
486 case FPM_NH_ENCAP_VXLAN
:
487 nl_attr_put16(&req
->n
, in_buf_len
, RTA_ENCAP_TYPE
,
489 vxlan
= &nhi
->encap_info
.vxlan_encap
;
491 nl_attr_nest(&req
->n
, in_buf_len
, RTA_ENCAP
);
492 nl_attr_put32(&req
->n
, in_buf_len
, VXLAN_VNI
,
494 nl_attr_nest_end(&req
->n
, inner_nest
);
498 nl_attr_rtnh_end(&req
->n
, rtnh
);
501 nl_attr_nest_end(&req
->n
, nest
);
502 assert(nest
->rta_len
> RTA_LENGTH(0));
507 nl_attr_put(&req
->n
, in_buf_len
, RTA_PREFSRC
, ri
->pref_src
,
511 assert(req
->n
.nlmsg_len
< in_buf_len
);
512 return req
->n
.nlmsg_len
;
516 * zfpm_log_route_info
518 * Helper function to log the information in a route_info structure.
520 static void zfpm_log_route_info(struct netlink_route_info
*ri
,
523 struct netlink_nh_info
*nhi
;
525 char buf
[PREFIX_STRLEN
];
527 zfpm_debug("%s : %s %pFX, Proto: %s, Metric: %u", label
,
528 nl_msg_type_to_str(ri
->nlmsg_type
), ri
->prefix
,
529 nl_rtproto_to_str(ri
->rtm_protocol
),
530 ri
->metric
? *ri
->metric
: 0);
532 for (i
= 0; i
< ri
->num_nhs
; i
++) {
536 if (ri
->af
== AF_INET
)
537 inet_ntop(AF_INET
, nhi
->gateway
, buf
,
540 inet_ntop(AF_INET6
, nhi
->gateway
, buf
,
543 strlcpy(buf
, "none", sizeof(buf
));
545 zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s, Encap type: %s",
546 nhi
->if_index
, buf
, nhi
->recursive
? "yes" : "no",
547 nexthop_type_to_str(nhi
->type
),
548 fpm_nh_encap_type_to_str(nhi
->encap_info
.encap_type
)
554 * zfpm_netlink_encode_route
556 * Create a netlink message corresponding to the given route in the
557 * given buffer space.
559 * Returns the number of bytes written to the buffer. 0 or a negative
560 * value indicates an error.
562 int zfpm_netlink_encode_route(int cmd
, rib_dest_t
*dest
, struct route_entry
*re
,
563 char *in_buf
, size_t in_buf_len
)
565 struct netlink_route_info ri_space
, *ri
;
569 if (!netlink_route_info_fill(ri
, cmd
, dest
, re
))
572 zfpm_log_route_info(ri
, __func__
);
574 return netlink_route_info_encode(ri
, in_buf
, in_buf_len
);
578 * zfpm_netlink_encode_mac
580 * Create a netlink message corresponding to the given MAC.
582 * Returns the number of bytes written to the buffer. 0 or a negative
583 * value indicates an error.
585 int zfpm_netlink_encode_mac(struct fpm_mac_info_t
*mac
, char *in_buf
,
595 req
= (void *)in_buf
;
597 buf_offset
= offsetof(struct macmsg
, buf
);
598 if (in_buf_len
< buf_offset
)
600 memset(req
, 0, buf_offset
);
602 /* Construct nlmsg header */
603 req
->hdr
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ndmsg
));
604 req
->hdr
.nlmsg_type
= CHECK_FLAG(mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
) ?
605 RTM_DELNEIGH
: RTM_NEWNEIGH
;
606 req
->hdr
.nlmsg_flags
= NLM_F_REQUEST
;
607 if (req
->hdr
.nlmsg_type
== RTM_NEWNEIGH
)
608 req
->hdr
.nlmsg_flags
|= (NLM_F_CREATE
| NLM_F_REPLACE
);
610 /* Construct ndmsg */
611 req
->ndm
.ndm_family
= AF_BRIDGE
;
612 req
->ndm
.ndm_ifindex
= mac
->vxlan_if
;
614 req
->ndm
.ndm_state
= NUD_REACHABLE
;
615 req
->ndm
.ndm_flags
|= NTF_SELF
| NTF_MASTER
;
616 if (CHECK_FLAG(mac
->zebra_flags
,
617 (ZEBRA_MAC_STICKY
| ZEBRA_MAC_REMOTE_DEF_GW
)))
618 req
->ndm
.ndm_state
|= NUD_NOARP
;
620 req
->ndm
.ndm_flags
|= NTF_EXT_LEARNED
;
623 nl_attr_put(&req
->hdr
, in_buf_len
, NDA_LLADDR
, &mac
->macaddr
, 6);
624 nl_attr_put(&req
->hdr
, in_buf_len
, NDA_DST
, &mac
->r_vtep_ip
, 4);
625 nl_attr_put32(&req
->hdr
, in_buf_len
, NDA_MASTER
, mac
->svi_if
);
626 nl_attr_put32(&req
->hdr
, in_buf_len
, NDA_VNI
, mac
->vni
);
628 assert(req
->hdr
.nlmsg_len
< in_buf_len
);
630 zfpm_debug("Tx %s family %s ifindex %u MAC %pEA DEST %pI4",
631 nl_msg_type_to_str(req
->hdr
.nlmsg_type
),
632 nl_family_to_str(req
->ndm
.ndm_family
), req
->ndm
.ndm_ifindex
,
633 &mac
->macaddr
, &mac
->r_vtep_ip
);
635 return req
->hdr
.nlmsg_len
;
638 #endif /* HAVE_NETLINK */