summaryrefslogtreecommitdiff
path: root/proto/ospf/hello.c
blob: 7eeee3b3d6119fdcdc4a59acd6377abdfaaf9fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 *	BIRD -- OSPF
 *
 *	(c) 1999--2004 Ondrej Filip <feela@network.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"


#ifdef OSPFv2
struct ospf_hello_packet
{
  struct ospf_packet ospf_packet;
  ip_addr netmask;
  u16 helloint;
  u8 options;
  u8 priority;
  u32 deadint;
  u32 dr;
  u32 bdr;
};
#endif


#ifdef OSPFv3
struct ospf_hello_packet
{
  struct ospf_packet ospf_packet;
  u32 iface_id;
  u8 priority;
  u8 options3;
  u8 options2;
  u8 options;
  u16 helloint;
  u16 deadint;
  u32 dr;
  u32 bdr;
};
#endif


void
ospf_hello_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
		   struct ospf_neighbor *n, ip_addr faddr)
{
  struct ospf_hello_packet *ps = (void *) ps_i;
  u32 *pnrid;
  u32 olddr, oldbdr, oldiface_id, tmp;
  ip_addr mask;
  char *beg = "Bad OSPF hello packet from ", *rec = " received: ";
  struct proto *p = (struct proto *) ifa->oa->po;
  unsigned int size = ntohs(ps->ospf_packet.length), i, twoway, oldpriority, eligible = 0, peers;

  OSPF_TRACE(D_PACKETS, "HELLO packet received from %I via %s%s", faddr,
      (ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);

#ifdef OSPFv2
  mask = ps->netmask;
  ipa_ntoh(mask);
  if (ifa->type != OSPF_IT_VLINK)
    {
      char *msg = L_WARN "Received HELLO packet %s (%I) is inconsistent "
	"with the primary address of interface %s.";

      if ((ifa->type != OSPF_IT_PTP) &&
	  !ipa_equal(mask, ipa_mkmask(ifa->iface->addr->pxlen)))
	{
	  if (!n) log(msg, "netmask", mask, ifa->iface->name);
	  return;
	}

      /* This check is not specified in RFC 2328, but it is needed
       * to handle the case when there is more IP networks on one
       * physical network (which is not handled in RFC 2328).
       * We allow OSPF on primary IP address only and ignore HELLO packets
       * with secondary addresses (which are sent for example by Quagga.
       */
      if ((ifa->iface->addr->flags & IA_UNNUMBERED) ?
	  !ipa_equal(faddr, ifa->iface->addr->opposite) :
	  !ipa_equal(ipa_and(faddr,mask), ifa->iface->addr->prefix))
	{
	  if (!n) log(msg, "address", faddr, ifa->iface->name);
	  return;
	}
    }
#endif

  tmp = ntohs(ps->helloint);
  if (tmp != ifa->helloint)
  {
    log(L_ERR "%s%I%shello interval mismatch (%d).", beg, faddr, rec, tmp);
    return;
  }

#ifdef OSPFv2
  tmp = ntohl(ps->deadint);
#else /* OSPFv3 */
  tmp = ntohs(ps->deadint);
#endif
  if (tmp != ifa->dead)
  {
    log(L_ERR "%s%I%sdead interval mismatch (%d).", beg, faddr, rec, tmp);
    return;
  }

  tmp = !(ps->options & OPT_E);
  if (tmp != ifa->oa->stub)
  {
    log(L_ERR "%s%I%sstub area flag mismatch (%d).", beg, faddr, rec, tmp);
    return;
  }

  if (!n)
  {
    if ((ifa->type == OSPF_IT_NBMA))
    {
      struct nbma_node *nn;
      int found = 0;

      WALK_LIST(nn, ifa->nbma_list)
      {
	if (ipa_equal(faddr, nn->ip))
	{
	  found = 1;
	  break;
	}
      }
      if ((found == 0) && (ifa->strictnbma))
      {
	log(L_WARN "Ignoring new neighbor: %I on %s.", faddr,
	    ifa->iface->name);
	return;
      }
      if (found)
      {
	eligible = nn->eligible;
	if (((ps->priority == 0) && eligible)
	    || ((ps->priority > 0) && (eligible == 0)))
	{
	  log(L_ERR "Eligibility mismatch for neighbor: %I on %s",
	      faddr, ifa->iface->name);
	  return;
	}
      }
    }
    OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s.", faddr,
	       ifa->iface->name);

    n = ospf_neighbor_new(ifa);

    n->rid = ntohl(((struct ospf_packet *) ps)->routerid);
    n->ip = faddr;
    n->dr = ntohl(ps->dr);
    n->bdr = ntohl(ps->bdr);
    n->priority = ps->priority;
#ifdef OSPFv3
    n->iface_id = ntohl(ps->iface_id);
#endif
  }
  ospf_neigh_sm(n, INM_HELLOREC);

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

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

  twoway = 0;
  for (i = 0; i < peers; i++)
  {
    if (ntohl(*(pnrid + i)) == p->cf->global->router_id)
    {
      DBG("%s: Twoway received from %I\n", p->name, faddr);
      ospf_neigh_sm(n, INM_2WAYREC);
      twoway = 1;
      break;
    }
  }

  if (!twoway)
    ospf_neigh_sm(n, INM_1WAYREC);

  olddr = n->dr;
  oldbdr = n->bdr;
  oldpriority = n->priority;
#ifdef OSPFv3
  oldiface_id = n->iface_id;
#endif

