Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / hello.c @ 9669362f

History | View | Annotate | Download (6.29 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999 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(p->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_waittim(struct ospf_iface *ifa)
45
{
46
  tm_start(ifa->wait_timer,ifa->waitint);
47
}
48

    
49
void
50
ospf_hello_rx(struct ospf_hello_packet *ps, struct proto *p,
51
  struct ospf_iface *ifa, int size, ip_addr faddr)
52
{
53
  u32 nrid, *pnrid;
54
  struct ospf_neighbor *neigh,*n;
55
  u8 i,twoway,oldpriority;
56
  u32 olddr,oldbdr;
57
  char *beg=": Bad OSPF hello packet from ", *rec=" received: ";
58

    
59
  nrid=ntohl(((struct ospf_packet *)ps)->routerid);
60

    
61
  if((unsigned)ipa_mklen(ipa_ntoh(ps->netmask))!=ifa->iface->addr->pxlen)
62
  {
63
    log("%s%s%I%s%Ibad netmask %I.\n", p->name, beg, nrid, rec,
64
      ipa_ntoh(ps->netmask));
65
    return;
66
  }
67
  
68
  if(ntohs(ps->helloint)!=ifa->helloint)
69
  {
70
    log("%s%s%I%shello interval mismatch.\n", p->name, beg, nrid, rec);
71
    return;
72
  }
73

    
74
  if(ntohl(ps->deadint)!=ifa->helloint*ifa->deadc)
75
  {
76
    log("%s%s%I%sdead interval mismatch.\n", p->name, beg, nrid, rec);
77
    return;
78
  }
79

    
80
  if(ps->options!=ifa->options)
81
  {
82
    log("%s%s%I%soptions mismatch.\n", p->name, beg, nrid, rec);
83
    return;
84
  }
85

    
86
  if((n=find_neigh(ifa, nrid))==NULL)
87
  {
88
    debug("%s: New neighbor found: %I.\n", p->name,nrid);
89
    n=mb_allocz(p->pool, sizeof(struct ospf_neighbor));
90
    add_tail(&ifa->neigh_list, NODE n);
91
    n->rid=nrid;
92
    n->ip=faddr;
93
    n->dr=ntohl(ps->dr);
94
    n->bdr=ntohl(ps->bdr);
95
    n->priority=ps->priority;
96
    n->options=ps->options;
97
    n->ifa=ifa;
98
    n->adj=0;
99
    n->ldbdes=mb_alloc(p->pool, ifa->iface->mtu);
100
    n->state=NEIGHBOR_DOWN;
101
    install_inactim(n);
102
    n->rxmt_timer=tm_new(p->pool);
103
    n->rxmt_timer->data=n;
104
    n->rxmt_timer->randomize=0;
105
    n->rxmt_timer->hook=rxmt_timer_hook;
106
    n->rxmt_timer->recurrent=ifa->rxmtint;
107
    DBG("%s: Installing rxmt timer.\n", p->name);
108
    n->lsrr_timer=tm_new(p->pool);
109
    n->lsrr_timer->data=n;
110
    n->lsrr_timer->randomize=0;
111
    n->lsrr_timer->hook=lsrr_timer_hook;
112
    n->lsrr_timer->recurrent=ifa->rxmtint;
113
    DBG("%s: Installing lsrr timer.\n", p->name);
114
    init_list(&n->ackl);
115
    n->ackd_timer=tm_new(p->pool);
116
    n->ackd_timer->data=n;
117
    n->ackd_timer->randomize=0;
118
    n->ackd_timer->hook=ackd_timer_hook;
119
    n->ackd_timer->recurrent=ifa->rxmtint/2;        /* FIXME use some config? */
120
    DBG("%s: Installing ackd timer.\n", p->name);
121
  }
122
  ospf_neigh_sm(n, INM_HELLOREC);
123

    
124
  pnrid=(u32 *)((struct ospf_hello_packet *)(ps+1));
125

    
126
  twoway=0;
127
  for(i=0;i<size-(sizeof(struct ospf_hello_packet));i++)
128
  {
129
    if(ntohl(*(pnrid+i))==p->cf->global->router_id)
130
    {
131
      DBG("%s: Twoway received from %I\n", p->name, nrid);
132
      ospf_neigh_sm(n, INM_2WAYREC);
133
      twoway=1;
134
      break;
135
    }
136
  }
137

    
138
  if(!twoway) ospf_neigh_sm(n, INM_1WAYREC);
139

    
140
  olddr = n->dr;
141
  n->dr = ntohl(ps->dr);
142
  oldbdr = n->bdr;
143
  n->bdr = ntohl(ps->bdr);
144
  oldpriority = n->priority;
145
  n->priority = ps->priority;
146

    
147
  /* Check priority change */
148
  if(n->state>=NEIGHBOR_2WAY)
149
  {
150
    if(n->priority!=oldpriority) ospf_int_sm(ifa, ISM_NEICH);
151

    
152
    /* Router is declaring itself ad DR and there is no BDR */
153
    if((n->rid==n->dr) && (n->bdr==0)) ospf_int_sm(ifa, ISM_BACKS);
154

    
155
    /* Neighbor is declaring itself as BDR */
156
    if(n->rid==n->bdr) ospf_int_sm(ifa, ISM_BACKS);
157

    
158
    /* Neighbor is newly declaring itself as DR or BDR */
159
    if(((n->rid==n->dr) && (n->dr!=olddr)) || ((n->rid==n->bdr) &&
160
      (n->bdr!=oldbdr)))
161
      ospf_int_sm(ifa, ISM_NEICH);
162

    
163
    /* Neighbor is no more declaring itself as DR or BDR */
164
    if(((n->rid==olddr) && (n->dr!=olddr)) || ((n->rid==oldbdr) &&
165
      (n->bdr!=oldbdr)))
166
      ospf_int_sm(ifa, ISM_NEICH);
167
  }
168

    
169
  ospf_neigh_sm(n, INM_HELLOREC);
170
}
171

    
172
void
173
hello_timer_hook(timer *timer)
174
{
175
  struct ospf_iface *ifa;
176
  struct ospf_hello_packet *pkt;
177
  struct ospf_packet *op;
178
  struct proto *p;
179
  struct ospf_neighbor *neigh;
180
  u16 length;
181
  u32 *pp;
182
  u8 i;
183

    
184
  ifa=(struct ospf_iface *)timer->data;
185
  p=(struct proto *)(ifa->proto);
186
  DBG("%s: Hello timer fired on interface %s.\n",
187
    p->name, ifa->iface->name);
188
  /* Now we should send a hello packet */
189
  /* First a common packet header */
190
  if(ifa->type!=OSPF_IT_NBMA)
191
  {
192
    /* Now fill ospf_hello header */
193
    pkt=(struct ospf_hello_packet *)(ifa->hello_sk->tbuf);
194
    op=(struct ospf_packet *)pkt;
195

    
196
    fill_ospf_pkt_hdr(ifa, pkt, HELLO);
197

    
198
    pkt->netmask=ipa_mkmask(ifa->iface->addr->pxlen);
199
    ipa_hton(pkt->netmask);
200
    pkt->helloint=ntohs(ifa->helloint);
201
    pkt->options=ifa->options;
202
    pkt->priority=ifa->priority;
203
    pkt->deadint=htonl(ifa->deadc*ifa->helloint);
204
    pkt->dr=htonl(ifa->drid);
205
    pkt->bdr=htonl(ifa->bdrid);
206

    
207
    /* Fill all neighbors */
208
    i=0;
209
    pp=(u32 *)(((u8 *)pkt)+sizeof(struct ospf_hello_packet));
210
    WALK_LIST (neigh, ifa->neigh_list)
211
    {
212
      *(pp+i)=htonl(neigh->rid);
213
      i++;
214
    }
215

    
216
    length=sizeof(struct ospf_hello_packet)+i*sizeof(u32);
217
    op->length=htons(length);
218

    
219
    ospf_pkt_finalize(ifa, op);
220

    
221
    /* And finally send it :-) */
222
    sk_send(ifa->hello_sk,length);
223
  }
224
}
225

    
226
void
227
wait_timer_hook(timer *timer)
228
{
229
  struct ospf_iface *ifa;
230
  struct proto *p;
231

    
232
  ifa=(struct ospf_iface *)timer->data;
233
  p=(struct proto *)(ifa->proto);
234
  debug("%s: Wait timer fired on interface %s.\n",
235
    p->name, ifa->iface->name);
236
  ospf_int_sm(ifa, ISM_WAITF);
237
}
238

    
239
/* Neighbor is inactive for a long time. Remove it. */
240
void
241
neighbor_timer_hook(timer *timer)
242
{
243
  struct ospf_neighbor *n;
244
  struct ospf_iface *ifa;
245
  struct proto *p;
246

    
247
  n=(struct ospf_neighbor *)timer->data;
248
  ifa=n->ifa;
249
  p=(struct proto *)(ifa->proto);
250
  debug("%s: Inactivity timer fired on interface %s for neighbor %I.\n",
251
    p->name, ifa->iface->name, n->rid);
252
  tm_stop(n->inactim);
253
  rfree(n->inactim);
254
  rem_node(NODE n);
255
  mb_free(n);
256
  debug("%s: Deleting neigbor.\n", p->name);
257
  /* FIXME: Go on */
258
}