3 * Copyright (C) 2018 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "srcdest_table.h"
36 #include "nexthop_group.h"
40 #include "static_vrf.h"
41 #include "static_routes.h"
42 #include "static_zebra.h"
43 #include "static_nht.h"
44 #include "static_vty.h"
46 /* Zebra structure to hold current status. */
47 struct zclient
*zclient
;
48 static struct hash
*static_nht_hash
;
50 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
52 char ifname_tmp
[INTERFACE_NAMSIZ
];
54 /* Read interface name. */
55 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
58 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
61 /* Inteface addition message from zebra. */
62 static int interface_add(int command
, struct zclient
*zclient
,
63 zebra_size_t length
, vrf_id_t vrf_id
)
65 struct interface
*ifp
;
67 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
72 static_ifindex_update(ifp
, true);
76 static int interface_delete(int command
, struct zclient
*zclient
,
77 zebra_size_t length
, vrf_id_t vrf_id
)
79 struct interface
*ifp
;
83 /* zebra_interface_state_read () updates interface structure in iflist
85 ifp
= zebra_interface_state_read(s
, vrf_id
);
90 if_set_index(ifp
, IFINDEX_INTERNAL
);
92 static_ifindex_update(ifp
, false);
96 static int interface_address_add(int command
, struct zclient
*zclient
,
97 zebra_size_t length
, vrf_id_t vrf_id
)
99 zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
104 static int interface_address_delete(int command
, struct zclient
*zclient
,
105 zebra_size_t length
, vrf_id_t vrf_id
)
109 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
118 static int interface_state_up(int command
, struct zclient
*zclient
,
119 zebra_size_t length
, vrf_id_t vrf_id
)
121 struct interface
*ifp
;
123 ifp
= zebra_interface_if_lookup(zclient
->ibuf
);
125 if (ifp
&& if_is_vrf(ifp
)) {
126 struct static_vrf
*svrf
= static_vrf_lookup_by_id(vrf_id
);
128 static_fixup_vrf_ids(svrf
);
129 static_config_install_delayed_routes(svrf
);
135 static int interface_state_down(int command
, struct zclient
*zclient
,
136 zebra_size_t length
, vrf_id_t vrf_id
)
138 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
143 static int route_notify_owner(int command
, struct zclient
*zclient
,
144 zebra_size_t length
, vrf_id_t vrf_id
)
147 enum zapi_route_notify_owner note
;
149 char buf
[PREFIX_STRLEN
];
151 prefix2str(&p
, buf
, sizeof(buf
));
153 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
))
157 case ZAPI_ROUTE_FAIL_INSTALL
:
158 zlog_warn("%s: Route %s failed to install for table: %u",
159 __PRETTY_FUNCTION__
, buf
, table_id
);
161 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
162 zlog_warn("%s: Route %s over-ridden by better route for table: %u",
163 __PRETTY_FUNCTION__
, buf
, table_id
);
165 case ZAPI_ROUTE_INSTALLED
:
167 case ZAPI_ROUTE_REMOVED
:
169 case ZAPI_ROUTE_REMOVE_FAIL
:
170 zlog_warn("%s: Route %s failure to remove for table: %u",
171 __PRETTY_FUNCTION__
, buf
, table_id
);
177 static void zebra_connected(struct zclient
*zclient
)
179 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
182 struct static_nht_data
{
191 static int static_zebra_nexthop_update(int command
, struct zclient
*zclient
,
192 zebra_size_t length
, vrf_id_t vrf_id
)
194 struct static_nht_data
*nhtd
, lookup
;
195 struct zapi_route nhr
;
198 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
199 zlog_warn("Failure to decode nexthop update message");
203 if (nhr
.prefix
.family
== AF_INET6
)
206 memset(&lookup
, 0, sizeof(lookup
));
207 lookup
.nh
= &nhr
.prefix
;
208 lookup
.nh_vrf_id
= vrf_id
;
210 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
213 nhtd
->nh_num
= nhr
.nexthop_num
;
215 static_nht_update(&nhr
.prefix
, nhr
.nexthop_num
, afi
,
218 zlog_err("No nhtd?");
223 static void static_zebra_capabilities(struct zclient_capabilities
*cap
)
225 mpls_enabled
= cap
->mpls_enabled
;
228 static unsigned int static_nht_hash_key(void *data
)
230 struct static_nht_data
*nhtd
= data
;
231 unsigned int key
= 0;
233 key
= prefix_hash_key(nhtd
->nh
);
234 return jhash_1word(nhtd
->nh_vrf_id
, key
);
237 static bool static_nht_hash_cmp(const void *d1
, const void *d2
)
239 const struct static_nht_data
*nhtd1
= d1
;
240 const struct static_nht_data
*nhtd2
= d2
;
242 if (nhtd1
->nh_vrf_id
!= nhtd2
->nh_vrf_id
)
245 return prefix_same(nhtd1
->nh
, nhtd2
->nh
);
248 static void *static_nht_hash_alloc(void *data
)
250 struct static_nht_data
*copy
= data
;
251 struct static_nht_data
*new;
253 new = XMALLOC(MTYPE_TMP
, sizeof(*new));
255 new->nh
= prefix_new();
256 prefix_copy(new->nh
, copy
->nh
);
259 new->nh_vrf_id
= copy
->nh_vrf_id
;
264 static void static_nht_hash_free(void *data
)
266 struct static_nht_data
*nhtd
= data
;
268 prefix_free(nhtd
->nh
);
269 XFREE(MTYPE_TMP
, nhtd
);
272 void static_zebra_nht_register(struct static_route
*si
, bool reg
)
274 struct static_nht_data
*nhtd
, lookup
;
280 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
282 if (si
->nh_registered
&& reg
)
285 if (!si
->nh_registered
&& !reg
)
288 memset(&p
, 0, sizeof(p
));
291 case STATIC_BLACKHOLE
:
293 case STATIC_IPV4_GATEWAY
:
294 case STATIC_IPV4_GATEWAY_IFNAME
:
296 p
.prefixlen
= IPV4_MAX_BITLEN
;
297 p
.u
.prefix4
= si
->addr
.ipv4
;
300 case STATIC_IPV6_GATEWAY
:
301 case STATIC_IPV6_GATEWAY_IFNAME
:
303 p
.prefixlen
= IPV6_MAX_BITLEN
;
304 p
.u
.prefix6
= si
->addr
.ipv6
;
309 memset(&lookup
, 0, sizeof(lookup
));
311 lookup
.nh_vrf_id
= si
->nh_vrf_id
;
313 si
->nh_registered
= reg
;
316 nhtd
= hash_get(static_nht_hash
, &lookup
,
317 static_nht_hash_alloc
);
320 if (nhtd
->refcount
> 1) {
321 static_nht_update(nhtd
->nh
, nhtd
->nh_num
,
326 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
331 if (nhtd
->refcount
>= 1)
334 hash_release(static_nht_hash
, nhtd
);
335 static_nht_hash_free(nhtd
);
338 if (zclient_send_rnh(zclient
, cmd
, &p
, false, si
->nh_vrf_id
) < 0)
339 zlog_warn("%s: Failure to send nexthop to zebra",
340 __PRETTY_FUNCTION__
);
343 extern void static_zebra_route_add(struct route_node
*rn
,
344 struct static_route
*si_changed
,
345 vrf_id_t vrf_id
, safi_t safi
, bool install
)
347 struct static_route
*si
= rn
->info
;
348 const struct prefix
*p
, *src_pp
;
349 struct zapi_nexthop
*api_nh
;
350 struct zapi_route api
;
354 srcdest_rnode_prefixes(rn
, &p
, &src_pp
);
356 memset(&api
, 0, sizeof(api
));
358 api
.type
= ZEBRA_ROUTE_STATIC
;
360 memcpy(&api
.prefix
, p
, sizeof(api
.prefix
));
363 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
364 memcpy(&api
.src_prefix
, src_pp
, sizeof(api
.src_prefix
));
366 SET_FLAG(api
.flags
, ZEBRA_FLAG_RR_USE_DISTANCE
);
367 if (si_changed
->onlink
)
368 SET_FLAG(api
.flags
, ZEBRA_FLAG_ONLINK
);
369 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
370 if (si_changed
->distance
) {
371 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
372 api
.distance
= si_changed
->distance
;
374 if (si_changed
->tag
) {
375 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
376 api
.tag
= si_changed
->tag
;
378 if (si_changed
->table_id
!= 0) {
379 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
380 api
.tableid
= si_changed
->table_id
;
382 for (/*loaded above*/; si
; si
= si
->next
) {
383 api_nh
= &api
.nexthops
[nh_num
];
384 if (si
->nh_vrf_id
== VRF_UNKNOWN
)
387 if (si
->distance
!= si_changed
->distance
)
390 if (si
->table_id
!= si_changed
->table_id
)
393 api_nh
->vrf_id
= si
->nh_vrf_id
;
396 if (si
->ifindex
== IFINDEX_INTERNAL
)
398 api_nh
->ifindex
= si
->ifindex
;
399 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
401 case STATIC_IPV4_GATEWAY
:
404 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
405 api_nh
->gate
= si
->addr
;
407 case STATIC_IPV4_GATEWAY_IFNAME
:
408 if (si
->ifindex
== IFINDEX_INTERNAL
)
410 api_nh
->ifindex
= si
->ifindex
;
411 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
412 api_nh
->gate
= si
->addr
;
414 case STATIC_IPV6_GATEWAY
:
417 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
418 api_nh
->gate
= si
->addr
;
420 case STATIC_IPV6_GATEWAY_IFNAME
:
421 if (si
->ifindex
== IFINDEX_INTERNAL
)
423 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
424 api_nh
->ifindex
= si
->ifindex
;
425 api_nh
->gate
= si
->addr
;
427 case STATIC_BLACKHOLE
:
428 api_nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
429 switch (si
->bh_type
) {
430 case STATIC_BLACKHOLE_DROP
:
431 case STATIC_BLACKHOLE_NULL
:
432 api_nh
->bh_type
= BLACKHOLE_NULL
;
434 case STATIC_BLACKHOLE_REJECT
:
435 api_nh
->bh_type
= BLACKHOLE_REJECT
;
440 if (si
->snh_label
.num_labels
) {
443 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
444 api_nh
->label_num
= si
->snh_label
.num_labels
;
445 for (i
= 0; i
< api_nh
->label_num
; i
++)
446 api_nh
->labels
[i
] = si
->snh_label
.label
[i
];
451 api
.nexthop_num
= nh_num
;
454 * If we have been given an install but nothing is valid
455 * go ahead and delete the route for double plus fun
457 if (!nh_num
&& install
)
460 zclient_route_send(install
?
461 ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
464 void static_zebra_init(void)
466 struct zclient_options opt
= { .receive_notify
= true };
468 zclient
= zclient_new(master
, &opt
);
470 zclient_init(zclient
, ZEBRA_ROUTE_STATIC
, 0, &static_privs
);
471 zclient
->zebra_capabilities
= static_zebra_capabilities
;
472 zclient
->zebra_connected
= zebra_connected
;
473 zclient
->interface_add
= interface_add
;
474 zclient
->interface_delete
= interface_delete
;
475 zclient
->interface_up
= interface_state_up
;
476 zclient
->interface_down
= interface_state_down
;
477 zclient
->interface_address_add
= interface_address_add
;
478 zclient
->interface_address_delete
= interface_address_delete
;
479 zclient
->route_notify_owner
= route_notify_owner
;
480 zclient
->nexthop_update
= static_zebra_nexthop_update
;
482 static_nht_hash
= hash_create(static_nht_hash_key
,
484 "Static Nexthop Tracking hash");