endpointola.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/slab.h>
  53. #include <linux/in.h>
  54. #include <linux/random.h> /* get_random_bytes() */
  55. #include <linux/crypto.h>
  56. #include <net/sock.h>
  57. #include <net/ipv6.h>
  58. #include <net/sctp/sctp.h>
  59. #include <net/sctp/sm.h>
  60. /* Forward declarations for internal helpers. */
  61. static void sctp_endpoint_bh_rcv(struct work_struct *work);
  62. /*
  63. * Initialize the base fields of the endpoint structure.
  64. */
  65. static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
  66. struct sock *sk,
  67. gfp_t gfp)
  68. {
  69. memset(ep, 0, sizeof(struct sctp_endpoint));
  70. ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
  71. if (!ep->digest)
  72. return NULL;
  73. /* Initialize the base structure. */
  74. /* What type of endpoint are we? */
  75. ep->base.type = SCTP_EP_TYPE_SOCKET;
  76. /* Initialize the basic object fields. */
  77. atomic_set(&ep->base.refcnt, 1);
  78. ep->base.dead = 0;
  79. ep->base.malloced = 1;
  80. /* Create an input queue. */
  81. sctp_inq_init(&ep->base.inqueue);
  82. /* Set its top-half handler */
  83. sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
  84. /* Initialize the bind addr area */
  85. sctp_bind_addr_init(&ep->base.bind_addr, 0);
  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. /* Use SCTP specific send buffer space queues. */
  92. ep->sndbuf_policy = sctp_sndbuf_policy;
  93. sk->sk_write_space = sctp_write_space;
  94. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  95. /* Get the receive buffer policy for this endpoint */
  96. ep->rcvbuf_policy = sctp_rcvbuf_policy;
  97. /* Initialize the secret key used with cookie. */
  98. get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
  99. ep->last_key = ep->current_key = 0;
  100. ep->key_changed_at = jiffies;
  101. return ep;
  102. }
  103. /* Create a sctp_endpoint with all that boring stuff initialized.
  104. * Returns NULL if there isn't enough memory.
  105. */
  106. struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
  107. {
  108. struct sctp_endpoint *ep;
  109. /* Build a local endpoint. */
  110. ep = t_new(struct sctp_endpoint, gfp);
  111. if (!ep)
  112. goto fail;
  113. if (!sctp_endpoint_init(ep, sk, gfp))
  114. goto fail_init;
  115. ep->base.malloced = 1;
  116. SCTP_DBG_OBJCNT_INC(ep);
  117. return ep;
  118. fail_init:
  119. kfree(ep);
  120. fail:
  121. return NULL;
  122. }
  123. /* Add an association to an endpoint. */
  124. void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
  125. struct sctp_association *asoc)
  126. {
  127. struct sock *sk = ep->base.sk;
  128. /* If this is a temporary association, don't bother
  129. * since we'll be removing it shortly and don't
  130. * want anyone to find it anyway.
  131. */
  132. if (asoc->temp)
  133. return;
  134. /* Now just add it to our list of asocs */
  135. list_add_tail(&asoc->asocs, &ep->asocs);
  136. /* Increment the backlog value for a TCP-style listening socket. */
  137. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  138. sk->sk_ack_backlog++;
  139. }
  140. /* Free the endpoint structure. Delay cleanup until
  141. * all users have released their reference count on this structure.
  142. */
  143. void sctp_endpoint_free(struct sctp_endpoint *ep)
  144. {
  145. ep->base.dead = 1;
  146. ep->base.sk->sk_state = SCTP_SS_CLOSED;
  147. /* Unlink this endpoint, so we can't find it again! */
  148. sctp_unhash_endpoint(ep);
  149. sctp_endpoint_put(ep);
  150. }
  151. /* Final destructor for endpoint. */
  152. static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
  153. {
  154. SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
  155. /* Free up the HMAC transform. */
  156. crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
  157. /* Free the digest buffer */
  158. kfree(ep->digest);
  159. /* Cleanup. */
  160. sctp_inq_free(&ep->base.inqueue);
  161. sctp_bind_addr_free(&ep->base.bind_addr);
  162. /* Remove and free the port */
  163. if (sctp_sk(ep->base.sk)->bind_hash)
  164. sctp_put_port(ep->base.sk);
  165. /* Give up our hold on the sock. */
  166. if (ep->base.sk)
  167. sock_put(ep->base.sk);
  168. /* Finally, free up our memory. */
  169. if (ep->base.malloced) {
  170. kfree(ep);
  171. SCTP_DBG_OBJCNT_DEC(ep);
  172. }
  173. }
  174. /* Hold a reference to an endpoint. */
  175. void sctp_endpoint_hold(struct sctp_endpoint *ep)
  176. {
  177. atomic_inc(&ep->base.refcnt);
  178. }
  179. /* Release a reference to an endpoint and clean up if there are
  180. * no more references.
  181. */
  182. void sctp_endpoint_put(struct sctp_endpoint *ep)
  183. {
  184. if (atomic_dec_and_test(&ep->base.refcnt))
  185. sctp_endpoint_destroy(ep);
  186. }
  187. /* Is this the endpoint we are looking for? */
  188. struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
  189. const union sctp_addr *laddr)
  190. {
  191. struct sctp_endpoint *retval = NULL;
  192. if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
  193. if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
  194. sctp_sk(ep->base.sk)))
  195. retval = ep;
  196. }
  197. return retval;
  198. }
  199. /* Find the association that goes with this chunk.
  200. * We do a linear search of the associations for this endpoint.
  201. * We return the matching transport address too.
  202. */
  203. static struct sctp_association *__sctp_endpoint_lookup_assoc(
  204. const struct sctp_endpoint *ep,
  205. const union sctp_addr *paddr,
  206. struct sctp_transport **transport)
  207. {
  208. int rport;
  209. struct sctp_association *asoc;
  210. struct list_head *pos;
  211. rport = ntohs(paddr->v4.sin_port);
  212. list_for_each(pos, &ep->asocs) {
  213. asoc = list_entry(pos, struct sctp_association, asocs);
  214. if (rport == asoc->peer.port) {
  215. *transport = sctp_assoc_lookup_paddr(asoc, paddr);
  216. if (*transport)
  217. return asoc;
  218. }
  219. }
  220. *transport = NULL;
  221. return NULL;
  222. }
  223. /* Lookup association on an endpoint based on a peer address. BH-safe. */
  224. struct sctp_association *sctp_endpoint_lookup_assoc(
  225. const struct sctp_endpoint *ep,
  226. const union sctp_addr *paddr,
  227. struct sctp_transport **transport)
  228. {
  229. struct sctp_association *asoc;
  230. sctp_local_bh_disable();
  231. asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
  232. sctp_local_bh_enable();
  233. return asoc;
  234. }
  235. /* Look for any peeled off association from the endpoint that matches the
  236. * given peer address.
  237. */
  238. int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
  239. const union sctp_addr *paddr)
  240. {
  241. struct sctp_sockaddr_entry *addr;
  242. struct sctp_bind_addr *bp;
  243. bp = &ep->base.bind_addr;
  244. /* This function is called with the socket lock held,
  245. * so the address_list can not change.
  246. */
  247. list_for_each_entry(addr, &bp->address_list, list) {
  248. if (sctp_has_association(&addr->a, paddr))
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. /* Do delayed input processing. This is scheduled by sctp_rcv().
  254. * This may be called on BH or task time.
  255. */
  256. static void sctp_endpoint_bh_rcv(struct work_struct *work)
  257. {
  258. struct sctp_endpoint *ep =
  259. container_of(work, struct sctp_endpoint,
  260. base.inqueue.immediate);
  261. struct sctp_association *asoc;
  262. struct sock *sk;
  263. struct sctp_transport *transport;
  264. struct sctp_chunk *chunk;
  265. struct sctp_inq *inqueue;
  266. sctp_subtype_t subtype;
  267. sctp_state_t state;
  268. int error = 0;
  269. if (ep->base.dead)
  270. return;
  271. asoc = NULL;
  272. inqueue = &ep->base.inqueue;
  273. sk = ep->base.sk;
  274. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  275. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  276. /* We might have grown an association since last we
  277. * looked, so try again.
  278. *
  279. * This happens when we've just processed our
  280. * COOKIE-ECHO chunk.
  281. */
  282. if (NULL == chunk->asoc) {
  283. asoc = sctp_endpoint_lookup_assoc(ep,
  284. sctp_source(chunk),
  285. &transport);
  286. chunk->asoc = asoc;
  287. chunk->transport = transport;
  288. }
  289. state = asoc ? asoc->state : SCTP_STATE_CLOSED;
  290. /* Remember where the last DATA chunk came from so we
  291. * know where to send the SACK.
  292. */
  293. if (asoc && sctp_chunk_is_data(chunk))
  294. asoc->peer.last_data_from = chunk->transport;
  295. else
  296. SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
  297. if (chunk->transport)
  298. chunk->transport->last_time_heard = jiffies;
  299. error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
  300. ep, asoc, chunk, GFP_ATOMIC);
  301. if (error && chunk)
  302. chunk->pdiscard = 1;
  303. /* Check to see if the endpoint is freed in response to
  304. * the incoming chunk. If so, get out of the while loop.
  305. */
  306. if (!sctp_sk(sk)->ep)
  307. break;
  308. }
  309. }