endpointola.c 10 KB

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