1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27c657876SArnaldo Carvalho de Melo /*
37c657876SArnaldo Carvalho de Melo * net/dccp/proto.c
47c657876SArnaldo Carvalho de Melo *
57c657876SArnaldo Carvalho de Melo * An implementation of the DCCP protocol
67c657876SArnaldo Carvalho de Melo * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
77c657876SArnaldo Carvalho de Melo */
87c657876SArnaldo Carvalho de Melo
97c657876SArnaldo Carvalho de Melo #include <linux/dccp.h>
107c657876SArnaldo Carvalho de Melo #include <linux/module.h>
117c657876SArnaldo Carvalho de Melo #include <linux/types.h>
127c657876SArnaldo Carvalho de Melo #include <linux/sched.h>
137c657876SArnaldo Carvalho de Melo #include <linux/kernel.h>
147c657876SArnaldo Carvalho de Melo #include <linux/skbuff.h>
157c657876SArnaldo Carvalho de Melo #include <linux/netdevice.h>
167c657876SArnaldo Carvalho de Melo #include <linux/in.h>
177c657876SArnaldo Carvalho de Melo #include <linux/if_arp.h>
187c657876SArnaldo Carvalho de Melo #include <linux/init.h>
197c657876SArnaldo Carvalho de Melo #include <linux/random.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
217c657876SArnaldo Carvalho de Melo #include <net/checksum.h>
227c657876SArnaldo Carvalho de Melo
2314c85021SArnaldo Carvalho de Melo #include <net/inet_sock.h>
24120e9dabSEric Dumazet #include <net/inet_common.h>
257c657876SArnaldo Carvalho de Melo #include <net/sock.h>
267c657876SArnaldo Carvalho de Melo #include <net/xfrm.h>
277c657876SArnaldo Carvalho de Melo
286273172eSArnaldo Carvalho de Melo #include <asm/ioctls.h>
297c657876SArnaldo Carvalho de Melo #include <linux/spinlock.h>
307c657876SArnaldo Carvalho de Melo #include <linux/timer.h>
317c657876SArnaldo Carvalho de Melo #include <linux/delay.h>
327c657876SArnaldo Carvalho de Melo #include <linux/poll.h>
337c657876SArnaldo Carvalho de Melo
347c657876SArnaldo Carvalho de Melo #include "ccid.h"
357c657876SArnaldo Carvalho de Melo #include "dccp.h"
36afe00251SAndrea Bittau #include "feat.h"
377c657876SArnaldo Carvalho de Melo
38ee549be6SMasami Hiramatsu #define CREATE_TRACE_POINTS
39ee549be6SMasami Hiramatsu #include "trace.h"
40ee549be6SMasami Hiramatsu
41ba89966cSEric Dumazet DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
427c657876SArnaldo Carvalho de Melo
43f21e68caSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_statistics);
44f21e68caSArnaldo Carvalho de Melo
4519757cebSEric Dumazet DEFINE_PER_CPU(unsigned int, dccp_orphan_count);
4619757cebSEric Dumazet EXPORT_PER_CPU_SYMBOL_GPL(dccp_orphan_count);
47f21e68caSArnaldo Carvalho de Melo
485caea4eaSEric Dumazet struct inet_hashinfo dccp_hashinfo;
49075ae866SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_hashinfo);
50075ae866SArnaldo Carvalho de Melo
51b1308dc0SIan McDonald /* the maximum queue length for tx in packets. 0 is no limit */
52b1308dc0SIan McDonald int sysctl_dccp_tx_qlen __read_mostly = 5;
53b1308dc0SIan McDonald
541f4f0f64Sstephen hemminger #ifdef CONFIG_IP_DCCP_DEBUG
dccp_state_name(const int state)551f4f0f64Sstephen hemminger static const char *dccp_state_name(const int state)
561f4f0f64Sstephen hemminger {
571f4f0f64Sstephen hemminger static const char *const dccp_state_names[] = {
581f4f0f64Sstephen hemminger [DCCP_OPEN] = "OPEN",
591f4f0f64Sstephen hemminger [DCCP_REQUESTING] = "REQUESTING",
601f4f0f64Sstephen hemminger [DCCP_PARTOPEN] = "PARTOPEN",
611f4f0f64Sstephen hemminger [DCCP_LISTEN] = "LISTEN",
621f4f0f64Sstephen hemminger [DCCP_RESPOND] = "RESPOND",
631f4f0f64Sstephen hemminger [DCCP_CLOSING] = "CLOSING",
641f4f0f64Sstephen hemminger [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
651f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
661f4f0f64Sstephen hemminger [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
671f4f0f64Sstephen hemminger [DCCP_TIME_WAIT] = "TIME_WAIT",
681f4f0f64Sstephen hemminger [DCCP_CLOSED] = "CLOSED",
691f4f0f64Sstephen hemminger };
701f4f0f64Sstephen hemminger
711f4f0f64Sstephen hemminger if (state >= DCCP_MAX_STATES)
721f4f0f64Sstephen hemminger return "INVALID STATE!";
731f4f0f64Sstephen hemminger else
741f4f0f64Sstephen hemminger return dccp_state_names[state];
751f4f0f64Sstephen hemminger }
761f4f0f64Sstephen hemminger #endif
771f4f0f64Sstephen hemminger
dccp_set_state(struct sock * sk,const int state)78c25a18baSArnaldo Carvalho de Melo void dccp_set_state(struct sock *sk, const int state)
79c25a18baSArnaldo Carvalho de Melo {
80c25a18baSArnaldo Carvalho de Melo const int oldstate = sk->sk_state;
81c25a18baSArnaldo Carvalho de Melo
82f11135a3SGerrit Renker dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
83c25a18baSArnaldo Carvalho de Melo dccp_state_name(oldstate), dccp_state_name(state));
84c25a18baSArnaldo Carvalho de Melo WARN_ON(state == oldstate);
85c25a18baSArnaldo Carvalho de Melo
86c25a18baSArnaldo Carvalho de Melo switch (state) {
87c25a18baSArnaldo Carvalho de Melo case DCCP_OPEN:
88c25a18baSArnaldo Carvalho de Melo if (oldstate != DCCP_OPEN)
89c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
906eb55d17SGerrit Renker /* Client retransmits all Confirm options until entering OPEN */
916eb55d17SGerrit Renker if (oldstate == DCCP_PARTOPEN)
926eb55d17SGerrit Renker dccp_feat_list_purge(&dccp_sk(sk)->dccps_featneg);
93c25a18baSArnaldo Carvalho de Melo break;
94c25a18baSArnaldo Carvalho de Melo
95c25a18baSArnaldo Carvalho de Melo case DCCP_CLOSED:
960c869620SGerrit Renker if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
970c869620SGerrit Renker oldstate == DCCP_CLOSING)
98c25a18baSArnaldo Carvalho de Melo DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
99c25a18baSArnaldo Carvalho de Melo
100c25a18baSArnaldo Carvalho de Melo sk->sk_prot->unhash(sk);
101c25a18baSArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL &&
102c25a18baSArnaldo Carvalho de Melo !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
103ab1e0a13SArnaldo Carvalho de Melo inet_put_port(sk);
104df561f66SGustavo A. R. Silva fallthrough;
105c25a18baSArnaldo Carvalho de Melo default:
106c25a18baSArnaldo Carvalho de Melo if (oldstate == DCCP_OPEN)
107c25a18baSArnaldo Carvalho de Melo DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
108c25a18baSArnaldo Carvalho de Melo }
109c25a18baSArnaldo Carvalho de Melo
110c25a18baSArnaldo Carvalho de Melo /* Change state AFTER socket is unhashed to avoid closed
111c25a18baSArnaldo Carvalho de Melo * socket sitting in hash tables.
112c25a18baSArnaldo Carvalho de Melo */
113b0832e30SYafang Shao inet_sk_set_state(sk, state);
114c25a18baSArnaldo Carvalho de Melo }
115c25a18baSArnaldo Carvalho de Melo
116c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_set_state);
117c25a18baSArnaldo Carvalho de Melo
dccp_finish_passive_close(struct sock * sk)1180c869620SGerrit Renker static void dccp_finish_passive_close(struct sock *sk)
1190c869620SGerrit Renker {
1200c869620SGerrit Renker switch (sk->sk_state) {
1210c869620SGerrit Renker case DCCP_PASSIVE_CLOSE:
1220c869620SGerrit Renker /* Node (client or server) has received Close packet. */
1230c869620SGerrit Renker dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
1240c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSED);
1250c869620SGerrit Renker break;
1260c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ:
1270c869620SGerrit Renker /*
1280c869620SGerrit Renker * Client received CloseReq. We set the `active' flag so that
1290c869620SGerrit Renker * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
1300c869620SGerrit Renker */
1310c869620SGerrit Renker dccp_send_close(sk, 1);
1320c869620SGerrit Renker dccp_set_state(sk, DCCP_CLOSING);
1330c869620SGerrit Renker }
1340c869620SGerrit Renker }
1350c869620SGerrit Renker
dccp_done(struct sock * sk)136c25a18baSArnaldo Carvalho de Melo void dccp_done(struct sock *sk)
137c25a18baSArnaldo Carvalho de Melo {
138c25a18baSArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED);
139c25a18baSArnaldo Carvalho de Melo dccp_clear_xmit_timers(sk);
140c25a18baSArnaldo Carvalho de Melo
141c25a18baSArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK;
142c25a18baSArnaldo Carvalho de Melo
143c25a18baSArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DEAD))
144c25a18baSArnaldo Carvalho de Melo sk->sk_state_change(sk);
145c25a18baSArnaldo Carvalho de Melo else
146c25a18baSArnaldo Carvalho de Melo inet_csk_destroy_sock(sk);
147c25a18baSArnaldo Carvalho de Melo }
148c25a18baSArnaldo Carvalho de Melo
149c25a18baSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_done);
150c25a18baSArnaldo Carvalho de Melo
dccp_packet_name(const int type)1517c657876SArnaldo Carvalho de Melo const char *dccp_packet_name(const int type)
1527c657876SArnaldo Carvalho de Melo {
15336cbd3dcSJan Engelhardt static const char *const dccp_packet_names[] = {
1547c657876SArnaldo Carvalho de Melo [DCCP_PKT_REQUEST] = "REQUEST",
1557c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESPONSE] = "RESPONSE",
1567c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATA] = "DATA",
1577c657876SArnaldo Carvalho de Melo [DCCP_PKT_ACK] = "ACK",
1587c657876SArnaldo Carvalho de Melo [DCCP_PKT_DATAACK] = "DATAACK",
1597c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
1607c657876SArnaldo Carvalho de Melo [DCCP_PKT_CLOSE] = "CLOSE",
1617c657876SArnaldo Carvalho de Melo [DCCP_PKT_RESET] = "RESET",
1627c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNC] = "SYNC",
1637c657876SArnaldo Carvalho de Melo [DCCP_PKT_SYNCACK] = "SYNCACK",
1647c657876SArnaldo Carvalho de Melo };
1657c657876SArnaldo Carvalho de Melo
1667c657876SArnaldo Carvalho de Melo if (type >= DCCP_NR_PKT_TYPES)
1677c657876SArnaldo Carvalho de Melo return "INVALID";
1687c657876SArnaldo Carvalho de Melo else
1697c657876SArnaldo Carvalho de Melo return dccp_packet_names[type];
1707c657876SArnaldo Carvalho de Melo }
1717c657876SArnaldo Carvalho de Melo
1727c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_packet_name);
1737c657876SArnaldo Carvalho de Melo
dccp_destruct_common(struct sock * sk)1741651951eSKuniyuki Iwashima void dccp_destruct_common(struct sock *sk)
175120e9dabSEric Dumazet {
176120e9dabSEric Dumazet struct dccp_sock *dp = dccp_sk(sk);
177120e9dabSEric Dumazet
178120e9dabSEric Dumazet ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
179120e9dabSEric Dumazet dp->dccps_hc_tx_ccid = NULL;
1801651951eSKuniyuki Iwashima }
1811651951eSKuniyuki Iwashima EXPORT_SYMBOL_GPL(dccp_destruct_common);
1821651951eSKuniyuki Iwashima
dccp_sk_destruct(struct sock * sk)1831651951eSKuniyuki Iwashima static void dccp_sk_destruct(struct sock *sk)
1841651951eSKuniyuki Iwashima {
1851651951eSKuniyuki Iwashima dccp_destruct_common(sk);
186120e9dabSEric Dumazet inet_sock_destruct(sk);
187120e9dabSEric Dumazet }
188120e9dabSEric Dumazet
dccp_init_sock(struct sock * sk,const __u8 ctl_sock_initialized)18972478873SArnaldo Carvalho de Melo int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
1903e0fadc5SArnaldo Carvalho de Melo {
1913e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
1923e0fadc5SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
1933e0fadc5SArnaldo Carvalho de Melo
194e18d7a98SArnaldo Carvalho de Melo pr_warn_once("DCCP is deprecated and scheduled to be removed in 2025, "
195e18d7a98SArnaldo Carvalho de Melo "please contact the netdev mailing list\n");
196e18d7a98SArnaldo Carvalho de Melo
197e18d7a98SArnaldo Carvalho de Melo icsk->icsk_rto = DCCP_TIMEOUT_INIT;
198120e9dabSEric Dumazet icsk->icsk_syn_retries = sysctl_dccp_request_retries;
199e18d7a98SArnaldo Carvalho de Melo sk->sk_state = DCCP_CLOSED;
200410e27a4SGerrit Renker sk->sk_write_space = dccp_write_space;
201e18d7a98SArnaldo Carvalho de Melo sk->sk_destruct = dccp_sk_destruct;
202e18d7a98SArnaldo Carvalho de Melo icsk->icsk_sync_mss = dccp_sync_mss;
203e18d7a98SArnaldo Carvalho de Melo dp->dccps_mss_cache = 536;
204871a2c16STomasz Grobelny dp->dccps_rate_last = jiffies;
205e18d7a98SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_UNDEFINED;
206e18d7a98SArnaldo Carvalho de Melo dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
207e18d7a98SArnaldo Carvalho de Melo dp->dccps_tx_qlen = sysctl_dccp_tx_qlen;
208ac75773cSGerrit Renker
2093e0fadc5SArnaldo Carvalho de Melo dccp_init_xmit_timers(sk);
2106eb55d17SGerrit Renker
2116eb55d17SGerrit Renker INIT_LIST_HEAD(&dp->dccps_featneg);
2123e0fadc5SArnaldo Carvalho de Melo /* control socket doesn't need feat nego */
2133e0fadc5SArnaldo Carvalho de Melo if (likely(ctl_sock_initialized))
2143e0fadc5SArnaldo Carvalho de Melo return dccp_feat_init(sk);
2153e0fadc5SArnaldo Carvalho de Melo return 0;
2163e0fadc5SArnaldo Carvalho de Melo }
2177d06b2e0SBrian Haley
2183e0fadc5SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_init_sock);
2193e0fadc5SArnaldo Carvalho de Melo
dccp_destroy_sock(struct sock * sk)2203e0fadc5SArnaldo Carvalho de Melo void dccp_destroy_sock(struct sock *sk)
2217749d4ffSEric Dumazet {
2223e0fadc5SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
2233e0fadc5SArnaldo Carvalho de Melo
2243e0fadc5SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_write_queue);
2253e0fadc5SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) {
2263e0fadc5SArnaldo Carvalho de Melo kfree_skb(sk->sk_send_head);
2273e0fadc5SArnaldo Carvalho de Melo sk->sk_send_head = NULL;
2283e0fadc5SArnaldo Carvalho de Melo }
229ab1e0a13SArnaldo Carvalho de Melo
2303e0fadc5SArnaldo Carvalho de Melo /* Clean up a referenced DCCP bind bucket. */
2313e0fadc5SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_bind_hash != NULL)
2323e0fadc5SArnaldo Carvalho de Melo inet_put_port(sk);
2333e0fadc5SArnaldo Carvalho de Melo
2346fdd34d4SGerrit Renker kfree(dp->dccps_service_list);
2353e0fadc5SArnaldo Carvalho de Melo dp->dccps_service_list = NULL;
2363e0fadc5SArnaldo Carvalho de Melo
2373e0fadc5SArnaldo Carvalho de Melo if (dp->dccps_hc_rx_ackvec != NULL) {
2383e0fadc5SArnaldo Carvalho de Melo dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
239120e9dabSEric Dumazet dp->dccps_hc_rx_ackvec = NULL;
2403e0fadc5SArnaldo Carvalho de Melo }
2413e0fadc5SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
242d99a7bd2SGerrit Renker dp->dccps_hc_rx_ccid = NULL;
2433e0fadc5SArnaldo Carvalho de Melo
2443e0fadc5SArnaldo Carvalho de Melo /* clean up feature negotiation state */
2453e0fadc5SArnaldo Carvalho de Melo dccp_feat_list_purge(&dp->dccps_featneg);
2463e0fadc5SArnaldo Carvalho de Melo }
247ce865a61SGerrit Renker
248ce865a61SGerrit Renker EXPORT_SYMBOL_GPL(dccp_destroy_sock);
249ce865a61SGerrit Renker
dccp_need_reset(int state)250ce865a61SGerrit Renker static inline int dccp_need_reset(int state)
251ce865a61SGerrit Renker {
252ce865a61SGerrit Renker return state != DCCP_CLOSED && state != DCCP_LISTEN &&
2537c657876SArnaldo Carvalho de Melo state != DCCP_REQUESTING;
2547c657876SArnaldo Carvalho de Melo }
2557c657876SArnaldo Carvalho de Melo
dccp_disconnect(struct sock * sk,int flags)2567c657876SArnaldo Carvalho de Melo int dccp_disconnect(struct sock *sk, int flags)
25769c64866SMohamed Ghannam {
2587c657876SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
2597c657876SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk);
2607c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
2617c657876SArnaldo Carvalho de Melo const int old_state = sk->sk_state;
2627c657876SArnaldo Carvalho de Melo
263ce865a61SGerrit Renker if (old_state != DCCP_CLOSED)
264ce865a61SGerrit Renker dccp_set_state(sk, DCCP_CLOSED);
265ce865a61SGerrit Renker
266ce865a61SGerrit Renker /*
2677c657876SArnaldo Carvalho de Melo * This corresponds to the ABORT function of RFC793, sec. 3.8
2687c657876SArnaldo Carvalho de Melo * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
269ce865a61SGerrit Renker */
270ce865a61SGerrit Renker if (old_state == DCCP_LISTEN) {
271ce865a61SGerrit Renker inet_csk_listen_stop(sk);
2727c657876SArnaldo Carvalho de Melo } else if (dccp_need_reset(old_state)) {
2737c657876SArnaldo Carvalho de Melo dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
2747c657876SArnaldo Carvalho de Melo sk->sk_err = ECONNRESET;
2757c657876SArnaldo Carvalho de Melo } else if (old_state == DCCP_REQUESTING)
27669c64866SMohamed Ghannam sk->sk_err = ECONNRESET;
27769c64866SMohamed Ghannam
27848816322SGerrit Renker dccp_clear_xmit_timers(sk);
2797c657876SArnaldo Carvalho de Melo ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
28048816322SGerrit Renker dp->dccps_hc_rx_ccid = NULL;
2817c657876SArnaldo Carvalho de Melo
2827c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_receive_queue);
2837c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_write_queue);
2847c657876SArnaldo Carvalho de Melo if (sk->sk_send_head != NULL) {
2857c657876SArnaldo Carvalho de Melo __kfree_skb(sk->sk_send_head);
286c720c7e8SEric Dumazet sk->sk_send_head = NULL;
2877c657876SArnaldo Carvalho de Melo }
288*e0833d1fSKuniyuki Iwashima
2897c657876SArnaldo Carvalho de Melo inet->inet_dport = 0;
2907c657876SArnaldo Carvalho de Melo
2917c657876SArnaldo Carvalho de Melo inet_bhash2_reset_saddr(sk);
2927c657876SArnaldo Carvalho de Melo
2937c657876SArnaldo Carvalho de Melo sk->sk_shutdown = 0;
2947c657876SArnaldo Carvalho de Melo sock_reset_flag(sk, SOCK_DONE);
2957c657876SArnaldo Carvalho de Melo
2967c657876SArnaldo Carvalho de Melo icsk->icsk_backoff = 0;
297c720c7e8SEric Dumazet inet_csk_delack_init(sk);
2987c657876SArnaldo Carvalho de Melo __sk_dst_reset(sk);
299e3ae2365SAlexander Aring
3003285a9aaSHariprasad Kelam WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
3017c657876SArnaldo Carvalho de Melo
3027c657876SArnaldo Carvalho de Melo sk_error_report(sk);
303f21e68caSArnaldo Carvalho de Melo return 0;
304f21e68caSArnaldo Carvalho de Melo }
305a11e1d43SLinus Torvalds
306a11e1d43SLinus Torvalds EXPORT_SYMBOL_GPL(dccp_disconnect);
307a11e1d43SLinus Torvalds
308a11e1d43SLinus Torvalds /*
309a11e1d43SLinus Torvalds * Wait for a DCCP event.
310a11e1d43SLinus Torvalds *
311a11e1d43SLinus Torvalds * Note that we don't need to lock the socket, as the upper poll layers
312a11e1d43SLinus Torvalds * take care of normal races (between the test and the event) and we don't
313a11e1d43SLinus Torvalds * go look at any of the socket buffers directly.
314331968bdSArnaldo Carvalho de Melo */
dccp_poll(struct file * file,struct socket * sock,poll_table * wait)315ade994f4SAl Viro __poll_t dccp_poll(struct file *file, struct socket *sock,
316331968bdSArnaldo Carvalho de Melo poll_table *wait)
317331968bdSArnaldo Carvalho de Melo {
31889ab066dSKarsten Graul struct sock *sk = sock->sk;
319331968bdSArnaldo Carvalho de Melo __poll_t mask;
320331968bdSArnaldo Carvalho de Melo u8 shutdown;
321331968bdSArnaldo Carvalho de Melo int state;
322331968bdSArnaldo Carvalho de Melo
323331968bdSArnaldo Carvalho de Melo sock_poll_wait(file, sock, wait);
324331968bdSArnaldo Carvalho de Melo
325331968bdSArnaldo Carvalho de Melo state = inet_sk_state_load(sk);
326331968bdSArnaldo Carvalho de Melo if (state == DCCP_LISTEN)
327331968bdSArnaldo Carvalho de Melo return inet_csk_listen_poll(sk);
328331968bdSArnaldo Carvalho de Melo
329a9a08845SLinus Torvalds /* Socket is not locked. We are protected from async events
330331968bdSArnaldo Carvalho de Melo by poll logic and correct handling of state changes
331331968bdSArnaldo Carvalho de Melo made by another threads is impossible in any case.
332a9a08845SLinus Torvalds */
333331968bdSArnaldo Carvalho de Melo
334a9a08845SLinus Torvalds mask = 0;
335331968bdSArnaldo Carvalho de Melo if (READ_ONCE(sk->sk_err))
336331968bdSArnaldo Carvalho de Melo mask = EPOLLERR;
337331968bdSArnaldo Carvalho de Melo shutdown = READ_ONCE(sk->sk_shutdown);
338331968bdSArnaldo Carvalho de Melo
339a9a08845SLinus Torvalds if (shutdown == SHUTDOWN_MASK || state == DCCP_CLOSED)
340331968bdSArnaldo Carvalho de Melo mask |= EPOLLHUP;
341331968bdSArnaldo Carvalho de Melo if (shutdown & RCV_SHUTDOWN)
34264dc6130SEric Dumazet mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
343a9a08845SLinus Torvalds
344331968bdSArnaldo Carvalho de Melo /* Connected? */
3459cd3e072SEric Dumazet if ((1 << state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
346331968bdSArnaldo Carvalho de Melo if (atomic_read(&sk->sk_rmem_alloc) > 0)
347331968bdSArnaldo Carvalho de Melo mask |= EPOLLIN | EPOLLRDNORM;
348331968bdSArnaldo Carvalho de Melo
349331968bdSArnaldo Carvalho de Melo if (!(shutdown & SEND_SHUTDOWN)) {
350331968bdSArnaldo Carvalho de Melo if (sk_stream_is_writeable(sk)) {
351331968bdSArnaldo Carvalho de Melo mask |= EPOLLOUT | EPOLLWRNORM;
35264dc6130SEric Dumazet } else { /* send SIGIO later */
353a9a08845SLinus Torvalds sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
354331968bdSArnaldo Carvalho de Melo set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
355331968bdSArnaldo Carvalho de Melo
356331968bdSArnaldo Carvalho de Melo /* Race breaker. If space is freed after
357331968bdSArnaldo Carvalho de Melo * wspace test but before the flags are set,
358331968bdSArnaldo Carvalho de Melo * IO signal will be lost.
359331968bdSArnaldo Carvalho de Melo */
360a11e1d43SLinus Torvalds if (sk_stream_is_writeable(sk))
361f21e68caSArnaldo Carvalho de Melo mask |= EPOLLOUT | EPOLLWRNORM;
3627c657876SArnaldo Carvalho de Melo }
3637c657876SArnaldo Carvalho de Melo }
3646273172eSArnaldo Carvalho de Melo }
3656273172eSArnaldo Carvalho de Melo return mask;
3666273172eSArnaldo Carvalho de Melo }
3676273172eSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_poll);
3686273172eSArnaldo Carvalho de Melo
dccp_ioctl(struct sock * sk,int cmd,int * karg)3696273172eSArnaldo Carvalho de Melo int dccp_ioctl(struct sock *sk, int cmd, int *karg)
3706273172eSArnaldo Carvalho de Melo {
3716273172eSArnaldo Carvalho de Melo int rc = -ENOTCONN;
372749c08f8SRichard Sailer
373749c08f8SRichard Sailer lock_sock(sk);
374749c08f8SRichard Sailer
375749c08f8SRichard Sailer if (sk->sk_state == DCCP_LISTEN)
376749c08f8SRichard Sailer goto out;
377749c08f8SRichard Sailer
378749c08f8SRichard Sailer switch (cmd) {
379749c08f8SRichard Sailer case SIOCOUTQ: {
380749c08f8SRichard Sailer *karg = sk_wmem_alloc_get(sk);
3816273172eSArnaldo Carvalho de Melo /* Using sk_wmem_alloc here because sk_wmem_queued is not used by DCCP and
3826273172eSArnaldo Carvalho de Melo * always 0, comparably to UDP.
3836273172eSArnaldo Carvalho de Melo */
3846273172eSArnaldo Carvalho de Melo
3856273172eSArnaldo Carvalho de Melo rc = 0;
3866273172eSArnaldo Carvalho de Melo }
3876273172eSArnaldo Carvalho de Melo break;
3886273172eSArnaldo Carvalho de Melo case SIOCINQ: {
3896273172eSArnaldo Carvalho de Melo struct sk_buff *skb;
3906273172eSArnaldo Carvalho de Melo *karg = 0;
3916273172eSArnaldo Carvalho de Melo
3926273172eSArnaldo Carvalho de Melo skb = skb_peek(&sk->sk_receive_queue);
3936273172eSArnaldo Carvalho de Melo if (skb != NULL) {
3946273172eSArnaldo Carvalho de Melo /*
3956273172eSArnaldo Carvalho de Melo * We will only return the amount of this packet since
3966273172eSArnaldo Carvalho de Melo * that is all that will be read.
3976273172eSArnaldo Carvalho de Melo */
3986273172eSArnaldo Carvalho de Melo *karg = skb->len;
3996273172eSArnaldo Carvalho de Melo }
4006273172eSArnaldo Carvalho de Melo rc = 0;
4016273172eSArnaldo Carvalho de Melo }
4026273172eSArnaldo Carvalho de Melo break;
4037c657876SArnaldo Carvalho de Melo default:
4047c657876SArnaldo Carvalho de Melo rc = -ENOIOCTLCMD;
405f21e68caSArnaldo Carvalho de Melo break;
406f21e68caSArnaldo Carvalho de Melo }
40760fe62e7SAndrea Bittau out:
408a7b75c5aSChristoph Hellwig release_sock(sk);
40967e6b629SArnaldo Carvalho de Melo return rc;
41067e6b629SArnaldo Carvalho de Melo }
41167e6b629SArnaldo Carvalho de Melo
41267e6b629SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_ioctl);
41367e6b629SArnaldo Carvalho de Melo
dccp_setsockopt_service(struct sock * sk,const __be32 service,sockptr_t optval,unsigned int optlen)41467e6b629SArnaldo Carvalho de Melo static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
41567e6b629SArnaldo Carvalho de Melo sockptr_t optval, unsigned int optlen)
41667e6b629SArnaldo Carvalho de Melo {
41767e6b629SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
41867e6b629SArnaldo Carvalho de Melo struct dccp_service_list *sl = NULL;
41967e6b629SArnaldo Carvalho de Melo
42067e6b629SArnaldo Carvalho de Melo if (service == DCCP_SERVICE_INVALID_VALUE ||
42167e6b629SArnaldo Carvalho de Melo optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
42267e6b629SArnaldo Carvalho de Melo return -EINVAL;
423d3c48151SChristoph Hellwig
424d3c48151SChristoph Hellwig if (optlen > sizeof(service)) {
42567e6b629SArnaldo Carvalho de Melo sl = kmalloc(optlen, GFP_KERNEL);
42667e6b629SArnaldo Carvalho de Melo if (sl == NULL)
42767e6b629SArnaldo Carvalho de Melo return -ENOMEM;
42867e6b629SArnaldo Carvalho de Melo
42967e6b629SArnaldo Carvalho de Melo sl->dccpsl_nr = optlen / sizeof(u32) - 1;
43067e6b629SArnaldo Carvalho de Melo if (copy_from_sockptr_offset(sl->dccpsl_list, optval,
43167e6b629SArnaldo Carvalho de Melo sizeof(service), optlen - sizeof(service)) ||
43267e6b629SArnaldo Carvalho de Melo dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
43367e6b629SArnaldo Carvalho de Melo kfree(sl);
43467e6b629SArnaldo Carvalho de Melo return -EFAULT;
43567e6b629SArnaldo Carvalho de Melo }
43667e6b629SArnaldo Carvalho de Melo }
43767e6b629SArnaldo Carvalho de Melo
43867e6b629SArnaldo Carvalho de Melo lock_sock(sk);
43967e6b629SArnaldo Carvalho de Melo dp->dccps_service = service;
44067e6b629SArnaldo Carvalho de Melo
44129450559SGerrit Renker kfree(dp->dccps_service_list);
44229450559SGerrit Renker
44329450559SGerrit Renker dp->dccps_service_list = sl;
44429450559SGerrit Renker release_sock(sk);
44529450559SGerrit Renker return 0;
44629450559SGerrit Renker }
44729450559SGerrit Renker
dccp_setsockopt_cscov(struct sock * sk,int cscov,bool rx)44829450559SGerrit Renker static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
44929450559SGerrit Renker {
45029450559SGerrit Renker u8 *list, len;
45129450559SGerrit Renker int i, rc;
45229450559SGerrit Renker
45329450559SGerrit Renker if (cscov < 0 || cscov > 15)
45429450559SGerrit Renker return -EINVAL;
45529450559SGerrit Renker /*
45629450559SGerrit Renker * Populate a list of permissible values, in the range cscov...15. This
45729450559SGerrit Renker * is necessary since feature negotiation of single values only works if
45829450559SGerrit Renker * both sides incidentally choose the same value. Since the list starts
45929450559SGerrit Renker * lowest-value first, negotiation will pick the smallest shared value.
46029450559SGerrit Renker */
46129450559SGerrit Renker if (cscov == 0)
46229450559SGerrit Renker return 0;
46329450559SGerrit Renker len = 16 - cscov;
46429450559SGerrit Renker
46529450559SGerrit Renker list = kmalloc(len, GFP_KERNEL);
46629450559SGerrit Renker if (list == NULL)
46729450559SGerrit Renker return -ENOBUFS;
46829450559SGerrit Renker
46929450559SGerrit Renker for (i = 0; i < len; i++)
47029450559SGerrit Renker list[i] = cscov++;
47129450559SGerrit Renker
47229450559SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
47329450559SGerrit Renker
47429450559SGerrit Renker if (rc == 0) {
47529450559SGerrit Renker if (rx)
47629450559SGerrit Renker dccp_sk(sk)->dccps_pcrlen = cscov;
477b20a9c24SGerrit Renker else
478a7b75c5aSChristoph Hellwig dccp_sk(sk)->dccps_pcslen = cscov;
479b20a9c24SGerrit Renker }
480b20a9c24SGerrit Renker kfree(list);
481b20a9c24SGerrit Renker return rc;
482b20a9c24SGerrit Renker }
483b20a9c24SGerrit Renker
dccp_setsockopt_ccid(struct sock * sk,int type,sockptr_t optval,unsigned int optlen)484b20a9c24SGerrit Renker static int dccp_setsockopt_ccid(struct sock *sk, int type,
485b20a9c24SGerrit Renker sockptr_t optval, unsigned int optlen)
486a7b75c5aSChristoph Hellwig {
487042604d2SJulia Lawall u8 *val;
488042604d2SJulia Lawall int rc = 0;
489b20a9c24SGerrit Renker
490b20a9c24SGerrit Renker if (optlen < 1 || optlen > DCCP_FEAT_MAX_SP_VALS)
491b20a9c24SGerrit Renker return -EINVAL;
492b20a9c24SGerrit Renker
493b20a9c24SGerrit Renker val = memdup_sockptr(optval, optlen);
494b20a9c24SGerrit Renker if (IS_ERR(val))
495b20a9c24SGerrit Renker return PTR_ERR(val);
496b20a9c24SGerrit Renker
497b20a9c24SGerrit Renker lock_sock(sk);
498b20a9c24SGerrit Renker if (type == DCCP_SOCKOPT_TX_CCID || type == DCCP_SOCKOPT_CCID)
499b20a9c24SGerrit Renker rc = dccp_feat_register_sp(sk, DCCPF_CCID, 1, val, optlen);
500b20a9c24SGerrit Renker
501b20a9c24SGerrit Renker if (!rc && (type == DCCP_SOCKOPT_RX_CCID || type == DCCP_SOCKOPT_CCID))
5023fdadf7dSDmitry Mishin rc = dccp_feat_register_sp(sk, DCCPF_CCID, 0, val, optlen);
503a7b75c5aSChristoph Hellwig release_sock(sk);
5047c657876SArnaldo Carvalho de Melo
50509dbc389SGerrit Renker kfree(val);
50609dbc389SGerrit Renker return rc;
5077c657876SArnaldo Carvalho de Melo }
50819102996SGerrit Renker
do_dccp_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)50919102996SGerrit Renker static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
51019102996SGerrit Renker sockptr_t optval, unsigned int optlen)
51119102996SGerrit Renker {
51219102996SGerrit Renker struct dccp_sock *dp = dccp_sk(sk);
51319102996SGerrit Renker int val, err = 0;
51419102996SGerrit Renker
51519102996SGerrit Renker switch (optname) {
516b20a9c24SGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE:
517b20a9c24SGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
518b20a9c24SGerrit Renker return 0;
519b20a9c24SGerrit Renker case DCCP_SOCKOPT_CHANGE_L:
52019102996SGerrit Renker case DCCP_SOCKOPT_CHANGE_R:
52119102996SGerrit Renker DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
52219102996SGerrit Renker return 0;
523a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_CCID:
524a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_RX_CCID:
525a7b75c5aSChristoph Hellwig case DCCP_SOCKOPT_TX_CCID:
526a84ffe43SArnaldo Carvalho de Melo return dccp_setsockopt_ccid(sk, optname, optval, optlen);
527a84ffe43SArnaldo Carvalho de Melo }
52867e6b629SArnaldo Carvalho de Melo
52967e6b629SArnaldo Carvalho de Melo if (optlen < (int)sizeof(int))
530a84ffe43SArnaldo Carvalho de Melo return -EINVAL;
53167e6b629SArnaldo Carvalho de Melo
532a84ffe43SArnaldo Carvalho de Melo if (copy_from_sockptr(&val, optval, sizeof(int)))
533b8599d20SGerrit Renker return -EFAULT;
534b8599d20SGerrit Renker
535b8599d20SGerrit Renker if (optname == DCCP_SOCKOPT_SERVICE)
536b8599d20SGerrit Renker return dccp_setsockopt_service(sk, val, optval, optlen);
537b8599d20SGerrit Renker
538b8599d20SGerrit Renker lock_sock(sk);
53929450559SGerrit Renker switch (optname) {
54029450559SGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT:
541d6da3511STomasz Grobelny if (dp->dccps_role != DCCP_ROLE_SERVER)
54229450559SGerrit Renker err = -EOPNOTSUPP;
54329450559SGerrit Renker else
544d6da3511STomasz Grobelny dp->dccps_server_timewait = (val != 0);
545871a2c16STomasz Grobelny break;
546871a2c16STomasz Grobelny case DCCP_SOCKOPT_SEND_CSCOV:
547871a2c16STomasz Grobelny err = dccp_setsockopt_cscov(sk, val, false);
548871a2c16STomasz Grobelny break;
549871a2c16STomasz Grobelny case DCCP_SOCKOPT_RECV_CSCOV:
550871a2c16STomasz Grobelny err = dccp_setsockopt_cscov(sk, val, true);
551871a2c16STomasz Grobelny break;
552871a2c16STomasz Grobelny case DCCP_SOCKOPT_QPOLICY_ID:
553871a2c16STomasz Grobelny if (sk->sk_state != DCCP_CLOSED)
554871a2c16STomasz Grobelny err = -EISCONN;
555871a2c16STomasz Grobelny else if (val < 0 || val >= DCCPQ_POLICY_MAX)
556871a2c16STomasz Grobelny err = -EINVAL;
557871a2c16STomasz Grobelny else
558871a2c16STomasz Grobelny dp->dccps_qpolicy = val;
559a84ffe43SArnaldo Carvalho de Melo break;
560a84ffe43SArnaldo Carvalho de Melo case DCCP_SOCKOPT_QPOLICY_TXQLEN:
561a84ffe43SArnaldo Carvalho de Melo if (val < 0)
562a84ffe43SArnaldo Carvalho de Melo err = -EINVAL;
563410e27a4SGerrit Renker else
56419102996SGerrit Renker dp->dccps_tx_qlen = val;
565a84ffe43SArnaldo Carvalho de Melo break;
5667c657876SArnaldo Carvalho de Melo default:
5677c657876SArnaldo Carvalho de Melo err = -ENOPROTOOPT;
568a7b75c5aSChristoph Hellwig break;
569a7b75c5aSChristoph Hellwig }
5703fdadf7dSDmitry Mishin release_sock(sk);
5713fdadf7dSDmitry Mishin
5723fdadf7dSDmitry Mishin return err;
5733fdadf7dSDmitry Mishin }
5743fdadf7dSDmitry Mishin
dccp_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)5753fdadf7dSDmitry Mishin int dccp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
5763fdadf7dSDmitry Mishin unsigned int optlen)
577543d9cfeSArnaldo Carvalho de Melo {
578f21e68caSArnaldo Carvalho de Melo if (level != SOL_DCCP)
579f21e68caSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
58067e6b629SArnaldo Carvalho de Melo optname, optval,
58160fe62e7SAndrea Bittau optlen);
58267e6b629SArnaldo Carvalho de Melo return do_dccp_setsockopt(sk, level, optname, optval, optlen);
58367e6b629SArnaldo Carvalho de Melo }
58467e6b629SArnaldo Carvalho de Melo
58567e6b629SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_setsockopt);
58667e6b629SArnaldo Carvalho de Melo
dccp_getsockopt_service(struct sock * sk,int len,__be32 __user * optval,int __user * optlen)58767e6b629SArnaldo Carvalho de Melo static int dccp_getsockopt_service(struct sock *sk, int len,
58867e6b629SArnaldo Carvalho de Melo __be32 __user *optval,
58967e6b629SArnaldo Carvalho de Melo int __user *optlen)
59067e6b629SArnaldo Carvalho de Melo {
59167e6b629SArnaldo Carvalho de Melo const struct dccp_sock *dp = dccp_sk(sk);
59267e6b629SArnaldo Carvalho de Melo const struct dccp_service_list *sl;
59367e6b629SArnaldo Carvalho de Melo int err = -ENOENT, slen = 0, total_len = sizeof(u32);
59467e6b629SArnaldo Carvalho de Melo
59567e6b629SArnaldo Carvalho de Melo lock_sock(sk);
59667e6b629SArnaldo Carvalho de Melo if ((sl = dp->dccps_service_list) != NULL) {
59767e6b629SArnaldo Carvalho de Melo slen = sl->dccpsl_nr * sizeof(u32);
59867e6b629SArnaldo Carvalho de Melo total_len += slen;
59967e6b629SArnaldo Carvalho de Melo }
60067e6b629SArnaldo Carvalho de Melo
60167e6b629SArnaldo Carvalho de Melo err = -EINVAL;
60267e6b629SArnaldo Carvalho de Melo if (total_len > len)
60367e6b629SArnaldo Carvalho de Melo goto out;
60467e6b629SArnaldo Carvalho de Melo
60567e6b629SArnaldo Carvalho de Melo err = 0;
60667e6b629SArnaldo Carvalho de Melo if (put_user(total_len, optlen) ||
60767e6b629SArnaldo Carvalho de Melo put_user(dp->dccps_service, optval) ||
6083fdadf7dSDmitry Mishin (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
609a1d3a355SArnaldo Carvalho de Melo err = -EFAULT;
6107c657876SArnaldo Carvalho de Melo out:
611a84ffe43SArnaldo Carvalho de Melo release_sock(sk);
612a84ffe43SArnaldo Carvalho de Melo return err;
6137c657876SArnaldo Carvalho de Melo }
614a84ffe43SArnaldo Carvalho de Melo
do_dccp_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)615a84ffe43SArnaldo Carvalho de Melo static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
616a84ffe43SArnaldo Carvalho de Melo char __user *optval, int __user *optlen)
61739ebc027SArnaldo Carvalho de Melo {
618a84ffe43SArnaldo Carvalho de Melo struct dccp_sock *dp;
619a84ffe43SArnaldo Carvalho de Melo int val, len;
620a84ffe43SArnaldo Carvalho de Melo
621a84ffe43SArnaldo Carvalho de Melo if (get_user(len, optlen))
622a84ffe43SArnaldo Carvalho de Melo return -EFAULT;
623a84ffe43SArnaldo Carvalho de Melo
6245aed3243SGerrit Renker if (len < (int)sizeof(int))
625841bac1dSArnaldo Carvalho de Melo return -EINVAL;
62688f964dbSArnaldo Carvalho de Melo
62788f964dbSArnaldo Carvalho de Melo dp = dccp_sk(sk);
62860fe62e7SAndrea Bittau
6297c559a9eSGerrit Renker switch (optname) {
6307c559a9eSGerrit Renker case DCCP_SOCKOPT_PACKET_SIZE:
6317c559a9eSGerrit Renker DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
632d90ebcbfSGerrit Renker return 0;
633d90ebcbfSGerrit Renker case DCCP_SOCKOPT_SERVICE:
63471c262a3SGerrit Renker return dccp_getsockopt_service(sk, len,
63571c262a3SGerrit Renker (__be32 __user *)optval, optlen);
63671c262a3SGerrit Renker case DCCP_SOCKOPT_GET_CUR_MPS:
63771c262a3SGerrit Renker val = READ_ONCE(dp->dccps_mss_cache);
63871c262a3SGerrit Renker break;
63971c262a3SGerrit Renker case DCCP_SOCKOPT_AVAILABLE_CCIDS:
64071c262a3SGerrit Renker return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
64171c262a3SGerrit Renker case DCCP_SOCKOPT_TX_CCID:
64271c262a3SGerrit Renker val = ccid_get_current_tx_ccid(dp);
64371c262a3SGerrit Renker if (val < 0)
644b8599d20SGerrit Renker return -ENOPROTOOPT;
645b8599d20SGerrit Renker break;
646b8599d20SGerrit Renker case DCCP_SOCKOPT_RX_CCID:
6476f4e5fffSGerrit Renker val = ccid_get_current_rx_ccid(dp);
6486f4e5fffSGerrit Renker if (val < 0)
6496f4e5fffSGerrit Renker return -ENOPROTOOPT;
6506f4e5fffSGerrit Renker break;
6516f4e5fffSGerrit Renker case DCCP_SOCKOPT_SERVER_TIMEWAIT:
6526f4e5fffSGerrit Renker val = dp->dccps_server_timewait;
653871a2c16STomasz Grobelny break;
654871a2c16STomasz Grobelny case DCCP_SOCKOPT_SEND_CSCOV:
655871a2c16STomasz Grobelny val = dp->dccps_pcslen;
656871a2c16STomasz Grobelny break;
657871a2c16STomasz Grobelny case DCCP_SOCKOPT_RECV_CSCOV:
658871a2c16STomasz Grobelny val = dp->dccps_pcrlen;
65988f964dbSArnaldo Carvalho de Melo break;
66088f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_QPOLICY_ID:
66188f964dbSArnaldo Carvalho de Melo val = dp->dccps_qpolicy;
66288f964dbSArnaldo Carvalho de Melo break;
66388f964dbSArnaldo Carvalho de Melo case DCCP_SOCKOPT_QPOLICY_TXQLEN:
66488f964dbSArnaldo Carvalho de Melo val = dp->dccps_tx_qlen;
665a84ffe43SArnaldo Carvalho de Melo break;
666a84ffe43SArnaldo Carvalho de Melo case 128 ... 191:
667a84ffe43SArnaldo Carvalho de Melo return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
668a84ffe43SArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen);
66979133506SGerrit Renker case 192 ... 255:
670a84ffe43SArnaldo Carvalho de Melo return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
671a84ffe43SArnaldo Carvalho de Melo len, (u32 __user *)optval, optlen);
672a84ffe43SArnaldo Carvalho de Melo default:
673a84ffe43SArnaldo Carvalho de Melo return -ENOPROTOOPT;
6747c657876SArnaldo Carvalho de Melo }
6757c657876SArnaldo Carvalho de Melo
6763fdadf7dSDmitry Mishin len = sizeof(val);
6773fdadf7dSDmitry Mishin if (put_user(len, optlen) || copy_to_user(optval, &val, len))
6783fdadf7dSDmitry Mishin return -EFAULT;
6793fdadf7dSDmitry Mishin
6803fdadf7dSDmitry Mishin return 0;
6813fdadf7dSDmitry Mishin }
6823fdadf7dSDmitry Mishin
dccp_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)6833fdadf7dSDmitry Mishin int dccp_getsockopt(struct sock *sk, int level, int optname,
6843fdadf7dSDmitry Mishin char __user *optval, int __user *optlen)
685543d9cfeSArnaldo Carvalho de Melo {
686f21e68caSArnaldo Carvalho de Melo if (level != SOL_DCCP)
687f21e68caSArnaldo Carvalho de Melo return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
688871a2c16STomasz Grobelny optname, optval,
689871a2c16STomasz Grobelny optlen);
690f95b414eSGu Zheng return do_dccp_getsockopt(sk, level, optname, optval, optlen);
691871a2c16STomasz Grobelny }
692871a2c16STomasz Grobelny
693871a2c16STomasz Grobelny EXPORT_SYMBOL_GPL(dccp_getsockopt);
694871a2c16STomasz Grobelny
dccp_msghdr_parse(struct msghdr * msg,struct sk_buff * skb)695871a2c16STomasz Grobelny static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
696871a2c16STomasz Grobelny {
697871a2c16STomasz Grobelny struct cmsghdr *cmsg;
698871a2c16STomasz Grobelny
699871a2c16STomasz Grobelny /*
700871a2c16STomasz Grobelny * Assign an (opaque) qpolicy priority value to skb->priority.
701871a2c16STomasz Grobelny *
702871a2c16STomasz Grobelny * We are overloading this skb field for use with the qpolicy subystem.
703871a2c16STomasz Grobelny * The skb->priority is normally used for the SO_PRIORITY option, which
704f95b414eSGu Zheng * is initialised from sk_priority. Since the assignment of sk_priority
705871a2c16STomasz Grobelny * to skb->priority happens later (on layer 3), we overload this field
706871a2c16STomasz Grobelny * for use with queueing priorities as long as the skb is on layer 4.
707871a2c16STomasz Grobelny * The default priority value (if nothing is set) is 0.
708871a2c16STomasz Grobelny */
709871a2c16STomasz Grobelny skb->priority = 0;
710871a2c16STomasz Grobelny
71104910265STomasz Grobelny for_each_cmsghdr(cmsg, msg) {
71204910265STomasz Grobelny if (!CMSG_OK(msg, cmsg))
71304910265STomasz Grobelny return -EINVAL;
71404910265STomasz Grobelny
715871a2c16STomasz Grobelny if (cmsg->cmsg_level != SOL_DCCP)
716871a2c16STomasz Grobelny continue;
717871a2c16STomasz Grobelny
718871a2c16STomasz Grobelny if (cmsg->cmsg_type <= DCCP_SCM_QPOLICY_MAX &&
719871a2c16STomasz Grobelny !dccp_qpolicy_param_ok(skb->sk, cmsg->cmsg_type))
720871a2c16STomasz Grobelny return -EINVAL;
721871a2c16STomasz Grobelny
722871a2c16STomasz Grobelny switch (cmsg->cmsg_type) {
723871a2c16STomasz Grobelny case DCCP_SCM_PRIORITY:
724871a2c16STomasz Grobelny if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u32)))
725871a2c16STomasz Grobelny return -EINVAL;
726871a2c16STomasz Grobelny skb->priority = *(__u32 *)CMSG_DATA(cmsg);
727871a2c16STomasz Grobelny break;
7281b784140SYing Xue default:
7297c657876SArnaldo Carvalho de Melo return -EINVAL;
7307c657876SArnaldo Carvalho de Melo }
7317c657876SArnaldo Carvalho de Melo }
7327c657876SArnaldo Carvalho de Melo return 0;
7337c657876SArnaldo Carvalho de Melo }
7347c657876SArnaldo Carvalho de Melo
dccp_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)7357c657876SArnaldo Carvalho de Melo int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7367c657876SArnaldo Carvalho de Melo {
737ee549be6SMasami Hiramatsu const struct dccp_sock *dp = dccp_sk(sk);
738ee549be6SMasami Hiramatsu const int flags = msg->msg_flags;
7397c657876SArnaldo Carvalho de Melo const int noblock = flags & MSG_DONTWAIT;
7407c657876SArnaldo Carvalho de Melo struct sk_buff *skb;
7417c657876SArnaldo Carvalho de Melo int rc, size;
7427c657876SArnaldo Carvalho de Melo long timeo;
743b1308dc0SIan McDonald
74427258ee5SArnaldo Carvalho de Melo trace_dccp_probe(sk, len);
7457c657876SArnaldo Carvalho de Melo
7467c657876SArnaldo Carvalho de Melo if (len > READ_ONCE(dp->dccps_mss_cache))
7477c657876SArnaldo Carvalho de Melo return -EMSGSIZE;
7487c657876SArnaldo Carvalho de Melo
7497c657876SArnaldo Carvalho de Melo lock_sock(sk);
7507c657876SArnaldo Carvalho de Melo
751cecd8d0eSGerrit Renker timeo = sock_sndtimeo(sk, noblock);
7527c657876SArnaldo Carvalho de Melo
75327258ee5SArnaldo Carvalho de Melo /*
7547c657876SArnaldo Carvalho de Melo * We have to use sk_stream_wait_connect here to set sk_write_pending,
7557c657876SArnaldo Carvalho de Melo * so that the trick in dccp_rcv_request_sent_state_process.
7567c657876SArnaldo Carvalho de Melo */
7577c657876SArnaldo Carvalho de Melo /* Wait for a connection to finish. */
7587c657876SArnaldo Carvalho de Melo if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
7597c657876SArnaldo Carvalho de Melo if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
7607c657876SArnaldo Carvalho de Melo goto out_release;
7617c657876SArnaldo Carvalho de Melo
762a41b17ffSHangyu Hua size = sk->sk_prot->max_header + len;
763a41b17ffSHangyu Hua release_sock(sk);
764a41b17ffSHangyu Hua skb = sock_alloc_send_skb(sk, size, noblock, &rc);
765a41b17ffSHangyu Hua lock_sock(sk);
766a41b17ffSHangyu Hua if (skb == NULL)
76767f93df7SAlexey Kodanev goto out_release;
76867f93df7SAlexey Kodanev
76967f93df7SAlexey Kodanev if (dccp_qpolicy_full(sk)) {
77067f93df7SAlexey Kodanev rc = -EAGAIN;
77167f93df7SAlexey Kodanev goto out_discard;
7727c657876SArnaldo Carvalho de Melo }
7736ce8e9ceSAl Viro
77427258ee5SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) {
77527258ee5SArnaldo Carvalho de Melo rc = -ENOTCONN;
7767c657876SArnaldo Carvalho de Melo goto out_discard;
777871a2c16STomasz Grobelny }
778871a2c16STomasz Grobelny
779871a2c16STomasz Grobelny /* We need to check dccps_mss_cache after socket is locked. */
780871a2c16STomasz Grobelny if (len > dp->dccps_mss_cache) {
781871a2c16STomasz Grobelny rc = -EMSGSIZE;
782b1fcf55eSGerrit Renker goto out_discard;
783b1fcf55eSGerrit Renker }
784b1fcf55eSGerrit Renker
785b1fcf55eSGerrit Renker skb_reserve(skb, sk->sk_prot->max_header);
786b1fcf55eSGerrit Renker rc = memcpy_from_msg(skb_put(skb, len), msg, len);
787b1fcf55eSGerrit Renker if (rc != 0)
788b1fcf55eSGerrit Renker goto out_discard;
7897c657876SArnaldo Carvalho de Melo
7907c657876SArnaldo Carvalho de Melo rc = dccp_msghdr_parse(msg, skb);
7917c657876SArnaldo Carvalho de Melo if (rc != 0)
79227258ee5SArnaldo Carvalho de Melo goto out_discard;
79327258ee5SArnaldo Carvalho de Melo
7947c657876SArnaldo Carvalho de Melo dccp_qpolicy_push(sk, skb);
7957c657876SArnaldo Carvalho de Melo /*
7967c657876SArnaldo Carvalho de Melo * The xmit_timer is set if the TX CCID is rate-based and will expire
797f21e68caSArnaldo Carvalho de Melo * when congestion control permits to release further packets into the
798f21e68caSArnaldo Carvalho de Melo * network. Window-based CCIDs do not use this timer.
799ec095263SOliver Hartkopp */
800ec095263SOliver Hartkopp if (!timer_pending(&dp->dccps_xmit_timer))
8017c657876SArnaldo Carvalho de Melo dccp_write_xmit(sk);
8027c657876SArnaldo Carvalho de Melo out_release:
8037c657876SArnaldo Carvalho de Melo release_sock(sk);
8047c657876SArnaldo Carvalho de Melo return rc ? : len;
8057c657876SArnaldo Carvalho de Melo out_discard:
8067c657876SArnaldo Carvalho de Melo kfree_skb(skb);
807531669a0SArnaldo Carvalho de Melo goto out_release;
808531669a0SArnaldo Carvalho de Melo }
8097c657876SArnaldo Carvalho de Melo
810531669a0SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_sendmsg);
8117c657876SArnaldo Carvalho de Melo
dccp_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)812ec095263SOliver Hartkopp int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int flags,
8137c657876SArnaldo Carvalho de Melo int *addr_len)
8147c657876SArnaldo Carvalho de Melo {
815531669a0SArnaldo Carvalho de Melo const struct dccp_hdr *dh;
8167c657876SArnaldo Carvalho de Melo long timeo;
817531669a0SArnaldo Carvalho de Melo
818531669a0SArnaldo Carvalho de Melo lock_sock(sk);
8197c657876SArnaldo Carvalho de Melo
8207c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) {
8217c657876SArnaldo Carvalho de Melo len = -ENOTCONN;
8220c869620SGerrit Renker goto out;
8230c869620SGerrit Renker }
8240c869620SGerrit Renker
8257c657876SArnaldo Carvalho de Melo timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
8267c657876SArnaldo Carvalho de Melo
8270c869620SGerrit Renker do {
8280c869620SGerrit Renker struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
8290c869620SGerrit Renker
8300c869620SGerrit Renker if (skb == NULL)
831df561f66SGustavo A. R. Silva goto verify_sock_status;
8320c869620SGerrit Renker
8330c869620SGerrit Renker dh = dccp_hdr(skb);
8340c869620SGerrit Renker
835531669a0SArnaldo Carvalho de Melo switch (dh->dccph_type) {
8367c657876SArnaldo Carvalho de Melo case DCCP_PKT_DATA:
8370c869620SGerrit Renker case DCCP_PKT_DATAACK:
8387690af3fSArnaldo Carvalho de Melo goto found_ok_skb;
8397690af3fSArnaldo Carvalho de Melo
8407bced397SDan Williams case DCCP_PKT_CLOSE:
8410c869620SGerrit Renker case DCCP_PKT_CLOSEREQ:
842531669a0SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK))
843531669a0SArnaldo Carvalho de Melo dccp_finish_passive_close(sk);
844531669a0SArnaldo Carvalho de Melo fallthrough;
8457c657876SArnaldo Carvalho de Melo case DCCP_PKT_RESET:
8467c657876SArnaldo Carvalho de Melo dccp_pr_debug("found fin (%s) ok!\n",
8477c657876SArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type));
848531669a0SArnaldo Carvalho de Melo len = 0;
849531669a0SArnaldo Carvalho de Melo goto found_fin_ok;
8507c657876SArnaldo Carvalho de Melo default:
851531669a0SArnaldo Carvalho de Melo dccp_pr_debug("packet_type=%s\n",
852531669a0SArnaldo Carvalho de Melo dccp_packet_name(dh->dccph_type));
853531669a0SArnaldo Carvalho de Melo sk_eat_skb(sk, skb);
854531669a0SArnaldo Carvalho de Melo }
855531669a0SArnaldo Carvalho de Melo verify_sock_status:
856531669a0SArnaldo Carvalho de Melo if (sock_flag(sk, SOCK_DONE)) {
8577c657876SArnaldo Carvalho de Melo len = 0;
8587c657876SArnaldo Carvalho de Melo break;
8597c657876SArnaldo Carvalho de Melo }
8607c657876SArnaldo Carvalho de Melo
8617c657876SArnaldo Carvalho de Melo if (sk->sk_err) {
8627c657876SArnaldo Carvalho de Melo len = sock_error(sk);
863531669a0SArnaldo Carvalho de Melo break;
8647c657876SArnaldo Carvalho de Melo }
8657c657876SArnaldo Carvalho de Melo
866531669a0SArnaldo Carvalho de Melo if (sk->sk_shutdown & RCV_SHUTDOWN) {
8677c657876SArnaldo Carvalho de Melo len = 0;
8687c657876SArnaldo Carvalho de Melo break;
8697c657876SArnaldo Carvalho de Melo }
8707c657876SArnaldo Carvalho de Melo
871531669a0SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED) {
8727c657876SArnaldo Carvalho de Melo if (!sock_flag(sk, SOCK_DONE)) {
8737c657876SArnaldo Carvalho de Melo /* This occurs when user tries to read
8747c657876SArnaldo Carvalho de Melo * from never connected socket.
8757c657876SArnaldo Carvalho de Melo */
876531669a0SArnaldo Carvalho de Melo len = -ENOTCONN;
8777c657876SArnaldo Carvalho de Melo break;
8787c657876SArnaldo Carvalho de Melo }
8797c657876SArnaldo Carvalho de Melo len = 0;
880dfbafc99SSabrina Dubroca break;
8817c657876SArnaldo Carvalho de Melo }
8827c657876SArnaldo Carvalho de Melo
883531669a0SArnaldo Carvalho de Melo if (!timeo) {
884531669a0SArnaldo Carvalho de Melo len = -EAGAIN;
885531669a0SArnaldo Carvalho de Melo break;
886531669a0SArnaldo Carvalho de Melo }
8877c657876SArnaldo Carvalho de Melo
88851f3d02bSDavid S. Miller if (signal_pending(current)) {
8897c657876SArnaldo Carvalho de Melo len = sock_intr_errno(timeo);
890531669a0SArnaldo Carvalho de Melo break;
8917c657876SArnaldo Carvalho de Melo }
8927c657876SArnaldo Carvalho de Melo
89355d95590SGerrit Renker sk_wait_data(sk, &timeo, NULL);
89455d95590SGerrit Renker continue;
8957c657876SArnaldo Carvalho de Melo found_ok_skb:
8967c657876SArnaldo Carvalho de Melo if (len > skb->len)
8977bced397SDan Williams len = skb->len;
8987c657876SArnaldo Carvalho de Melo else if (len < skb->len)
899531669a0SArnaldo Carvalho de Melo msg->msg_flags |= MSG_TRUNC;
9007c657876SArnaldo Carvalho de Melo
9017c657876SArnaldo Carvalho de Melo if (skb_copy_datagram_msg(skb, 0, msg, len)) {
902531669a0SArnaldo Carvalho de Melo /* Exception. Bailout! */
9037c657876SArnaldo Carvalho de Melo len = -EFAULT;
9047c657876SArnaldo Carvalho de Melo break;
905f21e68caSArnaldo Carvalho de Melo }
906f21e68caSArnaldo Carvalho de Melo if (flags & MSG_TRUNC)
907f21e68caSArnaldo Carvalho de Melo len = skb->len;
9087c657876SArnaldo Carvalho de Melo found_fin_ok:
9097c657876SArnaldo Carvalho de Melo if (!(flags & MSG_PEEK))
9107c657876SArnaldo Carvalho de Melo sk_eat_skb(sk, skb);
9117c657876SArnaldo Carvalho de Melo break;
9127c657876SArnaldo Carvalho de Melo } while (1);
9137c657876SArnaldo Carvalho de Melo out:
9147c657876SArnaldo Carvalho de Melo release_sock(sk);
9157c657876SArnaldo Carvalho de Melo return len;
9167c657876SArnaldo Carvalho de Melo }
9177c657876SArnaldo Carvalho de Melo
9187c657876SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_recvmsg);
9197c657876SArnaldo Carvalho de Melo
inet_dccp_listen(struct socket * sock,int backlog)9207c657876SArnaldo Carvalho de Melo int inet_dccp_listen(struct socket *sock, int backlog)
9217c657876SArnaldo Carvalho de Melo {
9227c657876SArnaldo Carvalho de Melo struct sock *sk = sock->sk;
923099ecf59SEric Dumazet unsigned char old_state;
9247c657876SArnaldo Carvalho de Melo int err;
9257c657876SArnaldo Carvalho de Melo
9267c657876SArnaldo Carvalho de Melo lock_sock(sk);
9277c657876SArnaldo Carvalho de Melo
928b4a8e749SKuniyuki Iwashima err = -EINVAL;
929b4a8e749SKuniyuki Iwashima if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
930b4a8e749SKuniyuki Iwashima goto out;
931b4a8e749SKuniyuki Iwashima
932b4a8e749SKuniyuki Iwashima old_state = sk->sk_state;
933b4a8e749SKuniyuki Iwashima if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
934b4a8e749SKuniyuki Iwashima goto out;
935b4a8e749SKuniyuki Iwashima
936b4a8e749SKuniyuki Iwashima WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
937b4a8e749SKuniyuki Iwashima /* Really, if the socket is already in listen state
938b4a8e749SKuniyuki Iwashima * we can only allow the backlog to be adjusted.
9397c657876SArnaldo Carvalho de Melo */
9407c657876SArnaldo Carvalho de Melo if (old_state != DCCP_LISTEN) {
9417c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
9427c657876SArnaldo Carvalho de Melo
9437c657876SArnaldo Carvalho de Melo dp->dccps_role = DCCP_ROLE_LISTEN;
9447c657876SArnaldo Carvalho de Melo
9457c657876SArnaldo Carvalho de Melo /* do not start to listen if feature negotiation setup fails */
9467c657876SArnaldo Carvalho de Melo if (dccp_feat_finalise_settings(dp)) {
9477c657876SArnaldo Carvalho de Melo err = -EPROTO;
9487c657876SArnaldo Carvalho de Melo goto out;
949f21e68caSArnaldo Carvalho de Melo }
950f21e68caSArnaldo Carvalho de Melo
9510c869620SGerrit Renker err = inet_csk_listen_start(sk);
9527c657876SArnaldo Carvalho de Melo if (err)
9530c869620SGerrit Renker goto out;
9547c657876SArnaldo Carvalho de Melo }
9550c869620SGerrit Renker err = 0;
9560c869620SGerrit Renker
9570c869620SGerrit Renker out:
9580c869620SGerrit Renker release_sock(sk);
9590c869620SGerrit Renker return err;
9600c869620SGerrit Renker }
9610c869620SGerrit Renker
9620c869620SGerrit Renker EXPORT_SYMBOL_GPL(inet_dccp_listen);
963df561f66SGustavo A. R. Silva
dccp_terminate_connection(struct sock * sk)9640c869620SGerrit Renker static void dccp_terminate_connection(struct sock *sk)
9650c869620SGerrit Renker {
9667c657876SArnaldo Carvalho de Melo u8 next_state = DCCP_CLOSED;
967b8599d20SGerrit Renker
968b8599d20SGerrit Renker switch (sk->sk_state) {
9690c869620SGerrit Renker case DCCP_PASSIVE_CLOSE:
9700c869620SGerrit Renker case DCCP_PASSIVE_CLOSEREQ:
9710c869620SGerrit Renker dccp_finish_passive_close(sk);
972df561f66SGustavo A. R. Silva break;
9730c869620SGerrit Renker case DCCP_PARTOPEN:
9740c869620SGerrit Renker dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
9750c869620SGerrit Renker inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
9767c657876SArnaldo Carvalho de Melo fallthrough;
9777c657876SArnaldo Carvalho de Melo case DCCP_OPEN:
9787c657876SArnaldo Carvalho de Melo dccp_send_close(sk, 1);
9797c657876SArnaldo Carvalho de Melo
98097e5848dSIan McDonald if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
9817c657876SArnaldo Carvalho de Melo !dccp_sk(sk)->dccps_server_timewait)
982d83bd95bSGerrit Renker next_state = DCCP_ACTIVE_CLOSEREQ;
983134af346SHerbert Xu else
9847c657876SArnaldo Carvalho de Melo next_state = DCCP_CLOSING;
9857c657876SArnaldo Carvalho de Melo fallthrough;
9867c657876SArnaldo Carvalho de Melo default:
9877c657876SArnaldo Carvalho de Melo dccp_set_state(sk, next_state);
9887c657876SArnaldo Carvalho de Melo }
9897c657876SArnaldo Carvalho de Melo }
9907c657876SArnaldo Carvalho de Melo
dccp_close(struct sock * sk,long timeout)9917c657876SArnaldo Carvalho de Melo void dccp_close(struct sock *sk, long timeout)
9927c657876SArnaldo Carvalho de Melo {
9937c657876SArnaldo Carvalho de Melo struct dccp_sock *dp = dccp_sk(sk);
9947c657876SArnaldo Carvalho de Melo struct sk_buff *skb;
9957c657876SArnaldo Carvalho de Melo u32 data_was_unread = 0;
9967c657876SArnaldo Carvalho de Melo int state;
9977c657876SArnaldo Carvalho de Melo
99897e5848dSIan McDonald lock_sock(sk);
99997e5848dSIan McDonald
10007c657876SArnaldo Carvalho de Melo sk->sk_shutdown = SHUTDOWN_MASK;
10017c657876SArnaldo Carvalho de Melo
10027c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_LISTEN) {
10037c657876SArnaldo Carvalho de Melo dccp_set_state(sk, DCCP_CLOSED);
10047c657876SArnaldo Carvalho de Melo
10057c657876SArnaldo Carvalho de Melo /* Special case. */
1006d83bd95bSGerrit Renker inet_csk_listen_stop(sk);
10077c657876SArnaldo Carvalho de Melo
10087c657876SArnaldo Carvalho de Melo goto adjudge_to_death;
10097c657876SArnaldo Carvalho de Melo }
1010346da62cSEric Dumazet
1011346da62cSEric Dumazet sk_stop_timer(sk, &dp->dccps_xmit_timer);
1012346da62cSEric Dumazet
1013346da62cSEric Dumazet /*
1014d83bd95bSGerrit Renker * We need to flush the recv. buffs. We do this only on the
1015d83bd95bSGerrit Renker * descriptor close, not protocol-sourced closes, because the
10162f34b329SGerrit Renker *reader process may not have drained the data yet!
1017d83bd95bSGerrit Renker */
1018d83bd95bSGerrit Renker while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
1019d83bd95bSGerrit Renker data_was_unread += skb->len;
10207c657876SArnaldo Carvalho de Melo __kfree_skb(skb);
10217c657876SArnaldo Carvalho de Melo }
10220c869620SGerrit Renker
1023b1fcf55eSGerrit Renker /* If socket has been already reset kill it. */
1024b1fcf55eSGerrit Renker if (sk->sk_state == DCCP_CLOSED)
1025b1fcf55eSGerrit Renker goto adjudge_to_death;
1026b1fcf55eSGerrit Renker
1027b1fcf55eSGerrit Renker if (data_was_unread) {
10280c869620SGerrit Renker /* Unread data was tossed, send an appropriate Reset Code */
10297c657876SArnaldo Carvalho de Melo DCCP_WARN("ABORT with %u bytes unread\n", data_was_unread);
10307c657876SArnaldo Carvalho de Melo dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
1031b1fcf55eSGerrit Renker dccp_set_state(sk, DCCP_CLOSED);
1032b1fcf55eSGerrit Renker } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
1033b1fcf55eSGerrit Renker /* Check zero linger _after_ checking for unread data. */
1034b1fcf55eSGerrit Renker sk->sk_prot->disconnect(sk, 0);
1035b1fcf55eSGerrit Renker } else if (sk->sk_state != DCCP_CLOSED) {
1036b1fcf55eSGerrit Renker /*
1037b1fcf55eSGerrit Renker * Normal connection termination. May need to wait if there are
1038b1fcf55eSGerrit Renker * still packets in the TX queue that are delayed by the CCID.
10397c657876SArnaldo Carvalho de Melo */
10407c657876SArnaldo Carvalho de Melo dccp_flush_write_queue(sk, &timeout);
10417c657876SArnaldo Carvalho de Melo dccp_terminate_connection(sk);
1042134af346SHerbert Xu }
1043134af346SHerbert Xu
1044134af346SHerbert Xu /*
1045134af346SHerbert Xu * Flush write queue. This may be necessary in several cases:
10467ad07e7cSArnaldo Carvalho de Melo * - we have been closed by the peer but still have application data;
10477ad07e7cSArnaldo Carvalho de Melo * - abortive termination (unread data or zero linger time),
10487ad07e7cSArnaldo Carvalho de Melo * - normal termination but queue could not be flushed within time limit
10497c657876SArnaldo Carvalho de Melo */
10507c657876SArnaldo Carvalho de Melo __skb_queue_purge(&sk->sk_write_queue);
10517c657876SArnaldo Carvalho de Melo
10527c657876SArnaldo Carvalho de Melo sk_stream_wait_close(sk, timeout);
10537c657876SArnaldo Carvalho de Melo
10547c657876SArnaldo Carvalho de Melo adjudge_to_death:
10557c657876SArnaldo Carvalho de Melo state = sk->sk_state;
1056547b792cSIlpo Järvinen sock_hold(sk);
10577c657876SArnaldo Carvalho de Melo sock_orphan(sk);
105819757cebSEric Dumazet
1059eb4dea58SHerbert Xu /*
1060134af346SHerbert Xu * It is the last release_sock in its life. It will remove backlog.
1061134af346SHerbert Xu */
1062134af346SHerbert Xu release_sock(sk);
10637c657876SArnaldo Carvalho de Melo /*
10647c657876SArnaldo Carvalho de Melo * Now socket is owned by kernel and we acquire BH lock
10657c657876SArnaldo Carvalho de Melo * to finish close. No need to check for user refs.
10667c657876SArnaldo Carvalho de Melo */
10677c657876SArnaldo Carvalho de Melo local_bh_disable();
10687c657876SArnaldo Carvalho de Melo bh_lock_sock(sk);
1069134af346SHerbert Xu WARN_ON(sock_owned_by_user(sk));
10707c657876SArnaldo Carvalho de Melo
10717c657876SArnaldo Carvalho de Melo this_cpu_inc(dccp_orphan_count);
10727c657876SArnaldo Carvalho de Melo
10737c657876SArnaldo Carvalho de Melo /* Have we already been destroyed by a softirq or backlog? */
10747c657876SArnaldo Carvalho de Melo if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
1075f21e68caSArnaldo Carvalho de Melo goto out;
1076f21e68caSArnaldo Carvalho de Melo
10777c657876SArnaldo Carvalho de Melo if (sk->sk_state == DCCP_CLOSED)
10787c657876SArnaldo Carvalho de Melo inet_csk_destroy_sock(sk);
10798e8c71f1SGerrit Renker
10807c657876SArnaldo Carvalho de Melo /* Otherwise, socket is reprieved until protocol close. */
10817c657876SArnaldo Carvalho de Melo
1082f21e68caSArnaldo Carvalho de Melo out:
1083f21e68caSArnaldo Carvalho de Melo bh_unlock_sock(sk);
10840c5b8a46SFabian Frederick local_bh_enable();
10857c657876SArnaldo Carvalho de Melo sock_put(sk);
1086698365faSWANG Cong }
1087698365faSWANG Cong
1088698365faSWANG Cong EXPORT_SYMBOL_GPL(dccp_close);
1089698365faSWANG Cong
dccp_shutdown(struct sock * sk,int how)10907c657876SArnaldo Carvalho de Melo void dccp_shutdown(struct sock *sk, int how)
10917c657876SArnaldo Carvalho de Melo {
109224e8b7e4SYOSHIFUJI Hideaki dccp_pr_debug("called shutdown(%x)\n", how);
109346f09ffaSArnaldo Carvalho de Melo }
1094698365faSWANG Cong
109546f09ffaSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(dccp_shutdown);
109646f09ffaSArnaldo Carvalho de Melo
dccp_mib_init(void)10977c657876SArnaldo Carvalho de Melo static inline int __init dccp_mib_init(void)
10987c657876SArnaldo Carvalho de Melo {
10997c657876SArnaldo Carvalho de Melo dccp_statistics = alloc_percpu(struct dccp_mib);
11007c657876SArnaldo Carvalho de Melo if (!dccp_statistics)
1101a1d3a355SArnaldo Carvalho de Melo return -ENOMEM;
1102eb939922SRusty Russell return 0;
110343264991SGerrit Renker }
11047c657876SArnaldo Carvalho de Melo
dccp_mib_exit(void)1105f21e68caSArnaldo Carvalho de Melo static inline void dccp_mib_exit(void)
1106f21e68caSArnaldo Carvalho de Melo {
1107a1d3a355SArnaldo Carvalho de Melo free_percpu(dccp_statistics);
11087c657876SArnaldo Carvalho de Melo }
11097c657876SArnaldo Carvalho de Melo
11107c657876SArnaldo Carvalho de Melo static int thash_entries;
11117c657876SArnaldo Carvalho de Melo module_param(thash_entries, int, 0444);
1112ca79b0c2SArun KS MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
11137c657876SArnaldo Carvalho de Melo
1114dd24c001SEric Dumazet #ifdef CONFIG_IP_DCCP_DEBUG
11157c657876SArnaldo Carvalho de Melo bool dccp_debug;
1116028b0275SPatrick McHardy module_param(dccp_debug, bool, 0644);
1117c593642cSPankaj Bharadiya MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1118c92c81dfSPeter Oskolkov
1119c92c81dfSPeter Oskolkov EXPORT_SYMBOL_GPL(dccp_debug);
112019757cebSEric Dumazet #endif
1121c92c81dfSPeter Oskolkov
dccp_init(void)11227690af3fSArnaldo Carvalho de Melo static int __init dccp_init(void)
11237690af3fSArnaldo Carvalho de Melo {
11247690af3fSArnaldo Carvalho de Melo unsigned long goal;
1125990c74e3SVasily Averin unsigned long nr_pages = totalram_pages();
11267c657876SArnaldo Carvalho de Melo int ehash_order, bhash_order, i;
1127c96b6accSWang Hai int rc;
112828044fc1SJoanne Koong
112928044fc1SJoanne Koong BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
113028044fc1SJoanne Koong sizeof_field(struct sk_buff, cb));
113128044fc1SJoanne Koong rc = inet_hashinfo2_init_mod(&dccp_hashinfo);
113228044fc1SJoanne Koong if (rc)
113328044fc1SJoanne Koong goto out_fail;
11347c657876SArnaldo Carvalho de Melo rc = -ENOBUFS;
11357c657876SArnaldo Carvalho de Melo dccp_hashinfo.bind_bucket_cachep =
11367c657876SArnaldo Carvalho de Melo kmem_cache_create("dccp_bind_bucket",
11377c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind_bucket), 0,
11387c657876SArnaldo Carvalho de Melo SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
11397c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind_bucket_cachep)
11407c657876SArnaldo Carvalho de Melo goto out_free_hashinfo2;
11413d6357deSArun KS dccp_hashinfo.bind2_bucket_cachep =
11423d6357deSArun KS kmem_cache_create("dccp_bind2_bucket",
11437c657876SArnaldo Carvalho de Melo sizeof(struct inet_bind2_bucket), 0,
11443d6357deSArun KS SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
11457c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bind2_bucket_cachep)
11467c657876SArnaldo Carvalho de Melo goto out_free_bind_bucket_cachep;
11477690af3fSArnaldo Carvalho de Melo
11487690af3fSArnaldo Carvalho de Melo /*
11497c657876SArnaldo Carvalho de Melo * Size and allocate the main established and bind bucket
11507c657876SArnaldo Carvalho de Melo * hash tables.
11517c657876SArnaldo Carvalho de Melo *
1152f373b53bSEric Dumazet * The methodology is similar to that of the buffer cache.
11537c657876SArnaldo Carvalho de Melo */
1154f373b53bSEric Dumazet if (nr_pages >= (128 * 1024))
1155f373b53bSEric Dumazet goal = nr_pages >> (21 - PAGE_SHIFT);
1156f373b53bSEric Dumazet else
1157f373b53bSEric Dumazet goal = nr_pages >> (23 - PAGE_SHIFT);
11587c657876SArnaldo Carvalho de Melo
11591c29b3ffSMel Gorman if (thash_entries)
11607c657876SArnaldo Carvalho de Melo goal = (thash_entries *
11617c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
11627c657876SArnaldo Carvalho de Melo for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
116359348b19SGerrit Renker ;
116428044fc1SJoanne Koong do {
11657c657876SArnaldo Carvalho de Melo unsigned long hash_size = (1UL << ehash_order) * PAGE_SIZE /
11667c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket);
116705dbc7b5SEric Dumazet
11683ab5aee7SEric Dumazet while (hash_size & (hash_size - 1))
11697c657876SArnaldo Carvalho de Melo hash_size--;
1170230140cfSEric Dumazet dccp_hashinfo.ehash_mask = hash_size - 1;
1171230140cfSEric Dumazet dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1172230140cfSEric Dumazet __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, ehash_order);
11737c657876SArnaldo Carvalho de Melo } while (!dccp_hashinfo.ehash && --ehash_order > 0);
11747c657876SArnaldo Carvalho de Melo
11757c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.ehash) {
11767c657876SArnaldo Carvalho de Melo DCCP_CRIT("Failed to allocate DCCP established hash table");
11777c657876SArnaldo Carvalho de Melo goto out_free_bind2_bucket_cachep;
11787690af3fSArnaldo Carvalho de Melo }
11797690af3fSArnaldo Carvalho de Melo
11807c657876SArnaldo Carvalho de Melo for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
11817c657876SArnaldo Carvalho de Melo INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
11821c29b3ffSMel Gorman
11837c657876SArnaldo Carvalho de Melo if (inet_ehash_locks_alloc(&dccp_hashinfo))
11847c657876SArnaldo Carvalho de Melo goto out_free_dccp_ehash;
11857c657876SArnaldo Carvalho de Melo
118659348b19SGerrit Renker bhash_order = ehash_order;
1187230140cfSEric Dumazet
11887c657876SArnaldo Carvalho de Melo do {
11897c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
119028044fc1SJoanne Koong sizeof(struct inet_bind_hashbucket);
119128044fc1SJoanne Koong if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
119228044fc1SJoanne Koong bhash_order > 0)
119328044fc1SJoanne Koong continue;
119428044fc1SJoanne Koong dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
119528044fc1SJoanne Koong __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, bhash_order);
119628044fc1SJoanne Koong } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
119728044fc1SJoanne Koong
11987c657876SArnaldo Carvalho de Melo if (!dccp_hashinfo.bhash) {
11997c657876SArnaldo Carvalho de Melo DCCP_CRIT("Failed to allocate DCCP bind hash table");
12007c657876SArnaldo Carvalho de Melo goto out_free_dccp_locks;
120128044fc1SJoanne Koong }
120228044fc1SJoanne Koong
12037c657876SArnaldo Carvalho de Melo dccp_hashinfo.bhash2 = (struct inet_bind_hashbucket *)
12047c657876SArnaldo Carvalho de Melo __get_free_pages(GFP_ATOMIC | __GFP_NOWARN, bhash_order);
1205d1e5e640SKuniyuki Iwashima
1206d1e5e640SKuniyuki Iwashima if (!dccp_hashinfo.bhash2) {
120746f09ffaSArnaldo Carvalho de Melo DCCP_CRIT("Failed to allocate DCCP bind2 hash table");
1208fa23e2ecSArnaldo Carvalho de Melo goto out_free_dccp_bhash;
120928044fc1SJoanne Koong }
12107c657876SArnaldo Carvalho de Melo
12119b07ef5dSArnaldo Carvalho de Melo for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
12127c657876SArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1213b61fafc4SArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
12149b07ef5dSArnaldo Carvalho de Melo spin_lock_init(&dccp_hashinfo.bhash2[i].lock);
1215e55d912fSArnaldo Carvalho de Melo INIT_HLIST_HEAD(&dccp_hashinfo.bhash2[i].chain);
12169b07ef5dSArnaldo Carvalho de Melo }
12179b07ef5dSArnaldo Carvalho de Melo
12184c70f383SGerrit Renker dccp_hashinfo.pernet = false;
1219ddebc973SGerrit Renker
1220ddebc973SGerrit Renker rc = dccp_mib_init();
1221ddebc973SGerrit Renker if (rc)
1222ddebc973SGerrit Renker goto out_free_dccp_bhash2;
12234c70f383SGerrit Renker
1224d14a0ebdSGerrit Renker rc = dccp_ackvec_init();
1225d14a0ebdSGerrit Renker if (rc)
1226d14a0ebdSGerrit Renker goto out_free_dccp_mib;
1227ddebc973SGerrit Renker
1228ddebc973SGerrit Renker rc = dccp_sysctl_init();
12299b07ef5dSArnaldo Carvalho de Melo if (rc)
12309b07ef5dSArnaldo Carvalho de Melo goto out_ackvec_exit;
1231b61fafc4SArnaldo Carvalho de Melo
123246f09ffaSArnaldo Carvalho de Melo rc = ccid_initialize_builtins();
123328044fc1SJoanne Koong if (rc)
123428044fc1SJoanne Koong goto out_sysctl_exit;
12357c657876SArnaldo Carvalho de Melo
12367c657876SArnaldo Carvalho de Melo dccp_timestamping_init();
1237230140cfSEric Dumazet
1238230140cfSEric Dumazet return 0;
12397c657876SArnaldo Carvalho de Melo
12407c657876SArnaldo Carvalho de Melo out_sysctl_exit:
124128044fc1SJoanne Koong dccp_sysctl_exit();
124228044fc1SJoanne Koong out_ackvec_exit:
12437c657876SArnaldo Carvalho de Melo dccp_ackvec_exit();
12447c657876SArnaldo Carvalho de Melo out_free_dccp_mib:
1245c96b6accSWang Hai dccp_mib_exit();
1246c96b6accSWang Hai out_free_dccp_bhash2:
1247d14a0ebdSGerrit Renker free_pages((unsigned long)dccp_hashinfo.bhash2, bhash_order);
1248d14a0ebdSGerrit Renker out_free_dccp_bhash:
124928044fc1SJoanne Koong free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1250d14a0ebdSGerrit Renker out_free_dccp_locks:
1251d14a0ebdSGerrit Renker inet_ehash_locks_free(&dccp_hashinfo);
125228044fc1SJoanne Koong out_free_dccp_ehash:
1253d14a0ebdSGerrit Renker free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
12547c657876SArnaldo Carvalho de Melo out_free_bind2_bucket_cachep:
12557c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind2_bucket_cachep);
12567c657876SArnaldo Carvalho de Melo out_free_bind_bucket_cachep:
12577c657876SArnaldo Carvalho de Melo kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
125828044fc1SJoanne Koong out_free_hashinfo2:
125928044fc1SJoanne Koong inet_hashinfo2_free_mod(&dccp_hashinfo);
126028044fc1SJoanne Koong out_fail:
1261ddebc973SGerrit Renker dccp_hashinfo.bhash = NULL;
126246f09ffaSArnaldo Carvalho de Melo dccp_hashinfo.bhash2 = NULL;
126328044fc1SJoanne Koong dccp_hashinfo.ehash = NULL;
126428044fc1SJoanne Koong dccp_hashinfo.bind_bucket_cachep = NULL;
1265725ba8eeSArnaldo Carvalho de Melo dccp_hashinfo.bind2_bucket_cachep = NULL;
1266f373b53bSEric Dumazet return rc;
1267725ba8eeSArnaldo Carvalho de Melo }
1268230140cfSEric Dumazet
dccp_fini(void)12697c657876SArnaldo Carvalho de Melo static void __exit dccp_fini(void)
12709b07ef5dSArnaldo Carvalho de Melo {
1271e55d912fSArnaldo Carvalho de Melo int bhash_order = get_order(dccp_hashinfo.bhash_size *
1272c96b6accSWang Hai sizeof(struct inet_bind_hashbucket));
12737c657876SArnaldo Carvalho de Melo
12747c657876SArnaldo Carvalho de Melo ccid_cleanup_builtins();
12757c657876SArnaldo Carvalho de Melo dccp_mib_exit();
12767c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
12777c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.bhash2, bhash_order);
12787c657876SArnaldo Carvalho de Melo free_pages((unsigned long)dccp_hashinfo.ehash,
12797c657876SArnaldo Carvalho de Melo get_order((dccp_hashinfo.ehash_mask + 1) *
12807c657876SArnaldo Carvalho de Melo sizeof(struct inet_ehash_bucket)));
1281 inet_ehash_locks_free(&dccp_hashinfo);
1282 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1283 dccp_ackvec_exit();
1284 dccp_sysctl_exit();
1285 inet_hashinfo2_free_mod(&dccp_hashinfo);
1286 }
1287
1288 module_init(dccp_init);
1289 module_exit(dccp_fini);
1290
1291 MODULE_LICENSE("GPL");
1292 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1293 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
1294