blob: 2b6db18e607faf05096de9e1bbd3fbe5a3632e7e [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
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/config.h>
13#include <linux/dccp.h>
14#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/sched.h>
17#include <linux/kernel.h>
18#include <linux/skbuff.h>
19#include <linux/netdevice.h>
20#include <linux/in.h>
21#include <linux/if_arp.h>
22#include <linux/init.h>
23#include <linux/random.h>
24#include <net/checksum.h>
25
26#include <net/inet_common.h>
27#include <net/ip.h>
28#include <net/protocol.h>
29#include <net/sock.h>
30#include <net/xfrm.h>
31
32#include <asm/semaphore.h>
33#include <linux/spinlock.h>
34#include <linux/timer.h>
35#include <linux/delay.h>
36#include <linux/poll.h>
37#include <linux/dccp.h>
38
39#include "ccid.h"
40#include "dccp.h"
41
42DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics);
43
44atomic_t dccp_orphan_count = ATOMIC_INIT(0);
45
46static struct net_protocol dccp_protocol = {
47 .handler = dccp_v4_rcv,
48 .err_handler = dccp_v4_err,
49};
50
51const char *dccp_packet_name(const int type)
52{
53 static const char *dccp_packet_names[] = {
54 [DCCP_PKT_REQUEST] = "REQUEST",
55 [DCCP_PKT_RESPONSE] = "RESPONSE",
56 [DCCP_PKT_DATA] = "DATA",
57 [DCCP_PKT_ACK] = "ACK",
58 [DCCP_PKT_DATAACK] = "DATAACK",
59 [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
60 [DCCP_PKT_CLOSE] = "CLOSE",
61 [DCCP_PKT_RESET] = "RESET",
62 [DCCP_PKT_SYNC] = "SYNC",
63 [DCCP_PKT_SYNCACK] = "SYNCACK",
64 };
65
66 if (type >= DCCP_NR_PKT_TYPES)
67 return "INVALID";
68 else
69 return dccp_packet_names[type];
70}
71
72EXPORT_SYMBOL_GPL(dccp_packet_name);
73
74const char *dccp_state_name(const int state)
75{
76 static char *dccp_state_names[] = {
77 [DCCP_OPEN] = "OPEN",
78 [DCCP_REQUESTING] = "REQUESTING",
79 [DCCP_PARTOPEN] = "PARTOPEN",
80 [DCCP_LISTEN] = "LISTEN",
81 [DCCP_RESPOND] = "RESPOND",
82 [DCCP_CLOSING] = "CLOSING",
83 [DCCP_TIME_WAIT] = "TIME_WAIT",
84 [DCCP_CLOSED] = "CLOSED",
85 };
86
87 if (state >= DCCP_MAX_STATES)
88 return "INVALID STATE!";
89 else
90 return dccp_state_names[state];
91}
92
93EXPORT_SYMBOL_GPL(dccp_state_name);
94
95static inline int dccp_listen_start(struct sock *sk)
96{
97 dccp_sk(sk)->dccps_role = DCCP_ROLE_LISTEN;
98 return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
99}
100
101int dccp_disconnect(struct sock *sk, int flags)
102{
103 struct inet_connection_sock *icsk = inet_csk(sk);
104 struct inet_sock *inet = inet_sk(sk);
105 int err = 0;
106 const int old_state = sk->sk_state;
107
108 if (old_state != DCCP_CLOSED)
109 dccp_set_state(sk, DCCP_CLOSED);
110
111 /* ABORT function of RFC793 */
112 if (old_state == DCCP_LISTEN) {
113 inet_csk_listen_stop(sk);
114 /* FIXME: do the active reset thing */
115 } else if (old_state == DCCP_REQUESTING)
116 sk->sk_err = ECONNRESET;
117
118 dccp_clear_xmit_timers(sk);
119 __skb_queue_purge(&sk->sk_receive_queue);
120 if (sk->sk_send_head != NULL) {
121 __kfree_skb(sk->sk_send_head);
122 sk->sk_send_head = NULL;
123 }
124
125 inet->dport = 0;
126
127 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
128 inet_reset_saddr(sk);
129
130 sk->sk_shutdown = 0;
131 sock_reset_flag(sk, SOCK_DONE);
132
133 icsk->icsk_backoff = 0;
134 inet_csk_delack_init(sk);
135 __sk_dst_reset(sk);
136
137 BUG_TRAP(!inet->num || icsk->icsk_bind_hash);
138
139 sk->sk_error_report(sk);
140 return err;
141}
142
143int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
144{
145 dccp_pr_debug("entry\n");
146 return -ENOIOCTLCMD;
147}
148
149int dccp_setsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300150 char __user *optval, int optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700151{
152 dccp_pr_debug("entry\n");
153
154 if (level != SOL_DCCP)
155 return ip_setsockopt(sk, level, optname, optval, optlen);
156
157 return -EOPNOTSUPP;
158}
159
160int dccp_getsockopt(struct sock *sk, int level, int optname,
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300161 char __user *optval, int __user *optlen)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700162{
163 dccp_pr_debug("entry\n");
164
165 if (level != SOL_DCCP)
166 return ip_getsockopt(sk, level, optname, optval, optlen);
167
168 return -EOPNOTSUPP;
169}
170
171int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
172 size_t len)
173{
174 const struct dccp_sock *dp = dccp_sk(sk);
175 const int flags = msg->msg_flags;
176 const int noblock = flags & MSG_DONTWAIT;
177 struct sk_buff *skb;
178 int rc, size;
179 long timeo;
180
181 if (len > dp->dccps_mss_cache)
182 return -EMSGSIZE;
183
184 lock_sock(sk);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700185 timeo = sock_sndtimeo(sk, noblock);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700186
187 /*
188 * We have to use sk_stream_wait_connect here to set sk_write_pending,
189 * so that the trick in dccp_rcv_request_sent_state_process.
190 */
191 /* Wait for a connection to finish. */
192 if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING))
193 if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700194 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700195
196 size = sk->sk_prot->max_header + len;
197 release_sock(sk);
198 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
199 lock_sock(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700200 if (skb == NULL)
201 goto out_release;
202
203 skb_reserve(skb, sk->sk_prot->max_header);
204 rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700205 if (rc != 0)
206 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700207
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700208 rc = dccp_write_xmit(sk, skb, len);
Arnaldo Carvalho de Melo20472af2005-08-23 21:50:21 -0700209 /*
210 * XXX we don't use sk_write_queue, so just discard the packet.
211 * Current plan however is to _use_ sk_write_queue with
212 * an algorith similar to tcp_sendmsg, where the main difference
213 * is that in DCCP we have to respect packet boundaries, so
214 * no coalescing of skbs.
215 *
216 * This bug was _quickly_ found & fixed by just looking at an OSTRA
217 * generated callgraph 8) -acme
218 */
219 if (rc != 0)
220 goto out_discard;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700221out_release:
222 release_sock(sk);
223 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700224out_discard:
225 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700226 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700227}
228
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700229int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
230 size_t len, int nonblock, int flags, int *addr_len)
231{
232 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700233 long timeo;
234
235 lock_sock(sk);
236
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300237 if (sk->sk_state == DCCP_LISTEN) {
238 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700239 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300240 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700241
242 timeo = sock_rcvtimeo(sk, nonblock);
243
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700244 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300245 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700246
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300247 if (skb == NULL)
248 goto verify_sock_status;
249
250 dh = dccp_hdr(skb);
251
252 if (dh->dccph_type == DCCP_PKT_DATA ||
253 dh->dccph_type == DCCP_PKT_DATAACK)
254 goto found_ok_skb;
255
256 if (dh->dccph_type == DCCP_PKT_RESET ||
257 dh->dccph_type == DCCP_PKT_CLOSE) {
258 dccp_pr_debug("found fin ok!\n");
259 len = 0;
260 goto found_fin_ok;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700261 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300262 dccp_pr_debug("packet_type=%s\n",
263 dccp_packet_name(dh->dccph_type));
264 sk_eat_skb(sk, skb);
265verify_sock_status:
266 if (sock_flag(sk, SOCK_DONE)) {
267 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700268 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700269 }
270
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300271 if (sk->sk_err) {
272 len = sock_error(sk);
273 break;
274 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700275
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300276 if (sk->sk_shutdown & RCV_SHUTDOWN) {
277 len = 0;
278 break;
279 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700280
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300281 if (sk->sk_state == DCCP_CLOSED) {
282 if (!sock_flag(sk, SOCK_DONE)) {
283 /* This occurs when user tries to read
284 * from never connected socket.
285 */
286 len = -ENOTCONN;
287 break;
288 }
289 len = 0;
290 break;
291 }
292
293 if (!timeo) {
294 len = -EAGAIN;
295 break;
296 }
297
298 if (signal_pending(current)) {
299 len = sock_intr_errno(timeo);
300 break;
301 }
302
303 sk_wait_data(sk, &timeo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700304 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700305 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300306 if (len > skb->len)
307 len = skb->len;
308 else if (len < skb->len)
309 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700310
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300311 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
312 /* Exception. Bailout! */
313 len = -EFAULT;
314 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700315 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700316 found_fin_ok:
317 if (!(flags & MSG_PEEK))
318 sk_eat_skb(sk, skb);
319 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300320 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700321out:
322 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300323 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700324}
325
326static int inet_dccp_listen(struct socket *sock, int backlog)
327{
328 struct sock *sk = sock->sk;
329 unsigned char old_state;
330 int err;
331
332 lock_sock(sk);
333
334 err = -EINVAL;
335 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
336 goto out;
337
338 old_state = sk->sk_state;
339 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
340 goto out;
341
342 /* Really, if the socket is already in listen state
343 * we can only allow the backlog to be adjusted.
344 */
345 if (old_state != DCCP_LISTEN) {
346 /*
347 * FIXME: here it probably should be sk->sk_prot->listen_start
348 * see tcp_listen_start
349 */
350 err = dccp_listen_start(sk);
351 if (err)
352 goto out;
353 }
354 sk->sk_max_ack_backlog = backlog;
355 err = 0;
356
357out:
358 release_sock(sk);
359 return err;
360}
361
362static const unsigned char dccp_new_state[] = {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300363 /* current state: new state: action: */
364 [0] = DCCP_CLOSED,
365 [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
366 [DCCP_REQUESTING] = DCCP_CLOSED,
367 [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
368 [DCCP_LISTEN] = DCCP_CLOSED,
369 [DCCP_RESPOND] = DCCP_CLOSED,
370 [DCCP_CLOSING] = DCCP_CLOSED,
371 [DCCP_TIME_WAIT] = DCCP_CLOSED,
372 [DCCP_CLOSED] = DCCP_CLOSED,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700373};
374
375static int dccp_close_state(struct sock *sk)
376{
377 const int next = dccp_new_state[sk->sk_state];
378 const int ns = next & DCCP_STATE_MASK;
379
380 if (ns != sk->sk_state)
381 dccp_set_state(sk, ns);
382
383 return next & DCCP_ACTION_FIN;
384}
385
386void dccp_close(struct sock *sk, long timeout)
387{
388 struct sk_buff *skb;
389
390 lock_sock(sk);
391
392 sk->sk_shutdown = SHUTDOWN_MASK;
393
394 if (sk->sk_state == DCCP_LISTEN) {
395 dccp_set_state(sk, DCCP_CLOSED);
396
397 /* Special case. */
398 inet_csk_listen_stop(sk);
399
400 goto adjudge_to_death;
401 }
402
403 /*
404 * We need to flush the recv. buffs. We do this only on the
405 * descriptor close, not protocol-sourced closes, because the
406 *reader process may not have drained the data yet!
407 */
408 /* FIXME: check for unread data */
409 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
410 __kfree_skb(skb);
411 }
412
413 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
414 /* Check zero linger _after_ checking for unread data. */
415 sk->sk_prot->disconnect(sk, 0);
416 } else if (dccp_close_state(sk)) {
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700417 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700418 }
419
420 sk_stream_wait_close(sk, timeout);
421
422adjudge_to_death:
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700423 /*
424 * It is the last release_sock in its life. It will remove backlog.
425 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700426 release_sock(sk);
427 /*
428 * Now socket is owned by kernel and we acquire BH lock
429 * to finish close. No need to check for user refs.
430 */
431 local_bh_disable();
432 bh_lock_sock(sk);
433 BUG_TRAP(!sock_owned_by_user(sk));
434
435 sock_hold(sk);
436 sock_orphan(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700437
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700438 /*
439 * The last release_sock may have processed the CLOSE or RESET
440 * packet moving sock to CLOSED state, if not we have to fire
441 * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination"
442 * in draft-ietf-dccp-spec-11. -acme
443 */
444 if (sk->sk_state == DCCP_CLOSING) {
445 /* FIXME: should start at 2 * RTT */
446 /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */
447 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
448 inet_csk(sk)->icsk_rto,
449 DCCP_RTO_MAX);
450#if 0
451 /* Yeah, we should use sk->sk_prot->orphan_count, etc */
452 dccp_set_state(sk, DCCP_CLOSED);
453#endif
454 }
455
456 atomic_inc(sk->sk_prot->orphan_count);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700457 if (sk->sk_state == DCCP_CLOSED)
458 inet_csk_destroy_sock(sk);
459
460 /* Otherwise, socket is reprieved until protocol close. */
461
462 bh_unlock_sock(sk);
463 local_bh_enable();
464 sock_put(sk);
465}
466
467void dccp_shutdown(struct sock *sk, int how)
468{
469 dccp_pr_debug("entry\n");
470}
471
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300472static struct proto_ops inet_dccp_ops = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700473 .family = PF_INET,
474 .owner = THIS_MODULE,
475 .release = inet_release,
476 .bind = inet_bind,
477 .connect = inet_stream_connect,
478 .socketpair = sock_no_socketpair,
479 .accept = inet_accept,
480 .getname = inet_getname,
481 .poll = sock_no_poll,
482 .ioctl = inet_ioctl,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300483 /* FIXME: work on inet_listen to rename it to sock_common_listen */
484 .listen = inet_dccp_listen,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700485 .shutdown = inet_shutdown,
486 .setsockopt = sock_common_setsockopt,
487 .getsockopt = sock_common_getsockopt,
488 .sendmsg = inet_sendmsg,
489 .recvmsg = sock_common_recvmsg,
490 .mmap = sock_no_mmap,
491 .sendpage = sock_no_sendpage,
492};
493
494extern struct net_proto_family inet_family_ops;
495
496static struct inet_protosw dccp_v4_protosw = {
497 .type = SOCK_DCCP,
498 .protocol = IPPROTO_DCCP,
499 .prot = &dccp_v4_prot,
500 .ops = &inet_dccp_ops,
501 .capability = -1,
502 .no_check = 0,
503 .flags = 0,
504};
505
506/*
507 * This is the global socket data structure used for responding to
508 * the Out-of-the-blue (OOTB) packets. A control sock will be created
509 * for this socket at the initialization time.
510 */
511struct socket *dccp_ctl_socket;
512
513static char dccp_ctl_socket_err_msg[] __initdata =
514 KERN_ERR "DCCP: Failed to create the control socket.\n";
515
516static int __init dccp_ctl_sock_init(void)
517{
518 int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP,
519 &dccp_ctl_socket);
520 if (rc < 0)
521 printk(dccp_ctl_socket_err_msg);
522 else {
523 dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
524 inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1;
525
526 /* Unhash it so that IP input processing does not even
527 * see it, we do not wish this socket to see incoming
528 * packets.
529 */
530 dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk);
531 }
532
533 return rc;
534}
535
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300536#ifdef CONFIG_IP_DCCP_UNLOAD_HACK
537void dccp_ctl_sock_exit(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700538{
Arnaldo Carvalho de Melo54808552005-08-18 20:47:02 -0300539 if (dccp_ctl_socket != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700540 sock_release(dccp_ctl_socket);
Arnaldo Carvalho de Melo54808552005-08-18 20:47:02 -0300541 dccp_ctl_socket = NULL;
542 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700543}
544
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300545EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit);
546#endif
547
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700548static int __init init_dccp_v4_mibs(void)
549{
550 int rc = -ENOMEM;
551
552 dccp_statistics[0] = alloc_percpu(struct dccp_mib);
553 if (dccp_statistics[0] == NULL)
554 goto out;
555
556 dccp_statistics[1] = alloc_percpu(struct dccp_mib);
557 if (dccp_statistics[1] == NULL)
558 goto out_free_one;
559
560 rc = 0;
561out:
562 return rc;
563out_free_one:
564 free_percpu(dccp_statistics[0]);
565 dccp_statistics[0] = NULL;
566 goto out;
567
568}
569
570static int thash_entries;
571module_param(thash_entries, int, 0444);
572MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
573
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300574#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700575int dccp_debug;
576module_param(dccp_debug, int, 0444);
577MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300578#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700579
580static int __init dccp_init(void)
581{
582 unsigned long goal;
583 int ehash_order, bhash_order, i;
584 int rc = proto_register(&dccp_v4_prot, 1);
585
586 if (rc)
587 goto out;
588
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300589 dccp_hashinfo.bind_bucket_cachep =
590 kmem_cache_create("dccp_bind_bucket",
591 sizeof(struct inet_bind_bucket), 0,
592 SLAB_HWCACHE_ALIGN, NULL, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700593 if (!dccp_hashinfo.bind_bucket_cachep)
594 goto out_proto_unregister;
595
596 /*
597 * Size and allocate the main established and bind bucket
598 * hash tables.
599 *
600 * The methodology is similar to that of the buffer cache.
601 */
602 if (num_physpages >= (128 * 1024))
603 goal = num_physpages >> (21 - PAGE_SHIFT);
604 else
605 goal = num_physpages >> (23 - PAGE_SHIFT);
606
607 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300608 goal = (thash_entries *
609 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700610 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
611 ;
612 do {
613 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
614 sizeof(struct inet_ehash_bucket);
615 dccp_hashinfo.ehash_size >>= 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300616 while (dccp_hashinfo.ehash_size &
617 (dccp_hashinfo.ehash_size - 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700618 dccp_hashinfo.ehash_size--;
619 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
620 __get_free_pages(GFP_ATOMIC, ehash_order);
621 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
622
623 if (!dccp_hashinfo.ehash) {
624 printk(KERN_CRIT "Failed to allocate DCCP "
625 "established hash table\n");
626 goto out_free_bind_bucket_cachep;
627 }
628
629 for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) {
630 rwlock_init(&dccp_hashinfo.ehash[i].lock);
631 INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
632 }
633
634 bhash_order = ehash_order;
635
636 do {
637 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
638 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300639 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
640 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700641 continue;
642 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
643 __get_free_pages(GFP_ATOMIC, bhash_order);
644 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
645
646 if (!dccp_hashinfo.bhash) {
647 printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n");
648 goto out_free_dccp_ehash;
649 }
650
651 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
652 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
653 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
654 }
655
656 if (init_dccp_v4_mibs())
657 goto out_free_dccp_bhash;
658
659 rc = -EAGAIN;
660 if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP))
661 goto out_free_dccp_v4_mibs;
662
663 inet_register_protosw(&dccp_v4_protosw);
664
665 rc = dccp_ctl_sock_init();
666 if (rc)
667 goto out_unregister_protosw;
668out:
669 return rc;
670out_unregister_protosw:
671 inet_unregister_protosw(&dccp_v4_protosw);
672 inet_del_protocol(&dccp_protocol, IPPROTO_DCCP);
673out_free_dccp_v4_mibs:
674 free_percpu(dccp_statistics[0]);
675 free_percpu(dccp_statistics[1]);
676 dccp_statistics[0] = dccp_statistics[1] = NULL;
677out_free_dccp_bhash:
678 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
679 dccp_hashinfo.bhash = NULL;
680out_free_dccp_ehash:
681 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
682 dccp_hashinfo.ehash = NULL;
683out_free_bind_bucket_cachep:
684 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
685 dccp_hashinfo.bind_bucket_cachep = NULL;
686out_proto_unregister:
687 proto_unregister(&dccp_v4_prot);
688 goto out;
689}
690
691static const char dccp_del_proto_err_msg[] __exitdata =
692 KERN_ERR "can't remove dccp net_protocol\n";
693
694static void __exit dccp_fini(void)
695{
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700696 inet_unregister_protosw(&dccp_v4_protosw);
697
698 if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0)
699 printk(dccp_del_proto_err_msg);
700
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300701 free_percpu(dccp_statistics[0]);
702 free_percpu(dccp_statistics[1]);
703 free_pages((unsigned long)dccp_hashinfo.bhash,
704 get_order(dccp_hashinfo.bhash_size *
705 sizeof(struct inet_bind_hashbucket)));
706 free_pages((unsigned long)dccp_hashinfo.ehash,
707 get_order(dccp_hashinfo.ehash_size *
708 sizeof(struct inet_ehash_bucket)));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700709 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300710 proto_unregister(&dccp_v4_prot);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700711}
712
713module_init(dccp_init);
714module_exit(dccp_fini);
715
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700716/*
717 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
718 * values directly, Also cover the case where the protocol is not specified,
719 * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
720 */
721MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
722MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700723MODULE_LICENSE("GPL");
724MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
725MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");