blob: af4f0a608438fd6715e5c064a7ff05842dbc4b55 [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#ifdef HAVE_OPAQUE_LSA
245 ospf_opaque_type9_lsa_init (oi);
246#endif /* HAVE_OPAQUE_LSA */
247
paul68980082003-03-25 05:07:42 +0000248 oi->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000249
250 return oi;
251}
252
253/* Restore an interface to its pre UP state
254 Used from ism_interface_down only */
255void
256ospf_if_cleanup (struct ospf_interface *oi)
257{
258 struct route_node *rn;
paul1eb8ef22005-04-07 07:30:20 +0000259 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000260 struct ospf_neighbor *nbr;
paul1eb8ef22005-04-07 07:30:20 +0000261 struct ospf_nbr_nbma *nbr_nbma;
262 struct ospf_lsa *lsa;
paul718e3742002-12-13 20:15:29 +0000263
paul1a643f82006-01-11 01:08:19 +0000264 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
paul718e3742002-12-13 20:15:29 +0000265 /* delete all static neighbors attached to this interface */
paul1eb8ef22005-04-07 07:30:20 +0000266 for (ALL_LIST_ELEMENTS (oi->nbr_nbma, node, nnode, nbr_nbma))
paul718e3742002-12-13 20:15:29 +0000267 {
paul718e3742002-12-13 20:15:29 +0000268 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
269
270 if (nbr_nbma->nbr)
271 {
272 nbr_nbma->nbr->nbr_nbma = NULL;
273 nbr_nbma->nbr = NULL;
274 }
275
276 nbr_nbma->oi = NULL;
277
278 listnode_delete (oi->nbr_nbma, nbr_nbma);
279 }
280
281 /* send Neighbor event KillNbr to all associated neighbors. */
282 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
283 if ((nbr = rn->info) != NULL)
284 if (nbr != oi->nbr_self)
285 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr);
286
287 /* Cleanup Link State Acknowlegdment list. */
paul1eb8ef22005-04-07 07:30:20 +0000288 for (ALL_LIST_ELEMENTS (oi->ls_ack, node, nnode, lsa))
Paul Jakma1fe6ed32006-07-26 09:37:26 +0000289 ospf_lsa_unlock (&lsa); /* oi->ls_ack */
paul718e3742002-12-13 20:15:29 +0000290 list_delete_all_node (oi->ls_ack);
291
292 oi->crypt_seqnum = 0;
293
294 /* Empty link state update queue */
295 ospf_ls_upd_queue_empty (oi);
296
paul1a643f82006-01-11 01:08:19 +0000297 /* Reset pseudo neighbor. */
Paul Jakmac920e512015-09-08 15:31:45 +0100298 ospf_nbr_self_reset (oi);
paul718e3742002-12-13 20:15:29 +0000299}
300
301void
302ospf_if_free (struct ospf_interface *oi)
303{
304 ospf_if_down (oi);
305
306 assert (oi->state == ISM_Down);
307
308#ifdef HAVE_OPAQUE_LSA
309 ospf_opaque_type9_lsa_term (oi);
310#endif /* HAVE_OPAQUE_LSA */
311
312 /* Free Pseudo Neighbour */
313 ospf_nbr_delete (oi->nbr_self);
314
315 route_table_finish (oi->nbrs);
316 route_table_finish (oi->ls_upd_queue);
317
318 /* Free any lists that should be freed */
319 list_free (oi->nbr_nbma);
320
321 list_free (oi->ls_ack);
322 list_free (oi->ls_ack_direct.ls_ack);
323
324 ospf_delete_from_if (oi->ifp, oi);
325
paul68980082003-03-25 05:07:42 +0000326 listnode_delete (oi->ospf->oiflist, oi);
paul718e3742002-12-13 20:15:29 +0000327 listnode_delete (oi->area->oiflist, oi);
328
Paul Jakmacfd670f2010-04-15 11:39:05 +0100329 thread_cancel_event (master, oi);
330
paul718e3742002-12-13 20:15:29 +0000331 memset (oi, 0, sizeof (*oi));
332 XFREE (MTYPE_OSPF_IF, oi);
333}
334
David Lamparter6b0655a2014-06-04 06:53:35 +0200335
paul718e3742002-12-13 20:15:29 +0000336/*
337* check if interface with given address is configured and
hasso3fb9cd62004-10-19 19:44:43 +0000338* return it if yes. special treatment for PtP networks.
paul718e3742002-12-13 20:15:29 +0000339*/
340struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000341ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
paul718e3742002-12-13 20:15:29 +0000342{
paul1eb8ef22005-04-07 07:30:20 +0000343 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000344 struct ospf_interface *oi;
hasso3fb9cd62004-10-19 19:44:43 +0000345 struct prefix_ipv4 addr;
346
347 addr.family = AF_INET;
348 addr.prefix = *address;
349 addr.prefixlen = IPV4_MAX_PREFIXLEN;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000350
paul1eb8ef22005-04-07 07:30:20 +0000351 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
352 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul718e3742002-12-13 20:15:29 +0000353 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000354 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
hasso3fb9cd62004-10-19 19:44:43 +0000355 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000356 /* special leniency: match if addr is anywhere on peer subnet */
357 if (prefix_match(CONNECTED_PREFIX(oi->connected),
358 (struct prefix *)&addr))
359 return oi;
hasso3fb9cd62004-10-19 19:44:43 +0000360 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000361 else
hasso3fb9cd62004-10-19 19:44:43 +0000362 {
363 if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
364 return oi;
365 }
paul718e3742002-12-13 20:15:29 +0000366 }
paul718e3742002-12-13 20:15:29 +0000367 return NULL;
368}
369
370int
371ospf_if_is_up (struct ospf_interface *oi)
372{
373 return if_is_up (oi->ifp);
374}
375
376struct ospf_interface *
hasso2db3d052004-02-11 21:52:13 +0000377ospf_if_exists (struct ospf_interface *oic)
378{
hasso52dc7ee2004-09-23 19:18:23 +0000379 struct listnode *node;
hasso2db3d052004-02-11 21:52:13 +0000380 struct ospf *ospf;
381 struct ospf_interface *oi;
382
Paul Jakmae43be0e2006-05-12 23:00:06 +0000383 if ((ospf = ospf_lookup ()) == NULL)
384 return NULL;
hasso2db3d052004-02-11 21:52:13 +0000385
paul1eb8ef22005-04-07 07:30:20 +0000386 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
387 if (oi == oic)
hasso2db3d052004-02-11 21:52:13 +0000388 return oi;
paul1eb8ef22005-04-07 07:30:20 +0000389
hasso2db3d052004-02-11 21:52:13 +0000390 return NULL;
391}
392
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200393/* Lookup OSPF interface by router LSA posistion */
394struct ospf_interface *
395ospf_if_lookup_by_lsa_pos (struct ospf_area *area, int lsa_pos)
396{
397 struct listnode *node;
398 struct ospf_interface *oi;
399
400 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
401 {
402 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
403 return oi;
404 }
405 return NULL;
406}
407
hasso2db3d052004-02-11 21:52:13 +0000408struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000409ospf_if_lookup_by_local_addr (struct ospf *ospf,
410 struct interface *ifp, struct in_addr address)
paul718e3742002-12-13 20:15:29 +0000411{
hasso52dc7ee2004-09-23 19:18:23 +0000412 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000413 struct ospf_interface *oi;
414
paul1eb8ef22005-04-07 07:30:20 +0000415 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
416 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul718e3742002-12-13 20:15:29 +0000417 {
418 if (ifp && oi->ifp != ifp)
419 continue;
420
421 if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
422 return oi;
423 }
424
425 return NULL;
426}
427
428struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000429ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
paul718e3742002-12-13 20:15:29 +0000430{
hasso52dc7ee2004-09-23 19:18:23 +0000431 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000432 struct ospf_interface *oi;
paul718e3742002-12-13 20:15:29 +0000433
434 /* Check each Interface. */
paul1eb8ef22005-04-07 07:30:20 +0000435 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
paul68980082003-03-25 05:07:42 +0000436 {
paul1eb8ef22005-04-07 07:30:20 +0000437 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul68980082003-03-25 05:07:42 +0000438 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000439 struct prefix ptmp;
440
441 prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
paul68980082003-03-25 05:07:42 +0000442 apply_mask (&ptmp);
443 if (prefix_same (&ptmp, (struct prefix *) p))
444 return oi;
445 }
446 }
paul718e3742002-12-13 20:15:29 +0000447 return NULL;
448}
449
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200450/* determine receiving interface by ifp and source address */
paul718e3742002-12-13 20:15:29 +0000451struct ospf_interface *
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200452ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src,
453 struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000454{
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200455 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000456 struct prefix_ipv4 addr;
457 struct ospf_interface *oi, *match;
458
459 addr.family = AF_INET;
460 addr.prefix = src;
461 addr.prefixlen = IPV4_MAX_BITLEN;
462
463 match = NULL;
464
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200465 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
paul718e3742002-12-13 20:15:29 +0000466 {
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200467 oi = rn->info;
468
469 if (!oi) /* oi can be NULL for PtP aliases */
470 continue;
471
paul718e3742002-12-13 20:15:29 +0000472 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
473 continue;
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +0200474
Paul Jakma1a8ee0e2006-03-30 14:20:00 +0000475 if (if_is_loopback (oi->ifp))
476 continue;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000477
478 if (prefix_match (CONNECTED_PREFIX(oi->connected),
479 (struct prefix *) &addr))
paul718e3742002-12-13 20:15:29 +0000480 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000481 if ( (match == NULL) ||
482 (match->address->prefixlen < oi->address->prefixlen)
483 )
484 match = oi;
paul718e3742002-12-13 20:15:29 +0000485 }
486 }
487
488 return match;
489}
David Lamparter6b0655a2014-06-04 06:53:35 +0200490
paul718e3742002-12-13 20:15:29 +0000491void
492ospf_if_stream_set (struct ospf_interface *oi)
493{
494 /* set output fifo queue. */
495 if (oi->obuf == NULL)
496 oi->obuf = ospf_fifo_new ();
497}
498
499void
500ospf_if_stream_unset (struct ospf_interface *oi)
501{
paul68980082003-03-25 05:07:42 +0000502 struct ospf *ospf = oi->ospf;
503
paul718e3742002-12-13 20:15:29 +0000504 if (oi->obuf)
505 {
506 ospf_fifo_free (oi->obuf);
507 oi->obuf = NULL;
508
509 if (oi->on_write_q)
510 {
paul68980082003-03-25 05:07:42 +0000511 listnode_delete (ospf->oi_write_q, oi);
512 if (list_isempty(ospf->oi_write_q))
513 OSPF_TIMER_OFF (ospf->t_write);
paul718e3742002-12-13 20:15:29 +0000514 oi->on_write_q = 0;
515 }
516 }
517}
paul68980082003-03-25 05:07:42 +0000518
David Lamparter6b0655a2014-06-04 06:53:35 +0200519
paul4dadc292005-05-06 21:37:42 +0000520static struct ospf_if_params *
521ospf_new_if_params (void)
paul718e3742002-12-13 20:15:29 +0000522{
523 struct ospf_if_params *oip;
524
Stephen Hemminger393deb92008-08-18 14:13:29 -0700525 oip = XCALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
paul718e3742002-12-13 20:15:29 +0000526
527 if (!oip)
528 return NULL;
529
paul718e3742002-12-13 20:15:29 +0000530 UNSET_IF_PARAM (oip, output_cost_cmd);
531 UNSET_IF_PARAM (oip, transmit_delay);
532 UNSET_IF_PARAM (oip, retransmit_interval);
533 UNSET_IF_PARAM (oip, passive_interface);
534 UNSET_IF_PARAM (oip, v_hello);
paulf9ad9372005-10-21 00:45:17 +0000535 UNSET_IF_PARAM (oip, fast_hello);
paul718e3742002-12-13 20:15:29 +0000536 UNSET_IF_PARAM (oip, v_wait);
537 UNSET_IF_PARAM (oip, priority);
538 UNSET_IF_PARAM (oip, type);
539 UNSET_IF_PARAM (oip, auth_simple);
540 UNSET_IF_PARAM (oip, auth_crypt);
541 UNSET_IF_PARAM (oip, auth_type);
paulec1ca632003-06-04 02:23:15 +0000542
543 oip->auth_crypt = list_new ();
paul718e3742002-12-13 20:15:29 +0000544
Paul Jakma7eb5b472009-10-13 16:13:13 +0100545 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
546
paul718e3742002-12-13 20:15:29 +0000547 return oip;
548}
549
550void
551ospf_del_if_params (struct ospf_if_params *oip)
552{
553 list_delete (oip->auth_crypt);
554 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
555}
556
557void
558ospf_free_if_params (struct interface *ifp, struct in_addr addr)
559{
560 struct ospf_if_params *oip;
561 struct prefix_ipv4 p;
562 struct route_node *rn;
gdt630e4802004-08-31 17:28:41 +0000563
564 p.family = AF_INET;
paul718e3742002-12-13 20:15:29 +0000565 p.prefixlen = IPV4_MAX_PREFIXLEN;
566 p.prefix = addr;
567 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
568 if (!rn || !rn->info)
569 return;
570
571 oip = rn->info;
572 route_unlock_node (rn);
573
574 if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
575 !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
576 !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
577 !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
578 !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
paulf9ad9372005-10-21 00:45:17 +0000579 !OSPF_IF_PARAM_CONFIGURED (oip, fast_hello) &&
paul718e3742002-12-13 20:15:29 +0000580 !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
581 !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
582 !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
583 !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
584 !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
Paul Jakma7eb5b472009-10-13 16:13:13 +0100585 listcount (oip->auth_crypt) == 0 &&
586 ntohl (oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER)
paul718e3742002-12-13 20:15:29 +0000587 {
588 ospf_del_if_params (oip);
589 rn->info = NULL;
590 route_unlock_node (rn);
591 }
592}
593
594struct ospf_if_params *
595ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
596{
597 struct prefix_ipv4 p;
598 struct route_node *rn;
599
gdt630e4802004-08-31 17:28:41 +0000600 p.family = AF_INET;
paul718e3742002-12-13 20:15:29 +0000601 p.prefixlen = IPV4_MAX_PREFIXLEN;
602 p.prefix = addr;
603
604 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
605
606 if (rn)
607 {
608 route_unlock_node (rn);
609 return rn->info;
610 }
611
612 return NULL;
613}
614
615struct ospf_if_params *
616ospf_get_if_params (struct interface *ifp, struct in_addr addr)
617{
618 struct prefix_ipv4 p;
619 struct route_node *rn;
620
621 p.family = AF_INET;
622 p.prefixlen = IPV4_MAX_PREFIXLEN;
623 p.prefix = addr;
624
625 rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
626
627 if (rn->info == NULL)
628 rn->info = ospf_new_if_params ();
629 else
630 route_unlock_node (rn);
631
632 return rn->info;
633}
634
635void
636ospf_if_update_params (struct interface *ifp, struct in_addr addr)
637{
638 struct route_node *rn;
639 struct ospf_interface *oi;
640
641 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
642 {
643 if ((oi = rn->info) == NULL)
644 continue;
645
646 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
647 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
648 }
649}
650
651int
652ospf_if_new_hook (struct interface *ifp)
653{
654 int rc = 0;
655
Stephen Hemminger393deb92008-08-18 14:13:29 -0700656 ifp->info = XCALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
paul718e3742002-12-13 20:15:29 +0000657
658 IF_OIFS (ifp) = route_table_init ();
659 IF_OIFS_PARAMS (ifp) = route_table_init ();
660
661 IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
662
663 SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
664 IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
665
666 SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
667 IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
668
669 SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
670 IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
671
vincentba682532005-09-29 13:52:57 +0000672 IF_DEF_PARAMS (ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
673
paul718e3742002-12-13 20:15:29 +0000674 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
675 IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
676
paulf9ad9372005-10-21 00:45:17 +0000677 SET_IF_PARAM (IF_DEF_PARAMS (ifp), fast_hello);
678 IF_DEF_PARAMS (ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
679
paul718e3742002-12-13 20:15:29 +0000680 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
681 IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
682
683 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
684 memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
685
paul718e3742002-12-13 20:15:29 +0000686 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
687 IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
688
689#ifdef HAVE_OPAQUE_LSA
690 rc = ospf_opaque_new_if (ifp);
691#endif /* HAVE_OPAQUE_LSA */
692 return rc;
693}
694
paul4dadc292005-05-06 21:37:42 +0000695static int
paul718e3742002-12-13 20:15:29 +0000696ospf_if_delete_hook (struct interface *ifp)
697{
698 int rc = 0;
paul940b01a2004-02-17 20:07:30 +0000699 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000700#ifdef HAVE_OPAQUE_LSA
701 rc = ospf_opaque_del_if (ifp);
702#endif /* HAVE_OPAQUE_LSA */
paul940b01a2004-02-17 20:07:30 +0000703
paul718e3742002-12-13 20:15:29 +0000704 route_table_finish (IF_OIFS (ifp));
paul940b01a2004-02-17 20:07:30 +0000705
706 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
707 if (rn->info)
708 ospf_del_if_params (rn->info);
paul718e3742002-12-13 20:15:29 +0000709 route_table_finish (IF_OIFS_PARAMS (ifp));
paul940b01a2004-02-17 20:07:30 +0000710
paulcfc959b2003-06-04 02:28:45 +0000711 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
paul718e3742002-12-13 20:15:29 +0000712 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
713 ifp->info = NULL;
714
715 return rc;
716}
717
718int
719ospf_if_is_enable (struct ospf_interface *oi)
720{
721 if (!if_is_loopback (oi->ifp))
722 if (if_is_up (oi->ifp))
723 return 1;
724
725 return 0;
726}
727
ajsba6454e2005-02-08 15:37:30 +0000728void
729ospf_if_set_multicast(struct ospf_interface *oi)
730{
731 if ((oi->state > ISM_Loopback) &&
732 (oi->type != OSPF_IFTYPE_LOOPBACK) &&
733 (oi->type != OSPF_IFTYPE_VIRTUALLINK) &&
Andrew J. Schorre8a56f02007-04-21 20:46:31 +0000734 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
ajsba6454e2005-02-08 15:37:30 +0000735 {
736 /* The interface should belong to the OSPF-all-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000737 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) &&
ajsba6454e2005-02-08 15:37:30 +0000738 (ospf_if_add_allspfrouters(oi->ospf, oi->address,
739 oi->ifp->ifindex) >= 0))
Paul Jakma429ac782006-06-15 18:40:49 +0000740 /* Set the flag only if the system call to join succeeded. */
741 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000742 }
743 else
744 {
745 /* The interface should NOT belong to the OSPF-all-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000746 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS))
ajsba6454e2005-02-08 15:37:30 +0000747 {
Paul Jakma429ac782006-06-15 18:40:49 +0000748 /* Only actually drop if this is the last reference */
749 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
750 ospf_if_drop_allspfrouters (oi->ospf, oi->address,
751 oi->ifp->ifindex);
ajsba6454e2005-02-08 15:37:30 +0000752 /* Unset the flag regardless of whether the system call to leave
753 the group succeeded, since it's much safer to assume that
754 we are not a member. */
Paul Jakma429ac782006-06-15 18:40:49 +0000755 OI_MEMBER_LEFT(oi,MEMBER_ALLROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000756 }
757 }
758
759 if (((oi->type == OSPF_IFTYPE_BROADCAST) ||
760 (oi->type == OSPF_IFTYPE_POINTOPOINT)) &&
761 ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) &&
Andrew J. Schorre8a56f02007-04-21 20:46:31 +0000762 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
ajsba6454e2005-02-08 15:37:30 +0000763 {
764 /* The interface should belong to the OSPF-designated-routers group. */
Paul Jakma429ac782006-06-15 18:40:49 +0000765 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) &&
ajsba6454e2005-02-08 15:37:30 +0000766 (ospf_if_add_alldrouters(oi->ospf, oi->address,
767 oi->ifp->ifindex) >= 0))
768 /* Set the flag only if the system call to join succeeded. */
Paul Jakma429ac782006-06-15 18:40:49 +0000769 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000770 }
771 else
772 {
773 /* The interface should NOT belong to the OSPF-designated-routers group */
Paul Jakma429ac782006-06-15 18:40:49 +0000774 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS))
ajsba6454e2005-02-08 15:37:30 +0000775 {
Paul Jakma429ac782006-06-15 18:40:49 +0000776 /* drop only if last reference */
777 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
778 ospf_if_drop_alldrouters(oi->ospf, oi->address, oi->ifp->ifindex);
779
ajsba6454e2005-02-08 15:37:30 +0000780 /* Unset the flag regardless of whether the system call to leave
781 the group succeeded, since it's much safer to assume that
782 we are not a member. */
Paul Jakma429ac782006-06-15 18:40:49 +0000783 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
ajsba6454e2005-02-08 15:37:30 +0000784 }
785 }
786}
787
paul718e3742002-12-13 20:15:29 +0000788int
789ospf_if_up (struct ospf_interface *oi)
790{
791 if (oi == NULL)
792 return 0;
793
794 if (oi->type == OSPF_IFTYPE_LOOPBACK)
795 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
796 else
797 {
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000798 struct ospf *ospf = ospf_lookup ();
799 if (ospf != NULL)
800 ospf_adjust_sndbuflen (ospf, oi->ifp->mtu);
801 else
Denis Ovsienkofb31c0f2007-09-18 09:03:13 +0000802 zlog_warn ("%s: ospf_lookup() returned NULL", __func__);
paul718e3742002-12-13 20:15:29 +0000803 ospf_if_stream_set (oi);
804 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
805 }
806
807 return 1;
808}
809
810int
811ospf_if_down (struct ospf_interface *oi)
812{
813 if (oi == NULL)
814 return 0;
815
816 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
Joakim Tjernlundc81ee5c2012-07-07 17:06:11 +0200817 /* delete position in router LSA */
818 oi->lsa_pos_beg = 0;
819 oi->lsa_pos_end = 0;
paul718e3742002-12-13 20:15:29 +0000820 /* Shutdown packet reception and sending */
821 ospf_if_stream_unset (oi);
paul718e3742002-12-13 20:15:29 +0000822
823 return 1;
824}
825
David Lamparter6b0655a2014-06-04 06:53:35 +0200826
paul718e3742002-12-13 20:15:29 +0000827/* Virtual Link related functions. */
828
829struct ospf_vl_data *
830ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
831{
832 struct ospf_vl_data *vl_data;
833
Stephen Hemminger393deb92008-08-18 14:13:29 -0700834 vl_data = XCALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
paul718e3742002-12-13 20:15:29 +0000835
836 vl_data->vl_peer.s_addr = vl_peer.s_addr;
837 vl_data->vl_area_id = area->area_id;
838 vl_data->format = area->format;
839
840 return vl_data;
841}
842
843void
844ospf_vl_data_free (struct ospf_vl_data *vl_data)
845{
846 XFREE (MTYPE_OSPF_VL_DATA, vl_data);
847}
848
849u_int vlink_count = 0;
850
851struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000852ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000853{
854 struct ospf_interface * voi;
855 struct interface * vi;
856 char ifname[INTERFACE_NAMSIZ + 1];
857 struct ospf_area *area;
858 struct in_addr area_id;
859 struct connected *co;
860 struct prefix_ipv4 *p;
861
862 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000863 zlog_debug ("ospf_vl_new(): Start");
paul718e3742002-12-13 20:15:29 +0000864 if (vlink_count == OSPF_VL_MAX_COUNT)
865 {
866 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000867 zlog_debug ("ospf_vl_new(): Alarm: "
paul718e3742002-12-13 20:15:29 +0000868 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
869 return NULL;
870 }
871
872 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000873 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
paul718e3742002-12-13 20:15:29 +0000874
ajsa3491982005-04-02 22:50:38 +0000875 snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count);
876 vi = if_create (ifname, strnlen(ifname, sizeof(ifname)));
paul718e3742002-12-13 20:15:29 +0000877 co = connected_new ();
878 co->ifp = vi;
879 listnode_add (vi->connected, co);
880
881 p = prefix_ipv4_new ();
882 p->family = AF_INET;
883 p->prefix.s_addr = 0;
884 p->prefixlen = 0;
885
886 co->address = (struct prefix *)p;
887
paul68980082003-03-25 05:07:42 +0000888 voi = ospf_if_new (ospf, vi, co->address);
paul718e3742002-12-13 20:15:29 +0000889 if (voi == NULL)
890 {
891 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000892 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
paul718e3742002-12-13 20:15:29 +0000893 return NULL;
894 }
895 voi->connected = co;
896 voi->vl_data = vl_data;
897 voi->ifp->mtu = OSPF_VL_MTU;
898 voi->type = OSPF_IFTYPE_VIRTUALLINK;
899
paul106d2fd2003-08-01 00:24:13 +0000900 vlink_count++;
paul718e3742002-12-13 20:15:29 +0000901 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000902 zlog_debug ("ospf_vl_new(): Created name: %s", ifname);
paul718e3742002-12-13 20:15:29 +0000903 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000904 zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name);
paul718e3742002-12-13 20:15:29 +0000905
906 area_id.s_addr = 0;
paul68980082003-03-25 05:07:42 +0000907 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000908 voi->area = area;
909
910 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000911 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
paul718e3742002-12-13 20:15:29 +0000912
Paul Jakma478aab92006-04-03 21:25:32 +0000913 ospf_nbr_add_self (voi);
paul718e3742002-12-13 20:15:29 +0000914 ospf_area_add_if (voi->area, voi);
915
916 ospf_if_stream_set (voi);
917
918 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +0000919 zlog_debug ("ospf_vl_new(): Stop");
paul718e3742002-12-13 20:15:29 +0000920 return voi;
921}
922
paul4dadc292005-05-06 21:37:42 +0000923static void
paul718e3742002-12-13 20:15:29 +0000924ospf_vl_if_delete (struct ospf_vl_data *vl_data)
925{
926 struct interface *ifp = vl_data->vl_oi->ifp;
927 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
928 vl_data->vl_oi->address->prefixlen = 0;
929 ospf_if_free (vl_data->vl_oi);
930 if_delete (ifp);
931 vlink_count--;
932}
933
Paul Jakma9c27ef92006-05-04 07:32:57 +0000934/* Look up vl_data for given peer, optionally qualified to be in the
935 * specified area. NULL area returns first found..
936 */
paul718e3742002-12-13 20:15:29 +0000937struct ospf_vl_data *
Paul Jakma9c27ef92006-05-04 07:32:57 +0000938ospf_vl_lookup (struct ospf *ospf, struct ospf_area *area,
939 struct in_addr vl_peer)
paul718e3742002-12-13 20:15:29 +0000940{
941 struct ospf_vl_data *vl_data;
hasso52dc7ee2004-09-23 19:18:23 +0000942 struct listnode *node;
Paul Jakma9c27ef92006-05-04 07:32:57 +0000943
944 if (IS_DEBUG_OSPF_EVENT)
945 {
946 zlog_debug ("%s: Looking for %s", __func__, inet_ntoa (vl_peer));
947 if (area)
948 zlog_debug ("%s: in area %s", __func__, inet_ntoa (area->area_id));
949 }
950
951 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
952 {
953 if (IS_DEBUG_OSPF_EVENT)
954 zlog_debug ("%s: VL %s, peer %s", __func__,
955 vl_data->vl_oi->ifp->name,
956 inet_ntoa (vl_data->vl_peer));
957
958 if (area && !IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
959 continue;
960
961 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &vl_peer))
962 return vl_data;
963 }
paul718e3742002-12-13 20:15:29 +0000964
965 return NULL;
966}
967
paul4dadc292005-05-06 21:37:42 +0000968static void
paul718e3742002-12-13 20:15:29 +0000969ospf_vl_shutdown (struct ospf_vl_data *vl_data)
970{
971 struct ospf_interface *oi;
972
973 if ((oi = vl_data->vl_oi) == NULL)
974 return;
975
976 oi->address->u.prefix4.s_addr = 0;
977 oi->address->prefixlen = 0;
978
979 UNSET_FLAG (oi->ifp->flags, IFF_UP);
980 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
981 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
982}
983
984void
paul68980082003-03-25 05:07:42 +0000985ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000986{
paul68980082003-03-25 05:07:42 +0000987 listnode_add (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +0000988#ifdef HAVE_SNMP
989 ospf_snmp_vl_add (vl_data);
990#endif /* HAVE_SNMP */
991}
992
993void
paul68980082003-03-25 05:07:42 +0000994ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000995{
996 ospf_vl_shutdown (vl_data);
997 ospf_vl_if_delete (vl_data);
998
999#ifdef HAVE_SNMP
1000 ospf_snmp_vl_delete (vl_data);
1001#endif /* HAVE_SNMP */
paul68980082003-03-25 05:07:42 +00001002 listnode_delete (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +00001003
1004 ospf_vl_data_free (vl_data);
1005}
1006
paul4dadc292005-05-06 21:37:42 +00001007static int
paul718e3742002-12-13 20:15:29 +00001008ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
1009{
1010 int changed = 0;
1011 struct ospf_interface *voi;
hasso52dc7ee2004-09-23 19:18:23 +00001012 struct listnode *node;
pauleb3da6d2005-10-18 04:20:33 +00001013 struct vertex_parent *vp = NULL;
paul718e3742002-12-13 20:15:29 +00001014 int i;
1015 struct router_lsa *rl;
1016
1017 voi = vl_data->vl_oi;
1018
1019 if (voi->output_cost != v->distance)
1020 {
paulcd59da62004-05-05 17:26:55 +00001021
paul718e3742002-12-13 20:15:29 +00001022 voi->output_cost = v->distance;
1023 changed = 1;
1024 }
1025
pauleb3da6d2005-10-18 04:20:33 +00001026 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
paul1eb8ef22005-04-07 07:30:20 +00001027 {
Paul Jakma9c27ef92006-05-04 07:32:57 +00001028 vl_data->nexthop.oi = vp->nexthop->oi;
1029 vl_data->nexthop.router = vp->nexthop->router;
paul1eb8ef22005-04-07 07:30:20 +00001030
1031 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
Paul Jakma9c27ef92006-05-04 07:32:57 +00001032 &vl_data->nexthop.oi->address->u.prefix4))
paul1eb8ef22005-04-07 07:30:20 +00001033 changed = 1;
paulcd59da62004-05-05 17:26:55 +00001034
Paul Jakma9c27ef92006-05-04 07:32:57 +00001035 voi->address->u.prefix4 = vl_data->nexthop.oi->address->u.prefix4;
1036 voi->address->prefixlen = vl_data->nexthop.oi->address->prefixlen;
paul718e3742002-12-13 20:15:29 +00001037
paul1eb8ef22005-04-07 07:30:20 +00001038 break; /* We take the first interface. */
1039 }
paul718e3742002-12-13 20:15:29 +00001040
1041 rl = (struct router_lsa *)v->lsa;
pauld355bfa2004-04-08 07:43:45 +00001042
1043 /* use SPF determined backlink index in struct vertex
1044 * for virtual link destination address
1045 */
pauleb3da6d2005-10-18 04:20:33 +00001046 if (vp && vp->backlink >= 0)
paul718e3742002-12-13 20:15:29 +00001047 {
pauld355bfa2004-04-08 07:43:45 +00001048 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
pauleb3da6d2005-10-18 04:20:33 +00001049 &rl->link[vp->backlink].link_data))
pauld355bfa2004-04-08 07:43:45 +00001050 changed = 1;
pauleb3da6d2005-10-18 04:20:33 +00001051 vl_data->peer_addr = rl->link[vp->backlink].link_data;
pauld355bfa2004-04-08 07:43:45 +00001052 }
1053 else
1054 {
1055 /* This is highly odd, there is no backlink index
1056 * there should be due to the ospf_spf_has_link() check
1057 * in SPF. Lets warn and try pick a link anyway.
1058 */
1059 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1060 vl_data->vl_oi->ifp->name);
1061 for (i = 0; i < ntohs (rl->links); i++)
paul2e6b0bb2003-06-22 08:17:12 +00001062 {
pauld355bfa2004-04-08 07:43:45 +00001063 switch (rl->link[i].type)
1064 {
1065 case LSA_LINK_TYPE_VIRTUALLINK:
1066 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001067 zlog_debug ("found back link through VL");
pauld355bfa2004-04-08 07:43:45 +00001068 case LSA_LINK_TYPE_TRANSIT:
1069 case LSA_LINK_TYPE_POINTOPOINT:
paulcd59da62004-05-05 17:26:55 +00001070 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
1071 &rl->link[i].link_data))
1072 changed = 1;
pauld355bfa2004-04-08 07:43:45 +00001073 vl_data->peer_addr = rl->link[i].link_data;
pauld355bfa2004-04-08 07:43:45 +00001074 }
paul2e6b0bb2003-06-22 08:17:12 +00001075 }
paul718e3742002-12-13 20:15:29 +00001076 }
pauld355bfa2004-04-08 07:43:45 +00001077
1078 if (IS_DEBUG_OSPF_EVENT)
Paul Jakma9c27ef92006-05-04 07:32:57 +00001079 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__,
pauld355bfa2004-04-08 07:43:45 +00001080 vl_data->vl_oi->ifp->name,
Paul Jakma9c27ef92006-05-04 07:32:57 +00001081 inet_ntoa(vl_data->peer_addr),
1082 voi->output_cost,
1083 (changed ? " " : " un"));
pauld355bfa2004-04-08 07:43:45 +00001084
paul2e6b0bb2003-06-22 08:17:12 +00001085 return changed;
paul718e3742002-12-13 20:15:29 +00001086}
1087
1088
1089void
paul68980082003-03-25 05:07:42 +00001090ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
paul718e3742002-12-13 20:15:29 +00001091 struct vertex *v)
1092{
paul68980082003-03-25 05:07:42 +00001093 struct ospf *ospf = area->ospf;
hasso52dc7ee2004-09-23 19:18:23 +00001094 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001095 struct ospf_vl_data *vl_data;
1096 struct ospf_interface *oi;
1097
1098 if (IS_DEBUG_OSPF_EVENT)
1099 {
ajs60925302004-12-08 17:45:02 +00001100 zlog_debug ("ospf_vl_up_check(): Start");
1101 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid));
1102 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id));
paul718e3742002-12-13 20:15:29 +00001103 }
1104
paul1eb8ef22005-04-07 07:30:20 +00001105 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
paul718e3742002-12-13 20:15:29 +00001106 {
paul718e3742002-12-13 20:15:29 +00001107 if (IS_DEBUG_OSPF_EVENT)
1108 {
Paul Jakma9c27ef92006-05-04 07:32:57 +00001109 zlog_debug ("%s: considering VL, %s in area %s", __func__,
1110 vl_data->vl_oi->ifp->name,
1111 inet_ntoa (vl_data->vl_area_id));
1112 zlog_debug ("%s: peer ID: %s", __func__,
paul718e3742002-12-13 20:15:29 +00001113 inet_ntoa (vl_data->vl_peer));
1114 }
1115
1116 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) &&
1117 IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1118 {
1119 oi = vl_data->vl_oi;
1120 SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1121
1122 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001123 zlog_debug ("ospf_vl_up_check(): this VL matched");
paul718e3742002-12-13 20:15:29 +00001124
1125 if (oi->state == ISM_Down)
1126 {
1127 if (IS_DEBUG_OSPF_EVENT)
ajs60925302004-12-08 17:45:02 +00001128 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
paul718e3742002-12-13 20:15:29 +00001129 SET_FLAG (oi->ifp->flags, IFF_UP);
paul2e6b0bb2003-06-22 08:17:12 +00001130 OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
paul718e3742002-12-13 20:15:29 +00001131 }
1132
paul2e6b0bb2003-06-22 08:17:12 +00001133 if (ospf_vl_set_params (vl_data, v))
1134 {
1135 if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
ajs60925302004-12-08 17:45:02 +00001136 zlog_debug ("ospf_vl_up_check: VL cost change,"
paulcd59da62004-05-05 17:26:55 +00001137 " scheduling router lsa refresh");
Paul Jakmac363d382010-01-24 22:42:13 +00001138 if (ospf->backbone)
1139 ospf_router_lsa_update_area (ospf->backbone);
paul2e6b0bb2003-06-22 08:17:12 +00001140 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
ajs60925302004-12-08 17:45:02 +00001141 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
paul2e6b0bb2003-06-22 08:17:12 +00001142 }
paul718e3742002-12-13 20:15:29 +00001143 }
1144 }
1145}
1146
1147void
paul68980082003-03-25 05:07:42 +00001148ospf_vl_unapprove (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001149{
hasso52dc7ee2004-09-23 19:18:23 +00001150 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001151 struct ospf_vl_data *vl_data;
1152
paul1eb8ef22005-04-07 07:30:20 +00001153 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
1154 UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
paul718e3742002-12-13 20:15:29 +00001155}
1156
1157void
paul68980082003-03-25 05:07:42 +00001158ospf_vl_shut_unapproved (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001159{
paul1eb8ef22005-04-07 07:30:20 +00001160 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001161 struct ospf_vl_data *vl_data;
1162
paul1eb8ef22005-04-07 07:30:20 +00001163 for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
1164 if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
1165 ospf_vl_shutdown (vl_data);
paul718e3742002-12-13 20:15:29 +00001166}
1167
1168int
1169ospf_full_virtual_nbrs (struct ospf_area *area)
1170{
1171 if (IS_DEBUG_OSPF_EVENT)
1172 {
ajs60925302004-12-08 17:45:02 +00001173 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
paul718e3742002-12-13 20:15:29 +00001174 inet_ntoa (area->area_id));
ajs60925302004-12-08 17:45:02 +00001175 zlog_debug ("there are %d of them", area->full_vls);
paul718e3742002-12-13 20:15:29 +00001176 }
1177
1178 return area->full_vls;
1179}
1180
1181int
1182ospf_vls_in_area (struct ospf_area *area)
1183{
hasso52dc7ee2004-09-23 19:18:23 +00001184 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001185 struct ospf_vl_data *vl_data;
1186 int c = 0;
1187
paul1eb8ef22005-04-07 07:30:20 +00001188 for (ALL_LIST_ELEMENTS_RO (area->ospf->vlinks, node, vl_data))
1189 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1190 c++;
paul718e3742002-12-13 20:15:29 +00001191
1192 return c;
1193}
1194
David Lamparter6b0655a2014-06-04 06:53:35 +02001195
paul718e3742002-12-13 20:15:29 +00001196struct crypt_key *
1197ospf_crypt_key_new ()
1198{
Stephen Hemminger393deb92008-08-18 14:13:29 -07001199 return XCALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
paul718e3742002-12-13 20:15:29 +00001200}
1201
1202void
hasso52dc7ee2004-09-23 19:18:23 +00001203ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
paul718e3742002-12-13 20:15:29 +00001204{
1205 listnode_add (crypt, ck);
1206}
1207
1208struct crypt_key *
hasso52dc7ee2004-09-23 19:18:23 +00001209ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
paul718e3742002-12-13 20:15:29 +00001210{
hasso52dc7ee2004-09-23 19:18:23 +00001211 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001212 struct crypt_key *ck;
1213
paul1eb8ef22005-04-07 07:30:20 +00001214 for (ALL_LIST_ELEMENTS_RO (auth_crypt, node, ck))
1215 if (ck->key_id == key_id)
1216 return ck;
paul718e3742002-12-13 20:15:29 +00001217
1218 return NULL;
1219}
1220
1221int
hasso52dc7ee2004-09-23 19:18:23 +00001222ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
paul718e3742002-12-13 20:15:29 +00001223{
paul1eb8ef22005-04-07 07:30:20 +00001224 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001225 struct crypt_key *ck;
1226
paul1eb8ef22005-04-07 07:30:20 +00001227 for (ALL_LIST_ELEMENTS (auth_crypt, node, nnode, ck))
paul718e3742002-12-13 20:15:29 +00001228 {
paul718e3742002-12-13 20:15:29 +00001229 if (ck->key_id == key_id)
1230 {
1231 listnode_delete (auth_crypt, ck);
vincentba682532005-09-29 13:52:57 +00001232 XFREE (MTYPE_OSPF_CRYPT_KEY, ck);
paul718e3742002-12-13 20:15:29 +00001233 return 1;
1234 }
1235 }
1236
1237 return 0;
1238}
1239
ajsbc18d612004-12-15 15:07:19 +00001240u_char
1241ospf_default_iftype(struct interface *ifp)
1242{
1243 if (if_is_pointopoint (ifp))
1244 return OSPF_IFTYPE_POINTOPOINT;
1245 else if (if_is_loopback (ifp))
1246 return OSPF_IFTYPE_LOOPBACK;
1247 else
1248 return OSPF_IFTYPE_BROADCAST;
1249}
1250
paul718e3742002-12-13 20:15:29 +00001251void
1252ospf_if_init ()
1253{
1254 /* Initialize Zebra interface data structure. */
paul020709f2003-04-04 02:44:16 +00001255 om->iflist = iflist;
paul718e3742002-12-13 20:15:29 +00001256 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1257 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
1258}