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_types.h"
45 #include "eigrpd/eigrp_structs.h"
46 #include "eigrpd/eigrpd.h"
47 #include "eigrpd/eigrp_interface.h"
48 #include "eigrpd/eigrp_neighbor.h"
49 #include "eigrpd/eigrp_packet.h"
50 #include "eigrpd/eigrp_zebra.h"
51 #include "eigrpd/eigrp_vty.h"
52 #include "eigrpd/eigrp_dump.h"
53 #include "eigrpd/eigrp_network.h"
54 #include "eigrpd/eigrp_topology.h"
55 #include "eigrpd/eigrp_fsm.h"
56 #include "eigrpd/eigrp_metric.h"
58 static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS
);
59 static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS
);
61 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS
);
63 /* Zebra structure to hold current status. */
64 struct zclient
*zclient
= NULL
;
66 /* For registering threads. */
67 extern struct thread_master
*master
;
68 struct in_addr router_id_zebra
;
70 /* Router-id update message from zebra. */
71 static int eigrp_router_id_update_zebra(ZAPI_CALLBACK_ARGS
)
74 struct prefix router_id
;
75 zebra_router_id_update_read(zclient
->ibuf
, &router_id
);
77 router_id_zebra
= router_id
.u
.prefix4
;
79 eigrp
= eigrp_lookup(vrf_id
);
82 eigrp_router_id_update(eigrp
);
87 static int eigrp_zebra_route_notify_owner(ZAPI_CALLBACK_ARGS
)
90 enum zapi_route_notify_owner note
;
93 if (!zapi_route_notify_decode(zclient
->ibuf
, &p
, &table
, ¬e
, NULL
,
100 static void eigrp_zebra_connected(struct zclient
*zclient
)
102 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
105 static zclient_handler
*const eigrp_handlers
[] = {
106 [ZEBRA_ROUTER_ID_UPDATE
] = eigrp_router_id_update_zebra
,
107 [ZEBRA_INTERFACE_ADDRESS_ADD
] = eigrp_interface_address_add
,
108 [ZEBRA_INTERFACE_ADDRESS_DELETE
] = eigrp_interface_address_delete
,
109 [ZEBRA_REDISTRIBUTE_ROUTE_ADD
] = eigrp_zebra_read_route
,
110 [ZEBRA_REDISTRIBUTE_ROUTE_DEL
] = eigrp_zebra_read_route
,
111 [ZEBRA_ROUTE_NOTIFY_OWNER
] = eigrp_zebra_route_notify_owner
,
114 void eigrp_zebra_init(void)
116 struct zclient_options opt
= {.receive_notify
= false};
118 zclient
= zclient_new(master
, &opt
, eigrp_handlers
,
119 array_size(eigrp_handlers
));
121 zclient_init(zclient
, ZEBRA_ROUTE_EIGRP
, 0, &eigrpd_privs
);
122 zclient
->zebra_connected
= eigrp_zebra_connected
;
126 /* Zebra route add and delete treatment. */
127 static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS
)
129 struct zapi_route api
;
132 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
135 if (IPV4_NET127(ntohl(api
.prefix
.u
.prefix4
.s_addr
)))
138 eigrp
= eigrp_lookup(vrf_id
);
142 if (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
) {
144 } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */
151 static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS
)
155 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
160 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
161 zlog_debug("Zebra: interface %s address add %pFX", c
->ifp
->name
,
164 eigrp_if_update(c
->ifp
);
169 static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
172 struct interface
*ifp
;
173 struct eigrp_interface
*ei
;
175 c
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
180 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_INTERFACE
))
181 zlog_debug("Zebra: interface %s address delete %pFX",
182 c
->ifp
->name
, c
->address
);
189 /* Call interface hook functions to clean up */
190 if (prefix_cmp(&ei
->address
, c
->address
) == 0)
191 eigrp_if_free(ei
, INTERFACE_DOWN_BY_ZEBRA
);
198 void eigrp_zebra_route_add(struct eigrp
*eigrp
, struct prefix
*p
,
199 struct list
*successors
, uint32_t distance
)
201 struct zapi_route api
;
202 struct zapi_nexthop
*api_nh
;
203 struct eigrp_route_descriptor
*te
;
204 struct listnode
*node
;
207 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
210 memset(&api
, 0, sizeof(api
));
211 api
.vrf_id
= eigrp
->vrf_id
;
212 api
.type
= ZEBRA_ROUTE_EIGRP
;
213 api
.safi
= SAFI_UNICAST
;
214 api
.metric
= distance
;
215 memcpy(&api
.prefix
, p
, sizeof(*p
));
217 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
218 SET_FLAG(api
.message
, ZAPI_MESSAGE_METRIC
);
220 /* Nexthop, ifindex, distance and metric information. */
221 for (ALL_LIST_ELEMENTS_RO(successors
, node
, te
)) {
222 if (count
>= MULTIPATH_NUM
)
224 api_nh
= &api
.nexthops
[count
];
225 api_nh
->vrf_id
= eigrp
->vrf_id
;
226 if (te
->adv_router
->src
.s_addr
) {
227 api_nh
->gate
.ipv4
= te
->adv_router
->src
;
228 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
230 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
231 api_nh
->ifindex
= te
->ei
->ifp
->ifindex
;
235 api
.nexthop_num
= count
;
237 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_REDISTRIBUTE
)) {
238 zlog_debug("Zebra: Route add %pFX", p
);
241 zclient_route_send(ZEBRA_ROUTE_ADD
, zclient
, &api
);
244 void eigrp_zebra_route_delete(struct eigrp
*eigrp
, struct prefix
*p
)
246 struct zapi_route api
;
248 if (!zclient
->redist
[AFI_IP
][ZEBRA_ROUTE_EIGRP
])
251 memset(&api
, 0, sizeof(api
));
252 api
.vrf_id
= eigrp
->vrf_id
;
253 api
.type
= ZEBRA_ROUTE_EIGRP
;
254 api
.safi
= SAFI_UNICAST
;
255 memcpy(&api
.prefix
, p
, sizeof(*p
));
256 zclient_route_send(ZEBRA_ROUTE_DELETE
, zclient
, &api
);
258 if (IS_DEBUG_EIGRP(zebra
, ZEBRA_REDISTRIBUTE
))
259 zlog_debug("Zebra: Route del %pFX", p
);
264 static int eigrp_is_type_redistributed(int type
, vrf_id_t vrf_id
)
266 return ((DEFAULT_ROUTE_TYPE(type
))
267 ? vrf_bitmap_check(zclient
->default_information
[AFI_IP
],
269 : vrf_bitmap_check(zclient
->redist
[AFI_IP
][type
],
273 int eigrp_redistribute_set(struct eigrp
*eigrp
, int type
,
274 struct eigrp_metrics metric
)
277 if (eigrp_is_type_redistributed(type
, eigrp
->vrf_id
)) {
278 if (eigrp_metrics_is_same(metric
, eigrp
->dmetric
[type
])) {
279 eigrp
->dmetric
[type
] = metric
;
282 eigrp_external_routes_refresh(eigrp
, type
);
284 // if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE))
285 // zlog_debug ("Redistribute[%s]: Refresh Type[%d],
287 // eigrp_redist_string(type),
288 // metric_type (eigrp, type), metric_value
293 eigrp
->dmetric
[type
] = metric
;
295 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
, type
, 0,
298 ++eigrp
->redistribute
;
303 int eigrp_redistribute_unset(struct eigrp
*eigrp
, int type
)
306 if (eigrp_is_type_redistributed(type
, eigrp
->vrf_id
)) {
307 memset(&eigrp
->dmetric
[type
], 0, sizeof(struct eigrp_metrics
));
308 zclient_redistribute(ZEBRA_REDISTRIBUTE_DELETE
, zclient
, AFI_IP
,
309 type
, 0, eigrp
->vrf_id
);
310 --eigrp
->redistribute
;