hello.c 7.2 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 7 8 9 10 11
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

void
12
ospf_hello_receive(struct ospf_hello_packet *ps,
13
		   struct ospf_iface *ifa, struct ospf_neighbor *n, ip_addr faddr)
14
{
15
  u32 *pnrid;
16
  ip_addr olddr, oldbdr;
17
  ip_addr mask;
18
  char *beg = "Bad OSPF hello packet from ", *rec = " received: ";
19
  struct proto *p = (struct proto *) ifa->oa->po;
Ondřej Filip's avatar
Ondřej Filip committed
20
  unsigned int size = ntohs(ps->ospf_packet.length), i, twoway, oldpriority, eligible = 0, peers;
21

22 23
  OSPF_TRACE(D_PACKETS, "Received hello from %I via %s%s", faddr,
      (ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);
24
  mask = ps->netmask;
25
  ipa_ntoh(mask);
Ondřej Filip's avatar
Ondřej Filip committed
26

27
  if (((ifa->type != OSPF_IT_VLINK) && (ifa->type != OSPF_IT_PTP)) &&
28
      ((unsigned) ipa_mklen(mask) != ifa->iface->addr->pxlen))
29
  {
30
    log(L_ERR "%s%I%sbad netmask %I.", beg, faddr, rec, mask);
31 32
    return;
  }
33 34

  if (ntohs(ps->helloint) != ifa->helloint)
35
  {
Ondřej Filip's avatar
Ondřej Filip committed
36 37
    log(L_WARN "%s%I%shello interval mismatch (%d).", beg, faddr, rec,
	ntohs(ps->helloint));
38 39 40
    return;
  }

41
  if (ntohl(ps->deadint) != ifa->helloint * ifa->deadc)
42
  {
Ondřej Filip's avatar
Ondřej Filip committed
43 44
    log(L_ERR "%s%I%sdead interval mismatch (%d).", beg, faddr, rec,
	ntohl(ps->deadint));
45 46 47
    return;
  }

48
  if (ps->options != ifa->oa->opt.byte)
49
  {
50
    log(L_ERR "%s%I%soptions mismatch (0x%x).", beg, faddr, rec, ps->options);
51 52 53
    return;
  }

54
  if (!n)
55
  {
56
    if ((ifa->type == OSPF_IT_NBMA))
57 58
    {
      struct nbma_node *nn;
59
      int found = 0;
60

61
      WALK_LIST(nn, ifa->nbma_list)
62
      {
63
	if (ipa_equal(faddr, nn->ip))
64
	{
65
	  found = 1;
66 67 68
	  break;
	}
      }
69
      if ((found == 0) && (ifa->strictnbma))
70
      {
71 72
	log(L_WARN "Ignoring new neighbor: %I on %s.", faddr,
	    ifa->iface->name);
73 74
	return;
      }
75
      if (found)
76
      {
77 78 79 80 81 82
	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);
83
	  return;
84
	}
85 86
      }
    }
Ondřej Filip's avatar
Ondřej Filip committed
87
    OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s.", faddr,
88
	       ifa->iface->name);
89 90 91

    n = ospf_neighbor_new(ifa);

92
    n->rid = ntohl(((struct ospf_packet *) ps)->routerid);
93 94
    n->ip = faddr;
    n->dr = ps->dr;
95
    ipa_ntoh(n->dr);
96
    n->bdr = ps->bdr;
97
    ipa_ntoh(n->bdr);
98 99
    n->priority = ps->priority;
    n->options = ps->options;
100 101 102
  }
  ospf_neigh_sm(n, INM_HELLOREC);

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

Ondřej Filip's avatar
Ondřej Filip committed
105 106
  peers = (size - sizeof(struct ospf_hello_packet))/ sizeof(u32);

107
  twoway = 0;
Ondřej Filip's avatar
Ondřej Filip committed
108
  for (i = 0; i < peers; i++)
109
  {
110
    if (ntohl(*(pnrid + i)) == p->cf->global->router_id)
111
    {
Ondřej Filip's avatar
Ondřej Filip committed
112
      DBG("%s: Twoway received from %I\n", p->name, faddr);
113
      ospf_neigh_sm(n, INM_2WAYREC);
114
      twoway = 1;
115 116 117 118
      break;
    }
  }

119 120
  if (!twoway)
    ospf_neigh_sm(n, INM_1WAYREC);
121

122
  olddr = n->dr;
123
  n->dr = ipa_ntoh(ps->dr);
124
  oldbdr = n->bdr;
125
  n->bdr = ipa_ntoh(ps->bdr);
126 127 128
  oldpriority = n->priority;
  n->priority = ps->priority;

129
  /* Check priority change */
130
  if (n->state >= NEIGHBOR_2WAY)
131
  {
132
    if (n->priority != oldpriority)
133
      ospf_iface_sm(ifa, ISM_NEICH);
134

135
    /* Router is declaring itself ad DR and there is no BDR */
136
    if (ipa_equal(n->ip, n->dr) && (ipa_to_u32(n->bdr) == 0)
137
	&& (n->state != NEIGHBOR_FULL))
138
      ospf_iface_sm(ifa, ISM_BACKS);
139

140
    /* Neighbor is declaring itself as BDR */
141
    if (ipa_equal(n->ip, n->bdr) && (n->state != NEIGHBOR_FULL))
142
      ospf_iface_sm(ifa, ISM_BACKS);
143 144

    /* Neighbor is newly declaring itself as DR or BDR */
145 146
    if ((ipa_equal(n->ip, n->dr) && (!ipa_equal(n->dr, olddr)))
	|| (ipa_equal(n->ip, n->bdr) && (!ipa_equal(n->bdr, oldbdr))))
147
      ospf_iface_sm(ifa, ISM_NEICH);
148 149

    /* Neighbor is no more declaring itself as DR or BDR */
150 151
    if ((ipa_equal(n->ip, olddr) && (!ipa_equal(n->dr, olddr)))
	|| (ipa_equal(n->ip, oldbdr) && (!ipa_equal(n->bdr, oldbdr))))
152
      ospf_iface_sm(ifa, ISM_NEICH);
153 154
  }

