xref: /openbsd/usr.sbin/ripd/ripe.c (revision f1b790a5)
1 /*	$OpenBSD: ripe.c,v 1.37 2024/11/21 13:38:15 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Michele Marchetto <mydecay@openbeer.it>
5  * Copyright (c) 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/types.h>
23 #include <sys/socket.h>
24 #include <sys/queue.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <net/if_types.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <unistd.h>
34 #include <event.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <stdio.h>
38 
39 #include "ripd.h"
40 #include "rip.h"
41 #include "ripe.h"
42 #include "log.h"
43 #include "control.h"
44 
45 void		 ripe_sig_handler(int, short, void *);
46 __dead void	 ripe_shutdown(void);
47 
48 struct ripd_conf	*oeconf = NULL;
49 static struct imsgev	*iev_main;
50 static struct imsgev	*iev_rde;
51 
52 void
ripe_sig_handler(int sig,short event,void * bula)53 ripe_sig_handler(int sig, short event, void *bula)
54 {
55 	switch (sig) {
56 	case SIGINT:
57 	case SIGTERM:
58 		ripe_shutdown();
59 		/* NOTREACHED */
60 	default:
61 		fatalx("unexpected signal");
62 	}
63 }
64 
65 /* rip engine */
66 pid_t
ripe(struct ripd_conf * xconf,int pipe_parent2ripe[2],int pipe_ripe2rde[2],int pipe_parent2rde[2])67 ripe(struct ripd_conf *xconf, int pipe_parent2ripe[2], int pipe_ripe2rde[2],
68     int pipe_parent2rde[2])
69 {
70 	struct event		 ev_sigint, ev_sigterm;
71 	struct sockaddr_in	 addr;
72 	struct iface		*iface = NULL;
73 	struct passwd		*pw;
74 	struct redistribute	*r;
75 	pid_t			 pid;
76 
77 	switch (pid = fork()) {
78 	case -1:
79 		fatal("cannot fork");
80 	case 0:
81 		break;
82 	default:
83 		return (pid);
84 	}
85 
86 	/* create ripd control socket outside chroot */
87 	if (control_init(xconf->csock) == -1)
88 		fatalx("control socket setup failed");
89 
90 	addr.sin_family = AF_INET;
91 	addr.sin_port = htons(RIP_PORT);
92 	addr.sin_addr.s_addr = INADDR_ANY;
93 
94 	if ((xconf->rip_socket = socket(AF_INET,
95 	    SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
96 	    IPPROTO_UDP)) == -1)
97 		fatalx("error creating socket");
98 
99 	if (bind(xconf->rip_socket, (struct sockaddr *)&addr,
100 	    sizeof(addr)) == -1)
101 		fatal("error binding socket");
102 
103 	/* set some defaults */
104 	if (if_set_opt(xconf->rip_socket) == -1)
105 		fatal("if_set_opt");
106 
107 	if (if_set_mcast_ttl(xconf->rip_socket, IP_DEFAULT_MULTICAST_TTL) == -1)
108 		fatal("if_set_mcast_ttl");
109 
110 	if (if_set_mcast_loop(xconf->rip_socket) == -1)
111 		fatal("if_set_mcast_loop");
112 
113 	if (if_set_tos(xconf->rip_socket, IPTOS_PREC_INTERNETCONTROL) == -1)
114 		fatal("if_set_tos");
115 
116 	if_set_recvbuf(xconf->rip_socket);
117 
118 	oeconf = xconf;
119 
120 	if ((pw = getpwnam(RIPD_USER)) == NULL)
121 		fatal("getpwnam");
122 
123 	if (chroot(pw->pw_dir) == -1)
124 		fatal("chroot");
125 	if (chdir("/") == -1)
126 		fatal("chdir(\"/\")");
127 
128 	setproctitle("rip engine");
129 	log_procname = "ripe";
130 
131 	if (setgroups(1, &pw->pw_gid) ||
132 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
133 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
134 		fatal("can't drop privileges");
135 
136 	event_init();
137 	nbr_init(NBR_HASHSIZE);
138 
139 	/* setup signal handler */
140 	signal_set(&ev_sigint, SIGINT, ripe_sig_handler, NULL);
141 	signal_set(&ev_sigterm, SIGTERM, ripe_sig_handler, NULL);
142 	signal_add(&ev_sigint, NULL);
143 	signal_add(&ev_sigterm, NULL);
144 	signal(SIGPIPE, SIG_IGN);
145 	signal(SIGHUP, SIG_IGN);
146 
147 	/* setup pipes */
148 	close(pipe_parent2ripe[0]);
149 	close(pipe_ripe2rde[1]);
150 	close(pipe_parent2rde[0]);
151 	close(pipe_parent2rde[1]);
152 
153 	if ((iev_rde = malloc(sizeof(struct imsgev))) == NULL ||
154 	    (iev_main = malloc(sizeof(struct imsgev))) == NULL)
155 		fatal(NULL);
156 	if (imsgbuf_init(&iev_rde->ibuf, pipe_ripe2rde[0]) == -1)
157 		fatal(NULL);
158 	iev_rde->handler = ripe_dispatch_rde;
159 	if (imsgbuf_init(&iev_main->ibuf, pipe_parent2ripe[1]) == -1)
160 		fatal(NULL);
161 	iev_main->handler = ripe_dispatch_main;
162 
163 	/* setup event handler */
164 	iev_rde->events = EV_READ;
165 	event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
166 	    iev_rde->handler, iev_rde);
167 	event_add(&iev_rde->ev, NULL);
168 
169 	iev_main->events = EV_READ;
170 	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
171 	    iev_main->handler, iev_main);
172 	event_add(&iev_main->ev, NULL);
173 
174 	event_set(&oeconf->ev, oeconf->rip_socket, EV_READ|EV_PERSIST,
175 	    recv_packet, oeconf);
176 	event_add(&oeconf->ev, NULL);
177 
178 	/* remove unneeded config stuff */
179 	while ((r = SIMPLEQ_FIRST(&oeconf->redist_list)) != NULL) {
180 		SIMPLEQ_REMOVE_HEAD(&oeconf->redist_list, entry);
181 		free(r);
182 	}
183 
184 	/* listen on ripd control socket */
185 	control_listen();
186 
187 	/* start interfaces */
188 	LIST_FOREACH(iface, &xconf->iface_list, entry) {
189 		if_init(xconf, iface);
190 		if (if_fsm(iface, IF_EVT_UP))
191 			log_debug("ripe: error starting interface: %s",
192 			    iface->name);
193 	}
194 
195 	if (pledge("stdio inet mcast", NULL) == -1)
196 		fatal("pledge");
197 
198 	evtimer_set(&oeconf->report_timer, report_timer, oeconf);
199 	start_report_timer();
200 
201 	ripe_imsg_compose_rde(IMSG_FULL_REQUEST, 0, 0, NULL, 0);
202 
203 	event_dispatch();
204 
205 	ripe_shutdown();
206 	/* NOTREACHED */
207 	return (0);
208 }
209 
210 int
ripe_imsg_compose_parent(int type,pid_t pid,void * data,u_int16_t datalen)211 ripe_imsg_compose_parent(int type, pid_t pid, void *data, u_int16_t datalen)
212 {
213 	return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
214 }
215 
216 int
ripe_imsg_compose_rde(int type,u_int32_t peerid,pid_t pid,void * data,u_int16_t datalen)217 ripe_imsg_compose_rde(int type, u_int32_t peerid, pid_t pid,
218     void *data, u_int16_t datalen)
219 {
220 	return (imsg_compose_event(iev_rde, type, peerid, pid, -1,
221 	    data, datalen));
222 }
223 
224 void
ripe_dispatch_main(int fd,short event,void * bula)225 ripe_dispatch_main(int fd, short event, void *bula)
226 {
227 	struct imsg	 imsg;
228 	struct imsgev	*iev = bula;
229 	struct imsgbuf	*ibuf = &iev->ibuf;
230 	struct kif	*kif;
231 	struct iface	*iface;
232 	ssize_t		 n;
233 	int		 link_ok, shut = 0;
234 
235 	if (event & EV_READ) {
236 		if ((n = imsgbuf_read(ibuf)) == -1)
237 			fatal("imsgbuf_read error");
238 		if (n == 0)	/* connection closed */
239 			shut = 1;
240 	}
241 	if (event & EV_WRITE) {
242 		if (imsgbuf_write(ibuf) == -1) {
243 			if (errno == EPIPE)	/* connection closed */
244 				shut = 1;
245 			else
246 				fatal("imsgbuf_write");
247 		}
248 	}
249 
250 	for (;;) {
251 		if ((n = imsg_get(ibuf, &imsg)) == -1)
252 			fatal("ripe_dispatch_main: imsg_get error");
253 		if (n == 0)
254 			break;
255 
256 		switch (imsg.hdr.type) {
257 		case IMSG_IFINFO:
258 			if (imsg.hdr.len - IMSG_HEADER_SIZE !=
259 			    sizeof(struct kif))
260 				fatalx("IFINFO imsg with wrong len");
261 			kif = imsg.data;
262 			link_ok = (kif->flags & IFF_UP) &&
263 			    LINK_STATE_IS_UP(kif->link_state);
264 
265 			LIST_FOREACH(iface, &oeconf->iface_list, entry) {
266 				if (kif->ifindex == iface->ifindex) {
267 					iface->flags = kif->flags;
268 					iface->linkstate = kif->link_state;
269 
270 					if (link_ok) {
271 						if_fsm(iface, IF_EVT_UP);
272 						log_warnx("interface %s up",
273 						    iface->name);
274 					} else {
275 						if_fsm(iface, IF_EVT_DOWN);
276 						log_warnx("interface %s down",
277 						    iface->name);
278 					}
279 				}
280 			}
281 			break;
282 		case IMSG_CTL_IFINFO:
283 		case IMSG_CTL_KROUTE:
284 		case IMSG_CTL_KROUTE_ADDR:
285 		case IMSG_CTL_END:
286 			control_imsg_relay(&imsg);
287 			break;
288 		default:
289 			log_debug("ripe_dispatch_main: error handling imsg %d",
290 			    imsg.hdr.type);
291 			break;
292 		}
293 		imsg_free(&imsg);
294 	}
295 	if (!shut)
296 		imsg_event_add(iev);
297 	else {
298 		/* this pipe is dead, so remove the event handler */
299 		event_del(&iev->ev);
300 		event_loopexit(NULL);
301 	}
302 }
303 
304 void
ripe_dispatch_rde(int fd,short event,void * bula)305 ripe_dispatch_rde(int fd, short event, void *bula)
306 {
307 	struct rip_route	*rr;
308 	struct imsg		 imsg;
309 	struct imsgev		*iev = bula;
310 	struct imsgbuf		*ibuf = &iev->ibuf;
311 	struct iface		*iface;
312 	struct nbr		*nbr;
313 	ssize_t			 n;
314 	int			 shut = 0;
315 
316 	if (event & EV_READ) {
317 		if ((n = imsgbuf_read(ibuf)) == -1)
318 			fatal("imsgbuf_read error");
319 		if (n == 0)	/* connection closed */
320 			shut = 1;
321 	}
322 	if (event & EV_WRITE) {
323 		if (imsgbuf_write(ibuf) == -1) {
324 			if (errno == EPIPE)	/* connection closed */
325 				shut = 1;
326 			else
327 				fatal("imsgbuf_write");
328 		}
329 	}
330 
331 	for (;;) {
332 		if ((n = imsg_get(ibuf, &imsg)) == -1)
333 			fatal("ripe_dispatch_rde: imsg_get error");
334 		if (n == 0)
335 			break;
336 
337 		switch (imsg.hdr.type) {
338 		case IMSG_REQUEST_ADD:
339 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(*rr))
340 				fatalx("invalid size of RDE request");
341 
342 			if ((rr = malloc(sizeof(*rr))) == NULL)
343 				fatal("ripe_dispatch_rde");
344 
345 			memcpy(rr, imsg.data, sizeof(*rr));
346 
347 			if (imsg.hdr.peerid != 0) {
348 				if ((nbr = nbr_find_peerid(imsg.hdr.peerid)) ==
349 				    NULL) {
350 					log_debug("unknown neighbor id %u",
351 					    imsg.hdr.peerid);
352 					free(rr);
353 					break;
354 				}
355 				add_entry(&nbr->rq_list, rr);
356 				break;
357 			}
358 
359 			LIST_FOREACH(iface, &oeconf->iface_list, entry) {
360 				add_entry(&iface->rq_list, rr);
361 			}
362 			break;
363 		case IMSG_SEND_REQUEST:
364 			if (imsg.hdr.peerid != 0) {
365 				if ((nbr = nbr_find_peerid(imsg.hdr.peerid)) ==
366 				    NULL) {
367 					log_debug("unknown neighbor id %u",
368 					    imsg.hdr.peerid);
369 					break;
370 				}
371 				send_request(&nbr->rq_list, NULL, nbr);
372 				break;
373 			}
374 
375 			LIST_FOREACH(iface, &oeconf->iface_list, entry) {
376 				send_request(&iface->rq_list, iface, NULL);
377 			}
378 			break;
379 		case IMSG_RESPONSE_ADD:
380 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(*rr))
381 				fatalx("invalid size of RDE request");
382 
383 			if ((rr = malloc(sizeof(*rr))) == NULL)
384 				fatal("ripe_dispatch_rde");
385 
386 			memcpy(rr, imsg.data, sizeof(*rr));
387 
388 			if (imsg.hdr.peerid == 0) {
389 				LIST_FOREACH(iface, &oeconf->iface_list, entry)
390 					add_entry(&iface->rp_list, rr);
391 
392 				break;
393 			}
394 
395 			if ((nbr = nbr_find_peerid(imsg.hdr.peerid)) == NULL) {
396 				log_debug("unknown neighbor id %u",
397 				    imsg.hdr.peerid);
398 				free(rr);
399 				break;
400 			}
401 			add_entry(&nbr->rp_list, rr);
402 
403 			break;
404 		case IMSG_SEND_RESPONSE:
405 			if (imsg.hdr.peerid == 0) {
406 				LIST_FOREACH(iface, &oeconf->iface_list,
407 				    entry) {
408 					send_response(&iface->rp_list,
409 					    iface, NULL);
410 				}
411 				break;
412 			}
413 
414 			if ((nbr = nbr_find_peerid(imsg.hdr.peerid)) == NULL) {
415 				log_debug("unknown neighbor id %u",
416 				    imsg.hdr.peerid);
417 				break;
418 			}
419 			send_response(&nbr->rp_list, NULL, nbr);
420 			nbr_fsm(nbr, NBR_EVT_RESPONSE_SENT);
421 			break;
422 		case IMSG_SEND_TRIGGERED_UPDATE:
423 			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct
424 			    rip_route))
425 				fatalx("invalid size of RDE request");
426 
427 			rr = imsg.data;
428 
429 			LIST_FOREACH(iface, &oeconf->iface_list,
430 			    entry) {
431 				if (rr->ifindex != iface->ifindex)
432 					send_triggered_update(iface, rr);
433 			}
434 			break;
435 		case IMSG_CTL_END:
436 		case IMSG_CTL_SHOW_RIB:
437 			control_imsg_relay(&imsg);
438 			break;
439 		default:
440 			log_debug("ripe_dispatch_rde: error handling imsg %d",
441 			    imsg.hdr.type);
442 			break;
443 		}
444 		imsg_free(&imsg);
445 	}
446 	if (!shut)
447 		imsg_event_add(iev);
448 	else {
449 		/* this pipe is dead, so remove the event handler */
450 		event_del(&iev->ev);
451 		event_loopexit(NULL);
452 	}
453 }
454 
455 __dead void
ripe_shutdown(void)456 ripe_shutdown(void)
457 {
458 	struct iface	*iface;
459 
460 	/* close pipes */
461 	imsgbuf_write(&iev_rde->ibuf);
462 	imsgbuf_clear(&iev_rde->ibuf);
463 	close(iev_rde->ibuf.fd);
464 	imsgbuf_write(&iev_main->ibuf);
465 	imsgbuf_clear(&iev_main->ibuf);
466 	close(iev_main->ibuf.fd);
467 
468 	LIST_FOREACH(iface, &oeconf->iface_list, entry) {
469 		if (if_fsm(iface, IF_EVT_DOWN)) {
470 			log_debug("error stopping interface %s",
471 			    iface->name);
472 		}
473 	}
474 	while ((iface = LIST_FIRST(&oeconf->iface_list)) != NULL) {
475 		LIST_REMOVE(iface, entry);
476 
477 		/* revert the demotion when the interface is deleted */
478 		if (iface->state == IF_STA_DOWN)
479 			ripe_demote_iface(iface, 1);
480 
481 		if_del(iface);
482 	}
483 
484 	close(oeconf->rip_socket);
485 
486 	/* clean up */
487 	free(iev_rde);
488 	free(iev_main);
489 	free(oeconf);
490 
491 	log_info("rip engine exiting");
492 	_exit(0);
493 }
494 
495 void
ripe_iface_ctl(struct ctl_conn * c,unsigned int idx)496 ripe_iface_ctl(struct ctl_conn *c, unsigned int idx)
497 {
498 	struct iface		*iface;
499 	struct ctl_iface	*ictl;
500 
501 	LIST_FOREACH(iface, &oeconf->iface_list, entry) {
502 		if (idx == 0 || idx == iface->ifindex) {
503 			ictl = if_to_ctl(iface);
504 			imsg_compose_event(&c->iev, IMSG_CTL_SHOW_IFACE,
505 			    0, 0, -1, ictl, sizeof(struct ctl_iface));
506 		}
507 	}
508 }
509 
510 void
ripe_nbr_ctl(struct ctl_conn * c)511 ripe_nbr_ctl(struct ctl_conn *c)
512 {
513 	struct iface	*iface;
514 	struct nbr	*nbr;
515 	struct ctl_nbr	*nctl;
516 
517 	LIST_FOREACH(iface, &oeconf->iface_list, entry)
518 		LIST_FOREACH(nbr, &iface->nbr_list, entry) {
519 				nctl = nbr_to_ctl(nbr);
520 				imsg_compose_event(&c->iev,
521 				    IMSG_CTL_SHOW_NBR, 0, 0, -1, nctl,
522 				    sizeof(struct ctl_nbr));
523 		}
524 
525 	imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 0, -1, NULL, 0);
526 }
527 
528 void
ripe_demote_iface(struct iface * iface,int active)529 ripe_demote_iface(struct iface *iface, int active)
530 {
531 	struct demote_msg	dmsg;
532 
533 	if (iface->demote_group[0] == '\0')
534 		return;
535 
536 	bzero(&dmsg, sizeof(dmsg));
537 	strlcpy(dmsg.demote_group, iface->demote_group,
538 	    sizeof(dmsg.demote_group));
539 	if (active)
540 		dmsg.level = -1;
541 	else
542 		dmsg.level = 1;
543 
544 	ripe_imsg_compose_parent(IMSG_DEMOTE, 0, &dmsg, sizeof(dmsg));
545 }
546