From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 564B4A0471 for ; Wed, 17 Jul 2019 20:49:51 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 631E91BDF5; Wed, 17 Jul 2019 20:49:50 +0200 (CEST) Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by dpdk.org (Postfix) with ESMTP id 87AB11BDEA for ; Wed, 17 Jul 2019 20:49:49 +0200 (CEST) Received: by mail-pl1-f195.google.com with SMTP id i2so12455227plt.1 for ; Wed, 17 Jul 2019 11:49:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=ytxlBDwgESjloV2KtSIYF2HhCv5QzC4VcShXhokGOTY=; b=xP1G8yP14viHw6ZqY20UiwODatHhcAz4bJb1NBQ37Myie+ixN7imrBQggam/sYibCS 9ScQhQhxbCS0vYjTztsaxLYv96A9OaHm13Ah5oeH3XyBWMhVdp39L324u0++EP5Uzwyh SbmSZTiPiqnauoYs6+HCC9y0ZK0KP5H1fNt9mDgEQTRDqsnqQbpRYrnyKAsg71WWIgJu o3X35Nc/wfV78RxeIg2WoO6whJqFyT/83OlFWF/yInRElArspjnGus6IZY7VC8F4QFeF XJwwv4tflS7Lva2VtWQgInc/3fswp038ladttMHX1T7lXEjDhhgLs+nlyU89vQ8My7rP 39Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ytxlBDwgESjloV2KtSIYF2HhCv5QzC4VcShXhokGOTY=; b=d5MaWK6X3Y1z6nTCM9jwGauq83j/WsqgbojZ16uMU5F01Z0q7yrTZ9xEMGNTuBIin/ 6UoSiPgmxteqd6mAswoAg/cf1hD8TmIjAiqMBsbZa4hLjEWEzt2yrf78Y4VMI9uGV/6c rQmrl8kOik0rWu1MYIAbzqwfH8HHQOVElKQ0O7h/dn0QqTJpHrk2rShFp+xzEtC/OKNs GhsJkuI1nnq0leU4Kgp+6XSmtIfEIRZbaAGaKBc7ln/yuMqY7fbIzJhdCdBeXntkakbT uOJMP9l9x2QoD8piK4Hm88dxHA83gXxAhfgv//a/kbBSyjv5Yty4lC1bria7ViY+7yqz Nzjg== X-Gm-Message-State: APjAAAWYGwonz5B/zNz+N5H3mFFkEJSrxq6I41tvxEshMXDcA99OqvFk dhaJlP6oG31iR001Cuv5h0ciJHEA X-Google-Smtp-Source: APXvYqzOMN53gOfzQaZMKNNWi5sbDca8uZ6wC1x/J6NSukK4FDLtzwopfK5p8pSW8hDITITZssGCpQ== X-Received: by 2002:a17:902:b28b:: with SMTP id u11mr43153029plr.11.1563389388311; Wed, 17 Jul 2019 11:49:48 -0700 (PDT) Received: from localhost.localdomain ([67.23.203.6]) by smtp.gmail.com with ESMTPSA id 81sm40241352pfx.111.2019.07.17.11.49.47 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 17 Jul 2019 11:49:47 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Wed, 17 Jul 2019 11:49:45 -0700 Message-Id: <20190717184945.4025-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [RFC] net: be more restrictive in ether_unformat_addr X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" The current code acts more like BSD ether_aton and allows leading zeros which breaks the cmdline tests. Change the code to be more restrictive and only allow the fully expanded standard formats. Fixes: 596d31092d32 ("net: add function to convert string to ethernet address") Signed-off-by: Stephen Hemminger --- lib/librte_net/rte_ether.c | 107 ++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 37 deletions(-) diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c index 8d040173cfc6..536449beffe4 100644 --- a/lib/librte_net/rte_ether.c +++ b/lib/librte_net/rte_ether.c @@ -2,6 +2,8 @@ * Copyright(c) 2010-2014 Intel Corporation */ +#include + #include #include @@ -29,50 +31,81 @@ rte_ether_format_addr(char *buf, uint16_t size, eth_addr->addr_bytes[5]); } +static int8_t get_xdigit(char ch) +{ + if (ch >= '0' && ch <= '9') + return ch - '0'; + if (ch >= 'a' && ch <= 'f') + return ch - 'a' + 10; + if (ch >= 'A' && ch <= 'F') + return ch - 'A' + 10; + return -1; +} + +/* Convert 00:11:22:33:44:55 to ethernet address */ +static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea) +{ + const char *s = s0; + int i; + + for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) { + int8_t x; + + x = get_xdigit(*s++); + if (x < 0) + return false; + ea->addr_bytes[i] = x << 4; + x = get_xdigit(*s++); + if (x < 0) + return false; + ea->addr_bytes[i] |= x; + + if (i < RTE_ETHER_ADDR_LEN - 1 && + *s++ != ':') + return false; + } + return *s == '\0'; +} + +/* Convert 0011:2233:4455 to ethernet address */ +static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea) +{ + int i, j; + + for (i = 0; i < RTE_ETHER_ADDR_LEN; i += 2) { + uint16_t w = 0; + + for (j = 0; j < 4; j++) { + int8_t x; + + x = get_xdigit(*s++); + if (x < 0) + return false; + w = (w << 4) | x; + } + ea->addr_bytes[i] = w >> 8; + ea->addr_bytes[i+1] = w & 0xff; + + if (i < RTE_ETHER_ADDR_LEN - 2 && + *s++ != ':') + return false; + } + + return *s == '\0'; +} + /* * Like ether_aton_r but can handle either * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX + * and is more restrictive. */ int rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea) { - unsigned int o0, o1, o2, o3, o4, o5; - int n; - - n = sscanf(s, "%x:%x:%x:%x:%x:%x", - &o0, &o1, &o2, &o3, &o4, &o5); - - if (n == 6) { - /* Standard format XX:XX:XX:XX:XX:XX */ - if (o0 > UINT8_MAX || o1 > UINT8_MAX || o2 > UINT8_MAX || - o3 > UINT8_MAX || o4 > UINT8_MAX || o5 > UINT8_MAX) { - rte_errno = ERANGE; - return -1; - } - - ea->addr_bytes[0] = o0; - ea->addr_bytes[1] = o1; - ea->addr_bytes[2] = o2; - ea->addr_bytes[3] = o3; - ea->addr_bytes[4] = o4; - ea->addr_bytes[5] = o5; - } else if (n == 3) { - /* Support the format XXXX:XXXX:XXXX */ - if (o0 > UINT16_MAX || o1 > UINT16_MAX || o2 > UINT16_MAX) { - rte_errno = ERANGE; - return -1; - } - - ea->addr_bytes[0] = o0 >> 8; - ea->addr_bytes[1] = o0 & 0xff; - ea->addr_bytes[2] = o1 >> 8; - ea->addr_bytes[3] = o1 & 0xff; - ea->addr_bytes[4] = o2 >> 8; - ea->addr_bytes[5] = o2 & 0xff; - } else { - /* unknown format */ - rte_errno = EINVAL; + if (get_ether_addr6(s, ea) || get_ether_addr3(s, ea)) + return 0; + else { + rte_errno = -EINVAL; return -1; } - return 0; } -- 2.17.1