2019-05-19 19:08:55 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-06-15 01:37:14 +07:00
|
|
|
/*
|
|
|
|
* Pluggable TCP upper layer protocol support.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
|
|
|
|
* Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
#include <linux/module.h>
|
2017-06-15 01:37:14 +07:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/gfp.h>
|
|
|
|
#include <net/tcp.h>
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(tcp_ulp_list_lock);
|
|
|
|
static LIST_HEAD(tcp_ulp_list);
|
|
|
|
|
|
|
|
/* Simple linear search, don't expect many entries! */
|
|
|
|
static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
|
|
|
|
{
|
|
|
|
struct tcp_ulp_ops *e;
|
|
|
|
|
2020-02-21 22:15:38 +07:00
|
|
|
list_for_each_entry_rcu(e, &tcp_ulp_list, list,
|
|
|
|
lockdep_is_held(&tcp_ulp_list_lock)) {
|
2017-06-15 01:37:14 +07:00
|
|
|
if (strcmp(e->name, name) == 0)
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
|
|
|
|
{
|
|
|
|
const struct tcp_ulp_ops *ulp = NULL;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
ulp = tcp_ulp_find(name);
|
|
|
|
|
|
|
|
#ifdef CONFIG_MODULES
|
|
|
|
if (!ulp && capable(CAP_NET_ADMIN)) {
|
|
|
|
rcu_read_unlock();
|
tcp, ulp: add alias for all ulp modules
Lets not turn the TCP ULP lookup into an arbitrary module loader as
we only intend to load ULP modules through this mechanism, not other
unrelated kernel modules:
[root@bar]# cat foo.c
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/tcp.h>
#include <linux/in.h>
int main(void)
{
int sock = socket(PF_INET, SOCK_STREAM, 0);
setsockopt(sock, IPPROTO_TCP, TCP_ULP, "sctp", sizeof("sctp"));
return 0;
}
[root@bar]# gcc foo.c -O2 -Wall
[root@bar]# lsmod | grep sctp
[root@bar]# ./a.out
[root@bar]# lsmod | grep sctp
sctp 1077248 4
libcrc32c 16384 3 nf_conntrack,nf_nat,sctp
[root@bar]#
Fix it by adding module alias to TCP ULP modules, so probing module
via request_module() will be limited to tcp-ulp-[name]. The existing
modules like kTLS will load fine given tcp-ulp-tls alias, but others
will fail to load:
[root@bar]# lsmod | grep sctp
[root@bar]# ./a.out
[root@bar]# lsmod | grep sctp
[root@bar]#
Sockmap is not affected from this since it's either built-in or not.
Fixes: 734942cc4ea6 ("tcp: ULP infrastructure")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Acked-by: Song Liu <songliubraving@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2018-08-17 02:49:06 +07:00
|
|
|
request_module("tcp-ulp-%s", name);
|
2017-06-15 01:37:14 +07:00
|
|
|
rcu_read_lock();
|
|
|
|
ulp = tcp_ulp_find(name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (!ulp || !try_module_get(ulp->owner))
|
|
|
|
ulp = NULL;
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
return ulp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach new upper layer protocol to the list
|
|
|
|
* of available protocols.
|
|
|
|
*/
|
|
|
|
int tcp_register_ulp(struct tcp_ulp_ops *ulp)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
spin_lock(&tcp_ulp_list_lock);
|
2018-02-06 01:17:43 +07:00
|
|
|
if (tcp_ulp_find(ulp->name))
|
2017-06-15 01:37:14 +07:00
|
|
|
ret = -EEXIST;
|
2018-02-06 01:17:43 +07:00
|
|
|
else
|
2017-06-15 01:37:14 +07:00
|
|
|
list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
|
|
|
|
spin_unlock(&tcp_ulp_list_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tcp_register_ulp);
|
|
|
|
|
|
|
|
void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
|
|
|
|
{
|
|
|
|
spin_lock(&tcp_ulp_list_lock);
|
|
|
|
list_del_rcu(&ulp->list);
|
|
|
|
spin_unlock(&tcp_ulp_list_lock);
|
|
|
|
|
|
|
|
synchronize_rcu();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
|
|
|
|
|
|
|
|
/* Build string with list of available upper layer protocl values */
|
|
|
|
void tcp_get_available_ulp(char *buf, size_t maxlen)
|
|
|
|
{
|
|
|
|
struct tcp_ulp_ops *ulp_ops;
|
|
|
|
size_t offs = 0;
|
|
|
|
|
2017-06-23 08:57:55 +07:00
|
|
|
*buf = '\0';
|
2017-06-15 01:37:14 +07:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
|
|
|
|
offs += snprintf(buf + offs, maxlen - offs,
|
|
|
|
"%s%s",
|
|
|
|
offs == 0 ? "" : " ", ulp_ops->name);
|
2019-11-20 15:38:08 +07:00
|
|
|
|
|
|
|
if (WARN_ON_ONCE(offs >= maxlen))
|
|
|
|
break;
|
2017-06-15 01:37:14 +07:00
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:12:01 +07:00
|
|
|
void tcp_update_ulp(struct sock *sk, struct proto *proto,
|
|
|
|
void (*write_space)(struct sock *sk))
|
2019-07-20 00:29:22 +07:00
|
|
|
{
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
|
|
|
|
|
|
|
if (icsk->icsk_ulp_ops->update)
|
2020-01-11 13:12:01 +07:00
|
|
|
icsk->icsk_ulp_ops->update(sk, proto, write_space);
|
2019-07-20 00:29:22 +07:00
|
|
|
}
|
|
|
|
|
2017-06-15 01:37:14 +07:00
|
|
|
void tcp_cleanup_ulp(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
|
|
|
|
2018-10-17 02:31:35 +07:00
|
|
|
/* No sock_owned_by_me() check here as at the time the
|
|
|
|
* stack calls this function, the socket is dead and
|
|
|
|
* about to be destroyed.
|
|
|
|
*/
|
2017-06-15 01:37:14 +07:00
|
|
|
if (!icsk->icsk_ulp_ops)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (icsk->icsk_ulp_ops->release)
|
|
|
|
icsk->icsk_ulp_ops->release(sk);
|
|
|
|
module_put(icsk->icsk_ulp_ops->owner);
|
2018-08-17 02:49:07 +07:00
|
|
|
|
|
|
|
icsk->icsk_ulp_ops = NULL;
|
2017-06-15 01:37:14 +07:00
|
|
|
}
|
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
|
2017-06-15 01:37:14 +07:00
|
|
|
{
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
2018-10-13 07:45:57 +07:00
|
|
|
int err;
|
2017-06-15 01:37:14 +07:00
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
err = -EEXIST;
|
2017-06-15 01:37:14 +07:00
|
|
|
if (icsk->icsk_ulp_ops)
|
2018-10-13 07:45:57 +07:00
|
|
|
goto out_err;
|
2018-02-06 01:17:43 +07:00
|
|
|
|
|
|
|
err = ulp_ops->init(sk);
|
2018-10-13 07:45:57 +07:00
|
|
|
if (err)
|
|
|
|
goto out_err;
|
2018-02-06 01:17:43 +07:00
|
|
|
|
|
|
|
icsk->icsk_ulp_ops = ulp_ops;
|
|
|
|
return 0;
|
2018-10-13 07:45:57 +07:00
|
|
|
out_err:
|
|
|
|
module_put(ulp_ops->owner);
|
|
|
|
return err;
|
2018-02-06 01:17:43 +07:00
|
|
|
}
|
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
int tcp_set_ulp(struct sock *sk, const char *name)
|
2018-02-06 01:17:43 +07:00
|
|
|
{
|
|
|
|
const struct tcp_ulp_ops *ulp_ops;
|
|
|
|
|
2018-10-13 07:45:56 +07:00
|
|
|
sock_owned_by_me(sk);
|
2018-02-06 01:17:43 +07:00
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
ulp_ops = __tcp_ulp_find_autoload(name);
|
2018-02-06 01:17:43 +07:00
|
|
|
if (!ulp_ops)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2018-10-13 07:45:57 +07:00
|
|
|
return __tcp_set_ulp(sk, ulp_ops);
|
2017-06-15 01:37:14 +07:00
|
|
|
}
|