ARM: amba: Make driver_override output consistent with other buses
[linux/fpc-iii.git] / net / ipv4 / tcp_ulp.c
blob622caa4039e025d70d1b0592fab554ab747dc666
1 /*
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.
7 */
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>
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)
26 return e;
29 return NULL;
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) {
37 if (e->uid == ulp)
38 return e;
41 return NULL;
44 static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
46 const struct tcp_ulp_ops *ulp = NULL;
48 rcu_read_lock();
49 ulp = tcp_ulp_find(name);
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);
58 #endif
59 if (!ulp || !try_module_get(ulp->owner))
60 ulp = NULL;
62 rcu_read_unlock();
63 return ulp;
66 static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
68 const struct tcp_ulp_ops *ulp;
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;
78 /* Attach new upper layer protocol to the list
79 * of available protocols.
81 int tcp_register_ulp(struct tcp_ulp_ops *ulp)
83 int ret = 0;
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);
92 return ret;
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);
102 synchronize_rcu();
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;
110 size_t offs = 0;
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);
119 rcu_read_unlock();
122 void tcp_cleanup_ulp(struct sock *sk)
124 struct inet_connection_sock *icsk = inet_csk(sk);
126 if (!icsk->icsk_ulp_ops)
127 return;
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;
139 int err = 0;
141 if (icsk->icsk_ulp_ops)
142 return -EEXIST;
144 ulp_ops = __tcp_ulp_find_autoload(name);
145 if (!ulp_ops)
146 return -ENOENT;
148 if (!ulp_ops->user_visible) {
149 module_put(ulp_ops->owner);
150 return -ENOENT;
153 err = ulp_ops->init(sk);
154 if (err) {
155 module_put(ulp_ops->owner);
156 return err;
159 icsk->icsk_ulp_ops = ulp_ops;
160 return 0;
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;
167 int err;
169 if (icsk->icsk_ulp_ops)
170 return -EEXIST;
172 ulp_ops = __tcp_ulp_lookup(ulp);
173 if (!ulp_ops)
174 return -ENOENT;
176 err = ulp_ops->init(sk);
177 if (err) {
178 module_put(ulp_ops->owner);
179 return err;
182 icsk->icsk_ulp_ops = ulp_ops;
183 return 0;