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