blob: f4242b0b383ce98f0a010a1d85a9762ddb69cd0f [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * OSPF Interface functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "thread.h"
26#include "linklist.h"
27#include "prefix.h"
28#include "if.h"
29#include "table.h"
30#include "memory.h"
31#include "command.h"
32#include "stream.h"
33#include "log.h"
34
paul68980082003-03-25 05:07:42 +000035#include "ospfd/ospfd.h"
paul718e3742002-12-13 20:15:29 +000036#include "ospfd/ospf_spf.h"
37#include "ospfd/ospf_interface.h"
38#include "ospfd/ospf_ism.h"
39#include "ospfd/ospf_asbr.h"
40#include "ospfd/ospf_lsa.h"
41#include "ospfd/ospf_lsdb.h"
42#include "ospfd/ospf_neighbor.h"
43#include "ospfd/ospf_nsm.h"
44#include "ospfd/ospf_packet.h"
45#include "ospfd/ospf_abr.h"
paul718e3742002-12-13 20:15:29 +000046#include "ospfd/ospf_network.h"
47#include "ospfd/ospf_dump.h"
48#ifdef HAVE_SNMP
49#include "ospfd/ospf_snmp.h"
50#endif /* HAVE_SNMP */
51
David Lamparter6b0655a2014-06-04 06:53:35 +020052
paul718e3742002-12-13 20:15:29 +000053int
54ospf_if_get_output_cost (struct ospf_interface *oi)
55{
56 /* If all else fails, use default OSPF cost */
57 u_int32_t cost;
58 u_int32_t bw, refbw;
59
60 bw = oi->ifp->bandwidth ? oi->ifp->bandwidth : OSPF_DEFAULT_BANDWIDTH;
paul68980082003-03-25 05:07:42 +000061 refbw = oi->ospf->ref_bandwidth;
paul718e3742002-12-13 20:15:29 +000062
63 /* A specifed ip ospf cost overrides a calculated one. */
64 if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi->ifp), output_cost_cmd) ||
65 OSPF_IF_PARAM_CONFIGURED (oi->params, output_cost_cmd))
66 cost = OSPF_IF_PARAM (oi, output_cost_cmd);
67 /* See if a cost can be calculated from the zebra processes
68 interface bandwidth field. */
69 else
70 {
71 cost = (u_int32_t) ((double)refbw / (double)bw + (double)0.5);
72 if (cost < 1)
73 cost = 1;
74 else if (cost > 65535)
75 cost = 65535;
76 }
77
78 return cost;
79}
80
81void
82ospf_if_recalculate_output_cost (struct interface *ifp)
83{
84 u_int32_t newcost;
85 struct route_node *rn;
86
87 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
88 {
89 struct ospf_interface *oi;
90
91 if ( (oi = rn->info) == NULL)
92 continue;
93
94 newcost = ospf_if_get_output_cost (oi);
95
96 /* Is actual output cost changed? */
97 if (oi->output_cost != newcost)
98 {
99 oi->output_cost = newcost;
Paul Jakmac363d382010-01-24 22:42:13 +0000100 ospf_router_lsa_update_area (oi->area);
paul718e3742002-12-13 20:15:29 +0000101 }
102 }
103}
104
ajsa608bbf2005-03-29 17:03:49 +0000105/* Simulate down/up on the interface. This is needed, for example, when
106 the MTU changes. */
107void
108ospf_if_reset(struct interface *ifp)
109{
110 struct route_node *rn;
111
112 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
113 {
114 struct ospf_interface *oi;
115
116 if ( (oi = rn->info) == NULL)
117 continue;
118
119 ospf_if_down(oi);
120 ospf_if_up(oi);
121 }
122}
123
paul718e3742002-12-13 20:15:29 +0000124void
125ospf_if_reset_variables (struct ospf_interface *oi)
126{
127 /* Set default values. */
128 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
129
130 if (oi->vl_data)
131 oi->type = OSPF_IFTYPE_VIRTUALLINK;
132 else
133 /* preserve network-type */
134 if (oi->type != OSPF_IFTYPE_NBMA)
135 oi->type = OSPF_IFTYPE_BROADCAST;
136
137 oi->state = ISM_Down;
138
139 oi->crypt_seqnum = 0;
140
141 /* This must be short, (less than RxmtInterval)
142 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
143 held back for too long - MAG */
144 oi->v_ls_ack = 1;
145}
146
paul20916fb2003-10-15 21:14:20 +0000147/* lookup oi for specified prefix/ifp */
Joakim Tjernlundf0f63842009-07-27 12:42:29 +0200148struct ospf_interface *
paul20916fb2003-10-15 21:14:20 +0000149ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
150{
151 struct prefix p;
paulaffe1d92003-10-15 21:40:57 +0000152 struct route_node *rn;
paula3387a42005-05-18 23:29:57 +0000153 struct ospf_interface *rninfo = NULL;
paul20916fb2003-10-15 21:14:20 +0000154
155 p = *prefix;
paula3387a42005-05-18 23:29:57 +0000156 p.prefixlen = IPV4_MAX_PREFIXLEN;
157
paul20916fb2003-10-15 21:14:20 +0000158 /* route_node_get implicitely locks */
paulf9ad9372005-10-21 00:45:17 +0000159 if ((rn = route_node_lookup (IF_OIFS (ifp), &p)))
paula3387a42005-05-18 23:29:57 +0000160 {
161 rninfo = (struct ospf_interface *) rn->info;
162 route_unlock_node (rn);
163 }
164
paulb5f2c122003-11-10 23:56:29 +0000165 return rninfo;
paul20916fb2003-10-15 21:14:20 +0000166}
167
paul4dadc292005-05-06 21:37:42 +0000168static void
paul718e3742002-12-13 20:15:29 +0000169ospf_add_to_if (struct interface *ifp, struct ospf_interface *oi)
170{
171 struct route_node *rn;
172 struct prefix p;
173
174 p = *oi->address;
175 p.prefixlen = IPV4_MAX_PREFIXLEN;
176
177 rn = route_node_get (IF_OIFS (ifp), &p);
paul8c80cb72003-02-18 23:25:44 +0000178 /* rn->info should either be NULL or equal to this oi
179 * as route_node_get may return an existing node
180 */
paul20916fb2003-10-15 21:14:20 +0000181 assert (!rn->info || rn->info == oi);
paul718e3742002-12-13 20:15:29 +0000182 rn->info = oi;
183}
184
paul4dadc292005-05-06 21:37:42 +0000185static void
paul718e3742002-12-13 20:15:29 +0000186ospf_delete_from_if (struct interface *ifp, struct ospf_interface *oi)
187{
188 struct route_node *rn;
189 struct prefix p;
190
191 p = *oi->address;
192 p.prefixlen = IPV4_MAX_PREFIXLEN;
193
194 rn = route_node_lookup (IF_OIFS (oi->ifp), &p);
195 assert (rn);
196 assert (rn->info);
197 rn->info = NULL;
198 route_unlock_node (rn);
199 route_unlock_node (rn);
200}
201
202struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000203ospf_if_new (struct ospf *ospf, struct interface *ifp, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000204{
205 struct ospf_interface *oi;
206
paul20916fb2003-10-15 21:14:20 +0000207 if ((oi = ospf_if_table_lookup (ifp, p)) == NULL)
208 {
209 oi = XCALLOC (MTYPE_OSPF_IF, sizeof (struct ospf_interface));
210 memset (oi, 0, sizeof (struct ospf_interface));
211 }
212 else
213 return oi;
214
paul718e3742002-12-13 20:15:29 +0000215 /* Set zebra interface pointer. */
216 oi->ifp = ifp;
217 oi->address = p;
218
219 ospf_add_to_if (ifp, oi);
paul68980082003-03-25 05:07:42 +0000220 listnode_add (ospf->oiflist, oi);
paul718e3742002-12-13 20:15:29 +0000221
paul718e3742002-12-13 20:15:29 +0000222 /* Initialize neighbor list. */
223 oi->nbrs = route_table_init ();
224
225 /* Initialize static neighbor list. */
226 oi->nbr_nbma = list_new ();
227
228 /* Initialize Link State Acknowledgment list. */
229 oi->ls_ack = list_new ();
230 oi->ls_ack_direct.ls_ack = list_new ();
231
232 /* Set default values. */
233 ospf_if_reset_variables (oi);
234
235 /* Add pseudo neighbor. */
236 oi->nbr_self = ospf_nbr_new (oi);
paul718e3742002-12-13 20:15:29 +0000237
238 oi->ls_upd_queue = route_table_init ();
239 oi->t_ls_upd_event = NULL;
240 oi->t_ls_ack_direct = NULL;
241
paul68980082003-03-25 05:07:42 +0000242 oi->crypt_seqnum = time (NULL);
243
paul718e3742002-12-13 20:15:29 +0000244 ospf_opaque_type9_lsa_init (oi);
paul718e3742002-12-13 20:15:29 +0000245
paul68980082003-03-25 05:07:42 +0000246 oi->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000247
248 return oi;
249}
250
251/* Restore an interface to its pre UP state
252 Used from ism_interface_down only */
253void
254ospf_if_cleanup (struct ospf_interface *oi)
255{
256 struct route_node *rn;
paul1eb8ef22005-04-07 07:30:20 +0000257 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000258 struct ospf_neighbor *nbr;
paul1eb8ef22005-04-07 07:30:20 +0000259 struct ospf_nbr_nbma *nbr_nbma;
260 struct ospf_lsa *lsa;
paul718e3742002-12-13 20:15:29 +0000261
paul1a643f82006-01-11 01:08:19 +0000262 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
paul718e3742002-12-13 20:15:29 +0000263 /* delete all static neighbors attached to this interface */
paul1eb8ef22005-04-07 07:30:20 +0000264 for (ALL_LIST_ELEMENTS (oi->nbr_nbma, node, nnode, nbr_nbma))
paul718e3742002-12-13 20:15:29 +0000265 {
paul718e3742002-12-13 20:15:29 +0000266 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
267
268 if (nbr_nbma->nbr)
269 {
270 nbr_nbma->nbr->nbr_nbma = NULL;
271 nbr_nbma->nbr = NULL;
272 }
273
274 nbr_nbma->oi = NULL;
275
276 listnode_delete (oi->nbr_nbma, nbr_nbma);
277 }
278
279 /* send Neighbor event KillNbr to all associated neighbors. */
280 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
281 if ((nbr = rn->info) != NULL)
282 if (nbr != oi->nbr_self)
283 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr);
284
285 /* Cleanup Link State Acknowlegdment list. */
paul1eb8ef22005-04-07 07:30:20 +0000286 for (ALL_LIST_ELEMENTS (oi->ls_ack, node, nnode, lsa))
Paul Jakma1fe6ed32006-07-26 09:37:26 +0000287 ospf_lsa_unlock (&lsa); /* oi->ls_ack */
paul718e3742002-12-13 20:15:29 +0000288 list_delete_all_node (oi->ls_ack);
289
290 oi->crypt_seqnum = 0;
291
292 /* Empty link state update queue */
293 ospf_ls_upd_queue_empty (oi);
294
paul1a643f82006-01-11 01:08:19 +0000295 /* Reset pseudo neighbor. */
Paul Jakmac920e512015-09-08 15:31:45 +0100296 ospf_nbr_self_reset (oi);
paul718e3742002-12-13 20:15:29 +0000297}
298
299void
300ospf_if_free (struct ospf_interface *oi)
301{
302 ospf_if_down (oi);
303
304 assert (oi->state == ISM_Down);
305
paul718e3742002-12-13 20:15:29 +0000306 ospf_opaque_type9_lsa_term (oi);
paul718e3742002-12-13 20:15:29 +0000307
308 /* Free Pseudo Neighbour */
309 ospf_nbr_delete (oi->nbr_self);
310
311 route_table_finish (oi->nbrs);
312 route_table_finish (oi->ls_upd_queue);
313
314 /* Free any lists that should be freed */
315 list_free (oi->nbr_nbma);
316
317 list_free (oi->ls_ack);
318 list_free (oi->ls_ack_direct.ls_ack);
319
320 ospf_delete_from_if (oi->ifp, oi);
321
paul68980082003-03-25 05:07:42 +0000322 listnode_delete (oi->ospf->oiflist, oi);
paul718e3742002-12-13 20:15:29 +0000323 listnode_delete (oi->area->oiflist, oi);
324
Paul Jakmacfd670f2010-04-15 11:39:05 +0100325 thread_cancel_event (master, oi);
326
paul718e3742002-12-13 20:15:29 +0000327 memset (oi, 0, sizeof (*oi));
328 XFREE (MTYPE_OSPF_IF, oi);
329}
330
David Lamparter6b0655a2014-06-04 06:53:35 +0200331
paul718e3742002-12-13 20:15:29 +0000332/*
333* check if interface with given address is configured and
hasso3fb9cd62004-10-19 19:44:43 +0000334* return it if yes. special treatment for PtP networks.
paul718e3742002-12-13 20:15:29 +0000335*/
336struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000337ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
paul718e3742002-12-13 20:15:29 +0000338{
paul1eb8ef22005-04-07 07:30:20 +0000339 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000340 struct ospf_interface *oi;
hasso3fb9cd62004-10-19 19:44:43 +0000341 struct prefix_ipv4 addr;
342
343 addr.family = AF_INET;
344 addr.prefix = *address;
345 addr.prefixlen = IPV4_MAX_PREFIXLEN;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000346
paul1eb8ef22005-04-07 07:30:20 +0000347 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
348 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul718e3742002-12-13 20:15:29 +0000349 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000350 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
hasso3fb9cd62004-10-19 19:44:43 +0000351 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000352 /* special leniency: match if addr is anywhere on peer subnet */
353 if (prefix_match(CONNECTED_PREFIX(oi->connected),
354 (struct prefix *)&addr))
355 return oi;
hasso3fb9cd62004-10-19 19:44:43 +0000356 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000357 else
hasso3fb9cd62004-10-19 19:44:43 +0000358 {
359 if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
360 return oi;
361 }
paul718e3742002-12-13 20:15:29 +0000362 }
paul718e3742002-12-13 20:15:29 +0000363 return NULL;
364}
365
366int
367ospf_if_is_up (struct ospf_interface *oi)
368{
369 return if_is_up (oi->ifp);
370}
371
372struct ospf_interface *
hasso2db3d052004-02-11 21:52:13 +0000373ospf_if_exists (struct ospf_interface *oic)
374{
hasso52dc7ee2004-09-23 19:18:23 +0000375 struct listnode *node;
hasso2db3d052004-02-11 21:52:13 +0000376 struct ospf *ospf;
377 struct ospf_interface *oi;
378
Paul Jakmae43be0e2006-05-12 23:00:06 +0000379 if ((ospf = ospf_lookup ()) == NULL)
380 return NULL;
hasso2db3d052004-02-11 21:52:13 +0000381
paul1eb8ef22005-04-07 07:30:20 +0000382 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
383 if (oi == oic)
hasso2db3d052004-02-11 21:52:13 +0000384 return oi;
paul1eb8ef22005-04-07 07:30:20 +0000385
hasso2db3d052004-02-11 21:52:13 +0000386 return NULL;
387}
388
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200389/* Lookup OSPF interface by router LSA posistion */
390struct ospf_interface *
391ospf_if_lookup_by_lsa_pos (struct ospf_area *area, int lsa_pos)
392{
393 struct listnode *node;
394 struct ospf_interface *oi;
395
396 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
397 {
398 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
399 return oi;
400 }
401 return NULL;
402}
403
hasso2db3d052004-02-11 21:52:13 +0000404struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000405ospf_if_lookup_by_local_addr (struct ospf *ospf,
406 struct interface *ifp, struct in_addr address)
paul718e3742002-12-13 20:15:29 +0000407{
hasso52dc7ee2004-09-23 19:18:23 +0000408 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000409 struct ospf_interface *oi;
410
paul1eb8ef22005-04-07 07:30:20 +0000411 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
412 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul718e3742002-12-13 20:15:29 +0000413 {
414 if (ifp && oi->ifp != ifp)
415 continue;
416
417 if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
418 return oi;
419 }
420
421 return NULL;
422}
423
424struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000425ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
paul718e3742002-12-13 20:15:29 +0000426{
hasso52dc7ee2004-09-23 19:18:23 +0000427 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000428 struct ospf_interface *oi;
paul718e3742002-12-13 20:15:29 +0000429
430 /* Check each Interface. */
paul1eb8ef22005-04-07 07:30:20 +0000431 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
paul68980082003-03-25 05:07:42 +0000432 {
paul1eb8ef22005-04-07 07:30:20 +0000433 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul68980082003-03-25 05:07:42 +0000434 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000435 struct prefix ptmp;
436
437 prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
paul68980082003-03-25 05:07:42 +0000438 apply_mask (&ptmp);
439 if (prefix_same (&ptmp, (struct prefix *) p))
440 return oi;
441 }
442 }
paul718e3742002-12-13 20:15:29 +0000443 return NULL;
444}
445
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200446/* determine receiving interface by ifp and source address */
paul718e3742002-12-13 20:15:29 +0000447struct ospf_interface *
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200448ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src,
449 struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000450{
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200451 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000452 struct prefix_ipv4 addr;
453 struct ospf_interface *oi, *match;
454
455 addr.family = AF_INET;
456 addr.prefix = src;
457 addr.prefixlen = IPV4_MAX_BITLEN;
458
459 match = NULL;
460
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200461 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000462 {
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200463 oi = rn->info;
464
465 if (!oi) /* oi can be NULL for PtP aliases */
466 continue;
467
paul718e3742002-12-13 20:15:29 +0000468 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
469 continue;
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200470
Paul Jakma1a8ee0e2006-03-30 14:20:00 +0000471 if (if_is_loopback (oi->ifp))
472 continue;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000473
474 if (prefix_match (CONNECTED_PREFIX(oi->connected),
475 (struct prefix *) &addr))
paul718e3742002-12-13 20:15:29 +0000476 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000477 if ( (match == NULL) ||
478 (match->address->prefixlen < oi->address->prefixlen)
479 )
480 match = oi;
paul718e3742002-12-13 20:15:29 +0000481 }
482 }
483
484 return match;
485}
David Lamparter6b0655a2014-06-04 06:53:35 +0200486
paul718e3742002-12-13 20:15:29 +0000487void
488ospf_if_stream_set (struct ospf_interface *oi)
489{
490 /* set output fifo queue. */
491 if (oi->obuf == NULL)
492 oi->obuf = ospf_fifo_new ();
493}
494
495void
496ospf_if_stream_unset (struct ospf_interface *oi)
497{
paul68980082003-03-25 05:07:42 +0000498 struct ospf *ospf = oi->ospf;
499
paul718e3742002-12-13 20:15:29 +0000500 if (oi->obuf)
501 {
502 ospf_fifo_free (oi->obuf);
503 oi->obuf = NULL;
504
505 if (oi->on_write_q)
506 {
paul68980082003-03-25 05:07:42 +0000507 listnode_delete (ospf->oi_write_q, oi);
508 if (list_isempty(ospf->oi_write_q))
509 OSPF_TIMER_OFF (ospf->t_write);
paul718e3742002-12-13 20:15:29 +0000510 oi->on_write_q = 0;
511 }
512 }
513}
paul68980082003-03-25 05:07:42 +0000514
David Lamparter6b0655a2014-06-04 06:53:35 +0200515
paul4dadc292005-05-06 21:37:42 +0000516static struct ospf_if_params *
517ospf_new_if_params (void)
paul718e3742002-12-13 20:15:29 +0000518{
519 struct ospf_if_params *oip;
520
Stephen Hemminger393deb92008-08-18 14:13:29 -0700521 oip = XCALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
paul718e3742002-12-13 20:15:29 +0000522
523 if (!oip)
524 return NULL;
525
paul718e3742002-12-13 20:15:29 +0000526 UNSET_IF_PARAM (oip, output_cost_cmd);
527 UNSET_IF_PARAM (oip, transmit_delay);
528 UNSET_IF_PARAM (oip, retransmit_interval);
529 UNSET_IF_PARAM (oip, passive_interface);
530 UNSET_IF_PARAM (oip, v_hello);
paulf9ad9372005-10-21 00:45:17 +0000531 UNSET_IF_PARAM (oip, fast_hello);
paul718e3742002-12-13 20:15:29 +0000532 UNSET_IF_PARAM (oip, v_wait);
533 UNSET_IF_PARAM (oip, priority);
534 UNSET_IF_PARAM (oip, type);
535 UNSET_IF_PARAM (oip, auth_simple);
536 UNSET_IF_PARAM (oip, auth_crypt);
537 UNSET_IF_PARAM (oip, auth_type);
paulec1ca632003-06-04 02:23:15 +0000538
539 oip->auth_crypt = list_new ();
paul718e3742002-12-13 20:15:29 +0000540
Paul Jakma7eb5b472009-10-13 16:13:13 +0100541 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
542
paul718e3742002-12-13 20:15:29 +0000543 return oip;
544}
545
546void
547ospf_del_if_params (struct ospf_if_params *oip)
548{
549 list_delete (oip->auth_crypt);
550 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
551}
552
553void
554ospf_free_if_params (struct interface *ifp, struct in_addr addr)
555{
556 struct ospf_if_params *oip;
557 struct prefix_ipv4 p;
558 struct route_node *rn;
gdt630e4802004-08-31 17:28:41 +0000559
560 p.family = AF_INET;
paul718e3742002-12-13 20:15:29 +0000561 p.prefixlen = IPV4_MAX_PREFIXLEN;
562 p.prefix = addr;
563 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
564 if (!rn || !rn->info)
565 return;
566
567 oip = rn->info;
568 route_unlock_node (rn);
569
570 if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
571 !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
572 !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
573 !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
574 !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
paulf9ad9372005-10-21 00:45:17 +0000575 !OSPF_IF_PARAM_CONFIGURED (oip, fast_hello) &&
paul718e3742002-12-13 20:15:29 +0000576 !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
577 !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
578 !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
579 !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
580 !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
Paul Jakma7eb5b472009-10-13 16:13:13 +0100581 listcount (oip->auth_crypt) == 0 &&
582 ntohl (oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER)
paul718e3742002-12-13 20:15:29 +0000583 {
584 ospf_del_if_params (oip);
585 rn->info = NULL;
586 route_unlock_node (rn);
587 }
588}
589
590struct ospf_if_params *
591ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
592{
593 struct prefix_ipv4 p;
594 struct route_node *rn;
595
gdt630e4802004-08-31 17:28:41 +0000596 p.family = AF_INET;
paul718e3742002-12-13 20:15:29 +0000597 p.prefixlen = IPV4_MAX_PREFIXLEN;
598 p.prefix = addr;
599
600 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
601
602 if (rn)
603 {
604 route_unlock_node (rn);
605 return rn->info;
606 }
607
608 return NULL;
609}
610
611struct ospf_if_params *
612ospf_get_if_params (struct interface *ifp, struct in_addr addr)
613{
614 struct prefix_ipv4 p;
615 struct route_node *rn;
616
617 p.family = AF_INET;
618 p.prefixlen = IPV4_MAX_PREFIXLEN;
619 p.prefix = addr;
620
621 rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
622
623 if (rn->info == NULL)
624 rn->info = ospf_new_if_params ();
625 else
626 route_unlock_node (rn);
627
628 return rn->info;
629}
630
631void
632ospf_if_update_params (struct interface *ifp, struct in_addr addr)
633{
634 struct route_node *rn;
635 struct ospf_interface *oi;
636
637 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
638 {
639 if ((oi = rn->info) == NULL)
640 continue;
641
642 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
643 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
644 }
645}
646
647int
648ospf_if_new_hook (struct interface *ifp)
649{
650 int rc = 0;
651
Stephen Hemminger393deb92008-08-18 14:13:29 -0700652 ifp->info = XCALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
paul718e3742002-12-13 20:15:29 +0000653
654 IF_OIFS (ifp) = route_table_init ();
655 IF_OIFS_PARAMS (ifp) = route_table_init ();
656
657 IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
658
659 SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
660 IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
661
662 SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
663 IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
664
665 SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
666 IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
667
vincentba682532005-09-29 13:52:57 +0000668 IF_DEF_PARAMS (ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
669
paul718e3742002-12-13 20:15:29 +0000670 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
671 IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
672
paulf9ad9372005-10-21 00:45:17 +0000673 SET_IF_PARAM (IF_DEF_PARAMS (ifp), fast_hello);
674 IF_DEF_PARAMS (ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
675
paul718e3742002-12-13 20:15:29 +0000676 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
677 IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
678
679 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
680 memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
681
paul718e3742002-12-13 20:15:29 +0000682 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
683 IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
684
paul718e3742002-12-13 20:15:29 +0000685 rc = ospf_opaque_new_if (ifp);
paul718e3742002-12-13 20:15:29 +0000686 return rc;
687}
688
paul4dadc292005-05-06 21:37:42 +0000689static int
paul718e3742002-12-13 20:15:29 +0000690ospf_if_delete_hook (struct interface *ifp)
691{
692 int rc = 0;
paul940b01a2004-02-17 20:07:30 +0000693 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000694 rc = ospf_opaque_del_if (ifp);
paul940b01a2004-02-17 20:07:30 +0000695
paul718e3742002-12-13 20:15:29 +0000696 route_table_finish (IF_OIFS (ifp));
paul940b01a2004-02-17 20:07:30 +0000697
698 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
699 if (rn->info)
700 ospf_del_if_params (rn->info);
paul718e3742002-12-13 20:15:29 +0000701 route_table_finish (IF_OIFS_PARAMS (ifp));
paul940b01a2004-02-17 20:07:30 +0000702
paulcfc959b2003-06-04 02:28:45 +0000703 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
paul718e3742002-12-13 20:15:29 +0000704 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
705 ifp->info = NULL;
706
707 return rc;
708}
709
710int
711ospf_if_is_enable (struct ospf_interface *oi)
712{
713 if (!if_is_loopback (oi->ifp))
714 if (if_is_up (oi->ifp))
715 return 1;
716
717 return 0;
718}
719
ajsba6454e2005-02-08 15:37:30 +0000720void
721ospf_if_set_multicast(struct ospf_interface *oi)
722{
723 if ((oi->state > ISM_Loopback) &&
724 (oi->type != OSPF_IFTYPE_LOOPBACK) &&
725 (oi->type != OSPF_IFTYPE_VIRTUALLINK) &&
Andrew J. Schorre8a56f02007-04-21 20:46:31 +0000726 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
ajsba6454e2005-02-08 15:37:30 +0000727 {
728 /* The interface should belong to the OSPF-all-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000729 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) &&
ajsba6454e2005-02-08 15:37:30 +0000730 (ospf_if_add_allspfrouters(oi->ospf, oi->address,
731 oi->ifp->ifindex) >= 0))
Paul Jakma429ac782006-06-15 18:40:49 +0000732 /* Set the flag only if the system call to join succeeded. */
733 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000734 }
735 else
736 {
737 /* The interface should NOT belong to the OSPF-all-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000738 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS))
ajsba6454e2005-02-08 15:37:30 +0000739 {
Paul Jakma429ac782006-06-15 18:40:49 +0000740 /* Only actually drop if this is the last reference */
741 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
742 ospf_if_drop_allspfrouters (oi->ospf, oi->address,
743 oi->ifp->ifindex);
ajsba6454e2005-02-08 15:37:30 +0000744 /* Unset the flag regardless of whether the system call to leave
745 the group succeeded, since it's much safer to assume that
746 we are not a member. */
Paul Jakma429ac782006-06-15 18:40:49 +0000747 OI_MEMBER_LEFT(oi,MEMBER_ALLROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000748 }
749 }
750
751 if (((oi->type == OSPF_IFTYPE_BROADCAST) ||
752 (oi->type == OSPF_IFTYPE_POINTOPOINT)) &&
753 ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) &&
Andrew J. Schorre8a56f02007-04-21 20:46:31 +0000754 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
ajsba6454e2005-02-08 15:37:30 +0000755 {
756 /* The interface should belong to the OSPF-designated-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000757 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) &&
ajsba6454e2005-02-08 15:37:30 +0000758 (ospf_if_add_alldrouters(oi->ospf, oi->address,
759 oi->ifp->ifindex) >= 0))
760 /* Set the flag only if the system call to join succeeded. */
Paul Jakma429ac782006-06-15 18:40:49 +0000761 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000762 }
763 else
764 {
765 /* The interface should NOT belong to the OSPF-designated-routers group */
Paul Jakma429ac782006-06-15 18:40:49 +0000766 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS))
ajsba6454e2005-02-08 15:37:30 +0000767 {
Paul Jakma429ac782006-06-15 18:40:49 +0000768 /* drop only if last reference */
769 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
770 ospf_if_drop_alldrouters(oi->ospf, oi->address, oi->ifp->ifindex);
771
ajsba6454e2005-02-08 15:37:30 +0000772 /* Unset the flag regardless of whether the system call to leave
773 the group succeeded, since it's much safer to assume that
774 we are not a member. */
Paul Jakma429ac782006-06-15 18:40:49 +0000775 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000776 }
777 }
778}
779
paul718e3742002-12-13 20:15:29 +0000780int
781ospf_if_up (struct ospf_interface *oi)
782{
783 if (oi == NULL)
784 return 0;
785
786 if (oi->type == OSPF_IFTYPE_LOOPBACK)
787 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
788 else
789 {
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000790 struct ospf *ospf = ospf_lookup ();
791 if (ospf != NULL)
792 ospf_adjust_sndbuflen (ospf, oi->ifp->mtu);
793 else
Denis Ovsienkofb31c0f2007-09-18 09:03:13 +0000794 zlog_warn ("%s: ospf_lookup() returned NULL", __func__);
paul718e3742002-12-13 20:15:29 +0000795 ospf_if_stream_set (oi);
796 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
797 }
798
799 return 1;
800}
801
802int
803ospf_if_down (struct ospf_interface *oi)
804{
805 if (oi == NULL)
806 return 0;
807
808 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200809 /* delete position in router LSA */
810 oi->lsa_pos_beg = 0;
811 oi->lsa_pos_end = 0;
paul718e3742002-12-13 20:15:29 +0000812 /* Shutdown packet reception and sending */
813 ospf_if_stream_unset (oi);
paul718e3742002-12-13 20:15:29 +0000814
815 return 1;
816}
817
David Lamparter6b0655a2014-06-04 06:53:35 +0200818
paul718e3742002-12-13 20:15:29 +0000819/* Virtual Link related functions. */
820
821struct ospf_vl_data *
822ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
823{
824 struct ospf_vl_data *vl_data;
825
Stephen Hemminger393deb92008-08-18 14:13:29 -0700826 vl_data = XCALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
paul718e3742002-12-13 20:15:29 +0000827
828 vl_data->vl_peer.s_addr = vl_peer.s_addr;
829 vl_data->vl_area_id = area->area_id;
830 vl_data->format = area->format;
831
832 return vl_data;
833}
834
835void
836ospf_vl_data_free (struct ospf_vl_data *vl_data)
837{
838 XFREE (MTYPE_OSPF_VL_DATA, vl_data);
839}
840
841u_int vlink_count = 0;
842
843struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000844ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000845{
846 struct ospf_interface * voi;
847 struct interface * vi;
848 char ifname[INTERFACE_NAMSIZ + 1];
849 struct ospf_area *area;
850 struct in_addr area_id;
851 struct connected *co;
852 struct prefix_ipv4 *p;
853
854 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000855 zlog_debug ("ospf_vl_new(): Start");
paul718e3742002-12-13 20:15:29 +0000856 if (vlink_count == OSPF_VL_MAX_COUNT)
857 {
858 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000859 zlog_debug ("ospf_vl_new(): Alarm: "
paul718e3742002-12-13 20:15:29 +0000860 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
861 return NULL;
862 }
863
864 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000865 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
paul718e3742002-12-13 20:15:29 +0000866
ajsa3491982005-04-02 22:50:38 +0000867 snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count);
868 vi = if_create (ifname, strnlen(ifname, sizeof(ifname)));
paul718e3742002-12-13 20:15:29 +0000869 co = connected_new ();
870 co->ifp = vi;
871 listnode_add (vi->connected, co);
872
873 p = prefix_ipv4_new ();
874 p->family = AF_INET;
875 p->prefix.s_addr = 0;
876 p->prefixlen = 0;
877
878 co->address = (struct prefix *)p;
879
paul68980082003-03-25 05:07:42 +0000880 voi = ospf_if_new (ospf, vi, co->address);
paul718e3742002-12-13 20:15:29 +0000881 if (voi == NULL)
882 {
883 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000884 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
paul718e3742002-12-13 20:15:29 +0000885 return NULL;
886 }
887 voi->connected = co;
888 voi->vl_data = vl_data;
889 voi->ifp->mtu = OSPF_VL_MTU;
890 voi->type = OSPF_IFTYPE_VIRTUALLINK;
891
paul106d2fd2003-08-01 00:24:13 +0000892 vlink_count++;
paul718e3742002-12-13 20:15:29 +0000893 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000894 zlog_debug ("ospf_vl_new(): Created name: %s", ifname);
paul718e3742002-12-13 20:15:29 +0000895 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000896 zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name);
paul718e3742002-12-13 20:15:29 +0000897
898 area_id.s_addr = 0;
paul68980082003-03-25 05:07:42 +0000899 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000900 voi->area = area;
901
902 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000903 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
paul718e3742002-12-13 20:15:29 +0000904
Paul Jakma478aab92006-04-03 21:25:32 +0000905 ospf_nbr_add_self (voi);
paul718e3742002-12-13 20:15:29 +0000906 ospf_area_add_if (voi->area, voi);
907
908 ospf_if_stream_set (voi);
909
910 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000911 zlog_debug ("ospf_vl_new(): Stop");
paul718e3742002-12-13 20:15:29 +0000912 return voi;
913}
914
paul4dadc292005-05-06 21:37:42 +0000915static void
paul718e3742002-12-13 20:15:29 +0000916ospf_vl_if_delete (struct ospf_vl_data *vl_data)
917{
918 struct interface *ifp = vl_data->vl_oi->ifp;
919 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
920 vl_data->vl_oi->address->prefixlen = 0;
921 ospf_if_free (vl_data->vl_oi);
922 if_delete (ifp);
923 vlink_count--;
924}
925
Paul Jakma9c27ef92006-05-04 07:32:57 +0000926/* Look up vl_data for given peer, optionally qualified to be in the
927 * specified area. NULL area returns first found..
928 */
paul718e3742002-12-13 20:15:29 +0000929struct ospf_vl_data *
Paul Jakma9c27ef92006-05-04 07:32:57 +0000930ospf_vl_lookup (struct ospf *ospf, struct ospf_area *area,
931 struct in_addr vl_peer)
paul718e3742002-12-13 20:15:29 +0000932{
933 struct ospf_vl_data *vl_data;
hasso52dc7ee2004-09-23 19:18:23 +0000934 struct listnode *node;
Paul Jakma9c27ef92006-05-04 07:32:57 +0000935
936 if (IS_DEBUG_OSPF_EVENT)
937 {
938 zlog_debug ("%s: Looking for %s", __func__, inet_ntoa (vl_peer));
939 if (area)
940 zlog_debug ("%s: in area %s", __func__, inet_ntoa (area->area_id));
941 }
942
943 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
944 {
945 if (IS_DEBUG_OSPF_EVENT)
946 zlog_debug ("%s: VL %s, peer %s", __func__,
947 vl_data->vl_oi->ifp->name,
948 inet_ntoa (vl_data->vl_peer));
949
950 if (area && !IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
951 continue;
952
953 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &vl_peer))
954 return vl_data;
955 }
paul718e3742002-12-13 20:15:29 +0000956
957 return NULL;
958}
959
paul4dadc292005-05-06 21:37:42 +0000960static void
paul718e3742002-12-13 20:15:29 +0000961ospf_vl_shutdown (struct ospf_vl_data *vl_data)
962{
963 struct ospf_interface *oi;
964
965 if ((oi = vl_data->vl_oi) == NULL)
966 return;
967
968 oi->address->u.prefix4.s_addr = 0;
969 oi->address->prefixlen = 0;
970
971 UNSET_FLAG (oi->ifp->flags, IFF_UP);
972 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
973 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
974}
975
976void
paul68980082003-03-25 05:07:42 +0000977ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000978{
paul68980082003-03-25 05:07:42 +0000979 listnode_add (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +0000980#ifdef HAVE_SNMP
981 ospf_snmp_vl_add (vl_data);
982#endif /* HAVE_SNMP */
983}
984
985void
paul68980082003-03-25 05:07:42 +0000986ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000987{
988 ospf_vl_shutdown (vl_data);
989 ospf_vl_if_delete (vl_data);
990
991#ifdef HAVE_SNMP
992 ospf_snmp_vl_delete (vl_data);
993#endif /* HAVE_SNMP */
paul68980082003-03-25 05:07:42 +0000994 listnode_delete (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +0000995
996 ospf_vl_data_free (vl_data);
997}
998
paul4dadc292005-05-06 21:37:42 +0000999static int
paul718e3742002-12-13 20:15:29 +00001000ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
1001{
1002 int changed = 0;
1003 struct ospf_interface *voi;
hasso52dc7ee2004-09-23 19:18:23 +00001004 struct listnode *node;
pauleb3da6d2005-10-18 04:20:33 +00001005 struct vertex_parent *vp = NULL;
Donald Sharp0bc874b2015-07-29 19:16:13 -04001006 unsigned int i;
paul718e3742002-12-13 20:15:29 +00001007 struct router_lsa *rl;
1008
1009 voi = vl_data->vl_oi;
1010
1011 if (voi->output_cost != v->distance)
1012 {
paulcd59da62004-05-05 17:26:55 +00001013
paul718e3742002-12-13 20:15:29 +00001014 voi->output_cost = v->distance;
1015 changed = 1;
1016 }
1017
pauleb3da6d2005-10-18 04:20:33 +00001018 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
paul1eb8ef22005-04-07 07:30:20 +00001019 {
Paul Jakma9c27ef92006-05-04 07:32:57 +00001020 vl_data->nexthop.oi = vp->nexthop->oi;
1021 vl_data->nexthop.router = vp->nexthop->router;
paul1eb8ef22005-04-07 07:30:20 +00001022
1023 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
Paul Jakma9c27ef92006-05-04 07:32:57 +00001024 &vl_data->nexthop.oi->address->u.prefix4))
paul1eb8ef22005-04-07 07:30:20 +00001025 changed = 1;
paulcd59da62004-05-05 17:26:55 +00001026
Paul Jakma9c27ef92006-05-04 07:32:57 +00001027 voi->address->u.prefix4 = vl_data->nexthop.oi->address->u.prefix4;
1028 voi->address->prefixlen = vl_data->nexthop.oi->address->prefixlen;
paul718e3742002-12-13 20:15:29 +00001029
paul1eb8ef22005-04-07 07:30:20 +00001030 break; /* We take the first interface. */
1031 }
paul718e3742002-12-13 20:15:29 +00001032
1033 rl = (struct router_lsa *)v->lsa;
pauld355bfa2004-04-08 07:43:45 +00001034
1035 /* use SPF determined backlink index in struct vertex
1036 * for virtual link destination address
1037 */
pauleb3da6d2005-10-18 04:20:33 +00001038 if (vp && vp->backlink >= 0)
paul718e3742002-12-13 20:15:29 +00001039 {
pauld355bfa2004-04-08 07:43:45 +00001040 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
pauleb3da6d2005-10-18 04:20:33 +00001041 &rl->link[vp->backlink].link_data))
pauld355bfa2004-04-08 07:43:45 +00001042 changed = 1;
pauleb3da6d2005-10-18 04:20:33 +00001043 vl_data->peer_addr = rl->link[vp->backlink].link_data;
pauld355bfa2004-04-08 07:43:45 +00001044 }
1045 else
1046 {
1047 /* This is highly odd, there is no backlink index
1048 * there should be due to the ospf_spf_has_link() check
1049 * in SPF. Lets warn and try pick a link anyway.
1050 */
1051 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1052 vl_data->vl_oi->ifp->name);
1053 for (i = 0; i < ntohs (rl->links); i++)
paul2e6b0bb2003-06-22 08:17:12 +00001054 {
pauld355bfa2004-04-08 07:43:45 +00001055 switch (rl->link[i].type)
1056 {
1057 case LSA_LINK_TYPE_VIRTUALLINK:
1058 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001059 zlog_debug ("found back link through VL");
pauld355bfa2004-04-08 07:43:45 +00001060 case LSA_LINK_TYPE_TRANSIT:
1061 case LSA_LINK_TYPE_POINTOPOINT:
paulcd59da62004-05-05 17:26:55 +00001062 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
1063 &rl->link[i].link_data))
1064 changed = 1;
pauld355bfa2004-04-08 07:43:45 +00001065 vl_data->peer_addr = rl->link[i].link_data;
pauld355bfa2004-04-08 07:43:45 +00001066 }
paul2e6b0bb2003-06-22 08:17:12 +00001067 }
paul718e3742002-12-13 20:15:29 +00001068 }
pauld355bfa2004-04-08 07:43:45 +00001069
1070 if (IS_DEBUG_OSPF_EVENT)
Paul Jakma9c27ef92006-05-04 07:32:57 +00001071 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__,
pauld355bfa2004-04-08 07:43:45 +00001072 vl_data->vl_oi->ifp->name,
Paul Jakma9c27ef92006-05-04 07:32:57 +00001073 inet_ntoa(vl_data->peer_addr),
1074 voi->output_cost,
1075 (changed ? " " : " un"));
pauld355bfa2004-04-08 07:43:45 +00001076
paul2e6b0bb2003-06-22 08:17:12 +00001077 return changed;
paul718e3742002-12-13 20:15:29 +00001078}
1079
1080
1081void
paul68980082003-03-25 05:07:42 +00001082ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
paul718e3742002-12-13 20:15:29 +00001083 struct vertex *v)
1084{
paul68980082003-03-25 05:07:42 +00001085 struct ospf *ospf = area->ospf;
hasso52dc7ee2004-09-23 19:18:23 +00001086 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001087 struct ospf_vl_data *vl_data;
1088 struct ospf_interface *oi;
1089
1090 if (IS_DEBUG_OSPF_EVENT)
1091 {
ajs60925302004-12-08 17:45:02 +00001092 zlog_debug ("ospf_vl_up_check(): Start");
1093 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid));
1094 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +00001095 }
1096
paul1eb8ef22005-04-07 07:30:20 +00001097 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
paul718e3742002-12-13 20:15:29 +00001098 {
paul718e3742002-12-13 20:15:29 +00001099 if (IS_DEBUG_OSPF_EVENT)
1100 {
Paul Jakma9c27ef92006-05-04 07:32:57 +00001101 zlog_debug ("%s: considering VL, %s in area %s", __func__,
1102 vl_data->vl_oi->ifp->name,
1103 inet_ntoa (vl_data->vl_area_id));
1104 zlog_debug ("%s: peer ID: %s", __func__,
paul718e3742002-12-13 20:15:29 +00001105 inet_ntoa (vl_data->vl_peer));
1106 }
1107
1108 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) &&
1109 IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1110 {
1111 oi = vl_data->vl_oi;
1112 SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1113
1114 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001115 zlog_debug ("ospf_vl_up_check(): this VL matched");
paul718e3742002-12-13 20:15:29 +00001116
1117 if (oi->state == ISM_Down)
1118 {
1119 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001120 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
paul718e3742002-12-13 20:15:29 +00001121 SET_FLAG (oi->ifp->flags, IFF_UP);
paul2e6b0bb2003-06-22 08:17:12 +00001122 OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
paul718e3742002-12-13 20:15:29 +00001123 }
1124
paul2e6b0bb2003-06-22 08:17:12 +00001125 if (ospf_vl_set_params (vl_data, v))
1126 {
1127 if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
ajs60925302004-12-08 17:45:02 +00001128 zlog_debug ("ospf_vl_up_check: VL cost change,"
paulcd59da62004-05-05 17:26:55 +00001129 " scheduling router lsa refresh");
Paul Jakmac363d382010-01-24 22:42:13 +00001130 if (ospf->backbone)
1131 ospf_router_lsa_update_area (ospf->backbone);
paul2e6b0bb2003-06-22 08:17:12 +00001132 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
ajs60925302004-12-08 17:45:02 +00001133 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
paul2e6b0bb2003-06-22 08:17:12 +00001134 }
paul718e3742002-12-13 20:15:29 +00001135 }
1136 }
1137}
1138
1139void
paul68980082003-03-25 05:07:42 +00001140ospf_vl_unapprove (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001141{
hasso52dc7ee2004-09-23 19:18:23 +00001142 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001143 struct ospf_vl_data *vl_data;
1144
paul1eb8ef22005-04-07 07:30:20 +00001145 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
1146 UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
paul718e3742002-12-13 20:15:29 +00001147}
1148
1149void
paul68980082003-03-25 05:07:42 +00001150ospf_vl_shut_unapproved (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001151{
paul1eb8ef22005-04-07 07:30:20 +00001152 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001153 struct ospf_vl_data *vl_data;
1154
paul1eb8ef22005-04-07 07:30:20 +00001155 for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
1156 if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
1157 ospf_vl_shutdown (vl_data);
paul718e3742002-12-13 20:15:29 +00001158}
1159
1160int
1161ospf_full_virtual_nbrs (struct ospf_area *area)
1162{
1163 if (IS_DEBUG_OSPF_EVENT)
1164 {
ajs60925302004-12-08 17:45:02 +00001165 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
paul718e3742002-12-13 20:15:29 +00001166 inet_ntoa (area->area_id));
ajs60925302004-12-08 17:45:02 +00001167 zlog_debug ("there are %d of them", area->full_vls);
paul718e3742002-12-13 20:15:29 +00001168 }
1169
1170 return area->full_vls;
1171}
1172
1173int
1174ospf_vls_in_area (struct ospf_area *area)
1175{
hasso52dc7ee2004-09-23 19:18:23 +00001176 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001177 struct ospf_vl_data *vl_data;
1178 int c = 0;
1179
paul1eb8ef22005-04-07 07:30:20 +00001180 for (ALL_LIST_ELEMENTS_RO (area->ospf->vlinks, node, vl_data))
1181 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1182 c++;
paul718e3742002-12-13 20:15:29 +00001183
1184 return c;
1185}
1186
David Lamparter6b0655a2014-06-04 06:53:35 +02001187
paul718e3742002-12-13 20:15:29 +00001188struct crypt_key *
1189ospf_crypt_key_new ()
1190{
Stephen Hemminger393deb92008-08-18 14:13:29 -07001191 return XCALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
paul718e3742002-12-13 20:15:29 +00001192}
1193
1194void
hasso52dc7ee2004-09-23 19:18:23 +00001195ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
paul718e3742002-12-13 20:15:29 +00001196{
1197 listnode_add (crypt, ck);
1198}
1199
1200struct crypt_key *
hasso52dc7ee2004-09-23 19:18:23 +00001201ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
paul718e3742002-12-13 20:15:29 +00001202{
hasso52dc7ee2004-09-23 19:18:23 +00001203 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001204 struct crypt_key *ck;
1205
paul1eb8ef22005-04-07 07:30:20 +00001206 for (ALL_LIST_ELEMENTS_RO (auth_crypt, node, ck))
1207 if (ck->key_id == key_id)
1208 return ck;
paul718e3742002-12-13 20:15:29 +00001209
1210 return NULL;
1211}
1212
1213int
hasso52dc7ee2004-09-23 19:18:23 +00001214ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
paul718e3742002-12-13 20:15:29 +00001215{
paul1eb8ef22005-04-07 07:30:20 +00001216 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001217 struct crypt_key *ck;
1218
paul1eb8ef22005-04-07 07:30:20 +00001219 for (ALL_LIST_ELEMENTS (auth_crypt, node, nnode, ck))
paul718e3742002-12-13 20:15:29 +00001220 {
paul718e3742002-12-13 20:15:29 +00001221 if (ck->key_id == key_id)
1222 {
1223 listnode_delete (auth_crypt, ck);
vincentba682532005-09-29 13:52:57 +00001224 XFREE (MTYPE_OSPF_CRYPT_KEY, ck);
paul718e3742002-12-13 20:15:29 +00001225 return 1;
1226 }
1227 }
1228
1229 return 0;
1230}
1231
ajsbc18d612004-12-15 15:07:19 +00001232u_char
1233ospf_default_iftype(struct interface *ifp)
1234{
1235 if (if_is_pointopoint (ifp))
1236 return OSPF_IFTYPE_POINTOPOINT;
1237 else if (if_is_loopback (ifp))
1238 return OSPF_IFTYPE_LOOPBACK;
1239 else
1240 return OSPF_IFTYPE_BROADCAST;
1241}
1242
paul718e3742002-12-13 20:15:29 +00001243void
1244ospf_if_init ()
1245{
1246 /* Initialize Zebra interface data structure. */
paul020709f2003-04-04 02:44:16 +00001247 om->iflist = iflist;
paul718e3742002-12-13 20:15:29 +00001248 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1249 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
1250}