1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RIPd and zebra interface.
3 * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
16 #include "ripd/ripd.h"
17 #include "ripd/rip_debug.h"
18 #include "ripd/rip_interface.h"
20 /* All information about zebra. */
21 struct zclient
*zclient
= NULL
;
23 /* Send ECMP routes to zebra. */
24 static void rip_zebra_ipv4_send(struct rip
*rip
, struct route_node
*rp
,
27 struct list
*list
= (struct list
*)rp
->info
;
28 struct zapi_route api
;
29 struct zapi_nexthop
*api_nh
;
30 struct listnode
*listnode
= NULL
;
31 struct rip_info
*rinfo
= NULL
;
34 memset(&api
, 0, sizeof(api
));
35 api
.vrf_id
= rip
->vrf
->vrf_id
;
36 api
.type
= ZEBRA_ROUTE_RIP
;
37 api
.safi
= SAFI_UNICAST
;
39 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
40 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, rinfo
)) {
41 if (count
>= MULTIPATH_NUM
)
43 api_nh
= &api
.nexthops
[count
];
44 api_nh
->vrf_id
= rip
->vrf
->vrf_id
;
45 api_nh
->gate
= rinfo
->nh
.gate
;
46 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
47 if (cmd
== ZEBRA_ROUTE_ADD
)
48 SET_FLAG(rinfo
->flags
, RIP_RTF_FIB
);
50 UNSET_FLAG(rinfo
->flags
, RIP_RTF_FIB
);
55 api
.nexthop_num
= count
;
57 rinfo
= listgetdata(listhead(list
));
59 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
60 api
.metric
= rinfo
->metric
;
62 if (rinfo
->distance
&& rinfo
->distance
!= ZEBRA_RIP_DISTANCE_DEFAULT
) {
63 SET_FLAG(api
.message
, ZAPI_MESSAGE_DISTANCE
);
64 api
.distance
= rinfo
->distance
;
68 SET_FLAG(api
.message
, ZAPI_MESSAGE_TAG
);
72 zclient_route_send(cmd
, zclient
, &api
);
74 if (IS_RIP_DEBUG_ZEBRA
) {
76 zlog_debug("%s: %pFX nexthops %d",
77 (cmd
== ZEBRA_ROUTE_ADD
)
78 ? "Install into zebra"
79 : "Delete from zebra",
82 zlog_debug("%s: %pFX",
83 (cmd
== ZEBRA_ROUTE_ADD
)
84 ? "Install into zebra"
85 : "Delete from zebra", &rp
->p
);
88 rip
->counters
.route_changes
++;
91 /* Add/update ECMP routes to zebra. */
92 void rip_zebra_ipv4_add(struct rip
*rip
, struct route_node
*rp
)
94 rip_zebra_ipv4_send(rip
, rp
, ZEBRA_ROUTE_ADD
);
97 /* Delete ECMP routes from zebra. */
98 void rip_zebra_ipv4_delete(struct rip
*rip
, struct route_node
*rp
)
100 rip_zebra_ipv4_send(rip
, rp
, ZEBRA_ROUTE_DELETE
);
103 /* Zebra route add and delete treatment. */
104 static int rip_zebra_read_route(ZAPI_CALLBACK_ARGS
)
107 struct zapi_route api
;
110 rip
= rip_lookup_by_vrf_id(vrf_id
);
114 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
117 memset(&nh
, 0, sizeof(nh
));
118 nh
.type
= api
.nexthops
[0].type
;
119 nh
.gate
.ipv4
= api
.nexthops
[0].gate
.ipv4
;
120 nh
.ifindex
= api
.nexthops
[0].ifindex
;
122 /* Then fetch IPv4 prefixes. */
123 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
)
124 rip_redistribute_add(rip
, api
.type
, RIP_ROUTE_REDISTRIBUTE
,
125 (struct prefix_ipv4
*)&api
.prefix
, &nh
,
126 api
.metric
, api
.distance
, api
.tag
);
127 else if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_DEL
)
128 rip_redistribute_delete(rip
, api
.type
, RIP_ROUTE_REDISTRIBUTE
,
129 (struct prefix_ipv4
*)&api
.prefix
,
135 void rip_redistribute_conf_update(struct rip
*rip
, int type
)
137 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
138 type
, 0, rip
->vrf
->vrf_id
);
141 void rip_redistribute_conf_delete(struct rip
*rip
, int type
)
143 if (zclient
->sock
> 0)
144 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
145 AFI_IP
, type
, 0, rip
->vrf
->vrf_id
);
147 /* Remove the routes from RIP table. */
148 rip_redistribute_withdraw(rip
, type
);
151 int rip_redistribute_check(struct rip
*rip
, int type
)
153 return rip
->redist
[type
].enabled
;
156 void rip_redistribute_enable(struct rip
*rip
)
158 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
159 if (!rip_redistribute_check(rip
, i
))
162 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
163 i
, 0, rip
->vrf
->vrf_id
);
167 void rip_redistribute_disable(struct rip
*rip
)
169 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
170 if (!rip_redistribute_check(rip
, i
))
173 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE
, zclient
,
174 AFI_IP
, i
, 0, rip
->vrf
->vrf_id
);
178 void rip_show_redistribute_config(struct vty
*vty
, struct rip
*rip
)
180 for (int i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
181 if (i
== zclient
->redist_default
182 || !rip_redistribute_check(rip
, i
))
185 vty_out(vty
, " %s", zebra_route_string(i
));
189 void rip_zebra_vrf_register(struct vrf
*vrf
)
191 if (vrf
->vrf_id
== VRF_DEFAULT
)
194 if (IS_RIP_DEBUG_EVENT
)
195 zlog_debug("%s: register VRF %s(%u) to zebra", __func__
,
196 vrf
->name
, vrf
->vrf_id
);
198 zclient_send_reg_requests(zclient
, vrf
->vrf_id
);
201 void rip_zebra_vrf_deregister(struct vrf
*vrf
)
203 if (vrf
->vrf_id
== VRF_DEFAULT
)
206 if (IS_RIP_DEBUG_EVENT
)
207 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__
,
208 vrf
->name
, vrf
->vrf_id
);
210 zclient_send_dereg_requests(zclient
, vrf
->vrf_id
);
213 static void rip_zebra_connected(struct zclient
*zclient
)
215 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
218 zclient_handler
*const rip_handlers
[] = {
219 [ZEBRA_INTERFACE_ADDRESS_ADD
] = rip_interface_address_add
,
220 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = rip_interface_address_delete
,
221 [ZEBRA_INTERFACE_VRF_UPDATE
] = rip_interface_vrf_update
,
222 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = rip_zebra_read_route
,
223 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = rip_zebra_read_route
,
226 void rip_zclient_init(struct event_loop
*master
)
228 /* Set default value to the zebra client structure. */
229 zclient
= zclient_new(master
, &zclient_options_default
, rip_handlers
,
230 array_size(rip_handlers
));
231 zclient_init(zclient
, ZEBRA_ROUTE_RIP
, 0, &ripd_privs
);
232 zclient
->zebra_connected
= rip_zebra_connected
;
235 void rip_zclient_stop(void)
237 zclient_stop(zclient
);
238 zclient_free(zclient
);