1 // SPDX-License-Identifier: GPL-2.0-only
3 * Pluggable TCP upper layer protocol support.
5 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
10 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/list.h>
14 #include <linux/gfp.h>
17 static DEFINE_SPINLOCK(tcp_ulp_list_lock);
18 static LIST_HEAD(tcp_ulp_list);
20 /* Simple linear search, don't expect many entries! */
21 static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
23 struct tcp_ulp_ops *e;
25 list_for_each_entry_rcu(e, &tcp_ulp_list, list,
26 lockdep_is_held(&tcp_ulp_list_lock)) {
27 if (strcmp(e->name, name) == 0)
34 static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
36 const struct tcp_ulp_ops *ulp = NULL;
39 ulp = tcp_ulp_find(name);
42 if (!ulp && capable(CAP_NET_ADMIN)) {
44 request_module("tcp-ulp-%s", name);
46 ulp = tcp_ulp_find(name);
49 if (!ulp || !try_module_get(ulp->owner))
56 /* Attach new upper layer protocol to the list
57 * of available protocols.
59 int tcp_register_ulp(struct tcp_ulp_ops *ulp)
63 spin_lock(&tcp_ulp_list_lock);
64 if (tcp_ulp_find(ulp->name))
67 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
68 spin_unlock(&tcp_ulp_list_lock);
72 EXPORT_SYMBOL_GPL(tcp_register_ulp);
74 void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
76 spin_lock(&tcp_ulp_list_lock);
77 list_del_rcu(&ulp->list);
78 spin_unlock(&tcp_ulp_list_lock);
82 EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
84 /* Build string with list of available upper layer protocl values */
85 void tcp_get_available_ulp(char *buf, size_t maxlen)
87 struct tcp_ulp_ops *ulp_ops;
92 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
93 offs += snprintf(buf + offs, maxlen - offs,
95 offs == 0 ? "" : " ", ulp_ops->name);
97 if (WARN_ON_ONCE(offs >= maxlen))
103 void tcp_update_ulp(struct sock *sk, struct proto *proto,
104 void (*write_space)(struct sock *sk))
106 struct inet_connection_sock *icsk = inet_csk(sk);
108 if (icsk->icsk_ulp_ops->update)
109 icsk->icsk_ulp_ops->update(sk, proto, write_space);
112 void tcp_cleanup_ulp(struct sock *sk)
114 struct inet_connection_sock *icsk = inet_csk(sk);
116 /* No sock_owned_by_me() check here as at the time the
117 * stack calls this function, the socket is dead and
118 * about to be destroyed.
120 if (!icsk->icsk_ulp_ops)
123 if (icsk->icsk_ulp_ops->release)
124 icsk->icsk_ulp_ops->release(sk);
125 module_put(icsk->icsk_ulp_ops->owner);
127 icsk->icsk_ulp_ops = NULL;
130 static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
132 struct inet_connection_sock *icsk = inet_csk(sk);
136 if (icsk->icsk_ulp_ops)
139 err = ulp_ops->init(sk);
143 icsk->icsk_ulp_ops = ulp_ops;
146 module_put(ulp_ops->owner);
150 int tcp_set_ulp(struct sock *sk, const char *name)
152 const struct tcp_ulp_ops *ulp_ops;
154 sock_owned_by_me(sk);
156 ulp_ops = __tcp_ulp_find_autoload(name);
160 return __tcp_set_ulp(sk, ulp_ops);