From patchwork Wed Oct 28 20:40:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 537567 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2B5AD141351 for ; Thu, 29 Oct 2015 07:43:54 +1100 (AEDT) Received: from localhost ([::1]:40669 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrXZY-0006Z6-9a for incoming@patchwork.ozlabs.org; Wed, 28 Oct 2015 16:43:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrXWg-0001qR-4a for qemu-devel@nongnu.org; Wed, 28 Oct 2015 16:40:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZrXWd-0004PQ-Ir for qemu-devel@nongnu.org; Wed, 28 Oct 2015 16:40:54 -0400 Received: from smtp4-g21.free.fr ([2a01:e0c:1:1599::13]:37948) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrXWd-0004P1-6k for qemu-devel@nongnu.org; Wed, 28 Oct 2015 16:40:51 -0400 Received: from Quad.localdomain (unknown [78.238.229.36]) by smtp4-g21.free.fr (Postfix) with ESMTPS id 9522E4C8079; Wed, 28 Oct 2015 21:40:50 +0100 (CET) From: Laurent Vivier To: qemu-devel@nongnu.org Date: Wed, 28 Oct 2015 21:40:45 +0100 Message-Id: <1446064846-12529-5-git-send-email-laurent@vivier.eu> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1446064846-12529-1-git-send-email-laurent@vivier.eu> References: <1446064846-12529-1-git-send-email-laurent@vivier.eu> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:e0c:1:1599::13 Cc: peter.maydell@linaro.org, riku.voipio@iki.fi, Laurent Vivier Subject: [Qemu-devel] [PATCH v3 4/5] linux-user: manage bind with a socket of SOCK_PACKET type. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This is obsolete, but if we want to use dhcp with an old distro (like debian etch), we need it. Some users (like dhclient) use SOCK_PACKET with AF_PACKET and the kernel allows that. packet(7) In Linux 2.0, the only way to get a packet socket was by calling socket(AF_INET, SOCK_PACKET, protocol). This is still supported but strongly deprecated. The main difference between the two methods is that SOCK_PACKET uses the old struct sockaddr_pkt to specify an inter‐ face, which doesn't provide physical layer independence. struct sockaddr_pkt { unsigned short spkt_family; unsigned char spkt_device[14]; unsigned short spkt_protocol; }; spkt_family contains the device type, spkt_protocol is the IEEE 802.3 protocol type as defined in and spkt_device is the device name as a null-terminated string, for example, eth0. Signed-off-by: Laurent Vivier --- v3: rename packet_target_to_host_addr to packet_target_to_host_sockaddr linux-user/syscall.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index a313846..22b28a4 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -2090,6 +2090,30 @@ static int sock_flags_fixup(int fd, int target_type) return fd; } +static abi_long packet_target_to_host_sockaddr(void *host_addr, + abi_ulong target_addr, + socklen_t len) +{ + struct sockaddr *addr = host_addr; + struct target_sockaddr *target_saddr; + + target_saddr = lock_user(VERIFY_READ, target_addr, len, 1); + if (!target_saddr) { + return -TARGET_EFAULT; + } + + memcpy(addr, target_saddr, len); + addr->sa_family = tswap16(target_saddr->sa_family); + /* spkt_protocol is big-endian */ + + unlock_user(target_saddr, target_addr, 0); + return 0; +} + +static TargetFdTrans target_packet_trans = { + .target_to_host_addr = packet_target_to_host_sockaddr, +}; + /* do_socket() Must return target values and target errnos. */ static abi_long do_socket(int domain, int type, int protocol) { @@ -2112,6 +2136,12 @@ static abi_long do_socket(int domain, int type, int protocol) ret = get_errno(socket(domain, type, protocol)); if (ret >= 0) { ret = sock_flags_fixup(ret, target_type); + if (type == SOCK_PACKET) { + /* Manage an obsolete case : + * if socket type is SOCK_PACKET, bind by name + */ + fd_trans_register(ret, &target_packet_trans); + } } return ret; }