1 /* $OpenBSD: control.c,v 1.27 2016/05/23 19:20:55 renato 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/un.h> 22 #include <errno.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include "ldpd.h" 28 #include "ldpe.h" 29 #include "log.h" 30 #include "control.h" 31 32 #define CONTROL_BACKLOG 5 33 34 static void control_accept(int, short, void *); 35 static struct ctl_conn *control_connbyfd(int); 36 static struct ctl_conn *control_connbypid(pid_t); 37 static void control_close(int); 38 static void control_dispatch_imsg(int, short, void *); 39 40 struct ctl_conns ctl_conns; 41 42 static int control_fd; 43 44 int 45 control_init(void) 46 { 47 struct sockaddr_un sun; 48 int fd; 49 mode_t old_umask; 50 51 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 52 0)) == -1) { 53 log_warn("%s: socket", __func__); 54 return (-1); 55 } 56 57 memset(&sun, 0, sizeof(sun)); 58 sun.sun_family = AF_UNIX; 59 strlcpy(sun.sun_path, LDPD_SOCKET, sizeof(sun.sun_path)); 60 61 if (unlink(LDPD_SOCKET) == -1) 62 if (errno != ENOENT) { 63 log_warn("%s: unlink %s", __func__, LDPD_SOCKET); 64 close(fd); 65 return (-1); 66 } 67 68 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); 69 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { 70 log_warn("%s: bind: %s", __func__, LDPD_SOCKET); 71 close(fd); 72 umask(old_umask); 73 return (-1); 74 } 75 umask(old_umask); 76 77 if (chmod(LDPD_SOCKET, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) { 78 log_warn("%s: chmod", __func__); 79 close(fd); 80 (void)unlink(LDPD_SOCKET); 81 return (-1); 82 } 83 84 control_fd = fd; 85 86 return (0); 87 } 88 89 int 90 control_listen(void) 91 { 92 if (listen(control_fd, CONTROL_BACKLOG) == -1) { 93 log_warn("%s: listen", __func__); 94 return (-1); 95 } 96 97 return (accept_add(control_fd, control_accept, NULL)); 98 } 99 100 void 101 control_cleanup(void) 102 { 103 accept_del(control_fd); 104 close(control_fd); 105 unlink(LDPD_SOCKET); 106 } 107 108 /* ARGSUSED */ 109 static void 110 control_accept(int listenfd, short event, void *bula) 111 { 112 int connfd; 113 socklen_t len; 114 struct sockaddr_un sun; 115 struct ctl_conn *c; 116 117 len = sizeof(sun); 118 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, 119 SOCK_NONBLOCK | SOCK_CLOEXEC)) == -1) { 120 /* 121 * Pause accept if we are out of file descriptors, or 122 * libevent will haunt us here too. 123 */ 124 if (errno == ENFILE || errno == EMFILE) 125 accept_pause(); 126 else if (errno != EWOULDBLOCK && errno != EINTR && 127 errno != ECONNABORTED) 128 log_warn("%s: accept4", __func__); 129 return; 130 } 131 132 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 133 log_warn(__func__); 134 close(connfd); 135 return; 136 } 137 138 imsg_init(&c->iev.ibuf, connfd); 139 c->iev.handler = control_dispatch_imsg; 140 c->iev.events = EV_READ; 141 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 142 c->iev.handler, &c->iev); 143 event_add(&c->iev.ev, NULL); 144 145 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 146 } 147 148 static struct ctl_conn * 149 control_connbyfd(int fd) 150 { 151 struct ctl_conn *c; 152 153 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.fd != fd; 154 c = TAILQ_NEXT(c, entry)) 155 ; /* nothing */ 156 157 return (c); 158 } 159 160 static struct ctl_conn * 161 control_connbypid(pid_t pid) 162 { 163 struct ctl_conn *c; 164 165 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.pid != pid; 166 c = TAILQ_NEXT(c, entry)) 167 ; /* nothing */ 168 169 return (c); 170 } 171 172 static void 173 control_close(int fd) 174 { 175 struct ctl_conn *c; 176 177 if ((c = control_connbyfd(fd)) == NULL) { 178 log_warnx("%s: fd %d: not found", __func__, fd); 179 return; 180 } 181 182 msgbuf_clear(&c->iev.ibuf.w); 183 TAILQ_REMOVE(&ctl_conns, c, entry); 184 185 event_del(&c->iev.ev); 186 close(c->iev.ibuf.fd); 187 accept_unpause(); 188 free(c); 189 } 190 191 /* ARGSUSED */ 192 static void 193 control_dispatch_imsg(int fd, short event, void *bula) 194 { 195 struct ctl_conn *c; 196 struct imsg imsg; 197 ssize_t n; 198 unsigned int ifidx; 199 int verbose; 200 201 if ((c = control_connbyfd(fd)) == NULL) { 202 log_warnx("%s: fd %d: not found", __func__, fd); 203 return; 204 } 205 206 if (event & EV_READ) { 207 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || 208 n == 0) { 209 control_close(fd); 210 return; 211 } 212 } 213 if (event & EV_WRITE) { 214 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { 215 control_close(fd); 216 return; 217 } 218 } 219 220 for (;;) { 221 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 222 control_close(fd); 223 return; 224 } 225 226 if (n == 0) 227 break; 228 229 switch (imsg.hdr.type) { 230 case IMSG_CTL_FIB_COUPLE: 231 case IMSG_CTL_FIB_DECOUPLE: 232 case IMSG_CTL_RELOAD: 233 c->iev.ibuf.pid = imsg.hdr.pid; 234 ldpe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0); 235 break; 236 case IMSG_CTL_KROUTE: 237 case IMSG_CTL_KROUTE_ADDR: 238 case IMSG_CTL_IFINFO: 239 c->iev.ibuf.pid = imsg.hdr.pid; 240 ldpe_imsg_compose_parent(imsg.hdr.type, 241 imsg.hdr.pid, imsg.data, 242 imsg.hdr.len - IMSG_HEADER_SIZE); 243 break; 244 case IMSG_CTL_SHOW_INTERFACE: 245 if (imsg.hdr.len == IMSG_HEADER_SIZE + 246 sizeof(ifidx)) { 247 memcpy(&ifidx, imsg.data, sizeof(ifidx)); 248 ldpe_iface_ctl(c, ifidx); 249 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 250 0, -1, NULL, 0); 251 } 252 break; 253 case IMSG_CTL_SHOW_DISCOVERY: 254 ldpe_adj_ctl(c); 255 break; 256 case IMSG_CTL_SHOW_LIB: 257 case IMSG_CTL_SHOW_L2VPN_PW: 258 case IMSG_CTL_SHOW_L2VPN_BINDING: 259 c->iev.ibuf.pid = imsg.hdr.pid; 260 ldpe_imsg_compose_lde(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 ldpe_nbr_ctl(c); 265 break; 266 case IMSG_CTL_CLEAR_NBR: 267 if (imsg.hdr.len != IMSG_HEADER_SIZE + 268 sizeof(struct ctl_nbr)) 269 break; 270 271 nbr_clear_ctl(imsg.data); 272 break; 273 case IMSG_CTL_LOG_VERBOSE: 274 if (imsg.hdr.len != IMSG_HEADER_SIZE + 275 sizeof(verbose)) 276 break; 277 278 /* forward to other processes */ 279 ldpe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, 280 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 281 ldpe_imsg_compose_lde(imsg.hdr.type, 0, imsg.hdr.pid, 282 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 283 284 memcpy(&verbose, imsg.data, sizeof(verbose)); 285 log_verbose(verbose); 286 break; 287 default: 288 log_debug("%s: error handling imsg %d", __func__, 289 imsg.hdr.type); 290 break; 291 } 292 imsg_free(&imsg); 293 } 294 295 imsg_event_add(&c->iev); 296 } 297 298 int 299 control_imsg_relay(struct imsg *imsg) 300 { 301 struct ctl_conn *c; 302 303 if ((c = control_connbypid(imsg->hdr.pid)) == NULL) 304 return (0); 305 306 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, 307 -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); 308 } 309