From 8e2f2b0dee138221e111571713b384a99a4d2aea Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 27 Nov 2023 15:59:11 +0100 Subject: [PATCH] xfrm: refactor nl_addr_build() calls in XFRM code Use _nl_addr_build() helper. No need for all this redundant code. --- lib/xfrm/sa.c | 38 ++++++++------------------------------ lib/xfrm/sp.c | 20 ++++---------------- 2 files changed, 12 insertions(+), 46 deletions(-) diff --git a/lib/xfrm/sa.c b/lib/xfrm/sa.c index 3655a5e6..c0307235 100644 --- a/lib/xfrm/sa.c +++ b/lib/xfrm/sa.c @@ -806,18 +806,12 @@ int xfrmnl_sa_parse(struct nlmsghdr *n, struct xfrmnl_sa **result) if (err < 0) goto errout; - if (sa_info->sel.family == AF_INET) - addr1 = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a4, sizeof (sa_info->sel.daddr.a4)); - else - addr1 = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a6, sizeof (sa_info->sel.daddr.a6)); + addr1 = _nl_addr_build(sa_info->sel.family, &sa_info->sel.daddr); nl_addr_set_prefixlen (addr1, sa_info->sel.prefixlen_d); xfrmnl_sel_set_daddr (sa->sel, addr1); xfrmnl_sel_set_prefixlen_d (sa->sel, sa_info->sel.prefixlen_d); - if (sa_info->sel.family == AF_INET) - addr2 = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a4, sizeof (sa_info->sel.saddr.a4)); - else - addr2 = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a6, sizeof (sa_info->sel.saddr.a6)); + addr2 = _nl_addr_build(sa_info->sel.family, &sa_info->sel.saddr); nl_addr_set_prefixlen (addr2, sa_info->sel.prefixlen_s); xfrmnl_sel_set_saddr (sa->sel, addr2); xfrmnl_sel_set_prefixlen_s (sa->sel, sa_info->sel.prefixlen_s); @@ -832,18 +826,12 @@ int xfrmnl_sa_parse(struct nlmsghdr *n, struct xfrmnl_sa **result) xfrmnl_sel_set_userid (sa->sel, sa_info->sel.user); sa->ce_mask |= XFRM_SA_ATTR_SEL; - if (sa_info->family == AF_INET) - sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a4, sizeof (sa_info->id.daddr.a4)); - else - sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a6, sizeof (sa_info->id.daddr.a6)); + sa->id.daddr = _nl_addr_build(sa_info->family, &sa_info->id.daddr); sa->id.spi = ntohl(sa_info->id.spi); sa->id.proto = sa_info->id.proto; sa->ce_mask |= (XFRM_SA_ATTR_DADDR | XFRM_SA_ATTR_SPI | XFRM_SA_ATTR_PROTO); - if (sa_info->family == AF_INET) - sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a4, sizeof (sa_info->saddr.a4)); - else - sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a6, sizeof (sa_info->saddr.a6)); + sa->saddr = _nl_addr_build(sa_info->family, &sa_info->saddr); sa->ce_mask |= XFRM_SA_ATTR_SADDR; sa->lft->soft_byte_limit = sa_info->lft.soft_byte_limit; @@ -950,10 +938,8 @@ int xfrmnl_sa_parse(struct nlmsghdr *n, struct xfrmnl_sa **result) sa->encap->encap_type = encap->encap_type; sa->encap->encap_sport = ntohs(encap->encap_sport); sa->encap->encap_dport = ntohs(encap->encap_dport); - if (sa_info->family == AF_INET) - sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a4, sizeof (encap->encap_oa.a4)); - else - sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a6, sizeof (encap->encap_oa.a6)); + sa->encap->encap_oa = + _nl_addr_build(sa_info->family, &encap->encap_oa); sa->ce_mask |= XFRM_SA_ATTR_ENCAP; } @@ -963,16 +949,8 @@ int xfrmnl_sa_parse(struct nlmsghdr *n, struct xfrmnl_sa **result) } if (tb[XFRMA_COADDR]) { - if (sa_info->family == AF_INET) - { - sa->coaddr = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]), - sizeof (uint32_t)); - } - else - { - sa->coaddr = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]), - sizeof (uint32_t) * 4); - } + sa->coaddr = _nl_addr_build(sa_info->family, + nla_data(tb[XFRMA_COADDR])); sa->ce_mask |= XFRM_SA_ATTR_COADDR; } diff --git a/lib/xfrm/sp.c b/lib/xfrm/sp.c index 6c2a8f4c..3b0d0b87 100644 --- a/lib/xfrm/sp.c +++ b/lib/xfrm/sp.c @@ -592,18 +592,12 @@ int xfrmnl_sp_parse(struct nlmsghdr *n, struct xfrmnl_sp **result) goto errout; } - if (sp_info->sel.family == AF_INET) - addr1 = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a4, sizeof (sp_info->sel.daddr.a4)); - else - addr1 = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a6, sizeof (sp_info->sel.daddr.a6)); + addr1 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.daddr); nl_addr_set_prefixlen (addr1, sp_info->sel.prefixlen_d); xfrmnl_sel_set_daddr (sp->sel, addr1); xfrmnl_sel_set_prefixlen_d (sp->sel, sp_info->sel.prefixlen_d); - if (sp_info->sel.family == AF_INET) - addr2 = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a4, sizeof (sp_info->sel.saddr.a4)); - else - addr2 = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a6, sizeof (sp_info->sel.saddr.a6)); + addr2 = _nl_addr_build(sp_info->sel.family, &sp_info->sel.saddr); nl_addr_set_prefixlen (addr2, sp_info->sel.prefixlen_s); xfrmnl_sel_set_saddr (sp->sel, addr2); xfrmnl_sel_set_prefixlen_s (sp->sel, sp_info->sel.prefixlen_s); @@ -679,19 +673,13 @@ int xfrmnl_sp_parse(struct nlmsghdr *n, struct xfrmnl_sp **result) goto errout; } - if (tmpl->family == AF_INET) - addr1 = nl_addr_build(tmpl->family, &tmpl->id.daddr.a4, sizeof (tmpl->id.daddr.a4)); - else - addr1 = nl_addr_build(tmpl->family, &tmpl->id.daddr.a6, sizeof (tmpl->id.daddr.a6)); + addr1 = _nl_addr_build(tmpl->family, &tmpl->id.daddr); xfrmnl_user_tmpl_set_daddr (sputmpl, addr1); xfrmnl_user_tmpl_set_spi (sputmpl, ntohl(tmpl->id.spi)); xfrmnl_user_tmpl_set_proto (sputmpl, tmpl->id.proto); xfrmnl_user_tmpl_set_family (sputmpl, tmpl->family); - if (tmpl->family == AF_INET) - addr2 = nl_addr_build(tmpl->family, &tmpl->saddr.a4, sizeof (tmpl->saddr.a4)); - else - addr2 = nl_addr_build(tmpl->family, &tmpl->saddr.a6, sizeof (tmpl->saddr.a6)); + addr2 = _nl_addr_build(tmpl->family, &tmpl->saddr); xfrmnl_user_tmpl_set_saddr (sputmpl, addr2); xfrmnl_user_tmpl_set_reqid (sputmpl, tmpl->reqid);