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