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.
6 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. 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 if (strcmp(e
->name
, name
) == 0)
33 static const struct tcp_ulp_ops
*__tcp_ulp_find_autoload(const char *name
)
35 const struct tcp_ulp_ops
*ulp
= NULL
;
38 ulp
= tcp_ulp_find(name
);
41 if (!ulp
&& capable(CAP_NET_ADMIN
)) {
43 request_module("tcp-ulp-%s", name
);
45 ulp
= tcp_ulp_find(name
);
48 if (!ulp
|| !try_module_get(ulp
->owner
))
55 /* Attach new upper layer protocol to the list
56 * of available protocols.
58 int tcp_register_ulp(struct tcp_ulp_ops
*ulp
)
62 spin_lock(&tcp_ulp_list_lock
);
63 if (tcp_ulp_find(ulp
->name
))
66 list_add_tail_rcu(&ulp
->list
, &tcp_ulp_list
);
67 spin_unlock(&tcp_ulp_list_lock
);
71 EXPORT_SYMBOL_GPL(tcp_register_ulp
);
73 void tcp_unregister_ulp(struct tcp_ulp_ops
*ulp
)
75 spin_lock(&tcp_ulp_list_lock
);
76 list_del_rcu(&ulp
->list
);
77 spin_unlock(&tcp_ulp_list_lock
);
81 EXPORT_SYMBOL_GPL(tcp_unregister_ulp
);
83 /* Build string with list of available upper layer protocl values */
84 void tcp_get_available_ulp(char *buf
, size_t maxlen
)
86 struct tcp_ulp_ops
*ulp_ops
;
91 list_for_each_entry_rcu(ulp_ops
, &tcp_ulp_list
, list
) {
92 offs
+= snprintf(buf
+ offs
, maxlen
- offs
,
94 offs
== 0 ? "" : " ", ulp_ops
->name
);
96 if (WARN_ON_ONCE(offs
>= maxlen
))
102 void tcp_update_ulp(struct sock
*sk
, struct proto
*proto
,
103 void (*write_space
)(struct sock
*sk
))
105 struct inet_connection_sock
*icsk
= inet_csk(sk
);
107 if (!icsk
->icsk_ulp_ops
) {
108 sk
->sk_write_space
= write_space
;
113 if (icsk
->icsk_ulp_ops
->update
)
114 icsk
->icsk_ulp_ops
->update(sk
, proto
, write_space
);
117 void tcp_cleanup_ulp(struct sock
*sk
)
119 struct inet_connection_sock
*icsk
= inet_csk(sk
);
121 /* No sock_owned_by_me() check here as at the time the
122 * stack calls this function, the socket is dead and
123 * about to be destroyed.
125 if (!icsk
->icsk_ulp_ops
)
128 if (icsk
->icsk_ulp_ops
->release
)
129 icsk
->icsk_ulp_ops
->release(sk
);
130 module_put(icsk
->icsk_ulp_ops
->owner
);
132 icsk
->icsk_ulp_ops
= NULL
;
135 static int __tcp_set_ulp(struct sock
*sk
, const struct tcp_ulp_ops
*ulp_ops
)
137 struct inet_connection_sock
*icsk
= inet_csk(sk
);
141 if (icsk
->icsk_ulp_ops
)
144 err
= ulp_ops
->init(sk
);
148 icsk
->icsk_ulp_ops
= ulp_ops
;
151 module_put(ulp_ops
->owner
);
155 int tcp_set_ulp(struct sock
*sk
, const char *name
)
157 const struct tcp_ulp_ops
*ulp_ops
;
159 sock_owned_by_me(sk
);
161 ulp_ops
= __tcp_ulp_find_autoload(name
);
165 return __tcp_set_ulp(sk
, ulp_ops
);