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