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]>
---
v3:
  - include <sys/socket.h> for AF_INET.

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 | 10 ++++++----
 drivers/net/ark/ark_pktgen.c  | 10 ++++++----
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ark/ark_pktchkr.c b/drivers/net/ark/ark_pktchkr.c
index e1f336c73c..e1a2943957 100644
--- a/drivers/net/ark/ark_pktchkr.c
+++ b/drivers/net/ark/ark_pktchkr.c
@@ -4,9 +4,12 @@

 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/socket.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 +377,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..17ac33fce6 100644
--- a/drivers/net/ark/ark_pktgen.c
+++ b/drivers/net/ark/ark_pktgen.c
@@ -4,9 +4,12 @@

 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/socket.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 +358,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