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