2 * Zebra connect library for EIGRP.
3 * Copyright (C) 2013-2014
11 * This file is part of GNU Zebra.
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
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.
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
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"
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
,
59 static int eigrp_interface_address_add(int, struct zclient
*, zebra_size_t
,
61 static int eigrp_interface_address_delete(int, struct zclient
*, zebra_size_t
,
63 static int eigrp_interface_state_up(int, struct zclient
*, zebra_size_t
,
65 static int eigrp_interface_state_down(int, struct zclient
*, zebra_size_t
,
67 static struct interface
*zebra_interface_if_lookup(struct stream
*);
69 static int eigrp_zebra_read_route(int, struct zclient
*, zebra_size_t
,
72 /* Zebra structure to hold current status. */
73 struct zclient
*zclient
= NULL
;
75 /* For registering threads. */
76 extern struct thread_master
*master
;
77 struct in_addr router_id_zebra
;
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
)
84 struct prefix router_id
;
85 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
87 router_id_zebra
= router_id
.u
.prefix4
;
89 eigrp
= eigrp_lookup();
92 eigrp_router_id_update(eigrp
);
97 static int eigrp_zebra_route_notify_owner(int command
, struct zclient
*zclient
,
98 zebra_size_t length
, vrf_id_t vrf_id
)
101 enum zapi_route_notify_owner note
;
104 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
110 static void eigrp_zebra_connected(struct zclient
*zclient
)
112 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
115 void eigrp_zebra_init(void)
117 struct zclient_options opt
= {.receive_notify
= false};
119 zclient
= zclient_new_notify(master
, &opt
);
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
;
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
)
140 struct zapi_route api
;
143 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
146 if (IPV4_NET127(ntohl(api
.prefix
.u
.prefix4
.s_addr
)))
149 eigrp
= eigrp_lookup();
153 if (command
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
155 } else /* if (command == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
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
)
166 struct interface
*ifp
;
167 struct eigrp_interface
*ei
;
169 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
176 ei
->params
.type
= eigrp_default_iftype(ifp
);
178 eigrp_if_update(ifp
);
183 static int eigrp_interface_delete(int command
, struct zclient
*zclient
,
184 zebra_size_t length
, vrf_id_t vrf_id
)
186 struct interface
*ifp
;
190 /* zebra_interface_state_read () updates interface structure in iflist
192 ifp
= zebra_interface_state_read(s
, vrf_id
);
198 zlog_warn("Zebra: got delete of %s, but interface is still up",
201 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
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
);
208 eigrp_if_free(ifp
->info
, INTERFACE_DOWN_BY_ZEBRA
);
210 if_set_index(ifp
, IFINDEX_INTERNAL
);
214 static int eigrp_interface_address_add(int command
, struct zclient
*zclient
,
215 zebra_size_t length
, vrf_id_t vrf_id
)
219 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
224 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
)) {
226 prefix2str(c
->address
, buf
, sizeof(buf
));
227 zlog_debug("Zebra: interface %s address add %s", c
->ifp
->name
,
231 eigrp_if_update(c
->ifp
);
236 static int eigrp_interface_address_delete(int command
, struct zclient
*zclient
,
237 zebra_size_t length
, vrf_id_t vrf_id
)
240 struct interface
*ifp
;
241 struct eigrp_interface
*ei
;
243 c
= zebra_interface_address_read(command
, zclient
->ibuf
, vrf_id
);
248 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
)) {
250 prefix2str(c
->address
, buf
, sizeof(buf
));
251 zlog_debug("Zebra: interface %s address delete %s",
260 /* Call interface hook functions to clean up */
261 eigrp_if_free(ei
, INTERFACE_DOWN_BY_ZEBRA
);
268 static int eigrp_interface_state_up(int command
, struct zclient
*zclient
,
269 zebra_size_t length
, vrf_id_t vrf_id
)
271 struct interface
*ifp
;
273 ifp
= zebra_interface_if_lookup(zclient
->ibuf
);
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
));
284 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
286 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
287 zlog_debug("Zebra: Interface[%s] state update.",
290 if (if_tmp
.bandwidth
!= ifp
->bandwidth
) {
291 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
293 "Zebra: Interface[%s] bandwidth change %d -> %d.",
294 ifp
->name
, if_tmp
.bandwidth
,
297 // eigrp_if_recalculate_output_cost (ifp);
300 if (if_tmp
.mtu
!= ifp
->mtu
) {
301 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
303 "Zebra: Interface[%s] MTU change %u -> %u.",
304 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
306 /* Must reset the interface (simulate down/up) when MTU
313 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
315 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
316 zlog_debug("Zebra: Interface[%s] state change to up.",
320 eigrp_if_up(ifp
->info
);
325 static int eigrp_interface_state_down(int command
, struct zclient
*zclient
,
326 zebra_size_t length
, vrf_id_t vrf_id
)
328 struct interface
*ifp
;
330 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
335 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
336 zlog_debug("Zebra: Interface[%s] state change to down.",
340 eigrp_if_down(ifp
->info
);
345 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
347 char ifname_tmp
[INTERFACE_NAMSIZ
];
349 /* Read interface name. */
350 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
352 /* And look it up. */
353 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
356 void eigrp_zebra_route_add(struct prefix
*p
, struct list
*successors
)
358 struct zapi_route api
;
359 struct zapi_nexthop
*api_nh
;
360 struct eigrp_nexthop_entry
*te
;
361 struct listnode
*node
;
364 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
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
));
373 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
375 /* Nexthop, ifindex, distance and metric information. */
376 for (ALL_LIST_ELEMENTS_RO(successors
, node
, te
)) {
377 if (count
>= MULTIPATH_NUM
)
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
;
385 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
386 api_nh
->ifindex
= te
->ei
->ifp
->ifindex
;
390 api
.nexthop_num
= count
;
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
));
399 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
402 void eigrp_zebra_route_delete(struct prefix
*p
)
404 struct zapi_route api
;
406 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
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
);
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
));
425 int eigrp_is_type_redistributed(int type
)
427 return ((DEFAULT_ROUTE_TYPE(type
))
428 ? vrf_bitmap_check(zclient
->default_information
,
430 : vrf_bitmap_check(zclient
->redist
[AFI_IP
][type
],
434 int eigrp_redistribute_set(struct eigrp
*eigrp
, int type
,
435 struct eigrp_metrics metric
)
438 if (eigrp_is_type_redistributed(type
)) {
439 if (eigrp_metrics_is_same(metric
, eigrp
->dmetric
[type
])) {
440 eigrp
->dmetric
[type
] = metric
;
443 eigrp_external_routes_refresh(eigrp
, type
);
445 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
446 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
448 // eigrp_redist_string(type),
449 // metric_type (eigrp, type), metric_value
454 eigrp
->dmetric
[type
] = metric
;
456 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
, 0,
459 ++eigrp
->redistribute
;
464 int eigrp_redistribute_unset(struct eigrp
*eigrp
, int type
)
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
;