Commit | Line | Data |
---|---|---|
17926a79 DH |
1 | /* AF_RXRPC implementation |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
9b6d5398 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
17926a79 | 14 | #include <linux/module.h> |
ce6654cf | 15 | #include <linux/kernel.h> |
17926a79 | 16 | #include <linux/net.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
17926a79 | 18 | #include <linux/skbuff.h> |
5f2d9c44 | 19 | #include <linux/random.h> |
17926a79 DH |
20 | #include <linux/poll.h> |
21 | #include <linux/proc_fs.h> | |
76181c13 | 22 | #include <linux/key-type.h> |
457c4cbc | 23 | #include <net/net_namespace.h> |
17926a79 DH |
24 | #include <net/sock.h> |
25 | #include <net/af_rxrpc.h> | |
df844fd4 | 26 | #define CREATE_TRACE_POINTS |
17926a79 DH |
27 | #include "ar-internal.h" |
28 | ||
29 | MODULE_DESCRIPTION("RxRPC network protocol"); | |
30 | MODULE_AUTHOR("Red Hat, Inc."); | |
31 | MODULE_LICENSE("GPL"); | |
32 | MODULE_ALIAS_NETPROTO(PF_RXRPC); | |
33 | ||
95c96174 | 34 | unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO; |
17926a79 | 35 | module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO); |
424b00e2 | 36 | MODULE_PARM_DESC(debug, "RxRPC debugging mask"); |
17926a79 | 37 | |
17926a79 DH |
38 | static struct proto rxrpc_proto; |
39 | static const struct proto_ops rxrpc_rpc_ops; | |
40 | ||
41 | /* local epoch for detecting local-end reset */ | |
0d12f8a4 | 42 | u32 rxrpc_epoch; |
17926a79 DH |
43 | |
44 | /* current debugging ID */ | |
45 | atomic_t rxrpc_debug_id; | |
46 | ||
47 | /* count of skbs currently in use */ | |
48 | atomic_t rxrpc_n_skbs; | |
49 | ||
651350d1 DH |
50 | struct workqueue_struct *rxrpc_workqueue; |
51 | ||
17926a79 DH |
52 | static void rxrpc_sock_destructor(struct sock *); |
53 | ||
54 | /* | |
55 | * see if an RxRPC socket is currently writable | |
56 | */ | |
57 | static inline int rxrpc_writable(struct sock *sk) | |
58 | { | |
59 | return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf; | |
60 | } | |
61 | ||
62 | /* | |
63 | * wait for write bufferage to become available | |
64 | */ | |
65 | static void rxrpc_write_space(struct sock *sk) | |
66 | { | |
67 | _enter("%p", sk); | |
43815482 | 68 | rcu_read_lock(); |
17926a79 | 69 | if (rxrpc_writable(sk)) { |
43815482 ED |
70 | struct socket_wq *wq = rcu_dereference(sk->sk_wq); |
71 | ||
1ce0bf50 | 72 | if (skwq_has_sleeper(wq)) |
43815482 | 73 | wake_up_interruptible(&wq->wait); |
8d8ad9d7 | 74 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
17926a79 | 75 | } |
43815482 | 76 | rcu_read_unlock(); |
17926a79 DH |
77 | } |
78 | ||
79 | /* | |
80 | * validate an RxRPC address | |
81 | */ | |
82 | static int rxrpc_validate_address(struct rxrpc_sock *rx, | |
83 | struct sockaddr_rxrpc *srx, | |
84 | int len) | |
85 | { | |
dad8aff7 | 86 | unsigned int tail; |
ab802ee0 | 87 | |
17926a79 DH |
88 | if (len < sizeof(struct sockaddr_rxrpc)) |
89 | return -EINVAL; | |
90 | ||
91 | if (srx->srx_family != AF_RXRPC) | |
92 | return -EAFNOSUPPORT; | |
93 | ||
94 | if (srx->transport_type != SOCK_DGRAM) | |
95 | return -ESOCKTNOSUPPORT; | |
96 | ||
97 | len -= offsetof(struct sockaddr_rxrpc, transport); | |
98 | if (srx->transport_len < sizeof(sa_family_t) || | |
99 | srx->transport_len > len) | |
100 | return -EINVAL; | |
101 | ||
19ffa01c | 102 | if (srx->transport.family != rx->family) |
17926a79 DH |
103 | return -EAFNOSUPPORT; |
104 | ||
105 | switch (srx->transport.family) { | |
106 | case AF_INET: | |
4f95dd78 DH |
107 | if (srx->transport_len < sizeof(struct sockaddr_in)) |
108 | return -EINVAL; | |
21454aaa | 109 | _debug("INET: %x @ %pI4", |
17926a79 | 110 | ntohs(srx->transport.sin.sin_port), |
21454aaa | 111 | &srx->transport.sin.sin_addr); |
ab802ee0 | 112 | tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad); |
17926a79 DH |
113 | break; |
114 | ||
115 | case AF_INET6: | |
116 | default: | |
117 | return -EAFNOSUPPORT; | |
118 | } | |
119 | ||
ab802ee0 DH |
120 | if (tail < len) |
121 | memset((void *)srx + tail, 0, len - tail); | |
17926a79 DH |
122 | return 0; |
123 | } | |
124 | ||
125 | /* | |
126 | * bind a local address to an RxRPC socket | |
127 | */ | |
128 | static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len) | |
129 | { | |
b4f1342f | 130 | struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr; |
17926a79 DH |
131 | struct sock *sk = sock->sk; |
132 | struct rxrpc_local *local; | |
133 | struct rxrpc_sock *rx = rxrpc_sk(sk), *prx; | |
17926a79 DH |
134 | int ret; |
135 | ||
136 | _enter("%p,%p,%d", rx, saddr, len); | |
137 | ||
138 | ret = rxrpc_validate_address(rx, srx, len); | |
139 | if (ret < 0) | |
140 | goto error; | |
141 | ||
142 | lock_sock(&rx->sk); | |
143 | ||
2341e077 | 144 | if (rx->sk.sk_state != RXRPC_UNBOUND) { |
17926a79 DH |
145 | ret = -EINVAL; |
146 | goto error_unlock; | |
147 | } | |
148 | ||
149 | memcpy(&rx->srx, srx, sizeof(rx->srx)); | |
150 | ||
17926a79 DH |
151 | local = rxrpc_lookup_local(&rx->srx); |
152 | if (IS_ERR(local)) { | |
153 | ret = PTR_ERR(local); | |
154 | goto error_unlock; | |
155 | } | |
156 | ||
2341e077 | 157 | if (rx->srx.srx_service) { |
17926a79 | 158 | write_lock_bh(&local->services_lock); |
de8d6c74 | 159 | hlist_for_each_entry(prx, &local->services, listen_link) { |
2341e077 | 160 | if (prx->srx.srx_service == rx->srx.srx_service) |
17926a79 DH |
161 | goto service_in_use; |
162 | } | |
163 | ||
2341e077 | 164 | rx->local = local; |
de8d6c74 | 165 | hlist_add_head_rcu(&rx->listen_link, &local->services); |
17926a79 DH |
166 | write_unlock_bh(&local->services_lock); |
167 | ||
168 | rx->sk.sk_state = RXRPC_SERVER_BOUND; | |
169 | } else { | |
2341e077 | 170 | rx->local = local; |
17926a79 DH |
171 | rx->sk.sk_state = RXRPC_CLIENT_BOUND; |
172 | } | |
173 | ||
174 | release_sock(&rx->sk); | |
175 | _leave(" = 0"); | |
176 | return 0; | |
177 | ||
178 | service_in_use: | |
17926a79 | 179 | write_unlock_bh(&local->services_lock); |
2341e077 DH |
180 | rxrpc_put_local(local); |
181 | ret = -EADDRINUSE; | |
17926a79 DH |
182 | error_unlock: |
183 | release_sock(&rx->sk); | |
184 | error: | |
185 | _leave(" = %d", ret); | |
186 | return ret; | |
187 | } | |
188 | ||
189 | /* | |
190 | * set the number of pending calls permitted on a listening socket | |
191 | */ | |
192 | static int rxrpc_listen(struct socket *sock, int backlog) | |
193 | { | |
194 | struct sock *sk = sock->sk; | |
195 | struct rxrpc_sock *rx = rxrpc_sk(sk); | |
0e119b41 | 196 | unsigned int max; |
17926a79 DH |
197 | int ret; |
198 | ||
199 | _enter("%p,%d", rx, backlog); | |
200 | ||
201 | lock_sock(&rx->sk); | |
202 | ||
203 | switch (rx->sk.sk_state) { | |
2341e077 | 204 | case RXRPC_UNBOUND: |
17926a79 DH |
205 | ret = -EADDRNOTAVAIL; |
206 | break; | |
17926a79 DH |
207 | case RXRPC_SERVER_BOUND: |
208 | ASSERT(rx->local != NULL); | |
0e119b41 DH |
209 | max = READ_ONCE(rxrpc_max_backlog); |
210 | ret = -EINVAL; | |
211 | if (backlog == INT_MAX) | |
212 | backlog = max; | |
213 | else if (backlog < 0 || backlog > max) | |
214 | break; | |
17926a79 DH |
215 | sk->sk_max_ack_backlog = backlog; |
216 | rx->sk.sk_state = RXRPC_SERVER_LISTENING; | |
217 | ret = 0; | |
218 | break; | |
0e119b41 DH |
219 | default: |
220 | ret = -EBUSY; | |
221 | break; | |
17926a79 DH |
222 | } |
223 | ||
224 | release_sock(&rx->sk); | |
225 | _leave(" = %d", ret); | |
226 | return ret; | |
227 | } | |
228 | ||
651350d1 DH |
229 | /** |
230 | * rxrpc_kernel_begin_call - Allow a kernel service to begin a call | |
231 | * @sock: The socket on which to make the call | |
2341e077 | 232 | * @srx: The address of the peer to contact |
651350d1 DH |
233 | * @key: The security context to use (defaults to socket setting) |
234 | * @user_call_ID: The ID to use | |
d001648e DH |
235 | * @gfp: The allocation constraints |
236 | * @notify_rx: Where to send notifications instead of socket queue | |
651350d1 DH |
237 | * |
238 | * Allow a kernel service to begin a call on the nominated socket. This just | |
239 | * sets up all the internal tracking structures and allocates connection and | |
240 | * call IDs as appropriate. The call to be used is returned. | |
241 | * | |
242 | * The default socket destination address and security may be overridden by | |
243 | * supplying @srx and @key. | |
244 | */ | |
245 | struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock, | |
246 | struct sockaddr_rxrpc *srx, | |
247 | struct key *key, | |
248 | unsigned long user_call_ID, | |
d001648e DH |
249 | gfp_t gfp, |
250 | rxrpc_notify_rx_t notify_rx) | |
651350d1 | 251 | { |
19ffa01c | 252 | struct rxrpc_conn_parameters cp; |
651350d1 DH |
253 | struct rxrpc_call *call; |
254 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); | |
f4552c2d | 255 | int ret; |
651350d1 DH |
256 | |
257 | _enter(",,%x,%lx", key_serial(key), user_call_ID); | |
258 | ||
f4552c2d DH |
259 | ret = rxrpc_validate_address(rx, srx, sizeof(*srx)); |
260 | if (ret < 0) | |
261 | return ERR_PTR(ret); | |
262 | ||
651350d1 DH |
263 | lock_sock(&rx->sk); |
264 | ||
19ffa01c DH |
265 | if (!key) |
266 | key = rx->key; | |
267 | if (key && !key->payload.data[0]) | |
268 | key = NULL; /* a no-security key */ | |
269 | ||
270 | memset(&cp, 0, sizeof(cp)); | |
271 | cp.local = rx->local; | |
272 | cp.key = key; | |
273 | cp.security_level = 0; | |
274 | cp.exclusive = false; | |
275 | cp.service_id = srx->srx_service; | |
aa390bbe | 276 | call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp); |
d001648e DH |
277 | if (!IS_ERR(call)) |
278 | call->notify_rx = notify_rx; | |
19ffa01c | 279 | |
651350d1 DH |
280 | release_sock(&rx->sk); |
281 | _leave(" = %p", call); | |
282 | return call; | |
283 | } | |
651350d1 DH |
284 | EXPORT_SYMBOL(rxrpc_kernel_begin_call); |
285 | ||
286 | /** | |
287 | * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using | |
4de48af6 | 288 | * @sock: The socket the call is on |
651350d1 DH |
289 | * @call: The call to end |
290 | * | |
291 | * Allow a kernel service to end a call it was using. The call must be | |
292 | * complete before this is called (the call should be aborted if necessary). | |
293 | */ | |
4de48af6 | 294 | void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call) |
651350d1 DH |
295 | { |
296 | _enter("%d{%d}", call->debug_id, atomic_read(&call->usage)); | |
8d94aa38 | 297 | rxrpc_release_call(rxrpc_sk(sock->sk), call); |
fff72429 | 298 | rxrpc_put_call(call, rxrpc_call_put); |
651350d1 | 299 | } |
651350d1 DH |
300 | EXPORT_SYMBOL(rxrpc_kernel_end_call); |
301 | ||
302 | /** | |
d001648e | 303 | * rxrpc_kernel_new_call_notification - Get notifications of new calls |
651350d1 | 304 | * @sock: The socket to intercept received messages on |
d001648e | 305 | * @notify_new_call: Function to be called when new calls appear |
651350d1 | 306 | * |
d001648e | 307 | * Allow a kernel service to be given notifications about new calls. |
651350d1 | 308 | */ |
d001648e DH |
309 | void rxrpc_kernel_new_call_notification( |
310 | struct socket *sock, | |
311 | rxrpc_notify_new_call_t notify_new_call) | |
651350d1 DH |
312 | { |
313 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); | |
314 | ||
d001648e | 315 | rx->notify_new_call = notify_new_call; |
651350d1 | 316 | } |
d001648e | 317 | EXPORT_SYMBOL(rxrpc_kernel_new_call_notification); |
651350d1 | 318 | |
17926a79 DH |
319 | /* |
320 | * connect an RxRPC socket | |
321 | * - this just targets it at a specific destination; no actual connection | |
322 | * negotiation takes place | |
323 | */ | |
324 | static int rxrpc_connect(struct socket *sock, struct sockaddr *addr, | |
325 | int addr_len, int flags) | |
326 | { | |
2341e077 DH |
327 | struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr; |
328 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); | |
17926a79 DH |
329 | int ret; |
330 | ||
331 | _enter("%p,%p,%d,%d", rx, addr, addr_len, flags); | |
332 | ||
333 | ret = rxrpc_validate_address(rx, srx, addr_len); | |
334 | if (ret < 0) { | |
335 | _leave(" = %d [bad addr]", ret); | |
336 | return ret; | |
337 | } | |
338 | ||
339 | lock_sock(&rx->sk); | |
340 | ||
2341e077 DH |
341 | ret = -EISCONN; |
342 | if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) | |
343 | goto error; | |
344 | ||
17926a79 | 345 | switch (rx->sk.sk_state) { |
2341e077 DH |
346 | case RXRPC_UNBOUND: |
347 | rx->sk.sk_state = RXRPC_CLIENT_UNBOUND; | |
348 | case RXRPC_CLIENT_UNBOUND: | |
17926a79 DH |
349 | case RXRPC_CLIENT_BOUND: |
350 | break; | |
17926a79 | 351 | default: |
2341e077 DH |
352 | ret = -EBUSY; |
353 | goto error; | |
17926a79 DH |
354 | } |
355 | ||
2341e077 DH |
356 | rx->connect_srx = *srx; |
357 | set_bit(RXRPC_SOCK_CONNECTED, &rx->flags); | |
358 | ret = 0; | |
17926a79 | 359 | |
2341e077 | 360 | error: |
17926a79 | 361 | release_sock(&rx->sk); |
2341e077 | 362 | return ret; |
17926a79 DH |
363 | } |
364 | ||
365 | /* | |
366 | * send a message through an RxRPC socket | |
367 | * - in a client this does a number of things: | |
368 | * - finds/sets up a connection for the security specified (if any) | |
369 | * - initiates a call (ID in control data) | |
370 | * - ends the request phase of a call (if MSG_MORE is not set) | |
371 | * - sends a call data packet | |
372 | * - may send an abort (abort code in control data) | |
373 | */ | |
1b784140 | 374 | static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len) |
17926a79 | 375 | { |
2341e077 | 376 | struct rxrpc_local *local; |
17926a79 DH |
377 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
378 | int ret; | |
379 | ||
380 | _enter(",{%d},,%zu", rx->sk.sk_state, len); | |
381 | ||
382 | if (m->msg_flags & MSG_OOB) | |
383 | return -EOPNOTSUPP; | |
384 | ||
385 | if (m->msg_name) { | |
386 | ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen); | |
387 | if (ret < 0) { | |
388 | _leave(" = %d [bad addr]", ret); | |
389 | return ret; | |
390 | } | |
391 | } | |
392 | ||
17926a79 DH |
393 | lock_sock(&rx->sk); |
394 | ||
17926a79 | 395 | switch (rx->sk.sk_state) { |
2341e077 DH |
396 | case RXRPC_UNBOUND: |
397 | local = rxrpc_lookup_local(&rx->srx); | |
398 | if (IS_ERR(local)) { | |
399 | ret = PTR_ERR(local); | |
400 | goto error_unlock; | |
17926a79 | 401 | } |
2341e077 DH |
402 | |
403 | rx->local = local; | |
404 | rx->sk.sk_state = RXRPC_CLIENT_UNBOUND; | |
405 | /* Fall through */ | |
406 | ||
407 | case RXRPC_CLIENT_UNBOUND: | |
17926a79 | 408 | case RXRPC_CLIENT_BOUND: |
2341e077 DH |
409 | if (!m->msg_name && |
410 | test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) { | |
411 | m->msg_name = &rx->connect_srx; | |
412 | m->msg_namelen = sizeof(rx->connect_srx); | |
17926a79 | 413 | } |
2341e077 DH |
414 | case RXRPC_SERVER_BOUND: |
415 | case RXRPC_SERVER_LISTENING: | |
416 | ret = rxrpc_do_sendmsg(rx, m, len); | |
17926a79 DH |
417 | break; |
418 | default: | |
2341e077 | 419 | ret = -EINVAL; |
17926a79 DH |
420 | break; |
421 | } | |
422 | ||
2341e077 | 423 | error_unlock: |
17926a79 | 424 | release_sock(&rx->sk); |
17926a79 DH |
425 | _leave(" = %d", ret); |
426 | return ret; | |
427 | } | |
428 | ||
429 | /* | |
430 | * set RxRPC socket options | |
431 | */ | |
432 | static int rxrpc_setsockopt(struct socket *sock, int level, int optname, | |
b7058842 | 433 | char __user *optval, unsigned int optlen) |
17926a79 DH |
434 | { |
435 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); | |
95c96174 | 436 | unsigned int min_sec_level; |
17926a79 DH |
437 | int ret; |
438 | ||
439 | _enter(",%d,%d,,%d", level, optname, optlen); | |
440 | ||
441 | lock_sock(&rx->sk); | |
442 | ret = -EOPNOTSUPP; | |
443 | ||
444 | if (level == SOL_RXRPC) { | |
445 | switch (optname) { | |
446 | case RXRPC_EXCLUSIVE_CONNECTION: | |
447 | ret = -EINVAL; | |
448 | if (optlen != 0) | |
449 | goto error; | |
450 | ret = -EISCONN; | |
2341e077 | 451 | if (rx->sk.sk_state != RXRPC_UNBOUND) |
17926a79 | 452 | goto error; |
cc8feb8e | 453 | rx->exclusive = true; |
17926a79 DH |
454 | goto success; |
455 | ||
456 | case RXRPC_SECURITY_KEY: | |
457 | ret = -EINVAL; | |
458 | if (rx->key) | |
459 | goto error; | |
460 | ret = -EISCONN; | |
2341e077 | 461 | if (rx->sk.sk_state != RXRPC_UNBOUND) |
17926a79 DH |
462 | goto error; |
463 | ret = rxrpc_request_key(rx, optval, optlen); | |
464 | goto error; | |
465 | ||
466 | case RXRPC_SECURITY_KEYRING: | |
467 | ret = -EINVAL; | |
468 | if (rx->key) | |
469 | goto error; | |
470 | ret = -EISCONN; | |
2341e077 | 471 | if (rx->sk.sk_state != RXRPC_UNBOUND) |
17926a79 DH |
472 | goto error; |
473 | ret = rxrpc_server_keyring(rx, optval, optlen); | |
474 | goto error; | |
475 | ||
476 | case RXRPC_MIN_SECURITY_LEVEL: | |
477 | ret = -EINVAL; | |
95c96174 | 478 | if (optlen != sizeof(unsigned int)) |
17926a79 DH |
479 | goto error; |
480 | ret = -EISCONN; | |
2341e077 | 481 | if (rx->sk.sk_state != RXRPC_UNBOUND) |
17926a79 DH |
482 | goto error; |
483 | ret = get_user(min_sec_level, | |
95c96174 | 484 | (unsigned int __user *) optval); |
17926a79 DH |
485 | if (ret < 0) |
486 | goto error; | |
487 | ret = -EINVAL; | |
488 | if (min_sec_level > RXRPC_SECURITY_MAX) | |
489 | goto error; | |
490 | rx->min_sec_level = min_sec_level; | |
491 | goto success; | |
492 | ||
493 | default: | |
494 | break; | |
495 | } | |
496 | } | |
497 | ||
498 | success: | |
499 | ret = 0; | |
500 | error: | |
501 | release_sock(&rx->sk); | |
502 | return ret; | |
503 | } | |
504 | ||
505 | /* | |
506 | * permit an RxRPC socket to be polled | |
507 | */ | |
508 | static unsigned int rxrpc_poll(struct file *file, struct socket *sock, | |
509 | poll_table *wait) | |
510 | { | |
511 | unsigned int mask; | |
512 | struct sock *sk = sock->sk; | |
513 | ||
aa395145 | 514 | sock_poll_wait(file, sk_sleep(sk), wait); |
17926a79 DH |
515 | mask = 0; |
516 | ||
517 | /* the socket is readable if there are any messages waiting on the Rx | |
518 | * queue */ | |
519 | if (!skb_queue_empty(&sk->sk_receive_queue)) | |
520 | mask |= POLLIN | POLLRDNORM; | |
521 | ||
522 | /* the socket is writable if there is space to add new data to the | |
523 | * socket; there is no guarantee that any particular call in progress | |
524 | * on the socket may have space in the Tx ACK window */ | |
525 | if (rxrpc_writable(sk)) | |
526 | mask |= POLLOUT | POLLWRNORM; | |
527 | ||
528 | return mask; | |
529 | } | |
530 | ||
531 | /* | |
532 | * create an RxRPC socket | |
533 | */ | |
3f378b68 EP |
534 | static int rxrpc_create(struct net *net, struct socket *sock, int protocol, |
535 | int kern) | |
17926a79 DH |
536 | { |
537 | struct rxrpc_sock *rx; | |
538 | struct sock *sk; | |
539 | ||
540 | _enter("%p,%d", sock, protocol); | |
541 | ||
09ad9bc7 | 542 | if (!net_eq(net, &init_net)) |
1b8d7ae4 EB |
543 | return -EAFNOSUPPORT; |
544 | ||
b4f1342f | 545 | /* we support transport protocol UDP/UDP6 only */ |
17926a79 DH |
546 | if (protocol != PF_INET) |
547 | return -EPROTONOSUPPORT; | |
548 | ||
549 | if (sock->type != SOCK_DGRAM) | |
550 | return -ESOCKTNOSUPPORT; | |
551 | ||
552 | sock->ops = &rxrpc_rpc_ops; | |
553 | sock->state = SS_UNCONNECTED; | |
554 | ||
11aa9c28 | 555 | sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern); |
17926a79 DH |
556 | if (!sk) |
557 | return -ENOMEM; | |
558 | ||
559 | sock_init_data(sock, sk); | |
8d94aa38 | 560 | sock_set_flag(sk, SOCK_RCU_FREE); |
2341e077 | 561 | sk->sk_state = RXRPC_UNBOUND; |
17926a79 | 562 | sk->sk_write_space = rxrpc_write_space; |
0e119b41 | 563 | sk->sk_max_ack_backlog = 0; |
17926a79 DH |
564 | sk->sk_destruct = rxrpc_sock_destructor; |
565 | ||
566 | rx = rxrpc_sk(sk); | |
19ffa01c | 567 | rx->family = protocol; |
17926a79 DH |
568 | rx->calls = RB_ROOT; |
569 | ||
de8d6c74 | 570 | INIT_HLIST_NODE(&rx->listen_link); |
17926a79 DH |
571 | INIT_LIST_HEAD(&rx->secureq); |
572 | INIT_LIST_HEAD(&rx->acceptq); | |
573 | rwlock_init(&rx->call_lock); | |
574 | memset(&rx->srx, 0, sizeof(rx->srx)); | |
575 | ||
576 | _leave(" = 0 [%p]", rx); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | /* | |
581 | * RxRPC socket destructor | |
582 | */ | |
583 | static void rxrpc_sock_destructor(struct sock *sk) | |
584 | { | |
585 | _enter("%p", sk); | |
586 | ||
587 | rxrpc_purge_queue(&sk->sk_receive_queue); | |
588 | ||
547b792c IJ |
589 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
590 | WARN_ON(!sk_unhashed(sk)); | |
591 | WARN_ON(sk->sk_socket); | |
17926a79 DH |
592 | |
593 | if (!sock_flag(sk, SOCK_DEAD)) { | |
594 | printk("Attempt to release alive rxrpc socket: %p\n", sk); | |
595 | return; | |
596 | } | |
597 | } | |
598 | ||
599 | /* | |
600 | * release an RxRPC socket | |
601 | */ | |
602 | static int rxrpc_release_sock(struct sock *sk) | |
603 | { | |
604 | struct rxrpc_sock *rx = rxrpc_sk(sk); | |
605 | ||
606 | _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt)); | |
607 | ||
608 | /* declare the socket closed for business */ | |
609 | sock_orphan(sk); | |
610 | sk->sk_shutdown = SHUTDOWN_MASK; | |
611 | ||
612 | spin_lock_bh(&sk->sk_receive_queue.lock); | |
613 | sk->sk_state = RXRPC_CLOSE; | |
614 | spin_unlock_bh(&sk->sk_receive_queue.lock); | |
615 | ||
616 | ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1); | |
617 | ||
de8d6c74 | 618 | if (!hlist_unhashed(&rx->listen_link)) { |
17926a79 | 619 | write_lock_bh(&rx->local->services_lock); |
de8d6c74 | 620 | hlist_del_rcu(&rx->listen_link); |
17926a79 DH |
621 | write_unlock_bh(&rx->local->services_lock); |
622 | } | |
623 | ||
624 | /* try to flush out this socket */ | |
625 | rxrpc_release_calls_on_socket(rx); | |
651350d1 | 626 | flush_workqueue(rxrpc_workqueue); |
17926a79 DH |
627 | rxrpc_purge_queue(&sk->sk_receive_queue); |
628 | ||
5627cc8b DH |
629 | rxrpc_put_local(rx->local); |
630 | rx->local = NULL; | |
17926a79 DH |
631 | key_put(rx->key); |
632 | rx->key = NULL; | |
633 | key_put(rx->securities); | |
634 | rx->securities = NULL; | |
635 | sock_put(sk); | |
636 | ||
637 | _leave(" = 0"); | |
638 | return 0; | |
639 | } | |
640 | ||
641 | /* | |
642 | * release an RxRPC BSD socket on close() or equivalent | |
643 | */ | |
644 | static int rxrpc_release(struct socket *sock) | |
645 | { | |
646 | struct sock *sk = sock->sk; | |
647 | ||
648 | _enter("%p{%p}", sock, sk); | |
649 | ||
650 | if (!sk) | |
651 | return 0; | |
652 | ||
653 | sock->sk = NULL; | |
654 | ||
655 | return rxrpc_release_sock(sk); | |
656 | } | |
657 | ||
658 | /* | |
659 | * RxRPC network protocol | |
660 | */ | |
661 | static const struct proto_ops rxrpc_rpc_ops = { | |
e33b3d97 | 662 | .family = PF_RXRPC, |
17926a79 DH |
663 | .owner = THIS_MODULE, |
664 | .release = rxrpc_release, | |
665 | .bind = rxrpc_bind, | |
666 | .connect = rxrpc_connect, | |
667 | .socketpair = sock_no_socketpair, | |
668 | .accept = sock_no_accept, | |
669 | .getname = sock_no_getname, | |
670 | .poll = rxrpc_poll, | |
671 | .ioctl = sock_no_ioctl, | |
672 | .listen = rxrpc_listen, | |
673 | .shutdown = sock_no_shutdown, | |
674 | .setsockopt = rxrpc_setsockopt, | |
675 | .getsockopt = sock_no_getsockopt, | |
676 | .sendmsg = rxrpc_sendmsg, | |
677 | .recvmsg = rxrpc_recvmsg, | |
678 | .mmap = sock_no_mmap, | |
679 | .sendpage = sock_no_sendpage, | |
680 | }; | |
681 | ||
682 | static struct proto rxrpc_proto = { | |
683 | .name = "RXRPC", | |
684 | .owner = THIS_MODULE, | |
685 | .obj_size = sizeof(struct rxrpc_sock), | |
0d12f8a4 | 686 | .max_header = sizeof(struct rxrpc_wire_header), |
17926a79 DH |
687 | }; |
688 | ||
ec1b4cf7 | 689 | static const struct net_proto_family rxrpc_family_ops = { |
17926a79 DH |
690 | .family = PF_RXRPC, |
691 | .create = rxrpc_create, | |
692 | .owner = THIS_MODULE, | |
693 | }; | |
694 | ||
695 | /* | |
696 | * initialise and register the RxRPC protocol | |
697 | */ | |
698 | static int __init af_rxrpc_init(void) | |
699 | { | |
17926a79 DH |
700 | int ret = -1; |
701 | ||
ce6654cf | 702 | BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb)); |
17926a79 | 703 | |
5f2d9c44 DH |
704 | get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch)); |
705 | rxrpc_epoch |= RXRPC_RANDOM_EPOCH; | |
706 | get_random_bytes(&rxrpc_client_conn_ids.cur, | |
707 | sizeof(rxrpc_client_conn_ids.cur)); | |
708 | rxrpc_client_conn_ids.cur &= 0x3fffffff; | |
709 | if (rxrpc_client_conn_ids.cur == 0) | |
710 | rxrpc_client_conn_ids.cur = 1; | |
17926a79 | 711 | |
651350d1 | 712 | ret = -ENOMEM; |
17926a79 DH |
713 | rxrpc_call_jar = kmem_cache_create( |
714 | "rxrpc_call_jar", sizeof(struct rxrpc_call), 0, | |
20c2df83 | 715 | SLAB_HWCACHE_ALIGN, NULL); |
17926a79 | 716 | if (!rxrpc_call_jar) { |
9b6d5398 | 717 | pr_notice("Failed to allocate call jar\n"); |
17926a79 DH |
718 | goto error_call_jar; |
719 | } | |
720 | ||
e1fcc7e2 | 721 | rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1); |
651350d1 | 722 | if (!rxrpc_workqueue) { |
9b6d5398 | 723 | pr_notice("Failed to allocate work queue\n"); |
651350d1 DH |
724 | goto error_work_queue; |
725 | } | |
726 | ||
648af7fc DH |
727 | ret = rxrpc_init_security(); |
728 | if (ret < 0) { | |
9b6d5398 | 729 | pr_crit("Cannot initialise security\n"); |
648af7fc DH |
730 | goto error_security; |
731 | } | |
732 | ||
17926a79 | 733 | ret = proto_register(&rxrpc_proto, 1); |
1c899641 | 734 | if (ret < 0) { |
9b6d5398 | 735 | pr_crit("Cannot register protocol\n"); |
17926a79 DH |
736 | goto error_proto; |
737 | } | |
738 | ||
739 | ret = sock_register(&rxrpc_family_ops); | |
740 | if (ret < 0) { | |
9b6d5398 | 741 | pr_crit("Cannot register socket family\n"); |
17926a79 DH |
742 | goto error_sock; |
743 | } | |
744 | ||
745 | ret = register_key_type(&key_type_rxrpc); | |
746 | if (ret < 0) { | |
9b6d5398 | 747 | pr_crit("Cannot register client key type\n"); |
17926a79 DH |
748 | goto error_key_type; |
749 | } | |
750 | ||
751 | ret = register_key_type(&key_type_rxrpc_s); | |
752 | if (ret < 0) { | |
9b6d5398 | 753 | pr_crit("Cannot register server key type\n"); |
17926a79 DH |
754 | goto error_key_type_s; |
755 | } | |
756 | ||
5873c083 DH |
757 | ret = rxrpc_sysctl_init(); |
758 | if (ret < 0) { | |
9b6d5398 | 759 | pr_crit("Cannot register sysctls\n"); |
5873c083 DH |
760 | goto error_sysctls; |
761 | } | |
762 | ||
17926a79 | 763 | #ifdef CONFIG_PROC_FS |
d4beaa66 G |
764 | proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops); |
765 | proc_create("rxrpc_conns", 0, init_net.proc_net, | |
766 | &rxrpc_connection_seq_fops); | |
17926a79 DH |
767 | #endif |
768 | return 0; | |
769 | ||
5873c083 DH |
770 | error_sysctls: |
771 | unregister_key_type(&key_type_rxrpc_s); | |
17926a79 DH |
772 | error_key_type_s: |
773 | unregister_key_type(&key_type_rxrpc); | |
774 | error_key_type: | |
775 | sock_unregister(PF_RXRPC); | |
776 | error_sock: | |
777 | proto_unregister(&rxrpc_proto); | |
778 | error_proto: | |
648af7fc | 779 | rxrpc_exit_security(); |
8addc044 WY |
780 | error_security: |
781 | destroy_workqueue(rxrpc_workqueue); | |
651350d1 | 782 | error_work_queue: |
17926a79 DH |
783 | kmem_cache_destroy(rxrpc_call_jar); |
784 | error_call_jar: | |
785 | return ret; | |
786 | } | |
787 | ||
788 | /* | |
789 | * unregister the RxRPC protocol | |
790 | */ | |
791 | static void __exit af_rxrpc_exit(void) | |
792 | { | |
793 | _enter(""); | |
5873c083 | 794 | rxrpc_sysctl_exit(); |
17926a79 DH |
795 | unregister_key_type(&key_type_rxrpc_s); |
796 | unregister_key_type(&key_type_rxrpc); | |
797 | sock_unregister(PF_RXRPC); | |
798 | proto_unregister(&rxrpc_proto); | |
799 | rxrpc_destroy_all_calls(); | |
800 | rxrpc_destroy_all_connections(); | |
17926a79 | 801 | ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0); |
4f95dd78 DH |
802 | rxrpc_destroy_all_locals(); |
803 | ||
ece31ffd G |
804 | remove_proc_entry("rxrpc_conns", init_net.proc_net); |
805 | remove_proc_entry("rxrpc_calls", init_net.proc_net); | |
651350d1 | 806 | destroy_workqueue(rxrpc_workqueue); |
648af7fc | 807 | rxrpc_exit_security(); |
17926a79 DH |
808 | kmem_cache_destroy(rxrpc_call_jar); |
809 | _leave(""); | |
810 | } | |
811 | ||
812 | module_init(af_rxrpc_init); | |
813 | module_exit(af_rxrpc_exit); |