xref: /openbsd-src/usr.sbin/ospfd/control.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: control.c,v 1.23 2009/04/07 14:57:33 reyk 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 "ospfd.h"
30 #include "ospf.h"
31 #include "ospfe.h"
32 #include "log.h"
33 #include "control.h"
34 
35 #define	CONTROL_BACKLOG	5
36 
37 int control_imsg_relay(struct imsg *imsg);
38 
39 struct ctl_conn	*control_connbyfd(int);
40 struct ctl_conn	*control_connbypid(pid_t);
41 void		 control_close(int);
42 
43 int
44 control_init(char *path)
45 {
46 	struct sockaddr_un	 sun;
47 	int			 fd;
48 	mode_t			 old_umask;
49 
50 	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
51 		log_warn("control_init: socket");
52 		return (-1);
53 	}
54 
55 	bzero(&sun, sizeof(sun));
56 	sun.sun_family = AF_UNIX;
57 	strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
58 
59 	if (unlink(path) == -1)
60 		if (errno != ENOENT) {
61 			log_warn("control_init: unlink %s", path);
62 			close(fd);
63 			return (-1);
64 		}
65 
66 	old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
67 	if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
68 		log_warn("control_init: bind: %s", path);
69 		close(fd);
70 		umask(old_umask);
71 		return (-1);
72 	}
73 	umask(old_umask);
74 
75 	if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
76 		log_warn("control_init: chmod");
77 		close(fd);
78 		(void)unlink(path);
79 		return (-1);
80 	}
81 
82 	session_socket_blockmode(fd, BM_NONBLOCK);
83 	control_state.fd = fd;
84 
85 	return (0);
86 }
87 
88 int
89 control_listen(void)
90 {
91 
92 	if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
93 		log_warn("control_listen: listen");
94 		return (-1);
95 	}
96 
97 	event_set(&control_state.ev, control_state.fd, EV_READ | EV_PERSIST,
98 	    control_accept, NULL);
99 	event_add(&control_state.ev, NULL);
100 
101 	return (0);
102 }
103 
104 void
105 control_cleanup(char *path)
106 {
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 	len = sizeof(sun);
121 	if ((connfd = accept(listenfd,
122 	    (struct sockaddr *)&sun, &len)) == -1) {
123 		if (errno != EWOULDBLOCK && errno != EINTR)
124 			log_warn("control_accept");
125 		return;
126 	}
127 
128 	session_socket_blockmode(connfd, BM_NONBLOCK);
129 
130 	if ((c = malloc(sizeof(struct ctl_conn))) == NULL) {
131 		log_warn("control_accept");
132 		close(connfd);
133 		return;
134 	}
135 
136 	imsg_init(&c->ibuf, connfd, control_dispatch_imsg);
137 	c->ibuf.events = EV_READ;
138 	event_set(&c->ibuf.ev, c->ibuf.fd, c->ibuf.events,
139 	    c->ibuf.handler, &c->ibuf);
140 	event_add(&c->ibuf.ev, NULL);
141 
142 	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
143 }
144 
145 struct ctl_conn *
146 control_connbyfd(int fd)
147 {
148 	struct ctl_conn	*c;
149 
150 	for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.fd != fd;
151 	    c = TAILQ_NEXT(c, entry))
152 		;	/* nothing */
153 
154 	return (c);
155 }
156 
157 struct ctl_conn *
158 control_connbypid(pid_t pid)
159 {
160 	struct ctl_conn	*c;
161 
162 	for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.pid != pid;
163 	    c = TAILQ_NEXT(c, entry))
164 		;	/* nothing */
165 
166 	return (c);
167 }
168 
169 void
170 control_close(int fd)
171 {
172 	struct ctl_conn	*c;
173 
174 	if ((c = control_connbyfd(fd)) == NULL) {
175 		log_warn("control_close: fd %d: not found", fd);
176 		return;
177 	}
178 
179 	msgbuf_clear(&c->ibuf.w);
180 	TAILQ_REMOVE(&ctl_conns, c, entry);
181 
182 	event_del(&c->ibuf.ev);
183 	close(c->ibuf.fd);
184 	free(c);
185 }
186 
187 /* ARGSUSED */
188 void
189 control_dispatch_imsg(int fd, short event, void *bula)
190 {
191 	struct ctl_conn	*c;
192 	struct imsg	 imsg;
193 	ssize_t		 n;
194 	unsigned int	 ifidx;
195 
196 	if ((c = control_connbyfd(fd)) == NULL) {
197 		log_warn("control_dispatch_imsg: fd %d: not found", fd);
198 		return;
199 	}
200 
201 	switch (event) {
202 	case EV_READ:
203 		if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) {
204 			control_close(fd);
205 			return;
206 		}
207 		break;
208 	case EV_WRITE:
209 		if (msgbuf_write(&c->ibuf.w) < 0) {
210 			control_close(fd);
211 			return;
212 		}
213 		imsg_event_add(&c->ibuf);
214 		return;
215 	default:
216 		fatalx("unknown event");
217 	}
218 
219 	for (;;) {
220 		if ((n = imsg_get(&c->ibuf, &imsg)) == -1) {
221 			control_close(fd);
222 			return;
223 		}
224 
225 		if (n == 0)
226 			break;
227 
228 		switch (imsg.hdr.type) {
229 		case IMSG_CTL_FIB_COUPLE:
230 		case IMSG_CTL_FIB_DECOUPLE:
231 			ospfe_fib_update(imsg.hdr.type);
232 			/* FALLTHROUGH */
233 		case IMSG_CTL_RELOAD:
234 			c->ibuf.pid = imsg.hdr.pid;
235 			ospfe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0);
236 			break;
237 		case IMSG_CTL_KROUTE:
238 		case IMSG_CTL_KROUTE_ADDR:
239 		case IMSG_CTL_IFINFO:
240 			c->ibuf.pid = imsg.hdr.pid;
241 			ospfe_imsg_compose_parent(imsg.hdr.type,
242 			    imsg.hdr.pid, imsg.data,
243 			    imsg.hdr.len - IMSG_HEADER_SIZE);
244 			break;
245 		case IMSG_CTL_SHOW_INTERFACE:
246 			if (imsg.hdr.len == IMSG_HEADER_SIZE +
247 			    sizeof(ifidx)) {
248 				memcpy(&ifidx, imsg.data, sizeof(ifidx));
249 				ospfe_iface_ctl(c, ifidx);
250 				imsg_compose(&c->ibuf, IMSG_CTL_END, 0,
251 				    0, NULL, 0);
252 			}
253 			break;
254 		case IMSG_CTL_SHOW_DATABASE:
255 		case IMSG_CTL_SHOW_DB_EXT:
256 		case IMSG_CTL_SHOW_DB_NET:
257 		case IMSG_CTL_SHOW_DB_RTR:
258 		case IMSG_CTL_SHOW_DB_SELF:
259 		case IMSG_CTL_SHOW_DB_SUM:
260 		case IMSG_CTL_SHOW_DB_ASBR:
261 		case IMSG_CTL_SHOW_RIB:
262 		case IMSG_CTL_SHOW_SUM:
263 			c->ibuf.pid = imsg.hdr.pid;
264 			ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid,
265 			    imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
266 			break;
267 		case IMSG_CTL_SHOW_NBR:
268 			ospfe_nbr_ctl(c);
269 			break;
270 		default:
271 			log_debug("control_dispatch_imsg: "
272 			    "error handling imsg %d", imsg.hdr.type);
273 			break;
274 		}
275 		imsg_free(&imsg);
276 	}
277 
278 	imsg_event_add(&c->ibuf);
279 }
280 
281 int
282 control_imsg_relay(struct imsg *imsg)
283 {
284 	struct ctl_conn	*c;
285 
286 	if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
287 		return (0);
288 
289 	return (imsg_compose(&c->ibuf, imsg->hdr.type, 0, imsg->hdr.pid,
290 	    imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE));
291 }
292 
293 void
294 session_socket_blockmode(int fd, enum blockmodes bm)
295 {
296 	int	flags;
297 
298 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
299 		fatal("fcntl F_GETFL");
300 
301 	if (bm == BM_NONBLOCK)
302 		flags |= O_NONBLOCK;
303 	else
304 		flags &= ~O_NONBLOCK;
305 
306 	if ((flags = fcntl(fd, F_SETFL, flags)) == -1)
307 		fatal("fcntl F_SETFL");
308 }
309