ospf.c 38.6 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF
 *
Ondřej Filip's avatar
Ondřej Filip committed
4
 *	(c) 1999--2004 Ondrej Filip <feela@network.cz>
5 6
 *	(c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
 *	(c) 2009--2014 CZ.NIC z.s.p.o.
7 8 9 10
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

Ondřej Filip's avatar
Ondřej Filip committed
11 12
/**
 * DOC: Open Shortest Path First (OSPF)
13
 *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * The OSPF protocol is quite complicated and its complex implemenation is split
 * to many files. In |ospf.c|, you will find mainly the interface for
 * communication with the core (e.g., reconfiguration hooks, shutdown and
 * initialisation and so on). File |iface.c| contains the interface state
 * machine and functions for allocation and deallocation of OSPF's interface
 * data structures. Source |neighbor.c| includes the neighbor state machine and
 * functions for election of Designated Router and Backup Designated router. In
 * |packet.c|, you will find various functions for sending and receiving generic
 * OSPF packets. There are also routines for authentication and checksumming.
 * In |hello.c|, there are routines for sending and receiving of hello packets
 * as well as functions for maintaining wait times and the inactivity timer.
 * Files |lsreq.c|, |lsack.c|, |dbdes.c| contain functions for sending and
 * receiving of link-state requests, link-state acknowledgements and database
 * descriptions respectively.  In |lsupd.c|, there are functions for sending and
 * receiving of link-state updates and also the flooding algorithm. Source
 * |topology.c| is a place where routines for searching LSAs in the link-state
 * database, adding and deleting them reside, there also are functions for
 * originating of various types of LSAs (router LSA, net LSA, external LSA).
 * File |rt.c| contains routines for calculating the routing table. |lsalib.c|
 * is a set of various functions for working with the LSAs (endianity
 * conversions, calculation of checksum etc.).
Ondřej Filip's avatar
Ondřej Filip committed
35
 *
36 37 38 39 40 41 42
 * One instance of the protocol is able to hold LSA databases for multiple OSPF
 * areas, to exchange routing information between multiple neighbors and to
 * calculate the routing tables. The core structure is &ospf_proto to which
 * multiple &ospf_area and &ospf_iface structures are connected. &ospf_proto is
 * also connected to &top_hash_graph which is a dynamic hashing structure that
 * describes the link-state database. It allows fast search, addition and
 * deletion. Each LSA is kept in two pieces: header and body. Both of them are
43
 * kept in the endianity of the CPU.
44
 *
45 46 47 48 49 50 51
 * In OSPFv2 specification, it is implied that there is one IP prefix for each
 * physical network/interface (unless it is an ptp link). But in modern systems,
 * there might be more independent IP prefixes associated with an interface.  To
 * handle this situation, we have one &ospf_iface for each active IP prefix
 * (instead for each active iface); This behaves like virtual interface for the
 * purpose of OSPF.  If we receive packet, we associate it with a proper virtual
 * interface mainly according to its source address.
Ondřej Zajíček's avatar
Ondřej Zajíček committed
52
 *
53 54 55 56 57 58 59 60
 * OSPF keeps one socket per &ospf_iface. This allows us (compared to one socket
 * approach) to evade problems with a limit of multicast groups per socket and
 * with sending multicast packets to appropriate interface in a portable way.
 * The socket is associated with underlying physical iface and should not
 * receive packets received on other ifaces (unfortunately, this is not true on
 * BSD). Generally, one packet can be received by more sockets (for example, if
 * there are more &ospf_iface on one physical iface), therefore we explicitly
 * filter received packets according to src/dst IP address and received iface.
Ondřej Zajíček's avatar
Ondřej Zajíček committed
61
 *
62 63 64 65
 * Vlinks are implemented using particularly degenerate form of &ospf_iface,
 * which has several exceptions: it does not have its iface or socket (it copies
 * these from 'parent' &ospf_iface) and it is present in iface list even when
 * down (it is not freed in ospf_iface_down()).
Ondřej Zajíček's avatar
Ondřej Zajíček committed
66
 *
67
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
68 69 70
 * (&ospf_proto->tick). It is responsible for aging and flushing of LSAs in the
 * database, updating topology information in LSAs and for routing table
 * calculation.
71
 *
72 73 74
 * To every &ospf_iface, we connect one or more &ospf_neighbor's -- a structure
 * containing many timers and queues for building adjacency and for exchange of
 * routing messages.
Ondřej Filip's avatar
Ondřej Filip committed
75
 *
76 77 78 79 80 81 82
 * BIRD's OSPF implementation respects RFC2328 in every detail, but some of
 * internal algorithms do differ. The RFC recommends making a snapshot of the
 * link-state database when a new adjacency is forming and sending the database
 * description packets based on the information in this snapshot. The database
 * can be quite large in some networks, so rather we walk through a &slist
 * structure which allows us to continue even if the actual LSA we were working
 * with is deleted. New LSAs are added at the tail of this &slist.
Ondřej Filip's avatar
Ondřej Filip committed
83
 *
84 85 86 87 88
 * We also do not keep a separate OSPF routing table, because the core helps us
 * by being able to recognize when a route is updated to an identical one and it
 * suppresses the update automatically. Due to this, we can flush all the routes
 * we have recalculated and also those we have deleted to the core's routing
 * table and the core will take care of the rest. This simplifies the process
89
 * and conserves memory.
90 91 92 93 94
 *
 * Supported standards:
 * - RFC 2328 - main OSPFv2 standard
 * - RFC 5340 - main OSPFv3 standard
 * - RFC 3101 - OSPFv2 NSSA areas
95
 * - RFC 5250 - OSPFv2 Opaque LSAs
96 97 98 99
 * - RFC 5709 - OSPFv2 HMAC-SHA Cryptographic Authentication
 * - RFC 5838 - OSPFv3 Support of Address Families
 * - RFC 6549 - OSPFv2 Multi-Instance Extensions
 * - RFC 6987 - OSPF Stub Router Advertisement
100
 * - RFC 7166 - OSPFv3 Authentication Trailer
101
 * - RFC 7770 - OSPF Router Information LSA
Ondřej Filip's avatar
Ondřej Filip committed
102 103
 */

104
#include <stdlib.h>
105 106
#include "ospf.h"

107
static int ospf_preexport(struct proto *P, rte **new, struct linpool *pool);
108
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
109
static void ospf_store_tmp_attrs(struct rte *rt);
110
static void ospf_reload_routes(struct channel *C);
111 112
static int ospf_rte_better(struct rte *new, struct rte *old);
static int ospf_rte_same(struct rte *new, struct rte *old);
113 114
static void ospf_disp(timer *timer);

Ondřej Filip's avatar
Ondřej Filip committed
115 116 117 118

static void
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
{
119 120 121
  struct ospf_proto *p = oa->po;
  struct area_net_config *anc;
  struct area_net *an;
Ondřej Filip's avatar
Ondřej Filip committed
122

123
  fib_init(&oa->net_fib,  p->p.pool, ospf_get_af(p),
124
	   sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
125
  fib_init(&oa->enet_fib, p->p.pool, ospf_get_af(p),
126
	   sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
Ondřej Filip's avatar
Ondřej Filip committed
127

128 129
  WALK_LIST(anc, ac->net_list)
  {
130
    an = fib_get(&oa->net_fib, &anc->prefix);
131 132
    an->hidden = anc->hidden;
  }
133

134 135
  WALK_LIST(anc, ac->enet_list)
  {
136
    an = fib_get(&oa->enet_fib, &anc->prefix);
137 138 139
    an->hidden = anc->hidden;
    an->tag = anc->tag;
  }
Ondřej Filip's avatar
Ondřej Filip committed
140
}
141

142 143 144 145 146 147 148 149 150 151
static inline uint
ospf_opts(struct ospf_proto *p)
{
  if (ospf_is_v2(p))
    return 0;

  return ((ospf_is_ip6(p) && !p->af_mc) ? OPT_V6 : 0) |
    (!p->stub_router ? OPT_R : 0) | (p->af_ext ? OPT_AF : 0);
}

152
static void
153
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
154 155 156 157 158
{
  struct ospf_area *oa;

  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);

159 160 161
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
  add_tail(&p->area_list, NODE oa);
  p->areano++;
162 163 164 165

  oa->ac = ac;
  oa->areaid = ac->areaid;
  oa->rt = NULL;
166
  oa->po = p;
167
  fib_init(&oa->rtr, p->p.pool, NET_IP4, sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
168 169 170
  add_area_nets(oa, ac);

  if (oa->areaid == 0)
171
    p->backbone = oa;
172

173
  oa->options = ac->type | ospf_opts(p);
174 175

  ospf_notify_rt_lsa(oa);
176
}
177

178 179 180 181
static void
ospf_flush_area(struct ospf_proto *p, u32 areaid)
{
  struct top_hash_entry *en;
182

183 184 185
  WALK_SLIST(en, p->lsal)
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
      ospf_flush_lsa(p, en);
186 187 188 189 190
}

static void
ospf_area_remove(struct ospf_area *oa)
{
191
  struct ospf_proto *p = oa->po;
192 193 194
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);

  /* We suppose that interfaces are already removed */
195
  ospf_flush_area(p, oa->areaid);
196

197 198
  fib_free(&oa->rtr);
  fib_free(&oa->net_fib);
199
  fib_free(&oa->enet_fib);
200

201 202 203
  if (oa->translator_timer)
    rfree(oa->translator_timer);

204
  p->areano--;
205 206 207 208 209 210
  rem_node(NODE oa);
  mb_free(oa);
}


struct ospf_area *
211
ospf_find_area(struct ospf_proto *p, u32 aid)
212 213
{
  struct ospf_area *oa;
214
  WALK_LIST(oa, p->area_list)
215 216 217 218 219 220
    if (((struct ospf_area *) oa)->areaid == aid)
      return oa;
  return NULL;
}

static struct ospf_iface *
221
ospf_find_vlink(struct ospf_proto *p, u32 voa, u32 vid)
222 223
{
  struct ospf_iface *ifa;
224
  WALK_LIST(ifa, p->iface_list)
225 226 227 228 229
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa->areaid == voa) && (ifa->vid == vid))
      return ifa;
  return NULL;
}

