PageRenderTime 39ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/linux-source-2.6.32/net/sctp-sagun/socket.c~

https://bitbucket.org/ChuloChumo/sctp_thesis
Unknown | 5845 lines | 5072 code | 773 blank | 0 comment | 0 complexity | f52196fd90b3a4d8b60c7eb676a3d770 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001-2003 Intel Corp.
  6. * Copyright (c) 2001-2002 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. * These functions interface with the sockets layer to implement the
  12. * SCTP Extensions for the Sockets API.
  13. *
  14. * Note that the descriptions from the specification are USER level
  15. * functions--this file is the functions which populate the struct proto
  16. * for SCTP which is the BOTTOM of the sockets interface.
  17. *
  18. * The SCTP reference implementation is free software;
  19. * you can redistribute it and/or modify it under the terms of
  20. * the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * The SCTP reference implementation is distributed in the hope that it
  25. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  26. * ************************
  27. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  28. * See the GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with GNU CC; see the file COPYING. If not, write to
  32. * the Free Software Foundation, 59 Temple Place - Suite 330,
  33. * Boston, MA 02111-1307, USA.
  34. *
  35. * Please send any bug reports or fixes you make to the
  36. * email address(es):
  37. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  38. *
  39. * Or submit a bug report through the following website:
  40. * http://www.sf.net/projects/lksctp
  41. *
  42. * Written or modified by:
  43. * La Monte H.P. Yarroll <piggy@acm.org>
  44. * Narasimha Budihal <narsi@refcode.org>
  45. * Karl Knutson <karl@athena.chicago.il.us>
  46. * Jon Grimm <jgrimm@us.ibm.com>
  47. * Xingang Guo <xingang.guo@intel.com>
  48. * Daisy Chang <daisyc@us.ibm.com>
  49. * Sridhar Samudrala <samudrala@us.ibm.com>
  50. * Inaky Perez-Gonzalez <inaky.gonzalez@intel.com>
  51. * Ardelle Fan <ardelle.fan@intel.com>
  52. * Ryan Layer <rmlayer@us.ibm.com>
  53. * Anup Pemmaiah <pemmaiah@cc.usu.edu>
  54. * Kevin Gao <kevin.gao@intel.com>
  55. *
  56. * Any bugs reported given to us we will try to fix... any fixes shared will
  57. * be incorporated into the next SCTP release.
  58. */
  59. #include <linux/types.h>
  60. #include <linux/kernel.h>
  61. #include <linux/wait.h>
  62. #include <linux/time.h>
  63. #include <linux/ip.h>
  64. #include <linux/capability.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/poll.h>
  67. #include <linux/init.h>
  68. #include <linux/crypto.h>
  69. #include <net/ip.h>
  70. #include <net/icmp.h>
  71. #include <net/route.h>
  72. #include <net/ipv6.h>
  73. #include <net/inet_common.h>
  74. #include <linux/socket.h> /* for sa_family_t */
  75. #include <net/sock.h>
  76. #include <net/sctp/sctp.h>
  77. #include <net/sctp/sm.h>
  78. /* WARNING: Please do not remove the SCTP_STATIC attribute to
  79. * any of the functions below as they are used to export functions
  80. * used by a project regression testsuite.
  81. */
  82. /* Forward declarations for internal helper functions. */
  83. static int sctp_writeable(struct sock *sk);
  84. static void sctp_wfree(struct sk_buff *skb);
  85. static int sctp_wait_for_sndbuf(struct sctp_association *, long *timeo_p,
  86. size_t msg_len);
  87. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p);
  88. static int sctp_wait_for_connect(struct sctp_association *, long *timeo_p);
  89. static int sctp_wait_for_accept(struct sock *sk, long timeo);
  90. static void sctp_wait_for_close(struct sock *sk, long timeo);
  91. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  92. union sctp_addr *addr, int len);
  93. static int sctp_bindx_add(struct sock *, struct sockaddr *, int);
  94. static int sctp_bindx_rem(struct sock *, struct sockaddr *, int);
  95. static int sctp_send_asconf_add_ip(struct sock *, struct sockaddr *, int);
  96. static int sctp_send_asconf_del_ip(struct sock *, struct sockaddr *, int);
  97. static int sctp_send_asconf(struct sctp_association *asoc,
  98. struct sctp_chunk *chunk);
  99. static int sctp_do_bind(struct sock *, union sctp_addr *, int);
  100. static int sctp_autobind(struct sock *sk);
  101. static void sctp_sock_migrate(struct sock *, struct sock *,
  102. struct sctp_association *, sctp_socket_type_t);
  103. static char *sctp_hmac_alg = SCTP_COOKIE_HMAC_ALG;
  104. extern kmem_cache_t *sctp_bucket_cachep;
  105. extern int sysctl_sctp_mem[3];
  106. extern int sysctl_sctp_rmem[3];
  107. extern int sysctl_sctp_wmem[3];
  108. int sctp_memory_pressure;
  109. atomic_t sctp_memory_allocated;
  110. atomic_t sctp_sockets_allocated;
  111. static void sctp_enter_memory_pressure(void)
  112. {
  113. sctp_memory_pressure = 1;
  114. }
  115. /* Get the sndbuf space available at the time on the association. */
  116. static inline int sctp_wspace(struct sctp_association *asoc)
  117. {
  118. int amt;
  119. struct sock *sk = asoc->base.sk;
  120. if (asoc->ep->sndbuf_policy)
  121. amt = asoc->sndbuf_used;
  122. else
  123. amt = atomic_read(&sk->sk_wmem_alloc);
  124. if (amt >= sk->sk_sndbuf) {
  125. if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  126. amt = 0;
  127. else {
  128. amt = sk_stream_wspace(sk);
  129. if (amt < 0)
  130. amt = 0;
  131. }
  132. } else {
  133. amt = sk->sk_sndbuf - amt;
  134. }
  135. return amt;
  136. }
  137. /* Increment the used sndbuf space count of the corresponding association by
  138. * the size of the outgoing data chunk.
  139. * Also, set the skb destructor for sndbuf accounting later.
  140. *
  141. * Since it is always 1-1 between chunk and skb, and also a new skb is always
  142. * allocated for chunk bundling in sctp_packet_transmit(), we can use the
  143. * destructor in the data chunk skb for the purpose of the sndbuf space
  144. * tracking.
  145. */
  146. static inline void sctp_set_owner_w(struct sctp_chunk *chunk)
  147. {
  148. struct sctp_association *asoc = chunk->asoc;
  149. struct sock *sk = asoc->base.sk;
  150. /* The sndbuf space is tracked per association. */
  151. sctp_association_hold(asoc);
  152. skb_set_owner_w(chunk->skb, sk);
  153. chunk->skb->destructor = sctp_wfree;
  154. /* Save the chunk pointer in skb for sctp_wfree to use later. */
  155. *((struct sctp_chunk **)(chunk->skb->cb)) = chunk;
  156. asoc->sndbuf_used += SCTP_DATA_SNDSIZE(chunk) +
  157. sizeof(struct sk_buff) +
  158. sizeof(struct sctp_chunk);
  159. atomic_add(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  160. sk->sk_wmem_queued += chunk->skb->truesize;
  161. sk_mem_charge(sk, chunk->skb->truesize);
  162. }
  163. /* Verify that this is a valid address. */
  164. static inline int sctp_verify_addr(struct sock *sk, union sctp_addr *addr,
  165. int len)
  166. {
  167. struct sctp_af *af;
  168. /* Verify basic sockaddr. */
  169. af = sctp_sockaddr_af(sctp_sk(sk), addr, len);
  170. if (!af)
  171. return -EINVAL;
  172. /* Is this a valid SCTP address? */
  173. if (!af->addr_valid(addr, sctp_sk(sk), NULL))
  174. return -EINVAL;
  175. if (!sctp_sk(sk)->pf->send_verify(sctp_sk(sk), (addr)))
  176. return -EINVAL;
  177. return 0;
  178. }
  179. /* Look up the association by its id. If this is not a UDP-style
  180. * socket, the ID field is always ignored.
  181. */
  182. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id)
  183. {
  184. struct sctp_association *asoc = NULL;
  185. /* If this is not a UDP-style socket, assoc id should be ignored. */
  186. if (!sctp_style(sk, UDP)) {
  187. /* Return NULL if the socket state is not ESTABLISHED. It
  188. * could be a TCP-style listening socket or a socket which
  189. * hasn't yet called connect() to establish an association.
  190. */
  191. if (!sctp_sstate(sk, ESTABLISHED))
  192. return NULL;
  193. /* Get the first and the only association from the list. */
  194. if (!list_empty(&sctp_sk(sk)->ep->asocs))
  195. asoc = list_entry(sctp_sk(sk)->ep->asocs.next,
  196. struct sctp_association, asocs);
  197. return asoc;
  198. }
  199. /* Otherwise this is a UDP-style socket. */
  200. if (!id || (id == (sctp_assoc_t)-1))
  201. return NULL;
  202. spin_lock_bh(&sctp_assocs_id_lock);
  203. asoc = (struct sctp_association *)idr_find(&sctp_assocs_id, (int)id);
  204. spin_unlock_bh(&sctp_assocs_id_lock);
  205. if (!asoc || (asoc->base.sk != sk) || asoc->base.dead)
  206. return NULL;
  207. return asoc;
  208. }
  209. /* Look up the transport from an address and an assoc id. If both address and
  210. * id are specified, the associations matching the address and the id should be
  211. * the same.
  212. */
  213. static struct sctp_transport *sctp_addr_id2transport(struct sock *sk,
  214. struct sockaddr_storage *addr,
  215. sctp_assoc_t id)
  216. {
  217. struct sctp_association *addr_asoc = NULL, *id_asoc = NULL;
  218. struct sctp_transport *transport;
  219. union sctp_addr *laddr = (union sctp_addr *)addr;
  220. laddr->v4.sin_port = ntohs(laddr->v4.sin_port);
  221. addr_asoc = sctp_endpoint_lookup_assoc(sctp_sk(sk)->ep,
  222. (union sctp_addr *)addr,
  223. &transport);
  224. laddr->v4.sin_port = htons(laddr->v4.sin_port);
  225. if (!addr_asoc)
  226. return NULL;
  227. id_asoc = sctp_id2assoc(sk, id);
  228. if (id_asoc && (id_asoc != addr_asoc))
  229. return NULL;
  230. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  231. (union sctp_addr *)addr);
  232. return transport;
  233. }
  234. /* API 3.1.2 bind() - UDP Style Syntax
  235. * The syntax of bind() is,
  236. *
  237. * ret = bind(int sd, struct sockaddr *addr, int addrlen);
  238. *
  239. * sd - the socket descriptor returned by socket().
  240. * addr - the address structure (struct sockaddr_in or struct
  241. * sockaddr_in6 [RFC 2553]),
  242. * addr_len - the size of the address structure.
  243. */
  244. SCTP_STATIC int sctp_bind(struct sock *sk, struct sockaddr *addr, int addr_len)
  245. {
  246. int retval = 0;
  247. sctp_lock_sock(sk);
  248. SCTP_DEBUG_PRINTK("sctp_bind(sk: %p, addr: %p, addr_len: %d)\n",
  249. sk, addr, addr_len);
  250. /* Disallow binding twice. */
  251. if (!sctp_sk(sk)->ep->base.bind_addr.port)
  252. retval = sctp_do_bind(sk, (union sctp_addr *)addr,
  253. addr_len);
  254. else
  255. retval = -EINVAL;
  256. sctp_release_sock(sk);
  257. return retval;
  258. }
  259. static long sctp_get_port_local(struct sock *, union sctp_addr *);
  260. /* Verify this is a valid sockaddr. */
  261. static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt,
  262. union sctp_addr *addr, int len)
  263. {
  264. struct sctp_af *af;
  265. /* Check minimum size. */
  266. if (len < sizeof (struct sockaddr))
  267. return NULL;
  268. /* Does this PF support this AF? */
  269. if (!opt->pf->af_supported(addr->sa.sa_family, opt))
  270. return NULL;
  271. /* If we get this far, af is valid. */
  272. af = sctp_get_af_specific(addr->sa.sa_family);
  273. if (len < af->sockaddr_len)
  274. return NULL;
  275. return af;
  276. }
  277. /* Bind a local address either to an endpoint or to an association. */
  278. SCTP_STATIC int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len)
  279. {
  280. struct sctp_sock *sp = sctp_sk(sk);
  281. struct sctp_endpoint *ep = sp->ep;
  282. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  283. struct sctp_af *af;
  284. unsigned short snum;
  285. int ret = 0;
  286. /* Common sockaddr verification. */
  287. af = sctp_sockaddr_af(sp, addr, len);
  288. if (!af) {
  289. SCTP_DEBUG_PRINTK("sctp_do_bind(sk: %p, newaddr: %p, len: %d) EINVAL\n",
  290. sk, addr, len);
  291. return -EINVAL;
  292. }
  293. snum = ntohs(addr->v4.sin_port);
  294. SCTP_DEBUG_PRINTK_IPADDR("sctp_do_bind(sk: %p, new addr: ",
  295. ", port: %d, new port: %d, len: %d)\n",
  296. sk,
  297. addr,
  298. bp->port, snum,
  299. len);
  300. /* PF specific bind() address verification. */
  301. if (!sp->pf->bind_verify(sp, addr))
  302. return -EADDRNOTAVAIL;
  303. /* We must either be unbound, or bind to the same port. */
  304. if (bp->port && (snum != bp->port)) {
  305. SCTP_DEBUG_PRINTK("sctp_do_bind:"
  306. " New port %d does not match existing port "
  307. "%d.\n", snum, bp->port);
  308. return -EINVAL;
  309. }
  310. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  311. return -EACCES;
  312. /* Make sure we are allowed to bind here.
  313. * The function sctp_get_port_local() does duplicate address
  314. * detection.
  315. */
  316. if ((ret = sctp_get_port_local(sk, addr))) {
  317. if (ret == (long) sk) {
  318. /* This endpoint has a conflicting address. */
  319. return -EINVAL;
  320. } else {
  321. return -EADDRINUSE;
  322. }
  323. }
  324. /* Refresh ephemeral port. */
  325. if (!bp->port)
  326. bp->port = inet_sk(sk)->num;
  327. /* Add the address to the bind address list. */
  328. sctp_local_bh_disable();
  329. sctp_write_lock(&ep->base.addr_lock);
  330. /* Use GFP_ATOMIC since BHs are disabled. */
  331. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  332. ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
  333. addr->v4.sin_port = htons(addr->v4.sin_port);
  334. sctp_write_unlock(&ep->base.addr_lock);
  335. sctp_local_bh_enable();
  336. /* Copy back into socket for getsockname() use. */
  337. if (!ret) {
  338. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  339. af->to_sk_saddr(addr, sk);
  340. }
  341. return ret;
  342. }
  343. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  344. *
  345. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  346. * at any one time. If a sender, after sending an ASCONF chunk, decides
  347. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  348. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  349. * subsequent ASCONF. Note this restriction binds each side, so at any
  350. * time two ASCONF may be in-transit on any given association (one sent
  351. * from each endpoint).
  352. */
  353. static int sctp_send_asconf(struct sctp_association *asoc,
  354. struct sctp_chunk *chunk)
  355. {
  356. int retval = 0;
  357. /* If there is an outstanding ASCONF chunk, queue it for later
  358. * transmission.
  359. */
  360. if (asoc->addip_last_asconf) {
  361. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  362. goto out;
  363. }
  364. /* Hold the chunk until an ASCONF_ACK is received. */
  365. sctp_chunk_hold(chunk);
  366. retval = sctp_primitive_ASCONF(asoc, chunk);
  367. if (retval)
  368. sctp_chunk_free(chunk);
  369. else
  370. asoc->addip_last_asconf = chunk;
  371. out:
  372. return retval;
  373. }
  374. /* Add a list of addresses as bind addresses to local endpoint or
  375. * association.
  376. *
  377. * Basically run through each address specified in the addrs/addrcnt
  378. * array/length pair, determine if it is IPv6 or IPv4 and call
  379. * sctp_do_bind() on it.
  380. *
  381. * If any of them fails, then the operation will be reversed and the
  382. * ones that were added will be removed.
  383. *
  384. * Only sctp_setsockopt_bindx() is supposed to call this function.
  385. */
  386. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  387. {
  388. int cnt;
  389. int retval = 0;
  390. void *addr_buf;
  391. struct sockaddr *sa_addr;
  392. struct sctp_af *af;
  393. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  394. sk, addrs, addrcnt);
  395. addr_buf = addrs;
  396. for (cnt = 0; cnt < addrcnt; cnt++) {
  397. /* The list may contain either IPv4 or IPv6 address;
  398. * determine the address length for walking thru the list.
  399. */
  400. sa_addr = (struct sockaddr *)addr_buf;
  401. af = sctp_get_af_specific(sa_addr->sa_family);
  402. if (!af) {
  403. retval = -EINVAL;
  404. goto err_bindx_add;
  405. }
  406. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  407. af->sockaddr_len);
  408. addr_buf += af->sockaddr_len;
  409. err_bindx_add:
  410. if (retval < 0) {
  411. /* Failed. Cleanup the ones that have been added */
  412. if (cnt > 0)
  413. sctp_bindx_rem(sk, addrs, cnt);
  414. return retval;
  415. }
  416. }
  417. return retval;
  418. }
  419. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  420. * associations that are part of the endpoint indicating that a list of local
  421. * addresses are added to the endpoint.
  422. *
  423. * If any of the addresses is already in the bind address list of the
  424. * association, we do not send the chunk for that association. But it will not
  425. * affect other associations.
  426. *
  427. * Only sctp_setsockopt_bindx() is supposed to call this function.
  428. */
  429. static int sctp_send_asconf_add_ip(struct sock *sk,
  430. struct sockaddr *addrs,
  431. int addrcnt)
  432. {
  433. struct sctp_sock *sp;
  434. struct sctp_endpoint *ep;
  435. struct sctp_association *asoc;
  436. struct sctp_bind_addr *bp;
  437. struct sctp_chunk *chunk;
  438. struct sctp_sockaddr_entry *laddr;
  439. union sctp_addr *addr;
  440. union sctp_addr saveaddr;
  441. void *addr_buf;
  442. struct sctp_af *af;
  443. struct list_head *pos;
  444. struct list_head *p;
  445. int i;
  446. int retval = 0;
  447. if (!sctp_addip_enable)
  448. return retval;
  449. sp = sctp_sk(sk);
  450. ep = sp->ep;
  451. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  452. __FUNCTION__, sk, addrs, addrcnt);
  453. list_for_each(pos, &ep->asocs) {
  454. asoc = list_entry(pos, struct sctp_association, asocs);
  455. if (!asoc->peer.asconf_capable)
  456. continue;
  457. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  458. continue;
  459. if (!sctp_state(asoc, ESTABLISHED))
  460. continue;
  461. /* Check if any address in the packed array of addresses is
  462. * in the bind address list of the association. If so,
  463. * do not send the asconf chunk to its peer, but continue with
  464. * other associations.
  465. */
  466. addr_buf = addrs;
  467. for (i = 0; i < addrcnt; i++) {
  468. addr = (union sctp_addr *)addr_buf;
  469. af = sctp_get_af_specific(addr->v4.sin_family);
  470. if (!af) {
  471. retval = -EINVAL;
  472. goto out;
  473. }
  474. if (sctp_assoc_lookup_laddr(asoc, addr))
  475. break;
  476. addr_buf += af->sockaddr_len;
  477. }
  478. if (i < addrcnt)
  479. continue;
  480. /* Use the first address in bind addr list of association as
  481. * Address Parameter of ASCONF CHUNK.
  482. */
  483. sctp_read_lock(&asoc->base.addr_lock);
  484. bp = &asoc->base.bind_addr;
  485. p = bp->address_list.next;
  486. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  487. sctp_read_unlock(&asoc->base.addr_lock);
  488. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  489. addrcnt, SCTP_PARAM_ADD_IP);
  490. if (!chunk) {
  491. retval = -ENOMEM;
  492. goto out;
  493. }
  494. retval = sctp_send_asconf(asoc, chunk);
  495. if (retval)
  496. goto out;
  497. /* Add the new addresses to the bind address list with
  498. * use_as_src set to 0.
  499. */
  500. sctp_local_bh_disable();
  501. sctp_write_lock(&asoc->base.addr_lock);
  502. addr_buf = addrs;
  503. for (i = 0; i < addrcnt; i++) {
  504. addr = (union sctp_addr *)addr_buf;
  505. af = sctp_get_af_specific(addr->v4.sin_family);
  506. memcpy(&saveaddr, addr, af->sockaddr_len);
  507. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  508. retval = sctp_add_bind_addr(bp, &saveaddr, 0,
  509. GFP_ATOMIC);
  510. addr_buf += af->sockaddr_len;
  511. }
  512. sctp_write_unlock(&asoc->base.addr_lock);
  513. sctp_local_bh_enable();
  514. }
  515. out:
  516. return retval;
  517. }
  518. /* Remove a list of addresses from bind addresses list. Do not remove the
  519. * last address.
  520. *
  521. * Basically run through each address specified in the addrs/addrcnt
  522. * array/length pair, determine if it is IPv6 or IPv4 and call
  523. * sctp_del_bind() on it.
  524. *
  525. * If any of them fails, then the operation will be reversed and the
  526. * ones that were removed will be added back.
  527. *
  528. * At least one address has to be left; if only one address is
  529. * available, the operation will return -EBUSY.
  530. *
  531. * Only sctp_setsockopt_bindx() is supposed to call this function.
  532. */
  533. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  534. {
  535. struct sctp_sock *sp = sctp_sk(sk);
  536. struct sctp_endpoint *ep = sp->ep;
  537. int cnt;
  538. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  539. int retval = 0;
  540. union sctp_addr saveaddr;
  541. void *addr_buf;
  542. struct sockaddr *sa_addr;
  543. struct sctp_af *af;
  544. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  545. sk, addrs, addrcnt);
  546. addr_buf = addrs;
  547. for (cnt = 0; cnt < addrcnt; cnt++) {
  548. /* If the bind address list is empty or if there is only one
  549. * bind address, there is nothing more to be removed (we need
  550. * at least one address here).
  551. */
  552. if (list_empty(&bp->address_list) ||
  553. (sctp_list_single_entry(&bp->address_list))) {
  554. retval = -EBUSY;
  555. goto err_bindx_rem;
  556. }
  557. /* The list may contain either IPv4 or IPv6 address;
  558. * determine the address length to copy the address to
  559. * saveaddr.
  560. */
  561. sa_addr = (struct sockaddr *)addr_buf;
  562. af = sctp_get_af_specific(sa_addr->sa_family);
  563. if (!af) {
  564. retval = -EINVAL;
  565. goto err_bindx_rem;
  566. }
  567. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  568. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  569. if (saveaddr.v4.sin_port != bp->port) {
  570. retval = -EINVAL;
  571. goto err_bindx_rem;
  572. }
  573. /* FIXME - There is probably a need to check if sk->sk_saddr and
  574. * sk->sk_rcv_addr are currently set to one of the addresses to
  575. * be removed. This is something which needs to be looked into
  576. * when we are fixing the outstanding issues with multi-homing
  577. * socket routing and failover schemes. Refer to comments in
  578. * sctp_do_bind(). -daisy
  579. */
  580. sctp_local_bh_disable();
  581. sctp_write_lock(&ep->base.addr_lock);
  582. retval = sctp_del_bind_addr(bp, &saveaddr);
  583. sctp_write_unlock(&ep->base.addr_lock);
  584. sctp_local_bh_enable();
  585. addr_buf += af->sockaddr_len;
  586. err_bindx_rem:
  587. if (retval < 0) {
  588. /* Failed. Add the ones that has been removed back */
  589. if (cnt > 0)
  590. sctp_bindx_add(sk, addrs, cnt);
  591. return retval;
  592. }
  593. }
  594. return retval;
  595. }
  596. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  597. * the associations that are part of the endpoint indicating that a list of
  598. * local addresses are removed from the endpoint.
  599. *
  600. * If any of the addresses is already in the bind address list of the
  601. * association, we do not send the chunk for that association. But it will not
  602. * affect other associations.
  603. *
  604. * Only sctp_setsockopt_bindx() is supposed to call this function.
  605. */
  606. static int sctp_send_asconf_del_ip(struct sock *sk,
  607. struct sockaddr *addrs,
  608. int addrcnt)
  609. {
  610. struct sctp_sock *sp;
  611. struct sctp_endpoint *ep;
  612. struct sctp_association *asoc;
  613. struct sctp_transport *transport;
  614. struct sctp_bind_addr *bp;
  615. struct sctp_chunk *chunk;
  616. union sctp_addr *laddr;
  617. union sctp_addr saveaddr;
  618. void *addr_buf;
  619. struct sctp_af *af;
  620. struct list_head *pos, *pos1;
  621. struct sctp_sockaddr_entry *saddr;
  622. int i;
  623. int retval = 0;
  624. if (!sctp_addip_enable)
  625. return retval;
  626. sp = sctp_sk(sk);
  627. ep = sp->ep;
  628. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  629. __FUNCTION__, sk, addrs, addrcnt);
  630. list_for_each(pos, &ep->asocs) {
  631. asoc = list_entry(pos, struct sctp_association, asocs);
  632. if (!asoc->peer.asconf_capable)
  633. continue;
  634. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  635. continue;
  636. if (!sctp_state(asoc, ESTABLISHED))
  637. continue;
  638. /* Check if any address in the packed array of addresses is
  639. * not present in the bind address list of the association.
  640. * If so, do not send the asconf chunk to its peer, but
  641. * continue with other associations.
  642. */
  643. addr_buf = addrs;
  644. for (i = 0; i < addrcnt; i++) {
  645. laddr = (union sctp_addr *)addr_buf;
  646. af = sctp_get_af_specific(laddr->v4.sin_family);
  647. if (!af) {
  648. retval = -EINVAL;
  649. goto out;
  650. }
  651. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  652. break;
  653. addr_buf += af->sockaddr_len;
  654. }
  655. if (i < addrcnt)
  656. continue;
  657. /* Find one address in the association's bind address list
  658. * that is not in the packed array of addresses. This is to
  659. * make sure that we do not delete all the addresses in the
  660. * association.
  661. */
  662. sctp_read_lock(&asoc->base.addr_lock);
  663. bp = &asoc->base.bind_addr;
  664. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  665. addrcnt, sp);
  666. sctp_read_unlock(&asoc->base.addr_lock);
  667. if (!laddr)
  668. continue;
  669. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  670. SCTP_PARAM_DEL_IP);
  671. if (!chunk) {
  672. retval = -ENOMEM;
  673. goto out;
  674. }
  675. /* Reset use_as_src flag for the addresses in the bind address
  676. * list that are to be deleted.
  677. */
  678. sctp_local_bh_disable();
  679. sctp_write_lock(&asoc->base.addr_lock);
  680. addr_buf = addrs;
  681. for (i = 0; i < addrcnt; i++) {
  682. laddr = (union sctp_addr *)addr_buf;
  683. af = sctp_get_af_specific(laddr->v4.sin_family);
  684. memcpy(&saveaddr, laddr, af->sockaddr_len);
  685. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  686. list_for_each(pos1, &bp->address_list) {
  687. saddr = list_entry(pos1,
  688. struct sctp_sockaddr_entry,
  689. list);
  690. if (sctp_cmp_addr_exact(&saddr->a, &saveaddr))
  691. saddr->use_as_src = 0;
  692. }
  693. addr_buf += af->sockaddr_len;
  694. }
  695. sctp_write_unlock(&asoc->base.addr_lock);
  696. sctp_local_bh_enable();
  697. /* Update the route and saddr entries for all the transports
  698. * as some of the addresses in the bind address list are
  699. * about to be deleted and cannot be used as source addresses.
  700. */
  701. list_for_each(pos1, &asoc->peer.transport_addr_list) {
  702. transport = list_entry(pos1, struct sctp_transport,
  703. transports);
  704. dst_release(transport->dst);
  705. sctp_transport_route(transport, NULL,
  706. sctp_sk(asoc->base.sk));
  707. }
  708. retval = sctp_send_asconf(asoc, chunk);
  709. }
  710. out:
  711. return retval;
  712. }
  713. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  714. *
  715. * API 8.1
  716. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  717. * int flags);
  718. *
  719. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  720. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  721. * or IPv6 addresses.
  722. *
  723. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  724. * Section 3.1.2 for this usage.
  725. *
  726. * addrs is a pointer to an array of one or more socket addresses. Each
  727. * address is contained in its appropriate structure (i.e. struct
  728. * sockaddr_in or struct sockaddr_in6) the family of the address type
  729. * must be used to distengish the address length (note that this
  730. * representation is termed a "packed array" of addresses). The caller
  731. * specifies the number of addresses in the array with addrcnt.
  732. *
  733. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  734. * -1, and sets errno to the appropriate error code.
  735. *
  736. * For SCTP, the port given in each socket address must be the same, or
  737. * sctp_bindx() will fail, setting errno to EINVAL.
  738. *
  739. * The flags parameter is formed from the bitwise OR of zero or more of
  740. * the following currently defined flags:
  741. *
  742. * SCTP_BINDX_ADD_ADDR
  743. *
  744. * SCTP_BINDX_REM_ADDR
  745. *
  746. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  747. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  748. * addresses from the association. The two flags are mutually exclusive;
  749. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  750. * not remove all addresses from an association; sctp_bindx() will
  751. * reject such an attempt with EINVAL.
  752. *
  753. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  754. * additional addresses with an endpoint after calling bind(). Or use
  755. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  756. * socket is associated with so that no new association accepted will be
  757. * associated with those addresses. If the endpoint supports dynamic
  758. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  759. * endpoint to send the appropriate message to the peer to change the
  760. * peers address lists.
  761. *
  762. * Adding and removing addresses from a connected association is
  763. * optional functionality. Implementations that do not support this
  764. * functionality should return EOPNOTSUPP.
  765. *
  766. * Basically do nothing but copying the addresses from user to kernel
  767. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  768. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  769. * from userspace.
  770. *
  771. * We don't use copy_from_user() for optimization: we first do the
  772. * sanity checks (buffer size -fast- and access check-healthy
  773. * pointer); if all of those succeed, then we can alloc the memory
  774. * (expensive operation) needed to copy the data to kernel. Then we do
  775. * the copying without checking the user space area
  776. * (__copy_from_user()).
  777. *
  778. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  779. * it.
  780. *
  781. * sk The sk of the socket
  782. * addrs The pointer to the addresses in user land
  783. * addrssize Size of the addrs buffer
  784. * op Operation to perform (add or remove, see the flags of
  785. * sctp_bindx)
  786. *
  787. * Returns 0 if ok, <0 errno code on error.
  788. */
  789. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  790. struct sockaddr __user *addrs,
  791. int addrs_size, int op)
  792. {
  793. struct sockaddr *kaddrs;
  794. int err;
  795. int addrcnt = 0;
  796. int walk_size = 0;
  797. struct sockaddr *sa_addr;
  798. void *addr_buf;
  799. struct sctp_af *af;
  800. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  801. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  802. if (unlikely(addrs_size <= 0))
  803. return -EINVAL;
  804. /* Check the user passed a healthy pointer. */
  805. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  806. return -EFAULT;
  807. /* Alloc space for the address array in kernel memory. */
  808. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  809. if (unlikely(!kaddrs))
  810. return -ENOMEM;
  811. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  812. kfree(kaddrs);
  813. return -EFAULT;
  814. }
  815. /* Walk through the addrs buffer and count the number of addresses. */
  816. addr_buf = kaddrs;
  817. while (walk_size < addrs_size) {
  818. sa_addr = (struct sockaddr *)addr_buf;
  819. af = sctp_get_af_specific(sa_addr->sa_family);
  820. /* If the address family is not supported or if this address
  821. * causes the address buffer to overflow return EINVAL.
  822. */
  823. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  824. kfree(kaddrs);
  825. return -EINVAL;
  826. }
  827. addrcnt++;
  828. addr_buf += af->sockaddr_len;
  829. walk_size += af->sockaddr_len;
  830. }
  831. /* Do the work. */
  832. switch (op) {
  833. case SCTP_BINDX_ADD_ADDR:
  834. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  835. if (err)
  836. goto out;
  837. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  838. break;
  839. case SCTP_BINDX_REM_ADDR:
  840. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  841. if (err)
  842. goto out;
  843. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  844. break;
  845. default:
  846. err = -EINVAL;
  847. break;
  848. };
  849. out:
  850. kfree(kaddrs);
  851. return err;
  852. }
  853. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  854. *
  855. * Common routine for handling connect() and sctp_connectx().
  856. * Connect will come in with just a single address.
  857. */
  858. static int __sctp_connect(struct sock* sk,
  859. struct sockaddr *kaddrs,
  860. int addrs_size)
  861. {
  862. struct sctp_sock *sp;
  863. struct sctp_endpoint *ep;
  864. struct sctp_association *asoc = NULL;
  865. struct sctp_association *asoc2;
  866. struct sctp_transport *transport;
  867. union sctp_addr to;
  868. struct sctp_af *af;
  869. sctp_scope_t scope;
  870. long timeo;
  871. int err = 0;
  872. int addrcnt = 0;
  873. int walk_size = 0;
  874. struct sockaddr *sa_addr;
  875. void *addr_buf;
  876. sp = sctp_sk(sk);
  877. ep = sp->ep;
  878. /* connect() cannot be done on a socket that is already in ESTABLISHED
  879. * state - UDP-style peeled off socket or a TCP-style socket that
  880. * is already connected.
  881. * It cannot be done even on a TCP-style listening socket.
  882. */
  883. if (sctp_sstate(sk, ESTABLISHED) ||
  884. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  885. err = -EISCONN;
  886. goto out_free;
  887. }
  888. /* Walk through the addrs buffer and count the number of addresses. */
  889. addr_buf = kaddrs;
  890. while (walk_size < addrs_size) {
  891. sa_addr = (struct sockaddr *)addr_buf;
  892. af = sctp_get_af_specific(sa_addr->sa_family);
  893. /* If the address family is not supported or if this address
  894. * causes the address buffer to overflow return EINVAL.
  895. */
  896. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  897. err = -EINVAL;
  898. goto out_free;
  899. }
  900. err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
  901. af->sockaddr_len);
  902. if (err)
  903. goto out_free;
  904. memcpy(&to, sa_addr, af->sockaddr_len);
  905. to.v4.sin_port = ntohs(to.v4.sin_port);
  906. /* Check if there already is a matching association on the
  907. * endpoint (other than the one created here).
  908. */
  909. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  910. if (asoc2 && asoc2 != asoc) {
  911. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  912. err = -EISCONN;
  913. else
  914. err = -EALREADY;
  915. goto out_free;
  916. }
  917. /* If we could not find a matching association on the endpoint,
  918. * make sure that there is no peeled-off association matching
  919. * the peer address even on another socket.
  920. */
  921. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  922. err = -EADDRNOTAVAIL;
  923. goto out_free;
  924. }
  925. if (!asoc) {
  926. /* If a bind() or sctp_bindx() is not called prior to
  927. * an sctp_connectx() call, the system picks an
  928. * ephemeral port and will choose an address set
  929. * equivalent to binding with a wildcard address.
  930. */
  931. if (!ep->base.bind_addr.port) {
  932. if (sctp_autobind(sk)) {
  933. err = -EAGAIN;
  934. goto out_free;
  935. }
  936. } else {
  937. /*
  938. * If an unprivileged user inherits a 1-many
  939. * style socket with open associations on a
  940. * privileged port, it MAY be permitted to
  941. * accept new associations, but it SHOULD NOT
  942. * be permitted to open new associations.
  943. */
  944. if (ep->base.bind_addr.port < PROT_SOCK &&
  945. !capable(CAP_NET_BIND_SERVICE)) {
  946. err = -EACCES;
  947. goto out_free;
  948. }
  949. }
  950. scope = sctp_scope(&to);
  951. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  952. if (!asoc) {
  953. err = -ENOMEM;
  954. goto out_free;
  955. }
  956. }
  957. /* Prime the peer's transport structures. */
  958. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  959. SCTP_UNKNOWN);
  960. if (!transport) {
  961. err = -ENOMEM;
  962. goto out_free;
  963. }
  964. addrcnt++;
  965. addr_buf += af->sockaddr_len;
  966. walk_size += af->sockaddr_len;
  967. }
  968. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  969. if (err < 0) {
  970. goto out_free;
  971. }
  972. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  973. if (err < 0) {
  974. goto out_free;
  975. }
  976. /* Initialize sk's dport and daddr for getpeername() */
  977. inet_sk(sk)->dport = htons(asoc->peer.port);
  978. af = sctp_get_af_specific(to.sa.sa_family);
  979. af->to_sk_daddr(&to, sk);
  980. sk->sk_err = 0;
  981. timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  982. err = sctp_wait_for_connect(asoc, &timeo);
  983. /* Don't free association on exit. */
  984. asoc = NULL;
  985. out_free:
  986. SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
  987. " kaddrs: %p err: %d\n",
  988. asoc, kaddrs, err);
  989. if (asoc)
  990. sctp_association_free(asoc);
  991. return err;
  992. }
  993. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  994. *
  995. * API 8.9
  996. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
  997. *
  998. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  999. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1000. * or IPv6 addresses.
  1001. *
  1002. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1003. * Section 3.1.2 for this usage.
  1004. *
  1005. * addrs is a pointer to an array of one or more socket addresses. Each
  1006. * address is contained in its appropriate structure (i.e. struct
  1007. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1008. * must be used to distengish the address length (note that this
  1009. * representation is termed a "packed array" of addresses). The caller
  1010. * specifies the number of addresses in the array with addrcnt.
  1011. *
  1012. * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
  1013. * -1, and sets errno to the appropriate error code.
  1014. *
  1015. * For SCTP, the port given in each socket address must be the same, or
  1016. * sctp_connectx() will fail, setting errno to EINVAL.
  1017. *
  1018. * An application can use sctp_connectx to initiate an association with
  1019. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1020. * allows a caller to specify multiple addresses at which a peer can be
  1021. * reached. The way the SCTP stack uses the list of addresses to set up
  1022. * the association is implementation dependant. This function only
  1023. * specifies that the stack will try to make use of all the addresses in
  1024. * the list when needed.
  1025. *
  1026. * Note that the list of addresses passed in is only used for setting up
  1027. * the association. It does not necessarily equal the set of addresses
  1028. * the peer uses for the resulting association. If the caller wants to
  1029. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1030. * retrieve them after the association has been set up.
  1031. *
  1032. * Basically do nothing but copying the addresses from user to kernel
  1033. * land and invoking either sctp_connectx(). This is used for tunneling
  1034. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1035. *
  1036. * We don't use copy_from_user() for optimization: we first do the
  1037. * sanity checks (buffer size -fast- and access check-healthy
  1038. * pointer); if all of those succeed, then we can alloc the memory
  1039. * (expensive operation) needed to copy the data to kernel. Then we do
  1040. * the copying without checking the user space area
  1041. * (__copy_from_user()).
  1042. *
  1043. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1044. * it.
  1045. *
  1046. * sk The sk of the socket
  1047. * addrs The pointer to the addresses in user land
  1048. * addrssize Size of the addrs buffer
  1049. *
  1050. * Returns 0 if ok, <0 errno code on error.
  1051. */
  1052. SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
  1053. struct sockaddr __user *addrs,
  1054. int addrs_size)
  1055. {
  1056. int err = 0;
  1057. struct sockaddr *kaddrs;
  1058. SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
  1059. __FUNCTION__, sk, addrs, addrs_size);
  1060. if (unlikely(addrs_size <= 0))
  1061. return -EINVAL;
  1062. /* Check the user passed a healthy pointer. */
  1063. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1064. return -EFAULT;
  1065. /* Alloc space for the address array in kernel memory. */
  1066. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  1067. if (unlikely(!kaddrs))
  1068. return -ENOMEM;
  1069. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1070. err = -EFAULT;
  1071. } else {
  1072. err = __sctp_connect(sk, kaddrs, addrs_size);
  1073. }
  1074. kfree(kaddrs);
  1075. return err;
  1076. }
  1077. /* API 3.1.4 close() - UDP Style Syntax
  1078. * Applications use close() to perform graceful shutdown (as described in
  1079. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1080. * by a UDP-style socket.
  1081. *
  1082. * The syntax is
  1083. *
  1084. * ret = close(int sd);
  1085. *
  1086. * sd - the socket descriptor of the associations to be closed.
  1087. *
  1088. * To gracefully shutdown a specific association represented by the
  1089. * UDP-style socket, an application should use the sendmsg() call,
  1090. * passing no user data, but including the appropriate flag in the
  1091. * ancillary data (see Section xxxx).
  1092. *
  1093. * If sd in the close() call is a branched-off socket representing only
  1094. * one association, the shutdown is performed on that association only.
  1095. *
  1096. * 4.1.6 close() - TCP Style Syntax
  1097. *
  1098. * Applications use close() to gracefully close down an association.
  1099. *
  1100. * The syntax is:
  1101. *
  1102. * int close(int sd);
  1103. *
  1104. * sd - the socket descriptor of the association to be closed.
  1105. *
  1106. * After an application calls close() on a socket descriptor, no further
  1107. * socket operations will succeed on that descriptor.
  1108. *
  1109. * API 7.1.4 SO_LINGER
  1110. *
  1111. * An application using the TCP-style socket can use this option to
  1112. * perform the SCTP ABORT primitive. The linger option structure is:
  1113. *
  1114. * struct linger {
  1115. * int l_onoff; // option on/off
  1116. * int l_linger; // linger time
  1117. * };
  1118. *
  1119. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1120. * to 0, calling close() is the same as the ABORT primitive. If the
  1121. * value is set to a negative value, the setsockopt() call will return
  1122. * an error. If the value is set to a positive value linger_time, the
  1123. * close() can be blocked for at most linger_time ms. If the graceful
  1124. * shutdown phase does not finish during this period, close() will
  1125. * return but the graceful shutdown phase continues in the system.
  1126. */
  1127. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  1128. {
  1129. struct sctp_endpoint *ep;
  1130. struct sctp_association *asoc;
  1131. struct list_head *pos, *temp;
  1132. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  1133. sctp_lock_sock(sk);
  1134. sk->sk_shutdown = SHUTDOWN_MASK;
  1135. ep = sctp_sk(sk)->ep;
  1136. /* Walk all associations on an endpoint. */
  1137. list_for_each_safe(pos, temp, &ep->asocs) {
  1138. asoc = list_entry(pos, struct sctp_association, asocs);
  1139. if (sctp_style(sk, TCP)) {
  1140. /* A closed association can still be in the list if
  1141. * it belongs to a TCP-style listening socket that is
  1142. * not yet accepted. If so, free it. If not, send an
  1143. * ABORT or SHUTDOWN based on the linger options.
  1144. */
  1145. if (sctp_state(asoc, CLOSED)) {
  1146. sctp_unhash_established(asoc);
  1147. sctp_association_free(asoc);
  1148. continue;
  1149. }
  1150. }
  1151. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  1152. struct sctp_chunk *chunk;
  1153. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1154. if (chunk)
  1155. sctp_primitive_ABORT(asoc, chunk);
  1156. } else
  1157. sctp_primitive_SHUTDOWN(asoc, NULL);
  1158. }
  1159. /* Clean up any skbs sitting on the receive queue. */
  1160. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1161. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1162. /* On a TCP-style socket, block for at most linger_time if set. */
  1163. if (sctp_style(sk, TCP) && timeout)
  1164. sctp_wait_for_close(sk, timeout);
  1165. /* This will run the backlog queue. */
  1166. sctp_release_sock(sk);
  1167. /* Supposedly, no process has access to the socket, but
  1168. * the net layers still may.
  1169. */
  1170. sctp_local_bh_disable();
  1171. sctp_bh_lock_sock(sk);
  1172. /* Hold the sock, since sk_common_release() will put sock_put()
  1173. * and we have just a little more cleanup.
  1174. */
  1175. sock_hold(sk);
  1176. sk_common_release(sk);
  1177. sctp_bh_unlock_sock(sk);
  1178. sctp_local_bh_enable();
  1179. sock_put(sk);
  1180. SCTP_DBG_OBJCNT_DEC(sock);
  1181. }
  1182. /* Handle EPIPE error. */
  1183. static int sctp_error(struct sock *sk, int flags, int err)
  1184. {
  1185. if (err == -EPIPE)
  1186. err = sock_error(sk) ? : -EPIPE;
  1187. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1188. send_sig(SIGPIPE, current, 0);
  1189. return err;
  1190. }
  1191. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1192. *
  1193. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1194. * and receive data from its peer.
  1195. *
  1196. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1197. * int flags);
  1198. *
  1199. * socket - the socket descriptor of the endpoint.
  1200. * message - pointer to the msghdr structure which contains a single
  1201. * user message and possibly some ancillary data.
  1202. *
  1203. * See Section 5 for complete description of the data
  1204. * structures.
  1205. *
  1206. * flags - flags sent or received with the user message, see Section
  1207. * 5 for complete description of the flags.
  1208. *
  1209. * Note: This function could use a rewrite especially when explicit
  1210. * connect support comes in.
  1211. */
  1212. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1213. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  1214. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  1215. struct msghdr *msg, size_t msg_len)
  1216. {
  1217. struct sctp_sock *sp;
  1218. struct sctp_endpoint *ep;
  1219. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  1220. struct sctp_transport *transport, *chunk_tp;
  1221. struct sctp_chunk *chunk;
  1222. union sctp_addr to;
  1223. struct sockaddr *msg_name = NULL;
  1224. struct sctp_sndrcvinfo default_sinfo = { 0 };
  1225. struct sctp_sndrcvinfo *sinfo;
  1226. struct sctp_initmsg *sinit;
  1227. sctp_assoc_t associd = 0;
  1228. sctp_cmsgs_t cmsgs = { NULL };
  1229. int err;
  1230. sctp_scope_t scope;
  1231. long timeo;
  1232. __u16 sinfo_flags = 0;
  1233. struct sctp_datamsg *datamsg;
  1234. struct list_head *pos;
  1235. int msg_flags = msg->msg_flags;
  1236. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  1237. sk, msg, msg_len);
  1238. err = 0;
  1239. sp = sctp_sk(sk);
  1240. ep = sp->ep;
  1241. SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
  1242. /* We cannot send a message over a TCP-style listening socket. */
  1243. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1244. err = -EPIPE;
  1245. goto out_nounlock;
  1246. }
  1247. /* Parse out the SCTP CMSGs. */
  1248. err = sctp_msghdr_parse(msg, &cmsgs);
  1249. if (err) {
  1250. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  1251. goto out_nounlock;
  1252. }
  1253. /* Fetch the destination address for this packet. This
  1254. * address only selects the association--it is not necessarily
  1255. * the address we will send to.
  1256. * For a peeled-off socket, msg_name is ignored.
  1257. */
  1258. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1259. int msg_namelen = msg->msg_namelen;
  1260. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1261. msg_namelen);
  1262. if (err)
  1263. return err;
  1264. if (msg_namelen > sizeof(to))
  1265. msg_namelen = sizeof(to);
  1266. memcpy(&to, msg->msg_name, msg_namelen);
  1267. SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
  1268. "0x%x:%u.\n",
  1269. to.v4.sin_addr.s_addr, to.v4.sin_port);
  1270. to.v4.sin_port = ntohs(to.v4.sin_port);
  1271. msg_name = msg->msg_name;
  1272. }
  1273. sinfo = cmsgs.info;
  1274. sinit = cmsgs.init;
  1275. /* Did the user specify SNDRCVINFO? */
  1276. if (sinfo) {
  1277. sinfo_flags = sinfo->sinfo_flags;
  1278. associd = sinfo->sinfo_assoc_id;
  1279. }
  1280. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  1281. msg_len, sinfo_flags);
  1282. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1283. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1284. err = -EINVAL;
  1285. goto out_nounlock;
  1286. }
  1287. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1288. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1289. * If SCTP_ABORT is set, the message length could be non zero with
  1290. * the msg_iov set to the user abort reason.
  1291. */
  1292. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1293. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1294. err = -EINVAL;
  1295. goto out_nounlock;
  1296. }
  1297. /* If SCTP_ADDR_OVER is set, there must be an address
  1298. * specified in msg_name.
  1299. */
  1300. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1301. err = -EINVAL;
  1302. goto out_nounlock;
  1303. }
  1304. transport = NULL;
  1305. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1306. sctp_lock_sock(sk);
  1307. /* If a msg_name has been specified, assume this is to be used. */
  1308. if (msg_name) {
  1309. /* Look for a matching association on the endpoint. */
  1310. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1311. if (!asoc) {
  1312. /* If we could not find a matching association on the
  1313. * endpoint, make sure that it is not a TCP-style
  1314. * socket that already has an association or there is
  1315. * no peeled-off association on another socket.
  1316. */
  1317. if ((sctp_style(sk, TCP) &&
  1318. sctp_sstate(sk, ESTABLISHED)) ||
  1319. sctp_endpoint_is_peeled_off(ep, &to)) {
  1320. err = -EADDRNOTAVAIL;
  1321. goto out_unlock;
  1322. }
  1323. }
  1324. } else {
  1325. asoc = sctp_id2assoc(sk, associd);
  1326. if (!asoc) {
  1327. err = -EPIPE;
  1328. goto out_unlock;
  1329. }
  1330. }
  1331. if (asoc) {
  1332. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1333. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1334. * socket that has an association in CLOSED state. This can
  1335. * happen when an accepted socket has an association that is
  1336. * already CLOSED.
  1337. */
  1338. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1339. err = -EPIPE;
  1340. goto out_unlock;
  1341. }
  1342. if (sinfo_flags & SCTP_EOF) {
  1343. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1344. asoc);
  1345. sctp_primitive_SHUTDOWN(asoc, NULL);
  1346. err = 0;
  1347. goto out_unlock;
  1348. }
  1349. if (sinfo_flags & SCTP_ABORT) {
  1350. struct sctp_chunk *chunk;
  1351. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1352. if (!chunk) {
  1353. err = -ENOMEM;
  1354. goto out_unlock;
  1355. }
  1356. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1357. sctp_primitive_ABORT(asoc, chunk);
  1358. err = 0;
  1359. goto out_unlock;
  1360. }
  1361. }
  1362. /* Do we need to create the association? */
  1363. if (!asoc) {
  1364. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1365. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1366. err = -EINVAL;
  1367. goto out_unlock;
  1368. }
  1369. /* Check for invalid stream against the stream counts,
  1370. * either the default or the user specified stream counts.
  1371. */
  1372. if (sinfo) {
  1373. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1374. /* Check against the defaults. */
  1375. if (sinfo->sinfo_stream >=
  1376. sp->initmsg.sinit_num_ostreams) {
  1377. err = -EINVAL;
  1378. goto out_unlock;
  1379. }
  1380. } else {
  1381. /* Check against the requested. */
  1382. if (sinfo->sinfo_stream >=
  1383. sinit->sinit_num_ostreams) {
  1384. err = -EINVAL;
  1385. goto out_unlock;
  1386. }
  1387. }
  1388. }
  1389. /*
  1390. * API 3.1.2 bind() - UDP Style Syntax
  1391. * If a bind() or sctp_bindx() is not called prior to a
  1392. * sendmsg() call that initiates a new association, the
  1393. * system picks an ephemeral port and will choose an address
  1394. * set equivalent to binding with a wildcard address.
  1395. */
  1396. if (!ep->base.bind_addr.port) {
  1397. if (sctp_autobind(sk)) {
  1398. err = -EAGAIN;
  1399. goto out_unlock;
  1400. }
  1401. } else {
  1402. /*
  1403. * If an unprivileged user inherits a one-to-many
  1404. * style socket with open associations on a privileged
  1405. * port, it MAY be permitted to accept new associations,
  1406. * but it SHOULD NOT be permitted to open new
  1407. * associations.
  1408. */
  1409. if (ep->base.bind_addr.port < PROT_SOCK &&
  1410. !capable(CAP_NET_BIND_SERVICE)) {
  1411. err = -EACCES;
  1412. goto out_unlock;
  1413. }
  1414. }
  1415. scope = sctp_scope(&to);
  1416. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1417. if (!new_asoc) {
  1418. err = -ENOMEM;
  1419. goto out_unlock;
  1420. }
  1421. asoc = new_asoc;
  1422. /* If the SCTP_INIT ancillary data is specified, set all
  1423. * the association init values accordingly.
  1424. */
  1425. if (sinit) {
  1426. if (sinit->sinit_num_ostreams) {
  1427. asoc->c.sinit_num_ostreams =
  1428. sinit->sinit_num_ostreams;
  1429. }
  1430. if (sinit->sinit_max_instreams) {
  1431. asoc->c.sinit_max_instreams =
  1432. sinit->sinit_max_instreams;
  1433. }
  1434. if (sinit->sinit_max_attempts) {
  1435. asoc->max_init_attempts
  1436. = sinit->sinit_max_attempts;
  1437. }
  1438. if (sinit->sinit_max_init_timeo) {
  1439. asoc->max_init_timeo =
  1440. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1441. }
  1442. }
  1443. /* Prime the peer's transport structures. */
  1444. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1445. if (!transport) {
  1446. err = -ENOMEM;
  1447. goto out_free;
  1448. }
  1449. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1450. if (err < 0) {
  1451. err = -ENOMEM;
  1452. goto out_free;
  1453. }
  1454. }
  1455. /* ASSERT: we have a valid association at this point. */
  1456. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1457. if (!sinfo) {
  1458. /* If the user didn't specify SNDRCVINFO, make up one with
  1459. * some defaults.
  1460. */
  1461. default_sinfo.sinfo_stream = asoc->default_stream;
  1462. default_sinfo.sinfo_flags = asoc->default_flags;
  1463. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1464. default_sinfo.sinfo_context = asoc->default_context;
  1465. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1466. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1467. sinfo = &default_sinfo;
  1468. }
  1469. /* API 7.1.7, the sndbuf size per association bounds the
  1470. * maximum size of data that can be sent in a single send call.
  1471. */
  1472. if (msg_len > sk->sk_sndbuf) {
  1473. err = -EMSGSIZE;
  1474. goto out_free;
  1475. }
  1476. /* If fragmentation is disabled and the message length exceeds the
  1477. * association fragmentation point, return EMSGSIZE. The I-D
  1478. * does not specify what this error is, but this looks like
  1479. * a great fit.
  1480. */
  1481. if (sctp_sk(sk)->disable_fragments && (msg_len > asoc->frag_point)) {
  1482. err = -EMSGSIZE;
  1483. goto out_free;
  1484. }
  1485. if (sinfo) {
  1486. /* Check for invalid stream. */
  1487. if (sinfo->sinfo_stream >= asoc->c.sinit_num_ostreams) {
  1488. err = -EINVAL;
  1489. goto out_free;
  1490. }
  1491. }
  1492. timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  1493. if (!sctp_wspace(asoc)) {
  1494. err = sctp_wait_for_sndbuf(asoc, &timeo, msg_len);
  1495. if (err)
  1496. goto out_free;
  1497. }
  1498. /* If an address is passed with the sendto/sendmsg call, it is used
  1499. * to override the primary destination address in the TCP model, or
  1500. * when SCTP_ADDR_OVER flag is set in the UDP model.
  1501. */
  1502. if ((sctp_style(sk, TCP) && msg_name) ||
  1503. (sinfo_flags & SCTP_ADDR_OVER)) {
  1504. chunk_tp = sctp_assoc_lookup_paddr(asoc, &to);
  1505. if (!chunk_tp) {
  1506. err = -EINVAL;
  1507. goto out_free;
  1508. }
  1509. } else
  1510. chunk_tp = NULL;
  1511. /* Auto-connect, if we aren't connected already. */
  1512. if (sctp_state(asoc, CLOSED)) {
  1513. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  1514. if (err < 0)
  1515. goto out_free;
  1516. SCTP_DEBUG_PRINTK("We associated primitively.\n");
  1517. }
  1518. /* Break the message into multiple chunks of maximum size. */
  1519. datamsg = sctp_datamsg_from_user(asoc, sinfo, msg, msg_len);
  1520. if (!datamsg) {
  1521. err = -ENOMEM;
  1522. goto out_free;
  1523. }
  1524. /* Now send the (possibly) fragmented message. */
  1525. list_for_each(pos, &datamsg->chunks) {
  1526. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  1527. sctp_datamsg_track(chunk);
  1528. /* Do accounting for the write space. */
  1529. sctp_set_owner_w(chunk);
  1530. chunk->transport = chunk_tp;
  1531. /* Send it to the lower layers. Note: all chunks
  1532. * must either fail or succeed. The lower layer
  1533. * works that way today. Keep it that way or this
  1534. * breaks.
  1535. */
  1536. err = sctp_primitive_SEND(asoc, chunk);
  1537. /* Did the lower layer accept the chunk? */
  1538. if (err)
  1539. sctp_chunk_free(chunk);
  1540. SCTP_DEBUG_PRINTK("We sent primitively.\n");
  1541. }
  1542. sctp_datamsg_free(datamsg);
  1543. if (err)
  1544. goto out_free;
  1545. else
  1546. err = msg_len;
  1547. /* If we are already past ASSOCIATE, the lower
  1548. * layers are responsible for association cleanup.
  1549. */
  1550. goto out_unlock;
  1551. out_free:
  1552. if (new_asoc)
  1553. sctp_association_free(asoc);
  1554. out_unlock:
  1555. sctp_release_sock(sk);
  1556. out_nounlock:
  1557. return sctp_error(sk, msg_flags, err);
  1558. #if 0
  1559. do_sock_err:
  1560. if (msg_len)
  1561. err = msg_len;
  1562. else
  1563. err = sock_error(sk);
  1564. goto out;
  1565. do_interrupted:
  1566. if (msg_len)
  1567. err = msg_len;
  1568. goto out;
  1569. #endif /* 0 */
  1570. }
  1571. /* This is an extended version of skb_pull() that removes the data from the
  1572. * start of a skb even when data is spread across the list of skb's in the
  1573. * frag_list. len specifies the total amount of data that needs to be removed.
  1574. * when 'len' bytes could be removed from the skb, it returns 0.
  1575. * If 'len' exceeds the total skb length, it returns the no. of bytes that
  1576. * could not be removed.
  1577. */
  1578. static int sctp_skb_pull(struct sk_buff *skb, int len)
  1579. {
  1580. struct sk_buff *list;
  1581. int skb_len = skb_headlen(skb);
  1582. int rlen;
  1583. if (len <= skb_len) {
  1584. __skb_pull(skb, len);
  1585. return 0;
  1586. }
  1587. len -= skb_len;
  1588. __skb_pull(skb, skb_len);
  1589. for (list = skb_shinfo(skb)->frag_list; list; list = list->next) {
  1590. rlen = sctp_skb_pull(list, len);
  1591. skb->len -= (len-rlen);
  1592. skb->data_len -= (len-rlen);
  1593. if (!rlen)
  1594. return 0;
  1595. len = rlen;
  1596. }
  1597. return len;
  1598. }
  1599. /* API 3.1.3 recvmsg() - UDP Style Syntax
  1600. *
  1601. * ssize_t recvmsg(int socket, struct msghdr *message,
  1602. * int flags);
  1603. *
  1604. * socket - the socket descriptor of the endpoint.
  1605. * message - pointer to the msghdr structure which contains a single
  1606. * user message and possibly some ancillary data.
  1607. *
  1608. * See Section 5 for complete description of the data
  1609. * structures.
  1610. *
  1611. * flags - flags sent or received with the user message, see Section
  1612. * 5 for complete description of the flags.
  1613. */
  1614. static struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
  1615. SCTP_STATIC int sctp_recvmsg(struct kiocb *iocb, struct sock *sk,
  1616. struct msghdr *msg, size_t len, int noblock,
  1617. int flags, int *addr_len)
  1618. {
  1619. struct sctp_ulpevent *event = NULL;
  1620. struct sctp_sock *sp = sctp_sk(sk);
  1621. struct sk_buff *skb;
  1622. int copied;
  1623. int err = 0;
  1624. int skb_len;
  1625. SCTP_DEBUG_PRINTK("sctp_recvmsg(%s: %p, %s: %p, %s: %zd, %s: %d, %s: "
  1626. "0x%x, %s: %p)\n", "sk", sk, "msghdr", msg,
  1627. "len", len, "knoblauch", noblock,
  1628. "flags", flags, "addr_len", addr_len);
  1629. sctp_lock_sock(sk);
  1630. if (sctp_style(sk, TCP) && !sctp_sstate(sk, ESTABLISHED)) {
  1631. err = -ENOTCONN;
  1632. goto out;
  1633. }
  1634. skb = sctp_skb_recv_datagram(sk, flags, noblock, &err);
  1635. if (!skb)
  1636. goto out;
  1637. /* Get the total length of the skb including any skb's in the
  1638. * frag_list.
  1639. */
  1640. skb_len = skb->len;
  1641. copied = skb_len;
  1642. if (copied > len)
  1643. copied = len;
  1644. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1645. event = sctp_skb2event(skb);
  1646. if (err)
  1647. goto out_free;
  1648. sock_recv_timestamp(msg, sk, skb);
  1649. if (sctp_ulpevent_is_notification(event)) {
  1650. msg->msg_flags |= MSG_NOTIFICATION;
  1651. sp->pf->event_msgname(event, msg->msg_name, addr_len);
  1652. } else {
  1653. sp->pf->skb_msgname(skb, msg->msg_name, addr_len);
  1654. }
  1655. /* Check if we allow SCTP_SNDRCVINFO. */
  1656. if (sp->subscribe.sctp_data_io_event)
  1657. sctp_ulpevent_read_sndrcvinfo(event, msg);
  1658. #if 0
  1659. /* FIXME: we should be calling IP/IPv6 layers. */
  1660. if (sk->sk_protinfo.af_inet.cmsg_flags)
  1661. ip_cmsg_recv(msg, skb);
  1662. #endif
  1663. err = copied;
  1664. /* If skb's length exceeds the user's buffer, update the skb and
  1665. * push it back to the receive_queue so that the next call to
  1666. * recvmsg() will return the remaining data. Don't set MSG_EOR.
  1667. */
  1668. if (skb_len > copied) {
  1669. msg->msg_flags &= ~MSG_EOR;
  1670. if (flags & MSG_PEEK)
  1671. goto out_free;
  1672. sctp_skb_pull(skb, copied);
  1673. skb_queue_head(&sk->sk_receive_queue, skb);
  1674. /* When only partial message is copied to the user, increase
  1675. * rwnd by that amount. If all the data in the skb is read,
  1676. * rwnd is updated when the event is freed.
  1677. */
  1678. sctp_assoc_rwnd_increase(event->asoc, copied);
  1679. goto out;
  1680. } else if ((event->msg_flags & MSG_NOTIFICATION) ||
  1681. (event->msg_flags & MSG_EOR))
  1682. msg->msg_flags |= MSG_EOR;
  1683. else
  1684. msg->msg_flags &= ~MSG_EOR;
  1685. out_free:
  1686. if (flags & MSG_PEEK) {
  1687. /* Release the skb reference acquired after peeking the skb in
  1688. * sctp_skb_recv_datagram().
  1689. */
  1690. kfree_skb(skb);
  1691. } else {
  1692. /* Free the event which includes releasing the reference to
  1693. * the owner of the skb, freeing the skb and updating the
  1694. * rwnd.
  1695. */
  1696. sctp_ulpevent_free(event);
  1697. }
  1698. out:
  1699. sctp_release_sock(sk);
  1700. return err;
  1701. }
  1702. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  1703. *
  1704. * This option is a on/off flag. If enabled no SCTP message
  1705. * fragmentation will be performed. Instead if a message being sent
  1706. * exceeds the current PMTU size, the message will NOT be sent and
  1707. * instead a error will be indicated to the user.
  1708. */
  1709. static int sctp_setsockopt_disable_fragments(struct sock *sk,
  1710. char __user *optval, int optlen)
  1711. {
  1712. int val;
  1713. if (optlen < sizeof(int))
  1714. return -EINVAL;
  1715. if (get_user(val, (int __user *)optval))
  1716. return -EFAULT;
  1717. sctp_sk(sk)->disable_fragments = (val == 0) ? 0 : 1;
  1718. return 0;
  1719. }
  1720. static int sctp_setsockopt_events(struct sock *sk, char __user *optval,
  1721. int optlen)
  1722. {
  1723. if (optlen != sizeof(struct sctp_event_subscribe))
  1724. return -EINVAL;
  1725. if (copy_from_user(&sctp_sk(sk)->subscribe, optval, optlen))
  1726. return -EFAULT;
  1727. return 0;
  1728. }
  1729. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  1730. *
  1731. * This socket option is applicable to the UDP-style socket only. When
  1732. * set it will cause associations that are idle for more than the
  1733. * specified number of seconds to automatically close. An association
  1734. * being idle is defined an association that has NOT sent or received
  1735. * user data. The special value of '0' indicates that no automatic
  1736. * close of any associations should be performed. The option expects an
  1737. * integer defining the number of seconds of idle time before an
  1738. * association is closed.
  1739. */
  1740. static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
  1741. int optlen)
  1742. {
  1743. struct sctp_sock *sp = sctp_sk(sk);
  1744. /* Applicable to UDP-style socket only */
  1745. if (sctp_style(sk, TCP))
  1746. return -EOPNOTSUPP;
  1747. if (optlen != sizeof(int))
  1748. return -EINVAL;
  1749. if (copy_from_user(&sp->autoclose, optval, optlen))
  1750. return -EFAULT;
  1751. return 0;
  1752. }
  1753. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  1754. *
  1755. * Applications can enable or disable heartbeats for any peer address of
  1756. * an association, modify an address's heartbeat interval, force a
  1757. * heartbeat to be sent immediately, and adjust the address's maximum
  1758. * number of retransmissions sent before an address is considered
  1759. * unreachable. The following structure is used to access and modify an
  1760. * address's parameters:
  1761. *
  1762. * struct sctp_paddrparams {
  1763. * sctp_assoc_t spp_assoc_id;
  1764. * struct sockaddr_storage spp_address;
  1765. * uint32_t spp_hbinterval;
  1766. * uint16_t spp_pathmaxrxt;
  1767. * uint32_t spp_pathmtu;
  1768. * uint32_t spp_sackdelay;
  1769. * uint32_t spp_flags;
  1770. * };
  1771. *
  1772. * spp_assoc_id - (one-to-many style socket) This is filled in the
  1773. * application, and identifies the association for
  1774. * this query.
  1775. * spp_address - This specifies which address is of interest.
  1776. * spp_hbinterval - This contains the value of the heartbeat interval,
  1777. * in milliseconds. If a value of zero
  1778. * is present in this field then no changes are to
  1779. * be made to this parameter.
  1780. * spp_pathmaxrxt - This contains the maximum number of
  1781. * retransmissions before this address shall be
  1782. * considered unreachable. If a value of zero
  1783. * is present in this field then no changes are to
  1784. * be made to this parameter.
  1785. * spp_pathmtu - When Path MTU discovery is disabled the value
  1786. * specified here will be the "fixed" path mtu.
  1787. * Note that if the spp_address field is empty
  1788. * then all associations on this address will
  1789. * have this fixed path mtu set upon them.
  1790. *
  1791. * spp_sackdelay - When delayed sack is enabled, this value specifies
  1792. * the number of milliseconds that sacks will be delayed
  1793. * for. This value will apply to all addresses of an
  1794. * association if the spp_address field is empty. Note
  1795. * also, that if delayed sack is enabled and this
  1796. * value is set to 0, no change is made to the last
  1797. * recorded delayed sack timer value.
  1798. *
  1799. * spp_flags - These flags are used to control various features
  1800. * on an association. The flag field may contain
  1801. * zero or more of the following options.
  1802. *
  1803. * SPP_HB_ENABLE - Enable heartbeats on the
  1804. * specified address. Note that if the address
  1805. * field is empty all addresses for the association
  1806. * have heartbeats enabled upon them.
  1807. *
  1808. * SPP_HB_DISABLE - Disable heartbeats on the
  1809. * speicifed address. Note that if the address
  1810. * field is empty all addresses for the association
  1811. * will have their heartbeats disabled. Note also
  1812. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  1813. * mutually exclusive, only one of these two should
  1814. * be specified. Enabling both fields will have
  1815. * undetermined results.
  1816. *
  1817. * SPP_HB_DEMAND - Request a user initiated heartbeat
  1818. * to be made immediately.
  1819. *
  1820. * SPP_PMTUD_ENABLE - This field will enable PMTU
  1821. * discovery upon the specified address. Note that
  1822. * if the address feild is empty then all addresses
  1823. * on the association are effected.
  1824. *
  1825. * SPP_PMTUD_DISABLE - This field will disable PMTU
  1826. * discovery upon the specified address. Note that
  1827. * if the address feild is empty then all addresses
  1828. * on the association are effected. Not also that
  1829. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  1830. * exclusive. Enabling both will have undetermined
  1831. * results.
  1832. *
  1833. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  1834. * on delayed sack. The time specified in spp_sackdelay
  1835. * is used to specify the sack delay for this address. Note
  1836. * that if spp_address is empty then all addresses will
  1837. * enable delayed sack and take on the sack delay
  1838. * value specified in spp_sackdelay.
  1839. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  1840. * off delayed sack. If the spp_address field is blank then
  1841. * delayed sack is disabled for the entire association. Note
  1842. * also that this field is mutually exclusive to
  1843. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  1844. * results.
  1845. */
  1846. int sctp_apply_peer_addr_params(struct sctp_paddrparams *params,
  1847. struct sctp_transport *trans,
  1848. struct sctp_association *asoc,
  1849. struct sctp_sock *sp,
  1850. int hb_change,
  1851. int pmtud_change,
  1852. int sackdelay_change)
  1853. {
  1854. int error;
  1855. if (params->spp_flags & SPP_HB_DEMAND && trans) {
  1856. error = sctp_primitive_REQUESTHEARTBEAT (trans->asoc, trans);
  1857. if (error)
  1858. return error;
  1859. }
  1860. if (params->spp_hbinterval) {
  1861. if (trans) {
  1862. trans->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
  1863. } else if (asoc) {
  1864. asoc->hbinterval = msecs_to_jiffies(params->spp_hbinterval);
  1865. } else {
  1866. sp->hbinterval = params->spp_hbinterval;
  1867. }
  1868. }
  1869. if (hb_change) {
  1870. if (trans) {
  1871. trans->param_flags =
  1872. (trans->param_flags & ~SPP_HB) | hb_change;
  1873. } else if (asoc) {
  1874. asoc->param_flags =
  1875. (asoc->param_flags & ~SPP_HB) | hb_change;
  1876. } else {
  1877. sp->param_flags =
  1878. (sp->param_flags & ~SPP_HB) | hb_change;
  1879. }
  1880. }
  1881. if (params->spp_pathmtu) {
  1882. if (trans) {
  1883. trans->pathmtu = params->spp_pathmtu;
  1884. sctp_assoc_sync_pmtu(asoc);
  1885. } else if (asoc) {
  1886. asoc->pathmtu = params->spp_pathmtu;
  1887. sctp_frag_point(sp, params->spp_pathmtu);
  1888. } else {
  1889. sp->pathmtu = params->spp_pathmtu;
  1890. }
  1891. }
  1892. if (pmtud_change) {
  1893. if (trans) {
  1894. int update = (trans->param_flags & SPP_PMTUD_DISABLE) &&
  1895. (params->spp_flags & SPP_PMTUD_ENABLE);
  1896. trans->param_flags =
  1897. (trans->param_flags & ~SPP_PMTUD) | pmtud_change;
  1898. if (update) {
  1899. sctp_transport_pmtu(trans);
  1900. sctp_assoc_sync_pmtu(asoc);
  1901. }
  1902. } else if (asoc) {
  1903. asoc->param_flags =
  1904. (asoc->param_flags & ~SPP_PMTUD) | pmtud_change;
  1905. } else {
  1906. sp->param_flags =
  1907. (sp->param_flags & ~SPP_PMTUD) | pmtud_change;
  1908. }
  1909. }
  1910. if (params->spp_sackdelay) {
  1911. if (trans) {
  1912. trans->sackdelay =
  1913. msecs_to_jiffies(params->spp_sackdelay);
  1914. } else if (asoc) {
  1915. asoc->sackdelay =
  1916. msecs_to_jiffies(params->spp_sackdelay);
  1917. } else {
  1918. sp->sackdelay = params->spp_sackdelay;
  1919. }
  1920. }
  1921. if (sackdelay_change) {
  1922. if (trans) {
  1923. trans->param_flags =
  1924. (trans->param_flags & ~SPP_SACKDELAY) |
  1925. sackdelay_change;
  1926. } else if (asoc) {
  1927. asoc->param_flags =
  1928. (asoc->param_flags & ~SPP_SACKDELAY) |
  1929. sackdelay_change;
  1930. } else {
  1931. sp->param_flags =
  1932. (sp->param_flags & ~SPP_SACKDELAY) |
  1933. sackdelay_change;
  1934. }
  1935. }
  1936. if (params->spp_pathmaxrxt) {
  1937. if (trans) {
  1938. trans->pathmaxrxt = params->spp_pathmaxrxt;
  1939. } else if (asoc) {
  1940. asoc->pathmaxrxt = params->spp_pathmaxrxt;
  1941. } else {
  1942. sp->pathmaxrxt = params->spp_pathmaxrxt;
  1943. }
  1944. }
  1945. return 0;
  1946. }
  1947. static int sctp_setsockopt_peer_addr_params(struct sock *sk,
  1948. char __user *optval, int optlen)
  1949. {
  1950. struct sctp_paddrparams params;
  1951. struct sctp_transport *trans = NULL;
  1952. struct sctp_association *asoc = NULL;
  1953. struct sctp_sock *sp = sctp_sk(sk);
  1954. int error;
  1955. int hb_change, pmtud_change, sackdelay_change;
  1956. if (optlen != sizeof(struct sctp_paddrparams))
  1957. return - EINVAL;
  1958. if (copy_from_user(&params, optval, optlen))
  1959. return -EFAULT;
  1960. /* Validate flags and value parameters. */
  1961. hb_change = params.spp_flags & SPP_HB;
  1962. pmtud_change = params.spp_flags & SPP_PMTUD;
  1963. sackdelay_change = params.spp_flags & SPP_SACKDELAY;
  1964. if (hb_change == SPP_HB ||
  1965. pmtud_change == SPP_PMTUD ||
  1966. sackdelay_change == SPP_SACKDELAY ||
  1967. params.spp_sackdelay > 500 ||
  1968. (params.spp_pathmtu
  1969. && params.spp_pathmtu < SCTP_DEFAULT_MINSEGMENT))
  1970. return -EINVAL;
  1971. /* If an address other than INADDR_ANY is specified, and
  1972. * no transport is found, then the request is invalid.
  1973. */
  1974. if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  1975. trans = sctp_addr_id2transport(sk, &params.spp_address,
  1976. params.spp_assoc_id);
  1977. if (!trans)
  1978. return -EINVAL;
  1979. }
  1980. /* Get association, if assoc_id != 0 and the socket is a one
  1981. * to many style socket, and an association was not found, then
  1982. * the id was invalid.
  1983. */
  1984. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  1985. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP))
  1986. return -EINVAL;
  1987. /* Heartbeat demand can only be sent on a transport or
  1988. * association, but not a socket.
  1989. */
  1990. if (params.spp_flags & SPP_HB_DEMAND && !trans && !asoc)
  1991. return -EINVAL;
  1992. /* Process parameters. */
  1993. error = sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  1994. hb_change, pmtud_change,
  1995. sackdelay_change);
  1996. if (error)
  1997. return error;
  1998. /* If changes are for association, also apply parameters to each
  1999. * transport.
  2000. */
  2001. if (!trans && asoc) {
  2002. struct list_head *pos;
  2003. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2004. trans = list_entry(pos, struct sctp_transport,
  2005. transports);
  2006. sctp_apply_peer_addr_params(&params, trans, asoc, sp,
  2007. hb_change, pmtud_change,
  2008. sackdelay_change);
  2009. }
  2010. }
  2011. return 0;
  2012. }
  2013. /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
  2014. *
  2015. * This options will get or set the delayed ack timer. The time is set
  2016. * in milliseconds. If the assoc_id is 0, then this sets or gets the
  2017. * endpoints default delayed ack timer value. If the assoc_id field is
  2018. * non-zero, then the set or get effects the specified association.
  2019. *
  2020. * struct sctp_assoc_value {
  2021. * sctp_assoc_t assoc_id;
  2022. * uint32_t assoc_value;
  2023. * };
  2024. *
  2025. * assoc_id - This parameter, indicates which association the
  2026. * user is preforming an action upon. Note that if
  2027. * this field's value is zero then the endpoints
  2028. * default value is changed (effecting future
  2029. * associations only).
  2030. *
  2031. * assoc_value - This parameter contains the number of milliseconds
  2032. * that the user is requesting the delayed ACK timer
  2033. * be set to. Note that this value is defined in
  2034. * the standard to be between 200 and 500 milliseconds.
  2035. *
  2036. * Note: a value of zero will leave the value alone,
  2037. * but disable SACK delay. A non-zero value will also
  2038. * enable SACK delay.
  2039. */
  2040. static int sctp_setsockopt_delayed_ack_time(struct sock *sk,
  2041. char __user *optval, int optlen)
  2042. {
  2043. struct sctp_assoc_value params;
  2044. struct sctp_transport *trans = NULL;
  2045. struct sctp_association *asoc = NULL;
  2046. struct sctp_sock *sp = sctp_sk(sk);
  2047. if (optlen != sizeof(struct sctp_assoc_value))
  2048. return - EINVAL;
  2049. if (copy_from_user(&params, optval, optlen))
  2050. return -EFAULT;
  2051. /* Validate value parameter. */
  2052. if (params.assoc_value > 500)
  2053. return -EINVAL;
  2054. /* Get association, if assoc_id != 0 and the socket is a one
  2055. * to many style socket, and an association was not found, then
  2056. * the id was invalid.
  2057. */
  2058. asoc = sctp_id2assoc(sk, params.assoc_id);
  2059. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  2060. return -EINVAL;
  2061. if (params.assoc_value) {
  2062. if (asoc) {
  2063. asoc->sackdelay =
  2064. msecs_to_jiffies(params.assoc_value);
  2065. asoc->param_flags =
  2066. (asoc->param_flags & ~SPP_SACKDELAY) |
  2067. SPP_SACKDELAY_ENABLE;
  2068. } else {
  2069. sp->sackdelay = params.assoc_value;
  2070. sp->param_flags =
  2071. (sp->param_flags & ~SPP_SACKDELAY) |
  2072. SPP_SACKDELAY_ENABLE;
  2073. }
  2074. } else {
  2075. if (asoc) {
  2076. asoc->param_flags =
  2077. (asoc->param_flags & ~SPP_SACKDELAY) |
  2078. SPP_SACKDELAY_DISABLE;
  2079. } else {
  2080. sp->param_flags =
  2081. (sp->param_flags & ~SPP_SACKDELAY) |
  2082. SPP_SACKDELAY_DISABLE;
  2083. }
  2084. }
  2085. /* If change is for association, also apply to each transport. */
  2086. if (asoc) {
  2087. struct list_head *pos;
  2088. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2089. trans = list_entry(pos, struct sctp_transport,
  2090. transports);
  2091. if (params.assoc_value) {
  2092. trans->sackdelay =
  2093. msecs_to_jiffies(params.assoc_value);
  2094. trans->param_flags =
  2095. (trans->param_flags & ~SPP_SACKDELAY) |
  2096. SPP_SACKDELAY_ENABLE;
  2097. } else {
  2098. trans->param_flags =
  2099. (trans->param_flags & ~SPP_SACKDELAY) |
  2100. SPP_SACKDELAY_DISABLE;
  2101. }
  2102. }
  2103. }
  2104. return 0;
  2105. }
  2106. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  2107. *
  2108. * Applications can specify protocol parameters for the default association
  2109. * initialization. The option name argument to setsockopt() and getsockopt()
  2110. * is SCTP_INITMSG.
  2111. *
  2112. * Setting initialization parameters is effective only on an unconnected
  2113. * socket (for UDP-style sockets only future associations are effected
  2114. * by the change). With TCP-style sockets, this option is inherited by
  2115. * sockets derived from a listener socket.
  2116. */
  2117. static int sctp_setsockopt_initmsg(struct sock *sk, char __user *optval, int optlen)
  2118. {
  2119. struct sctp_initmsg sinit;
  2120. struct sctp_sock *sp = sctp_sk(sk);
  2121. if (optlen != sizeof(struct sctp_initmsg))
  2122. return -EINVAL;
  2123. if (copy_from_user(&sinit, optval, optlen))
  2124. return -EFAULT;
  2125. if (sinit.sinit_num_ostreams)
  2126. sp->initmsg.sinit_num_ostreams = sinit.sinit_num_ostreams;
  2127. if (sinit.sinit_max_instreams)
  2128. sp->initmsg.sinit_max_instreams = sinit.sinit_max_instreams;
  2129. if (sinit.sinit_max_attempts)
  2130. sp->initmsg.sinit_max_attempts = sinit.sinit_max_attempts;
  2131. if (sinit.sinit_max_init_timeo)
  2132. sp->initmsg.sinit_max_init_timeo = sinit.sinit_max_init_timeo;
  2133. return 0;
  2134. }
  2135. /*
  2136. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  2137. *
  2138. * Applications that wish to use the sendto() system call may wish to
  2139. * specify a default set of parameters that would normally be supplied
  2140. * through the inclusion of ancillary data. This socket option allows
  2141. * such an application to set the default sctp_sndrcvinfo structure.
  2142. * The application that wishes to use this socket option simply passes
  2143. * in to this call the sctp_sndrcvinfo structure defined in Section
  2144. * 5.2.2) The input parameters accepted by this call include
  2145. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  2146. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  2147. * to this call if the caller is using the UDP model.
  2148. */
  2149. static int sctp_setsockopt_default_send_param(struct sock *sk,
  2150. char __user *optval, int optlen)
  2151. {
  2152. struct sctp_sndrcvinfo info;
  2153. struct sctp_association *asoc;
  2154. struct sctp_sock *sp = sctp_sk(sk);
  2155. if (optlen != sizeof(struct sctp_sndrcvinfo))
  2156. return -EINVAL;
  2157. if (copy_from_user(&info, optval, optlen))
  2158. return -EFAULT;
  2159. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  2160. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  2161. return -EINVAL;
  2162. if (asoc) {
  2163. asoc->default_stream = info.sinfo_stream;
  2164. asoc->default_flags = info.sinfo_flags;
  2165. asoc->default_ppid = info.sinfo_ppid;
  2166. asoc->default_context = info.sinfo_context;
  2167. asoc->default_timetolive = info.sinfo_timetolive;
  2168. } else {
  2169. sp->default_stream = info.sinfo_stream;
  2170. sp->default_flags = info.sinfo_flags;
  2171. sp->default_ppid = info.sinfo_ppid;
  2172. sp->default_context = info.sinfo_context;
  2173. sp->default_timetolive = info.sinfo_timetolive;
  2174. }
  2175. return 0;
  2176. }
  2177. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  2178. *
  2179. * Requests that the local SCTP stack use the enclosed peer address as
  2180. * the association primary. The enclosed address must be one of the
  2181. * association peer's addresses.
  2182. */
  2183. static int sctp_setsockopt_primary_addr(struct sock *sk, char __user *optval,
  2184. int optlen)
  2185. {
  2186. struct sctp_prim prim;
  2187. struct sctp_transport *trans;
  2188. if (optlen != sizeof(struct sctp_prim))
  2189. return -EINVAL;
  2190. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  2191. return -EFAULT;
  2192. trans = sctp_addr_id2transport(sk, &prim.ssp_addr, prim.ssp_assoc_id);
  2193. if (!trans)
  2194. return -EINVAL;
  2195. sctp_assoc_set_primary(trans->asoc, trans);
  2196. return 0;
  2197. }
  2198. /*
  2199. * 7.1.5 SCTP_NODELAY
  2200. *
  2201. * Turn on/off any Nagle-like algorithm. This means that packets are
  2202. * generally sent as soon as possible and no unnecessary delays are
  2203. * introduced, at the cost of more packets in the network. Expects an
  2204. * integer boolean flag.
  2205. */
  2206. static int sctp_setsockopt_nodelay(struct sock *sk, char __user *optval,
  2207. int optlen)
  2208. {
  2209. int val;
  2210. if (optlen < sizeof(int))
  2211. return -EINVAL;
  2212. if (get_user(val, (int __user *)optval))
  2213. return -EFAULT;
  2214. sctp_sk(sk)->nodelay = (val == 0) ? 0 : 1;
  2215. return 0;
  2216. }
  2217. /*
  2218. *
  2219. * 7.1.1 SCTP_RTOINFO
  2220. *
  2221. * The protocol parameters used to initialize and bound retransmission
  2222. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  2223. * and modify these parameters.
  2224. * All parameters are time values, in milliseconds. A value of 0, when
  2225. * modifying the parameters, indicates that the current value should not
  2226. * be changed.
  2227. *
  2228. */
  2229. static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, int optlen) {
  2230. struct sctp_rtoinfo rtoinfo;
  2231. struct sctp_association *asoc;
  2232. if (optlen != sizeof (struct sctp_rtoinfo))
  2233. return -EINVAL;
  2234. if (copy_from_user(&rtoinfo, optval, optlen))
  2235. return -EFAULT;
  2236. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  2237. /* Set the values to the specific association */
  2238. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  2239. return -EINVAL;
  2240. if (asoc) {
  2241. if (rtoinfo.srto_initial != 0)
  2242. asoc->rto_initial =
  2243. msecs_to_jiffies(rtoinfo.srto_initial);
  2244. if (rtoinfo.srto_max != 0)
  2245. asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max);
  2246. if (rtoinfo.srto_min != 0)
  2247. asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
  2248. } else {
  2249. /* If there is no association or the association-id = 0
  2250. * set the values to the endpoint.
  2251. */
  2252. struct sctp_sock *sp = sctp_sk(sk);
  2253. if (rtoinfo.srto_initial != 0)
  2254. sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
  2255. if (rtoinfo.srto_max != 0)
  2256. sp->rtoinfo.srto_max = rtoinfo.srto_max;
  2257. if (rtoinfo.srto_min != 0)
  2258. sp->rtoinfo.srto_min = rtoinfo.srto_min;
  2259. }
  2260. return 0;
  2261. }
  2262. /*
  2263. *
  2264. * 7.1.2 SCTP_ASSOCINFO
  2265. *
  2266. * This option is used to tune the the maximum retransmission attempts
  2267. * of the association.
  2268. * Returns an error if the new association retransmission value is
  2269. * greater than the sum of the retransmission value of the peer.
  2270. * See [SCTP] for more information.
  2271. *
  2272. */
  2273. static int sctp_setsockopt_associnfo(struct sock *sk, char __user *optval, int optlen)
  2274. {
  2275. struct sctp_assocparams assocparams;
  2276. struct sctp_association *asoc;
  2277. if (optlen != sizeof(struct sctp_assocparams))
  2278. return -EINVAL;
  2279. if (copy_from_user(&assocparams, optval, optlen))
  2280. return -EFAULT;
  2281. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  2282. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  2283. return -EINVAL;
  2284. /* Set the values to the specific association */
  2285. if (asoc) {
  2286. if (assocparams.sasoc_asocmaxrxt != 0) {
  2287. __u32 path_sum = 0;
  2288. int paths = 0;
  2289. struct list_head *pos;
  2290. struct sctp_transport *peer_addr;
  2291. list_for_each(pos, &asoc->peer.transport_addr_list) {
  2292. peer_addr = list_entry(pos,
  2293. struct sctp_transport,
  2294. transports);
  2295. path_sum += peer_addr->pathmaxrxt;
  2296. paths++;
  2297. }
  2298. /* Only validate asocmaxrxt if we have more then
  2299. * one path/transport. We do this because path
  2300. * retransmissions are only counted when we have more
  2301. * then one path.
  2302. */
  2303. if (paths > 1 &&
  2304. assocparams.sasoc_asocmaxrxt > path_sum)
  2305. return -EINVAL;
  2306. asoc->max_retrans = assocparams.sasoc_asocmaxrxt;
  2307. }
  2308. if (assocparams.sasoc_cookie_life != 0) {
  2309. asoc->cookie_life.tv_sec =
  2310. assocparams.sasoc_cookie_life / 1000;
  2311. asoc->cookie_life.tv_usec =
  2312. (assocparams.sasoc_cookie_life % 1000)
  2313. * 1000;
  2314. }
  2315. } else {
  2316. /* Set the values to the endpoint */
  2317. struct sctp_sock *sp = sctp_sk(sk);
  2318. if (assocparams.sasoc_asocmaxrxt != 0)
  2319. sp->assocparams.sasoc_asocmaxrxt =
  2320. assocparams.sasoc_asocmaxrxt;
  2321. if (assocparams.sasoc_cookie_life != 0)
  2322. sp->assocparams.sasoc_cookie_life =
  2323. assocparams.sasoc_cookie_life;
  2324. }
  2325. return 0;
  2326. }
  2327. /*
  2328. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  2329. *
  2330. * This socket option is a boolean flag which turns on or off mapped V4
  2331. * addresses. If this option is turned on and the socket is type
  2332. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  2333. * If this option is turned off, then no mapping will be done of V4
  2334. * addresses and a user will receive both PF_INET6 and PF_INET type
  2335. * addresses on the socket.
  2336. */
  2337. static int sctp_setsockopt_mappedv4(struct sock *sk, char __user *optval, int optlen)
  2338. {
  2339. int val;
  2340. struct sctp_sock *sp = sctp_sk(sk);
  2341. if (optlen < sizeof(int))
  2342. return -EINVAL;
  2343. if (get_user(val, (int __user *)optval))
  2344. return -EFAULT;
  2345. if (val)
  2346. sp->v4mapped = 1;
  2347. else
  2348. sp->v4mapped = 0;
  2349. return 0;
  2350. }
  2351. /*
  2352. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  2353. *
  2354. * This socket option specifies the maximum size to put in any outgoing
  2355. * SCTP chunk. If a message is larger than this size it will be
  2356. * fragmented by SCTP into the specified size. Note that the underlying
  2357. * SCTP implementation may fragment into smaller sized chunks when the
  2358. * PMTU of the underlying association is smaller than the value set by
  2359. * the user.
  2360. */
  2361. static int sctp_setsockopt_maxseg(struct sock *sk, char __user *optval, int optlen)
  2362. {
  2363. struct sctp_association *asoc;
  2364. struct list_head *pos;
  2365. struct sctp_sock *sp = sctp_sk(sk);
  2366. int val;
  2367. if (optlen < sizeof(int))
  2368. return -EINVAL;
  2369. if (get_user(val, (int __user *)optval))
  2370. return -EFAULT;
  2371. if ((val != 0) && ((val < 8) || (val > SCTP_MAX_CHUNK_LEN)))
  2372. return -EINVAL;
  2373. sp->user_frag = val;
  2374. /* Update the frag_point of the existing associations. */
  2375. list_for_each(pos, &(sp->ep->asocs)) {
  2376. asoc = list_entry(pos, struct sctp_association, asocs);
  2377. asoc->frag_point = sctp_frag_point(sp, asoc->pathmtu);
  2378. }
  2379. return 0;
  2380. }
  2381. /*
  2382. * 7.1.9 Set Peer Primary Address (SCTP_SET_PEER_PRIMARY_ADDR)
  2383. *
  2384. * Requests that the peer mark the enclosed address as the association
  2385. * primary. The enclosed address must be one of the association's
  2386. * locally bound addresses. The following structure is used to make a
  2387. * set primary request:
  2388. */
  2389. static int sctp_setsockopt_peer_primary_addr(struct sock *sk, char __user *optval,
  2390. int optlen)
  2391. {
  2392. struct sctp_sock *sp;
  2393. struct sctp_endpoint *ep;
  2394. struct sctp_association *asoc = NULL;
  2395. struct sctp_setpeerprim prim;
  2396. struct sctp_chunk *chunk;
  2397. int err;
  2398. sp = sctp_sk(sk);
  2399. ep = sp->ep;
  2400. if (!sctp_addip_enable)
  2401. return -EPERM;
  2402. if (optlen != sizeof(struct sctp_setpeerprim))
  2403. return -EINVAL;
  2404. if (copy_from_user(&prim, optval, optlen))
  2405. return -EFAULT;
  2406. asoc = sctp_id2assoc(sk, prim.sspp_assoc_id);
  2407. if (!asoc)
  2408. return -EINVAL;
  2409. if (!asoc->peer.asconf_capable)
  2410. return -EPERM;
  2411. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_SET_PRIMARY)
  2412. return -EPERM;
  2413. if (!sctp_state(asoc, ESTABLISHED))
  2414. return -ENOTCONN;
  2415. if (!sctp_assoc_lookup_laddr(asoc, (union sctp_addr *)&prim.sspp_addr))
  2416. return -EADDRNOTAVAIL;
  2417. /* Create an ASCONF chunk with SET_PRIMARY parameter */
  2418. chunk = sctp_make_asconf_set_prim(asoc,
  2419. (union sctp_addr *)&prim.sspp_addr);
  2420. if (!chunk)
  2421. return -ENOMEM;
  2422. err = sctp_send_asconf(asoc, chunk);
  2423. SCTP_DEBUG_PRINTK("We set peer primary addr primitively.\n");
  2424. return err;
  2425. }
  2426. static int sctp_setsockopt_adaption_layer(struct sock *sk, char __user *optval,
  2427. int optlen)
  2428. {
  2429. struct sctp_setadaption adaption;
  2430. if (optlen != sizeof(struct sctp_setadaption))
  2431. return -EINVAL;
  2432. if (copy_from_user(&adaption, optval, optlen))
  2433. return -EFAULT;
  2434. sctp_sk(sk)->adaption_ind = adaption.ssb_adaption_ind;
  2435. return 0;
  2436. }
  2437. /* API 6.2 setsockopt(), getsockopt()
  2438. *
  2439. * Applications use setsockopt() and getsockopt() to set or retrieve
  2440. * socket options. Socket options are used to change the default
  2441. * behavior of sockets calls. They are described in Section 7.
  2442. *
  2443. * The syntax is:
  2444. *
  2445. * ret = getsockopt(int sd, int level, int optname, void __user *optval,
  2446. * int __user *optlen);
  2447. * ret = setsockopt(int sd, int level, int optname, const void __user *optval,
  2448. * int optlen);
  2449. *
  2450. * sd - the socket descript.
  2451. * level - set to IPPROTO_SCTP for all SCTP options.
  2452. * optname - the option name.
  2453. * optval - the buffer to store the value of the option.
  2454. * optlen - the size of the buffer.
  2455. */
  2456. SCTP_STATIC int sctp_setsockopt(struct sock *sk, int level, int optname,
  2457. char __user *optval, int optlen)
  2458. {
  2459. int retval = 0;
  2460. SCTP_DEBUG_PRINTK("sctp_setsockopt(sk: %p... optname: %d)\n",
  2461. sk, optname);
  2462. /* I can hardly begin to describe how wrong this is. This is
  2463. * so broken as to be worse than useless. The API draft
  2464. * REALLY is NOT helpful here... I am not convinced that the
  2465. * semantics of setsockopt() with a level OTHER THAN SOL_SCTP
  2466. * are at all well-founded.
  2467. */
  2468. if (level != SOL_SCTP) {
  2469. struct sctp_af *af = sctp_sk(sk)->pf->af;
  2470. retval = af->setsockopt(sk, level, optname, optval, optlen);
  2471. goto out_nounlock;
  2472. }
  2473. sctp_lock_sock(sk);
  2474. switch (optname) {
  2475. case SCTP_SOCKOPT_BINDX_ADD:
  2476. /* 'optlen' is the size of the addresses buffer. */
  2477. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2478. optlen, SCTP_BINDX_ADD_ADDR);
  2479. break;
  2480. case SCTP_SOCKOPT_BINDX_REM:
  2481. /* 'optlen' is the size of the addresses buffer. */
  2482. retval = sctp_setsockopt_bindx(sk, (struct sockaddr __user *)optval,
  2483. optlen, SCTP_BINDX_REM_ADDR);
  2484. break;
  2485. case SCTP_SOCKOPT_CONNECTX:
  2486. /* 'optlen' is the size of the addresses buffer. */
  2487. retval = sctp_setsockopt_connectx(sk, (struct sockaddr __user *)optval,
  2488. optlen);
  2489. break;
  2490. case SCTP_DISABLE_FRAGMENTS:
  2491. retval = sctp_setsockopt_disable_fragments(sk, optval, optlen);
  2492. break;
  2493. case SCTP_EVENTS:
  2494. retval = sctp_setsockopt_events(sk, optval, optlen);
  2495. break;
  2496. case SCTP_AUTOCLOSE:
  2497. retval = sctp_setsockopt_autoclose(sk, optval, optlen);
  2498. break;
  2499. case SCTP_PEER_ADDR_PARAMS:
  2500. retval = sctp_setsockopt_peer_addr_params(sk, optval, optlen);
  2501. break;
  2502. case SCTP_DELAYED_ACK_TIME:
  2503. retval = sctp_setsockopt_delayed_ack_time(sk, optval, optlen);
  2504. break;
  2505. case SCTP_INITMSG:
  2506. retval = sctp_setsockopt_initmsg(sk, optval, optlen);
  2507. break;
  2508. case SCTP_DEFAULT_SEND_PARAM:
  2509. retval = sctp_setsockopt_default_send_param(sk, optval,
  2510. optlen);
  2511. break;
  2512. case SCTP_PRIMARY_ADDR:
  2513. retval = sctp_setsockopt_primary_addr(sk, optval, optlen);
  2514. break;
  2515. case SCTP_SET_PEER_PRIMARY_ADDR:
  2516. retval = sctp_setsockopt_peer_primary_addr(sk, optval, optlen);
  2517. break;
  2518. case SCTP_NODELAY:
  2519. retval = sctp_setsockopt_nodelay(sk, optval, optlen);
  2520. break;
  2521. case SCTP_RTOINFO:
  2522. retval = sctp_setsockopt_rtoinfo(sk, optval, optlen);
  2523. break;
  2524. case SCTP_ASSOCINFO:
  2525. retval = sctp_setsockopt_associnfo(sk, optval, optlen);
  2526. break;
  2527. case SCTP_I_WANT_MAPPED_V4_ADDR:
  2528. retval = sctp_setsockopt_mappedv4(sk, optval, optlen);
  2529. break;
  2530. case SCTP_MAXSEG:
  2531. retval = sctp_setsockopt_maxseg(sk, optval, optlen);
  2532. break;
  2533. case SCTP_ADAPTION_LAYER:
  2534. retval = sctp_setsockopt_adaption_layer(sk, optval, optlen);
  2535. break;
  2536. default:
  2537. retval = -ENOPROTOOPT;
  2538. break;
  2539. };
  2540. sctp_release_sock(sk);
  2541. out_nounlock:
  2542. return retval;
  2543. }
  2544. /* API 3.1.6 connect() - UDP Style Syntax
  2545. *
  2546. * An application may use the connect() call in the UDP model to initiate an
  2547. * association without sending data.
  2548. *
  2549. * The syntax is:
  2550. *
  2551. * ret = connect(int sd, const struct sockaddr *nam, socklen_t len);
  2552. *
  2553. * sd: the socket descriptor to have a new association added to.
  2554. *
  2555. * nam: the address structure (either struct sockaddr_in or struct
  2556. * sockaddr_in6 defined in RFC2553 [7]).
  2557. *
  2558. * len: the size of the address.
  2559. */
  2560. SCTP_STATIC int sctp_connect(struct sock *sk, struct sockaddr *addr,
  2561. int addr_len)
  2562. {
  2563. int err = 0;
  2564. struct sctp_af *af;
  2565. sctp_lock_sock(sk);
  2566. SCTP_DEBUG_PRINTK("%s - sk: %p, sockaddr: %p, addr_len: %d\n",
  2567. __FUNCTION__, sk, addr, addr_len);
  2568. /* Validate addr_len before calling common connect/connectx routine. */
  2569. af = sctp_get_af_specific(addr->sa_family);
  2570. if (!af || addr_len < af->sockaddr_len) {
  2571. err = -EINVAL;
  2572. } else {
  2573. /* Pass correct addr len to common routine (so it knows there
  2574. * is only one address being passed.
  2575. */
  2576. err = __sctp_connect(sk, addr, af->sockaddr_len);
  2577. }
  2578. sctp_release_sock(sk);
  2579. return err;
  2580. }
  2581. /* FIXME: Write comments. */
  2582. SCTP_STATIC int sctp_disconnect(struct sock *sk, int flags)
  2583. {
  2584. return -EOPNOTSUPP; /* STUB */
  2585. }
  2586. /* 4.1.4 accept() - TCP Style Syntax
  2587. *
  2588. * Applications use accept() call to remove an established SCTP
  2589. * association from the accept queue of the endpoint. A new socket
  2590. * descriptor will be returned from accept() to represent the newly
  2591. * formed association.
  2592. */
  2593. SCTP_STATIC struct sock *sctp_accept(struct sock *sk, int flags, int *err)
  2594. {
  2595. struct sctp_sock *sp;
  2596. struct sctp_endpoint *ep;
  2597. struct sock *newsk = NULL;
  2598. struct sctp_association *asoc;
  2599. long timeo;
  2600. int error = 0;
  2601. sctp_lock_sock(sk);
  2602. sp = sctp_sk(sk);
  2603. ep = sp->ep;
  2604. if (!sctp_style(sk, TCP)) {
  2605. error = -EOPNOTSUPP;
  2606. goto out;
  2607. }
  2608. if (!sctp_sstate(sk, LISTENING)) {
  2609. error = -EINVAL;
  2610. goto out;
  2611. }
  2612. timeo = sock_rcvtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  2613. error = sctp_wait_for_accept(sk, timeo);
  2614. if (error)
  2615. goto out;
  2616. /* We treat the list of associations on the endpoint as the accept
  2617. * queue and pick the first association on the list.
  2618. */
  2619. asoc = list_entry(ep->asocs.next, struct sctp_association, asocs);
  2620. newsk = sp->pf->create_accept_sk(sk, asoc);
  2621. if (!newsk) {
  2622. error = -ENOMEM;
  2623. goto out;
  2624. }
  2625. /* Populate the fields of the newsk from the oldsk and migrate the
  2626. * asoc to the newsk.
  2627. */
  2628. sctp_sock_migrate(sk, newsk, asoc, SCTP_SOCKET_TCP);
  2629. out:
  2630. sctp_release_sock(sk);
  2631. *err = error;
  2632. return newsk;
  2633. }
  2634. /* The SCTP ioctl handler. */
  2635. SCTP_STATIC int sctp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  2636. {
  2637. return -ENOIOCTLCMD;
  2638. }
  2639. /* This is the function which gets called during socket creation to
  2640. * initialized the SCTP-specific portion of the sock.
  2641. * The sock structure should already be zero-filled memory.
  2642. */
  2643. SCTP_STATIC int sctp_init_sock(struct sock *sk)
  2644. {
  2645. struct sctp_endpoint *ep;
  2646. struct sctp_sock *sp;
  2647. SCTP_DEBUG_PRINTK("sctp_init_sock(sk: %p)\n", sk);
  2648. sp = sctp_sk(sk);
  2649. /* Initialize the SCTP per socket area. */
  2650. switch (sk->sk_type) {
  2651. case SOCK_SEQPACKET:
  2652. sp->type = SCTP_SOCKET_UDP;
  2653. break;
  2654. case SOCK_STREAM:
  2655. sp->type = SCTP_SOCKET_TCP;
  2656. break;
  2657. default:
  2658. return -ESOCKTNOSUPPORT;
  2659. }
  2660. /* Initialize default send parameters. These parameters can be
  2661. * modified with the SCTP_DEFAULT_SEND_PARAM socket option.
  2662. */
  2663. sp->default_stream = 0;
  2664. sp->default_ppid = 0;
  2665. sp->default_flags = 0;
  2666. sp->default_context = 0;
  2667. sp->default_timetolive = 0;
  2668. /* Initialize default setup parameters. These parameters
  2669. * can be modified with the SCTP_INITMSG socket option or
  2670. * overridden by the SCTP_INIT CMSG.
  2671. */
  2672. sp->initmsg.sinit_num_ostreams = sctp_max_outstreams;
  2673. sp->initmsg.sinit_max_instreams = sctp_max_instreams;
  2674. sp->initmsg.sinit_max_attempts = sctp_max_retrans_init;
  2675. sp->initmsg.sinit_max_init_timeo = jiffies_to_msecs(sctp_rto_max);
  2676. /* Initialize default RTO related parameters. These parameters can
  2677. * be modified for with the SCTP_RTOINFO socket option.
  2678. */
  2679. sp->rtoinfo.srto_initial = jiffies_to_msecs(sctp_rto_initial);
  2680. sp->rtoinfo.srto_max = jiffies_to_msecs(sctp_rto_max);
  2681. sp->rtoinfo.srto_min = jiffies_to_msecs(sctp_rto_min);
  2682. /* Initialize default association related parameters. These parameters
  2683. * can be modified with the SCTP_ASSOCINFO socket option.
  2684. */
  2685. sp->assocparams.sasoc_asocmaxrxt = sctp_max_retrans_association;
  2686. sp->assocparams.sasoc_number_peer_destinations = 0;
  2687. sp->assocparams.sasoc_peer_rwnd = 0;
  2688. sp->assocparams.sasoc_local_rwnd = 0;
  2689. sp->assocparams.sasoc_cookie_life =
  2690. jiffies_to_msecs(sctp_valid_cookie_life);
  2691. /* Initialize default event subscriptions. By default, all the
  2692. * options are off.
  2693. */
  2694. memset(&sp->subscribe, 0, sizeof(struct sctp_event_subscribe));
  2695. /* Default Peer Address Parameters. These defaults can
  2696. * be modified via SCTP_PEER_ADDR_PARAMS
  2697. */
  2698. sp->hbinterval = jiffies_to_msecs(sctp_hb_interval);
  2699. sp->pathmaxrxt = sctp_max_retrans_path;
  2700. sp->pathmtu = 0; // allow default discovery
  2701. sp->sackdelay = jiffies_to_msecs(sctp_sack_timeout);
  2702. sp->param_flags = SPP_HB_ENABLE |
  2703. SPP_PMTUD_ENABLE |
  2704. SPP_SACKDELAY_ENABLE;
  2705. /* If enabled no SCTP message fragmentation will be performed.
  2706. * Configure through SCTP_DISABLE_FRAGMENTS socket option.
  2707. */
  2708. sp->disable_fragments = 0;
  2709. /* Turn on/off any Nagle-like algorithm. */
  2710. sp->nodelay = 1;
  2711. /* Enable by default. */
  2712. sp->v4mapped = 1;
  2713. /* Auto-close idle associations after the configured
  2714. * number of seconds. A value of 0 disables this
  2715. * feature. Configure through the SCTP_AUTOCLOSE socket option,
  2716. * for UDP-style sockets only.
  2717. */
  2718. sp->autoclose = 0;
  2719. /* User specified fragmentation limit. */
  2720. sp->user_frag = 0;
  2721. sp->adaption_ind = 0;
  2722. sp->pf = sctp_get_pf_specific(sk->sk_family);
  2723. /* Control variables for partial data delivery. */
  2724. sp->pd_mode = 0;
  2725. skb_queue_head_init(&sp->pd_lobby);
  2726. /* Create a per socket endpoint structure. Even if we
  2727. * change the data structure relationships, this may still
  2728. * be useful for storing pre-connect address information.
  2729. */
  2730. ep = sctp_endpoint_new(sk, GFP_KERNEL);
  2731. if (!ep)
  2732. return -ENOMEM;
  2733. sp->ep = ep;
  2734. sp->hmac = NULL;
  2735. SCTP_DBG_OBJCNT_INC(sock);
  2736. atomic_inc(&sctp_sockets_allocated);
  2737. return 0;
  2738. }
  2739. /* Cleanup any SCTP per socket resources. */
  2740. SCTP_STATIC int sctp_destroy_sock(struct sock *sk)
  2741. {
  2742. struct sctp_endpoint *ep;
  2743. SCTP_DEBUG_PRINTK("sctp_destroy_sock(sk: %p)\n", sk);
  2744. /* Release our hold on the endpoint. */
  2745. ep = sctp_sk(sk)->ep;
  2746. sctp_endpoint_free(ep);
  2747. atomic_dec(&sctp_sockets_allocated);
  2748. return 0;
  2749. }
  2750. /* API 4.1.7 shutdown() - TCP Style Syntax
  2751. * int shutdown(int socket, int how);
  2752. *
  2753. * sd - the socket descriptor of the association to be closed.
  2754. * how - Specifies the type of shutdown. The values are
  2755. * as follows:
  2756. * SHUT_RD
  2757. * Disables further receive operations. No SCTP
  2758. * protocol action is taken.
  2759. * SHUT_WR
  2760. * Disables further send operations, and initiates
  2761. * the SCTP shutdown sequence.
  2762. * SHUT_RDWR
  2763. * Disables further send and receive operations
  2764. * and initiates the SCTP shutdown sequence.
  2765. */
  2766. SCTP_STATIC void sctp_shutdown(struct sock *sk, int how)
  2767. {
  2768. struct sctp_endpoint *ep;
  2769. struct sctp_association *asoc;
  2770. if (!sctp_style(sk, TCP))
  2771. return;
  2772. if (how & SEND_SHUTDOWN) {
  2773. ep = sctp_sk(sk)->ep;
  2774. if (!list_empty(&ep->asocs)) {
  2775. asoc = list_entry(ep->asocs.next,
  2776. struct sctp_association, asocs);
  2777. sctp_primitive_SHUTDOWN(asoc, NULL);
  2778. }
  2779. }
  2780. }
  2781. /* 7.2.1 Association Status (SCTP_STATUS)
  2782. * Applications can retrieve current status information about an
  2783. * association, including association state, peer receiver window size,
  2784. * number of unacked data chunks, and number of data chunks pending
  2785. * receipt. This information is read-only.
  2786. */
  2787. static int sctp_getsockopt_sctp_status(struct sock *sk, int len,
  2788. char __user *optval,
  2789. int __user *optlen)
  2790. {
  2791. struct sctp_status status;
  2792. struct sctp_association *asoc = NULL;
  2793. struct sctp_transport *transport;
  2794. sctp_assoc_t associd;
  2795. int retval = 0;
  2796. if (len != sizeof(status)) {
  2797. retval = -EINVAL;
  2798. goto out;
  2799. }
  2800. if (copy_from_user(&status, optval, sizeof(status))) {
  2801. retval = -EFAULT;
  2802. goto out;
  2803. }
  2804. associd = status.sstat_assoc_id;
  2805. asoc = sctp_id2assoc(sk, associd);
  2806. if (!asoc) {
  2807. retval = -EINVAL;
  2808. goto out;
  2809. }
  2810. transport = asoc->peer.primary_path;
  2811. status.sstat_assoc_id = sctp_assoc2id(asoc);
  2812. status.sstat_state = asoc->state;
  2813. status.sstat_rwnd = asoc->peer.rwnd;
  2814. status.sstat_unackdata = asoc->unack_data;
  2815. status.sstat_penddata = sctp_tsnmap_pending(&asoc->peer.tsn_map);
  2816. status.sstat_instrms = asoc->c.sinit_max_instreams;
  2817. status.sstat_outstrms = asoc->c.sinit_num_ostreams;
  2818. status.sstat_fragmentation_point = asoc->frag_point;
  2819. status.sstat_primary.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2820. memcpy(&status.sstat_primary.spinfo_address,
  2821. &(transport->ipaddr), sizeof(union sctp_addr));
  2822. /* Map ipv4 address into v4-mapped-on-v6 address. */
  2823. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  2824. (union sctp_addr *)&status.sstat_primary.spinfo_address);
  2825. status.sstat_primary.spinfo_state = transport->state;
  2826. status.sstat_primary.spinfo_cwnd = transport->cwnd;
  2827. status.sstat_primary.spinfo_srtt = transport->srtt;
  2828. status.sstat_primary.spinfo_rto = jiffies_to_msecs(transport->rto);
  2829. status.sstat_primary.spinfo_mtu = transport->pathmtu;
  2830. if (status.sstat_primary.spinfo_state == SCTP_UNKNOWN)
  2831. status.sstat_primary.spinfo_state = SCTP_ACTIVE;
  2832. if (put_user(len, optlen)) {
  2833. retval = -EFAULT;
  2834. goto out;
  2835. }
  2836. SCTP_DEBUG_PRINTK("sctp_getsockopt_sctp_status(%d): %d %d %d\n",
  2837. len, status.sstat_state, status.sstat_rwnd,
  2838. status.sstat_assoc_id);
  2839. if (copy_to_user(optval, &status, len)) {
  2840. retval = -EFAULT;
  2841. goto out;
  2842. }
  2843. out:
  2844. return (retval);
  2845. }
  2846. /* 7.2.2 Peer Address Information (SCTP_GET_PEER_ADDR_INFO)
  2847. *
  2848. * Applications can retrieve information about a specific peer address
  2849. * of an association, including its reachability state, congestion
  2850. * window, and retransmission timer values. This information is
  2851. * read-only.
  2852. */
  2853. static int sctp_getsockopt_peer_addr_info(struct sock *sk, int len,
  2854. char __user *optval,
  2855. int __user *optlen)
  2856. {
  2857. struct sctp_paddrinfo pinfo;
  2858. struct sctp_transport *transport;
  2859. int retval = 0;
  2860. if (len != sizeof(pinfo)) {
  2861. retval = -EINVAL;
  2862. goto out;
  2863. }
  2864. if (copy_from_user(&pinfo, optval, sizeof(pinfo))) {
  2865. retval = -EFAULT;
  2866. goto out;
  2867. }
  2868. transport = sctp_addr_id2transport(sk, &pinfo.spinfo_address,
  2869. pinfo.spinfo_assoc_id);
  2870. if (!transport)
  2871. return -EINVAL;
  2872. pinfo.spinfo_assoc_id = sctp_assoc2id(transport->asoc);
  2873. pinfo.spinfo_state = transport->state;
  2874. pinfo.spinfo_cwnd = transport->cwnd;
  2875. pinfo.spinfo_srtt = transport->srtt;
  2876. pinfo.spinfo_rto = jiffies_to_msecs(transport->rto);
  2877. pinfo.spinfo_mtu = transport->pathmtu;
  2878. if (pinfo.spinfo_state == SCTP_UNKNOWN)
  2879. pinfo.spinfo_state = SCTP_ACTIVE;
  2880. if (put_user(len, optlen)) {
  2881. retval = -EFAULT;
  2882. goto out;
  2883. }
  2884. if (copy_to_user(optval, &pinfo, len)) {
  2885. retval = -EFAULT;
  2886. goto out;
  2887. }
  2888. out:
  2889. return (retval);
  2890. }
  2891. /* 7.1.12 Enable/Disable message fragmentation (SCTP_DISABLE_FRAGMENTS)
  2892. *
  2893. * This option is a on/off flag. If enabled no SCTP message
  2894. * fragmentation will be performed. Instead if a message being sent
  2895. * exceeds the current PMTU size, the message will NOT be sent and
  2896. * instead a error will be indicated to the user.
  2897. */
  2898. static int sctp_getsockopt_disable_fragments(struct sock *sk, int len,
  2899. char __user *optval, int __user *optlen)
  2900. {
  2901. int val;
  2902. if (len < sizeof(int))
  2903. return -EINVAL;
  2904. len = sizeof(int);
  2905. val = (sctp_sk(sk)->disable_fragments == 1);
  2906. if (put_user(len, optlen))
  2907. return -EFAULT;
  2908. if (copy_to_user(optval, &val, len))
  2909. return -EFAULT;
  2910. return 0;
  2911. }
  2912. /* 7.1.15 Set notification and ancillary events (SCTP_EVENTS)
  2913. *
  2914. * This socket option is used to specify various notifications and
  2915. * ancillary data the user wishes to receive.
  2916. */
  2917. static int sctp_getsockopt_events(struct sock *sk, int len, char __user *optval,
  2918. int __user *optlen)
  2919. {
  2920. if (len != sizeof(struct sctp_event_subscribe))
  2921. return -EINVAL;
  2922. if (copy_to_user(optval, &sctp_sk(sk)->subscribe, len))
  2923. return -EFAULT;
  2924. return 0;
  2925. }
  2926. /* 7.1.8 Automatic Close of associations (SCTP_AUTOCLOSE)
  2927. *
  2928. * This socket option is applicable to the UDP-style socket only. When
  2929. * set it will cause associations that are idle for more than the
  2930. * specified number of seconds to automatically close. An association
  2931. * being idle is defined an association that has NOT sent or received
  2932. * user data. The special value of '0' indicates that no automatic
  2933. * close of any associations should be performed. The option expects an
  2934. * integer defining the number of seconds of idle time before an
  2935. * association is closed.
  2936. */
  2937. static int sctp_getsockopt_autoclose(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2938. {
  2939. /* Applicable to UDP-style socket only */
  2940. if (sctp_style(sk, TCP))
  2941. return -EOPNOTSUPP;
  2942. if (len != sizeof(int))
  2943. return -EINVAL;
  2944. if (copy_to_user(optval, &sctp_sk(sk)->autoclose, len))
  2945. return -EFAULT;
  2946. return 0;
  2947. }
  2948. /* Helper routine to branch off an association to a new socket. */
  2949. SCTP_STATIC int sctp_do_peeloff(struct sctp_association *asoc,
  2950. struct socket **sockp)
  2951. {
  2952. struct sock *sk = asoc->base.sk;
  2953. struct socket *sock;
  2954. int err = 0;
  2955. /* An association cannot be branched off from an already peeled-off
  2956. * socket, nor is this supported for tcp style sockets.
  2957. */
  2958. if (!sctp_style(sk, UDP))
  2959. return -EINVAL;
  2960. /* Create a new socket. */
  2961. err = sock_create(sk->sk_family, SOCK_SEQPACKET, IPPROTO_SCTP, &sock);
  2962. if (err < 0)
  2963. return err;
  2964. /* Populate the fields of the newsk from the oldsk and migrate the
  2965. * asoc to the newsk.
  2966. */
  2967. sctp_sock_migrate(sk, sock->sk, asoc, SCTP_SOCKET_UDP_HIGH_BANDWIDTH);
  2968. *sockp = sock;
  2969. return err;
  2970. }
  2971. static int sctp_getsockopt_peeloff(struct sock *sk, int len, char __user *optval, int __user *optlen)
  2972. {
  2973. sctp_peeloff_arg_t peeloff;
  2974. struct socket *newsock;
  2975. int retval = 0;
  2976. struct sctp_association *asoc;
  2977. if (len != sizeof(sctp_peeloff_arg_t))
  2978. return -EINVAL;
  2979. if (copy_from_user(&peeloff, optval, len))
  2980. return -EFAULT;
  2981. asoc = sctp_id2assoc(sk, peeloff.associd);
  2982. if (!asoc) {
  2983. retval = -EINVAL;
  2984. goto out;
  2985. }
  2986. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p\n", __FUNCTION__, sk, asoc);
  2987. retval = sctp_do_peeloff(asoc, &newsock);
  2988. if (retval < 0)
  2989. goto out;
  2990. /* Map the socket to an unused fd that can be returned to the user. */
  2991. retval = sock_map_fd(newsock);
  2992. if (retval < 0) {
  2993. sock_release(newsock);
  2994. goto out;
  2995. }
  2996. SCTP_DEBUG_PRINTK("%s: sk: %p asoc: %p newsk: %p sd: %d\n",
  2997. __FUNCTION__, sk, asoc, newsock->sk, retval);
  2998. /* Return the fd mapped to the new socket. */
  2999. peeloff.sd = retval;
  3000. if (copy_to_user(optval, &peeloff, len))
  3001. retval = -EFAULT;
  3002. out:
  3003. return retval;
  3004. }
  3005. /* 7.1.13 Peer Address Parameters (SCTP_PEER_ADDR_PARAMS)
  3006. *
  3007. * Applications can enable or disable heartbeats for any peer address of
  3008. * an association, modify an address's heartbeat interval, force a
  3009. * heartbeat to be sent immediately, and adjust the address's maximum
  3010. * number of retransmissions sent before an address is considered
  3011. * unreachable. The following structure is used to access and modify an
  3012. * address's parameters:
  3013. *
  3014. * struct sctp_paddrparams {
  3015. * sctp_assoc_t spp_assoc_id;
  3016. * struct sockaddr_storage spp_address;
  3017. * uint32_t spp_hbinterval;
  3018. * uint16_t spp_pathmaxrxt;
  3019. * uint32_t spp_pathmtu;
  3020. * uint32_t spp_sackdelay;
  3021. * uint32_t spp_flags;
  3022. * };
  3023. *
  3024. * spp_assoc_id - (one-to-many style socket) This is filled in the
  3025. * application, and identifies the association for
  3026. * this query.
  3027. * spp_address - This specifies which address is of interest.
  3028. * spp_hbinterval - This contains the value of the heartbeat interval,
  3029. * in milliseconds. If a value of zero
  3030. * is present in this field then no changes are to
  3031. * be made to this parameter.
  3032. * spp_pathmaxrxt - This contains the maximum number of
  3033. * retransmissions before this address shall be
  3034. * considered unreachable. If a value of zero
  3035. * is present in this field then no changes are to
  3036. * be made to this parameter.
  3037. * spp_pathmtu - When Path MTU discovery is disabled the value
  3038. * specified here will be the "fixed" path mtu.
  3039. * Note that if the spp_address field is empty
  3040. * then all associations on this address will
  3041. * have this fixed path mtu set upon them.
  3042. *
  3043. * spp_sackdelay - When delayed sack is enabled, this value specifies
  3044. * the number of milliseconds that sacks will be delayed
  3045. * for. This value will apply to all addresses of an
  3046. * association if the spp_address field is empty. Note
  3047. * also, that if delayed sack is enabled and this
  3048. * value is set to 0, no change is made to the last
  3049. * recorded delayed sack timer value.
  3050. *
  3051. * spp_flags - These flags are used to control various features
  3052. * on an association. The flag field may contain
  3053. * zero or more of the following options.
  3054. *
  3055. * SPP_HB_ENABLE - Enable heartbeats on the
  3056. * specified address. Note that if the address
  3057. * field is empty all addresses for the association
  3058. * have heartbeats enabled upon them.
  3059. *
  3060. * SPP_HB_DISABLE - Disable heartbeats on the
  3061. * speicifed address. Note that if the address
  3062. * field is empty all addresses for the association
  3063. * will have their heartbeats disabled. Note also
  3064. * that SPP_HB_ENABLE and SPP_HB_DISABLE are
  3065. * mutually exclusive, only one of these two should
  3066. * be specified. Enabling both fields will have
  3067. * undetermined results.
  3068. *
  3069. * SPP_HB_DEMAND - Request a user initiated heartbeat
  3070. * to be made immediately.
  3071. *
  3072. * SPP_PMTUD_ENABLE - This field will enable PMTU
  3073. * discovery upon the specified address. Note that
  3074. * if the address feild is empty then all addresses
  3075. * on the association are effected.
  3076. *
  3077. * SPP_PMTUD_DISABLE - This field will disable PMTU
  3078. * discovery upon the specified address. Note that
  3079. * if the address feild is empty then all addresses
  3080. * on the association are effected. Not also that
  3081. * SPP_PMTUD_ENABLE and SPP_PMTUD_DISABLE are mutually
  3082. * exclusive. Enabling both will have undetermined
  3083. * results.
  3084. *
  3085. * SPP_SACKDELAY_ENABLE - Setting this flag turns
  3086. * on delayed sack. The time specified in spp_sackdelay
  3087. * is used to specify the sack delay for this address. Note
  3088. * that if spp_address is empty then all addresses will
  3089. * enable delayed sack and take on the sack delay
  3090. * value specified in spp_sackdelay.
  3091. * SPP_SACKDELAY_DISABLE - Setting this flag turns
  3092. * off delayed sack. If the spp_address field is blank then
  3093. * delayed sack is disabled for the entire association. Note
  3094. * also that this field is mutually exclusive to
  3095. * SPP_SACKDELAY_ENABLE, setting both will have undefined
  3096. * results.
  3097. */
  3098. static int sctp_getsockopt_peer_addr_params(struct sock *sk, int len,
  3099. char __user *optval, int __user *optlen)
  3100. {
  3101. struct sctp_paddrparams params;
  3102. struct sctp_transport *trans = NULL;
  3103. struct sctp_association *asoc = NULL;
  3104. struct sctp_sock *sp = sctp_sk(sk);
  3105. if (len != sizeof(struct sctp_paddrparams))
  3106. return -EINVAL;
  3107. if (copy_from_user(&params, optval, len))
  3108. return -EFAULT;
  3109. /* If an address other than INADDR_ANY is specified, and
  3110. * no transport is found, then the request is invalid.
  3111. */
  3112. if (!sctp_is_any(( union sctp_addr *)&params.spp_address)) {
  3113. trans = sctp_addr_id2transport(sk, &params.spp_address,
  3114. params.spp_assoc_id);
  3115. if (!trans) {
  3116. SCTP_DEBUG_PRINTK("Failed no transport\n");
  3117. return -EINVAL;
  3118. }
  3119. }
  3120. /* Get association, if assoc_id != 0 and the socket is a one
  3121. * to many style socket, and an association was not found, then
  3122. * the id was invalid.
  3123. */
  3124. asoc = sctp_id2assoc(sk, params.spp_assoc_id);
  3125. if (!asoc && params.spp_assoc_id && sctp_style(sk, UDP)) {
  3126. SCTP_DEBUG_PRINTK("Failed no association\n");
  3127. return -EINVAL;
  3128. }
  3129. if (trans) {
  3130. /* Fetch transport values. */
  3131. params.spp_hbinterval = jiffies_to_msecs(trans->hbinterval);
  3132. params.spp_pathmtu = trans->pathmtu;
  3133. params.spp_pathmaxrxt = trans->pathmaxrxt;
  3134. params.spp_sackdelay = jiffies_to_msecs(trans->sackdelay);
  3135. /*draft-11 doesn't say what to return in spp_flags*/
  3136. params.spp_flags = trans->param_flags;
  3137. } else if (asoc) {
  3138. /* Fetch association values. */
  3139. params.spp_hbinterval = jiffies_to_msecs(asoc->hbinterval);
  3140. params.spp_pathmtu = asoc->pathmtu;
  3141. params.spp_pathmaxrxt = asoc->pathmaxrxt;
  3142. params.spp_sackdelay = jiffies_to_msecs(asoc->sackdelay);
  3143. /*draft-11 doesn't say what to return in spp_flags*/
  3144. params.spp_flags = asoc->param_flags;
  3145. } else {
  3146. /* Fetch socket values. */
  3147. params.spp_hbinterval = sp->hbinterval;
  3148. params.spp_pathmtu = sp->pathmtu;
  3149. params.spp_sackdelay = sp->sackdelay;
  3150. params.spp_pathmaxrxt = sp->pathmaxrxt;
  3151. /*draft-11 doesn't say what to return in spp_flags*/
  3152. params.spp_flags = sp->param_flags;
  3153. }
  3154. if (copy_to_user(optval, &params, len))
  3155. return -EFAULT;
  3156. if (put_user(len, optlen))
  3157. return -EFAULT;
  3158. return 0;
  3159. }
  3160. /* 7.1.24. Delayed Ack Timer (SCTP_DELAYED_ACK_TIME)
  3161. *
  3162. * This options will get or set the delayed ack timer. The time is set
  3163. * in milliseconds. If the assoc_id is 0, then this sets or gets the
  3164. * endpoints default delayed ack timer value. If the assoc_id field is
  3165. * non-zero, then the set or get effects the specified association.
  3166. *
  3167. * struct sctp_assoc_value {
  3168. * sctp_assoc_t assoc_id;
  3169. * uint32_t assoc_value;
  3170. * };
  3171. *
  3172. * assoc_id - This parameter, indicates which association the
  3173. * user is preforming an action upon. Note that if
  3174. * this field's value is zero then the endpoints
  3175. * default value is changed (effecting future
  3176. * associations only).
  3177. *
  3178. * assoc_value - This parameter contains the number of milliseconds
  3179. * that the user is requesting the delayed ACK timer
  3180. * be set to. Note that this value is defined in
  3181. * the standard to be between 200 and 500 milliseconds.
  3182. *
  3183. * Note: a value of zero will leave the value alone,
  3184. * but disable SACK delay. A non-zero value will also
  3185. * enable SACK delay.
  3186. */
  3187. static int sctp_getsockopt_delayed_ack_time(struct sock *sk, int len,
  3188. char __user *optval,
  3189. int __user *optlen)
  3190. {
  3191. struct sctp_assoc_value params;
  3192. struct sctp_association *asoc = NULL;
  3193. struct sctp_sock *sp = sctp_sk(sk);
  3194. if (len != sizeof(struct sctp_assoc_value))
  3195. return - EINVAL;
  3196. if (copy_from_user(&params, optval, len))
  3197. return -EFAULT;
  3198. /* Get association, if assoc_id != 0 and the socket is a one
  3199. * to many style socket, and an association was not found, then
  3200. * the id was invalid.
  3201. */
  3202. asoc = sctp_id2assoc(sk, params.assoc_id);
  3203. if (!asoc && params.assoc_id && sctp_style(sk, UDP))
  3204. return -EINVAL;
  3205. if (asoc) {
  3206. /* Fetch association values. */
  3207. if (asoc->param_flags & SPP_SACKDELAY_ENABLE)
  3208. params.assoc_value = jiffies_to_msecs(
  3209. asoc->sackdelay);
  3210. else
  3211. params.assoc_value = 0;
  3212. } else {
  3213. /* Fetch socket values. */
  3214. if (sp->param_flags & SPP_SACKDELAY_ENABLE)
  3215. params.assoc_value = sp->sackdelay;
  3216. else
  3217. params.assoc_value = 0;
  3218. }
  3219. if (copy_to_user(optval, &params, len))
  3220. return -EFAULT;
  3221. if (put_user(len, optlen))
  3222. return -EFAULT;
  3223. return 0;
  3224. }
  3225. /* 7.1.3 Initialization Parameters (SCTP_INITMSG)
  3226. *
  3227. * Applications can specify protocol parameters for the default association
  3228. * initialization. The option name argument to setsockopt() and getsockopt()
  3229. * is SCTP_INITMSG.
  3230. *
  3231. * Setting initialization parameters is effective only on an unconnected
  3232. * socket (for UDP-style sockets only future associations are effected
  3233. * by the change). With TCP-style sockets, this option is inherited by
  3234. * sockets derived from a listener socket.
  3235. */
  3236. static int sctp_getsockopt_initmsg(struct sock *sk, int len, char __user *optval, int __user *optlen)
  3237. {
  3238. if (len != sizeof(struct sctp_initmsg))
  3239. return -EINVAL;
  3240. if (copy_to_user(optval, &sctp_sk(sk)->initmsg, len))
  3241. return -EFAULT;
  3242. return 0;
  3243. }
  3244. static int sctp_getsockopt_peer_addrs_num_old(struct sock *sk, int len,
  3245. char __user *optval,
  3246. int __user *optlen)
  3247. {
  3248. sctp_assoc_t id;
  3249. struct sctp_association *asoc;
  3250. struct list_head *pos;
  3251. int cnt = 0;
  3252. if (len != sizeof(sctp_assoc_t))
  3253. return -EINVAL;
  3254. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3255. return -EFAULT;
  3256. /* For UDP-style sockets, id specifies the association to query. */
  3257. asoc = sctp_id2assoc(sk, id);
  3258. if (!asoc)
  3259. return -EINVAL;
  3260. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3261. cnt ++;
  3262. }
  3263. return cnt;
  3264. }
  3265. /*
  3266. * Old API for getting list of peer addresses. Does not work for 32-bit
  3267. * programs running on a 64-bit kernel
  3268. */
  3269. static int sctp_getsockopt_peer_addrs_old(struct sock *sk, int len,
  3270. char __user *optval,
  3271. int __user *optlen)
  3272. {
  3273. struct sctp_association *asoc;
  3274. struct list_head *pos;
  3275. int cnt = 0;
  3276. struct sctp_getaddrs_old getaddrs;
  3277. struct sctp_transport *from;
  3278. void __user *to;
  3279. union sctp_addr temp;
  3280. struct sctp_sock *sp = sctp_sk(sk);
  3281. int addrlen;
  3282. if (len != sizeof(struct sctp_getaddrs_old))
  3283. return -EINVAL;
  3284. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  3285. return -EFAULT;
  3286. if (getaddrs.addr_num <= 0) return -EINVAL;
  3287. /* For UDP-style sockets, id specifies the association to query. */
  3288. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3289. if (!asoc)
  3290. return -EINVAL;
  3291. to = (void __user *)getaddrs.addrs;
  3292. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3293. from = list_entry(pos, struct sctp_transport, transports);
  3294. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3295. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3296. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3297. temp.v4.sin_port = htons(temp.v4.sin_port);
  3298. if (copy_to_user(to, &temp, addrlen))
  3299. return -EFAULT;
  3300. to += addrlen ;
  3301. cnt ++;
  3302. if (cnt >= getaddrs.addr_num) break;
  3303. }
  3304. getaddrs.addr_num = cnt;
  3305. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  3306. return -EFAULT;
  3307. return 0;
  3308. }
  3309. static int sctp_getsockopt_peer_addrs(struct sock *sk, int len,
  3310. char __user *optval, int __user *optlen)
  3311. {
  3312. struct sctp_association *asoc;
  3313. struct list_head *pos;
  3314. int cnt = 0;
  3315. struct sctp_getaddrs getaddrs;
  3316. struct sctp_transport *from;
  3317. void __user *to;
  3318. union sctp_addr temp;
  3319. struct sctp_sock *sp = sctp_sk(sk);
  3320. int addrlen;
  3321. size_t space_left;
  3322. int bytes_copied;
  3323. if (len < sizeof(struct sctp_getaddrs))
  3324. return -EINVAL;
  3325. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3326. return -EFAULT;
  3327. /* For UDP-style sockets, id specifies the association to query. */
  3328. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3329. if (!asoc)
  3330. return -EINVAL;
  3331. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3332. space_left = len - sizeof(struct sctp_getaddrs) -
  3333. offsetof(struct sctp_getaddrs,addrs);
  3334. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3335. from = list_entry(pos, struct sctp_transport, transports);
  3336. memcpy(&temp, &from->ipaddr, sizeof(temp));
  3337. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3338. addrlen = sctp_get_af_specific(sk->sk_family)->sockaddr_len;
  3339. if(space_left < addrlen)
  3340. return -ENOMEM;
  3341. temp.v4.sin_port = htons(temp.v4.sin_port);
  3342. if (copy_to_user(to, &temp, addrlen))
  3343. return -EFAULT;
  3344. to += addrlen;
  3345. cnt++;
  3346. space_left -= addrlen;
  3347. }
  3348. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3349. return -EFAULT;
  3350. bytes_copied = ((char __user *)to) - optval;
  3351. if (put_user(bytes_copied, optlen))
  3352. return -EFAULT;
  3353. return 0;
  3354. }
  3355. static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
  3356. char __user *optval,
  3357. int __user *optlen)
  3358. {
  3359. sctp_assoc_t id;
  3360. struct sctp_bind_addr *bp;
  3361. struct sctp_association *asoc;
  3362. struct list_head *pos;
  3363. struct sctp_sockaddr_entry *addr;
  3364. rwlock_t *addr_lock;
  3365. unsigned long flags;
  3366. int cnt = 0;
  3367. if (len != sizeof(sctp_assoc_t))
  3368. return -EINVAL;
  3369. if (copy_from_user(&id, optval, sizeof(sctp_assoc_t)))
  3370. return -EFAULT;
  3371. /*
  3372. * For UDP-style sockets, id specifies the association to query.
  3373. * If the id field is set to the value '0' then the locally bound
  3374. * addresses are returned without regard to any particular
  3375. * association.
  3376. */
  3377. if (0 == id) {
  3378. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3379. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3380. } else {
  3381. asoc = sctp_id2assoc(sk, id);
  3382. if (!asoc)
  3383. return -EINVAL;
  3384. bp = &asoc->base.bind_addr;
  3385. addr_lock = &asoc->base.addr_lock;
  3386. }
  3387. sctp_read_lock(addr_lock);
  3388. /* If the endpoint is bound to 0.0.0.0 or ::0, count the valid
  3389. * addresses from the global local address list.
  3390. */
  3391. if (sctp_list_single_entry(&bp->address_list)) {
  3392. addr = list_entry(bp->address_list.next,
  3393. struct sctp_sockaddr_entry, list);
  3394. if (sctp_is_any(&addr->a)) {
  3395. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3396. list_for_each(pos, &sctp_local_addr_list) {
  3397. addr = list_entry(pos,
  3398. struct sctp_sockaddr_entry,
  3399. list);
  3400. if ((PF_INET == sk->sk_family) &&
  3401. (AF_INET6 == addr->a.sa.sa_family))
  3402. continue;
  3403. cnt++;
  3404. }
  3405. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3406. flags);
  3407. } else {
  3408. cnt = 1;
  3409. }
  3410. goto done;
  3411. }
  3412. list_for_each(pos, &bp->address_list) {
  3413. cnt ++;
  3414. }
  3415. done:
  3416. sctp_read_unlock(addr_lock);
  3417. return cnt;
  3418. }
  3419. /* Helper function that copies local addresses to user and returns the number
  3420. * of addresses copied.
  3421. */
  3422. static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
  3423. void __user *to)
  3424. {
  3425. struct list_head *pos;
  3426. struct sctp_sockaddr_entry *addr;
  3427. unsigned long flags;
  3428. union sctp_addr temp;
  3429. int cnt = 0;
  3430. int addrlen;
  3431. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3432. list_for_each(pos, &sctp_local_addr_list) {
  3433. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3434. if ((PF_INET == sk->sk_family) &&
  3435. (AF_INET6 == addr->a.sa.sa_family))
  3436. continue;
  3437. memcpy(&temp, &addr->a, sizeof(temp));
  3438. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3439. &temp);
  3440. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3441. temp.v4.sin_port = htons(port);
  3442. if (copy_to_user(to, &temp, addrlen)) {
  3443. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3444. flags);
  3445. return -EFAULT;
  3446. }
  3447. to += addrlen;
  3448. cnt ++;
  3449. if (cnt >= max_addrs) break;
  3450. }
  3451. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  3452. return cnt;
  3453. }
  3454. static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
  3455. void __user **to, size_t space_left)
  3456. {
  3457. struct list_head *pos;
  3458. struct sctp_sockaddr_entry *addr;
  3459. unsigned long flags;
  3460. union sctp_addr temp;
  3461. int cnt = 0;
  3462. int addrlen;
  3463. sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
  3464. list_for_each(pos, &sctp_local_addr_list) {
  3465. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3466. if ((PF_INET == sk->sk_family) &&
  3467. (AF_INET6 == addr->a.sa.sa_family))
  3468. continue;
  3469. memcpy(&temp, &addr->a, sizeof(temp));
  3470. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
  3471. &temp);
  3472. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3473. if(space_left<addrlen)
  3474. return -ENOMEM;
  3475. temp.v4.sin_port = htons(port);
  3476. if (copy_to_user(*to, &temp, addrlen)) {
  3477. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
  3478. flags);
  3479. return -EFAULT;
  3480. }
  3481. *to += addrlen;
  3482. cnt ++;
  3483. space_left -= addrlen;
  3484. }
  3485. sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
  3486. return cnt;
  3487. }
  3488. /* Old API for getting list of local addresses. Does not work for 32-bit
  3489. * programs running on a 64-bit kernel
  3490. */
  3491. static int sctp_getsockopt_local_addrs_old(struct sock *sk, int len,
  3492. char __user *optval, int __user *optlen)
  3493. {
  3494. struct sctp_bind_addr *bp;
  3495. struct sctp_association *asoc;
  3496. struct list_head *pos;
  3497. int cnt = 0;
  3498. struct sctp_getaddrs_old getaddrs;
  3499. struct sctp_sockaddr_entry *addr;
  3500. void __user *to;
  3501. union sctp_addr temp;
  3502. struct sctp_sock *sp = sctp_sk(sk);
  3503. int addrlen;
  3504. rwlock_t *addr_lock;
  3505. int err = 0;
  3506. if (len != sizeof(struct sctp_getaddrs_old))
  3507. return -EINVAL;
  3508. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs_old)))
  3509. return -EFAULT;
  3510. if (getaddrs.addr_num <= 0 ||
  3511. getaddrs.addr_num >= (INT_MAX / sizeof(union sctp_addr)))
  3512. return -EINVAL;
  3513. /*
  3514. * For UDP-style sockets, id specifies the association to query.
  3515. * If the id field is set to the value '0' then the locally bound
  3516. * addresses are returned without regard to any particular
  3517. * association.
  3518. */
  3519. if (0 == getaddrs.assoc_id) {
  3520. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3521. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3522. } else {
  3523. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3524. if (!asoc)
  3525. return -EINVAL;
  3526. bp = &asoc->base.bind_addr;
  3527. addr_lock = &asoc->base.addr_lock;
  3528. }
  3529. to = getaddrs.addrs;
  3530. sctp_read_lock(addr_lock);
  3531. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3532. * addresses from the global local address list.
  3533. */
  3534. if (sctp_list_single_entry(&bp->address_list)) {
  3535. addr = list_entry(bp->address_list.next,
  3536. struct sctp_sockaddr_entry, list);
  3537. if (sctp_is_any(&addr->a)) {
  3538. cnt = sctp_copy_laddrs_to_user_old(sk, bp->port,
  3539. getaddrs.addr_num,
  3540. to);
  3541. if (cnt < 0) {
  3542. err = cnt;
  3543. goto unlock;
  3544. }
  3545. goto copy_getaddrs;
  3546. }
  3547. }
  3548. list_for_each(pos, &bp->address_list) {
  3549. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3550. memcpy(&temp, &addr->a, sizeof(temp));
  3551. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3552. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3553. temp.v4.sin_port = htons(temp.v4.sin_port);
  3554. if (copy_to_user(to, &temp, addrlen)) {
  3555. err = -EFAULT;
  3556. goto unlock;
  3557. }
  3558. to += addrlen;
  3559. cnt ++;
  3560. if (cnt >= getaddrs.addr_num) break;
  3561. }
  3562. copy_getaddrs:
  3563. getaddrs.addr_num = cnt;
  3564. if (copy_to_user(optval, &getaddrs, sizeof(struct sctp_getaddrs_old)))
  3565. err = -EFAULT;
  3566. unlock:
  3567. sctp_read_unlock(addr_lock);
  3568. return err;
  3569. }
  3570. static int sctp_getsockopt_local_addrs(struct sock *sk, int len,
  3571. char __user *optval, int __user *optlen)
  3572. {
  3573. struct sctp_bind_addr *bp;
  3574. struct sctp_association *asoc;
  3575. struct list_head *pos;
  3576. int cnt = 0;
  3577. struct sctp_getaddrs getaddrs;
  3578. struct sctp_sockaddr_entry *addr;
  3579. void __user *to;
  3580. union sctp_addr temp;
  3581. struct sctp_sock *sp = sctp_sk(sk);
  3582. int addrlen;
  3583. rwlock_t *addr_lock;
  3584. int err = 0;
  3585. size_t space_left;
  3586. int bytes_copied;
  3587. if (len <= sizeof(struct sctp_getaddrs))
  3588. return -EINVAL;
  3589. if (copy_from_user(&getaddrs, optval, sizeof(struct sctp_getaddrs)))
  3590. return -EFAULT;
  3591. /*
  3592. * For UDP-style sockets, id specifies the association to query.
  3593. * If the id field is set to the value '0' then the locally bound
  3594. * addresses are returned without regard to any particular
  3595. * association.
  3596. */
  3597. if (0 == getaddrs.assoc_id) {
  3598. bp = &sctp_sk(sk)->ep->base.bind_addr;
  3599. addr_lock = &sctp_sk(sk)->ep->base.addr_lock;
  3600. } else {
  3601. asoc = sctp_id2assoc(sk, getaddrs.assoc_id);
  3602. if (!asoc)
  3603. return -EINVAL;
  3604. bp = &asoc->base.bind_addr;
  3605. addr_lock = &asoc->base.addr_lock;
  3606. }
  3607. to = optval + offsetof(struct sctp_getaddrs,addrs);
  3608. space_left = len - sizeof(struct sctp_getaddrs) -
  3609. offsetof(struct sctp_getaddrs,addrs);
  3610. sctp_read_lock(addr_lock);
  3611. /* If the endpoint is bound to 0.0.0.0 or ::0, get the valid
  3612. * addresses from the global local address list.
  3613. */
  3614. if (sctp_list_single_entry(&bp->address_list)) {
  3615. addr = list_entry(bp->address_list.next,
  3616. struct sctp_sockaddr_entry, list);
  3617. if (sctp_is_any(&addr->a)) {
  3618. cnt = sctp_copy_laddrs_to_user(sk, bp->port,
  3619. &to, space_left);
  3620. if (cnt < 0) {
  3621. err = cnt;
  3622. goto unlock;
  3623. }
  3624. goto copy_getaddrs;
  3625. }
  3626. }
  3627. list_for_each(pos, &bp->address_list) {
  3628. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  3629. memcpy(&temp, &addr->a, sizeof(temp));
  3630. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp, &temp);
  3631. addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
  3632. if(space_left < addrlen)
  3633. return -ENOMEM; /*fixme: right error?*/
  3634. temp.v4.sin_port = htons(temp.v4.sin_port);
  3635. if (copy_to_user(to, &temp, addrlen)) {
  3636. err = -EFAULT;
  3637. goto unlock;
  3638. }
  3639. to += addrlen;
  3640. cnt ++;
  3641. space_left -= addrlen;
  3642. }
  3643. copy_getaddrs:
  3644. if (put_user(cnt, &((struct sctp_getaddrs __user *)optval)->addr_num))
  3645. return -EFAULT;
  3646. bytes_copied = ((char __user *)to) - optval;
  3647. if (put_user(bytes_copied, optlen))
  3648. return -EFAULT;
  3649. unlock:
  3650. sctp_read_unlock(addr_lock);
  3651. return err;
  3652. }
  3653. /* 7.1.10 Set Primary Address (SCTP_PRIMARY_ADDR)
  3654. *
  3655. * Requests that the local SCTP stack use the enclosed peer address as
  3656. * the association primary. The enclosed address must be one of the
  3657. * association peer's addresses.
  3658. */
  3659. static int sctp_getsockopt_primary_addr(struct sock *sk, int len,
  3660. char __user *optval, int __user *optlen)
  3661. {
  3662. struct sctp_prim prim;
  3663. struct sctp_association *asoc;
  3664. struct sctp_sock *sp = sctp_sk(sk);
  3665. if (len != sizeof(struct sctp_prim))
  3666. return -EINVAL;
  3667. if (copy_from_user(&prim, optval, sizeof(struct sctp_prim)))
  3668. return -EFAULT;
  3669. asoc = sctp_id2assoc(sk, prim.ssp_assoc_id);
  3670. if (!asoc)
  3671. return -EINVAL;
  3672. if (!asoc->peer.primary_path)
  3673. return -ENOTCONN;
  3674. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3675. htons(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3676. memcpy(&prim.ssp_addr, &asoc->peer.primary_path->ipaddr,
  3677. sizeof(union sctp_addr));
  3678. asoc->peer.primary_path->ipaddr.v4.sin_port =
  3679. ntohs(asoc->peer.primary_path->ipaddr.v4.sin_port);
  3680. sctp_get_pf_specific(sk->sk_family)->addr_v4map(sp,
  3681. (union sctp_addr *)&prim.ssp_addr);
  3682. if (copy_to_user(optval, &prim, sizeof(struct sctp_prim)))
  3683. return -EFAULT;
  3684. return 0;
  3685. }
  3686. /*
  3687. * 7.1.11 Set Adaption Layer Indicator (SCTP_ADAPTION_LAYER)
  3688. *
  3689. * Requests that the local endpoint set the specified Adaption Layer
  3690. * Indication parameter for all future INIT and INIT-ACK exchanges.
  3691. */
  3692. static int sctp_getsockopt_adaption_layer(struct sock *sk, int len,
  3693. char __user *optval, int __user *optlen)
  3694. {
  3695. struct sctp_setadaption adaption;
  3696. if (len != sizeof(struct sctp_setadaption))
  3697. return -EINVAL;
  3698. adaption.ssb_adaption_ind = sctp_sk(sk)->adaption_ind;
  3699. if (copy_to_user(optval, &adaption, len))
  3700. return -EFAULT;
  3701. return 0;
  3702. }
  3703. /*
  3704. *
  3705. * 7.1.14 Set default send parameters (SCTP_DEFAULT_SEND_PARAM)
  3706. *
  3707. * Applications that wish to use the sendto() system call may wish to
  3708. * specify a default set of parameters that would normally be supplied
  3709. * through the inclusion of ancillary data. This socket option allows
  3710. * such an application to set the default sctp_sndrcvinfo structure.
  3711. * The application that wishes to use this socket option simply passes
  3712. * in to this call the sctp_sndrcvinfo structure defined in Section
  3713. * 5.2.2) The input parameters accepted by this call include
  3714. * sinfo_stream, sinfo_flags, sinfo_ppid, sinfo_context,
  3715. * sinfo_timetolive. The user must provide the sinfo_assoc_id field in
  3716. * to this call if the caller is using the UDP model.
  3717. *
  3718. * For getsockopt, it get the default sctp_sndrcvinfo structure.
  3719. */
  3720. static int sctp_getsockopt_default_send_param(struct sock *sk,
  3721. int len, char __user *optval,
  3722. int __user *optlen)
  3723. {
  3724. struct sctp_sndrcvinfo info;
  3725. struct sctp_association *asoc;
  3726. struct sctp_sock *sp = sctp_sk(sk);
  3727. if (len != sizeof(struct sctp_sndrcvinfo))
  3728. return -EINVAL;
  3729. if (copy_from_user(&info, optval, sizeof(struct sctp_sndrcvinfo)))
  3730. return -EFAULT;
  3731. asoc = sctp_id2assoc(sk, info.sinfo_assoc_id);
  3732. if (!asoc && info.sinfo_assoc_id && sctp_style(sk, UDP))
  3733. return -EINVAL;
  3734. if (asoc) {
  3735. info.sinfo_stream = asoc->default_stream;
  3736. info.sinfo_flags = asoc->default_flags;
  3737. info.sinfo_ppid = asoc->default_ppid;
  3738. info.sinfo_context = asoc->default_context;
  3739. info.sinfo_timetolive = asoc->default_timetolive;
  3740. } else {
  3741. info.sinfo_stream = sp->default_stream;
  3742. info.sinfo_flags = sp->default_flags;
  3743. info.sinfo_ppid = sp->default_ppid;
  3744. info.sinfo_context = sp->default_context;
  3745. info.sinfo_timetolive = sp->default_timetolive;
  3746. }
  3747. if (copy_to_user(optval, &info, sizeof(struct sctp_sndrcvinfo)))
  3748. return -EFAULT;
  3749. return 0;
  3750. }
  3751. /*
  3752. *
  3753. * 7.1.5 SCTP_NODELAY
  3754. *
  3755. * Turn on/off any Nagle-like algorithm. This means that packets are
  3756. * generally sent as soon as possible and no unnecessary delays are
  3757. * introduced, at the cost of more packets in the network. Expects an
  3758. * integer boolean flag.
  3759. */
  3760. static int sctp_getsockopt_nodelay(struct sock *sk, int len,
  3761. char __user *optval, int __user *optlen)
  3762. {
  3763. int val;
  3764. if (len < sizeof(int))
  3765. return -EINVAL;
  3766. len = sizeof(int);
  3767. val = (sctp_sk(sk)->nodelay == 1);
  3768. if (put_user(len, optlen))
  3769. return -EFAULT;
  3770. if (copy_to_user(optval, &val, len))
  3771. return -EFAULT;
  3772. return 0;
  3773. }
  3774. /*
  3775. *
  3776. * 7.1.1 SCTP_RTOINFO
  3777. *
  3778. * The protocol parameters used to initialize and bound retransmission
  3779. * timeout (RTO) are tunable. sctp_rtoinfo structure is used to access
  3780. * and modify these parameters.
  3781. * All parameters are time values, in milliseconds. A value of 0, when
  3782. * modifying the parameters, indicates that the current value should not
  3783. * be changed.
  3784. *
  3785. */
  3786. static int sctp_getsockopt_rtoinfo(struct sock *sk, int len,
  3787. char __user *optval,
  3788. int __user *optlen) {
  3789. struct sctp_rtoinfo rtoinfo;
  3790. struct sctp_association *asoc;
  3791. if (len != sizeof (struct sctp_rtoinfo))
  3792. return -EINVAL;
  3793. if (copy_from_user(&rtoinfo, optval, sizeof (struct sctp_rtoinfo)))
  3794. return -EFAULT;
  3795. asoc = sctp_id2assoc(sk, rtoinfo.srto_assoc_id);
  3796. if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
  3797. return -EINVAL;
  3798. /* Values corresponding to the specific association. */
  3799. if (asoc) {
  3800. rtoinfo.srto_initial = jiffies_to_msecs(asoc->rto_initial);
  3801. rtoinfo.srto_max = jiffies_to_msecs(asoc->rto_max);
  3802. rtoinfo.srto_min = jiffies_to_msecs(asoc->rto_min);
  3803. } else {
  3804. /* Values corresponding to the endpoint. */
  3805. struct sctp_sock *sp = sctp_sk(sk);
  3806. rtoinfo.srto_initial = sp->rtoinfo.srto_initial;
  3807. rtoinfo.srto_max = sp->rtoinfo.srto_max;
  3808. rtoinfo.srto_min = sp->rtoinfo.srto_min;
  3809. }
  3810. if (put_user(len, optlen))
  3811. return -EFAULT;
  3812. if (copy_to_user(optval, &rtoinfo, len))
  3813. return -EFAULT;
  3814. return 0;
  3815. }
  3816. /*
  3817. *
  3818. * 7.1.2 SCTP_ASSOCINFO
  3819. *
  3820. * This option is used to tune the the maximum retransmission attempts
  3821. * of the association.
  3822. * Returns an error if the new association retransmission value is
  3823. * greater than the sum of the retransmission value of the peer.
  3824. * See [SCTP] for more information.
  3825. *
  3826. */
  3827. static int sctp_getsockopt_associnfo(struct sock *sk, int len,
  3828. char __user *optval,
  3829. int __user *optlen)
  3830. {
  3831. struct sctp_assocparams assocparams;
  3832. struct sctp_association *asoc;
  3833. struct list_head *pos;
  3834. int cnt = 0;
  3835. if (len != sizeof (struct sctp_assocparams))
  3836. return -EINVAL;
  3837. if (copy_from_user(&assocparams, optval,
  3838. sizeof (struct sctp_assocparams)))
  3839. return -EFAULT;
  3840. asoc = sctp_id2assoc(sk, assocparams.sasoc_assoc_id);
  3841. if (!asoc && assocparams.sasoc_assoc_id && sctp_style(sk, UDP))
  3842. return -EINVAL;
  3843. /* Values correspoinding to the specific association */
  3844. if (asoc) {
  3845. assocparams.sasoc_asocmaxrxt = asoc->max_retrans;
  3846. assocparams.sasoc_peer_rwnd = asoc->peer.rwnd;
  3847. assocparams.sasoc_local_rwnd = asoc->a_rwnd;
  3848. assocparams.sasoc_cookie_life = (asoc->cookie_life.tv_sec
  3849. * 1000) +
  3850. (asoc->cookie_life.tv_usec
  3851. / 1000);
  3852. list_for_each(pos, &asoc->peer.transport_addr_list) {
  3853. cnt ++;
  3854. }
  3855. assocparams.sasoc_number_peer_destinations = cnt;
  3856. } else {
  3857. /* Values corresponding to the endpoint */
  3858. struct sctp_sock *sp = sctp_sk(sk);
  3859. assocparams.sasoc_asocmaxrxt = sp->assocparams.sasoc_asocmaxrxt;
  3860. assocparams.sasoc_peer_rwnd = sp->assocparams.sasoc_peer_rwnd;
  3861. assocparams.sasoc_local_rwnd = sp->assocparams.sasoc_local_rwnd;
  3862. assocparams.sasoc_cookie_life =
  3863. sp->assocparams.sasoc_cookie_life;
  3864. assocparams.sasoc_number_peer_destinations =
  3865. sp->assocparams.
  3866. sasoc_number_peer_destinations;
  3867. }
  3868. if (put_user(len, optlen))
  3869. return -EFAULT;
  3870. if (copy_to_user(optval, &assocparams, len))
  3871. return -EFAULT;
  3872. return 0;
  3873. }
  3874. /*
  3875. * 7.1.16 Set/clear IPv4 mapped addresses (SCTP_I_WANT_MAPPED_V4_ADDR)
  3876. *
  3877. * This socket option is a boolean flag which turns on or off mapped V4
  3878. * addresses. If this option is turned on and the socket is type
  3879. * PF_INET6, then IPv4 addresses will be mapped to V6 representation.
  3880. * If this option is turned off, then no mapping will be done of V4
  3881. * addresses and a user will receive both PF_INET6 and PF_INET type
  3882. * addresses on the socket.
  3883. */
  3884. static int sctp_getsockopt_mappedv4(struct sock *sk, int len,
  3885. char __user *optval, int __user *optlen)
  3886. {
  3887. int val;
  3888. struct sctp_sock *sp = sctp_sk(sk);
  3889. if (len < sizeof(int))
  3890. return -EINVAL;
  3891. len = sizeof(int);
  3892. val = sp->v4mapped;
  3893. if (put_user(len, optlen))
  3894. return -EFAULT;
  3895. if (copy_to_user(optval, &val, len))
  3896. return -EFAULT;
  3897. return 0;
  3898. }
  3899. /*
  3900. * 7.1.17 Set the maximum fragrmentation size (SCTP_MAXSEG)
  3901. *
  3902. * This socket option specifies the maximum size to put in any outgoing
  3903. * SCTP chunk. If a message is larger than this size it will be
  3904. * fragmented by SCTP into the specified size. Note that the underlying
  3905. * SCTP implementation may fragment into smaller sized chunks when the
  3906. * PMTU of the underlying association is smaller than the value set by
  3907. * the user.
  3908. */
  3909. static int sctp_getsockopt_maxseg(struct sock *sk, int len,
  3910. char __user *optval, int __user *optlen)
  3911. {
  3912. int val;
  3913. if (len < sizeof(int))
  3914. return -EINVAL;
  3915. len = sizeof(int);
  3916. val = sctp_sk(sk)->user_frag;
  3917. if (put_user(len, optlen))
  3918. return -EFAULT;
  3919. if (copy_to_user(optval, &val, len))
  3920. return -EFAULT;
  3921. return 0;
  3922. }
  3923. SCTP_STATIC int sctp_getsockopt(struct sock *sk, int level, int optname,
  3924. char __user *optval, int __user *optlen)
  3925. {
  3926. int retval = 0;
  3927. int len;
  3928. SCTP_DEBUG_PRINTK("sctp_getsockopt(sk: %p... optname: %d)\n",
  3929. sk, optname);
  3930. /* I can hardly begin to describe how wrong this is. This is
  3931. * so broken as to be worse than useless. The API draft
  3932. * REALLY is NOT helpful here... I am not convinced that the
  3933. * semantics of getsockopt() with a level OTHER THAN SOL_SCTP
  3934. * are at all well-founded.
  3935. */
  3936. if (level != SOL_SCTP) {
  3937. struct sctp_af *af = sctp_sk(sk)->pf->af;
  3938. retval = af->getsockopt(sk, level, optname, optval, optlen);
  3939. return retval;
  3940. }
  3941. if (get_user(len, optlen))
  3942. return -EFAULT;
  3943. sctp_lock_sock(sk);
  3944. switch (optname) {
  3945. case SCTP_STATUS:
  3946. retval = sctp_getsockopt_sctp_status(sk, len, optval, optlen);
  3947. break;
  3948. case SCTP_DISABLE_FRAGMENTS:
  3949. retval = sctp_getsockopt_disable_fragments(sk, len, optval,
  3950. optlen);
  3951. break;
  3952. case SCTP_EVENTS:
  3953. retval = sctp_getsockopt_events(sk, len, optval, optlen);
  3954. break;
  3955. case SCTP_AUTOCLOSE:
  3956. retval = sctp_getsockopt_autoclose(sk, len, optval, optlen);
  3957. break;
  3958. case SCTP_SOCKOPT_PEELOFF:
  3959. retval = sctp_getsockopt_peeloff(sk, len, optval, optlen);
  3960. break;
  3961. case SCTP_PEER_ADDR_PARAMS:
  3962. retval = sctp_getsockopt_peer_addr_params(sk, len, optval,
  3963. optlen);
  3964. break;
  3965. case SCTP_DELAYED_ACK_TIME:
  3966. retval = sctp_getsockopt_delayed_ack_time(sk, len, optval,
  3967. optlen);
  3968. break;
  3969. case SCTP_INITMSG:
  3970. retval = sctp_getsockopt_initmsg(sk, len, optval, optlen);
  3971. break;
  3972. case SCTP_GET_PEER_ADDRS_NUM_OLD:
  3973. retval = sctp_getsockopt_peer_addrs_num_old(sk, len, optval,
  3974. optlen);
  3975. break;
  3976. case SCTP_GET_LOCAL_ADDRS_NUM_OLD:
  3977. retval = sctp_getsockopt_local_addrs_num_old(sk, len, optval,
  3978. optlen);
  3979. break;
  3980. case SCTP_GET_PEER_ADDRS_OLD:
  3981. retval = sctp_getsockopt_peer_addrs_old(sk, len, optval,
  3982. optlen);
  3983. break;
  3984. case SCTP_GET_LOCAL_ADDRS_OLD:
  3985. retval = sctp_getsockopt_local_addrs_old(sk, len, optval,
  3986. optlen);
  3987. break;
  3988. case SCTP_GET_PEER_ADDRS:
  3989. retval = sctp_getsockopt_peer_addrs(sk, len, optval,
  3990. optlen);
  3991. break;
  3992. case SCTP_GET_LOCAL_ADDRS:
  3993. retval = sctp_getsockopt_local_addrs(sk, len, optval,
  3994. optlen);
  3995. break;
  3996. case SCTP_DEFAULT_SEND_PARAM:
  3997. retval = sctp_getsockopt_default_send_param(sk, len,
  3998. optval, optlen);
  3999. break;
  4000. case SCTP_PRIMARY_ADDR:
  4001. retval = sctp_getsockopt_primary_addr(sk, len, optval, optlen);
  4002. break;
  4003. case SCTP_NODELAY:
  4004. retval = sctp_getsockopt_nodelay(sk, len, optval, optlen);
  4005. break;
  4006. case SCTP_RTOINFO:
  4007. retval = sctp_getsockopt_rtoinfo(sk, len, optval, optlen);
  4008. break;
  4009. case SCTP_ASSOCINFO:
  4010. retval = sctp_getsockopt_associnfo(sk, len, optval, optlen);
  4011. break;
  4012. case SCTP_I_WANT_MAPPED_V4_ADDR:
  4013. retval = sctp_getsockopt_mappedv4(sk, len, optval, optlen);
  4014. break;
  4015. case SCTP_MAXSEG:
  4016. retval = sctp_getsockopt_maxseg(sk, len, optval, optlen);
  4017. break;
  4018. case SCTP_GET_PEER_ADDR_INFO:
  4019. retval = sctp_getsockopt_peer_addr_info(sk, len, optval,
  4020. optlen);
  4021. break;
  4022. case SCTP_ADAPTION_LAYER:
  4023. retval = sctp_getsockopt_adaption_layer(sk, len, optval,
  4024. optlen);
  4025. break;
  4026. default:
  4027. retval = -ENOPROTOOPT;
  4028. break;
  4029. };
  4030. sctp_release_sock(sk);
  4031. return retval;
  4032. }
  4033. static void sctp_hash(struct sock *sk)
  4034. {
  4035. /* STUB */
  4036. }
  4037. static void sctp_unhash(struct sock *sk)
  4038. {
  4039. /* STUB */
  4040. }
  4041. /* Check if port is acceptable. Possibly find first available port.
  4042. *
  4043. * The port hash table (contained in the 'global' SCTP protocol storage
  4044. * returned by struct sctp_protocol *sctp_get_protocol()). The hash
  4045. * table is an array of 4096 lists (sctp_bind_hashbucket). Each
  4046. * list (the list number is the port number hashed out, so as you
  4047. * would expect from a hash function, all the ports in a given list have
  4048. * such a number that hashes out to the same list number; you were
  4049. * expecting that, right?); so each list has a set of ports, with a
  4050. * link to the socket (struct sock) that uses it, the port number and
  4051. * a fastreuse flag (FIXME: NPI ipg).
  4052. */
  4053. static struct sctp_bind_bucket *sctp_bucket_create(
  4054. struct sctp_bind_hashbucket *head, unsigned short snum);
  4055. static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
  4056. {
  4057. struct sctp_bind_hashbucket *head; /* hash list */
  4058. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4059. unsigned short snum;
  4060. int ret;
  4061. /* NOTE: Remember to put this back to net order. */
  4062. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  4063. snum = addr->v4.sin_port;
  4064. SCTP_DEBUG_PRINTK("sctp_get_port() begins, snum=%d\n", snum);
  4065. sctp_local_bh_disable();
  4066. if (snum == 0) {
  4067. /* Search for an available port. */
  4068. int low, high, remaining, index;
  4069. unsigned int rover;
  4070. inet_get_local_port_range(&low, &high);
  4071. remaining = (high - low) + 1;
  4072. rover = net_random() % remaining + low;
  4073. do {
  4074. rover++;
  4075. if ((rover < low) || (rover > high))
  4076. rover = low;
  4077. index = sctp_phashfn(rover);
  4078. head = &sctp_port_hashtable[index];
  4079. sctp_spin_lock(&head->lock);
  4080. for (pp = head->chain; pp; pp = pp->next)
  4081. if (pp->port == rover)
  4082. goto next;
  4083. break;
  4084. next:
  4085. sctp_spin_unlock(&head->lock);
  4086. } while (--remaining > 0);
  4087. /* Exhausted local port range during search? */
  4088. ret = 1;
  4089. if (remaining <= 0)
  4090. goto fail;
  4091. /* OK, here is the one we will use. HEAD (the port
  4092. * hash table list entry) is non-NULL and we hold it's
  4093. * mutex.
  4094. */
  4095. snum = rover;
  4096. } else {
  4097. /* We are given an specific port number; we verify
  4098. * that it is not being used. If it is used, we will
  4099. * exahust the search in the hash list corresponding
  4100. * to the port number (snum) - we detect that with the
  4101. * port iterator, pp being NULL.
  4102. */
  4103. head = &sctp_port_hashtable[sctp_phashfn(snum)];
  4104. sctp_spin_lock(&head->lock);
  4105. for (pp = head->chain; pp; pp = pp->next) {
  4106. if (pp->port == snum)
  4107. goto pp_found;
  4108. }
  4109. }
  4110. pp = NULL;
  4111. goto pp_not_found;
  4112. pp_found:
  4113. if (!hlist_empty(&pp->owner)) {
  4114. /* We had a port hash table hit - there is an
  4115. * available port (pp != NULL) and it is being
  4116. * used by other socket (pp->owner not empty); that other
  4117. * socket is going to be sk2.
  4118. */
  4119. int reuse = sk->sk_reuse;
  4120. struct sock *sk2;
  4121. struct hlist_node *node;
  4122. SCTP_DEBUG_PRINTK("sctp_get_port() found a possible match\n");
  4123. if (pp->fastreuse && sk->sk_reuse)
  4124. goto success;
  4125. /* Run through the list of sockets bound to the port
  4126. * (pp->port) [via the pointers bind_next and
  4127. * bind_pprev in the struct sock *sk2 (pp->sk)]. On each one,
  4128. * we get the endpoint they describe and run through
  4129. * the endpoint's list of IP (v4 or v6) addresses,
  4130. * comparing each of the addresses with the address of
  4131. * the socket sk. If we find a match, then that means
  4132. * that this port/socket (sk) combination are already
  4133. * in an endpoint.
  4134. */
  4135. sk_for_each_bound(sk2, node, &pp->owner) {
  4136. struct sctp_endpoint *ep2;
  4137. ep2 = sctp_sk(sk2)->ep;
  4138. if (reuse && sk2->sk_reuse)
  4139. continue;
  4140. if (sctp_bind_addr_match(&ep2->base.bind_addr, addr,
  4141. sctp_sk(sk))) {
  4142. ret = (long)sk2;
  4143. goto fail_unlock;
  4144. }
  4145. }
  4146. SCTP_DEBUG_PRINTK("sctp_get_port(): Found a match\n");
  4147. }
  4148. pp_not_found:
  4149. /* If there was a hash table miss, create a new port. */
  4150. ret = 1;
  4151. if (!pp && !(pp = sctp_bucket_create(head, snum)))
  4152. goto fail_unlock;
  4153. /* In either case (hit or miss), make sure fastreuse is 1 only
  4154. * if sk->sk_reuse is too (that is, if the caller requested
  4155. * SO_REUSEADDR on this socket -sk-).
  4156. */
  4157. if (hlist_empty(&pp->owner))
  4158. pp->fastreuse = sk->sk_reuse ? 1 : 0;
  4159. else if (pp->fastreuse && !sk->sk_reuse)
  4160. pp->fastreuse = 0;
  4161. /* We are set, so fill up all the data in the hash table
  4162. * entry, tie the socket list information with the rest of the
  4163. * sockets FIXME: Blurry, NPI (ipg).
  4164. */
  4165. success:
  4166. inet_sk(sk)->num = snum;
  4167. if (!sctp_sk(sk)->bind_hash) {
  4168. sk_add_bind_node(sk, &pp->owner);
  4169. sctp_sk(sk)->bind_hash = pp;
  4170. }
  4171. ret = 0;
  4172. fail_unlock:
  4173. sctp_spin_unlock(&head->lock);
  4174. fail:
  4175. sctp_local_bh_enable();
  4176. addr->v4.sin_port = htons(addr->v4.sin_port);
  4177. return ret;
  4178. }
  4179. /* Assign a 'snum' port to the socket. If snum == 0, an ephemeral
  4180. * port is requested.
  4181. */
  4182. static int sctp_get_port(struct sock *sk, unsigned short snum)
  4183. {
  4184. long ret;
  4185. union sctp_addr addr;
  4186. struct sctp_af *af = sctp_sk(sk)->pf->af;
  4187. /* Set up a dummy address struct from the sk. */
  4188. af->from_sk(&addr, sk);
  4189. addr.v4.sin_port = htons(snum);
  4190. /* Note: sk->sk_num gets filled in if ephemeral port request. */
  4191. ret = sctp_get_port_local(sk, &addr);
  4192. return (ret ? 1 : 0);
  4193. }
  4194. /*
  4195. * 3.1.3 listen() - UDP Style Syntax
  4196. *
  4197. * By default, new associations are not accepted for UDP style sockets.
  4198. * An application uses listen() to mark a socket as being able to
  4199. * accept new associations.
  4200. */
  4201. SCTP_STATIC int sctp_seqpacket_listen(struct sock *sk, int backlog)
  4202. {
  4203. struct sctp_sock *sp = sctp_sk(sk);
  4204. struct sctp_endpoint *ep = sp->ep;
  4205. /* Only UDP style sockets that are not peeled off are allowed to
  4206. * listen().
  4207. */
  4208. if (!sctp_style(sk, UDP))
  4209. return -EINVAL;
  4210. /* If backlog is zero, disable listening. */
  4211. if (!backlog) {
  4212. if (sctp_sstate(sk, CLOSED))
  4213. return 0;
  4214. sctp_unhash_endpoint(ep);
  4215. sk->sk_state = SCTP_SS_CLOSED;
  4216. }
  4217. /* Return if we are already listening. */
  4218. if (sctp_sstate(sk, LISTENING))
  4219. return 0;
  4220. /*
  4221. * If a bind() or sctp_bindx() is not called prior to a listen()
  4222. * call that allows new associations to be accepted, the system
  4223. * picks an ephemeral port and will choose an address set equivalent
  4224. * to binding with a wildcard address.
  4225. *
  4226. * This is not currently spelled out in the SCTP sockets
  4227. * extensions draft, but follows the practice as seen in TCP
  4228. * sockets.
  4229. */
  4230. if (!ep->base.bind_addr.port) {
  4231. if (sctp_autobind(sk))
  4232. return -EAGAIN;
  4233. }
  4234. sk->sk_state = SCTP_SS_LISTENING;
  4235. sctp_hash_endpoint(ep);
  4236. return 0;
  4237. }
  4238. /*
  4239. * 4.1.3 listen() - TCP Style Syntax
  4240. *
  4241. * Applications uses listen() to ready the SCTP endpoint for accepting
  4242. * inbound associations.
  4243. */
  4244. SCTP_STATIC int sctp_stream_listen(struct sock *sk, int backlog)
  4245. {
  4246. struct sctp_sock *sp = sctp_sk(sk);
  4247. struct sctp_endpoint *ep = sp->ep;
  4248. /* If backlog is zero, disable listening. */
  4249. if (!backlog) {
  4250. if (sctp_sstate(sk, CLOSED))
  4251. return 0;
  4252. sctp_unhash_endpoint(ep);
  4253. sk->sk_state = SCTP_SS_CLOSED;
  4254. }
  4255. if (sctp_sstate(sk, LISTENING))
  4256. return 0;
  4257. /*
  4258. * If a bind() or sctp_bindx() is not called prior to a listen()
  4259. * call that allows new associations to be accepted, the system
  4260. * picks an ephemeral port and will choose an address set equivalent
  4261. * to binding with a wildcard address.
  4262. *
  4263. * This is not currently spelled out in the SCTP sockets
  4264. * extensions draft, but follows the practice as seen in TCP
  4265. * sockets.
  4266. */
  4267. if (!ep->base.bind_addr.port) {
  4268. if (sctp_autobind(sk))
  4269. return -EAGAIN;
  4270. }
  4271. sk->sk_state = SCTP_SS_LISTENING;
  4272. sk->sk_max_ack_backlog = backlog;
  4273. sctp_hash_endpoint(ep);
  4274. return 0;
  4275. }
  4276. /*
  4277. * Move a socket to LISTENING state.
  4278. */
  4279. int sctp_inet_listen(struct socket *sock, int backlog)
  4280. {
  4281. struct sock *sk = sock->sk;
  4282. struct crypto_tfm *tfm=NULL;
  4283. int err = -EINVAL;
  4284. if (unlikely(backlog < 0))
  4285. goto out;
  4286. sctp_lock_sock(sk);
  4287. if (sock->state != SS_UNCONNECTED)
  4288. goto out;
  4289. /* Allocate HMAC for generating cookie. */
  4290. if (sctp_hmac_alg) {
  4291. tfm = sctp_crypto_alloc_tfm(sctp_hmac_alg, 0);
  4292. if (!tfm) {
  4293. err = -ENOSYS;
  4294. goto out;
  4295. }
  4296. }
  4297. switch (sock->type) {
  4298. case SOCK_SEQPACKET:
  4299. err = sctp_seqpacket_listen(sk, backlog);
  4300. break;
  4301. case SOCK_STREAM:
  4302. err = sctp_stream_listen(sk, backlog);
  4303. break;
  4304. default:
  4305. break;
  4306. };
  4307. if (err)
  4308. goto cleanup;
  4309. /* Store away the transform reference. */
  4310. sctp_sk(sk)->hmac = tfm;
  4311. out:
  4312. sctp_release_sock(sk);
  4313. return err;
  4314. cleanup:
  4315. sctp_crypto_free_tfm(tfm);
  4316. goto out;
  4317. }
  4318. /*
  4319. * This function is done by modeling the current datagram_poll() and the
  4320. * tcp_poll(). Note that, based on these implementations, we don't
  4321. * lock the socket in this function, even though it seems that,
  4322. * ideally, locking or some other mechanisms can be used to ensure
  4323. * the integrity of the counters (sndbuf and wmem_alloc) used
  4324. * in this place. We assume that we don't need locks either until proven
  4325. * otherwise.
  4326. *
  4327. * Another thing to note is that we include the Async I/O support
  4328. * here, again, by modeling the current TCP/UDP code. We don't have
  4329. * a good way to test with it yet.
  4330. */
  4331. unsigned int sctp_poll(struct file *file, struct socket *sock, poll_table *wait)
  4332. {
  4333. struct sock *sk = sock->sk;
  4334. struct sctp_sock *sp = sctp_sk(sk);
  4335. unsigned int mask;
  4336. poll_wait(file, sk->sk_sleep, wait);
  4337. /* A TCP-style listening socket becomes readable when the accept queue
  4338. * is not empty.
  4339. */
  4340. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
  4341. return (!list_empty(&sp->ep->asocs)) ?
  4342. (POLLIN | POLLRDNORM) : 0;
  4343. mask = 0;
  4344. /* Is there any exceptional events? */
  4345. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue))
  4346. mask |= POLLERR;
  4347. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4348. mask |= POLLRDHUP;
  4349. if (sk->sk_shutdown == SHUTDOWN_MASK)
  4350. mask |= POLLHUP;
  4351. /* Is it readable? Reconsider this code with TCP-style support. */
  4352. if (!skb_queue_empty(&sk->sk_receive_queue) ||
  4353. (sk->sk_shutdown & RCV_SHUTDOWN))
  4354. mask |= POLLIN | POLLRDNORM;
  4355. /* The association is either gone or not ready. */
  4356. if (!sctp_style(sk, UDP) && sctp_sstate(sk, CLOSED))
  4357. return mask;
  4358. /* Is it writable? */
  4359. if (sctp_writeable(sk)) {
  4360. mask |= POLLOUT | POLLWRNORM;
  4361. } else {
  4362. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  4363. /*
  4364. * Since the socket is not locked, the buffer
  4365. * might be made available after the writeable check and
  4366. * before the bit is set. This could cause a lost I/O
  4367. * signal. tcp_poll() has a race breaker for this race
  4368. * condition. Based on their implementation, we put
  4369. * in the following code to cover it as well.
  4370. */
  4371. if (sctp_writeable(sk))
  4372. mask |= POLLOUT | POLLWRNORM;
  4373. }
  4374. return mask;
  4375. }
  4376. /********************************************************************
  4377. * 2nd Level Abstractions
  4378. ********************************************************************/
  4379. static struct sctp_bind_bucket *sctp_bucket_create(
  4380. struct sctp_bind_hashbucket *head, unsigned short snum)
  4381. {
  4382. struct sctp_bind_bucket *pp;
  4383. pp = kmem_cache_alloc(sctp_bucket_cachep, SLAB_ATOMIC);
  4384. SCTP_DBG_OBJCNT_INC(bind_bucket);
  4385. if (pp) {
  4386. pp->port = snum;
  4387. pp->fastreuse = 0;
  4388. INIT_HLIST_HEAD(&pp->owner);
  4389. if ((pp->next = head->chain) != NULL)
  4390. pp->next->pprev = &pp->next;
  4391. head->chain = pp;
  4392. pp->pprev = &head->chain;
  4393. }
  4394. return pp;
  4395. }
  4396. /* Caller must hold hashbucket lock for this tb with local BH disabled */
  4397. static void sctp_bucket_destroy(struct sctp_bind_bucket *pp)
  4398. {
  4399. if (pp && hlist_empty(&pp->owner)) {
  4400. if (pp->next)
  4401. pp->next->pprev = pp->pprev;
  4402. *(pp->pprev) = pp->next;
  4403. kmem_cache_free(sctp_bucket_cachep, pp);
  4404. SCTP_DBG_OBJCNT_DEC(bind_bucket);
  4405. }
  4406. }
  4407. /* Release this socket's reference to a local port. */
  4408. static inline void __sctp_put_port(struct sock *sk)
  4409. {
  4410. struct sctp_bind_hashbucket *head =
  4411. &sctp_port_hashtable[sctp_phashfn(inet_sk(sk)->num)];
  4412. struct sctp_bind_bucket *pp;
  4413. sctp_spin_lock(&head->lock);
  4414. pp = sctp_sk(sk)->bind_hash;
  4415. __sk_del_bind_node(sk);
  4416. sctp_sk(sk)->bind_hash = NULL;
  4417. inet_sk(sk)->num = 0;
  4418. sctp_bucket_destroy(pp);
  4419. sctp_spin_unlock(&head->lock);
  4420. }
  4421. void sctp_put_port(struct sock *sk)
  4422. {
  4423. sctp_local_bh_disable();
  4424. __sctp_put_port(sk);
  4425. sctp_local_bh_enable();
  4426. }
  4427. /*
  4428. * The system picks an ephemeral port and choose an address set equivalent
  4429. * to binding with a wildcard address.
  4430. * One of those addresses will be the primary address for the association.
  4431. * This automatically enables the multihoming capability of SCTP.
  4432. */
  4433. static int sctp_autobind(struct sock *sk)
  4434. {
  4435. union sctp_addr autoaddr;
  4436. struct sctp_af *af;
  4437. unsigned short port;
  4438. /* Initialize a local sockaddr structure to INADDR_ANY. */
  4439. af = sctp_sk(sk)->pf->af;
  4440. port = htons(inet_sk(sk)->num);
  4441. af->inaddr_any(&autoaddr, port);
  4442. return sctp_do_bind(sk, &autoaddr, af->sockaddr_len);
  4443. }
  4444. /* Parse out IPPROTO_SCTP CMSG headers. Perform only minimal validation.
  4445. *
  4446. * From RFC 2292
  4447. * 4.2 The cmsghdr Structure *
  4448. *
  4449. * When ancillary data is sent or received, any number of ancillary data
  4450. * objects can be specified by the msg_control and msg_controllen members of
  4451. * the msghdr structure, because each object is preceded by
  4452. * a cmsghdr structure defining the object's length (the cmsg_len member).
  4453. * Historically Berkeley-derived implementations have passed only one object
  4454. * at a time, but this API allows multiple objects to be
  4455. * passed in a single call to sendmsg() or recvmsg(). The following example
  4456. * shows two ancillary data objects in a control buffer.
  4457. *
  4458. * |<--------------------------- msg_controllen -------------------------->|
  4459. * | |
  4460. *
  4461. * |<----- ancillary data object ----->|<----- ancillary data object ----->|
  4462. *
  4463. * |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  4464. * | | |
  4465. *
  4466. * |<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
  4467. *
  4468. * |<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
  4469. * | | | | |
  4470. *
  4471. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4472. * |cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX|
  4473. *
  4474. * |len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX|
  4475. *
  4476. * +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  4477. * ^
  4478. * |
  4479. *
  4480. * msg_control
  4481. * points here
  4482. */
  4483. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *msg,
  4484. sctp_cmsgs_t *cmsgs)
  4485. {
  4486. struct cmsghdr *cmsg;
  4487. for (cmsg = CMSG_FIRSTHDR(msg);
  4488. cmsg != NULL;
  4489. cmsg = CMSG_NXTHDR((struct msghdr*)msg, cmsg)) {
  4490. if (!CMSG_OK(msg, cmsg))
  4491. return -EINVAL;
  4492. /* Should we parse this header or ignore? */
  4493. if (cmsg->cmsg_level != IPPROTO_SCTP)
  4494. continue;
  4495. /* Strictly check lengths following example in SCM code. */
  4496. switch (cmsg->cmsg_type) {
  4497. case SCTP_INIT:
  4498. /* SCTP Socket API Extension
  4499. * 5.2.1 SCTP Initiation Structure (SCTP_INIT)
  4500. *
  4501. * This cmsghdr structure provides information for
  4502. * initializing new SCTP associations with sendmsg().
  4503. * The SCTP_INITMSG socket option uses this same data
  4504. * structure. This structure is not used for
  4505. * recvmsg().
  4506. *
  4507. * cmsg_level cmsg_type cmsg_data[]
  4508. * ------------ ------------ ----------------------
  4509. * IPPROTO_SCTP SCTP_INIT struct sctp_initmsg
  4510. */
  4511. if (cmsg->cmsg_len !=
  4512. CMSG_LEN(sizeof(struct sctp_initmsg)))
  4513. return -EINVAL;
  4514. cmsgs->init = (struct sctp_initmsg *)CMSG_DATA(cmsg);
  4515. break;
  4516. case SCTP_SNDRCV:
  4517. /* SCTP Socket API Extension
  4518. * 5.2.2 SCTP Header Information Structure(SCTP_SNDRCV)
  4519. *
  4520. * This cmsghdr structure specifies SCTP options for
  4521. * sendmsg() and describes SCTP header information
  4522. * about a received message through recvmsg().
  4523. *
  4524. * cmsg_level cmsg_type cmsg_data[]
  4525. * ------------ ------------ ----------------------
  4526. * IPPROTO_SCTP SCTP_SNDRCV struct sctp_sndrcvinfo
  4527. */
  4528. if (cmsg->cmsg_len !=
  4529. CMSG_LEN(sizeof(struct sctp_sndrcvinfo)))
  4530. return -EINVAL;
  4531. cmsgs->info =
  4532. (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
  4533. /* Minimally, validate the sinfo_flags. */
  4534. if (cmsgs->info->sinfo_flags &
  4535. ~(SCTP_UNORDERED | SCTP_ADDR_OVER |
  4536. SCTP_ABORT | SCTP_EOF))
  4537. return -EINVAL;
  4538. break;
  4539. default:
  4540. return -EINVAL;
  4541. };
  4542. }
  4543. return 0;
  4544. }
  4545. /*
  4546. * Wait for a packet..
  4547. * Note: This function is the same function as in core/datagram.c
  4548. * with a few modifications to make lksctp work.
  4549. */
  4550. static int sctp_wait_for_packet(struct sock * sk, int *err, long *timeo_p)
  4551. {
  4552. int error;
  4553. DEFINE_WAIT(wait);
  4554. prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4555. /* Socket errors? */
  4556. error = sock_error(sk);
  4557. if (error)
  4558. goto out;
  4559. if (!skb_queue_empty(&sk->sk_receive_queue))
  4560. goto ready;
  4561. /* Socket shut down? */
  4562. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4563. goto out;
  4564. /* Sequenced packets can come disconnected. If so we report the
  4565. * problem.
  4566. */
  4567. error = -ENOTCONN;
  4568. /* Is there a good reason to think that we may receive some data? */
  4569. if (list_empty(&sctp_sk(sk)->ep->asocs) && !sctp_sstate(sk, LISTENING))
  4570. goto out;
  4571. /* Handle signals. */
  4572. if (signal_pending(current))
  4573. goto interrupted;
  4574. /* Let another process have a go. Since we are going to sleep
  4575. * anyway. Note: This may cause odd behaviors if the message
  4576. * does not fit in the user's buffer, but this seems to be the
  4577. * only way to honor MSG_DONTWAIT realistically.
  4578. */
  4579. sctp_release_sock(sk);
  4580. *timeo_p = schedule_timeout(*timeo_p);
  4581. sctp_lock_sock(sk);
  4582. ready:
  4583. finish_wait(sk->sk_sleep, &wait);
  4584. return 0;
  4585. interrupted:
  4586. error = sock_intr_errno(*timeo_p);
  4587. out:
  4588. finish_wait(sk->sk_sleep, &wait);
  4589. *err = error;
  4590. return error;
  4591. }
  4592. /* Receive a datagram.
  4593. * Note: This is pretty much the same routine as in core/datagram.c
  4594. * with a few changes to make lksctp work.
  4595. */
  4596. static struct sk_buff *sctp_skb_recv_datagram(struct sock *sk, int flags,
  4597. int noblock, int *err)
  4598. {
  4599. int error;
  4600. struct sk_buff *skb;
  4601. long timeo;
  4602. timeo = sock_rcvtimeo(sk, noblock);
  4603. SCTP_DEBUG_PRINTK("Timeout: timeo: %ld, MAX: %ld.\n",
  4604. timeo, MAX_SCHEDULE_TIMEOUT);
  4605. do {
  4606. /* Again only user level code calls this function,
  4607. * so nothing interrupt level
  4608. * will suddenly eat the receive_queue.
  4609. *
  4610. * Look at current nfs client by the way...
  4611. * However, this function was corrent in any case. 8)
  4612. */
  4613. if (flags & MSG_PEEK) {
  4614. spin_lock_bh(&sk->sk_receive_queue.lock);
  4615. skb = skb_peek(&sk->sk_receive_queue);
  4616. if (skb)
  4617. atomic_inc(&skb->users);
  4618. spin_unlock_bh(&sk->sk_receive_queue.lock);
  4619. } else {
  4620. skb = skb_dequeue(&sk->sk_receive_queue);
  4621. }
  4622. if (skb)
  4623. return skb;
  4624. /* Caller is allowed not to check sk->sk_err before calling. */
  4625. error = sock_error(sk);
  4626. if (error)
  4627. goto no_packet;
  4628. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4629. break;
  4630. /* User doesn't want to wait. */
  4631. error = -EAGAIN;
  4632. if (!timeo)
  4633. goto no_packet;
  4634. } while (sctp_wait_for_packet(sk, err, &timeo) == 0);
  4635. return NULL;
  4636. no_packet:
  4637. *err = error;
  4638. return NULL;
  4639. }
  4640. /* If sndbuf has changed, wake up per association sndbuf waiters. */
  4641. static void __sctp_write_space(struct sctp_association *asoc)
  4642. {
  4643. struct sock *sk = asoc->base.sk;
  4644. struct socket *sock = sk->sk_socket;
  4645. if ((sctp_wspace(asoc) > 0) && sock) {
  4646. if (waitqueue_active(&asoc->wait))
  4647. wake_up_interruptible(&asoc->wait);
  4648. if (sctp_writeable(sk)) {
  4649. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  4650. wake_up_interruptible(sk->sk_sleep);
  4651. /* Note that we try to include the Async I/O support
  4652. * here by modeling from the current TCP/UDP code.
  4653. * We have not tested with it yet.
  4654. */
  4655. if (sock->fasync_list &&
  4656. !(sk->sk_shutdown & SEND_SHUTDOWN))
  4657. sock_wake_async(sock, 2, POLL_OUT);
  4658. }
  4659. }
  4660. }
  4661. /* Do accounting for the sndbuf space.
  4662. * Decrement the used sndbuf space of the corresponding association by the
  4663. * data size which was just transmitted(freed).
  4664. */
  4665. static void sctp_wfree(struct sk_buff *skb)
  4666. {
  4667. struct sctp_association *asoc;
  4668. struct sctp_chunk *chunk;
  4669. struct sock *sk;
  4670. /* Get the saved chunk pointer. */
  4671. chunk = *((struct sctp_chunk **)(skb->cb));
  4672. asoc = chunk->asoc;
  4673. sk = asoc->base.sk;
  4674. asoc->sndbuf_used -= SCTP_DATA_SNDSIZE(chunk) +
  4675. sizeof(struct sk_buff) +
  4676. sizeof(struct sctp_chunk);
  4677. atomic_sub(sizeof(struct sctp_chunk), &sk->sk_wmem_alloc);
  4678. /*
  4679. * This undoes what is done via sctp_set_owner_w and sk_mem_charge
  4680. */
  4681. sk->sk_wmem_queued -= skb->truesize;
  4682. sk_mem_uncharge(sk, skb->truesize);
  4683. sock_wfree(skb);
  4684. __sctp_write_space(asoc);
  4685. sctp_association_put(asoc);
  4686. }
  4687. /* Do accounting for the receive space on the socket.
  4688. * Accounting for the association is done in ulpevent.c
  4689. * We set this as a destructor for the cloned data skbs so that
  4690. * accounting is done at the correct time.
  4691. */
  4692. void sctp_sock_rfree(struct sk_buff *skb)
  4693. {
  4694. struct sock *sk = skb->sk;
  4695. struct sctp_ulpevent *event = sctp_skb2event(skb);
  4696. atomic_sub(event->rmem_len, &sk->sk_rmem_alloc);
  4697. /*
  4698. * Mimic the behavior of sock_rfree
  4699. */
  4700. sk_mem_uncharge(sk, event->rmem_len);
  4701. }
  4702. /* Helper function to wait for space in the sndbuf. */
  4703. static int sctp_wait_for_sndbuf(struct sctp_association *asoc, long *timeo_p,
  4704. size_t msg_len)
  4705. {
  4706. struct sock *sk = asoc->base.sk;
  4707. int err = 0;
  4708. long current_timeo = *timeo_p;
  4709. DEFINE_WAIT(wait);
  4710. SCTP_DEBUG_PRINTK("wait_for_sndbuf: asoc=%p, timeo=%ld, msg_len=%zu\n",
  4711. asoc, (long)(*timeo_p), msg_len);
  4712. /* Increment the association's refcnt. */
  4713. sctp_association_hold(asoc);
  4714. /* Wait on the association specific sndbuf space. */
  4715. for (;;) {
  4716. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4717. TASK_INTERRUPTIBLE);
  4718. if (!*timeo_p)
  4719. goto do_nonblock;
  4720. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4721. asoc->base.dead)
  4722. goto do_error;
  4723. if (signal_pending(current))
  4724. goto do_interrupted;
  4725. if (msg_len <= sctp_wspace(asoc))
  4726. break;
  4727. /* Let another process have a go. Since we are going
  4728. * to sleep anyway.
  4729. */
  4730. sctp_release_sock(sk);
  4731. current_timeo = schedule_timeout(current_timeo);
  4732. BUG_ON(sk != asoc->base.sk);
  4733. sctp_lock_sock(sk);
  4734. *timeo_p = current_timeo;
  4735. }
  4736. out:
  4737. finish_wait(&asoc->wait, &wait);
  4738. /* Release the association's refcnt. */
  4739. sctp_association_put(asoc);
  4740. return err;
  4741. do_error:
  4742. err = -EPIPE;
  4743. goto out;
  4744. do_interrupted:
  4745. err = sock_intr_errno(*timeo_p);
  4746. goto out;
  4747. do_nonblock:
  4748. err = -EAGAIN;
  4749. goto out;
  4750. }
  4751. /* If socket sndbuf has changed, wake up all per association waiters. */
  4752. void sctp_write_space(struct sock *sk)
  4753. {
  4754. struct sctp_association *asoc;
  4755. struct list_head *pos;
  4756. /* Wake up the tasks in each wait queue. */
  4757. list_for_each(pos, &((sctp_sk(sk))->ep->asocs)) {
  4758. asoc = list_entry(pos, struct sctp_association, asocs);
  4759. __sctp_write_space(asoc);
  4760. }
  4761. }
  4762. /* Is there any sndbuf space available on the socket?
  4763. *
  4764. * Note that sk_wmem_alloc is the sum of the send buffers on all of the
  4765. * associations on the same socket. For a UDP-style socket with
  4766. * multiple associations, it is possible for it to be "unwriteable"
  4767. * prematurely. I assume that this is acceptable because
  4768. * a premature "unwriteable" is better than an accidental "writeable" which
  4769. * would cause an unwanted block under certain circumstances. For the 1-1
  4770. * UDP-style sockets or TCP-style sockets, this code should work.
  4771. * - Daisy
  4772. */
  4773. static int sctp_writeable(struct sock *sk)
  4774. {
  4775. int amt = 0;
  4776. amt = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
  4777. if (amt < 0)
  4778. amt = 0;
  4779. return amt;
  4780. }
  4781. /* Wait for an association to go into ESTABLISHED state. If timeout is 0,
  4782. * returns immediately with EINPROGRESS.
  4783. */
  4784. static int sctp_wait_for_connect(struct sctp_association *asoc, long *timeo_p)
  4785. {
  4786. struct sock *sk = asoc->base.sk;
  4787. int err = 0;
  4788. long current_timeo = *timeo_p;
  4789. DEFINE_WAIT(wait);
  4790. SCTP_DEBUG_PRINTK("%s: asoc=%p, timeo=%ld\n", __FUNCTION__, asoc,
  4791. (long)(*timeo_p));
  4792. /* Increment the association's refcnt. */
  4793. sctp_association_hold(asoc);
  4794. for (;;) {
  4795. prepare_to_wait_exclusive(&asoc->wait, &wait,
  4796. TASK_INTERRUPTIBLE);
  4797. if (!*timeo_p)
  4798. goto do_nonblock;
  4799. if (sk->sk_shutdown & RCV_SHUTDOWN)
  4800. break;
  4801. if (sk->sk_err || asoc->state >= SCTP_STATE_SHUTDOWN_PENDING ||
  4802. asoc->base.dead)
  4803. goto do_error;
  4804. if (signal_pending(current))
  4805. goto do_interrupted;
  4806. if (sctp_state(asoc, ESTABLISHED))
  4807. break;
  4808. /* Let another process have a go. Since we are going
  4809. * to sleep anyway.
  4810. */
  4811. sctp_release_sock(sk);
  4812. current_timeo = schedule_timeout(current_timeo);
  4813. sctp_lock_sock(sk);
  4814. *timeo_p = current_timeo;
  4815. }
  4816. out:
  4817. finish_wait(&asoc->wait, &wait);
  4818. /* Release the association's refcnt. */
  4819. sctp_association_put(asoc);
  4820. return err;
  4821. do_error:
  4822. if (asoc->init_err_counter + 1 > asoc->max_init_attempts)
  4823. err = -ETIMEDOUT;
  4824. else
  4825. err = -ECONNREFUSED;
  4826. goto out;
  4827. do_interrupted:
  4828. err = sock_intr_errno(*timeo_p);
  4829. goto out;
  4830. do_nonblock:
  4831. err = -EINPROGRESS;
  4832. goto out;
  4833. }
  4834. static int sctp_wait_for_accept(struct sock *sk, long timeo)
  4835. {
  4836. struct sctp_endpoint *ep;
  4837. int err = 0;
  4838. DEFINE_WAIT(wait);
  4839. ep = sctp_sk(sk)->ep;
  4840. for (;;) {
  4841. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  4842. TASK_INTERRUPTIBLE);
  4843. if (list_empty(&ep->asocs)) {
  4844. sctp_release_sock(sk);
  4845. timeo = schedule_timeout(timeo);
  4846. sctp_lock_sock(sk);
  4847. }
  4848. err = -EINVAL;
  4849. if (!sctp_sstate(sk, LISTENING))
  4850. break;
  4851. err = 0;
  4852. if (!list_empty(&ep->asocs))
  4853. break;
  4854. err = sock_intr_errno(timeo);
  4855. if (signal_pending(current))
  4856. break;
  4857. err = -EAGAIN;
  4858. if (!timeo)
  4859. break;
  4860. }
  4861. finish_wait(sk->sk_sleep, &wait);
  4862. return err;
  4863. }
  4864. void sctp_wait_for_close(struct sock *sk, long timeout)
  4865. {
  4866. DEFINE_WAIT(wait);
  4867. do {
  4868. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  4869. if (list_empty(&sctp_sk(sk)->ep->asocs))
  4870. break;
  4871. sctp_release_sock(sk);
  4872. timeout = schedule_timeout(timeout);
  4873. sctp_lock_sock(sk);
  4874. } while (!signal_pending(current) && timeout);
  4875. finish_wait(sk->sk_sleep, &wait);
  4876. }
  4877. static void sctp_sock_rfree_frag(struct sk_buff *skb)
  4878. {
  4879. struct sk_buff *frag;
  4880. if (!skb->data_len)
  4881. goto done;
  4882. /* Don't forget the fragments. */
  4883. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
  4884. sctp_sock_rfree_frag(frag);
  4885. done:
  4886. sctp_sock_rfree(skb);
  4887. }
  4888. static void sctp_skb_set_owner_r_frag(struct sk_buff *skb, struct sock *sk)
  4889. {
  4890. struct sk_buff *frag;
  4891. if (!skb->data_len)
  4892. goto done;
  4893. /* Don't forget the fragments. */
  4894. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next)
  4895. sctp_skb_set_owner_r_frag(frag, sk);
  4896. done:
  4897. sctp_skb_set_owner_r(skb, sk);
  4898. }
  4899. /* Populate the fields of the newsk from the oldsk and migrate the assoc
  4900. * and its messages to the newsk.
  4901. */
  4902. static void sctp_sock_migrate(struct sock *oldsk, struct sock *newsk,
  4903. struct sctp_association *assoc,
  4904. sctp_socket_type_t type)
  4905. {
  4906. struct sctp_sock *oldsp = sctp_sk(oldsk);
  4907. struct sctp_sock *newsp = sctp_sk(newsk);
  4908. struct sctp_bind_bucket *pp; /* hash list port iterator */
  4909. struct sctp_endpoint *newep = newsp->ep;
  4910. struct sk_buff *skb, *tmp;
  4911. struct sctp_ulpevent *event;
  4912. struct sctp_bind_hashbucket *head;
  4913. int flags = 0;
  4914. /* Migrate socket buffer sizes and all the socket level options to the
  4915. * new socket.
  4916. */
  4917. newsk->sk_sndbuf = oldsk->sk_sndbuf;
  4918. newsk->sk_rcvbuf = oldsk->sk_rcvbuf;
  4919. /* Brute force copy old sctp opt. */
  4920. inet_sk_copy_descendant(newsk, oldsk);
  4921. /* Restore the ep value that was overwritten with the above structure
  4922. * copy.
  4923. */
  4924. newsp->ep = newep;
  4925. newsp->hmac = NULL;
  4926. /* Hook this new socket in to the bind_hash list. */
  4927. head = &sctp_port_hashtable[sctp_phashfn(inet_sk(oldsk)->num)];
  4928. sctp_local_bh_disable();
  4929. sctp_spin_lock(&head->lock);
  4930. pp = sctp_sk(oldsk)->bind_hash;
  4931. sk_add_bind_node(newsk, &pp->owner);
  4932. sctp_sk(newsk)->bind_hash = pp;
  4933. inet_sk(newsk)->num = inet_sk(oldsk)->num;
  4934. sctp_spin_unlock(&head->lock);
  4935. sctp_local_bh_enable();
  4936. /* Copy the bind_addr list from the original endpoint to the new
  4937. * endpoint so that we can handle restarts properly
  4938. */
  4939. if (PF_INET6 == assoc->base.sk->sk_family)
  4940. flags = SCTP_ADDR6_ALLOWED;
  4941. if (assoc->peer.ipv4_address)
  4942. flags |= SCTP_ADDR4_PEERSUPP;
  4943. if (assoc->peer.ipv6_address)
  4944. flags |= SCTP_ADDR6_PEERSUPP;
  4945. sctp_bind_addr_copy(&newsp->ep->base.bind_addr,
  4946. &oldsp->ep->base.bind_addr,
  4947. SCTP_SCOPE_GLOBAL, GFP_KERNEL, flags);
  4948. /* Move any messages in the old socket's receive queue that are for the
  4949. * peeled off association to the new socket's receive queue.
  4950. */
  4951. sctp_skb_for_each(skb, &oldsk->sk_receive_queue, tmp) {
  4952. event = sctp_skb2event(skb);
  4953. if (event->asoc == assoc) {
  4954. sctp_sock_rfree_frag(skb);
  4955. __skb_unlink(skb, &oldsk->sk_receive_queue);
  4956. __skb_queue_tail(&newsk->sk_receive_queue, skb);
  4957. sctp_skb_set_owner_r_frag(skb, newsk);
  4958. }
  4959. }
  4960. /* Clean up any messages pending delivery due to partial
  4961. * delivery. Three cases:
  4962. * 1) No partial deliver; no work.
  4963. * 2) Peeling off partial delivery; keep pd_lobby in new pd_lobby.
  4964. * 3) Peeling off non-partial delivery; move pd_lobby to receive_queue.
  4965. */
  4966. skb_queue_head_init(&newsp->pd_lobby);
  4967. sctp_sk(newsk)->pd_mode = assoc->ulpq.pd_mode;
  4968. if (sctp_sk(oldsk)->pd_mode) {
  4969. struct sk_buff_head *queue;
  4970. /* Decide which queue to move pd_lobby skbs to. */
  4971. if (assoc->ulpq.pd_mode) {
  4972. queue = &newsp->pd_lobby;
  4973. } else
  4974. queue = &newsk->sk_receive_queue;
  4975. /* Walk through the pd_lobby, looking for skbs that
  4976. * need moved to the new socket.
  4977. */
  4978. sctp_skb_for_each(skb, &oldsp->pd_lobby, tmp) {
  4979. event = sctp_skb2event(skb);
  4980. if (event->asoc == assoc) {
  4981. sctp_sock_rfree_frag(skb);
  4982. __skb_unlink(skb, &oldsp->pd_lobby);
  4983. __skb_queue_tail(queue, skb);
  4984. sctp_skb_set_owner_r_frag(skb, newsk);
  4985. }
  4986. }
  4987. /* Clear up any skbs waiting for the partial
  4988. * delivery to finish.
  4989. */
  4990. if (assoc->ulpq.pd_mode)
  4991. sctp_clear_pd(oldsk);
  4992. }
  4993. /* Set the type of socket to indicate that it is peeled off from the
  4994. * original UDP-style socket or created with the accept() call on a
  4995. * TCP-style socket..
  4996. */
  4997. newsp->type = type;
  4998. /* Mark the new socket "in-use" by the user so that any packets
  4999. * that may arrive on the association after we've moved it are
  5000. * queued to the backlog. This prevents a potential race between
  5001. * backlog processing on the old socket and new-packet processing
  5002. * on the new socket.
  5003. */
  5004. sctp_lock_sock(newsk);
  5005. sctp_assoc_migrate(assoc, newsk);
  5006. /* If the association on the newsk is already closed before accept()
  5007. * is called, set RCV_SHUTDOWN flag.
  5008. */
  5009. if (sctp_state(assoc, CLOSED) && sctp_style(newsk, TCP))
  5010. newsk->sk_shutdown |= RCV_SHUTDOWN;
  5011. newsk->sk_state = SCTP_SS_ESTABLISHED;
  5012. sctp_release_sock(newsk);
  5013. }
  5014. /* This proto struct describes the ULP interface for SCTP. */
  5015. struct proto sctp_prot = {
  5016. .name = "SCTP",
  5017. .owner = THIS_MODULE,
  5018. .close = sctp_close,
  5019. .connect = sctp_connect,
  5020. .disconnect = sctp_disconnect,
  5021. .accept = sctp_accept,
  5022. .ioctl = sctp_ioctl,
  5023. .init = sctp_init_sock,
  5024. .destroy = sctp_destroy_sock,
  5025. .shutdown = sctp_shutdown,
  5026. .setsockopt = sctp_setsockopt,
  5027. .getsockopt = sctp_getsockopt,
  5028. .sendmsg = sctp_sendmsg,
  5029. .recvmsg = sctp_recvmsg,
  5030. .bind = sctp_bind,
  5031. .backlog_rcv = sctp_backlog_rcv,
  5032. .hash = sctp_hash,
  5033. .unhash = sctp_unhash,
  5034. .get_port = sctp_get_port,
  5035. .obj_size = sizeof(struct sctp_sock),
  5036. .sysctl_mem = sysctl_sctp_mem,
  5037. .sysctl_rmem = sysctl_sctp_rmem,
  5038. .sysctl_wmem = sysctl_sctp_wmem,
  5039. .memory_pressure = &sctp_memory_pressure,
  5040. .enter_memory_pressure = sctp_enter_memory_pressure,
  5041. .memory_allocated = &sctp_memory_allocated,
  5042. };
  5043. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  5044. struct proto sctpv6_prot = {
  5045. .name = "SCTPv6",
  5046. .owner = THIS_MODULE,
  5047. .close = sctp_close,
  5048. .connect = sctp_connect,
  5049. .disconnect = sctp_disconnect,
  5050. .accept = sctp_accept,
  5051. .ioctl = sctp_ioctl,
  5052. .init = sctp_init_sock,
  5053. .destroy = sctp_destroy_sock,
  5054. .shutdown = sctp_shutdown,
  5055. .setsockopt = sctp_setsockopt,
  5056. .getsockopt = sctp_getsockopt,
  5057. .sendmsg = sctp_sendmsg,
  5058. .recvmsg = sctp_recvmsg,
  5059. .bind = sctp_bind,
  5060. .backlog_rcv = sctp_backlog_rcv,
  5061. .hash = sctp_hash,
  5062. .unhash = sctp_unhash,
  5063. .get_port = sctp_get_port,
  5064. .obj_size = sizeof(struct sctp6_sock),
  5065. .sysctl_mem = sysctl_sctp_mem,
  5066. .sysctl_rmem = sysctl_sctp_rmem,
  5067. .sysctl_wmem = sysctl_sctp_wmem,
  5068. .memory_pressure = &sctp_memory_pressure,
  5069. .enter_memory_pressure = sctp_enter_memory_pressure,
  5070. .memory_allocated = &sctp_memory_allocated,
  5071. };
  5072. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */