Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / hello.c @ 48e5f32d

History | View | Annotate | Download (8.46 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 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

    
12
#ifdef OSPFv2
13
struct ospf_hello_packet
14
{
15
  struct ospf_packet ospf_packet;
16
  ip_addr netmask;
17
  u16 helloint;
18
  u8 options;
19
  u8 priority;
20
  u32 deadint;
21
  u32 dr;
22
  u32 bdr;
23
};
24
#endif
25

    
26

    
27
#ifdef OSPFv3
28
struct ospf_hello_packet
29
{
30
  struct ospf_packet ospf_packet;
31
  u32 iface_id;
32
  u8 priority;
33
  u8 options3;
34
  u8 options2;
35
  u8 options;
36
  u16 helloint;
37
  u16 deadint;
38
  u32 dr;
39
  u32 bdr;
40
};
41
#endif
42

    
43

    
44
void
45
ospf_hello_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
46
                   struct ospf_neighbor *n, ip_addr faddr)
47
{
48
  struct proto_ospf *po = ifa->oa->po;
49
  struct proto *p = &po->proto;
50
  char *beg = "OSPF: Bad HELLO packet from ";
51
  unsigned int size, i, twoway, peers;
52
  u32 tmp;
53
  u32 *pnrid;
54

    
55
  size = ntohs(ps_i->length);
56
  if (size < sizeof(struct ospf_hello_packet))
57
  {
58
    log(L_ERR "%s%I - too short (%u B)", beg, faddr, size);
59
    return;
60
  }
61

    
62
  struct ospf_hello_packet *ps = (void *) ps_i;
63

    
64
  OSPF_TRACE(D_PACKETS, "HELLO packet received from %I via %s", faddr, ifa->ifname);
65

    
66
#ifdef OSPFv2
67
  ip_addr mask = ps->netmask;
68
  ipa_ntoh(mask);
69
  if ((ifa->type != OSPF_IT_VLINK) &&
70
      (ifa->type != OSPF_IT_PTP) &&
71
      !ipa_equal(mask, ipa_mkmask(ifa->addr->pxlen)))
72
  {
73
    log(L_ERR "%s%I - netmask mismatch (%I)", beg, faddr, mask);
74
    return;
75
  }
76
#endif
77

    
78
  tmp = ntohs(ps->helloint);
79
  if (tmp != ifa->helloint)
80
  {
81
    log(L_ERR "%s%I - hello interval mismatch (%d)", beg, faddr, tmp);
82
    return;
83
  }
84

    
85
#ifdef OSPFv2
86
  tmp = ntohl(ps->deadint);
87
#else /* OSPFv3 */
88
  tmp = ntohs(ps->deadint);
89
#endif
90
  if (tmp != ifa->deadint)
91
  {
92
    log(L_ERR "%s%I - dead interval mismatch (%d)", beg, faddr, tmp);
93
    return;
94
  }
95

    
96
  /* Check whether bits E, N match */
97
  if ((ps->options ^ ifa->oa->options) & (OPT_E | OPT_N))
98
  {
99
    log(L_ERR "%s%I - area type mismatch (%x)", beg, faddr, ps->options);
100
    return;
101
  }
102

    
103
#ifdef OSPFv2
104
  if (n && (n->rid != ntohl(ps_i->routerid)))
105
  {
106
    OSPF_TRACE(D_EVENTS,
107
        "Neighbor %I has changed router id from %R to %R.",
108
             n->ip, n->rid, ntohl(ps_i->routerid));
109
    ospf_neigh_remove(n);
110
    n = NULL;
111
  }
112
#endif
113

    
114
  if (!n)
115
  {
116
    if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
117
    {
118
      struct nbma_node *nn = find_nbma_node(ifa, faddr);
119

    
120
      if (!nn && ifa->strictnbma)
121
      {
122
        log(L_WARN "Ignoring new neighbor: %I on %s", faddr, ifa->ifname);
123
        return;
124
      }
125

    
126
      if (nn && (ifa->type == OSPF_IT_NBMA) &&
127
          (((ps->priority == 0) && nn->eligible) ||
128
           ((ps->priority > 0) && !nn->eligible)))
129
      {
130
        log(L_ERR "Eligibility mismatch for neighbor: %I on %s", faddr, ifa->ifname);
131
        return;
132
      }
133

    
134
      if (nn)
135
        nn->found = 1;
136
    }
137

    
138
    OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s", faddr, ifa->ifname);
139

    
140
    n = ospf_neighbor_new(ifa);
141

    
142
    n->rid = ntohl(ps_i->routerid);
143
    n->ip = faddr;
144
    n->dr = ntohl(ps->dr);
145
    n->bdr = ntohl(ps->bdr);
146
    n->priority = ps->priority;
147
#ifdef OSPFv3
148
    n->iface_id = ntohl(ps->iface_id);
149
#endif
150

    
151
    if (n->ifa->cf->bfd)
152
      ospf_neigh_update_bfd(n, n->ifa->bfd);
153
  }
154
#ifdef OSPFv3        /* NOTE: this could also be relevant for OSPFv2 on PtP ifaces */
155
  else if (!ipa_equal(faddr, n->ip))
156
  {
157
    OSPF_TRACE(D_EVENTS, "Neighbor address changed from %I to %I", n->ip, faddr);
158
    n->ip = faddr;
159
  }
160
#endif
161

    
162
  ospf_neigh_sm(n, INM_HELLOREC);
163

    
164
  pnrid = (u32 *) ((struct ospf_hello_packet *) (ps + 1));
165

    
166
  peers = (size - sizeof(struct ospf_hello_packet))/ sizeof(u32);
167

    
168
  twoway = 0;
169
  for (i = 0; i < peers; i++)
170
  {
171
    if (ntohl(pnrid[i]) == po->router_id)
172
    {
173
      DBG("%s: Twoway received from %I\n", p->name, faddr);
174
      ospf_neigh_sm(n, INM_2WAYREC);
175
      twoway = 1;
176
      break;
177
    }
178
  }
179

    
180
  if (!twoway)
181
    ospf_neigh_sm(n, INM_1WAYREC);
182

    
183
  u32 olddr = n->dr;
184
  u32 oldbdr = n->bdr;
185
  u32 oldpriority = n->priority;
186
#ifdef OSPFv3
187
  u32 oldiface_id = n->iface_id;
188
#endif
189

    
190
  n->dr = ntohl(ps->dr);
191
  n->bdr = ntohl(ps->bdr);
192
  n->priority = ps->priority;
193
#ifdef OSPFv3
194
  n->iface_id = ntohl(ps->iface_id);
195
#endif
196

    
197

    
198
  /* Check priority change */
199
  if (n->state >= NEIGHBOR_2WAY)
200
  {
201
#ifdef OSPFv2
202
    u32 neigh = ipa_to_u32(n->ip);
203
#else /* OSPFv3 */
204
    u32 neigh = n->rid;
205
#endif
206

    
207
    if (n->priority != oldpriority)
208
      ospf_iface_sm(ifa, ISM_NEICH);
209

    
210
#ifdef OSPFv3
211
    if (n->iface_id != oldiface_id)
212
      ospf_iface_sm(ifa, ISM_NEICH);
213
#endif
214

    
215
    /* Neighbor is declaring itself ad DR and there is no BDR */
216
    if ((n->dr == neigh) && (n->bdr == 0)
217
        && (n->state != NEIGHBOR_FULL))
218
      ospf_iface_sm(ifa, ISM_BACKS);
219

    
220
    /* Neighbor is declaring itself as BDR */
221
    if ((n->bdr == neigh) && (n->state != NEIGHBOR_FULL))
222
      ospf_iface_sm(ifa, ISM_BACKS);
223

    
224
    /* Neighbor is newly declaring itself as DR or BDR */
225
    if (((n->dr == neigh) && (n->dr != olddr))
226
        || ((n->bdr == neigh) && (n->bdr != oldbdr)))
227
      ospf_iface_sm(ifa, ISM_NEICH);
228

    
229
    /* Neighbor is no more declaring itself as DR or BDR */
230
    if (((olddr == neigh) && (n->dr != olddr))
231
        || ((oldbdr == neigh) && (n->bdr != oldbdr)))
232
      ospf_iface_sm(ifa, ISM_NEICH);
233
  }
234

    
235
  if (ifa->type == OSPF_IT_NBMA)
236
  {
237
    if ((ifa->priority == 0) && (n->priority > 0))
238
      ospf_hello_send(n->ifa, OHS_HELLO, n);
239
  }
240
  ospf_neigh_sm(n, INM_HELLOREC);
241
}
242

    
243
void
244
ospf_hello_send(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn)
245
{
246
  struct ospf_hello_packet *pkt;
247
  struct ospf_packet *op;
248
  struct proto *p;
249
  struct ospf_neighbor *neigh, *n1;
250
  u16 length;
251
  int i;
252
  struct nbma_node *nb;
253

    
254
  if (ifa->state <= OSPF_IS_LOOP)
255
    return;
256

    
257
  if (ifa->stub)
258
    return;                        /* Don't send any packet on stub iface */
259

    
260
  p = (struct proto *) (ifa->oa->po);
261
  DBG("%s: Hello/Poll timer fired on interface %s with IP %I\n",
262
      p->name, ifa->ifname, ifa->addr->ip);
263

    
264
  /* Now we should send a hello packet */
265
  pkt = ospf_tx_buffer(ifa);
266
  op = &pkt->ospf_packet;
267

    
268
  /* Now fill ospf_hello header */
269
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
270

    
271
#ifdef OSPFv2
272
  pkt->netmask = ipa_mkmask(ifa->addr->pxlen);
273
  ipa_hton(pkt->netmask);
274
  if ((ifa->type == OSPF_IT_VLINK) ||
275
      ((ifa->type == OSPF_IT_PTP) && !ifa->ptp_netmask))
276
    pkt->netmask = IPA_NONE;
277
#endif
278

    
279
  pkt->helloint = ntohs(ifa->helloint);
280
  pkt->priority = ifa->priority;
281

    
282
#ifdef OSPFv3
283
  pkt->iface_id = htonl(ifa->iface_id);
284

    
285
  pkt->options3 = ifa->oa->options >> 16;
286
  pkt->options2 = ifa->oa->options >> 8;
287
#endif
288
  pkt->options = ifa->oa->options;
289

    
290
#ifdef OSPFv2
291
  pkt->deadint = htonl(ifa->deadint);
292
  pkt->dr = htonl(ipa_to_u32(ifa->drip));
293
  pkt->bdr = htonl(ipa_to_u32(ifa->bdrip));
294
#else /* OSPFv3 */
295
  pkt->deadint = htons(ifa->deadint);
296
  pkt->dr = htonl(ifa->drid);
297
  pkt->bdr = htonl(ifa->bdrid);
298
#endif
299

    
300
  /* Fill all neighbors */
301
  i = 0;
302

    
303
  if (kind != OHS_SHUTDOWN)
304
  {
305
    u32 *pp = (u32 *) (((u8 *) pkt) + sizeof(struct ospf_hello_packet));
306
    WALK_LIST(neigh, ifa->neigh_list)
307
    {
308
      if ((i+1) * sizeof(u32) + sizeof(struct ospf_hello_packet) > ospf_pkt_maxsize(ifa))
309
      {
310
        log(L_WARN "%s: Too many neighbors on interface %s", p->name, ifa->ifname);
311
        break;
312
      }
313
      *(pp + i) = htonl(neigh->rid);
314
      i++;
315
    }
316
  }
317

    
318
  length = sizeof(struct ospf_hello_packet) + i * sizeof(u32);
319
  op->length = htons(length);
320

    
321
  switch(ifa->type)
322
  {
323
  case OSPF_IT_BCAST:
324
  case OSPF_IT_PTP:
325
    ospf_send_to_all(ifa);
326
    break;
327

    
328
  case OSPF_IT_NBMA:
329
    if (dirn)                /* Response to received hello */
330
    {
331
      ospf_send_to(ifa, dirn->ip);
332
      break;
333
    }
334

    
335
    int to_all = ifa->state > OSPF_IS_DROTHER;
336
    int me_elig = ifa->priority > 0;
337
 
338
    if (kind == OHS_POLL)        /* Poll timer */
339
    {
340
      WALK_LIST(nb, ifa->nbma_list)
341
        if (!nb->found && (to_all || (me_elig && nb->eligible)))
342
          ospf_send_to(ifa, nb->ip);
343
    }
344
    else                        /* Hello timer */
345
    {
346
      WALK_LIST(n1, ifa->neigh_list)
347
        if (to_all || (me_elig && (n1->priority > 0)) ||
348
            (n1->rid == ifa->drid) || (n1->rid == ifa->bdrid))
349
          ospf_send_to(ifa, n1->ip);
350
    }
351
    break;
352

    
353
  case OSPF_IT_PTMP:
354
    WALK_LIST(n1, ifa->neigh_list)
355
      ospf_send_to(ifa, n1->ip);
356

    
357
    WALK_LIST(nb, ifa->nbma_list)
358
      if (!nb->found)
359
        ospf_send_to(ifa, nb->ip);
360

    
361
    /* If there is no other target, we also send HELLO packet to the other end */
362
    if (ipa_nonzero(ifa->addr->opposite) && !ifa->strictnbma &&
363
        EMPTY_LIST(ifa->neigh_list) && EMPTY_LIST(ifa->nbma_list))
364
      ospf_send_to(ifa, ifa->addr->opposite);
365
    break;
366

    
367
  case OSPF_IT_VLINK:
368
    ospf_send_to(ifa, ifa->vip);
369
    break;
370

    
371
  default:
372
    bug("Bug in ospf_hello_send()");
373
  }
374

    
375
  OSPF_TRACE(D_PACKETS, "HELLO packet sent via %s", ifa->ifname);
376
}