230
static int
231
ospf_start(struct proto *P)
232
{
233 234
  struct ospf_proto *p = (struct ospf_proto *) P;
  struct ospf_config *c = (struct ospf_config *) (P->cf);
235
  struct ospf_area_config *ac;
Ondřej Filip's avatar
Ondřej Filip committed
236

237 238
  p->router_id = proto_get_router_id(P->cf);
  p->ospf2 = c->ospf2;
239 240
  p->af_ext = c->af_ext;
  p->af_mc = c->af_mc;
241 242 243
  p->rfc1583 = c->rfc1583;
  p->stub_router = c->stub_router;
  p->merge_external = c->merge_external;
244
  p->instance_id = c->instance_id;
245 246 247
  p->asbr = c->asbr;
  p->ecmp = c->ecmp;
  p->tick = c->tick;
248 249
  p->disp_timer = tm_new_init(P->pool, ospf_disp, p, p->tick S, 0);
  tm_start(p->disp_timer, 100 MS);
250 251 252
  p->lsab_size = 256;
  p->lsab_used = 0;
  p->lsab = mb_alloc(P->pool, p->lsab_size);
253
  p->nhpool = lp_new(P->pool, 12*sizeof(struct nexthop));
254 255
  init_list(&(p->iface_list));
  init_list(&(p->area_list));
256
  fib_init(&p->rtf, P->pool, ospf_get_af(p), sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
257 258
  if (ospf_is_v3(p))
    idm_init(&p->idm, P->pool, 16);
259
  p->areano = 0;
260
  p->gr = ospf_top_new(p, P->pool);
261
  s_init_list(&(p->lsal));
262

263
  p->flood_event = ev_new_init(P->pool, ospf_flood_event, p);
264

265 266 267
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };

Ondřej Filip's avatar
Ondřej Filip committed
268
  WALK_LIST(ac, c->area_list)
269
    ospf_area_add(p, ac);
270

271
  if (c->abr)
272
    ospf_open_vlink_sk(p);
273

274 275 276
  /* Add all virtual links */
  struct ospf_iface_patt *ic;
  WALK_LIST(ic, c->vlink_list)
277
    ospf_iface_new_vlink(p, ic);
278

279 280 281 282
  return PS_UP;
}

