]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_interface.c
Merge pull request #7946 from ckishimo/ospfv3_json_2
[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 #include "eigrpd/eigrp_dump.h"
58 #include "eigrpd/eigrp_types.h"
59 #include "eigrpd/eigrp_metric.h"
60
61 struct eigrp_interface *eigrp_if_new(struct eigrp *eigrp, struct interface *ifp,
62 struct prefix *p)
63 {
64 struct eigrp_interface *ei = ifp->info;
65 int i;
66
67 if (ei)
68 return ei;
69
70 ei = XCALLOC(MTYPE_EIGRP_IF, sizeof(struct eigrp_interface));
71
72 /* Set zebra interface pointer. */
73 ei->ifp = ifp;
74 prefix_copy(&ei->address, p);
75
76 ifp->info = ei;
77 listnode_add(eigrp->eiflist, ei);
78
79 ei->type = EIGRP_IFTYPE_BROADCAST;
80
81 /* Initialize neighbor list. */
82 ei->nbrs = list_new();
83
84 ei->crypt_seqnum = time(NULL);
85
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 }
92
93 ei->eigrp = eigrp;
94
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;
103
104 ei->curr_bandwidth = ifp->bandwidth;
105 ei->curr_mtu = ifp->mtu;
106
107 return ei;
108 }
109
110 int eigrp_if_delete_hook(struct interface *ifp)
111 {
112 struct eigrp_interface *ei = ifp->info;
113 struct eigrp *eigrp;
114
115 if (!ei)
116 return 0;
117
118 list_delete(&ei->nbrs);
119
120 eigrp = ei->eigrp;
121 listnode_delete(eigrp->eiflist, ei);
122
123 eigrp_fifo_free(ei->obuf);
124
125 XFREE(MTYPE_EIGRP_IF_INFO, ifp->info);
126
127 return 0;
128 }
129
130 static int eigrp_ifp_create(struct interface *ifp)
131 {
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
141 return 0;
142 }
143
144 static int eigrp_ifp_up(struct interface *ifp)
145 {
146 struct eigrp_interface *ei = ifp->info;
147
148 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
149 zlog_debug("Zebra: Interface[%s] state change to up.",
150 ifp->name);
151
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);
180
181 return 0;
182 }
183
184 static int eigrp_ifp_down(struct interface *ifp)
185 {
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
193 return 0;
194 }
195
196 static int eigrp_ifp_destroy(struct interface *ifp)
197 {
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
211 return 0;
212 }
213
214 struct list *eigrp_iflist;
215
216 void eigrp_if_init(void)
217 {
218 if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up,
219 eigrp_ifp_down, eigrp_ifp_destroy);
220 /* Initialize Zebra interface data structure. */
221 // hook_register_prio(if_add, 0, eigrp_if_new);
222 hook_register_prio(if_del, 0, eigrp_if_delete_hook);
223 }
224
225
226 void eigrp_del_if_params(struct eigrp_if_params *eip)
227 {
228 if (eip->auth_keychain)
229 free(eip->auth_keychain);
230 }
231
232 /*
233 * Set the network byte order of the 3 bytes we send
234 * of the mtu of the link.
235 */
236 static 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
246 int eigrp_if_up(struct eigrp_interface *ei)
247 {
248 struct eigrp_prefix_descriptor *pe;
249 struct eigrp_route_descriptor *ne;
250 struct eigrp_metrics metric;
251 struct eigrp_interface *ei2;
252 struct listnode *node, *nnode;
253 struct eigrp *eigrp;
254
255 if (ei == NULL)
256 return 0;
257
258 eigrp = ei->eigrp;
259 eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu);
260
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*/
269 metric.bandwidth = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
270 metric.delay = eigrp_delay_to_scaled(ei->params.delay);
271 metric.load = ei->params.load;
272 metric.reliability = ei->params.reliability;
273 eigrp_mtu_convert(&metric, ei->ifp->mtu);
274 metric.hop_count = 0;
275 metric.flags = 0;
276 metric.tag = 0;
277
278 /*Add connected entry to topology table*/
279
280 ne = eigrp_route_descriptor_new();
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;
287 ne->flags = EIGRP_ROUTE_DESCRIPTOR_SUCCESSOR_FLAG;
288
289 struct prefix dest_addr;
290
291 dest_addr = ei->address;
292 apply_mask(&dest_addr);
293 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
294 &dest_addr);
295
296 if (pe == NULL) {
297 pe = eigrp_prefix_descriptor_new();
298 pe->serno = eigrp->serno;
299 pe->destination = (struct prefix *)prefix_ipv4_new();
300 prefix_copy(pe->destination, &dest_addr);
301 pe->af = AF_INET;
302 pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED;
303
304 ne->prefix = pe;
305 pe->reported_metric = metric;
306 pe->state = EIGRP_FSM_STATE_PASSIVE;
307 pe->fdistance = eigrp_calculate_metrics(eigrp, metric);
308 pe->req_action |= EIGRP_FSM_NEED_UPDATE;
309 eigrp_prefix_descriptor_add(eigrp->topology_table, pe);
310 listnode_add(eigrp->topology_changes_internalIPV4, pe);
311
312 eigrp_route_descriptor_add(eigrp, pe, ne);
313
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;
324 eigrp_route_descriptor_add(eigrp, pe, ne);
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 }
335
336 return 1;
337 }
338
339 int eigrp_if_down(struct eigrp_interface *ei)
340 {
341 struct listnode *node, *nnode;
342 struct eigrp_neighbor *nbr;
343
344 if (ei == NULL)
345 return 0;
346
347 /* Shutdown packet reception and sending */
348 THREAD_OFF(ei->t_hello);
349
350 eigrp_if_stream_unset(ei);
351
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 }
357
358 return 1;
359 }
360
361 void eigrp_if_stream_set(struct eigrp_interface *ei)
362 {
363 /* set output fifo queue. */
364 if (ei->obuf == NULL)
365 ei->obuf = eigrp_fifo_new();
366 }
367
368 void eigrp_if_stream_unset(struct eigrp_interface *ei)
369 {
370 struct eigrp *eigrp = ei->eigrp;
371
372 if (ei->on_write_q) {
373 listnode_delete(eigrp->oi_write_q, ei);
374 if (list_isempty(eigrp->oi_write_q))
375 thread_cancel(&(eigrp->t_write));
376 ei->on_write_q = 0;
377 }
378 }
379
380 bool 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
391 void eigrp_if_set_multicast(struct eigrp_interface *ei)
392 {
393 if (!eigrp_if_is_passive(ei)) {
394 /* The interface should belong to the EIGRP-all-routers group.
395 */
396 if (!ei->member_allrouters
397 && (eigrp_if_add_allspfrouters(ei->eigrp, &ei->address,
398 ei->ifp->ifindex)
399 >= 0))
400 /* Set the flag only if the system call to join
401 * succeeded. */
402 ei->member_allrouters = true;
403 } else {
404 /* The interface should NOT belong to the EIGRP-all-routers
405 * group. */
406 if (ei->member_allrouters) {
407 /* Only actually drop if this is the last reference */
408 eigrp_if_drop_allspfrouters(ei->eigrp, &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_allrouters = false;
416 }
417 }
418 }
419
420 uint8_t 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_descriptor *pe;
434 struct eigrp *eigrp = ei->eigrp;
435
436 if (source == INTERFACE_DOWN_BY_VTY) {
437 thread_cancel(&ei->t_hello);
438 eigrp_hello_send(ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN, NULL);
439 }
440
441 dest_addr = ei->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_descriptor_delete(eigrp, eigrp->topology_table,
447 pe);
448
449 eigrp_if_down(ei);
450
451 listnode_delete(ei->eigrp->eiflist, ei);
452 }
453
454 /* Simulate down/up on the interface. This is needed, for example, when
455 the MTU changes. */
456 void eigrp_if_reset(struct interface *ifp)
457 {
458 struct eigrp_interface *ei = ifp->info;
459
460 if (!ei)
461 return;
462
463 eigrp_if_down(ei);
464 eigrp_if_up(ei);
465 }
466
467 struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp,
468 struct interface *ifp,
469 struct in_addr address)
470 {
471 struct listnode *node;
472 struct eigrp_interface *ei;
473
474 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
475 if (ifp && ei->ifp != ifp)
476 continue;
477
478 if (IPV4_ADDR_SAME(&address, &ei->address.u.prefix4))
479 return ei;
480 }
481
482 return NULL;
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 */
496 struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp,
497 const char *if_name)
498 {
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;
511 }