neighbor.c 14.3 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF
 *
4
 *	(c) 1999 - 2004 Ondrej Filip <feela@network.cz>
5 6 7 8 9 10
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

11 12 13 14 15 16 17 18
char *ospf_ns[]={"    down",
                 " attempt",
                 "    init",
                 "    2way",
                 " exstart",
                 "exchange",
                 " loading",
                 "    full"};
19

Ondřej Filip's avatar
Ondřej Filip committed
20 21 22 23 24
const char *ospf_inm[]={ "hello received", "neighbor start", "2-way received",
  "negotiation done", "exstart done", "bad ls request", "load done",
  "adjacency ok?", "sequence mismatch", "1-way received", "kill neighbor",
  "inactivity timer", "line down" };

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

void neighbor_timer_hook(timer *timer);
void rxmt_timer_hook(timer *timer);



struct ospf_neighbor *
ospf_neighbor_new(struct ospf_iface *ifa)
{
  struct proto *p = (struct proto *)(ifa->proto);
  struct pool *pool = rp_new(p->pool, "OSPF Neighbor");
  struct ospf_neighbor *n = mb_allocz(pool, sizeof(struct ospf_neighbor));

  n->pool = pool;
  n->ifa = ifa;
  add_tail(&ifa->neigh_list, NODE n);
  n->adj = 0;
  n->ldbdes=mb_alloc(pool, ifa->iface->mtu);
  n->state=NEIGHBOR_DOWN;

  n->inactim = tm_new(pool);
  n->inactim->data = n;
  n->inactim->randomize = 0;
  n->inactim->hook = neighbor_timer_hook;
  n->inactim->recurrent = 0;
  DBG("%s: Installing inactivity timer.\n", p->name);

  n->rxmt_timer = tm_new(pool);
  n->rxmt_timer->data = n;
  n->rxmt_timer->randomize = 0;
  n->rxmt_timer->hook = rxmt_timer_hook;
  n->rxmt_timer->recurrent = ifa->rxmtint;
  s_init_list(&(n->lsrql));
  n->lsrqh = ospf_top_new(pool, n->ifa->proto);
  s_init_list(&(n->lsrtl));
  n->lsrth = ospf_top_new(pool, n->ifa->proto);
  s_init(&(n->dbsi), &(n->ifa->oa->lsal));
  s_init(&(n->lsrqi), &(n->lsrql));
  s_init(&(n->lsrti), &(n->lsrtl));
  tm_start(n->rxmt_timer,n->ifa->rxmtint);
  DBG("%s: Installing rxmt timer.\n", p->name);

  n->ackd_timer = tm_new(pool);
  n->ackd_timer->data = n;
  n->ackd_timer->randomize = 0;
  n->ackd_timer->hook = ackd_timer_hook;
  n->ackd_timer->recurrent = ifa->rxmtint/2;
  init_list(&n->ackl);
  tm_start(n->ackd_timer,n->ifa->rxmtint/2);
  DBG("%s: Installing ackd timer.\n", p->name);

  return(n);
}

79 80 81 82 83
/**
 * neigh_chstate - handles changes related to new or lod state of neighbor
 * @n: OSPF neighbor
 * @state: new state
 *
84
 * Many actions have to be taken acording to a change of state of a neighbor. It
85 86 87
 * starts rxmt timers, call interface state machine etc.
 */

