summaryrefslogtreecommitdiff
path: root/proto/ospf/lsack.c
blob: 1654953f72931f57caf1af4e9dbb0eff6bd3b5c3 (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
/*
 *	BIRD -- OSPF
 *
 *	(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.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"


/*
struct ospf_lsack_packet
{
  struct ospf_packet hdr;
  // union ospf_auth auth;

  struct ospf_lsa_header lsas[];
};
*/

struct lsa_node
{
  node n;
  struct ospf_lsa_header lsa;
};


static inline void
ospf_lsack_body(struct ospf_proto *p, struct ospf_packet *pkt,
		struct ospf_lsa_header **body, uint *count)
{
  uint plen = ntohs(pkt->length);
  uint hlen = ospf_pkt_hdrlen(p);

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

static void
ospf_dump_lsack(struct ospf_proto *p, struct ospf_packet *pkt)
{
  struct ospf_lsa_header *lsas;
  uint i, lsa_count;

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

  ospf_lsack_body(p, pkt, &lsas, &lsa_count);
  for (i = 0; i < lsa_count; i++)
    ospf_dump_lsahdr(p, lsas + i);
}


void
ospf_enqueue_lsack(struct ospf_neighbor *n, struct ospf_lsa_header *h_n, int queue)
{
  /* Note that h_n is in network endianity */
  struct lsa_node *no = mb_alloc(n->pool, sizeof(struct lsa_node));
  no->n = (node) {};
  memcpy(&no->lsa, h_n, sizeof(struct ospf_lsa_header));
  add_tail(&n->ackl[queue], NODE no);
  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);
}

void
ospf_reset_lsack_queue(struct ospf_neighbor *n)
{
  struct lsa_node *no;

  WALK_LIST_FIRST(no, n->ackl[ACKL_DELAY])
  {
    rem_node(NODE no);
    mb_free(no);
  }
}

static inline void
ospf_send_lsack_(struct ospf_proto *p, struct ospf_neighbor *n, int queue)
{
  struct ospf_iface *ifa = n->ifa;
  struct ospf_lsa_header *lsas;
  struct ospf_packet *pkt;
  struct lsa_node *no;
  uint i, lsa_max, length;

  /* RFC 2328 13.5 */

  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++)
  {
    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);
    rem_node(NODE no);
    mb_free(no);
  }

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

  if (queue == ACKL_DIRECT)
  {
    OSPF_PACKET(ospf_dump_lsack, pkt, "LSACK packet sent to nbr %R on %s", n->rid, ifa->ifname);
    ospf_send_to(ifa, n->ip);
  }
  else
  {
    OSPF_PACKET(ospf_dump_lsack, pkt, "LSACK packet sent via %s", ifa->ifname);
    ospf_send_to_iface(ifa);
  }
}

void
ospf_send_lsack(struct ospf_proto *p, struct ospf_neighbor *n, int queue)
{
  while (!EMPTY_LIST(n->ackl[queue]))
    ospf_send_lsack_(p, n, queue);
}

void
ospf_receive_lsack(struct ospf_packet *pkt, struct ospf_iface *ifa,
		   struct ospf_neighbor *n)
{
  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;

  /* RFC 2328 13.7 */

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

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

  if (n->state < NEIGHBOR_EXCHANGE)
  {
    OSPF_TRACE(D_PACKETS, "LSACK packet ignored - lesser state than Exchange");
    return;
  }

  ospf_neigh_sm(n, INM_HELLOREC);	/* Not in RFC */

  ospf_lsack_body(p, pkt, &lsas, &lsa_count);
  for (i = 0; i < lsa_count; i++)
  {
    lsa_ntoh_hdr(&lsas[i], &lsa);
    lsa_get_type_domain(&lsa, n->ifa, &lsa_type, &lsa_domain);

    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)
    {
      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",
		 lsa_type, lsa.id, lsa.rt);
      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);
      continue;
    }

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

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