static void
283
ospf_dump(struct proto *P)
284
{
285
  struct ospf_proto *p = (struct ospf_proto *) P;
Ondřej Filip's avatar
Ondřej Filip committed
286 287
  struct ospf_iface *ifa;
  struct ospf_neighbor *n;
288

289
  OSPF_TRACE(D_EVENTS, "Area number: %d", p->areano);
Ondřej Filip's avatar
Ondřej Filip committed
290

291
  WALK_LIST(ifa, p->iface_list)
Ondřej Filip's avatar
Ondřej Filip committed
292
  {
293
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->ifname);
Ondřej Filip's avatar
Ondřej Filip committed
294
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
295 296
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
Ondřej Filip's avatar
Ondřej Filip committed
297 298
    WALK_LIST(n, ifa->neigh_list)
    {
299
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
Ondřej Filip's avatar
Ondřej Filip committed
300 301
    }
  }
302

303
  /*
304
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
305
  ospf_top_dump(p->gr, p);
306
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
307
  */
308
  neigh_dump_all();
309 310 311
}

static struct proto *
312
ospf_init(struct proto_config *CF)
313
{
314 315 316 317
  struct ospf_config *cf = (struct ospf_config *) CF;
  struct proto *P = proto_new(CF);

  P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF));
318 319 320

  P->rt_notify = ospf_rt_notify;
  P->if_notify = ospf_if_notify;
321
  P->ifa_notify = cf->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3;
322
  P->preexport = ospf_preexport;
323 324 325 326 327 328 329
  P->reload_routes = ospf_reload_routes;
  P->make_tmp_attrs = ospf_make_tmp_attrs;
  P->store_tmp_attrs = ospf_store_tmp_attrs;
  P->rte_better = ospf_rte_better;
  P->rte_same = ospf_rte_same;

  return P;
330 331
}

Ondřej Filip's avatar
Ondřej Filip committed
332 333 334 335
/* If new is better return 1 */
static int
ospf_rte_better(struct rte *new, struct rte *old)
{
Ondřej Filip's avatar
Ondřej Filip committed
336 337
  if (new->u.ospf.metric1 == LSINFINITY)
    return 0;
Ondřej Filip's avatar
Ondřej Filip committed
338

Ondřej Filip's avatar
Ondřej Filip committed
339 340
  if(new->attrs->source < old->attrs->source) return 1;
  if(new->attrs->source > old->attrs->source) return 0;
Ondřej Filip's avatar
Ondřej Filip committed
341

Ondřej Filip's avatar
Ondřej Filip committed
342
  if(new->attrs->source == RTS_OSPF_EXT2)
Ondřej Filip's avatar
Ondřej Filip committed
343
  {
Ondřej Filip's avatar
Ondřej Filip committed
344 345
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
Ondřej Filip's avatar
Ondřej Filip committed
346
  }
Ondřej Filip's avatar
Ondřej Filip committed
347

Ondřej Filip's avatar
Ondřej Filip committed
348 349
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
350

Ondřej Filip's avatar
Ondřej Filip committed
351
  return 0;			/* Old is shorter or same */
Ondřej Filip's avatar
Ondřej Filip committed
352 353
}

