]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_zebra.c
*: Convert `struct event_master` to `struct event_loop`
[mirror_frr.git] / ripngd / ripng_zebra.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * RIPngd and zebra interface.
4 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
5 */
6
7 #include <zebra.h>
8
9 #include "command.h"
10 #include "prefix.h"
11 #include "agg_table.h"
12 #include "stream.h"
13 #include "memory.h"
14 #include "routemap.h"
15 #include "zclient.h"
16 #include "log.h"
17
18 #include "ripngd/ripngd.h"
19 #include "ripngd/ripng_debug.h"
20
21 /* All information about zebra. */
22 struct zclient *zclient = NULL;
23
24 /* Send ECMP routes to zebra. */
25 static void ripng_zebra_ipv6_send(struct ripng *ripng, struct agg_node *rp,
26 uint8_t cmd)
27 {
28 struct list *list = (struct list *)rp->info;
29 struct zapi_route api;
30 struct zapi_nexthop *api_nh;
31 struct listnode *listnode = NULL;
32 struct ripng_info *rinfo = NULL;
33 int count = 0;
34 const struct prefix *p = agg_node_get_prefix(rp);
35
36 memset(&api, 0, sizeof(api));
37 api.vrf_id = ripng->vrf->vrf_id;
38 api.type = ZEBRA_ROUTE_RIPNG;
39 api.safi = SAFI_UNICAST;
40 api.prefix = *p;
41
42 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
43 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
44 if (count >= MULTIPATH_NUM)
45 break;
46 api_nh = &api.nexthops[count];
47 api_nh->vrf_id = ripng->vrf->vrf_id;
48 api_nh->gate.ipv6 = rinfo->nexthop;
49 api_nh->ifindex = rinfo->ifindex;
50 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
51 count++;
52 if (cmd == ZEBRA_ROUTE_ADD)
53 SET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
54 else
55 UNSET_FLAG(rinfo->flags, RIPNG_RTF_FIB);
56 }
57
58 api.nexthop_num = count;
59
60 rinfo = listgetdata(listhead(list));
61
62 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
63 api.metric = rinfo->metric;
64
65 if (rinfo->tag) {
66 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
67 api.tag = rinfo->tag;
68 }
69
70 zclient_route_send(cmd, zclient, &api);
71
72 if (IS_RIPNG_DEBUG_ZEBRA) {
73 if (ripng->ecmp)
74 zlog_debug("%s: %pRN nexthops %d",
75 (cmd == ZEBRA_ROUTE_ADD)
76 ? "Install into zebra"
77 : "Delete from zebra",
78 rp, count);
79 else
80 zlog_debug("%s: %pRN",
81 (cmd == ZEBRA_ROUTE_ADD)
82 ? "Install into zebra"
83 : "Delete from zebra",
84 rp);
85 }
86 }
87
88 /* Add/update ECMP routes to zebra. */
89 void ripng_zebra_ipv6_add(struct ripng *ripng, struct agg_node *rp)
90 {
91 ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_ADD);
92 }
93
94 /* Delete ECMP routes from zebra. */
95 void ripng_zebra_ipv6_delete(struct ripng *ripng, struct agg_node *rp)
96 {
97 ripng_zebra_ipv6_send(ripng, rp, ZEBRA_ROUTE_DELETE);
98 }
99
100 /* Zebra route add and delete treatment. */
101 static int ripng_zebra_read_route(ZAPI_CALLBACK_ARGS)
102 {
103 struct ripng *ripng;
104 struct zapi_route api;
105 struct in6_addr nexthop;
106 unsigned long ifindex;
107
108 ripng = ripng_lookup_by_vrf_id(vrf_id);
109 if (!ripng)
110 return 0;
111
112 if (zapi_route_decode(zclient->ibuf, &api) < 0)
113 return -1;
114
115 /* we completely ignore srcdest routes for now. */
116 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
117 return 0;
118
119 if (IN6_IS_ADDR_LINKLOCAL(&api.prefix.u.prefix6))
120 return 0;
121
122 nexthop = api.nexthops[0].gate.ipv6;
123 ifindex = api.nexthops[0].ifindex;
124
125 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
126 ripng_redistribute_add(ripng, api.type,
127 RIPNG_ROUTE_REDISTRIBUTE,
128 (struct prefix_ipv6 *)&api.prefix,
129 ifindex, &nexthop, api.tag);
130 else
131 ripng_redistribute_delete(
132 ripng, api.type, RIPNG_ROUTE_REDISTRIBUTE,
133 (struct prefix_ipv6 *)&api.prefix, ifindex);
134
135 return 0;
136 }
137
138 void ripng_redistribute_conf_update(struct ripng *ripng, int type)
139 {
140 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
141 type, 0, ripng->vrf->vrf_id);
142 }
143
144 void ripng_redistribute_conf_delete(struct ripng *ripng, int type)
145 {
146 if (zclient->sock > 0)
147 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
148 AFI_IP6, type, 0, ripng->vrf->vrf_id);
149
150 ripng_redistribute_withdraw(ripng, type);
151 }
152
153 int ripng_redistribute_check(struct ripng *ripng, int type)
154 {
155 return ripng->redist[type].enabled;
156 }
157
158 void ripng_redistribute_enable(struct ripng *ripng)
159 {
160 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
161 if (!ripng_redistribute_check(ripng, i))
162 continue;
163
164 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
165 AFI_IP6, i, 0, ripng->vrf->vrf_id);
166 }
167 }
168
169 void ripng_redistribute_disable(struct ripng *ripng)
170 {
171 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
172 if (!ripng_redistribute_check(ripng, i))
173 continue;
174
175 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
176 AFI_IP6, i, 0, ripng->vrf->vrf_id);
177 }
178 }
179
180 void ripng_redistribute_write(struct vty *vty, struct ripng *ripng)
181 {
182 int i;
183
184 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
185 if (i == zclient->redist_default
186 || !ripng_redistribute_check(ripng, i))
187 continue;
188
189 vty_out(vty, " %s", zebra_route_string(i));
190 }
191 }
192
193 void ripng_zebra_vrf_register(struct vrf *vrf)
194 {
195 if (vrf->vrf_id == VRF_DEFAULT)
196 return;
197
198 if (IS_RIPNG_DEBUG_EVENT)
199 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
200 vrf->name, vrf->vrf_id);
201
202 zclient_send_reg_requests(zclient, vrf->vrf_id);
203 }
204
205 void ripng_zebra_vrf_deregister(struct vrf *vrf)
206 {
207 if (vrf->vrf_id == VRF_DEFAULT)
208 return;
209
210 if (IS_RIPNG_DEBUG_EVENT)
211 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
212 vrf->name, vrf->vrf_id);
213
214 zclient_send_dereg_requests(zclient, vrf->vrf_id);
215 }
216
217 static void ripng_zebra_connected(struct zclient *zclient)
218 {
219 zclient_send_reg_requests(zclient, VRF_DEFAULT);
220 }
221
222 static zclient_handler *const ripng_handlers[] = {
223 [ZEBRA_INTERFACE_ADDRESS_ADD] = ripng_interface_address_add,
224 [ZEBRA_INTERFACE_ADDRESS_DELETE] = ripng_interface_address_delete,
225 [ZEBRA_INTERFACE_VRF_UPDATE] = ripng_interface_vrf_update,
226 [ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ripng_zebra_read_route,
227 [ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ripng_zebra_read_route,
228 };
229
230 /* Initialize zebra structure and it's commands. */
231 void zebra_init(struct event_loop *master)
232 {
233 /* Allocate zebra structure. */
234 zclient = zclient_new(master, &zclient_options_default, ripng_handlers,
235 array_size(ripng_handlers));
236 zclient_init(zclient, ZEBRA_ROUTE_RIPNG, 0, &ripngd_privs);
237
238 zclient->zebra_connected = ripng_zebra_connected;
239 }
240
241 void ripng_zebra_stop(void)
242 {
243 zclient_stop(zclient);
244 zclient_free(zclient);
245 }