PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

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

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

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