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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
/* $OpenBSD: rde.c,v 1.3 2005/02/02 19:15:07 henning Exp $ */
/*
* Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
* Copyright (c) 2004 Esben Norby <norby@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@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 <sys/queue.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <pwd.h>
#include <unistd.h>
#include <event.h>
#include "ospf.h"
#include "ospfd.h"
#include "ospfe.h"
#include "log.h"
#include "rde.h"
void rde_sig_handler(int sig, short, void *);
void rde_shutdown(void);
void rde_dispatch_imsg(int, short, void *);
void rde_nbr_init(u_int32_t);
struct rde_nbr *rde_nbr_find(u_int32_t);
struct rde_nbr *rde_nbr_new(u_int32_t, struct rde_nbr *);
void rde_nbr_del(struct rde_nbr *);
volatile sig_atomic_t rde_quit = 0;
struct ospfd_conf *rdeconf = NULL;
struct imsgbuf *ibuf_ospfe;
struct imsgbuf *ibuf_main;
void
rde_sig_handler(int sig, short event, void *arg)
{
/*
* signal handler rules don't apply, libevent decouples for us
*/
switch (sig) {
case SIGINT:
case SIGTERM:
rde_shutdown();
/* NOTREACHED */
default:
fatalx("unexpected signal");
/* NOTREACHED */
}
}
/* route decision engine */
pid_t
rde(struct ospfd_conf *xconf, int pipe_parent2rde[2], int pipe_ospfe2rde[2],
int pipe_parent2ospfe[2])
{
struct passwd *pw;
struct event ev_sigint, ev_sigterm;
pid_t pid;
switch (pid = fork()) {
case -1:
fatal("cannot fork");
case 0:
break;
default:
return (pid);
}
rdeconf = xconf;
if ((pw = getpwnam(OSPFD_USER)) == NULL)
fatal("getpwnam");
if (chroot(pw->pw_dir) == -1)
fatal("chroot");
if (chdir("/") == -1)
fatal("chdir(\"/\")");
setproctitle("route decision engine");
ospfd_process = PROC_RDE_ENGINE;
if (setgroups(1, &pw->pw_gid) ||
setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
seteuid(pw->pw_uid) || setuid(pw->pw_uid)) {
fatal("can't drop privileges");
}
endpwent();
event_init();
rde_nbr_init(NBR_HASHSIZE);
lsa_init(&rdeconf->lsa_tree);
/* setup signal handler */
signal_set(&ev_sigint, SIGINT, rde_sig_handler, NULL);
signal_set(&ev_sigterm, SIGTERM, rde_sig_handler, NULL);
signal_add(&ev_sigint, NULL);
signal_add(&ev_sigterm, NULL);
/* setup pipes */
close(pipe_ospfe2rde[0]);
close(pipe_parent2rde[0]);
close(pipe_parent2ospfe[0]);
close(pipe_parent2ospfe[1]);
if ((ibuf_ospfe = malloc(sizeof(struct imsgbuf))) == NULL ||
(ibuf_main = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_ospfe, pipe_ospfe2rde[1], rde_dispatch_imsg);
imsg_init(ibuf_main, pipe_parent2rde[1], rde_dispatch_imsg);
/* setup event handler */
ibuf_ospfe->events = EV_READ;
event_set(&ibuf_ospfe->ev, ibuf_ospfe->fd, ibuf_ospfe->events,
ibuf_ospfe->handler, ibuf_ospfe);
event_add(&ibuf_ospfe->ev, NULL);
ibuf_main->events = EV_READ;
event_set(&ibuf_main->ev, ibuf_main->fd, ibuf_main->events,
ibuf_main->handler, ibuf_main);
event_add(&ibuf_main->ev, NULL);
event_dispatch();
rde_shutdown();
/* NOTREACHED */
return (0);
}
void
rde_shutdown(void)
{
/* ... */
msgbuf_write(&ibuf_ospfe->w);
msgbuf_clear(&ibuf_ospfe->w);
free(ibuf_ospfe);
msgbuf_write(&ibuf_main->w);
msgbuf_clear(&ibuf_main->w);
free(ibuf_main);
log_info("route decision engine exiting");
_exit(0);
}
/* imesg */
int
rde_imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen)
{
return (imsg_compose(ibuf_main, type, 0, pid, -1, data, datalen));
}
int
rde_imsg_compose_ospfe(int type, u_int32_t peerid, pid_t pid, void *data,
u_int16_t datalen)
{
return (imsg_compose(ibuf_ospfe, type, peerid, pid, -1, data, datalen));
}
void
rde_dispatch_imsg(int fd, short event, void *bula)
{
struct imsgbuf *ibuf = bula;
struct imsg imsg;
struct in_addr aid;
struct ls_req_hdr req_hdr;
struct lsa_hdr lsa_hdr, *db_hdr;
struct rde_nbr rn, *nbr;
struct lsa *lsa;
struct area *area;
struct vertex *v;
char *buf;
time_t now;
int r, n, state, self;
u_int16_t l;
switch (event) {
case EV_READ:
if ((n = imsg_read(ibuf)) == -1)
fatal("imsg_read error");
if (n == 0) /* connection closed */
fatalx("pipe closed");
break;
case EV_WRITE:
if (msgbuf_write(&ibuf->w) == -1)
fatal("msgbuf_write");
imsg_event_add(ibuf);
return;
default:
fatalx("unknown event");
}
now = time(NULL);
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("rde_dispatch_imsg: imsg_read error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_NEIGHBOR_UP:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rn))
fatalx("invalid size of OE request");
memcpy(&rn, imsg.data, sizeof(rn));
if (rde_nbr_find(imsg.hdr.peerid))
fatalx("rde_dispatch_imsg: "
"neighbor already exists");
rde_nbr_new(imsg.hdr.peerid, &rn);
break;
case IMSG_NEIGHBOR_DOWN:
rde_nbr_del(rde_nbr_find(imsg.hdr.peerid));
break;
case IMSG_NEIGHBOR_CHANGE:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(state))
fatalx("invalid size of OE request");
memcpy(&state, imsg.data, sizeof(state));
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
nbr->state = state;
break;
case IMSG_DB_SNAPSHOT:
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
log_debug("rde_dispatch_imsg: IMSG_DB_SNAPSHOT, "
"neighbor %s", inet_ntoa(nbr->id));
lsa_snap(nbr->area, imsg.hdr.peerid);
imsg_compose(ibuf_ospfe, IMSG_DB_END, imsg.hdr.peerid,
0, -1, NULL, 0);
break;
case IMSG_DD:
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
log_debug("rde_dispatch_imsg: IMSG_DD, "
"neighbor %s", inet_ntoa(nbr->id));
buf = imsg.data;
for (l = imsg.hdr.len - IMSG_HEADER_SIZE;
l >= sizeof(lsa_hdr); l -= sizeof(lsa_hdr)) {
memcpy(&lsa_hdr, buf, sizeof(lsa_hdr));
buf += sizeof(lsa_hdr);
v = lsa_find(nbr->area, lsa_hdr.type,
lsa_hdr.ls_id, lsa_hdr.adv_rtr);
if (v == NULL)
db_hdr = NULL;
else
db_hdr = &v->lsa->hdr;
if (lsa_newer(&lsa_hdr, db_hdr) > 0)
/*
* only request LSA's that are
* newer or missing
*/
/* XXX add to local REQ list */
imsg_compose(ibuf_ospfe, IMSG_DD,
imsg.hdr.peerid, 0, -1,
&lsa_hdr, sizeof(lsa_hdr));
}
if (l != 0)
log_warnx("rde_dispatch_imsg: peerid %lu, "
"trailing garbage in Database Description "
"packet", imsg.hdr.peerid);
break;
case IMSG_LS_REQ:
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
log_debug("rde_dispatch_imsg: IMSG_LS_REQ, "
"neighbor %s len %d", inet_ntoa(nbr->id),
imsg.hdr.len - IMSG_HEADER_SIZE);
buf = imsg.data;
for (l = imsg.hdr.len - IMSG_HEADER_SIZE;
l >= sizeof(req_hdr); l -= sizeof(req_hdr)) {
memcpy(&req_hdr, buf, sizeof(req_hdr));
buf += sizeof(req_hdr);
if ((v = lsa_find(nbr->area,
ntohl(req_hdr.type), req_hdr.ls_id,
req_hdr.adv_rtr)) == NULL) {
imsg_compose(ibuf_ospfe, IMSG_LS_BADREQ,
imsg.hdr.peerid, 0, -1, NULL, 0);
continue;
}
imsg_compose(ibuf_ospfe, IMSG_LS_UPD,
imsg.hdr.peerid, 0, -1,
v->lsa, ntohs(v->lsa->hdr.len));
}
if (l != 0)
log_warnx("rde_dispatch_imsg: peerid %lu, "
"trailing garbage in LS Request "
"packet", imsg.hdr.peerid);
break;
case IMSG_LS_UPD:
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
log_debug("rde_dispatch_imsg: IMSG_LS_UPD, "
"neighbor %s len %d", inet_ntoa(nbr->id),
imsg.hdr.len - IMSG_HEADER_SIZE);
lsa = malloc(imsg.hdr.len - IMSG_HEADER_SIZE);
if (lsa == NULL)
fatal(NULL);
memcpy(lsa, imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
if (!lsa_check(nbr, lsa,
imsg.hdr.len - IMSG_HEADER_SIZE))
break;
v = lsa_find(nbr->area, lsa->hdr.type, lsa->hdr.ls_id,
lsa->hdr.adv_rtr);
if (v == NULL)
db_hdr = NULL;
else
db_hdr = &v->lsa->hdr;
r = lsa_newer(&lsa->hdr, db_hdr);
if (r > 0) {
/* new LSA newer than DB */
if (v && v->flooded &&
v->changed + MIN_LS_ARRIVAL >= now)
break;
if (!(self = lsa_self(nbr, lsa, v)))
lsa_add(nbr, lsa);
/* flood and perhaps ack LSA */
imsg_compose(ibuf_ospfe, IMSG_LS_FLOOD,
imsg.hdr.peerid, 0, -1,
lsa, ntohs(lsa->hdr.len));
/* reflood self originated LSA */
if (self && v)
imsg_compose(ibuf_ospfe, IMSG_LS_FLOOD,
v->nbr->peerid, 0, -1,
v->lsa, ntohs(v->lsa->hdr.len));
/* TODO LSA on req list -> BadLSReq */
} else if (r < 0) {
/* new LSA older than DB */
if (ntohl(db_hdr->seq_num) == MAX_SEQ_NUM &&
ntohs(db_hdr->age) == MAX_AGE)
/* seq-num wrap */
break;
if (v->changed + MIN_LS_ARRIVAL >= now)
break;
/* directly send current LSA, no ack */
imsg_compose(ibuf_ospfe, IMSG_LS_UPD,
imsg.hdr.peerid, 0, -1,
v->lsa, ntohs(v->lsa->hdr.len));
} else {
/* LSA equal send direct ack */
imsg_compose(ibuf_ospfe, IMSG_LS_ACK,
imsg.hdr.peerid, 0, -1, &lsa->hdr,
sizeof(lsa->hdr));
}
break;
case IMSG_LS_MAXAGE:
nbr = rde_nbr_find(imsg.hdr.peerid);
if (nbr == NULL)
fatalx("rde_dispatch_imsg: "
"neighbor does not exist");
log_debug("rde_dispatch_imsg: IMSG_LS_MAXAGE, "
"neighbor %s len %d", inet_ntoa(nbr->id),
imsg.hdr.len - IMSG_HEADER_SIZE);
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct lsa_hdr))
fatalx("invalid size of OE request");
memcpy(&lsa_hdr, imsg.data, sizeof(lsa_hdr));
if (rde_nbr_loading(nbr->area))
break;
lsa_del(nbr, &lsa_hdr);
break;
case IMSG_CTL_SHOW_DATABASE:
if (imsg.hdr.len != IMSG_HEADER_SIZE &&
imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(aid)) {
log_warnx("rde_dispatch: wrong imsg len");
break;
}
if (imsg.hdr.len == IMSG_HEADER_SIZE) {
LIST_FOREACH(area, &rdeconf->area_list, entry) {
imsg_compose(ibuf_ospfe, IMSG_CTL_AREA,
0, imsg.hdr.pid, -1, area,
sizeof(*area));
lsa_dump(&area->lsa_tree, imsg.hdr.pid);
}
lsa_dump(&rdeconf->lsa_tree, imsg.hdr.pid);
} else {
memcpy(&aid, imsg.data, sizeof(aid));
if ((area = area_find(rdeconf, aid)) != NULL) {
imsg_compose(ibuf_ospfe, IMSG_CTL_AREA,
0, imsg.hdr.pid, -1, area,
sizeof(*area));
lsa_dump(&area->lsa_tree, imsg.hdr.pid);
if (!area->stub)
lsa_dump(&rdeconf->lsa_tree,
imsg.hdr.pid);
}
}
imsg_compose(ibuf_ospfe, IMSG_CTL_END, 0, imsg.hdr.pid,
-1, NULL, 0);
break;
default:
log_debug("rde_dispatch_msg: unexpected imsg %d",
imsg.hdr.type);
break;
}
imsg_free(&imsg);
}
imsg_event_add(ibuf);
}
u_int32_t
rde_router_id(void)
{
return (rdeconf->rtr_id.s_addr);
}
LIST_HEAD(rde_nbr_head, rde_nbr);
struct nbr_table {
struct rde_nbr_head *hashtbl;
u_int32_t hashmask;
} rdenbrtable;
#define RDE_NBR_HASH(x) \
&rdenbrtable.hashtbl[(x) & rdenbrtable.hashmask]
void
rde_nbr_init(u_int32_t hashsize)
{
u_int32_t hs, i;
for (hs = 1; hs < hashsize; hs <<= 1)
;
rdenbrtable.hashtbl = calloc(hs, sizeof(struct rde_nbr_head));
if (rdenbrtable.hashtbl == NULL)
fatal("rde_nbr_init");
for (i = 0; i < hs; i++)
LIST_INIT(&rdenbrtable.hashtbl[i]);
rdenbrtable.hashmask = hs - 1;
}
struct rde_nbr *
rde_nbr_find(u_int32_t peerid)
{
struct rde_nbr_head *head;
struct rde_nbr *nbr;
head = RDE_NBR_HASH(peerid);
LIST_FOREACH(nbr, head, hash) {
if (nbr->peerid == peerid)
return (nbr);
}
return (NULL);
}
struct rde_nbr *
rde_nbr_new(u_int32_t peerid, struct rde_nbr *new)
{
struct rde_nbr_head *head;
struct rde_nbr *nbr;
struct area *area;
if (rde_nbr_find(peerid))
return (NULL);
if ((area = area_find(rdeconf, new->area_id)) == NULL)
fatalx("rde_nbr_new: unknown area");
if ((nbr = calloc(1, sizeof(*nbr))) == NULL)
fatal("rde_nbr_new");
memcpy(nbr, new, sizeof(*nbr));
nbr->peerid = peerid;
nbr->area = area;
head = RDE_NBR_HASH(peerid);
LIST_INSERT_HEAD(head, nbr, hash);
LIST_INSERT_HEAD(&area->nbr_list, nbr, entry);
return (nbr);
}
void
rde_nbr_del(struct rde_nbr *nbr)
{
if (nbr == NULL)
return;
LIST_REMOVE(nbr, entry);
LIST_REMOVE(nbr, hash);
free(nbr);
}
int
rde_nbr_loading(struct area *area)
{
struct rde_nbr *nbr;
LIST_FOREACH(nbr, &area->nbr_list, entry) {
if (nbr->self)
continue;
if (nbr->state & NBR_STA_XCHNG ||
nbr->state & NBR_STA_LOAD)
return (1);
}
return (0);
}
|