Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.46 KB)

1 4364b47e Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 bc4ea680 Ondrej Filip
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5 4364b47e Ondrej Filip
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9
#include "ospf.h"
10
11 c3226991 Ondrej Zajicek
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 4364b47e Ondrej Filip
void
45 c3226991 Ondrej Zajicek
ospf_hello_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
46
                   struct ospf_neighbor *n, ip_addr faddr)
47 4364b47e Ondrej Filip
{
48 8a70a13e Ondrej Zajicek
  struct proto_ospf *po = ifa->oa->po;
49
  struct proto *p = &po->proto;
50 353729f5 Ondrej Zajicek
  char *beg = "OSPF: Bad HELLO packet from ";
51 919f5411 Ondrej Zajicek
  unsigned int size, i, twoway, peers;
52 e81b440f Ondrej Zajicek
  u32 tmp;
53 a6bc04d5 Ondrej Zajicek
  u32 *pnrid;
54
55
  size = ntohs(ps_i->length);
56
  if (size < sizeof(struct ospf_hello_packet))
57
  {
58 353729f5 Ondrej Zajicek
    log(L_ERR "%s%I - too short (%u B)", beg, faddr, size);
59 a6bc04d5 Ondrej Zajicek
    return;
60
  }
61
62
  struct ospf_hello_packet *ps = (void *) ps_i;
63 4364b47e Ondrej Filip
64 48e5f32d Ondrej Zajicek
  OSPF_TRACE(D_PACKETS, "HELLO packet received from %I via %s", faddr, ifa->ifname);
65 c3226991 Ondrej Zajicek
66
#ifdef OSPFv2
67 f9c799a0 Ondrej Zajicek
  ip_addr mask = ps->netmask;
68 099c017f Ondrej Filip
  ipa_ntoh(mask);
69 353729f5 Ondrej Zajicek
  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 c3226991 Ondrej Zajicek
#endif
77 77539c44 Ondrej Filip
78 c3226991 Ondrej Zajicek
  tmp = ntohs(ps->helloint);
79
  if (tmp != ifa->helloint)
80 4364b47e Ondrej Filip
  {
81 353729f5 Ondrej Zajicek
    log(L_ERR "%s%I - hello interval mismatch (%d)", beg, faddr, tmp);
82 4364b47e Ondrej Filip
    return;
83
  }
84
85 c3226991 Ondrej Zajicek
#ifdef OSPFv2
86
  tmp = ntohl(ps->deadint);
87
#else /* OSPFv3 */
88
  tmp = ntohs(ps->deadint);
89
#endif
90 8e48831a Ondrej Zajicek
  if (tmp != ifa->deadint)
91 4364b47e Ondrej Filip
  {
92 353729f5 Ondrej Zajicek
    log(L_ERR "%s%I - dead interval mismatch (%d)", beg, faddr, tmp);
93 4364b47e Ondrej Filip
    return;
94
  }
95
96 41b612c3 Ondrej Zajicek
  /* Check whether bits E, N match */
97
  if ((ps->options ^ ifa->oa->options) & (OPT_E | OPT_N))
98 4364b47e Ondrej Filip
  {
99 41b612c3 Ondrej Zajicek
    log(L_ERR "%s%I - area type mismatch (%x)", beg, faddr, ps->options);
100 4364b47e Ondrej Filip
    return;
101
  }
102
103 e550a372 Ondrej Filip
#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 5e3436d2 Ondrej Filip
  if (!n)
115 4364b47e Ondrej Filip
  {
116 919f5411 Ondrej Zajicek
    if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
117 a190e720 Ondrej Filip
    {
118 919f5411 Ondrej Zajicek
      struct nbma_node *nn = find_nbma_node(ifa, faddr);
119 a190e720 Ondrej Filip
120 919f5411 Ondrej Zajicek
      if (!nn && ifa->strictnbma)
121 a190e720 Ondrej Filip
      {
122 48e5f32d Ondrej Zajicek
        log(L_WARN "Ignoring new neighbor: %I on %s", faddr, ifa->ifname);
123 a190e720 Ondrej Filip
        return;
124
      }
125 919f5411 Ondrej Zajicek
126
      if (nn && (ifa->type == OSPF_IT_NBMA) &&
127
          (((ps->priority == 0) && nn->eligible) ||
128
           ((ps->priority > 0) && !nn->eligible)))
129 a190e720 Ondrej Filip
      {
130 48e5f32d Ondrej Zajicek
        log(L_ERR "Eligibility mismatch for neighbor: %I on %s", faddr, ifa->ifname);
131 919f5411 Ondrej Zajicek
        return;
132 a190e720 Ondrej Filip
      }
133 919f5411 Ondrej Zajicek
134
      if (nn)
135
        nn->found = 1;
136 a190e720 Ondrej Filip
    }
137 919f5411 Ondrej Zajicek
138 48e5f32d Ondrej Zajicek
    OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s", faddr, ifa->ifname);
139 39e517d4 Ondrej Filip
140
    n = ospf_neighbor_new(ifa);
141
142 e550a372 Ondrej Filip
    n->rid = ntohl(ps_i->routerid);
143 77539c44 Ondrej Filip
    n->ip = faddr;
144 c3226991 Ondrej Zajicek
    n->dr = ntohl(ps->dr);
145
    n->bdr = ntohl(ps->bdr);
146 77539c44 Ondrej Filip
    n->priority = ps->priority;
147 c3226991 Ondrej Zajicek
#ifdef OSPFv3
148
    n->iface_id = ntohl(ps->iface_id);
149
#endif
150 1ec52253 Ondrej Zajicek
151
    if (n->ifa->cf->bfd)
152
      ospf_neigh_update_bfd(n, n->ifa->bfd);
153 4364b47e Ondrej Filip
  }
154 33be3ba7 Ondrej Zajicek
#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 4364b47e Ondrej Filip
  ospf_neigh_sm(n, INM_HELLOREC);
163
164 77539c44 Ondrej Filip
  pnrid = (u32 *) ((struct ospf_hello_packet *) (ps + 1));
165 4364b47e Ondrej Filip
166 bc4ea680 Ondrej Filip
  peers = (size - sizeof(struct ospf_hello_packet))/ sizeof(u32);
167
168 77539c44 Ondrej Filip
  twoway = 0;
169 bc4ea680 Ondrej Filip
  for (i = 0; i < peers; i++)
170 4364b47e Ondrej Filip
  {
171 8a70a13e Ondrej Zajicek
    if (ntohl(pnrid[i]) == po->router_id)
172 4364b47e Ondrej Filip
    {
173 31dca435 Ondrej Filip
      DBG("%s: Twoway received from %I\n", p->name, faddr);
174 4364b47e Ondrej Filip
      ospf_neigh_sm(n, INM_2WAYREC);
175 77539c44 Ondrej Filip
      twoway = 1;
176 4364b47e Ondrej Filip
      break;
177
    }
178
  }
179
180 77539c44 Ondrej Filip
  if (!twoway)
181
    ospf_neigh_sm(n, INM_1WAYREC);
182 4364b47e Ondrej Filip
183 e81b440f Ondrej Zajicek
  u32 olddr = n->dr;
184
  u32 oldbdr = n->bdr;
185
  u32 oldpriority = n->priority;
186 c3226991 Ondrej Zajicek
#ifdef OSPFv3
187 e81b440f Ondrej Zajicek
  u32 oldiface_id = n->iface_id;
188 c3226991 Ondrej Zajicek
#endif
189
190
  n->dr = ntohl(ps->dr);
191
  n->bdr = ntohl(ps->bdr);
192 b29c620f Ondrej Filip
  n->priority = ps->priority;
193 c3226991 Ondrej Zajicek
#ifdef OSPFv3
194
  n->iface_id = ntohl(ps->iface_id);
195
#endif
196
197 b29c620f Ondrej Filip
198 4364b47e Ondrej Filip
  /* Check priority change */
199 77539c44 Ondrej Filip
  if (n->state >= NEIGHBOR_2WAY)
200 4364b47e Ondrej Filip
  {
201 c3226991 Ondrej Zajicek
#ifdef OSPFv2
202 8a70a13e Ondrej Zajicek
    u32 neigh = ipa_to_u32(n->ip);
203 c3226991 Ondrej Zajicek
#else /* OSPFv3 */
204 8a70a13e Ondrej Zajicek
    u32 neigh = n->rid;
205 c3226991 Ondrej Zajicek
#endif
206
207 77539c44 Ondrej Filip
    if (n->priority != oldpriority)
208 a5918961 Ondrej Filip
      ospf_iface_sm(ifa, ISM_NEICH);
209 4364b47e Ondrej Filip
210 c3226991 Ondrej Zajicek
#ifdef OSPFv3
211
    if (n->iface_id != oldiface_id)
212
      ospf_iface_sm(ifa, ISM_NEICH);
213
#endif
214
215 8a70a13e Ondrej Zajicek
    /* Neighbor is declaring itself ad DR and there is no BDR */
216
    if ((n->dr == neigh) && (n->bdr == 0)
217 77539c44 Ondrej Filip
        && (n->state != NEIGHBOR_FULL))
218 a5918961 Ondrej Filip
      ospf_iface_sm(ifa, ISM_BACKS);
219 4364b47e Ondrej Filip
220 b29c620f Ondrej Filip
    /* Neighbor is declaring itself as BDR */
221 8a70a13e Ondrej Zajicek
    if ((n->bdr == neigh) && (n->state != NEIGHBOR_FULL))
222 a5918961 Ondrej Filip
      ospf_iface_sm(ifa, ISM_BACKS);
223 b29c620f Ondrej Filip
224
    /* Neighbor is newly declaring itself as DR or BDR */
225 8a70a13e Ondrej Zajicek
    if (((n->dr == neigh) && (n->dr != olddr))
226
        || ((n->bdr == neigh) && (n->bdr != oldbdr)))
227 a5918961 Ondrej Filip
      ospf_iface_sm(ifa, ISM_NEICH);
228 b29c620f Ondrej Filip
229
    /* Neighbor is no more declaring itself as DR or BDR */
230 8a70a13e Ondrej Zajicek
    if (((olddr == neigh) && (n->dr != olddr))
231
        || ((oldbdr == neigh) && (n->bdr != oldbdr)))
232 a5918961 Ondrej Filip
      ospf_iface_sm(ifa, ISM_NEICH);
233 4364b47e Ondrej Filip
  }
234
235 3b16080c Ondrej Filip
  if (ifa->type == OSPF_IT_NBMA)
236 a190e720 Ondrej Filip
  {
237 77539c44 Ondrej Filip
    if ((ifa->priority == 0) && (n->priority > 0))
238 beeda6af Ondrej Zajicek
      ospf_hello_send(n->ifa, OHS_HELLO, n);
239 a190e720 Ondrej Filip
  }
240 4364b47e Ondrej Filip
  ospf_neigh_sm(n, INM_HELLOREC);
241
}
242
243
void
244 beeda6af Ondrej Zajicek
ospf_hello_send(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn)
245 a190e720 Ondrej Filip
{
246 4364b47e Ondrej Filip
  struct ospf_hello_packet *pkt;
247
  struct ospf_packet *op;
248
  struct proto *p;
249 98ac6176 Ondrej Filip
  struct ospf_neighbor *neigh, *n1;
250 4364b47e Ondrej Filip
  u16 length;
251 919f5411 Ondrej Zajicek
  int i;
252 98ac6176 Ondrej Filip
  struct nbma_node *nb;
253 86c84d76 Ondrej Filip
254 391931d4 Ondrej Zajicek
  if (ifa->state <= OSPF_IS_LOOP)
255 98ac6176 Ondrej Filip
    return;
256
257 77539c44 Ondrej Filip
  if (ifa->stub)
258
    return;                        /* Don't send any packet on stub iface */
259 e3bc10fd Ondrej Filip
260 86c84d76 Ondrej Filip
  p = (struct proto *) (ifa->oa->po);
261 0aad2b92 Ondrej Zajicek
  DBG("%s: Hello/Poll timer fired on interface %s with IP %I\n",
262 48e5f32d Ondrej Zajicek
      p->name, ifa->ifname, ifa->addr->ip);
263 4364b47e Ondrej Filip
264 f9c799a0 Ondrej Zajicek
  /* Now we should send a hello packet */
265 e7b76b97 Ondrej Zajicek
  pkt = ospf_tx_buffer(ifa);
266 353729f5 Ondrej Zajicek
  op = &pkt->ospf_packet;
267 4364b47e Ondrej Filip
268 f9c799a0 Ondrej Zajicek
  /* Now fill ospf_hello header */
269 3e2bd0f1 Ondrej Filip
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
270 e5b5d18c Ondrej Filip
271 c3226991 Ondrej Zajicek
#ifdef OSPFv2
272 353729f5 Ondrej Zajicek
  pkt->netmask = ipa_mkmask(ifa->addr->pxlen);
273 e5b5d18c Ondrej Filip
  ipa_hton(pkt->netmask);
274 8df02847 Ondrej Zajicek
  if ((ifa->type == OSPF_IT_VLINK) ||
275
      ((ifa->type == OSPF_IT_PTP) && !ifa->ptp_netmask))
276 f571473e Ondrej Zajicek
    pkt->netmask = IPA_NONE;
277 c3226991 Ondrej Zajicek
#endif
278
279 77539c44 Ondrej Filip
  pkt->helloint = ntohs(ifa->helloint);
280
  pkt->priority = ifa->priority;
281 c3226991 Ondrej Zajicek
282
#ifdef OSPFv3
283 dd4da6f6 Ondrej Zajicek
  pkt->iface_id = htonl(ifa->iface_id);
284 c3226991 Ondrej Zajicek
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 8e48831a Ondrej Zajicek
  pkt->deadint = htonl(ifa->deadint);
292 c3226991 Ondrej Zajicek
  pkt->dr = htonl(ipa_to_u32(ifa->drip));
293
  pkt->bdr = htonl(ipa_to_u32(ifa->bdrip));
294
#else /* OSPFv3 */
295 8e48831a Ondrej Zajicek
  pkt->deadint = htons(ifa->deadint);
296 c3226991 Ondrej Zajicek
  pkt->dr = htonl(ifa->drid);
297
  pkt->bdr = htonl(ifa->bdrid);
298
#endif
299 e5b5d18c Ondrej Filip
300
  /* Fill all neighbors */
301 77539c44 Ondrej Filip
  i = 0;
302 8e48831a Ondrej Zajicek
303
  if (kind != OHS_SHUTDOWN)
304 e5b5d18c Ondrej Filip
  {
305 8e48831a Ondrej Zajicek
    u32 *pp = (u32 *) (((u8 *) pkt) + sizeof(struct ospf_hello_packet));
306
    WALK_LIST(neigh, ifa->neigh_list)
307 3e2bd0f1 Ondrej Filip
    {
308 48e5f32d Ondrej Zajicek
      if ((i+1) * sizeof(u32) + sizeof(struct ospf_hello_packet) > ospf_pkt_maxsize(ifa))
309 8e48831a Ondrej Zajicek
      {
310 48e5f32d Ondrej Zajicek
        log(L_WARN "%s: Too many neighbors on interface %s", p->name, ifa->ifname);
311 8e48831a Ondrej Zajicek
        break;
312
      }
313
      *(pp + i) = htonl(neigh->rid);
314
      i++;
315 3e2bd0f1 Ondrej Filip
    }
316 e5b5d18c Ondrej Filip
  }
317
318 77539c44 Ondrej Filip
  length = sizeof(struct ospf_hello_packet) + i * sizeof(u32);
319
  op->length = htons(length);
320 e5b5d18c Ondrej Filip
321 98ac6176 Ondrej Filip
  switch(ifa->type)
322 e5b5d18c Ondrej Filip
  {
323 919f5411 Ondrej Zajicek
  case OSPF_IT_BCAST:
324
  case OSPF_IT_PTP:
325 95127cbb Ondrej Zajicek
    ospf_send_to_all(ifa);
326 919f5411 Ondrej Zajicek
    break;
327
328
  case OSPF_IT_NBMA:
329 beeda6af Ondrej Zajicek
    if (dirn)                /* Response to received hello */
330 919f5411 Ondrej Zajicek
    {
331
      ospf_send_to(ifa, dirn->ip);
332 98ac6176 Ondrej Filip
      break;
333 919f5411 Ondrej Zajicek
    }
334
335
    int to_all = ifa->state > OSPF_IS_DROTHER;
336
    int me_elig = ifa->priority > 0;
337
 
338 8e48831a Ondrej Zajicek
    if (kind == OHS_POLL)        /* Poll timer */
339 919f5411 Ondrej Zajicek
    {
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 e5b5d18c Ondrej Filip
  }
374 8298d780 Ondrej Zajicek
375 48e5f32d Ondrej Zajicek
  OSPF_TRACE(D_PACKETS, "HELLO packet sent via %s", ifa->ifname);
376 4364b47e Ondrej Filip
}