PageRenderTime 78ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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