endpointola.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. int dif, int sdif)
  202. {
  203. int bound_dev_if = READ_ONCE(ep->base.sk->sk_bound_dev_if);
  204. struct sctp_endpoint *retval = NULL;
  205. if (net_eq(ep->base.net, net) &&
  206. sctp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif) &&
  207. (htons(ep->base.bind_addr.port) == laddr->v4.sin_port)) {
  208. if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
  209. sctp_sk(ep->base.sk)))
  210. retval = ep;
  211. }
  212. return retval;
  213. }
  214. /* Find the association that goes with this chunk.
  215. * We lookup the transport from hashtable at first, then get association
  216. * through t->assoc.
  217. */
  218. struct sctp_association *sctp_endpoint_lookup_assoc(
  219. const struct sctp_endpoint *ep,
  220. const union sctp_addr *paddr,
  221. struct sctp_transport **transport)
  222. {
  223. struct sctp_association *asoc = NULL;
  224. struct sctp_transport *t;
  225. *transport = NULL;
  226. /* If the local port is not set, there can't be any associations
  227. * on this endpoint.
  228. */
  229. if (!ep->base.bind_addr.port)
  230. return NULL;
  231. rcu_read_lock();
  232. t = sctp_epaddr_lookup_transport(ep, paddr);
  233. if (!t)
  234. goto out;
  235. *transport = t;
  236. asoc = t->asoc;
  237. out:
  238. rcu_read_unlock();
  239. return asoc;
  240. }
  241. /* Look for any peeled off association from the endpoint that matches the
  242. * given peer address.
  243. */
  244. bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
  245. const union sctp_addr *paddr)
  246. {
  247. int bound_dev_if = READ_ONCE(ep->base.sk->sk_bound_dev_if);
  248. struct sctp_sockaddr_entry *addr;
  249. struct net *net = ep->base.net;
  250. struct sctp_bind_addr *bp;
  251. bp = &ep->base.bind_addr;
  252. /* This function is called with the socket lock held,
  253. * so the address_list can not change.
  254. */
  255. list_for_each_entry(addr, &bp->address_list, list) {
  256. if (sctp_has_association(net, &addr->a, paddr,
  257. bound_dev_if, bound_dev_if))
  258. return true;
  259. }
  260. return false;
  261. }
  262. /* Do delayed input processing. This is scheduled by sctp_rcv().
  263. * This may be called on BH or task time.
  264. */
  265. static void sctp_endpoint_bh_rcv(struct work_struct *work)
  266. {
  267. struct sctp_endpoint *ep =
  268. container_of(work, struct sctp_endpoint,
  269. base.inqueue.immediate);
  270. struct sctp_association *asoc;
  271. struct sock *sk;
  272. struct net *net;
  273. struct sctp_transport *transport;
  274. struct sctp_chunk *chunk;
  275. struct sctp_inq *inqueue;
  276. union sctp_subtype subtype;
  277. enum sctp_state state;
  278. int error = 0;
  279. int first_time = 1; /* is this the first time through the loop */
  280. if (ep->base.dead)
  281. return;
  282. asoc = NULL;
  283. inqueue = &ep->base.inqueue;
  284. sk = ep->base.sk;
  285. net = sock_net(sk);
  286. while (NULL != (chunk = sctp_inq_pop(inqueue))) {
  287. subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
  288. /* If the first chunk in the packet is AUTH, do special
  289. * processing specified in Section 6.3 of SCTP-AUTH spec
  290. */
  291. if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
  292. struct sctp_chunkhdr *next_hdr;
  293. next_hdr = sctp_inq_peek(inqueue);
  294. if (!next_hdr)
  295. goto normal;
  296. /* If the next chunk is COOKIE-ECHO, skip the AUTH
  297. * chunk while saving a pointer to it so we can do
  298. * Authentication later (during cookie-echo
  299. * processing).
  300. */
  301. if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
  302. chunk->auth_chunk = skb_clone(chunk->skb,
  303. GFP_ATOMIC);
  304. chunk->auth = 1;
  305. continue;
  306. }
  307. }
  308. normal:
  309. /* We might have grown an association since last we
  310. * looked, so try again.
  311. *
  312. * This happens when we've just processed our
  313. * COOKIE-ECHO chunk.
  314. */
  315. if (NULL == chunk->asoc) {
  316. asoc = sctp_endpoint_lookup_assoc(ep,
  317. sctp_source(chunk),
  318. &transport);
  319. chunk->asoc = asoc;
  320. chunk->transport = transport;
  321. }
  322. state = asoc ? asoc->state : SCTP_STATE_CLOSED;
  323. if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
  324. continue;
  325. /* Remember where the last DATA chunk came from so we
  326. * know where to send the SACK.
  327. */
  328. if (asoc && sctp_chunk_is_data(chunk))
  329. asoc->peer.last_data_from = chunk->transport;
  330. else {
  331. SCTP_INC_STATS(ep->base.net, SCTP_MIB_INCTRLCHUNKS);
  332. if (asoc)
  333. asoc->stats.ictrlchunks++;
  334. }
  335. if (chunk->transport)
  336. chunk->transport->last_time_heard = ktime_get();
  337. error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state,
  338. ep, asoc, chunk, GFP_ATOMIC);
  339. if (error && chunk)
  340. chunk->pdiscard = 1;
  341. /* Check to see if the endpoint is freed in response to
  342. * the incoming chunk. If so, get out of the while loop.
  343. */
  344. if (!sctp_sk(sk)->ep)
  345. break;
  346. if (first_time)
  347. first_time = 0;
  348. }
  349. }