Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ e43ae633

History | View | Annotate | Download (24.1 KB)

1 c1f8dc91 Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 7ab3ff6a Ondrej Filip
 *        (c) 1999 - 2000 Ondrej Filip <feela@network.cz>
5 c1f8dc91 Ondrej Filip
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 89755a86 Ondrej Filip
/**
10
 * DOC: Open Shortest Path First (OSPF)
11
 * 
12 725270cb Martin Mares
 * The OSPF protocol is quite complicated and its complex implemenation is
13
 * split to many files. In |ospf.c|, you can find mostly interface
14
 * for communication with the core (e.g., reconfiguration hooks, shutdown
15
 * and initialisation and so on). In |packet.c|, you can find various
16
 * functions for sending and receiving of generic OSPF packets. There are
17
 * also routines for autentication and checksumming. File |iface.c| contains
18
 * the interface state machine, allocation and deallocation of OSPF's
19
 * interface data structures. Source |neighbor.c| includes the neighbor state
20
 * machine and functions for election of Designed Router and Backup
21
 * Designed router. In |hello.c|, there are routines for sending
22
 * and receiving of hello packets as well as functions for maintaining
23
 * wait times and the inactivity timer. Files |lsreq.c|, |lsack.c|, |dbdes.c|
24
 * contain functions for sending and receiving of link-state requests,
25
 * link-state acknoledges and database descriptions respectively.
26
 * In |lsupd.c|, there are functions for sending and receiving
27
 * of link-state updates and also the flooding algorithm. Source |topology.c| is
28
 * a place where routines for searching LSA's in the link-state database,
29
 * adding and deleting them reside, there also are functions for originating
30
 * of various types of LSA's (router LSA, net LSA, external LSA). File |rt.c|
31
 * contains routines for calculating the routing table. |lsalib.c| is a set
32
 * of various functions for working with the LSA's (endianity conversions,
33
 * calculation of checksum etc.).
34 89755a86 Ondrej Filip
 *
35 725270cb Martin Mares
 * One instance of the protocol is able to hold LSA databases for
36
 * multiple OSPF areas, to exchange routing information between
37
 * multiple neighbors and to calculate the routing tables. The core
38
 * structure is &proto_ospf to which multiple &ospf_area and
39
 * &ospf_iface structures are connected. To &ospf_area is also connected
40
 * &top_hash_graph which is a dynamic hashing structure that
41
 * describes the link-state database. It allows fast search, addition
42
 * and deletion. Each LSA is kept in two pieces: header and body. Both of them are
43
 * kept in endianity of the CPU.
44 37c3e558 Ondrej Filip
 * 
45 725270cb Martin Mares
 * Every area has its own area_disp() which is
46
 * responsible for late originating of router LSA, calculating
47
 * of the routing table and it also ages and flushes the LSA's. This
48 89755a86 Ondrej Filip
 * function is called in regular intervals.
49 725270cb Martin Mares
 * To every &ospf_iface, we connect one or more
50
 * &ospf_neighbor's -- a structure containing many timers and queues
51
 * for building adjacency and for exchange of routing messages.
52 89755a86 Ondrej Filip
 *
53 725270cb Martin Mares
 * BIRD's OSPF implementation respects RFC2328 in every detail, but
54
 * some of internal algorithms do differ. The RFC recommends to make a snapshot
55
 * of the link-state database when a new adjacency is forming and send
56
 * the database description packets based on information of this 
57
 * snapshot. The database can be quite large in some networks, so
58
 * we rather walk through a &slist structure which allows us to
59
 * continue even if the actual LSA we were worked with is deleted. New
60
 * LSA's are added at the tail of this &slist.
61 89755a86 Ondrej Filip
 *
62 725270cb Martin Mares
 * We also don't keep a separate OSPF routing table, because the core
63
 * helps us by being able to recognize when a route is updated
64
 * to an identical one and it suppresses the update automatically.
65
 * Due to this, we can flush all the routes we've recalculated and
66
 * also those we're deleted to the core's routing table and the
67
 * core will take care of the rest. This simplifies the process
68
 * and conserves memory.
69 89755a86 Ondrej Filip
 */
