net: listening_hash get a spinlock per bucket
[deliverable/linux.git] / net / dccp / proto.c
1 /*
2 * net/dccp/proto.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
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>
17 #include <linux/skbuff.h>
18 #include <linux/netdevice.h>
19 #include <linux/in.h>
20 #include <linux/if_arp.h>
21 #include <linux/init.h>
22 #include <linux/random.h>
23 #include <net/checksum.h>
24
25 #include <net/inet_sock.h>
26 #include <net/sock.h>
27 #include <net/xfrm.h>
28
29 #include <asm/ioctls.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/delay.h>
33 #include <linux/poll.h>
34
35 #include "ccid.h"
36 #include "dccp.h"
37 #include "feat.h"
38
39 DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
40
41 EXPORT_SYMBOL_GPL(dccp_statistics);
42
43 atomic_t dccp_orphan_count = ATOMIC_INIT(0);
44
45 EXPORT_SYMBOL_GPL(dccp_orphan_count);
46
47 struct inet_hashinfo dccp_hashinfo;
48 EXPORT_SYMBOL_GPL(dccp_hashinfo);
49
50 /* the maximum queue length for tx in packets. 0 is no limit */
51 int sysctl_dccp_tx_qlen __read_mostly = 5;
52
53 void dccp_set_state(struct sock *sk, const int state)
54 {
55 const int oldstate = sk->sk_state;
56
57 dccp_pr_debug("%s(%p) %s --> %s\n", dccp_role(sk), sk,
58 dccp_state_name(oldstate), dccp_state_name(state));
59 WARN_ON(state == oldstate);
60
61 switch (state) {
62 case DCCP_OPEN:
63 if (oldstate != DCCP_OPEN)
64 DCCP_INC_STATS(DCCP_MIB_CURRESTAB);
65 break;
66
67 case DCCP_CLOSED:
68 if (oldstate == DCCP_OPEN || oldstate == DCCP_ACTIVE_CLOSEREQ ||
69 oldstate == DCCP_CLOSING)
70 DCCP_INC_STATS(DCCP_MIB_ESTABRESETS);
71
72 sk->sk_prot->unhash(sk);
73 if (inet_csk(sk)->icsk_bind_hash != NULL &&
74 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
75 inet_put_port(sk);
76 /* fall through */
77 default:
78 if (oldstate == DCCP_OPEN)
79 DCCP_DEC_STATS(DCCP_MIB_CURRESTAB);
80 }
81
82 /* Change state AFTER socket is unhashed to avoid closed
83 * socket sitting in hash tables.
84 */
85 sk->sk_state = state;
86 }
87
88 EXPORT_SYMBOL_GPL(dccp_set_state);
89
90 static void dccp_finish_passive_close(struct sock *sk)
91 {
92 switch (sk->sk_state) {
93 case DCCP_PASSIVE_CLOSE:
94 /* Node (client or server) has received Close packet. */
95 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
96 dccp_set_state(sk, DCCP_CLOSED);
97 break;
98 case DCCP_PASSIVE_CLOSEREQ:
99 /*
100 * Client received CloseReq. We set the `active' flag so that
101 * dccp_send_close() retransmits the Close as per RFC 4340, 8.3.
102 */
103 dccp_send_close(sk, 1);
104 dccp_set_state(sk, DCCP_CLOSING);
105 }
106 }
107
108 void dccp_done(struct sock *sk)
109 {
110 dccp_set_state(sk, DCCP_CLOSED);
111 dccp_clear_xmit_timers(sk);
112
113 sk->sk_shutdown = SHUTDOWN_MASK;
114
115 if (!sock_flag(sk, SOCK_DEAD))
116 sk->sk_state_change(sk);
117 else
118 inet_csk_destroy_sock(sk);
119 }
120
121 EXPORT_SYMBOL_GPL(dccp_done);
122
123 const char *dccp_packet_name(const int type)
124 {
125 static const char *dccp_packet_names[] = {
126 [DCCP_PKT_REQUEST] = "REQUEST",
127 [DCCP_PKT_RESPONSE] = "RESPONSE",
128 [DCCP_PKT_DATA] = "DATA",
129 [DCCP_PKT_ACK] = "ACK",
130 [DCCP_PKT_DATAACK] = "DATAACK",
131 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
132 [DCCP_PKT_CLOSE] = "CLOSE",
133 [DCCP_PKT_RESET] = "RESET",
134 [DCCP_PKT_SYNC] = "SYNC",
135 [DCCP_PKT_SYNCACK] = "SYNCACK",
136 };
137
138 if (type >= DCCP_NR_PKT_TYPES)
139 return "INVALID";
140 else
141 return dccp_packet_names[type];
142 }
143
144 EXPORT_SYMBOL_GPL(dccp_packet_name);
145
146 const char *dccp_state_name(const int state)
147 {
148 static char *dccp_state_names[] = {
149 [DCCP_OPEN] = "OPEN",
150 [DCCP_REQUESTING] = "REQUESTING",
151 [DCCP_PARTOPEN] = "PARTOPEN",
152 [DCCP_LISTEN] = "LISTEN",
153 [DCCP_RESPOND] = "RESPOND",
154 [DCCP_CLOSING] = "CLOSING",
155 [DCCP_ACTIVE_CLOSEREQ] = "CLOSEREQ",
156 [DCCP_PASSIVE_CLOSE] = "PASSIVE_CLOSE",
157 [DCCP_PASSIVE_CLOSEREQ] = "PASSIVE_CLOSEREQ",
158 [DCCP_TIME_WAIT] = "TIME_WAIT",
159 [DCCP_CLOSED] = "CLOSED",
160 };
161
162 if (state >= DCCP_MAX_STATES)
163 return "INVALID STATE!";
164 else
165 return dccp_state_names[state];
166 }
167
168 EXPORT_SYMBOL_GPL(dccp_state_name);
169
170 int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized)
171 {
172 struct dccp_sock *dp = dccp_sk(sk);
173 struct dccp_minisock *dmsk = dccp_msk(sk);
174 struct inet_connection_sock *icsk = inet_csk(sk);
175
176 dccp_minisock_init(&dp->dccps_minisock);
177
178 icsk->icsk_rto = DCCP_TIMEOUT_INIT;
179 icsk->icsk_syn_retries = sysctl_dccp_request_retries;
180 sk->sk_state = DCCP_CLOSED;
181 sk->sk_write_space = dccp_write_space;
182 icsk->icsk_sync_mss = dccp_sync_mss;
183 dp->dccps_mss_cache = 536;
184 dp->dccps_rate_last = jiffies;
185 dp->dccps_role = DCCP_ROLE_UNDEFINED;
186 dp->dccps_service = DCCP_SERVICE_CODE_IS_ABSENT;
187 dp->dccps_l_ack_ratio = dp->dccps_r_ack_ratio = 1;
188
189 dccp_init_xmit_timers(sk);
190
191 INIT_LIST_HEAD(&dp->dccps_featneg);
192 /*
193 * FIXME: We're hardcoding the CCID, and doing this at this point makes
194 * the listening (master) sock get CCID control blocks, which is not
195 * necessary, but for now, to not mess with the test userspace apps,
196 * lets leave it here, later the real solution is to do this in a
197 * setsockopt(CCIDs-I-want/accept). -acme
198 */
199 if (likely(ctl_sock_initialized)) {
200 int rc = dccp_feat_init(sk);
201
202 if (rc)
203 return rc;
204
205 if (dmsk->dccpms_send_ack_vector) {
206 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(GFP_KERNEL);
207 if (dp->dccps_hc_rx_ackvec == NULL)
208 return -ENOMEM;
209 }
210 dp->dccps_hc_rx_ccid = ccid_hc_rx_new(dmsk->dccpms_rx_ccid,
211 sk, GFP_KERNEL);
212 dp->dccps_hc_tx_ccid = ccid_hc_tx_new(dmsk->dccpms_tx_ccid,
213 sk, GFP_KERNEL);
214 if (unlikely(dp->dccps_hc_rx_ccid == NULL ||
215 dp->dccps_hc_tx_ccid == NULL)) {
216 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
217 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
218 if (dmsk->dccpms_send_ack_vector) {
219 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
220 dp->dccps_hc_rx_ackvec = NULL;
221 }
222 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
223 return -ENOMEM;
224 }
225 } else {
226 /* control socket doesn't need feat nego */
227 INIT_LIST_HEAD(&dmsk->dccpms_pending);
228 INIT_LIST_HEAD(&dmsk->dccpms_conf);
229 }
230
231 return 0;
232 }
233
234 EXPORT_SYMBOL_GPL(dccp_init_sock);
235
236 void dccp_destroy_sock(struct sock *sk)
237 {
238 struct dccp_sock *dp = dccp_sk(sk);
239 struct dccp_minisock *dmsk = dccp_msk(sk);
240
241 /*
242 * DCCP doesn't use sk_write_queue, just sk_send_head
243 * for retransmissions
244 */
245 if (sk->sk_send_head != NULL) {
246 kfree_skb(sk->sk_send_head);
247 sk->sk_send_head = NULL;
248 }
249
250 /* Clean up a referenced DCCP bind bucket. */
251 if (inet_csk(sk)->icsk_bind_hash != NULL)
252 inet_put_port(sk);
253
254 kfree(dp->dccps_service_list);
255 dp->dccps_service_list = NULL;
256
257 if (dmsk->dccpms_send_ack_vector) {
258 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
259 dp->dccps_hc_rx_ackvec = NULL;
260 }
261 ccid_hc_rx_delete(dp->dccps_hc_rx_ccid, sk);
262 ccid_hc_tx_delete(dp->dccps_hc_tx_ccid, sk);
263 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
264
265 /* clean up feature negotiation state */
266 dccp_feat_list_purge(&dp->dccps_featneg);
267 }
268
269 EXPORT_SYMBOL_GPL(dccp_destroy_sock);
270
271 static inline int dccp_listen_start(struct sock *sk, int backlog)
272 {
273 struct dccp_sock *dp = dccp_sk(sk);
274
275 dp->dccps_role = DCCP_ROLE_LISTEN;
276 /* do not start to listen if feature negotiation setup fails */
277 if (dccp_feat_finalise_settings(dp))
278 return -EPROTO;
279 return inet_csk_listen_start(sk, backlog);
280 }
281
282 static inline int dccp_need_reset(int state)
283 {
284 return state != DCCP_CLOSED && state != DCCP_LISTEN &&
285 state != DCCP_REQUESTING;
286 }
287
288 int dccp_disconnect(struct sock *sk, int flags)
289 {
290 struct inet_connection_sock *icsk = inet_csk(sk);
291 struct inet_sock *inet = inet_sk(sk);
292 int err = 0;
293 const int old_state = sk->sk_state;
294
295 if (old_state != DCCP_CLOSED)
296 dccp_set_state(sk, DCCP_CLOSED);
297
298 /*
299 * This corresponds to the ABORT function of RFC793, sec. 3.8
300 * TCP uses a RST segment, DCCP a Reset packet with Code 2, "Aborted".
301 */
302 if (old_state == DCCP_LISTEN) {
303 inet_csk_listen_stop(sk);
304 } else if (dccp_need_reset(old_state)) {
305 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
306 sk->sk_err = ECONNRESET;
307 } else if (old_state == DCCP_REQUESTING)
308 sk->sk_err = ECONNRESET;
309
310 dccp_clear_xmit_timers(sk);
311
312 __skb_queue_purge(&sk->sk_receive_queue);
313 __skb_queue_purge(&sk->sk_write_queue);
314 if (sk->sk_send_head != NULL) {
315 __kfree_skb(sk->sk_send_head);
316 sk->sk_send_head = NULL;
317 }
318
319 inet->dport = 0;
320
321 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
322 inet_reset_saddr(sk);
323
324 sk->sk_shutdown = 0;
325 sock_reset_flag(sk, SOCK_DONE);
326
327 icsk->icsk_backoff = 0;
328 inet_csk_delack_init(sk);
329 __sk_dst_reset(sk);
330
331 WARN_ON(inet->num && !icsk->icsk_bind_hash);
332
333 sk->sk_error_report(sk);
334 return err;
335 }
336
337 EXPORT_SYMBOL_GPL(dccp_disconnect);
338
339 /*
340 * Wait for a DCCP event.
341 *
342 * Note that we don't need to lock the socket, as the upper poll layers
343 * take care of normal races (between the test and the event) and we don't
344 * go look at any of the socket buffers directly.
345 */
346 unsigned int dccp_poll(struct file *file, struct socket *sock,
347 poll_table *wait)
348 {
349 unsigned int mask;
350 struct sock *sk = sock->sk;
351
352 poll_wait(file, sk->sk_sleep, wait);
353 if (sk->sk_state == DCCP_LISTEN)
354 return inet_csk_listen_poll(sk);
355
356 /* Socket is not locked. We are protected from async events
357 by poll logic and correct handling of state changes
358 made by another threads is impossible in any case.
359 */
360
361 mask = 0;
362 if (sk->sk_err)
363 mask = POLLERR;
364
365 if (sk->sk_shutdown == SHUTDOWN_MASK || sk->sk_state == DCCP_CLOSED)
366 mask |= POLLHUP;
367 if (sk->sk_shutdown & RCV_SHUTDOWN)
368 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
369
370 /* Connected? */
371 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_RESPOND)) {
372 if (atomic_read(&sk->sk_rmem_alloc) > 0)
373 mask |= POLLIN | POLLRDNORM;
374
375 if (!(sk->sk_shutdown & SEND_SHUTDOWN)) {
376 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
377 mask |= POLLOUT | POLLWRNORM;
378 } else { /* send SIGIO later */
379 set_bit(SOCK_ASYNC_NOSPACE,
380 &sk->sk_socket->flags);
381 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
382
383 /* Race breaker. If space is freed after
384 * wspace test but before the flags are set,
385 * IO signal will be lost.
386 */
387 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
388 mask |= POLLOUT | POLLWRNORM;
389 }
390 }
391 }
392 return mask;
393 }
394
395 EXPORT_SYMBOL_GPL(dccp_poll);
396
397 int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
398 {
399 int rc = -ENOTCONN;
400
401 lock_sock(sk);
402
403 if (sk->sk_state == DCCP_LISTEN)
404 goto out;
405
406 switch (cmd) {
407 case SIOCINQ: {
408 struct sk_buff *skb;
409 unsigned long amount = 0;
410
411 skb = skb_peek(&sk->sk_receive_queue);
412 if (skb != NULL) {
413 /*
414 * We will only return the amount of this packet since
415 * that is all that will be read.
416 */
417 amount = skb->len;
418 }
419 rc = put_user(amount, (int __user *)arg);
420 }
421 break;
422 default:
423 rc = -ENOIOCTLCMD;
424 break;
425 }
426 out:
427 release_sock(sk);
428 return rc;
429 }
430
431 EXPORT_SYMBOL_GPL(dccp_ioctl);
432
433 static int dccp_setsockopt_service(struct sock *sk, const __be32 service,
434 char __user *optval, int optlen)
435 {
436 struct dccp_sock *dp = dccp_sk(sk);
437 struct dccp_service_list *sl = NULL;
438
439 if (service == DCCP_SERVICE_INVALID_VALUE ||
440 optlen > DCCP_SERVICE_LIST_MAX_LEN * sizeof(u32))
441 return -EINVAL;
442
443 if (optlen > sizeof(service)) {
444 sl = kmalloc(optlen, GFP_KERNEL);
445 if (sl == NULL)
446 return -ENOMEM;
447
448 sl->dccpsl_nr = optlen / sizeof(u32) - 1;
449 if (copy_from_user(sl->dccpsl_list,
450 optval + sizeof(service),
451 optlen - sizeof(service)) ||
452 dccp_list_has_service(sl, DCCP_SERVICE_INVALID_VALUE)) {
453 kfree(sl);
454 return -EFAULT;
455 }
456 }
457
458 lock_sock(sk);
459 dp->dccps_service = service;
460
461 kfree(dp->dccps_service_list);
462
463 dp->dccps_service_list = sl;
464 release_sock(sk);
465 return 0;
466 }
467
468 static int dccp_setsockopt_cscov(struct sock *sk, int cscov, bool rx)
469 {
470 u8 *list, len;
471 int i, rc;
472
473 if (cscov < 0 || cscov > 15)
474 return -EINVAL;
475 /*
476 * Populate a list of permissible values, in the range cscov...15. This
477 * is necessary since feature negotiation of single values only works if
478 * both sides incidentally choose the same value. Since the list starts
479 * lowest-value first, negotiation will pick the smallest shared value.
480 */
481 if (cscov == 0)
482 return 0;
483 len = 16 - cscov;
484
485 list = kmalloc(len, GFP_KERNEL);
486 if (list == NULL)
487 return -ENOBUFS;
488
489 for (i = 0; i < len; i++)
490 list[i] = cscov++;
491
492 rc = dccp_feat_register_sp(sk, DCCPF_MIN_CSUM_COVER, rx, list, len);
493
494 if (rc == 0) {
495 if (rx)
496 dccp_sk(sk)->dccps_pcrlen = cscov;
497 else
498 dccp_sk(sk)->dccps_pcslen = cscov;
499 }
500 kfree(list);
501 return rc;
502 }
503
504 static int do_dccp_setsockopt(struct sock *sk, int level, int optname,
505 char __user *optval, int optlen)
506 {
507 struct dccp_sock *dp = dccp_sk(sk);
508 int val, err = 0;
509
510 switch (optname) {
511 case DCCP_SOCKOPT_PACKET_SIZE:
512 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
513 return 0;
514 case DCCP_SOCKOPT_CHANGE_L:
515 case DCCP_SOCKOPT_CHANGE_R:
516 DCCP_WARN("sockopt(CHANGE_L/R) is deprecated: fix your app\n");
517 return 0;
518 }
519
520 if (optlen < (int)sizeof(int))
521 return -EINVAL;
522
523 if (get_user(val, (int __user *)optval))
524 return -EFAULT;
525
526 if (optname == DCCP_SOCKOPT_SERVICE)
527 return dccp_setsockopt_service(sk, val, optval, optlen);
528
529 lock_sock(sk);
530 switch (optname) {
531 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;
537 case DCCP_SOCKOPT_SEND_CSCOV:
538 err = dccp_setsockopt_cscov(sk, val, false);
539 break;
540 case DCCP_SOCKOPT_RECV_CSCOV:
541 err = dccp_setsockopt_cscov(sk, val, true);
542 break;
543 default:
544 err = -ENOPROTOOPT;
545 break;
546 }
547 release_sock(sk);
548
549 return err;
550 }
551
552 int dccp_setsockopt(struct sock *sk, int level, int optname,
553 char __user *optval, int optlen)
554 {
555 if (level != SOL_DCCP)
556 return inet_csk(sk)->icsk_af_ops->setsockopt(sk, level,
557 optname, optval,
558 optlen);
559 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
560 }
561
562 EXPORT_SYMBOL_GPL(dccp_setsockopt);
563
564 #ifdef CONFIG_COMPAT
565 int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
566 char __user *optval, int optlen)
567 {
568 if (level != SOL_DCCP)
569 return inet_csk_compat_setsockopt(sk, level, optname,
570 optval, optlen);
571 return do_dccp_setsockopt(sk, level, optname, optval, optlen);
572 }
573
574 EXPORT_SYMBOL_GPL(compat_dccp_setsockopt);
575 #endif
576
577 static int dccp_getsockopt_service(struct sock *sk, int len,
578 __be32 __user *optval,
579 int __user *optlen)
580 {
581 const struct dccp_sock *dp = dccp_sk(sk);
582 const struct dccp_service_list *sl;
583 int err = -ENOENT, slen = 0, total_len = sizeof(u32);
584
585 lock_sock(sk);
586 if ((sl = dp->dccps_service_list) != NULL) {
587 slen = sl->dccpsl_nr * sizeof(u32);
588 total_len += slen;
589 }
590
591 err = -EINVAL;
592 if (total_len > len)
593 goto out;
594
595 err = 0;
596 if (put_user(total_len, optlen) ||
597 put_user(dp->dccps_service, optval) ||
598 (sl != NULL && copy_to_user(optval + 1, sl->dccpsl_list, slen)))
599 err = -EFAULT;
600 out:
601 release_sock(sk);
602 return err;
603 }
604
605 static int do_dccp_getsockopt(struct sock *sk, int level, int optname,
606 char __user *optval, int __user *optlen)
607 {
608 struct dccp_sock *dp;
609 int val, len;
610
611 if (get_user(len, optlen))
612 return -EFAULT;
613
614 if (len < (int)sizeof(int))
615 return -EINVAL;
616
617 dp = dccp_sk(sk);
618
619 switch (optname) {
620 case DCCP_SOCKOPT_PACKET_SIZE:
621 DCCP_WARN("sockopt(PACKET_SIZE) is deprecated: fix your app\n");
622 return 0;
623 case DCCP_SOCKOPT_SERVICE:
624 return dccp_getsockopt_service(sk, len,
625 (__be32 __user *)optval, optlen);
626 case DCCP_SOCKOPT_GET_CUR_MPS:
627 val = dp->dccps_mss_cache;
628 break;
629 case DCCP_SOCKOPT_AVAILABLE_CCIDS:
630 return ccid_getsockopt_builtin_ccids(sk, len, optval, optlen);
631 case DCCP_SOCKOPT_SERVER_TIMEWAIT:
632 val = dp->dccps_server_timewait;
633 break;
634 case DCCP_SOCKOPT_SEND_CSCOV:
635 val = dp->dccps_pcslen;
636 break;
637 case DCCP_SOCKOPT_RECV_CSCOV:
638 val = dp->dccps_pcrlen;
639 break;
640 case 128 ... 191:
641 return ccid_hc_rx_getsockopt(dp->dccps_hc_rx_ccid, sk, optname,
642 len, (u32 __user *)optval, optlen);
643 case 192 ... 255:
644 return ccid_hc_tx_getsockopt(dp->dccps_hc_tx_ccid, sk, optname,
645 len, (u32 __user *)optval, optlen);
646 default:
647 return -ENOPROTOOPT;
648 }
649
650 len = sizeof(val);
651 if (put_user(len, optlen) || copy_to_user(optval, &val, len))
652 return -EFAULT;
653
654 return 0;
655 }
656
657 int dccp_getsockopt(struct sock *sk, int level, int optname,
658 char __user *optval, int __user *optlen)
659 {
660 if (level != SOL_DCCP)
661 return inet_csk(sk)->icsk_af_ops->getsockopt(sk, level,
662 optname, optval,
663 optlen);
664 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
665 }
666
667 EXPORT_SYMBOL_GPL(dccp_getsockopt);
668
669 #ifdef CONFIG_COMPAT
670 int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
671 char __user *optval, int __user *optlen)
672 {
673 if (level != SOL_DCCP)
674 return inet_csk_compat_getsockopt(sk, level, optname,
675 optval, optlen);
676 return do_dccp_getsockopt(sk, level, optname, optval, optlen);
677 }
678
679 EXPORT_SYMBOL_GPL(compat_dccp_getsockopt);
680 #endif
681
682 int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
683 size_t len)
684 {
685 const struct dccp_sock *dp = dccp_sk(sk);
686 const int flags = msg->msg_flags;
687 const int noblock = flags & MSG_DONTWAIT;
688 struct sk_buff *skb;
689 int rc, size;
690 long timeo;
691
692 if (len > dp->dccps_mss_cache)
693 return -EMSGSIZE;
694
695 lock_sock(sk);
696
697 if (sysctl_dccp_tx_qlen &&
698 (sk->sk_write_queue.qlen >= sysctl_dccp_tx_qlen)) {
699 rc = -EAGAIN;
700 goto out_release;
701 }
702
703 timeo = sock_sndtimeo(sk, noblock);
704
705 /*
706 * We have to use sk_stream_wait_connect here to set sk_write_pending,
707 * so that the trick in dccp_rcv_request_sent_state_process.
708 */
709 /* Wait for a connection to finish. */
710 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN))
711 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
712 goto out_release;
713
714 size = sk->sk_prot->max_header + len;
715 release_sock(sk);
716 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
717 lock_sock(sk);
718 if (skb == NULL)
719 goto out_release;
720
721 skb_reserve(skb, sk->sk_prot->max_header);
722 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
723 if (rc != 0)
724 goto out_discard;
725
726 skb_queue_tail(&sk->sk_write_queue, skb);
727 dccp_write_xmit(sk,0);
728 out_release:
729 release_sock(sk);
730 return rc ? : len;
731 out_discard:
732 kfree_skb(skb);
733 goto out_release;
734 }
735
736 EXPORT_SYMBOL_GPL(dccp_sendmsg);
737
738 int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
739 size_t len, int nonblock, int flags, int *addr_len)
740 {
741 const struct dccp_hdr *dh;
742 long timeo;
743
744 lock_sock(sk);
745
746 if (sk->sk_state == DCCP_LISTEN) {
747 len = -ENOTCONN;
748 goto out;
749 }
750
751 timeo = sock_rcvtimeo(sk, nonblock);
752
753 do {
754 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
755
756 if (skb == NULL)
757 goto verify_sock_status;
758
759 dh = dccp_hdr(skb);
760
761 switch (dh->dccph_type) {
762 case DCCP_PKT_DATA:
763 case DCCP_PKT_DATAACK:
764 goto found_ok_skb;
765
766 case DCCP_PKT_CLOSE:
767 case DCCP_PKT_CLOSEREQ:
768 if (!(flags & MSG_PEEK))
769 dccp_finish_passive_close(sk);
770 /* fall through */
771 case DCCP_PKT_RESET:
772 dccp_pr_debug("found fin (%s) ok!\n",
773 dccp_packet_name(dh->dccph_type));
774 len = 0;
775 goto found_fin_ok;
776 default:
777 dccp_pr_debug("packet_type=%s\n",
778 dccp_packet_name(dh->dccph_type));
779 sk_eat_skb(sk, skb, 0);
780 }
781 verify_sock_status:
782 if (sock_flag(sk, SOCK_DONE)) {
783 len = 0;
784 break;
785 }
786
787 if (sk->sk_err) {
788 len = sock_error(sk);
789 break;
790 }
791
792 if (sk->sk_shutdown & RCV_SHUTDOWN) {
793 len = 0;
794 break;
795 }
796
797 if (sk->sk_state == DCCP_CLOSED) {
798 if (!sock_flag(sk, SOCK_DONE)) {
799 /* This occurs when user tries to read
800 * from never connected socket.
801 */
802 len = -ENOTCONN;
803 break;
804 }
805 len = 0;
806 break;
807 }
808
809 if (!timeo) {
810 len = -EAGAIN;
811 break;
812 }
813
814 if (signal_pending(current)) {
815 len = sock_intr_errno(timeo);
816 break;
817 }
818
819 sk_wait_data(sk, &timeo);
820 continue;
821 found_ok_skb:
822 if (len > skb->len)
823 len = skb->len;
824 else if (len < skb->len)
825 msg->msg_flags |= MSG_TRUNC;
826
827 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
828 /* Exception. Bailout! */
829 len = -EFAULT;
830 break;
831 }
832 found_fin_ok:
833 if (!(flags & MSG_PEEK))
834 sk_eat_skb(sk, skb, 0);
835 break;
836 } while (1);
837 out:
838 release_sock(sk);
839 return len;
840 }
841
842 EXPORT_SYMBOL_GPL(dccp_recvmsg);
843
844 int inet_dccp_listen(struct socket *sock, int backlog)
845 {
846 struct sock *sk = sock->sk;
847 unsigned char old_state;
848 int err;
849
850 lock_sock(sk);
851
852 err = -EINVAL;
853 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
854 goto out;
855
856 old_state = sk->sk_state;
857 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
858 goto out;
859
860 /* Really, if the socket is already in listen state
861 * we can only allow the backlog to be adjusted.
862 */
863 if (old_state != DCCP_LISTEN) {
864 /*
865 * FIXME: here it probably should be sk->sk_prot->listen_start
866 * see tcp_listen_start
867 */
868 err = dccp_listen_start(sk, backlog);
869 if (err)
870 goto out;
871 }
872 sk->sk_max_ack_backlog = backlog;
873 err = 0;
874
875 out:
876 release_sock(sk);
877 return err;
878 }
879
880 EXPORT_SYMBOL_GPL(inet_dccp_listen);
881
882 static void dccp_terminate_connection(struct sock *sk)
883 {
884 u8 next_state = DCCP_CLOSED;
885
886 switch (sk->sk_state) {
887 case DCCP_PASSIVE_CLOSE:
888 case DCCP_PASSIVE_CLOSEREQ:
889 dccp_finish_passive_close(sk);
890 break;
891 case DCCP_PARTOPEN:
892 dccp_pr_debug("Stop PARTOPEN timer (%p)\n", sk);
893 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
894 /* fall through */
895 case DCCP_OPEN:
896 dccp_send_close(sk, 1);
897
898 if (dccp_sk(sk)->dccps_role == DCCP_ROLE_SERVER &&
899 !dccp_sk(sk)->dccps_server_timewait)
900 next_state = DCCP_ACTIVE_CLOSEREQ;
901 else
902 next_state = DCCP_CLOSING;
903 /* fall through */
904 default:
905 dccp_set_state(sk, next_state);
906 }
907 }
908
909 void dccp_close(struct sock *sk, long timeout)
910 {
911 struct dccp_sock *dp = dccp_sk(sk);
912 struct sk_buff *skb;
913 u32 data_was_unread = 0;
914 int state;
915
916 lock_sock(sk);
917
918 sk->sk_shutdown = SHUTDOWN_MASK;
919
920 if (sk->sk_state == DCCP_LISTEN) {
921 dccp_set_state(sk, DCCP_CLOSED);
922
923 /* Special case. */
924 inet_csk_listen_stop(sk);
925
926 goto adjudge_to_death;
927 }
928
929 sk_stop_timer(sk, &dp->dccps_xmit_timer);
930
931 /*
932 * We need to flush the recv. buffs. We do this only on the
933 * descriptor close, not protocol-sourced closes, because the
934 *reader process may not have drained the data yet!
935 */
936 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
937 data_was_unread += skb->len;
938 __kfree_skb(skb);
939 }
940
941 if (data_was_unread) {
942 /* Unread data was tossed, send an appropriate Reset Code */
943 DCCP_WARN("DCCP: ABORT -- %u bytes unread\n", data_was_unread);
944 dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
945 dccp_set_state(sk, DCCP_CLOSED);
946 } else if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
947 /* Check zero linger _after_ checking for unread data. */
948 sk->sk_prot->disconnect(sk, 0);
949 } else if (sk->sk_state != DCCP_CLOSED) {
950 dccp_terminate_connection(sk);
951 }
952
953 sk_stream_wait_close(sk, timeout);
954
955 adjudge_to_death:
956 state = sk->sk_state;
957 sock_hold(sk);
958 sock_orphan(sk);
959 atomic_inc(sk->sk_prot->orphan_count);
960
961 /*
962 * It is the last release_sock in its life. It will remove backlog.
963 */
964 release_sock(sk);
965 /*
966 * Now socket is owned by kernel and we acquire BH lock
967 * to finish close. No need to check for user refs.
968 */
969 local_bh_disable();
970 bh_lock_sock(sk);
971 WARN_ON(sock_owned_by_user(sk));
972
973 /* Have we already been destroyed by a softirq or backlog? */
974 if (state != DCCP_CLOSED && sk->sk_state == DCCP_CLOSED)
975 goto out;
976
977 if (sk->sk_state == DCCP_CLOSED)
978 inet_csk_destroy_sock(sk);
979
980 /* Otherwise, socket is reprieved until protocol close. */
981
982 out:
983 bh_unlock_sock(sk);
984 local_bh_enable();
985 sock_put(sk);
986 }
987
988 EXPORT_SYMBOL_GPL(dccp_close);
989
990 void dccp_shutdown(struct sock *sk, int how)
991 {
992 dccp_pr_debug("called shutdown(%x)\n", how);
993 }
994
995 EXPORT_SYMBOL_GPL(dccp_shutdown);
996
997 static inline int dccp_mib_init(void)
998 {
999 return snmp_mib_init((void**)dccp_statistics, sizeof(struct dccp_mib));
1000 }
1001
1002 static inline void dccp_mib_exit(void)
1003 {
1004 snmp_mib_free((void**)dccp_statistics);
1005 }
1006
1007 static int thash_entries;
1008 module_param(thash_entries, int, 0444);
1009 MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
1010
1011 #ifdef CONFIG_IP_DCCP_DEBUG
1012 int dccp_debug;
1013 module_param(dccp_debug, bool, 0644);
1014 MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
1015
1016 EXPORT_SYMBOL_GPL(dccp_debug);
1017 #endif
1018
1019 static int __init dccp_init(void)
1020 {
1021 unsigned long goal;
1022 int ehash_order, bhash_order, i;
1023 int rc = -ENOBUFS;
1024
1025 BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
1026 FIELD_SIZEOF(struct sk_buff, cb));
1027
1028 inet_hashinfo_init(&dccp_hashinfo);
1029 dccp_hashinfo.bind_bucket_cachep =
1030 kmem_cache_create("dccp_bind_bucket",
1031 sizeof(struct inet_bind_bucket), 0,
1032 SLAB_HWCACHE_ALIGN, NULL);
1033 if (!dccp_hashinfo.bind_bucket_cachep)
1034 goto out;
1035
1036 /*
1037 * Size and allocate the main established and bind bucket
1038 * hash tables.
1039 *
1040 * The methodology is similar to that of the buffer cache.
1041 */
1042 if (num_physpages >= (128 * 1024))
1043 goal = num_physpages >> (21 - PAGE_SHIFT);
1044 else
1045 goal = num_physpages >> (23 - PAGE_SHIFT);
1046
1047 if (thash_entries)
1048 goal = (thash_entries *
1049 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
1050 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
1051 ;
1052 do {
1053 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
1054 sizeof(struct inet_ehash_bucket);
1055 while (dccp_hashinfo.ehash_size &
1056 (dccp_hashinfo.ehash_size - 1))
1057 dccp_hashinfo.ehash_size--;
1058 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
1059 __get_free_pages(GFP_ATOMIC, ehash_order);
1060 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
1061
1062 if (!dccp_hashinfo.ehash) {
1063 DCCP_CRIT("Failed to allocate DCCP established hash table");
1064 goto out_free_bind_bucket_cachep;
1065 }
1066
1067 for (i = 0; i < dccp_hashinfo.ehash_size; i++) {
1068 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
1069 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
1070 }
1071
1072 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1073 goto out_free_dccp_ehash;
1074
1075 bhash_order = ehash_order;
1076
1077 do {
1078 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
1079 sizeof(struct inet_bind_hashbucket);
1080 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
1081 bhash_order > 0)
1082 continue;
1083 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
1084 __get_free_pages(GFP_ATOMIC, bhash_order);
1085 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
1086
1087 if (!dccp_hashinfo.bhash) {
1088 DCCP_CRIT("Failed to allocate DCCP bind hash table");
1089 goto out_free_dccp_locks;
1090 }
1091
1092 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
1093 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
1094 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
1095 }
1096
1097 rc = dccp_mib_init();
1098 if (rc)
1099 goto out_free_dccp_bhash;
1100
1101 rc = dccp_ackvec_init();
1102 if (rc)
1103 goto out_free_dccp_mib;
1104
1105 rc = dccp_sysctl_init();
1106 if (rc)
1107 goto out_ackvec_exit;
1108
1109 dccp_timestamping_init();
1110 out:
1111 return rc;
1112 out_ackvec_exit:
1113 dccp_ackvec_exit();
1114 out_free_dccp_mib:
1115 dccp_mib_exit();
1116 out_free_dccp_bhash:
1117 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
1118 dccp_hashinfo.bhash = NULL;
1119 out_free_dccp_locks:
1120 inet_ehash_locks_free(&dccp_hashinfo);
1121 out_free_dccp_ehash:
1122 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
1123 dccp_hashinfo.ehash = NULL;
1124 out_free_bind_bucket_cachep:
1125 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1126 dccp_hashinfo.bind_bucket_cachep = NULL;
1127 goto out;
1128 }
1129
1130 static void __exit dccp_fini(void)
1131 {
1132 dccp_mib_exit();
1133 free_pages((unsigned long)dccp_hashinfo.bhash,
1134 get_order(dccp_hashinfo.bhash_size *
1135 sizeof(struct inet_bind_hashbucket)));
1136 free_pages((unsigned long)dccp_hashinfo.ehash,
1137 get_order(dccp_hashinfo.ehash_size *
1138 sizeof(struct inet_ehash_bucket)));
1139 inet_ehash_locks_free(&dccp_hashinfo);
1140 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
1141 dccp_ackvec_exit();
1142 dccp_sysctl_exit();
1143 }
1144
1145 module_init(dccp_init);
1146 module_exit(dccp_fini);
1147
1148 MODULE_LICENSE("GPL");
1149 MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
1150 MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");
This page took 0.054077 seconds and 6 git commands to generate.