Merge branch 'mlxsw-Introduce-ACL-traps'
[linux-2.6-block.git] / net / ipv4 / tcp_ulp.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
734942cc
DW
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
1243a51f 10#include <linux/module.h>
734942cc
DW
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 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();
037b0b86 43 request_module("tcp-ulp-%s", name);
734942cc
DW
44 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);
b11a632c 63 if (tcp_ulp_find(ulp->name))
734942cc 64 ret = -EEXIST;
b11a632c 65 else
734942cc 66 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
734942cc
DW
67 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
926f38e9 89 *buf = '\0';
734942cc
DW
90 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);
9bb59a21
HL
95
96 if (WARN_ON_ONCE(offs >= maxlen))
97 break;
734942cc
DW
98 }
99 rcu_read_unlock();
100}
101
33bfe20d
JF
102void tcp_update_ulp(struct sock *sk, struct proto *proto,
103 void (*write_space)(struct sock *sk))
95fa1454
JF
104{
105 struct inet_connection_sock *icsk = inet_csk(sk);
106
107 if (!icsk->icsk_ulp_ops) {
33bfe20d 108 sk->sk_write_space = write_space;
b8e202d1
JS
109 /* Pairs with lockless read in sk_clone_lock() */
110 WRITE_ONCE(sk->sk_prot, proto);
95fa1454
JF
111 return;
112 }
113
114 if (icsk->icsk_ulp_ops->update)
33bfe20d 115 icsk->icsk_ulp_ops->update(sk, proto, write_space);
95fa1454
JF
116}
117
734942cc
DW
118void tcp_cleanup_ulp(struct sock *sk)
119{
120 struct inet_connection_sock *icsk = inet_csk(sk);
121
aadd4355
DB
122 /* No sock_owned_by_me() check here as at the time the
123 * stack calls this function, the socket is dead and
124 * about to be destroyed.
125 */
734942cc
DW
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);
90545cdc
DB
132
133 icsk->icsk_ulp_ops = NULL;
734942cc
DW
134}
135
1243a51f 136static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
734942cc
DW
137{
138 struct inet_connection_sock *icsk = inet_csk(sk);
1243a51f 139 int err;
734942cc 140
1243a51f 141 err = -EEXIST;
734942cc 142 if (icsk->icsk_ulp_ops)
1243a51f 143 goto out_err;
b11a632c
JF
144
145 err = ulp_ops->init(sk);
1243a51f
DB
146 if (err)
147 goto out_err;
b11a632c
JF
148
149 icsk->icsk_ulp_ops = ulp_ops;
150 return 0;
1243a51f
DB
151out_err:
152 module_put(ulp_ops->owner);
153 return err;
b11a632c
JF
154}
155
1243a51f 156int tcp_set_ulp(struct sock *sk, const char *name)
b11a632c 157{
b11a632c 158 const struct tcp_ulp_ops *ulp_ops;
b11a632c 159
8b9088f8 160 sock_owned_by_me(sk);
b11a632c 161
1243a51f 162 ulp_ops = __tcp_ulp_find_autoload(name);
b11a632c
JF
163 if (!ulp_ops)
164 return -ENOENT;
165
1243a51f 166 return __tcp_set_ulp(sk, ulp_ops);
734942cc 167}