sctp_input.c revision 1.10 1 1.1 rjs /* $KAME: sctp_input.c,v 1.28 2005/04/21 18:36:21 nishida Exp $ */
2 1.10 riastrad /* $NetBSD: sctp_input.c,v 1.10 2018/09/03 16:29:36 riastradh Exp $ */
3 1.1 rjs
4 1.1 rjs /*
5 1.1 rjs * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
6 1.1 rjs * All rights reserved.
7 1.1 rjs *
8 1.1 rjs * Redistribution and use in source and binary forms, with or without
9 1.1 rjs * modification, are permitted provided that the following conditions
10 1.1 rjs * are met:
11 1.1 rjs * 1. Redistributions of source code must retain the above copyright
12 1.1 rjs * notice, this list of conditions and the following disclaimer.
13 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rjs * notice, this list of conditions and the following disclaimer in the
15 1.1 rjs * documentation and/or other materials provided with the distribution.
16 1.1 rjs * 3. Neither the name of the project nor the names of its contributors
17 1.1 rjs * may be used to endorse or promote products derived from this software
18 1.1 rjs * without specific prior written permission.
19 1.1 rjs *
20 1.1 rjs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 rjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 rjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 rjs * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 rjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 rjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 rjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 rjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 rjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 rjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 rjs * SUCH DAMAGE.
31 1.1 rjs */
32 1.1 rjs
33 1.1 rjs #include <sys/cdefs.h>
34 1.10 riastrad __KERNEL_RCSID(0, "$NetBSD: sctp_input.c,v 1.10 2018/09/03 16:29:36 riastradh Exp $");
35 1.1 rjs
36 1.1 rjs #ifdef _KERNEL_OPT
37 1.1 rjs #include "opt_ipsec.h"
38 1.1 rjs #include "opt_inet.h"
39 1.1 rjs #include "opt_sctp.h"
40 1.1 rjs #endif /* _KERNEL_OPT */
41 1.1 rjs
42 1.1 rjs #include <sys/param.h>
43 1.1 rjs #include <sys/systm.h>
44 1.1 rjs #include <sys/malloc.h>
45 1.1 rjs #include <sys/mbuf.h>
46 1.1 rjs #include <sys/socket.h>
47 1.1 rjs #include <sys/socketvar.h>
48 1.1 rjs #include <sys/sysctl.h>
49 1.1 rjs #include <sys/domain.h>
50 1.1 rjs #include <sys/protosw.h>
51 1.1 rjs #include <sys/kernel.h>
52 1.1 rjs #include <sys/errno.h>
53 1.1 rjs #include <sys/syslog.h>
54 1.1 rjs
55 1.1 rjs #include <machine/limits.h>
56 1.1 rjs #include <machine/cpu.h>
57 1.1 rjs
58 1.1 rjs #include <net/if.h>
59 1.1 rjs #include <net/route.h>
60 1.1 rjs #include <net/if_types.h>
61 1.1 rjs
62 1.1 rjs #include <netinet/in.h>
63 1.1 rjs #include <netinet/in_systm.h>
64 1.1 rjs #include <netinet/ip.h>
65 1.1 rjs #include <netinet/in_pcb.h>
66 1.1 rjs #include <netinet/in_var.h>
67 1.1 rjs #include <netinet/ip_var.h>
68 1.1 rjs
69 1.1 rjs #ifdef INET6
70 1.1 rjs #include <netinet/ip6.h>
71 1.1 rjs #include <netinet6/ip6_var.h>
72 1.1 rjs #endif /* INET6 */
73 1.1 rjs
74 1.1 rjs #include <netinet/ip_icmp.h>
75 1.1 rjs #include <netinet/icmp_var.h>
76 1.1 rjs #include <netinet/sctp_var.h>
77 1.1 rjs #include <netinet/sctp_pcb.h>
78 1.1 rjs #include <netinet/sctp_header.h>
79 1.1 rjs #include <netinet/sctputil.h>
80 1.1 rjs #include <netinet/sctp_output.h>
81 1.1 rjs #include <netinet/sctp_input.h>
82 1.1 rjs #include <netinet/sctp_hashdriver.h>
83 1.1 rjs #include <netinet/sctp_indata.h>
84 1.1 rjs #include <netinet/sctp_asconf.h>
85 1.1 rjs
86 1.1 rjs #ifdef IPSEC
87 1.2 rjs #include <netipsec/ipsec.h>
88 1.2 rjs #include <netipsec/key.h>
89 1.1 rjs #endif /*IPSEC*/
90 1.1 rjs
91 1.1 rjs #ifdef SCTP_DEBUG
92 1.1 rjs extern u_int32_t sctp_debug_on;
93 1.1 rjs #endif
94 1.1 rjs
95 1.1 rjs /* INIT handler */
96 1.1 rjs static void
97 1.1 rjs sctp_handle_init(struct mbuf *m, int iphlen, int offset,
98 1.1 rjs struct sctphdr *sh, struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
99 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
100 1.1 rjs {
101 1.1 rjs struct sctp_init *init;
102 1.1 rjs struct mbuf *op_err;
103 1.1 rjs #ifdef SCTP_DEBUG
104 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
105 1.1 rjs printf("sctp_handle_init: handling INIT tcb:%p\n", stcb);
106 1.1 rjs }
107 1.1 rjs #endif
108 1.1 rjs op_err = NULL;
109 1.1 rjs init = &cp->init;
110 1.1 rjs /* First are we accepting? */
111 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
112 1.1 rjs (inp->sctp_socket->so_qlimit == 0)) {
113 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
114 1.1 rjs return;
115 1.1 rjs }
116 1.1 rjs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
117 1.1 rjs /* Invalid length */
118 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
119 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
120 1.1 rjs return;
121 1.1 rjs }
122 1.1 rjs /* validate parameters */
123 1.1 rjs if (init->initiate_tag == 0) {
124 1.1 rjs /* protocol error... send abort */
125 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
126 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
127 1.1 rjs return;
128 1.1 rjs }
129 1.1 rjs if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
130 1.1 rjs /* invalid parameter... send abort */
131 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
132 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
133 1.1 rjs return;
134 1.1 rjs }
135 1.1 rjs if (init->num_inbound_streams == 0) {
136 1.1 rjs /* protocol error... send abort */
137 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
138 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
139 1.1 rjs return;
140 1.1 rjs }
141 1.1 rjs if (init->num_outbound_streams == 0) {
142 1.1 rjs /* protocol error... send abort */
143 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
144 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh, op_err);
145 1.1 rjs return;
146 1.1 rjs }
147 1.1 rjs
148 1.1 rjs /* send an INIT-ACK w/cookie */
149 1.1 rjs #ifdef SCTP_DEBUG
150 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
151 1.1 rjs printf("sctp_handle_init: sending INIT-ACK\n");
152 1.1 rjs }
153 1.1 rjs #endif
154 1.1 rjs
155 1.1 rjs sctp_send_initiate_ack(inp, stcb, m, iphlen, offset, sh, cp);
156 1.1 rjs }
157 1.1 rjs
158 1.1 rjs /*
159 1.1 rjs * process peer "INIT/INIT-ACK" chunk
160 1.1 rjs * returns value < 0 on error
161 1.1 rjs */
162 1.1 rjs
163 1.1 rjs static int
164 1.1 rjs sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb,
165 1.1 rjs struct sctp_nets *net)
166 1.1 rjs {
167 1.1 rjs struct sctp_init *init;
168 1.1 rjs struct sctp_association *asoc;
169 1.1 rjs struct sctp_nets *lnet;
170 1.1 rjs unsigned int i;
171 1.1 rjs
172 1.1 rjs init = &cp->init;
173 1.1 rjs asoc = &stcb->asoc;
174 1.1 rjs /* save off parameters */
175 1.1 rjs asoc->peer_vtag = ntohl(init->initiate_tag);
176 1.1 rjs asoc->peers_rwnd = ntohl(init->a_rwnd);
177 1.1 rjs
178 1.1 rjs if (TAILQ_FIRST(&asoc->nets)) {
179 1.1 rjs /* update any ssthresh's that may have a default */
180 1.1 rjs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
181 1.1 rjs lnet->ssthresh = asoc->peers_rwnd;
182 1.1 rjs }
183 1.1 rjs }
184 1.1 rjs if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
185 1.1 rjs unsigned int newcnt;
186 1.1 rjs struct sctp_stream_out *outs;
187 1.1 rjs struct sctp_tmit_chunk *chk;
188 1.1 rjs
189 1.1 rjs /* cut back on number of streams */
190 1.1 rjs newcnt = ntohs(init->num_inbound_streams);
191 1.1 rjs /* This if is probably not needed but I am cautious */
192 1.1 rjs if (asoc->strmout) {
193 1.1 rjs /* First make sure no data chunks are trapped */
194 1.1 rjs for (i=newcnt; i < asoc->pre_open_streams; i++) {
195 1.1 rjs outs = &asoc->strmout[i];
196 1.1 rjs chk = TAILQ_FIRST(&outs->outqueue);
197 1.1 rjs while (chk) {
198 1.1 rjs TAILQ_REMOVE(&outs->outqueue, chk,
199 1.1 rjs sctp_next);
200 1.1 rjs asoc->stream_queue_cnt--;
201 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL,
202 1.1 rjs stcb, SCTP_NOTIFY_DATAGRAM_UNSENT,
203 1.1 rjs chk);
204 1.1 rjs if (chk->data) {
205 1.1 rjs sctp_m_freem(chk->data);
206 1.1 rjs chk->data = NULL;
207 1.1 rjs }
208 1.1 rjs sctp_free_remote_addr(chk->whoTo);
209 1.1 rjs chk->whoTo = NULL;
210 1.1 rjs chk->asoc = NULL;
211 1.1 rjs /* Free the chunk */
212 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
213 1.1 rjs sctppcbinfo.ipi_count_chunk--;
214 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
215 1.1 rjs panic("Chunk count is negative");
216 1.1 rjs }
217 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
218 1.1 rjs chk = TAILQ_FIRST(&outs->outqueue);
219 1.1 rjs }
220 1.1 rjs }
221 1.1 rjs }
222 1.1 rjs /* cut back the count and abandon the upper streams */
223 1.1 rjs asoc->pre_open_streams = newcnt;
224 1.1 rjs }
225 1.1 rjs asoc->streamincnt = ntohs(init->num_outbound_streams);
226 1.1 rjs if (asoc->streamincnt > MAX_SCTP_STREAMS) {
227 1.1 rjs asoc->streamincnt = MAX_SCTP_STREAMS;
228 1.1 rjs }
229 1.1 rjs
230 1.1 rjs asoc->streamoutcnt = asoc->pre_open_streams;
231 1.1 rjs /* init tsn's */
232 1.1 rjs asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
233 1.1 rjs #ifdef SCTP_MAP_LOGGING
234 1.1 rjs sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
235 1.1 rjs #endif
236 1.1 rjs /* This is the next one we expect */
237 1.1 rjs asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
238 1.1 rjs
239 1.1 rjs asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
240 1.1 rjs asoc->cumulative_tsn = asoc->asconf_seq_in;
241 1.1 rjs asoc->last_echo_tsn = asoc->asconf_seq_in;
242 1.1 rjs asoc->advanced_peer_ack_point = asoc->last_acked_seq;
243 1.1 rjs /* open the requested streams */
244 1.1 rjs if (asoc->strmin != NULL) {
245 1.1 rjs /* Free the old ones */
246 1.1 rjs free(asoc->strmin, M_PCB);
247 1.1 rjs }
248 1.1 rjs asoc->strmin = malloc(asoc->streamincnt * sizeof(struct sctp_stream_in),
249 1.1 rjs M_PCB, M_NOWAIT);
250 1.1 rjs if (asoc->strmin == NULL) {
251 1.1 rjs /* we didn't get memory for the streams! */
252 1.1 rjs #ifdef SCTP_DEBUG
253 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
254 1.1 rjs printf("process_init: couldn't get memory for the streams!\n");
255 1.1 rjs }
256 1.1 rjs #endif
257 1.1 rjs return (-1);
258 1.1 rjs }
259 1.1 rjs for (i = 0; i < asoc->streamincnt; i++) {
260 1.1 rjs asoc->strmin[i].stream_no = i;
261 1.1 rjs asoc->strmin[i].last_sequence_delivered = 0xffff;
262 1.1 rjs /*
263 1.1 rjs * U-stream ranges will be set when the cookie
264 1.1 rjs * is unpacked. Or for the INIT sender they
265 1.1 rjs * are un set (if pr-sctp not supported) when the
266 1.1 rjs * INIT-ACK arrives.
267 1.1 rjs */
268 1.1 rjs TAILQ_INIT(&asoc->strmin[i].inqueue);
269 1.1 rjs /*
270 1.1 rjs * we are not on any wheel, pr-sctp streams
271 1.1 rjs * will go on the wheel when they have data waiting
272 1.1 rjs * for reorder.
273 1.1 rjs */
274 1.1 rjs asoc->strmin[i].next_spoke.tqe_next = 0;
275 1.1 rjs asoc->strmin[i].next_spoke.tqe_prev = 0;
276 1.1 rjs }
277 1.1 rjs
278 1.1 rjs /*
279 1.1 rjs * load_address_from_init will put the addresses into the
280 1.1 rjs * association when the COOKIE is processed or the INIT-ACK
281 1.1 rjs * is processed. Both types of COOKIE's existing and new
282 1.1 rjs * call this routine. It will remove addresses that
283 1.1 rjs * are no longer in the association (for the restarting
284 1.1 rjs * case where addresses are removed). Up front when the
285 1.1 rjs * INIT arrives we will discard it if it is a restart
286 1.1 rjs * and new addresses have been added.
287 1.1 rjs */
288 1.1 rjs return (0);
289 1.1 rjs }
290 1.1 rjs
291 1.1 rjs /*
292 1.1 rjs * INIT-ACK message processing/consumption
293 1.1 rjs * returns value < 0 on error
294 1.1 rjs */
295 1.1 rjs static int
296 1.1 rjs sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
297 1.1 rjs struct sctphdr *sh, struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
298 1.1 rjs struct sctp_nets *net)
299 1.1 rjs {
300 1.1 rjs struct sctp_association *asoc;
301 1.1 rjs struct mbuf *op_err;
302 1.1 rjs int retval, abort_flag;
303 1.1 rjs uint32_t initack_limit;
304 1.1 rjs /* First verify that we have no illegal param's */
305 1.1 rjs abort_flag = 0;
306 1.1 rjs op_err = NULL;
307 1.1 rjs
308 1.1 rjs op_err = sctp_arethere_unrecognized_parameters(m,
309 1.1 rjs (offset+sizeof(struct sctp_init_chunk)) ,
310 1.1 rjs &abort_flag, (struct sctp_chunkhdr *)cp);
311 1.1 rjs if (abort_flag) {
312 1.1 rjs /* Send an abort and notify peer */
313 1.1 rjs if (op_err != NULL) {
314 1.1 rjs sctp_send_operr_to(m, iphlen, op_err, cp->init.initiate_tag);
315 1.1 rjs } else {
316 1.1 rjs /*
317 1.1 rjs * Just notify (abort_assoc does this if
318 1.1 rjs * we send an abort).
319 1.1 rjs */
320 1.1 rjs sctp_abort_notification(stcb, 0);
321 1.1 rjs /*
322 1.1 rjs * No sense in further INIT's since
323 1.1 rjs * we will get the same param back
324 1.1 rjs */
325 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
326 1.1 rjs }
327 1.1 rjs return (-1);
328 1.1 rjs }
329 1.1 rjs asoc = &stcb->asoc;
330 1.1 rjs /* process the peer's parameters in the INIT-ACK */
331 1.1 rjs retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb, net);
332 1.1 rjs if (retval < 0) {
333 1.1 rjs return (retval);
334 1.1 rjs }
335 1.1 rjs
336 1.1 rjs initack_limit = offset + ntohs(cp->ch.chunk_length);
337 1.1 rjs /* load all addresses */
338 1.1 rjs if (sctp_load_addresses_from_init(stcb, m, iphlen,
339 1.1 rjs (offset + sizeof(struct sctp_init_chunk)), initack_limit, sh,
340 1.1 rjs NULL)) {
341 1.1 rjs /* Huh, we should abort */
342 1.1 rjs sctp_abort_notification(stcb, 0);
343 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
344 1.1 rjs return (-1);
345 1.1 rjs }
346 1.1 rjs if (op_err) {
347 1.1 rjs sctp_queue_op_err(stcb, op_err);
348 1.1 rjs /* queuing will steal away the mbuf chain to the out queue */
349 1.1 rjs op_err = NULL;
350 1.1 rjs }
351 1.1 rjs /* extract the cookie and queue it to "echo" it back... */
352 1.1 rjs stcb->asoc.overall_error_count = 0;
353 1.1 rjs net->error_count = 0;
354 1.1 rjs retval = sctp_send_cookie_echo(m, offset, stcb, net);
355 1.1 rjs if (retval < 0) {
356 1.1 rjs /*
357 1.1 rjs * No cookie, we probably should send a op error.
358 1.1 rjs * But in any case if there is no cookie in the INIT-ACK,
359 1.1 rjs * we can abandon the peer, its broke.
360 1.1 rjs */
361 1.1 rjs if (retval == -3) {
362 1.1 rjs /* We abort with an error of missing mandatory param */
363 1.1 rjs op_err =
364 1.1 rjs sctp_generate_invmanparam(SCTP_CAUSE_MISS_PARAM);
365 1.1 rjs if (op_err) {
366 1.1 rjs /*
367 1.1 rjs * Expand beyond to include the mandatory
368 1.1 rjs * param cookie
369 1.1 rjs */
370 1.1 rjs struct sctp_inv_mandatory_param *mp;
371 1.1 rjs op_err->m_len =
372 1.1 rjs sizeof(struct sctp_inv_mandatory_param);
373 1.1 rjs mp = mtod(op_err,
374 1.1 rjs struct sctp_inv_mandatory_param *);
375 1.1 rjs /* Subtract the reserved param */
376 1.1 rjs mp->length =
377 1.1 rjs htons(sizeof(struct sctp_inv_mandatory_param) - 2);
378 1.1 rjs mp->num_param = htonl(1);
379 1.1 rjs mp->param = htons(SCTP_STATE_COOKIE);
380 1.1 rjs mp->resv = 0;
381 1.1 rjs }
382 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
383 1.1 rjs sh, op_err);
384 1.1 rjs }
385 1.1 rjs return (retval);
386 1.1 rjs }
387 1.1 rjs
388 1.1 rjs /*
389 1.1 rjs * Cancel the INIT timer, We do this first before queueing
390 1.1 rjs * the cookie. We always cancel at the primary to assue that
391 1.1 rjs * we are canceling the timer started by the INIT which always
392 1.1 rjs * goes to the primary.
393 1.1 rjs */
394 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
395 1.1 rjs asoc->primary_destination);
396 1.1 rjs
397 1.1 rjs /* calculate the RTO */
398 1.1 rjs net->RTO = sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered);
399 1.1 rjs
400 1.1 rjs return (0);
401 1.1 rjs }
402 1.1 rjs
403 1.1 rjs static void
404 1.1 rjs sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
405 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
406 1.1 rjs {
407 1.1 rjs struct sockaddr_storage store;
408 1.1 rjs struct sockaddr_in *sin;
409 1.1 rjs struct sockaddr_in6 *sin6;
410 1.1 rjs struct sctp_nets *r_net;
411 1.1 rjs struct timeval tv;
412 1.1 rjs
413 1.1 rjs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
414 1.1 rjs /* Invalid length */
415 1.1 rjs return;
416 1.1 rjs }
417 1.1 rjs
418 1.1 rjs sin = (struct sockaddr_in *)&store;
419 1.1 rjs sin6 = (struct sockaddr_in6 *)&store;
420 1.1 rjs
421 1.1 rjs memset(&store, 0, sizeof(store));
422 1.1 rjs if (cp->heartbeat.hb_info.addr_family == AF_INET &&
423 1.1 rjs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
424 1.1 rjs sin->sin_family = cp->heartbeat.hb_info.addr_family;
425 1.1 rjs sin->sin_len = cp->heartbeat.hb_info.addr_len;
426 1.1 rjs sin->sin_port = stcb->rport;
427 1.1 rjs memcpy(&sin->sin_addr, cp->heartbeat.hb_info.address,
428 1.1 rjs sizeof(sin->sin_addr));
429 1.1 rjs } else if (cp->heartbeat.hb_info.addr_family == AF_INET6 &&
430 1.1 rjs cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
431 1.1 rjs sin6->sin6_family = cp->heartbeat.hb_info.addr_family;
432 1.1 rjs sin6->sin6_len = cp->heartbeat.hb_info.addr_len;
433 1.1 rjs sin6->sin6_port = stcb->rport;
434 1.1 rjs memcpy(&sin6->sin6_addr, cp->heartbeat.hb_info.address,
435 1.1 rjs sizeof(sin6->sin6_addr));
436 1.1 rjs } else {
437 1.1 rjs #ifdef SCTP_DEBUG
438 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
439 1.1 rjs printf("unsupported address family");
440 1.1 rjs }
441 1.1 rjs #endif
442 1.1 rjs return;
443 1.1 rjs }
444 1.1 rjs r_net = sctp_findnet(stcb, (struct sockaddr *)sin);
445 1.1 rjs if (r_net == NULL) {
446 1.1 rjs #ifdef SCTP_DEBUG
447 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
448 1.1 rjs printf("Huh? I can't find the address I sent it to, discard\n");
449 1.1 rjs }
450 1.1 rjs #endif
451 1.1 rjs return;
452 1.1 rjs }
453 1.1 rjs if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
454 1.1 rjs (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
455 1.1 rjs (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
456 1.1 rjs /*
457 1.1 rjs * If the its a HB and it's random value is correct when
458 1.1 rjs * can confirm the destination.
459 1.1 rjs */
460 1.1 rjs r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
461 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
462 1.1 rjs stcb, 0, (void *)r_net);
463 1.1 rjs }
464 1.1 rjs r_net->error_count = 0;
465 1.1 rjs r_net->hb_responded = 1;
466 1.1 rjs tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
467 1.1 rjs tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
468 1.1 rjs if (r_net->dest_state & SCTP_ADDR_NOT_REACHABLE) {
469 1.1 rjs r_net->dest_state = SCTP_ADDR_REACHABLE;
470 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
471 1.1 rjs SCTP_HEARTBEAT_SUCCESS, (void *)r_net);
472 1.1 rjs
473 1.1 rjs /* now was it the primary? if so restore */
474 1.1 rjs if (r_net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
475 1.1 rjs sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, r_net);
476 1.1 rjs }
477 1.1 rjs }
478 1.1 rjs /* Now lets do a RTO with this */
479 1.1 rjs r_net->RTO = sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv);
480 1.1 rjs }
481 1.1 rjs
482 1.1 rjs static void
483 1.1 rjs sctp_handle_abort(struct sctp_abort_chunk *cp,
484 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
485 1.1 rjs {
486 1.1 rjs
487 1.1 rjs #ifdef SCTP_DEBUG
488 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
489 1.1 rjs printf("sctp_handle_abort: handling ABORT\n");
490 1.1 rjs }
491 1.1 rjs #endif
492 1.1 rjs if (stcb == NULL)
493 1.1 rjs return;
494 1.1 rjs /* verify that the destination addr is in the association */
495 1.1 rjs /* ignore abort for addresses being deleted */
496 1.1 rjs
497 1.1 rjs /* stop any receive timers */
498 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net);
499 1.1 rjs /* notify user of the abort and clean up... */
500 1.1 rjs sctp_abort_notification(stcb, 0);
501 1.1 rjs /* free the tcb */
502 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
503 1.1 rjs #ifdef SCTP_DEBUG
504 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
505 1.1 rjs printf("sctp_handle_abort: finished\n");
506 1.1 rjs }
507 1.1 rjs #endif
508 1.1 rjs }
509 1.1 rjs
510 1.1 rjs static void
511 1.1 rjs sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
512 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
513 1.1 rjs {
514 1.1 rjs struct sctp_association *asoc;
515 1.1 rjs int some_on_streamwheel;
516 1.1 rjs
517 1.1 rjs #ifdef SCTP_DEBUG
518 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
519 1.1 rjs printf("sctp_handle_shutdown: handling SHUTDOWN\n");
520 1.1 rjs }
521 1.1 rjs #endif
522 1.1 rjs if (stcb == NULL)
523 1.1 rjs return;
524 1.1 rjs
525 1.1 rjs if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_WAIT) ||
526 1.1 rjs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
527 1.1 rjs return;
528 1.1 rjs }
529 1.1 rjs
530 1.1 rjs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
531 1.1 rjs /* update current data status */
532 1.1 rjs #ifdef SCTP_DEBUG
533 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
534 1.1 rjs printf("Warning Shutdown NOT the expected size.. skipping (%d:%d)\n",
535 1.1 rjs ntohs(cp->ch.chunk_length),
536 1.1 rjs (int)sizeof(struct sctp_shutdown_chunk));
537 1.1 rjs }
538 1.1 rjs #endif
539 1.1 rjs return;
540 1.1 rjs } else {
541 1.1 rjs sctp_update_acked(stcb, cp, net, abort_flag);
542 1.1 rjs }
543 1.1 rjs asoc = &stcb->asoc;
544 1.1 rjs /* goto SHUTDOWN_RECEIVED state to block new requests */
545 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
546 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT)) {
547 1.1 rjs asoc->state = SCTP_STATE_SHUTDOWN_RECEIVED;
548 1.1 rjs #ifdef SCTP_DEBUG
549 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
550 1.1 rjs printf("Moving to SHUTDOWN-RECEIVED state\n");
551 1.1 rjs }
552 1.1 rjs #endif
553 1.1 rjs /* notify upper layer that peer has initiated a shutdown */
554 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL);
555 1.1 rjs
556 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
557 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
558 1.1 rjs
559 1.1 rjs /* Set the flag so we cannot send more, we
560 1.1 rjs * would call the function but we don't want to
561 1.1 rjs * wake up the ulp necessarily.
562 1.1 rjs */
563 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
564 1.1 rjs stcb->sctp_ep->sctp_socket->so_rcv.sb_state |= SBS_CANTSENDMORE;
565 1.1 rjs #else
566 1.1 rjs stcb->sctp_ep->sctp_socket->so_state |= SS_CANTSENDMORE;
567 1.1 rjs #endif
568 1.1 rjs }
569 1.1 rjs /* reset time */
570 1.1 rjs SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
571 1.1 rjs }
572 1.1 rjs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
573 1.1 rjs /*
574 1.1 rjs * stop the shutdown timer, since we WILL move
575 1.1 rjs * to SHUTDOWN-ACK-SENT.
576 1.1 rjs */
577 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
578 1.1 rjs }
579 1.1 rjs /* Now are we there yet? */
580 1.1 rjs some_on_streamwheel = 0;
581 1.1 rjs if (!TAILQ_EMPTY(&asoc->out_wheel)) {
582 1.1 rjs /* Check to see if some data queued */
583 1.1 rjs struct sctp_stream_out *outs;
584 1.1 rjs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
585 1.1 rjs if (!TAILQ_EMPTY(&outs->outqueue)) {
586 1.1 rjs some_on_streamwheel = 1;
587 1.1 rjs break;
588 1.1 rjs }
589 1.1 rjs }
590 1.1 rjs }
591 1.1 rjs #ifdef SCTP_DEBUG
592 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
593 1.1 rjs printf("some_on_streamwheel:%d send_q_empty:%d sent_q_empty:%d\n",
594 1.1 rjs some_on_streamwheel,
595 1.1 rjs !TAILQ_EMPTY(&asoc->send_queue),
596 1.1 rjs !TAILQ_EMPTY(&asoc->sent_queue));
597 1.1 rjs }
598 1.1 rjs #endif
599 1.1 rjs if (!TAILQ_EMPTY(&asoc->send_queue) ||
600 1.1 rjs !TAILQ_EMPTY(&asoc->sent_queue) ||
601 1.1 rjs some_on_streamwheel) {
602 1.1 rjs /* By returning we will push more data out */
603 1.1 rjs return;
604 1.1 rjs } else {
605 1.1 rjs /* no outstanding data to send, so move on... */
606 1.1 rjs /* send SHUTDOWN-ACK */
607 1.1 rjs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
608 1.1 rjs /* move to SHUTDOWN-ACK-SENT state */
609 1.1 rjs asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
610 1.1 rjs #ifdef SCTP_DEBUG
611 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
612 1.1 rjs printf("moving to SHUTDOWN_ACK state\n");
613 1.1 rjs }
614 1.1 rjs #endif
615 1.1 rjs /* start SHUTDOWN timer */
616 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep,
617 1.1 rjs stcb, net);
618 1.1 rjs }
619 1.1 rjs }
620 1.1 rjs
621 1.1 rjs static void
622 1.1 rjs sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp,
623 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
624 1.1 rjs {
625 1.1 rjs struct sctp_association *asoc;
626 1.1 rjs
627 1.1 rjs #ifdef SCTP_DEBUG
628 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
629 1.1 rjs printf("sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
630 1.1 rjs }
631 1.1 rjs #endif
632 1.1 rjs if (stcb == NULL)
633 1.1 rjs return;
634 1.1 rjs
635 1.1 rjs asoc = &stcb->asoc;
636 1.1 rjs /* process according to association state */
637 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
638 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
639 1.1 rjs /* unexpected SHUTDOWN-ACK... so ignore... */
640 1.1 rjs return;
641 1.1 rjs }
642 1.1 rjs /* are the queues empty? */
643 1.1 rjs if (!TAILQ_EMPTY(&asoc->send_queue) ||
644 1.1 rjs !TAILQ_EMPTY(&asoc->sent_queue) ||
645 1.1 rjs !TAILQ_EMPTY(&asoc->out_wheel)) {
646 1.1 rjs sctp_report_all_outbound(stcb);
647 1.1 rjs }
648 1.1 rjs /* stop the timer */
649 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
650 1.1 rjs /* send SHUTDOWN-COMPLETE */
651 1.1 rjs sctp_send_shutdown_complete(stcb, net);
652 1.1 rjs /* notify upper layer protocol */
653 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
654 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
655 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
656 1.1 rjs stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
657 1.1 rjs /* Set the connected flag to disconnected */
658 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
659 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
660 1.1 rjs soisdisconnected(stcb->sctp_ep->sctp_socket);
661 1.1 rjs }
662 1.1 rjs /* free the TCB but first save off the ep */
663 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
664 1.1 rjs }
665 1.1 rjs
666 1.1 rjs /*
667 1.1 rjs * Skip past the param header and then we will find the chunk that
668 1.1 rjs * caused the problem. There are two possiblities ASCONF or FWD-TSN
669 1.1 rjs * other than that and our peer must be broken.
670 1.1 rjs */
671 1.1 rjs static void
672 1.1 rjs sctp_process_unrecog_chunk(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr,
673 1.1 rjs struct sctp_nets *net)
674 1.1 rjs {
675 1.1 rjs struct sctp_chunkhdr *chk;
676 1.1 rjs
677 1.1 rjs chk = (struct sctp_chunkhdr *)((vaddr_t)phdr + sizeof(*phdr));
678 1.1 rjs switch (chk->chunk_type) {
679 1.1 rjs case SCTP_ASCONF_ACK:
680 1.1 rjs #ifdef SCTP_DEBUG
681 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
682 1.1 rjs printf("Strange peer, snds ASCONF but does not recongnize asconf-ack?\n");
683 1.1 rjs }
684 1.1 rjs #endif
685 1.1 rjs case SCTP_ASCONF:
686 1.1 rjs #ifdef SCTP_DEBUG
687 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
688 1.1 rjs printf("Peer does not support ASCONF/ASCONF-ACK chunks\n");
689 1.1 rjs }
690 1.1 rjs #endif /* SCTP_DEBUG */
691 1.1 rjs sctp_asconf_cleanup(stcb, net);
692 1.1 rjs break;
693 1.1 rjs case SCTP_FORWARD_CUM_TSN:
694 1.1 rjs stcb->asoc.peer_supports_prsctp = 0;
695 1.1 rjs break;
696 1.1 rjs default:
697 1.1 rjs #ifdef SCTP_DEBUG
698 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
699 1.1 rjs printf("Peer does not support chunk type %d(%x)??\n",
700 1.1 rjs chk->chunk_type, (u_int)chk->chunk_type);
701 1.1 rjs }
702 1.1 rjs #endif
703 1.1 rjs break;
704 1.1 rjs }
705 1.1 rjs }
706 1.1 rjs
707 1.1 rjs /*
708 1.1 rjs * Skip past the param header and then we will find the param that
709 1.1 rjs * caused the problem. There are a number of param's in a ASCONF
710 1.1 rjs * OR the prsctp param these will turn of specific features.
711 1.1 rjs */
712 1.1 rjs static void
713 1.1 rjs sctp_process_unrecog_param(struct sctp_tcb *stcb, struct sctp_paramhdr *phdr)
714 1.1 rjs {
715 1.1 rjs struct sctp_paramhdr *pbad;
716 1.1 rjs
717 1.1 rjs pbad = phdr + 1;
718 1.1 rjs switch (ntohs(pbad->param_type)) {
719 1.1 rjs /* pr-sctp draft */
720 1.1 rjs case SCTP_PRSCTP_SUPPORTED:
721 1.1 rjs stcb->asoc.peer_supports_prsctp = 0;
722 1.1 rjs break;
723 1.1 rjs case SCTP_SUPPORTED_CHUNK_EXT:
724 1.1 rjs break;
725 1.1 rjs /* draft-ietf-tsvwg-addip-sctp */
726 1.1 rjs case SCTP_ECN_NONCE_SUPPORTED:
727 1.1 rjs stcb->asoc.peer_supports_ecn_nonce = 0;
728 1.1 rjs stcb->asoc.ecn_nonce_allowed = 0;
729 1.1 rjs stcb->asoc.ecn_allowed = 0;
730 1.1 rjs break;
731 1.1 rjs case SCTP_ADD_IP_ADDRESS:
732 1.1 rjs case SCTP_DEL_IP_ADDRESS:
733 1.1 rjs stcb->asoc.peer_supports_asconf = 0;
734 1.1 rjs break;
735 1.1 rjs case SCTP_SET_PRIM_ADDR:
736 1.1 rjs stcb->asoc.peer_supports_asconf_setprim = 0;
737 1.1 rjs break;
738 1.1 rjs case SCTP_SUCCESS_REPORT:
739 1.1 rjs case SCTP_ERROR_CAUSE_IND:
740 1.1 rjs #ifdef SCTP_DEBUG
741 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
742 1.1 rjs printf("Huh, the peer does not support success? or error cause?\n");
743 1.1 rjs printf("Turning off ASCONF to this strange peer\n");
744 1.1 rjs }
745 1.1 rjs #endif
746 1.1 rjs stcb->asoc.peer_supports_asconf = 0;
747 1.1 rjs stcb->asoc.peer_supports_asconf_setprim = 0;
748 1.1 rjs break;
749 1.1 rjs default:
750 1.1 rjs #ifdef SCTP_DEBUG
751 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
752 1.1 rjs printf("Peer does not support base param type %d(%x)??\n",
753 1.1 rjs pbad->param_type, (u_int)pbad->param_type);
754 1.1 rjs }
755 1.1 rjs #endif
756 1.1 rjs break;
757 1.1 rjs }
758 1.1 rjs }
759 1.1 rjs
760 1.1 rjs static int
761 1.1 rjs sctp_handle_error(struct sctp_chunkhdr *ch,
762 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
763 1.1 rjs {
764 1.1 rjs int chklen;
765 1.1 rjs struct sctp_paramhdr *phdr;
766 1.1 rjs uint16_t error_type;
767 1.1 rjs uint16_t error_len;
768 1.1 rjs struct sctp_association *asoc;
769 1.1 rjs
770 1.1 rjs int adjust;
771 1.1 rjs /* parse through all of the errors and process */
772 1.1 rjs asoc = &stcb->asoc;
773 1.1 rjs phdr = (struct sctp_paramhdr *)((vaddr_t)ch +
774 1.1 rjs sizeof(struct sctp_chunkhdr));
775 1.1 rjs chklen = ntohs(ch->chunk_length) - sizeof(struct sctp_chunkhdr);
776 1.1 rjs while ((size_t)chklen >= sizeof(struct sctp_paramhdr)) {
777 1.1 rjs /* Process an Error Cause */
778 1.1 rjs error_type = ntohs(phdr->param_type);
779 1.1 rjs error_len = ntohs(phdr->param_length);
780 1.1 rjs if ((error_len > chklen) || (error_len == 0)) {
781 1.1 rjs /* invalid param length for this param */
782 1.1 rjs #ifdef SCTP_DEBUG
783 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
784 1.1 rjs printf("Bogus length in error param- chunk left:%d errorlen:%d\n",
785 1.1 rjs chklen, error_len);
786 1.1 rjs }
787 1.1 rjs #endif /* SCTP_DEBUG */
788 1.1 rjs return (0);
789 1.1 rjs }
790 1.1 rjs switch (error_type) {
791 1.1 rjs case SCTP_CAUSE_INV_STRM:
792 1.1 rjs case SCTP_CAUSE_MISS_PARAM:
793 1.1 rjs case SCTP_CAUSE_INVALID_PARAM:
794 1.1 rjs case SCTP_CAUSE_NOUSER_DATA:
795 1.1 rjs #ifdef SCTP_DEBUG
796 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
797 1.1 rjs printf("Software error we got a %d back? We have a bug :/ (or do they?)\n",
798 1.1 rjs error_type);
799 1.1 rjs }
800 1.1 rjs #endif
801 1.1 rjs break;
802 1.1 rjs case SCTP_CAUSE_STALE_COOKIE:
803 1.1 rjs /* We only act if we have echoed a cookie and are waiting. */
804 1.1 rjs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
805 1.1 rjs int *p;
806 1.1 rjs p = (int *)((vaddr_t)phdr + sizeof(*phdr));
807 1.1 rjs /* Save the time doubled */
808 1.1 rjs asoc->cookie_preserve_req = ntohl(*p) << 1;
809 1.1 rjs asoc->stale_cookie_count++;
810 1.1 rjs if (asoc->stale_cookie_count >
811 1.1 rjs asoc->max_init_times) {
812 1.1 rjs sctp_abort_notification(stcb, 0);
813 1.1 rjs /* now free the asoc */
814 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
815 1.1 rjs return (-1);
816 1.1 rjs }
817 1.1 rjs /* blast back to INIT state */
818 1.1 rjs asoc->state &= ~SCTP_STATE_COOKIE_ECHOED;
819 1.1 rjs asoc->state |= SCTP_STATE_COOKIE_WAIT;
820 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
821 1.1 rjs stcb->sctp_ep, stcb, net);
822 1.1 rjs sctp_send_initiate(stcb->sctp_ep, stcb);
823 1.1 rjs }
824 1.1 rjs break;
825 1.1 rjs case SCTP_CAUSE_UNRESOLV_ADDR:
826 1.1 rjs /*
827 1.1 rjs * Nothing we can do here, we don't do hostname
828 1.1 rjs * addresses so if the peer does not like my IPv6 (or
829 1.1 rjs * IPv4 for that matter) it does not matter. If they
830 1.1 rjs * don't support that type of address, they can NOT
831 1.1 rjs * possibly get that packet type... i.e. with no IPv6
832 1.1 rjs * you can't recieve a IPv6 packet. so we can safely
833 1.1 rjs * ignore this one. If we ever added support for
834 1.1 rjs * HOSTNAME Addresses, then we would need to do
835 1.1 rjs * something here.
836 1.1 rjs */
837 1.1 rjs break;
838 1.1 rjs case SCTP_CAUSE_UNRECOG_CHUNK:
839 1.1 rjs sctp_process_unrecog_chunk(stcb, phdr, net);
840 1.1 rjs break;
841 1.1 rjs case SCTP_CAUSE_UNRECOG_PARAM:
842 1.1 rjs sctp_process_unrecog_param(stcb, phdr);
843 1.1 rjs break;
844 1.1 rjs case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
845 1.1 rjs /*
846 1.1 rjs * We ignore this since the timer will drive out a new
847 1.1 rjs * cookie anyway and there timer will drive us to send
848 1.1 rjs * a SHUTDOWN_COMPLETE. We can't send one here since
849 1.1 rjs * we don't have their tag.
850 1.1 rjs */
851 1.1 rjs break;
852 1.1 rjs case SCTP_CAUSE_DELETEING_LAST_ADDR:
853 1.1 rjs case SCTP_CAUSE_OPERATION_REFUSED:
854 1.1 rjs case SCTP_CAUSE_DELETING_SRC_ADDR:
855 1.1 rjs /* We should NOT get these here, but in a ASCONF-ACK. */
856 1.1 rjs #ifdef SCTP_DEBUG
857 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
858 1.1 rjs printf("Peer sends ASCONF errors in a Operational Error?<%d>?\n",
859 1.1 rjs error_type);
860 1.1 rjs }
861 1.1 rjs #endif
862 1.1 rjs break;
863 1.1 rjs case SCTP_CAUSE_OUT_OF_RESC:
864 1.1 rjs /*
865 1.1 rjs * And what, pray tell do we do with the fact
866 1.1 rjs * that the peer is out of resources? Not
867 1.1 rjs * really sure we could do anything but abort.
868 1.1 rjs * I suspect this should have came WITH an
869 1.1 rjs * abort instead of in a OP-ERROR.
870 1.1 rjs */
871 1.1 rjs break;
872 1.1 rjs default:
873 1.1 rjs #ifdef SCTP_DEBUG
874 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
875 1.1 rjs /* don't know what this error cause is... */
876 1.1 rjs printf("sctp_handle_error: unknown error type = 0x%xh\n",
877 1.1 rjs error_type);
878 1.1 rjs }
879 1.1 rjs #endif /* SCTP_DEBUG */
880 1.1 rjs break;
881 1.1 rjs }
882 1.1 rjs adjust = SCTP_SIZE32(error_len);
883 1.1 rjs chklen -= adjust;
884 1.1 rjs phdr = (struct sctp_paramhdr *)((vaddr_t)phdr + adjust);
885 1.1 rjs }
886 1.1 rjs return (0);
887 1.1 rjs }
888 1.1 rjs
889 1.1 rjs static int
890 1.1 rjs sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset, struct sctphdr *sh,
891 1.1 rjs struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
892 1.1 rjs struct sctp_nets *net)
893 1.1 rjs {
894 1.1 rjs struct sctp_init_ack *init_ack;
895 1.1 rjs int *state;
896 1.1 rjs struct mbuf *op_err;
897 1.1 rjs
898 1.1 rjs #ifdef SCTP_DEBUG
899 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
900 1.1 rjs printf("sctp_handle_init_ack: handling INIT-ACK\n");
901 1.1 rjs }
902 1.1 rjs #endif
903 1.1 rjs if (stcb == NULL) {
904 1.1 rjs #ifdef SCTP_DEBUG
905 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
906 1.1 rjs printf("sctp_handle_init_ack: TCB is null\n");
907 1.1 rjs }
908 1.1 rjs #endif
909 1.1 rjs return (-1);
910 1.1 rjs }
911 1.1 rjs if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
912 1.1 rjs /* Invalid length */
913 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
914 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
915 1.1 rjs op_err);
916 1.1 rjs return (-1);
917 1.1 rjs }
918 1.1 rjs init_ack = &cp->init;
919 1.1 rjs /* validate parameters */
920 1.1 rjs if (init_ack->initiate_tag == 0) {
921 1.1 rjs /* protocol error... send an abort */
922 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
923 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
924 1.1 rjs op_err);
925 1.1 rjs return (-1);
926 1.1 rjs }
927 1.1 rjs if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
928 1.1 rjs /* protocol error... send an abort */
929 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
930 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
931 1.1 rjs op_err);
932 1.1 rjs return (-1);
933 1.1 rjs }
934 1.1 rjs if (init_ack->num_inbound_streams == 0) {
935 1.1 rjs /* protocol error... send an abort */
936 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
937 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
938 1.1 rjs op_err);
939 1.1 rjs return (-1);
940 1.1 rjs }
941 1.1 rjs if (init_ack->num_outbound_streams == 0) {
942 1.1 rjs /* protocol error... send an abort */
943 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_INVALID_PARAM);
944 1.1 rjs sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen, sh,
945 1.1 rjs op_err);
946 1.1 rjs return (-1);
947 1.1 rjs }
948 1.1 rjs
949 1.1 rjs /* process according to association state... */
950 1.1 rjs state = &stcb->asoc.state;
951 1.1 rjs switch (*state & SCTP_STATE_MASK) {
952 1.1 rjs case SCTP_STATE_COOKIE_WAIT:
953 1.1 rjs /* this is the expected state for this chunk */
954 1.1 rjs /* process the INIT-ACK parameters */
955 1.1 rjs if (stcb->asoc.primary_destination->dest_state &
956 1.1 rjs SCTP_ADDR_UNCONFIRMED) {
957 1.1 rjs /*
958 1.1 rjs * The primary is where we sent the INIT, we can
959 1.1 rjs * always consider it confirmed when the INIT-ACK
960 1.1 rjs * is returned. Do this before we load addresses
961 1.1 rjs * though.
962 1.1 rjs */
963 1.1 rjs stcb->asoc.primary_destination->dest_state &=
964 1.1 rjs ~SCTP_ADDR_UNCONFIRMED;
965 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
966 1.1 rjs stcb, 0, (void *)stcb->asoc.primary_destination);
967 1.1 rjs }
968 1.1 rjs if (sctp_process_init_ack(m, iphlen, offset, sh, cp, stcb, net
969 1.1 rjs ) < 0) {
970 1.1 rjs /* error in parsing parameters */
971 1.1 rjs #ifdef SCTP_DEBUG
972 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
973 1.1 rjs printf("sctp_process_init_ack: error in msg, discarding\n");
974 1.1 rjs }
975 1.1 rjs #endif
976 1.1 rjs return (-1);
977 1.1 rjs }
978 1.1 rjs /* update our state */
979 1.1 rjs #ifdef SCTP_DEBUG
980 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
981 1.1 rjs printf("moving to COOKIE-ECHOED state\n");
982 1.1 rjs }
983 1.1 rjs #endif
984 1.1 rjs if (*state & SCTP_STATE_SHUTDOWN_PENDING) {
985 1.1 rjs *state = SCTP_STATE_COOKIE_ECHOED |
986 1.1 rjs SCTP_STATE_SHUTDOWN_PENDING;
987 1.1 rjs } else {
988 1.1 rjs *state = SCTP_STATE_COOKIE_ECHOED;
989 1.1 rjs }
990 1.1 rjs
991 1.1 rjs /* reset the RTO calc */
992 1.1 rjs stcb->asoc.overall_error_count = 0;
993 1.1 rjs SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
994 1.1 rjs /*
995 1.1 rjs * collapse the init timer back in case of a exponential backoff
996 1.1 rjs */
997 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
998 1.1 rjs stcb, net);
999 1.1 rjs /*
1000 1.1 rjs * the send at the end of the inbound data processing will
1001 1.1 rjs * cause the cookie to be sent
1002 1.1 rjs */
1003 1.1 rjs break;
1004 1.1 rjs case SCTP_STATE_SHUTDOWN_SENT:
1005 1.1 rjs /* incorrect state... discard */
1006 1.1 rjs break;
1007 1.1 rjs case SCTP_STATE_COOKIE_ECHOED:
1008 1.1 rjs /* incorrect state... discard */
1009 1.1 rjs break;
1010 1.1 rjs case SCTP_STATE_OPEN:
1011 1.1 rjs /* incorrect state... discard */
1012 1.1 rjs break;
1013 1.1 rjs case SCTP_STATE_EMPTY:
1014 1.1 rjs case SCTP_STATE_INUSE:
1015 1.1 rjs default:
1016 1.1 rjs /* incorrect state... discard */
1017 1.1 rjs #ifdef SCTP_DEBUG
1018 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1019 1.1 rjs printf("Leaving handle-init-ack default\n");
1020 1.1 rjs }
1021 1.1 rjs #endif
1022 1.1 rjs return (-1);
1023 1.1 rjs break;
1024 1.1 rjs } /* end switch asoc state */
1025 1.1 rjs #ifdef SCTP_DEBUG
1026 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1027 1.1 rjs printf("Leaving handle-init-ack end\n");
1028 1.1 rjs }
1029 1.1 rjs #endif
1030 1.1 rjs return (0);
1031 1.1 rjs }
1032 1.1 rjs
1033 1.1 rjs
1034 1.1 rjs /*
1035 1.1 rjs * handle a state cookie for an existing association
1036 1.1 rjs * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1037 1.1 rjs * note: this is a "split" mbuf and the cookie signature does not exist
1038 1.1 rjs * offset: offset into mbuf to the cookie-echo chunk
1039 1.1 rjs */
1040 1.1 rjs static struct sctp_tcb *
1041 1.1 rjs sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1042 1.1 rjs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1043 1.1 rjs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
1044 1.1 rjs struct sockaddr *init_src, int *notification)
1045 1.1 rjs {
1046 1.1 rjs struct sctp_association *asoc;
1047 1.1 rjs struct sctp_init_chunk *init_cp, init_buf;
1048 1.1 rjs struct sctp_init_ack_chunk *initack_cp, initack_buf;
1049 1.1 rjs int chk_length;
1050 1.1 rjs int init_offset, initack_offset;
1051 1.1 rjs int retval;
1052 1.1 rjs
1053 1.1 rjs /* I know that the TCB is non-NULL from the caller */
1054 1.1 rjs asoc = &stcb->asoc;
1055 1.1 rjs
1056 1.1 rjs if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1057 1.1 rjs /* SHUTDOWN came in after sending INIT-ACK */
1058 1.1 rjs struct mbuf *op_err;
1059 1.1 rjs struct sctp_paramhdr *ph;
1060 1.1 rjs
1061 1.1 rjs sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1062 1.1 rjs #ifdef SCTP_DEBUG
1063 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1064 1.1 rjs printf("sctp_handle_cookie: got a cookie, while shutting down!\n");
1065 1.1 rjs }
1066 1.1 rjs #endif
1067 1.1 rjs MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
1068 1.1 rjs if (op_err == NULL) {
1069 1.1 rjs /* FOOBAR */
1070 1.1 rjs return (NULL);
1071 1.1 rjs }
1072 1.1 rjs /* pre-reserve some space */
1073 1.1 rjs op_err->m_data += sizeof(struct ip6_hdr);
1074 1.1 rjs op_err->m_data += sizeof(struct sctphdr);
1075 1.1 rjs op_err->m_data += sizeof(struct sctp_chunkhdr);
1076 1.1 rjs /* Set the len */
1077 1.1 rjs op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_paramhdr);
1078 1.1 rjs ph = mtod(op_err, struct sctp_paramhdr *);
1079 1.1 rjs ph->param_type = htons(SCTP_CAUSE_COOKIE_IN_SHUTDOWN);
1080 1.1 rjs ph->param_length = htons(sizeof(struct sctp_paramhdr));
1081 1.1 rjs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1082 1.1 rjs return (NULL);
1083 1.1 rjs }
1084 1.1 rjs /*
1085 1.1 rjs * find and validate the INIT chunk in the cookie (peer's info)
1086 1.1 rjs * the INIT should start after the cookie-echo header struct
1087 1.1 rjs * (chunk header, state cookie header struct)
1088 1.1 rjs */
1089 1.1 rjs init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1090 1.1 rjs
1091 1.1 rjs init_cp = (struct sctp_init_chunk *)
1092 1.1 rjs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1093 1.1 rjs (u_int8_t *)&init_buf);
1094 1.1 rjs if (init_cp == NULL) {
1095 1.1 rjs /* could not pull a INIT chunk in cookie */
1096 1.1 rjs #ifdef SCTP_DEBUG
1097 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1098 1.1 rjs printf("process_cookie_existing: could not pull INIT chunk hdr\n");
1099 1.1 rjs }
1100 1.1 rjs #endif /* SCTP_DEBUG */
1101 1.1 rjs return (NULL);
1102 1.1 rjs }
1103 1.1 rjs chk_length = ntohs(init_cp->ch.chunk_length);
1104 1.1 rjs if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1105 1.1 rjs #ifdef SCTP_DEBUG
1106 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1107 1.1 rjs printf("process_cookie_existing: could not find INIT chunk!\n");
1108 1.1 rjs }
1109 1.1 rjs #endif /* SCTP_DEBUG */
1110 1.1 rjs return (NULL);
1111 1.1 rjs }
1112 1.1 rjs
1113 1.1 rjs /*
1114 1.1 rjs * find and validate the INIT-ACK chunk in the cookie (my info)
1115 1.1 rjs * the INIT-ACK follows the INIT chunk
1116 1.1 rjs */
1117 1.1 rjs initack_offset = init_offset + SCTP_SIZE32(chk_length);
1118 1.1 rjs initack_cp = (struct sctp_init_ack_chunk *)
1119 1.1 rjs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1120 1.1 rjs (u_int8_t *)&initack_buf);
1121 1.1 rjs if (initack_cp == NULL) {
1122 1.1 rjs /* could not pull INIT-ACK chunk in cookie */
1123 1.1 rjs #ifdef SCTP_DEBUG
1124 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1125 1.1 rjs printf("process_cookie_existing: could not pull INIT-ACK chunk hdr\n");
1126 1.1 rjs }
1127 1.1 rjs #endif /* SCTP_DEBUG */
1128 1.1 rjs return (NULL);
1129 1.1 rjs }
1130 1.1 rjs chk_length = ntohs(initack_cp->ch.chunk_length);
1131 1.1 rjs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1132 1.1 rjs #ifdef SCTP_DEBUG
1133 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1134 1.1 rjs printf("process_cookie_existing: could not find INIT-ACK chunk!\n");
1135 1.1 rjs }
1136 1.1 rjs #endif /* SCTP_DEBUG */
1137 1.1 rjs return (NULL);
1138 1.1 rjs }
1139 1.1 rjs if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1140 1.1 rjs (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1141 1.1 rjs /*
1142 1.1 rjs * case D in Section 5.2.4 Table 2: MMAA
1143 1.1 rjs * process accordingly to get into the OPEN state
1144 1.1 rjs */
1145 1.1 rjs switch SCTP_GET_STATE(asoc) {
1146 1.1 rjs case SCTP_STATE_COOKIE_WAIT:
1147 1.1 rjs /*
1148 1.1 rjs * INIT was sent, but got got a COOKIE_ECHO with
1149 1.1 rjs * the correct tags... just accept it...
1150 1.1 rjs */
1151 1.1 rjs /* First we must process the INIT !! */
1152 1.1 rjs retval = sctp_process_init(init_cp, stcb, net);
1153 1.1 rjs if (retval < 0) {
1154 1.1 rjs #ifdef SCTP_DEBUG
1155 1.1 rjs printf("process_cookie_existing: INIT processing failed\n");
1156 1.1 rjs #endif
1157 1.1 rjs return (NULL);
1158 1.1 rjs }
1159 1.1 rjs /* intentional fall through to below... */
1160 1.1 rjs
1161 1.1 rjs case SCTP_STATE_COOKIE_ECHOED:
1162 1.1 rjs /* Duplicate INIT case */
1163 1.1 rjs /* we have already processed the INIT so no problem */
1164 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1165 1.1 rjs net);
1166 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1167 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb,
1168 1.1 rjs net);
1169 1.1 rjs /* update current state */
1170 1.1 rjs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1171 1.1 rjs asoc->state = SCTP_STATE_OPEN |
1172 1.1 rjs SCTP_STATE_SHUTDOWN_PENDING;
1173 1.1 rjs } else if ((asoc->state & SCTP_STATE_SHUTDOWN_SENT) == 0) {
1174 1.1 rjs /* if ok, move to OPEN state */
1175 1.1 rjs asoc->state = SCTP_STATE_OPEN;
1176 1.1 rjs }
1177 1.1 rjs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1178 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1179 1.1 rjs (!(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING))) {
1180 1.1 rjs /*
1181 1.1 rjs * Here is where collision would go if we did a
1182 1.1 rjs * connect() and instead got a
1183 1.1 rjs * init/init-ack/cookie done before the
1184 1.1 rjs * init-ack came back..
1185 1.1 rjs */
1186 1.1 rjs stcb->sctp_ep->sctp_flags |=
1187 1.1 rjs SCTP_PCB_FLAGS_CONNECTED;
1188 1.1 rjs soisconnected(stcb->sctp_ep->sctp_socket);
1189 1.1 rjs }
1190 1.1 rjs /* notify upper layer */
1191 1.1 rjs *notification = SCTP_NOTIFY_ASSOC_UP;
1192 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb,
1193 1.1 rjs net);
1194 1.1 rjs /*
1195 1.1 rjs * since we did not send a HB make sure we don't double
1196 1.1 rjs * things
1197 1.1 rjs */
1198 1.1 rjs net->hb_responded = 1;
1199 1.1 rjs
1200 1.1 rjs if (stcb->asoc.sctp_autoclose_ticks &&
1201 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1202 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1203 1.1 rjs inp, stcb, NULL);
1204 1.1 rjs }
1205 1.1 rjs break;
1206 1.1 rjs default:
1207 1.1 rjs /*
1208 1.1 rjs * we're in the OPEN state (or beyond), so peer
1209 1.1 rjs * must have simply lost the COOKIE-ACK
1210 1.1 rjs */
1211 1.1 rjs break;
1212 1.1 rjs } /* end switch */
1213 1.1 rjs
1214 1.1 rjs /*
1215 1.1 rjs * We ignore the return code here.. not sure if we should
1216 1.1 rjs * somehow abort.. but we do have an existing asoc. This
1217 1.1 rjs * really should not fail.
1218 1.1 rjs */
1219 1.1 rjs if (sctp_load_addresses_from_init(stcb, m, iphlen,
1220 1.1 rjs init_offset + sizeof(struct sctp_init_chunk),
1221 1.1 rjs initack_offset, sh, init_src)) {
1222 1.1 rjs #ifdef SCTP_DEBUG
1223 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1224 1.1 rjs printf("Weird cookie load_address failure on cookie existing - 1\n");
1225 1.1 rjs }
1226 1.1 rjs #endif
1227 1.1 rjs return (NULL);
1228 1.1 rjs }
1229 1.1 rjs
1230 1.1 rjs /* respond with a COOKIE-ACK */
1231 1.1 rjs sctp_send_cookie_ack(stcb);
1232 1.1 rjs return (stcb);
1233 1.1 rjs } /* end if */
1234 1.1 rjs if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1235 1.1 rjs ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1236 1.1 rjs cookie->tie_tag_my_vtag == 0 &&
1237 1.1 rjs cookie->tie_tag_peer_vtag == 0) {
1238 1.1 rjs /*
1239 1.1 rjs * case C in Section 5.2.4 Table 2: XMOO
1240 1.1 rjs * silently discard
1241 1.1 rjs */
1242 1.1 rjs return (NULL);
1243 1.1 rjs }
1244 1.1 rjs if (ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag &&
1245 1.1 rjs (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag ||
1246 1.1 rjs init_cp->init.initiate_tag == 0)) {
1247 1.1 rjs /*
1248 1.1 rjs * case B in Section 5.2.4 Table 2: MXAA or MOAA
1249 1.1 rjs * my info should be ok, re-accept peer info
1250 1.1 rjs */
1251 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1252 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1253 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1254 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1255 1.1 rjs /*
1256 1.1 rjs * since we did not send a HB make sure we don't double things
1257 1.1 rjs */
1258 1.1 rjs net->hb_responded = 1;
1259 1.1 rjs if (stcb->asoc.sctp_autoclose_ticks &&
1260 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1261 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1262 1.1 rjs NULL);
1263 1.1 rjs }
1264 1.1 rjs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1265 1.1 rjs asoc->pre_open_streams =
1266 1.1 rjs ntohs(initack_cp->init.num_outbound_streams);
1267 1.1 rjs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1268 1.1 rjs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1269 1.1 rjs asoc->init_seq_number;
1270 1.1 rjs asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1271 1.1 rjs asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1272 1.1 rjs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1273 1.1 rjs asoc->str_reset_seq_in = asoc->init_seq_number;
1274 1.1 rjs asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1275 1.1 rjs
1276 1.1 rjs /* process the INIT info (peer's info) */
1277 1.1 rjs retval = sctp_process_init(init_cp, stcb, net);
1278 1.1 rjs if (retval < 0) {
1279 1.1 rjs #ifdef SCTP_DEBUG
1280 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1281 1.1 rjs printf("process_cookie_existing: INIT processing failed\n");
1282 1.1 rjs }
1283 1.1 rjs #endif
1284 1.1 rjs return (NULL);
1285 1.1 rjs }
1286 1.1 rjs if (sctp_load_addresses_from_init(stcb, m, iphlen,
1287 1.1 rjs init_offset + sizeof(struct sctp_init_chunk),
1288 1.1 rjs initack_offset, sh, init_src)) {
1289 1.1 rjs #ifdef SCTP_DEBUG
1290 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1291 1.1 rjs printf("Weird cookie load_address failure on cookie existing - 2\n");
1292 1.1 rjs }
1293 1.1 rjs #endif
1294 1.1 rjs return (NULL);
1295 1.1 rjs }
1296 1.1 rjs
1297 1.1 rjs if ((asoc->state & SCTP_STATE_COOKIE_WAIT) ||
1298 1.1 rjs (asoc->state & SCTP_STATE_COOKIE_ECHOED)) {
1299 1.1 rjs *notification = SCTP_NOTIFY_ASSOC_UP;
1300 1.1 rjs
1301 1.1 rjs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1302 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1303 1.1 rjs !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1304 1.1 rjs stcb->sctp_ep->sctp_flags |=
1305 1.1 rjs SCTP_PCB_FLAGS_CONNECTED;
1306 1.1 rjs soisconnected(stcb->sctp_ep->sctp_socket);
1307 1.1 rjs }
1308 1.1 rjs }
1309 1.1 rjs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1310 1.1 rjs asoc->state = SCTP_STATE_OPEN |
1311 1.1 rjs SCTP_STATE_SHUTDOWN_PENDING;
1312 1.1 rjs } else {
1313 1.1 rjs asoc->state = SCTP_STATE_OPEN;
1314 1.1 rjs }
1315 1.1 rjs sctp_send_cookie_ack(stcb);
1316 1.1 rjs return (stcb);
1317 1.1 rjs }
1318 1.1 rjs
1319 1.1 rjs if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1320 1.1 rjs ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1321 1.1 rjs cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1322 1.1 rjs cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1323 1.1 rjs cookie->tie_tag_peer_vtag != 0) {
1324 1.1 rjs /*
1325 1.1 rjs * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1326 1.1 rjs */
1327 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
1328 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
1329 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1330 1.1 rjs
1331 1.1 rjs /* notify upper layer */
1332 1.1 rjs *notification = SCTP_NOTIFY_ASSOC_RESTART;
1333 1.1 rjs
1334 1.1 rjs /* send up all the data */
1335 1.1 rjs sctp_report_all_outbound(stcb);
1336 1.1 rjs
1337 1.1 rjs /* process the INIT-ACK info (my info) */
1338 1.1 rjs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1339 1.1 rjs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1340 1.1 rjs asoc->pre_open_streams =
1341 1.1 rjs ntohs(initack_cp->init.num_outbound_streams);
1342 1.1 rjs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1343 1.1 rjs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out =
1344 1.1 rjs asoc->init_seq_number;
1345 1.1 rjs asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1346 1.1 rjs asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1347 1.1 rjs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1348 1.1 rjs asoc->str_reset_seq_in = asoc->init_seq_number;
1349 1.1 rjs
1350 1.1 rjs asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1351 1.1 rjs if (asoc->mapping_array)
1352 1.1 rjs memset(asoc->mapping_array, 0,
1353 1.1 rjs asoc->mapping_array_size);
1354 1.1 rjs /* process the INIT info (peer's info) */
1355 1.1 rjs retval = sctp_process_init(init_cp, stcb, net);
1356 1.1 rjs if (retval < 0) {
1357 1.1 rjs #ifdef SCTP_DEBUG
1358 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1359 1.1 rjs printf("process_cookie_existing: INIT processing failed\n");
1360 1.1 rjs }
1361 1.1 rjs #endif
1362 1.1 rjs return (NULL);
1363 1.1 rjs }
1364 1.1 rjs
1365 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1366 1.1 rjs /*
1367 1.1 rjs * since we did not send a HB make sure we don't double things
1368 1.1 rjs */
1369 1.1 rjs net->hb_responded = 1;
1370 1.1 rjs
1371 1.1 rjs if (sctp_load_addresses_from_init(stcb, m, iphlen,
1372 1.1 rjs init_offset + sizeof(struct sctp_init_chunk),
1373 1.1 rjs initack_offset, sh, init_src)) {
1374 1.1 rjs #ifdef SCTP_DEBUG
1375 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1376 1.1 rjs printf("Weird cookie load_address failure on cookie existing - 3\n");
1377 1.1 rjs }
1378 1.1 rjs #endif
1379 1.1 rjs return (NULL);
1380 1.1 rjs }
1381 1.1 rjs
1382 1.1 rjs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1383 1.1 rjs asoc->state = SCTP_STATE_OPEN |
1384 1.1 rjs SCTP_STATE_SHUTDOWN_PENDING;
1385 1.1 rjs } else if (!(asoc->state & SCTP_STATE_SHUTDOWN_SENT)) {
1386 1.1 rjs /* move to OPEN state, if not in SHUTDOWN_SENT */
1387 1.1 rjs asoc->state = SCTP_STATE_OPEN;
1388 1.1 rjs }
1389 1.1 rjs /* respond with a COOKIE-ACK */
1390 1.1 rjs sctp_send_cookie_ack(stcb);
1391 1.1 rjs
1392 1.1 rjs return (stcb);
1393 1.1 rjs }
1394 1.1 rjs /* all other cases... */
1395 1.1 rjs return (NULL);
1396 1.1 rjs }
1397 1.1 rjs
1398 1.1 rjs /*
1399 1.1 rjs * handle a state cookie for a new association
1400 1.1 rjs * m: input packet mbuf chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk
1401 1.1 rjs * note: this is a "split" mbuf and the cookie signature does not exist
1402 1.1 rjs * offset: offset into mbuf to the cookie-echo chunk
1403 1.1 rjs * length: length of the cookie chunk
1404 1.1 rjs * to: where the init was from
1405 1.1 rjs * returns a new TCB
1406 1.1 rjs */
1407 1.1 rjs static struct sctp_tcb *
1408 1.1 rjs sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1409 1.1 rjs struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1410 1.1 rjs struct sctp_inpcb *inp, struct sctp_nets **netp,
1411 1.1 rjs struct sockaddr *init_src, int *notification)
1412 1.1 rjs {
1413 1.1 rjs struct sctp_tcb *stcb;
1414 1.1 rjs struct sctp_init_chunk *init_cp, init_buf;
1415 1.1 rjs struct sctp_init_ack_chunk *initack_cp, initack_buf;
1416 1.1 rjs struct sockaddr_storage sa_store;
1417 1.1 rjs struct sockaddr *initack_src = (struct sockaddr *)&sa_store;
1418 1.1 rjs struct sockaddr_in *sin;
1419 1.1 rjs struct sockaddr_in6 *sin6;
1420 1.1 rjs struct sctp_association *asoc;
1421 1.1 rjs int chk_length;
1422 1.1 rjs int init_offset, initack_offset, initack_limit;
1423 1.1 rjs int retval;
1424 1.1 rjs int error = 0;
1425 1.1 rjs /*
1426 1.1 rjs * find and validate the INIT chunk in the cookie (peer's info)
1427 1.1 rjs * the INIT should start after the cookie-echo header struct
1428 1.1 rjs * (chunk header, state cookie header struct)
1429 1.1 rjs */
1430 1.1 rjs init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
1431 1.1 rjs init_cp = (struct sctp_init_chunk *)
1432 1.1 rjs sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1433 1.1 rjs (u_int8_t *)&init_buf);
1434 1.1 rjs if (init_cp == NULL) {
1435 1.1 rjs /* could not pull a INIT chunk in cookie */
1436 1.1 rjs #ifdef SCTP_DEBUG
1437 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1438 1.1 rjs printf("process_cookie_new: could not pull INIT chunk hdr\n");
1439 1.1 rjs }
1440 1.1 rjs #endif /* SCTP_DEBUG */
1441 1.1 rjs return (NULL);
1442 1.1 rjs }
1443 1.1 rjs chk_length = ntohs(init_cp->ch.chunk_length);
1444 1.1 rjs if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1445 1.1 rjs #ifdef SCTP_DEBUG
1446 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1447 1.1 rjs printf("HUH? process_cookie_new: could not find INIT chunk!\n");
1448 1.1 rjs }
1449 1.1 rjs #endif /* SCTP_DEBUG */
1450 1.1 rjs return (NULL);
1451 1.1 rjs }
1452 1.1 rjs
1453 1.1 rjs initack_offset = init_offset + SCTP_SIZE32(chk_length);
1454 1.1 rjs /*
1455 1.1 rjs * find and validate the INIT-ACK chunk in the cookie (my info)
1456 1.1 rjs * the INIT-ACK follows the INIT chunk
1457 1.1 rjs */
1458 1.1 rjs initack_cp = (struct sctp_init_ack_chunk *)
1459 1.1 rjs sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1460 1.1 rjs (u_int8_t *)&initack_buf);
1461 1.1 rjs if (initack_cp == NULL) {
1462 1.1 rjs /* could not pull INIT-ACK chunk in cookie */
1463 1.1 rjs #ifdef SCTP_DEBUG
1464 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1465 1.1 rjs printf("process_cookie_new: could not pull INIT-ACK chunk hdr\n");
1466 1.1 rjs }
1467 1.1 rjs #endif /* SCTP_DEBUG */
1468 1.1 rjs return (NULL);
1469 1.1 rjs }
1470 1.1 rjs chk_length = ntohs(initack_cp->ch.chunk_length);
1471 1.1 rjs if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1472 1.1 rjs #ifdef SCTP_DEBUG
1473 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1474 1.1 rjs u_int8_t *pp;
1475 1.1 rjs pp = (u_int8_t *)initack_cp;
1476 1.1 rjs printf("process_cookie_new: could not find INIT-ACK chunk!\n");
1477 1.1 rjs printf("Found bytes %x %x %x %x at postion %d\n",
1478 1.1 rjs (u_int)pp[0], (u_int)pp[1], (u_int)pp[2],
1479 1.1 rjs (u_int)pp[3], initack_offset);
1480 1.1 rjs }
1481 1.1 rjs #endif /* SCTP_DEBUG */
1482 1.1 rjs return (NULL);
1483 1.1 rjs }
1484 1.1 rjs initack_limit = initack_offset + SCTP_SIZE32(chk_length);
1485 1.1 rjs
1486 1.1 rjs /*
1487 1.1 rjs * now that we know the INIT/INIT-ACK are in place,
1488 1.1 rjs * create a new TCB and popluate
1489 1.1 rjs */
1490 1.1 rjs stcb = sctp_aloc_assoc(inp, init_src, 0, &error, ntohl(initack_cp->init.initiate_tag));
1491 1.1 rjs if (stcb == NULL) {
1492 1.1 rjs struct mbuf *op_err;
1493 1.1 rjs /* memory problem? */
1494 1.1 rjs #ifdef SCTP_DEBUG
1495 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1496 1.1 rjs printf("process_cookie_new: no room for another TCB!\n");
1497 1.1 rjs }
1498 1.1 rjs #endif /* SCTP_DEBUG */
1499 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1500 1.1 rjs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1501 1.1 rjs sh, op_err);
1502 1.1 rjs return (NULL);
1503 1.1 rjs }
1504 1.1 rjs
1505 1.1 rjs /* get the correct sctp_nets */
1506 1.1 rjs *netp = sctp_findnet(stcb, init_src);
1507 1.1 rjs asoc = &stcb->asoc;
1508 1.1 rjs /* get scope variables out of cookie */
1509 1.1 rjs asoc->ipv4_local_scope = cookie->ipv4_scope;
1510 1.1 rjs asoc->site_scope = cookie->site_scope;
1511 1.1 rjs asoc->local_scope = cookie->local_scope;
1512 1.1 rjs asoc->loopback_scope = cookie->loopback_scope;
1513 1.1 rjs
1514 1.1 rjs if ((asoc->ipv4_addr_legal != cookie->ipv4_addr_legal) ||
1515 1.1 rjs (asoc->ipv6_addr_legal != cookie->ipv6_addr_legal)) {
1516 1.1 rjs struct mbuf *op_err;
1517 1.1 rjs /*
1518 1.1 rjs * Houston we have a problem. The EP changed while the cookie
1519 1.1 rjs * was in flight. Only recourse is to abort the association.
1520 1.1 rjs */
1521 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1522 1.1 rjs sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
1523 1.1 rjs sh, op_err);
1524 1.1 rjs return (NULL);
1525 1.1 rjs }
1526 1.1 rjs
1527 1.1 rjs /* process the INIT-ACK info (my info) */
1528 1.1 rjs asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
1529 1.1 rjs asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1530 1.1 rjs asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1531 1.1 rjs asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1532 1.1 rjs asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1533 1.1 rjs asoc->t3timeout_highest_marked = asoc->asconf_seq_out;
1534 1.1 rjs asoc->last_cwr_tsn = asoc->init_seq_number - 1;
1535 1.1 rjs asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1536 1.1 rjs asoc->str_reset_seq_in = asoc->init_seq_number;
1537 1.1 rjs
1538 1.1 rjs asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1539 1.1 rjs
1540 1.1 rjs /* process the INIT info (peer's info) */
1541 1.1 rjs retval = sctp_process_init(init_cp, stcb, *netp);
1542 1.1 rjs if (retval < 0) {
1543 1.1 rjs #ifdef SCTP_DEBUG
1544 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1545 1.1 rjs printf("process_cookie_new: INIT processing failed\n");
1546 1.1 rjs }
1547 1.1 rjs #endif
1548 1.1 rjs sctp_free_assoc(inp, stcb);
1549 1.1 rjs return (NULL);
1550 1.1 rjs }
1551 1.1 rjs /* load all addresses */
1552 1.1 rjs if (sctp_load_addresses_from_init(stcb, m, iphlen,
1553 1.1 rjs init_offset + sizeof(struct sctp_init_chunk), initack_offset, sh,
1554 1.1 rjs init_src)) {
1555 1.1 rjs sctp_free_assoc(inp, stcb);
1556 1.1 rjs return (NULL);
1557 1.1 rjs }
1558 1.1 rjs
1559 1.1 rjs /* update current state */
1560 1.1 rjs #ifdef SCTP_DEBUG
1561 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1562 1.1 rjs printf("moving to OPEN state\n");
1563 1.1 rjs }
1564 1.1 rjs #endif
1565 1.1 rjs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1566 1.1 rjs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
1567 1.1 rjs } else {
1568 1.1 rjs asoc->state = SCTP_STATE_OPEN;
1569 1.1 rjs }
1570 1.1 rjs /* calculate the RTT */
1571 1.1 rjs (*netp)->RTO = sctp_calculate_rto(stcb, asoc, *netp,
1572 1.1 rjs &cookie->time_entered);
1573 1.1 rjs
1574 1.1 rjs /*
1575 1.1 rjs * if we're doing ASCONFs, check to see if we have any new
1576 1.1 rjs * local addresses that need to get added to the peer (eg.
1577 1.1 rjs * addresses changed while cookie echo in flight). This needs
1578 1.1 rjs * to be done after we go to the OPEN state to do the correct
1579 1.1 rjs * asconf processing.
1580 1.1 rjs * else, make sure we have the correct addresses in our lists
1581 1.1 rjs */
1582 1.1 rjs
1583 1.1 rjs /* warning, we re-use sin, sin6, sa_store here! */
1584 1.1 rjs /* pull in local_address (our "from" address) */
1585 1.1 rjs if (cookie->laddr_type == SCTP_IPV4_ADDRESS) {
1586 1.1 rjs /* source addr is IPv4 */
1587 1.1 rjs sin = (struct sockaddr_in *)initack_src;
1588 1.1 rjs memset(sin, 0, sizeof(*sin));
1589 1.1 rjs sin->sin_family = AF_INET;
1590 1.1 rjs sin->sin_len = sizeof(struct sockaddr_in);
1591 1.1 rjs sin->sin_addr.s_addr = cookie->laddress[0];
1592 1.1 rjs } else if (cookie->laddr_type == SCTP_IPV6_ADDRESS) {
1593 1.1 rjs /* source addr is IPv6 */
1594 1.1 rjs sin6 = (struct sockaddr_in6 *)initack_src;
1595 1.1 rjs memset(sin6, 0, sizeof(*sin6));
1596 1.1 rjs sin6->sin6_family = AF_INET6;
1597 1.1 rjs sin6->sin6_len = sizeof(struct sockaddr_in6);
1598 1.1 rjs sin6->sin6_scope_id = cookie->scope_id;
1599 1.1 rjs memcpy(&sin6->sin6_addr, cookie->laddress,
1600 1.1 rjs sizeof(sin6->sin6_addr));
1601 1.1 rjs } else {
1602 1.1 rjs sctp_free_assoc(inp, stcb);
1603 1.1 rjs return (NULL);
1604 1.1 rjs }
1605 1.1 rjs
1606 1.1 rjs sctp_check_address_list(stcb, m, initack_offset +
1607 1.1 rjs sizeof(struct sctp_init_ack_chunk), initack_limit,
1608 1.1 rjs initack_src, cookie->local_scope, cookie->site_scope,
1609 1.1 rjs cookie->ipv4_scope, cookie->loopback_scope);
1610 1.1 rjs
1611 1.1 rjs
1612 1.1 rjs /* set up to notify upper layer */
1613 1.1 rjs *notification = SCTP_NOTIFY_ASSOC_UP;
1614 1.1 rjs if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1615 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1616 1.1 rjs !(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1617 1.1 rjs /*
1618 1.1 rjs * This is an endpoint that called connect()
1619 1.1 rjs * how it got a cookie that is NEW is a bit of
1620 1.1 rjs * a mystery. It must be that the INIT was sent, but
1621 1.1 rjs * before it got there.. a complete INIT/INIT-ACK/COOKIE
1622 1.1 rjs * arrived. But of course then it should have went to
1623 1.1 rjs * the other code.. not here.. oh well.. a bit of protection
1624 1.1 rjs * is worth having..
1625 1.1 rjs */
1626 1.1 rjs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1627 1.1 rjs soisconnected(stcb->sctp_ep->sctp_socket);
1628 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1629 1.1 rjs } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1630 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
1631 1.1 rjs /*
1632 1.1 rjs * We don't want to do anything with this
1633 1.1 rjs * one. Since it is the listening guy. The timer will
1634 1.1 rjs * get started for accepted connections in the caller.
1635 1.1 rjs */
1636 1.1 rjs ;
1637 1.1 rjs } else {
1638 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, *netp);
1639 1.1 rjs }
1640 1.1 rjs /* since we did not send a HB make sure we don't double things */
1641 1.1 rjs (*netp)->hb_responded = 1;
1642 1.1 rjs
1643 1.1 rjs if (stcb->asoc.sctp_autoclose_ticks &&
1644 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
1645 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
1646 1.1 rjs }
1647 1.1 rjs
1648 1.1 rjs /* respond with a COOKIE-ACK */
1649 1.1 rjs sctp_send_cookie_ack(stcb);
1650 1.1 rjs
1651 1.1 rjs return (stcb);
1652 1.1 rjs }
1653 1.1 rjs
1654 1.1 rjs
1655 1.1 rjs /*
1656 1.1 rjs * handles a COOKIE-ECHO message
1657 1.1 rjs * stcb: modified to either a new or left as existing (non-NULL) TCB
1658 1.1 rjs */
1659 1.1 rjs static struct mbuf *
1660 1.1 rjs sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
1661 1.1 rjs struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
1662 1.1 rjs struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp)
1663 1.1 rjs {
1664 1.1 rjs struct sctp_state_cookie *cookie;
1665 1.1 rjs struct sockaddr_in6 sin6;
1666 1.1 rjs struct sockaddr_in sin;
1667 1.1 rjs struct sctp_tcb *l_stcb=*stcb;
1668 1.1 rjs struct sctp_inpcb *l_inp;
1669 1.1 rjs struct sockaddr *to;
1670 1.1 rjs struct sctp_pcb *ep;
1671 1.1 rjs struct mbuf *m_sig;
1672 1.1 rjs uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
1673 1.1 rjs uint8_t *sig;
1674 1.1 rjs uint8_t cookie_ok = 0;
1675 1.1 rjs unsigned int size_of_pkt, sig_offset, cookie_offset;
1676 1.1 rjs unsigned int cookie_len;
1677 1.1 rjs struct timeval now;
1678 1.1 rjs struct timeval time_expires;
1679 1.1 rjs struct sockaddr_storage dest_store;
1680 1.1 rjs struct sockaddr *localep_sa = (struct sockaddr *)&dest_store;
1681 1.1 rjs struct ip *iph;
1682 1.1 rjs int notification = 0;
1683 1.1 rjs struct sctp_nets *netl;
1684 1.1 rjs int had_a_existing_tcb = 0;
1685 1.1 rjs
1686 1.1 rjs #ifdef SCTP_DEBUG
1687 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1688 1.1 rjs printf("sctp_handle_cookie: handling COOKIE-ECHO\n");
1689 1.1 rjs }
1690 1.1 rjs #endif
1691 1.1 rjs
1692 1.1 rjs if (inp_p == NULL) {
1693 1.1 rjs #ifdef SCTP_DEBUG
1694 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1695 1.1 rjs printf("sctp_handle_cookie: null inp_p!\n");
1696 1.1 rjs }
1697 1.1 rjs #endif
1698 1.1 rjs return (NULL);
1699 1.1 rjs }
1700 1.1 rjs /* First get the destination address setup too. */
1701 1.1 rjs iph = mtod(m, struct ip *);
1702 1.1 rjs if (iph->ip_v == IPVERSION) {
1703 1.1 rjs /* its IPv4 */
1704 1.1 rjs struct sockaddr_in *sin_d;
1705 1.1 rjs sin_d = (struct sockaddr_in *)(localep_sa);
1706 1.1 rjs memset(sin_d, 0, sizeof(*sin_d));
1707 1.1 rjs sin_d->sin_family = AF_INET;
1708 1.1 rjs sin_d->sin_len = sizeof(*sin_d);
1709 1.1 rjs sin_d->sin_port = sh->dest_port;
1710 1.1 rjs sin_d->sin_addr.s_addr = iph->ip_dst.s_addr ;
1711 1.1 rjs } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1712 1.1 rjs /* its IPv6 */
1713 1.1 rjs struct ip6_hdr *ip6;
1714 1.1 rjs struct sockaddr_in6 *sin6_d;
1715 1.1 rjs sin6_d = (struct sockaddr_in6 *)(localep_sa);
1716 1.1 rjs memset(sin6_d, 0, sizeof(*sin6_d));
1717 1.1 rjs sin6_d->sin6_family = AF_INET6;
1718 1.1 rjs sin6_d->sin6_len = sizeof(struct sockaddr_in6);
1719 1.1 rjs ip6 = mtod(m, struct ip6_hdr *);
1720 1.1 rjs sin6_d->sin6_port = sh->dest_port;
1721 1.1 rjs sin6_d->sin6_addr = ip6->ip6_dst;
1722 1.1 rjs } else {
1723 1.1 rjs return (NULL);
1724 1.1 rjs }
1725 1.1 rjs
1726 1.1 rjs cookie = &cp->cookie;
1727 1.1 rjs cookie_offset = offset + sizeof(struct sctp_chunkhdr);
1728 1.1 rjs cookie_len = ntohs(cp->ch.chunk_length);
1729 1.1 rjs
1730 1.1 rjs /* compute size of packet */
1731 1.1 rjs if (m->m_flags & M_PKTHDR) {
1732 1.1 rjs size_of_pkt = m->m_pkthdr.len;
1733 1.1 rjs } else {
1734 1.1 rjs /* Should have a pkt hdr really */
1735 1.1 rjs struct mbuf *mat;
1736 1.1 rjs mat = m;
1737 1.1 rjs size_of_pkt = 0;
1738 1.1 rjs while (mat != NULL) {
1739 1.1 rjs size_of_pkt += mat->m_len;
1740 1.1 rjs mat = mat->m_next;
1741 1.1 rjs }
1742 1.1 rjs }
1743 1.1 rjs if (cookie_len > size_of_pkt ||
1744 1.1 rjs cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
1745 1.1 rjs sizeof(struct sctp_init_chunk) +
1746 1.1 rjs sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
1747 1.1 rjs /* cookie too long! or too small */
1748 1.1 rjs #ifdef SCTP_DEBUG
1749 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1750 1.1 rjs printf("sctp_handle_cookie: cookie_len=%u, pkt size=%u\n", cookie_len, size_of_pkt);
1751 1.1 rjs }
1752 1.1 rjs #endif /* SCTP_DEBUG */
1753 1.1 rjs return (NULL);
1754 1.1 rjs }
1755 1.7 rjs
1756 1.7 rjs if ((cookie->peerport != sh->src_port) &&
1757 1.7 rjs (cookie->myport != sh->dest_port) &&
1758 1.7 rjs (cookie->my_vtag != sh->v_tag)) {
1759 1.7 rjs /*
1760 1.7 rjs * invalid ports or bad tag. Note that we always leave
1761 1.7 rjs * the v_tag in the header in network order and when we
1762 1.7 rjs * stored it in the my_vtag slot we also left it in network
1763 1.7 rjs * order. This maintians the match even though it may be in
1764 1.7 rjs * the opposite byte order of the machine :->
1765 1.7 rjs */
1766 1.7 rjs return (NULL);
1767 1.7 rjs }
1768 1.7 rjs
1769 1.1 rjs /*
1770 1.1 rjs * split off the signature into its own mbuf (since it
1771 1.1 rjs * should not be calculated in the sctp_hash_digest_m() call).
1772 1.1 rjs */
1773 1.1 rjs sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
1774 1.1 rjs if (sig_offset > size_of_pkt) {
1775 1.1 rjs /* packet not correct size! */
1776 1.1 rjs /* XXX this may already be accounted for earlier... */
1777 1.1 rjs #ifdef SCTP_DEBUG
1778 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1779 1.1 rjs printf("sctp_handle_cookie: sig offset=%u, pkt size=%u\n", sig_offset, size_of_pkt);
1780 1.1 rjs }
1781 1.1 rjs #endif
1782 1.1 rjs return (NULL);
1783 1.1 rjs }
1784 1.1 rjs
1785 1.1 rjs m_sig = m_split(m, sig_offset, M_DONTWAIT);
1786 1.1 rjs if (m_sig == NULL) {
1787 1.1 rjs /* out of memory or ?? */
1788 1.1 rjs #ifdef SCTP_DEBUG
1789 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1790 1.1 rjs printf("sctp_handle_cookie: couldn't m_split the signature\n");
1791 1.1 rjs }
1792 1.1 rjs #endif
1793 1.1 rjs return (NULL);
1794 1.1 rjs }
1795 1.1 rjs /*
1796 1.1 rjs * compute the signature/digest for the cookie
1797 1.1 rjs */
1798 1.1 rjs ep = &(*inp_p)->sctp_ep;
1799 1.1 rjs l_inp = *inp_p;
1800 1.1 rjs if (l_stcb) {
1801 1.1 rjs SCTP_TCB_UNLOCK(l_stcb);
1802 1.1 rjs }
1803 1.1 rjs SCTP_INP_RLOCK(l_inp);
1804 1.1 rjs if (l_stcb) {
1805 1.1 rjs SCTP_TCB_LOCK(l_stcb);
1806 1.1 rjs }
1807 1.1 rjs /* which cookie is it? */
1808 1.1 rjs if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
1809 1.1 rjs (ep->current_secret_number != ep->last_secret_number)) {
1810 1.1 rjs /* it's the old cookie */
1811 1.1 rjs #ifdef SCTP_DEBUG
1812 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1813 1.1 rjs printf("sctp_handle_cookie: old cookie sig\n");
1814 1.1 rjs }
1815 1.1 rjs #endif
1816 1.1 rjs sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1817 1.1 rjs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1818 1.1 rjs } else {
1819 1.1 rjs /* it's the current cookie */
1820 1.1 rjs #ifdef SCTP_DEBUG
1821 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1822 1.1 rjs printf("sctp_handle_cookie: current cookie sig\n");
1823 1.1 rjs }
1824 1.1 rjs #endif
1825 1.1 rjs sctp_hash_digest_m((char *)ep->secret_key[(int)ep->current_secret_number],
1826 1.1 rjs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1827 1.1 rjs }
1828 1.1 rjs /* get the signature */
1829 1.1 rjs SCTP_INP_RUNLOCK(l_inp);
1830 1.1 rjs sig = (u_int8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (u_int8_t *)&tmp_sig);
1831 1.1 rjs if (sig == NULL) {
1832 1.1 rjs /* couldn't find signature */
1833 1.1 rjs #ifdef SCTP_DEBUG
1834 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
1835 1.1 rjs printf("sctp_handle_cookie: couldn't pull the signature\n");
1836 1.1 rjs }
1837 1.1 rjs #endif
1838 1.1 rjs return (NULL);
1839 1.1 rjs }
1840 1.1 rjs /* compare the received digest with the computed digest */
1841 1.1 rjs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
1842 1.1 rjs /* try the old cookie? */
1843 1.1 rjs if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
1844 1.1 rjs (ep->current_secret_number != ep->last_secret_number)) {
1845 1.1 rjs /* compute digest with old */
1846 1.1 rjs #ifdef SCTP_DEBUG
1847 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1848 1.1 rjs printf("sctp_handle_cookie: old cookie sig\n");
1849 1.1 rjs }
1850 1.1 rjs #endif
1851 1.1 rjs sctp_hash_digest_m((char *)ep->secret_key[(int)ep->last_secret_number],
1852 1.1 rjs SCTP_SECRET_SIZE, m, cookie_offset, calc_sig);
1853 1.1 rjs /* compare */
1854 1.1 rjs if (memcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
1855 1.1 rjs cookie_ok = 1;
1856 1.1 rjs }
1857 1.1 rjs } else {
1858 1.1 rjs cookie_ok = 1;
1859 1.1 rjs }
1860 1.1 rjs
1861 1.1 rjs /*
1862 1.1 rjs * Now before we continue we must reconstruct our mbuf so
1863 1.1 rjs * that normal processing of any other chunks will work.
1864 1.1 rjs */
1865 1.1 rjs {
1866 1.1 rjs struct mbuf *m_at;
1867 1.1 rjs m_at = m;
1868 1.1 rjs while (m_at->m_next != NULL) {
1869 1.1 rjs m_at = m_at->m_next;
1870 1.1 rjs }
1871 1.1 rjs m_at->m_next = m_sig;
1872 1.1 rjs if (m->m_flags & M_PKTHDR) {
1873 1.1 rjs /*
1874 1.1 rjs * We should only do this if and only if the front
1875 1.1 rjs * mbuf has a m_pkthdr... it should in theory.
1876 1.1 rjs */
1877 1.1 rjs if (m_sig->m_flags & M_PKTHDR) {
1878 1.1 rjs /* Add back to the pkt hdr of main m chain */
1879 1.1 rjs m->m_pkthdr.len += m_sig->m_len;
1880 1.1 rjs } else {
1881 1.1 rjs /*
1882 1.1 rjs * Got a problem, no pkthdr in split chain.
1883 1.1 rjs * TSNH but we will handle it just in case
1884 1.1 rjs */
1885 1.1 rjs int mmlen = 0;
1886 1.1 rjs struct mbuf *lat;
1887 1.1 rjs printf("Warning: Hitting m_split join TSNH code - fixed\n");
1888 1.1 rjs lat = m_sig;
1889 1.1 rjs while (lat) {
1890 1.1 rjs mmlen += lat->m_len;
1891 1.1 rjs lat = lat->m_next;
1892 1.1 rjs }
1893 1.1 rjs m->m_pkthdr.len += mmlen;
1894 1.1 rjs }
1895 1.1 rjs }
1896 1.1 rjs }
1897 1.1 rjs
1898 1.1 rjs if (cookie_ok == 0) {
1899 1.1 rjs #ifdef SCTP_DEBUG
1900 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1901 1.1 rjs printf("handle_cookie_echo: cookie signature validation failed!\n");
1902 1.1 rjs printf("offset = %u, cookie_offset = %u, sig_offset = %u\n",
1903 1.1 rjs (u_int32_t)offset, cookie_offset, sig_offset);
1904 1.1 rjs }
1905 1.1 rjs #endif
1906 1.1 rjs return (NULL);
1907 1.1 rjs }
1908 1.1 rjs #ifdef SCTP_DEBUG
1909 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
1910 1.1 rjs printf("handle_cookie_echo: cookie signature validation passed\n");
1911 1.1 rjs }
1912 1.1 rjs #endif
1913 1.1 rjs
1914 1.1 rjs /*
1915 1.1 rjs * check the cookie timestamps to be sure it's not stale
1916 1.1 rjs */
1917 1.1 rjs SCTP_GETTIME_TIMEVAL(&now);
1918 1.1 rjs /* Expire time is in Ticks, so we convert to seconds */
1919 1.1 rjs time_expires.tv_sec = cookie->time_entered.tv_sec + cookie->cookie_life;
1920 1.1 rjs time_expires.tv_usec = cookie->time_entered.tv_usec;
1921 1.1 rjs #ifndef __FreeBSD__
1922 1.1 rjs if (timercmp(&now, &time_expires, >))
1923 1.1 rjs #else
1924 1.1 rjs if (timevalcmp(&now, &time_expires, >))
1925 1.1 rjs #endif
1926 1.1 rjs {
1927 1.1 rjs /* cookie is stale! */
1928 1.1 rjs struct mbuf *op_err;
1929 1.1 rjs struct sctp_stale_cookie_msg *scm;
1930 1.1 rjs u_int32_t tim;
1931 1.1 rjs #ifdef SCTP_DEBUG
1932 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1933 1.1 rjs printf("sctp_handle_cookie: got a STALE cookie!\n");
1934 1.1 rjs }
1935 1.1 rjs #endif
1936 1.1 rjs MGETHDR(op_err, M_DONTWAIT, MT_HEADER);
1937 1.1 rjs if (op_err == NULL) {
1938 1.1 rjs /* FOOBAR */
1939 1.1 rjs return (NULL);
1940 1.1 rjs }
1941 1.1 rjs /* pre-reserve some space */
1942 1.1 rjs op_err->m_data += sizeof(struct ip6_hdr);
1943 1.1 rjs op_err->m_data += sizeof(struct sctphdr);
1944 1.1 rjs op_err->m_data += sizeof(struct sctp_chunkhdr);
1945 1.1 rjs
1946 1.1 rjs /* Set the len */
1947 1.1 rjs op_err->m_len = op_err->m_pkthdr.len = sizeof(struct sctp_stale_cookie_msg);
1948 1.1 rjs scm = mtod(op_err, struct sctp_stale_cookie_msg *);
1949 1.1 rjs scm->ph.param_type = htons(SCTP_CAUSE_STALE_COOKIE);
1950 1.1 rjs scm->ph.param_length = htons((sizeof(struct sctp_paramhdr) +
1951 1.1 rjs (sizeof(u_int32_t))));
1952 1.1 rjs /* seconds to usec */
1953 1.1 rjs tim = (now.tv_sec - time_expires.tv_sec) * 1000000;
1954 1.1 rjs /* add in usec */
1955 1.1 rjs if (tim == 0)
1956 1.1 rjs tim = now.tv_usec - cookie->time_entered.tv_usec;
1957 1.1 rjs scm->time_usec = htonl(tim);
1958 1.1 rjs sctp_send_operr_to(m, iphlen, op_err, cookie->peers_vtag);
1959 1.1 rjs return (NULL);
1960 1.1 rjs }
1961 1.1 rjs /*
1962 1.1 rjs * Now we must see with the lookup address if we have an existing
1963 1.1 rjs * asoc. This will only happen if we were in the COOKIE-WAIT state
1964 1.1 rjs * and a INIT collided with us and somewhere the peer sent the
1965 1.1 rjs * cookie on another address besides the single address our assoc
1966 1.1 rjs * had for him. In this case we will have one of the tie-tags set
1967 1.1 rjs * at least AND the address field in the cookie can be used to
1968 1.1 rjs * look it up.
1969 1.1 rjs */
1970 1.1 rjs to = NULL;
1971 1.1 rjs if (cookie->addr_type == SCTP_IPV6_ADDRESS) {
1972 1.1 rjs memset(&sin6, 0, sizeof(sin6));
1973 1.1 rjs sin6.sin6_family = AF_INET6;
1974 1.1 rjs sin6.sin6_len = sizeof(sin6);
1975 1.1 rjs sin6.sin6_port = sh->src_port;
1976 1.1 rjs sin6.sin6_scope_id = cookie->scope_id;
1977 1.1 rjs memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
1978 1.1 rjs sizeof(sin6.sin6_addr.s6_addr));
1979 1.1 rjs to = (struct sockaddr *)&sin6;
1980 1.1 rjs } else if (cookie->addr_type == SCTP_IPV4_ADDRESS) {
1981 1.1 rjs memset(&sin, 0, sizeof(sin));
1982 1.1 rjs sin.sin_family = AF_INET;
1983 1.1 rjs sin.sin_len = sizeof(sin);
1984 1.1 rjs sin.sin_port = sh->src_port;
1985 1.1 rjs sin.sin_addr.s_addr = cookie->address[0];
1986 1.1 rjs to = (struct sockaddr *)&sin;
1987 1.1 rjs }
1988 1.1 rjs
1989 1.1 rjs if ((*stcb == NULL) && to) {
1990 1.1 rjs /* Yep, lets check */
1991 1.1 rjs *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, localep_sa, NULL);
1992 1.1 rjs if (*stcb == NULL) {
1993 1.1 rjs /* We should have only got back the same inp. If we
1994 1.1 rjs * got back a different ep we have a problem. The original
1995 1.1 rjs * findep got back l_inp and now
1996 1.1 rjs */
1997 1.1 rjs if (l_inp != *inp_p) {
1998 1.1 rjs printf("Bad problem find_ep got a diff inp then special_locate?\n");
1999 1.1 rjs }
2000 1.1 rjs }
2001 1.1 rjs }
2002 1.1 rjs
2003 1.1 rjs cookie_len -= SCTP_SIGNATURE_SIZE;
2004 1.1 rjs if (*stcb == NULL) {
2005 1.1 rjs /* this is the "normal" case... get a new TCB */
2006 1.1 rjs #ifdef SCTP_DEBUG
2007 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2008 1.1 rjs printf("sctp_handle_cookie: processing NEW cookie\n");
2009 1.1 rjs }
2010 1.1 rjs #endif
2011 1.1 rjs *stcb = sctp_process_cookie_new(m, iphlen, offset, sh, cookie,
2012 1.1 rjs cookie_len, *inp_p, netp, to, ¬ification);
2013 1.1 rjs /* now always decrement, since this is the normal
2014 1.1 rjs * case.. we had no tcb when we entered.
2015 1.1 rjs */
2016 1.1 rjs } else {
2017 1.1 rjs /* this is abnormal... cookie-echo on existing TCB */
2018 1.1 rjs #ifdef SCTP_DEBUG
2019 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2020 1.1 rjs printf("sctp_handle_cookie: processing EXISTING cookie\n");
2021 1.1 rjs }
2022 1.1 rjs #endif
2023 1.1 rjs had_a_existing_tcb = 1;
2024 1.1 rjs *stcb = sctp_process_cookie_existing(m, iphlen, offset, sh,
2025 1.1 rjs cookie, cookie_len, *inp_p, *stcb, *netp, to, ¬ification);
2026 1.1 rjs }
2027 1.1 rjs
2028 1.1 rjs if (*stcb == NULL) {
2029 1.1 rjs /* still no TCB... must be bad cookie-echo */
2030 1.1 rjs #ifdef SCTP_DEBUG
2031 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2032 1.1 rjs printf("handle_cookie_echo: ACK! don't have a TCB!\n");
2033 1.1 rjs }
2034 1.1 rjs #endif /* SCTP_DEBUG */
2035 1.1 rjs return (NULL);
2036 1.1 rjs }
2037 1.1 rjs
2038 1.1 rjs /*
2039 1.1 rjs * Ok, we built an association so confirm the address
2040 1.1 rjs * we sent the INIT-ACK to.
2041 1.1 rjs */
2042 1.1 rjs netl = sctp_findnet(*stcb, to);
2043 1.1 rjs /* This code should in theory NOT run but
2044 1.1 rjs */
2045 1.1 rjs if (netl == NULL) {
2046 1.1 rjs #ifdef SCTP_DEBUG
2047 1.1 rjs printf("TSNH! Huh, why do I need to add this address here?\n");
2048 1.1 rjs #endif
2049 1.1 rjs sctp_add_remote_addr(*stcb, to, 0, 100);
2050 1.1 rjs netl = sctp_findnet(*stcb, to);
2051 1.1 rjs }
2052 1.1 rjs if (netl) {
2053 1.1 rjs if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2054 1.1 rjs netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2055 1.1 rjs sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2056 1.1 rjs netl);
2057 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2058 1.1 rjs (*stcb), 0, (void *)netl);
2059 1.1 rjs }
2060 1.1 rjs }
2061 1.1 rjs #ifdef SCTP_DEBUG
2062 1.1 rjs else {
2063 1.1 rjs printf("Could not add source address for some reason\n");
2064 1.1 rjs }
2065 1.1 rjs #endif
2066 1.1 rjs
2067 1.1 rjs if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2068 1.1 rjs if (!had_a_existing_tcb ||
2069 1.1 rjs (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2070 1.1 rjs /*
2071 1.1 rjs * If we have a NEW cookie or the connect never reached
2072 1.1 rjs * the connected state during collision we must do the
2073 1.1 rjs * TCP accept thing.
2074 1.1 rjs */
2075 1.1 rjs struct socket *so, *oso;
2076 1.1 rjs struct sctp_inpcb *inp;
2077 1.1 rjs if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2078 1.1 rjs /*
2079 1.1 rjs * For a restart we will keep the same socket,
2080 1.1 rjs * no need to do anything. I THINK!!
2081 1.1 rjs */
2082 1.1 rjs sctp_ulp_notify(notification, *stcb, 0, NULL);
2083 1.1 rjs return (m);
2084 1.1 rjs }
2085 1.1 rjs oso = (*inp_p)->sctp_socket;
2086 1.1 rjs SCTP_TCB_UNLOCK((*stcb));
2087 1.1 rjs so = sonewconn(oso, SS_ISCONNECTED);
2088 1.1 rjs SCTP_INP_WLOCK((*stcb)->sctp_ep);
2089 1.1 rjs SCTP_TCB_LOCK((*stcb));
2090 1.1 rjs SCTP_INP_WUNLOCK((*stcb)->sctp_ep);
2091 1.1 rjs if (so == NULL) {
2092 1.1 rjs struct mbuf *op_err;
2093 1.1 rjs /* Too many sockets */
2094 1.1 rjs #ifdef SCTP_DEBUG
2095 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
2096 1.1 rjs printf("process_cookie_new: no room for another socket!\n");
2097 1.1 rjs }
2098 1.1 rjs #endif /* SCTP_DEBUG */
2099 1.1 rjs op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2100 1.1 rjs sctp_abort_association(*inp_p, NULL, m, iphlen,
2101 1.1 rjs sh, op_err);
2102 1.1 rjs sctp_free_assoc(*inp_p, *stcb);
2103 1.1 rjs return (NULL);
2104 1.1 rjs }
2105 1.1 rjs inp = (struct sctp_inpcb *)so->so_pcb;
2106 1.1 rjs inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2107 1.1 rjs SCTP_PCB_FLAGS_CONNECTED |
2108 1.1 rjs SCTP_PCB_FLAGS_IN_TCPPOOL |
2109 1.1 rjs (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2110 1.1 rjs SCTP_PCB_FLAGS_DONT_WAKE);
2111 1.1 rjs inp->sctp_socket = so;
2112 1.1 rjs
2113 1.1 rjs /*
2114 1.1 rjs * Now we must move it from one hash table to another
2115 1.1 rjs * and get the tcb in the right place.
2116 1.1 rjs */
2117 1.1 rjs sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2118 1.1 rjs
2119 1.1 rjs /* Switch over to the new guy */
2120 1.1 rjs *inp_p = inp;
2121 1.1 rjs
2122 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp,
2123 1.1 rjs *stcb, *netp);
2124 1.1 rjs
2125 1.1 rjs sctp_ulp_notify(notification, *stcb, 0, NULL);
2126 1.1 rjs return (m);
2127 1.1 rjs }
2128 1.1 rjs }
2129 1.1 rjs if ((notification) && ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
2130 1.1 rjs sctp_ulp_notify(notification, *stcb, 0, NULL);
2131 1.1 rjs }
2132 1.1 rjs return (m);
2133 1.1 rjs }
2134 1.1 rjs
2135 1.1 rjs static void
2136 1.1 rjs sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp,
2137 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
2138 1.1 rjs {
2139 1.1 rjs /* cp must not be used, others call this without a c-ack :-) */
2140 1.1 rjs struct sctp_association *asoc;
2141 1.1 rjs
2142 1.1 rjs #ifdef SCTP_DEBUG
2143 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2144 1.1 rjs printf("sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2145 1.1 rjs }
2146 1.1 rjs #endif
2147 1.1 rjs if (stcb == NULL)
2148 1.1 rjs return;
2149 1.1 rjs
2150 1.1 rjs asoc = &stcb->asoc;
2151 1.1 rjs
2152 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2153 1.1 rjs
2154 1.1 rjs /* process according to association state */
2155 1.1 rjs if (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) {
2156 1.1 rjs /* state change only needed when I am in right state */
2157 1.1 rjs #ifdef SCTP_DEBUG
2158 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2159 1.1 rjs printf("moving to OPEN state\n");
2160 1.1 rjs }
2161 1.1 rjs #endif
2162 1.1 rjs if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2163 1.1 rjs asoc->state = SCTP_STATE_OPEN | SCTP_STATE_SHUTDOWN_PENDING;
2164 1.1 rjs } else {
2165 1.1 rjs asoc->state = SCTP_STATE_OPEN;
2166 1.1 rjs }
2167 1.1 rjs
2168 1.1 rjs /* update RTO */
2169 1.1 rjs if (asoc->overall_error_count == 0) {
2170 1.1 rjs net->RTO = sctp_calculate_rto(stcb, asoc, net,
2171 1.1 rjs &asoc->time_entered);
2172 1.1 rjs }
2173 1.1 rjs SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2174 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL);
2175 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2176 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2177 1.1 rjs stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2178 1.1 rjs soisconnected(stcb->sctp_ep->sctp_socket);
2179 1.1 rjs }
2180 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
2181 1.1 rjs stcb, net);
2182 1.1 rjs /* since we did not send a HB make sure we don't double things */
2183 1.1 rjs net->hb_responded = 1;
2184 1.1 rjs
2185 1.1 rjs if (stcb->asoc.sctp_autoclose_ticks &&
2186 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE)) {
2187 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
2188 1.1 rjs stcb->sctp_ep, stcb, NULL);
2189 1.1 rjs }
2190 1.1 rjs
2191 1.1 rjs /*
2192 1.1 rjs * set ASCONF timer if ASCONFs are pending and allowed
2193 1.1 rjs * (eg. addresses changed when init/cookie echo in flight)
2194 1.1 rjs */
2195 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) &&
2196 1.1 rjs (stcb->asoc.peer_supports_asconf) &&
2197 1.1 rjs (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
2198 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
2199 1.1 rjs stcb->sctp_ep, stcb,
2200 1.1 rjs stcb->asoc.primary_destination);
2201 1.1 rjs }
2202 1.1 rjs
2203 1.1 rjs }
2204 1.1 rjs /* Toss the cookie if I can */
2205 1.1 rjs sctp_toss_old_cookies(asoc);
2206 1.1 rjs if (!TAILQ_EMPTY(&asoc->sent_queue)) {
2207 1.1 rjs /* Restart the timer if we have pending data */
2208 1.1 rjs struct sctp_tmit_chunk *chk;
2209 1.1 rjs chk = TAILQ_FIRST(&asoc->sent_queue);
2210 1.1 rjs if (chk) {
2211 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2212 1.1 rjs stcb, chk->whoTo);
2213 1.1 rjs }
2214 1.1 rjs }
2215 1.1 rjs
2216 1.1 rjs }
2217 1.1 rjs
2218 1.1 rjs static void
2219 1.1 rjs sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
2220 1.1 rjs struct sctp_tcb *stcb)
2221 1.1 rjs {
2222 1.1 rjs struct sctp_nets *net;
2223 1.1 rjs struct sctp_tmit_chunk *lchk;
2224 1.1 rjs u_int32_t tsn;
2225 1.1 rjs if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_ecne_chunk)) {
2226 1.1 rjs return;
2227 1.1 rjs }
2228 1.1 rjs sctp_pegs[SCTP_ECNE_RCVD]++;
2229 1.1 rjs tsn = ntohl(cp->tsn);
2230 1.1 rjs /* ECN Nonce stuff: need a resync and disable the nonce sum check */
2231 1.1 rjs /* Also we make sure we disable the nonce_wait */
2232 1.1 rjs lchk = TAILQ_FIRST(&stcb->asoc.send_queue);
2233 1.1 rjs if (lchk == NULL) {
2234 1.1 rjs stcb->asoc.nonce_resync_tsn = stcb->asoc.sending_seq;
2235 1.1 rjs } else {
2236 1.1 rjs stcb->asoc.nonce_resync_tsn = lchk->rec.data.TSN_seq;
2237 1.1 rjs }
2238 1.1 rjs stcb->asoc.nonce_wait_for_ecne = 0;
2239 1.1 rjs stcb->asoc.nonce_sum_check = 0;
2240 1.1 rjs
2241 1.1 rjs /* Find where it was sent, if possible */
2242 1.1 rjs net = NULL;
2243 1.1 rjs lchk = TAILQ_FIRST(&stcb->asoc.sent_queue);
2244 1.1 rjs while (lchk) {
2245 1.1 rjs if (lchk->rec.data.TSN_seq == tsn) {
2246 1.1 rjs net = lchk->whoTo;
2247 1.1 rjs break;
2248 1.1 rjs }
2249 1.1 rjs if (compare_with_wrap(lchk->rec.data.TSN_seq, tsn, MAX_SEQ))
2250 1.1 rjs break;
2251 1.1 rjs lchk = TAILQ_NEXT(lchk, sctp_next);
2252 1.1 rjs }
2253 1.1 rjs if (net == NULL)
2254 1.1 rjs /* default is we use the primary */
2255 1.1 rjs net = stcb->asoc.primary_destination;
2256 1.1 rjs
2257 1.1 rjs if (compare_with_wrap(tsn, stcb->asoc.last_cwr_tsn, MAX_TSN)) {
2258 1.1 rjs #ifdef SCTP_CWND_LOGGING
2259 1.1 rjs int old_cwnd;
2260 1.1 rjs #endif
2261 1.1 rjs #ifdef SCTP_CWND_LOGGING
2262 1.1 rjs old_cwnd = net->cwnd;
2263 1.1 rjs #endif
2264 1.1 rjs sctp_pegs[SCTP_CWR_PERFO]++;
2265 1.1 rjs net->ssthresh = net->cwnd / 2;
2266 1.1 rjs if (net->ssthresh < net->mtu) {
2267 1.1 rjs net->ssthresh = net->mtu;
2268 1.1 rjs /* here back off the timer as well, to slow us down */
2269 1.1 rjs net->RTO <<= 2;
2270 1.1 rjs }
2271 1.1 rjs net->cwnd = net->ssthresh;
2272 1.1 rjs #ifdef SCTP_CWND_LOGGING
2273 1.1 rjs sctp_log_cwnd(net, (net->cwnd-old_cwnd), SCTP_CWND_LOG_FROM_SAT);
2274 1.1 rjs #endif
2275 1.1 rjs /* we reduce once every RTT. So we will only lower
2276 1.1 rjs * cwnd at the next sending seq i.e. the resync_tsn.
2277 1.1 rjs */
2278 1.1 rjs stcb->asoc.last_cwr_tsn = stcb->asoc.nonce_resync_tsn;
2279 1.1 rjs }
2280 1.1 rjs /*
2281 1.1 rjs * We always send a CWR this way if our previous one was lost
2282 1.1 rjs * our peer will get an update, or if it is not time again
2283 1.1 rjs * to reduce we still get the cwr to the peer.
2284 1.1 rjs */
2285 1.1 rjs sctp_send_cwr(stcb, net, tsn);
2286 1.1 rjs }
2287 1.1 rjs
2288 1.1 rjs static void
2289 1.1 rjs sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb)
2290 1.1 rjs {
2291 1.1 rjs /* Here we get a CWR from the peer. We must look in
2292 1.1 rjs * the outqueue and make sure that we have a covered
2293 1.1 rjs * ECNE in teh control chunk part. If so remove it.
2294 1.1 rjs */
2295 1.1 rjs struct sctp_tmit_chunk *chk;
2296 1.1 rjs struct sctp_ecne_chunk *ecne;
2297 1.1 rjs
2298 1.1 rjs TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
2299 1.1 rjs if (chk->rec.chunk_id != SCTP_ECN_ECHO) {
2300 1.1 rjs continue;
2301 1.1 rjs }
2302 1.1 rjs /* Look for and remove if it is the right TSN. Since
2303 1.1 rjs * there is only ONE ECNE on the control queue at
2304 1.1 rjs * any one time we don't need to worry about more than
2305 1.1 rjs * one!
2306 1.1 rjs */
2307 1.1 rjs ecne = mtod(chk->data, struct sctp_ecne_chunk *);
2308 1.1 rjs if (compare_with_wrap(ntohl(cp->tsn), ntohl(ecne->tsn),
2309 1.1 rjs MAX_TSN) || (cp->tsn == ecne->tsn)) {
2310 1.1 rjs /* this covers this ECNE, we can remove it */
2311 1.1 rjs TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
2312 1.1 rjs sctp_next);
2313 1.1 rjs if (chk->data) {
2314 1.1 rjs sctp_m_freem(chk->data);
2315 1.1 rjs chk->data = NULL;
2316 1.1 rjs }
2317 1.1 rjs stcb->asoc.ctrl_queue_cnt--;
2318 1.1 rjs sctp_free_remote_addr(chk->whoTo);
2319 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2320 1.1 rjs sctppcbinfo.ipi_count_chunk--;
2321 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2322 1.1 rjs panic("Chunk count is negative");
2323 1.1 rjs }
2324 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
2325 1.1 rjs break;
2326 1.1 rjs }
2327 1.1 rjs }
2328 1.1 rjs }
2329 1.1 rjs
2330 1.1 rjs static void
2331 1.1 rjs sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp,
2332 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
2333 1.1 rjs {
2334 1.1 rjs struct sctp_association *asoc;
2335 1.1 rjs
2336 1.1 rjs #ifdef SCTP_DEBUG
2337 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
2338 1.1 rjs printf("sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
2339 1.1 rjs }
2340 1.1 rjs #endif
2341 1.1 rjs if (stcb == NULL)
2342 1.1 rjs return;
2343 1.1 rjs
2344 1.1 rjs asoc = &stcb->asoc;
2345 1.1 rjs /* process according to association state */
2346 1.1 rjs if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
2347 1.1 rjs /* unexpected SHUTDOWN-COMPLETE... so ignore... */
2348 1.1 rjs return;
2349 1.1 rjs }
2350 1.1 rjs /* notify upper layer protocol */
2351 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL);
2352 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2353 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2354 1.1 rjs stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
2355 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
2356 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_mbcnt = 0;
2357 1.1 rjs soisdisconnected(stcb->sctp_ep->sctp_socket);
2358 1.1 rjs }
2359 1.1 rjs /* are the queues empty? they should be */
2360 1.1 rjs if (!TAILQ_EMPTY(&asoc->send_queue) ||
2361 1.1 rjs !TAILQ_EMPTY(&asoc->sent_queue) ||
2362 1.1 rjs !TAILQ_EMPTY(&asoc->out_wheel)) {
2363 1.1 rjs sctp_report_all_outbound(stcb);
2364 1.1 rjs }
2365 1.1 rjs /* stop the timer */
2366 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net);
2367 1.1 rjs /* free the TCB */
2368 1.1 rjs sctp_free_assoc(stcb->sctp_ep, stcb);
2369 1.1 rjs return;
2370 1.1 rjs }
2371 1.1 rjs
2372 1.1 rjs static int
2373 1.1 rjs process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
2374 1.1 rjs struct sctp_nets *net, u_int8_t flg)
2375 1.1 rjs {
2376 1.1 rjs switch (desc->chunk_type) {
2377 1.1 rjs case SCTP_DATA:
2378 1.1 rjs /* find the tsn to resend (possibly */
2379 1.1 rjs {
2380 1.1 rjs u_int32_t tsn;
2381 1.1 rjs struct sctp_tmit_chunk *tp1;
2382 1.1 rjs tsn = ntohl(desc->tsn_ifany);
2383 1.1 rjs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2384 1.1 rjs while (tp1) {
2385 1.1 rjs if (tp1->rec.data.TSN_seq == tsn) {
2386 1.1 rjs /* found it */
2387 1.1 rjs break;
2388 1.1 rjs }
2389 1.1 rjs if (compare_with_wrap(tp1->rec.data.TSN_seq, tsn,
2390 1.1 rjs MAX_TSN)) {
2391 1.1 rjs /* not found */
2392 1.1 rjs tp1 = NULL;
2393 1.1 rjs break;
2394 1.1 rjs }
2395 1.1 rjs tp1 = TAILQ_NEXT(tp1, sctp_next);
2396 1.1 rjs }
2397 1.1 rjs if (tp1 == NULL) {
2398 1.1 rjs /* Do it the other way */
2399 1.1 rjs sctp_pegs[SCTP_PDRP_DNFND]++;
2400 1.1 rjs tp1 = TAILQ_FIRST(&stcb->asoc.sent_queue);
2401 1.1 rjs while (tp1) {
2402 1.1 rjs if (tp1->rec.data.TSN_seq == tsn) {
2403 1.1 rjs /* found it */
2404 1.1 rjs break;
2405 1.1 rjs }
2406 1.1 rjs tp1 = TAILQ_NEXT(tp1, sctp_next);
2407 1.1 rjs }
2408 1.1 rjs }
2409 1.1 rjs if (tp1 == NULL) {
2410 1.1 rjs sctp_pegs[SCTP_PDRP_TSNNF]++;
2411 1.1 rjs }
2412 1.1 rjs if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
2413 1.1 rjs u_int8_t *ddp;
2414 1.1 rjs if (((tp1->rec.data.state_flags & SCTP_WINDOW_PROBE) == SCTP_WINDOW_PROBE) &&
2415 1.1 rjs ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
2416 1.1 rjs sctp_pegs[SCTP_PDRP_DIWNP]++;
2417 1.1 rjs return (0);
2418 1.1 rjs }
2419 1.1 rjs if (stcb->asoc.peers_rwnd == 0 &&
2420 1.1 rjs (flg & SCTP_FROM_MIDDLE_BOX)) {
2421 1.1 rjs sctp_pegs[SCTP_PDRP_DIZRW]++;
2422 1.1 rjs return (0);
2423 1.1 rjs }
2424 1.1 rjs ddp = (u_int8_t *)(mtod(tp1->data, vaddr_t) +
2425 1.1 rjs sizeof(struct sctp_data_chunk));
2426 1.1 rjs {
2427 1.1 rjs unsigned int iii;
2428 1.1 rjs for (iii = 0; iii < sizeof(desc->data_bytes);
2429 1.1 rjs iii++) {
2430 1.1 rjs if (ddp[iii] != desc->data_bytes[iii]) {
2431 1.1 rjs sctp_pegs[SCTP_PDRP_BADD]++;
2432 1.1 rjs return (-1);
2433 1.1 rjs }
2434 1.1 rjs }
2435 1.1 rjs }
2436 1.1 rjs if (tp1->sent != SCTP_DATAGRAM_RESEND) {
2437 1.1 rjs stcb->asoc.sent_queue_retran_cnt++;
2438 1.1 rjs }
2439 1.1 rjs /* We zero out the nonce so resync not needed */
2440 1.1 rjs tp1->rec.data.ect_nonce = 0;
2441 1.1 rjs
2442 1.1 rjs if (tp1->do_rtt) {
2443 1.1 rjs /*
2444 1.1 rjs * this guy had a RTO calculation pending on it,
2445 1.1 rjs * cancel it
2446 1.1 rjs */
2447 1.1 rjs tp1->whoTo->rto_pending = 0;
2448 1.1 rjs tp1->do_rtt = 0;
2449 1.1 rjs }
2450 1.1 rjs sctp_pegs[SCTP_PDRP_MARK]++;
2451 1.1 rjs tp1->sent = SCTP_DATAGRAM_RESEND;
2452 1.1 rjs /*
2453 1.1 rjs * mark it as if we were doing a FR, since we
2454 1.1 rjs * will be getting gap ack reports behind the
2455 1.1 rjs * info from the router.
2456 1.1 rjs */
2457 1.1 rjs tp1->rec.data.doing_fast_retransmit = 1;
2458 1.1 rjs /*
2459 1.1 rjs * mark the tsn with what sequences can cause a new FR.
2460 1.1 rjs */
2461 1.1 rjs if (TAILQ_EMPTY(&stcb->asoc.send_queue) ) {
2462 1.1 rjs tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
2463 1.1 rjs } else {
2464 1.1 rjs tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.TSN_seq;
2465 1.1 rjs }
2466 1.1 rjs
2467 1.1 rjs /* restart the timer */
2468 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2469 1.1 rjs stcb, tp1->whoTo);
2470 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
2471 1.1 rjs stcb, tp1->whoTo);
2472 1.1 rjs
2473 1.1 rjs /* fix counts and things */
2474 1.1 rjs sctp_flight_size_decrease(tp1);
2475 1.1 rjs sctp_total_flight_decrease(stcb, tp1);
2476 1.1 rjs tp1->snd_count--;
2477 1.1 rjs }
2478 1.1 rjs {
2479 1.1 rjs /* audit code */
2480 1.1 rjs unsigned int audit;
2481 1.1 rjs audit = 0;
2482 1.1 rjs TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
2483 1.1 rjs if (tp1->sent == SCTP_DATAGRAM_RESEND)
2484 1.1 rjs audit++;
2485 1.1 rjs }
2486 1.1 rjs TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
2487 1.1 rjs sctp_next) {
2488 1.1 rjs if (tp1->sent == SCTP_DATAGRAM_RESEND)
2489 1.1 rjs audit++;
2490 1.1 rjs }
2491 1.1 rjs if (audit != stcb->asoc.sent_queue_retran_cnt) {
2492 1.1 rjs printf("**Local Audit finds cnt:%d asoc cnt:%d\n",
2493 1.1 rjs audit, stcb->asoc.sent_queue_retran_cnt);
2494 1.1 rjs #ifndef SCTP_AUDITING_ENABLED
2495 1.1 rjs stcb->asoc.sent_queue_retran_cnt = audit;
2496 1.1 rjs #endif
2497 1.1 rjs }
2498 1.1 rjs }
2499 1.1 rjs }
2500 1.1 rjs break;
2501 1.1 rjs case SCTP_ASCONF:
2502 1.1 rjs {
2503 1.1 rjs struct sctp_tmit_chunk *asconf;
2504 1.1 rjs TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
2505 1.1 rjs sctp_next) {
2506 1.1 rjs if (asconf->rec.chunk_id == SCTP_ASCONF) {
2507 1.1 rjs break;
2508 1.1 rjs }
2509 1.1 rjs }
2510 1.1 rjs if (asconf) {
2511 1.1 rjs if (asconf->sent != SCTP_DATAGRAM_RESEND)
2512 1.1 rjs stcb->asoc.sent_queue_retran_cnt++;
2513 1.1 rjs asconf->sent = SCTP_DATAGRAM_RESEND;
2514 1.1 rjs asconf->snd_count--;
2515 1.1 rjs }
2516 1.1 rjs }
2517 1.1 rjs break;
2518 1.1 rjs case SCTP_INITIATION:
2519 1.1 rjs /* resend the INIT */
2520 1.1 rjs stcb->asoc.dropped_special_cnt++;
2521 1.1 rjs if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
2522 1.1 rjs /*
2523 1.1 rjs * If we can get it in, in a few attempts we do this,
2524 1.1 rjs * otherwise we let the timer fire.
2525 1.1 rjs */
2526 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
2527 1.1 rjs stcb, net);
2528 1.1 rjs sctp_send_initiate(stcb->sctp_ep, stcb);
2529 1.1 rjs }
2530 1.1 rjs break;
2531 1.1 rjs case SCTP_SELECTIVE_ACK:
2532 1.1 rjs /* resend the sack */
2533 1.1 rjs sctp_send_sack(stcb);
2534 1.1 rjs break;
2535 1.1 rjs case SCTP_HEARTBEAT_REQUEST:
2536 1.1 rjs /* resend a demand HB */
2537 1.1 rjs sctp_send_hb(stcb, 1, net);
2538 1.1 rjs break;
2539 1.1 rjs case SCTP_SHUTDOWN:
2540 1.1 rjs #ifdef SCTP_DEBUG
2541 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2542 1.1 rjs printf("%s:%d sends a shutdown\n",
2543 1.1 rjs __FILE__,
2544 1.1 rjs __LINE__
2545 1.1 rjs );
2546 1.1 rjs }
2547 1.1 rjs #endif
2548 1.1 rjs sctp_send_shutdown(stcb, net);
2549 1.1 rjs break;
2550 1.1 rjs case SCTP_SHUTDOWN_ACK:
2551 1.1 rjs sctp_send_shutdown_ack(stcb, net);
2552 1.1 rjs break;
2553 1.1 rjs case SCTP_COOKIE_ECHO:
2554 1.1 rjs {
2555 1.1 rjs struct sctp_tmit_chunk *cookie;
2556 1.1 rjs cookie = NULL;
2557 1.1 rjs TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
2558 1.1 rjs sctp_next) {
2559 1.1 rjs if (cookie->rec.chunk_id == SCTP_COOKIE_ECHO) {
2560 1.1 rjs break;
2561 1.1 rjs }
2562 1.1 rjs }
2563 1.1 rjs if (cookie) {
2564 1.1 rjs if (cookie->sent != SCTP_DATAGRAM_RESEND)
2565 1.1 rjs stcb->asoc.sent_queue_retran_cnt++;
2566 1.1 rjs cookie->sent = SCTP_DATAGRAM_RESEND;
2567 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep, stcb, net);
2568 1.1 rjs }
2569 1.1 rjs }
2570 1.1 rjs break;
2571 1.1 rjs case SCTP_COOKIE_ACK:
2572 1.1 rjs sctp_send_cookie_ack(stcb);
2573 1.1 rjs break;
2574 1.1 rjs case SCTP_ASCONF_ACK:
2575 1.1 rjs /* resend last asconf ack */
2576 1.1 rjs sctp_send_asconf_ack(stcb, 1);
2577 1.1 rjs break;
2578 1.1 rjs case SCTP_FORWARD_CUM_TSN:
2579 1.1 rjs send_forward_tsn(stcb, &stcb->asoc);
2580 1.1 rjs break;
2581 1.1 rjs /* can't do anything with these */
2582 1.1 rjs case SCTP_PACKET_DROPPED:
2583 1.1 rjs case SCTP_INITIATION_ACK: /* this should not happen */
2584 1.1 rjs case SCTP_HEARTBEAT_ACK:
2585 1.1 rjs case SCTP_ABORT_ASSOCIATION:
2586 1.1 rjs case SCTP_OPERATION_ERROR:
2587 1.1 rjs case SCTP_SHUTDOWN_COMPLETE:
2588 1.1 rjs case SCTP_ECN_ECHO:
2589 1.1 rjs case SCTP_ECN_CWR:
2590 1.1 rjs default:
2591 1.1 rjs break;
2592 1.1 rjs }
2593 1.1 rjs return (0);
2594 1.1 rjs }
2595 1.1 rjs
2596 1.1 rjs static void
2597 1.1 rjs sctp_reset_in_stream(struct sctp_tcb *stcb,
2598 1.1 rjs struct sctp_stream_reset_response *resp, int number_entries)
2599 1.1 rjs {
2600 1.1 rjs int i;
2601 1.1 rjs uint16_t *list, temp;
2602 1.1 rjs
2603 1.1 rjs /* We set things to 0xffff since this is the last delivered
2604 1.1 rjs * sequence and we will be sending in 0 after the reset.
2605 1.1 rjs */
2606 1.1 rjs
2607 1.1 rjs if (resp->reset_flags & SCTP_RESET_PERFORMED) {
2608 1.1 rjs if (number_entries) {
2609 1.1 rjs list = resp->list_of_streams;
2610 1.1 rjs for (i = 0; i < number_entries; i++) {
2611 1.1 rjs temp = ntohs(list[i]);
2612 1.1 rjs list[i] = temp;
2613 1.1 rjs if (list[i] >= stcb->asoc.streamincnt) {
2614 1.1 rjs printf("Invalid stream in-stream reset %d\n", list[i]);
2615 1.1 rjs continue;
2616 1.1 rjs }
2617 1.1 rjs stcb->asoc.strmin[(list[i])].last_sequence_delivered = 0xffff;
2618 1.1 rjs }
2619 1.1 rjs } else {
2620 1.1 rjs list = NULL;
2621 1.1 rjs for (i = 0; i < stcb->asoc.streamincnt; i++) {
2622 1.1 rjs stcb->asoc.strmin[i].last_sequence_delivered = 0xffff;
2623 1.1 rjs }
2624 1.1 rjs }
2625 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list);
2626 1.1 rjs }
2627 1.1 rjs }
2628 1.1 rjs
2629 1.1 rjs static void
2630 1.1 rjs sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
2631 1.1 rjs {
2632 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
2633 1.1 rjs struct sctp_association *asoc;
2634 1.1 rjs
2635 1.1 rjs asoc = &stcb->asoc;
2636 1.1 rjs
2637 1.1 rjs for (chk = TAILQ_FIRST(&asoc->control_send_queue);
2638 1.1 rjs chk; chk = nchk) {
2639 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
2640 1.1 rjs if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
2641 1.1 rjs struct sctp_stream_reset_req *strreq;
2642 1.1 rjs strreq = mtod(chk->data, struct sctp_stream_reset_req *);
2643 1.1 rjs if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
2644 1.1 rjs /* we only clean up the request */
2645 1.1 rjs continue;
2646 1.1 rjs } else if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
2647 1.1 rjs printf("TSNH, an unknown stream reset request is in queue %x\n",
2648 1.1 rjs (u_int)ntohs(strreq->sr_req.ph.param_type));
2649 1.1 rjs continue;
2650 1.1 rjs }
2651 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
2652 1.1 rjs TAILQ_REMOVE(&asoc->control_send_queue,
2653 1.1 rjs chk,
2654 1.1 rjs sctp_next);
2655 1.1 rjs if (chk->data) {
2656 1.1 rjs sctp_m_freem(chk->data);
2657 1.1 rjs chk->data = NULL;
2658 1.1 rjs }
2659 1.1 rjs asoc->ctrl_queue_cnt--;
2660 1.1 rjs sctp_free_remote_addr(chk->whoTo);
2661 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
2662 1.1 rjs sctppcbinfo.ipi_count_chunk--;
2663 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
2664 1.1 rjs panic("Chunk count is negative");
2665 1.1 rjs }
2666 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
2667 1.1 rjs /* we can only have one of these so we break */
2668 1.1 rjs break;
2669 1.1 rjs }
2670 1.1 rjs }
2671 1.1 rjs }
2672 1.1 rjs
2673 1.1 rjs
2674 1.1 rjs void
2675 1.1 rjs sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
2676 1.1 rjs struct sctp_stream_reset_response *resp)
2677 1.1 rjs {
2678 1.1 rjs uint32_t seq, tsn;
2679 1.1 rjs int number_entries, param_length;
2680 1.1 rjs
2681 1.1 rjs param_length = ntohs(resp->ph.param_length);
2682 1.1 rjs seq = ntohl(resp->reset_req_seq_resp);
2683 1.1 rjs if (seq == stcb->asoc.str_reset_seq_out) {
2684 1.1 rjs sctp_clean_up_stream_reset(stcb);
2685 1.1 rjs stcb->asoc.str_reset_seq_out++;
2686 1.1 rjs stcb->asoc.stream_reset_outstanding = 0;
2687 1.1 rjs tsn = ntohl(resp->reset_at_tsn);
2688 1.1 rjs number_entries = (param_length - sizeof(struct sctp_stream_reset_response))/sizeof(uint16_t);
2689 1.1 rjs tsn--;
2690 1.1 rjs if ((tsn == stcb->asoc.cumulative_tsn) ||
2691 1.1 rjs (compare_with_wrap(stcb->asoc.cumulative_tsn, tsn, MAX_TSN))) {
2692 1.1 rjs /* no problem we are good to go */
2693 1.1 rjs sctp_reset_in_stream(stcb, resp, number_entries);
2694 1.1 rjs } else {
2695 1.1 rjs /* So, we have a stream reset but there
2696 1.1 rjs * is pending data. We need to copy
2697 1.1 rjs * out the stream_reset and then queue
2698 1.1 rjs * any data = or > resp->reset_at_tsn
2699 1.1 rjs */
2700 1.1 rjs if (stcb->asoc.pending_reply != NULL) {
2701 1.1 rjs /* FIX ME FIX ME
2702 1.1 rjs * This IS WRONG. We need
2703 1.1 rjs * to queue each of these up
2704 1.1 rjs * and only release the chunks
2705 1.1 rjs * for each reset that the cum-ack
2706 1.1 rjs * goes by. This is a short cut.
2707 1.1 rjs */
2708 1.1 rjs free(stcb->asoc.pending_reply, M_PCB);
2709 1.1 rjs }
2710 1.1 rjs stcb->asoc.pending_reply = malloc(param_length,
2711 1.1 rjs M_PCB, M_NOWAIT);
2712 1.1 rjs memcpy(stcb->asoc.pending_reply, resp, param_length);
2713 1.1 rjs }
2714 1.1 rjs
2715 1.1 rjs } else {
2716 1.1 rjs /* duplicate */
2717 1.1 rjs #ifdef SCTP_DEBUG
2718 1.1 rjs printf("Duplicate old stream reset resp next:%x this one:%x\n",
2719 1.1 rjs stcb->asoc.str_reset_seq_out, seq);
2720 1.1 rjs #endif
2721 1.1 rjs }
2722 1.1 rjs }
2723 1.1 rjs
2724 1.1 rjs
2725 1.1 rjs static void
2726 1.1 rjs sctp_handle_stream_reset(struct sctp_tcb *stcb, struct sctp_stream_reset_req *sr_req)
2727 1.1 rjs {
2728 1.1 rjs int chk_length, param_len;
2729 1.1 rjs struct sctp_paramhdr *ph;
2730 1.1 rjs /* now it may be a reset or a reset-response */
2731 1.1 rjs struct sctp_stream_reset_request *req;
2732 1.1 rjs struct sctp_stream_reset_response *resp;
2733 1.1 rjs chk_length = ntohs(sr_req->ch.chunk_length);
2734 1.1 rjs
2735 1.1 rjs ph = (struct sctp_paramhdr *)&sr_req->sr_req;
2736 1.1 rjs while ((size_t)chk_length >= sizeof(struct sctp_stream_reset_request)) {
2737 1.1 rjs param_len = ntohs(ph->param_length);
2738 1.1 rjs if (ntohs(ph->param_type) == SCTP_STR_RESET_REQUEST) {
2739 1.1 rjs /* this will send the ACK and do the reset if needed */
2740 1.1 rjs req = (struct sctp_stream_reset_request *)ph;
2741 1.1 rjs sctp_send_str_reset_ack(stcb, req);
2742 1.1 rjs } else if (ntohs(ph->param_type) == SCTP_STR_RESET_RESPONSE) {
2743 1.1 rjs /* Now here is a tricky one. We reset our receive side
2744 1.1 rjs * of the streams. But what happens if the peers
2745 1.1 rjs * next sending TSN is NOT equal to 1 minus our cumack?
2746 1.1 rjs * And if his cumack is not equal to our next one out - 1
2747 1.1 rjs * we have another problem if this is receprical.
2748 1.1 rjs */
2749 1.1 rjs resp = (struct sctp_stream_reset_response *)ph;
2750 1.1 rjs sctp_handle_stream_reset_response(stcb, resp);
2751 1.1 rjs }
2752 1.1 rjs ph = (struct sctp_paramhdr *)((vaddr_t)ph + SCTP_SIZE32(param_len));
2753 1.1 rjs chk_length -= SCTP_SIZE32(param_len);
2754 1.1 rjs }
2755 1.1 rjs }
2756 1.1 rjs
2757 1.1 rjs /*
2758 1.1 rjs * Handle a router or endpoints report of a packet loss, there
2759 1.1 rjs * are two ways to handle this, either we get the whole packet
2760 1.1 rjs * and must disect it ourselves (possibly with truncation and
2761 1.1 rjs * or corruption) or it is a summary from a middle box that did
2762 1.1 rjs * the disectting for us.
2763 1.1 rjs */
2764 1.1 rjs static void
2765 1.1 rjs sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
2766 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets *net)
2767 1.1 rjs {
2768 1.1 rjs u_int32_t bottle_bw, on_queue;
2769 1.1 rjs u_int16_t trunc_len;
2770 1.1 rjs unsigned int chlen;
2771 1.1 rjs unsigned int at;
2772 1.1 rjs struct sctp_chunk_desc desc;
2773 1.1 rjs struct sctp_chunkhdr *ch;
2774 1.1 rjs
2775 1.1 rjs chlen = ntohs(cp->ch.chunk_length);
2776 1.1 rjs chlen -= sizeof(struct sctp_pktdrop_chunk);
2777 1.1 rjs /* XXX possible chlen underflow */
2778 1.1 rjs if (chlen == 0) {
2779 1.1 rjs ch = NULL;
2780 1.1 rjs if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
2781 1.1 rjs sctp_pegs[SCTP_PDRP_BWRPT]++;
2782 1.1 rjs } else {
2783 1.1 rjs ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
2784 1.1 rjs chlen -= sizeof(struct sctphdr);
2785 1.1 rjs /* XXX possible chlen underflow */
2786 1.1 rjs memset(&desc, 0, sizeof(desc));
2787 1.1 rjs }
2788 1.1 rjs
2789 1.1 rjs /* first update a rwnd possibly */
2790 1.1 rjs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
2791 1.1 rjs /* From a peer, we get a rwnd report */
2792 1.1 rjs u_int32_t a_rwnd;
2793 1.1 rjs
2794 1.1 rjs sctp_pegs[SCTP_PDRP_FEHOS]++;
2795 1.1 rjs
2796 1.1 rjs bottle_bw = ntohl(cp->bottle_bw);
2797 1.1 rjs on_queue = ntohl(cp->current_onq);
2798 1.1 rjs if (bottle_bw && on_queue) {
2799 1.1 rjs /* a rwnd report is in here */
2800 1.1 rjs if (bottle_bw > on_queue)
2801 1.1 rjs a_rwnd = bottle_bw - on_queue;
2802 1.1 rjs else
2803 1.1 rjs a_rwnd = 0;
2804 1.1 rjs
2805 1.1 rjs if (a_rwnd <= 0)
2806 1.1 rjs stcb->asoc.peers_rwnd = 0;
2807 1.1 rjs else {
2808 1.1 rjs if (a_rwnd > stcb->asoc.total_flight) {
2809 1.1 rjs stcb->asoc.peers_rwnd =
2810 1.1 rjs a_rwnd - stcb->asoc.total_flight;
2811 1.1 rjs } else {
2812 1.1 rjs stcb->asoc.peers_rwnd = 0;
2813 1.1 rjs }
2814 1.1 rjs if (stcb->asoc.peers_rwnd <
2815 1.1 rjs stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
2816 1.1 rjs /* SWS sender side engages */
2817 1.1 rjs stcb->asoc.peers_rwnd = 0;
2818 1.1 rjs }
2819 1.1 rjs }
2820 1.1 rjs }
2821 1.1 rjs } else {
2822 1.1 rjs sctp_pegs[SCTP_PDRP_FMBOX]++;
2823 1.1 rjs }
2824 1.1 rjs trunc_len = (u_int16_t)ntohs(cp->trunc_len);
2825 1.1 rjs /* now the chunks themselves */
2826 1.1 rjs while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
2827 1.1 rjs desc.chunk_type = ch->chunk_type;
2828 1.1 rjs /* get amount we need to move */
2829 1.1 rjs at = ntohs(ch->chunk_length);
2830 1.1 rjs if (at < sizeof(struct sctp_chunkhdr)) {
2831 1.1 rjs /* corrupt chunk, maybe at the end? */
2832 1.1 rjs sctp_pegs[SCTP_PDRP_CRUPT]++;
2833 1.1 rjs break;
2834 1.1 rjs }
2835 1.1 rjs if (trunc_len == 0) {
2836 1.1 rjs /* we are supposed to have all of it */
2837 1.1 rjs if (at > chlen) {
2838 1.1 rjs /* corrupt skip it */
2839 1.1 rjs sctp_pegs[SCTP_PDRP_CRUPT]++;
2840 1.1 rjs break;
2841 1.1 rjs }
2842 1.1 rjs } else {
2843 1.1 rjs /* is there enough of it left ? */
2844 1.1 rjs if (desc.chunk_type == SCTP_DATA) {
2845 1.1 rjs if (chlen < (sizeof(struct sctp_data_chunk) +
2846 1.1 rjs sizeof(desc.data_bytes))) {
2847 1.1 rjs break;
2848 1.1 rjs }
2849 1.1 rjs } else {
2850 1.1 rjs if (chlen < sizeof(struct sctp_chunkhdr)) {
2851 1.1 rjs break;
2852 1.1 rjs }
2853 1.1 rjs }
2854 1.1 rjs }
2855 1.1 rjs if (desc.chunk_type == SCTP_DATA) {
2856 1.1 rjs /* can we get out the tsn? */
2857 1.1 rjs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2858 1.1 rjs sctp_pegs[SCTP_PDRP_MB_DA]++;
2859 1.1 rjs
2860 1.1 rjs if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(u_int32_t)) ) {
2861 1.1 rjs /* yep */
2862 1.1 rjs struct sctp_data_chunk *dcp;
2863 1.1 rjs u_int8_t *ddp;
2864 1.1 rjs unsigned int iii;
2865 1.1 rjs dcp = (struct sctp_data_chunk *)ch;
2866 1.1 rjs ddp = (u_int8_t *)(dcp + 1);
2867 1.1 rjs for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
2868 1.1 rjs desc.data_bytes[iii] = ddp[iii];
2869 1.1 rjs }
2870 1.1 rjs desc.tsn_ifany = dcp->dp.tsn;
2871 1.1 rjs } else {
2872 1.1 rjs /* nope we are done. */
2873 1.1 rjs sctp_pegs[SCTP_PDRP_NEDAT]++;
2874 1.1 rjs break;
2875 1.1 rjs }
2876 1.1 rjs } else {
2877 1.1 rjs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
2878 1.1 rjs sctp_pegs[SCTP_PDRP_MB_CT]++;
2879 1.1 rjs }
2880 1.1 rjs
2881 1.1 rjs if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
2882 1.1 rjs sctp_pegs[SCTP_PDRP_PDBRK]++;
2883 1.1 rjs break;
2884 1.1 rjs }
2885 1.1 rjs if (SCTP_SIZE32(at) > chlen) {
2886 1.1 rjs break;
2887 1.1 rjs }
2888 1.1 rjs chlen -= SCTP_SIZE32(at);
2889 1.1 rjs if (chlen < sizeof(struct sctp_chunkhdr)) {
2890 1.1 rjs /* done, none left */
2891 1.1 rjs break;
2892 1.1 rjs }
2893 1.1 rjs ch = (struct sctp_chunkhdr *)((vaddr_t)ch + SCTP_SIZE32(at));
2894 1.1 rjs }
2895 1.1 rjs
2896 1.1 rjs /* now middle boxes in sat networks get a cwnd bump */
2897 1.1 rjs if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
2898 1.1 rjs (stcb->asoc.sat_t3_loss_recovery == 0) &&
2899 1.1 rjs (stcb->asoc.sat_network)) {
2900 1.1 rjs /*
2901 1.1 rjs * This is debateable but for sat networks it makes sense
2902 1.1 rjs * Note if a T3 timer has went off, we will prohibit any
2903 1.1 rjs * changes to cwnd until we exit the t3 loss recovery.
2904 1.1 rjs */
2905 1.1 rjs u_int32_t bw_avail;
2906 1.1 rjs int rtt, incr;
2907 1.1 rjs #ifdef SCTP_CWND_LOGGING
2908 1.1 rjs int old_cwnd=net->cwnd;
2909 1.1 rjs #endif
2910 1.1 rjs /* need real RTT for this calc */
2911 1.1 rjs rtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2912 1.1 rjs /* get bottle neck bw */
2913 1.1 rjs bottle_bw = ntohl(cp->bottle_bw);
2914 1.1 rjs /* and whats on queue */
2915 1.1 rjs on_queue = ntohl(cp->current_onq);
2916 1.1 rjs /*
2917 1.1 rjs * adjust the on-queue if our flight is more it could be
2918 1.1 rjs * that the router has not yet gotten data "in-flight" to it
2919 1.1 rjs */
2920 1.1 rjs if (on_queue < net->flight_size)
2921 1.1 rjs on_queue = net->flight_size;
2922 1.1 rjs
2923 1.1 rjs /* calculate the available space */
2924 1.1 rjs bw_avail = (bottle_bw*rtt)/1000;
2925 1.1 rjs if (bw_avail > bottle_bw) {
2926 1.1 rjs /*
2927 1.1 rjs * Cap the growth to no more than the bottle neck.
2928 1.1 rjs * This can happen as RTT slides up due to queues.
2929 1.1 rjs * It also means if you have more than a 1 second
2930 1.1 rjs * RTT with a empty queue you will be limited to
2931 1.1 rjs * the bottle_bw per second no matter if
2932 1.1 rjs * other points have 1/2 the RTT and you could
2933 1.1 rjs * get more out...
2934 1.1 rjs */
2935 1.1 rjs bw_avail = bottle_bw;
2936 1.1 rjs }
2937 1.1 rjs
2938 1.1 rjs if (on_queue > bw_avail) {
2939 1.1 rjs /*
2940 1.1 rjs * No room for anything else don't allow anything
2941 1.1 rjs * else to be "added to the fire".
2942 1.1 rjs */
2943 1.1 rjs int seg_inflight, seg_onqueue, my_portion;
2944 1.1 rjs net->partial_bytes_acked = 0;
2945 1.1 rjs
2946 1.1 rjs /* how much are we over queue size? */
2947 1.1 rjs incr = on_queue - bw_avail;
2948 1.1 rjs if (stcb->asoc.seen_a_sack_this_pkt) {
2949 1.1 rjs /* undo any cwnd adjustment that
2950 1.1 rjs * the sack might have made
2951 1.1 rjs */
2952 1.1 rjs net->cwnd = net->prev_cwnd;
2953 1.1 rjs }
2954 1.1 rjs
2955 1.1 rjs /* Now how much of that is mine? */
2956 1.1 rjs seg_inflight = net->flight_size / net->mtu;
2957 1.1 rjs seg_onqueue = on_queue / net->mtu;
2958 1.1 rjs my_portion = (incr * seg_inflight)/seg_onqueue;
2959 1.1 rjs
2960 1.1 rjs /* Have I made an adjustment already */
2961 1.1 rjs if (net->cwnd > net->flight_size) {
2962 1.1 rjs /* for this flight I made an adjustment
2963 1.1 rjs * we need to decrease the portion by a share
2964 1.1 rjs * our previous adjustment.
2965 1.1 rjs */
2966 1.1 rjs int diff_adj;
2967 1.1 rjs diff_adj = net->cwnd - net->flight_size;
2968 1.1 rjs if (diff_adj > my_portion)
2969 1.1 rjs my_portion = 0;
2970 1.1 rjs else
2971 1.1 rjs my_portion -= diff_adj;
2972 1.1 rjs }
2973 1.1 rjs
2974 1.1 rjs /* back down to the previous cwnd (assume
2975 1.1 rjs * we have had a sack before this packet). minus
2976 1.1 rjs * what ever portion of the overage is my fault.
2977 1.1 rjs */
2978 1.1 rjs net->cwnd -= my_portion;
2979 1.1 rjs
2980 1.1 rjs /* we will NOT back down more than 1 MTU */
2981 1.1 rjs if (net->cwnd <= net->mtu) {
2982 1.1 rjs net->cwnd = net->mtu;
2983 1.1 rjs }
2984 1.1 rjs /* force into CA */
2985 1.1 rjs net->ssthresh = net->cwnd - 1;
2986 1.1 rjs } else {
2987 1.1 rjs /*
2988 1.1 rjs * Take 1/4 of the space left or
2989 1.1 rjs * max burst up .. whichever is less.
2990 1.1 rjs */
2991 1.10 riastrad incr = uimin((bw_avail - on_queue) >> 2,
2992 1.1 rjs (int)stcb->asoc.max_burst * (int)net->mtu);
2993 1.1 rjs net->cwnd += incr;
2994 1.1 rjs }
2995 1.1 rjs if (net->cwnd > bw_avail) {
2996 1.1 rjs /* We can't exceed the pipe size */
2997 1.1 rjs net->cwnd = bw_avail;
2998 1.1 rjs }
2999 1.1 rjs if (net->cwnd < net->mtu) {
3000 1.1 rjs /* We always have 1 MTU */
3001 1.1 rjs net->cwnd = net->mtu;
3002 1.1 rjs }
3003 1.1 rjs #ifdef SCTP_CWND_LOGGING
3004 1.1 rjs if (net->cwnd - old_cwnd != 0) {
3005 1.1 rjs /* log only changes */
3006 1.1 rjs sctp_log_cwnd(net, (net->cwnd - old_cwnd),
3007 1.1 rjs SCTP_CWND_LOG_FROM_SAT);
3008 1.1 rjs }
3009 1.1 rjs #endif
3010 1.1 rjs }
3011 1.1 rjs }
3012 1.1 rjs
3013 1.1 rjs extern int sctp_strict_init;
3014 1.1 rjs
3015 1.1 rjs /*
3016 1.1 rjs * handles all control chunks in a packet
3017 1.1 rjs * inputs:
3018 1.1 rjs * - m: mbuf chain, assumed to still contain IP/SCTP header
3019 1.1 rjs * - stcb: is the tcb found for this packet
3020 1.1 rjs * - offset: offset into the mbuf chain to first chunkhdr
3021 1.1 rjs * - length: is the length of the complete packet
3022 1.1 rjs * outputs:
3023 1.1 rjs * - length: modified to remaining length after control processing
3024 1.1 rjs * - netp: modified to new sctp_nets after cookie-echo processing
3025 1.1 rjs * - return NULL to discard the packet (ie. no asoc, bad packet,...)
3026 1.1 rjs * otherwise return the tcb for this packet
3027 1.1 rjs */
3028 1.1 rjs static struct sctp_tcb *
3029 1.1 rjs sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
3030 1.1 rjs struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
3031 1.1 rjs struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen)
3032 1.1 rjs {
3033 1.1 rjs struct sctp_association *asoc;
3034 1.1 rjs u_int32_t vtag_in;
3035 1.1 rjs int num_chunks = 0; /* number of control chunks processed */
3036 1.1 rjs int chk_length;
3037 1.1 rjs int ret;
3038 1.1 rjs
3039 1.1 rjs /*
3040 1.1 rjs * How big should this be, and should it be alloc'd?
3041 1.1 rjs * Lets try the d-mtu-ceiling for now (2k) and that should
3042 1.1 rjs * hopefully work ... until we get into jumbo grams and such..
3043 1.1 rjs */
3044 1.1 rjs u_int8_t chunk_buf[DEFAULT_CHUNK_BUFFER];
3045 1.1 rjs struct sctp_tcb *locked_tcb = stcb;
3046 1.1 rjs
3047 1.1 rjs #ifdef SCTP_DEBUG
3048 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3049 1.1 rjs printf("sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
3050 1.1 rjs iphlen, *offset, length, stcb);
3051 1.1 rjs }
3052 1.1 rjs #endif /* SCTP_DEBUG */
3053 1.1 rjs
3054 1.1 rjs /* validate chunk header length... */
3055 1.1 rjs if (ntohs(ch->chunk_length) < sizeof(*ch)) {
3056 1.1 rjs return (NULL);
3057 1.1 rjs }
3058 1.1 rjs
3059 1.1 rjs /*
3060 1.1 rjs * validate the verification tag
3061 1.1 rjs */
3062 1.1 rjs #ifdef SCTP_DEBUG
3063 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3064 1.1 rjs printf("sctp_process_control: validating vtags\n");
3065 1.1 rjs }
3066 1.1 rjs #endif /* SCTP_DEBUG */
3067 1.1 rjs vtag_in = ntohl(sh->v_tag);
3068 1.1 rjs if (ch->chunk_type == SCTP_INITIATION) {
3069 1.1 rjs if (vtag_in != 0) {
3070 1.1 rjs /* protocol error- silently discard... */
3071 1.1 rjs #ifdef SCTP_DEBUG
3072 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3073 1.1 rjs printf("sctp_process_control: INIT with vtag != 0\n");
3074 1.1 rjs }
3075 1.1 rjs #endif /* SCTP_DEBUG */
3076 1.1 rjs sctp_pegs[SCTP_BAD_VTAGS]++;
3077 1.1 rjs if (locked_tcb) {
3078 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3079 1.1 rjs }
3080 1.1 rjs return (NULL);
3081 1.1 rjs }
3082 1.1 rjs } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
3083 1.1 rjs /*
3084 1.1 rjs * first check if it's an ASCONF with an unknown src addr
3085 1.1 rjs * we need to look inside to find the association
3086 1.1 rjs */
3087 1.1 rjs if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
3088 1.1 rjs stcb = sctp_findassociation_ep_asconf(m, iphlen,
3089 1.1 rjs *offset, sh, &inp, netp);
3090 1.1 rjs }
3091 1.1 rjs if (stcb == NULL) {
3092 1.1 rjs /* no association, so it's out of the blue... */
3093 1.1 rjs sctp_handle_ootb(m, iphlen, *offset, sh, inp, NULL);
3094 1.1 rjs #ifdef SCTP_DEBUG
3095 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3096 1.1 rjs printf("sctp_process_control: handling OOTB packet, chunk type=%xh\n",
3097 1.1 rjs ch->chunk_type);
3098 1.1 rjs }
3099 1.1 rjs #endif /* SCTP_DEBUG */
3100 1.1 rjs *offset = length;
3101 1.1 rjs if (locked_tcb) {
3102 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3103 1.1 rjs }
3104 1.1 rjs return (NULL);
3105 1.1 rjs }
3106 1.1 rjs asoc = &stcb->asoc;
3107 1.1 rjs /* ABORT and SHUTDOWN can use either v_tag... */
3108 1.1 rjs if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
3109 1.1 rjs (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
3110 1.1 rjs (ch->chunk_type == SCTP_PACKET_DROPPED)) {
3111 1.1 rjs if ((vtag_in == asoc->my_vtag) ||
3112 1.1 rjs ((ch->chunk_flags & SCTP_HAD_NO_TCB) &&
3113 1.1 rjs (vtag_in == asoc->peer_vtag))) {
3114 1.1 rjs /* this is valid */
3115 1.1 rjs } else {
3116 1.1 rjs /* drop this packet... */
3117 1.1 rjs sctp_pegs[SCTP_BAD_VTAGS]++;
3118 1.1 rjs if (locked_tcb) {
3119 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3120 1.1 rjs }
3121 1.1 rjs return (NULL);
3122 1.1 rjs }
3123 1.1 rjs } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
3124 1.1 rjs if (vtag_in != asoc->my_vtag) {
3125 1.1 rjs /*
3126 1.1 rjs * this could be a stale SHUTDOWN-ACK or the
3127 1.1 rjs * peer never got the SHUTDOWN-COMPLETE and
3128 1.1 rjs * is still hung; we have started a new asoc
3129 1.1 rjs * but it won't complete until the shutdown is
3130 1.1 rjs * completed
3131 1.1 rjs */
3132 1.1 rjs if (locked_tcb) {
3133 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3134 1.1 rjs }
3135 1.1 rjs sctp_handle_ootb(m, iphlen, *offset, sh, inp,
3136 1.1 rjs NULL);
3137 1.1 rjs return (NULL);
3138 1.1 rjs }
3139 1.1 rjs } else {
3140 1.1 rjs /* for all other chunks, vtag must match */
3141 1.1 rjs
3142 1.1 rjs if (vtag_in != asoc->my_vtag) {
3143 1.1 rjs /* invalid vtag... */
3144 1.1 rjs #ifdef SCTP_DEBUG
3145 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3146 1.1 rjs printf("invalid vtag: %xh, expect %xh\n", vtag_in, asoc->my_vtag);
3147 1.1 rjs }
3148 1.1 rjs #endif /* SCTP_DEBUG */
3149 1.1 rjs sctp_pegs[SCTP_BAD_VTAGS]++;
3150 1.1 rjs if (locked_tcb) {
3151 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3152 1.1 rjs }
3153 1.1 rjs *offset = length;
3154 1.1 rjs return (NULL);
3155 1.1 rjs }
3156 1.1 rjs }
3157 1.1 rjs } /* end if !SCTP_COOKIE_ECHO */
3158 1.1 rjs
3159 1.1 rjs #ifdef SCTP_DEBUG
3160 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3161 1.1 rjs printf("sctp_process_control: vtags ok, processing ctrl chunks\n");
3162 1.1 rjs }
3163 1.1 rjs #endif /* SCTP_DEBUG */
3164 1.1 rjs
3165 1.1 rjs /*
3166 1.1 rjs * process all control chunks...
3167 1.1 rjs */
3168 1.1 rjs if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
3169 1.1 rjs (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
3170 1.1 rjs (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3171 1.1 rjs /* implied cookie-ack.. we must have lost the ack */
3172 1.1 rjs stcb->asoc.overall_error_count = 0;
3173 1.1 rjs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
3174 1.1 rjs }
3175 1.1 rjs
3176 1.1 rjs while (IS_SCTP_CONTROL(ch)) {
3177 1.1 rjs /* validate chunk length */
3178 1.1 rjs chk_length = ntohs(ch->chunk_length);
3179 1.1 rjs #ifdef SCTP_DEBUG
3180 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT2) {
3181 1.1 rjs printf("sctp_process_control: processing a chunk type=%u, len=%u\n", ch->chunk_type, chk_length);
3182 1.1 rjs }
3183 1.1 rjs #endif /* SCTP_DEBUG */
3184 1.1 rjs if ((size_t)chk_length < sizeof(*ch) ||
3185 1.1 rjs (*offset + chk_length) > length) {
3186 1.1 rjs #ifdef SCTP_DEBUG
3187 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3188 1.1 rjs printf("sctp_process_control: chunk length invalid! *offset:%u, chk_length:%u > length:%u\n",
3189 1.6 rjs *offset, chk_length, length);
3190 1.1 rjs }
3191 1.1 rjs #endif /* SCTP_DEBUG */
3192 1.1 rjs *offset = length;
3193 1.1 rjs if (locked_tcb) {
3194 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3195 1.1 rjs }
3196 1.1 rjs return (NULL);
3197 1.1 rjs }
3198 1.1 rjs
3199 1.1 rjs /*
3200 1.1 rjs * INIT-ACK only gets the init ack "header" portion only
3201 1.1 rjs * because we don't have to process the peer's COOKIE.
3202 1.1 rjs * All others get a complete chunk.
3203 1.1 rjs */
3204 1.1 rjs if (ch->chunk_type == SCTP_INITIATION_ACK) {
3205 1.1 rjs /* get an init-ack chunk */
3206 1.1 rjs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3207 1.1 rjs sizeof(struct sctp_init_ack), chunk_buf);
3208 1.1 rjs if (ch == NULL) {
3209 1.1 rjs *offset = length;
3210 1.1 rjs if (locked_tcb) {
3211 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3212 1.1 rjs }
3213 1.1 rjs return (NULL);
3214 1.1 rjs }
3215 1.1 rjs } else {
3216 1.1 rjs /* get a complete chunk... */
3217 1.1 rjs if ((size_t)chk_length > sizeof(chunk_buf)) {
3218 1.1 rjs struct mbuf *oper;
3219 1.1 rjs struct sctp_paramhdr *phdr;
3220 1.1 rjs oper = NULL;
3221 1.1 rjs MGETHDR(oper, M_DONTWAIT, MT_HEADER);
3222 1.1 rjs if (oper) {
3223 1.1 rjs /* pre-reserve some space */
3224 1.1 rjs oper->m_data +=
3225 1.1 rjs sizeof(struct sctp_chunkhdr);
3226 1.1 rjs phdr =
3227 1.1 rjs mtod(oper, struct sctp_paramhdr *);
3228 1.1 rjs phdr->param_type =
3229 1.1 rjs htons(SCTP_CAUSE_OUT_OF_RESC);
3230 1.1 rjs phdr->param_length =
3231 1.1 rjs htons(sizeof(struct sctp_paramhdr));
3232 1.1 rjs sctp_queue_op_err(stcb, oper);
3233 1.1 rjs }
3234 1.1 rjs if (locked_tcb) {
3235 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3236 1.1 rjs }
3237 1.1 rjs return (NULL);
3238 1.1 rjs }
3239 1.1 rjs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3240 1.1 rjs chk_length, chunk_buf);
3241 1.1 rjs if (ch == NULL) {
3242 1.1 rjs printf("sctp_process_control: Can't get the all data....\n");
3243 1.1 rjs *offset = length;
3244 1.1 rjs if (locked_tcb) {
3245 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3246 1.1 rjs }
3247 1.1 rjs return (NULL);
3248 1.1 rjs }
3249 1.1 rjs
3250 1.1 rjs }
3251 1.1 rjs num_chunks++;
3252 1.1 rjs /* Save off the last place we got a control from */
3253 1.1 rjs if ((*netp) && stcb) {
3254 1.1 rjs stcb->asoc.last_control_chunk_from = *netp;
3255 1.1 rjs }
3256 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
3257 1.1 rjs sctp_audit_log(0xB0, ch->chunk_type);
3258 1.1 rjs #endif
3259 1.1 rjs switch (ch->chunk_type) {
3260 1.1 rjs case SCTP_INITIATION:
3261 1.1 rjs /* must be first and only chunk */
3262 1.1 rjs #ifdef SCTP_DEBUG
3263 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3264 1.1 rjs printf("SCTP_INIT\n");
3265 1.1 rjs }
3266 1.1 rjs #endif /* SCTP_DEBUG */
3267 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3268 1.1 rjs /* We are not interested anymore */
3269 1.1 rjs if (locked_tcb) {
3270 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3271 1.1 rjs }
3272 1.1 rjs if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3273 1.1 rjs /* finish the job now */
3274 1.1 rjs sctp_inpcb_free(inp, 1);
3275 1.1 rjs }
3276 1.1 rjs *offset = length;
3277 1.1 rjs return (NULL);
3278 1.1 rjs }
3279 1.1 rjs if ((num_chunks > 1) ||
3280 1.1 rjs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3281 1.1 rjs *offset = length;
3282 1.1 rjs if (locked_tcb) {
3283 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3284 1.1 rjs }
3285 1.1 rjs return (NULL);
3286 1.1 rjs }
3287 1.1 rjs if ((stcb != NULL) &&
3288 1.1 rjs (SCTP_GET_STATE(&stcb->asoc) ==
3289 1.1 rjs SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3290 1.1 rjs sctp_send_shutdown_ack(stcb,
3291 1.1 rjs stcb->asoc.primary_destination);
3292 1.1 rjs *offset = length;
3293 1.1 rjs if (locked_tcb) {
3294 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3295 1.1 rjs }
3296 1.1 rjs return (NULL);
3297 1.1 rjs }
3298 1.1 rjs sctp_handle_init(m, iphlen, *offset, sh,
3299 1.1 rjs (struct sctp_init_chunk *)ch, inp, stcb, *netp);
3300 1.1 rjs *offset = length;
3301 1.1 rjs if (locked_tcb) {
3302 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3303 1.1 rjs }
3304 1.1 rjs return (NULL);
3305 1.1 rjs break;
3306 1.1 rjs case SCTP_INITIATION_ACK:
3307 1.1 rjs /* must be first and only chunk */
3308 1.1 rjs #ifdef SCTP_DEBUG
3309 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3310 1.1 rjs printf("SCTP_INIT-ACK\n");
3311 1.1 rjs }
3312 1.1 rjs #endif /* SCTP_DEBUG */
3313 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3314 1.1 rjs /* We are not interested anymore */
3315 1.1 rjs if (locked_tcb) {
3316 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3317 1.1 rjs }
3318 1.1 rjs *offset = length;
3319 1.1 rjs if (stcb) {
3320 1.1 rjs sctp_free_assoc(inp, stcb);
3321 1.1 rjs } else {
3322 1.1 rjs if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3323 1.1 rjs /* finish the job now */
3324 1.1 rjs sctp_inpcb_free(inp, 1);
3325 1.1 rjs }
3326 1.1 rjs }
3327 1.1 rjs return (NULL);
3328 1.1 rjs }
3329 1.1 rjs if ((num_chunks > 1) ||
3330 1.1 rjs (sctp_strict_init && (length - *offset > SCTP_SIZE32(chk_length)))) {
3331 1.1 rjs #ifdef SCTP_DEBUG
3332 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3333 1.1 rjs printf("Length is %d rounded chk_length:%d .. dropping\n",
3334 1.1 rjs length - *offset,
3335 1.1 rjs SCTP_SIZE32(chk_length));
3336 1.1 rjs }
3337 1.1 rjs #endif
3338 1.1 rjs *offset = length;
3339 1.1 rjs if (locked_tcb) {
3340 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3341 1.1 rjs }
3342 1.1 rjs return (NULL);
3343 1.1 rjs }
3344 1.1 rjs ret = sctp_handle_init_ack(m, iphlen, *offset, sh,
3345 1.1 rjs (struct sctp_init_ack_chunk *)ch, stcb, *netp);
3346 1.1 rjs /*
3347 1.1 rjs * Special case, I must call the output routine
3348 1.1 rjs * to get the cookie echoed
3349 1.1 rjs */
3350 1.1 rjs if ((stcb) && ret == 0)
3351 1.1 rjs sctp_chunk_output(stcb->sctp_ep, stcb, 2);
3352 1.1 rjs *offset = length;
3353 1.1 rjs #ifdef SCTP_DEBUG
3354 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3355 1.1 rjs printf("All done INIT-ACK processing\n");
3356 1.1 rjs }
3357 1.1 rjs #endif
3358 1.1 rjs if (locked_tcb) {
3359 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3360 1.1 rjs }
3361 1.1 rjs return (NULL);
3362 1.1 rjs break;
3363 1.1 rjs case SCTP_SELECTIVE_ACK:
3364 1.1 rjs #ifdef SCTP_DEBUG
3365 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3366 1.1 rjs printf("SCTP_SACK\n");
3367 1.1 rjs }
3368 1.1 rjs #endif /* SCTP_DEBUG */
3369 1.1 rjs sctp_pegs[SCTP_PEG_SACKS_SEEN]++;
3370 1.1 rjs {
3371 1.1 rjs int abort_now = 0;
3372 1.1 rjs stcb->asoc.seen_a_sack_this_pkt = 1;
3373 1.1 rjs sctp_handle_sack((struct sctp_sack_chunk *)ch,
3374 1.1 rjs stcb, *netp, &abort_now);
3375 1.1 rjs if (abort_now) {
3376 1.1 rjs /* ABORT signal from sack processing */
3377 1.1 rjs *offset = length;
3378 1.1 rjs return (NULL);
3379 1.1 rjs }
3380 1.1 rjs }
3381 1.1 rjs break;
3382 1.1 rjs case SCTP_HEARTBEAT_REQUEST:
3383 1.1 rjs #ifdef SCTP_DEBUG
3384 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3385 1.1 rjs printf("SCTP_HEARTBEAT\n");
3386 1.1 rjs }
3387 1.1 rjs #endif /* SCTP_DEBUG */
3388 1.1 rjs sctp_pegs[SCTP_HB_RECV]++;
3389 1.1 rjs sctp_send_heartbeat_ack(stcb, m, *offset, chk_length,
3390 1.1 rjs *netp);
3391 1.1 rjs
3392 1.1 rjs /* He's alive so give him credit */
3393 1.1 rjs stcb->asoc.overall_error_count = 0;
3394 1.1 rjs break;
3395 1.1 rjs case SCTP_HEARTBEAT_ACK:
3396 1.1 rjs #ifdef SCTP_DEBUG
3397 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3398 1.1 rjs printf("SCTP_HEARTBEAT-ACK\n");
3399 1.1 rjs }
3400 1.1 rjs #endif /* SCTP_DEBUG */
3401 1.1 rjs
3402 1.1 rjs /* He's alive so give him credit */
3403 1.1 rjs stcb->asoc.overall_error_count = 0;
3404 1.1 rjs
3405 1.1 rjs sctp_pegs[SCTP_HB_ACK_RECV]++;
3406 1.1 rjs sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
3407 1.1 rjs stcb, *netp);
3408 1.1 rjs break;
3409 1.1 rjs case SCTP_ABORT_ASSOCIATION:
3410 1.1 rjs #ifdef SCTP_DEBUG
3411 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3412 1.1 rjs printf("SCTP_ABORT\n");
3413 1.1 rjs }
3414 1.1 rjs #endif /* SCTP_DEBUG */
3415 1.1 rjs sctp_handle_abort((struct sctp_abort_chunk *)ch,
3416 1.1 rjs stcb, *netp);
3417 1.1 rjs *offset = length;
3418 1.1 rjs return (NULL);
3419 1.1 rjs break;
3420 1.1 rjs case SCTP_SHUTDOWN:
3421 1.1 rjs #ifdef SCTP_DEBUG
3422 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3423 1.1 rjs printf("SCTP_SHUTDOWN\n");
3424 1.1 rjs }
3425 1.1 rjs #endif /* SCTP_DEBUG */
3426 1.1 rjs {
3427 1.1 rjs int abort_flag = 0;
3428 1.1 rjs sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
3429 1.1 rjs stcb, *netp, &abort_flag);
3430 1.1 rjs if (abort_flag) {
3431 1.1 rjs *offset = length;
3432 1.1 rjs return (NULL);
3433 1.1 rjs }
3434 1.1 rjs }
3435 1.1 rjs break;
3436 1.1 rjs case SCTP_SHUTDOWN_ACK:
3437 1.1 rjs #ifdef SCTP_DEBUG
3438 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3439 1.1 rjs printf("SCTP_SHUTDOWN-ACK\n");
3440 1.1 rjs }
3441 1.1 rjs #endif /* SCTP_DEBUG */
3442 1.1 rjs sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
3443 1.1 rjs *offset = length;
3444 1.1 rjs return (NULL);
3445 1.1 rjs break;
3446 1.1 rjs case SCTP_OPERATION_ERROR:
3447 1.1 rjs #ifdef SCTP_DEBUG
3448 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3449 1.1 rjs printf("SCTP_OP-ERR\n");
3450 1.1 rjs }
3451 1.1 rjs #endif /* SCTP_DEBUG */
3452 1.1 rjs if (sctp_handle_error(ch, stcb, *netp) < 0) {
3453 1.1 rjs *offset = length;
3454 1.1 rjs return (NULL);
3455 1.1 rjs }
3456 1.1 rjs break;
3457 1.1 rjs case SCTP_COOKIE_ECHO:
3458 1.1 rjs #ifdef SCTP_DEBUG
3459 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3460 1.1 rjs printf("SCTP_COOKIE-ECHO stcb is %p\n", stcb);
3461 1.1 rjs }
3462 1.1 rjs #endif /* SCTP_DEBUG */
3463 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3464 1.1 rjs /* We are not interested anymore */
3465 1.1 rjs *offset = length;
3466 1.1 rjs if (stcb) {
3467 1.1 rjs sctp_free_assoc(inp, stcb);
3468 1.1 rjs } else {
3469 1.1 rjs if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
3470 1.1 rjs /* finish the job now */
3471 1.1 rjs sctp_inpcb_free(inp, 1);
3472 1.1 rjs }
3473 1.1 rjs }
3474 1.1 rjs return (NULL);
3475 1.1 rjs }
3476 1.1 rjs /*
3477 1.1 rjs * First are we accepting?
3478 1.1 rjs * We do this again here since it is possible
3479 1.1 rjs * that a previous endpoint WAS listening responded to
3480 1.1 rjs * a INIT-ACK and then closed. We opened and bound..
3481 1.1 rjs * and are now no longer listening.
3482 1.1 rjs */
3483 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) == 0) ||
3484 1.1 rjs (inp->sctp_socket->so_qlimit == 0)) {
3485 1.1 rjs sctp_abort_association(inp, stcb, m, iphlen, sh,
3486 1.1 rjs NULL);
3487 1.1 rjs *offset = length;
3488 1.1 rjs return (NULL);
3489 1.1 rjs } else if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3490 1.1 rjs /* we are accepting so check limits like TCP */
3491 1.1 rjs if (inp->sctp_socket->so_qlen >
3492 1.1 rjs inp->sctp_socket->so_qlimit) {
3493 1.1 rjs /* no space */
3494 1.1 rjs struct mbuf *oper;
3495 1.1 rjs struct sctp_paramhdr *phdr;
3496 1.1 rjs oper = NULL;
3497 1.1 rjs MGETHDR(oper, M_DONTWAIT, MT_HEADER);
3498 1.1 rjs if (oper) {
3499 1.1 rjs oper->m_len =
3500 1.1 rjs oper->m_pkthdr.len =
3501 1.1 rjs sizeof(struct sctp_paramhdr);
3502 1.1 rjs phdr = mtod(oper,
3503 1.1 rjs struct sctp_paramhdr *);
3504 1.1 rjs phdr->param_type =
3505 1.1 rjs htons(SCTP_CAUSE_OUT_OF_RESC);
3506 1.1 rjs phdr->param_length =
3507 1.1 rjs htons(sizeof(struct sctp_paramhdr));
3508 1.1 rjs }
3509 1.1 rjs sctp_abort_association(inp, stcb, m,
3510 1.1 rjs iphlen, sh, oper);
3511 1.1 rjs *offset = length;
3512 1.1 rjs return (NULL);
3513 1.1 rjs }
3514 1.1 rjs }
3515 1.1 rjs {
3516 1.1 rjs struct mbuf *ret_buf;
3517 1.1 rjs ret_buf = sctp_handle_cookie_echo(m, iphlen,
3518 1.1 rjs *offset, sh,
3519 1.1 rjs (struct sctp_cookie_echo_chunk *)ch, &inp,
3520 1.1 rjs &stcb, netp);
3521 1.1 rjs #ifdef SCTP_DEBUG
3522 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3523 1.1 rjs printf("ret_buf:%p length:%d off:%d\n",
3524 1.1 rjs ret_buf, length, *offset);
3525 1.1 rjs }
3526 1.1 rjs #endif /* SCTP_DEBUG */
3527 1.1 rjs
3528 1.1 rjs if (ret_buf == NULL) {
3529 1.1 rjs if (locked_tcb) {
3530 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3531 1.1 rjs }
3532 1.1 rjs #ifdef SCTP_DEBUG
3533 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3534 1.1 rjs printf("GAK, null buffer\n");
3535 1.1 rjs }
3536 1.1 rjs #endif /* SCTP_DEBUG */
3537 1.1 rjs *offset = length;
3538 1.1 rjs return (NULL);
3539 1.1 rjs }
3540 1.1 rjs if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
3541 1.1 rjs /*
3542 1.1 rjs * Restart the timer if we have pending
3543 1.1 rjs * data
3544 1.1 rjs */
3545 1.1 rjs struct sctp_tmit_chunk *chk;
3546 1.1 rjs chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
3547 1.1 rjs if (chk) {
3548 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND,
3549 1.1 rjs stcb->sctp_ep, stcb,
3550 1.1 rjs chk->whoTo);
3551 1.1 rjs }
3552 1.1 rjs }
3553 1.1 rjs }
3554 1.1 rjs break;
3555 1.1 rjs case SCTP_COOKIE_ACK:
3556 1.1 rjs #ifdef SCTP_DEBUG
3557 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3558 1.1 rjs printf("SCTP_COOKIE-ACK\n");
3559 1.1 rjs }
3560 1.1 rjs #endif /* SCTP_DEBUG */
3561 1.1 rjs
3562 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3563 1.1 rjs /* We are not interested anymore */
3564 1.1 rjs sctp_free_assoc(inp, stcb);
3565 1.1 rjs *offset = length;
3566 1.1 rjs return (NULL);
3567 1.1 rjs }
3568 1.1 rjs /* He's alive so give him credit */
3569 1.1 rjs stcb->asoc.overall_error_count = 0;
3570 1.1 rjs sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch,
3571 1.1 rjs stcb, *netp);
3572 1.1 rjs break;
3573 1.1 rjs case SCTP_ECN_ECHO:
3574 1.1 rjs #ifdef SCTP_DEBUG
3575 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3576 1.1 rjs printf("SCTP_ECN-ECHO\n");
3577 1.1 rjs }
3578 1.1 rjs #endif /* SCTP_DEBUG */
3579 1.1 rjs /* He's alive so give him credit */
3580 1.1 rjs stcb->asoc.overall_error_count = 0;
3581 1.1 rjs sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch,
3582 1.1 rjs stcb);
3583 1.1 rjs break;
3584 1.1 rjs case SCTP_ECN_CWR:
3585 1.1 rjs #ifdef SCTP_DEBUG
3586 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3587 1.1 rjs printf("SCTP_ECN-CWR\n");
3588 1.1 rjs }
3589 1.1 rjs #endif /* SCTP_DEBUG */
3590 1.1 rjs /* He's alive so give him credit */
3591 1.1 rjs stcb->asoc.overall_error_count = 0;
3592 1.1 rjs
3593 1.1 rjs sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb);
3594 1.1 rjs break;
3595 1.1 rjs case SCTP_SHUTDOWN_COMPLETE:
3596 1.1 rjs #ifdef SCTP_DEBUG
3597 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3598 1.1 rjs printf("SCTP_SHUTDOWN-COMPLETE\n");
3599 1.1 rjs }
3600 1.1 rjs #endif /* SCTP_DEBUG */
3601 1.1 rjs /* must be first and only chunk */
3602 1.1 rjs if ((num_chunks > 1) ||
3603 1.1 rjs (length - *offset > SCTP_SIZE32(chk_length))) {
3604 1.1 rjs *offset = length;
3605 1.1 rjs if (locked_tcb) {
3606 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3607 1.1 rjs }
3608 1.1 rjs return (NULL);
3609 1.1 rjs }
3610 1.1 rjs sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
3611 1.1 rjs stcb, *netp);
3612 1.1 rjs *offset = length;
3613 1.1 rjs return (NULL);
3614 1.1 rjs break;
3615 1.1 rjs case SCTP_ASCONF:
3616 1.1 rjs #ifdef SCTP_DEBUG
3617 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3618 1.1 rjs printf("SCTP_ASCONF\n");
3619 1.1 rjs }
3620 1.1 rjs #endif /* SCTP_DEBUG */
3621 1.1 rjs /* He's alive so give him credit */
3622 1.1 rjs stcb->asoc.overall_error_count = 0;
3623 1.1 rjs
3624 1.1 rjs sctp_handle_asconf(m, *offset,
3625 1.1 rjs (struct sctp_asconf_chunk *)ch, stcb, *netp);
3626 1.1 rjs break;
3627 1.1 rjs case SCTP_ASCONF_ACK:
3628 1.1 rjs #ifdef SCTP_DEBUG
3629 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3630 1.1 rjs printf("SCTP_ASCONF-ACK\n");
3631 1.1 rjs }
3632 1.1 rjs #endif /* SCTP_DEBUG */
3633 1.1 rjs /* He's alive so give him credit */
3634 1.1 rjs stcb->asoc.overall_error_count = 0;
3635 1.1 rjs
3636 1.1 rjs sctp_handle_asconf_ack(m, *offset,
3637 1.1 rjs (struct sctp_asconf_ack_chunk *)ch, stcb, *netp);
3638 1.1 rjs break;
3639 1.1 rjs case SCTP_FORWARD_CUM_TSN:
3640 1.1 rjs #ifdef SCTP_DEBUG
3641 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3642 1.1 rjs printf("SCTP_FWD-TSN\n");
3643 1.1 rjs }
3644 1.1 rjs #endif /* SCTP_DEBUG */
3645 1.1 rjs /* He's alive so give him credit */
3646 1.1 rjs {
3647 1.1 rjs int abort_flag = 0;
3648 1.1 rjs stcb->asoc.overall_error_count = 0;
3649 1.1 rjs *fwd_tsn_seen = 1;
3650 1.1 rjs sctp_handle_forward_tsn(stcb,
3651 1.1 rjs (struct sctp_forward_tsn_chunk *)ch, &abort_flag);
3652 1.1 rjs if (abort_flag) {
3653 1.1 rjs *offset = length;
3654 1.1 rjs return (NULL);
3655 1.1 rjs } else {
3656 1.1 rjs stcb->asoc.overall_error_count = 0;
3657 1.1 rjs }
3658 1.1 rjs
3659 1.1 rjs }
3660 1.1 rjs break;
3661 1.1 rjs case SCTP_STREAM_RESET:
3662 1.1 rjs #ifdef SCTP_DEBUG
3663 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3664 1.1 rjs printf("SCTP_STREAM_RESET\n");
3665 1.1 rjs }
3666 1.1 rjs #endif /* SCTP_DEBUG */
3667 1.1 rjs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3668 1.1 rjs chk_length, chunk_buf);
3669 1.1 rjs if (stcb->asoc.peer_supports_strreset == 0) {
3670 1.1 rjs /* hmm, peer should have annonced this, but
3671 1.1 rjs * we will turn it on since he is sending us
3672 1.1 rjs * a stream reset.
3673 1.1 rjs */
3674 1.1 rjs stcb->asoc.peer_supports_strreset = 1;
3675 1.1 rjs }
3676 1.1 rjs sctp_handle_stream_reset(stcb, (struct sctp_stream_reset_req *)ch);
3677 1.1 rjs break;
3678 1.1 rjs case SCTP_PACKET_DROPPED:
3679 1.1 rjs #ifdef SCTP_DEBUG
3680 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3681 1.1 rjs printf("SCTP_PACKET_DROPPED\n");
3682 1.1 rjs }
3683 1.1 rjs #endif /* SCTP_DEBUG */
3684 1.1 rjs /* re-get it all please */
3685 1.1 rjs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3686 1.1 rjs chk_length, chunk_buf);
3687 1.1 rjs
3688 1.1 rjs sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
3689 1.1 rjs stcb, *netp);
3690 1.1 rjs
3691 1.1 rjs
3692 1.1 rjs break;
3693 1.1 rjs default:
3694 1.1 rjs /* it's an unknown chunk! */
3695 1.1 rjs if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
3696 1.1 rjs struct mbuf *mm;
3697 1.1 rjs struct sctp_paramhdr *phd;
3698 1.1 rjs MGETHDR(mm, M_DONTWAIT, MT_HEADER);
3699 1.1 rjs if (mm) {
3700 1.1 rjs phd = mtod(mm, struct sctp_paramhdr *);
3701 1.1 rjs /* We cheat and use param type since we
3702 1.1 rjs * did not bother to define a error
3703 1.1 rjs * cause struct.
3704 1.1 rjs * They are the same basic format with
3705 1.1 rjs * different names.
3706 1.1 rjs */
3707 1.1 rjs phd->param_type =
3708 1.1 rjs htons(SCTP_CAUSE_UNRECOG_CHUNK);
3709 1.1 rjs phd->param_length =
3710 1.1 rjs htons(chk_length + sizeof(*phd));
3711 1.1 rjs mm->m_len = sizeof(*phd);
3712 1.1 rjs mm->m_next = sctp_m_copym(m, *offset,
3713 1.1 rjs SCTP_SIZE32(chk_length),
3714 1.1 rjs M_DONTWAIT);
3715 1.1 rjs if (mm->m_next) {
3716 1.1 rjs mm->m_pkthdr.len =
3717 1.1 rjs SCTP_SIZE32(chk_length) +
3718 1.1 rjs sizeof(*phd);
3719 1.1 rjs sctp_queue_op_err(stcb, mm);
3720 1.1 rjs } else {
3721 1.1 rjs sctp_m_freem(mm);
3722 1.1 rjs #ifdef SCTP_DEBUG
3723 1.1 rjs if (sctp_debug_on &
3724 1.1 rjs SCTP_DEBUG_INPUT1) {
3725 1.1 rjs printf("Gak can't copy the chunk into operr %d bytes\n",
3726 1.1 rjs chk_length);
3727 1.1 rjs }
3728 1.1 rjs #endif
3729 1.1 rjs }
3730 1.1 rjs }
3731 1.1 rjs #ifdef SCTP_DEBUG
3732 1.1 rjs else {
3733 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
3734 1.1 rjs printf("Gak can't mgethdr for op-err of unrec chunk\n");
3735 1.1 rjs }
3736 1.1 rjs }
3737 1.1 rjs #endif
3738 1.1 rjs }
3739 1.1 rjs if ((ch->chunk_type & 0x80) == 0) {
3740 1.1 rjs /* discard this packet */
3741 1.1 rjs *offset = length;
3742 1.1 rjs return (stcb);
3743 1.1 rjs } /* else skip this bad chunk and continue... */
3744 1.1 rjs break;
3745 1.1 rjs } /* switch (ch->chunk_type) */
3746 1.1 rjs /* get the next chunk */
3747 1.1 rjs *offset += SCTP_SIZE32(chk_length);
3748 1.1 rjs if (*offset >= length) {
3749 1.1 rjs /* no more data left in the mbuf chain */
3750 1.1 rjs break;
3751 1.1 rjs }
3752 1.1 rjs ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
3753 1.1 rjs sizeof(struct sctp_chunkhdr), chunk_buf);
3754 1.1 rjs if (ch == NULL) {
3755 1.1 rjs if (locked_tcb) {
3756 1.1 rjs SCTP_TCB_UNLOCK(locked_tcb);
3757 1.1 rjs }
3758 1.1 rjs *offset = length;
3759 1.1 rjs return (NULL);
3760 1.1 rjs }
3761 1.1 rjs } /* while */
3762 1.1 rjs return (stcb);
3763 1.1 rjs }
3764 1.1 rjs
3765 1.1 rjs
3766 1.1 rjs /*
3767 1.1 rjs * Process the ECN bits we have something set so
3768 1.1 rjs * we must look to see if it is ECN(0) or ECN(1) or CE
3769 1.1 rjs */
3770 1.1 rjs static void
3771 1.1 rjs sctp_process_ecn_marked_a(struct sctp_tcb *stcb, struct sctp_nets *net,
3772 1.1 rjs u_int8_t ecn_bits)
3773 1.1 rjs {
3774 1.1 rjs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3775 1.1 rjs ;
3776 1.1 rjs } else if ((ecn_bits & SCTP_ECT1_BIT) == SCTP_ECT1_BIT) {
3777 1.1 rjs /*
3778 1.1 rjs * we only add to the nonce sum for ECT1, ECT0
3779 1.1 rjs * does not change the NS bit (that we have
3780 1.1 rjs * yet to find a way to send it yet).
3781 1.1 rjs */
3782 1.1 rjs
3783 1.1 rjs /* ECN Nonce stuff */
3784 1.1 rjs stcb->asoc.receiver_nonce_sum++;
3785 1.1 rjs stcb->asoc.receiver_nonce_sum &= SCTP_SACK_NONCE_SUM;
3786 1.1 rjs
3787 1.1 rjs /*
3788 1.1 rjs * Drag up the last_echo point if cumack is larger since we
3789 1.1 rjs * don't want the point falling way behind by more than 2^^31
3790 1.1 rjs * and then having it be incorrect.
3791 1.1 rjs */
3792 1.1 rjs if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3793 1.1 rjs stcb->asoc.last_echo_tsn, MAX_TSN)) {
3794 1.1 rjs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3795 1.1 rjs }
3796 1.1 rjs } else if ((ecn_bits & SCTP_ECT0_BIT) == SCTP_ECT0_BIT) {
3797 1.1 rjs /*
3798 1.1 rjs * Drag up the last_echo point if cumack is larger since we
3799 1.1 rjs * don't want the point falling way behind by more than 2^^31
3800 1.1 rjs * and then having it be incorrect.
3801 1.1 rjs */
3802 1.1 rjs if (compare_with_wrap(stcb->asoc.cumulative_tsn,
3803 1.1 rjs stcb->asoc.last_echo_tsn, MAX_TSN)) {
3804 1.1 rjs stcb->asoc.last_echo_tsn = stcb->asoc.cumulative_tsn;
3805 1.1 rjs }
3806 1.1 rjs }
3807 1.1 rjs }
3808 1.1 rjs
3809 1.1 rjs static void
3810 1.1 rjs sctp_process_ecn_marked_b(struct sctp_tcb *stcb, struct sctp_nets *net,
3811 1.1 rjs u_int32_t high_tsn, u_int8_t ecn_bits)
3812 1.1 rjs {
3813 1.1 rjs if ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS) {
3814 1.1 rjs /*
3815 1.1 rjs * we possibly must notify the sender that a congestion
3816 1.1 rjs * window reduction is in order. We do this
3817 1.1 rjs * by adding a ECNE chunk to the output chunk
3818 1.1 rjs * queue. The incoming CWR will remove this chunk.
3819 1.1 rjs */
3820 1.1 rjs if (compare_with_wrap(high_tsn, stcb->asoc.last_echo_tsn,
3821 1.1 rjs MAX_TSN)) {
3822 1.1 rjs /* Yep, we need to add a ECNE */
3823 1.1 rjs sctp_send_ecn_echo(stcb, net, high_tsn);
3824 1.1 rjs stcb->asoc.last_echo_tsn = high_tsn;
3825 1.1 rjs }
3826 1.1 rjs }
3827 1.1 rjs }
3828 1.1 rjs
3829 1.1 rjs /*
3830 1.1 rjs * common input chunk processing (v4 and v6)
3831 1.1 rjs */
3832 1.1 rjs int
3833 1.1 rjs sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset,
3834 1.1 rjs int length, struct sctphdr *sh, struct sctp_chunkhdr *ch,
3835 1.1 rjs struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets *net,
3836 1.1 rjs u_int8_t ecn_bits)
3837 1.1 rjs {
3838 1.1 rjs /*
3839 1.1 rjs * Control chunk processing
3840 1.1 rjs */
3841 1.1 rjs u_int32_t high_tsn;
3842 1.1 rjs int fwd_tsn_seen = 0, data_processed = 0;
3843 1.1 rjs struct mbuf *m = *mm;
3844 1.1 rjs int abort_flag = 0;
3845 1.1 rjs
3846 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_RCVD]++;
3847 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
3848 1.1 rjs sctp_audit_log(0xE0, 1);
3849 1.1 rjs sctp_auditing(0, inp, stcb, net);
3850 1.1 rjs #endif
3851 1.1 rjs
3852 1.1 rjs #ifdef SCTP_DEBUG
3853 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3854 1.1 rjs printf("Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d\n",
3855 1.1 rjs m, iphlen, offset, length);
3856 1.1 rjs }
3857 1.1 rjs #endif /* SCTP_DEBUG */
3858 1.1 rjs if (IS_SCTP_CONTROL(ch)) {
3859 1.1 rjs /* process the control portion of the SCTP packet */
3860 1.1 rjs #ifdef SCTP_DEBUG
3861 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3862 1.1 rjs printf("Processing control\n");
3863 1.1 rjs }
3864 1.1 rjs #endif /* SCTP_DEBUG */
3865 1.1 rjs
3866 1.1 rjs stcb = sctp_process_control(m, iphlen, &offset, length, sh, ch,
3867 1.1 rjs inp, stcb, &net, &fwd_tsn_seen);
3868 1.1 rjs } else {
3869 1.1 rjs /*
3870 1.1 rjs * no control chunks, so pre-process DATA chunks
3871 1.1 rjs * (these checks are taken care of by control processing)
3872 1.1 rjs */
3873 1.1 rjs #ifdef SCTP_DEBUG
3874 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3875 1.1 rjs printf("No control present\n");
3876 1.1 rjs }
3877 1.1 rjs #endif /* SCTP_DEBUG */
3878 1.1 rjs
3879 1.1 rjs if (stcb == NULL) {
3880 1.1 rjs /* out of the blue DATA chunk */
3881 1.1 rjs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3882 1.1 rjs return (1);
3883 1.1 rjs }
3884 1.1 rjs if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
3885 1.1 rjs /* v_tag mismatch! */
3886 1.1 rjs sctp_pegs[SCTP_BAD_VTAGS]++;
3887 1.1 rjs SCTP_TCB_UNLOCK(stcb);
3888 1.1 rjs return (1);
3889 1.1 rjs }
3890 1.1 rjs }
3891 1.1 rjs if (stcb == NULL) {
3892 1.1 rjs /*
3893 1.1 rjs * no valid TCB for this packet,
3894 1.1 rjs * or we found it's a bad packet while processing control,
3895 1.1 rjs * or we're done with this packet (done or skip rest of data),
3896 1.1 rjs * so we drop it...
3897 1.1 rjs */
3898 1.1 rjs return (1);
3899 1.1 rjs }
3900 1.1 rjs #ifdef SCTP_DEBUG
3901 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3902 1.1 rjs printf("Ok, control finished time to look for data (%d) offset:%d\n",
3903 1.1 rjs length, offset);
3904 1.1 rjs }
3905 1.1 rjs #endif /* SCTP_DEBUG */
3906 1.1 rjs /*
3907 1.1 rjs * DATA chunk processing
3908 1.1 rjs */
3909 1.1 rjs /* plow through the data chunks while length > offset */
3910 1.1 rjs stcb->asoc.seen_a_sack_this_pkt = 0;
3911 1.1 rjs
3912 1.1 rjs if (length > offset) {
3913 1.1 rjs int retval;
3914 1.1 rjs /*
3915 1.1 rjs * First check to make sure our state is correct.
3916 1.1 rjs * We would not get here unless we really did have a
3917 1.1 rjs * tag, so we don't abort if this happens, just
3918 1.1 rjs * dump the chunk silently.
3919 1.1 rjs */
3920 1.1 rjs switch (SCTP_GET_STATE(&stcb->asoc)) {
3921 1.1 rjs case SCTP_STATE_COOKIE_ECHOED:
3922 1.1 rjs /*
3923 1.1 rjs * we consider data with valid tags in
3924 1.1 rjs * this state shows us the cookie-ack was lost.
3925 1.1 rjs * Imply it was there.
3926 1.1 rjs */
3927 1.1 rjs stcb->asoc.overall_error_count = 0;
3928 1.1 rjs sctp_handle_cookie_ack(
3929 1.1 rjs (struct sctp_cookie_ack_chunk *)ch, stcb, net);
3930 1.1 rjs break;
3931 1.1 rjs case SCTP_STATE_COOKIE_WAIT:
3932 1.1 rjs /*
3933 1.1 rjs * We consider OOTB any data sent during asoc setup.
3934 1.1 rjs */
3935 1.1 rjs sctp_handle_ootb(m, iphlen, offset, sh, inp, NULL);
3936 1.1 rjs SCTP_TCB_UNLOCK(stcb);
3937 1.1 rjs return (1);
3938 1.1 rjs break;
3939 1.1 rjs case SCTP_STATE_EMPTY: /* should not happen */
3940 1.1 rjs case SCTP_STATE_INUSE: /* should not happen */
3941 1.1 rjs case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
3942 1.1 rjs case SCTP_STATE_SHUTDOWN_ACK_SENT:
3943 1.1 rjs default:
3944 1.1 rjs #ifdef SCTP_DEBUG
3945 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
3946 1.1 rjs printf("Got data in invalid state %d.. dropping\n", stcb->asoc.state);
3947 1.1 rjs }
3948 1.1 rjs #endif
3949 1.1 rjs SCTP_TCB_UNLOCK(stcb);
3950 1.1 rjs return (1);
3951 1.1 rjs break;
3952 1.1 rjs case SCTP_STATE_OPEN:
3953 1.1 rjs case SCTP_STATE_SHUTDOWN_SENT:
3954 1.1 rjs break;
3955 1.1 rjs }
3956 1.1 rjs /* take care of ECN, part 1. */
3957 1.1 rjs if (stcb->asoc.ecn_allowed &&
3958 1.1 rjs (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3959 1.1 rjs sctp_process_ecn_marked_a(stcb, net, ecn_bits);
3960 1.1 rjs }
3961 1.1 rjs /* plow through the data chunks while length > offset */
3962 1.1 rjs retval = sctp_process_data(mm, iphlen, &offset, length, sh,
3963 1.1 rjs inp, stcb, net, &high_tsn);
3964 1.1 rjs if (retval == 2) {
3965 1.1 rjs /* The association aborted, NO UNLOCK needed
3966 1.1 rjs * since the association is destroyed.
3967 1.1 rjs */
3968 1.1 rjs return (0);
3969 1.1 rjs }
3970 1.1 rjs
3971 1.1 rjs data_processed = 1;
3972 1.1 rjs if (retval == 0) {
3973 1.1 rjs /* take care of ecn part 2. */
3974 1.1 rjs if (stcb->asoc.ecn_allowed && (ecn_bits & (SCTP_ECT0_BIT|SCTP_ECT1_BIT)) ) {
3975 1.1 rjs sctp_process_ecn_marked_b(stcb, net, high_tsn, ecn_bits);
3976 1.1 rjs
3977 1.1 rjs }
3978 1.1 rjs }
3979 1.1 rjs
3980 1.1 rjs /*
3981 1.1 rjs * Anything important needs to have been m_copy'ed in
3982 1.1 rjs * process_data
3983 1.1 rjs */
3984 1.1 rjs }
3985 1.1 rjs if ((data_processed == 0) && (fwd_tsn_seen)) {
3986 1.1 rjs int was_a_gap = 0;
3987 1.1 rjs if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
3988 1.1 rjs stcb->asoc.cumulative_tsn, MAX_TSN)) {
3989 1.1 rjs /* there was a gap before this data was processed */
3990 1.1 rjs was_a_gap = 1;
3991 1.1 rjs }
3992 1.1 rjs sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3993 1.1 rjs if (abort_flag) {
3994 1.1 rjs /* Again, we aborted so NO UNLOCK needed */
3995 1.1 rjs return (0);
3996 1.1 rjs }
3997 1.1 rjs }
3998 1.1 rjs /* trigger send of any chunks in queue... */
3999 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
4000 1.1 rjs sctp_audit_log(0xE0, 2);
4001 1.1 rjs sctp_auditing(1, inp, stcb, net);
4002 1.1 rjs #endif
4003 1.1 rjs #ifdef SCTP_DEBUG
4004 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4005 1.1 rjs printf("Check for chunk output prw:%d tqe:%d tf=%d\n",
4006 1.1 rjs stcb->asoc.peers_rwnd,
4007 1.1 rjs TAILQ_EMPTY(&stcb->asoc.control_send_queue),
4008 1.1 rjs stcb->asoc.total_flight);
4009 1.1 rjs }
4010 1.1 rjs #endif
4011 1.1 rjs if (stcb->asoc.peers_rwnd > 0 ||
4012 1.1 rjs !TAILQ_EMPTY(&stcb->asoc.control_send_queue) ||
4013 1.1 rjs (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)) {
4014 1.1 rjs #ifdef SCTP_DEBUG
4015 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4016 1.1 rjs printf("Calling chunk OUTPUT\n");
4017 1.1 rjs }
4018 1.1 rjs #endif
4019 1.1 rjs sctp_chunk_output(inp, stcb, 3);
4020 1.1 rjs #ifdef SCTP_DEBUG
4021 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
4022 1.1 rjs printf("chunk OUTPUT returns\n");
4023 1.1 rjs }
4024 1.1 rjs #endif
4025 1.1 rjs }
4026 1.1 rjs
4027 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
4028 1.1 rjs sctp_audit_log(0xE0, 3);
4029 1.1 rjs sctp_auditing(2, inp, stcb, net);
4030 1.1 rjs #endif
4031 1.1 rjs SCTP_TCB_UNLOCK(stcb);
4032 1.1 rjs return (0);
4033 1.1 rjs }
4034 1.1 rjs
4035 1.1 rjs #if defined(__OpenBSD__)
4036 1.1 rjs static void
4037 1.1 rjs sctp_saveopt(struct sctp_inpcb *inp, struct mbuf **mp, struct ip *ip,
4038 1.1 rjs struct mbuf *m)
4039 1.1 rjs {
4040 1.1 rjs if (inp->ip_inp.inp.inp_flags & INP_RECVDSTADDR) {
4041 1.1 rjs *mp = sbcreatecontrol((vaddr_t) &ip->ip_dst,
4042 1.1 rjs sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
4043 1.1 rjs if (*mp)
4044 1.1 rjs mp = &(*mp)->m_next;
4045 1.1 rjs }
4046 1.1 rjs }
4047 1.1 rjs #endif
4048 1.1 rjs
4049 1.1 rjs extern int sctp_no_csum_on_loopback;
4050 1.1 rjs
4051 1.1 rjs void
4052 1.1 rjs sctp_input(struct mbuf *m, ...)
4053 1.1 rjs {
4054 1.1 rjs int iphlen;
4055 1.1 rjs u_int8_t ecn_bits;
4056 1.1 rjs struct ip *ip;
4057 1.1 rjs struct sctphdr *sh;
4058 1.1 rjs struct sctp_inpcb *inp = NULL;
4059 1.1 rjs struct mbuf *opts = 0;
4060 1.1 rjs /*#ifdef INET6*/
4061 1.1 rjs /* Don't think this is needed */
4062 1.1 rjs /* struct ip6_recvpktopts opts6;*/
4063 1.1 rjs /*#endif INET6 */
4064 1.1 rjs
4065 1.1 rjs u_int32_t check, calc_check;
4066 1.1 rjs struct sctp_nets *net;
4067 1.1 rjs struct sctp_tcb *stcb = NULL;
4068 1.1 rjs struct sctp_chunkhdr *ch;
4069 1.1 rjs int refcount_up = 0;
4070 1.1 rjs int length, mlen, offset;
4071 1.1 rjs
4072 1.1 rjs int off;
4073 1.1 rjs va_list ap;
4074 1.1 rjs
4075 1.1 rjs va_start(ap, m);
4076 1.1 rjs iphlen = off = va_arg(ap, int);
4077 1.1 rjs va_end(ap);
4078 1.1 rjs
4079 1.1 rjs net = NULL;
4080 1.1 rjs sctp_pegs[SCTP_INPKTS]++;
4081 1.1 rjs #ifdef SCTP_DEBUG
4082 1.1 rjs /*if (sctp_debug_on & SCTP_DEBUG_INPUT1) {*/
4083 1.1 rjs printf("V4 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
4084 1.1 rjs /*}*/
4085 1.1 rjs #endif
4086 1.1 rjs /*#ifdef INET6*/
4087 1.1 rjs /* Don't think this is needed */
4088 1.1 rjs /* bzero(&opts6, sizeof(opts6));*/
4089 1.1 rjs /*#endif INET6 */
4090 1.1 rjs
4091 1.1 rjs /*
4092 1.1 rjs * Strip IP options, we don't allow any in or out.
4093 1.1 rjs */
4094 1.1 rjs if ((size_t)iphlen > sizeof(struct ip)) {
4095 1.1 rjs printf("sctp_input: got options\n");
4096 1.1 rjs #if 0 /* XXX */
4097 1.1 rjs ip_stripoptions(m, (struct mbuf *)0);
4098 1.1 rjs #endif
4099 1.1 rjs iphlen = sizeof(struct ip);
4100 1.1 rjs }
4101 1.1 rjs
4102 1.1 rjs /*
4103 1.1 rjs * Get IP, SCTP, and first chunk header together in first mbuf.
4104 1.1 rjs */
4105 1.1 rjs ip = mtod(m, struct ip *);
4106 1.1 rjs offset = iphlen + sizeof(*sh) + sizeof(*ch);
4107 1.1 rjs if (m->m_len < offset) {
4108 1.1 rjs if ((m = m_pullup(m, offset)) == 0) {
4109 1.1 rjs sctp_pegs[SCTP_HDR_DROPS]++;
4110 1.1 rjs return;
4111 1.1 rjs }
4112 1.1 rjs ip = mtod(m, struct ip *);
4113 1.1 rjs }
4114 1.1 rjs sh = (struct sctphdr *)((vaddr_t)ip + iphlen);
4115 1.1 rjs ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(*sh));
4116 1.1 rjs
4117 1.1 rjs /* SCTP does not allow broadcasts or multicasts */
4118 1.1 rjs if (IN_MULTICAST(ip->ip_dst.s_addr))
4119 1.1 rjs {
4120 1.1 rjs sctp_pegs[SCTP_IN_MCAST]++;
4121 1.1 rjs goto bad;
4122 1.1 rjs }
4123 1.3 ozaki if (in_broadcast(ip->ip_dst, m_get_rcvif_NOMPSAFE(m))) {
4124 1.1 rjs sctp_pegs[SCTP_IN_MCAST]++;
4125 1.1 rjs goto bad;
4126 1.1 rjs }
4127 1.1 rjs
4128 1.1 rjs /* destination port of 0 is illegal, based on RFC2960. */
4129 1.1 rjs if (sh->dest_port == 0) {
4130 1.1 rjs sctp_pegs[SCTP_HDR_DROPS]++;
4131 1.1 rjs goto bad;
4132 1.1 rjs }
4133 1.1 rjs
4134 1.1 rjs /* validate SCTP checksum */
4135 1.1 rjs if ((sctp_no_csum_on_loopback == 0) ||
4136 1.3 ozaki (m_get_rcvif_NOMPSAFE(m) == NULL) ||
4137 1.3 ozaki (m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
4138 1.1 rjs /* we do NOT validate things from the loopback if the
4139 1.1 rjs * sysctl is set to 1.
4140 1.1 rjs */
4141 1.1 rjs check = sh->checksum; /* save incoming checksum */
4142 1.1 rjs if ((check == 0) && (sctp_no_csum_on_loopback)) {
4143 1.1 rjs /* special hook for where we got a local address
4144 1.1 rjs * somehow routed across a non IFT_LOOP type interface
4145 1.1 rjs */
4146 1.1 rjs if (ip->ip_src.s_addr == ip->ip_dst.s_addr)
4147 1.1 rjs goto sctp_skip_csum_4;
4148 1.1 rjs }
4149 1.1 rjs sh->checksum = 0; /* prepare for calc */
4150 1.1 rjs calc_check = sctp_calculate_sum(m, &mlen, iphlen);
4151 1.1 rjs if (calc_check != check) {
4152 1.1 rjs #ifdef SCTP_DEBUG
4153 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4154 1.1 rjs printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
4155 1.1 rjs calc_check, check, m, mlen, iphlen);
4156 1.1 rjs }
4157 1.1 rjs #endif
4158 1.1 rjs
4159 1.1 rjs stcb = sctp_findassociation_addr(m, iphlen,
4160 1.1 rjs offset - sizeof(*ch),
4161 1.1 rjs sh, ch, &inp, &net);
4162 1.1 rjs if ((inp) && (stcb)) {
4163 1.1 rjs sctp_send_packet_dropped(stcb, net, m, iphlen,
4164 1.1 rjs 1);
4165 1.1 rjs sctp_chunk_output(inp, stcb, 2);
4166 1.1 rjs } else if ((inp != NULL) && (stcb == NULL)) {
4167 1.1 rjs refcount_up = 1;
4168 1.1 rjs }
4169 1.1 rjs sctp_pegs[SCTP_BAD_CSUM]++;
4170 1.1 rjs goto bad;
4171 1.1 rjs }
4172 1.1 rjs sh->checksum = calc_check;
4173 1.1 rjs } else {
4174 1.1 rjs sctp_skip_csum_4:
4175 1.1 rjs mlen = m->m_pkthdr.len;
4176 1.1 rjs }
4177 1.1 rjs /* validate mbuf chain length with IP payload length */
4178 1.1 rjs #if defined(__NetBSD__) || defined(__OpenBSD__)
4179 1.1 rjs /* Open BSD gives us the len in network order, fix it */
4180 1.1 rjs NTOHS(ip->ip_len);
4181 1.1 rjs #endif
4182 1.1 rjs if (mlen < (ip->ip_len - iphlen)) {
4183 1.1 rjs sctp_pegs[SCTP_HDR_DROPS]++;
4184 1.1 rjs goto bad;
4185 1.1 rjs }
4186 1.1 rjs
4187 1.1 rjs /*
4188 1.1 rjs * Locate pcb and tcb for datagram
4189 1.1 rjs * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
4190 1.1 rjs */
4191 1.1 rjs #ifdef SCTP_DEBUG
4192 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4193 1.1 rjs printf("V4 find association\n");
4194 1.1 rjs }
4195 1.1 rjs #endif
4196 1.1 rjs
4197 1.1 rjs stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
4198 1.1 rjs sh, ch, &inp, &net);
4199 1.1 rjs /* inp's ref-count increased && stcb locked */
4200 1.1 rjs if (inp == NULL) {
4201 1.1 rjs struct sctp_init_chunk *init_chk, chunk_buf;
4202 1.1 rjs
4203 1.1 rjs sctp_pegs[SCTP_NOPORTS]++;
4204 1.1 rjs #ifdef ICMP_BANDLIM
4205 1.1 rjs /*
4206 1.1 rjs * we use the bandwidth limiting to protect against
4207 1.1 rjs * sending too many ABORTS all at once. In this case
4208 1.1 rjs * these count the same as an ICMP message.
4209 1.1 rjs */
4210 1.1 rjs if (badport_bandlim(0) < 0)
4211 1.1 rjs goto bad;
4212 1.1 rjs #endif /* ICMP_BANDLIM */
4213 1.1 rjs #ifdef SCTP_DEBUG
4214 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
4215 1.1 rjs printf("Sending a ABORT from packet entry!\n");
4216 1.1 rjs }
4217 1.1 rjs #endif
4218 1.1 rjs if (ch->chunk_type == SCTP_INITIATION) {
4219 1.1 rjs /* we do a trick here to get the INIT tag,
4220 1.1 rjs * dig in and get the tag from the INIT and
4221 1.1 rjs * put it in the common header.
4222 1.1 rjs */
4223 1.1 rjs init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4224 1.1 rjs iphlen + sizeof(*sh), sizeof(*init_chk),
4225 1.1 rjs (u_int8_t *)&chunk_buf);
4226 1.1 rjs if (init_chk != NULL)
4227 1.1 rjs sh->v_tag = init_chk->init.initiate_tag;
4228 1.1 rjs }
4229 1.1 rjs sctp_send_abort(m, iphlen, sh, 0, NULL);
4230 1.1 rjs goto bad;
4231 1.1 rjs } else if (stcb == NULL) {
4232 1.1 rjs refcount_up = 1;
4233 1.1 rjs }
4234 1.1 rjs #ifdef IPSEC
4235 1.1 rjs /*
4236 1.1 rjs * I very much doubt any of the IPSEC stuff will work but I have
4237 1.1 rjs * no idea, so I will leave it in place.
4238 1.1 rjs */
4239 1.8 maxv if (ipsec_used && ipsec_in_reject(m, (struct inpcb *)inp)) {
4240 1.2 rjs #if 0
4241 1.1 rjs ipsecstat.in_polvio++;
4242 1.2 rjs #endif
4243 1.1 rjs sctp_pegs[SCTP_HDR_DROPS]++;
4244 1.1 rjs goto bad;
4245 1.1 rjs }
4246 1.1 rjs #endif /* IPSEC */
4247 1.1 rjs
4248 1.1 rjs /*
4249 1.1 rjs * Construct sockaddr format source address.
4250 1.1 rjs * Stuff source address and datagram in user buffer.
4251 1.1 rjs */
4252 1.1 rjs if ((inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
4253 1.1 rjs || (inp->sctp_socket->so_options & SO_TIMESTAMP)
4254 1.1 rjs ) {
4255 1.1 rjs ip_savecontrol((struct inpcb *)inp, &opts, ip, m);
4256 1.1 rjs }
4257 1.1 rjs
4258 1.1 rjs /*
4259 1.1 rjs * common chunk processing
4260 1.1 rjs */
4261 1.1 rjs length = ip->ip_len - (ip->ip_hl << 2) + iphlen;
4262 1.1 rjs offset -= sizeof(struct sctp_chunkhdr);
4263 1.1 rjs
4264 1.1 rjs ecn_bits = ip->ip_tos;
4265 1.1 rjs sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
4266 1.1 rjs inp, stcb, net, ecn_bits);
4267 1.1 rjs /* inp's ref-count reduced && stcb unlocked */
4268 1.1 rjs if (m) {
4269 1.1 rjs sctp_m_freem(m);
4270 1.1 rjs }
4271 1.1 rjs if (opts)
4272 1.1 rjs sctp_m_freem(opts);
4273 1.1 rjs
4274 1.1 rjs if ((inp) && (refcount_up)) {
4275 1.1 rjs /* reduce ref-count */
4276 1.1 rjs SCTP_INP_WLOCK(inp);
4277 1.1 rjs SCTP_INP_DECR_REF(inp);
4278 1.1 rjs SCTP_INP_WUNLOCK(inp);
4279 1.1 rjs }
4280 1.1 rjs
4281 1.1 rjs return;
4282 1.1 rjs bad:
4283 1.1 rjs if (stcb) {
4284 1.1 rjs SCTP_TCB_UNLOCK(stcb);
4285 1.1 rjs }
4286 1.1 rjs
4287 1.1 rjs if ((inp) && (refcount_up)) {
4288 1.1 rjs /* reduce ref-count */
4289 1.1 rjs SCTP_INP_WLOCK(inp);
4290 1.1 rjs SCTP_INP_DECR_REF(inp);
4291 1.1 rjs SCTP_INP_WUNLOCK(inp);
4292 1.1 rjs }
4293 1.1 rjs
4294 1.1 rjs if (m) {
4295 1.1 rjs sctp_m_freem(m);
4296 1.1 rjs }
4297 1.1 rjs if (opts)
4298 1.1 rjs sctp_m_freem(opts);
4299 1.1 rjs return;
4300 1.1 rjs }
4301