70
71 c1f8dc91 Ondrej Filip
#include "ospf.h"
72
73
static int
74
ospf_start(struct proto *p)
75
{
76 163b2073 Ondrej Filip
  struct proto_ospf *po=(struct proto_ospf *)p;
77 51cff78b Ondrej Filip
  struct ospf_config *c=(struct ospf_config *)(p->cf);
78
  struct ospf_area_config *ac;
79
  struct ospf_area *oa;
80 c926eee7 Ondrej Filip
  struct area_net *anet,*antmp;
81 f14032ef Ondrej Filip
82 aa1e082c Ondrej Filip
  fib_init(&po->efib,p->pool,sizeof(struct extfib),16,init_efib);
83 5970fcda Ondrej Filip
  init_list(&(po->iface_list));
84
  init_list(&(po->area_list));
85
  po->areano=0;
86 51cff78b Ondrej Filip
  if(EMPTY_LIST(c->area_list))
87
  {
88
    log("%s: Cannot start, no OSPF areas configured", p->name);
89
    return PS_DOWN;
90
  }
91 5b1a92e6 Ondrej Filip
92 51cff78b Ondrej Filip
  WALK_LIST(ac,c->area_list)
93
  {
94
    oa=mb_allocz(po->proto.pool, sizeof(struct ospf_area));
95 c926eee7 Ondrej Filip
    add_tail(&po->area_list, NODE oa);
96 51cff78b Ondrej Filip
    po->areano++;
97
    oa->stub=ac->stub;
98
    oa->tick=ac->tick;
99
    oa->areaid=ac->areaid;
100
    oa->gr=ospf_top_new(po);
101
    s_init_list(&(oa->lsal));
102
    oa->rt=NULL;
103
    oa->po=po;
104
    oa->disp_timer=tm_new(po->proto.pool);
105
    oa->disp_timer->data=oa;
106
    oa->disp_timer->randomize=0;
107
    oa->disp_timer->hook=area_disp;
108
    oa->disp_timer->recurrent=oa->tick;
109
    tm_start(oa->disp_timer,oa->tick);
110
    oa->calcrt=0;
111
    oa->origrt=0;
112 c926eee7 Ondrej Filip
    init_list(&oa->net_list);
113
    WALK_LIST(anet,ac->net_list)
114
    {
115
      antmp=mb_allocz(po->proto.pool, sizeof(struct area_net));
116
      antmp->net=anet->net;
117
      antmp->mlen=anet->mlen;
118
      antmp->hidden=anet->hidden;
119
      add_tail(&oa->net_list, NODE antmp);
120
    }
121 51cff78b Ondrej Filip
    fib_init(&oa->infib,po->proto.pool,sizeof(struct infib),16,init_infib);
122
  }
123 c1f8dc91 Ondrej Filip
  return PS_UP;
124
}
125
126
static void
127
ospf_dump(struct proto *p)
128
{
129
  char areastr[20];
130 b332fcdf Ondrej Filip
  struct ospf_iface *ifa;
131
  struct ospf_neighbor *n;
132 c1f8dc91 Ondrej Filip
  struct ospf_config *c = (void *) p->cf;
133 163b2073 Ondrej Filip
  struct proto_ospf *po=(struct proto_ospf *)p;
134 15087574 Ondrej Filip
  struct ospf_area *oa;
135 c1f8dc91 Ondrej Filip
136 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Area number: %d", po->areano);
137 b332fcdf Ondrej Filip
138 163b2073 Ondrej Filip
  WALK_LIST(ifa, po->iface_list)
139 b332fcdf Ondrej Filip
  {
140 f14032ef Ondrej Filip
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->iface->name);
141
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
142
    OSPF_TRACE(D_EVENTS, "DR:  %I", ifa->drid);
143
    OSPF_TRACE(D_EVENTS, "BDR: %I", ifa->bdrid);
144 b332fcdf Ondrej Filip
    WALK_LIST(n, ifa->neigh_list)
145
    {
146 ee4880c8 Ondrej Filip
      OSPF_TRACE(D_EVENTS, "  neighbor %I in state %u", n->rid, n->state);
147 b332fcdf Ondrej Filip
    }
148
  }
149 163b2073 Ondrej Filip
150 8496b2e4 Ondrej Filip
  WALK_LIST(NODE oa,po->area_list)
151 15087574 Ondrej Filip
  {
152 f14032ef Ondrej Filip
    OSPF_TRACE(D_EVENTS, "LSA graph dump for area \"%I\" start:", oa->areaid);
153 992705f6 Ondrej Filip
    ospf_top_dump(oa->gr,p);
154 f14032ef Ondrej Filip
    OSPF_TRACE(D_EVENTS, "LSA graph dump for area \"%I\" finished", oa->areaid);
155 15087574 Ondrej Filip
  }
156 c6c56264 Ondrej Filip
  neigh_dump_all();
