]> git.proxmox.com Git - mirror_frr.git/blob - sharpd/sharp_zebra.c
Merge pull request #1913 from LabNConsulting/working/master/bgp-vpn-leak-cli
[mirror_frr.git] / sharpd / sharp_zebra.c
1 /*
2 * Zebra connect code.
3 * Copyright (C) Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This file is part of FRR.
7 *
8 * FRR is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * FRR is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #include <zebra.h>
23
24 #include "thread.h"
25 #include "command.h"
26 #include "network.h"
27 #include "prefix.h"
28 #include "routemap.h"
29 #include "table.h"
30 #include "stream.h"
31 #include "memory.h"
32 #include "zclient.h"
33 #include "filter.h"
34 #include "plist.h"
35 #include "log.h"
36 #include "nexthop.h"
37
38 #include "sharp_zebra.h"
39
40 /* Zebra structure to hold current status. */
41 struct zclient *zclient = NULL;
42
43 /* For registering threads. */
44 extern struct thread_master *master;
45
46 static struct interface *zebra_interface_if_lookup(struct stream *s)
47 {
48 char ifname_tmp[INTERFACE_NAMSIZ];
49
50 /* Read interface name. */
51 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
52
53 /* And look it up. */
54 return if_lookup_by_name(ifname_tmp, VRF_DEFAULT);
55 }
56
57 /* Inteface addition message from zebra. */
58 static int interface_add(int command, struct zclient *zclient,
59 zebra_size_t length, vrf_id_t vrf_id)
60 {
61 struct interface *ifp;
62
63 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
64
65 if (!ifp->info)
66 return 0;
67
68 return 0;
69 }
70
71 static int interface_delete(int command, struct zclient *zclient,
72 zebra_size_t length, vrf_id_t vrf_id)
73 {
74 struct interface *ifp;
75 struct stream *s;
76
77 s = zclient->ibuf;
78 /* zebra_interface_state_read () updates interface structure in iflist
79 */
80 ifp = zebra_interface_state_read(s, vrf_id);
81
82 if (ifp == NULL)
83 return 0;
84
85 if_set_index(ifp, IFINDEX_INTERNAL);
86
87 return 0;
88 }
89
90 static int interface_address_add(int command, struct zclient *zclient,
91 zebra_size_t length, vrf_id_t vrf_id)
92 {
93
94 zebra_interface_address_read(command, zclient->ibuf, vrf_id);
95
96 return 0;
97 }
98
99 static int interface_address_delete(int command, struct zclient *zclient,
100 zebra_size_t length, vrf_id_t vrf_id)
101 {
102 struct connected *c;
103
104 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
105
106 if (!c)
107 return 0;
108
109 connected_free(c);
110 return 0;
111 }
112
113 static int interface_state_up(int command, struct zclient *zclient,
114 zebra_size_t length, vrf_id_t vrf_id)
115 {
116
117 zebra_interface_if_lookup(zclient->ibuf);
118
119 return 0;
120 }
121
122 static int interface_state_down(int command, struct zclient *zclient,
123 zebra_size_t length, vrf_id_t vrf_id)
124 {
125
126 zebra_interface_state_read(zclient->ibuf, vrf_id);
127
128 return 0;
129 }
130
131 extern uint32_t total_routes;
132 extern uint32_t installed_routes;
133 extern uint32_t removed_routes;
134
135 static int route_notify_owner(int command, struct zclient *zclient,
136 zebra_size_t length, vrf_id_t vrf_id)
137 {
138 struct prefix p;
139 enum zapi_route_notify_owner note;
140 uint32_t table;
141
142 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
143 return -1;
144
145 switch (note) {
146 case ZAPI_ROUTE_INSTALLED:
147 installed_routes++;
148 if (total_routes == installed_routes)
149 zlog_debug("Installed All Items");
150 break;
151 case ZAPI_ROUTE_FAIL_INSTALL:
152 zlog_debug("Failed install of route");
153 break;
154 case ZAPI_ROUTE_BETTER_ADMIN_WON:
155 zlog_debug("Better Admin Distance won over us");
156 break;
157 case ZAPI_ROUTE_REMOVED:
158 removed_routes++;
159 if (total_routes == removed_routes)
160 zlog_debug("Removed all Items");
161 break;
162 case ZAPI_ROUTE_REMOVE_FAIL:
163 zlog_debug("Route removal Failure");
164 break;
165 }
166 return 0;
167 }
168
169 static void zebra_connected(struct zclient *zclient)
170 {
171 zclient_send_reg_requests(zclient, VRF_DEFAULT);
172 }
173
174 void vrf_label_add(vrf_id_t vrf_id, afi_t afi, mpls_label_t label)
175 {
176 zclient_send_vrf_label(zclient, vrf_id, afi, label, ZEBRA_LSP_SHARP);
177 }
178
179 void route_add(struct prefix *p, struct nexthop *nh)
180 {
181 struct zapi_route api;
182 struct zapi_nexthop *api_nh;
183
184 memset(&api, 0, sizeof(api));
185 api.vrf_id = VRF_DEFAULT;
186 api.type = ZEBRA_ROUTE_SHARP;
187 api.safi = SAFI_UNICAST;
188 memcpy(&api.prefix, p, sizeof(*p));
189
190 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
191 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
192
193 api_nh = &api.nexthops[0];
194 api_nh->vrf_id = VRF_DEFAULT;
195 api_nh->gate.ipv4 = nh->gate.ipv4;
196 api_nh->type = nh->type;
197 api_nh->ifindex = nh->ifindex;
198 api.nexthop_num = 1;
199
200 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
201 }
202
203 void route_delete(struct prefix *p)
204 {
205 struct zapi_route api;
206
207 memset(&api, 0, sizeof(api));
208 api.vrf_id = VRF_DEFAULT;
209 api.type = ZEBRA_ROUTE_SHARP;
210 api.safi = SAFI_UNICAST;
211 memcpy(&api.prefix, p, sizeof(*p));
212 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
213
214 return;
215 }
216
217 void sharp_zebra_nexthop_watch(struct prefix *p, bool watch)
218 {
219 int command = ZEBRA_NEXTHOP_REGISTER;
220
221 if (!watch)
222 command = ZEBRA_NEXTHOP_UNREGISTER;
223
224 zclient_send_rnh(zclient, command, p, true, VRF_DEFAULT);
225 }
226
227 static int sharp_nexthop_update(int command, struct zclient *zclient,
228 zebra_size_t length, vrf_id_t vrf_id)
229 {
230 struct zapi_route nhr;
231 char buf[PREFIX_STRLEN];
232 int i;
233
234 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
235 zlog_warn("%s: Decode of update failed", __PRETTY_FUNCTION__);
236
237 return 0;
238 }
239
240 zlog_debug("Received update for %s",
241 prefix2str(&nhr.prefix, buf, sizeof(buf)));
242 for (i = 0; i < nhr.nexthop_num; i++) {
243 struct zapi_nexthop *znh = &nhr.nexthops[i];
244
245 switch (znh->type) {
246 case NEXTHOP_TYPE_IPV4_IFINDEX:
247 case NEXTHOP_TYPE_IPV4:
248 zlog_debug(
249 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
250 inet_ntop(AF_INET, &znh->gate.ipv4.s_addr, buf,
251 sizeof(buf)),
252 znh->type, znh->ifindex, znh->vrf_id,
253 znh->label_num);
254 break;
255 case NEXTHOP_TYPE_IPV6_IFINDEX:
256 case NEXTHOP_TYPE_IPV6:
257 zlog_debug(
258 "\tNexthop %s, type: %d, ifindex: %d, vrf: %d, label_num: %d",
259 inet_ntop(AF_INET6, &znh->gate.ipv6, buf,
260 sizeof(buf)),
261 znh->type, znh->ifindex, znh->vrf_id,
262 znh->label_num);
263 break;
264 case NEXTHOP_TYPE_IFINDEX:
265 zlog_debug("\tNexthop IFINDEX: %d, ifindex: %d",
266 znh->type, znh->ifindex);
267 break;
268 case NEXTHOP_TYPE_BLACKHOLE:
269 zlog_debug("\tNexthop blackhole");
270 break;
271 }
272 }
273 return 0;
274 }
275
276 extern struct zebra_privs_t sharp_privs;
277
278 void sharp_zebra_init(void)
279 {
280 struct zclient_options opt = {.receive_notify = true};
281
282 zclient = zclient_new_notify(master, &opt);
283
284 zclient_init(zclient, ZEBRA_ROUTE_SHARP, 0, &sharp_privs);
285 zclient->zebra_connected = zebra_connected;
286 zclient->interface_add = interface_add;
287 zclient->interface_delete = interface_delete;
288 zclient->interface_up = interface_state_up;
289 zclient->interface_down = interface_state_down;
290 zclient->interface_address_add = interface_address_add;
291 zclient->interface_address_delete = interface_address_delete;
292 zclient->route_notify_owner = route_notify_owner;
293 zclient->nexthop_update = sharp_nexthop_update;
294 }