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