Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / hello.c @ 035f6acb

History | View | Annotate | Download (8.48 KB)

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

    
9
#include "ospf.h"
10

    
11
void
12
install_inactim(struct ospf_neighbor *n)
13
{
14
  struct proto *p;
15
  struct ospf_iface *ifa;
16

    
17
  ifa=n->ifa;
18
  p=(struct proto *)(ifa->proto);
19

    
20
  if(n->inactim==NULL)
21
  {
22
    n->inactim=tm_new(n->pool);
23
    n->inactim->data=n;
24
    n->inactim->randomize=0;
25
    n->inactim->hook=neighbor_timer_hook;
26
    n->inactim->recurrent=0;
27
    DBG("%s: Installing inactivity timer.\n", p->name);
28
  }
29
}
30

    
31
void
32
restart_inactim(struct ospf_neighbor *n)
33
{
34
  tm_start(n->inactim,n->ifa->deadc*n->ifa->helloint);
35
}
36

    
37
void
38
restart_hellotim(struct ospf_iface *ifa)
39
{
40
  tm_start(ifa->hello_timer,ifa->helloint);
41
}
42

    
43
void
44
restart_polltim(struct ospf_iface *ifa)
45
{
46
  if(ifa->poll_timer)
47
    tm_start(ifa->poll_timer,ifa->pollint);
48
}
49

    
50
void
51
restart_waittim(struct ospf_iface *ifa)
52
{
53
  tm_start(ifa->wait_timer,ifa->waitint);
54
}
55

    
56
void
57
ospf_hello_rx(struct ospf_hello_packet *ps, struct proto *p,
58
  struct ospf_iface *ifa, int size, ip_addr faddr)
59
{
60
  u32 nrid, *pnrid;
61
  struct ospf_neighbor *neigh,*n;
62
  u8 i,twoway,oldpriority;
63
  ip_addr olddr,oldbdr;
64
  ip_addr mask;
65
  char *beg=": Bad OSPF hello packet from ", *rec=" received: ";
66
  int eligible=0;
67
  pool *pool;
68

    
69
  nrid=ntohl(((struct ospf_packet *)ps)->routerid);
70

    
71
  OSPF_TRACE(D_PACKETS, "Received hello from %I via %s",faddr,ifa->iface->name);
72
  mask=ps->netmask;
73
  ipa_ntoh(mask);
74

    
75
  if((unsigned)ipa_mklen(mask)!=ifa->iface->addr->pxlen)
76
  {
77
    log("%s%s%I%sbad netmask %I.", p->name, beg, nrid, rec,
78
      mask);
79
    return;
80
  }
81
  
82
  if(ntohs(ps->helloint)!=ifa->helloint)
83
  {
84
    log("%s%s%I%shello interval mismatch.", p->name, beg, faddr, rec);
85
    return;
86
  }
87

    
88
  if(ntohl(ps->deadint)!=ifa->helloint*ifa->deadc)
89
  {
90
    log("%s%s%I%sdead interval mismatch.", p->name, beg, faddr, rec);
91
    return;
92
  }
93

    
94
  if(ps->options!=ifa->options)
95
  {
96
    log("%s%s%I%soptions mismatch.", p->name, beg, faddr, rec);
97
    return;
98
  }
99

    
100
  if((n=find_neigh(ifa, nrid))==NULL)
101
  {
102
    if((ifa->type==OSPF_IT_NBMA))
103
    {
104
      struct nbma_node *nn;
105
      int found=0;
106

    
107
      WALK_LIST(nn,ifa->nbma_list)
108
      {
109
        if(ipa_compare(faddr,nn->ip)==0)
110
        {
111
          found=1;
112
          break;
113
        }
114
      }
115
      if((found==0)&&(ifa->strictnbma))
116
      {
117
        log("%s: Ignoring new neighbor: %I on %s.", p->name, faddr,
118
          ifa->iface->name);
119
        return;
120
      }
121
      if(found)
122
      {
123
        eligible=nn->eligible;
124
        if(((ps->priority==0)&&eligible)||((ps->priority>0)&&(eligible==0)))
125
        {
126
          log("%s: Eligibility mismatch for neighbor: %I on %s", p->name,
127
            faddr, ifa->iface->name);
128
          return;
129
        }
130
      }
131
    }
132
    OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s.", faddr,
133
      ifa->iface->name);
134
    pool=rp_new(p->pool, "OSPF Neighbor");
135
    n=mb_allocz(pool, sizeof(struct ospf_neighbor));
136
    n->pool=pool;
137
    add_tail(&ifa->neigh_list, NODE n);
138
    n->rid=nrid;
139
    n->ip=faddr;
140
    n->dr=ps->dr;
141
    ipa_ntoh(n->dr);
142
    n->bdr=ps->bdr;
143
    ipa_ntoh(n->bdr);
144
    n->priority=ps->priority;
145
    n->options=ps->options;
146
    n->ifa=ifa;
147
    n->adj=0;
148
    n->ldbdes=mb_alloc(pool, ifa->iface->mtu);
149
    n->state=NEIGHBOR_DOWN;
150
    install_inactim(n);
151
    n->rxmt_timer=tm_new(pool);
152
    n->rxmt_timer->data=n;
153
    n->rxmt_timer->randomize=0;
154
    n->rxmt_timer->hook=rxmt_timer_hook;
155
    n->rxmt_timer->recurrent=ifa->rxmtint;
156
    DBG("%s: Installing rxmt timer.\n", p->name);
157
    n->lsrr_timer=tm_new(pool);
158
    n->lsrr_timer->data=n;
159
    n->lsrr_timer->randomize=0;
160
    n->lsrr_timer->hook=lsrr_timer_hook;
161
    n->lsrr_timer->recurrent=ifa->rxmtint;
162
    DBG("%s: Installing lsrr timer.\n", p->name);
163
    init_list(&n->ackl);
164
    n->ackd_timer=tm_new(pool);
165
    n->ackd_timer->data=n;
166
    n->ackd_timer->randomize=0;
167
    n->ackd_timer->hook=ackd_timer_hook;
168
    n->ackd_timer->recurrent=ifa->rxmtint/2;
169
    DBG("%s: Installing ackd timer.\n", p->name);
170
  }
