GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ripd/rde.c Lines: 0 231 0.0 %
Date: 2017-11-13 Branches: 0 125 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: rde.c,v 1.21 2016/09/03 10:28:08 renato Exp $ */
2
3
/*
4
 * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it>
5
 * Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
6
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
7
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8
 *
9
 * Permission to use, copy, modify, and distribute this software for any
10
 * purpose with or without fee is hereby granted, provided that the above
11
 * copyright notice and this permission notice appear in all copies.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 */
21
22
#include <sys/socket.h>
23
#include <sys/queue.h>
24
#include <netinet/in.h>
25
#include <arpa/inet.h>
26
#include <err.h>
27
#include <errno.h>
28
#include <stdlib.h>
29
#include <signal.h>
30
#include <string.h>
31
#include <pwd.h>
32
#include <unistd.h>
33
#include <event.h>
34
35
#include "ripd.h"
36
#include "rip.h"
37
#include "ripe.h"
38
#include "log.h"
39
#include "rde.h"
40
41
#define	MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
42
43
struct ripd_conf	*rdeconf = NULL;
44
struct imsgev		*iev_ripe;
45
struct imsgev		*iev_main;
46
47
void	rde_sig_handler(int, short, void *);
48
__dead void rde_shutdown(void);
49
void	rde_dispatch_imsg(int, short, void *);
50
void	rde_dispatch_parent(int, short, void *);
51
int	rde_imsg_compose_ripe(int, u_int32_t, pid_t, void *, u_int16_t);
52
int	rde_check_route(struct rip_route *);
53
void	triggered_update(struct rt_node *);
54
55
/* ARGSUSED */
56
void
57
rde_sig_handler(int sig, short event, void *arg)
58
{
59
	/*
60
	 * signal handler rules don't apply, libevent decouples for us
61
	 */
62
63
	switch (sig) {
64
	case SIGINT:
65
	case SIGTERM:
66
		rde_shutdown();
67
		/* NOTREACHED */
68
	default:
69
		fatalx("unexpected signal");
70
	}
71
}
72
73
/* route decision engine */
74
pid_t
75
rde(struct ripd_conf *xconf, int pipe_parent2rde[2], int pipe_ripe2rde[2],
76
    int pipe_parent2ripe[2])
77
{
78
	struct event		 ev_sigint, ev_sigterm;
79
	struct passwd		*pw;
80
	struct redistribute	*r;
81
	pid_t			 pid;
82
83
	switch (pid = fork()) {
84
	case -1:
85
		fatal("cannot fork");
86
		/* NOTREACHED */
87
	case 0:
88
		break;
89
	default:
90
		return (pid);
91
	}
92
93
	rdeconf = xconf;
94
95
	if ((pw = getpwnam(RIPD_USER)) == NULL)
96
		fatal("getpwnam");
97
98
	if (chroot(pw->pw_dir) == -1)
99
		fatal("chroot");
100
	if (chdir("/") == -1)
101
		fatal("chdir(\"/\")");
102
103
	setproctitle("route decision engine");
104
	ripd_process = PROC_RDE_ENGINE;
105
	log_procname = log_procnames[ripd_process];
106
107
	if (setgroups(1, &pw->pw_gid) ||
108
	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
109
	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
110
		fatal("can't drop privileges");
111
112
	event_init();
113
114
	/* setup signal handler */
115
	signal_set(&ev_sigint, SIGINT, rde_sig_handler, NULL);
116
	signal_set(&ev_sigterm, SIGTERM, rde_sig_handler, NULL);
117
	signal_add(&ev_sigint, NULL);
118
	signal_add(&ev_sigterm, NULL);
119
	signal(SIGPIPE, SIG_IGN);
120
	signal(SIGHUP, SIG_IGN);
121
122
	/* setup pipes */
123
	close(pipe_ripe2rde[0]);
124
	close(pipe_parent2rde[0]);
125
	close(pipe_parent2ripe[0]);
126
	close(pipe_parent2ripe[1]);
127
128
	if ((iev_ripe = malloc(sizeof(struct imsgev))) == NULL ||
129
	    (iev_main = malloc(sizeof(struct imsgev))) == NULL)
130
		fatal(NULL);
131
	imsg_init(&iev_ripe->ibuf, pipe_ripe2rde[1]);
132
	iev_ripe->handler =  rde_dispatch_imsg;
133
	imsg_init(&iev_main->ibuf, pipe_parent2rde[1]);
134
	iev_main->handler = rde_dispatch_parent;
135
136
	/* setup event handler */
137
	iev_ripe->events = EV_READ;
138
	event_set(&iev_ripe->ev, iev_ripe->ibuf.fd, iev_ripe->events,
139
	    iev_ripe->handler, iev_ripe);
140
	event_add(&iev_ripe->ev, NULL);
141
142
	iev_main->events = EV_READ;
143
	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
144
	    iev_main->handler, iev_main);
145
	event_add(&iev_main->ev, NULL);
146
	rt_init();
147
148
	/* remove unneeded config stuff */
149
	while ((r = SIMPLEQ_FIRST(&rdeconf->redist_list)) != NULL) {
150
		SIMPLEQ_REMOVE_HEAD(&rdeconf->redist_list, entry);
151
		free(r);
152
	}
153
154
	event_dispatch();
155
156
	rde_shutdown();
157
	/* NOTREACHED */
158
159
	return (0);
160
}
161
162
__dead void
163
rde_shutdown(void)
164
{
165
	/* close pipes */
166
	msgbuf_clear(&iev_ripe->ibuf.w);
167
	close(iev_ripe->ibuf.fd);
168
	msgbuf_clear(&iev_main->ibuf.w);
169
	close(iev_main->ibuf.fd);
170
171
	rt_clear();
172
	free(iev_ripe);
173
	free(iev_main);
174
	free(rdeconf);
175
176
	log_info("route decision engine exiting");
177
	_exit(0);
178
}
179
180
int
181
rde_imsg_compose_ripe(int type, u_int32_t peerid, pid_t pid, void *data,
182
    u_int16_t datalen)