Ondřej Filip's avatar
Ondřej Filip committed
354 355 356
static int
ospf_rte_same(struct rte *new, struct rte *old)
{
357 358 359 360
  /* new->attrs == old->attrs always */
  return
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
361 362
    new->u.ospf.tag == old->u.ospf.tag &&
    new->u.ospf.router_id == old->u.ospf.router_id;
363
}
Ondřej Filip's avatar
Ondřej Filip committed
364

365
static ea_list *
Ondřej Filip's avatar
Ondřej Filip committed
366
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
367
		 u32 tag, u32 rid)
368
{
Ondřej Filip's avatar
Ondřej Filip committed
369
  struct ea_list *l =
370
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
371 372 373

  l->next = next;
  l->flags = EALF_SORTED;
374
  l->count = 4;
375 376 377 378 379 380 381 382 383 384 385 386
  l->attrs[0].id = EA_OSPF_METRIC1;
  l->attrs[0].flags = 0;
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
  l->attrs[0].u.data = m1;
  l->attrs[1].id = EA_OSPF_METRIC2;
  l->attrs[1].flags = 0;
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
  l->attrs[1].u.data = m2;
  l->attrs[2].id = EA_OSPF_TAG;
  l->attrs[2].flags = 0;
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
  l->attrs[2].u.data = tag;
387 388
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
  l->attrs[3].flags = 0;
389
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
390
  l->attrs[3].u.data = rid;
391
  return l;
Ondřej Filip's avatar
Ondřej Filip committed
392 393
}

394 395

void
396
ospf_schedule_rtcalc(struct ospf_proto *p)
397
{
398
  if (p->calcrt)
Ondřej Filip's avatar
Ondřej Filip committed
399
    return;
400

401
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
402
  p->calcrt = 1;
403 404
}

405 406
static void
ospf_reload_routes(struct channel *C)
407
{
408
  struct ospf_proto *p = (struct ospf_proto *) C->proto;
409

410 411
  if (p->calcrt == 2)
    return;
412

413
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
414
  p->calcrt = 2;
415 416
}

417

418
/**
419
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
420 421
 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
 * point to @ospf_proto
422
 */
423
static void
424 425
ospf_disp(timer * timer)
{
426
  struct ospf_proto *p = timer->data;
427

428 429
  /* Originate or flush local topology LSAs */
  ospf_update_topology(p);
430

431
  /* Process LSA DB */
432
  ospf_update_lsadb(p);
433

434
  /* Calculate routing table */
435 436
  if (p->calcrt)
    ospf_rt_spf(p);
437 438
}

439

Ondřej Filip's avatar
Ondřej Filip committed
440
/**
441
 * ospf_preexport - accept or reject new route from nest's routing table
442
 * @P: OSPF protocol instance
Martin Mareš's avatar
Martin Mareš committed
443
 * @new: the new route
444 445
 * @attrs: list of attributes
 * @pool: pool for allocation of attributes
Ondřej Filip's avatar
Ondřej Filip committed
446
 *
447 448
 * Its quite simple. It does not accept our own routes and leaves the decision on
 * import to the filters.
Ondřej Filip's avatar
Ondřej Filip committed
449
 */
450
static int
451
ospf_preexport(struct proto *P, rte **new, struct linpool *pool UNUSED)
452
{
453 454
  struct ospf_proto *p = (struct ospf_proto *) P;
  struct ospf_area *oa = ospf_main_area(p);
Ondřej Filip's avatar
Ondřej Filip committed
455
  rte *e = *new;
456

457
  /* Reject our own routes */
458
  if (e->attrs->src->proto == P)
459
    return -1;
460

461
  /* Do not export routes to stub areas */
462
  if (oa_is_stub(oa))
463
    return -1;
464

465
  return 0;
466
}
467

468
static struct ea_list *
469 470
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
{
Ondřej Filip's avatar
Ondřej Filip committed
471
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
472
			  rt->u.ospf.tag, rt->u.ospf.router_id);
473 474
}

475
static void
476
ospf_store_tmp_attrs(struct rte *rt)
477
{
478 479 480 481
  rt->u.ospf.metric1 = ea_get_int(rt->attrs->eattrs, EA_OSPF_METRIC1, LSINFINITY);
  rt->u.ospf.metric2 = ea_get_int(rt->attrs->eattrs, EA_OSPF_METRIC2, 10000);
  rt->u.ospf.tag = ea_get_int(rt->attrs->eattrs, EA_OSPF_TAG, 0);
  rt->u.ospf.router_id = ea_get_int(rt->attrs->eattrs, EA_OSPF_ROUTER_ID, 0);
482 483
}

Ondřej Filip's avatar
Ondřej Filip committed
484
/**
485
 * ospf_shutdown - Finish of OSPF instance
486
 * @P: OSPF protocol instance
Ondřej Filip's avatar
Ondřej Filip committed
487
 *
488
 * RFC does not define any action that should be taken before router
Ondřej Filip's avatar
Ondřej Filip committed
489 490
 * shutdown. To make my neighbors react as fast as possible, I send
 * them hello packet with empty neighbor list. They should start
491
 * their neighbor state machine with event %NEIGHBOR_1WAY.
Ondřej Filip's avatar
Ondřej Filip committed
492
 */
