]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_zebra.c
*: Make zapi route install Notifications optional
[mirror_frr.git] / eigrpd / eigrp_zebra.c
1 /*
2 * Zebra connect library for EIGRP.
3 * Copyright (C) 2013-2014
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 *
11 * This file is part of GNU Zebra.
12 *
13 * GNU Zebra is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2, or (at your option) any
16 * later version.
17 *
18 * GNU Zebra is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; see the file COPYING; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 #include <zebra.h>
29
30 #include "thread.h"
31 #include "command.h"
32 #include "network.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "table.h"
36 #include "stream.h"
37 #include "memory.h"
38 #include "zclient.h"
39 #include "filter.h"
40 #include "plist.h"
41 #include "log.h"
42 #include "nexthop.h"
43
44 #include "eigrpd/eigrp_structs.h"
45 #include "eigrpd/eigrpd.h"
46 #include "eigrpd/eigrp_interface.h"
47 #include "eigrpd/eigrp_neighbor.h"
48 #include "eigrpd/eigrp_packet.h"
49 #include "eigrpd/eigrp_zebra.h"
50 #include "eigrpd/eigrp_vty.h"
51 #include "eigrpd/eigrp_dump.h"
52 #include "eigrpd/eigrp_network.h"
53 #include "eigrpd/eigrp_topology.h"
54 #include "eigrpd/eigrp_fsm.h"
55
56 static int eigrp_interface_add(int, struct zclient *, zebra_size_t, vrf_id_t);
57 static int eigrp_interface_delete(int, struct zclient *, zebra_size_t,
58 vrf_id_t);
59 static int eigrp_interface_address_add(int, struct zclient *, zebra_size_t,
60 vrf_id_t vrf_id);
61 static int eigrp_interface_address_delete(int, struct zclient *, zebra_size_t,
62 vrf_id_t vrf_id);
63 static int eigrp_interface_state_up(int, struct zclient *, zebra_size_t,
64 vrf_id_t vrf_id);
65 static int eigrp_interface_state_down(int, struct zclient *, zebra_size_t,
66 vrf_id_t vrf_id);
67 static struct interface *zebra_interface_if_lookup(struct stream *);
68
69 static int eigrp_zebra_read_route(int, struct zclient *, zebra_size_t,
70 vrf_id_t vrf_id);
71
72 /* Zebra structure to hold current status. */
73 struct zclient *zclient = NULL;
74
75 /* For registering threads. */
76 extern struct thread_master *master;
77 struct in_addr router_id_zebra;
78
79 /* Router-id update message from zebra. */
80 static int eigrp_router_id_update_zebra(int command, struct zclient *zclient,
81 zebra_size_t length, vrf_id_t vrf_id)
82 {
83 struct eigrp *eigrp;
84 struct prefix router_id;
85 zebra_router_id_update_read(zclient->ibuf, &router_id);
86
87 router_id_zebra = router_id.u.prefix4;
88
89 eigrp = eigrp_lookup();
90
91 if (eigrp != NULL)
92 eigrp_router_id_update(eigrp);
93
94 return 0;
95 }
96
97 static void eigrp_zebra_connected(struct zclient *zclient)
98 {
99 zclient_send_reg_requests(zclient, VRF_DEFAULT);
100 }
101
102 void eigrp_zebra_init(void)
103 {
104 zclient = zclient_new_notify(master, &zclient_options_default);
105
106 zclient_init(zclient, ZEBRA_ROUTE_EIGRP, 0, &eigrpd_privs);
107 zclient->zebra_connected = eigrp_zebra_connected;
108 zclient->router_id_update = eigrp_router_id_update_zebra;
109 zclient->interface_add = eigrp_interface_add;
110 zclient->interface_delete = eigrp_interface_delete;
111 zclient->interface_up = eigrp_interface_state_up;
112 zclient->interface_down = eigrp_interface_state_down;
113 zclient->interface_address_add = eigrp_interface_address_add;
114 zclient->interface_address_delete = eigrp_interface_address_delete;
115 zclient->redistribute_route_add = eigrp_zebra_read_route;
116 zclient->redistribute_route_del = eigrp_zebra_read_route;
117 }
118
119
120 /* Zebra route add and delete treatment. */
121 static int eigrp_zebra_read_route(int command, struct zclient *zclient,
122 zebra_size_t length, vrf_id_t vrf_id)
123 {
124 struct zapi_route api;
125 struct eigrp *eigrp;
126
127 if (zapi_route_decode(zclient->ibuf, &api) < 0)
128 return -1;
129
130 if (IPV4_NET127(ntohl(api.prefix.u.prefix4.s_addr)))
131 return 0;
132
133 eigrp = eigrp_lookup();
134 if (eigrp == NULL)
135 return 0;
136
137 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
138
139 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
140 {
141 }
142
143 return 0;
144 }
145
146 /* Inteface addition message from zebra. */
147 static int eigrp_interface_add(int command, struct zclient *zclient,
148 zebra_size_t length, vrf_id_t vrf_id)
149 {
150 struct interface *ifp;
151 struct eigrp_interface *ei;
152
153 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
154
155 if (!ifp->info)
156 return 0;
157
158 ei = ifp->info;
159
160 ei->params.type = eigrp_default_iftype(ifp);
161
162 eigrp_if_update(ifp);
163
164 return 0;
165 }
166
167 static int eigrp_interface_delete(int command, struct zclient *zclient,
168 zebra_size_t length, vrf_id_t vrf_id)
169 {
170 struct interface *ifp;
171 struct stream *s;
172
173 s = zclient->ibuf;
174 /* zebra_interface_state_read () updates interface structure in iflist
175 */
176 ifp = zebra_interface_state_read(s, vrf_id);
177
178 if (ifp == NULL)
179 return 0;
180
181 if (if_is_up(ifp))
182 zlog_warn("Zebra: got delete of %s, but interface is still up",
183 ifp->name);
184
185 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
186 zlog_debug(
187 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
188 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
189 ifp->metric, ifp->mtu);
190
191 if (ifp->info)
192 eigrp_if_free(ifp->info,
193 INTERFACE_DOWN_BY_ZEBRA);
194
195 if_set_index(ifp, IFINDEX_INTERNAL);
196 return 0;
197 }
198
199 static int eigrp_interface_address_add(int command, struct zclient *zclient,
200 zebra_size_t length, vrf_id_t vrf_id)
201 {
202 struct connected *c;
203
204 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
205
206 if (c == NULL)
207 return 0;
208
209 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
210 char buf[128];
211 prefix2str(c->address, buf, sizeof(buf));
212 zlog_debug("Zebra: interface %s address add %s", c->ifp->name,
213 buf);
214 }
215
216 eigrp_if_update(c->ifp);
217
218 return 0;
219 }
220
221 static int eigrp_interface_address_delete(int command, struct zclient *zclient,
222 zebra_size_t length, vrf_id_t vrf_id)
223 {
224 struct connected *c;
225 struct interface *ifp;
226 struct eigrp_interface *ei;
227
228 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
229
230 if (c == NULL)
231 return 0;
232
233 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
234 char buf[128];
235 prefix2str(c->address, buf, sizeof(buf));
236 zlog_debug("Zebra: interface %s address delete %s",
237 c->ifp->name, buf);
238 }
239
240 ifp = c->ifp;
241 ei = ifp->info;
242 if (!ei)
243 return 0;
244
245 /* Call interface hook functions to clean up */
246 eigrp_if_free(ei, INTERFACE_DOWN_BY_ZEBRA);
247
248 connected_free(c);
249
250 return 0;
251 }
252
253 static int eigrp_interface_state_up(int command, struct zclient *zclient,
254 zebra_size_t length, vrf_id_t vrf_id)
255 {
256 struct interface *ifp;
257
258 ifp = zebra_interface_if_lookup(zclient->ibuf);
259
260 if (ifp == NULL)
261 return 0;
262
263 /* Interface is already up. */
264 if (if_is_operative(ifp)) {
265 /* Temporarily keep ifp values. */
266 struct interface if_tmp;
267 memcpy(&if_tmp, ifp, sizeof(struct interface));
268
269 zebra_interface_if_set_value(zclient->ibuf, ifp);
270
271 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
272 zlog_debug("Zebra: Interface[%s] state update.",
273 ifp->name);
274
275 if (if_tmp.bandwidth != ifp->bandwidth) {
276 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
277 zlog_debug(
278 "Zebra: Interface[%s] bandwidth change %d -> %d.",
279 ifp->name, if_tmp.bandwidth,
280 ifp->bandwidth);
281
282 // eigrp_if_recalculate_output_cost (ifp);
283 }
284
285 if (if_tmp.mtu != ifp->mtu) {
286 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
287 zlog_debug(
288 "Zebra: Interface[%s] MTU change %u -> %u.",
289 ifp->name, if_tmp.mtu, ifp->mtu);
290
291 /* Must reset the interface (simulate down/up) when MTU
292 * changes. */
293 eigrp_if_reset(ifp);
294 }
295 return 0;
296 }
297
298 zebra_interface_if_set_value(zclient->ibuf, ifp);
299
300 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
301 zlog_debug("Zebra: Interface[%s] state change to up.",
302 ifp->name);
303
304 if (ifp->info)
305 eigrp_if_up(ifp->info);
306
307 return 0;
308 }
309
310 static int eigrp_interface_state_down(int command, struct zclient *zclient,
311 zebra_size_t length, vrf_id_t vrf_id)
312 {
313 struct interface *ifp;
314
315 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
316
317 if (ifp == NULL)
318 return 0;
319
320 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
321 zlog_debug("Zebra: Interface[%s] state change to down.",
322 ifp->name);
323
324 if (ifp->info)
325 eigrp_if_down(ifp->info);
326
327 return 0;
328 }
329
330 static struct interface *zebra_interface_if_lookup(struct stream *s)
331 {
332 char ifname_tmp[INTERFACE_NAMSIZ];
333
334 /* Read interface name. */
335 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
336
337 /* And look it up. */
338 return if_lookup_by_name(ifname_tmp, VRF_DEFAULT);
339 }
340
341 void eigrp_zebra_route_add(struct prefix *p, struct list *successors)
342 {
343 struct zapi_route api;
344 struct zapi_nexthop *api_nh;
345 struct eigrp_nexthop_entry *te;
346 struct listnode *node;
347 int count = 0;
348
349 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
350 return;
351
352 memset(&api, 0, sizeof(api));
353 api.vrf_id = VRF_DEFAULT;
354 api.type = ZEBRA_ROUTE_EIGRP;
355 api.safi = SAFI_UNICAST;
356 memcpy(&api.prefix, p, sizeof(*p));
357
358 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
359
360 /* Nexthop, ifindex, distance and metric information. */
361 for (ALL_LIST_ELEMENTS_RO(successors, node, te)) {
362 if (count >= MULTIPATH_NUM)
363 break;
364 api_nh = &api.nexthops[count];
365 if (te->adv_router->src.s_addr) {
366 api_nh->gate.ipv4 = te->adv_router->src;
367 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
368 } else
369 api_nh->type = NEXTHOP_TYPE_IFINDEX;
370 api_nh->ifindex = te->ei->ifp->ifindex;
371
372 count++;
373 }
374 api.nexthop_num = count;
375
376 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
377 char buf[2][PREFIX_STRLEN];
378 zlog_debug("Zebra: Route add %s nexthop %s",
379 prefix2str(p, buf[0], PREFIX_STRLEN),
380 inet_ntop(AF_INET, 0, buf[1], PREFIX_STRLEN));
381 }
382
383 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
384 }
385
386 void eigrp_zebra_route_delete(struct prefix *p)
387 {
388 struct zapi_route api;
389
390 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
391 return;
392
393 memset(&api, 0, sizeof(api));
394 api.vrf_id = VRF_DEFAULT;
395 api.type = ZEBRA_ROUTE_EIGRP;
396 api.safi = SAFI_UNICAST;
397 memcpy(&api.prefix, p, sizeof(*p));
398 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
399
400 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
401 char buf[PREFIX_STRLEN];
402 zlog_debug("Zebra: Route del %s",
403 prefix2str(p, buf, PREFIX_STRLEN));
404 }
405
406 return;
407 }
408
409 int eigrp_is_type_redistributed(int type)
410 {
411 return ((DEFAULT_ROUTE_TYPE(type))
412 ? vrf_bitmap_check(zclient->default_information,
413 VRF_DEFAULT)
414 : vrf_bitmap_check(zclient->redist[AFI_IP][type],
415 VRF_DEFAULT));
416 }
417
418 int eigrp_redistribute_set(struct eigrp *eigrp, int type,
419 struct eigrp_metrics metric)
420 {
421
422 if (eigrp_is_type_redistributed(type)) {
423 if (eigrp_metrics_is_same(metric, eigrp->dmetric[type])) {
424 eigrp->dmetric[type] = metric;
425 }
426
427 eigrp_external_routes_refresh(eigrp, type);
428
429 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
430 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
431 // Metric[%d]",
432 // eigrp_redist_string(type),
433 // metric_type (eigrp, type), metric_value
434 // (eigrp, type));
435 return CMD_SUCCESS;
436 }
437
438 eigrp->dmetric[type] = metric;
439
440 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, 0,
441 VRF_DEFAULT);
442
443 ++eigrp->redistribute;
444
445 return CMD_SUCCESS;
446 }
447
448 int eigrp_redistribute_unset(struct eigrp *eigrp, int type)
449 {
450
451 if (eigrp_is_type_redistributed(type)) {
452 memset(&eigrp->dmetric[type], 0, sizeof(struct eigrp_metrics));
453 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP,
454 type, 0, VRF_DEFAULT);
455 --eigrp->redistribute;
456 }
457
458 return CMD_SUCCESS;
459 }