183
{
184
	return (imsg_compose_event(iev_ripe, type, peerid, pid, -1,
185
		    data, datalen));
186
}
187
188
/* ARGSUSED */
189
void
190
rde_dispatch_imsg(int fd, short event, void *bula)
191
{
192
	struct imsgev		*iev = bula;
193
	struct imsgbuf		*ibuf = &iev->ibuf;
194
	struct rip_route	 rr;
195
	struct imsg		 imsg;
196
	ssize_t			 n;
197
	int			 shut = 0, verbose;
198
199
	if (event & EV_READ) {
200
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
201
			fatal("imsg_read error");
202
		if (n == 0)	/* connection closed */
203
			shut = 1;
204
	}
205
	if (event & EV_WRITE) {
206
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
207
			fatal("msgbuf_write");
208
		if (n == 0)	/* connection closed */
209
			shut = 1;
210
	}
211
212
	for (;;) {
213
		if ((n = imsg_get(ibuf, &imsg)) == -1)
214
			fatal("rde_dispatch_imsg: imsg_get error");
215
		if (n == 0)
216
			break;
217
218
		switch (imsg.hdr.type) {
219
		case IMSG_ROUTE_FEED:
220
			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rr))
221
				fatalx("invalid size of RDE request");
222
223
			memcpy(&rr, imsg.data, sizeof(rr));
224
225
			if (rde_check_route(&rr) == -1)
226
				log_debug("rde_dispatch_imsg: "
227
				    "packet malformed\n");
228
			break;
229
		case IMSG_FULL_REQUEST:
230
			bzero(&rr, sizeof(rr));
231
			/*
232
			 * AFI == 0 && metric == INFINITY request the
233
			 * whole routing table
234
			 */
235
			rr.metric = INFINITY;
236
			rde_imsg_compose_ripe(IMSG_REQUEST_ADD, 0,
237
			    0, &rr, sizeof(rr));
238
			rde_imsg_compose_ripe(IMSG_SEND_REQUEST, 0,
239
			    0, NULL, 0);
240
			break;
241
		case IMSG_FULL_RESPONSE:
242
			rt_snap(imsg.hdr.peerid);
243
			rde_imsg_compose_ripe(IMSG_SEND_RESPONSE,
244
			    imsg.hdr.peerid, 0, NULL, 0);
245
			break;
246
		case IMSG_ROUTE_REQUEST:
247
			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rr))