157 c1f8dc91 Ondrej Filip
}
158
159
static struct proto *
160
ospf_init(struct proto_config *c)
161
{
162 4c630a6d Ondrej Filip
  struct proto *p = proto_new(c, sizeof(struct proto_ospf));
163 163b2073 Ondrej Filip
  struct proto_ospf *po=(struct proto_ospf *)p;
164 3fa5722d Ondrej Filip
  struct ospf_config *oc=(struct ospf_config *)c;
165 b36a0a79 Ondrej Filip
  struct ospf_area_config *ac;
166 89d6782d Ondrej Filip
  struct ospf_iface_patt *patt;
167 c1f8dc91 Ondrej Filip
168 2d5b9992 Ondrej Filip
  p->import_control = ospf_import_control;
169 5919c66e Martin Mares
  p->make_tmp_attrs = ospf_make_tmp_attrs;
170
  p->store_tmp_attrs = ospf_store_tmp_attrs;
171 e8085aba Ondrej Filip
  p->rt_notify = ospf_rt_notify;
172 04c3a83c Ondrej Filip
  p->if_notify = ospf_if_notify;
173 5919c66e Martin Mares
  p->rte_better = ospf_rte_better;
174
  p->rte_same = ospf_rte_same;
175 163b2073 Ondrej Filip
176 3fa5722d Ondrej Filip
  po->rfc1583=oc->rfc1583;
177 fdb19982 Ondrej Filip
  po->ebit=0;
178 c1f8dc91 Ondrej Filip
  return p;
179
}
180
181 bbd76b42 Ondrej Filip
/* If new is better return 1 */
182
static int
183
ospf_rte_better(struct rte *new, struct rte *old)
184
{
185
  struct proto *p = new->attrs->proto;
186
187 1eec7607 Ondrej Filip
  if(new->u.ospf.metric1==LSINFINITY) return 0;
188 bbd76b42 Ondrej Filip
189 d6a7e2fb Ondrej Filip
  /* External paths are always longer that internal */
190 bbd76b42 Ondrej Filip
  if(((new->attrs->source==RTS_OSPF) || (new->attrs->source==RTS_OSPF_IA))
191
    && (old->attrs->source==RTS_OSPF_EXT)) return 1;
192
  if(((old->attrs->source==RTS_OSPF) || (old->attrs->source==RTS_OSPF_IA))
193
    && (new->attrs->source==RTS_OSPF_EXT)) return 0;
194
195 d6a7e2fb Ondrej Filip
  if(new->u.ospf.metric2<old->u.ospf.metric2)
196 bbd76b42 Ondrej Filip
  {
197 d6a7e2fb Ondrej Filip
    if(old->u.ospf.metric2==LSINFINITY) return 0; /* Old is E1, new is E2 */
198
    return 1;        /* Both are E2 */
199 bbd76b42 Ondrej Filip
  }
200 d6a7e2fb Ondrej Filip
        
201
  if(new->u.ospf.metric2>old->u.ospf.metric2)
202 bbd76b42 Ondrej Filip
  {
203 d6a7e2fb Ondrej Filip
    if(new->u.ospf.metric2==LSINFINITY) return 1; /* New is E1, old is E2 */
204
    return 0;        /* Both are E2 */
205 bbd76b42 Ondrej Filip
  }
206 d6a7e2fb Ondrej Filip
207
  /* 
208
   * E2 metrics are the same. It means that:
209
   * 1) Paths are E2 with same metric
210
   * 2) Paths are E1.
211
   */
212
213
  if(new->u.ospf.metric1<old->u.ospf.metric1) return 1;
214
  return 0;        /* Old is shorter or same */
215 bbd76b42 Ondrej Filip
}
216
217 00c1f79a Ondrej Filip
static int
218
ospf_rte_same(struct rte *new, struct rte *old)
219
{
220 5919c66e Martin Mares
  /* new->attrs == old->attrs always */
221
  return
222
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
223
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
224
    new->u.ospf.tag     == old->u.ospf.tag;
225
}
226 00c1f79a Ondrej Filip
227 5919c66e Martin Mares
static ea_list *
228
ospf_build_attrs(ea_list *next, struct linpool *pool, u32 m1, u32 m2, u32 tag)
229
{
230
  struct ea_list *l = lp_alloc(pool, sizeof(struct ea_list) + 3*sizeof(eattr));
231
232
  l->next = next;
233
  l->flags = EALF_SORTED;
234
  l->count = 3;
235
  l->attrs[0].id = EA_OSPF_METRIC1;
236
  l->attrs[0].flags = 0;
237
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
238
  l->attrs[0].u.data = m1;
239
  l->attrs[1].id = EA_OSPF_METRIC2;
240
  l->attrs[1].flags = 0;
241
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
242
  l->attrs[1].u.data = m2;
243
  l->attrs[2].id = EA_OSPF_TAG;
244
  l->attrs[2].flags = 0;
245
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
246
  l->attrs[2].u.data = tag;
247
  return l;
248 00c1f79a Ondrej Filip
}
249
250 70a38319 Ondrej Filip
void
251
schedule_rt_lsa(struct ospf_area *oa)
252
{
253
  struct proto_ospf *po=oa->po;
254
  struct proto *p=&po->proto;
255
256 f14032ef Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Scheduling RT lsa origination for area %I.",
257 70a38319 Ondrej Filip
    oa->areaid);
258
  oa->origrt=1;
259
}
260
261
void
262
schedule_rtcalc(struct ospf_area *oa)
263
{
264
  struct proto_ospf *po=oa->po;
265
  struct proto *p=&po->proto;
266
267 f14032ef Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Scheduling RT calculation for area %I.", 
268 70a38319 Ondrej Filip
    oa->areaid);
269
  oa->calcrt=1;
270
}
271
272 6f58dc64 Ondrej Filip
/**
273
 * area_disp - invokes link-state database aging, originating of
274
 * router LSA and routing table calculation
275 6567e6cf Martin Mares
 * @timer: it's called every @ospf_area->tick seconds
276 6f58dc64 Ondrej Filip
 *
277
 * It ivokes aging and when @ospf_area->origrt is set to 1, start
278 78e2c6cc Ondrej Filip
 * function for origination of router LSA and network LSA's.
279
 * It also start routing
280 6f58dc64 Ondrej Filip
 * table calculation when @ospf_area->calcrt is set.
281
 */
282 70a38319 Ondrej Filip
void
283
area_disp(timer *timer)
284
{
285
  struct ospf_area *oa=timer->data;
286
  struct top_hash_entry *en,*nxt;
287 78e2c6cc Ondrej Filip
  struct proto_ospf *po=oa->po;
288
  struct ospf_iface *ifa;
289 70a38319 Ondrej Filip
290
  /* First of all try to age LSA DB */
291 d1660fd3 Ondrej Filip
  ospf_age(oa);
292 70a38319 Ondrej Filip
293
  /* Now try to originage rt_lsa */
294
  if(oa->origrt) originate_rt_lsa(oa);
295 78e2c6cc Ondrej Filip
296
  /* Now try to originate network LSA's */
297
  WALK_LIST(ifa, po->iface_list)
298
  {
299
    if(ifa->orignet&&(ifa->an==oa->areaid)) originate_net_lsa(ifa);
300
  }
301 70a38319 Ondrej Filip
302
  if(oa->calcrt) ospf_rt_spfa(oa);
303
  oa->calcrt=0;
304
}
305
306 6f58dc64 Ondrej Filip
/**
307
 * ospf_import_control - accept or reject new route from nest's routing table
308
 * @p: current instance of protocol
309 6567e6cf Martin Mares
 * @new: the new route
310 6f58dc64 Ondrej Filip
 * @attrs: list of arttributes
311
 * @pool: pool for alloction of attributes
312
 *
313
 * Its quite simple. It does not accept our own routes and decision of
314
 * import leaves to the filters.
315
 */
