From: Ido Schimmel <idosch@nvidia.com>
To: <netdev@vger.kernel.org>
Cc: <stephen@networkplumber.org>, <dsahern@gmail.com>,
<gnault@redhat.com>, <petrm@nvidia.com>,
Ido Schimmel <idosch@nvidia.com>
Subject: [PATCH iproute2-next 2/5] iprule: Move port parsing to a function
Date: Mon, 24 Feb 2025 08:52:37 +0200 [thread overview]
Message-ID: <20250224065241.236141-3-idosch@nvidia.com> (raw)
In-Reply-To: <20250224065241.236141-1-idosch@nvidia.com>
In preparation for adding port mask support, move port parsing to a
function.
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
---
ip/iprule.c | 57 +++++++++++++++++++++++++----------------------------
1 file changed, 27 insertions(+), 30 deletions(-)
diff --git a/ip/iprule.c b/ip/iprule.c
index ea30d418712c..61e092bc5693 100644
--- a/ip/iprule.c
+++ b/ip/iprule.c
@@ -600,6 +600,29 @@ static int flush_rule(struct nlmsghdr *n, void *arg)
return 0;
}
+static void iprule_port_parse(char *arg, struct fib_rule_port_range *r)
+{
+ char *sep;
+
+ sep = strchr(arg, '-');
+ if (sep) {
+ *sep = '\0';
+
+ if (get_u16(&r->start, arg, 10))
+ invarg("invalid port range start", arg);
+
+ if (get_u16(&r->end, sep + 1, 10))
+ invarg("invalid port range end", sep + 1);
+
+ return;
+ }
+
+ if (get_u16(&r->start, arg, 10))
+ invarg("invalid port", arg);
+
+ r->end = r->start;
+}
+
static void iprule_flowlabel_parse(char *arg, __u32 *flowlabel,
__u32 *flowlabel_mask)
{
@@ -746,27 +769,11 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
invarg("Invalid \"ipproto\" value\n", *argv);
filter.ipproto = ipproto;
} else if (strcmp(*argv, "sport") == 0) {
- struct fib_rule_port_range r;
- int ret;
-
NEXT_ARG();
- ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
- if (ret == 1)
- r.end = r.start;
- else if (ret != 2)
- invarg("invalid port range\n", *argv);
- filter.sport = r;
+ iprule_port_parse(*argv, &filter.sport);
} else if (strcmp(*argv, "dport") == 0) {
- struct fib_rule_port_range r;
- int ret;
-
NEXT_ARG();
- ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
- if (ret == 1)
- r.end = r.start;
- else if (ret != 2)
- invarg("invalid dport range\n", *argv);
- filter.dport = r;
+ iprule_port_parse(*argv, &filter.dport);
} else if (strcmp(*argv, "dscp") == 0) {
__u32 dscp;
@@ -1036,26 +1043,16 @@ static int iprule_modify(int cmd, int argc, char **argv)
addattr8(&req.n, sizeof(req), FRA_IP_PROTO, ipproto);
} else if (strcmp(*argv, "sport") == 0) {
struct fib_rule_port_range r;
- int ret = 0;
NEXT_ARG();
- ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
- if (ret == 1)
- r.end = r.start;
- else if (ret != 2)
- invarg("invalid port range\n", *argv);
+ iprule_port_parse(*argv, &r);
addattr_l(&req.n, sizeof(req), FRA_SPORT_RANGE, &r,
sizeof(r));
} else if (strcmp(*argv, "dport") == 0) {
struct fib_rule_port_range r;
- int ret = 0;
NEXT_ARG();
- ret = sscanf(*argv, "%hu-%hu", &r.start, &r.end);
- if (ret == 1)
- r.end = r.start;
- else if (ret != 2)
- invarg("invalid dport range\n", *argv);
+ iprule_port_parse(*argv, &r);
addattr_l(&req.n, sizeof(req), FRA_DPORT_RANGE, &r,
sizeof(r));
} else if (strcmp(*argv, "dscp") == 0) {
--
2.48.1
next prev parent reply other threads:[~2025-02-24 6:53 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-24 6:52 [PATCH iproute2-next 0/5] iprule: Add mask support for L4 ports and DSCP Ido Schimmel
2025-02-24 6:52 ` [PATCH iproute2-next 1/5] Sync uAPI headers Ido Schimmel
2025-02-24 6:52 ` Ido Schimmel [this message]
2025-02-24 14:47 ` [PATCH iproute2-next 2/5] iprule: Move port parsing to a function Petr Machata
2025-02-24 6:52 ` [PATCH iproute2-next 3/5] iprule: Allow specifying ports in hexadecimal notation Ido Schimmel
2025-02-24 14:47 ` Petr Machata
2025-02-24 6:52 ` [PATCH iproute2-next 4/5] iprule: Add port mask support Ido Schimmel
2025-02-24 14:03 ` Petr Machata
2025-02-24 16:17 ` Ido Schimmel
2025-02-24 6:52 ` [PATCH iproute2-next 5/5] iprule: Add DSCP " Ido Schimmel
2025-02-24 14:35 ` Petr Machata
2025-02-24 16:27 ` Ido Schimmel
2025-02-25 8:58 ` Petr Machata
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250224065241.236141-3-idosch@nvidia.com \
--to=idosch@nvidia.com \
--cc=dsahern@gmail.com \
--cc=gnault@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=petrm@nvidia.com \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).