155
  if (ifa->type == OSPF_IT_NBMA)
156
  {
157 158
    if ((ifa->priority == 0) && (n->priority > 0))
      ospf_hello_send(NULL, 0, n);
159
  }
160 161 162
  ospf_neigh_sm(n, INM_HELLOREC);
}

163
void
164
ospf_hello_send(timer * timer, int poll, struct ospf_neighbor *dirn)
165 166 167 168 169
{
  struct ospf_iface *ifa;
  struct ospf_hello_packet *pkt;
  struct ospf_packet *op;
  struct proto *p;
Ondřej Filip's avatar
Ondřej Filip committed
170
  struct ospf_neighbor *neigh, *n1;
171 172
  u16 length;
  u32 *pp;
Ondřej Filip's avatar
Ondřej Filip committed
173 174
  int i, send;
  struct nbma_node *nb;
175

176 177 178 179
  if (timer == NULL)
    ifa = dirn->ifa;
  else
    ifa = (struct ospf_iface *) timer->data;
180

Ondřej Filip's avatar
Ondřej Filip committed
181 182 183
  if (ifa->state == OSPF_IS_DOWN)
    return;

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

187
  p = (struct proto *) (ifa->oa->po);
188
  DBG("%s: Hello/Poll timer fired on interface %s.\n",
189
      p->name, ifa->iface->name);
190 191
  /* Now we should send a hello packet */
  /* First a common packet header */
Ondřej Filip's avatar
Ondřej Filip committed
192
  if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_VLINK))
193
  {
Ondřej Filip's avatar
Ondřej Filip committed
194
    pkt = (struct ospf_hello_packet *) (ifa->ip_sk->tbuf);
195
  }
196
  else
197
  {
Ondřej Filip's avatar
Ondřej Filip committed
198
    pkt = (struct ospf_hello_packet *) (ifa->hello_sk->tbuf);
199
  }
200

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

204
  ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
205

206
  pkt->netmask = ipa_mkmask(ifa->iface->addr->pxlen);
207
  ipa_hton(pkt->netmask);
208
  if (ifa->type == OSPF_IT_VLINK) pkt->netmask = IPA_NONE;
209
  pkt->helloint = ntohs(ifa->helloint);
210
  pkt->options = ifa->oa->opt.byte;
211 212 213
  pkt->priority = ifa->priority;
  pkt->deadint = htonl(ifa->deadc * ifa->helloint);
  pkt->dr = ifa->drip;
214
  ipa_hton(pkt->dr);
215
  pkt->bdr = ifa->bdrip;
216
  ipa_hton(pkt->bdr);
217 218

  /* Fill all neighbors */
219 220 221
  i = 0;
  pp = (u32 *) (((u8 *) pkt) + sizeof(struct ospf_hello_packet));
  WALK_LIST(neigh, ifa->neigh_list)
222
  {
223 224 225 226 227
    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;
    }
228
    *(pp + i) = htonl(neigh->rid);
229 230 231
    i++;
  }

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

Ondřej Filip's avatar
Ondřej Filip committed
235
  switch(ifa->type)
236
  {
Ondřej Filip's avatar
Ondřej Filip committed
237 238
    case OSPF_IT_NBMA:
      if (timer == NULL)		/* Response to received hello */
239
      {
240
        ospf_send_to(ifa->ip_sk, dirn->ip, ifa);
241
      }
Ondřej Filip's avatar
Ondřej Filip committed
242
      else
243
      {
Ondřej Filip's avatar
Ondřej Filip committed
244 245 246 247 248 249 250 251 252 253 254 255
        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)
          {
256
            if (ipa_equal(nb->ip, n1->ip))
Ondřej Filip's avatar
Ondřej Filip committed
257 258 259 260 261 262 263 264
            {
              send = 0;
              break;
            }
          }
          if ((poll == 1) && (send))
          {
            if (toall || (meeli && nb->eligible))
265
              ospf_send_to(ifa->ip_sk, nb->ip, ifa);
Ondřej Filip's avatar
Ondřej Filip committed
266 267 268 269 270 271 272 273
          }
        }
        if (poll == 0)
        {
          WALK_LIST(n1, ifa->neigh_list)
          {
            if (toall || (n1->rid == ifa->drid) || (n1->rid == ifa->bdrid) ||
                (meeli && (n1->priority > 0)))
274
              ospf_send_to(ifa->ip_sk, n1->ip, ifa);
Ondřej Filip's avatar
Ondřej Filip committed
275 276
          }
        }
277
      }
Ondřej Filip's avatar
Ondřej Filip committed
278 279
      break;
    case OSPF_IT_VLINK:
280
      ospf_send_to(ifa->ip_sk, ifa->vip, ifa);
Ondřej Filip's avatar
Ondřej Filip committed
281 282
      break;
    default:
283
      ospf_send_to(ifa->hello_sk, IPA_NONE, ifa);
284
  }
285 286
  OSPF_TRACE(D_PACKETS, "Hello sent via %s%s",
      (ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);
287
}