493
static int
494
ospf_shutdown(struct proto *P)
495
{
496
  struct ospf_proto *p = (struct ospf_proto *) P;
497
  struct ospf_iface *ifa;
498

Ondřej Filip's avatar
Ondřej Filip committed
499
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
500 501

  /* And send to all my neighbors 1WAY */
502
  WALK_LIST(ifa, p->iface_list)
503
    ospf_iface_shutdown(ifa);
Ondřej Filip's avatar
Ondřej Filip committed
504

505
  /* Cleanup locked rta entries */
506
  FIB_WALK(&p->rtf, ort, nf)
507
  {
508
    rta_free(nf->old_rta);
509 510 511
  }
  FIB_WALK_END;

512 513 514
  return PS_DOWN;
}

515
static void
516
ospf_get_status(struct proto *P, byte * buf)
517
{
518
  struct ospf_proto *p = (struct ospf_proto *) P;
519

520
  if (p->p.proto_state == PS_DOWN)
Ondřej Filip's avatar
Ondřej Filip committed
521
    buf[0] = 0;
522 523 524 525
  else
  {
    struct ospf_iface *ifa;
    struct ospf_neighbor *n;
Ondřej Filip's avatar
Ondřej Filip committed
526
    int adj = 0;
527

528
    WALK_LIST(ifa, p->iface_list)
Ondřej Filip's avatar
Ondřej Filip committed
529 530
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
      adj = 1;
531

Ondřej Filip's avatar
Ondřej Filip committed
532 533 534 535
    if (adj == 0)
      strcpy(buf, "Alone");
    else
      strcpy(buf, "Running");
536 537 538
  }
}

Ondřej Filip's avatar
Ondřej Filip committed
539
static void
540
ospf_get_route_info(rte * rte, byte * buf)
Ondřej Filip's avatar
Ondřej Filip committed
541
{
Ondřej Filip's avatar
Ondřej Filip committed
542
  char *type = "<bug>";
Ondřej Filip's avatar
Ondřej Filip committed
543

544
  switch (rte->attrs->source)
Ondřej Filip's avatar
Ondřej Filip committed
545
  {
546 547 548 549 550 551 552 553 554 555 556 557
  case RTS_OSPF:
    type = "I";
    break;
  case RTS_OSPF_IA:
    type = "IA";
    break;
  case RTS_OSPF_EXT1:
    type = "E1";
    break;
  case RTS_OSPF_EXT2:
    type = "E2";
    break;
Ondřej Filip's avatar
Ondřej Filip committed
558
  }
Ondřej Filip's avatar
Ondřej Filip committed
559 560

  buf += bsprintf(buf, " %s", type);
Ondřej Filip's avatar
Ondřej Filip committed
561
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
Ondřej Filip's avatar
Ondřej Filip committed
562
  if (rte->attrs->source == RTS_OSPF_EXT2)
Ondřej Filip's avatar
Ondřej Filip committed
563
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
Ondřej Filip's avatar
Ondřej Filip committed
564
  buf += bsprintf(buf, ")");
565
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
566
  {
567
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
568
  }
569 570
  if (rte->u.ospf.router_id)
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
Ondřej Filip's avatar
Ondřej Filip committed
571 572
}

573
static int
574
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
575 576
{
  switch (a->id)
Ondřej Filip's avatar
Ondřej Filip committed
577 578 579 580 581 582 583 584
  {
  case EA_OSPF_METRIC1:
    bsprintf(buf, "metric1");
    return GA_NAME;
  case EA_OSPF_METRIC2:
    bsprintf(buf, "metric2");
    return GA_NAME;
  case EA_OSPF_TAG:
585
    bsprintf(buf, "tag: 0x%08x", a->u.data);
Ondřej Filip's avatar
Ondřej Filip committed
586
    return GA_FULL;
587 588 589
  case EA_OSPF_ROUTER_ID:
    bsprintf(buf, "router_id");
    return GA_NAME;
Ondřej Filip's avatar
Ondřej Filip committed
590 591 592
  default:
    return GA_UNKNOWN;
  }
593
}
Ondřej Filip's avatar
Ondřej Filip committed
594

595 596
static void
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
Ondřej Filip's avatar
Ondřej Filip committed
597
{
598 599 600 601
  struct ospf_proto *p = oa->po;
  struct ospf_area_config *oac = oa->ac;
  struct ospf_iface *ifa;

602
  oa->ac = nac;
603
  oa->options = nac->type | ospf_opts(p);
604

605 606 607 608 609 610 611
  if (nac->type != oac->type)
  {
    /* Force restart of area interfaces */
    WALK_LIST(ifa, p->iface_list)
      if (ifa->oa == oa)
	ifa->marked = 2;
  }
612 613 614

  /* Handle net_list */
  fib_free(&oa->net_fib);
615
  fib_free(&oa->enet_fib);
616 617 618 619 620
  add_area_nets(oa, nac);

  /* No need to handle stubnet_list */

  oa->marked = 0;
621
  ospf_notify_rt_lsa(oa);
Ondřej Filip's avatar
Ondřej Filip committed
622
}
623

