]>
Commit | Line | Data |
---|---|---|
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 | ei->curr_bandwidth = ifp->bandwidth; | |
103 | ei->curr_mtu = ifp->mtu; | |
104 | ||
105 | return ei; | |
106 | } | |
107 | ||
108 | int eigrp_if_delete_hook(struct interface *ifp) | |
109 | { | |
110 | struct eigrp_interface *ei = ifp->info; | |
111 | struct eigrp *eigrp; | |
112 | ||
113 | if (!ei) | |
114 | return 0; | |
115 | ||
116 | list_delete(&ei->nbrs); | |
117 | ||
118 | eigrp = ei->eigrp; | |
119 | listnode_delete(eigrp->eiflist, ei); | |
120 | ||
121 | eigrp_fifo_free(ei->obuf); | |
122 | ||
123 | XFREE(MTYPE_EIGRP_IF_INFO, ifp->info); | |
124 | ||
125 | return 0; | |
126 | } | |
127 | ||
128 | static int eigrp_ifp_create(struct interface *ifp) | |
129 | { | |
130 | struct eigrp_interface *ei = ifp->info; | |
131 | ||
132 | if (!ei) | |
133 | return 0; | |
134 | ||
135 | ei->params.type = eigrp_default_iftype(ifp); | |
136 | ||
137 | eigrp_if_update(ifp); | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
142 | static int eigrp_ifp_up(struct interface *ifp) | |
143 | { | |
144 | struct eigrp_interface *ei = ifp->info; | |
145 | ||
146 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) | |
147 | zlog_debug("Zebra: Interface[%s] state change to up.", | |
148 | ifp->name); | |
149 | ||
150 | if (!ei) | |
151 | return 0; | |
152 | ||
153 | if (ei->curr_bandwidth != ifp->bandwidth) { | |
154 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) | |
155 | zlog_debug( | |
156 | "Zebra: Interface[%s] bandwidth change %d -> %d.", | |
157 | ifp->name, ei->curr_bandwidth, | |
158 | ifp->bandwidth); | |
159 | ||
160 | ei->curr_bandwidth = ifp->bandwidth; | |
161 | // eigrp_if_recalculate_output_cost (ifp); | |
162 | } | |
163 | ||
164 | if (ei->curr_mtu != ifp->mtu) { | |
165 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) | |
166 | zlog_debug( | |
167 | "Zebra: Interface[%s] MTU change %u -> %u.", | |
168 | ifp->name, ei->curr_mtu, ifp->mtu); | |
169 | ||
170 | ei->curr_mtu = ifp->mtu; | |
171 | /* Must reset the interface (simulate down/up) when MTU | |
172 | * changes. */ | |
173 | eigrp_if_reset(ifp); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | eigrp_if_up(ifp->info); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | static int eigrp_ifp_down(struct interface *ifp) | |
183 | { | |
184 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) | |
185 | zlog_debug("Zebra: Interface[%s] state change to down.", | |
186 | ifp->name); | |
187 | ||
188 | if (ifp->info) | |
189 | eigrp_if_down(ifp->info); | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static int eigrp_ifp_destroy(struct interface *ifp) | |
195 | { | |
196 | if (if_is_up(ifp)) | |
197 | zlog_warn("Zebra: got delete of %s, but interface is still up", | |
198 | ifp->name); | |
199 | ||
200 | if (IS_DEBUG_EIGRP(zebra, ZEBRA_INTERFACE)) | |
201 | zlog_debug( | |
202 | "Zebra: interface delete %s index %d flags %llx metric %d mtu %d", | |
203 | ifp->name, ifp->ifindex, (unsigned long long)ifp->flags, | |
204 | ifp->metric, ifp->mtu); | |
205 | ||
206 | if (ifp->info) | |
207 | eigrp_if_free(ifp->info, INTERFACE_DOWN_BY_ZEBRA); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | struct list *eigrp_iflist; | |
213 | ||
214 | void eigrp_if_init(void) | |
215 | { | |
216 | if_zapi_callbacks(eigrp_ifp_create, eigrp_ifp_up, | |
217 | eigrp_ifp_down, eigrp_ifp_destroy); | |
218 | /* Initialize Zebra interface data structure. */ | |
219 | // hook_register_prio(if_add, 0, eigrp_if_new); | |
220 | hook_register_prio(if_del, 0, eigrp_if_delete_hook); | |
221 | } | |
222 | ||
223 | ||
224 | void eigrp_del_if_params(struct eigrp_if_params *eip) | |
225 | { | |
226 | if (eip->auth_keychain) | |
227 | free(eip->auth_keychain); | |
228 | } | |
229 | ||
230 | int eigrp_if_up(struct eigrp_interface *ei) | |
231 | { | |
232 | struct eigrp_prefix_entry *pe; | |
233 | struct eigrp_nexthop_entry *ne; | |
234 | struct eigrp_metrics metric; | |
235 | struct eigrp_interface *ei2; | |
236 | struct listnode *node, *nnode; | |
237 | struct eigrp *eigrp; | |
238 | ||
239 | if (ei == NULL) | |
240 | return 0; | |
241 | ||
242 | eigrp = ei->eigrp; | |
243 | eigrp_adjust_sndbuflen(eigrp, ei->ifp->mtu); | |
244 | ||
245 | eigrp_if_stream_set(ei); | |
246 | ||
247 | /* Set multicast memberships appropriately for new state. */ | |
248 | eigrp_if_set_multicast(ei); | |
249 | ||
250 | thread_add_event(master, eigrp_hello_timer, ei, (1), NULL); | |
251 | ||
252 | /*Prepare metrics*/ | |
253 | metric.bandwidth = eigrp_bandwidth_to_scaled(ei->params.bandwidth); | |
254 | metric.delay = eigrp_delay_to_scaled(ei->params.delay); | |
255 | metric.load = ei->params.load; | |
256 | metric.reliability = ei->params.reliability; | |
257 | metric.mtu[0] = 0xDC; | |
258 | metric.mtu[1] = 0x05; | |
259 | metric.mtu[2] = 0x00; | |
260 | metric.hop_count = 0; | |
261 | metric.flags = 0; | |
262 | metric.tag = 0; | |
263 | ||
264 | /*Add connected entry to topology table*/ | |
265 | ||
266 | ne = eigrp_nexthop_entry_new(); | |
267 | ne->ei = ei; | |
268 | ne->reported_metric = metric; | |
269 | ne->total_metric = metric; | |
270 | ne->distance = eigrp_calculate_metrics(eigrp, metric); | |
271 | ne->reported_distance = 0; | |
272 | ne->adv_router = eigrp->neighbor_self; | |
273 | ne->flags = EIGRP_NEXTHOP_ENTRY_SUCCESSOR_FLAG; | |
274 | ||
275 | struct prefix dest_addr; | |
276 | ||
277 | dest_addr = ei->address; | |
278 | apply_mask(&dest_addr); | |
279 | pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table, | |
280 | &dest_addr); | |
281 | ||
282 | if (pe == NULL) { | |
283 | pe = eigrp_prefix_entry_new(); | |
284 | pe->serno = eigrp->serno; | |
285 | pe->destination = (struct prefix *)prefix_ipv4_new(); | |
286 | prefix_copy(pe->destination, &dest_addr); | |
287 | pe->af = AF_INET; | |
288 | pe->nt = EIGRP_TOPOLOGY_TYPE_CONNECTED; | |
289 | ||
290 | ne->prefix = pe; | |
291 | pe->reported_metric = metric; | |
292 | pe->state = EIGRP_FSM_STATE_PASSIVE; | |
293 | pe->fdistance = eigrp_calculate_metrics(eigrp, metric); | |
294 | pe->req_action |= EIGRP_FSM_NEED_UPDATE; | |
295 | eigrp_prefix_entry_add(eigrp->topology_table, pe); | |
296 | listnode_add(eigrp->topology_changes_internalIPV4, pe); | |
297 | ||
298 | eigrp_nexthop_entry_add(eigrp, pe, ne); | |
299 | ||
300 | for (ALL_LIST_ELEMENTS(eigrp->eiflist, node, nnode, ei2)) { | |
301 | eigrp_update_send(ei2); | |
302 | } | |
303 | ||
304 | pe->req_action &= ~EIGRP_FSM_NEED_UPDATE; | |
305 | listnode_delete(eigrp->topology_changes_internalIPV4, pe); | |
306 | } else { | |
307 | struct eigrp_fsm_action_message msg; | |
308 | ||
309 | ne->prefix = pe; | |
310 | eigrp_nexthop_entry_add(eigrp, pe, ne); | |
311 | ||
312 | msg.packet_type = EIGRP_OPC_UPDATE; | |
313 | msg.eigrp = eigrp; | |
314 | msg.data_type = EIGRP_CONNECTED; | |
315 | msg.adv_router = NULL; | |
316 | msg.entry = ne; | |
317 | msg.prefix = pe; | |
318 | ||
319 | eigrp_fsm_event(&msg); | |
320 | } | |
321 | ||
322 | return 1; | |
323 | } | |
324 | ||
325 | int eigrp_if_down(struct eigrp_interface *ei) | |
326 | { | |
327 | struct listnode *node, *nnode; | |
328 | struct eigrp_neighbor *nbr; | |
329 | ||
330 | if (ei == NULL) | |
331 | return 0; | |
332 | ||
333 | /* Shutdown packet reception and sending */ | |
334 | if (ei->t_hello) | |
335 | THREAD_OFF(ei->t_hello); | |
336 | ||
337 | eigrp_if_stream_unset(ei); | |
338 | ||
339 | /*Set infinite metrics to routes learned by this interface and start | |
340 | * query process*/ | |
341 | for (ALL_LIST_ELEMENTS(ei->nbrs, node, nnode, nbr)) { | |
342 | eigrp_nbr_delete(nbr); | |
343 | } | |
344 | ||
345 | return 1; | |
346 | } | |
347 | ||
348 | void eigrp_if_stream_set(struct eigrp_interface *ei) | |
349 | { | |
350 | /* set output fifo queue. */ | |
351 | if (ei->obuf == NULL) | |
352 | ei->obuf = eigrp_fifo_new(); | |
353 | } | |
354 | ||
355 | void eigrp_if_stream_unset(struct eigrp_interface *ei) | |
356 | { | |
357 | struct eigrp *eigrp = ei->eigrp; | |
358 | ||
359 | if (ei->on_write_q) { | |
360 | listnode_delete(eigrp->oi_write_q, ei); | |
361 | if (list_isempty(eigrp->oi_write_q)) | |
362 | thread_cancel(eigrp->t_write); | |
363 | ei->on_write_q = 0; | |
364 | } | |
365 | } | |
366 | ||
367 | bool eigrp_if_is_passive(struct eigrp_interface *ei) | |
368 | { | |
369 | if (ei->params.passive_interface == EIGRP_IF_ACTIVE) | |
370 | return false; | |
371 | ||
372 | if (ei->eigrp->passive_interface_default == EIGRP_IF_ACTIVE) | |
373 | return false; | |
374 | ||
375 | return true; | |
376 | } | |
377 | ||
378 | void eigrp_if_set_multicast(struct eigrp_interface *ei) | |
379 | { | |
380 | if (!eigrp_if_is_passive(ei)) { | |
381 | /* The interface should belong to the EIGRP-all-routers group. | |
382 | */ | |
383 | if (!ei->member_allrouters | |
384 | && (eigrp_if_add_allspfrouters(ei->eigrp, &ei->address, | |
385 | ei->ifp->ifindex) | |
386 | >= 0)) | |
387 | /* Set the flag only if the system call to join | |
388 | * succeeded. */ | |
389 | ei->member_allrouters = true; | |
390 | } else { | |
391 | /* The interface should NOT belong to the EIGRP-all-routers | |
392 | * group. */ | |
393 | if (ei->member_allrouters) { | |
394 | /* Only actually drop if this is the last reference */ | |
395 | eigrp_if_drop_allspfrouters(ei->eigrp, &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_allrouters = false; | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
407 | uint8_t 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 dest_addr; | |
420 | struct eigrp_prefix_entry *pe; | |
421 | struct eigrp *eigrp = ei->eigrp; | |
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 = ei->address; | |
429 | apply_mask(&dest_addr); | |
430 | pe = eigrp_topology_table_lookup_ipv4(eigrp->topology_table, | |
431 | &dest_addr); | |
432 | if (pe) | |
433 | eigrp_prefix_entry_delete(eigrp, eigrp->topology_table, pe); | |
434 | ||
435 | eigrp_if_down(ei); | |
436 | ||
437 | listnode_delete(ei->eigrp->eiflist, ei); | |
438 | } | |
439 | ||
440 | /* Simulate down/up on the interface. This is needed, for example, when | |
441 | the MTU changes. */ | |
442 | void eigrp_if_reset(struct interface *ifp) | |
443 | { | |
444 | struct eigrp_interface *ei = ifp->info; | |
445 | ||
446 | if (!ei) | |
447 | return; | |
448 | ||
449 | eigrp_if_down(ei); | |
450 | eigrp_if_up(ei); | |
451 | } | |
452 | ||
453 | struct eigrp_interface *eigrp_if_lookup_by_local_addr(struct eigrp *eigrp, | |
454 | struct interface *ifp, | |
455 | struct in_addr address) | |
456 | { | |
457 | struct listnode *node; | |
458 | struct eigrp_interface *ei; | |
459 | ||
460 | for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) { | |
461 | if (ifp && ei->ifp != ifp) | |
462 | continue; | |
463 | ||
464 | if (IPV4_ADDR_SAME(&address, &ei->address.u.prefix4)) | |
465 | return ei; | |
466 | } | |
467 | ||
468 | return NULL; | |
469 | } | |
470 | ||
471 | /** | |
472 | * @fn eigrp_if_lookup_by_name | |
473 | * | |
474 | * @param[in] eigrp EIGRP process | |
475 | * @param[in] if_name Name of the interface | |
476 | * | |
477 | * @return struct eigrp_interface * | |
478 | * | |
479 | * @par | |
480 | * Function is used for lookup interface by name. | |
481 | */ | |
482 | struct eigrp_interface *eigrp_if_lookup_by_name(struct eigrp *eigrp, | |
483 | const char *if_name) | |
484 | { | |
485 | struct eigrp_interface *ei; | |
486 | struct listnode *node; | |
487 | ||
488 | /* iterate over all eigrp interfaces */ | |
489 | for (ALL_LIST_ELEMENTS_RO(eigrp->eiflist, node, ei)) { | |
490 | /* compare int name with eigrp interface's name */ | |
491 | if (strcmp(ei->ifp->name, if_name) == 0) { | |
492 | return ei; | |
493 | } | |
494 | } | |
495 | ||
496 | return NULL; | |
497 | } | |
498 | ||
499 | uint32_t eigrp_bandwidth_to_scaled(uint32_t bandwidth) | |
500 | { | |
501 | uint64_t temp_bandwidth = (256ull * 10000000) / bandwidth; | |
502 | ||
503 | temp_bandwidth = temp_bandwidth < EIGRP_MAX_METRIC ? temp_bandwidth | |
504 | : EIGRP_MAX_METRIC; | |
505 | ||
506 | return (uint32_t)temp_bandwidth; | |
507 | } | |
508 | ||
509 | uint32_t eigrp_scaled_to_bandwidth(uint32_t scaled) | |
510 | { | |
511 | uint64_t temp_scaled = scaled * (256ull * 10000000); | |
512 | ||
513 | temp_scaled = | |
514 | temp_scaled < EIGRP_MAX_METRIC ? temp_scaled : EIGRP_MAX_METRIC; | |
515 | ||
516 | return (uint32_t)temp_scaled; | |
517 | } | |
518 | ||
519 | uint32_t eigrp_delay_to_scaled(uint32_t delay) | |
520 | { | |
521 | return delay * 256; | |
522 | } | |
523 | ||
524 | uint32_t eigrp_scaled_to_delay(uint32_t scaled) | |
525 | { | |
526 | return scaled / 256; | |
527 | } |