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