]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_zebra.c
Merge pull request #13326 from opensourcerouting/feature/rip_topotest_distribute_list
[mirror_frr.git] / ripd / rip_zebra.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RIPd and zebra interface.
3 * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
4 */
5
6 #include <zebra.h>
7
8 #include "command.h"
9 #include "prefix.h"
10 #include "table.h"
11 #include "stream.h"
12 #include "memory.h"
13 #include "zclient.h"
14 #include "log.h"
15 #include "vrf.h"
16 #include "ripd/ripd.h"
17 #include "ripd/rip_debug.h"
18 #include "ripd/rip_interface.h"
19
20 /* All information about zebra. */
21 struct zclient *zclient = NULL;
22
23 /* Send ECMP routes to zebra. */
24 static void rip_zebra_ipv4_send(struct rip *rip, struct route_node *rp,
25 uint8_t cmd)
26 {
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;
32 int count = 0;
33
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;
38
39 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
40 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
41 if (count >= MULTIPATH_NUM)
42 break;
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);
49 else
50 UNSET_FLAG(rinfo->flags, RIP_RTF_FIB);
51 count++;
52 }
53
54 api.prefix = rp->p;
55 api.nexthop_num = count;
56
57 rinfo = listgetdata(listhead(list));
58
59 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
60 api.metric = rinfo->metric;
61
62 if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) {
63 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
64 api.distance = rinfo->distance;
65 }
66
67 if (rinfo->tag) {
68 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
69 api.tag = rinfo->tag;
70 }
71
72 zclient_route_send(cmd, zclient, &api);
73
74 if (IS_RIP_DEBUG_ZEBRA) {
75 if (rip->ecmp)
76 zlog_debug("%s: %pFX nexthops %d",
77 (cmd == ZEBRA_ROUTE_ADD)
78 ? "Install into zebra"
79 : "Delete from zebra",
80 &rp->p, count);
81 else
82 zlog_debug("%s: %pFX",
83 (cmd == ZEBRA_ROUTE_ADD)
84 ? "Install into zebra"
85 : "Delete from zebra", &rp->p);
86 }
87
88 rip->counters.route_changes++;
89 }
90
91 /* Add/update ECMP routes to zebra. */
92 void rip_zebra_ipv4_add(struct rip *rip, struct route_node *rp)
93 {
94 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_ADD);
95 }
96
97 /* Delete ECMP routes from zebra. */
98 void rip_zebra_ipv4_delete(struct rip *rip, struct route_node *rp)
99 {
100 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_DELETE);
101 }
102
103 /* Zebra route add and delete treatment. */
104 static int rip_zebra_read_route(ZAPI_CALLBACK_ARGS)
105 {
106 struct rip *rip;
107 struct zapi_route api;
108 struct nexthop nh;
109
110 rip = rip_lookup_by_vrf_id(vrf_id);
111 if (!rip)
112 return 0;
113
114 if (zapi_route_decode(zclient->ibuf, &api) < 0)
115 return -1;
116
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;
121
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,
130 nh.ifindex);
131
132 return 0;
133 }
134
135 void rip_redistribute_conf_update(struct rip *rip, int type)
136 {
137 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
138 type, 0, rip->vrf->vrf_id);
139 }
140
141 void rip_redistribute_conf_delete(struct rip *rip, int type)
142 {
143 if (zclient->sock > 0)
144 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
145 AFI_IP, type, 0, rip->vrf->vrf_id);
146
147 /* Remove the routes from RIP table. */
148 rip_redistribute_withdraw(rip, type);
149 }
150
151 int rip_redistribute_check(struct rip *rip, int type)
152 {
153 return rip->redist[type].enabled;
154 }
155
156 void rip_redistribute_enable(struct rip *rip)
157 {
158 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
159 if (!rip_redistribute_check(rip, i))
160 continue;
161
162 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
163 i, 0, rip->vrf->vrf_id);
164 }
165 }
166
167 void rip_redistribute_disable(struct rip *rip)
168 {
169 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
170 if (!rip_redistribute_check(rip, i))
171 continue;
172
173 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
174 AFI_IP, i, 0, rip->vrf->vrf_id);
175 }
176 }
177
178 void rip_show_redistribute_config(struct vty *vty, struct rip *rip)
179 {
180 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
181 if (i == zclient->redist_default
182 || !rip_redistribute_check(rip, i))
183 continue;
184
185 vty_out(vty, " %s", zebra_route_string(i));
186 }
187 }
188
189 void rip_zebra_vrf_register(struct vrf *vrf)
190 {
191 if (vrf->vrf_id == VRF_DEFAULT)
192 return;
193
194 if (IS_RIP_DEBUG_EVENT)
195 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
196 vrf->name, vrf->vrf_id);
197
198 zclient_send_reg_requests(zclient, vrf->vrf_id);
199 }
200
201 void rip_zebra_vrf_deregister(struct vrf *vrf)
202 {
203 if (vrf->vrf_id == VRF_DEFAULT)
204 return;
205
206 if (IS_RIP_DEBUG_EVENT)
207 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
208 vrf->name, vrf->vrf_id);
209
210 zclient_send_dereg_requests(zclient, vrf->vrf_id);
211 }
212
213 static void rip_zebra_connected(struct zclient *zclient)
214 {
215 zclient_send_reg_requests(zclient, VRF_DEFAULT);
216 }
217
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,
224 };
225
226 void rip_zclient_init(struct event_loop *master)
227 {
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;
233 }
234
235 void rip_zclient_stop(void)
236 {
237 zclient_stop(zclient);
238 zclient_free(zclient);
239 }