1 /* $OpenBSD: control.c,v 1.35 2011/05/09 12:24:41 claudio 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 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 90 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) { 91 log_warn("control_listen: listen"); 92 return (-1); 93 } 94 95 event_set(&control_state.ev, control_state.fd, EV_READ | EV_PERSIST, 96 control_accept, NULL); 97 event_add(&control_state.ev, NULL); 98 99 return (0); 100 } 101 102 void 103 control_cleanup(char *path) 104 { 105 if (path) 106 unlink(path); 107 } 108 109 /* ARGSUSED */ 110 void 111 control_accept(int listenfd, short event, void *bula) 112 { 113 int connfd; 114 socklen_t len; 115 struct sockaddr_un sun; 116 struct ctl_conn *c; 117 118 len = sizeof(sun); 119 if ((connfd = accept(listenfd, 120 (struct sockaddr *)&sun, &len)) == -1) { 121 if (errno != EWOULDBLOCK && errno != EINTR) 122 log_warn("control_accept: accept"); 123 return; 124 } 125 126 session_socket_blockmode(connfd, BM_NONBLOCK); 127 128 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 129 log_warn("control_accept"); 130 close(connfd); 131 return; 132 } 133 134 imsg_init(&c->iev.ibuf, connfd); 135 c->iev.handler = control_dispatch_imsg; 136 c->iev.events = EV_READ; 137 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 138 c->iev.handler, &c->iev); 139 event_add(&c->iev.ev, NULL); 140 141 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 142 } 143 144 struct ctl_conn * 145 control_connbyfd(int fd) 146 { 147 struct ctl_conn *c; 148 149 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.fd != fd; 150 c = TAILQ_NEXT(c, entry)) 151 ; /* nothing */ 152 153 return (c); 154 } 155 156 struct ctl_conn * 157 control_connbypid(pid_t pid) 158 { 159 struct ctl_conn *c; 160 161 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.pid != pid; 162 c = TAILQ_NEXT(c, entry)) 163 ; /* nothing */ 164 165 return (c); 166 } 167 168 void 169 control_close(int fd) 170 { 171 struct ctl_conn *c; 172 173 if ((c = control_connbyfd(fd)) == NULL) { 174 log_warn("control_close: fd %d: not found", fd); 175 return; 176 } 177 178 msgbuf_clear(&c->iev.ibuf.w); 179 TAILQ_REMOVE(&ctl_conns, c, entry); 180 181 event_del(&c->iev.ev); 182 close(c->iev.ibuf.fd); 183 free(c); 184 } 185 186 /* ARGSUSED */ 187 void 188 control_dispatch_imsg(int fd, short event, void *bula) 189 { 190 struct ctl_conn *c; 191 struct imsg imsg; 192 ssize_t n; 193 unsigned int ifidx; 194 int verbose; 195 196 if ((c = control_connbyfd(fd)) == NULL) { 197 log_warn("control_dispatch_imsg: fd %d: not found", fd); 198 return; 199 } 200 201 if (event & EV_READ) { 202 if ((n = imsg_read(&c->iev.ibuf)) == -1 || n == 0) { 203 control_close(fd); 204 return; 205 } 206 } 207 if (event & EV_WRITE) { 208 if (msgbuf_write(&c->iev.ibuf.w) == -1) { 209 control_close(fd); 210 return; 211 } 212 } 213 214 for (;;) { 215 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 216 control_close(fd); 217 return; 218 } 219 220 if (n == 0) 221 break; 222 223 switch (imsg.hdr.type) { 224 case IMSG_CTL_FIB_COUPLE: 225 case IMSG_CTL_FIB_DECOUPLE: 226 ospfe_fib_update(imsg.hdr.type); 227 /* FALLTHROUGH */ 228 case IMSG_CTL_FIB_RELOAD: 229 case IMSG_CTL_RELOAD: 230 c->iev.ibuf.pid = imsg.hdr.pid; 231 ospfe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0); 232 break; 233 case IMSG_CTL_KROUTE: 234 case IMSG_CTL_KROUTE_ADDR: 235 case IMSG_CTL_IFINFO: 236 c->iev.ibuf.pid = imsg.hdr.pid; 237 ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, 238 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 239 break; 240 case IMSG_CTL_SHOW_INTERFACE: 241 if (imsg.hdr.len == IMSG_HEADER_SIZE + 242 sizeof(ifidx)) { 243 memcpy(&ifidx, imsg.data, sizeof(ifidx)); 244 ospfe_iface_ctl(c, ifidx); 245 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 246 0, -1, NULL, 0); 247 } 248 break; 249 case IMSG_CTL_SHOW_DATABASE: 250 case IMSG_CTL_SHOW_DB_EXT: 251 case IMSG_CTL_SHOW_DB_NET: 252 case IMSG_CTL_SHOW_DB_RTR: 253 case IMSG_CTL_SHOW_DB_SELF: 254 case IMSG_CTL_SHOW_DB_SUM: 255 case IMSG_CTL_SHOW_DB_ASBR: 256 case IMSG_CTL_SHOW_DB_OPAQ: 257 case IMSG_CTL_SHOW_RIB: 258 case IMSG_CTL_SHOW_SUM: 259 c->iev.ibuf.pid = imsg.hdr.pid; 260 ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, 261 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 262 break; 263 case IMSG_CTL_SHOW_NBR: 264 ospfe_nbr_ctl(c); 265 break; 266 case IMSG_CTL_LOG_VERBOSE: 267 if (imsg.hdr.len != IMSG_HEADER_SIZE + 268 sizeof(verbose)) 269 break; 270 271 /* forward to other processes */ 272 ospfe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, 273 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 274 ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, 275 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 276 277 memcpy(&verbose, imsg.data, sizeof(verbose)); 278 log_verbose(verbose); 279 break; 280 default: 281 log_debug("control_dispatch_imsg: " 282 "error handling imsg %d", imsg.hdr.type); 283 break; 284 } 285 imsg_free(&imsg); 286 } 287 288 imsg_event_add(&c->iev); 289 } 290 291 int 292 control_imsg_relay(struct imsg *imsg) 293 { 294 struct ctl_conn *c; 295 296 if ((c = control_connbypid(imsg->hdr.pid)) == NULL) 297 return (0); 298 299 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, 300 -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); 301 } 302 303 void 304 session_socket_blockmode(int fd, enum blockmodes bm) 305 { 306 int flags; 307 308 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) 309 fatal("fcntl F_GETFL"); 310 311 if (bm == BM_NONBLOCK) 312 flags |= O_NONBLOCK; 313 else 314 flags &= ~O_NONBLOCK; 315 316 if ((flags = fcntl(fd, F_SETFL, flags)) == -1) 317 fatal("fcntl F_SETFL"); 318 } 319