2 * Pluggable TCP upper layer protocol support.
4 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
9 #include<linux/module.h>
11 #include <linux/types.h>
12 #include <linux/list.h>
13 #include <linux/gfp.h>
16 static DEFINE_SPINLOCK(tcp_ulp_list_lock
);
17 static LIST_HEAD(tcp_ulp_list
);
19 /* Simple linear search, don't expect many entries! */
20 static struct tcp_ulp_ops
*tcp_ulp_find(const char *name
)
22 struct tcp_ulp_ops
*e
;
24 list_for_each_entry_rcu(e
, &tcp_ulp_list
, list
) {
25 if (strcmp(e
->name
, name
) == 0)
32 static struct tcp_ulp_ops
*tcp_ulp_find_id(const int ulp
)
34 struct tcp_ulp_ops
*e
;
36 list_for_each_entry_rcu(e
, &tcp_ulp_list
, list
) {
44 static const struct tcp_ulp_ops
*__tcp_ulp_find_autoload(const char *name
)
46 const struct tcp_ulp_ops
*ulp
= NULL
;
49 ulp
= tcp_ulp_find(name
);
52 if (!ulp
&& capable(CAP_NET_ADMIN
)) {
54 request_module("%s", name
);
56 ulp
= tcp_ulp_find(name
);
59 if (!ulp
|| !try_module_get(ulp
->owner
))
66 static const struct tcp_ulp_ops
*__tcp_ulp_lookup(const int uid
)
68 const struct tcp_ulp_ops
*ulp
;
71 ulp
= tcp_ulp_find_id(uid
);
72 if (!ulp
|| !try_module_get(ulp
->owner
))
78 /* Attach new upper layer protocol to the list
79 * of available protocols.
81 int tcp_register_ulp(struct tcp_ulp_ops
*ulp
)
85 spin_lock(&tcp_ulp_list_lock
);
86 if (tcp_ulp_find(ulp
->name
))
89 list_add_tail_rcu(&ulp
->list
, &tcp_ulp_list
);
90 spin_unlock(&tcp_ulp_list_lock
);
94 EXPORT_SYMBOL_GPL(tcp_register_ulp
);
96 void tcp_unregister_ulp(struct tcp_ulp_ops
*ulp
)
98 spin_lock(&tcp_ulp_list_lock
);
99 list_del_rcu(&ulp
->list
);
100 spin_unlock(&tcp_ulp_list_lock
);
104 EXPORT_SYMBOL_GPL(tcp_unregister_ulp
);
106 /* Build string with list of available upper layer protocl values */
107 void tcp_get_available_ulp(char *buf
, size_t maxlen
)
109 struct tcp_ulp_ops
*ulp_ops
;
114 list_for_each_entry_rcu(ulp_ops
, &tcp_ulp_list
, list
) {
115 offs
+= snprintf(buf
+ offs
, maxlen
- offs
,
117 offs
== 0 ? "" : " ", ulp_ops
->name
);
122 void tcp_cleanup_ulp(struct sock
*sk
)
124 struct inet_connection_sock
*icsk
= inet_csk(sk
);
126 if (!icsk
->icsk_ulp_ops
)
129 if (icsk
->icsk_ulp_ops
->release
)
130 icsk
->icsk_ulp_ops
->release(sk
);
131 module_put(icsk
->icsk_ulp_ops
->owner
);
134 /* Change upper layer protocol for socket */
135 int tcp_set_ulp(struct sock
*sk
, const char *name
)
137 struct inet_connection_sock
*icsk
= inet_csk(sk
);
138 const struct tcp_ulp_ops
*ulp_ops
;
141 if (icsk
->icsk_ulp_ops
)
144 ulp_ops
= __tcp_ulp_find_autoload(name
);
148 if (!ulp_ops
->user_visible
) {
149 module_put(ulp_ops
->owner
);
153 err
= ulp_ops
->init(sk
);
155 module_put(ulp_ops
->owner
);
159 icsk
->icsk_ulp_ops
= ulp_ops
;
163 int tcp_set_ulp_id(struct sock
*sk
, int ulp
)
165 struct inet_connection_sock
*icsk
= inet_csk(sk
);
166 const struct tcp_ulp_ops
*ulp_ops
;
169 if (icsk
->icsk_ulp_ops
)
172 ulp_ops
= __tcp_ulp_lookup(ulp
);
176 err
= ulp_ops
->init(sk
);
178 module_put(ulp_ops
->owner
);
182 icsk
->icsk_ulp_ops
= ulp_ops
;