From 91c0a62b5df3446be6e5289965d7c49fef5046e5 Mon Sep 17 00:00:00 2001 From: Claudio Jeker Date: Mon, 9 Feb 2015 11:37:32 +0000 Subject: Kill session_socket_blockmode() and replace it with SOCK_CLOEXEC or SOCK_NONBLOCK and accept4(). OK henning@ tested & OK benno@ --- usr.sbin/bgpd/bgpd.c | 22 +++++++++------------- usr.sbin/bgpd/config.c | 5 +++-- usr.sbin/bgpd/control.c | 14 ++++++-------- usr.sbin/bgpd/kroute.c | 5 +++-- usr.sbin/bgpd/pfkey.c | 4 ++-- usr.sbin/bgpd/session.c | 33 ++++++--------------------------- usr.sbin/bgpd/session.h | 8 +------- 7 files changed, 30 insertions(+), 61 deletions(-) (limited to 'usr.sbin/bgpd') diff --git a/usr.sbin/bgpd/bgpd.c b/usr.sbin/bgpd/bgpd.c index 3d628b6154d..1108b71b164 100644 --- a/usr.sbin/bgpd/bgpd.c +++ b/usr.sbin/bgpd/bgpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.c,v 1.174 2013/11/13 20:41:01 benno Exp $ */ +/* $OpenBSD: bgpd.c,v 1.175 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -192,22 +192,18 @@ main(int argc, char *argv[]) log_info("startup"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2s) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_m2s) == -1) fatal("socketpair"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_m2r) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_m2r) == -1) fatal("socketpair"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_s2r) == -1) fatal("socketpair"); - if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_s2r_c) == -1) + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + PF_UNSPEC, pipe_s2r_c) == -1) fatal("socketpair"); - session_socket_blockmode(pipe_m2s[0], BM_NONBLOCK); - session_socket_blockmode(pipe_m2s[1], BM_NONBLOCK); - session_socket_blockmode(pipe_m2r[0], BM_NONBLOCK); - session_socket_blockmode(pipe_m2r[1], BM_NONBLOCK); - session_socket_blockmode(pipe_s2r[0], BM_NONBLOCK); - session_socket_blockmode(pipe_s2r[1], BM_NONBLOCK); - session_socket_blockmode(pipe_s2r_c[0], BM_NONBLOCK); - session_socket_blockmode(pipe_s2r_c[1], BM_NONBLOCK); /* fork children */ rde_pid = rde_main(pipe_m2r, pipe_s2r, pipe_m2s, pipe_s2r_c, debug); diff --git a/usr.sbin/bgpd/config.c b/usr.sbin/bgpd/config.c index f4b96c5f385..9533e2f97d8 100644 --- a/usr.sbin/bgpd/config.c +++ b/usr.sbin/bgpd/config.c @@ -1,4 +1,4 @@ -/* $OpenBSD: config.c,v 1.57 2013/11/13 09:14:48 florian Exp $ */ +/* $OpenBSD: config.c,v 1.58 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer @@ -275,7 +275,8 @@ prepare_listeners(struct bgpd_config *conf) if (la->reconf != RECONF_REINIT) continue; - if ((la->fd = socket(la->sa.ss_family, SOCK_STREAM, + if ((la->fd = socket(la->sa.ss_family, + SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_TCP)) == -1) { if (la->flags & DEFAULT_LISTENER && (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT)) { diff --git a/usr.sbin/bgpd/control.c b/usr.sbin/bgpd/control.c index e5f0dd7236d..345b35c4685 100644 --- a/usr.sbin/bgpd/control.c +++ b/usr.sbin/bgpd/control.c @@ -1,4 +1,4 @@ -/* $OpenBSD: control.c,v 1.75 2013/11/13 20:41:01 benno Exp $ */ +/* $OpenBSD: control.c,v 1.76 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -42,7 +42,8 @@ control_init(int restricted, char *path) int fd; mode_t old_umask, mode; - 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); } @@ -82,8 +83,6 @@ control_init(int restricted, char *path) return (-1); } - session_socket_blockmode(fd, BM_NONBLOCK); - return (fd); } @@ -120,8 +119,9 @@ control_accept(int listenfd, int restricted) struct ctl_conn *ctl_conn; len = sizeof(sun); - if ((connfd = accept(listenfd, - (struct sockaddr *)&sun, &len)) == -1) { + if ((connfd = accept4(listenfd, + (struct sockaddr *)&sun, &len, + SOCK_NONBLOCK | SOCK_CLOEXEC)) == -1) { if (errno == ENFILE || errno == EMFILE) { pauseaccept = getmonotime(); return (0); @@ -131,8 +131,6 @@ control_accept(int listenfd, int restricted) return (0); } - session_socket_blockmode(connfd, BM_NONBLOCK); - if ((ctl_conn = calloc(1, sizeof(struct ctl_conn))) == NULL) { log_warn("control_accept"); close(connfd); diff --git a/usr.sbin/bgpd/kroute.c b/usr.sbin/bgpd/kroute.c index f225c3a565d..0c4f3720a8f 100644 --- a/usr.sbin/bgpd/kroute.c +++ b/usr.sbin/bgpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.199 2014/10/08 16:15:37 deraadt Exp $ */ +/* $OpenBSD: kroute.c,v 1.200 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -199,7 +199,8 @@ kr_init(void) unsigned int tid = RTABLE_ANY; socklen_t optlen; - if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) { + if ((kr_state.fd = socket(AF_ROUTE, SOCK_RAW | SOCK_CLOEXEC, + 0)) == -1) { log_warn("kr_init: socket"); return (-1); } diff --git a/usr.sbin/bgpd/pfkey.c b/usr.sbin/bgpd/pfkey.c index 461c5ebbd49..af97d4777f0 100644 --- a/usr.sbin/bgpd/pfkey.c +++ b/usr.sbin/bgpd/pfkey.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfkey.c,v 1.42 2014/10/08 16:15:37 deraadt Exp $ */ +/* $OpenBSD: pfkey.c,v 1.43 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -734,7 +734,7 @@ pfkey_remove(struct peer *p) int pfkey_init(struct bgpd_sysdep *sysdep) { - if ((fd = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) == -1) { + if ((fd = socket(PF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2)) == -1) { if (errno == EPROTONOSUPPORT) { log_warnx("PF_KEY not available, disabling ipsec"); sysdep->no_pfkey = 1; diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index f95a237c9f0..39a6760c44b 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.337 2014/12/04 19:55:49 sthen Exp $ */ +/* $OpenBSD: session.c,v 1.338 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004, 2005 Henning Brauer @@ -168,8 +168,6 @@ setup_listeners(u_int *la_cnt) continue; } - session_socket_blockmode(la->fd, BM_NONBLOCK); - if (listen(la->fd, MAX_BACKLOG)) { close(la->fd); fatal("listen"); @@ -1038,8 +1036,9 @@ session_accept(int listenfd) struct peer *p = NULL; len = sizeof(cliaddr); - if ((connfd = accept(listenfd, - (struct sockaddr *)&cliaddr, &len)) == -1) { + if ((connfd = accept4(listenfd, + (struct sockaddr *)&cliaddr, &len, + SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) { if (errno == ENFILE || errno == EMFILE) pauseaccept = getmonotime(); else if (errno != EWOULDBLOCK && errno != EINTR && @@ -1100,7 +1099,6 @@ open: close(connfd); return; } - session_socket_blockmode(connfd, BM_NONBLOCK); bgp_fsm(p, EVNT_CON_OPEN); return; } else if (p != NULL && p->state == STATE_ESTABLISHED && @@ -1129,8 +1127,8 @@ session_connect(struct peer *peer) if (peer->fd != -1) return (-1); - if ((peer->fd = socket(aid2af(peer->conf.remote_addr.aid), SOCK_STREAM, - IPPROTO_TCP)) == -1) { + if ((peer->fd = socket(aid2af(peer->conf.remote_addr.aid), + SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, IPPROTO_TCP)) == -1) { log_peer_warn(&peer->conf, "session_connect socket"); bgp_fsm(peer, EVNT_CON_OPENFAIL); return (-1); @@ -1173,8 +1171,6 @@ session_connect(struct peer *peer) return (-1); } - session_socket_blockmode(peer->fd, BM_NONBLOCK); - sa = addr2sa(&peer->conf.remote_addr, BGP_PORT); if (connect(peer->fd, sa, sa->sa_len) == -1) { if (errno != EINPROGRESS) { @@ -1280,23 +1276,6 @@ session_setup_socket(struct peer *p) return (0); } -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"); -} - void session_tcp_established(struct peer *peer) { diff --git a/usr.sbin/bgpd/session.h b/usr.sbin/bgpd/session.h index 375e41e42c3..01e6c4734c2 100644 --- a/usr.sbin/bgpd/session.h +++ b/usr.sbin/bgpd/session.h @@ -1,4 +1,4 @@ -/* $OpenBSD: session.h,v 1.116 2013/05/30 20:29:27 florian Exp $ */ +/* $OpenBSD: session.h,v 1.117 2015/02/09 11:37:31 claudio Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -65,11 +65,6 @@ enum session_events { EVNT_RCVD_NOTIFICATION }; -enum blockmodes { - BM_NORMAL, - BM_NONBLOCK -}; - enum msg_type { OPEN = 1, UPDATE, @@ -291,7 +286,6 @@ void print_config(struct bgpd_config *, struct rib_names *, pid_t rde_main(int[2], int[2], int[2], int[2], int); /* session.c */ -void session_socket_blockmode(int, enum blockmodes); pid_t session_main(int[2], int[2], int[2], int[2]); void bgp_fsm(struct peer *, enum session_events); int session_neighbor_rrefresh(struct peer *p); -- cgit v1.2.3