]> git.proxmox.com Git - mirror_frr.git/blame - eigrpd/eigrp_interface.c
Merge pull request #7981 from gromit1811/fix_ospf6_lsa_str_retval
[mirror_frr.git] / eigrpd / eigrp_interface.c
CommitLineData
7f57883e
DS
1/*
2 * EIGRP Interface Functions.
3 * Copyright (C) 2013-2016
4 * Authors:
5 * Donnie Savage
6 * Jan Janovic
7 * Matej Perina
8 * Peter Orsag
9 * Peter Paluch
10 * Frantisek Gazo
11 * Tomas Hvorkovy
12 * Martin Kontsek
13 * Lukas Koribsky
14 *
15 * This file is part of GNU Zebra.
16 *
17 * GNU Zebra is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2, or (at your option) any
20 * later version.
21 *
22 * GNU Zebra is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
896014f4
DL
27 * You should have received a copy of the GNU General Public License along
28 * with this program; see the file COPYING; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
7f57883e
DS
30 */
31
32#include <zebra.h>
33
34#include "thread.h"
35#include "linklist.h"
36#include "prefix.h"
37#include "if.h"
38#include "table.h"
39#include "memory.h"
40#include "command.h"
41#include "stream.h"
42#include "log.h"
43#include "keychain.h"
44#include "vrf.h"
45
46#include "eigrpd/eigrp_structs.h"
47#include "eigrpd/eigrpd.h"
48#include "eigrpd/eigrp_interface.h"
49#include "eigrpd/eigrp_neighbor.h"
50#include "eigrpd/eigrp_packet.h"
51#include "eigrpd/eigrp_zebra.h"
52#include "eigrpd/eigrp_vty.h"
53#include "eigrpd/eigrp_network.h"
54#include "eigrpd/eigrp_topology.h"
55#include "eigrpd/eigrp_memory.h"
348addb4 56#include "eigrpd/eigrp_fsm.h"
ddbf3e60 57#include "eigrpd/eigrp_dump.h"
e9f1847e
DS
58#include "eigrpd/eigrp_types.h"
59#include "eigrpd/eigrp_metric.h"
7f57883e 60
d62a17ae 61struct eigrp_interface *eigrp_if_new(struct eigrp *eigrp, struct interface *ifp,
62 struct prefix *p)
7f57883e 63{
b748db67 64 struct eigrp_interface *ei = ifp->info;
d62a17ae 65 int i;
7f57883e 66
b748db67 67 if (ei)
d62a17ae 68 return ei;
7f57883e 69
b748db67
DS
70 ei = XCALLOC(MTYPE_EIGRP_IF, sizeof(struct eigrp_interface));
71
d62a17ae 72 /* Set zebra interface pointer. */
73 ei->ifp = ifp;
b245781a 74 prefix_copy(&ei->address, p);
7f57883e 75
b748db67 76 ifp->info = ei;
d62a17ae 77 listnode_add(eigrp->eiflist, ei);
7f57883e 78
d62a17ae 79 ei->type = EIGRP_IFTYPE_BROADCAST;
7f57883e 80
d62a17ae 81 /* Initialize neighbor list. */
82 ei->nbrs = list_new();
7f57883e 83
d62a17ae 84 ei->crypt_seqnum = time(NULL);
7f57883e 85
d62a17ae 86 /* Initialize lists */
87 for (i = 0; i < EIGRP_FILTER_MAX; i++) {
88 ei->list[i] = NULL;
89 ei->prefix[i] = NULL;
90 ei->routemap[i] = NULL;
91 }
7f57883e 92
b748db67 93 ei->eigrp = eigrp;
7f57883e 94
b748db67
DS
95 ei->params.v_hello = EIGRP_HELLO_INTERVAL_DEFAULT;
96 ei->params.v_wait = EIGRP_HOLD_INTERVAL_DEFAULT;
97 ei->params.bandwidth = EIGRP_BANDWIDTH_DEFAULT;
98 ei->params.delay = EIGRP_DELAY_DEFAULT;
99 ei->params.reliability = EIGRP_RELIABILITY_DEFAULT;
100 ei->params.load = EIGRP_LOAD_DEFAULT;
101 ei->params.auth_type = EIGRP_AUTH_TYPE_NONE;
102 ei->params.auth_keychain = NULL;
7f57883e 103
85d25587
DS
104 ei->curr_bandwidth = ifp->bandwidth;
105 ei->curr_mtu = ifp->mtu;
106
b748db67 107 return ei;
7f57883e
DS
108}
109
d62a17ae 110int eigrp_if_delete_hook(struct interface *ifp)
7f57883e 111{
b748db67
DS
112 struct eigrp_interface *ei = ifp->info;
113 struct eigrp *eigrp;
7f57883e 114
b748db67
DS
115 if (!ei)
116 return 0;
117
6a154c88 118 list_delete(&ei->nbrs);
7f57883e 119
b748db67
DS
120 eigrp = ei->eigrp;
121 listnode_delete(eigrp->eiflist, ei);
7f57883e 122
c45db77d
DS
123 eigrp_fifo_free(ei->obuf);
124
d62a17ae 125 XFREE(MTYPE_EIGRP_IF_INFO, ifp->info);
7f57883e 126
d62a17ae 127 return 0;
7f57883e
DS
128}
129
138c5a74
DS
130static int eigrp_ifp_create(struct interface *ifp)
131{
ef7bd2a3
DS
132 struct eigrp_interface *ei = ifp->info;
133
134 if (!ei)
135 return 0;
136
137 ei->params.type = eigrp_default_iftype(ifp);
138
139 eigrp_if_update(ifp);
140
138c5a74
DS
141 return 0;
142}
143
144static int eigrp_ifp_up(struct interface *ifp)
145{
85d25587 146 struct eigrp_interface *ei = ifp->info;
ddbf3e60
DS
147
148 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
149 zlog_debug("Zebra: Interface[%s] state change to up.",
150 ifp->name);
151
85d25587
DS
152 if (!ei)
153 return 0;
154
155 if (ei->curr_bandwidth != ifp->bandwidth) {
156 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
157 zlog_debug(
158 "Zebra: Interface[%s] bandwidth change %d -> %d.",
159 ifp->name, ei->curr_bandwidth,
160 ifp->bandwidth);
161
162 ei->curr_bandwidth = ifp->bandwidth;
163 // eigrp_if_recalculate_output_cost (ifp);
164 }
165
166 if (ei->curr_mtu != ifp->mtu) {
167 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
168 zlog_debug(
169 "Zebra: Interface[%s] MTU change %u -> %u.",
170 ifp->name, ei->curr_mtu, ifp->mtu);
171
172 ei->curr_mtu = ifp->mtu;
173 /* Must reset the interface (simulate down/up) when MTU
174 * changes. */
175 eigrp_if_reset(ifp);
176 return 0;
177 }
178
179 eigrp_if_up(ifp->info);
ddbf3e60 180
138c5a74
DS
181 return 0;
182}
183
184static int eigrp_ifp_down(struct interface *ifp)
185{
b0b69e59
DS
186 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
187 zlog_debug("Zebra: Interface[%s] state change to down.",
188 ifp->name);
189
190 if (ifp->info)
191 eigrp_if_down(ifp->info);
192
138c5a74
DS
193 return 0;
194}
195
196static int eigrp_ifp_destroy(struct interface *ifp)
197{
3c3c3252
DS
198 if (if_is_up(ifp))
199 zlog_warn("Zebra: got delete of %s, but interface is still up",
200 ifp->name);
201
202 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
203 zlog_debug(
204 "Zebra: interface delete %s index %d flags %llx metric %d mtu %d",
205 ifp->name, ifp->ifindex, (unsigned long long)ifp->flags,
206 ifp->metric, ifp->mtu);
207
208 if (ifp->info)
209 eigrp_if_free(ifp->info, INTERFACE_DOWN_BY_ZEBRA);
210
138c5a74
DS
211 return 0;
212}
213
7f57883e
DS
214struct list *eigrp_iflist;
215
4d762f26 216void eigrp_if_init(void)
7f57883e 217{
138c5a74
DS
218 if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up,
219 eigrp_ifp_down, eigrp_ifp_destroy);
d62a17ae 220 /* Initialize Zebra interface data structure. */
996c9314 221 // hook_register_prio(if_add, 0, eigrp_if_new);
ce19a04a 222 hook_register_prio(if_del, 0, eigrp_if_delete_hook);
7f57883e
DS
223}
224
7f57883e 225
d62a17ae 226void eigrp_del_if_params(struct eigrp_if_params *eip)
7f57883e 227{
d62a17ae 228 if (eip->auth_keychain)
229 free(eip->auth_keychain);
7f57883e
DS
230}
231
7c6ff2c5
DS
232/*
233 * Set the network byte order of the 3 bytes we send
234 * of the mtu of the link.
235 */
236static void eigrp_mtu_convert(struct eigrp_metrics *metric, uint32_t host_mtu)
237{
238 uint32_t network_mtu = htonl(host_mtu);
239 uint8_t *nm = (uint8_t *)&network_mtu;
240
241 metric->mtu[0] = nm[1];
242 metric->mtu[1] = nm[2];
243 metric->mtu[2] = nm[3];
244}
245
d62a17ae 246int eigrp_if_up(struct eigrp_interface *ei)
7f57883e 247{
dc4accdd
DS
248 struct eigrp_prefix_descriptor *pe;
249 struct eigrp_route_descriptor *ne;
d62a17ae 250 struct eigrp_metrics metric;
251 struct eigrp_interface *ei2;
252 struct listnode *node, *nnode;
dbfd865b 253 struct eigrp *eigrp;
d62a17ae 254
255 if (ei == NULL)
256 return 0;
257
dbfd865b
DS
258 eigrp = ei->eigrp;
259 eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu);
260
d62a17ae 261 eigrp_if_stream_set(ei);
262
263 /* Set multicast memberships appropriately for new state. */
264 eigrp_if_set_multicast(ei);
265
266 thread_add_event(master, eigrp_hello_timer, ei, (1), NULL);
267
268 /*Prepare metrics*/
996c9314 269 metric.bandwidth = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
b748db67
DS
270 metric.delay = eigrp_delay_to_scaled(ei->params.delay);
271 metric.load = ei->params.load;
272 metric.reliability = ei->params.reliability;
7c6ff2c5 273 eigrp_mtu_convert(&metric, ei->ifp->mtu);
d62a17ae 274 metric.hop_count = 0;
275 metric.flags = 0;
276 metric.tag = 0;
277
278 /*Add connected entry to topology table*/
279
dc4accdd 280 ne = eigrp_route_descriptor_new();
348addb4
DS
281 ne->ei = ei;
282 ne->reported_metric = metric;
283 ne->total_metric = metric;
284 ne->distance = eigrp_calculate_metrics(eigrp, metric);
285 ne->reported_distance = 0;
286 ne->adv_router = eigrp->neighbor_self;
dc4accdd 287 ne->flags = EIGRP_ROUTE_DESCRIPTOR_SUCCESSOR_FLAG;
348addb4 288
02b45998 289 struct prefix dest_addr;
d62a17ae 290
b245781a 291 dest_addr = ei->address;
02b45998 292 apply_mask(&dest_addr);
d62a17ae 293 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
476a1469 294 &dest_addr);
d62a17ae 295
296 if (pe == NULL) {
dc4accdd 297 pe = eigrp_prefix_descriptor_new();
d62a17ae 298 pe->serno = eigrp->serno;
02b45998
DS
299 pe->destination = (struct prefix *)prefix_ipv4_new();
300 prefix_copy(pe->destination, &dest_addr);
d62a17ae 301 pe->af = AF_INET;
302 pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED;
303
348addb4 304 ne->prefix = pe;
cd6c573c 305 pe->reported_metric = metric;
d62a17ae 306 pe->state = EIGRP_FSM_STATE_PASSIVE;
307 pe->fdistance = eigrp_calculate_metrics(eigrp, metric);
308 pe->req_action |= EIGRP_FSM_NEED_UPDATE;
dc4accdd 309 eigrp_prefix_descriptor_add(eigrp->topology_table, pe);
d62a17ae 310 listnode_add(eigrp->topology_changes_internalIPV4, pe);
d62a17ae 311
dc4accdd 312 eigrp_route_descriptor_add(eigrp, pe, ne);
d62a17ae 313
348addb4
DS
314 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei2)) {
315 eigrp_update_send(ei2);
316 }
317
318 pe->req_action &= ~EIGRP_FSM_NEED_UPDATE;
319 listnode_delete(eigrp->topology_changes_internalIPV4, pe);
320 } else {
321 struct eigrp_fsm_action_message msg;
322
323 ne->prefix = pe;
dc4accdd 324 eigrp_route_descriptor_add(eigrp, pe, ne);
348addb4
DS
325
326 msg.packet_type = EIGRP_OPC_UPDATE;
327 msg.eigrp = eigrp;
328 msg.data_type = EIGRP_CONNECTED;
329 msg.adv_router = NULL;
330 msg.entry = ne;
331 msg.prefix = pe;
332
333 eigrp_fsm_event(&msg);
334 }
d62a17ae 335
336 return 1;
7f57883e
DS
337}
338
d62a17ae 339int eigrp_if_down(struct eigrp_interface *ei)
7f57883e 340{
d62a17ae 341 struct listnode *node, *nnode;
342 struct eigrp_neighbor *nbr;
7f57883e 343
d62a17ae 344 if (ei == NULL)
345 return 0;
7f57883e 346
d62a17ae 347 /* Shutdown packet reception and sending */
28ef0ee1 348 THREAD_OFF(ei->t_hello);
7f57883e 349
d62a17ae 350 eigrp_if_stream_unset(ei);
7f57883e 351
d62a17ae 352 /*Set infinite metrics to routes learned by this interface and start
353 * query process*/
354 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
355 eigrp_nbr_delete(nbr);
356 }
7f57883e 357
d62a17ae 358 return 1;
7f57883e
DS
359}
360
d62a17ae 361void eigrp_if_stream_set(struct eigrp_interface *ei)
7f57883e 362{
d62a17ae 363 /* set output fifo queue. */
364 if (ei->obuf == NULL)
365 ei->obuf = eigrp_fifo_new();
7f57883e
DS
366}
367
d62a17ae 368void eigrp_if_stream_unset(struct eigrp_interface *ei)
7f57883e 369{
d62a17ae 370 struct eigrp *eigrp = ei->eigrp;
371
c45db77d
DS
372 if (ei->on_write_q) {
373 listnode_delete(eigrp->oi_write_q, ei);
374 if (list_isempty(eigrp->oi_write_q))
b3d6bc6e 375 thread_cancel(&(eigrp->t_write));
c45db77d 376 ei->on_write_q = 0;
d62a17ae 377 }
7f57883e
DS
378}
379
b748db67
DS
380bool eigrp_if_is_passive(struct eigrp_interface *ei)
381{
382 if (ei->params.passive_interface == EIGRP_IF_ACTIVE)
383 return false;
384
385 if (ei->eigrp->passive_interface_default == EIGRP_IF_ACTIVE)
386 return false;
387
388 return true;
389}
390
d62a17ae 391void eigrp_if_set_multicast(struct eigrp_interface *ei)
7f57883e 392{
b748db67 393 if (!eigrp_if_is_passive(ei)) {
d62a17ae 394 /* The interface should belong to the EIGRP-all-routers group.
395 */
b748db67 396 if (!ei->member_allrouters
b245781a 397 && (eigrp_if_add_allspfrouters(ei->eigrp, &ei->address,
d62a17ae 398 ei->ifp->ifindex)
399 >= 0))
400 /* Set the flag only if the system call to join
401 * succeeded. */
b748db67 402 ei->member_allrouters = true;
d62a17ae 403 } else {
404 /* The interface should NOT belong to the EIGRP-all-routers
405 * group. */
b748db67 406 if (ei->member_allrouters) {
d62a17ae 407 /* Only actually drop if this is the last reference */
b245781a 408 eigrp_if_drop_allspfrouters(ei->eigrp, &ei->address,
b748db67 409 ei->ifp->ifindex);
d62a17ae 410 /* Unset the flag regardless of whether the system call
411 to leave
412 the group succeeded, since it's much safer to assume
413 that
414 we are not a member. */
b748db67 415 ei->member_allrouters = false;
d62a17ae 416 }
417 }
7f57883e
DS
418}
419
d7c0a89a 420uint8_t eigrp_default_iftype(struct interface *ifp)
7f57883e 421{
d62a17ae 422 if (if_is_pointopoint(ifp))
423 return EIGRP_IFTYPE_POINTOPOINT;
424 else if (if_is_loopback(ifp))
425 return EIGRP_IFTYPE_LOOPBACK;
426 else
427 return EIGRP_IFTYPE_BROADCAST;
7f57883e
DS
428}
429
996c9314 430void eigrp_if_free(struct eigrp_interface *ei, int source)
7f57883e 431{
476a1469 432 struct prefix dest_addr;
dc4accdd 433 struct eigrp_prefix_descriptor *pe;
e9449961 434 struct eigrp *eigrp = ei->eigrp;
0bf75bd5 435
d62a17ae 436 if (source == INTERFACE_DOWN_BY_VTY) {
50478845 437 thread_cancel(&ei->t_hello);
d62a17ae 438 eigrp_hello_send(ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN, NULL);
439 }
440
b245781a 441 dest_addr = ei->address;
476a1469 442 apply_mask(&dest_addr);
d62a17ae 443 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
444 &dest_addr);
445 if (pe)
dc4accdd
DS
446 eigrp_prefix_descriptor_delete(eigrp, eigrp->topology_table,
447 pe);
d62a17ae 448
449 eigrp_if_down(ei);
450
d62a17ae 451 listnode_delete(ei->eigrp->eiflist, ei);
7f57883e
DS
452}
453
454/* Simulate down/up on the interface. This is needed, for example, when
f9e5c9ca 455 the MTU changes. */
d62a17ae 456void eigrp_if_reset(struct interface *ifp)
7f57883e 457{
b748db67 458 struct eigrp_interface *ei = ifp->info;
7f57883e 459
b748db67
DS
460 if (!ei)
461 return;
7f57883e 462
b748db67
DS
463 eigrp_if_down(ei);
464 eigrp_if_up(ei);
7f57883e
DS
465}
466
d62a17ae 467struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp,
468 struct interface *ifp,
469 struct in_addr address)
7f57883e 470{
d62a17ae 471 struct listnode *node;
472 struct eigrp_interface *ei;
7f57883e 473
d62a17ae 474 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
475 if (ifp && ei->ifp != ifp)
476 continue;
7f57883e 477
b245781a 478 if (IPV4_ADDR_SAME(&address, &ei->address.u.prefix4))
d62a17ae 479 return ei;
480 }
7f57883e 481
d62a17ae 482 return NULL;
7f57883e
DS
483}
484
485/**
486 * @fn eigrp_if_lookup_by_name
487 *
488 * @param[in] eigrp EIGRP process
489 * @param[in] if_name Name of the interface
490 *
491 * @return struct eigrp_interface *
492 *
493 * @par
494 * Function is used for lookup interface by name.
495 */
d62a17ae 496struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp,
497 const char *if_name)
7f57883e 498{
d62a17ae 499 struct eigrp_interface *ei;
500 struct listnode *node;
501
502 /* iterate over all eigrp interfaces */
503 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
504 /* compare int name with eigrp interface's name */
505 if (strcmp(ei->ifp->name, if_name) == 0) {
506 return ei;
507 }
508 }
509
510 return NULL;
7f57883e 511}