Re: [PATCH v21 19/24] ovpn: implement peer add/get/dump/delete via netlink

2025-03-04 Thread Antonio Quartulli

On 04/03/2025 15:35, Sabrina Dubroca wrote:

2025-03-04, 01:33:49 +0100, Antonio Quartulli wrote:

@@ -1317,11 +1336,16 @@ void ovpn_peer_keepalive_work(struct work_struct *work)
  
  	/* prevent rearming if the interface is being destroyed */

if (next_run > 0 && ovpn->registered) {
+   time64_t delta = next_run - now;
+
netdev_dbg(ovpn->dev,
   "scheduling keepalive work: now=%llu next_run=%llu 
delta=%llu\n",
-  next_run, now, next_run - now);
+  next_run, now, delta > 0 ? delta : 0);
+   /* due to the waiting above, the next_run deadline may have
+* passed: in this case we reschedule the worker immediately
+*/


I don't understand this bit. I don't see what waiting you're refering
to (in particular within this patch), and I don't see how we could get
next_run < now based on how next_run is computed in
ovpn_peer_keepalive_work_single (next_run1/next_run2 is always set to
now + X or something that we just tested to be > now).

Am I missing something?


Sorry - this is just noise.
Before getting to this final solution, I went for an intermediate 
implementation where I was calling wait_for_completion() on a list of 
removed peers (this is "the waiting above" the comment refers to).


Since I had no clue how long I waited, I had to update 'now'.
But at that point there was a chance that we had passed the deadline for 
the next run, hence ending up with a negative delta.


This said, all this logic was thrown away, but I accidentally kept the 
check on delta (that I even argued about in a previous email when you 
saw the bogus ternary-op)


so yeah, the ternary-op can go away.

Thanks for double/triple checking with me.

Cheers,




schedule_delayed_work(&ovpn->keepalive_work,
- (next_run - now) * HZ);
+ delta * HZ > 0 ? delta * HZ : 0);
}
unlock_ovpn(ovpn, &release_list);
  }




--
Antonio Quartulli
OpenVPN Inc.




Re: [PATCH v21 19/24] ovpn: implement peer add/get/dump/delete via netlink

2025-03-04 Thread Sabrina Dubroca
2025-03-04, 01:33:49 +0100, Antonio Quartulli wrote:
> @@ -1317,11 +1336,16 @@ void ovpn_peer_keepalive_work(struct work_struct 
> *work)
>  
>   /* prevent rearming if the interface is being destroyed */
>   if (next_run > 0 && ovpn->registered) {
> + time64_t delta = next_run - now;
> +
>   netdev_dbg(ovpn->dev,
>  "scheduling keepalive work: now=%llu next_run=%llu 
> delta=%llu\n",
> -next_run, now, next_run - now);
> +next_run, now, delta > 0 ? delta : 0);
> + /* due to the waiting above, the next_run deadline may have
> +  * passed: in this case we reschedule the worker immediately
> +  */

I don't understand this bit. I don't see what waiting you're refering
to (in particular within this patch), and I don't see how we could get
next_run < now based on how next_run is computed in
ovpn_peer_keepalive_work_single (next_run1/next_run2 is always set to
now + X or something that we just tested to be > now).

Am I missing something?

>   schedule_delayed_work(&ovpn->keepalive_work,
> -   (next_run - now) * HZ);
> +   delta * HZ > 0 ? delta * HZ : 0);
>   }
>   unlock_ovpn(ovpn, &release_list);
>  }

-- 
Sabrina



[PATCH v21 19/24] ovpn: implement peer add/get/dump/delete via netlink

2025-03-03 Thread Antonio Quartulli
This change introduces the netlink command needed to add, delete and
retrieve/dump known peers. Userspace is expected to use these commands
to handle known peer lifecycles.

Signed-off-by: Antonio Quartulli 
---
 drivers/net/ovpn/netlink.c | 677 -
 drivers/net/ovpn/peer.c|  62 +++--
 drivers/net/ovpn/peer.h|   5 +
 drivers/net/ovpn/socket.c  |   4 +-
 4 files changed, 722 insertions(+), 26 deletions(-)

diff --git a/drivers/net/ovpn/netlink.c b/drivers/net/ovpn/netlink.c
index 
8d267d4c82283d9b5f989478102086ce385195d5..9824ce4437e63e602d37d31105fb9ac06f99d421
 100644
--- a/drivers/net/ovpn/netlink.c
+++ b/drivers/net/ovpn/netlink.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 
 #include 
@@ -15,6 +16,9 @@
 #include "main.h"
 #include "netlink.h"
 #include "netlink-gen.h"
+#include "bind.h"
+#include "peer.h"
+#include "socket.h"
 
 MODULE_ALIAS_GENL_FAMILY(OVPN_FAMILY_NAME);
 
