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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* $OpenBSD: lsack.c,v 1.5 2010/05/26 13:56:08 nicm Exp $ */
/*
* Copyright (c) 2004, 2005, 2007 Esben Norby <norby@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include "ospf6d.h"
#include "ospf6.h"
#include "log.h"
#include "ospfe.h"
void start_ls_ack_tx_timer_now(struct iface *);
/* link state acknowledgement packet handling */
int
send_ls_ack(struct iface *iface, struct in6_addr addr, void *data, size_t len)
{
struct ibuf *buf;
int ret;
/* XXX IBUF_READ_SIZE */
if ((buf = ibuf_dynamic(PKG_DEF_SIZE, IBUF_READ_SIZE)) == NULL)
fatal("send_ls_ack");
/* OSPF header */
if (gen_ospf_hdr(buf, iface, PACKET_TYPE_LS_ACK))
goto fail;
/* LS ack(s) */
if (ibuf_add(buf, data, len))
goto fail;
/* calculate checksum */
if (upd_ospf_hdr(buf, iface))
goto fail;
ret = send_packet(iface, buf->buf, buf->wpos, &addr);
ibuf_free(buf);
return (ret);
fail:
log_warn("send_ls_ack");
ibuf_free(buf);
return (-1);
}
void
recv_ls_ack(struct nbr *nbr, char *buf, u_int16_t len)
{
struct lsa_hdr lsa_hdr;
switch (nbr->state) {
case NBR_STA_DOWN:
case NBR_STA_ATTEMPT:
case NBR_STA_INIT:
case NBR_STA_2_WAY:
case NBR_STA_XSTRT:
case NBR_STA_SNAP:
log_debug("recv_ls_ack: packet ignored in state %s, "
"neighbor ID %s", nbr_state_name(nbr->state),
inet_ntoa(nbr->id));
break;
case NBR_STA_XCHNG:
case NBR_STA_LOAD:
case NBR_STA_FULL:
while (len >= sizeof(lsa_hdr)) {
memcpy(&lsa_hdr, buf, sizeof(lsa_hdr));
if (lsa_hdr_check(nbr, &lsa_hdr)) {
/* try both list in case of DROTHER */
if (nbr->iface->state & IF_STA_DROTHER)
(void)ls_retrans_list_del(
nbr->iface->self, &lsa_hdr);
(void)ls_retrans_list_del(nbr, &lsa_hdr);
}
buf += sizeof(lsa_hdr);
len -= sizeof(lsa_hdr);
}
if (len > 0) {
log_warnx("recv_ls_ack: bad packet size, "
"neighbor ID %s", inet_ntoa(nbr->id));
return;
}
break;
default:
fatalx("recv_ls_ack: unknown neighbor state");
}
}
int
lsa_hdr_check(struct nbr *nbr, struct lsa_hdr *lsa_hdr)
{
/* invalid age */
if ((ntohs(lsa_hdr->age) < 1) || (ntohs(lsa_hdr->age) > MAX_AGE)) {
log_debug("lsa_hdr_check: invalid age, neighbor ID %s",
inet_ntoa(nbr->id));
return (0);
}
/* invalid type */
switch (ntohs(lsa_hdr->type)) {
case LSA_TYPE_LINK:
case LSA_TYPE_ROUTER:
case LSA_TYPE_NETWORK:
case LSA_TYPE_INTER_A_PREFIX:
case LSA_TYPE_INTER_A_ROUTER:
case LSA_TYPE_INTRA_A_PREFIX:
case LSA_TYPE_EXTERNAL:
break;
default:
log_debug("lsa_hdr_check: invalid LSA type %d, neighbor ID %s",
lsa_hdr->type, inet_ntoa(nbr->id));
return (0);
}
/* invalid sequence number */
if (ntohl(lsa_hdr->seq_num) == RESV_SEQ_NUM) {
log_debug("ls_hdr_check: invalid seq num, neighbor ID %s",
inet_ntoa(nbr->id));
return (0);
}
return (1);
}
/* link state ack list */
void
ls_ack_list_add(struct iface *iface, struct lsa_hdr *lsa)
{
struct lsa_entry *le;
if (lsa == NULL)
fatalx("ls_ack_list_add: no LSA header");
if ((le = calloc(1, sizeof(*le))) == NULL)
fatal("ls_ack_list_add");
if (ls_ack_list_empty(iface))
start_ls_ack_tx_timer(iface);
TAILQ_INSERT_TAIL(&iface->ls_ack_list, le, entry);
le->le_lsa = lsa;
iface->ls_ack_cnt++;
/* reschedule now if we have enough for a full packet */
if (iface->ls_ack_cnt >
((iface->mtu - PACKET_HDR) / sizeof(struct lsa_hdr))) {
start_ls_ack_tx_timer_now(iface);
}
}
void
ls_ack_list_free(struct iface *iface, struct lsa_entry *le)
{
TAILQ_REMOVE(&iface->ls_ack_list, le, entry);
free(le->le_lsa);
free(le);
iface->ls_ack_cnt--;
}
void
ls_ack_list_clr(struct iface *iface)
{
struct lsa_entry *le;
while ((le = TAILQ_FIRST(&iface->ls_ack_list)) != NULL) {
TAILQ_REMOVE(&iface->ls_ack_list, le, entry);
free(le->le_lsa);
free(le);
}
iface->ls_ack_cnt = 0;
}
int
ls_ack_list_empty(struct iface *iface)
{
return (TAILQ_EMPTY(&iface->ls_ack_list));
}
/* timers */
/* ARGSUSED */
void
ls_ack_tx_timer(int fd, short event, void *arg)
{
struct in6_addr addr;
struct iface *iface = arg;
struct lsa_hdr *lsa_hdr;
struct lsa_entry *le, *nle;
struct nbr *nbr;
char *buf;
char *ptr;
int cnt = 0;
if ((buf = calloc(1, READ_BUF_SIZE)) == NULL)
fatal("ls_ack_tx_timer");
while (!ls_ack_list_empty(iface)) {
ptr = buf;
cnt = 0;
for (le = TAILQ_FIRST(&iface->ls_ack_list); le != NULL &&
(ptr - buf < iface->mtu - PACKET_HDR); le = nle) {
nle = TAILQ_NEXT(le, entry);
memcpy(ptr, le->le_lsa, sizeof(struct lsa_hdr));
ptr += sizeof(*lsa_hdr);
ls_ack_list_free(iface, le);
cnt++;
}
/* send LS ack(s) but first set correct destination */
switch (iface->type) {
case IF_TYPE_POINTOPOINT:
inet_pton(AF_INET6, AllSPFRouters, &addr);
send_ls_ack(iface, addr, buf, ptr - buf);
break;
case IF_TYPE_BROADCAST:
if (iface->state & IF_STA_DRORBDR)
inet_pton(AF_INET6, AllSPFRouters, &addr);
else
inet_pton(AF_INET6, AllDRouters, &addr);
send_ls_ack(iface, addr, buf, ptr - buf);
break;
case IF_TYPE_NBMA:
case IF_TYPE_POINTOMULTIPOINT:
case IF_TYPE_VIRTUALLINK:
LIST_FOREACH(nbr, &iface->nbr_list, entry) {
if (nbr == iface->self)
continue;
if (!(nbr->state & NBR_STA_FLOOD))
continue;
send_ls_ack(iface, nbr->addr, buf, ptr - buf);
}
break;
default:
fatalx("lsa_ack_tx_timer: unknown interface type");
}
}
free(buf);
}
void
start_ls_ack_tx_timer(struct iface *iface)
{
struct timeval tv;
timerclear(&tv);
tv.tv_sec = iface->rxmt_interval / 2;
if (evtimer_add(&iface->lsack_tx_timer, &tv) == -1)
fatal("start_ls_ack_tx_timer");
}
void
start_ls_ack_tx_timer_now(struct iface *iface)
{
struct timeval tv;
timerclear(&tv);
if (evtimer_add(&iface->lsack_tx_timer, &tv) == -1)
fatal("start_ls_ack_tx_timer_now");
}
void
stop_ls_ack_tx_timer(struct iface *iface)
{
if (evtimer_del(&iface->lsack_tx_timer) == -1)
fatal("stop_ls_ack_tx_timer");
}
|