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