From patchwork Wed Feb 3 23:35:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhijian Li (Fujitsu)\" via" X-Patchwork-Id: 12065717 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3EF0EC433E0 for ; Wed, 3 Feb 2021 23:38:19 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BCD9864F43 for ; Wed, 3 Feb 2021 23:38:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BCD9864F43 Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:45264 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l7Riz-0007Zp-Ty for qemu-devel@archiver.kernel.org; Wed, 03 Feb 2021 18:38:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:45020) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3UTMbYAMKCiwLRMOWWOTM.KWUYMUc-LMdMTVWVOVc.WZO@flex--dje.bounces.google.com>) id 1l7Rgd-0006At-D2 for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:51 -0500 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]:42208) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3UTMbYAMKCiwLRMOWWOTM.KWUYMUc-LMdMTVWVOVc.WZO@flex--dje.bounces.google.com>) id 1l7Rga-00017a-GL for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:50 -0500 Received: by mail-pf1-x449.google.com with SMTP id t22so910905pfc.9 for ; Wed, 03 Feb 2021 15:35:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=HsT+Ov8zrbL/HsoFJSvrskJLSXDBhfAhVatmUMllo8Y=; b=UiUoGfM5qVmGbd+MUc4p4XaEBfvNufeCXTv3jovNCkRGZkvKwVIHqJ1oj3g3p+5bXQ C93IHucQJ64rJdExABaa/3nMGtK3vOKlVik4i6qblLATwC5PGdNiDscROXtDpre8FN71 6ZXkf6l+0nEPmWdHf1WyjwF8eOLOgk4PeAnYnlGpxUhAFKIWZ1YUoaB7kyXESZGPetwW KVD7KkvKQSYjMVi7ZtRfRBOfmBLo95wMsgD4axxqt65sOA4Def08MuoQH6x8txvo5brq 48hc96RaJ8MIefg76tkAF3Mi7UgBz9Acl2QLw/r+OJ1nhia0v9kSn2axpvheUocjMVYf fX/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=HsT+Ov8zrbL/HsoFJSvrskJLSXDBhfAhVatmUMllo8Y=; b=MtUQQesVSOUVacBsgP7i+AICyQ3/9u5bPSmrmhGBNUCQxVNUojEqn3hjk8/ztbpVNr t3lvnhxGPVqlm+XTGuYfepTqbaebEzcoRwbI2VmLqcsoUtiBM9uGnEbrQI95f6S9Rf+u b9Z+tm7rsekJrehuQxT7E6jPFz8Hj/m5R+xWi77Gfbt7cwKuR/Df0SM3JruSQ6/8Z7DZ IY7q/Ca3xQMcFUWmhpN2oC8PjGLhW77l2LRZBwCA9a9AJ6y0n+XzuI1O46iL5TPyhspS Ao7GyDDCZvgosvx0TIhEMWJkqvPTr2Ig16sOvfFDqTwBfElm8Oypwi9lh7tfrHG4kkDB /Wvg== X-Gm-Message-State: AOAM530wI7fYzaIo+YE9OqJdYipnzNMSDCRLS4YVuRU5+7Gb8HQ62ZDv lIopJn1FzuAQCkB5Djgsn9jx0oiPJ0W+ifielSYa77L+UJlqwYcz/NZBi3l8/6/JvkyCwGf4AOj qQ7rt7boDgWmtv8a/PaDZ0LnhuJW6Mse/smUhqempGlQpiBlxPs0a X-Google-Smtp-Source: ABdhPJxv7Pa2t3XX/qRiVKXr31USYBVLSfs+UThc3MM0ZocmXIkIGtwG8MyuqyCW+E94nNSX3+HsYQw= X-Received: from ruffy.mtv.corp.google.com ([2620:0:1000:1412:25d4:96ad:ca30:7913]) (user=dje job=sendgmr) by 2002:a17:90a:53:: with SMTP id 19mr5305514pjb.29.1612395345381; Wed, 03 Feb 2021 15:35:45 -0800 (PST) Date: Wed, 3 Feb 2021 15:35:37 -0800 In-Reply-To: <20210203233539.1990032-1-dje@google.com> Message-Id: <20210203233539.1990032-2-dje@google.com> Mime-Version: 1.0 References: <20210203233539.1990032-1-dje@google.com> X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 1/3] slirp: Placeholder for libslirp ipv6 hostfwd support To: qemu-devel@nongnu.org Cc: Samuel Thibault , Doug Evans Received-SPF: pass client-ip=2607:f8b0:4864:20::449; envelope-from=3UTMbYAMKCiwLRMOWWOTM.KWUYMUc-LMdMTVWVOVc.WZO@flex--dje.bounces.google.com; helo=mail-pf1-x449.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Doug Evans X-Patchwork-Original-From: dje--- via From: "Zhijian Li (Fujitsu)\" via" This commit is intended to only contain the slirp submodule change that adds ipv6 hostfwd support. --- slirp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slirp b/slirp index 8f43a99191..358c0827d4 160000 --- a/slirp +++ b/slirp @@ -1 +1 @@ -Subproject commit 8f43a99191afb47ca3f3c6972f6306209f367ece +Subproject commit 358c0827d49778f016312bfb4167fe639900681f From patchwork Wed Feb 3 23:35:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhijian Li (Fujitsu)\" via" X-Patchwork-Id: 12065719 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BB33C433DB for ; Wed, 3 Feb 2021 23:38:21 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2229364E49 for ; Wed, 3 Feb 2021 23:38:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2229364E49 Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:45388 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l7Rj2-0007cx-4E for qemu-devel@archiver.kernel.org; Wed, 03 Feb 2021 18:38:20 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:45040) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3UzMbYAMKCi4NTOQYYQVO.MYWaOWe-NOfOVXYXQXe.YbQ@flex--dje.bounces.google.com>) id 1l7Rgf-0006DX-3i for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:53 -0500 Received: from mail-pf1-x449.google.com ([2607:f8b0:4864:20::449]:39902) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3UzMbYAMKCi4NTOQYYQVO.MYWaOWe-NOfOVXYXQXe.YbQ@flex--dje.bounces.google.com>) id 1l7Rgc-00017k-6c for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:52 -0500 Received: by mail-pf1-x449.google.com with SMTP id f6so917627pfn.6 for ; Wed, 03 Feb 2021 15:35:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=5El4f2Z4lPWquzBwOXabKYcHGb6MT4F27B38FnUhd0c=; b=IKDqDRgTQO6rwP1Spsf0lG9wtZOpjQUJ6VNy9tSFmnpcUwF9uFvqUBqeSSR59Yb0mN zJ6F7H8J2M/n1+tq+ILhOp8nPrjsQ0g6WEjH4ZT2bMwydwdOSAMPLp1yTFXsgb5Oskeh hTQhrNLPOO9deqfqA/lBzcNHqMK6SHHtpxOJBWJ5O/BapX4nIZGi13ixP83iUdPaLjP4 ueBFuGlQKFHWHflN1QjxZ6++JgTpTNzOPZP++fN0AUuHj/Zd8pnK1UR4HWaxHGxLYPoX tw4/u5SPY3FwqL4DswQjJrx0nNYU/zX2C1PwxpEEX/XQfYsWnLJhaof/QzzFq48tNqa9 cupA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5El4f2Z4lPWquzBwOXabKYcHGb6MT4F27B38FnUhd0c=; b=iH6ZdtXYy8x1zZKVlYwm+2QIW8U52jNTqKl1K84LpyluOpM3doiwz3dbCa+uxPnVGQ 9gWYAL07UWFN+CSLGkU1pbQyhCbnn0ls9u0yEDpaI6ber9orkzBWa8p6AmeYB2COU0I+ mLRJrPPRmz9kNY/vUb86hO2I7eg1ugz/UzeR8i/+Zdb2JYcbmJgeINY9k4wQ2DnxC1pl uhW8JRhqPZbZw3XxVBcwZAfj08ABJDZ8Kte3A/2FTJK97Svis+YOssiMS9NCGyWkDvA/ sbVSzRONaTkrI7xj6pbLszRA9x9IEopl/g6+XTb5gIP6ot3+FgXvE7nPdrYilnF8NUZp MXEA== X-Gm-Message-State: AOAM530pilfOn7ws8ZfXLy8ZzrYE3ARQKNlXwqP+zLvQDrdnGefAlDT7 /TcaX8e5Av0icCrCotk2lYdWP24wjn6/hXivxa1laQi22PDznixWrTZmo08hob/q8PZvQZg/sjP p69T3Z/8h3grjDzmR2sqd3fg2EqIW+k4h1sOEFyrHwERk7b0ArgxX X-Google-Smtp-Source: ABdhPJwtDaRJ4UjOJJEKWyOH/tMP2bkCvBFZOrXlkfEmLRXtc85p/T+4Xmv2bhWXYypce4PbPbpj8hY= X-Received: from ruffy.mtv.corp.google.com ([2620:0:1000:1412:25d4:96ad:ca30:7913]) (user=dje job=sendgmr) by 2002:a63:f60e:: with SMTP id m14mr6057586pgh.413.1612395347635; Wed, 03 Feb 2021 15:35:47 -0800 (PST) Date: Wed, 3 Feb 2021 15:35:38 -0800 In-Reply-To: <20210203233539.1990032-1-dje@google.com> Message-Id: <20210203233539.1990032-3-dje@google.com> Mime-Version: 1.0 References: <20210203233539.1990032-1-dje@google.com> X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 2/3] net/slirp.c: Refactor address parsing To: qemu-devel@nongnu.org Cc: Samuel Thibault , Doug Evans Received-SPF: pass client-ip=2607:f8b0:4864:20::449; envelope-from=3UzMbYAMKCi4NTOQYYQVO.MYWaOWe-NOfOVXYXQXe.YbQ@flex--dje.bounces.google.com; helo=mail-pf1-x449.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Doug Evans X-Patchwork-Original-From: dje--- via From: "Zhijian Li (Fujitsu)\" via" ... in preparation for adding ipv6 host forwarding support. Signed-off-by: Doug Evans --- net/slirp.c | 200 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 129 insertions(+), 71 deletions(-) diff --git a/net/slirp.c b/net/slirp.c index be914c0be0..a21a313302 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -631,15 +631,83 @@ static SlirpState *slirp_lookup(Monitor *mon, const char *id) } } -void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) +/* + * Parse a protocol name of the form "name". + * Valid protocols are "tcp" and "udp". An empty string means "tcp". + * Returns a pointer to the end of the parsed string on success, and stores + * the result in *is_udp. + * Otherwise returns NULL and stores the error message in *errmsg, which must + * be freed by the caller. + */ +static const char *parse_protocol(const char *str, int sep, int *is_udp, + char **errmsg) +{ + char buf[10]; + const char *p = str; + + if (get_str_sep(buf, sizeof(buf), &p, sep) < 0) { + *errmsg = g_strdup("Missing protcol name separator"); + return NULL; + } + + if (!strcmp(buf, "tcp") || buf[0] == '\0') { + *is_udp = 0; + } else if (!strcmp(buf, "udp")) { + *is_udp = 1; + } else { + *errmsg = g_strdup("Bad protcol name"); + return NULL; + } + + return p; +} + +/* + * Parse an ipv4 address/port of the form "addrport". + * "kind" is either "host" or "guest" and is included in error messages. + * An empty address means INADDR_ANY. + * Returns a pointer to the end of the parsed string on success, and stores + * the results in *addr, *port. + * Otherwise returns NULL and stores the error message in *errmsg, which must + * be freed by the caller. + */ +static const char *parse_in4_addr_port(const char *str, const char *kind, + int addr_sep, int port_sep, + struct in_addr *addr, int *port, + char **errmsg) { - struct in_addr host_addr = { .s_addr = INADDR_ANY }; - int host_port; char buf[256]; - const char *src_str, *p; + const char *p = str; + + if (get_str_sep(buf, sizeof(buf), &p, addr_sep) < 0) { + *errmsg = g_strdup_printf("Missing %s address separator", kind); + return NULL; + } + if (buf[0] == '\0') { + addr->s_addr = INADDR_ANY; + } else if (!inet_aton(buf, addr)) { + *errmsg = g_strdup_printf("Bad %s address", kind); + return NULL; + } + + if (get_str_sep(buf, sizeof(buf), &p, port_sep) < 0) { + *errmsg = g_strdup_printf("Missing %s port separator", kind); + return NULL; + } + if (qemu_strtoi(buf, NULL, 10, port) < 0 || + *port < 0 || *port > 65535) { + *errmsg = g_strdup_printf("Bad %s port", kind); + return NULL; + } + + return p; +} + +static void hmp_hostfwd_remove_worker(Monitor *mon, const QDict *qdict, + int family) +{ + const char *src_str; 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"); @@ -654,38 +722,42 @@ void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) return; } - p = src_str; - if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { - goto fail_syntax; - } + int host_port; + int is_udp; + char *errmsg = NULL; + int err; - if (!strcmp(buf, "tcp") || buf[0] == '\0') { - is_udp = 0; - } else if (!strcmp(buf, "udp")) { - is_udp = 1; - } else { - goto fail_syntax; - } + g_assert(src_str != NULL); + const char *p = src_str; - if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { - goto fail_syntax; - } - if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { + p = parse_protocol(p, ':', &is_udp, &errmsg); + if (p == NULL) { goto fail_syntax; } - if (qemu_strtoi(p, NULL, 10, &host_port)) { - goto fail_syntax; + if (family == AF_INET) { + struct in_addr host_addr; + if (parse_in4_addr_port(p, "host", ':', '\0', &host_addr, &host_port, + &errmsg) == NULL) { + goto fail_syntax; + } + err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port); + } else { + g_assert_not_reached(); } - err = slirp_remove_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"); + monitor_printf(mon, "Invalid format: %s\n", errmsg); + g_free(errmsg); +} + +void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) +{ + hmp_hostfwd_remove_worker(mon, qdict, AF_INET); } static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) @@ -694,56 +766,29 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) struct in_addr guest_addr = { .s_addr = 0 }; int host_port, guest_port; const char *p; - char buf[256]; int is_udp; - char *end; - const char *fail_reason = "Unknown reason"; + char *errmsg = NULL; + g_assert(redir_str != NULL); 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 (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { - fail_reason = "Missing : separator"; - goto fail_syntax; - } - if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { - fail_reason = "Bad host address"; + p = parse_protocol(p, ':', &is_udp, &errmsg); + if (p == NULL) { goto fail_syntax; } - if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { - fail_reason = "Bad host port separator"; - goto fail_syntax; - } - host_port = strtol(buf, &end, 0); - if (*end != '\0' || host_port < 0 || host_port > 65535) { - fail_reason = "Bad host port"; + p = parse_in4_addr_port(p, "host", ':', '-', &host_addr, &host_port, + &errmsg); + if (p == NULL) { goto fail_syntax; } - if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { - fail_reason = "Missing guest address"; + if (parse_in4_addr_port(p, "guest", ':', '\0', &guest_addr, &guest_port, + &errmsg) == NULL) { goto fail_syntax; } - if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { - fail_reason = "Bad guest address"; - goto fail_syntax; - } - - guest_port = strtol(p, &end, 0); - if (*end != '\0' || guest_port < 1 || guest_port > 65535) { - fail_reason = "Bad guest port"; + if (guest_port == 0) { + errmsg = g_strdup("Bad guest port"); goto fail_syntax; } @@ -757,11 +802,12 @@ static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) fail_syntax: error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str, - fail_reason); + errmsg); + g_free(errmsg); return -1; } -void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) +static void hmp_hostfwd_add_worker(Monitor *mon, const QDict *qdict, int family) { const char *redir_str; SlirpState *s; @@ -775,13 +821,25 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) s = slirp_lookup(mon, NULL); redir_str = arg1; } - if (s) { - Error *err = NULL; - if (slirp_hostfwd(s, redir_str, &err) < 0) { - error_report_err(err); - } + if (!s) { + return; } + Error *err = NULL; + int rc; + if (family == AF_INET) { + rc = slirp_hostfwd(s, redir_str, &err); + } else { + g_assert_not_reached(); + } + if (rc < 0) { + error_report_err(err); + } +} + +void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) +{ + hmp_hostfwd_add_worker(mon, qdict, AF_INET); } #ifndef _WIN32 From patchwork Wed Feb 3 23:35:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhijian Li (Fujitsu)\" via" X-Patchwork-Id: 12065721 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 820CEC433DB for ; Wed, 3 Feb 2021 23:40:35 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F07C164E49 for ; Wed, 3 Feb 2021 23:40:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F07C164E49 Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:49878 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l7RlC-00014T-48 for qemu-devel@archiver.kernel.org; Wed, 03 Feb 2021 18:40:34 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:45050) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3VTMbYAMKCjAPVQSaaSXQ.OaYcQYg-PQhQXZaZSZg.adS@flex--dje.bounces.google.com>) id 1l7Rgh-0006Hj-Ca for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:55 -0500 Received: from mail-pl1-x649.google.com ([2607:f8b0:4864:20::649]:42586) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3VTMbYAMKCjAPVQSaaSXQ.OaYcQYg-PQhQXZaZSZg.adS@flex--dje.bounces.google.com>) id 1l7Rge-00017y-U3 for qemu-devel@nongnu.org; Wed, 03 Feb 2021 18:35:55 -0500 Received: by mail-pl1-x649.google.com with SMTP id q12so855822plr.9 for ; Wed, 03 Feb 2021 15:35:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=SHHYVrdyyTBmAPWoQx1i3Bhxjp/C3+JJaAKcUlrNtdA=; b=NUBh8oSX+ZkqUxrIKLmFu6hLGDBiNrIrm+N8BU1PcFSnyHQ8uFnskv67ZoYo2lrJgP Ip9lZ+NaRZ6Lnw1KKVfBMRRCsWCWypp9hderUjwL5DFCjVTNwmEYwRgSJIF3EBgDQun7 hlHN3yzNg/p7iOWmz92EqNZPLJX3cl0q95mEcTkXCSorR+8IOHvvKpenZxf5/TUFxGQS kNx60sn/0eWRdw3xKjB9BZZsULfR0hnl/XLmrTr2NPjO7LCALk8ROefS/YqUBauIAZg+ 4w/nTgU3NEEPrMmODLYe9g7cn9qheed3XVcC1qx2/wLOymxfNwWwmzopA0yHaXXC6GMI AShQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=SHHYVrdyyTBmAPWoQx1i3Bhxjp/C3+JJaAKcUlrNtdA=; b=rPxoe93Xif6PVOtwLYU6UEHg2hzcwwkfUev9Dib69ziqJAiqKNdmyS8UDXZA6GI4lr xpy1OCE2/v3oMxiccIIga9DBRe/icujn4oRJ12df/ntO+UeTPiXRoXePAiI4sz6vFHF3 icoSHVwaJWhANgxOrklgwoIIDCJwx7DHkUDomiL5/JWqtg23D/tAXqR1GsXn+2V5jQjY l8rAzxIMfvVOvh6J6rJ2WPsXSqiaIp15fFw3joNgk3uSrUcD/1f602THeCnImjyVfHIg PUGN/ATE/Tr2R6047SmtXnLfmMAM9A0zQSZCkKBZSgmQ4JV4csJ13O7CT2lwkOyqCZ4T S5GQ== X-Gm-Message-State: AOAM532xMxxoF8axxUieEKqQE4F+wIBxILodWzyM5BSsdp2jbEG0B35j 4UjCd4XjG5ENXeGXjpU6DvHj44dBZYHgHIwmIyRwaEY9ejYOMaDCxi5w9FOt32zZuxCZNdIMHn0 5URoOjm2PJj5zAJUx8Rpq++spkOcjnDZOVxeyrlZRbs+ydTStDAL0 X-Google-Smtp-Source: ABdhPJyF6vBmLlHbSbbCNEXLltu/N+fZx046fQsMK2Hg0xw70ZRUei0R5tJM1j5Q5bHf9BXwESAwM3Q= X-Received: from ruffy.mtv.corp.google.com ([2620:0:1000:1412:25d4:96ad:ca30:7913]) (user=dje job=sendgmr) by 2002:a17:90b:198d:: with SMTP id mv13mr5497249pjb.68.1612395349989; Wed, 03 Feb 2021 15:35:49 -0800 (PST) Date: Wed, 3 Feb 2021 15:35:39 -0800 In-Reply-To: <20210203233539.1990032-1-dje@google.com> Message-Id: <20210203233539.1990032-4-dje@google.com> Mime-Version: 1.0 References: <20210203233539.1990032-1-dje@google.com> X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 3/3] net: Add -ipv6-hostfwd option, ipv6_hostfwd_add/remove commands To: qemu-devel@nongnu.org Cc: Samuel Thibault , Doug Evans Received-SPF: pass client-ip=2607:f8b0:4864:20::649; envelope-from=3VTMbYAMKCjAPVQSaaSXQ.OaYcQYg-PQhQXZaZSZg.adS@flex--dje.bounces.google.com; helo=mail-pl1-x649.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Doug Evans X-Patchwork-Original-From: dje--- via From: "Zhijian Li (Fujitsu)\" via" These are identical to their ipv4 counterparts, but for ipv6. Signed-off-by: Doug Evans --- hmp-commands.hx | 32 +++++++++++ include/net/slirp.h | 2 + net/slirp.c | 128 +++++++++++++++++++++++++++++++++++++++++++- qapi/net.json | 4 ++ 4 files changed, 164 insertions(+), 2 deletions(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index d4001f9c5d..6a9ec0361d 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1392,6 +1392,38 @@ SRST Remove host-to-guest TCP or UDP redirection. ERST +#ifdef CONFIG_SLIRP + { + .name = "ipv6_hostfwd_add", + .args_type = "arg1:s,arg2:s?", + .params = "[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 +SRST +``ipv6_hostfwd_add`` + Redirect TCP6 or UDP6 connections from host to guest (requires -net user). + Note that IPV6 addresses must be wrapped in square brackets []. + E.g., write "[::1]" not "::1". +ERST + +#ifdef CONFIG_SLIRP + { + .name = "ipv6_hostfwd_remove", + .args_type = "arg1:s,arg2:s?", + .params = "[netdev_id] [tcp|udp]:[hostaddr6]:hostport", + .help = "remove host-to-guest TCP6 or UDP6 redirection", + .cmd = hmp_ipv6_hostfwd_remove, + }, +#endif +SRST +``ipv6_hostfwd_remove`` + Remove host-to-guest TCP6 or UDP6 redirection. + Note that IPV6 addresses must be wrapped in square brackets []. + E.g., write "[::1]" not "::1". +ERST + { .name = "balloon", .args_type = "value:M", diff --git a/include/net/slirp.h b/include/net/slirp.h index bad3e1e241..4796a5cd39 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 a21a313302..2a59c20286 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -70,6 +70,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; @@ -101,6 +102,8 @@ static QTAILQ_HEAD(, 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 @@ -586,6 +589,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; @@ -703,6 +710,58 @@ static const char *parse_in4_addr_port(const char *str, const char *kind, return p; } +/* + * Parse an ipv6 address/port of the form "addrport". + * "kind" is either "host" or "guest" and is included in error messages. + * An empty address means in6addr_any. + * Returns a pointer to the end of the parsed string on success, and stores + * the results in *addr, *port. + * Otherwise returns NULL and stores the error message in *errmsg, which must + * be freed by the caller. + */ +static const char *parse_in6_addr_port(const char *str, const char *kind, + int addr_sep, int port_sep, + struct in6_addr *addr, int *port, + char **errmsg) +{ + char buf[256]; + const char *p = str; + + if (*(p++) != '[') { + *errmsg = g_strdup_printf("IPv6 %s address must be enclosed" + " in square brackets", kind); + return NULL; + } + if (get_str_sep(buf, sizeof(buf), &p, ']') < 0) { + *errmsg = g_strdup_printf("IPv6 %s address must be enclosed" + " in square brackets", kind); + return NULL; + } + if (buf[0] == '\0') { + *addr = in6addr_any; + } else if (!inet_pton(AF_INET6, buf, addr)) { + *errmsg = g_strdup_printf("Bad %s address", kind); + return NULL; + } + + if (*p++ != addr_sep) { + *errmsg = g_strdup_printf("Missing %s address separator after IPv6 address", kind); + return NULL; + } + + if (get_str_sep(buf, sizeof(buf), &p, port_sep) < 0) { + *errmsg = g_strdup_printf("Missing %s port separator", kind); + return NULL; + } + if (qemu_strtoi(buf, NULL, 10, port) < 0 || + *port < 0 || *port > 65535) { + *errmsg = g_strdup_printf("Bad %s port", kind); + return NULL; + } + + return p; +} + static void hmp_hostfwd_remove_worker(Monitor *mon, const QDict *qdict, int family) { @@ -743,7 +802,12 @@ static void hmp_hostfwd_remove_worker(Monitor *mon, const QDict *qdict, } err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port); } else { - g_assert_not_reached(); + struct in6_addr host_addr; + if (parse_in6_addr_port(p, "host", ':', '\0', &host_addr, &host_port, + &errmsg) == NULL) { + 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, @@ -760,6 +824,11 @@ void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) hmp_hostfwd_remove_worker(mon, qdict, AF_INET); } +void hmp_ipv6_hostfwd_remove(Monitor *mon, const QDict *qdict) +{ + hmp_hostfwd_remove_worker(mon, qdict, AF_INET6); +} + static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) { struct in_addr host_addr = { .s_addr = INADDR_ANY }; @@ -807,6 +876,55 @@ 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; + int is_udp; + char *errmsg = NULL; + + memset(&guest_addr, 0, sizeof(guest_addr)); + g_assert(redir_str != NULL); + p = redir_str; + + p = parse_protocol(p, ':', &is_udp, &errmsg); + if (p == NULL) { + goto fail_syntax; + } + + p = parse_in6_addr_port(p, "host", ':', '-', &host_addr, &host_port, + &errmsg); + if (p == NULL) { + goto fail_syntax; + } + + if (parse_in6_addr_port(p, "guest", ':', '\0', &guest_addr, &guest_port, + &errmsg) == NULL) { + goto fail_syntax; + } + if (guest_port == 0) { + errmsg = g_strdup("Bad guest port"); + goto fail_syntax; + } + + if (slirp_add_ipv6_hostfwd(s->slirp, is_udp, host_addr, host_port, + guest_addr, guest_port) < 0) { + error_setg(errp, "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, + errmsg); + g_free(errmsg); + return -1; +} + static void hmp_hostfwd_add_worker(Monitor *mon, const QDict *qdict, int family) { const char *redir_str; @@ -830,7 +948,7 @@ static void hmp_hostfwd_add_worker(Monitor *mon, const QDict *qdict, int family) if (family == AF_INET) { rc = slirp_hostfwd(s, redir_str, &err); } else { - g_assert_not_reached(); + rc = slirp_ipv6_hostfwd(s, redir_str, &err); } if (rc < 0) { error_report_err(err); @@ -842,6 +960,11 @@ void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) hmp_hostfwd_add_worker(mon, qdict, AF_INET); } +void hmp_ipv6_hostfwd_add(Monitor *mon, const QDict *qdict) +{ + hmp_hostfwd_add_worker(mon, qdict, AF_INET6); +} + #ifndef _WIN32 /* automatic user mode samba server configuration */ @@ -1148,6 +1271,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 c31748c87f..443473107a 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -161,6 +161,9 @@ # @hostfwd: redirect incoming TCP or UDP host connections to guest # endpoints # +# @ipv6-hostfwd: redirect incoming IPV6 TCP or UDP host connections to +# guest endpoints (since 6.0) +# # @guestfwd: forward guest TCP connections # # @tftp-server-name: RFC2132 "TFTP server name" string (Since 3.1) @@ -189,6 +192,7 @@ '*smb': 'str', '*smbserver': 'str', '*hostfwd': ['String'], + '*ipv6-hostfwd': ['String'], '*guestfwd': ['String'], '*tftp-server-name': 'str' } }