endpointola.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* SCTP kernel implementation
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2002 International Business Machines, Corp.
  6. * Copyright (c) 2001 Intel Corp.
  7. * Copyright (c) 2001 Nokia, Inc.
  8. * Copyright (c) 2001 La Monte H.P. Yarroll
  9. *
  10. * This file is part of the SCTP kernel implementation
  11. *
  12. * This abstraction represents an SCTP endpoint.
  13. *
  14. * Please send any bug reports or fixes you make to the
  15. * email address(es):
  16. * lksctp developers <linux-sctp@vger.kernel.org>
  17. *
  18. * Written or modified by:
  19. * La Monte H.P. Yarroll <piggy@acm.org>
  20. * Karl Knutson <karl@athena.chicago.il.us>
  21. * Jon Grimm <jgrimm@austin.ibm.com>
  22. * Daisy Chang <daisyc@us.ibm.com>
  23. * Dajiang Zhang <dajiang.zhang@nokia.com>
  24. */
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/in.h>
  28. #include <linux/random.h> /* get_random_bytes() */
  29. #include <net/sock.h>
  30. #include <net/ipv6.h>
  31. #include <net/sctp/sctp.h>
  32. #include <net/sctp/sm.h>
  33. /* Forward declarations for internal helpers. */
  34. static void sctp_endpoint_bh_rcv(struct work_struct *work);
  35. /*
  36. * Initialize the base fields of the endpoint structure.
  37. */
  38. static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
  39. struct sock *sk,
  40. gfp_t gfp)
  41. {
  42. struct net *net = sock_net(sk);
  43. struct sctp_shared_key *null_key;
  44. ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
  45. if (!ep->digest)
  46. return NULL;
  47. ep->asconf_enable = net->sctp.addip_enable;
  48. ep->auth_enable = net->sctp.auth_enable;
  49. if (ep->auth_enable) {
  50. if (sctp_auth_init(ep, gfp))
  51. goto nomem;
  52. if (ep->asconf_enable) {
  53. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
  54. sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
  55. }
  56. }
  57. /* Initialize the base structure. */
  58. /* What type of endpoint are we? */
  59. ep->base.type = SCTP_EP_TYPE_SOCKET;
  60. /* Initialize the basic object fields. */
  61. refcount_set(&ep->base.refcnt, 1);
  62. ep->base.dead = false;
  63. /* Create an input queue. */
  64. sctp_inq_init(&ep->base.inqueue);
  65. /* Set its top-half handler */
  66. sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
  67. /* Initialize the bind addr area */
  68. sctp_bind_addr_init(&ep->base.bind_addr, 0);
  69. /* Create the lists of associations. */
  70. INIT_LIST_HEAD(&ep->asocs);
  71. /* Use SCTP specific send buffer space queues. */
  72. ep->sndbuf_policy = net->sctp.sndbuf_policy;
  73. sk->sk_data_ready = sctp_data_ready;
  74. sk->sk_write_space = sctp_write_space;
  75. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  76. /* Get the receive buffer policy for this endpoint */
  77. ep->rcvbuf_policy = net->sctp.rcvbuf_policy;
  78. /* Initialize the secret key used with cookie. */
  79. get_random_bytes(ep->secret_key, sizeof(ep->secret_key));
  80. /* SCTP-AUTH extensions*/
  81. INIT_LIST_HEAD(&ep->endpoint_shared_keys);
  82. null_key = sctp_auth_shkey_create(0, gfp);
  83. if (!null_key)
  84. goto nomem_shkey;
  85. list_add(&null_key->key_list, &ep->endpoint_shared_keys);
  86. /* Add the null key to the endpoint shared keys list and
  87. * set the hmcas and chunks pointers.
  88. */
  89. ep->prsctp_enable = net->sctp.prsctp_enable;
  90. ep->reconf_enable = net->sctp.reconf_enable;
  91. ep->ecn_enable = net->sctp.ecn_enable;
  92. /* Remember who we are attached to. */
  93. ep->base.sk = sk;
  94. ep->base.net = sock_net(sk);
  95. sock_hold(ep->base.sk);
  96. return ep;
  97. nomem_shkey:
  98. sctp_auth_free(ep);
  99. nomem:
  100. kfree(ep->digest);
  101. return NULL;
  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 = kzalloc(sizeof(*ep), gfp);
  111. if (!ep)
  112. goto fail;
  113. if (!sctp_endpoint_init(ep, sk, gfp))
  114. goto fail_init;
  115. SCTP_DBG_OBJCNT_INC(ep);
  116. return ep;
  117. fail_init:
  118. kfree(ep);
  119. fail:
  120. return NULL;
  121. }
  122. /* Add an association to an endpoint. */
  123. void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
  124. struct sctp_association *asoc)
  125. {
  126. struct sock *sk = ep->base.sk;
  127. /* If this is a temporary association, don't bother
  128. * since we'll be removing it shortly and don't
  129. * want anyone to find it anyway.
  130. */
  131. if (asoc->temp)
  132. return;
  133. /* Now just add it to our list of asocs */
  134. list_add_tail(&asoc->asocs, &ep->asocs);
  135. /* Increment the backlog value for a TCP-style listening socket. */
  136. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  137. sk_acceptq_added(sk);
  138. }
  139. /* Free the endpoint structure. Delay cleanup until
  140. * all users have released their reference count on this structure.
  141. */
  142. void sctp_endpoint_free(struct sctp_endpoint *ep)
  143. {
  144. ep->base.dead = true;
  145. inet_sk_set_state(ep->base.sk, SCTP_SS_CLOSED);
  146. /* Unlink this endpoint, so we can't find it again! */
  147. sctp_unhash_endpoint(ep);
  148. sctp_endpoint_put(ep);
  149. }
  150. /* Final destructor for endpoint. */
  151. static void sctp_endpoint_destroy_rcu(struct rcu_head *head)
  152. {
  153. struct sctp_endpoint *ep = container_of(head, struct sctp_endpoint, rcu);
  154. struct sock *sk = ep->base.sk;
  155. sctp_sk(sk)->ep = NULL;
  156. sock_put(sk);
  157. kfree(ep);
  158. SCTP_DBG_OBJCNT_DEC(ep);
  159. }
  160. static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
  161. {
  162. struct sock *sk;
  163. if (unlikely(!ep->base.dead)) {
  164. WARN(1, "Attempt to destroy undead endpoint %p!\n", ep);
  165. return;
  166. }
  167. /* Free the digest buffer */
  168. kfree(ep->digest);
  169. /* SCTP-AUTH: Free up AUTH releated data such as shared keys
  170. * chunks and hmacs arrays that were allocated
  171. */
  172. sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
  173. sctp_auth_free(ep);
  174. /* Cleanup. */
  175. sctp_inq_free(&ep->base.inqueue);
  176. sctp_bind_addr_free(&ep->base.bind_addr);
  177. memset(ep->secret_key, 0, sizeof(ep->secret_key));
  178. sk = ep->base.sk;
  179. /* Remove and free the port */
  180. if (sctp_sk(sk)->bind_hash)
  181. sctp_put_port(sk);
  182. call_rcu(&ep->rcu, sctp_endpoint_destroy_rcu);
  183. }
  184. /* Hold a reference to an endpoint. */
  185. int sctp_endpoint_hold(struct sctp_endpoint *ep)
  186. {
  187. return refcount_inc_not_zero(&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 (refcount_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. struct net *net,
  200. const union sctp_addr *laddr)
  201. {
  202. struct sctp_endpoint *retval = NULL;
  203. if ((htons(ep->base.bind_addr.port) == laddr->v4.sin_port) &&
  204. net_eq(ep->base.net, net)) {
  205. if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
  206. sctp_sk(ep->base.sk)))
  207. retval = ep;
  208. }
  209. return retval;
  210. }
  211. /* Find the association that goes with this chunk.
  212. * We lookup the transport from hashtable at first, then get association
  213. * through t->assoc.
  214. */
  215. struct sctp_association *sctp_endpoint_lookup_assoc(
  216. const struct sctp_endpoint *ep,
  217. const union sctp_addr *paddr,
  218. struct sctp_transport **transport)
  219. {
  220. struct sctp_association *asoc = NULL;
  221. struct sctp_transport *t;
  222. *transport = NULL;
  223. /* If the local port is not set, there can't be any associations
  224. * on this endpoint.
  225. */
  226. if (!ep->base.bind_addr.port)
  227. return NULL;
  228. rcu_read_lock();
  229. t = sctp_epaddr_lookup_transport(ep, paddr);
  230. if (!t)
  231. goto out;
  232. *transport = t;
  233. asoc = t->asoc;
  234. out:
  235. rcu_read_unlock();
  236. return asoc;
  237. }
  238. /* Look for any peeled off association from the endpoint that matches the
  239. * given peer address.
  240. */
  241. bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
  242. const union sctp_addr *paddr)
  243. {
  244. struct sctp_sockaddr_entry *addr;
  245. struct net *net = ep->base.net;
  246. struct sctp_bind_addr *bp;
  247. bp = &ep->base.bind_addr;
  248. /* This function is called with the socket lock held,
  249. * so the address_list can not change.
  250. */
  251. list_for_each_entry(addr, &bp->address_list, list) {
  252. if (sctp_has_association(net, &addr->a, paddr))
  253. return true;
  254. }
  255. return false;
  256. }
  257. /* Do delayed input processing. This is scheduled by sctp_rcv().
  258. * This may be called on BH or task time.
  259. */
  260. static void sctp_endpoint_bh_rcv(struct work_struct *work)
  261. {
  262. struct sctp_endpoint *ep =
  263. container_of(work, struct sctp_endpoint,
  264. base.inqueue.immediate);
  265. struct sctp_association *asoc;
  266. struct sock *sk;
  267. struct net *net;
  268. struct sctp_transport *transport;
  269. struct sctp_chunk *chunk;
  270. struct sctp_inq *inqueue;
  271. union sctp_subtype subtype;
  272. enum sctp_state state;
  273. int error = 0;
  274. int first_time = 1; /* is this the first time through the loop */
  275. if (ep->base.dead)
  276. return;
  277. asoc = NULL;
  278. inqueue = &ep->base.inqueue;
  279. sk = ep->base.sk;
  280. net = sock_net(sk);
  281. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  282. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  283. /* If the first chunk in the packet is AUTH, do special
  284. * processing specified in Section 6.3 of SCTP-AUTH spec
  285. */
  286. if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
  287. struct sctp_chunkhdr *next_hdr;
  288. next_hdr = sctp_inq_peek(inqueue);
  289. if (!next_hdr)
  290. goto normal;
  291. /* If the next chunk is COOKIE-ECHO, skip the AUTH
  292. * chunk while saving a pointer to it so we can do
  293. * Authentication later (during cookie-echo
  294. * processing).
  295. */
  296. if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
  297. chunk->auth_chunk = skb_clone(chunk->skb,
  298. GFP_ATOMIC);
  299. chunk->auth = 1;
  300. continue;
  301. }
  302. }
  303. normal:
  304. /* We might have grown an association since last we
  305. * looked, so try again.
  306. *
  307. * This happens when we've just processed our
  308. * COOKIE-ECHO chunk.
  309. */
  310. if (NULL == chunk->asoc) {
  311. asoc = sctp_endpoint_lookup_assoc(ep,
  312. sctp_source(chunk),
  313. &transport);
  314. chunk->asoc = asoc;
  315. chunk->transport = transport;
  316. }
  317. state = asoc ? asoc->state : SCTP_STATE_CLOSED;
  318. if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
  319. continue;
  320. /* Remember where the last DATA chunk came from so we
  321. * know where to send the SACK.
  322. */
  323. if (asoc && sctp_chunk_is_data(chunk))
  324. asoc->peer.last_data_from = chunk->transport;
  325. else {
  326. SCTP_INC_STATS(ep->base.net, SCTP_MIB_INCTRLCHUNKS);
  327. if (asoc)
  328. asoc->stats.ictrlchunks++;
  329. }
  330. if (chunk->transport)
  331. chunk->transport->last_time_heard = ktime_get();
  332. error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state,
  333. ep, asoc, chunk, GFP_ATOMIC);
  334. if (error && chunk)
  335. chunk->pdiscard = 1;
  336. /* Check to see if the endpoint is freed in response to
  337. * the incoming chunk. If so, get out of the while loop.
  338. */
  339. if (!sctp_sk(sk)->ep)
  340. break;
  341. if (first_time)
  342. first_time = 0;
  343. }
  344. }