316
317 2d5b9992 Ondrej Filip
int
318
ospf_import_control(struct proto *p, rte **new, ea_list **attrs, struct linpool *pool)
319
{
320
  rte *e=*new;
321
  struct proto_ospf *po=(struct proto_ospf *)p;
322
323 5919c66e Martin Mares
  if(p==e->attrs->proto) return -1;        /* Reject our own routes */
324 8441f179 Martin Mares
  *attrs = ospf_build_attrs(*attrs, pool, LSINFINITY, 10000, 0);
325 5919c66e Martin Mares
  return 0;                                /* Leave decision to the filters */
326
}
327 2d5b9992 Ondrej Filip
328 5919c66e Martin Mares
struct ea_list *
329
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
330
{
331
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2, rt->u.ospf.tag);
332
}
333
334
void
335
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
336
{
337 8441f179 Martin Mares
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
338
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
339 5919c66e Martin Mares
  rt->u.ospf.tag     = ea_get_int(attrs, EA_OSPF_TAG,     0);
340 2d5b9992 Ondrej Filip
}
341
342 6f58dc64 Ondrej Filip
/**
343
 * ospf_shutdown - Finnish of OSPF instance
344
 * @p: current instance of protocol
345
 *
346
 * RFC does not define any action that should be taken befor router
347
 * shutdown. To make my neighbors react as fast as possible, I send
348
 * them hello packet with empty neighbor list. They should start
349
 * theirs neighbor state machine with event %NEIGHBOR_1WAY.
350
 */
351
352 4bd28fb6 Ondrej Filip
static int
353
ospf_shutdown(struct proto *p)
354
{
355
  struct proto_ospf *po=(struct proto_ospf *)p;
356
  struct ospf_iface *ifa;
357
  struct ospf_neighbor *n;
358
  struct ospf_area *oa;
359 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
360 4bd28fb6 Ondrej Filip
361
  /* And send to all my neighbors 1WAY */
362
  WALK_LIST(ifa, po->iface_list)
363
  {
364 1b128de3 Ondrej Filip
    init_list(&ifa->neigh_list);
365 4bd28fb6 Ondrej Filip
    hello_timer_hook(ifa->hello_timer);
366
  }
367
  
368
  return PS_DOWN;
369
}
370
371 e8085aba Ondrej Filip
void
372
ospf_rt_notify(struct proto *p, net *n, rte *new, rte *old, ea_list *attrs)
373
{
374
  struct proto_ospf *po=(struct proto_ospf *)p;
375
376 52fa3e38 Ondrej Filip
/* Temporarily down write anythink
377 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Got route %I/%d %s", p->name, n->n.prefix,
378 e8085aba Ondrej Filip
    n->n.pxlen, new ? "up" : "down");
379 52fa3e38 Ondrej Filip
*/
380 e8085aba Ondrej Filip
381
  if(new)                /* Got some new route */
382
  {
383 5919c66e Martin Mares
    originate_ext_lsa(n, new, po, attrs);
384 e8085aba Ondrej Filip
  }
385
  else
386
  {
387 4bd28fb6 Ondrej Filip
    u32 rtid=po->proto.cf->global->router_id;
388
    struct ospf_area *oa;
389
    struct top_hash_entry *en;
390 273fd2c1 Ondrej Filip
    u32 pr=ipa_to_u32(n->n.prefix);
391
    struct ospf_lsa_ext *ext;
392
    int i;
393 4bd28fb6 Ondrej Filip
394
    /* Flush old external LSA */
395
    WALK_LIST(oa, po->area_list)
396
    {
397 273fd2c1 Ondrej Filip
      for(i=0;i<MAXNETS;i++,pr++)
398
      {
399
        if(en=ospf_hash_find(oa->gr, pr, rtid, LSA_T_EXT))
400
        {
401
          ext=en->lsa_body;
402
          if(ipa_compare(ext->netmask, ipa_mkmask(n->n.pxlen))==0)
403
          {
404
            net_flush_lsa(en,po,oa);
405
            break;
406
          }
407
        }
408
      }
409 4bd28fb6 Ondrej Filip
    }
410 e8085aba Ondrej Filip
  }
411
}
412
413 4414d9a5 Ondrej Filip
static void
414
ospf_get_status(struct proto *p, byte *buf)
415
{
416
  struct proto_ospf *po=(struct proto_ospf *)p;
417
418
  if (p->proto_state == PS_DOWN) buf[0] = 0;
419
  else
420
  {
421
    struct ospf_iface *ifa;
422
    struct ospf_neighbor *n;
423
    int adj=0;
424
425
    WALK_LIST(ifa,po->iface_list)
426
      WALK_LIST(n,ifa->neigh_list)
427
        if(n->state==NEIGHBOR_FULL) adj=1;
428
429
    if(adj==0) strcpy(buf, "Alone");
430
    else strcpy(buf, "Running");
431
  }
432
}
433
434 f7c0525e Ondrej Filip
static void
435
ospf_get_route_info(rte *rte, byte *buf, ea_list *attrs)
436
{
437
  char met=' ';
438 05dbc97b Ondrej Filip
  char type=' ';
439 f7c0525e Ondrej Filip
440 05dbc97b Ondrej Filip
  if(rte->attrs->source==RTS_OSPF_EXT)
441
  {
442
    met='1';
443
    type='E';
444 a7a3a0a3 Ondrej Filip
445 05dbc97b Ondrej Filip
  }
446 a7a3a0a3 Ondrej Filip
  if(rte->u.ospf.metric2!=LSINFINITY) met='2';
447 05dbc97b Ondrej Filip
  if(rte->attrs->source==RTS_OSPF_IA) type='A';
448
  if(rte->attrs->source==RTS_OSPF) type='I';
449 73232f6b Ondrej Filip
  buf += bsprintf(buf, " %c", type);
450
  if(met!=' ') buf += bsprintf(buf, "%c", met);
451 d6a7e2fb Ondrej Filip
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
452
  if(rte->u.ospf.metric2!=LSINFINITY) buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
453
  buf += bsprintf(buf, ")");
454 23df5e4c Martin Mares
  if(rte->attrs->source==RTS_OSPF_EXT && rte->u.ospf.tag)
455 a7a3a0a3 Ondrej Filip
  {
456 23df5e4c Martin Mares
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
457 a7a3a0a3 Ondrej Filip
  }
458 f7c0525e Ondrej Filip
}
459
460 5919c66e Martin Mares
static int
461
ospf_get_attr(eattr *a, byte *buf)
462
{
463
  switch (a->id)
464
    {
465
    case EA_OSPF_METRIC1: bsprintf(buf, "metric1"); return GA_NAME;
466
    case EA_OSPF_METRIC2: bsprintf(buf, "metric2"); return GA_NAME;
467
    case EA_OSPF_TAG: bsprintf(buf, "tag: %08x", a->u.data); return GA_FULL;
468
    default: return GA_UNKNOWN;
469
    }
470
}
471 6f58dc64 Ondrej Filip
472 edc34dc9 Ondrej Filip
static int
473
ospf_patt_compare(struct ospf_iface_patt *a, struct ospf_iface_patt *b)
474
{
475 fa6c2405 Ondrej Filip
  return ((a->type==b->type)&&(a->priority==b->priority));
476 edc34dc9 Ondrej Filip
}
477 5919c66e Martin Mares
478 6f58dc64 Ondrej Filip
/**
479
 * ospf_reconfigure - reconfiguration hook
480
 * @p: current instance of protocol (with old configuration)
481
 * @c: new configuration requested by user
482
 *
483
 * This hook tries to be a little bit inteligent. Instance of OSPF
484
 * will survive change of many constants like hello interval,
485
 * password change, addition of deletion of some neighbor on
486
 * nonbroadcast network, cost of interface, etc.
487
 */
