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