248
				fatalx("invalid size of RDE request");
249
250
			memcpy(&rr, imsg.data, sizeof(rr));
251
252
			rt_complete(&rr);
253
			rde_imsg_compose_ripe(IMSG_RESPONSE_ADD,
254
			    imsg.hdr.peerid, 0, &rr, sizeof(rr));
255
256
			break;
257
		case IMSG_ROUTE_REQUEST_END:
258
			rde_imsg_compose_ripe(IMSG_SEND_RESPONSE,
259
			    imsg.hdr.peerid, 0, NULL, 0);
260
			break;
261
		case IMSG_CTL_SHOW_RIB:
262
			rt_dump(imsg.hdr.pid);
263
264
			imsg_compose_event(iev_ripe, IMSG_CTL_END, 0,
265
			    imsg.hdr.pid, -1, NULL, 0);
266
267
			break;
268
		case IMSG_CTL_LOG_VERBOSE:
269
			/* already checked by ripe */
270
			memcpy(&verbose, imsg.data, sizeof(verbose));
271
			log_verbose(verbose);
272
			break;
273
		default:
274
			log_debug("rde_dispatch_msg: unexpected imsg %d",
275
			    imsg.hdr.type);
276
			break;
277
		}
278
		imsg_free(&imsg);
279
	}
280
	if (!shut)
281
		imsg_event_add(iev);
282
	else {
283
		/* this pipe is dead, so remove the event handler */
284
		event_del(&iev->ev);
285
		event_loopexit(NULL);
286
	}
287
}
288
289
/* ARGSUSED */
290
void
291
rde_dispatch_parent(int fd, short event, void *bula)
292
{
293
	struct imsg		 imsg;
294
	struct rt_node		*rt;
295
	struct kroute		 kr;
296
	struct imsgev		*iev = bula;
297
	struct imsgbuf		*ibuf = &iev->ibuf;
298
	ssize_t			 n;
299
	int			 shut = 0;
300
301
	if (event & EV_READ) {
302
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
303
			fatal("imsg_read error");
304
		if (n == 0)	/* connection closed */
305
			shut = 1;
306
	}
307
	if (event & EV_WRITE) {
308
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
309
			fatal("msgbuf_write");
310
		if (n == 0)	/* connection closed */
311
			shut = 1;
312
	}
313
314
	for (;;) {
315
		if ((n = imsg_get(ibuf, &imsg)) == -1)
316
			fatal("rde_dispatch_parent: imsg_get error");
317
		if (n == 0)
318
			break;
319
320
		switch (imsg.hdr.type) {
321
		case IMSG_NETWORK_ADD:
322
			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(kr)) {
323
				log_warnx("rde_dispatch: wrong imsg len");
324
				break;
325
			}
326
327
			memcpy(&kr, imsg.data, sizeof(kr));
328
329
			rt = rt_new_kr(&kr);
330
			rt_insert(rt);
331
			break;
332
		case IMSG_NETWORK_DEL:
333
			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(kr)) {
334
				log_warnx("rde_dispatch: wrong imsg len");
335
				break;
336
			}
337
			memcpy(&kr, imsg.data, sizeof(kr));
338
339
			if ((rt = rt_find(kr.prefix.s_addr,
340
			    kr.netmask.s_addr)) != NULL)
341
				rt_remove(rt);
342
			break;
343
		default:
344
			log_debug("rde_dispatch_parent: unexpected imsg %d",
345
			    imsg.hdr.type);
346
			break;
347
		}
348
		imsg_free(&imsg);
349
	}
350
	if (!shut)
351
		imsg_event_add(iev);
352
	else {
353
		/* this pipe is dead, so remove the event handler */
354
		event_del(&iev->ev);
355
		event_loopexit(NULL);
356
	}
