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