diff mbox series

[iproute2-next,2/6] bridge: mdb: Split source parsing to a separate function

Message ID 20221215175230.1907938-3-idosch@nvidia.com (mailing list archive)
State Accepted
Commit c305c76a01e1c81d8f04582dfce2fbc9194b9801
Delegated to: David Ahern
Headers show
Series bridge: mdb: Add support for new attributes | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Ido Schimmel Dec. 15, 2022, 5:52 p.m. UTC
Currently, the only attribute inside the 'MDBA_SET_ENTRY_ATTRS' nest is
'MDBE_ATTR_SOURCE', but subsequent patches are going to add more
attributes to the nest.

Prepare for the addition of these attributes by splitting the parsing of
individual attributes inside the nest to separate functions.

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
---
 bridge/mdb.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

Comments

Nikolay Aleksandrov Dec. 16, 2022, 11:59 a.m. UTC | #1
On 15/12/2022 19:52, Ido Schimmel wrote:
> Currently, the only attribute inside the 'MDBA_SET_ENTRY_ATTRS' nest is
> 'MDBE_ATTR_SOURCE', but subsequent patches are going to add more
> attributes to the nest.
> 
> Prepare for the addition of these attributes by splitting the parsing of
> individual attributes inside the nest to separate functions.
> 
> Signed-off-by: Ido Schimmel <idosch@nvidia.com>
> ---
>  bridge/mdb.c | 34 ++++++++++++++++++++++++----------
>  1 file changed, 24 insertions(+), 10 deletions(-)
> 

Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
diff mbox series

Patch

diff --git a/bridge/mdb.c b/bridge/mdb.c
index 4ae91f15dac8..64db2ee03c42 100644
--- a/bridge/mdb.c
+++ b/bridge/mdb.c
@@ -474,6 +474,25 @@  static int mdb_parse_grp(const char *grp, struct br_mdb_entry *e)
 	return -1;
 }
 
+static int mdb_parse_src(struct nlmsghdr *n, int maxlen, const char *src)
+{
+	struct in6_addr src_ip6;
+	__be32 src_ip4;
+
+	if (inet_pton(AF_INET, src, &src_ip4)) {
+		addattr32(n, maxlen, MDBE_ATTR_SOURCE, src_ip4);
+		return 0;
+	}
+
+	if (inet_pton(AF_INET6, src, &src_ip6)) {
+		addattr_l(n, maxlen, MDBE_ATTR_SOURCE, &src_ip6,
+			  sizeof(src_ip6));
+		return 0;
+	}
+
+	return -1;
+}
+
 static int mdb_modify(int cmd, int flags, int argc, char **argv)
 {
 	struct {
@@ -543,19 +562,14 @@  static int mdb_modify(int cmd, int flags, int argc, char **argv)
 	if (set_attrs) {
 		struct rtattr *nest = addattr_nest(&req.n, sizeof(req),
 						   MDBA_SET_ENTRY_ATTRS);
-		struct in6_addr src_ip6;
-		__be32 src_ip4;
 
 		nest->rta_type |= NLA_F_NESTED;
-		if (!inet_pton(AF_INET, src, &src_ip4)) {
-			if (!inet_pton(AF_INET6, src, &src_ip6)) {
-				fprintf(stderr, "Invalid source address \"%s\"\n", src);
-				return -1;
-			}
-			addattr_l(&req.n, sizeof(req), MDBE_ATTR_SOURCE, &src_ip6, sizeof(src_ip6));
-		} else {
-			addattr32(&req.n, sizeof(req), MDBE_ATTR_SOURCE, src_ip4);
+
+		if (src && mdb_parse_src(&req.n, sizeof(req), src)) {
+			fprintf(stderr, "Invalid source address \"%s\"\n", src);
+			return -1;
 		}
+
 		addattr_nest_end(&req.n, nest);
 	}