From patchwork Fri Oct 26 00:03:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: max7255 X-Patchwork-Id: 10656875 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8DC0D109C for ; Fri, 26 Oct 2018 01:26:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 828D92C571 for ; Fri, 26 Oct 2018 01:26:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 76B162C577; Fri, 26 Oct 2018 01:26:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 05DF52C572 for ; Fri, 26 Oct 2018 01:26:05 +0000 (UTC) Received: from localhost ([::1]:57577 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFqt2-0004uo-RF for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 21:26:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37625) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFpbn-0002vH-P8 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFpbk-0007eF-Hz for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:11 -0400 Received: from forwardcorp1g.cmail.yandex.net ([87.250.241.190]:60700) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFpbk-0007dE-0B for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:08 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1g.cmail.yandex.net (Yandex) with ESMTP id B6B2D21800 for ; Fri, 26 Oct 2018 03:04:03 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [2a02:6b8:0:1a2d::30]) by mxbackcorp1g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id ZnAP0tGMLE-43l4mm8S; Fri, 26 Oct 2018 03:04:03 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512243; bh=ej+TccYuoLLMbDn/2FkgmYKkd50lcfKi2xuCTPsvRrg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=tyzuKFbeZZZH23fxuQHlJvwFEF+AcIsvfepDGpKnm715CRv4ZQYJbeJzP8PnoM8/O Hdw5clQ7R/it+b7X97HrIN5li5TWOC8xJD0XRTGpMbsXxnV8k/Xti1W21FBHKSvu5c TCGBwRaPj46qiHl/dYFqPUl7FjU+OSLfHsA7RnkA= Received: from dynamic-iva.dhcp.yndx.net (dynamic-iva.dhcp.yndx.net [2a02:6b8:b010:d002::1:1]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id bOfpZ9lZrb-43FqJEJh; Fri, 26 Oct 2018 03:04:03 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512243; bh=ej+TccYuoLLMbDn/2FkgmYKkd50lcfKi2xuCTPsvRrg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=tyzuKFbeZZZH23fxuQHlJvwFEF+AcIsvfepDGpKnm715CRv4ZQYJbeJzP8PnoM8/O Hdw5clQ7R/it+b7X97HrIN5li5TWOC8xJD0XRTGpMbsXxnV8k/Xti1W21FBHKSvu5c TCGBwRaPj46qiHl/dYFqPUl7FjU+OSLfHsA7RnkA= Authentication-Results: smtpcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Maxim Samoylov To: qemu-devel@nongnu.org Date: Fri, 26 Oct 2018 03:03:40 +0300 Message-Id: <1540512223-21199-2-git-send-email-max7255@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> References: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 87.250.241.190 X-Mailman-Approved-At: Thu, 25 Oct 2018 21:24:29 -0400 Subject: [Qemu-devel] [PATCH RFC 1/4] slirp: add helper for tcp6 socket creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: max7255@yandex-team.ru Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Maxim Samoylov --- slirp/socket.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ slirp/socket.h | 2 ++ 2 files changed, 75 insertions(+) diff --git a/slirp/socket.c b/slirp/socket.c index 322383a..e16e6c1 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -776,6 +776,79 @@ tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr, return so; } +struct socket * +tcp6_listen(Slirp *slirp, struct in6_addr haddr, u_int hport, + struct in6_addr laddr, u_int lport, int flags) +{ + struct sockaddr_in6 addr; + struct socket *so; + int s, opt = 1; + socklen_t addrlen = sizeof(addr); + memset(&addr, 0, addrlen); + + /* The same flow as in tcp_listen above */ + + so = socreate(slirp); + if (!so) { + return NULL; + } + + so->so_tcpcb = tcp_newtcpcb(so); + if (so->so_tcpcb == NULL) { + free(so); + return NULL; + } + + insque(so, &slirp->tcb); + + if (flags & SS_FACCEPTONCE) { + so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT * 2; + } + + so->so_state &= SS_PERSISTENT_MASK; + so->so_state |= (SS_FACCEPTCONN | flags); + so->so_lfamily = AF_INET6; + so->so_lport6 = lport; /* Kept in network format */ + so->so_laddr6 = laddr; + + addr.sin6_family = AF_INET6; + addr.sin6_addr = haddr; + addr.sin6_port = hport; + + s = qemu_socket(AF_INET6, SOCK_STREAM, 0); + if ((s < 0) || + (socket_set_fast_reuse(s) < 0) || + (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) || + (listen(s, 1) < 0)) { + int tmperrno = errno; /* Don't clobber the real reason we failed */ + if (s >= 0) { + closesocket(s); + } + sofree(so); + /* Restore the real errno */ +#ifdef _WIN32 + WSASetLastError(tmperrno); +#else + errno = tmperrno; +#endif + return NULL; + } + qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int)); + + getsockname(s, (struct sockaddr *)&addr, &addrlen); + so->fhost.sin6 = addr; + + if (!memcmp(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any)) || + !memcmp(&addr.sin6_addr, &in6addr_loopback, + sizeof(in6addr_loopback))) { + + memcpy(&so->so_faddr6, &slirp->vhost_addr6, sizeof(slirp->vhost_addr6)); + } + + so->s = s; + return so; +} + /* * Various session state calls * XXX Should be #define's diff --git a/slirp/socket.h b/slirp/socket.h index 2f224bc..b200bb6 100644 --- a/slirp/socket.h +++ b/slirp/socket.h @@ -144,6 +144,8 @@ void sorecvfrom(struct socket *); int sosendto(struct socket *, struct mbuf *); struct socket * tcp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int, int); +struct socket *tcp6_listen(Slirp *, struct in6_addr, u_int, + struct in6_addr, u_int, int); void soisfconnecting(register struct socket *); void soisfconnected(register struct socket *); void sofwdrain(struct socket *); From patchwork Fri Oct 26 00:03:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: max7255 X-Patchwork-Id: 10656877 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E0173109C for ; Fri, 26 Oct 2018 01:26:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D56462C571 for ; Fri, 26 Oct 2018 01:26:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9E522C57B; Fri, 26 Oct 2018 01:26:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5BD9A2C571 for ; Fri, 26 Oct 2018 01:26:09 +0000 (UTC) Received: from localhost ([::1]:57570 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFqt3-0003i8-Bc for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 21:26:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37602) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFpbm-0002v5-A8 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFpbk-0007eJ-Hx for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:10 -0400 Received: from forwardcorp1j.cmail.yandex.net ([2a02:6b8:0:1630::190]:34832) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFpbk-0007dL-0D for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:08 -0400 Received: from mxbackcorp1o.mail.yandex.net (mxbackcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::301]) by forwardcorp1j.cmail.yandex.net (Yandex) with ESMTP id 637D920F69 for ; Fri, 26 Oct 2018 03:04:04 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [2a02:6b8:0:1a2d::30]) by mxbackcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id yiWmMSqtqQ-44rKuC3C; Fri, 26 Oct 2018 03:04:04 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512244; bh=DKbEDdhIJzUlQWUs0aXMGMSwUDYheHnIAu4V97dUgJc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=GX/SJEEdXcBTxKbSRQYPEWWzvlx9nHYyt69X74V1vCEQG3UQGfFb4Un5/9eVZyJNv VBhivIQ8WFQwLpkscBIedUJU8FqtIQVGuXhyTl0wmlnRSXEZdto1sVvKWcLzTVrkLc hq0WGWRNOBBLOTZZ9W0PZnQhoB/Ve9Z2R5e9diWA= Received: from dynamic-iva.dhcp.yndx.net (dynamic-iva.dhcp.yndx.net [2a02:6b8:b010:d002::1:1]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id bOfpZ9lZrb-43FqBbMH; Fri, 26 Oct 2018 03:04:04 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512244; bh=DKbEDdhIJzUlQWUs0aXMGMSwUDYheHnIAu4V97dUgJc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=GX/SJEEdXcBTxKbSRQYPEWWzvlx9nHYyt69X74V1vCEQG3UQGfFb4Un5/9eVZyJNv VBhivIQ8WFQwLpkscBIedUJU8FqtIQVGuXhyTl0wmlnRSXEZdto1sVvKWcLzTVrkLc hq0WGWRNOBBLOTZZ9W0PZnQhoB/Ve9Z2R5e9diWA= Authentication-Results: smtpcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Maxim Samoylov To: qemu-devel@nongnu.org Date: Fri, 26 Oct 2018 03:03:41 +0300 Message-Id: <1540512223-21199-3-git-send-email-max7255@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> References: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:6b8:0:1630::190 X-Mailman-Approved-At: Thu, 25 Oct 2018 21:24:29 -0400 Subject: [Qemu-devel] [PATCH RFC 2/4] slirp: add helper for udp6 socket creation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: max7255@yandex-team.ru Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Maxim Samoylov --- slirp/udp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ slirp/udp.h | 2 ++ 2 files changed, 50 insertions(+) diff --git a/slirp/udp.c b/slirp/udp.c index e5bf065..ab24935 100644 --- a/slirp/udp.c +++ b/slirp/udp.c @@ -366,3 +366,51 @@ udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr, return so; } + +struct socket * +udp6_listen(Slirp *slirp, struct in6_addr haddr, u_int hport, + struct in6_addr laddr, u_int lport, int flags) +{ + struct sockaddr_in6 addr; + struct socket *so; + socklen_t addrlen = sizeof(addr); + + memset(&addr, 0, sizeof(addr)); + + so = socreate(slirp); + if (!so) { + return NULL; + } + so->s = qemu_socket(AF_INET6, SOCK_DGRAM, 0); + if (so->s < 0) { + sofree(so); + return NULL; + } + so->so_expire = curtime + SO_EXPIRE; + insque(so, &slirp->udb); + + addr.sin6_family = AF_INET6; + addr.sin6_addr = haddr; + addr.sin6_port = hport; + + if (bind(so->s, (struct sockaddr *)&addr, addrlen) < 0) { + udp_detach(so); + return NULL; + } + socket_set_fast_reuse(so->s); + + getsockname(so->s, (struct sockaddr *)&addr, &addrlen); + so->fhost.sin6 = addr; + sotranslate_accept(so); + so->so_lfamily = AF_INET6; + so->so_lport = lport; + so->so_laddr6 = laddr; + if (flags != SS_FACCEPTONCE) { + so->so_expire = 0; + } + + so->so_state &= SS_PERSISTENT_MASK; + so->so_state |= SS_ISFCONNECTED | flags; + + return so; +} diff --git a/slirp/udp.h b/slirp/udp.h index be657cf..862c6e8 100644 --- a/slirp/udp.h +++ b/slirp/udp.h @@ -80,6 +80,8 @@ int udp_attach(struct socket *, unsigned short af); void udp_detach(struct socket *); struct socket * udp_listen(Slirp *, uint32_t, u_int, uint32_t, u_int, int); +struct socket *udp6_listen(Slirp *slirp, struct in6_addr, u_int, + struct in6_addr, u_int, int); int udp_output(struct socket *so, struct mbuf *m, struct sockaddr_in *saddr, struct sockaddr_in *daddr, int iptos); From patchwork Fri Oct 26 00:03:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: max7255 X-Patchwork-Id: 10656873 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8424114BB for ; Fri, 26 Oct 2018 01:26:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F4EF2C571 for ; Fri, 26 Oct 2018 01:26:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5ED772C592; Fri, 26 Oct 2018 01:26:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 05D332C571 for ; Fri, 26 Oct 2018 01:26:05 +0000 (UTC) Received: from localhost ([::1]:57576 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFqt3-0004uA-3W for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 21:26:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37604) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFpbm-0002v6-Af for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFpbk-0007eK-Hy for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:10 -0400 Received: from forwardcorp1j.cmail.yandex.net ([2a02:6b8:0:1630::190]:34835) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFpbk-0007dP-0C for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:08 -0400 Received: from mxbackcorp2j.mail.yandex.net (mxbackcorp2j.mail.yandex.net [IPv6:2a02:6b8:0:1619::119]) by forwardcorp1j.cmail.yandex.net (Yandex) with ESMTP id 0F65F20F6F for ; Fri, 26 Oct 2018 03:04:05 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [2a02:6b8:0:1a2d::30]) by mxbackcorp2j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id RnuLfnKwEP-44xeEIHe; Fri, 26 Oct 2018 03:04:05 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512245; bh=FttTaZ5olyP43jy6tGDRHv2G9vKibYR95w4VTfl8s5I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=GPDjsFI57OWRfXmsuQx9qZ02p7TG64dna3gl3WZXDUDf6zsEQxftfOuZYl+ZlIgbu fjp3yCy1My3Iz5ofgZsei4//x/08GPMRth/APTj16nmqGJCV5qw9uCvIwyXdEujME/ Hm9KHcFr+ZyUZIg4dIiexaI87lCBV1HSG4RngKGQ= Received: from dynamic-iva.dhcp.yndx.net (dynamic-iva.dhcp.yndx.net [2a02:6b8:b010:d002::1:1]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id bOfpZ9lZrb-44FGPvXa; Fri, 26 Oct 2018 03:04:04 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512244; bh=FttTaZ5olyP43jy6tGDRHv2G9vKibYR95w4VTfl8s5I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=TL2jzTUGlBDCDyWVons0LgWGSZGah9QjsyLF9XIpWF25pqVtPGE4V61GHiar+/Xdb q7T23Z+W0UEWGp5x9BF+S/plpqkQq8KdnGewqErVh+GMGdgXs1mxsdSEJbJW7ELNdl bIJyurkZeHsa/dlhO8xcxN+Ui9s9Jx98HO3Oeoes= Authentication-Results: smtpcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Maxim Samoylov To: qemu-devel@nongnu.org Date: Fri, 26 Oct 2018 03:03:42 +0300 Message-Id: <1540512223-21199-4-git-send-email-max7255@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> References: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:6b8:0:1630::190 X-Mailman-Approved-At: Thu, 25 Oct 2018 21:24:29 -0400 Subject: [Qemu-devel] [PATCH RFC 3/4] slirp: add helpers for ipv6 hostfwd manipulation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: max7255@yandex-team.ru Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Maxim Samoylov --- slirp/libslirp.h | 6 ++++++ slirp/slirp.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/slirp/libslirp.h b/slirp/libslirp.h index 42e42e9..3710650 100644 --- a/slirp/libslirp.h +++ b/slirp/libslirp.h @@ -34,6 +34,12 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr guest_addr, int guest_port); int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, int host_port); +int slirp_add_ipv6_hostfwd(Slirp *slirp, int is_udp, + struct in6_addr host_addr, int host_port, + struct in6_addr guest_addr, int guest_port); +int slirp_remove_ipv6_hostfwd(Slirp *slirp, int is_udp, + struct in6_addr host_addr, int host_port); + int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, struct in_addr *guest_addr, int guest_port); diff --git a/slirp/slirp.c b/slirp/slirp.c index 51de41f..143ddea 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -1065,6 +1065,49 @@ int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, return 0; } +int slirp_remove_ipv6_hostfwd(Slirp *slirp, int is_udp, + struct in6_addr host_addr, int host_port) +{ + struct socket *so; + struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); + struct sockaddr_in6 addr; + int port = htons(host_port); + socklen_t addr_len; + + for (so = head->so_next; so != head; so = so->so_next) { + addr_len = sizeof(addr); + if ((so->so_state & SS_HOSTFWD) && + getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && + addr_len == sizeof(host_addr) && + !memcmp(&host_addr, &addr, addr_len) && + addr.sin6_port == port) { + + close(so->s); + sofree(so); + return 0; + } + } + + return -1; +} + +int slirp_add_ipv6_hostfwd(Slirp *slirp, int is_udp, + struct in6_addr host_addr, int host_port, + struct in6_addr guest_addr, int guest_port) +{ + if (is_udp) { + if (!udp6_listen(slirp, host_addr, htons(host_port), + guest_addr, htons(guest_port), SS_HOSTFWD)) + return -1; + } else { + if (!tcp6_listen(slirp, host_addr, htons(host_port), + guest_addr, htons(guest_port), SS_HOSTFWD)) + return -1; + } + + return 0; +} + int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, struct in_addr *guest_addr, int guest_port) { From patchwork Fri Oct 26 00:03:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: max7255 X-Patchwork-Id: 10656881 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2B86314BB for ; Fri, 26 Oct 2018 01:28:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C4A82BE39 for ; Fri, 26 Oct 2018 01:28:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EF6852BE49; Fri, 26 Oct 2018 01:28:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 338F72BE39 for ; Fri, 26 Oct 2018 01:28:35 +0000 (UTC) Received: from localhost ([::1]:57592 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFqvS-0007lc-Ig for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 21:28:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37621) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFpbn-0002vF-Nd for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFpbm-0007er-66 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:11 -0400 Received: from forwardcorp1g.cmail.yandex.net ([2a02:6b8:0:1465::fd]:59678) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFpbk-0007da-0J for qemu-devel@nongnu.org; Thu, 25 Oct 2018 20:04:08 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1g.cmail.yandex.net (Yandex) with ESMTP id D19CE21804 for ; Fri, 26 Oct 2018 03:04:05 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [2a02:6b8:0:1a2d::30]) by mxbackcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id PuIrsX2NYT-45Imk7jf; Fri, 26 Oct 2018 03:04:05 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512245; bh=EEuNkUO1F6OIRJyxM2cr0pMjMwq9WBtd07tfCJFPdhc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=cgmQI/oMzpCHO60jmjaoHGHPdeC6uWkL2ECej/6VIbIEvdaeCBYElzJE/mFhbv7i7 utt8PG2N5PyGrZ5xM39Et981XGbj63DmLTM7RF+L5D+0S5AmTdkhjpnERqyzzpR+bN U2sbBHHF6/xgbXz9a+z1u38CjE9BdhBLYyQFHDmQ= Received: from dynamic-iva.dhcp.yndx.net (dynamic-iva.dhcp.yndx.net [2a02:6b8:b010:d002::1:1]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id bOfpZ9lZrb-45Fq7IiA; Fri, 26 Oct 2018 03:04:05 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1540512245; bh=EEuNkUO1F6OIRJyxM2cr0pMjMwq9WBtd07tfCJFPdhc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=cgmQI/oMzpCHO60jmjaoHGHPdeC6uWkL2ECej/6VIbIEvdaeCBYElzJE/mFhbv7i7 utt8PG2N5PyGrZ5xM39Et981XGbj63DmLTM7RF+L5D+0S5AmTdkhjpnERqyzzpR+bN U2sbBHHF6/xgbXz9a+z1u38CjE9BdhBLYyQFHDmQ= Authentication-Results: smtpcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Maxim Samoylov To: qemu-devel@nongnu.org Date: Fri, 26 Oct 2018 03:03:43 +0300 Message-Id: <1540512223-21199-5-git-send-email-max7255@yandex-team.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> References: <1540512223-21199-1-git-send-email-max7255@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a02:6b8:0:1465::fd X-Mailman-Approved-At: Thu, 25 Oct 2018 21:24:29 -0400 Subject: [Qemu-devel] [PATCH RFC 4/4] net/slirp: add ipv6-hostfwd option for user netdev type X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: max7255@yandex-team.ru Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This allows forwarding TCP6 and UDP6 connections down to netdev=user connected guests. Signed-off-by: Maxim Samoylov --- hmp-commands.hx | 31 ++++++++ include/net/slirp.h | 2 + net/slirp.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++ qapi/net.json | 3 +- 4 files changed, 249 insertions(+), 1 deletion(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index db0c681..b0e1a08 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1448,6 +1448,37 @@ STEXI Remove host-to-guest TCP or UDP redirection. ETEXI +#ifdef CONFIG_SLIRP + { + .name = "ipv6_hostfwd_add", + .args_type = "arg1:s,arg2:s?,arg3:s?", + .params = "[hub_id name]|[netdev_id] [tcp|udp]:[hostaddr6]:hostport-guestaddr6:guestport", + .help = "redirect TCP6 or UDP6 connections from host to guest (requires -net user)", + .cmd = hmp_ipv6_hostfwd_add, + }, +#endif +STEXI +@item hostfwd_add +@findex hostfwd_add +Redirect TCP6 or UDP6 connections from host to guest (requires -net user). +ETEXI + +#ifdef CONFIG_SLIRP + { + .name = "ipv6_hostfwd_remove", + .args_type = "arg1:s,arg2:s?,arg3:s?", + .params = "[hub_id name]|[netdev_id] [tcp|udp]:[hostaddr6]:hostport", + .help = "remove host-to-guest TCP6 or UDP6 redirection", + .cmd = hmp_ipv6_hostfwd_remove, + }, + +#endif +STEXI +@item hostfwd_remove +@findex hostfwd_remove +Remove host-to-guest TCP6 or UDP6 redirection. +ETEXI + { .name = "balloon", .args_type = "value:M", diff --git a/include/net/slirp.h b/include/net/slirp.h index bad3e1e..4796a5c 100644 --- a/include/net/slirp.h +++ b/include/net/slirp.h @@ -29,6 +29,8 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict); void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict); +void hmp_ipv6_hostfwd_add(Monitor *mon, const QDict *qdict); +void hmp_ipv6_hostfwd_remove(Monitor *mon, const QDict *qdict); void hmp_info_usernet(Monitor *mon, const QDict *qdict); diff --git a/net/slirp.c b/net/slirp.c index f6dc039..abe112b 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -67,6 +67,7 @@ static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) /* slirp network adapter */ #define SLIRP_CFG_HOSTFWD 1 +#define SLIRP_CFG_IPV6_HOSTFWD 2 struct slirp_config_str { struct slirp_config_str *next; @@ -89,6 +90,8 @@ static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = QTAILQ_HEAD_INITIALIZER(slirp_stacks); static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp); +static int slirp_ipv6_hostfwd(SlirpState *s, const char *redir_str, + Error **errp); static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp); #ifndef _WIN32 @@ -386,6 +389,10 @@ static int net_slirp_init(NetClientState *peer, const char *model, if (slirp_hostfwd(s, config->str, errp) < 0) { goto error; } + } else if (config->flags & SLIRP_CFG_IPV6_HOSTFWD) { + if (slirp_ipv6_hostfwd(s, config->str, errp) < 0) { + goto error; + } } else { if (slirp_guestfwd(s, config->str, errp) < 0) { goto error; @@ -504,6 +511,73 @@ void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) monitor_printf(mon, "invalid format\n"); } +void hmp_ipv6_hostfwd_remove(Monitor *mon, const QDict *qdict) +{ + struct in6_addr host_addr = in6addr_any; + int host_port; + char buf[256]; + const char *src_str, *p; + SlirpState *s; + int is_udp = 0; + int err; + const char *arg1 = qdict_get_str(qdict, "arg1"); + const char *arg2 = qdict_get_try_str(qdict, "arg2"); + const char *arg3 = qdict_get_try_str(qdict, "arg3"); + + if (arg2) { + s = slirp_lookup(mon, arg1, arg2); + src_str = arg3; + } else { + s = slirp_lookup(mon, NULL, NULL); + src_str = arg1; + } + if (!s) { + return; + } + + p = src_str; + if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { + goto fail_syntax; + } + + if (!strcmp(buf, "tcp") || buf[0] == '\0') { + is_udp = 0; + } else if (!strcmp(buf, "udp")) { + is_udp = 1; + } else { + goto fail_syntax; + } + + if (*(p++) != '[') { + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ']') < 0) { + goto fail_syntax; + } + + if (!inet_pton(AF_INET6, buf, &host_addr)) { + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { + goto fail_syntax; + } + + if (qemu_strtoi(p, NULL, 10, &host_port) < 0) { + goto fail_syntax; + } + + err = slirp_remove_ipv6_hostfwd(s->slirp, is_udp, host_addr, host_port); + + monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, + err ? "not found" : "removed"); + return; + + fail_syntax: + monitor_printf(mon, "invalid format\n"); +} + static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) { struct in_addr host_addr = { .s_addr = INADDR_ANY }; @@ -577,6 +651,119 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) return -1; } +static int slirp_ipv6_hostfwd(SlirpState *s, const char *redir_str, + Error **errp) +{ + struct in6_addr host_addr = in6addr_any; + struct in6_addr guest_addr; + int host_port, guest_port; + const char *p; + char buf[256]; + int is_udp; + const char *end; + const char *fail_reason = "Unknown reason"; + + memset(&guest_addr, 0, sizeof(guest_addr)); + + p = redir_str; + if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { + fail_reason = "No : separators"; + goto fail_syntax; + } + if (!strcmp(buf, "tcp") || buf[0] == '\0') { + is_udp = 0; + } else if (!strcmp(buf, "udp")) { + is_udp = 1; + } else { + fail_reason = "Bad protocol name"; + goto fail_syntax; + } + + if (*(p++) != '[') { + fail_reason = "IPv6 address must be enclosed in square brackets"; + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ']') < 0) { + fail_reason = "IPv6 address must be enclosed in square brackets"; + goto fail_syntax; + } + + if (!inet_pton(AF_INET6, buf, &host_addr)) { + fail_reason = "Bad host address"; + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ':') < 0 || + buf[0] != '\0') { + fail_reason = "Bad ipv6 address and port separator"; + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { + fail_reason = "Bad host port separator"; + goto fail_syntax; + } + + if (qemu_strtoi(buf, &end, 0, &host_port)) { + fail_reason = "Bad host port"; + goto fail_syntax; + } + + if (*end != '\0' || host_port < 0 || host_port > 65535) { + fail_reason = "Host port out of range"; + goto fail_syntax; + } + + if (*p == '\0') { + fail_reason = "Missing guest ipv6 address"; + goto fail_syntax; + } + + if (*(p++) != '[') { + fail_reason = "IPv6 address must be enclosed in square brackets"; + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ']') < 0) { + fail_reason = "IPv6 address must be enclosed in square brackets"; + goto fail_syntax; + } + + if (!inet_pton(AF_INET6, buf, &guest_addr)) { + fail_reason = "Bad guest address"; + goto fail_syntax; + } + + if (get_str_sep(buf, sizeof(buf), &p, ':') < 0 || buf[0] != '\0') { + fail_reason = "Bad ipv6 address and port separator"; + goto fail_syntax; + } + + if (qemu_strtoi(p, &end, 0, &guest_port) < 0) { + fail_reason = "Bad guest port"; + goto fail_syntax; + } + + if (*end != '\0' || guest_port < 1 || guest_port > 65535) { + fail_reason = "Guest port number out of range"; + goto fail_syntax; + } + + if (slirp_add_ipv6_hostfwd(s->slirp, is_udp, host_addr, host_port, + guest_addr, guest_port) < 0) { + error_report("could not set up host forwarding rule '%s'", + redir_str); + return -1; + } + return 0; + + fail_syntax: + error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str, + fail_reason); + return -1; +} + void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) { const char *redir_str; @@ -604,6 +791,32 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) } +void hmp_ipv6_hostfwd_add(Monitor *mon, const QDict *qdict) +{ + const char *redir_str; + SlirpState *s; + const char *arg1 = qdict_get_str(qdict, "arg1"); + const char *arg2 = qdict_get_try_str(qdict, "arg2"); + const char *arg3 = qdict_get_try_str(qdict, "arg3"); + + if (arg2) { + s = slirp_lookup(mon, arg1, arg2); + redir_str = arg3; + } else if (arg2) { + s = slirp_lookup(mon, NULL, arg1); + redir_str = arg2; + } else { + s = slirp_lookup(mon, NULL, NULL); + redir_str = arg1; + } + if (s) { + Error *err = NULL; + if (slirp_ipv6_hostfwd(s, redir_str, &err) < 0) { + error_report_err(err); + } + } +} + #ifndef _WIN32 /* automatic user mode samba server configuration */ @@ -906,6 +1119,7 @@ int net_init_slirp(const Netdev *netdev, const char *name, /* all optional fields are initialized to "all bits zero" */ net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD); + net_init_slirp_configs(user->ipv6_hostfwd, SLIRP_CFG_IPV6_HOSTFWD); net_init_slirp_configs(user->guestfwd, 0); ret = net_slirp_init(peer, "user", name, user->q_restrict, diff --git a/qapi/net.json b/qapi/net.json index 8f99fd9..4f67caf 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -201,7 +201,8 @@ '*smbserver': 'str', '*hostfwd': ['String'], '*guestfwd': ['String'], - '*tftp-server-name': 'str' } } + '*tftp-server-name': 'str', + '*ipv6-hostfwd': ['String']} } ## # @NetdevTapOptions: