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(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
*);
64 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS
);
66 /* Zebra structure to hold current status. */
67 struct zclient
*zclient
= NULL
;
69 /* For registering threads. */
70 extern struct thread_master
*master
;
71 struct in_addr router_id_zebra
;
73 /* Router-id update message from zebra. */
74 static int eigrp_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
77 struct prefix router_id
;
78 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
80 router_id_zebra
= router_id
.u
.prefix4
;
82 eigrp
= eigrp_lookup();
85 eigrp_router_id_update(eigrp
);
90 static int eigrp_zebra_route_notify_owner(ZAPI_CALLBACK_ARGS
)
93 enum zapi_route_notify_owner note
;
96 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
))
102 static void eigrp_zebra_connected(struct zclient
*zclient
)
104 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
107 void eigrp_zebra_init(void)
109 struct zclient_options opt
= {.receive_notify
= false};
111 zclient
= zclient_new(master
, &opt
);
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
;
128 /* Zebra route add and delete treatment. */
129 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
131 struct zapi_route api
;
134 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
137 if (IPV4_NET127(ntohl(api
.prefix
.u
.prefix4
.s_addr
)))
140 eigrp
= eigrp_lookup();
144 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
146 } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
153 /* Inteface addition message from zebra. */
154 static int eigrp_interface_add(ZAPI_CALLBACK_ARGS
)
156 struct interface
*ifp
;
157 struct eigrp_interface
*ei
;
159 ifp
= zebra_interface_add_read(zclient
->ibuf
, vrf_id
);
166 ei
->params
.type
= eigrp_default_iftype(ifp
);
168 eigrp_if_update(ifp
);
173 static int eigrp_interface_delete(ZAPI_CALLBACK_ARGS
)
175 struct interface
*ifp
;
179 /* zebra_interface_state_read () updates interface structure in iflist
181 ifp
= zebra_interface_state_read(s
, vrf_id
);
187 zlog_warn("Zebra: got delete of %s, but interface is still up",
190 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
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
);
197 eigrp_if_free(ifp
->info
, INTERFACE_DOWN_BY_ZEBRA
);
199 if_set_index(ifp
, IFINDEX_INTERNAL
);
203 static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS
)
207 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
212 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
)) {
214 prefix2str(c
->address
, buf
, sizeof(buf
));
215 zlog_debug("Zebra: interface %s address add %s", c
->ifp
->name
,
219 eigrp_if_update(c
->ifp
);
224 static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
227 struct interface
*ifp
;
228 struct eigrp_interface
*ei
;
230 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
235 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
)) {
237 prefix2str(c
->address
, buf
, sizeof(buf
));
238 zlog_debug("Zebra: interface %s address delete %s",
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
);
256 static int eigrp_interface_state_up(ZAPI_CALLBACK_ARGS
)
258 struct interface
*ifp
;
260 ifp
= zebra_interface_if_lookup(zclient
->ibuf
);
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
));
271 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
273 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
274 zlog_debug("Zebra: Interface[%s] state update.",
277 if (if_tmp
.bandwidth
!= ifp
->bandwidth
) {
278 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
280 "Zebra: Interface[%s] bandwidth change %d -> %d.",
281 ifp
->name
, if_tmp
.bandwidth
,
284 // eigrp_if_recalculate_output_cost (ifp);
287 if (if_tmp
.mtu
!= ifp
->mtu
) {
288 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
290 "Zebra: Interface[%s] MTU change %u -> %u.",
291 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
293 /* Must reset the interface (simulate down/up) when MTU
300 zebra_interface_if_set_value(zclient
->ibuf
, ifp
);
302 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
303 zlog_debug("Zebra: Interface[%s] state change to up.",
307 eigrp_if_up(ifp
->info
);
312 static int eigrp_interface_state_down(ZAPI_CALLBACK_ARGS
)
314 struct interface
*ifp
;
316 ifp
= zebra_interface_state_read(zclient
->ibuf
, vrf_id
);
321 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
322 zlog_debug("Zebra: Interface[%s] state change to down.",
326 eigrp_if_down(ifp
->info
);
331 static struct interface
*zebra_interface_if_lookup(struct stream
*s
)
333 char ifname_tmp
[INTERFACE_NAMSIZ
];
335 /* Read interface name. */
336 stream_get(ifname_tmp
, s
, INTERFACE_NAMSIZ
);
338 /* And look it up. */
339 return if_lookup_by_name(ifname_tmp
, VRF_DEFAULT
);
342 void eigrp_zebra_route_add(struct prefix
*p
, struct list
*successors
,
345 struct zapi_route api
;
346 struct zapi_nexthop
*api_nh
;
347 struct eigrp_nexthop_entry
*te
;
348 struct listnode
*node
;
351 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
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
));
361 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
362 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
364 /* Nexthop, ifindex, distance and metric information. */
365 for (ALL_LIST_ELEMENTS_RO(successors
, node
, te
)) {
366 if (count
>= MULTIPATH_NUM
)
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
;
374 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
375 api_nh
->ifindex
= te
->ei
->ifp
->ifindex
;
379 api
.nexthop_num
= count
;
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
));
388 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
391 void eigrp_zebra_route_delete(struct prefix
*p
)
393 struct zapi_route api
;
395 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
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
);
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
));
414 int eigrp_is_type_redistributed(int type
)
416 return ((DEFAULT_ROUTE_TYPE(type
))
417 ? vrf_bitmap_check(zclient
->default_information
[AFI_IP
],
419 : vrf_bitmap_check(zclient
->redist
[AFI_IP
][type
],
423 int eigrp_redistribute_set(struct eigrp
*eigrp
, int type
,
424 struct eigrp_metrics metric
)
427 if (eigrp_is_type_redistributed(type
)) {
428 if (eigrp_metrics_is_same(metric
, eigrp
->dmetric
[type
])) {
429 eigrp
->dmetric
[type
] = metric
;
432 eigrp_external_routes_refresh(eigrp
, type
);
434 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
435 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
437 // eigrp_redist_string(type),
438 // metric_type (eigrp, type), metric_value
443 eigrp
->dmetric
[type
] = metric
;
445 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
, 0,
448 ++eigrp
->redistribute
;
453 int eigrp_redistribute_unset(struct eigrp
*eigrp
, int type
)
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
;