88 89 90
void
neigh_chstate(struct ospf_neighbor *n, u8 state)
{
91
  u8 oldstate;
92

93 94 95
  oldstate=n->state;

  if(oldstate!=state)
96
  {
Ondřej Filip's avatar
Ondřej Filip committed
97 98 99 100
    struct ospf_iface *ifa=n->ifa;
    struct proto_ospf *po=ifa->oa->po;
    struct proto *p=&po->proto;

101
    n->state=state;
Ondřej Filip's avatar
Ondřej Filip committed
102

Ondřej Filip's avatar
Ondřej Filip committed
103 104
    OSPF_TRACE( D_EVENTS, "Neighbor %I changes state from \"%s\" to \"%s\".",
      n->ip, ospf_ns[oldstate], ospf_ns[state]);
Ondřej Filip's avatar
Ondřej Filip committed
105

106
    if((state==NEIGHBOR_2WAY) && (oldstate<NEIGHBOR_2WAY))
Ondřej Filip's avatar
Ondřej Filip committed
107
      ospf_int_sm(ifa, ISM_NEICH);
108
    if((state<NEIGHBOR_2WAY) && (oldstate>=NEIGHBOR_2WAY))
Ondřej Filip's avatar
Ondřej Filip committed
109 110
      ospf_int_sm(ifa, ISM_NEICH);

111
    if(oldstate==NEIGHBOR_FULL)	/* Decrease number of adjacencies */
112
    {
113
      ifa->fadj--;
114
      schedule_rt_lsa(ifa->oa);
115
      schedule_net_lsa(ifa);
116
    }
117
  
118
    if(state==NEIGHBOR_FULL)	/* Increase number of adjacencies */
119
    {
120
      ifa->fadj++;
121
      schedule_rt_lsa(ifa->oa);
122
      schedule_net_lsa(ifa);
123
    }
124 125 126 127 128 129 130 131 132 133 134 135
    if(state==NEIGHBOR_EXSTART)
    {
      if(n->adj==0)	/* First time adjacency */
      {
        n->dds=random_u32();
      }
      n->dds++;
      n->myimms.byte=0;
      n->myimms.bit.ms=1;
      n->myimms.bit.m=1;
      n->myimms.bit.i=1;
    }
136
    if(state>NEIGHBOR_EXSTART) n->myimms.bit.i=0;
137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
}

struct ospf_neighbor *
electbdr(list nl)
{
  struct ospf_neighbor *neigh,*n1,*n2;

  n1=NULL;
  n2=NULL;
  WALK_LIST (neigh, nl)		/* First try those decl. themselves */
  {
    if(neigh->state>=NEIGHBOR_2WAY)	/* Higher than 2WAY */
      if(neigh->priority>0)		/* Eligible */
151
        if(ipa_compare(neigh->ip,neigh->dr)!=0)	/* And not decl. itself DR */
152
	{
153
	  if(ipa_compare(neigh->ip,neigh->bdr)==0)	/* Declaring BDR */
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
          {
            if(n1!=NULL)
            {
              if(neigh->priority>n1->priority) n1=neigh;
	      else if(neigh->priority==n1->priority)
	          if(neigh->rid>n1->rid) n1=neigh;
            }
	    else
            {
              n1=neigh;
            }
          }
	  else				/* And NOT declaring BDR */
          {
            if(n2!=NULL)
            {
              if(neigh->priority>n2->priority) n2=neigh;
	      else if(neigh->priority==n2->priority)
	          if(neigh->rid>n2->rid) n2=neigh;
            }
	    else
            {
              n2=neigh;
            }
          }
      }
  }
  if(n1==NULL) n1=n2;

  return(n1);
}

struct ospf_neighbor *
electdr(list nl)
{
  struct ospf_neighbor *neigh,*n;

  n=NULL;
  WALK_LIST (neigh, nl)	/* And now DR */
  {
    if(neigh->state>=NEIGHBOR_2WAY)	/* Higher than 2WAY */
      if(neigh->priority>0)		/* Eligible */
196
        if(ipa_compare(neigh->ip,neigh->dr)==0)	/* And declaring itself DR */
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	{
          if(n!=NULL)
          {
            if(neigh->priority>n->priority) n=neigh;
	    else if(neigh->priority==n->priority)
	        if(neigh->rid>n->rid) n=neigh;
          }
	  else
          {
            n=neigh;
          }
      }
  }

  return(n);
}

int
can_do_adj(struct ospf_neighbor *n)
{
  struct ospf_iface *ifa;
  struct proto *p;
  int i;

  ifa=n->ifa;
  p=(struct proto *)(ifa->proto);
  i=0;

  switch(ifa->type)
  {
    case OSPF_IT_PTP:
    case OSPF_IT_VLINK:
      i=1;
      break;
    case OSPF_IT_BCAST:
    case OSPF_IT_NBMA:
      switch(ifa->state)
      {
        case OSPF_IS_DOWN:
236
          bug("%s: Iface %s in down state?", p->name, ifa->iface->name);
237 238 239 240 241 242
          break;
        case OSPF_IS_WAITING:
          DBG("%s: Neighbor? on iface %s\n",p->name, ifa->iface->name);
          break;
        case OSPF_IS_DROTHER:
          if(((n->rid==ifa->drid) || (n->rid==ifa->bdrid))
243
            && (n->state>=NEIGHBOR_2WAY)) i=1;
244 245 246 247
          break;
        case OSPF_IS_PTP:
        case OSPF_IS_BACKUP:
        case OSPF_IS_DR:
248
          if(n->state>=NEIGHBOR_2WAY) i=1;
249 250
          break;
        default:
251
          bug("%s: Iface %s in unknown state?",p->name, ifa->iface->name);
252 253 254 255
          break;
      }
      break;
    default:
256
      bug("%s: Iface %s is unknown type?",p->name, ifa->iface->name);
257 258 259 260 261 262
      break;
  }
  DBG("%s: Iface %s can_do_adj=%d\n",p->name, ifa->iface->name,i);
  return i;
}

263 264 265 266 267
/**
 * ospf_neigh_sm - ospf neighbor state machine
 * @n: neighor
 * @event: actual event
 *
268 269 270 271 272
 * This part implements the neighbor state machine as described in 10.3 of
 * RFC 2328. The only difference is that state %NEIGHBOR_ATTEMPT is not
 * used. We discover neighbors on nonbroadcast networks in the
 * same way as on broadcast networks. The only difference is in
 * sending hello packets. These are sent to IPs listed in
273 274
 * @ospf_iface->nbma_list .
 */
275 276 277
void
ospf_neigh_sm(struct ospf_neighbor *n, int event)
{
278 279
  struct proto *p=(struct proto *)(n->ifa->proto);
  struct proto_ospf *po=n->ifa->proto;
280

Ondřej Filip's avatar
Ondřej Filip committed
281 282 283
  DBG("%s: Neighbor state machine for neighbor %I, event \"%s\".\n",
    p->name, n->rid, ospf_inm[event]);

284 285 286 287
  switch(event)
  {
    case INM_START:
      neigh_chstate(n,NEIGHBOR_ATTEMPT);
288
      /* NBMA are used different way */
289 290 291 292 293 294
      break;
    case INM_HELLOREC:
      switch(n->state)
      {
        case NEIGHBOR_ATTEMPT:
	case NEIGHBOR_DOWN:
295
	  neigh_chstate(n, NEIGHBOR_INIT);
296
	default:
297
          tm_start(n->inactim, n->ifa->deadc*n->ifa->helloint);	/* Restart inactivity timer */
298 299 300 301
	  break;
      }
      break;
    case INM_2WAYREC:
302 303 304
      if(n->state<NEIGHBOR_2WAY) neigh_chstate(n,NEIGHBOR_2WAY);
      if((n->state==NEIGHBOR_2WAY) && can_do_adj(n))
        neigh_chstate(n,NEIGHBOR_EXSTART);
305 306 307 308 309
      break;
    case INM_NEGDONE:
      if(n->state==NEIGHBOR_EXSTART)
      {
        neigh_chstate(n,NEIGHBOR_EXCHANGE);
310 311 312 313 314 315 316
        while(!EMPTY_LIST(n->ackl))
        {
          struct lsah_n *no;
          no=(struct lsah_n *)HEAD(n->ackl);
          rem_node(NODE no);
          mb_free(no);
        }
317
      }
318
      else bug("NEGDONE and I'm not in EXSTART?");
319 320
      break;
    case INM_EXDONE:
321
        neigh_chstate(n,NEIGHBOR_LOADING);
322 323
      break;
    case INM_LOADDONE:
Ondřej Filip's avatar
Ondřej Filip committed
324
        neigh_chstate(n,NEIGHBOR_FULL);
325 326 327 328 329
      break;
    case INM_ADJOK:
        switch(n->state)
        {
          case NEIGHBOR_2WAY:
330
            /* Can In build adjacency? */
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            if(can_do_adj(n))
            {
              neigh_chstate(n,NEIGHBOR_EXSTART);
            }
            break;
          default:
	    if(n->state>=NEIGHBOR_EXSTART)
              if(!can_do_adj(n))
              {
                neigh_chstate(n,NEIGHBOR_2WAY);
              }
            break;
        }
      break;
    case INM_SEQMIS:
346
      OSPF_TRACE(D_EVENTS, "Seq mis!");
347
    case INM_BADLSREQ:
Ondřej Filip's avatar
Ondřej Filip committed
348
      OSPF_TRACE(D_EVENTS, "Bad LS req!");
349 350 351 352 353 354 355 356 357 358 359
      if(n->state>=NEIGHBOR_EXCHANGE)
      {
        neigh_chstate(n,NEIGHBOR_EXSTART);
      }
      break;
    case INM_KILLNBR:
    case INM_LLDOWN:
    case INM_INACTTIM:
      neigh_chstate(n,NEIGHBOR_DOWN);
      break;
    case INM_1WAYREC:
360
      neigh_chstate(n,NEIGHBOR_INIT);
361 362
      break;
    default:
363
      bug("%s: INM - Unknown event?",p->name);
364 365 366 367
      break;
  }
}

368 369 370 371
/**
 * bdr_election - (Backup) Designed Router election
 * @ifa: actual interface
 *
372
 * When the wait timer fires, it is time to elect (Backup) Designated Router.
373
 * Structure describing me is added to this list so every electing router
374
 * has the same list. Backup Designated Router is elected before Designated
375 376
 * Router. This process is described in 9.4 of RFC 2328.
 */
377
void
378
bdr_election(struct ospf_iface *ifa)
379
{
380
  struct ospf_neighbor *neigh,*ndr,*nbdr,me,*tmp;
381 382
  u32 myid;
  ip_addr ndrip, nbdrip;
383
  int doadj;
384
  struct proto *p=&ifa->proto->proto;
385

386
  DBG("(B)DR election.\n");
387 388 389 390 391 392

  myid=p->cf->global->router_id;

  me.state=NEIGHBOR_2WAY;
  me.rid=myid;
  me.priority=ifa->priority;
393 394
  me.dr=ifa->drip;
  me.bdr=ifa->bdrip;
395
  me.ip=ifa->iface->addr->ip;
396 397 398 399 400 401

  add_tail(&ifa->neigh_list, NODE &me);

  nbdr=electbdr(ifa->neigh_list);
  ndr=electdr(ifa->neigh_list);

402
  if(ndr==NULL) ndr=nbdr;
403

404 405 406 407
  if(((ifa->drid==myid) && (ndr!=&me))
    || ((ifa->drid!=myid) && (ndr==&me))
    || ((ifa->bdrid==myid) && (nbdr!=&me)) 
    || ((ifa->bdrid!=myid) && (nbdr==&me)))
408
  {
409 410
    if(ndr==NULL) ifa->drip=me.dr=ipa_from_u32(0);
    else ifa->drip=me.dr=ndr->ip;
411

412 413
    if(nbdr==NULL) ifa->bdrip=me.bdr=ipa_from_u32(0);
    else ifa->bdrip=me.bdr=nbdr->ip;
414 415 416 417 418

    nbdr=electbdr(ifa->neigh_list);
    ndr=electdr(ifa->neigh_list);
  }

419 420
  if(ndr==NULL) ndrip=ipa_from_u32(0);
  else ndrip=ndr->ip;
421

422 423
  if(nbdr==NULL) nbdrip=ipa_from_u32(0);
  else nbdrip=nbdr->ip;
424 425

  doadj=0;
426 427 428 429
  if((ipa_compare(ifa->drip,ndrip)!=0) || (ipa_compare(ifa->bdrip,nbdrip)!=0))
    doadj=1;

  if(ndr==NULL)
430 431 432 433 434 435
  {
    ifa->drid=0;
    ifa->drip=ipa_from_u32(0);
  }
  else
  {
436 437
    ifa->drid=ndr->rid;
    ifa->drip=ndr->ip;
438 439
  }

440
  if(nbdr==NULL)
441 442 443 444 445 446
  {
    ifa->bdrid=0;
    ifa->bdrip=ipa_from_u32(0);
  }
  else
  {
447 448
    ifa->bdrid=nbdr->rid;
    ifa->bdrip=nbdr->ip;
449
  }
450

451
  DBG("DR=%I, BDR=%I\n", ifa->drid, ifa->bdrid);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

  if(myid==ifa->drid) iface_chstate(ifa, OSPF_IS_DR);
  else
  {
    if(myid==ifa->bdrid) iface_chstate(ifa, OSPF_IS_BACKUP);
    else iface_chstate(ifa, OSPF_IS_DROTHER);
  }

  rem_node(NODE &me);

  if(doadj)
  {
    WALK_LIST (neigh, ifa->neigh_list)
    {
      ospf_neigh_sm(neigh, INM_ADJOK);
    }
  }
}

struct ospf_neighbor *
find_neigh(struct ospf_iface *ifa, u32 rid)
{
  struct ospf_neighbor *n;

  WALK_LIST (n, ifa->neigh_list)
    if(n->rid == rid)
      return n;
  return NULL;
}

482 483

/* Find a closest neighbor which is at leas 2-Way */
484 485 486
struct ospf_neighbor *
find_neigh_noifa(struct proto_ospf *po, u32 rid)
{
487
  struct ospf_neighbor *n=NULL,*m;
488 489 490
  struct ospf_iface *ifa;

  WALK_LIST (ifa, po->iface_list)
491 492 493 494 495 496 497 498 499 500
    if((m=find_neigh(ifa, rid))!=NULL)
    {
      if(m->state>=NEIGHBOR_2WAY)
      {
        if(n==NULL) n=m;
        else
          if(m->ifa->cost < n->ifa->cost) n=m;
      }
    }
  return n;
501 502
}

503 504 505 506 507 508 509 510 511
struct ospf_area *
ospf_find_area(struct proto_ospf *po, u32 aid)
{
  struct ospf_area *oa;
  WALK_LIST(NODE oa,po->area_list)
    if(((struct ospf_area *)oa)->areaid==aid) return oa;
  return NULL;
}

512 513 514 515 516 517 518 519 520 521 522
/* Neighbor is inactive for a long time. Remove it. */
void
neighbor_timer_hook(timer *timer)
{
  struct ospf_neighbor *n;
  struct ospf_iface *ifa;
  struct proto *p;

  n=(struct ospf_neighbor *)timer->data;
  ifa=n->ifa;
  p=(struct proto *)(ifa->proto);
Ondřej Filip's avatar
Ondřej Filip committed
523 524
  OSPF_TRACE(D_EVENTS,"Inactivity timer fired on interface %s for neighbor %I.",
    ifa->iface->name, n->ip);
525 526 527 528 529 530 531 532 533 534 535
  ospf_neigh_remove(n);
}

void
ospf_neigh_remove(struct ospf_neighbor *n)
{
  struct ospf_iface *ifa;
  struct proto *p;

  ifa=n->ifa;
  p=(struct proto *)(ifa->proto);
536 537
  neigh_chstate(n, NEIGHBOR_DOWN);
  rem_node(NODE n);
538
  rfree(n->pool);
Ondřej Filip's avatar
Ondřej Filip committed
539
  OSPF_TRACE(D_EVENTS, "Deleting neigbor.");
540
}
541 542 543 544 545 546 547 548 549 550 551 552 553 554

void
ospf_sh_neigh_info(struct ospf_neighbor *n)
{
   struct ospf_iface *ifa=n->ifa;
   char *pos="other";
   char etime[6];
   int exp,sec,min;

   exp=n->inactim->expires-now;
   sec=exp-(exp/60);
   min=(exp-sec)/60;
   if(min>59)
   {
Ondřej Filip's avatar
Ondřej Filip committed
555
     bsprintf(etime,"-Inf-");
556 557 558
   }
   else
   {
Ondřej Filip's avatar
Ondřej Filip committed
559
     bsprintf(etime,"%02u:%02u", min, sec);
560 561 562 563
   }
   
   if(n->rid==ifa->drid) pos="dr   ";
   if(n->rid==ifa->bdrid) pos="bdr  ";
564
   if(n->ifa->type==OSPF_IT_PTP) pos="ptp  ";
565

566
   cli_msg(-1013,"%-1I\t%3u\t%s/%s\t%-5s\t%-1I\t%-10s",n->rid, n->priority,
567 568
     ospf_ns[n->state], pos, etime, n->ip,ifa->iface->name);
}
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610

void
rxmt_timer_hook(timer *timer)
{
  struct ospf_neighbor *n = (struct ospf_neighbor *)timer->data;
  struct ospf_iface *ifa = n->ifa;
  struct proto *p = (struct proto *)(ifa->proto);
  struct top_hash_entry *en;

  DBG("%s: RXMT timer fired on interface %s for neigh: %I.\n",
    p->name, ifa->iface->name, n->ip);
  if (n->state < NEIGHBOR_LOADING) ospf_dbdes_tx(n);

  if(n->state < NEIGHBOR_FULL) ospf_lsreq_tx(n);
  else
  {
    if(!EMPTY_SLIST(n->lsrtl))
    {
      list uplist;
      slab *upslab;
      struct l_lsr_head *llsh;

      init_list(&uplist);
      upslab=sl_new(n->pool,sizeof(struct l_lsr_head));

      WALK_SLIST(SNODE en,n->lsrtl)
      {
	if((SNODE en)->next==(SNODE en)) bug("RTList is cycled");
        llsh=sl_alloc(upslab);
        llsh->lsh.id=en->lsa.id;
        llsh->lsh.rt=en->lsa.rt;
        llsh->lsh.type=en->lsa.type;
	DBG("Working on ID: %I, RT: %I, Type: %u\n",
          en->lsa.id, en->lsa.rt, en->lsa.type);
        add_tail(&uplist, NODE llsh);
      }
      ospf_lsupd_tx_list(n, &uplist);
      rfree(upslab);
    }
  }
}