Ondřej Filip's avatar
Ondřej Filip committed
624 625
/**
 * ospf_reconfigure - reconfiguration hook
626
 * @P: current instance of protocol (with old configuration)
Ondřej Filip's avatar
Ondřej Filip committed
627 628
 * @c: new configuration requested by user
 *
629
 * This hook tries to be a little bit intelligent. Instance of OSPF
Ondřej Filip's avatar
Ondřej Filip committed
630
 * will survive change of many constants like hello interval,
631
 * password change, addition or deletion of some neighbor on
Ondřej Filip's avatar
Ondřej Filip committed
632 633
 * nonbroadcast network, cost of interface, etc.
 */
Ondřej Filip's avatar
Ondřej Filip committed
634
static int
635
ospf_reconfigure(struct proto *P, struct proto_config *CF)
Ondřej Filip's avatar
Ondřej Filip committed
636
{
637 638
  struct ospf_proto *p = (struct ospf_proto *) P;
  struct ospf_config *old = (struct ospf_config *) (P->cf);
639
  struct ospf_config *new = (struct ospf_config *) CF;
640 641 642 643 644
  struct ospf_area_config *nac;
  struct ospf_area *oa, *oax;
  struct ospf_iface *ifa, *ifx;
  struct ospf_iface_patt *ip;

645
  if (proto_get_router_id(CF) != p->router_id)
646 647
    return 0;

648 649 650
  if (p->ospf2 != new->ospf2)
    return 0;

651
  if (p->rfc1583 != new->rfc1583)
652 653
    return 0;

654 655 656
  if (p->instance_id != new->instance_id)
    return 0;

657 658
  if (old->abr != new->abr)
    return 0;
Ondřej Filip's avatar
Ondřej Filip committed
659

660 661 662
  if ((p->af_ext != new->af_ext) || (p->af_mc != new->af_mc))
    return 0;

663 664 665
  if (!proto_configure_channel(P, &P->main_channel, proto_cf_main_channel(CF)))
    return 0;

666 667 668 669 670
  p->stub_router = new->stub_router;
  p->merge_external = new->merge_external;
  p->asbr = new->asbr;
  p->ecmp = new->ecmp;
  p->tick = new->tick;
671
  p->disp_timer->recurrent = p->tick S;
672
  tm_start(p->disp_timer, 100 MS);
673

674
  /* Mark all areas and ifaces */
675
  WALK_LIST(oa, p->area_list)
676
    oa->marked = 1;
Ondřej Filip's avatar
Ondřej Filip committed
677

678
  WALK_LIST(ifa, p->iface_list)
679
    ifa->marked = 1;
Ondřej Filip's avatar
Ondřej Filip committed
680

681 682
  /* Add and update areas */
  WALK_LIST(nac, new->area_list)
Ondřej Filip's avatar
Ondřej Filip committed
683
  {
684
    oa = ospf_find_area(p, nac->areaid);
685 686 687
    if (oa)
      ospf_area_reconfigure(oa, nac);
    else
688
      ospf_area_add(p, nac);
689
  }
690

691 692 693
  /* Add and update interfaces */
  ospf_reconfigure_ifaces(p);

694 695 696
  /* Add and update vlinks */
  WALK_LIST(ip, new->vlink_list)
  {
697
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
698 699 700
    if (ifa)
      ospf_iface_reconfigure(ifa, ip);
    else
701
      ospf_iface_new_vlink(p, ip);
702
  }
Ondřej Filip's avatar
Ondřej Filip committed
703

704
  /* Delete remaining ifaces and areas */
705
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
706
    if (ifa->marked)
Ondřej Filip's avatar
Ondřej Filip committed
707
    {
708 709
      ospf_iface_shutdown(ifa);
      ospf_iface_remove(ifa);
Ondřej Filip's avatar
Ondřej Filip committed
710 711
    }

712
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
713 714
    if (oa->marked)
      ospf_area_remove(oa);
Ondřej Filip's avatar
Ondřej Filip committed
715

716
  ospf_schedule_rtcalc(p);
717

718
  return 1;
Ondřej Filip's avatar
Ondřej Filip committed
719
}
720

721

722
void
723
ospf_sh_neigh(struct proto *P, char *iff)
724
{
725
  struct ospf_proto *p = (struct ospf_proto *) P;
726
  struct ospf_iface *ifa = NULL;
727
  struct ospf_neighbor *n;
728

729
  if (p->p.proto_state != PS_UP)
730
  {
731
    cli_msg(-1013, "%s: is not up", p->p.name);
Ondřej Filip's avatar
Ondřej Filip committed
732
    cli_msg(0, "");
733 734
    return;
  }
Ondřej Filip's avatar
Ondřej Filip committed
735

736
  cli_msg(-1013, "%s:", p->p.name);
737 738
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
	  "     State", "DTime", "Interface", "Router IP");
739
  WALK_LIST(ifa, p->iface_list)
740
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
741 742
      WALK_LIST(n, ifa->neigh_list)
	ospf_sh_neigh_info(n);
Ondřej Filip's avatar
Ondřej Filip committed
743
  cli_msg(0, "");
744
}
Ondřej Filip's avatar
Ondřej Filip committed
745 746

