PageRenderTime 74ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ChuloChumo/sctp_thesis
Unknown | 5845 lines | 5072 code | 773 blank | 0 comment | 0 complexity | f52196fd90b3a4d8b60c7eb676a3d770 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

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. }
  324. /* Refresh ephemeral port. */
  325. if (!bp->port)
  326. bp->port = inet_sk(sk)->num;
  327. /* Add the address to the bind address list. */
  328. sctp_local_bh_disable();
  329. sctp_write_lock(&ep->base.addr_lock);
  330. /* Use GFP_ATOMIC since BHs are disabled. */
  331. addr->v4.sin_port = ntohs(addr->v4.sin_port);
  332. ret = sctp_add_bind_addr(bp, addr, 1, GFP_ATOMIC);
  333. addr->v4.sin_port = htons(addr->v4.sin_port);
  334. sctp_write_unlock(&ep->base.addr_lock);
  335. sctp_local_bh_enable();
  336. /* Copy back into socket for getsockname() use. */
  337. if (!ret) {
  338. inet_sk(sk)->sport = htons(inet_sk(sk)->num);
  339. af->to_sk_saddr(addr, sk);
  340. }
  341. return ret;
  342. }
  343. /* ADDIP Section 4.1.1 Congestion Control of ASCONF Chunks
  344. *
  345. * R1) One and only one ASCONF Chunk MAY be in transit and unacknowledged
  346. * at any one time. If a sender, after sending an ASCONF chunk, decides
  347. * it needs to transfer another ASCONF Chunk, it MUST wait until the
  348. * ASCONF-ACK Chunk returns from the previous ASCONF Chunk before sending a
  349. * subsequent ASCONF. Note this restriction binds each side, so at any
  350. * time two ASCONF may be in-transit on any given association (one sent
  351. * from each endpoint).
  352. */
  353. static int sctp_send_asconf(struct sctp_association *asoc,
  354. struct sctp_chunk *chunk)
  355. {
  356. int retval = 0;
  357. /* If there is an outstanding ASCONF chunk, queue it for later
  358. * transmission.
  359. */
  360. if (asoc->addip_last_asconf) {
  361. list_add_tail(&chunk->list, &asoc->addip_chunk_list);
  362. goto out;
  363. }
  364. /* Hold the chunk until an ASCONF_ACK is received. */
  365. sctp_chunk_hold(chunk);
  366. retval = sctp_primitive_ASCONF(asoc, chunk);
  367. if (retval)
  368. sctp_chunk_free(chunk);
  369. else
  370. asoc->addip_last_asconf = chunk;
  371. out:
  372. return retval;
  373. }
  374. /* Add a list of addresses as bind addresses to local endpoint or
  375. * association.
  376. *
  377. * Basically run through each address specified in the addrs/addrcnt
  378. * array/length pair, determine if it is IPv6 or IPv4 and call
  379. * sctp_do_bind() on it.
  380. *
  381. * If any of them fails, then the operation will be reversed and the
  382. * ones that were added will be removed.
  383. *
  384. * Only sctp_setsockopt_bindx() is supposed to call this function.
  385. */
  386. int sctp_bindx_add(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  387. {
  388. int cnt;
  389. int retval = 0;
  390. void *addr_buf;
  391. struct sockaddr *sa_addr;
  392. struct sctp_af *af;
  393. SCTP_DEBUG_PRINTK("sctp_bindx_add (sk: %p, addrs: %p, addrcnt: %d)\n",
  394. sk, addrs, addrcnt);
  395. addr_buf = addrs;
  396. for (cnt = 0; cnt < addrcnt; cnt++) {
  397. /* The list may contain either IPv4 or IPv6 address;
  398. * determine the address length for walking thru the list.
  399. */
  400. sa_addr = (struct sockaddr *)addr_buf;
  401. af = sctp_get_af_specific(sa_addr->sa_family);
  402. if (!af) {
  403. retval = -EINVAL;
  404. goto err_bindx_add;
  405. }
  406. retval = sctp_do_bind(sk, (union sctp_addr *)sa_addr,
  407. af->sockaddr_len);
  408. addr_buf += af->sockaddr_len;
  409. err_bindx_add:
  410. if (retval < 0) {
  411. /* Failed. Cleanup the ones that have been added */
  412. if (cnt > 0)
  413. sctp_bindx_rem(sk, addrs, cnt);
  414. return retval;
  415. }
  416. }
  417. return retval;
  418. }
  419. /* Send an ASCONF chunk with Add IP address parameters to all the peers of the
  420. * associations that are part of the endpoint indicating that a list of local
  421. * addresses are added to the endpoint.
  422. *
  423. * If any of the addresses is already in the bind address list of the
  424. * association, we do not send the chunk for that association. But it will not
  425. * affect other associations.
  426. *
  427. * Only sctp_setsockopt_bindx() is supposed to call this function.
  428. */
  429. static int sctp_send_asconf_add_ip(struct sock *sk,
  430. struct sockaddr *addrs,
  431. int addrcnt)
  432. {
  433. struct sctp_sock *sp;
  434. struct sctp_endpoint *ep;
  435. struct sctp_association *asoc;
  436. struct sctp_bind_addr *bp;
  437. struct sctp_chunk *chunk;
  438. struct sctp_sockaddr_entry *laddr;
  439. union sctp_addr *addr;
  440. union sctp_addr saveaddr;
  441. void *addr_buf;
  442. struct sctp_af *af;
  443. struct list_head *pos;
  444. struct list_head *p;
  445. int i;
  446. int retval = 0;
  447. if (!sctp_addip_enable)
  448. return retval;
  449. sp = sctp_sk(sk);
  450. ep = sp->ep;
  451. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  452. __FUNCTION__, sk, addrs, addrcnt);
  453. list_for_each(pos, &ep->asocs) {
  454. asoc = list_entry(pos, struct sctp_association, asocs);
  455. if (!asoc->peer.asconf_capable)
  456. continue;
  457. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_ADD_IP)
  458. continue;
  459. if (!sctp_state(asoc, ESTABLISHED))
  460. continue;
  461. /* Check if any address in the packed array of addresses is
  462. * in the bind address list of the association. If so,
  463. * do not send the asconf chunk to its peer, but continue with
  464. * other associations.
  465. */
  466. addr_buf = addrs;
  467. for (i = 0; i < addrcnt; i++) {
  468. addr = (union sctp_addr *)addr_buf;
  469. af = sctp_get_af_specific(addr->v4.sin_family);
  470. if (!af) {
  471. retval = -EINVAL;
  472. goto out;
  473. }
  474. if (sctp_assoc_lookup_laddr(asoc, addr))
  475. break;
  476. addr_buf += af->sockaddr_len;
  477. }
  478. if (i < addrcnt)
  479. continue;
  480. /* Use the first address in bind addr list of association as
  481. * Address Parameter of ASCONF CHUNK.
  482. */
  483. sctp_read_lock(&asoc->base.addr_lock);
  484. bp = &asoc->base.bind_addr;
  485. p = bp->address_list.next;
  486. laddr = list_entry(p, struct sctp_sockaddr_entry, list);
  487. sctp_read_unlock(&asoc->base.addr_lock);
  488. chunk = sctp_make_asconf_update_ip(asoc, &laddr->a, addrs,
  489. addrcnt, SCTP_PARAM_ADD_IP);
  490. if (!chunk) {
  491. retval = -ENOMEM;
  492. goto out;
  493. }
  494. retval = sctp_send_asconf(asoc, chunk);
  495. if (retval)
  496. goto out;
  497. /* Add the new addresses to the bind address list with
  498. * use_as_src set to 0.
  499. */
  500. sctp_local_bh_disable();
  501. sctp_write_lock(&asoc->base.addr_lock);
  502. addr_buf = addrs;
  503. for (i = 0; i < addrcnt; i++) {
  504. addr = (union sctp_addr *)addr_buf;
  505. af = sctp_get_af_specific(addr->v4.sin_family);
  506. memcpy(&saveaddr, addr, af->sockaddr_len);
  507. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  508. retval = sctp_add_bind_addr(bp, &saveaddr, 0,
  509. GFP_ATOMIC);
  510. addr_buf += af->sockaddr_len;
  511. }
  512. sctp_write_unlock(&asoc->base.addr_lock);
  513. sctp_local_bh_enable();
  514. }
  515. out:
  516. return retval;
  517. }
  518. /* Remove a list of addresses from bind addresses list. Do not remove the
  519. * last address.
  520. *
  521. * Basically run through each address specified in the addrs/addrcnt
  522. * array/length pair, determine if it is IPv6 or IPv4 and call
  523. * sctp_del_bind() on it.
  524. *
  525. * If any of them fails, then the operation will be reversed and the
  526. * ones that were removed will be added back.
  527. *
  528. * At least one address has to be left; if only one address is
  529. * available, the operation will return -EBUSY.
  530. *
  531. * Only sctp_setsockopt_bindx() is supposed to call this function.
  532. */
  533. int sctp_bindx_rem(struct sock *sk, struct sockaddr *addrs, int addrcnt)
  534. {
  535. struct sctp_sock *sp = sctp_sk(sk);
  536. struct sctp_endpoint *ep = sp->ep;
  537. int cnt;
  538. struct sctp_bind_addr *bp = &ep->base.bind_addr;
  539. int retval = 0;
  540. union sctp_addr saveaddr;
  541. void *addr_buf;
  542. struct sockaddr *sa_addr;
  543. struct sctp_af *af;
  544. SCTP_DEBUG_PRINTK("sctp_bindx_rem (sk: %p, addrs: %p, addrcnt: %d)\n",
  545. sk, addrs, addrcnt);
  546. addr_buf = addrs;
  547. for (cnt = 0; cnt < addrcnt; cnt++) {
  548. /* If the bind address list is empty or if there is only one
  549. * bind address, there is nothing more to be removed (we need
  550. * at least one address here).
  551. */
  552. if (list_empty(&bp->address_list) ||
  553. (sctp_list_single_entry(&bp->address_list))) {
  554. retval = -EBUSY;
  555. goto err_bindx_rem;
  556. }
  557. /* The list may contain either IPv4 or IPv6 address;
  558. * determine the address length to copy the address to
  559. * saveaddr.
  560. */
  561. sa_addr = (struct sockaddr *)addr_buf;
  562. af = sctp_get_af_specific(sa_addr->sa_family);
  563. if (!af) {
  564. retval = -EINVAL;
  565. goto err_bindx_rem;
  566. }
  567. memcpy(&saveaddr, sa_addr, af->sockaddr_len);
  568. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  569. if (saveaddr.v4.sin_port != bp->port) {
  570. retval = -EINVAL;
  571. goto err_bindx_rem;
  572. }
  573. /* FIXME - There is probably a need to check if sk->sk_saddr and
  574. * sk->sk_rcv_addr are currently set to one of the addresses to
  575. * be removed. This is something which needs to be looked into
  576. * when we are fixing the outstanding issues with multi-homing
  577. * socket routing and failover schemes. Refer to comments in
  578. * sctp_do_bind(). -daisy
  579. */
  580. sctp_local_bh_disable();
  581. sctp_write_lock(&ep->base.addr_lock);
  582. retval = sctp_del_bind_addr(bp, &saveaddr);
  583. sctp_write_unlock(&ep->base.addr_lock);
  584. sctp_local_bh_enable();
  585. addr_buf += af->sockaddr_len;
  586. err_bindx_rem:
  587. if (retval < 0) {
  588. /* Failed. Add the ones that has been removed back */
  589. if (cnt > 0)
  590. sctp_bindx_add(sk, addrs, cnt);
  591. return retval;
  592. }
  593. }
  594. return retval;
  595. }
  596. /* Send an ASCONF chunk with Delete IP address parameters to all the peers of
  597. * the associations that are part of the endpoint indicating that a list of
  598. * local addresses are removed from the endpoint.
  599. *
  600. * If any of the addresses is already in the bind address list of the
  601. * association, we do not send the chunk for that association. But it will not
  602. * affect other associations.
  603. *
  604. * Only sctp_setsockopt_bindx() is supposed to call this function.
  605. */
  606. static int sctp_send_asconf_del_ip(struct sock *sk,
  607. struct sockaddr *addrs,
  608. int addrcnt)
  609. {
  610. struct sctp_sock *sp;
  611. struct sctp_endpoint *ep;
  612. struct sctp_association *asoc;
  613. struct sctp_transport *transport;
  614. struct sctp_bind_addr *bp;
  615. struct sctp_chunk *chunk;
  616. union sctp_addr *laddr;
  617. union sctp_addr saveaddr;
  618. void *addr_buf;
  619. struct sctp_af *af;
  620. struct list_head *pos, *pos1;
  621. struct sctp_sockaddr_entry *saddr;
  622. int i;
  623. int retval = 0;
  624. if (!sctp_addip_enable)
  625. return retval;
  626. sp = sctp_sk(sk);
  627. ep = sp->ep;
  628. SCTP_DEBUG_PRINTK("%s: (sk: %p, addrs: %p, addrcnt: %d)\n",
  629. __FUNCTION__, sk, addrs, addrcnt);
  630. list_for_each(pos, &ep->asocs) {
  631. asoc = list_entry(pos, struct sctp_association, asocs);
  632. if (!asoc->peer.asconf_capable)
  633. continue;
  634. if (asoc->peer.addip_disabled_mask & SCTP_PARAM_DEL_IP)
  635. continue;
  636. if (!sctp_state(asoc, ESTABLISHED))
  637. continue;
  638. /* Check if any address in the packed array of addresses is
  639. * not present in the bind address list of the association.
  640. * If so, do not send the asconf chunk to its peer, but
  641. * continue with other associations.
  642. */
  643. addr_buf = addrs;
  644. for (i = 0; i < addrcnt; i++) {
  645. laddr = (union sctp_addr *)addr_buf;
  646. af = sctp_get_af_specific(laddr->v4.sin_family);
  647. if (!af) {
  648. retval = -EINVAL;
  649. goto out;
  650. }
  651. if (!sctp_assoc_lookup_laddr(asoc, laddr))
  652. break;
  653. addr_buf += af->sockaddr_len;
  654. }
  655. if (i < addrcnt)
  656. continue;
  657. /* Find one address in the association's bind address list
  658. * that is not in the packed array of addresses. This is to
  659. * make sure that we do not delete all the addresses in the
  660. * association.
  661. */
  662. sctp_read_lock(&asoc->base.addr_lock);
  663. bp = &asoc->base.bind_addr;
  664. laddr = sctp_find_unmatch_addr(bp, (union sctp_addr *)addrs,
  665. addrcnt, sp);
  666. sctp_read_unlock(&asoc->base.addr_lock);
  667. if (!laddr)
  668. continue;
  669. chunk = sctp_make_asconf_update_ip(asoc, laddr, addrs, addrcnt,
  670. SCTP_PARAM_DEL_IP);
  671. if (!chunk) {
  672. retval = -ENOMEM;
  673. goto out;
  674. }
  675. /* Reset use_as_src flag for the addresses in the bind address
  676. * list that are to be deleted.
  677. */
  678. sctp_local_bh_disable();
  679. sctp_write_lock(&asoc->base.addr_lock);
  680. addr_buf = addrs;
  681. for (i = 0; i < addrcnt; i++) {
  682. laddr = (union sctp_addr *)addr_buf;
  683. af = sctp_get_af_specific(laddr->v4.sin_family);
  684. memcpy(&saveaddr, laddr, af->sockaddr_len);
  685. saveaddr.v4.sin_port = ntohs(saveaddr.v4.sin_port);
  686. list_for_each(pos1, &bp->address_list) {
  687. saddr = list_entry(pos1,
  688. struct sctp_sockaddr_entry,
  689. list);
  690. if (sctp_cmp_addr_exact(&saddr->a, &saveaddr))
  691. saddr->use_as_src = 0;
  692. }
  693. addr_buf += af->sockaddr_len;
  694. }
  695. sctp_write_unlock(&asoc->base.addr_lock);
  696. sctp_local_bh_enable();
  697. /* Update the route and saddr entries for all the transports
  698. * as some of the addresses in the bind address list are
  699. * about to be deleted and cannot be used as source addresses.
  700. */
  701. list_for_each(pos1, &asoc->peer.transport_addr_list) {
  702. transport = list_entry(pos1, struct sctp_transport,
  703. transports);
  704. dst_release(transport->dst);
  705. sctp_transport_route(transport, NULL,
  706. sctp_sk(asoc->base.sk));
  707. }
  708. retval = sctp_send_asconf(asoc, chunk);
  709. }
  710. out:
  711. return retval;
  712. }
  713. /* Helper for tunneling sctp_bindx() requests through sctp_setsockopt()
  714. *
  715. * API 8.1
  716. * int sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt,
  717. * int flags);
  718. *
  719. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  720. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  721. * or IPv6 addresses.
  722. *
  723. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  724. * Section 3.1.2 for this usage.
  725. *
  726. * addrs is a pointer to an array of one or more socket addresses. Each
  727. * address is contained in its appropriate structure (i.e. struct
  728. * sockaddr_in or struct sockaddr_in6) the family of the address type
  729. * must be used to distengish the address length (note that this
  730. * representation is termed a "packed array" of addresses). The caller
  731. * specifies the number of addresses in the array with addrcnt.
  732. *
  733. * On success, sctp_bindx() returns 0. On failure, sctp_bindx() returns
  734. * -1, and sets errno to the appropriate error code.
  735. *
  736. * For SCTP, the port given in each socket address must be the same, or
  737. * sctp_bindx() will fail, setting errno to EINVAL.
  738. *
  739. * The flags parameter is formed from the bitwise OR of zero or more of
  740. * the following currently defined flags:
  741. *
  742. * SCTP_BINDX_ADD_ADDR
  743. *
  744. * SCTP_BINDX_REM_ADDR
  745. *
  746. * SCTP_BINDX_ADD_ADDR directs SCTP to add the given addresses to the
  747. * association, and SCTP_BINDX_REM_ADDR directs SCTP to remove the given
  748. * addresses from the association. The two flags are mutually exclusive;
  749. * if both are given, sctp_bindx() will fail with EINVAL. A caller may
  750. * not remove all addresses from an association; sctp_bindx() will
  751. * reject such an attempt with EINVAL.
  752. *
  753. * An application can use sctp_bindx(SCTP_BINDX_ADD_ADDR) to associate
  754. * additional addresses with an endpoint after calling bind(). Or use
  755. * sctp_bindx(SCTP_BINDX_REM_ADDR) to remove some addresses a listening
  756. * socket is associated with so that no new association accepted will be
  757. * associated with those addresses. If the endpoint supports dynamic
  758. * address a SCTP_BINDX_REM_ADDR or SCTP_BINDX_ADD_ADDR may cause a
  759. * endpoint to send the appropriate message to the peer to change the
  760. * peers address lists.
  761. *
  762. * Adding and removing addresses from a connected association is
  763. * optional functionality. Implementations that do not support this
  764. * functionality should return EOPNOTSUPP.
  765. *
  766. * Basically do nothing but copying the addresses from user to kernel
  767. * land and invoking either sctp_bindx_add() or sctp_bindx_rem() on the sk.
  768. * This is used for tunneling the sctp_bindx() request through sctp_setsockopt()
  769. * from userspace.
  770. *
  771. * We don't use copy_from_user() for optimization: we first do the
  772. * sanity checks (buffer size -fast- and access check-healthy
  773. * pointer); if all of those succeed, then we can alloc the memory
  774. * (expensive operation) needed to copy the data to kernel. Then we do
  775. * the copying without checking the user space area
  776. * (__copy_from_user()).
  777. *
  778. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  779. * it.
  780. *
  781. * sk The sk of the socket
  782. * addrs The pointer to the addresses in user land
  783. * addrssize Size of the addrs buffer
  784. * op Operation to perform (add or remove, see the flags of
  785. * sctp_bindx)
  786. *
  787. * Returns 0 if ok, <0 errno code on error.
  788. */
  789. SCTP_STATIC int sctp_setsockopt_bindx(struct sock* sk,
  790. struct sockaddr __user *addrs,
  791. int addrs_size, int op)
  792. {
  793. struct sockaddr *kaddrs;
  794. int err;
  795. int addrcnt = 0;
  796. int walk_size = 0;
  797. struct sockaddr *sa_addr;
  798. void *addr_buf;
  799. struct sctp_af *af;
  800. SCTP_DEBUG_PRINTK("sctp_setsocktopt_bindx: sk %p addrs %p"
  801. " addrs_size %d opt %d\n", sk, addrs, addrs_size, op);
  802. if (unlikely(addrs_size <= 0))
  803. return -EINVAL;
  804. /* Check the user passed a healthy pointer. */
  805. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  806. return -EFAULT;
  807. /* Alloc space for the address array in kernel memory. */
  808. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  809. if (unlikely(!kaddrs))
  810. return -ENOMEM;
  811. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  812. kfree(kaddrs);
  813. return -EFAULT;
  814. }
  815. /* Walk through the addrs buffer and count the number of addresses. */
  816. addr_buf = kaddrs;
  817. while (walk_size < addrs_size) {
  818. sa_addr = (struct sockaddr *)addr_buf;
  819. af = sctp_get_af_specific(sa_addr->sa_family);
  820. /* If the address family is not supported or if this address
  821. * causes the address buffer to overflow return EINVAL.
  822. */
  823. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  824. kfree(kaddrs);
  825. return -EINVAL;
  826. }
  827. addrcnt++;
  828. addr_buf += af->sockaddr_len;
  829. walk_size += af->sockaddr_len;
  830. }
  831. /* Do the work. */
  832. switch (op) {
  833. case SCTP_BINDX_ADD_ADDR:
  834. err = sctp_bindx_add(sk, kaddrs, addrcnt);
  835. if (err)
  836. goto out;
  837. err = sctp_send_asconf_add_ip(sk, kaddrs, addrcnt);
  838. break;
  839. case SCTP_BINDX_REM_ADDR:
  840. err = sctp_bindx_rem(sk, kaddrs, addrcnt);
  841. if (err)
  842. goto out;
  843. err = sctp_send_asconf_del_ip(sk, kaddrs, addrcnt);
  844. break;
  845. default:
  846. err = -EINVAL;
  847. break;
  848. };
  849. out:
  850. kfree(kaddrs);
  851. return err;
  852. }
  853. /* __sctp_connect(struct sock* sk, struct sockaddr *kaddrs, int addrs_size)
  854. *
  855. * Common routine for handling connect() and sctp_connectx().
  856. * Connect will come in with just a single address.
  857. */
  858. static int __sctp_connect(struct sock* sk,
  859. struct sockaddr *kaddrs,
  860. int addrs_size)
  861. {
  862. struct sctp_sock *sp;
  863. struct sctp_endpoint *ep;
  864. struct sctp_association *asoc = NULL;
  865. struct sctp_association *asoc2;
  866. struct sctp_transport *transport;
  867. union sctp_addr to;
  868. struct sctp_af *af;
  869. sctp_scope_t scope;
  870. long timeo;
  871. int err = 0;
  872. int addrcnt = 0;
  873. int walk_size = 0;
  874. struct sockaddr *sa_addr;
  875. void *addr_buf;
  876. sp = sctp_sk(sk);
  877. ep = sp->ep;
  878. /* connect() cannot be done on a socket that is already in ESTABLISHED
  879. * state - UDP-style peeled off socket or a TCP-style socket that
  880. * is already connected.
  881. * It cannot be done even on a TCP-style listening socket.
  882. */
  883. if (sctp_sstate(sk, ESTABLISHED) ||
  884. (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))) {
  885. err = -EISCONN;
  886. goto out_free;
  887. }
  888. /* Walk through the addrs buffer and count the number of addresses. */
  889. addr_buf = kaddrs;
  890. while (walk_size < addrs_size) {
  891. sa_addr = (struct sockaddr *)addr_buf;
  892. af = sctp_get_af_specific(sa_addr->sa_family);
  893. /* If the address family is not supported or if this address
  894. * causes the address buffer to overflow return EINVAL.
  895. */
  896. if (!af || (walk_size + af->sockaddr_len) > addrs_size) {
  897. err = -EINVAL;
  898. goto out_free;
  899. }
  900. err = sctp_verify_addr(sk, (union sctp_addr *)sa_addr,
  901. af->sockaddr_len);
  902. if (err)
  903. goto out_free;
  904. memcpy(&to, sa_addr, af->sockaddr_len);
  905. to.v4.sin_port = ntohs(to.v4.sin_port);
  906. /* Check if there already is a matching association on the
  907. * endpoint (other than the one created here).
  908. */
  909. asoc2 = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  910. if (asoc2 && asoc2 != asoc) {
  911. if (asoc2->state >= SCTP_STATE_ESTABLISHED)
  912. err = -EISCONN;
  913. else
  914. err = -EALREADY;
  915. goto out_free;
  916. }
  917. /* If we could not find a matching association on the endpoint,
  918. * make sure that there is no peeled-off association matching
  919. * the peer address even on another socket.
  920. */
  921. if (sctp_endpoint_is_peeled_off(ep, &to)) {
  922. err = -EADDRNOTAVAIL;
  923. goto out_free;
  924. }
  925. if (!asoc) {
  926. /* If a bind() or sctp_bindx() is not called prior to
  927. * an sctp_connectx() call, the system picks an
  928. * ephemeral port and will choose an address set
  929. * equivalent to binding with a wildcard address.
  930. */
  931. if (!ep->base.bind_addr.port) {
  932. if (sctp_autobind(sk)) {
  933. err = -EAGAIN;
  934. goto out_free;
  935. }
  936. } else {
  937. /*
  938. * If an unprivileged user inherits a 1-many
  939. * style socket with open associations on a
  940. * privileged port, it MAY be permitted to
  941. * accept new associations, but it SHOULD NOT
  942. * be permitted to open new associations.
  943. */
  944. if (ep->base.bind_addr.port < PROT_SOCK &&
  945. !capable(CAP_NET_BIND_SERVICE)) {
  946. err = -EACCES;
  947. goto out_free;
  948. }
  949. }
  950. scope = sctp_scope(&to);
  951. asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  952. if (!asoc) {
  953. err = -ENOMEM;
  954. goto out_free;
  955. }
  956. }
  957. /* Prime the peer's transport structures. */
  958. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL,
  959. SCTP_UNKNOWN);
  960. if (!transport) {
  961. err = -ENOMEM;
  962. goto out_free;
  963. }
  964. addrcnt++;
  965. addr_buf += af->sockaddr_len;
  966. walk_size += af->sockaddr_len;
  967. }
  968. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  969. if (err < 0) {
  970. goto out_free;
  971. }
  972. err = sctp_primitive_ASSOCIATE(asoc, NULL);
  973. if (err < 0) {
  974. goto out_free;
  975. }
  976. /* Initialize sk's dport and daddr for getpeername() */
  977. inet_sk(sk)->dport = htons(asoc->peer.port);
  978. af = sctp_get_af_specific(to.sa.sa_family);
  979. af->to_sk_daddr(&to, sk);
  980. sk->sk_err = 0;
  981. timeo = sock_sndtimeo(sk, sk->sk_socket->file->f_flags & O_NONBLOCK);
  982. err = sctp_wait_for_connect(asoc, &timeo);
  983. /* Don't free association on exit. */
  984. asoc = NULL;
  985. out_free:
  986. SCTP_DEBUG_PRINTK("About to exit __sctp_connect() free asoc: %p"
  987. " kaddrs: %p err: %d\n",
  988. asoc, kaddrs, err);
  989. if (asoc)
  990. sctp_association_free(asoc);
  991. return err;
  992. }
  993. /* Helper for tunneling sctp_connectx() requests through sctp_setsockopt()
  994. *
  995. * API 8.9
  996. * int sctp_connectx(int sd, struct sockaddr *addrs, int addrcnt);
  997. *
  998. * If sd is an IPv4 socket, the addresses passed must be IPv4 addresses.
  999. * If the sd is an IPv6 socket, the addresses passed can either be IPv4
  1000. * or IPv6 addresses.
  1001. *
  1002. * A single address may be specified as INADDR_ANY or IN6ADDR_ANY, see
  1003. * Section 3.1.2 for this usage.
  1004. *
  1005. * addrs is a pointer to an array of one or more socket addresses. Each
  1006. * address is contained in its appropriate structure (i.e. struct
  1007. * sockaddr_in or struct sockaddr_in6) the family of the address type
  1008. * must be used to distengish the address length (note that this
  1009. * representation is termed a "packed array" of addresses). The caller
  1010. * specifies the number of addresses in the array with addrcnt.
  1011. *
  1012. * On success, sctp_connectx() returns 0. On failure, sctp_connectx() returns
  1013. * -1, and sets errno to the appropriate error code.
  1014. *
  1015. * For SCTP, the port given in each socket address must be the same, or
  1016. * sctp_connectx() will fail, setting errno to EINVAL.
  1017. *
  1018. * An application can use sctp_connectx to initiate an association with
  1019. * an endpoint that is multi-homed. Much like sctp_bindx() this call
  1020. * allows a caller to specify multiple addresses at which a peer can be
  1021. * reached. The way the SCTP stack uses the list of addresses to set up
  1022. * the association is implementation dependant. This function only
  1023. * specifies that the stack will try to make use of all the addresses in
  1024. * the list when needed.
  1025. *
  1026. * Note that the list of addresses passed in is only used for setting up
  1027. * the association. It does not necessarily equal the set of addresses
  1028. * the peer uses for the resulting association. If the caller wants to
  1029. * find out the set of peer addresses, it must use sctp_getpaddrs() to
  1030. * retrieve them after the association has been set up.
  1031. *
  1032. * Basically do nothing but copying the addresses from user to kernel
  1033. * land and invoking either sctp_connectx(). This is used for tunneling
  1034. * the sctp_connectx() request through sctp_setsockopt() from userspace.
  1035. *
  1036. * We don't use copy_from_user() for optimization: we first do the
  1037. * sanity checks (buffer size -fast- and access check-healthy
  1038. * pointer); if all of those succeed, then we can alloc the memory
  1039. * (expensive operation) needed to copy the data to kernel. Then we do
  1040. * the copying without checking the user space area
  1041. * (__copy_from_user()).
  1042. *
  1043. * On exit there is no need to do sockfd_put(), sys_setsockopt() does
  1044. * it.
  1045. *
  1046. * sk The sk of the socket
  1047. * addrs The pointer to the addresses in user land
  1048. * addrssize Size of the addrs buffer
  1049. *
  1050. * Returns 0 if ok, <0 errno code on error.
  1051. */
  1052. SCTP_STATIC int sctp_setsockopt_connectx(struct sock* sk,
  1053. struct sockaddr __user *addrs,
  1054. int addrs_size)
  1055. {
  1056. int err = 0;
  1057. struct sockaddr *kaddrs;
  1058. SCTP_DEBUG_PRINTK("%s - sk %p addrs %p addrs_size %d\n",
  1059. __FUNCTION__, sk, addrs, addrs_size);
  1060. if (unlikely(addrs_size <= 0))
  1061. return -EINVAL;
  1062. /* Check the user passed a healthy pointer. */
  1063. if (unlikely(!access_ok(VERIFY_READ, addrs, addrs_size)))
  1064. return -EFAULT;
  1065. /* Alloc space for the address array in kernel memory. */
  1066. kaddrs = kmalloc(addrs_size, GFP_KERNEL);
  1067. if (unlikely(!kaddrs))
  1068. return -ENOMEM;
  1069. if (__copy_from_user(kaddrs, addrs, addrs_size)) {
  1070. err = -EFAULT;
  1071. } else {
  1072. err = __sctp_connect(sk, kaddrs, addrs_size);
  1073. }
  1074. kfree(kaddrs);
  1075. return err;
  1076. }
  1077. /* API 3.1.4 close() - UDP Style Syntax
  1078. * Applications use close() to perform graceful shutdown (as described in
  1079. * Section 10.1 of [SCTP]) on ALL the associations currently represented
  1080. * by a UDP-style socket.
  1081. *
  1082. * The syntax is
  1083. *
  1084. * ret = close(int sd);
  1085. *
  1086. * sd - the socket descriptor of the associations to be closed.
  1087. *
  1088. * To gracefully shutdown a specific association represented by the
  1089. * UDP-style socket, an application should use the sendmsg() call,
  1090. * passing no user data, but including the appropriate flag in the
  1091. * ancillary data (see Section xxxx).
  1092. *
  1093. * If sd in the close() call is a branched-off socket representing only
  1094. * one association, the shutdown is performed on that association only.
  1095. *
  1096. * 4.1.6 close() - TCP Style Syntax
  1097. *
  1098. * Applications use close() to gracefully close down an association.
  1099. *
  1100. * The syntax is:
  1101. *
  1102. * int close(int sd);
  1103. *
  1104. * sd - the socket descriptor of the association to be closed.
  1105. *
  1106. * After an application calls close() on a socket descriptor, no further
  1107. * socket operations will succeed on that descriptor.
  1108. *
  1109. * API 7.1.4 SO_LINGER
  1110. *
  1111. * An application using the TCP-style socket can use this option to
  1112. * perform the SCTP ABORT primitive. The linger option structure is:
  1113. *
  1114. * struct linger {
  1115. * int l_onoff; // option on/off
  1116. * int l_linger; // linger time
  1117. * };
  1118. *
  1119. * To enable the option, set l_onoff to 1. If the l_linger value is set
  1120. * to 0, calling close() is the same as the ABORT primitive. If the
  1121. * value is set to a negative value, the setsockopt() call will return
  1122. * an error. If the value is set to a positive value linger_time, the
  1123. * close() can be blocked for at most linger_time ms. If the graceful
  1124. * shutdown phase does not finish during this period, close() will
  1125. * return but the graceful shutdown phase continues in the system.
  1126. */
  1127. SCTP_STATIC void sctp_close(struct sock *sk, long timeout)
  1128. {
  1129. struct sctp_endpoint *ep;
  1130. struct sctp_association *asoc;
  1131. struct list_head *pos, *temp;
  1132. SCTP_DEBUG_PRINTK("sctp_close(sk: 0x%p, timeout:%ld)\n", sk, timeout);
  1133. sctp_lock_sock(sk);
  1134. sk->sk_shutdown = SHUTDOWN_MASK;
  1135. ep = sctp_sk(sk)->ep;
  1136. /* Walk all associations on an endpoint. */
  1137. list_for_each_safe(pos, temp, &ep->asocs) {
  1138. asoc = list_entry(pos, struct sctp_association, asocs);
  1139. if (sctp_style(sk, TCP)) {
  1140. /* A closed association can still be in the list if
  1141. * it belongs to a TCP-style listening socket that is
  1142. * not yet accepted. If so, free it. If not, send an
  1143. * ABORT or SHUTDOWN based on the linger options.
  1144. */
  1145. if (sctp_state(asoc, CLOSED)) {
  1146. sctp_unhash_established(asoc);
  1147. sctp_association_free(asoc);
  1148. continue;
  1149. }
  1150. }
  1151. if (sock_flag(sk, SOCK_LINGER) && !sk->sk_lingertime) {
  1152. struct sctp_chunk *chunk;
  1153. chunk = sctp_make_abort_user(asoc, NULL, 0);
  1154. if (chunk)
  1155. sctp_primitive_ABORT(asoc, chunk);
  1156. } else
  1157. sctp_primitive_SHUTDOWN(asoc, NULL);
  1158. }
  1159. /* Clean up any skbs sitting on the receive queue. */
  1160. sctp_queue_purge_ulpevents(&sk->sk_receive_queue);
  1161. sctp_queue_purge_ulpevents(&sctp_sk(sk)->pd_lobby);
  1162. /* On a TCP-style socket, block for at most linger_time if set. */
  1163. if (sctp_style(sk, TCP) && timeout)
  1164. sctp_wait_for_close(sk, timeout);
  1165. /* This will run the backlog queue. */
  1166. sctp_release_sock(sk);
  1167. /* Supposedly, no process has access to the socket, but
  1168. * the net layers still may.
  1169. */
  1170. sctp_local_bh_disable();
  1171. sctp_bh_lock_sock(sk);
  1172. /* Hold the sock, since sk_common_release() will put sock_put()
  1173. * and we have just a little more cleanup.
  1174. */
  1175. sock_hold(sk);
  1176. sk_common_release(sk);
  1177. sctp_bh_unlock_sock(sk);
  1178. sctp_local_bh_enable();
  1179. sock_put(sk);
  1180. SCTP_DBG_OBJCNT_DEC(sock);
  1181. }
  1182. /* Handle EPIPE error. */
  1183. static int sctp_error(struct sock *sk, int flags, int err)
  1184. {
  1185. if (err == -EPIPE)
  1186. err = sock_error(sk) ? : -EPIPE;
  1187. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  1188. send_sig(SIGPIPE, current, 0);
  1189. return err;
  1190. }
  1191. /* API 3.1.3 sendmsg() - UDP Style Syntax
  1192. *
  1193. * An application uses sendmsg() and recvmsg() calls to transmit data to
  1194. * and receive data from its peer.
  1195. *
  1196. * ssize_t sendmsg(int socket, const struct msghdr *message,
  1197. * int flags);
  1198. *
  1199. * socket - the socket descriptor of the endpoint.
  1200. * message - pointer to the msghdr structure which contains a single
  1201. * user message and possibly some ancillary data.
  1202. *
  1203. * See Section 5 for complete description of the data
  1204. * structures.
  1205. *
  1206. * flags - flags sent or received with the user message, see Section
  1207. * 5 for complete description of the flags.
  1208. *
  1209. * Note: This function could use a rewrite especially when explicit
  1210. * connect support comes in.
  1211. */
  1212. /* BUG: We do not implement the equivalent of sk_stream_wait_memory(). */
  1213. SCTP_STATIC int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
  1214. SCTP_STATIC int sctp_sendmsg(struct kiocb *iocb, struct sock *sk,
  1215. struct msghdr *msg, size_t msg_len)
  1216. {
  1217. struct sctp_sock *sp;
  1218. struct sctp_endpoint *ep;
  1219. struct sctp_association *new_asoc=NULL, *asoc=NULL;
  1220. struct sctp_transport *transport, *chunk_tp;
  1221. struct sctp_chunk *chunk;
  1222. union sctp_addr to;
  1223. struct sockaddr *msg_name = NULL;
  1224. struct sctp_sndrcvinfo default_sinfo = { 0 };
  1225. struct sctp_sndrcvinfo *sinfo;
  1226. struct sctp_initmsg *sinit;
  1227. sctp_assoc_t associd = 0;
  1228. sctp_cmsgs_t cmsgs = { NULL };
  1229. int err;
  1230. sctp_scope_t scope;
  1231. long timeo;
  1232. __u16 sinfo_flags = 0;
  1233. struct sctp_datamsg *datamsg;
  1234. struct list_head *pos;
  1235. int msg_flags = msg->msg_flags;
  1236. SCTP_DEBUG_PRINTK("sctp_sendmsg(sk: %p, msg: %p, msg_len: %zu)\n",
  1237. sk, msg, msg_len);
  1238. err = 0;
  1239. sp = sctp_sk(sk);
  1240. ep = sp->ep;
  1241. SCTP_DEBUG_PRINTK("Using endpoint: %p.\n", ep);
  1242. /* We cannot send a message over a TCP-style listening socket. */
  1243. if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) {
  1244. err = -EPIPE;
  1245. goto out_nounlock;
  1246. }
  1247. /* Parse out the SCTP CMSGs. */
  1248. err = sctp_msghdr_parse(msg, &cmsgs);
  1249. if (err) {
  1250. SCTP_DEBUG_PRINTK("msghdr parse err = %x\n", err);
  1251. goto out_nounlock;
  1252. }
  1253. /* Fetch the destination address for this packet. This
  1254. * address only selects the association--it is not necessarily
  1255. * the address we will send to.
  1256. * For a peeled-off socket, msg_name is ignored.
  1257. */
  1258. if (!sctp_style(sk, UDP_HIGH_BANDWIDTH) && msg->msg_name) {
  1259. int msg_namelen = msg->msg_namelen;
  1260. err = sctp_verify_addr(sk, (union sctp_addr *)msg->msg_name,
  1261. msg_namelen);
  1262. if (err)
  1263. return err;
  1264. if (msg_namelen > sizeof(to))
  1265. msg_namelen = sizeof(to);
  1266. memcpy(&to, msg->msg_name, msg_namelen);
  1267. SCTP_DEBUG_PRINTK("Just memcpy'd. msg_name is "
  1268. "0x%x:%u.\n",
  1269. to.v4.sin_addr.s_addr, to.v4.sin_port);
  1270. to.v4.sin_port = ntohs(to.v4.sin_port);
  1271. msg_name = msg->msg_name;
  1272. }
  1273. sinfo = cmsgs.info;
  1274. sinit = cmsgs.init;
  1275. /* Did the user specify SNDRCVINFO? */
  1276. if (sinfo) {
  1277. sinfo_flags = sinfo->sinfo_flags;
  1278. associd = sinfo->sinfo_assoc_id;
  1279. }
  1280. SCTP_DEBUG_PRINTK("msg_len: %zu, sinfo_flags: 0x%x\n",
  1281. msg_len, sinfo_flags);
  1282. /* SCTP_EOF or SCTP_ABORT cannot be set on a TCP-style socket. */
  1283. if (sctp_style(sk, TCP) && (sinfo_flags & (SCTP_EOF | SCTP_ABORT))) {
  1284. err = -EINVAL;
  1285. goto out_nounlock;
  1286. }
  1287. /* If SCTP_EOF is set, no data can be sent. Disallow sending zero
  1288. * length messages when SCTP_EOF|SCTP_ABORT is not set.
  1289. * If SCTP_ABORT is set, the message length could be non zero with
  1290. * the msg_iov set to the user abort reason.
  1291. */
  1292. if (((sinfo_flags & SCTP_EOF) && (msg_len > 0)) ||
  1293. (!(sinfo_flags & (SCTP_EOF|SCTP_ABORT)) && (msg_len == 0))) {
  1294. err = -EINVAL;
  1295. goto out_nounlock;
  1296. }
  1297. /* If SCTP_ADDR_OVER is set, there must be an address
  1298. * specified in msg_name.
  1299. */
  1300. if ((sinfo_flags & SCTP_ADDR_OVER) && (!msg->msg_name)) {
  1301. err = -EINVAL;
  1302. goto out_nounlock;
  1303. }
  1304. transport = NULL;
  1305. SCTP_DEBUG_PRINTK("About to look up association.\n");
  1306. sctp_lock_sock(sk);
  1307. /* If a msg_name has been specified, assume this is to be used. */
  1308. if (msg_name) {
  1309. /* Look for a matching association on the endpoint. */
  1310. asoc = sctp_endpoint_lookup_assoc(ep, &to, &transport);
  1311. if (!asoc) {
  1312. /* If we could not find a matching association on the
  1313. * endpoint, make sure that it is not a TCP-style
  1314. * socket that already has an association or there is
  1315. * no peeled-off association on another socket.
  1316. */
  1317. if ((sctp_style(sk, TCP) &&
  1318. sctp_sstate(sk, ESTABLISHED)) ||
  1319. sctp_endpoint_is_peeled_off(ep, &to)) {
  1320. err = -EADDRNOTAVAIL;
  1321. goto out_unlock;
  1322. }
  1323. }
  1324. } else {
  1325. asoc = sctp_id2assoc(sk, associd);
  1326. if (!asoc) {
  1327. err = -EPIPE;
  1328. goto out_unlock;
  1329. }
  1330. }
  1331. if (asoc) {
  1332. SCTP_DEBUG_PRINTK("Just looked up association: %p.\n", asoc);
  1333. /* We cannot send a message on a TCP-style SCTP_SS_ESTABLISHED
  1334. * socket that has an association in CLOSED state. This can
  1335. * happen when an accepted socket has an association that is
  1336. * already CLOSED.
  1337. */
  1338. if (sctp_state(asoc, CLOSED) && sctp_style(sk, TCP)) {
  1339. err = -EPIPE;
  1340. goto out_unlock;
  1341. }
  1342. if (sinfo_flags & SCTP_EOF) {
  1343. SCTP_DEBUG_PRINTK("Shutting down association: %p\n",
  1344. asoc);
  1345. sctp_primitive_SHUTDOWN(asoc, NULL);
  1346. err = 0;
  1347. goto out_unlock;
  1348. }
  1349. if (sinfo_flags & SCTP_ABORT) {
  1350. struct sctp_chunk *chunk;
  1351. chunk = sctp_make_abort_user(asoc, msg, msg_len);
  1352. if (!chunk) {
  1353. err = -ENOMEM;
  1354. goto out_unlock;
  1355. }
  1356. SCTP_DEBUG_PRINTK("Aborting association: %p\n", asoc);
  1357. sctp_primitive_ABORT(asoc, chunk);
  1358. err = 0;
  1359. goto out_unlock;
  1360. }
  1361. }
  1362. /* Do we need to create the association? */
  1363. if (!asoc) {
  1364. SCTP_DEBUG_PRINTK("There is no association yet.\n");
  1365. if (sinfo_flags & (SCTP_EOF | SCTP_ABORT)) {
  1366. err = -EINVAL;
  1367. goto out_unlock;
  1368. }
  1369. /* Check for invalid stream against the stream counts,
  1370. * either the default or the user specified stream counts.
  1371. */
  1372. if (sinfo) {
  1373. if (!sinit || (sinit && !sinit->sinit_num_ostreams)) {
  1374. /* Check against the defaults. */
  1375. if (sinfo->sinfo_stream >=
  1376. sp->initmsg.sinit_num_ostreams) {
  1377. err = -EINVAL;
  1378. goto out_unlock;
  1379. }
  1380. } else {
  1381. /* Check against the requested. */
  1382. if (sinfo->sinfo_stream >=
  1383. sinit->sinit_num_ostreams) {
  1384. err = -EINVAL;
  1385. goto out_unlock;
  1386. }
  1387. }
  1388. }
  1389. /*
  1390. * API 3.1.2 bind() - UDP Style Syntax
  1391. * If a bind() or sctp_bindx() is not called prior to a
  1392. * sendmsg() call that initiates a new association, the
  1393. * system picks an ephemeral port and will choose an address
  1394. * set equivalent to binding with a wildcard address.
  1395. */
  1396. if (!ep->base.bind_addr.port) {
  1397. if (sctp_autobind(sk)) {
  1398. err = -EAGAIN;
  1399. goto out_unlock;
  1400. }
  1401. } else {
  1402. /*
  1403. * If an unprivileged user inherits a one-to-many
  1404. * style socket with open associations on a privileged
  1405. * port, it MAY be permitted to accept new associations,
  1406. * but it SHOULD NOT be permitted to open new
  1407. * associations.
  1408. */
  1409. if (ep->base.bind_addr.port < PROT_SOCK &&
  1410. !capable(CAP_NET_BIND_SERVICE)) {
  1411. err = -EACCES;
  1412. goto out_unlock;
  1413. }
  1414. }
  1415. scope = sctp_scope(&to);
  1416. new_asoc = sctp_association_new(ep, sk, scope, GFP_KERNEL);
  1417. if (!new_asoc) {
  1418. err = -ENOMEM;
  1419. goto out_unlock;
  1420. }
  1421. asoc = new_asoc;
  1422. /* If the SCTP_INIT ancillary data is specified, set all
  1423. * the association init values accordingly.
  1424. */
  1425. if (sinit) {
  1426. if (sinit->sinit_num_ostreams) {
  1427. asoc->c.sinit_num_ostreams =
  1428. sinit->sinit_num_ostreams;
  1429. }
  1430. if (sinit->sinit_max_instreams) {
  1431. asoc->c.sinit_max_instreams =
  1432. sinit->sinit_max_instreams;
  1433. }
  1434. if (sinit->sinit_max_attempts) {
  1435. asoc->max_init_attempts
  1436. = sinit->sinit_max_attempts;
  1437. }
  1438. if (sinit->sinit_max_init_timeo) {
  1439. asoc->max_init_timeo =
  1440. msecs_to_jiffies(sinit->sinit_max_init_timeo);
  1441. }
  1442. }
  1443. /* Prime the peer's transport structures. */
  1444. transport = sctp_assoc_add_peer(asoc, &to, GFP_KERNEL, SCTP_UNKNOWN);
  1445. if (!transport) {
  1446. err = -ENOMEM;
  1447. goto out_free;
  1448. }
  1449. err = sctp_assoc_set_bind_addr_from_ep(asoc, GFP_KERNEL);
  1450. if (err < 0) {
  1451. err = -ENOMEM;
  1452. goto out_free;
  1453. }
  1454. }
  1455. /* ASSERT: we have a valid association at this point. */
  1456. SCTP_DEBUG_PRINTK("We have a valid association.\n");
  1457. if (!sinfo) {
  1458. /* If the user didn't specify SNDRCVINFO, make up one with
  1459. * some defaults.
  1460. */
  1461. default_sinfo.sinfo_stream = asoc->default_stream;
  1462. default_sinfo.sinfo_flags = asoc->default_flags;
  1463. default_sinfo.sinfo_ppid = asoc->default_ppid;
  1464. default_sinfo.sinfo_context = asoc->default_context;
  1465. default_sinfo.sinfo_timetolive = asoc->default_timetolive;
  1466. default_sinfo.sinfo_assoc_id = sctp_assoc2id(asoc);
  1467. sinfo = &default_sinfo;
  1468. }
  1469. /* API 7.1.7, the sndbuf size per association bounds the
  1470. * maximum size of data that can be sent in a single send call.
  1471. */
  1472. if (msg_len > sk->sk_sndbuf) {
  1473. err = -EMSGSIZE;
  1474. goto out_free;
  1475. }
  1476. /* If fragmentation is disabled and the message length exceeds the
  1477. * association fragmentation point, return EMSGSIZE. The I-D
  1478. * does not specify what this error is, but this looks likeā€¦

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