]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_zebra.c
ripd: add VRF support
[mirror_frr.git] / ripd / rip_zebra.c
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "prefix.h"
25 #include "table.h"
26 #include "stream.h"
27 #include "memory.h"
28 #include "zclient.h"
29 #include "log.h"
30 #include "vrf.h"
31 #include "ripd/ripd.h"
32 #include "ripd/rip_debug.h"
33 #include "ripd/rip_interface.h"
34
35 /* All information about zebra. */
36 struct zclient *zclient = NULL;
37
38 /* Send ECMP routes to zebra. */
39 static void rip_zebra_ipv4_send(struct rip *rip, struct route_node *rp,
40 uint8_t cmd)
41 {
42 struct list *list = (struct list *)rp->info;
43 struct zapi_route api;
44 struct zapi_nexthop *api_nh;
45 struct listnode *listnode = NULL;
46 struct rip_info *rinfo = NULL;
47 int count = 0;
48
49 memset(&api, 0, sizeof(api));
50 api.vrf_id = rip->vrf->vrf_id;
51 api.type = ZEBRA_ROUTE_RIP;
52 api.safi = SAFI_UNICAST;
53
54 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
55 for (ALL_LIST_ELEMENTS_RO(list, listnode, rinfo)) {
56 if (count >= MULTIPATH_NUM)
57 break;
58 api_nh = &api.nexthops[count];
59 api_nh->vrf_id = rip->vrf->vrf_id;
60 api_nh->gate = rinfo->nh.gate;
61 api_nh->type = NEXTHOP_TYPE_IPV4;
62 if (cmd == ZEBRA_ROUTE_ADD)
63 SET_FLAG(rinfo->flags, RIP_RTF_FIB);
64 else
65 UNSET_FLAG(rinfo->flags, RIP_RTF_FIB);
66 count++;
67 }
68
69 api.prefix = rp->p;
70 api.nexthop_num = count;
71
72 rinfo = listgetdata(listhead(list));
73
74 SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
75 api.metric = rinfo->metric;
76
77 if (rinfo->distance && rinfo->distance != ZEBRA_RIP_DISTANCE_DEFAULT) {
78 SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
79 api.distance = rinfo->distance;
80 }
81
82 if (rinfo->tag) {
83 SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
84 api.tag = rinfo->tag;
85 }
86
87 zclient_route_send(cmd, zclient, &api);
88
89 if (IS_RIP_DEBUG_ZEBRA) {
90 if (rip->ecmp)
91 zlog_debug("%s: %s/%d nexthops %d",
92 (cmd == ZEBRA_ROUTE_ADD)
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",
99 (cmd == ZEBRA_ROUTE_ADD)
100 ? "Install into zebra"
101 : "Delete from zebra",
102 inet_ntoa(rp->p.u.prefix4), rp->p.prefixlen);
103 }
104
105 rip->counters.route_changes++;
106 }
107
108 /* Add/update ECMP routes to zebra. */
109 void rip_zebra_ipv4_add(struct rip *rip, struct route_node *rp)
110 {
111 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_ADD);
112 }
113
114 /* Delete ECMP routes from zebra. */
115 void rip_zebra_ipv4_delete(struct rip *rip, struct route_node *rp)
116 {
117 rip_zebra_ipv4_send(rip, rp, ZEBRA_ROUTE_DELETE);
118 }
119
120 /* Zebra route add and delete treatment. */
121 static int rip_zebra_read_route(int command, struct zclient *zclient,
122 zebra_size_t length, vrf_id_t vrf_id)
123 {
124 struct rip *rip;
125 struct zapi_route api;
126 struct nexthop nh;
127
128 rip = rip_lookup_by_vrf_id(vrf_id);
129 if (!rip)
130 return 0;
131
132 if (zapi_route_decode(zclient->ibuf, &api) < 0)
133 return -1;
134
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;
139
140 /* Then fetch IPv4 prefixes. */
141 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
142 rip_redistribute_add(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
143 (struct prefix_ipv4 *)&api.prefix, &nh,
144 api.metric, api.distance, api.tag);
145 else if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL)
146 rip_redistribute_delete(rip, api.type, RIP_ROUTE_REDISTRIBUTE,
147 (struct prefix_ipv4 *)&api.prefix,
148 nh.ifindex);
149
150 return 0;
151 }
152
153 void rip_redistribute_conf_update(struct rip *rip, int type)
154 {
155 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type,
156 0, rip->vrf->vrf_id);
157 }
158
159 void rip_redistribute_conf_delete(struct rip *rip, int type)
160 {
161 if (zclient->sock > 0)
162 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
163 AFI_IP, type, 0, rip->vrf->vrf_id);
164
165 /* Remove the routes from RIP table. */
166 rip_redistribute_withdraw(rip, type);
167 }
168
169 int rip_redistribute_check(struct rip *rip, int type)
170 {
171 return vrf_bitmap_check(zclient->redist[AFI_IP][type],
172 rip->vrf->vrf_id);
173 }
174
175 void rip_redistribute_clean(struct rip *rip)
176 {
177 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
178 if (!vrf_bitmap_check(zclient->redist[AFI_IP][i],
179 rip->vrf->vrf_id))
180 continue;
181
182 if (zclient->sock > 0)
183 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE,
184 zclient, AFI_IP, i, 0,
185 rip->vrf->vrf_id);
186
187 vrf_bitmap_unset(zclient->redist[AFI_IP][i], rip->vrf->vrf_id);
188 }
189 }
190
191 void rip_show_redistribute_config(struct vty *vty, struct rip *rip)
192 {
193 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++) {
194 if (i == zclient->redist_default
195 || !vrf_bitmap_check(zclient->redist[AFI_IP][i],
196 rip->vrf->vrf_id))
197 continue;
198
199 vty_out(vty, " %s", zebra_route_string(i));
200 }
201 }
202
203 void rip_zebra_vrf_register(struct vrf *vrf)
204 {
205 if (vrf->vrf_id == VRF_DEFAULT)
206 return;
207
208 if (IS_RIP_DEBUG_EVENT)
209 zlog_debug("%s: register VRF %s(%u) to zebra", __func__,
210 vrf->name, vrf->vrf_id);
211
212 zclient_send_reg_requests(zclient, vrf->vrf_id);
213 }
214
215 void rip_zebra_vrf_deregister(struct vrf *vrf)
216 {
217 if (vrf->vrf_id == VRF_DEFAULT)
218 return;
219
220 if (IS_RIP_DEBUG_EVENT)
221 zlog_debug("%s: deregister VRF %s(%u) from zebra.", __func__,
222 vrf->name, vrf->vrf_id);
223
224 zclient_send_dereg_requests(zclient, vrf->vrf_id);
225 }
226
227 static void rip_zebra_connected(struct zclient *zclient)
228 {
229 zclient_send_reg_requests(zclient, VRF_DEFAULT);
230 }
231
232 void rip_zclient_init(struct thread_master *master)
233 {
234 /* Set default value to the zebra client structure. */
235 zclient = zclient_new(master, &zclient_options_default);
236 zclient_init(zclient, ZEBRA_ROUTE_RIP, 0, &ripd_privs);
237 zclient->zebra_connected = rip_zebra_connected;
238 zclient->interface_add = rip_interface_add;
239 zclient->interface_delete = rip_interface_delete;
240 zclient->interface_address_add = rip_interface_address_add;
241 zclient->interface_address_delete = rip_interface_address_delete;
242 zclient->interface_up = rip_interface_up;
243 zclient->interface_down = rip_interface_down;
244 zclient->interface_vrf_update = rip_interface_vrf_update;
245 zclient->redistribute_route_add = rip_zebra_read_route;
246 zclient->redistribute_route_del = rip_zebra_read_route;
247 }
248
249 void rip_zclient_stop(void)
250 {
251 zclient_stop(zclient);
252 zclient_free(zclient);
253 }