]>
Commit | Line | Data |
---|---|---|
7c657876 ACM |
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/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, | |
a1d3a355 | 150 | char __user *optval, int optlen) |
7c657876 ACM |
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, | |
a1d3a355 | 161 | char __user *optval, int __user *optlen) |
7c657876 ACM |
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); | |
20472af9 ACM |
209 | /* |
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; | |
7c657876 ACM |
221 | out_release: |
222 | release_sock(sk); | |
223 | return rc ? : len; | |
27258ee5 ACM |
224 | out_discard: |
225 | kfree_skb(skb); | |
7c657876 | 226 | goto out_release; |
7c657876 ACM |
227 | } |
228 | ||
7c657876 ACM |
229 | int 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; | |
7c657876 ACM |
233 | long timeo; |
234 | ||
235 | lock_sock(sk); | |
236 | ||
531669a0 ACM |
237 | if (sk->sk_state == DCCP_LISTEN) { |
238 | len = -ENOTCONN; | |
7c657876 | 239 | goto out; |
7c657876 | 240 | } |
7c657876 | 241 | |
531669a0 | 242 | timeo = sock_rcvtimeo(sk, nonblock); |
7c657876 ACM |
243 | |
244 | do { | |
531669a0 | 245 | struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); |
7c657876 | 246 | |
531669a0 ACM |
247 | if (skb == NULL) |
248 | goto verify_sock_status; | |
7c657876 | 249 | |
531669a0 | 250 | dh = dccp_hdr(skb); |
7c657876 | 251 | |
531669a0 ACM |
252 | if (dh->dccph_type == DCCP_PKT_DATA || |
253 | dh->dccph_type == DCCP_PKT_DATAACK) | |
254 | goto found_ok_skb; | |
7c657876 | 255 | |
531669a0 ACM |
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; | |
261 | } | |
262 | dccp_pr_debug("packet_type=%s\n", | |
263 | dccp_packet_name(dh->dccph_type)); | |
264 | sk_eat_skb(sk, skb); | |
265 | verify_sock_status: | |
266 | if (sock_flag(sk, SOCK_DONE)) { | |
267 | len = 0; | |
7c657876 | 268 | break; |
531669a0 | 269 | } |
7c657876 | 270 | |
531669a0 ACM |
271 | if (sk->sk_err) { |
272 | len = sock_error(sk); | |
273 | break; | |
274 | } | |
7c657876 | 275 | |
531669a0 ACM |
276 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
277 | len = 0; | |
278 | break; | |
279 | } | |
7c657876 | 280 | |
531669a0 ACM |
281 | 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; | |
7c657876 ACM |
287 | break; |
288 | } | |
531669a0 ACM |
289 | len = 0; |
290 | break; | |
7c657876 ACM |
291 | } |
292 | ||
531669a0 ACM |
293 | if (!timeo) { |
294 | len = -EAGAIN; | |
295 | break; | |
296 | } | |
7c657876 | 297 | |
531669a0 ACM |
298 | if (signal_pending(current)) { |
299 | len = sock_intr_errno(timeo); | |
300 | break; | |
301 | } | |
7c657876 | 302 | |
531669a0 | 303 | sk_wait_data(sk, &timeo); |
7c657876 | 304 | continue; |
7c657876 | 305 | found_ok_skb: |
531669a0 ACM |
306 | if (len > skb->len) |
307 | len = skb->len; | |
308 | else if (len < skb->len) | |
309 | msg->msg_flags |= MSG_TRUNC; | |
310 | ||
311 | if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) { | |
312 | /* Exception. Bailout! */ | |
313 | len = -EFAULT; | |
314 | break; | |
7c657876 | 315 | } |
7c657876 ACM |
316 | found_fin_ok: |
317 | if (!(flags & MSG_PEEK)) | |
318 | sk_eat_skb(sk, skb); | |
319 | break; | |
531669a0 | 320 | } while (1); |
7c657876 ACM |
321 | out: |
322 | release_sock(sk); | |
531669a0 | 323 | return len; |
7c657876 ACM |
324 | } |
325 | ||
326 | static 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 | ||
357 | out: | |
358 | release_sock(sk); | |
359 | return err; | |
360 | } | |
361 | ||
362 | static const unsigned char dccp_new_state[] = { | |
7690af3f ACM |
363 | /* 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, | |
7c657876 ACM |
373 | }; |
374 | ||
375 | static 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 | ||
386 | void 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)) { | |
7ad07e7c | 417 | dccp_send_close(sk, 1); |
7c657876 ACM |
418 | } |
419 | ||
420 | sk_stream_wait_close(sk, timeout); | |
421 | ||
422 | adjudge_to_death: | |
7ad07e7c ACM |
423 | /* |
424 | * It is the last release_sock in its life. It will remove backlog. | |
425 | */ | |
7c657876 ACM |
426 | 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); | |
7ad07e7c ACM |
437 | |
438 | /* | |
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 */ | |
7c657876 | 452 | dccp_set_state(sk, DCCP_CLOSED); |
7ad07e7c ACM |
453 | #endif |
454 | } | |
7c657876 | 455 | |
7ad07e7c | 456 | atomic_inc(sk->sk_prot->orphan_count); |
7c657876 ACM |
457 | 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 | ||
467 | void dccp_shutdown(struct sock *sk, int how) | |
468 | { | |
469 | dccp_pr_debug("entry\n"); | |
470 | } | |
471 | ||
a1d3a355 | 472 | static struct proto_ops inet_dccp_ops = { |
7c657876 ACM |
473 | .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, | |
7690af3f ACM |
483 | /* FIXME: work on inet_listen to rename it to sock_common_listen */ |
484 | .listen = inet_dccp_listen, | |
7c657876 ACM |
485 | .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 | ||
494 | extern struct net_proto_family inet_family_ops; | |
495 | ||
496 | static 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 | */ | |
511 | struct socket *dccp_ctl_socket; | |
512 | ||
513 | static char dccp_ctl_socket_err_msg[] __initdata = | |
514 | KERN_ERR "DCCP: Failed to create the control socket.\n"; | |
515 | ||
516 | static 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 | ||
725ba8ee ACM |
536 | #ifdef CONFIG_IP_DCCP_UNLOAD_HACK |
537 | void dccp_ctl_sock_exit(void) | |
7c657876 | 538 | { |
5480855b | 539 | if (dccp_ctl_socket != NULL) { |
7c657876 | 540 | sock_release(dccp_ctl_socket); |
5480855b ACM |
541 | dccp_ctl_socket = NULL; |
542 | } | |
7c657876 ACM |
543 | } |
544 | ||
725ba8ee ACM |
545 | EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit); |
546 | #endif | |
547 | ||
7c657876 ACM |
548 | static 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; | |
561 | out: | |
562 | return rc; | |
563 | out_free_one: | |
564 | free_percpu(dccp_statistics[0]); | |
565 | dccp_statistics[0] = NULL; | |
566 | goto out; | |
567 | ||
568 | } | |
569 | ||
570 | static int thash_entries; | |
571 | module_param(thash_entries, int, 0444); | |
572 | MODULE_PARM_DESC(thash_entries, "Number of ehash buckets"); | |
573 | ||
a1d3a355 | 574 | #ifdef CONFIG_IP_DCCP_DEBUG |
7c657876 ACM |
575 | int dccp_debug; |
576 | module_param(dccp_debug, int, 0444); | |
577 | MODULE_PARM_DESC(dccp_debug, "Enable debug messages"); | |
a1d3a355 | 578 | #endif |
7c657876 ACM |
579 | |
580 | static 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 | ||
7690af3f ACM |
589 | 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); | |
7c657876 ACM |
593 | 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) | |
7690af3f ACM |
608 | goal = (thash_entries * |
609 | sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT; | |
7c657876 ACM |
610 | 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; | |
7690af3f ACM |
616 | while (dccp_hashinfo.ehash_size & |
617 | (dccp_hashinfo.ehash_size - 1)) | |
7c657876 ACM |
618 | 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); | |
7690af3f ACM |
639 | if ((dccp_hashinfo.bhash_size > (64 * 1024)) && |
640 | bhash_order > 0) | |
7c657876 ACM |
641 | 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; | |
668 | out: | |
669 | return rc; | |
670 | out_unregister_protosw: | |
671 | inet_unregister_protosw(&dccp_v4_protosw); | |
672 | inet_del_protocol(&dccp_protocol, IPPROTO_DCCP); | |
673 | out_free_dccp_v4_mibs: | |
674 | free_percpu(dccp_statistics[0]); | |
675 | free_percpu(dccp_statistics[1]); | |
676 | dccp_statistics[0] = dccp_statistics[1] = NULL; | |
677 | out_free_dccp_bhash: | |
678 | free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order); | |
679 | dccp_hashinfo.bhash = NULL; | |
680 | out_free_dccp_ehash: | |
681 | free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order); | |
682 | dccp_hashinfo.ehash = NULL; | |
683 | out_free_bind_bucket_cachep: | |
684 | kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); | |
685 | dccp_hashinfo.bind_bucket_cachep = NULL; | |
686 | out_proto_unregister: | |
687 | proto_unregister(&dccp_v4_prot); | |
688 | goto out; | |
689 | } | |
690 | ||
691 | static const char dccp_del_proto_err_msg[] __exitdata = | |
692 | KERN_ERR "can't remove dccp net_protocol\n"; | |
693 | ||
694 | static void __exit dccp_fini(void) | |
695 | { | |
7c657876 ACM |
696 | 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 | ||
725ba8ee ACM |
701 | 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))); | |
7c657876 | 709 | kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep); |
725ba8ee | 710 | proto_unregister(&dccp_v4_prot); |
7c657876 ACM |
711 | } |
712 | ||
713 | module_init(dccp_init); | |
714 | module_exit(dccp_fini); | |
715 | ||
bb97d31f ACM |
716 | /* |
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 | */ | |
721 | MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6"); | |
722 | MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6"); | |
7c657876 ACM |
723 | MODULE_LICENSE("GPL"); |
724 | MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>"); | |
725 | MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol"); |