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
);
126 if (if_is_vrf(ifp
)) {
127 struct static_vrf
*svrf
=
128 static_vrf_lookup_by_id(vrf_id
);
130 static_fixup_vrf_ids(svrf
);
131 static_config_install_delayed_routes(svrf
);
134 /* Install any static reliant on this interface coming up */
135 static_install_intf_nh(ifp
);
141 static int interface_state_down(int command
, struct zclient
*zclient
,
142 zebra_size_t length
, vrf_id_t vrf_id
)
144 zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
149 static int route_notify_owner(int command
, struct zclient
*zclient
,
150 zebra_size_t length
, vrf_id_t vrf_id
)
153 enum zapi_route_notify_owner note
;
155 char buf
[PREFIX_STRLEN
];
157 prefix2str(&p
, buf
, sizeof(buf
));
159 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table_id
, ¬e
))
163 case ZAPI_ROUTE_FAIL_INSTALL
:
164 zlog_warn("%s: Route %s failed to install for table: %u",
165 __PRETTY_FUNCTION__
, buf
, table_id
);
167 case ZAPI_ROUTE_BETTER_ADMIN_WON
:
168 zlog_warn("%s: Route %s over-ridden by better route for table: %u",
169 __PRETTY_FUNCTION__
, buf
, table_id
);
171 case ZAPI_ROUTE_INSTALLED
:
173 case ZAPI_ROUTE_REMOVED
:
175 case ZAPI_ROUTE_REMOVE_FAIL
:
176 zlog_warn("%s: Route %s failure to remove for table: %u",
177 __PRETTY_FUNCTION__
, buf
, table_id
);
183 static void zebra_connected(struct zclient
*zclient
)
185 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
188 struct static_nht_data
{
197 static int static_zebra_nexthop_update(int command
, struct zclient
*zclient
,
198 zebra_size_t length
, vrf_id_t vrf_id
)
200 struct static_nht_data
*nhtd
, lookup
;
201 struct zapi_route nhr
;
204 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
205 zlog_warn("Failure to decode nexthop update message");
209 if (nhr
.prefix
.family
== AF_INET6
)
212 memset(&lookup
, 0, sizeof(lookup
));
213 lookup
.nh
= &nhr
.prefix
;
214 lookup
.nh_vrf_id
= vrf_id
;
216 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
219 nhtd
->nh_num
= nhr
.nexthop_num
;
221 static_nht_update(&nhr
.prefix
, nhr
.nexthop_num
, afi
,
224 zlog_err("No nhtd?");
229 static void static_zebra_capabilities(struct zclient_capabilities
*cap
)
231 mpls_enabled
= cap
->mpls_enabled
;
234 static unsigned int static_nht_hash_key(void *data
)
236 struct static_nht_data
*nhtd
= data
;
237 unsigned int key
= 0;
239 key
= prefix_hash_key(nhtd
->nh
);
240 return jhash_1word(nhtd
->nh_vrf_id
, key
);
243 static bool static_nht_hash_cmp(const void *d1
, const void *d2
)
245 const struct static_nht_data
*nhtd1
= d1
;
246 const struct static_nht_data
*nhtd2
= d2
;
248 if (nhtd1
->nh_vrf_id
!= nhtd2
->nh_vrf_id
)
251 return prefix_same(nhtd1
->nh
, nhtd2
->nh
);
254 static void *static_nht_hash_alloc(void *data
)
256 struct static_nht_data
*copy
= data
;
257 struct static_nht_data
*new;
259 new = XMALLOC(MTYPE_TMP
, sizeof(*new));
261 new->nh
= prefix_new();
262 prefix_copy(new->nh
, copy
->nh
);
265 new->nh_vrf_id
= copy
->nh_vrf_id
;
270 static void static_nht_hash_free(void *data
)
272 struct static_nht_data
*nhtd
= data
;
274 prefix_free(nhtd
->nh
);
275 XFREE(MTYPE_TMP
, nhtd
);
278 void static_zebra_nht_register(struct static_route
*si
, bool reg
)
280 struct static_nht_data
*nhtd
, lookup
;
286 ZEBRA_NEXTHOP_REGISTER
: ZEBRA_NEXTHOP_UNREGISTER
;
288 if (si
->nh_registered
&& reg
)
291 if (!si
->nh_registered
&& !reg
)
294 memset(&p
, 0, sizeof(p
));
297 case STATIC_BLACKHOLE
:
299 case STATIC_IPV4_GATEWAY
:
300 case STATIC_IPV4_GATEWAY_IFNAME
:
302 p
.prefixlen
= IPV4_MAX_BITLEN
;
303 p
.u
.prefix4
= si
->addr
.ipv4
;
306 case STATIC_IPV6_GATEWAY
:
307 case STATIC_IPV6_GATEWAY_IFNAME
:
309 p
.prefixlen
= IPV6_MAX_BITLEN
;
310 p
.u
.prefix6
= si
->addr
.ipv6
;
315 memset(&lookup
, 0, sizeof(lookup
));
317 lookup
.nh_vrf_id
= si
->nh_vrf_id
;
319 si
->nh_registered
= reg
;
322 nhtd
= hash_get(static_nht_hash
, &lookup
,
323 static_nht_hash_alloc
);
326 if (nhtd
->refcount
> 1) {
327 static_nht_update(nhtd
->nh
, nhtd
->nh_num
,
332 nhtd
= hash_lookup(static_nht_hash
, &lookup
);
337 if (nhtd
->refcount
>= 1)
340 hash_release(static_nht_hash
, nhtd
);
341 static_nht_hash_free(nhtd
);
344 if (zclient_send_rnh(zclient
, cmd
, &p
, false, si
->nh_vrf_id
) < 0)
345 zlog_warn("%s: Failure to send nexthop to zebra",
346 __PRETTY_FUNCTION__
);
349 extern void static_zebra_route_add(struct route_node
*rn
,
350 struct static_route
*si_changed
,
351 vrf_id_t vrf_id
, safi_t safi
, bool install
)
353 struct static_route
*si
= rn
->info
;
354 const struct prefix
*p
, *src_pp
;
355 struct zapi_nexthop
*api_nh
;
356 struct zapi_route api
;
360 srcdest_rnode_prefixes(rn
, &p
, &src_pp
);
362 memset(&api
, 0, sizeof(api
));
364 api
.type
= ZEBRA_ROUTE_STATIC
;
366 memcpy(&api
.prefix
, p
, sizeof(api
.prefix
));
369 SET_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
);
370 memcpy(&api
.src_prefix
, src_pp
, sizeof(api
.src_prefix
));
372 SET_FLAG(api
.flags
, ZEBRA_FLAG_RR_USE_DISTANCE
);
373 if (si_changed
->onlink
)
374 SET_FLAG(api
.flags
, ZEBRA_FLAG_ONLINK
);
375 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
376 if (si_changed
->distance
) {
377 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
378 api
.distance
= si_changed
->distance
;
380 if (si_changed
->tag
) {
381 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
382 api
.tag
= si_changed
->tag
;
384 if (si_changed
->table_id
!= 0) {
385 SET_FLAG(api
.message
, ZAPI_MESSAGE_TABLEID
);
386 api
.tableid
= si_changed
->table_id
;
388 for (/*loaded above*/; si
; si
= si
->next
) {
389 api_nh
= &api
.nexthops
[nh_num
];
390 if (si
->nh_vrf_id
== VRF_UNKNOWN
)
393 if (si
->distance
!= si_changed
->distance
)
396 if (si
->table_id
!= si_changed
->table_id
)
399 api_nh
->vrf_id
= si
->nh_vrf_id
;
402 if (si
->ifindex
== IFINDEX_INTERNAL
)
404 api_nh
->ifindex
= si
->ifindex
;
405 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
407 case STATIC_IPV4_GATEWAY
:
410 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
411 api_nh
->gate
= si
->addr
;
413 case STATIC_IPV4_GATEWAY_IFNAME
:
414 if (si
->ifindex
== IFINDEX_INTERNAL
)
416 api_nh
->ifindex
= si
->ifindex
;
417 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
418 api_nh
->gate
= si
->addr
;
420 case STATIC_IPV6_GATEWAY
:
423 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
424 api_nh
->gate
= si
->addr
;
426 case STATIC_IPV6_GATEWAY_IFNAME
:
427 if (si
->ifindex
== IFINDEX_INTERNAL
)
429 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
430 api_nh
->ifindex
= si
->ifindex
;
431 api_nh
->gate
= si
->addr
;
433 case STATIC_BLACKHOLE
:
434 api_nh
->type
= NEXTHOP_TYPE_BLACKHOLE
;
435 switch (si
->bh_type
) {
436 case STATIC_BLACKHOLE_DROP
:
437 case STATIC_BLACKHOLE_NULL
:
438 api_nh
->bh_type
= BLACKHOLE_NULL
;
440 case STATIC_BLACKHOLE_REJECT
:
441 api_nh
->bh_type
= BLACKHOLE_REJECT
;
446 if (si
->snh_label
.num_labels
) {
449 SET_FLAG(api
.message
, ZAPI_MESSAGE_LABEL
);
450 api_nh
->label_num
= si
->snh_label
.num_labels
;
451 for (i
= 0; i
< api_nh
->label_num
; i
++)
452 api_nh
->labels
[i
] = si
->snh_label
.label
[i
];
457 api
.nexthop_num
= nh_num
;
460 * If we have been given an install but nothing is valid
461 * go ahead and delete the route for double plus fun
463 if (!nh_num
&& install
)
466 zclient_route_send(install
?
467 ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
,
470 void static_zebra_init(void)
472 struct zclient_options opt
= { .receive_notify
= true };
474 zclient
= zclient_new(master
, &opt
);
476 zclient_init(zclient
, ZEBRA_ROUTE_STATIC
, 0, &static_privs
);
477 zclient
->zebra_capabilities
= static_zebra_capabilities
;
478 zclient
->zebra_connected
= zebra_connected
;
479 zclient
->interface_add
= interface_add
;
480 zclient
->interface_delete
= interface_delete
;
481 zclient
->interface_up
= interface_state_up
;
482 zclient
->interface_down
= interface_state_down
;
483 zclient
->interface_address_add
= interface_address_add
;
484 zclient
->interface_address_delete
= interface_address_delete
;
485 zclient
->route_notify_owner
= route_notify_owner
;
486 zclient
->nexthop_update
= static_zebra_nexthop_update
;
488 static_nht_hash
= hash_create(static_nht_hash_key
,
490 "Static Nexthop Tracking hash");