Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / proto / ospf / hello.c @ 6b3f1a54

History | View | Annotate | Download (9.53 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
  u32 options;
36
  u16 helloint;
37
  u16 deadint;
38
  u32 dr;
39
  u32 bdr;
40

    
41
  u32 neighbors[];
42
};
43

    
44

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

    
56
  if (ifa->state <= OSPF_IS_LOOP)
57
    return;
58

    
59
  if (ifa->stub)
60
    return;
61

    
62

    
63
  pkt = ospf_tx_buffer(ifa);
64
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
65

    
66
  if (ospf_is_v2(p))
67
  {
68
    struct ospf_hello2_packet *ps = (void *) pkt;
69

    
70
    if ((ifa->type == OSPF_IT_VLINK) ||
71
        ((ifa->type == OSPF_IT_PTP) && !ifa->ptp_netmask))
72
      ps->netmask = 0;
73
    else
74
      ps->netmask = htonl(u32_mkmask(ifa->addr->prefix.pxlen));
75

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

    
83
    length = sizeof(struct ospf_hello2_packet);
84
    neighbors = ps->neighbors;
85
  }
86
  else
87
  {
88
    struct ospf_hello3_packet *ps = (void *) pkt;
89

    
90
    ps->iface_id = htonl(ifa->iface_id);
91
    ps->options = ntohl(ifa->oa->options | (ifa->priority << 24));
92
    ps->helloint = ntohs(ifa->helloint);
93
    ps->deadint = htons(ifa->deadint);
94
    ps->dr = htonl(ifa->drid);
95
    ps->bdr = htonl(ifa->bdrid);
96

    
97
    length = sizeof(struct ospf_hello3_packet);
98
    neighbors = ps->neighbors;
99
  }
100

    
101
  i = 0;
102
  max = (ospf_pkt_maxsize(ifa) - length) / sizeof(u32);
103

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

    
119
  length += i * sizeof(u32);
120
  pkt->length = htons(length);
121

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

    
124
  switch(ifa->type)
125
  {
126
  case OSPF_IT_BCAST:
127
  case OSPF_IT_PTP:
128
    ospf_send_to_all(ifa);
129
    break;
130

    
131
  case OSPF_IT_NBMA:
132
    if (dirn)                /* Response to received hello */
133
    {
134
      ospf_send_to(ifa, dirn->ip);
135
      break;
136
    }
137

    
138
    int to_all = ifa->state > OSPF_IS_DROTHER;
139
    int me_elig = ifa->priority > 0;
140

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

    
156
  case OSPF_IT_PTMP:
157
    WALK_LIST(n1, ifa->neigh_list)
158
      ospf_send_to(ifa, n1->ip);
159

    
160
    WALK_LIST(nb, ifa->nbma_list)
161
      if (!nb->found)
162
        ospf_send_to(ifa, nb->ip);
163

    
164
    /* If there is no other target, we also send HELLO packet to the other end */
165
    if (ipa_nonzero(ifa->addr->opposite) && !ifa->strictnbma &&
166
        EMPTY_LIST(ifa->neigh_list) && EMPTY_LIST(ifa->nbma_list))
167
      ospf_send_to(ifa, ifa->addr->opposite);
168
    break;
169

    
170
  case OSPF_IT_VLINK:
171
    ospf_send_to(ifa, ifa->vip);
172
    break;
173

    
174
  default:
175
    bug("Bug in ospf_send_hello()");
176
  }
177
}
178

    
179

    
180
void
181
ospf_receive_hello(struct ospf_packet *pkt, struct ospf_iface *ifa,
182
                   struct ospf_neighbor *n, ip_addr faddr)
183
{
184
  struct ospf_proto *p = ifa->oa->po;
185
  const char *err_dsc = NULL;
186
  u32 rcv_iface_id, rcv_helloint, rcv_deadint, rcv_dr, rcv_bdr;
187
  uint rcv_options, rcv_priority;
188
  uint loc_options = ifa->oa->options;
189
  u32 *neighbors;
190
  u32 neigh_count;
191
  uint plen, i, err_val = 0;
192

    
193
  /* RFC 2328 10.5 */
194

    
195
  /*
196
   * We may not yet have the associate neighbor, so we use Router ID from the
197
   * packet instead of one from the neighbor structure for log messages.
198
   */
199
  u32 rcv_rid = ntohl(pkt->routerid);
200
  OSPF_TRACE(D_PACKETS, "HELLO packet received from nbr %R on %s", rcv_rid, 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
      DROP("too short", plen);
210

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

    
219
    int pxlen = u32_masklen(ntohl(ps->netmask));
220
    if (pxlen < 0)
221
      DROP("prefix garbled", ntohl(ps->netmask));
222

    
223
    if ((ifa->type != OSPF_IT_VLINK) &&
224
        (ifa->type != OSPF_IT_PTP) &&
225
        ((uint) pxlen != ifa->addr->prefix.pxlen))
226
      DROP("prefix length mismatch", pxlen);
227

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

    
235
    if (plen < sizeof(struct ospf_hello3_packet))
236
      DROP("too short", plen);
237

    
238
    rcv_iface_id = ntohl(ps->iface_id);
239
    rcv_helloint = ntohs(ps->helloint);
240
    rcv_deadint = ntohs(ps->deadint);
241
    rcv_dr = ntohl(ps->dr);
242
    rcv_bdr = ntohl(ps->bdr);
243
    rcv_options = ntohl(ps->options) & 0x00FFFFFF;
244
    rcv_priority = ntohl(ps->options) >> 24;
245

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

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

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

    
256
  /* Check whether bits E, N match */
257
  if ((rcv_options ^ loc_options) & (OPT_E | OPT_N))
258
    DROP("area type mismatch", rcv_options);
259

    
260
  /* RFC 5838 2.4 - AF-bit check unless on IPv6 unicast */
261
  if ((loc_options & OPT_AF) && !(loc_options & OPT_V6) && !(rcv_options & OPT_AF))
262
    DROP("AF-bit mismatch", rcv_options);
263

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

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

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

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

    
305
      if (nn)
306
        nn->found = 1;
307
    }
308

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

    
312
    n = ospf_neighbor_new(ifa);
313

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

    
321
    if (n->ifa->cf->bfd)
322
      ospf_neigh_update_bfd(n, n->ifa->bfd);
323
  }
324

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

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

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

    
337

    
338
  /* Update inactivity timer */
339
  ospf_neigh_sm(n, INM_HELLOREC);
340

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

    
346

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

    
352
  ospf_neigh_sm(n, INM_1WAYREC);
353
  return;
354

    
355
found_self:
356
  ospf_neigh_sm(n, INM_2WAYREC);
357

    
358

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

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

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

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

    
389
  return;
390

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