Skip to content

Commit

Permalink
xfrm: erge branch 'th/xfrm-addr-cleanup'
Browse files Browse the repository at this point in the history
  • Loading branch information
thom311 committed Nov 29, 2023
2 parents cd4016b + 2f485cc commit 30d6e63
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 164 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -315,6 +315,7 @@ noinst_HEADERS = \
include/linux-private/linux/xfrm.h \
include/nl-aux-core/nl-core.h \
include/nl-aux-route/nl-route.h \
include/nl-aux-xfrm/nl-xfrm.h \
include/nl-default.h \
include/nl-priv-dynamic-core/cache-api.h \
include/nl-priv-dynamic-core/nl-core.h \
Expand Down
11 changes: 11 additions & 0 deletions include/base/nl-base-utils.h
Expand Up @@ -663,6 +663,17 @@ static inline void *_nl_memdup(const void *ptr, size_t len)

/*****************************************************************************/

static inline size_t _nl_addr_family_to_size(int addr_family)
{
if (addr_family == AF_INET)
return sizeof(in_addr_t);
if (addr_family == AF_INET6)
return sizeof(struct in6_addr);
return 0;
}

/*****************************************************************************/

typedef union {
in_addr_t addr4;
struct in_addr a4;
Expand Down
7 changes: 7 additions & 0 deletions include/nl-aux-core/nl-core.h
Expand Up @@ -49,4 +49,11 @@ void nl_socket_free(struct nl_sock *);
_NL_AUTO_DEFINE_FCN_TYPED0(struct nl_sock *, _nl_auto_nl_socket_fcn,
nl_socket_free);

struct nl_addr *nl_addr_build(int, const void *, size_t);

static inline struct nl_addr *_nl_addr_build(int family, const void *buf)
{
return nl_addr_build(family, buf, _nl_addr_family_to_size(family));
}

#endif /* NETLINK_NL_AUTO_H_ */
18 changes: 18 additions & 0 deletions include/nl-aux-xfrm/README.md
@@ -0,0 +1,18 @@
include/nl-aux-xfrm
===================

This contains private/internal helpers that depend on the public libnl-3 (core)
and libnl-xfrm-3.

Itself, it must only rely on C, include/base/ and public headers of libnl-3 (core)
and libnl-xfrm-3.

They can be used by all internal code that uses the public API of both libnl-3 (core)
and libnl-xfrm-3.

It can also be used by lib/xfrm itself (that is, the implementation of
libnl-xfrm-3).

It must not be used in public headers, it's internal only.

Currently this is header-only, it does not require any additional linking.
26 changes: 26 additions & 0 deletions include/nl-aux-xfrm/nl-xfrm.h
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: LGPL-2.1-only */

#ifndef __NETLINK_NL_AUX_XFRM_NL_XFRM_H__
#define __NETLINK_NL_AUX_XFRM_NL_XFRM_H__

#include "base/nl-base-utils.h"

struct xfrmnl_sp;
void xfrmnl_sp_put(struct xfrmnl_sp *sp);
#define _nl_auto_xfrmnl_sp _nl_auto(_nl_auto_xfrmnl_sp_fcn)
_NL_AUTO_DEFINE_FCN_TYPED0(struct xfrmnl_sp *, _nl_auto_xfrmnl_sp_fcn,
xfrmnl_sp_put);

struct xfrmnl_sa;
void xfrmnl_sa_put(struct xfrmnl_sa *sa);
#define _nl_auto_xfrmnl_sa _nl_auto(_nl_auto_xfrmnl_sa_fcn)
_NL_AUTO_DEFINE_FCN_TYPED0(struct xfrmnl_sa *, _nl_auto_xfrmnl_sa_fcn,
xfrmnl_sa_put);

struct xfrmnl_ae;
void xfrmnl_ae_put(struct xfrmnl_ae *ae);
#define _nl_auto_xfrmnl_ae _nl_auto(_nl_auto_xfrmnl_ae_fcn)
_NL_AUTO_DEFINE_FCN_TYPED0(struct xfrmnl_ae *, _nl_auto_xfrmnl_ae_fcn,
xfrmnl_ae_put);

#endif /* __NETLINK_NL_AUX_XFRM_NL_XFRM_H__ */
32 changes: 15 additions & 17 deletions lib/xfrm/ae.c
Expand Up @@ -133,6 +133,8 @@
#include "nl-priv-dynamic-core/object-api.h"
#include "nl-priv-dynamic-core/nl-core.h"
#include "nl-priv-dynamic-core/cache-api.h"
#include "nl-aux-core/nl-core.h"
#include "nl-aux-xfrm/nl-xfrm.h"

/** @cond SKIP */

Expand Down Expand Up @@ -522,29 +524,30 @@ static struct nla_policy xfrm_ae_policy[XFRMA_MAX+1] = {

int xfrmnl_ae_parse(struct nlmsghdr *n, struct xfrmnl_ae **result)
{
struct xfrmnl_ae* ae;
_nl_auto_xfrmnl_ae struct xfrmnl_ae *ae = NULL;
struct nlattr *tb[XFRMA_MAX + 1];
struct xfrm_aevent_id* ae_id;
int err;

ae = xfrmnl_ae_alloc();
if (!ae) {
err = -NLE_NOMEM;
goto errout;
}
if (!ae)
return -NLE_NOMEM;

ae->ce_msgtype = n->nlmsg_type;
ae_id = nlmsg_data(n);

err = nlmsg_parse(n, sizeof(struct xfrm_aevent_id), tb, XFRMA_MAX, xfrm_ae_policy);
if (err < 0)
goto errout;
return err;

ae->sa_id.daddr = nl_addr_build(ae_id->sa_id.family, &ae_id->sa_id.daddr, sizeof (ae_id->sa_id.daddr));
if (!(ae->sa_id.daddr =
_nl_addr_build(ae_id->sa_id.family, &ae_id->sa_id.daddr)))
return -NLE_NOMEM;
ae->sa_id.family= ae_id->sa_id.family;
ae->sa_id.spi = ntohl(ae_id->sa_id.spi);
ae->sa_id.proto = ae_id->sa_id.proto;
ae->saddr = nl_addr_build(ae_id->sa_id.family, &ae_id->saddr, sizeof (ae_id->saddr));
if (!(ae->saddr = _nl_addr_build(ae_id->sa_id.family, &ae_id->saddr)))
return -NLE_NOMEM;
ae->reqid = ae_id->reqid;
ae->flags = ae_id->flags;
ae->ce_mask |= (XFRM_AE_ATTR_DADDR | XFRM_AE_ATTR_FAMILY | XFRM_AE_ATTR_SPI |
Expand All @@ -560,6 +563,7 @@ int xfrmnl_ae_parse(struct nlmsghdr *n, struct xfrmnl_ae **result)

if (tb[XFRMA_LTIME_VAL]) {
struct xfrm_lifetime_cur* cur = nla_data(tb[XFRMA_LTIME_VAL]);

ae->lifetime_cur.bytes = cur->bytes;
ae->lifetime_cur.packets = cur->packets;
ae->lifetime_cur.add_time = cur->add_time;
Expand All @@ -581,10 +585,8 @@ int xfrmnl_ae_parse(struct nlmsghdr *n, struct xfrmnl_ae **result)
struct xfrm_replay_state_esn* esn = nla_data (tb[XFRMA_REPLAY_ESN_VAL]);
uint32_t len = sizeof (struct xfrmnl_replay_state_esn) + (sizeof (uint32_t) * esn->bmp_len);

if ((ae->replay_state_esn = calloc (1, len)) == NULL) {
err = -ENOMEM;
goto errout;
}
if ((ae->replay_state_esn = calloc (1, len)) == NULL)
return -NLE_NOMEM;
ae->replay_state_esn->oseq = esn->oseq;
ae->replay_state_esn->seq = esn->seq;
ae->replay_state_esn->oseq_hi = esn->oseq_hi;
Expand All @@ -605,12 +607,8 @@ int xfrmnl_ae_parse(struct nlmsghdr *n, struct xfrmnl_ae **result)
ae->replay_state_esn = NULL;
}

*result = ae;
*result = _nl_steal_pointer(&ae);
return 0;

errout:
xfrmnl_ae_put(ae);
return err;
}

static int xfrm_ae_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
Expand Down

0 comments on commit 30d6e63

Please sign in to comment.