Commit db40980fcdb560d7992b0511df16cdd3f7e381f3
Committed by
David S. Miller
1 parent
d61702f127
Exists in
master
and in
39 other branches
net: poll() optimizations
No need to test twice sk->sk_shutdown & RCV_SHUTDOWN Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Showing 4 changed files with 8 additions and 12 deletions Inline Diff
net/bluetooth/af_bluetooth.c
1 | /* | 1 | /* |
2 | BlueZ - Bluetooth protocol stack for Linux | 2 | BlueZ - Bluetooth protocol stack for Linux |
3 | Copyright (C) 2000-2001 Qualcomm Incorporated | 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
4 | 4 | ||
5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> | 5 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
6 | 6 | ||
7 | This program is free software; you can redistribute it and/or modify | 7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License version 2 as | 8 | it under the terms of the GNU General Public License version 2 as |
9 | published by the Free Software Foundation; | 9 | published by the Free Software Foundation; |
10 | 10 | ||
11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. | 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY | 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES | 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
19 | 19 | ||
20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
22 | SOFTWARE IS DISCLAIMED. | 22 | SOFTWARE IS DISCLAIMED. |
23 | */ | 23 | */ |
24 | 24 | ||
25 | /* Bluetooth address family and sockets. */ | 25 | /* Bluetooth address family and sockets. */ |
26 | 26 | ||
27 | #include <linux/module.h> | 27 | #include <linux/module.h> |
28 | 28 | ||
29 | #include <linux/types.h> | 29 | #include <linux/types.h> |
30 | #include <linux/list.h> | 30 | #include <linux/list.h> |
31 | #include <linux/errno.h> | 31 | #include <linux/errno.h> |
32 | #include <linux/kernel.h> | 32 | #include <linux/kernel.h> |
33 | #include <linux/sched.h> | 33 | #include <linux/sched.h> |
34 | #include <linux/skbuff.h> | 34 | #include <linux/skbuff.h> |
35 | #include <linux/init.h> | 35 | #include <linux/init.h> |
36 | #include <linux/poll.h> | 36 | #include <linux/poll.h> |
37 | #include <net/sock.h> | 37 | #include <net/sock.h> |
38 | #include <asm/ioctls.h> | 38 | #include <asm/ioctls.h> |
39 | #include <linux/kmod.h> | 39 | #include <linux/kmod.h> |
40 | 40 | ||
41 | #include <net/bluetooth/bluetooth.h> | 41 | #include <net/bluetooth/bluetooth.h> |
42 | 42 | ||
43 | #define VERSION "2.15" | 43 | #define VERSION "2.15" |
44 | 44 | ||
45 | /* Bluetooth sockets */ | 45 | /* Bluetooth sockets */ |
46 | #define BT_MAX_PROTO 8 | 46 | #define BT_MAX_PROTO 8 |
47 | static const struct net_proto_family *bt_proto[BT_MAX_PROTO]; | 47 | static const struct net_proto_family *bt_proto[BT_MAX_PROTO]; |
48 | static DEFINE_RWLOCK(bt_proto_lock); | 48 | static DEFINE_RWLOCK(bt_proto_lock); |
49 | 49 | ||
50 | static struct lock_class_key bt_lock_key[BT_MAX_PROTO]; | 50 | static struct lock_class_key bt_lock_key[BT_MAX_PROTO]; |
51 | static const char *const bt_key_strings[BT_MAX_PROTO] = { | 51 | static const char *const bt_key_strings[BT_MAX_PROTO] = { |
52 | "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP", | 52 | "sk_lock-AF_BLUETOOTH-BTPROTO_L2CAP", |
53 | "sk_lock-AF_BLUETOOTH-BTPROTO_HCI", | 53 | "sk_lock-AF_BLUETOOTH-BTPROTO_HCI", |
54 | "sk_lock-AF_BLUETOOTH-BTPROTO_SCO", | 54 | "sk_lock-AF_BLUETOOTH-BTPROTO_SCO", |
55 | "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM", | 55 | "sk_lock-AF_BLUETOOTH-BTPROTO_RFCOMM", |
56 | "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP", | 56 | "sk_lock-AF_BLUETOOTH-BTPROTO_BNEP", |
57 | "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP", | 57 | "sk_lock-AF_BLUETOOTH-BTPROTO_CMTP", |
58 | "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP", | 58 | "sk_lock-AF_BLUETOOTH-BTPROTO_HIDP", |
59 | "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP", | 59 | "sk_lock-AF_BLUETOOTH-BTPROTO_AVDTP", |
60 | }; | 60 | }; |
61 | 61 | ||
62 | static struct lock_class_key bt_slock_key[BT_MAX_PROTO]; | 62 | static struct lock_class_key bt_slock_key[BT_MAX_PROTO]; |
63 | static const char *const bt_slock_key_strings[BT_MAX_PROTO] = { | 63 | static const char *const bt_slock_key_strings[BT_MAX_PROTO] = { |
64 | "slock-AF_BLUETOOTH-BTPROTO_L2CAP", | 64 | "slock-AF_BLUETOOTH-BTPROTO_L2CAP", |
65 | "slock-AF_BLUETOOTH-BTPROTO_HCI", | 65 | "slock-AF_BLUETOOTH-BTPROTO_HCI", |
66 | "slock-AF_BLUETOOTH-BTPROTO_SCO", | 66 | "slock-AF_BLUETOOTH-BTPROTO_SCO", |
67 | "slock-AF_BLUETOOTH-BTPROTO_RFCOMM", | 67 | "slock-AF_BLUETOOTH-BTPROTO_RFCOMM", |
68 | "slock-AF_BLUETOOTH-BTPROTO_BNEP", | 68 | "slock-AF_BLUETOOTH-BTPROTO_BNEP", |
69 | "slock-AF_BLUETOOTH-BTPROTO_CMTP", | 69 | "slock-AF_BLUETOOTH-BTPROTO_CMTP", |
70 | "slock-AF_BLUETOOTH-BTPROTO_HIDP", | 70 | "slock-AF_BLUETOOTH-BTPROTO_HIDP", |
71 | "slock-AF_BLUETOOTH-BTPROTO_AVDTP", | 71 | "slock-AF_BLUETOOTH-BTPROTO_AVDTP", |
72 | }; | 72 | }; |
73 | 73 | ||
74 | static inline void bt_sock_reclassify_lock(struct socket *sock, int proto) | 74 | static inline void bt_sock_reclassify_lock(struct socket *sock, int proto) |
75 | { | 75 | { |
76 | struct sock *sk = sock->sk; | 76 | struct sock *sk = sock->sk; |
77 | 77 | ||
78 | if (!sk) | 78 | if (!sk) |
79 | return; | 79 | return; |
80 | 80 | ||
81 | BUG_ON(sock_owned_by_user(sk)); | 81 | BUG_ON(sock_owned_by_user(sk)); |
82 | 82 | ||
83 | sock_lock_init_class_and_name(sk, | 83 | sock_lock_init_class_and_name(sk, |
84 | bt_slock_key_strings[proto], &bt_slock_key[proto], | 84 | bt_slock_key_strings[proto], &bt_slock_key[proto], |
85 | bt_key_strings[proto], &bt_lock_key[proto]); | 85 | bt_key_strings[proto], &bt_lock_key[proto]); |
86 | } | 86 | } |
87 | 87 | ||
88 | int bt_sock_register(int proto, const struct net_proto_family *ops) | 88 | int bt_sock_register(int proto, const struct net_proto_family *ops) |
89 | { | 89 | { |
90 | int err = 0; | 90 | int err = 0; |
91 | 91 | ||
92 | if (proto < 0 || proto >= BT_MAX_PROTO) | 92 | if (proto < 0 || proto >= BT_MAX_PROTO) |
93 | return -EINVAL; | 93 | return -EINVAL; |
94 | 94 | ||
95 | write_lock(&bt_proto_lock); | 95 | write_lock(&bt_proto_lock); |
96 | 96 | ||
97 | if (bt_proto[proto]) | 97 | if (bt_proto[proto]) |
98 | err = -EEXIST; | 98 | err = -EEXIST; |
99 | else | 99 | else |
100 | bt_proto[proto] = ops; | 100 | bt_proto[proto] = ops; |
101 | 101 | ||
102 | write_unlock(&bt_proto_lock); | 102 | write_unlock(&bt_proto_lock); |
103 | 103 | ||
104 | return err; | 104 | return err; |
105 | } | 105 | } |
106 | EXPORT_SYMBOL(bt_sock_register); | 106 | EXPORT_SYMBOL(bt_sock_register); |
107 | 107 | ||
108 | int bt_sock_unregister(int proto) | 108 | int bt_sock_unregister(int proto) |
109 | { | 109 | { |
110 | int err = 0; | 110 | int err = 0; |
111 | 111 | ||
112 | if (proto < 0 || proto >= BT_MAX_PROTO) | 112 | if (proto < 0 || proto >= BT_MAX_PROTO) |
113 | return -EINVAL; | 113 | return -EINVAL; |
114 | 114 | ||
115 | write_lock(&bt_proto_lock); | 115 | write_lock(&bt_proto_lock); |
116 | 116 | ||
117 | if (!bt_proto[proto]) | 117 | if (!bt_proto[proto]) |
118 | err = -ENOENT; | 118 | err = -ENOENT; |
119 | else | 119 | else |
120 | bt_proto[proto] = NULL; | 120 | bt_proto[proto] = NULL; |
121 | 121 | ||
122 | write_unlock(&bt_proto_lock); | 122 | write_unlock(&bt_proto_lock); |
123 | 123 | ||
124 | return err; | 124 | return err; |
125 | } | 125 | } |
126 | EXPORT_SYMBOL(bt_sock_unregister); | 126 | EXPORT_SYMBOL(bt_sock_unregister); |
127 | 127 | ||
128 | static int bt_sock_create(struct net *net, struct socket *sock, int proto, | 128 | static int bt_sock_create(struct net *net, struct socket *sock, int proto, |
129 | int kern) | 129 | int kern) |
130 | { | 130 | { |
131 | int err; | 131 | int err; |
132 | 132 | ||
133 | if (net != &init_net) | 133 | if (net != &init_net) |
134 | return -EAFNOSUPPORT; | 134 | return -EAFNOSUPPORT; |
135 | 135 | ||
136 | if (proto < 0 || proto >= BT_MAX_PROTO) | 136 | if (proto < 0 || proto >= BT_MAX_PROTO) |
137 | return -EINVAL; | 137 | return -EINVAL; |
138 | 138 | ||
139 | if (!bt_proto[proto]) | 139 | if (!bt_proto[proto]) |
140 | request_module("bt-proto-%d", proto); | 140 | request_module("bt-proto-%d", proto); |
141 | 141 | ||
142 | err = -EPROTONOSUPPORT; | 142 | err = -EPROTONOSUPPORT; |
143 | 143 | ||
144 | read_lock(&bt_proto_lock); | 144 | read_lock(&bt_proto_lock); |
145 | 145 | ||
146 | if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) { | 146 | if (bt_proto[proto] && try_module_get(bt_proto[proto]->owner)) { |
147 | err = bt_proto[proto]->create(net, sock, proto, kern); | 147 | err = bt_proto[proto]->create(net, sock, proto, kern); |
148 | bt_sock_reclassify_lock(sock, proto); | 148 | bt_sock_reclassify_lock(sock, proto); |
149 | module_put(bt_proto[proto]->owner); | 149 | module_put(bt_proto[proto]->owner); |
150 | } | 150 | } |
151 | 151 | ||
152 | read_unlock(&bt_proto_lock); | 152 | read_unlock(&bt_proto_lock); |
153 | 153 | ||
154 | return err; | 154 | return err; |
155 | } | 155 | } |
156 | 156 | ||
157 | void bt_sock_link(struct bt_sock_list *l, struct sock *sk) | 157 | void bt_sock_link(struct bt_sock_list *l, struct sock *sk) |
158 | { | 158 | { |
159 | write_lock_bh(&l->lock); | 159 | write_lock_bh(&l->lock); |
160 | sk_add_node(sk, &l->head); | 160 | sk_add_node(sk, &l->head); |
161 | write_unlock_bh(&l->lock); | 161 | write_unlock_bh(&l->lock); |
162 | } | 162 | } |
163 | EXPORT_SYMBOL(bt_sock_link); | 163 | EXPORT_SYMBOL(bt_sock_link); |
164 | 164 | ||
165 | void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk) | 165 | void bt_sock_unlink(struct bt_sock_list *l, struct sock *sk) |
166 | { | 166 | { |
167 | write_lock_bh(&l->lock); | 167 | write_lock_bh(&l->lock); |
168 | sk_del_node_init(sk); | 168 | sk_del_node_init(sk); |
169 | write_unlock_bh(&l->lock); | 169 | write_unlock_bh(&l->lock); |
170 | } | 170 | } |
171 | EXPORT_SYMBOL(bt_sock_unlink); | 171 | EXPORT_SYMBOL(bt_sock_unlink); |
172 | 172 | ||
173 | void bt_accept_enqueue(struct sock *parent, struct sock *sk) | 173 | void bt_accept_enqueue(struct sock *parent, struct sock *sk) |
174 | { | 174 | { |
175 | BT_DBG("parent %p, sk %p", parent, sk); | 175 | BT_DBG("parent %p, sk %p", parent, sk); |
176 | 176 | ||
177 | sock_hold(sk); | 177 | sock_hold(sk); |
178 | list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q); | 178 | list_add_tail(&bt_sk(sk)->accept_q, &bt_sk(parent)->accept_q); |
179 | bt_sk(sk)->parent = parent; | 179 | bt_sk(sk)->parent = parent; |
180 | parent->sk_ack_backlog++; | 180 | parent->sk_ack_backlog++; |
181 | } | 181 | } |
182 | EXPORT_SYMBOL(bt_accept_enqueue); | 182 | EXPORT_SYMBOL(bt_accept_enqueue); |
183 | 183 | ||
184 | void bt_accept_unlink(struct sock *sk) | 184 | void bt_accept_unlink(struct sock *sk) |
185 | { | 185 | { |
186 | BT_DBG("sk %p state %d", sk, sk->sk_state); | 186 | BT_DBG("sk %p state %d", sk, sk->sk_state); |
187 | 187 | ||
188 | list_del_init(&bt_sk(sk)->accept_q); | 188 | list_del_init(&bt_sk(sk)->accept_q); |
189 | bt_sk(sk)->parent->sk_ack_backlog--; | 189 | bt_sk(sk)->parent->sk_ack_backlog--; |
190 | bt_sk(sk)->parent = NULL; | 190 | bt_sk(sk)->parent = NULL; |
191 | sock_put(sk); | 191 | sock_put(sk); |
192 | } | 192 | } |
193 | EXPORT_SYMBOL(bt_accept_unlink); | 193 | EXPORT_SYMBOL(bt_accept_unlink); |
194 | 194 | ||
195 | struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock) | 195 | struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock) |
196 | { | 196 | { |
197 | struct list_head *p, *n; | 197 | struct list_head *p, *n; |
198 | struct sock *sk; | 198 | struct sock *sk; |
199 | 199 | ||
200 | BT_DBG("parent %p", parent); | 200 | BT_DBG("parent %p", parent); |
201 | 201 | ||
202 | list_for_each_safe(p, n, &bt_sk(parent)->accept_q) { | 202 | list_for_each_safe(p, n, &bt_sk(parent)->accept_q) { |
203 | sk = (struct sock *) list_entry(p, struct bt_sock, accept_q); | 203 | sk = (struct sock *) list_entry(p, struct bt_sock, accept_q); |
204 | 204 | ||
205 | lock_sock(sk); | 205 | lock_sock(sk); |
206 | 206 | ||
207 | /* FIXME: Is this check still needed */ | 207 | /* FIXME: Is this check still needed */ |
208 | if (sk->sk_state == BT_CLOSED) { | 208 | if (sk->sk_state == BT_CLOSED) { |
209 | release_sock(sk); | 209 | release_sock(sk); |
210 | bt_accept_unlink(sk); | 210 | bt_accept_unlink(sk); |
211 | continue; | 211 | continue; |
212 | } | 212 | } |
213 | 213 | ||
214 | if (sk->sk_state == BT_CONNECTED || !newsock || | 214 | if (sk->sk_state == BT_CONNECTED || !newsock || |
215 | bt_sk(parent)->defer_setup) { | 215 | bt_sk(parent)->defer_setup) { |
216 | bt_accept_unlink(sk); | 216 | bt_accept_unlink(sk); |
217 | if (newsock) | 217 | if (newsock) |
218 | sock_graft(sk, newsock); | 218 | sock_graft(sk, newsock); |
219 | release_sock(sk); | 219 | release_sock(sk); |
220 | return sk; | 220 | return sk; |
221 | } | 221 | } |
222 | 222 | ||
223 | release_sock(sk); | 223 | release_sock(sk); |
224 | } | 224 | } |
225 | return NULL; | 225 | return NULL; |
226 | } | 226 | } |
227 | EXPORT_SYMBOL(bt_accept_dequeue); | 227 | EXPORT_SYMBOL(bt_accept_dequeue); |
228 | 228 | ||
229 | int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock, | 229 | int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock, |
230 | struct msghdr *msg, size_t len, int flags) | 230 | struct msghdr *msg, size_t len, int flags) |
231 | { | 231 | { |
232 | int noblock = flags & MSG_DONTWAIT; | 232 | int noblock = flags & MSG_DONTWAIT; |
233 | struct sock *sk = sock->sk; | 233 | struct sock *sk = sock->sk; |
234 | struct sk_buff *skb; | 234 | struct sk_buff *skb; |
235 | size_t copied; | 235 | size_t copied; |
236 | int err; | 236 | int err; |
237 | 237 | ||
238 | BT_DBG("sock %p sk %p len %zu", sock, sk, len); | 238 | BT_DBG("sock %p sk %p len %zu", sock, sk, len); |
239 | 239 | ||
240 | if (flags & (MSG_OOB)) | 240 | if (flags & (MSG_OOB)) |
241 | return -EOPNOTSUPP; | 241 | return -EOPNOTSUPP; |
242 | 242 | ||
243 | if (!(skb = skb_recv_datagram(sk, flags, noblock, &err))) { | 243 | if (!(skb = skb_recv_datagram(sk, flags, noblock, &err))) { |
244 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 244 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
245 | return 0; | 245 | return 0; |
246 | return err; | 246 | return err; |
247 | } | 247 | } |
248 | 248 | ||
249 | msg->msg_namelen = 0; | 249 | msg->msg_namelen = 0; |
250 | 250 | ||
251 | copied = skb->len; | 251 | copied = skb->len; |
252 | if (len < copied) { | 252 | if (len < copied) { |
253 | msg->msg_flags |= MSG_TRUNC; | 253 | msg->msg_flags |= MSG_TRUNC; |
254 | copied = len; | 254 | copied = len; |
255 | } | 255 | } |
256 | 256 | ||
257 | skb_reset_transport_header(skb); | 257 | skb_reset_transport_header(skb); |
258 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | 258 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
259 | if (err == 0) | 259 | if (err == 0) |
260 | sock_recv_ts_and_drops(msg, sk, skb); | 260 | sock_recv_ts_and_drops(msg, sk, skb); |
261 | 261 | ||
262 | skb_free_datagram(sk, skb); | 262 | skb_free_datagram(sk, skb); |
263 | 263 | ||
264 | return err ? : copied; | 264 | return err ? : copied; |
265 | } | 265 | } |
266 | EXPORT_SYMBOL(bt_sock_recvmsg); | 266 | EXPORT_SYMBOL(bt_sock_recvmsg); |
267 | 267 | ||
268 | static inline unsigned int bt_accept_poll(struct sock *parent) | 268 | static inline unsigned int bt_accept_poll(struct sock *parent) |
269 | { | 269 | { |
270 | struct list_head *p, *n; | 270 | struct list_head *p, *n; |
271 | struct sock *sk; | 271 | struct sock *sk; |
272 | 272 | ||
273 | list_for_each_safe(p, n, &bt_sk(parent)->accept_q) { | 273 | list_for_each_safe(p, n, &bt_sk(parent)->accept_q) { |
274 | sk = (struct sock *) list_entry(p, struct bt_sock, accept_q); | 274 | sk = (struct sock *) list_entry(p, struct bt_sock, accept_q); |
275 | if (sk->sk_state == BT_CONNECTED || | 275 | if (sk->sk_state == BT_CONNECTED || |
276 | (bt_sk(parent)->defer_setup && | 276 | (bt_sk(parent)->defer_setup && |
277 | sk->sk_state == BT_CONNECT2)) | 277 | sk->sk_state == BT_CONNECT2)) |
278 | return POLLIN | POLLRDNORM; | 278 | return POLLIN | POLLRDNORM; |
279 | } | 279 | } |
280 | 280 | ||
281 | return 0; | 281 | return 0; |
282 | } | 282 | } |
283 | 283 | ||
284 | unsigned int bt_sock_poll(struct file * file, struct socket *sock, poll_table *wait) | 284 | unsigned int bt_sock_poll(struct file * file, struct socket *sock, poll_table *wait) |
285 | { | 285 | { |
286 | struct sock *sk = sock->sk; | 286 | struct sock *sk = sock->sk; |
287 | unsigned int mask = 0; | 287 | unsigned int mask = 0; |
288 | 288 | ||
289 | BT_DBG("sock %p, sk %p", sock, sk); | 289 | BT_DBG("sock %p, sk %p", sock, sk); |
290 | 290 | ||
291 | poll_wait(file, sk_sleep(sk), wait); | 291 | poll_wait(file, sk_sleep(sk), wait); |
292 | 292 | ||
293 | if (sk->sk_state == BT_LISTEN) | 293 | if (sk->sk_state == BT_LISTEN) |
294 | return bt_accept_poll(sk); | 294 | return bt_accept_poll(sk); |
295 | 295 | ||
296 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 296 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
297 | mask |= POLLERR; | 297 | mask |= POLLERR; |
298 | 298 | ||
299 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 299 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
300 | mask |= POLLRDHUP; | 300 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
301 | 301 | ||
302 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 302 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
303 | mask |= POLLHUP; | 303 | mask |= POLLHUP; |
304 | 304 | ||
305 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 305 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
306 | (sk->sk_shutdown & RCV_SHUTDOWN)) | ||
307 | mask |= POLLIN | POLLRDNORM; | 306 | mask |= POLLIN | POLLRDNORM; |
308 | 307 | ||
309 | if (sk->sk_state == BT_CLOSED) | 308 | if (sk->sk_state == BT_CLOSED) |
310 | mask |= POLLHUP; | 309 | mask |= POLLHUP; |
311 | 310 | ||
312 | if (sk->sk_state == BT_CONNECT || | 311 | if (sk->sk_state == BT_CONNECT || |
313 | sk->sk_state == BT_CONNECT2 || | 312 | sk->sk_state == BT_CONNECT2 || |
314 | sk->sk_state == BT_CONFIG) | 313 | sk->sk_state == BT_CONFIG) |
315 | return mask; | 314 | return mask; |
316 | 315 | ||
317 | if (sock_writeable(sk)) | 316 | if (sock_writeable(sk)) |
318 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | 317 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
319 | else | 318 | else |
320 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | 319 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
321 | 320 | ||
322 | return mask; | 321 | return mask; |
323 | } | 322 | } |
324 | EXPORT_SYMBOL(bt_sock_poll); | 323 | EXPORT_SYMBOL(bt_sock_poll); |
325 | 324 | ||
326 | int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | 325 | int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
327 | { | 326 | { |
328 | struct sock *sk = sock->sk; | 327 | struct sock *sk = sock->sk; |
329 | struct sk_buff *skb; | 328 | struct sk_buff *skb; |
330 | long amount; | 329 | long amount; |
331 | int err; | 330 | int err; |
332 | 331 | ||
333 | BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg); | 332 | BT_DBG("sk %p cmd %x arg %lx", sk, cmd, arg); |
334 | 333 | ||
335 | switch (cmd) { | 334 | switch (cmd) { |
336 | case TIOCOUTQ: | 335 | case TIOCOUTQ: |
337 | if (sk->sk_state == BT_LISTEN) | 336 | if (sk->sk_state == BT_LISTEN) |
338 | return -EINVAL; | 337 | return -EINVAL; |
339 | 338 | ||
340 | amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); | 339 | amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
341 | if (amount < 0) | 340 | if (amount < 0) |
342 | amount = 0; | 341 | amount = 0; |
343 | err = put_user(amount, (int __user *) arg); | 342 | err = put_user(amount, (int __user *) arg); |
344 | break; | 343 | break; |
345 | 344 | ||
346 | case TIOCINQ: | 345 | case TIOCINQ: |
347 | if (sk->sk_state == BT_LISTEN) | 346 | if (sk->sk_state == BT_LISTEN) |
348 | return -EINVAL; | 347 | return -EINVAL; |
349 | 348 | ||
350 | lock_sock(sk); | 349 | lock_sock(sk); |
351 | skb = skb_peek(&sk->sk_receive_queue); | 350 | skb = skb_peek(&sk->sk_receive_queue); |
352 | amount = skb ? skb->len : 0; | 351 | amount = skb ? skb->len : 0; |
353 | release_sock(sk); | 352 | release_sock(sk); |
354 | err = put_user(amount, (int __user *) arg); | 353 | err = put_user(amount, (int __user *) arg); |
355 | break; | 354 | break; |
356 | 355 | ||
357 | case SIOCGSTAMP: | 356 | case SIOCGSTAMP: |
358 | err = sock_get_timestamp(sk, (struct timeval __user *) arg); | 357 | err = sock_get_timestamp(sk, (struct timeval __user *) arg); |
359 | break; | 358 | break; |
360 | 359 | ||
361 | case SIOCGSTAMPNS: | 360 | case SIOCGSTAMPNS: |
362 | err = sock_get_timestampns(sk, (struct timespec __user *) arg); | 361 | err = sock_get_timestampns(sk, (struct timespec __user *) arg); |
363 | break; | 362 | break; |
364 | 363 | ||
365 | default: | 364 | default: |
366 | err = -ENOIOCTLCMD; | 365 | err = -ENOIOCTLCMD; |
367 | break; | 366 | break; |
368 | } | 367 | } |
369 | 368 | ||
370 | return err; | 369 | return err; |
371 | } | 370 | } |
372 | EXPORT_SYMBOL(bt_sock_ioctl); | 371 | EXPORT_SYMBOL(bt_sock_ioctl); |
373 | 372 | ||
374 | int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) | 373 | int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) |
375 | { | 374 | { |
376 | DECLARE_WAITQUEUE(wait, current); | 375 | DECLARE_WAITQUEUE(wait, current); |
377 | int err = 0; | 376 | int err = 0; |
378 | 377 | ||
379 | BT_DBG("sk %p", sk); | 378 | BT_DBG("sk %p", sk); |
380 | 379 | ||
381 | add_wait_queue(sk_sleep(sk), &wait); | 380 | add_wait_queue(sk_sleep(sk), &wait); |
382 | while (sk->sk_state != state) { | 381 | while (sk->sk_state != state) { |
383 | set_current_state(TASK_INTERRUPTIBLE); | 382 | set_current_state(TASK_INTERRUPTIBLE); |
384 | 383 | ||
385 | if (!timeo) { | 384 | if (!timeo) { |
386 | err = -EINPROGRESS; | 385 | err = -EINPROGRESS; |
387 | break; | 386 | break; |
388 | } | 387 | } |
389 | 388 | ||
390 | if (signal_pending(current)) { | 389 | if (signal_pending(current)) { |
391 | err = sock_intr_errno(timeo); | 390 | err = sock_intr_errno(timeo); |
392 | break; | 391 | break; |
393 | } | 392 | } |
394 | 393 | ||
395 | release_sock(sk); | 394 | release_sock(sk); |
396 | timeo = schedule_timeout(timeo); | 395 | timeo = schedule_timeout(timeo); |
397 | lock_sock(sk); | 396 | lock_sock(sk); |
398 | 397 | ||
399 | err = sock_error(sk); | 398 | err = sock_error(sk); |
400 | if (err) | 399 | if (err) |
401 | break; | 400 | break; |
402 | } | 401 | } |
403 | set_current_state(TASK_RUNNING); | 402 | set_current_state(TASK_RUNNING); |
404 | remove_wait_queue(sk_sleep(sk), &wait); | 403 | remove_wait_queue(sk_sleep(sk), &wait); |
405 | return err; | 404 | return err; |
406 | } | 405 | } |
407 | EXPORT_SYMBOL(bt_sock_wait_state); | 406 | EXPORT_SYMBOL(bt_sock_wait_state); |
408 | 407 | ||
409 | static struct net_proto_family bt_sock_family_ops = { | 408 | static struct net_proto_family bt_sock_family_ops = { |
410 | .owner = THIS_MODULE, | 409 | .owner = THIS_MODULE, |
411 | .family = PF_BLUETOOTH, | 410 | .family = PF_BLUETOOTH, |
412 | .create = bt_sock_create, | 411 | .create = bt_sock_create, |
413 | }; | 412 | }; |
414 | 413 | ||
415 | static int __init bt_init(void) | 414 | static int __init bt_init(void) |
416 | { | 415 | { |
417 | int err; | 416 | int err; |
418 | 417 | ||
419 | BT_INFO("Core ver %s", VERSION); | 418 | BT_INFO("Core ver %s", VERSION); |
420 | 419 | ||
421 | err = bt_sysfs_init(); | 420 | err = bt_sysfs_init(); |
422 | if (err < 0) | 421 | if (err < 0) |
423 | return err; | 422 | return err; |
424 | 423 | ||
425 | err = sock_register(&bt_sock_family_ops); | 424 | err = sock_register(&bt_sock_family_ops); |
426 | if (err < 0) { | 425 | if (err < 0) { |
427 | bt_sysfs_cleanup(); | 426 | bt_sysfs_cleanup(); |
428 | return err; | 427 | return err; |
429 | } | 428 | } |
430 | 429 | ||
431 | BT_INFO("HCI device and connection manager initialized"); | 430 | BT_INFO("HCI device and connection manager initialized"); |
432 | 431 | ||
433 | hci_sock_init(); | 432 | hci_sock_init(); |
434 | 433 | ||
435 | return 0; | 434 | return 0; |
436 | } | 435 | } |
437 | 436 | ||
438 | static void __exit bt_exit(void) | 437 | static void __exit bt_exit(void) |
439 | { | 438 | { |
440 | hci_sock_cleanup(); | 439 | hci_sock_cleanup(); |
441 | 440 | ||
442 | sock_unregister(PF_BLUETOOTH); | 441 | sock_unregister(PF_BLUETOOTH); |
443 | 442 | ||
444 | bt_sysfs_cleanup(); | 443 | bt_sysfs_cleanup(); |
445 | } | 444 | } |
446 | 445 | ||
447 | subsys_initcall(bt_init); | 446 | subsys_initcall(bt_init); |
448 | module_exit(bt_exit); | 447 | module_exit(bt_exit); |
449 | 448 | ||
450 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); | 449 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
451 | MODULE_DESCRIPTION("Bluetooth Core ver " VERSION); | 450 | MODULE_DESCRIPTION("Bluetooth Core ver " VERSION); |
452 | MODULE_VERSION(VERSION); | 451 | MODULE_VERSION(VERSION); |
453 | MODULE_LICENSE("GPL"); | 452 | MODULE_LICENSE("GPL"); |
454 | MODULE_ALIAS_NETPROTO(PF_BLUETOOTH); | 453 | MODULE_ALIAS_NETPROTO(PF_BLUETOOTH); |
455 | 454 |
net/core/datagram.c
1 | /* | 1 | /* |
2 | * SUCS NET3: | 2 | * SUCS NET3: |
3 | * | 3 | * |
4 | * Generic datagram handling routines. These are generic for all | 4 | * Generic datagram handling routines. These are generic for all |
5 | * protocols. Possibly a generic IP version on top of these would | 5 | * protocols. Possibly a generic IP version on top of these would |
6 | * make sense. Not tonight however 8-). | 6 | * make sense. Not tonight however 8-). |
7 | * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and | 7 | * This is used because UDP, RAW, PACKET, DDP, IPX, AX.25 and |
8 | * NetROM layer all have identical poll code and mostly | 8 | * NetROM layer all have identical poll code and mostly |
9 | * identical recvmsg() code. So we share it here. The poll was | 9 | * identical recvmsg() code. So we share it here. The poll was |
10 | * shared before but buried in udp.c so I moved it. | 10 | * shared before but buried in udp.c so I moved it. |
11 | * | 11 | * |
12 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old | 12 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>. (datagram_poll() from old |
13 | * udp.c code) | 13 | * udp.c code) |
14 | * | 14 | * |
15 | * Fixes: | 15 | * Fixes: |
16 | * Alan Cox : NULL return from skb_peek_copy() | 16 | * Alan Cox : NULL return from skb_peek_copy() |
17 | * understood | 17 | * understood |
18 | * Alan Cox : Rewrote skb_read_datagram to avoid the | 18 | * Alan Cox : Rewrote skb_read_datagram to avoid the |
19 | * skb_peek_copy stuff. | 19 | * skb_peek_copy stuff. |
20 | * Alan Cox : Added support for SOCK_SEQPACKET. | 20 | * Alan Cox : Added support for SOCK_SEQPACKET. |
21 | * IPX can no longer use the SO_TYPE hack | 21 | * IPX can no longer use the SO_TYPE hack |
22 | * but AX.25 now works right, and SPX is | 22 | * but AX.25 now works right, and SPX is |
23 | * feasible. | 23 | * feasible. |
24 | * Alan Cox : Fixed write poll of non IP protocol | 24 | * Alan Cox : Fixed write poll of non IP protocol |
25 | * crash. | 25 | * crash. |
26 | * Florian La Roche: Changed for my new skbuff handling. | 26 | * Florian La Roche: Changed for my new skbuff handling. |
27 | * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET. | 27 | * Darryl Miles : Fixed non-blocking SOCK_SEQPACKET. |
28 | * Linus Torvalds : BSD semantic fixes. | 28 | * Linus Torvalds : BSD semantic fixes. |
29 | * Alan Cox : Datagram iovec handling | 29 | * Alan Cox : Datagram iovec handling |
30 | * Darryl Miles : Fixed non-blocking SOCK_STREAM. | 30 | * Darryl Miles : Fixed non-blocking SOCK_STREAM. |
31 | * Alan Cox : POSIXisms | 31 | * Alan Cox : POSIXisms |
32 | * Pete Wyckoff : Unconnected accept() fix. | 32 | * Pete Wyckoff : Unconnected accept() fix. |
33 | * | 33 | * |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include <linux/module.h> | 36 | #include <linux/module.h> |
37 | #include <linux/types.h> | 37 | #include <linux/types.h> |
38 | #include <linux/kernel.h> | 38 | #include <linux/kernel.h> |
39 | #include <asm/uaccess.h> | 39 | #include <asm/uaccess.h> |
40 | #include <asm/system.h> | 40 | #include <asm/system.h> |
41 | #include <linux/mm.h> | 41 | #include <linux/mm.h> |
42 | #include <linux/interrupt.h> | 42 | #include <linux/interrupt.h> |
43 | #include <linux/errno.h> | 43 | #include <linux/errno.h> |
44 | #include <linux/sched.h> | 44 | #include <linux/sched.h> |
45 | #include <linux/inet.h> | 45 | #include <linux/inet.h> |
46 | #include <linux/netdevice.h> | 46 | #include <linux/netdevice.h> |
47 | #include <linux/rtnetlink.h> | 47 | #include <linux/rtnetlink.h> |
48 | #include <linux/poll.h> | 48 | #include <linux/poll.h> |
49 | #include <linux/highmem.h> | 49 | #include <linux/highmem.h> |
50 | #include <linux/spinlock.h> | 50 | #include <linux/spinlock.h> |
51 | #include <linux/slab.h> | 51 | #include <linux/slab.h> |
52 | 52 | ||
53 | #include <net/protocol.h> | 53 | #include <net/protocol.h> |
54 | #include <linux/skbuff.h> | 54 | #include <linux/skbuff.h> |
55 | 55 | ||
56 | #include <net/checksum.h> | 56 | #include <net/checksum.h> |
57 | #include <net/sock.h> | 57 | #include <net/sock.h> |
58 | #include <net/tcp_states.h> | 58 | #include <net/tcp_states.h> |
59 | #include <trace/events/skb.h> | 59 | #include <trace/events/skb.h> |
60 | 60 | ||
61 | /* | 61 | /* |
62 | * Is a socket 'connection oriented' ? | 62 | * Is a socket 'connection oriented' ? |
63 | */ | 63 | */ |
64 | static inline int connection_based(struct sock *sk) | 64 | static inline int connection_based(struct sock *sk) |
65 | { | 65 | { |
66 | return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM; | 66 | return sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM; |
67 | } | 67 | } |
68 | 68 | ||
69 | static int receiver_wake_function(wait_queue_t *wait, unsigned mode, int sync, | 69 | static int receiver_wake_function(wait_queue_t *wait, unsigned mode, int sync, |
70 | void *key) | 70 | void *key) |
71 | { | 71 | { |
72 | unsigned long bits = (unsigned long)key; | 72 | unsigned long bits = (unsigned long)key; |
73 | 73 | ||
74 | /* | 74 | /* |
75 | * Avoid a wakeup if event not interesting for us | 75 | * Avoid a wakeup if event not interesting for us |
76 | */ | 76 | */ |
77 | if (bits && !(bits & (POLLIN | POLLERR))) | 77 | if (bits && !(bits & (POLLIN | POLLERR))) |
78 | return 0; | 78 | return 0; |
79 | return autoremove_wake_function(wait, mode, sync, key); | 79 | return autoremove_wake_function(wait, mode, sync, key); |
80 | } | 80 | } |
81 | /* | 81 | /* |
82 | * Wait for a packet.. | 82 | * Wait for a packet.. |
83 | */ | 83 | */ |
84 | static int wait_for_packet(struct sock *sk, int *err, long *timeo_p) | 84 | static int wait_for_packet(struct sock *sk, int *err, long *timeo_p) |
85 | { | 85 | { |
86 | int error; | 86 | int error; |
87 | DEFINE_WAIT_FUNC(wait, receiver_wake_function); | 87 | DEFINE_WAIT_FUNC(wait, receiver_wake_function); |
88 | 88 | ||
89 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 89 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
90 | 90 | ||
91 | /* Socket errors? */ | 91 | /* Socket errors? */ |
92 | error = sock_error(sk); | 92 | error = sock_error(sk); |
93 | if (error) | 93 | if (error) |
94 | goto out_err; | 94 | goto out_err; |
95 | 95 | ||
96 | if (!skb_queue_empty(&sk->sk_receive_queue)) | 96 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
97 | goto out; | 97 | goto out; |
98 | 98 | ||
99 | /* Socket shut down? */ | 99 | /* Socket shut down? */ |
100 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 100 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
101 | goto out_noerr; | 101 | goto out_noerr; |
102 | 102 | ||
103 | /* Sequenced packets can come disconnected. | 103 | /* Sequenced packets can come disconnected. |
104 | * If so we report the problem | 104 | * If so we report the problem |
105 | */ | 105 | */ |
106 | error = -ENOTCONN; | 106 | error = -ENOTCONN; |
107 | if (connection_based(sk) && | 107 | if (connection_based(sk) && |
108 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) | 108 | !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) |
109 | goto out_err; | 109 | goto out_err; |
110 | 110 | ||
111 | /* handle signals */ | 111 | /* handle signals */ |
112 | if (signal_pending(current)) | 112 | if (signal_pending(current)) |
113 | goto interrupted; | 113 | goto interrupted; |
114 | 114 | ||
115 | error = 0; | 115 | error = 0; |
116 | *timeo_p = schedule_timeout(*timeo_p); | 116 | *timeo_p = schedule_timeout(*timeo_p); |
117 | out: | 117 | out: |
118 | finish_wait(sk_sleep(sk), &wait); | 118 | finish_wait(sk_sleep(sk), &wait); |
119 | return error; | 119 | return error; |
120 | interrupted: | 120 | interrupted: |
121 | error = sock_intr_errno(*timeo_p); | 121 | error = sock_intr_errno(*timeo_p); |
122 | out_err: | 122 | out_err: |
123 | *err = error; | 123 | *err = error; |
124 | goto out; | 124 | goto out; |
125 | out_noerr: | 125 | out_noerr: |
126 | *err = 0; | 126 | *err = 0; |
127 | error = 1; | 127 | error = 1; |
128 | goto out; | 128 | goto out; |
129 | } | 129 | } |
130 | 130 | ||
131 | /** | 131 | /** |
132 | * __skb_recv_datagram - Receive a datagram skbuff | 132 | * __skb_recv_datagram - Receive a datagram skbuff |
133 | * @sk: socket | 133 | * @sk: socket |
134 | * @flags: MSG_ flags | 134 | * @flags: MSG_ flags |
135 | * @peeked: returns non-zero if this packet has been seen before | 135 | * @peeked: returns non-zero if this packet has been seen before |
136 | * @err: error code returned | 136 | * @err: error code returned |
137 | * | 137 | * |
138 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups | 138 | * Get a datagram skbuff, understands the peeking, nonblocking wakeups |
139 | * and possible races. This replaces identical code in packet, raw and | 139 | * and possible races. This replaces identical code in packet, raw and |
140 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes | 140 | * udp, as well as the IPX AX.25 and Appletalk. It also finally fixes |
141 | * the long standing peek and read race for datagram sockets. If you | 141 | * the long standing peek and read race for datagram sockets. If you |
142 | * alter this routine remember it must be re-entrant. | 142 | * alter this routine remember it must be re-entrant. |
143 | * | 143 | * |
144 | * This function will lock the socket if a skb is returned, so the caller | 144 | * This function will lock the socket if a skb is returned, so the caller |
145 | * needs to unlock the socket in that case (usually by calling | 145 | * needs to unlock the socket in that case (usually by calling |
146 | * skb_free_datagram) | 146 | * skb_free_datagram) |
147 | * | 147 | * |
148 | * * It does not lock socket since today. This function is | 148 | * * It does not lock socket since today. This function is |
149 | * * free of race conditions. This measure should/can improve | 149 | * * free of race conditions. This measure should/can improve |
150 | * * significantly datagram socket latencies at high loads, | 150 | * * significantly datagram socket latencies at high loads, |
151 | * * when data copying to user space takes lots of time. | 151 | * * when data copying to user space takes lots of time. |
152 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet | 152 | * * (BTW I've just killed the last cli() in IP/IPv6/core/netlink/packet |
153 | * * 8) Great win.) | 153 | * * 8) Great win.) |
154 | * * --ANK (980729) | 154 | * * --ANK (980729) |
155 | * | 155 | * |
156 | * The order of the tests when we find no data waiting are specified | 156 | * The order of the tests when we find no data waiting are specified |
157 | * quite explicitly by POSIX 1003.1g, don't change them without having | 157 | * quite explicitly by POSIX 1003.1g, don't change them without having |
158 | * the standard around please. | 158 | * the standard around please. |
159 | */ | 159 | */ |
160 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags, | 160 | struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags, |
161 | int *peeked, int *err) | 161 | int *peeked, int *err) |
162 | { | 162 | { |
163 | struct sk_buff *skb; | 163 | struct sk_buff *skb; |
164 | long timeo; | 164 | long timeo; |
165 | /* | 165 | /* |
166 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() | 166 | * Caller is allowed not to check sk->sk_err before skb_recv_datagram() |
167 | */ | 167 | */ |
168 | int error = sock_error(sk); | 168 | int error = sock_error(sk); |
169 | 169 | ||
170 | if (error) | 170 | if (error) |
171 | goto no_packet; | 171 | goto no_packet; |
172 | 172 | ||
173 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); | 173 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
174 | 174 | ||
175 | do { | 175 | do { |
176 | /* Again only user level code calls this function, so nothing | 176 | /* Again only user level code calls this function, so nothing |
177 | * interrupt level will suddenly eat the receive_queue. | 177 | * interrupt level will suddenly eat the receive_queue. |
178 | * | 178 | * |
179 | * Look at current nfs client by the way... | 179 | * Look at current nfs client by the way... |
180 | * However, this function was corrent in any case. 8) | 180 | * However, this function was corrent in any case. 8) |
181 | */ | 181 | */ |
182 | unsigned long cpu_flags; | 182 | unsigned long cpu_flags; |
183 | 183 | ||
184 | spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags); | 184 | spin_lock_irqsave(&sk->sk_receive_queue.lock, cpu_flags); |
185 | skb = skb_peek(&sk->sk_receive_queue); | 185 | skb = skb_peek(&sk->sk_receive_queue); |
186 | if (skb) { | 186 | if (skb) { |
187 | *peeked = skb->peeked; | 187 | *peeked = skb->peeked; |
188 | if (flags & MSG_PEEK) { | 188 | if (flags & MSG_PEEK) { |
189 | skb->peeked = 1; | 189 | skb->peeked = 1; |
190 | atomic_inc(&skb->users); | 190 | atomic_inc(&skb->users); |
191 | } else | 191 | } else |
192 | __skb_unlink(skb, &sk->sk_receive_queue); | 192 | __skb_unlink(skb, &sk->sk_receive_queue); |
193 | } | 193 | } |
194 | spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags); | 194 | spin_unlock_irqrestore(&sk->sk_receive_queue.lock, cpu_flags); |
195 | 195 | ||
196 | if (skb) | 196 | if (skb) |
197 | return skb; | 197 | return skb; |
198 | 198 | ||
199 | /* User doesn't want to wait */ | 199 | /* User doesn't want to wait */ |
200 | error = -EAGAIN; | 200 | error = -EAGAIN; |
201 | if (!timeo) | 201 | if (!timeo) |
202 | goto no_packet; | 202 | goto no_packet; |
203 | 203 | ||
204 | } while (!wait_for_packet(sk, err, &timeo)); | 204 | } while (!wait_for_packet(sk, err, &timeo)); |
205 | 205 | ||
206 | return NULL; | 206 | return NULL; |
207 | 207 | ||
208 | no_packet: | 208 | no_packet: |
209 | *err = error; | 209 | *err = error; |
210 | return NULL; | 210 | return NULL; |
211 | } | 211 | } |
212 | EXPORT_SYMBOL(__skb_recv_datagram); | 212 | EXPORT_SYMBOL(__skb_recv_datagram); |
213 | 213 | ||
214 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, | 214 | struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, |
215 | int noblock, int *err) | 215 | int noblock, int *err) |
216 | { | 216 | { |
217 | int peeked; | 217 | int peeked; |
218 | 218 | ||
219 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), | 219 | return __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), |
220 | &peeked, err); | 220 | &peeked, err); |
221 | } | 221 | } |
222 | EXPORT_SYMBOL(skb_recv_datagram); | 222 | EXPORT_SYMBOL(skb_recv_datagram); |
223 | 223 | ||
224 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) | 224 | void skb_free_datagram(struct sock *sk, struct sk_buff *skb) |
225 | { | 225 | { |
226 | consume_skb(skb); | 226 | consume_skb(skb); |
227 | sk_mem_reclaim_partial(sk); | 227 | sk_mem_reclaim_partial(sk); |
228 | } | 228 | } |
229 | EXPORT_SYMBOL(skb_free_datagram); | 229 | EXPORT_SYMBOL(skb_free_datagram); |
230 | 230 | ||
231 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb) | 231 | void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb) |
232 | { | 232 | { |
233 | bool slow; | 233 | bool slow; |
234 | 234 | ||
235 | if (likely(atomic_read(&skb->users) == 1)) | 235 | if (likely(atomic_read(&skb->users) == 1)) |
236 | smp_rmb(); | 236 | smp_rmb(); |
237 | else if (likely(!atomic_dec_and_test(&skb->users))) | 237 | else if (likely(!atomic_dec_and_test(&skb->users))) |
238 | return; | 238 | return; |
239 | 239 | ||
240 | slow = lock_sock_fast(sk); | 240 | slow = lock_sock_fast(sk); |
241 | skb_orphan(skb); | 241 | skb_orphan(skb); |
242 | sk_mem_reclaim_partial(sk); | 242 | sk_mem_reclaim_partial(sk); |
243 | unlock_sock_fast(sk, slow); | 243 | unlock_sock_fast(sk, slow); |
244 | 244 | ||
245 | /* skb is now orphaned, can be freed outside of locked section */ | 245 | /* skb is now orphaned, can be freed outside of locked section */ |
246 | __kfree_skb(skb); | 246 | __kfree_skb(skb); |
247 | } | 247 | } |
248 | EXPORT_SYMBOL(skb_free_datagram_locked); | 248 | EXPORT_SYMBOL(skb_free_datagram_locked); |
249 | 249 | ||
250 | /** | 250 | /** |
251 | * skb_kill_datagram - Free a datagram skbuff forcibly | 251 | * skb_kill_datagram - Free a datagram skbuff forcibly |
252 | * @sk: socket | 252 | * @sk: socket |
253 | * @skb: datagram skbuff | 253 | * @skb: datagram skbuff |
254 | * @flags: MSG_ flags | 254 | * @flags: MSG_ flags |
255 | * | 255 | * |
256 | * This function frees a datagram skbuff that was received by | 256 | * This function frees a datagram skbuff that was received by |
257 | * skb_recv_datagram. The flags argument must match the one | 257 | * skb_recv_datagram. The flags argument must match the one |
258 | * used for skb_recv_datagram. | 258 | * used for skb_recv_datagram. |
259 | * | 259 | * |
260 | * If the MSG_PEEK flag is set, and the packet is still on the | 260 | * If the MSG_PEEK flag is set, and the packet is still on the |
261 | * receive queue of the socket, it will be taken off the queue | 261 | * receive queue of the socket, it will be taken off the queue |
262 | * before it is freed. | 262 | * before it is freed. |
263 | * | 263 | * |
264 | * This function currently only disables BH when acquiring the | 264 | * This function currently only disables BH when acquiring the |
265 | * sk_receive_queue lock. Therefore it must not be used in a | 265 | * sk_receive_queue lock. Therefore it must not be used in a |
266 | * context where that lock is acquired in an IRQ context. | 266 | * context where that lock is acquired in an IRQ context. |
267 | * | 267 | * |
268 | * It returns 0 if the packet was removed by us. | 268 | * It returns 0 if the packet was removed by us. |
269 | */ | 269 | */ |
270 | 270 | ||
271 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) | 271 | int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags) |
272 | { | 272 | { |
273 | int err = 0; | 273 | int err = 0; |
274 | 274 | ||
275 | if (flags & MSG_PEEK) { | 275 | if (flags & MSG_PEEK) { |
276 | err = -ENOENT; | 276 | err = -ENOENT; |
277 | spin_lock_bh(&sk->sk_receive_queue.lock); | 277 | spin_lock_bh(&sk->sk_receive_queue.lock); |
278 | if (skb == skb_peek(&sk->sk_receive_queue)) { | 278 | if (skb == skb_peek(&sk->sk_receive_queue)) { |
279 | __skb_unlink(skb, &sk->sk_receive_queue); | 279 | __skb_unlink(skb, &sk->sk_receive_queue); |
280 | atomic_dec(&skb->users); | 280 | atomic_dec(&skb->users); |
281 | err = 0; | 281 | err = 0; |
282 | } | 282 | } |
283 | spin_unlock_bh(&sk->sk_receive_queue.lock); | 283 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
284 | } | 284 | } |
285 | 285 | ||
286 | kfree_skb(skb); | 286 | kfree_skb(skb); |
287 | atomic_inc(&sk->sk_drops); | 287 | atomic_inc(&sk->sk_drops); |
288 | sk_mem_reclaim_partial(sk); | 288 | sk_mem_reclaim_partial(sk); |
289 | 289 | ||
290 | return err; | 290 | return err; |
291 | } | 291 | } |
292 | EXPORT_SYMBOL(skb_kill_datagram); | 292 | EXPORT_SYMBOL(skb_kill_datagram); |
293 | 293 | ||
294 | /** | 294 | /** |
295 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. | 295 | * skb_copy_datagram_iovec - Copy a datagram to an iovec. |
296 | * @skb: buffer to copy | 296 | * @skb: buffer to copy |
297 | * @offset: offset in the buffer to start copying from | 297 | * @offset: offset in the buffer to start copying from |
298 | * @to: io vector to copy to | 298 | * @to: io vector to copy to |
299 | * @len: amount of data to copy from buffer to iovec | 299 | * @len: amount of data to copy from buffer to iovec |
300 | * | 300 | * |
301 | * Note: the iovec is modified during the copy. | 301 | * Note: the iovec is modified during the copy. |
302 | */ | 302 | */ |
303 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, | 303 | int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, |
304 | struct iovec *to, int len) | 304 | struct iovec *to, int len) |
305 | { | 305 | { |
306 | int start = skb_headlen(skb); | 306 | int start = skb_headlen(skb); |
307 | int i, copy = start - offset; | 307 | int i, copy = start - offset; |
308 | struct sk_buff *frag_iter; | 308 | struct sk_buff *frag_iter; |
309 | 309 | ||
310 | trace_skb_copy_datagram_iovec(skb, len); | 310 | trace_skb_copy_datagram_iovec(skb, len); |
311 | 311 | ||
312 | /* Copy header. */ | 312 | /* Copy header. */ |
313 | if (copy > 0) { | 313 | if (copy > 0) { |
314 | if (copy > len) | 314 | if (copy > len) |
315 | copy = len; | 315 | copy = len; |
316 | if (memcpy_toiovec(to, skb->data + offset, copy)) | 316 | if (memcpy_toiovec(to, skb->data + offset, copy)) |
317 | goto fault; | 317 | goto fault; |
318 | if ((len -= copy) == 0) | 318 | if ((len -= copy) == 0) |
319 | return 0; | 319 | return 0; |
320 | offset += copy; | 320 | offset += copy; |
321 | } | 321 | } |
322 | 322 | ||
323 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | 323 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
324 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 324 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
325 | int end; | 325 | int end; |
326 | 326 | ||
327 | WARN_ON(start > offset + len); | 327 | WARN_ON(start > offset + len); |
328 | 328 | ||
329 | end = start + skb_shinfo(skb)->frags[i].size; | 329 | end = start + skb_shinfo(skb)->frags[i].size; |
330 | if ((copy = end - offset) > 0) { | 330 | if ((copy = end - offset) > 0) { |
331 | int err; | 331 | int err; |
332 | u8 *vaddr; | 332 | u8 *vaddr; |
333 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 333 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
334 | struct page *page = frag->page; | 334 | struct page *page = frag->page; |
335 | 335 | ||
336 | if (copy > len) | 336 | if (copy > len) |
337 | copy = len; | 337 | copy = len; |
338 | vaddr = kmap(page); | 338 | vaddr = kmap(page); |
339 | err = memcpy_toiovec(to, vaddr + frag->page_offset + | 339 | err = memcpy_toiovec(to, vaddr + frag->page_offset + |
340 | offset - start, copy); | 340 | offset - start, copy); |
341 | kunmap(page); | 341 | kunmap(page); |
342 | if (err) | 342 | if (err) |
343 | goto fault; | 343 | goto fault; |
344 | if (!(len -= copy)) | 344 | if (!(len -= copy)) |
345 | return 0; | 345 | return 0; |
346 | offset += copy; | 346 | offset += copy; |
347 | } | 347 | } |
348 | start = end; | 348 | start = end; |
349 | } | 349 | } |
350 | 350 | ||
351 | skb_walk_frags(skb, frag_iter) { | 351 | skb_walk_frags(skb, frag_iter) { |
352 | int end; | 352 | int end; |
353 | 353 | ||
354 | WARN_ON(start > offset + len); | 354 | WARN_ON(start > offset + len); |
355 | 355 | ||
356 | end = start + frag_iter->len; | 356 | end = start + frag_iter->len; |
357 | if ((copy = end - offset) > 0) { | 357 | if ((copy = end - offset) > 0) { |
358 | if (copy > len) | 358 | if (copy > len) |
359 | copy = len; | 359 | copy = len; |
360 | if (skb_copy_datagram_iovec(frag_iter, | 360 | if (skb_copy_datagram_iovec(frag_iter, |
361 | offset - start, | 361 | offset - start, |
362 | to, copy)) | 362 | to, copy)) |
363 | goto fault; | 363 | goto fault; |
364 | if ((len -= copy) == 0) | 364 | if ((len -= copy) == 0) |
365 | return 0; | 365 | return 0; |
366 | offset += copy; | 366 | offset += copy; |
367 | } | 367 | } |
368 | start = end; | 368 | start = end; |
369 | } | 369 | } |
370 | if (!len) | 370 | if (!len) |
371 | return 0; | 371 | return 0; |
372 | 372 | ||
373 | fault: | 373 | fault: |
374 | return -EFAULT; | 374 | return -EFAULT; |
375 | } | 375 | } |
376 | EXPORT_SYMBOL(skb_copy_datagram_iovec); | 376 | EXPORT_SYMBOL(skb_copy_datagram_iovec); |
377 | 377 | ||
378 | /** | 378 | /** |
379 | * skb_copy_datagram_const_iovec - Copy a datagram to an iovec. | 379 | * skb_copy_datagram_const_iovec - Copy a datagram to an iovec. |
380 | * @skb: buffer to copy | 380 | * @skb: buffer to copy |
381 | * @offset: offset in the buffer to start copying from | 381 | * @offset: offset in the buffer to start copying from |
382 | * @to: io vector to copy to | 382 | * @to: io vector to copy to |
383 | * @to_offset: offset in the io vector to start copying to | 383 | * @to_offset: offset in the io vector to start copying to |
384 | * @len: amount of data to copy from buffer to iovec | 384 | * @len: amount of data to copy from buffer to iovec |
385 | * | 385 | * |
386 | * Returns 0 or -EFAULT. | 386 | * Returns 0 or -EFAULT. |
387 | * Note: the iovec is not modified during the copy. | 387 | * Note: the iovec is not modified during the copy. |
388 | */ | 388 | */ |
389 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, | 389 | int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, |
390 | const struct iovec *to, int to_offset, | 390 | const struct iovec *to, int to_offset, |
391 | int len) | 391 | int len) |
392 | { | 392 | { |
393 | int start = skb_headlen(skb); | 393 | int start = skb_headlen(skb); |
394 | int i, copy = start - offset; | 394 | int i, copy = start - offset; |
395 | struct sk_buff *frag_iter; | 395 | struct sk_buff *frag_iter; |
396 | 396 | ||
397 | /* Copy header. */ | 397 | /* Copy header. */ |
398 | if (copy > 0) { | 398 | if (copy > 0) { |
399 | if (copy > len) | 399 | if (copy > len) |
400 | copy = len; | 400 | copy = len; |
401 | if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) | 401 | if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) |
402 | goto fault; | 402 | goto fault; |
403 | if ((len -= copy) == 0) | 403 | if ((len -= copy) == 0) |
404 | return 0; | 404 | return 0; |
405 | offset += copy; | 405 | offset += copy; |
406 | to_offset += copy; | 406 | to_offset += copy; |
407 | } | 407 | } |
408 | 408 | ||
409 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | 409 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
410 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 410 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
411 | int end; | 411 | int end; |
412 | 412 | ||
413 | WARN_ON(start > offset + len); | 413 | WARN_ON(start > offset + len); |
414 | 414 | ||
415 | end = start + skb_shinfo(skb)->frags[i].size; | 415 | end = start + skb_shinfo(skb)->frags[i].size; |
416 | if ((copy = end - offset) > 0) { | 416 | if ((copy = end - offset) > 0) { |
417 | int err; | 417 | int err; |
418 | u8 *vaddr; | 418 | u8 *vaddr; |
419 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 419 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
420 | struct page *page = frag->page; | 420 | struct page *page = frag->page; |
421 | 421 | ||
422 | if (copy > len) | 422 | if (copy > len) |
423 | copy = len; | 423 | copy = len; |
424 | vaddr = kmap(page); | 424 | vaddr = kmap(page); |
425 | err = memcpy_toiovecend(to, vaddr + frag->page_offset + | 425 | err = memcpy_toiovecend(to, vaddr + frag->page_offset + |
426 | offset - start, to_offset, copy); | 426 | offset - start, to_offset, copy); |
427 | kunmap(page); | 427 | kunmap(page); |
428 | if (err) | 428 | if (err) |
429 | goto fault; | 429 | goto fault; |
430 | if (!(len -= copy)) | 430 | if (!(len -= copy)) |
431 | return 0; | 431 | return 0; |
432 | offset += copy; | 432 | offset += copy; |
433 | to_offset += copy; | 433 | to_offset += copy; |
434 | } | 434 | } |
435 | start = end; | 435 | start = end; |
436 | } | 436 | } |
437 | 437 | ||
438 | skb_walk_frags(skb, frag_iter) { | 438 | skb_walk_frags(skb, frag_iter) { |
439 | int end; | 439 | int end; |
440 | 440 | ||
441 | WARN_ON(start > offset + len); | 441 | WARN_ON(start > offset + len); |
442 | 442 | ||
443 | end = start + frag_iter->len; | 443 | end = start + frag_iter->len; |
444 | if ((copy = end - offset) > 0) { | 444 | if ((copy = end - offset) > 0) { |
445 | if (copy > len) | 445 | if (copy > len) |
446 | copy = len; | 446 | copy = len; |
447 | if (skb_copy_datagram_const_iovec(frag_iter, | 447 | if (skb_copy_datagram_const_iovec(frag_iter, |
448 | offset - start, | 448 | offset - start, |
449 | to, to_offset, | 449 | to, to_offset, |
450 | copy)) | 450 | copy)) |
451 | goto fault; | 451 | goto fault; |
452 | if ((len -= copy) == 0) | 452 | if ((len -= copy) == 0) |
453 | return 0; | 453 | return 0; |
454 | offset += copy; | 454 | offset += copy; |
455 | to_offset += copy; | 455 | to_offset += copy; |
456 | } | 456 | } |
457 | start = end; | 457 | start = end; |
458 | } | 458 | } |
459 | if (!len) | 459 | if (!len) |
460 | return 0; | 460 | return 0; |
461 | 461 | ||
462 | fault: | 462 | fault: |
463 | return -EFAULT; | 463 | return -EFAULT; |
464 | } | 464 | } |
465 | EXPORT_SYMBOL(skb_copy_datagram_const_iovec); | 465 | EXPORT_SYMBOL(skb_copy_datagram_const_iovec); |
466 | 466 | ||
467 | /** | 467 | /** |
468 | * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. | 468 | * skb_copy_datagram_from_iovec - Copy a datagram from an iovec. |
469 | * @skb: buffer to copy | 469 | * @skb: buffer to copy |
470 | * @offset: offset in the buffer to start copying to | 470 | * @offset: offset in the buffer to start copying to |
471 | * @from: io vector to copy to | 471 | * @from: io vector to copy to |
472 | * @from_offset: offset in the io vector to start copying from | 472 | * @from_offset: offset in the io vector to start copying from |
473 | * @len: amount of data to copy to buffer from iovec | 473 | * @len: amount of data to copy to buffer from iovec |
474 | * | 474 | * |
475 | * Returns 0 or -EFAULT. | 475 | * Returns 0 or -EFAULT. |
476 | * Note: the iovec is not modified during the copy. | 476 | * Note: the iovec is not modified during the copy. |
477 | */ | 477 | */ |
478 | int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset, | 478 | int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset, |
479 | const struct iovec *from, int from_offset, | 479 | const struct iovec *from, int from_offset, |
480 | int len) | 480 | int len) |
481 | { | 481 | { |
482 | int start = skb_headlen(skb); | 482 | int start = skb_headlen(skb); |
483 | int i, copy = start - offset; | 483 | int i, copy = start - offset; |
484 | struct sk_buff *frag_iter; | 484 | struct sk_buff *frag_iter; |
485 | 485 | ||
486 | /* Copy header. */ | 486 | /* Copy header. */ |
487 | if (copy > 0) { | 487 | if (copy > 0) { |
488 | if (copy > len) | 488 | if (copy > len) |
489 | copy = len; | 489 | copy = len; |
490 | if (memcpy_fromiovecend(skb->data + offset, from, from_offset, | 490 | if (memcpy_fromiovecend(skb->data + offset, from, from_offset, |
491 | copy)) | 491 | copy)) |
492 | goto fault; | 492 | goto fault; |
493 | if ((len -= copy) == 0) | 493 | if ((len -= copy) == 0) |
494 | return 0; | 494 | return 0; |
495 | offset += copy; | 495 | offset += copy; |
496 | from_offset += copy; | 496 | from_offset += copy; |
497 | } | 497 | } |
498 | 498 | ||
499 | /* Copy paged appendix. Hmm... why does this look so complicated? */ | 499 | /* Copy paged appendix. Hmm... why does this look so complicated? */ |
500 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 500 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
501 | int end; | 501 | int end; |
502 | 502 | ||
503 | WARN_ON(start > offset + len); | 503 | WARN_ON(start > offset + len); |
504 | 504 | ||
505 | end = start + skb_shinfo(skb)->frags[i].size; | 505 | end = start + skb_shinfo(skb)->frags[i].size; |
506 | if ((copy = end - offset) > 0) { | 506 | if ((copy = end - offset) > 0) { |
507 | int err; | 507 | int err; |
508 | u8 *vaddr; | 508 | u8 *vaddr; |
509 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 509 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
510 | struct page *page = frag->page; | 510 | struct page *page = frag->page; |
511 | 511 | ||
512 | if (copy > len) | 512 | if (copy > len) |
513 | copy = len; | 513 | copy = len; |
514 | vaddr = kmap(page); | 514 | vaddr = kmap(page); |
515 | err = memcpy_fromiovecend(vaddr + frag->page_offset + | 515 | err = memcpy_fromiovecend(vaddr + frag->page_offset + |
516 | offset - start, | 516 | offset - start, |
517 | from, from_offset, copy); | 517 | from, from_offset, copy); |
518 | kunmap(page); | 518 | kunmap(page); |
519 | if (err) | 519 | if (err) |
520 | goto fault; | 520 | goto fault; |
521 | 521 | ||
522 | if (!(len -= copy)) | 522 | if (!(len -= copy)) |
523 | return 0; | 523 | return 0; |
524 | offset += copy; | 524 | offset += copy; |
525 | from_offset += copy; | 525 | from_offset += copy; |
526 | } | 526 | } |
527 | start = end; | 527 | start = end; |
528 | } | 528 | } |
529 | 529 | ||
530 | skb_walk_frags(skb, frag_iter) { | 530 | skb_walk_frags(skb, frag_iter) { |
531 | int end; | 531 | int end; |
532 | 532 | ||
533 | WARN_ON(start > offset + len); | 533 | WARN_ON(start > offset + len); |
534 | 534 | ||
535 | end = start + frag_iter->len; | 535 | end = start + frag_iter->len; |
536 | if ((copy = end - offset) > 0) { | 536 | if ((copy = end - offset) > 0) { |
537 | if (copy > len) | 537 | if (copy > len) |
538 | copy = len; | 538 | copy = len; |
539 | if (skb_copy_datagram_from_iovec(frag_iter, | 539 | if (skb_copy_datagram_from_iovec(frag_iter, |
540 | offset - start, | 540 | offset - start, |
541 | from, | 541 | from, |
542 | from_offset, | 542 | from_offset, |
543 | copy)) | 543 | copy)) |
544 | goto fault; | 544 | goto fault; |
545 | if ((len -= copy) == 0) | 545 | if ((len -= copy) == 0) |
546 | return 0; | 546 | return 0; |
547 | offset += copy; | 547 | offset += copy; |
548 | from_offset += copy; | 548 | from_offset += copy; |
549 | } | 549 | } |
550 | start = end; | 550 | start = end; |
551 | } | 551 | } |
552 | if (!len) | 552 | if (!len) |
553 | return 0; | 553 | return 0; |
554 | 554 | ||
555 | fault: | 555 | fault: |
556 | return -EFAULT; | 556 | return -EFAULT; |
557 | } | 557 | } |
558 | EXPORT_SYMBOL(skb_copy_datagram_from_iovec); | 558 | EXPORT_SYMBOL(skb_copy_datagram_from_iovec); |
559 | 559 | ||
560 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, | 560 | static int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, |
561 | u8 __user *to, int len, | 561 | u8 __user *to, int len, |
562 | __wsum *csump) | 562 | __wsum *csump) |
563 | { | 563 | { |
564 | int start = skb_headlen(skb); | 564 | int start = skb_headlen(skb); |
565 | int i, copy = start - offset; | 565 | int i, copy = start - offset; |
566 | struct sk_buff *frag_iter; | 566 | struct sk_buff *frag_iter; |
567 | int pos = 0; | 567 | int pos = 0; |
568 | 568 | ||
569 | /* Copy header. */ | 569 | /* Copy header. */ |
570 | if (copy > 0) { | 570 | if (copy > 0) { |
571 | int err = 0; | 571 | int err = 0; |
572 | if (copy > len) | 572 | if (copy > len) |
573 | copy = len; | 573 | copy = len; |
574 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, | 574 | *csump = csum_and_copy_to_user(skb->data + offset, to, copy, |
575 | *csump, &err); | 575 | *csump, &err); |
576 | if (err) | 576 | if (err) |
577 | goto fault; | 577 | goto fault; |
578 | if ((len -= copy) == 0) | 578 | if ((len -= copy) == 0) |
579 | return 0; | 579 | return 0; |
580 | offset += copy; | 580 | offset += copy; |
581 | to += copy; | 581 | to += copy; |
582 | pos = copy; | 582 | pos = copy; |
583 | } | 583 | } |
584 | 584 | ||
585 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 585 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
586 | int end; | 586 | int end; |
587 | 587 | ||
588 | WARN_ON(start > offset + len); | 588 | WARN_ON(start > offset + len); |
589 | 589 | ||
590 | end = start + skb_shinfo(skb)->frags[i].size; | 590 | end = start + skb_shinfo(skb)->frags[i].size; |
591 | if ((copy = end - offset) > 0) { | 591 | if ((copy = end - offset) > 0) { |
592 | __wsum csum2; | 592 | __wsum csum2; |
593 | int err = 0; | 593 | int err = 0; |
594 | u8 *vaddr; | 594 | u8 *vaddr; |
595 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 595 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
596 | struct page *page = frag->page; | 596 | struct page *page = frag->page; |
597 | 597 | ||
598 | if (copy > len) | 598 | if (copy > len) |
599 | copy = len; | 599 | copy = len; |
600 | vaddr = kmap(page); | 600 | vaddr = kmap(page); |
601 | csum2 = csum_and_copy_to_user(vaddr + | 601 | csum2 = csum_and_copy_to_user(vaddr + |
602 | frag->page_offset + | 602 | frag->page_offset + |
603 | offset - start, | 603 | offset - start, |
604 | to, copy, 0, &err); | 604 | to, copy, 0, &err); |
605 | kunmap(page); | 605 | kunmap(page); |
606 | if (err) | 606 | if (err) |
607 | goto fault; | 607 | goto fault; |
608 | *csump = csum_block_add(*csump, csum2, pos); | 608 | *csump = csum_block_add(*csump, csum2, pos); |
609 | if (!(len -= copy)) | 609 | if (!(len -= copy)) |
610 | return 0; | 610 | return 0; |
611 | offset += copy; | 611 | offset += copy; |
612 | to += copy; | 612 | to += copy; |
613 | pos += copy; | 613 | pos += copy; |
614 | } | 614 | } |
615 | start = end; | 615 | start = end; |
616 | } | 616 | } |
617 | 617 | ||
618 | skb_walk_frags(skb, frag_iter) { | 618 | skb_walk_frags(skb, frag_iter) { |
619 | int end; | 619 | int end; |
620 | 620 | ||
621 | WARN_ON(start > offset + len); | 621 | WARN_ON(start > offset + len); |
622 | 622 | ||
623 | end = start + frag_iter->len; | 623 | end = start + frag_iter->len; |
624 | if ((copy = end - offset) > 0) { | 624 | if ((copy = end - offset) > 0) { |
625 | __wsum csum2 = 0; | 625 | __wsum csum2 = 0; |
626 | if (copy > len) | 626 | if (copy > len) |
627 | copy = len; | 627 | copy = len; |
628 | if (skb_copy_and_csum_datagram(frag_iter, | 628 | if (skb_copy_and_csum_datagram(frag_iter, |
629 | offset - start, | 629 | offset - start, |
630 | to, copy, | 630 | to, copy, |
631 | &csum2)) | 631 | &csum2)) |
632 | goto fault; | 632 | goto fault; |
633 | *csump = csum_block_add(*csump, csum2, pos); | 633 | *csump = csum_block_add(*csump, csum2, pos); |
634 | if ((len -= copy) == 0) | 634 | if ((len -= copy) == 0) |
635 | return 0; | 635 | return 0; |
636 | offset += copy; | 636 | offset += copy; |
637 | to += copy; | 637 | to += copy; |
638 | pos += copy; | 638 | pos += copy; |
639 | } | 639 | } |
640 | start = end; | 640 | start = end; |
641 | } | 641 | } |
642 | if (!len) | 642 | if (!len) |
643 | return 0; | 643 | return 0; |
644 | 644 | ||
645 | fault: | 645 | fault: |
646 | return -EFAULT; | 646 | return -EFAULT; |
647 | } | 647 | } |
648 | 648 | ||
649 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) | 649 | __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) |
650 | { | 650 | { |
651 | __sum16 sum; | 651 | __sum16 sum; |
652 | 652 | ||
653 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); | 653 | sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); |
654 | if (likely(!sum)) { | 654 | if (likely(!sum)) { |
655 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) | 655 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
656 | netdev_rx_csum_fault(skb->dev); | 656 | netdev_rx_csum_fault(skb->dev); |
657 | skb->ip_summed = CHECKSUM_UNNECESSARY; | 657 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
658 | } | 658 | } |
659 | return sum; | 659 | return sum; |
660 | } | 660 | } |
661 | EXPORT_SYMBOL(__skb_checksum_complete_head); | 661 | EXPORT_SYMBOL(__skb_checksum_complete_head); |
662 | 662 | ||
663 | __sum16 __skb_checksum_complete(struct sk_buff *skb) | 663 | __sum16 __skb_checksum_complete(struct sk_buff *skb) |
664 | { | 664 | { |
665 | return __skb_checksum_complete_head(skb, skb->len); | 665 | return __skb_checksum_complete_head(skb, skb->len); |
666 | } | 666 | } |
667 | EXPORT_SYMBOL(__skb_checksum_complete); | 667 | EXPORT_SYMBOL(__skb_checksum_complete); |
668 | 668 | ||
669 | /** | 669 | /** |
670 | * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec. | 670 | * skb_copy_and_csum_datagram_iovec - Copy and checkum skb to user iovec. |
671 | * @skb: skbuff | 671 | * @skb: skbuff |
672 | * @hlen: hardware length | 672 | * @hlen: hardware length |
673 | * @iov: io vector | 673 | * @iov: io vector |
674 | * | 674 | * |
675 | * Caller _must_ check that skb will fit to this iovec. | 675 | * Caller _must_ check that skb will fit to this iovec. |
676 | * | 676 | * |
677 | * Returns: 0 - success. | 677 | * Returns: 0 - success. |
678 | * -EINVAL - checksum failure. | 678 | * -EINVAL - checksum failure. |
679 | * -EFAULT - fault during copy. Beware, in this case iovec | 679 | * -EFAULT - fault during copy. Beware, in this case iovec |
680 | * can be modified! | 680 | * can be modified! |
681 | */ | 681 | */ |
682 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, | 682 | int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb, |
683 | int hlen, struct iovec *iov) | 683 | int hlen, struct iovec *iov) |
684 | { | 684 | { |
685 | __wsum csum; | 685 | __wsum csum; |
686 | int chunk = skb->len - hlen; | 686 | int chunk = skb->len - hlen; |
687 | 687 | ||
688 | if (!chunk) | 688 | if (!chunk) |
689 | return 0; | 689 | return 0; |
690 | 690 | ||
691 | /* Skip filled elements. | 691 | /* Skip filled elements. |
692 | * Pretty silly, look at memcpy_toiovec, though 8) | 692 | * Pretty silly, look at memcpy_toiovec, though 8) |
693 | */ | 693 | */ |
694 | while (!iov->iov_len) | 694 | while (!iov->iov_len) |
695 | iov++; | 695 | iov++; |
696 | 696 | ||
697 | if (iov->iov_len < chunk) { | 697 | if (iov->iov_len < chunk) { |
698 | if (__skb_checksum_complete(skb)) | 698 | if (__skb_checksum_complete(skb)) |
699 | goto csum_error; | 699 | goto csum_error; |
700 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) | 700 | if (skb_copy_datagram_iovec(skb, hlen, iov, chunk)) |
701 | goto fault; | 701 | goto fault; |
702 | } else { | 702 | } else { |
703 | csum = csum_partial(skb->data, hlen, skb->csum); | 703 | csum = csum_partial(skb->data, hlen, skb->csum); |
704 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, | 704 | if (skb_copy_and_csum_datagram(skb, hlen, iov->iov_base, |
705 | chunk, &csum)) | 705 | chunk, &csum)) |
706 | goto fault; | 706 | goto fault; |
707 | if (csum_fold(csum)) | 707 | if (csum_fold(csum)) |
708 | goto csum_error; | 708 | goto csum_error; |
709 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) | 709 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE)) |
710 | netdev_rx_csum_fault(skb->dev); | 710 | netdev_rx_csum_fault(skb->dev); |
711 | iov->iov_len -= chunk; | 711 | iov->iov_len -= chunk; |
712 | iov->iov_base += chunk; | 712 | iov->iov_base += chunk; |
713 | } | 713 | } |
714 | return 0; | 714 | return 0; |
715 | csum_error: | 715 | csum_error: |
716 | return -EINVAL; | 716 | return -EINVAL; |
717 | fault: | 717 | fault: |
718 | return -EFAULT; | 718 | return -EFAULT; |
719 | } | 719 | } |
720 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); | 720 | EXPORT_SYMBOL(skb_copy_and_csum_datagram_iovec); |
721 | 721 | ||
722 | /** | 722 | /** |
723 | * datagram_poll - generic datagram poll | 723 | * datagram_poll - generic datagram poll |
724 | * @file: file struct | 724 | * @file: file struct |
725 | * @sock: socket | 725 | * @sock: socket |
726 | * @wait: poll table | 726 | * @wait: poll table |
727 | * | 727 | * |
728 | * Datagram poll: Again totally generic. This also handles | 728 | * Datagram poll: Again totally generic. This also handles |
729 | * sequenced packet sockets providing the socket receive queue | 729 | * sequenced packet sockets providing the socket receive queue |
730 | * is only ever holding data ready to receive. | 730 | * is only ever holding data ready to receive. |
731 | * | 731 | * |
732 | * Note: when you _don't_ use this routine for this protocol, | 732 | * Note: when you _don't_ use this routine for this protocol, |
733 | * and you use a different write policy from sock_writeable() | 733 | * and you use a different write policy from sock_writeable() |
734 | * then please supply your own write_space callback. | 734 | * then please supply your own write_space callback. |
735 | */ | 735 | */ |
736 | unsigned int datagram_poll(struct file *file, struct socket *sock, | 736 | unsigned int datagram_poll(struct file *file, struct socket *sock, |
737 | poll_table *wait) | 737 | poll_table *wait) |
738 | { | 738 | { |
739 | struct sock *sk = sock->sk; | 739 | struct sock *sk = sock->sk; |
740 | unsigned int mask; | 740 | unsigned int mask; |
741 | 741 | ||
742 | sock_poll_wait(file, sk_sleep(sk), wait); | 742 | sock_poll_wait(file, sk_sleep(sk), wait); |
743 | mask = 0; | 743 | mask = 0; |
744 | 744 | ||
745 | /* exceptional events? */ | 745 | /* exceptional events? */ |
746 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 746 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
747 | mask |= POLLERR; | 747 | mask |= POLLERR; |
748 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 748 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
749 | mask |= POLLRDHUP; | 749 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
750 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 750 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
751 | mask |= POLLHUP; | 751 | mask |= POLLHUP; |
752 | 752 | ||
753 | /* readable? */ | 753 | /* readable? */ |
754 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 754 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
755 | (sk->sk_shutdown & RCV_SHUTDOWN)) | ||
756 | mask |= POLLIN | POLLRDNORM; | 755 | mask |= POLLIN | POLLRDNORM; |
757 | 756 | ||
758 | /* Connection-based need to check for termination and startup */ | 757 | /* Connection-based need to check for termination and startup */ |
759 | if (connection_based(sk)) { | 758 | if (connection_based(sk)) { |
760 | if (sk->sk_state == TCP_CLOSE) | 759 | if (sk->sk_state == TCP_CLOSE) |
761 | mask |= POLLHUP; | 760 | mask |= POLLHUP; |
762 | /* connection hasn't started yet? */ | 761 | /* connection hasn't started yet? */ |
763 | if (sk->sk_state == TCP_SYN_SENT) | 762 | if (sk->sk_state == TCP_SYN_SENT) |
764 | return mask; | 763 | return mask; |
765 | } | 764 | } |
766 | 765 | ||
767 | /* writable? */ | 766 | /* writable? */ |
768 | if (sock_writeable(sk)) | 767 | if (sock_writeable(sk)) |
769 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | 768 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
770 | else | 769 | else |
771 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | 770 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
772 | 771 | ||
773 | return mask; | 772 | return mask; |
774 | } | 773 | } |
775 | EXPORT_SYMBOL(datagram_poll); | 774 | EXPORT_SYMBOL(datagram_poll); |
776 | 775 |
net/sctp/socket.c
1 | /* SCTP kernel implementation | 1 | /* SCTP kernel implementation |
2 | * (C) Copyright IBM Corp. 2001, 2004 | 2 | * (C) Copyright IBM Corp. 2001, 2004 |
3 | * Copyright (c) 1999-2000 Cisco, Inc. | 3 | * Copyright (c) 1999-2000 Cisco, Inc. |
4 | * Copyright (c) 1999-2001 Motorola, Inc. | 4 | * Copyright (c) 1999-2001 Motorola, Inc. |
5 | * Copyright (c) 2001-2003 Intel Corp. | 5 | * Copyright (c) 2001-2003 Intel Corp. |
6 | * Copyright (c) 2001-2002 Nokia, Inc. | 6 | * Copyright (c) 2001-2002 Nokia, Inc. |
7 | * Copyright (c) 2001 La Monte H.P. Yarroll | 7 | * Copyright (c) 2001 La Monte H.P. Yarroll |
8 | * | 8 | * |
9 | * This file is part of the SCTP kernel implementation | 9 | * This file is part of the SCTP kernel implementation |
10 | * | 10 | * |
11 | * These functions interface with the sockets layer to implement the | 11 | * These functions interface with the sockets layer to implement the |
12 | * SCTP Extensions for the Sockets API. | 12 | * SCTP Extensions for the Sockets API. |
13 | * | 13 | * |
14 | * Note that the descriptions from the specification are USER level | 14 | * Note that the descriptions from the specification are USER level |
15 | * functions--this file is the functions which populate the struct proto | 15 | * functions--this file is the functions which populate the struct proto |
16 | * for SCTP which is the BOTTOM of the sockets interface. | 16 | * for SCTP which is the BOTTOM of the sockets interface. |
17 | * | 17 | * |
18 | * This SCTP implementation is free software; | 18 | * This SCTP implementation is free software; |
19 | * you can redistribute it and/or modify it under the terms of | 19 | * you can redistribute it and/or modify it under the terms of |
20 | * the GNU General Public License as published by | 20 | * the GNU General Public License as published by |
21 | * the Free Software Foundation; either version 2, or (at your option) | 21 | * the Free Software Foundation; either version 2, or (at your option) |
22 | * any later version. | 22 | * any later version. |
23 | * | 23 | * |
24 | * This SCTP implementation is distributed in the hope that it | 24 | * This SCTP implementation is distributed in the hope that it |
25 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied | 25 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
26 | * ************************ | 26 | * ************************ |
27 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 27 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
28 | * See the GNU General Public License for more details. | 28 | * See the GNU General Public License for more details. |
29 | * | 29 | * |
30 | * You should have received a copy of the GNU General Public License | 30 | * You should have received a copy of the GNU General Public License |
31 | * along with GNU CC; see the file COPYING. If not, write to | 31 | * along with GNU CC; see the file COPYING. If not, write to |
32 | * the Free Software Foundation, 59 Temple Place - Suite 330, | 32 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
33 | * Boston, MA 02111-1307, USA. | 33 | * Boston, MA 02111-1307, USA. |
34 | * | 34 | * |
35 | * Please send any bug reports or fixes you make to the | 35 | * Please send any bug reports or fixes you make to the |
36 | * email address(es): | 36 | * email address(es): |
37 | * lksctp developers <lksctp-developers@lists.sourceforge.net> | 37 | * lksctp developers <lksctp-developers@lists.sourceforge.net> |
38 | * | 38 | * |
39 | * Or submit a bug report through the following website: | 39 | * Or submit a bug report through the following website: |
40 | * http://www.sf.net/projects/lksctp | 40 | * http://www.sf.net/projects/lksctp |
41 | * | 41 | * |
42 | * Written or modified by: | 42 | * Written or modified by: |
43 | * La Monte H.P. Yarroll <piggy@acm.org> | 43 | * La Monte H.P. Yarroll <piggy@acm.org> |
44 | * Narasimha Budihal <narsi@refcode.org> | 44 | * Narasimha Budihal <narsi@refcode.org> |
45 | * Karl Knutson <karl@athena.chicago.il.us> | 45 | * Karl Knutson <karl@athena.chicago.il.us> |
46 | * Jon Grimm <jgrimm@us.ibm.com> | 46 | * Jon Grimm <jgrimm@us.ibm.com> |
47 | * Xingang Guo <xingang.guo@intel.com> | 47 | * Xingang Guo <xingang.guo@intel.com> |
48 | * Daisy Chang <daisyc@us.ibm.com> | 48 | * Daisy Chang <daisyc@us.ibm.com> |
49 | * Sridhar Samudrala <samudrala@us.ibm.com> | 49 | * Sridhar Samudrala <samudrala@us.ibm.com> |
50 | * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> | 50 | * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com> |
51 | * Ardelle Fan <ardelle.fan@intel.com> | 51 | * Ardelle Fan <ardelle.fan@intel.com> |
52 | * Ryan Layer <rmlayer@us.ibm.com> | 52 | * Ryan Layer <rmlayer@us.ibm.com> |
53 | * Anup Pemmaiah <pemmaiah@cc.usu.edu> | 53 | * Anup Pemmaiah <pemmaiah@cc.usu.edu> |
54 | * Kevin Gao <kevin.gao@intel.com> | 54 | * Kevin Gao <kevin.gao@intel.com> |
55 | * | 55 | * |
56 | * Any bugs reported given to us we will try to fix... any fixes shared will | 56 | * Any bugs reported given to us we will try to fix... any fixes shared will |
57 | * be incorporated into the next SCTP release. | 57 | * be incorporated into the next SCTP release. |
58 | */ | 58 | */ |
59 | 59 | ||
60 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | 60 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
61 | 61 | ||
62 | #include <linux/types.h> | 62 | #include <linux/types.h> |
63 | #include <linux/kernel.h> | 63 | #include <linux/kernel.h> |
64 | #include <linux/wait.h> | 64 | #include <linux/wait.h> |
65 | #include <linux/time.h> | 65 | #include <linux/time.h> |
66 | #include <linux/ip.h> | 66 | #include <linux/ip.h> |
67 | #include <linux/capability.h> | 67 | #include <linux/capability.h> |
68 | #include <linux/fcntl.h> | 68 | #include <linux/fcntl.h> |
69 | #include <linux/poll.h> | 69 | #include <linux/poll.h> |
70 | #include <linux/init.h> | 70 | #include <linux/init.h> |
71 | #include <linux/crypto.h> | 71 | #include <linux/crypto.h> |
72 | #include <linux/slab.h> | 72 | #include <linux/slab.h> |
73 | 73 | ||
74 | #include <net/ip.h> | 74 | #include <net/ip.h> |
75 | #include <net/icmp.h> | 75 | #include <net/icmp.h> |
76 | #include <net/route.h> | 76 | #include <net/route.h> |
77 | #include <net/ipv6.h> | 77 | #include <net/ipv6.h> |
78 | #include <net/inet_common.h> | 78 | #include <net/inet_common.h> |
79 | 79 | ||
80 | #include <linux/socket.h> /* for sa_family_t */ | 80 | #include <linux/socket.h> /* for sa_family_t */ |
81 | #include <net/sock.h> | 81 | #include <net/sock.h> |
82 | #include <net/sctp/sctp.h> | 82 | #include <net/sctp/sctp.h> |
83 | #include <net/sctp/sm.h> | 83 | #include <net/sctp/sm.h> |
84 | 84 | ||
85 | /* WARNING: Please do not remove the SCTP_STATIC attribute to | 85 | /* WARNING: Please do not remove the SCTP_STATIC attribute to |
86 | * any of the functions below as they are used to export functions | 86 | * any of the functions below as they are used to export functions |
87 | * used by a project regression testsuite. | 87 | * used by a project regression testsuite. |
88 | */ | 88 | */ |
89 | 89 | ||
90 | /* Forward declarations for internal helper functions. */ | 90 | /* Forward declarations for internal helper functions. */ |
91 | static int sctp_writeable(struct sock *sk); | 91 | static int sctp_writeable(struct sock *sk); |
92 | static void sctp_wfree(struct sk_buff *skb); | 92 | static void sctp_wfree(struct sk_buff *skb); |
93 | static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p, | 93 | static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p, |
94 | size_t msg_len); | 94 | size_t msg_len); |
95 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p); | 95 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p); |
96 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); | 96 | static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p); |
97 | static int sctp_wait_for_accept(struct sock *sk, long timeo); | 97 | static int sctp_wait_for_accept(struct sock *sk, long timeo); |
98 | static void sctp_wait_for_close(struct sock *sk, long timeo); | 98 | static void sctp_wait_for_close(struct sock *sk, long timeo); |
99 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 99 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
100 | union sctp_addr *addr, int len); | 100 | union sctp_addr *addr, int len); |
101 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); | 101 | static int sctp_bindx_add(struct sock *, struct sockaddr *, int); |
102 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); | 102 | static int sctp_bindx_rem(struct sock *, struct sockaddr *, int); |
103 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); | 103 | static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int); |
104 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); | 104 | static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int); |
105 | static int sctp_send_asconf(struct sctp_association *asoc, | 105 | static int sctp_send_asconf(struct sctp_association *asoc, |
106 | struct sctp_chunk *chunk); | 106 | struct sctp_chunk *chunk); |
107 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); | 107 | static int sctp_do_bind(struct sock *, union sctp_addr *, int); |
108 | static int sctp_autobind(struct sock *sk); | 108 | static int sctp_autobind(struct sock *sk); |
109 | static void sctp_sock_migrate(struct sock *, struct sock *, | 109 | static void sctp_sock_migrate(struct sock *, struct sock *, |
110 | struct sctp_association *, sctp_socket_type_t); | 110 | struct sctp_association *, sctp_socket_type_t); |
111 | static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG; | 111 | static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG; |
112 | 112 | ||
113 | extern struct kmem_cache *sctp_bucket_cachep; | 113 | extern struct kmem_cache *sctp_bucket_cachep; |
114 | extern int sysctl_sctp_mem[3]; | 114 | extern int sysctl_sctp_mem[3]; |
115 | extern int sysctl_sctp_rmem[3]; | 115 | extern int sysctl_sctp_rmem[3]; |
116 | extern int sysctl_sctp_wmem[3]; | 116 | extern int sysctl_sctp_wmem[3]; |
117 | 117 | ||
118 | static int sctp_memory_pressure; | 118 | static int sctp_memory_pressure; |
119 | static atomic_t sctp_memory_allocated; | 119 | static atomic_t sctp_memory_allocated; |
120 | struct percpu_counter sctp_sockets_allocated; | 120 | struct percpu_counter sctp_sockets_allocated; |
121 | 121 | ||
122 | static void sctp_enter_memory_pressure(struct sock *sk) | 122 | static void sctp_enter_memory_pressure(struct sock *sk) |
123 | { | 123 | { |
124 | sctp_memory_pressure = 1; | 124 | sctp_memory_pressure = 1; |
125 | } | 125 | } |
126 | 126 | ||
127 | 127 | ||
128 | /* Get the sndbuf space available at the time on the association. */ | 128 | /* Get the sndbuf space available at the time on the association. */ |
129 | static inline int sctp_wspace(struct sctp_association *asoc) | 129 | static inline int sctp_wspace(struct sctp_association *asoc) |
130 | { | 130 | { |
131 | int amt; | 131 | int amt; |
132 | 132 | ||
133 | if (asoc->ep->sndbuf_policy) | 133 | if (asoc->ep->sndbuf_policy) |
134 | amt = asoc->sndbuf_used; | 134 | amt = asoc->sndbuf_used; |
135 | else | 135 | else |
136 | amt = sk_wmem_alloc_get(asoc->base.sk); | 136 | amt = sk_wmem_alloc_get(asoc->base.sk); |
137 | 137 | ||
138 | if (amt >= asoc->base.sk->sk_sndbuf) { | 138 | if (amt >= asoc->base.sk->sk_sndbuf) { |
139 | if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK) | 139 | if (asoc->base.sk->sk_userlocks & SOCK_SNDBUF_LOCK) |
140 | amt = 0; | 140 | amt = 0; |
141 | else { | 141 | else { |
142 | amt = sk_stream_wspace(asoc->base.sk); | 142 | amt = sk_stream_wspace(asoc->base.sk); |
143 | if (amt < 0) | 143 | if (amt < 0) |
144 | amt = 0; | 144 | amt = 0; |
145 | } | 145 | } |
146 | } else { | 146 | } else { |
147 | amt = asoc->base.sk->sk_sndbuf - amt; | 147 | amt = asoc->base.sk->sk_sndbuf - amt; |
148 | } | 148 | } |
149 | return amt; | 149 | return amt; |
150 | } | 150 | } |
151 | 151 | ||
152 | /* Increment the used sndbuf space count of the corresponding association by | 152 | /* Increment the used sndbuf space count of the corresponding association by |
153 | * the size of the outgoing data chunk. | 153 | * the size of the outgoing data chunk. |
154 | * Also, set the skb destructor for sndbuf accounting later. | 154 | * Also, set the skb destructor for sndbuf accounting later. |
155 | * | 155 | * |
156 | * Since it is always 1-1 between chunk and skb, and also a new skb is always | 156 | * Since it is always 1-1 between chunk and skb, and also a new skb is always |
157 | * allocated for chunk bundling in sctp_packet_transmit(), we can use the | 157 | * allocated for chunk bundling in sctp_packet_transmit(), we can use the |
158 | * destructor in the data chunk skb for the purpose of the sndbuf space | 158 | * destructor in the data chunk skb for the purpose of the sndbuf space |
159 | * tracking. | 159 | * tracking. |
160 | */ | 160 | */ |
161 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) | 161 | static inline void sctp_set_owner_w(struct sctp_chunk *chunk) |
162 | { | 162 | { |
163 | struct sctp_association *asoc = chunk->asoc; | 163 | struct sctp_association *asoc = chunk->asoc; |
164 | struct sock *sk = asoc->base.sk; | 164 | struct sock *sk = asoc->base.sk; |
165 | 165 | ||
166 | /* The sndbuf space is tracked per association. */ | 166 | /* The sndbuf space is tracked per association. */ |
167 | sctp_association_hold(asoc); | 167 | sctp_association_hold(asoc); |
168 | 168 | ||
169 | skb_set_owner_w(chunk->skb, sk); | 169 | skb_set_owner_w(chunk->skb, sk); |
170 | 170 | ||
171 | chunk->skb->destructor = sctp_wfree; | 171 | chunk->skb->destructor = sctp_wfree; |
172 | /* Save the chunk pointer in skb for sctp_wfree to use later. */ | 172 | /* Save the chunk pointer in skb for sctp_wfree to use later. */ |
173 | *((struct sctp_chunk **)(chunk->skb->cb)) = chunk; | 173 | *((struct sctp_chunk **)(chunk->skb->cb)) = chunk; |
174 | 174 | ||
175 | asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + | 175 | asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) + |
176 | sizeof(struct sk_buff) + | 176 | sizeof(struct sk_buff) + |
177 | sizeof(struct sctp_chunk); | 177 | sizeof(struct sctp_chunk); |
178 | 178 | ||
179 | atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); | 179 | atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
180 | sk->sk_wmem_queued += chunk->skb->truesize; | 180 | sk->sk_wmem_queued += chunk->skb->truesize; |
181 | sk_mem_charge(sk, chunk->skb->truesize); | 181 | sk_mem_charge(sk, chunk->skb->truesize); |
182 | } | 182 | } |
183 | 183 | ||
184 | /* Verify that this is a valid address. */ | 184 | /* Verify that this is a valid address. */ |
185 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, | 185 | static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr, |
186 | int len) | 186 | int len) |
187 | { | 187 | { |
188 | struct sctp_af *af; | 188 | struct sctp_af *af; |
189 | 189 | ||
190 | /* Verify basic sockaddr. */ | 190 | /* Verify basic sockaddr. */ |
191 | af = sctp_sockaddr_af(sctp_sk(sk), addr, len); | 191 | af = sctp_sockaddr_af(sctp_sk(sk), addr, len); |
192 | if (!af) | 192 | if (!af) |
193 | return -EINVAL; | 193 | return -EINVAL; |
194 | 194 | ||
195 | /* Is this a valid SCTP address? */ | 195 | /* Is this a valid SCTP address? */ |
196 | if (!af->addr_valid(addr, sctp_sk(sk), NULL)) | 196 | if (!af->addr_valid(addr, sctp_sk(sk), NULL)) |
197 | return -EINVAL; | 197 | return -EINVAL; |
198 | 198 | ||
199 | if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) | 199 | if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr))) |
200 | return -EINVAL; | 200 | return -EINVAL; |
201 | 201 | ||
202 | return 0; | 202 | return 0; |
203 | } | 203 | } |
204 | 204 | ||
205 | /* Look up the association by its id. If this is not a UDP-style | 205 | /* Look up the association by its id. If this is not a UDP-style |
206 | * socket, the ID field is always ignored. | 206 | * socket, the ID field is always ignored. |
207 | */ | 207 | */ |
208 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) | 208 | struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id) |
209 | { | 209 | { |
210 | struct sctp_association *asoc = NULL; | 210 | struct sctp_association *asoc = NULL; |
211 | 211 | ||
212 | /* If this is not a UDP-style socket, assoc id should be ignored. */ | 212 | /* If this is not a UDP-style socket, assoc id should be ignored. */ |
213 | if (!sctp_style(sk, UDP)) { | 213 | if (!sctp_style(sk, UDP)) { |
214 | /* Return NULL if the socket state is not ESTABLISHED. It | 214 | /* Return NULL if the socket state is not ESTABLISHED. It |
215 | * could be a TCP-style listening socket or a socket which | 215 | * could be a TCP-style listening socket or a socket which |
216 | * hasn't yet called connect() to establish an association. | 216 | * hasn't yet called connect() to establish an association. |
217 | */ | 217 | */ |
218 | if (!sctp_sstate(sk, ESTABLISHED)) | 218 | if (!sctp_sstate(sk, ESTABLISHED)) |
219 | return NULL; | 219 | return NULL; |
220 | 220 | ||
221 | /* Get the first and the only association from the list. */ | 221 | /* Get the first and the only association from the list. */ |
222 | if (!list_empty(&sctp_sk(sk)->ep->asocs)) | 222 | if (!list_empty(&sctp_sk(sk)->ep->asocs)) |
223 | asoc = list_entry(sctp_sk(sk)->ep->asocs.next, | 223 | asoc = list_entry(sctp_sk(sk)->ep->asocs.next, |
224 | struct sctp_association, asocs); | 224 | struct sctp_association, asocs); |
225 | return asoc; | 225 | return asoc; |
226 | } | 226 | } |
227 | 227 | ||
228 | /* Otherwise this is a UDP-style socket. */ | 228 | /* Otherwise this is a UDP-style socket. */ |
229 | if (!id || (id == (sctp_assoc_t)-1)) | 229 | if (!id || (id == (sctp_assoc_t)-1)) |
230 | return NULL; | 230 | return NULL; |
231 | 231 | ||
232 | spin_lock_bh(&sctp_assocs_id_lock); | 232 | spin_lock_bh(&sctp_assocs_id_lock); |
233 | asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); | 233 | asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id); |
234 | spin_unlock_bh(&sctp_assocs_id_lock); | 234 | spin_unlock_bh(&sctp_assocs_id_lock); |
235 | 235 | ||
236 | if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) | 236 | if (!asoc || (asoc->base.sk != sk) || asoc->base.dead) |
237 | return NULL; | 237 | return NULL; |
238 | 238 | ||
239 | return asoc; | 239 | return asoc; |
240 | } | 240 | } |
241 | 241 | ||
242 | /* Look up the transport from an address and an assoc id. If both address and | 242 | /* Look up the transport from an address and an assoc id. If both address and |
243 | * id are specified, the associations matching the address and the id should be | 243 | * id are specified, the associations matching the address and the id should be |
244 | * the same. | 244 | * the same. |
245 | */ | 245 | */ |
246 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, | 246 | static struct sctp_transport *sctp_addr_id2transport(struct sock *sk, |
247 | struct sockaddr_storage *addr, | 247 | struct sockaddr_storage *addr, |
248 | sctp_assoc_t id) | 248 | sctp_assoc_t id) |
249 | { | 249 | { |
250 | struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; | 250 | struct sctp_association *addr_asoc = NULL, *id_asoc = NULL; |
251 | struct sctp_transport *transport; | 251 | struct sctp_transport *transport; |
252 | union sctp_addr *laddr = (union sctp_addr *)addr; | 252 | union sctp_addr *laddr = (union sctp_addr *)addr; |
253 | 253 | ||
254 | addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, | 254 | addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep, |
255 | laddr, | 255 | laddr, |
256 | &transport); | 256 | &transport); |
257 | 257 | ||
258 | if (!addr_asoc) | 258 | if (!addr_asoc) |
259 | return NULL; | 259 | return NULL; |
260 | 260 | ||
261 | id_asoc = sctp_id2assoc(sk, id); | 261 | id_asoc = sctp_id2assoc(sk, id); |
262 | if (id_asoc && (id_asoc != addr_asoc)) | 262 | if (id_asoc && (id_asoc != addr_asoc)) |
263 | return NULL; | 263 | return NULL; |
264 | 264 | ||
265 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 265 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
266 | (union sctp_addr *)addr); | 266 | (union sctp_addr *)addr); |
267 | 267 | ||
268 | return transport; | 268 | return transport; |
269 | } | 269 | } |
270 | 270 | ||
271 | /* API 3.1.2 bind() - UDP Style Syntax | 271 | /* API 3.1.2 bind() - UDP Style Syntax |
272 | * The syntax of bind() is, | 272 | * The syntax of bind() is, |
273 | * | 273 | * |
274 | * ret = bind(int sd, struct sockaddr *addr, int addrlen); | 274 | * ret = bind(int sd, struct sockaddr *addr, int addrlen); |
275 | * | 275 | * |
276 | * sd - the socket descriptor returned by socket(). | 276 | * sd - the socket descriptor returned by socket(). |
277 | * addr - the address structure (struct sockaddr_in or struct | 277 | * addr - the address structure (struct sockaddr_in or struct |
278 | * sockaddr_in6 [RFC 2553]), | 278 | * sockaddr_in6 [RFC 2553]), |
279 | * addr_len - the size of the address structure. | 279 | * addr_len - the size of the address structure. |
280 | */ | 280 | */ |
281 | SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len) | 281 | SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len) |
282 | { | 282 | { |
283 | int retval = 0; | 283 | int retval = 0; |
284 | 284 | ||
285 | sctp_lock_sock(sk); | 285 | sctp_lock_sock(sk); |
286 | 286 | ||
287 | SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n", | 287 | SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n", |
288 | sk, addr, addr_len); | 288 | sk, addr, addr_len); |
289 | 289 | ||
290 | /* Disallow binding twice. */ | 290 | /* Disallow binding twice. */ |
291 | if (!sctp_sk(sk)->ep->base.bind_addr.port) | 291 | if (!sctp_sk(sk)->ep->base.bind_addr.port) |
292 | retval = sctp_do_bind(sk, (union sctp_addr *)addr, | 292 | retval = sctp_do_bind(sk, (union sctp_addr *)addr, |
293 | addr_len); | 293 | addr_len); |
294 | else | 294 | else |
295 | retval = -EINVAL; | 295 | retval = -EINVAL; |
296 | 296 | ||
297 | sctp_release_sock(sk); | 297 | sctp_release_sock(sk); |
298 | 298 | ||
299 | return retval; | 299 | return retval; |
300 | } | 300 | } |
301 | 301 | ||
302 | static long sctp_get_port_local(struct sock *, union sctp_addr *); | 302 | static long sctp_get_port_local(struct sock *, union sctp_addr *); |
303 | 303 | ||
304 | /* Verify this is a valid sockaddr. */ | 304 | /* Verify this is a valid sockaddr. */ |
305 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, | 305 | static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, |
306 | union sctp_addr *addr, int len) | 306 | union sctp_addr *addr, int len) |
307 | { | 307 | { |
308 | struct sctp_af *af; | 308 | struct sctp_af *af; |
309 | 309 | ||
310 | /* Check minimum size. */ | 310 | /* Check minimum size. */ |
311 | if (len < sizeof (struct sockaddr)) | 311 | if (len < sizeof (struct sockaddr)) |
312 | return NULL; | 312 | return NULL; |
313 | 313 | ||
314 | /* V4 mapped address are really of AF_INET family */ | 314 | /* V4 mapped address are really of AF_INET family */ |
315 | if (addr->sa.sa_family == AF_INET6 && | 315 | if (addr->sa.sa_family == AF_INET6 && |
316 | ipv6_addr_v4mapped(&addr->v6.sin6_addr)) { | 316 | ipv6_addr_v4mapped(&addr->v6.sin6_addr)) { |
317 | if (!opt->pf->af_supported(AF_INET, opt)) | 317 | if (!opt->pf->af_supported(AF_INET, opt)) |
318 | return NULL; | 318 | return NULL; |
319 | } else { | 319 | } else { |
320 | /* Does this PF support this AF? */ | 320 | /* Does this PF support this AF? */ |
321 | if (!opt->pf->af_supported(addr->sa.sa_family, opt)) | 321 | if (!opt->pf->af_supported(addr->sa.sa_family, opt)) |
322 | return NULL; | 322 | return NULL; |
323 | } | 323 | } |
324 | 324 | ||
325 | /* If we get this far, af is valid. */ | 325 | /* If we get this far, af is valid. */ |
326 | af = sctp_get_af_specific(addr->sa.sa_family); | 326 | af = sctp_get_af_specific(addr->sa.sa_family); |
327 | 327 | ||
328 | if (len < af->sockaddr_len) | 328 | if (len < af->sockaddr_len) |
329 | return NULL; | 329 | return NULL; |
330 | 330 | ||
331 | return af; | 331 | return af; |
332 | } | 332 | } |
333 | 333 | ||
334 | /* Bind a local address either to an endpoint or to an association. */ | 334 | /* Bind a local address either to an endpoint or to an association. */ |
335 | SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) | 335 | SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) |
336 | { | 336 | { |
337 | struct sctp_sock *sp = sctp_sk(sk); | 337 | struct sctp_sock *sp = sctp_sk(sk); |
338 | struct sctp_endpoint *ep = sp->ep; | 338 | struct sctp_endpoint *ep = sp->ep; |
339 | struct sctp_bind_addr *bp = &ep->base.bind_addr; | 339 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
340 | struct sctp_af *af; | 340 | struct sctp_af *af; |
341 | unsigned short snum; | 341 | unsigned short snum; |
342 | int ret = 0; | 342 | int ret = 0; |
343 | 343 | ||
344 | /* Common sockaddr verification. */ | 344 | /* Common sockaddr verification. */ |
345 | af = sctp_sockaddr_af(sp, addr, len); | 345 | af = sctp_sockaddr_af(sp, addr, len); |
346 | if (!af) { | 346 | if (!af) { |
347 | SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n", | 347 | SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n", |
348 | sk, addr, len); | 348 | sk, addr, len); |
349 | return -EINVAL; | 349 | return -EINVAL; |
350 | } | 350 | } |
351 | 351 | ||
352 | snum = ntohs(addr->v4.sin_port); | 352 | snum = ntohs(addr->v4.sin_port); |
353 | 353 | ||
354 | SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ", | 354 | SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ", |
355 | ", port: %d, new port: %d, len: %d)\n", | 355 | ", port: %d, new port: %d, len: %d)\n", |
356 | sk, | 356 | sk, |
357 | addr, | 357 | addr, |
358 | bp->port, snum, | 358 | bp->port, snum, |
359 | len); | 359 | len); |
360 | 360 | ||
361 | /* PF specific bind() address verification. */ | 361 | /* PF specific bind() address verification. */ |
362 | if (!sp->pf->bind_verify(sp, addr)) | 362 | if (!sp->pf->bind_verify(sp, addr)) |
363 | return -EADDRNOTAVAIL; | 363 | return -EADDRNOTAVAIL; |
364 | 364 | ||
365 | /* We must either be unbound, or bind to the same port. | 365 | /* We must either be unbound, or bind to the same port. |
366 | * It's OK to allow 0 ports if we are already bound. | 366 | * It's OK to allow 0 ports if we are already bound. |
367 | * We'll just inhert an already bound port in this case | 367 | * We'll just inhert an already bound port in this case |
368 | */ | 368 | */ |
369 | if (bp->port) { | 369 | if (bp->port) { |
370 | if (!snum) | 370 | if (!snum) |
371 | snum = bp->port; | 371 | snum = bp->port; |
372 | else if (snum != bp->port) { | 372 | else if (snum != bp->port) { |
373 | SCTP_DEBUG_PRINTK("sctp_do_bind:" | 373 | SCTP_DEBUG_PRINTK("sctp_do_bind:" |
374 | " New port %d does not match existing port " | 374 | " New port %d does not match existing port " |
375 | "%d.\n", snum, bp->port); | 375 | "%d.\n", snum, bp->port); |
376 | return -EINVAL; | 376 | return -EINVAL; |
377 | } | 377 | } |
378 | } | 378 | } |
379 | 379 | ||
380 | if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) | 380 | if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) |
381 | return -EACCES; | 381 | return -EACCES; |
382 | 382 | ||
383 | /* See if the address matches any of the addresses we may have | 383 | /* See if the address matches any of the addresses we may have |
384 | * already bound before checking against other endpoints. | 384 | * already bound before checking against other endpoints. |
385 | */ | 385 | */ |
386 | if (sctp_bind_addr_match(bp, addr, sp)) | 386 | if (sctp_bind_addr_match(bp, addr, sp)) |
387 | return -EINVAL; | 387 | return -EINVAL; |
388 | 388 | ||
389 | /* Make sure we are allowed to bind here. | 389 | /* Make sure we are allowed to bind here. |
390 | * The function sctp_get_port_local() does duplicate address | 390 | * The function sctp_get_port_local() does duplicate address |
391 | * detection. | 391 | * detection. |
392 | */ | 392 | */ |
393 | addr->v4.sin_port = htons(snum); | 393 | addr->v4.sin_port = htons(snum); |
394 | if ((ret = sctp_get_port_local(sk, addr))) { | 394 | if ((ret = sctp_get_port_local(sk, addr))) { |
395 | return -EADDRINUSE; | 395 | return -EADDRINUSE; |
396 | } | 396 | } |
397 | 397 | ||
398 | /* Refresh ephemeral port. */ | 398 | /* Refresh ephemeral port. */ |
399 | if (!bp->port) | 399 | if (!bp->port) |
400 | bp->port = inet_sk(sk)->inet_num; | 400 | bp->port = inet_sk(sk)->inet_num; |
401 | 401 | ||
402 | /* Add the address to the bind address list. | 402 | /* Add the address to the bind address list. |
403 | * Use GFP_ATOMIC since BHs will be disabled. | 403 | * Use GFP_ATOMIC since BHs will be disabled. |
404 | */ | 404 | */ |
405 | ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC); | 405 | ret = sctp_add_bind_addr(bp, addr, SCTP_ADDR_SRC, GFP_ATOMIC); |
406 | 406 | ||
407 | /* Copy back into socket for getsockname() use. */ | 407 | /* Copy back into socket for getsockname() use. */ |
408 | if (!ret) { | 408 | if (!ret) { |
409 | inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); | 409 | inet_sk(sk)->inet_sport = htons(inet_sk(sk)->inet_num); |
410 | af->to_sk_saddr(addr, sk); | 410 | af->to_sk_saddr(addr, sk); |
411 | } | 411 | } |
412 | 412 | ||
413 | return ret; | 413 | return ret; |
414 | } | 414 | } |
415 | 415 | ||
416 | /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks | 416 | /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks |
417 | * | 417 | * |
418 | * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged | 418 | * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged |
419 | * at any one time. If a sender, after sending an ASCONF chunk, decides | 419 | * at any one time. If a sender, after sending an ASCONF chunk, decides |
420 | * it needs to transfer another ASCONF Chunk, it MUST wait until the | 420 | * it needs to transfer another ASCONF Chunk, it MUST wait until the |
421 | * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a | 421 | * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a |
422 | * subsequent ASCONF. Note this restriction binds each side, so at any | 422 | * subsequent ASCONF. Note this restriction binds each side, so at any |
423 | * time two ASCONF may be in-transit on any given association (one sent | 423 | * time two ASCONF may be in-transit on any given association (one sent |
424 | * from each endpoint). | 424 | * from each endpoint). |
425 | */ | 425 | */ |
426 | static int sctp_send_asconf(struct sctp_association *asoc, | 426 | static int sctp_send_asconf(struct sctp_association *asoc, |
427 | struct sctp_chunk *chunk) | 427 | struct sctp_chunk *chunk) |
428 | { | 428 | { |
429 | int retval = 0; | 429 | int retval = 0; |
430 | 430 | ||
431 | /* If there is an outstanding ASCONF chunk, queue it for later | 431 | /* If there is an outstanding ASCONF chunk, queue it for later |
432 | * transmission. | 432 | * transmission. |
433 | */ | 433 | */ |
434 | if (asoc->addip_last_asconf) { | 434 | if (asoc->addip_last_asconf) { |
435 | list_add_tail(&chunk->list, &asoc->addip_chunk_list); | 435 | list_add_tail(&chunk->list, &asoc->addip_chunk_list); |
436 | goto out; | 436 | goto out; |
437 | } | 437 | } |
438 | 438 | ||
439 | /* Hold the chunk until an ASCONF_ACK is received. */ | 439 | /* Hold the chunk until an ASCONF_ACK is received. */ |
440 | sctp_chunk_hold(chunk); | 440 | sctp_chunk_hold(chunk); |
441 | retval = sctp_primitive_ASCONF(asoc, chunk); | 441 | retval = sctp_primitive_ASCONF(asoc, chunk); |
442 | if (retval) | 442 | if (retval) |
443 | sctp_chunk_free(chunk); | 443 | sctp_chunk_free(chunk); |
444 | else | 444 | else |
445 | asoc->addip_last_asconf = chunk; | 445 | asoc->addip_last_asconf = chunk; |
446 | 446 | ||
447 | out: | 447 | out: |
448 | return retval; | 448 | return retval; |
449 | } | 449 | } |
450 | 450 | ||
451 | /* Add a list of addresses as bind addresses to local endpoint or | 451 | /* Add a list of addresses as bind addresses to local endpoint or |
452 | * association. | 452 | * association. |
453 | * | 453 | * |
454 | * Basically run through each address specified in the addrs/addrcnt | 454 | * Basically run through each address specified in the addrs/addrcnt |
455 | * array/length pair, determine if it is IPv6 or IPv4 and call | 455 | * array/length pair, determine if it is IPv6 or IPv4 and call |
456 | * sctp_do_bind() on it. | 456 | * sctp_do_bind() on it. |
457 | * | 457 | * |
458 | * If any of them fails, then the operation will be reversed and the | 458 | * If any of them fails, then the operation will be reversed and the |
459 | * ones that were added will be removed. | 459 | * ones that were added will be removed. |
460 | * | 460 | * |
461 | * Only sctp_setsockopt_bindx() is supposed to call this function. | 461 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
462 | */ | 462 | */ |
463 | static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) | 463 | static int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
464 | { | 464 | { |
465 | int cnt; | 465 | int cnt; |
466 | int retval = 0; | 466 | int retval = 0; |
467 | void *addr_buf; | 467 | void *addr_buf; |
468 | struct sockaddr *sa_addr; | 468 | struct sockaddr *sa_addr; |
469 | struct sctp_af *af; | 469 | struct sctp_af *af; |
470 | 470 | ||
471 | SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n", | 471 | SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n", |
472 | sk, addrs, addrcnt); | 472 | sk, addrs, addrcnt); |
473 | 473 | ||
474 | addr_buf = addrs; | 474 | addr_buf = addrs; |
475 | for (cnt = 0; cnt < addrcnt; cnt++) { | 475 | for (cnt = 0; cnt < addrcnt; cnt++) { |
476 | /* The list may contain either IPv4 or IPv6 address; | 476 | /* The list may contain either IPv4 or IPv6 address; |
477 | * determine the address length for walking thru the list. | 477 | * determine the address length for walking thru the list. |
478 | */ | 478 | */ |
479 | sa_addr = (struct sockaddr *)addr_buf; | 479 | sa_addr = (struct sockaddr *)addr_buf; |
480 | af = sctp_get_af_specific(sa_addr->sa_family); | 480 | af = sctp_get_af_specific(sa_addr->sa_family); |
481 | if (!af) { | 481 | if (!af) { |
482 | retval = -EINVAL; | 482 | retval = -EINVAL; |
483 | goto err_bindx_add; | 483 | goto err_bindx_add; |
484 | } | 484 | } |
485 | 485 | ||
486 | retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, | 486 | retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr, |
487 | af->sockaddr_len); | 487 | af->sockaddr_len); |
488 | 488 | ||
489 | addr_buf += af->sockaddr_len; | 489 | addr_buf += af->sockaddr_len; |
490 | 490 | ||
491 | err_bindx_add: | 491 | err_bindx_add: |
492 | if (retval < 0) { | 492 | if (retval < 0) { |
493 | /* Failed. Cleanup the ones that have been added */ | 493 | /* Failed. Cleanup the ones that have been added */ |
494 | if (cnt > 0) | 494 | if (cnt > 0) |
495 | sctp_bindx_rem(sk, addrs, cnt); | 495 | sctp_bindx_rem(sk, addrs, cnt); |
496 | return retval; | 496 | return retval; |
497 | } | 497 | } |
498 | } | 498 | } |
499 | 499 | ||
500 | return retval; | 500 | return retval; |
501 | } | 501 | } |
502 | 502 | ||
503 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the | 503 | /* Send an ASCONF chunk with Add IP address parameters to all the peers of the |
504 | * associations that are part of the endpoint indicating that a list of local | 504 | * associations that are part of the endpoint indicating that a list of local |
505 | * addresses are added to the endpoint. | 505 | * addresses are added to the endpoint. |
506 | * | 506 | * |
507 | * If any of the addresses is already in the bind address list of the | 507 | * If any of the addresses is already in the bind address list of the |
508 | * association, we do not send the chunk for that association. But it will not | 508 | * association, we do not send the chunk for that association. But it will not |
509 | * affect other associations. | 509 | * affect other associations. |
510 | * | 510 | * |
511 | * Only sctp_setsockopt_bindx() is supposed to call this function. | 511 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
512 | */ | 512 | */ |
513 | static int sctp_send_asconf_add_ip(struct sock *sk, | 513 | static int sctp_send_asconf_add_ip(struct sock *sk, |
514 | struct sockaddr *addrs, | 514 | struct sockaddr *addrs, |
515 | int addrcnt) | 515 | int addrcnt) |
516 | { | 516 | { |
517 | struct sctp_sock *sp; | 517 | struct sctp_sock *sp; |
518 | struct sctp_endpoint *ep; | 518 | struct sctp_endpoint *ep; |
519 | struct sctp_association *asoc; | 519 | struct sctp_association *asoc; |
520 | struct sctp_bind_addr *bp; | 520 | struct sctp_bind_addr *bp; |
521 | struct sctp_chunk *chunk; | 521 | struct sctp_chunk *chunk; |
522 | struct sctp_sockaddr_entry *laddr; | 522 | struct sctp_sockaddr_entry *laddr; |
523 | union sctp_addr *addr; | 523 | union sctp_addr *addr; |
524 | union sctp_addr saveaddr; | 524 | union sctp_addr saveaddr; |
525 | void *addr_buf; | 525 | void *addr_buf; |
526 | struct sctp_af *af; | 526 | struct sctp_af *af; |
527 | struct list_head *p; | 527 | struct list_head *p; |
528 | int i; | 528 | int i; |
529 | int retval = 0; | 529 | int retval = 0; |
530 | 530 | ||
531 | if (!sctp_addip_enable) | 531 | if (!sctp_addip_enable) |
532 | return retval; | 532 | return retval; |
533 | 533 | ||
534 | sp = sctp_sk(sk); | 534 | sp = sctp_sk(sk); |
535 | ep = sp->ep; | 535 | ep = sp->ep; |
536 | 536 | ||
537 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", | 537 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", |
538 | __func__, sk, addrs, addrcnt); | 538 | __func__, sk, addrs, addrcnt); |
539 | 539 | ||
540 | list_for_each_entry(asoc, &ep->asocs, asocs) { | 540 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
541 | 541 | ||
542 | if (!asoc->peer.asconf_capable) | 542 | if (!asoc->peer.asconf_capable) |
543 | continue; | 543 | continue; |
544 | 544 | ||
545 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) | 545 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP) |
546 | continue; | 546 | continue; |
547 | 547 | ||
548 | if (!sctp_state(asoc, ESTABLISHED)) | 548 | if (!sctp_state(asoc, ESTABLISHED)) |
549 | continue; | 549 | continue; |
550 | 550 | ||
551 | /* Check if any address in the packed array of addresses is | 551 | /* Check if any address in the packed array of addresses is |
552 | * in the bind address list of the association. If so, | 552 | * in the bind address list of the association. If so, |
553 | * do not send the asconf chunk to its peer, but continue with | 553 | * do not send the asconf chunk to its peer, but continue with |
554 | * other associations. | 554 | * other associations. |
555 | */ | 555 | */ |
556 | addr_buf = addrs; | 556 | addr_buf = addrs; |
557 | for (i = 0; i < addrcnt; i++) { | 557 | for (i = 0; i < addrcnt; i++) { |
558 | addr = (union sctp_addr *)addr_buf; | 558 | addr = (union sctp_addr *)addr_buf; |
559 | af = sctp_get_af_specific(addr->v4.sin_family); | 559 | af = sctp_get_af_specific(addr->v4.sin_family); |
560 | if (!af) { | 560 | if (!af) { |
561 | retval = -EINVAL; | 561 | retval = -EINVAL; |
562 | goto out; | 562 | goto out; |
563 | } | 563 | } |
564 | 564 | ||
565 | if (sctp_assoc_lookup_laddr(asoc, addr)) | 565 | if (sctp_assoc_lookup_laddr(asoc, addr)) |
566 | break; | 566 | break; |
567 | 567 | ||
568 | addr_buf += af->sockaddr_len; | 568 | addr_buf += af->sockaddr_len; |
569 | } | 569 | } |
570 | if (i < addrcnt) | 570 | if (i < addrcnt) |
571 | continue; | 571 | continue; |
572 | 572 | ||
573 | /* Use the first valid address in bind addr list of | 573 | /* Use the first valid address in bind addr list of |
574 | * association as Address Parameter of ASCONF CHUNK. | 574 | * association as Address Parameter of ASCONF CHUNK. |
575 | */ | 575 | */ |
576 | bp = &asoc->base.bind_addr; | 576 | bp = &asoc->base.bind_addr; |
577 | p = bp->address_list.next; | 577 | p = bp->address_list.next; |
578 | laddr = list_entry(p, struct sctp_sockaddr_entry, list); | 578 | laddr = list_entry(p, struct sctp_sockaddr_entry, list); |
579 | chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, | 579 | chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs, |
580 | addrcnt, SCTP_PARAM_ADD_IP); | 580 | addrcnt, SCTP_PARAM_ADD_IP); |
581 | if (!chunk) { | 581 | if (!chunk) { |
582 | retval = -ENOMEM; | 582 | retval = -ENOMEM; |
583 | goto out; | 583 | goto out; |
584 | } | 584 | } |
585 | 585 | ||
586 | retval = sctp_send_asconf(asoc, chunk); | 586 | retval = sctp_send_asconf(asoc, chunk); |
587 | if (retval) | 587 | if (retval) |
588 | goto out; | 588 | goto out; |
589 | 589 | ||
590 | /* Add the new addresses to the bind address list with | 590 | /* Add the new addresses to the bind address list with |
591 | * use_as_src set to 0. | 591 | * use_as_src set to 0. |
592 | */ | 592 | */ |
593 | addr_buf = addrs; | 593 | addr_buf = addrs; |
594 | for (i = 0; i < addrcnt; i++) { | 594 | for (i = 0; i < addrcnt; i++) { |
595 | addr = (union sctp_addr *)addr_buf; | 595 | addr = (union sctp_addr *)addr_buf; |
596 | af = sctp_get_af_specific(addr->v4.sin_family); | 596 | af = sctp_get_af_specific(addr->v4.sin_family); |
597 | memcpy(&saveaddr, addr, af->sockaddr_len); | 597 | memcpy(&saveaddr, addr, af->sockaddr_len); |
598 | retval = sctp_add_bind_addr(bp, &saveaddr, | 598 | retval = sctp_add_bind_addr(bp, &saveaddr, |
599 | SCTP_ADDR_NEW, GFP_ATOMIC); | 599 | SCTP_ADDR_NEW, GFP_ATOMIC); |
600 | addr_buf += af->sockaddr_len; | 600 | addr_buf += af->sockaddr_len; |
601 | } | 601 | } |
602 | } | 602 | } |
603 | 603 | ||
604 | out: | 604 | out: |
605 | return retval; | 605 | return retval; |
606 | } | 606 | } |
607 | 607 | ||
608 | /* Remove a list of addresses from bind addresses list. Do not remove the | 608 | /* Remove a list of addresses from bind addresses list. Do not remove the |
609 | * last address. | 609 | * last address. |
610 | * | 610 | * |
611 | * Basically run through each address specified in the addrs/addrcnt | 611 | * Basically run through each address specified in the addrs/addrcnt |
612 | * array/length pair, determine if it is IPv6 or IPv4 and call | 612 | * array/length pair, determine if it is IPv6 or IPv4 and call |
613 | * sctp_del_bind() on it. | 613 | * sctp_del_bind() on it. |
614 | * | 614 | * |
615 | * If any of them fails, then the operation will be reversed and the | 615 | * If any of them fails, then the operation will be reversed and the |
616 | * ones that were removed will be added back. | 616 | * ones that were removed will be added back. |
617 | * | 617 | * |
618 | * At least one address has to be left; if only one address is | 618 | * At least one address has to be left; if only one address is |
619 | * available, the operation will return -EBUSY. | 619 | * available, the operation will return -EBUSY. |
620 | * | 620 | * |
621 | * Only sctp_setsockopt_bindx() is supposed to call this function. | 621 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
622 | */ | 622 | */ |
623 | static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) | 623 | static int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt) |
624 | { | 624 | { |
625 | struct sctp_sock *sp = sctp_sk(sk); | 625 | struct sctp_sock *sp = sctp_sk(sk); |
626 | struct sctp_endpoint *ep = sp->ep; | 626 | struct sctp_endpoint *ep = sp->ep; |
627 | int cnt; | 627 | int cnt; |
628 | struct sctp_bind_addr *bp = &ep->base.bind_addr; | 628 | struct sctp_bind_addr *bp = &ep->base.bind_addr; |
629 | int retval = 0; | 629 | int retval = 0; |
630 | void *addr_buf; | 630 | void *addr_buf; |
631 | union sctp_addr *sa_addr; | 631 | union sctp_addr *sa_addr; |
632 | struct sctp_af *af; | 632 | struct sctp_af *af; |
633 | 633 | ||
634 | SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n", | 634 | SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n", |
635 | sk, addrs, addrcnt); | 635 | sk, addrs, addrcnt); |
636 | 636 | ||
637 | addr_buf = addrs; | 637 | addr_buf = addrs; |
638 | for (cnt = 0; cnt < addrcnt; cnt++) { | 638 | for (cnt = 0; cnt < addrcnt; cnt++) { |
639 | /* If the bind address list is empty or if there is only one | 639 | /* If the bind address list is empty or if there is only one |
640 | * bind address, there is nothing more to be removed (we need | 640 | * bind address, there is nothing more to be removed (we need |
641 | * at least one address here). | 641 | * at least one address here). |
642 | */ | 642 | */ |
643 | if (list_empty(&bp->address_list) || | 643 | if (list_empty(&bp->address_list) || |
644 | (sctp_list_single_entry(&bp->address_list))) { | 644 | (sctp_list_single_entry(&bp->address_list))) { |
645 | retval = -EBUSY; | 645 | retval = -EBUSY; |
646 | goto err_bindx_rem; | 646 | goto err_bindx_rem; |
647 | } | 647 | } |
648 | 648 | ||
649 | sa_addr = (union sctp_addr *)addr_buf; | 649 | sa_addr = (union sctp_addr *)addr_buf; |
650 | af = sctp_get_af_specific(sa_addr->sa.sa_family); | 650 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
651 | if (!af) { | 651 | if (!af) { |
652 | retval = -EINVAL; | 652 | retval = -EINVAL; |
653 | goto err_bindx_rem; | 653 | goto err_bindx_rem; |
654 | } | 654 | } |
655 | 655 | ||
656 | if (!af->addr_valid(sa_addr, sp, NULL)) { | 656 | if (!af->addr_valid(sa_addr, sp, NULL)) { |
657 | retval = -EADDRNOTAVAIL; | 657 | retval = -EADDRNOTAVAIL; |
658 | goto err_bindx_rem; | 658 | goto err_bindx_rem; |
659 | } | 659 | } |
660 | 660 | ||
661 | if (sa_addr->v4.sin_port != htons(bp->port)) { | 661 | if (sa_addr->v4.sin_port != htons(bp->port)) { |
662 | retval = -EINVAL; | 662 | retval = -EINVAL; |
663 | goto err_bindx_rem; | 663 | goto err_bindx_rem; |
664 | } | 664 | } |
665 | 665 | ||
666 | /* FIXME - There is probably a need to check if sk->sk_saddr and | 666 | /* FIXME - There is probably a need to check if sk->sk_saddr and |
667 | * sk->sk_rcv_addr are currently set to one of the addresses to | 667 | * sk->sk_rcv_addr are currently set to one of the addresses to |
668 | * be removed. This is something which needs to be looked into | 668 | * be removed. This is something which needs to be looked into |
669 | * when we are fixing the outstanding issues with multi-homing | 669 | * when we are fixing the outstanding issues with multi-homing |
670 | * socket routing and failover schemes. Refer to comments in | 670 | * socket routing and failover schemes. Refer to comments in |
671 | * sctp_do_bind(). -daisy | 671 | * sctp_do_bind(). -daisy |
672 | */ | 672 | */ |
673 | retval = sctp_del_bind_addr(bp, sa_addr); | 673 | retval = sctp_del_bind_addr(bp, sa_addr); |
674 | 674 | ||
675 | addr_buf += af->sockaddr_len; | 675 | addr_buf += af->sockaddr_len; |
676 | err_bindx_rem: | 676 | err_bindx_rem: |
677 | if (retval < 0) { | 677 | if (retval < 0) { |
678 | /* Failed. Add the ones that has been removed back */ | 678 | /* Failed. Add the ones that has been removed back */ |
679 | if (cnt > 0) | 679 | if (cnt > 0) |
680 | sctp_bindx_add(sk, addrs, cnt); | 680 | sctp_bindx_add(sk, addrs, cnt); |
681 | return retval; | 681 | return retval; |
682 | } | 682 | } |
683 | } | 683 | } |
684 | 684 | ||
685 | return retval; | 685 | return retval; |
686 | } | 686 | } |
687 | 687 | ||
688 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of | 688 | /* Send an ASCONF chunk with Delete IP address parameters to all the peers of |
689 | * the associations that are part of the endpoint indicating that a list of | 689 | * the associations that are part of the endpoint indicating that a list of |
690 | * local addresses are removed from the endpoint. | 690 | * local addresses are removed from the endpoint. |
691 | * | 691 | * |
692 | * If any of the addresses is already in the bind address list of the | 692 | * If any of the addresses is already in the bind address list of the |
693 | * association, we do not send the chunk for that association. But it will not | 693 | * association, we do not send the chunk for that association. But it will not |
694 | * affect other associations. | 694 | * affect other associations. |
695 | * | 695 | * |
696 | * Only sctp_setsockopt_bindx() is supposed to call this function. | 696 | * Only sctp_setsockopt_bindx() is supposed to call this function. |
697 | */ | 697 | */ |
698 | static int sctp_send_asconf_del_ip(struct sock *sk, | 698 | static int sctp_send_asconf_del_ip(struct sock *sk, |
699 | struct sockaddr *addrs, | 699 | struct sockaddr *addrs, |
700 | int addrcnt) | 700 | int addrcnt) |
701 | { | 701 | { |
702 | struct sctp_sock *sp; | 702 | struct sctp_sock *sp; |
703 | struct sctp_endpoint *ep; | 703 | struct sctp_endpoint *ep; |
704 | struct sctp_association *asoc; | 704 | struct sctp_association *asoc; |
705 | struct sctp_transport *transport; | 705 | struct sctp_transport *transport; |
706 | struct sctp_bind_addr *bp; | 706 | struct sctp_bind_addr *bp; |
707 | struct sctp_chunk *chunk; | 707 | struct sctp_chunk *chunk; |
708 | union sctp_addr *laddr; | 708 | union sctp_addr *laddr; |
709 | void *addr_buf; | 709 | void *addr_buf; |
710 | struct sctp_af *af; | 710 | struct sctp_af *af; |
711 | struct sctp_sockaddr_entry *saddr; | 711 | struct sctp_sockaddr_entry *saddr; |
712 | int i; | 712 | int i; |
713 | int retval = 0; | 713 | int retval = 0; |
714 | 714 | ||
715 | if (!sctp_addip_enable) | 715 | if (!sctp_addip_enable) |
716 | return retval; | 716 | return retval; |
717 | 717 | ||
718 | sp = sctp_sk(sk); | 718 | sp = sctp_sk(sk); |
719 | ep = sp->ep; | 719 | ep = sp->ep; |
720 | 720 | ||
721 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", | 721 | SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n", |
722 | __func__, sk, addrs, addrcnt); | 722 | __func__, sk, addrs, addrcnt); |
723 | 723 | ||
724 | list_for_each_entry(asoc, &ep->asocs, asocs) { | 724 | list_for_each_entry(asoc, &ep->asocs, asocs) { |
725 | 725 | ||
726 | if (!asoc->peer.asconf_capable) | 726 | if (!asoc->peer.asconf_capable) |
727 | continue; | 727 | continue; |
728 | 728 | ||
729 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) | 729 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP) |
730 | continue; | 730 | continue; |
731 | 731 | ||
732 | if (!sctp_state(asoc, ESTABLISHED)) | 732 | if (!sctp_state(asoc, ESTABLISHED)) |
733 | continue; | 733 | continue; |
734 | 734 | ||
735 | /* Check if any address in the packed array of addresses is | 735 | /* Check if any address in the packed array of addresses is |
736 | * not present in the bind address list of the association. | 736 | * not present in the bind address list of the association. |
737 | * If so, do not send the asconf chunk to its peer, but | 737 | * If so, do not send the asconf chunk to its peer, but |
738 | * continue with other associations. | 738 | * continue with other associations. |
739 | */ | 739 | */ |
740 | addr_buf = addrs; | 740 | addr_buf = addrs; |
741 | for (i = 0; i < addrcnt; i++) { | 741 | for (i = 0; i < addrcnt; i++) { |
742 | laddr = (union sctp_addr *)addr_buf; | 742 | laddr = (union sctp_addr *)addr_buf; |
743 | af = sctp_get_af_specific(laddr->v4.sin_family); | 743 | af = sctp_get_af_specific(laddr->v4.sin_family); |
744 | if (!af) { | 744 | if (!af) { |
745 | retval = -EINVAL; | 745 | retval = -EINVAL; |
746 | goto out; | 746 | goto out; |
747 | } | 747 | } |
748 | 748 | ||
749 | if (!sctp_assoc_lookup_laddr(asoc, laddr)) | 749 | if (!sctp_assoc_lookup_laddr(asoc, laddr)) |
750 | break; | 750 | break; |
751 | 751 | ||
752 | addr_buf += af->sockaddr_len; | 752 | addr_buf += af->sockaddr_len; |
753 | } | 753 | } |
754 | if (i < addrcnt) | 754 | if (i < addrcnt) |
755 | continue; | 755 | continue; |
756 | 756 | ||
757 | /* Find one address in the association's bind address list | 757 | /* Find one address in the association's bind address list |
758 | * that is not in the packed array of addresses. This is to | 758 | * that is not in the packed array of addresses. This is to |
759 | * make sure that we do not delete all the addresses in the | 759 | * make sure that we do not delete all the addresses in the |
760 | * association. | 760 | * association. |
761 | */ | 761 | */ |
762 | bp = &asoc->base.bind_addr; | 762 | bp = &asoc->base.bind_addr; |
763 | laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, | 763 | laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs, |
764 | addrcnt, sp); | 764 | addrcnt, sp); |
765 | if (!laddr) | 765 | if (!laddr) |
766 | continue; | 766 | continue; |
767 | 767 | ||
768 | /* We do not need RCU protection throughout this loop | 768 | /* We do not need RCU protection throughout this loop |
769 | * because this is done under a socket lock from the | 769 | * because this is done under a socket lock from the |
770 | * setsockopt call. | 770 | * setsockopt call. |
771 | */ | 771 | */ |
772 | chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, | 772 | chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt, |
773 | SCTP_PARAM_DEL_IP); | 773 | SCTP_PARAM_DEL_IP); |
774 | if (!chunk) { | 774 | if (!chunk) { |
775 | retval = -ENOMEM; | 775 | retval = -ENOMEM; |
776 | goto out; | 776 | goto out; |
777 | } | 777 | } |
778 | 778 | ||
779 | /* Reset use_as_src flag for the addresses in the bind address | 779 | /* Reset use_as_src flag for the addresses in the bind address |
780 | * list that are to be deleted. | 780 | * list that are to be deleted. |
781 | */ | 781 | */ |
782 | addr_buf = addrs; | 782 | addr_buf = addrs; |
783 | for (i = 0; i < addrcnt; i++) { | 783 | for (i = 0; i < addrcnt; i++) { |
784 | laddr = (union sctp_addr *)addr_buf; | 784 | laddr = (union sctp_addr *)addr_buf; |
785 | af = sctp_get_af_specific(laddr->v4.sin_family); | 785 | af = sctp_get_af_specific(laddr->v4.sin_family); |
786 | list_for_each_entry(saddr, &bp->address_list, list) { | 786 | list_for_each_entry(saddr, &bp->address_list, list) { |
787 | if (sctp_cmp_addr_exact(&saddr->a, laddr)) | 787 | if (sctp_cmp_addr_exact(&saddr->a, laddr)) |
788 | saddr->state = SCTP_ADDR_DEL; | 788 | saddr->state = SCTP_ADDR_DEL; |
789 | } | 789 | } |
790 | addr_buf += af->sockaddr_len; | 790 | addr_buf += af->sockaddr_len; |
791 | } | 791 | } |
792 | 792 | ||
793 | /* Update the route and saddr entries for all the transports | 793 | /* Update the route and saddr entries for all the transports |
794 | * as some of the addresses in the bind address list are | 794 | * as some of the addresses in the bind address list are |
795 | * about to be deleted and cannot be used as source addresses. | 795 | * about to be deleted and cannot be used as source addresses. |
796 | */ | 796 | */ |
797 | list_for_each_entry(transport, &asoc->peer.transport_addr_list, | 797 | list_for_each_entry(transport, &asoc->peer.transport_addr_list, |
798 | transports) { | 798 | transports) { |
799 | dst_release(transport->dst); | 799 | dst_release(transport->dst); |
800 | sctp_transport_route(transport, NULL, | 800 | sctp_transport_route(transport, NULL, |
801 | sctp_sk(asoc->base.sk)); | 801 | sctp_sk(asoc->base.sk)); |
802 | } | 802 | } |
803 | 803 | ||
804 | retval = sctp_send_asconf(asoc, chunk); | 804 | retval = sctp_send_asconf(asoc, chunk); |
805 | } | 805 | } |
806 | out: | 806 | out: |
807 | return retval; | 807 | return retval; |
808 | } | 808 | } |
809 | 809 | ||
810 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() | 810 | /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt() |
811 | * | 811 | * |
812 | * API 8.1 | 812 | * API 8.1 |
813 | * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, | 813 | * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, |
814 | * int flags); | 814 | * int flags); |
815 | * | 815 | * |
816 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 816 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
817 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 817 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
818 | * or IPv6 addresses. | 818 | * or IPv6 addresses. |
819 | * | 819 | * |
820 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 820 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
821 | * Section 3.1.2 for this usage. | 821 | * Section 3.1.2 for this usage. |
822 | * | 822 | * |
823 | * addrs is a pointer to an array of one or more socket addresses. Each | 823 | * addrs is a pointer to an array of one or more socket addresses. Each |
824 | * address is contained in its appropriate structure (i.e. struct | 824 | * address is contained in its appropriate structure (i.e. struct |
825 | * sockaddr_in or struct sockaddr_in6) the family of the address type | 825 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
826 | * must be used to distinguish the address length (note that this | 826 | * must be used to distinguish the address length (note that this |
827 | * representation is termed a "packed array" of addresses). The caller | 827 | * representation is termed a "packed array" of addresses). The caller |
828 | * specifies the number of addresses in the array with addrcnt. | 828 | * specifies the number of addresses in the array with addrcnt. |
829 | * | 829 | * |
830 | * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns | 830 | * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns |
831 | * -1, and sets errno to the appropriate error code. | 831 | * -1, and sets errno to the appropriate error code. |
832 | * | 832 | * |
833 | * For SCTP, the port given in each socket address must be the same, or | 833 | * For SCTP, the port given in each socket address must be the same, or |
834 | * sctp_bindx() will fail, setting errno to EINVAL. | 834 | * sctp_bindx() will fail, setting errno to EINVAL. |
835 | * | 835 | * |
836 | * The flags parameter is formed from the bitwise OR of zero or more of | 836 | * The flags parameter is formed from the bitwise OR of zero or more of |
837 | * the following currently defined flags: | 837 | * the following currently defined flags: |
838 | * | 838 | * |
839 | * SCTP_BINDX_ADD_ADDR | 839 | * SCTP_BINDX_ADD_ADDR |
840 | * | 840 | * |
841 | * SCTP_BINDX_REM_ADDR | 841 | * SCTP_BINDX_REM_ADDR |
842 | * | 842 | * |
843 | * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the | 843 | * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the |
844 | * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given | 844 | * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given |
845 | * addresses from the association. The two flags are mutually exclusive; | 845 | * addresses from the association. The two flags are mutually exclusive; |
846 | * if both are given, sctp_bindx() will fail with EINVAL. A caller may | 846 | * if both are given, sctp_bindx() will fail with EINVAL. A caller may |
847 | * not remove all addresses from an association; sctp_bindx() will | 847 | * not remove all addresses from an association; sctp_bindx() will |
848 | * reject such an attempt with EINVAL. | 848 | * reject such an attempt with EINVAL. |
849 | * | 849 | * |
850 | * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate | 850 | * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate |
851 | * additional addresses with an endpoint after calling bind(). Or use | 851 | * additional addresses with an endpoint after calling bind(). Or use |
852 | * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening | 852 | * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening |
853 | * socket is associated with so that no new association accepted will be | 853 | * socket is associated with so that no new association accepted will be |
854 | * associated with those addresses. If the endpoint supports dynamic | 854 | * associated with those addresses. If the endpoint supports dynamic |
855 | * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a | 855 | * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a |
856 | * endpoint to send the appropriate message to the peer to change the | 856 | * endpoint to send the appropriate message to the peer to change the |
857 | * peers address lists. | 857 | * peers address lists. |
858 | * | 858 | * |
859 | * Adding and removing addresses from a connected association is | 859 | * Adding and removing addresses from a connected association is |
860 | * optional functionality. Implementations that do not support this | 860 | * optional functionality. Implementations that do not support this |
861 | * functionality should return EOPNOTSUPP. | 861 | * functionality should return EOPNOTSUPP. |
862 | * | 862 | * |
863 | * Basically do nothing but copying the addresses from user to kernel | 863 | * Basically do nothing but copying the addresses from user to kernel |
864 | * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. | 864 | * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk. |
865 | * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() | 865 | * This is used for tunneling the sctp_bindx() request through sctp_setsockopt() |
866 | * from userspace. | 866 | * from userspace. |
867 | * | 867 | * |
868 | * We don't use copy_from_user() for optimization: we first do the | 868 | * We don't use copy_from_user() for optimization: we first do the |
869 | * sanity checks (buffer size -fast- and access check-healthy | 869 | * sanity checks (buffer size -fast- and access check-healthy |
870 | * pointer); if all of those succeed, then we can alloc the memory | 870 | * pointer); if all of those succeed, then we can alloc the memory |
871 | * (expensive operation) needed to copy the data to kernel. Then we do | 871 | * (expensive operation) needed to copy the data to kernel. Then we do |
872 | * the copying without checking the user space area | 872 | * the copying without checking the user space area |
873 | * (__copy_from_user()). | 873 | * (__copy_from_user()). |
874 | * | 874 | * |
875 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 875 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
876 | * it. | 876 | * it. |
877 | * | 877 | * |
878 | * sk The sk of the socket | 878 | * sk The sk of the socket |
879 | * addrs The pointer to the addresses in user land | 879 | * addrs The pointer to the addresses in user land |
880 | * addrssize Size of the addrs buffer | 880 | * addrssize Size of the addrs buffer |
881 | * op Operation to perform (add or remove, see the flags of | 881 | * op Operation to perform (add or remove, see the flags of |
882 | * sctp_bindx) | 882 | * sctp_bindx) |
883 | * | 883 | * |
884 | * Returns 0 if ok, <0 errno code on error. | 884 | * Returns 0 if ok, <0 errno code on error. |
885 | */ | 885 | */ |
886 | SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, | 886 | SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk, |
887 | struct sockaddr __user *addrs, | 887 | struct sockaddr __user *addrs, |
888 | int addrs_size, int op) | 888 | int addrs_size, int op) |
889 | { | 889 | { |
890 | struct sockaddr *kaddrs; | 890 | struct sockaddr *kaddrs; |
891 | int err; | 891 | int err; |
892 | int addrcnt = 0; | 892 | int addrcnt = 0; |
893 | int walk_size = 0; | 893 | int walk_size = 0; |
894 | struct sockaddr *sa_addr; | 894 | struct sockaddr *sa_addr; |
895 | void *addr_buf; | 895 | void *addr_buf; |
896 | struct sctp_af *af; | 896 | struct sctp_af *af; |
897 | 897 | ||
898 | SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p" | 898 | SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p" |
899 | " addrs_size %d opt %d\n", sk, addrs, addrs_size, op); | 899 | " addrs_size %d opt %d\n", sk, addrs, addrs_size, op); |
900 | 900 | ||
901 | if (unlikely(addrs_size <= 0)) | 901 | if (unlikely(addrs_size <= 0)) |
902 | return -EINVAL; | 902 | return -EINVAL; |
903 | 903 | ||
904 | /* Check the user passed a healthy pointer. */ | 904 | /* Check the user passed a healthy pointer. */ |
905 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) | 905 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
906 | return -EFAULT; | 906 | return -EFAULT; |
907 | 907 | ||
908 | /* Alloc space for the address array in kernel memory. */ | 908 | /* Alloc space for the address array in kernel memory. */ |
909 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); | 909 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); |
910 | if (unlikely(!kaddrs)) | 910 | if (unlikely(!kaddrs)) |
911 | return -ENOMEM; | 911 | return -ENOMEM; |
912 | 912 | ||
913 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { | 913 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
914 | kfree(kaddrs); | 914 | kfree(kaddrs); |
915 | return -EFAULT; | 915 | return -EFAULT; |
916 | } | 916 | } |
917 | 917 | ||
918 | /* Walk through the addrs buffer and count the number of addresses. */ | 918 | /* Walk through the addrs buffer and count the number of addresses. */ |
919 | addr_buf = kaddrs; | 919 | addr_buf = kaddrs; |
920 | while (walk_size < addrs_size) { | 920 | while (walk_size < addrs_size) { |
921 | sa_addr = (struct sockaddr *)addr_buf; | 921 | sa_addr = (struct sockaddr *)addr_buf; |
922 | af = sctp_get_af_specific(sa_addr->sa_family); | 922 | af = sctp_get_af_specific(sa_addr->sa_family); |
923 | 923 | ||
924 | /* If the address family is not supported or if this address | 924 | /* If the address family is not supported or if this address |
925 | * causes the address buffer to overflow return EINVAL. | 925 | * causes the address buffer to overflow return EINVAL. |
926 | */ | 926 | */ |
927 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 927 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
928 | kfree(kaddrs); | 928 | kfree(kaddrs); |
929 | return -EINVAL; | 929 | return -EINVAL; |
930 | } | 930 | } |
931 | addrcnt++; | 931 | addrcnt++; |
932 | addr_buf += af->sockaddr_len; | 932 | addr_buf += af->sockaddr_len; |
933 | walk_size += af->sockaddr_len; | 933 | walk_size += af->sockaddr_len; |
934 | } | 934 | } |
935 | 935 | ||
936 | /* Do the work. */ | 936 | /* Do the work. */ |
937 | switch (op) { | 937 | switch (op) { |
938 | case SCTP_BINDX_ADD_ADDR: | 938 | case SCTP_BINDX_ADD_ADDR: |
939 | err = sctp_bindx_add(sk, kaddrs, addrcnt); | 939 | err = sctp_bindx_add(sk, kaddrs, addrcnt); |
940 | if (err) | 940 | if (err) |
941 | goto out; | 941 | goto out; |
942 | err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); | 942 | err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt); |
943 | break; | 943 | break; |
944 | 944 | ||
945 | case SCTP_BINDX_REM_ADDR: | 945 | case SCTP_BINDX_REM_ADDR: |
946 | err = sctp_bindx_rem(sk, kaddrs, addrcnt); | 946 | err = sctp_bindx_rem(sk, kaddrs, addrcnt); |
947 | if (err) | 947 | if (err) |
948 | goto out; | 948 | goto out; |
949 | err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); | 949 | err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt); |
950 | break; | 950 | break; |
951 | 951 | ||
952 | default: | 952 | default: |
953 | err = -EINVAL; | 953 | err = -EINVAL; |
954 | break; | 954 | break; |
955 | } | 955 | } |
956 | 956 | ||
957 | out: | 957 | out: |
958 | kfree(kaddrs); | 958 | kfree(kaddrs); |
959 | 959 | ||
960 | return err; | 960 | return err; |
961 | } | 961 | } |
962 | 962 | ||
963 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) | 963 | /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size) |
964 | * | 964 | * |
965 | * Common routine for handling connect() and sctp_connectx(). | 965 | * Common routine for handling connect() and sctp_connectx(). |
966 | * Connect will come in with just a single address. | 966 | * Connect will come in with just a single address. |
967 | */ | 967 | */ |
968 | static int __sctp_connect(struct sock* sk, | 968 | static int __sctp_connect(struct sock* sk, |
969 | struct sockaddr *kaddrs, | 969 | struct sockaddr *kaddrs, |
970 | int addrs_size, | 970 | int addrs_size, |
971 | sctp_assoc_t *assoc_id) | 971 | sctp_assoc_t *assoc_id) |
972 | { | 972 | { |
973 | struct sctp_sock *sp; | 973 | struct sctp_sock *sp; |
974 | struct sctp_endpoint *ep; | 974 | struct sctp_endpoint *ep; |
975 | struct sctp_association *asoc = NULL; | 975 | struct sctp_association *asoc = NULL; |
976 | struct sctp_association *asoc2; | 976 | struct sctp_association *asoc2; |
977 | struct sctp_transport *transport; | 977 | struct sctp_transport *transport; |
978 | union sctp_addr to; | 978 | union sctp_addr to; |
979 | struct sctp_af *af; | 979 | struct sctp_af *af; |
980 | sctp_scope_t scope; | 980 | sctp_scope_t scope; |
981 | long timeo; | 981 | long timeo; |
982 | int err = 0; | 982 | int err = 0; |
983 | int addrcnt = 0; | 983 | int addrcnt = 0; |
984 | int walk_size = 0; | 984 | int walk_size = 0; |
985 | union sctp_addr *sa_addr = NULL; | 985 | union sctp_addr *sa_addr = NULL; |
986 | void *addr_buf; | 986 | void *addr_buf; |
987 | unsigned short port; | 987 | unsigned short port; |
988 | unsigned int f_flags = 0; | 988 | unsigned int f_flags = 0; |
989 | 989 | ||
990 | sp = sctp_sk(sk); | 990 | sp = sctp_sk(sk); |
991 | ep = sp->ep; | 991 | ep = sp->ep; |
992 | 992 | ||
993 | /* connect() cannot be done on a socket that is already in ESTABLISHED | 993 | /* connect() cannot be done on a socket that is already in ESTABLISHED |
994 | * state - UDP-style peeled off socket or a TCP-style socket that | 994 | * state - UDP-style peeled off socket or a TCP-style socket that |
995 | * is already connected. | 995 | * is already connected. |
996 | * It cannot be done even on a TCP-style listening socket. | 996 | * It cannot be done even on a TCP-style listening socket. |
997 | */ | 997 | */ |
998 | if (sctp_sstate(sk, ESTABLISHED) || | 998 | if (sctp_sstate(sk, ESTABLISHED) || |
999 | (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { | 999 | (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) { |
1000 | err = -EISCONN; | 1000 | err = -EISCONN; |
1001 | goto out_free; | 1001 | goto out_free; |
1002 | } | 1002 | } |
1003 | 1003 | ||
1004 | /* Walk through the addrs buffer and count the number of addresses. */ | 1004 | /* Walk through the addrs buffer and count the number of addresses. */ |
1005 | addr_buf = kaddrs; | 1005 | addr_buf = kaddrs; |
1006 | while (walk_size < addrs_size) { | 1006 | while (walk_size < addrs_size) { |
1007 | sa_addr = (union sctp_addr *)addr_buf; | 1007 | sa_addr = (union sctp_addr *)addr_buf; |
1008 | af = sctp_get_af_specific(sa_addr->sa.sa_family); | 1008 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
1009 | port = ntohs(sa_addr->v4.sin_port); | 1009 | port = ntohs(sa_addr->v4.sin_port); |
1010 | 1010 | ||
1011 | /* If the address family is not supported or if this address | 1011 | /* If the address family is not supported or if this address |
1012 | * causes the address buffer to overflow return EINVAL. | 1012 | * causes the address buffer to overflow return EINVAL. |
1013 | */ | 1013 | */ |
1014 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { | 1014 | if (!af || (walk_size + af->sockaddr_len) > addrs_size) { |
1015 | err = -EINVAL; | 1015 | err = -EINVAL; |
1016 | goto out_free; | 1016 | goto out_free; |
1017 | } | 1017 | } |
1018 | 1018 | ||
1019 | /* Save current address so we can work with it */ | 1019 | /* Save current address so we can work with it */ |
1020 | memcpy(&to, sa_addr, af->sockaddr_len); | 1020 | memcpy(&to, sa_addr, af->sockaddr_len); |
1021 | 1021 | ||
1022 | err = sctp_verify_addr(sk, &to, af->sockaddr_len); | 1022 | err = sctp_verify_addr(sk, &to, af->sockaddr_len); |
1023 | if (err) | 1023 | if (err) |
1024 | goto out_free; | 1024 | goto out_free; |
1025 | 1025 | ||
1026 | /* Make sure the destination port is correctly set | 1026 | /* Make sure the destination port is correctly set |
1027 | * in all addresses. | 1027 | * in all addresses. |
1028 | */ | 1028 | */ |
1029 | if (asoc && asoc->peer.port && asoc->peer.port != port) | 1029 | if (asoc && asoc->peer.port && asoc->peer.port != port) |
1030 | goto out_free; | 1030 | goto out_free; |
1031 | 1031 | ||
1032 | 1032 | ||
1033 | /* Check if there already is a matching association on the | 1033 | /* Check if there already is a matching association on the |
1034 | * endpoint (other than the one created here). | 1034 | * endpoint (other than the one created here). |
1035 | */ | 1035 | */ |
1036 | asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); | 1036 | asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport); |
1037 | if (asoc2 && asoc2 != asoc) { | 1037 | if (asoc2 && asoc2 != asoc) { |
1038 | if (asoc2->state >= SCTP_STATE_ESTABLISHED) | 1038 | if (asoc2->state >= SCTP_STATE_ESTABLISHED) |
1039 | err = -EISCONN; | 1039 | err = -EISCONN; |
1040 | else | 1040 | else |
1041 | err = -EALREADY; | 1041 | err = -EALREADY; |
1042 | goto out_free; | 1042 | goto out_free; |
1043 | } | 1043 | } |
1044 | 1044 | ||
1045 | /* If we could not find a matching association on the endpoint, | 1045 | /* If we could not find a matching association on the endpoint, |
1046 | * make sure that there is no peeled-off association matching | 1046 | * make sure that there is no peeled-off association matching |
1047 | * the peer address even on another socket. | 1047 | * the peer address even on another socket. |
1048 | */ | 1048 | */ |
1049 | if (sctp_endpoint_is_peeled_off(ep, &to)) { | 1049 | if (sctp_endpoint_is_peeled_off(ep, &to)) { |
1050 | err = -EADDRNOTAVAIL; | 1050 | err = -EADDRNOTAVAIL; |
1051 | goto out_free; | 1051 | goto out_free; |
1052 | } | 1052 | } |
1053 | 1053 | ||
1054 | if (!asoc) { | 1054 | if (!asoc) { |
1055 | /* If a bind() or sctp_bindx() is not called prior to | 1055 | /* If a bind() or sctp_bindx() is not called prior to |
1056 | * an sctp_connectx() call, the system picks an | 1056 | * an sctp_connectx() call, the system picks an |
1057 | * ephemeral port and will choose an address set | 1057 | * ephemeral port and will choose an address set |
1058 | * equivalent to binding with a wildcard address. | 1058 | * equivalent to binding with a wildcard address. |
1059 | */ | 1059 | */ |
1060 | if (!ep->base.bind_addr.port) { | 1060 | if (!ep->base.bind_addr.port) { |
1061 | if (sctp_autobind(sk)) { | 1061 | if (sctp_autobind(sk)) { |
1062 | err = -EAGAIN; | 1062 | err = -EAGAIN; |
1063 | goto out_free; | 1063 | goto out_free; |
1064 | } | 1064 | } |
1065 | } else { | 1065 | } else { |
1066 | /* | 1066 | /* |
1067 | * If an unprivileged user inherits a 1-many | 1067 | * If an unprivileged user inherits a 1-many |
1068 | * style socket with open associations on a | 1068 | * style socket with open associations on a |
1069 | * privileged port, it MAY be permitted to | 1069 | * privileged port, it MAY be permitted to |
1070 | * accept new associations, but it SHOULD NOT | 1070 | * accept new associations, but it SHOULD NOT |
1071 | * be permitted to open new associations. | 1071 | * be permitted to open new associations. |
1072 | */ | 1072 | */ |
1073 | if (ep->base.bind_addr.port < PROT_SOCK && | 1073 | if (ep->base.bind_addr.port < PROT_SOCK && |
1074 | !capable(CAP_NET_BIND_SERVICE)) { | 1074 | !capable(CAP_NET_BIND_SERVICE)) { |
1075 | err = -EACCES; | 1075 | err = -EACCES; |
1076 | goto out_free; | 1076 | goto out_free; |
1077 | } | 1077 | } |
1078 | } | 1078 | } |
1079 | 1079 | ||
1080 | scope = sctp_scope(&to); | 1080 | scope = sctp_scope(&to); |
1081 | asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 1081 | asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
1082 | if (!asoc) { | 1082 | if (!asoc) { |
1083 | err = -ENOMEM; | 1083 | err = -ENOMEM; |
1084 | goto out_free; | 1084 | goto out_free; |
1085 | } | 1085 | } |
1086 | 1086 | ||
1087 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, | 1087 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, |
1088 | GFP_KERNEL); | 1088 | GFP_KERNEL); |
1089 | if (err < 0) { | 1089 | if (err < 0) { |
1090 | goto out_free; | 1090 | goto out_free; |
1091 | } | 1091 | } |
1092 | 1092 | ||
1093 | } | 1093 | } |
1094 | 1094 | ||
1095 | /* Prime the peer's transport structures. */ | 1095 | /* Prime the peer's transport structures. */ |
1096 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, | 1096 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, |
1097 | SCTP_UNKNOWN); | 1097 | SCTP_UNKNOWN); |
1098 | if (!transport) { | 1098 | if (!transport) { |
1099 | err = -ENOMEM; | 1099 | err = -ENOMEM; |
1100 | goto out_free; | 1100 | goto out_free; |
1101 | } | 1101 | } |
1102 | 1102 | ||
1103 | addrcnt++; | 1103 | addrcnt++; |
1104 | addr_buf += af->sockaddr_len; | 1104 | addr_buf += af->sockaddr_len; |
1105 | walk_size += af->sockaddr_len; | 1105 | walk_size += af->sockaddr_len; |
1106 | } | 1106 | } |
1107 | 1107 | ||
1108 | /* In case the user of sctp_connectx() wants an association | 1108 | /* In case the user of sctp_connectx() wants an association |
1109 | * id back, assign one now. | 1109 | * id back, assign one now. |
1110 | */ | 1110 | */ |
1111 | if (assoc_id) { | 1111 | if (assoc_id) { |
1112 | err = sctp_assoc_set_id(asoc, GFP_KERNEL); | 1112 | err = sctp_assoc_set_id(asoc, GFP_KERNEL); |
1113 | if (err < 0) | 1113 | if (err < 0) |
1114 | goto out_free; | 1114 | goto out_free; |
1115 | } | 1115 | } |
1116 | 1116 | ||
1117 | err = sctp_primitive_ASSOCIATE(asoc, NULL); | 1117 | err = sctp_primitive_ASSOCIATE(asoc, NULL); |
1118 | if (err < 0) { | 1118 | if (err < 0) { |
1119 | goto out_free; | 1119 | goto out_free; |
1120 | } | 1120 | } |
1121 | 1121 | ||
1122 | /* Initialize sk's dport and daddr for getpeername() */ | 1122 | /* Initialize sk's dport and daddr for getpeername() */ |
1123 | inet_sk(sk)->inet_dport = htons(asoc->peer.port); | 1123 | inet_sk(sk)->inet_dport = htons(asoc->peer.port); |
1124 | af = sctp_get_af_specific(sa_addr->sa.sa_family); | 1124 | af = sctp_get_af_specific(sa_addr->sa.sa_family); |
1125 | af->to_sk_daddr(sa_addr, sk); | 1125 | af->to_sk_daddr(sa_addr, sk); |
1126 | sk->sk_err = 0; | 1126 | sk->sk_err = 0; |
1127 | 1127 | ||
1128 | /* in-kernel sockets don't generally have a file allocated to them | 1128 | /* in-kernel sockets don't generally have a file allocated to them |
1129 | * if all they do is call sock_create_kern(). | 1129 | * if all they do is call sock_create_kern(). |
1130 | */ | 1130 | */ |
1131 | if (sk->sk_socket->file) | 1131 | if (sk->sk_socket->file) |
1132 | f_flags = sk->sk_socket->file->f_flags; | 1132 | f_flags = sk->sk_socket->file->f_flags; |
1133 | 1133 | ||
1134 | timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); | 1134 | timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK); |
1135 | 1135 | ||
1136 | err = sctp_wait_for_connect(asoc, &timeo); | 1136 | err = sctp_wait_for_connect(asoc, &timeo); |
1137 | if ((err == 0 || err == -EINPROGRESS) && assoc_id) | 1137 | if ((err == 0 || err == -EINPROGRESS) && assoc_id) |
1138 | *assoc_id = asoc->assoc_id; | 1138 | *assoc_id = asoc->assoc_id; |
1139 | 1139 | ||
1140 | /* Don't free association on exit. */ | 1140 | /* Don't free association on exit. */ |
1141 | asoc = NULL; | 1141 | asoc = NULL; |
1142 | 1142 | ||
1143 | out_free: | 1143 | out_free: |
1144 | 1144 | ||
1145 | SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p" | 1145 | SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p" |
1146 | " kaddrs: %p err: %d\n", | 1146 | " kaddrs: %p err: %d\n", |
1147 | asoc, kaddrs, err); | 1147 | asoc, kaddrs, err); |
1148 | if (asoc) | 1148 | if (asoc) |
1149 | sctp_association_free(asoc); | 1149 | sctp_association_free(asoc); |
1150 | return err; | 1150 | return err; |
1151 | } | 1151 | } |
1152 | 1152 | ||
1153 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() | 1153 | /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt() |
1154 | * | 1154 | * |
1155 | * API 8.9 | 1155 | * API 8.9 |
1156 | * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, | 1156 | * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt, |
1157 | * sctp_assoc_t *asoc); | 1157 | * sctp_assoc_t *asoc); |
1158 | * | 1158 | * |
1159 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. | 1159 | * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses. |
1160 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 | 1160 | * If the sd is an IPv6 socket, the addresses passed can either be IPv4 |
1161 | * or IPv6 addresses. | 1161 | * or IPv6 addresses. |
1162 | * | 1162 | * |
1163 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see | 1163 | * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see |
1164 | * Section 3.1.2 for this usage. | 1164 | * Section 3.1.2 for this usage. |
1165 | * | 1165 | * |
1166 | * addrs is a pointer to an array of one or more socket addresses. Each | 1166 | * addrs is a pointer to an array of one or more socket addresses. Each |
1167 | * address is contained in its appropriate structure (i.e. struct | 1167 | * address is contained in its appropriate structure (i.e. struct |
1168 | * sockaddr_in or struct sockaddr_in6) the family of the address type | 1168 | * sockaddr_in or struct sockaddr_in6) the family of the address type |
1169 | * must be used to distengish the address length (note that this | 1169 | * must be used to distengish the address length (note that this |
1170 | * representation is termed a "packed array" of addresses). The caller | 1170 | * representation is termed a "packed array" of addresses). The caller |
1171 | * specifies the number of addresses in the array with addrcnt. | 1171 | * specifies the number of addresses in the array with addrcnt. |
1172 | * | 1172 | * |
1173 | * On success, sctp_connectx() returns 0. It also sets the assoc_id to | 1173 | * On success, sctp_connectx() returns 0. It also sets the assoc_id to |
1174 | * the association id of the new association. On failure, sctp_connectx() | 1174 | * the association id of the new association. On failure, sctp_connectx() |
1175 | * returns -1, and sets errno to the appropriate error code. The assoc_id | 1175 | * returns -1, and sets errno to the appropriate error code. The assoc_id |
1176 | * is not touched by the kernel. | 1176 | * is not touched by the kernel. |
1177 | * | 1177 | * |
1178 | * For SCTP, the port given in each socket address must be the same, or | 1178 | * For SCTP, the port given in each socket address must be the same, or |
1179 | * sctp_connectx() will fail, setting errno to EINVAL. | 1179 | * sctp_connectx() will fail, setting errno to EINVAL. |
1180 | * | 1180 | * |
1181 | * An application can use sctp_connectx to initiate an association with | 1181 | * An application can use sctp_connectx to initiate an association with |
1182 | * an endpoint that is multi-homed. Much like sctp_bindx() this call | 1182 | * an endpoint that is multi-homed. Much like sctp_bindx() this call |
1183 | * allows a caller to specify multiple addresses at which a peer can be | 1183 | * allows a caller to specify multiple addresses at which a peer can be |
1184 | * reached. The way the SCTP stack uses the list of addresses to set up | 1184 | * reached. The way the SCTP stack uses the list of addresses to set up |
1185 | * the association is implementation dependant. This function only | 1185 | * the association is implementation dependant. This function only |
1186 | * specifies that the stack will try to make use of all the addresses in | 1186 | * specifies that the stack will try to make use of all the addresses in |
1187 | * the list when needed. | 1187 | * the list when needed. |
1188 | * | 1188 | * |
1189 | * Note that the list of addresses passed in is only used for setting up | 1189 | * Note that the list of addresses passed in is only used for setting up |
1190 | * the association. It does not necessarily equal the set of addresses | 1190 | * the association. It does not necessarily equal the set of addresses |
1191 | * the peer uses for the resulting association. If the caller wants to | 1191 | * the peer uses for the resulting association. If the caller wants to |
1192 | * find out the set of peer addresses, it must use sctp_getpaddrs() to | 1192 | * find out the set of peer addresses, it must use sctp_getpaddrs() to |
1193 | * retrieve them after the association has been set up. | 1193 | * retrieve them after the association has been set up. |
1194 | * | 1194 | * |
1195 | * Basically do nothing but copying the addresses from user to kernel | 1195 | * Basically do nothing but copying the addresses from user to kernel |
1196 | * land and invoking either sctp_connectx(). This is used for tunneling | 1196 | * land and invoking either sctp_connectx(). This is used for tunneling |
1197 | * the sctp_connectx() request through sctp_setsockopt() from userspace. | 1197 | * the sctp_connectx() request through sctp_setsockopt() from userspace. |
1198 | * | 1198 | * |
1199 | * We don't use copy_from_user() for optimization: we first do the | 1199 | * We don't use copy_from_user() for optimization: we first do the |
1200 | * sanity checks (buffer size -fast- and access check-healthy | 1200 | * sanity checks (buffer size -fast- and access check-healthy |
1201 | * pointer); if all of those succeed, then we can alloc the memory | 1201 | * pointer); if all of those succeed, then we can alloc the memory |
1202 | * (expensive operation) needed to copy the data to kernel. Then we do | 1202 | * (expensive operation) needed to copy the data to kernel. Then we do |
1203 | * the copying without checking the user space area | 1203 | * the copying without checking the user space area |
1204 | * (__copy_from_user()). | 1204 | * (__copy_from_user()). |
1205 | * | 1205 | * |
1206 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does | 1206 | * On exit there is no need to do sockfd_put(), sys_setsockopt() does |
1207 | * it. | 1207 | * it. |
1208 | * | 1208 | * |
1209 | * sk The sk of the socket | 1209 | * sk The sk of the socket |
1210 | * addrs The pointer to the addresses in user land | 1210 | * addrs The pointer to the addresses in user land |
1211 | * addrssize Size of the addrs buffer | 1211 | * addrssize Size of the addrs buffer |
1212 | * | 1212 | * |
1213 | * Returns >=0 if ok, <0 errno code on error. | 1213 | * Returns >=0 if ok, <0 errno code on error. |
1214 | */ | 1214 | */ |
1215 | SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk, | 1215 | SCTP_STATIC int __sctp_setsockopt_connectx(struct sock* sk, |
1216 | struct sockaddr __user *addrs, | 1216 | struct sockaddr __user *addrs, |
1217 | int addrs_size, | 1217 | int addrs_size, |
1218 | sctp_assoc_t *assoc_id) | 1218 | sctp_assoc_t *assoc_id) |
1219 | { | 1219 | { |
1220 | int err = 0; | 1220 | int err = 0; |
1221 | struct sockaddr *kaddrs; | 1221 | struct sockaddr *kaddrs; |
1222 | 1222 | ||
1223 | SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n", | 1223 | SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n", |
1224 | __func__, sk, addrs, addrs_size); | 1224 | __func__, sk, addrs, addrs_size); |
1225 | 1225 | ||
1226 | if (unlikely(addrs_size <= 0)) | 1226 | if (unlikely(addrs_size <= 0)) |
1227 | return -EINVAL; | 1227 | return -EINVAL; |
1228 | 1228 | ||
1229 | /* Check the user passed a healthy pointer. */ | 1229 | /* Check the user passed a healthy pointer. */ |
1230 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) | 1230 | if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size))) |
1231 | return -EFAULT; | 1231 | return -EFAULT; |
1232 | 1232 | ||
1233 | /* Alloc space for the address array in kernel memory. */ | 1233 | /* Alloc space for the address array in kernel memory. */ |
1234 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); | 1234 | kaddrs = kmalloc(addrs_size, GFP_KERNEL); |
1235 | if (unlikely(!kaddrs)) | 1235 | if (unlikely(!kaddrs)) |
1236 | return -ENOMEM; | 1236 | return -ENOMEM; |
1237 | 1237 | ||
1238 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { | 1238 | if (__copy_from_user(kaddrs, addrs, addrs_size)) { |
1239 | err = -EFAULT; | 1239 | err = -EFAULT; |
1240 | } else { | 1240 | } else { |
1241 | err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id); | 1241 | err = __sctp_connect(sk, kaddrs, addrs_size, assoc_id); |
1242 | } | 1242 | } |
1243 | 1243 | ||
1244 | kfree(kaddrs); | 1244 | kfree(kaddrs); |
1245 | 1245 | ||
1246 | return err; | 1246 | return err; |
1247 | } | 1247 | } |
1248 | 1248 | ||
1249 | /* | 1249 | /* |
1250 | * This is an older interface. It's kept for backward compatibility | 1250 | * This is an older interface. It's kept for backward compatibility |
1251 | * to the option that doesn't provide association id. | 1251 | * to the option that doesn't provide association id. |
1252 | */ | 1252 | */ |
1253 | SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk, | 1253 | SCTP_STATIC int sctp_setsockopt_connectx_old(struct sock* sk, |
1254 | struct sockaddr __user *addrs, | 1254 | struct sockaddr __user *addrs, |
1255 | int addrs_size) | 1255 | int addrs_size) |
1256 | { | 1256 | { |
1257 | return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); | 1257 | return __sctp_setsockopt_connectx(sk, addrs, addrs_size, NULL); |
1258 | } | 1258 | } |
1259 | 1259 | ||
1260 | /* | 1260 | /* |
1261 | * New interface for the API. The since the API is done with a socket | 1261 | * New interface for the API. The since the API is done with a socket |
1262 | * option, to make it simple we feed back the association id is as a return | 1262 | * option, to make it simple we feed back the association id is as a return |
1263 | * indication to the call. Error is always negative and association id is | 1263 | * indication to the call. Error is always negative and association id is |
1264 | * always positive. | 1264 | * always positive. |
1265 | */ | 1265 | */ |
1266 | SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk, | 1266 | SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk, |
1267 | struct sockaddr __user *addrs, | 1267 | struct sockaddr __user *addrs, |
1268 | int addrs_size) | 1268 | int addrs_size) |
1269 | { | 1269 | { |
1270 | sctp_assoc_t assoc_id = 0; | 1270 | sctp_assoc_t assoc_id = 0; |
1271 | int err = 0; | 1271 | int err = 0; |
1272 | 1272 | ||
1273 | err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); | 1273 | err = __sctp_setsockopt_connectx(sk, addrs, addrs_size, &assoc_id); |
1274 | 1274 | ||
1275 | if (err) | 1275 | if (err) |
1276 | return err; | 1276 | return err; |
1277 | else | 1277 | else |
1278 | return assoc_id; | 1278 | return assoc_id; |
1279 | } | 1279 | } |
1280 | 1280 | ||
1281 | /* | 1281 | /* |
1282 | * New (hopefully final) interface for the API. | 1282 | * New (hopefully final) interface for the API. |
1283 | * We use the sctp_getaddrs_old structure so that use-space library | 1283 | * We use the sctp_getaddrs_old structure so that use-space library |
1284 | * can avoid any unnecessary allocations. The only defferent part | 1284 | * can avoid any unnecessary allocations. The only defferent part |
1285 | * is that we store the actual length of the address buffer into the | 1285 | * is that we store the actual length of the address buffer into the |
1286 | * addrs_num structure member. That way we can re-use the existing | 1286 | * addrs_num structure member. That way we can re-use the existing |
1287 | * code. | 1287 | * code. |
1288 | */ | 1288 | */ |
1289 | SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len, | 1289 | SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len, |
1290 | char __user *optval, | 1290 | char __user *optval, |
1291 | int __user *optlen) | 1291 | int __user *optlen) |
1292 | { | 1292 | { |
1293 | struct sctp_getaddrs_old param; | 1293 | struct sctp_getaddrs_old param; |
1294 | sctp_assoc_t assoc_id = 0; | 1294 | sctp_assoc_t assoc_id = 0; |
1295 | int err = 0; | 1295 | int err = 0; |
1296 | 1296 | ||
1297 | if (len < sizeof(param)) | 1297 | if (len < sizeof(param)) |
1298 | return -EINVAL; | 1298 | return -EINVAL; |
1299 | 1299 | ||
1300 | if (copy_from_user(¶m, optval, sizeof(param))) | 1300 | if (copy_from_user(¶m, optval, sizeof(param))) |
1301 | return -EFAULT; | 1301 | return -EFAULT; |
1302 | 1302 | ||
1303 | err = __sctp_setsockopt_connectx(sk, | 1303 | err = __sctp_setsockopt_connectx(sk, |
1304 | (struct sockaddr __user *)param.addrs, | 1304 | (struct sockaddr __user *)param.addrs, |
1305 | param.addr_num, &assoc_id); | 1305 | param.addr_num, &assoc_id); |
1306 | 1306 | ||
1307 | if (err == 0 || err == -EINPROGRESS) { | 1307 | if (err == 0 || err == -EINPROGRESS) { |
1308 | if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) | 1308 | if (copy_to_user(optval, &assoc_id, sizeof(assoc_id))) |
1309 | return -EFAULT; | 1309 | return -EFAULT; |
1310 | if (put_user(sizeof(assoc_id), optlen)) | 1310 | if (put_user(sizeof(assoc_id), optlen)) |
1311 | return -EFAULT; | 1311 | return -EFAULT; |
1312 | } | 1312 | } |
1313 | 1313 | ||
1314 | return err; | 1314 | return err; |
1315 | } | 1315 | } |
1316 | 1316 | ||
1317 | /* API 3.1.4 close() - UDP Style Syntax | 1317 | /* API 3.1.4 close() - UDP Style Syntax |
1318 | * Applications use close() to perform graceful shutdown (as described in | 1318 | * Applications use close() to perform graceful shutdown (as described in |
1319 | * Section 10.1 of [SCTP]) on ALL the associations currently represented | 1319 | * Section 10.1 of [SCTP]) on ALL the associations currently represented |
1320 | * by a UDP-style socket. | 1320 | * by a UDP-style socket. |
1321 | * | 1321 | * |
1322 | * The syntax is | 1322 | * The syntax is |
1323 | * | 1323 | * |
1324 | * ret = close(int sd); | 1324 | * ret = close(int sd); |
1325 | * | 1325 | * |
1326 | * sd - the socket descriptor of the associations to be closed. | 1326 | * sd - the socket descriptor of the associations to be closed. |
1327 | * | 1327 | * |
1328 | * To gracefully shutdown a specific association represented by the | 1328 | * To gracefully shutdown a specific association represented by the |
1329 | * UDP-style socket, an application should use the sendmsg() call, | 1329 | * UDP-style socket, an application should use the sendmsg() call, |
1330 | * passing no user data, but including the appropriate flag in the | 1330 | * passing no user data, but including the appropriate flag in the |
1331 | * ancillary data (see Section xxxx). | 1331 | * ancillary data (see Section xxxx). |
1332 | * | 1332 | * |
1333 | * If sd in the close() call is a branched-off socket representing only | 1333 | * If sd in the close() call is a branched-off socket representing only |
1334 | * one association, the shutdown is performed on that association only. | 1334 | * one association, the shutdown is performed on that association only. |
1335 | * | 1335 | * |
1336 | * 4.1.6 close() - TCP Style Syntax | 1336 | * 4.1.6 close() - TCP Style Syntax |
1337 | * | 1337 | * |
1338 | * Applications use close() to gracefully close down an association. | 1338 | * Applications use close() to gracefully close down an association. |
1339 | * | 1339 | * |
1340 | * The syntax is: | 1340 | * The syntax is: |
1341 | * | 1341 | * |
1342 | * int close(int sd); | 1342 | * int close(int sd); |
1343 | * | 1343 | * |
1344 | * sd - the socket descriptor of the association to be closed. | 1344 | * sd - the socket descriptor of the association to be closed. |
1345 | * | 1345 | * |
1346 | * After an application calls close() on a socket descriptor, no further | 1346 | * After an application calls close() on a socket descriptor, no further |
1347 | * socket operations will succeed on that descriptor. | 1347 | * socket operations will succeed on that descriptor. |
1348 | * | 1348 | * |
1349 | * API 7.1.4 SO_LINGER | 1349 | * API 7.1.4 SO_LINGER |
1350 | * | 1350 | * |
1351 | * An application using the TCP-style socket can use this option to | 1351 | * An application using the TCP-style socket can use this option to |
1352 | * perform the SCTP ABORT primitive. The linger option structure is: | 1352 | * perform the SCTP ABORT primitive. The linger option structure is: |
1353 | * | 1353 | * |
1354 | * struct linger { | 1354 | * struct linger { |
1355 | * int l_onoff; // option on/off | 1355 | * int l_onoff; // option on/off |
1356 | * int l_linger; // linger time | 1356 | * int l_linger; // linger time |
1357 | * }; | 1357 | * }; |
1358 | * | 1358 | * |
1359 | * To enable the option, set l_onoff to 1. If the l_linger value is set | 1359 | * To enable the option, set l_onoff to 1. If the l_linger value is set |
1360 | * to 0, calling close() is the same as the ABORT primitive. If the | 1360 | * to 0, calling close() is the same as the ABORT primitive. If the |
1361 | * value is set to a negative value, the setsockopt() call will return | 1361 | * value is set to a negative value, the setsockopt() call will return |
1362 | * an error. If the value is set to a positive value linger_time, the | 1362 | * an error. If the value is set to a positive value linger_time, the |
1363 | * close() can be blocked for at most linger_time ms. If the graceful | 1363 | * close() can be blocked for at most linger_time ms. If the graceful |
1364 | * shutdown phase does not finish during this period, close() will | 1364 | * shutdown phase does not finish during this period, close() will |
1365 | * return but the graceful shutdown phase continues in the system. | 1365 | * return but the graceful shutdown phase continues in the system. |
1366 | */ | 1366 | */ |
1367 | SCTP_STATIC void sctp_close(struct sock *sk, long timeout) | 1367 | SCTP_STATIC void sctp_close(struct sock *sk, long timeout) |
1368 | { | 1368 | { |
1369 | struct sctp_endpoint *ep; | 1369 | struct sctp_endpoint *ep; |
1370 | struct sctp_association *asoc; | 1370 | struct sctp_association *asoc; |
1371 | struct list_head *pos, *temp; | 1371 | struct list_head *pos, *temp; |
1372 | 1372 | ||
1373 | SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout); | 1373 | SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout); |
1374 | 1374 | ||
1375 | sctp_lock_sock(sk); | 1375 | sctp_lock_sock(sk); |
1376 | sk->sk_shutdown = SHUTDOWN_MASK; | 1376 | sk->sk_shutdown = SHUTDOWN_MASK; |
1377 | sk->sk_state = SCTP_SS_CLOSING; | 1377 | sk->sk_state = SCTP_SS_CLOSING; |
1378 | 1378 | ||
1379 | ep = sctp_sk(sk)->ep; | 1379 | ep = sctp_sk(sk)->ep; |
1380 | 1380 | ||
1381 | /* Walk all associations on an endpoint. */ | 1381 | /* Walk all associations on an endpoint. */ |
1382 | list_for_each_safe(pos, temp, &ep->asocs) { | 1382 | list_for_each_safe(pos, temp, &ep->asocs) { |
1383 | asoc = list_entry(pos, struct sctp_association, asocs); | 1383 | asoc = list_entry(pos, struct sctp_association, asocs); |
1384 | 1384 | ||
1385 | if (sctp_style(sk, TCP)) { | 1385 | if (sctp_style(sk, TCP)) { |
1386 | /* A closed association can still be in the list if | 1386 | /* A closed association can still be in the list if |
1387 | * it belongs to a TCP-style listening socket that is | 1387 | * it belongs to a TCP-style listening socket that is |
1388 | * not yet accepted. If so, free it. If not, send an | 1388 | * not yet accepted. If so, free it. If not, send an |
1389 | * ABORT or SHUTDOWN based on the linger options. | 1389 | * ABORT or SHUTDOWN based on the linger options. |
1390 | */ | 1390 | */ |
1391 | if (sctp_state(asoc, CLOSED)) { | 1391 | if (sctp_state(asoc, CLOSED)) { |
1392 | sctp_unhash_established(asoc); | 1392 | sctp_unhash_established(asoc); |
1393 | sctp_association_free(asoc); | 1393 | sctp_association_free(asoc); |
1394 | continue; | 1394 | continue; |
1395 | } | 1395 | } |
1396 | } | 1396 | } |
1397 | 1397 | ||
1398 | if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { | 1398 | if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { |
1399 | struct sctp_chunk *chunk; | 1399 | struct sctp_chunk *chunk; |
1400 | 1400 | ||
1401 | chunk = sctp_make_abort_user(asoc, NULL, 0); | 1401 | chunk = sctp_make_abort_user(asoc, NULL, 0); |
1402 | if (chunk) | 1402 | if (chunk) |
1403 | sctp_primitive_ABORT(asoc, chunk); | 1403 | sctp_primitive_ABORT(asoc, chunk); |
1404 | } else | 1404 | } else |
1405 | sctp_primitive_SHUTDOWN(asoc, NULL); | 1405 | sctp_primitive_SHUTDOWN(asoc, NULL); |
1406 | } | 1406 | } |
1407 | 1407 | ||
1408 | /* Clean up any skbs sitting on the receive queue. */ | 1408 | /* Clean up any skbs sitting on the receive queue. */ |
1409 | sctp_queue_purge_ulpevents(&sk->sk_receive_queue); | 1409 | sctp_queue_purge_ulpevents(&sk->sk_receive_queue); |
1410 | sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); | 1410 | sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby); |
1411 | 1411 | ||
1412 | /* On a TCP-style socket, block for at most linger_time if set. */ | 1412 | /* On a TCP-style socket, block for at most linger_time if set. */ |
1413 | if (sctp_style(sk, TCP) && timeout) | 1413 | if (sctp_style(sk, TCP) && timeout) |
1414 | sctp_wait_for_close(sk, timeout); | 1414 | sctp_wait_for_close(sk, timeout); |
1415 | 1415 | ||
1416 | /* This will run the backlog queue. */ | 1416 | /* This will run the backlog queue. */ |
1417 | sctp_release_sock(sk); | 1417 | sctp_release_sock(sk); |
1418 | 1418 | ||
1419 | /* Supposedly, no process has access to the socket, but | 1419 | /* Supposedly, no process has access to the socket, but |
1420 | * the net layers still may. | 1420 | * the net layers still may. |
1421 | */ | 1421 | */ |
1422 | sctp_local_bh_disable(); | 1422 | sctp_local_bh_disable(); |
1423 | sctp_bh_lock_sock(sk); | 1423 | sctp_bh_lock_sock(sk); |
1424 | 1424 | ||
1425 | /* Hold the sock, since sk_common_release() will put sock_put() | 1425 | /* Hold the sock, since sk_common_release() will put sock_put() |
1426 | * and we have just a little more cleanup. | 1426 | * and we have just a little more cleanup. |
1427 | */ | 1427 | */ |
1428 | sock_hold(sk); | 1428 | sock_hold(sk); |
1429 | sk_common_release(sk); | 1429 | sk_common_release(sk); |
1430 | 1430 | ||
1431 | sctp_bh_unlock_sock(sk); | 1431 | sctp_bh_unlock_sock(sk); |
1432 | sctp_local_bh_enable(); | 1432 | sctp_local_bh_enable(); |
1433 | 1433 | ||
1434 | sock_put(sk); | 1434 | sock_put(sk); |
1435 | 1435 | ||
1436 | SCTP_DBG_OBJCNT_DEC(sock); | 1436 | SCTP_DBG_OBJCNT_DEC(sock); |
1437 | } | 1437 | } |
1438 | 1438 | ||
1439 | /* Handle EPIPE error. */ | 1439 | /* Handle EPIPE error. */ |
1440 | static int sctp_error(struct sock *sk, int flags, int err) | 1440 | static int sctp_error(struct sock *sk, int flags, int err) |
1441 | { | 1441 | { |
1442 | if (err == -EPIPE) | 1442 | if (err == -EPIPE) |
1443 | err = sock_error(sk) ? : -EPIPE; | 1443 | err = sock_error(sk) ? : -EPIPE; |
1444 | if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) | 1444 | if (err == -EPIPE && !(flags & MSG_NOSIGNAL)) |
1445 | send_sig(SIGPIPE, current, 0); | 1445 | send_sig(SIGPIPE, current, 0); |
1446 | return err; | 1446 | return err; |
1447 | } | 1447 | } |
1448 | 1448 | ||
1449 | /* API 3.1.3 sendmsg() - UDP Style Syntax | 1449 | /* API 3.1.3 sendmsg() - UDP Style Syntax |
1450 | * | 1450 | * |
1451 | * An application uses sendmsg() and recvmsg() calls to transmit data to | 1451 | * An application uses sendmsg() and recvmsg() calls to transmit data to |
1452 | * and receive data from its peer. | 1452 | * and receive data from its peer. |
1453 | * | 1453 | * |
1454 | * ssize_t sendmsg(int socket, const struct msghdr *message, | 1454 | * ssize_t sendmsg(int socket, const struct msghdr *message, |
1455 | * int flags); | 1455 | * int flags); |
1456 | * | 1456 | * |
1457 | * socket - the socket descriptor of the endpoint. | 1457 | * socket - the socket descriptor of the endpoint. |
1458 | * message - pointer to the msghdr structure which contains a single | 1458 | * message - pointer to the msghdr structure which contains a single |
1459 | * user message and possibly some ancillary data. | 1459 | * user message and possibly some ancillary data. |
1460 | * | 1460 | * |
1461 | * See Section 5 for complete description of the data | 1461 | * See Section 5 for complete description of the data |
1462 | * structures. | 1462 | * structures. |
1463 | * | 1463 | * |
1464 | * flags - flags sent or received with the user message, see Section | 1464 | * flags - flags sent or received with the user message, see Section |
1465 | * 5 for complete description of the flags. | 1465 | * 5 for complete description of the flags. |
1466 | * | 1466 | * |
1467 | * Note: This function could use a rewrite especially when explicit | 1467 | * Note: This function could use a rewrite especially when explicit |
1468 | * connect support comes in. | 1468 | * connect support comes in. |
1469 | */ | 1469 | */ |
1470 | /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ | 1470 | /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */ |
1471 | 1471 | ||
1472 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); | 1472 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); |
1473 | 1473 | ||
1474 | SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, | 1474 | SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, |
1475 | struct msghdr *msg, size_t msg_len) | 1475 | struct msghdr *msg, size_t msg_len) |
1476 | { | 1476 | { |
1477 | struct sctp_sock *sp; | 1477 | struct sctp_sock *sp; |
1478 | struct sctp_endpoint *ep; | 1478 | struct sctp_endpoint *ep; |
1479 | struct sctp_association *new_asoc=NULL, *asoc=NULL; | 1479 | struct sctp_association *new_asoc=NULL, *asoc=NULL; |
1480 | struct sctp_transport *transport, *chunk_tp; | 1480 | struct sctp_transport *transport, *chunk_tp; |
1481 | struct sctp_chunk *chunk; | 1481 | struct sctp_chunk *chunk; |
1482 | union sctp_addr to; | 1482 | union sctp_addr to; |
1483 | struct sockaddr *msg_name = NULL; | 1483 | struct sockaddr *msg_name = NULL; |
1484 | struct sctp_sndrcvinfo default_sinfo = { 0 }; | 1484 | struct sctp_sndrcvinfo default_sinfo = { 0 }; |
1485 | struct sctp_sndrcvinfo *sinfo; | 1485 | struct sctp_sndrcvinfo *sinfo; |
1486 | struct sctp_initmsg *sinit; | 1486 | struct sctp_initmsg *sinit; |
1487 | sctp_assoc_t associd = 0; | 1487 | sctp_assoc_t associd = 0; |
1488 | sctp_cmsgs_t cmsgs = { NULL }; | 1488 | sctp_cmsgs_t cmsgs = { NULL }; |
1489 | int err; | 1489 | int err; |
1490 | sctp_scope_t scope; | 1490 | sctp_scope_t scope; |
1491 | long timeo; | 1491 | long timeo; |
1492 | __u16 sinfo_flags = 0; | 1492 | __u16 sinfo_flags = 0; |
1493 | struct sctp_datamsg *datamsg; | 1493 | struct sctp_datamsg *datamsg; |
1494 | int msg_flags = msg->msg_flags; | 1494 | int msg_flags = msg->msg_flags; |
1495 | 1495 | ||
1496 | SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n", | 1496 | SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n", |
1497 | sk, msg, msg_len); | 1497 | sk, msg, msg_len); |
1498 | 1498 | ||
1499 | err = 0; | 1499 | err = 0; |
1500 | sp = sctp_sk(sk); | 1500 | sp = sctp_sk(sk); |
1501 | ep = sp->ep; | 1501 | ep = sp->ep; |
1502 | 1502 | ||
1503 | SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep); | 1503 | SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep); |
1504 | 1504 | ||
1505 | /* We cannot send a message over a TCP-style listening socket. */ | 1505 | /* We cannot send a message over a TCP-style listening socket. */ |
1506 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { | 1506 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) { |
1507 | err = -EPIPE; | 1507 | err = -EPIPE; |
1508 | goto out_nounlock; | 1508 | goto out_nounlock; |
1509 | } | 1509 | } |
1510 | 1510 | ||
1511 | /* Parse out the SCTP CMSGs. */ | 1511 | /* Parse out the SCTP CMSGs. */ |
1512 | err = sctp_msghdr_parse(msg, &cmsgs); | 1512 | err = sctp_msghdr_parse(msg, &cmsgs); |
1513 | 1513 | ||
1514 | if (err) { | 1514 | if (err) { |
1515 | SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err); | 1515 | SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err); |
1516 | goto out_nounlock; | 1516 | goto out_nounlock; |
1517 | } | 1517 | } |
1518 | 1518 | ||
1519 | /* Fetch the destination address for this packet. This | 1519 | /* Fetch the destination address for this packet. This |
1520 | * address only selects the association--it is not necessarily | 1520 | * address only selects the association--it is not necessarily |
1521 | * the address we will send to. | 1521 | * the address we will send to. |
1522 | * For a peeled-off socket, msg_name is ignored. | 1522 | * For a peeled-off socket, msg_name is ignored. |
1523 | */ | 1523 | */ |
1524 | if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { | 1524 | if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) { |
1525 | int msg_namelen = msg->msg_namelen; | 1525 | int msg_namelen = msg->msg_namelen; |
1526 | 1526 | ||
1527 | err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, | 1527 | err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name, |
1528 | msg_namelen); | 1528 | msg_namelen); |
1529 | if (err) | 1529 | if (err) |
1530 | return err; | 1530 | return err; |
1531 | 1531 | ||
1532 | if (msg_namelen > sizeof(to)) | 1532 | if (msg_namelen > sizeof(to)) |
1533 | msg_namelen = sizeof(to); | 1533 | msg_namelen = sizeof(to); |
1534 | memcpy(&to, msg->msg_name, msg_namelen); | 1534 | memcpy(&to, msg->msg_name, msg_namelen); |
1535 | msg_name = msg->msg_name; | 1535 | msg_name = msg->msg_name; |
1536 | } | 1536 | } |
1537 | 1537 | ||
1538 | sinfo = cmsgs.info; | 1538 | sinfo = cmsgs.info; |
1539 | sinit = cmsgs.init; | 1539 | sinit = cmsgs.init; |
1540 | 1540 | ||
1541 | /* Did the user specify SNDRCVINFO? */ | 1541 | /* Did the user specify SNDRCVINFO? */ |
1542 | if (sinfo) { | 1542 | if (sinfo) { |
1543 | sinfo_flags = sinfo->sinfo_flags; | 1543 | sinfo_flags = sinfo->sinfo_flags; |
1544 | associd = sinfo->sinfo_assoc_id; | 1544 | associd = sinfo->sinfo_assoc_id; |
1545 | } | 1545 | } |
1546 | 1546 | ||
1547 | SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n", | 1547 | SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n", |
1548 | msg_len, sinfo_flags); | 1548 | msg_len, sinfo_flags); |
1549 | 1549 | ||
1550 | /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ | 1550 | /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */ |
1551 | if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { | 1551 | if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) { |
1552 | err = -EINVAL; | 1552 | err = -EINVAL; |
1553 | goto out_nounlock; | 1553 | goto out_nounlock; |
1554 | } | 1554 | } |
1555 | 1555 | ||
1556 | /* If SCTP_EOF is set, no data can be sent. Disallow sending zero | 1556 | /* If SCTP_EOF is set, no data can be sent. Disallow sending zero |
1557 | * length messages when SCTP_EOF|SCTP_ABORT is not set. | 1557 | * length messages when SCTP_EOF|SCTP_ABORT is not set. |
1558 | * If SCTP_ABORT is set, the message length could be non zero with | 1558 | * If SCTP_ABORT is set, the message length could be non zero with |
1559 | * the msg_iov set to the user abort reason. | 1559 | * the msg_iov set to the user abort reason. |
1560 | */ | 1560 | */ |
1561 | if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || | 1561 | if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) || |
1562 | (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { | 1562 | (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) { |
1563 | err = -EINVAL; | 1563 | err = -EINVAL; |
1564 | goto out_nounlock; | 1564 | goto out_nounlock; |
1565 | } | 1565 | } |
1566 | 1566 | ||
1567 | /* If SCTP_ADDR_OVER is set, there must be an address | 1567 | /* If SCTP_ADDR_OVER is set, there must be an address |
1568 | * specified in msg_name. | 1568 | * specified in msg_name. |
1569 | */ | 1569 | */ |
1570 | if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { | 1570 | if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) { |
1571 | err = -EINVAL; | 1571 | err = -EINVAL; |
1572 | goto out_nounlock; | 1572 | goto out_nounlock; |
1573 | } | 1573 | } |
1574 | 1574 | ||
1575 | transport = NULL; | 1575 | transport = NULL; |
1576 | 1576 | ||
1577 | SCTP_DEBUG_PRINTK("About to look up association.\n"); | 1577 | SCTP_DEBUG_PRINTK("About to look up association.\n"); |
1578 | 1578 | ||
1579 | sctp_lock_sock(sk); | 1579 | sctp_lock_sock(sk); |
1580 | 1580 | ||
1581 | /* If a msg_name has been specified, assume this is to be used. */ | 1581 | /* If a msg_name has been specified, assume this is to be used. */ |
1582 | if (msg_name) { | 1582 | if (msg_name) { |
1583 | /* Look for a matching association on the endpoint. */ | 1583 | /* Look for a matching association on the endpoint. */ |
1584 | asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); | 1584 | asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport); |
1585 | if (!asoc) { | 1585 | if (!asoc) { |
1586 | /* If we could not find a matching association on the | 1586 | /* If we could not find a matching association on the |
1587 | * endpoint, make sure that it is not a TCP-style | 1587 | * endpoint, make sure that it is not a TCP-style |
1588 | * socket that already has an association or there is | 1588 | * socket that already has an association or there is |
1589 | * no peeled-off association on another socket. | 1589 | * no peeled-off association on another socket. |
1590 | */ | 1590 | */ |
1591 | if ((sctp_style(sk, TCP) && | 1591 | if ((sctp_style(sk, TCP) && |
1592 | sctp_sstate(sk, ESTABLISHED)) || | 1592 | sctp_sstate(sk, ESTABLISHED)) || |
1593 | sctp_endpoint_is_peeled_off(ep, &to)) { | 1593 | sctp_endpoint_is_peeled_off(ep, &to)) { |
1594 | err = -EADDRNOTAVAIL; | 1594 | err = -EADDRNOTAVAIL; |
1595 | goto out_unlock; | 1595 | goto out_unlock; |
1596 | } | 1596 | } |
1597 | } | 1597 | } |
1598 | } else { | 1598 | } else { |
1599 | asoc = sctp_id2assoc(sk, associd); | 1599 | asoc = sctp_id2assoc(sk, associd); |
1600 | if (!asoc) { | 1600 | if (!asoc) { |
1601 | err = -EPIPE; | 1601 | err = -EPIPE; |
1602 | goto out_unlock; | 1602 | goto out_unlock; |
1603 | } | 1603 | } |
1604 | } | 1604 | } |
1605 | 1605 | ||
1606 | if (asoc) { | 1606 | if (asoc) { |
1607 | SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc); | 1607 | SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc); |
1608 | 1608 | ||
1609 | /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED | 1609 | /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED |
1610 | * socket that has an association in CLOSED state. This can | 1610 | * socket that has an association in CLOSED state. This can |
1611 | * happen when an accepted socket has an association that is | 1611 | * happen when an accepted socket has an association that is |
1612 | * already CLOSED. | 1612 | * already CLOSED. |
1613 | */ | 1613 | */ |
1614 | if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { | 1614 | if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) { |
1615 | err = -EPIPE; | 1615 | err = -EPIPE; |
1616 | goto out_unlock; | 1616 | goto out_unlock; |
1617 | } | 1617 | } |
1618 | 1618 | ||
1619 | if (sinfo_flags & SCTP_EOF) { | 1619 | if (sinfo_flags & SCTP_EOF) { |
1620 | SCTP_DEBUG_PRINTK("Shutting down association: %p\n", | 1620 | SCTP_DEBUG_PRINTK("Shutting down association: %p\n", |
1621 | asoc); | 1621 | asoc); |
1622 | sctp_primitive_SHUTDOWN(asoc, NULL); | 1622 | sctp_primitive_SHUTDOWN(asoc, NULL); |
1623 | err = 0; | 1623 | err = 0; |
1624 | goto out_unlock; | 1624 | goto out_unlock; |
1625 | } | 1625 | } |
1626 | if (sinfo_flags & SCTP_ABORT) { | 1626 | if (sinfo_flags & SCTP_ABORT) { |
1627 | 1627 | ||
1628 | chunk = sctp_make_abort_user(asoc, msg, msg_len); | 1628 | chunk = sctp_make_abort_user(asoc, msg, msg_len); |
1629 | if (!chunk) { | 1629 | if (!chunk) { |
1630 | err = -ENOMEM; | 1630 | err = -ENOMEM; |
1631 | goto out_unlock; | 1631 | goto out_unlock; |
1632 | } | 1632 | } |
1633 | 1633 | ||
1634 | SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc); | 1634 | SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc); |
1635 | sctp_primitive_ABORT(asoc, chunk); | 1635 | sctp_primitive_ABORT(asoc, chunk); |
1636 | err = 0; | 1636 | err = 0; |
1637 | goto out_unlock; | 1637 | goto out_unlock; |
1638 | } | 1638 | } |
1639 | } | 1639 | } |
1640 | 1640 | ||
1641 | /* Do we need to create the association? */ | 1641 | /* Do we need to create the association? */ |
1642 | if (!asoc) { | 1642 | if (!asoc) { |
1643 | SCTP_DEBUG_PRINTK("There is no association yet.\n"); | 1643 | SCTP_DEBUG_PRINTK("There is no association yet.\n"); |
1644 | 1644 | ||
1645 | if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { | 1645 | if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) { |
1646 | err = -EINVAL; | 1646 | err = -EINVAL; |
1647 | goto out_unlock; | 1647 | goto out_unlock; |
1648 | } | 1648 | } |
1649 | 1649 | ||
1650 | /* Check for invalid stream against the stream counts, | 1650 | /* Check for invalid stream against the stream counts, |
1651 | * either the default or the user specified stream counts. | 1651 | * either the default or the user specified stream counts. |
1652 | */ | 1652 | */ |
1653 | if (sinfo) { | 1653 | if (sinfo) { |
1654 | if (!sinit || (sinit && !sinit->sinit_num_ostreams)) { | 1654 | if (!sinit || (sinit && !sinit->sinit_num_ostreams)) { |
1655 | /* Check against the defaults. */ | 1655 | /* Check against the defaults. */ |
1656 | if (sinfo->sinfo_stream >= | 1656 | if (sinfo->sinfo_stream >= |
1657 | sp->initmsg.sinit_num_ostreams) { | 1657 | sp->initmsg.sinit_num_ostreams) { |
1658 | err = -EINVAL; | 1658 | err = -EINVAL; |
1659 | goto out_unlock; | 1659 | goto out_unlock; |
1660 | } | 1660 | } |
1661 | } else { | 1661 | } else { |
1662 | /* Check against the requested. */ | 1662 | /* Check against the requested. */ |
1663 | if (sinfo->sinfo_stream >= | 1663 | if (sinfo->sinfo_stream >= |
1664 | sinit->sinit_num_ostreams) { | 1664 | sinit->sinit_num_ostreams) { |
1665 | err = -EINVAL; | 1665 | err = -EINVAL; |
1666 | goto out_unlock; | 1666 | goto out_unlock; |
1667 | } | 1667 | } |
1668 | } | 1668 | } |
1669 | } | 1669 | } |
1670 | 1670 | ||
1671 | /* | 1671 | /* |
1672 | * API 3.1.2 bind() - UDP Style Syntax | 1672 | * API 3.1.2 bind() - UDP Style Syntax |
1673 | * If a bind() or sctp_bindx() is not called prior to a | 1673 | * If a bind() or sctp_bindx() is not called prior to a |
1674 | * sendmsg() call that initiates a new association, the | 1674 | * sendmsg() call that initiates a new association, the |
1675 | * system picks an ephemeral port and will choose an address | 1675 | * system picks an ephemeral port and will choose an address |
1676 | * set equivalent to binding with a wildcard address. | 1676 | * set equivalent to binding with a wildcard address. |
1677 | */ | 1677 | */ |
1678 | if (!ep->base.bind_addr.port) { | 1678 | if (!ep->base.bind_addr.port) { |
1679 | if (sctp_autobind(sk)) { | 1679 | if (sctp_autobind(sk)) { |
1680 | err = -EAGAIN; | 1680 | err = -EAGAIN; |
1681 | goto out_unlock; | 1681 | goto out_unlock; |
1682 | } | 1682 | } |
1683 | } else { | 1683 | } else { |
1684 | /* | 1684 | /* |
1685 | * If an unprivileged user inherits a one-to-many | 1685 | * If an unprivileged user inherits a one-to-many |
1686 | * style socket with open associations on a privileged | 1686 | * style socket with open associations on a privileged |
1687 | * port, it MAY be permitted to accept new associations, | 1687 | * port, it MAY be permitted to accept new associations, |
1688 | * but it SHOULD NOT be permitted to open new | 1688 | * but it SHOULD NOT be permitted to open new |
1689 | * associations. | 1689 | * associations. |
1690 | */ | 1690 | */ |
1691 | if (ep->base.bind_addr.port < PROT_SOCK && | 1691 | if (ep->base.bind_addr.port < PROT_SOCK && |
1692 | !capable(CAP_NET_BIND_SERVICE)) { | 1692 | !capable(CAP_NET_BIND_SERVICE)) { |
1693 | err = -EACCES; | 1693 | err = -EACCES; |
1694 | goto out_unlock; | 1694 | goto out_unlock; |
1695 | } | 1695 | } |
1696 | } | 1696 | } |
1697 | 1697 | ||
1698 | scope = sctp_scope(&to); | 1698 | scope = sctp_scope(&to); |
1699 | new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); | 1699 | new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL); |
1700 | if (!new_asoc) { | 1700 | if (!new_asoc) { |
1701 | err = -ENOMEM; | 1701 | err = -ENOMEM; |
1702 | goto out_unlock; | 1702 | goto out_unlock; |
1703 | } | 1703 | } |
1704 | asoc = new_asoc; | 1704 | asoc = new_asoc; |
1705 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); | 1705 | err = sctp_assoc_set_bind_addr_from_ep(asoc, scope, GFP_KERNEL); |
1706 | if (err < 0) { | 1706 | if (err < 0) { |
1707 | err = -ENOMEM; | 1707 | err = -ENOMEM; |
1708 | goto out_free; | 1708 | goto out_free; |
1709 | } | 1709 | } |
1710 | 1710 | ||
1711 | /* If the SCTP_INIT ancillary data is specified, set all | 1711 | /* If the SCTP_INIT ancillary data is specified, set all |
1712 | * the association init values accordingly. | 1712 | * the association init values accordingly. |
1713 | */ | 1713 | */ |
1714 | if (sinit) { | 1714 | if (sinit) { |
1715 | if (sinit->sinit_num_ostreams) { | 1715 | if (sinit->sinit_num_ostreams) { |
1716 | asoc->c.sinit_num_ostreams = | 1716 | asoc->c.sinit_num_ostreams = |
1717 | sinit->sinit_num_ostreams; | 1717 | sinit->sinit_num_ostreams; |
1718 | } | 1718 | } |
1719 | if (sinit->sinit_max_instreams) { | 1719 | if (sinit->sinit_max_instreams) { |
1720 | asoc->c.sinit_max_instreams = | 1720 | asoc->c.sinit_max_instreams = |
1721 | sinit->sinit_max_instreams; | 1721 | sinit->sinit_max_instreams; |
1722 | } | 1722 | } |
1723 | if (sinit->sinit_max_attempts) { | 1723 | if (sinit->sinit_max_attempts) { |
1724 | asoc->max_init_attempts | 1724 | asoc->max_init_attempts |
1725 | = sinit->sinit_max_attempts; | 1725 | = sinit->sinit_max_attempts; |
1726 | } | 1726 | } |
1727 | if (sinit->sinit_max_init_timeo) { | 1727 | if (sinit->sinit_max_init_timeo) { |
1728 | asoc->max_init_timeo = | 1728 | asoc->max_init_timeo = |
1729 | msecs_to_jiffies(sinit->sinit_max_init_timeo); | 1729 | msecs_to_jiffies(sinit->sinit_max_init_timeo); |
1730 | } | 1730 | } |
1731 | } | 1731 | } |
1732 | 1732 | ||
1733 | /* Prime the peer's transport structures. */ | 1733 | /* Prime the peer's transport structures. */ |
1734 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); | 1734 | transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN); |
1735 | if (!transport) { | 1735 | if (!transport) { |
1736 | err = -ENOMEM; | 1736 | err = -ENOMEM; |
1737 | goto out_free; | 1737 | goto out_free; |
1738 | } | 1738 | } |
1739 | } | 1739 | } |
1740 | 1740 | ||
1741 | /* ASSERT: we have a valid association at this point. */ | 1741 | /* ASSERT: we have a valid association at this point. */ |
1742 | SCTP_DEBUG_PRINTK("We have a valid association.\n"); | 1742 | SCTP_DEBUG_PRINTK("We have a valid association.\n"); |
1743 | 1743 | ||
1744 | if (!sinfo) { | 1744 | if (!sinfo) { |
1745 | /* If the user didn't specify SNDRCVINFO, make up one with | 1745 | /* If the user didn't specify SNDRCVINFO, make up one with |
1746 | * some defaults. | 1746 | * some defaults. |
1747 | */ | 1747 | */ |
1748 | default_sinfo.sinfo_stream = asoc->default_stream; | 1748 | default_sinfo.sinfo_stream = asoc->default_stream; |
1749 | default_sinfo.sinfo_flags = asoc->default_flags; | 1749 | default_sinfo.sinfo_flags = asoc->default_flags; |
1750 | default_sinfo.sinfo_ppid = asoc->default_ppid; | 1750 | default_sinfo.sinfo_ppid = asoc->default_ppid; |
1751 | default_sinfo.sinfo_context = asoc->default_context; | 1751 | default_sinfo.sinfo_context = asoc->default_context; |
1752 | default_sinfo.sinfo_timetolive = asoc->default_timetolive; | 1752 | default_sinfo.sinfo_timetolive = asoc->default_timetolive; |
1753 | default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); | 1753 | default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc); |
1754 | sinfo = &default_sinfo; | 1754 | sinfo = &default_sinfo; |
1755 | } | 1755 | } |
1756 | 1756 | ||
1757 | /* API 7.1.7, the sndbuf size per association bounds the | 1757 | /* API 7.1.7, the sndbuf size per association bounds the |
1758 | * maximum size of data that can be sent in a single send call. | 1758 | * maximum size of data that can be sent in a single send call. |
1759 | */ | 1759 | */ |
1760 | if (msg_len > sk->sk_sndbuf) { | 1760 | if (msg_len > sk->sk_sndbuf) { |
1761 | err = -EMSGSIZE; | 1761 | err = -EMSGSIZE; |
1762 | goto out_free; | 1762 | goto out_free; |
1763 | } | 1763 | } |
1764 | 1764 | ||
1765 | if (asoc->pmtu_pending) | 1765 | if (asoc->pmtu_pending) |
1766 | sctp_assoc_pending_pmtu(asoc); | 1766 | sctp_assoc_pending_pmtu(asoc); |
1767 | 1767 | ||
1768 | /* If fragmentation is disabled and the message length exceeds the | 1768 | /* If fragmentation is disabled and the message length exceeds the |
1769 | * association fragmentation point, return EMSGSIZE. The I-D | 1769 | * association fragmentation point, return EMSGSIZE. The I-D |
1770 | * does not specify what this error is, but this looks like | 1770 | * does not specify what this error is, but this looks like |
1771 | * a great fit. | 1771 | * a great fit. |
1772 | */ | 1772 | */ |
1773 | if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { | 1773 | if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) { |
1774 | err = -EMSGSIZE; | 1774 | err = -EMSGSIZE; |
1775 | goto out_free; | 1775 | goto out_free; |
1776 | } | 1776 | } |
1777 | 1777 | ||
1778 | if (sinfo) { | 1778 | if (sinfo) { |
1779 | /* Check for invalid stream. */ | 1779 | /* Check for invalid stream. */ |
1780 | if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) { | 1780 | if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) { |
1781 | err = -EINVAL; | 1781 | err = -EINVAL; |
1782 | goto out_free; | 1782 | goto out_free; |
1783 | } | 1783 | } |
1784 | } | 1784 | } |
1785 | 1785 | ||
1786 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | 1786 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
1787 | if (!sctp_wspace(asoc)) { | 1787 | if (!sctp_wspace(asoc)) { |
1788 | err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); | 1788 | err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len); |
1789 | if (err) | 1789 | if (err) |
1790 | goto out_free; | 1790 | goto out_free; |
1791 | } | 1791 | } |
1792 | 1792 | ||
1793 | /* If an address is passed with the sendto/sendmsg call, it is used | 1793 | /* If an address is passed with the sendto/sendmsg call, it is used |
1794 | * to override the primary destination address in the TCP model, or | 1794 | * to override the primary destination address in the TCP model, or |
1795 | * when SCTP_ADDR_OVER flag is set in the UDP model. | 1795 | * when SCTP_ADDR_OVER flag is set in the UDP model. |
1796 | */ | 1796 | */ |
1797 | if ((sctp_style(sk, TCP) && msg_name) || | 1797 | if ((sctp_style(sk, TCP) && msg_name) || |
1798 | (sinfo_flags & SCTP_ADDR_OVER)) { | 1798 | (sinfo_flags & SCTP_ADDR_OVER)) { |
1799 | chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); | 1799 | chunk_tp = sctp_assoc_lookup_paddr(asoc, &to); |
1800 | if (!chunk_tp) { | 1800 | if (!chunk_tp) { |
1801 | err = -EINVAL; | 1801 | err = -EINVAL; |
1802 | goto out_free; | 1802 | goto out_free; |
1803 | } | 1803 | } |
1804 | } else | 1804 | } else |
1805 | chunk_tp = NULL; | 1805 | chunk_tp = NULL; |
1806 | 1806 | ||
1807 | /* Auto-connect, if we aren't connected already. */ | 1807 | /* Auto-connect, if we aren't connected already. */ |
1808 | if (sctp_state(asoc, CLOSED)) { | 1808 | if (sctp_state(asoc, CLOSED)) { |
1809 | err = sctp_primitive_ASSOCIATE(asoc, NULL); | 1809 | err = sctp_primitive_ASSOCIATE(asoc, NULL); |
1810 | if (err < 0) | 1810 | if (err < 0) |
1811 | goto out_free; | 1811 | goto out_free; |
1812 | SCTP_DEBUG_PRINTK("We associated primitively.\n"); | 1812 | SCTP_DEBUG_PRINTK("We associated primitively.\n"); |
1813 | } | 1813 | } |
1814 | 1814 | ||
1815 | /* Break the message into multiple chunks of maximum size. */ | 1815 | /* Break the message into multiple chunks of maximum size. */ |
1816 | datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len); | 1816 | datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len); |
1817 | if (!datamsg) { | 1817 | if (!datamsg) { |
1818 | err = -ENOMEM; | 1818 | err = -ENOMEM; |
1819 | goto out_free; | 1819 | goto out_free; |
1820 | } | 1820 | } |
1821 | 1821 | ||
1822 | /* Now send the (possibly) fragmented message. */ | 1822 | /* Now send the (possibly) fragmented message. */ |
1823 | list_for_each_entry(chunk, &datamsg->chunks, frag_list) { | 1823 | list_for_each_entry(chunk, &datamsg->chunks, frag_list) { |
1824 | sctp_chunk_hold(chunk); | 1824 | sctp_chunk_hold(chunk); |
1825 | 1825 | ||
1826 | /* Do accounting for the write space. */ | 1826 | /* Do accounting for the write space. */ |
1827 | sctp_set_owner_w(chunk); | 1827 | sctp_set_owner_w(chunk); |
1828 | 1828 | ||
1829 | chunk->transport = chunk_tp; | 1829 | chunk->transport = chunk_tp; |
1830 | } | 1830 | } |
1831 | 1831 | ||
1832 | /* Send it to the lower layers. Note: all chunks | 1832 | /* Send it to the lower layers. Note: all chunks |
1833 | * must either fail or succeed. The lower layer | 1833 | * must either fail or succeed. The lower layer |
1834 | * works that way today. Keep it that way or this | 1834 | * works that way today. Keep it that way or this |
1835 | * breaks. | 1835 | * breaks. |
1836 | */ | 1836 | */ |
1837 | err = sctp_primitive_SEND(asoc, datamsg); | 1837 | err = sctp_primitive_SEND(asoc, datamsg); |
1838 | /* Did the lower layer accept the chunk? */ | 1838 | /* Did the lower layer accept the chunk? */ |
1839 | if (err) | 1839 | if (err) |
1840 | sctp_datamsg_free(datamsg); | 1840 | sctp_datamsg_free(datamsg); |
1841 | else | 1841 | else |
1842 | sctp_datamsg_put(datamsg); | 1842 | sctp_datamsg_put(datamsg); |
1843 | 1843 | ||
1844 | SCTP_DEBUG_PRINTK("We sent primitively.\n"); | 1844 | SCTP_DEBUG_PRINTK("We sent primitively.\n"); |
1845 | 1845 | ||
1846 | if (err) | 1846 | if (err) |
1847 | goto out_free; | 1847 | goto out_free; |
1848 | else | 1848 | else |
1849 | err = msg_len; | 1849 | err = msg_len; |
1850 | 1850 | ||
1851 | /* If we are already past ASSOCIATE, the lower | 1851 | /* If we are already past ASSOCIATE, the lower |
1852 | * layers are responsible for association cleanup. | 1852 | * layers are responsible for association cleanup. |
1853 | */ | 1853 | */ |
1854 | goto out_unlock; | 1854 | goto out_unlock; |
1855 | 1855 | ||
1856 | out_free: | 1856 | out_free: |
1857 | if (new_asoc) | 1857 | if (new_asoc) |
1858 | sctp_association_free(asoc); | 1858 | sctp_association_free(asoc); |
1859 | out_unlock: | 1859 | out_unlock: |
1860 | sctp_release_sock(sk); | 1860 | sctp_release_sock(sk); |
1861 | 1861 | ||
1862 | out_nounlock: | 1862 | out_nounlock: |
1863 | return sctp_error(sk, msg_flags, err); | 1863 | return sctp_error(sk, msg_flags, err); |
1864 | 1864 | ||
1865 | #if 0 | 1865 | #if 0 |
1866 | do_sock_err: | 1866 | do_sock_err: |
1867 | if (msg_len) | 1867 | if (msg_len) |
1868 | err = msg_len; | 1868 | err = msg_len; |
1869 | else | 1869 | else |
1870 | err = sock_error(sk); | 1870 | err = sock_error(sk); |
1871 | goto out; | 1871 | goto out; |
1872 | 1872 | ||
1873 | do_interrupted: | 1873 | do_interrupted: |
1874 | if (msg_len) | 1874 | if (msg_len) |
1875 | err = msg_len; | 1875 | err = msg_len; |
1876 | goto out; | 1876 | goto out; |
1877 | #endif /* 0 */ | 1877 | #endif /* 0 */ |
1878 | } | 1878 | } |
1879 | 1879 | ||
1880 | /* This is an extended version of skb_pull() that removes the data from the | 1880 | /* This is an extended version of skb_pull() that removes the data from the |
1881 | * start of a skb even when data is spread across the list of skb's in the | 1881 | * start of a skb even when data is spread across the list of skb's in the |
1882 | * frag_list. len specifies the total amount of data that needs to be removed. | 1882 | * frag_list. len specifies the total amount of data that needs to be removed. |
1883 | * when 'len' bytes could be removed from the skb, it returns 0. | 1883 | * when 'len' bytes could be removed from the skb, it returns 0. |
1884 | * If 'len' exceeds the total skb length, it returns the no. of bytes that | 1884 | * If 'len' exceeds the total skb length, it returns the no. of bytes that |
1885 | * could not be removed. | 1885 | * could not be removed. |
1886 | */ | 1886 | */ |
1887 | static int sctp_skb_pull(struct sk_buff *skb, int len) | 1887 | static int sctp_skb_pull(struct sk_buff *skb, int len) |
1888 | { | 1888 | { |
1889 | struct sk_buff *list; | 1889 | struct sk_buff *list; |
1890 | int skb_len = skb_headlen(skb); | 1890 | int skb_len = skb_headlen(skb); |
1891 | int rlen; | 1891 | int rlen; |
1892 | 1892 | ||
1893 | if (len <= skb_len) { | 1893 | if (len <= skb_len) { |
1894 | __skb_pull(skb, len); | 1894 | __skb_pull(skb, len); |
1895 | return 0; | 1895 | return 0; |
1896 | } | 1896 | } |
1897 | len -= skb_len; | 1897 | len -= skb_len; |
1898 | __skb_pull(skb, skb_len); | 1898 | __skb_pull(skb, skb_len); |
1899 | 1899 | ||
1900 | skb_walk_frags(skb, list) { | 1900 | skb_walk_frags(skb, list) { |
1901 | rlen = sctp_skb_pull(list, len); | 1901 | rlen = sctp_skb_pull(list, len); |
1902 | skb->len -= (len-rlen); | 1902 | skb->len -= (len-rlen); |
1903 | skb->data_len -= (len-rlen); | 1903 | skb->data_len -= (len-rlen); |
1904 | 1904 | ||
1905 | if (!rlen) | 1905 | if (!rlen) |
1906 | return 0; | 1906 | return 0; |
1907 | 1907 | ||
1908 | len = rlen; | 1908 | len = rlen; |
1909 | } | 1909 | } |
1910 | 1910 | ||
1911 | return len; | 1911 | return len; |
1912 | } | 1912 | } |
1913 | 1913 | ||
1914 | /* API 3.1.3 recvmsg() - UDP Style Syntax | 1914 | /* API 3.1.3 recvmsg() - UDP Style Syntax |
1915 | * | 1915 | * |
1916 | * ssize_t recvmsg(int socket, struct msghdr *message, | 1916 | * ssize_t recvmsg(int socket, struct msghdr *message, |
1917 | * int flags); | 1917 | * int flags); |
1918 | * | 1918 | * |
1919 | * socket - the socket descriptor of the endpoint. | 1919 | * socket - the socket descriptor of the endpoint. |
1920 | * message - pointer to the msghdr structure which contains a single | 1920 | * message - pointer to the msghdr structure which contains a single |
1921 | * user message and possibly some ancillary data. | 1921 | * user message and possibly some ancillary data. |
1922 | * | 1922 | * |
1923 | * See Section 5 for complete description of the data | 1923 | * See Section 5 for complete description of the data |
1924 | * structures. | 1924 | * structures. |
1925 | * | 1925 | * |
1926 | * flags - flags sent or received with the user message, see Section | 1926 | * flags - flags sent or received with the user message, see Section |
1927 | * 5 for complete description of the flags. | 1927 | * 5 for complete description of the flags. |
1928 | */ | 1928 | */ |
1929 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *); | 1929 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *); |
1930 | 1930 | ||
1931 | SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, | 1931 | SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, |
1932 | struct msghdr *msg, size_t len, int noblock, | 1932 | struct msghdr *msg, size_t len, int noblock, |
1933 | int flags, int *addr_len) | 1933 | int flags, int *addr_len) |
1934 | { | 1934 | { |
1935 | struct sctp_ulpevent *event = NULL; | 1935 | struct sctp_ulpevent *event = NULL; |
1936 | struct sctp_sock *sp = sctp_sk(sk); | 1936 | struct sctp_sock *sp = sctp_sk(sk); |
1937 | struct sk_buff *skb; | 1937 | struct sk_buff *skb; |
1938 | int copied; | 1938 | int copied; |
1939 | int err = 0; | 1939 | int err = 0; |
1940 | int skb_len; | 1940 | int skb_len; |
1941 | 1941 | ||
1942 | SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: " | 1942 | SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: " |
1943 | "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg, | 1943 | "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg, |
1944 | "len", len, "knoblauch", noblock, | 1944 | "len", len, "knoblauch", noblock, |
1945 | "flags", flags, "addr_len", addr_len); | 1945 | "flags", flags, "addr_len", addr_len); |
1946 | 1946 | ||
1947 | sctp_lock_sock(sk); | 1947 | sctp_lock_sock(sk); |
1948 | 1948 | ||
1949 | if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) { | 1949 | if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) { |
1950 | err = -ENOTCONN; | 1950 | err = -ENOTCONN; |
1951 | goto out; | 1951 | goto out; |
1952 | } | 1952 | } |
1953 | 1953 | ||
1954 | skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); | 1954 | skb = sctp_skb_recv_datagram(sk, flags, noblock, &err); |
1955 | if (!skb) | 1955 | if (!skb) |
1956 | goto out; | 1956 | goto out; |
1957 | 1957 | ||
1958 | /* Get the total length of the skb including any skb's in the | 1958 | /* Get the total length of the skb including any skb's in the |
1959 | * frag_list. | 1959 | * frag_list. |
1960 | */ | 1960 | */ |
1961 | skb_len = skb->len; | 1961 | skb_len = skb->len; |
1962 | 1962 | ||
1963 | copied = skb_len; | 1963 | copied = skb_len; |
1964 | if (copied > len) | 1964 | if (copied > len) |
1965 | copied = len; | 1965 | copied = len; |
1966 | 1966 | ||
1967 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); | 1967 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
1968 | 1968 | ||
1969 | event = sctp_skb2event(skb); | 1969 | event = sctp_skb2event(skb); |
1970 | 1970 | ||
1971 | if (err) | 1971 | if (err) |
1972 | goto out_free; | 1972 | goto out_free; |
1973 | 1973 | ||
1974 | sock_recv_ts_and_drops(msg, sk, skb); | 1974 | sock_recv_ts_and_drops(msg, sk, skb); |
1975 | if (sctp_ulpevent_is_notification(event)) { | 1975 | if (sctp_ulpevent_is_notification(event)) { |
1976 | msg->msg_flags |= MSG_NOTIFICATION; | 1976 | msg->msg_flags |= MSG_NOTIFICATION; |
1977 | sp->pf->event_msgname(event, msg->msg_name, addr_len); | 1977 | sp->pf->event_msgname(event, msg->msg_name, addr_len); |
1978 | } else { | 1978 | } else { |
1979 | sp->pf->skb_msgname(skb, msg->msg_name, addr_len); | 1979 | sp->pf->skb_msgname(skb, msg->msg_name, addr_len); |
1980 | } | 1980 | } |
1981 | 1981 | ||
1982 | /* Check if we allow SCTP_SNDRCVINFO. */ | 1982 | /* Check if we allow SCTP_SNDRCVINFO. */ |
1983 | if (sp->subscribe.sctp_data_io_event) | 1983 | if (sp->subscribe.sctp_data_io_event) |
1984 | sctp_ulpevent_read_sndrcvinfo(event, msg); | 1984 | sctp_ulpevent_read_sndrcvinfo(event, msg); |
1985 | #if 0 | 1985 | #if 0 |
1986 | /* FIXME: we should be calling IP/IPv6 layers. */ | 1986 | /* FIXME: we should be calling IP/IPv6 layers. */ |
1987 | if (sk->sk_protinfo.af_inet.cmsg_flags) | 1987 | if (sk->sk_protinfo.af_inet.cmsg_flags) |
1988 | ip_cmsg_recv(msg, skb); | 1988 | ip_cmsg_recv(msg, skb); |
1989 | #endif | 1989 | #endif |
1990 | 1990 | ||
1991 | err = copied; | 1991 | err = copied; |
1992 | 1992 | ||
1993 | /* If skb's length exceeds the user's buffer, update the skb and | 1993 | /* If skb's length exceeds the user's buffer, update the skb and |
1994 | * push it back to the receive_queue so that the next call to | 1994 | * push it back to the receive_queue so that the next call to |
1995 | * recvmsg() will return the remaining data. Don't set MSG_EOR. | 1995 | * recvmsg() will return the remaining data. Don't set MSG_EOR. |
1996 | */ | 1996 | */ |
1997 | if (skb_len > copied) { | 1997 | if (skb_len > copied) { |
1998 | msg->msg_flags &= ~MSG_EOR; | 1998 | msg->msg_flags &= ~MSG_EOR; |
1999 | if (flags & MSG_PEEK) | 1999 | if (flags & MSG_PEEK) |
2000 | goto out_free; | 2000 | goto out_free; |
2001 | sctp_skb_pull(skb, copied); | 2001 | sctp_skb_pull(skb, copied); |
2002 | skb_queue_head(&sk->sk_receive_queue, skb); | 2002 | skb_queue_head(&sk->sk_receive_queue, skb); |
2003 | 2003 | ||
2004 | /* When only partial message is copied to the user, increase | 2004 | /* When only partial message is copied to the user, increase |
2005 | * rwnd by that amount. If all the data in the skb is read, | 2005 | * rwnd by that amount. If all the data in the skb is read, |
2006 | * rwnd is updated when the event is freed. | 2006 | * rwnd is updated when the event is freed. |
2007 | */ | 2007 | */ |
2008 | if (!sctp_ulpevent_is_notification(event)) | 2008 | if (!sctp_ulpevent_is_notification(event)) |
2009 | sctp_assoc_rwnd_increase(event->asoc, copied); | 2009 | sctp_assoc_rwnd_increase(event->asoc, copied); |
2010 | goto out; | 2010 | goto out; |
2011 | } else if ((event->msg_flags & MSG_NOTIFICATION) || | 2011 | } else if ((event->msg_flags & MSG_NOTIFICATION) || |
2012 | (event->msg_flags & MSG_EOR)) | 2012 | (event->msg_flags & MSG_EOR)) |
2013 | msg->msg_flags |= MSG_EOR; | 2013 | msg->msg_flags |= MSG_EOR; |
2014 | else | 2014 | else |
2015 | msg->msg_flags &= ~MSG_EOR; | 2015 | msg->msg_flags &= ~MSG_EOR; |
2016 | 2016 | ||
2017 | out_free: | 2017 | out_free: |
2018 | if (flags & MSG_PEEK) { | 2018 | if (flags & MSG_PEEK) { |
2019 | /* Release the skb reference acquired after peeking the skb in | 2019 | /* Release the skb reference acquired after peeking the skb in |
2020 | * sctp_skb_recv_datagram(). | 2020 | * sctp_skb_recv_datagram(). |
2021 | */ | 2021 | */ |
2022 | kfree_skb(skb); | 2022 | kfree_skb(skb); |
2023 | } else { | 2023 | } else { |
2024 | /* Free the event which includes releasing the reference to | 2024 | /* Free the event which includes releasing the reference to |
2025 | * the owner of the skb, freeing the skb and updating the | 2025 | * the owner of the skb, freeing the skb and updating the |
2026 | * rwnd. | 2026 | * rwnd. |
2027 | */ | 2027 | */ |
2028 | sctp_ulpevent_free(event); | 2028 | sctp_ulpevent_free(event); |
2029 | } | 2029 | } |
2030 | out: | 2030 | out: |
2031 | sctp_release_sock(sk); | 2031 | sctp_release_sock(sk); |
2032 | return err; | 2032 | return err; |
2033 | } | 2033 | } |
2034 | 2034 | ||
2035 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 2035 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
2036 | * | 2036 | * |
2037 | * This option is a on/off flag. If enabled no SCTP message | 2037 | * This option is a on/off flag. If enabled no SCTP message |
2038 | * fragmentation will be performed. Instead if a message being sent | 2038 | * fragmentation will be performed. Instead if a message being sent |
2039 | * exceeds the current PMTU size, the message will NOT be sent and | 2039 | * exceeds the current PMTU size, the message will NOT be sent and |
2040 | * instead a error will be indicated to the user. | 2040 | * instead a error will be indicated to the user. |
2041 | */ | 2041 | */ |
2042 | static int sctp_setsockopt_disable_fragments(struct sock *sk, | 2042 | static int sctp_setsockopt_disable_fragments(struct sock *sk, |
2043 | char __user *optval, | 2043 | char __user *optval, |
2044 | unsigned int optlen) | 2044 | unsigned int optlen) |
2045 | { | 2045 | { |
2046 | int val; | 2046 | int val; |
2047 | 2047 | ||
2048 | if (optlen < sizeof(int)) | 2048 | if (optlen < sizeof(int)) |
2049 | return -EINVAL; | 2049 | return -EINVAL; |
2050 | 2050 | ||
2051 | if (get_user(val, (int __user *)optval)) | 2051 | if (get_user(val, (int __user *)optval)) |
2052 | return -EFAULT; | 2052 | return -EFAULT; |
2053 | 2053 | ||
2054 | sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; | 2054 | sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1; |
2055 | 2055 | ||
2056 | return 0; | 2056 | return 0; |
2057 | } | 2057 | } |
2058 | 2058 | ||
2059 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, | 2059 | static int sctp_setsockopt_events(struct sock *sk, char __user *optval, |
2060 | unsigned int optlen) | 2060 | unsigned int optlen) |
2061 | { | 2061 | { |
2062 | if (optlen > sizeof(struct sctp_event_subscribe)) | 2062 | if (optlen > sizeof(struct sctp_event_subscribe)) |
2063 | return -EINVAL; | 2063 | return -EINVAL; |
2064 | if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) | 2064 | if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen)) |
2065 | return -EFAULT; | 2065 | return -EFAULT; |
2066 | return 0; | 2066 | return 0; |
2067 | } | 2067 | } |
2068 | 2068 | ||
2069 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 2069 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
2070 | * | 2070 | * |
2071 | * This socket option is applicable to the UDP-style socket only. When | 2071 | * This socket option is applicable to the UDP-style socket only. When |
2072 | * set it will cause associations that are idle for more than the | 2072 | * set it will cause associations that are idle for more than the |
2073 | * specified number of seconds to automatically close. An association | 2073 | * specified number of seconds to automatically close. An association |
2074 | * being idle is defined an association that has NOT sent or received | 2074 | * being idle is defined an association that has NOT sent or received |
2075 | * user data. The special value of '0' indicates that no automatic | 2075 | * user data. The special value of '0' indicates that no automatic |
2076 | * close of any associations should be performed. The option expects an | 2076 | * close of any associations should be performed. The option expects an |
2077 | * integer defining the number of seconds of idle time before an | 2077 | * integer defining the number of seconds of idle time before an |
2078 | * association is closed. | 2078 | * association is closed. |
2079 | */ | 2079 | */ |
2080 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, | 2080 | static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval, |
2081 | unsigned int optlen) | 2081 | unsigned int optlen) |
2082 | { | 2082 | { |
2083 | struct sctp_sock *sp = sctp_sk(sk); | 2083 | struct sctp_sock *sp = sctp_sk(sk); |
2084 | 2084 | ||
2085 | /* Applicable to UDP-style socket only */ | 2085 | /* Applicable to UDP-style socket only */ |
2086 | if (sctp_style(sk, TCP)) | 2086 | if (sctp_style(sk, TCP)) |
2087 | return -EOPNOTSUPP; | 2087 | return -EOPNOTSUPP; |
2088 | if (optlen != sizeof(int)) | 2088 | if (optlen != sizeof(int)) |
2089 | return -EINVAL; | 2089 | return -EINVAL; |
2090 | if (copy_from_user(&sp->autoclose, optval, optlen)) | 2090 | if (copy_from_user(&sp->autoclose, optval, optlen)) |
2091 | return -EFAULT; | 2091 | return -EFAULT; |
2092 | /* make sure it won't exceed MAX_SCHEDULE_TIMEOUT */ | 2092 | /* make sure it won't exceed MAX_SCHEDULE_TIMEOUT */ |
2093 | sp->autoclose = min_t(long, sp->autoclose, MAX_SCHEDULE_TIMEOUT / HZ); | 2093 | sp->autoclose = min_t(long, sp->autoclose, MAX_SCHEDULE_TIMEOUT / HZ); |
2094 | 2094 | ||
2095 | return 0; | 2095 | return 0; |
2096 | } | 2096 | } |
2097 | 2097 | ||
2098 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 2098 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
2099 | * | 2099 | * |
2100 | * Applications can enable or disable heartbeats for any peer address of | 2100 | * Applications can enable or disable heartbeats for any peer address of |
2101 | * an association, modify an address's heartbeat interval, force a | 2101 | * an association, modify an address's heartbeat interval, force a |
2102 | * heartbeat to be sent immediately, and adjust the address's maximum | 2102 | * heartbeat to be sent immediately, and adjust the address's maximum |
2103 | * number of retransmissions sent before an address is considered | 2103 | * number of retransmissions sent before an address is considered |
2104 | * unreachable. The following structure is used to access and modify an | 2104 | * unreachable. The following structure is used to access and modify an |
2105 | * address's parameters: | 2105 | * address's parameters: |
2106 | * | 2106 | * |
2107 | * struct sctp_paddrparams { | 2107 | * struct sctp_paddrparams { |
2108 | * sctp_assoc_t spp_assoc_id; | 2108 | * sctp_assoc_t spp_assoc_id; |
2109 | * struct sockaddr_storage spp_address; | 2109 | * struct sockaddr_storage spp_address; |
2110 | * uint32_t spp_hbinterval; | 2110 | * uint32_t spp_hbinterval; |
2111 | * uint16_t spp_pathmaxrxt; | 2111 | * uint16_t spp_pathmaxrxt; |
2112 | * uint32_t spp_pathmtu; | 2112 | * uint32_t spp_pathmtu; |
2113 | * uint32_t spp_sackdelay; | 2113 | * uint32_t spp_sackdelay; |
2114 | * uint32_t spp_flags; | 2114 | * uint32_t spp_flags; |
2115 | * }; | 2115 | * }; |
2116 | * | 2116 | * |
2117 | * spp_assoc_id - (one-to-many style socket) This is filled in the | 2117 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
2118 | * application, and identifies the association for | 2118 | * application, and identifies the association for |
2119 | * this query. | 2119 | * this query. |
2120 | * spp_address - This specifies which address is of interest. | 2120 | * spp_address - This specifies which address is of interest. |
2121 | * spp_hbinterval - This contains the value of the heartbeat interval, | 2121 | * spp_hbinterval - This contains the value of the heartbeat interval, |
2122 | * in milliseconds. If a value of zero | 2122 | * in milliseconds. If a value of zero |
2123 | * is present in this field then no changes are to | 2123 | * is present in this field then no changes are to |
2124 | * be made to this parameter. | 2124 | * be made to this parameter. |
2125 | * spp_pathmaxrxt - This contains the maximum number of | 2125 | * spp_pathmaxrxt - This contains the maximum number of |
2126 | * retransmissions before this address shall be | 2126 | * retransmissions before this address shall be |
2127 | * considered unreachable. If a value of zero | 2127 | * considered unreachable. If a value of zero |
2128 | * is present in this field then no changes are to | 2128 | * is present in this field then no changes are to |
2129 | * be made to this parameter. | 2129 | * be made to this parameter. |
2130 | * spp_pathmtu - When Path MTU discovery is disabled the value | 2130 | * spp_pathmtu - When Path MTU discovery is disabled the value |
2131 | * specified here will be the "fixed" path mtu. | 2131 | * specified here will be the "fixed" path mtu. |
2132 | * Note that if the spp_address field is empty | 2132 | * Note that if the spp_address field is empty |
2133 | * then all associations on this address will | 2133 | * then all associations on this address will |
2134 | * have this fixed path mtu set upon them. | 2134 | * have this fixed path mtu set upon them. |
2135 | * | 2135 | * |
2136 | * spp_sackdelay - When delayed sack is enabled, this value specifies | 2136 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
2137 | * the number of milliseconds that sacks will be delayed | 2137 | * the number of milliseconds that sacks will be delayed |
2138 | * for. This value will apply to all addresses of an | 2138 | * for. This value will apply to all addresses of an |
2139 | * association if the spp_address field is empty. Note | 2139 | * association if the spp_address field is empty. Note |
2140 | * also, that if delayed sack is enabled and this | 2140 | * also, that if delayed sack is enabled and this |
2141 | * value is set to 0, no change is made to the last | 2141 | * value is set to 0, no change is made to the last |
2142 | * recorded delayed sack timer value. | 2142 | * recorded delayed sack timer value. |
2143 | * | 2143 | * |
2144 | * spp_flags - These flags are used to control various features | 2144 | * spp_flags - These flags are used to control various features |
2145 | * on an association. The flag field may contain | 2145 | * on an association. The flag field may contain |
2146 | * zero or more of the following options. | 2146 | * zero or more of the following options. |
2147 | * | 2147 | * |
2148 | * SPP_HB_ENABLE - Enable heartbeats on the | 2148 | * SPP_HB_ENABLE - Enable heartbeats on the |
2149 | * specified address. Note that if the address | 2149 | * specified address. Note that if the address |
2150 | * field is empty all addresses for the association | 2150 | * field is empty all addresses for the association |
2151 | * have heartbeats enabled upon them. | 2151 | * have heartbeats enabled upon them. |
2152 | * | 2152 | * |
2153 | * SPP_HB_DISABLE - Disable heartbeats on the | 2153 | * SPP_HB_DISABLE - Disable heartbeats on the |
2154 | * speicifed address. Note that if the address | 2154 | * speicifed address. Note that if the address |
2155 | * field is empty all addresses for the association | 2155 | * field is empty all addresses for the association |
2156 | * will have their heartbeats disabled. Note also | 2156 | * will have their heartbeats disabled. Note also |
2157 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are | 2157 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
2158 | * mutually exclusive, only one of these two should | 2158 | * mutually exclusive, only one of these two should |
2159 | * be specified. Enabling both fields will have | 2159 | * be specified. Enabling both fields will have |
2160 | * undetermined results. | 2160 | * undetermined results. |
2161 | * | 2161 | * |
2162 | * SPP_HB_DEMAND - Request a user initiated heartbeat | 2162 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
2163 | * to be made immediately. | 2163 | * to be made immediately. |
2164 | * | 2164 | * |
2165 | * SPP_HB_TIME_IS_ZERO - Specify's that the time for | 2165 | * SPP_HB_TIME_IS_ZERO - Specify's that the time for |
2166 | * heartbeat delayis to be set to the value of 0 | 2166 | * heartbeat delayis to be set to the value of 0 |
2167 | * milliseconds. | 2167 | * milliseconds. |
2168 | * | 2168 | * |
2169 | * SPP_PMTUD_ENABLE - This field will enable PMTU | 2169 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
2170 | * discovery upon the specified address. Note that | 2170 | * discovery upon the specified address. Note that |
2171 | * if the address feild is empty then all addresses | 2171 | * if the address feild is empty then all addresses |
2172 | * on the association are effected. | 2172 | * on the association are effected. |
2173 | * | 2173 | * |
2174 | * SPP_PMTUD_DISABLE - This field will disable PMTU | 2174 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
2175 | * discovery upon the specified address. Note that | 2175 | * discovery upon the specified address. Note that |
2176 | * if the address feild is empty then all addresses | 2176 | * if the address feild is empty then all addresses |
2177 | * on the association are effected. Not also that | 2177 | * on the association are effected. Not also that |
2178 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 2178 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
2179 | * exclusive. Enabling both will have undetermined | 2179 | * exclusive. Enabling both will have undetermined |
2180 | * results. | 2180 | * results. |
2181 | * | 2181 | * |
2182 | * SPP_SACKDELAY_ENABLE - Setting this flag turns | 2182 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
2183 | * on delayed sack. The time specified in spp_sackdelay | 2183 | * on delayed sack. The time specified in spp_sackdelay |
2184 | * is used to specify the sack delay for this address. Note | 2184 | * is used to specify the sack delay for this address. Note |
2185 | * that if spp_address is empty then all addresses will | 2185 | * that if spp_address is empty then all addresses will |
2186 | * enable delayed sack and take on the sack delay | 2186 | * enable delayed sack and take on the sack delay |
2187 | * value specified in spp_sackdelay. | 2187 | * value specified in spp_sackdelay. |
2188 | * SPP_SACKDELAY_DISABLE - Setting this flag turns | 2188 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
2189 | * off delayed sack. If the spp_address field is blank then | 2189 | * off delayed sack. If the spp_address field is blank then |
2190 | * delayed sack is disabled for the entire association. Note | 2190 | * delayed sack is disabled for the entire association. Note |
2191 | * also that this field is mutually exclusive to | 2191 | * also that this field is mutually exclusive to |
2192 | * SPP_SACKDELAY_ENABLE, setting both will have undefined | 2192 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
2193 | * results. | 2193 | * results. |
2194 | */ | 2194 | */ |
2195 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, | 2195 | static int sctp_apply_peer_addr_params(struct sctp_paddrparams *params, |
2196 | struct sctp_transport *trans, | 2196 | struct sctp_transport *trans, |
2197 | struct sctp_association *asoc, | 2197 | struct sctp_association *asoc, |
2198 | struct sctp_sock *sp, | 2198 | struct sctp_sock *sp, |
2199 | int hb_change, | 2199 | int hb_change, |
2200 | int pmtud_change, | 2200 | int pmtud_change, |
2201 | int sackdelay_change) | 2201 | int sackdelay_change) |
2202 | { | 2202 | { |
2203 | int error; | 2203 | int error; |
2204 | 2204 | ||
2205 | if (params->spp_flags & SPP_HB_DEMAND && trans) { | 2205 | if (params->spp_flags & SPP_HB_DEMAND && trans) { |
2206 | error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans); | 2206 | error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans); |
2207 | if (error) | 2207 | if (error) |
2208 | return error; | 2208 | return error; |
2209 | } | 2209 | } |
2210 | 2210 | ||
2211 | /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of | 2211 | /* Note that unless the spp_flag is set to SPP_HB_ENABLE the value of |
2212 | * this field is ignored. Note also that a value of zero indicates | 2212 | * this field is ignored. Note also that a value of zero indicates |
2213 | * the current setting should be left unchanged. | 2213 | * the current setting should be left unchanged. |
2214 | */ | 2214 | */ |
2215 | if (params->spp_flags & SPP_HB_ENABLE) { | 2215 | if (params->spp_flags & SPP_HB_ENABLE) { |
2216 | 2216 | ||
2217 | /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is | 2217 | /* Re-zero the interval if the SPP_HB_TIME_IS_ZERO is |
2218 | * set. This lets us use 0 value when this flag | 2218 | * set. This lets us use 0 value when this flag |
2219 | * is set. | 2219 | * is set. |
2220 | */ | 2220 | */ |
2221 | if (params->spp_flags & SPP_HB_TIME_IS_ZERO) | 2221 | if (params->spp_flags & SPP_HB_TIME_IS_ZERO) |
2222 | params->spp_hbinterval = 0; | 2222 | params->spp_hbinterval = 0; |
2223 | 2223 | ||
2224 | if (params->spp_hbinterval || | 2224 | if (params->spp_hbinterval || |
2225 | (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { | 2225 | (params->spp_flags & SPP_HB_TIME_IS_ZERO)) { |
2226 | if (trans) { | 2226 | if (trans) { |
2227 | trans->hbinterval = | 2227 | trans->hbinterval = |
2228 | msecs_to_jiffies(params->spp_hbinterval); | 2228 | msecs_to_jiffies(params->spp_hbinterval); |
2229 | } else if (asoc) { | 2229 | } else if (asoc) { |
2230 | asoc->hbinterval = | 2230 | asoc->hbinterval = |
2231 | msecs_to_jiffies(params->spp_hbinterval); | 2231 | msecs_to_jiffies(params->spp_hbinterval); |
2232 | } else { | 2232 | } else { |
2233 | sp->hbinterval = params->spp_hbinterval; | 2233 | sp->hbinterval = params->spp_hbinterval; |
2234 | } | 2234 | } |
2235 | } | 2235 | } |
2236 | } | 2236 | } |
2237 | 2237 | ||
2238 | if (hb_change) { | 2238 | if (hb_change) { |
2239 | if (trans) { | 2239 | if (trans) { |
2240 | trans->param_flags = | 2240 | trans->param_flags = |
2241 | (trans->param_flags & ~SPP_HB) | hb_change; | 2241 | (trans->param_flags & ~SPP_HB) | hb_change; |
2242 | } else if (asoc) { | 2242 | } else if (asoc) { |
2243 | asoc->param_flags = | 2243 | asoc->param_flags = |
2244 | (asoc->param_flags & ~SPP_HB) | hb_change; | 2244 | (asoc->param_flags & ~SPP_HB) | hb_change; |
2245 | } else { | 2245 | } else { |
2246 | sp->param_flags = | 2246 | sp->param_flags = |
2247 | (sp->param_flags & ~SPP_HB) | hb_change; | 2247 | (sp->param_flags & ~SPP_HB) | hb_change; |
2248 | } | 2248 | } |
2249 | } | 2249 | } |
2250 | 2250 | ||
2251 | /* When Path MTU discovery is disabled the value specified here will | 2251 | /* When Path MTU discovery is disabled the value specified here will |
2252 | * be the "fixed" path mtu (i.e. the value of the spp_flags field must | 2252 | * be the "fixed" path mtu (i.e. the value of the spp_flags field must |
2253 | * include the flag SPP_PMTUD_DISABLE for this field to have any | 2253 | * include the flag SPP_PMTUD_DISABLE for this field to have any |
2254 | * effect). | 2254 | * effect). |
2255 | */ | 2255 | */ |
2256 | if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { | 2256 | if ((params->spp_flags & SPP_PMTUD_DISABLE) && params->spp_pathmtu) { |
2257 | if (trans) { | 2257 | if (trans) { |
2258 | trans->pathmtu = params->spp_pathmtu; | 2258 | trans->pathmtu = params->spp_pathmtu; |
2259 | sctp_assoc_sync_pmtu(asoc); | 2259 | sctp_assoc_sync_pmtu(asoc); |
2260 | } else if (asoc) { | 2260 | } else if (asoc) { |
2261 | asoc->pathmtu = params->spp_pathmtu; | 2261 | asoc->pathmtu = params->spp_pathmtu; |
2262 | sctp_frag_point(asoc, params->spp_pathmtu); | 2262 | sctp_frag_point(asoc, params->spp_pathmtu); |
2263 | } else { | 2263 | } else { |
2264 | sp->pathmtu = params->spp_pathmtu; | 2264 | sp->pathmtu = params->spp_pathmtu; |
2265 | } | 2265 | } |
2266 | } | 2266 | } |
2267 | 2267 | ||
2268 | if (pmtud_change) { | 2268 | if (pmtud_change) { |
2269 | if (trans) { | 2269 | if (trans) { |
2270 | int update = (trans->param_flags & SPP_PMTUD_DISABLE) && | 2270 | int update = (trans->param_flags & SPP_PMTUD_DISABLE) && |
2271 | (params->spp_flags & SPP_PMTUD_ENABLE); | 2271 | (params->spp_flags & SPP_PMTUD_ENABLE); |
2272 | trans->param_flags = | 2272 | trans->param_flags = |
2273 | (trans->param_flags & ~SPP_PMTUD) | pmtud_change; | 2273 | (trans->param_flags & ~SPP_PMTUD) | pmtud_change; |
2274 | if (update) { | 2274 | if (update) { |
2275 | sctp_transport_pmtu(trans); | 2275 | sctp_transport_pmtu(trans); |
2276 | sctp_assoc_sync_pmtu(asoc); | 2276 | sctp_assoc_sync_pmtu(asoc); |
2277 | } | 2277 | } |
2278 | } else if (asoc) { | 2278 | } else if (asoc) { |
2279 | asoc->param_flags = | 2279 | asoc->param_flags = |
2280 | (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; | 2280 | (asoc->param_flags & ~SPP_PMTUD) | pmtud_change; |
2281 | } else { | 2281 | } else { |
2282 | sp->param_flags = | 2282 | sp->param_flags = |
2283 | (sp->param_flags & ~SPP_PMTUD) | pmtud_change; | 2283 | (sp->param_flags & ~SPP_PMTUD) | pmtud_change; |
2284 | } | 2284 | } |
2285 | } | 2285 | } |
2286 | 2286 | ||
2287 | /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the | 2287 | /* Note that unless the spp_flag is set to SPP_SACKDELAY_ENABLE the |
2288 | * value of this field is ignored. Note also that a value of zero | 2288 | * value of this field is ignored. Note also that a value of zero |
2289 | * indicates the current setting should be left unchanged. | 2289 | * indicates the current setting should be left unchanged. |
2290 | */ | 2290 | */ |
2291 | if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { | 2291 | if ((params->spp_flags & SPP_SACKDELAY_ENABLE) && params->spp_sackdelay) { |
2292 | if (trans) { | 2292 | if (trans) { |
2293 | trans->sackdelay = | 2293 | trans->sackdelay = |
2294 | msecs_to_jiffies(params->spp_sackdelay); | 2294 | msecs_to_jiffies(params->spp_sackdelay); |
2295 | } else if (asoc) { | 2295 | } else if (asoc) { |
2296 | asoc->sackdelay = | 2296 | asoc->sackdelay = |
2297 | msecs_to_jiffies(params->spp_sackdelay); | 2297 | msecs_to_jiffies(params->spp_sackdelay); |
2298 | } else { | 2298 | } else { |
2299 | sp->sackdelay = params->spp_sackdelay; | 2299 | sp->sackdelay = params->spp_sackdelay; |
2300 | } | 2300 | } |
2301 | } | 2301 | } |
2302 | 2302 | ||
2303 | if (sackdelay_change) { | 2303 | if (sackdelay_change) { |
2304 | if (trans) { | 2304 | if (trans) { |
2305 | trans->param_flags = | 2305 | trans->param_flags = |
2306 | (trans->param_flags & ~SPP_SACKDELAY) | | 2306 | (trans->param_flags & ~SPP_SACKDELAY) | |
2307 | sackdelay_change; | 2307 | sackdelay_change; |
2308 | } else if (asoc) { | 2308 | } else if (asoc) { |
2309 | asoc->param_flags = | 2309 | asoc->param_flags = |
2310 | (asoc->param_flags & ~SPP_SACKDELAY) | | 2310 | (asoc->param_flags & ~SPP_SACKDELAY) | |
2311 | sackdelay_change; | 2311 | sackdelay_change; |
2312 | } else { | 2312 | } else { |
2313 | sp->param_flags = | 2313 | sp->param_flags = |
2314 | (sp->param_flags & ~SPP_SACKDELAY) | | 2314 | (sp->param_flags & ~SPP_SACKDELAY) | |
2315 | sackdelay_change; | 2315 | sackdelay_change; |
2316 | } | 2316 | } |
2317 | } | 2317 | } |
2318 | 2318 | ||
2319 | /* Note that a value of zero indicates the current setting should be | 2319 | /* Note that a value of zero indicates the current setting should be |
2320 | left unchanged. | 2320 | left unchanged. |
2321 | */ | 2321 | */ |
2322 | if (params->spp_pathmaxrxt) { | 2322 | if (params->spp_pathmaxrxt) { |
2323 | if (trans) { | 2323 | if (trans) { |
2324 | trans->pathmaxrxt = params->spp_pathmaxrxt; | 2324 | trans->pathmaxrxt = params->spp_pathmaxrxt; |
2325 | } else if (asoc) { | 2325 | } else if (asoc) { |
2326 | asoc->pathmaxrxt = params->spp_pathmaxrxt; | 2326 | asoc->pathmaxrxt = params->spp_pathmaxrxt; |
2327 | } else { | 2327 | } else { |
2328 | sp->pathmaxrxt = params->spp_pathmaxrxt; | 2328 | sp->pathmaxrxt = params->spp_pathmaxrxt; |
2329 | } | 2329 | } |
2330 | } | 2330 | } |
2331 | 2331 | ||
2332 | return 0; | 2332 | return 0; |
2333 | } | 2333 | } |
2334 | 2334 | ||
2335 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, | 2335 | static int sctp_setsockopt_peer_addr_params(struct sock *sk, |
2336 | char __user *optval, | 2336 | char __user *optval, |
2337 | unsigned int optlen) | 2337 | unsigned int optlen) |
2338 | { | 2338 | { |
2339 | struct sctp_paddrparams params; | 2339 | struct sctp_paddrparams params; |
2340 | struct sctp_transport *trans = NULL; | 2340 | struct sctp_transport *trans = NULL; |
2341 | struct sctp_association *asoc = NULL; | 2341 | struct sctp_association *asoc = NULL; |
2342 | struct sctp_sock *sp = sctp_sk(sk); | 2342 | struct sctp_sock *sp = sctp_sk(sk); |
2343 | int error; | 2343 | int error; |
2344 | int hb_change, pmtud_change, sackdelay_change; | 2344 | int hb_change, pmtud_change, sackdelay_change; |
2345 | 2345 | ||
2346 | if (optlen != sizeof(struct sctp_paddrparams)) | 2346 | if (optlen != sizeof(struct sctp_paddrparams)) |
2347 | return - EINVAL; | 2347 | return - EINVAL; |
2348 | 2348 | ||
2349 | if (copy_from_user(¶ms, optval, optlen)) | 2349 | if (copy_from_user(¶ms, optval, optlen)) |
2350 | return -EFAULT; | 2350 | return -EFAULT; |
2351 | 2351 | ||
2352 | /* Validate flags and value parameters. */ | 2352 | /* Validate flags and value parameters. */ |
2353 | hb_change = params.spp_flags & SPP_HB; | 2353 | hb_change = params.spp_flags & SPP_HB; |
2354 | pmtud_change = params.spp_flags & SPP_PMTUD; | 2354 | pmtud_change = params.spp_flags & SPP_PMTUD; |
2355 | sackdelay_change = params.spp_flags & SPP_SACKDELAY; | 2355 | sackdelay_change = params.spp_flags & SPP_SACKDELAY; |
2356 | 2356 | ||
2357 | if (hb_change == SPP_HB || | 2357 | if (hb_change == SPP_HB || |
2358 | pmtud_change == SPP_PMTUD || | 2358 | pmtud_change == SPP_PMTUD || |
2359 | sackdelay_change == SPP_SACKDELAY || | 2359 | sackdelay_change == SPP_SACKDELAY || |
2360 | params.spp_sackdelay > 500 || | 2360 | params.spp_sackdelay > 500 || |
2361 | (params.spp_pathmtu && | 2361 | (params.spp_pathmtu && |
2362 | params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) | 2362 | params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT)) |
2363 | return -EINVAL; | 2363 | return -EINVAL; |
2364 | 2364 | ||
2365 | /* If an address other than INADDR_ANY is specified, and | 2365 | /* If an address other than INADDR_ANY is specified, and |
2366 | * no transport is found, then the request is invalid. | 2366 | * no transport is found, then the request is invalid. |
2367 | */ | 2367 | */ |
2368 | if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { | 2368 | if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { |
2369 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 2369 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
2370 | params.spp_assoc_id); | 2370 | params.spp_assoc_id); |
2371 | if (!trans) | 2371 | if (!trans) |
2372 | return -EINVAL; | 2372 | return -EINVAL; |
2373 | } | 2373 | } |
2374 | 2374 | ||
2375 | /* Get association, if assoc_id != 0 and the socket is a one | 2375 | /* Get association, if assoc_id != 0 and the socket is a one |
2376 | * to many style socket, and an association was not found, then | 2376 | * to many style socket, and an association was not found, then |
2377 | * the id was invalid. | 2377 | * the id was invalid. |
2378 | */ | 2378 | */ |
2379 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 2379 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
2380 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) | 2380 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) |
2381 | return -EINVAL; | 2381 | return -EINVAL; |
2382 | 2382 | ||
2383 | /* Heartbeat demand can only be sent on a transport or | 2383 | /* Heartbeat demand can only be sent on a transport or |
2384 | * association, but not a socket. | 2384 | * association, but not a socket. |
2385 | */ | 2385 | */ |
2386 | if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) | 2386 | if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc) |
2387 | return -EINVAL; | 2387 | return -EINVAL; |
2388 | 2388 | ||
2389 | /* Process parameters. */ | 2389 | /* Process parameters. */ |
2390 | error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 2390 | error = sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
2391 | hb_change, pmtud_change, | 2391 | hb_change, pmtud_change, |
2392 | sackdelay_change); | 2392 | sackdelay_change); |
2393 | 2393 | ||
2394 | if (error) | 2394 | if (error) |
2395 | return error; | 2395 | return error; |
2396 | 2396 | ||
2397 | /* If changes are for association, also apply parameters to each | 2397 | /* If changes are for association, also apply parameters to each |
2398 | * transport. | 2398 | * transport. |
2399 | */ | 2399 | */ |
2400 | if (!trans && asoc) { | 2400 | if (!trans && asoc) { |
2401 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 2401 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
2402 | transports) { | 2402 | transports) { |
2403 | sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, | 2403 | sctp_apply_peer_addr_params(¶ms, trans, asoc, sp, |
2404 | hb_change, pmtud_change, | 2404 | hb_change, pmtud_change, |
2405 | sackdelay_change); | 2405 | sackdelay_change); |
2406 | } | 2406 | } |
2407 | } | 2407 | } |
2408 | 2408 | ||
2409 | return 0; | 2409 | return 0; |
2410 | } | 2410 | } |
2411 | 2411 | ||
2412 | /* | 2412 | /* |
2413 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) | 2413 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
2414 | * | 2414 | * |
2415 | * This option will effect the way delayed acks are performed. This | 2415 | * This option will effect the way delayed acks are performed. This |
2416 | * option allows you to get or set the delayed ack time, in | 2416 | * option allows you to get or set the delayed ack time, in |
2417 | * milliseconds. It also allows changing the delayed ack frequency. | 2417 | * milliseconds. It also allows changing the delayed ack frequency. |
2418 | * Changing the frequency to 1 disables the delayed sack algorithm. If | 2418 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
2419 | * the assoc_id is 0, then this sets or gets the endpoints default | 2419 | * the assoc_id is 0, then this sets or gets the endpoints default |
2420 | * values. If the assoc_id field is non-zero, then the set or get | 2420 | * values. If the assoc_id field is non-zero, then the set or get |
2421 | * effects the specified association for the one to many model (the | 2421 | * effects the specified association for the one to many model (the |
2422 | * assoc_id field is ignored by the one to one model). Note that if | 2422 | * assoc_id field is ignored by the one to one model). Note that if |
2423 | * sack_delay or sack_freq are 0 when setting this option, then the | 2423 | * sack_delay or sack_freq are 0 when setting this option, then the |
2424 | * current values will remain unchanged. | 2424 | * current values will remain unchanged. |
2425 | * | 2425 | * |
2426 | * struct sctp_sack_info { | 2426 | * struct sctp_sack_info { |
2427 | * sctp_assoc_t sack_assoc_id; | 2427 | * sctp_assoc_t sack_assoc_id; |
2428 | * uint32_t sack_delay; | 2428 | * uint32_t sack_delay; |
2429 | * uint32_t sack_freq; | 2429 | * uint32_t sack_freq; |
2430 | * }; | 2430 | * }; |
2431 | * | 2431 | * |
2432 | * sack_assoc_id - This parameter, indicates which association the user | 2432 | * sack_assoc_id - This parameter, indicates which association the user |
2433 | * is performing an action upon. Note that if this field's value is | 2433 | * is performing an action upon. Note that if this field's value is |
2434 | * zero then the endpoints default value is changed (effecting future | 2434 | * zero then the endpoints default value is changed (effecting future |
2435 | * associations only). | 2435 | * associations only). |
2436 | * | 2436 | * |
2437 | * sack_delay - This parameter contains the number of milliseconds that | 2437 | * sack_delay - This parameter contains the number of milliseconds that |
2438 | * the user is requesting the delayed ACK timer be set to. Note that | 2438 | * the user is requesting the delayed ACK timer be set to. Note that |
2439 | * this value is defined in the standard to be between 200 and 500 | 2439 | * this value is defined in the standard to be between 200 and 500 |
2440 | * milliseconds. | 2440 | * milliseconds. |
2441 | * | 2441 | * |
2442 | * sack_freq - This parameter contains the number of packets that must | 2442 | * sack_freq - This parameter contains the number of packets that must |
2443 | * be received before a sack is sent without waiting for the delay | 2443 | * be received before a sack is sent without waiting for the delay |
2444 | * timer to expire. The default value for this is 2, setting this | 2444 | * timer to expire. The default value for this is 2, setting this |
2445 | * value to 1 will disable the delayed sack algorithm. | 2445 | * value to 1 will disable the delayed sack algorithm. |
2446 | */ | 2446 | */ |
2447 | 2447 | ||
2448 | static int sctp_setsockopt_delayed_ack(struct sock *sk, | 2448 | static int sctp_setsockopt_delayed_ack(struct sock *sk, |
2449 | char __user *optval, unsigned int optlen) | 2449 | char __user *optval, unsigned int optlen) |
2450 | { | 2450 | { |
2451 | struct sctp_sack_info params; | 2451 | struct sctp_sack_info params; |
2452 | struct sctp_transport *trans = NULL; | 2452 | struct sctp_transport *trans = NULL; |
2453 | struct sctp_association *asoc = NULL; | 2453 | struct sctp_association *asoc = NULL; |
2454 | struct sctp_sock *sp = sctp_sk(sk); | 2454 | struct sctp_sock *sp = sctp_sk(sk); |
2455 | 2455 | ||
2456 | if (optlen == sizeof(struct sctp_sack_info)) { | 2456 | if (optlen == sizeof(struct sctp_sack_info)) { |
2457 | if (copy_from_user(¶ms, optval, optlen)) | 2457 | if (copy_from_user(¶ms, optval, optlen)) |
2458 | return -EFAULT; | 2458 | return -EFAULT; |
2459 | 2459 | ||
2460 | if (params.sack_delay == 0 && params.sack_freq == 0) | 2460 | if (params.sack_delay == 0 && params.sack_freq == 0) |
2461 | return 0; | 2461 | return 0; |
2462 | } else if (optlen == sizeof(struct sctp_assoc_value)) { | 2462 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
2463 | pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); | 2463 | pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); |
2464 | pr_warn("Use struct sctp_sack_info instead\n"); | 2464 | pr_warn("Use struct sctp_sack_info instead\n"); |
2465 | if (copy_from_user(¶ms, optval, optlen)) | 2465 | if (copy_from_user(¶ms, optval, optlen)) |
2466 | return -EFAULT; | 2466 | return -EFAULT; |
2467 | 2467 | ||
2468 | if (params.sack_delay == 0) | 2468 | if (params.sack_delay == 0) |
2469 | params.sack_freq = 1; | 2469 | params.sack_freq = 1; |
2470 | else | 2470 | else |
2471 | params.sack_freq = 0; | 2471 | params.sack_freq = 0; |
2472 | } else | 2472 | } else |
2473 | return - EINVAL; | 2473 | return - EINVAL; |
2474 | 2474 | ||
2475 | /* Validate value parameter. */ | 2475 | /* Validate value parameter. */ |
2476 | if (params.sack_delay > 500) | 2476 | if (params.sack_delay > 500) |
2477 | return -EINVAL; | 2477 | return -EINVAL; |
2478 | 2478 | ||
2479 | /* Get association, if sack_assoc_id != 0 and the socket is a one | 2479 | /* Get association, if sack_assoc_id != 0 and the socket is a one |
2480 | * to many style socket, and an association was not found, then | 2480 | * to many style socket, and an association was not found, then |
2481 | * the id was invalid. | 2481 | * the id was invalid. |
2482 | */ | 2482 | */ |
2483 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 2483 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
2484 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 2484 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) |
2485 | return -EINVAL; | 2485 | return -EINVAL; |
2486 | 2486 | ||
2487 | if (params.sack_delay) { | 2487 | if (params.sack_delay) { |
2488 | if (asoc) { | 2488 | if (asoc) { |
2489 | asoc->sackdelay = | 2489 | asoc->sackdelay = |
2490 | msecs_to_jiffies(params.sack_delay); | 2490 | msecs_to_jiffies(params.sack_delay); |
2491 | asoc->param_flags = | 2491 | asoc->param_flags = |
2492 | (asoc->param_flags & ~SPP_SACKDELAY) | | 2492 | (asoc->param_flags & ~SPP_SACKDELAY) | |
2493 | SPP_SACKDELAY_ENABLE; | 2493 | SPP_SACKDELAY_ENABLE; |
2494 | } else { | 2494 | } else { |
2495 | sp->sackdelay = params.sack_delay; | 2495 | sp->sackdelay = params.sack_delay; |
2496 | sp->param_flags = | 2496 | sp->param_flags = |
2497 | (sp->param_flags & ~SPP_SACKDELAY) | | 2497 | (sp->param_flags & ~SPP_SACKDELAY) | |
2498 | SPP_SACKDELAY_ENABLE; | 2498 | SPP_SACKDELAY_ENABLE; |
2499 | } | 2499 | } |
2500 | } | 2500 | } |
2501 | 2501 | ||
2502 | if (params.sack_freq == 1) { | 2502 | if (params.sack_freq == 1) { |
2503 | if (asoc) { | 2503 | if (asoc) { |
2504 | asoc->param_flags = | 2504 | asoc->param_flags = |
2505 | (asoc->param_flags & ~SPP_SACKDELAY) | | 2505 | (asoc->param_flags & ~SPP_SACKDELAY) | |
2506 | SPP_SACKDELAY_DISABLE; | 2506 | SPP_SACKDELAY_DISABLE; |
2507 | } else { | 2507 | } else { |
2508 | sp->param_flags = | 2508 | sp->param_flags = |
2509 | (sp->param_flags & ~SPP_SACKDELAY) | | 2509 | (sp->param_flags & ~SPP_SACKDELAY) | |
2510 | SPP_SACKDELAY_DISABLE; | 2510 | SPP_SACKDELAY_DISABLE; |
2511 | } | 2511 | } |
2512 | } else if (params.sack_freq > 1) { | 2512 | } else if (params.sack_freq > 1) { |
2513 | if (asoc) { | 2513 | if (asoc) { |
2514 | asoc->sackfreq = params.sack_freq; | 2514 | asoc->sackfreq = params.sack_freq; |
2515 | asoc->param_flags = | 2515 | asoc->param_flags = |
2516 | (asoc->param_flags & ~SPP_SACKDELAY) | | 2516 | (asoc->param_flags & ~SPP_SACKDELAY) | |
2517 | SPP_SACKDELAY_ENABLE; | 2517 | SPP_SACKDELAY_ENABLE; |
2518 | } else { | 2518 | } else { |
2519 | sp->sackfreq = params.sack_freq; | 2519 | sp->sackfreq = params.sack_freq; |
2520 | sp->param_flags = | 2520 | sp->param_flags = |
2521 | (sp->param_flags & ~SPP_SACKDELAY) | | 2521 | (sp->param_flags & ~SPP_SACKDELAY) | |
2522 | SPP_SACKDELAY_ENABLE; | 2522 | SPP_SACKDELAY_ENABLE; |
2523 | } | 2523 | } |
2524 | } | 2524 | } |
2525 | 2525 | ||
2526 | /* If change is for association, also apply to each transport. */ | 2526 | /* If change is for association, also apply to each transport. */ |
2527 | if (asoc) { | 2527 | if (asoc) { |
2528 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, | 2528 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, |
2529 | transports) { | 2529 | transports) { |
2530 | if (params.sack_delay) { | 2530 | if (params.sack_delay) { |
2531 | trans->sackdelay = | 2531 | trans->sackdelay = |
2532 | msecs_to_jiffies(params.sack_delay); | 2532 | msecs_to_jiffies(params.sack_delay); |
2533 | trans->param_flags = | 2533 | trans->param_flags = |
2534 | (trans->param_flags & ~SPP_SACKDELAY) | | 2534 | (trans->param_flags & ~SPP_SACKDELAY) | |
2535 | SPP_SACKDELAY_ENABLE; | 2535 | SPP_SACKDELAY_ENABLE; |
2536 | } | 2536 | } |
2537 | if (params.sack_freq == 1) { | 2537 | if (params.sack_freq == 1) { |
2538 | trans->param_flags = | 2538 | trans->param_flags = |
2539 | (trans->param_flags & ~SPP_SACKDELAY) | | 2539 | (trans->param_flags & ~SPP_SACKDELAY) | |
2540 | SPP_SACKDELAY_DISABLE; | 2540 | SPP_SACKDELAY_DISABLE; |
2541 | } else if (params.sack_freq > 1) { | 2541 | } else if (params.sack_freq > 1) { |
2542 | trans->sackfreq = params.sack_freq; | 2542 | trans->sackfreq = params.sack_freq; |
2543 | trans->param_flags = | 2543 | trans->param_flags = |
2544 | (trans->param_flags & ~SPP_SACKDELAY) | | 2544 | (trans->param_flags & ~SPP_SACKDELAY) | |
2545 | SPP_SACKDELAY_ENABLE; | 2545 | SPP_SACKDELAY_ENABLE; |
2546 | } | 2546 | } |
2547 | } | 2547 | } |
2548 | } | 2548 | } |
2549 | 2549 | ||
2550 | return 0; | 2550 | return 0; |
2551 | } | 2551 | } |
2552 | 2552 | ||
2553 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 2553 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
2554 | * | 2554 | * |
2555 | * Applications can specify protocol parameters for the default association | 2555 | * Applications can specify protocol parameters for the default association |
2556 | * initialization. The option name argument to setsockopt() and getsockopt() | 2556 | * initialization. The option name argument to setsockopt() and getsockopt() |
2557 | * is SCTP_INITMSG. | 2557 | * is SCTP_INITMSG. |
2558 | * | 2558 | * |
2559 | * Setting initialization parameters is effective only on an unconnected | 2559 | * Setting initialization parameters is effective only on an unconnected |
2560 | * socket (for UDP-style sockets only future associations are effected | 2560 | * socket (for UDP-style sockets only future associations are effected |
2561 | * by the change). With TCP-style sockets, this option is inherited by | 2561 | * by the change). With TCP-style sockets, this option is inherited by |
2562 | * sockets derived from a listener socket. | 2562 | * sockets derived from a listener socket. |
2563 | */ | 2563 | */ |
2564 | static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen) | 2564 | static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, unsigned int optlen) |
2565 | { | 2565 | { |
2566 | struct sctp_initmsg sinit; | 2566 | struct sctp_initmsg sinit; |
2567 | struct sctp_sock *sp = sctp_sk(sk); | 2567 | struct sctp_sock *sp = sctp_sk(sk); |
2568 | 2568 | ||
2569 | if (optlen != sizeof(struct sctp_initmsg)) | 2569 | if (optlen != sizeof(struct sctp_initmsg)) |
2570 | return -EINVAL; | 2570 | return -EINVAL; |
2571 | if (copy_from_user(&sinit, optval, optlen)) | 2571 | if (copy_from_user(&sinit, optval, optlen)) |
2572 | return -EFAULT; | 2572 | return -EFAULT; |
2573 | 2573 | ||
2574 | if (sinit.sinit_num_ostreams) | 2574 | if (sinit.sinit_num_ostreams) |
2575 | sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; | 2575 | sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams; |
2576 | if (sinit.sinit_max_instreams) | 2576 | if (sinit.sinit_max_instreams) |
2577 | sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; | 2577 | sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams; |
2578 | if (sinit.sinit_max_attempts) | 2578 | if (sinit.sinit_max_attempts) |
2579 | sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; | 2579 | sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts; |
2580 | if (sinit.sinit_max_init_timeo) | 2580 | if (sinit.sinit_max_init_timeo) |
2581 | sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; | 2581 | sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo; |
2582 | 2582 | ||
2583 | return 0; | 2583 | return 0; |
2584 | } | 2584 | } |
2585 | 2585 | ||
2586 | /* | 2586 | /* |
2587 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 2587 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
2588 | * | 2588 | * |
2589 | * Applications that wish to use the sendto() system call may wish to | 2589 | * Applications that wish to use the sendto() system call may wish to |
2590 | * specify a default set of parameters that would normally be supplied | 2590 | * specify a default set of parameters that would normally be supplied |
2591 | * through the inclusion of ancillary data. This socket option allows | 2591 | * through the inclusion of ancillary data. This socket option allows |
2592 | * such an application to set the default sctp_sndrcvinfo structure. | 2592 | * such an application to set the default sctp_sndrcvinfo structure. |
2593 | * The application that wishes to use this socket option simply passes | 2593 | * The application that wishes to use this socket option simply passes |
2594 | * in to this call the sctp_sndrcvinfo structure defined in Section | 2594 | * in to this call the sctp_sndrcvinfo structure defined in Section |
2595 | * 5.2.2) The input parameters accepted by this call include | 2595 | * 5.2.2) The input parameters accepted by this call include |
2596 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 2596 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
2597 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in | 2597 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
2598 | * to this call if the caller is using the UDP model. | 2598 | * to this call if the caller is using the UDP model. |
2599 | */ | 2599 | */ |
2600 | static int sctp_setsockopt_default_send_param(struct sock *sk, | 2600 | static int sctp_setsockopt_default_send_param(struct sock *sk, |
2601 | char __user *optval, | 2601 | char __user *optval, |
2602 | unsigned int optlen) | 2602 | unsigned int optlen) |
2603 | { | 2603 | { |
2604 | struct sctp_sndrcvinfo info; | 2604 | struct sctp_sndrcvinfo info; |
2605 | struct sctp_association *asoc; | 2605 | struct sctp_association *asoc; |
2606 | struct sctp_sock *sp = sctp_sk(sk); | 2606 | struct sctp_sock *sp = sctp_sk(sk); |
2607 | 2607 | ||
2608 | if (optlen != sizeof(struct sctp_sndrcvinfo)) | 2608 | if (optlen != sizeof(struct sctp_sndrcvinfo)) |
2609 | return -EINVAL; | 2609 | return -EINVAL; |
2610 | if (copy_from_user(&info, optval, optlen)) | 2610 | if (copy_from_user(&info, optval, optlen)) |
2611 | return -EFAULT; | 2611 | return -EFAULT; |
2612 | 2612 | ||
2613 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 2613 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
2614 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 2614 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
2615 | return -EINVAL; | 2615 | return -EINVAL; |
2616 | 2616 | ||
2617 | if (asoc) { | 2617 | if (asoc) { |
2618 | asoc->default_stream = info.sinfo_stream; | 2618 | asoc->default_stream = info.sinfo_stream; |
2619 | asoc->default_flags = info.sinfo_flags; | 2619 | asoc->default_flags = info.sinfo_flags; |
2620 | asoc->default_ppid = info.sinfo_ppid; | 2620 | asoc->default_ppid = info.sinfo_ppid; |
2621 | asoc->default_context = info.sinfo_context; | 2621 | asoc->default_context = info.sinfo_context; |
2622 | asoc->default_timetolive = info.sinfo_timetolive; | 2622 | asoc->default_timetolive = info.sinfo_timetolive; |
2623 | } else { | 2623 | } else { |
2624 | sp->default_stream = info.sinfo_stream; | 2624 | sp->default_stream = info.sinfo_stream; |
2625 | sp->default_flags = info.sinfo_flags; | 2625 | sp->default_flags = info.sinfo_flags; |
2626 | sp->default_ppid = info.sinfo_ppid; | 2626 | sp->default_ppid = info.sinfo_ppid; |
2627 | sp->default_context = info.sinfo_context; | 2627 | sp->default_context = info.sinfo_context; |
2628 | sp->default_timetolive = info.sinfo_timetolive; | 2628 | sp->default_timetolive = info.sinfo_timetolive; |
2629 | } | 2629 | } |
2630 | 2630 | ||
2631 | return 0; | 2631 | return 0; |
2632 | } | 2632 | } |
2633 | 2633 | ||
2634 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 2634 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
2635 | * | 2635 | * |
2636 | * Requests that the local SCTP stack use the enclosed peer address as | 2636 | * Requests that the local SCTP stack use the enclosed peer address as |
2637 | * the association primary. The enclosed address must be one of the | 2637 | * the association primary. The enclosed address must be one of the |
2638 | * association peer's addresses. | 2638 | * association peer's addresses. |
2639 | */ | 2639 | */ |
2640 | static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, | 2640 | static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval, |
2641 | unsigned int optlen) | 2641 | unsigned int optlen) |
2642 | { | 2642 | { |
2643 | struct sctp_prim prim; | 2643 | struct sctp_prim prim; |
2644 | struct sctp_transport *trans; | 2644 | struct sctp_transport *trans; |
2645 | 2645 | ||
2646 | if (optlen != sizeof(struct sctp_prim)) | 2646 | if (optlen != sizeof(struct sctp_prim)) |
2647 | return -EINVAL; | 2647 | return -EINVAL; |
2648 | 2648 | ||
2649 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) | 2649 | if (copy_from_user(&prim, optval, sizeof(struct sctp_prim))) |
2650 | return -EFAULT; | 2650 | return -EFAULT; |
2651 | 2651 | ||
2652 | trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); | 2652 | trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id); |
2653 | if (!trans) | 2653 | if (!trans) |
2654 | return -EINVAL; | 2654 | return -EINVAL; |
2655 | 2655 | ||
2656 | sctp_assoc_set_primary(trans->asoc, trans); | 2656 | sctp_assoc_set_primary(trans->asoc, trans); |
2657 | 2657 | ||
2658 | return 0; | 2658 | return 0; |
2659 | } | 2659 | } |
2660 | 2660 | ||
2661 | /* | 2661 | /* |
2662 | * 7.1.5 SCTP_NODELAY | 2662 | * 7.1.5 SCTP_NODELAY |
2663 | * | 2663 | * |
2664 | * Turn on/off any Nagle-like algorithm. This means that packets are | 2664 | * Turn on/off any Nagle-like algorithm. This means that packets are |
2665 | * generally sent as soon as possible and no unnecessary delays are | 2665 | * generally sent as soon as possible and no unnecessary delays are |
2666 | * introduced, at the cost of more packets in the network. Expects an | 2666 | * introduced, at the cost of more packets in the network. Expects an |
2667 | * integer boolean flag. | 2667 | * integer boolean flag. |
2668 | */ | 2668 | */ |
2669 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, | 2669 | static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval, |
2670 | unsigned int optlen) | 2670 | unsigned int optlen) |
2671 | { | 2671 | { |
2672 | int val; | 2672 | int val; |
2673 | 2673 | ||
2674 | if (optlen < sizeof(int)) | 2674 | if (optlen < sizeof(int)) |
2675 | return -EINVAL; | 2675 | return -EINVAL; |
2676 | if (get_user(val, (int __user *)optval)) | 2676 | if (get_user(val, (int __user *)optval)) |
2677 | return -EFAULT; | 2677 | return -EFAULT; |
2678 | 2678 | ||
2679 | sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; | 2679 | sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1; |
2680 | return 0; | 2680 | return 0; |
2681 | } | 2681 | } |
2682 | 2682 | ||
2683 | /* | 2683 | /* |
2684 | * | 2684 | * |
2685 | * 7.1.1 SCTP_RTOINFO | 2685 | * 7.1.1 SCTP_RTOINFO |
2686 | * | 2686 | * |
2687 | * The protocol parameters used to initialize and bound retransmission | 2687 | * The protocol parameters used to initialize and bound retransmission |
2688 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 2688 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
2689 | * and modify these parameters. | 2689 | * and modify these parameters. |
2690 | * All parameters are time values, in milliseconds. A value of 0, when | 2690 | * All parameters are time values, in milliseconds. A value of 0, when |
2691 | * modifying the parameters, indicates that the current value should not | 2691 | * modifying the parameters, indicates that the current value should not |
2692 | * be changed. | 2692 | * be changed. |
2693 | * | 2693 | * |
2694 | */ | 2694 | */ |
2695 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) | 2695 | static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigned int optlen) |
2696 | { | 2696 | { |
2697 | struct sctp_rtoinfo rtoinfo; | 2697 | struct sctp_rtoinfo rtoinfo; |
2698 | struct sctp_association *asoc; | 2698 | struct sctp_association *asoc; |
2699 | 2699 | ||
2700 | if (optlen != sizeof (struct sctp_rtoinfo)) | 2700 | if (optlen != sizeof (struct sctp_rtoinfo)) |
2701 | return -EINVAL; | 2701 | return -EINVAL; |
2702 | 2702 | ||
2703 | if (copy_from_user(&rtoinfo, optval, optlen)) | 2703 | if (copy_from_user(&rtoinfo, optval, optlen)) |
2704 | return -EFAULT; | 2704 | return -EFAULT; |
2705 | 2705 | ||
2706 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 2706 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
2707 | 2707 | ||
2708 | /* Set the values to the specific association */ | 2708 | /* Set the values to the specific association */ |
2709 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 2709 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
2710 | return -EINVAL; | 2710 | return -EINVAL; |
2711 | 2711 | ||
2712 | if (asoc) { | 2712 | if (asoc) { |
2713 | if (rtoinfo.srto_initial != 0) | 2713 | if (rtoinfo.srto_initial != 0) |
2714 | asoc->rto_initial = | 2714 | asoc->rto_initial = |
2715 | msecs_to_jiffies(rtoinfo.srto_initial); | 2715 | msecs_to_jiffies(rtoinfo.srto_initial); |
2716 | if (rtoinfo.srto_max != 0) | 2716 | if (rtoinfo.srto_max != 0) |
2717 | asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); | 2717 | asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); |
2718 | if (rtoinfo.srto_min != 0) | 2718 | if (rtoinfo.srto_min != 0) |
2719 | asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min); | 2719 | asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min); |
2720 | } else { | 2720 | } else { |
2721 | /* If there is no association or the association-id = 0 | 2721 | /* If there is no association or the association-id = 0 |
2722 | * set the values to the endpoint. | 2722 | * set the values to the endpoint. |
2723 | */ | 2723 | */ |
2724 | struct sctp_sock *sp = sctp_sk(sk); | 2724 | struct sctp_sock *sp = sctp_sk(sk); |
2725 | 2725 | ||
2726 | if (rtoinfo.srto_initial != 0) | 2726 | if (rtoinfo.srto_initial != 0) |
2727 | sp->rtoinfo.srto_initial = rtoinfo.srto_initial; | 2727 | sp->rtoinfo.srto_initial = rtoinfo.srto_initial; |
2728 | if (rtoinfo.srto_max != 0) | 2728 | if (rtoinfo.srto_max != 0) |
2729 | sp->rtoinfo.srto_max = rtoinfo.srto_max; | 2729 | sp->rtoinfo.srto_max = rtoinfo.srto_max; |
2730 | if (rtoinfo.srto_min != 0) | 2730 | if (rtoinfo.srto_min != 0) |
2731 | sp->rtoinfo.srto_min = rtoinfo.srto_min; | 2731 | sp->rtoinfo.srto_min = rtoinfo.srto_min; |
2732 | } | 2732 | } |
2733 | 2733 | ||
2734 | return 0; | 2734 | return 0; |
2735 | } | 2735 | } |
2736 | 2736 | ||
2737 | /* | 2737 | /* |
2738 | * | 2738 | * |
2739 | * 7.1.2 SCTP_ASSOCINFO | 2739 | * 7.1.2 SCTP_ASSOCINFO |
2740 | * | 2740 | * |
2741 | * This option is used to tune the maximum retransmission attempts | 2741 | * This option is used to tune the maximum retransmission attempts |
2742 | * of the association. | 2742 | * of the association. |
2743 | * Returns an error if the new association retransmission value is | 2743 | * Returns an error if the new association retransmission value is |
2744 | * greater than the sum of the retransmission value of the peer. | 2744 | * greater than the sum of the retransmission value of the peer. |
2745 | * See [SCTP] for more information. | 2745 | * See [SCTP] for more information. |
2746 | * | 2746 | * |
2747 | */ | 2747 | */ |
2748 | static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen) | 2748 | static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, unsigned int optlen) |
2749 | { | 2749 | { |
2750 | 2750 | ||
2751 | struct sctp_assocparams assocparams; | 2751 | struct sctp_assocparams assocparams; |
2752 | struct sctp_association *asoc; | 2752 | struct sctp_association *asoc; |
2753 | 2753 | ||
2754 | if (optlen != sizeof(struct sctp_assocparams)) | 2754 | if (optlen != sizeof(struct sctp_assocparams)) |
2755 | return -EINVAL; | 2755 | return -EINVAL; |
2756 | if (copy_from_user(&assocparams, optval, optlen)) | 2756 | if (copy_from_user(&assocparams, optval, optlen)) |
2757 | return -EFAULT; | 2757 | return -EFAULT; |
2758 | 2758 | ||
2759 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 2759 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
2760 | 2760 | ||
2761 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 2761 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
2762 | return -EINVAL; | 2762 | return -EINVAL; |
2763 | 2763 | ||
2764 | /* Set the values to the specific association */ | 2764 | /* Set the values to the specific association */ |
2765 | if (asoc) { | 2765 | if (asoc) { |
2766 | if (assocparams.sasoc_asocmaxrxt != 0) { | 2766 | if (assocparams.sasoc_asocmaxrxt != 0) { |
2767 | __u32 path_sum = 0; | 2767 | __u32 path_sum = 0; |
2768 | int paths = 0; | 2768 | int paths = 0; |
2769 | struct sctp_transport *peer_addr; | 2769 | struct sctp_transport *peer_addr; |
2770 | 2770 | ||
2771 | list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, | 2771 | list_for_each_entry(peer_addr, &asoc->peer.transport_addr_list, |
2772 | transports) { | 2772 | transports) { |
2773 | path_sum += peer_addr->pathmaxrxt; | 2773 | path_sum += peer_addr->pathmaxrxt; |
2774 | paths++; | 2774 | paths++; |
2775 | } | 2775 | } |
2776 | 2776 | ||
2777 | /* Only validate asocmaxrxt if we have more than | 2777 | /* Only validate asocmaxrxt if we have more than |
2778 | * one path/transport. We do this because path | 2778 | * one path/transport. We do this because path |
2779 | * retransmissions are only counted when we have more | 2779 | * retransmissions are only counted when we have more |
2780 | * then one path. | 2780 | * then one path. |
2781 | */ | 2781 | */ |
2782 | if (paths > 1 && | 2782 | if (paths > 1 && |
2783 | assocparams.sasoc_asocmaxrxt > path_sum) | 2783 | assocparams.sasoc_asocmaxrxt > path_sum) |
2784 | return -EINVAL; | 2784 | return -EINVAL; |
2785 | 2785 | ||
2786 | asoc->max_retrans = assocparams.sasoc_asocmaxrxt; | 2786 | asoc->max_retrans = assocparams.sasoc_asocmaxrxt; |
2787 | } | 2787 | } |
2788 | 2788 | ||
2789 | if (assocparams.sasoc_cookie_life != 0) { | 2789 | if (assocparams.sasoc_cookie_life != 0) { |
2790 | asoc->cookie_life.tv_sec = | 2790 | asoc->cookie_life.tv_sec = |
2791 | assocparams.sasoc_cookie_life / 1000; | 2791 | assocparams.sasoc_cookie_life / 1000; |
2792 | asoc->cookie_life.tv_usec = | 2792 | asoc->cookie_life.tv_usec = |
2793 | (assocparams.sasoc_cookie_life % 1000) | 2793 | (assocparams.sasoc_cookie_life % 1000) |
2794 | * 1000; | 2794 | * 1000; |
2795 | } | 2795 | } |
2796 | } else { | 2796 | } else { |
2797 | /* Set the values to the endpoint */ | 2797 | /* Set the values to the endpoint */ |
2798 | struct sctp_sock *sp = sctp_sk(sk); | 2798 | struct sctp_sock *sp = sctp_sk(sk); |
2799 | 2799 | ||
2800 | if (assocparams.sasoc_asocmaxrxt != 0) | 2800 | if (assocparams.sasoc_asocmaxrxt != 0) |
2801 | sp->assocparams.sasoc_asocmaxrxt = | 2801 | sp->assocparams.sasoc_asocmaxrxt = |
2802 | assocparams.sasoc_asocmaxrxt; | 2802 | assocparams.sasoc_asocmaxrxt; |
2803 | if (assocparams.sasoc_cookie_life != 0) | 2803 | if (assocparams.sasoc_cookie_life != 0) |
2804 | sp->assocparams.sasoc_cookie_life = | 2804 | sp->assocparams.sasoc_cookie_life = |
2805 | assocparams.sasoc_cookie_life; | 2805 | assocparams.sasoc_cookie_life; |
2806 | } | 2806 | } |
2807 | return 0; | 2807 | return 0; |
2808 | } | 2808 | } |
2809 | 2809 | ||
2810 | /* | 2810 | /* |
2811 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 2811 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
2812 | * | 2812 | * |
2813 | * This socket option is a boolean flag which turns on or off mapped V4 | 2813 | * This socket option is a boolean flag which turns on or off mapped V4 |
2814 | * addresses. If this option is turned on and the socket is type | 2814 | * addresses. If this option is turned on and the socket is type |
2815 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 2815 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
2816 | * If this option is turned off, then no mapping will be done of V4 | 2816 | * If this option is turned off, then no mapping will be done of V4 |
2817 | * addresses and a user will receive both PF_INET6 and PF_INET type | 2817 | * addresses and a user will receive both PF_INET6 and PF_INET type |
2818 | * addresses on the socket. | 2818 | * addresses on the socket. |
2819 | */ | 2819 | */ |
2820 | static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen) | 2820 | static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, unsigned int optlen) |
2821 | { | 2821 | { |
2822 | int val; | 2822 | int val; |
2823 | struct sctp_sock *sp = sctp_sk(sk); | 2823 | struct sctp_sock *sp = sctp_sk(sk); |
2824 | 2824 | ||
2825 | if (optlen < sizeof(int)) | 2825 | if (optlen < sizeof(int)) |
2826 | return -EINVAL; | 2826 | return -EINVAL; |
2827 | if (get_user(val, (int __user *)optval)) | 2827 | if (get_user(val, (int __user *)optval)) |
2828 | return -EFAULT; | 2828 | return -EFAULT; |
2829 | if (val) | 2829 | if (val) |
2830 | sp->v4mapped = 1; | 2830 | sp->v4mapped = 1; |
2831 | else | 2831 | else |
2832 | sp->v4mapped = 0; | 2832 | sp->v4mapped = 0; |
2833 | 2833 | ||
2834 | return 0; | 2834 | return 0; |
2835 | } | 2835 | } |
2836 | 2836 | ||
2837 | /* | 2837 | /* |
2838 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 2838 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
2839 | * This option will get or set the maximum size to put in any outgoing | 2839 | * This option will get or set the maximum size to put in any outgoing |
2840 | * SCTP DATA chunk. If a message is larger than this size it will be | 2840 | * SCTP DATA chunk. If a message is larger than this size it will be |
2841 | * fragmented by SCTP into the specified size. Note that the underlying | 2841 | * fragmented by SCTP into the specified size. Note that the underlying |
2842 | * SCTP implementation may fragment into smaller sized chunks when the | 2842 | * SCTP implementation may fragment into smaller sized chunks when the |
2843 | * PMTU of the underlying association is smaller than the value set by | 2843 | * PMTU of the underlying association is smaller than the value set by |
2844 | * the user. The default value for this option is '0' which indicates | 2844 | * the user. The default value for this option is '0' which indicates |
2845 | * the user is NOT limiting fragmentation and only the PMTU will effect | 2845 | * the user is NOT limiting fragmentation and only the PMTU will effect |
2846 | * SCTP's choice of DATA chunk size. Note also that values set larger | 2846 | * SCTP's choice of DATA chunk size. Note also that values set larger |
2847 | * than the maximum size of an IP datagram will effectively let SCTP | 2847 | * than the maximum size of an IP datagram will effectively let SCTP |
2848 | * control fragmentation (i.e. the same as setting this option to 0). | 2848 | * control fragmentation (i.e. the same as setting this option to 0). |
2849 | * | 2849 | * |
2850 | * The following structure is used to access and modify this parameter: | 2850 | * The following structure is used to access and modify this parameter: |
2851 | * | 2851 | * |
2852 | * struct sctp_assoc_value { | 2852 | * struct sctp_assoc_value { |
2853 | * sctp_assoc_t assoc_id; | 2853 | * sctp_assoc_t assoc_id; |
2854 | * uint32_t assoc_value; | 2854 | * uint32_t assoc_value; |
2855 | * }; | 2855 | * }; |
2856 | * | 2856 | * |
2857 | * assoc_id: This parameter is ignored for one-to-one style sockets. | 2857 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
2858 | * For one-to-many style sockets this parameter indicates which | 2858 | * For one-to-many style sockets this parameter indicates which |
2859 | * association the user is performing an action upon. Note that if | 2859 | * association the user is performing an action upon. Note that if |
2860 | * this field's value is zero then the endpoints default value is | 2860 | * this field's value is zero then the endpoints default value is |
2861 | * changed (effecting future associations only). | 2861 | * changed (effecting future associations only). |
2862 | * assoc_value: This parameter specifies the maximum size in bytes. | 2862 | * assoc_value: This parameter specifies the maximum size in bytes. |
2863 | */ | 2863 | */ |
2864 | static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen) | 2864 | static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, unsigned int optlen) |
2865 | { | 2865 | { |
2866 | struct sctp_assoc_value params; | 2866 | struct sctp_assoc_value params; |
2867 | struct sctp_association *asoc; | 2867 | struct sctp_association *asoc; |
2868 | struct sctp_sock *sp = sctp_sk(sk); | 2868 | struct sctp_sock *sp = sctp_sk(sk); |
2869 | int val; | 2869 | int val; |
2870 | 2870 | ||
2871 | if (optlen == sizeof(int)) { | 2871 | if (optlen == sizeof(int)) { |
2872 | pr_warn("Use of int in maxseg socket option deprecated\n"); | 2872 | pr_warn("Use of int in maxseg socket option deprecated\n"); |
2873 | pr_warn("Use struct sctp_assoc_value instead\n"); | 2873 | pr_warn("Use struct sctp_assoc_value instead\n"); |
2874 | if (copy_from_user(&val, optval, optlen)) | 2874 | if (copy_from_user(&val, optval, optlen)) |
2875 | return -EFAULT; | 2875 | return -EFAULT; |
2876 | params.assoc_id = 0; | 2876 | params.assoc_id = 0; |
2877 | } else if (optlen == sizeof(struct sctp_assoc_value)) { | 2877 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
2878 | if (copy_from_user(¶ms, optval, optlen)) | 2878 | if (copy_from_user(¶ms, optval, optlen)) |
2879 | return -EFAULT; | 2879 | return -EFAULT; |
2880 | val = params.assoc_value; | 2880 | val = params.assoc_value; |
2881 | } else | 2881 | } else |
2882 | return -EINVAL; | 2882 | return -EINVAL; |
2883 | 2883 | ||
2884 | if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))) | 2884 | if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN))) |
2885 | return -EINVAL; | 2885 | return -EINVAL; |
2886 | 2886 | ||
2887 | asoc = sctp_id2assoc(sk, params.assoc_id); | 2887 | asoc = sctp_id2assoc(sk, params.assoc_id); |
2888 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) | 2888 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) |
2889 | return -EINVAL; | 2889 | return -EINVAL; |
2890 | 2890 | ||
2891 | if (asoc) { | 2891 | if (asoc) { |
2892 | if (val == 0) { | 2892 | if (val == 0) { |
2893 | val = asoc->pathmtu; | 2893 | val = asoc->pathmtu; |
2894 | val -= sp->pf->af->net_header_len; | 2894 | val -= sp->pf->af->net_header_len; |
2895 | val -= sizeof(struct sctphdr) + | 2895 | val -= sizeof(struct sctphdr) + |
2896 | sizeof(struct sctp_data_chunk); | 2896 | sizeof(struct sctp_data_chunk); |
2897 | } | 2897 | } |
2898 | asoc->user_frag = val; | 2898 | asoc->user_frag = val; |
2899 | asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu); | 2899 | asoc->frag_point = sctp_frag_point(asoc, asoc->pathmtu); |
2900 | } else { | 2900 | } else { |
2901 | sp->user_frag = val; | 2901 | sp->user_frag = val; |
2902 | } | 2902 | } |
2903 | 2903 | ||
2904 | return 0; | 2904 | return 0; |
2905 | } | 2905 | } |
2906 | 2906 | ||
2907 | 2907 | ||
2908 | /* | 2908 | /* |
2909 | * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) | 2909 | * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR) |
2910 | * | 2910 | * |
2911 | * Requests that the peer mark the enclosed address as the association | 2911 | * Requests that the peer mark the enclosed address as the association |
2912 | * primary. The enclosed address must be one of the association's | 2912 | * primary. The enclosed address must be one of the association's |
2913 | * locally bound addresses. The following structure is used to make a | 2913 | * locally bound addresses. The following structure is used to make a |
2914 | * set primary request: | 2914 | * set primary request: |
2915 | */ | 2915 | */ |
2916 | static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, | 2916 | static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval, |
2917 | unsigned int optlen) | 2917 | unsigned int optlen) |
2918 | { | 2918 | { |
2919 | struct sctp_sock *sp; | 2919 | struct sctp_sock *sp; |
2920 | struct sctp_endpoint *ep; | 2920 | struct sctp_endpoint *ep; |
2921 | struct sctp_association *asoc = NULL; | 2921 | struct sctp_association *asoc = NULL; |
2922 | struct sctp_setpeerprim prim; | 2922 | struct sctp_setpeerprim prim; |
2923 | struct sctp_chunk *chunk; | 2923 | struct sctp_chunk *chunk; |
2924 | int err; | 2924 | int err; |
2925 | 2925 | ||
2926 | sp = sctp_sk(sk); | 2926 | sp = sctp_sk(sk); |
2927 | ep = sp->ep; | 2927 | ep = sp->ep; |
2928 | 2928 | ||
2929 | if (!sctp_addip_enable) | 2929 | if (!sctp_addip_enable) |
2930 | return -EPERM; | 2930 | return -EPERM; |
2931 | 2931 | ||
2932 | if (optlen != sizeof(struct sctp_setpeerprim)) | 2932 | if (optlen != sizeof(struct sctp_setpeerprim)) |
2933 | return -EINVAL; | 2933 | return -EINVAL; |
2934 | 2934 | ||
2935 | if (copy_from_user(&prim, optval, optlen)) | 2935 | if (copy_from_user(&prim, optval, optlen)) |
2936 | return -EFAULT; | 2936 | return -EFAULT; |
2937 | 2937 | ||
2938 | asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); | 2938 | asoc = sctp_id2assoc(sk, prim.sspp_assoc_id); |
2939 | if (!asoc) | 2939 | if (!asoc) |
2940 | return -EINVAL; | 2940 | return -EINVAL; |
2941 | 2941 | ||
2942 | if (!asoc->peer.asconf_capable) | 2942 | if (!asoc->peer.asconf_capable) |
2943 | return -EPERM; | 2943 | return -EPERM; |
2944 | 2944 | ||
2945 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) | 2945 | if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY) |
2946 | return -EPERM; | 2946 | return -EPERM; |
2947 | 2947 | ||
2948 | if (!sctp_state(asoc, ESTABLISHED)) | 2948 | if (!sctp_state(asoc, ESTABLISHED)) |
2949 | return -ENOTCONN; | 2949 | return -ENOTCONN; |
2950 | 2950 | ||
2951 | if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) | 2951 | if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr)) |
2952 | return -EADDRNOTAVAIL; | 2952 | return -EADDRNOTAVAIL; |
2953 | 2953 | ||
2954 | /* Create an ASCONF chunk with SET_PRIMARY parameter */ | 2954 | /* Create an ASCONF chunk with SET_PRIMARY parameter */ |
2955 | chunk = sctp_make_asconf_set_prim(asoc, | 2955 | chunk = sctp_make_asconf_set_prim(asoc, |
2956 | (union sctp_addr *)&prim.sspp_addr); | 2956 | (union sctp_addr *)&prim.sspp_addr); |
2957 | if (!chunk) | 2957 | if (!chunk) |
2958 | return -ENOMEM; | 2958 | return -ENOMEM; |
2959 | 2959 | ||
2960 | err = sctp_send_asconf(asoc, chunk); | 2960 | err = sctp_send_asconf(asoc, chunk); |
2961 | 2961 | ||
2962 | SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n"); | 2962 | SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n"); |
2963 | 2963 | ||
2964 | return err; | 2964 | return err; |
2965 | } | 2965 | } |
2966 | 2966 | ||
2967 | static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, | 2967 | static int sctp_setsockopt_adaptation_layer(struct sock *sk, char __user *optval, |
2968 | unsigned int optlen) | 2968 | unsigned int optlen) |
2969 | { | 2969 | { |
2970 | struct sctp_setadaptation adaptation; | 2970 | struct sctp_setadaptation adaptation; |
2971 | 2971 | ||
2972 | if (optlen != sizeof(struct sctp_setadaptation)) | 2972 | if (optlen != sizeof(struct sctp_setadaptation)) |
2973 | return -EINVAL; | 2973 | return -EINVAL; |
2974 | if (copy_from_user(&adaptation, optval, optlen)) | 2974 | if (copy_from_user(&adaptation, optval, optlen)) |
2975 | return -EFAULT; | 2975 | return -EFAULT; |
2976 | 2976 | ||
2977 | sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; | 2977 | sctp_sk(sk)->adaptation_ind = adaptation.ssb_adaptation_ind; |
2978 | 2978 | ||
2979 | return 0; | 2979 | return 0; |
2980 | } | 2980 | } |
2981 | 2981 | ||
2982 | /* | 2982 | /* |
2983 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) | 2983 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
2984 | * | 2984 | * |
2985 | * The context field in the sctp_sndrcvinfo structure is normally only | 2985 | * The context field in the sctp_sndrcvinfo structure is normally only |
2986 | * used when a failed message is retrieved holding the value that was | 2986 | * used when a failed message is retrieved holding the value that was |
2987 | * sent down on the actual send call. This option allows the setting of | 2987 | * sent down on the actual send call. This option allows the setting of |
2988 | * a default context on an association basis that will be received on | 2988 | * a default context on an association basis that will be received on |
2989 | * reading messages from the peer. This is especially helpful in the | 2989 | * reading messages from the peer. This is especially helpful in the |
2990 | * one-2-many model for an application to keep some reference to an | 2990 | * one-2-many model for an application to keep some reference to an |
2991 | * internal state machine that is processing messages on the | 2991 | * internal state machine that is processing messages on the |
2992 | * association. Note that the setting of this value only effects | 2992 | * association. Note that the setting of this value only effects |
2993 | * received messages from the peer and does not effect the value that is | 2993 | * received messages from the peer and does not effect the value that is |
2994 | * saved with outbound messages. | 2994 | * saved with outbound messages. |
2995 | */ | 2995 | */ |
2996 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, | 2996 | static int sctp_setsockopt_context(struct sock *sk, char __user *optval, |
2997 | unsigned int optlen) | 2997 | unsigned int optlen) |
2998 | { | 2998 | { |
2999 | struct sctp_assoc_value params; | 2999 | struct sctp_assoc_value params; |
3000 | struct sctp_sock *sp; | 3000 | struct sctp_sock *sp; |
3001 | struct sctp_association *asoc; | 3001 | struct sctp_association *asoc; |
3002 | 3002 | ||
3003 | if (optlen != sizeof(struct sctp_assoc_value)) | 3003 | if (optlen != sizeof(struct sctp_assoc_value)) |
3004 | return -EINVAL; | 3004 | return -EINVAL; |
3005 | if (copy_from_user(¶ms, optval, optlen)) | 3005 | if (copy_from_user(¶ms, optval, optlen)) |
3006 | return -EFAULT; | 3006 | return -EFAULT; |
3007 | 3007 | ||
3008 | sp = sctp_sk(sk); | 3008 | sp = sctp_sk(sk); |
3009 | 3009 | ||
3010 | if (params.assoc_id != 0) { | 3010 | if (params.assoc_id != 0) { |
3011 | asoc = sctp_id2assoc(sk, params.assoc_id); | 3011 | asoc = sctp_id2assoc(sk, params.assoc_id); |
3012 | if (!asoc) | 3012 | if (!asoc) |
3013 | return -EINVAL; | 3013 | return -EINVAL; |
3014 | asoc->default_rcv_context = params.assoc_value; | 3014 | asoc->default_rcv_context = params.assoc_value; |
3015 | } else { | 3015 | } else { |
3016 | sp->default_rcv_context = params.assoc_value; | 3016 | sp->default_rcv_context = params.assoc_value; |
3017 | } | 3017 | } |
3018 | 3018 | ||
3019 | return 0; | 3019 | return 0; |
3020 | } | 3020 | } |
3021 | 3021 | ||
3022 | /* | 3022 | /* |
3023 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 3023 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
3024 | * | 3024 | * |
3025 | * This options will at a minimum specify if the implementation is doing | 3025 | * This options will at a minimum specify if the implementation is doing |
3026 | * fragmented interleave. Fragmented interleave, for a one to many | 3026 | * fragmented interleave. Fragmented interleave, for a one to many |
3027 | * socket, is when subsequent calls to receive a message may return | 3027 | * socket, is when subsequent calls to receive a message may return |
3028 | * parts of messages from different associations. Some implementations | 3028 | * parts of messages from different associations. Some implementations |
3029 | * may allow you to turn this value on or off. If so, when turned off, | 3029 | * may allow you to turn this value on or off. If so, when turned off, |
3030 | * no fragment interleave will occur (which will cause a head of line | 3030 | * no fragment interleave will occur (which will cause a head of line |
3031 | * blocking amongst multiple associations sharing the same one to many | 3031 | * blocking amongst multiple associations sharing the same one to many |
3032 | * socket). When this option is turned on, then each receive call may | 3032 | * socket). When this option is turned on, then each receive call may |
3033 | * come from a different association (thus the user must receive data | 3033 | * come from a different association (thus the user must receive data |
3034 | * with the extended calls (e.g. sctp_recvmsg) to keep track of which | 3034 | * with the extended calls (e.g. sctp_recvmsg) to keep track of which |
3035 | * association each receive belongs to. | 3035 | * association each receive belongs to. |
3036 | * | 3036 | * |
3037 | * This option takes a boolean value. A non-zero value indicates that | 3037 | * This option takes a boolean value. A non-zero value indicates that |
3038 | * fragmented interleave is on. A value of zero indicates that | 3038 | * fragmented interleave is on. A value of zero indicates that |
3039 | * fragmented interleave is off. | 3039 | * fragmented interleave is off. |
3040 | * | 3040 | * |
3041 | * Note that it is important that an implementation that allows this | 3041 | * Note that it is important that an implementation that allows this |
3042 | * option to be turned on, have it off by default. Otherwise an unaware | 3042 | * option to be turned on, have it off by default. Otherwise an unaware |
3043 | * application using the one to many model may become confused and act | 3043 | * application using the one to many model may become confused and act |
3044 | * incorrectly. | 3044 | * incorrectly. |
3045 | */ | 3045 | */ |
3046 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, | 3046 | static int sctp_setsockopt_fragment_interleave(struct sock *sk, |
3047 | char __user *optval, | 3047 | char __user *optval, |
3048 | unsigned int optlen) | 3048 | unsigned int optlen) |
3049 | { | 3049 | { |
3050 | int val; | 3050 | int val; |
3051 | 3051 | ||
3052 | if (optlen != sizeof(int)) | 3052 | if (optlen != sizeof(int)) |
3053 | return -EINVAL; | 3053 | return -EINVAL; |
3054 | if (get_user(val, (int __user *)optval)) | 3054 | if (get_user(val, (int __user *)optval)) |
3055 | return -EFAULT; | 3055 | return -EFAULT; |
3056 | 3056 | ||
3057 | sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1; | 3057 | sctp_sk(sk)->frag_interleave = (val == 0) ? 0 : 1; |
3058 | 3058 | ||
3059 | return 0; | 3059 | return 0; |
3060 | } | 3060 | } |
3061 | 3061 | ||
3062 | /* | 3062 | /* |
3063 | * 8.1.21. Set or Get the SCTP Partial Delivery Point | 3063 | * 8.1.21. Set or Get the SCTP Partial Delivery Point |
3064 | * (SCTP_PARTIAL_DELIVERY_POINT) | 3064 | * (SCTP_PARTIAL_DELIVERY_POINT) |
3065 | * | 3065 | * |
3066 | * This option will set or get the SCTP partial delivery point. This | 3066 | * This option will set or get the SCTP partial delivery point. This |
3067 | * point is the size of a message where the partial delivery API will be | 3067 | * point is the size of a message where the partial delivery API will be |
3068 | * invoked to help free up rwnd space for the peer. Setting this to a | 3068 | * invoked to help free up rwnd space for the peer. Setting this to a |
3069 | * lower value will cause partial deliveries to happen more often. The | 3069 | * lower value will cause partial deliveries to happen more often. The |
3070 | * calls argument is an integer that sets or gets the partial delivery | 3070 | * calls argument is an integer that sets or gets the partial delivery |
3071 | * point. Note also that the call will fail if the user attempts to set | 3071 | * point. Note also that the call will fail if the user attempts to set |
3072 | * this value larger than the socket receive buffer size. | 3072 | * this value larger than the socket receive buffer size. |
3073 | * | 3073 | * |
3074 | * Note that any single message having a length smaller than or equal to | 3074 | * Note that any single message having a length smaller than or equal to |
3075 | * the SCTP partial delivery point will be delivered in one single read | 3075 | * the SCTP partial delivery point will be delivered in one single read |
3076 | * call as long as the user provided buffer is large enough to hold the | 3076 | * call as long as the user provided buffer is large enough to hold the |
3077 | * message. | 3077 | * message. |
3078 | */ | 3078 | */ |
3079 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, | 3079 | static int sctp_setsockopt_partial_delivery_point(struct sock *sk, |
3080 | char __user *optval, | 3080 | char __user *optval, |
3081 | unsigned int optlen) | 3081 | unsigned int optlen) |
3082 | { | 3082 | { |
3083 | u32 val; | 3083 | u32 val; |
3084 | 3084 | ||
3085 | if (optlen != sizeof(u32)) | 3085 | if (optlen != sizeof(u32)) |
3086 | return -EINVAL; | 3086 | return -EINVAL; |
3087 | if (get_user(val, (int __user *)optval)) | 3087 | if (get_user(val, (int __user *)optval)) |
3088 | return -EFAULT; | 3088 | return -EFAULT; |
3089 | 3089 | ||
3090 | /* Note: We double the receive buffer from what the user sets | 3090 | /* Note: We double the receive buffer from what the user sets |
3091 | * it to be, also initial rwnd is based on rcvbuf/2. | 3091 | * it to be, also initial rwnd is based on rcvbuf/2. |
3092 | */ | 3092 | */ |
3093 | if (val > (sk->sk_rcvbuf >> 1)) | 3093 | if (val > (sk->sk_rcvbuf >> 1)) |
3094 | return -EINVAL; | 3094 | return -EINVAL; |
3095 | 3095 | ||
3096 | sctp_sk(sk)->pd_point = val; | 3096 | sctp_sk(sk)->pd_point = val; |
3097 | 3097 | ||
3098 | return 0; /* is this the right error code? */ | 3098 | return 0; /* is this the right error code? */ |
3099 | } | 3099 | } |
3100 | 3100 | ||
3101 | /* | 3101 | /* |
3102 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) | 3102 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
3103 | * | 3103 | * |
3104 | * This option will allow a user to change the maximum burst of packets | 3104 | * This option will allow a user to change the maximum burst of packets |
3105 | * that can be emitted by this association. Note that the default value | 3105 | * that can be emitted by this association. Note that the default value |
3106 | * is 4, and some implementations may restrict this setting so that it | 3106 | * is 4, and some implementations may restrict this setting so that it |
3107 | * can only be lowered. | 3107 | * can only be lowered. |
3108 | * | 3108 | * |
3109 | * NOTE: This text doesn't seem right. Do this on a socket basis with | 3109 | * NOTE: This text doesn't seem right. Do this on a socket basis with |
3110 | * future associations inheriting the socket value. | 3110 | * future associations inheriting the socket value. |
3111 | */ | 3111 | */ |
3112 | static int sctp_setsockopt_maxburst(struct sock *sk, | 3112 | static int sctp_setsockopt_maxburst(struct sock *sk, |
3113 | char __user *optval, | 3113 | char __user *optval, |
3114 | unsigned int optlen) | 3114 | unsigned int optlen) |
3115 | { | 3115 | { |
3116 | struct sctp_assoc_value params; | 3116 | struct sctp_assoc_value params; |
3117 | struct sctp_sock *sp; | 3117 | struct sctp_sock *sp; |
3118 | struct sctp_association *asoc; | 3118 | struct sctp_association *asoc; |
3119 | int val; | 3119 | int val; |
3120 | int assoc_id = 0; | 3120 | int assoc_id = 0; |
3121 | 3121 | ||
3122 | if (optlen == sizeof(int)) { | 3122 | if (optlen == sizeof(int)) { |
3123 | pr_warn("Use of int in max_burst socket option deprecated\n"); | 3123 | pr_warn("Use of int in max_burst socket option deprecated\n"); |
3124 | pr_warn("Use struct sctp_assoc_value instead\n"); | 3124 | pr_warn("Use struct sctp_assoc_value instead\n"); |
3125 | if (copy_from_user(&val, optval, optlen)) | 3125 | if (copy_from_user(&val, optval, optlen)) |
3126 | return -EFAULT; | 3126 | return -EFAULT; |
3127 | } else if (optlen == sizeof(struct sctp_assoc_value)) { | 3127 | } else if (optlen == sizeof(struct sctp_assoc_value)) { |
3128 | if (copy_from_user(¶ms, optval, optlen)) | 3128 | if (copy_from_user(¶ms, optval, optlen)) |
3129 | return -EFAULT; | 3129 | return -EFAULT; |
3130 | val = params.assoc_value; | 3130 | val = params.assoc_value; |
3131 | assoc_id = params.assoc_id; | 3131 | assoc_id = params.assoc_id; |
3132 | } else | 3132 | } else |
3133 | return -EINVAL; | 3133 | return -EINVAL; |
3134 | 3134 | ||
3135 | sp = sctp_sk(sk); | 3135 | sp = sctp_sk(sk); |
3136 | 3136 | ||
3137 | if (assoc_id != 0) { | 3137 | if (assoc_id != 0) { |
3138 | asoc = sctp_id2assoc(sk, assoc_id); | 3138 | asoc = sctp_id2assoc(sk, assoc_id); |
3139 | if (!asoc) | 3139 | if (!asoc) |
3140 | return -EINVAL; | 3140 | return -EINVAL; |
3141 | asoc->max_burst = val; | 3141 | asoc->max_burst = val; |
3142 | } else | 3142 | } else |
3143 | sp->max_burst = val; | 3143 | sp->max_burst = val; |
3144 | 3144 | ||
3145 | return 0; | 3145 | return 0; |
3146 | } | 3146 | } |
3147 | 3147 | ||
3148 | /* | 3148 | /* |
3149 | * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) | 3149 | * 7.1.18. Add a chunk that must be authenticated (SCTP_AUTH_CHUNK) |
3150 | * | 3150 | * |
3151 | * This set option adds a chunk type that the user is requesting to be | 3151 | * This set option adds a chunk type that the user is requesting to be |
3152 | * received only in an authenticated way. Changes to the list of chunks | 3152 | * received only in an authenticated way. Changes to the list of chunks |
3153 | * will only effect future associations on the socket. | 3153 | * will only effect future associations on the socket. |
3154 | */ | 3154 | */ |
3155 | static int sctp_setsockopt_auth_chunk(struct sock *sk, | 3155 | static int sctp_setsockopt_auth_chunk(struct sock *sk, |
3156 | char __user *optval, | 3156 | char __user *optval, |
3157 | unsigned int optlen) | 3157 | unsigned int optlen) |
3158 | { | 3158 | { |
3159 | struct sctp_authchunk val; | 3159 | struct sctp_authchunk val; |
3160 | 3160 | ||
3161 | if (!sctp_auth_enable) | 3161 | if (!sctp_auth_enable) |
3162 | return -EACCES; | 3162 | return -EACCES; |
3163 | 3163 | ||
3164 | if (optlen != sizeof(struct sctp_authchunk)) | 3164 | if (optlen != sizeof(struct sctp_authchunk)) |
3165 | return -EINVAL; | 3165 | return -EINVAL; |
3166 | if (copy_from_user(&val, optval, optlen)) | 3166 | if (copy_from_user(&val, optval, optlen)) |
3167 | return -EFAULT; | 3167 | return -EFAULT; |
3168 | 3168 | ||
3169 | switch (val.sauth_chunk) { | 3169 | switch (val.sauth_chunk) { |
3170 | case SCTP_CID_INIT: | 3170 | case SCTP_CID_INIT: |
3171 | case SCTP_CID_INIT_ACK: | 3171 | case SCTP_CID_INIT_ACK: |
3172 | case SCTP_CID_SHUTDOWN_COMPLETE: | 3172 | case SCTP_CID_SHUTDOWN_COMPLETE: |
3173 | case SCTP_CID_AUTH: | 3173 | case SCTP_CID_AUTH: |
3174 | return -EINVAL; | 3174 | return -EINVAL; |
3175 | } | 3175 | } |
3176 | 3176 | ||
3177 | /* add this chunk id to the endpoint */ | 3177 | /* add this chunk id to the endpoint */ |
3178 | return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk); | 3178 | return sctp_auth_ep_add_chunkid(sctp_sk(sk)->ep, val.sauth_chunk); |
3179 | } | 3179 | } |
3180 | 3180 | ||
3181 | /* | 3181 | /* |
3182 | * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) | 3182 | * 7.1.19. Get or set the list of supported HMAC Identifiers (SCTP_HMAC_IDENT) |
3183 | * | 3183 | * |
3184 | * This option gets or sets the list of HMAC algorithms that the local | 3184 | * This option gets or sets the list of HMAC algorithms that the local |
3185 | * endpoint requires the peer to use. | 3185 | * endpoint requires the peer to use. |
3186 | */ | 3186 | */ |
3187 | static int sctp_setsockopt_hmac_ident(struct sock *sk, | 3187 | static int sctp_setsockopt_hmac_ident(struct sock *sk, |
3188 | char __user *optval, | 3188 | char __user *optval, |
3189 | unsigned int optlen) | 3189 | unsigned int optlen) |
3190 | { | 3190 | { |
3191 | struct sctp_hmacalgo *hmacs; | 3191 | struct sctp_hmacalgo *hmacs; |
3192 | u32 idents; | 3192 | u32 idents; |
3193 | int err; | 3193 | int err; |
3194 | 3194 | ||
3195 | if (!sctp_auth_enable) | 3195 | if (!sctp_auth_enable) |
3196 | return -EACCES; | 3196 | return -EACCES; |
3197 | 3197 | ||
3198 | if (optlen < sizeof(struct sctp_hmacalgo)) | 3198 | if (optlen < sizeof(struct sctp_hmacalgo)) |
3199 | return -EINVAL; | 3199 | return -EINVAL; |
3200 | 3200 | ||
3201 | hmacs = kmalloc(optlen, GFP_KERNEL); | 3201 | hmacs = kmalloc(optlen, GFP_KERNEL); |
3202 | if (!hmacs) | 3202 | if (!hmacs) |
3203 | return -ENOMEM; | 3203 | return -ENOMEM; |
3204 | 3204 | ||
3205 | if (copy_from_user(hmacs, optval, optlen)) { | 3205 | if (copy_from_user(hmacs, optval, optlen)) { |
3206 | err = -EFAULT; | 3206 | err = -EFAULT; |
3207 | goto out; | 3207 | goto out; |
3208 | } | 3208 | } |
3209 | 3209 | ||
3210 | idents = hmacs->shmac_num_idents; | 3210 | idents = hmacs->shmac_num_idents; |
3211 | if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || | 3211 | if (idents == 0 || idents > SCTP_AUTH_NUM_HMACS || |
3212 | (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { | 3212 | (idents * sizeof(u16)) > (optlen - sizeof(struct sctp_hmacalgo))) { |
3213 | err = -EINVAL; | 3213 | err = -EINVAL; |
3214 | goto out; | 3214 | goto out; |
3215 | } | 3215 | } |
3216 | 3216 | ||
3217 | err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs); | 3217 | err = sctp_auth_ep_set_hmacs(sctp_sk(sk)->ep, hmacs); |
3218 | out: | 3218 | out: |
3219 | kfree(hmacs); | 3219 | kfree(hmacs); |
3220 | return err; | 3220 | return err; |
3221 | } | 3221 | } |
3222 | 3222 | ||
3223 | /* | 3223 | /* |
3224 | * 7.1.20. Set a shared key (SCTP_AUTH_KEY) | 3224 | * 7.1.20. Set a shared key (SCTP_AUTH_KEY) |
3225 | * | 3225 | * |
3226 | * This option will set a shared secret key which is used to build an | 3226 | * This option will set a shared secret key which is used to build an |
3227 | * association shared key. | 3227 | * association shared key. |
3228 | */ | 3228 | */ |
3229 | static int sctp_setsockopt_auth_key(struct sock *sk, | 3229 | static int sctp_setsockopt_auth_key(struct sock *sk, |
3230 | char __user *optval, | 3230 | char __user *optval, |
3231 | unsigned int optlen) | 3231 | unsigned int optlen) |
3232 | { | 3232 | { |
3233 | struct sctp_authkey *authkey; | 3233 | struct sctp_authkey *authkey; |
3234 | struct sctp_association *asoc; | 3234 | struct sctp_association *asoc; |
3235 | int ret; | 3235 | int ret; |
3236 | 3236 | ||
3237 | if (!sctp_auth_enable) | 3237 | if (!sctp_auth_enable) |
3238 | return -EACCES; | 3238 | return -EACCES; |
3239 | 3239 | ||
3240 | if (optlen <= sizeof(struct sctp_authkey)) | 3240 | if (optlen <= sizeof(struct sctp_authkey)) |
3241 | return -EINVAL; | 3241 | return -EINVAL; |
3242 | 3242 | ||
3243 | authkey = kmalloc(optlen, GFP_KERNEL); | 3243 | authkey = kmalloc(optlen, GFP_KERNEL); |
3244 | if (!authkey) | 3244 | if (!authkey) |
3245 | return -ENOMEM; | 3245 | return -ENOMEM; |
3246 | 3246 | ||
3247 | if (copy_from_user(authkey, optval, optlen)) { | 3247 | if (copy_from_user(authkey, optval, optlen)) { |
3248 | ret = -EFAULT; | 3248 | ret = -EFAULT; |
3249 | goto out; | 3249 | goto out; |
3250 | } | 3250 | } |
3251 | 3251 | ||
3252 | if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { | 3252 | if (authkey->sca_keylength > optlen - sizeof(struct sctp_authkey)) { |
3253 | ret = -EINVAL; | 3253 | ret = -EINVAL; |
3254 | goto out; | 3254 | goto out; |
3255 | } | 3255 | } |
3256 | 3256 | ||
3257 | asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); | 3257 | asoc = sctp_id2assoc(sk, authkey->sca_assoc_id); |
3258 | if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { | 3258 | if (!asoc && authkey->sca_assoc_id && sctp_style(sk, UDP)) { |
3259 | ret = -EINVAL; | 3259 | ret = -EINVAL; |
3260 | goto out; | 3260 | goto out; |
3261 | } | 3261 | } |
3262 | 3262 | ||
3263 | ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey); | 3263 | ret = sctp_auth_set_key(sctp_sk(sk)->ep, asoc, authkey); |
3264 | out: | 3264 | out: |
3265 | kfree(authkey); | 3265 | kfree(authkey); |
3266 | return ret; | 3266 | return ret; |
3267 | } | 3267 | } |
3268 | 3268 | ||
3269 | /* | 3269 | /* |
3270 | * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) | 3270 | * 7.1.21. Get or set the active shared key (SCTP_AUTH_ACTIVE_KEY) |
3271 | * | 3271 | * |
3272 | * This option will get or set the active shared key to be used to build | 3272 | * This option will get or set the active shared key to be used to build |
3273 | * the association shared key. | 3273 | * the association shared key. |
3274 | */ | 3274 | */ |
3275 | static int sctp_setsockopt_active_key(struct sock *sk, | 3275 | static int sctp_setsockopt_active_key(struct sock *sk, |
3276 | char __user *optval, | 3276 | char __user *optval, |
3277 | unsigned int optlen) | 3277 | unsigned int optlen) |
3278 | { | 3278 | { |
3279 | struct sctp_authkeyid val; | 3279 | struct sctp_authkeyid val; |
3280 | struct sctp_association *asoc; | 3280 | struct sctp_association *asoc; |
3281 | 3281 | ||
3282 | if (!sctp_auth_enable) | 3282 | if (!sctp_auth_enable) |
3283 | return -EACCES; | 3283 | return -EACCES; |
3284 | 3284 | ||
3285 | if (optlen != sizeof(struct sctp_authkeyid)) | 3285 | if (optlen != sizeof(struct sctp_authkeyid)) |
3286 | return -EINVAL; | 3286 | return -EINVAL; |
3287 | if (copy_from_user(&val, optval, optlen)) | 3287 | if (copy_from_user(&val, optval, optlen)) |
3288 | return -EFAULT; | 3288 | return -EFAULT; |
3289 | 3289 | ||
3290 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 3290 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
3291 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 3291 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
3292 | return -EINVAL; | 3292 | return -EINVAL; |
3293 | 3293 | ||
3294 | return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc, | 3294 | return sctp_auth_set_active_key(sctp_sk(sk)->ep, asoc, |
3295 | val.scact_keynumber); | 3295 | val.scact_keynumber); |
3296 | } | 3296 | } |
3297 | 3297 | ||
3298 | /* | 3298 | /* |
3299 | * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) | 3299 | * 7.1.22. Delete a shared key (SCTP_AUTH_DELETE_KEY) |
3300 | * | 3300 | * |
3301 | * This set option will delete a shared secret key from use. | 3301 | * This set option will delete a shared secret key from use. |
3302 | */ | 3302 | */ |
3303 | static int sctp_setsockopt_del_key(struct sock *sk, | 3303 | static int sctp_setsockopt_del_key(struct sock *sk, |
3304 | char __user *optval, | 3304 | char __user *optval, |
3305 | unsigned int optlen) | 3305 | unsigned int optlen) |
3306 | { | 3306 | { |
3307 | struct sctp_authkeyid val; | 3307 | struct sctp_authkeyid val; |
3308 | struct sctp_association *asoc; | 3308 | struct sctp_association *asoc; |
3309 | 3309 | ||
3310 | if (!sctp_auth_enable) | 3310 | if (!sctp_auth_enable) |
3311 | return -EACCES; | 3311 | return -EACCES; |
3312 | 3312 | ||
3313 | if (optlen != sizeof(struct sctp_authkeyid)) | 3313 | if (optlen != sizeof(struct sctp_authkeyid)) |
3314 | return -EINVAL; | 3314 | return -EINVAL; |
3315 | if (copy_from_user(&val, optval, optlen)) | 3315 | if (copy_from_user(&val, optval, optlen)) |
3316 | return -EFAULT; | 3316 | return -EFAULT; |
3317 | 3317 | ||
3318 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 3318 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
3319 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 3319 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
3320 | return -EINVAL; | 3320 | return -EINVAL; |
3321 | 3321 | ||
3322 | return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc, | 3322 | return sctp_auth_del_key_id(sctp_sk(sk)->ep, asoc, |
3323 | val.scact_keynumber); | 3323 | val.scact_keynumber); |
3324 | 3324 | ||
3325 | } | 3325 | } |
3326 | 3326 | ||
3327 | 3327 | ||
3328 | /* API 6.2 setsockopt(), getsockopt() | 3328 | /* API 6.2 setsockopt(), getsockopt() |
3329 | * | 3329 | * |
3330 | * Applications use setsockopt() and getsockopt() to set or retrieve | 3330 | * Applications use setsockopt() and getsockopt() to set or retrieve |
3331 | * socket options. Socket options are used to change the default | 3331 | * socket options. Socket options are used to change the default |
3332 | * behavior of sockets calls. They are described in Section 7. | 3332 | * behavior of sockets calls. They are described in Section 7. |
3333 | * | 3333 | * |
3334 | * The syntax is: | 3334 | * The syntax is: |
3335 | * | 3335 | * |
3336 | * ret = getsockopt(int sd, int level, int optname, void __user *optval, | 3336 | * ret = getsockopt(int sd, int level, int optname, void __user *optval, |
3337 | * int __user *optlen); | 3337 | * int __user *optlen); |
3338 | * ret = setsockopt(int sd, int level, int optname, const void __user *optval, | 3338 | * ret = setsockopt(int sd, int level, int optname, const void __user *optval, |
3339 | * int optlen); | 3339 | * int optlen); |
3340 | * | 3340 | * |
3341 | * sd - the socket descript. | 3341 | * sd - the socket descript. |
3342 | * level - set to IPPROTO_SCTP for all SCTP options. | 3342 | * level - set to IPPROTO_SCTP for all SCTP options. |
3343 | * optname - the option name. | 3343 | * optname - the option name. |
3344 | * optval - the buffer to store the value of the option. | 3344 | * optval - the buffer to store the value of the option. |
3345 | * optlen - the size of the buffer. | 3345 | * optlen - the size of the buffer. |
3346 | */ | 3346 | */ |
3347 | SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname, | 3347 | SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname, |
3348 | char __user *optval, unsigned int optlen) | 3348 | char __user *optval, unsigned int optlen) |
3349 | { | 3349 | { |
3350 | int retval = 0; | 3350 | int retval = 0; |
3351 | 3351 | ||
3352 | SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n", | 3352 | SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n", |
3353 | sk, optname); | 3353 | sk, optname); |
3354 | 3354 | ||
3355 | /* I can hardly begin to describe how wrong this is. This is | 3355 | /* I can hardly begin to describe how wrong this is. This is |
3356 | * so broken as to be worse than useless. The API draft | 3356 | * so broken as to be worse than useless. The API draft |
3357 | * REALLY is NOT helpful here... I am not convinced that the | 3357 | * REALLY is NOT helpful here... I am not convinced that the |
3358 | * semantics of setsockopt() with a level OTHER THAN SOL_SCTP | 3358 | * semantics of setsockopt() with a level OTHER THAN SOL_SCTP |
3359 | * are at all well-founded. | 3359 | * are at all well-founded. |
3360 | */ | 3360 | */ |
3361 | if (level != SOL_SCTP) { | 3361 | if (level != SOL_SCTP) { |
3362 | struct sctp_af *af = sctp_sk(sk)->pf->af; | 3362 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
3363 | retval = af->setsockopt(sk, level, optname, optval, optlen); | 3363 | retval = af->setsockopt(sk, level, optname, optval, optlen); |
3364 | goto out_nounlock; | 3364 | goto out_nounlock; |
3365 | } | 3365 | } |
3366 | 3366 | ||
3367 | sctp_lock_sock(sk); | 3367 | sctp_lock_sock(sk); |
3368 | 3368 | ||
3369 | switch (optname) { | 3369 | switch (optname) { |
3370 | case SCTP_SOCKOPT_BINDX_ADD: | 3370 | case SCTP_SOCKOPT_BINDX_ADD: |
3371 | /* 'optlen' is the size of the addresses buffer. */ | 3371 | /* 'optlen' is the size of the addresses buffer. */ |
3372 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 3372 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
3373 | optlen, SCTP_BINDX_ADD_ADDR); | 3373 | optlen, SCTP_BINDX_ADD_ADDR); |
3374 | break; | 3374 | break; |
3375 | 3375 | ||
3376 | case SCTP_SOCKOPT_BINDX_REM: | 3376 | case SCTP_SOCKOPT_BINDX_REM: |
3377 | /* 'optlen' is the size of the addresses buffer. */ | 3377 | /* 'optlen' is the size of the addresses buffer. */ |
3378 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, | 3378 | retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval, |
3379 | optlen, SCTP_BINDX_REM_ADDR); | 3379 | optlen, SCTP_BINDX_REM_ADDR); |
3380 | break; | 3380 | break; |
3381 | 3381 | ||
3382 | case SCTP_SOCKOPT_CONNECTX_OLD: | 3382 | case SCTP_SOCKOPT_CONNECTX_OLD: |
3383 | /* 'optlen' is the size of the addresses buffer. */ | 3383 | /* 'optlen' is the size of the addresses buffer. */ |
3384 | retval = sctp_setsockopt_connectx_old(sk, | 3384 | retval = sctp_setsockopt_connectx_old(sk, |
3385 | (struct sockaddr __user *)optval, | 3385 | (struct sockaddr __user *)optval, |
3386 | optlen); | 3386 | optlen); |
3387 | break; | 3387 | break; |
3388 | 3388 | ||
3389 | case SCTP_SOCKOPT_CONNECTX: | 3389 | case SCTP_SOCKOPT_CONNECTX: |
3390 | /* 'optlen' is the size of the addresses buffer. */ | 3390 | /* 'optlen' is the size of the addresses buffer. */ |
3391 | retval = sctp_setsockopt_connectx(sk, | 3391 | retval = sctp_setsockopt_connectx(sk, |
3392 | (struct sockaddr __user *)optval, | 3392 | (struct sockaddr __user *)optval, |
3393 | optlen); | 3393 | optlen); |
3394 | break; | 3394 | break; |
3395 | 3395 | ||
3396 | case SCTP_DISABLE_FRAGMENTS: | 3396 | case SCTP_DISABLE_FRAGMENTS: |
3397 | retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); | 3397 | retval = sctp_setsockopt_disable_fragments(sk, optval, optlen); |
3398 | break; | 3398 | break; |
3399 | 3399 | ||
3400 | case SCTP_EVENTS: | 3400 | case SCTP_EVENTS: |
3401 | retval = sctp_setsockopt_events(sk, optval, optlen); | 3401 | retval = sctp_setsockopt_events(sk, optval, optlen); |
3402 | break; | 3402 | break; |
3403 | 3403 | ||
3404 | case SCTP_AUTOCLOSE: | 3404 | case SCTP_AUTOCLOSE: |
3405 | retval = sctp_setsockopt_autoclose(sk, optval, optlen); | 3405 | retval = sctp_setsockopt_autoclose(sk, optval, optlen); |
3406 | break; | 3406 | break; |
3407 | 3407 | ||
3408 | case SCTP_PEER_ADDR_PARAMS: | 3408 | case SCTP_PEER_ADDR_PARAMS: |
3409 | retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); | 3409 | retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen); |
3410 | break; | 3410 | break; |
3411 | 3411 | ||
3412 | case SCTP_DELAYED_ACK: | 3412 | case SCTP_DELAYED_ACK: |
3413 | retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); | 3413 | retval = sctp_setsockopt_delayed_ack(sk, optval, optlen); |
3414 | break; | 3414 | break; |
3415 | case SCTP_PARTIAL_DELIVERY_POINT: | 3415 | case SCTP_PARTIAL_DELIVERY_POINT: |
3416 | retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); | 3416 | retval = sctp_setsockopt_partial_delivery_point(sk, optval, optlen); |
3417 | break; | 3417 | break; |
3418 | 3418 | ||
3419 | case SCTP_INITMSG: | 3419 | case SCTP_INITMSG: |
3420 | retval = sctp_setsockopt_initmsg(sk, optval, optlen); | 3420 | retval = sctp_setsockopt_initmsg(sk, optval, optlen); |
3421 | break; | 3421 | break; |
3422 | case SCTP_DEFAULT_SEND_PARAM: | 3422 | case SCTP_DEFAULT_SEND_PARAM: |
3423 | retval = sctp_setsockopt_default_send_param(sk, optval, | 3423 | retval = sctp_setsockopt_default_send_param(sk, optval, |
3424 | optlen); | 3424 | optlen); |
3425 | break; | 3425 | break; |
3426 | case SCTP_PRIMARY_ADDR: | 3426 | case SCTP_PRIMARY_ADDR: |
3427 | retval = sctp_setsockopt_primary_addr(sk, optval, optlen); | 3427 | retval = sctp_setsockopt_primary_addr(sk, optval, optlen); |
3428 | break; | 3428 | break; |
3429 | case SCTP_SET_PEER_PRIMARY_ADDR: | 3429 | case SCTP_SET_PEER_PRIMARY_ADDR: |
3430 | retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); | 3430 | retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen); |
3431 | break; | 3431 | break; |
3432 | case SCTP_NODELAY: | 3432 | case SCTP_NODELAY: |
3433 | retval = sctp_setsockopt_nodelay(sk, optval, optlen); | 3433 | retval = sctp_setsockopt_nodelay(sk, optval, optlen); |
3434 | break; | 3434 | break; |
3435 | case SCTP_RTOINFO: | 3435 | case SCTP_RTOINFO: |
3436 | retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); | 3436 | retval = sctp_setsockopt_rtoinfo(sk, optval, optlen); |
3437 | break; | 3437 | break; |
3438 | case SCTP_ASSOCINFO: | 3438 | case SCTP_ASSOCINFO: |
3439 | retval = sctp_setsockopt_associnfo(sk, optval, optlen); | 3439 | retval = sctp_setsockopt_associnfo(sk, optval, optlen); |
3440 | break; | 3440 | break; |
3441 | case SCTP_I_WANT_MAPPED_V4_ADDR: | 3441 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
3442 | retval = sctp_setsockopt_mappedv4(sk, optval, optlen); | 3442 | retval = sctp_setsockopt_mappedv4(sk, optval, optlen); |
3443 | break; | 3443 | break; |
3444 | case SCTP_MAXSEG: | 3444 | case SCTP_MAXSEG: |
3445 | retval = sctp_setsockopt_maxseg(sk, optval, optlen); | 3445 | retval = sctp_setsockopt_maxseg(sk, optval, optlen); |
3446 | break; | 3446 | break; |
3447 | case SCTP_ADAPTATION_LAYER: | 3447 | case SCTP_ADAPTATION_LAYER: |
3448 | retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); | 3448 | retval = sctp_setsockopt_adaptation_layer(sk, optval, optlen); |
3449 | break; | 3449 | break; |
3450 | case SCTP_CONTEXT: | 3450 | case SCTP_CONTEXT: |
3451 | retval = sctp_setsockopt_context(sk, optval, optlen); | 3451 | retval = sctp_setsockopt_context(sk, optval, optlen); |
3452 | break; | 3452 | break; |
3453 | case SCTP_FRAGMENT_INTERLEAVE: | 3453 | case SCTP_FRAGMENT_INTERLEAVE: |
3454 | retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); | 3454 | retval = sctp_setsockopt_fragment_interleave(sk, optval, optlen); |
3455 | break; | 3455 | break; |
3456 | case SCTP_MAX_BURST: | 3456 | case SCTP_MAX_BURST: |
3457 | retval = sctp_setsockopt_maxburst(sk, optval, optlen); | 3457 | retval = sctp_setsockopt_maxburst(sk, optval, optlen); |
3458 | break; | 3458 | break; |
3459 | case SCTP_AUTH_CHUNK: | 3459 | case SCTP_AUTH_CHUNK: |
3460 | retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); | 3460 | retval = sctp_setsockopt_auth_chunk(sk, optval, optlen); |
3461 | break; | 3461 | break; |
3462 | case SCTP_HMAC_IDENT: | 3462 | case SCTP_HMAC_IDENT: |
3463 | retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); | 3463 | retval = sctp_setsockopt_hmac_ident(sk, optval, optlen); |
3464 | break; | 3464 | break; |
3465 | case SCTP_AUTH_KEY: | 3465 | case SCTP_AUTH_KEY: |
3466 | retval = sctp_setsockopt_auth_key(sk, optval, optlen); | 3466 | retval = sctp_setsockopt_auth_key(sk, optval, optlen); |
3467 | break; | 3467 | break; |
3468 | case SCTP_AUTH_ACTIVE_KEY: | 3468 | case SCTP_AUTH_ACTIVE_KEY: |
3469 | retval = sctp_setsockopt_active_key(sk, optval, optlen); | 3469 | retval = sctp_setsockopt_active_key(sk, optval, optlen); |
3470 | break; | 3470 | break; |
3471 | case SCTP_AUTH_DELETE_KEY: | 3471 | case SCTP_AUTH_DELETE_KEY: |
3472 | retval = sctp_setsockopt_del_key(sk, optval, optlen); | 3472 | retval = sctp_setsockopt_del_key(sk, optval, optlen); |
3473 | break; | 3473 | break; |
3474 | default: | 3474 | default: |
3475 | retval = -ENOPROTOOPT; | 3475 | retval = -ENOPROTOOPT; |
3476 | break; | 3476 | break; |
3477 | } | 3477 | } |
3478 | 3478 | ||
3479 | sctp_release_sock(sk); | 3479 | sctp_release_sock(sk); |
3480 | 3480 | ||
3481 | out_nounlock: | 3481 | out_nounlock: |
3482 | return retval; | 3482 | return retval; |
3483 | } | 3483 | } |
3484 | 3484 | ||
3485 | /* API 3.1.6 connect() - UDP Style Syntax | 3485 | /* API 3.1.6 connect() - UDP Style Syntax |
3486 | * | 3486 | * |
3487 | * An application may use the connect() call in the UDP model to initiate an | 3487 | * An application may use the connect() call in the UDP model to initiate an |
3488 | * association without sending data. | 3488 | * association without sending data. |
3489 | * | 3489 | * |
3490 | * The syntax is: | 3490 | * The syntax is: |
3491 | * | 3491 | * |
3492 | * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); | 3492 | * ret = connect(int sd, const struct sockaddr *nam, socklen_t len); |
3493 | * | 3493 | * |
3494 | * sd: the socket descriptor to have a new association added to. | 3494 | * sd: the socket descriptor to have a new association added to. |
3495 | * | 3495 | * |
3496 | * nam: the address structure (either struct sockaddr_in or struct | 3496 | * nam: the address structure (either struct sockaddr_in or struct |
3497 | * sockaddr_in6 defined in RFC2553 [7]). | 3497 | * sockaddr_in6 defined in RFC2553 [7]). |
3498 | * | 3498 | * |
3499 | * len: the size of the address. | 3499 | * len: the size of the address. |
3500 | */ | 3500 | */ |
3501 | SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr, | 3501 | SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr, |
3502 | int addr_len) | 3502 | int addr_len) |
3503 | { | 3503 | { |
3504 | int err = 0; | 3504 | int err = 0; |
3505 | struct sctp_af *af; | 3505 | struct sctp_af *af; |
3506 | 3506 | ||
3507 | sctp_lock_sock(sk); | 3507 | sctp_lock_sock(sk); |
3508 | 3508 | ||
3509 | SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n", | 3509 | SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n", |
3510 | __func__, sk, addr, addr_len); | 3510 | __func__, sk, addr, addr_len); |
3511 | 3511 | ||
3512 | /* Validate addr_len before calling common connect/connectx routine. */ | 3512 | /* Validate addr_len before calling common connect/connectx routine. */ |
3513 | af = sctp_get_af_specific(addr->sa_family); | 3513 | af = sctp_get_af_specific(addr->sa_family); |
3514 | if (!af || addr_len < af->sockaddr_len) { | 3514 | if (!af || addr_len < af->sockaddr_len) { |
3515 | err = -EINVAL; | 3515 | err = -EINVAL; |
3516 | } else { | 3516 | } else { |
3517 | /* Pass correct addr len to common routine (so it knows there | 3517 | /* Pass correct addr len to common routine (so it knows there |
3518 | * is only one address being passed. | 3518 | * is only one address being passed. |
3519 | */ | 3519 | */ |
3520 | err = __sctp_connect(sk, addr, af->sockaddr_len, NULL); | 3520 | err = __sctp_connect(sk, addr, af->sockaddr_len, NULL); |
3521 | } | 3521 | } |
3522 | 3522 | ||
3523 | sctp_release_sock(sk); | 3523 | sctp_release_sock(sk); |
3524 | return err; | 3524 | return err; |
3525 | } | 3525 | } |
3526 | 3526 | ||
3527 | /* FIXME: Write comments. */ | 3527 | /* FIXME: Write comments. */ |
3528 | SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags) | 3528 | SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags) |
3529 | { | 3529 | { |
3530 | return -EOPNOTSUPP; /* STUB */ | 3530 | return -EOPNOTSUPP; /* STUB */ |
3531 | } | 3531 | } |
3532 | 3532 | ||
3533 | /* 4.1.4 accept() - TCP Style Syntax | 3533 | /* 4.1.4 accept() - TCP Style Syntax |
3534 | * | 3534 | * |
3535 | * Applications use accept() call to remove an established SCTP | 3535 | * Applications use accept() call to remove an established SCTP |
3536 | * association from the accept queue of the endpoint. A new socket | 3536 | * association from the accept queue of the endpoint. A new socket |
3537 | * descriptor will be returned from accept() to represent the newly | 3537 | * descriptor will be returned from accept() to represent the newly |
3538 | * formed association. | 3538 | * formed association. |
3539 | */ | 3539 | */ |
3540 | SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err) | 3540 | SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err) |
3541 | { | 3541 | { |
3542 | struct sctp_sock *sp; | 3542 | struct sctp_sock *sp; |
3543 | struct sctp_endpoint *ep; | 3543 | struct sctp_endpoint *ep; |
3544 | struct sock *newsk = NULL; | 3544 | struct sock *newsk = NULL; |
3545 | struct sctp_association *asoc; | 3545 | struct sctp_association *asoc; |
3546 | long timeo; | 3546 | long timeo; |
3547 | int error = 0; | 3547 | int error = 0; |
3548 | 3548 | ||
3549 | sctp_lock_sock(sk); | 3549 | sctp_lock_sock(sk); |
3550 | 3550 | ||
3551 | sp = sctp_sk(sk); | 3551 | sp = sctp_sk(sk); |
3552 | ep = sp->ep; | 3552 | ep = sp->ep; |
3553 | 3553 | ||
3554 | if (!sctp_style(sk, TCP)) { | 3554 | if (!sctp_style(sk, TCP)) { |
3555 | error = -EOPNOTSUPP; | 3555 | error = -EOPNOTSUPP; |
3556 | goto out; | 3556 | goto out; |
3557 | } | 3557 | } |
3558 | 3558 | ||
3559 | if (!sctp_sstate(sk, LISTENING)) { | 3559 | if (!sctp_sstate(sk, LISTENING)) { |
3560 | error = -EINVAL; | 3560 | error = -EINVAL; |
3561 | goto out; | 3561 | goto out; |
3562 | } | 3562 | } |
3563 | 3563 | ||
3564 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); | 3564 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
3565 | 3565 | ||
3566 | error = sctp_wait_for_accept(sk, timeo); | 3566 | error = sctp_wait_for_accept(sk, timeo); |
3567 | if (error) | 3567 | if (error) |
3568 | goto out; | 3568 | goto out; |
3569 | 3569 | ||
3570 | /* We treat the list of associations on the endpoint as the accept | 3570 | /* We treat the list of associations on the endpoint as the accept |
3571 | * queue and pick the first association on the list. | 3571 | * queue and pick the first association on the list. |
3572 | */ | 3572 | */ |
3573 | asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); | 3573 | asoc = list_entry(ep->asocs.next, struct sctp_association, asocs); |
3574 | 3574 | ||
3575 | newsk = sp->pf->create_accept_sk(sk, asoc); | 3575 | newsk = sp->pf->create_accept_sk(sk, asoc); |
3576 | if (!newsk) { | 3576 | if (!newsk) { |
3577 | error = -ENOMEM; | 3577 | error = -ENOMEM; |
3578 | goto out; | 3578 | goto out; |
3579 | } | 3579 | } |
3580 | 3580 | ||
3581 | /* Populate the fields of the newsk from the oldsk and migrate the | 3581 | /* Populate the fields of the newsk from the oldsk and migrate the |
3582 | * asoc to the newsk. | 3582 | * asoc to the newsk. |
3583 | */ | 3583 | */ |
3584 | sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); | 3584 | sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP); |
3585 | 3585 | ||
3586 | out: | 3586 | out: |
3587 | sctp_release_sock(sk); | 3587 | sctp_release_sock(sk); |
3588 | *err = error; | 3588 | *err = error; |
3589 | return newsk; | 3589 | return newsk; |
3590 | } | 3590 | } |
3591 | 3591 | ||
3592 | /* The SCTP ioctl handler. */ | 3592 | /* The SCTP ioctl handler. */ |
3593 | SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) | 3593 | SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg) |
3594 | { | 3594 | { |
3595 | return -ENOIOCTLCMD; | 3595 | return -ENOIOCTLCMD; |
3596 | } | 3596 | } |
3597 | 3597 | ||
3598 | /* This is the function which gets called during socket creation to | 3598 | /* This is the function which gets called during socket creation to |
3599 | * initialized the SCTP-specific portion of the sock. | 3599 | * initialized the SCTP-specific portion of the sock. |
3600 | * The sock structure should already be zero-filled memory. | 3600 | * The sock structure should already be zero-filled memory. |
3601 | */ | 3601 | */ |
3602 | SCTP_STATIC int sctp_init_sock(struct sock *sk) | 3602 | SCTP_STATIC int sctp_init_sock(struct sock *sk) |
3603 | { | 3603 | { |
3604 | struct sctp_endpoint *ep; | 3604 | struct sctp_endpoint *ep; |
3605 | struct sctp_sock *sp; | 3605 | struct sctp_sock *sp; |
3606 | 3606 | ||
3607 | SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk); | 3607 | SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk); |
3608 | 3608 | ||
3609 | sp = sctp_sk(sk); | 3609 | sp = sctp_sk(sk); |
3610 | 3610 | ||
3611 | /* Initialize the SCTP per socket area. */ | 3611 | /* Initialize the SCTP per socket area. */ |
3612 | switch (sk->sk_type) { | 3612 | switch (sk->sk_type) { |
3613 | case SOCK_SEQPACKET: | 3613 | case SOCK_SEQPACKET: |
3614 | sp->type = SCTP_SOCKET_UDP; | 3614 | sp->type = SCTP_SOCKET_UDP; |
3615 | break; | 3615 | break; |
3616 | case SOCK_STREAM: | 3616 | case SOCK_STREAM: |
3617 | sp->type = SCTP_SOCKET_TCP; | 3617 | sp->type = SCTP_SOCKET_TCP; |
3618 | break; | 3618 | break; |
3619 | default: | 3619 | default: |
3620 | return -ESOCKTNOSUPPORT; | 3620 | return -ESOCKTNOSUPPORT; |
3621 | } | 3621 | } |
3622 | 3622 | ||
3623 | /* Initialize default send parameters. These parameters can be | 3623 | /* Initialize default send parameters. These parameters can be |
3624 | * modified with the SCTP_DEFAULT_SEND_PARAM socket option. | 3624 | * modified with the SCTP_DEFAULT_SEND_PARAM socket option. |
3625 | */ | 3625 | */ |
3626 | sp->default_stream = 0; | 3626 | sp->default_stream = 0; |
3627 | sp->default_ppid = 0; | 3627 | sp->default_ppid = 0; |
3628 | sp->default_flags = 0; | 3628 | sp->default_flags = 0; |
3629 | sp->default_context = 0; | 3629 | sp->default_context = 0; |
3630 | sp->default_timetolive = 0; | 3630 | sp->default_timetolive = 0; |
3631 | 3631 | ||
3632 | sp->default_rcv_context = 0; | 3632 | sp->default_rcv_context = 0; |
3633 | sp->max_burst = sctp_max_burst; | 3633 | sp->max_burst = sctp_max_burst; |
3634 | 3634 | ||
3635 | /* Initialize default setup parameters. These parameters | 3635 | /* Initialize default setup parameters. These parameters |
3636 | * can be modified with the SCTP_INITMSG socket option or | 3636 | * can be modified with the SCTP_INITMSG socket option or |
3637 | * overridden by the SCTP_INIT CMSG. | 3637 | * overridden by the SCTP_INIT CMSG. |
3638 | */ | 3638 | */ |
3639 | sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; | 3639 | sp->initmsg.sinit_num_ostreams = sctp_max_outstreams; |
3640 | sp->initmsg.sinit_max_instreams = sctp_max_instreams; | 3640 | sp->initmsg.sinit_max_instreams = sctp_max_instreams; |
3641 | sp->initmsg.sinit_max_attempts = sctp_max_retrans_init; | 3641 | sp->initmsg.sinit_max_attempts = sctp_max_retrans_init; |
3642 | sp->initmsg.sinit_max_init_timeo = sctp_rto_max; | 3642 | sp->initmsg.sinit_max_init_timeo = sctp_rto_max; |
3643 | 3643 | ||
3644 | /* Initialize default RTO related parameters. These parameters can | 3644 | /* Initialize default RTO related parameters. These parameters can |
3645 | * be modified for with the SCTP_RTOINFO socket option. | 3645 | * be modified for with the SCTP_RTOINFO socket option. |
3646 | */ | 3646 | */ |
3647 | sp->rtoinfo.srto_initial = sctp_rto_initial; | 3647 | sp->rtoinfo.srto_initial = sctp_rto_initial; |
3648 | sp->rtoinfo.srto_max = sctp_rto_max; | 3648 | sp->rtoinfo.srto_max = sctp_rto_max; |
3649 | sp->rtoinfo.srto_min = sctp_rto_min; | 3649 | sp->rtoinfo.srto_min = sctp_rto_min; |
3650 | 3650 | ||
3651 | /* Initialize default association related parameters. These parameters | 3651 | /* Initialize default association related parameters. These parameters |
3652 | * can be modified with the SCTP_ASSOCINFO socket option. | 3652 | * can be modified with the SCTP_ASSOCINFO socket option. |
3653 | */ | 3653 | */ |
3654 | sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association; | 3654 | sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association; |
3655 | sp->assocparams.sasoc_number_peer_destinations = 0; | 3655 | sp->assocparams.sasoc_number_peer_destinations = 0; |
3656 | sp->assocparams.sasoc_peer_rwnd = 0; | 3656 | sp->assocparams.sasoc_peer_rwnd = 0; |
3657 | sp->assocparams.sasoc_local_rwnd = 0; | 3657 | sp->assocparams.sasoc_local_rwnd = 0; |
3658 | sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life; | 3658 | sp->assocparams.sasoc_cookie_life = sctp_valid_cookie_life; |
3659 | 3659 | ||
3660 | /* Initialize default event subscriptions. By default, all the | 3660 | /* Initialize default event subscriptions. By default, all the |
3661 | * options are off. | 3661 | * options are off. |
3662 | */ | 3662 | */ |
3663 | memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); | 3663 | memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe)); |
3664 | 3664 | ||
3665 | /* Default Peer Address Parameters. These defaults can | 3665 | /* Default Peer Address Parameters. These defaults can |
3666 | * be modified via SCTP_PEER_ADDR_PARAMS | 3666 | * be modified via SCTP_PEER_ADDR_PARAMS |
3667 | */ | 3667 | */ |
3668 | sp->hbinterval = sctp_hb_interval; | 3668 | sp->hbinterval = sctp_hb_interval; |
3669 | sp->pathmaxrxt = sctp_max_retrans_path; | 3669 | sp->pathmaxrxt = sctp_max_retrans_path; |
3670 | sp->pathmtu = 0; // allow default discovery | 3670 | sp->pathmtu = 0; // allow default discovery |
3671 | sp->sackdelay = sctp_sack_timeout; | 3671 | sp->sackdelay = sctp_sack_timeout; |
3672 | sp->sackfreq = 2; | 3672 | sp->sackfreq = 2; |
3673 | sp->param_flags = SPP_HB_ENABLE | | 3673 | sp->param_flags = SPP_HB_ENABLE | |
3674 | SPP_PMTUD_ENABLE | | 3674 | SPP_PMTUD_ENABLE | |
3675 | SPP_SACKDELAY_ENABLE; | 3675 | SPP_SACKDELAY_ENABLE; |
3676 | 3676 | ||
3677 | /* If enabled no SCTP message fragmentation will be performed. | 3677 | /* If enabled no SCTP message fragmentation will be performed. |
3678 | * Configure through SCTP_DISABLE_FRAGMENTS socket option. | 3678 | * Configure through SCTP_DISABLE_FRAGMENTS socket option. |
3679 | */ | 3679 | */ |
3680 | sp->disable_fragments = 0; | 3680 | sp->disable_fragments = 0; |
3681 | 3681 | ||
3682 | /* Enable Nagle algorithm by default. */ | 3682 | /* Enable Nagle algorithm by default. */ |
3683 | sp->nodelay = 0; | 3683 | sp->nodelay = 0; |
3684 | 3684 | ||
3685 | /* Enable by default. */ | 3685 | /* Enable by default. */ |
3686 | sp->v4mapped = 1; | 3686 | sp->v4mapped = 1; |
3687 | 3687 | ||
3688 | /* Auto-close idle associations after the configured | 3688 | /* Auto-close idle associations after the configured |
3689 | * number of seconds. A value of 0 disables this | 3689 | * number of seconds. A value of 0 disables this |
3690 | * feature. Configure through the SCTP_AUTOCLOSE socket option, | 3690 | * feature. Configure through the SCTP_AUTOCLOSE socket option, |
3691 | * for UDP-style sockets only. | 3691 | * for UDP-style sockets only. |
3692 | */ | 3692 | */ |
3693 | sp->autoclose = 0; | 3693 | sp->autoclose = 0; |
3694 | 3694 | ||
3695 | /* User specified fragmentation limit. */ | 3695 | /* User specified fragmentation limit. */ |
3696 | sp->user_frag = 0; | 3696 | sp->user_frag = 0; |
3697 | 3697 | ||
3698 | sp->adaptation_ind = 0; | 3698 | sp->adaptation_ind = 0; |
3699 | 3699 | ||
3700 | sp->pf = sctp_get_pf_specific(sk->sk_family); | 3700 | sp->pf = sctp_get_pf_specific(sk->sk_family); |
3701 | 3701 | ||
3702 | /* Control variables for partial data delivery. */ | 3702 | /* Control variables for partial data delivery. */ |
3703 | atomic_set(&sp->pd_mode, 0); | 3703 | atomic_set(&sp->pd_mode, 0); |
3704 | skb_queue_head_init(&sp->pd_lobby); | 3704 | skb_queue_head_init(&sp->pd_lobby); |
3705 | sp->frag_interleave = 0; | 3705 | sp->frag_interleave = 0; |
3706 | 3706 | ||
3707 | /* Create a per socket endpoint structure. Even if we | 3707 | /* Create a per socket endpoint structure. Even if we |
3708 | * change the data structure relationships, this may still | 3708 | * change the data structure relationships, this may still |
3709 | * be useful for storing pre-connect address information. | 3709 | * be useful for storing pre-connect address information. |
3710 | */ | 3710 | */ |
3711 | ep = sctp_endpoint_new(sk, GFP_KERNEL); | 3711 | ep = sctp_endpoint_new(sk, GFP_KERNEL); |
3712 | if (!ep) | 3712 | if (!ep) |
3713 | return -ENOMEM; | 3713 | return -ENOMEM; |
3714 | 3714 | ||
3715 | sp->ep = ep; | 3715 | sp->ep = ep; |
3716 | sp->hmac = NULL; | 3716 | sp->hmac = NULL; |
3717 | 3717 | ||
3718 | SCTP_DBG_OBJCNT_INC(sock); | 3718 | SCTP_DBG_OBJCNT_INC(sock); |
3719 | 3719 | ||
3720 | local_bh_disable(); | 3720 | local_bh_disable(); |
3721 | percpu_counter_inc(&sctp_sockets_allocated); | 3721 | percpu_counter_inc(&sctp_sockets_allocated); |
3722 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 3722 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
3723 | local_bh_enable(); | 3723 | local_bh_enable(); |
3724 | 3724 | ||
3725 | return 0; | 3725 | return 0; |
3726 | } | 3726 | } |
3727 | 3727 | ||
3728 | /* Cleanup any SCTP per socket resources. */ | 3728 | /* Cleanup any SCTP per socket resources. */ |
3729 | SCTP_STATIC void sctp_destroy_sock(struct sock *sk) | 3729 | SCTP_STATIC void sctp_destroy_sock(struct sock *sk) |
3730 | { | 3730 | { |
3731 | struct sctp_endpoint *ep; | 3731 | struct sctp_endpoint *ep; |
3732 | 3732 | ||
3733 | SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk); | 3733 | SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk); |
3734 | 3734 | ||
3735 | /* Release our hold on the endpoint. */ | 3735 | /* Release our hold on the endpoint. */ |
3736 | ep = sctp_sk(sk)->ep; | 3736 | ep = sctp_sk(sk)->ep; |
3737 | sctp_endpoint_free(ep); | 3737 | sctp_endpoint_free(ep); |
3738 | local_bh_disable(); | 3738 | local_bh_disable(); |
3739 | percpu_counter_dec(&sctp_sockets_allocated); | 3739 | percpu_counter_dec(&sctp_sockets_allocated); |
3740 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 3740 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
3741 | local_bh_enable(); | 3741 | local_bh_enable(); |
3742 | } | 3742 | } |
3743 | 3743 | ||
3744 | /* API 4.1.7 shutdown() - TCP Style Syntax | 3744 | /* API 4.1.7 shutdown() - TCP Style Syntax |
3745 | * int shutdown(int socket, int how); | 3745 | * int shutdown(int socket, int how); |
3746 | * | 3746 | * |
3747 | * sd - the socket descriptor of the association to be closed. | 3747 | * sd - the socket descriptor of the association to be closed. |
3748 | * how - Specifies the type of shutdown. The values are | 3748 | * how - Specifies the type of shutdown. The values are |
3749 | * as follows: | 3749 | * as follows: |
3750 | * SHUT_RD | 3750 | * SHUT_RD |
3751 | * Disables further receive operations. No SCTP | 3751 | * Disables further receive operations. No SCTP |
3752 | * protocol action is taken. | 3752 | * protocol action is taken. |
3753 | * SHUT_WR | 3753 | * SHUT_WR |
3754 | * Disables further send operations, and initiates | 3754 | * Disables further send operations, and initiates |
3755 | * the SCTP shutdown sequence. | 3755 | * the SCTP shutdown sequence. |
3756 | * SHUT_RDWR | 3756 | * SHUT_RDWR |
3757 | * Disables further send and receive operations | 3757 | * Disables further send and receive operations |
3758 | * and initiates the SCTP shutdown sequence. | 3758 | * and initiates the SCTP shutdown sequence. |
3759 | */ | 3759 | */ |
3760 | SCTP_STATIC void sctp_shutdown(struct sock *sk, int how) | 3760 | SCTP_STATIC void sctp_shutdown(struct sock *sk, int how) |
3761 | { | 3761 | { |
3762 | struct sctp_endpoint *ep; | 3762 | struct sctp_endpoint *ep; |
3763 | struct sctp_association *asoc; | 3763 | struct sctp_association *asoc; |
3764 | 3764 | ||
3765 | if (!sctp_style(sk, TCP)) | 3765 | if (!sctp_style(sk, TCP)) |
3766 | return; | 3766 | return; |
3767 | 3767 | ||
3768 | if (how & SEND_SHUTDOWN) { | 3768 | if (how & SEND_SHUTDOWN) { |
3769 | ep = sctp_sk(sk)->ep; | 3769 | ep = sctp_sk(sk)->ep; |
3770 | if (!list_empty(&ep->asocs)) { | 3770 | if (!list_empty(&ep->asocs)) { |
3771 | asoc = list_entry(ep->asocs.next, | 3771 | asoc = list_entry(ep->asocs.next, |
3772 | struct sctp_association, asocs); | 3772 | struct sctp_association, asocs); |
3773 | sctp_primitive_SHUTDOWN(asoc, NULL); | 3773 | sctp_primitive_SHUTDOWN(asoc, NULL); |
3774 | } | 3774 | } |
3775 | } | 3775 | } |
3776 | } | 3776 | } |
3777 | 3777 | ||
3778 | /* 7.2.1 Association Status (SCTP_STATUS) | 3778 | /* 7.2.1 Association Status (SCTP_STATUS) |
3779 | 3779 | ||
3780 | * Applications can retrieve current status information about an | 3780 | * Applications can retrieve current status information about an |
3781 | * association, including association state, peer receiver window size, | 3781 | * association, including association state, peer receiver window size, |
3782 | * number of unacked data chunks, and number of data chunks pending | 3782 | * number of unacked data chunks, and number of data chunks pending |
3783 | * receipt. This information is read-only. | 3783 | * receipt. This information is read-only. |
3784 | */ | 3784 | */ |
3785 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, | 3785 | static int sctp_getsockopt_sctp_status(struct sock *sk, int len, |
3786 | char __user *optval, | 3786 | char __user *optval, |
3787 | int __user *optlen) | 3787 | int __user *optlen) |
3788 | { | 3788 | { |
3789 | struct sctp_status status; | 3789 | struct sctp_status status; |
3790 | struct sctp_association *asoc = NULL; | 3790 | struct sctp_association *asoc = NULL; |
3791 | struct sctp_transport *transport; | 3791 | struct sctp_transport *transport; |
3792 | sctp_assoc_t associd; | 3792 | sctp_assoc_t associd; |
3793 | int retval = 0; | 3793 | int retval = 0; |
3794 | 3794 | ||
3795 | if (len < sizeof(status)) { | 3795 | if (len < sizeof(status)) { |
3796 | retval = -EINVAL; | 3796 | retval = -EINVAL; |
3797 | goto out; | 3797 | goto out; |
3798 | } | 3798 | } |
3799 | 3799 | ||
3800 | len = sizeof(status); | 3800 | len = sizeof(status); |
3801 | if (copy_from_user(&status, optval, len)) { | 3801 | if (copy_from_user(&status, optval, len)) { |
3802 | retval = -EFAULT; | 3802 | retval = -EFAULT; |
3803 | goto out; | 3803 | goto out; |
3804 | } | 3804 | } |
3805 | 3805 | ||
3806 | associd = status.sstat_assoc_id; | 3806 | associd = status.sstat_assoc_id; |
3807 | asoc = sctp_id2assoc(sk, associd); | 3807 | asoc = sctp_id2assoc(sk, associd); |
3808 | if (!asoc) { | 3808 | if (!asoc) { |
3809 | retval = -EINVAL; | 3809 | retval = -EINVAL; |
3810 | goto out; | 3810 | goto out; |
3811 | } | 3811 | } |
3812 | 3812 | ||
3813 | transport = asoc->peer.primary_path; | 3813 | transport = asoc->peer.primary_path; |
3814 | 3814 | ||
3815 | status.sstat_assoc_id = sctp_assoc2id(asoc); | 3815 | status.sstat_assoc_id = sctp_assoc2id(asoc); |
3816 | status.sstat_state = asoc->state; | 3816 | status.sstat_state = asoc->state; |
3817 | status.sstat_rwnd = asoc->peer.rwnd; | 3817 | status.sstat_rwnd = asoc->peer.rwnd; |
3818 | status.sstat_unackdata = asoc->unack_data; | 3818 | status.sstat_unackdata = asoc->unack_data; |
3819 | 3819 | ||
3820 | status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); | 3820 | status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map); |
3821 | status.sstat_instrms = asoc->c.sinit_max_instreams; | 3821 | status.sstat_instrms = asoc->c.sinit_max_instreams; |
3822 | status.sstat_outstrms = asoc->c.sinit_num_ostreams; | 3822 | status.sstat_outstrms = asoc->c.sinit_num_ostreams; |
3823 | status.sstat_fragmentation_point = asoc->frag_point; | 3823 | status.sstat_fragmentation_point = asoc->frag_point; |
3824 | status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 3824 | status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
3825 | memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, | 3825 | memcpy(&status.sstat_primary.spinfo_address, &transport->ipaddr, |
3826 | transport->af_specific->sockaddr_len); | 3826 | transport->af_specific->sockaddr_len); |
3827 | /* Map ipv4 address into v4-mapped-on-v6 address. */ | 3827 | /* Map ipv4 address into v4-mapped-on-v6 address. */ |
3828 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 3828 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
3829 | (union sctp_addr *)&status.sstat_primary.spinfo_address); | 3829 | (union sctp_addr *)&status.sstat_primary.spinfo_address); |
3830 | status.sstat_primary.spinfo_state = transport->state; | 3830 | status.sstat_primary.spinfo_state = transport->state; |
3831 | status.sstat_primary.spinfo_cwnd = transport->cwnd; | 3831 | status.sstat_primary.spinfo_cwnd = transport->cwnd; |
3832 | status.sstat_primary.spinfo_srtt = transport->srtt; | 3832 | status.sstat_primary.spinfo_srtt = transport->srtt; |
3833 | status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); | 3833 | status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto); |
3834 | status.sstat_primary.spinfo_mtu = transport->pathmtu; | 3834 | status.sstat_primary.spinfo_mtu = transport->pathmtu; |
3835 | 3835 | ||
3836 | if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) | 3836 | if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN) |
3837 | status.sstat_primary.spinfo_state = SCTP_ACTIVE; | 3837 | status.sstat_primary.spinfo_state = SCTP_ACTIVE; |
3838 | 3838 | ||
3839 | if (put_user(len, optlen)) { | 3839 | if (put_user(len, optlen)) { |
3840 | retval = -EFAULT; | 3840 | retval = -EFAULT; |
3841 | goto out; | 3841 | goto out; |
3842 | } | 3842 | } |
3843 | 3843 | ||
3844 | SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n", | 3844 | SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n", |
3845 | len, status.sstat_state, status.sstat_rwnd, | 3845 | len, status.sstat_state, status.sstat_rwnd, |
3846 | status.sstat_assoc_id); | 3846 | status.sstat_assoc_id); |
3847 | 3847 | ||
3848 | if (copy_to_user(optval, &status, len)) { | 3848 | if (copy_to_user(optval, &status, len)) { |
3849 | retval = -EFAULT; | 3849 | retval = -EFAULT; |
3850 | goto out; | 3850 | goto out; |
3851 | } | 3851 | } |
3852 | 3852 | ||
3853 | out: | 3853 | out: |
3854 | return (retval); | 3854 | return (retval); |
3855 | } | 3855 | } |
3856 | 3856 | ||
3857 | 3857 | ||
3858 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) | 3858 | /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO) |
3859 | * | 3859 | * |
3860 | * Applications can retrieve information about a specific peer address | 3860 | * Applications can retrieve information about a specific peer address |
3861 | * of an association, including its reachability state, congestion | 3861 | * of an association, including its reachability state, congestion |
3862 | * window, and retransmission timer values. This information is | 3862 | * window, and retransmission timer values. This information is |
3863 | * read-only. | 3863 | * read-only. |
3864 | */ | 3864 | */ |
3865 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, | 3865 | static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len, |
3866 | char __user *optval, | 3866 | char __user *optval, |
3867 | int __user *optlen) | 3867 | int __user *optlen) |
3868 | { | 3868 | { |
3869 | struct sctp_paddrinfo pinfo; | 3869 | struct sctp_paddrinfo pinfo; |
3870 | struct sctp_transport *transport; | 3870 | struct sctp_transport *transport; |
3871 | int retval = 0; | 3871 | int retval = 0; |
3872 | 3872 | ||
3873 | if (len < sizeof(pinfo)) { | 3873 | if (len < sizeof(pinfo)) { |
3874 | retval = -EINVAL; | 3874 | retval = -EINVAL; |
3875 | goto out; | 3875 | goto out; |
3876 | } | 3876 | } |
3877 | 3877 | ||
3878 | len = sizeof(pinfo); | 3878 | len = sizeof(pinfo); |
3879 | if (copy_from_user(&pinfo, optval, len)) { | 3879 | if (copy_from_user(&pinfo, optval, len)) { |
3880 | retval = -EFAULT; | 3880 | retval = -EFAULT; |
3881 | goto out; | 3881 | goto out; |
3882 | } | 3882 | } |
3883 | 3883 | ||
3884 | transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, | 3884 | transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address, |
3885 | pinfo.spinfo_assoc_id); | 3885 | pinfo.spinfo_assoc_id); |
3886 | if (!transport) | 3886 | if (!transport) |
3887 | return -EINVAL; | 3887 | return -EINVAL; |
3888 | 3888 | ||
3889 | pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); | 3889 | pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc); |
3890 | pinfo.spinfo_state = transport->state; | 3890 | pinfo.spinfo_state = transport->state; |
3891 | pinfo.spinfo_cwnd = transport->cwnd; | 3891 | pinfo.spinfo_cwnd = transport->cwnd; |
3892 | pinfo.spinfo_srtt = transport->srtt; | 3892 | pinfo.spinfo_srtt = transport->srtt; |
3893 | pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); | 3893 | pinfo.spinfo_rto = jiffies_to_msecs(transport->rto); |
3894 | pinfo.spinfo_mtu = transport->pathmtu; | 3894 | pinfo.spinfo_mtu = transport->pathmtu; |
3895 | 3895 | ||
3896 | if (pinfo.spinfo_state == SCTP_UNKNOWN) | 3896 | if (pinfo.spinfo_state == SCTP_UNKNOWN) |
3897 | pinfo.spinfo_state = SCTP_ACTIVE; | 3897 | pinfo.spinfo_state = SCTP_ACTIVE; |
3898 | 3898 | ||
3899 | if (put_user(len, optlen)) { | 3899 | if (put_user(len, optlen)) { |
3900 | retval = -EFAULT; | 3900 | retval = -EFAULT; |
3901 | goto out; | 3901 | goto out; |
3902 | } | 3902 | } |
3903 | 3903 | ||
3904 | if (copy_to_user(optval, &pinfo, len)) { | 3904 | if (copy_to_user(optval, &pinfo, len)) { |
3905 | retval = -EFAULT; | 3905 | retval = -EFAULT; |
3906 | goto out; | 3906 | goto out; |
3907 | } | 3907 | } |
3908 | 3908 | ||
3909 | out: | 3909 | out: |
3910 | return (retval); | 3910 | return (retval); |
3911 | } | 3911 | } |
3912 | 3912 | ||
3913 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) | 3913 | /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS) |
3914 | * | 3914 | * |
3915 | * This option is a on/off flag. If enabled no SCTP message | 3915 | * This option is a on/off flag. If enabled no SCTP message |
3916 | * fragmentation will be performed. Instead if a message being sent | 3916 | * fragmentation will be performed. Instead if a message being sent |
3917 | * exceeds the current PMTU size, the message will NOT be sent and | 3917 | * exceeds the current PMTU size, the message will NOT be sent and |
3918 | * instead a error will be indicated to the user. | 3918 | * instead a error will be indicated to the user. |
3919 | */ | 3919 | */ |
3920 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, | 3920 | static int sctp_getsockopt_disable_fragments(struct sock *sk, int len, |
3921 | char __user *optval, int __user *optlen) | 3921 | char __user *optval, int __user *optlen) |
3922 | { | 3922 | { |
3923 | int val; | 3923 | int val; |
3924 | 3924 | ||
3925 | if (len < sizeof(int)) | 3925 | if (len < sizeof(int)) |
3926 | return -EINVAL; | 3926 | return -EINVAL; |
3927 | 3927 | ||
3928 | len = sizeof(int); | 3928 | len = sizeof(int); |
3929 | val = (sctp_sk(sk)->disable_fragments == 1); | 3929 | val = (sctp_sk(sk)->disable_fragments == 1); |
3930 | if (put_user(len, optlen)) | 3930 | if (put_user(len, optlen)) |
3931 | return -EFAULT; | 3931 | return -EFAULT; |
3932 | if (copy_to_user(optval, &val, len)) | 3932 | if (copy_to_user(optval, &val, len)) |
3933 | return -EFAULT; | 3933 | return -EFAULT; |
3934 | return 0; | 3934 | return 0; |
3935 | } | 3935 | } |
3936 | 3936 | ||
3937 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) | 3937 | /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS) |
3938 | * | 3938 | * |
3939 | * This socket option is used to specify various notifications and | 3939 | * This socket option is used to specify various notifications and |
3940 | * ancillary data the user wishes to receive. | 3940 | * ancillary data the user wishes to receive. |
3941 | */ | 3941 | */ |
3942 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, | 3942 | static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval, |
3943 | int __user *optlen) | 3943 | int __user *optlen) |
3944 | { | 3944 | { |
3945 | if (len < sizeof(struct sctp_event_subscribe)) | 3945 | if (len < sizeof(struct sctp_event_subscribe)) |
3946 | return -EINVAL; | 3946 | return -EINVAL; |
3947 | len = sizeof(struct sctp_event_subscribe); | 3947 | len = sizeof(struct sctp_event_subscribe); |
3948 | if (put_user(len, optlen)) | 3948 | if (put_user(len, optlen)) |
3949 | return -EFAULT; | 3949 | return -EFAULT; |
3950 | if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) | 3950 | if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len)) |
3951 | return -EFAULT; | 3951 | return -EFAULT; |
3952 | return 0; | 3952 | return 0; |
3953 | } | 3953 | } |
3954 | 3954 | ||
3955 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) | 3955 | /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE) |
3956 | * | 3956 | * |
3957 | * This socket option is applicable to the UDP-style socket only. When | 3957 | * This socket option is applicable to the UDP-style socket only. When |
3958 | * set it will cause associations that are idle for more than the | 3958 | * set it will cause associations that are idle for more than the |
3959 | * specified number of seconds to automatically close. An association | 3959 | * specified number of seconds to automatically close. An association |
3960 | * being idle is defined an association that has NOT sent or received | 3960 | * being idle is defined an association that has NOT sent or received |
3961 | * user data. The special value of '0' indicates that no automatic | 3961 | * user data. The special value of '0' indicates that no automatic |
3962 | * close of any associations should be performed. The option expects an | 3962 | * close of any associations should be performed. The option expects an |
3963 | * integer defining the number of seconds of idle time before an | 3963 | * integer defining the number of seconds of idle time before an |
3964 | * association is closed. | 3964 | * association is closed. |
3965 | */ | 3965 | */ |
3966 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) | 3966 | static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen) |
3967 | { | 3967 | { |
3968 | /* Applicable to UDP-style socket only */ | 3968 | /* Applicable to UDP-style socket only */ |
3969 | if (sctp_style(sk, TCP)) | 3969 | if (sctp_style(sk, TCP)) |
3970 | return -EOPNOTSUPP; | 3970 | return -EOPNOTSUPP; |
3971 | if (len < sizeof(int)) | 3971 | if (len < sizeof(int)) |
3972 | return -EINVAL; | 3972 | return -EINVAL; |
3973 | len = sizeof(int); | 3973 | len = sizeof(int); |
3974 | if (put_user(len, optlen)) | 3974 | if (put_user(len, optlen)) |
3975 | return -EFAULT; | 3975 | return -EFAULT; |
3976 | if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int))) | 3976 | if (copy_to_user(optval, &sctp_sk(sk)->autoclose, sizeof(int))) |
3977 | return -EFAULT; | 3977 | return -EFAULT; |
3978 | return 0; | 3978 | return 0; |
3979 | } | 3979 | } |
3980 | 3980 | ||
3981 | /* Helper routine to branch off an association to a new socket. */ | 3981 | /* Helper routine to branch off an association to a new socket. */ |
3982 | SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc, | 3982 | SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc, |
3983 | struct socket **sockp) | 3983 | struct socket **sockp) |
3984 | { | 3984 | { |
3985 | struct sock *sk = asoc->base.sk; | 3985 | struct sock *sk = asoc->base.sk; |
3986 | struct socket *sock; | 3986 | struct socket *sock; |
3987 | struct sctp_af *af; | 3987 | struct sctp_af *af; |
3988 | int err = 0; | 3988 | int err = 0; |
3989 | 3989 | ||
3990 | /* An association cannot be branched off from an already peeled-off | 3990 | /* An association cannot be branched off from an already peeled-off |
3991 | * socket, nor is this supported for tcp style sockets. | 3991 | * socket, nor is this supported for tcp style sockets. |
3992 | */ | 3992 | */ |
3993 | if (!sctp_style(sk, UDP)) | 3993 | if (!sctp_style(sk, UDP)) |
3994 | return -EINVAL; | 3994 | return -EINVAL; |
3995 | 3995 | ||
3996 | /* Create a new socket. */ | 3996 | /* Create a new socket. */ |
3997 | err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); | 3997 | err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock); |
3998 | if (err < 0) | 3998 | if (err < 0) |
3999 | return err; | 3999 | return err; |
4000 | 4000 | ||
4001 | sctp_copy_sock(sock->sk, sk, asoc); | 4001 | sctp_copy_sock(sock->sk, sk, asoc); |
4002 | 4002 | ||
4003 | /* Make peeled-off sockets more like 1-1 accepted sockets. | 4003 | /* Make peeled-off sockets more like 1-1 accepted sockets. |
4004 | * Set the daddr and initialize id to something more random | 4004 | * Set the daddr and initialize id to something more random |
4005 | */ | 4005 | */ |
4006 | af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family); | 4006 | af = sctp_get_af_specific(asoc->peer.primary_addr.sa.sa_family); |
4007 | af->to_sk_daddr(&asoc->peer.primary_addr, sk); | 4007 | af->to_sk_daddr(&asoc->peer.primary_addr, sk); |
4008 | 4008 | ||
4009 | /* Populate the fields of the newsk from the oldsk and migrate the | 4009 | /* Populate the fields of the newsk from the oldsk and migrate the |
4010 | * asoc to the newsk. | 4010 | * asoc to the newsk. |
4011 | */ | 4011 | */ |
4012 | sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); | 4012 | sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH); |
4013 | 4013 | ||
4014 | *sockp = sock; | 4014 | *sockp = sock; |
4015 | 4015 | ||
4016 | return err; | 4016 | return err; |
4017 | } | 4017 | } |
4018 | 4018 | ||
4019 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) | 4019 | static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen) |
4020 | { | 4020 | { |
4021 | sctp_peeloff_arg_t peeloff; | 4021 | sctp_peeloff_arg_t peeloff; |
4022 | struct socket *newsock; | 4022 | struct socket *newsock; |
4023 | int retval = 0; | 4023 | int retval = 0; |
4024 | struct sctp_association *asoc; | 4024 | struct sctp_association *asoc; |
4025 | 4025 | ||
4026 | if (len < sizeof(sctp_peeloff_arg_t)) | 4026 | if (len < sizeof(sctp_peeloff_arg_t)) |
4027 | return -EINVAL; | 4027 | return -EINVAL; |
4028 | len = sizeof(sctp_peeloff_arg_t); | 4028 | len = sizeof(sctp_peeloff_arg_t); |
4029 | if (copy_from_user(&peeloff, optval, len)) | 4029 | if (copy_from_user(&peeloff, optval, len)) |
4030 | return -EFAULT; | 4030 | return -EFAULT; |
4031 | 4031 | ||
4032 | asoc = sctp_id2assoc(sk, peeloff.associd); | 4032 | asoc = sctp_id2assoc(sk, peeloff.associd); |
4033 | if (!asoc) { | 4033 | if (!asoc) { |
4034 | retval = -EINVAL; | 4034 | retval = -EINVAL; |
4035 | goto out; | 4035 | goto out; |
4036 | } | 4036 | } |
4037 | 4037 | ||
4038 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __func__, sk, asoc); | 4038 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __func__, sk, asoc); |
4039 | 4039 | ||
4040 | retval = sctp_do_peeloff(asoc, &newsock); | 4040 | retval = sctp_do_peeloff(asoc, &newsock); |
4041 | if (retval < 0) | 4041 | if (retval < 0) |
4042 | goto out; | 4042 | goto out; |
4043 | 4043 | ||
4044 | /* Map the socket to an unused fd that can be returned to the user. */ | 4044 | /* Map the socket to an unused fd that can be returned to the user. */ |
4045 | retval = sock_map_fd(newsock, 0); | 4045 | retval = sock_map_fd(newsock, 0); |
4046 | if (retval < 0) { | 4046 | if (retval < 0) { |
4047 | sock_release(newsock); | 4047 | sock_release(newsock); |
4048 | goto out; | 4048 | goto out; |
4049 | } | 4049 | } |
4050 | 4050 | ||
4051 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n", | 4051 | SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n", |
4052 | __func__, sk, asoc, newsock->sk, retval); | 4052 | __func__, sk, asoc, newsock->sk, retval); |
4053 | 4053 | ||
4054 | /* Return the fd mapped to the new socket. */ | 4054 | /* Return the fd mapped to the new socket. */ |
4055 | peeloff.sd = retval; | 4055 | peeloff.sd = retval; |
4056 | if (put_user(len, optlen)) | 4056 | if (put_user(len, optlen)) |
4057 | return -EFAULT; | 4057 | return -EFAULT; |
4058 | if (copy_to_user(optval, &peeloff, len)) | 4058 | if (copy_to_user(optval, &peeloff, len)) |
4059 | retval = -EFAULT; | 4059 | retval = -EFAULT; |
4060 | 4060 | ||
4061 | out: | 4061 | out: |
4062 | return retval; | 4062 | return retval; |
4063 | } | 4063 | } |
4064 | 4064 | ||
4065 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) | 4065 | /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS) |
4066 | * | 4066 | * |
4067 | * Applications can enable or disable heartbeats for any peer address of | 4067 | * Applications can enable or disable heartbeats for any peer address of |
4068 | * an association, modify an address's heartbeat interval, force a | 4068 | * an association, modify an address's heartbeat interval, force a |
4069 | * heartbeat to be sent immediately, and adjust the address's maximum | 4069 | * heartbeat to be sent immediately, and adjust the address's maximum |
4070 | * number of retransmissions sent before an address is considered | 4070 | * number of retransmissions sent before an address is considered |
4071 | * unreachable. The following structure is used to access and modify an | 4071 | * unreachable. The following structure is used to access and modify an |
4072 | * address's parameters: | 4072 | * address's parameters: |
4073 | * | 4073 | * |
4074 | * struct sctp_paddrparams { | 4074 | * struct sctp_paddrparams { |
4075 | * sctp_assoc_t spp_assoc_id; | 4075 | * sctp_assoc_t spp_assoc_id; |
4076 | * struct sockaddr_storage spp_address; | 4076 | * struct sockaddr_storage spp_address; |
4077 | * uint32_t spp_hbinterval; | 4077 | * uint32_t spp_hbinterval; |
4078 | * uint16_t spp_pathmaxrxt; | 4078 | * uint16_t spp_pathmaxrxt; |
4079 | * uint32_t spp_pathmtu; | 4079 | * uint32_t spp_pathmtu; |
4080 | * uint32_t spp_sackdelay; | 4080 | * uint32_t spp_sackdelay; |
4081 | * uint32_t spp_flags; | 4081 | * uint32_t spp_flags; |
4082 | * }; | 4082 | * }; |
4083 | * | 4083 | * |
4084 | * spp_assoc_id - (one-to-many style socket) This is filled in the | 4084 | * spp_assoc_id - (one-to-many style socket) This is filled in the |
4085 | * application, and identifies the association for | 4085 | * application, and identifies the association for |
4086 | * this query. | 4086 | * this query. |
4087 | * spp_address - This specifies which address is of interest. | 4087 | * spp_address - This specifies which address is of interest. |
4088 | * spp_hbinterval - This contains the value of the heartbeat interval, | 4088 | * spp_hbinterval - This contains the value of the heartbeat interval, |
4089 | * in milliseconds. If a value of zero | 4089 | * in milliseconds. If a value of zero |
4090 | * is present in this field then no changes are to | 4090 | * is present in this field then no changes are to |
4091 | * be made to this parameter. | 4091 | * be made to this parameter. |
4092 | * spp_pathmaxrxt - This contains the maximum number of | 4092 | * spp_pathmaxrxt - This contains the maximum number of |
4093 | * retransmissions before this address shall be | 4093 | * retransmissions before this address shall be |
4094 | * considered unreachable. If a value of zero | 4094 | * considered unreachable. If a value of zero |
4095 | * is present in this field then no changes are to | 4095 | * is present in this field then no changes are to |
4096 | * be made to this parameter. | 4096 | * be made to this parameter. |
4097 | * spp_pathmtu - When Path MTU discovery is disabled the value | 4097 | * spp_pathmtu - When Path MTU discovery is disabled the value |
4098 | * specified here will be the "fixed" path mtu. | 4098 | * specified here will be the "fixed" path mtu. |
4099 | * Note that if the spp_address field is empty | 4099 | * Note that if the spp_address field is empty |
4100 | * then all associations on this address will | 4100 | * then all associations on this address will |
4101 | * have this fixed path mtu set upon them. | 4101 | * have this fixed path mtu set upon them. |
4102 | * | 4102 | * |
4103 | * spp_sackdelay - When delayed sack is enabled, this value specifies | 4103 | * spp_sackdelay - When delayed sack is enabled, this value specifies |
4104 | * the number of milliseconds that sacks will be delayed | 4104 | * the number of milliseconds that sacks will be delayed |
4105 | * for. This value will apply to all addresses of an | 4105 | * for. This value will apply to all addresses of an |
4106 | * association if the spp_address field is empty. Note | 4106 | * association if the spp_address field is empty. Note |
4107 | * also, that if delayed sack is enabled and this | 4107 | * also, that if delayed sack is enabled and this |
4108 | * value is set to 0, no change is made to the last | 4108 | * value is set to 0, no change is made to the last |
4109 | * recorded delayed sack timer value. | 4109 | * recorded delayed sack timer value. |
4110 | * | 4110 | * |
4111 | * spp_flags - These flags are used to control various features | 4111 | * spp_flags - These flags are used to control various features |
4112 | * on an association. The flag field may contain | 4112 | * on an association. The flag field may contain |
4113 | * zero or more of the following options. | 4113 | * zero or more of the following options. |
4114 | * | 4114 | * |
4115 | * SPP_HB_ENABLE - Enable heartbeats on the | 4115 | * SPP_HB_ENABLE - Enable heartbeats on the |
4116 | * specified address. Note that if the address | 4116 | * specified address. Note that if the address |
4117 | * field is empty all addresses for the association | 4117 | * field is empty all addresses for the association |
4118 | * have heartbeats enabled upon them. | 4118 | * have heartbeats enabled upon them. |
4119 | * | 4119 | * |
4120 | * SPP_HB_DISABLE - Disable heartbeats on the | 4120 | * SPP_HB_DISABLE - Disable heartbeats on the |
4121 | * speicifed address. Note that if the address | 4121 | * speicifed address. Note that if the address |
4122 | * field is empty all addresses for the association | 4122 | * field is empty all addresses for the association |
4123 | * will have their heartbeats disabled. Note also | 4123 | * will have their heartbeats disabled. Note also |
4124 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are | 4124 | * that SPP_HB_ENABLE and SPP_HB_DISABLE are |
4125 | * mutually exclusive, only one of these two should | 4125 | * mutually exclusive, only one of these two should |
4126 | * be specified. Enabling both fields will have | 4126 | * be specified. Enabling both fields will have |
4127 | * undetermined results. | 4127 | * undetermined results. |
4128 | * | 4128 | * |
4129 | * SPP_HB_DEMAND - Request a user initiated heartbeat | 4129 | * SPP_HB_DEMAND - Request a user initiated heartbeat |
4130 | * to be made immediately. | 4130 | * to be made immediately. |
4131 | * | 4131 | * |
4132 | * SPP_PMTUD_ENABLE - This field will enable PMTU | 4132 | * SPP_PMTUD_ENABLE - This field will enable PMTU |
4133 | * discovery upon the specified address. Note that | 4133 | * discovery upon the specified address. Note that |
4134 | * if the address feild is empty then all addresses | 4134 | * if the address feild is empty then all addresses |
4135 | * on the association are effected. | 4135 | * on the association are effected. |
4136 | * | 4136 | * |
4137 | * SPP_PMTUD_DISABLE - This field will disable PMTU | 4137 | * SPP_PMTUD_DISABLE - This field will disable PMTU |
4138 | * discovery upon the specified address. Note that | 4138 | * discovery upon the specified address. Note that |
4139 | * if the address feild is empty then all addresses | 4139 | * if the address feild is empty then all addresses |
4140 | * on the association are effected. Not also that | 4140 | * on the association are effected. Not also that |
4141 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually | 4141 | * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually |
4142 | * exclusive. Enabling both will have undetermined | 4142 | * exclusive. Enabling both will have undetermined |
4143 | * results. | 4143 | * results. |
4144 | * | 4144 | * |
4145 | * SPP_SACKDELAY_ENABLE - Setting this flag turns | 4145 | * SPP_SACKDELAY_ENABLE - Setting this flag turns |
4146 | * on delayed sack. The time specified in spp_sackdelay | 4146 | * on delayed sack. The time specified in spp_sackdelay |
4147 | * is used to specify the sack delay for this address. Note | 4147 | * is used to specify the sack delay for this address. Note |
4148 | * that if spp_address is empty then all addresses will | 4148 | * that if spp_address is empty then all addresses will |
4149 | * enable delayed sack and take on the sack delay | 4149 | * enable delayed sack and take on the sack delay |
4150 | * value specified in spp_sackdelay. | 4150 | * value specified in spp_sackdelay. |
4151 | * SPP_SACKDELAY_DISABLE - Setting this flag turns | 4151 | * SPP_SACKDELAY_DISABLE - Setting this flag turns |
4152 | * off delayed sack. If the spp_address field is blank then | 4152 | * off delayed sack. If the spp_address field is blank then |
4153 | * delayed sack is disabled for the entire association. Note | 4153 | * delayed sack is disabled for the entire association. Note |
4154 | * also that this field is mutually exclusive to | 4154 | * also that this field is mutually exclusive to |
4155 | * SPP_SACKDELAY_ENABLE, setting both will have undefined | 4155 | * SPP_SACKDELAY_ENABLE, setting both will have undefined |
4156 | * results. | 4156 | * results. |
4157 | */ | 4157 | */ |
4158 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, | 4158 | static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len, |
4159 | char __user *optval, int __user *optlen) | 4159 | char __user *optval, int __user *optlen) |
4160 | { | 4160 | { |
4161 | struct sctp_paddrparams params; | 4161 | struct sctp_paddrparams params; |
4162 | struct sctp_transport *trans = NULL; | 4162 | struct sctp_transport *trans = NULL; |
4163 | struct sctp_association *asoc = NULL; | 4163 | struct sctp_association *asoc = NULL; |
4164 | struct sctp_sock *sp = sctp_sk(sk); | 4164 | struct sctp_sock *sp = sctp_sk(sk); |
4165 | 4165 | ||
4166 | if (len < sizeof(struct sctp_paddrparams)) | 4166 | if (len < sizeof(struct sctp_paddrparams)) |
4167 | return -EINVAL; | 4167 | return -EINVAL; |
4168 | len = sizeof(struct sctp_paddrparams); | 4168 | len = sizeof(struct sctp_paddrparams); |
4169 | if (copy_from_user(¶ms, optval, len)) | 4169 | if (copy_from_user(¶ms, optval, len)) |
4170 | return -EFAULT; | 4170 | return -EFAULT; |
4171 | 4171 | ||
4172 | /* If an address other than INADDR_ANY is specified, and | 4172 | /* If an address other than INADDR_ANY is specified, and |
4173 | * no transport is found, then the request is invalid. | 4173 | * no transport is found, then the request is invalid. |
4174 | */ | 4174 | */ |
4175 | if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { | 4175 | if (!sctp_is_any(sk, ( union sctp_addr *)¶ms.spp_address)) { |
4176 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, | 4176 | trans = sctp_addr_id2transport(sk, ¶ms.spp_address, |
4177 | params.spp_assoc_id); | 4177 | params.spp_assoc_id); |
4178 | if (!trans) { | 4178 | if (!trans) { |
4179 | SCTP_DEBUG_PRINTK("Failed no transport\n"); | 4179 | SCTP_DEBUG_PRINTK("Failed no transport\n"); |
4180 | return -EINVAL; | 4180 | return -EINVAL; |
4181 | } | 4181 | } |
4182 | } | 4182 | } |
4183 | 4183 | ||
4184 | /* Get association, if assoc_id != 0 and the socket is a one | 4184 | /* Get association, if assoc_id != 0 and the socket is a one |
4185 | * to many style socket, and an association was not found, then | 4185 | * to many style socket, and an association was not found, then |
4186 | * the id was invalid. | 4186 | * the id was invalid. |
4187 | */ | 4187 | */ |
4188 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); | 4188 | asoc = sctp_id2assoc(sk, params.spp_assoc_id); |
4189 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { | 4189 | if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) { |
4190 | SCTP_DEBUG_PRINTK("Failed no association\n"); | 4190 | SCTP_DEBUG_PRINTK("Failed no association\n"); |
4191 | return -EINVAL; | 4191 | return -EINVAL; |
4192 | } | 4192 | } |
4193 | 4193 | ||
4194 | if (trans) { | 4194 | if (trans) { |
4195 | /* Fetch transport values. */ | 4195 | /* Fetch transport values. */ |
4196 | params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); | 4196 | params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval); |
4197 | params.spp_pathmtu = trans->pathmtu; | 4197 | params.spp_pathmtu = trans->pathmtu; |
4198 | params.spp_pathmaxrxt = trans->pathmaxrxt; | 4198 | params.spp_pathmaxrxt = trans->pathmaxrxt; |
4199 | params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); | 4199 | params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay); |
4200 | 4200 | ||
4201 | /*draft-11 doesn't say what to return in spp_flags*/ | 4201 | /*draft-11 doesn't say what to return in spp_flags*/ |
4202 | params.spp_flags = trans->param_flags; | 4202 | params.spp_flags = trans->param_flags; |
4203 | } else if (asoc) { | 4203 | } else if (asoc) { |
4204 | /* Fetch association values. */ | 4204 | /* Fetch association values. */ |
4205 | params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); | 4205 | params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval); |
4206 | params.spp_pathmtu = asoc->pathmtu; | 4206 | params.spp_pathmtu = asoc->pathmtu; |
4207 | params.spp_pathmaxrxt = asoc->pathmaxrxt; | 4207 | params.spp_pathmaxrxt = asoc->pathmaxrxt; |
4208 | params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); | 4208 | params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay); |
4209 | 4209 | ||
4210 | /*draft-11 doesn't say what to return in spp_flags*/ | 4210 | /*draft-11 doesn't say what to return in spp_flags*/ |
4211 | params.spp_flags = asoc->param_flags; | 4211 | params.spp_flags = asoc->param_flags; |
4212 | } else { | 4212 | } else { |
4213 | /* Fetch socket values. */ | 4213 | /* Fetch socket values. */ |
4214 | params.spp_hbinterval = sp->hbinterval; | 4214 | params.spp_hbinterval = sp->hbinterval; |
4215 | params.spp_pathmtu = sp->pathmtu; | 4215 | params.spp_pathmtu = sp->pathmtu; |
4216 | params.spp_sackdelay = sp->sackdelay; | 4216 | params.spp_sackdelay = sp->sackdelay; |
4217 | params.spp_pathmaxrxt = sp->pathmaxrxt; | 4217 | params.spp_pathmaxrxt = sp->pathmaxrxt; |
4218 | 4218 | ||
4219 | /*draft-11 doesn't say what to return in spp_flags*/ | 4219 | /*draft-11 doesn't say what to return in spp_flags*/ |
4220 | params.spp_flags = sp->param_flags; | 4220 | params.spp_flags = sp->param_flags; |
4221 | } | 4221 | } |
4222 | 4222 | ||
4223 | if (copy_to_user(optval, ¶ms, len)) | 4223 | if (copy_to_user(optval, ¶ms, len)) |
4224 | return -EFAULT; | 4224 | return -EFAULT; |
4225 | 4225 | ||
4226 | if (put_user(len, optlen)) | 4226 | if (put_user(len, optlen)) |
4227 | return -EFAULT; | 4227 | return -EFAULT; |
4228 | 4228 | ||
4229 | return 0; | 4229 | return 0; |
4230 | } | 4230 | } |
4231 | 4231 | ||
4232 | /* | 4232 | /* |
4233 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) | 4233 | * 7.1.23. Get or set delayed ack timer (SCTP_DELAYED_SACK) |
4234 | * | 4234 | * |
4235 | * This option will effect the way delayed acks are performed. This | 4235 | * This option will effect the way delayed acks are performed. This |
4236 | * option allows you to get or set the delayed ack time, in | 4236 | * option allows you to get or set the delayed ack time, in |
4237 | * milliseconds. It also allows changing the delayed ack frequency. | 4237 | * milliseconds. It also allows changing the delayed ack frequency. |
4238 | * Changing the frequency to 1 disables the delayed sack algorithm. If | 4238 | * Changing the frequency to 1 disables the delayed sack algorithm. If |
4239 | * the assoc_id is 0, then this sets or gets the endpoints default | 4239 | * the assoc_id is 0, then this sets or gets the endpoints default |
4240 | * values. If the assoc_id field is non-zero, then the set or get | 4240 | * values. If the assoc_id field is non-zero, then the set or get |
4241 | * effects the specified association for the one to many model (the | 4241 | * effects the specified association for the one to many model (the |
4242 | * assoc_id field is ignored by the one to one model). Note that if | 4242 | * assoc_id field is ignored by the one to one model). Note that if |
4243 | * sack_delay or sack_freq are 0 when setting this option, then the | 4243 | * sack_delay or sack_freq are 0 when setting this option, then the |
4244 | * current values will remain unchanged. | 4244 | * current values will remain unchanged. |
4245 | * | 4245 | * |
4246 | * struct sctp_sack_info { | 4246 | * struct sctp_sack_info { |
4247 | * sctp_assoc_t sack_assoc_id; | 4247 | * sctp_assoc_t sack_assoc_id; |
4248 | * uint32_t sack_delay; | 4248 | * uint32_t sack_delay; |
4249 | * uint32_t sack_freq; | 4249 | * uint32_t sack_freq; |
4250 | * }; | 4250 | * }; |
4251 | * | 4251 | * |
4252 | * sack_assoc_id - This parameter, indicates which association the user | 4252 | * sack_assoc_id - This parameter, indicates which association the user |
4253 | * is performing an action upon. Note that if this field's value is | 4253 | * is performing an action upon. Note that if this field's value is |
4254 | * zero then the endpoints default value is changed (effecting future | 4254 | * zero then the endpoints default value is changed (effecting future |
4255 | * associations only). | 4255 | * associations only). |
4256 | * | 4256 | * |
4257 | * sack_delay - This parameter contains the number of milliseconds that | 4257 | * sack_delay - This parameter contains the number of milliseconds that |
4258 | * the user is requesting the delayed ACK timer be set to. Note that | 4258 | * the user is requesting the delayed ACK timer be set to. Note that |
4259 | * this value is defined in the standard to be between 200 and 500 | 4259 | * this value is defined in the standard to be between 200 and 500 |
4260 | * milliseconds. | 4260 | * milliseconds. |
4261 | * | 4261 | * |
4262 | * sack_freq - This parameter contains the number of packets that must | 4262 | * sack_freq - This parameter contains the number of packets that must |
4263 | * be received before a sack is sent without waiting for the delay | 4263 | * be received before a sack is sent without waiting for the delay |
4264 | * timer to expire. The default value for this is 2, setting this | 4264 | * timer to expire. The default value for this is 2, setting this |
4265 | * value to 1 will disable the delayed sack algorithm. | 4265 | * value to 1 will disable the delayed sack algorithm. |
4266 | */ | 4266 | */ |
4267 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, | 4267 | static int sctp_getsockopt_delayed_ack(struct sock *sk, int len, |
4268 | char __user *optval, | 4268 | char __user *optval, |
4269 | int __user *optlen) | 4269 | int __user *optlen) |
4270 | { | 4270 | { |
4271 | struct sctp_sack_info params; | 4271 | struct sctp_sack_info params; |
4272 | struct sctp_association *asoc = NULL; | 4272 | struct sctp_association *asoc = NULL; |
4273 | struct sctp_sock *sp = sctp_sk(sk); | 4273 | struct sctp_sock *sp = sctp_sk(sk); |
4274 | 4274 | ||
4275 | if (len >= sizeof(struct sctp_sack_info)) { | 4275 | if (len >= sizeof(struct sctp_sack_info)) { |
4276 | len = sizeof(struct sctp_sack_info); | 4276 | len = sizeof(struct sctp_sack_info); |
4277 | 4277 | ||
4278 | if (copy_from_user(¶ms, optval, len)) | 4278 | if (copy_from_user(¶ms, optval, len)) |
4279 | return -EFAULT; | 4279 | return -EFAULT; |
4280 | } else if (len == sizeof(struct sctp_assoc_value)) { | 4280 | } else if (len == sizeof(struct sctp_assoc_value)) { |
4281 | pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); | 4281 | pr_warn("Use of struct sctp_assoc_value in delayed_ack socket option deprecated\n"); |
4282 | pr_warn("Use struct sctp_sack_info instead\n"); | 4282 | pr_warn("Use struct sctp_sack_info instead\n"); |
4283 | if (copy_from_user(¶ms, optval, len)) | 4283 | if (copy_from_user(¶ms, optval, len)) |
4284 | return -EFAULT; | 4284 | return -EFAULT; |
4285 | } else | 4285 | } else |
4286 | return - EINVAL; | 4286 | return - EINVAL; |
4287 | 4287 | ||
4288 | /* Get association, if sack_assoc_id != 0 and the socket is a one | 4288 | /* Get association, if sack_assoc_id != 0 and the socket is a one |
4289 | * to many style socket, and an association was not found, then | 4289 | * to many style socket, and an association was not found, then |
4290 | * the id was invalid. | 4290 | * the id was invalid. |
4291 | */ | 4291 | */ |
4292 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); | 4292 | asoc = sctp_id2assoc(sk, params.sack_assoc_id); |
4293 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) | 4293 | if (!asoc && params.sack_assoc_id && sctp_style(sk, UDP)) |
4294 | return -EINVAL; | 4294 | return -EINVAL; |
4295 | 4295 | ||
4296 | if (asoc) { | 4296 | if (asoc) { |
4297 | /* Fetch association values. */ | 4297 | /* Fetch association values. */ |
4298 | if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { | 4298 | if (asoc->param_flags & SPP_SACKDELAY_ENABLE) { |
4299 | params.sack_delay = jiffies_to_msecs( | 4299 | params.sack_delay = jiffies_to_msecs( |
4300 | asoc->sackdelay); | 4300 | asoc->sackdelay); |
4301 | params.sack_freq = asoc->sackfreq; | 4301 | params.sack_freq = asoc->sackfreq; |
4302 | 4302 | ||
4303 | } else { | 4303 | } else { |
4304 | params.sack_delay = 0; | 4304 | params.sack_delay = 0; |
4305 | params.sack_freq = 1; | 4305 | params.sack_freq = 1; |
4306 | } | 4306 | } |
4307 | } else { | 4307 | } else { |
4308 | /* Fetch socket values. */ | 4308 | /* Fetch socket values. */ |
4309 | if (sp->param_flags & SPP_SACKDELAY_ENABLE) { | 4309 | if (sp->param_flags & SPP_SACKDELAY_ENABLE) { |
4310 | params.sack_delay = sp->sackdelay; | 4310 | params.sack_delay = sp->sackdelay; |
4311 | params.sack_freq = sp->sackfreq; | 4311 | params.sack_freq = sp->sackfreq; |
4312 | } else { | 4312 | } else { |
4313 | params.sack_delay = 0; | 4313 | params.sack_delay = 0; |
4314 | params.sack_freq = 1; | 4314 | params.sack_freq = 1; |
4315 | } | 4315 | } |
4316 | } | 4316 | } |
4317 | 4317 | ||
4318 | if (copy_to_user(optval, ¶ms, len)) | 4318 | if (copy_to_user(optval, ¶ms, len)) |
4319 | return -EFAULT; | 4319 | return -EFAULT; |
4320 | 4320 | ||
4321 | if (put_user(len, optlen)) | 4321 | if (put_user(len, optlen)) |
4322 | return -EFAULT; | 4322 | return -EFAULT; |
4323 | 4323 | ||
4324 | return 0; | 4324 | return 0; |
4325 | } | 4325 | } |
4326 | 4326 | ||
4327 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) | 4327 | /* 7.1.3 Initialization Parameters (SCTP_INITMSG) |
4328 | * | 4328 | * |
4329 | * Applications can specify protocol parameters for the default association | 4329 | * Applications can specify protocol parameters for the default association |
4330 | * initialization. The option name argument to setsockopt() and getsockopt() | 4330 | * initialization. The option name argument to setsockopt() and getsockopt() |
4331 | * is SCTP_INITMSG. | 4331 | * is SCTP_INITMSG. |
4332 | * | 4332 | * |
4333 | * Setting initialization parameters is effective only on an unconnected | 4333 | * Setting initialization parameters is effective only on an unconnected |
4334 | * socket (for UDP-style sockets only future associations are effected | 4334 | * socket (for UDP-style sockets only future associations are effected |
4335 | * by the change). With TCP-style sockets, this option is inherited by | 4335 | * by the change). With TCP-style sockets, this option is inherited by |
4336 | * sockets derived from a listener socket. | 4336 | * sockets derived from a listener socket. |
4337 | */ | 4337 | */ |
4338 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) | 4338 | static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen) |
4339 | { | 4339 | { |
4340 | if (len < sizeof(struct sctp_initmsg)) | 4340 | if (len < sizeof(struct sctp_initmsg)) |
4341 | return -EINVAL; | 4341 | return -EINVAL; |
4342 | len = sizeof(struct sctp_initmsg); | 4342 | len = sizeof(struct sctp_initmsg); |
4343 | if (put_user(len, optlen)) | 4343 | if (put_user(len, optlen)) |
4344 | return -EFAULT; | 4344 | return -EFAULT; |
4345 | if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) | 4345 | if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len)) |
4346 | return -EFAULT; | 4346 | return -EFAULT; |
4347 | return 0; | 4347 | return 0; |
4348 | } | 4348 | } |
4349 | 4349 | ||
4350 | 4350 | ||
4351 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, | 4351 | static int sctp_getsockopt_peer_addrs(struct sock *sk, int len, |
4352 | char __user *optval, int __user *optlen) | 4352 | char __user *optval, int __user *optlen) |
4353 | { | 4353 | { |
4354 | struct sctp_association *asoc; | 4354 | struct sctp_association *asoc; |
4355 | int cnt = 0; | 4355 | int cnt = 0; |
4356 | struct sctp_getaddrs getaddrs; | 4356 | struct sctp_getaddrs getaddrs; |
4357 | struct sctp_transport *from; | 4357 | struct sctp_transport *from; |
4358 | void __user *to; | 4358 | void __user *to; |
4359 | union sctp_addr temp; | 4359 | union sctp_addr temp; |
4360 | struct sctp_sock *sp = sctp_sk(sk); | 4360 | struct sctp_sock *sp = sctp_sk(sk); |
4361 | int addrlen; | 4361 | int addrlen; |
4362 | size_t space_left; | 4362 | size_t space_left; |
4363 | int bytes_copied; | 4363 | int bytes_copied; |
4364 | 4364 | ||
4365 | if (len < sizeof(struct sctp_getaddrs)) | 4365 | if (len < sizeof(struct sctp_getaddrs)) |
4366 | return -EINVAL; | 4366 | return -EINVAL; |
4367 | 4367 | ||
4368 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 4368 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
4369 | return -EFAULT; | 4369 | return -EFAULT; |
4370 | 4370 | ||
4371 | /* For UDP-style sockets, id specifies the association to query. */ | 4371 | /* For UDP-style sockets, id specifies the association to query. */ |
4372 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 4372 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
4373 | if (!asoc) | 4373 | if (!asoc) |
4374 | return -EINVAL; | 4374 | return -EINVAL; |
4375 | 4375 | ||
4376 | to = optval + offsetof(struct sctp_getaddrs,addrs); | 4376 | to = optval + offsetof(struct sctp_getaddrs,addrs); |
4377 | space_left = len - offsetof(struct sctp_getaddrs,addrs); | 4377 | space_left = len - offsetof(struct sctp_getaddrs,addrs); |
4378 | 4378 | ||
4379 | list_for_each_entry(from, &asoc->peer.transport_addr_list, | 4379 | list_for_each_entry(from, &asoc->peer.transport_addr_list, |
4380 | transports) { | 4380 | transports) { |
4381 | memcpy(&temp, &from->ipaddr, sizeof(temp)); | 4381 | memcpy(&temp, &from->ipaddr, sizeof(temp)); |
4382 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); | 4382 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
4383 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 4383 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
4384 | if (space_left < addrlen) | 4384 | if (space_left < addrlen) |
4385 | return -ENOMEM; | 4385 | return -ENOMEM; |
4386 | if (copy_to_user(to, &temp, addrlen)) | 4386 | if (copy_to_user(to, &temp, addrlen)) |
4387 | return -EFAULT; | 4387 | return -EFAULT; |
4388 | to += addrlen; | 4388 | to += addrlen; |
4389 | cnt++; | 4389 | cnt++; |
4390 | space_left -= addrlen; | 4390 | space_left -= addrlen; |
4391 | } | 4391 | } |
4392 | 4392 | ||
4393 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) | 4393 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) |
4394 | return -EFAULT; | 4394 | return -EFAULT; |
4395 | bytes_copied = ((char __user *)to) - optval; | 4395 | bytes_copied = ((char __user *)to) - optval; |
4396 | if (put_user(bytes_copied, optlen)) | 4396 | if (put_user(bytes_copied, optlen)) |
4397 | return -EFAULT; | 4397 | return -EFAULT; |
4398 | 4398 | ||
4399 | return 0; | 4399 | return 0; |
4400 | } | 4400 | } |
4401 | 4401 | ||
4402 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, | 4402 | static int sctp_copy_laddrs(struct sock *sk, __u16 port, void *to, |
4403 | size_t space_left, int *bytes_copied) | 4403 | size_t space_left, int *bytes_copied) |
4404 | { | 4404 | { |
4405 | struct sctp_sockaddr_entry *addr; | 4405 | struct sctp_sockaddr_entry *addr; |
4406 | union sctp_addr temp; | 4406 | union sctp_addr temp; |
4407 | int cnt = 0; | 4407 | int cnt = 0; |
4408 | int addrlen; | 4408 | int addrlen; |
4409 | 4409 | ||
4410 | rcu_read_lock(); | 4410 | rcu_read_lock(); |
4411 | list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) { | 4411 | list_for_each_entry_rcu(addr, &sctp_local_addr_list, list) { |
4412 | if (!addr->valid) | 4412 | if (!addr->valid) |
4413 | continue; | 4413 | continue; |
4414 | 4414 | ||
4415 | if ((PF_INET == sk->sk_family) && | 4415 | if ((PF_INET == sk->sk_family) && |
4416 | (AF_INET6 == addr->a.sa.sa_family)) | 4416 | (AF_INET6 == addr->a.sa.sa_family)) |
4417 | continue; | 4417 | continue; |
4418 | if ((PF_INET6 == sk->sk_family) && | 4418 | if ((PF_INET6 == sk->sk_family) && |
4419 | inet_v6_ipv6only(sk) && | 4419 | inet_v6_ipv6only(sk) && |
4420 | (AF_INET == addr->a.sa.sa_family)) | 4420 | (AF_INET == addr->a.sa.sa_family)) |
4421 | continue; | 4421 | continue; |
4422 | memcpy(&temp, &addr->a, sizeof(temp)); | 4422 | memcpy(&temp, &addr->a, sizeof(temp)); |
4423 | if (!temp.v4.sin_port) | 4423 | if (!temp.v4.sin_port) |
4424 | temp.v4.sin_port = htons(port); | 4424 | temp.v4.sin_port = htons(port); |
4425 | 4425 | ||
4426 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), | 4426 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), |
4427 | &temp); | 4427 | &temp); |
4428 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 4428 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
4429 | if (space_left < addrlen) { | 4429 | if (space_left < addrlen) { |
4430 | cnt = -ENOMEM; | 4430 | cnt = -ENOMEM; |
4431 | break; | 4431 | break; |
4432 | } | 4432 | } |
4433 | memcpy(to, &temp, addrlen); | 4433 | memcpy(to, &temp, addrlen); |
4434 | 4434 | ||
4435 | to += addrlen; | 4435 | to += addrlen; |
4436 | cnt ++; | 4436 | cnt ++; |
4437 | space_left -= addrlen; | 4437 | space_left -= addrlen; |
4438 | *bytes_copied += addrlen; | 4438 | *bytes_copied += addrlen; |
4439 | } | 4439 | } |
4440 | rcu_read_unlock(); | 4440 | rcu_read_unlock(); |
4441 | 4441 | ||
4442 | return cnt; | 4442 | return cnt; |
4443 | } | 4443 | } |
4444 | 4444 | ||
4445 | 4445 | ||
4446 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, | 4446 | static int sctp_getsockopt_local_addrs(struct sock *sk, int len, |
4447 | char __user *optval, int __user *optlen) | 4447 | char __user *optval, int __user *optlen) |
4448 | { | 4448 | { |
4449 | struct sctp_bind_addr *bp; | 4449 | struct sctp_bind_addr *bp; |
4450 | struct sctp_association *asoc; | 4450 | struct sctp_association *asoc; |
4451 | int cnt = 0; | 4451 | int cnt = 0; |
4452 | struct sctp_getaddrs getaddrs; | 4452 | struct sctp_getaddrs getaddrs; |
4453 | struct sctp_sockaddr_entry *addr; | 4453 | struct sctp_sockaddr_entry *addr; |
4454 | void __user *to; | 4454 | void __user *to; |
4455 | union sctp_addr temp; | 4455 | union sctp_addr temp; |
4456 | struct sctp_sock *sp = sctp_sk(sk); | 4456 | struct sctp_sock *sp = sctp_sk(sk); |
4457 | int addrlen; | 4457 | int addrlen; |
4458 | int err = 0; | 4458 | int err = 0; |
4459 | size_t space_left; | 4459 | size_t space_left; |
4460 | int bytes_copied = 0; | 4460 | int bytes_copied = 0; |
4461 | void *addrs; | 4461 | void *addrs; |
4462 | void *buf; | 4462 | void *buf; |
4463 | 4463 | ||
4464 | if (len < sizeof(struct sctp_getaddrs)) | 4464 | if (len < sizeof(struct sctp_getaddrs)) |
4465 | return -EINVAL; | 4465 | return -EINVAL; |
4466 | 4466 | ||
4467 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) | 4467 | if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs))) |
4468 | return -EFAULT; | 4468 | return -EFAULT; |
4469 | 4469 | ||
4470 | /* | 4470 | /* |
4471 | * For UDP-style sockets, id specifies the association to query. | 4471 | * For UDP-style sockets, id specifies the association to query. |
4472 | * If the id field is set to the value '0' then the locally bound | 4472 | * If the id field is set to the value '0' then the locally bound |
4473 | * addresses are returned without regard to any particular | 4473 | * addresses are returned without regard to any particular |
4474 | * association. | 4474 | * association. |
4475 | */ | 4475 | */ |
4476 | if (0 == getaddrs.assoc_id) { | 4476 | if (0 == getaddrs.assoc_id) { |
4477 | bp = &sctp_sk(sk)->ep->base.bind_addr; | 4477 | bp = &sctp_sk(sk)->ep->base.bind_addr; |
4478 | } else { | 4478 | } else { |
4479 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); | 4479 | asoc = sctp_id2assoc(sk, getaddrs.assoc_id); |
4480 | if (!asoc) | 4480 | if (!asoc) |
4481 | return -EINVAL; | 4481 | return -EINVAL; |
4482 | bp = &asoc->base.bind_addr; | 4482 | bp = &asoc->base.bind_addr; |
4483 | } | 4483 | } |
4484 | 4484 | ||
4485 | to = optval + offsetof(struct sctp_getaddrs,addrs); | 4485 | to = optval + offsetof(struct sctp_getaddrs,addrs); |
4486 | space_left = len - offsetof(struct sctp_getaddrs,addrs); | 4486 | space_left = len - offsetof(struct sctp_getaddrs,addrs); |
4487 | 4487 | ||
4488 | addrs = kmalloc(space_left, GFP_KERNEL); | 4488 | addrs = kmalloc(space_left, GFP_KERNEL); |
4489 | if (!addrs) | 4489 | if (!addrs) |
4490 | return -ENOMEM; | 4490 | return -ENOMEM; |
4491 | 4491 | ||
4492 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid | 4492 | /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid |
4493 | * addresses from the global local address list. | 4493 | * addresses from the global local address list. |
4494 | */ | 4494 | */ |
4495 | if (sctp_list_single_entry(&bp->address_list)) { | 4495 | if (sctp_list_single_entry(&bp->address_list)) { |
4496 | addr = list_entry(bp->address_list.next, | 4496 | addr = list_entry(bp->address_list.next, |
4497 | struct sctp_sockaddr_entry, list); | 4497 | struct sctp_sockaddr_entry, list); |
4498 | if (sctp_is_any(sk, &addr->a)) { | 4498 | if (sctp_is_any(sk, &addr->a)) { |
4499 | cnt = sctp_copy_laddrs(sk, bp->port, addrs, | 4499 | cnt = sctp_copy_laddrs(sk, bp->port, addrs, |
4500 | space_left, &bytes_copied); | 4500 | space_left, &bytes_copied); |
4501 | if (cnt < 0) { | 4501 | if (cnt < 0) { |
4502 | err = cnt; | 4502 | err = cnt; |
4503 | goto out; | 4503 | goto out; |
4504 | } | 4504 | } |
4505 | goto copy_getaddrs; | 4505 | goto copy_getaddrs; |
4506 | } | 4506 | } |
4507 | } | 4507 | } |
4508 | 4508 | ||
4509 | buf = addrs; | 4509 | buf = addrs; |
4510 | /* Protection on the bound address list is not needed since | 4510 | /* Protection on the bound address list is not needed since |
4511 | * in the socket option context we hold a socket lock and | 4511 | * in the socket option context we hold a socket lock and |
4512 | * thus the bound address list can't change. | 4512 | * thus the bound address list can't change. |
4513 | */ | 4513 | */ |
4514 | list_for_each_entry(addr, &bp->address_list, list) { | 4514 | list_for_each_entry(addr, &bp->address_list, list) { |
4515 | memcpy(&temp, &addr->a, sizeof(temp)); | 4515 | memcpy(&temp, &addr->a, sizeof(temp)); |
4516 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); | 4516 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp); |
4517 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; | 4517 | addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; |
4518 | if (space_left < addrlen) { | 4518 | if (space_left < addrlen) { |
4519 | err = -ENOMEM; /*fixme: right error?*/ | 4519 | err = -ENOMEM; /*fixme: right error?*/ |
4520 | goto out; | 4520 | goto out; |
4521 | } | 4521 | } |
4522 | memcpy(buf, &temp, addrlen); | 4522 | memcpy(buf, &temp, addrlen); |
4523 | buf += addrlen; | 4523 | buf += addrlen; |
4524 | bytes_copied += addrlen; | 4524 | bytes_copied += addrlen; |
4525 | cnt ++; | 4525 | cnt ++; |
4526 | space_left -= addrlen; | 4526 | space_left -= addrlen; |
4527 | } | 4527 | } |
4528 | 4528 | ||
4529 | copy_getaddrs: | 4529 | copy_getaddrs: |
4530 | if (copy_to_user(to, addrs, bytes_copied)) { | 4530 | if (copy_to_user(to, addrs, bytes_copied)) { |
4531 | err = -EFAULT; | 4531 | err = -EFAULT; |
4532 | goto out; | 4532 | goto out; |
4533 | } | 4533 | } |
4534 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { | 4534 | if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num)) { |
4535 | err = -EFAULT; | 4535 | err = -EFAULT; |
4536 | goto out; | 4536 | goto out; |
4537 | } | 4537 | } |
4538 | if (put_user(bytes_copied, optlen)) | 4538 | if (put_user(bytes_copied, optlen)) |
4539 | err = -EFAULT; | 4539 | err = -EFAULT; |
4540 | out: | 4540 | out: |
4541 | kfree(addrs); | 4541 | kfree(addrs); |
4542 | return err; | 4542 | return err; |
4543 | } | 4543 | } |
4544 | 4544 | ||
4545 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) | 4545 | /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR) |
4546 | * | 4546 | * |
4547 | * Requests that the local SCTP stack use the enclosed peer address as | 4547 | * Requests that the local SCTP stack use the enclosed peer address as |
4548 | * the association primary. The enclosed address must be one of the | 4548 | * the association primary. The enclosed address must be one of the |
4549 | * association peer's addresses. | 4549 | * association peer's addresses. |
4550 | */ | 4550 | */ |
4551 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, | 4551 | static int sctp_getsockopt_primary_addr(struct sock *sk, int len, |
4552 | char __user *optval, int __user *optlen) | 4552 | char __user *optval, int __user *optlen) |
4553 | { | 4553 | { |
4554 | struct sctp_prim prim; | 4554 | struct sctp_prim prim; |
4555 | struct sctp_association *asoc; | 4555 | struct sctp_association *asoc; |
4556 | struct sctp_sock *sp = sctp_sk(sk); | 4556 | struct sctp_sock *sp = sctp_sk(sk); |
4557 | 4557 | ||
4558 | if (len < sizeof(struct sctp_prim)) | 4558 | if (len < sizeof(struct sctp_prim)) |
4559 | return -EINVAL; | 4559 | return -EINVAL; |
4560 | 4560 | ||
4561 | len = sizeof(struct sctp_prim); | 4561 | len = sizeof(struct sctp_prim); |
4562 | 4562 | ||
4563 | if (copy_from_user(&prim, optval, len)) | 4563 | if (copy_from_user(&prim, optval, len)) |
4564 | return -EFAULT; | 4564 | return -EFAULT; |
4565 | 4565 | ||
4566 | asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); | 4566 | asoc = sctp_id2assoc(sk, prim.ssp_assoc_id); |
4567 | if (!asoc) | 4567 | if (!asoc) |
4568 | return -EINVAL; | 4568 | return -EINVAL; |
4569 | 4569 | ||
4570 | if (!asoc->peer.primary_path) | 4570 | if (!asoc->peer.primary_path) |
4571 | return -ENOTCONN; | 4571 | return -ENOTCONN; |
4572 | 4572 | ||
4573 | memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, | 4573 | memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr, |
4574 | asoc->peer.primary_path->af_specific->sockaddr_len); | 4574 | asoc->peer.primary_path->af_specific->sockaddr_len); |
4575 | 4575 | ||
4576 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, | 4576 | sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, |
4577 | (union sctp_addr *)&prim.ssp_addr); | 4577 | (union sctp_addr *)&prim.ssp_addr); |
4578 | 4578 | ||
4579 | if (put_user(len, optlen)) | 4579 | if (put_user(len, optlen)) |
4580 | return -EFAULT; | 4580 | return -EFAULT; |
4581 | if (copy_to_user(optval, &prim, len)) | 4581 | if (copy_to_user(optval, &prim, len)) |
4582 | return -EFAULT; | 4582 | return -EFAULT; |
4583 | 4583 | ||
4584 | return 0; | 4584 | return 0; |
4585 | } | 4585 | } |
4586 | 4586 | ||
4587 | /* | 4587 | /* |
4588 | * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) | 4588 | * 7.1.11 Set Adaptation Layer Indicator (SCTP_ADAPTATION_LAYER) |
4589 | * | 4589 | * |
4590 | * Requests that the local endpoint set the specified Adaptation Layer | 4590 | * Requests that the local endpoint set the specified Adaptation Layer |
4591 | * Indication parameter for all future INIT and INIT-ACK exchanges. | 4591 | * Indication parameter for all future INIT and INIT-ACK exchanges. |
4592 | */ | 4592 | */ |
4593 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, | 4593 | static int sctp_getsockopt_adaptation_layer(struct sock *sk, int len, |
4594 | char __user *optval, int __user *optlen) | 4594 | char __user *optval, int __user *optlen) |
4595 | { | 4595 | { |
4596 | struct sctp_setadaptation adaptation; | 4596 | struct sctp_setadaptation adaptation; |
4597 | 4597 | ||
4598 | if (len < sizeof(struct sctp_setadaptation)) | 4598 | if (len < sizeof(struct sctp_setadaptation)) |
4599 | return -EINVAL; | 4599 | return -EINVAL; |
4600 | 4600 | ||
4601 | len = sizeof(struct sctp_setadaptation); | 4601 | len = sizeof(struct sctp_setadaptation); |
4602 | 4602 | ||
4603 | adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; | 4603 | adaptation.ssb_adaptation_ind = sctp_sk(sk)->adaptation_ind; |
4604 | 4604 | ||
4605 | if (put_user(len, optlen)) | 4605 | if (put_user(len, optlen)) |
4606 | return -EFAULT; | 4606 | return -EFAULT; |
4607 | if (copy_to_user(optval, &adaptation, len)) | 4607 | if (copy_to_user(optval, &adaptation, len)) |
4608 | return -EFAULT; | 4608 | return -EFAULT; |
4609 | 4609 | ||
4610 | return 0; | 4610 | return 0; |
4611 | } | 4611 | } |
4612 | 4612 | ||
4613 | /* | 4613 | /* |
4614 | * | 4614 | * |
4615 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) | 4615 | * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM) |
4616 | * | 4616 | * |
4617 | * Applications that wish to use the sendto() system call may wish to | 4617 | * Applications that wish to use the sendto() system call may wish to |
4618 | * specify a default set of parameters that would normally be supplied | 4618 | * specify a default set of parameters that would normally be supplied |
4619 | * through the inclusion of ancillary data. This socket option allows | 4619 | * through the inclusion of ancillary data. This socket option allows |
4620 | * such an application to set the default sctp_sndrcvinfo structure. | 4620 | * such an application to set the default sctp_sndrcvinfo structure. |
4621 | 4621 | ||
4622 | 4622 | ||
4623 | * The application that wishes to use this socket option simply passes | 4623 | * The application that wishes to use this socket option simply passes |
4624 | * in to this call the sctp_sndrcvinfo structure defined in Section | 4624 | * in to this call the sctp_sndrcvinfo structure defined in Section |
4625 | * 5.2.2) The input parameters accepted by this call include | 4625 | * 5.2.2) The input parameters accepted by this call include |
4626 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, | 4626 | * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context, |
4627 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in | 4627 | * sinfo_timetolive. The user must provide the sinfo_assoc_id field in |
4628 | * to this call if the caller is using the UDP model. | 4628 | * to this call if the caller is using the UDP model. |
4629 | * | 4629 | * |
4630 | * For getsockopt, it get the default sctp_sndrcvinfo structure. | 4630 | * For getsockopt, it get the default sctp_sndrcvinfo structure. |
4631 | */ | 4631 | */ |
4632 | static int sctp_getsockopt_default_send_param(struct sock *sk, | 4632 | static int sctp_getsockopt_default_send_param(struct sock *sk, |
4633 | int len, char __user *optval, | 4633 | int len, char __user *optval, |
4634 | int __user *optlen) | 4634 | int __user *optlen) |
4635 | { | 4635 | { |
4636 | struct sctp_sndrcvinfo info; | 4636 | struct sctp_sndrcvinfo info; |
4637 | struct sctp_association *asoc; | 4637 | struct sctp_association *asoc; |
4638 | struct sctp_sock *sp = sctp_sk(sk); | 4638 | struct sctp_sock *sp = sctp_sk(sk); |
4639 | 4639 | ||
4640 | if (len < sizeof(struct sctp_sndrcvinfo)) | 4640 | if (len < sizeof(struct sctp_sndrcvinfo)) |
4641 | return -EINVAL; | 4641 | return -EINVAL; |
4642 | 4642 | ||
4643 | len = sizeof(struct sctp_sndrcvinfo); | 4643 | len = sizeof(struct sctp_sndrcvinfo); |
4644 | 4644 | ||
4645 | if (copy_from_user(&info, optval, len)) | 4645 | if (copy_from_user(&info, optval, len)) |
4646 | return -EFAULT; | 4646 | return -EFAULT; |
4647 | 4647 | ||
4648 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); | 4648 | asoc = sctp_id2assoc(sk, info.sinfo_assoc_id); |
4649 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) | 4649 | if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP)) |
4650 | return -EINVAL; | 4650 | return -EINVAL; |
4651 | 4651 | ||
4652 | if (asoc) { | 4652 | if (asoc) { |
4653 | info.sinfo_stream = asoc->default_stream; | 4653 | info.sinfo_stream = asoc->default_stream; |
4654 | info.sinfo_flags = asoc->default_flags; | 4654 | info.sinfo_flags = asoc->default_flags; |
4655 | info.sinfo_ppid = asoc->default_ppid; | 4655 | info.sinfo_ppid = asoc->default_ppid; |
4656 | info.sinfo_context = asoc->default_context; | 4656 | info.sinfo_context = asoc->default_context; |
4657 | info.sinfo_timetolive = asoc->default_timetolive; | 4657 | info.sinfo_timetolive = asoc->default_timetolive; |
4658 | } else { | 4658 | } else { |
4659 | info.sinfo_stream = sp->default_stream; | 4659 | info.sinfo_stream = sp->default_stream; |
4660 | info.sinfo_flags = sp->default_flags; | 4660 | info.sinfo_flags = sp->default_flags; |
4661 | info.sinfo_ppid = sp->default_ppid; | 4661 | info.sinfo_ppid = sp->default_ppid; |
4662 | info.sinfo_context = sp->default_context; | 4662 | info.sinfo_context = sp->default_context; |
4663 | info.sinfo_timetolive = sp->default_timetolive; | 4663 | info.sinfo_timetolive = sp->default_timetolive; |
4664 | } | 4664 | } |
4665 | 4665 | ||
4666 | if (put_user(len, optlen)) | 4666 | if (put_user(len, optlen)) |
4667 | return -EFAULT; | 4667 | return -EFAULT; |
4668 | if (copy_to_user(optval, &info, len)) | 4668 | if (copy_to_user(optval, &info, len)) |
4669 | return -EFAULT; | 4669 | return -EFAULT; |
4670 | 4670 | ||
4671 | return 0; | 4671 | return 0; |
4672 | } | 4672 | } |
4673 | 4673 | ||
4674 | /* | 4674 | /* |
4675 | * | 4675 | * |
4676 | * 7.1.5 SCTP_NODELAY | 4676 | * 7.1.5 SCTP_NODELAY |
4677 | * | 4677 | * |
4678 | * Turn on/off any Nagle-like algorithm. This means that packets are | 4678 | * Turn on/off any Nagle-like algorithm. This means that packets are |
4679 | * generally sent as soon as possible and no unnecessary delays are | 4679 | * generally sent as soon as possible and no unnecessary delays are |
4680 | * introduced, at the cost of more packets in the network. Expects an | 4680 | * introduced, at the cost of more packets in the network. Expects an |
4681 | * integer boolean flag. | 4681 | * integer boolean flag. |
4682 | */ | 4682 | */ |
4683 | 4683 | ||
4684 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, | 4684 | static int sctp_getsockopt_nodelay(struct sock *sk, int len, |
4685 | char __user *optval, int __user *optlen) | 4685 | char __user *optval, int __user *optlen) |
4686 | { | 4686 | { |
4687 | int val; | 4687 | int val; |
4688 | 4688 | ||
4689 | if (len < sizeof(int)) | 4689 | if (len < sizeof(int)) |
4690 | return -EINVAL; | 4690 | return -EINVAL; |
4691 | 4691 | ||
4692 | len = sizeof(int); | 4692 | len = sizeof(int); |
4693 | val = (sctp_sk(sk)->nodelay == 1); | 4693 | val = (sctp_sk(sk)->nodelay == 1); |
4694 | if (put_user(len, optlen)) | 4694 | if (put_user(len, optlen)) |
4695 | return -EFAULT; | 4695 | return -EFAULT; |
4696 | if (copy_to_user(optval, &val, len)) | 4696 | if (copy_to_user(optval, &val, len)) |
4697 | return -EFAULT; | 4697 | return -EFAULT; |
4698 | return 0; | 4698 | return 0; |
4699 | } | 4699 | } |
4700 | 4700 | ||
4701 | /* | 4701 | /* |
4702 | * | 4702 | * |
4703 | * 7.1.1 SCTP_RTOINFO | 4703 | * 7.1.1 SCTP_RTOINFO |
4704 | * | 4704 | * |
4705 | * The protocol parameters used to initialize and bound retransmission | 4705 | * The protocol parameters used to initialize and bound retransmission |
4706 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access | 4706 | * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access |
4707 | * and modify these parameters. | 4707 | * and modify these parameters. |
4708 | * All parameters are time values, in milliseconds. A value of 0, when | 4708 | * All parameters are time values, in milliseconds. A value of 0, when |
4709 | * modifying the parameters, indicates that the current value should not | 4709 | * modifying the parameters, indicates that the current value should not |
4710 | * be changed. | 4710 | * be changed. |
4711 | * | 4711 | * |
4712 | */ | 4712 | */ |
4713 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, | 4713 | static int sctp_getsockopt_rtoinfo(struct sock *sk, int len, |
4714 | char __user *optval, | 4714 | char __user *optval, |
4715 | int __user *optlen) { | 4715 | int __user *optlen) { |
4716 | struct sctp_rtoinfo rtoinfo; | 4716 | struct sctp_rtoinfo rtoinfo; |
4717 | struct sctp_association *asoc; | 4717 | struct sctp_association *asoc; |
4718 | 4718 | ||
4719 | if (len < sizeof (struct sctp_rtoinfo)) | 4719 | if (len < sizeof (struct sctp_rtoinfo)) |
4720 | return -EINVAL; | 4720 | return -EINVAL; |
4721 | 4721 | ||
4722 | len = sizeof(struct sctp_rtoinfo); | 4722 | len = sizeof(struct sctp_rtoinfo); |
4723 | 4723 | ||
4724 | if (copy_from_user(&rtoinfo, optval, len)) | 4724 | if (copy_from_user(&rtoinfo, optval, len)) |
4725 | return -EFAULT; | 4725 | return -EFAULT; |
4726 | 4726 | ||
4727 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); | 4727 | asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id); |
4728 | 4728 | ||
4729 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) | 4729 | if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) |
4730 | return -EINVAL; | 4730 | return -EINVAL; |
4731 | 4731 | ||
4732 | /* Values corresponding to the specific association. */ | 4732 | /* Values corresponding to the specific association. */ |
4733 | if (asoc) { | 4733 | if (asoc) { |
4734 | rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); | 4734 | rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial); |
4735 | rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); | 4735 | rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max); |
4736 | rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); | 4736 | rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min); |
4737 | } else { | 4737 | } else { |
4738 | /* Values corresponding to the endpoint. */ | 4738 | /* Values corresponding to the endpoint. */ |
4739 | struct sctp_sock *sp = sctp_sk(sk); | 4739 | struct sctp_sock *sp = sctp_sk(sk); |
4740 | 4740 | ||
4741 | rtoinfo.srto_initial = sp->rtoinfo.srto_initial; | 4741 | rtoinfo.srto_initial = sp->rtoinfo.srto_initial; |
4742 | rtoinfo.srto_max = sp->rtoinfo.srto_max; | 4742 | rtoinfo.srto_max = sp->rtoinfo.srto_max; |
4743 | rtoinfo.srto_min = sp->rtoinfo.srto_min; | 4743 | rtoinfo.srto_min = sp->rtoinfo.srto_min; |
4744 | } | 4744 | } |
4745 | 4745 | ||
4746 | if (put_user(len, optlen)) | 4746 | if (put_user(len, optlen)) |
4747 | return -EFAULT; | 4747 | return -EFAULT; |
4748 | 4748 | ||
4749 | if (copy_to_user(optval, &rtoinfo, len)) | 4749 | if (copy_to_user(optval, &rtoinfo, len)) |
4750 | return -EFAULT; | 4750 | return -EFAULT; |
4751 | 4751 | ||
4752 | return 0; | 4752 | return 0; |
4753 | } | 4753 | } |
4754 | 4754 | ||
4755 | /* | 4755 | /* |
4756 | * | 4756 | * |
4757 | * 7.1.2 SCTP_ASSOCINFO | 4757 | * 7.1.2 SCTP_ASSOCINFO |
4758 | * | 4758 | * |
4759 | * This option is used to tune the maximum retransmission attempts | 4759 | * This option is used to tune the maximum retransmission attempts |
4760 | * of the association. | 4760 | * of the association. |
4761 | * Returns an error if the new association retransmission value is | 4761 | * Returns an error if the new association retransmission value is |
4762 | * greater than the sum of the retransmission value of the peer. | 4762 | * greater than the sum of the retransmission value of the peer. |
4763 | * See [SCTP] for more information. | 4763 | * See [SCTP] for more information. |
4764 | * | 4764 | * |
4765 | */ | 4765 | */ |
4766 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, | 4766 | static int sctp_getsockopt_associnfo(struct sock *sk, int len, |
4767 | char __user *optval, | 4767 | char __user *optval, |
4768 | int __user *optlen) | 4768 | int __user *optlen) |
4769 | { | 4769 | { |
4770 | 4770 | ||
4771 | struct sctp_assocparams assocparams; | 4771 | struct sctp_assocparams assocparams; |
4772 | struct sctp_association *asoc; | 4772 | struct sctp_association *asoc; |
4773 | struct list_head *pos; | 4773 | struct list_head *pos; |
4774 | int cnt = 0; | 4774 | int cnt = 0; |
4775 | 4775 | ||
4776 | if (len < sizeof (struct sctp_assocparams)) | 4776 | if (len < sizeof (struct sctp_assocparams)) |
4777 | return -EINVAL; | 4777 | return -EINVAL; |
4778 | 4778 | ||
4779 | len = sizeof(struct sctp_assocparams); | 4779 | len = sizeof(struct sctp_assocparams); |
4780 | 4780 | ||
4781 | if (copy_from_user(&assocparams, optval, len)) | 4781 | if (copy_from_user(&assocparams, optval, len)) |
4782 | return -EFAULT; | 4782 | return -EFAULT; |
4783 | 4783 | ||
4784 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); | 4784 | asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id); |
4785 | 4785 | ||
4786 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) | 4786 | if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP)) |
4787 | return -EINVAL; | 4787 | return -EINVAL; |
4788 | 4788 | ||
4789 | /* Values correspoinding to the specific association */ | 4789 | /* Values correspoinding to the specific association */ |
4790 | if (asoc) { | 4790 | if (asoc) { |
4791 | assocparams.sasoc_asocmaxrxt = asoc->max_retrans; | 4791 | assocparams.sasoc_asocmaxrxt = asoc->max_retrans; |
4792 | assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; | 4792 | assocparams.sasoc_peer_rwnd = asoc->peer.rwnd; |
4793 | assocparams.sasoc_local_rwnd = asoc->a_rwnd; | 4793 | assocparams.sasoc_local_rwnd = asoc->a_rwnd; |
4794 | assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec | 4794 | assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec |
4795 | * 1000) + | 4795 | * 1000) + |
4796 | (asoc->cookie_life.tv_usec | 4796 | (asoc->cookie_life.tv_usec |
4797 | / 1000); | 4797 | / 1000); |
4798 | 4798 | ||
4799 | list_for_each(pos, &asoc->peer.transport_addr_list) { | 4799 | list_for_each(pos, &asoc->peer.transport_addr_list) { |
4800 | cnt ++; | 4800 | cnt ++; |
4801 | } | 4801 | } |
4802 | 4802 | ||
4803 | assocparams.sasoc_number_peer_destinations = cnt; | 4803 | assocparams.sasoc_number_peer_destinations = cnt; |
4804 | } else { | 4804 | } else { |
4805 | /* Values corresponding to the endpoint */ | 4805 | /* Values corresponding to the endpoint */ |
4806 | struct sctp_sock *sp = sctp_sk(sk); | 4806 | struct sctp_sock *sp = sctp_sk(sk); |
4807 | 4807 | ||
4808 | assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; | 4808 | assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt; |
4809 | assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; | 4809 | assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd; |
4810 | assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; | 4810 | assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd; |
4811 | assocparams.sasoc_cookie_life = | 4811 | assocparams.sasoc_cookie_life = |
4812 | sp->assocparams.sasoc_cookie_life; | 4812 | sp->assocparams.sasoc_cookie_life; |
4813 | assocparams.sasoc_number_peer_destinations = | 4813 | assocparams.sasoc_number_peer_destinations = |
4814 | sp->assocparams. | 4814 | sp->assocparams. |
4815 | sasoc_number_peer_destinations; | 4815 | sasoc_number_peer_destinations; |
4816 | } | 4816 | } |
4817 | 4817 | ||
4818 | if (put_user(len, optlen)) | 4818 | if (put_user(len, optlen)) |
4819 | return -EFAULT; | 4819 | return -EFAULT; |
4820 | 4820 | ||
4821 | if (copy_to_user(optval, &assocparams, len)) | 4821 | if (copy_to_user(optval, &assocparams, len)) |
4822 | return -EFAULT; | 4822 | return -EFAULT; |
4823 | 4823 | ||
4824 | return 0; | 4824 | return 0; |
4825 | } | 4825 | } |
4826 | 4826 | ||
4827 | /* | 4827 | /* |
4828 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) | 4828 | * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR) |
4829 | * | 4829 | * |
4830 | * This socket option is a boolean flag which turns on or off mapped V4 | 4830 | * This socket option is a boolean flag which turns on or off mapped V4 |
4831 | * addresses. If this option is turned on and the socket is type | 4831 | * addresses. If this option is turned on and the socket is type |
4832 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. | 4832 | * PF_INET6, then IPv4 addresses will be mapped to V6 representation. |
4833 | * If this option is turned off, then no mapping will be done of V4 | 4833 | * If this option is turned off, then no mapping will be done of V4 |
4834 | * addresses and a user will receive both PF_INET6 and PF_INET type | 4834 | * addresses and a user will receive both PF_INET6 and PF_INET type |
4835 | * addresses on the socket. | 4835 | * addresses on the socket. |
4836 | */ | 4836 | */ |
4837 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, | 4837 | static int sctp_getsockopt_mappedv4(struct sock *sk, int len, |
4838 | char __user *optval, int __user *optlen) | 4838 | char __user *optval, int __user *optlen) |
4839 | { | 4839 | { |
4840 | int val; | 4840 | int val; |
4841 | struct sctp_sock *sp = sctp_sk(sk); | 4841 | struct sctp_sock *sp = sctp_sk(sk); |
4842 | 4842 | ||
4843 | if (len < sizeof(int)) | 4843 | if (len < sizeof(int)) |
4844 | return -EINVAL; | 4844 | return -EINVAL; |
4845 | 4845 | ||
4846 | len = sizeof(int); | 4846 | len = sizeof(int); |
4847 | val = sp->v4mapped; | 4847 | val = sp->v4mapped; |
4848 | if (put_user(len, optlen)) | 4848 | if (put_user(len, optlen)) |
4849 | return -EFAULT; | 4849 | return -EFAULT; |
4850 | if (copy_to_user(optval, &val, len)) | 4850 | if (copy_to_user(optval, &val, len)) |
4851 | return -EFAULT; | 4851 | return -EFAULT; |
4852 | 4852 | ||
4853 | return 0; | 4853 | return 0; |
4854 | } | 4854 | } |
4855 | 4855 | ||
4856 | /* | 4856 | /* |
4857 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) | 4857 | * 7.1.29. Set or Get the default context (SCTP_CONTEXT) |
4858 | * (chapter and verse is quoted at sctp_setsockopt_context()) | 4858 | * (chapter and verse is quoted at sctp_setsockopt_context()) |
4859 | */ | 4859 | */ |
4860 | static int sctp_getsockopt_context(struct sock *sk, int len, | 4860 | static int sctp_getsockopt_context(struct sock *sk, int len, |
4861 | char __user *optval, int __user *optlen) | 4861 | char __user *optval, int __user *optlen) |
4862 | { | 4862 | { |
4863 | struct sctp_assoc_value params; | 4863 | struct sctp_assoc_value params; |
4864 | struct sctp_sock *sp; | 4864 | struct sctp_sock *sp; |
4865 | struct sctp_association *asoc; | 4865 | struct sctp_association *asoc; |
4866 | 4866 | ||
4867 | if (len < sizeof(struct sctp_assoc_value)) | 4867 | if (len < sizeof(struct sctp_assoc_value)) |
4868 | return -EINVAL; | 4868 | return -EINVAL; |
4869 | 4869 | ||
4870 | len = sizeof(struct sctp_assoc_value); | 4870 | len = sizeof(struct sctp_assoc_value); |
4871 | 4871 | ||
4872 | if (copy_from_user(¶ms, optval, len)) | 4872 | if (copy_from_user(¶ms, optval, len)) |
4873 | return -EFAULT; | 4873 | return -EFAULT; |
4874 | 4874 | ||
4875 | sp = sctp_sk(sk); | 4875 | sp = sctp_sk(sk); |
4876 | 4876 | ||
4877 | if (params.assoc_id != 0) { | 4877 | if (params.assoc_id != 0) { |
4878 | asoc = sctp_id2assoc(sk, params.assoc_id); | 4878 | asoc = sctp_id2assoc(sk, params.assoc_id); |
4879 | if (!asoc) | 4879 | if (!asoc) |
4880 | return -EINVAL; | 4880 | return -EINVAL; |
4881 | params.assoc_value = asoc->default_rcv_context; | 4881 | params.assoc_value = asoc->default_rcv_context; |
4882 | } else { | 4882 | } else { |
4883 | params.assoc_value = sp->default_rcv_context; | 4883 | params.assoc_value = sp->default_rcv_context; |
4884 | } | 4884 | } |
4885 | 4885 | ||
4886 | if (put_user(len, optlen)) | 4886 | if (put_user(len, optlen)) |
4887 | return -EFAULT; | 4887 | return -EFAULT; |
4888 | if (copy_to_user(optval, ¶ms, len)) | 4888 | if (copy_to_user(optval, ¶ms, len)) |
4889 | return -EFAULT; | 4889 | return -EFAULT; |
4890 | 4890 | ||
4891 | return 0; | 4891 | return 0; |
4892 | } | 4892 | } |
4893 | 4893 | ||
4894 | /* | 4894 | /* |
4895 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) | 4895 | * 8.1.16. Get or Set the Maximum Fragmentation Size (SCTP_MAXSEG) |
4896 | * This option will get or set the maximum size to put in any outgoing | 4896 | * This option will get or set the maximum size to put in any outgoing |
4897 | * SCTP DATA chunk. If a message is larger than this size it will be | 4897 | * SCTP DATA chunk. If a message is larger than this size it will be |
4898 | * fragmented by SCTP into the specified size. Note that the underlying | 4898 | * fragmented by SCTP into the specified size. Note that the underlying |
4899 | * SCTP implementation may fragment into smaller sized chunks when the | 4899 | * SCTP implementation may fragment into smaller sized chunks when the |
4900 | * PMTU of the underlying association is smaller than the value set by | 4900 | * PMTU of the underlying association is smaller than the value set by |
4901 | * the user. The default value for this option is '0' which indicates | 4901 | * the user. The default value for this option is '0' which indicates |
4902 | * the user is NOT limiting fragmentation and only the PMTU will effect | 4902 | * the user is NOT limiting fragmentation and only the PMTU will effect |
4903 | * SCTP's choice of DATA chunk size. Note also that values set larger | 4903 | * SCTP's choice of DATA chunk size. Note also that values set larger |
4904 | * than the maximum size of an IP datagram will effectively let SCTP | 4904 | * than the maximum size of an IP datagram will effectively let SCTP |
4905 | * control fragmentation (i.e. the same as setting this option to 0). | 4905 | * control fragmentation (i.e. the same as setting this option to 0). |
4906 | * | 4906 | * |
4907 | * The following structure is used to access and modify this parameter: | 4907 | * The following structure is used to access and modify this parameter: |
4908 | * | 4908 | * |
4909 | * struct sctp_assoc_value { | 4909 | * struct sctp_assoc_value { |
4910 | * sctp_assoc_t assoc_id; | 4910 | * sctp_assoc_t assoc_id; |
4911 | * uint32_t assoc_value; | 4911 | * uint32_t assoc_value; |
4912 | * }; | 4912 | * }; |
4913 | * | 4913 | * |
4914 | * assoc_id: This parameter is ignored for one-to-one style sockets. | 4914 | * assoc_id: This parameter is ignored for one-to-one style sockets. |
4915 | * For one-to-many style sockets this parameter indicates which | 4915 | * For one-to-many style sockets this parameter indicates which |
4916 | * association the user is performing an action upon. Note that if | 4916 | * association the user is performing an action upon. Note that if |
4917 | * this field's value is zero then the endpoints default value is | 4917 | * this field's value is zero then the endpoints default value is |
4918 | * changed (effecting future associations only). | 4918 | * changed (effecting future associations only). |
4919 | * assoc_value: This parameter specifies the maximum size in bytes. | 4919 | * assoc_value: This parameter specifies the maximum size in bytes. |
4920 | */ | 4920 | */ |
4921 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, | 4921 | static int sctp_getsockopt_maxseg(struct sock *sk, int len, |
4922 | char __user *optval, int __user *optlen) | 4922 | char __user *optval, int __user *optlen) |
4923 | { | 4923 | { |
4924 | struct sctp_assoc_value params; | 4924 | struct sctp_assoc_value params; |
4925 | struct sctp_association *asoc; | 4925 | struct sctp_association *asoc; |
4926 | 4926 | ||
4927 | if (len == sizeof(int)) { | 4927 | if (len == sizeof(int)) { |
4928 | pr_warn("Use of int in maxseg socket option deprecated\n"); | 4928 | pr_warn("Use of int in maxseg socket option deprecated\n"); |
4929 | pr_warn("Use struct sctp_assoc_value instead\n"); | 4929 | pr_warn("Use struct sctp_assoc_value instead\n"); |
4930 | params.assoc_id = 0; | 4930 | params.assoc_id = 0; |
4931 | } else if (len >= sizeof(struct sctp_assoc_value)) { | 4931 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
4932 | len = sizeof(struct sctp_assoc_value); | 4932 | len = sizeof(struct sctp_assoc_value); |
4933 | if (copy_from_user(¶ms, optval, sizeof(params))) | 4933 | if (copy_from_user(¶ms, optval, sizeof(params))) |
4934 | return -EFAULT; | 4934 | return -EFAULT; |
4935 | } else | 4935 | } else |
4936 | return -EINVAL; | 4936 | return -EINVAL; |
4937 | 4937 | ||
4938 | asoc = sctp_id2assoc(sk, params.assoc_id); | 4938 | asoc = sctp_id2assoc(sk, params.assoc_id); |
4939 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) | 4939 | if (!asoc && params.assoc_id && sctp_style(sk, UDP)) |
4940 | return -EINVAL; | 4940 | return -EINVAL; |
4941 | 4941 | ||
4942 | if (asoc) | 4942 | if (asoc) |
4943 | params.assoc_value = asoc->frag_point; | 4943 | params.assoc_value = asoc->frag_point; |
4944 | else | 4944 | else |
4945 | params.assoc_value = sctp_sk(sk)->user_frag; | 4945 | params.assoc_value = sctp_sk(sk)->user_frag; |
4946 | 4946 | ||
4947 | if (put_user(len, optlen)) | 4947 | if (put_user(len, optlen)) |
4948 | return -EFAULT; | 4948 | return -EFAULT; |
4949 | if (len == sizeof(int)) { | 4949 | if (len == sizeof(int)) { |
4950 | if (copy_to_user(optval, ¶ms.assoc_value, len)) | 4950 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
4951 | return -EFAULT; | 4951 | return -EFAULT; |
4952 | } else { | 4952 | } else { |
4953 | if (copy_to_user(optval, ¶ms, len)) | 4953 | if (copy_to_user(optval, ¶ms, len)) |
4954 | return -EFAULT; | 4954 | return -EFAULT; |
4955 | } | 4955 | } |
4956 | 4956 | ||
4957 | return 0; | 4957 | return 0; |
4958 | } | 4958 | } |
4959 | 4959 | ||
4960 | /* | 4960 | /* |
4961 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) | 4961 | * 7.1.24. Get or set fragmented interleave (SCTP_FRAGMENT_INTERLEAVE) |
4962 | * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) | 4962 | * (chapter and verse is quoted at sctp_setsockopt_fragment_interleave()) |
4963 | */ | 4963 | */ |
4964 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, | 4964 | static int sctp_getsockopt_fragment_interleave(struct sock *sk, int len, |
4965 | char __user *optval, int __user *optlen) | 4965 | char __user *optval, int __user *optlen) |
4966 | { | 4966 | { |
4967 | int val; | 4967 | int val; |
4968 | 4968 | ||
4969 | if (len < sizeof(int)) | 4969 | if (len < sizeof(int)) |
4970 | return -EINVAL; | 4970 | return -EINVAL; |
4971 | 4971 | ||
4972 | len = sizeof(int); | 4972 | len = sizeof(int); |
4973 | 4973 | ||
4974 | val = sctp_sk(sk)->frag_interleave; | 4974 | val = sctp_sk(sk)->frag_interleave; |
4975 | if (put_user(len, optlen)) | 4975 | if (put_user(len, optlen)) |
4976 | return -EFAULT; | 4976 | return -EFAULT; |
4977 | if (copy_to_user(optval, &val, len)) | 4977 | if (copy_to_user(optval, &val, len)) |
4978 | return -EFAULT; | 4978 | return -EFAULT; |
4979 | 4979 | ||
4980 | return 0; | 4980 | return 0; |
4981 | } | 4981 | } |
4982 | 4982 | ||
4983 | /* | 4983 | /* |
4984 | * 7.1.25. Set or Get the sctp partial delivery point | 4984 | * 7.1.25. Set or Get the sctp partial delivery point |
4985 | * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) | 4985 | * (chapter and verse is quoted at sctp_setsockopt_partial_delivery_point()) |
4986 | */ | 4986 | */ |
4987 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, | 4987 | static int sctp_getsockopt_partial_delivery_point(struct sock *sk, int len, |
4988 | char __user *optval, | 4988 | char __user *optval, |
4989 | int __user *optlen) | 4989 | int __user *optlen) |
4990 | { | 4990 | { |
4991 | u32 val; | 4991 | u32 val; |
4992 | 4992 | ||
4993 | if (len < sizeof(u32)) | 4993 | if (len < sizeof(u32)) |
4994 | return -EINVAL; | 4994 | return -EINVAL; |
4995 | 4995 | ||
4996 | len = sizeof(u32); | 4996 | len = sizeof(u32); |
4997 | 4997 | ||
4998 | val = sctp_sk(sk)->pd_point; | 4998 | val = sctp_sk(sk)->pd_point; |
4999 | if (put_user(len, optlen)) | 4999 | if (put_user(len, optlen)) |
5000 | return -EFAULT; | 5000 | return -EFAULT; |
5001 | if (copy_to_user(optval, &val, len)) | 5001 | if (copy_to_user(optval, &val, len)) |
5002 | return -EFAULT; | 5002 | return -EFAULT; |
5003 | 5003 | ||
5004 | return -ENOTSUPP; | 5004 | return -ENOTSUPP; |
5005 | } | 5005 | } |
5006 | 5006 | ||
5007 | /* | 5007 | /* |
5008 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) | 5008 | * 7.1.28. Set or Get the maximum burst (SCTP_MAX_BURST) |
5009 | * (chapter and verse is quoted at sctp_setsockopt_maxburst()) | 5009 | * (chapter and verse is quoted at sctp_setsockopt_maxburst()) |
5010 | */ | 5010 | */ |
5011 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, | 5011 | static int sctp_getsockopt_maxburst(struct sock *sk, int len, |
5012 | char __user *optval, | 5012 | char __user *optval, |
5013 | int __user *optlen) | 5013 | int __user *optlen) |
5014 | { | 5014 | { |
5015 | struct sctp_assoc_value params; | 5015 | struct sctp_assoc_value params; |
5016 | struct sctp_sock *sp; | 5016 | struct sctp_sock *sp; |
5017 | struct sctp_association *asoc; | 5017 | struct sctp_association *asoc; |
5018 | 5018 | ||
5019 | if (len == sizeof(int)) { | 5019 | if (len == sizeof(int)) { |
5020 | pr_warn("Use of int in max_burst socket option deprecated\n"); | 5020 | pr_warn("Use of int in max_burst socket option deprecated\n"); |
5021 | pr_warn("Use struct sctp_assoc_value instead\n"); | 5021 | pr_warn("Use struct sctp_assoc_value instead\n"); |
5022 | params.assoc_id = 0; | 5022 | params.assoc_id = 0; |
5023 | } else if (len >= sizeof(struct sctp_assoc_value)) { | 5023 | } else if (len >= sizeof(struct sctp_assoc_value)) { |
5024 | len = sizeof(struct sctp_assoc_value); | 5024 | len = sizeof(struct sctp_assoc_value); |
5025 | if (copy_from_user(¶ms, optval, len)) | 5025 | if (copy_from_user(¶ms, optval, len)) |
5026 | return -EFAULT; | 5026 | return -EFAULT; |
5027 | } else | 5027 | } else |
5028 | return -EINVAL; | 5028 | return -EINVAL; |
5029 | 5029 | ||
5030 | sp = sctp_sk(sk); | 5030 | sp = sctp_sk(sk); |
5031 | 5031 | ||
5032 | if (params.assoc_id != 0) { | 5032 | if (params.assoc_id != 0) { |
5033 | asoc = sctp_id2assoc(sk, params.assoc_id); | 5033 | asoc = sctp_id2assoc(sk, params.assoc_id); |
5034 | if (!asoc) | 5034 | if (!asoc) |
5035 | return -EINVAL; | 5035 | return -EINVAL; |
5036 | params.assoc_value = asoc->max_burst; | 5036 | params.assoc_value = asoc->max_burst; |
5037 | } else | 5037 | } else |
5038 | params.assoc_value = sp->max_burst; | 5038 | params.assoc_value = sp->max_burst; |
5039 | 5039 | ||
5040 | if (len == sizeof(int)) { | 5040 | if (len == sizeof(int)) { |
5041 | if (copy_to_user(optval, ¶ms.assoc_value, len)) | 5041 | if (copy_to_user(optval, ¶ms.assoc_value, len)) |
5042 | return -EFAULT; | 5042 | return -EFAULT; |
5043 | } else { | 5043 | } else { |
5044 | if (copy_to_user(optval, ¶ms, len)) | 5044 | if (copy_to_user(optval, ¶ms, len)) |
5045 | return -EFAULT; | 5045 | return -EFAULT; |
5046 | } | 5046 | } |
5047 | 5047 | ||
5048 | return 0; | 5048 | return 0; |
5049 | 5049 | ||
5050 | } | 5050 | } |
5051 | 5051 | ||
5052 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, | 5052 | static int sctp_getsockopt_hmac_ident(struct sock *sk, int len, |
5053 | char __user *optval, int __user *optlen) | 5053 | char __user *optval, int __user *optlen) |
5054 | { | 5054 | { |
5055 | struct sctp_hmacalgo __user *p = (void __user *)optval; | 5055 | struct sctp_hmacalgo __user *p = (void __user *)optval; |
5056 | struct sctp_hmac_algo_param *hmacs; | 5056 | struct sctp_hmac_algo_param *hmacs; |
5057 | __u16 data_len = 0; | 5057 | __u16 data_len = 0; |
5058 | u32 num_idents; | 5058 | u32 num_idents; |
5059 | 5059 | ||
5060 | if (!sctp_auth_enable) | 5060 | if (!sctp_auth_enable) |
5061 | return -EACCES; | 5061 | return -EACCES; |
5062 | 5062 | ||
5063 | hmacs = sctp_sk(sk)->ep->auth_hmacs_list; | 5063 | hmacs = sctp_sk(sk)->ep->auth_hmacs_list; |
5064 | data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t); | 5064 | data_len = ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t); |
5065 | 5065 | ||
5066 | if (len < sizeof(struct sctp_hmacalgo) + data_len) | 5066 | if (len < sizeof(struct sctp_hmacalgo) + data_len) |
5067 | return -EINVAL; | 5067 | return -EINVAL; |
5068 | 5068 | ||
5069 | len = sizeof(struct sctp_hmacalgo) + data_len; | 5069 | len = sizeof(struct sctp_hmacalgo) + data_len; |
5070 | num_idents = data_len / sizeof(u16); | 5070 | num_idents = data_len / sizeof(u16); |
5071 | 5071 | ||
5072 | if (put_user(len, optlen)) | 5072 | if (put_user(len, optlen)) |
5073 | return -EFAULT; | 5073 | return -EFAULT; |
5074 | if (put_user(num_idents, &p->shmac_num_idents)) | 5074 | if (put_user(num_idents, &p->shmac_num_idents)) |
5075 | return -EFAULT; | 5075 | return -EFAULT; |
5076 | if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len)) | 5076 | if (copy_to_user(p->shmac_idents, hmacs->hmac_ids, data_len)) |
5077 | return -EFAULT; | 5077 | return -EFAULT; |
5078 | return 0; | 5078 | return 0; |
5079 | } | 5079 | } |
5080 | 5080 | ||
5081 | static int sctp_getsockopt_active_key(struct sock *sk, int len, | 5081 | static int sctp_getsockopt_active_key(struct sock *sk, int len, |
5082 | char __user *optval, int __user *optlen) | 5082 | char __user *optval, int __user *optlen) |
5083 | { | 5083 | { |
5084 | struct sctp_authkeyid val; | 5084 | struct sctp_authkeyid val; |
5085 | struct sctp_association *asoc; | 5085 | struct sctp_association *asoc; |
5086 | 5086 | ||
5087 | if (!sctp_auth_enable) | 5087 | if (!sctp_auth_enable) |
5088 | return -EACCES; | 5088 | return -EACCES; |
5089 | 5089 | ||
5090 | if (len < sizeof(struct sctp_authkeyid)) | 5090 | if (len < sizeof(struct sctp_authkeyid)) |
5091 | return -EINVAL; | 5091 | return -EINVAL; |
5092 | if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid))) | 5092 | if (copy_from_user(&val, optval, sizeof(struct sctp_authkeyid))) |
5093 | return -EFAULT; | 5093 | return -EFAULT; |
5094 | 5094 | ||
5095 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); | 5095 | asoc = sctp_id2assoc(sk, val.scact_assoc_id); |
5096 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) | 5096 | if (!asoc && val.scact_assoc_id && sctp_style(sk, UDP)) |
5097 | return -EINVAL; | 5097 | return -EINVAL; |
5098 | 5098 | ||
5099 | if (asoc) | 5099 | if (asoc) |
5100 | val.scact_keynumber = asoc->active_key_id; | 5100 | val.scact_keynumber = asoc->active_key_id; |
5101 | else | 5101 | else |
5102 | val.scact_keynumber = sctp_sk(sk)->ep->active_key_id; | 5102 | val.scact_keynumber = sctp_sk(sk)->ep->active_key_id; |
5103 | 5103 | ||
5104 | len = sizeof(struct sctp_authkeyid); | 5104 | len = sizeof(struct sctp_authkeyid); |
5105 | if (put_user(len, optlen)) | 5105 | if (put_user(len, optlen)) |
5106 | return -EFAULT; | 5106 | return -EFAULT; |
5107 | if (copy_to_user(optval, &val, len)) | 5107 | if (copy_to_user(optval, &val, len)) |
5108 | return -EFAULT; | 5108 | return -EFAULT; |
5109 | 5109 | ||
5110 | return 0; | 5110 | return 0; |
5111 | } | 5111 | } |
5112 | 5112 | ||
5113 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, | 5113 | static int sctp_getsockopt_peer_auth_chunks(struct sock *sk, int len, |
5114 | char __user *optval, int __user *optlen) | 5114 | char __user *optval, int __user *optlen) |
5115 | { | 5115 | { |
5116 | struct sctp_authchunks __user *p = (void __user *)optval; | 5116 | struct sctp_authchunks __user *p = (void __user *)optval; |
5117 | struct sctp_authchunks val; | 5117 | struct sctp_authchunks val; |
5118 | struct sctp_association *asoc; | 5118 | struct sctp_association *asoc; |
5119 | struct sctp_chunks_param *ch; | 5119 | struct sctp_chunks_param *ch; |
5120 | u32 num_chunks = 0; | 5120 | u32 num_chunks = 0; |
5121 | char __user *to; | 5121 | char __user *to; |
5122 | 5122 | ||
5123 | if (!sctp_auth_enable) | 5123 | if (!sctp_auth_enable) |
5124 | return -EACCES; | 5124 | return -EACCES; |
5125 | 5125 | ||
5126 | if (len < sizeof(struct sctp_authchunks)) | 5126 | if (len < sizeof(struct sctp_authchunks)) |
5127 | return -EINVAL; | 5127 | return -EINVAL; |
5128 | 5128 | ||
5129 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) | 5129 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) |
5130 | return -EFAULT; | 5130 | return -EFAULT; |
5131 | 5131 | ||
5132 | to = p->gauth_chunks; | 5132 | to = p->gauth_chunks; |
5133 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 5133 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
5134 | if (!asoc) | 5134 | if (!asoc) |
5135 | return -EINVAL; | 5135 | return -EINVAL; |
5136 | 5136 | ||
5137 | ch = asoc->peer.peer_chunks; | 5137 | ch = asoc->peer.peer_chunks; |
5138 | if (!ch) | 5138 | if (!ch) |
5139 | goto num; | 5139 | goto num; |
5140 | 5140 | ||
5141 | /* See if the user provided enough room for all the data */ | 5141 | /* See if the user provided enough room for all the data */ |
5142 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); | 5142 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); |
5143 | if (len < num_chunks) | 5143 | if (len < num_chunks) |
5144 | return -EINVAL; | 5144 | return -EINVAL; |
5145 | 5145 | ||
5146 | if (copy_to_user(to, ch->chunks, num_chunks)) | 5146 | if (copy_to_user(to, ch->chunks, num_chunks)) |
5147 | return -EFAULT; | 5147 | return -EFAULT; |
5148 | num: | 5148 | num: |
5149 | len = sizeof(struct sctp_authchunks) + num_chunks; | 5149 | len = sizeof(struct sctp_authchunks) + num_chunks; |
5150 | if (put_user(len, optlen)) return -EFAULT; | 5150 | if (put_user(len, optlen)) return -EFAULT; |
5151 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 5151 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
5152 | return -EFAULT; | 5152 | return -EFAULT; |
5153 | return 0; | 5153 | return 0; |
5154 | } | 5154 | } |
5155 | 5155 | ||
5156 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, | 5156 | static int sctp_getsockopt_local_auth_chunks(struct sock *sk, int len, |
5157 | char __user *optval, int __user *optlen) | 5157 | char __user *optval, int __user *optlen) |
5158 | { | 5158 | { |
5159 | struct sctp_authchunks __user *p = (void __user *)optval; | 5159 | struct sctp_authchunks __user *p = (void __user *)optval; |
5160 | struct sctp_authchunks val; | 5160 | struct sctp_authchunks val; |
5161 | struct sctp_association *asoc; | 5161 | struct sctp_association *asoc; |
5162 | struct sctp_chunks_param *ch; | 5162 | struct sctp_chunks_param *ch; |
5163 | u32 num_chunks = 0; | 5163 | u32 num_chunks = 0; |
5164 | char __user *to; | 5164 | char __user *to; |
5165 | 5165 | ||
5166 | if (!sctp_auth_enable) | 5166 | if (!sctp_auth_enable) |
5167 | return -EACCES; | 5167 | return -EACCES; |
5168 | 5168 | ||
5169 | if (len < sizeof(struct sctp_authchunks)) | 5169 | if (len < sizeof(struct sctp_authchunks)) |
5170 | return -EINVAL; | 5170 | return -EINVAL; |
5171 | 5171 | ||
5172 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) | 5172 | if (copy_from_user(&val, optval, sizeof(struct sctp_authchunks))) |
5173 | return -EFAULT; | 5173 | return -EFAULT; |
5174 | 5174 | ||
5175 | to = p->gauth_chunks; | 5175 | to = p->gauth_chunks; |
5176 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); | 5176 | asoc = sctp_id2assoc(sk, val.gauth_assoc_id); |
5177 | if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) | 5177 | if (!asoc && val.gauth_assoc_id && sctp_style(sk, UDP)) |
5178 | return -EINVAL; | 5178 | return -EINVAL; |
5179 | 5179 | ||
5180 | if (asoc) | 5180 | if (asoc) |
5181 | ch = (struct sctp_chunks_param*)asoc->c.auth_chunks; | 5181 | ch = (struct sctp_chunks_param*)asoc->c.auth_chunks; |
5182 | else | 5182 | else |
5183 | ch = sctp_sk(sk)->ep->auth_chunk_list; | 5183 | ch = sctp_sk(sk)->ep->auth_chunk_list; |
5184 | 5184 | ||
5185 | if (!ch) | 5185 | if (!ch) |
5186 | goto num; | 5186 | goto num; |
5187 | 5187 | ||
5188 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); | 5188 | num_chunks = ntohs(ch->param_hdr.length) - sizeof(sctp_paramhdr_t); |
5189 | if (len < sizeof(struct sctp_authchunks) + num_chunks) | 5189 | if (len < sizeof(struct sctp_authchunks) + num_chunks) |
5190 | return -EINVAL; | 5190 | return -EINVAL; |
5191 | 5191 | ||
5192 | if (copy_to_user(to, ch->chunks, num_chunks)) | 5192 | if (copy_to_user(to, ch->chunks, num_chunks)) |
5193 | return -EFAULT; | 5193 | return -EFAULT; |
5194 | num: | 5194 | num: |
5195 | len = sizeof(struct sctp_authchunks) + num_chunks; | 5195 | len = sizeof(struct sctp_authchunks) + num_chunks; |
5196 | if (put_user(len, optlen)) | 5196 | if (put_user(len, optlen)) |
5197 | return -EFAULT; | 5197 | return -EFAULT; |
5198 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) | 5198 | if (put_user(num_chunks, &p->gauth_number_of_chunks)) |
5199 | return -EFAULT; | 5199 | return -EFAULT; |
5200 | 5200 | ||
5201 | return 0; | 5201 | return 0; |
5202 | } | 5202 | } |
5203 | 5203 | ||
5204 | /* | 5204 | /* |
5205 | * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) | 5205 | * 8.2.5. Get the Current Number of Associations (SCTP_GET_ASSOC_NUMBER) |
5206 | * This option gets the current number of associations that are attached | 5206 | * This option gets the current number of associations that are attached |
5207 | * to a one-to-many style socket. The option value is an uint32_t. | 5207 | * to a one-to-many style socket. The option value is an uint32_t. |
5208 | */ | 5208 | */ |
5209 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, | 5209 | static int sctp_getsockopt_assoc_number(struct sock *sk, int len, |
5210 | char __user *optval, int __user *optlen) | 5210 | char __user *optval, int __user *optlen) |
5211 | { | 5211 | { |
5212 | struct sctp_sock *sp = sctp_sk(sk); | 5212 | struct sctp_sock *sp = sctp_sk(sk); |
5213 | struct sctp_association *asoc; | 5213 | struct sctp_association *asoc; |
5214 | u32 val = 0; | 5214 | u32 val = 0; |
5215 | 5215 | ||
5216 | if (sctp_style(sk, TCP)) | 5216 | if (sctp_style(sk, TCP)) |
5217 | return -EOPNOTSUPP; | 5217 | return -EOPNOTSUPP; |
5218 | 5218 | ||
5219 | if (len < sizeof(u32)) | 5219 | if (len < sizeof(u32)) |
5220 | return -EINVAL; | 5220 | return -EINVAL; |
5221 | 5221 | ||
5222 | len = sizeof(u32); | 5222 | len = sizeof(u32); |
5223 | 5223 | ||
5224 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { | 5224 | list_for_each_entry(asoc, &(sp->ep->asocs), asocs) { |
5225 | val++; | 5225 | val++; |
5226 | } | 5226 | } |
5227 | 5227 | ||
5228 | if (put_user(len, optlen)) | 5228 | if (put_user(len, optlen)) |
5229 | return -EFAULT; | 5229 | return -EFAULT; |
5230 | if (copy_to_user(optval, &val, len)) | 5230 | if (copy_to_user(optval, &val, len)) |
5231 | return -EFAULT; | 5231 | return -EFAULT; |
5232 | 5232 | ||
5233 | return 0; | 5233 | return 0; |
5234 | } | 5234 | } |
5235 | 5235 | ||
5236 | SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname, | 5236 | SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname, |
5237 | char __user *optval, int __user *optlen) | 5237 | char __user *optval, int __user *optlen) |
5238 | { | 5238 | { |
5239 | int retval = 0; | 5239 | int retval = 0; |
5240 | int len; | 5240 | int len; |
5241 | 5241 | ||
5242 | SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n", | 5242 | SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n", |
5243 | sk, optname); | 5243 | sk, optname); |
5244 | 5244 | ||
5245 | /* I can hardly begin to describe how wrong this is. This is | 5245 | /* I can hardly begin to describe how wrong this is. This is |
5246 | * so broken as to be worse than useless. The API draft | 5246 | * so broken as to be worse than useless. The API draft |
5247 | * REALLY is NOT helpful here... I am not convinced that the | 5247 | * REALLY is NOT helpful here... I am not convinced that the |
5248 | * semantics of getsockopt() with a level OTHER THAN SOL_SCTP | 5248 | * semantics of getsockopt() with a level OTHER THAN SOL_SCTP |
5249 | * are at all well-founded. | 5249 | * are at all well-founded. |
5250 | */ | 5250 | */ |
5251 | if (level != SOL_SCTP) { | 5251 | if (level != SOL_SCTP) { |
5252 | struct sctp_af *af = sctp_sk(sk)->pf->af; | 5252 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
5253 | 5253 | ||
5254 | retval = af->getsockopt(sk, level, optname, optval, optlen); | 5254 | retval = af->getsockopt(sk, level, optname, optval, optlen); |
5255 | return retval; | 5255 | return retval; |
5256 | } | 5256 | } |
5257 | 5257 | ||
5258 | if (get_user(len, optlen)) | 5258 | if (get_user(len, optlen)) |
5259 | return -EFAULT; | 5259 | return -EFAULT; |
5260 | 5260 | ||
5261 | sctp_lock_sock(sk); | 5261 | sctp_lock_sock(sk); |
5262 | 5262 | ||
5263 | switch (optname) { | 5263 | switch (optname) { |
5264 | case SCTP_STATUS: | 5264 | case SCTP_STATUS: |
5265 | retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); | 5265 | retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen); |
5266 | break; | 5266 | break; |
5267 | case SCTP_DISABLE_FRAGMENTS: | 5267 | case SCTP_DISABLE_FRAGMENTS: |
5268 | retval = sctp_getsockopt_disable_fragments(sk, len, optval, | 5268 | retval = sctp_getsockopt_disable_fragments(sk, len, optval, |
5269 | optlen); | 5269 | optlen); |
5270 | break; | 5270 | break; |
5271 | case SCTP_EVENTS: | 5271 | case SCTP_EVENTS: |
5272 | retval = sctp_getsockopt_events(sk, len, optval, optlen); | 5272 | retval = sctp_getsockopt_events(sk, len, optval, optlen); |
5273 | break; | 5273 | break; |
5274 | case SCTP_AUTOCLOSE: | 5274 | case SCTP_AUTOCLOSE: |
5275 | retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); | 5275 | retval = sctp_getsockopt_autoclose(sk, len, optval, optlen); |
5276 | break; | 5276 | break; |
5277 | case SCTP_SOCKOPT_PEELOFF: | 5277 | case SCTP_SOCKOPT_PEELOFF: |
5278 | retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); | 5278 | retval = sctp_getsockopt_peeloff(sk, len, optval, optlen); |
5279 | break; | 5279 | break; |
5280 | case SCTP_PEER_ADDR_PARAMS: | 5280 | case SCTP_PEER_ADDR_PARAMS: |
5281 | retval = sctp_getsockopt_peer_addr_params(sk, len, optval, | 5281 | retval = sctp_getsockopt_peer_addr_params(sk, len, optval, |
5282 | optlen); | 5282 | optlen); |
5283 | break; | 5283 | break; |
5284 | case SCTP_DELAYED_ACK: | 5284 | case SCTP_DELAYED_ACK: |
5285 | retval = sctp_getsockopt_delayed_ack(sk, len, optval, | 5285 | retval = sctp_getsockopt_delayed_ack(sk, len, optval, |
5286 | optlen); | 5286 | optlen); |
5287 | break; | 5287 | break; |
5288 | case SCTP_INITMSG: | 5288 | case SCTP_INITMSG: |
5289 | retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); | 5289 | retval = sctp_getsockopt_initmsg(sk, len, optval, optlen); |
5290 | break; | 5290 | break; |
5291 | case SCTP_GET_PEER_ADDRS: | 5291 | case SCTP_GET_PEER_ADDRS: |
5292 | retval = sctp_getsockopt_peer_addrs(sk, len, optval, | 5292 | retval = sctp_getsockopt_peer_addrs(sk, len, optval, |
5293 | optlen); | 5293 | optlen); |
5294 | break; | 5294 | break; |
5295 | case SCTP_GET_LOCAL_ADDRS: | 5295 | case SCTP_GET_LOCAL_ADDRS: |
5296 | retval = sctp_getsockopt_local_addrs(sk, len, optval, | 5296 | retval = sctp_getsockopt_local_addrs(sk, len, optval, |
5297 | optlen); | 5297 | optlen); |
5298 | break; | 5298 | break; |
5299 | case SCTP_SOCKOPT_CONNECTX3: | 5299 | case SCTP_SOCKOPT_CONNECTX3: |
5300 | retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); | 5300 | retval = sctp_getsockopt_connectx3(sk, len, optval, optlen); |
5301 | break; | 5301 | break; |
5302 | case SCTP_DEFAULT_SEND_PARAM: | 5302 | case SCTP_DEFAULT_SEND_PARAM: |
5303 | retval = sctp_getsockopt_default_send_param(sk, len, | 5303 | retval = sctp_getsockopt_default_send_param(sk, len, |
5304 | optval, optlen); | 5304 | optval, optlen); |
5305 | break; | 5305 | break; |
5306 | case SCTP_PRIMARY_ADDR: | 5306 | case SCTP_PRIMARY_ADDR: |
5307 | retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); | 5307 | retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen); |
5308 | break; | 5308 | break; |
5309 | case SCTP_NODELAY: | 5309 | case SCTP_NODELAY: |
5310 | retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); | 5310 | retval = sctp_getsockopt_nodelay(sk, len, optval, optlen); |
5311 | break; | 5311 | break; |
5312 | case SCTP_RTOINFO: | 5312 | case SCTP_RTOINFO: |
5313 | retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); | 5313 | retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen); |
5314 | break; | 5314 | break; |
5315 | case SCTP_ASSOCINFO: | 5315 | case SCTP_ASSOCINFO: |
5316 | retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); | 5316 | retval = sctp_getsockopt_associnfo(sk, len, optval, optlen); |
5317 | break; | 5317 | break; |
5318 | case SCTP_I_WANT_MAPPED_V4_ADDR: | 5318 | case SCTP_I_WANT_MAPPED_V4_ADDR: |
5319 | retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); | 5319 | retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen); |
5320 | break; | 5320 | break; |
5321 | case SCTP_MAXSEG: | 5321 | case SCTP_MAXSEG: |
5322 | retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); | 5322 | retval = sctp_getsockopt_maxseg(sk, len, optval, optlen); |
5323 | break; | 5323 | break; |
5324 | case SCTP_GET_PEER_ADDR_INFO: | 5324 | case SCTP_GET_PEER_ADDR_INFO: |
5325 | retval = sctp_getsockopt_peer_addr_info(sk, len, optval, | 5325 | retval = sctp_getsockopt_peer_addr_info(sk, len, optval, |
5326 | optlen); | 5326 | optlen); |
5327 | break; | 5327 | break; |
5328 | case SCTP_ADAPTATION_LAYER: | 5328 | case SCTP_ADAPTATION_LAYER: |
5329 | retval = sctp_getsockopt_adaptation_layer(sk, len, optval, | 5329 | retval = sctp_getsockopt_adaptation_layer(sk, len, optval, |
5330 | optlen); | 5330 | optlen); |
5331 | break; | 5331 | break; |
5332 | case SCTP_CONTEXT: | 5332 | case SCTP_CONTEXT: |
5333 | retval = sctp_getsockopt_context(sk, len, optval, optlen); | 5333 | retval = sctp_getsockopt_context(sk, len, optval, optlen); |
5334 | break; | 5334 | break; |
5335 | case SCTP_FRAGMENT_INTERLEAVE: | 5335 | case SCTP_FRAGMENT_INTERLEAVE: |
5336 | retval = sctp_getsockopt_fragment_interleave(sk, len, optval, | 5336 | retval = sctp_getsockopt_fragment_interleave(sk, len, optval, |
5337 | optlen); | 5337 | optlen); |
5338 | break; | 5338 | break; |
5339 | case SCTP_PARTIAL_DELIVERY_POINT: | 5339 | case SCTP_PARTIAL_DELIVERY_POINT: |
5340 | retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, | 5340 | retval = sctp_getsockopt_partial_delivery_point(sk, len, optval, |
5341 | optlen); | 5341 | optlen); |
5342 | break; | 5342 | break; |
5343 | case SCTP_MAX_BURST: | 5343 | case SCTP_MAX_BURST: |
5344 | retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); | 5344 | retval = sctp_getsockopt_maxburst(sk, len, optval, optlen); |
5345 | break; | 5345 | break; |
5346 | case SCTP_AUTH_KEY: | 5346 | case SCTP_AUTH_KEY: |
5347 | case SCTP_AUTH_CHUNK: | 5347 | case SCTP_AUTH_CHUNK: |
5348 | case SCTP_AUTH_DELETE_KEY: | 5348 | case SCTP_AUTH_DELETE_KEY: |
5349 | retval = -EOPNOTSUPP; | 5349 | retval = -EOPNOTSUPP; |
5350 | break; | 5350 | break; |
5351 | case SCTP_HMAC_IDENT: | 5351 | case SCTP_HMAC_IDENT: |
5352 | retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); | 5352 | retval = sctp_getsockopt_hmac_ident(sk, len, optval, optlen); |
5353 | break; | 5353 | break; |
5354 | case SCTP_AUTH_ACTIVE_KEY: | 5354 | case SCTP_AUTH_ACTIVE_KEY: |
5355 | retval = sctp_getsockopt_active_key(sk, len, optval, optlen); | 5355 | retval = sctp_getsockopt_active_key(sk, len, optval, optlen); |
5356 | break; | 5356 | break; |
5357 | case SCTP_PEER_AUTH_CHUNKS: | 5357 | case SCTP_PEER_AUTH_CHUNKS: |
5358 | retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, | 5358 | retval = sctp_getsockopt_peer_auth_chunks(sk, len, optval, |
5359 | optlen); | 5359 | optlen); |
5360 | break; | 5360 | break; |
5361 | case SCTP_LOCAL_AUTH_CHUNKS: | 5361 | case SCTP_LOCAL_AUTH_CHUNKS: |
5362 | retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, | 5362 | retval = sctp_getsockopt_local_auth_chunks(sk, len, optval, |
5363 | optlen); | 5363 | optlen); |
5364 | break; | 5364 | break; |
5365 | case SCTP_GET_ASSOC_NUMBER: | 5365 | case SCTP_GET_ASSOC_NUMBER: |
5366 | retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); | 5366 | retval = sctp_getsockopt_assoc_number(sk, len, optval, optlen); |
5367 | break; | 5367 | break; |
5368 | default: | 5368 | default: |
5369 | retval = -ENOPROTOOPT; | 5369 | retval = -ENOPROTOOPT; |
5370 | break; | 5370 | break; |
5371 | } | 5371 | } |
5372 | 5372 | ||
5373 | sctp_release_sock(sk); | 5373 | sctp_release_sock(sk); |
5374 | return retval; | 5374 | return retval; |
5375 | } | 5375 | } |
5376 | 5376 | ||
5377 | static void sctp_hash(struct sock *sk) | 5377 | static void sctp_hash(struct sock *sk) |
5378 | { | 5378 | { |
5379 | /* STUB */ | 5379 | /* STUB */ |
5380 | } | 5380 | } |
5381 | 5381 | ||
5382 | static void sctp_unhash(struct sock *sk) | 5382 | static void sctp_unhash(struct sock *sk) |
5383 | { | 5383 | { |
5384 | /* STUB */ | 5384 | /* STUB */ |
5385 | } | 5385 | } |
5386 | 5386 | ||
5387 | /* Check if port is acceptable. Possibly find first available port. | 5387 | /* Check if port is acceptable. Possibly find first available port. |
5388 | * | 5388 | * |
5389 | * The port hash table (contained in the 'global' SCTP protocol storage | 5389 | * The port hash table (contained in the 'global' SCTP protocol storage |
5390 | * returned by struct sctp_protocol *sctp_get_protocol()). The hash | 5390 | * returned by struct sctp_protocol *sctp_get_protocol()). The hash |
5391 | * table is an array of 4096 lists (sctp_bind_hashbucket). Each | 5391 | * table is an array of 4096 lists (sctp_bind_hashbucket). Each |
5392 | * list (the list number is the port number hashed out, so as you | 5392 | * list (the list number is the port number hashed out, so as you |
5393 | * would expect from a hash function, all the ports in a given list have | 5393 | * would expect from a hash function, all the ports in a given list have |
5394 | * such a number that hashes out to the same list number; you were | 5394 | * such a number that hashes out to the same list number; you were |
5395 | * expecting that, right?); so each list has a set of ports, with a | 5395 | * expecting that, right?); so each list has a set of ports, with a |
5396 | * link to the socket (struct sock) that uses it, the port number and | 5396 | * link to the socket (struct sock) that uses it, the port number and |
5397 | * a fastreuse flag (FIXME: NPI ipg). | 5397 | * a fastreuse flag (FIXME: NPI ipg). |
5398 | */ | 5398 | */ |
5399 | static struct sctp_bind_bucket *sctp_bucket_create( | 5399 | static struct sctp_bind_bucket *sctp_bucket_create( |
5400 | struct sctp_bind_hashbucket *head, unsigned short snum); | 5400 | struct sctp_bind_hashbucket *head, unsigned short snum); |
5401 | 5401 | ||
5402 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) | 5402 | static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr) |
5403 | { | 5403 | { |
5404 | struct sctp_bind_hashbucket *head; /* hash list */ | 5404 | struct sctp_bind_hashbucket *head; /* hash list */ |
5405 | struct sctp_bind_bucket *pp; /* hash list port iterator */ | 5405 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
5406 | struct hlist_node *node; | 5406 | struct hlist_node *node; |
5407 | unsigned short snum; | 5407 | unsigned short snum; |
5408 | int ret; | 5408 | int ret; |
5409 | 5409 | ||
5410 | snum = ntohs(addr->v4.sin_port); | 5410 | snum = ntohs(addr->v4.sin_port); |
5411 | 5411 | ||
5412 | SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum); | 5412 | SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum); |
5413 | sctp_local_bh_disable(); | 5413 | sctp_local_bh_disable(); |
5414 | 5414 | ||
5415 | if (snum == 0) { | 5415 | if (snum == 0) { |
5416 | /* Search for an available port. */ | 5416 | /* Search for an available port. */ |
5417 | int low, high, remaining, index; | 5417 | int low, high, remaining, index; |
5418 | unsigned int rover; | 5418 | unsigned int rover; |
5419 | 5419 | ||
5420 | inet_get_local_port_range(&low, &high); | 5420 | inet_get_local_port_range(&low, &high); |
5421 | remaining = (high - low) + 1; | 5421 | remaining = (high - low) + 1; |
5422 | rover = net_random() % remaining + low; | 5422 | rover = net_random() % remaining + low; |
5423 | 5423 | ||
5424 | do { | 5424 | do { |
5425 | rover++; | 5425 | rover++; |
5426 | if ((rover < low) || (rover > high)) | 5426 | if ((rover < low) || (rover > high)) |
5427 | rover = low; | 5427 | rover = low; |
5428 | if (inet_is_reserved_local_port(rover)) | 5428 | if (inet_is_reserved_local_port(rover)) |
5429 | continue; | 5429 | continue; |
5430 | index = sctp_phashfn(rover); | 5430 | index = sctp_phashfn(rover); |
5431 | head = &sctp_port_hashtable[index]; | 5431 | head = &sctp_port_hashtable[index]; |
5432 | sctp_spin_lock(&head->lock); | 5432 | sctp_spin_lock(&head->lock); |
5433 | sctp_for_each_hentry(pp, node, &head->chain) | 5433 | sctp_for_each_hentry(pp, node, &head->chain) |
5434 | if (pp->port == rover) | 5434 | if (pp->port == rover) |
5435 | goto next; | 5435 | goto next; |
5436 | break; | 5436 | break; |
5437 | next: | 5437 | next: |
5438 | sctp_spin_unlock(&head->lock); | 5438 | sctp_spin_unlock(&head->lock); |
5439 | } while (--remaining > 0); | 5439 | } while (--remaining > 0); |
5440 | 5440 | ||
5441 | /* Exhausted local port range during search? */ | 5441 | /* Exhausted local port range during search? */ |
5442 | ret = 1; | 5442 | ret = 1; |
5443 | if (remaining <= 0) | 5443 | if (remaining <= 0) |
5444 | goto fail; | 5444 | goto fail; |
5445 | 5445 | ||
5446 | /* OK, here is the one we will use. HEAD (the port | 5446 | /* OK, here is the one we will use. HEAD (the port |
5447 | * hash table list entry) is non-NULL and we hold it's | 5447 | * hash table list entry) is non-NULL and we hold it's |
5448 | * mutex. | 5448 | * mutex. |
5449 | */ | 5449 | */ |
5450 | snum = rover; | 5450 | snum = rover; |
5451 | } else { | 5451 | } else { |
5452 | /* We are given an specific port number; we verify | 5452 | /* We are given an specific port number; we verify |
5453 | * that it is not being used. If it is used, we will | 5453 | * that it is not being used. If it is used, we will |
5454 | * exahust the search in the hash list corresponding | 5454 | * exahust the search in the hash list corresponding |
5455 | * to the port number (snum) - we detect that with the | 5455 | * to the port number (snum) - we detect that with the |
5456 | * port iterator, pp being NULL. | 5456 | * port iterator, pp being NULL. |
5457 | */ | 5457 | */ |
5458 | head = &sctp_port_hashtable[sctp_phashfn(snum)]; | 5458 | head = &sctp_port_hashtable[sctp_phashfn(snum)]; |
5459 | sctp_spin_lock(&head->lock); | 5459 | sctp_spin_lock(&head->lock); |
5460 | sctp_for_each_hentry(pp, node, &head->chain) { | 5460 | sctp_for_each_hentry(pp, node, &head->chain) { |
5461 | if (pp->port == snum) | 5461 | if (pp->port == snum) |
5462 | goto pp_found; | 5462 | goto pp_found; |
5463 | } | 5463 | } |
5464 | } | 5464 | } |
5465 | pp = NULL; | 5465 | pp = NULL; |
5466 | goto pp_not_found; | 5466 | goto pp_not_found; |
5467 | pp_found: | 5467 | pp_found: |
5468 | if (!hlist_empty(&pp->owner)) { | 5468 | if (!hlist_empty(&pp->owner)) { |
5469 | /* We had a port hash table hit - there is an | 5469 | /* We had a port hash table hit - there is an |
5470 | * available port (pp != NULL) and it is being | 5470 | * available port (pp != NULL) and it is being |
5471 | * used by other socket (pp->owner not empty); that other | 5471 | * used by other socket (pp->owner not empty); that other |
5472 | * socket is going to be sk2. | 5472 | * socket is going to be sk2. |
5473 | */ | 5473 | */ |
5474 | int reuse = sk->sk_reuse; | 5474 | int reuse = sk->sk_reuse; |
5475 | struct sock *sk2; | 5475 | struct sock *sk2; |
5476 | 5476 | ||
5477 | SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n"); | 5477 | SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n"); |
5478 | if (pp->fastreuse && sk->sk_reuse && | 5478 | if (pp->fastreuse && sk->sk_reuse && |
5479 | sk->sk_state != SCTP_SS_LISTENING) | 5479 | sk->sk_state != SCTP_SS_LISTENING) |
5480 | goto success; | 5480 | goto success; |
5481 | 5481 | ||
5482 | /* Run through the list of sockets bound to the port | 5482 | /* Run through the list of sockets bound to the port |
5483 | * (pp->port) [via the pointers bind_next and | 5483 | * (pp->port) [via the pointers bind_next and |
5484 | * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, | 5484 | * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one, |
5485 | * we get the endpoint they describe and run through | 5485 | * we get the endpoint they describe and run through |
5486 | * the endpoint's list of IP (v4 or v6) addresses, | 5486 | * the endpoint's list of IP (v4 or v6) addresses, |
5487 | * comparing each of the addresses with the address of | 5487 | * comparing each of the addresses with the address of |
5488 | * the socket sk. If we find a match, then that means | 5488 | * the socket sk. If we find a match, then that means |
5489 | * that this port/socket (sk) combination are already | 5489 | * that this port/socket (sk) combination are already |
5490 | * in an endpoint. | 5490 | * in an endpoint. |
5491 | */ | 5491 | */ |
5492 | sk_for_each_bound(sk2, node, &pp->owner) { | 5492 | sk_for_each_bound(sk2, node, &pp->owner) { |
5493 | struct sctp_endpoint *ep2; | 5493 | struct sctp_endpoint *ep2; |
5494 | ep2 = sctp_sk(sk2)->ep; | 5494 | ep2 = sctp_sk(sk2)->ep; |
5495 | 5495 | ||
5496 | if (sk == sk2 || | 5496 | if (sk == sk2 || |
5497 | (reuse && sk2->sk_reuse && | 5497 | (reuse && sk2->sk_reuse && |
5498 | sk2->sk_state != SCTP_SS_LISTENING)) | 5498 | sk2->sk_state != SCTP_SS_LISTENING)) |
5499 | continue; | 5499 | continue; |
5500 | 5500 | ||
5501 | if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, | 5501 | if (sctp_bind_addr_conflict(&ep2->base.bind_addr, addr, |
5502 | sctp_sk(sk2), sctp_sk(sk))) { | 5502 | sctp_sk(sk2), sctp_sk(sk))) { |
5503 | ret = (long)sk2; | 5503 | ret = (long)sk2; |
5504 | goto fail_unlock; | 5504 | goto fail_unlock; |
5505 | } | 5505 | } |
5506 | } | 5506 | } |
5507 | SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n"); | 5507 | SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n"); |
5508 | } | 5508 | } |
5509 | pp_not_found: | 5509 | pp_not_found: |
5510 | /* If there was a hash table miss, create a new port. */ | 5510 | /* If there was a hash table miss, create a new port. */ |
5511 | ret = 1; | 5511 | ret = 1; |
5512 | if (!pp && !(pp = sctp_bucket_create(head, snum))) | 5512 | if (!pp && !(pp = sctp_bucket_create(head, snum))) |
5513 | goto fail_unlock; | 5513 | goto fail_unlock; |
5514 | 5514 | ||
5515 | /* In either case (hit or miss), make sure fastreuse is 1 only | 5515 | /* In either case (hit or miss), make sure fastreuse is 1 only |
5516 | * if sk->sk_reuse is too (that is, if the caller requested | 5516 | * if sk->sk_reuse is too (that is, if the caller requested |
5517 | * SO_REUSEADDR on this socket -sk-). | 5517 | * SO_REUSEADDR on this socket -sk-). |
5518 | */ | 5518 | */ |
5519 | if (hlist_empty(&pp->owner)) { | 5519 | if (hlist_empty(&pp->owner)) { |
5520 | if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) | 5520 | if (sk->sk_reuse && sk->sk_state != SCTP_SS_LISTENING) |
5521 | pp->fastreuse = 1; | 5521 | pp->fastreuse = 1; |
5522 | else | 5522 | else |
5523 | pp->fastreuse = 0; | 5523 | pp->fastreuse = 0; |
5524 | } else if (pp->fastreuse && | 5524 | } else if (pp->fastreuse && |
5525 | (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) | 5525 | (!sk->sk_reuse || sk->sk_state == SCTP_SS_LISTENING)) |
5526 | pp->fastreuse = 0; | 5526 | pp->fastreuse = 0; |
5527 | 5527 | ||
5528 | /* We are set, so fill up all the data in the hash table | 5528 | /* We are set, so fill up all the data in the hash table |
5529 | * entry, tie the socket list information with the rest of the | 5529 | * entry, tie the socket list information with the rest of the |
5530 | * sockets FIXME: Blurry, NPI (ipg). | 5530 | * sockets FIXME: Blurry, NPI (ipg). |
5531 | */ | 5531 | */ |
5532 | success: | 5532 | success: |
5533 | if (!sctp_sk(sk)->bind_hash) { | 5533 | if (!sctp_sk(sk)->bind_hash) { |
5534 | inet_sk(sk)->inet_num = snum; | 5534 | inet_sk(sk)->inet_num = snum; |
5535 | sk_add_bind_node(sk, &pp->owner); | 5535 | sk_add_bind_node(sk, &pp->owner); |
5536 | sctp_sk(sk)->bind_hash = pp; | 5536 | sctp_sk(sk)->bind_hash = pp; |
5537 | } | 5537 | } |
5538 | ret = 0; | 5538 | ret = 0; |
5539 | 5539 | ||
5540 | fail_unlock: | 5540 | fail_unlock: |
5541 | sctp_spin_unlock(&head->lock); | 5541 | sctp_spin_unlock(&head->lock); |
5542 | 5542 | ||
5543 | fail: | 5543 | fail: |
5544 | sctp_local_bh_enable(); | 5544 | sctp_local_bh_enable(); |
5545 | return ret; | 5545 | return ret; |
5546 | } | 5546 | } |
5547 | 5547 | ||
5548 | /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral | 5548 | /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral |
5549 | * port is requested. | 5549 | * port is requested. |
5550 | */ | 5550 | */ |
5551 | static int sctp_get_port(struct sock *sk, unsigned short snum) | 5551 | static int sctp_get_port(struct sock *sk, unsigned short snum) |
5552 | { | 5552 | { |
5553 | long ret; | 5553 | long ret; |
5554 | union sctp_addr addr; | 5554 | union sctp_addr addr; |
5555 | struct sctp_af *af = sctp_sk(sk)->pf->af; | 5555 | struct sctp_af *af = sctp_sk(sk)->pf->af; |
5556 | 5556 | ||
5557 | /* Set up a dummy address struct from the sk. */ | 5557 | /* Set up a dummy address struct from the sk. */ |
5558 | af->from_sk(&addr, sk); | 5558 | af->from_sk(&addr, sk); |
5559 | addr.v4.sin_port = htons(snum); | 5559 | addr.v4.sin_port = htons(snum); |
5560 | 5560 | ||
5561 | /* Note: sk->sk_num gets filled in if ephemeral port request. */ | 5561 | /* Note: sk->sk_num gets filled in if ephemeral port request. */ |
5562 | ret = sctp_get_port_local(sk, &addr); | 5562 | ret = sctp_get_port_local(sk, &addr); |
5563 | 5563 | ||
5564 | return (ret ? 1 : 0); | 5564 | return (ret ? 1 : 0); |
5565 | } | 5565 | } |
5566 | 5566 | ||
5567 | /* | 5567 | /* |
5568 | * Move a socket to LISTENING state. | 5568 | * Move a socket to LISTENING state. |
5569 | */ | 5569 | */ |
5570 | SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog) | 5570 | SCTP_STATIC int sctp_listen_start(struct sock *sk, int backlog) |
5571 | { | 5571 | { |
5572 | struct sctp_sock *sp = sctp_sk(sk); | 5572 | struct sctp_sock *sp = sctp_sk(sk); |
5573 | struct sctp_endpoint *ep = sp->ep; | 5573 | struct sctp_endpoint *ep = sp->ep; |
5574 | struct crypto_hash *tfm = NULL; | 5574 | struct crypto_hash *tfm = NULL; |
5575 | 5575 | ||
5576 | /* Allocate HMAC for generating cookie. */ | 5576 | /* Allocate HMAC for generating cookie. */ |
5577 | if (!sctp_sk(sk)->hmac && sctp_hmac_alg) { | 5577 | if (!sctp_sk(sk)->hmac && sctp_hmac_alg) { |
5578 | tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC); | 5578 | tfm = crypto_alloc_hash(sctp_hmac_alg, 0, CRYPTO_ALG_ASYNC); |
5579 | if (IS_ERR(tfm)) { | 5579 | if (IS_ERR(tfm)) { |
5580 | if (net_ratelimit()) { | 5580 | if (net_ratelimit()) { |
5581 | pr_info("failed to load transform for %s: %ld\n", | 5581 | pr_info("failed to load transform for %s: %ld\n", |
5582 | sctp_hmac_alg, PTR_ERR(tfm)); | 5582 | sctp_hmac_alg, PTR_ERR(tfm)); |
5583 | } | 5583 | } |
5584 | return -ENOSYS; | 5584 | return -ENOSYS; |
5585 | } | 5585 | } |
5586 | sctp_sk(sk)->hmac = tfm; | 5586 | sctp_sk(sk)->hmac = tfm; |
5587 | } | 5587 | } |
5588 | 5588 | ||
5589 | /* | 5589 | /* |
5590 | * If a bind() or sctp_bindx() is not called prior to a listen() | 5590 | * If a bind() or sctp_bindx() is not called prior to a listen() |
5591 | * call that allows new associations to be accepted, the system | 5591 | * call that allows new associations to be accepted, the system |
5592 | * picks an ephemeral port and will choose an address set equivalent | 5592 | * picks an ephemeral port and will choose an address set equivalent |
5593 | * to binding with a wildcard address. | 5593 | * to binding with a wildcard address. |
5594 | * | 5594 | * |
5595 | * This is not currently spelled out in the SCTP sockets | 5595 | * This is not currently spelled out in the SCTP sockets |
5596 | * extensions draft, but follows the practice as seen in TCP | 5596 | * extensions draft, but follows the practice as seen in TCP |
5597 | * sockets. | 5597 | * sockets. |
5598 | * | 5598 | * |
5599 | */ | 5599 | */ |
5600 | sk->sk_state = SCTP_SS_LISTENING; | 5600 | sk->sk_state = SCTP_SS_LISTENING; |
5601 | if (!ep->base.bind_addr.port) { | 5601 | if (!ep->base.bind_addr.port) { |
5602 | if (sctp_autobind(sk)) | 5602 | if (sctp_autobind(sk)) |
5603 | return -EAGAIN; | 5603 | return -EAGAIN; |
5604 | } else { | 5604 | } else { |
5605 | if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { | 5605 | if (sctp_get_port(sk, inet_sk(sk)->inet_num)) { |
5606 | sk->sk_state = SCTP_SS_CLOSED; | 5606 | sk->sk_state = SCTP_SS_CLOSED; |
5607 | return -EADDRINUSE; | 5607 | return -EADDRINUSE; |
5608 | } | 5608 | } |
5609 | } | 5609 | } |
5610 | 5610 | ||
5611 | sk->sk_max_ack_backlog = backlog; | 5611 | sk->sk_max_ack_backlog = backlog; |
5612 | sctp_hash_endpoint(ep); | 5612 | sctp_hash_endpoint(ep); |
5613 | return 0; | 5613 | return 0; |
5614 | } | 5614 | } |
5615 | 5615 | ||
5616 | /* | 5616 | /* |
5617 | * 4.1.3 / 5.1.3 listen() | 5617 | * 4.1.3 / 5.1.3 listen() |
5618 | * | 5618 | * |
5619 | * By default, new associations are not accepted for UDP style sockets. | 5619 | * By default, new associations are not accepted for UDP style sockets. |
5620 | * An application uses listen() to mark a socket as being able to | 5620 | * An application uses listen() to mark a socket as being able to |
5621 | * accept new associations. | 5621 | * accept new associations. |
5622 | * | 5622 | * |
5623 | * On TCP style sockets, applications use listen() to ready the SCTP | 5623 | * On TCP style sockets, applications use listen() to ready the SCTP |
5624 | * endpoint for accepting inbound associations. | 5624 | * endpoint for accepting inbound associations. |
5625 | * | 5625 | * |
5626 | * On both types of endpoints a backlog of '0' disables listening. | 5626 | * On both types of endpoints a backlog of '0' disables listening. |
5627 | * | 5627 | * |
5628 | * Move a socket to LISTENING state. | 5628 | * Move a socket to LISTENING state. |
5629 | */ | 5629 | */ |
5630 | int sctp_inet_listen(struct socket *sock, int backlog) | 5630 | int sctp_inet_listen(struct socket *sock, int backlog) |
5631 | { | 5631 | { |
5632 | struct sock *sk = sock->sk; | 5632 | struct sock *sk = sock->sk; |
5633 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; | 5633 | struct sctp_endpoint *ep = sctp_sk(sk)->ep; |
5634 | int err = -EINVAL; | 5634 | int err = -EINVAL; |
5635 | 5635 | ||
5636 | if (unlikely(backlog < 0)) | 5636 | if (unlikely(backlog < 0)) |
5637 | return err; | 5637 | return err; |
5638 | 5638 | ||
5639 | sctp_lock_sock(sk); | 5639 | sctp_lock_sock(sk); |
5640 | 5640 | ||
5641 | /* Peeled-off sockets are not allowed to listen(). */ | 5641 | /* Peeled-off sockets are not allowed to listen(). */ |
5642 | if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) | 5642 | if (sctp_style(sk, UDP_HIGH_BANDWIDTH)) |
5643 | goto out; | 5643 | goto out; |
5644 | 5644 | ||
5645 | if (sock->state != SS_UNCONNECTED) | 5645 | if (sock->state != SS_UNCONNECTED) |
5646 | goto out; | 5646 | goto out; |
5647 | 5647 | ||
5648 | /* If backlog is zero, disable listening. */ | 5648 | /* If backlog is zero, disable listening. */ |
5649 | if (!backlog) { | 5649 | if (!backlog) { |
5650 | if (sctp_sstate(sk, CLOSED)) | 5650 | if (sctp_sstate(sk, CLOSED)) |
5651 | goto out; | 5651 | goto out; |
5652 | 5652 | ||
5653 | err = 0; | 5653 | err = 0; |
5654 | sctp_unhash_endpoint(ep); | 5654 | sctp_unhash_endpoint(ep); |
5655 | sk->sk_state = SCTP_SS_CLOSED; | 5655 | sk->sk_state = SCTP_SS_CLOSED; |
5656 | if (sk->sk_reuse) | 5656 | if (sk->sk_reuse) |
5657 | sctp_sk(sk)->bind_hash->fastreuse = 1; | 5657 | sctp_sk(sk)->bind_hash->fastreuse = 1; |
5658 | goto out; | 5658 | goto out; |
5659 | } | 5659 | } |
5660 | 5660 | ||
5661 | /* If we are already listening, just update the backlog */ | 5661 | /* If we are already listening, just update the backlog */ |
5662 | if (sctp_sstate(sk, LISTENING)) | 5662 | if (sctp_sstate(sk, LISTENING)) |
5663 | sk->sk_max_ack_backlog = backlog; | 5663 | sk->sk_max_ack_backlog = backlog; |
5664 | else { | 5664 | else { |
5665 | err = sctp_listen_start(sk, backlog); | 5665 | err = sctp_listen_start(sk, backlog); |
5666 | if (err) | 5666 | if (err) |
5667 | goto out; | 5667 | goto out; |
5668 | } | 5668 | } |
5669 | 5669 | ||
5670 | err = 0; | 5670 | err = 0; |
5671 | out: | 5671 | out: |
5672 | sctp_release_sock(sk); | 5672 | sctp_release_sock(sk); |
5673 | return err; | 5673 | return err; |
5674 | } | 5674 | } |
5675 | 5675 | ||
5676 | /* | 5676 | /* |
5677 | * This function is done by modeling the current datagram_poll() and the | 5677 | * This function is done by modeling the current datagram_poll() and the |
5678 | * tcp_poll(). Note that, based on these implementations, we don't | 5678 | * tcp_poll(). Note that, based on these implementations, we don't |
5679 | * lock the socket in this function, even though it seems that, | 5679 | * lock the socket in this function, even though it seems that, |
5680 | * ideally, locking or some other mechanisms can be used to ensure | 5680 | * ideally, locking or some other mechanisms can be used to ensure |
5681 | * the integrity of the counters (sndbuf and wmem_alloc) used | 5681 | * the integrity of the counters (sndbuf and wmem_alloc) used |
5682 | * in this place. We assume that we don't need locks either until proven | 5682 | * in this place. We assume that we don't need locks either until proven |
5683 | * otherwise. | 5683 | * otherwise. |
5684 | * | 5684 | * |
5685 | * Another thing to note is that we include the Async I/O support | 5685 | * Another thing to note is that we include the Async I/O support |
5686 | * here, again, by modeling the current TCP/UDP code. We don't have | 5686 | * here, again, by modeling the current TCP/UDP code. We don't have |
5687 | * a good way to test with it yet. | 5687 | * a good way to test with it yet. |
5688 | */ | 5688 | */ |
5689 | unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) | 5689 | unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait) |
5690 | { | 5690 | { |
5691 | struct sock *sk = sock->sk; | 5691 | struct sock *sk = sock->sk; |
5692 | struct sctp_sock *sp = sctp_sk(sk); | 5692 | struct sctp_sock *sp = sctp_sk(sk); |
5693 | unsigned int mask; | 5693 | unsigned int mask; |
5694 | 5694 | ||
5695 | poll_wait(file, sk_sleep(sk), wait); | 5695 | poll_wait(file, sk_sleep(sk), wait); |
5696 | 5696 | ||
5697 | /* A TCP-style listening socket becomes readable when the accept queue | 5697 | /* A TCP-style listening socket becomes readable when the accept queue |
5698 | * is not empty. | 5698 | * is not empty. |
5699 | */ | 5699 | */ |
5700 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | 5700 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
5701 | return (!list_empty(&sp->ep->asocs)) ? | 5701 | return (!list_empty(&sp->ep->asocs)) ? |
5702 | (POLLIN | POLLRDNORM) : 0; | 5702 | (POLLIN | POLLRDNORM) : 0; |
5703 | 5703 | ||
5704 | mask = 0; | 5704 | mask = 0; |
5705 | 5705 | ||
5706 | /* Is there any exceptional events? */ | 5706 | /* Is there any exceptional events? */ |
5707 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 5707 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
5708 | mask |= POLLERR; | 5708 | mask |= POLLERR; |
5709 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 5709 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
5710 | mask |= POLLRDHUP; | 5710 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
5711 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 5711 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
5712 | mask |= POLLHUP; | 5712 | mask |= POLLHUP; |
5713 | 5713 | ||
5714 | /* Is it readable? Reconsider this code with TCP-style support. */ | 5714 | /* Is it readable? Reconsider this code with TCP-style support. */ |
5715 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 5715 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
5716 | (sk->sk_shutdown & RCV_SHUTDOWN)) | ||
5717 | mask |= POLLIN | POLLRDNORM; | 5716 | mask |= POLLIN | POLLRDNORM; |
5718 | 5717 | ||
5719 | /* The association is either gone or not ready. */ | 5718 | /* The association is either gone or not ready. */ |
5720 | if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) | 5719 | if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED)) |
5721 | return mask; | 5720 | return mask; |
5722 | 5721 | ||
5723 | /* Is it writable? */ | 5722 | /* Is it writable? */ |
5724 | if (sctp_writeable(sk)) { | 5723 | if (sctp_writeable(sk)) { |
5725 | mask |= POLLOUT | POLLWRNORM; | 5724 | mask |= POLLOUT | POLLWRNORM; |
5726 | } else { | 5725 | } else { |
5727 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | 5726 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
5728 | /* | 5727 | /* |
5729 | * Since the socket is not locked, the buffer | 5728 | * Since the socket is not locked, the buffer |
5730 | * might be made available after the writeable check and | 5729 | * might be made available after the writeable check and |
5731 | * before the bit is set. This could cause a lost I/O | 5730 | * before the bit is set. This could cause a lost I/O |
5732 | * signal. tcp_poll() has a race breaker for this race | 5731 | * signal. tcp_poll() has a race breaker for this race |
5733 | * condition. Based on their implementation, we put | 5732 | * condition. Based on their implementation, we put |
5734 | * in the following code to cover it as well. | 5733 | * in the following code to cover it as well. |
5735 | */ | 5734 | */ |
5736 | if (sctp_writeable(sk)) | 5735 | if (sctp_writeable(sk)) |
5737 | mask |= POLLOUT | POLLWRNORM; | 5736 | mask |= POLLOUT | POLLWRNORM; |
5738 | } | 5737 | } |
5739 | return mask; | 5738 | return mask; |
5740 | } | 5739 | } |
5741 | 5740 | ||
5742 | /******************************************************************** | 5741 | /******************************************************************** |
5743 | * 2nd Level Abstractions | 5742 | * 2nd Level Abstractions |
5744 | ********************************************************************/ | 5743 | ********************************************************************/ |
5745 | 5744 | ||
5746 | static struct sctp_bind_bucket *sctp_bucket_create( | 5745 | static struct sctp_bind_bucket *sctp_bucket_create( |
5747 | struct sctp_bind_hashbucket *head, unsigned short snum) | 5746 | struct sctp_bind_hashbucket *head, unsigned short snum) |
5748 | { | 5747 | { |
5749 | struct sctp_bind_bucket *pp; | 5748 | struct sctp_bind_bucket *pp; |
5750 | 5749 | ||
5751 | pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); | 5750 | pp = kmem_cache_alloc(sctp_bucket_cachep, GFP_ATOMIC); |
5752 | if (pp) { | 5751 | if (pp) { |
5753 | SCTP_DBG_OBJCNT_INC(bind_bucket); | 5752 | SCTP_DBG_OBJCNT_INC(bind_bucket); |
5754 | pp->port = snum; | 5753 | pp->port = snum; |
5755 | pp->fastreuse = 0; | 5754 | pp->fastreuse = 0; |
5756 | INIT_HLIST_HEAD(&pp->owner); | 5755 | INIT_HLIST_HEAD(&pp->owner); |
5757 | hlist_add_head(&pp->node, &head->chain); | 5756 | hlist_add_head(&pp->node, &head->chain); |
5758 | } | 5757 | } |
5759 | return pp; | 5758 | return pp; |
5760 | } | 5759 | } |
5761 | 5760 | ||
5762 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ | 5761 | /* Caller must hold hashbucket lock for this tb with local BH disabled */ |
5763 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) | 5762 | static void sctp_bucket_destroy(struct sctp_bind_bucket *pp) |
5764 | { | 5763 | { |
5765 | if (pp && hlist_empty(&pp->owner)) { | 5764 | if (pp && hlist_empty(&pp->owner)) { |
5766 | __hlist_del(&pp->node); | 5765 | __hlist_del(&pp->node); |
5767 | kmem_cache_free(sctp_bucket_cachep, pp); | 5766 | kmem_cache_free(sctp_bucket_cachep, pp); |
5768 | SCTP_DBG_OBJCNT_DEC(bind_bucket); | 5767 | SCTP_DBG_OBJCNT_DEC(bind_bucket); |
5769 | } | 5768 | } |
5770 | } | 5769 | } |
5771 | 5770 | ||
5772 | /* Release this socket's reference to a local port. */ | 5771 | /* Release this socket's reference to a local port. */ |
5773 | static inline void __sctp_put_port(struct sock *sk) | 5772 | static inline void __sctp_put_port(struct sock *sk) |
5774 | { | 5773 | { |
5775 | struct sctp_bind_hashbucket *head = | 5774 | struct sctp_bind_hashbucket *head = |
5776 | &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)]; | 5775 | &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->inet_num)]; |
5777 | struct sctp_bind_bucket *pp; | 5776 | struct sctp_bind_bucket *pp; |
5778 | 5777 | ||
5779 | sctp_spin_lock(&head->lock); | 5778 | sctp_spin_lock(&head->lock); |
5780 | pp = sctp_sk(sk)->bind_hash; | 5779 | pp = sctp_sk(sk)->bind_hash; |
5781 | __sk_del_bind_node(sk); | 5780 | __sk_del_bind_node(sk); |
5782 | sctp_sk(sk)->bind_hash = NULL; | 5781 | sctp_sk(sk)->bind_hash = NULL; |
5783 | inet_sk(sk)->inet_num = 0; | 5782 | inet_sk(sk)->inet_num = 0; |
5784 | sctp_bucket_destroy(pp); | 5783 | sctp_bucket_destroy(pp); |
5785 | sctp_spin_unlock(&head->lock); | 5784 | sctp_spin_unlock(&head->lock); |
5786 | } | 5785 | } |
5787 | 5786 | ||
5788 | void sctp_put_port(struct sock *sk) | 5787 | void sctp_put_port(struct sock *sk) |
5789 | { | 5788 | { |
5790 | sctp_local_bh_disable(); | 5789 | sctp_local_bh_disable(); |
5791 | __sctp_put_port(sk); | 5790 | __sctp_put_port(sk); |
5792 | sctp_local_bh_enable(); | 5791 | sctp_local_bh_enable(); |
5793 | } | 5792 | } |
5794 | 5793 | ||
5795 | /* | 5794 | /* |
5796 | * The system picks an ephemeral port and choose an address set equivalent | 5795 | * The system picks an ephemeral port and choose an address set equivalent |
5797 | * to binding with a wildcard address. | 5796 | * to binding with a wildcard address. |
5798 | * One of those addresses will be the primary address for the association. | 5797 | * One of those addresses will be the primary address for the association. |
5799 | * This automatically enables the multihoming capability of SCTP. | 5798 | * This automatically enables the multihoming capability of SCTP. |
5800 | */ | 5799 | */ |
5801 | static int sctp_autobind(struct sock *sk) | 5800 | static int sctp_autobind(struct sock *sk) |
5802 | { | 5801 | { |
5803 | union sctp_addr autoaddr; | 5802 | union sctp_addr autoaddr; |
5804 | struct sctp_af *af; | 5803 | struct sctp_af *af; |
5805 | __be16 port; | 5804 | __be16 port; |
5806 | 5805 | ||
5807 | /* Initialize a local sockaddr structure to INADDR_ANY. */ | 5806 | /* Initialize a local sockaddr structure to INADDR_ANY. */ |
5808 | af = sctp_sk(sk)->pf->af; | 5807 | af = sctp_sk(sk)->pf->af; |
5809 | 5808 | ||
5810 | port = htons(inet_sk(sk)->inet_num); | 5809 | port = htons(inet_sk(sk)->inet_num); |
5811 | af->inaddr_any(&autoaddr, port); | 5810 | af->inaddr_any(&autoaddr, port); |
5812 | 5811 | ||
5813 | return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); | 5812 | return sctp_do_bind(sk, &autoaddr, af->sockaddr_len); |
5814 | } | 5813 | } |
5815 | 5814 | ||
5816 | /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. | 5815 | /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation. |
5817 | * | 5816 | * |
5818 | * From RFC 2292 | 5817 | * From RFC 2292 |
5819 | * 4.2 The cmsghdr Structure * | 5818 | * 4.2 The cmsghdr Structure * |
5820 | * | 5819 | * |
5821 | * When ancillary data is sent or received, any number of ancillary data | 5820 | * When ancillary data is sent or received, any number of ancillary data |
5822 | * objects can be specified by the msg_control and msg_controllen members of | 5821 | * objects can be specified by the msg_control and msg_controllen members of |
5823 | * the msghdr structure, because each object is preceded by | 5822 | * the msghdr structure, because each object is preceded by |
5824 | * a cmsghdr structure defining the object's length (the cmsg_len member). | 5823 | * a cmsghdr structure defining the object's length (the cmsg_len member). |
5825 | * Historically Berkeley-derived implementations have passed only one object | 5824 | * Historically Berkeley-derived implementations have passed only one object |
5826 | * at a time, but this API allows multiple objects to be | 5825 | * at a time, but this API allows multiple objects to be |
5827 | * passed in a single call to sendmsg() or recvmsg(). The following example | 5826 | * passed in a single call to sendmsg() or recvmsg(). The following example |
5828 | * shows two ancillary data objects in a control buffer. | 5827 | * shows two ancillary data objects in a control buffer. |
5829 | * | 5828 | * |
5830 | * |<--------------------------- msg_controllen -------------------------->| | 5829 | * |<--------------------------- msg_controllen -------------------------->| |
5831 | * | | | 5830 | * | | |
5832 | * | 5831 | * |
5833 | * |<----- ancillary data object ----->|<----- ancillary data object ----->| | 5832 | * |<----- ancillary data object ----->|<----- ancillary data object ----->| |
5834 | * | 5833 | * |
5835 | * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| | 5834 | * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->| |
5836 | * | | | | 5835 | * | | | |
5837 | * | 5836 | * |
5838 | * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | | 5837 | * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| | |
5839 | * | 5838 | * |
5840 | * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | | 5839 | * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| | |
5841 | * | | | | | | 5840 | * | | | | | |
5842 | * | 5841 | * |
5843 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 5842 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
5844 | * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| | 5843 | * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX| |
5845 | * | 5844 | * |
5846 | * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| | 5845 | * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX| |
5847 | * | 5846 | * |
5848 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ | 5847 | * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+ |
5849 | * ^ | 5848 | * ^ |
5850 | * | | 5849 | * | |
5851 | * | 5850 | * |
5852 | * msg_control | 5851 | * msg_control |
5853 | * points here | 5852 | * points here |
5854 | */ | 5853 | */ |
5855 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg, | 5854 | SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg, |
5856 | sctp_cmsgs_t *cmsgs) | 5855 | sctp_cmsgs_t *cmsgs) |
5857 | { | 5856 | { |
5858 | struct cmsghdr *cmsg; | 5857 | struct cmsghdr *cmsg; |
5859 | struct msghdr *my_msg = (struct msghdr *)msg; | 5858 | struct msghdr *my_msg = (struct msghdr *)msg; |
5860 | 5859 | ||
5861 | for (cmsg = CMSG_FIRSTHDR(msg); | 5860 | for (cmsg = CMSG_FIRSTHDR(msg); |
5862 | cmsg != NULL; | 5861 | cmsg != NULL; |
5863 | cmsg = CMSG_NXTHDR(my_msg, cmsg)) { | 5862 | cmsg = CMSG_NXTHDR(my_msg, cmsg)) { |
5864 | if (!CMSG_OK(my_msg, cmsg)) | 5863 | if (!CMSG_OK(my_msg, cmsg)) |
5865 | return -EINVAL; | 5864 | return -EINVAL; |
5866 | 5865 | ||
5867 | /* Should we parse this header or ignore? */ | 5866 | /* Should we parse this header or ignore? */ |
5868 | if (cmsg->cmsg_level != IPPROTO_SCTP) | 5867 | if (cmsg->cmsg_level != IPPROTO_SCTP) |
5869 | continue; | 5868 | continue; |
5870 | 5869 | ||
5871 | /* Strictly check lengths following example in SCM code. */ | 5870 | /* Strictly check lengths following example in SCM code. */ |
5872 | switch (cmsg->cmsg_type) { | 5871 | switch (cmsg->cmsg_type) { |
5873 | case SCTP_INIT: | 5872 | case SCTP_INIT: |
5874 | /* SCTP Socket API Extension | 5873 | /* SCTP Socket API Extension |
5875 | * 5.2.1 SCTP Initiation Structure (SCTP_INIT) | 5874 | * 5.2.1 SCTP Initiation Structure (SCTP_INIT) |
5876 | * | 5875 | * |
5877 | * This cmsghdr structure provides information for | 5876 | * This cmsghdr structure provides information for |
5878 | * initializing new SCTP associations with sendmsg(). | 5877 | * initializing new SCTP associations with sendmsg(). |
5879 | * The SCTP_INITMSG socket option uses this same data | 5878 | * The SCTP_INITMSG socket option uses this same data |
5880 | * structure. This structure is not used for | 5879 | * structure. This structure is not used for |
5881 | * recvmsg(). | 5880 | * recvmsg(). |
5882 | * | 5881 | * |
5883 | * cmsg_level cmsg_type cmsg_data[] | 5882 | * cmsg_level cmsg_type cmsg_data[] |
5884 | * ------------ ------------ ---------------------- | 5883 | * ------------ ------------ ---------------------- |
5885 | * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg | 5884 | * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg |
5886 | */ | 5885 | */ |
5887 | if (cmsg->cmsg_len != | 5886 | if (cmsg->cmsg_len != |
5888 | CMSG_LEN(sizeof(struct sctp_initmsg))) | 5887 | CMSG_LEN(sizeof(struct sctp_initmsg))) |
5889 | return -EINVAL; | 5888 | return -EINVAL; |
5890 | cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg); | 5889 | cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg); |
5891 | break; | 5890 | break; |
5892 | 5891 | ||
5893 | case SCTP_SNDRCV: | 5892 | case SCTP_SNDRCV: |
5894 | /* SCTP Socket API Extension | 5893 | /* SCTP Socket API Extension |
5895 | * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV) | 5894 | * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV) |
5896 | * | 5895 | * |
5897 | * This cmsghdr structure specifies SCTP options for | 5896 | * This cmsghdr structure specifies SCTP options for |
5898 | * sendmsg() and describes SCTP header information | 5897 | * sendmsg() and describes SCTP header information |
5899 | * about a received message through recvmsg(). | 5898 | * about a received message through recvmsg(). |
5900 | * | 5899 | * |
5901 | * cmsg_level cmsg_type cmsg_data[] | 5900 | * cmsg_level cmsg_type cmsg_data[] |
5902 | * ------------ ------------ ---------------------- | 5901 | * ------------ ------------ ---------------------- |
5903 | * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo | 5902 | * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo |
5904 | */ | 5903 | */ |
5905 | if (cmsg->cmsg_len != | 5904 | if (cmsg->cmsg_len != |
5906 | CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) | 5905 | CMSG_LEN(sizeof(struct sctp_sndrcvinfo))) |
5907 | return -EINVAL; | 5906 | return -EINVAL; |
5908 | 5907 | ||
5909 | cmsgs->info = | 5908 | cmsgs->info = |
5910 | (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); | 5909 | (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
5911 | 5910 | ||
5912 | /* Minimally, validate the sinfo_flags. */ | 5911 | /* Minimally, validate the sinfo_flags. */ |
5913 | if (cmsgs->info->sinfo_flags & | 5912 | if (cmsgs->info->sinfo_flags & |
5914 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | | 5913 | ~(SCTP_UNORDERED | SCTP_ADDR_OVER | |
5915 | SCTP_ABORT | SCTP_EOF)) | 5914 | SCTP_ABORT | SCTP_EOF)) |
5916 | return -EINVAL; | 5915 | return -EINVAL; |
5917 | break; | 5916 | break; |
5918 | 5917 | ||
5919 | default: | 5918 | default: |
5920 | return -EINVAL; | 5919 | return -EINVAL; |
5921 | } | 5920 | } |
5922 | } | 5921 | } |
5923 | return 0; | 5922 | return 0; |
5924 | } | 5923 | } |
5925 | 5924 | ||
5926 | /* | 5925 | /* |
5927 | * Wait for a packet.. | 5926 | * Wait for a packet.. |
5928 | * Note: This function is the same function as in core/datagram.c | 5927 | * Note: This function is the same function as in core/datagram.c |
5929 | * with a few modifications to make lksctp work. | 5928 | * with a few modifications to make lksctp work. |
5930 | */ | 5929 | */ |
5931 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p) | 5930 | static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p) |
5932 | { | 5931 | { |
5933 | int error; | 5932 | int error; |
5934 | DEFINE_WAIT(wait); | 5933 | DEFINE_WAIT(wait); |
5935 | 5934 | ||
5936 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 5935 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
5937 | 5936 | ||
5938 | /* Socket errors? */ | 5937 | /* Socket errors? */ |
5939 | error = sock_error(sk); | 5938 | error = sock_error(sk); |
5940 | if (error) | 5939 | if (error) |
5941 | goto out; | 5940 | goto out; |
5942 | 5941 | ||
5943 | if (!skb_queue_empty(&sk->sk_receive_queue)) | 5942 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
5944 | goto ready; | 5943 | goto ready; |
5945 | 5944 | ||
5946 | /* Socket shut down? */ | 5945 | /* Socket shut down? */ |
5947 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 5946 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
5948 | goto out; | 5947 | goto out; |
5949 | 5948 | ||
5950 | /* Sequenced packets can come disconnected. If so we report the | 5949 | /* Sequenced packets can come disconnected. If so we report the |
5951 | * problem. | 5950 | * problem. |
5952 | */ | 5951 | */ |
5953 | error = -ENOTCONN; | 5952 | error = -ENOTCONN; |
5954 | 5953 | ||
5955 | /* Is there a good reason to think that we may receive some data? */ | 5954 | /* Is there a good reason to think that we may receive some data? */ |
5956 | if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) | 5955 | if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING)) |
5957 | goto out; | 5956 | goto out; |
5958 | 5957 | ||
5959 | /* Handle signals. */ | 5958 | /* Handle signals. */ |
5960 | if (signal_pending(current)) | 5959 | if (signal_pending(current)) |
5961 | goto interrupted; | 5960 | goto interrupted; |
5962 | 5961 | ||
5963 | /* Let another process have a go. Since we are going to sleep | 5962 | /* Let another process have a go. Since we are going to sleep |
5964 | * anyway. Note: This may cause odd behaviors if the message | 5963 | * anyway. Note: This may cause odd behaviors if the message |
5965 | * does not fit in the user's buffer, but this seems to be the | 5964 | * does not fit in the user's buffer, but this seems to be the |
5966 | * only way to honor MSG_DONTWAIT realistically. | 5965 | * only way to honor MSG_DONTWAIT realistically. |
5967 | */ | 5966 | */ |
5968 | sctp_release_sock(sk); | 5967 | sctp_release_sock(sk); |
5969 | *timeo_p = schedule_timeout(*timeo_p); | 5968 | *timeo_p = schedule_timeout(*timeo_p); |
5970 | sctp_lock_sock(sk); | 5969 | sctp_lock_sock(sk); |
5971 | 5970 | ||
5972 | ready: | 5971 | ready: |
5973 | finish_wait(sk_sleep(sk), &wait); | 5972 | finish_wait(sk_sleep(sk), &wait); |
5974 | return 0; | 5973 | return 0; |
5975 | 5974 | ||
5976 | interrupted: | 5975 | interrupted: |
5977 | error = sock_intr_errno(*timeo_p); | 5976 | error = sock_intr_errno(*timeo_p); |
5978 | 5977 | ||
5979 | out: | 5978 | out: |
5980 | finish_wait(sk_sleep(sk), &wait); | 5979 | finish_wait(sk_sleep(sk), &wait); |
5981 | *err = error; | 5980 | *err = error; |
5982 | return error; | 5981 | return error; |
5983 | } | 5982 | } |
5984 | 5983 | ||
5985 | /* Receive a datagram. | 5984 | /* Receive a datagram. |
5986 | * Note: This is pretty much the same routine as in core/datagram.c | 5985 | * Note: This is pretty much the same routine as in core/datagram.c |
5987 | * with a few changes to make lksctp work. | 5986 | * with a few changes to make lksctp work. |
5988 | */ | 5987 | */ |
5989 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, | 5988 | static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags, |
5990 | int noblock, int *err) | 5989 | int noblock, int *err) |
5991 | { | 5990 | { |
5992 | int error; | 5991 | int error; |
5993 | struct sk_buff *skb; | 5992 | struct sk_buff *skb; |
5994 | long timeo; | 5993 | long timeo; |
5995 | 5994 | ||
5996 | timeo = sock_rcvtimeo(sk, noblock); | 5995 | timeo = sock_rcvtimeo(sk, noblock); |
5997 | 5996 | ||
5998 | SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n", | 5997 | SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n", |
5999 | timeo, MAX_SCHEDULE_TIMEOUT); | 5998 | timeo, MAX_SCHEDULE_TIMEOUT); |
6000 | 5999 | ||
6001 | do { | 6000 | do { |
6002 | /* Again only user level code calls this function, | 6001 | /* Again only user level code calls this function, |
6003 | * so nothing interrupt level | 6002 | * so nothing interrupt level |
6004 | * will suddenly eat the receive_queue. | 6003 | * will suddenly eat the receive_queue. |
6005 | * | 6004 | * |
6006 | * Look at current nfs client by the way... | 6005 | * Look at current nfs client by the way... |
6007 | * However, this function was corrent in any case. 8) | 6006 | * However, this function was corrent in any case. 8) |
6008 | */ | 6007 | */ |
6009 | if (flags & MSG_PEEK) { | 6008 | if (flags & MSG_PEEK) { |
6010 | spin_lock_bh(&sk->sk_receive_queue.lock); | 6009 | spin_lock_bh(&sk->sk_receive_queue.lock); |
6011 | skb = skb_peek(&sk->sk_receive_queue); | 6010 | skb = skb_peek(&sk->sk_receive_queue); |
6012 | if (skb) | 6011 | if (skb) |
6013 | atomic_inc(&skb->users); | 6012 | atomic_inc(&skb->users); |
6014 | spin_unlock_bh(&sk->sk_receive_queue.lock); | 6013 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
6015 | } else { | 6014 | } else { |
6016 | skb = skb_dequeue(&sk->sk_receive_queue); | 6015 | skb = skb_dequeue(&sk->sk_receive_queue); |
6017 | } | 6016 | } |
6018 | 6017 | ||
6019 | if (skb) | 6018 | if (skb) |
6020 | return skb; | 6019 | return skb; |
6021 | 6020 | ||
6022 | /* Caller is allowed not to check sk->sk_err before calling. */ | 6021 | /* Caller is allowed not to check sk->sk_err before calling. */ |
6023 | error = sock_error(sk); | 6022 | error = sock_error(sk); |
6024 | if (error) | 6023 | if (error) |
6025 | goto no_packet; | 6024 | goto no_packet; |
6026 | 6025 | ||
6027 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 6026 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
6028 | break; | 6027 | break; |
6029 | 6028 | ||
6030 | /* User doesn't want to wait. */ | 6029 | /* User doesn't want to wait. */ |
6031 | error = -EAGAIN; | 6030 | error = -EAGAIN; |
6032 | if (!timeo) | 6031 | if (!timeo) |
6033 | goto no_packet; | 6032 | goto no_packet; |
6034 | } while (sctp_wait_for_packet(sk, err, &timeo) == 0); | 6033 | } while (sctp_wait_for_packet(sk, err, &timeo) == 0); |
6035 | 6034 | ||
6036 | return NULL; | 6035 | return NULL; |
6037 | 6036 | ||
6038 | no_packet: | 6037 | no_packet: |
6039 | *err = error; | 6038 | *err = error; |
6040 | return NULL; | 6039 | return NULL; |
6041 | } | 6040 | } |
6042 | 6041 | ||
6043 | /* If sndbuf has changed, wake up per association sndbuf waiters. */ | 6042 | /* If sndbuf has changed, wake up per association sndbuf waiters. */ |
6044 | static void __sctp_write_space(struct sctp_association *asoc) | 6043 | static void __sctp_write_space(struct sctp_association *asoc) |
6045 | { | 6044 | { |
6046 | struct sock *sk = asoc->base.sk; | 6045 | struct sock *sk = asoc->base.sk; |
6047 | struct socket *sock = sk->sk_socket; | 6046 | struct socket *sock = sk->sk_socket; |
6048 | 6047 | ||
6049 | if ((sctp_wspace(asoc) > 0) && sock) { | 6048 | if ((sctp_wspace(asoc) > 0) && sock) { |
6050 | if (waitqueue_active(&asoc->wait)) | 6049 | if (waitqueue_active(&asoc->wait)) |
6051 | wake_up_interruptible(&asoc->wait); | 6050 | wake_up_interruptible(&asoc->wait); |
6052 | 6051 | ||
6053 | if (sctp_writeable(sk)) { | 6052 | if (sctp_writeable(sk)) { |
6054 | if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk))) | 6053 | if (sk_sleep(sk) && waitqueue_active(sk_sleep(sk))) |
6055 | wake_up_interruptible(sk_sleep(sk)); | 6054 | wake_up_interruptible(sk_sleep(sk)); |
6056 | 6055 | ||
6057 | /* Note that we try to include the Async I/O support | 6056 | /* Note that we try to include the Async I/O support |
6058 | * here by modeling from the current TCP/UDP code. | 6057 | * here by modeling from the current TCP/UDP code. |
6059 | * We have not tested with it yet. | 6058 | * We have not tested with it yet. |
6060 | */ | 6059 | */ |
6061 | if (sock->wq->fasync_list && | 6060 | if (sock->wq->fasync_list && |
6062 | !(sk->sk_shutdown & SEND_SHUTDOWN)) | 6061 | !(sk->sk_shutdown & SEND_SHUTDOWN)) |
6063 | sock_wake_async(sock, | 6062 | sock_wake_async(sock, |
6064 | SOCK_WAKE_SPACE, POLL_OUT); | 6063 | SOCK_WAKE_SPACE, POLL_OUT); |
6065 | } | 6064 | } |
6066 | } | 6065 | } |
6067 | } | 6066 | } |
6068 | 6067 | ||
6069 | /* Do accounting for the sndbuf space. | 6068 | /* Do accounting for the sndbuf space. |
6070 | * Decrement the used sndbuf space of the corresponding association by the | 6069 | * Decrement the used sndbuf space of the corresponding association by the |
6071 | * data size which was just transmitted(freed). | 6070 | * data size which was just transmitted(freed). |
6072 | */ | 6071 | */ |
6073 | static void sctp_wfree(struct sk_buff *skb) | 6072 | static void sctp_wfree(struct sk_buff *skb) |
6074 | { | 6073 | { |
6075 | struct sctp_association *asoc; | 6074 | struct sctp_association *asoc; |
6076 | struct sctp_chunk *chunk; | 6075 | struct sctp_chunk *chunk; |
6077 | struct sock *sk; | 6076 | struct sock *sk; |
6078 | 6077 | ||
6079 | /* Get the saved chunk pointer. */ | 6078 | /* Get the saved chunk pointer. */ |
6080 | chunk = *((struct sctp_chunk **)(skb->cb)); | 6079 | chunk = *((struct sctp_chunk **)(skb->cb)); |
6081 | asoc = chunk->asoc; | 6080 | asoc = chunk->asoc; |
6082 | sk = asoc->base.sk; | 6081 | sk = asoc->base.sk; |
6083 | asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + | 6082 | asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) + |
6084 | sizeof(struct sk_buff) + | 6083 | sizeof(struct sk_buff) + |
6085 | sizeof(struct sctp_chunk); | 6084 | sizeof(struct sctp_chunk); |
6086 | 6085 | ||
6087 | atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); | 6086 | atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc); |
6088 | 6087 | ||
6089 | /* | 6088 | /* |
6090 | * This undoes what is done via sctp_set_owner_w and sk_mem_charge | 6089 | * This undoes what is done via sctp_set_owner_w and sk_mem_charge |
6091 | */ | 6090 | */ |
6092 | sk->sk_wmem_queued -= skb->truesize; | 6091 | sk->sk_wmem_queued -= skb->truesize; |
6093 | sk_mem_uncharge(sk, skb->truesize); | 6092 | sk_mem_uncharge(sk, skb->truesize); |
6094 | 6093 | ||
6095 | sock_wfree(skb); | 6094 | sock_wfree(skb); |
6096 | __sctp_write_space(asoc); | 6095 | __sctp_write_space(asoc); |
6097 | 6096 | ||
6098 | sctp_association_put(asoc); | 6097 | sctp_association_put(asoc); |
6099 | } | 6098 | } |
6100 | 6099 | ||
6101 | /* Do accounting for the receive space on the socket. | 6100 | /* Do accounting for the receive space on the socket. |
6102 | * Accounting for the association is done in ulpevent.c | 6101 | * Accounting for the association is done in ulpevent.c |
6103 | * We set this as a destructor for the cloned data skbs so that | 6102 | * We set this as a destructor for the cloned data skbs so that |
6104 | * accounting is done at the correct time. | 6103 | * accounting is done at the correct time. |
6105 | */ | 6104 | */ |
6106 | void sctp_sock_rfree(struct sk_buff *skb) | 6105 | void sctp_sock_rfree(struct sk_buff *skb) |
6107 | { | 6106 | { |
6108 | struct sock *sk = skb->sk; | 6107 | struct sock *sk = skb->sk; |
6109 | struct sctp_ulpevent *event = sctp_skb2event(skb); | 6108 | struct sctp_ulpevent *event = sctp_skb2event(skb); |
6110 | 6109 | ||
6111 | atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); | 6110 | atomic_sub(event->rmem_len, &sk->sk_rmem_alloc); |
6112 | 6111 | ||
6113 | /* | 6112 | /* |
6114 | * Mimic the behavior of sock_rfree | 6113 | * Mimic the behavior of sock_rfree |
6115 | */ | 6114 | */ |
6116 | sk_mem_uncharge(sk, event->rmem_len); | 6115 | sk_mem_uncharge(sk, event->rmem_len); |
6117 | } | 6116 | } |
6118 | 6117 | ||
6119 | 6118 | ||
6120 | /* Helper function to wait for space in the sndbuf. */ | 6119 | /* Helper function to wait for space in the sndbuf. */ |
6121 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, | 6120 | static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p, |
6122 | size_t msg_len) | 6121 | size_t msg_len) |
6123 | { | 6122 | { |
6124 | struct sock *sk = asoc->base.sk; | 6123 | struct sock *sk = asoc->base.sk; |
6125 | int err = 0; | 6124 | int err = 0; |
6126 | long current_timeo = *timeo_p; | 6125 | long current_timeo = *timeo_p; |
6127 | DEFINE_WAIT(wait); | 6126 | DEFINE_WAIT(wait); |
6128 | 6127 | ||
6129 | SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n", | 6128 | SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n", |
6130 | asoc, (long)(*timeo_p), msg_len); | 6129 | asoc, (long)(*timeo_p), msg_len); |
6131 | 6130 | ||
6132 | /* Increment the association's refcnt. */ | 6131 | /* Increment the association's refcnt. */ |
6133 | sctp_association_hold(asoc); | 6132 | sctp_association_hold(asoc); |
6134 | 6133 | ||
6135 | /* Wait on the association specific sndbuf space. */ | 6134 | /* Wait on the association specific sndbuf space. */ |
6136 | for (;;) { | 6135 | for (;;) { |
6137 | prepare_to_wait_exclusive(&asoc->wait, &wait, | 6136 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
6138 | TASK_INTERRUPTIBLE); | 6137 | TASK_INTERRUPTIBLE); |
6139 | if (!*timeo_p) | 6138 | if (!*timeo_p) |
6140 | goto do_nonblock; | 6139 | goto do_nonblock; |
6141 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || | 6140 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
6142 | asoc->base.dead) | 6141 | asoc->base.dead) |
6143 | goto do_error; | 6142 | goto do_error; |
6144 | if (signal_pending(current)) | 6143 | if (signal_pending(current)) |
6145 | goto do_interrupted; | 6144 | goto do_interrupted; |
6146 | if (msg_len <= sctp_wspace(asoc)) | 6145 | if (msg_len <= sctp_wspace(asoc)) |
6147 | break; | 6146 | break; |
6148 | 6147 | ||
6149 | /* Let another process have a go. Since we are going | 6148 | /* Let another process have a go. Since we are going |
6150 | * to sleep anyway. | 6149 | * to sleep anyway. |
6151 | */ | 6150 | */ |
6152 | sctp_release_sock(sk); | 6151 | sctp_release_sock(sk); |
6153 | current_timeo = schedule_timeout(current_timeo); | 6152 | current_timeo = schedule_timeout(current_timeo); |
6154 | BUG_ON(sk != asoc->base.sk); | 6153 | BUG_ON(sk != asoc->base.sk); |
6155 | sctp_lock_sock(sk); | 6154 | sctp_lock_sock(sk); |
6156 | 6155 | ||
6157 | *timeo_p = current_timeo; | 6156 | *timeo_p = current_timeo; |
6158 | } | 6157 | } |
6159 | 6158 | ||
6160 | out: | 6159 | out: |
6161 | finish_wait(&asoc->wait, &wait); | 6160 | finish_wait(&asoc->wait, &wait); |
6162 | 6161 | ||
6163 | /* Release the association's refcnt. */ | 6162 | /* Release the association's refcnt. */ |
6164 | sctp_association_put(asoc); | 6163 | sctp_association_put(asoc); |
6165 | 6164 | ||
6166 | return err; | 6165 | return err; |
6167 | 6166 | ||
6168 | do_error: | 6167 | do_error: |
6169 | err = -EPIPE; | 6168 | err = -EPIPE; |
6170 | goto out; | 6169 | goto out; |
6171 | 6170 | ||
6172 | do_interrupted: | 6171 | do_interrupted: |
6173 | err = sock_intr_errno(*timeo_p); | 6172 | err = sock_intr_errno(*timeo_p); |
6174 | goto out; | 6173 | goto out; |
6175 | 6174 | ||
6176 | do_nonblock: | 6175 | do_nonblock: |
6177 | err = -EAGAIN; | 6176 | err = -EAGAIN; |
6178 | goto out; | 6177 | goto out; |
6179 | } | 6178 | } |
6180 | 6179 | ||
6181 | void sctp_data_ready(struct sock *sk, int len) | 6180 | void sctp_data_ready(struct sock *sk, int len) |
6182 | { | 6181 | { |
6183 | struct socket_wq *wq; | 6182 | struct socket_wq *wq; |
6184 | 6183 | ||
6185 | rcu_read_lock(); | 6184 | rcu_read_lock(); |
6186 | wq = rcu_dereference(sk->sk_wq); | 6185 | wq = rcu_dereference(sk->sk_wq); |
6187 | if (wq_has_sleeper(wq)) | 6186 | if (wq_has_sleeper(wq)) |
6188 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | | 6187 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
6189 | POLLRDNORM | POLLRDBAND); | 6188 | POLLRDNORM | POLLRDBAND); |
6190 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); | 6189 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); |
6191 | rcu_read_unlock(); | 6190 | rcu_read_unlock(); |
6192 | } | 6191 | } |
6193 | 6192 | ||
6194 | /* If socket sndbuf has changed, wake up all per association waiters. */ | 6193 | /* If socket sndbuf has changed, wake up all per association waiters. */ |
6195 | void sctp_write_space(struct sock *sk) | 6194 | void sctp_write_space(struct sock *sk) |
6196 | { | 6195 | { |
6197 | struct sctp_association *asoc; | 6196 | struct sctp_association *asoc; |
6198 | 6197 | ||
6199 | /* Wake up the tasks in each wait queue. */ | 6198 | /* Wake up the tasks in each wait queue. */ |
6200 | list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { | 6199 | list_for_each_entry(asoc, &((sctp_sk(sk))->ep->asocs), asocs) { |
6201 | __sctp_write_space(asoc); | 6200 | __sctp_write_space(asoc); |
6202 | } | 6201 | } |
6203 | } | 6202 | } |
6204 | 6203 | ||
6205 | /* Is there any sndbuf space available on the socket? | 6204 | /* Is there any sndbuf space available on the socket? |
6206 | * | 6205 | * |
6207 | * Note that sk_wmem_alloc is the sum of the send buffers on all of the | 6206 | * Note that sk_wmem_alloc is the sum of the send buffers on all of the |
6208 | * associations on the same socket. For a UDP-style socket with | 6207 | * associations on the same socket. For a UDP-style socket with |
6209 | * multiple associations, it is possible for it to be "unwriteable" | 6208 | * multiple associations, it is possible for it to be "unwriteable" |
6210 | * prematurely. I assume that this is acceptable because | 6209 | * prematurely. I assume that this is acceptable because |
6211 | * a premature "unwriteable" is better than an accidental "writeable" which | 6210 | * a premature "unwriteable" is better than an accidental "writeable" which |
6212 | * would cause an unwanted block under certain circumstances. For the 1-1 | 6211 | * would cause an unwanted block under certain circumstances. For the 1-1 |
6213 | * UDP-style sockets or TCP-style sockets, this code should work. | 6212 | * UDP-style sockets or TCP-style sockets, this code should work. |
6214 | * - Daisy | 6213 | * - Daisy |
6215 | */ | 6214 | */ |
6216 | static int sctp_writeable(struct sock *sk) | 6215 | static int sctp_writeable(struct sock *sk) |
6217 | { | 6216 | { |
6218 | int amt = 0; | 6217 | int amt = 0; |
6219 | 6218 | ||
6220 | amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk); | 6219 | amt = sk->sk_sndbuf - sk_wmem_alloc_get(sk); |
6221 | if (amt < 0) | 6220 | if (amt < 0) |
6222 | amt = 0; | 6221 | amt = 0; |
6223 | return amt; | 6222 | return amt; |
6224 | } | 6223 | } |
6225 | 6224 | ||
6226 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, | 6225 | /* Wait for an association to go into ESTABLISHED state. If timeout is 0, |
6227 | * returns immediately with EINPROGRESS. | 6226 | * returns immediately with EINPROGRESS. |
6228 | */ | 6227 | */ |
6229 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) | 6228 | static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p) |
6230 | { | 6229 | { |
6231 | struct sock *sk = asoc->base.sk; | 6230 | struct sock *sk = asoc->base.sk; |
6232 | int err = 0; | 6231 | int err = 0; |
6233 | long current_timeo = *timeo_p; | 6232 | long current_timeo = *timeo_p; |
6234 | DEFINE_WAIT(wait); | 6233 | DEFINE_WAIT(wait); |
6235 | 6234 | ||
6236 | SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc, | 6235 | SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __func__, asoc, |
6237 | (long)(*timeo_p)); | 6236 | (long)(*timeo_p)); |
6238 | 6237 | ||
6239 | /* Increment the association's refcnt. */ | 6238 | /* Increment the association's refcnt. */ |
6240 | sctp_association_hold(asoc); | 6239 | sctp_association_hold(asoc); |
6241 | 6240 | ||
6242 | for (;;) { | 6241 | for (;;) { |
6243 | prepare_to_wait_exclusive(&asoc->wait, &wait, | 6242 | prepare_to_wait_exclusive(&asoc->wait, &wait, |
6244 | TASK_INTERRUPTIBLE); | 6243 | TASK_INTERRUPTIBLE); |
6245 | if (!*timeo_p) | 6244 | if (!*timeo_p) |
6246 | goto do_nonblock; | 6245 | goto do_nonblock; |
6247 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 6246 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
6248 | break; | 6247 | break; |
6249 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || | 6248 | if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING || |
6250 | asoc->base.dead) | 6249 | asoc->base.dead) |
6251 | goto do_error; | 6250 | goto do_error; |
6252 | if (signal_pending(current)) | 6251 | if (signal_pending(current)) |
6253 | goto do_interrupted; | 6252 | goto do_interrupted; |
6254 | 6253 | ||
6255 | if (sctp_state(asoc, ESTABLISHED)) | 6254 | if (sctp_state(asoc, ESTABLISHED)) |
6256 | break; | 6255 | break; |
6257 | 6256 | ||
6258 | /* Let another process have a go. Since we are going | 6257 | /* Let another process have a go. Since we are going |
6259 | * to sleep anyway. | 6258 | * to sleep anyway. |
6260 | */ | 6259 | */ |
6261 | sctp_release_sock(sk); | 6260 | sctp_release_sock(sk); |
6262 | current_timeo = schedule_timeout(current_timeo); | 6261 | current_timeo = schedule_timeout(current_timeo); |
6263 | sctp_lock_sock(sk); | 6262 | sctp_lock_sock(sk); |
6264 | 6263 | ||
6265 | *timeo_p = current_timeo; | 6264 | *timeo_p = current_timeo; |
6266 | } | 6265 | } |
6267 | 6266 | ||
6268 | out: | 6267 | out: |
6269 | finish_wait(&asoc->wait, &wait); | 6268 | finish_wait(&asoc->wait, &wait); |
6270 | 6269 | ||
6271 | /* Release the association's refcnt. */ | 6270 | /* Release the association's refcnt. */ |
6272 | sctp_association_put(asoc); | 6271 | sctp_association_put(asoc); |
6273 | 6272 | ||
6274 | return err; | 6273 | return err; |
6275 | 6274 | ||
6276 | do_error: | 6275 | do_error: |
6277 | if (asoc->init_err_counter + 1 > asoc->max_init_attempts) | 6276 | if (asoc->init_err_counter + 1 > asoc->max_init_attempts) |
6278 | err = -ETIMEDOUT; | 6277 | err = -ETIMEDOUT; |
6279 | else | 6278 | else |
6280 | err = -ECONNREFUSED; | 6279 | err = -ECONNREFUSED; |
6281 | goto out; | 6280 | goto out; |
6282 | 6281 | ||
6283 | do_interrupted: | 6282 | do_interrupted: |
6284 | err = sock_intr_errno(*timeo_p); | 6283 | err = sock_intr_errno(*timeo_p); |
6285 | goto out; | 6284 | goto out; |
6286 | 6285 | ||
6287 | do_nonblock: | 6286 | do_nonblock: |
6288 | err = -EINPROGRESS; | 6287 | err = -EINPROGRESS; |
6289 | goto out; | 6288 | goto out; |
6290 | } | 6289 | } |
6291 | 6290 | ||
6292 | static int sctp_wait_for_accept(struct sock *sk, long timeo) | 6291 | static int sctp_wait_for_accept(struct sock *sk, long timeo) |
6293 | { | 6292 | { |
6294 | struct sctp_endpoint *ep; | 6293 | struct sctp_endpoint *ep; |
6295 | int err = 0; | 6294 | int err = 0; |
6296 | DEFINE_WAIT(wait); | 6295 | DEFINE_WAIT(wait); |
6297 | 6296 | ||
6298 | ep = sctp_sk(sk)->ep; | 6297 | ep = sctp_sk(sk)->ep; |
6299 | 6298 | ||
6300 | 6299 | ||
6301 | for (;;) { | 6300 | for (;;) { |
6302 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, | 6301 | prepare_to_wait_exclusive(sk_sleep(sk), &wait, |
6303 | TASK_INTERRUPTIBLE); | 6302 | TASK_INTERRUPTIBLE); |
6304 | 6303 | ||
6305 | if (list_empty(&ep->asocs)) { | 6304 | if (list_empty(&ep->asocs)) { |
6306 | sctp_release_sock(sk); | 6305 | sctp_release_sock(sk); |
6307 | timeo = schedule_timeout(timeo); | 6306 | timeo = schedule_timeout(timeo); |
6308 | sctp_lock_sock(sk); | 6307 | sctp_lock_sock(sk); |
6309 | } | 6308 | } |
6310 | 6309 | ||
6311 | err = -EINVAL; | 6310 | err = -EINVAL; |
6312 | if (!sctp_sstate(sk, LISTENING)) | 6311 | if (!sctp_sstate(sk, LISTENING)) |
6313 | break; | 6312 | break; |
6314 | 6313 | ||
6315 | err = 0; | 6314 | err = 0; |
6316 | if (!list_empty(&ep->asocs)) | 6315 | if (!list_empty(&ep->asocs)) |
6317 | break; | 6316 | break; |
6318 | 6317 | ||
6319 | err = sock_intr_errno(timeo); | 6318 | err = sock_intr_errno(timeo); |
6320 | if (signal_pending(current)) | 6319 | if (signal_pending(current)) |
6321 | break; | 6320 | break; |
6322 | 6321 | ||
6323 | err = -EAGAIN; | 6322 | err = -EAGAIN; |
6324 | if (!timeo) | 6323 | if (!timeo) |
6325 | break; | 6324 | break; |
6326 | } | 6325 | } |
6327 | 6326 | ||
6328 | finish_wait(sk_sleep(sk), &wait); | 6327 | finish_wait(sk_sleep(sk), &wait); |
6329 | 6328 | ||
6330 | return err; | 6329 | return err; |
6331 | } | 6330 | } |
6332 | 6331 | ||
6333 | static void sctp_wait_for_close(struct sock *sk, long timeout) | 6332 | static void sctp_wait_for_close(struct sock *sk, long timeout) |
6334 | { | 6333 | { |
6335 | DEFINE_WAIT(wait); | 6334 | DEFINE_WAIT(wait); |
6336 | 6335 | ||
6337 | do { | 6336 | do { |
6338 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 6337 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
6339 | if (list_empty(&sctp_sk(sk)->ep->asocs)) | 6338 | if (list_empty(&sctp_sk(sk)->ep->asocs)) |
6340 | break; | 6339 | break; |
6341 | sctp_release_sock(sk); | 6340 | sctp_release_sock(sk); |
6342 | timeout = schedule_timeout(timeout); | 6341 | timeout = schedule_timeout(timeout); |
6343 | sctp_lock_sock(sk); | 6342 | sctp_lock_sock(sk); |
6344 | } while (!signal_pending(current) && timeout); | 6343 | } while (!signal_pending(current) && timeout); |
6345 | 6344 | ||
6346 | finish_wait(sk_sleep(sk), &wait); | 6345 | finish_wait(sk_sleep(sk), &wait); |
6347 | } | 6346 | } |
6348 | 6347 | ||
6349 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) | 6348 | static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk) |
6350 | { | 6349 | { |
6351 | struct sk_buff *frag; | 6350 | struct sk_buff *frag; |
6352 | 6351 | ||
6353 | if (!skb->data_len) | 6352 | if (!skb->data_len) |
6354 | goto done; | 6353 | goto done; |
6355 | 6354 | ||
6356 | /* Don't forget the fragments. */ | 6355 | /* Don't forget the fragments. */ |
6357 | skb_walk_frags(skb, frag) | 6356 | skb_walk_frags(skb, frag) |
6358 | sctp_skb_set_owner_r_frag(frag, sk); | 6357 | sctp_skb_set_owner_r_frag(frag, sk); |
6359 | 6358 | ||
6360 | done: | 6359 | done: |
6361 | sctp_skb_set_owner_r(skb, sk); | 6360 | sctp_skb_set_owner_r(skb, sk); |
6362 | } | 6361 | } |
6363 | 6362 | ||
6364 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, | 6363 | void sctp_copy_sock(struct sock *newsk, struct sock *sk, |
6365 | struct sctp_association *asoc) | 6364 | struct sctp_association *asoc) |
6366 | { | 6365 | { |
6367 | struct inet_sock *inet = inet_sk(sk); | 6366 | struct inet_sock *inet = inet_sk(sk); |
6368 | struct inet_sock *newinet; | 6367 | struct inet_sock *newinet; |
6369 | 6368 | ||
6370 | newsk->sk_type = sk->sk_type; | 6369 | newsk->sk_type = sk->sk_type; |
6371 | newsk->sk_bound_dev_if = sk->sk_bound_dev_if; | 6370 | newsk->sk_bound_dev_if = sk->sk_bound_dev_if; |
6372 | newsk->sk_flags = sk->sk_flags; | 6371 | newsk->sk_flags = sk->sk_flags; |
6373 | newsk->sk_no_check = sk->sk_no_check; | 6372 | newsk->sk_no_check = sk->sk_no_check; |
6374 | newsk->sk_reuse = sk->sk_reuse; | 6373 | newsk->sk_reuse = sk->sk_reuse; |
6375 | 6374 | ||
6376 | newsk->sk_shutdown = sk->sk_shutdown; | 6375 | newsk->sk_shutdown = sk->sk_shutdown; |
6377 | newsk->sk_destruct = inet_sock_destruct; | 6376 | newsk->sk_destruct = inet_sock_destruct; |
6378 | newsk->sk_family = sk->sk_family; | 6377 | newsk->sk_family = sk->sk_family; |
6379 | newsk->sk_protocol = IPPROTO_SCTP; | 6378 | newsk->sk_protocol = IPPROTO_SCTP; |
6380 | newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; | 6379 | newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; |
6381 | newsk->sk_sndbuf = sk->sk_sndbuf; | 6380 | newsk->sk_sndbuf = sk->sk_sndbuf; |
6382 | newsk->sk_rcvbuf = sk->sk_rcvbuf; | 6381 | newsk->sk_rcvbuf = sk->sk_rcvbuf; |
6383 | newsk->sk_lingertime = sk->sk_lingertime; | 6382 | newsk->sk_lingertime = sk->sk_lingertime; |
6384 | newsk->sk_rcvtimeo = sk->sk_rcvtimeo; | 6383 | newsk->sk_rcvtimeo = sk->sk_rcvtimeo; |
6385 | newsk->sk_sndtimeo = sk->sk_sndtimeo; | 6384 | newsk->sk_sndtimeo = sk->sk_sndtimeo; |
6386 | 6385 | ||
6387 | newinet = inet_sk(newsk); | 6386 | newinet = inet_sk(newsk); |
6388 | 6387 | ||
6389 | /* Initialize sk's sport, dport, rcv_saddr and daddr for | 6388 | /* Initialize sk's sport, dport, rcv_saddr and daddr for |
6390 | * getsockname() and getpeername() | 6389 | * getsockname() and getpeername() |
6391 | */ | 6390 | */ |
6392 | newinet->inet_sport = inet->inet_sport; | 6391 | newinet->inet_sport = inet->inet_sport; |
6393 | newinet->inet_saddr = inet->inet_saddr; | 6392 | newinet->inet_saddr = inet->inet_saddr; |
6394 | newinet->inet_rcv_saddr = inet->inet_rcv_saddr; | 6393 | newinet->inet_rcv_saddr = inet->inet_rcv_saddr; |
6395 | newinet->inet_dport = htons(asoc->peer.port); | 6394 | newinet->inet_dport = htons(asoc->peer.port); |
6396 | newinet->pmtudisc = inet->pmtudisc; | 6395 | newinet->pmtudisc = inet->pmtudisc; |
6397 | newinet->inet_id = asoc->next_tsn ^ jiffies; | 6396 | newinet->inet_id = asoc->next_tsn ^ jiffies; |
6398 | 6397 | ||
6399 | newinet->uc_ttl = inet->uc_ttl; | 6398 | newinet->uc_ttl = inet->uc_ttl; |
6400 | newinet->mc_loop = 1; | 6399 | newinet->mc_loop = 1; |
6401 | newinet->mc_ttl = 1; | 6400 | newinet->mc_ttl = 1; |
6402 | newinet->mc_index = 0; | 6401 | newinet->mc_index = 0; |
6403 | newinet->mc_list = NULL; | 6402 | newinet->mc_list = NULL; |
6404 | } | 6403 | } |
6405 | 6404 | ||
6406 | /* Populate the fields of the newsk from the oldsk and migrate the assoc | 6405 | /* Populate the fields of the newsk from the oldsk and migrate the assoc |
6407 | * and its messages to the newsk. | 6406 | * and its messages to the newsk. |
6408 | */ | 6407 | */ |
6409 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, | 6408 | static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, |
6410 | struct sctp_association *assoc, | 6409 | struct sctp_association *assoc, |
6411 | sctp_socket_type_t type) | 6410 | sctp_socket_type_t type) |
6412 | { | 6411 | { |
6413 | struct sctp_sock *oldsp = sctp_sk(oldsk); | 6412 | struct sctp_sock *oldsp = sctp_sk(oldsk); |
6414 | struct sctp_sock *newsp = sctp_sk(newsk); | 6413 | struct sctp_sock *newsp = sctp_sk(newsk); |
6415 | struct sctp_bind_bucket *pp; /* hash list port iterator */ | 6414 | struct sctp_bind_bucket *pp; /* hash list port iterator */ |
6416 | struct sctp_endpoint *newep = newsp->ep; | 6415 | struct sctp_endpoint *newep = newsp->ep; |
6417 | struct sk_buff *skb, *tmp; | 6416 | struct sk_buff *skb, *tmp; |
6418 | struct sctp_ulpevent *event; | 6417 | struct sctp_ulpevent *event; |
6419 | struct sctp_bind_hashbucket *head; | 6418 | struct sctp_bind_hashbucket *head; |
6420 | 6419 | ||
6421 | /* Migrate socket buffer sizes and all the socket level options to the | 6420 | /* Migrate socket buffer sizes and all the socket level options to the |
6422 | * new socket. | 6421 | * new socket. |
6423 | */ | 6422 | */ |
6424 | newsk->sk_sndbuf = oldsk->sk_sndbuf; | 6423 | newsk->sk_sndbuf = oldsk->sk_sndbuf; |
6425 | newsk->sk_rcvbuf = oldsk->sk_rcvbuf; | 6424 | newsk->sk_rcvbuf = oldsk->sk_rcvbuf; |
6426 | /* Brute force copy old sctp opt. */ | 6425 | /* Brute force copy old sctp opt. */ |
6427 | inet_sk_copy_descendant(newsk, oldsk); | 6426 | inet_sk_copy_descendant(newsk, oldsk); |
6428 | 6427 | ||
6429 | /* Restore the ep value that was overwritten with the above structure | 6428 | /* Restore the ep value that was overwritten with the above structure |
6430 | * copy. | 6429 | * copy. |
6431 | */ | 6430 | */ |
6432 | newsp->ep = newep; | 6431 | newsp->ep = newep; |
6433 | newsp->hmac = NULL; | 6432 | newsp->hmac = NULL; |
6434 | 6433 | ||
6435 | /* Hook this new socket in to the bind_hash list. */ | 6434 | /* Hook this new socket in to the bind_hash list. */ |
6436 | head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)]; | 6435 | head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->inet_num)]; |
6437 | sctp_local_bh_disable(); | 6436 | sctp_local_bh_disable(); |
6438 | sctp_spin_lock(&head->lock); | 6437 | sctp_spin_lock(&head->lock); |
6439 | pp = sctp_sk(oldsk)->bind_hash; | 6438 | pp = sctp_sk(oldsk)->bind_hash; |
6440 | sk_add_bind_node(newsk, &pp->owner); | 6439 | sk_add_bind_node(newsk, &pp->owner); |
6441 | sctp_sk(newsk)->bind_hash = pp; | 6440 | sctp_sk(newsk)->bind_hash = pp; |
6442 | inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; | 6441 | inet_sk(newsk)->inet_num = inet_sk(oldsk)->inet_num; |
6443 | sctp_spin_unlock(&head->lock); | 6442 | sctp_spin_unlock(&head->lock); |
6444 | sctp_local_bh_enable(); | 6443 | sctp_local_bh_enable(); |
6445 | 6444 | ||
6446 | /* Copy the bind_addr list from the original endpoint to the new | 6445 | /* Copy the bind_addr list from the original endpoint to the new |
6447 | * endpoint so that we can handle restarts properly | 6446 | * endpoint so that we can handle restarts properly |
6448 | */ | 6447 | */ |
6449 | sctp_bind_addr_dup(&newsp->ep->base.bind_addr, | 6448 | sctp_bind_addr_dup(&newsp->ep->base.bind_addr, |
6450 | &oldsp->ep->base.bind_addr, GFP_KERNEL); | 6449 | &oldsp->ep->base.bind_addr, GFP_KERNEL); |
6451 | 6450 | ||
6452 | /* Move any messages in the old socket's receive queue that are for the | 6451 | /* Move any messages in the old socket's receive queue that are for the |
6453 | * peeled off association to the new socket's receive queue. | 6452 | * peeled off association to the new socket's receive queue. |
6454 | */ | 6453 | */ |
6455 | sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { | 6454 | sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) { |
6456 | event = sctp_skb2event(skb); | 6455 | event = sctp_skb2event(skb); |
6457 | if (event->asoc == assoc) { | 6456 | if (event->asoc == assoc) { |
6458 | __skb_unlink(skb, &oldsk->sk_receive_queue); | 6457 | __skb_unlink(skb, &oldsk->sk_receive_queue); |
6459 | __skb_queue_tail(&newsk->sk_receive_queue, skb); | 6458 | __skb_queue_tail(&newsk->sk_receive_queue, skb); |
6460 | sctp_skb_set_owner_r_frag(skb, newsk); | 6459 | sctp_skb_set_owner_r_frag(skb, newsk); |
6461 | } | 6460 | } |
6462 | } | 6461 | } |
6463 | 6462 | ||
6464 | /* Clean up any messages pending delivery due to partial | 6463 | /* Clean up any messages pending delivery due to partial |
6465 | * delivery. Three cases: | 6464 | * delivery. Three cases: |
6466 | * 1) No partial deliver; no work. | 6465 | * 1) No partial deliver; no work. |
6467 | * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. | 6466 | * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby. |
6468 | * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. | 6467 | * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue. |
6469 | */ | 6468 | */ |
6470 | skb_queue_head_init(&newsp->pd_lobby); | 6469 | skb_queue_head_init(&newsp->pd_lobby); |
6471 | atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); | 6470 | atomic_set(&sctp_sk(newsk)->pd_mode, assoc->ulpq.pd_mode); |
6472 | 6471 | ||
6473 | if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { | 6472 | if (atomic_read(&sctp_sk(oldsk)->pd_mode)) { |
6474 | struct sk_buff_head *queue; | 6473 | struct sk_buff_head *queue; |
6475 | 6474 | ||
6476 | /* Decide which queue to move pd_lobby skbs to. */ | 6475 | /* Decide which queue to move pd_lobby skbs to. */ |
6477 | if (assoc->ulpq.pd_mode) { | 6476 | if (assoc->ulpq.pd_mode) { |
6478 | queue = &newsp->pd_lobby; | 6477 | queue = &newsp->pd_lobby; |
6479 | } else | 6478 | } else |
6480 | queue = &newsk->sk_receive_queue; | 6479 | queue = &newsk->sk_receive_queue; |
6481 | 6480 | ||
6482 | /* Walk through the pd_lobby, looking for skbs that | 6481 | /* Walk through the pd_lobby, looking for skbs that |
6483 | * need moved to the new socket. | 6482 | * need moved to the new socket. |
6484 | */ | 6483 | */ |
6485 | sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { | 6484 | sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) { |
6486 | event = sctp_skb2event(skb); | 6485 | event = sctp_skb2event(skb); |
6487 | if (event->asoc == assoc) { | 6486 | if (event->asoc == assoc) { |
6488 | __skb_unlink(skb, &oldsp->pd_lobby); | 6487 | __skb_unlink(skb, &oldsp->pd_lobby); |
6489 | __skb_queue_tail(queue, skb); | 6488 | __skb_queue_tail(queue, skb); |
6490 | sctp_skb_set_owner_r_frag(skb, newsk); | 6489 | sctp_skb_set_owner_r_frag(skb, newsk); |
6491 | } | 6490 | } |
6492 | } | 6491 | } |
6493 | 6492 | ||
6494 | /* Clear up any skbs waiting for the partial | 6493 | /* Clear up any skbs waiting for the partial |
6495 | * delivery to finish. | 6494 | * delivery to finish. |
6496 | */ | 6495 | */ |
6497 | if (assoc->ulpq.pd_mode) | 6496 | if (assoc->ulpq.pd_mode) |
6498 | sctp_clear_pd(oldsk, NULL); | 6497 | sctp_clear_pd(oldsk, NULL); |
6499 | 6498 | ||
6500 | } | 6499 | } |
6501 | 6500 | ||
6502 | sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) | 6501 | sctp_skb_for_each(skb, &assoc->ulpq.reasm, tmp) |
6503 | sctp_skb_set_owner_r_frag(skb, newsk); | 6502 | sctp_skb_set_owner_r_frag(skb, newsk); |
6504 | 6503 | ||
6505 | sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) | 6504 | sctp_skb_for_each(skb, &assoc->ulpq.lobby, tmp) |
6506 | sctp_skb_set_owner_r_frag(skb, newsk); | 6505 | sctp_skb_set_owner_r_frag(skb, newsk); |
6507 | 6506 | ||
6508 | /* Set the type of socket to indicate that it is peeled off from the | 6507 | /* Set the type of socket to indicate that it is peeled off from the |
6509 | * original UDP-style socket or created with the accept() call on a | 6508 | * original UDP-style socket or created with the accept() call on a |
6510 | * TCP-style socket.. | 6509 | * TCP-style socket.. |
6511 | */ | 6510 | */ |
6512 | newsp->type = type; | 6511 | newsp->type = type; |
6513 | 6512 | ||
6514 | /* Mark the new socket "in-use" by the user so that any packets | 6513 | /* Mark the new socket "in-use" by the user so that any packets |
6515 | * that may arrive on the association after we've moved it are | 6514 | * that may arrive on the association after we've moved it are |
6516 | * queued to the backlog. This prevents a potential race between | 6515 | * queued to the backlog. This prevents a potential race between |
6517 | * backlog processing on the old socket and new-packet processing | 6516 | * backlog processing on the old socket and new-packet processing |
6518 | * on the new socket. | 6517 | * on the new socket. |
6519 | * | 6518 | * |
6520 | * The caller has just allocated newsk so we can guarantee that other | 6519 | * The caller has just allocated newsk so we can guarantee that other |
6521 | * paths won't try to lock it and then oldsk. | 6520 | * paths won't try to lock it and then oldsk. |
6522 | */ | 6521 | */ |
6523 | lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); | 6522 | lock_sock_nested(newsk, SINGLE_DEPTH_NESTING); |
6524 | sctp_assoc_migrate(assoc, newsk); | 6523 | sctp_assoc_migrate(assoc, newsk); |
6525 | 6524 | ||
6526 | /* If the association on the newsk is already closed before accept() | 6525 | /* If the association on the newsk is already closed before accept() |
6527 | * is called, set RCV_SHUTDOWN flag. | 6526 | * is called, set RCV_SHUTDOWN flag. |
6528 | */ | 6527 | */ |
6529 | if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) | 6528 | if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP)) |
6530 | newsk->sk_shutdown |= RCV_SHUTDOWN; | 6529 | newsk->sk_shutdown |= RCV_SHUTDOWN; |
6531 | 6530 | ||
6532 | newsk->sk_state = SCTP_SS_ESTABLISHED; | 6531 | newsk->sk_state = SCTP_SS_ESTABLISHED; |
6533 | sctp_release_sock(newsk); | 6532 | sctp_release_sock(newsk); |
6534 | } | 6533 | } |
6535 | 6534 | ||
6536 | 6535 | ||
6537 | /* This proto struct describes the ULP interface for SCTP. */ | 6536 | /* This proto struct describes the ULP interface for SCTP. */ |
6538 | struct proto sctp_prot = { | 6537 | struct proto sctp_prot = { |
6539 | .name = "SCTP", | 6538 | .name = "SCTP", |
6540 | .owner = THIS_MODULE, | 6539 | .owner = THIS_MODULE, |
6541 | .close = sctp_close, | 6540 | .close = sctp_close, |
6542 | .connect = sctp_connect, | 6541 | .connect = sctp_connect, |
6543 | .disconnect = sctp_disconnect, | 6542 | .disconnect = sctp_disconnect, |
6544 | .accept = sctp_accept, | 6543 | .accept = sctp_accept, |
6545 | .ioctl = sctp_ioctl, | 6544 | .ioctl = sctp_ioctl, |
6546 | .init = sctp_init_sock, | 6545 | .init = sctp_init_sock, |
6547 | .destroy = sctp_destroy_sock, | 6546 | .destroy = sctp_destroy_sock, |
6548 | .shutdown = sctp_shutdown, | 6547 | .shutdown = sctp_shutdown, |
6549 | .setsockopt = sctp_setsockopt, | 6548 | .setsockopt = sctp_setsockopt, |
6550 | .getsockopt = sctp_getsockopt, | 6549 | .getsockopt = sctp_getsockopt, |
6551 | .sendmsg = sctp_sendmsg, | 6550 | .sendmsg = sctp_sendmsg, |
6552 | .recvmsg = sctp_recvmsg, | 6551 | .recvmsg = sctp_recvmsg, |
6553 | .bind = sctp_bind, | 6552 | .bind = sctp_bind, |
6554 | .backlog_rcv = sctp_backlog_rcv, | 6553 | .backlog_rcv = sctp_backlog_rcv, |
6555 | .hash = sctp_hash, | 6554 | .hash = sctp_hash, |
6556 | .unhash = sctp_unhash, | 6555 | .unhash = sctp_unhash, |
6557 | .get_port = sctp_get_port, | 6556 | .get_port = sctp_get_port, |
6558 | .obj_size = sizeof(struct sctp_sock), | 6557 | .obj_size = sizeof(struct sctp_sock), |
6559 | .sysctl_mem = sysctl_sctp_mem, | 6558 | .sysctl_mem = sysctl_sctp_mem, |
6560 | .sysctl_rmem = sysctl_sctp_rmem, | 6559 | .sysctl_rmem = sysctl_sctp_rmem, |
6561 | .sysctl_wmem = sysctl_sctp_wmem, | 6560 | .sysctl_wmem = sysctl_sctp_wmem, |
6562 | .memory_pressure = &sctp_memory_pressure, | 6561 | .memory_pressure = &sctp_memory_pressure, |
6563 | .enter_memory_pressure = sctp_enter_memory_pressure, | 6562 | .enter_memory_pressure = sctp_enter_memory_pressure, |
6564 | .memory_allocated = &sctp_memory_allocated, | 6563 | .memory_allocated = &sctp_memory_allocated, |
6565 | .sockets_allocated = &sctp_sockets_allocated, | 6564 | .sockets_allocated = &sctp_sockets_allocated, |
6566 | }; | 6565 | }; |
6567 | 6566 | ||
6568 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | 6567 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
6569 | 6568 | ||
6570 | struct proto sctpv6_prot = { | 6569 | struct proto sctpv6_prot = { |
6571 | .name = "SCTPv6", | 6570 | .name = "SCTPv6", |
6572 | .owner = THIS_MODULE, | 6571 | .owner = THIS_MODULE, |
6573 | .close = sctp_close, | 6572 | .close = sctp_close, |
6574 | .connect = sctp_connect, | 6573 | .connect = sctp_connect, |
6575 | .disconnect = sctp_disconnect, | 6574 | .disconnect = sctp_disconnect, |
6576 | .accept = sctp_accept, | 6575 | .accept = sctp_accept, |
6577 | .ioctl = sctp_ioctl, | 6576 | .ioctl = sctp_ioctl, |
6578 | .init = sctp_init_sock, | 6577 | .init = sctp_init_sock, |
6579 | .destroy = sctp_destroy_sock, | 6578 | .destroy = sctp_destroy_sock, |
6580 | .shutdown = sctp_shutdown, | 6579 | .shutdown = sctp_shutdown, |
6581 | .setsockopt = sctp_setsockopt, | 6580 | .setsockopt = sctp_setsockopt, |
6582 | .getsockopt = sctp_getsockopt, | 6581 | .getsockopt = sctp_getsockopt, |
6583 | .sendmsg = sctp_sendmsg, | 6582 | .sendmsg = sctp_sendmsg, |
6584 | .recvmsg = sctp_recvmsg, | 6583 | .recvmsg = sctp_recvmsg, |
6585 | .bind = sctp_bind, | 6584 | .bind = sctp_bind, |
6586 | .backlog_rcv = sctp_backlog_rcv, | 6585 | .backlog_rcv = sctp_backlog_rcv, |
6587 | .hash = sctp_hash, | 6586 | .hash = sctp_hash, |
6588 | .unhash = sctp_unhash, | 6587 | .unhash = sctp_unhash, |
6589 | .get_port = sctp_get_port, | 6588 | .get_port = sctp_get_port, |
6590 | .obj_size = sizeof(struct sctp6_sock), | 6589 | .obj_size = sizeof(struct sctp6_sock), |
6591 | .sysctl_mem = sysctl_sctp_mem, | 6590 | .sysctl_mem = sysctl_sctp_mem, |
6592 | .sysctl_rmem = sysctl_sctp_rmem, | 6591 | .sysctl_rmem = sysctl_sctp_rmem, |
6593 | .sysctl_wmem = sysctl_sctp_wmem, | 6592 | .sysctl_wmem = sysctl_sctp_wmem, |
6594 | .memory_pressure = &sctp_memory_pressure, | 6593 | .memory_pressure = &sctp_memory_pressure, |
6595 | .enter_memory_pressure = sctp_enter_memory_pressure, | 6594 | .enter_memory_pressure = sctp_enter_memory_pressure, |
6596 | .memory_allocated = &sctp_memory_allocated, | 6595 | .memory_allocated = &sctp_memory_allocated, |
6597 | .sockets_allocated = &sctp_sockets_allocated, | 6596 | .sockets_allocated = &sctp_sockets_allocated, |
6598 | }; | 6597 | }; |
6599 | #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */ | 6598 | #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */ |
6600 | 6599 |
net/unix/af_unix.c
1 | /* | 1 | /* |
2 | * NET4: Implementation of BSD Unix domain sockets. | 2 | * NET4: Implementation of BSD Unix domain sockets. |
3 | * | 3 | * |
4 | * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> | 4 | * Authors: Alan Cox, <alan@lxorguk.ukuu.org.uk> |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or | 6 | * This program is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU General Public License | 7 | * modify it under the terms of the GNU General Public License |
8 | * as published by the Free Software Foundation; either version | 8 | * as published by the Free Software Foundation; either version |
9 | * 2 of the License, or (at your option) any later version. | 9 | * 2 of the License, or (at your option) any later version. |
10 | * | 10 | * |
11 | * Fixes: | 11 | * Fixes: |
12 | * Linus Torvalds : Assorted bug cures. | 12 | * Linus Torvalds : Assorted bug cures. |
13 | * Niibe Yutaka : async I/O support. | 13 | * Niibe Yutaka : async I/O support. |
14 | * Carsten Paeth : PF_UNIX check, address fixes. | 14 | * Carsten Paeth : PF_UNIX check, address fixes. |
15 | * Alan Cox : Limit size of allocated blocks. | 15 | * Alan Cox : Limit size of allocated blocks. |
16 | * Alan Cox : Fixed the stupid socketpair bug. | 16 | * Alan Cox : Fixed the stupid socketpair bug. |
17 | * Alan Cox : BSD compatibility fine tuning. | 17 | * Alan Cox : BSD compatibility fine tuning. |
18 | * Alan Cox : Fixed a bug in connect when interrupted. | 18 | * Alan Cox : Fixed a bug in connect when interrupted. |
19 | * Alan Cox : Sorted out a proper draft version of | 19 | * Alan Cox : Sorted out a proper draft version of |
20 | * file descriptor passing hacked up from | 20 | * file descriptor passing hacked up from |
21 | * Mike Shaver's work. | 21 | * Mike Shaver's work. |
22 | * Marty Leisner : Fixes to fd passing | 22 | * Marty Leisner : Fixes to fd passing |
23 | * Nick Nevin : recvmsg bugfix. | 23 | * Nick Nevin : recvmsg bugfix. |
24 | * Alan Cox : Started proper garbage collector | 24 | * Alan Cox : Started proper garbage collector |
25 | * Heiko EiBfeldt : Missing verify_area check | 25 | * Heiko EiBfeldt : Missing verify_area check |
26 | * Alan Cox : Started POSIXisms | 26 | * Alan Cox : Started POSIXisms |
27 | * Andreas Schwab : Replace inode by dentry for proper | 27 | * Andreas Schwab : Replace inode by dentry for proper |
28 | * reference counting | 28 | * reference counting |
29 | * Kirk Petersen : Made this a module | 29 | * Kirk Petersen : Made this a module |
30 | * Christoph Rohland : Elegant non-blocking accept/connect algorithm. | 30 | * Christoph Rohland : Elegant non-blocking accept/connect algorithm. |
31 | * Lots of bug fixes. | 31 | * Lots of bug fixes. |
32 | * Alexey Kuznetosv : Repaired (I hope) bugs introduces | 32 | * Alexey Kuznetosv : Repaired (I hope) bugs introduces |
33 | * by above two patches. | 33 | * by above two patches. |
34 | * Andrea Arcangeli : If possible we block in connect(2) | 34 | * Andrea Arcangeli : If possible we block in connect(2) |
35 | * if the max backlog of the listen socket | 35 | * if the max backlog of the listen socket |
36 | * is been reached. This won't break | 36 | * is been reached. This won't break |
37 | * old apps and it will avoid huge amount | 37 | * old apps and it will avoid huge amount |
38 | * of socks hashed (this for unix_gc() | 38 | * of socks hashed (this for unix_gc() |
39 | * performances reasons). | 39 | * performances reasons). |
40 | * Security fix that limits the max | 40 | * Security fix that limits the max |
41 | * number of socks to 2*max_files and | 41 | * number of socks to 2*max_files and |
42 | * the number of skb queueable in the | 42 | * the number of skb queueable in the |
43 | * dgram receiver. | 43 | * dgram receiver. |
44 | * Artur Skawina : Hash function optimizations | 44 | * Artur Skawina : Hash function optimizations |
45 | * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8) | 45 | * Alexey Kuznetsov : Full scale SMP. Lot of bugs are introduced 8) |
46 | * Malcolm Beattie : Set peercred for socketpair | 46 | * Malcolm Beattie : Set peercred for socketpair |
47 | * Michal Ostrowski : Module initialization cleanup. | 47 | * Michal Ostrowski : Module initialization cleanup. |
48 | * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT, | 48 | * Arnaldo C. Melo : Remove MOD_{INC,DEC}_USE_COUNT, |
49 | * the core infrastructure is doing that | 49 | * the core infrastructure is doing that |
50 | * for all net proto families now (2.5.69+) | 50 | * for all net proto families now (2.5.69+) |
51 | * | 51 | * |
52 | * | 52 | * |
53 | * Known differences from reference BSD that was tested: | 53 | * Known differences from reference BSD that was tested: |
54 | * | 54 | * |
55 | * [TO FIX] | 55 | * [TO FIX] |
56 | * ECONNREFUSED is not returned from one end of a connected() socket to the | 56 | * ECONNREFUSED is not returned from one end of a connected() socket to the |
57 | * other the moment one end closes. | 57 | * other the moment one end closes. |
58 | * fstat() doesn't return st_dev=0, and give the blksize as high water mark | 58 | * fstat() doesn't return st_dev=0, and give the blksize as high water mark |
59 | * and a fake inode identifier (nor the BSD first socket fstat twice bug). | 59 | * and a fake inode identifier (nor the BSD first socket fstat twice bug). |
60 | * [NOT TO FIX] | 60 | * [NOT TO FIX] |
61 | * accept() returns a path name even if the connecting socket has closed | 61 | * accept() returns a path name even if the connecting socket has closed |
62 | * in the meantime (BSD loses the path and gives up). | 62 | * in the meantime (BSD loses the path and gives up). |
63 | * accept() returns 0 length path for an unbound connector. BSD returns 16 | 63 | * accept() returns 0 length path for an unbound connector. BSD returns 16 |
64 | * and a null first byte in the path (but not for gethost/peername - BSD bug ??) | 64 | * and a null first byte in the path (but not for gethost/peername - BSD bug ??) |
65 | * socketpair(...SOCK_RAW..) doesn't panic the kernel. | 65 | * socketpair(...SOCK_RAW..) doesn't panic the kernel. |
66 | * BSD af_unix apparently has connect forgetting to block properly. | 66 | * BSD af_unix apparently has connect forgetting to block properly. |
67 | * (need to check this with the POSIX spec in detail) | 67 | * (need to check this with the POSIX spec in detail) |
68 | * | 68 | * |
69 | * Differences from 2.0.0-11-... (ANK) | 69 | * Differences from 2.0.0-11-... (ANK) |
70 | * Bug fixes and improvements. | 70 | * Bug fixes and improvements. |
71 | * - client shutdown killed server socket. | 71 | * - client shutdown killed server socket. |
72 | * - removed all useless cli/sti pairs. | 72 | * - removed all useless cli/sti pairs. |
73 | * | 73 | * |
74 | * Semantic changes/extensions. | 74 | * Semantic changes/extensions. |
75 | * - generic control message passing. | 75 | * - generic control message passing. |
76 | * - SCM_CREDENTIALS control message. | 76 | * - SCM_CREDENTIALS control message. |
77 | * - "Abstract" (not FS based) socket bindings. | 77 | * - "Abstract" (not FS based) socket bindings. |
78 | * Abstract names are sequences of bytes (not zero terminated) | 78 | * Abstract names are sequences of bytes (not zero terminated) |
79 | * started by 0, so that this name space does not intersect | 79 | * started by 0, so that this name space does not intersect |
80 | * with BSD names. | 80 | * with BSD names. |
81 | */ | 81 | */ |
82 | 82 | ||
83 | #include <linux/module.h> | 83 | #include <linux/module.h> |
84 | #include <linux/kernel.h> | 84 | #include <linux/kernel.h> |
85 | #include <linux/signal.h> | 85 | #include <linux/signal.h> |
86 | #include <linux/sched.h> | 86 | #include <linux/sched.h> |
87 | #include <linux/errno.h> | 87 | #include <linux/errno.h> |
88 | #include <linux/string.h> | 88 | #include <linux/string.h> |
89 | #include <linux/stat.h> | 89 | #include <linux/stat.h> |
90 | #include <linux/dcache.h> | 90 | #include <linux/dcache.h> |
91 | #include <linux/namei.h> | 91 | #include <linux/namei.h> |
92 | #include <linux/socket.h> | 92 | #include <linux/socket.h> |
93 | #include <linux/un.h> | 93 | #include <linux/un.h> |
94 | #include <linux/fcntl.h> | 94 | #include <linux/fcntl.h> |
95 | #include <linux/termios.h> | 95 | #include <linux/termios.h> |
96 | #include <linux/sockios.h> | 96 | #include <linux/sockios.h> |
97 | #include <linux/net.h> | 97 | #include <linux/net.h> |
98 | #include <linux/in.h> | 98 | #include <linux/in.h> |
99 | #include <linux/fs.h> | 99 | #include <linux/fs.h> |
100 | #include <linux/slab.h> | 100 | #include <linux/slab.h> |
101 | #include <asm/uaccess.h> | 101 | #include <asm/uaccess.h> |
102 | #include <linux/skbuff.h> | 102 | #include <linux/skbuff.h> |
103 | #include <linux/netdevice.h> | 103 | #include <linux/netdevice.h> |
104 | #include <net/net_namespace.h> | 104 | #include <net/net_namespace.h> |
105 | #include <net/sock.h> | 105 | #include <net/sock.h> |
106 | #include <net/tcp_states.h> | 106 | #include <net/tcp_states.h> |
107 | #include <net/af_unix.h> | 107 | #include <net/af_unix.h> |
108 | #include <linux/proc_fs.h> | 108 | #include <linux/proc_fs.h> |
109 | #include <linux/seq_file.h> | 109 | #include <linux/seq_file.h> |
110 | #include <net/scm.h> | 110 | #include <net/scm.h> |
111 | #include <linux/init.h> | 111 | #include <linux/init.h> |
112 | #include <linux/poll.h> | 112 | #include <linux/poll.h> |
113 | #include <linux/rtnetlink.h> | 113 | #include <linux/rtnetlink.h> |
114 | #include <linux/mount.h> | 114 | #include <linux/mount.h> |
115 | #include <net/checksum.h> | 115 | #include <net/checksum.h> |
116 | #include <linux/security.h> | 116 | #include <linux/security.h> |
117 | 117 | ||
118 | static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1]; | 118 | static struct hlist_head unix_socket_table[UNIX_HASH_SIZE + 1]; |
119 | static DEFINE_SPINLOCK(unix_table_lock); | 119 | static DEFINE_SPINLOCK(unix_table_lock); |
120 | static atomic_t unix_nr_socks = ATOMIC_INIT(0); | 120 | static atomic_t unix_nr_socks = ATOMIC_INIT(0); |
121 | 121 | ||
122 | #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE]) | 122 | #define unix_sockets_unbound (&unix_socket_table[UNIX_HASH_SIZE]) |
123 | 123 | ||
124 | #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE) | 124 | #define UNIX_ABSTRACT(sk) (unix_sk(sk)->addr->hash != UNIX_HASH_SIZE) |
125 | 125 | ||
126 | #ifdef CONFIG_SECURITY_NETWORK | 126 | #ifdef CONFIG_SECURITY_NETWORK |
127 | static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) | 127 | static void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
128 | { | 128 | { |
129 | memcpy(UNIXSID(skb), &scm->secid, sizeof(u32)); | 129 | memcpy(UNIXSID(skb), &scm->secid, sizeof(u32)); |
130 | } | 130 | } |
131 | 131 | ||
132 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) | 132 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
133 | { | 133 | { |
134 | scm->secid = *UNIXSID(skb); | 134 | scm->secid = *UNIXSID(skb); |
135 | } | 135 | } |
136 | #else | 136 | #else |
137 | static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) | 137 | static inline void unix_get_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
138 | { } | 138 | { } |
139 | 139 | ||
140 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) | 140 | static inline void unix_set_secdata(struct scm_cookie *scm, struct sk_buff *skb) |
141 | { } | 141 | { } |
142 | #endif /* CONFIG_SECURITY_NETWORK */ | 142 | #endif /* CONFIG_SECURITY_NETWORK */ |
143 | 143 | ||
144 | /* | 144 | /* |
145 | * SMP locking strategy: | 145 | * SMP locking strategy: |
146 | * hash table is protected with spinlock unix_table_lock | 146 | * hash table is protected with spinlock unix_table_lock |
147 | * each socket state is protected by separate spin lock. | 147 | * each socket state is protected by separate spin lock. |
148 | */ | 148 | */ |
149 | 149 | ||
150 | static inline unsigned unix_hash_fold(__wsum n) | 150 | static inline unsigned unix_hash_fold(__wsum n) |
151 | { | 151 | { |
152 | unsigned hash = (__force unsigned)n; | 152 | unsigned hash = (__force unsigned)n; |
153 | hash ^= hash>>16; | 153 | hash ^= hash>>16; |
154 | hash ^= hash>>8; | 154 | hash ^= hash>>8; |
155 | return hash&(UNIX_HASH_SIZE-1); | 155 | return hash&(UNIX_HASH_SIZE-1); |
156 | } | 156 | } |
157 | 157 | ||
158 | #define unix_peer(sk) (unix_sk(sk)->peer) | 158 | #define unix_peer(sk) (unix_sk(sk)->peer) |
159 | 159 | ||
160 | static inline int unix_our_peer(struct sock *sk, struct sock *osk) | 160 | static inline int unix_our_peer(struct sock *sk, struct sock *osk) |
161 | { | 161 | { |
162 | return unix_peer(osk) == sk; | 162 | return unix_peer(osk) == sk; |
163 | } | 163 | } |
164 | 164 | ||
165 | static inline int unix_may_send(struct sock *sk, struct sock *osk) | 165 | static inline int unix_may_send(struct sock *sk, struct sock *osk) |
166 | { | 166 | { |
167 | return unix_peer(osk) == NULL || unix_our_peer(sk, osk); | 167 | return unix_peer(osk) == NULL || unix_our_peer(sk, osk); |
168 | } | 168 | } |
169 | 169 | ||
170 | static inline int unix_recvq_full(struct sock const *sk) | 170 | static inline int unix_recvq_full(struct sock const *sk) |
171 | { | 171 | { |
172 | return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; | 172 | return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; |
173 | } | 173 | } |
174 | 174 | ||
175 | static struct sock *unix_peer_get(struct sock *s) | 175 | static struct sock *unix_peer_get(struct sock *s) |
176 | { | 176 | { |
177 | struct sock *peer; | 177 | struct sock *peer; |
178 | 178 | ||
179 | unix_state_lock(s); | 179 | unix_state_lock(s); |
180 | peer = unix_peer(s); | 180 | peer = unix_peer(s); |
181 | if (peer) | 181 | if (peer) |
182 | sock_hold(peer); | 182 | sock_hold(peer); |
183 | unix_state_unlock(s); | 183 | unix_state_unlock(s); |
184 | return peer; | 184 | return peer; |
185 | } | 185 | } |
186 | 186 | ||
187 | static inline void unix_release_addr(struct unix_address *addr) | 187 | static inline void unix_release_addr(struct unix_address *addr) |
188 | { | 188 | { |
189 | if (atomic_dec_and_test(&addr->refcnt)) | 189 | if (atomic_dec_and_test(&addr->refcnt)) |
190 | kfree(addr); | 190 | kfree(addr); |
191 | } | 191 | } |
192 | 192 | ||
193 | /* | 193 | /* |
194 | * Check unix socket name: | 194 | * Check unix socket name: |
195 | * - should be not zero length. | 195 | * - should be not zero length. |
196 | * - if started by not zero, should be NULL terminated (FS object) | 196 | * - if started by not zero, should be NULL terminated (FS object) |
197 | * - if started by zero, it is abstract name. | 197 | * - if started by zero, it is abstract name. |
198 | */ | 198 | */ |
199 | 199 | ||
200 | static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned *hashp) | 200 | static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned *hashp) |
201 | { | 201 | { |
202 | if (len <= sizeof(short) || len > sizeof(*sunaddr)) | 202 | if (len <= sizeof(short) || len > sizeof(*sunaddr)) |
203 | return -EINVAL; | 203 | return -EINVAL; |
204 | if (!sunaddr || sunaddr->sun_family != AF_UNIX) | 204 | if (!sunaddr || sunaddr->sun_family != AF_UNIX) |
205 | return -EINVAL; | 205 | return -EINVAL; |
206 | if (sunaddr->sun_path[0]) { | 206 | if (sunaddr->sun_path[0]) { |
207 | /* | 207 | /* |
208 | * This may look like an off by one error but it is a bit more | 208 | * This may look like an off by one error but it is a bit more |
209 | * subtle. 108 is the longest valid AF_UNIX path for a binding. | 209 | * subtle. 108 is the longest valid AF_UNIX path for a binding. |
210 | * sun_path[108] doesnt as such exist. However in kernel space | 210 | * sun_path[108] doesnt as such exist. However in kernel space |
211 | * we are guaranteed that it is a valid memory location in our | 211 | * we are guaranteed that it is a valid memory location in our |
212 | * kernel address buffer. | 212 | * kernel address buffer. |
213 | */ | 213 | */ |
214 | ((char *)sunaddr)[len] = 0; | 214 | ((char *)sunaddr)[len] = 0; |
215 | len = strlen(sunaddr->sun_path)+1+sizeof(short); | 215 | len = strlen(sunaddr->sun_path)+1+sizeof(short); |
216 | return len; | 216 | return len; |
217 | } | 217 | } |
218 | 218 | ||
219 | *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0)); | 219 | *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0)); |
220 | return len; | 220 | return len; |
221 | } | 221 | } |
222 | 222 | ||
223 | static void __unix_remove_socket(struct sock *sk) | 223 | static void __unix_remove_socket(struct sock *sk) |
224 | { | 224 | { |
225 | sk_del_node_init(sk); | 225 | sk_del_node_init(sk); |
226 | } | 226 | } |
227 | 227 | ||
228 | static void __unix_insert_socket(struct hlist_head *list, struct sock *sk) | 228 | static void __unix_insert_socket(struct hlist_head *list, struct sock *sk) |
229 | { | 229 | { |
230 | WARN_ON(!sk_unhashed(sk)); | 230 | WARN_ON(!sk_unhashed(sk)); |
231 | sk_add_node(sk, list); | 231 | sk_add_node(sk, list); |
232 | } | 232 | } |
233 | 233 | ||
234 | static inline void unix_remove_socket(struct sock *sk) | 234 | static inline void unix_remove_socket(struct sock *sk) |
235 | { | 235 | { |
236 | spin_lock(&unix_table_lock); | 236 | spin_lock(&unix_table_lock); |
237 | __unix_remove_socket(sk); | 237 | __unix_remove_socket(sk); |
238 | spin_unlock(&unix_table_lock); | 238 | spin_unlock(&unix_table_lock); |
239 | } | 239 | } |
240 | 240 | ||
241 | static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk) | 241 | static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk) |
242 | { | 242 | { |
243 | spin_lock(&unix_table_lock); | 243 | spin_lock(&unix_table_lock); |
244 | __unix_insert_socket(list, sk); | 244 | __unix_insert_socket(list, sk); |
245 | spin_unlock(&unix_table_lock); | 245 | spin_unlock(&unix_table_lock); |
246 | } | 246 | } |
247 | 247 | ||
248 | static struct sock *__unix_find_socket_byname(struct net *net, | 248 | static struct sock *__unix_find_socket_byname(struct net *net, |
249 | struct sockaddr_un *sunname, | 249 | struct sockaddr_un *sunname, |
250 | int len, int type, unsigned hash) | 250 | int len, int type, unsigned hash) |
251 | { | 251 | { |
252 | struct sock *s; | 252 | struct sock *s; |
253 | struct hlist_node *node; | 253 | struct hlist_node *node; |
254 | 254 | ||
255 | sk_for_each(s, node, &unix_socket_table[hash ^ type]) { | 255 | sk_for_each(s, node, &unix_socket_table[hash ^ type]) { |
256 | struct unix_sock *u = unix_sk(s); | 256 | struct unix_sock *u = unix_sk(s); |
257 | 257 | ||
258 | if (!net_eq(sock_net(s), net)) | 258 | if (!net_eq(sock_net(s), net)) |
259 | continue; | 259 | continue; |
260 | 260 | ||
261 | if (u->addr->len == len && | 261 | if (u->addr->len == len && |
262 | !memcmp(u->addr->name, sunname, len)) | 262 | !memcmp(u->addr->name, sunname, len)) |
263 | goto found; | 263 | goto found; |
264 | } | 264 | } |
265 | s = NULL; | 265 | s = NULL; |
266 | found: | 266 | found: |
267 | return s; | 267 | return s; |
268 | } | 268 | } |
269 | 269 | ||
270 | static inline struct sock *unix_find_socket_byname(struct net *net, | 270 | static inline struct sock *unix_find_socket_byname(struct net *net, |
271 | struct sockaddr_un *sunname, | 271 | struct sockaddr_un *sunname, |
272 | int len, int type, | 272 | int len, int type, |
273 | unsigned hash) | 273 | unsigned hash) |
274 | { | 274 | { |
275 | struct sock *s; | 275 | struct sock *s; |
276 | 276 | ||
277 | spin_lock(&unix_table_lock); | 277 | spin_lock(&unix_table_lock); |
278 | s = __unix_find_socket_byname(net, sunname, len, type, hash); | 278 | s = __unix_find_socket_byname(net, sunname, len, type, hash); |
279 | if (s) | 279 | if (s) |
280 | sock_hold(s); | 280 | sock_hold(s); |
281 | spin_unlock(&unix_table_lock); | 281 | spin_unlock(&unix_table_lock); |
282 | return s; | 282 | return s; |
283 | } | 283 | } |
284 | 284 | ||
285 | static struct sock *unix_find_socket_byinode(struct inode *i) | 285 | static struct sock *unix_find_socket_byinode(struct inode *i) |
286 | { | 286 | { |
287 | struct sock *s; | 287 | struct sock *s; |
288 | struct hlist_node *node; | 288 | struct hlist_node *node; |
289 | 289 | ||
290 | spin_lock(&unix_table_lock); | 290 | spin_lock(&unix_table_lock); |
291 | sk_for_each(s, node, | 291 | sk_for_each(s, node, |
292 | &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) { | 292 | &unix_socket_table[i->i_ino & (UNIX_HASH_SIZE - 1)]) { |
293 | struct dentry *dentry = unix_sk(s)->dentry; | 293 | struct dentry *dentry = unix_sk(s)->dentry; |
294 | 294 | ||
295 | if (dentry && dentry->d_inode == i) { | 295 | if (dentry && dentry->d_inode == i) { |
296 | sock_hold(s); | 296 | sock_hold(s); |
297 | goto found; | 297 | goto found; |
298 | } | 298 | } |
299 | } | 299 | } |
300 | s = NULL; | 300 | s = NULL; |
301 | found: | 301 | found: |
302 | spin_unlock(&unix_table_lock); | 302 | spin_unlock(&unix_table_lock); |
303 | return s; | 303 | return s; |
304 | } | 304 | } |
305 | 305 | ||
306 | static inline int unix_writable(struct sock *sk) | 306 | static inline int unix_writable(struct sock *sk) |
307 | { | 307 | { |
308 | return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf; | 308 | return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf; |
309 | } | 309 | } |
310 | 310 | ||
311 | static void unix_write_space(struct sock *sk) | 311 | static void unix_write_space(struct sock *sk) |
312 | { | 312 | { |
313 | struct socket_wq *wq; | 313 | struct socket_wq *wq; |
314 | 314 | ||
315 | rcu_read_lock(); | 315 | rcu_read_lock(); |
316 | if (unix_writable(sk)) { | 316 | if (unix_writable(sk)) { |
317 | wq = rcu_dereference(sk->sk_wq); | 317 | wq = rcu_dereference(sk->sk_wq); |
318 | if (wq_has_sleeper(wq)) | 318 | if (wq_has_sleeper(wq)) |
319 | wake_up_interruptible_sync(&wq->wait); | 319 | wake_up_interruptible_sync(&wq->wait); |
320 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); | 320 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
321 | } | 321 | } |
322 | rcu_read_unlock(); | 322 | rcu_read_unlock(); |
323 | } | 323 | } |
324 | 324 | ||
325 | /* When dgram socket disconnects (or changes its peer), we clear its receive | 325 | /* When dgram socket disconnects (or changes its peer), we clear its receive |
326 | * queue of packets arrived from previous peer. First, it allows to do | 326 | * queue of packets arrived from previous peer. First, it allows to do |
327 | * flow control based only on wmem_alloc; second, sk connected to peer | 327 | * flow control based only on wmem_alloc; second, sk connected to peer |
328 | * may receive messages only from that peer. */ | 328 | * may receive messages only from that peer. */ |
329 | static void unix_dgram_disconnected(struct sock *sk, struct sock *other) | 329 | static void unix_dgram_disconnected(struct sock *sk, struct sock *other) |
330 | { | 330 | { |
331 | if (!skb_queue_empty(&sk->sk_receive_queue)) { | 331 | if (!skb_queue_empty(&sk->sk_receive_queue)) { |
332 | skb_queue_purge(&sk->sk_receive_queue); | 332 | skb_queue_purge(&sk->sk_receive_queue); |
333 | wake_up_interruptible_all(&unix_sk(sk)->peer_wait); | 333 | wake_up_interruptible_all(&unix_sk(sk)->peer_wait); |
334 | 334 | ||
335 | /* If one link of bidirectional dgram pipe is disconnected, | 335 | /* If one link of bidirectional dgram pipe is disconnected, |
336 | * we signal error. Messages are lost. Do not make this, | 336 | * we signal error. Messages are lost. Do not make this, |
337 | * when peer was not connected to us. | 337 | * when peer was not connected to us. |
338 | */ | 338 | */ |
339 | if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) { | 339 | if (!sock_flag(other, SOCK_DEAD) && unix_peer(other) == sk) { |
340 | other->sk_err = ECONNRESET; | 340 | other->sk_err = ECONNRESET; |
341 | other->sk_error_report(other); | 341 | other->sk_error_report(other); |
342 | } | 342 | } |
343 | } | 343 | } |
344 | } | 344 | } |
345 | 345 | ||
346 | static void unix_sock_destructor(struct sock *sk) | 346 | static void unix_sock_destructor(struct sock *sk) |
347 | { | 347 | { |
348 | struct unix_sock *u = unix_sk(sk); | 348 | struct unix_sock *u = unix_sk(sk); |
349 | 349 | ||
350 | skb_queue_purge(&sk->sk_receive_queue); | 350 | skb_queue_purge(&sk->sk_receive_queue); |
351 | 351 | ||
352 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); | 352 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
353 | WARN_ON(!sk_unhashed(sk)); | 353 | WARN_ON(!sk_unhashed(sk)); |
354 | WARN_ON(sk->sk_socket); | 354 | WARN_ON(sk->sk_socket); |
355 | if (!sock_flag(sk, SOCK_DEAD)) { | 355 | if (!sock_flag(sk, SOCK_DEAD)) { |
356 | printk(KERN_INFO "Attempt to release alive unix socket: %p\n", sk); | 356 | printk(KERN_INFO "Attempt to release alive unix socket: %p\n", sk); |
357 | return; | 357 | return; |
358 | } | 358 | } |
359 | 359 | ||
360 | if (u->addr) | 360 | if (u->addr) |
361 | unix_release_addr(u->addr); | 361 | unix_release_addr(u->addr); |
362 | 362 | ||
363 | atomic_dec(&unix_nr_socks); | 363 | atomic_dec(&unix_nr_socks); |
364 | local_bh_disable(); | 364 | local_bh_disable(); |
365 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); | 365 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
366 | local_bh_enable(); | 366 | local_bh_enable(); |
367 | #ifdef UNIX_REFCNT_DEBUG | 367 | #ifdef UNIX_REFCNT_DEBUG |
368 | printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, | 368 | printk(KERN_DEBUG "UNIX %p is destroyed, %d are still alive.\n", sk, |
369 | atomic_read(&unix_nr_socks)); | 369 | atomic_read(&unix_nr_socks)); |
370 | #endif | 370 | #endif |
371 | } | 371 | } |
372 | 372 | ||
373 | static int unix_release_sock(struct sock *sk, int embrion) | 373 | static int unix_release_sock(struct sock *sk, int embrion) |
374 | { | 374 | { |
375 | struct unix_sock *u = unix_sk(sk); | 375 | struct unix_sock *u = unix_sk(sk); |
376 | struct dentry *dentry; | 376 | struct dentry *dentry; |
377 | struct vfsmount *mnt; | 377 | struct vfsmount *mnt; |
378 | struct sock *skpair; | 378 | struct sock *skpair; |
379 | struct sk_buff *skb; | 379 | struct sk_buff *skb; |
380 | int state; | 380 | int state; |
381 | 381 | ||
382 | unix_remove_socket(sk); | 382 | unix_remove_socket(sk); |
383 | 383 | ||
384 | /* Clear state */ | 384 | /* Clear state */ |
385 | unix_state_lock(sk); | 385 | unix_state_lock(sk); |
386 | sock_orphan(sk); | 386 | sock_orphan(sk); |
387 | sk->sk_shutdown = SHUTDOWN_MASK; | 387 | sk->sk_shutdown = SHUTDOWN_MASK; |
388 | dentry = u->dentry; | 388 | dentry = u->dentry; |
389 | u->dentry = NULL; | 389 | u->dentry = NULL; |
390 | mnt = u->mnt; | 390 | mnt = u->mnt; |
391 | u->mnt = NULL; | 391 | u->mnt = NULL; |
392 | state = sk->sk_state; | 392 | state = sk->sk_state; |
393 | sk->sk_state = TCP_CLOSE; | 393 | sk->sk_state = TCP_CLOSE; |
394 | unix_state_unlock(sk); | 394 | unix_state_unlock(sk); |
395 | 395 | ||
396 | wake_up_interruptible_all(&u->peer_wait); | 396 | wake_up_interruptible_all(&u->peer_wait); |
397 | 397 | ||
398 | skpair = unix_peer(sk); | 398 | skpair = unix_peer(sk); |
399 | 399 | ||
400 | if (skpair != NULL) { | 400 | if (skpair != NULL) { |
401 | if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) { | 401 | if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) { |
402 | unix_state_lock(skpair); | 402 | unix_state_lock(skpair); |
403 | /* No more writes */ | 403 | /* No more writes */ |
404 | skpair->sk_shutdown = SHUTDOWN_MASK; | 404 | skpair->sk_shutdown = SHUTDOWN_MASK; |
405 | if (!skb_queue_empty(&sk->sk_receive_queue) || embrion) | 405 | if (!skb_queue_empty(&sk->sk_receive_queue) || embrion) |
406 | skpair->sk_err = ECONNRESET; | 406 | skpair->sk_err = ECONNRESET; |
407 | unix_state_unlock(skpair); | 407 | unix_state_unlock(skpair); |
408 | skpair->sk_state_change(skpair); | 408 | skpair->sk_state_change(skpair); |
409 | sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP); | 409 | sk_wake_async(skpair, SOCK_WAKE_WAITD, POLL_HUP); |
410 | } | 410 | } |
411 | sock_put(skpair); /* It may now die */ | 411 | sock_put(skpair); /* It may now die */ |
412 | unix_peer(sk) = NULL; | 412 | unix_peer(sk) = NULL; |
413 | } | 413 | } |
414 | 414 | ||
415 | /* Try to flush out this socket. Throw out buffers at least */ | 415 | /* Try to flush out this socket. Throw out buffers at least */ |
416 | 416 | ||
417 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { | 417 | while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { |
418 | if (state == TCP_LISTEN) | 418 | if (state == TCP_LISTEN) |
419 | unix_release_sock(skb->sk, 1); | 419 | unix_release_sock(skb->sk, 1); |
420 | /* passed fds are erased in the kfree_skb hook */ | 420 | /* passed fds are erased in the kfree_skb hook */ |
421 | kfree_skb(skb); | 421 | kfree_skb(skb); |
422 | } | 422 | } |
423 | 423 | ||
424 | if (dentry) { | 424 | if (dentry) { |
425 | dput(dentry); | 425 | dput(dentry); |
426 | mntput(mnt); | 426 | mntput(mnt); |
427 | } | 427 | } |
428 | 428 | ||
429 | sock_put(sk); | 429 | sock_put(sk); |
430 | 430 | ||
431 | /* ---- Socket is dead now and most probably destroyed ---- */ | 431 | /* ---- Socket is dead now and most probably destroyed ---- */ |
432 | 432 | ||
433 | /* | 433 | /* |
434 | * Fixme: BSD difference: In BSD all sockets connected to use get | 434 | * Fixme: BSD difference: In BSD all sockets connected to use get |
435 | * ECONNRESET and we die on the spot. In Linux we behave | 435 | * ECONNRESET and we die on the spot. In Linux we behave |
436 | * like files and pipes do and wait for the last | 436 | * like files and pipes do and wait for the last |
437 | * dereference. | 437 | * dereference. |
438 | * | 438 | * |
439 | * Can't we simply set sock->err? | 439 | * Can't we simply set sock->err? |
440 | * | 440 | * |
441 | * What the above comment does talk about? --ANK(980817) | 441 | * What the above comment does talk about? --ANK(980817) |
442 | */ | 442 | */ |
443 | 443 | ||
444 | if (unix_tot_inflight) | 444 | if (unix_tot_inflight) |
445 | unix_gc(); /* Garbage collect fds */ | 445 | unix_gc(); /* Garbage collect fds */ |
446 | 446 | ||
447 | return 0; | 447 | return 0; |
448 | } | 448 | } |
449 | 449 | ||
450 | static void init_peercred(struct sock *sk) | 450 | static void init_peercred(struct sock *sk) |
451 | { | 451 | { |
452 | put_pid(sk->sk_peer_pid); | 452 | put_pid(sk->sk_peer_pid); |
453 | if (sk->sk_peer_cred) | 453 | if (sk->sk_peer_cred) |
454 | put_cred(sk->sk_peer_cred); | 454 | put_cred(sk->sk_peer_cred); |
455 | sk->sk_peer_pid = get_pid(task_tgid(current)); | 455 | sk->sk_peer_pid = get_pid(task_tgid(current)); |
456 | sk->sk_peer_cred = get_current_cred(); | 456 | sk->sk_peer_cred = get_current_cred(); |
457 | } | 457 | } |
458 | 458 | ||
459 | static void copy_peercred(struct sock *sk, struct sock *peersk) | 459 | static void copy_peercred(struct sock *sk, struct sock *peersk) |
460 | { | 460 | { |
461 | put_pid(sk->sk_peer_pid); | 461 | put_pid(sk->sk_peer_pid); |
462 | if (sk->sk_peer_cred) | 462 | if (sk->sk_peer_cred) |
463 | put_cred(sk->sk_peer_cred); | 463 | put_cred(sk->sk_peer_cred); |
464 | sk->sk_peer_pid = get_pid(peersk->sk_peer_pid); | 464 | sk->sk_peer_pid = get_pid(peersk->sk_peer_pid); |
465 | sk->sk_peer_cred = get_cred(peersk->sk_peer_cred); | 465 | sk->sk_peer_cred = get_cred(peersk->sk_peer_cred); |
466 | } | 466 | } |
467 | 467 | ||
468 | static int unix_listen(struct socket *sock, int backlog) | 468 | static int unix_listen(struct socket *sock, int backlog) |
469 | { | 469 | { |
470 | int err; | 470 | int err; |
471 | struct sock *sk = sock->sk; | 471 | struct sock *sk = sock->sk; |
472 | struct unix_sock *u = unix_sk(sk); | 472 | struct unix_sock *u = unix_sk(sk); |
473 | struct pid *old_pid = NULL; | 473 | struct pid *old_pid = NULL; |
474 | const struct cred *old_cred = NULL; | 474 | const struct cred *old_cred = NULL; |
475 | 475 | ||
476 | err = -EOPNOTSUPP; | 476 | err = -EOPNOTSUPP; |
477 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) | 477 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
478 | goto out; /* Only stream/seqpacket sockets accept */ | 478 | goto out; /* Only stream/seqpacket sockets accept */ |
479 | err = -EINVAL; | 479 | err = -EINVAL; |
480 | if (!u->addr) | 480 | if (!u->addr) |
481 | goto out; /* No listens on an unbound socket */ | 481 | goto out; /* No listens on an unbound socket */ |
482 | unix_state_lock(sk); | 482 | unix_state_lock(sk); |
483 | if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN) | 483 | if (sk->sk_state != TCP_CLOSE && sk->sk_state != TCP_LISTEN) |
484 | goto out_unlock; | 484 | goto out_unlock; |
485 | if (backlog > sk->sk_max_ack_backlog) | 485 | if (backlog > sk->sk_max_ack_backlog) |
486 | wake_up_interruptible_all(&u->peer_wait); | 486 | wake_up_interruptible_all(&u->peer_wait); |
487 | sk->sk_max_ack_backlog = backlog; | 487 | sk->sk_max_ack_backlog = backlog; |
488 | sk->sk_state = TCP_LISTEN; | 488 | sk->sk_state = TCP_LISTEN; |
489 | /* set credentials so connect can copy them */ | 489 | /* set credentials so connect can copy them */ |
490 | init_peercred(sk); | 490 | init_peercred(sk); |
491 | err = 0; | 491 | err = 0; |
492 | 492 | ||
493 | out_unlock: | 493 | out_unlock: |
494 | unix_state_unlock(sk); | 494 | unix_state_unlock(sk); |
495 | put_pid(old_pid); | 495 | put_pid(old_pid); |
496 | if (old_cred) | 496 | if (old_cred) |
497 | put_cred(old_cred); | 497 | put_cred(old_cred); |
498 | out: | 498 | out: |
499 | return err; | 499 | return err; |
500 | } | 500 | } |
501 | 501 | ||
502 | static int unix_release(struct socket *); | 502 | static int unix_release(struct socket *); |
503 | static int unix_bind(struct socket *, struct sockaddr *, int); | 503 | static int unix_bind(struct socket *, struct sockaddr *, int); |
504 | static int unix_stream_connect(struct socket *, struct sockaddr *, | 504 | static int unix_stream_connect(struct socket *, struct sockaddr *, |
505 | int addr_len, int flags); | 505 | int addr_len, int flags); |
506 | static int unix_socketpair(struct socket *, struct socket *); | 506 | static int unix_socketpair(struct socket *, struct socket *); |
507 | static int unix_accept(struct socket *, struct socket *, int); | 507 | static int unix_accept(struct socket *, struct socket *, int); |
508 | static int unix_getname(struct socket *, struct sockaddr *, int *, int); | 508 | static int unix_getname(struct socket *, struct sockaddr *, int *, int); |
509 | static unsigned int unix_poll(struct file *, struct socket *, poll_table *); | 509 | static unsigned int unix_poll(struct file *, struct socket *, poll_table *); |
510 | static unsigned int unix_dgram_poll(struct file *, struct socket *, | 510 | static unsigned int unix_dgram_poll(struct file *, struct socket *, |
511 | poll_table *); | 511 | poll_table *); |
512 | static int unix_ioctl(struct socket *, unsigned int, unsigned long); | 512 | static int unix_ioctl(struct socket *, unsigned int, unsigned long); |
513 | static int unix_shutdown(struct socket *, int); | 513 | static int unix_shutdown(struct socket *, int); |
514 | static int unix_stream_sendmsg(struct kiocb *, struct socket *, | 514 | static int unix_stream_sendmsg(struct kiocb *, struct socket *, |
515 | struct msghdr *, size_t); | 515 | struct msghdr *, size_t); |
516 | static int unix_stream_recvmsg(struct kiocb *, struct socket *, | 516 | static int unix_stream_recvmsg(struct kiocb *, struct socket *, |
517 | struct msghdr *, size_t, int); | 517 | struct msghdr *, size_t, int); |
518 | static int unix_dgram_sendmsg(struct kiocb *, struct socket *, | 518 | static int unix_dgram_sendmsg(struct kiocb *, struct socket *, |
519 | struct msghdr *, size_t); | 519 | struct msghdr *, size_t); |
520 | static int unix_dgram_recvmsg(struct kiocb *, struct socket *, | 520 | static int unix_dgram_recvmsg(struct kiocb *, struct socket *, |
521 | struct msghdr *, size_t, int); | 521 | struct msghdr *, size_t, int); |
522 | static int unix_dgram_connect(struct socket *, struct sockaddr *, | 522 | static int unix_dgram_connect(struct socket *, struct sockaddr *, |
523 | int, int); | 523 | int, int); |
524 | static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *, | 524 | static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *, |
525 | struct msghdr *, size_t); | 525 | struct msghdr *, size_t); |
526 | 526 | ||
527 | static const struct proto_ops unix_stream_ops = { | 527 | static const struct proto_ops unix_stream_ops = { |
528 | .family = PF_UNIX, | 528 | .family = PF_UNIX, |
529 | .owner = THIS_MODULE, | 529 | .owner = THIS_MODULE, |
530 | .release = unix_release, | 530 | .release = unix_release, |
531 | .bind = unix_bind, | 531 | .bind = unix_bind, |
532 | .connect = unix_stream_connect, | 532 | .connect = unix_stream_connect, |
533 | .socketpair = unix_socketpair, | 533 | .socketpair = unix_socketpair, |
534 | .accept = unix_accept, | 534 | .accept = unix_accept, |
535 | .getname = unix_getname, | 535 | .getname = unix_getname, |
536 | .poll = unix_poll, | 536 | .poll = unix_poll, |
537 | .ioctl = unix_ioctl, | 537 | .ioctl = unix_ioctl, |
538 | .listen = unix_listen, | 538 | .listen = unix_listen, |
539 | .shutdown = unix_shutdown, | 539 | .shutdown = unix_shutdown, |
540 | .setsockopt = sock_no_setsockopt, | 540 | .setsockopt = sock_no_setsockopt, |
541 | .getsockopt = sock_no_getsockopt, | 541 | .getsockopt = sock_no_getsockopt, |
542 | .sendmsg = unix_stream_sendmsg, | 542 | .sendmsg = unix_stream_sendmsg, |
543 | .recvmsg = unix_stream_recvmsg, | 543 | .recvmsg = unix_stream_recvmsg, |
544 | .mmap = sock_no_mmap, | 544 | .mmap = sock_no_mmap, |
545 | .sendpage = sock_no_sendpage, | 545 | .sendpage = sock_no_sendpage, |
546 | }; | 546 | }; |
547 | 547 | ||
548 | static const struct proto_ops unix_dgram_ops = { | 548 | static const struct proto_ops unix_dgram_ops = { |
549 | .family = PF_UNIX, | 549 | .family = PF_UNIX, |
550 | .owner = THIS_MODULE, | 550 | .owner = THIS_MODULE, |
551 | .release = unix_release, | 551 | .release = unix_release, |
552 | .bind = unix_bind, | 552 | .bind = unix_bind, |
553 | .connect = unix_dgram_connect, | 553 | .connect = unix_dgram_connect, |
554 | .socketpair = unix_socketpair, | 554 | .socketpair = unix_socketpair, |
555 | .accept = sock_no_accept, | 555 | .accept = sock_no_accept, |
556 | .getname = unix_getname, | 556 | .getname = unix_getname, |
557 | .poll = unix_dgram_poll, | 557 | .poll = unix_dgram_poll, |
558 | .ioctl = unix_ioctl, | 558 | .ioctl = unix_ioctl, |
559 | .listen = sock_no_listen, | 559 | .listen = sock_no_listen, |
560 | .shutdown = unix_shutdown, | 560 | .shutdown = unix_shutdown, |
561 | .setsockopt = sock_no_setsockopt, | 561 | .setsockopt = sock_no_setsockopt, |
562 | .getsockopt = sock_no_getsockopt, | 562 | .getsockopt = sock_no_getsockopt, |
563 | .sendmsg = unix_dgram_sendmsg, | 563 | .sendmsg = unix_dgram_sendmsg, |
564 | .recvmsg = unix_dgram_recvmsg, | 564 | .recvmsg = unix_dgram_recvmsg, |
565 | .mmap = sock_no_mmap, | 565 | .mmap = sock_no_mmap, |
566 | .sendpage = sock_no_sendpage, | 566 | .sendpage = sock_no_sendpage, |
567 | }; | 567 | }; |
568 | 568 | ||
569 | static const struct proto_ops unix_seqpacket_ops = { | 569 | static const struct proto_ops unix_seqpacket_ops = { |
570 | .family = PF_UNIX, | 570 | .family = PF_UNIX, |
571 | .owner = THIS_MODULE, | 571 | .owner = THIS_MODULE, |
572 | .release = unix_release, | 572 | .release = unix_release, |
573 | .bind = unix_bind, | 573 | .bind = unix_bind, |
574 | .connect = unix_stream_connect, | 574 | .connect = unix_stream_connect, |
575 | .socketpair = unix_socketpair, | 575 | .socketpair = unix_socketpair, |
576 | .accept = unix_accept, | 576 | .accept = unix_accept, |
577 | .getname = unix_getname, | 577 | .getname = unix_getname, |
578 | .poll = unix_dgram_poll, | 578 | .poll = unix_dgram_poll, |
579 | .ioctl = unix_ioctl, | 579 | .ioctl = unix_ioctl, |
580 | .listen = unix_listen, | 580 | .listen = unix_listen, |
581 | .shutdown = unix_shutdown, | 581 | .shutdown = unix_shutdown, |
582 | .setsockopt = sock_no_setsockopt, | 582 | .setsockopt = sock_no_setsockopt, |
583 | .getsockopt = sock_no_getsockopt, | 583 | .getsockopt = sock_no_getsockopt, |
584 | .sendmsg = unix_seqpacket_sendmsg, | 584 | .sendmsg = unix_seqpacket_sendmsg, |
585 | .recvmsg = unix_dgram_recvmsg, | 585 | .recvmsg = unix_dgram_recvmsg, |
586 | .mmap = sock_no_mmap, | 586 | .mmap = sock_no_mmap, |
587 | .sendpage = sock_no_sendpage, | 587 | .sendpage = sock_no_sendpage, |
588 | }; | 588 | }; |
589 | 589 | ||
590 | static struct proto unix_proto = { | 590 | static struct proto unix_proto = { |
591 | .name = "UNIX", | 591 | .name = "UNIX", |
592 | .owner = THIS_MODULE, | 592 | .owner = THIS_MODULE, |
593 | .obj_size = sizeof(struct unix_sock), | 593 | .obj_size = sizeof(struct unix_sock), |
594 | }; | 594 | }; |
595 | 595 | ||
596 | /* | 596 | /* |
597 | * AF_UNIX sockets do not interact with hardware, hence they | 597 | * AF_UNIX sockets do not interact with hardware, hence they |
598 | * dont trigger interrupts - so it's safe for them to have | 598 | * dont trigger interrupts - so it's safe for them to have |
599 | * bh-unsafe locking for their sk_receive_queue.lock. Split off | 599 | * bh-unsafe locking for their sk_receive_queue.lock. Split off |
600 | * this special lock-class by reinitializing the spinlock key: | 600 | * this special lock-class by reinitializing the spinlock key: |
601 | */ | 601 | */ |
602 | static struct lock_class_key af_unix_sk_receive_queue_lock_key; | 602 | static struct lock_class_key af_unix_sk_receive_queue_lock_key; |
603 | 603 | ||
604 | static struct sock *unix_create1(struct net *net, struct socket *sock) | 604 | static struct sock *unix_create1(struct net *net, struct socket *sock) |
605 | { | 605 | { |
606 | struct sock *sk = NULL; | 606 | struct sock *sk = NULL; |
607 | struct unix_sock *u; | 607 | struct unix_sock *u; |
608 | 608 | ||
609 | atomic_inc(&unix_nr_socks); | 609 | atomic_inc(&unix_nr_socks); |
610 | if (atomic_read(&unix_nr_socks) > 2 * get_max_files()) | 610 | if (atomic_read(&unix_nr_socks) > 2 * get_max_files()) |
611 | goto out; | 611 | goto out; |
612 | 612 | ||
613 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto); | 613 | sk = sk_alloc(net, PF_UNIX, GFP_KERNEL, &unix_proto); |
614 | if (!sk) | 614 | if (!sk) |
615 | goto out; | 615 | goto out; |
616 | 616 | ||
617 | sock_init_data(sock, sk); | 617 | sock_init_data(sock, sk); |
618 | lockdep_set_class(&sk->sk_receive_queue.lock, | 618 | lockdep_set_class(&sk->sk_receive_queue.lock, |
619 | &af_unix_sk_receive_queue_lock_key); | 619 | &af_unix_sk_receive_queue_lock_key); |
620 | 620 | ||
621 | sk->sk_write_space = unix_write_space; | 621 | sk->sk_write_space = unix_write_space; |
622 | sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen; | 622 | sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen; |
623 | sk->sk_destruct = unix_sock_destructor; | 623 | sk->sk_destruct = unix_sock_destructor; |
624 | u = unix_sk(sk); | 624 | u = unix_sk(sk); |
625 | u->dentry = NULL; | 625 | u->dentry = NULL; |
626 | u->mnt = NULL; | 626 | u->mnt = NULL; |
627 | spin_lock_init(&u->lock); | 627 | spin_lock_init(&u->lock); |
628 | atomic_long_set(&u->inflight, 0); | 628 | atomic_long_set(&u->inflight, 0); |
629 | INIT_LIST_HEAD(&u->link); | 629 | INIT_LIST_HEAD(&u->link); |
630 | mutex_init(&u->readlock); /* single task reading lock */ | 630 | mutex_init(&u->readlock); /* single task reading lock */ |
631 | init_waitqueue_head(&u->peer_wait); | 631 | init_waitqueue_head(&u->peer_wait); |
632 | unix_insert_socket(unix_sockets_unbound, sk); | 632 | unix_insert_socket(unix_sockets_unbound, sk); |
633 | out: | 633 | out: |
634 | if (sk == NULL) | 634 | if (sk == NULL) |
635 | atomic_dec(&unix_nr_socks); | 635 | atomic_dec(&unix_nr_socks); |
636 | else { | 636 | else { |
637 | local_bh_disable(); | 637 | local_bh_disable(); |
638 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); | 638 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
639 | local_bh_enable(); | 639 | local_bh_enable(); |
640 | } | 640 | } |
641 | return sk; | 641 | return sk; |
642 | } | 642 | } |
643 | 643 | ||
644 | static int unix_create(struct net *net, struct socket *sock, int protocol, | 644 | static int unix_create(struct net *net, struct socket *sock, int protocol, |
645 | int kern) | 645 | int kern) |
646 | { | 646 | { |
647 | if (protocol && protocol != PF_UNIX) | 647 | if (protocol && protocol != PF_UNIX) |
648 | return -EPROTONOSUPPORT; | 648 | return -EPROTONOSUPPORT; |
649 | 649 | ||
650 | sock->state = SS_UNCONNECTED; | 650 | sock->state = SS_UNCONNECTED; |
651 | 651 | ||
652 | switch (sock->type) { | 652 | switch (sock->type) { |
653 | case SOCK_STREAM: | 653 | case SOCK_STREAM: |
654 | sock->ops = &unix_stream_ops; | 654 | sock->ops = &unix_stream_ops; |
655 | break; | 655 | break; |
656 | /* | 656 | /* |
657 | * Believe it or not BSD has AF_UNIX, SOCK_RAW though | 657 | * Believe it or not BSD has AF_UNIX, SOCK_RAW though |
658 | * nothing uses it. | 658 | * nothing uses it. |
659 | */ | 659 | */ |
660 | case SOCK_RAW: | 660 | case SOCK_RAW: |
661 | sock->type = SOCK_DGRAM; | 661 | sock->type = SOCK_DGRAM; |
662 | case SOCK_DGRAM: | 662 | case SOCK_DGRAM: |
663 | sock->ops = &unix_dgram_ops; | 663 | sock->ops = &unix_dgram_ops; |
664 | break; | 664 | break; |
665 | case SOCK_SEQPACKET: | 665 | case SOCK_SEQPACKET: |
666 | sock->ops = &unix_seqpacket_ops; | 666 | sock->ops = &unix_seqpacket_ops; |
667 | break; | 667 | break; |
668 | default: | 668 | default: |
669 | return -ESOCKTNOSUPPORT; | 669 | return -ESOCKTNOSUPPORT; |
670 | } | 670 | } |
671 | 671 | ||
672 | return unix_create1(net, sock) ? 0 : -ENOMEM; | 672 | return unix_create1(net, sock) ? 0 : -ENOMEM; |
673 | } | 673 | } |
674 | 674 | ||
675 | static int unix_release(struct socket *sock) | 675 | static int unix_release(struct socket *sock) |
676 | { | 676 | { |
677 | struct sock *sk = sock->sk; | 677 | struct sock *sk = sock->sk; |
678 | 678 | ||
679 | if (!sk) | 679 | if (!sk) |
680 | return 0; | 680 | return 0; |
681 | 681 | ||
682 | sock->sk = NULL; | 682 | sock->sk = NULL; |
683 | 683 | ||
684 | return unix_release_sock(sk, 0); | 684 | return unix_release_sock(sk, 0); |
685 | } | 685 | } |
686 | 686 | ||
687 | static int unix_autobind(struct socket *sock) | 687 | static int unix_autobind(struct socket *sock) |
688 | { | 688 | { |
689 | struct sock *sk = sock->sk; | 689 | struct sock *sk = sock->sk; |
690 | struct net *net = sock_net(sk); | 690 | struct net *net = sock_net(sk); |
691 | struct unix_sock *u = unix_sk(sk); | 691 | struct unix_sock *u = unix_sk(sk); |
692 | static u32 ordernum = 1; | 692 | static u32 ordernum = 1; |
693 | struct unix_address *addr; | 693 | struct unix_address *addr; |
694 | int err; | 694 | int err; |
695 | 695 | ||
696 | mutex_lock(&u->readlock); | 696 | mutex_lock(&u->readlock); |
697 | 697 | ||
698 | err = 0; | 698 | err = 0; |
699 | if (u->addr) | 699 | if (u->addr) |
700 | goto out; | 700 | goto out; |
701 | 701 | ||
702 | err = -ENOMEM; | 702 | err = -ENOMEM; |
703 | addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL); | 703 | addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL); |
704 | if (!addr) | 704 | if (!addr) |
705 | goto out; | 705 | goto out; |
706 | 706 | ||
707 | addr->name->sun_family = AF_UNIX; | 707 | addr->name->sun_family = AF_UNIX; |
708 | atomic_set(&addr->refcnt, 1); | 708 | atomic_set(&addr->refcnt, 1); |
709 | 709 | ||
710 | retry: | 710 | retry: |
711 | addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short); | 711 | addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short); |
712 | addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0)); | 712 | addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0)); |
713 | 713 | ||
714 | spin_lock(&unix_table_lock); | 714 | spin_lock(&unix_table_lock); |
715 | ordernum = (ordernum+1)&0xFFFFF; | 715 | ordernum = (ordernum+1)&0xFFFFF; |
716 | 716 | ||
717 | if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type, | 717 | if (__unix_find_socket_byname(net, addr->name, addr->len, sock->type, |
718 | addr->hash)) { | 718 | addr->hash)) { |
719 | spin_unlock(&unix_table_lock); | 719 | spin_unlock(&unix_table_lock); |
720 | /* Sanity yield. It is unusual case, but yet... */ | 720 | /* Sanity yield. It is unusual case, but yet... */ |
721 | if (!(ordernum&0xFF)) | 721 | if (!(ordernum&0xFF)) |
722 | yield(); | 722 | yield(); |
723 | goto retry; | 723 | goto retry; |
724 | } | 724 | } |
725 | addr->hash ^= sk->sk_type; | 725 | addr->hash ^= sk->sk_type; |
726 | 726 | ||
727 | __unix_remove_socket(sk); | 727 | __unix_remove_socket(sk); |
728 | u->addr = addr; | 728 | u->addr = addr; |
729 | __unix_insert_socket(&unix_socket_table[addr->hash], sk); | 729 | __unix_insert_socket(&unix_socket_table[addr->hash], sk); |
730 | spin_unlock(&unix_table_lock); | 730 | spin_unlock(&unix_table_lock); |
731 | err = 0; | 731 | err = 0; |
732 | 732 | ||
733 | out: mutex_unlock(&u->readlock); | 733 | out: mutex_unlock(&u->readlock); |
734 | return err; | 734 | return err; |
735 | } | 735 | } |
736 | 736 | ||
737 | static struct sock *unix_find_other(struct net *net, | 737 | static struct sock *unix_find_other(struct net *net, |
738 | struct sockaddr_un *sunname, int len, | 738 | struct sockaddr_un *sunname, int len, |
739 | int type, unsigned hash, int *error) | 739 | int type, unsigned hash, int *error) |
740 | { | 740 | { |
741 | struct sock *u; | 741 | struct sock *u; |
742 | struct path path; | 742 | struct path path; |
743 | int err = 0; | 743 | int err = 0; |
744 | 744 | ||
745 | if (sunname->sun_path[0]) { | 745 | if (sunname->sun_path[0]) { |
746 | struct inode *inode; | 746 | struct inode *inode; |
747 | err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path); | 747 | err = kern_path(sunname->sun_path, LOOKUP_FOLLOW, &path); |
748 | if (err) | 748 | if (err) |
749 | goto fail; | 749 | goto fail; |
750 | inode = path.dentry->d_inode; | 750 | inode = path.dentry->d_inode; |
751 | err = inode_permission(inode, MAY_WRITE); | 751 | err = inode_permission(inode, MAY_WRITE); |
752 | if (err) | 752 | if (err) |
753 | goto put_fail; | 753 | goto put_fail; |
754 | 754 | ||
755 | err = -ECONNREFUSED; | 755 | err = -ECONNREFUSED; |
756 | if (!S_ISSOCK(inode->i_mode)) | 756 | if (!S_ISSOCK(inode->i_mode)) |
757 | goto put_fail; | 757 | goto put_fail; |
758 | u = unix_find_socket_byinode(inode); | 758 | u = unix_find_socket_byinode(inode); |
759 | if (!u) | 759 | if (!u) |
760 | goto put_fail; | 760 | goto put_fail; |
761 | 761 | ||
762 | if (u->sk_type == type) | 762 | if (u->sk_type == type) |
763 | touch_atime(path.mnt, path.dentry); | 763 | touch_atime(path.mnt, path.dentry); |
764 | 764 | ||
765 | path_put(&path); | 765 | path_put(&path); |
766 | 766 | ||
767 | err = -EPROTOTYPE; | 767 | err = -EPROTOTYPE; |
768 | if (u->sk_type != type) { | 768 | if (u->sk_type != type) { |
769 | sock_put(u); | 769 | sock_put(u); |
770 | goto fail; | 770 | goto fail; |
771 | } | 771 | } |
772 | } else { | 772 | } else { |
773 | err = -ECONNREFUSED; | 773 | err = -ECONNREFUSED; |
774 | u = unix_find_socket_byname(net, sunname, len, type, hash); | 774 | u = unix_find_socket_byname(net, sunname, len, type, hash); |
775 | if (u) { | 775 | if (u) { |
776 | struct dentry *dentry; | 776 | struct dentry *dentry; |
777 | dentry = unix_sk(u)->dentry; | 777 | dentry = unix_sk(u)->dentry; |
778 | if (dentry) | 778 | if (dentry) |
779 | touch_atime(unix_sk(u)->mnt, dentry); | 779 | touch_atime(unix_sk(u)->mnt, dentry); |
780 | } else | 780 | } else |
781 | goto fail; | 781 | goto fail; |
782 | } | 782 | } |
783 | return u; | 783 | return u; |
784 | 784 | ||
785 | put_fail: | 785 | put_fail: |
786 | path_put(&path); | 786 | path_put(&path); |
787 | fail: | 787 | fail: |
788 | *error = err; | 788 | *error = err; |
789 | return NULL; | 789 | return NULL; |
790 | } | 790 | } |
791 | 791 | ||
792 | 792 | ||
793 | static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) | 793 | static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) |
794 | { | 794 | { |
795 | struct sock *sk = sock->sk; | 795 | struct sock *sk = sock->sk; |
796 | struct net *net = sock_net(sk); | 796 | struct net *net = sock_net(sk); |
797 | struct unix_sock *u = unix_sk(sk); | 797 | struct unix_sock *u = unix_sk(sk); |
798 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; | 798 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; |
799 | struct dentry *dentry = NULL; | 799 | struct dentry *dentry = NULL; |
800 | struct nameidata nd; | 800 | struct nameidata nd; |
801 | int err; | 801 | int err; |
802 | unsigned hash; | 802 | unsigned hash; |
803 | struct unix_address *addr; | 803 | struct unix_address *addr; |
804 | struct hlist_head *list; | 804 | struct hlist_head *list; |
805 | 805 | ||
806 | err = -EINVAL; | 806 | err = -EINVAL; |
807 | if (sunaddr->sun_family != AF_UNIX) | 807 | if (sunaddr->sun_family != AF_UNIX) |
808 | goto out; | 808 | goto out; |
809 | 809 | ||
810 | if (addr_len == sizeof(short)) { | 810 | if (addr_len == sizeof(short)) { |
811 | err = unix_autobind(sock); | 811 | err = unix_autobind(sock); |
812 | goto out; | 812 | goto out; |
813 | } | 813 | } |
814 | 814 | ||
815 | err = unix_mkname(sunaddr, addr_len, &hash); | 815 | err = unix_mkname(sunaddr, addr_len, &hash); |
816 | if (err < 0) | 816 | if (err < 0) |
817 | goto out; | 817 | goto out; |
818 | addr_len = err; | 818 | addr_len = err; |
819 | 819 | ||
820 | mutex_lock(&u->readlock); | 820 | mutex_lock(&u->readlock); |
821 | 821 | ||
822 | err = -EINVAL; | 822 | err = -EINVAL; |
823 | if (u->addr) | 823 | if (u->addr) |
824 | goto out_up; | 824 | goto out_up; |
825 | 825 | ||
826 | err = -ENOMEM; | 826 | err = -ENOMEM; |
827 | addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL); | 827 | addr = kmalloc(sizeof(*addr)+addr_len, GFP_KERNEL); |
828 | if (!addr) | 828 | if (!addr) |
829 | goto out_up; | 829 | goto out_up; |
830 | 830 | ||
831 | memcpy(addr->name, sunaddr, addr_len); | 831 | memcpy(addr->name, sunaddr, addr_len); |
832 | addr->len = addr_len; | 832 | addr->len = addr_len; |
833 | addr->hash = hash ^ sk->sk_type; | 833 | addr->hash = hash ^ sk->sk_type; |
834 | atomic_set(&addr->refcnt, 1); | 834 | atomic_set(&addr->refcnt, 1); |
835 | 835 | ||
836 | if (sunaddr->sun_path[0]) { | 836 | if (sunaddr->sun_path[0]) { |
837 | unsigned int mode; | 837 | unsigned int mode; |
838 | err = 0; | 838 | err = 0; |
839 | /* | 839 | /* |
840 | * Get the parent directory, calculate the hash for last | 840 | * Get the parent directory, calculate the hash for last |
841 | * component. | 841 | * component. |
842 | */ | 842 | */ |
843 | err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd); | 843 | err = path_lookup(sunaddr->sun_path, LOOKUP_PARENT, &nd); |
844 | if (err) | 844 | if (err) |
845 | goto out_mknod_parent; | 845 | goto out_mknod_parent; |
846 | 846 | ||
847 | dentry = lookup_create(&nd, 0); | 847 | dentry = lookup_create(&nd, 0); |
848 | err = PTR_ERR(dentry); | 848 | err = PTR_ERR(dentry); |
849 | if (IS_ERR(dentry)) | 849 | if (IS_ERR(dentry)) |
850 | goto out_mknod_unlock; | 850 | goto out_mknod_unlock; |
851 | 851 | ||
852 | /* | 852 | /* |
853 | * All right, let's create it. | 853 | * All right, let's create it. |
854 | */ | 854 | */ |
855 | mode = S_IFSOCK | | 855 | mode = S_IFSOCK | |
856 | (SOCK_INODE(sock)->i_mode & ~current_umask()); | 856 | (SOCK_INODE(sock)->i_mode & ~current_umask()); |
857 | err = mnt_want_write(nd.path.mnt); | 857 | err = mnt_want_write(nd.path.mnt); |
858 | if (err) | 858 | if (err) |
859 | goto out_mknod_dput; | 859 | goto out_mknod_dput; |
860 | err = security_path_mknod(&nd.path, dentry, mode, 0); | 860 | err = security_path_mknod(&nd.path, dentry, mode, 0); |
861 | if (err) | 861 | if (err) |
862 | goto out_mknod_drop_write; | 862 | goto out_mknod_drop_write; |
863 | err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0); | 863 | err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0); |
864 | out_mknod_drop_write: | 864 | out_mknod_drop_write: |
865 | mnt_drop_write(nd.path.mnt); | 865 | mnt_drop_write(nd.path.mnt); |
866 | if (err) | 866 | if (err) |
867 | goto out_mknod_dput; | 867 | goto out_mknod_dput; |
868 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); | 868 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
869 | dput(nd.path.dentry); | 869 | dput(nd.path.dentry); |
870 | nd.path.dentry = dentry; | 870 | nd.path.dentry = dentry; |
871 | 871 | ||
872 | addr->hash = UNIX_HASH_SIZE; | 872 | addr->hash = UNIX_HASH_SIZE; |
873 | } | 873 | } |
874 | 874 | ||
875 | spin_lock(&unix_table_lock); | 875 | spin_lock(&unix_table_lock); |
876 | 876 | ||
877 | if (!sunaddr->sun_path[0]) { | 877 | if (!sunaddr->sun_path[0]) { |
878 | err = -EADDRINUSE; | 878 | err = -EADDRINUSE; |
879 | if (__unix_find_socket_byname(net, sunaddr, addr_len, | 879 | if (__unix_find_socket_byname(net, sunaddr, addr_len, |
880 | sk->sk_type, hash)) { | 880 | sk->sk_type, hash)) { |
881 | unix_release_addr(addr); | 881 | unix_release_addr(addr); |
882 | goto out_unlock; | 882 | goto out_unlock; |
883 | } | 883 | } |
884 | 884 | ||
885 | list = &unix_socket_table[addr->hash]; | 885 | list = &unix_socket_table[addr->hash]; |
886 | } else { | 886 | } else { |
887 | list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)]; | 887 | list = &unix_socket_table[dentry->d_inode->i_ino & (UNIX_HASH_SIZE-1)]; |
888 | u->dentry = nd.path.dentry; | 888 | u->dentry = nd.path.dentry; |
889 | u->mnt = nd.path.mnt; | 889 | u->mnt = nd.path.mnt; |
890 | } | 890 | } |
891 | 891 | ||
892 | err = 0; | 892 | err = 0; |
893 | __unix_remove_socket(sk); | 893 | __unix_remove_socket(sk); |
894 | u->addr = addr; | 894 | u->addr = addr; |
895 | __unix_insert_socket(list, sk); | 895 | __unix_insert_socket(list, sk); |
896 | 896 | ||
897 | out_unlock: | 897 | out_unlock: |
898 | spin_unlock(&unix_table_lock); | 898 | spin_unlock(&unix_table_lock); |
899 | out_up: | 899 | out_up: |
900 | mutex_unlock(&u->readlock); | 900 | mutex_unlock(&u->readlock); |
901 | out: | 901 | out: |
902 | return err; | 902 | return err; |
903 | 903 | ||
904 | out_mknod_dput: | 904 | out_mknod_dput: |
905 | dput(dentry); | 905 | dput(dentry); |
906 | out_mknod_unlock: | 906 | out_mknod_unlock: |
907 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); | 907 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
908 | path_put(&nd.path); | 908 | path_put(&nd.path); |
909 | out_mknod_parent: | 909 | out_mknod_parent: |
910 | if (err == -EEXIST) | 910 | if (err == -EEXIST) |
911 | err = -EADDRINUSE; | 911 | err = -EADDRINUSE; |
912 | unix_release_addr(addr); | 912 | unix_release_addr(addr); |
913 | goto out_up; | 913 | goto out_up; |
914 | } | 914 | } |
915 | 915 | ||
916 | static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) | 916 | static void unix_state_double_lock(struct sock *sk1, struct sock *sk2) |
917 | { | 917 | { |
918 | if (unlikely(sk1 == sk2) || !sk2) { | 918 | if (unlikely(sk1 == sk2) || !sk2) { |
919 | unix_state_lock(sk1); | 919 | unix_state_lock(sk1); |
920 | return; | 920 | return; |
921 | } | 921 | } |
922 | if (sk1 < sk2) { | 922 | if (sk1 < sk2) { |
923 | unix_state_lock(sk1); | 923 | unix_state_lock(sk1); |
924 | unix_state_lock_nested(sk2); | 924 | unix_state_lock_nested(sk2); |
925 | } else { | 925 | } else { |
926 | unix_state_lock(sk2); | 926 | unix_state_lock(sk2); |
927 | unix_state_lock_nested(sk1); | 927 | unix_state_lock_nested(sk1); |
928 | } | 928 | } |
929 | } | 929 | } |
930 | 930 | ||
931 | static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) | 931 | static void unix_state_double_unlock(struct sock *sk1, struct sock *sk2) |
932 | { | 932 | { |
933 | if (unlikely(sk1 == sk2) || !sk2) { | 933 | if (unlikely(sk1 == sk2) || !sk2) { |
934 | unix_state_unlock(sk1); | 934 | unix_state_unlock(sk1); |
935 | return; | 935 | return; |
936 | } | 936 | } |
937 | unix_state_unlock(sk1); | 937 | unix_state_unlock(sk1); |
938 | unix_state_unlock(sk2); | 938 | unix_state_unlock(sk2); |
939 | } | 939 | } |
940 | 940 | ||
941 | static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, | 941 | static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, |
942 | int alen, int flags) | 942 | int alen, int flags) |
943 | { | 943 | { |
944 | struct sock *sk = sock->sk; | 944 | struct sock *sk = sock->sk; |
945 | struct net *net = sock_net(sk); | 945 | struct net *net = sock_net(sk); |
946 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; | 946 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)addr; |
947 | struct sock *other; | 947 | struct sock *other; |
948 | unsigned hash; | 948 | unsigned hash; |
949 | int err; | 949 | int err; |
950 | 950 | ||
951 | if (addr->sa_family != AF_UNSPEC) { | 951 | if (addr->sa_family != AF_UNSPEC) { |
952 | err = unix_mkname(sunaddr, alen, &hash); | 952 | err = unix_mkname(sunaddr, alen, &hash); |
953 | if (err < 0) | 953 | if (err < 0) |
954 | goto out; | 954 | goto out; |
955 | alen = err; | 955 | alen = err; |
956 | 956 | ||
957 | if (test_bit(SOCK_PASSCRED, &sock->flags) && | 957 | if (test_bit(SOCK_PASSCRED, &sock->flags) && |
958 | !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0) | 958 | !unix_sk(sk)->addr && (err = unix_autobind(sock)) != 0) |
959 | goto out; | 959 | goto out; |
960 | 960 | ||
961 | restart: | 961 | restart: |
962 | other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err); | 962 | other = unix_find_other(net, sunaddr, alen, sock->type, hash, &err); |
963 | if (!other) | 963 | if (!other) |
964 | goto out; | 964 | goto out; |
965 | 965 | ||
966 | unix_state_double_lock(sk, other); | 966 | unix_state_double_lock(sk, other); |
967 | 967 | ||
968 | /* Apparently VFS overslept socket death. Retry. */ | 968 | /* Apparently VFS overslept socket death. Retry. */ |
969 | if (sock_flag(other, SOCK_DEAD)) { | 969 | if (sock_flag(other, SOCK_DEAD)) { |
970 | unix_state_double_unlock(sk, other); | 970 | unix_state_double_unlock(sk, other); |
971 | sock_put(other); | 971 | sock_put(other); |
972 | goto restart; | 972 | goto restart; |
973 | } | 973 | } |
974 | 974 | ||
975 | err = -EPERM; | 975 | err = -EPERM; |
976 | if (!unix_may_send(sk, other)) | 976 | if (!unix_may_send(sk, other)) |
977 | goto out_unlock; | 977 | goto out_unlock; |
978 | 978 | ||
979 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); | 979 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); |
980 | if (err) | 980 | if (err) |
981 | goto out_unlock; | 981 | goto out_unlock; |
982 | 982 | ||
983 | } else { | 983 | } else { |
984 | /* | 984 | /* |
985 | * 1003.1g breaking connected state with AF_UNSPEC | 985 | * 1003.1g breaking connected state with AF_UNSPEC |
986 | */ | 986 | */ |
987 | other = NULL; | 987 | other = NULL; |
988 | unix_state_double_lock(sk, other); | 988 | unix_state_double_lock(sk, other); |
989 | } | 989 | } |
990 | 990 | ||
991 | /* | 991 | /* |
992 | * If it was connected, reconnect. | 992 | * If it was connected, reconnect. |
993 | */ | 993 | */ |
994 | if (unix_peer(sk)) { | 994 | if (unix_peer(sk)) { |
995 | struct sock *old_peer = unix_peer(sk); | 995 | struct sock *old_peer = unix_peer(sk); |
996 | unix_peer(sk) = other; | 996 | unix_peer(sk) = other; |
997 | unix_state_double_unlock(sk, other); | 997 | unix_state_double_unlock(sk, other); |
998 | 998 | ||
999 | if (other != old_peer) | 999 | if (other != old_peer) |
1000 | unix_dgram_disconnected(sk, old_peer); | 1000 | unix_dgram_disconnected(sk, old_peer); |
1001 | sock_put(old_peer); | 1001 | sock_put(old_peer); |
1002 | } else { | 1002 | } else { |
1003 | unix_peer(sk) = other; | 1003 | unix_peer(sk) = other; |
1004 | unix_state_double_unlock(sk, other); | 1004 | unix_state_double_unlock(sk, other); |
1005 | } | 1005 | } |
1006 | return 0; | 1006 | return 0; |
1007 | 1007 | ||
1008 | out_unlock: | 1008 | out_unlock: |
1009 | unix_state_double_unlock(sk, other); | 1009 | unix_state_double_unlock(sk, other); |
1010 | sock_put(other); | 1010 | sock_put(other); |
1011 | out: | 1011 | out: |
1012 | return err; | 1012 | return err; |
1013 | } | 1013 | } |
1014 | 1014 | ||
1015 | static long unix_wait_for_peer(struct sock *other, long timeo) | 1015 | static long unix_wait_for_peer(struct sock *other, long timeo) |
1016 | { | 1016 | { |
1017 | struct unix_sock *u = unix_sk(other); | 1017 | struct unix_sock *u = unix_sk(other); |
1018 | int sched; | 1018 | int sched; |
1019 | DEFINE_WAIT(wait); | 1019 | DEFINE_WAIT(wait); |
1020 | 1020 | ||
1021 | prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE); | 1021 | prepare_to_wait_exclusive(&u->peer_wait, &wait, TASK_INTERRUPTIBLE); |
1022 | 1022 | ||
1023 | sched = !sock_flag(other, SOCK_DEAD) && | 1023 | sched = !sock_flag(other, SOCK_DEAD) && |
1024 | !(other->sk_shutdown & RCV_SHUTDOWN) && | 1024 | !(other->sk_shutdown & RCV_SHUTDOWN) && |
1025 | unix_recvq_full(other); | 1025 | unix_recvq_full(other); |
1026 | 1026 | ||
1027 | unix_state_unlock(other); | 1027 | unix_state_unlock(other); |
1028 | 1028 | ||
1029 | if (sched) | 1029 | if (sched) |
1030 | timeo = schedule_timeout(timeo); | 1030 | timeo = schedule_timeout(timeo); |
1031 | 1031 | ||
1032 | finish_wait(&u->peer_wait, &wait); | 1032 | finish_wait(&u->peer_wait, &wait); |
1033 | return timeo; | 1033 | return timeo; |
1034 | } | 1034 | } |
1035 | 1035 | ||
1036 | static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, | 1036 | static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, |
1037 | int addr_len, int flags) | 1037 | int addr_len, int flags) |
1038 | { | 1038 | { |
1039 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; | 1039 | struct sockaddr_un *sunaddr = (struct sockaddr_un *)uaddr; |
1040 | struct sock *sk = sock->sk; | 1040 | struct sock *sk = sock->sk; |
1041 | struct net *net = sock_net(sk); | 1041 | struct net *net = sock_net(sk); |
1042 | struct unix_sock *u = unix_sk(sk), *newu, *otheru; | 1042 | struct unix_sock *u = unix_sk(sk), *newu, *otheru; |
1043 | struct sock *newsk = NULL; | 1043 | struct sock *newsk = NULL; |
1044 | struct sock *other = NULL; | 1044 | struct sock *other = NULL; |
1045 | struct sk_buff *skb = NULL; | 1045 | struct sk_buff *skb = NULL; |
1046 | unsigned hash; | 1046 | unsigned hash; |
1047 | int st; | 1047 | int st; |
1048 | int err; | 1048 | int err; |
1049 | long timeo; | 1049 | long timeo; |
1050 | 1050 | ||
1051 | err = unix_mkname(sunaddr, addr_len, &hash); | 1051 | err = unix_mkname(sunaddr, addr_len, &hash); |
1052 | if (err < 0) | 1052 | if (err < 0) |
1053 | goto out; | 1053 | goto out; |
1054 | addr_len = err; | 1054 | addr_len = err; |
1055 | 1055 | ||
1056 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr && | 1056 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr && |
1057 | (err = unix_autobind(sock)) != 0) | 1057 | (err = unix_autobind(sock)) != 0) |
1058 | goto out; | 1058 | goto out; |
1059 | 1059 | ||
1060 | timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); | 1060 | timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); |
1061 | 1061 | ||
1062 | /* First of all allocate resources. | 1062 | /* First of all allocate resources. |
1063 | If we will make it after state is locked, | 1063 | If we will make it after state is locked, |
1064 | we will have to recheck all again in any case. | 1064 | we will have to recheck all again in any case. |
1065 | */ | 1065 | */ |
1066 | 1066 | ||
1067 | err = -ENOMEM; | 1067 | err = -ENOMEM; |
1068 | 1068 | ||
1069 | /* create new sock for complete connection */ | 1069 | /* create new sock for complete connection */ |
1070 | newsk = unix_create1(sock_net(sk), NULL); | 1070 | newsk = unix_create1(sock_net(sk), NULL); |
1071 | if (newsk == NULL) | 1071 | if (newsk == NULL) |
1072 | goto out; | 1072 | goto out; |
1073 | 1073 | ||
1074 | /* Allocate skb for sending to listening sock */ | 1074 | /* Allocate skb for sending to listening sock */ |
1075 | skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL); | 1075 | skb = sock_wmalloc(newsk, 1, 0, GFP_KERNEL); |
1076 | if (skb == NULL) | 1076 | if (skb == NULL) |
1077 | goto out; | 1077 | goto out; |
1078 | 1078 | ||
1079 | restart: | 1079 | restart: |
1080 | /* Find listening sock. */ | 1080 | /* Find listening sock. */ |
1081 | other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err); | 1081 | other = unix_find_other(net, sunaddr, addr_len, sk->sk_type, hash, &err); |
1082 | if (!other) | 1082 | if (!other) |
1083 | goto out; | 1083 | goto out; |
1084 | 1084 | ||
1085 | /* Latch state of peer */ | 1085 | /* Latch state of peer */ |
1086 | unix_state_lock(other); | 1086 | unix_state_lock(other); |
1087 | 1087 | ||
1088 | /* Apparently VFS overslept socket death. Retry. */ | 1088 | /* Apparently VFS overslept socket death. Retry. */ |
1089 | if (sock_flag(other, SOCK_DEAD)) { | 1089 | if (sock_flag(other, SOCK_DEAD)) { |
1090 | unix_state_unlock(other); | 1090 | unix_state_unlock(other); |
1091 | sock_put(other); | 1091 | sock_put(other); |
1092 | goto restart; | 1092 | goto restart; |
1093 | } | 1093 | } |
1094 | 1094 | ||
1095 | err = -ECONNREFUSED; | 1095 | err = -ECONNREFUSED; |
1096 | if (other->sk_state != TCP_LISTEN) | 1096 | if (other->sk_state != TCP_LISTEN) |
1097 | goto out_unlock; | 1097 | goto out_unlock; |
1098 | if (other->sk_shutdown & RCV_SHUTDOWN) | 1098 | if (other->sk_shutdown & RCV_SHUTDOWN) |
1099 | goto out_unlock; | 1099 | goto out_unlock; |
1100 | 1100 | ||
1101 | if (unix_recvq_full(other)) { | 1101 | if (unix_recvq_full(other)) { |
1102 | err = -EAGAIN; | 1102 | err = -EAGAIN; |
1103 | if (!timeo) | 1103 | if (!timeo) |
1104 | goto out_unlock; | 1104 | goto out_unlock; |
1105 | 1105 | ||
1106 | timeo = unix_wait_for_peer(other, timeo); | 1106 | timeo = unix_wait_for_peer(other, timeo); |
1107 | 1107 | ||
1108 | err = sock_intr_errno(timeo); | 1108 | err = sock_intr_errno(timeo); |
1109 | if (signal_pending(current)) | 1109 | if (signal_pending(current)) |
1110 | goto out; | 1110 | goto out; |
1111 | sock_put(other); | 1111 | sock_put(other); |
1112 | goto restart; | 1112 | goto restart; |
1113 | } | 1113 | } |
1114 | 1114 | ||
1115 | /* Latch our state. | 1115 | /* Latch our state. |
1116 | 1116 | ||
1117 | It is tricky place. We need to grab write lock and cannot | 1117 | It is tricky place. We need to grab write lock and cannot |
1118 | drop lock on peer. It is dangerous because deadlock is | 1118 | drop lock on peer. It is dangerous because deadlock is |
1119 | possible. Connect to self case and simultaneous | 1119 | possible. Connect to self case and simultaneous |
1120 | attempt to connect are eliminated by checking socket | 1120 | attempt to connect are eliminated by checking socket |
1121 | state. other is TCP_LISTEN, if sk is TCP_LISTEN we | 1121 | state. other is TCP_LISTEN, if sk is TCP_LISTEN we |
1122 | check this before attempt to grab lock. | 1122 | check this before attempt to grab lock. |
1123 | 1123 | ||
1124 | Well, and we have to recheck the state after socket locked. | 1124 | Well, and we have to recheck the state after socket locked. |
1125 | */ | 1125 | */ |
1126 | st = sk->sk_state; | 1126 | st = sk->sk_state; |
1127 | 1127 | ||
1128 | switch (st) { | 1128 | switch (st) { |
1129 | case TCP_CLOSE: | 1129 | case TCP_CLOSE: |
1130 | /* This is ok... continue with connect */ | 1130 | /* This is ok... continue with connect */ |
1131 | break; | 1131 | break; |
1132 | case TCP_ESTABLISHED: | 1132 | case TCP_ESTABLISHED: |
1133 | /* Socket is already connected */ | 1133 | /* Socket is already connected */ |
1134 | err = -EISCONN; | 1134 | err = -EISCONN; |
1135 | goto out_unlock; | 1135 | goto out_unlock; |
1136 | default: | 1136 | default: |
1137 | err = -EINVAL; | 1137 | err = -EINVAL; |
1138 | goto out_unlock; | 1138 | goto out_unlock; |
1139 | } | 1139 | } |
1140 | 1140 | ||
1141 | unix_state_lock_nested(sk); | 1141 | unix_state_lock_nested(sk); |
1142 | 1142 | ||
1143 | if (sk->sk_state != st) { | 1143 | if (sk->sk_state != st) { |
1144 | unix_state_unlock(sk); | 1144 | unix_state_unlock(sk); |
1145 | unix_state_unlock(other); | 1145 | unix_state_unlock(other); |
1146 | sock_put(other); | 1146 | sock_put(other); |
1147 | goto restart; | 1147 | goto restart; |
1148 | } | 1148 | } |
1149 | 1149 | ||
1150 | err = security_unix_stream_connect(sock, other->sk_socket, newsk); | 1150 | err = security_unix_stream_connect(sock, other->sk_socket, newsk); |
1151 | if (err) { | 1151 | if (err) { |
1152 | unix_state_unlock(sk); | 1152 | unix_state_unlock(sk); |
1153 | goto out_unlock; | 1153 | goto out_unlock; |
1154 | } | 1154 | } |
1155 | 1155 | ||
1156 | /* The way is open! Fastly set all the necessary fields... */ | 1156 | /* The way is open! Fastly set all the necessary fields... */ |
1157 | 1157 | ||
1158 | sock_hold(sk); | 1158 | sock_hold(sk); |
1159 | unix_peer(newsk) = sk; | 1159 | unix_peer(newsk) = sk; |
1160 | newsk->sk_state = TCP_ESTABLISHED; | 1160 | newsk->sk_state = TCP_ESTABLISHED; |
1161 | newsk->sk_type = sk->sk_type; | 1161 | newsk->sk_type = sk->sk_type; |
1162 | init_peercred(newsk); | 1162 | init_peercred(newsk); |
1163 | newu = unix_sk(newsk); | 1163 | newu = unix_sk(newsk); |
1164 | newsk->sk_wq = &newu->peer_wq; | 1164 | newsk->sk_wq = &newu->peer_wq; |
1165 | otheru = unix_sk(other); | 1165 | otheru = unix_sk(other); |
1166 | 1166 | ||
1167 | /* copy address information from listening to new sock*/ | 1167 | /* copy address information from listening to new sock*/ |
1168 | if (otheru->addr) { | 1168 | if (otheru->addr) { |
1169 | atomic_inc(&otheru->addr->refcnt); | 1169 | atomic_inc(&otheru->addr->refcnt); |
1170 | newu->addr = otheru->addr; | 1170 | newu->addr = otheru->addr; |
1171 | } | 1171 | } |
1172 | if (otheru->dentry) { | 1172 | if (otheru->dentry) { |
1173 | newu->dentry = dget(otheru->dentry); | 1173 | newu->dentry = dget(otheru->dentry); |
1174 | newu->mnt = mntget(otheru->mnt); | 1174 | newu->mnt = mntget(otheru->mnt); |
1175 | } | 1175 | } |
1176 | 1176 | ||
1177 | /* Set credentials */ | 1177 | /* Set credentials */ |
1178 | copy_peercred(sk, other); | 1178 | copy_peercred(sk, other); |
1179 | 1179 | ||
1180 | sock->state = SS_CONNECTED; | 1180 | sock->state = SS_CONNECTED; |
1181 | sk->sk_state = TCP_ESTABLISHED; | 1181 | sk->sk_state = TCP_ESTABLISHED; |
1182 | sock_hold(newsk); | 1182 | sock_hold(newsk); |
1183 | 1183 | ||
1184 | smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */ | 1184 | smp_mb__after_atomic_inc(); /* sock_hold() does an atomic_inc() */ |
1185 | unix_peer(sk) = newsk; | 1185 | unix_peer(sk) = newsk; |
1186 | 1186 | ||
1187 | unix_state_unlock(sk); | 1187 | unix_state_unlock(sk); |
1188 | 1188 | ||
1189 | /* take ten and and send info to listening sock */ | 1189 | /* take ten and and send info to listening sock */ |
1190 | spin_lock(&other->sk_receive_queue.lock); | 1190 | spin_lock(&other->sk_receive_queue.lock); |
1191 | __skb_queue_tail(&other->sk_receive_queue, skb); | 1191 | __skb_queue_tail(&other->sk_receive_queue, skb); |
1192 | spin_unlock(&other->sk_receive_queue.lock); | 1192 | spin_unlock(&other->sk_receive_queue.lock); |
1193 | unix_state_unlock(other); | 1193 | unix_state_unlock(other); |
1194 | other->sk_data_ready(other, 0); | 1194 | other->sk_data_ready(other, 0); |
1195 | sock_put(other); | 1195 | sock_put(other); |
1196 | return 0; | 1196 | return 0; |
1197 | 1197 | ||
1198 | out_unlock: | 1198 | out_unlock: |
1199 | if (other) | 1199 | if (other) |
1200 | unix_state_unlock(other); | 1200 | unix_state_unlock(other); |
1201 | 1201 | ||
1202 | out: | 1202 | out: |
1203 | kfree_skb(skb); | 1203 | kfree_skb(skb); |
1204 | if (newsk) | 1204 | if (newsk) |
1205 | unix_release_sock(newsk, 0); | 1205 | unix_release_sock(newsk, 0); |
1206 | if (other) | 1206 | if (other) |
1207 | sock_put(other); | 1207 | sock_put(other); |
1208 | return err; | 1208 | return err; |
1209 | } | 1209 | } |
1210 | 1210 | ||
1211 | static int unix_socketpair(struct socket *socka, struct socket *sockb) | 1211 | static int unix_socketpair(struct socket *socka, struct socket *sockb) |
1212 | { | 1212 | { |
1213 | struct sock *ska = socka->sk, *skb = sockb->sk; | 1213 | struct sock *ska = socka->sk, *skb = sockb->sk; |
1214 | 1214 | ||
1215 | /* Join our sockets back to back */ | 1215 | /* Join our sockets back to back */ |
1216 | sock_hold(ska); | 1216 | sock_hold(ska); |
1217 | sock_hold(skb); | 1217 | sock_hold(skb); |
1218 | unix_peer(ska) = skb; | 1218 | unix_peer(ska) = skb; |
1219 | unix_peer(skb) = ska; | 1219 | unix_peer(skb) = ska; |
1220 | init_peercred(ska); | 1220 | init_peercred(ska); |
1221 | init_peercred(skb); | 1221 | init_peercred(skb); |
1222 | 1222 | ||
1223 | if (ska->sk_type != SOCK_DGRAM) { | 1223 | if (ska->sk_type != SOCK_DGRAM) { |
1224 | ska->sk_state = TCP_ESTABLISHED; | 1224 | ska->sk_state = TCP_ESTABLISHED; |
1225 | skb->sk_state = TCP_ESTABLISHED; | 1225 | skb->sk_state = TCP_ESTABLISHED; |
1226 | socka->state = SS_CONNECTED; | 1226 | socka->state = SS_CONNECTED; |
1227 | sockb->state = SS_CONNECTED; | 1227 | sockb->state = SS_CONNECTED; |
1228 | } | 1228 | } |
1229 | return 0; | 1229 | return 0; |
1230 | } | 1230 | } |
1231 | 1231 | ||
1232 | static int unix_accept(struct socket *sock, struct socket *newsock, int flags) | 1232 | static int unix_accept(struct socket *sock, struct socket *newsock, int flags) |
1233 | { | 1233 | { |
1234 | struct sock *sk = sock->sk; | 1234 | struct sock *sk = sock->sk; |
1235 | struct sock *tsk; | 1235 | struct sock *tsk; |
1236 | struct sk_buff *skb; | 1236 | struct sk_buff *skb; |
1237 | int err; | 1237 | int err; |
1238 | 1238 | ||
1239 | err = -EOPNOTSUPP; | 1239 | err = -EOPNOTSUPP; |
1240 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) | 1240 | if (sock->type != SOCK_STREAM && sock->type != SOCK_SEQPACKET) |
1241 | goto out; | 1241 | goto out; |
1242 | 1242 | ||
1243 | err = -EINVAL; | 1243 | err = -EINVAL; |
1244 | if (sk->sk_state != TCP_LISTEN) | 1244 | if (sk->sk_state != TCP_LISTEN) |
1245 | goto out; | 1245 | goto out; |
1246 | 1246 | ||
1247 | /* If socket state is TCP_LISTEN it cannot change (for now...), | 1247 | /* If socket state is TCP_LISTEN it cannot change (for now...), |
1248 | * so that no locks are necessary. | 1248 | * so that no locks are necessary. |
1249 | */ | 1249 | */ |
1250 | 1250 | ||
1251 | skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err); | 1251 | skb = skb_recv_datagram(sk, 0, flags&O_NONBLOCK, &err); |
1252 | if (!skb) { | 1252 | if (!skb) { |
1253 | /* This means receive shutdown. */ | 1253 | /* This means receive shutdown. */ |
1254 | if (err == 0) | 1254 | if (err == 0) |
1255 | err = -EINVAL; | 1255 | err = -EINVAL; |
1256 | goto out; | 1256 | goto out; |
1257 | } | 1257 | } |
1258 | 1258 | ||
1259 | tsk = skb->sk; | 1259 | tsk = skb->sk; |
1260 | skb_free_datagram(sk, skb); | 1260 | skb_free_datagram(sk, skb); |
1261 | wake_up_interruptible(&unix_sk(sk)->peer_wait); | 1261 | wake_up_interruptible(&unix_sk(sk)->peer_wait); |
1262 | 1262 | ||
1263 | /* attach accepted sock to socket */ | 1263 | /* attach accepted sock to socket */ |
1264 | unix_state_lock(tsk); | 1264 | unix_state_lock(tsk); |
1265 | newsock->state = SS_CONNECTED; | 1265 | newsock->state = SS_CONNECTED; |
1266 | sock_graft(tsk, newsock); | 1266 | sock_graft(tsk, newsock); |
1267 | unix_state_unlock(tsk); | 1267 | unix_state_unlock(tsk); |
1268 | return 0; | 1268 | return 0; |
1269 | 1269 | ||
1270 | out: | 1270 | out: |
1271 | return err; | 1271 | return err; |
1272 | } | 1272 | } |
1273 | 1273 | ||
1274 | 1274 | ||
1275 | static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) | 1275 | static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) |
1276 | { | 1276 | { |
1277 | struct sock *sk = sock->sk; | 1277 | struct sock *sk = sock->sk; |
1278 | struct unix_sock *u; | 1278 | struct unix_sock *u; |
1279 | DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr); | 1279 | DECLARE_SOCKADDR(struct sockaddr_un *, sunaddr, uaddr); |
1280 | int err = 0; | 1280 | int err = 0; |
1281 | 1281 | ||
1282 | if (peer) { | 1282 | if (peer) { |
1283 | sk = unix_peer_get(sk); | 1283 | sk = unix_peer_get(sk); |
1284 | 1284 | ||
1285 | err = -ENOTCONN; | 1285 | err = -ENOTCONN; |
1286 | if (!sk) | 1286 | if (!sk) |
1287 | goto out; | 1287 | goto out; |
1288 | err = 0; | 1288 | err = 0; |
1289 | } else { | 1289 | } else { |
1290 | sock_hold(sk); | 1290 | sock_hold(sk); |
1291 | } | 1291 | } |
1292 | 1292 | ||
1293 | u = unix_sk(sk); | 1293 | u = unix_sk(sk); |
1294 | unix_state_lock(sk); | 1294 | unix_state_lock(sk); |
1295 | if (!u->addr) { | 1295 | if (!u->addr) { |
1296 | sunaddr->sun_family = AF_UNIX; | 1296 | sunaddr->sun_family = AF_UNIX; |
1297 | sunaddr->sun_path[0] = 0; | 1297 | sunaddr->sun_path[0] = 0; |
1298 | *uaddr_len = sizeof(short); | 1298 | *uaddr_len = sizeof(short); |
1299 | } else { | 1299 | } else { |
1300 | struct unix_address *addr = u->addr; | 1300 | struct unix_address *addr = u->addr; |
1301 | 1301 | ||
1302 | *uaddr_len = addr->len; | 1302 | *uaddr_len = addr->len; |
1303 | memcpy(sunaddr, addr->name, *uaddr_len); | 1303 | memcpy(sunaddr, addr->name, *uaddr_len); |
1304 | } | 1304 | } |
1305 | unix_state_unlock(sk); | 1305 | unix_state_unlock(sk); |
1306 | sock_put(sk); | 1306 | sock_put(sk); |
1307 | out: | 1307 | out: |
1308 | return err; | 1308 | return err; |
1309 | } | 1309 | } |
1310 | 1310 | ||
1311 | static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb) | 1311 | static void unix_detach_fds(struct scm_cookie *scm, struct sk_buff *skb) |
1312 | { | 1312 | { |
1313 | int i; | 1313 | int i; |
1314 | 1314 | ||
1315 | scm->fp = UNIXCB(skb).fp; | 1315 | scm->fp = UNIXCB(skb).fp; |
1316 | UNIXCB(skb).fp = NULL; | 1316 | UNIXCB(skb).fp = NULL; |
1317 | 1317 | ||
1318 | for (i = scm->fp->count-1; i >= 0; i--) | 1318 | for (i = scm->fp->count-1; i >= 0; i--) |
1319 | unix_notinflight(scm->fp->fp[i]); | 1319 | unix_notinflight(scm->fp->fp[i]); |
1320 | } | 1320 | } |
1321 | 1321 | ||
1322 | static void unix_destruct_scm(struct sk_buff *skb) | 1322 | static void unix_destruct_scm(struct sk_buff *skb) |
1323 | { | 1323 | { |
1324 | struct scm_cookie scm; | 1324 | struct scm_cookie scm; |
1325 | memset(&scm, 0, sizeof(scm)); | 1325 | memset(&scm, 0, sizeof(scm)); |
1326 | scm.pid = UNIXCB(skb).pid; | 1326 | scm.pid = UNIXCB(skb).pid; |
1327 | scm.cred = UNIXCB(skb).cred; | 1327 | scm.cred = UNIXCB(skb).cred; |
1328 | if (UNIXCB(skb).fp) | 1328 | if (UNIXCB(skb).fp) |
1329 | unix_detach_fds(&scm, skb); | 1329 | unix_detach_fds(&scm, skb); |
1330 | 1330 | ||
1331 | /* Alas, it calls VFS */ | 1331 | /* Alas, it calls VFS */ |
1332 | /* So fscking what? fput() had been SMP-safe since the last Summer */ | 1332 | /* So fscking what? fput() had been SMP-safe since the last Summer */ |
1333 | scm_destroy(&scm); | 1333 | scm_destroy(&scm); |
1334 | sock_wfree(skb); | 1334 | sock_wfree(skb); |
1335 | } | 1335 | } |
1336 | 1336 | ||
1337 | static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) | 1337 | static int unix_attach_fds(struct scm_cookie *scm, struct sk_buff *skb) |
1338 | { | 1338 | { |
1339 | int i; | 1339 | int i; |
1340 | 1340 | ||
1341 | /* | 1341 | /* |
1342 | * Need to duplicate file references for the sake of garbage | 1342 | * Need to duplicate file references for the sake of garbage |
1343 | * collection. Otherwise a socket in the fps might become a | 1343 | * collection. Otherwise a socket in the fps might become a |
1344 | * candidate for GC while the skb is not yet queued. | 1344 | * candidate for GC while the skb is not yet queued. |
1345 | */ | 1345 | */ |
1346 | UNIXCB(skb).fp = scm_fp_dup(scm->fp); | 1346 | UNIXCB(skb).fp = scm_fp_dup(scm->fp); |
1347 | if (!UNIXCB(skb).fp) | 1347 | if (!UNIXCB(skb).fp) |
1348 | return -ENOMEM; | 1348 | return -ENOMEM; |
1349 | 1349 | ||
1350 | for (i = scm->fp->count-1; i >= 0; i--) | 1350 | for (i = scm->fp->count-1; i >= 0; i--) |
1351 | unix_inflight(scm->fp->fp[i]); | 1351 | unix_inflight(scm->fp->fp[i]); |
1352 | return 0; | 1352 | return 0; |
1353 | } | 1353 | } |
1354 | 1354 | ||
1355 | static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds) | 1355 | static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool send_fds) |
1356 | { | 1356 | { |
1357 | int err = 0; | 1357 | int err = 0; |
1358 | UNIXCB(skb).pid = get_pid(scm->pid); | 1358 | UNIXCB(skb).pid = get_pid(scm->pid); |
1359 | UNIXCB(skb).cred = get_cred(scm->cred); | 1359 | UNIXCB(skb).cred = get_cred(scm->cred); |
1360 | UNIXCB(skb).fp = NULL; | 1360 | UNIXCB(skb).fp = NULL; |
1361 | if (scm->fp && send_fds) | 1361 | if (scm->fp && send_fds) |
1362 | err = unix_attach_fds(scm, skb); | 1362 | err = unix_attach_fds(scm, skb); |
1363 | 1363 | ||
1364 | skb->destructor = unix_destruct_scm; | 1364 | skb->destructor = unix_destruct_scm; |
1365 | return err; | 1365 | return err; |
1366 | } | 1366 | } |
1367 | 1367 | ||
1368 | /* | 1368 | /* |
1369 | * Send AF_UNIX data. | 1369 | * Send AF_UNIX data. |
1370 | */ | 1370 | */ |
1371 | 1371 | ||
1372 | static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, | 1372 | static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, |
1373 | struct msghdr *msg, size_t len) | 1373 | struct msghdr *msg, size_t len) |
1374 | { | 1374 | { |
1375 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | 1375 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
1376 | struct sock *sk = sock->sk; | 1376 | struct sock *sk = sock->sk; |
1377 | struct net *net = sock_net(sk); | 1377 | struct net *net = sock_net(sk); |
1378 | struct unix_sock *u = unix_sk(sk); | 1378 | struct unix_sock *u = unix_sk(sk); |
1379 | struct sockaddr_un *sunaddr = msg->msg_name; | 1379 | struct sockaddr_un *sunaddr = msg->msg_name; |
1380 | struct sock *other = NULL; | 1380 | struct sock *other = NULL; |
1381 | int namelen = 0; /* fake GCC */ | 1381 | int namelen = 0; /* fake GCC */ |
1382 | int err; | 1382 | int err; |
1383 | unsigned hash; | 1383 | unsigned hash; |
1384 | struct sk_buff *skb; | 1384 | struct sk_buff *skb; |
1385 | long timeo; | 1385 | long timeo; |
1386 | struct scm_cookie tmp_scm; | 1386 | struct scm_cookie tmp_scm; |
1387 | 1387 | ||
1388 | if (NULL == siocb->scm) | 1388 | if (NULL == siocb->scm) |
1389 | siocb->scm = &tmp_scm; | 1389 | siocb->scm = &tmp_scm; |
1390 | wait_for_unix_gc(); | 1390 | wait_for_unix_gc(); |
1391 | err = scm_send(sock, msg, siocb->scm); | 1391 | err = scm_send(sock, msg, siocb->scm); |
1392 | if (err < 0) | 1392 | if (err < 0) |
1393 | return err; | 1393 | return err; |
1394 | 1394 | ||
1395 | err = -EOPNOTSUPP; | 1395 | err = -EOPNOTSUPP; |
1396 | if (msg->msg_flags&MSG_OOB) | 1396 | if (msg->msg_flags&MSG_OOB) |
1397 | goto out; | 1397 | goto out; |
1398 | 1398 | ||
1399 | if (msg->msg_namelen) { | 1399 | if (msg->msg_namelen) { |
1400 | err = unix_mkname(sunaddr, msg->msg_namelen, &hash); | 1400 | err = unix_mkname(sunaddr, msg->msg_namelen, &hash); |
1401 | if (err < 0) | 1401 | if (err < 0) |
1402 | goto out; | 1402 | goto out; |
1403 | namelen = err; | 1403 | namelen = err; |
1404 | } else { | 1404 | } else { |
1405 | sunaddr = NULL; | 1405 | sunaddr = NULL; |
1406 | err = -ENOTCONN; | 1406 | err = -ENOTCONN; |
1407 | other = unix_peer_get(sk); | 1407 | other = unix_peer_get(sk); |
1408 | if (!other) | 1408 | if (!other) |
1409 | goto out; | 1409 | goto out; |
1410 | } | 1410 | } |
1411 | 1411 | ||
1412 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr | 1412 | if (test_bit(SOCK_PASSCRED, &sock->flags) && !u->addr |
1413 | && (err = unix_autobind(sock)) != 0) | 1413 | && (err = unix_autobind(sock)) != 0) |
1414 | goto out; | 1414 | goto out; |
1415 | 1415 | ||
1416 | err = -EMSGSIZE; | 1416 | err = -EMSGSIZE; |
1417 | if (len > sk->sk_sndbuf - 32) | 1417 | if (len > sk->sk_sndbuf - 32) |
1418 | goto out; | 1418 | goto out; |
1419 | 1419 | ||
1420 | skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err); | 1420 | skb = sock_alloc_send_skb(sk, len, msg->msg_flags&MSG_DONTWAIT, &err); |
1421 | if (skb == NULL) | 1421 | if (skb == NULL) |
1422 | goto out; | 1422 | goto out; |
1423 | 1423 | ||
1424 | err = unix_scm_to_skb(siocb->scm, skb, true); | 1424 | err = unix_scm_to_skb(siocb->scm, skb, true); |
1425 | if (err) | 1425 | if (err) |
1426 | goto out_free; | 1426 | goto out_free; |
1427 | unix_get_secdata(siocb->scm, skb); | 1427 | unix_get_secdata(siocb->scm, skb); |
1428 | 1428 | ||
1429 | skb_reset_transport_header(skb); | 1429 | skb_reset_transport_header(skb); |
1430 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); | 1430 | err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len); |
1431 | if (err) | 1431 | if (err) |
1432 | goto out_free; | 1432 | goto out_free; |
1433 | 1433 | ||
1434 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); | 1434 | timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
1435 | 1435 | ||
1436 | restart: | 1436 | restart: |
1437 | if (!other) { | 1437 | if (!other) { |
1438 | err = -ECONNRESET; | 1438 | err = -ECONNRESET; |
1439 | if (sunaddr == NULL) | 1439 | if (sunaddr == NULL) |
1440 | goto out_free; | 1440 | goto out_free; |
1441 | 1441 | ||
1442 | other = unix_find_other(net, sunaddr, namelen, sk->sk_type, | 1442 | other = unix_find_other(net, sunaddr, namelen, sk->sk_type, |
1443 | hash, &err); | 1443 | hash, &err); |
1444 | if (other == NULL) | 1444 | if (other == NULL) |
1445 | goto out_free; | 1445 | goto out_free; |
1446 | } | 1446 | } |
1447 | 1447 | ||
1448 | unix_state_lock(other); | 1448 | unix_state_lock(other); |
1449 | err = -EPERM; | 1449 | err = -EPERM; |
1450 | if (!unix_may_send(sk, other)) | 1450 | if (!unix_may_send(sk, other)) |
1451 | goto out_unlock; | 1451 | goto out_unlock; |
1452 | 1452 | ||
1453 | if (sock_flag(other, SOCK_DEAD)) { | 1453 | if (sock_flag(other, SOCK_DEAD)) { |
1454 | /* | 1454 | /* |
1455 | * Check with 1003.1g - what should | 1455 | * Check with 1003.1g - what should |
1456 | * datagram error | 1456 | * datagram error |
1457 | */ | 1457 | */ |
1458 | unix_state_unlock(other); | 1458 | unix_state_unlock(other); |
1459 | sock_put(other); | 1459 | sock_put(other); |
1460 | 1460 | ||
1461 | err = 0; | 1461 | err = 0; |
1462 | unix_state_lock(sk); | 1462 | unix_state_lock(sk); |
1463 | if (unix_peer(sk) == other) { | 1463 | if (unix_peer(sk) == other) { |
1464 | unix_peer(sk) = NULL; | 1464 | unix_peer(sk) = NULL; |
1465 | unix_state_unlock(sk); | 1465 | unix_state_unlock(sk); |
1466 | 1466 | ||
1467 | unix_dgram_disconnected(sk, other); | 1467 | unix_dgram_disconnected(sk, other); |
1468 | sock_put(other); | 1468 | sock_put(other); |
1469 | err = -ECONNREFUSED; | 1469 | err = -ECONNREFUSED; |
1470 | } else { | 1470 | } else { |
1471 | unix_state_unlock(sk); | 1471 | unix_state_unlock(sk); |
1472 | } | 1472 | } |
1473 | 1473 | ||
1474 | other = NULL; | 1474 | other = NULL; |
1475 | if (err) | 1475 | if (err) |
1476 | goto out_free; | 1476 | goto out_free; |
1477 | goto restart; | 1477 | goto restart; |
1478 | } | 1478 | } |
1479 | 1479 | ||
1480 | err = -EPIPE; | 1480 | err = -EPIPE; |
1481 | if (other->sk_shutdown & RCV_SHUTDOWN) | 1481 | if (other->sk_shutdown & RCV_SHUTDOWN) |
1482 | goto out_unlock; | 1482 | goto out_unlock; |
1483 | 1483 | ||
1484 | if (sk->sk_type != SOCK_SEQPACKET) { | 1484 | if (sk->sk_type != SOCK_SEQPACKET) { |
1485 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); | 1485 | err = security_unix_may_send(sk->sk_socket, other->sk_socket); |
1486 | if (err) | 1486 | if (err) |
1487 | goto out_unlock; | 1487 | goto out_unlock; |
1488 | } | 1488 | } |
1489 | 1489 | ||
1490 | if (unix_peer(other) != sk && unix_recvq_full(other)) { | 1490 | if (unix_peer(other) != sk && unix_recvq_full(other)) { |
1491 | if (!timeo) { | 1491 | if (!timeo) { |
1492 | err = -EAGAIN; | 1492 | err = -EAGAIN; |
1493 | goto out_unlock; | 1493 | goto out_unlock; |
1494 | } | 1494 | } |
1495 | 1495 | ||
1496 | timeo = unix_wait_for_peer(other, timeo); | 1496 | timeo = unix_wait_for_peer(other, timeo); |
1497 | 1497 | ||
1498 | err = sock_intr_errno(timeo); | 1498 | err = sock_intr_errno(timeo); |
1499 | if (signal_pending(current)) | 1499 | if (signal_pending(current)) |
1500 | goto out_free; | 1500 | goto out_free; |
1501 | 1501 | ||
1502 | goto restart; | 1502 | goto restart; |
1503 | } | 1503 | } |
1504 | 1504 | ||
1505 | skb_queue_tail(&other->sk_receive_queue, skb); | 1505 | skb_queue_tail(&other->sk_receive_queue, skb); |
1506 | unix_state_unlock(other); | 1506 | unix_state_unlock(other); |
1507 | other->sk_data_ready(other, len); | 1507 | other->sk_data_ready(other, len); |
1508 | sock_put(other); | 1508 | sock_put(other); |
1509 | scm_destroy(siocb->scm); | 1509 | scm_destroy(siocb->scm); |
1510 | return len; | 1510 | return len; |
1511 | 1511 | ||
1512 | out_unlock: | 1512 | out_unlock: |
1513 | unix_state_unlock(other); | 1513 | unix_state_unlock(other); |
1514 | out_free: | 1514 | out_free: |
1515 | kfree_skb(skb); | 1515 | kfree_skb(skb); |
1516 | out: | 1516 | out: |
1517 | if (other) | 1517 | if (other) |
1518 | sock_put(other); | 1518 | sock_put(other); |
1519 | scm_destroy(siocb->scm); | 1519 | scm_destroy(siocb->scm); |
1520 | return err; | 1520 | return err; |
1521 | } | 1521 | } |
1522 | 1522 | ||
1523 | 1523 | ||
1524 | static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, | 1524 | static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, |
1525 | struct msghdr *msg, size_t len) | 1525 | struct msghdr *msg, size_t len) |
1526 | { | 1526 | { |
1527 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); | 1527 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
1528 | struct sock *sk = sock->sk; | 1528 | struct sock *sk = sock->sk; |
1529 | struct sock *other = NULL; | 1529 | struct sock *other = NULL; |
1530 | struct sockaddr_un *sunaddr = msg->msg_name; | 1530 | struct sockaddr_un *sunaddr = msg->msg_name; |
1531 | int err, size; | 1531 | int err, size; |
1532 | struct sk_buff *skb; | 1532 | struct sk_buff *skb; |
1533 | int sent = 0; | 1533 | int sent = 0; |
1534 | struct scm_cookie tmp_scm; | 1534 | struct scm_cookie tmp_scm; |
1535 | bool fds_sent = false; | 1535 | bool fds_sent = false; |
1536 | 1536 | ||
1537 | if (NULL == siocb->scm) | 1537 | if (NULL == siocb->scm) |
1538 | siocb->scm = &tmp_scm; | 1538 | siocb->scm = &tmp_scm; |
1539 | wait_for_unix_gc(); | 1539 | wait_for_unix_gc(); |
1540 | err = scm_send(sock, msg, siocb->scm); | 1540 | err = scm_send(sock, msg, siocb->scm); |
1541 | if (err < 0) | 1541 | if (err < 0) |
1542 | return err; | 1542 | return err; |
1543 | 1543 | ||
1544 | err = -EOPNOTSUPP; | 1544 | err = -EOPNOTSUPP; |
1545 | if (msg->msg_flags&MSG_OOB) | 1545 | if (msg->msg_flags&MSG_OOB) |
1546 | goto out_err; | 1546 | goto out_err; |
1547 | 1547 | ||
1548 | if (msg->msg_namelen) { | 1548 | if (msg->msg_namelen) { |
1549 | err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; | 1549 | err = sk->sk_state == TCP_ESTABLISHED ? -EISCONN : -EOPNOTSUPP; |
1550 | goto out_err; | 1550 | goto out_err; |
1551 | } else { | 1551 | } else { |
1552 | sunaddr = NULL; | 1552 | sunaddr = NULL; |
1553 | err = -ENOTCONN; | 1553 | err = -ENOTCONN; |
1554 | other = unix_peer(sk); | 1554 | other = unix_peer(sk); |
1555 | if (!other) | 1555 | if (!other) |
1556 | goto out_err; | 1556 | goto out_err; |
1557 | } | 1557 | } |
1558 | 1558 | ||
1559 | if (sk->sk_shutdown & SEND_SHUTDOWN) | 1559 | if (sk->sk_shutdown & SEND_SHUTDOWN) |
1560 | goto pipe_err; | 1560 | goto pipe_err; |
1561 | 1561 | ||
1562 | while (sent < len) { | 1562 | while (sent < len) { |
1563 | /* | 1563 | /* |
1564 | * Optimisation for the fact that under 0.01% of X | 1564 | * Optimisation for the fact that under 0.01% of X |
1565 | * messages typically need breaking up. | 1565 | * messages typically need breaking up. |
1566 | */ | 1566 | */ |
1567 | 1567 | ||
1568 | size = len-sent; | 1568 | size = len-sent; |
1569 | 1569 | ||
1570 | /* Keep two messages in the pipe so it schedules better */ | 1570 | /* Keep two messages in the pipe so it schedules better */ |
1571 | if (size > ((sk->sk_sndbuf >> 1) - 64)) | 1571 | if (size > ((sk->sk_sndbuf >> 1) - 64)) |
1572 | size = (sk->sk_sndbuf >> 1) - 64; | 1572 | size = (sk->sk_sndbuf >> 1) - 64; |
1573 | 1573 | ||
1574 | if (size > SKB_MAX_ALLOC) | 1574 | if (size > SKB_MAX_ALLOC) |
1575 | size = SKB_MAX_ALLOC; | 1575 | size = SKB_MAX_ALLOC; |
1576 | 1576 | ||
1577 | /* | 1577 | /* |
1578 | * Grab a buffer | 1578 | * Grab a buffer |
1579 | */ | 1579 | */ |
1580 | 1580 | ||
1581 | skb = sock_alloc_send_skb(sk, size, msg->msg_flags&MSG_DONTWAIT, | 1581 | skb = sock_alloc_send_skb(sk, size, msg->msg_flags&MSG_DONTWAIT, |
1582 | &err); | 1582 | &err); |
1583 | 1583 | ||
1584 | if (skb == NULL) | 1584 | if (skb == NULL) |
1585 | goto out_err; | 1585 | goto out_err; |
1586 | 1586 | ||
1587 | /* | 1587 | /* |
1588 | * If you pass two values to the sock_alloc_send_skb | 1588 | * If you pass two values to the sock_alloc_send_skb |
1589 | * it tries to grab the large buffer with GFP_NOFS | 1589 | * it tries to grab the large buffer with GFP_NOFS |
1590 | * (which can fail easily), and if it fails grab the | 1590 | * (which can fail easily), and if it fails grab the |
1591 | * fallback size buffer which is under a page and will | 1591 | * fallback size buffer which is under a page and will |
1592 | * succeed. [Alan] | 1592 | * succeed. [Alan] |
1593 | */ | 1593 | */ |
1594 | size = min_t(int, size, skb_tailroom(skb)); | 1594 | size = min_t(int, size, skb_tailroom(skb)); |
1595 | 1595 | ||
1596 | 1596 | ||
1597 | /* Only send the fds in the first buffer */ | 1597 | /* Only send the fds in the first buffer */ |
1598 | err = unix_scm_to_skb(siocb->scm, skb, !fds_sent); | 1598 | err = unix_scm_to_skb(siocb->scm, skb, !fds_sent); |
1599 | if (err) { | 1599 | if (err) { |
1600 | kfree_skb(skb); | 1600 | kfree_skb(skb); |
1601 | goto out_err; | 1601 | goto out_err; |
1602 | } | 1602 | } |
1603 | fds_sent = true; | 1603 | fds_sent = true; |
1604 | 1604 | ||
1605 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); | 1605 | err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size); |
1606 | if (err) { | 1606 | if (err) { |
1607 | kfree_skb(skb); | 1607 | kfree_skb(skb); |
1608 | goto out_err; | 1608 | goto out_err; |
1609 | } | 1609 | } |
1610 | 1610 | ||
1611 | unix_state_lock(other); | 1611 | unix_state_lock(other); |
1612 | 1612 | ||
1613 | if (sock_flag(other, SOCK_DEAD) || | 1613 | if (sock_flag(other, SOCK_DEAD) || |
1614 | (other->sk_shutdown & RCV_SHUTDOWN)) | 1614 | (other->sk_shutdown & RCV_SHUTDOWN)) |
1615 | goto pipe_err_free; | 1615 | goto pipe_err_free; |
1616 | 1616 | ||
1617 | skb_queue_tail(&other->sk_receive_queue, skb); | 1617 | skb_queue_tail(&other->sk_receive_queue, skb); |
1618 | unix_state_unlock(other); | 1618 | unix_state_unlock(other); |
1619 | other->sk_data_ready(other, size); | 1619 | other->sk_data_ready(other, size); |
1620 | sent += size; | 1620 | sent += size; |
1621 | } | 1621 | } |
1622 | 1622 | ||
1623 | scm_destroy(siocb->scm); | 1623 | scm_destroy(siocb->scm); |
1624 | siocb->scm = NULL; | 1624 | siocb->scm = NULL; |
1625 | 1625 | ||
1626 | return sent; | 1626 | return sent; |
1627 | 1627 | ||
1628 | pipe_err_free: | 1628 | pipe_err_free: |
1629 | unix_state_unlock(other); | 1629 | unix_state_unlock(other); |
1630 | kfree_skb(skb); | 1630 | kfree_skb(skb); |
1631 | pipe_err: | 1631 | pipe_err: |
1632 | if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) | 1632 | if (sent == 0 && !(msg->msg_flags&MSG_NOSIGNAL)) |
1633 | send_sig(SIGPIPE, current, 0); | 1633 | send_sig(SIGPIPE, current, 0); |
1634 | err = -EPIPE; | 1634 | err = -EPIPE; |
1635 | out_err: | 1635 | out_err: |
1636 | scm_destroy(siocb->scm); | 1636 | scm_destroy(siocb->scm); |
1637 | siocb->scm = NULL; | 1637 | siocb->scm = NULL; |
1638 | return sent ? : err; | 1638 | return sent ? : err; |
1639 | } | 1639 | } |
1640 | 1640 | ||
1641 | static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock, | 1641 | static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock, |
1642 | struct msghdr *msg, size_t len) | 1642 | struct msghdr *msg, size_t len) |
1643 | { | 1643 | { |
1644 | int err; | 1644 | int err; |
1645 | struct sock *sk = sock->sk; | 1645 | struct sock *sk = sock->sk; |
1646 | 1646 | ||
1647 | err = sock_error(sk); | 1647 | err = sock_error(sk); |
1648 | if (err) | 1648 | if (err) |
1649 | return err; | 1649 | return err; |
1650 | 1650 | ||
1651 | if (sk->sk_state != TCP_ESTABLISHED) | 1651 | if (sk->sk_state != TCP_ESTABLISHED) |
1652 | return -ENOTCONN; | 1652 | return -ENOTCONN; |
1653 | 1653 | ||
1654 | if (msg->msg_namelen) | 1654 | if (msg->msg_namelen) |
1655 | msg->msg_namelen = 0; | 1655 | msg->msg_namelen = 0; |
1656 | 1656 | ||
1657 | return unix_dgram_sendmsg(kiocb, sock, msg, len); | 1657 | return unix_dgram_sendmsg(kiocb, sock, msg, len); |
1658 | } | 1658 | } |
1659 | 1659 | ||
1660 | static void unix_copy_addr(struct msghdr *msg, struct sock *sk) | 1660 | static void unix_copy_addr(struct msghdr *msg, struct sock *sk) |
1661 | { | 1661 | { |
1662 | struct unix_sock *u = unix_sk(sk); | 1662 | struct unix_sock *u = unix_sk(sk); |
1663 | 1663 | ||
1664 | msg->msg_namelen = 0; | 1664 | msg->msg_namelen = 0; |
1665 | if (u->addr) { | 1665 | if (u->addr) { |
1666 | msg->msg_namelen = u->addr->len; | 1666 | msg->msg_namelen = u->addr->len; |
1667 | memcpy(msg->msg_name, u->addr->name, u->addr->len); | 1667 | memcpy(msg->msg_name, u->addr->name, u->addr->len); |
1668 | } | 1668 | } |
1669 | } | 1669 | } |
1670 | 1670 | ||
1671 | static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock, | 1671 | static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock, |
1672 | struct msghdr *msg, size_t size, | 1672 | struct msghdr *msg, size_t size, |
1673 | int flags) | 1673 | int flags) |
1674 | { | 1674 | { |
1675 | struct sock_iocb *siocb = kiocb_to_siocb(iocb); | 1675 | struct sock_iocb *siocb = kiocb_to_siocb(iocb); |
1676 | struct scm_cookie tmp_scm; | 1676 | struct scm_cookie tmp_scm; |
1677 | struct sock *sk = sock->sk; | 1677 | struct sock *sk = sock->sk; |
1678 | struct unix_sock *u = unix_sk(sk); | 1678 | struct unix_sock *u = unix_sk(sk); |
1679 | int noblock = flags & MSG_DONTWAIT; | 1679 | int noblock = flags & MSG_DONTWAIT; |
1680 | struct sk_buff *skb; | 1680 | struct sk_buff *skb; |
1681 | int err; | 1681 | int err; |
1682 | 1682 | ||
1683 | err = -EOPNOTSUPP; | 1683 | err = -EOPNOTSUPP; |
1684 | if (flags&MSG_OOB) | 1684 | if (flags&MSG_OOB) |
1685 | goto out; | 1685 | goto out; |
1686 | 1686 | ||
1687 | msg->msg_namelen = 0; | 1687 | msg->msg_namelen = 0; |
1688 | 1688 | ||
1689 | mutex_lock(&u->readlock); | 1689 | mutex_lock(&u->readlock); |
1690 | 1690 | ||
1691 | skb = skb_recv_datagram(sk, flags, noblock, &err); | 1691 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
1692 | if (!skb) { | 1692 | if (!skb) { |
1693 | unix_state_lock(sk); | 1693 | unix_state_lock(sk); |
1694 | /* Signal EOF on disconnected non-blocking SEQPACKET socket. */ | 1694 | /* Signal EOF on disconnected non-blocking SEQPACKET socket. */ |
1695 | if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN && | 1695 | if (sk->sk_type == SOCK_SEQPACKET && err == -EAGAIN && |
1696 | (sk->sk_shutdown & RCV_SHUTDOWN)) | 1696 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
1697 | err = 0; | 1697 | err = 0; |
1698 | unix_state_unlock(sk); | 1698 | unix_state_unlock(sk); |
1699 | goto out_unlock; | 1699 | goto out_unlock; |
1700 | } | 1700 | } |
1701 | 1701 | ||
1702 | wake_up_interruptible_sync(&u->peer_wait); | 1702 | wake_up_interruptible_sync(&u->peer_wait); |
1703 | 1703 | ||
1704 | if (msg->msg_name) | 1704 | if (msg->msg_name) |
1705 | unix_copy_addr(msg, skb->sk); | 1705 | unix_copy_addr(msg, skb->sk); |
1706 | 1706 | ||
1707 | if (size > skb->len) | 1707 | if (size > skb->len) |
1708 | size = skb->len; | 1708 | size = skb->len; |
1709 | else if (size < skb->len) | 1709 | else if (size < skb->len) |
1710 | msg->msg_flags |= MSG_TRUNC; | 1710 | msg->msg_flags |= MSG_TRUNC; |
1711 | 1711 | ||
1712 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size); | 1712 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, size); |
1713 | if (err) | 1713 | if (err) |
1714 | goto out_free; | 1714 | goto out_free; |
1715 | 1715 | ||
1716 | if (!siocb->scm) { | 1716 | if (!siocb->scm) { |
1717 | siocb->scm = &tmp_scm; | 1717 | siocb->scm = &tmp_scm; |
1718 | memset(&tmp_scm, 0, sizeof(tmp_scm)); | 1718 | memset(&tmp_scm, 0, sizeof(tmp_scm)); |
1719 | } | 1719 | } |
1720 | scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred); | 1720 | scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred); |
1721 | unix_set_secdata(siocb->scm, skb); | 1721 | unix_set_secdata(siocb->scm, skb); |
1722 | 1722 | ||
1723 | if (!(flags & MSG_PEEK)) { | 1723 | if (!(flags & MSG_PEEK)) { |
1724 | if (UNIXCB(skb).fp) | 1724 | if (UNIXCB(skb).fp) |
1725 | unix_detach_fds(siocb->scm, skb); | 1725 | unix_detach_fds(siocb->scm, skb); |
1726 | } else { | 1726 | } else { |
1727 | /* It is questionable: on PEEK we could: | 1727 | /* It is questionable: on PEEK we could: |
1728 | - do not return fds - good, but too simple 8) | 1728 | - do not return fds - good, but too simple 8) |
1729 | - return fds, and do not return them on read (old strategy, | 1729 | - return fds, and do not return them on read (old strategy, |
1730 | apparently wrong) | 1730 | apparently wrong) |
1731 | - clone fds (I chose it for now, it is the most universal | 1731 | - clone fds (I chose it for now, it is the most universal |
1732 | solution) | 1732 | solution) |
1733 | 1733 | ||
1734 | POSIX 1003.1g does not actually define this clearly | 1734 | POSIX 1003.1g does not actually define this clearly |
1735 | at all. POSIX 1003.1g doesn't define a lot of things | 1735 | at all. POSIX 1003.1g doesn't define a lot of things |
1736 | clearly however! | 1736 | clearly however! |
1737 | 1737 | ||
1738 | */ | 1738 | */ |
1739 | if (UNIXCB(skb).fp) | 1739 | if (UNIXCB(skb).fp) |
1740 | siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp); | 1740 | siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp); |
1741 | } | 1741 | } |
1742 | err = size; | 1742 | err = size; |
1743 | 1743 | ||
1744 | scm_recv(sock, msg, siocb->scm, flags); | 1744 | scm_recv(sock, msg, siocb->scm, flags); |
1745 | 1745 | ||
1746 | out_free: | 1746 | out_free: |
1747 | skb_free_datagram(sk, skb); | 1747 | skb_free_datagram(sk, skb); |
1748 | out_unlock: | 1748 | out_unlock: |
1749 | mutex_unlock(&u->readlock); | 1749 | mutex_unlock(&u->readlock); |
1750 | out: | 1750 | out: |
1751 | return err; | 1751 | return err; |
1752 | } | 1752 | } |
1753 | 1753 | ||
1754 | /* | 1754 | /* |
1755 | * Sleep until data has arrive. But check for races.. | 1755 | * Sleep until data has arrive. But check for races.. |
1756 | */ | 1756 | */ |
1757 | 1757 | ||
1758 | static long unix_stream_data_wait(struct sock *sk, long timeo) | 1758 | static long unix_stream_data_wait(struct sock *sk, long timeo) |
1759 | { | 1759 | { |
1760 | DEFINE_WAIT(wait); | 1760 | DEFINE_WAIT(wait); |
1761 | 1761 | ||
1762 | unix_state_lock(sk); | 1762 | unix_state_lock(sk); |
1763 | 1763 | ||
1764 | for (;;) { | 1764 | for (;;) { |
1765 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); | 1765 | prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); |
1766 | 1766 | ||
1767 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 1767 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
1768 | sk->sk_err || | 1768 | sk->sk_err || |
1769 | (sk->sk_shutdown & RCV_SHUTDOWN) || | 1769 | (sk->sk_shutdown & RCV_SHUTDOWN) || |
1770 | signal_pending(current) || | 1770 | signal_pending(current) || |
1771 | !timeo) | 1771 | !timeo) |
1772 | break; | 1772 | break; |
1773 | 1773 | ||
1774 | set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); | 1774 | set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); |
1775 | unix_state_unlock(sk); | 1775 | unix_state_unlock(sk); |
1776 | timeo = schedule_timeout(timeo); | 1776 | timeo = schedule_timeout(timeo); |
1777 | unix_state_lock(sk); | 1777 | unix_state_lock(sk); |
1778 | clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); | 1778 | clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags); |
1779 | } | 1779 | } |
1780 | 1780 | ||
1781 | finish_wait(sk_sleep(sk), &wait); | 1781 | finish_wait(sk_sleep(sk), &wait); |
1782 | unix_state_unlock(sk); | 1782 | unix_state_unlock(sk); |
1783 | return timeo; | 1783 | return timeo; |
1784 | } | 1784 | } |
1785 | 1785 | ||
1786 | 1786 | ||
1787 | 1787 | ||
1788 | static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock, | 1788 | static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock, |
1789 | struct msghdr *msg, size_t size, | 1789 | struct msghdr *msg, size_t size, |
1790 | int flags) | 1790 | int flags) |
1791 | { | 1791 | { |
1792 | struct sock_iocb *siocb = kiocb_to_siocb(iocb); | 1792 | struct sock_iocb *siocb = kiocb_to_siocb(iocb); |
1793 | struct scm_cookie tmp_scm; | 1793 | struct scm_cookie tmp_scm; |
1794 | struct sock *sk = sock->sk; | 1794 | struct sock *sk = sock->sk; |
1795 | struct unix_sock *u = unix_sk(sk); | 1795 | struct unix_sock *u = unix_sk(sk); |
1796 | struct sockaddr_un *sunaddr = msg->msg_name; | 1796 | struct sockaddr_un *sunaddr = msg->msg_name; |
1797 | int copied = 0; | 1797 | int copied = 0; |
1798 | int check_creds = 0; | 1798 | int check_creds = 0; |
1799 | int target; | 1799 | int target; |
1800 | int err = 0; | 1800 | int err = 0; |
1801 | long timeo; | 1801 | long timeo; |
1802 | 1802 | ||
1803 | err = -EINVAL; | 1803 | err = -EINVAL; |
1804 | if (sk->sk_state != TCP_ESTABLISHED) | 1804 | if (sk->sk_state != TCP_ESTABLISHED) |
1805 | goto out; | 1805 | goto out; |
1806 | 1806 | ||
1807 | err = -EOPNOTSUPP; | 1807 | err = -EOPNOTSUPP; |
1808 | if (flags&MSG_OOB) | 1808 | if (flags&MSG_OOB) |
1809 | goto out; | 1809 | goto out; |
1810 | 1810 | ||
1811 | target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); | 1811 | target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); |
1812 | timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); | 1812 | timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); |
1813 | 1813 | ||
1814 | msg->msg_namelen = 0; | 1814 | msg->msg_namelen = 0; |
1815 | 1815 | ||
1816 | /* Lock the socket to prevent queue disordering | 1816 | /* Lock the socket to prevent queue disordering |
1817 | * while sleeps in memcpy_tomsg | 1817 | * while sleeps in memcpy_tomsg |
1818 | */ | 1818 | */ |
1819 | 1819 | ||
1820 | if (!siocb->scm) { | 1820 | if (!siocb->scm) { |
1821 | siocb->scm = &tmp_scm; | 1821 | siocb->scm = &tmp_scm; |
1822 | memset(&tmp_scm, 0, sizeof(tmp_scm)); | 1822 | memset(&tmp_scm, 0, sizeof(tmp_scm)); |
1823 | } | 1823 | } |
1824 | 1824 | ||
1825 | mutex_lock(&u->readlock); | 1825 | mutex_lock(&u->readlock); |
1826 | 1826 | ||
1827 | do { | 1827 | do { |
1828 | int chunk; | 1828 | int chunk; |
1829 | struct sk_buff *skb; | 1829 | struct sk_buff *skb; |
1830 | 1830 | ||
1831 | unix_state_lock(sk); | 1831 | unix_state_lock(sk); |
1832 | skb = skb_dequeue(&sk->sk_receive_queue); | 1832 | skb = skb_dequeue(&sk->sk_receive_queue); |
1833 | if (skb == NULL) { | 1833 | if (skb == NULL) { |
1834 | if (copied >= target) | 1834 | if (copied >= target) |
1835 | goto unlock; | 1835 | goto unlock; |
1836 | 1836 | ||
1837 | /* | 1837 | /* |
1838 | * POSIX 1003.1g mandates this order. | 1838 | * POSIX 1003.1g mandates this order. |
1839 | */ | 1839 | */ |
1840 | 1840 | ||
1841 | err = sock_error(sk); | 1841 | err = sock_error(sk); |
1842 | if (err) | 1842 | if (err) |
1843 | goto unlock; | 1843 | goto unlock; |
1844 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 1844 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
1845 | goto unlock; | 1845 | goto unlock; |
1846 | 1846 | ||
1847 | unix_state_unlock(sk); | 1847 | unix_state_unlock(sk); |
1848 | err = -EAGAIN; | 1848 | err = -EAGAIN; |
1849 | if (!timeo) | 1849 | if (!timeo) |
1850 | break; | 1850 | break; |
1851 | mutex_unlock(&u->readlock); | 1851 | mutex_unlock(&u->readlock); |
1852 | 1852 | ||
1853 | timeo = unix_stream_data_wait(sk, timeo); | 1853 | timeo = unix_stream_data_wait(sk, timeo); |
1854 | 1854 | ||
1855 | if (signal_pending(current)) { | 1855 | if (signal_pending(current)) { |
1856 | err = sock_intr_errno(timeo); | 1856 | err = sock_intr_errno(timeo); |
1857 | goto out; | 1857 | goto out; |
1858 | } | 1858 | } |
1859 | mutex_lock(&u->readlock); | 1859 | mutex_lock(&u->readlock); |
1860 | continue; | 1860 | continue; |
1861 | unlock: | 1861 | unlock: |
1862 | unix_state_unlock(sk); | 1862 | unix_state_unlock(sk); |
1863 | break; | 1863 | break; |
1864 | } | 1864 | } |
1865 | unix_state_unlock(sk); | 1865 | unix_state_unlock(sk); |
1866 | 1866 | ||
1867 | if (check_creds) { | 1867 | if (check_creds) { |
1868 | /* Never glue messages from different writers */ | 1868 | /* Never glue messages from different writers */ |
1869 | if ((UNIXCB(skb).pid != siocb->scm->pid) || | 1869 | if ((UNIXCB(skb).pid != siocb->scm->pid) || |
1870 | (UNIXCB(skb).cred != siocb->scm->cred)) { | 1870 | (UNIXCB(skb).cred != siocb->scm->cred)) { |
1871 | skb_queue_head(&sk->sk_receive_queue, skb); | 1871 | skb_queue_head(&sk->sk_receive_queue, skb); |
1872 | break; | 1872 | break; |
1873 | } | 1873 | } |
1874 | } else { | 1874 | } else { |
1875 | /* Copy credentials */ | 1875 | /* Copy credentials */ |
1876 | scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred); | 1876 | scm_set_cred(siocb->scm, UNIXCB(skb).pid, UNIXCB(skb).cred); |
1877 | check_creds = 1; | 1877 | check_creds = 1; |
1878 | } | 1878 | } |
1879 | 1879 | ||
1880 | /* Copy address just once */ | 1880 | /* Copy address just once */ |
1881 | if (sunaddr) { | 1881 | if (sunaddr) { |
1882 | unix_copy_addr(msg, skb->sk); | 1882 | unix_copy_addr(msg, skb->sk); |
1883 | sunaddr = NULL; | 1883 | sunaddr = NULL; |
1884 | } | 1884 | } |
1885 | 1885 | ||
1886 | chunk = min_t(unsigned int, skb->len, size); | 1886 | chunk = min_t(unsigned int, skb->len, size); |
1887 | if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) { | 1887 | if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) { |
1888 | skb_queue_head(&sk->sk_receive_queue, skb); | 1888 | skb_queue_head(&sk->sk_receive_queue, skb); |
1889 | if (copied == 0) | 1889 | if (copied == 0) |
1890 | copied = -EFAULT; | 1890 | copied = -EFAULT; |
1891 | break; | 1891 | break; |
1892 | } | 1892 | } |
1893 | copied += chunk; | 1893 | copied += chunk; |
1894 | size -= chunk; | 1894 | size -= chunk; |
1895 | 1895 | ||
1896 | /* Mark read part of skb as used */ | 1896 | /* Mark read part of skb as used */ |
1897 | if (!(flags & MSG_PEEK)) { | 1897 | if (!(flags & MSG_PEEK)) { |
1898 | skb_pull(skb, chunk); | 1898 | skb_pull(skb, chunk); |
1899 | 1899 | ||
1900 | if (UNIXCB(skb).fp) | 1900 | if (UNIXCB(skb).fp) |
1901 | unix_detach_fds(siocb->scm, skb); | 1901 | unix_detach_fds(siocb->scm, skb); |
1902 | 1902 | ||
1903 | /* put the skb back if we didn't use it up.. */ | 1903 | /* put the skb back if we didn't use it up.. */ |
1904 | if (skb->len) { | 1904 | if (skb->len) { |
1905 | skb_queue_head(&sk->sk_receive_queue, skb); | 1905 | skb_queue_head(&sk->sk_receive_queue, skb); |
1906 | break; | 1906 | break; |
1907 | } | 1907 | } |
1908 | 1908 | ||
1909 | consume_skb(skb); | 1909 | consume_skb(skb); |
1910 | 1910 | ||
1911 | if (siocb->scm->fp) | 1911 | if (siocb->scm->fp) |
1912 | break; | 1912 | break; |
1913 | } else { | 1913 | } else { |
1914 | /* It is questionable, see note in unix_dgram_recvmsg. | 1914 | /* It is questionable, see note in unix_dgram_recvmsg. |
1915 | */ | 1915 | */ |
1916 | if (UNIXCB(skb).fp) | 1916 | if (UNIXCB(skb).fp) |
1917 | siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp); | 1917 | siocb->scm->fp = scm_fp_dup(UNIXCB(skb).fp); |
1918 | 1918 | ||
1919 | /* put message back and return */ | 1919 | /* put message back and return */ |
1920 | skb_queue_head(&sk->sk_receive_queue, skb); | 1920 | skb_queue_head(&sk->sk_receive_queue, skb); |
1921 | break; | 1921 | break; |
1922 | } | 1922 | } |
1923 | } while (size); | 1923 | } while (size); |
1924 | 1924 | ||
1925 | mutex_unlock(&u->readlock); | 1925 | mutex_unlock(&u->readlock); |
1926 | scm_recv(sock, msg, siocb->scm, flags); | 1926 | scm_recv(sock, msg, siocb->scm, flags); |
1927 | out: | 1927 | out: |
1928 | return copied ? : err; | 1928 | return copied ? : err; |
1929 | } | 1929 | } |
1930 | 1930 | ||
1931 | static int unix_shutdown(struct socket *sock, int mode) | 1931 | static int unix_shutdown(struct socket *sock, int mode) |
1932 | { | 1932 | { |
1933 | struct sock *sk = sock->sk; | 1933 | struct sock *sk = sock->sk; |
1934 | struct sock *other; | 1934 | struct sock *other; |
1935 | 1935 | ||
1936 | mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN); | 1936 | mode = (mode+1)&(RCV_SHUTDOWN|SEND_SHUTDOWN); |
1937 | 1937 | ||
1938 | if (mode) { | 1938 | if (mode) { |
1939 | unix_state_lock(sk); | 1939 | unix_state_lock(sk); |
1940 | sk->sk_shutdown |= mode; | 1940 | sk->sk_shutdown |= mode; |
1941 | other = unix_peer(sk); | 1941 | other = unix_peer(sk); |
1942 | if (other) | 1942 | if (other) |
1943 | sock_hold(other); | 1943 | sock_hold(other); |
1944 | unix_state_unlock(sk); | 1944 | unix_state_unlock(sk); |
1945 | sk->sk_state_change(sk); | 1945 | sk->sk_state_change(sk); |
1946 | 1946 | ||
1947 | if (other && | 1947 | if (other && |
1948 | (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) { | 1948 | (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET)) { |
1949 | 1949 | ||
1950 | int peer_mode = 0; | 1950 | int peer_mode = 0; |
1951 | 1951 | ||
1952 | if (mode&RCV_SHUTDOWN) | 1952 | if (mode&RCV_SHUTDOWN) |
1953 | peer_mode |= SEND_SHUTDOWN; | 1953 | peer_mode |= SEND_SHUTDOWN; |
1954 | if (mode&SEND_SHUTDOWN) | 1954 | if (mode&SEND_SHUTDOWN) |
1955 | peer_mode |= RCV_SHUTDOWN; | 1955 | peer_mode |= RCV_SHUTDOWN; |
1956 | unix_state_lock(other); | 1956 | unix_state_lock(other); |
1957 | other->sk_shutdown |= peer_mode; | 1957 | other->sk_shutdown |= peer_mode; |
1958 | unix_state_unlock(other); | 1958 | unix_state_unlock(other); |
1959 | other->sk_state_change(other); | 1959 | other->sk_state_change(other); |
1960 | if (peer_mode == SHUTDOWN_MASK) | 1960 | if (peer_mode == SHUTDOWN_MASK) |
1961 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP); | 1961 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_HUP); |
1962 | else if (peer_mode & RCV_SHUTDOWN) | 1962 | else if (peer_mode & RCV_SHUTDOWN) |
1963 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN); | 1963 | sk_wake_async(other, SOCK_WAKE_WAITD, POLL_IN); |
1964 | } | 1964 | } |
1965 | if (other) | 1965 | if (other) |
1966 | sock_put(other); | 1966 | sock_put(other); |
1967 | } | 1967 | } |
1968 | return 0; | 1968 | return 0; |
1969 | } | 1969 | } |
1970 | 1970 | ||
1971 | static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) | 1971 | static int unix_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
1972 | { | 1972 | { |
1973 | struct sock *sk = sock->sk; | 1973 | struct sock *sk = sock->sk; |
1974 | long amount = 0; | 1974 | long amount = 0; |
1975 | int err; | 1975 | int err; |
1976 | 1976 | ||
1977 | switch (cmd) { | 1977 | switch (cmd) { |
1978 | case SIOCOUTQ: | 1978 | case SIOCOUTQ: |
1979 | amount = sk_wmem_alloc_get(sk); | 1979 | amount = sk_wmem_alloc_get(sk); |
1980 | err = put_user(amount, (int __user *)arg); | 1980 | err = put_user(amount, (int __user *)arg); |
1981 | break; | 1981 | break; |
1982 | case SIOCINQ: | 1982 | case SIOCINQ: |
1983 | { | 1983 | { |
1984 | struct sk_buff *skb; | 1984 | struct sk_buff *skb; |
1985 | 1985 | ||
1986 | if (sk->sk_state == TCP_LISTEN) { | 1986 | if (sk->sk_state == TCP_LISTEN) { |
1987 | err = -EINVAL; | 1987 | err = -EINVAL; |
1988 | break; | 1988 | break; |
1989 | } | 1989 | } |
1990 | 1990 | ||
1991 | spin_lock(&sk->sk_receive_queue.lock); | 1991 | spin_lock(&sk->sk_receive_queue.lock); |
1992 | if (sk->sk_type == SOCK_STREAM || | 1992 | if (sk->sk_type == SOCK_STREAM || |
1993 | sk->sk_type == SOCK_SEQPACKET) { | 1993 | sk->sk_type == SOCK_SEQPACKET) { |
1994 | skb_queue_walk(&sk->sk_receive_queue, skb) | 1994 | skb_queue_walk(&sk->sk_receive_queue, skb) |
1995 | amount += skb->len; | 1995 | amount += skb->len; |
1996 | } else { | 1996 | } else { |
1997 | skb = skb_peek(&sk->sk_receive_queue); | 1997 | skb = skb_peek(&sk->sk_receive_queue); |
1998 | if (skb) | 1998 | if (skb) |
1999 | amount = skb->len; | 1999 | amount = skb->len; |
2000 | } | 2000 | } |
2001 | spin_unlock(&sk->sk_receive_queue.lock); | 2001 | spin_unlock(&sk->sk_receive_queue.lock); |
2002 | err = put_user(amount, (int __user *)arg); | 2002 | err = put_user(amount, (int __user *)arg); |
2003 | break; | 2003 | break; |
2004 | } | 2004 | } |
2005 | 2005 | ||
2006 | default: | 2006 | default: |
2007 | err = -ENOIOCTLCMD; | 2007 | err = -ENOIOCTLCMD; |
2008 | break; | 2008 | break; |
2009 | } | 2009 | } |
2010 | return err; | 2010 | return err; |
2011 | } | 2011 | } |
2012 | 2012 | ||
2013 | static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait) | 2013 | static unsigned int unix_poll(struct file *file, struct socket *sock, poll_table *wait) |
2014 | { | 2014 | { |
2015 | struct sock *sk = sock->sk; | 2015 | struct sock *sk = sock->sk; |
2016 | unsigned int mask; | 2016 | unsigned int mask; |
2017 | 2017 | ||
2018 | sock_poll_wait(file, sk_sleep(sk), wait); | 2018 | sock_poll_wait(file, sk_sleep(sk), wait); |
2019 | mask = 0; | 2019 | mask = 0; |
2020 | 2020 | ||
2021 | /* exceptional events? */ | 2021 | /* exceptional events? */ |
2022 | if (sk->sk_err) | 2022 | if (sk->sk_err) |
2023 | mask |= POLLERR; | 2023 | mask |= POLLERR; |
2024 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 2024 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
2025 | mask |= POLLHUP; | 2025 | mask |= POLLHUP; |
2026 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 2026 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
2027 | mask |= POLLRDHUP; | 2027 | mask |= POLLRDHUP | POLLIN | POLLRDNORM; |
2028 | 2028 | ||
2029 | /* readable? */ | 2029 | /* readable? */ |
2030 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 2030 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
2031 | (sk->sk_shutdown & RCV_SHUTDOWN)) | ||
2032 | mask |= POLLIN | POLLRDNORM; | 2031 | mask |= POLLIN | POLLRDNORM; |
2033 | 2032 | ||
2034 | /* Connection-based need to check for termination and startup */ | 2033 | /* Connection-based need to check for termination and startup */ |
2035 | if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && | 2034 | if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && |
2036 | sk->sk_state == TCP_CLOSE) | 2035 | sk->sk_state == TCP_CLOSE) |
2037 | mask |= POLLHUP; | 2036 | mask |= POLLHUP; |
2038 | 2037 | ||
2039 | /* | 2038 | /* |
2040 | * we set writable also when the other side has shut down the | 2039 | * we set writable also when the other side has shut down the |
2041 | * connection. This prevents stuck sockets. | 2040 | * connection. This prevents stuck sockets. |
2042 | */ | 2041 | */ |
2043 | if (unix_writable(sk)) | 2042 | if (unix_writable(sk)) |
2044 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | 2043 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
2045 | 2044 | ||
2046 | return mask; | 2045 | return mask; |
2047 | } | 2046 | } |
2048 | 2047 | ||
2049 | static unsigned int unix_dgram_poll(struct file *file, struct socket *sock, | 2048 | static unsigned int unix_dgram_poll(struct file *file, struct socket *sock, |
2050 | poll_table *wait) | 2049 | poll_table *wait) |
2051 | { | 2050 | { |
2052 | struct sock *sk = sock->sk, *other; | 2051 | struct sock *sk = sock->sk, *other; |
2053 | unsigned int mask, writable; | 2052 | unsigned int mask, writable; |
2054 | 2053 | ||
2055 | sock_poll_wait(file, sk_sleep(sk), wait); | 2054 | sock_poll_wait(file, sk_sleep(sk), wait); |
2056 | mask = 0; | 2055 | mask = 0; |
2057 | 2056 | ||
2058 | /* exceptional events? */ | 2057 | /* exceptional events? */ |
2059 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) | 2058 | if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) |
2060 | mask |= POLLERR; | 2059 | mask |= POLLERR; |
2061 | if (sk->sk_shutdown & RCV_SHUTDOWN) | 2060 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
2062 | mask |= POLLRDHUP; | 2061 | mask |= POLLRDHUP; |
2063 | if (sk->sk_shutdown == SHUTDOWN_MASK) | 2062 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
2064 | mask |= POLLHUP; | 2063 | mask |= POLLHUP; |
2065 | 2064 | ||
2066 | /* readable? */ | 2065 | /* readable? */ |
2067 | if (!skb_queue_empty(&sk->sk_receive_queue) || | 2066 | if (!skb_queue_empty(&sk->sk_receive_queue) || |
2068 | (sk->sk_shutdown & RCV_SHUTDOWN)) | 2067 | (sk->sk_shutdown & RCV_SHUTDOWN)) |
2069 | mask |= POLLIN | POLLRDNORM; | 2068 | mask |= POLLIN | POLLRDNORM; |
2070 | 2069 | ||
2071 | /* Connection-based need to check for termination and startup */ | 2070 | /* Connection-based need to check for termination and startup */ |
2072 | if (sk->sk_type == SOCK_SEQPACKET) { | 2071 | if (sk->sk_type == SOCK_SEQPACKET) { |
2073 | if (sk->sk_state == TCP_CLOSE) | 2072 | if (sk->sk_state == TCP_CLOSE) |
2074 | mask |= POLLHUP; | 2073 | mask |= POLLHUP; |
2075 | /* connection hasn't started yet? */ | 2074 | /* connection hasn't started yet? */ |
2076 | if (sk->sk_state == TCP_SYN_SENT) | 2075 | if (sk->sk_state == TCP_SYN_SENT) |
2077 | return mask; | 2076 | return mask; |
2078 | } | 2077 | } |
2079 | 2078 | ||
2080 | /* writable? */ | 2079 | /* writable? */ |
2081 | writable = unix_writable(sk); | 2080 | writable = unix_writable(sk); |
2082 | if (writable) { | 2081 | if (writable) { |
2083 | other = unix_peer_get(sk); | 2082 | other = unix_peer_get(sk); |
2084 | if (other) { | 2083 | if (other) { |
2085 | if (unix_peer(other) != sk) { | 2084 | if (unix_peer(other) != sk) { |
2086 | sock_poll_wait(file, &unix_sk(other)->peer_wait, | 2085 | sock_poll_wait(file, &unix_sk(other)->peer_wait, |
2087 | wait); | 2086 | wait); |
2088 | if (unix_recvq_full(other)) | 2087 | if (unix_recvq_full(other)) |
2089 | writable = 0; | 2088 | writable = 0; |
2090 | } | 2089 | } |
2091 | 2090 | ||
2092 | sock_put(other); | 2091 | sock_put(other); |
2093 | } | 2092 | } |
2094 | } | 2093 | } |
2095 | 2094 | ||
2096 | if (writable) | 2095 | if (writable) |
2097 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; | 2096 | mask |= POLLOUT | POLLWRNORM | POLLWRBAND; |
2098 | else | 2097 | else |
2099 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); | 2098 | set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags); |
2100 | 2099 | ||
2101 | return mask; | 2100 | return mask; |
2102 | } | 2101 | } |
2103 | 2102 | ||
2104 | #ifdef CONFIG_PROC_FS | 2103 | #ifdef CONFIG_PROC_FS |
2105 | static struct sock *first_unix_socket(int *i) | 2104 | static struct sock *first_unix_socket(int *i) |
2106 | { | 2105 | { |
2107 | for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) { | 2106 | for (*i = 0; *i <= UNIX_HASH_SIZE; (*i)++) { |
2108 | if (!hlist_empty(&unix_socket_table[*i])) | 2107 | if (!hlist_empty(&unix_socket_table[*i])) |
2109 | return __sk_head(&unix_socket_table[*i]); | 2108 | return __sk_head(&unix_socket_table[*i]); |
2110 | } | 2109 | } |
2111 | return NULL; | 2110 | return NULL; |
2112 | } | 2111 | } |
2113 | 2112 | ||
2114 | static struct sock *next_unix_socket(int *i, struct sock *s) | 2113 | static struct sock *next_unix_socket(int *i, struct sock *s) |
2115 | { | 2114 | { |
2116 | struct sock *next = sk_next(s); | 2115 | struct sock *next = sk_next(s); |
2117 | /* More in this chain? */ | 2116 | /* More in this chain? */ |
2118 | if (next) | 2117 | if (next) |
2119 | return next; | 2118 | return next; |
2120 | /* Look for next non-empty chain. */ | 2119 | /* Look for next non-empty chain. */ |
2121 | for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) { | 2120 | for ((*i)++; *i <= UNIX_HASH_SIZE; (*i)++) { |
2122 | if (!hlist_empty(&unix_socket_table[*i])) | 2121 | if (!hlist_empty(&unix_socket_table[*i])) |
2123 | return __sk_head(&unix_socket_table[*i]); | 2122 | return __sk_head(&unix_socket_table[*i]); |
2124 | } | 2123 | } |
2125 | return NULL; | 2124 | return NULL; |
2126 | } | 2125 | } |
2127 | 2126 | ||
2128 | struct unix_iter_state { | 2127 | struct unix_iter_state { |
2129 | struct seq_net_private p; | 2128 | struct seq_net_private p; |
2130 | int i; | 2129 | int i; |
2131 | }; | 2130 | }; |
2132 | 2131 | ||
2133 | static struct sock *unix_seq_idx(struct seq_file *seq, loff_t pos) | 2132 | static struct sock *unix_seq_idx(struct seq_file *seq, loff_t pos) |
2134 | { | 2133 | { |
2135 | struct unix_iter_state *iter = seq->private; | 2134 | struct unix_iter_state *iter = seq->private; |
2136 | loff_t off = 0; | 2135 | loff_t off = 0; |
2137 | struct sock *s; | 2136 | struct sock *s; |
2138 | 2137 | ||
2139 | for (s = first_unix_socket(&iter->i); s; s = next_unix_socket(&iter->i, s)) { | 2138 | for (s = first_unix_socket(&iter->i); s; s = next_unix_socket(&iter->i, s)) { |
2140 | if (sock_net(s) != seq_file_net(seq)) | 2139 | if (sock_net(s) != seq_file_net(seq)) |
2141 | continue; | 2140 | continue; |
2142 | if (off == pos) | 2141 | if (off == pos) |
2143 | return s; | 2142 | return s; |
2144 | ++off; | 2143 | ++off; |
2145 | } | 2144 | } |
2146 | return NULL; | 2145 | return NULL; |
2147 | } | 2146 | } |
2148 | 2147 | ||
2149 | static void *unix_seq_start(struct seq_file *seq, loff_t *pos) | 2148 | static void *unix_seq_start(struct seq_file *seq, loff_t *pos) |
2150 | __acquires(unix_table_lock) | 2149 | __acquires(unix_table_lock) |
2151 | { | 2150 | { |
2152 | spin_lock(&unix_table_lock); | 2151 | spin_lock(&unix_table_lock); |
2153 | return *pos ? unix_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN; | 2152 | return *pos ? unix_seq_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
2154 | } | 2153 | } |
2155 | 2154 | ||
2156 | static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 2155 | static void *unix_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
2157 | { | 2156 | { |
2158 | struct unix_iter_state *iter = seq->private; | 2157 | struct unix_iter_state *iter = seq->private; |
2159 | struct sock *sk = v; | 2158 | struct sock *sk = v; |
2160 | ++*pos; | 2159 | ++*pos; |
2161 | 2160 | ||
2162 | if (v == SEQ_START_TOKEN) | 2161 | if (v == SEQ_START_TOKEN) |
2163 | sk = first_unix_socket(&iter->i); | 2162 | sk = first_unix_socket(&iter->i); |
2164 | else | 2163 | else |
2165 | sk = next_unix_socket(&iter->i, sk); | 2164 | sk = next_unix_socket(&iter->i, sk); |
2166 | while (sk && (sock_net(sk) != seq_file_net(seq))) | 2165 | while (sk && (sock_net(sk) != seq_file_net(seq))) |
2167 | sk = next_unix_socket(&iter->i, sk); | 2166 | sk = next_unix_socket(&iter->i, sk); |
2168 | return sk; | 2167 | return sk; |
2169 | } | 2168 | } |
2170 | 2169 | ||
2171 | static void unix_seq_stop(struct seq_file *seq, void *v) | 2170 | static void unix_seq_stop(struct seq_file *seq, void *v) |
2172 | __releases(unix_table_lock) | 2171 | __releases(unix_table_lock) |
2173 | { | 2172 | { |
2174 | spin_unlock(&unix_table_lock); | 2173 | spin_unlock(&unix_table_lock); |
2175 | } | 2174 | } |
2176 | 2175 | ||
2177 | static int unix_seq_show(struct seq_file *seq, void *v) | 2176 | static int unix_seq_show(struct seq_file *seq, void *v) |
2178 | { | 2177 | { |
2179 | 2178 | ||
2180 | if (v == SEQ_START_TOKEN) | 2179 | if (v == SEQ_START_TOKEN) |
2181 | seq_puts(seq, "Num RefCount Protocol Flags Type St " | 2180 | seq_puts(seq, "Num RefCount Protocol Flags Type St " |
2182 | "Inode Path\n"); | 2181 | "Inode Path\n"); |
2183 | else { | 2182 | else { |
2184 | struct sock *s = v; | 2183 | struct sock *s = v; |
2185 | struct unix_sock *u = unix_sk(s); | 2184 | struct unix_sock *u = unix_sk(s); |
2186 | unix_state_lock(s); | 2185 | unix_state_lock(s); |
2187 | 2186 | ||
2188 | seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu", | 2187 | seq_printf(seq, "%p: %08X %08X %08X %04X %02X %5lu", |
2189 | s, | 2188 | s, |
2190 | atomic_read(&s->sk_refcnt), | 2189 | atomic_read(&s->sk_refcnt), |
2191 | 0, | 2190 | 0, |
2192 | s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0, | 2191 | s->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0, |
2193 | s->sk_type, | 2192 | s->sk_type, |
2194 | s->sk_socket ? | 2193 | s->sk_socket ? |
2195 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) : | 2194 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) : |
2196 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING), | 2195 | (s->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING), |
2197 | sock_i_ino(s)); | 2196 | sock_i_ino(s)); |
2198 | 2197 | ||
2199 | if (u->addr) { | 2198 | if (u->addr) { |
2200 | int i, len; | 2199 | int i, len; |
2201 | seq_putc(seq, ' '); | 2200 | seq_putc(seq, ' '); |
2202 | 2201 | ||
2203 | i = 0; | 2202 | i = 0; |
2204 | len = u->addr->len - sizeof(short); | 2203 | len = u->addr->len - sizeof(short); |
2205 | if (!UNIX_ABSTRACT(s)) | 2204 | if (!UNIX_ABSTRACT(s)) |
2206 | len--; | 2205 | len--; |
2207 | else { | 2206 | else { |
2208 | seq_putc(seq, '@'); | 2207 | seq_putc(seq, '@'); |
2209 | i++; | 2208 | i++; |
2210 | } | 2209 | } |
2211 | for ( ; i < len; i++) | 2210 | for ( ; i < len; i++) |
2212 | seq_putc(seq, u->addr->name->sun_path[i]); | 2211 | seq_putc(seq, u->addr->name->sun_path[i]); |
2213 | } | 2212 | } |
2214 | unix_state_unlock(s); | 2213 | unix_state_unlock(s); |
2215 | seq_putc(seq, '\n'); | 2214 | seq_putc(seq, '\n'); |
2216 | } | 2215 | } |
2217 | 2216 | ||
2218 | return 0; | 2217 | return 0; |
2219 | } | 2218 | } |
2220 | 2219 | ||
2221 | static const struct seq_operations unix_seq_ops = { | 2220 | static const struct seq_operations unix_seq_ops = { |
2222 | .start = unix_seq_start, | 2221 | .start = unix_seq_start, |
2223 | .next = unix_seq_next, | 2222 | .next = unix_seq_next, |
2224 | .stop = unix_seq_stop, | 2223 | .stop = unix_seq_stop, |
2225 | .show = unix_seq_show, | 2224 | .show = unix_seq_show, |
2226 | }; | 2225 | }; |
2227 | 2226 | ||
2228 | static int unix_seq_open(struct inode *inode, struct file *file) | 2227 | static int unix_seq_open(struct inode *inode, struct file *file) |
2229 | { | 2228 | { |
2230 | return seq_open_net(inode, file, &unix_seq_ops, | 2229 | return seq_open_net(inode, file, &unix_seq_ops, |
2231 | sizeof(struct unix_iter_state)); | 2230 | sizeof(struct unix_iter_state)); |
2232 | } | 2231 | } |
2233 | 2232 | ||
2234 | static const struct file_operations unix_seq_fops = { | 2233 | static const struct file_operations unix_seq_fops = { |
2235 | .owner = THIS_MODULE, | 2234 | .owner = THIS_MODULE, |
2236 | .open = unix_seq_open, | 2235 | .open = unix_seq_open, |
2237 | .read = seq_read, | 2236 | .read = seq_read, |
2238 | .llseek = seq_lseek, | 2237 | .llseek = seq_lseek, |
2239 | .release = seq_release_net, | 2238 | .release = seq_release_net, |
2240 | }; | 2239 | }; |
2241 | 2240 | ||
2242 | #endif | 2241 | #endif |
2243 | 2242 | ||
2244 | static const struct net_proto_family unix_family_ops = { | 2243 | static const struct net_proto_family unix_family_ops = { |
2245 | .family = PF_UNIX, | 2244 | .family = PF_UNIX, |
2246 | .create = unix_create, | 2245 | .create = unix_create, |
2247 | .owner = THIS_MODULE, | 2246 | .owner = THIS_MODULE, |
2248 | }; | 2247 | }; |
2249 | 2248 | ||
2250 | 2249 | ||
2251 | static int __net_init unix_net_init(struct net *net) | 2250 | static int __net_init unix_net_init(struct net *net) |
2252 | { | 2251 | { |
2253 | int error = -ENOMEM; | 2252 | int error = -ENOMEM; |
2254 | 2253 | ||
2255 | net->unx.sysctl_max_dgram_qlen = 10; | 2254 | net->unx.sysctl_max_dgram_qlen = 10; |
2256 | if (unix_sysctl_register(net)) | 2255 | if (unix_sysctl_register(net)) |
2257 | goto out; | 2256 | goto out; |
2258 | 2257 | ||
2259 | #ifdef CONFIG_PROC_FS | 2258 | #ifdef CONFIG_PROC_FS |
2260 | if (!proc_net_fops_create(net, "unix", 0, &unix_seq_fops)) { | 2259 | if (!proc_net_fops_create(net, "unix", 0, &unix_seq_fops)) { |
2261 | unix_sysctl_unregister(net); | 2260 | unix_sysctl_unregister(net); |
2262 | goto out; | 2261 | goto out; |
2263 | } | 2262 | } |
2264 | #endif | 2263 | #endif |
2265 | error = 0; | 2264 | error = 0; |
2266 | out: | 2265 | out: |
2267 | return error; | 2266 | return error; |
2268 | } | 2267 | } |
2269 | 2268 | ||
2270 | static void __net_exit unix_net_exit(struct net *net) | 2269 | static void __net_exit unix_net_exit(struct net *net) |
2271 | { | 2270 | { |
2272 | unix_sysctl_unregister(net); | 2271 | unix_sysctl_unregister(net); |
2273 | proc_net_remove(net, "unix"); | 2272 | proc_net_remove(net, "unix"); |
2274 | } | 2273 | } |
2275 | 2274 | ||
2276 | static struct pernet_operations unix_net_ops = { | 2275 | static struct pernet_operations unix_net_ops = { |
2277 | .init = unix_net_init, | 2276 | .init = unix_net_init, |
2278 | .exit = unix_net_exit, | 2277 | .exit = unix_net_exit, |
2279 | }; | 2278 | }; |
2280 | 2279 | ||
2281 | static int __init af_unix_init(void) | 2280 | static int __init af_unix_init(void) |
2282 | { | 2281 | { |
2283 | int rc = -1; | 2282 | int rc = -1; |
2284 | struct sk_buff *dummy_skb; | 2283 | struct sk_buff *dummy_skb; |
2285 | 2284 | ||
2286 | BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb)); | 2285 | BUILD_BUG_ON(sizeof(struct unix_skb_parms) > sizeof(dummy_skb->cb)); |
2287 | 2286 | ||
2288 | rc = proto_register(&unix_proto, 1); | 2287 | rc = proto_register(&unix_proto, 1); |
2289 | if (rc != 0) { | 2288 | if (rc != 0) { |
2290 | printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n", | 2289 | printk(KERN_CRIT "%s: Cannot create unix_sock SLAB cache!\n", |
2291 | __func__); | 2290 | __func__); |
2292 | goto out; | 2291 | goto out; |
2293 | } | 2292 | } |
2294 | 2293 | ||
2295 | sock_register(&unix_family_ops); | 2294 | sock_register(&unix_family_ops); |
2296 | register_pernet_subsys(&unix_net_ops); | 2295 | register_pernet_subsys(&unix_net_ops); |
2297 | out: | 2296 | out: |
2298 | return rc; | 2297 | return rc; |
2299 | } | 2298 | } |
2300 | 2299 | ||
2301 | static void __exit af_unix_exit(void) | 2300 | static void __exit af_unix_exit(void) |
2302 | { | 2301 | { |
2303 | sock_unregister(PF_UNIX); | 2302 | sock_unregister(PF_UNIX); |
2304 | proto_unregister(&unix_proto); | 2303 | proto_unregister(&unix_proto); |
2305 | unregister_pernet_subsys(&unix_net_ops); | 2304 | unregister_pernet_subsys(&unix_net_ops); |
2306 | } | 2305 | } |
2307 | 2306 | ||
2308 | /* Earlier than device_initcall() so that other drivers invoking | 2307 | /* Earlier than device_initcall() so that other drivers invoking |
2309 | request_module() don't end up in a loop when modprobe tries | 2308 | request_module() don't end up in a loop when modprobe tries |
2310 | to use a UNIX socket. But later than subsys_initcall() because | 2309 | to use a UNIX socket. But later than subsys_initcall() because |
2311 | we depend on stuff initialised there */ | 2310 | we depend on stuff initialised there */ |
2312 | fs_initcall(af_unix_init); | 2311 | fs_initcall(af_unix_init); |
2313 | module_exit(af_unix_exit); | 2312 | module_exit(af_unix_exit); |
2314 | 2313 | ||
2315 | MODULE_LICENSE("GPL"); | 2314 | MODULE_LICENSE("GPL"); |
2316 | MODULE_ALIAS_NETPROTO(PF_UNIX); | 2315 | MODULE_ALIAS_NETPROTO(PF_UNIX); |
2317 | 2316 |