Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
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
10#include <linux/module.h>
11#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 lockdep_is_held(&tcp_ulp_list_lock)) {
27 if (strcmp(e->name, name) == 0)
28 return e;
29 }
30
31 return NULL;
32}
33
34static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
35{
36 const struct tcp_ulp_ops *ulp = NULL;
37
38 rcu_read_lock();
39 ulp = tcp_ulp_find(name);
40
41#ifdef CONFIG_MODULES
42 if (!ulp && capable(CAP_NET_ADMIN)) {
43 rcu_read_unlock();
44 request_module("tcp-ulp-%s", name);
45 rcu_read_lock();
46 ulp = tcp_ulp_find(name);
47 }
48#endif
49 if (!ulp || !try_module_get(ulp->owner))
50 ulp = NULL;
51
52 rcu_read_unlock();
53 return ulp;
54}
55
56/* Attach new upper layer protocol to the list
57 * of available protocols.
58 */
59int tcp_register_ulp(struct tcp_ulp_ops *ulp)
60{
61 int ret = 0;
62
63 spin_lock(&tcp_ulp_list_lock);
64 if (tcp_ulp_find(ulp->name))
65 ret = -EEXIST;
66 else
67 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
68 spin_unlock(&tcp_ulp_list_lock);
69
70 return ret;
71}
72EXPORT_SYMBOL_GPL(tcp_register_ulp);
73
74void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
75{
76 spin_lock(&tcp_ulp_list_lock);
77 list_del_rcu(&ulp->list);
78 spin_unlock(&tcp_ulp_list_lock);
79
80 synchronize_rcu();
81}
82EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
83
84/* Build string with list of available upper layer protocl values */
85void tcp_get_available_ulp(char *buf, size_t maxlen)
86{
87 struct tcp_ulp_ops *ulp_ops;
88 size_t offs = 0;
89
90 *buf = '\0';
91 rcu_read_lock();
92 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
93 offs += snprintf(buf + offs, maxlen - offs,
94 "%s%s",
95 offs == 0 ? "" : " ", ulp_ops->name);
96
97 if (WARN_ON_ONCE(offs >= maxlen))
98 break;
99 }
100 rcu_read_unlock();
101}
102
103void tcp_update_ulp(struct sock *sk, struct proto *proto,
104 void (*write_space)(struct sock *sk))
105{
106 struct inet_connection_sock *icsk = inet_csk(sk);
107
108 if (icsk->icsk_ulp_ops->update)
109 icsk->icsk_ulp_ops->update(sk, proto, write_space);
110}
111
112void tcp_cleanup_ulp(struct sock *sk)
113{
114 struct inet_connection_sock *icsk = inet_csk(sk);
115
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.
119 */
120 if (!icsk->icsk_ulp_ops)
121 return;
122
123 if (icsk->icsk_ulp_ops->release)
124 icsk->icsk_ulp_ops->release(sk);
125 module_put(icsk->icsk_ulp_ops->owner);
126
127 icsk->icsk_ulp_ops = NULL;
128}
129
130static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
131{
132 struct inet_connection_sock *icsk = inet_csk(sk);
133 int err;
134
135 err = -EEXIST;
136 if (icsk->icsk_ulp_ops)
137 goto out_err;
138
139 if (sk->sk_socket)
140 clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
141
142 err = -ENOTCONN;
143 if (!ulp_ops->clone && sk->sk_state == TCP_LISTEN)
144 goto out_err;
145
146 err = ulp_ops->init(sk);
147 if (err)
148 goto out_err;
149
150 icsk->icsk_ulp_ops = ulp_ops;
151 return 0;
152out_err:
153 module_put(ulp_ops->owner);
154 return err;
155}
156
157int tcp_set_ulp(struct sock *sk, const char *name)
158{
159 const struct tcp_ulp_ops *ulp_ops;
160
161 sock_owned_by_me(sk);
162
163 ulp_ops = __tcp_ulp_find_autoload(name);
164 if (!ulp_ops)
165 return -ENOENT;
166
167 return __tcp_set_ulp(sk, ulp_ops);
168}
1/*
2 * Pluggable TCP upper layer protocol support.
3 *
4 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
6 *
7 */
8
9#include<linux/module.h>
10#include <linux/mm.h>
11#include <linux/types.h>
12#include <linux/list.h>
13#include <linux/gfp.h>
14#include <net/tcp.h>
15
16static DEFINE_SPINLOCK(tcp_ulp_list_lock);
17static LIST_HEAD(tcp_ulp_list);
18
19/* Simple linear search, don't expect many entries! */
20static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
21{
22 struct tcp_ulp_ops *e;
23
24 list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
25 if (strcmp(e->name, name) == 0)
26 return e;
27 }
28
29 return NULL;
30}
31
32static struct tcp_ulp_ops *tcp_ulp_find_id(const int ulp)
33{
34 struct tcp_ulp_ops *e;
35
36 list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
37 if (e->uid == ulp)
38 return e;
39 }
40
41 return NULL;
42}
43
44static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
45{
46 const struct tcp_ulp_ops *ulp = NULL;
47
48 rcu_read_lock();
49 ulp = tcp_ulp_find(name);
50
51#ifdef CONFIG_MODULES
52 if (!ulp && capable(CAP_NET_ADMIN)) {
53 rcu_read_unlock();
54 request_module("%s", name);
55 rcu_read_lock();
56 ulp = tcp_ulp_find(name);
57 }
58#endif
59 if (!ulp || !try_module_get(ulp->owner))
60 ulp = NULL;
61
62 rcu_read_unlock();
63 return ulp;
64}
65
66static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
67{
68 const struct tcp_ulp_ops *ulp;
69
70 rcu_read_lock();
71 ulp = tcp_ulp_find_id(uid);
72 if (!ulp || !try_module_get(ulp->owner))
73 ulp = NULL;
74 rcu_read_unlock();
75 return ulp;
76}
77
78/* Attach new upper layer protocol to the list
79 * of available protocols.
80 */
81int tcp_register_ulp(struct tcp_ulp_ops *ulp)
82{
83 int ret = 0;
84
85 spin_lock(&tcp_ulp_list_lock);
86 if (tcp_ulp_find(ulp->name))
87 ret = -EEXIST;
88 else
89 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
90 spin_unlock(&tcp_ulp_list_lock);
91
92 return ret;
93}
94EXPORT_SYMBOL_GPL(tcp_register_ulp);
95
96void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
97{
98 spin_lock(&tcp_ulp_list_lock);
99 list_del_rcu(&ulp->list);
100 spin_unlock(&tcp_ulp_list_lock);
101
102 synchronize_rcu();
103}
104EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
105
106/* Build string with list of available upper layer protocl values */
107void tcp_get_available_ulp(char *buf, size_t maxlen)
108{
109 struct tcp_ulp_ops *ulp_ops;
110 size_t offs = 0;
111
112 *buf = '\0';
113 rcu_read_lock();
114 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
115 offs += snprintf(buf + offs, maxlen - offs,
116 "%s%s",
117 offs == 0 ? "" : " ", ulp_ops->name);
118 }
119 rcu_read_unlock();
120}
121
122void tcp_cleanup_ulp(struct sock *sk)
123{
124 struct inet_connection_sock *icsk = inet_csk(sk);
125
126 if (!icsk->icsk_ulp_ops)
127 return;
128
129 if (icsk->icsk_ulp_ops->release)
130 icsk->icsk_ulp_ops->release(sk);
131 module_put(icsk->icsk_ulp_ops->owner);
132}
133
134/* Change upper layer protocol for socket */
135int tcp_set_ulp(struct sock *sk, const char *name)
136{
137 struct inet_connection_sock *icsk = inet_csk(sk);
138 const struct tcp_ulp_ops *ulp_ops;
139 int err = 0;
140
141 if (icsk->icsk_ulp_ops)
142 return -EEXIST;
143
144 ulp_ops = __tcp_ulp_find_autoload(name);
145 if (!ulp_ops)
146 return -ENOENT;
147
148 if (!ulp_ops->user_visible) {
149 module_put(ulp_ops->owner);
150 return -ENOENT;
151 }
152
153 err = ulp_ops->init(sk);
154 if (err) {
155 module_put(ulp_ops->owner);
156 return err;
157 }
158
159 icsk->icsk_ulp_ops = ulp_ops;
160 return 0;
161}
162
163int tcp_set_ulp_id(struct sock *sk, int ulp)
164{
165 struct inet_connection_sock *icsk = inet_csk(sk);
166 const struct tcp_ulp_ops *ulp_ops;
167 int err;
168
169 if (icsk->icsk_ulp_ops)
170 return -EEXIST;
171
172 ulp_ops = __tcp_ulp_lookup(ulp);
173 if (!ulp_ops)
174 return -ENOENT;
175
176 err = ulp_ops->init(sk);
177 if (err) {
178 module_put(ulp_ops->owner);
179 return err;
180 }
181
182 icsk->icsk_ulp_ops = ulp_ops;
183 return 0;
184}