Merge branch 'hns3-Some-cleanup-and-bugfix-for-desc-filling'
[linux-2.6-block.git] / net / ipv4 / tcp_ulp.c
CommitLineData
734942cc
DW
1/*
2 * Pluggable TCP upper layer protocol support.
3 *
4 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
5 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
6 *
7 */
8
1243a51f 9#include <linux/module.h>
734942cc
DW
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>
15
16static DEFINE_SPINLOCK(tcp_ulp_list_lock);
17static LIST_HEAD(tcp_ulp_list);
18
19/* Simple linear search, don't expect many entries! */
20static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
21{
22 struct tcp_ulp_ops *e;
23
24 list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
25 if (strcmp(e->name, name) == 0)
26 return e;
27 }
28
29 return NULL;
30}
31
32static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
33{
34 const struct tcp_ulp_ops *ulp = NULL;
35
36 rcu_read_lock();
37 ulp = tcp_ulp_find(name);
38
39#ifdef CONFIG_MODULES
40 if (!ulp && capable(CAP_NET_ADMIN)) {
41 rcu_read_unlock();
037b0b86 42 request_module("tcp-ulp-%s", name);
734942cc
DW
43 rcu_read_lock();
44 ulp = tcp_ulp_find(name);
45 }
46#endif
47 if (!ulp || !try_module_get(ulp->owner))
48 ulp = NULL;
49
50 rcu_read_unlock();
51 return ulp;
52}
53
54/* Attach new upper layer protocol to the list
55 * of available protocols.
56 */
57int tcp_register_ulp(struct tcp_ulp_ops *ulp)
58{
59 int ret = 0;
60
61 spin_lock(&tcp_ulp_list_lock);
b11a632c 62 if (tcp_ulp_find(ulp->name))
734942cc 63 ret = -EEXIST;
b11a632c 64 else
734942cc 65 list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
734942cc
DW
66 spin_unlock(&tcp_ulp_list_lock);
67
68 return ret;
69}
70EXPORT_SYMBOL_GPL(tcp_register_ulp);
71
72void tcp_unregister_ulp(struct tcp_ulp_ops *ulp)
73{
74 spin_lock(&tcp_ulp_list_lock);
75 list_del_rcu(&ulp->list);
76 spin_unlock(&tcp_ulp_list_lock);
77
78 synchronize_rcu();
79}
80EXPORT_SYMBOL_GPL(tcp_unregister_ulp);
81
82/* Build string with list of available upper layer protocl values */
83void tcp_get_available_ulp(char *buf, size_t maxlen)
84{
85 struct tcp_ulp_ops *ulp_ops;
86 size_t offs = 0;
87
926f38e9 88 *buf = '\0';
734942cc
DW
89 rcu_read_lock();
90 list_for_each_entry_rcu(ulp_ops, &tcp_ulp_list, list) {
91 offs += snprintf(buf + offs, maxlen - offs,
92 "%s%s",
93 offs == 0 ? "" : " ", ulp_ops->name);
94 }
95 rcu_read_unlock();
96}
97
98void tcp_cleanup_ulp(struct sock *sk)
99{
100 struct inet_connection_sock *icsk = inet_csk(sk);
101
8b9088f8
DB
102 sock_owned_by_me(sk);
103
734942cc
DW
104 if (!icsk->icsk_ulp_ops)
105 return;
106
107 if (icsk->icsk_ulp_ops->release)
108 icsk->icsk_ulp_ops->release(sk);
109 module_put(icsk->icsk_ulp_ops->owner);
90545cdc
DB
110
111 icsk->icsk_ulp_ops = NULL;
734942cc
DW
112}
113
1243a51f 114static int __tcp_set_ulp(struct sock *sk, const struct tcp_ulp_ops *ulp_ops)
734942cc
DW
115{
116 struct inet_connection_sock *icsk = inet_csk(sk);
1243a51f 117 int err;
734942cc 118
1243a51f 119 err = -EEXIST;
734942cc 120 if (icsk->icsk_ulp_ops)
1243a51f 121 goto out_err;
b11a632c
JF
122
123 err = ulp_ops->init(sk);
1243a51f
DB
124 if (err)
125 goto out_err;
b11a632c
JF
126
127 icsk->icsk_ulp_ops = ulp_ops;
128 return 0;
1243a51f
DB
129out_err:
130 module_put(ulp_ops->owner);
131 return err;
b11a632c
JF
132}
133
1243a51f 134int tcp_set_ulp(struct sock *sk, const char *name)
b11a632c 135{
b11a632c 136 const struct tcp_ulp_ops *ulp_ops;
b11a632c 137
8b9088f8 138 sock_owned_by_me(sk);
b11a632c 139
1243a51f 140 ulp_ops = __tcp_ulp_find_autoload(name);
b11a632c
JF
141 if (!ulp_ops)
142 return -ENOENT;
143
1243a51f 144 return __tcp_set_ulp(sk, ulp_ops);
734942cc 145}