]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_interface.c
eigrpd: Cleanup various SA Issues
[mirror_frr.git] / eigrpd / eigrp_interface.c
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 *
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
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"
56 #include "eigrpd/eigrp_fsm.h"
57
58 static void eigrp_delete_from_if(struct interface *, struct eigrp_interface *);
59
60 static void eigrp_add_to_if(struct interface *ifp, struct eigrp_interface *ei)
61 {
62 struct route_node *rn;
63 struct prefix p;
64
65 p = *ei->address;
66 p.prefixlen = IPV4_MAX_PREFIXLEN;
67
68 rn = route_node_get(IF_OIFS(ifp), &p);
69 /* rn->info should either be NULL or equal to this ei
70 * as route_node_get may return an existing node
71 */
72 assert(!rn->info || rn->info == ei);
73 rn->info = ei;
74 }
75
76 struct eigrp_interface *eigrp_if_new(struct eigrp *eigrp, struct interface *ifp,
77 struct prefix *p)
78 {
79 struct eigrp_interface *ei;
80 int i;
81
82 if ((ei = eigrp_if_table_lookup(ifp, p)) == NULL) {
83 ei = XCALLOC(MTYPE_EIGRP_IF, sizeof(struct eigrp_interface));
84 memset(ei, 0, sizeof(struct eigrp_interface));
85 } else
86 return ei;
87
88 /* Set zebra interface pointer. */
89 ei->ifp = ifp;
90 ei->address = p;
91
92 eigrp_add_to_if(ifp, ei);
93 listnode_add(eigrp->eiflist, ei);
94
95 ei->type = EIGRP_IFTYPE_BROADCAST;
96
97 /* Initialize neighbor list. */
98 ei->nbrs = list_new();
99
100 ei->crypt_seqnum = time(NULL);
101
102 /* Initialize lists */
103 for (i = 0; i < EIGRP_FILTER_MAX; i++) {
104 ei->list[i] = NULL;
105 ei->prefix[i] = NULL;
106 ei->routemap[i] = NULL;
107 }
108
109 return ei;
110 }
111
112 /* lookup ei for specified prefix/ifp */
113 struct eigrp_interface *eigrp_if_table_lookup(struct interface *ifp,
114 struct prefix *prefix)
115 {
116 struct prefix p;
117 struct route_node *rn;
118 struct eigrp_interface *rninfo = NULL;
119
120 p = *prefix;
121 p.prefixlen = IPV4_MAX_PREFIXLEN;
122
123 /* route_node_get implicitly locks */
124 if ((rn = route_node_lookup(IF_OIFS(ifp), &p))) {
125 rninfo = (struct eigrp_interface *)rn->info;
126 route_unlock_node(rn);
127 }
128
129 return rninfo;
130 }
131
132 int eigrp_if_delete_hook(struct interface *ifp)
133 {
134 struct route_node *rn;
135
136 route_table_finish(IF_OIFS(ifp));
137
138 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
139 if (rn->info)
140 eigrp_del_if_params(rn->info);
141 route_table_finish(IF_OIFS_PARAMS(ifp));
142
143 XFREE(MTYPE_EIGRP_IF_INFO, ifp->info);
144 ifp->info = NULL;
145
146 return 0;
147 }
148
149 struct list *eigrp_iflist;
150
151 void eigrp_if_init()
152 {
153 /* Initialize Zebra interface data structure. */
154 hook_register_prio(if_add, 0, eigrp_if_new_hook);
155 hook_register_prio(if_del, 0, eigrp_if_delete_hook);
156 }
157
158 int eigrp_if_new_hook(struct interface *ifp)
159 {
160 int rc = 0;
161
162 ifp->info = XCALLOC(MTYPE_EIGRP_IF_INFO, sizeof(struct eigrp_if_info));
163
164 IF_OIFS(ifp) = route_table_init();
165 IF_OIFS_PARAMS(ifp) = route_table_init();
166
167 IF_DEF_PARAMS(ifp) = eigrp_new_if_params();
168
169 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
170 IF_DEF_PARAMS(ifp)->v_hello = (u_int32_t)EIGRP_HELLO_INTERVAL_DEFAULT;
171
172 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
173 IF_DEF_PARAMS(ifp)->v_wait = (u_int16_t)EIGRP_HOLD_INTERVAL_DEFAULT;
174
175 SET_IF_PARAM(IF_DEF_PARAMS(ifp), bandwidth);
176 IF_DEF_PARAMS(ifp)->bandwidth = (u_int32_t)EIGRP_BANDWIDTH_DEFAULT;
177
178 SET_IF_PARAM(IF_DEF_PARAMS(ifp), delay);
179 IF_DEF_PARAMS(ifp)->delay = (u_int32_t)EIGRP_DELAY_DEFAULT;
180
181 SET_IF_PARAM(IF_DEF_PARAMS(ifp), reliability);
182 IF_DEF_PARAMS(ifp)->reliability = (u_char)EIGRP_RELIABILITY_DEFAULT;
183
184 SET_IF_PARAM(IF_DEF_PARAMS(ifp), load);
185 IF_DEF_PARAMS(ifp)->load = (u_char)EIGRP_LOAD_DEFAULT;
186
187 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
188 IF_DEF_PARAMS(ifp)->auth_type = EIGRP_AUTH_TYPE_NONE;
189
190 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_keychain);
191 IF_DEF_PARAMS(ifp)->auth_keychain = NULL;
192
193 return rc;
194 }
195
196 struct eigrp_if_params *eigrp_new_if_params(void)
197 {
198 struct eigrp_if_params *eip;
199
200 eip = XCALLOC(MTYPE_EIGRP_IF_PARAMS, sizeof(struct eigrp_if_params));
201 if (!eip)
202 return NULL;
203
204 UNSET_IF_PARAM(eip, passive_interface);
205 UNSET_IF_PARAM(eip, v_hello);
206 UNSET_IF_PARAM(eip, v_wait);
207 UNSET_IF_PARAM(eip, bandwidth);
208 UNSET_IF_PARAM(eip, delay);
209 UNSET_IF_PARAM(eip, reliability);
210 UNSET_IF_PARAM(eip, load);
211 UNSET_IF_PARAM(eip, auth_keychain);
212 UNSET_IF_PARAM(eip, auth_type);
213
214 return eip;
215 }
216
217 void eigrp_del_if_params(struct eigrp_if_params *eip)
218 {
219 if (eip->auth_keychain)
220 free(eip->auth_keychain);
221
222 XFREE(MTYPE_EIGRP_IF_PARAMS, eip);
223 }
224
225 struct eigrp_if_params *eigrp_lookup_if_params(struct interface *ifp,
226 struct in_addr addr)
227 {
228 struct prefix p;
229 struct route_node *rn;
230
231 p.family = AF_INET;
232 p.prefixlen = IPV4_MAX_PREFIXLEN;
233 p.u.prefix4 = addr;
234
235 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), &p);
236
237 if (rn) {
238 route_unlock_node(rn);
239 return rn->info;
240 }
241
242 return NULL;
243 }
244
245 int eigrp_if_up(struct eigrp_interface *ei)
246 {
247 struct eigrp_prefix_entry *pe;
248 struct eigrp_neighbor_entry *ne;
249 struct eigrp_metrics metric;
250 struct eigrp_interface *ei2;
251 struct listnode *node, *nnode;
252 struct eigrp *eigrp;
253
254 if (ei == NULL)
255 return 0;
256
257 eigrp = ei->eigrp;
258 eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu);
259
260 eigrp_if_stream_set(ei);
261
262 /* Set multicast memberships appropriately for new state. */
263 eigrp_if_set_multicast(ei);
264
265 thread_add_event(master, eigrp_hello_timer, ei, (1), NULL);
266
267 /*Prepare metrics*/
268 metric.bandwidth =
269 eigrp_bandwidth_to_scaled(EIGRP_IF_PARAM(ei, bandwidth));
270 metric.delay = eigrp_delay_to_scaled(EIGRP_IF_PARAM(ei, delay));
271 metric.load = EIGRP_IF_PARAM(ei, load);
272 metric.reliability = EIGRP_IF_PARAM(ei, reliability);
273 metric.mtu[0] = 0xDC;
274 metric.mtu[1] = 0x05;
275 metric.mtu[2] = 0x00;
276 metric.hop_count = 0;
277 metric.flags = 0;
278 metric.tag = 0;
279
280 /*Add connected entry to topology table*/
281
282 ne = eigrp_neighbor_entry_new();
283 ne->ei = ei;
284 ne->reported_metric = metric;
285 ne->total_metric = metric;
286 ne->distance = eigrp_calculate_metrics(eigrp, metric);
287 ne->reported_distance = 0;
288 ne->adv_router = eigrp->neighbor_self;
289 ne->flags = EIGRP_NEIGHBOR_ENTRY_SUCCESSOR_FLAG;
290
291 struct prefix dest_addr;
292
293 dest_addr.family = AF_INET;
294 dest_addr.u.prefix4 = ei->connected->address->u.prefix4;
295 dest_addr.prefixlen = ei->connected->address->prefixlen;
296 apply_mask(&dest_addr);
297 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
298 &dest_addr);
299
300 if (pe == NULL) {
301 pe = eigrp_prefix_entry_new();
302 pe->serno = eigrp->serno;
303 pe->destination = (struct prefix *)prefix_ipv4_new();
304 prefix_copy(pe->destination, &dest_addr);
305 pe->af = AF_INET;
306 pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED;
307
308 ne->prefix = pe;
309 pe->state = EIGRP_FSM_STATE_PASSIVE;
310 pe->fdistance = eigrp_calculate_metrics(eigrp, metric);
311 pe->req_action |= EIGRP_FSM_NEED_UPDATE;
312 eigrp_prefix_entry_add(eigrp->topology_table, pe);
313 listnode_add(eigrp->topology_changes_internalIPV4, pe);
314
315 eigrp_neighbor_entry_add(pe, ne);
316
317 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei2)) {
318 eigrp_update_send(ei2);
319 }
320
321 pe->req_action &= ~EIGRP_FSM_NEED_UPDATE;
322 listnode_delete(eigrp->topology_changes_internalIPV4, pe);
323 } else {
324 struct eigrp_fsm_action_message msg;
325
326 ne->prefix = pe;
327 eigrp_neighbor_entry_add(pe, ne);
328
329 msg.packet_type = EIGRP_OPC_UPDATE;
330 msg.eigrp = eigrp;
331 msg.data_type = EIGRP_CONNECTED;
332 msg.adv_router = NULL;
333 msg.entry = ne;
334 msg.prefix = pe;
335
336 eigrp_fsm_event(&msg);
337 }
338
339 return 1;
340 }
341
342 int eigrp_if_down(struct eigrp_interface *ei)
343 {
344 struct listnode *node, *nnode;
345 struct eigrp_neighbor *nbr;
346
347 if (ei == NULL)
348 return 0;
349
350 /* Shutdown packet reception and sending */
351 if (ei->t_hello)
352 THREAD_OFF(ei->t_hello);
353
354 eigrp_if_stream_unset(ei);
355
356 /*Set infinite metrics to routes learned by this interface and start
357 * query process*/
358 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
359 eigrp_nbr_delete(nbr);
360 }
361
362 return 1;
363 }
364
365 void eigrp_if_stream_set(struct eigrp_interface *ei)
366 {
367 /* set output fifo queue. */
368 if (ei->obuf == NULL)
369 ei->obuf = eigrp_fifo_new();
370 }
371
372 void eigrp_if_stream_unset(struct eigrp_interface *ei)
373 {
374 struct eigrp *eigrp = ei->eigrp;
375
376 if (ei->obuf) {
377 eigrp_fifo_free(ei->obuf);
378 ei->obuf = NULL;
379
380 if (ei->on_write_q) {
381 listnode_delete(eigrp->oi_write_q, ei);
382 if (list_isempty(eigrp->oi_write_q))
383 thread_cancel(eigrp->t_write);
384 ei->on_write_q = 0;
385 }
386 }
387 }
388
389 void eigrp_if_set_multicast(struct eigrp_interface *ei)
390 {
391 if ((EIGRP_IF_PASSIVE_STATUS(ei) == EIGRP_IF_ACTIVE)) {
392 /* The interface should belong to the EIGRP-all-routers group.
393 */
394 if (!EI_MEMBER_CHECK(ei, MEMBER_ALLROUTERS)
395 && (eigrp_if_add_allspfrouters(ei->eigrp, ei->address,
396 ei->ifp->ifindex)
397 >= 0))
398 /* Set the flag only if the system call to join
399 * succeeded. */
400 EI_MEMBER_JOINED(ei, MEMBER_ALLROUTERS);
401 } else {
402 /* The interface should NOT belong to the EIGRP-all-routers
403 * group. */
404 if (EI_MEMBER_CHECK(ei, MEMBER_ALLROUTERS)) {
405 /* Only actually drop if this is the last reference */
406 if (EI_MEMBER_COUNT(ei, MEMBER_ALLROUTERS) == 1)
407 eigrp_if_drop_allspfrouters(ei->eigrp,
408 ei->address,
409 ei->ifp->ifindex);
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. */
415 EI_MEMBER_LEFT(ei, MEMBER_ALLROUTERS);
416 }
417 }
418 }
419
420 u_char eigrp_default_iftype(struct interface *ifp)
421 {
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;
428 }
429
430 void eigrp_if_free(struct eigrp_interface *ei, int source)
431 {
432 struct prefix dest_addr;
433 struct eigrp_prefix_entry *pe;
434 struct eigrp *eigrp = eigrp_lookup();
435
436 if (source == INTERFACE_DOWN_BY_VTY) {
437 THREAD_OFF(ei->t_hello);
438 eigrp_hello_send(ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN, NULL);
439 }
440
441 dest_addr = *ei->connected->address;
442 apply_mask(&dest_addr);
443 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
444 &dest_addr);
445 if (pe)
446 eigrp_prefix_entry_delete(eigrp->topology_table, pe);
447
448 eigrp_if_down(ei);
449
450 list_delete(ei->nbrs);
451 eigrp_delete_from_if(ei->ifp, ei);
452 listnode_delete(ei->eigrp->eiflist, ei);
453
454 thread_cancel_event(master, ei);
455
456 memset(ei, 0, sizeof(*ei));
457 XFREE(MTYPE_EIGRP_IF, ei);
458 }
459
460 static void eigrp_delete_from_if(struct interface *ifp,
461 struct eigrp_interface *ei)
462 {
463 struct route_node *rn;
464 struct prefix p;
465
466 p = *ei->address;
467 p.prefixlen = IPV4_MAX_PREFIXLEN;
468
469 rn = route_node_lookup(IF_OIFS(ei->ifp), &p);
470 assert(rn);
471 assert(rn->info);
472 rn->info = NULL;
473 route_unlock_node(rn);
474 route_unlock_node(rn);
475 }
476
477 /* Simulate down/up on the interface. This is needed, for example, when
478 the MTU changes. */
479 void eigrp_if_reset(struct interface *ifp)
480 {
481 struct route_node *rn;
482
483 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
484 struct eigrp_interface *ei;
485
486 if ((ei = rn->info) == NULL)
487 continue;
488
489 eigrp_if_down(ei);
490 eigrp_if_up(ei);
491 }
492 }
493
494 struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp,
495 struct interface *ifp,
496 struct in_addr address)
497 {
498 struct listnode *node;
499 struct eigrp_interface *ei;
500
501 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
502 if (ifp && ei->ifp != ifp)
503 continue;
504
505 if (IPV4_ADDR_SAME(&address, &ei->address->u.prefix4))
506 return ei;
507 }
508
509 return NULL;
510 }
511
512 /**
513 * @fn eigrp_if_lookup_by_name
514 *
515 * @param[in] eigrp EIGRP process
516 * @param[in] if_name Name of the interface
517 *
518 * @return struct eigrp_interface *
519 *
520 * @par
521 * Function is used for lookup interface by name.
522 */
523 struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp,
524 const char *if_name)
525 {
526 struct eigrp_interface *ei;
527 struct listnode *node;
528
529 /* iterate over all eigrp interfaces */
530 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
531 /* compare int name with eigrp interface's name */
532 if (strcmp(ei->ifp->name, if_name) == 0) {
533 return ei;
534 }
535 }
536
537 return NULL;
538 }
539
540 /* determine receiving interface by ifp and source address */
541 struct eigrp_interface *eigrp_if_lookup_recv_if(struct eigrp *eigrp,
542 struct in_addr src,
543 struct interface *ifp)
544 {
545 struct route_node *rn;
546 struct prefix addr;
547 struct eigrp_interface *ei, *match;
548
549 addr.family = AF_INET;
550 addr.u.prefix4 = src;
551 addr.prefixlen = IPV4_MAX_BITLEN;
552
553 match = NULL;
554
555 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
556 ei = rn->info;
557
558 if (!ei) /* oi can be NULL for PtP aliases */
559 continue;
560
561 if (if_is_loopback(ei->ifp))
562 continue;
563
564 if (prefix_match(CONNECTED_PREFIX(ei->connected),
565 &addr)) {
566 if ((match == NULL) || (match->address->prefixlen
567 < ei->address->prefixlen))
568 match = ei;
569 }
570 }
571
572 return match;
573 }
574
575 u_int32_t eigrp_bandwidth_to_scaled(u_int32_t bandwidth)
576 {
577 uint64_t temp_bandwidth = (256ull * 10000000) / bandwidth;
578
579 temp_bandwidth = temp_bandwidth < EIGRP_MAX_METRIC ? temp_bandwidth
580 : EIGRP_MAX_METRIC;
581
582 return (u_int32_t)temp_bandwidth;
583 }
584
585 u_int32_t eigrp_scaled_to_bandwidth(u_int32_t scaled)
586 {
587 uint64_t temp_scaled = scaled * (256ull * 10000000);
588
589 temp_scaled =
590 temp_scaled < EIGRP_MAX_METRIC ? temp_scaled : EIGRP_MAX_METRIC;
591
592 return (u_int32_t)temp_scaled;
593 }
594
595 u_int32_t eigrp_delay_to_scaled(u_int32_t delay)
596 {
597 return delay * 256;
598 }
599
600 u_int32_t eigrp_scaled_to_delay(u_int32_t scaled)
601 {
602 return scaled / 256;
603 }