The IPv4 parsing helper used by pktgen and pktchkr read each octet with
"%u", which accepts values above 255 from the configuration file and
encodes them into unintended device register values.

Replace the hand-rolled parser in both modules with inet_pton(), which
validates the dotted-quad format and the octet range, and matches the
IPv4 parsing already used by other DPDK drivers. For valid input the
returned value is byte-order identical to the previous helper, so the
register contents are unchanged.

Fixes: 9c7188a68d7b ("net/ark: provide API for hardware modules pktchkr and 
pktgen")
Cc: [email protected]

Signed-off-by: Denis Sergeev <[email protected]>
---
v2:
 - Replace the hand-rolled parser with inet_pton() in both modules
   instead of adding explicit octet range checks.

 drivers/net/ark/ark_pktchkr.c | 9 +++++----
 drivers/net/ark/ark_pktgen.c  | 9 +++++----
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ark/ark_pktchkr.c b/drivers/net/ark/ark_pktchkr.c
index e1f336c73c..67405ac18a 100644
--- a/drivers/net/ark/ark_pktchkr.c
+++ b/drivers/net/ark/ark_pktchkr.c
@@ -4,9 +4,11 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <arpa/inet.h>
 
 #include <rte_string_fns.h>
 #include <rte_malloc.h>
+#include <rte_byteorder.h>
 
 #include "ark_pktchkr.h"
 #include "ark_logs.h"
@@ -374,12 +376,11 @@ static int32_t parse_ipv4_string(char const *ip_address);
 static int32_t
 parse_ipv4_string(char const *ip_address)
 {
-       unsigned int ip[4];
+       struct in_addr addr;
 
-       if (sscanf(ip_address, "%u.%u.%u.%u",
-                  &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
+       if (inet_pton(AF_INET, ip_address, &addr) != 1)
                return 0;
-       return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
+       return rte_be_to_cpu_32(addr.s_addr);
 }
 
 void
diff --git a/drivers/net/ark/ark_pktgen.c b/drivers/net/ark/ark_pktgen.c
index 69ff7072b2..05d5773d44 100644
--- a/drivers/net/ark/ark_pktgen.c
+++ b/drivers/net/ark/ark_pktgen.c
@@ -4,9 +4,11 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <arpa/inet.h>
 
 #include <rte_string_fns.h>
 #include <rte_malloc.h>
+#include <rte_byteorder.h>
 #include <rte_thread.h>
 
 #include "ark_pktgen.h"
@@ -355,12 +357,11 @@ static int32_t parse_ipv4_string(char const *ip_address);
 static int32_t
 parse_ipv4_string(char const *ip_address)
 {
-       unsigned int ip[4];
+       struct in_addr addr;
 
-       if (sscanf(ip_address, "%u.%u.%u.%u",
-                  &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
+       if (inet_pton(AF_INET, ip_address, &addr) != 1)
                return 0;
-       return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
+       return rte_be_to_cpu_32(addr.s_addr);
 }
 
 static void
-- 
2.50.1

Reply via email to