488 80787d41 Ondrej Filip
static int
489
ospf_reconfigure(struct proto *p, struct proto_config *c)
490
{
491 edc34dc9 Ondrej Filip
  struct ospf_config *old=(struct ospf_config *)(p->cf);
492
  struct ospf_config *new=(struct ospf_config *)c;
493
  struct ospf_area_config *ac1,*ac2;
494
  struct proto_ospf *po=( struct proto_ospf *)p;
495
  struct ospf_iface_patt *ip1,*ip2;
496
  struct ospf_iface *ifa;
497
  struct nbma_node *nb1,*nb2,*nbnx;
498 c926eee7 Ondrej Filip
  struct ospf_area *oa=NULL;
499
  struct area_net *anet,*antmp;
500 edc34dc9 Ondrej Filip
  int found;
501
502 6f58dc64 Ondrej Filip
  po->rfc1583=new->rfc1583;
503
  WALK_LIST(oa, po->area_list)        /* Routing table must be recalculated */
504
  {
505
    schedule_rtcalc(oa);
506
  }
507 edc34dc9 Ondrej Filip
508
  ac1=HEAD(old->area_list);
509
  ac2=HEAD(new->area_list);
510
511
  /* I should get it in same order */
512
  
513
  while(((NODE (ac1))->next!=NULL) && ((NODE (ac2))->next!=NULL))
514
  {
515
    if(ac1->areaid!=ac2->areaid) return 0;
516 c926eee7 Ondrej Filip
    if(ac1->stub!=ac2->stub) return 0;        /* FIXME: non zero values can change */
517
518
    WALK_LIST(oa,po->area_list)
519
      if(oa->areaid==ac2->areaid) break;
520
521
    if(!oa) return 0;
522
523 edc34dc9 Ondrej Filip
    if(ac1->tick!=ac2->tick)
524
    {
525 c926eee7 Ondrej Filip
      if(oa->areaid==ac2->areaid)
526 edc34dc9 Ondrej Filip
      {
527 c926eee7 Ondrej Filip
        oa->tick=ac2->tick;
528
        tm_start(oa->disp_timer,oa->tick);
529
        OSPF_TRACE(D_EVENTS,
530
         "Changing tick interval on area %I from %d to %d",
531
         oa->areaid, ac1->tick, ac2->tick);
532
        break;
533 edc34dc9 Ondrej Filip
      }
534
    }
535
536 c926eee7 Ondrej Filip
    /* Change net_list */
537
    WALK_LIST_DELSAFE(anet, antmp, oa->net_list)
538
    {
539
       rem_node(NODE anet);
540
       mb_free(anet);
541
    }
542
    WALK_LIST(anet, ac2->net_list)
543
    {
544
       antmp=mb_alloc(p->pool, sizeof(struct area_net));
545
       antmp->net=anet->net;
546
       antmp->mlen=anet->mlen;
547
       antmp->hidden=anet->hidden;
548
       add_tail(&oa->net_list, NODE antmp);
549
    }
550
551 edc34dc9 Ondrej Filip
    if(!iface_patts_equal(&ac1->patt_list, &ac2->patt_list,
552
      (void *) ospf_patt_compare))
553
        return 0;
554
555
    WALK_LIST(ifa, po->iface_list)
556
    {
557
      if(ip1=(struct ospf_iface_patt *)
558
        iface_patt_match(&ac1->patt_list, ifa->iface))
559
      {
560
        /* Now reconfigure interface */
561
        if(!(ip2=(struct ospf_iface_patt *)
562
          iface_patt_match(&ac2->patt_list, ifa->iface))) return 0;
563
564
        /* HELLO TIMER */
565
        if(ip1->helloint!=ip2->helloint)
566
        {
567
          ifa->helloint=ip2->helloint;
568
          ifa->hello_timer->recurrent=ifa->helloint;
569
          tm_start(ifa->hello_timer,ifa->helloint);
570
          OSPF_TRACE(D_EVENTS,
571
            "Changing hello interval on interface %s from %d to %d",
572
            ifa->iface->name,ip1->helloint,ip2->helloint);
573
        }
574
575 5ddec4e6 Ondrej Filip
        /* POLL TIMER */
576
        if(ip1->pollint!=ip2->pollint)
577
        {
578
          ifa->pollint=ip2->helloint;
579
          ifa->poll_timer->recurrent=ifa->pollint;
580
          tm_start(ifa->poll_timer,ifa->pollint);
581
          OSPF_TRACE(D_EVENTS,
582
            "Changing poll interval on interface %s from %d to %d",
583
            ifa->iface->name,ip1->pollint,ip2->pollint);
584
        }
585
586 fa6c2405 Ondrej Filip
        /* COST */
587
        if(ip1->cost!=ip2->cost)
588
        {
589
          ifa->cost=ip2->cost;
590
          OSPF_TRACE(D_EVENTS,
591
            "Changing cost interface %s from %d to %d",
592
            ifa->iface->name,ip1->cost,ip2->cost);
593
          schedule_rt_lsa(ifa->oa);
594
        }
595
596 7cedbf21 Ondrej Filip
        /* strict nbma */
597
        if((ip1->strictnbma==0)&&(ip2->strictnbma!=0))
598
        {
599
          ifa->strictnbma=ip2->strictnbma;
600
          OSPF_TRACE(D_EVENTS,
601 e3bc10fd Ondrej Filip
            "Interface %s is now strict NBMA.",
602 7cedbf21 Ondrej Filip
            ifa->iface->name);
603
        }
604
        if((ip1->strictnbma!=0)&&(ip2->strictnbma==0))
605
        {
606
          ifa->strictnbma=ip2->strictnbma;
607
          OSPF_TRACE(D_EVENTS,
608 e3bc10fd Ondrej Filip
            "Interface %s is no longer strict NBMA.",
609
            ifa->iface->name);
610
        }
611
612
        /* stub */
613
        if((ip1->stub==0)&&(ip2->stub!=0))
614
        {
615
          ifa->stub=ip2->stub;
616
          OSPF_TRACE(D_EVENTS,
617
            "Interface %s is now stub.",
618
            ifa->iface->name);
619
        }
620 b02e4011 Ondrej Filip
        if((ip1->stub!=0)&&(ip2->stub==0)&&
621
          ((ifa->ioprob & OSPF_I_IP)==0)&&
622
          (((ifa->ioprob & OSPF_I_MC)==0)||
623
          (ifa->type==OSPF_IT_NBMA)))
624 e3bc10fd Ondrej Filip
        {
625
          ifa->stub=ip2->stub;
626
          OSPF_TRACE(D_EVENTS,
627
            "Interface %s is no longer stub.",
628 7cedbf21 Ondrej Filip
            ifa->iface->name);
629
        }
630
631 edc34dc9 Ondrej Filip
        /* AUTHETICATION */
632
        if(ip1->autype!=ip2->autype)
633
        {
634
          ifa->autype=ip2->autype;
635
          OSPF_TRACE(D_EVENTS,
636
            "Changing autentication type on interface %s",
637
            ifa->iface->name);
638
        }
639
        if(strncmp(ip1->password,ip2->password,8)!=0)
640
        {
641
          memcpy(ifa->aukey,ip2->password,8);
642
          OSPF_TRACE(D_EVENTS,
643
            "Changing password on interface %s",
644
            ifa->iface->name);
645
        }
646
647
        /* RXMT */
648
        if(ip1->rxmtint!=ip2->rxmtint)
649
        {
650
          ifa->rxmtint=ip2->rxmtint;
651
          OSPF_TRACE(D_EVENTS,
652
            "Changing retransmit interval on interface %s from %d to %d",
653
            ifa->iface->name,ip1->rxmtint,ip2->rxmtint);
654
        }
655
656
        /* WAIT */
657
        if(ip1->waitint!=ip2->waitint)
658
        {
659
          ifa->waitint=ip2->waitint;
660
          if(ifa->wait_timer->expires!=0)
661
            tm_start(ifa->wait_timer,ifa->waitint);
662
          OSPF_TRACE(D_EVENTS,
663
            "Changing wait interval on interface %s from %d to %d",
664
            ifa->iface->name,ip1->waitint,ip2->waitint);
665
        }
666
667
        /* INFTRANS */
668
        if(ip1->inftransdelay!=ip2->inftransdelay)
669
        {
670
          ifa->inftransdelay=ip2->inftransdelay;
671
          OSPF_TRACE(D_EVENTS,
672
            "Changing transmit delay on interface %s from %d to %d",
673
            ifa->iface->name,ip1->inftransdelay,ip2->inftransdelay);
674
        }
675
676
        /* DEAD COUNT */
677
        if(ip1->deadc!=ip2->deadc)
678
        {
679
          ifa->deadc=ip2->deadc;
680
          OSPF_TRACE(D_EVENTS,
681
            "Changing dead count on interface %s from %d to %d",
682
            ifa->iface->name,ip1->deadc,ip2->deadc);
683
        }
684
685
        /* NBMA LIST */
686
        /* First remove old */
687
        WALK_LIST_DELSAFE(nb1, nbnx, ifa->nbma_list)
688
        {
689
          found=0;
690
          WALK_LIST(nb2, ip2->nbma_list)
691
            if(ipa_compare(nb1->ip,nb2->ip)==0)
692
            {
693
              found=1;
694 7cedbf21 Ondrej Filip
              if(nb1->eligible!=nb2->eligible)
695
                OSPF_TRACE(D_EVENTS,
696
                  "Changing neighbor eligibility %I on interface %s",
697
                  nb1->ip,ifa->iface->name);
698 edc34dc9 Ondrej Filip
              break;
699
            }
700
701
          if(!found)
702
          {
703
            OSPF_TRACE(D_EVENTS,
704
              "Removing NBMA neighbor %I on interface %s",
705
              nb1->ip,ifa->iface->name);
706
            rem_node(NODE nb1);
707
            mb_free(nb1);
708
          }
709
        }
710
        /* And then add new */
711
        WALK_LIST(nb2, ip2->nbma_list)
712
        {
713
          found=0;
714
          WALK_LIST(nb1, ifa->nbma_list)
715
            if(ipa_compare(nb1->ip,nb2->ip)==0)
716
            {
717
              found=1;
718
              break;
719
            }
720
          if(!found)
721
          {
722
            nb1=mb_alloc(p->pool,sizeof(struct nbma_node));
723
            nb1->ip=nb2->ip;
724 7cedbf21 Ondrej Filip
            nb1->eligible=nb2->eligible;
725 edc34dc9 Ondrej Filip
            add_tail(&ifa->nbma_list, NODE nb1);
726
            OSPF_TRACE(D_EVENTS,
727
              "Adding NBMA neighbor %I on interface %s",
728
              nb1->ip,ifa->iface->name);
729
          }
730
        }
731
      }
732
    }
733
734
    NODE ac1=(NODE (ac1))->next;
735
    NODE ac2=(NODE (ac2))->next;
736
  }
737
738
  if(((NODE (ac1))->next)!=((NODE (ac2))->next))
739
    return 0;        /* One is not null */
740
741
  return 1;        /* Everythink OK :-) */
742 80787d41 Ondrej Filip
}
743 a783e259 Ondrej Filip
744
void
745 c4f0f014 Ondrej Filip
ospf_sh_neigh(struct proto *p, char *iff)
746 a783e259 Ondrej Filip
{
747 c4f0f014 Ondrej Filip
  struct ospf_iface *ifa=NULL,*f;
748 a783e259 Ondrej Filip
  struct ospf_neighbor *n;
749
  struct proto_ospf *po=(struct proto_ospf *)p;
750 3488634c Ondrej Filip
751
  if(p->proto_state != PS_UP)
752
  {
753
    cli_msg(-1013,"%s: is not up", p->name);
754
    cli_msg(0,"");
755
    return;
756
  }
757 c4f0f014 Ondrej Filip
  
758
  if(iff!=NULL)
759
  {
760
    WALK_LIST(f, po->iface_list)
761
    {
762 a489f0ce Ondrej Filip
      if(strcmp(iff,f->iface->name)==0)
763
      {
764
        ifa=f;
765
        break;
766
      }
767 c4f0f014 Ondrej Filip
    }
768
    if(ifa==NULL)
769
    {
770
      cli_msg(0,"");
771
      return;
772
    }
773 a489f0ce Ondrej Filip
    cli_msg(-1013,"%s:", p->name);
774
    cli_msg(-1013,"%-12s\t%3s\t%-15s\t%-5s\t%-12s\t%-10s","Router ID","Pri",
775
      "     State", "DTime", "Router IP", "Interface");
776
    WALK_LIST(n, ifa->neigh_list) ospf_sh_neigh_info(n);
777
    cli_msg(0,"");
778 ece612e1 Ondrej Filip
    return;
779 a489f0ce Ondrej Filip
  }
780
781 4ab4e977 Ondrej Filip
  cli_msg(-1013,"%s:", p->name);
782 a783e259 Ondrej Filip
  cli_msg(-1013,"%-12s\t%3s\t%-15s\t%-5s\t%-12s\t%-10s","Router ID","Pri",
783
    "     State", "DTime", "Router IP", "Interface");
784 a489f0ce Ondrej Filip
  WALK_LIST(ifa,po->iface_list)
785
    WALK_LIST(n, ifa->neigh_list)
786
      ospf_sh_neigh_info(n);
787 a783e259 Ondrej Filip
  cli_msg(0,"");
788
}
789 4ab4e977 Ondrej Filip
790
void
791
ospf_sh(struct proto *p)
792
{
793
  struct ospf_area *oa;
794
  struct proto_ospf *po=(struct proto_ospf *)p;
795
  struct ospf_iface *ifa;
796
  struct ospf_neighbor *n;
797
  int ifano;
798
  int nno;
799
  int adjno;
800
801 3488634c Ondrej Filip
  if(p->proto_state != PS_UP)
802
  {
803
    cli_msg(-1014,"%s: is not up", p->name);
804
    cli_msg(0,"");
805
    return;
806
  }
807
808 4ab4e977 Ondrej Filip
  cli_msg(-1014,"%s:", p->name);
809
  cli_msg(-1014,"Number of areas: %u", po->areano);
810
  
811
  WALK_LIST(oa,po->area_list)
812
  {
813
    cli_msg(-1014,"\tArea: %I (%u) %s", oa->areaid, oa->areaid,
814
      oa->areaid==0 ? "[BACKBONE]" : "");
815
    ifano=0;
816
    nno=0;
817
    adjno=0;
818
    WALK_LIST(ifa, po->iface_list)
819
    {
820
      if(oa==ifa->oa) ifano++;
821
      WALK_LIST(n, ifa->neigh_list)
822
      {
823
        nno++;
824
        if(n->state==NEIGHBOR_FULL) adjno++;
825
      }
826
    }
827 3b580a23 Ondrej Filip
    cli_msg(-1014,"\t\tStub:\t%s", oa->stub ? "Yes" : "No");
828
    cli_msg(-1014,"\t\tRT scheduler tick:\t%u", oa->tick);
829
    cli_msg(-1014,"\t\tNumber of interfaces:\t%u", ifano);
830
    cli_msg(-1014,"\t\tNumber of LSAs in DB:\t%u", oa->gr->hash_entries);
831
    cli_msg(-1014,"\t\tNumber of neighbors:\t%u", nno);
832
    cli_msg(-1014,"\t\tNumber of adjacent neighbors:\t%u", adjno);
833 c926eee7 Ondrej Filip
    if(!EMPTY_LIST(oa->net_list))
834
    {
835
      struct area_net *anet;
836
      cli_msg(-1014,"\t\tArea networks:");
837
      WALK_LIST(anet, oa->net_list)
838
      {
839 e43ae633 Martin Mares
        cli_msg(-1014,"\t\t\t%1I/%u\t%s", anet->net, anet->mlen,
840 c926eee7 Ondrej Filip
          anet->hidden ? "Hidden" : "Advertise");
841
      }
842
    }
843 4ab4e977 Ondrej Filip
  }
844
  cli_msg(0,"");
845
}
846
847 c4f0f014 Ondrej Filip
void
848
ospf_sh_iface(struct proto *p, char *iff)
849
{
850
  struct ospf_area *oa;
851
  struct proto_ospf *po=(struct proto_ospf *)p;
852
  struct ospf_iface *ifa=NULL,*f;
853
  struct ospf_neighbor *n;
854
  int ifano;
855
  int nno;
856
  int adjno;
857
858 3488634c Ondrej Filip
  if(p->proto_state != PS_UP)
859
  {
860
    cli_msg(-1015,"%s: is not up", p->name);
861
    cli_msg(0,"");
862
    return;
863
  }
864
865 c4f0f014 Ondrej Filip
  if(iff!=NULL)
866
  {
867
    WALK_LIST(f, po->iface_list)
868
    {
869 a489f0ce Ondrej Filip
      if(strcmp(iff,f->iface->name)==0)
870
      {
871
        ifa=f;
872
        break;
873
      }
874 c4f0f014 Ondrej Filip
    }
875 a489f0ce Ondrej Filip
876 c4f0f014 Ondrej Filip
    if(ifa==NULL)
877
    {
878
      cli_msg(0,"");
879
      return;
880
    }
881
    cli_msg(-1015,"%s:", p->name);
882
    ospf_iface_info(ifa);
883
    cli_msg(0,"");
884
    return;
885
  }
886
  cli_msg(-1015,"%s:", p->name);
887
  WALK_LIST(ifa, po->iface_list) ospf_iface_info(ifa);
888
  cli_msg(0,"");
889
}
890
891 80787d41 Ondrej Filip
struct protocol proto_ospf = {
892
  name:                 "OSPF",
893
  template:                "ospf%d",
894
  attr_class:                EAP_OSPF,
895
  init:                        ospf_init,
896
  dump:                        ospf_dump,
897
  start:                ospf_start,
898
  shutdown:                ospf_shutdown,
899
  get_route_info:        ospf_get_route_info,
900
  get_attr:                ospf_get_attr,
901
  get_status:                ospf_get_status,
902
  reconfigure:                ospf_reconfigure
903
};