357
}
358
359
void
360
rde_send_change_kroute(struct rt_node *r)
361
{
362
	struct kroute	 kr;
363
364
	bzero(&kr, sizeof(kr));
365
	kr.prefix.s_addr = r->prefix.s_addr;
366
	kr.nexthop.s_addr = r->nexthop.s_addr;
367
	kr.netmask.s_addr = r->netmask.s_addr;
368
	kr.metric = r->metric;
369
	kr.flags = r->flags;
370
	kr.ifindex = r->ifindex;
371
372
	imsg_compose_event(iev_main, IMSG_KROUTE_CHANGE, 0, 0, -1,
373
	    &kr, sizeof(kr));
374
}
375
376
void
377
rde_send_delete_kroute(struct rt_node *r)
378
{
379
	struct kroute	 kr;
380
381
	bzero(&kr, sizeof(kr));
382
	kr.prefix.s_addr = r->prefix.s_addr;
383
	kr.nexthop.s_addr = r->nexthop.s_addr;
384
	kr.netmask.s_addr = r->netmask.s_addr;
385
	kr.metric = r->metric;
386
	kr.flags = r->flags;
387
	kr.ifindex = r->ifindex;
388
389
	imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 0, 0, -1,
390
	    &kr, sizeof(kr));
391
}
392
393
int
394
rde_check_route(struct rip_route *e)
395
{
396
	struct timeval	 tv, now;
397
	struct rt_node	*rn;
398
	struct iface	*iface;
399
	u_int8_t	 metric;
400
401
	if ((e->nexthop.s_addr & htonl(IN_CLASSA_NET)) ==
402
	    htonl(INADDR_LOOPBACK & IN_CLASSA_NET) ||
403
	    e->nexthop.s_addr == INADDR_ANY)
404
		return (-1);
405
406
	if ((iface = if_find_index(e->ifindex)) == NULL)
407
		return (-1);
408
409
	metric = MINIMUM(INFINITY, e->metric + iface->cost);
410
411
	if ((rn = rt_find(e->address.s_addr, e->mask.s_addr)) == NULL) {
412
		if (metric >= INFINITY)
413
			return (0);
414
		rn = rt_new_rr(e, metric);
415
		rt_insert(rn);
416
		rde_send_change_kroute(rn);
417
		route_start_timeout(rn);
418
		triggered_update(rn);
419
	} else {
420
		/*
421
		 * XXX don't we have to track all incoming routes?
422
		 * what happens if the kernel route is removed later.
423
		 */
424
		if (rn->flags & F_KERNEL)
425
			return (0);
426
427
		if (metric < rn->metric) {
428
			rn->metric = metric;
429
			rn->nexthop.s_addr = e->nexthop.s_addr;
430
			rn->ifindex = e->ifindex;
431
			rde_send_change_kroute(rn);
432
			triggered_update(rn);
433
		} else if (e->nexthop.s_addr == rn->nexthop.s_addr &&
434
		    metric > rn->metric) {
435
				rn->metric = metric;
436
				rde_send_change_kroute(rn);
437
				triggered_update(rn);
438
				if (rn->metric == INFINITY)
439
					route_start_garbage(rn);
440
		} else if (e->nexthop.s_addr != rn->nexthop.s_addr &&
441
		    metric == rn->metric) {
442
			/* If the new metric is the same as the old one,
443
			 * examine the timeout for the existing route.  If it
444
			 * is at least halfway to the expiration point, switch
445
			 * to the new route.
446
			 */
447
			timerclear(&tv);
448
			gettimeofday(&now, NULL);
449
			evtimer_pending(&rn->timeout_timer, &tv);
450
			if (tv.tv_sec - now.tv_sec < ROUTE_TIMEOUT / 2) {
451
				rn->nexthop.s_addr = e->nexthop.s_addr;
452
				rn->ifindex = e->ifindex;
453
				rde_send_change_kroute(rn);
454
			}
455
		}
456
457
		if (e->nexthop.s_addr == rn->nexthop.s_addr &&
458
		    rn->metric < INFINITY)
459
			route_reset_timers(rn);
460
	}
461
462
	return (0);
463
}
464
465
void
466
triggered_update(struct rt_node *rn)
467
{
468
	struct rip_route	 rr;
469
470
	rr.address.s_addr = rn->prefix.s_addr;
471
	rr.mask.s_addr = rn->netmask.s_addr;
472
	rr.nexthop.s_addr = rn->nexthop.s_addr;
473
	rr.metric = rn->metric;
474
	rr.ifindex = rn->ifindex;
475
476
	rde_imsg_compose_ripe(IMSG_SEND_TRIGGERED_UPDATE, 0, 0, &rr,
477
	    sizeof(struct rip_route));
478
}