proto.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 __user *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 __user *optval, int __user *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. /*
  172. * XXX we don't use sk_write_queue, so just discard the packet.
  173. * Current plan however is to _use_ sk_write_queue with
  174. * an algorith similar to tcp_sendmsg, where the main difference
  175. * is that in DCCP we have to respect packet boundaries, so
  176. * no coalescing of skbs.
  177. *
  178. * This bug was _quickly_ found & fixed by just looking at an OSTRA
  179. * generated callgraph 8) -acme
  180. */
  181. if (rc != 0)
  182. goto out_discard;
  183. out_release:
  184. release_sock(sk);
  185. return rc ? : len;
  186. out_discard:
  187. kfree_skb(skb);
  188. goto out_release;
  189. }
  190. int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  191. size_t len, int nonblock, int flags, int *addr_len)
  192. {
  193. const struct dccp_hdr *dh;
  194. long timeo;
  195. lock_sock(sk);
  196. if (sk->sk_state == DCCP_LISTEN) {
  197. len = -ENOTCONN;
  198. goto out;
  199. }
  200. timeo = sock_rcvtimeo(sk, nonblock);
  201. do {
  202. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  203. if (skb == NULL)
  204. goto verify_sock_status;
  205. dh = dccp_hdr(skb);
  206. if (dh->dccph_type == DCCP_PKT_DATA ||
  207. dh->dccph_type == DCCP_PKT_DATAACK)
  208. goto found_ok_skb;
  209. if (dh->dccph_type == DCCP_PKT_RESET ||
  210. dh->dccph_type == DCCP_PKT_CLOSE) {
  211. dccp_pr_debug("found fin ok!\n");
  212. len = 0;
  213. goto found_fin_ok;
  214. }
  215. dccp_pr_debug("packet_type=%s\n",
  216. dccp_packet_name(dh->dccph_type));
  217. sk_eat_skb(sk, skb);
  218. verify_sock_status:
  219. if (sock_flag(sk, SOCK_DONE)) {
  220. len = 0;
  221. break;
  222. }
  223. if (sk->sk_err) {
  224. len = sock_error(sk);
  225. break;
  226. }
  227. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  228. len = 0;
  229. break;
  230. }
  231. if (sk->sk_state == DCCP_CLOSED) {
  232. if (!sock_flag(sk, SOCK_DONE)) {
  233. /* This occurs when user tries to read
  234. * from never connected socket.
  235. */
  236. len = -ENOTCONN;
  237. break;
  238. }
  239. len = 0;
  240. break;
  241. }
  242. if (!timeo) {
  243. len = -EAGAIN;
  244. break;
  245. }
  246. if (signal_pending(current)) {
  247. len = sock_intr_errno(timeo);
  248. break;
  249. }
  250. sk_wait_data(sk, &timeo);
  251. continue;
  252. found_ok_skb:
  253. if (len > skb->len)
  254. len = skb->len;
  255. else if (len < skb->len)
  256. msg->msg_flags |= MSG_TRUNC;
  257. if (skb_copy_datagram_iovec(skb, 0, msg->msg_iov, len)) {
  258. /* Exception. Bailout! */
  259. len = -EFAULT;
  260. break;
  261. }
  262. found_fin_ok:
  263. if (!(flags & MSG_PEEK))
  264. sk_eat_skb(sk, skb);
  265. break;
  266. } while (1);
  267. out:
  268. release_sock(sk);
  269. return len;
  270. }
  271. static int inet_dccp_listen(struct socket *sock, int backlog)
  272. {
  273. struct sock *sk = sock->sk;
  274. unsigned char old_state;
  275. int err;
  276. lock_sock(sk);
  277. err = -EINVAL;
  278. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_DCCP)
  279. goto out;
  280. old_state = sk->sk_state;
  281. if (!((1 << old_state) & (DCCPF_CLOSED | DCCPF_LISTEN)))
  282. goto out;
  283. /* Really, if the socket is already in listen state
  284. * we can only allow the backlog to be adjusted.
  285. */
  286. if (old_state != DCCP_LISTEN) {
  287. /*
  288. * FIXME: here it probably should be sk->sk_prot->listen_start
  289. * see tcp_listen_start
  290. */
  291. err = dccp_listen_start(sk);
  292. if (err)
  293. goto out;
  294. }
  295. sk->sk_max_ack_backlog = backlog;
  296. err = 0;
  297. out:
  298. release_sock(sk);
  299. return err;
  300. }
  301. static const unsigned char dccp_new_state[] = {
  302. /* current state: new state: action: */
  303. [0] = DCCP_CLOSED,
  304. [DCCP_OPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
  305. [DCCP_REQUESTING] = DCCP_CLOSED,
  306. [DCCP_PARTOPEN] = DCCP_CLOSING | DCCP_ACTION_FIN,
  307. [DCCP_LISTEN] = DCCP_CLOSED,
  308. [DCCP_RESPOND] = DCCP_CLOSED,
  309. [DCCP_CLOSING] = DCCP_CLOSED,
  310. [DCCP_TIME_WAIT] = DCCP_CLOSED,
  311. [DCCP_CLOSED] = DCCP_CLOSED,
  312. };
  313. static int dccp_close_state(struct sock *sk)
  314. {
  315. const int next = dccp_new_state[sk->sk_state];
  316. const int ns = next & DCCP_STATE_MASK;
  317. if (ns != sk->sk_state)
  318. dccp_set_state(sk, ns);
  319. return next & DCCP_ACTION_FIN;
  320. }
  321. void dccp_close(struct sock *sk, long timeout)
  322. {
  323. struct sk_buff *skb;
  324. lock_sock(sk);
  325. sk->sk_shutdown = SHUTDOWN_MASK;
  326. if (sk->sk_state == DCCP_LISTEN) {
  327. dccp_set_state(sk, DCCP_CLOSED);
  328. /* Special case. */
  329. inet_csk_listen_stop(sk);
  330. goto adjudge_to_death;
  331. }
  332. /*
  333. * We need to flush the recv. buffs. We do this only on the
  334. * descriptor close, not protocol-sourced closes, because the
  335. *reader process may not have drained the data yet!
  336. */
  337. /* FIXME: check for unread data */
  338. while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  339. __kfree_skb(skb);
  340. }
  341. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  342. /* Check zero linger _after_ checking for unread data. */
  343. sk->sk_prot->disconnect(sk, 0);
  344. } else if (dccp_close_state(sk)) {
  345. dccp_send_close(sk, 1);
  346. }
  347. sk_stream_wait_close(sk, timeout);
  348. adjudge_to_death:
  349. /*
  350. * It is the last release_sock in its life. It will remove backlog.
  351. */
  352. release_sock(sk);
  353. /*
  354. * Now socket is owned by kernel and we acquire BH lock
  355. * to finish close. No need to check for user refs.
  356. */
  357. local_bh_disable();
  358. bh_lock_sock(sk);
  359. BUG_TRAP(!sock_owned_by_user(sk));
  360. sock_hold(sk);
  361. sock_orphan(sk);
  362. /*
  363. * The last release_sock may have processed the CLOSE or RESET
  364. * packet moving sock to CLOSED state, if not we have to fire
  365. * the CLOSE/CLOSEREQ retransmission timer, see "8.3. Termination"
  366. * in draft-ietf-dccp-spec-11. -acme
  367. */
  368. if (sk->sk_state == DCCP_CLOSING) {
  369. /* FIXME: should start at 2 * RTT */
  370. /* Timer for repeating the CLOSE/CLOSEREQ until an answer. */
  371. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  372. inet_csk(sk)->icsk_rto,
  373. DCCP_RTO_MAX);
  374. #if 0
  375. /* Yeah, we should use sk->sk_prot->orphan_count, etc */
  376. dccp_set_state(sk, DCCP_CLOSED);
  377. #endif
  378. }
  379. atomic_inc(sk->sk_prot->orphan_count);
  380. if (sk->sk_state == DCCP_CLOSED)
  381. inet_csk_destroy_sock(sk);
  382. /* Otherwise, socket is reprieved until protocol close. */
  383. bh_unlock_sock(sk);
  384. local_bh_enable();
  385. sock_put(sk);
  386. }
  387. void dccp_shutdown(struct sock *sk, int how)
  388. {
  389. dccp_pr_debug("entry\n");
  390. }
  391. static struct proto_ops inet_dccp_ops = {
  392. .family = PF_INET,
  393. .owner = THIS_MODULE,
  394. .release = inet_release,
  395. .bind = inet_bind,
  396. .connect = inet_stream_connect,
  397. .socketpair = sock_no_socketpair,
  398. .accept = inet_accept,
  399. .getname = inet_getname,
  400. .poll = sock_no_poll,
  401. .ioctl = inet_ioctl,
  402. /* FIXME: work on inet_listen to rename it to sock_common_listen */
  403. .listen = inet_dccp_listen,
  404. .shutdown = inet_shutdown,
  405. .setsockopt = sock_common_setsockopt,
  406. .getsockopt = sock_common_getsockopt,
  407. .sendmsg = inet_sendmsg,
  408. .recvmsg = sock_common_recvmsg,
  409. .mmap = sock_no_mmap,
  410. .sendpage = sock_no_sendpage,
  411. };
  412. extern struct net_proto_family inet_family_ops;
  413. static struct inet_protosw dccp_v4_protosw = {
  414. .type = SOCK_DCCP,
  415. .protocol = IPPROTO_DCCP,
  416. .prot = &dccp_v4_prot,
  417. .ops = &inet_dccp_ops,
  418. .capability = -1,
  419. .no_check = 0,
  420. .flags = 0,
  421. };
  422. /*
  423. * This is the global socket data structure used for responding to
  424. * the Out-of-the-blue (OOTB) packets. A control sock will be created
  425. * for this socket at the initialization time.
  426. */
  427. struct socket *dccp_ctl_socket;
  428. static char dccp_ctl_socket_err_msg[] __initdata =
  429. KERN_ERR "DCCP: Failed to create the control socket.\n";
  430. static int __init dccp_ctl_sock_init(void)
  431. {
  432. int rc = sock_create_kern(PF_INET, SOCK_DCCP, IPPROTO_DCCP,
  433. &dccp_ctl_socket);
  434. if (rc < 0)
  435. printk(dccp_ctl_socket_err_msg);
  436. else {
  437. dccp_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
  438. inet_sk(dccp_ctl_socket->sk)->uc_ttl = -1;
  439. /* Unhash it so that IP input processing does not even
  440. * see it, we do not wish this socket to see incoming
  441. * packets.
  442. */
  443. dccp_ctl_socket->sk->sk_prot->unhash(dccp_ctl_socket->sk);
  444. }
  445. return rc;
  446. }
  447. #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
  448. void dccp_ctl_sock_exit(void)
  449. {
  450. if (dccp_ctl_socket != NULL) {
  451. sock_release(dccp_ctl_socket);
  452. dccp_ctl_socket = NULL;
  453. }
  454. }
  455. EXPORT_SYMBOL_GPL(dccp_ctl_sock_exit);
  456. #endif
  457. static int __init init_dccp_v4_mibs(void)
  458. {
  459. int rc = -ENOMEM;
  460. dccp_statistics[0] = alloc_percpu(struct dccp_mib);
  461. if (dccp_statistics[0] == NULL)
  462. goto out;
  463. dccp_statistics[1] = alloc_percpu(struct dccp_mib);
  464. if (dccp_statistics[1] == NULL)
  465. goto out_free_one;
  466. rc = 0;
  467. out:
  468. return rc;
  469. out_free_one:
  470. free_percpu(dccp_statistics[0]);
  471. dccp_statistics[0] = NULL;
  472. goto out;
  473. }
  474. static int thash_entries;
  475. module_param(thash_entries, int, 0444);
  476. MODULE_PARM_DESC(thash_entries, "Number of ehash buckets");
  477. #ifdef CONFIG_IP_DCCP_DEBUG
  478. int dccp_debug;
  479. module_param(dccp_debug, int, 0444);
  480. MODULE_PARM_DESC(dccp_debug, "Enable debug messages");
  481. #endif
  482. static int __init dccp_init(void)
  483. {
  484. unsigned long goal;
  485. int ehash_order, bhash_order, i;
  486. int rc = proto_register(&dccp_v4_prot, 1);
  487. if (rc)
  488. goto out;
  489. dccp_hashinfo.bind_bucket_cachep =
  490. kmem_cache_create("dccp_bind_bucket",
  491. sizeof(struct inet_bind_bucket), 0,
  492. SLAB_HWCACHE_ALIGN, NULL, NULL);
  493. if (!dccp_hashinfo.bind_bucket_cachep)
  494. goto out_proto_unregister;
  495. /*
  496. * Size and allocate the main established and bind bucket
  497. * hash tables.
  498. *
  499. * The methodology is similar to that of the buffer cache.
  500. */
  501. if (num_physpages >= (128 * 1024))
  502. goal = num_physpages >> (21 - PAGE_SHIFT);
  503. else
  504. goal = num_physpages >> (23 - PAGE_SHIFT);
  505. if (thash_entries)
  506. goal = (thash_entries *
  507. sizeof(struct inet_ehash_bucket)) >> PAGE_SHIFT;
  508. for (ehash_order = 0; (1UL << ehash_order) < goal; ehash_order++)
  509. ;
  510. do {
  511. dccp_hashinfo.ehash_size = (1UL << ehash_order) * PAGE_SIZE /
  512. sizeof(struct inet_ehash_bucket);
  513. dccp_hashinfo.ehash_size >>= 1;
  514. while (dccp_hashinfo.ehash_size &
  515. (dccp_hashinfo.ehash_size - 1))
  516. dccp_hashinfo.ehash_size--;
  517. dccp_hashinfo.ehash = (struct inet_ehash_bucket *)
  518. __get_free_pages(GFP_ATOMIC, ehash_order);
  519. } while (!dccp_hashinfo.ehash && --ehash_order > 0);
  520. if (!dccp_hashinfo.ehash) {
  521. printk(KERN_CRIT "Failed to allocate DCCP "
  522. "established hash table\n");
  523. goto out_free_bind_bucket_cachep;
  524. }
  525. for (i = 0; i < (dccp_hashinfo.ehash_size << 1); i++) {
  526. rwlock_init(&dccp_hashinfo.ehash[i].lock);
  527. INIT_HLIST_HEAD(&dccp_hashinfo.ehash[i].chain);
  528. }
  529. bhash_order = ehash_order;
  530. do {
  531. dccp_hashinfo.bhash_size = (1UL << bhash_order) * PAGE_SIZE /
  532. sizeof(struct inet_bind_hashbucket);
  533. if ((dccp_hashinfo.bhash_size > (64 * 1024)) &&
  534. bhash_order > 0)
  535. continue;
  536. dccp_hashinfo.bhash = (struct inet_bind_hashbucket *)
  537. __get_free_pages(GFP_ATOMIC, bhash_order);
  538. } while (!dccp_hashinfo.bhash && --bhash_order >= 0);
  539. if (!dccp_hashinfo.bhash) {
  540. printk(KERN_CRIT "Failed to allocate DCCP bind hash table\n");
  541. goto out_free_dccp_ehash;
  542. }
  543. for (i = 0; i < dccp_hashinfo.bhash_size; i++) {
  544. spin_lock_init(&dccp_hashinfo.bhash[i].lock);
  545. INIT_HLIST_HEAD(&dccp_hashinfo.bhash[i].chain);
  546. }
  547. if (init_dccp_v4_mibs())
  548. goto out_free_dccp_bhash;
  549. rc = -EAGAIN;
  550. if (inet_add_protocol(&dccp_protocol, IPPROTO_DCCP))
  551. goto out_free_dccp_v4_mibs;
  552. inet_register_protosw(&dccp_v4_protosw);
  553. rc = dccp_ctl_sock_init();
  554. if (rc)
  555. goto out_unregister_protosw;
  556. out:
  557. return rc;
  558. out_unregister_protosw:
  559. inet_unregister_protosw(&dccp_v4_protosw);
  560. inet_del_protocol(&dccp_protocol, IPPROTO_DCCP);
  561. out_free_dccp_v4_mibs:
  562. free_percpu(dccp_statistics[0]);
  563. free_percpu(dccp_statistics[1]);
  564. dccp_statistics[0] = dccp_statistics[1] = NULL;
  565. out_free_dccp_bhash:
  566. free_pages((unsigned long)dccp_hashinfo.bhash, bhash_order);
  567. dccp_hashinfo.bhash = NULL;
  568. out_free_dccp_ehash:
  569. free_pages((unsigned long)dccp_hashinfo.ehash, ehash_order);
  570. dccp_hashinfo.ehash = NULL;
  571. out_free_bind_bucket_cachep:
  572. kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
  573. dccp_hashinfo.bind_bucket_cachep = NULL;
  574. out_proto_unregister:
  575. proto_unregister(&dccp_v4_prot);
  576. goto out;
  577. }
  578. static const char dccp_del_proto_err_msg[] __exitdata =
  579. KERN_ERR "can't remove dccp net_protocol\n";
  580. static void __exit dccp_fini(void)
  581. {
  582. inet_unregister_protosw(&dccp_v4_protosw);
  583. if (inet_del_protocol(&dccp_protocol, IPPROTO_DCCP) < 0)
  584. printk(dccp_del_proto_err_msg);
  585. free_percpu(dccp_statistics[0]);
  586. free_percpu(dccp_statistics[1]);
  587. free_pages((unsigned long)dccp_hashinfo.bhash,
  588. get_order(dccp_hashinfo.bhash_size *
  589. sizeof(struct inet_bind_hashbucket)));
  590. free_pages((unsigned long)dccp_hashinfo.ehash,
  591. get_order(dccp_hashinfo.ehash_size *
  592. sizeof(struct inet_ehash_bucket)));
  593. kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
  594. proto_unregister(&dccp_v4_prot);
  595. }
  596. module_init(dccp_init);
  597. module_exit(dccp_fini);
  598. /*
  599. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  600. * values directly, Also cover the case where the protocol is not specified,
  601. * i.e. net-pf-PF_INET-proto-0-type-SOCK_DCCP
  602. */
  603. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-33-type-6");
  604. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-0-type-6");
  605. MODULE_LICENSE("GPL");
  606. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@conectiva.com.br>");
  607. MODULE_DESCRIPTION("DCCP - Datagram Congestion Controlled Protocol");