Commit | Line | Data |
---|---|---|
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 | ||
17 | static DEFINE_SPINLOCK(tcp_ulp_list_lock); | |
18 | static LIST_HEAD(tcp_ulp_list); | |
19 | ||
20 | /* Simple linear search, don't expect many entries! */ | |
21 | static struct tcp_ulp_ops *tcp_ulp_find(const char *name) | |
22 | { | |
23 | struct tcp_ulp_ops *e; | |
24 | ||
958a93c1 AG |
25 | list_for_each_entry_rcu(e, &tcp_ulp_list, list, |
26 | lockdep_is_held(&tcp_ulp_list_lock)) { | |
734942cc DW |
27 | if (strcmp(e->name, name) == 0) |
28 | return e; | |
29 | } | |
30 | ||
31 | return NULL; | |
32 | } | |
33 | ||
34 | static 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(); | |
037b0b86 | 44 | request_module("tcp-ulp-%s", name); |
734942cc DW |
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 | */ | |
59 | int tcp_register_ulp(struct tcp_ulp_ops *ulp) | |
60 | { | |
61 | int ret = 0; | |
62 | ||
63 | spin_lock(&tcp_ulp_list_lock); | |
b11a632c | 64 | if (tcp_ulp_find(ulp->name)) |
734942cc | 65 | ret = -EEXIST; |
b11a632c | 66 | else |
734942cc | 67 | list_add_tail_rcu(&ulp->list, &tcp_ulp_list); |
734942cc DW |
68 | spin_unlock(&tcp_ulp_list_lock); |
69 | ||
70 | return ret; | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(tcp_register_ulp); | |
73 | ||
74 | void 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 | } | |
82 | EXPORT_SYMBOL_GPL(tcp_unregister_ulp); | |
83 | ||
84 | /* Build string with list of available upper layer protocl values */ | |
85 | void tcp_get_available_ulp(char *buf, size_t maxlen) | |
86 | { | |
87 | struct tcp_ulp_ops *ulp_ops; | |
88 | size_t offs = 0; | |
89 | ||
926f38e9 | 90 | *buf = '\0'; |
734942cc DW |
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); | |
9bb59a21 HL |
96 | |
97 | if (WARN_ON_ONCE(offs >= maxlen)) | |
98 | break; | |
734942cc DW |
99 | } |
100 | rcu_read_unlock(); | |
101 | } | |
102 | ||
33bfe20d JF |
103 | void tcp_update_ulp(struct sock *sk, struct proto *proto, |
104 | void (*write_space)(struct sock *sk)) | |
95fa1454 JF |
105 | { |
106 | struct inet_connection_sock *icsk = inet_csk(sk); | |
107 | ||
95fa1454 | 108 | if (icsk->icsk_ulp_ops->update) |
33bfe20d | 109 | icsk->icsk_ulp_ops->update(sk, proto, write_space); |
95fa1454 JF |
110 | } |
111 | ||
734942cc DW |
112 | void tcp_cleanup_ulp(struct sock *sk) |
113 | { | |
114 | struct inet_connection_sock *icsk = inet_csk(sk); | |
115 | ||
aadd4355 DB |
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 | */ | |
734942cc DW |
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); | |
90545cdc DB |
126 | |
127 | icsk->icsk_ulp_ops = NULL; | |
734942cc DW |
128 | } |
129 | ||
1243a51f | 130 | static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops) |
734942cc DW |
131 | { |
132 | struct inet_connection_sock *icsk = inet_csk(sk); | |
1243a51f | 133 | int err; |
734942cc | 134 | |
1243a51f | 135 | err = -EEXIST; |
734942cc | 136 | if (icsk->icsk_ulp_ops) |
1243a51f | 137 | goto out_err; |
b11a632c | 138 | |
e276d62d PB |
139 | if (sk->sk_socket) |
140 | clear_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags); | |
141 | ||
b11a632c | 142 | err = ulp_ops->init(sk); |
1243a51f DB |
143 | if (err) |
144 | goto out_err; | |
b11a632c JF |
145 | |
146 | icsk->icsk_ulp_ops = ulp_ops; | |
147 | return 0; | |
1243a51f DB |
148 | out_err: |
149 | module_put(ulp_ops->owner); | |
150 | return err; | |
b11a632c JF |
151 | } |
152 | ||
1243a51f | 153 | int tcp_set_ulp(struct sock *sk, const char *name) |
b11a632c | 154 | { |
b11a632c | 155 | const struct tcp_ulp_ops *ulp_ops; |
b11a632c | 156 | |
8b9088f8 | 157 | sock_owned_by_me(sk); |
b11a632c | 158 | |
1243a51f | 159 | ulp_ops = __tcp_ulp_find_autoload(name); |
b11a632c JF |
160 | if (!ulp_ops) |
161 | return -ENOENT; | |
162 | ||
1243a51f | 163 | return __tcp_set_ulp(sk, ulp_ops); |
734942cc | 164 | } |