summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/sctp/sctp_init.c
blob: 5547609c98fba79cd7e27a1d961faca9f6aa765f (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * 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/ddi.h>
#include <sys/sunddi.h>

#include <netinet/in.h>
#include <netinet/ip6.h>

#include <inet/common.h>
#include <inet/ipclassifier.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <inet/mib2.h>
#include <inet/nd.h>
#include <inet/optcom.h>
#include <inet/ipclassifier.h>
#include "sctp_impl.h"
#include "sctp_addr.h"

/*
 * This will compute the checksum over the SCTP packet, so this
 * function should only be called after the whole packet has been
 * built.
 *
 * rptr should point to the IP / SCTP composite header.
 * len should be the length of the entire packet, including the IP
 *     header.
 */
void
sctp_add_hdr(sctp_t *sctp, uchar_t *rptr, size_t len)
{
	ipha_t *iphdr;
	short iplen;

	ASSERT(len >= sctp->sctp_hdr_len);

	/* Copy the common header from the template */
	bcopy(sctp->sctp_iphc, rptr, sctp->sctp_hdr_len);

	/* Set the total length in the IP hdr */
	iplen = (short)len;
	iphdr = (ipha_t *)rptr;
	U16_TO_ABE16(iplen, &iphdr->ipha_length);
}

/*ARGSUSED*/
size_t
sctp_supaddr_param_len(sctp_t *sctp)
{
	return (sizeof (sctp_parm_hdr_t) + sizeof (int32_t));
}

size_t
sctp_supaddr_param(sctp_t *sctp, uchar_t *p)
{
	sctp_parm_hdr_t *sph;
	uint16_t *addrtype;

	sph = (sctp_parm_hdr_t *)p;
	sph->sph_type = htons(PARM_SUPP_ADDRS);
	addrtype = (uint16_t *)(sph + 1);
	switch (sctp->sctp_ipversion) {
	case IPV4_VERSION:
		*addrtype++ = htons(PARM_ADDR4);
		*addrtype = 0;
		sph->sph_len = htons(sizeof (*sph) + sizeof (*addrtype));
		break;
	case IPV6_VERSION:
		*addrtype++ = htons(PARM_ADDR6);
		if (!sctp->sctp_connp->conn_ipv6_v6only) {
			*addrtype = htons(PARM_ADDR4);
			sph->sph_len = htons(sizeof (*sph) +
			    sizeof (*addrtype) * 2);
		} else {
			*addrtype = 0;
			sph->sph_len = htons(sizeof (*sph) +
			    sizeof (*addrtype));
		}
		break;
	default:
		break;
	}
	return (sizeof (*sph) + (sizeof (*addrtype) * 2));
}

/*
 * Currently, we support on PRSCTP option, there is more to come.
 */
/*ARGSUSED*/
size_t
sctp_options_param_len(const sctp_t *sctp, int option)
{
	size_t	optlen;

	switch (option) {
	case SCTP_PRSCTP_OPTION:
		optlen = sizeof (sctp_parm_hdr_t);
		break;
	default:
		ASSERT(0);
	}

	return (optlen);
}

/*ARGSUSED*/
size_t
sctp_options_param(const sctp_t *sctp, void *p, int option)
{
	sctp_parm_hdr_t	*sph = (sctp_parm_hdr_t *)p;

	switch (option) {
	case SCTP_PRSCTP_OPTION:
		sph->sph_type = htons(PARM_FORWARD_TSN);
		sph->sph_len = htons(sizeof (*sph));
		break;
	default:
		ASSERT(0);
	}

	return (sizeof (*sph));

}

size_t
sctp_adaptation_code_param(sctp_t *sctp, uchar_t *p)
{
	sctp_parm_hdr_t *sph;

	if (!sctp->sctp_send_adaptation) {
		return (0);
	}
	sph = (sctp_parm_hdr_t *)p;
	sph->sph_type = htons(PARM_ADAPT_LAYER_IND);
	sph->sph_len = htons(sizeof (*sph) + sizeof (uint32_t));
	*(uint32_t *)(sph + 1) = htonl(sctp->sctp_tx_adaptation_code);

	return (sizeof (*sph) + sizeof (uint32_t));
}

mblk_t *
sctp_init_mp(sctp_t *sctp)
{
	mblk_t			*mp;
	uchar_t			*p;
	size_t			initlen;
	sctp_init_chunk_t	*icp;
	sctp_chunk_hdr_t	*chp;
	uint16_t		schlen;
	int			supp_af;
	sctp_stack_t	*sctps = sctp->sctp_sctps;

	if (sctp->sctp_family == AF_INET) {
		supp_af = PARM_SUPP_V4;
	} else {
		/* Assume here that a v6 endpoint supports v4 address. */
		if (sctp->sctp_connp->conn_ipv6_v6only)
			supp_af = PARM_SUPP_V6;
		else
			supp_af = PARM_SUPP_V6 | PARM_SUPP_V4;
	}
	initlen = sizeof (*chp) + sizeof (*icp);
	if (sctp->sctp_send_adaptation) {
		initlen += (sizeof (sctp_parm_hdr_t) + sizeof (uint32_t));
	}
	initlen += sctp_supaddr_param_len(sctp);
	initlen += sctp_addr_params(sctp, supp_af, NULL, B_TRUE);
	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
		initlen += sctp_options_param_len(sctp, SCTP_PRSCTP_OPTION);

	/*
	 * This could be a INIT retransmission in which case sh_verf may
	 * be non-zero, zero it out just to be sure.
	 */
	sctp->sctp_sctph->sh_verf = 0;
	sctp->sctp_sctph6->sh_verf = 0;

	mp = sctp_make_mp(sctp, NULL, initlen);
	if (mp == NULL) {
		SCTP_KSTAT(sctps, sctp_send_init_failed);
		return (NULL);
	}

	/* Lay in a new INIT chunk, starting with the chunk header */
	chp = (sctp_chunk_hdr_t *)mp->b_wptr;
	chp->sch_id = CHUNK_INIT;
	chp->sch_flags = 0;
	schlen = (uint16_t)initlen;
	U16_TO_ABE16(schlen, &(chp->sch_len));

	mp->b_wptr += initlen;

	icp = (sctp_init_chunk_t *)(chp + 1);
	icp->sic_inittag = sctp->sctp_lvtag;
	U32_TO_ABE32(sctp->sctp_rwnd, &(icp->sic_a_rwnd));
	U16_TO_ABE16(sctp->sctp_num_ostr, &(icp->sic_outstr));
	U16_TO_ABE16(sctp->sctp_num_istr, &(icp->sic_instr));
	U32_TO_ABE32(sctp->sctp_ltsn, &(icp->sic_inittsn));

	p = (uchar_t *)(icp + 1);

	/* Adaptation layer param */
	p += sctp_adaptation_code_param(sctp, p);

	/* Add supported address types parameter */
	p += sctp_supaddr_param(sctp, p);

	/* Add address parameters */
	p += sctp_addr_params(sctp, supp_af, p, B_FALSE);

	/* Add Forward-TSN-Supported param */
	if (sctp->sctp_prsctp_aware && sctps->sctps_prsctp_enabled)
		p += sctp_options_param(sctp, p, SCTP_PRSCTP_OPTION);

	BUMP_LOCAL(sctp->sctp_obchunks);

	sctp_set_iplen(sctp, mp);

	return (mp);
}

/*
 * Extracts the verification tag from an INIT chunk. If the INIT
 * chunk is truncated or malformed, returns 0.
 */
uint32_t
sctp_init2vtag(sctp_chunk_hdr_t *initch)
{
	sctp_init_chunk_t *init;

	init = (sctp_init_chunk_t *)(initch + 1);
	return (init->sic_inittag);
}

size_t
sctp_addr_params(sctp_t *sctp, int af, uchar_t *p, boolean_t modify)
{
	size_t	param_len;

	ASSERT(sctp->sctp_nsaddrs > 0);

	/*
	 * If we have only one local address or it is a loopback or linklocal
	 * association, we let the peer pull the address from the IP header.
	 */
	if ((!modify && sctp->sctp_nsaddrs == 1) || sctp->sctp_loopback ||
	    sctp->sctp_linklocal) {
		return (0);
	}

	param_len = sctp_saddr_info(sctp, af, p, modify);
	return ((sctp->sctp_nsaddrs == 1) ? 0 : param_len);
}