12 #include <linux/dccp.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
20 #include <linux/if_arp.h>
22 #include <linux/random.h>
23 #include <linux/slab.h>
30 #include <asm/ioctls.h>
34 #include <linux/poll.h>
51 int sysctl_dccp_tx_qlen __read_mostly = 5;
53 #ifdef CONFIG_IP_DCCP_DEBUG
54 static const char *dccp_state_name(
const int state)
56 static const char *
const dccp_state_names[] = {
71 return "INVALID STATE!";
73 return dccp_state_names[
state];
79 const int oldstate = sk->sk_state;
82 dccp_state_name(oldstate), dccp_state_name(state));
99 sk->sk_prot->unhash(sk);
100 if (inet_csk(sk)->icsk_bind_hash !=
NULL &&
112 sk->sk_state =
state;
117 static void dccp_finish_passive_close(
struct sock *sk)
119 switch (sk->sk_state) {
138 dccp_clear_xmit_timers(sk);
152 static const char *
const dccp_packet_names[] = {
168 return dccp_packet_names[
type];
193 if (
likely(ctl_sock_initialized))
214 if (inet_csk(sk)->icsk_bind_hash !=
NULL)
234 static inline int dccp_listen_start(
struct sock *sk,
int backlog)
245 static inline int dccp_need_reset(
int state)
256 const int old_state = sk->sk_state;
267 }
else if (dccp_need_reset(old_state)) {
273 dccp_clear_xmit_timers(sk);
285 inet_reset_saddr(sk);
291 inet_csk_delack_init(sk);
313 struct sock *sk = sock->
sk;
315 sock_poll_wait(file, sk_sleep(sk), wait);
317 return inet_csk_listen_poll(sk);
339 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
350 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
372 unsigned long amount = 0;
397 char __user *optval,
unsigned int optlen)
406 if (optlen >
sizeof(service)) {
413 optval +
sizeof(service),
414 optlen -
sizeof(service)) ||
431 static int dccp_setsockopt_cscov(
struct sock *sk,
int cscov,
bool rx)
436 if (cscov < 0 || cscov > 15)
452 for (i = 0; i < len; i++)
459 dccp_sk(sk)->dccps_pcrlen = cscov;
461 dccp_sk(sk)->dccps_pcslen = cscov;
467 static int dccp_setsockopt_ccid(
struct sock *sk,
int type,
468 char __user *optval,
unsigned int optlen)
492 static int do_dccp_setsockopt(
struct sock *sk,
int level,
int optname,
493 char __user *optval,
unsigned int optlen)
500 DCCP_WARN(
"sockopt(PACKET_SIZE) is deprecated: fix your app\n");
504 DCCP_WARN(
"sockopt(CHANGE_L/R) is deprecated: fix your app\n");
509 return dccp_setsockopt_ccid(sk, optname, optval, optlen);
512 if (optlen < (
int)
sizeof(
int))
515 if (
get_user(val, (
int __user *)optval))
519 return dccp_setsockopt_service(sk, val, optval, optlen);
530 err = dccp_setsockopt_cscov(sk, val,
false);
533 err = dccp_setsockopt_cscov(sk, val,
true);
559 char __user *optval,
unsigned int optlen)
562 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
565 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
571 int compat_dccp_setsockopt(
struct sock *sk,
int level,
int optname,
572 char __user *optval,
unsigned int optlen)
577 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
583 static int dccp_getsockopt_service(
struct sock *sk,
int len,
587 const struct dccp_sock *dp = dccp_sk(sk);
611 static int do_dccp_getsockopt(
struct sock *sk,
int level,
int optname,
612 char __user *optval,
int __user *optlen)
620 if (len < (
int)
sizeof(
int))
627 DCCP_WARN(
"sockopt(PACKET_SIZE) is deprecated: fix your app\n");
630 return dccp_getsockopt_service(sk, len,
631 (
__be32 __user *)optval, optlen);
638 val = ccid_get_current_tx_ccid(dp);
643 val = ccid_get_current_rx_ccid(dp);
664 len, (
u32 __user *)optval, optlen);
667 len, (
u32 __user *)optval, optlen);
680 char __user *optval,
int __user *optlen)
683 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
686 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
692 int compat_dccp_getsockopt(
struct sock *sk,
int level,
int optname,
693 char __user *optval,
int __user *optlen)
698 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
748 const struct dccp_sock *dp = dccp_sk(sk);
765 timeo = sock_sndtimeo(sk, noblock);
776 size = sk->sk_prot->max_header +
len;
783 skb_reserve(skb, sk->sk_prot->max_header);
788 rc = dccp_msghdr_parse(msg, skb);
811 size_t len,
int nonblock,
int flags,
int *addr_len)
823 timeo = sock_rcvtimeo(sk, nonblock);
829 goto verify_sock_status;
833 switch (dh->dccph_type) {
841 dccp_finish_passive_close(sk);
851 sk_eat_skb(sk, skb,
false);
860 len = sock_error(sk);
887 len = sock_intr_errno(timeo);
896 else if (len < skb->len)
908 sk_eat_skb(sk, skb,
false);
920 struct sock *sk = sock->
sk;
921 unsigned char old_state;
930 old_state = sk->sk_state;
942 err = dccp_listen_start(sk, backlog);
956 static void dccp_terminate_connection(
struct sock *sk)
960 switch (sk->sk_state) {
963 dccp_finish_passive_close(sk);
973 !dccp_sk(sk)->dccps_server_timewait)
987 u32 data_was_unread = 0;
1000 goto adjudge_to_death;
1011 data_was_unread += skb->
len;
1015 if (data_was_unread) {
1017 DCCP_WARN(
"ABORT with %u bytes unread\n", data_was_unread);
1022 sk->sk_prot->disconnect(sk, 0);
1029 dccp_terminate_connection(sk);
1043 state = sk->sk_state;
1059 percpu_counter_inc(sk->sk_prot->orphan_count);
1085 static inline int dccp_mib_init(
void)
1092 static inline void dccp_mib_exit(
void)
1097 static int thash_entries;
1101 #ifdef CONFIG_IP_DCCP_DEBUG
1109 static int __init dccp_init(
void)
1112 int ehash_order, bhash_order,
i;
1117 rc = percpu_counter_init(&dccp_orphan_count, 0);
1122 dccp_hashinfo.bind_bucket_cachep =
1126 if (!dccp_hashinfo.bind_bucket_cachep)
1127 goto out_free_percpu;
1135 if (totalram_pages >= (128 * 1024))
1141 goal = (thash_entries *
1143 for (ehash_order = 0; (1
UL << ehash_order) < goal; ehash_order++)
1146 unsigned long hash_size = (1
UL << ehash_order) *
PAGE_SIZE /
1149 while (hash_size & (hash_size - 1))
1151 dccp_hashinfo.ehash_mask = hash_size - 1;
1154 }
while (!dccp_hashinfo.ehash && --ehash_order > 0);
1156 if (!dccp_hashinfo.ehash) {
1157 DCCP_CRIT(
"Failed to allocate DCCP established hash table");
1158 goto out_free_bind_bucket_cachep;
1161 for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) {
1166 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1167 goto out_free_dccp_ehash;
1169 bhash_order = ehash_order;
1172 dccp_hashinfo.bhash_size = (1
UL << bhash_order) *
PAGE_SIZE /
1174 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1179 }
while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1181 if (!dccp_hashinfo.bhash) {
1182 DCCP_CRIT(
"Failed to allocate DCCP bind hash table");
1183 goto out_free_dccp_locks;
1186 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1191 rc = dccp_mib_init();
1193 goto out_free_dccp_bhash;
1197 goto out_free_dccp_mib;
1201 goto out_ackvec_exit;
1205 goto out_sysctl_exit;
1217 out_free_dccp_bhash:
1218 free_pages((
unsigned long)dccp_hashinfo.bhash, bhash_order);
1219 out_free_dccp_locks:
1220 inet_ehash_locks_free(&dccp_hashinfo);
1221 out_free_dccp_ehash:
1222 free_pages((
unsigned long)dccp_hashinfo.ehash, ehash_order);
1223 out_free_bind_bucket_cachep:
1228 dccp_hashinfo.bhash =
NULL;
1229 dccp_hashinfo.ehash =
NULL;
1230 dccp_hashinfo.bind_bucket_cachep =
NULL;
1234 static void __exit dccp_fini(
void)
1238 free_pages((
unsigned long)dccp_hashinfo.bhash,
1241 free_pages((
unsigned long)dccp_hashinfo.ehash,
1242 get_order((dccp_hashinfo.ehash_mask + 1) *
1244 inet_ehash_locks_free(&dccp_hashinfo);