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

#include "ospf.h"

13

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

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

27 28
  u32 neighbors[];
};
29

30
struct ospf_hello3_packet
31
{
32 33
  struct ospf_packet hdr;

34
  u32 iface_id;
35
  u32 options;
36 37 38 39 40
  u16 helloint;
  u16 deadint;
  u32 dr;
  u32 bdr;

41 42
  u32 neighbors[];
};
43

44

45 46 47 48 49 50 51
uint
ospf_hello3_options(struct ospf_packet *pkt)
{
  struct ospf_hello3_packet *ps = (void *) pkt;
  return ntohl(ps->options) & 0x00FFFFFF;
}

52
void
53
ospf_send_hello(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn)
54
{
55 56
  struct ospf_proto *p = ifa->oa->po;
  struct ospf_packet *pkt;
Ondřej Filip's avatar
Ondřej Filip committed
57 58
  struct ospf_neighbor *neigh, *n1;
  struct nbma_node *nb;
59 60 61
  u32 *neighbors;
  uint length;
  int i, max;
62

63
  if (ifa->state <= OSPF_IS_LOOP)
Ondřej Filip's avatar
Ondřej Filip committed
64 65
    return;

66
  if (ifa->stub)
67
    return;
68

69

70
  pkt = ospf_tx_buffer(ifa);
71
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
72

73 74 75 76 77 78 79 80
  if (ospf_is_v2(p))
  {
    struct ospf_hello2_packet *ps = (void *) pkt;

    if ((ifa->type == OSPF_IT_VLINK) ||
	((ifa->type == OSPF_IT_PTP) && !ifa->ptp_netmask))
      ps->netmask = 0;
    else
81
      ps->netmask = htonl(u32_mkmask(ifa->addr->prefix.pxlen));
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    ps->helloint = ntohs(ifa->helloint);
    ps->options = ifa->oa->options;
    ps->priority = ifa->priority;
    ps->deadint = htonl(ifa->deadint);
    ps->dr = htonl(ipa_to_u32(ifa->drip));
    ps->bdr = htonl(ipa_to_u32(ifa->bdrip));

    length = sizeof(struct ospf_hello2_packet);
    neighbors = ps->neighbors;
  }
  else
  {
    struct ospf_hello3_packet *ps = (void *) pkt;
96
    u32 options = ifa->oa->options | (ifa->autype == OSPF_AUTH_CRYPT ? OPT_AT : 0);
97 98

    ps->iface_id = htonl(ifa->iface_id);
99
    ps->options = ntohl(options | (ifa->priority << 24));
100 101 102 103 104 105 106 107
    ps->helloint = ntohs(ifa->helloint);
    ps->deadint = htons(ifa->deadint);
    ps->dr = htonl(ifa->drid);
    ps->bdr = htonl(ifa->bdrid);

    length = sizeof(struct ospf_hello3_packet);
    neighbors = ps->neighbors;
  }
108

109
  i = 0;
110
  max = (ospf_pkt_maxsize(ifa) - length) / sizeof(u32);
111

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

127 128 129 130
  length += i * sizeof(u32);
  pkt->length = htons(length);

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

Ondřej Filip's avatar
Ondřej Filip committed
132
  switch(ifa->type)
133
  {
134 135
  case OSPF_IT_BCAST:
  case OSPF_IT_PTP:
136
    ospf_send_to_all(ifa);
137 138 139
    break;

  case OSPF_IT_NBMA:
140
    if (dirn)		/* Response to received hello */
141 142
    {
      ospf_send_to(ifa, dirn->ip);
Ondřej Filip's avatar
Ondřej Filip committed
143
      break;
144 145 146 147
    }

    int to_all = ifa->state > OSPF_IS_DROTHER;
    int me_elig = ifa->priority > 0;
148

149
    if (kind == OHS_POLL)	/* Poll timer */
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
    {
      WALK_LIST(nb, ifa->nbma_list)
	if (!nb->found && (to_all || (me_elig && nb->eligible)))
	  ospf_send_to(ifa, nb->ip);
    }
    else			/* Hello timer */
    {
      WALK_LIST(n1, ifa->neigh_list)
	if (to_all || (me_elig && (n1->priority > 0)) ||
	    (n1->rid == ifa->drid) || (n1->rid == ifa->bdrid))
	  ospf_send_to(ifa, n1->ip);
    }
    break;

  case OSPF_IT_PTMP:
    WALK_LIST(n1, ifa->neigh_list)
      ospf_send_to(ifa, n1->ip);

    WALK_LIST(nb, ifa->nbma_list)
      if (!nb->found)
	ospf_send_to(ifa, nb->ip);

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

  case OSPF_IT_VLINK:
    ospf_send_to(ifa, ifa->vip);
    break;

  default:
183
    bug("Bug in ospf_send_hello()");
184
  }
185
}
186

187 188 189 190 191 192

