1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/* RIPd and zebra interface.
* Copyright (C) 1997, 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "command.h"
#include "prefix.h"
#include "table.h"
#include "stream.h"
#include "memory.h"
#include "zclient.h"
#include "log.h"
#include "vrf.h"
#include "ripd/ripd.h"
#include "ripd/rip_debug.h"
#include "ripd/rip_interface.h"
/* All information about zebra. */
struct zclient *zclient = NULL;
/* Send ECMP routes to zebra. */
static void rip_zebra_ipv4_send(struct route_node *rp, uint8_t cmd)
{
struct list *list = (struct list *)rp->info;
struct zapi_route api;
struct zapi_nexthop *api_nh;
struct listnode *listnode = NULL;
struct rip_info *rinfo = NULL;
int count = 0;
memset(&api, 0, sizeof(api));
api.vrf_id = VRF_DEFAULT;
api.type = ZEBRA_ROUTE_RIP;
api.safi = SAFI_UNICAST;
SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
if (count >= MULTIPATH_NUM)
break;
api_nh = &api.nexthops[count];
api_nh->vrf_id = VRF_DEFAULT;
api_nh->gate = rinfo->nh.gate;
api_nh->type = NEXTHOP_TYPE_IPV4;
if (cmd == ZEBRA_ROUTE_ADD)
SET_FLAG(rinfo->flags, RIP_RTF_FIB);
else
UNSET_FLAG(rinfo->flags, RIP_RTF_FIB);
count++;
}
api.prefix = rp->p;
api.nexthop_num = count;
rinfo = listgetdata(listhead(list));
SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
api.metric = rinfo->metric;
if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) {
SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
api.distance = rinfo->distance;
}
if (rinfo->tag) {
SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
api.tag = rinfo->tag;
}
zclient_route_send(cmd, zclient, &api);
if (IS_RIP_DEBUG_ZEBRA) {
if (rip->ecmp)
zlog_debug("%s: %s/%d nexthops %d",
(cmd == ZEBRA_ROUTE_ADD)
? "Install into zebra"
: "Delete from zebra",
inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen,
count);
else
zlog_debug("%s: %s/%d",
(cmd == ZEBRA_ROUTE_ADD)
? "Install into zebra"
: "Delete from zebra",
inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen);
}
rip_global_route_changes++;
}
/* Add/update ECMP routes to zebra. */
void rip_zebra_ipv4_add(struct route_node *rp)
{
rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_ADD);
}
/* Delete ECMP routes from zebra. */
void rip_zebra_ipv4_delete(struct route_node *rp)
{
rip_zebra_ipv4_send(rp, ZEBRA_ROUTE_DELETE);
}
/* Zebra route add and delete treatment. */
static int rip_zebra_read_route(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct zapi_route api;
struct nexthop nh;
if (!rip)
return 0;
if (zapi_route_decode(zclient->ibuf, &api) < 0)
return -1;
memset(&nh, 0, sizeof(nh));
nh.type = api.nexthops[0].type;
nh.gate.ipv4 = api.nexthops[0].gate.ipv4;
nh.ifindex = api.nexthops[0].ifindex;
/* Then fetch IPv4 prefixes. */
if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
rip_redistribute_add(api.type, RIP_ROUTE_REDISTRIBUTE,
(struct prefix_ipv4 *)&api.prefix, &nh,
api.metric, api.distance, api.tag);
else if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL)
rip_redistribute_delete(api.type, RIP_ROUTE_REDISTRIBUTE,
(struct prefix_ipv4 *)&api.prefix,
nh.ifindex);
return 0;
}
void rip_zclient_reset(void)
{
zclient_reset(zclient);
}
void rip_redistribute_conf_update(int type)
{
zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
0, VRF_DEFAULT);
}
void rip_redistribute_conf_delete(int type)
{
if (zclient->sock > 0)
zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
AFI_IP, type, 0, VRF_DEFAULT);
/* Remove the routes from RIP table. */
rip_redistribute_withdraw(type);
}
int rip_redistribute_check(int type)
{
return vrf_bitmap_check(zclient->redist[AFI_IP][type], VRF_DEFAULT);
}
void rip_redistribute_clean(void)
{
for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
if (!vrf_bitmap_check(zclient->redist[AFI_IP][i], VRF_DEFAULT))
continue;
if (zclient->sock > 0)
zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE,
zclient, AFI_IP, i, 0,
VRF_DEFAULT);
vrf_bitmap_unset(zclient->redist[AFI_IP][i], VRF_DEFAULT);
/* Remove the routes from RIP table. */
rip_redistribute_withdraw(i);
}
}
void rip_show_redistribute_config(struct vty *vty)
{
for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
if (i == zclient->redist_default
|| !vrf_bitmap_check(zclient->redist[AFI_IP][i],
VRF_DEFAULT))
continue;
vty_out(vty, " %s", zebra_route_string(i));
}
}
static void rip_zebra_connected(struct zclient *zclient)
{
zclient_send_reg_requests(zclient, VRF_DEFAULT);
}
void rip_zclient_init(struct thread_master *master)
{
/* Set default value to the zebra client structure. */
zclient = zclient_new_notify(master, &zclient_options_default);
zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs);
zclient->zebra_connected = rip_zebra_connected;
zclient->interface_add = rip_interface_add;
zclient->interface_delete = rip_interface_delete;
zclient->interface_address_add = rip_interface_address_add;
zclient->interface_address_delete = rip_interface_address_delete;
zclient->interface_up = rip_interface_up;
zclient->interface_down = rip_interface_down;
zclient->redistribute_route_add = rip_zebra_read_route;
zclient->redistribute_route_del = rip_zebra_read_route;
}
void rip_zclient_stop(void)
{
zclient_stop(zclient);
zclient_free(zclient);
}
|