blob: a3f8a8095f81fd835c73a30c290d4f5440c9c0cf [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 Melo7c657872005-08-09 20:14:34 -0700209out_release:
210 release_sock(sk);
211 return rc ? : len;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700212out_discard:
213 kfree_skb(skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700214 goto out_release;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700215}
216
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700217int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
218 size_t len, int nonblock, int flags, int *addr_len)
219{
220 const struct dccp_hdr *dh;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700221 long timeo;
222
223 lock_sock(sk);
224
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300225 if (sk->sk_state == DCCP_LISTEN) {
226 len = -ENOTCONN;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700227 goto out;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300228 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700229
230 timeo = sock_rcvtimeo(sk, nonblock);
231
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700232 do {
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300233 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700234
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300235 if (skb == NULL)
236 goto verify_sock_status;
237
238 dh = dccp_hdr(skb);
239
240 if (dh->dccph_type == DCCP_PKT_DATA ||
241 dh->dccph_type == DCCP_PKT_DATAACK)
242 goto found_ok_skb;
243
244 if (dh->dccph_type == DCCP_PKT_RESET ||
245 dh->dccph_type == DCCP_PKT_CLOSE) {
246 dccp_pr_debug("found fin ok!\n");
247 len = 0;
248 goto found_fin_ok;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700249 }
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300250 dccp_pr_debug("packet_type=%s\n",
251 dccp_packet_name(dh->dccph_type));
252 sk_eat_skb(sk, skb);
253verify_sock_status:
254 if (sock_flag(sk, SOCK_DONE)) {
255 len = 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700256 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700257 }
258
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300259 if (sk->sk_err) {
260 len = sock_error(sk);
261 break;
262 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700263
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300264 if (sk->sk_shutdown & RCV_SHUTDOWN) {
265 len = 0;
266 break;
267 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700268
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300269 if (sk->sk_state == DCCP_CLOSED) {
270 if (!sock_flag(sk, SOCK_DONE)) {
271 /* This occurs when user tries to read
272 * from never connected socket.
273 */
274 len = -ENOTCONN;
275 break;
276 }
277 len = 0;
278 break;
279 }
280
281 if (!timeo) {
282 len = -EAGAIN;
283 break;
284 }
285
286 if (signal_pending(current)) {
287 len = sock_intr_errno(timeo);
288 break;
289 }
290
291 sk_wait_data(sk, &timeo);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700292 continue;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700293 found_ok_skb:
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300294 if (len > skb->len)
295 len = skb->len;
296 else if (len < skb->len)
297 msg->msg_flags |= MSG_TRUNC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700298
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300299 if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
300 /* Exception. Bailout! */
301 len = -EFAULT;
302 break;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700303 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700304 found_fin_ok:
305 if (!(flags & MSG_PEEK))
306 sk_eat_skb(sk, skb);
307 break;
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300308 } while (1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700309out:
310 release_sock(sk);
Arnaldo Carvalho de Melo531669a2005-08-13 20:35:17 -0300311 return len;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700312}
313
314static int inet_dccp_listen(struct socket *sock, int backlog)
315{
316 struct sock *sk = sock->sk;
317 unsigned char old_state;
318 int err;
319
320 lock_sock(sk);
321
322 err = -EINVAL;
323 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
324 goto out;
325
326 old_state = sk->sk_state;
327 if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
328 goto out;
329
330 /* Really, if the socket is already in listen state
331 * we can only allow the backlog to be adjusted.
332 */
333 if (old_state != DCCP_LISTEN) {
334 /*
335 * FIXME: here it probably should be sk->sk_prot->listen_start
336 * see tcp_listen_start
337 */
338 err = dccp_listen_start(sk);
339 if (err)
340 goto out;
341 }
342 sk->sk_max_ack_backlog = backlog;
343 err = 0;
344
345out:
346 release_sock(sk);
347 return err;
348}
349
350static const unsigned char dccp_new_state[] = {
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300351 /* current state: new state: action: */
352 [0] = DCCP_CLOSED,
353 [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
354 [DCCP_REQUESTING] = DCCP_CLOSED,
355 [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
356 [DCCP_LISTEN] = DCCP_CLOSED,
357 [DCCP_RESPOND] = DCCP_CLOSED,
358 [DCCP_CLOSING] = DCCP_CLOSED,
359 [DCCP_TIME_WAIT] = DCCP_CLOSED,
360 [DCCP_CLOSED] = DCCP_CLOSED,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700361};
362
363static int dccp_close_state(struct sock *sk)
364{
365 const int next = dccp_new_state[sk->sk_state];
366 const int ns = next & DCCP_STATE_MASK;
367
368 if (ns != sk->sk_state)
369 dccp_set_state(sk, ns);
370
371 return next & DCCP_ACTION_FIN;
372}
373
374void dccp_close(struct sock *sk, long timeout)
375{
376 struct sk_buff *skb;
377
378 lock_sock(sk);
379
380 sk->sk_shutdown = SHUTDOWN_MASK;
381
382 if (sk->sk_state == DCCP_LISTEN) {
383 dccp_set_state(sk, DCCP_CLOSED);
384
385 /* Special case. */
386 inet_csk_listen_stop(sk);
387
388 goto adjudge_to_death;
389 }
390
391 /*
392 * We need to flush the recv. buffs. We do this only on the
393 * descriptor close, not protocol-sourced closes, because the
394 *reader process may not have drained the data yet!
395 */
396 /* FIXME: check for unread data */
397 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
398 __kfree_skb(skb);
399 }
400
401 if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
402 /* Check zero linger _after_ checking for unread data. */
403 sk->sk_prot->disconnect(sk, 0);
404 } else if (dccp_close_state(sk)) {
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700405 dccp_send_close(sk, 1);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700406 }
407
408 sk_stream_wait_close(sk, timeout);
409
410adjudge_to_death:
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700411 /*
412 * It is the last release_sock in its life. It will remove backlog.
413 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700414 release_sock(sk);
415 /*
416 * Now socket is owned by kernel and we acquire BH lock
417 * to finish close. No need to check for user refs.
418 */
419 local_bh_disable();
420 bh_lock_sock(sk);
421 BUG_TRAP(!sock_owned_by_user(sk));
422
423 sock_hold(sk);
424 sock_orphan(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700425
Arnaldo Carvalho de Melo7ad07e72005-08-23 21:50:06 -0700426 /*
427 * The last release_sock may have processed the CLOSE or RESET
428 * packet moving sock to CLOSED state, if not we have to fire
429 * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination"
430 * in draft-ietf-dccp-spec-11. -acme
431 */
432 if (sk->sk_state == DCCP_CLOSING) {
433 /* FIXME: should start at 2 * RTT */
434 /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */
435 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
436 inet_csk(sk)->icsk_rto,
437 DCCP_RTO_MAX);
438#if 0
439 /* Yeah, we should use sk->sk_prot->orphan_count, etc */
440 dccp_set_state(sk, DCCP_CLOSED);
441#endif
442 }
443
444 atomic_inc(sk->sk_prot->orphan_count);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700445 if (sk->sk_state == DCCP_CLOSED)
446 inet_csk_destroy_sock(sk);
447
448 /* Otherwise, socket is reprieved until protocol close. */
449
450 bh_unlock_sock(sk);
451 local_bh_enable();
452 sock_put(sk);
453}
454
455void dccp_shutdown(struct sock *sk, int how)
456{
457 dccp_pr_debug("entry\n");
458}
459
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300460static struct proto_ops inet_dccp_ops = {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700461 .family = PF_INET,
462 .owner = THIS_MODULE,
463 .release = inet_release,
464 .bind = inet_bind,
465 .connect = inet_stream_connect,
466 .socketpair = sock_no_socketpair,
467 .accept = inet_accept,
468 .getname = inet_getname,
469 .poll = sock_no_poll,
470 .ioctl = inet_ioctl,
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300471 /* FIXME: work on inet_listen to rename it to sock_common_listen */
472 .listen = inet_dccp_listen,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700473 .shutdown = inet_shutdown,
474 .setsockopt = sock_common_setsockopt,
475 .getsockopt = sock_common_getsockopt,
476 .sendmsg = inet_sendmsg,
477 .recvmsg = sock_common_recvmsg,
478 .mmap = sock_no_mmap,
479 .sendpage = sock_no_sendpage,
480};
481
482extern struct net_proto_family inet_family_ops;
483
484static struct inet_protosw dccp_v4_protosw = {
485 .type = SOCK_DCCP,
486 .protocol = IPPROTO_DCCP,
487 .prot = &dccp_v4_prot,
488 .ops = &inet_dccp_ops,
489 .capability = -1,
490 .no_check = 0,
491 .flags = 0,
492};
493
494/*
495 * This is the global socket data structure used for responding to
496 * the Out-of-the-blue (OOTB) packets. A control sock will be created
497 * for this socket at the initialization time.
498 */
499struct socket *dccp_ctl_socket;
500
501static char dccp_ctl_socket_err_msg[] __initdata =
502 KERN_ERR "DCCP: Failed to create the control socket.\n";
503
504static int __init dccp_ctl_sock_init(void)
505{
506 int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP,
507 &dccp_ctl_socket);
508 if (rc < 0)
509 printk(dccp_ctl_socket_err_msg);
510 else {
511 dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
512 inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1;
513
514 /* Unhash it so that IP input processing does not even
515 * see it, we do not wish this socket to see incoming
516 * packets.
517 */
518 dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk);
519 }
520
521 return rc;
522}
523
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300524#ifdef CONFIG_IP_DCCP_UNLOAD_HACK
525void dccp_ctl_sock_exit(void)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700526{
Arnaldo Carvalho de Melo54808552005-08-18 20:47:02 -0300527 if (dccp_ctl_socket != NULL) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700528 sock_release(dccp_ctl_socket);
Arnaldo Carvalho de Melo54808552005-08-18 20:47:02 -0300529 dccp_ctl_socket = NULL;
530 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700531}
532
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300533EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit);
534#endif
535
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700536static int __init init_dccp_v4_mibs(void)
537{
538 int rc = -ENOMEM;
539
540 dccp_statistics[0] = alloc_percpu(struct dccp_mib);
541 if (dccp_statistics[0] == NULL)
542 goto out;
543
544 dccp_statistics[1] = alloc_percpu(struct dccp_mib);
545 if (dccp_statistics[1] == NULL)
546 goto out_free_one;
547
548 rc = 0;
549out:
550 return rc;
551out_free_one:
552 free_percpu(dccp_statistics[0]);
553 dccp_statistics[0] = NULL;
554 goto out;
555
556}
557
558static int thash_entries;
559module_param(thash_entries, int, 0444);
560MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
561
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300562#ifdef CONFIG_IP_DCCP_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700563int dccp_debug;
564module_param(dccp_debug, int, 0444);
565MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -0300566#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700567
568static int __init dccp_init(void)
569{
570 unsigned long goal;
571 int ehash_order, bhash_order, i;
572 int rc = proto_register(&dccp_v4_prot, 1);
573
574 if (rc)
575 goto out;
576
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300577 dccp_hashinfo.bind_bucket_cachep =
578 kmem_cache_create("dccp_bind_bucket",
579 sizeof(struct inet_bind_bucket), 0,
580 SLAB_HWCACHE_ALIGN, NULL, NULL);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700581 if (!dccp_hashinfo.bind_bucket_cachep)
582 goto out_proto_unregister;
583
584 /*
585 * Size and allocate the main established and bind bucket
586 * hash tables.
587 *
588 * The methodology is similar to that of the buffer cache.
589 */
590 if (num_physpages >= (128 * 1024))
591 goal = num_physpages >> (21 - PAGE_SHIFT);
592 else
593 goal = num_physpages >> (23 - PAGE_SHIFT);
594
595 if (thash_entries)
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300596 goal = (thash_entries *
597 sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700598 for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
599 ;
600 do {
601 dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
602 sizeof(struct inet_ehash_bucket);
603 dccp_hashinfo.ehash_size >>= 1;
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300604 while (dccp_hashinfo.ehash_size &
605 (dccp_hashinfo.ehash_size - 1))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700606 dccp_hashinfo.ehash_size--;
607 dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
608 __get_free_pages(GFP_ATOMIC, ehash_order);
609 } while (!dccp_hashinfo.ehash && --ehash_order > 0);
610
611 if (!dccp_hashinfo.ehash) {
612 printk(KERN_CRIT "Failed to allocate DCCP "
613 "established hash table\n");
614 goto out_free_bind_bucket_cachep;
615 }
616
617 for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) {
618 rwlock_init(&dccp_hashinfo.ehash[i].lock);
619 INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
620 }
621
622 bhash_order = ehash_order;
623
624 do {
625 dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
626 sizeof(struct inet_bind_hashbucket);
Arnaldo Carvalho de Melo7690af32005-08-13 20:34:54 -0300627 if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
628 bhash_order > 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700629 continue;
630 dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
631 __get_free_pages(GFP_ATOMIC, bhash_order);
632 } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
633
634 if (!dccp_hashinfo.bhash) {
635 printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n");
636 goto out_free_dccp_ehash;
637 }
638
639 for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
640 spin_lock_init(&dccp_hashinfo.bhash[i].lock);
641 INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
642 }
643
644 if (init_dccp_v4_mibs())
645 goto out_free_dccp_bhash;
646
647 rc = -EAGAIN;
648 if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP))
649 goto out_free_dccp_v4_mibs;
650
651 inet_register_protosw(&dccp_v4_protosw);
652
653 rc = dccp_ctl_sock_init();
654 if (rc)
655 goto out_unregister_protosw;
656out:
657 return rc;
658out_unregister_protosw:
659 inet_unregister_protosw(&dccp_v4_protosw);
660 inet_del_protocol(&dccp_protocol, IPPROTO_DCCP);
661out_free_dccp_v4_mibs:
662 free_percpu(dccp_statistics[0]);
663 free_percpu(dccp_statistics[1]);
664 dccp_statistics[0] = dccp_statistics[1] = NULL;
665out_free_dccp_bhash:
666 free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
667 dccp_hashinfo.bhash = NULL;
668out_free_dccp_ehash:
669 free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
670 dccp_hashinfo.ehash = NULL;
671out_free_bind_bucket_cachep:
672 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
673 dccp_hashinfo.bind_bucket_cachep = NULL;
674out_proto_unregister:
675 proto_unregister(&dccp_v4_prot);
676 goto out;
677}
678
679static const char dccp_del_proto_err_msg[] __exitdata =
680 KERN_ERR "can't remove dccp net_protocol\n";
681
682static void __exit dccp_fini(void)
683{
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700684 inet_unregister_protosw(&dccp_v4_protosw);
685
686 if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0)
687 printk(dccp_del_proto_err_msg);
688
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300689 free_percpu(dccp_statistics[0]);
690 free_percpu(dccp_statistics[1]);
691 free_pages((unsigned long)dccp_hashinfo.bhash,
692 get_order(dccp_hashinfo.bhash_size *
693 sizeof(struct inet_bind_hashbucket)));
694 free_pages((unsigned long)dccp_hashinfo.ehash,
695 get_order(dccp_hashinfo.ehash_size *
696 sizeof(struct inet_ehash_bucket)));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700697 kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -0300698 proto_unregister(&dccp_v4_prot);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700699}
700
701module_init(dccp_init);
702module_exit(dccp_fini);
703
Arnaldo Carvalho de Melobb97d312005-08-09 20:19:14 -0700704/*
705 * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
706 * values directly, Also cover the case where the protocol is not specified,
707 * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
708 */
709MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
710MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700711MODULE_LICENSE("GPL");
712MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
713MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");