]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_zebra.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / ripd / rip_zebra.c
CommitLineData
718e3744 1/* RIPd and zebra interface.
2 * Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra 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
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "prefix.h"
bce8e868 25#include "table.h"
718e3744 26#include "stream.h"
bce8e868 27#include "memory.h"
718e3744 28#include "zclient.h"
29#include "log.h"
7076bb2f 30#include "vrf.h"
718e3744 31#include "ripd/ripd.h"
32#include "ripd/rip_debug.h"
dc63bfd4 33#include "ripd/rip_interface.h"
718e3744 34
35/* All information about zebra. */
36struct zclient *zclient = NULL;
6b0655a2 37
bce8e868 38/* Send ECMP routes to zebra. */
045c5389
RW
39static void rip_zebra_ipv4_send(struct rip *rip, struct route_node *rp,
40 uint8_t cmd)
718e3744 41{
d62a17ae 42 struct list *list = (struct list *)rp->info;
c9fb3e23
RW
43 struct zapi_route api;
44 struct zapi_nexthop *api_nh;
d62a17ae 45 struct listnode *listnode = NULL;
46 struct rip_info *rinfo = NULL;
47 int count = 0;
48
c9fb3e23 49 memset(&api, 0, sizeof(api));
ae7b826a 50 api.vrf_id = rip->vrf->vrf_id;
d00061ea 51 api.type = ZEBRA_ROUTE_RIP;
d00061ea
RW
52 api.safi = SAFI_UNICAST;
53
d00061ea
RW
54 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
55 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
a74e593b
RW
56 if (count >= MULTIPATH_NUM)
57 break;
c9fb3e23 58 api_nh = &api.nexthops[count];
ae7b826a 59 api_nh->vrf_id = rip->vrf->vrf_id;
dd127197 60 api_nh->gate = rinfo->nh.gate;
c9fb3e23
RW
61 api_nh->type = NEXTHOP_TYPE_IPV4;
62 if (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
63 SET_FLAG(rinfo->flags, RIP_RTF_FIB);
64 else
65 UNSET_FLAG(rinfo->flags, RIP_RTF_FIB);
c9fb3e23 66 count++;
d00061ea 67 }
d62a17ae 68
c9fb3e23 69 api.prefix = rp->p;
d00061ea 70 api.nexthop_num = count;
d62a17ae 71
d00061ea 72 rinfo = listgetdata(listhead(list));
d62a17ae 73
d00061ea
RW
74 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
75 api.metric = rinfo->metric;
d62a17ae 76
d00061ea
RW
77 if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) {
78 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
79 api.distance = rinfo->distance;
80 }
d62a17ae 81
d00061ea
RW
82 if (rinfo->tag) {
83 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
84 api.tag = rinfo->tag;
85 }
d62a17ae 86
c9fb3e23 87 zclient_route_send(cmd, zclient, &api);
d00061ea
RW
88
89 if (IS_RIP_DEBUG_ZEBRA) {
90 if (rip->ecmp)
91 zlog_debug("%s: %s/%d nexthops %d",
c9fb3e23 92 (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
93 ? "Install into zebra"
94 : "Delete from zebra",
95 inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen,
96 count);
97 else
98 zlog_debug("%s: %s/%d",
c9fb3e23 99 (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
100 ? "Install into zebra"
101 : "Delete from zebra",
102 inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen);
d62a17ae 103 }
d00061ea 104
c08a2107 105 rip->counters.route_changes++;
718e3744 106}
107
bce8e868 108/* Add/update ECMP routes to zebra. */
045c5389 109void rip_zebra_ipv4_add(struct rip *rip, struct route_node *rp)
718e3744 110{
045c5389 111 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_ADD);
bce8e868 112}
718e3744 113
bce8e868 114/* Delete ECMP routes from zebra. */
045c5389 115void rip_zebra_ipv4_delete(struct rip *rip, struct route_node *rp)
bce8e868 116{
045c5389 117 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_DELETE);
718e3744 118}
119
120/* Zebra route add and delete treatment. */
121f9dee 121static int rip_zebra_read_route(ZAPI_CALLBACK_ARGS)
718e3744 122{
045c5389 123 struct rip *rip;
74489921 124 struct zapi_route api;
3f5682c8 125 struct nexthop nh;
d62a17ae 126
045c5389 127 rip = rip_lookup_by_vrf_id(vrf_id);
d62a17ae 128 if (!rip)
129 return 0;
130
74489921
RW
131 if (zapi_route_decode(zclient->ibuf, &api) < 0)
132 return -1;
133
3f5682c8
DS
134 memset(&nh, 0, sizeof(nh));
135 nh.type = api.nexthops[0].type;
136 nh.gate.ipv4 = api.nexthops[0].gate.ipv4;
137 nh.ifindex = api.nexthops[0].ifindex;
d62a17ae 138
139 /* Then fetch IPv4 prefixes. */
121f9dee 140 if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
045c5389 141 rip_redistribute_add(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
3f5682c8 142 (struct prefix_ipv4 *)&api.prefix, &nh,
996c9314 143 api.metric, api.distance, api.tag);
121f9dee 144 else if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL)
045c5389 145 rip_redistribute_delete(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
74489921 146 (struct prefix_ipv4 *)&api.prefix,
3f5682c8 147 nh.ifindex);
d62a17ae 148
149 return 0;
718e3744 150}
151
045c5389 152void rip_redistribute_conf_update(struct rip *rip, int type)
718e3744 153{
ad21f675
DS
154 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
155 type, 0, rip->vrf->vrf_id);
718e3744 156}
6b0655a2 157
045c5389 158void rip_redistribute_conf_delete(struct rip *rip, int type)
718e3744 159{
d62a17ae 160 if (zclient->sock > 0)
161 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
ae7b826a 162 AFI_IP, type, 0, rip->vrf->vrf_id);
718e3744 163
d62a17ae 164 /* Remove the routes from RIP table. */
045c5389 165 rip_redistribute_withdraw(rip, type);
718e3744 166}
167
045c5389 168int rip_redistribute_check(struct rip *rip, int type)
718e3744 169{
f9120f71 170 return rip->redist[type].enabled;
718e3744 171}
172
f9120f71 173void rip_redistribute_enable(struct rip *rip)
718e3744 174{
908f0020 175 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
f9120f71 176 if (!rip_redistribute_check(rip, i))
908f0020 177 continue;
d62a17ae 178
f9120f71
RW
179 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
180 i, 0, rip->vrf->vrf_id);
181 }
182}
d62a17ae 183
f9120f71
RW
184void rip_redistribute_disable(struct rip *rip)
185{
186 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
187 if (!rip_redistribute_check(rip, i))
188 continue;
189
190 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
191 AFI_IP, i, 0, rip->vrf->vrf_id);
d62a17ae 192 }
16705130 193}
194
045c5389 195void rip_show_redistribute_config(struct vty *vty, struct rip *rip)
718e3744 196{
908f0020 197 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
d00061ea 198 if (i == zclient->redist_default
f9120f71 199 || !rip_redistribute_check(rip, i))
d00061ea
RW
200 continue;
201
908f0020 202 vty_out(vty, " %s", zebra_route_string(i));
d00061ea 203 }
718e3744 204}
205
ae7b826a
RW
206void rip_zebra_vrf_register(struct vrf *vrf)
207{
208 if (vrf->vrf_id == VRF_DEFAULT)
209 return;
210
211 if (IS_RIP_DEBUG_EVENT)
212 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
213 vrf->name, vrf->vrf_id);
214
215 zclient_send_reg_requests(zclient, vrf->vrf_id);
216}
217
218void rip_zebra_vrf_deregister(struct vrf *vrf)
219{
220 if (vrf->vrf_id == VRF_DEFAULT)
221 return;
222
223 if (IS_RIP_DEBUG_EVENT)
224 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
225 vrf->name, vrf->vrf_id);
226
227 zclient_send_dereg_requests(zclient, vrf->vrf_id);
228}
229
d62a17ae 230static void rip_zebra_connected(struct zclient *zclient)
7076bb2f 231{
d62a17ae 232 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
233}
234
d62a17ae 235void rip_zclient_init(struct thread_master *master)
718e3744 236{
d62a17ae 237 /* Set default value to the zebra client structure. */
26f63a1e 238 zclient = zclient_new(master, &zclient_options_default);
342213ea 239 zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs);
d62a17ae 240 zclient->zebra_connected = rip_zebra_connected;
d62a17ae 241 zclient->interface_address_add = rip_interface_address_add;
242 zclient->interface_address_delete = rip_interface_address_delete;
ae7b826a 243 zclient->interface_vrf_update = rip_interface_vrf_update;
74489921
RW
244 zclient->redistribute_route_add = rip_zebra_read_route;
245 zclient->redistribute_route_del = rip_zebra_read_route;
718e3744 246}
a2f9eb82 247
d62a17ae 248void rip_zclient_stop(void)
a2f9eb82 249{
d62a17ae 250 zclient_stop(zclient);
251 zclient_free(zclient);
a2f9eb82 252}