proto.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #include <linux/config.h>
  12. #include <linux/dccp.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/in.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/init.h>
  22. #include <linux/random.h>
  23. #include <net/checksum.h>
  24. #include <net/inet_common.h>
  25. #include <net/ip.h>
  26. #include <net/protocol.h>
  27. #include <net/sock.h>
  28. #include <net/xfrm.h>
  29. #include <asm/semaphore.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/timer.h>
  32. #include <linux/delay.h>
  33. #include <linux/poll.h>
  34. #include <linux/dccp.h>
  35. #include "ccid.h"
  36. #include "dccp.h"
  37. DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics);
  38. atomic_t dccp_orphan_count = ATOMIC_INIT(0);
  39. static struct net_protocol dccp_protocol = {
  40. .handler = dccp_v4_rcv,
  41. .err_handler = dccp_v4_err,
  42. };
  43. const char *dccp_packet_name(const int type)
  44. {
  45. static const char *dccp_packet_names[] = {
  46. [DCCP_PKT_REQUEST] = "REQUEST",
  47. [DCCP_PKT_RESPONSE] = "RESPONSE",
  48. [DCCP_PKT_DATA] = "DATA",
  49. [DCCP_PKT_ACK] = "ACK",
  50. [DCCP_PKT_DATAACK] = "DATAACK",
  51. [DCCP_PKT_CLOSEREQ] = "CLOSEREQ",
  52. [DCCP_PKT_CLOSE] = "CLOSE",
  53. [DCCP_PKT_RESET] = "RESET",
  54. [DCCP_PKT_SYNC] = "SYNC",
  55. [DCCP_PKT_SYNCACK] = "SYNCACK",
  56. };
  57. if (type >= DCCP_NR_PKT_TYPES)
  58. return "INVALID";
  59. else
  60. return dccp_packet_names[type];
  61. }
  62. EXPORT_SYMBOL_GPL(dccp_packet_name);
  63. const char *dccp_state_name(const int state)
  64. {
  65. static char *dccp_state_names[] = {
  66. [DCCP_OPEN] = "OPEN",
  67. [DCCP_REQUESTING] = "REQUESTING",
  68. [DCCP_PARTOPEN] = "PARTOPEN",
  69. [DCCP_LISTEN] = "LISTEN",
  70. [DCCP_RESPOND] = "RESPOND",
  71. [DCCP_CLOSING] = "CLOSING",
  72. [DCCP_TIME_WAIT] = "TIME_WAIT",
  73. [DCCP_CLOSED] = "CLOSED",
  74. };
  75. if (state >= DCCP_MAX_STATES)
  76. return "INVALID STATE!";
  77. else
  78. return dccp_state_names[state];
  79. }
  80. EXPORT_SYMBOL_GPL(dccp_state_name);
  81. static inline int dccp_listen_start(struct sock *sk)
  82. {
  83. dccp_sk(sk)->dccps_role = DCCP_ROLE_LISTEN;
  84. return inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
  85. }
  86. int dccp_disconnect(struct sock *sk, int flags)
  87. {
  88. struct inet_connection_sock *icsk = inet_csk(sk);
  89. struct inet_sock *inet = inet_sk(sk);
  90. int err = 0;
  91. const int old_state = sk->sk_state;
  92. if (old_state != DCCP_CLOSED)
  93. dccp_set_state(sk, DCCP_CLOSED);
  94. /* ABORT function of RFC793 */
  95. if (old_state == DCCP_LISTEN) {
  96. inet_csk_listen_stop(sk);
  97. /* FIXME: do the active reset thing */
  98. } else if (old_state == DCCP_REQUESTING)
  99. sk->sk_err = ECONNRESET;
  100. dccp_clear_xmit_timers(sk);
  101. __skb_queue_purge(&sk->sk_receive_queue);
  102. if (sk->sk_send_head != NULL) {
  103. __kfree_skb(sk->sk_send_head);
  104. sk->sk_send_head = NULL;
  105. }
  106. inet->dport = 0;
  107. if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
  108. inet_reset_saddr(sk);
  109. sk->sk_shutdown = 0;
  110. sock_reset_flag(sk, SOCK_DONE);
  111. icsk->icsk_backoff = 0;
  112. inet_csk_delack_init(sk);
  113. __sk_dst_reset(sk);
  114. BUG_TRAP(!inet->num || icsk->icsk_bind_hash);
  115. sk->sk_error_report(sk);
  116. return err;
  117. }
  118. int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  119. {
  120. dccp_pr_debug("entry\n");
  121. return -ENOIOCTLCMD;
  122. }
  123. int dccp_setsockopt(struct sock *sk, int level, int optname,
  124. char *optval, int optlen)
  125. {
  126. dccp_pr_debug("entry\n");
  127. if (level != SOL_DCCP)
  128. return ip_setsockopt(sk, level, optname, optval, optlen);
  129. return -EOPNOTSUPP;
  130. }
  131. int dccp_getsockopt(struct sock *sk, int level, int optname,
  132. char *optval, int *optlen)
  133. {
  134. dccp_pr_debug("entry\n");
  135. if (level != SOL_DCCP)
  136. return ip_getsockopt(sk, level, optname, optval, optlen);
  137. return -EOPNOTSUPP;
  138. }
  139. int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  140. size_t len)
  141. {
  142. const struct dccp_sock *dp = dccp_sk(sk);
  143. const int flags = msg->msg_flags;
  144. const int noblock = flags & MSG_DONTWAIT;
  145. struct sk_buff *skb;
  146. int rc, size;
  147. long timeo;
  148. if (len > dp->dccps_mss_cache)
  149. return -EMSGSIZE;
  150. lock_sock(sk);
  151. timeo = sock_sndtimeo(sk, noblock);
  152. /*
  153. * We have to use sk_stream_wait_connect here to set sk_write_pending,
  154. * so that the trick in dccp_rcv_request_sent_state_process.
  155. */
  156. /* Wait for a connection to finish. */
  157. if ((1 << sk->sk_state) & ~(DCCPF_OPEN | DCCPF_PARTOPEN | DCCPF_CLOSING))
  158. if ((rc = sk_stream_wait_connect(sk, &timeo)) != 0)
  159. goto out_release;
  160. size = sk->sk_prot->max_header + len;
  161. release_sock(sk);
  162. skb = sock_alloc_send_skb(sk, size, noblock, &rc);
  163. lock_sock(sk);
  164. if (skb == NULL)
  165. goto out_release;
  166. skb_reserve(skb, sk->sk_prot->max_header);
  167. rc = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  168. if (rc != 0)
  169. goto out_discard;
  170. rc = dccp_write_xmit(sk, skb, len);
  171. out_release:
  172. release_sock(sk);
  173. return rc ? : len;
  174. out_discard:
  175. kfree_skb(skb);
  176. goto out_release;
  177. }
  178. EXPORT_SYMBOL(dccp_sendmsg);
  179. int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  180. size_t len, int nonblock, int flags, int *addr_len)
  181. {
  182. const struct dccp_hdr *dh;
  183. int copied = 0;
  184. unsigned long used;
  185. int err;
  186. int target; /* Read at least this many bytes */
  187. long timeo;
  188. lock_sock(sk);
  189. err = -ENOTCONN;
  190. if (sk->sk_state == DCCP_LISTEN)
  191. goto out;
  192. timeo = sock_rcvtimeo(sk, nonblock);
  193. /* Urgent data needs to be handled specially. */
  194. if (flags & MSG_OOB)
  195. goto recv_urg;
  196. /* FIXME */
  197. #if 0
  198. seq = &tp->copied_seq;
  199. if (flags & MSG_PEEK) {
  200. peek_seq = tp->copied_seq;
  201. seq = &peek_seq;
  202. }
  203. #endif
  204. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  205. do {
  206. struct sk_buff *skb;
  207. u32 offset;
  208. /* FIXME */
  209. #if 0
  210. /* Are we at urgent data? Stop if we have read anything or have SIGURG pending. */
  211. if (tp->urg_data && tp->urg_seq == *seq) {
  212. if (copied)
  213. break;
  214. if (signal_pending(current)) {
  215. copied = timeo ? sock_intr_errno(timeo) : -EAGAIN;
  216. break;
  217. }
  218. }
  219. #endif
  220. /* Next get a buffer. */
  221. skb = skb_peek(&sk->sk_receive_queue);
  222. do {
  223. if (!skb)
  224. break;
  225. offset = 0;
  226. dh = dccp_hdr(skb);
  227. if (dh->dccph_type == DCCP_PKT_DATA ||
  228. dh->dccph_type == DCCP_PKT_DATAACK)
  229. goto found_ok_skb;
  230. if (dh->dccph_type == DCCP_PKT_RESET ||
  231. dh->dccph_type == DCCP_PKT_CLOSE) {
  232. dccp_pr_debug("found fin ok!\n");
  233. goto found_fin_ok;
  234. }
  235. dccp_pr_debug("packet_type=%s\n", dccp_packet_name(dh->dccph_type));
  236. BUG_TRAP(flags & MSG_PEEK);
  237. skb = skb->next;
  238. } while (skb != (struct sk_buff *)&sk->sk_receive_queue);
  239. /* Well, if we have backlog, try to process it now yet. */
  240. if (copied >= target && !sk->sk_backlog.tail)
  241. break;
  242. if (copied) {
  243. if (sk->sk_err ||
  244. sk->sk_state == DCCP_CLOSED ||
  245. (sk->sk_shutdown & RCV_SHUTDOWN) ||
  246. !timeo ||
  247. signal_pending(current) ||
  248. (flags & MSG_PEEK))
  249. break;
  250. } else {
  251. if (sock_flag(sk, SOCK_DONE))
  252. break;
  253. if (sk->sk_err) {
  254. copied = sock_error(sk);
  255. break;
  256. }
  257. if (sk->sk_shutdown & RCV_SHUTDOWN)
  258. break;
  259. if (sk->sk_state == DCCP_CLOSED) {
  260. if (!sock_flag(sk, SOCK_DONE)) {
  261. /* This occurs when user tries to read
  262. * from never connected socket.
  263. */
  264. copied = -ENOTCONN;
  265. break;
  266. }
  267. break;
  268. }
  269. if (!timeo) {
  270. copied = -EAGAIN;
  271. break;
  272. }
  273. if (signal_pending(current)) {
  274. copied = sock_intr_errno(timeo);
  275. break;
  276. }
  277. }
  278. /* FIXME: cleanup_rbuf(sk, copied); */
  279. if (copied >= target) {
  280. /* Do not sleep, just process backlog. */
  281. release_sock(sk);
  282. lock_sock(sk);
  283. } else
  284. sk_wait_data(sk, &timeo);
  285. continue;
  286. found_ok_skb:
  287. /* Ok so how much can we use? */
  288. used = skb->len - offset;
  289. if (len < used)
  290. used = len;
  291. if (!(flags & MSG_TRUNC)) {
  292. err = skb_copy_datagram_iovec(skb, offset,
  293. msg->msg_iov, used);
  294. if (err) {
  295. /* Exception. Bailout! */
  296. if (!copied)
  297. copied = -EFAULT;
  298. break;
  299. }
  300. }
  301. copied += used;
  302. len -= used;
  303. /* FIXME: tcp_rcv_space_adjust(sk); */
  304. //skip_copy:
  305. if (used + offset < skb->len)
  306. continue;
  307. if (!(flags & MSG_PEEK))
  308. sk_eat_skb(sk, skb);
  309. continue;
  310. found_fin_ok:
  311. if (!(flags & MSG_PEEK))
  312. sk_eat_skb(sk, skb);
  313. break;
  314. } while (len > 0);
  315. /* According to UNIX98, msg_name/msg_namelen are ignored
  316. * on connected socket. I was just happy when found this 8) --ANK
  317. */
  318. /* Clean up data we have read: This will do ACK frames. */
  319. /* FIXME: cleanup_rbuf(sk, copied); */
  320. release_sock(sk);
  321. return copied;
  322. out:
  323. release_sock(sk);
  324. return err;
  325. recv_urg:
  326. /* FIXME: err = tcp_recv_urg(sk, timeo, msg, len, flags, addr_len); */
  327. goto out;
  328. }
  329. static int inet_dccp_listen(struct socket *sock, int backlog)
  330. {
  331. struct sock *sk = sock->sk;
  332. unsigned char old_state;
  333. int err;
  334. lock_sock(sk);
  335. err = -EINVAL;
  336. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
  337. goto out;
  338. old_state = sk->sk_state;
  339. if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
  340. goto out;
  341. /* Really, if the socket is already in listen state
  342. * we can only allow the backlog to be adjusted.
  343. */
  344. if (old_state != DCCP_LISTEN) {
  345. /*
  346. * FIXME: here it probably should be sk->sk_prot->listen_start
  347. * see tcp_listen_start
  348. */
  349. err = dccp_listen_start(sk);
  350. if (err)
  351. goto out;
  352. }
  353. sk->sk_max_ack_backlog = backlog;
  354. err = 0;
  355. out:
  356. release_sock(sk);
  357. return err;
  358. }
  359. static const unsigned char dccp_new_state[] = {
  360. /* current state: new state: action: */
  361. [0] = DCCP_CLOSED,
  362. [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
  363. [DCCP_REQUESTING] = DCCP_CLOSED,
  364. [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
  365. [DCCP_LISTEN] = DCCP_CLOSED,
  366. [DCCP_RESPOND] = DCCP_CLOSED,
  367. [DCCP_CLOSING] = DCCP_CLOSED,
  368. [DCCP_TIME_WAIT] = DCCP_CLOSED,
  369. [DCCP_CLOSED] = DCCP_CLOSED,
  370. };
  371. static int dccp_close_state(struct sock *sk)
  372. {
  373. const int next = dccp_new_state[sk->sk_state];
  374. const int ns = next & DCCP_STATE_MASK;
  375. if (ns != sk->sk_state)
  376. dccp_set_state(sk, ns);
  377. return next & DCCP_ACTION_FIN;
  378. }
  379. void dccp_close(struct sock *sk, long timeout)
  380. {
  381. struct sk_buff *skb;
  382. lock_sock(sk);
  383. sk->sk_shutdown = SHUTDOWN_MASK;
  384. if (sk->sk_state == DCCP_LISTEN) {
  385. dccp_set_state(sk, DCCP_CLOSED);
  386. /* Special case. */
  387. inet_csk_listen_stop(sk);
  388. goto adjudge_to_death;
  389. }
  390. /*
  391. * We need to flush the recv. buffs. We do this only on the
  392. * descriptor close, not protocol-sourced closes, because the
  393. *reader process may not have drained the data yet!
  394. */
  395. /* FIXME: check for unread data */
  396. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  397. __kfree_skb(skb);
  398. }
  399. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  400. /* Check zero linger _after_ checking for unread data. */
  401. sk->sk_prot->disconnect(sk, 0);
  402. } else if (dccp_close_state(sk)) {
  403. dccp_send_close(sk);
  404. }
  405. sk_stream_wait_close(sk, timeout);
  406. adjudge_to_death:
  407. release_sock(sk);
  408. /*
  409. * Now socket is owned by kernel and we acquire BH lock
  410. * to finish close. No need to check for user refs.
  411. */
  412. local_bh_disable();
  413. bh_lock_sock(sk);
  414. BUG_TRAP(!sock_owned_by_user(sk));
  415. sock_hold(sk);
  416. sock_orphan(sk);
  417. if (sk->sk_state != DCCP_CLOSED)
  418. dccp_set_state(sk, DCCP_CLOSED);
  419. atomic_inc(&dccp_orphan_count);
  420. if (sk->sk_state == DCCP_CLOSED)
  421. inet_csk_destroy_sock(sk);
  422. /* Otherwise, socket is reprieved until protocol close. */
  423. bh_unlock_sock(sk);
  424. local_bh_enable();
  425. sock_put(sk);
  426. }
  427. void dccp_shutdown(struct sock *sk, int how)
  428. {
  429. dccp_pr_debug("entry\n");
  430. }
  431. struct proto_ops inet_dccp_ops = {
  432. .family = PF_INET,
  433. .owner = THIS_MODULE,
  434. .release = inet_release,
  435. .bind = inet_bind,
  436. .connect = inet_stream_connect,
  437. .socketpair = sock_no_socketpair,
  438. .accept = inet_accept,
  439. .getname = inet_getname,
  440. .poll = sock_no_poll,
  441. .ioctl = inet_ioctl,
  442. .listen = inet_dccp_listen, /* FIXME: work on inet_listen to rename it to sock_common_listen */
  443. .shutdown = inet_shutdown,
  444. .setsockopt = sock_common_setsockopt,
  445. .getsockopt = sock_common_getsockopt,
  446. .sendmsg = inet_sendmsg,
  447. .recvmsg = sock_common_recvmsg,
  448. .mmap = sock_no_mmap,
  449. .sendpage = sock_no_sendpage,
  450. };
  451. extern struct net_proto_family inet_family_ops;
  452. static struct inet_protosw dccp_v4_protosw = {
  453. .type = SOCK_DCCP,
  454. .protocol = IPPROTO_DCCP,
  455. .prot = &dccp_v4_prot,
  456. .ops = &inet_dccp_ops,
  457. .capability = -1,
  458. .no_check = 0,
  459. .flags = 0,
  460. };
  461. /*
  462. * This is the global socket data structure used for responding to
  463. * the Out-of-the-blue (OOTB) packets. A control sock will be created
  464. * for this socket at the initialization time.
  465. */
  466. struct socket *dccp_ctl_socket;
  467. static char dccp_ctl_socket_err_msg[] __initdata =
  468. KERN_ERR "DCCP: Failed to create the control socket.\n";
  469. static int __init dccp_ctl_sock_init(void)
  470. {
  471. int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP,
  472. &dccp_ctl_socket);
  473. if (rc < 0)
  474. printk(dccp_ctl_socket_err_msg);
  475. else {
  476. dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
  477. inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1;
  478. /* Unhash it so that IP input processing does not even
  479. * see it, we do not wish this socket to see incoming
  480. * packets.
  481. */
  482. dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk);
  483. }
  484. return rc;
  485. }
  486. static void __exit dccp_ctl_sock_exit(void)
  487. {
  488. if (dccp_ctl_socket != NULL)
  489. sock_release(dccp_ctl_socket);
  490. }
  491. static int __init init_dccp_v4_mibs(void)
  492. {
  493. int rc = -ENOMEM;
  494. dccp_statistics[0] = alloc_percpu(struct dccp_mib);
  495. if (dccp_statistics[0] == NULL)
  496. goto out;
  497. dccp_statistics[1] = alloc_percpu(struct dccp_mib);
  498. if (dccp_statistics[1] == NULL)
  499. goto out_free_one;
  500. rc = 0;
  501. out:
  502. return rc;
  503. out_free_one:
  504. free_percpu(dccp_statistics[0]);
  505. dccp_statistics[0] = NULL;
  506. goto out;
  507. }
  508. static int thash_entries;
  509. module_param(thash_entries, int, 0444);
  510. MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
  511. int dccp_debug;
  512. module_param(dccp_debug, int, 0444);
  513. MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
  514. static int __init dccp_init(void)
  515. {
  516. unsigned long goal;
  517. int ehash_order, bhash_order, i;
  518. int rc = proto_register(&dccp_v4_prot, 1);
  519. if (rc)
  520. goto out;
  521. dccp_hashinfo.bind_bucket_cachep = kmem_cache_create("dccp_bind_bucket",
  522. sizeof(struct inet_bind_bucket),
  523. 0, SLAB_HWCACHE_ALIGN,
  524. NULL, NULL);
  525. if (!dccp_hashinfo.bind_bucket_cachep)
  526. goto out_proto_unregister;
  527. /*
  528. * Size and allocate the main established and bind bucket
  529. * hash tables.
  530. *
  531. * The methodology is similar to that of the buffer cache.
  532. */
  533. if (num_physpages >= (128 * 1024))
  534. goal = num_physpages >> (21 - PAGE_SHIFT);
  535. else
  536. goal = num_physpages >> (23 - PAGE_SHIFT);
  537. if (thash_entries)
  538. goal = (thash_entries * sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
  539. for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
  540. ;
  541. do {
  542. dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
  543. sizeof(struct inet_ehash_bucket);
  544. dccp_hashinfo.ehash_size >>= 1;
  545. while (dccp_hashinfo.ehash_size & (dccp_hashinfo.ehash_size - 1))
  546. dccp_hashinfo.ehash_size--;
  547. dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
  548. __get_free_pages(GFP_ATOMIC, ehash_order);
  549. } while (!dccp_hashinfo.ehash && --ehash_order > 0);
  550. if (!dccp_hashinfo.ehash) {
  551. printk(KERN_CRIT "Failed to allocate DCCP "
  552. "established hash table\n");
  553. goto out_free_bind_bucket_cachep;
  554. }
  555. for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) {
  556. rwlock_init(&dccp_hashinfo.ehash[i].lock);
  557. INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
  558. }
  559. bhash_order = ehash_order;
  560. do {
  561. dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
  562. sizeof(struct inet_bind_hashbucket);
  563. if ((dccp_hashinfo.bhash_size > (64 * 1024)) && bhash_order > 0)
  564. continue;
  565. dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
  566. __get_free_pages(GFP_ATOMIC, bhash_order);
  567. } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
  568. if (!dccp_hashinfo.bhash) {
  569. printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n");
  570. goto out_free_dccp_ehash;
  571. }
  572. for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
  573. spin_lock_init(&dccp_hashinfo.bhash[i].lock);
  574. INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
  575. }
  576. if (init_dccp_v4_mibs())
  577. goto out_free_dccp_bhash;
  578. rc = -EAGAIN;
  579. if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP))
  580. goto out_free_dccp_v4_mibs;
  581. inet_register_protosw(&dccp_v4_protosw);
  582. rc = dccp_ctl_sock_init();
  583. if (rc)
  584. goto out_unregister_protosw;
  585. out:
  586. return rc;
  587. out_unregister_protosw:
  588. inet_unregister_protosw(&dccp_v4_protosw);
  589. inet_del_protocol(&dccp_protocol, IPPROTO_DCCP);
  590. out_free_dccp_v4_mibs:
  591. free_percpu(dccp_statistics[0]);
  592. free_percpu(dccp_statistics[1]);
  593. dccp_statistics[0] = dccp_statistics[1] = NULL;
  594. out_free_dccp_bhash:
  595. free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
  596. dccp_hashinfo.bhash = NULL;
  597. out_free_dccp_ehash:
  598. free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
  599. dccp_hashinfo.ehash = NULL;
  600. out_free_bind_bucket_cachep:
  601. kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
  602. dccp_hashinfo.bind_bucket_cachep = NULL;
  603. out_proto_unregister:
  604. proto_unregister(&dccp_v4_prot);
  605. goto out;
  606. }
  607. static const char dccp_del_proto_err_msg[] __exitdata =
  608. KERN_ERR "can't remove dccp net_protocol\n";
  609. static void __exit dccp_fini(void)
  610. {
  611. dccp_ctl_sock_exit();
  612. inet_unregister_protosw(&dccp_v4_protosw);
  613. if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0)
  614. printk(dccp_del_proto_err_msg);
  615. /* Free the control endpoint. */
  616. sock_release(dccp_ctl_socket);
  617. proto_unregister(&dccp_v4_prot);
  618. kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
  619. }
  620. module_init(dccp_init);
  621. module_exit(dccp_fini);
  622. /*
  623. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  624. * values directly, Also cover the case where the protocol is not specified,
  625. * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
  626. */
  627. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
  628. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
  629. MODULE_LICENSE("GPL");
  630. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
  631. MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");