blob: c13b6609474b65d6c96d63510b5626b9f2ee2dd6 [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 Dumazetdd24c002008-11-25 21:17:14 -080045struct percpu_counter dccp_orphan_count;
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -080046EXPORT_SYMBOL_GPL(dccp_orphan_count);
47
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);
Arnaldo Carvalho de Meloc25a18b2006-03-20 21:58:56 -0800104 /* fall through */
105 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
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800241static inline int dccp_listen_start(struct sock *sk, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700242{
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700243 struct dccp_sock *dp = dccp_sk(sk);
244
245 dp->dccps_role = DCCP_ROLE_LISTEN;
Gerrit Renker9eca0a42008-11-12 00:48:44 -0800246 /* do not start to listen if feature negotiation setup fails */
247 if (dccp_feat_finalise_settings(dp))
248 return -EPROTO;
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800249 return inet_csk_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700250}
251
Gerrit Renkerce865a62007-11-24 22:14:15 -0200252static inline int dccp_need_reset(int state)
253{
254 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
255 state != DCCP_REQUESTING;
256}
257
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700258int dccp_disconnect(struct sock *sk, int flags)
259{
260 struct inet_connection_sock *icsk = inet_csk(sk);
261 struct inet_sock *inet = inet_sk(sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000262 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700263 const int old_state = sk->sk_state;
264
265 if (old_state != DCCP_CLOSED)
266 dccp_set_state(sk, DCCP_CLOSED);
267
Gerrit Renkerce865a62007-11-24 22:14:15 -0200268 /*
269 * This corresponds to the ABORT function of RFC793, sec. 3.8
270 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
271 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700272 if (old_state == DCCP_LISTEN) {
273 inet_csk_listen_stop(sk);
Gerrit Renkerce865a62007-11-24 22:14:15 -0200274 } else if (dccp_need_reset(old_state)) {
275 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
276 sk->sk_err = ECONNRESET;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700277 } else if (old_state == DCCP_REQUESTING)
278 sk->sk_err = ECONNRESET;
279
280 dccp_clear_xmit_timers(sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000281 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
Mohamed Ghannam69c64862017-12-05 20:58:35 +0000282 dp->dccps_hc_rx_ccid = NULL;
Gerrit Renker48816322008-08-23 13:28:27 +0200283
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700284 __skb_queue_purge(&sk->sk_receive_queue);
Gerrit Renker48816322008-08-23 13:28:27 +0200285 __skb_queue_purge(&sk->sk_write_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700286 if (sk->sk_send_head != NULL) {
287 __kfree_skb(sk->sk_send_head);
288 sk->sk_send_head = NULL;
289 }
290
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000291 inet->inet_dport = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700292
293 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
294 inet_reset_saddr(sk);
295
296 sk->sk_shutdown = 0;
297 sock_reset_flag(sk, SOCK_DONE);
298
299 icsk->icsk_backoff = 0;
300 inet_csk_delack_init(sk);
301 __sk_dst_reset(sk);
302
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000303 WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700304
305 sk->sk_error_report(sk);
Hariprasad Kelam3285a9a2019-05-12 16:09:49 +0530306 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700307}
308
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800309EXPORT_SYMBOL_GPL(dccp_disconnect);
310
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700311/*
312 * Wait for a DCCP event.
313 *
314 * Note that we don't need to lock the socket, as the upper poll layers
315 * take care of normal races (between the test and the event) and we don't
316 * go look at any of the socket buffers directly.
317 */
318__poll_t dccp_poll(struct file *file, struct socket *sock,
319 poll_table *wait)
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700320{
Al Viroade994f2017-07-03 00:01:49 -0400321 __poll_t mask;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700322 struct sock *sk = sock->sk;
323
Karsten Graul89ab0662018-10-23 13:40:39 +0200324 sock_poll_wait(file, sock, wait);
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700325 if (sk->sk_state == DCCP_LISTEN)
326 return inet_csk_listen_poll(sk);
327
328 /* Socket is not locked. We are protected from async events
329 by poll logic and correct handling of state changes
330 made by another threads is impossible in any case.
331 */
332
333 mask = 0;
334 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800335 mask = EPOLLERR;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700336
337 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800338 mask |= EPOLLHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700339 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800340 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700341
342 /* Connected? */
343 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
344 if (atomic_read(&sk->sk_rmem_alloc) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800345 mask |= EPOLLIN | EPOLLRDNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700346
347 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
Eric Dumazet64dc6132013-07-22 20:26:31 -0700348 if (sk_stream_is_writeable(sk)) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800349 mask |= EPOLLOUT | EPOLLWRNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700350 } else { /* send SIGIO later */
Eric Dumazet9cd3e072015-11-29 20:03:10 -0800351 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700352 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
353
354 /* Race breaker. If space is freed after
355 * wspace test but before the flags are set,
356 * IO signal will be lost.
357 */
Eric Dumazet64dc6132013-07-22 20:26:31 -0700358 if (sk_stream_is_writeable(sk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800359 mask |= EPOLLOUT | EPOLLWRNORM;
Arnaldo Carvalho de Melo331968b2005-08-23 21:54:23 -0700360 }
361 }
362 }
363 return mask;
364}
365
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700366EXPORT_SYMBOL_GPL(dccp_poll);
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800367
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700368int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
369{
Arnaldo Carvalho de Melo62731722007-10-23 20:23:30 -0700370 int rc = -ENOTCONN;
371
372 lock_sock(sk);
373
374 if (sk->sk_state == DCCP_LISTEN)
375 goto out;
376
377 switch (cmd) {
378 case SIOCINQ: {
379 struct sk_buff *skb;
380 unsigned long amount = 0;
381
382 skb = skb_peek(&sk->sk_receive_queue);
383 if (skb != NULL) {
384 /*
385 * We will only return the amount of this packet since
386 * that is all that will be read.
387 */
388 amount = skb->len;
389 }
390 rc = put_user(amount, (int __user *)arg);
391 }
392 break;
393 default:
394 rc = -ENOIOCTLCMD;
395 break;
396 }
397out:
398 release_sock(sk);
399 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700400}
401
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800402EXPORT_SYMBOL_GPL(dccp_ioctl);
403
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800404static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
David S. Millerb7058842009-09-30 16:12:20 -0700405 char __user *optval, unsigned int optlen)
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700406{
407 struct dccp_sock *dp = dccp_sk(sk);
408 struct dccp_service_list *sl = NULL;
409
Arnaldo Carvalho de Melo8109b022006-12-10 16:01:18 -0200410 if (service == DCCP_SERVICE_INVALID_VALUE ||
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700411 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
412 return -EINVAL;
413
414 if (optlen > sizeof(service)) {
415 sl = kmalloc(optlen, GFP_KERNEL);
416 if (sl == NULL)
417 return -ENOMEM;
418
419 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
420 if (copy_from_user(sl->dccpsl_list,
421 optval + sizeof(service),
422 optlen - sizeof(service)) ||
423 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
424 kfree(sl);
425 return -EFAULT;
426 }
427 }
428
429 lock_sock(sk);
430 dp->dccps_service = service;
431
Jesper Juhla51482b2005-11-08 09:41:34 -0800432 kfree(dp->dccps_service_list);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700433
434 dp->dccps_service_list = sl;
435 release_sock(sk);
436 return 0;
437}
438
Gerrit Renker29450552008-11-16 22:53:48 -0800439static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
440{
441 u8 *list, len;
442 int i, rc;
443
444 if (cscov < 0 || cscov > 15)
445 return -EINVAL;
446 /*
447 * Populate a list of permissible values, in the range cscov...15. This
448 * is necessary since feature negotiation of single values only works if
449 * both sides incidentally choose the same value. Since the list starts
450 * lowest-value first, negotiation will pick the smallest shared value.
451 */
452 if (cscov == 0)
453 return 0;
454 len = 16 - cscov;
455
456 list = kmalloc(len, GFP_KERNEL);
457 if (list == NULL)
458 return -ENOBUFS;
459
460 for (i = 0; i < len; i++)
461 list[i] = cscov++;
462
463 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
464
465 if (rc == 0) {
466 if (rx)
467 dccp_sk(sk)->dccps_pcrlen = cscov;
468 else
469 dccp_sk(sk)->dccps_pcslen = cscov;
470 }
471 kfree(list);
472 return rc;
473}
474
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800475static int dccp_setsockopt_ccid(struct sock *sk, int type,
David S. Millerb7058842009-09-30 16:12:20 -0700476 char __user *optval, unsigned int optlen)
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800477{
478 u8 *val;
479 int rc = 0;
480
481 if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
482 return -EINVAL;
483
Julia Lawall042604d2010-05-21 22:25:19 +0000484 val = memdup_user(optval, optlen);
485 if (IS_ERR(val))
486 return PTR_ERR(val);
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800487
488 lock_sock(sk);
489 if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
490 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
491
492 if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
493 rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
494 release_sock(sk);
495
496 kfree(val);
497 return rc;
498}
499
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800500static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700501 char __user *optval, unsigned int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700502{
Gerrit Renker09dbc382006-11-14 12:57:34 -0200503 struct dccp_sock *dp = dccp_sk(sk);
504 int val, err = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700505
Gerrit Renker19102992008-11-16 22:56:55 -0800506 switch (optname) {
507 case DCCP_SOCKOPT_PACKET_SIZE:
508 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
509 return 0;
510 case DCCP_SOCKOPT_CHANGE_L:
511 case DCCP_SOCKOPT_CHANGE_R:
512 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
513 return 0;
Gerrit Renkerb20a9c22008-11-23 16:02:31 -0800514 case DCCP_SOCKOPT_CCID:
515 case DCCP_SOCKOPT_RX_CCID:
516 case DCCP_SOCKOPT_TX_CCID:
517 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
Gerrit Renker19102992008-11-16 22:56:55 -0800518 }
519
520 if (optlen < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300521 return -EINVAL;
522
523 if (get_user(val, (int __user *)optval))
524 return -EFAULT;
525
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700526 if (optname == DCCP_SOCKOPT_SERVICE)
527 return dccp_setsockopt_service(sk, val, optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300528
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700529 lock_sock(sk);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300530 switch (optname) {
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200531 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
532 if (dp->dccps_role != DCCP_ROLE_SERVER)
533 err = -EOPNOTSUPP;
534 else
535 dp->dccps_server_timewait = (val != 0);
536 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800537 case DCCP_SOCKOPT_SEND_CSCOV:
538 err = dccp_setsockopt_cscov(sk, val, false);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200539 break;
Gerrit Renker29450552008-11-16 22:53:48 -0800540 case DCCP_SOCKOPT_RECV_CSCOV:
541 err = dccp_setsockopt_cscov(sk, val, true);
Tomasz Grobelnyd6da3512008-09-04 07:30:19 +0200542 break;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100543 case DCCP_SOCKOPT_QPOLICY_ID:
544 if (sk->sk_state != DCCP_CLOSED)
545 err = -EISCONN;
546 else if (val < 0 || val >= DCCPQ_POLICY_MAX)
547 err = -EINVAL;
548 else
549 dp->dccps_qpolicy = val;
550 break;
551 case DCCP_SOCKOPT_QPOLICY_TXQLEN:
552 if (val < 0)
553 err = -EINVAL;
554 else
555 dp->dccps_tx_qlen = val;
556 break;
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300557 default:
558 err = -ENOPROTOOPT;
559 break;
560 }
Gerrit Renker410e27a2008-09-09 13:27:22 +0200561 release_sock(sk);
Gerrit Renker19102992008-11-16 22:56:55 -0800562
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300563 return err;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700564}
565
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800566int dccp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700567 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800568{
569 if (level != SOL_DCCP)
570 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
571 optname, optval,
572 optlen);
573 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
574}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800575
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800576EXPORT_SYMBOL_GPL(dccp_setsockopt);
577
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800578#ifdef CONFIG_COMPAT
579int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700580 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800581{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800582 if (level != SOL_DCCP)
583 return inet_csk_compat_setsockopt(sk, level, optname,
584 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800585 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
586}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800587
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800588EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
589#endif
590
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700591static int dccp_getsockopt_service(struct sock *sk, int len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800592 __be32 __user *optval,
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700593 int __user *optlen)
594{
595 const struct dccp_sock *dp = dccp_sk(sk);
596 const struct dccp_service_list *sl;
597 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
598
599 lock_sock(sk);
Arnaldo Carvalho de Melo67e6b622005-09-16 16:58:40 -0700600 if ((sl = dp->dccps_service_list) != NULL) {
601 slen = sl->dccpsl_nr * sizeof(u32);
602 total_len += slen;
603 }
604
605 err = -EINVAL;
606 if (total_len > len)
607 goto out;
608
609 err = 0;
610 if (put_user(total_len, optlen) ||
611 put_user(dp->dccps_service, optval) ||
612 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
613 err = -EFAULT;
614out:
615 release_sock(sk);
616 return err;
617}
618
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800619static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300620 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700621{
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300622 struct dccp_sock *dp;
623 int val, len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700624
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300625 if (get_user(len, optlen))
626 return -EFAULT;
627
Arnaldo Carvalho de Melo39ebc022007-03-28 11:54:32 -0700628 if (len < (int)sizeof(int))
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300629 return -EINVAL;
630
631 dp = dccp_sk(sk);
632
633 switch (optname) {
634 case DCCP_SOCKOPT_PACKET_SIZE:
Gerrit Renker5aed3242006-11-28 19:33:36 -0200635 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
Arnaldo Carvalho de Melo841bac12006-11-28 19:42:03 -0200636 return 0;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700637 case DCCP_SOCKOPT_SERVICE:
638 return dccp_getsockopt_service(sk, len,
Andrea Bittau60fe62e2006-03-20 19:23:32 -0800639 (__be32 __user *)optval, optlen);
Gerrit Renker7c559a92007-10-04 14:39:22 -0700640 case DCCP_SOCKOPT_GET_CUR_MPS:
641 val = dp->dccps_mss_cache;
Gerrit Renker7c559a92007-10-04 14:39:22 -0700642 break;
Gerrit Renkerd90ebcb2008-11-12 00:47:26 -0800643 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
644 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
Gerrit Renker71c262a2008-11-23 16:04:59 -0800645 case DCCP_SOCKOPT_TX_CCID:
646 val = ccid_get_current_tx_ccid(dp);
647 if (val < 0)
648 return -ENOPROTOOPT;
649 break;
650 case DCCP_SOCKOPT_RX_CCID:
651 val = ccid_get_current_rx_ccid(dp);
652 if (val < 0)
653 return -ENOPROTOOPT;
654 break;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200655 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
656 val = dp->dccps_server_timewait;
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200657 break;
Gerrit Renker6f4e5ff2006-11-10 17:43:06 -0200658 case DCCP_SOCKOPT_SEND_CSCOV:
659 val = dp->dccps_pcslen;
660 break;
661 case DCCP_SOCKOPT_RECV_CSCOV:
662 val = dp->dccps_pcrlen;
663 break;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100664 case DCCP_SOCKOPT_QPOLICY_ID:
665 val = dp->dccps_qpolicy;
666 break;
667 case DCCP_SOCKOPT_QPOLICY_TXQLEN:
668 val = dp->dccps_tx_qlen;
669 break;
Arnaldo Carvalho de Melo88f964d2005-09-18 00:19:32 -0700670 case 128 ... 191:
671 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
672 len, (u32 __user *)optval, optlen);
673 case 192 ... 255:
674 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
675 len, (u32 __user *)optval, optlen);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300676 default:
677 return -ENOPROTOOPT;
678 }
679
Gerrit Renker79133502007-12-13 12:27:14 -0200680 len = sizeof(val);
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300681 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
682 return -EFAULT;
683
684 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700685}
686
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800687int dccp_getsockopt(struct sock *sk, int level, int optname,
688 char __user *optval, int __user *optlen)
689{
690 if (level != SOL_DCCP)
691 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
692 optname, optval,
693 optlen);
694 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
695}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800696
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800697EXPORT_SYMBOL_GPL(dccp_getsockopt);
698
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800699#ifdef CONFIG_COMPAT
700int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800701 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800702{
Arnaldo Carvalho de Melodec73ff2006-03-20 22:46:16 -0800703 if (level != SOL_DCCP)
704 return inet_csk_compat_getsockopt(sk, level, optname,
705 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800706 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
707}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800708
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800709EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
710#endif
711
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100712static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
713{
Gu Zhengf95b4142014-12-11 11:22:04 +0800714 struct cmsghdr *cmsg;
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100715
716 /*
717 * Assign an (opaque) qpolicy priority value to skb->priority.
718 *
719 * We are overloading this skb field for use with the qpolicy subystem.
720 * The skb->priority is normally used for the SO_PRIORITY option, which
721 * is initialised from sk_priority. Since the assignment of sk_priority
722 * to skb->priority happens later (on layer 3), we overload this field
723 * for use with queueing priorities as long as the skb is on layer 4.
724 * The default priority value (if nothing is set) is 0.
725 */
726 skb->priority = 0;
727
Gu Zhengf95b4142014-12-11 11:22:04 +0800728 for_each_cmsghdr(cmsg, msg) {
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100729 if (!CMSG_OK(msg, cmsg))
730 return -EINVAL;
731
732 if (cmsg->cmsg_level != SOL_DCCP)
733 continue;
734
Tomasz Grobelny04910262010-12-04 13:39:13 +0100735 if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX &&
736 !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type))
737 return -EINVAL;
738
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100739 switch (cmsg->cmsg_type) {
740 case DCCP_SCM_PRIORITY:
741 if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32)))
742 return -EINVAL;
743 skb->priority = *(__u32 *)CMSG_DATA(cmsg);
744 break;
745 default:
746 return -EINVAL;
747 }
748 }
749 return 0;
750}
751
Ying Xue1b784142015-03-02 15:37:48 +0800752int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700753{
754 const struct dccp_sock *dp = dccp_sk(sk);
755 const int flags = msg->msg_flags;
756 const int noblock = flags & MSG_DONTWAIT;
757 struct sk_buff *skb;
758 int rc, size;
759 long timeo;
760
Masami Hiramatsuee549be2017-12-29 11:47:55 +0900761 trace_dccp_probe(sk, len);
762
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700763 if (len > dp->dccps_mss_cache)
764 return -EMSGSIZE;
765
766 lock_sock(sk);
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200767
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100768 if (dccp_qpolicy_full(sk)) {
Ian McDonaldb1308dc2006-11-20 18:30:17 -0200769 rc = -EAGAIN;
770 goto out_release;
771 }
772
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700773 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700774
775 /*
776 * We have to use sk_stream_wait_connect here to set sk_write_pending,
777 * so that the trick in dccp_rcv_request_sent_state_process.
778 */
779 /* Wait for a connection to finish. */
Gerrit Renkercecd8d02007-09-26 19:36:08 -0300780 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700781 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700782 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700783
784 size = sk->sk_prot->max_header + len;
785 release_sock(sk);
786 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
787 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700788 if (skb == NULL)
789 goto out_release;
790
Alexey Kodanev67f93df2018-03-06 22:57:01 +0300791 if (sk->sk_state == DCCP_CLOSED) {
792 rc = -ENOTCONN;
793 goto out_discard;
794 }
795
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700796 skb_reserve(skb, sk->sk_prot->max_header);
Al Viro6ce8e9c2014-04-06 21:25:44 -0400797 rc = memcpy_from_msg(skb_put(skb, len), msg, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700798 if (rc != 0)
799 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700800
Tomasz Grobelny871a2c12010-12-04 13:38:01 +0100801 rc = dccp_msghdr_parse(msg, skb);
802 if (rc != 0)
803 goto out_discard;
804
805 dccp_qpolicy_push(sk, skb);
Gerrit Renkerb1fcf552010-10-27 19:16:27 +0000806 /*
807 * The xmit_timer is set if the TX CCID is rate-based and will expire
808 * when congestion control permits to release further packets into the
809 * network. Window-based CCIDs do not use this timer.
810 */
811 if (!timer_pending(&dp->dccps_xmit_timer))
812 dccp_write_xmit(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700813out_release:
814 release_sock(sk);
815 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700816out_discard:
817 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700818 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700819}
820
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800821EXPORT_SYMBOL_GPL(dccp_sendmsg);
822
Ying Xue1b784142015-03-02 15:37:48 +0800823int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
824 int flags, int *addr_len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700825{
826 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700827 long timeo;
828
829 lock_sock(sk);
830
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300831 if (sk->sk_state == DCCP_LISTEN) {
832 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700833 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300834 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700835
836 timeo = sock_rcvtimeo(sk, nonblock);
837
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700838 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300839 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700840
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300841 if (skb == NULL)
842 goto verify_sock_status;
843
844 dh = dccp_hdr(skb);
845
Gerrit Renker0c869622007-11-28 11:59:48 -0200846 switch (dh->dccph_type) {
847 case DCCP_PKT_DATA:
848 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300849 goto found_ok_skb;
850
Gerrit Renker0c869622007-11-28 11:59:48 -0200851 case DCCP_PKT_CLOSE:
852 case DCCP_PKT_CLOSEREQ:
853 if (!(flags & MSG_PEEK))
854 dccp_finish_passive_close(sk);
855 /* fall through */
856 case DCCP_PKT_RESET:
857 dccp_pr_debug("found fin (%s) ok!\n",
858 dccp_packet_name(dh->dccph_type));
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300859 len = 0;
860 goto found_fin_ok;
Gerrit Renker0c869622007-11-28 11:59:48 -0200861 default:
862 dccp_pr_debug("packet_type=%s\n",
863 dccp_packet_name(dh->dccph_type));
Dan Williams7bced392013-12-30 12:37:29 -0800864 sk_eat_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700865 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300866verify_sock_status:
867 if (sock_flag(sk, SOCK_DONE)) {
868 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700869 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700870 }
871
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300872 if (sk->sk_err) {
873 len = sock_error(sk);
874 break;
875 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700876
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300877 if (sk->sk_shutdown & RCV_SHUTDOWN) {
878 len = 0;
879 break;
880 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700881
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300882 if (sk->sk_state == DCCP_CLOSED) {
883 if (!sock_flag(sk, SOCK_DONE)) {
884 /* This occurs when user tries to read
885 * from never connected socket.
886 */
887 len = -ENOTCONN;
888 break;
889 }
890 len = 0;
891 break;
892 }
893
894 if (!timeo) {
895 len = -EAGAIN;
896 break;
897 }
898
899 if (signal_pending(current)) {
900 len = sock_intr_errno(timeo);
901 break;
902 }
903
Sabrina Dubrocadfbafc92015-07-24 18:19:25 +0200904 sk_wait_data(sk, &timeo, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700905 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700906 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300907 if (len > skb->len)
908 len = skb->len;
909 else if (len < skb->len)
910 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700911
David S. Miller51f3d022014-11-05 16:46:40 -0500912 if (skb_copy_datagram_msg(skb, 0, msg, len)) {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300913 /* Exception. Bailout! */
914 len = -EFAULT;
915 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700916 }
Gerrit Renker55d95592010-02-10 20:26:18 +0000917 if (flags & MSG_TRUNC)
918 len = skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700919 found_fin_ok:
920 if (!(flags & MSG_PEEK))
Dan Williams7bced392013-12-30 12:37:29 -0800921 sk_eat_skb(sk, skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700922 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300923 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700924out:
925 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300926 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700927}
928
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800929EXPORT_SYMBOL_GPL(dccp_recvmsg);
930
931int inet_dccp_listen(struct socket *sock, int backlog)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700932{
933 struct sock *sk = sock->sk;
934 unsigned char old_state;
935 int err;
936
937 lock_sock(sk);
938
939 err = -EINVAL;
940 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
941 goto out;
942
943 old_state = sk->sk_state;
944 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
945 goto out;
946
Eric Dumazet099ecf52019-11-05 14:11:54 -0800947 WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700948 /* Really, if the socket is already in listen state
949 * we can only allow the backlog to be adjusted.
950 */
951 if (old_state != DCCP_LISTEN) {
952 /*
953 * FIXME: here it probably should be sk->sk_prot->listen_start
954 * see tcp_listen_start
955 */
Eric Dumazet72a3eff2006-11-16 02:30:37 -0800956 err = dccp_listen_start(sk, backlog);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700957 if (err)
958 goto out;
959 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700960 err = 0;
961
962out:
963 release_sock(sk);
964 return err;
965}
966
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -0800967EXPORT_SYMBOL_GPL(inet_dccp_listen);
968
Gerrit Renker0c869622007-11-28 11:59:48 -0200969static void dccp_terminate_connection(struct sock *sk)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700970{
Gerrit Renker0c869622007-11-28 11:59:48 -0200971 u8 next_state = DCCP_CLOSED;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700972
Gerrit Renker0c869622007-11-28 11:59:48 -0200973 switch (sk->sk_state) {
974 case DCCP_PASSIVE_CLOSE:
975 case DCCP_PASSIVE_CLOSEREQ:
976 dccp_finish_passive_close(sk);
977 break;
978 case DCCP_PARTOPEN:
979 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
980 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
981 /* fall through */
982 case DCCP_OPEN:
983 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700984
Gerrit Renkerb8599d22007-12-13 12:25:01 -0200985 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
986 !dccp_sk(sk)->dccps_server_timewait)
Gerrit Renker0c869622007-11-28 11:59:48 -0200987 next_state = DCCP_ACTIVE_CLOSEREQ;
988 else
989 next_state = DCCP_CLOSING;
990 /* fall through */
991 default:
992 dccp_set_state(sk, next_state);
993 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700994}
995
996void dccp_close(struct sock *sk, long timeout)
997{
Ian McDonald97e58482006-08-26 19:16:45 -0700998 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700999 struct sk_buff *skb;
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001000 u32 data_was_unread = 0;
Herbert Xu134af342006-05-05 17:09:13 -07001001 int state;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001002
1003 lock_sock(sk);
1004
1005 sk->sk_shutdown = SHUTDOWN_MASK;
1006
1007 if (sk->sk_state == DCCP_LISTEN) {
1008 dccp_set_state(sk, DCCP_CLOSED);
1009
1010 /* Special case. */
1011 inet_csk_listen_stop(sk);
1012
1013 goto adjudge_to_death;
1014 }
1015
Ian McDonald97e58482006-08-26 19:16:45 -07001016 sk_stop_timer(sk, &dp->dccps_xmit_timer);
1017
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001018 /*
1019 * We need to flush the recv. buffs. We do this only on the
1020 * descriptor close, not protocol-sourced closes, because the
1021 *reader process may not have drained the data yet!
1022 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001023 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001024 data_was_unread += skb->len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001025 __kfree_skb(skb);
1026 }
1027
Eric Dumazet346da622016-11-02 18:04:24 -07001028 /* If socket has been already reset kill it. */
1029 if (sk->sk_state == DCCP_CLOSED)
1030 goto adjudge_to_death;
1031
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001032 if (data_was_unread) {
1033 /* Unread data was tossed, send an appropriate Reset Code */
Gerrit Renker2f34b322010-10-11 20:44:42 +02001034 DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread);
Gerrit Renkerd83bd952007-12-16 16:06:03 -08001035 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
1036 dccp_set_state(sk, DCCP_CLOSED);
1037 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001038 /* Check zero linger _after_ checking for unread data. */
1039 sk->sk_prot->disconnect(sk, 0);
Gerrit Renker0c869622007-11-28 11:59:48 -02001040 } else if (sk->sk_state != DCCP_CLOSED) {
Gerrit Renkerb1fcf552010-10-27 19:16:27 +00001041 /*
1042 * Normal connection termination. May need to wait if there are
1043 * still packets in the TX queue that are delayed by the CCID.
1044 */
1045 dccp_flush_write_queue(sk, &timeout);
Gerrit Renker0c869622007-11-28 11:59:48 -02001046 dccp_terminate_connection(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001047 }
1048
Gerrit Renkerb1fcf552010-10-27 19:16:27 +00001049 /*
1050 * Flush write queue. This may be necessary in several cases:
1051 * - we have been closed by the peer but still have application data;
1052 * - abortive termination (unread data or zero linger time),
1053 * - normal termination but queue could not be flushed within time limit
1054 */
1055 __skb_queue_purge(&sk->sk_write_queue);
1056
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001057 sk_stream_wait_close(sk, timeout);
1058
1059adjudge_to_death:
Herbert Xu134af342006-05-05 17:09:13 -07001060 state = sk->sk_state;
1061 sock_hold(sk);
1062 sock_orphan(sk);
Herbert Xu134af342006-05-05 17:09:13 -07001063
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -07001064 /*
1065 * It is the last release_sock in its life. It will remove backlog.
1066 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001067 release_sock(sk);
1068 /*
1069 * Now socket is owned by kernel and we acquire BH lock
1070 * to finish close. No need to check for user refs.
1071 */
1072 local_bh_disable();
1073 bh_lock_sock(sk);
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001074 WARN_ON(sock_owned_by_user(sk));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001075
Herbert Xueb4dea52008-12-29 23:04:08 -08001076 percpu_counter_inc(sk->sk_prot->orphan_count);
1077
Herbert Xu134af342006-05-05 17:09:13 -07001078 /* Have we already been destroyed by a softirq or backlog? */
1079 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1080 goto out;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001081
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001082 if (sk->sk_state == DCCP_CLOSED)
1083 inet_csk_destroy_sock(sk);
1084
1085 /* Otherwise, socket is reprieved until protocol close. */
1086
Herbert Xu134af342006-05-05 17:09:13 -07001087out:
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001088 bh_unlock_sock(sk);
1089 local_bh_enable();
1090 sock_put(sk);
1091}
1092
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001093EXPORT_SYMBOL_GPL(dccp_close);
1094
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001095void dccp_shutdown(struct sock *sk, int how)
1096{
Gerrit Renker8e8c71f2007-11-21 09:56:48 -02001097 dccp_pr_debug("called shutdown(%x)\n", how);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001098}
1099
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001100EXPORT_SYMBOL_GPL(dccp_shutdown);
1101
Fabian Frederick0c5b8a42014-10-01 06:48:03 +02001102static inline int __init dccp_mib_init(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001103{
WANG Cong698365f2014-05-05 15:55:55 -07001104 dccp_statistics = alloc_percpu(struct dccp_mib);
1105 if (!dccp_statistics)
1106 return -ENOMEM;
1107 return 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001108}
1109
YOSHIFUJI Hideaki24e8b7e2008-04-10 03:48:43 -07001110static inline void dccp_mib_exit(void)
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001111{
WANG Cong698365f2014-05-05 15:55:55 -07001112 free_percpu(dccp_statistics);
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001113}
1114
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001115static int thash_entries;
1116module_param(thash_entries, int, 0444);
1117MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1118
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001119#ifdef CONFIG_IP_DCCP_DEBUG
Rusty Russelleb939922011-12-19 14:08:01 +00001120bool dccp_debug;
Gerrit Renker43264992008-08-23 13:28:27 +02001121module_param(dccp_debug, bool, 0644);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001122MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Melof21e68c2005-12-13 23:24:16 -08001123
1124EXPORT_SYMBOL_GPL(dccp_debug);
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -03001125#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001126
1127static int __init dccp_init(void)
1128{
1129 unsigned long goal;
Arun KSca79b0c2018-12-28 00:34:29 -08001130 unsigned long nr_pages = totalram_pages();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001131 int ehash_order, bhash_order, i;
Eric Dumazetdd24c002008-11-25 21:17:14 -08001132 int rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001133
Patrick McHardy028b0272008-04-12 18:35:41 -07001134 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001135 sizeof_field(struct sk_buff, cb));
Tejun Heo908c7f12014-09-08 09:51:29 +09001136 rc = percpu_counter_init(&dccp_orphan_count, 0, GFP_KERNEL);
Eric Dumazetdd24c002008-11-25 21:17:14 -08001137 if (rc)
Gerrit Renkerd14a0eb2010-03-14 20:13:19 +00001138 goto out_fail;
Eric Dumazet5caea4e2008-11-20 00:40:07 -08001139 inet_hashinfo_init(&dccp_hashinfo);
Peter Oskolkovc92c81d2018-12-24 12:57:17 -08001140 rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
1141 if (rc)
Wang Haic96b6ac2020-06-09 22:18:16 +08001142 goto out_free_percpu;
Peter Oskolkovc92c81d2018-12-24 12:57:17 -08001143 rc = -ENOBUFS;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001144 dccp_hashinfo.bind_bucket_cachep =
1145 kmem_cache_create("dccp_bind_bucket",
1146 sizeof(struct inet_bind_bucket), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001147 SLAB_HWCACHE_ALIGN, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001148 if (!dccp_hashinfo.bind_bucket_cachep)
Wang Haic96b6ac2020-06-09 22:18:16 +08001149 goto out_free_hashinfo2;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001150
1151 /*
1152 * Size and allocate the main established and bind bucket
1153 * hash tables.
1154 *
1155 * The methodology is similar to that of the buffer cache.
1156 */
Arun KS3d6357d2018-12-28 00:34:20 -08001157 if (nr_pages >= (128 * 1024))
1158 goal = nr_pages >> (21 - PAGE_SHIFT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001159 else
Arun KS3d6357d2018-12-28 00:34:20 -08001160 goal = nr_pages >> (23 - PAGE_SHIFT);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001161
1162 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001163 goal = (thash_entries *
1164 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001165 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1166 ;
1167 do {
Eric Dumazetf373b532009-10-09 00:16:19 +00001168 unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001169 sizeof(struct inet_ehash_bucket);
Eric Dumazetf373b532009-10-09 00:16:19 +00001170
1171 while (hash_size & (hash_size - 1))
1172 hash_size--;
1173 dccp_hashinfo.ehash_mask = hash_size - 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001174 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
Mel Gorman1c29b3f2009-07-29 15:04:10 -07001175 __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001176 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1177
1178 if (!dccp_hashinfo.ehash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001179 DCCP_CRIT("Failed to allocate DCCP established hash table");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001180 goto out_free_bind_bucket_cachep;
1181 }
1182
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001183 for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001184 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001185
Eric Dumazet230140c2007-11-07 02:40:20 -08001186 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1187 goto out_free_dccp_ehash;
1188
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001189 bhash_order = ehash_order;
1190
1191 do {
1192 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1193 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -03001194 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1195 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001196 continue;
1197 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
Mel Gorman1c29b3f2009-07-29 15:04:10 -07001198 __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001199 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1200
1201 if (!dccp_hashinfo.bhash) {
Gerrit Renker59348b12006-11-20 18:39:23 -02001202 DCCP_CRIT("Failed to allocate DCCP bind hash table");
Eric Dumazet230140c2007-11-07 02:40:20 -08001203 goto out_free_dccp_locks;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001204 }
1205
1206 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1207 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1208 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1209 }
1210
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001211 rc = dccp_mib_init();
Arnaldo Carvalho de Melofa23e2e2006-03-20 17:16:01 -08001212 if (rc)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001213 goto out_free_dccp_bhash;
1214
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001215 rc = dccp_ackvec_init();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001216 if (rc)
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001217 goto out_free_dccp_mib;
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001218
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001219 rc = dccp_sysctl_init();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001220 if (rc)
1221 goto out_ackvec_exit;
Gerrit Renker4c70f382007-09-25 22:40:13 -07001222
Gerrit Renkerddebc972009-01-04 21:42:53 -08001223 rc = ccid_initialize_builtins();
1224 if (rc)
1225 goto out_sysctl_exit;
1226
Gerrit Renker4c70f382007-09-25 22:40:13 -07001227 dccp_timestamping_init();
Gerrit Renkerd14a0eb2010-03-14 20:13:19 +00001228
1229 return 0;
1230
Gerrit Renkerddebc972009-01-04 21:42:53 -08001231out_sysctl_exit:
1232 dccp_sysctl_exit();
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001233out_ackvec_exit:
1234 dccp_ackvec_exit();
Arnaldo Carvalho de Melob61fafc2006-03-20 21:25:11 -08001235out_free_dccp_mib:
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001236 dccp_mib_exit();
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001237out_free_dccp_bhash:
1238 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
Eric Dumazet230140c2007-11-07 02:40:20 -08001239out_free_dccp_locks:
1240 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001241out_free_dccp_ehash:
1242 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001243out_free_bind_bucket_cachep:
1244 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Wang Haic96b6ac2020-06-09 22:18:16 +08001245out_free_hashinfo2:
1246 inet_hashinfo2_free_mod(&dccp_hashinfo);
Eric Dumazetdd24c002008-11-25 21:17:14 -08001247out_free_percpu:
1248 percpu_counter_destroy(&dccp_orphan_count);
Gerrit Renkerd14a0eb2010-03-14 20:13:19 +00001249out_fail:
1250 dccp_hashinfo.bhash = NULL;
1251 dccp_hashinfo.ehash = NULL;
1252 dccp_hashinfo.bind_bucket_cachep = NULL;
1253 return rc;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001254}
1255
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001256static void __exit dccp_fini(void)
1257{
Gerrit Renkerddebc972009-01-04 21:42:53 -08001258 ccid_cleanup_builtins();
Arnaldo Carvalho de Melo46f09ff2006-03-20 21:24:42 -08001259 dccp_mib_exit();
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001260 free_pages((unsigned long)dccp_hashinfo.bhash,
1261 get_order(dccp_hashinfo.bhash_size *
1262 sizeof(struct inet_bind_hashbucket)));
1263 free_pages((unsigned long)dccp_hashinfo.ehash,
Eric Dumazetf373b532009-10-09 00:16:19 +00001264 get_order((dccp_hashinfo.ehash_mask + 1) *
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001265 sizeof(struct inet_ehash_bucket)));
Eric Dumazet230140c2007-11-07 02:40:20 -08001266 inet_ehash_locks_free(&dccp_hashinfo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001267 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo9b07ef52006-03-20 17:16:17 -08001268 dccp_ackvec_exit();
Arnaldo Carvalho de Meloe55d9122006-03-20 19:25:02 -08001269 dccp_sysctl_exit();
Wang Haic96b6ac2020-06-09 22:18:16 +08001270 inet_hashinfo2_free_mod(&dccp_hashinfo);
Wei Yongjun476181c2009-08-04 21:44:39 +00001271 percpu_counter_destroy(&dccp_orphan_count);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001272}
1273
1274module_init(dccp_init);
1275module_exit(dccp_fini);
1276
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001277MODULE_LICENSE("GPL");
1278MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1279MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");