xref: /titanic_44/usr/src/uts/common/inet/sctp/sctp_bind.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/stream.h>
32 #include <sys/cmn_err.h>
33 #include <sys/kmem.h>
34 #define	_SUN_TPI_VERSION 2
35 #include <sys/tihdr.h>
36 #include <sys/stropts.h>
37 #include <sys/socket.h>
38 #include <sys/random.h>
39 #include <sys/policy.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/ip6.h>
43 
44 #include <inet/common.h>
45 #include <inet/ip.h>
46 #include <inet/ip6.h>
47 #include <inet/ipclassifier.h>
48 #include "sctp_impl.h"
49 #include "sctp_asconf.h"
50 #include "sctp_addr.h"
51 
52 uint_t	sctp_next_port_to_try;
53 
54 /*
55  * Returns 0 on success, EACCES on permission failure.
56  */
57 static int
58 sctp_select_port(sctp_t *sctp, in_port_t *requested_port, int *user_specified)
59 {
60 	/*
61 	 * Get a valid port (within the anonymous range and should not
62 	 * be a privileged one) to use if the user has not given a port.
63 	 * If multiple threads are here, they may all start with
64 	 * with the same initial port. But, it should be fine as long as
65 	 * sctp_bindi will ensure that no two threads will be assigned
66 	 * the same port.
67 	 */
68 	if (*requested_port == 0) {
69 		*requested_port = sctp_update_next_port(sctp_next_port_to_try);
70 		*user_specified = 0;
71 	} else {
72 		int i;
73 		boolean_t priv = B_FALSE;
74 
75 		/*
76 		 * If the requested_port is in the well-known privileged range,
77 		 * verify that the stream was opened by a privileged user.
78 		 * Note: No locks are held when inspecting sctp_g_*epriv_ports
79 		 * but instead the code relies on:
80 		 * - the fact that the address of the array and its size never
81 		 *   changes
82 		 * - the atomic assignment of the elements of the array
83 		 */
84 		if (*requested_port < sctp_smallest_nonpriv_port) {
85 			priv = B_TRUE;
86 		} else {
87 			for (i = 0; i < sctp_g_num_epriv_ports; i++) {
88 				if (*requested_port == sctp_g_epriv_ports[i]) {
89 					priv = B_TRUE;
90 					break;
91 				}
92 			}
93 		}
94 		if (priv) {
95 			/*
96 			 * sctp_bind() should take a cred_t argument so that
97 			 * we can use it here.
98 			 */
99 			if (secpolicy_net_privaddr(sctp->sctp_credp,
100 			    *requested_port) != 0) {
101 				dprint(1,
102 				    ("sctp_bind(x): no prive for port %d",
103 				    *requested_port));
104 				return (TACCES);
105 			}
106 		}
107 		*user_specified = 1;
108 	}
109 
110 	return (0);
111 }
112 
113 int
114 sctp_listen(sctp_t *sctp)
115 {
116 	sctp_tf_t	*tf;
117 
118 	RUN_SCTP(sctp);
119 	/*
120 	 * TCP handles listen() increasing the backlog, need to check
121 	 * if it should be handled here too - VENU.
122 	 */
123 	if (sctp->sctp_state > SCTPS_BOUND) {
124 		WAKE_SCTP(sctp);
125 		return (EINVAL);
126 	}
127 
128 	/* Do an anonymous bind for unbound socket doing listen(). */
129 	if (sctp->sctp_nsaddrs == 0) {
130 		struct sockaddr_storage ss;
131 		int ret;
132 
133 		bzero(&ss, sizeof (ss));
134 		ss.ss_family = sctp->sctp_family;
135 
136 		WAKE_SCTP(sctp);
137 		if ((ret = sctp_bind(sctp, (struct sockaddr *)&ss,
138 			sizeof (ss))) != 0)
139 			return (ret);
140 		RUN_SCTP(sctp)
141 	}
142 
143 	sctp->sctp_state = SCTPS_LISTEN;
144 	(void) random_get_pseudo_bytes(sctp->sctp_secret, SCTP_SECRET_LEN);
145 	sctp->sctp_last_secret_update = lbolt64;
146 	bzero(sctp->sctp_old_secret, SCTP_SECRET_LEN);
147 	tf = &sctp_listen_fanout[SCTP_LISTEN_HASH(ntohs(sctp->sctp_lport))];
148 	sctp_listen_hash_insert(tf, sctp);
149 
150 	WAKE_SCTP(sctp);
151 	return (0);
152 }
153 
154 /*
155  * Bind the sctp_t to a sockaddr, which includes an address and other
156  * information, such as port or flowinfo.
157  */
158 int
159 sctp_bind(sctp_t *sctp, struct sockaddr *sa, socklen_t len)
160 {
161 	int		user_specified;
162 	boolean_t	bind_to_req_port_only;
163 	in_port_t	requested_port;
164 	in_port_t	allocated_port;
165 	int		err = 0;
166 
167 	ASSERT(sctp != NULL);
168 	ASSERT(sa);
169 
170 	RUN_SCTP(sctp);
171 
172 	if (sctp->sctp_state > SCTPS_BOUND) {
173 		err = EINVAL;
174 		goto done;
175 	}
176 
177 	switch (sa->sa_family) {
178 	case AF_INET:
179 		if (len < sizeof (struct sockaddr_in) ||
180 		    sctp->sctp_family == AF_INET6) {
181 			err = EINVAL;
182 			goto done;
183 		}
184 		requested_port = ntohs(((struct sockaddr_in *)sa)->sin_port);
185 		break;
186 	case AF_INET6:
187 		if (len < sizeof (struct sockaddr_in6) ||
188 		    sctp->sctp_family == AF_INET) {
189 			err = EINVAL;
190 			goto done;
191 		}
192 		requested_port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
193 		/* Set the flowinfo. */
194 		sctp->sctp_ip6h->ip6_vcf =
195 		    (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
196 		    (((struct sockaddr_in6 *)sa)->sin6_flowinfo &
197 		    ~IPV6_VERS_AND_FLOW_MASK);
198 		break;
199 	default:
200 		err = EAFNOSUPPORT;
201 		goto done;
202 	}
203 	bind_to_req_port_only = requested_port == 0 ? B_FALSE : B_TRUE;
204 
205 	if (sctp_select_port(sctp, &requested_port, &user_specified) != 0) {
206 		err = EPERM;
207 		goto done;
208 	}
209 
210 	if ((err = sctp_bind_add(sctp, sa, 1, B_TRUE)) != 0)
211 		goto done;
212 
213 	allocated_port = sctp_bindi(sctp, requested_port,
214 	    bind_to_req_port_only, user_specified);
215 	if (allocated_port == 0) {
216 		sctp_free_saddrs(sctp);
217 		if (bind_to_req_port_only) {
218 			err = EADDRINUSE;
219 			goto done;
220 		} else {
221 			err = EADDRNOTAVAIL;
222 			goto done;
223 		}
224 	}
225 	ASSERT(sctp->sctp_state == SCTPS_BOUND);
226 done:
227 	WAKE_SCTP(sctp);
228 	return (err);
229 }
230 
231 /*
232  * Perform bind/unbind operation of a list of addresses on a sctp_t
233  */
234 int
235 sctp_bindx(sctp_t *sctp, const void *addrs, int addrcnt, int bindop)
236 {
237 	ASSERT(sctp != NULL);
238 	ASSERT(addrs != NULL);
239 	ASSERT(addrcnt > 0);
240 
241 	switch (bindop) {
242 	case SCTP_BINDX_ADD_ADDR:
243 		return (sctp_bind_add(sctp, addrs, addrcnt, B_FALSE));
244 	case SCTP_BINDX_REM_ADDR:
245 		return (sctp_bind_del(sctp, addrs, addrcnt, B_FALSE));
246 	default:
247 		return (EINVAL);
248 	}
249 }
250 
251 /*
252  * Add a list of addresses to a sctp_t.
253  */
254 int
255 sctp_bind_add(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
256     boolean_t caller_hold_lock)
257 {
258 	int		err = 0;
259 	boolean_t	do_asconf = B_FALSE;
260 
261 	if (!caller_hold_lock)
262 		RUN_SCTP(sctp);
263 
264 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
265 		if (!caller_hold_lock)
266 			WAKE_SCTP(sctp);
267 		return (EINVAL);
268 	}
269 	if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
270 		do_asconf = B_TRUE;
271 	err = sctp_valid_addr_list(sctp, addrs, addrcnt);
272 	if (err != 0) {
273 		if (!caller_hold_lock)
274 			WAKE_SCTP(sctp);
275 		return (err);
276 	}
277 
278 	/* Need to send  ASCONF messages */
279 	if (do_asconf) {
280 		err = sctp_add_ip(sctp, addrs, addrcnt);
281 		if (err != 0) {
282 			sctp_del_saddr_list(sctp, addrs, addrcnt, B_FALSE);
283 			if (!caller_hold_lock)
284 				WAKE_SCTP(sctp);
285 			return (err);
286 		}
287 	}
288 	if (!caller_hold_lock)
289 		WAKE_SCTP(sctp);
290 	if (do_asconf)
291 		sctp_process_sendq(sctp);
292 	return (0);
293 }
294 
295 /*
296  * Remove one or more addresses bound to the sctp_t.
297  */
298 int
299 sctp_bind_del(sctp_t *sctp, const void *addrs, uint32_t addrcnt,
300     boolean_t caller_hold_lock)
301 {
302 	int		error = 0;
303 	boolean_t	do_asconf = B_FALSE;
304 
305 	if (!caller_hold_lock)
306 		RUN_SCTP(sctp);
307 
308 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
309 		if (!caller_hold_lock)
310 			WAKE_SCTP(sctp);
311 		return (EINVAL);
312 	}
313 	if (sctp->sctp_state > SCTPS_LISTEN && sctp_addip_enabled)
314 		do_asconf = B_TRUE;
315 
316 	/* Can't delete the last address nor all of the addresses */
317 	if (sctp->sctp_nsaddrs == 1 || addrcnt >= sctp->sctp_nsaddrs) {
318 		if (!caller_hold_lock)
319 			WAKE_SCTP(sctp);
320 		return (EINVAL);
321 	}
322 
323 	error = sctp_del_ip(sctp, addrs, addrcnt);
324 	if (!caller_hold_lock)
325 		WAKE_SCTP(sctp);
326 	if (error == 0 && do_asconf)
327 		sctp_process_sendq(sctp);
328 	return (error);
329 }
330 
331 /*
332  * If the "bind_to_req_port_only" parameter is set, if the requested port
333  * number is available, return it, If not return 0
334  *
335  * If "bind_to_req_port_only" parameter is not set and
336  * If the requested port number is available, return it.  If not, return
337  * the first anonymous port we happen across.  If no anonymous ports are
338  * available, return 0. addr is the requested local address, if any.
339  *
340  * In either case, when succeeding update the sctp_t to record the port number
341  * and insert it in the bind hash table.
342  */
343 in_port_t
344 sctp_bindi(sctp_t *sctp, in_port_t port, int bind_to_req_port_only,
345     int user_specified)
346 {
347 	/* number of times we have run around the loop */
348 	int count = 0;
349 	/* maximum number of times to run around the loop */
350 	int loopmax;
351 	zoneid_t zoneid = sctp->sctp_zoneid;
352 
353 	/*
354 	 * Lookup for free addresses is done in a loop and "loopmax"
355 	 * influences how long we spin in the loop
356 	 */
357 	if (bind_to_req_port_only) {
358 		/*
359 		 * If the requested port is busy, don't bother to look
360 		 * for a new one. Setting loop maximum count to 1 has
361 		 * that effect.
362 		 */
363 		loopmax = 1;
364 	} else {
365 		/*
366 		 * If the requested port is busy, look for a free one
367 		 * in the anonymous port range.
368 		 * Set loopmax appropriately so that one does not look
369 		 * forever in the case all of the anonymous ports are in use.
370 		 */
371 		loopmax = (sctp_largest_anon_port -
372 		    sctp_smallest_anon_port + 1);
373 	}
374 	do {
375 		uint16_t	lport;
376 		sctp_tf_t	*tbf;
377 		sctp_t		*lsctp;
378 		int		addrcmp;
379 
380 		lport = htons(port);
381 
382 		/*
383 		 * Ensure that the sctp_t is not currently in the bind hash.
384 		 * Hold the lock on the hash bucket to ensure that
385 		 * the duplicate check plus the insertion is an atomic
386 		 * operation.
387 		 *
388 		 * This function does an inline lookup on the bind hash list
389 		 * Make sure that we access only members of sctp_t
390 		 * and that we don't look at sctp_sctp, since we are not
391 		 * doing a SCTPB_REFHOLD. For more details please see the notes
392 		 * in sctp_compress()
393 		 */
394 		sctp_bind_hash_remove(sctp);
395 		tbf = &sctp_bind_fanout[SCTP_BIND_HASH(port)];
396 		mutex_enter(&tbf->tf_lock);
397 		for (lsctp = tbf->tf_sctp; lsctp != NULL;
398 		    lsctp = lsctp->sctp_bind_hash) {
399 
400 			if (lport != lsctp->sctp_lport ||
401 			    lsctp->sctp_zoneid != zoneid ||
402 			    lsctp->sctp_state < SCTPS_BOUND)
403 				continue;
404 
405 			addrcmp = sctp_compare_saddrs(sctp, lsctp);
406 			if (addrcmp != SCTP_ADDR_DISJOINT) {
407 				if (!sctp->sctp_reuseaddr) {
408 					/* in use */
409 					break;
410 				} else if (lsctp->sctp_state == SCTPS_BOUND ||
411 				    lsctp->sctp_state == SCTPS_LISTEN) {
412 					/*
413 					 * socket option SO_REUSEADDR is set
414 					 * on the binding sctp_t.
415 					 *
416 					 * We have found a match of IP source
417 					 * address and source port, which is
418 					 * refused regardless of the
419 					 * SO_REUSEADDR setting, so we break.
420 					 */
421 					break;
422 				}
423 			}
424 		}
425 		if (lsctp != NULL) {
426 			/* The port number is busy */
427 			mutex_exit(&tbf->tf_lock);
428 		} else {
429 			/*
430 			 * This port is ours. Insert in fanout and mark as
431 			 * bound to prevent others from getting the port
432 			 * number.
433 			 */
434 			sctp->sctp_state = SCTPS_BOUND;
435 			sctp->sctp_lport = lport;
436 			sctp->sctp_sctph->sh_sport = sctp->sctp_lport;
437 
438 			ASSERT(&sctp_bind_fanout[SCTP_BIND_HASH(port)] == tbf);
439 			sctp_bind_hash_insert(tbf, sctp, 1);
440 
441 			mutex_exit(&tbf->tf_lock);
442 
443 			/*
444 			 * We don't want sctp_next_port_to_try to "inherit"
445 			 * a port number supplied by the user in a bind.
446 			 */
447 			if (user_specified != 0)
448 				return (port);
449 
450 			/*
451 			 * This is the only place where sctp_next_port_to_try
452 			 * is updated. After the update, it may or may not
453 			 * be in the valid range.
454 			 */
455 			sctp_next_port_to_try = port + 1;
456 			return (port);
457 		}
458 
459 		if ((count == 0) && (user_specified)) {
460 			/*
461 			 * We may have to return an anonymous port. So
462 			 * get one to start with.
463 			 */
464 			port = sctp_update_next_port(sctp_next_port_to_try);
465 			user_specified = 0;
466 		} else {
467 			port = sctp_update_next_port(port + 1);
468 		}
469 
470 		/*
471 		 * Don't let this loop run forever in the case where
472 		 * all of the anonymous ports are in use.
473 		 */
474 	} while (++count < loopmax);
475 	return (0);
476 }
477 
478 /*
479  * Don't let port fall into the privileged range.
480  * Since the extra privileged ports can be arbitrary we also
481  * ensure that we exclude those from consideration.
482  * sctp_g_epriv_ports is not sorted thus we loop over it until
483  * there are no changes.
484  *
485  * Note: No locks are held when inspecting sctp_g_*epriv_ports
486  * but instead the code relies on:
487  * - the fact that the address of the array and its size never changes
488  * - the atomic assignment of the elements of the array
489  */
490 in_port_t
491 sctp_update_next_port(in_port_t port)
492 {
493 	int i;
494 
495 retry:
496 	if (port < sctp_smallest_anon_port || port > sctp_largest_anon_port)
497 		port = sctp_smallest_anon_port;
498 
499 	if (port < sctp_smallest_nonpriv_port)
500 		port = sctp_smallest_nonpriv_port;
501 
502 	for (i = 0; i < sctp_g_num_epriv_ports; i++) {
503 		if (port == sctp_g_epriv_ports[i]) {
504 			port++;
505 			/*
506 			 * Make sure whether the port is in the
507 			 * valid range.
508 			 *
509 			 * XXX Note that if sctp_g_epriv_ports contains
510 			 * all the anonymous ports this will be an
511 			 * infinite loop.
512 			 */
513 			goto retry;
514 		}
515 	}
516 	return (port);
517 }
518