From owner-freebsd-bugs@freebsd.org Mon Aug 10 16:55:30 2020 Return-Path: Delivered-To: freebsd-bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E614B3A8782 for ; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mailman.nyi.freebsd.org (mailman.nyi.freebsd.org [IPv6:2610:1c1:1:606c::50:13]) by mx1.freebsd.org (Postfix) with ESMTP id 4BQMXf5v3Pz4WCh for ; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: by mailman.nyi.freebsd.org (Postfix) id C9F4B3A8781; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) Delivered-To: bugs@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C9BA93A851F for ; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) (envelope-from bugzilla-noreply@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BQMXf4wyRz4WZY for ; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (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 8D861D295 for ; Mon, 10 Aug 2020 16:55:30 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 07AGtUE8035404 for ; Mon, 10 Aug 2020 16:55:30 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 07AGtUN7035403 for bugs@FreeBSD.org; Mon, 10 Aug 2020 16:55:30 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 248579] PRoblem with accept(2) and dual IPv4/IPv6 TCP servers Date: Mon, 10 Aug 2020 16:55:30 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: 12.1-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Some People X-Bugzilla-Who: rec@rcousins.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 Aug 2020 16:55:31 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D248579 Bug ID: 248579 Summary: PRoblem with accept(2) and dual IPv4/IPv6 TCP servers Product: Base System Version: 12.1-RELEASE Hardware: Any OS: Any Status: New Severity: Affects Some People Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: rec@rcousins.com While writing a TCP server designed to accept both IPv4 and IPv6 connection= s, I've found that the recommended way fails, but the same code runs under Lin= ux perfectly. The failure is that IPv6 connections succeed and IPv4 connections always fail. Way to exercise bug: - Compile code below (cc -o foo foo.c will work) - run command ("./foo") in one window - In another window try to connect to port 5000 for example: telnet ::1 5000 <-- should work telnet 127.0.0.1 5000 <-- should work but fails 100% of the time. Repeating this test under modern Linux results in both telnet sessions connecting. Code to exercise bug: #include #include #include #include #include #include #include #include #include #include int make_listen(char *port) { struct addrinfo hints, *res, *res0; int error; int s; const char *cause =3D NULL; memset(&hints, 0, sizeof(hints)); // hints.ai_family =3D PF_UNSPEC; hints.ai_family =3D AF_INET6;=20=20 hints.ai_socktype =3D SOCK_STREAM; hints.ai_protocol =3D IPPROTO_TCP; hints.ai_flags =3D AI_PASSIVE; if ((error =3D getaddrinfo(NULL, port, &hints, &res0))) errx(1, "%s", gai_strerror(error)); for (res =3D res0; res; res =3D res->ai_next) { printf("Trying Family=3D%d, Socktype=3D%d, Protocol=3D%d\n", res->ai_family,=20 res->ai_socktype, res->ai_protocol); if ((s =3D socket(res->ai_family,=20 res->ai_socktype, res->ai_protocol)) < 0) { cause =3D "socket"; continue; } int yes =3D 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) errx(1,"Setsockopt(SO_REUSEADDR) failed."); if (bind(s, res->ai_addr, res->ai_addrlen) < 0) { cause =3D "bind"; close(s); continue; } listen(s, 5); freeaddrinfo(res0); return s; } err(1, "%s", cause); freeaddrinfo(res0); return -1; } int main(int argc, char *argv[]) { int listen_fd =3D make_listen("5000"); while (1) { char addr[80]; struct sockaddr_storage client_addr; socklen_t client_addr_len =3D sizeof(client_addr); int fc =3D accept(listen_fd, (struct sockaddr *)&client_addr, &client_addr_len); if (fc < 0) { warn("Accept()"); continue; } if (client_addr.ss_family =3D=3D AF_INET) { struct sockaddr_in *p =3D (struct sockaddr_in *)&client_addr; inet_ntop(p->sin_family, &p->sin_addr, addr,sizeof(addr)); } else { struct sockaddr_in6 *p =3D (struct sockaddr_in6 *)&client_addr; inet_ntop(p->sin6_family, &p->sin6_addr, addr,sizeof(addr)); } printf("Connection from (%d) %s\n", client_addr_len, addr); (void)write(fc,"Hello\n",6); close(fc); } } --=20 You are receiving this mail because: You are the assignee for the bug.=