lsack.c 5.03 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF
 *
4
 *	(c) 2000-2004 Ondrej Filip <feela@network.cz>
5 6 7 8 9 10
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

11 12 13 14 15 16 17 18

struct ospf_lsack_packet
{
  struct ospf_packet ospf_packet;
  struct ospf_lsa_header lsh[];
};


19
char *s_queue[] = { "direct", "delayed" };
20

21 22 23 24 25 26 27 28

static void ospf_dump_lsack(struct proto *p, struct ospf_lsack_packet *pkt)
{
  struct ospf_packet *op = &pkt->ospf_packet;

  ASSERT(op->type == LSACK_P);
  ospf_dump_common(p, op);

29
  unsigned int i, j;
30 31 32 33
  j = (ntohs(op->length) - sizeof(struct ospf_lsack_packet)) /
    sizeof(struct ospf_lsa_header);

  for (i = 0; i < j; i++)
34
    ospf_dump_lsahdr(p, pkt->lsh + i);
35 36 37
}


38 39 40 41 42
/*
 * =====================================
 * Note, that h is in network endianity!
 * =====================================
 */
43

44
void
45
ospf_lsack_enqueue(struct ospf_neighbor *n, struct ospf_lsa_header *h,
46
		   int queue)
47
{
48
  struct lsah_n *no = mb_alloc(n->pool, sizeof(struct lsah_n));
49
  memcpy(&no->lsa, h, sizeof(struct ospf_lsa_header));
50
  add_tail(&n->ackl[queue], NODE no);
51 52
  DBG("Adding (%s) ack for %R, ID: %R, RT: %R, Type: %u\n", s_queue[queue],
      n->rid, ntohl(h->id), ntohl(h->rt), h->type);
53 54 55
}

void
56
ospf_lsack_send(struct ospf_neighbor *n, int queue)
57 58 59
{
  struct ospf_packet *op;
  struct ospf_lsack_packet *pk;
60
  u16 len, i = 0;
61 62
  struct ospf_lsa_header *h;
  struct lsah_n *no;
63
  struct ospf_iface *ifa = n->ifa;
64
  struct proto *p = &n->ifa->oa->po->proto;
65

66 67
  if (EMPTY_LIST(n->ackl[queue]))
    return;
68

69 70
  pk = (struct ospf_lsack_packet *) ifa->sk->tbuf;
  op = (struct ospf_packet *) ifa->sk->tbuf;
71

72
  ospf_pkt_fill_hdr(n->ifa, pk, LSACK_P);
73
  h = pk->lsh;
74

75
  while (!EMPTY_LIST(n->ackl[queue]))
76
  {
77 78
    no = (struct lsah_n *) HEAD(n->ackl[queue]);
    memcpy(h + i, &no->lsa, sizeof(struct ospf_lsa_header));
79
    i++;
80 81
    DBG("Iter %u ID: %R, RT: %R, Type: %u\n", i, ntohl((h + i)->id),
	ntohl((h + i)->rt), (h + i)->type);
82
    rem_node(NODE no);
83
    mb_free(no);
84
    if ((i * sizeof(struct ospf_lsa_header) +
85
	 sizeof(struct ospf_lsack_packet)) > ospf_pkt_maxsize(n->ifa))
86
    {
87
      if (!EMPTY_LIST(n->ackl[queue]))
88
      {
89 90 91 92 93
	len =
	  sizeof(struct ospf_lsack_packet) +
	  i * sizeof(struct ospf_lsa_header);
	op->length = htons(len);
	DBG("Sending and continuing! Len=%u\n", len);
94

95
	OSPF_PACKET(ospf_dump_lsack, (struct ospf_lsack_packet *) ifa->sk->tbuf,
96 97
		    "LSACK packet sent via %s", ifa->iface->name);

98
	if (ifa->type == OSPF_IT_BCAST)
99
	{
100
	  if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP))
101
	    ospf_send_to(ifa, AllSPFRouters);
102
	  else
103
	    ospf_send_to(ifa, AllDRouters);
104 105 106
	}
	else
	{
107
	  if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP))
108
	    ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
109
	  else
110
	    ospf_send_to_bdr(ifa);
111
	}
112

113
	ospf_pkt_fill_hdr(n->ifa, pk, LSACK_P);
114
	i = 0;
