xref: /openbsd/usr.sbin/ospf6d/control.c (revision 898184e3)
1 /*	$OpenBSD: control.c,v 1.20 2013/03/22 14:25:31 sthen Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include "ospf6d.h"
30 #include "ospf6.h"
31 #include "ospfe.h"
32 #include "log.h"
33 #include "control.h"
34 
35 #define	CONTROL_BACKLOG	5
36 
37 struct ctl_conn	*control_connbyfd(int);
38 struct ctl_conn	*control_connbypid(pid_t);
39 void		 control_close(int);
40 
41 int
42 control_init(char *path)
43 {
44 	struct sockaddr_un	 sun;
45 	int			 fd;
46 	mode_t			 old_umask;
47 
48 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
49 		log_warn("control_init: socket");
50 		return (-1);
51 	}
52 
53 	bzero(&sun, sizeof(sun));
54 	sun.sun_family = AF_UNIX;
55 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
56 
57 	if (unlink(path) == -1)
58 		if (errno != ENOENT) {
59 			log_warn("control_init: unlink %s", path);
60 			close(fd);
61 			return (-1);
62 		}
63 
64 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
65 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
66 		log_warn("control_init: bind: %s", path);
67 		close(fd);
68 		umask(old_umask);
69 		return (-1);
70 	}
71 	umask(old_umask);
72 
73 	if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
74 		log_warn("control_init: chmod");
75 		close(fd);
76 		(void)unlink(path);
77 		return (-1);
78 	}
79 
80 	session_socket_blockmode(fd, BM_NONBLOCK);
81 	control_state.fd = fd;
82 
83 	return (0);
84 }
85 
86 int
87 control_listen(void)
88 {
89 	if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
90 		log_warn("control_listen: listen");
91 		return (-1);
92 	}
93 
94 	event_set(&control_state.ev, control_state.fd, EV_READ,
95 	    control_accept, NULL);
96 	event_add(&control_state.ev, NULL);
97 	evtimer_set(&control_state.evt, control_accept, NULL);
98 
99 	return (0);
100 }
101 
102 void
103 control_cleanup(char *path)
104 {
105 	event_del(&control_state.ev);
106 	event_del(&control_state.evt);
107 	if (path)
108 		unlink(path);
109 }
110 
111 /* ARGSUSED */
112 void
113 control_accept(int listenfd, short event, void *bula)
114 {
115 	int			 connfd;
116 	socklen_t		 len;
117 	struct sockaddr_un	 sun;
118 	struct ctl_conn		*c;
119 
120 	event_add(&control_state.ev, NULL);
121 	if ((event & EV_TIMEOUT))
122 		return;
123 
124 	len = sizeof(sun);
125 	if ((connfd = accept(listenfd,
126 	    (struct sockaddr *)&sun, &len)) == -1) {
127 		/*
128 		 * Pause accept if we are out of file descriptors, or
129 		 * libevent will haunt us here too.
130 		 */
131 		if (errno == ENFILE || errno == EMFILE) {
132 			struct timeval evtpause = { 1, 0 };
133 
134 			event_del(&control_state.ev);
135 			evtimer_add(&control_state.evt, &evtpause);
136 		} else if (errno != EWOULDBLOCK && errno != EINTR &&
137 		    errno != ECONNABORTED)
138 			log_warn("control_accept: accept");
139 		return;
140 	}
141 
142 	session_socket_blockmode(connfd, BM_NONBLOCK);
143 
144 	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
145 		log_warn("control_accept");
146 		close(connfd);
147 		return;
148 	}
149 
150 	imsg_init(&c->iev.ibuf, connfd);
151 	c->iev.handler = control_dispatch_imsg;
152 	c->iev.events = EV_READ;
153 	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
154 	    c->iev.handler, &c->iev);
155 	event_add(&c->iev.ev, NULL);
156 
157 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
158 }
159 
160 struct ctl_conn *
161 control_connbyfd(int fd)
162 {
163 	struct ctl_conn	*c;
164 
165 	for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.fd != fd;
166 	    c = TAILQ_NEXT(c, entry))
167 		;	/* nothing */
168 
169 	return (c);
170 }
171 
172 struct ctl_conn *
173 control_connbypid(pid_t pid)
174 {
175 	struct ctl_conn	*c;
176 
177 	for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.pid != pid;
178 	    c = TAILQ_NEXT(c, entry))
179 		;	/* nothing */
180 
181 	return (c);
182 }
183 
184 void
185 control_close(int fd)
186 {
187 	struct ctl_conn	*c;
188 
189 	if ((c = control_connbyfd(fd)) == NULL) {
190 		log_warn("control_close: fd %d: not found", fd);
191 		return;
192 	}
193 
194 	msgbuf_clear(&c->iev.ibuf.w);
195 	TAILQ_REMOVE(&ctl_conns, c, entry);
196 
197 	event_del(&c->iev.ev);
198 	close(c->iev.ibuf.fd);
199 
200 	/* Some file descriptors are available again. */
201 	if (evtimer_pending(&control_state.evt, NULL)) {
202 		evtimer_del(&control_state.evt);
203 		event_add(&control_state.ev, NULL);
204 	}
205 
206 	free(c);
207 }
208 
209 /* ARGSUSED */
210 void
211 control_dispatch_imsg(int fd, short event, void *bula)
212 {
213 	struct ctl_conn	*c;
214 	struct imsg	 imsg;
215 	int		 n;
216 	unsigned int	 ifidx;
217 	int		 verbose;
218 
219 	if ((c = control_connbyfd(fd)) == NULL) {
220 		log_warn("control_dispatch_imsg: fd %d: not found", fd);
221 		return;
222 	}
223 
224 	if (event & EV_READ) {
225 		if ((n = imsg_read(&c->iev.ibuf)) == -1 || n == 0) {
226 			control_close(fd);
227 			return;
228 		}
229 	}
230 	if (event & EV_WRITE) {
231 		if (msgbuf_write(&c->iev.ibuf.w) == -1) {
232 			control_close(fd);
233 			return;
234 		}
235 	}
236 
237 	for (;;) {
238 		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
239 			control_close(fd);
240 			return;
241 		}
242 
243 		if (n == 0)
244 			break;
245 
246 		switch (imsg.hdr.type) {
247 		case IMSG_CTL_FIB_COUPLE:
248 		case IMSG_CTL_FIB_DECOUPLE:
249 			ospfe_fib_update(imsg.hdr.type);
250 			/* FALLTHROUGH */
251 		case IMSG_CTL_RELOAD:
252 			c->iev.ibuf.pid = imsg.hdr.pid;
253 			ospfe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0);
254 			break;
255 		case IMSG_CTL_KROUTE:
256 		case IMSG_CTL_KROUTE_ADDR:
257 			c->iev.ibuf.pid = imsg.hdr.pid;
258 			ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid,
259 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
260 			break;
261 		case IMSG_CTL_SHOW_INTERFACE:
262 			if (imsg.hdr.len == IMSG_HEADER_SIZE +
263 			    sizeof(ifidx)) {
264 				memcpy(&ifidx, imsg.data, sizeof(ifidx));
265 				ospfe_iface_ctl(c, ifidx);
266 				imsg_compose(&c->iev.ibuf, IMSG_CTL_END, 0,
267 				    0, -1, NULL, 0);
268 			}
269 			break;
270 		case IMSG_CTL_SHOW_DATABASE:
271 		case IMSG_CTL_SHOW_DB_EXT:
272 		case IMSG_CTL_SHOW_DB_LINK:
273 		case IMSG_CTL_SHOW_DB_NET:
274 		case IMSG_CTL_SHOW_DB_RTR:
275 		case IMSG_CTL_SHOW_DB_INTRA:
276 		case IMSG_CTL_SHOW_DB_SELF:
277 		case IMSG_CTL_SHOW_DB_SUM:
278 		case IMSG_CTL_SHOW_DB_ASBR:
279 		case IMSG_CTL_SHOW_RIB:
280 		case IMSG_CTL_SHOW_SUM:
281 			c->iev.ibuf.pid = imsg.hdr.pid;
282 			ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
283 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
284 			break;
285 		case IMSG_CTL_SHOW_NBR:
286 			ospfe_nbr_ctl(c);
287 			break;
288 		case IMSG_CTL_LOG_VERBOSE:
289 			if (imsg.hdr.len != IMSG_HEADER_SIZE +
290 			    sizeof(verbose))
291 				break;
292 
293 			/* forward to other processes */
294 			ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid,
295 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
296 			ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
297 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
298 
299 			memcpy(&verbose, imsg.data, sizeof(verbose));
300 			log_verbose(verbose);
301 			break;
302 		default:
303 			log_debug("control_dispatch_imsg: "
304 			    "error handling imsg %d", imsg.hdr.type);
305 			break;
306 		}
307 		imsg_free(&imsg);
308 	}
309 
310 	imsg_event_add(&c->iev);
311 }
312 
313 int
314 control_imsg_relay(struct imsg *imsg)
315 {
316 	struct ctl_conn	*c;
317 
318 	if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
319 		return (0);
320 
321 	return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
322 	    -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
323 }
324 
325 void
326 session_socket_blockmode(int fd, enum blockmodes bm)
327 {
328 	int	flags;
329 
330 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
331 		fatal("fcntl F_GETFL");
332 
333 	if (bm == BM_NONBLOCK)
334 		flags |= O_NONBLOCK;
335 	else
336 		flags &= ~O_NONBLOCK;
337 
338 	if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
339 		fatal("fcntl F_SETFL");
340 }
341