Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.43 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
#include "ospf.h"
12

    
13

    
14
struct ospf_hello2_packet
15
{
16
  struct ospf_packet hdr;
17
  union ospf_auth auth;
18

    
19
  u32 netmask;
20
  u16 helloint;
21
  u8 options;
22
  u8 priority;
23
  u32 deadint;
24
  u32 dr;
25
  u32 bdr;
26

    
27
  u32 neighbors[];
28
};
29

    
30
struct ospf_hello3_packet
31
{
32
  struct ospf_packet hdr;
33

    
34
  u32 iface_id;
35
  u8 priority;
36
  u8 options3;
37
  u8 options2;
38
  u8 options;
39
  u16 helloint;
40
  u16 deadint;
41
  u32 dr;
42
  u32 bdr;
43

    
44
  u32 neighbors[];
45
};
46

    
47

    
48
void
49
ospf_send_hello(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn)
50
{
51
  struct ospf_proto *p = ifa->oa->po;
52
  struct ospf_packet *pkt;
53
  struct ospf_neighbor *neigh, *n1;
54
  struct nbma_node *nb;
55
  u32 *neighbors;
56
  uint length;
57
  int i, max;
58

    
59
  if (ifa->state <= OSPF_IS_LOOP)
60
    return;
61

    
62
  if (ifa->stub)
63
    return;
64

    
65

    
66
  pkt = ospf_tx_buffer(ifa);
67
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
68

    
69
  if (ospf_is_v2(p))
70
  {
71
    struct ospf_hello2_packet *ps = (void *) pkt;
72

    
73
    if ((ifa->type == OSPF_IT_VLINK) ||
74
        ((ifa->type == OSPF_IT_PTP) && !ifa->ptp_netmask))
75
      ps->netmask = 0;
76
    else
77
      ps->netmask = htonl(u32_mkmask(ifa->addr->pxlen));
78

    
79
    ps->helloint = ntohs(ifa->helloint);
80
    ps->options = ifa->oa->options;
81
    ps->priority = ifa->priority;
82
    ps->deadint = htonl(ifa->deadint);
83
    ps->dr = htonl(ipa_to_u32(ifa->drip));
84
    ps->bdr = htonl(ipa_to_u32(ifa->bdrip));
85

    
86
    length = sizeof(struct ospf_hello2_packet);
87
    neighbors = ps->neighbors;
88
  }
89
  else
90
  {
91
    struct ospf_hello3_packet *ps = (void *) pkt;
92

    
93
    ps->iface_id = htonl(ifa->iface_id);
94
    ps->priority = ifa->priority;
95
    ps->options3 = ifa->oa->options >> 16;
96
    ps->options2 = ifa->oa->options >> 8;
97
    ps->options = ifa->oa->options;
98
    ps->helloint = ntohs(ifa->helloint);
99
    ps->deadint = htons(ifa->deadint);
100
    ps->dr = htonl(ifa->drid);
101
    ps->bdr = htonl(ifa->bdrid);
102

    
103
    length = sizeof(struct ospf_hello3_packet);
104
    neighbors = ps->neighbors;
105
  }
106

    
107
  i = 0;
108
  max = (ospf_pkt_maxsize(ifa) - length) / sizeof(u32);
109

    
110
  /* Fill all neighbors */
111
  if (kind != OHS_SHUTDOWN)
112
  {
113
    WALK_LIST(neigh, ifa->neigh_list)
114
    {
115
      if (i == max)
116
      {
117
         log(L_WARN "%s: Too many neighbors on interface %s", p->p.name, ifa->ifname);
118
        break;
119
      }
120
      neighbors[i] = htonl(neigh->rid);
121
      i++;
122
    }
123
  }
124

    
125
  length += i * sizeof(u32);
126
  pkt->length = htons(length);
127

    
128
  OSPF_TRACE(D_PACKETS, "HELLO packet sent via %s", ifa->ifname);
129

    
130
  switch(ifa->type)
131
  {
132
  case OSPF_IT_BCAST:
133
  case OSPF_IT_PTP:
134
    ospf_send_to_all(ifa);
135
    break;
136

    
137
  case OSPF_IT_NBMA:
138
    if (dirn)                /* Response to received hello */
139
    {
140
      ospf_send_to(ifa, dirn->ip);
141
      break;
142
    }
143

    
144
    int to_all = ifa->state > OSPF_IS_DROTHER;
145
    int me_elig = ifa->priority > 0;
146
 
147
    if (kind == OHS_POLL)        /* Poll timer */
148
    {
149
      WALK_LIST(nb, ifa->nbma_list)
150
        if (!nb->found && (to_all || (me_elig && nb->eligible)))
151
          ospf_send_to(ifa, nb->ip);
152
    }
153
    else                        /* Hello timer */
154
    {
155
      WALK_LIST(n1, ifa->neigh_list)
156
        if (to_all || (me_elig && (n1->priority > 0)) ||
157
            (n1->rid == ifa->drid) || (n1->rid == ifa->bdrid))
158
          ospf_send_to(ifa, n1->ip);
159
    }
160
    break;
161

    
162
  case OSPF_IT_PTMP:
163
    WALK_LIST(n1, ifa->neigh_list)
164
      ospf_send_to(ifa, n1->ip);
165

    
166
    WALK_LIST(nb, ifa->nbma_list)
167
      if (!nb->found)
168
        ospf_send_to(ifa, nb->ip);
169

    
170
    /* If there is no other target, we also send HELLO packet to the other end */
171
    if (ipa_nonzero(ifa->addr->opposite) && !ifa->strictnbma &&
172
        EMPTY_LIST(ifa->neigh_list) && EMPTY_LIST(ifa->nbma_list))
173
      ospf_send_to(ifa, ifa->addr->opposite);
174
    break;
175

    
176
  case OSPF_IT_VLINK:
177
    ospf_send_to(ifa, ifa->vip);
178
    break;
179

    
180
  default:
181
    bug("Bug in ospf_send_hello()");
182
  }
183
}
184

    
185

    
186
void
187
ospf_receive_hello(struct ospf_packet *pkt, struct ospf_iface *ifa,
188
                   struct ospf_neighbor *n, ip_addr faddr)