void
ospf_receive_hello(struct ospf_packet *pkt, struct ospf_iface *ifa,
		   struct ospf_neighbor *n, ip_addr faddr)
{
  struct ospf_proto *p = ifa->oa->po;
193
  const char *err_dsc = NULL;
194
  u32 rcv_iface_id, rcv_helloint, rcv_deadint, rcv_dr, rcv_bdr;
195 196
  uint rcv_options, rcv_priority;
  uint loc_options = ifa->oa->options;
197 198
  u32 *neighbors;
  u32 neigh_count;
199
  uint plen, i, err_val = 0;
200 201 202

  /* RFC 2328 10.5 */

203
  /*
204
   * We may not yet have the associate neighbor, so we use Router ID from the
205 206 207 208
   * packet instead of one from the neighbor structure for log messages.
   */
  u32 rcv_rid = ntohl(pkt->routerid);
  OSPF_TRACE(D_PACKETS, "HELLO packet received from nbr %R on %s", rcv_rid, ifa->ifname);
209 210 211 212 213 214 215 216

  plen = ntohs(pkt->length);

  if (ospf_is_v2(p))
  {
    struct ospf_hello2_packet *ps = (void *) pkt;

    if (plen < sizeof(struct ospf_hello2_packet))
217
      DROP("too short", plen);
218 219 220 221 222 223 224 225 226 227

    rcv_iface_id = 0;
    rcv_helloint = ntohs(ps->helloint);
    rcv_deadint = ntohl(ps->deadint);
    rcv_dr = ntohl(ps->dr);
    rcv_bdr = ntohl(ps->bdr);
    rcv_options = ps->options;
    rcv_priority = ps->priority;

    int pxlen = u32_masklen(ntohl(ps->netmask));
228 229 230
    if (pxlen < 0)
      DROP("prefix garbled", ntohl(ps->netmask));

231 232
    if ((ifa->type != OSPF_IT_VLINK) &&
	(ifa->type != OSPF_IT_PTP) &&
233
	((uint) pxlen != ifa->addr->prefix.pxlen))
234
      DROP("prefix length mismatch", pxlen);
235 236 237 238 239 240 241 242 243

    neighbors = ps->neighbors;
    neigh_count = (plen - sizeof(struct ospf_hello2_packet)) / sizeof(u32);
  }
  else /* OSPFv3 */
  {
    struct ospf_hello3_packet *ps = (void *) pkt;

    if (plen < sizeof(struct ospf_hello3_packet))
244
      DROP("too short", plen);
245 246 247 248 249 250

    rcv_iface_id = ntohl(ps->iface_id);
    rcv_helloint = ntohs(ps->helloint);
    rcv_deadint = ntohs(ps->deadint);
    rcv_dr = ntohl(ps->dr);
    rcv_bdr = ntohl(ps->bdr);
251 252
    rcv_options = ntohl(ps->options) & 0x00FFFFFF;
    rcv_priority = ntohl(ps->options) >> 24;
253 254 255 256 257 258

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

  if (rcv_helloint != ifa->helloint)
259
    DROP("hello interval mismatch", rcv_helloint);
260 261

  if (rcv_deadint != ifa->deadint)
262
    DROP("dead interval mismatch", rcv_deadint);
263 264

  /* Check whether bits E, N match */
265
  if ((rcv_options ^ loc_options) & (OPT_E | OPT_N))
266
    DROP("area type mismatch", rcv_options);
267

268 269 270 271
  /* RFC 5838 2.4 - AF-bit check unless on IPv6 unicast */
  if ((loc_options & OPT_AF) && !(loc_options & OPT_V6) && !(rcv_options & OPT_AF))
    DROP("AF-bit mismatch", rcv_options);

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

  if (!n)
  {
    if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
    {
      struct nbma_node *nn = find_nbma_node(ifa, faddr);

      if (!nn && ifa->strictnbma)
306
	DROP1("new neighbor denied");
307 308 309 310

      if (nn && (ifa->type == OSPF_IT_NBMA) &&
	  (((rcv_priority == 0) && nn->eligible) ||
	   ((rcv_priority > 0) && !nn->eligible)))
311
	DROP("eligibility mismatch", rcv_priority);
312 313 314 315 316

      if (nn)
	nn->found = 1;
    }

317 318
    OSPF_TRACE(D_EVENTS, "New neighbor %R on %s, IP address %I",
	       rcv_rid, ifa->ifname, faddr);
319 320 321

    n = ospf_neighbor_new(ifa);

322
    n->rid = rcv_rid;
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
    n->ip = faddr;
    n->dr = rcv_dr;
    n->bdr = rcv_bdr;
    n->priority = rcv_priority;
    n->iface_id = rcv_iface_id;

    if (n->ifa->cf->bfd)
      ospf_neigh_update_bfd(n, n->ifa->bfd);
  }

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

  u32 old_dr = n->dr;
  u32 old_bdr = n->bdr;
  u32 old_priority = n->priority;
  u32 old_iface_id = n->iface_id;

  n->dr = rcv_dr;
  n->bdr = rcv_bdr;
  n->priority = rcv_priority;
  n->iface_id = rcv_iface_id;

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

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

353 354 355 356 357 358 359 360 361

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

  ospf_neigh_sm(n, INM_1WAYREC);
  return;

362
found_self:
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  ospf_neigh_sm(n, INM_2WAYREC);


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

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

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

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

  return;

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