171
  ospf_neigh_sm(n, INM_HELLOREC);
172

    
173
  pnrid=(u32 *)((struct ospf_hello_packet *)(ps+1));
174

    
175
  twoway=0;
176
  for(i=0;i<size-(sizeof(struct ospf_hello_packet));i++)
177
  {
178
    if(ntohl(*(pnrid+i))==p->cf->global->router_id)
179
    {
180
      DBG("%s: Twoway received from %I\n", p->name, faddr);
181
      ospf_neigh_sm(n, INM_2WAYREC);
182
      twoway=1;
183
      break;
184
    }
185
  }
186

    
187
  if(!twoway) ospf_neigh_sm(n, INM_1WAYREC);
188

    
189
  olddr = n->dr;
190
  n->dr = ipa_ntoh(ps->dr);
191
  oldbdr = n->bdr;
192
  n->bdr = ipa_ntoh(ps->bdr);
193
  oldpriority = n->priority;
194
  n->priority = ps->priority;
195

    
196
  /* Check priority change */
197
  if(n->state>=NEIGHBOR_2WAY)
198
  {
199
    if(n->priority!=oldpriority) ospf_int_sm(ifa, ISM_NEICH);
200

    
201
    /* Router is declaring itself ad DR and there is no BDR */
202
    if((ipa_compare(n->ip,n->dr)==0) && (ipa_to_u32(n->bdr)==0)
203
      && (n->state!=NEIGHBOR_FULL))
204
      ospf_int_sm(ifa, ISM_BACKS);
205

    
206
    /* Neighbor is declaring itself as BDR */
207
    if((ipa_compare(n->ip,n->bdr)==0) && (n->state!=NEIGHBOR_FULL))
208
      ospf_int_sm(ifa, ISM_BACKS);
209

    
210
    /* Neighbor is newly declaring itself as DR or BDR */
211
    if(((ipa_compare(n->ip,n->dr)==0) && (ipa_compare(n->dr,olddr)!=0))
212
      || ((ipa_compare(n->ip,n->bdr)==0) && (ipa_compare(n->bdr,oldbdr)!=0)))
213
      ospf_int_sm(ifa, ISM_NEICH);
214

    
215
    /* Neighbor is no more declaring itself as DR or BDR */
216
    if(((ipa_compare(n->ip,olddr)==0) && (ipa_compare(n->dr,olddr)!=0))
217
      || ((ipa_compare(n->ip,oldbdr)==0) && (ipa_compare(n->bdr,oldbdr)!=0)))
218
      ospf_int_sm(ifa, ISM_NEICH);
219
  }
220

    
221
  if(ifa->type!=OSPF_IT_NBMA)
222
  {
223
    if((ifa->priority==0)&&(n->priority>0)) hello_send(NULL,0, n);
224
  }
225
  ospf_neigh_sm(n, INM_HELLOREC);