189
{
190
  struct ospf_proto *p = ifa->oa->po;
191
  char *beg = "OSPF: Bad HELLO packet from ";
192
  u32 rcv_iface_id, rcv_helloint, rcv_deadint, rcv_dr, rcv_bdr;
193
  u8 rcv_options, rcv_priority;
194
  u32 *neighbors;
195
  u32 neigh_count;
196
  uint plen, i;
197

    
198
  /* RFC 2328 10.5 */
199

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

    
202
  plen = ntohs(pkt->length);
203

    
204
  if (ospf_is_v2(p))
205
  {
206
    struct ospf_hello2_packet *ps = (void *) pkt;
207

    
208
    if (plen < sizeof(struct ospf_hello2_packet))
209
    {
210
      log(L_ERR "%s%I - too short (%u B)", beg, faddr, plen);
211
      return;
212
    }
213

    
214
    rcv_iface_id = 0;
215
    rcv_helloint = ntohs(ps->helloint);
216
    rcv_deadint = ntohl(ps->deadint);
217
    rcv_dr = ntohl(ps->dr);
218
    rcv_bdr = ntohl(ps->bdr);
219
    rcv_options = ps->options;
220
    rcv_priority = ps->priority;
221

    
222
    int pxlen = u32_masklen(ntohl(ps->netmask));
223
    if ((ifa->type != OSPF_IT_VLINK) &&
224
        (ifa->type != OSPF_IT_PTP) &&
225
        (pxlen != ifa->addr->pxlen))
226
    {
227
      log(L_ERR "%s%I - prefix length mismatch (%d)", beg, faddr, pxlen);
228
      return;
229
    }
230

    
231
    neighbors = ps->neighbors;
232
    neigh_count = (plen - sizeof(struct ospf_hello2_packet)) / sizeof(u32);
233
  }
234
  else /* OSPFv3 */
235
  {
236
    struct ospf_hello3_packet *ps = (void *) pkt;
237

    
238
    if (plen < sizeof(struct ospf_hello3_packet))
239
    {
240
      log(L_ERR "%s%I - too short (%u B)", beg, faddr, plen);
241
      return;
242
    }
243

    
244
    rcv_iface_id = ntohl(ps->iface_id);
245
    rcv_helloint = ntohs(ps->helloint);
246
    rcv_deadint = ntohs(ps->deadint);
247
    rcv_dr = ntohl(ps->dr);
248
    rcv_bdr = ntohl(ps->bdr);
249
    rcv_options = ps->options;
250
    rcv_priority = ps->priority;
251

    
252
    neighbors = ps->neighbors;
253
    neigh_count = (plen - sizeof(struct ospf_hello3_packet)) / sizeof(u32);
254
  }
255

    
256
  if (rcv_helloint != ifa->helloint)
257
  {
258
    log(L_ERR "%s%I - hello interval mismatch (%d)", beg, faddr, rcv_helloint);
259
    return;
260
  }
261

    
262
  if (rcv_deadint != ifa->deadint)
263
  {
264
    log(L_ERR "%s%I - dead interval mismatch (%d)", beg, faddr, rcv_deadint);
265
    return;
266
  }
267

    
268
  /* Check whether bits E, N match */
269
  if ((rcv_options ^ ifa->oa->options) & (OPT_E | OPT_N))
270
  {
271
    log(L_ERR "%s%I - area type mismatch (%x)", beg, faddr, rcv_options);
272
    return;
273
  }
274

    
275
  /* Check consistency of existing neighbor entry */
276
  if (n)
277
  {
278
    uint t = ifa->type;
279
    if (ospf_is_v2(p) && ((t == OSPF_IT_BCAST) || (t == OSPF_IT_NBMA) || (t == OSPF_IT_PTMP)))
280
    {
281
      /* Neighbor identified by IP address; Router ID may change */
282
      if (n->rid != ntohl(pkt->routerid))
283
      {
284
        OSPF_TRACE(D_EVENTS, "Neighbor %I has changed Router ID from %R to %R",
285
                   n->ip, n->rid, ntohl(pkt->routerid));
286
        ospf_neigh_remove(n);
287
        n = NULL;
288
      }
289
    }
290
    else /* OSPFv3 or OSPFv2/PtP */
291
    {
292
      /* Neighbor identified by Router ID; IP address may change */
293
      if (!ipa_equal(faddr, n->ip))
294
      {
295
        OSPF_TRACE(D_EVENTS, "Neighbor address changed from %I to %I", n->ip, faddr);
296
        n->ip = faddr;
297
      }
298
    }
299
  }
300

    
301
  if (!n)
302
  {
303
    if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
304
    {
305
      struct nbma_node *nn = find_nbma_node(ifa, faddr);
306

    
307
      if (!nn && ifa->strictnbma)
308
      {
309
        log(L_WARN "Ignoring new neighbor: %I on %s", faddr, ifa->ifname);
310
        return;
311
      }
312

    
313
      if (nn && (ifa->type == OSPF_IT_NBMA) &&
314
          (((rcv_priority == 0) && nn->eligible) ||
315
           ((rcv_priority > 0) && !nn->eligible)))
316
      {
317
        log(L_ERR "Eligibility mismatch for neighbor: %I on %s", faddr, ifa->ifname);
318
        return;
319
      }
320

    
321
      if (nn)
322
        nn->found = 1;
323
    }
324

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

    
327
    n = ospf_neighbor_new(ifa);
328

    
329
    n->rid = ntohl(pkt->routerid);
330
    n->ip = faddr;
331
    n->dr = rcv_dr;
332
    n->bdr = rcv_bdr;
333
    n->priority = rcv_priority;
334
    n->iface_id = rcv_iface_id;
335

    
336
    if (n->ifa->cf->bfd)
337
      ospf_neigh_update_bfd(n, n->ifa->bfd);
338
  }
339

    
340
  u32 n_id = ospf_is_v2(p) ? ipa_to_u32(n->ip) : n->rid;
341

    
342
  u32 old_dr = n->dr;
343
  u32 old_bdr = n->bdr;
344
  u32 old_priority = n->priority;
345
  u32 old_iface_id = n->iface_id;
346

    
347
  n->dr = rcv_dr;
348
  n->bdr = rcv_bdr;
349
  n->priority = rcv_priority;
350
  n->iface_id = rcv_iface_id;
351

    
352

    
353
  /* Update inactivity timer */
354
  ospf_neigh_sm(n, INM_HELLOREC);
355

    
356
  /* RFC 2328 9.5.1 - non-eligible routers reply to hello on NBMA nets */
357
  if (ifa->type == OSPF_IT_NBMA)
358
    if ((ifa->priority == 0) && (n->priority > 0))
359
      ospf_send_hello(n->ifa, OHS_HELLO, n);
360
 
361

    
362
  /* Examine list of neighbors */
363
  for (i = 0; i < neigh_count; i++)
364
    if (neighbors[i] == htonl(p->router_id))
365
      goto found_self;
366

    
367
  ospf_neigh_sm(n, INM_1WAYREC);
368
  return;
369

    
370
 found_self:
371
  ospf_neigh_sm(n, INM_2WAYREC);
372

    
373

    
374
  if (n->iface_id != old_iface_id)
375
  {
376
    /* If neighbor is DR, also update cached DR interface ID */
377
    if (ifa->drid == n->rid)
378
      ifa->dr_iface_id = n->iface_id;
379

    
380
    /* RFC 5340 4.4.3 Event 4 - change of neighbor's interface ID */
381
    ospf_notify_rt_lsa(ifa->oa);
382

    
383
    /* Missed in RFC 5340 4.4.3 Event 4 - (Px-)Net-LSA uses iface_id to ref Link-LSAs */
384
    ospf_notify_net_lsa(ifa);
385
  }
386

    
387
  if (ifa->state == OSPF_IS_WAITING)
388
  {
389
    /* Neighbor is declaring itself DR (and there is no BDR) or as BDR */
390
    if (((n->dr == n_id) && (n->bdr == 0)) || (n->bdr == n_id))
391
      ospf_iface_sm(ifa, ISM_BACKS);
392
  }
393
  else if (ifa->state >= OSPF_IS_DROTHER)
394
  {
395
    /* Neighbor changed priority or started/stopped declaring itself as DR/BDR */
396
    if ((n->priority != old_priority) ||
397
        ((n->dr == n_id) && (old_dr != n_id)) ||
398
        ((n->dr != n_id) && (old_dr == n_id)) ||
399
        ((n->bdr == n_id) && (old_bdr != n_id)) ||
400
        ((n->bdr != n_id) && (old_bdr == n_id)))
401
      ospf_iface_sm(ifa, ISM_NEICH);
402
  }
403
}