4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/stream.h>
29 #include <sys/cmn_err.h>
31 #define _SUN_TPI_VERSION 2
32 #include <sys/tihdr.h>
33 #include <sys/stropts.h>
34 #include <sys/socket.h>
35 #include <sys/random.h>
36 #include <sys/policy.h>
38 #include <netinet/in.h>
39 #include <netinet/ip6.h>
41 #include <inet/common.h>
44 #include <inet/ipclassifier.h>
45 #include <inet/sctp/sctp_impl.h>
46 #include <inet/sctp/sctp_asconf.h>
47 #include <inet/sctp/sctp_addr.h>
50 * Minimum number of associations which can be created per listener. Used
51 * when the listener association count is in effect.
53 static uint32_t sctp_min_assoc_listener
= 2;
56 * Returns 0 on success, EACCES on permission failure.
59 sctp_select_port(sctp_t
*sctp
, in_port_t
*requested_port
, int *user_specified
)
61 sctp_stack_t
*sctps
= sctp
->sctp_sctps
;
62 conn_t
*connp
= sctp
->sctp_connp
;
65 * Get a valid port (within the anonymous range and should not
66 * be a privileged one) to use if the user has not given a port.
67 * If multiple threads are here, they may all start with
68 * with the same initial port. But, it should be fine as long as
69 * sctp_bindi will ensure that no two threads will be assigned
72 if (*requested_port
== 0) {
73 *requested_port
= sctp_update_next_port(
74 sctps
->sctps_next_port_to_try
,
75 crgetzone(connp
->conn_cred
), sctps
);
76 if (*requested_port
== 0)
81 boolean_t priv
= B_FALSE
;
84 * If the requested_port is in the well-known privileged range,
85 * verify that the stream was opened by a privileged user.
86 * Note: No locks are held when inspecting sctp_g_*epriv_ports
87 * but instead the code relies on:
88 * - the fact that the address of the array and its size never
90 * - the atomic assignment of the elements of the array
92 if (*requested_port
< sctps
->sctps_smallest_nonpriv_port
) {
95 for (i
= 0; i
< sctps
->sctps_g_num_epriv_ports
; i
++) {
96 if (*requested_port
==
97 sctps
->sctps_g_epriv_ports
[i
]) {
105 * sctp_bind() should take a cred_t argument so that
106 * we can use it here.
108 if (secpolicy_net_privaddr(connp
->conn_cred
,
109 *requested_port
, IPPROTO_SCTP
) != 0) {
111 ("sctp_bind(x): no prive for port %d",
123 sctp_listen(sctp_t
*sctp
)
126 sctp_stack_t
*sctps
= sctp
->sctp_sctps
;
127 conn_t
*connp
= sctp
->sctp_connp
;
131 * TCP handles listen() increasing the backlog, need to check
132 * if it should be handled here too
134 if (sctp
->sctp_state
> SCTPS_BOUND
||
135 (sctp
->sctp_connp
->conn_state_flags
& CONN_CLOSING
)) {
140 /* Do an anonymous bind for unbound socket doing listen(). */
141 if (sctp
->sctp_nsaddrs
== 0) {
142 struct sockaddr_storage ss
;
145 bzero(&ss
, sizeof (ss
));
146 ss
.ss_family
= connp
->conn_family
;
149 if ((ret
= sctp_bind(sctp
, (struct sockaddr
*)&ss
,
155 /* Cache things in the ixa without any refhold */
156 ASSERT(!(connp
->conn_ixa
->ixa_free_flags
& IXA_FREE_CRED
));
157 connp
->conn_ixa
->ixa_cred
= connp
->conn_cred
;
158 connp
->conn_ixa
->ixa_cpid
= connp
->conn_cpid
;
160 sctp
->sctp_state
= SCTPS_LISTEN
;
161 (void) random_get_pseudo_bytes(sctp
->sctp_secret
, SCTP_SECRET_LEN
);
162 sctp
->sctp_last_secret_update
= ddi_get_lbolt64();
163 bzero(sctp
->sctp_old_secret
, SCTP_SECRET_LEN
);
166 * If there is an association limit, allocate and initialize
167 * the counter struct. Note that since listen can be called
168 * multiple times, the struct may have been allready allocated.
170 if (!list_is_empty(&sctps
->sctps_listener_conf
) &&
171 sctp
->sctp_listen_cnt
== NULL
) {
172 sctp_listen_cnt_t
*slc
;
175 ratio
= sctp_find_listener_conf(sctps
,
176 ntohs(connp
->conn_lport
));
178 uint32_t mem_ratio
, tot_buf
;
180 slc
= kmem_alloc(sizeof (sctp_listen_cnt_t
), KM_SLEEP
);
182 * Calculate the connection limit based on
183 * the configured ratio and maxusers. Maxusers
184 * are calculated based on memory size,
185 * ~ 1 user per MB. Note that the conn_rcvbuf
186 * and conn_sndbuf may change after a
187 * connection is accepted. So what we have
188 * is only an approximation.
190 if ((tot_buf
= connp
->conn_rcvbuf
+
191 connp
->conn_sndbuf
) < MB
) {
192 mem_ratio
= MB
/ tot_buf
;
193 slc
->slc_max
= maxusers
/ ratio
* mem_ratio
;
195 mem_ratio
= tot_buf
/ MB
;
196 slc
->slc_max
= maxusers
/ ratio
/ mem_ratio
;
198 /* At least we should allow some associations! */
199 if (slc
->slc_max
< sctp_min_assoc_listener
)
200 slc
->slc_max
= sctp_min_assoc_listener
;
203 sctp
->sctp_listen_cnt
= slc
;
208 tf
= &sctps
->sctps_listen_fanout
[SCTP_LISTEN_HASH(
209 ntohs(connp
->conn_lport
))];
210 sctp_listen_hash_insert(tf
, sctp
);
217 * Bind the sctp_t to a sockaddr, which includes an address and other
218 * information, such as port or flowinfo.
221 sctp_bind(sctp_t
*sctp
, struct sockaddr
*sa
, socklen_t len
)
224 boolean_t bind_to_req_port_only
;
225 in_port_t requested_port
;
226 in_port_t allocated_port
;
228 conn_t
*connp
= sctp
->sctp_connp
;
233 ASSERT(sctp
!= NULL
);
237 if ((sctp
->sctp_state
>= SCTPS_BOUND
) ||
238 (sctp
->sctp_connp
->conn_state_flags
& CONN_CLOSING
) ||
239 (sa
== NULL
|| len
== 0)) {
241 * Multiple binds not allowed for any SCTP socket
242 * Also binding with null address is not supported.
248 switch (sa
->sa_family
) {
251 if (len
< sizeof (struct sockaddr_in
) ||
252 connp
->conn_family
== AF_INET6
) {
256 requested_port
= ntohs(sin
->sin_port
);
260 if (len
< sizeof (struct sockaddr_in6
) ||
261 connp
->conn_family
== AF_INET
) {
265 requested_port
= ntohs(sin6
->sin6_port
);
266 /* Set the flowinfo. */
267 connp
->conn_flowinfo
=
268 sin6
->sin6_flowinfo
& ~IPV6_VERS_AND_FLOW_MASK
;
270 scope_id
= sin6
->sin6_scope_id
;
271 if (scope_id
!= 0 && IN6_IS_ADDR_LINKSCOPE(&sin6
->sin6_addr
)) {
272 connp
->conn_ixa
->ixa_flags
|= IXAF_SCOPEID_SET
;
273 connp
->conn_ixa
->ixa_scopeid
= scope_id
;
274 connp
->conn_incoming_ifindex
= scope_id
;
276 connp
->conn_ixa
->ixa_flags
&= ~IXAF_SCOPEID_SET
;
277 connp
->conn_incoming_ifindex
= connp
->conn_bound_if
;
284 bind_to_req_port_only
= requested_port
== 0 ? B_FALSE
: B_TRUE
;
286 err
= sctp_select_port(sctp
, &requested_port
, &user_specified
);
290 if ((err
= sctp_bind_add(sctp
, sa
, 1, B_TRUE
)) != 0) {
293 err
= sctp_bindi(sctp
, requested_port
, bind_to_req_port_only
,
294 user_specified
, &allocated_port
);
296 sctp_free_saddrs(sctp
);
298 ASSERT(sctp
->sctp_state
== SCTPS_BOUND
);
306 * Perform bind/unbind operation of a list of addresses on a sctp_t
309 sctp_bindx(sctp_t
*sctp
, const void *addrs
, int addrcnt
, int bindop
)
311 ASSERT(sctp
!= NULL
);
312 ASSERT(addrs
!= NULL
);
316 case SCTP_BINDX_ADD_ADDR
:
317 return (sctp_bind_add(sctp
, addrs
, addrcnt
, B_FALSE
));
318 case SCTP_BINDX_REM_ADDR
:
319 return (sctp_bind_del(sctp
, addrs
, addrcnt
, B_FALSE
));
326 * Add a list of addresses to a sctp_t.
329 sctp_bind_add(sctp_t
*sctp
, const void *addrs
, uint32_t addrcnt
,
330 boolean_t caller_hold_lock
)
333 boolean_t do_asconf
= B_FALSE
;
334 sctp_stack_t
*sctps
= sctp
->sctp_sctps
;
336 if (!caller_hold_lock
)
339 if (sctp
->sctp_state
> SCTPS_ESTABLISHED
||
340 (sctp
->sctp_connp
->conn_state_flags
& CONN_CLOSING
)) {
341 if (!caller_hold_lock
)
346 if (sctp
->sctp_state
> SCTPS_LISTEN
) {
348 * Let's do some checking here rather than undoing the
349 * add later (for these reasons).
351 if (!sctps
->sctps_addip_enabled
||
352 !sctp
->sctp_understands_asconf
||
353 !sctp
->sctp_understands_addip
) {
354 if (!caller_hold_lock
)
360 err
= sctp_valid_addr_list(sctp
, addrs
, addrcnt
, NULL
, 0);
362 if (!caller_hold_lock
)
366 /* Need to send ASCONF messages */
368 err
= sctp_add_ip(sctp
, addrs
, addrcnt
);
370 sctp_del_saddr_list(sctp
, addrs
, addrcnt
, B_FALSE
);
371 if (!caller_hold_lock
)
376 if (!caller_hold_lock
)
382 * Remove one or more addresses bound to the sctp_t.
385 sctp_bind_del(sctp_t
*sctp
, const void *addrs
, uint32_t addrcnt
,
386 boolean_t caller_hold_lock
)
389 sctp_stack_t
*sctps
= sctp
->sctp_sctps
;
391 if (!caller_hold_lock
)
394 if (sctp
->sctp_state
> SCTPS_ESTABLISHED
||
395 (sctp
->sctp_connp
->conn_state_flags
& CONN_CLOSING
)) {
396 if (!caller_hold_lock
)
401 * Fail the remove if we are beyond listen, but can't send this
404 if (sctp
->sctp_state
> SCTPS_LISTEN
) {
405 if (!sctps
->sctps_addip_enabled
||
406 !sctp
->sctp_understands_asconf
||
407 !sctp
->sctp_understands_addip
) {
408 if (!caller_hold_lock
)
414 /* Can't delete the last address nor all of the addresses */
415 if (sctp
->sctp_nsaddrs
== 1 || addrcnt
>= sctp
->sctp_nsaddrs
) {
416 if (!caller_hold_lock
)
421 error
= sctp_del_ip(sctp
, addrs
, addrcnt
);
423 if (!caller_hold_lock
)
427 if (!caller_hold_lock
)
433 * Returns 0 for success, errno value otherwise.
435 * If the "bind_to_req_port_only" parameter is set and the requested port
436 * number is available, then set allocated_port to it. If not available,
439 * If the "bind_to_req_port_only" parameter is not set and the requested port
440 * number is available, then set allocated_port to it. If not available,
441 * find the first anonymous port we can and set allocated_port to that. If no
442 * anonymous ports are available, return an error.
444 * In either case, when succeeding, update the sctp_t to record the port number
445 * and insert it in the bind hash table.
448 sctp_bindi(sctp_t
*sctp
, in_port_t port
, boolean_t bind_to_req_port_only
,
449 int user_specified
, in_port_t
*allocated_port
)
451 /* number of times we have run around the loop */
453 /* maximum number of times to run around the loop */
455 sctp_stack_t
*sctps
= sctp
->sctp_sctps
;
456 conn_t
*connp
= sctp
->sctp_connp
;
457 zone_t
*zone
= crgetzone(connp
->conn_cred
);
458 zoneid_t zoneid
= connp
->conn_zoneid
;
461 * Lookup for free addresses is done in a loop and "loopmax"
462 * influences how long we spin in the loop
464 if (bind_to_req_port_only
) {
466 * If the requested port is busy, don't bother to look
467 * for a new one. Setting loop maximum count to 1 has
473 * If the requested port is busy, look for a free one
474 * in the anonymous port range.
475 * Set loopmax appropriately so that one does not look
476 * forever in the case all of the anonymous ports are in use.
478 loopmax
= (sctps
->sctps_largest_anon_port
-
479 sctps
->sctps_smallest_anon_port
+ 1);
490 * Ensure that the sctp_t is not currently in the bind hash.
491 * Hold the lock on the hash bucket to ensure that
492 * the duplicate check plus the insertion is an atomic
495 * This function does an inline lookup on the bind hash list
496 * Make sure that we access only members of sctp_t
497 * and that we don't look at sctp_sctp, since we are not
498 * doing a SCTPB_REFHOLD. For more details please see the notes
501 sctp_bind_hash_remove(sctp
);
502 tbf
= &sctps
->sctps_bind_fanout
[SCTP_BIND_HASH(port
)];
503 mutex_enter(&tbf
->tf_lock
);
504 for (lsctp
= tbf
->tf_sctp
; lsctp
!= NULL
;
505 lsctp
= lsctp
->sctp_bind_hash
) {
506 conn_t
*lconnp
= lsctp
->sctp_connp
;
508 if (lport
!= lconnp
->conn_lport
||
509 lsctp
->sctp_state
< SCTPS_BOUND
)
512 if (lconnp
->conn_zoneid
!= zoneid
)
515 addrcmp
= sctp_compare_saddrs(sctp
, lsctp
);
516 if (addrcmp
!= SCTP_ADDR_DISJOINT
) {
517 if (!connp
->conn_reuseaddr
) {
520 } else if (lsctp
->sctp_state
== SCTPS_BOUND
||
521 lsctp
->sctp_state
== SCTPS_LISTEN
) {
523 * socket option SO_REUSEADDR is set
524 * on the binding sctp_t.
526 * We have found a match of IP source
527 * address and source port, which is
528 * refused regardless of the
529 * SO_REUSEADDR setting, so we break.
536 /* The port number is busy */
537 mutex_exit(&tbf
->tf_lock
);
540 * This port is ours. Insert in fanout and mark as
541 * bound to prevent others from getting the port
544 sctp
->sctp_state
= SCTPS_BOUND
;
545 connp
->conn_lport
= lport
;
547 ASSERT(&sctps
->sctps_bind_fanout
[
548 SCTP_BIND_HASH(port
)] == tbf
);
549 sctp_bind_hash_insert(tbf
, sctp
, 1);
551 mutex_exit(&tbf
->tf_lock
);
554 * We don't want sctp_next_port_to_try to "inherit"
555 * a port number supplied by the user in a bind.
557 * This is the only place where sctp_next_port_to_try
558 * is updated. After the update, it may or may not
559 * be in the valid range.
561 if (user_specified
== 0)
562 sctps
->sctps_next_port_to_try
= port
+ 1;
564 *allocated_port
= port
;
569 if ((count
== 0) && (user_specified
)) {
571 * We may have to return an anonymous port. So
572 * get one to start with.
574 port
= sctp_update_next_port(
575 sctps
->sctps_next_port_to_try
,
579 port
= sctp_update_next_port(port
+ 1, zone
, sctps
);
585 * Don't let this loop run forever in the case where
586 * all of the anonymous ports are in use.
588 } while (++count
< loopmax
);
590 return (bind_to_req_port_only
? EADDRINUSE
: EADDRNOTAVAIL
);
594 * Don't let port fall into the privileged range.
595 * Since the extra privileged ports can be arbitrary we also
596 * ensure that we exclude those from consideration.
597 * sctp_g_epriv_ports is not sorted thus we loop over it until
598 * there are no changes.
600 * Note: No locks are held when inspecting sctp_g_*epriv_ports
601 * but instead the code relies on:
602 * - the fact that the address of the array and its size never changes
603 * - the atomic assignment of the elements of the array
606 sctp_update_next_port(in_port_t port
, zone_t
*zone
, sctp_stack_t
*sctps
)
609 boolean_t restart
= B_FALSE
;
612 if (port
< sctps
->sctps_smallest_anon_port
)
613 port
= sctps
->sctps_smallest_anon_port
;
615 if (port
> sctps
->sctps_largest_anon_port
) {
619 port
= sctps
->sctps_smallest_anon_port
;
622 if (port
< sctps
->sctps_smallest_nonpriv_port
)
623 port
= sctps
->sctps_smallest_nonpriv_port
;
625 for (i
= 0; i
< sctps
->sctps_g_num_epriv_ports
; i
++) {
626 if (port
== sctps
->sctps_g_epriv_ports
[i
]) {
629 * Make sure whether the port is in the
632 * XXX Note that if sctp_g_epriv_ports contains
633 * all the anonymous ports this will be an