PageRenderTime 70ms CodeModel.GetById 15ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file