endpointola.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. struct sctp_hmac_algo_param *auth_hmacs = NULL;
  70. struct sctp_chunks_param *auth_chunks = NULL;
  71. struct sctp_shared_key *null_key;
  72. int err;
  73. memset(ep, 0, sizeof(struct sctp_endpoint));
  74. ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
  75. if (!ep->digest)
  76. return NULL;
  77. if (sctp_auth_enable) {
  78. /* Allocate space for HMACS and CHUNKS authentication
  79. * variables. There are arrays that we encode directly
  80. * into parameters to make the rest of the operations easier.
  81. */
  82. auth_hmacs = kzalloc(sizeof(sctp_hmac_algo_param_t) +
  83. sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp);
  84. if (!auth_hmacs)
  85. goto nomem;
  86. auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) +
  87. SCTP_NUM_CHUNK_TYPES, gfp);
  88. if (!auth_chunks)
  89. goto nomem;
  90. /* Initialize the HMACS parameter.
  91. * SCTP-AUTH: Section 3.3
  92. * Every endpoint supporting SCTP chunk authentication MUST
  93. * support the HMAC based on the SHA-1 algorithm.
  94. */
  95. auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
  96. auth_hmacs->param_hdr.length =
  97. htons(sizeof(sctp_paramhdr_t) + 2);
  98. auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
  99. /* Initialize the CHUNKS parameter */
  100. auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
  101. /* If the Add-IP functionality is enabled, we must
  102. * authenticate, ASCONF and ASCONF-ACK chunks
  103. */
  104. if (sctp_addip_enable) {
  105. auth_chunks->chunks[0] = SCTP_CID_ASCONF;
  106. auth_chunks->chunks[1] = SCTP_CID_ASCONF_ACK;
  107. auth_chunks->param_hdr.length =
  108. htons(sizeof(sctp_paramhdr_t) + 2);
  109. }
  110. }
  111. /* Initialize the base structure. */
  112. /* What type of endpoint are we? */
  113. ep->base.type = SCTP_EP_TYPE_SOCKET;
  114. /* Initialize the basic object fields. */
  115. atomic_set(&ep->base.refcnt, 1);
  116. ep->base.dead = 0;
  117. ep->base.malloced = 1;
  118. /* Create an input queue. */
  119. sctp_inq_init(&ep->base.inqueue);
  120. /* Set its top-half handler */
  121. sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
  122. /* Initialize the bind addr area */
  123. sctp_bind_addr_init(&ep->base.bind_addr, 0);
  124. /* Remember who we are attached to. */
  125. ep->base.sk = sk;
  126. sock_hold(ep->base.sk);
  127. /* Create the lists of associations. */
  128. INIT_LIST_HEAD(&ep->asocs);
  129. /* Use SCTP specific send buffer space queues. */
  130. ep->sndbuf_policy = sctp_sndbuf_policy;
  131. sk->sk_write_space = sctp_write_space;
  132. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  133. /* Get the receive buffer policy for this endpoint */
  134. ep->rcvbuf_policy = sctp_rcvbuf_policy;
  135. /* Initialize the secret key used with cookie. */
  136. get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
  137. ep->last_key = ep->current_key = 0;
  138. ep->key_changed_at = jiffies;
  139. /* SCTP-AUTH extensions*/
  140. INIT_LIST_HEAD(&ep->endpoint_shared_keys);
  141. null_key = sctp_auth_shkey_create(0, GFP_KERNEL);
  142. if (!null_key)
  143. goto nomem;
  144. list_add(&null_key->key_list, &ep->endpoint_shared_keys);
  145. /* Allocate and initialize transorms arrays for suported HMACs. */
  146. err = sctp_auth_init_hmacs(ep, gfp);
  147. if (err)
  148. goto nomem_hmacs;
  149. /* Add the null key to the endpoint shared keys list and
  150. * set the hmcas and chunks pointers.
  151. */
  152. ep->auth_hmacs_list = auth_hmacs;
  153. ep->auth_chunk_list = auth_chunks;
  154. return ep;
  155. nomem_hmacs:
  156. sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
  157. nomem:
  158. /* Free all allocations */
  159. kfree(auth_hmacs);
  160. kfree(auth_chunks);
  161. kfree(ep->digest);
  162. return NULL;
  163. }
  164. /* Create a sctp_endpoint with all that boring stuff initialized.
  165. * Returns NULL if there isn't enough memory.
  166. */
  167. struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
  168. {
  169. struct sctp_endpoint *ep;
  170. /* Build a local endpoint. */
  171. ep = t_new(struct sctp_endpoint, gfp);
  172. if (!ep)
  173. goto fail;
  174. if (!sctp_endpoint_init(ep, sk, gfp))
  175. goto fail_init;
  176. ep->base.malloced = 1;
  177. SCTP_DBG_OBJCNT_INC(ep);
  178. return ep;
  179. fail_init:
  180. kfree(ep);
  181. fail:
  182. return NULL;
  183. }
  184. /* Add an association to an endpoint. */
  185. void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
  186. struct sctp_association *asoc)
  187. {
  188. struct sock *sk = ep->base.sk;
  189. /* If this is a temporary association, don't bother
  190. * since we'll be removing it shortly and don't
  191. * want anyone to find it anyway.
  192. */
  193. if (asoc->temp)
  194. return;
  195. /* Now just add it to our list of asocs */
  196. list_add_tail(&asoc->asocs, &ep->asocs);
  197. /* Increment the backlog value for a TCP-style listening socket. */
  198. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  199. sk->sk_ack_backlog++;
  200. }
  201. /* Free the endpoint structure. Delay cleanup until
  202. * all users have released their reference count on this structure.
  203. */
  204. void sctp_endpoint_free(struct sctp_endpoint *ep)
  205. {
  206. ep->base.dead = 1;
  207. ep->base.sk->sk_state = SCTP_SS_CLOSED;
  208. /* Unlink this endpoint, so we can't find it again! */
  209. sctp_unhash_endpoint(ep);
  210. sctp_endpoint_put(ep);
  211. }
  212. /* Final destructor for endpoint. */
  213. static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
  214. {
  215. SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
  216. /* Free up the HMAC transform. */
  217. crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
  218. /* Free the digest buffer */
  219. kfree(ep->digest);
  220. /* SCTP-AUTH: Free up AUTH releated data such as shared keys
  221. * chunks and hmacs arrays that were allocated
  222. */
  223. sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
  224. kfree(ep->auth_hmacs_list);
  225. kfree(ep->auth_chunk_list);
  226. /* AUTH - Free any allocated HMAC transform containers */
  227. sctp_auth_destroy_hmacs(ep->auth_hmacs);
  228. /* Cleanup. */
  229. sctp_inq_free(&ep->base.inqueue);
  230. sctp_bind_addr_free(&ep->base.bind_addr);
  231. /* Remove and free the port */
  232. if (sctp_sk(ep->base.sk)->bind_hash)
  233. sctp_put_port(ep->base.sk);
  234. /* Give up our hold on the sock. */
  235. if (ep->base.sk)
  236. sock_put(ep->base.sk);
  237. /* Finally, free up our memory. */
  238. if (ep->base.malloced) {
  239. kfree(ep);
  240. SCTP_DBG_OBJCNT_DEC(ep);
  241. }
  242. }
  243. /* Hold a reference to an endpoint. */
  244. void sctp_endpoint_hold(struct sctp_endpoint *ep)
  245. {
  246. atomic_inc(&ep->base.refcnt);
  247. }
  248. /* Release a reference to an endpoint and clean up if there are
  249. * no more references.
  250. */
  251. void sctp_endpoint_put(struct sctp_endpoint *ep)
  252. {
  253. if (atomic_dec_and_test(&ep->base.refcnt))
  254. sctp_endpoint_destroy(ep);
  255. }
  256. /* Is this the endpoint we are looking for? */
  257. struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
  258. const union sctp_addr *laddr)
  259. {
  260. struct sctp_endpoint *retval = NULL;
  261. if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
  262. if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
  263. sctp_sk(ep->base.sk)))
  264. retval = ep;
  265. }
  266. return retval;
  267. }
  268. /* Find the association that goes with this chunk.
  269. * We do a linear search of the associations for this endpoint.
  270. * We return the matching transport address too.
  271. */
  272. static struct sctp_association *__sctp_endpoint_lookup_assoc(
  273. const struct sctp_endpoint *ep,
  274. const union sctp_addr *paddr,
  275. struct sctp_transport **transport)
  276. {
  277. int rport;
  278. struct sctp_association *asoc;
  279. struct list_head *pos;
  280. rport = ntohs(paddr->v4.sin_port);
  281. list_for_each(pos, &ep->asocs) {
  282. asoc = list_entry(pos, struct sctp_association, asocs);
  283. if (rport == asoc->peer.port) {
  284. *transport = sctp_assoc_lookup_paddr(asoc, paddr);
  285. if (*transport)
  286. return asoc;
  287. }
  288. }
  289. *transport = NULL;
  290. return NULL;
  291. }
  292. /* Lookup association on an endpoint based on a peer address. BH-safe. */
  293. struct sctp_association *sctp_endpoint_lookup_assoc(
  294. const struct sctp_endpoint *ep,
  295. const union sctp_addr *paddr,
  296. struct sctp_transport **transport)
  297. {
  298. struct sctp_association *asoc;
  299. sctp_local_bh_disable();
  300. asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
  301. sctp_local_bh_enable();
  302. return asoc;
  303. }
  304. /* Look for any peeled off association from the endpoint that matches the
  305. * given peer address.
  306. */
  307. int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
  308. const union sctp_addr *paddr)
  309. {
  310. struct sctp_sockaddr_entry *addr;
  311. struct sctp_bind_addr *bp;
  312. bp = &ep->base.bind_addr;
  313. /* This function is called with the socket lock held,
  314. * so the address_list can not change.
  315. */
  316. list_for_each_entry(addr, &bp->address_list, list) {
  317. if (sctp_has_association(&addr->a, paddr))
  318. return 1;
  319. }
  320. return 0;
  321. }
  322. /* Do delayed input processing. This is scheduled by sctp_rcv().
  323. * This may be called on BH or task time.
  324. */
  325. static void sctp_endpoint_bh_rcv(struct work_struct *work)
  326. {
  327. struct sctp_endpoint *ep =
  328. container_of(work, struct sctp_endpoint,
  329. base.inqueue.immediate);
  330. struct sctp_association *asoc;
  331. struct sock *sk;
  332. struct sctp_transport *transport;
  333. struct sctp_chunk *chunk;
  334. struct sctp_inq *inqueue;
  335. sctp_subtype_t subtype;
  336. sctp_state_t state;
  337. int error = 0;
  338. int first_time = 1; /* is this the first time through the looop */
  339. if (ep->base.dead)
  340. return;
  341. asoc = NULL;
  342. inqueue = &ep->base.inqueue;
  343. sk = ep->base.sk;
  344. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  345. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  346. /* If the first chunk in the packet is AUTH, do special
  347. * processing specified in Section 6.3 of SCTP-AUTH spec
  348. */
  349. if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
  350. struct sctp_chunkhdr *next_hdr;
  351. next_hdr = sctp_inq_peek(inqueue);
  352. if (!next_hdr)
  353. goto normal;
  354. /* If the next chunk is COOKIE-ECHO, skip the AUTH
  355. * chunk while saving a pointer to it so we can do
  356. * Authentication later (during cookie-echo
  357. * processing).
  358. */
  359. if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
  360. chunk->auth_chunk = skb_clone(chunk->skb,
  361. GFP_ATOMIC);
  362. chunk->auth = 1;
  363. continue;
  364. }
  365. }
  366. normal:
  367. /* We might have grown an association since last we
  368. * looked, so try again.
  369. *
  370. * This happens when we've just processed our
  371. * COOKIE-ECHO chunk.
  372. */
  373. if (NULL == chunk->asoc) {
  374. asoc = sctp_endpoint_lookup_assoc(ep,
  375. sctp_source(chunk),
  376. &transport);
  377. chunk->asoc = asoc;
  378. chunk->transport = transport;
  379. }
  380. state = asoc ? asoc->state : SCTP_STATE_CLOSED;
  381. if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
  382. continue;
  383. /* Remember where the last DATA chunk came from so we
  384. * know where to send the SACK.
  385. */
  386. if (asoc && sctp_chunk_is_data(chunk))
  387. asoc->peer.last_data_from = chunk->transport;
  388. else
  389. SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
  390. if (chunk->transport)
  391. chunk->transport->last_time_heard = jiffies;
  392. error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
  393. ep, asoc, chunk, GFP_ATOMIC);
  394. if (error && chunk)
  395. chunk->pdiscard = 1;
  396. /* Check to see if the endpoint is freed in response to
  397. * the incoming chunk. If so, get out of the while loop.
  398. */
  399. if (!sctp_sk(sk)->ep)
  400. break;
  401. if (first_time)
  402. first_time = 0;
  403. }
  404. }