lsack.c 4.49 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF
 *
4 5 6
 *	(c) 2000--2004 Ondrej Filip <feela@network.cz>
 *	(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
/*
15 16
struct ospf_lsack_packet
{
17 18
  struct ospf_packet hdr;
  // union ospf_auth auth;
19

20 21 22
  struct ospf_lsa_header lsas[];
};
*/
23

24 25 26 27 28
struct lsa_node
{
  node n;
  struct ospf_lsa_header lsa;
};
29

30

31 32 33
static inline void
ospf_lsack_body(struct ospf_proto *p, struct ospf_packet *pkt,
		struct ospf_lsa_header **body, uint *count)
34
{
35 36
  uint plen = ntohs(pkt->length);
  uint hlen = ospf_pkt_hdrlen(p);
37

38 39 40
  *body = ((void *) pkt) + hlen;
  *count = (plen - hlen) / sizeof(struct ospf_lsa_header);
}
41

42 43 44 45 46
static void
ospf_dump_lsack(struct ospf_proto *p, struct ospf_packet *pkt)
{
  struct ospf_lsa_header *lsas;
  uint i, lsa_count;
47

48 49
  ASSERT(pkt->type == LSACK_P);
  ospf_dump_common(p, pkt);
50

51 52 53 54
  ospf_lsack_body(p, pkt, &lsas, &lsa_count);
  for (i = 0; i < lsa_count; i++)
    ospf_dump_lsahdr(p, lsas + i);
}
55

56

57
void
58
ospf_enqueue_lsack(struct ospf_neighbor *n, struct ospf_lsa_header *h_n, int queue)
59
{
60 61 62
  /* Note that h_n is in network endianity */
  struct lsa_node *no = mb_alloc(n->pool, sizeof(struct lsa_node));
  memcpy(&no->lsa, h_n, sizeof(struct ospf_lsa_header));
63
  add_tail(&n->ackl[queue], NODE no);
64 65 66
  DBG("Adding %s ack for %R, ID: %R, RT: %R, Type: %u\n",
      (queue == ACKL_DIRECT) ? "direct" : "delayed",
      n->rid, ntohl(h_n->id), ntohl(h_n->rt), h_n->type);
67 68 69
}

void
70
ospf_reset_lsack_queue(struct ospf_neighbor *n)
71
{
72
  struct lsa_node *no;
73

74 75 76 77 78 79
  WALK_LIST_FIRST(no, n->ackl[ACKL_DELAY])
  {
    rem_node(NODE no);
    mb_free(no);
  }
}
80

81
static inline void
82
ospf_send_lsack_(struct ospf_proto *p, struct ospf_neighbor *n, int queue)
83 84 85 86 87 88
{
  struct ospf_iface *ifa = n->ifa;
  struct ospf_lsa_header *lsas;
  struct ospf_packet *pkt;
  struct lsa_node *no;
  uint i, lsa_max, length;
89

90
  /* RFC 2328 13.5 */
91

92 93 94 95 96
  pkt = ospf_tx_buffer(ifa);
  ospf_pkt_fill_hdr(ifa, pkt, LSACK_P);
  ospf_lsack_body(p, pkt, &lsas, &lsa_max);

  for (i = 0; i < lsa_max && !EMPTY_LIST(n->ackl[queue]); i++)
97
  {
98 99 100 101
    no = (struct lsa_node *) HEAD(n->ackl[queue]);
    memcpy(&lsas[i], &no->lsa, sizeof(struct ospf_lsa_header));
    DBG("Iter %u ID: %R, RT: %R, Type: %04x\n",
	i, ntohl(lsas[i].id), ntohl(lsas[i].rt), lsas[i].type);
102
    rem_node(NODE no);
103
    mb_free(no);
104 105
  }

106 107
  length = ospf_pkt_hdrlen(p) + i * sizeof(struct ospf_lsa_header);
  pkt->length = htons(length);
108

109
  OSPF_PACKET(ospf_dump_lsack, pkt, "LSACK packet sent via %s", ifa->ifname);
110

111
  if (ifa->type == OSPF_IT_BCAST)
112
  {
113
    if ((ifa->state == OSPF_IS_DR) || (ifa->state == OSPF_IS_BACKUP))
114
      ospf_send_to_all(ifa);
115
    else
116
      ospf_send_to_des(ifa);
117 118
  }
  else
119
    ospf_send_to_agt(ifa, NEIGHBOR_EXCHANGE);
120 121 122
}

void
123
ospf_send_lsack(struct ospf_proto *p, struct ospf_neighbor *n, int queue)
124 125
{
  while (!EMPTY_LIST(n->ackl[queue]))
126
    ospf_send_lsack_(p, n, queue);
127 128 129 130
}

void
ospf_receive_lsack(struct ospf_packet *pkt, struct ospf_iface *ifa,
131
		   struct ospf_neighbor *n)
132
{
133 134 135 136 137
  struct ospf_proto *p = ifa->oa->po;
  struct ospf_lsa_header lsa, *lsas;
  struct top_hash_entry *ret, *en;
  uint i, lsa_count;
  u32 lsa_type, lsa_domain;
138

139
  /* RFC 2328 13.7 */
Ondřej Filip's avatar
Ondřej Filip committed
140

141
  /* No need to check length, lsack has only basic header */
142

143
  OSPF_PACKET(ospf_dump_lsack, pkt, "LSACK packet received from nbr %R on %s", n->rid, ifa->ifname);
144

145
  if (n->state < NEIGHBOR_EXCHANGE)
146 147
  {
    OSPF_TRACE(D_PACKETS, "LSACK packet ignored - lesser state than Exchange");
148
    return;
149
  }
150

151 152 153 154
  ospf_neigh_sm(n, INM_HELLOREC);	/* Not in RFC */

  ospf_lsack_body(p, pkt, &lsas, &lsa_count);
  for (i = 0; i < lsa_count; i++)
Ondřej Filip's avatar
Ondřej Filip committed
155
  {
156 157
    lsa_ntoh_hdr(&lsas[i], &lsa);
    lsa_get_type_domain(&lsa, n->ifa, &lsa_type, &lsa_domain);
Ondřej Filip's avatar
Ondřej Filip committed
158

159 160 161 162 163
    ret = ospf_hash_find(n->lsrth, lsa_domain, lsa.id, lsa.rt, lsa_type);
    if (!ret)
      continue;

    if (lsa_comp(&lsa, &ret->lsa) != CMP_SAME)
Ondřej Filip's avatar
Ondřej Filip committed
164
    {
165 166
      OSPF_TRACE(D_PACKETS, "Strange LSACK from nbr %R on %s", n->rid, ifa->ifname);
      OSPF_TRACE(D_PACKETS, "    Type: %04x, Id: %R, Rt: %R",
167
		 lsa_type, lsa.id, lsa.rt);
168 169 170 171
      OSPF_TRACE(D_PACKETS, "    I have: Seq: %08x, Age: %4u, Sum: %04x",
		 ret->lsa.sn, ret->lsa.age, ret->lsa.checksum);
      OSPF_TRACE(D_PACKETS, "    It has: Seq: %08x, Age: %4u, Sum: %04x",
		 lsa.sn, lsa.age, lsa.checksum);
Ondřej Filip's avatar
Ondřej Filip committed
172 173 174
      continue;
    }

175 176
    DBG("Deleting LSA (Type: %04x Id: %R Rt: %R) from lsrtl for neighbor %R\n",
	lsa_type, lsa.id, lsa.rt, n->rid);
177 178 179

    en = ospf_hash_find_entry(p->gr, ret);
    ospf_lsa_lsrt_down_(en, n, ret);
180
  }
181
}