226
}
227

    
228
void
229
poll_timer_hook(timer *timer)
230
{
231
  hello_send(timer,1, NULL);
232
}
233

    
234
void
235
hello_timer_hook(timer *timer)
236
{
237
  hello_send(timer,0, NULL);
238
}
239

    
240
void
241
hello_send(timer *timer,int poll, struct ospf_neighbor *dirn)
242
{
243
  struct ospf_iface *ifa;
244
  struct ospf_hello_packet *pkt;
245
  struct ospf_packet *op;
246
  struct proto *p;
247
  struct ospf_neighbor *neigh;
248
  u16 length;
249
  u32 *pp;
250
  u8 i;
251

    
252
  if(timer==NULL) ifa=dirn->ifa;
253
  else ifa=(struct ospf_iface *)timer->data;
254

    
255
  if(ifa->stub) return;                /* Don't send any packet on stub iface */
256

    
257
  p=(struct proto *)(ifa->proto);
258
  DBG("%s: Hello/Poll timer fired on interface %s.\n",
259
    p->name, ifa->iface->name);
260
  /* Now we should send a hello packet */
261
  /* First a common packet header */
262
  if(ifa->type!=OSPF_IT_NBMA)
263
  {
264
    pkt=(struct ospf_hello_packet *)(ifa->hello_sk->tbuf);
265
  }
266
  else 
267
  {
268
    pkt=(struct ospf_hello_packet *)(ifa->ip_sk->tbuf);
269
  }
270

    
271
  /* Now fill ospf_hello header */
272
  op=(struct ospf_packet *)pkt;
273

    
274
  fill_ospf_pkt_hdr(ifa, pkt, HELLO_P);
275

    
276
  pkt->netmask=ipa_mkmask(ifa->iface->addr->pxlen);
277
  ipa_hton(pkt->netmask);
278
  pkt->helloint=ntohs(ifa->helloint);
279
  pkt->options=ifa->options;
280
  pkt->priority=ifa->priority;
281
  pkt->deadint=htonl(ifa->deadc*ifa->helloint);
282
  pkt->dr=ifa->drip;
283
  ipa_hton(pkt->dr);
284
  pkt->bdr=ifa->bdrip;
285
  ipa_hton(pkt->bdr);
286

    
287
  /* Fill all neighbors */
288
  i=0;
289
  pp=(u32 *)(((u8 *)pkt)+sizeof(struct ospf_hello_packet));
290
  WALK_LIST (neigh, ifa->neigh_list)
291
  {
292
    *(pp+i)=htonl(neigh->rid);
293
    i++;
294
  }
295

    
296
  length=sizeof(struct ospf_hello_packet)+i*sizeof(u32);
297
  op->length=htons(length);
298

    
299
  ospf_pkt_finalize(ifa, op);
300

    
301
    /* And finally send it :-) */
302
  if(ifa->type!=OSPF_IT_NBMA)
303
  {
304
    sk_send(ifa->hello_sk,length);
305
  }
306
  else        /* NBMA */
307
  {
308
    struct ospf_neighbor *n1;
309
    struct nbma_node *nb;
310
    int send;
311

    
312
    if(timer==NULL)        /* Response to received hello */
313
    {
314
      sk_send_to(ifa->ip_sk, length, dirn->ip, OSPF_PROTO);
315
    }
316
    else
317
    {
318
      int toall=0;
319
      int meeli=0;
320
      if(ifa->state>OSPF_IS_DROTHER) toall=1;
321
      if(ifa->priority>0) meeli=1;
322

    
323
      WALK_LIST(nb,ifa->nbma_list)
324
      {
325
        send=1;
326
        WALK_LIST(n1, ifa->neigh_list)
327
        {
328
          if(ipa_compare(nb->ip,n1->ip)==0)
329
          {
330
            send=0;
331
            break;
332
          }
333
        }
334
        if((poll==1)&&(send))
335
        {
336
          if(toall||(meeli&&nb->eligible))
337
            sk_send_to(ifa->ip_sk, length, nb->ip, OSPF_PROTO);
338
        }
339
      }
340
      if(poll==0)
341
      {
342
        WALK_LIST(n1,ifa->neigh_list)
343
        {
344
          if(toall||(n1->rid==ifa->drid)||(n1->rid==ifa->bdrid)||
345
            (meeli&&(n1->priority>0)))
346
            sk_send_to(ifa->ip_sk, length, n1->ip, OSPF_PROTO);
347
        }
348
      }
349
    }
350
  }
351
  OSPF_TRACE(D_PACKETS, "Hello sent via %s",ifa->iface->name);
352
}
353

    
354
void
355
wait_timer_hook(timer *timer)
356
{
357
  struct ospf_iface *ifa;
358
  struct proto *p;
359

    
360
  ifa=(struct ospf_iface *)timer->data;
361
  p=(struct proto *)(ifa->proto);
362
  OSPF_TRACE(D_EVENTS, "Wait timer fired on interface %s.",
363
    ifa->iface->name);
364
  ospf_int_sm(ifa, ISM_WAITF);
365
}
366