]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_zebra.c
lib: define ZAPI_CALLBACK_ARGS macro
[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. */
74489921
RW
121static int rip_zebra_read_route(int command, struct zclient *zclient,
122 zebra_size_t length, vrf_id_t vrf_id)
718e3744 123{
045c5389 124 struct rip *rip;
74489921 125 struct zapi_route api;
3f5682c8 126 struct nexthop nh;
d62a17ae 127
045c5389 128 rip = rip_lookup_by_vrf_id(vrf_id);
d62a17ae 129 if (!rip)
130 return 0;
131
74489921
RW
132 if (zapi_route_decode(zclient->ibuf, &api) < 0)
133 return -1;
134
3f5682c8
DS
135 memset(&nh, 0, sizeof(nh));
136 nh.type = api.nexthops[0].type;
137 nh.gate.ipv4 = api.nexthops[0].gate.ipv4;
138 nh.ifindex = api.nexthops[0].ifindex;
d62a17ae 139
140 /* Then fetch IPv4 prefixes. */
74489921 141 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
045c5389 142 rip_redistribute_add(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
3f5682c8 143 (struct prefix_ipv4 *)&api.prefix, &nh,
996c9314 144 api.metric, api.distance, api.tag);
74489921 145 else if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL)
045c5389 146 rip_redistribute_delete(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
74489921 147 (struct prefix_ipv4 *)&api.prefix,
3f5682c8 148 nh.ifindex);
d62a17ae 149
150 return 0;
718e3744 151}
152
045c5389 153void rip_redistribute_conf_update(struct rip *rip, int type)
718e3744 154{
908f0020 155 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
ae7b826a 156 0, rip->vrf->vrf_id);
718e3744 157}
6b0655a2 158
045c5389 159void rip_redistribute_conf_delete(struct rip *rip, int type)
718e3744 160{
d62a17ae 161 if (zclient->sock > 0)
162 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
ae7b826a 163 AFI_IP, type, 0, rip->vrf->vrf_id);
718e3744 164
d62a17ae 165 /* Remove the routes from RIP table. */
045c5389 166 rip_redistribute_withdraw(rip, type);
718e3744 167}
168
045c5389 169int rip_redistribute_check(struct rip *rip, int type)
718e3744 170{
f9120f71 171 return rip->redist[type].enabled;
718e3744 172}
173
f9120f71 174void rip_redistribute_enable(struct rip *rip)
718e3744 175{
908f0020 176 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
f9120f71 177 if (!rip_redistribute_check(rip, i))
908f0020 178 continue;
d62a17ae 179
f9120f71
RW
180 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
181 i, 0, rip->vrf->vrf_id);
182 }
183}
d62a17ae 184
f9120f71
RW
185void rip_redistribute_disable(struct rip *rip)
186{
187 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
188 if (!rip_redistribute_check(rip, i))
189 continue;
190
191 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
192 AFI_IP, i, 0, rip->vrf->vrf_id);
d62a17ae 193 }
16705130 194}
195
045c5389 196void rip_show_redistribute_config(struct vty *vty, struct rip *rip)
718e3744 197{
908f0020 198 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
d00061ea 199 if (i == zclient->redist_default
f9120f71 200 || !rip_redistribute_check(rip, i))
d00061ea
RW
201 continue;
202
908f0020 203 vty_out(vty, " %s", zebra_route_string(i));
d00061ea 204 }
718e3744 205}
206
ae7b826a
RW
207void rip_zebra_vrf_register(struct vrf *vrf)
208{
209 if (vrf->vrf_id == VRF_DEFAULT)
210 return;
211
212 if (IS_RIP_DEBUG_EVENT)
213 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
214 vrf->name, vrf->vrf_id);
215
216 zclient_send_reg_requests(zclient, vrf->vrf_id);
217}
218
219void rip_zebra_vrf_deregister(struct vrf *vrf)
220{
221 if (vrf->vrf_id == VRF_DEFAULT)
222 return;
223
224 if (IS_RIP_DEBUG_EVENT)
225 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
226 vrf->name, vrf->vrf_id);
227
228 zclient_send_dereg_requests(zclient, vrf->vrf_id);
229}
230
d62a17ae 231static void rip_zebra_connected(struct zclient *zclient)
7076bb2f 232{
d62a17ae 233 zclient_send_reg_requests(zclient, VRF_DEFAULT);
7076bb2f
FL
234}
235
d62a17ae 236void rip_zclient_init(struct thread_master *master)
718e3744 237{
d62a17ae 238 /* Set default value to the zebra client structure. */
26f63a1e 239 zclient = zclient_new(master, &zclient_options_default);
342213ea 240 zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs);
d62a17ae 241 zclient->zebra_connected = rip_zebra_connected;
242 zclient->interface_add = rip_interface_add;
243 zclient->interface_delete = rip_interface_delete;
244 zclient->interface_address_add = rip_interface_address_add;
245 zclient->interface_address_delete = rip_interface_address_delete;
246 zclient->interface_up = rip_interface_up;
247 zclient->interface_down = rip_interface_down;
ae7b826a 248 zclient->interface_vrf_update = rip_interface_vrf_update;
74489921
RW
249 zclient->redistribute_route_add = rip_zebra_read_route;
250 zclient->redistribute_route_del = rip_zebra_read_route;
718e3744 251}
a2f9eb82 252
d62a17ae 253void rip_zclient_stop(void)
a2f9eb82 254{
d62a17ae 255 zclient_stop(zclient);
256 zclient_free(zclient);
a2f9eb82 257}