]>
Commit | Line | Data |
---|---|---|
7c657876 ACM |
1 | /* |
2 | * net/dccp/proto.c | |
3 | * | |
4 | * An implementation of the DCCP protocol | |
5 | * Arnaldo Carvalho de Melo <[email protected]> | |
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 | ||
42 | DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics); | |
43 | ||
44 | atomic_t dccp_orphan_count = ATOMIC_INIT(0); | |
45 | ||
46 | static struct net_protocol dccp_protocol = { | |
47 | .handler = dccp_v4_rcv, | |
48 | .err_handler = dccp_v4_err, | |
49 | }; | |
50 | ||
51 | const 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 | ||
72 | EXPORT_SYMBOL_GPL(dccp_packet_name); | |
73 | ||
74 | const 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 | ||
93 | EXPORT_SYMBOL_GPL(dccp_state_name); | |
94 | ||
95 | static 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 | ||
101 | int 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 | ||
143 | int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg) | |
144 | { | |
145 | dccp_pr_debug("entry\n"); | |
146 | return -ENOIOCTLCMD; | |
147 | } | |
148 | ||
149 | int dccp_setsockopt(struct sock *sk, int level, int optname, | |
150 | char *optval, int optlen) | |
151 | { | |
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 | ||
160 | int dccp_getsockopt(struct sock *sk, int level, int optname, | |
161 | char *optval, int *optlen) | |
162 | { | |
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 | ||
171 | int 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); | |
27258ee5 | 185 | timeo = sock_sndtimeo(sk, noblock); |
7c657876 ACM |
186 | |
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) | |
27258ee5 | 194 | goto out_release; |
7c657876 ACM |
195 | |
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); | |
7c657876 ACM |
200 | 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); | |
27258ee5 ACM |
205 | if (rc != 0) |
206 | goto out_discard; | |
207 | ||
208 | rc = dccp_write_xmit(sk, skb, len); | |
7c657876 ACM |
209 | out_release: |
210 | release_sock(sk); | |
211 | return rc ? : len; | |
27258ee5 ACM |
212 | out_discard: |
213 | kfree_skb(skb); | |
7c657876 | 214 | goto out_release; |
7c657876 ACM |
215 | } |
216 | ||
217 | EXPORT_SYMBOL(dccp_sendmsg); | |
218 | ||
219 | int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, | |
220 | size_t len, int nonblock, int flags, int *addr_len) | |
221 | { | |
222 | const struct dccp_hdr *dh; | |
223 | int copied = 0; | |
224 | unsigned long used; | |
225 | int err; | |
226 | int target; /* Read at least this many bytes */ | |
227 | long timeo; | |
228 | ||
229 | lock_sock(sk); | |
230 | ||
231 | err = -ENOTCONN; | |
232 | if (sk->sk_state == DCCP_LISTEN) | |
233 | goto out; | |
234 | ||
235 | timeo = sock_rcvtimeo(sk, nonblock); | |
236 | ||
237 | /* Urgent data needs to be handled specially. */ | |
238 | if (flags & MSG_OOB) | |
239 | goto recv_urg; | |
240 | ||
241 | /* FIXME */ | |
242 | #if 0 | |
243 | seq = &tp->copied_seq; | |
244 | if (flags & MSG_PEEK) { | |
245 | peek_seq = tp->copied_seq; | |
246 | seq = &peek_seq; | |
247 | } | |
248 | #endif | |
249 | ||
250 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); | |
251 | ||
252 | do { | |
253 | struct sk_buff *skb; | |
254 | u32 offset; | |
255 | ||
256 | /* FIXME */ | |
257 | #if 0 | |
258 | /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */ | |
259 | if (tp->urg_data && tp->urg_seq == *seq) { | |
260 | if (copied) | |
261 | break; | |
262 | if (signal_pending(current)) { | |
263 | copied = timeo ? sock_intr_errno(timeo) : -EAGAIN; | |
264 | break; | |
265 | } | |
266 | } | |
267 | #endif | |
268 | ||
269 | /* Next get a buffer. */ | |
270 | ||
271 | skb = skb_peek(&sk->sk_receive_queue); | |
272 | do { | |
273 | if (!skb) | |
274 | break; | |
275 | ||
276 | offset = 0; | |
277 | dh = dccp_hdr(skb); | |
278 | ||
279 | if (dh->dccph_type == DCCP_PKT_DATA || | |
280 | dh->dccph_type == DCCP_PKT_DATAACK) | |
281 | goto found_ok_skb; | |
282 | ||
283 | if (dh->dccph_type == DCCP_PKT_RESET || | |
284 | dh->dccph_type == DCCP_PKT_CLOSE) { | |
285 | dccp_pr_debug("found fin ok!\n"); | |
286 | goto found_fin_ok; | |
287 | } | |
288 | dccp_pr_debug("packet_type=%s\n", dccp_packet_name(dh->dccph_type)); | |
289 | BUG_TRAP(flags & MSG_PEEK); | |
290 | skb = skb->next; | |
291 | } while (skb != (struct sk_buff *)&sk->sk_receive_queue); | |
292 | ||
293 | /* Well, if we have backlog, try to process it now yet. */ | |
294 | if (copied >= target && !sk->sk_backlog.tail) | |
295 | break; | |
296 | ||
297 | if (copied) { | |
298 | if (sk->sk_err || | |
299 | sk->sk_state == DCCP_CLOSED || | |
300 | (sk->sk_shutdown & RCV_SHUTDOWN) || | |
301 | !timeo || | |
302 | signal_pending(current) || | |
303 | (flags & MSG_PEEK)) | |
304 | break; | |
305 | } else { | |
306 | if (sock_flag(sk, SOCK_DONE)) | |
307 | break; | |
308 | ||
309 | if (sk->sk_err) { | |
310 | copied = sock_error(sk); | |
311 | break; | |
312 | } | |
313 | ||
314 | if (sk->sk_shutdown & RCV_SHUTDOWN) | |
315 | break; | |
316 | ||
317 | if (sk->sk_state == DCCP_CLOSED) { | |
318 | if (!sock_flag(sk, SOCK_DONE)) { | |
319 | /* This occurs when user tries to read | |
320 | * from never connected socket. | |
321 | */ | |
322 | copied = -ENOTCONN; | |
323 | break; | |
324 | } | |
325 | break; | |
326 | } | |
327 | ||
328 | if (!timeo) { | |
329 | copied = -EAGAIN; | |
330 | break; | |
331 | } | |
332 | ||
333 | if (signal_pending(current)) { | |
334 | copied = sock_intr_errno(timeo); | |
335 | break; | |
336 | } | |
337 | } | |
338 | ||
339 | /* FIXME: cleanup_rbuf(sk, copied); */ | |
340 | ||
341 | if (copied >= target) { | |
342 | /* Do not sleep, just process backlog. */ | |
343 | release_sock(sk); | |
344 | lock_sock(sk); | |
345 | } else | |
346 | sk_wait_data(sk, &timeo); | |
347 | ||
348 | continue; | |
349 | ||
350 | found_ok_skb: | |
351 | /* Ok so how much can we use? */ | |
352 | used = skb->len - offset; | |
353 | if (len < used) | |
354 | used = len; | |
355 | ||
356 | if (!(flags & MSG_TRUNC)) { | |
357 | err = skb_copy_datagram_iovec(skb, offset, | |
358 | msg->msg_iov, used); | |
359 | if (err) { | |
360 | /* Exception. Bailout! */ | |
361 | if (!copied) | |
362 | copied = -EFAULT; | |
363 | break; | |
364 | } | |
365 | } | |
366 | ||
367 | copied += used; | |
368 | len -= used; | |
369 | ||
370 | /* FIXME: tcp_rcv_space_adjust(sk); */ | |
371 | ||
372 | //skip_copy: | |
373 | if (used + offset < skb->len) | |
374 | continue; | |
375 | ||
376 | if (!(flags & MSG_PEEK)) | |
377 | sk_eat_skb(sk, skb); | |
378 | continue; | |
379 | found_fin_ok: | |
380 | if (!(flags & MSG_PEEK)) | |
381 | sk_eat_skb(sk, skb); | |
382 | break; | |
383 | ||
384 | } while (len > 0); | |
385 | ||
386 | /* According to UNIX98, msg_name/msg_namelen are ignored | |
387 | * on connected socket. I was just happy when found this 8) --ANK | |
388 | */ | |
389 | ||
390 | /* Clean up data we have read: This will do ACK frames. */ | |
391 | /* FIXME: cleanup_rbuf(sk, copied); */ | |
392 | ||
393 | release_sock(sk); | |
394 | return copied; | |
395 | ||
396 | out: | |
397 | release_sock(sk); | |
398 | return err; | |
399 | ||
400 | recv_urg: | |
401 | /* FIXME: err = tcp_recv_urg(sk, timeo, msg, len, flags, addr_len); */ | |
402 | goto out; | |
403 | } | |
404 | ||
405 | static int inet_dccp_listen(struct socket *sock, int backlog) | |
406 | { | |
407 | struct sock *sk = sock->sk; | |
408 | unsigned char old_state; | |
409 | int err; | |
410 | ||
411 | lock_sock(sk); | |
412 | ||
413 | err = -EINVAL; | |
414 | if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP) | |
415 | goto out; | |
416 | ||
417 | old_state = sk->sk_state; | |
418 | if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN))) | |
419 | goto out; | |
420 | ||
421 | /* Really, if the socket is already in listen state | |
422 | * we can only allow the backlog to be adjusted. | |
423 | */ | |
424 | if (old_state != DCCP_LISTEN) { | |
425 | /* | |
426 | * FIXME: here it probably should be sk->sk_prot->listen_start | |
427 | * see tcp_listen_start | |
428 | */ | |
429 | err = dccp_listen_start(sk); | |
430 | if (err) | |
431 | goto out; | |
432 | } | |
433 | sk->sk_max_ack_backlog = backlog; | |
434 | err = 0; | |
435 | ||
436 | out: | |
437 | release_sock(sk); | |
438 | return err; | |
439 | } | |
440 | ||
441 | static const unsigned char dccp_new_state[] = { | |
442 | /* current state: new state: action: */ | |
443 | [0] = DCCP_CLOSED, | |
444 | [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, | |
445 | [DCCP_REQUESTING] = DCCP_CLOSED, | |
446 | [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN, | |
447 | [DCCP_LISTEN] = DCCP_CLOSED, | |
448 | [DCCP_RESPOND] = DCCP_CLOSED, | |
449 | [DCCP_CLOSING] = DCCP_CLOSED, | |
450 | [DCCP_TIME_WAIT] = DCCP_CLOSED, | |
451 | [DCCP_CLOSED] = DCCP_CLOSED, | |
452 | }; | |
453 | ||
454 | static int dccp_close_state(struct sock *sk) | |
455 | { | |
456 | const int next = dccp_new_state[sk->sk_state]; | |
457 | const int ns = next & DCCP_STATE_MASK; | |
458 | ||
459 | if (ns != sk->sk_state) | |
460 | dccp_set_state(sk, ns); | |
461 | ||
462 | return next & DCCP_ACTION_FIN; | |
463 | } | |
464 | ||
465 | void dccp_close(struct sock *sk, long timeout) | |
466 | { | |
467 | struct sk_buff *skb; | |
468 | ||
469 | lock_sock(sk); | |
470 | ||
471 | sk->sk_shutdown = SHUTDOWN_MASK; | |
472 | ||
473 | if (sk->sk_state == DCCP_LISTEN) { | |
474 | dccp_set_state(sk, DCCP_CLOSED); | |
475 | ||
476 | /* Special case. */ | |
477 | inet_csk_listen_stop(sk); | |
478 | ||
479 | goto adjudge_to_death; | |
480 | } | |
481 | ||
482 | /* | |
483 | * We need to flush the recv. buffs. We do this only on the | |
484 | * descriptor close, not protocol-sourced closes, because the | |
485 | *reader process may not have drained the data yet! | |
486 | */ | |
487 | /* FIXME: check for unread data */ | |
488 | while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) { | |
489 | __kfree_skb(skb); | |
490 | } | |
491 | ||
492 | if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) { | |
493 | /* Check zero linger _after_ checking for unread data. */ | |
494 | sk->sk_prot->disconnect(sk, 0); | |
495 | } else if (dccp_close_state(sk)) { | |
496 | dccp_send_close(sk); | |
497 | } | |
498 | ||
499 | sk_stream_wait_close(sk, timeout); | |
500 | ||
501 | adjudge_to_death: | |
502 | release_sock(sk); | |
503 | /* | |
504 | * Now socket is owned by kernel and we acquire BH lock | |
505 | * to finish close. No need to check for user refs. | |
506 | */ | |
507 | local_bh_disable(); | |
508 | bh_lock_sock(sk); | |
509 | BUG_TRAP(!sock_owned_by_user(sk)); | |
510 | ||
511 | sock_hold(sk); | |
512 | sock_orphan(sk); | |
513 | ||
514 | if (sk->sk_state != DCCP_CLOSED) | |
515 | dccp_set_state(sk, DCCP_CLOSED); | |
516 | ||
517 | atomic_inc(&dccp_orphan_count); | |
518 | if (sk->sk_state == DCCP_CLOSED) | |
519 | inet_csk_destroy_sock(sk); | |
520 | ||
521 | /* Otherwise, socket is reprieved until protocol close. */ | |
522 | ||
523 | bh_unlock_sock(sk); | |
524 | local_bh_enable(); | |
525 | sock_put(sk); | |
526 | } | |
527 | ||
528 | void dccp_shutdown(struct sock *sk, int how) | |
529 | { | |
530 | dccp_pr_debug("entry\n"); | |
531 | } | |
532 | ||
533 | struct proto_ops inet_dccp_ops = { | |
534 | .family = PF_INET, | |
535 | .owner = THIS_MODULE, | |
536 | .release = inet_release, | |
537 | .bind = inet_bind, | |
538 | .connect = inet_stream_connect, | |
539 | .socketpair = sock_no_socketpair, | |
540 | .accept = inet_accept, | |
541 | .getname = inet_getname, | |
542 | .poll = sock_no_poll, | |
543 | .ioctl = inet_ioctl, | |
544 | .listen = inet_dccp_listen, /* FIXME: work on inet_listen to rename it to sock_common_listen */ | |
545 | .shutdown = inet_shutdown, | |
546 | .setsockopt = sock_common_setsockopt, | |
547 | .getsockopt = sock_common_getsockopt, | |
548 | .sendmsg = inet_sendmsg, | |
549 | .recvmsg = sock_common_recvmsg, | |
550 | .mmap = sock_no_mmap, | |
551 | .sendpage = sock_no_sendpage, | |
552 | }; | |
553 | ||
554 | extern struct net_proto_family inet_family_ops; | |
555 | ||
556 | static struct inet_protosw dccp_v4_protosw = { | |
557 | .type = SOCK_DCCP, | |
558 | .protocol = IPPROTO_DCCP, | |
559 | .prot = &dccp_v4_prot, | |
560 | .ops = &inet_dccp_ops, | |
561 | .capability = -1, | |
562 | .no_check = 0, | |
563 | .flags = 0, | |
564 | }; | |
565 | ||
566 | /* | |
567 | * This is the global socket data structure used for responding to | |
568 | * the Out-of-the-blue (OOTB) packets. A control sock will be created | |
569 | * for this socket at the initialization time. | |
570 | */ | |
571 | struct socket *dccp_ctl_socket; | |
572 | ||
573 | static char dccp_ctl_socket_err_msg[] __initdata = | |
574 | KERN_ERR "DCCP: Failed to create the control socket.\n"; | |
575 | ||
576 | static int __init dccp_ctl_sock_init(void) | |
577 | { | |
578 | int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP, | |
579 | &dccp_ctl_socket); | |
580 | if (rc < 0) | |
581 | printk(dccp_ctl_socket_err_msg); | |
582 | else { | |
583 | dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; | |
584 | inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1; | |
585 | ||
586 | /* Unhash it so that IP input processing does not even | |
587 | * see it, we do not wish this socket to see incoming | |
588 | * packets. | |
589 | */ | |
590 | dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk); | |
591 | } | |
592 | ||
593 | return rc; | |
594 | } | |
595 | ||
596 | static void __exit dccp_ctl_sock_exit(void) | |
597 | { | |
598 | if (dccp_ctl_socket != NULL) | |
599 | sock_release(dccp_ctl_socket); | |
600 | } | |
601 | ||
602 | static int __init init_dccp_v4_mibs(void) | |
603 | { | |
604 | int rc = -ENOMEM; | |
605 | ||
606 | dccp_statistics[0] = alloc_percpu(struct dccp_mib); | |
607 | if (dccp_statistics[0] == NULL) | |
608 | goto out; | |
609 | ||
610 | dccp_statistics[1] = alloc_percpu(struct dccp_mib); | |
611 | if (dccp_statistics[1] == NULL) | |
612 | goto out_free_one; | |
613 | ||
614 | rc = 0; | |
615 | out: | |
616 | return rc; | |
617 | out_free_one: | |
618 | free_percpu(dccp_statistics[0]); | |
619 | dccp_statistics[0] = NULL; | |
620 | goto out; | |
621 | ||
622 | } | |
623 | ||
624 | static int thash_entries; | |
625 | module_param(thash_entries, int, 0444); | |
626 | MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); | |
627 | ||
628 | int dccp_debug; | |
629 | module_param(dccp_debug, int, 0444); | |
630 | MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); | |
631 | ||
632 | static int __init dccp_init(void) | |
633 | { | |
634 | unsigned long goal; | |
635 | int ehash_order, bhash_order, i; | |
636 | int rc = proto_register(&dccp_v4_prot, 1); | |
637 | ||
638 | if (rc) | |
639 | goto out; | |
640 | ||
641 | dccp_hashinfo.bind_bucket_cachep = kmem_cache_create("dccp_bind_bucket", | |
642 | sizeof(struct inet_bind_bucket), | |
643 | 0, SLAB_HWCACHE_ALIGN, | |
644 | NULL, NULL); | |
645 | if (!dccp_hashinfo.bind_bucket_cachep) | |
646 | goto out_proto_unregister; | |
647 | ||
648 | /* | |
649 | * Size and allocate the main established and bind bucket | |
650 | * hash tables. | |
651 | * | |
652 | * The methodology is similar to that of the buffer cache. | |
653 | */ | |
654 | if (num_physpages >= (128 * 1024)) | |
655 | goal = num_physpages >> (21 - PAGE_SHIFT); | |
656 | else | |
657 | goal = num_physpages >> (23 - PAGE_SHIFT); | |
658 | ||
659 | if (thash_entries) | |
660 | goal = (thash_entries * sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; | |
661 | for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++) | |
662 | ; | |
663 | do { | |
664 | dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE / | |
665 | sizeof(struct inet_ehash_bucket); | |
666 | dccp_hashinfo.ehash_size >>= 1; | |
667 | while (dccp_hashinfo.ehash_size & (dccp_hashinfo.ehash_size - 1)) | |
668 | dccp_hashinfo.ehash_size--; | |
669 | dccp_hashinfo.ehash = (struct inet_ehash_bucket *) | |
670 | __get_free_pages(GFP_ATOMIC, ehash_order); | |
671 | } while (!dccp_hashinfo.ehash && --ehash_order > 0); | |
672 | ||
673 | if (!dccp_hashinfo.ehash) { | |
674 | printk(KERN_CRIT "Failed to allocate DCCP " | |
675 | "established hash table\n"); | |
676 | goto out_free_bind_bucket_cachep; | |
677 | } | |
678 | ||
679 | for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) { | |
680 | rwlock_init(&dccp_hashinfo.ehash[i].lock); | |
681 | INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain); | |
682 | } | |
683 | ||
684 | bhash_order = ehash_order; | |
685 | ||
686 | do { | |
687 | dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE / | |
688 | sizeof(struct inet_bind_hashbucket); | |
689 | if ((dccp_hashinfo.bhash_size > (64 * 1024)) && bhash_order > 0) | |
690 | continue; | |
691 | dccp_hashinfo.bhash = (struct inet_bind_hashbucket *) | |
692 | __get_free_pages(GFP_ATOMIC, bhash_order); | |
693 | } while (!dccp_hashinfo.bhash && --bhash_order >= 0); | |
694 | ||
695 | if (!dccp_hashinfo.bhash) { | |
696 | printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n"); | |
697 | goto out_free_dccp_ehash; | |
698 | } | |
699 | ||
700 | for (i = 0; i < dccp_hashinfo.bhash_size; i++) { | |
701 | spin_lock_init(&dccp_hashinfo.bhash[i].lock); | |
702 | INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain); | |
703 | } | |
704 | ||
705 | if (init_dccp_v4_mibs()) | |
706 | goto out_free_dccp_bhash; | |
707 | ||
708 | rc = -EAGAIN; | |
709 | if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP)) | |
710 | goto out_free_dccp_v4_mibs; | |
711 | ||
712 | inet_register_protosw(&dccp_v4_protosw); | |
713 | ||
714 | rc = dccp_ctl_sock_init(); | |
715 | if (rc) | |
716 | goto out_unregister_protosw; | |
717 | out: | |
718 | return rc; | |
719 | out_unregister_protosw: | |
720 | inet_unregister_protosw(&dccp_v4_protosw); | |
721 | inet_del_protocol(&dccp_protocol, IPPROTO_DCCP); | |
722 | out_free_dccp_v4_mibs: | |
723 | free_percpu(dccp_statistics[0]); | |
724 | free_percpu(dccp_statistics[1]); | |
725 | dccp_statistics[0] = dccp_statistics[1] = NULL; | |
726 | out_free_dccp_bhash: | |
727 | free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); | |
728 | dccp_hashinfo.bhash = NULL; | |
729 | out_free_dccp_ehash: | |
730 | free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); | |
731 | dccp_hashinfo.ehash = NULL; | |
732 | out_free_bind_bucket_cachep: | |
733 | kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); | |
734 | dccp_hashinfo.bind_bucket_cachep = NULL; | |
735 | out_proto_unregister: | |
736 | proto_unregister(&dccp_v4_prot); | |
737 | goto out; | |
738 | } | |
739 | ||
740 | static const char dccp_del_proto_err_msg[] __exitdata = | |
741 | KERN_ERR "can't remove dccp net_protocol\n"; | |
742 | ||
743 | static void __exit dccp_fini(void) | |
744 | { | |
745 | dccp_ctl_sock_exit(); | |
746 | ||
747 | inet_unregister_protosw(&dccp_v4_protosw); | |
748 | ||
749 | if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0) | |
750 | printk(dccp_del_proto_err_msg); | |
751 | ||
752 | /* Free the control endpoint. */ | |
753 | sock_release(dccp_ctl_socket); | |
754 | ||
755 | proto_unregister(&dccp_v4_prot); | |
756 | ||
757 | kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); | |
758 | } | |
759 | ||
760 | module_init(dccp_init); | |
761 | module_exit(dccp_fini); | |
762 | ||
bb97d31f ACM |
763 | /* |
764 | * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33) | |
765 | * values directly, Also cover the case where the protocol is not specified, | |
766 | * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP | |
767 | */ | |
768 | MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6"); | |
769 | MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6"); | |
7c657876 ACM |
770 | MODULE_LICENSE("GPL"); |
771 | MODULE_AUTHOR("Arnaldo Carvalho de Melo <[email protected]>"); | |
772 | MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); |