]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_zebra.c
Merge pull request #3397 from mjstapp/fix_stream_macros
[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. */
d7c0a89a 39static void rip_zebra_ipv4_send(struct route_node *rp, uint8_t cmd)
718e3744 40{
d62a17ae 41 struct list *list = (struct list *)rp->info;
c9fb3e23
RW
42 struct zapi_route api;
43 struct zapi_nexthop *api_nh;
d62a17ae 44 struct listnode *listnode = NULL;
45 struct rip_info *rinfo = NULL;
46 int count = 0;
47
c9fb3e23 48 memset(&api, 0, sizeof(api));
d00061ea
RW
49 api.vrf_id = VRF_DEFAULT;
50 api.type = ZEBRA_ROUTE_RIP;
d00061ea
RW
51 api.safi = SAFI_UNICAST;
52
d00061ea
RW
53 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
54 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
a74e593b
RW
55 if (count >= MULTIPATH_NUM)
56 break;
c9fb3e23 57 api_nh = &api.nexthops[count];
4a7371e9 58 api_nh->vrf_id = VRF_DEFAULT;
dd127197 59 api_nh->gate = rinfo->nh.gate;
c9fb3e23
RW
60 api_nh->type = NEXTHOP_TYPE_IPV4;
61 if (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
62 SET_FLAG(rinfo->flags, RIP_RTF_FIB);
63 else
64 UNSET_FLAG(rinfo->flags, RIP_RTF_FIB);
c9fb3e23 65 count++;
d00061ea 66 }
d62a17ae 67
c9fb3e23 68 api.prefix = rp->p;
d00061ea 69 api.nexthop_num = count;
d62a17ae 70
d00061ea 71 rinfo = listgetdata(listhead(list));
d62a17ae 72
d00061ea
RW
73 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
74 api.metric = rinfo->metric;
d62a17ae 75
d00061ea
RW
76 if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) {
77 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
78 api.distance = rinfo->distance;
79 }
d62a17ae 80
d00061ea
RW
81 if (rinfo->tag) {
82 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
83 api.tag = rinfo->tag;
84 }
d62a17ae 85
c9fb3e23 86 zclient_route_send(cmd, zclient, &api);
d00061ea
RW
87
88 if (IS_RIP_DEBUG_ZEBRA) {
89 if (rip->ecmp)
90 zlog_debug("%s: %s/%d nexthops %d",
c9fb3e23 91 (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
92 ? "Install into zebra"
93 : "Delete from zebra",
94 inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen,
95 count);
96 else
97 zlog_debug("%s: %s/%d",
c9fb3e23 98 (cmd == ZEBRA_ROUTE_ADD)
d00061ea
RW
99 ? "Install into zebra"
100 : "Delete from zebra",
101 inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen);
d62a17ae 102 }
d00061ea
RW
103
104 rip_global_route_changes++;
718e3744 105}
106
bce8e868 107/* Add/update ECMP routes to zebra. */
d62a17ae 108void rip_zebra_ipv4_add(struct route_node *rp)
718e3744 109{
c9fb3e23 110 rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_ADD);
bce8e868 111}
718e3744 112
bce8e868 113/* Delete ECMP routes from zebra. */
d62a17ae 114void rip_zebra_ipv4_delete(struct route_node *rp)
bce8e868 115{
c9fb3e23 116 rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_DELETE);
718e3744 117}
118
119/* Zebra route add and delete treatment. */
74489921
RW
120static int rip_zebra_read_route(int command, struct zclient *zclient,
121 zebra_size_t length, vrf_id_t vrf_id)
718e3744 122{
74489921 123 struct zapi_route api;
3f5682c8 124 struct nexthop nh;
d62a17ae 125
126 if (!rip)
127 return 0;
128
74489921
RW
129 if (zapi_route_decode(zclient->ibuf, &api) < 0)
130 return -1;
131
3f5682c8
DS
132 memset(&nh, 0, sizeof(nh));
133 nh.type = api.nexthops[0].type;
134 nh.gate.ipv4 = api.nexthops[0].gate.ipv4;
135 nh.ifindex = api.nexthops[0].ifindex;
d62a17ae 136
137 /* Then fetch IPv4 prefixes. */
74489921
RW
138 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
139 rip_redistribute_add(api.type, RIP_ROUTE_REDISTRIBUTE,
3f5682c8 140 (struct prefix_ipv4 *)&api.prefix, &nh,
996c9314 141 api.metric, api.distance, api.tag);
74489921
RW
142 else if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL)
143 rip_redistribute_delete(api.type, RIP_ROUTE_REDISTRIBUTE,
144 (struct prefix_ipv4 *)&api.prefix,
3f5682c8 145 nh.ifindex);
d62a17ae 146
147 return 0;
718e3744 148}
149
d62a17ae 150void rip_zclient_reset(void)
718e3744 151{
d62a17ae 152 zclient_reset(zclient);
718e3744 153}
154
908f0020 155void rip_redistribute_conf_update(int type)
718e3744 156{
908f0020
RW
157 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
158 0, VRF_DEFAULT);
718e3744 159}
6b0655a2 160
908f0020 161void rip_redistribute_conf_delete(int type)
718e3744 162{
d62a17ae 163 if (zclient->sock > 0)
164 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
165 AFI_IP, type, 0, VRF_DEFAULT);
718e3744 166
d62a17ae 167 /* Remove the routes from RIP table. */
168 rip_redistribute_withdraw(type);
718e3744 169}
170
d62a17ae 171int rip_redistribute_check(int type)
718e3744 172{
d62a17ae 173 return vrf_bitmap_check(zclient->redist[AFI_IP][type], VRF_DEFAULT);
718e3744 174}
175
d62a17ae 176void rip_redistribute_clean(void)
718e3744 177{
908f0020
RW
178 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
179 if (!vrf_bitmap_check(zclient->redist[AFI_IP][i], VRF_DEFAULT))
180 continue;
d62a17ae 181
908f0020
RW
182 if (zclient->sock > 0)
183 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE,
184 zclient, AFI_IP, i, 0,
185 VRF_DEFAULT);
d62a17ae 186
908f0020 187 vrf_bitmap_unset(zclient->redist[AFI_IP][i], VRF_DEFAULT);
718e3744 188
908f0020
RW
189 /* Remove the routes from RIP table. */
190 rip_redistribute_withdraw(i);
d62a17ae 191 }
16705130 192}
193
908f0020 194void rip_show_redistribute_config(struct vty *vty)
718e3744 195{
908f0020 196 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
d00061ea
RW
197 if (i == zclient->redist_default
198 || !vrf_bitmap_check(zclient->redist[AFI_IP][i],
199 VRF_DEFAULT))
200 continue;
201
908f0020 202 vty_out(vty, " %s", zebra_route_string(i));
d00061ea 203 }
718e3744 204}
205
d62a17ae 206static void rip_zebra_connected(struct zclient *zclient)
7076bb2f 207{
d62a17ae 208 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
209}
210
d62a17ae 211void rip_zclient_init(struct thread_master *master)
718e3744 212{
d62a17ae 213 /* Set default value to the zebra client structure. */
26f63a1e 214 zclient = zclient_new(master, &zclient_options_default);
342213ea 215 zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs);
d62a17ae 216 zclient->zebra_connected = rip_zebra_connected;
217 zclient->interface_add = rip_interface_add;
218 zclient->interface_delete = rip_interface_delete;
219 zclient->interface_address_add = rip_interface_address_add;
220 zclient->interface_address_delete = rip_interface_address_delete;
221 zclient->interface_up = rip_interface_up;
222 zclient->interface_down = rip_interface_down;
74489921
RW
223 zclient->redistribute_route_add = rip_zebra_read_route;
224 zclient->redistribute_route_del = rip_zebra_read_route;
718e3744 225}
a2f9eb82 226
d62a17ae 227void rip_zclient_stop(void)
a2f9eb82 228{
d62a17ae 229 zclient_stop(zclient);
230 zclient_free(zclient);
a2f9eb82 231}