void
747
ospf_sh(struct proto *P)
Ondřej Filip's avatar
Ondřej Filip committed
748
{
749
  struct ospf_proto *p = (struct ospf_proto *) P;
Ondřej Filip's avatar
Ondřej Filip committed
750 751 752
  struct ospf_area *oa;
  struct ospf_iface *ifa;
  struct ospf_neighbor *n;
Ondřej Filip's avatar
Ondřej Filip committed
753
  int ifano, nno, adjno, firstfib;
Ondřej Filip's avatar
Ondřej Filip committed
754

755
  if (p->p.proto_state != PS_UP)
756
  {
757
    cli_msg(-1014, "%s: is not up", p->p.name);
Ondřej Filip's avatar
Ondřej Filip committed
758
    cli_msg(0, "");
759 760 761
    return;
  }

762
  cli_msg(-1014, "%s:", p->p.name);
763
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enabled" : "disabled"));
764 765 766 767
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
  cli_msg(-1014, "Number of areas: %u", p->areano);
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
Ondřej Filip's avatar
Ondřej Filip committed
768

769
  WALK_LIST(oa, p->area_list)
Ondřej Filip's avatar
Ondřej Filip committed
770
  {
771
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
Ondřej Filip's avatar
Ondřej Filip committed
772 773 774 775
	    oa->areaid == 0 ? "[BACKBONE]" : "");
    ifano = 0;
    nno = 0;
    adjno = 0;
776
    WALK_LIST(ifa, p->iface_list)
Ondřej Filip's avatar
Ondřej Filip committed
777
    {
Ondřej Filip's avatar
Ondřej Filip committed
778
      if (oa == ifa->oa)
Ondřej Filip's avatar
Ondřej Filip committed
779
      {
780
	ifano++;
781 782
	WALK_LIST(n, ifa->neigh_list)
	{
783 784 785
	  nno++;
	  if (n->state == NEIGHBOR_FULL)
	    adjno++;
786
	}
Ondřej Filip's avatar
Ondřej Filip committed
787 788
      }
    }
789 790 791

    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
792
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
793 794 795 796

    if (oa_is_nssa(oa))
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
	      oa->translate == TRANS_WAIT ? " (run down)" : "");
Ondřej Filip's avatar
Ondřej Filip committed
797 798 799
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
Ondřej Filip's avatar
Ondřej Filip committed
800 801

    firstfib = 1;
802
    FIB_WALK(&oa->net_fib, struct area_net, anet)
803
    {
Ondřej Filip's avatar
Ondřej Filip committed
804
      if(firstfib)
805
      {
806 807
	cli_msg(-1014, "\t\tArea networks:");
	firstfib = 0;
808
      }
809
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
Ondřej Filip's avatar
Ondřej Filip committed
810
		anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
811
    }
Ondřej Filip's avatar
Ondřej Filip committed
812
    FIB_WALK_END;
813 814

    firstfib = 1;
815
    FIB_WALK(&oa->enet_fib, struct area_net, anet)
816 817 818
    {
      if(firstfib)
      {
819 820
	cli_msg(-1014, "\t\tArea external networks:");
	firstfib = 0;
821
      }
822
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
823 824 825 826
		anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
    }
    FIB_WALK_END;

Ondřej Filip's avatar
Ondřej Filip committed
827
  }
Ondřej Filip's avatar
Ondřej Filip committed
828
  cli_msg(0, "");
Ondřej Filip's avatar
Ondřej Filip committed
829 830
}

831
void
832
ospf_sh_iface(struct proto *P, char *iff)
833
{
834
  struct ospf_proto *p = (struct ospf_proto *) P;
835
  struct ospf_iface *ifa = NULL;
836

837
  if (p->p.proto_state != PS_UP)
838
  {
839
    cli_msg(-1015, "%s: is not up", p->p.name);
Ondřej Filip's avatar
Ondřej Filip committed
840
    cli_msg(0, "");
841 842 843
    return;
  }

844 845
  cli_msg(-1015, "%s:", p->p.name);
  WALK_LIST(ifa, p->iface_list)
846
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
847
      ospf_iface_info(ifa);
Ondřej Filip's avatar
Ondřej Filip committed
848
  cli_msg(0, "");
849 850
}

851 852 853
/* lsa_compare_for_state() - Compare function for 'show ospf state'
 *
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
854 855 856 857
 * will be presented as network nodes and other LSAs together as router nodes)
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
 * according to originating router id (to get all LSA needed to represent one
 * router node together). Then, according to LSA type, ID and age.
858 859 860 861
 *
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
 * immediately after the referenced LSA. We will make faked LSA based on ref_
 * values
862
 */
863 864 865 866 867 868

static struct ospf_lsa_header *
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
			 struct ospf_lsa_prefix *px)
{
  dst->age = src->age;
869
  dst->type_raw = px->ref_type;
870 871 872 873 874 875 876
  dst->id = px->ref_id;
  dst->rt = px->ref_rt;
  dst->sn = src->sn;

  return dst;
}

877 878

static int lsa_compare_ospf3;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
879

