Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.33 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(p, 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 %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
  const char *err_dsc = NULL;
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, err_val = 0;
197

    
198
  /* RFC 2328 10.5 */
199

    
200
  /*
201
   * We may not yet havethe associate neighbor, so we use Router ID from the
202
   * packet instead of one from the neighbor structure for log messages.
203
   */
204
  u32 rcv_rid = ntohl(pkt->routerid);
205
  OSPF_TRACE(D_PACKETS, "HELLO packet received from nbr %R on %s", rcv_rid, ifa->ifname);
206

    
207
  plen = ntohs(pkt->length);
208

    
209
  if (ospf_is_v2(p))
210
  {
211
    struct ospf_hello2_packet *ps = (void *) pkt;
212

    
213
    if (plen < sizeof(struct ospf_hello2_packet))
214
      DROP("too short", plen);
215

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

    
224
    int pxlen = u32_masklen(ntohl(ps->netmask));
225
    if ((ifa->type != OSPF_IT_VLINK) &&
226
        (ifa->type != OSPF_IT_PTP) &&
227
        (pxlen != ifa->addr->pxlen))
228
      DROP("prefix length mismatch", pxlen);
229

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

    
237
    if (plen < sizeof(struct ospf_hello3_packet))
238
      DROP("too short", plen);
239

    
240
    rcv_iface_id = ntohl(ps->iface_id);
241
    rcv_helloint = ntohs(ps->helloint);
242
    rcv_deadint = ntohs(ps->deadint);
243
    rcv_dr = ntohl(ps->dr);
244
    rcv_bdr = ntohl(ps->bdr);
245
    rcv_options = ps->options;
246
    rcv_priority = ps->priority;
247

    
248
    neighbors = ps->neighbors;
249
    neigh_count = (plen - sizeof(struct ospf_hello3_packet)) / sizeof(u32);
250
  }
251

    
252
  if (rcv_helloint != ifa->helloint)
253
    DROP("hello interval mismatch", rcv_helloint);
254

    
255
  if (rcv_deadint != ifa->deadint)
256
    DROP("dead interval mismatch", rcv_deadint);
257

    
258
  /* Check whether bits E, N match */
259
  if ((rcv_options ^ ifa->oa->options) & (OPT_E | OPT_N))
260
    DROP("area type mismatch", rcv_options);
261

    
262
  /* Check consistency of existing neighbor entry */
263
  if (n)
264
  {
265
    uint t = ifa->type;
266
    if (ospf_is_v2(p) && ((t == OSPF_IT_BCAST) || (t == OSPF_IT_NBMA) || (t == OSPF_IT_PTMP)))
267
    {
268
      /* Neighbor identified by IP address; Router ID may change */
269
      if (n->rid != rcv_rid)
270
      {
271
        OSPF_TRACE(D_EVENTS, "Neighbor %R on %s changed Router ID to %R",
272
                   n->rid, ifa->ifname, rcv_rid);
273
        ospf_neigh_sm(n, INM_KILLNBR);
274
        n = NULL;
275
      }
276
    }
277
    else /* OSPFv3 or OSPFv2/PtP */
278
    {
279
      /* Neighbor identified by Router ID; IP address may change */
280
      if (!ipa_equal(faddr, n->ip))
281
      {
282
        OSPF_TRACE(D_EVENTS, "Neighbor %R on %s changed IP address to %I",
283
                   n->rid, ifa->ifname, n->ip, faddr);
284
        n->ip = faddr;
285
      }
286
    }
287
  }
288

    
289
  if (!n)
290
  {
291
    if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
292
    {
293
      struct nbma_node *nn = find_nbma_node(ifa, faddr);
294

    
295
      if (!nn && ifa->strictnbma)
296
        DROP1("new neighbor denied");
297

    
298
      if (nn && (ifa->type == OSPF_IT_NBMA) &&
299
          (((rcv_priority == 0) && nn->eligible) ||
300
           ((rcv_priority > 0) && !nn->eligible)))
301
        DROP("eligibility mismatch", rcv_priority);
302

    
303
      if (nn)
304
        nn->found = 1;
305
    }
306

    
307
    OSPF_TRACE(D_EVENTS, "New neighbor %R on %s, IP address %I",
308
               rcv_rid, ifa->ifname, faddr);
309

    
310
    n = ospf_neighbor_new(ifa);
311

    
312
    n->rid = rcv_rid;
313
    n->ip = faddr;
314
    n->dr = rcv_dr;
315
    n->bdr = rcv_bdr;
316
    n->priority = rcv_priority;
317
    n->iface_id = rcv_iface_id;
318

    
319
    if (n->ifa->cf->bfd)
320
      ospf_neigh_update_bfd(n, n->ifa->bfd);
321
  }
322

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

    
325
  u32 old_dr = n->dr;
326
  u32 old_bdr = n->bdr;
327
  u32 old_priority = n->priority;
328
  u32 old_iface_id = n->iface_id;
329

    
330
  n->dr = rcv_dr;
331
  n->bdr = rcv_bdr;
332
  n->priority = rcv_priority;
333
  n->iface_id = rcv_iface_id;
334

    
335

    
336
  /* Update inactivity timer */
337
  ospf_neigh_sm(n, INM_HELLOREC);
338

    
339
  /* RFC 2328 9.5.1 - non-eligible routers reply to hello on NBMA nets */
340
  if (ifa->type == OSPF_IT_NBMA)
341
    if ((ifa->priority == 0) && (n->priority > 0))
342
      ospf_send_hello(n->ifa, OHS_HELLO, n);
343

    
344

    
345
  /* Examine list of neighbors */
346
  for (i = 0; i < neigh_count; i++)
347
    if (neighbors[i] == htonl(p->router_id))
348
      goto found_self;
349

    
350
  ospf_neigh_sm(n, INM_1WAYREC);
351
  return;
352

    
353
found_self:
354
  ospf_neigh_sm(n, INM_2WAYREC);
355

    
356

    
357
  if (n->iface_id != old_iface_id)
358
  {
359
    /* If neighbor is DR, also update cached DR interface ID */
360
    if (ifa->drid == n->rid)
361
      ifa->dr_iface_id = n->iface_id;
362

    
363
    /* RFC 5340 4.4.3 Event 4 - change of neighbor's interface ID */
364
    ospf_notify_rt_lsa(ifa->oa);
365

    
366
    /* Missed in RFC 5340 4.4.3 Event 4 - (Px-)Net-LSA uses iface_id to ref Link-LSAs */
367
    ospf_notify_net_lsa(ifa);
368
  }
369

    
370
  if (ifa->state == OSPF_IS_WAITING)
371
  {
372
    /* Neighbor is declaring itself DR (and there is no BDR) or as BDR */
373
    if (((n->dr == n_id) && (n->bdr == 0)) || (n->bdr == n_id))
374
      ospf_iface_sm(ifa, ISM_BACKS);
375
  }
376
  else if (ifa->state >= OSPF_IS_DROTHER)
377
  {
378
    /* Neighbor changed priority or started/stopped declaring itself as DR/BDR */
379
    if ((n->priority != old_priority) ||
380
        ((n->dr == n_id) && (old_dr != n_id)) ||
381
        ((n->dr != n_id) && (old_dr == n_id)) ||
382
        ((n->bdr == n_id) && (old_bdr != n_id)) ||
383
        ((n->bdr != n_id) && (old_bdr == n_id)))
384
      ospf_iface_sm(ifa, ISM_NEICH);
385
  }
386

    
387
  return;
388

    
389
drop:
390
  LOG_PKT("Bad HELLO packet from nbr %R on %s - %s (%u)",
391
          rcv_rid, ifa->ifname, err_dsc, err_val);
392
}