  n->dr = ntohl(ps->dr);
  n->bdr = ntohl(ps->bdr);
  n->priority = ps->priority;
#ifdef OSPFv3
  n->iface_id = ntohl(ps->iface_id);
#endif


  /* Check priority change */
  if (n->state >= NEIGHBOR_2WAY)
  {
#ifdef OSPFv2
    u32 rid = n->ip;
#else /* OSPFv3 */
    u32 rid = p->cf->global->router_id;
#endif

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

#ifdef OSPFv3
    if (n->iface_id != oldiface_id)
      ospf_iface_sm(ifa, ISM_NEICH);
#endif

    /* Router is declaring itself ad DR and there is no BDR */
    if ((rid == n->dr) && (n->bdr == 0)
	&& (n->state != NEIGHBOR_FULL))
      ospf_iface_sm(ifa, ISM_BACKS);

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

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

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

  if (ifa->type == OSPF_IT_NBMA)
  {
    if ((ifa->priority == 0) && (n->priority > 0))
      ospf_hello_send(NULL, 0, n);
  }
  ospf_neigh_sm(n, INM_HELLOREC);
}

void
ospf_hello_send(timer *timer, int poll, struct ospf_neighbor *dirn)
{
  struct ospf_iface *ifa;
  struct ospf_hello_packet *pkt;
  struct ospf_packet *op;
  struct proto *p;
  struct ospf_neighbor *neigh, *n1;
  u16 length;
  u32 *pp;
  int i, send;
  struct nbma_node *nb;

  if (timer == NULL)
    ifa = dirn->ifa;
  else
    ifa = (struct ospf_iface *) timer->data;

  if (ifa->state == OSPF_IS_DOWN)
    return;

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

  p = (struct proto *) (ifa->oa->po);
  DBG("%s: Hello/Poll timer fired on interface %s.\n",
      p->name, ifa->iface->name);
  /* Now we should send a hello packet */
  /* First a common packet header */
  if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_VLINK))
  {
    pkt = (struct ospf_hello_packet *) (ifa->ip_sk->tbuf);
  }
  else
  {
    pkt = (struct ospf_hello_packet *) (ifa->hello_sk->tbuf);
  }

  /* Now fill ospf_hello header */
  op = (struct ospf_packet *) pkt;

  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);

#ifdef OSPFv2
  pkt->netmask = ipa_mkmask(ifa->iface->addr->pxlen);
  ipa_hton(pkt->netmask);
  if ((ifa->type == OSPF_IT_VLINK) || (ifa->type == OSPF_IT_PTP))
    pkt->netmask = IPA_NONE;
#endif

  pkt->helloint = ntohs(ifa->helloint);
  pkt->priority = ifa->priority;

#ifdef OSPFv3
  pkt->iface_id = htonl(ifa->iface->index);

  pkt->options3 = ifa->oa->options >> 16;
  pkt->options2 = ifa->oa->options >> 8;
#endif
  pkt->options = ifa->oa->options;

#ifdef OSPFv2
  pkt->deadint = htonl(ifa->dead);
  pkt->dr = htonl(ipa_to_u32(ifa->drip));
  pkt->bdr = htonl(ipa_to_u32(ifa->bdrip));
#else /* OSPFv3 */
  pkt->deadint = htons(ifa->dead);
  pkt->dr = htonl(ifa->drid);
  pkt->bdr = htonl(ifa->bdrid);
#endif

  /* Fill all neighbors */
  i = 0;
  pp = (u32 *) (((u8 *) pkt) + sizeof(struct ospf_hello_packet));
  WALK_LIST(neigh, ifa->neigh_list)
  {
    if ((i+1) * sizeof(u32) + sizeof(struct ospf_hello_packet) > ospf_pkt_maxsize(ifa))
    {
      OSPF_TRACE(D_PACKETS, "Too many neighbors on the interface!");
      break;
    }
    *(pp + i) = htonl(neigh->rid);
    i++;
  }

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

  switch(ifa->type)
  {
    case OSPF_IT_NBMA:
      if (timer == NULL)		/* Response to received hello */
      {
        ospf_send_to(ifa->ip_sk, dirn->ip, ifa);
      }
      else
      {
        int toall = 0;
        int meeli = 0;
        if (ifa->state > OSPF_IS_DROTHER)
          toall = 1;
        if (ifa->priority > 0)
          meeli = 1;
 
        WALK_LIST(nb, ifa->nbma_list)
        {
          send = 1;
          WALK_LIST(n1, ifa->neigh_list)
          {
            if (ipa_equal(nb->ip, n1->ip))
            {
              send = 0;
              break;
            }
          }
          if ((poll == 1) && (send))
          {
            if (toall || (meeli && nb->eligible))
              ospf_send_to(ifa->ip_sk, nb->ip, ifa);
          }
        }
        if (poll == 0)
        {
          WALK_LIST(n1, ifa->neigh_list)
          {
            if (toall || (n1->rid == ifa->drid) || (n1->rid == ifa->bdrid) ||
                (meeli && (n1->priority > 0)))
              ospf_send_to(ifa->ip_sk, n1->ip, ifa);
          }
        }
      }
      break;
    case OSPF_IT_VLINK:
      ospf_send_to(ifa->ip_sk, ifa->vip, ifa);
      break;
    default:
      ospf_send_to(ifa->hello_sk, IPA_NONE, ifa);
  }

  OSPF_TRACE(D_PACKETS, "HELLO packet sent via %s%s",
	     (ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);
}