summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/sctp/sctp_ioc.c
blob: 6fa9abc632f1b6cb7fda96be0c1e0ea562ee3fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#define	_SUN_TPI_VERSION 2
#include <sys/tihdr.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/kmem.h>
#include <sys/random.h>
#include <sys/policy.h>

#include <netinet/in.h>

#include <inet/common.h>
#include <inet/ip.h>
#include <inet/nd.h>
#include <inet/ipclassifier.h>
#include <inet/optcom.h>
#include <inet/sctp_ip.h>
#include "sctp_impl.h"

/*
 * We need a stream q for sending packets to IP.  This q should
 * be set in strplumb() time.  Once it is set, it will never
 * be removed.  Since it is done in strplumb() time, there is
 * no need to have a lock on the default q.
 */
static void
sctp_def_q_set(queue_t *q, mblk_t *mp)
{
	conn_t		*connp = (conn_t *)q->q_ptr;
	struct iocblk	*iocp = (struct iocblk *)mp->b_rptr;
	mblk_t		*mp1;
	hrtime_t	t;
	sctp_stack_t	*sctps = connp->conn_netstack->
	    netstack_sctp;

	ASSERT(connp != NULL && connp->conn_ulp == IPPROTO_SCTP &&
	    connp->conn_rq == NULL);

	if ((mp1 = mp->b_cont) == NULL) {
		iocp->ioc_error = EINVAL;
		ip0dbg(("sctp_def_q_set: no file descriptor\n"));
		goto done;
	}

	mutex_enter(&sctps->sctps_g_q_lock);
	if (sctps->sctps_g_q != NULL) {
		mutex_exit(&sctps->sctps_g_q_lock);
		ip0dbg(("sctp_def_q_set: already set\n"));
		iocp->ioc_error = EALREADY;
		goto done;
	}

	sctps->sctps_g_q = q;
	mutex_exit(&sctps->sctps_g_q_lock);
	sctps->sctps_gsctp = (sctp_t *)sctp_create(NULL, NULL, AF_INET6,
	    SCTP_CAN_BLOCK, NULL, NULL, connp->conn_cred);
	mutex_enter(&sctps->sctps_g_q_lock);
	if (sctps->sctps_gsctp == NULL) {
		sctps->sctps_g_q = NULL;
		mutex_exit(&sctps->sctps_g_q_lock);
		iocp->ioc_error = ENOMEM;
		goto done;
	}
	mutex_exit(&sctps->sctps_g_q_lock);
	ASSERT(sctps->sctps_g_q_ref >= 1);
	ASSERT(list_head(&sctps->sctps_g_list) == sctps->sctps_gsctp);

	/*
	 * As a good citizen of using /dev/urandom, add some entropy
	 * to the random number pool.
	 */
	t = gethrtime();
	(void) random_add_entropy((uint8_t *)&t, sizeof (t), 0);
done:
	if (mp1 != NULL) {
		freemsg(mp1);
		mp->b_cont = NULL;
	}
	iocp->ioc_count = 0;
	mp->b_datap->db_type = M_IOCACK;
	qreply(q, mp);
}


/*
 * sctp_wput_ioctl is called by sctp_wput_slow to handle all
 * M_IOCTL messages.
 */
void
sctp_wput_ioctl(queue_t *q, mblk_t *mp)
{
	conn_t	*connp = (conn_t *)q->q_ptr;
	struct iocblk	*iocp;
	cred_t *cr;

	if (connp == NULL) {
		ip0dbg(("sctp_wput_ioctl: null conn\n"));
		return;
	}

	iocp = (struct iocblk *)mp->b_rptr;
	cr = DB_CREDDEF(mp, iocp->ioc_cr);
	switch (iocp->ioc_cmd) {
	case SCTP_IOC_DEFAULT_Q:
		/* Wants to be the default wq. */
		if (cr != NULL && secpolicy_ip_config(cr, B_FALSE) != 0) {
			iocp->ioc_error = EPERM;
			goto err_ret;
		}
		sctp_def_q_set(q, mp);
		return;

	case ND_SET:
		/* sctp_nd_getset() -> nd_getset() does the checking. */
	case ND_GET:
		if (!sctp_nd_getset(q, mp)) {
			break;
		}
		qreply(q, mp);
		return;
	default:
		iocp->ioc_error = EOPNOTSUPP;
		break;
	}
err_ret:
	iocp->ioc_count = 0;
	mp->b_datap->db_type = M_IOCNAK;
	qreply(q, mp);
}