blob: a976b4d2989259a521a94f91a779cb79a81dada9 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07002/*
3 * net/dccp/proto.c
4 *
5 * An implementation of the DCCP protocol
6 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07007 */
8
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07009#include <linux/dccp.h>
10#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/sched.h>
13#include <linux/kernel.h>
14#include <linux/skbuff.h>
15#include <linux/netdevice.h>
16#include <linux/in.h>
17#include <linux/if_arp.h>
18#include <linux/init.h>
19#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070021#include <net/checksum.h>
22
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020023#include <net/inet_sock.h>
Eric Dumazet120e9da2017-08-16 07:03:15 -070024#include <net/inet_common.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070025#include <net/sock.h>
26#include <net/xfrm.h>
27
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -070028#include <asm/ioctls.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070029#include <linux/spinlock.h>
30#include <linux/timer.h>
31#include <linux/delay.h>
32#include <linux/poll.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070033
34#include "ccid.h"
35#include "dccp.h"
Andrea Bittauafe00252006-03-20 17:43:56 -080036#include "feat.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070037
Masami Hiramatsuee549be2017-12-29 11:47:55 +090038#define CREATE_TRACE_POINTS
39#include "trace.h"
40
Eric Dumazetba899662005-08-26 12:05:31 -070041DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070042
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080043EXPORT_SYMBOL_GPL(dccp_statistics);
44
Eric Dumazet19757ce2021-10-14 06:41:26 -070045DEFINE_PER_CPU(unsigned int, dccp_orphan_count);
46EXPORT_PER_CPU_SYMBOL_GPL(dccp_orphan_count);
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080047
Eric Dumazet5caea4e2008-11-20 00:40:07 -080048struct inet_hashinfo dccp_hashinfo;
Arnaldo Carvalho de Melo075ae862006-03-20 21:24:19 -080049EXPORT_SYMBOL_GPL(dccp_hashinfo);
50
Ian McDonaldb1308dc2006-11-20 18:30:17 -020051/* the maximum queue length for tx in packets. 0 is no limit */
52int sysctl_dccp_tx_qlen __read_mostly = 5;
53
stephen hemminger1f4f0f62010-10-05 04:24:09 +000054#ifdef CONFIG_IP_DCCP_DEBUG
55static const char *dccp_state_name(const int state)
56{
57 static const char *const dccp_state_names[] = {
58 [DCCP_OPEN] = "OPEN",
59 [DCCP_REQUESTING] = "REQUESTING",
60 [DCCP_PARTOPEN] = "PARTOPEN",
61 [DCCP_LISTEN] = "LISTEN",
62 [DCCP_RESPOND] = "RESPOND",
63 [DCCP_CLOSING] = "CLOSING",
64 [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
65 [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
66 [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
67 [DCCP_TIME_WAIT] = "TIME_WAIT",
68 [DCCP_CLOSED] = "CLOSED",
69 };
70
71 if (state >= DCCP_MAX_STATES)
72 return "INVALID STATE!";
73 else
74 return dccp_state_names[state];
75}
76#endif
77
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080078void dccp_set_state(struct sock *sk, const int state)
79{
80 const int oldstate = sk->sk_state;
81
Gerrit Renkerf11135a2007-11-28 11:34:53 -020082 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080083 dccp_state_name(oldstate), dccp_state_name(state));
84 WARN_ON(state == oldstate);
85
86 switch (state) {
87 case DCCP_OPEN:
88 if (oldstate != DCCP_OPEN)
89 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
Gerrit Renker6eb55d12008-12-08 01:15:26 -080090 /* Client retransmits all Confirm options until entering OPEN */
91 if (oldstate == DCCP_PARTOPEN)
92 dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080093 break;
94
95 case DCCP_CLOSED:
Gerrit Renker0c869622007-11-28 11:59:48 -020096 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
97 oldstate == DCCP_CLOSING)
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -080098 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
99
100 sk->sk_prot->unhash(sk);
101 if (inet_csk(sk)->icsk_bind_hash != NULL &&
102 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800103 inet_put_port(sk);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500104 fallthrough;
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800105 default:
106 if (oldstate == DCCP_OPEN)
107 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
108 }
109
110 /* Change state AFTER socket is unhashed to avoid closed
111 * socket sitting in hash tables.
112 */
Yafang Shaob0832e32017-12-20 11:12:53 +0800113 inet_sk_set_state(sk, state);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800114}
115
116EXPORT_SYMBOL_GPL(dccp_set_state);
117
Gerrit Renker0c869622007-11-28 11:59:48 -0200118static void dccp_finish_passive_close(struct sock *sk)
119{
120 switch (sk->sk_state) {
121 case DCCP_PASSIVE_CLOSE:
122 /* Node (client or server) has received Close packet. */
123 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
124 dccp_set_state(sk, DCCP_CLOSED);
125 break;
126 case DCCP_PASSIVE_CLOSEREQ:
127 /*
128 * Client received CloseReq. We set the `active' flag so that
129 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
130 */
131 dccp_send_close(sk, 1);
132 dccp_set_state(sk, DCCP_CLOSING);
133 }
134}
135
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800136void dccp_done(struct sock *sk)
137{
138 dccp_set_state(sk, DCCP_CLOSED);
139 dccp_clear_xmit_timers(sk);
140
141 sk->sk_shutdown = SHUTDOWN_MASK;
142
143 if (!sock_flag(sk, SOCK_DEAD))
144 sk->sk_state_change(sk);
145 else
146 inet_csk_destroy_sock(sk);
147}
148
149EXPORT_SYMBOL_GPL(dccp_done);
150
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700151const char *dccp_packet_name(const int type)
152{
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -0700153 static const char *const dccp_packet_names[] = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700154 [DCCP_PKT_REQUEST] = "REQUEST",
155 [DCCP_PKT_RESPONSE] = "RESPONSE",
156 [DCCP_PKT_DATA] = "DATA",
157 [DCCP_PKT_ACK] = "ACK",
158 [DCCP_PKT_DATAACK] = "DATAACK",
159 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
160 [DCCP_PKT_CLOSE] = "CLOSE",
161 [DCCP_PKT_RESET] = "RESET",
162 [DCCP_PKT_SYNC] = "SYNC",
163 [DCCP_PKT_SYNCACK] = "SYNCACK",
164 };
165
166 if (type >= DCCP_NR_PKT_TYPES)
167 return "INVALID";
168 else
169 return dccp_packet_names[type];
170}
171
172EXPORT_SYMBOL_GPL(dccp_packet_name);
173
Eric Dumazet120e9da2017-08-16 07:03:15 -0700174static void dccp_sk_destruct(struct sock *sk)
175{
176 struct dccp_sock *dp = dccp_sk(sk);
177
178 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
179 dp->dccps_hc_tx_ccid = NULL;
180 inet_sock_destruct(sk);
181}
182
Arnaldo Carvalho de Melo72478872006-03-20 22:00:37 -0800183int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800184{
185 struct dccp_sock *dp = dccp_sk(sk);
186 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800187
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200188 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
189 icsk->icsk_syn_retries = sysctl_dccp_request_retries;
190 sk->sk_state = DCCP_CLOSED;
191 sk->sk_write_space = dccp_write_space;
Eric Dumazet120e9da2017-08-16 07:03:15 -0700192 sk->sk_destruct = dccp_sk_destruct;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200193 icsk->icsk_sync_mss = dccp_sync_mss;
Gerrit Renker410e27a2008-09-09 13:27:22 +0200194 dp->dccps_mss_cache = 536;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200195 dp->dccps_rate_last = jiffies;
196 dp->dccps_role = DCCP_ROLE_UNDEFINED;
197 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100198 dp->dccps_tx_qlen = sysctl_dccp_tx_qlen;
Arnaldo Carvalho de Meloe18d7a92007-11-24 21:42:53 -0200199
200 dccp_init_xmit_timers(sk);
201
Gerrit Renkerac757732008-11-04 23:55:49 -0800202 INIT_LIST_HEAD(&dp->dccps_featneg);
Gerrit Renker6eb55d12008-12-08 01:15:26 -0800203 /* control socket doesn't need feat nego */
204 if (likely(ctl_sock_initialized))
205 return dccp_feat_init(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800206 return 0;
207}
208
209EXPORT_SYMBOL_GPL(dccp_init_sock);
210
Brian Haley7d06b2e2008-06-14 17:04:49 -0700211void dccp_destroy_sock(struct sock *sk)
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800212{
213 struct dccp_sock *dp = dccp_sk(sk);
214
Eric Dumazet7749d4f2017-08-14 14:10:25 -0700215 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800216 if (sk->sk_send_head != NULL) {
217 kfree_skb(sk->sk_send_head);
218 sk->sk_send_head = NULL;
219 }
220
221 /* Clean up a referenced DCCP bind bucket. */
222 if (inet_csk(sk)->icsk_bind_hash != NULL)
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -0800223 inet_put_port(sk);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800224
225 kfree(dp->dccps_service_list);
226 dp->dccps_service_list = NULL;
227
Gerrit Renker6fdd34d2008-12-08 01:19:06 -0800228 if (dp->dccps_hc_rx_ackvec != NULL) {
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800229 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
230 dp->dccps_hc_rx_ackvec = NULL;
231 }
232 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
Eric Dumazet120e9da2017-08-16 07:03:15 -0700233 dp->dccps_hc_rx_ccid = NULL;
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800234
235 /* clean up feature negotiation state */
Gerrit Renkerd99a7bd2008-11-04 23:56:30 -0800236 dccp_feat_list_purge(&dp->dccps_featneg);
Arnaldo Carvalho de Melo3e0fadc2006-03-20 21:23:15 -0800237}
238
239EXPORT_SYMBOL_GPL(dccp_destroy_sock);
240
Gerrit Renkerce865a62007-11-24 22:14:15 -0200241static inline int dccp_need_reset(int state)
242{
243 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
244 state != DCCP_REQUESTING;
245}
246
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700247int dccp_disconnect(struct sock *sk, int flags)
248{
249 struct inet_connection_sock *icsk = inet_csk(sk);
250 struct inet_sock *inet = inet_sk(sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000251 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700252 const int old_state = sk->sk_state;
253
254 if (old_state != DCCP_CLOSED)
255 dccp_set_state(sk, DCCP_CLOSED);
256
Gerrit Renkerce865a62007-11-24 22:14:15 -0200257 /*
258 * This corresponds to the ABORT function of RFC793, sec. 3.8
259 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
260 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700261 if (old_state == DCCP_LISTEN) {
262 inet_csk_listen_stop(sk);
Gerrit Renkerce865a62007-11-24 22:14:15 -0200263 } else if (dccp_need_reset(old_state)) {
264 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
265 sk->sk_err = ECONNRESET;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700266 } else if (old_state == DCCP_REQUESTING)
267 sk->sk_err = ECONNRESET;
268
269 dccp_clear_xmit_timers(sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000270 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000271 dp->dccps_hc_rx_ccid = NULL;
Gerrit Renker48816322008-08-23 13:28:27 +0200272
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700273 __skb_queue_purge(&sk->sk_receive_queue);
Gerrit Renker48816322008-08-23 13:28:27 +0200274 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700275 if (sk->sk_send_head != NULL) {
276 __kfree_skb(sk->sk_send_head);
277 sk->sk_send_head = NULL;
278 }
279
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000280 inet->inet_dport = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700281
282 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
283 inet_reset_saddr(sk);
284
285 sk->sk_shutdown = 0;
286 sock_reset_flag(sk, SOCK_DONE);
287
288 icsk->icsk_backoff = 0;
289 inet_csk_delack_init(sk);
290 __sk_dst_reset(sk);
291
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000292 WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700293
Alexander Aringe3ae2362021-06-27 18:48:21 -0400294 sk_error_report(sk);
Hariprasad Kelam3285a9a2019-05-12 16:09:49 +0530295 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700296}
297
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800298EXPORT_SYMBOL_GPL(dccp_disconnect);
299
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700300/*
301 * Wait for a DCCP event.
302 *
303 * Note that we don't need to lock the socket, as the upper poll layers
304 * take care of normal races (between the test and the event) and we don't
305 * go look at any of the socket buffers directly.
306 */
307__poll_t dccp_poll(struct file *file, struct socket *sock,
308 poll_table *wait)
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700309{
Al Viroade994f2017-07-03 00:01:49 -0400310 __poll_t mask;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700311 struct sock *sk = sock->sk;
312
Karsten Graul89ab0662018-10-23 13:40:39 +0200313 sock_poll_wait(file, sock, wait);
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700314 if (sk->sk_state == DCCP_LISTEN)
315 return inet_csk_listen_poll(sk);
316
317 /* Socket is not locked. We are protected from async events
318 by poll logic and correct handling of state changes
319 made by another threads is impossible in any case.
320 */
321
322 mask = 0;
323 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800324 mask = EPOLLERR;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700325
326 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800327 mask |= EPOLLHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700328 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800329 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700330
331 /* Connected? */
332 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
333 if (atomic_read(&sk->sk_rmem_alloc) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800334 mask |= EPOLLIN | EPOLLRDNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700335
336 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700337 if (sk_stream_is_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800338 mask |= EPOLLOUT | EPOLLWRNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700339 } else { /* send SIGIO later */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800340 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700341 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
342
343 /* Race breaker. If space is freed after
344 * wspace test but before the flags are set,
345 * IO signal will be lost.
346 */
Eric Dumazet64dc6132013-07-22 20:26:31 -0700347 if (sk_stream_is_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800348 mask |= EPOLLOUT | EPOLLWRNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700349 }
350 }
351 }
352 return mask;
353}
354
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700355EXPORT_SYMBOL_GPL(dccp_poll);
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800356
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700357int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
358{
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700359 int rc = -ENOTCONN;
360
361 lock_sock(sk);
362
363 if (sk->sk_state == DCCP_LISTEN)
364 goto out;
365
366 switch (cmd) {
Richard Sailer749c08f2020-07-20 18:06:14 +0200367 case SIOCOUTQ: {
368 int amount = sk_wmem_alloc_get(sk);
369 /* Using sk_wmem_alloc here because sk_wmem_queued is not used by DCCP and
370 * always 0, comparably to UDP.
371 */
372
373 rc = put_user(amount, (int __user *)arg);
374 }
375 break;
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700376 case SIOCINQ: {
377 struct sk_buff *skb;
378 unsigned long amount = 0;
379
380 skb = skb_peek(&sk->sk_receive_queue);
381 if (skb != NULL) {
382 /*
383 * We will only return the amount of this packet since
384 * that is all that will be read.
385 */
386 amount = skb->len;
387 }
388 rc = put_user(amount, (int __user *)arg);
389 }
390 break;
391 default:
392 rc = -ENOIOCTLCMD;
393 break;
394 }
395out:
396 release_sock(sk);
397 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700398}
399
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800400EXPORT_SYMBOL_GPL(dccp_ioctl);
401
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800402static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200403 sockptr_t optval, unsigned int optlen)
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700404{
405 struct dccp_sock *dp = dccp_sk(sk);
406 struct dccp_service_list *sl = NULL;
407
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200408 if (service == DCCP_SERVICE_INVALID_VALUE ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700409 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
410 return -EINVAL;
411
412 if (optlen > sizeof(service)) {
413 sl = kmalloc(optlen, GFP_KERNEL);
414 if (sl == NULL)
415 return -ENOMEM;
416
417 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
Christoph Hellwigd3c48152020-07-28 18:38:35 +0200418 if (copy_from_sockptr_offset(sl->dccpsl_list, optval,
419 sizeof(service), optlen - sizeof(service)) ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700420 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
421 kfree(sl);
422 return -EFAULT;
423 }
424 }
425
426 lock_sock(sk);
427 dp->dccps_service = service;
428
Jesper Juhla51482b2005-11-08 09:41:34 -0800429 kfree(dp->dccps_service_list);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700430
431 dp->dccps_service_list = sl;
432 release_sock(sk);
433 return 0;
434}
435
Gerrit Renker29450552008-11-16 22:53:48 -0800436static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
437{
438 u8 *list, len;
439 int i, rc;
440
441 if (cscov < 0 || cscov > 15)
442 return -EINVAL;
443 /*
444 * Populate a list of permissible values, in the range cscov...15. This
445 * is necessary since feature negotiation of single values only works if
446 * both sides incidentally choose the same value. Since the list starts
447 * lowest-value first, negotiation will pick the smallest shared value.
448 */
449 if (cscov == 0)
450 return 0;
451 len = 16 - cscov;
452
453 list = kmalloc(len, GFP_KERNEL);
454 if (list == NULL)
455 return -ENOBUFS;
456
457 for (i = 0; i < len; i++)
458 list[i] = cscov++;
459
460 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
461
462 if (rc == 0) {
463 if (rx)
464 dccp_sk(sk)->dccps_pcrlen = cscov;
465 else
466 dccp_sk(sk)->dccps_pcslen = cscov;
467 }
468 kfree(list);
469 return rc;
470}
471
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800472static int dccp_setsockopt_ccid(struct sock *sk, int type,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200473 sockptr_t optval, unsigned int optlen)
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800474{
475 u8 *val;
476 int rc = 0;
477
478 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
479 return -EINVAL;
480
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200481 val = memdup_sockptr(optval, optlen);
Julia Lawall042604d2010-05-21 22:25:19 +0000482 if (IS_ERR(val))
483 return PTR_ERR(val);
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800484
485 lock_sock(sk);
486 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
487 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
488
489 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
490 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
491 release_sock(sk);
492
493 kfree(val);
494 return rc;
495}
496
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800497static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200498 sockptr_t optval, unsigned int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700499{
Gerrit Renker09dbc382006-11-14 12:57:34 -0200500 struct dccp_sock *dp = dccp_sk(sk);
501 int val, err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700502
Gerrit Renker19102992008-11-16 22:56:55 -0800503 switch (optname) {
504 case DCCP_SOCKOPT_PACKET_SIZE:
505 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
506 return 0;
507 case DCCP_SOCKOPT_CHANGE_L:
508 case DCCP_SOCKOPT_CHANGE_R:
509 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
510 return 0;
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800511 case DCCP_SOCKOPT_CCID:
512 case DCCP_SOCKOPT_RX_CCID:
513 case DCCP_SOCKOPT_TX_CCID:
514 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
Gerrit Renker19102992008-11-16 22:56:55 -0800515 }
516
517 if (optlen < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300518 return -EINVAL;
519
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200520 if (copy_from_sockptr(&val, optval, sizeof(int)))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300521 return -EFAULT;
522
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700523 if (optname == DCCP_SOCKOPT_SERVICE)
524 return dccp_setsockopt_service(sk, val, optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300525
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700526 lock_sock(sk);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300527 switch (optname) {
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200528 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
529 if (dp->dccps_role != DCCP_ROLE_SERVER)
530 err = -EOPNOTSUPP;
531 else
532 dp->dccps_server_timewait = (val != 0);
533 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800534 case DCCP_SOCKOPT_SEND_CSCOV:
535 err = dccp_setsockopt_cscov(sk, val, false);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200536 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800537 case DCCP_SOCKOPT_RECV_CSCOV:
538 err = dccp_setsockopt_cscov(sk, val, true);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200539 break;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100540 case DCCP_SOCKOPT_QPOLICY_ID:
541 if (sk->sk_state != DCCP_CLOSED)
542 err = -EISCONN;
543 else if (val < 0 || val >= DCCPQ_POLICY_MAX)
544 err = -EINVAL;
545 else
546 dp->dccps_qpolicy = val;
547 break;
548 case DCCP_SOCKOPT_QPOLICY_TXQLEN:
549 if (val < 0)
550 err = -EINVAL;
551 else
552 dp->dccps_tx_qlen = val;
553 break;
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300554 default:
555 err = -ENOPROTOOPT;
556 break;
557 }
Gerrit Renker410e27a2008-09-09 13:27:22 +0200558 release_sock(sk);
Gerrit Renker19102992008-11-16 22:56:55 -0800559
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300560 return err;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700561}
562
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200563int dccp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
564 unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800565{
566 if (level != SOL_DCCP)
567 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
568 optname, optval,
569 optlen);
570 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
571}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800572
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800573EXPORT_SYMBOL_GPL(dccp_setsockopt);
574
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700575static int dccp_getsockopt_service(struct sock *sk, int len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800576 __be32 __user *optval,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700577 int __user *optlen)
578{
579 const struct dccp_sock *dp = dccp_sk(sk);
580 const struct dccp_service_list *sl;
581 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
582
583 lock_sock(sk);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700584 if ((sl = dp->dccps_service_list) != NULL) {
585 slen = sl->dccpsl_nr * sizeof(u32);
586 total_len += slen;
587 }
588
589 err = -EINVAL;
590 if (total_len > len)
591 goto out;
592
593 err = 0;
594 if (put_user(total_len, optlen) ||
595 put_user(dp->dccps_service, optval) ||
596 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
597 err = -EFAULT;
598out:
599 release_sock(sk);
600 return err;
601}
602
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800603static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300604 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700605{
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300606 struct dccp_sock *dp;
607 int val, len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700608
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300609 if (get_user(len, optlen))
610 return -EFAULT;
611
Arnaldo Carvalho de Melo39ebc022007-03-28 11:54:32 -0700612 if (len < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300613 return -EINVAL;
614
615 dp = dccp_sk(sk);
616
617 switch (optname) {
618 case DCCP_SOCKOPT_PACKET_SIZE:
Gerrit Renker5aed3242006-11-28 19:33:36 -0200619 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
Arnaldo Carvalho de Melo841bac12006-11-28 19:42:03 -0200620 return 0;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700621 case DCCP_SOCKOPT_SERVICE:
622 return dccp_getsockopt_service(sk, len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800623 (__be32 __user *)optval, optlen);
Gerrit Renker7c559a92007-10-04 14:39:22 -0700624 case DCCP_SOCKOPT_GET_CUR_MPS:
625 val = dp->dccps_mss_cache;
Gerrit Renker7c559a92007-10-04 14:39:22 -0700626 break;
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800627 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
628 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
Gerrit Renker71c262a2008-11-23 16:04:59 -0800629 case DCCP_SOCKOPT_TX_CCID:
630 val = ccid_get_current_tx_ccid(dp);
631 if (val < 0)
632 return -ENOPROTOOPT;
633 break;
634 case DCCP_SOCKOPT_RX_CCID:
635 val = ccid_get_current_rx_ccid(dp);
636 if (val < 0)
637 return -ENOPROTOOPT;
638 break;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200639 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
640 val = dp->dccps_server_timewait;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200641 break;
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200642 case DCCP_SOCKOPT_SEND_CSCOV:
643 val = dp->dccps_pcslen;
644 break;
645 case DCCP_SOCKOPT_RECV_CSCOV:
646 val = dp->dccps_pcrlen;
647 break;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100648 case DCCP_SOCKOPT_QPOLICY_ID:
649 val = dp->dccps_qpolicy;
650 break;
651 case DCCP_SOCKOPT_QPOLICY_TXQLEN:
652 val = dp->dccps_tx_qlen;
653 break;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700654 case 128 ... 191:
655 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
656 len, (u32 __user *)optval, optlen);
657 case 192 ... 255:
658 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
659 len, (u32 __user *)optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300660 default:
661 return -ENOPROTOOPT;
662 }
663
Gerrit Renker79133502007-12-13 12:27:14 -0200664 len = sizeof(val);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300665 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
666 return -EFAULT;
667
668 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700669}
670
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800671int dccp_getsockopt(struct sock *sk, int level, int optname,
672 char __user *optval, int __user *optlen)
673{
674 if (level != SOL_DCCP)
675 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
676 optname, optval,
677 optlen);
678 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
679}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800680
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800681EXPORT_SYMBOL_GPL(dccp_getsockopt);
682
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100683static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
684{
Gu Zhengf95b4142014-12-11 11:22:04 +0800685 struct cmsghdr *cmsg;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100686
687 /*
688 * Assign an (opaque) qpolicy priority value to skb->priority.
689 *
690 * We are overloading this skb field for use with the qpolicy subystem.
691 * The skb->priority is normally used for the SO_PRIORITY option, which
692 * is initialised from sk_priority. Since the assignment of sk_priority
693 * to skb->priority happens later (on layer 3), we overload this field
694 * for use with queueing priorities as long as the skb is on layer 4.
695 * The default priority value (if nothing is set) is 0.
696 */
697 skb->priority = 0;
698
Gu Zhengf95b4142014-12-11 11:22:04 +0800699 for_each_cmsghdr(cmsg, msg) {
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100700 if (!CMSG_OK(msg, cmsg))
701 return -EINVAL;
702
703 if (cmsg->cmsg_level != SOL_DCCP)
704 continue;
705
Tomasz Grobelny04910262010-12-04 13:39:13 +0100706 if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX &&
707 !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type))
708 return -EINVAL;
709
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100710 switch (cmsg->cmsg_type) {
711 case DCCP_SCM_PRIORITY:
712 if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32)))
713 return -EINVAL;
714 skb->priority = *(__u32 *)CMSG_DATA(cmsg);
715 break;
716 default:
717 return -EINVAL;
718 }
719 }
720 return 0;
721}
722
Ying Xue1b784142015-03-02 15:37:48 +0800723int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700724{
725 const struct dccp_sock *dp = dccp_sk(sk);
726 const int flags = msg->msg_flags;
727 const int noblock = flags & MSG_DONTWAIT;
728 struct sk_buff *skb;
729 int rc, size;
730 long timeo;
731
Masami Hiramatsuee549be2017-12-29 11:47:55 +0900732 trace_dccp_probe(sk, len);
733
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700734 if (len > dp->dccps_mss_cache)
735 return -EMSGSIZE;
736
737 lock_sock(sk);
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200738
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100739 if (dccp_qpolicy_full(sk)) {
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200740 rc = -EAGAIN;
741 goto out_release;
742 }
743
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700744 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700745
746 /*
747 * We have to use sk_stream_wait_connect here to set sk_write_pending,
748 * so that the trick in dccp_rcv_request_sent_state_process.
749 */
750 /* Wait for a connection to finish. */
Gerrit Renkercecd8d02007-09-26 19:36:08 -0300751 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700752 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700753 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700754
755 size = sk->sk_prot->max_header + len;
756 release_sock(sk);
757 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
758 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700759 if (skb == NULL)
760 goto out_release;
761
Alexey Kodanev67f93df2018-03-06 22:57:01 +0300762 if (sk->sk_state == DCCP_CLOSED) {
763 rc = -ENOTCONN;
764 goto out_discard;
765 }
766
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700767 skb_reserve(skb, sk->sk_prot->max_header);
Al Viro6ce8e9c2014-04-06 21:25:44 -0400768 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700769 if (rc != 0)
770 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700771
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100772 rc = dccp_msghdr_parse(msg, skb);
773 if (rc != 0)
774 goto out_discard;
775
776 dccp_qpolicy_push(sk, skb);
Gerrit Renkerb1fcf552010-10-27 19:16:27 +0000777 /*
778 * The xmit_timer is set if the TX CCID is rate-based and will expire
779 * when congestion control permits to release further packets into the
780 * network. Window-based CCIDs do not use this timer.
781 */
782 if (!timer_pending(&dp->dccps_xmit_timer))
783 dccp_write_xmit(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700784out_release:
785 release_sock(sk);
786 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700787out_discard:
788 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700789 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700790}
791
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800792EXPORT_SYMBOL_GPL(dccp_sendmsg);
793
Ying Xue1b784142015-03-02 15:37:48 +0800794int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
795 int flags, int *addr_len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700796{
797 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700798 long timeo;
799
800 lock_sock(sk);
801
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300802 if (sk->sk_state == DCCP_LISTEN) {
803 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700804 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300805 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700806
807 timeo = sock_rcvtimeo(sk, nonblock);
808
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700809 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300810 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700811
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300812 if (skb == NULL)
813 goto verify_sock_status;
814
815 dh = dccp_hdr(skb);
816
Gerrit Renker0c869622007-11-28 11:59:48 -0200817 switch (dh->dccph_type) {
818 case DCCP_PKT_DATA:
819 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300820 goto found_ok_skb;
821
Gerrit Renker0c869622007-11-28 11:59:48 -0200822 case DCCP_PKT_CLOSE:
823 case DCCP_PKT_CLOSEREQ:
824 if (!(flags & MSG_PEEK))
825 dccp_finish_passive_close(sk);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500826 fallthrough;
Gerrit Renker0c869622007-11-28 11:59:48 -0200827 case DCCP_PKT_RESET:
828 dccp_pr_debug("found fin (%s) ok!\n",
829 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300830 len = 0;
831 goto found_fin_ok;
Gerrit Renker0c869622007-11-28 11:59:48 -0200832 default:
833 dccp_pr_debug("packet_type=%s\n",
834 dccp_packet_name(dh->dccph_type));
Dan Williams7bced392013-12-30 12:37:29 -0800835 sk_eat_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700836 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300837verify_sock_status:
838 if (sock_flag(sk, SOCK_DONE)) {
839 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700841 }
842
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300843 if (sk->sk_err) {
844 len = sock_error(sk);
845 break;
846 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700847
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300848 if (sk->sk_shutdown & RCV_SHUTDOWN) {
849 len = 0;
850 break;
851 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700852
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300853 if (sk->sk_state == DCCP_CLOSED) {
854 if (!sock_flag(sk, SOCK_DONE)) {
855 /* This occurs when user tries to read
856 * from never connected socket.
857 */
858 len = -ENOTCONN;
859 break;
860 }
861 len = 0;
862 break;
863 }
864
865 if (!timeo) {
866 len = -EAGAIN;
867 break;
868 }
869
870 if (signal_pending(current)) {
871 len = sock_intr_errno(timeo);
872 break;
873 }
874
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200875 sk_wait_data(sk, &timeo, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700876 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700877 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300878 if (len > skb->len)
879 len = skb->len;
880 else if (len < skb->len)
881 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700882
David S. Miller51f3d022014-11-05 16:46:40 -0500883 if (skb_copy_datagram_msg(skb, 0, msg, len)) {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300884 /* Exception. Bailout! */
885 len = -EFAULT;
886 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700887 }
Gerrit Renker55d95592010-02-10 20:26:18 +0000888 if (flags & MSG_TRUNC)
889 len = skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700890 found_fin_ok:
891 if (!(flags & MSG_PEEK))
Dan Williams7bced392013-12-30 12:37:29 -0800892 sk_eat_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700893 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300894 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700895out:
896 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300897 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700898}
899
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800900EXPORT_SYMBOL_GPL(dccp_recvmsg);
901
902int inet_dccp_listen(struct socket *sock, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700903{
904 struct sock *sk = sock->sk;
905 unsigned char old_state;
906 int err;
907
908 lock_sock(sk);
909
910 err = -EINVAL;
911 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
912 goto out;
913
914 old_state = sk->sk_state;
915 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
916 goto out;
917
Eric Dumazet099ecf52019-11-05 14:11:54 -0800918 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700919 /* Really, if the socket is already in listen state
920 * we can only allow the backlog to be adjusted.
921 */
922 if (old_state != DCCP_LISTEN) {
Kuniyuki Iwashimab4a8e742021-11-22 19:16:22 +0900923 struct dccp_sock *dp = dccp_sk(sk);
924
925 dp->dccps_role = DCCP_ROLE_LISTEN;
926
927 /* do not start to listen if feature negotiation setup fails */
928 if (dccp_feat_finalise_settings(dp)) {
929 err = -EPROTO;
930 goto out;
931 }
932
933 err = inet_csk_listen_start(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700934 if (err)
935 goto out;
936 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700937 err = 0;
938
939out:
940 release_sock(sk);
941 return err;
942}
943
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800944EXPORT_SYMBOL_GPL(inet_dccp_listen);
945
Gerrit Renker0c869622007-11-28 11:59:48 -0200946static void dccp_terminate_connection(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700947{
Gerrit Renker0c869622007-11-28 11:59:48 -0200948 u8 next_state = DCCP_CLOSED;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700949
Gerrit Renker0c869622007-11-28 11:59:48 -0200950 switch (sk->sk_state) {
951 case DCCP_PASSIVE_CLOSE:
952 case DCCP_PASSIVE_CLOSEREQ:
953 dccp_finish_passive_close(sk);
954 break;
955 case DCCP_PARTOPEN:
956 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
957 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500958 fallthrough;
Gerrit Renker0c869622007-11-28 11:59:48 -0200959 case DCCP_OPEN:
960 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700961
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200962 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
963 !dccp_sk(sk)->dccps_server_timewait)
Gerrit Renker0c869622007-11-28 11:59:48 -0200964 next_state = DCCP_ACTIVE_CLOSEREQ;
965 else
966 next_state = DCCP_CLOSING;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500967 fallthrough;
Gerrit Renker0c869622007-11-28 11:59:48 -0200968 default:
969 dccp_set_state(sk, next_state);
970 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700971}
972
973void dccp_close(struct sock *sk, long timeout)
974{
Ian McDonald97e58482006-08-26 19:16:45 -0700975 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700976 struct sk_buff *skb;
Gerrit Renkerd83bd952007-12-16 16:06:03 -0800977 u32 data_was_unread = 0;
Herbert Xu134af342006-05-05 17:09:13 -0700978 int state;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700979
980 lock_sock(sk);
981
982 sk->sk_shutdown = SHUTDOWN_MASK;
983
984 if (sk->sk_state == DCCP_LISTEN) {
985 dccp_set_state(sk, DCCP_CLOSED);
986
987 /* Special case. */
988 inet_csk_listen_stop(sk);
989
990 goto adjudge_to_death;
991 }
992
Ian McDonald97e58482006-08-26 19:16:45 -0700993 sk_stop_timer(sk, &dp->dccps_xmit_timer);
994
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700995 /*
996 * We need to flush the recv. buffs. We do this only on the
997 * descriptor close, not protocol-sourced closes, because the
998 *reader process may not have drained the data yet!
999 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001000 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001001 data_was_unread += skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001002 __kfree_skb(skb);
1003 }
1004
Eric Dumazet346da622016-11-02 18:04:24 -07001005 /* If socket has been already reset kill it. */
1006 if (sk->sk_state == DCCP_CLOSED)
1007 goto adjudge_to_death;
1008
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001009 if (data_was_unread) {
1010 /* Unread data was tossed, send an appropriate Reset Code */
Gerrit Renker2f34b322010-10-11 20:44:42 +02001011 DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread);
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001012 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
1013 dccp_set_state(sk, DCCP_CLOSED);
1014 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001015 /* Check zero linger _after_ checking for unread data. */
1016 sk->sk_prot->disconnect(sk, 0);
Gerrit Renker0c869622007-11-28 11:59:48 -02001017 } else if (sk->sk_state != DCCP_CLOSED) {
Gerrit Renkerb1fcf552010-10-27 19:16:27 +00001018 /*
1019 * Normal connection termination. May need to wait if there are
1020 * still packets in the TX queue that are delayed by the CCID.
1021 */
1022 dccp_flush_write_queue(sk, &timeout);
Gerrit Renker0c869622007-11-28 11:59:48 -02001023 dccp_terminate_connection(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001024 }
1025
Gerrit Renkerb1fcf552010-10-27 19:16:27 +00001026 /*
1027 * Flush write queue. This may be necessary in several cases:
1028 * - we have been closed by the peer but still have application data;
1029 * - abortive termination (unread data or zero linger time),
1030 * - normal termination but queue could not be flushed within time limit
1031 */
1032 __skb_queue_purge(&sk->sk_write_queue);
1033
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001034 sk_stream_wait_close(sk, timeout);
1035
1036adjudge_to_death:
Herbert Xu134af342006-05-05 17:09:13 -07001037 state = sk->sk_state;
1038 sock_hold(sk);
1039 sock_orphan(sk);
Herbert Xu134af342006-05-05 17:09:13 -07001040
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -07001041 /*
1042 * It is the last release_sock in its life. It will remove backlog.
1043 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001044 release_sock(sk);
1045 /*
1046 * Now socket is owned by kernel and we acquire BH lock
1047 * to finish close. No need to check for user refs.
1048 */
1049 local_bh_disable();
1050 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001051 WARN_ON(sock_owned_by_user(sk));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001052
Eric Dumazet19757ce2021-10-14 06:41:26 -07001053 this_cpu_inc(dccp_orphan_count);
Herbert Xueb4dea52008-12-29 23:04:08 -08001054
Herbert Xu134af342006-05-05 17:09:13 -07001055 /* Have we already been destroyed by a softirq or backlog? */
1056 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1057 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001058
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001059 if (sk->sk_state == DCCP_CLOSED)
1060 inet_csk_destroy_sock(sk);
1061
1062 /* Otherwise, socket is reprieved until protocol close. */
1063
Herbert Xu134af342006-05-05 17:09:13 -07001064out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001065 bh_unlock_sock(sk);
1066 local_bh_enable();
1067 sock_put(sk);
1068}
1069
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001070EXPORT_SYMBOL_GPL(dccp_close);
1071
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001072void dccp_shutdown(struct sock *sk, int how)
1073{
Gerrit Renker8e8c71f2007-11-21 09:56:48 -02001074 dccp_pr_debug("called shutdown(%x)\n", how);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001075}
1076
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001077EXPORT_SYMBOL_GPL(dccp_shutdown);
1078
Fabian Frederick0c5b8a42014-10-01 06:48:03 +02001079static inline int __init dccp_mib_init(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001080{
WANG Cong698365f2014-05-05 15:55:55 -07001081 dccp_statistics = alloc_percpu(struct dccp_mib);
1082 if (!dccp_statistics)
1083 return -ENOMEM;
1084 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001085}
1086
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001087static inline void dccp_mib_exit(void)
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001088{
WANG Cong698365f2014-05-05 15:55:55 -07001089 free_percpu(dccp_statistics);
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001090}
1091
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001092static int thash_entries;
1093module_param(thash_entries, int, 0444);
1094MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1095
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001096#ifdef CONFIG_IP_DCCP_DEBUG
Rusty Russelleb939922011-12-19 14:08:01 +00001097bool dccp_debug;
Gerrit Renker43264992008-08-23 13:28:27 +02001098module_param(dccp_debug, bool, 0644);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001099MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001100
1101EXPORT_SYMBOL_GPL(dccp_debug);
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001102#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001103
1104static int __init dccp_init(void)
1105{
1106 unsigned long goal;
Arun KSca79b0c2018-12-28 00:34:29 -08001107 unsigned long nr_pages = totalram_pages();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001108 int ehash_order, bhash_order, i;
Eric Dumazetdd24c002008-11-25 21:17:14 -08001109 int rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001110
Patrick McHardy028b0272008-04-12 18:35:41 -07001111 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001112 sizeof_field(struct sk_buff, cb));
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001113 inet_hashinfo_init(&dccp_hashinfo);
Peter Oskolkovc92c81d2018-12-24 12:57:17 -08001114 rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
1115 if (rc)
Eric Dumazet19757ce2021-10-14 06:41:26 -07001116 goto out_fail;
Peter Oskolkovc92c81d2018-12-24 12:57:17 -08001117 rc = -ENOBUFS;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001118 dccp_hashinfo.bind_bucket_cachep =
1119 kmem_cache_create("dccp_bind_bucket",
1120 sizeof(struct inet_bind_bucket), 0,
Vasily Averin990c74e2021-07-19 13:44:37 +03001121 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001122 if (!dccp_hashinfo.bind_bucket_cachep)
Wang Haic96b6ac2020-06-09 22:18:16 +08001123 goto out_free_hashinfo2;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001124
1125 /*
1126 * Size and allocate the main established and bind bucket
1127 * hash tables.
1128 *
1129 * The methodology is similar to that of the buffer cache.
1130 */
Arun KS3d6357d2018-12-28 00:34:20 -08001131 if (nr_pages >= (128 * 1024))
1132 goal = nr_pages >> (21 - PAGE_SHIFT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001133 else
Arun KS3d6357d2018-12-28 00:34:20 -08001134 goal = nr_pages >> (23 - PAGE_SHIFT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001135
1136 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001137 goal = (thash_entries *
1138 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001139 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1140 ;
1141 do {
Eric Dumazetf373b532009-10-09 00:16:19 +00001142 unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001143 sizeof(struct inet_ehash_bucket);
Eric Dumazetf373b532009-10-09 00:16:19 +00001144
1145 while (hash_size & (hash_size - 1))
1146 hash_size--;
1147 dccp_hashinfo.ehash_mask = hash_size - 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001148 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
Mel Gorman1c29b3f2009-07-29 15:04:10 -07001149 __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001150 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1151
1152 if (!dccp_hashinfo.ehash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001153 DCCP_CRIT("Failed to allocate DCCP established hash table");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001154 goto out_free_bind_bucket_cachep;
1155 }
1156
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001157 for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001158 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001159
Eric Dumazet230140c2007-11-07 02:40:20 -08001160 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1161 goto out_free_dccp_ehash;
1162
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001163 bhash_order = ehash_order;
1164
1165 do {
1166 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1167 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001168 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1169 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001170 continue;
1171 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
Mel Gorman1c29b3f2009-07-29 15:04:10 -07001172 __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001173 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1174
1175 if (!dccp_hashinfo.bhash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001176 DCCP_CRIT("Failed to allocate DCCP bind hash table");
Eric Dumazet230140c2007-11-07 02:40:20 -08001177 goto out_free_dccp_locks;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001178 }
1179
1180 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1181 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1182 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1183 }
1184
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001185 rc = dccp_mib_init();
Arnaldo Carvalho de Melofa23e2e2006-03-20 17:16:01 -08001186 if (rc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001187 goto out_free_dccp_bhash;
1188
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001189 rc = dccp_ackvec_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001190 if (rc)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001191 goto out_free_dccp_mib;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001192
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001193 rc = dccp_sysctl_init();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001194 if (rc)
1195 goto out_ackvec_exit;
Gerrit Renker4c70f382007-09-25 22:40:13 -07001196
Gerrit Renkerddebc972009-01-04 21:42:53 -08001197 rc = ccid_initialize_builtins();
1198 if (rc)
1199 goto out_sysctl_exit;
1200
Gerrit Renker4c70f382007-09-25 22:40:13 -07001201 dccp_timestamping_init();
Gerrit Renkerd14a0eb2010-03-14 20:13:19 +00001202
1203 return 0;
1204
Gerrit Renkerddebc972009-01-04 21:42:53 -08001205out_sysctl_exit:
1206 dccp_sysctl_exit();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001207out_ackvec_exit:
1208 dccp_ackvec_exit();
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001209out_free_dccp_mib:
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001210 dccp_mib_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001211out_free_dccp_bhash:
1212 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
Eric Dumazet230140c2007-11-07 02:40:20 -08001213out_free_dccp_locks:
1214 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001215out_free_dccp_ehash:
1216 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001217out_free_bind_bucket_cachep:
1218 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Wang Haic96b6ac2020-06-09 22:18:16 +08001219out_free_hashinfo2:
1220 inet_hashinfo2_free_mod(&dccp_hashinfo);
Gerrit Renkerd14a0eb2010-03-14 20:13:19 +00001221out_fail:
1222 dccp_hashinfo.bhash = NULL;
1223 dccp_hashinfo.ehash = NULL;
1224 dccp_hashinfo.bind_bucket_cachep = NULL;
1225 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001226}
1227
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001228static void __exit dccp_fini(void)
1229{
Gerrit Renkerddebc972009-01-04 21:42:53 -08001230 ccid_cleanup_builtins();
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001231 dccp_mib_exit();
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001232 free_pages((unsigned long)dccp_hashinfo.bhash,
1233 get_order(dccp_hashinfo.bhash_size *
1234 sizeof(struct inet_bind_hashbucket)));
1235 free_pages((unsigned long)dccp_hashinfo.ehash,
Eric Dumazetf373b532009-10-09 00:16:19 +00001236 get_order((dccp_hashinfo.ehash_mask + 1) *
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001237 sizeof(struct inet_ehash_bucket)));
Eric Dumazet230140c2007-11-07 02:40:20 -08001238 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001239 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001240 dccp_ackvec_exit();
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001241 dccp_sysctl_exit();
Wang Haic96b6ac2020-06-09 22:18:16 +08001242 inet_hashinfo2_free_mod(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001243}
1244
1245module_init(dccp_init);
1246module_exit(dccp_fini);
1247
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001248MODULE_LICENSE("GPL");
1249MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1250MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");