[PATCH] net: add sock_open() for unified socket creation

From: 0-x-0-0

Date: Thu Jun 18 2026 - 08:21:32 EST


Signed-off-by: 0-x-0-0 <sasha.goltsev777@xxxxxxxxx>
---
include/linux/net.h | 14 ++++++++++++++
net/socket.c | 31 +++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+)

diff --git a/include/linux/net.h b/include/linux/net.h
index f268f395c..18883ea74 100644
--- a/include/linux/net.h
+++ b/include/linux/net.h
@@ -116,6 +116,18 @@ enum sock_shutdown_cmd {
SHUT_RDWR,
};

+/**
+ * enum sock_create_type_t - Socket create types
+ * @SOCK_USER: creates a userspace socket
+ * @SOCK_KERN: creates a kernel socket
+ * @SOCK_LITE: creates a lightweight uninitialized socket
+ */
+enum sock_create_type_t {
+ SOCK_USER,
+ SOCK_KERN,
+ SOCK_LITE,
+};
+
struct socket_wq {
/* Note: wait MUST be first field of socket_wq */
wait_queue_head_t wait;
@@ -273,6 +285,8 @@ int sock_wake_async(struct socket_wq *sk_wq, int
how, int band);
int sock_register(const struct net_proto_family *fam);
void sock_unregister(int family);
bool sock_is_registered(int family);
+int sock_open(int family, int type, int protocol, struct socket **res,
+ enum sock_create_type_t sock_type);
int __sock_create(struct net *net, int family, int type, int proto,
struct socket **res, int kern);
int sock_create(int family, int type, int proto, struct socket **res);
diff --git a/net/socket.c b/net/socket.c
index 63c69a0fa..2c79c022a 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1562,6 +1562,37 @@ int sock_wake_async(struct socket_wq *wq, int
how, int band)
}
EXPORT_SYMBOL(sock_wake_async);

+/**
+ * sock_open - creates a socket (unified interface)
+ * @family: protocol family (AF_INET, ...)
+ * @type: communication type (SOCK_STREAM, ...)
+ * @protocol: protocol (0, ...)
+ * @res: new socket
+ * @sock_type: one of SOCK_USER, SOCK_KERN, or SOCK_LITE
+ *
+ * Unified entry point for all socket creation variants.
+ * SOCK_USER creates a userspace socket (via sock_create).
+ * SOCK_KERN creates a kernel socket (via sock_create_kern).
+ * SOCK_LITE creates a lightweight uninitialized socket (via sock_create_lite).
+ *
+ * Return: 0 on success, negative errno on failure. On failure @res is NULL.
+ */
+
+int sock_open(int family, int type, int protocol, struct socket
**res, enum sock_create_type_t sock_type)
+{
+ switch (sock_type) {
+ case SOCK_USER:
+ return sock_create(family, type, protocol, res);
+ case SOCK_KERN:
+ return sock_create_kern(current->nsproxy->net_ns, family, type,
protocol, res);
+ case SOCK_LITE:
+ return sock_create_lite(family, type, protocol, res);
+ default:
+ return -EINVAL;
+ }
+}
+EXPORT_SYMBOL(sock_open);
+
/**
* __sock_create - creates a socket
* @net: net namespace
--
2.47.3