880
static int
881
lsa_compare_for_state(const void *p1, const void *p2)
882
{
883 884
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
885 886
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
887 888 889
  struct ospf_lsa_header lsatmp1, lsatmp2;
  u16 lsa1_type = he1->lsa_type;
  u16 lsa2_type = he2->lsa_type;
890

891 892 893 894
  if (he1->domain < he2->domain)
    return -1;
  if (he1->domain > he2->domain)
    return 1;
895 896


897 898 899
  /* px1 or px2 assumes OSPFv3 */
  int px1 = (lsa1_type == LSA_T_PREFIX);
  int px2 = (lsa2_type == LSA_T_PREFIX);
900 901

  if (px1)
902
  {
903
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
904 905
    lsa1_type = lsa1->type_raw;	/* FIXME: handle unknown ref_type */
  }
906 907

  if (px2)
908
  {
909
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
910 911 912
    lsa2_type = lsa2->type_raw;
  }

913

914 915
  int nt1 = (lsa1_type == LSA_T_NET);
  int nt2 = (lsa2_type == LSA_T_NET);
916 917 918 919 920 921

  if (nt1 != nt2)
    return nt1 - nt2;

  if (nt1)
  {
922 923 924 925 926 927 928 929
    /* In OSPFv3, networks are named based on ID of DR */
    if (lsa_compare_ospf3)
    {
      if (lsa1->rt < lsa2->rt)
	return -1;
      if (lsa1->rt > lsa2->rt)
	return 1;
    }
930 931 932

    /* For OSPFv2, this is IP of the network,
       for OSPFv3, this is interface ID */
933 934 935 936
    if (lsa1->id < lsa2->id)
      return -1;
    if (lsa1->id > lsa2->id)
      return 1;
937 938 939 940 941

    if (px1 != px2)
      return px1 - px2;

    return lsa1->sn - lsa2->sn;
942
  }
943
  else
944
  {
945 946 947 948
    if (lsa1->rt < lsa2->rt)
      return -1;
    if (lsa1->rt > lsa2->rt)
      return 1;
949

950
    if (lsa1_type < lsa2_type)
951
      return -1;
952
    if (lsa1_type > lsa2_type)
953 954 955 956 957 958
      return 1;

    if (lsa1->id < lsa2->id)
      return -1;
    if (lsa1->id > lsa2->id)
      return 1;
959 960 961

    if (px1 != px2)
      return px1 - px2;
962

963 964
    return lsa1->sn - lsa2->sn;
  }
965
}
966

967 968 969 970 971 972 973 974
static int
ext_compare_for_state(const void *p1, const void *p2)
{
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
  struct ospf_lsa_header *lsa2 = &(he2->lsa);

975 976 977 978 979 980 981 982 983
  if (lsa1->rt < lsa2->rt)
    return -1;
  if (lsa1->rt > lsa2->rt)
    return 1;

  if (lsa1->id < lsa2->id)
    return -1;
  if (lsa1->id > lsa2->id)
    return 1;
984 985 986 987 988 989 990 991 992 993 994 995 996

  return lsa1->sn - lsa2->sn;
}

static inline void
show_lsa_distance(struct top_hash_entry *he)
{
  if (he->color == INSPF)
    cli_msg(-1016, "\t\tdistance %u", he->dist);
  else
    cli_msg(-1016, "\t\tunreachable");
}

997
static inline void
998
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
999
{
1000
  struct ospf_lsa_rt_walk rtl;
1001

1002 1003 1004
  cli_msg(-1016, "");
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
  show_lsa_distance(he);
1005

1006 1007 1008 1009
  lsa_walk_rt_init(p, he, &rtl);
  while (lsa_walk_rt(&rtl))
    if (rtl.type == LSART_VLNK)
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
1010

1011 1012 1013 1014
  lsa_walk_rt_init(p, he, &rtl);
  while (lsa_walk_rt(&rtl))
    if (rtl.type == LSART_PTP)
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1015

1016 1017 1018
  lsa_walk_rt_init(p, he, &rtl);
  while (lsa_walk_rt(&rtl))
    if (rtl.type == LSART_NET)
1019
    {
1020
      if (ospf_is_v2(p))
1021
      {
1022 1023
	/* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
	struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1024

1025
	if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1026 1027 1028 1029
	{
	  struct ospf_lsa_header *net_lsa = &(net_he->lsa);
	  struct ospf_lsa_net *net_ln = net_he->lsa_body;

1030
	  cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1031 1032 1033 1034 1035
		  ipa_from_u32(net_lsa->id & net_ln->optx),
		  u32_masklen(net_ln->optx), rtl.metric);
	}
	else
	  cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1036 1037
      }
      else
1038
	cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1039 1040
    }

1041 1042 1043 1044 1045 1046 1047 1048
  if (ospf_is_v2(p) && verbose)
  {
    lsa_walk_rt_init(p, he, &rtl);
    while (lsa_walk_rt(&rtl))
      if (rtl.type == LSART_STUB)
	cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
		ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
  }
1049 1050 1051
}

static inline void
1052
show_lsa_network(struct top_hash_entry *he, int ospf2)
1053 1054 1055 1056 1057
{
  struct ospf_lsa_header *lsa = &(he->lsa);
  struct ospf_lsa_net *ln = he->lsa_body;
  u32 i;

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
  if (ospf2)
  {
    cli_msg(-1016, "");
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
  }
  else
  {
    cli_msg(-1016, "");
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
  }
1069

1070 1071
  show_lsa_distance(he);

1072
  for (i = 0; i < lsa_net_count(lsa); i++)
1073
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1074 1075 1076
}

static inline void
1077
show_lsa_sum_net(struct top_hash_entry *he, int ospf2,