blob: 12ab5db2b71cb5781fd9517d944138e1cecbd5dc [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Dave Watson734942c2017-06-14 11:37:14 -07002/*
3 * Pluggable TCP upper layer protocol support.
4 *
5 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
7 *
8 */
9
Daniel Borkmann1243a512018-10-13 02:45:57 +020010#include <linux/module.h>
Dave Watson734942c2017-06-14 11:37:14 -070011#include <linux/mm.h>
12#include <linux/types.h>
13#include <linux/list.h>
14#include <linux/gfp.h>
15#include <net/tcp.h>
16
17static DEFINE_SPINLOCK(tcp_ulp_list_lock);
18static LIST_HEAD(tcp_ulp_list);
19
20/* Simple linear search, don't expect many entries! */
21static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
22{
23 struct tcp_ulp_ops *e;
24
25 list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
26 if (strcmp(e->name, name) == 0)
27 return e;
28 }
29
30 return NULL;
31}
32
33static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
34{
35 const struct tcp_ulp_ops *ulp = NULL;
36
37 rcu_read_lock();
38 ulp = tcp_ulp_find(name);
39
40#ifdef CONFIG_MODULES
41 if (!ulp && capable(CAP_NET_ADMIN)) {
42 rcu_read_unlock();
Daniel Borkmann037b0b82018-08-16 21:49:06 +020043 request_module("tcp-ulp-%s", name);
Dave Watson734942c2017-06-14 11:37:14 -070044 rcu_read_lock();
45 ulp = tcp_ulp_find(name);
46 }
47#endif
48 if (!ulp || !try_module_get(ulp->owner))
49 ulp = NULL;
50
51 rcu_read_unlock();
52 return ulp;
53}
54
55/* Attach new upper layer protocol to the list
56 * of available protocols.
57 */
58int tcp_register_ulp(struct tcp_ulp_ops *ulp)
59{
60 int ret = 0;
61
62 spin_lock(&tcp_ulp_list_lock);
John Fastabendb11a6322018-02-05 10:17:43 -080063 if (tcp_ulp_find(ulp->name))
Dave Watson734942c2017-06-14 11:37:14 -070064 ret = -EEXIST;
John Fastabendb11a6322018-02-05 10:17:43 -080065 else
Dave Watson734942c2017-06-14 11:37:14 -070066 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
Dave Watson734942c2017-06-14 11:37:14 -070067 spin_unlock(&tcp_ulp_list_lock);
68
69 return ret;
70}
71EXPORT_SYMBOL_GPL(tcp_register_ulp);
72
73void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
74{
75 spin_lock(&tcp_ulp_list_lock);
76 list_del_rcu(&ulp->list);
77 spin_unlock(&tcp_ulp_list_lock);
78
79 synchronize_rcu();
80}
81EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
82
83/* Build string with list of available upper layer protocl values */
84void tcp_get_available_ulp(char *buf, size_t maxlen)
85{
86 struct tcp_ulp_ops *ulp_ops;
87 size_t offs = 0;
88
Jakub Kicinski926f38e2017-06-22 18:57:55 -070089 *buf = '\0';
Dave Watson734942c2017-06-14 11:37:14 -070090 rcu_read_lock();
91 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
92 offs += snprintf(buf + offs, maxlen - offs,
93 "%s%s",
94 offs == 0 ? "" : " ", ulp_ops->name);
Hangbin Liu9bb59a22019-11-20 16:38:08 +080095
96 if (WARN_ON_ONCE(offs >= maxlen))
97 break;
Dave Watson734942c2017-06-14 11:37:14 -070098 }
99 rcu_read_unlock();
100}
101
John Fastabend95fa1452019-07-19 10:29:22 -0700102void tcp_update_ulp(struct sock *sk, struct proto *proto)
103{
104 struct inet_connection_sock *icsk = inet_csk(sk);
105
106 if (!icsk->icsk_ulp_ops) {
107 sk->sk_prot = proto;
108 return;
109 }
110
111 if (icsk->icsk_ulp_ops->update)
112 icsk->icsk_ulp_ops->update(sk, proto);
113}
114
Dave Watson734942c2017-06-14 11:37:14 -0700115void tcp_cleanup_ulp(struct sock *sk)
116{
117 struct inet_connection_sock *icsk = inet_csk(sk);
118
Daniel Borkmannaadd4352018-10-16 21:31:35 +0200119 /* No sock_owned_by_me() check here as at the time the
120 * stack calls this function, the socket is dead and
121 * about to be destroyed.
122 */
Dave Watson734942c2017-06-14 11:37:14 -0700123 if (!icsk->icsk_ulp_ops)
124 return;
125
126 if (icsk->icsk_ulp_ops->release)
127 icsk->icsk_ulp_ops->release(sk);
128 module_put(icsk->icsk_ulp_ops->owner);
Daniel Borkmann90545cd2018-08-16 21:49:07 +0200129
130 icsk->icsk_ulp_ops = NULL;
Dave Watson734942c2017-06-14 11:37:14 -0700131}
132
Daniel Borkmann1243a512018-10-13 02:45:57 +0200133static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
Dave Watson734942c2017-06-14 11:37:14 -0700134{
135 struct inet_connection_sock *icsk = inet_csk(sk);
Daniel Borkmann1243a512018-10-13 02:45:57 +0200136 int err;
137
138 err = -EEXIST;
139 if (icsk->icsk_ulp_ops)
140 goto out_err;
141
142 err = ulp_ops->init(sk);
143 if (err)
144 goto out_err;
145
146 icsk->icsk_ulp_ops = ulp_ops;
147 return 0;
148out_err:
149 module_put(ulp_ops->owner);
150 return err;
151}
152
153int tcp_set_ulp(struct sock *sk, const char *name)
154{
Dave Watson734942c2017-06-14 11:37:14 -0700155 const struct tcp_ulp_ops *ulp_ops;
Dave Watson734942c2017-06-14 11:37:14 -0700156
Daniel Borkmann8b9088f2018-10-13 02:45:56 +0200157 sock_owned_by_me(sk);
Dave Watson734942c2017-06-14 11:37:14 -0700158
159 ulp_ops = __tcp_ulp_find_autoload(name);
160 if (!ulp_ops)
Sabrina Dubroca539a06b2017-08-14 18:04:24 +0200161 return -ENOENT;
Dave Watson734942c2017-06-14 11:37:14 -0700162
Daniel Borkmann1243a512018-10-13 02:45:57 +0200163 return __tcp_set_ulp(sk, ulp_ops);
Dave Watson734942c2017-06-14 11:37:14 -0700164}