115 116 117 118
      }
    }
  }

119 120 121
  len = sizeof(struct ospf_lsack_packet) + i * sizeof(struct ospf_lsa_header);
  op->length = htons(len);
  DBG("Sending! Len=%u\n", len);
122

123
  OSPF_PACKET(ospf_dump_lsack, (struct ospf_lsack_packet *) ifa->sk->tbuf,
124 125
	      "LSACK packet sent via %s", ifa->iface->name);

126
  if (ifa->type == OSPF_IT_BCAST)
127
  {
128
    if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP))
129
      ospf_send_to(ifa, AllSPFRouters);
130
    else
131
      ospf_send_to(ifa, AllDRouters);
132 133
  }
  else
134
    ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
135 136 137
}

void
138 139
ospf_lsack_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
		   struct ospf_neighbor *n)
140
{
141
  struct proto *p = &ifa->oa->po->proto;
142
  struct ospf_lsa_header lsa;
Ondřej Filip's avatar
Ondřej Filip committed
143
  struct top_hash_entry *en;
144 145 146 147 148 149 150 151
  unsigned int i, lsano;

  unsigned int size = ntohs(ps_i->length);
  if (size < sizeof(struct ospf_lsack_packet))
  {
    log(L_ERR "Bad OSPF LSACK packet from %I -  too short (%u B)", n->ip, size);
    return;
  }
Ondřej Filip's avatar
Ondřej Filip committed
152

153
  struct ospf_lsack_packet *ps = (void *) ps_i;
154 155
  OSPF_PACKET(ospf_dump_lsack, ps, "LSACK packet received from %I via %s", n->ip, ifa->iface->name);

156
  ospf_neigh_sm(n, INM_HELLOREC);
157

158 159
  if (n->state < NEIGHBOR_EXCHANGE)
    return;
160

161
  lsano = (size - sizeof(struct ospf_lsack_packet)) /
Ondřej Filip's avatar
Ondřej Filip committed
162
    sizeof(struct ospf_lsa_header);
163
  for (i = 0; i < lsano; i++)
Ondřej Filip's avatar
Ondřej Filip committed
164
  {
165
    ntohlsah(ps->lsh + i, &lsa);
166 167
    u32 dom = ospf_lsa_domain(lsa.type, n->ifa);
    if ((en = ospf_hash_find_header(n->lsrth, dom, &lsa)) == NULL)
Ondřej Filip's avatar
Ondřej Filip committed
168
      continue;			/* pg 155 */
Ondřej Filip's avatar
Ondřej Filip committed
169

Ondřej Filip's avatar
Ondřej Filip committed
170
    if (lsa_comp(&lsa, &en->lsa) != CMP_SAME)	/* pg 156 */
Ondřej Filip's avatar
Ondřej Filip committed
171
    {
172 173
      if ((lsa.sn == LSA_MAXSEQNO) && (lsa.age == LSA_MAXAGE))
	continue;
174

175
      OSPF_TRACE(D_PACKETS, "Strange LS acknoledgement from %I", n->ip);
176 177
      OSPF_TRACE(D_PACKETS, "Id: %R, Rt: %R, Type: %u",
		 lsa.id, lsa.rt, lsa.type);
Ondřej Filip's avatar
Ondřej Filip committed
178
      OSPF_TRACE(D_PACKETS, "I have: Age: %4u, Seqno: 0x%08x, Sum: %u",
179
		 en->lsa.age, en->lsa.sn, en->lsa.checksum);
Ondřej Filip's avatar
Ondřej Filip committed
180
      OSPF_TRACE(D_PACKETS, "He has: Age: %4u, Seqno: 0x%08x, Sum: %u",
181
		 lsa.age, lsa.sn, lsa.checksum);
Ondřej Filip's avatar
Ondřej Filip committed
182 183 184
      continue;
    }

185 186
    DBG("Deleting LS Id: %R RT: %R Type: %u from LS Retl for neighbor %R\n",
	lsa.id, lsa.rt, lsa.type, n->rid);
Ondřej Filip's avatar
Ondřej Filip committed
187
    s_rem_node(SNODE en);
188 189 190 191 192
    if (en->lsa_body != NULL)
      mb_free(en->lsa_body);
    en->lsa_body = NULL;
    ospf_hash_delete(n->lsrth, en);
  }
193
}