From nobody Fri Jan 24 14:54:41 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Yfgnt1FmLz5lWSL; Fri, 24 Jan 2025 14:54:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Yfgns65yCz3dMf; Fri, 24 Jan 2025 14:54:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737730481; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/ggieH6/GrGHpuonSWOXQ3av5VFzI0TiV0bGXQdaILI=; b=UZ3XJCwOaswktYmUEvy0JkqioqTTyazDIRxNQ7w8LraRgkboQVQZFlQ+QlBszC+pHtpo6r 6Xhh4EsXCAdj7cSJeWyUE9WfFA9oIbiP9HOZxTqLFgA9Wi6i4Fk1LFDMmpRAF+6GWKeE/5 RjedJDAJcLwXlzq3XUuTPZ6zLxlM9+vMaZpYzR9tLCp3aliDFRN1KUzSHD5mTLpYPS+LW3 tslJHUlvgj74oDACjZi7brfTUDMbD9XX0IYCV//9UHS5T6L/IZ6jgcR2Huow5Vx3MukikJ qz9jHq/Wmq2foI/EKMB9zhd6Ukjx1qEoayCl8lX3RwDOC1thtuAOG6uBc4rGSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1737730481; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=/ggieH6/GrGHpuonSWOXQ3av5VFzI0TiV0bGXQdaILI=; b=wtyZ5ZzxTOKWhrueCu9csmqx0nxAVGABTu4JMAHToalU42c5YvaK2jLBAVOtGcSDI1O3kS gAOHWEDPX7zNewKElEl7ABBEXb0lnMXjdgKuE1eSkfM2WfBgqqjnCP+DxCFJYIDtBcbIWg OS6WyBQKygXxuFm4C3B0fTxp4vGPMUAt1NDV7Bv40kyh378Inq4ZMD6Fs/P3KFWdTkX3nV +bLAJfSDtG80IdSa0TAAF8Lm2saSfp5u5Ciip7eHqM/vLYIb7Hld1Q8R9s4mQwUJW2yvkC K2qxPj2tniK6vtk2dxzBqffZeJj1dy6FCLconZo2XhVTLdoRogayzIaWQhDQEw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1737730481; a=rsa-sha256; cv=none; b=cvBlvFViy3mi7mxwZ+zJDxIyxalh33q9Yb/ETy87+SdcieSg7D28nIg61Ck8ahRDDtgDjp BdQ+cQ2/vaProgRPBje+HKBWVbI3mjznvpz88dJWONV5uaQBgUrQXLnPhrj9AR+bP+zq2s eUGW6PdSnAGnkM9tIAhyJ41uO8SFPe/JmcF4mwCzXwehLpLSU08jlIU+Bey3sgClpKv6eo CM7w17ymYTQA0w+4onfvbwJgwuDXhtZkM+cVqorOWCrJ9dGNf5iJMrzkISqfEeYpJxEH9B pKVbbXdpwvADb40O7wfRlyjBHObJQvA2a5/AXllOMCCbUBLBPmfpxOVV9RakEQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Yfgns5KMBzDNM; Fri, 24 Jan 2025 14:54:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 50OEsfpO043785; Fri, 24 Jan 2025 14:54:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 50OEsfK1043782; Fri, 24 Jan 2025 14:54:41 GMT (envelope-from git) Date: Fri, 24 Jan 2025 14:54:41 GMT Message-Id: <202501241454.50OEsfK1043782@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: b4a5ab97c810 - main - ctld: Factor out code to setup a listening socket into a separate function List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: b4a5ab97c8104bd9e3f36b164b4648bf9dc6bc94 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=b4a5ab97c8104bd9e3f36b164b4648bf9dc6bc94 commit b4a5ab97c8104bd9e3f36b164b4648bf9dc6bc94 Author: John Baldwin AuthorDate: 2025-01-24 14:52:51 +0000 Commit: John Baldwin CommitDate: 2025-01-24 14:53:30 +0000 ctld: Factor out code to setup a listening socket into a separate function While here: - Reuse the address family from the addrinfo structure instead of calling getsockname(2) on the socket. - Remove a bogus comment about the PCP option. Reviewed by: mav, asomers Sponsored by: Chelsio Communications Differential Revision: https://reviews.freebsd.org/D48596 --- usr.sbin/ctld/ctld.c | 199 ++++++++++++++++++++++++--------------------------- 1 file changed, 94 insertions(+), 105 deletions(-) diff --git a/usr.sbin/ctld/ctld.c b/usr.sbin/ctld/ctld.c index 93a749b9109a..a67ff51a451d 100644 --- a/usr.sbin/ctld/ctld.c +++ b/usr.sbin/ctld/ctld.c @@ -1806,6 +1806,98 @@ conf_verify(struct conf *conf) return (0); } +static bool +portal_init_socket(struct portal *p) +{ + struct portal_group *pg = p->p_portal_group; + int error, sockbuf; + int one = 1; + + log_debugx("listening on %s, portal-group \"%s\"", + p->p_listen, pg->pg_name); + p->p_socket = socket(p->p_ai->ai_family, p->p_ai->ai_socktype, + p->p_ai->ai_protocol); + if (p->p_socket < 0) { + log_warn("socket(2) failed for %s", + p->p_listen); + return (false); + } + + sockbuf = SOCKBUF_SIZE; + if (setsockopt(p->p_socket, SOL_SOCKET, SO_RCVBUF, &sockbuf, + sizeof(sockbuf)) == -1) + log_warn("setsockopt(SO_RCVBUF) failed for %s", + p->p_listen); + sockbuf = SOCKBUF_SIZE; + if (setsockopt(p->p_socket, SOL_SOCKET, SO_SNDBUF, &sockbuf, + sizeof(sockbuf)) == -1) + log_warn("setsockopt(SO_SNDBUF) failed for %s", p->p_listen); + if (setsockopt(p->p_socket, SOL_SOCKET, SO_NO_DDP, &one, + sizeof(one)) == -1) + log_warn("setsockopt(SO_NO_DDP) failed for %s", p->p_listen); + error = setsockopt(p->p_socket, SOL_SOCKET, SO_REUSEADDR, &one, + sizeof(one)); + if (error != 0) { + log_warn("setsockopt(SO_REUSEADDR) failed for %s", p->p_listen); + close(p->p_socket); + p->p_socket = 0; + return (false); + } + + if (pg->pg_dscp != -1) { + /* Only allow the 6-bit DSCP field to be modified */ + int tos = pg->pg_dscp << 2; + switch (p->p_ai->ai_family) { + case AF_INET: + if (setsockopt(p->p_socket, IPPROTO_IP, IP_TOS, + &tos, sizeof(tos)) == -1) + log_warn("setsockopt(IP_TOS) failed for %s", + p->p_listen); + break; + case AF_INET6: + if (setsockopt(p->p_socket, IPPROTO_IPV6, IPV6_TCLASS, + &tos, sizeof(tos)) == -1) + log_warn("setsockopt(IPV6_TCLASS) failed for %s", + p->p_listen); + break; + } + } + if (pg->pg_pcp != -1) { + int pcp = pg->pg_pcp; + switch (p->p_ai->ai_family) { + case AF_INET: + if (setsockopt(p->p_socket, IPPROTO_IP, IP_VLAN_PCP, + &pcp, sizeof(pcp)) == -1) + log_warn("setsockopt(IP_VLAN_PCP) failed for %s", + p->p_listen); + break; + case AF_INET6: + if (setsockopt(p->p_socket, IPPROTO_IPV6, IPV6_VLAN_PCP, + &pcp, sizeof(pcp)) == -1) + log_warn("setsockopt(IPV6_VLAN_PCP) failed for %s", + p->p_listen); + break; + } + } + + error = bind(p->p_socket, p->p_ai->ai_addr, + p->p_ai->ai_addrlen); + if (error != 0) { + log_warn("bind(2) failed for %s", p->p_listen); + close(p->p_socket); + p->p_socket = 0; + return (false); + } + error = listen(p->p_socket, -1); + if (error != 0) { + log_warn("listen(2) failed for %s", p->p_listen); + close(p->p_socket); + p->p_socket = 0; + return (false); + } + return (true); +} + static int conf_apply(struct conf *oldconf, struct conf *newconf) { @@ -1814,8 +1906,7 @@ conf_apply(struct conf *oldconf, struct conf *newconf) struct portal *oldp, *newp; struct port *oldport, *newport, *tmpport; struct isns *oldns, *newns; - int changed, cumulated_error = 0, error, sockbuf; - int one = 1; + int changed, cumulated_error = 0, error; if (oldconf->conf_debug != newconf->conf_debug) { log_debugx("changing debug level to %d", newconf->conf_debug); @@ -2082,109 +2173,7 @@ conf_apply(struct conf *oldconf, struct conf *newconf) assert(proxy_mode == false); assert(newp->p_iser == false); - log_debugx("listening on %s, portal-group \"%s\"", - newp->p_listen, newpg->pg_name); - newp->p_socket = socket(newp->p_ai->ai_family, - newp->p_ai->ai_socktype, - newp->p_ai->ai_protocol); - if (newp->p_socket < 0) { - log_warn("socket(2) failed for %s", - newp->p_listen); - cumulated_error++; - continue; - } - sockbuf = SOCKBUF_SIZE; - if (setsockopt(newp->p_socket, SOL_SOCKET, SO_RCVBUF, - &sockbuf, sizeof(sockbuf)) == -1) - log_warn("setsockopt(SO_RCVBUF) failed " - "for %s", newp->p_listen); - sockbuf = SOCKBUF_SIZE; - if (setsockopt(newp->p_socket, SOL_SOCKET, SO_SNDBUF, - &sockbuf, sizeof(sockbuf)) == -1) - log_warn("setsockopt(SO_SNDBUF) failed " - "for %s", newp->p_listen); - if (setsockopt(newp->p_socket, SOL_SOCKET, SO_NO_DDP, - &one, sizeof(one)) == -1) - log_warn("setsockopt(SO_NO_DDP) failed " - "for %s", newp->p_listen); - error = setsockopt(newp->p_socket, SOL_SOCKET, - SO_REUSEADDR, &one, sizeof(one)); - if (error != 0) { - log_warn("setsockopt(SO_REUSEADDR) failed " - "for %s", newp->p_listen); - close(newp->p_socket); - newp->p_socket = 0; - cumulated_error++; - continue; - } - if (newpg->pg_dscp != -1) { - struct sockaddr sa; - int len = sizeof(sa); - getsockname(newp->p_socket, &sa, &len); - /* - * Only allow the 6-bit DSCP - * field to be modified - */ - int tos = newpg->pg_dscp << 2; - if (sa.sa_family == AF_INET) { - if (setsockopt(newp->p_socket, - IPPROTO_IP, IP_TOS, - &tos, sizeof(tos)) == -1) - log_warn("setsockopt(IP_TOS) " - "failed for %s", - newp->p_listen); - } else - if (sa.sa_family == AF_INET6) { - if (setsockopt(newp->p_socket, - IPPROTO_IPV6, IPV6_TCLASS, - &tos, sizeof(tos)) == -1) - log_warn("setsockopt(IPV6_TCLASS) " - "failed for %s", - newp->p_listen); - } - } - if (newpg->pg_pcp != -1) { - struct sockaddr sa; - int len = sizeof(sa); - getsockname(newp->p_socket, &sa, &len); - /* - * Only allow the 6-bit DSCP - * field to be modified - */ - int pcp = newpg->pg_pcp; - if (sa.sa_family == AF_INET) { - if (setsockopt(newp->p_socket, - IPPROTO_IP, IP_VLAN_PCP, - &pcp, sizeof(pcp)) == -1) - log_warn("setsockopt(IP_VLAN_PCP) " - "failed for %s", - newp->p_listen); - } else - if (sa.sa_family == AF_INET6) { - if (setsockopt(newp->p_socket, - IPPROTO_IPV6, IPV6_VLAN_PCP, - &pcp, sizeof(pcp)) == -1) - log_warn("setsockopt(IPV6_VLAN_PCP) " - "failed for %s", - newp->p_listen); - } - } - error = bind(newp->p_socket, newp->p_ai->ai_addr, - newp->p_ai->ai_addrlen); - if (error != 0) { - log_warn("bind(2) failed for %s", - newp->p_listen); - close(newp->p_socket); - newp->p_socket = 0; - cumulated_error++; - continue; - } - error = listen(newp->p_socket, -1); - if (error != 0) { - log_warn("listen(2) failed for %s", - newp->p_listen); - close(newp->p_socket); - newp->p_socket = 0; + if (!portal_init_socket(newp)) { cumulated_error++; continue; }