1 /* $OpenBSD: control.c,v 1.4 2008/01/31 12:17:35 henning 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 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(void) 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, OSPF6D_SOCKET, sizeof(sun.sun_path)); 58 59 if (unlink(OSPF6D_SOCKET) == -1) 60 if (errno != ENOENT) { 61 log_warn("control_init: unlink %s", OSPF6D_SOCKET); 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", OSPF6D_SOCKET); 69 close(fd); 70 umask(old_umask); 71 return (-1); 72 } 73 umask(old_umask); 74 75 if (chmod(OSPF6D_SOCKET, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) { 76 log_warn("control_init: chmod"); 77 close(fd); 78 (void)unlink(OSPF6D_SOCKET); 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 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) { 92 log_warn("control_listen: listen"); 93 return (-1); 94 } 95 96 event_set(&control_state.ev, control_state.fd, EV_READ | EV_PERSIST, 97 control_accept, NULL); 98 event_add(&control_state.ev, NULL); 99 100 return (0); 101 } 102 103 void 104 control_cleanup(void) 105 { 106 unlink(OSPF6D_SOCKET); 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"); 123 return; 124 } 125 126 session_socket_blockmode(connfd, BM_NONBLOCK); 127 128 if ((c = malloc(sizeof(struct ctl_conn))) == NULL) { 129 log_warn("control_accept"); 130 close(connfd); 131 return; 132 } 133 134 imsg_init(&c->ibuf, connfd, control_dispatch_imsg); 135 c->ibuf.events = EV_READ; 136 event_set(&c->ibuf.ev, c->ibuf.fd, c->ibuf.events, 137 c->ibuf.handler, &c->ibuf); 138 event_add(&c->ibuf.ev, NULL); 139 140 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 141 } 142 143 struct ctl_conn * 144 control_connbyfd(int fd) 145 { 146 struct ctl_conn *c; 147 148 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.fd != fd; 149 c = TAILQ_NEXT(c, entry)) 150 ; /* nothing */ 151 152 return (c); 153 } 154 155 struct ctl_conn * 156 control_connbypid(pid_t pid) 157 { 158 struct ctl_conn *c; 159 160 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->ibuf.pid != pid; 161 c = TAILQ_NEXT(c, entry)) 162 ; /* nothing */ 163 164 return (c); 165 } 166 167 void 168 control_close(int fd) 169 { 170 struct ctl_conn *c; 171 172 if ((c = control_connbyfd(fd)) == NULL) 173 log_warn("control_close: fd %d: not found", fd); 174 175 msgbuf_clear(&c->ibuf.w); 176 TAILQ_REMOVE(&ctl_conns, c, entry); 177 178 event_del(&c->ibuf.ev); 179 close(c->ibuf.fd); 180 free(c); 181 } 182 183 /* ARGSUSED */ 184 void 185 control_dispatch_imsg(int fd, short event, void *bula) 186 { 187 struct ctl_conn *c; 188 struct imsg imsg; 189 int n; 190 unsigned int ifidx; 191 192 if ((c = control_connbyfd(fd)) == NULL) { 193 log_warn("control_dispatch_imsg: fd %d: not found", fd); 194 return; 195 } 196 197 switch (event) { 198 case EV_READ: 199 if ((n = imsg_read(&c->ibuf)) == -1 || n == 0) { 200 control_close(fd); 201 return; 202 } 203 break; 204 case EV_WRITE: 205 if (msgbuf_write(&c->ibuf.w) < 0) { 206 control_close(fd); 207 return; 208 } 209 imsg_event_add(&c->ibuf); 210 return; 211 default: 212 fatalx("unknown event"); 213 } 214 215 for (;;) { 216 if ((n = imsg_get(&c->ibuf, &imsg)) == -1) { 217 control_close(fd); 218 return; 219 } 220 221 if (n == 0) 222 break; 223 224 switch (imsg.hdr.type) { 225 case IMSG_CTL_FIB_COUPLE: 226 case IMSG_CTL_FIB_DECOUPLE: 227 ospfe_fib_update(imsg.hdr.type); 228 /* FALLTHROUGH */ 229 case IMSG_CTL_RELOAD: 230 c->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 c->ibuf.pid = imsg.hdr.pid; 236 ospfe_imsg_compose_parent(imsg.hdr.type, 237 imsg.hdr.pid, imsg.data, 238 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(&c->ibuf, IMSG_CTL_END, 0, 246 0, 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_RIB: 257 case IMSG_CTL_SHOW_SUM: 258 c->ibuf.pid = imsg.hdr.pid; 259 ospfe_imsg_compose_rde(imsg.hdr.type, 0, imsg.hdr.pid, 260 imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE); 261 break; 262 case IMSG_CTL_SHOW_NBR: 263 ospfe_nbr_ctl(c); 264 break; 265 default: 266 log_debug("control_dispatch_imsg: " 267 "error handling imsg %d", imsg.hdr.type); 268 break; 269 } 270 imsg_free(&imsg); 271 } 272 273 imsg_event_add(&c->ibuf); 274 } 275 276 int 277 control_imsg_relay(struct imsg *imsg) 278 { 279 struct ctl_conn *c; 280 281 if ((c = control_connbypid(imsg->hdr.pid)) == NULL) 282 return (0); 283 284 return (imsg_compose(&c->ibuf, imsg->hdr.type, 0, imsg->hdr.pid, 285 imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); 286 } 287 288 void 289 session_socket_blockmode(int fd, enum blockmodes bm) 290 { 291 int flags; 292 293 if ((flags = fcntl(fd, F_GETFL, 0)) == -1) 294 fatal("fcntl F_GETFL"); 295 296 if (bm == BM_NONBLOCK) 297 flags |= O_NONBLOCK; 298 else 299 flags &= ~O_NONBLOCK; 300 301 if ((flags = fcntl(fd, F_SETFL, flags)) == -1) 302 fatal("fcntl F_SETFL"); 303 } 304