endpointola.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* SCTP kernel reference Implementation
  2. * Copyright (c) 1999-2000 Cisco, Inc.
  3. * Copyright (c) 1999-2001 Motorola, Inc.
  4. * Copyright (c) 2001-2002 International Business Machines, Corp.
  5. * Copyright (c) 2001 Intel Corp.
  6. * Copyright (c) 2001 Nokia, Inc.
  7. * Copyright (c) 2001 La Monte H.P. Yarroll
  8. *
  9. * This file is part of the SCTP kernel reference Implementation
  10. *
  11. * This abstraction represents an SCTP endpoint.
  12. *
  13. * This file is part of the implementation of the add-IP extension,
  14. * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
  15. * for the SCTP kernel reference Implementation.
  16. *
  17. * The SCTP reference implementation is free software;
  18. * you can redistribute it and/or modify it under the terms of
  19. * the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2, or (at your option)
  21. * any later version.
  22. *
  23. * The SCTP reference implementation is distributed in the hope that it
  24. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  25. * ************************
  26. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. * See the GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with GNU CC; see the file COPYING. If not, write to
  31. * the Free Software Foundation, 59 Temple Place - Suite 330,
  32. * Boston, MA 02111-1307, USA.
  33. *
  34. * Please send any bug reports or fixes you make to the
  35. * email address(es):
  36. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  37. *
  38. * Or submit a bug report through the following website:
  39. * http://www.sf.net/projects/lksctp
  40. *
  41. * Written or modified by:
  42. * La Monte H.P. Yarroll <piggy@acm.org>
  43. * Karl Knutson <karl@athena.chicago.il.us>
  44. * Jon Grimm <jgrimm@austin.ibm.com>
  45. * Daisy Chang <daisyc@us.ibm.com>
  46. * Dajiang Zhang <dajiang.zhang@nokia.com>
  47. *
  48. * Any bugs reported given to us we will try to fix... any fixes shared will
  49. * be incorporated into the next SCTP release.
  50. */
  51. #include <linux/types.h>
  52. #include <linux/sched.h>
  53. #include <linux/slab.h>
  54. #include <linux/in.h>
  55. #include <linux/random.h> /* get_random_bytes() */
  56. #include <linux/crypto.h>
  57. #include <net/sock.h>
  58. #include <net/ipv6.h>
  59. #include <net/sctp/sctp.h>
  60. #include <net/sctp/sm.h>
  61. /* Forward declarations for internal helpers. */
  62. static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep);
  63. /*
  64. * Initialize the base fields of the endpoint structure.
  65. */
  66. static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
  67. struct sock *sk, int gfp)
  68. {
  69. struct sctp_sock *sp = sctp_sk(sk);
  70. memset(ep, 0, sizeof(struct sctp_endpoint));
  71. /* Initialize the base structure. */
  72. /* What type of endpoint are we? */
  73. ep->base.type = SCTP_EP_TYPE_SOCKET;
  74. /* Initialize the basic object fields. */
  75. atomic_set(&ep->base.refcnt, 1);
  76. ep->base.dead = 0;
  77. ep->base.malloced = 1;
  78. /* Create an input queue. */
  79. sctp_inq_init(&ep->base.inqueue);
  80. /* Set its top-half handler */
  81. sctp_inq_set_th_handler(&ep->base.inqueue,
  82. (void (*)(void *))sctp_endpoint_bh_rcv, ep);
  83. /* Initialize the bind addr area */
  84. sctp_bind_addr_init(&ep->base.bind_addr, 0);
  85. rwlock_init(&ep->base.addr_lock);
  86. /* Remember who we are attached to. */
  87. ep->base.sk = sk;
  88. sock_hold(ep->base.sk);
  89. /* Create the lists of associations. */
  90. INIT_LIST_HEAD(&ep->asocs);
  91. /* Set up the base timeout information. */
  92. ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
  93. ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
  94. msecs_to_jiffies(sp->rtoinfo.srto_initial);
  95. ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
  96. msecs_to_jiffies(sp->rtoinfo.srto_initial);
  97. ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
  98. msecs_to_jiffies(sp->rtoinfo.srto_initial);
  99. ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
  100. ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
  101. /* sctpimpguide-05 Section 2.12.2
  102. * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
  103. * recommended value of 5 times 'RTO.Max'.
  104. */
  105. ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
  106. = 5 * msecs_to_jiffies(sp->rtoinfo.srto_max);
  107. ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
  108. ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] = sctp_sack_timeout;
  109. ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
  110. /* Use SCTP specific send buffer space queues. */
  111. ep->sndbuf_policy = sctp_sndbuf_policy;
  112. sk->sk_write_space = sctp_write_space;
  113. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  114. /* Initialize the secret key used with cookie. */
  115. get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
  116. ep->last_key = ep->current_key = 0;
  117. ep->key_changed_at = jiffies;
  118. return ep;
  119. }
  120. /* Create a sctp_endpoint with all that boring stuff initialized.
  121. * Returns NULL if there isn't enough memory.
  122. */
  123. struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, int gfp)
  124. {
  125. struct sctp_endpoint *ep;
  126. /* Build a local endpoint. */
  127. ep = t_new(struct sctp_endpoint, gfp);
  128. if (!ep)
  129. goto fail;
  130. if (!sctp_endpoint_init(ep, sk, gfp))
  131. goto fail_init;
  132. ep->base.malloced = 1;
  133. SCTP_DBG_OBJCNT_INC(ep);
  134. return ep;
  135. fail_init:
  136. kfree(ep);
  137. fail:
  138. return NULL;
  139. }
  140. /* Add an association to an endpoint. */
  141. void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
  142. struct sctp_association *asoc)
  143. {
  144. struct sock *sk = ep->base.sk;
  145. /* Now just add it to our list of asocs */
  146. list_add_tail(&asoc->asocs, &ep->asocs);
  147. /* Increment the backlog value for a TCP-style listening socket. */
  148. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  149. sk->sk_ack_backlog++;
  150. }
  151. /* Free the endpoint structure. Delay cleanup until
  152. * all users have released their reference count on this structure.
  153. */
  154. void sctp_endpoint_free(struct sctp_endpoint *ep)
  155. {
  156. ep->base.dead = 1;
  157. sctp_endpoint_put(ep);
  158. }
  159. /* Final destructor for endpoint. */
  160. static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
  161. {
  162. SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
  163. ep->base.sk->sk_state = SCTP_SS_CLOSED;
  164. /* Unlink this endpoint, so we can't find it again! */
  165. sctp_unhash_endpoint(ep);
  166. /* Free up the HMAC transform. */
  167. if (sctp_sk(ep->base.sk)->hmac)
  168. sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
  169. /* Cleanup. */
  170. sctp_inq_free(&ep->base.inqueue);
  171. sctp_bind_addr_free(&ep->base.bind_addr);
  172. /* Remove and free the port */
  173. if (sctp_sk(ep->base.sk)->bind_hash)
  174. sctp_put_port(ep->base.sk);
  175. /* Give up our hold on the sock. */
  176. if (ep->base.sk)
  177. sock_put(ep->base.sk);
  178. /* Finally, free up our memory. */
  179. if (ep->base.malloced) {
  180. kfree(ep);
  181. SCTP_DBG_OBJCNT_DEC(ep);
  182. }
  183. }
  184. /* Hold a reference to an endpoint. */
  185. void sctp_endpoint_hold(struct sctp_endpoint *ep)
  186. {
  187. atomic_inc(&ep->base.refcnt);
  188. }
  189. /* Release a reference to an endpoint and clean up if there are
  190. * no more references.
  191. */
  192. void sctp_endpoint_put(struct sctp_endpoint *ep)
  193. {
  194. if (atomic_dec_and_test(&ep->base.refcnt))
  195. sctp_endpoint_destroy(ep);
  196. }
  197. /* Is this the endpoint we are looking for? */
  198. struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
  199. const union sctp_addr *laddr)
  200. {
  201. struct sctp_endpoint *retval;
  202. sctp_read_lock(&ep->base.addr_lock);
  203. if (ep->base.bind_addr.port == laddr->v4.sin_port) {
  204. if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
  205. sctp_sk(ep->base.sk))) {
  206. retval = ep;
  207. goto out;
  208. }
  209. }
  210. retval = NULL;
  211. out:
  212. sctp_read_unlock(&ep->base.addr_lock);
  213. return retval;
  214. }
  215. /* Find the association that goes with this chunk.
  216. * We do a linear search of the associations for this endpoint.
  217. * We return the matching transport address too.
  218. */
  219. static struct sctp_association *__sctp_endpoint_lookup_assoc(
  220. const struct sctp_endpoint *ep,
  221. const union sctp_addr *paddr,
  222. struct sctp_transport **transport)
  223. {
  224. int rport;
  225. struct sctp_association *asoc;
  226. struct list_head *pos;
  227. rport = paddr->v4.sin_port;
  228. list_for_each(pos, &ep->asocs) {
  229. asoc = list_entry(pos, struct sctp_association, asocs);
  230. if (rport == asoc->peer.port) {
  231. sctp_read_lock(&asoc->base.addr_lock);
  232. *transport = sctp_assoc_lookup_paddr(asoc, paddr);
  233. sctp_read_unlock(&asoc->base.addr_lock);
  234. if (*transport)
  235. return asoc;
  236. }
  237. }
  238. *transport = NULL;
  239. return NULL;
  240. }
  241. /* Lookup association on an endpoint based on a peer address. BH-safe. */
  242. struct sctp_association *sctp_endpoint_lookup_assoc(
  243. const struct sctp_endpoint *ep,
  244. const union sctp_addr *paddr,
  245. struct sctp_transport **transport)
  246. {
  247. struct sctp_association *asoc;
  248. sctp_local_bh_disable();
  249. asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
  250. sctp_local_bh_enable();
  251. return asoc;
  252. }
  253. /* Look for any peeled off association from the endpoint that matches the
  254. * given peer address.
  255. */
  256. int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
  257. const union sctp_addr *paddr)
  258. {
  259. struct list_head *pos;
  260. struct sctp_sockaddr_entry *addr;
  261. struct sctp_bind_addr *bp;
  262. sctp_read_lock(&ep->base.addr_lock);
  263. bp = &ep->base.bind_addr;
  264. list_for_each(pos, &bp->address_list) {
  265. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  266. if (sctp_has_association(&addr->a, paddr)) {
  267. sctp_read_unlock(&ep->base.addr_lock);
  268. return 1;
  269. }
  270. }
  271. sctp_read_unlock(&ep->base.addr_lock);
  272. return 0;
  273. }
  274. /* Do delayed input processing. This is scheduled by sctp_rcv().
  275. * This may be called on BH or task time.
  276. */
  277. static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
  278. {
  279. struct sctp_association *asoc;
  280. struct sock *sk;
  281. struct sctp_transport *transport;
  282. struct sctp_chunk *chunk;
  283. struct sctp_inq *inqueue;
  284. sctp_subtype_t subtype;
  285. sctp_state_t state;
  286. int error = 0;
  287. if (ep->base.dead)
  288. return;
  289. asoc = NULL;
  290. inqueue = &ep->base.inqueue;
  291. sk = ep->base.sk;
  292. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  293. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  294. /* We might have grown an association since last we
  295. * looked, so try again.
  296. *
  297. * This happens when we've just processed our
  298. * COOKIE-ECHO chunk.
  299. */
  300. if (NULL == chunk->asoc) {
  301. asoc = sctp_endpoint_lookup_assoc(ep,
  302. sctp_source(chunk),
  303. &transport);
  304. chunk->asoc = asoc;
  305. chunk->transport = transport;
  306. }
  307. state = asoc ? asoc->state : SCTP_STATE_CLOSED;
  308. /* Remember where the last DATA chunk came from so we
  309. * know where to send the SACK.
  310. */
  311. if (asoc && sctp_chunk_is_data(chunk))
  312. asoc->peer.last_data_from = chunk->transport;
  313. else
  314. SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
  315. if (chunk->transport)
  316. chunk->transport->last_time_heard = jiffies;
  317. error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
  318. ep, asoc, chunk, GFP_ATOMIC);
  319. if (error && chunk)
  320. chunk->pdiscard = 1;
  321. /* Check to see if the endpoint is freed in response to
  322. * the incoming chunk. If so, get out of the while loop.
  323. */
  324. if (!sctp_sk(sk)->ep)
  325. break;
  326. }
  327. }