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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/* $OpenBSD: lsupdate.c,v 1.12 2005/10/03 15:05:49 msf Exp $ */
/*
* Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
* Copyright (c) 2004, 2005 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 <arpa/inet.h>
#include <stdlib.h>
#include <strings.h>
#include "ospf.h"
#include "ospfd.h"
#include "log.h"
#include "ospfe.h"
#include "rde.h"
extern struct ospfd_conf *oeconf;
extern struct imsgbuf *ibuf_rde;
/* link state update packet handling */
int
lsa_flood(struct iface *iface, struct nbr *originator, struct lsa_hdr *lsa_hdr,
void *data, u_int16_t len)
{
struct in_addr addr;
struct nbr *nbr;
struct lsa_entry *le = NULL;
int queued = 0, dont_ack = 0;
int r;
LIST_FOREACH(nbr, &iface->nbr_list, entry) {
if (nbr == iface->self)
continue;
if (!(nbr->state & NBR_STA_FLOOD))
continue;
if (iface->state & IF_STA_DROTHER)
if ((le = ls_retrans_list_get(iface->self, lsa_hdr)))
ls_retrans_list_free(nbr, le);
if ((le = ls_retrans_list_get(nbr, lsa_hdr)))
ls_retrans_list_free(nbr, le);
if (!(nbr->state & NBR_STA_FULL) &&
(le = ls_req_list_get(nbr, lsa_hdr)) != NULL) {
r = lsa_newer(lsa_hdr, le->le_lsa);
if (r > 0) {
/* to flood LSA is newer than requested */
ls_req_list_free(nbr, le);
/* new needs to be flooded */
} else if (r < 0) {
/* to flood LSA is older than requested */
continue;
} else {
/* LSA are equal */
ls_req_list_free(nbr, le);
continue;
}
}
if (nbr == originator) {
dont_ack++;
continue;
}
queued = 1;
if (iface->state & IF_STA_DROTHER) {
if (!queued)
ls_retrans_list_add(iface->self, data);
} else
ls_retrans_list_add(nbr, data);
}
if (!queued)
return (0);
if (iface == originator->iface && iface->self != originator) {
if (iface->dr == originator || iface->bdr == originator)
return (0);
if (iface->state & IF_STA_BACKUP)
return (0);
dont_ack++;
}
/* flood LSA but first set correct destination */
switch (iface->type) {
case IF_TYPE_POINTOPOINT:
inet_aton(AllSPFRouters, &addr);
send_ls_update(iface, addr, data, len);
break;
case IF_TYPE_BROADCAST:
if (iface->state & IF_STA_DRORBDR)
inet_aton(AllSPFRouters, &addr);
else
inet_aton(AllDRouters, &addr);
send_ls_update(iface, addr, data, len);
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;
if (!TAILQ_EMPTY(&nbr->ls_retrans_list)) {
le = TAILQ_LAST(&nbr->ls_retrans_list,
lsa_head);
if (lsa_hdr->type != le->le_lsa->type ||
lsa_hdr->ls_id != le->le_lsa->ls_id ||
lsa_hdr->adv_rtr != le->le_lsa->adv_rtr)
continue;
}
send_ls_update(iface, nbr->addr, data, len);
}
break;
default:
fatalx("lsa_flood: unknown interface type");
}
return (dont_ack == 2);
}
int
send_ls_update(struct iface *iface, struct in_addr addr, void *data, int len)
{
struct sockaddr_in dst;
struct buf *buf;
size_t pos;
u_int32_t nlsa;
u_int16_t age;
int ret;
/* XXX READ_BUF_SIZE */
if ((buf = buf_dynamic(PKG_DEF_SIZE, READ_BUF_SIZE)) == NULL)
fatal("send_ls_update");
/* set destination */
dst.sin_family = AF_INET;
dst.sin_len = sizeof(struct sockaddr_in);
dst.sin_addr.s_addr = addr.s_addr;
/* OSPF header */
if (gen_ospf_hdr(buf, iface, PACKET_TYPE_LS_UPDATE))
goto fail;
nlsa = htonl(1);
if (buf_add(buf, &nlsa, sizeof(nlsa)))
goto fail;
pos = buf->wpos;
if (buf_add(buf, data, len))
goto fail;
/* age LSA befor sending it out */
memcpy(&age, data, sizeof(age));
age = ntohs(age);
if ((age += iface->transmit_delay) >= MAX_AGE)
age = MAX_AGE;
age = ntohs(age);
memcpy(buf_seek(buf, pos, sizeof(age)), &age, sizeof(age));
/* update authentication and calculate checksum */
if (auth_gen(buf, iface))
goto fail;
ret = send_packet(iface, buf->buf, buf->wpos, &dst);
buf_free(buf);
return (ret);
fail:
log_warn("send_hello");
buf_free(buf);
return (-1);
}
void
recv_ls_update(struct nbr *nbr, char *buf, u_int16_t len)
{
struct lsa_hdr lsa;
u_int32_t nlsa;
if (len < sizeof(nlsa)) {
log_warnx("recv_ls_update: bad packet size, neighbor ID %s",
inet_ntoa(nbr->id));
return;
}
memcpy(&nlsa, buf, sizeof(nlsa));
nlsa = ntohl(nlsa);
buf += sizeof(nlsa);
len -= sizeof(nlsa);
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_update: 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:
for (; nlsa > 0 && len > 0; nlsa--) {
if (len < sizeof(lsa)) {
log_warnx("recv_ls_update: bad packet size, "
"neighbor ID %s", inet_ntoa(nbr->id));
return;
}
memcpy(&lsa, buf, sizeof(lsa));
if (len < ntohs(lsa.len)) {
log_warnx("recv_ls_update: bad packet size, "
"neighbor ID %s", inet_ntoa(nbr->id));
return;
}
imsg_compose(ibuf_rde, IMSG_LS_UPD, nbr->peerid, 0, -1,
buf, ntohs(lsa.len));
buf += ntohs(lsa.len);
len -= ntohs(lsa.len);
}
if (nlsa > 0 || len > 0) {
log_warnx("recv_ls_update: bad packet size, "
"neighbor ID %s", inet_ntoa(nbr->id));
return;
}
break;
default:
fatalx("recv_ls_update: unknown neighbor state");
}
return;
}
/* link state retransmit list */
void
ls_retrans_list_add(struct nbr *nbr, struct lsa_hdr *lsa)
{
struct timeval tv;
struct lsa_entry *le;
struct lsa_ref *ref;
if ((ref = lsa_cache_get(lsa)) == NULL)
fatalx("King Bula sez: somebody forgot to lsa_cache_add");
if ((le = calloc(1, sizeof(*le))) == NULL)
fatal("ls_retrans_list_add");
le->le_ref = ref;
TAILQ_INSERT_TAIL(&nbr->ls_retrans_list, le, entry);
if (!evtimer_pending(&nbr->ls_retrans_timer, NULL)) {
timerclear(&tv);
tv.tv_sec = nbr->iface->rxmt_interval;
if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1)
log_warn("ls_retrans_list_add: evtimer_add failed");
}
}
int
ls_retrans_list_del(struct nbr *nbr, struct lsa_hdr *lsa_hdr)
{
struct lsa_entry *le;
le = ls_retrans_list_get(nbr, lsa_hdr);
if (le == NULL)
return (-1);
if (lsa_hdr->seq_num == le->le_ref->hdr.seq_num &&
lsa_hdr->ls_chksum == le->le_ref->hdr.ls_chksum) {
ls_retrans_list_free(nbr, le);
return (0);
}
log_warnx("ls_retrans_list_del: invalid LS ack received, neighbor %s",
inet_ntoa(nbr->id));
return (-1);
}
struct lsa_entry *
ls_retrans_list_get(struct nbr *nbr, struct lsa_hdr *lsa_hdr)
{
struct lsa_entry *le;
TAILQ_FOREACH(le, &nbr->ls_retrans_list, entry) {
if ((lsa_hdr->type == le->le_ref->hdr.type) &&
(lsa_hdr->ls_id == le->le_ref->hdr.ls_id) &&
(lsa_hdr->adv_rtr == le->le_ref->hdr.adv_rtr))
return (le);
}
return (NULL);
}
void
ls_retrans_list_free(struct nbr *nbr, struct lsa_entry *le)
{
TAILQ_REMOVE(&nbr->ls_retrans_list, le, entry);
lsa_cache_put(le->le_ref, nbr);
free(le);
}
void
ls_retrans_list_clr(struct nbr *nbr)
{
struct lsa_entry *le;
while ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL)
ls_retrans_list_free(nbr, le);
}
int
ls_retrans_list_empty(struct nbr *nbr)
{
return (TAILQ_EMPTY(&nbr->ls_retrans_list));
}
void
ls_retrans_timer(int fd, short event, void *bula)
{
struct timeval tv;
struct in_addr addr;
struct nbr *nbr = bula;
struct lsa_entry *le;
if (nbr->iface->self == nbr) {
if (!(nbr->iface->state & IF_STA_DROTHER)) {
/*
* Iick, we are suddenly DR or BDDR so convert this
* retrans list into a real flood. I'm not 100% sure if
* using iface->self as originator is correct but we
* will flood the whole net with this and that's the
* idea.
*/
while ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) !=
NULL) {
lsa_flood(nbr->iface, nbr, &le->le_ref->hdr,
le->le_ref->data, le->le_ref->len);
ls_retrans_list_free(nbr, le);
}
return;
}
inet_aton(AllDRouters, &addr);
} else
memcpy(&addr, &nbr->addr, sizeof(addr));
if ((le = TAILQ_FIRST(&nbr->ls_retrans_list)) != NULL) {
send_ls_update(nbr->iface, addr, le->le_ref->data,
le->le_ref->len);
timerclear(&tv);
tv.tv_sec = nbr->iface->rxmt_interval;
if (evtimer_add(&nbr->ls_retrans_timer, &tv) == -1)
log_warn("ls_retrans_list_add: evtimer_add failed");
}
}
LIST_HEAD(lsa_cache_head, lsa_ref);
struct lsa_cache {
struct lsa_cache_head *hashtbl;
u_int32_t hashmask;
} lsacache;
struct lsa_ref *lsa_cache_look(struct lsa_hdr *);
struct lsa_cache_head *lsa_cache_hash(struct lsa_hdr *);
void
lsa_cache_init(u_int32_t hashsize)
{
u_int32_t hs, i;
for (hs = 1; hs < hashsize; hs <<= 1)
;
lsacache.hashtbl = calloc(hs, sizeof(struct lsa_cache_head));
if (lsacache.hashtbl == NULL)
fatal("lsa_cache_init");
for (i = 0; i < hs; i++)
LIST_INIT(&lsacache.hashtbl[i]);
lsacache.hashmask = hs - 1;
}
struct lsa_ref *
lsa_cache_add(void *data, u_int16_t len)
{
struct lsa_cache_head *head;
struct lsa_ref *ref, *old;
if ((ref = calloc(1, sizeof(*ref))) == NULL)
fatal("lsa_cache_add");
memcpy(&ref->hdr, data, sizeof(ref->hdr));
if ((old = lsa_cache_look(&ref->hdr))) {
free(ref);
old->refcnt++;
return (old);
}
if ((ref->data = malloc(len)) == NULL)
fatal("lsa_cache_add");
memcpy(ref->data, data, len);
ref->stamp = time(NULL);
ref->len = len;
ref->refcnt = 1;
head = lsa_cache_hash(&ref->hdr);
LIST_INSERT_HEAD(head, ref, entry);
return (ref);
}
struct lsa_ref *
lsa_cache_get(struct lsa_hdr *lsa_hdr)
{
struct lsa_ref *ref;
ref = lsa_cache_look(lsa_hdr);
if (ref)
ref->refcnt++;
return (ref);
}
void
lsa_cache_put(struct lsa_ref *ref, struct nbr *nbr)
{
if (--ref->refcnt > 0)
return;
if (ntohs(ref->hdr.age) >= MAX_AGE)
ospfe_imsg_compose_rde(IMSG_LS_MAXAGE, nbr->peerid, 0,
ref->data, sizeof(struct lsa_hdr));
free(ref->data);
LIST_REMOVE(ref, entry);
free(ref);
}
struct lsa_ref *
lsa_cache_look(struct lsa_hdr *lsa_hdr)
{
struct lsa_cache_head *head;
struct lsa_ref *ref;
head = lsa_cache_hash(lsa_hdr);
LIST_FOREACH(ref, head, entry) {
if (ref->hdr.type == lsa_hdr->type &&
ref->hdr.ls_id == lsa_hdr->ls_id &&
ref->hdr.adv_rtr == lsa_hdr->adv_rtr &&
ref->hdr.seq_num == lsa_hdr->seq_num &&
ref->hdr.ls_chksum == lsa_hdr->ls_chksum) {
/* found match */
return (ref);
}
}
return (NULL);
}
struct lsa_cache_head *
lsa_cache_hash(struct lsa_hdr *lsa_hdr)
{
u_int32_t hash = 8271;
hash ^= lsa_hdr->type;
hash ^= lsa_hdr->ls_id;
hash ^= lsa_hdr->adv_rtr;
hash ^= lsa_hdr->seq_num;
hash ^= lsa_hdr->ls_chksum;
hash &= lsacache.hashmask;
return (&lsacache.hashtbl[hash]);
}
|