1 /* $OpenBSD: control.c,v 1.38 2024/11/21 13:38:14 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/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 TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns); 33 34 #define CONTROL_BACKLOG 5 35 36 static void control_accept(int, short, void *); 37 static struct ctl_conn *control_connbyfd(int); 38 static struct ctl_conn *control_connbypid(pid_t); 39 static void control_close(int); 40 static void control_dispatch_imsg(int, short, void *); 41 42 static int control_fd; 43 44 int 45 control_init(char *path) 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, path, sizeof(sun.sun_path)); 60 61 if (unlink(path) == -1) 62 if (errno != ENOENT) { 63 log_warn("%s: unlink %s", __func__, path); 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__, path); 71 close(fd); 72 umask(old_umask); 73 return (-1); 74 } 75 umask(old_umask); 76 77 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) { 78 log_warn("%s: chmod", __func__); 79 close(fd); 80 (void)unlink(path); 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 } 106 107 static void 108 control_accept(int listenfd, short event, void *bula) 109 { 110 int connfd; 111 socklen_t len; 112 struct sockaddr_un sun; 113 struct ctl_conn *c; 114 115 len = sizeof(sun); 116 if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, 117 SOCK_NONBLOCK | SOCK_CLOEXEC)) == -1) { 118 /* 119 * Pause accept if we are out of file descriptors, or 120 * libevent will haunt us here too. 121 */ 122 if (errno == ENFILE || errno == EMFILE) 123 accept_pause(); 124 else if (errno != EWOULDBLOCK && errno != EINTR && 125 errno != ECONNABORTED) 126 log_warn("%s: accept4", __func__); 127 return; 128 } 129 130 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 131 log_warn(__func__); 132 close(connfd); 133 return; 134 } 135 136 if (imsgbuf_init(&c->iev.ibuf, connfd) == -1) { 137 log_warn(__func__); 138 close(connfd); 139 free(c); 140 return; 141 } 142 143 c->iev.handler = control_dispatch_imsg; 144 c->iev.events = EV_READ; 145 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 146 c->iev.handler, &c->iev); 147 event_add(&c->iev.ev, NULL); 148 149 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 150 } 151 152 static struct ctl_conn * 153 control_connbyfd(int fd) 154 { 155 struct ctl_conn *c; 156 157 TAILQ_FOREACH(c, &ctl_conns, entry) { 158 if (c->iev.ibuf.fd == fd) 159 break; 160 } 161 162 return (c); 163 } 164 165 static struct ctl_conn * 166 control_connbypid(pid_t pid) 167 { 168 struct ctl_conn *c; 169 170 TAILQ_FOREACH(c, &ctl_conns, entry) { 171 if (c->iev.ibuf.pid == pid) 172 break; 173 } 174 175 return (c); 176 } 177 178 static void 179 control_close(int fd) 180 { 181 struct ctl_conn *c; 182 183 if ((c = control_connbyfd(fd)) == NULL) { 184 log_warnx("%s: fd %d: not found", __func__, fd); 185 return; 186 } 187 188 imsgbuf_clear(&c->iev.ibuf); 189 TAILQ_REMOVE(&ctl_conns, c, entry); 190 191 event_del(&c->iev.ev); 192 close(c->iev.ibuf.fd); 193 accept_unpause(); 194 free(c); 195 } 196 197 static void 198 control_dispatch_imsg(int fd, short event, void *bula) 199 { 200 struct ctl_conn *c; 201 struct imsg imsg; 202 ssize_t n; 203 unsigned int ifidx; 204 int verbose; 205 206 if ((c = control_connbyfd(fd)) == NULL) { 207 log_warnx("%s: fd %d: not found", __func__, fd); 208 return; 209 } 210 211 if (event & EV_READ) { 212 if (imsgbuf_read(&c->iev.ibuf) != 1) { 213 control_close(fd); 214 return; 215 } 216 } 217 if (event & EV_WRITE) { 218 if (imsgbuf_write(&c->iev.ibuf) == -1) { 219 control_close(fd); 220 return; 221 } 222 } 223 224 for (;;) { 225 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 226 control_close(fd); 227 return; 228 } 229 230 if (n == 0) 231 break; 232 233 switch (imsg.hdr.type) { 234 case IMSG_CTL_FIB_COUPLE: 235 case IMSG_CTL_FIB_DECOUPLE: 236 case IMSG_CTL_RELOAD: 237 c->iev.ibuf.pid = imsg.hdr.pid; 238 ldpe_imsg_compose_parent(imsg.hdr.type, 0, NULL, 0); 239 break; 240 case IMSG_CTL_KROUTE: 241 case IMSG_CTL_KROUTE_ADDR: 242 case IMSG_CTL_IFINFO: 243 c->iev.ibuf.pid = imsg.hdr.pid; 244 ldpe_imsg_compose_parent(imsg.hdr.type, 245 imsg.hdr.pid, imsg.data, 246 imsg.hdr.len - IMSG_HEADER_SIZE); 247 break; 248 case IMSG_CTL_SHOW_INTERFACE: 249 if (imsg.hdr.len == IMSG_HEADER_SIZE + 250 sizeof(ifidx)) { 251 memcpy(&ifidx, imsg.data, sizeof(ifidx)); 252 ldpe_iface_ctl(c, ifidx); 253 imsg_compose_event(&c->iev, IMSG_CTL_END, 0, 254 0, -1, NULL, 0); 255 } 256 break; 257 case IMSG_CTL_SHOW_DISCOVERY: 258 ldpe_adj_ctl(c); 259 break; 260 case IMSG_CTL_SHOW_LIB: 261 case IMSG_CTL_SHOW_L2VPN_PW: 262 case IMSG_CTL_SHOW_L2VPN_BINDING: 263 c->iev.ibuf.pid = imsg.hdr.pid; 264 ldpe_imsg_compose_lde(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 ldpe_nbr_ctl(c); 269 break; 270 case IMSG_CTL_CLEAR_NBR: 271 if (imsg.hdr.len != IMSG_HEADER_SIZE + 272 sizeof(struct ctl_nbr)) 273 break; 274 275 nbr_clear_ctl(imsg.data); 276 break; 277 case IMSG_CTL_LOG_VERBOSE: 278 if (imsg.hdr.len != IMSG_HEADER_SIZE + 279 sizeof(verbose)) 280 break; 281 282 /* forward to other processes */ 283 ldpe_imsg_compose_parent(imsg.hdr.type, imsg.hdr.pid, 284 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 285 ldpe_imsg_compose_lde(imsg.hdr.type, 0, imsg.hdr.pid, 286 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 287 288 memcpy(&verbose, imsg.data, sizeof(verbose)); 289 log_verbose(verbose); 290 break; 291 default: 292 log_debug("%s: error handling imsg %d", __func__, 293 imsg.hdr.type); 294 break; 295 } 296 imsg_free(&imsg); 297 } 298 299 imsg_event_add(&c->iev); 300 } 301 302 int 303 control_imsg_relay(struct imsg *imsg) 304 { 305 struct ctl_conn *c; 306 307 if ((c = control_connbypid(imsg->hdr.pid)) == NULL) 308 return (0); 309 310 return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, 311 -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); 312 } 313