@@ -89,29 +93,692 @@ void ovpn_nl_post_doit(const struct genl_split_ops *ops, 
struct sk_buff *skb,
netdev_put(ovpn->dev, tracker);
 }
 
+static bool ovpn_nl_attr_sockaddr_remote(struct nlattr **attrs,
+struct sockaddr_storage *ss)
+{
+   struct sockaddr_in6 *sin6;
+   struct sockaddr_in *sin;
+   struct in6_addr *in6;
+   __be16 port = 0;
+   __be32 *in;
+
+   ss->ss_family = AF_UNSPEC;
+
+   if (attrs[OVPN_A_PEER_REMOTE_PORT])
+   port = nla_get_be16(attrs[OVPN_A_PEER_REMOTE_PORT]);
+
+   if (attrs[OVPN_A_PEER_REMOTE_IPV4]) {
+   ss->ss_family = AF_INET;
+   in = nla_data(attrs[OVPN_A_PEER_REMOTE_IPV4]);
+   } else if (attrs[OVPN_A_PEER_REMOTE_IPV6]) {
+   ss->ss_family = AF_INET6;
+   in6 = nla_data(attrs[OVPN_A_PEER_REMOTE_IPV6]);
+   } else {
+   return false;
+   }
+
+   switch (ss->ss_family) {
+   case AF_INET6:
+   /* If this is a regular IPv6 just break and move on,
+* otherwise switch to AF_INET and extract the IPv4 accordingly
+*/
+   if (!ipv6_addr_v4mapped(in6)) {
+   sin6 = (struct sockaddr_in6 *)ss;
+   sin6->sin6_port = port;
+   memcpy(&sin6->sin6_addr, in6, sizeof(*in6));
+   break;
+   }
+
+   /* v4-mapped-v6 address */
+   ss->ss_family = AF_INET;
+   in = &in6->s6_addr32[3];
+   fallthrough;
+   case AF_INET:
+   sin = (struct sockaddr_in *)ss;
+   sin->sin_port = port;
+   sin->sin_addr.s_addr = *in;
+   break;
+   }
+
+   return true;
+}
+
+static u8 *ovpn_nl_attr_local_ip(struct nlattr **attrs)
+{
+   u8 *addr6;
+
+   if (!attrs[OVPN_A_PEER_LOCAL_IPV4] && !attrs[OVPN_A_PEER_LOCAL_IPV6])
+   return NULL;
+
+   if (attrs[OVPN_A_PEER_LOCAL_IPV4])
+   return nla_data(attrs[OVPN_A_PEER_LOCAL_IPV4]);
+
+   addr6 = nla_data(attrs[OVPN_A_PEER_LOCAL_IPV6]);
+   /* this is an IPv4-mapped IPv6 address, therefore extract the actual
+* v4 address from the last 4 bytes
+*/
+   if (ipv6_addr_v4mapped((struct in6_addr *)addr6))
+   return addr6 + 12;
+
+   return addr6;
+}
+
+static sa_family_t ovpn_nl_family_get(struct nlattr *addr4,
+ struct nlattr *addr6)
+{
+   if (addr4)
+   return AF_INET;
+
+   if (addr6) {
+   if (ipv6_addr_v4mapped((struct in6_addr *)nla_data(addr6)))
+   return AF_INET;
+   return AF_INET6;
+   }
+
+   return AF_UNSPEC;
+}
+
+static int ovpn_nl_peer_precheck(struct ovpn_priv *ovpn,
+struct genl_info *info,
+struct nlattr **attrs)
+{
+   sa_family_t local_fam, remote_fam;
+
+   if (NL_REQ_ATTR_CHECK(info->extack, info->attrs[OVPN_A_PEER], attrs,
+ OVPN_A_PEER_ID))
+   return -EINVAL;
+
+   if (attrs[OVPN_A_PEER_REMOTE_IPV4] && attrs[OVPN_A_PEER_REMOTE_IPV6]) {
+   NL_SET_ERR_MSG_MOD(info->extack,
+  "cannot specify both remote IPv4 or IPv6 
address");
+   return -EINVAL;
+   }
+
+   if (!attrs[OVPN_A_PEER_REMOTE_IPV4] &&
+   !attrs[OVPN_A_PEER_REMOTE_IPV6] && attrs[OVPN_A_PEER_REMOTE_PORT]) {
+   NL_SET_ERR_MSG_MOD(info->extack,
+  "cannot specify remote port without IP 
address");
+   return -EINVAL;
+   }
+
+   if (!attrs[OVPN_A_PEER_REMOTE_IPV4] &&
+   attrs[OVPN_A_PEER_LOCAL_IPV4]) {
+   NL_SET_ERR_MSG_MOD(info->extack,
+  "cannot specify local IPv4 address without 
remote");
+   return -EINVAL;
+   }
+
+