]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_zebra.c
zebra: Allow ns delete to happen after under/over flow checks
[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 int eigrp_zebra_route_notify_owner(int command, struct zclient *zclient,
98 zebra_size_t length, vrf_id_t vrf_id)
99 {
100 struct prefix p;
101 enum zapi_route_notify_owner note;
102 uint32_t table;
103
104 if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, &note))
105 return -1;
106
107 return 0;
108 }
109
110 static void eigrp_zebra_connected(struct zclient *zclient)
111 {
112 zclient_send_reg_requests(zclient, VRF_DEFAULT);
113 }
114
115 void eigrp_zebra_init(void)
116 {
117 struct zclient_options opt = {.receive_notify = false};
118
119 zclient = zclient_new(master, &opt);
120
121 zclient_init(zclient, ZEBRA_ROUTE_EIGRP, 0, &eigrpd_privs);
122 zclient->zebra_connected = eigrp_zebra_connected;
123 zclient->router_id_update = eigrp_router_id_update_zebra;
124 zclient->interface_add = eigrp_interface_add;
125 zclient->interface_delete = eigrp_interface_delete;
126 zclient->interface_up = eigrp_interface_state_up;
127 zclient->interface_down = eigrp_interface_state_down;
128 zclient->interface_address_add = eigrp_interface_address_add;
129 zclient->interface_address_delete = eigrp_interface_address_delete;
130 zclient->redistribute_route_add = eigrp_zebra_read_route;
131 zclient->redistribute_route_del = eigrp_zebra_read_route;
132 zclient->route_notify_owner = eigrp_zebra_route_notify_owner;
133 }
134
135
136 /* Zebra route add and delete treatment. */
137 static int eigrp_zebra_read_route(int command, struct zclient *zclient,
138 zebra_size_t length, vrf_id_t vrf_id)
139 {
140 struct zapi_route api;
141 struct eigrp *eigrp;
142
143 if (zapi_route_decode(zclient->ibuf, &api) < 0)
144 return -1;
145
146 if (IPV4_NET127(ntohl(api.prefix.u.prefix4.s_addr)))
147 return 0;
148
149 eigrp = eigrp_lookup();
150 if (eigrp == NULL)
151 return 0;
152
153 if (command == ZEBRA_REDISTRIBUTE_ROUTE_ADD) {
154
155 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
156 {
157 }
158
159 return 0;
160 }
161
162 /* Inteface addition message from zebra. */
163 static int eigrp_interface_add(int command, struct zclient *zclient,
164 zebra_size_t length, vrf_id_t vrf_id)
165 {
166 struct interface *ifp;
167 struct eigrp_interface *ei;
168
169 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
170
171 if (!ifp->info)
172 return 0;
173
174 ei = ifp->info;
175
176 ei->params.type = eigrp_default_iftype(ifp);
177
178 eigrp_if_update(ifp);
179
180 return 0;
181 }
182
183 static int eigrp_interface_delete(int command, struct zclient *zclient,
184 zebra_size_t length, vrf_id_t vrf_id)
185 {
186 struct interface *ifp;
187 struct stream *s;
188
189 s = zclient->ibuf;
190 /* zebra_interface_state_read () updates interface structure in iflist
191 */
192 ifp = zebra_interface_state_read(s, vrf_id);
193
194 if (ifp == NULL)
195 return 0;
196
197 if (if_is_up(ifp))
198 zlog_warn("Zebra: got delete of %s, but interface is still up",
199 ifp->name);
200
201 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
202 zlog_debug(
203 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
204 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
205 ifp->metric, ifp->mtu);
206
207 if (ifp->info)
208 eigrp_if_free(ifp->info, INTERFACE_DOWN_BY_ZEBRA);
209
210 if_set_index(ifp, IFINDEX_INTERNAL);
211 return 0;
212 }
213
214 static int eigrp_interface_address_add(int command, struct zclient *zclient,
215 zebra_size_t length, vrf_id_t vrf_id)
216 {
217 struct connected *c;
218
219 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
220
221 if (c == NULL)
222 return 0;
223
224 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
225 char buf[128];
226 prefix2str(c->address, buf, sizeof(buf));
227 zlog_debug("Zebra: interface %s address add %s", c->ifp->name,
228 buf);
229 }
230
231 eigrp_if_update(c->ifp);
232
233 return 0;
234 }
235
236 static int eigrp_interface_address_delete(int command, struct zclient *zclient,
237 zebra_size_t length, vrf_id_t vrf_id)
238 {
239 struct connected *c;
240 struct interface *ifp;
241 struct eigrp_interface *ei;
242
243 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
244
245 if (c == NULL)
246 return 0;
247
248 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) {
249 char buf[128];
250 prefix2str(c->address, buf, sizeof(buf));
251 zlog_debug("Zebra: interface %s address delete %s",
252 c->ifp->name, buf);
253 }
254
255 ifp = c->ifp;
256 ei = ifp->info;
257 if (!ei)
258 return 0;
259
260 /* Call interface hook functions to clean up */
261 eigrp_if_free(ei, INTERFACE_DOWN_BY_ZEBRA);
262
263 connected_free(c);
264
265 return 0;
266 }
267
268 static int eigrp_interface_state_up(int command, struct zclient *zclient,
269 zebra_size_t length, vrf_id_t vrf_id)
270 {
271 struct interface *ifp;
272
273 ifp = zebra_interface_if_lookup(zclient->ibuf);
274
275 if (ifp == NULL)
276 return 0;
277
278 /* Interface is already up. */
279 if (if_is_operative(ifp)) {
280 /* Temporarily keep ifp values. */
281 struct interface if_tmp;
282 memcpy(&if_tmp, ifp, sizeof(struct interface));
283
284 zebra_interface_if_set_value(zclient->ibuf, ifp);
285
286 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
287 zlog_debug("Zebra: Interface[%s] state update.",
288 ifp->name);
289
290 if (if_tmp.bandwidth != ifp->bandwidth) {
291 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
292 zlog_debug(
293 "Zebra: Interface[%s] bandwidth change %d -> %d.",
294 ifp->name, if_tmp.bandwidth,
295 ifp->bandwidth);
296
297 // eigrp_if_recalculate_output_cost (ifp);
298 }
299
300 if (if_tmp.mtu != ifp->mtu) {
301 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
302 zlog_debug(
303 "Zebra: Interface[%s] MTU change %u -> %u.",
304 ifp->name, if_tmp.mtu, ifp->mtu);
305
306 /* Must reset the interface (simulate down/up) when MTU
307 * changes. */
308 eigrp_if_reset(ifp);
309 }
310 return 0;
311 }
312
313 zebra_interface_if_set_value(zclient->ibuf, ifp);
314
315 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
316 zlog_debug("Zebra: Interface[%s] state change to up.",
317 ifp->name);
318
319 if (ifp->info)
320 eigrp_if_up(ifp->info);
321
322 return 0;
323 }
324
325 static int eigrp_interface_state_down(int command, struct zclient *zclient,
326 zebra_size_t length, vrf_id_t vrf_id)
327 {
328 struct interface *ifp;
329
330 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
331
332 if (ifp == NULL)
333 return 0;
334
335 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
336 zlog_debug("Zebra: Interface[%s] state change to down.",
337 ifp->name);
338
339 if (ifp->info)
340 eigrp_if_down(ifp->info);
341
342 return 0;
343 }
344
345 static struct interface *zebra_interface_if_lookup(struct stream *s)
346 {
347 char ifname_tmp[INTERFACE_NAMSIZ];
348
349 /* Read interface name. */
350 stream_get(ifname_tmp, s, INTERFACE_NAMSIZ);
351
352 /* And look it up. */
353 return if_lookup_by_name(ifname_tmp, VRF_DEFAULT);
354 }
355
356 void eigrp_zebra_route_add(struct prefix *p, struct list *successors)
357 {
358 struct zapi_route api;
359 struct zapi_nexthop *api_nh;
360 struct eigrp_nexthop_entry *te;
361 struct listnode *node;
362 int count = 0;
363
364 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
365 return;
366
367 memset(&api, 0, sizeof(api));
368 api.vrf_id = VRF_DEFAULT;
369 api.type = ZEBRA_ROUTE_EIGRP;
370 api.safi = SAFI_UNICAST;
371 memcpy(&api.prefix, p, sizeof(*p));
372
373 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
374
375 /* Nexthop, ifindex, distance and metric information. */
376 for (ALL_LIST_ELEMENTS_RO(successors, node, te)) {
377 if (count >= MULTIPATH_NUM)
378 break;
379 api_nh = &api.nexthops[count];
380 api_nh->vrf_id = VRF_DEFAULT;
381 if (te->adv_router->src.s_addr) {
382 api_nh->gate.ipv4 = te->adv_router->src;
383 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
384 } else
385 api_nh->type = NEXTHOP_TYPE_IFINDEX;
386 api_nh->ifindex = te->ei->ifp->ifindex;
387
388 count++;
389 }
390 api.nexthop_num = count;
391
392 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
393 char buf[2][PREFIX_STRLEN];
394 zlog_debug("Zebra: Route add %s nexthop %s",
395 prefix2str(p, buf[0], PREFIX_STRLEN),
396 inet_ntop(AF_INET, 0, buf[1], PREFIX_STRLEN));
397 }
398
399 zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api);
400 }
401
402 void eigrp_zebra_route_delete(struct prefix *p)
403 {
404 struct zapi_route api;
405
406 if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP])
407 return;
408
409 memset(&api, 0, sizeof(api));
410 api.vrf_id = VRF_DEFAULT;
411 api.type = ZEBRA_ROUTE_EIGRP;
412 api.safi = SAFI_UNICAST;
413 memcpy(&api.prefix, p, sizeof(*p));
414 zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api);
415
416 if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) {
417 char buf[PREFIX_STRLEN];
418 zlog_debug("Zebra: Route del %s",
419 prefix2str(p, buf, PREFIX_STRLEN));
420 }
421
422 return;
423 }
424
425 int eigrp_is_type_redistributed(int type)
426 {
427 return ((DEFAULT_ROUTE_TYPE(type))
428 ? vrf_bitmap_check(zclient->default_information,
429 VRF_DEFAULT)
430 : vrf_bitmap_check(zclient->redist[AFI_IP][type],
431 VRF_DEFAULT));
432 }
433
434 int eigrp_redistribute_set(struct eigrp *eigrp, int type,
435 struct eigrp_metrics metric)
436 {
437
438 if (eigrp_is_type_redistributed(type)) {
439 if (eigrp_metrics_is_same(metric, eigrp->dmetric[type])) {
440 eigrp->dmetric[type] = metric;
441 }
442
443 eigrp_external_routes_refresh(eigrp, type);
444
445 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
446 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
447 // Metric[%d]",
448 // eigrp_redist_string(type),
449 // metric_type (eigrp, type), metric_value
450 // (eigrp, type));
451 return CMD_SUCCESS;
452 }
453
454 eigrp->dmetric[type] = metric;
455
456 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, 0,
457 VRF_DEFAULT);
458
459 ++eigrp->redistribute;
460
461 return CMD_SUCCESS;
462 }
463
464 int eigrp_redistribute_unset(struct eigrp *eigrp, int type)
465 {
466
467 if (eigrp_is_type_redistributed(type)) {
468 memset(&eigrp->dmetric[type], 0, sizeof(struct eigrp_metrics));
469 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE, zclient, AFI_IP,
470 type, 0, VRF_DEFAULT);
471 --eigrp->redistribute;
472 }
473
474 return CMD_SUCCESS;
475 }