diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2015-02-10 05:24:49 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2015-02-10 05:24:49 +0000 |
commit | 95e40724d331550a7461fa4662213cba6ae0c535 (patch) | |
tree | 9362b1659d8bb70caf65ba4e263e391a78506111 | |
parent | b4218cc65b63357cc69f467f1f4fdf296f0fe118 (diff) |
Convert ospfd over to SOCK_CLOEXEC | SOCK_NONBLOCK and make the route
socket non-blocking. Introduce the same trigger for partial rt msgs.
-rw-r--r-- | usr.sbin/ospfd/control.c | 29 | ||||
-rw-r--r-- | usr.sbin/ospfd/control.h | 9 | ||||
-rw-r--r-- | usr.sbin/ospfd/kroute.c | 14 | ||||
-rw-r--r-- | usr.sbin/ospfd/ospfd.c | 18 | ||||
-rw-r--r-- | usr.sbin/ospfd/ospfe.c | 5 |
5 files changed, 25 insertions, 50 deletions
diff --git a/usr.sbin/ospfd/control.c b/usr.sbin/ospfd/control.c index 1cade9b7969..815a42a78bd 100644 --- a/usr.sbin/ospfd/control.c +++ b/usr.sbin/ospfd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.40 2014/07/11 16:43:33 krw Exp $ */ +/* $OpenBSD: control.c,v 1.41 2015/02/10 05:24:48 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -45,7 +45,8 @@ control_init(char *path) int fd; mode_t old_umask; - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + 0)) == -1) { log_warn("control_init: socket"); return (-1); } @@ -77,7 +78,6 @@ control_init(char *path) return (-1); } - session_socket_blockmode(fd, BM_NONBLOCK); control_state.fd = fd; return (0); @@ -124,8 +124,8 @@ control_accept(int listenfd, short event, void *bula) return; len = sizeof(sun); - if ((connfd = accept(listenfd, - (struct sockaddr *)&sun, &len)) == -1) { + if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len, + SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) { /* * Pause accept if we are out of file descriptors, or * libevent will haunt us here too. @@ -141,8 +141,6 @@ control_accept(int listenfd, short event, void *bula) return; } - session_socket_blockmode(connfd, BM_NONBLOCK); - if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { log_warn("control_accept"); close(connfd); @@ -324,20 +322,3 @@ control_imsg_relay(struct imsg *imsg) return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid, -1, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE)); } - -void -session_socket_blockmode(int fd, enum blockmodes bm) -{ - int flags; - - if ((flags = fcntl(fd, F_GETFL, 0)) == -1) - fatal("fcntl F_GETFL"); - - if (bm == BM_NONBLOCK) - flags |= O_NONBLOCK; - else - flags &= ~O_NONBLOCK; - - if ((flags = fcntl(fd, F_SETFL, flags)) == -1) - fatal("fcntl F_SETFL"); -} diff --git a/usr.sbin/ospfd/control.h b/usr.sbin/ospfd/control.h index 8f70ef63ebc..27abc2278b6 100644 --- a/usr.sbin/ospfd/control.h +++ b/usr.sbin/ospfd/control.h @@ -1,4 +1,4 @@ -/* $OpenBSD: control.h,v 1.5 2012/04/10 07:56:54 deraadt Exp $ */ +/* $OpenBSD: control.h,v 1.6 2015/02/10 05:24:48 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -29,11 +29,6 @@ struct { int fd; } control_state; -enum blockmodes { - BM_NORMAL, - BM_NONBLOCK -}; - struct ctl_conn { TAILQ_ENTRY(ctl_conn) entry; struct imsgev iev; @@ -46,6 +41,4 @@ void control_dispatch_imsg(int, short, void *); int control_imsg_relay(struct imsg *); void control_cleanup(char *); -void session_socket_blockmode(int, enum blockmodes); - #endif /* _CONTROL_H_ */ diff --git a/usr.sbin/ospfd/kroute.c b/usr.sbin/ospfd/kroute.c index 9f6841c7363..058ce700cb0 100644 --- a/usr.sbin/ospfd/kroute.c +++ b/usr.sbin/ospfd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.95 2015/01/16 06:40:19 deraadt Exp $ */ +/* $OpenBSD: kroute.c,v 1.96 2015/02/10 05:24:48 claudio Exp $ */ /* * Copyright (c) 2004 Esben Norby <norby@openbsd.org> @@ -106,7 +106,7 @@ int send_rtmsg(int, int, struct kroute *); int dispatch_rtmsg(void); int fetchtable(void); int fetchifs(u_short); -int rtmsg_process(char *, int); +int rtmsg_process(char *, size_t); void kr_fib_reload_timer(int, short, void *); void kr_fib_reload_arm_timer(int); @@ -141,7 +141,8 @@ kr_init(int fs, u_int rdomain) kr_state.fib_sync = fs; kr_state.rdomain = rdomain; - if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, AF_INET)) == -1) { + if ((kr_state.fd = socket(AF_ROUTE, + SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, AF_INET)) == -1) { log_warn("kr_init: socket"); return (-1); } @@ -1319,7 +1320,7 @@ dispatch_rtmsg(void) } int -rtmsg_process(char *buf, int len) +rtmsg_process(char *buf, size_t len) { struct rt_msghdr *rtm; struct if_msghdr ifm; @@ -1334,12 +1335,15 @@ rtmsg_process(char *buf, int len) u_short ifindex = 0; int rv, delay; - int offset; + size_t offset; char *next; for (offset = 0; offset < len; offset += rtm->rtm_msglen) { next = buf + offset; rtm = (struct rt_msghdr *)next; + if (len < offset + sizeof(*rtm) || + len < offset + rtm->rtm_msglen) + fatalx("rtmsg_process: partial rtm in buffer"); if (rtm->rtm_version != RTM_VERSION) continue; diff --git a/usr.sbin/ospfd/ospfd.c b/usr.sbin/ospfd/ospfd.c index edce6fd599c..39ee9d67db7 100644 --- a/usr.sbin/ospfd/ospfd.c +++ b/usr.sbin/ospfd/ospfd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ospfd.c,v 1.82 2015/01/16 06:40:19 deraadt Exp $ */ +/* $OpenBSD: ospfd.c,v 1.83 2015/02/10 05:24:48 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> @@ -228,19 +228,15 @@ main(int argc, char *argv[]) log_info("startup"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, - pipe_parent2ospfe) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_parent2ospfe) == -1) fatal("socketpair"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_parent2rde) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_parent2rde) == -1) fatal("socketpair"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_ospfe2rde) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_ospfe2rde) == -1) fatal("socketpair"); - session_socket_blockmode(pipe_parent2ospfe[0], BM_NONBLOCK); - session_socket_blockmode(pipe_parent2ospfe[1], BM_NONBLOCK); - session_socket_blockmode(pipe_parent2rde[0], BM_NONBLOCK); - session_socket_blockmode(pipe_parent2rde[1], BM_NONBLOCK); - session_socket_blockmode(pipe_ospfe2rde[0], BM_NONBLOCK); - session_socket_blockmode(pipe_ospfe2rde[1], BM_NONBLOCK); /* start children */ rde_pid = rde(ospfd_conf, pipe_parent2rde, pipe_ospfe2rde, diff --git a/usr.sbin/ospfd/ospfe.c b/usr.sbin/ospfd/ospfe.c index cb80c8ab249..d04d0975f83 100644 --- a/usr.sbin/ospfd/ospfe.c +++ b/usr.sbin/ospfd/ospfe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ospfe.c,v 1.89 2014/11/18 20:54:29 krw Exp $ */ +/* $OpenBSD: ospfe.c,v 1.90 2015/02/10 05:24:48 claudio Exp $ */ /* * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> @@ -92,7 +92,8 @@ ospfe(struct ospfd_conf *xconf, int pipe_parent2ospfe[2], int pipe_ospfe2rde[2], fatalx("control socket setup failed"); /* create the raw ip socket */ - if ((xconf->ospf_socket = socket(AF_INET, SOCK_RAW, + if ((xconf->ospf_socket = socket(AF_INET, + SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_OSPF)) == -1) fatal("error creating raw socket"); |