]> git.proxmox.com Git - mirror_frr.git/blob - eigrpd/eigrp_interface.c
*: Convert from ->interface_up to the interface callback
[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
59 struct eigrp_interface *eigrp_if_new(struct eigrp *eigrp, struct interface *ifp,
60 struct prefix *p)
61 {
62 struct eigrp_interface *ei = ifp->info;
63 int i;
64
65 if (ei)
66 return ei;
67
68 ei = XCALLOC(MTYPE_EIGRP_IF, sizeof(struct eigrp_interface));
69
70 /* Set zebra interface pointer. */
71 ei->ifp = ifp;
72 prefix_copy(&ei->address, p);
73
74 ifp->info = ei;
75 listnode_add(eigrp->eiflist, ei);
76
77 ei->type = EIGRP_IFTYPE_BROADCAST;
78
79 /* Initialize neighbor list. */
80 ei->nbrs = list_new();
81
82 ei->crypt_seqnum = time(NULL);
83
84 /* Initialize lists */
85 for (i = 0; i < EIGRP_FILTER_MAX; i++) {
86 ei->list[i] = NULL;
87 ei->prefix[i] = NULL;
88 ei->routemap[i] = NULL;
89 }
90
91 ei->eigrp = eigrp;
92
93 ei->params.v_hello = EIGRP_HELLO_INTERVAL_DEFAULT;
94 ei->params.v_wait = EIGRP_HOLD_INTERVAL_DEFAULT;
95 ei->params.bandwidth = EIGRP_BANDWIDTH_DEFAULT;
96 ei->params.delay = EIGRP_DELAY_DEFAULT;
97 ei->params.reliability = EIGRP_RELIABILITY_DEFAULT;
98 ei->params.load = EIGRP_LOAD_DEFAULT;
99 ei->params.auth_type = EIGRP_AUTH_TYPE_NONE;
100 ei->params.auth_keychain = NULL;
101
102 return ei;
103 }
104
105 int eigrp_if_delete_hook(struct interface *ifp)
106 {
107 struct eigrp_interface *ei = ifp->info;
108 struct eigrp *eigrp;
109
110 if (!ei)
111 return 0;
112
113 list_delete(&ei->nbrs);
114
115 eigrp = ei->eigrp;
116 listnode_delete(eigrp->eiflist, ei);
117
118 eigrp_fifo_free(ei->obuf);
119
120 XFREE(MTYPE_EIGRP_IF_INFO, ifp->info);
121 ifp->info = NULL;
122
123 return 0;
124 }
125
126 static int eigrp_ifp_create(struct interface *ifp)
127 {
128 struct eigrp_interface *ei = ifp->info;
129
130 if (!ei)
131 return 0;
132
133 ei->params.type = eigrp_default_iftype(ifp);
134
135 eigrp_if_update(ifp);
136
137 return 0;
138 }
139
140 static int eigrp_ifp_up(struct interface *ifp)
141 {
142 /* Interface is already up. */
143 if (if_is_operative(ifp)) {
144 /* Temporarily keep ifp values. */
145 struct interface if_tmp;
146 memcpy(&if_tmp, ifp, sizeof(struct interface));
147
148 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
149 zlog_debug("Zebra: Interface[%s] state update.",
150 ifp->name);
151
152 if (if_tmp.bandwidth != ifp->bandwidth) {
153 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
154 zlog_debug(
155 "Zebra: Interface[%s] bandwidth change %d -> %d.",
156 ifp->name, if_tmp.bandwidth,
157 ifp->bandwidth);
158
159 // eigrp_if_recalculate_output_cost (ifp);
160 }
161
162 if (if_tmp.mtu != ifp->mtu) {
163 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
164 zlog_debug(
165 "Zebra: Interface[%s] MTU change %u -> %u.",
166 ifp->name, if_tmp.mtu, ifp->mtu);
167
168 /* Must reset the interface (simulate down/up) when MTU
169 * changes. */
170 eigrp_if_reset(ifp);
171 }
172 return 0;
173 }
174
175 if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE))
176 zlog_debug("Zebra: Interface[%s] state change to up.",
177 ifp->name);
178
179 if (ifp->info)
180 eigrp_if_up(ifp->info);
181
182 return 0;
183 }
184
185 static int eigrp_ifp_down(struct interface *ifp)
186 {
187 return 0;
188 }
189
190 static int eigrp_ifp_destroy(struct interface *ifp)
191 {
192 return 0;
193 }
194
195 struct list *eigrp_iflist;
196
197 void eigrp_if_init(void)
198 {
199 if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up,
200 eigrp_ifp_down, eigrp_ifp_destroy);
201 /* Initialize Zebra interface data structure. */
202 // hook_register_prio(if_add, 0, eigrp_if_new);
203 hook_register_prio(if_del, 0, eigrp_if_delete_hook);
204 }
205
206
207 void eigrp_del_if_params(struct eigrp_if_params *eip)
208 {
209 if (eip->auth_keychain)
210 free(eip->auth_keychain);
211 }
212
213 int eigrp_if_up(struct eigrp_interface *ei)
214 {
215 struct eigrp_prefix_entry *pe;
216 struct eigrp_nexthop_entry *ne;
217 struct eigrp_metrics metric;
218 struct eigrp_interface *ei2;
219 struct listnode *node, *nnode;
220 struct eigrp *eigrp;
221
222 if (ei == NULL)
223 return 0;
224
225 eigrp = ei->eigrp;
226 eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu);
227
228 eigrp_if_stream_set(ei);
229
230 /* Set multicast memberships appropriately for new state. */
231 eigrp_if_set_multicast(ei);
232
233 thread_add_event(master, eigrp_hello_timer, ei, (1), NULL);
234
235 /*Prepare metrics*/
236 metric.bandwidth = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
237 metric.delay = eigrp_delay_to_scaled(ei->params.delay);
238 metric.load = ei->params.load;
239 metric.reliability = ei->params.reliability;
240 metric.mtu[0] = 0xDC;
241 metric.mtu[1] = 0x05;
242 metric.mtu[2] = 0x00;
243 metric.hop_count = 0;
244 metric.flags = 0;
245 metric.tag = 0;
246
247 /*Add connected entry to topology table*/
248
249 ne = eigrp_nexthop_entry_new();
250 ne->ei = ei;
251 ne->reported_metric = metric;
252 ne->total_metric = metric;
253 ne->distance = eigrp_calculate_metrics(eigrp, metric);
254 ne->reported_distance = 0;
255 ne->adv_router = eigrp->neighbor_self;
256 ne->flags = EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG;
257
258 struct prefix dest_addr;
259
260 dest_addr = ei->address;
261 apply_mask(&dest_addr);
262 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
263 &dest_addr);
264
265 if (pe == NULL) {
266 pe = eigrp_prefix_entry_new();
267 pe->serno = eigrp->serno;
268 pe->destination = (struct prefix *)prefix_ipv4_new();
269 prefix_copy(pe->destination, &dest_addr);
270 pe->af = AF_INET;
271 pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED;
272
273 ne->prefix = pe;
274 pe->reported_metric = metric;
275 pe->state = EIGRP_FSM_STATE_PASSIVE;
276 pe->fdistance = eigrp_calculate_metrics(eigrp, metric);
277 pe->req_action |= EIGRP_FSM_NEED_UPDATE;
278 eigrp_prefix_entry_add(eigrp->topology_table, pe);
279 listnode_add(eigrp->topology_changes_internalIPV4, pe);
280
281 eigrp_nexthop_entry_add(eigrp, pe, ne);
282
283 for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei2)) {
284 eigrp_update_send(ei2);
285 }
286
287 pe->req_action &= ~EIGRP_FSM_NEED_UPDATE;
288 listnode_delete(eigrp->topology_changes_internalIPV4, pe);
289 } else {
290 struct eigrp_fsm_action_message msg;
291
292 ne->prefix = pe;
293 eigrp_nexthop_entry_add(eigrp, pe, ne);
294
295 msg.packet_type = EIGRP_OPC_UPDATE;
296 msg.eigrp = eigrp;
297 msg.data_type = EIGRP_CONNECTED;
298 msg.adv_router = NULL;
299 msg.entry = ne;
300 msg.prefix = pe;
301
302 eigrp_fsm_event(&msg);
303 }
304
305 return 1;
306 }
307
308 int eigrp_if_down(struct eigrp_interface *ei)
309 {
310 struct listnode *node, *nnode;
311 struct eigrp_neighbor *nbr;
312
313 if (ei == NULL)
314 return 0;
315
316 /* Shutdown packet reception and sending */
317 if (ei->t_hello)
318 THREAD_OFF(ei->t_hello);
319
320 eigrp_if_stream_unset(ei);
321
322 /*Set infinite metrics to routes learned by this interface and start
323 * query process*/
324 for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) {
325 eigrp_nbr_delete(nbr);
326 }
327
328 return 1;
329 }
330
331 void eigrp_if_stream_set(struct eigrp_interface *ei)
332 {
333 /* set output fifo queue. */
334 if (ei->obuf == NULL)
335 ei->obuf = eigrp_fifo_new();
336 }
337
338 void eigrp_if_stream_unset(struct eigrp_interface *ei)
339 {
340 struct eigrp *eigrp = ei->eigrp;
341
342 if (ei->on_write_q) {
343 listnode_delete(eigrp->oi_write_q, ei);
344 if (list_isempty(eigrp->oi_write_q))
345 thread_cancel(eigrp->t_write);
346 ei->on_write_q = 0;
347 }
348 }
349
350 bool eigrp_if_is_passive(struct eigrp_interface *ei)
351 {
352 if (ei->params.passive_interface == EIGRP_IF_ACTIVE)
353 return false;
354
355 if (ei->eigrp->passive_interface_default == EIGRP_IF_ACTIVE)
356 return false;
357
358 return true;
359 }
360
361 void eigrp_if_set_multicast(struct eigrp_interface *ei)
362 {
363 if (!eigrp_if_is_passive(ei)) {
364 /* The interface should belong to the EIGRP-all-routers group.
365 */
366 if (!ei->member_allrouters
367 && (eigrp_if_add_allspfrouters(ei->eigrp, &ei->address,
368 ei->ifp->ifindex)
369 >= 0))
370 /* Set the flag only if the system call to join
371 * succeeded. */
372 ei->member_allrouters = true;
373 } else {
374 /* The interface should NOT belong to the EIGRP-all-routers
375 * group. */
376 if (ei->member_allrouters) {
377 /* Only actually drop if this is the last reference */
378 eigrp_if_drop_allspfrouters(ei->eigrp, &ei->address,
379 ei->ifp->ifindex);
380 /* Unset the flag regardless of whether the system call
381 to leave
382 the group succeeded, since it's much safer to assume
383 that
384 we are not a member. */
385 ei->member_allrouters = false;
386 }
387 }
388 }
389
390 uint8_t eigrp_default_iftype(struct interface *ifp)
391 {
392 if (if_is_pointopoint(ifp))
393 return EIGRP_IFTYPE_POINTOPOINT;
394 else if (if_is_loopback(ifp))
395 return EIGRP_IFTYPE_LOOPBACK;
396 else
397 return EIGRP_IFTYPE_BROADCAST;
398 }
399
400 void eigrp_if_free(struct eigrp_interface *ei, int source)
401 {
402 struct prefix dest_addr;
403 struct eigrp_prefix_entry *pe;
404 struct eigrp *eigrp = ei->eigrp;
405
406 if (source == INTERFACE_DOWN_BY_VTY) {
407 THREAD_OFF(ei->t_hello);
408 eigrp_hello_send(ei, EIGRP_HELLO_GRACEFUL_SHUTDOWN, NULL);
409 }
410
411 dest_addr = ei->address;
412 apply_mask(&dest_addr);
413 pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table,
414 &dest_addr);
415 if (pe)
416 eigrp_prefix_entry_delete(eigrp, eigrp->topology_table, pe);
417
418 eigrp_if_down(ei);
419
420 listnode_delete(ei->eigrp->eiflist, ei);
421 }
422
423 /* Simulate down/up on the interface. This is needed, for example, when
424 the MTU changes. */
425 void eigrp_if_reset(struct interface *ifp)
426 {
427 struct eigrp_interface *ei = ifp->info;
428
429 if (!ei)
430 return;
431
432 eigrp_if_down(ei);
433 eigrp_if_up(ei);
434 }
435
436 struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp,
437 struct interface *ifp,
438 struct in_addr address)
439 {
440 struct listnode *node;
441 struct eigrp_interface *ei;
442
443 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
444 if (ifp && ei->ifp != ifp)
445 continue;
446
447 if (IPV4_ADDR_SAME(&address, &ei->address.u.prefix4))
448 return ei;
449 }
450
451 return NULL;
452 }
453
454 /**
455 * @fn eigrp_if_lookup_by_name
456 *
457 * @param[in] eigrp EIGRP process
458 * @param[in] if_name Name of the interface
459 *
460 * @return struct eigrp_interface *
461 *
462 * @par
463 * Function is used for lookup interface by name.
464 */
465 struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp,
466 const char *if_name)
467 {
468 struct eigrp_interface *ei;
469 struct listnode *node;
470
471 /* iterate over all eigrp interfaces */
472 for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) {
473 /* compare int name with eigrp interface's name */
474 if (strcmp(ei->ifp->name, if_name) == 0) {
475 return ei;
476 }
477 }
478
479 return NULL;
480 }
481
482 uint32_t eigrp_bandwidth_to_scaled(uint32_t bandwidth)
483 {
484 uint64_t temp_bandwidth = (256ull * 10000000) / bandwidth;
485
486 temp_bandwidth = temp_bandwidth < EIGRP_MAX_METRIC ? temp_bandwidth
487 : EIGRP_MAX_METRIC;
488
489 return (uint32_t)temp_bandwidth;
490 }
491
492 uint32_t eigrp_scaled_to_bandwidth(uint32_t scaled)
493 {
494 uint64_t temp_scaled = scaled * (256ull * 10000000);
495
496 temp_scaled =
497 temp_scaled < EIGRP_MAX_METRIC ? temp_scaled : EIGRP_MAX_METRIC;
498
499 return (uint32_t)temp_scaled;
500 }
501
502 uint32_t eigrp_delay_to_scaled(uint32_t delay)
503 {
504 return delay * 256;
505 }
506
507 uint32_t eigrp_scaled_to_delay(uint32_t scaled)
508 {
509 return scaled / 256;
510 }