]>
Commit | Line | Data |
---|---|---|
7f57883e DS |
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 | * | |
896014f4 DL |
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 | |
7f57883e DS |
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 | ||
121f9dee QY |
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); | |
d62a17ae | 62 | static struct interface *zebra_interface_if_lookup(struct stream *); |
63 | ||
121f9dee | 64 | static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS); |
7f57883e DS |
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. */ | |
121f9dee | 74 | static int eigrp_router_id_update_zebra(ZAPI_CALLBACK_ARGS) |
7f57883e | 75 | { |
d62a17ae | 76 | struct eigrp *eigrp; |
77 | struct prefix router_id; | |
78 | zebra_router_id_update_read(zclient->ibuf, &router_id); | |
7f57883e | 79 | |
d62a17ae | 80 | router_id_zebra = router_id.u.prefix4; |
7f57883e | 81 | |
d62a17ae | 82 | eigrp = eigrp_lookup(); |
7f57883e | 83 | |
d62a17ae | 84 | if (eigrp != NULL) |
85 | eigrp_router_id_update(eigrp); | |
7f57883e | 86 | |
d62a17ae | 87 | return 0; |
7f57883e DS |
88 | } |
89 | ||
121f9dee | 90 | static int eigrp_zebra_route_notify_owner(ZAPI_CALLBACK_ARGS) |
ca890872 DS |
91 | { |
92 | struct prefix p; | |
93 | enum zapi_route_notify_owner note; | |
28610f7e | 94 | uint32_t table; |
ca890872 | 95 | |
28610f7e | 96 | if (!zapi_route_notify_decode(zclient->ibuf, &p, &table, ¬e)) |
ca890872 DS |
97 | return -1; |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
d62a17ae | 102 | static void eigrp_zebra_connected(struct zclient *zclient) |
08ff1a68 | 103 | { |
d62a17ae | 104 | zclient_send_reg_requests(zclient, VRF_DEFAULT); |
08ff1a68 | 105 | } |
7f57883e | 106 | |
d62a17ae | 107 | void eigrp_zebra_init(void) |
7f57883e | 108 | { |
996c9314 | 109 | struct zclient_options opt = {.receive_notify = false}; |
ca890872 | 110 | |
26f63a1e | 111 | zclient = zclient_new(master, &opt); |
d62a17ae | 112 | |
342213ea | 113 | zclient_init(zclient, ZEBRA_ROUTE_EIGRP, 0, &eigrpd_privs); |
d62a17ae | 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; | |
74489921 RW |
122 | zclient->redistribute_route_add = eigrp_zebra_read_route; |
123 | zclient->redistribute_route_del = eigrp_zebra_read_route; | |
28b11f81 | 124 | zclient->route_notify_owner = eigrp_zebra_route_notify_owner; |
7f57883e DS |
125 | } |
126 | ||
127 | ||
128 | /* Zebra route add and delete treatment. */ | |
121f9dee | 129 | static int eigrp_zebra_read_route(ZAPI_CALLBACK_ARGS) |
7f57883e | 130 | { |
74489921 | 131 | struct zapi_route api; |
d62a17ae | 132 | struct eigrp *eigrp; |
133 | ||
74489921 RW |
134 | if (zapi_route_decode(zclient->ibuf, &api) < 0) |
135 | return -1; | |
d62a17ae | 136 | |
74489921 | 137 | if (IPV4_NET127(ntohl(api.prefix.u.prefix4.s_addr))) |
d62a17ae | 138 | return 0; |
139 | ||
d62a17ae | 140 | eigrp = eigrp_lookup(); |
141 | if (eigrp == NULL) | |
142 | return 0; | |
143 | ||
121f9dee | 144 | if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD) { |
d62a17ae | 145 | |
121f9dee | 146 | } else /* if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_DEL) */ |
d62a17ae | 147 | { |
148 | } | |
149 | ||
150 | return 0; | |
7f57883e DS |
151 | } |
152 | ||
153 | /* Inteface addition message from zebra. */ | |
121f9dee | 154 | static int eigrp_interface_add(ZAPI_CALLBACK_ARGS) |
7f57883e | 155 | { |
d62a17ae | 156 | struct interface *ifp; |
b748db67 | 157 | struct eigrp_interface *ei; |
7f57883e | 158 | |
d62a17ae | 159 | ifp = zebra_interface_add_read(zclient->ibuf, vrf_id); |
7f57883e | 160 | |
b748db67 DS |
161 | if (!ifp->info) |
162 | return 0; | |
7f57883e | 163 | |
b748db67 DS |
164 | ei = ifp->info; |
165 | ||
166 | ei->params.type = eigrp_default_iftype(ifp); | |
7f57883e | 167 | |
d62a17ae | 168 | eigrp_if_update(ifp); |
7f57883e | 169 | |
d62a17ae | 170 | return 0; |
7f57883e DS |
171 | } |
172 | ||
121f9dee | 173 | static int eigrp_interface_delete(ZAPI_CALLBACK_ARGS) |
7f57883e | 174 | { |
d62a17ae | 175 | struct interface *ifp; |
176 | struct stream *s; | |
d62a17ae | 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 | ||
b748db67 | 196 | if (ifp->info) |
996c9314 | 197 | eigrp_if_free(ifp->info, INTERFACE_DOWN_BY_ZEBRA); |
d62a17ae | 198 | |
ff880b78 | 199 | if_set_index(ifp, IFINDEX_INTERNAL); |
d62a17ae | 200 | return 0; |
7f57883e DS |
201 | } |
202 | ||
121f9dee | 203 | static int eigrp_interface_address_add(ZAPI_CALLBACK_ARGS) |
7f57883e | 204 | { |
d62a17ae | 205 | struct connected *c; |
7f57883e | 206 | |
121f9dee | 207 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
7f57883e | 208 | |
d62a17ae | 209 | if (c == NULL) |
210 | return 0; | |
7f57883e | 211 | |
d62a17ae | 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 | } | |
7f57883e | 218 | |
d62a17ae | 219 | eigrp_if_update(c->ifp); |
7f57883e | 220 | |
d62a17ae | 221 | return 0; |
7f57883e DS |
222 | } |
223 | ||
121f9dee | 224 | static int eigrp_interface_address_delete(ZAPI_CALLBACK_ARGS) |
7f57883e | 225 | { |
d62a17ae | 226 | struct connected *c; |
227 | struct interface *ifp; | |
228 | struct eigrp_interface *ei; | |
7f57883e | 229 | |
121f9dee | 230 | c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id); |
7f57883e | 231 | |
d62a17ae | 232 | if (c == NULL) |
233 | return 0; | |
7f57883e | 234 | |
d62a17ae | 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 | } | |
7f57883e | 241 | |
d62a17ae | 242 | ifp = c->ifp; |
b748db67 DS |
243 | ei = ifp->info; |
244 | if (!ei) | |
d62a17ae | 245 | return 0; |
7f57883e | 246 | |
d62a17ae | 247 | /* Call interface hook functions to clean up */ |
84b5e534 DS |
248 | if (prefix_cmp(&ei->address, c->address) == 0) |
249 | eigrp_if_free(ei, INTERFACE_DOWN_BY_ZEBRA); | |
7f57883e | 250 | |
d62a17ae | 251 | connected_free(c); |
7f57883e | 252 | |
d62a17ae | 253 | return 0; |
7f57883e DS |
254 | } |
255 | ||
121f9dee | 256 | static int eigrp_interface_state_up(ZAPI_CALLBACK_ARGS) |
7f57883e | 257 | { |
d62a17ae | 258 | struct interface *ifp; |
7f57883e | 259 | |
d62a17ae | 260 | ifp = zebra_interface_if_lookup(zclient->ibuf); |
7f57883e | 261 | |
d62a17ae | 262 | if (ifp == NULL) |
263 | return 0; | |
7f57883e | 264 | |
d62a17ae | 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)); | |
7f57883e | 270 | |
d62a17ae | 271 | zebra_interface_if_set_value(zclient->ibuf, ifp); |
7f57883e | 272 | |
d62a17ae | 273 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) |
274 | zlog_debug("Zebra: Interface[%s] state update.", | |
275 | ifp->name); | |
7f57883e | 276 | |
d62a17ae | 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); | |
7f57883e | 283 | |
d62a17ae | 284 | // eigrp_if_recalculate_output_cost (ifp); |
285 | } | |
7f57883e | 286 | |
d62a17ae | 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); | |
7f57883e | 292 | |
d62a17ae | 293 | /* Must reset the interface (simulate down/up) when MTU |
294 | * changes. */ | |
295 | eigrp_if_reset(ifp); | |
296 | } | |
297 | return 0; | |
298 | } | |
7f57883e | 299 | |
d62a17ae | 300 | zebra_interface_if_set_value(zclient->ibuf, ifp); |
7f57883e | 301 | |
d62a17ae | 302 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) |
303 | zlog_debug("Zebra: Interface[%s] state change to up.", | |
304 | ifp->name); | |
7f57883e | 305 | |
b748db67 DS |
306 | if (ifp->info) |
307 | eigrp_if_up(ifp->info); | |
7f57883e | 308 | |
d62a17ae | 309 | return 0; |
7f57883e DS |
310 | } |
311 | ||
121f9dee | 312 | static int eigrp_interface_state_down(ZAPI_CALLBACK_ARGS) |
7f57883e | 313 | { |
d62a17ae | 314 | struct interface *ifp; |
7f57883e | 315 | |
d62a17ae | 316 | ifp = zebra_interface_state_read(zclient->ibuf, vrf_id); |
7f57883e | 317 | |
d62a17ae | 318 | if (ifp == NULL) |
319 | return 0; | |
7f57883e | 320 | |
d62a17ae | 321 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) |
322 | zlog_debug("Zebra: Interface[%s] state change to down.", | |
323 | ifp->name); | |
7f57883e | 324 | |
b748db67 DS |
325 | if (ifp->info) |
326 | eigrp_if_down(ifp->info); | |
7f57883e | 327 | |
d62a17ae | 328 | return 0; |
7f57883e DS |
329 | } |
330 | ||
d62a17ae | 331 | static struct interface *zebra_interface_if_lookup(struct stream *s) |
7f57883e | 332 | { |
d62a17ae | 333 | char ifname_tmp[INTERFACE_NAMSIZ]; |
7f57883e | 334 | |
d62a17ae | 335 | /* Read interface name. */ |
336 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
7f57883e | 337 | |
d62a17ae | 338 | /* And look it up. */ |
bcc24579 | 339 | return if_lookup_by_name(ifname_tmp, VRF_DEFAULT); |
7f57883e DS |
340 | } |
341 | ||
0f9bc496 DS |
342 | void eigrp_zebra_route_add(struct prefix *p, struct list *successors, |
343 | uint32_t distance) | |
7f57883e | 344 | { |
8fb753e3 RW |
345 | struct zapi_route api; |
346 | struct zapi_nexthop *api_nh; | |
255ab940 | 347 | struct eigrp_nexthop_entry *te; |
d62a17ae | 348 | struct listnode *node; |
8fb753e3 | 349 | int count = 0; |
d62a17ae | 350 | |
d00061ea RW |
351 | if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP]) |
352 | return; | |
353 | ||
8fb753e3 RW |
354 | memset(&api, 0, sizeof(api)); |
355 | api.vrf_id = VRF_DEFAULT; | |
356 | api.type = ZEBRA_ROUTE_EIGRP; | |
357 | api.safi = SAFI_UNICAST; | |
0f9bc496 | 358 | api.metric = distance; |
8fb753e3 | 359 | memcpy(&api.prefix, p, sizeof(*p)); |
d00061ea | 360 | |
8fb753e3 | 361 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
0f9bc496 | 362 | SET_FLAG(api.message, ZAPI_MESSAGE_METRIC); |
d00061ea RW |
363 | |
364 | /* Nexthop, ifindex, distance and metric information. */ | |
365 | for (ALL_LIST_ELEMENTS_RO(successors, node, te)) { | |
a74e593b RW |
366 | if (count >= MULTIPATH_NUM) |
367 | break; | |
8fb753e3 | 368 | api_nh = &api.nexthops[count]; |
4a7371e9 | 369 | api_nh->vrf_id = VRF_DEFAULT; |
d00061ea | 370 | if (te->adv_router->src.s_addr) { |
8fb753e3 RW |
371 | api_nh->gate.ipv4 = te->adv_router->src; |
372 | api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
d00061ea | 373 | } else |
8fb753e3 RW |
374 | api_nh->type = NEXTHOP_TYPE_IFINDEX; |
375 | api_nh->ifindex = te->ei->ifp->ifindex; | |
376 | ||
377 | count++; | |
d00061ea | 378 | } |
a74e593b | 379 | api.nexthop_num = count; |
d62a17ae | 380 | |
d00061ea | 381 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) { |
836aad7e DS |
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)); | |
d00061ea | 386 | } |
d62a17ae | 387 | |
8fb753e3 | 388 | zclient_route_send(ZEBRA_ROUTE_ADD, zclient, &api); |
7f57883e DS |
389 | } |
390 | ||
836aad7e | 391 | void eigrp_zebra_route_delete(struct prefix *p) |
7f57883e | 392 | { |
8fb753e3 | 393 | struct zapi_route api; |
d62a17ae | 394 | |
d00061ea RW |
395 | if (!zclient->redist[AFI_IP][ZEBRA_ROUTE_EIGRP]) |
396 | return; | |
397 | ||
8fb753e3 | 398 | memset(&api, 0, sizeof(api)); |
d00061ea RW |
399 | api.vrf_id = VRF_DEFAULT; |
400 | api.type = ZEBRA_ROUTE_EIGRP; | |
d00061ea | 401 | api.safi = SAFI_UNICAST; |
8fb753e3 RW |
402 | memcpy(&api.prefix, p, sizeof(*p)); |
403 | zclient_route_send(ZEBRA_ROUTE_DELETE, zclient, &api); | |
d00061ea RW |
404 | |
405 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_REDISTRIBUTE)) { | |
836aad7e DS |
406 | char buf[PREFIX_STRLEN]; |
407 | zlog_debug("Zebra: Route del %s", | |
408 | prefix2str(p, buf, PREFIX_STRLEN)); | |
d62a17ae | 409 | } |
410 | ||
411 | return; | |
7f57883e DS |
412 | } |
413 | ||
d62a17ae | 414 | int eigrp_is_type_redistributed(int type) |
7f57883e | 415 | { |
d62a17ae | 416 | return ((DEFAULT_ROUTE_TYPE(type)) |
49db7a7b | 417 | ? vrf_bitmap_check(zclient->default_information[AFI_IP], |
d62a17ae | 418 | VRF_DEFAULT) |
419 | : vrf_bitmap_check(zclient->redist[AFI_IP][type], | |
420 | VRF_DEFAULT)); | |
7f57883e DS |
421 | } |
422 | ||
d62a17ae | 423 | int eigrp_redistribute_set(struct eigrp *eigrp, int type, |
424 | struct eigrp_metrics metric) | |
7f57883e DS |
425 | { |
426 | ||
d62a17ae | 427 | if (eigrp_is_type_redistributed(type)) { |
428 | if (eigrp_metrics_is_same(metric, eigrp->dmetric[type])) { | |
429 | eigrp->dmetric[type] = metric; | |
430 | } | |
7f57883e | 431 | |
d62a17ae | 432 | eigrp_external_routes_refresh(eigrp, type); |
7f57883e | 433 | |
d62a17ae | 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 | } | |
7f57883e | 442 | |
d62a17ae | 443 | eigrp->dmetric[type] = metric; |
7f57883e | 444 | |
d62a17ae | 445 | zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, type, 0, |
446 | VRF_DEFAULT); | |
7f57883e | 447 | |
d62a17ae | 448 | ++eigrp->redistribute; |
7f57883e | 449 | |
d62a17ae | 450 | return CMD_SUCCESS; |
7f57883e DS |
451 | } |
452 | ||
d62a17ae | 453 | int eigrp_redistribute_unset(struct eigrp *eigrp, int type) |
7f57883e DS |
454 | { |
455 | ||
d62a17ae | 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 | } | |
7f57883e | 462 | |
d62a17ae | 463 | return CMD_SUCCESS; |
7f57883e | 464 | } |