Skip to content

Commit

Permalink
xfrm: refactor nl_addr_build() calls in XFRM code
Browse files Browse the repository at this point in the history
Use _nl_addr_build() helper. No need for all this redundant code.
  • Loading branch information
thom311 committed Nov 28, 2023
1 parent cbe2a77 commit 8e2f2b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 46 deletions.
38 changes: 8 additions & 30 deletions lib/xfrm/sa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
20 changes: 4 additions & 16 deletions lib/xfrm/sp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8e2f2b0

Please sign in to comment.