From: "cron2 (Code Review)" cron2 submitted this change.dco_linux: rearrange functions
In prep=
aration for the implementation of a generic netlink
message parser, move=
all parsing functions above ovpn_handle_msg().
The latter is soon g=
oing to become a generic message parser which
will invoke specific handl=
ers, thus they are required to be defined
earlier in the file.
No=
functional change is intended.
This patch is only meant to reduce e=
ntropy in the patch which will do
the real netlink parser change.
Better reviewed with: git show --color-moved
Change-Id: I94004579ae=
f4a1ccccdbcf8edd7b722e5a611c72
Signed-off-by: Antonio Quartulli <anto=
nio@...2726...>
Acked-by: Gert Doering <gert@...1296...>
Mes=
sage-Id: <20250723060747.19524-1-gert@...1296...>
URL: https://www=
.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32263.html
Sign=
ed-off-by: Gert Doering <gert@...1296...>
---
M src/openvpn/dco=
_linux.c
1 file changed, 144 insertions(+), 144 deletions(-)
index a04a164..3652a49 100644
--- a/src/openvpn/dco_=
linux.c
+++ b/src/openvpn/dco_linux.c
@@ -8=
06,6 +806,150 @@
return false;
}
+/* lib=
nl < 3.11.0 does not implement nla_get_uint() */
+static uint64_t
+ovpn_nla_get_uint(struct nlattr *attr)=
+{
+ if (nla_len(attr) =3D=3D sizeof(uint32_t=
))
+ {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ return nla_get_u32(attr=
);
+ }
<=
span style=3D"color: hsl(120, 100%, 40%);">+ else
+ {
+ return nla_get_u64(attr);
+ }
+}
+
+static void
+dco_update_peer_stat(stru=
ct context_2 *c2, struct nlattr *tb[], uint32_t id)
+{
+ if (tb[OVPN_A_PEER_LINK_RX_BYTES])
+ {
+ c2->dco_read_bytes =3D ovpn_nla_get_uint(tb=
[OVPN_A_PEER_LINK_RX_BYTES]);
+ msg(D_DCO_DEBUG, "%s / dco_read_bytes: " counte=
r_format, __func__,
+=
c2->dco_read_bytes);
+ }
+ else
+ {
+ msg(M_WARN, &qu=
ot;%s: no link RX bytes provided in reply for peer %u",
+
+ if (tb[OVPN_A_PEER_LINK_TX_BYTES])
+ {
+ c2->dco_write_bytes =3D ovpn_nla_get_uint(tb=
[OVPN_A_PEER_LINK_TX_BYTES]);
+ msg(D_DCO_DEBUG, "%s / dco_write_bytes: " count=
er_format, __func__,
=
+ c2->dco_write_bytes);
+ }
+ else
+ {=
span>
+ msg(M_WARN, &=
quot;%s: no link TX bytes provided in reply for peer %u",+ __func__, id);
+ }
+
+ if (tb[OVPN_A_PEER_VPN_RX_BYTES])
+ {
+ c2->tun_read_bytes =3D ovpn_nla_get_uint(tb=
[OVPN_A_PEER_VPN_RX_BYTES]);
+ msg(D_DCO_DEBUG, "%s / tun_read_bytes: " counter=
_format, __func__,
+ =
c2->tun_read_bytes);
+ }
=
+ else
+ {
+ msg(M_WARN, &quo=
t;%s: no VPN RX bytes provided in reply for peer %u",
+ __func__, id);
+ }
+
+ if (tb[OVPN_A_PEER_VPN_TX_BYTES])
+ {
+ c2->tun_write_bytes =3D ovpn_nla_get_uint(tb=
[OVPN_A_PEER_VPN_TX_BYTES]);
+ msg(D_DCO_DEBUG, "%s / tun_write_bytes: " counte=
r_format, __func__,
+=
c2->tun_write_bytes);
+ }
+ else
+ {
+ msg(M_WARN, &q=
uot;%s: no VPN TX bytes provided in reply for peer %u",
+}
+
+sta=
tic int
+dco_parse_pe=
er_multi(struct nl_msg *msg, void *arg)
+{
+ struct nlattr *tb[OVPN_A_MAX + 1];
+ struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_hdr(m=
sg));
+
+
+ =
nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0),
+ genlmsg_attrlen(gnlh, 0), N=
ULL);
+
+ return NL_SKIP;
+ }
+
+ struct nlattr *tb_peer[OVPN_A_PEER_MAX + 1];
+ nla_parse_nested(tb_peer, OVPN_A_PEER=
_MAX, tb[OVPN_A_PEER], NULL);
+
+ if (!=
tb_peer[OVPN_A_PEER_ID])
+ {
+ m=
sg(M_WARN, "%s: no peer-id provided in reply", __func__);<=
br>+ return NL_SKIP;
+ }
+
+ struct multi_context *m =3D arg;
+ uint32_t peer_id =3D nla_get_u32(tb=
_peer[OVPN_A_PEER_ID]);
+
+ if (peer_id=
>=3D m->max_clients || !m->instances[peer_id])
+ {
+ msg(M_WARN, "%s: cannot store DCO stats=
for peer %u", __func__,
+ peer_id);
+ return NL_SKIP;
+ }
+
+ dco_update_pe=
er_stat(&m->instances[peer_id]->context.c2, tb_peer, peer_id);
+
+ return NL_OK;
+}
+
+sta=
tic int
+dco_parse_pe=
er(struct nl_msg *msg, void *arg)
+{
+ =
struct context *c =3D arg;
+ struct nlattr *tb[OVPN_A_MAX + 1];
+ struct genlmsghdr *gnlh =3D nlmsg_data(nlmsg_=
hdr(msg));
++ msg(D_DCO_DEBUG, "%=
s: parsing message...", __func__);
+
=
+ nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0),
+ genlmsg_attrlen(gnlh, 0=
), NULL);
+
+ if (!tb[OVPN_A_PEER])
+ {
+ msg(D_DCO_DEBUG, "%s: ma=
lformed reply", __func__);
+ return NL_SKIP;
+ }
+
+ struct nlattr=
*tb_peer[OVPN_A_PEER_MAX + 1];
+ nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER],=
NULL);
+
<=
span style=3D"color: hsl(120, 100%, 40%);">+ if (!tb_peer[OVPN_A_PEER_ID=
])
+ {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ msg(M_WARN, "%s: n=
o peer-id provided in reply", __func__);
+ return NL_SKIP;
+ }
+
+ =
uint32_t peer_id =3D nla_get_u32(tb_peer[OVPN_A_PEER_ID]);
+ if (c->c2.tls_multi->dco_=
peer_id !=3D peer_id)
+ {
+ retu=
rn NL_SKIP;
+ }
+
+ dco_update_peer_stat(&c->c2, t=
b_peer, peer_id);
+=
span>
+ return NL_OK;
+}
+
/* This function parses=
any netlink message sent by ovpn-dco to userspace */
stat=
ic int
ovpn_handle_msg(struct nl_msg *msg, void *arg)
@@ -988,112 +1132,6 @@
return ovpn_nl_recv=
msgs(dco, __func__);
}
-/* libnl < 3.11.0 does not implement=
nla_get_uint() */
-sta=
tic uint64_t
-ovpn_nla_=
get_uint(struct nlattr *attr)
-{
- if (nla_=
len(attr) =3D=3D sizeof(uint32_t))
- {
- =
return nla_get_u32(attr);
- }
- els=
e
- {
- }
-}
-
-static=
void
-dco_update_peer_=
stat(struct context_2 *c2, struct nlattr *tb[], uint32_t id)
- if (tb[OVPN_A_PEER_LINK_RX_BYTES])
- {
- c2->dco_read_bytes =3D ovpn_nla_get_uint(tb[=
OVPN_A_PEER_LINK_RX_BYTES]);
- msg(D_DCO_DEBUG, "%s / dco_read_bytes: " counter_f=
ormat, __func__,
- =
c2->dco_read_bytes);
- }
- els=
e
- {
- __func__, id);
- }
-
- i=
f (tb[OVPN_A_PEER_LINK_TX_BYTES])
- {
- =
c2->dco_write_bytes =3D ovpn_nla_get_uint(tb[OVPN_A_PEER_LINK_TX_BYT=
ES]);
- msg(D_DC=
O_DEBUG, "%s / dco_write_bytes: " counter_format, __func__,
- c2->dco_writ=
e_bytes);
- }=
- else
- {
- msg(M_WARN, "%s: no link TX bytes provided i=
n reply for peer %u",
- __func__, id);
- }
-
- if (tb[OVPN_A_PEER_VPN_=
RX_BYTES])
- {
- c2->tun_read_byt=
es =3D ovpn_nla_get_uint(tb[OVPN_A_PEER_VPN_RX_BYTES]);
- msg(D_DCO_DEBUG, "%s / tun_r=
ead_bytes: " counter_format, __func__,
- c2->tun_read_bytes);
- }
- else
- {
- m=
sg(M_WARN, "%s: no VPN RX bytes provided in reply for peer %u",=
span>
- __func__, i=
d);
- }-
- if (tb[OVPN_A_PEER_VPN_TX_BYTES])
- {
- c2->tun_write_bytes =3D ovpn_nla_get_uint(tb=
[OVPN_A_PEER_VPN_TX_BYTES]);
- msg(D_DCO_DEBUG, "%s / tun_write_bytes: " counter_=
format, __func__,
- =
c2->tun_write_bytes);
- }
- e=
lse
- {- msg(M_WARN, "%s: no V=
PN TX bytes provided in reply for peer %u",
- __func__, id);
- }
-}
-
-int
-dco_parse_peer_multi(struct nl_msg *msg, vo=
id *arg)
-{
<=
span style=3D"color: hsl(0, 100%, 40%);">- struct nlattr *tb[OVPN_A_MAX =
+ 1];
- struct genlm=
sghdr *gnlh =3D nlmsg_data(nlmsg_hdr(msg));
-
=
- msg(D_DCO_DEBUG, "%s: parsing message...", __func__);=
-
- nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gn=
lh, 0),
- =
genlmsg_attrlen(gnlh, 0), NULL);
-
- if (!t=
b[OVPN_A_PEER])
- {<=
/span>
- return NL_SKIP=
;
- }
- struct nlattr *tb_peer[OVPN_A_PEER_MAX + 1];- nla_parse_nested(tb_peer, =
OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL);
-
- =
if (!tb_peer[OVPN_A_PEER_ID])
- {
- =
msg(M_WARN, "%s: no peer-id provided in reply", __func__);
- return NL_SKIP;
- }
-
- struct multi_context *m =3D arg;
- uint32_t peer_id =3D nla_get_u32(tb_peer[O=
VPN_A_PEER_ID]);
-
- if (peer_id >=3D m-=
>max_clients || !m->instances[peer_id])
- {
- msg(M_WARN, "%s: cannot store DCO stats for peer %u&qu=
ot;, __func__,
- =
peer_id);
- =
return NL_SKIP;
- }=
-
- dco_update_peer_stat(&m->instan=
ces[peer_id]->context.c2, tb_peer, peer_id);
-
- return NL_OK;
=
-}
-
i=
nt
dco_get_peer_stats_multi(dco_context_t *dco, struct mul=
ti_context *m,
const bool raise_s=
igusr1_on_err)
@@ -1118,44 +1156,6 @@
=
return ret;
}
-static int
-dco_parse_peer(struct nl_msg *msg, void *arg)
- struct context *c =3D arg;
- struct nlattr *tb[OVPN_A_MAX + 1];<=
br>- struct genlmsghdr *gnlh =
=3D nlmsg_data(nlmsg_hdr(msg));
-
- msg(D_D=
CO_DEBUG, "%s: parsing message...", __func__);
-
- nla_parse(tb, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0),
- genlmsg_attr=
len(gnlh, 0), NULL);
-<=
/span>
- if (!tb[OVPN_A_PEE=
R])
- {- msg(D_DCO_DEBUG, "%s:=
malformed reply", __func__);
- return NL_SKIP;
- }
-<=
/span>
- struct nlattr *tb_=
peer[OVPN_A_PEER_MAX + 1];
- nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, tb[OVPN_A_PEER], NULL);=
-
- if (!tb_peer[OVPN_A_PEER_ID])- {
- msg(M_WARN, "%s: no peer-id provi=
ded in reply", __func__);
- return NL_SKIP;
- }
-
- uint32_t peer_id =3D n=
la_get_u32(tb_peer[OVPN_A_PEER_ID]);
- if (c->c2.tls_multi->dco_peer_id !=3D peer_id)
- {
- return NL_SKIP;
- }
-
- =
dco_update_peer_stat(&c->c2, tb_peer, peer_id);
-
- return NL_OK;
-}
- int
dco_get_peer_stats(struct context *c, const b=
ool raise_sigusr1_on_err)
{
To view, visit change 1098. To unsubscribe, or for help writing mail filt= ers, visit settings.
=