Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / hello.c @ df49d4e1

History | View | Annotate | Download (6.02 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
  debug("%s: Received hello from %I via %s\n",p->name,faddr,ifa->iface->name);
62

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

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

    
82
  if(ps->options!=ifa->options)
83
  {
84
    log("%s%s%I%soptions mismatch.", p->name, beg, faddr, rec);
85
    return;
86
  }
87

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

    
127
  pnrid=(u32 *)((struct ospf_hello_packet *)(ps+1));
128

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

    
141
  if(!twoway) ospf_neigh_sm(n, INM_1WAYREC);
142

    
143
  olddr = n->dr;
144
  n->dr = ntohl(ps->dr);
145
  oldbdr = n->bdr;
146
  n->bdr = ntohl(ps->bdr);
147
  oldpriority = n->priority;
148
  n->priority = ps->priority;
149

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

    
155
    /* Router is declaring itself ad DR and there is no BDR */
156
    if((n->rid==n->dr) && (n->bdr==0) && (n->state!=NEIGHBOR_FULL))
157
      ospf_int_sm(ifa, ISM_BACKS);
158

    
159
    /* Neighbor is declaring itself as BDR */
160
    if((n->rid==n->bdr) && (n->state!=NEIGHBOR_FULL))
161
      ospf_int_sm(ifa, ISM_BACKS);
162

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

    
168
    /* Neighbor is no more declaring itself as DR or BDR */
169
    if(((n->rid==olddr) && (n->dr!=olddr)) || ((n->rid==oldbdr) &&
170
      (n->bdr!=oldbdr)))
171
      ospf_int_sm(ifa, ISM_NEICH);
172
  }
173

    
174
  ospf_neigh_sm(n, INM_HELLOREC);
175
}
176

    
177
void
178
hello_timer_hook(timer *timer)
179
{
180
  struct ospf_iface *ifa;
181
  struct ospf_hello_packet *pkt;
182
  struct ospf_packet *op;
183
  struct proto *p;
184
  struct ospf_neighbor *neigh;
185
  u16 length;
186
  u32 *pp;
187
  u8 i;
188

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

    
201
    fill_ospf_pkt_hdr(ifa, pkt, HELLO_P);
202

    
203
    pkt->netmask=ipa_mkmask(ifa->iface->addr->pxlen);
204
    ipa_hton(pkt->netmask);
205
    pkt->helloint=ntohs(ifa->helloint);
206
    pkt->options=ifa->options;
207
    pkt->priority=ifa->priority;
208
    pkt->deadint=htonl(ifa->deadc*ifa->helloint);
209
    pkt->dr=htonl(ifa->drid);
210
    pkt->bdr=htonl(ifa->bdrid);
211

    
212
    /* Fill all neighbors */
213
    i=0;
214
    pp=(u32 *)(((u8 *)pkt)+sizeof(struct ospf_hello_packet));
215
    WALK_LIST (neigh, ifa->neigh_list)
216
    {
217
      *(pp+i)=htonl(neigh->rid);
218
      i++;
219
    }
220

    
221
    length=sizeof(struct ospf_hello_packet)+i*sizeof(u32);
222
    op->length=htons(length);
223

    
224
    ospf_pkt_finalize(ifa, op);
225

    
226
    /* And finally send it :-) */
227
    sk_send(ifa->hello_sk,length);
228
  }
229

    
230
  debug("%s: Hello sent via %s\n",p->name,ifa->iface->name);
231
}
232

    
233
void
234
wait_timer_hook(timer *timer)
235
{
236
  struct ospf_iface *ifa;
237
  struct proto *p;
238

    
239
  ifa=(struct ospf_iface *)timer->data;
240
  p=(struct proto *)(ifa->proto);
241
  debug("%s: Wait timer fired on interface %s.\n",
242
    p->name, ifa->iface->name);
243
  ospf_int_sm(ifa, ISM_WAITF);
244
}
245