1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
17 *
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_sysctl.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_input.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_timer.h>
51 #include <netinet/sctp_crc32.h>
52 #if defined(INET) || defined(INET6)
53 #include <netinet/udp.h>
54 #endif
55 #include <netinet/in_kdtrace.h>
56 #include <sys/smp.h>
57
58
59
60 static void
sctp_stop_all_cookie_timers(struct sctp_tcb * stcb)61 sctp_stop_all_cookie_timers(struct sctp_tcb *stcb)
62 {
63 struct sctp_nets *net;
64
65 /*
66 * This now not only stops all cookie timers it also stops any INIT
67 * timers as well. This will make sure that the timers are stopped
68 * in all collision cases.
69 */
70 SCTP_TCB_LOCK_ASSERT(stcb);
71 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
72 if (net->rxt_timer.type == SCTP_TIMER_TYPE_COOKIE) {
73 sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE,
74 stcb->sctp_ep,
75 stcb,
76 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_1);
77 } else if (net->rxt_timer.type == SCTP_TIMER_TYPE_INIT) {
78 sctp_timer_stop(SCTP_TIMER_TYPE_INIT,
79 stcb->sctp_ep,
80 stcb,
81 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_2);
82 }
83 }
84 }
85
86 /* INIT handler */
87 static void
sctp_handle_init(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_chunk * cp,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_no_unlock,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)88 sctp_handle_init(struct mbuf *m, int iphlen, int offset,
89 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
90 struct sctp_init_chunk *cp, struct sctp_inpcb *inp,
91 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_no_unlock,
92 uint8_t mflowtype, uint32_t mflowid,
93 uint32_t vrf_id, uint16_t port)
94 {
95 struct sctp_init *init;
96 struct mbuf *op_err;
97
98 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_init: handling INIT tcb:%p\n",
99 (void *)stcb);
100 if (stcb == NULL) {
101 SCTP_INP_RLOCK(inp);
102 }
103 /* validate length */
104 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_chunk)) {
105 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
106 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
107 mflowtype, mflowid,
108 vrf_id, port);
109 if (stcb)
110 *abort_no_unlock = 1;
111 goto outnow;
112 }
113 /* validate parameters */
114 init = &cp->init;
115 if (init->initiate_tag == 0) {
116 /* protocol error... send abort */
117 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
118 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
119 mflowtype, mflowid,
120 vrf_id, port);
121 if (stcb)
122 *abort_no_unlock = 1;
123 goto outnow;
124 }
125 if (ntohl(init->a_rwnd) < SCTP_MIN_RWND) {
126 /* invalid parameter... send abort */
127 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
128 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
129 mflowtype, mflowid,
130 vrf_id, port);
131 if (stcb)
132 *abort_no_unlock = 1;
133 goto outnow;
134 }
135 if (init->num_inbound_streams == 0) {
136 /* protocol error... send abort */
137 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
138 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
139 mflowtype, mflowid,
140 vrf_id, port);
141 if (stcb)
142 *abort_no_unlock = 1;
143 goto outnow;
144 }
145 if (init->num_outbound_streams == 0) {
146 /* protocol error... send abort */
147 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
148 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
149 mflowtype, mflowid,
150 vrf_id, port);
151 if (stcb)
152 *abort_no_unlock = 1;
153 goto outnow;
154 }
155 if (sctp_validate_init_auth_params(m, offset + sizeof(*cp),
156 offset + ntohs(cp->ch.chunk_length))) {
157 /* auth parameter(s) error... send abort */
158 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
159 "Problem with AUTH parameters");
160 sctp_abort_association(inp, stcb, m, iphlen, src, dst, sh, op_err,
161 mflowtype, mflowid,
162 vrf_id, port);
163 if (stcb)
164 *abort_no_unlock = 1;
165 goto outnow;
166 }
167 /* We are only accepting if we have a listening socket. */
168 if ((stcb == NULL) &&
169 ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
170 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
171 (!SCTP_IS_LISTENING(inp)))) {
172 /*
173 * FIX ME ?? What about TCP model and we have a
174 * match/restart case? Actually no fix is needed. the lookup
175 * will always find the existing assoc so stcb would not be
176 * NULL. It may be questionable to do this since we COULD
177 * just send back the INIT-ACK and hope that the app did
178 * accept()'s by the time the COOKIE was sent. But there is
179 * a price to pay for COOKIE generation and I don't want to
180 * pay it on the chance that the app will actually do some
181 * accepts(). The App just looses and should NOT be in this
182 * state :-)
183 */
184 if (SCTP_BASE_SYSCTL(sctp_blackhole) == 0) {
185 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
186 "No listener");
187 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
188 mflowtype, mflowid, inp->fibnum,
189 vrf_id, port);
190 }
191 goto outnow;
192 }
193 if ((stcb != NULL) &&
194 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
195 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending SHUTDOWN-ACK\n");
196 sctp_send_shutdown_ack(stcb, NULL);
197 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
198 } else {
199 SCTPDBG(SCTP_DEBUG_INPUT3, "sctp_handle_init: sending INIT-ACK\n");
200 sctp_send_initiate_ack(inp, stcb, net, m, iphlen, offset,
201 src, dst, sh, cp,
202 mflowtype, mflowid,
203 vrf_id, port);
204 }
205 outnow:
206 if (stcb == NULL) {
207 SCTP_INP_RUNLOCK(inp);
208 }
209 }
210
211 /*
212 * process peer "INIT/INIT-ACK" chunk returns value < 0 on error
213 */
214
215 int
sctp_is_there_unsent_data(struct sctp_tcb * stcb,int so_locked SCTP_UNUSED)216 sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked
217 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
218 SCTP_UNUSED
219 #endif
220 )
221 {
222 int unsent_data;
223 unsigned int i;
224 struct sctp_stream_queue_pending *sp;
225 struct sctp_association *asoc;
226
227 /*
228 * This function returns if any stream has true unsent data on it.
229 * Note that as it looks through it will clean up any places that
230 * have old data that has been sent but left at top of stream queue.
231 */
232 asoc = &stcb->asoc;
233 unsent_data = 0;
234 SCTP_TCB_SEND_LOCK(stcb);
235 if (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
236 /* Check to see if some data queued */
237 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
238 /* sa_ignore FREED_MEMORY */
239 sp = TAILQ_FIRST(&stcb->asoc.strmout[i].outqueue);
240 if (sp == NULL) {
241 continue;
242 }
243 if ((sp->msg_is_complete) &&
244 (sp->length == 0) &&
245 (sp->sender_all_done)) {
246 /*
247 * We are doing differed cleanup. Last time
248 * through when we took all the data the
249 * sender_all_done was not set.
250 */
251 if (sp->put_last_out == 0) {
252 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
253 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
254 sp->sender_all_done,
255 sp->length,
256 sp->msg_is_complete,
257 sp->put_last_out);
258 }
259 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
260 TAILQ_REMOVE(&stcb->asoc.strmout[i].outqueue, sp, next);
261 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, &asoc->strmout[i], sp, 1);
262 if (sp->net) {
263 sctp_free_remote_addr(sp->net);
264 sp->net = NULL;
265 }
266 if (sp->data) {
267 sctp_m_freem(sp->data);
268 sp->data = NULL;
269 }
270 sctp_free_a_strmoq(stcb, sp, so_locked);
271 if (!TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
272 unsent_data++;
273 }
274 } else {
275 unsent_data++;
276 }
277 if (unsent_data > 0) {
278 break;
279 }
280 }
281 }
282 SCTP_TCB_SEND_UNLOCK(stcb);
283 return (unsent_data);
284 }
285
286 static int
sctp_process_init(struct sctp_init_chunk * cp,struct sctp_tcb * stcb)287 sctp_process_init(struct sctp_init_chunk *cp, struct sctp_tcb *stcb)
288 {
289 struct sctp_init *init;
290 struct sctp_association *asoc;
291 struct sctp_nets *lnet;
292 unsigned int i;
293
294 init = &cp->init;
295 asoc = &stcb->asoc;
296 /* save off parameters */
297 asoc->peer_vtag = ntohl(init->initiate_tag);
298 asoc->peers_rwnd = ntohl(init->a_rwnd);
299 /* init tsn's */
300 asoc->highest_tsn_inside_map = asoc->asconf_seq_in = ntohl(init->initial_tsn) - 1;
301
302 if (!TAILQ_EMPTY(&asoc->nets)) {
303 /* update any ssthresh's that may have a default */
304 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
305 lnet->ssthresh = asoc->peers_rwnd;
306 if (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_CWND_MONITOR_ENABLE | SCTP_CWND_LOGGING_ENABLE)) {
307 sctp_log_cwnd(stcb, lnet, 0, SCTP_CWND_INITIALIZATION);
308 }
309
310 }
311 }
312 SCTP_TCB_SEND_LOCK(stcb);
313 if (asoc->pre_open_streams > ntohs(init->num_inbound_streams)) {
314 unsigned int newcnt;
315 struct sctp_stream_out *outs;
316 struct sctp_stream_queue_pending *sp, *nsp;
317 struct sctp_tmit_chunk *chk, *nchk;
318
319 /* abandon the upper streams */
320 newcnt = ntohs(init->num_inbound_streams);
321 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
322 if (chk->rec.data.sid >= newcnt) {
323 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
324 asoc->send_queue_cnt--;
325 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
326 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
327 #ifdef INVARIANTS
328 } else {
329 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
330 #endif
331 }
332 if (chk->data != NULL) {
333 sctp_free_bufspace(stcb, asoc, chk, 1);
334 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
335 0, chk, SCTP_SO_NOT_LOCKED);
336 if (chk->data) {
337 sctp_m_freem(chk->data);
338 chk->data = NULL;
339 }
340 }
341 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
342 /* sa_ignore FREED_MEMORY */
343 }
344 }
345 if (asoc->strmout) {
346 for (i = newcnt; i < asoc->pre_open_streams; i++) {
347 outs = &asoc->strmout[i];
348 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
349 atomic_subtract_int(&stcb->asoc.stream_queue_cnt, 1);
350 TAILQ_REMOVE(&outs->outqueue, sp, next);
351 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
352 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL,
353 stcb, 0, sp, SCTP_SO_NOT_LOCKED);
354 if (sp->data) {
355 sctp_m_freem(sp->data);
356 sp->data = NULL;
357 }
358 if (sp->net) {
359 sctp_free_remote_addr(sp->net);
360 sp->net = NULL;
361 }
362 /* Free the chunk */
363 sctp_free_a_strmoq(stcb, sp, SCTP_SO_NOT_LOCKED);
364 /* sa_ignore FREED_MEMORY */
365 }
366 outs->state = SCTP_STREAM_CLOSED;
367 }
368 }
369 /* cut back the count */
370 asoc->pre_open_streams = newcnt;
371 }
372 SCTP_TCB_SEND_UNLOCK(stcb);
373 asoc->streamoutcnt = asoc->pre_open_streams;
374 if (asoc->strmout) {
375 for (i = 0; i < asoc->streamoutcnt; i++) {
376 asoc->strmout[i].state = SCTP_STREAM_OPEN;
377 }
378 }
379 /* EY - nr_sack: initialize highest tsn in nr_mapping_array */
380 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
382 sctp_log_map(0, 5, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
383 }
384 /* This is the next one we expect */
385 asoc->str_reset_seq_in = asoc->asconf_seq_in + 1;
386
387 asoc->mapping_array_base_tsn = ntohl(init->initial_tsn);
388 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->asconf_seq_in;
389
390 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
391 /* open the requested streams */
392
393 if (asoc->strmin != NULL) {
394 /* Free the old ones */
395 for (i = 0; i < asoc->streamincnt; i++) {
396 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
397 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
398 }
399 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
400 }
401 if (asoc->max_inbound_streams > ntohs(init->num_outbound_streams)) {
402 asoc->streamincnt = ntohs(init->num_outbound_streams);
403 } else {
404 asoc->streamincnt = asoc->max_inbound_streams;
405 }
406 SCTP_MALLOC(asoc->strmin, struct sctp_stream_in *, asoc->streamincnt *
407 sizeof(struct sctp_stream_in), SCTP_M_STRMI);
408 if (asoc->strmin == NULL) {
409 /* we didn't get memory for the streams! */
410 SCTPDBG(SCTP_DEBUG_INPUT2, "process_init: couldn't get memory for the streams!\n");
411 return (-1);
412 }
413 for (i = 0; i < asoc->streamincnt; i++) {
414 asoc->strmin[i].sid = i;
415 asoc->strmin[i].last_mid_delivered = 0xffffffff;
416 TAILQ_INIT(&asoc->strmin[i].inqueue);
417 TAILQ_INIT(&asoc->strmin[i].uno_inqueue);
418 asoc->strmin[i].pd_api_started = 0;
419 asoc->strmin[i].delivery_started = 0;
420 }
421 /*
422 * load_address_from_init will put the addresses into the
423 * association when the COOKIE is processed or the INIT-ACK is
424 * processed. Both types of COOKIE's existing and new call this
425 * routine. It will remove addresses that are no longer in the
426 * association (for the restarting case where addresses are
427 * removed). Up front when the INIT arrives we will discard it if it
428 * is a restart and new addresses have been added.
429 */
430 /* sa_ignore MEMLEAK */
431 return (0);
432 }
433
434 /*
435 * INIT-ACK message processing/consumption returns value < 0 on error
436 */
437 static int
sctp_process_init_ack(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_ack_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_no_unlock,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id)438 sctp_process_init_ack(struct mbuf *m, int iphlen, int offset,
439 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
440 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
441 struct sctp_nets *net, int *abort_no_unlock,
442 uint8_t mflowtype, uint32_t mflowid,
443 uint32_t vrf_id)
444 {
445 struct sctp_association *asoc;
446 struct mbuf *op_err;
447 int retval, abort_flag, cookie_found;
448 int initack_limit;
449 int nat_friendly = 0;
450
451 /* First verify that we have no illegal param's */
452 abort_flag = 0;
453 cookie_found = 0;
454
455 op_err = sctp_arethere_unrecognized_parameters(m,
456 (offset + sizeof(struct sctp_init_chunk)),
457 &abort_flag, (struct sctp_chunkhdr *)cp,
458 &nat_friendly, &cookie_found);
459 if (abort_flag) {
460 /* Send an abort and notify peer */
461 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err, SCTP_SO_NOT_LOCKED);
462 *abort_no_unlock = 1;
463 return (-1);
464 }
465 if (!cookie_found) {
466 uint16_t len;
467
468 /* Only report the missing cookie parameter */
469 if (op_err != NULL) {
470 sctp_m_freem(op_err);
471 }
472 len = (uint16_t)(sizeof(struct sctp_error_missing_param) + sizeof(uint16_t));
473 /* We abort with an error of missing mandatory param */
474 op_err = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
475 if (op_err != NULL) {
476 struct sctp_error_missing_param *cause;
477
478 SCTP_BUF_LEN(op_err) = len;
479 cause = mtod(op_err, struct sctp_error_missing_param *);
480 /* Subtract the reserved param */
481 cause->cause.code = htons(SCTP_CAUSE_MISSING_PARAM);
482 cause->cause.length = htons(len);
483 cause->num_missing_params = htonl(1);
484 cause->type[0] = htons(SCTP_STATE_COOKIE);
485 }
486 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
487 src, dst, sh, op_err,
488 mflowtype, mflowid,
489 vrf_id, net->port);
490 *abort_no_unlock = 1;
491 return (-3);
492 }
493 asoc = &stcb->asoc;
494 asoc->peer_supports_nat = (uint8_t)nat_friendly;
495 /* process the peer's parameters in the INIT-ACK */
496 retval = sctp_process_init((struct sctp_init_chunk *)cp, stcb);
497 if (retval < 0) {
498 return (retval);
499 }
500 initack_limit = offset + ntohs(cp->ch.chunk_length);
501 /* load all addresses */
502 if ((retval = sctp_load_addresses_from_init(stcb, m,
503 (offset + sizeof(struct sctp_init_chunk)), initack_limit,
504 src, dst, NULL, stcb->asoc.port))) {
505 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
506 "Problem with address parameters");
507 SCTPDBG(SCTP_DEBUG_INPUT1,
508 "Load addresses from INIT causes an abort %d\n",
509 retval);
510 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
511 src, dst, sh, op_err,
512 mflowtype, mflowid,
513 vrf_id, net->port);
514 *abort_no_unlock = 1;
515 return (-1);
516 }
517 /* if the peer doesn't support asconf, flush the asconf queue */
518 if (asoc->asconf_supported == 0) {
519 struct sctp_asconf_addr *param, *nparam;
520
521 TAILQ_FOREACH_SAFE(param, &asoc->asconf_queue, next, nparam) {
522 TAILQ_REMOVE(&asoc->asconf_queue, param, next);
523 SCTP_FREE(param, SCTP_M_ASC_ADDR);
524 }
525 }
526
527 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
528 stcb->asoc.local_hmacs);
529 if (op_err) {
530 sctp_queue_op_err(stcb, op_err);
531 /* queuing will steal away the mbuf chain to the out queue */
532 op_err = NULL;
533 }
534 /* extract the cookie and queue it to "echo" it back... */
535 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
536 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
537 stcb->asoc.overall_error_count,
538 0,
539 SCTP_FROM_SCTP_INPUT,
540 __LINE__);
541 }
542 stcb->asoc.overall_error_count = 0;
543 net->error_count = 0;
544
545 /*
546 * Cancel the INIT timer, We do this first before queueing the
547 * cookie. We always cancel at the primary to assue that we are
548 * canceling the timer started by the INIT which always goes to the
549 * primary.
550 */
551 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep, stcb,
552 asoc->primary_destination, SCTP_FROM_SCTP_INPUT + SCTP_LOC_3);
553
554 /* calculate the RTO */
555 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
556 SCTP_RTT_FROM_NON_DATA);
557 retval = sctp_send_cookie_echo(m, offset, initack_limit, stcb, net);
558 return (retval);
559 }
560
561 static void
sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net)562 sctp_handle_heartbeat_ack(struct sctp_heartbeat_chunk *cp,
563 struct sctp_tcb *stcb, struct sctp_nets *net)
564 {
565 union sctp_sockstore store;
566 struct sctp_nets *r_net, *f_net;
567 struct timeval tv;
568 int req_prim = 0;
569 uint16_t old_error_counter;
570
571 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_heartbeat_chunk)) {
572 /* Invalid length */
573 return;
574 }
575
576 memset(&store, 0, sizeof(store));
577 switch (cp->heartbeat.hb_info.addr_family) {
578 #ifdef INET
579 case AF_INET:
580 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in)) {
581 store.sin.sin_family = cp->heartbeat.hb_info.addr_family;
582 store.sin.sin_len = cp->heartbeat.hb_info.addr_len;
583 store.sin.sin_port = stcb->rport;
584 memcpy(&store.sin.sin_addr, cp->heartbeat.hb_info.address,
585 sizeof(store.sin.sin_addr));
586 } else {
587 return;
588 }
589 break;
590 #endif
591 #ifdef INET6
592 case AF_INET6:
593 if (cp->heartbeat.hb_info.addr_len == sizeof(struct sockaddr_in6)) {
594 store.sin6.sin6_family = cp->heartbeat.hb_info.addr_family;
595 store.sin6.sin6_len = cp->heartbeat.hb_info.addr_len;
596 store.sin6.sin6_port = stcb->rport;
597 memcpy(&store.sin6.sin6_addr, cp->heartbeat.hb_info.address, sizeof(struct in6_addr));
598 } else {
599 return;
600 }
601 break;
602 #endif
603 default:
604 return;
605 }
606 r_net = sctp_findnet(stcb, &store.sa);
607 if (r_net == NULL) {
608 SCTPDBG(SCTP_DEBUG_INPUT1, "Huh? I can't find the address I sent it to, discard\n");
609 return;
610 }
611 if ((r_net && (r_net->dest_state & SCTP_ADDR_UNCONFIRMED)) &&
612 (r_net->heartbeat_random1 == cp->heartbeat.hb_info.random_value1) &&
613 (r_net->heartbeat_random2 == cp->heartbeat.hb_info.random_value2)) {
614 /*
615 * If the its a HB and it's random value is correct when can
616 * confirm the destination.
617 */
618 r_net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
619 if (r_net->dest_state & SCTP_ADDR_REQ_PRIMARY) {
620 stcb->asoc.primary_destination = r_net;
621 r_net->dest_state &= ~SCTP_ADDR_REQ_PRIMARY;
622 f_net = TAILQ_FIRST(&stcb->asoc.nets);
623 if (f_net != r_net) {
624 /*
625 * first one on the list is NOT the primary
626 * sctp_cmpaddr() is much more efficient if
627 * the primary is the first on the list,
628 * make it so.
629 */
630 TAILQ_REMOVE(&stcb->asoc.nets, r_net, sctp_next);
631 TAILQ_INSERT_HEAD(&stcb->asoc.nets, r_net, sctp_next);
632 }
633 req_prim = 1;
634 }
635 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
636 stcb, 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
637 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb,
638 r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_4);
639 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
640 }
641 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
642 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
643 stcb->asoc.overall_error_count,
644 0,
645 SCTP_FROM_SCTP_INPUT,
646 __LINE__);
647 }
648 stcb->asoc.overall_error_count = 0;
649 old_error_counter = r_net->error_count;
650 r_net->error_count = 0;
651 r_net->hb_responded = 1;
652 tv.tv_sec = cp->heartbeat.hb_info.time_value_1;
653 tv.tv_usec = cp->heartbeat.hb_info.time_value_2;
654 /* Now lets do a RTO with this */
655 sctp_calculate_rto(stcb, &stcb->asoc, r_net, &tv,
656 SCTP_RTT_FROM_NON_DATA);
657 if (!(r_net->dest_state & SCTP_ADDR_REACHABLE)) {
658 r_net->dest_state |= SCTP_ADDR_REACHABLE;
659 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
660 0, (void *)r_net, SCTP_SO_NOT_LOCKED);
661 }
662 if (r_net->dest_state & SCTP_ADDR_PF) {
663 r_net->dest_state &= ~SCTP_ADDR_PF;
664 stcb->asoc.cc_functions.sctp_cwnd_update_exit_pf(stcb, net);
665 }
666 if (old_error_counter > 0) {
667 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
668 stcb, r_net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_5);
669 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, r_net);
670 }
671 if (r_net == stcb->asoc.primary_destination) {
672 if (stcb->asoc.alternate) {
673 /* release the alternate, primary is good */
674 sctp_free_remote_addr(stcb->asoc.alternate);
675 stcb->asoc.alternate = NULL;
676 }
677 }
678 /* Mobility adaptation */
679 if (req_prim) {
680 if ((sctp_is_mobility_feature_on(stcb->sctp_ep,
681 SCTP_MOBILITY_BASE) ||
682 sctp_is_mobility_feature_on(stcb->sctp_ep,
683 SCTP_MOBILITY_FASTHANDOFF)) &&
684 sctp_is_mobility_feature_on(stcb->sctp_ep,
685 SCTP_MOBILITY_PRIM_DELETED)) {
686
687 sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED,
688 stcb->sctp_ep, stcb, NULL,
689 SCTP_FROM_SCTP_INPUT + SCTP_LOC_6);
690 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
691 SCTP_MOBILITY_FASTHANDOFF)) {
692 sctp_assoc_immediate_retrans(stcb,
693 stcb->asoc.primary_destination);
694 }
695 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
696 SCTP_MOBILITY_BASE)) {
697 sctp_move_chunks_from_net(stcb,
698 stcb->asoc.deleted_primary);
699 }
700 sctp_delete_prim_timer(stcb->sctp_ep, stcb,
701 stcb->asoc.deleted_primary);
702 }
703 }
704 }
705
706 static int
sctp_handle_nat_colliding_state(struct sctp_tcb * stcb)707 sctp_handle_nat_colliding_state(struct sctp_tcb *stcb)
708 {
709 /*
710 * Return 0 means we want you to proceed with the abort non-zero
711 * means no abort processing.
712 */
713 uint32_t new_vtag;
714 struct sctpasochead *head;
715
716 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
717 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
718 new_vtag = sctp_select_a_tag(stcb->sctp_ep, stcb->sctp_ep->sctp_lport, stcb->rport, 1);
719 atomic_add_int(&stcb->asoc.refcnt, 1);
720 SCTP_TCB_UNLOCK(stcb);
721 SCTP_INP_INFO_WLOCK();
722 SCTP_TCB_LOCK(stcb);
723 atomic_subtract_int(&stcb->asoc.refcnt, 1);
724 } else {
725 return (0);
726 }
727 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
728 /* generate a new vtag and send init */
729 LIST_REMOVE(stcb, sctp_asocs);
730 stcb->asoc.my_vtag = new_vtag;
731 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
732 /*
733 * put it in the bucket in the vtag hash of assoc's for the
734 * system
735 */
736 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
737 SCTP_INP_INFO_WUNLOCK();
738 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
739 return (1);
740 } else {
741 /*
742 * treat like a case where the cookie expired i.e.: - dump
743 * current cookie. - generate a new vtag. - resend init.
744 */
745 /* generate a new vtag and send init */
746 LIST_REMOVE(stcb, sctp_asocs);
747 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
748 sctp_stop_all_cookie_timers(stcb);
749 sctp_toss_old_cookies(stcb, &stcb->asoc);
750 stcb->asoc.my_vtag = new_vtag;
751 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
752 /*
753 * put it in the bucket in the vtag hash of assoc's for the
754 * system
755 */
756 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
757 SCTP_INP_INFO_WUNLOCK();
758 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
759 return (1);
760 }
761 return (0);
762 }
763
764 static int
sctp_handle_nat_missing_state(struct sctp_tcb * stcb,struct sctp_nets * net)765 sctp_handle_nat_missing_state(struct sctp_tcb *stcb,
766 struct sctp_nets *net)
767 {
768 /*
769 * return 0 means we want you to proceed with the abort non-zero
770 * means no abort processing
771 */
772 if (stcb->asoc.auth_supported == 0) {
773 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_nat_missing_state: Peer does not support AUTH, cannot send an asconf\n");
774 return (0);
775 }
776 sctp_asconf_send_nat_state_update(stcb, net);
777 return (1);
778 }
779
780
781 /* Returns 1 if the stcb was aborted, 0 otherwise */
782 static int
sctp_handle_abort(struct sctp_abort_chunk * abort,struct sctp_tcb * stcb,struct sctp_nets * net)783 sctp_handle_abort(struct sctp_abort_chunk *abort,
784 struct sctp_tcb *stcb, struct sctp_nets *net)
785 {
786 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
787 struct socket *so;
788 #endif
789 uint16_t len;
790 uint16_t error;
791
792 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: handling ABORT\n");
793 if (stcb == NULL)
794 return (0);
795
796 len = ntohs(abort->ch.chunk_length);
797 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_error_cause)) {
798 /*
799 * Need to check the cause codes for our two magic nat
800 * aborts which don't kill the assoc necessarily.
801 */
802 struct sctp_error_cause *cause;
803
804 cause = (struct sctp_error_cause *)(abort + 1);
805 error = ntohs(cause->code);
806 if (error == SCTP_CAUSE_NAT_COLLIDING_STATE) {
807 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags:%x\n",
808 abort->ch.chunk_flags);
809 if (sctp_handle_nat_colliding_state(stcb)) {
810 return (0);
811 }
812 } else if (error == SCTP_CAUSE_NAT_MISSING_STATE) {
813 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags:%x\n",
814 abort->ch.chunk_flags);
815 if (sctp_handle_nat_missing_state(stcb, net)) {
816 return (0);
817 }
818 }
819 } else {
820 error = 0;
821 }
822 /* stop any receive timers */
823 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, net,
824 SCTP_FROM_SCTP_INPUT + SCTP_LOC_7);
825 /* notify user of the abort and clean up... */
826 sctp_abort_notification(stcb, 1, error, abort, SCTP_SO_NOT_LOCKED);
827 /* free the tcb */
828 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
829 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
830 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
831 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
832 }
833 #ifdef SCTP_ASOCLOG_OF_TSNS
834 sctp_print_out_track_log(stcb);
835 #endif
836 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
837 so = SCTP_INP_SO(stcb->sctp_ep);
838 atomic_add_int(&stcb->asoc.refcnt, 1);
839 SCTP_TCB_UNLOCK(stcb);
840 SCTP_SOCKET_LOCK(so, 1);
841 SCTP_TCB_LOCK(stcb);
842 atomic_subtract_int(&stcb->asoc.refcnt, 1);
843 #endif
844 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
845 SCTP_FROM_SCTP_INPUT + SCTP_LOC_8);
846 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
847 SCTP_SOCKET_UNLOCK(so, 1);
848 #endif
849 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_handle_abort: finished\n");
850 return (1);
851 }
852
853 static void
sctp_start_net_timers(struct sctp_tcb * stcb)854 sctp_start_net_timers(struct sctp_tcb *stcb)
855 {
856 uint32_t cnt_hb_sent;
857 struct sctp_nets *net;
858
859 cnt_hb_sent = 0;
860 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
861 /*
862 * For each network start: 1) A pmtu timer. 2) A HB timer 3)
863 * If the dest in unconfirmed send a hb as well if under
864 * max_hb_burst have been sent.
865 */
866 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb, net);
867 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep, stcb, net);
868 if ((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
869 (cnt_hb_sent < SCTP_BASE_SYSCTL(sctp_hb_maxburst))) {
870 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
871 cnt_hb_sent++;
872 }
873 }
874 if (cnt_hb_sent) {
875 sctp_chunk_output(stcb->sctp_ep, stcb,
876 SCTP_OUTPUT_FROM_COOKIE_ACK,
877 SCTP_SO_NOT_LOCKED);
878 }
879 }
880
881
882 static void
sctp_handle_shutdown(struct sctp_shutdown_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_flag)883 sctp_handle_shutdown(struct sctp_shutdown_chunk *cp,
884 struct sctp_tcb *stcb, struct sctp_nets *net, int *abort_flag)
885 {
886 struct sctp_association *asoc;
887 int some_on_streamwheel;
888 int old_state;
889 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
890 struct socket *so;
891 #endif
892
893 SCTPDBG(SCTP_DEBUG_INPUT2,
894 "sctp_handle_shutdown: handling SHUTDOWN\n");
895 if (stcb == NULL)
896 return;
897 asoc = &stcb->asoc;
898 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
899 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
900 return;
901 }
902 if (ntohs(cp->ch.chunk_length) != sizeof(struct sctp_shutdown_chunk)) {
903 /* Shutdown NOT the expected size */
904 return;
905 }
906 old_state = SCTP_GET_STATE(stcb);
907 sctp_update_acked(stcb, cp, abort_flag);
908 if (*abort_flag) {
909 return;
910 }
911 if (asoc->control_pdapi) {
912 /*
913 * With a normal shutdown we assume the end of last record.
914 */
915 SCTP_INP_READ_LOCK(stcb->sctp_ep);
916 if (asoc->control_pdapi->on_strm_q) {
917 struct sctp_stream_in *strm;
918
919 strm = &asoc->strmin[asoc->control_pdapi->sinfo_stream];
920 if (asoc->control_pdapi->on_strm_q == SCTP_ON_UNORDERED) {
921 /* Unordered */
922 TAILQ_REMOVE(&strm->uno_inqueue, asoc->control_pdapi, next_instrm);
923 asoc->control_pdapi->on_strm_q = 0;
924 } else if (asoc->control_pdapi->on_strm_q == SCTP_ON_ORDERED) {
925 /* Ordered */
926 TAILQ_REMOVE(&strm->inqueue, asoc->control_pdapi, next_instrm);
927 asoc->control_pdapi->on_strm_q = 0;
928 #ifdef INVARIANTS
929 } else {
930 panic("Unknown state on ctrl:%p on_strm_q:%d",
931 asoc->control_pdapi,
932 asoc->control_pdapi->on_strm_q);
933 #endif
934 }
935 }
936 asoc->control_pdapi->end_added = 1;
937 asoc->control_pdapi->pdapi_aborted = 1;
938 asoc->control_pdapi = NULL;
939 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
940 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
941 so = SCTP_INP_SO(stcb->sctp_ep);
942 atomic_add_int(&stcb->asoc.refcnt, 1);
943 SCTP_TCB_UNLOCK(stcb);
944 SCTP_SOCKET_LOCK(so, 1);
945 SCTP_TCB_LOCK(stcb);
946 atomic_subtract_int(&stcb->asoc.refcnt, 1);
947 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
948 /* assoc was freed while we were unlocked */
949 SCTP_SOCKET_UNLOCK(so, 1);
950 return;
951 }
952 #endif
953 if (stcb->sctp_socket) {
954 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
955 }
956 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
957 SCTP_SOCKET_UNLOCK(so, 1);
958 #endif
959 }
960 /* goto SHUTDOWN_RECEIVED state to block new requests */
961 if (stcb->sctp_socket) {
962 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
963 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) &&
964 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
965 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_RECEIVED);
966 /*
967 * notify upper layer that peer has initiated a
968 * shutdown
969 */
970 sctp_ulp_notify(SCTP_NOTIFY_PEER_SHUTDOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
971
972 /* reset time */
973 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
974 }
975 }
976 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) {
977 /*
978 * stop the shutdown timer, since we WILL move to
979 * SHUTDOWN-ACK-SENT.
980 */
981 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
982 net, SCTP_FROM_SCTP_INPUT + SCTP_LOC_9);
983 }
984 /* Now is there unsent data on a stream somewhere? */
985 some_on_streamwheel = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
986
987 if (!TAILQ_EMPTY(&asoc->send_queue) ||
988 !TAILQ_EMPTY(&asoc->sent_queue) ||
989 some_on_streamwheel) {
990 /* By returning we will push more data out */
991 return;
992 } else {
993 /* no outstanding data to send, so move on... */
994 /* send SHUTDOWN-ACK */
995 /* move to SHUTDOWN-ACK-SENT state */
996 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
997 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
998 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
999 }
1000 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
1001 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_ACK_SENT);
1002 sctp_stop_timers_for_shutdown(stcb);
1003 sctp_send_shutdown_ack(stcb, net);
1004 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
1005 stcb->sctp_ep, stcb, net);
1006 } else if (old_state == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1007 sctp_send_shutdown_ack(stcb, net);
1008 }
1009 }
1010 }
1011
1012 static void
sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)1013 sctp_handle_shutdown_ack(struct sctp_shutdown_ack_chunk *cp SCTP_UNUSED,
1014 struct sctp_tcb *stcb,
1015 struct sctp_nets *net)
1016 {
1017 struct sctp_association *asoc;
1018 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1019 struct socket *so;
1020
1021 so = SCTP_INP_SO(stcb->sctp_ep);
1022 #endif
1023 SCTPDBG(SCTP_DEBUG_INPUT2,
1024 "sctp_handle_shutdown_ack: handling SHUTDOWN ACK\n");
1025 if (stcb == NULL)
1026 return;
1027
1028 asoc = &stcb->asoc;
1029 /* process according to association state */
1030 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1031 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1032 /* unexpected SHUTDOWN-ACK... do OOTB handling... */
1033 sctp_send_shutdown_complete(stcb, net, 1);
1034 SCTP_TCB_UNLOCK(stcb);
1035 return;
1036 }
1037 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
1038 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1039 /* unexpected SHUTDOWN-ACK... so ignore... */
1040 SCTP_TCB_UNLOCK(stcb);
1041 return;
1042 }
1043 if (asoc->control_pdapi) {
1044 /*
1045 * With a normal shutdown we assume the end of last record.
1046 */
1047 SCTP_INP_READ_LOCK(stcb->sctp_ep);
1048 asoc->control_pdapi->end_added = 1;
1049 asoc->control_pdapi->pdapi_aborted = 1;
1050 asoc->control_pdapi = NULL;
1051 SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
1052 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1053 atomic_add_int(&stcb->asoc.refcnt, 1);
1054 SCTP_TCB_UNLOCK(stcb);
1055 SCTP_SOCKET_LOCK(so, 1);
1056 SCTP_TCB_LOCK(stcb);
1057 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1058 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1059 /* assoc was freed while we were unlocked */
1060 SCTP_SOCKET_UNLOCK(so, 1);
1061 return;
1062 }
1063 #endif
1064 sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1065 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1066 SCTP_SOCKET_UNLOCK(so, 1);
1067 #endif
1068 }
1069 #ifdef INVARIANTS
1070 if (!TAILQ_EMPTY(&asoc->send_queue) ||
1071 !TAILQ_EMPTY(&asoc->sent_queue) ||
1072 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
1073 panic("Queues are not empty when handling SHUTDOWN-ACK");
1074 }
1075 #endif
1076 /* stop the timer */
1077 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, net,
1078 SCTP_FROM_SCTP_INPUT + SCTP_LOC_10);
1079 /* send SHUTDOWN-COMPLETE */
1080 sctp_send_shutdown_complete(stcb, net, 0);
1081 /* notify upper layer protocol */
1082 if (stcb->sctp_socket) {
1083 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1084 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1085 stcb->sctp_socket->so_snd.sb_cc = 0;
1086 }
1087 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
1088 }
1089 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
1090 /* free the TCB but first save off the ep */
1091 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1092 atomic_add_int(&stcb->asoc.refcnt, 1);
1093 SCTP_TCB_UNLOCK(stcb);
1094 SCTP_SOCKET_LOCK(so, 1);
1095 SCTP_TCB_LOCK(stcb);
1096 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1097 #endif
1098 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1099 SCTP_FROM_SCTP_INPUT + SCTP_LOC_11);
1100 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1101 SCTP_SOCKET_UNLOCK(so, 1);
1102 #endif
1103 }
1104
1105 static void
sctp_process_unrecog_chunk(struct sctp_tcb * stcb,uint8_t chunk_type,struct sctp_nets * net)1106 sctp_process_unrecog_chunk(struct sctp_tcb *stcb, uint8_t chunk_type,
1107 struct sctp_nets *net)
1108 {
1109 switch (chunk_type) {
1110 case SCTP_ASCONF_ACK:
1111 case SCTP_ASCONF:
1112 sctp_asconf_cleanup(stcb, net);
1113 break;
1114 case SCTP_IFORWARD_CUM_TSN:
1115 case SCTP_FORWARD_CUM_TSN:
1116 stcb->asoc.prsctp_supported = 0;
1117 break;
1118 default:
1119 SCTPDBG(SCTP_DEBUG_INPUT2,
1120 "Peer does not support chunk type %d (0x%x).\n",
1121 chunk_type, chunk_type);
1122 break;
1123 }
1124 }
1125
1126 /*
1127 * Skip past the param header and then we will find the param that caused the
1128 * problem. There are a number of param's in a ASCONF OR the prsctp param
1129 * these will turn of specific features.
1130 * XXX: Is this the right thing to do?
1131 */
1132 static void
sctp_process_unrecog_param(struct sctp_tcb * stcb,uint16_t parameter_type)1133 sctp_process_unrecog_param(struct sctp_tcb *stcb, uint16_t parameter_type)
1134 {
1135 switch (parameter_type) {
1136 /* pr-sctp draft */
1137 case SCTP_PRSCTP_SUPPORTED:
1138 stcb->asoc.prsctp_supported = 0;
1139 break;
1140 case SCTP_SUPPORTED_CHUNK_EXT:
1141 break;
1142 /* draft-ietf-tsvwg-addip-sctp */
1143 case SCTP_HAS_NAT_SUPPORT:
1144 stcb->asoc.peer_supports_nat = 0;
1145 break;
1146 case SCTP_ADD_IP_ADDRESS:
1147 case SCTP_DEL_IP_ADDRESS:
1148 case SCTP_SET_PRIM_ADDR:
1149 stcb->asoc.asconf_supported = 0;
1150 break;
1151 case SCTP_SUCCESS_REPORT:
1152 case SCTP_ERROR_CAUSE_IND:
1153 SCTPDBG(SCTP_DEBUG_INPUT2, "Huh, the peer does not support success? or error cause?\n");
1154 SCTPDBG(SCTP_DEBUG_INPUT2,
1155 "Turning off ASCONF to this strange peer\n");
1156 stcb->asoc.asconf_supported = 0;
1157 break;
1158 default:
1159 SCTPDBG(SCTP_DEBUG_INPUT2,
1160 "Peer does not support param type %d (0x%x)??\n",
1161 parameter_type, parameter_type);
1162 break;
1163 }
1164 }
1165
1166 static int
sctp_handle_error(struct sctp_chunkhdr * ch,struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t limit)1167 sctp_handle_error(struct sctp_chunkhdr *ch,
1168 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
1169 {
1170 struct sctp_error_cause *cause;
1171 struct sctp_association *asoc;
1172 uint32_t remaining_length, adjust;
1173 uint16_t code, cause_code, cause_length;
1174 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1175 struct socket *so;
1176 #endif
1177
1178 /* parse through all of the errors and process */
1179 asoc = &stcb->asoc;
1180 cause = (struct sctp_error_cause *)((caddr_t)ch +
1181 sizeof(struct sctp_chunkhdr));
1182 remaining_length = ntohs(ch->chunk_length);
1183 if (remaining_length > limit) {
1184 remaining_length = limit;
1185 }
1186 if (remaining_length >= sizeof(struct sctp_chunkhdr)) {
1187 remaining_length -= sizeof(struct sctp_chunkhdr);
1188 } else {
1189 remaining_length = 0;
1190 }
1191 code = 0;
1192 while (remaining_length >= sizeof(struct sctp_error_cause)) {
1193 /* Process an Error Cause */
1194 cause_code = ntohs(cause->code);
1195 cause_length = ntohs(cause->length);
1196 if ((cause_length > remaining_length) || (cause_length == 0)) {
1197 /* Invalid cause length, possibly due to truncation. */
1198 SCTPDBG(SCTP_DEBUG_INPUT1, "Bogus length in cause - bytes left: %u cause length: %u\n",
1199 remaining_length, cause_length);
1200 return (0);
1201 }
1202 if (code == 0) {
1203 /* report the first error cause */
1204 code = cause_code;
1205 }
1206 switch (cause_code) {
1207 case SCTP_CAUSE_INVALID_STREAM:
1208 case SCTP_CAUSE_MISSING_PARAM:
1209 case SCTP_CAUSE_INVALID_PARAM:
1210 case SCTP_CAUSE_NO_USER_DATA:
1211 SCTPDBG(SCTP_DEBUG_INPUT1, "Software error we got a %u back? We have a bug :/ (or do they?)\n",
1212 cause_code);
1213 break;
1214 case SCTP_CAUSE_NAT_COLLIDING_STATE:
1215 SCTPDBG(SCTP_DEBUG_INPUT2, "Received Colliding state abort flags: %x\n",
1216 ch->chunk_flags);
1217 if (sctp_handle_nat_colliding_state(stcb)) {
1218 return (0);
1219 }
1220 break;
1221 case SCTP_CAUSE_NAT_MISSING_STATE:
1222 SCTPDBG(SCTP_DEBUG_INPUT2, "Received missing state abort flags: %x\n",
1223 ch->chunk_flags);
1224 if (sctp_handle_nat_missing_state(stcb, net)) {
1225 return (0);
1226 }
1227 break;
1228 case SCTP_CAUSE_STALE_COOKIE:
1229 /*
1230 * We only act if we have echoed a cookie and are
1231 * waiting.
1232 */
1233 if ((cause_length >= sizeof(struct sctp_error_stale_cookie)) &&
1234 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1235 struct sctp_error_stale_cookie *stale_cookie;
1236
1237 stale_cookie = (struct sctp_error_stale_cookie *)cause;
1238 asoc->cookie_preserve_req = ntohl(stale_cookie->stale_time);
1239 /* Double it to be more robust on RTX */
1240 if (asoc->cookie_preserve_req <= UINT32_MAX / 2) {
1241 asoc->cookie_preserve_req *= 2;
1242 } else {
1243 asoc->cookie_preserve_req = UINT32_MAX;
1244 }
1245 asoc->stale_cookie_count++;
1246 if (asoc->stale_cookie_count >
1247 asoc->max_init_times) {
1248 sctp_abort_notification(stcb, 0, 0, NULL, SCTP_SO_NOT_LOCKED);
1249 /* now free the asoc */
1250 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1251 so = SCTP_INP_SO(stcb->sctp_ep);
1252 atomic_add_int(&stcb->asoc.refcnt, 1);
1253 SCTP_TCB_UNLOCK(stcb);
1254 SCTP_SOCKET_LOCK(so, 1);
1255 SCTP_TCB_LOCK(stcb);
1256 atomic_subtract_int(&stcb->asoc.refcnt, 1);
1257 #endif
1258 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
1259 SCTP_FROM_SCTP_INPUT + SCTP_LOC_12);
1260 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1261 SCTP_SOCKET_UNLOCK(so, 1);
1262 #endif
1263 return (-1);
1264 }
1265 /* blast back to INIT state */
1266 sctp_toss_old_cookies(stcb, &stcb->asoc);
1267 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1268 sctp_stop_all_cookie_timers(stcb);
1269 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
1270 }
1271 break;
1272 case SCTP_CAUSE_UNRESOLVABLE_ADDR:
1273 /*
1274 * Nothing we can do here, we don't do hostname
1275 * addresses so if the peer does not like my IPv6
1276 * (or IPv4 for that matter) it does not matter. If
1277 * they don't support that type of address, they can
1278 * NOT possibly get that packet type... i.e. with no
1279 * IPv6 you can't receive a IPv6 packet. so we can
1280 * safely ignore this one. If we ever added support
1281 * for HOSTNAME Addresses, then we would need to do
1282 * something here.
1283 */
1284 break;
1285 case SCTP_CAUSE_UNRECOG_CHUNK:
1286 if (cause_length >= sizeof(struct sctp_error_unrecognized_chunk)) {
1287 struct sctp_error_unrecognized_chunk *unrec_chunk;
1288
1289 unrec_chunk = (struct sctp_error_unrecognized_chunk *)cause;
1290 sctp_process_unrecog_chunk(stcb, unrec_chunk->ch.chunk_type, net);
1291 }
1292 break;
1293 case SCTP_CAUSE_UNRECOG_PARAM:
1294 /* XXX: We only consider the first parameter */
1295 if (cause_length >= sizeof(struct sctp_error_cause) + sizeof(struct sctp_paramhdr)) {
1296 struct sctp_paramhdr *unrec_parameter;
1297
1298 unrec_parameter = (struct sctp_paramhdr *)(cause + 1);
1299 sctp_process_unrecog_param(stcb, ntohs(unrec_parameter->param_type));
1300 }
1301 break;
1302 case SCTP_CAUSE_COOKIE_IN_SHUTDOWN:
1303 /*
1304 * We ignore this since the timer will drive out a
1305 * new cookie anyway and there timer will drive us
1306 * to send a SHUTDOWN_COMPLETE. We can't send one
1307 * here since we don't have their tag.
1308 */
1309 break;
1310 case SCTP_CAUSE_DELETING_LAST_ADDR:
1311 case SCTP_CAUSE_RESOURCE_SHORTAGE:
1312 case SCTP_CAUSE_DELETING_SRC_ADDR:
1313 /*
1314 * We should NOT get these here, but in a
1315 * ASCONF-ACK.
1316 */
1317 SCTPDBG(SCTP_DEBUG_INPUT2, "Peer sends ASCONF errors in a error cause with code %u.\n",
1318 cause_code);
1319 break;
1320 case SCTP_CAUSE_OUT_OF_RESC:
1321 /*
1322 * And what, pray tell do we do with the fact that
1323 * the peer is out of resources? Not really sure we
1324 * could do anything but abort. I suspect this
1325 * should have came WITH an abort instead of in a
1326 * OP-ERROR.
1327 */
1328 break;
1329 default:
1330 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_handle_error: unknown code 0x%x\n",
1331 cause_code);
1332 break;
1333 }
1334 adjust = SCTP_SIZE32(cause_length);
1335 if (remaining_length >= adjust) {
1336 remaining_length -= adjust;
1337 } else {
1338 remaining_length = 0;
1339 }
1340 cause = (struct sctp_error_cause *)((caddr_t)cause + adjust);
1341 }
1342 sctp_ulp_notify(SCTP_NOTIFY_REMOTE_ERROR, stcb, code, ch, SCTP_SO_NOT_LOCKED);
1343 return (0);
1344 }
1345
1346 static int
sctp_handle_init_ack(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_ack_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,int * abort_no_unlock,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id)1347 sctp_handle_init_ack(struct mbuf *m, int iphlen, int offset,
1348 struct sockaddr *src, struct sockaddr *dst, struct sctphdr *sh,
1349 struct sctp_init_ack_chunk *cp, struct sctp_tcb *stcb,
1350 struct sctp_nets *net, int *abort_no_unlock,
1351 uint8_t mflowtype, uint32_t mflowid,
1352 uint32_t vrf_id)
1353 {
1354 struct sctp_init_ack *init_ack;
1355 struct mbuf *op_err;
1356
1357 SCTPDBG(SCTP_DEBUG_INPUT2,
1358 "sctp_handle_init_ack: handling INIT-ACK\n");
1359
1360 if (stcb == NULL) {
1361 SCTPDBG(SCTP_DEBUG_INPUT2,
1362 "sctp_handle_init_ack: TCB is null\n");
1363 return (-1);
1364 }
1365 if (ntohs(cp->ch.chunk_length) < sizeof(struct sctp_init_ack_chunk)) {
1366 /* Invalid length */
1367 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1368 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1369 src, dst, sh, op_err,
1370 mflowtype, mflowid,
1371 vrf_id, net->port);
1372 *abort_no_unlock = 1;
1373 return (-1);
1374 }
1375 init_ack = &cp->init;
1376 /* validate parameters */
1377 if (init_ack->initiate_tag == 0) {
1378 /* protocol error... send an abort */
1379 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1380 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1381 src, dst, sh, op_err,
1382 mflowtype, mflowid,
1383 vrf_id, net->port);
1384 *abort_no_unlock = 1;
1385 return (-1);
1386 }
1387 if (ntohl(init_ack->a_rwnd) < SCTP_MIN_RWND) {
1388 /* protocol error... send an abort */
1389 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1390 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1391 src, dst, sh, op_err,
1392 mflowtype, mflowid,
1393 vrf_id, net->port);
1394 *abort_no_unlock = 1;
1395 return (-1);
1396 }
1397 if (init_ack->num_inbound_streams == 0) {
1398 /* protocol error... send an abort */
1399 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1400 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1401 src, dst, sh, op_err,
1402 mflowtype, mflowid,
1403 vrf_id, net->port);
1404 *abort_no_unlock = 1;
1405 return (-1);
1406 }
1407 if (init_ack->num_outbound_streams == 0) {
1408 /* protocol error... send an abort */
1409 op_err = sctp_generate_cause(SCTP_CAUSE_INVALID_PARAM, "");
1410 sctp_abort_association(stcb->sctp_ep, stcb, m, iphlen,
1411 src, dst, sh, op_err,
1412 mflowtype, mflowid,
1413 vrf_id, net->port);
1414 *abort_no_unlock = 1;
1415 return (-1);
1416 }
1417 /* process according to association state... */
1418 switch (SCTP_GET_STATE(stcb)) {
1419 case SCTP_STATE_COOKIE_WAIT:
1420 /* this is the expected state for this chunk */
1421 /* process the INIT-ACK parameters */
1422 if (stcb->asoc.primary_destination->dest_state &
1423 SCTP_ADDR_UNCONFIRMED) {
1424 /*
1425 * The primary is where we sent the INIT, we can
1426 * always consider it confirmed when the INIT-ACK is
1427 * returned. Do this before we load addresses
1428 * though.
1429 */
1430 stcb->asoc.primary_destination->dest_state &=
1431 ~SCTP_ADDR_UNCONFIRMED;
1432 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
1433 stcb, 0, (void *)stcb->asoc.primary_destination, SCTP_SO_NOT_LOCKED);
1434 }
1435 if (sctp_process_init_ack(m, iphlen, offset, src, dst, sh, cp, stcb,
1436 net, abort_no_unlock,
1437 mflowtype, mflowid,
1438 vrf_id) < 0) {
1439 /* error in parsing parameters */
1440 return (-1);
1441 }
1442 /* update our state */
1443 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to COOKIE-ECHOED state\n");
1444 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_ECHOED);
1445
1446 /* reset the RTO calc */
1447 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
1448 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
1449 stcb->asoc.overall_error_count,
1450 0,
1451 SCTP_FROM_SCTP_INPUT,
1452 __LINE__);
1453 }
1454 stcb->asoc.overall_error_count = 0;
1455 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1456 /*
1457 * collapse the init timer back in case of a exponential
1458 * backoff
1459 */
1460 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, stcb->sctp_ep,
1461 stcb, net);
1462 /*
1463 * the send at the end of the inbound data processing will
1464 * cause the cookie to be sent
1465 */
1466 break;
1467 case SCTP_STATE_SHUTDOWN_SENT:
1468 /* incorrect state... discard */
1469 break;
1470 case SCTP_STATE_COOKIE_ECHOED:
1471 /* incorrect state... discard */
1472 break;
1473 case SCTP_STATE_OPEN:
1474 /* incorrect state... discard */
1475 break;
1476 case SCTP_STATE_EMPTY:
1477 case SCTP_STATE_INUSE:
1478 default:
1479 /* incorrect state... discard */
1480 return (-1);
1481 break;
1482 }
1483 SCTPDBG(SCTP_DEBUG_INPUT1, "Leaving handle-init-ack end\n");
1484 return (0);
1485 }
1486
1487 static struct sctp_tcb *
1488 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
1489 struct sockaddr *src, struct sockaddr *dst,
1490 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1491 struct sctp_inpcb *inp, struct sctp_nets **netp,
1492 struct sockaddr *init_src, int *notification,
1493 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1494 uint8_t mflowtype, uint32_t mflowid,
1495 uint32_t vrf_id, uint16_t port);
1496
1497
1498 /*
1499 * handle a state cookie for an existing association m: input packet mbuf
1500 * chain-- assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a
1501 * "split" mbuf and the cookie signature does not exist offset: offset into
1502 * mbuf to the cookie-echo chunk
1503 */
1504 static struct sctp_tcb *
sctp_process_cookie_existing(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_state_cookie * cookie,int cookie_len,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets ** netp,struct sockaddr * init_src,int * notification,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)1505 sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
1506 struct sockaddr *src, struct sockaddr *dst,
1507 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
1508 struct sctp_inpcb *inp, struct sctp_tcb *stcb, struct sctp_nets **netp,
1509 struct sockaddr *init_src, int *notification,
1510 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
1511 uint8_t mflowtype, uint32_t mflowid,
1512 uint32_t vrf_id, uint16_t port)
1513 {
1514 struct sctp_association *asoc;
1515 struct sctp_init_chunk *init_cp, init_buf;
1516 struct sctp_init_ack_chunk *initack_cp, initack_buf;
1517 struct sctp_nets *net;
1518 struct mbuf *op_err;
1519 struct timeval old;
1520 int init_offset, initack_offset, i;
1521 int retval;
1522 int spec_flag = 0;
1523 uint32_t how_indx;
1524 #if defined(SCTP_DETAILED_STR_STATS)
1525 int j;
1526 #endif
1527
1528 net = *netp;
1529 /* I know that the TCB is non-NULL from the caller */
1530 asoc = &stcb->asoc;
1531 for (how_indx = 0; how_indx < sizeof(asoc->cookie_how); how_indx++) {
1532 if (asoc->cookie_how[how_indx] == 0)
1533 break;
1534 }
1535 if (how_indx < sizeof(asoc->cookie_how)) {
1536 asoc->cookie_how[how_indx] = 1;
1537 }
1538 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
1539 /* SHUTDOWN came in after sending INIT-ACK */
1540 sctp_send_shutdown_ack(stcb, stcb->asoc.primary_destination);
1541 op_err = sctp_generate_cause(SCTP_CAUSE_COOKIE_IN_SHUTDOWN, "");
1542 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
1543 mflowtype, mflowid, inp->fibnum,
1544 vrf_id, net->port);
1545 if (how_indx < sizeof(asoc->cookie_how))
1546 asoc->cookie_how[how_indx] = 2;
1547 return (NULL);
1548 }
1549 /*
1550 * find and validate the INIT chunk in the cookie (peer's info) the
1551 * INIT should start after the cookie-echo header struct (chunk
1552 * header, state cookie header struct)
1553 */
1554 init_offset = offset += sizeof(struct sctp_cookie_echo_chunk);
1555
1556 init_cp = (struct sctp_init_chunk *)
1557 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
1558 (uint8_t *)&init_buf);
1559 if (init_cp == NULL) {
1560 /* could not pull a INIT chunk in cookie */
1561 return (NULL);
1562 }
1563 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
1564 return (NULL);
1565 }
1566 /*
1567 * find and validate the INIT-ACK chunk in the cookie (my info) the
1568 * INIT-ACK follows the INIT chunk
1569 */
1570 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
1571 initack_cp = (struct sctp_init_ack_chunk *)
1572 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
1573 (uint8_t *)&initack_buf);
1574 if (initack_cp == NULL) {
1575 /* could not pull INIT-ACK chunk in cookie */
1576 return (NULL);
1577 }
1578 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
1579 return (NULL);
1580 }
1581 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1582 (ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag)) {
1583 /*
1584 * case D in Section 5.2.4 Table 2: MMAA process accordingly
1585 * to get into the OPEN state
1586 */
1587 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1588 /*-
1589 * Opps, this means that we somehow generated two vtag's
1590 * the same. I.e. we did:
1591 * Us Peer
1592 * <---INIT(tag=a)------
1593 * ----INIT-ACK(tag=t)-->
1594 * ----INIT(tag=t)------> *1
1595 * <---INIT-ACK(tag=a)---
1596 * <----CE(tag=t)------------- *2
1597 *
1598 * At point *1 we should be generating a different
1599 * tag t'. Which means we would throw away the CE and send
1600 * ours instead. Basically this is case C (throw away side).
1601 */
1602 if (how_indx < sizeof(asoc->cookie_how))
1603 asoc->cookie_how[how_indx] = 17;
1604 return (NULL);
1605
1606 }
1607 switch (SCTP_GET_STATE(stcb)) {
1608 case SCTP_STATE_COOKIE_WAIT:
1609 case SCTP_STATE_COOKIE_ECHOED:
1610 /*
1611 * INIT was sent but got a COOKIE_ECHO with the
1612 * correct tags... just accept it...but we must
1613 * process the init so that we can make sure we have
1614 * the right seq no's.
1615 */
1616 /* First we must process the INIT !! */
1617 retval = sctp_process_init(init_cp, stcb);
1618 if (retval < 0) {
1619 if (how_indx < sizeof(asoc->cookie_how))
1620 asoc->cookie_how[how_indx] = 3;
1621 return (NULL);
1622 }
1623 /* we have already processed the INIT so no problem */
1624 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp,
1625 stcb, net,
1626 SCTP_FROM_SCTP_INPUT + SCTP_LOC_13);
1627 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp,
1628 stcb, net,
1629 SCTP_FROM_SCTP_INPUT + SCTP_LOC_14);
1630 /* update current state */
1631 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1632 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1633 else
1634 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1635
1636 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1637 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1638 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1639 stcb->sctp_ep, stcb, asoc->primary_destination);
1640 }
1641 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1642 sctp_stop_all_cookie_timers(stcb);
1643 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1644 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1645 (!SCTP_IS_LISTENING(inp))) {
1646 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1647 struct socket *so;
1648 #endif
1649 /*
1650 * Here is where collision would go if we
1651 * did a connect() and instead got a
1652 * init/init-ack/cookie done before the
1653 * init-ack came back..
1654 */
1655 stcb->sctp_ep->sctp_flags |=
1656 SCTP_PCB_FLAGS_CONNECTED;
1657 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1658 so = SCTP_INP_SO(stcb->sctp_ep);
1659 atomic_add_int(&stcb->asoc.refcnt, 1);
1660 SCTP_TCB_UNLOCK(stcb);
1661 SCTP_SOCKET_LOCK(so, 1);
1662 SCTP_TCB_LOCK(stcb);
1663 atomic_add_int(&stcb->asoc.refcnt, -1);
1664 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1665 SCTP_SOCKET_UNLOCK(so, 1);
1666 return (NULL);
1667 }
1668 #endif
1669 soisconnected(stcb->sctp_socket);
1670 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1671 SCTP_SOCKET_UNLOCK(so, 1);
1672 #endif
1673 }
1674 /* notify upper layer */
1675 *notification = SCTP_NOTIFY_ASSOC_UP;
1676 /*
1677 * since we did not send a HB make sure we don't
1678 * double things
1679 */
1680 old.tv_sec = cookie->time_entered.tv_sec;
1681 old.tv_usec = cookie->time_entered.tv_usec;
1682 net->hb_responded = 1;
1683 sctp_calculate_rto(stcb, asoc, net, &old,
1684 SCTP_RTT_FROM_NON_DATA);
1685
1686 if (stcb->asoc.sctp_autoclose_ticks &&
1687 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE))) {
1688 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
1689 inp, stcb, NULL);
1690 }
1691 break;
1692 default:
1693 /*
1694 * we're in the OPEN state (or beyond), so peer must
1695 * have simply lost the COOKIE-ACK
1696 */
1697 break;
1698 } /* end switch */
1699 sctp_stop_all_cookie_timers(stcb);
1700 /*
1701 * We ignore the return code here.. not sure if we should
1702 * somehow abort.. but we do have an existing asoc. This
1703 * really should not fail.
1704 */
1705 if (sctp_load_addresses_from_init(stcb, m,
1706 init_offset + sizeof(struct sctp_init_chunk),
1707 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1708 if (how_indx < sizeof(asoc->cookie_how))
1709 asoc->cookie_how[how_indx] = 4;
1710 return (NULL);
1711 }
1712 /* respond with a COOKIE-ACK */
1713 sctp_toss_old_cookies(stcb, asoc);
1714 sctp_send_cookie_ack(stcb);
1715 if (how_indx < sizeof(asoc->cookie_how))
1716 asoc->cookie_how[how_indx] = 5;
1717 return (stcb);
1718 }
1719
1720 if (ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1721 ntohl(init_cp->init.initiate_tag) == asoc->peer_vtag &&
1722 cookie->tie_tag_my_vtag == 0 &&
1723 cookie->tie_tag_peer_vtag == 0) {
1724 /*
1725 * case C in Section 5.2.4 Table 2: XMOO silently discard
1726 */
1727 if (how_indx < sizeof(asoc->cookie_how))
1728 asoc->cookie_how[how_indx] = 6;
1729 return (NULL);
1730 }
1731 /*
1732 * If nat support, and the below and stcb is established, send back
1733 * a ABORT(colliding state) if we are established.
1734 */
1735 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) &&
1736 (asoc->peer_supports_nat) &&
1737 ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1738 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1739 (asoc->peer_vtag == 0)))) {
1740 /*
1741 * Special case - Peer's support nat. We may have two init's
1742 * that we gave out the same tag on since one was not
1743 * established.. i.e. we get INIT from host-1 behind the nat
1744 * and we respond tag-a, we get a INIT from host-2 behind
1745 * the nat and we get tag-a again. Then we bring up host-1
1746 * (or 2's) assoc, Then comes the cookie from hsot-2 (or 1).
1747 * Now we have colliding state. We must send an abort here
1748 * with colliding state indication.
1749 */
1750 op_err = sctp_generate_cause(SCTP_CAUSE_NAT_COLLIDING_STATE, "");
1751 sctp_send_abort(m, iphlen, src, dst, sh, 0, op_err,
1752 mflowtype, mflowid, inp->fibnum,
1753 vrf_id, port);
1754 return (NULL);
1755 }
1756 if ((ntohl(initack_cp->init.initiate_tag) == asoc->my_vtag) &&
1757 ((ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) ||
1758 (asoc->peer_vtag == 0))) {
1759 /*
1760 * case B in Section 5.2.4 Table 2: MXAA or MOAA my info
1761 * should be ok, re-accept peer info
1762 */
1763 if (ntohl(initack_cp->init.initial_tsn) != asoc->init_seq_number) {
1764 /*
1765 * Extension of case C. If we hit this, then the
1766 * random number generator returned the same vtag
1767 * when we first sent our INIT-ACK and when we later
1768 * sent our INIT. The side with the seq numbers that
1769 * are different will be the one that normnally
1770 * would have hit case C. This in effect "extends"
1771 * our vtags in this collision case to be 64 bits.
1772 * The same collision could occur aka you get both
1773 * vtag and seq number the same twice in a row.. but
1774 * is much less likely. If it did happen then we
1775 * would proceed through and bring up the assoc.. we
1776 * may end up with the wrong stream setup however..
1777 * which would be bad.. but there is no way to
1778 * tell.. until we send on a stream that does not
1779 * exist :-)
1780 */
1781 if (how_indx < sizeof(asoc->cookie_how))
1782 asoc->cookie_how[how_indx] = 7;
1783
1784 return (NULL);
1785 }
1786 if (how_indx < sizeof(asoc->cookie_how))
1787 asoc->cookie_how[how_indx] = 8;
1788 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1789 SCTP_FROM_SCTP_INPUT + SCTP_LOC_15);
1790 sctp_stop_all_cookie_timers(stcb);
1791 /*
1792 * since we did not send a HB make sure we don't double
1793 * things
1794 */
1795 net->hb_responded = 1;
1796 if (stcb->asoc.sctp_autoclose_ticks &&
1797 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
1798 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb,
1799 NULL);
1800 }
1801 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
1802 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
1803
1804 if (ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) {
1805 /*
1806 * Ok the peer probably discarded our data (if we
1807 * echoed a cookie+data). So anything on the
1808 * sent_queue should be marked for retransmit, we
1809 * may not get something to kick us so it COULD
1810 * still take a timeout to move these.. but it can't
1811 * hurt to mark them.
1812 */
1813 struct sctp_tmit_chunk *chk;
1814
1815 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1816 if (chk->sent < SCTP_DATAGRAM_RESEND) {
1817 chk->sent = SCTP_DATAGRAM_RESEND;
1818 sctp_flight_size_decrease(chk);
1819 sctp_total_flight_decrease(stcb, chk);
1820 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
1821 spec_flag++;
1822 }
1823 }
1824
1825 }
1826 /* process the INIT info (peer's info) */
1827 retval = sctp_process_init(init_cp, stcb);
1828 if (retval < 0) {
1829 if (how_indx < sizeof(asoc->cookie_how))
1830 asoc->cookie_how[how_indx] = 9;
1831 return (NULL);
1832 }
1833 if (sctp_load_addresses_from_init(stcb, m,
1834 init_offset + sizeof(struct sctp_init_chunk),
1835 initack_offset, src, dst, init_src, stcb->asoc.port)) {
1836 if (how_indx < sizeof(asoc->cookie_how))
1837 asoc->cookie_how[how_indx] = 10;
1838 return (NULL);
1839 }
1840 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
1841 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
1842 *notification = SCTP_NOTIFY_ASSOC_UP;
1843
1844 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1845 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
1846 (!SCTP_IS_LISTENING(inp))) {
1847 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1848 struct socket *so;
1849 #endif
1850 stcb->sctp_ep->sctp_flags |=
1851 SCTP_PCB_FLAGS_CONNECTED;
1852 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1853 so = SCTP_INP_SO(stcb->sctp_ep);
1854 atomic_add_int(&stcb->asoc.refcnt, 1);
1855 SCTP_TCB_UNLOCK(stcb);
1856 SCTP_SOCKET_LOCK(so, 1);
1857 SCTP_TCB_LOCK(stcb);
1858 atomic_add_int(&stcb->asoc.refcnt, -1);
1859 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1860 SCTP_SOCKET_UNLOCK(so, 1);
1861 return (NULL);
1862 }
1863 #endif
1864 soisconnected(stcb->sctp_socket);
1865 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1866 SCTP_SOCKET_UNLOCK(so, 1);
1867 #endif
1868 }
1869 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)
1870 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
1871 else
1872 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1873 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1874 } else if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1875 SCTP_STAT_INCR_COUNTER32(sctps_restartestab);
1876 } else {
1877 SCTP_STAT_INCR_COUNTER32(sctps_collisionestab);
1878 }
1879 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1880 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1881 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1882 stcb->sctp_ep, stcb, asoc->primary_destination);
1883 }
1884 sctp_stop_all_cookie_timers(stcb);
1885 sctp_toss_old_cookies(stcb, asoc);
1886 sctp_send_cookie_ack(stcb);
1887 if (spec_flag) {
1888 /*
1889 * only if we have retrans set do we do this. What
1890 * this call does is get only the COOKIE-ACK out and
1891 * then when we return the normal call to
1892 * sctp_chunk_output will get the retrans out behind
1893 * this.
1894 */
1895 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_COOKIE_ACK, SCTP_SO_NOT_LOCKED);
1896 }
1897 if (how_indx < sizeof(asoc->cookie_how))
1898 asoc->cookie_how[how_indx] = 11;
1899
1900 return (stcb);
1901 }
1902 if ((ntohl(initack_cp->init.initiate_tag) != asoc->my_vtag &&
1903 ntohl(init_cp->init.initiate_tag) != asoc->peer_vtag) &&
1904 cookie->tie_tag_my_vtag == asoc->my_vtag_nonce &&
1905 cookie->tie_tag_peer_vtag == asoc->peer_vtag_nonce &&
1906 cookie->tie_tag_peer_vtag != 0) {
1907 struct sctpasochead *head;
1908 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1909 struct socket *so;
1910 #endif
1911
1912 if (asoc->peer_supports_nat) {
1913 /*
1914 * This is a gross gross hack. Just call the
1915 * cookie_new code since we are allowing a duplicate
1916 * association. I hope this works...
1917 */
1918 return (sctp_process_cookie_new(m, iphlen, offset, src, dst,
1919 sh, cookie, cookie_len,
1920 inp, netp, init_src, notification,
1921 auth_skipped, auth_offset, auth_len,
1922 mflowtype, mflowid,
1923 vrf_id, port));
1924 }
1925 /*
1926 * case A in Section 5.2.4 Table 2: XXMM (peer restarted)
1927 */
1928 /* temp code */
1929 if (how_indx < sizeof(asoc->cookie_how))
1930 asoc->cookie_how[how_indx] = 12;
1931 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
1932 SCTP_FROM_SCTP_INPUT + SCTP_LOC_16);
1933 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
1934 SCTP_FROM_SCTP_INPUT + SCTP_LOC_17);
1935
1936 /* notify upper layer */
1937 *notification = SCTP_NOTIFY_ASSOC_RESTART;
1938 atomic_add_int(&stcb->asoc.refcnt, 1);
1939 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_OPEN) &&
1940 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
1941 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT)) {
1942 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
1943 }
1944 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
1945 SCTP_STAT_INCR_GAUGE32(sctps_restartestab);
1946 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1947 SCTP_STAT_INCR_GAUGE32(sctps_collisionestab);
1948 }
1949 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
1950 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1951 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1952 stcb->sctp_ep, stcb, asoc->primary_destination);
1953
1954 } else if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) {
1955 /* move to OPEN state, if not in SHUTDOWN_SENT */
1956 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
1957 }
1958 asoc->pre_open_streams =
1959 ntohs(initack_cp->init.num_outbound_streams);
1960 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
1961 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
1962 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
1963
1964 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
1965
1966 asoc->str_reset_seq_in = asoc->init_seq_number;
1967
1968 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
1969 if (asoc->mapping_array) {
1970 memset(asoc->mapping_array, 0,
1971 asoc->mapping_array_size);
1972 }
1973 if (asoc->nr_mapping_array) {
1974 memset(asoc->nr_mapping_array, 0,
1975 asoc->mapping_array_size);
1976 }
1977 SCTP_TCB_UNLOCK(stcb);
1978 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1979 so = SCTP_INP_SO(stcb->sctp_ep);
1980 SCTP_SOCKET_LOCK(so, 1);
1981 #endif
1982 SCTP_INP_INFO_WLOCK();
1983 SCTP_INP_WLOCK(stcb->sctp_ep);
1984 SCTP_TCB_LOCK(stcb);
1985 atomic_add_int(&stcb->asoc.refcnt, -1);
1986 /* send up all the data */
1987 SCTP_TCB_SEND_LOCK(stcb);
1988
1989 sctp_report_all_outbound(stcb, 0, SCTP_SO_LOCKED);
1990 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
1991 stcb->asoc.strmout[i].chunks_on_queues = 0;
1992 #if defined(SCTP_DETAILED_STR_STATS)
1993 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1994 asoc->strmout[i].abandoned_sent[j] = 0;
1995 asoc->strmout[i].abandoned_unsent[j] = 0;
1996 }
1997 #else
1998 asoc->strmout[i].abandoned_sent[0] = 0;
1999 asoc->strmout[i].abandoned_unsent[0] = 0;
2000 #endif
2001 stcb->asoc.strmout[i].sid = i;
2002 stcb->asoc.strmout[i].next_mid_ordered = 0;
2003 stcb->asoc.strmout[i].next_mid_unordered = 0;
2004 stcb->asoc.strmout[i].last_msg_incomplete = 0;
2005 }
2006 /* process the INIT-ACK info (my info) */
2007 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2008 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2009
2010 /* pull from vtag hash */
2011 LIST_REMOVE(stcb, sctp_asocs);
2012 /* re-insert to new vtag position */
2013 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2014 SCTP_BASE_INFO(hashasocmark))];
2015 /*
2016 * put it in the bucket in the vtag hash of assoc's for the
2017 * system
2018 */
2019 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2020
2021 SCTP_TCB_SEND_UNLOCK(stcb);
2022 SCTP_INP_WUNLOCK(stcb->sctp_ep);
2023 SCTP_INP_INFO_WUNLOCK();
2024 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2025 SCTP_SOCKET_UNLOCK(so, 1);
2026 #endif
2027 asoc->total_flight = 0;
2028 asoc->total_flight_count = 0;
2029 /* process the INIT info (peer's info) */
2030 retval = sctp_process_init(init_cp, stcb);
2031 if (retval < 0) {
2032 if (how_indx < sizeof(asoc->cookie_how))
2033 asoc->cookie_how[how_indx] = 13;
2034
2035 return (NULL);
2036 }
2037 /*
2038 * since we did not send a HB make sure we don't double
2039 * things
2040 */
2041 net->hb_responded = 1;
2042
2043 if (sctp_load_addresses_from_init(stcb, m,
2044 init_offset + sizeof(struct sctp_init_chunk),
2045 initack_offset, src, dst, init_src, stcb->asoc.port)) {
2046 if (how_indx < sizeof(asoc->cookie_how))
2047 asoc->cookie_how[how_indx] = 14;
2048
2049 return (NULL);
2050 }
2051 /* respond with a COOKIE-ACK */
2052 sctp_stop_all_cookie_timers(stcb);
2053 sctp_toss_old_cookies(stcb, asoc);
2054 sctp_send_cookie_ack(stcb);
2055 if (how_indx < sizeof(asoc->cookie_how))
2056 asoc->cookie_how[how_indx] = 15;
2057
2058 return (stcb);
2059 }
2060 if (how_indx < sizeof(asoc->cookie_how))
2061 asoc->cookie_how[how_indx] = 16;
2062 /* all other cases... */
2063 return (NULL);
2064 }
2065
2066
2067 /*
2068 * handle a state cookie for a new association m: input packet mbuf chain--
2069 * assumes a pullup on IP/SCTP/COOKIE-ECHO chunk note: this is a "split" mbuf
2070 * and the cookie signature does not exist offset: offset into mbuf to the
2071 * cookie-echo chunk length: length of the cookie chunk to: where the init
2072 * was from returns a new TCB
2073 */
2074 static struct sctp_tcb *
sctp_process_cookie_new(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_state_cookie * cookie,int cookie_len,struct sctp_inpcb * inp,struct sctp_nets ** netp,struct sockaddr * init_src,int * notification,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)2075 sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
2076 struct sockaddr *src, struct sockaddr *dst,
2077 struct sctphdr *sh, struct sctp_state_cookie *cookie, int cookie_len,
2078 struct sctp_inpcb *inp, struct sctp_nets **netp,
2079 struct sockaddr *init_src, int *notification,
2080 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2081 uint8_t mflowtype, uint32_t mflowid,
2082 uint32_t vrf_id, uint16_t port)
2083 {
2084 struct sctp_tcb *stcb;
2085 struct sctp_init_chunk *init_cp, init_buf;
2086 struct sctp_init_ack_chunk *initack_cp, initack_buf;
2087 union sctp_sockstore store;
2088 struct sctp_association *asoc;
2089 int init_offset, initack_offset, initack_limit;
2090 int retval;
2091 int error = 0;
2092 uint8_t auth_chunk_buf[SCTP_PARAM_BUFFER_SIZE];
2093 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2094 struct socket *so;
2095
2096 so = SCTP_INP_SO(inp);
2097 #endif
2098
2099 /*
2100 * find and validate the INIT chunk in the cookie (peer's info) the
2101 * INIT should start after the cookie-echo header struct (chunk
2102 * header, state cookie header struct)
2103 */
2104 init_offset = offset + sizeof(struct sctp_cookie_echo_chunk);
2105 init_cp = (struct sctp_init_chunk *)
2106 sctp_m_getptr(m, init_offset, sizeof(struct sctp_init_chunk),
2107 (uint8_t *)&init_buf);
2108 if (init_cp == NULL) {
2109 /* could not pull a INIT chunk in cookie */
2110 SCTPDBG(SCTP_DEBUG_INPUT1,
2111 "process_cookie_new: could not pull INIT chunk hdr\n");
2112 return (NULL);
2113 }
2114 if (init_cp->ch.chunk_type != SCTP_INITIATION) {
2115 SCTPDBG(SCTP_DEBUG_INPUT1, "HUH? process_cookie_new: could not find INIT chunk!\n");
2116 return (NULL);
2117 }
2118 initack_offset = init_offset + SCTP_SIZE32(ntohs(init_cp->ch.chunk_length));
2119 /*
2120 * find and validate the INIT-ACK chunk in the cookie (my info) the
2121 * INIT-ACK follows the INIT chunk
2122 */
2123 initack_cp = (struct sctp_init_ack_chunk *)
2124 sctp_m_getptr(m, initack_offset, sizeof(struct sctp_init_ack_chunk),
2125 (uint8_t *)&initack_buf);
2126 if (initack_cp == NULL) {
2127 /* could not pull INIT-ACK chunk in cookie */
2128 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: could not pull INIT-ACK chunk hdr\n");
2129 return (NULL);
2130 }
2131 if (initack_cp->ch.chunk_type != SCTP_INITIATION_ACK) {
2132 return (NULL);
2133 }
2134 /*
2135 * NOTE: We can't use the INIT_ACK's chk_length to determine the
2136 * "initack_limit" value. This is because the chk_length field
2137 * includes the length of the cookie, but the cookie is omitted when
2138 * the INIT and INIT_ACK are tacked onto the cookie...
2139 */
2140 initack_limit = offset + cookie_len;
2141
2142 /*
2143 * now that we know the INIT/INIT-ACK are in place, create a new TCB
2144 * and popluate
2145 */
2146
2147 /*
2148 * Here we do a trick, we set in NULL for the proc/thread argument.
2149 * We do this since in effect we only use the p argument when the
2150 * socket is unbound and we must do an implicit bind. Since we are
2151 * getting a cookie, we cannot be unbound.
2152 */
2153 stcb = sctp_aloc_assoc(inp, init_src, &error,
2154 ntohl(initack_cp->init.initiate_tag), vrf_id,
2155 ntohs(initack_cp->init.num_outbound_streams),
2156 port,
2157 (struct thread *)NULL,
2158 SCTP_DONT_INITIALIZE_AUTH_PARAMS);
2159 if (stcb == NULL) {
2160 struct mbuf *op_err;
2161
2162 /* memory problem? */
2163 SCTPDBG(SCTP_DEBUG_INPUT1,
2164 "process_cookie_new: no room for another TCB!\n");
2165 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2166 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2167 src, dst, sh, op_err,
2168 mflowtype, mflowid,
2169 vrf_id, port);
2170 return (NULL);
2171 }
2172 /* get the correct sctp_nets */
2173 if (netp)
2174 *netp = sctp_findnet(stcb, init_src);
2175
2176 asoc = &stcb->asoc;
2177 /* get scope variables out of cookie */
2178 asoc->scope.ipv4_local_scope = cookie->ipv4_scope;
2179 asoc->scope.site_scope = cookie->site_scope;
2180 asoc->scope.local_scope = cookie->local_scope;
2181 asoc->scope.loopback_scope = cookie->loopback_scope;
2182
2183 if ((asoc->scope.ipv4_addr_legal != cookie->ipv4_addr_legal) ||
2184 (asoc->scope.ipv6_addr_legal != cookie->ipv6_addr_legal)) {
2185 struct mbuf *op_err;
2186
2187 /*
2188 * Houston we have a problem. The EP changed while the
2189 * cookie was in flight. Only recourse is to abort the
2190 * association.
2191 */
2192 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2193 sctp_abort_association(inp, (struct sctp_tcb *)NULL, m, iphlen,
2194 src, dst, sh, op_err,
2195 mflowtype, mflowid,
2196 vrf_id, port);
2197 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2198 atomic_add_int(&stcb->asoc.refcnt, 1);
2199 SCTP_TCB_UNLOCK(stcb);
2200 SCTP_SOCKET_LOCK(so, 1);
2201 SCTP_TCB_LOCK(stcb);
2202 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2203 #endif
2204 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2205 SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
2206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2207 SCTP_SOCKET_UNLOCK(so, 1);
2208 #endif
2209 return (NULL);
2210 }
2211 /* process the INIT-ACK info (my info) */
2212 asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
2213 asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
2214 asoc->pre_open_streams = ntohs(initack_cp->init.num_outbound_streams);
2215 asoc->init_seq_number = ntohl(initack_cp->init.initial_tsn);
2216 asoc->sending_seq = asoc->asconf_seq_out = asoc->str_reset_seq_out = asoc->init_seq_number;
2217 asoc->asconf_seq_out_acked = asoc->asconf_seq_out - 1;
2218 asoc->asconf_seq_in = asoc->last_acked_seq = asoc->init_seq_number - 1;
2219 asoc->str_reset_seq_in = asoc->init_seq_number;
2220
2221 asoc->advanced_peer_ack_point = asoc->last_acked_seq;
2222
2223 /* process the INIT info (peer's info) */
2224 if (netp)
2225 retval = sctp_process_init(init_cp, stcb);
2226 else
2227 retval = 0;
2228 if (retval < 0) {
2229 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2230 atomic_add_int(&stcb->asoc.refcnt, 1);
2231 SCTP_TCB_UNLOCK(stcb);
2232 SCTP_SOCKET_LOCK(so, 1);
2233 SCTP_TCB_LOCK(stcb);
2234 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2235 #endif
2236 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2237 SCTP_FROM_SCTP_INPUT + SCTP_LOC_19);
2238 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2239 SCTP_SOCKET_UNLOCK(so, 1);
2240 #endif
2241 return (NULL);
2242 }
2243 /* load all addresses */
2244 if (sctp_load_addresses_from_init(stcb, m,
2245 init_offset + sizeof(struct sctp_init_chunk), initack_offset,
2246 src, dst, init_src, port)) {
2247 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2248 atomic_add_int(&stcb->asoc.refcnt, 1);
2249 SCTP_TCB_UNLOCK(stcb);
2250 SCTP_SOCKET_LOCK(so, 1);
2251 SCTP_TCB_LOCK(stcb);
2252 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2253 #endif
2254 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2255 SCTP_FROM_SCTP_INPUT + SCTP_LOC_20);
2256 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2257 SCTP_SOCKET_UNLOCK(so, 1);
2258 #endif
2259 return (NULL);
2260 }
2261 /*
2262 * verify any preceding AUTH chunk that was skipped
2263 */
2264 /* pull the local authentication parameters from the cookie/init-ack */
2265 sctp_auth_get_cookie_params(stcb, m,
2266 initack_offset + sizeof(struct sctp_init_ack_chunk),
2267 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)));
2268 if (auth_skipped) {
2269 struct sctp_auth_chunk *auth;
2270
2271 auth = (struct sctp_auth_chunk *)
2272 sctp_m_getptr(m, auth_offset, auth_len, auth_chunk_buf);
2273 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m, auth_offset)) {
2274 /* auth HMAC failed, dump the assoc and packet */
2275 SCTPDBG(SCTP_DEBUG_AUTH1,
2276 "COOKIE-ECHO: AUTH failed\n");
2277 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2278 atomic_add_int(&stcb->asoc.refcnt, 1);
2279 SCTP_TCB_UNLOCK(stcb);
2280 SCTP_SOCKET_LOCK(so, 1);
2281 SCTP_TCB_LOCK(stcb);
2282 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2283 #endif
2284 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2285 SCTP_FROM_SCTP_INPUT + SCTP_LOC_21);
2286 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2287 SCTP_SOCKET_UNLOCK(so, 1);
2288 #endif
2289 return (NULL);
2290 } else {
2291 /* remaining chunks checked... good to go */
2292 stcb->asoc.authenticated = 1;
2293 }
2294 }
2295
2296 /*
2297 * if we're doing ASCONFs, check to see if we have any new local
2298 * addresses that need to get added to the peer (eg. addresses
2299 * changed while cookie echo in flight). This needs to be done
2300 * after we go to the OPEN state to do the correct asconf
2301 * processing. else, make sure we have the correct addresses in our
2302 * lists
2303 */
2304
2305 /* warning, we re-use sin, sin6, sa_store here! */
2306 /* pull in local_address (our "from" address) */
2307 switch (cookie->laddr_type) {
2308 #ifdef INET
2309 case SCTP_IPV4_ADDRESS:
2310 /* source addr is IPv4 */
2311 memset(&store.sin, 0, sizeof(struct sockaddr_in));
2312 store.sin.sin_family = AF_INET;
2313 store.sin.sin_len = sizeof(struct sockaddr_in);
2314 store.sin.sin_addr.s_addr = cookie->laddress[0];
2315 break;
2316 #endif
2317 #ifdef INET6
2318 case SCTP_IPV6_ADDRESS:
2319 /* source addr is IPv6 */
2320 memset(&store.sin6, 0, sizeof(struct sockaddr_in6));
2321 store.sin6.sin6_family = AF_INET6;
2322 store.sin6.sin6_len = sizeof(struct sockaddr_in6);
2323 store.sin6.sin6_scope_id = cookie->scope_id;
2324 memcpy(&store.sin6.sin6_addr, cookie->laddress, sizeof(struct in6_addr));
2325 break;
2326 #endif
2327 default:
2328 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2329 atomic_add_int(&stcb->asoc.refcnt, 1);
2330 SCTP_TCB_UNLOCK(stcb);
2331 SCTP_SOCKET_LOCK(so, 1);
2332 SCTP_TCB_LOCK(stcb);
2333 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2334 #endif
2335 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2336 SCTP_FROM_SCTP_INPUT + SCTP_LOC_22);
2337 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2338 SCTP_SOCKET_UNLOCK(so, 1);
2339 #endif
2340 return (NULL);
2341 }
2342
2343 /* update current state */
2344 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2345 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2346 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2347 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2348 stcb->sctp_ep, stcb, asoc->primary_destination);
2349 }
2350 sctp_stop_all_cookie_timers(stcb);
2351 SCTP_STAT_INCR_COUNTER32(sctps_passiveestab);
2352 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2353
2354 /* set up to notify upper layer */
2355 *notification = SCTP_NOTIFY_ASSOC_UP;
2356 if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2357 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
2358 (!SCTP_IS_LISTENING(inp))) {
2359 /*
2360 * This is an endpoint that called connect() how it got a
2361 * cookie that is NEW is a bit of a mystery. It must be that
2362 * the INIT was sent, but before it got there.. a complete
2363 * INIT/INIT-ACK/COOKIE arrived. But of course then it
2364 * should have went to the other code.. not here.. oh well..
2365 * a bit of protection is worth having..
2366 */
2367 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2368 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2369 atomic_add_int(&stcb->asoc.refcnt, 1);
2370 SCTP_TCB_UNLOCK(stcb);
2371 SCTP_SOCKET_LOCK(so, 1);
2372 SCTP_TCB_LOCK(stcb);
2373 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2374 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
2375 SCTP_SOCKET_UNLOCK(so, 1);
2376 return (NULL);
2377 }
2378 #endif
2379 soisconnected(stcb->sctp_socket);
2380 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2381 SCTP_SOCKET_UNLOCK(so, 1);
2382 #endif
2383 } else if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
2384 (SCTP_IS_LISTENING(inp))) {
2385 /*
2386 * We don't want to do anything with this one. Since it is
2387 * the listening guy. The timer will get started for
2388 * accepted connections in the caller.
2389 */
2390 ;
2391 }
2392 /* since we did not send a HB make sure we don't double things */
2393 if ((netp) && (*netp))
2394 (*netp)->hb_responded = 1;
2395
2396 if (stcb->asoc.sctp_autoclose_ticks &&
2397 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_AUTOCLOSE)) {
2398 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL);
2399 }
2400 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2401 if ((netp != NULL) && (*netp != NULL)) {
2402 struct timeval old;
2403
2404 /* calculate the RTT and set the encaps port */
2405 old.tv_sec = cookie->time_entered.tv_sec;
2406 old.tv_usec = cookie->time_entered.tv_usec;
2407 sctp_calculate_rto(stcb, asoc, *netp, &old, SCTP_RTT_FROM_NON_DATA);
2408 }
2409 /* respond with a COOKIE-ACK */
2410 sctp_send_cookie_ack(stcb);
2411
2412 /*
2413 * check the address lists for any ASCONFs that need to be sent
2414 * AFTER the cookie-ack is sent
2415 */
2416 sctp_check_address_list(stcb, m,
2417 initack_offset + sizeof(struct sctp_init_ack_chunk),
2418 initack_limit - (initack_offset + sizeof(struct sctp_init_ack_chunk)),
2419 &store.sa, cookie->local_scope, cookie->site_scope,
2420 cookie->ipv4_scope, cookie->loopback_scope);
2421
2422
2423 return (stcb);
2424 }
2425
2426 /*
2427 * CODE LIKE THIS NEEDS TO RUN IF the peer supports the NAT extension, i.e
2428 * we NEED to make sure we are not already using the vtag. If so we
2429 * need to send back an ABORT-TRY-AGAIN-WITH-NEW-TAG No middle box bit!
2430 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
2431 SCTP_BASE_INFO(hashasocmark))];
2432 LIST_FOREACH(stcb, head, sctp_asocs) {
2433 if ((stcb->asoc.my_vtag == tag) && (stcb->rport == rport) && (inp == stcb->sctp_ep)) {
2434 -- SEND ABORT - TRY AGAIN --
2435 }
2436 }
2437 */
2438
2439 /*
2440 * handles a COOKIE-ECHO message stcb: modified to either a new or left as
2441 * existing (non-NULL) TCB
2442 */
2443 static struct mbuf *
sctp_handle_cookie_echo(struct mbuf * m,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_cookie_echo_chunk * cp,struct sctp_inpcb ** inp_p,struct sctp_tcb ** stcb,struct sctp_nets ** netp,int auth_skipped,uint32_t auth_offset,uint32_t auth_len,struct sctp_tcb ** locked_tcb,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)2444 sctp_handle_cookie_echo(struct mbuf *m, int iphlen, int offset,
2445 struct sockaddr *src, struct sockaddr *dst,
2446 struct sctphdr *sh, struct sctp_cookie_echo_chunk *cp,
2447 struct sctp_inpcb **inp_p, struct sctp_tcb **stcb, struct sctp_nets **netp,
2448 int auth_skipped, uint32_t auth_offset, uint32_t auth_len,
2449 struct sctp_tcb **locked_tcb,
2450 uint8_t mflowtype, uint32_t mflowid,
2451 uint32_t vrf_id, uint16_t port)
2452 {
2453 struct sctp_state_cookie *cookie;
2454 struct sctp_tcb *l_stcb = *stcb;
2455 struct sctp_inpcb *l_inp;
2456 struct sockaddr *to;
2457 struct sctp_pcb *ep;
2458 struct mbuf *m_sig;
2459 uint8_t calc_sig[SCTP_SIGNATURE_SIZE], tmp_sig[SCTP_SIGNATURE_SIZE];
2460 uint8_t *sig;
2461 uint8_t cookie_ok = 0;
2462 unsigned int sig_offset, cookie_offset;
2463 unsigned int cookie_len;
2464 struct timeval now;
2465 struct timeval time_expires;
2466 int notification = 0;
2467 struct sctp_nets *netl;
2468 int had_a_existing_tcb = 0;
2469 int send_int_conf = 0;
2470 #ifdef INET
2471 struct sockaddr_in sin;
2472 #endif
2473 #ifdef INET6
2474 struct sockaddr_in6 sin6;
2475 #endif
2476
2477 SCTPDBG(SCTP_DEBUG_INPUT2,
2478 "sctp_handle_cookie: handling COOKIE-ECHO\n");
2479
2480 if (inp_p == NULL) {
2481 return (NULL);
2482 }
2483 cookie = &cp->cookie;
2484 cookie_offset = offset + sizeof(struct sctp_chunkhdr);
2485 cookie_len = ntohs(cp->ch.chunk_length);
2486
2487 if (cookie_len < sizeof(struct sctp_cookie_echo_chunk) +
2488 sizeof(struct sctp_init_chunk) +
2489 sizeof(struct sctp_init_ack_chunk) + SCTP_SIGNATURE_SIZE) {
2490 /* cookie too small */
2491 return (NULL);
2492 }
2493 if ((cookie->peerport != sh->src_port) ||
2494 (cookie->myport != sh->dest_port) ||
2495 (cookie->my_vtag != sh->v_tag)) {
2496 /*
2497 * invalid ports or bad tag. Note that we always leave the
2498 * v_tag in the header in network order and when we stored
2499 * it in the my_vtag slot we also left it in network order.
2500 * This maintains the match even though it may be in the
2501 * opposite byte order of the machine :->
2502 */
2503 return (NULL);
2504 }
2505 /*
2506 * split off the signature into its own mbuf (since it should not be
2507 * calculated in the sctp_hmac_m() call).
2508 */
2509 sig_offset = offset + cookie_len - SCTP_SIGNATURE_SIZE;
2510 m_sig = m_split(m, sig_offset, M_NOWAIT);
2511 if (m_sig == NULL) {
2512 /* out of memory or ?? */
2513 return (NULL);
2514 }
2515 #ifdef SCTP_MBUF_LOGGING
2516 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
2517 sctp_log_mbc(m_sig, SCTP_MBUF_SPLIT);
2518 }
2519 #endif
2520
2521 /*
2522 * compute the signature/digest for the cookie
2523 */
2524 ep = &(*inp_p)->sctp_ep;
2525 l_inp = *inp_p;
2526 if (l_stcb) {
2527 SCTP_TCB_UNLOCK(l_stcb);
2528 }
2529 SCTP_INP_RLOCK(l_inp);
2530 if (l_stcb) {
2531 SCTP_TCB_LOCK(l_stcb);
2532 }
2533 /* which cookie is it? */
2534 if ((cookie->time_entered.tv_sec < (long)ep->time_of_secret_change) &&
2535 (ep->current_secret_number != ep->last_secret_number)) {
2536 /* it's the old cookie */
2537 (void)sctp_hmac_m(SCTP_HMAC,
2538 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2539 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2540 } else {
2541 /* it's the current cookie */
2542 (void)sctp_hmac_m(SCTP_HMAC,
2543 (uint8_t *)ep->secret_key[(int)ep->current_secret_number],
2544 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2545 }
2546 /* get the signature */
2547 SCTP_INP_RUNLOCK(l_inp);
2548 sig = (uint8_t *)sctp_m_getptr(m_sig, 0, SCTP_SIGNATURE_SIZE, (uint8_t *)&tmp_sig);
2549 if (sig == NULL) {
2550 /* couldn't find signature */
2551 sctp_m_freem(m_sig);
2552 return (NULL);
2553 }
2554 /* compare the received digest with the computed digest */
2555 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) != 0) {
2556 /* try the old cookie? */
2557 if ((cookie->time_entered.tv_sec == (long)ep->time_of_secret_change) &&
2558 (ep->current_secret_number != ep->last_secret_number)) {
2559 /* compute digest with old */
2560 (void)sctp_hmac_m(SCTP_HMAC,
2561 (uint8_t *)ep->secret_key[(int)ep->last_secret_number],
2562 SCTP_SECRET_SIZE, m, cookie_offset, calc_sig, 0);
2563 /* compare */
2564 if (timingsafe_bcmp(calc_sig, sig, SCTP_SIGNATURE_SIZE) == 0)
2565 cookie_ok = 1;
2566 }
2567 } else {
2568 cookie_ok = 1;
2569 }
2570
2571 /*
2572 * Now before we continue we must reconstruct our mbuf so that
2573 * normal processing of any other chunks will work.
2574 */
2575 {
2576 struct mbuf *m_at;
2577
2578 m_at = m;
2579 while (SCTP_BUF_NEXT(m_at) != NULL) {
2580 m_at = SCTP_BUF_NEXT(m_at);
2581 }
2582 SCTP_BUF_NEXT(m_at) = m_sig;
2583 }
2584
2585 if (cookie_ok == 0) {
2586 SCTPDBG(SCTP_DEBUG_INPUT2, "handle_cookie_echo: cookie signature validation failed!\n");
2587 SCTPDBG(SCTP_DEBUG_INPUT2,
2588 "offset = %u, cookie_offset = %u, sig_offset = %u\n",
2589 (uint32_t)offset, cookie_offset, sig_offset);
2590 return (NULL);
2591 }
2592
2593 /*
2594 * check the cookie timestamps to be sure it's not stale
2595 */
2596 (void)SCTP_GETTIME_TIMEVAL(&now);
2597 /* Expire time is in Ticks, so we convert to seconds */
2598 time_expires.tv_sec = cookie->time_entered.tv_sec + TICKS_TO_SEC(cookie->cookie_life);
2599 time_expires.tv_usec = cookie->time_entered.tv_usec;
2600 if (timevalcmp(&now, &time_expires, >)) {
2601 /* cookie is stale! */
2602 struct mbuf *op_err;
2603 struct sctp_error_stale_cookie *cause;
2604 struct timeval diff;
2605 uint32_t staleness;
2606
2607 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_stale_cookie),
2608 0, M_NOWAIT, 1, MT_DATA);
2609 if (op_err == NULL) {
2610 /* FOOBAR */
2611 return (NULL);
2612 }
2613 /* Set the len */
2614 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_stale_cookie);
2615 cause = mtod(op_err, struct sctp_error_stale_cookie *);
2616 cause->cause.code = htons(SCTP_CAUSE_STALE_COOKIE);
2617 cause->cause.length = htons((sizeof(struct sctp_paramhdr) +
2618 (sizeof(uint32_t))));
2619 diff = now;
2620 timevalsub(&diff, &time_expires);
2621 if ((uint32_t)diff.tv_sec > UINT32_MAX / 1000000) {
2622 staleness = UINT32_MAX;
2623 } else {
2624 staleness = diff.tv_sec * 1000000;
2625 }
2626 if (UINT32_MAX - staleness >= (uint32_t)diff.tv_usec) {
2627 staleness += diff.tv_usec;
2628 } else {
2629 staleness = UINT32_MAX;
2630 }
2631 cause->stale_time = htonl(staleness);
2632 sctp_send_operr_to(src, dst, sh, cookie->peers_vtag, op_err,
2633 mflowtype, mflowid, l_inp->fibnum,
2634 vrf_id, port);
2635 return (NULL);
2636 }
2637 /*
2638 * Now we must see with the lookup address if we have an existing
2639 * asoc. This will only happen if we were in the COOKIE-WAIT state
2640 * and a INIT collided with us and somewhere the peer sent the
2641 * cookie on another address besides the single address our assoc
2642 * had for him. In this case we will have one of the tie-tags set at
2643 * least AND the address field in the cookie can be used to look it
2644 * up.
2645 */
2646 to = NULL;
2647 switch (cookie->addr_type) {
2648 #ifdef INET6
2649 case SCTP_IPV6_ADDRESS:
2650 memset(&sin6, 0, sizeof(sin6));
2651 sin6.sin6_family = AF_INET6;
2652 sin6.sin6_len = sizeof(sin6);
2653 sin6.sin6_port = sh->src_port;
2654 sin6.sin6_scope_id = cookie->scope_id;
2655 memcpy(&sin6.sin6_addr.s6_addr, cookie->address,
2656 sizeof(sin6.sin6_addr.s6_addr));
2657 to = (struct sockaddr *)&sin6;
2658 break;
2659 #endif
2660 #ifdef INET
2661 case SCTP_IPV4_ADDRESS:
2662 memset(&sin, 0, sizeof(sin));
2663 sin.sin_family = AF_INET;
2664 sin.sin_len = sizeof(sin);
2665 sin.sin_port = sh->src_port;
2666 sin.sin_addr.s_addr = cookie->address[0];
2667 to = (struct sockaddr *)&sin;
2668 break;
2669 #endif
2670 default:
2671 /* This should not happen */
2672 return (NULL);
2673 }
2674 if (*stcb == NULL) {
2675 /* Yep, lets check */
2676 *stcb = sctp_findassociation_ep_addr(inp_p, to, netp, dst, NULL);
2677 if (*stcb == NULL) {
2678 /*
2679 * We should have only got back the same inp. If we
2680 * got back a different ep we have a problem. The
2681 * original findep got back l_inp and now
2682 */
2683 if (l_inp != *inp_p) {
2684 SCTP_PRINTF("Bad problem find_ep got a diff inp then special_locate?\n");
2685 }
2686 } else {
2687 if (*locked_tcb == NULL) {
2688 /*
2689 * In this case we found the assoc only
2690 * after we locked the create lock. This
2691 * means we are in a colliding case and we
2692 * must make sure that we unlock the tcb if
2693 * its one of the cases where we throw away
2694 * the incoming packets.
2695 */
2696 *locked_tcb = *stcb;
2697
2698 /*
2699 * We must also increment the inp ref count
2700 * since the ref_count flags was set when we
2701 * did not find the TCB, now we found it
2702 * which reduces the refcount.. we must
2703 * raise it back out to balance it all :-)
2704 */
2705 SCTP_INP_INCR_REF((*stcb)->sctp_ep);
2706 if ((*stcb)->sctp_ep != l_inp) {
2707 SCTP_PRINTF("Huh? ep:%p diff then l_inp:%p?\n",
2708 (void *)(*stcb)->sctp_ep, (void *)l_inp);
2709 }
2710 }
2711 }
2712 }
2713
2714 cookie_len -= SCTP_SIGNATURE_SIZE;
2715 if (*stcb == NULL) {
2716 /* this is the "normal" case... get a new TCB */
2717 *stcb = sctp_process_cookie_new(m, iphlen, offset, src, dst, sh,
2718 cookie, cookie_len, *inp_p,
2719 netp, to, ¬ification,
2720 auth_skipped, auth_offset, auth_len,
2721 mflowtype, mflowid,
2722 vrf_id, port);
2723 } else {
2724 /* this is abnormal... cookie-echo on existing TCB */
2725 had_a_existing_tcb = 1;
2726 *stcb = sctp_process_cookie_existing(m, iphlen, offset,
2727 src, dst, sh,
2728 cookie, cookie_len, *inp_p, *stcb, netp, to,
2729 ¬ification, auth_skipped, auth_offset, auth_len,
2730 mflowtype, mflowid,
2731 vrf_id, port);
2732 }
2733
2734 if (*stcb == NULL) {
2735 /* still no TCB... must be bad cookie-echo */
2736 return (NULL);
2737 }
2738 if (*netp != NULL) {
2739 (*netp)->flowtype = mflowtype;
2740 (*netp)->flowid = mflowid;
2741 }
2742 /*
2743 * Ok, we built an association so confirm the address we sent the
2744 * INIT-ACK to.
2745 */
2746 netl = sctp_findnet(*stcb, to);
2747 /*
2748 * This code should in theory NOT run but
2749 */
2750 if (netl == NULL) {
2751 /* TSNH! Huh, why do I need to add this address here? */
2752 if (sctp_add_remote_addr(*stcb, to, NULL, port,
2753 SCTP_DONOT_SETSCOPE, SCTP_IN_COOKIE_PROC)) {
2754 return (NULL);
2755 }
2756 netl = sctp_findnet(*stcb, to);
2757 }
2758 if (netl) {
2759 if (netl->dest_state & SCTP_ADDR_UNCONFIRMED) {
2760 netl->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2761 (void)sctp_set_primary_addr((*stcb), (struct sockaddr *)NULL,
2762 netl);
2763 send_int_conf = 1;
2764 }
2765 }
2766 sctp_start_net_timers(*stcb);
2767 if ((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
2768 if (!had_a_existing_tcb ||
2769 (((*inp_p)->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
2770 /*
2771 * If we have a NEW cookie or the connect never
2772 * reached the connected state during collision we
2773 * must do the TCP accept thing.
2774 */
2775 struct socket *so, *oso;
2776 struct sctp_inpcb *inp;
2777
2778 if (notification == SCTP_NOTIFY_ASSOC_RESTART) {
2779 /*
2780 * For a restart we will keep the same
2781 * socket, no need to do anything. I THINK!!
2782 */
2783 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2784 if (send_int_conf) {
2785 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2786 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2787 }
2788 return (m);
2789 }
2790 oso = (*inp_p)->sctp_socket;
2791 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2792 SCTP_TCB_UNLOCK((*stcb));
2793 CURVNET_SET(oso->so_vnet);
2794 so = sonewconn(oso, 0
2795 );
2796 CURVNET_RESTORE();
2797 SCTP_TCB_LOCK((*stcb));
2798 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2799
2800 if (so == NULL) {
2801 struct mbuf *op_err;
2802 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2803 struct socket *pcb_so;
2804 #endif
2805 /* Too many sockets */
2806 SCTPDBG(SCTP_DEBUG_INPUT1, "process_cookie_new: no room for another socket!\n");
2807 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
2808 sctp_abort_association(*inp_p, NULL, m, iphlen,
2809 src, dst, sh, op_err,
2810 mflowtype, mflowid,
2811 vrf_id, port);
2812 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2813 pcb_so = SCTP_INP_SO(*inp_p);
2814 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2815 SCTP_TCB_UNLOCK((*stcb));
2816 SCTP_SOCKET_LOCK(pcb_so, 1);
2817 SCTP_TCB_LOCK((*stcb));
2818 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2819 #endif
2820 (void)sctp_free_assoc(*inp_p, *stcb, SCTP_NORMAL_PROC,
2821 SCTP_FROM_SCTP_INPUT + SCTP_LOC_23);
2822 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2823 SCTP_SOCKET_UNLOCK(pcb_so, 1);
2824 #endif
2825 return (NULL);
2826 }
2827 inp = (struct sctp_inpcb *)so->so_pcb;
2828 SCTP_INP_INCR_REF(inp);
2829 /*
2830 * We add the unbound flag here so that if we get an
2831 * soabort() before we get the move_pcb done, we
2832 * will properly cleanup.
2833 */
2834 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2835 SCTP_PCB_FLAGS_CONNECTED |
2836 SCTP_PCB_FLAGS_IN_TCPPOOL |
2837 SCTP_PCB_FLAGS_UNBOUND |
2838 (SCTP_PCB_COPY_FLAGS & (*inp_p)->sctp_flags) |
2839 SCTP_PCB_FLAGS_DONT_WAKE);
2840 inp->sctp_features = (*inp_p)->sctp_features;
2841 inp->sctp_mobility_features = (*inp_p)->sctp_mobility_features;
2842 inp->sctp_socket = so;
2843 inp->sctp_frag_point = (*inp_p)->sctp_frag_point;
2844 inp->max_cwnd = (*inp_p)->max_cwnd;
2845 inp->sctp_cmt_on_off = (*inp_p)->sctp_cmt_on_off;
2846 inp->ecn_supported = (*inp_p)->ecn_supported;
2847 inp->prsctp_supported = (*inp_p)->prsctp_supported;
2848 inp->auth_supported = (*inp_p)->auth_supported;
2849 inp->asconf_supported = (*inp_p)->asconf_supported;
2850 inp->reconfig_supported = (*inp_p)->reconfig_supported;
2851 inp->nrsack_supported = (*inp_p)->nrsack_supported;
2852 inp->pktdrop_supported = (*inp_p)->pktdrop_supported;
2853 inp->partial_delivery_point = (*inp_p)->partial_delivery_point;
2854 inp->sctp_context = (*inp_p)->sctp_context;
2855 inp->local_strreset_support = (*inp_p)->local_strreset_support;
2856 inp->fibnum = (*inp_p)->fibnum;
2857 inp->inp_starting_point_for_iterator = NULL;
2858 /*
2859 * copy in the authentication parameters from the
2860 * original endpoint
2861 */
2862 if (inp->sctp_ep.local_hmacs)
2863 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2864 inp->sctp_ep.local_hmacs =
2865 sctp_copy_hmaclist((*inp_p)->sctp_ep.local_hmacs);
2866 if (inp->sctp_ep.local_auth_chunks)
2867 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2868 inp->sctp_ep.local_auth_chunks =
2869 sctp_copy_chunklist((*inp_p)->sctp_ep.local_auth_chunks);
2870
2871 /*
2872 * Now we must move it from one hash table to
2873 * another and get the tcb in the right place.
2874 */
2875
2876 /*
2877 * This is where the one-2-one socket is put into
2878 * the accept state waiting for the accept!
2879 */
2880 if (*stcb) {
2881 SCTP_ADD_SUBSTATE(*stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
2882 }
2883 sctp_move_pcb_and_assoc(*inp_p, inp, *stcb);
2884
2885 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2886 SCTP_TCB_UNLOCK((*stcb));
2887
2888 sctp_pull_off_control_to_new_inp((*inp_p), inp, *stcb,
2889 0);
2890 SCTP_TCB_LOCK((*stcb));
2891 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2892
2893
2894 /*
2895 * now we must check to see if we were aborted while
2896 * the move was going on and the lock/unlock
2897 * happened.
2898 */
2899 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2900 /*
2901 * yep it was, we leave the assoc attached
2902 * to the socket since the sctp_inpcb_free()
2903 * call will send an abort for us.
2904 */
2905 SCTP_INP_DECR_REF(inp);
2906 return (NULL);
2907 }
2908 SCTP_INP_DECR_REF(inp);
2909 /* Switch over to the new guy */
2910 *inp_p = inp;
2911 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2912 if (send_int_conf) {
2913 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2914 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2915 }
2916
2917 /*
2918 * Pull it from the incomplete queue and wake the
2919 * guy
2920 */
2921 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2922 atomic_add_int(&(*stcb)->asoc.refcnt, 1);
2923 SCTP_TCB_UNLOCK((*stcb));
2924 SCTP_SOCKET_LOCK(so, 1);
2925 #endif
2926 soisconnected(so);
2927 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2928 SCTP_TCB_LOCK((*stcb));
2929 atomic_subtract_int(&(*stcb)->asoc.refcnt, 1);
2930 SCTP_SOCKET_UNLOCK(so, 1);
2931 #endif
2932 return (m);
2933 }
2934 }
2935 if (notification) {
2936 sctp_ulp_notify(notification, *stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2937 }
2938 if (send_int_conf) {
2939 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_CONFIRMED,
2940 (*stcb), 0, (void *)netl, SCTP_SO_NOT_LOCKED);
2941 }
2942 return (m);
2943 }
2944
2945 static void
sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)2946 sctp_handle_cookie_ack(struct sctp_cookie_ack_chunk *cp SCTP_UNUSED,
2947 struct sctp_tcb *stcb, struct sctp_nets *net)
2948 {
2949 /* cp must not be used, others call this without a c-ack :-) */
2950 struct sctp_association *asoc;
2951
2952 SCTPDBG(SCTP_DEBUG_INPUT2,
2953 "sctp_handle_cookie_ack: handling COOKIE-ACK\n");
2954 if ((stcb == NULL) || (net == NULL)) {
2955 return;
2956 }
2957
2958 asoc = &stcb->asoc;
2959 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
2960 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2961 asoc->overall_error_count,
2962 0,
2963 SCTP_FROM_SCTP_INPUT,
2964 __LINE__);
2965 }
2966 asoc->overall_error_count = 0;
2967 sctp_stop_all_cookie_timers(stcb);
2968 /* process according to association state */
2969 if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) {
2970 /* state change only needed when I am in right state */
2971 SCTPDBG(SCTP_DEBUG_INPUT2, "moving to OPEN state\n");
2972 SCTP_SET_STATE(stcb, SCTP_STATE_OPEN);
2973 sctp_start_net_timers(stcb);
2974 if (asoc->state & SCTP_STATE_SHUTDOWN_PENDING) {
2975 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
2976 stcb->sctp_ep, stcb, asoc->primary_destination);
2977
2978 }
2979 /* update RTO */
2980 SCTP_STAT_INCR_COUNTER32(sctps_activeestab);
2981 SCTP_STAT_INCR_GAUGE32(sctps_currestab);
2982 if (asoc->overall_error_count == 0) {
2983 sctp_calculate_rto(stcb, asoc, net, &asoc->time_entered,
2984 SCTP_RTT_FROM_NON_DATA);
2985 }
2986 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
2987 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_UP, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
2988 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
2989 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
2990 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2991 struct socket *so;
2992
2993 #endif
2994 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
2995 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
2996 so = SCTP_INP_SO(stcb->sctp_ep);
2997 atomic_add_int(&stcb->asoc.refcnt, 1);
2998 SCTP_TCB_UNLOCK(stcb);
2999 SCTP_SOCKET_LOCK(so, 1);
3000 SCTP_TCB_LOCK(stcb);
3001 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3002 #endif
3003 if ((stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) == 0) {
3004 soisconnected(stcb->sctp_socket);
3005 }
3006 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3007 SCTP_SOCKET_UNLOCK(so, 1);
3008 #endif
3009 }
3010 /*
3011 * since we did not send a HB make sure we don't double
3012 * things
3013 */
3014 net->hb_responded = 1;
3015
3016 if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3017 /*
3018 * We don't need to do the asconf thing, nor hb or
3019 * autoclose if the socket is closed.
3020 */
3021 goto closed_socket;
3022 }
3023
3024 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
3025 stcb, net);
3026
3027
3028 if (stcb->asoc.sctp_autoclose_ticks &&
3029 sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTOCLOSE)) {
3030 sctp_timer_start(SCTP_TIMER_TYPE_AUTOCLOSE,
3031 stcb->sctp_ep, stcb, NULL);
3032 }
3033 /*
3034 * send ASCONF if parameters are pending and ASCONFs are
3035 * allowed (eg. addresses changed when init/cookie echo were
3036 * in flight)
3037 */
3038 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_DO_ASCONF)) &&
3039 (stcb->asoc.asconf_supported == 1) &&
3040 (!TAILQ_EMPTY(&stcb->asoc.asconf_queue))) {
3041 #ifdef SCTP_TIMER_BASED_ASCONF
3042 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF,
3043 stcb->sctp_ep, stcb,
3044 stcb->asoc.primary_destination);
3045 #else
3046 sctp_send_asconf(stcb, stcb->asoc.primary_destination,
3047 SCTP_ADDR_NOT_LOCKED);
3048 #endif
3049 }
3050 }
3051 closed_socket:
3052 /* Toss the cookie if I can */
3053 sctp_toss_old_cookies(stcb, asoc);
3054 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3055 /* Restart the timer if we have pending data */
3056 struct sctp_tmit_chunk *chk;
3057
3058 chk = TAILQ_FIRST(&asoc->sent_queue);
3059 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
3060 }
3061 }
3062
3063 static void
sctp_handle_ecn_echo(struct sctp_ecne_chunk * cp,struct sctp_tcb * stcb)3064 sctp_handle_ecn_echo(struct sctp_ecne_chunk *cp,
3065 struct sctp_tcb *stcb)
3066 {
3067 struct sctp_nets *net;
3068 struct sctp_tmit_chunk *lchk;
3069 struct sctp_ecne_chunk bkup;
3070 uint8_t override_bit;
3071 uint32_t tsn, window_data_tsn;
3072 int len;
3073 unsigned int pkt_cnt;
3074
3075 len = ntohs(cp->ch.chunk_length);
3076 if ((len != sizeof(struct sctp_ecne_chunk)) &&
3077 (len != sizeof(struct old_sctp_ecne_chunk))) {
3078 return;
3079 }
3080 if (len == sizeof(struct old_sctp_ecne_chunk)) {
3081 /* Its the old format */
3082 memcpy(&bkup, cp, sizeof(struct old_sctp_ecne_chunk));
3083 bkup.num_pkts_since_cwr = htonl(1);
3084 cp = &bkup;
3085 }
3086 SCTP_STAT_INCR(sctps_recvecne);
3087 tsn = ntohl(cp->tsn);
3088 pkt_cnt = ntohl(cp->num_pkts_since_cwr);
3089 lchk = TAILQ_LAST(&stcb->asoc.send_queue, sctpchunk_listhead);
3090 if (lchk == NULL) {
3091 window_data_tsn = stcb->asoc.sending_seq - 1;
3092 } else {
3093 window_data_tsn = lchk->rec.data.tsn;
3094 }
3095
3096 /* Find where it was sent to if possible. */
3097 net = NULL;
3098 TAILQ_FOREACH(lchk, &stcb->asoc.sent_queue, sctp_next) {
3099 if (lchk->rec.data.tsn == tsn) {
3100 net = lchk->whoTo;
3101 net->ecn_prev_cwnd = lchk->rec.data.cwnd_at_send;
3102 break;
3103 }
3104 if (SCTP_TSN_GT(lchk->rec.data.tsn, tsn)) {
3105 break;
3106 }
3107 }
3108 if (net == NULL) {
3109 /*
3110 * What to do. A previous send of a CWR was possibly lost.
3111 * See how old it is, we may have it marked on the actual
3112 * net.
3113 */
3114 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3115 if (tsn == net->last_cwr_tsn) {
3116 /* Found him, send it off */
3117 break;
3118 }
3119 }
3120 if (net == NULL) {
3121 /*
3122 * If we reach here, we need to send a special CWR
3123 * that says hey, we did this a long time ago and
3124 * you lost the response.
3125 */
3126 net = TAILQ_FIRST(&stcb->asoc.nets);
3127 if (net == NULL) {
3128 /* TSNH */
3129 return;
3130 }
3131 override_bit = SCTP_CWR_REDUCE_OVERRIDE;
3132 } else {
3133 override_bit = 0;
3134 }
3135 } else {
3136 override_bit = 0;
3137 }
3138 if (SCTP_TSN_GT(tsn, net->cwr_window_tsn) &&
3139 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3140 /*
3141 * JRS - Use the congestion control given in the pluggable
3142 * CC module
3143 */
3144 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 0, pkt_cnt);
3145 /*
3146 * We reduce once every RTT. So we will only lower cwnd at
3147 * the next sending seq i.e. the window_data_tsn
3148 */
3149 net->cwr_window_tsn = window_data_tsn;
3150 net->ecn_ce_pkt_cnt += pkt_cnt;
3151 net->lost_cnt = pkt_cnt;
3152 net->last_cwr_tsn = tsn;
3153 } else {
3154 override_bit |= SCTP_CWR_IN_SAME_WINDOW;
3155 if (SCTP_TSN_GT(tsn, net->last_cwr_tsn) &&
3156 ((override_bit & SCTP_CWR_REDUCE_OVERRIDE) == 0)) {
3157 /*
3158 * Another loss in the same window update how many
3159 * marks/packets lost we have had.
3160 */
3161 int cnt = 1;
3162
3163 if (pkt_cnt > net->lost_cnt) {
3164 /* Should be the case */
3165 cnt = (pkt_cnt - net->lost_cnt);
3166 net->ecn_ce_pkt_cnt += cnt;
3167 }
3168 net->lost_cnt = pkt_cnt;
3169 net->last_cwr_tsn = tsn;
3170 /*
3171 * Most CC functions will ignore this call, since we
3172 * are in-window yet of the initial CE the peer saw.
3173 */
3174 stcb->asoc.cc_functions.sctp_cwnd_update_after_ecn_echo(stcb, net, 1, cnt);
3175 }
3176 }
3177 /*
3178 * We always send a CWR this way if our previous one was lost our
3179 * peer will get an update, or if it is not time again to reduce we
3180 * still get the cwr to the peer. Note we set the override when we
3181 * could not find the TSN on the chunk or the destination network.
3182 */
3183 sctp_send_cwr(stcb, net, net->last_cwr_tsn, override_bit);
3184 }
3185
3186 static void
sctp_handle_ecn_cwr(struct sctp_cwr_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net)3187 sctp_handle_ecn_cwr(struct sctp_cwr_chunk *cp, struct sctp_tcb *stcb, struct sctp_nets *net)
3188 {
3189 /*
3190 * Here we get a CWR from the peer. We must look in the outqueue and
3191 * make sure that we have a covered ECNE in the control chunk part.
3192 * If so remove it.
3193 */
3194 struct sctp_tmit_chunk *chk, *nchk;
3195 struct sctp_ecne_chunk *ecne;
3196 int override;
3197 uint32_t cwr_tsn;
3198
3199 cwr_tsn = ntohl(cp->tsn);
3200 override = cp->ch.chunk_flags & SCTP_CWR_REDUCE_OVERRIDE;
3201 TAILQ_FOREACH_SAFE(chk, &stcb->asoc.control_send_queue, sctp_next, nchk) {
3202 if (chk->rec.chunk_id.id != SCTP_ECN_ECHO) {
3203 continue;
3204 }
3205 if ((override == 0) && (chk->whoTo != net)) {
3206 /* Must be from the right src unless override is set */
3207 continue;
3208 }
3209 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
3210 if (SCTP_TSN_GE(cwr_tsn, ntohl(ecne->tsn))) {
3211 /* this covers this ECNE, we can remove it */
3212 stcb->asoc.ecn_echo_cnt_onq--;
3213 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk,
3214 sctp_next);
3215 stcb->asoc.ctrl_queue_cnt--;
3216 sctp_m_freem(chk->data);
3217 chk->data = NULL;
3218 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3219 if (override == 0) {
3220 break;
3221 }
3222 }
3223 }
3224 }
3225
3226 static void
sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk * cp SCTP_UNUSED,struct sctp_tcb * stcb,struct sctp_nets * net)3227 sctp_handle_shutdown_complete(struct sctp_shutdown_complete_chunk *cp SCTP_UNUSED,
3228 struct sctp_tcb *stcb, struct sctp_nets *net)
3229 {
3230 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3231 struct socket *so;
3232 #endif
3233
3234 SCTPDBG(SCTP_DEBUG_INPUT2,
3235 "sctp_handle_shutdown_complete: handling SHUTDOWN-COMPLETE\n");
3236 if (stcb == NULL)
3237 return;
3238
3239 /* process according to association state */
3240 if (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT) {
3241 /* unexpected SHUTDOWN-COMPLETE... so ignore... */
3242 SCTPDBG(SCTP_DEBUG_INPUT2,
3243 "sctp_handle_shutdown_complete: not in SCTP_STATE_SHUTDOWN_ACK_SENT --- ignore\n");
3244 SCTP_TCB_UNLOCK(stcb);
3245 return;
3246 }
3247 /* notify upper layer protocol */
3248 if (stcb->sctp_socket) {
3249 sctp_ulp_notify(SCTP_NOTIFY_ASSOC_DOWN, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
3250 }
3251 #ifdef INVARIANTS
3252 if (!TAILQ_EMPTY(&stcb->asoc.send_queue) ||
3253 !TAILQ_EMPTY(&stcb->asoc.sent_queue) ||
3254 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED)) {
3255 panic("Queues are not empty when handling SHUTDOWN-COMPLETE");
3256 }
3257 #endif
3258 /* stop the timer */
3259 sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, stcb->sctp_ep, stcb, net,
3260 SCTP_FROM_SCTP_INPUT + SCTP_LOC_24);
3261 SCTP_STAT_INCR_COUNTER32(sctps_shutdown);
3262 /* free the TCB */
3263 SCTPDBG(SCTP_DEBUG_INPUT2,
3264 "sctp_handle_shutdown_complete: calls free-asoc\n");
3265 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3266 so = SCTP_INP_SO(stcb->sctp_ep);
3267 atomic_add_int(&stcb->asoc.refcnt, 1);
3268 SCTP_TCB_UNLOCK(stcb);
3269 SCTP_SOCKET_LOCK(so, 1);
3270 SCTP_TCB_LOCK(stcb);
3271 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3272 #endif
3273 (void)sctp_free_assoc(stcb->sctp_ep, stcb, SCTP_NORMAL_PROC,
3274 SCTP_FROM_SCTP_INPUT + SCTP_LOC_25);
3275 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3276 SCTP_SOCKET_UNLOCK(so, 1);
3277 #endif
3278 return;
3279 }
3280
3281 static int
process_chunk_drop(struct sctp_tcb * stcb,struct sctp_chunk_desc * desc,struct sctp_nets * net,uint8_t flg)3282 process_chunk_drop(struct sctp_tcb *stcb, struct sctp_chunk_desc *desc,
3283 struct sctp_nets *net, uint8_t flg)
3284 {
3285 switch (desc->chunk_type) {
3286 case SCTP_DATA:
3287 /* find the tsn to resend (possibly */
3288 {
3289 uint32_t tsn;
3290 struct sctp_tmit_chunk *tp1;
3291
3292 tsn = ntohl(desc->tsn_ifany);
3293 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3294 if (tp1->rec.data.tsn == tsn) {
3295 /* found it */
3296 break;
3297 }
3298 if (SCTP_TSN_GT(tp1->rec.data.tsn, tsn)) {
3299 /* not found */
3300 tp1 = NULL;
3301 break;
3302 }
3303 }
3304 if (tp1 == NULL) {
3305 /*
3306 * Do it the other way , aka without paying
3307 * attention to queue seq order.
3308 */
3309 SCTP_STAT_INCR(sctps_pdrpdnfnd);
3310 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3311 if (tp1->rec.data.tsn == tsn) {
3312 /* found it */
3313 break;
3314 }
3315 }
3316 }
3317 if (tp1 == NULL) {
3318 SCTP_STAT_INCR(sctps_pdrptsnnf);
3319 }
3320 if ((tp1) && (tp1->sent < SCTP_DATAGRAM_ACKED)) {
3321 uint8_t *ddp;
3322
3323 if (((flg & SCTP_BADCRC) == 0) &&
3324 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3325 return (0);
3326 }
3327 if ((stcb->asoc.peers_rwnd == 0) &&
3328 ((flg & SCTP_FROM_MIDDLE_BOX) == 0)) {
3329 SCTP_STAT_INCR(sctps_pdrpdiwnp);
3330 return (0);
3331 }
3332 if (stcb->asoc.peers_rwnd == 0 &&
3333 (flg & SCTP_FROM_MIDDLE_BOX)) {
3334 SCTP_STAT_INCR(sctps_pdrpdizrw);
3335 return (0);
3336 }
3337 ddp = (uint8_t *)(mtod(tp1->data, caddr_t)+
3338 sizeof(struct sctp_data_chunk));
3339 {
3340 unsigned int iii;
3341
3342 for (iii = 0; iii < sizeof(desc->data_bytes);
3343 iii++) {
3344 if (ddp[iii] != desc->data_bytes[iii]) {
3345 SCTP_STAT_INCR(sctps_pdrpbadd);
3346 return (-1);
3347 }
3348 }
3349 }
3350
3351 if (tp1->do_rtt) {
3352 /*
3353 * this guy had a RTO calculation
3354 * pending on it, cancel it
3355 */
3356 if (tp1->whoTo->rto_needed == 0) {
3357 tp1->whoTo->rto_needed = 1;
3358 }
3359 tp1->do_rtt = 0;
3360 }
3361 SCTP_STAT_INCR(sctps_pdrpmark);
3362 if (tp1->sent != SCTP_DATAGRAM_RESEND)
3363 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3364 /*
3365 * mark it as if we were doing a FR, since
3366 * we will be getting gap ack reports behind
3367 * the info from the router.
3368 */
3369 tp1->rec.data.doing_fast_retransmit = 1;
3370 /*
3371 * mark the tsn with what sequences can
3372 * cause a new FR.
3373 */
3374 if (TAILQ_EMPTY(&stcb->asoc.send_queue)) {
3375 tp1->rec.data.fast_retran_tsn = stcb->asoc.sending_seq;
3376 } else {
3377 tp1->rec.data.fast_retran_tsn = (TAILQ_FIRST(&stcb->asoc.send_queue))->rec.data.tsn;
3378 }
3379
3380 /* restart the timer */
3381 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3382 stcb, tp1->whoTo,
3383 SCTP_FROM_SCTP_INPUT + SCTP_LOC_26);
3384 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
3385 stcb, tp1->whoTo);
3386
3387 /* fix counts and things */
3388 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3389 sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_PDRP,
3390 tp1->whoTo->flight_size,
3391 tp1->book_size,
3392 (uint32_t)(uintptr_t)stcb,
3393 tp1->rec.data.tsn);
3394 }
3395 if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3396 sctp_flight_size_decrease(tp1);
3397 sctp_total_flight_decrease(stcb, tp1);
3398 }
3399 tp1->sent = SCTP_DATAGRAM_RESEND;
3400 } {
3401 /* audit code */
3402 unsigned int audit;
3403
3404 audit = 0;
3405 TAILQ_FOREACH(tp1, &stcb->asoc.sent_queue, sctp_next) {
3406 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3407 audit++;
3408 }
3409 TAILQ_FOREACH(tp1, &stcb->asoc.control_send_queue,
3410 sctp_next) {
3411 if (tp1->sent == SCTP_DATAGRAM_RESEND)
3412 audit++;
3413 }
3414 if (audit != stcb->asoc.sent_queue_retran_cnt) {
3415 SCTP_PRINTF("**Local Audit finds cnt:%d asoc cnt:%d\n",
3416 audit, stcb->asoc.sent_queue_retran_cnt);
3417 #ifndef SCTP_AUDITING_ENABLED
3418 stcb->asoc.sent_queue_retran_cnt = audit;
3419 #endif
3420 }
3421 }
3422 }
3423 break;
3424 case SCTP_ASCONF:
3425 {
3426 struct sctp_tmit_chunk *asconf;
3427
3428 TAILQ_FOREACH(asconf, &stcb->asoc.control_send_queue,
3429 sctp_next) {
3430 if (asconf->rec.chunk_id.id == SCTP_ASCONF) {
3431 break;
3432 }
3433 }
3434 if (asconf) {
3435 if (asconf->sent != SCTP_DATAGRAM_RESEND)
3436 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3437 asconf->sent = SCTP_DATAGRAM_RESEND;
3438 asconf->snd_count--;
3439 }
3440 }
3441 break;
3442 case SCTP_INITIATION:
3443 /* resend the INIT */
3444 stcb->asoc.dropped_special_cnt++;
3445 if (stcb->asoc.dropped_special_cnt < SCTP_RETRY_DROPPED_THRESH) {
3446 /*
3447 * If we can get it in, in a few attempts we do
3448 * this, otherwise we let the timer fire.
3449 */
3450 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, stcb->sctp_ep,
3451 stcb, net,
3452 SCTP_FROM_SCTP_INPUT + SCTP_LOC_27);
3453 sctp_send_initiate(stcb->sctp_ep, stcb, SCTP_SO_NOT_LOCKED);
3454 }
3455 break;
3456 case SCTP_SELECTIVE_ACK:
3457 case SCTP_NR_SELECTIVE_ACK:
3458 /* resend the sack */
3459 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
3460 break;
3461 case SCTP_HEARTBEAT_REQUEST:
3462 /* resend a demand HB */
3463 if ((stcb->asoc.overall_error_count + 3) < stcb->asoc.max_send_times) {
3464 /*
3465 * Only retransmit if we KNOW we wont destroy the
3466 * tcb
3467 */
3468 sctp_send_hb(stcb, net, SCTP_SO_NOT_LOCKED);
3469 }
3470 break;
3471 case SCTP_SHUTDOWN:
3472 sctp_send_shutdown(stcb, net);
3473 break;
3474 case SCTP_SHUTDOWN_ACK:
3475 sctp_send_shutdown_ack(stcb, net);
3476 break;
3477 case SCTP_COOKIE_ECHO:
3478 {
3479 struct sctp_tmit_chunk *cookie;
3480
3481 cookie = NULL;
3482 TAILQ_FOREACH(cookie, &stcb->asoc.control_send_queue,
3483 sctp_next) {
3484 if (cookie->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
3485 break;
3486 }
3487 }
3488 if (cookie) {
3489 if (cookie->sent != SCTP_DATAGRAM_RESEND)
3490 sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3491 cookie->sent = SCTP_DATAGRAM_RESEND;
3492 sctp_stop_all_cookie_timers(stcb);
3493 }
3494 }
3495 break;
3496 case SCTP_COOKIE_ACK:
3497 sctp_send_cookie_ack(stcb);
3498 break;
3499 case SCTP_ASCONF_ACK:
3500 /* resend last asconf ack */
3501 sctp_send_asconf_ack(stcb);
3502 break;
3503 case SCTP_IFORWARD_CUM_TSN:
3504 case SCTP_FORWARD_CUM_TSN:
3505 send_forward_tsn(stcb, &stcb->asoc);
3506 break;
3507 /* can't do anything with these */
3508 case SCTP_PACKET_DROPPED:
3509 case SCTP_INITIATION_ACK: /* this should not happen */
3510 case SCTP_HEARTBEAT_ACK:
3511 case SCTP_ABORT_ASSOCIATION:
3512 case SCTP_OPERATION_ERROR:
3513 case SCTP_SHUTDOWN_COMPLETE:
3514 case SCTP_ECN_ECHO:
3515 case SCTP_ECN_CWR:
3516 default:
3517 break;
3518 }
3519 return (0);
3520 }
3521
3522 void
sctp_reset_in_stream(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)3523 sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3524 {
3525 uint32_t i;
3526 uint16_t temp;
3527
3528 /*
3529 * We set things to 0xffffffff since this is the last delivered
3530 * sequence and we will be sending in 0 after the reset.
3531 */
3532
3533 if (number_entries) {
3534 for (i = 0; i < number_entries; i++) {
3535 temp = ntohs(list[i]);
3536 if (temp >= stcb->asoc.streamincnt) {
3537 continue;
3538 }
3539 stcb->asoc.strmin[temp].last_mid_delivered = 0xffffffff;
3540 }
3541 } else {
3542 list = NULL;
3543 for (i = 0; i < stcb->asoc.streamincnt; i++) {
3544 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
3545 }
3546 }
3547 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_RECV, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3548 }
3549
3550 static void
sctp_reset_out_streams(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)3551 sctp_reset_out_streams(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3552 {
3553 uint32_t i;
3554 uint16_t temp;
3555
3556 if (number_entries > 0) {
3557 for (i = 0; i < number_entries; i++) {
3558 temp = ntohs(list[i]);
3559 if (temp >= stcb->asoc.streamoutcnt) {
3560 /* no such stream */
3561 continue;
3562 }
3563 stcb->asoc.strmout[temp].next_mid_ordered = 0;
3564 stcb->asoc.strmout[temp].next_mid_unordered = 0;
3565 }
3566 } else {
3567 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3568 stcb->asoc.strmout[i].next_mid_ordered = 0;
3569 stcb->asoc.strmout[i].next_mid_unordered = 0;
3570 }
3571 }
3572 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list, SCTP_SO_NOT_LOCKED);
3573 }
3574
3575 static void
sctp_reset_clear_pending(struct sctp_tcb * stcb,uint32_t number_entries,uint16_t * list)3576 sctp_reset_clear_pending(struct sctp_tcb *stcb, uint32_t number_entries, uint16_t *list)
3577 {
3578 uint32_t i;
3579 uint16_t temp;
3580
3581 if (number_entries > 0) {
3582 for (i = 0; i < number_entries; i++) {
3583 temp = ntohs(list[i]);
3584 if (temp >= stcb->asoc.streamoutcnt) {
3585 /* no such stream */
3586 continue;
3587 }
3588 stcb->asoc.strmout[temp].state = SCTP_STREAM_OPEN;
3589 }
3590 } else {
3591 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3592 stcb->asoc.strmout[i].state = SCTP_STREAM_OPEN;
3593 }
3594 }
3595 }
3596
3597
3598 struct sctp_stream_reset_request *
sctp_find_stream_reset(struct sctp_tcb * stcb,uint32_t seq,struct sctp_tmit_chunk ** bchk)3599 sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq, struct sctp_tmit_chunk **bchk)
3600 {
3601 struct sctp_association *asoc;
3602 struct sctp_chunkhdr *ch;
3603 struct sctp_stream_reset_request *r;
3604 struct sctp_tmit_chunk *chk;
3605 int len, clen;
3606
3607 asoc = &stcb->asoc;
3608 if (TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
3609 asoc->stream_reset_outstanding = 0;
3610 return (NULL);
3611 }
3612 if (stcb->asoc.str_reset == NULL) {
3613 asoc->stream_reset_outstanding = 0;
3614 return (NULL);
3615 }
3616 chk = stcb->asoc.str_reset;
3617 if (chk->data == NULL) {
3618 return (NULL);
3619 }
3620 if (bchk) {
3621 /* he wants a copy of the chk pointer */
3622 *bchk = chk;
3623 }
3624 clen = chk->send_size;
3625 ch = mtod(chk->data, struct sctp_chunkhdr *);
3626 r = (struct sctp_stream_reset_request *)(ch + 1);
3627 if (ntohl(r->request_seq) == seq) {
3628 /* found it */
3629 return (r);
3630 }
3631 len = SCTP_SIZE32(ntohs(r->ph.param_length));
3632 if (clen > (len + (int)sizeof(struct sctp_chunkhdr))) {
3633 /* move to the next one, there can only be a max of two */
3634 r = (struct sctp_stream_reset_request *)((caddr_t)r + len);
3635 if (ntohl(r->request_seq) == seq) {
3636 return (r);
3637 }
3638 }
3639 /* that seq is not here */
3640 return (NULL);
3641 }
3642
3643 static void
sctp_clean_up_stream_reset(struct sctp_tcb * stcb)3644 sctp_clean_up_stream_reset(struct sctp_tcb *stcb)
3645 {
3646 struct sctp_association *asoc;
3647 struct sctp_tmit_chunk *chk;
3648
3649 asoc = &stcb->asoc;
3650 chk = asoc->str_reset;
3651 if (chk == NULL) {
3652 return;
3653 }
3654 asoc->str_reset = NULL;
3655 sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb,
3656 chk->whoTo, SCTP_FROM_SCTP_INPUT + SCTP_LOC_28);
3657 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3658 asoc->ctrl_queue_cnt--;
3659 if (chk->data) {
3660 sctp_m_freem(chk->data);
3661 chk->data = NULL;
3662 }
3663 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
3664 }
3665
3666
3667 static int
sctp_handle_stream_reset_response(struct sctp_tcb * stcb,uint32_t seq,uint32_t action,struct sctp_stream_reset_response * respin)3668 sctp_handle_stream_reset_response(struct sctp_tcb *stcb,
3669 uint32_t seq, uint32_t action,
3670 struct sctp_stream_reset_response *respin)
3671 {
3672 uint16_t type;
3673 int lparam_len;
3674 struct sctp_association *asoc = &stcb->asoc;
3675 struct sctp_tmit_chunk *chk;
3676 struct sctp_stream_reset_request *req_param;
3677 struct sctp_stream_reset_out_request *req_out_param;
3678 struct sctp_stream_reset_in_request *req_in_param;
3679 uint32_t number_entries;
3680
3681 if (asoc->stream_reset_outstanding == 0) {
3682 /* duplicate */
3683 return (0);
3684 }
3685 if (seq == stcb->asoc.str_reset_seq_out) {
3686 req_param = sctp_find_stream_reset(stcb, seq, &chk);
3687 if (req_param != NULL) {
3688 stcb->asoc.str_reset_seq_out++;
3689 type = ntohs(req_param->ph.param_type);
3690 lparam_len = ntohs(req_param->ph.param_length);
3691 if (type == SCTP_STR_RESET_OUT_REQUEST) {
3692 int no_clear = 0;
3693
3694 req_out_param = (struct sctp_stream_reset_out_request *)req_param;
3695 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t);
3696 asoc->stream_reset_out_is_outstanding = 0;
3697 if (asoc->stream_reset_outstanding)
3698 asoc->stream_reset_outstanding--;
3699 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3700 /* do it */
3701 sctp_reset_out_streams(stcb, number_entries, req_out_param->list_of_streams);
3702 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3703 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3704 } else if (action == SCTP_STREAM_RESET_RESULT_IN_PROGRESS) {
3705 /*
3706 * Set it up so we don't stop
3707 * retransmitting
3708 */
3709 asoc->stream_reset_outstanding++;
3710 stcb->asoc.str_reset_seq_out--;
3711 asoc->stream_reset_out_is_outstanding = 1;
3712 no_clear = 1;
3713 } else {
3714 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_OUT, stcb, number_entries, req_out_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3715 }
3716 if (no_clear == 0) {
3717 sctp_reset_clear_pending(stcb, number_entries, req_out_param->list_of_streams);
3718 }
3719 } else if (type == SCTP_STR_RESET_IN_REQUEST) {
3720 req_in_param = (struct sctp_stream_reset_in_request *)req_param;
3721 number_entries = (lparam_len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t);
3722 if (asoc->stream_reset_outstanding)
3723 asoc->stream_reset_outstanding--;
3724 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3725 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_DENIED_IN, stcb,
3726 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3727 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3728 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_FAILED_IN, stcb,
3729 number_entries, req_in_param->list_of_streams, SCTP_SO_NOT_LOCKED);
3730 }
3731 } else if (type == SCTP_STR_RESET_ADD_OUT_STREAMS) {
3732 /* Ok we now may have more streams */
3733 int num_stream;
3734
3735 num_stream = stcb->asoc.strm_pending_add_size;
3736 if (num_stream > (stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt)) {
3737 /* TSNH */
3738 num_stream = stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt;
3739 }
3740 stcb->asoc.strm_pending_add_size = 0;
3741 if (asoc->stream_reset_outstanding)
3742 asoc->stream_reset_outstanding--;
3743 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3744 /* Put the new streams into effect */
3745 int i;
3746
3747 for (i = asoc->streamoutcnt; i < (asoc->streamoutcnt + num_stream); i++) {
3748 asoc->strmout[i].state = SCTP_STREAM_OPEN;
3749 }
3750 asoc->streamoutcnt += num_stream;
3751 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
3752 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3753 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3754 SCTP_STREAM_CHANGE_DENIED);
3755 } else {
3756 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3757 SCTP_STREAM_CHANGE_FAILED);
3758 }
3759 } else if (type == SCTP_STR_RESET_ADD_IN_STREAMS) {
3760 if (asoc->stream_reset_outstanding)
3761 asoc->stream_reset_outstanding--;
3762 if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3763 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3764 SCTP_STREAM_CHANGE_DENIED);
3765 } else if (action != SCTP_STREAM_RESET_RESULT_PERFORMED) {
3766 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt,
3767 SCTP_STREAM_CHANGE_FAILED);
3768 }
3769 } else if (type == SCTP_STR_RESET_TSN_REQUEST) {
3770 /**
3771 * a) Adopt the new in tsn.
3772 * b) reset the map
3773 * c) Adopt the new out-tsn
3774 */
3775 struct sctp_stream_reset_response_tsn *resp;
3776 struct sctp_forward_tsn_chunk fwdtsn;
3777 int abort_flag = 0;
3778
3779 if (respin == NULL) {
3780 /* huh ? */
3781 return (0);
3782 }
3783 if (ntohs(respin->ph.param_length) < sizeof(struct sctp_stream_reset_response_tsn)) {
3784 return (0);
3785 }
3786 if (action == SCTP_STREAM_RESET_RESULT_PERFORMED) {
3787 resp = (struct sctp_stream_reset_response_tsn *)respin;
3788 asoc->stream_reset_outstanding--;
3789 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3790 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3791 fwdtsn.new_cumulative_tsn = htonl(ntohl(resp->senders_next_tsn) - 1);
3792 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3793 if (abort_flag) {
3794 return (1);
3795 }
3796 stcb->asoc.highest_tsn_inside_map = (ntohl(resp->senders_next_tsn) - 1);
3797 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3798 sctp_log_map(0, 7, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3799 }
3800
3801 stcb->asoc.tsn_last_delivered = stcb->asoc.cumulative_tsn = stcb->asoc.highest_tsn_inside_map;
3802 stcb->asoc.mapping_array_base_tsn = ntohl(resp->senders_next_tsn);
3803 memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
3804
3805 stcb->asoc.highest_tsn_inside_nr_map = stcb->asoc.highest_tsn_inside_map;
3806 memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.mapping_array_size);
3807
3808 stcb->asoc.sending_seq = ntohl(resp->receivers_next_tsn);
3809 stcb->asoc.last_acked_seq = stcb->asoc.cumulative_tsn;
3810
3811 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3812 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3813 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1), 0);
3814 } else if (action == SCTP_STREAM_RESET_RESULT_DENIED) {
3815 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3816 SCTP_ASSOC_RESET_DENIED);
3817 } else {
3818 sctp_notify_stream_reset_tsn(stcb, stcb->asoc.sending_seq, (stcb->asoc.mapping_array_base_tsn + 1),
3819 SCTP_ASSOC_RESET_FAILED);
3820 }
3821 }
3822 /* get rid of the request and get the request flags */
3823 if (asoc->stream_reset_outstanding == 0) {
3824 sctp_clean_up_stream_reset(stcb);
3825 }
3826 }
3827 }
3828 if (asoc->stream_reset_outstanding == 0) {
3829 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3830 }
3831 return (0);
3832 }
3833
3834 static void
sctp_handle_str_reset_request_in(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_in_request * req,int trunc)3835 sctp_handle_str_reset_request_in(struct sctp_tcb *stcb,
3836 struct sctp_tmit_chunk *chk,
3837 struct sctp_stream_reset_in_request *req, int trunc)
3838 {
3839 uint32_t seq;
3840 int len, i;
3841 int number_entries;
3842 uint16_t temp;
3843
3844 /*
3845 * peer wants me to send a str-reset to him for my outgoing seq's if
3846 * seq_in is right.
3847 */
3848 struct sctp_association *asoc = &stcb->asoc;
3849
3850 seq = ntohl(req->request_seq);
3851 if (asoc->str_reset_seq_in == seq) {
3852 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3853 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3854 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3855 } else if (trunc) {
3856 /* Can't do it, since they exceeded our buffer size */
3857 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3858 } else if (stcb->asoc.stream_reset_out_is_outstanding == 0) {
3859 len = ntohs(req->ph.param_length);
3860 number_entries = ((len - sizeof(struct sctp_stream_reset_in_request)) / sizeof(uint16_t));
3861 if (number_entries) {
3862 for (i = 0; i < number_entries; i++) {
3863 temp = ntohs(req->list_of_streams[i]);
3864 if (temp >= stcb->asoc.streamoutcnt) {
3865 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3866 goto bad_boy;
3867 }
3868 req->list_of_streams[i] = temp;
3869 }
3870 for (i = 0; i < number_entries; i++) {
3871 if (stcb->asoc.strmout[req->list_of_streams[i]].state == SCTP_STREAM_OPEN) {
3872 stcb->asoc.strmout[req->list_of_streams[i]].state = SCTP_STREAM_RESET_PENDING;
3873 }
3874 }
3875 } else {
3876 /* Its all */
3877 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3878 if (stcb->asoc.strmout[i].state == SCTP_STREAM_OPEN)
3879 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_PENDING;
3880 }
3881 }
3882 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3883 } else {
3884 /* Can't do it, since we have sent one out */
3885 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
3886 }
3887 bad_boy:
3888 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3889 asoc->str_reset_seq_in++;
3890 } else if (asoc->str_reset_seq_in - 1 == seq) {
3891 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
3892 } else if (asoc->str_reset_seq_in - 2 == seq) {
3893 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
3894 } else {
3895 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3896 }
3897 sctp_send_stream_reset_out_if_possible(stcb, SCTP_SO_NOT_LOCKED);
3898 }
3899
3900 static int
sctp_handle_str_reset_request_tsn(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_tsn_request * req)3901 sctp_handle_str_reset_request_tsn(struct sctp_tcb *stcb,
3902 struct sctp_tmit_chunk *chk,
3903 struct sctp_stream_reset_tsn_request *req)
3904 {
3905 /* reset all in and out and update the tsn */
3906 /*
3907 * A) reset my str-seq's on in and out. B) Select a receive next,
3908 * and set cum-ack to it. Also process this selected number as a
3909 * fwd-tsn as well. C) set in the response my next sending seq.
3910 */
3911 struct sctp_forward_tsn_chunk fwdtsn;
3912 struct sctp_association *asoc = &stcb->asoc;
3913 int abort_flag = 0;
3914 uint32_t seq;
3915
3916 seq = ntohl(req->request_seq);
3917 if (asoc->str_reset_seq_in == seq) {
3918 asoc->last_reset_action[1] = stcb->asoc.last_reset_action[0];
3919 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
3920 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3921 } else {
3922 fwdtsn.ch.chunk_length = htons(sizeof(struct sctp_forward_tsn_chunk));
3923 fwdtsn.ch.chunk_type = SCTP_FORWARD_CUM_TSN;
3924 fwdtsn.ch.chunk_flags = 0;
3925 fwdtsn.new_cumulative_tsn = htonl(stcb->asoc.highest_tsn_inside_map + 1);
3926 sctp_handle_forward_tsn(stcb, &fwdtsn, &abort_flag, NULL, 0);
3927 if (abort_flag) {
3928 return (1);
3929 }
3930 asoc->highest_tsn_inside_map += SCTP_STREAM_RESET_TSN_DELTA;
3931 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
3932 sctp_log_map(0, 10, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
3933 }
3934 asoc->tsn_last_delivered = asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
3935 asoc->mapping_array_base_tsn = asoc->highest_tsn_inside_map + 1;
3936 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
3937 asoc->highest_tsn_inside_nr_map = asoc->highest_tsn_inside_map;
3938 memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
3939 atomic_add_int(&asoc->sending_seq, 1);
3940 /* save off historical data for retrans */
3941 asoc->last_sending_seq[1] = asoc->last_sending_seq[0];
3942 asoc->last_sending_seq[0] = asoc->sending_seq;
3943 asoc->last_base_tsnsent[1] = asoc->last_base_tsnsent[0];
3944 asoc->last_base_tsnsent[0] = asoc->mapping_array_base_tsn;
3945 sctp_reset_out_streams(stcb, 0, (uint16_t *)NULL);
3946 sctp_reset_in_stream(stcb, 0, (uint16_t *)NULL);
3947 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3948 sctp_notify_stream_reset_tsn(stcb, asoc->sending_seq, (asoc->mapping_array_base_tsn + 1), 0);
3949 }
3950 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3951 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3952 asoc->str_reset_seq_in++;
3953 } else if (asoc->str_reset_seq_in - 1 == seq) {
3954 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[0],
3955 asoc->last_sending_seq[0], asoc->last_base_tsnsent[0]);
3956 } else if (asoc->str_reset_seq_in - 2 == seq) {
3957 sctp_add_stream_reset_result_tsn(chk, seq, asoc->last_reset_action[1],
3958 asoc->last_sending_seq[1], asoc->last_base_tsnsent[1]);
3959 } else {
3960 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
3961 }
3962 return (0);
3963 }
3964
3965 static void
sctp_handle_str_reset_request_out(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_out_request * req,int trunc)3966 sctp_handle_str_reset_request_out(struct sctp_tcb *stcb,
3967 struct sctp_tmit_chunk *chk,
3968 struct sctp_stream_reset_out_request *req, int trunc)
3969 {
3970 uint32_t seq, tsn;
3971 int number_entries, len;
3972 struct sctp_association *asoc = &stcb->asoc;
3973
3974 seq = ntohl(req->request_seq);
3975
3976 /* now if its not a duplicate we process it */
3977 if (asoc->str_reset_seq_in == seq) {
3978 len = ntohs(req->ph.param_length);
3979 number_entries = ((len - sizeof(struct sctp_stream_reset_out_request)) / sizeof(uint16_t));
3980 /*
3981 * the sender is resetting, handle the list issue.. we must
3982 * a) verify if we can do the reset, if so no problem b) If
3983 * we can't do the reset we must copy the request. c) queue
3984 * it, and setup the data in processor to trigger it off
3985 * when needed and dequeue all the queued data.
3986 */
3987 tsn = ntohl(req->send_reset_at_tsn);
3988
3989 /* move the reset action back one */
3990 asoc->last_reset_action[1] = asoc->last_reset_action[0];
3991 if (!(asoc->local_strreset_support & SCTP_ENABLE_RESET_STREAM_REQ)) {
3992 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3993 } else if (trunc) {
3994 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
3995 } else if (SCTP_TSN_GE(asoc->cumulative_tsn, tsn)) {
3996 /* we can do it now */
3997 sctp_reset_in_stream(stcb, number_entries, req->list_of_streams);
3998 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
3999 } else {
4000 /*
4001 * we must queue it up and thus wait for the TSN's
4002 * to arrive that are at or before tsn
4003 */
4004 struct sctp_stream_reset_list *liste;
4005 int siz;
4006
4007 siz = sizeof(struct sctp_stream_reset_list) + (number_entries * sizeof(uint16_t));
4008 SCTP_MALLOC(liste, struct sctp_stream_reset_list *,
4009 siz, SCTP_M_STRESET);
4010 if (liste == NULL) {
4011 /* gak out of memory */
4012 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4013 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4014 return;
4015 }
4016 liste->seq = seq;
4017 liste->tsn = tsn;
4018 liste->number_entries = number_entries;
4019 memcpy(&liste->list_of_streams, req->list_of_streams, number_entries * sizeof(uint16_t));
4020 TAILQ_INSERT_TAIL(&asoc->resetHead, liste, next_resp);
4021 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_IN_PROGRESS;
4022 }
4023 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4024 asoc->str_reset_seq_in++;
4025 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4026 /*
4027 * one seq back, just echo back last action since my
4028 * response was lost.
4029 */
4030 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4031 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4032 /*
4033 * two seq back, just echo back last action since my
4034 * response was lost.
4035 */
4036 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4037 } else {
4038 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4039 }
4040 }
4041
4042 static void
sctp_handle_str_reset_add_strm(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_add_strm * str_add)4043 sctp_handle_str_reset_add_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4044 struct sctp_stream_reset_add_strm *str_add)
4045 {
4046 /*
4047 * Peer is requesting to add more streams. If its within our
4048 * max-streams we will allow it.
4049 */
4050 uint32_t num_stream, i;
4051 uint32_t seq;
4052 struct sctp_association *asoc = &stcb->asoc;
4053 struct sctp_queued_to_read *ctl, *nctl;
4054
4055 /* Get the number. */
4056 seq = ntohl(str_add->request_seq);
4057 num_stream = ntohs(str_add->number_of_streams);
4058 /* Now what would be the new total? */
4059 if (asoc->str_reset_seq_in == seq) {
4060 num_stream += stcb->asoc.streamincnt;
4061 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4062 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4063 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4064 } else if ((num_stream > stcb->asoc.max_inbound_streams) ||
4065 (num_stream > 0xffff)) {
4066 /* We must reject it they ask for to many */
4067 denied:
4068 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4069 } else {
4070 /* Ok, we can do that :-) */
4071 struct sctp_stream_in *oldstrm;
4072
4073 /* save off the old */
4074 oldstrm = stcb->asoc.strmin;
4075 SCTP_MALLOC(stcb->asoc.strmin, struct sctp_stream_in *,
4076 (num_stream * sizeof(struct sctp_stream_in)),
4077 SCTP_M_STRMI);
4078 if (stcb->asoc.strmin == NULL) {
4079 stcb->asoc.strmin = oldstrm;
4080 goto denied;
4081 }
4082 /* copy off the old data */
4083 for (i = 0; i < stcb->asoc.streamincnt; i++) {
4084 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4085 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4086 stcb->asoc.strmin[i].sid = i;
4087 stcb->asoc.strmin[i].last_mid_delivered = oldstrm[i].last_mid_delivered;
4088 stcb->asoc.strmin[i].delivery_started = oldstrm[i].delivery_started;
4089 stcb->asoc.strmin[i].pd_api_started = oldstrm[i].pd_api_started;
4090 /* now anything on those queues? */
4091 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].inqueue, next_instrm, nctl) {
4092 TAILQ_REMOVE(&oldstrm[i].inqueue, ctl, next_instrm);
4093 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].inqueue, ctl, next_instrm);
4094 }
4095 TAILQ_FOREACH_SAFE(ctl, &oldstrm[i].uno_inqueue, next_instrm, nctl) {
4096 TAILQ_REMOVE(&oldstrm[i].uno_inqueue, ctl, next_instrm);
4097 TAILQ_INSERT_TAIL(&stcb->asoc.strmin[i].uno_inqueue, ctl, next_instrm);
4098 }
4099 }
4100 /* Init the new streams */
4101 for (i = stcb->asoc.streamincnt; i < num_stream; i++) {
4102 TAILQ_INIT(&stcb->asoc.strmin[i].inqueue);
4103 TAILQ_INIT(&stcb->asoc.strmin[i].uno_inqueue);
4104 stcb->asoc.strmin[i].sid = i;
4105 stcb->asoc.strmin[i].last_mid_delivered = 0xffffffff;
4106 stcb->asoc.strmin[i].pd_api_started = 0;
4107 stcb->asoc.strmin[i].delivery_started = 0;
4108 }
4109 SCTP_FREE(oldstrm, SCTP_M_STRMI);
4110 /* update the size */
4111 stcb->asoc.streamincnt = num_stream;
4112 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4113 sctp_notify_stream_reset_add(stcb, stcb->asoc.streamincnt, stcb->asoc.streamoutcnt, 0);
4114 }
4115 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4116 asoc->str_reset_seq_in++;
4117 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4118 /*
4119 * one seq back, just echo back last action since my
4120 * response was lost.
4121 */
4122 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4123 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4124 /*
4125 * two seq back, just echo back last action since my
4126 * response was lost.
4127 */
4128 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4129 } else {
4130 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4131
4132 }
4133 }
4134
4135 static void
sctp_handle_str_reset_add_out_strm(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,struct sctp_stream_reset_add_strm * str_add)4136 sctp_handle_str_reset_add_out_strm(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
4137 struct sctp_stream_reset_add_strm *str_add)
4138 {
4139 /*
4140 * Peer is requesting to add more streams. If its within our
4141 * max-streams we will allow it.
4142 */
4143 uint16_t num_stream;
4144 uint32_t seq;
4145 struct sctp_association *asoc = &stcb->asoc;
4146
4147 /* Get the number. */
4148 seq = ntohl(str_add->request_seq);
4149 num_stream = ntohs(str_add->number_of_streams);
4150 /* Now what would be the new total? */
4151 if (asoc->str_reset_seq_in == seq) {
4152 stcb->asoc.last_reset_action[1] = stcb->asoc.last_reset_action[0];
4153 if (!(asoc->local_strreset_support & SCTP_ENABLE_CHANGE_ASSOC_REQ)) {
4154 asoc->last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4155 } else if (stcb->asoc.stream_reset_outstanding) {
4156 /* We must reject it we have something pending */
4157 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_ERR_IN_PROGRESS;
4158 } else {
4159 /* Ok, we can do that :-) */
4160 int mychk;
4161
4162 mychk = stcb->asoc.streamoutcnt;
4163 mychk += num_stream;
4164 if (mychk < 0x10000) {
4165 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_PERFORMED;
4166 if (sctp_send_str_reset_req(stcb, 0, NULL, 0, 0, 1, num_stream, 0, 1)) {
4167 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4168 }
4169 } else {
4170 stcb->asoc.last_reset_action[0] = SCTP_STREAM_RESET_RESULT_DENIED;
4171 }
4172 }
4173 sctp_add_stream_reset_result(chk, seq, stcb->asoc.last_reset_action[0]);
4174 asoc->str_reset_seq_in++;
4175 } else if ((asoc->str_reset_seq_in - 1) == seq) {
4176 /*
4177 * one seq back, just echo back last action since my
4178 * response was lost.
4179 */
4180 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[0]);
4181 } else if ((asoc->str_reset_seq_in - 2) == seq) {
4182 /*
4183 * two seq back, just echo back last action since my
4184 * response was lost.
4185 */
4186 sctp_add_stream_reset_result(chk, seq, asoc->last_reset_action[1]);
4187 } else {
4188 sctp_add_stream_reset_result(chk, seq, SCTP_STREAM_RESET_RESULT_ERR_BAD_SEQNO);
4189 }
4190 }
4191
4192 #ifdef __GNUC__
4193 __attribute__((noinline))
4194 #endif
4195 static int
sctp_handle_stream_reset(struct sctp_tcb * stcb,struct mbuf * m,int offset,struct sctp_chunkhdr * ch_req)4196 sctp_handle_stream_reset(struct sctp_tcb *stcb, struct mbuf *m, int offset,
4197 struct sctp_chunkhdr *ch_req)
4198 {
4199 uint16_t remaining_length, param_len, ptype;
4200 struct sctp_paramhdr pstore;
4201 uint8_t cstore[SCTP_CHUNK_BUFFER_SIZE];
4202 uint32_t seq = 0;
4203 int num_req = 0;
4204 int trunc = 0;
4205 struct sctp_tmit_chunk *chk;
4206 struct sctp_chunkhdr *ch;
4207 struct sctp_paramhdr *ph;
4208 int ret_code = 0;
4209 int num_param = 0;
4210
4211 /* now it may be a reset or a reset-response */
4212 remaining_length = ntohs(ch_req->chunk_length) - sizeof(struct sctp_chunkhdr);
4213
4214 /* setup for adding the response */
4215 sctp_alloc_a_chunk(stcb, chk);
4216 if (chk == NULL) {
4217 return (ret_code);
4218 }
4219 chk->copy_by_ref = 0;
4220 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
4221 chk->rec.chunk_id.can_take_data = 0;
4222 chk->flags = 0;
4223 chk->asoc = &stcb->asoc;
4224 chk->no_fr_allowed = 0;
4225 chk->book_size = chk->send_size = sizeof(struct sctp_chunkhdr);
4226 chk->book_size_scale = 0;
4227 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4228 if (chk->data == NULL) {
4229 strres_nochunk:
4230 if (chk->data) {
4231 sctp_m_freem(chk->data);
4232 chk->data = NULL;
4233 }
4234 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
4235 return (ret_code);
4236 }
4237 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
4238
4239 /* setup chunk parameters */
4240 chk->sent = SCTP_DATAGRAM_UNSENT;
4241 chk->snd_count = 0;
4242 chk->whoTo = NULL;
4243
4244 ch = mtod(chk->data, struct sctp_chunkhdr *);
4245 ch->chunk_type = SCTP_STREAM_RESET;
4246 ch->chunk_flags = 0;
4247 ch->chunk_length = htons(chk->send_size);
4248 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
4249 offset += sizeof(struct sctp_chunkhdr);
4250 while (remaining_length >= sizeof(struct sctp_paramhdr)) {
4251 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(pstore), (uint8_t *)&pstore);
4252 if (ph == NULL) {
4253 /* TSNH */
4254 break;
4255 }
4256 param_len = ntohs(ph->param_length);
4257 if ((param_len > remaining_length) ||
4258 (param_len < (sizeof(struct sctp_paramhdr) + sizeof(uint32_t)))) {
4259 /* bad parameter length */
4260 break;
4261 }
4262 ph = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, min(param_len, sizeof(cstore)),
4263 (uint8_t *)&cstore);
4264 if (ph == NULL) {
4265 /* TSNH */
4266 break;
4267 }
4268 ptype = ntohs(ph->param_type);
4269 num_param++;
4270 if (param_len > sizeof(cstore)) {
4271 trunc = 1;
4272 } else {
4273 trunc = 0;
4274 }
4275 if (num_param > SCTP_MAX_RESET_PARAMS) {
4276 /* hit the max of parameters already sorry.. */
4277 break;
4278 }
4279 if (ptype == SCTP_STR_RESET_OUT_REQUEST) {
4280 struct sctp_stream_reset_out_request *req_out;
4281
4282 if (param_len < sizeof(struct sctp_stream_reset_out_request)) {
4283 break;
4284 }
4285 req_out = (struct sctp_stream_reset_out_request *)ph;
4286 num_req++;
4287 if (stcb->asoc.stream_reset_outstanding) {
4288 seq = ntohl(req_out->response_seq);
4289 if (seq == stcb->asoc.str_reset_seq_out) {
4290 /* implicit ack */
4291 (void)sctp_handle_stream_reset_response(stcb, seq, SCTP_STREAM_RESET_RESULT_PERFORMED, NULL);
4292 }
4293 }
4294 sctp_handle_str_reset_request_out(stcb, chk, req_out, trunc);
4295 } else if (ptype == SCTP_STR_RESET_ADD_OUT_STREAMS) {
4296 struct sctp_stream_reset_add_strm *str_add;
4297
4298 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4299 break;
4300 }
4301 str_add = (struct sctp_stream_reset_add_strm *)ph;
4302 num_req++;
4303 sctp_handle_str_reset_add_strm(stcb, chk, str_add);
4304 } else if (ptype == SCTP_STR_RESET_ADD_IN_STREAMS) {
4305 struct sctp_stream_reset_add_strm *str_add;
4306
4307 if (param_len < sizeof(struct sctp_stream_reset_add_strm)) {
4308 break;
4309 }
4310 str_add = (struct sctp_stream_reset_add_strm *)ph;
4311 num_req++;
4312 sctp_handle_str_reset_add_out_strm(stcb, chk, str_add);
4313 } else if (ptype == SCTP_STR_RESET_IN_REQUEST) {
4314 struct sctp_stream_reset_in_request *req_in;
4315
4316 num_req++;
4317 req_in = (struct sctp_stream_reset_in_request *)ph;
4318 sctp_handle_str_reset_request_in(stcb, chk, req_in, trunc);
4319 } else if (ptype == SCTP_STR_RESET_TSN_REQUEST) {
4320 struct sctp_stream_reset_tsn_request *req_tsn;
4321
4322 num_req++;
4323 req_tsn = (struct sctp_stream_reset_tsn_request *)ph;
4324 if (sctp_handle_str_reset_request_tsn(stcb, chk, req_tsn)) {
4325 ret_code = 1;
4326 goto strres_nochunk;
4327 }
4328 /* no more */
4329 break;
4330 } else if (ptype == SCTP_STR_RESET_RESPONSE) {
4331 struct sctp_stream_reset_response *resp;
4332 uint32_t result;
4333
4334 if (param_len < sizeof(struct sctp_stream_reset_response)) {
4335 break;
4336 }
4337 resp = (struct sctp_stream_reset_response *)ph;
4338 seq = ntohl(resp->response_seq);
4339 result = ntohl(resp->result);
4340 if (sctp_handle_stream_reset_response(stcb, seq, result, resp)) {
4341 ret_code = 1;
4342 goto strres_nochunk;
4343 }
4344 } else {
4345 break;
4346 }
4347 offset += SCTP_SIZE32(param_len);
4348 if (remaining_length >= SCTP_SIZE32(param_len)) {
4349 remaining_length -= SCTP_SIZE32(param_len);
4350 } else {
4351 remaining_length = 0;
4352 }
4353 }
4354 if (num_req == 0) {
4355 /* we have no response free the stuff */
4356 goto strres_nochunk;
4357 }
4358 /* ok we have a chunk to link in */
4359 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue,
4360 chk,
4361 sctp_next);
4362 stcb->asoc.ctrl_queue_cnt++;
4363 return (ret_code);
4364 }
4365
4366 /*
4367 * Handle a router or endpoints report of a packet loss, there are two ways
4368 * to handle this, either we get the whole packet and must disect it
4369 * ourselves (possibly with truncation and or corruption) or it is a summary
4370 * from a middle box that did the disectting for us.
4371 */
4372 static void
sctp_handle_packet_dropped(struct sctp_pktdrop_chunk * cp,struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t limit)4373 sctp_handle_packet_dropped(struct sctp_pktdrop_chunk *cp,
4374 struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t limit)
4375 {
4376 uint32_t bottle_bw, on_queue;
4377 uint16_t trunc_len;
4378 unsigned int chlen;
4379 unsigned int at;
4380 struct sctp_chunk_desc desc;
4381 struct sctp_chunkhdr *ch;
4382
4383 chlen = ntohs(cp->ch.chunk_length);
4384 chlen -= sizeof(struct sctp_pktdrop_chunk);
4385 /* XXX possible chlen underflow */
4386 if (chlen == 0) {
4387 ch = NULL;
4388 if (cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX)
4389 SCTP_STAT_INCR(sctps_pdrpbwrpt);
4390 } else {
4391 ch = (struct sctp_chunkhdr *)(cp->data + sizeof(struct sctphdr));
4392 chlen -= sizeof(struct sctphdr);
4393 /* XXX possible chlen underflow */
4394 memset(&desc, 0, sizeof(desc));
4395 }
4396 trunc_len = (uint16_t)ntohs(cp->trunc_len);
4397 if (trunc_len > limit) {
4398 trunc_len = limit;
4399 }
4400
4401 /* now the chunks themselves */
4402 while ((ch != NULL) && (chlen >= sizeof(struct sctp_chunkhdr))) {
4403 desc.chunk_type = ch->chunk_type;
4404 /* get amount we need to move */
4405 at = ntohs(ch->chunk_length);
4406 if (at < sizeof(struct sctp_chunkhdr)) {
4407 /* corrupt chunk, maybe at the end? */
4408 SCTP_STAT_INCR(sctps_pdrpcrupt);
4409 break;
4410 }
4411 if (trunc_len == 0) {
4412 /* we are supposed to have all of it */
4413 if (at > chlen) {
4414 /* corrupt skip it */
4415 SCTP_STAT_INCR(sctps_pdrpcrupt);
4416 break;
4417 }
4418 } else {
4419 /* is there enough of it left ? */
4420 if (desc.chunk_type == SCTP_DATA) {
4421 if (chlen < (sizeof(struct sctp_data_chunk) +
4422 sizeof(desc.data_bytes))) {
4423 break;
4424 }
4425 } else {
4426 if (chlen < sizeof(struct sctp_chunkhdr)) {
4427 break;
4428 }
4429 }
4430 }
4431 if (desc.chunk_type == SCTP_DATA) {
4432 /* can we get out the tsn? */
4433 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4434 SCTP_STAT_INCR(sctps_pdrpmbda);
4435
4436 if (chlen >= (sizeof(struct sctp_data_chunk) + sizeof(uint32_t))) {
4437 /* yep */
4438 struct sctp_data_chunk *dcp;
4439 uint8_t *ddp;
4440 unsigned int iii;
4441
4442 dcp = (struct sctp_data_chunk *)ch;
4443 ddp = (uint8_t *)(dcp + 1);
4444 for (iii = 0; iii < sizeof(desc.data_bytes); iii++) {
4445 desc.data_bytes[iii] = ddp[iii];
4446 }
4447 desc.tsn_ifany = dcp->dp.tsn;
4448 } else {
4449 /* nope we are done. */
4450 SCTP_STAT_INCR(sctps_pdrpnedat);
4451 break;
4452 }
4453 } else {
4454 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX))
4455 SCTP_STAT_INCR(sctps_pdrpmbct);
4456 }
4457
4458 if (process_chunk_drop(stcb, &desc, net, cp->ch.chunk_flags)) {
4459 SCTP_STAT_INCR(sctps_pdrppdbrk);
4460 break;
4461 }
4462 if (SCTP_SIZE32(at) > chlen) {
4463 break;
4464 }
4465 chlen -= SCTP_SIZE32(at);
4466 if (chlen < sizeof(struct sctp_chunkhdr)) {
4467 /* done, none left */
4468 break;
4469 }
4470 ch = (struct sctp_chunkhdr *)((caddr_t)ch + SCTP_SIZE32(at));
4471 }
4472 /* Now update any rwnd --- possibly */
4473 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) == 0) {
4474 /* From a peer, we get a rwnd report */
4475 uint32_t a_rwnd;
4476
4477 SCTP_STAT_INCR(sctps_pdrpfehos);
4478
4479 bottle_bw = ntohl(cp->bottle_bw);
4480 on_queue = ntohl(cp->current_onq);
4481 if (bottle_bw && on_queue) {
4482 /* a rwnd report is in here */
4483 if (bottle_bw > on_queue)
4484 a_rwnd = bottle_bw - on_queue;
4485 else
4486 a_rwnd = 0;
4487
4488 if (a_rwnd == 0)
4489 stcb->asoc.peers_rwnd = 0;
4490 else {
4491 if (a_rwnd > stcb->asoc.total_flight) {
4492 stcb->asoc.peers_rwnd =
4493 a_rwnd - stcb->asoc.total_flight;
4494 } else {
4495 stcb->asoc.peers_rwnd = 0;
4496 }
4497 if (stcb->asoc.peers_rwnd <
4498 stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4499 /* SWS sender side engages */
4500 stcb->asoc.peers_rwnd = 0;
4501 }
4502 }
4503 }
4504 } else {
4505 SCTP_STAT_INCR(sctps_pdrpfmbox);
4506 }
4507
4508 /* now middle boxes in sat networks get a cwnd bump */
4509 if ((cp->ch.chunk_flags & SCTP_FROM_MIDDLE_BOX) &&
4510 (stcb->asoc.sat_t3_loss_recovery == 0) &&
4511 (stcb->asoc.sat_network)) {
4512 /*
4513 * This is debatable but for sat networks it makes sense
4514 * Note if a T3 timer has went off, we will prohibit any
4515 * changes to cwnd until we exit the t3 loss recovery.
4516 */
4517 stcb->asoc.cc_functions.sctp_cwnd_update_after_packet_dropped(stcb,
4518 net, cp, &bottle_bw, &on_queue);
4519 }
4520 }
4521
4522 /*
4523 * handles all control chunks in a packet inputs: - m: mbuf chain, assumed to
4524 * still contain IP/SCTP header - stcb: is the tcb found for this packet -
4525 * offset: offset into the mbuf chain to first chunkhdr - length: is the
4526 * length of the complete packet outputs: - length: modified to remaining
4527 * length after control processing - netp: modified to new sctp_nets after
4528 * cookie-echo processing - return NULL to discard the packet (ie. no asoc,
4529 * bad packet,...) otherwise return the tcb for this packet
4530 */
4531 #ifdef __GNUC__
4532 __attribute__((noinline))
4533 #endif
4534 static struct sctp_tcb *
sctp_process_control(struct mbuf * m,int iphlen,int * offset,int length,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_chunkhdr * ch,struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets ** netp,int * fwd_tsn_seen,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)4535 sctp_process_control(struct mbuf *m, int iphlen, int *offset, int length,
4536 struct sockaddr *src, struct sockaddr *dst,
4537 struct sctphdr *sh, struct sctp_chunkhdr *ch, struct sctp_inpcb *inp,
4538 struct sctp_tcb *stcb, struct sctp_nets **netp, int *fwd_tsn_seen,
4539 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4540 uint32_t vrf_id, uint16_t port)
4541 {
4542 struct sctp_association *asoc;
4543 struct mbuf *op_err;
4544 char msg[SCTP_DIAG_INFO_LEN];
4545 uint32_t vtag_in;
4546 int num_chunks = 0; /* number of control chunks processed */
4547 uint32_t chk_length, contiguous;
4548 int ret;
4549 int abort_no_unlock = 0;
4550 int ecne_seen = 0;
4551
4552 /*
4553 * How big should this be, and should it be alloc'd? Lets try the
4554 * d-mtu-ceiling for now (2k) and that should hopefully work ...
4555 * until we get into jumbo grams and such..
4556 */
4557 uint8_t chunk_buf[SCTP_CHUNK_BUFFER_SIZE];
4558 int got_auth = 0;
4559 uint32_t auth_offset = 0, auth_len = 0;
4560 int auth_skipped = 0;
4561 int asconf_cnt = 0;
4562 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4563 struct socket *so;
4564 #endif
4565
4566 SCTPDBG(SCTP_DEBUG_INPUT1, "sctp_process_control: iphlen=%u, offset=%u, length=%u stcb:%p\n",
4567 iphlen, *offset, length, (void *)stcb);
4568
4569 if (stcb) {
4570 SCTP_TCB_LOCK_ASSERT(stcb);
4571 }
4572 /* validate chunk header length... */
4573 if (ntohs(ch->chunk_length) < sizeof(*ch)) {
4574 SCTPDBG(SCTP_DEBUG_INPUT1, "Invalid header length %d\n",
4575 ntohs(ch->chunk_length));
4576 *offset = length;
4577 return (stcb);
4578 }
4579 /*
4580 * validate the verification tag
4581 */
4582 vtag_in = ntohl(sh->v_tag);
4583
4584 if (ch->chunk_type == SCTP_INITIATION) {
4585 SCTPDBG(SCTP_DEBUG_INPUT1, "Its an INIT of len:%d vtag:%x\n",
4586 ntohs(ch->chunk_length), vtag_in);
4587 if (vtag_in != 0) {
4588 /* protocol error- silently discard... */
4589 SCTP_STAT_INCR(sctps_badvtag);
4590 if (stcb != NULL) {
4591 SCTP_TCB_UNLOCK(stcb);
4592 }
4593 return (NULL);
4594 }
4595 } else if (ch->chunk_type != SCTP_COOKIE_ECHO) {
4596 /*
4597 * If there is no stcb, skip the AUTH chunk and process
4598 * later after a stcb is found (to validate the lookup was
4599 * valid.
4600 */
4601 if ((ch->chunk_type == SCTP_AUTHENTICATION) &&
4602 (stcb == NULL) &&
4603 (inp->auth_supported == 1)) {
4604 /* save this chunk for later processing */
4605 auth_skipped = 1;
4606 auth_offset = *offset;
4607 auth_len = ntohs(ch->chunk_length);
4608
4609 /* (temporarily) move past this chunk */
4610 *offset += SCTP_SIZE32(auth_len);
4611 if (*offset >= length) {
4612 /* no more data left in the mbuf chain */
4613 *offset = length;
4614 return (NULL);
4615 }
4616 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4617 sizeof(struct sctp_chunkhdr), chunk_buf);
4618 }
4619 if (ch == NULL) {
4620 /* Help */
4621 *offset = length;
4622 return (stcb);
4623 }
4624 if (ch->chunk_type == SCTP_COOKIE_ECHO) {
4625 goto process_control_chunks;
4626 }
4627 /*
4628 * first check if it's an ASCONF with an unknown src addr we
4629 * need to look inside to find the association
4630 */
4631 if (ch->chunk_type == SCTP_ASCONF && stcb == NULL) {
4632 struct sctp_chunkhdr *asconf_ch = ch;
4633 uint32_t asconf_offset = 0, asconf_len = 0;
4634
4635 /* inp's refcount may be reduced */
4636 SCTP_INP_INCR_REF(inp);
4637
4638 asconf_offset = *offset;
4639 do {
4640 asconf_len = ntohs(asconf_ch->chunk_length);
4641 if (asconf_len < sizeof(struct sctp_asconf_paramhdr))
4642 break;
4643 stcb = sctp_findassociation_ep_asconf(m,
4644 *offset,
4645 dst,
4646 sh, &inp, netp, vrf_id);
4647 if (stcb != NULL)
4648 break;
4649 asconf_offset += SCTP_SIZE32(asconf_len);
4650 asconf_ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, asconf_offset,
4651 sizeof(struct sctp_chunkhdr), chunk_buf);
4652 } while (asconf_ch != NULL && asconf_ch->chunk_type == SCTP_ASCONF);
4653 if (stcb == NULL) {
4654 /*
4655 * reduce inp's refcount if not reduced in
4656 * sctp_findassociation_ep_asconf().
4657 */
4658 SCTP_INP_DECR_REF(inp);
4659 }
4660
4661 /* now go back and verify any auth chunk to be sure */
4662 if (auth_skipped && (stcb != NULL)) {
4663 struct sctp_auth_chunk *auth;
4664
4665 auth = (struct sctp_auth_chunk *)
4666 sctp_m_getptr(m, auth_offset,
4667 auth_len, chunk_buf);
4668 got_auth = 1;
4669 auth_skipped = 0;
4670 if ((auth == NULL) || sctp_handle_auth(stcb, auth, m,
4671 auth_offset)) {
4672 /* auth HMAC failed so dump it */
4673 *offset = length;
4674 return (stcb);
4675 } else {
4676 /* remaining chunks are HMAC checked */
4677 stcb->asoc.authenticated = 1;
4678 }
4679 }
4680 }
4681 if (stcb == NULL) {
4682 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4683 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4684 msg);
4685 /* no association, so it's out of the blue... */
4686 sctp_handle_ootb(m, iphlen, *offset, src, dst, sh, inp, op_err,
4687 mflowtype, mflowid, inp->fibnum,
4688 vrf_id, port);
4689 *offset = length;
4690 return (NULL);
4691 }
4692 asoc = &stcb->asoc;
4693 /* ABORT and SHUTDOWN can use either v_tag... */
4694 if ((ch->chunk_type == SCTP_ABORT_ASSOCIATION) ||
4695 (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) ||
4696 (ch->chunk_type == SCTP_PACKET_DROPPED)) {
4697 /* Take the T-bit always into account. */
4698 if ((((ch->chunk_flags & SCTP_HAD_NO_TCB) == 0) &&
4699 (vtag_in == asoc->my_vtag)) ||
4700 (((ch->chunk_flags & SCTP_HAD_NO_TCB) == SCTP_HAD_NO_TCB) &&
4701 (asoc->peer_vtag != htonl(0)) &&
4702 (vtag_in == asoc->peer_vtag))) {
4703 /* this is valid */
4704 } else {
4705 /* drop this packet... */
4706 SCTP_STAT_INCR(sctps_badvtag);
4707 if (stcb != NULL) {
4708 SCTP_TCB_UNLOCK(stcb);
4709 }
4710 return (NULL);
4711 }
4712 } else if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
4713 if (vtag_in != asoc->my_vtag) {
4714 /*
4715 * this could be a stale SHUTDOWN-ACK or the
4716 * peer never got the SHUTDOWN-COMPLETE and
4717 * is still hung; we have started a new asoc
4718 * but it won't complete until the shutdown
4719 * is completed
4720 */
4721 if (stcb != NULL) {
4722 SCTP_TCB_UNLOCK(stcb);
4723 }
4724 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
4725 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
4726 msg);
4727 sctp_handle_ootb(m, iphlen, *offset, src, dst,
4728 sh, inp, op_err,
4729 mflowtype, mflowid, fibnum,
4730 vrf_id, port);
4731 return (NULL);
4732 }
4733 } else {
4734 /* for all other chunks, vtag must match */
4735 if (vtag_in != asoc->my_vtag) {
4736 /* invalid vtag... */
4737 SCTPDBG(SCTP_DEBUG_INPUT3,
4738 "invalid vtag: %xh, expect %xh\n",
4739 vtag_in, asoc->my_vtag);
4740 SCTP_STAT_INCR(sctps_badvtag);
4741 if (stcb != NULL) {
4742 SCTP_TCB_UNLOCK(stcb);
4743 }
4744 *offset = length;
4745 return (NULL);
4746 }
4747 }
4748 } /* end if !SCTP_COOKIE_ECHO */
4749 /*
4750 * process all control chunks...
4751 */
4752 if (((ch->chunk_type == SCTP_SELECTIVE_ACK) ||
4753 (ch->chunk_type == SCTP_NR_SELECTIVE_ACK) ||
4754 (ch->chunk_type == SCTP_HEARTBEAT_REQUEST)) &&
4755 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4756 /* implied cookie-ack.. we must have lost the ack */
4757 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb,
4758 *netp);
4759 }
4760
4761 process_control_chunks:
4762 while (IS_SCTP_CONTROL(ch)) {
4763 /* validate chunk length */
4764 chk_length = ntohs(ch->chunk_length);
4765 SCTPDBG(SCTP_DEBUG_INPUT2, "sctp_process_control: processing a chunk type=%u, len=%u\n",
4766 ch->chunk_type, chk_length);
4767 SCTP_LTRACE_CHK(inp, stcb, ch->chunk_type, chk_length);
4768 if (chk_length < sizeof(*ch) ||
4769 (*offset + (int)chk_length) > length) {
4770 *offset = length;
4771 return (stcb);
4772 }
4773 SCTP_STAT_INCR_COUNTER64(sctps_incontrolchunks);
4774 /*
4775 * INIT and INIT-ACK only gets the init ack "header" portion
4776 * only because we don't have to process the peer's COOKIE.
4777 * All others get a complete chunk.
4778 */
4779 switch (ch->chunk_type) {
4780 case SCTP_INITIATION:
4781 contiguous = sizeof(struct sctp_init_chunk);
4782 break;
4783 case SCTP_INITIATION_ACK:
4784 contiguous = sizeof(struct sctp_init_ack_chunk);
4785 break;
4786 default:
4787 contiguous = min(chk_length, sizeof(chunk_buf));
4788 break;
4789 }
4790 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
4791 contiguous,
4792 chunk_buf);
4793 if (ch == NULL) {
4794 *offset = length;
4795 if (stcb != NULL) {
4796 SCTP_TCB_UNLOCK(stcb);
4797 }
4798 return (NULL);
4799 }
4800
4801 num_chunks++;
4802 /* Save off the last place we got a control from */
4803 if (stcb != NULL) {
4804 if (((netp != NULL) && (*netp != NULL)) || (ch->chunk_type == SCTP_ASCONF)) {
4805 /*
4806 * allow last_control to be NULL if
4807 * ASCONF... ASCONF processing will find the
4808 * right net later
4809 */
4810 if ((netp != NULL) && (*netp != NULL))
4811 stcb->asoc.last_control_chunk_from = *netp;
4812 }
4813 }
4814 #ifdef SCTP_AUDITING_ENABLED
4815 sctp_audit_log(0xB0, ch->chunk_type);
4816 #endif
4817
4818 /* check to see if this chunk required auth, but isn't */
4819 if ((stcb != NULL) &&
4820 sctp_auth_is_required_chunk(ch->chunk_type, stcb->asoc.local_auth_chunks) &&
4821 !stcb->asoc.authenticated) {
4822 /* "silently" ignore */
4823 SCTP_STAT_INCR(sctps_recvauthmissing);
4824 goto next_chunk;
4825 }
4826 switch (ch->chunk_type) {
4827 case SCTP_INITIATION:
4828 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT\n");
4829 /* The INIT chunk must be the only chunk. */
4830 if ((num_chunks > 1) ||
4831 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4832 /* RFC 4960 requires that no ABORT is sent */
4833 *offset = length;
4834 if (stcb != NULL) {
4835 SCTP_TCB_UNLOCK(stcb);
4836 }
4837 return (NULL);
4838 }
4839 /* Honor our resource limit. */
4840 if (chk_length > SCTP_LARGEST_INIT_ACCEPTED) {
4841 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
4842 sctp_abort_association(inp, stcb, m, iphlen,
4843 src, dst, sh, op_err,
4844 mflowtype, mflowid,
4845 vrf_id, port);
4846 *offset = length;
4847 return (NULL);
4848 }
4849 sctp_handle_init(m, iphlen, *offset, src, dst, sh,
4850 (struct sctp_init_chunk *)ch, inp,
4851 stcb, *netp, &abort_no_unlock,
4852 mflowtype, mflowid,
4853 vrf_id, port);
4854 *offset = length;
4855 if ((!abort_no_unlock) && (stcb != NULL)) {
4856 SCTP_TCB_UNLOCK(stcb);
4857 }
4858 return (NULL);
4859 break;
4860 case SCTP_PAD_CHUNK:
4861 break;
4862 case SCTP_INITIATION_ACK:
4863 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_INIT_ACK\n");
4864 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4865 /* We are not interested anymore */
4866 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size)) {
4867 ;
4868 } else {
4869 *offset = length;
4870 if (stcb != NULL) {
4871 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4872 so = SCTP_INP_SO(inp);
4873 atomic_add_int(&stcb->asoc.refcnt, 1);
4874 SCTP_TCB_UNLOCK(stcb);
4875 SCTP_SOCKET_LOCK(so, 1);
4876 SCTP_TCB_LOCK(stcb);
4877 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4878 #endif
4879 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4880 SCTP_FROM_SCTP_INPUT + SCTP_LOC_29);
4881 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4882 SCTP_SOCKET_UNLOCK(so, 1);
4883 #endif
4884 }
4885 return (NULL);
4886 }
4887 }
4888 /* The INIT-ACK chunk must be the only chunk. */
4889 if ((num_chunks > 1) ||
4890 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
4891 *offset = length;
4892 return (stcb);
4893 }
4894 if ((netp != NULL) && (*netp != NULL)) {
4895 ret = sctp_handle_init_ack(m, iphlen, *offset,
4896 src, dst, sh,
4897 (struct sctp_init_ack_chunk *)ch,
4898 stcb, *netp,
4899 &abort_no_unlock,
4900 mflowtype, mflowid,
4901 vrf_id);
4902 } else {
4903 ret = -1;
4904 }
4905 *offset = length;
4906 if (abort_no_unlock) {
4907 return (NULL);
4908 }
4909 /*
4910 * Special case, I must call the output routine to
4911 * get the cookie echoed
4912 */
4913 if ((stcb != NULL) && (ret == 0)) {
4914 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
4915 }
4916 return (stcb);
4917 break;
4918 case SCTP_SELECTIVE_ACK:
4919 case SCTP_NR_SELECTIVE_ACK:
4920 {
4921 int abort_now = 0;
4922 uint32_t a_rwnd, cum_ack;
4923 uint16_t num_seg, num_nr_seg, num_dup;
4924 uint8_t flags;
4925 int offset_seg, offset_dup;
4926
4927 SCTPDBG(SCTP_DEBUG_INPUT3, "%s\n",
4928 ch->chunk_type == SCTP_SELECTIVE_ACK ? "SCTP_SACK" : "SCTP_NR_SACK");
4929 SCTP_STAT_INCR(sctps_recvsacks);
4930 if (stcb == NULL) {
4931 SCTPDBG(SCTP_DEBUG_INDATA1, "No stcb when processing %s chunk\n",
4932 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK");
4933 break;
4934 }
4935 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4936 if (chk_length < sizeof(struct sctp_sack_chunk)) {
4937 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on SACK chunk, too small\n");
4938 break;
4939 }
4940 } else {
4941 if (stcb->asoc.nrsack_supported == 0) {
4942 goto unknown_chunk;
4943 }
4944 if (chk_length < sizeof(struct sctp_nr_sack_chunk)) {
4945 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size on NR_SACK chunk, too small\n");
4946 break;
4947 }
4948 }
4949 if (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) {
4950 /*-
4951 * If we have sent a shutdown-ack, we will pay no
4952 * attention to a sack sent in to us since
4953 * we don't care anymore.
4954 */
4955 break;
4956 }
4957 flags = ch->chunk_flags;
4958 if (ch->chunk_type == SCTP_SELECTIVE_ACK) {
4959 struct sctp_sack_chunk *sack;
4960
4961 sack = (struct sctp_sack_chunk *)ch;
4962 cum_ack = ntohl(sack->sack.cum_tsn_ack);
4963 num_seg = ntohs(sack->sack.num_gap_ack_blks);
4964 num_nr_seg = 0;
4965 num_dup = ntohs(sack->sack.num_dup_tsns);
4966 a_rwnd = ntohl(sack->sack.a_rwnd);
4967 if (sizeof(struct sctp_sack_chunk) +
4968 num_seg * sizeof(struct sctp_gap_ack_block) +
4969 num_dup * sizeof(uint32_t) != chk_length) {
4970 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of SACK chunk\n");
4971 break;
4972 }
4973 offset_seg = *offset + sizeof(struct sctp_sack_chunk);
4974 offset_dup = offset_seg + num_seg * sizeof(struct sctp_gap_ack_block);
4975 } else {
4976 struct sctp_nr_sack_chunk *nr_sack;
4977
4978 nr_sack = (struct sctp_nr_sack_chunk *)ch;
4979 cum_ack = ntohl(nr_sack->nr_sack.cum_tsn_ack);
4980 num_seg = ntohs(nr_sack->nr_sack.num_gap_ack_blks);
4981 num_nr_seg = ntohs(nr_sack->nr_sack.num_nr_gap_ack_blks);
4982 num_dup = ntohs(nr_sack->nr_sack.num_dup_tsns);
4983 a_rwnd = ntohl(nr_sack->nr_sack.a_rwnd);
4984 if (sizeof(struct sctp_nr_sack_chunk) +
4985 (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block) +
4986 num_dup * sizeof(uint32_t) != chk_length) {
4987 SCTPDBG(SCTP_DEBUG_INDATA1, "Bad size of NR_SACK chunk\n");
4988 break;
4989 }
4990 offset_seg = *offset + sizeof(struct sctp_nr_sack_chunk);
4991 offset_dup = offset_seg + (num_seg + num_nr_seg) * sizeof(struct sctp_gap_ack_block);
4992 }
4993 SCTPDBG(SCTP_DEBUG_INPUT3, "%s process cum_ack:%x num_seg:%d a_rwnd:%d\n",
4994 (ch->chunk_type == SCTP_SELECTIVE_ACK) ? "SCTP_SACK" : "SCTP_NR_SACK",
4995 cum_ack, num_seg, a_rwnd);
4996 stcb->asoc.seen_a_sack_this_pkt = 1;
4997 if ((stcb->asoc.pr_sctp_cnt == 0) &&
4998 (num_seg == 0) && (num_nr_seg == 0) &&
4999 SCTP_TSN_GE(cum_ack, stcb->asoc.last_acked_seq) &&
5000 (stcb->asoc.saw_sack_with_frags == 0) &&
5001 (stcb->asoc.saw_sack_with_nr_frags == 0) &&
5002 (!TAILQ_EMPTY(&stcb->asoc.sent_queue))) {
5003 /*
5004 * We have a SIMPLE sack having no
5005 * prior segments and data on sent
5006 * queue to be acked. Use the faster
5007 * path sack processing. We also
5008 * allow window update sacks with no
5009 * missing segments to go this way
5010 * too.
5011 */
5012 sctp_express_handle_sack(stcb, cum_ack, a_rwnd,
5013 &abort_now, ecne_seen);
5014 } else {
5015 if ((netp != NULL) && (*netp != NULL)) {
5016 sctp_handle_sack(m, offset_seg, offset_dup, stcb,
5017 num_seg, num_nr_seg, num_dup, &abort_now, flags,
5018 cum_ack, a_rwnd, ecne_seen);
5019 }
5020 }
5021 if (abort_now) {
5022 /* ABORT signal from sack processing */
5023 *offset = length;
5024 return (NULL);
5025 }
5026 if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
5027 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
5028 (stcb->asoc.stream_queue_cnt == 0)) {
5029 sctp_ulp_notify(SCTP_NOTIFY_SENDER_DRY, stcb, 0, NULL, SCTP_SO_NOT_LOCKED);
5030 }
5031 break;
5032 }
5033 case SCTP_HEARTBEAT_REQUEST:
5034 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT\n");
5035 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5036 SCTP_STAT_INCR(sctps_recvheartbeat);
5037 sctp_send_heartbeat_ack(stcb, m, *offset,
5038 chk_length, *netp);
5039 }
5040 break;
5041 case SCTP_HEARTBEAT_ACK:
5042 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_HEARTBEAT_ACK\n");
5043 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_heartbeat_chunk))) {
5044 /* Its not ours */
5045 *offset = length;
5046 return (stcb);
5047 }
5048 SCTP_STAT_INCR(sctps_recvheartbeatack);
5049 if ((netp != NULL) && (*netp != NULL)) {
5050 sctp_handle_heartbeat_ack((struct sctp_heartbeat_chunk *)ch,
5051 stcb, *netp);
5052 }
5053 break;
5054 case SCTP_ABORT_ASSOCIATION:
5055 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ABORT, stcb %p\n",
5056 (void *)stcb);
5057 *offset = length;
5058 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5059 if (sctp_handle_abort((struct sctp_abort_chunk *)ch, stcb, *netp)) {
5060 return (NULL);
5061 } else {
5062 return (stcb);
5063 }
5064 } else {
5065 return (NULL);
5066 }
5067 break;
5068 case SCTP_SHUTDOWN:
5069 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN, stcb %p\n",
5070 (void *)stcb);
5071 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_shutdown_chunk))) {
5072 *offset = length;
5073 return (stcb);
5074 }
5075 if ((netp != NULL) && (*netp != NULL)) {
5076 int abort_flag = 0;
5077
5078 sctp_handle_shutdown((struct sctp_shutdown_chunk *)ch,
5079 stcb, *netp, &abort_flag);
5080 if (abort_flag) {
5081 *offset = length;
5082 return (NULL);
5083 }
5084 }
5085 break;
5086 case SCTP_SHUTDOWN_ACK:
5087 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_ACK, stcb %p\n", (void *)stcb);
5088 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5089 sctp_handle_shutdown_ack((struct sctp_shutdown_ack_chunk *)ch, stcb, *netp);
5090 }
5091 *offset = length;
5092 return (NULL);
5093 break;
5094 case SCTP_OPERATION_ERROR:
5095 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_OP_ERR\n");
5096 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL) &&
5097 sctp_handle_error(ch, stcb, *netp, contiguous) < 0) {
5098 *offset = length;
5099 return (NULL);
5100 }
5101 break;
5102 case SCTP_COOKIE_ECHO:
5103 SCTPDBG(SCTP_DEBUG_INPUT3,
5104 "SCTP_COOKIE_ECHO, stcb %p\n", (void *)stcb);
5105 if ((stcb != NULL) && (stcb->asoc.total_output_queue_size > 0)) {
5106 ;
5107 } else {
5108 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5109 /* We are not interested anymore */
5110 abend:
5111 if (stcb != NULL) {
5112 SCTP_TCB_UNLOCK(stcb);
5113 }
5114 *offset = length;
5115 return (NULL);
5116 }
5117 }
5118 /*-
5119 * First are we accepting? We do this again here
5120 * since it is possible that a previous endpoint WAS
5121 * listening responded to a INIT-ACK and then
5122 * closed. We opened and bound.. and are now no
5123 * longer listening.
5124 *
5125 * XXXGL: notes on checking listen queue length.
5126 * 1) SCTP_IS_LISTENING() doesn't necessarily mean
5127 * SOLISTENING(), because a listening "UDP type"
5128 * socket isn't listening in terms of the socket
5129 * layer. It is a normal data flow socket, that
5130 * can fork off new connections. Thus, we should
5131 * look into sol_qlen only in case we are !UDP.
5132 * 2) Checking sol_qlen in general requires locking
5133 * the socket, and this code lacks that.
5134 */
5135 if ((stcb == NULL) &&
5136 (!SCTP_IS_LISTENING(inp) ||
5137 (!(inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
5138 inp->sctp_socket->sol_qlen >= inp->sctp_socket->sol_qlimit))) {
5139 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5140 (SCTP_BASE_SYSCTL(sctp_abort_if_one_2_one_hits_limit))) {
5141 op_err = sctp_generate_cause(SCTP_CAUSE_OUT_OF_RESC, "");
5142 sctp_abort_association(inp, stcb, m, iphlen,
5143 src, dst, sh, op_err,
5144 mflowtype, mflowid,
5145 vrf_id, port);
5146 }
5147 *offset = length;
5148 return (NULL);
5149 } else {
5150 struct mbuf *ret_buf;
5151 struct sctp_inpcb *linp;
5152
5153 if (stcb) {
5154 linp = NULL;
5155 } else {
5156 linp = inp;
5157 }
5158
5159 if (linp != NULL) {
5160 SCTP_ASOC_CREATE_LOCK(linp);
5161 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
5162 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5163 SCTP_ASOC_CREATE_UNLOCK(linp);
5164 goto abend;
5165 }
5166 }
5167
5168 if (netp != NULL) {
5169 struct sctp_tcb *locked_stcb;
5170
5171 locked_stcb = stcb;
5172 ret_buf =
5173 sctp_handle_cookie_echo(m, iphlen,
5174 *offset,
5175 src, dst,
5176 sh,
5177 (struct sctp_cookie_echo_chunk *)ch,
5178 &inp, &stcb, netp,
5179 auth_skipped,
5180 auth_offset,
5181 auth_len,
5182 &locked_stcb,
5183 mflowtype,
5184 mflowid,
5185 vrf_id,
5186 port);
5187 if ((locked_stcb != NULL) && (locked_stcb != stcb)) {
5188 SCTP_TCB_UNLOCK(locked_stcb);
5189 }
5190 if (stcb != NULL) {
5191 SCTP_TCB_LOCK_ASSERT(stcb);
5192 }
5193 } else {
5194 ret_buf = NULL;
5195 }
5196 if (linp != NULL) {
5197 SCTP_ASOC_CREATE_UNLOCK(linp);
5198 }
5199 if (ret_buf == NULL) {
5200 if (stcb != NULL) {
5201 SCTP_TCB_UNLOCK(stcb);
5202 }
5203 SCTPDBG(SCTP_DEBUG_INPUT3,
5204 "GAK, null buffer\n");
5205 *offset = length;
5206 return (NULL);
5207 }
5208 /* if AUTH skipped, see if it verified... */
5209 if (auth_skipped) {
5210 got_auth = 1;
5211 auth_skipped = 0;
5212 }
5213 if (!TAILQ_EMPTY(&stcb->asoc.sent_queue)) {
5214 /*
5215 * Restart the timer if we have
5216 * pending data
5217 */
5218 struct sctp_tmit_chunk *chk;
5219
5220 chk = TAILQ_FIRST(&stcb->asoc.sent_queue);
5221 sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
5222 }
5223 }
5224 break;
5225 case SCTP_COOKIE_ACK:
5226 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_COOKIE_ACK, stcb %p\n", (void *)stcb);
5227 if ((stcb == NULL) || chk_length != sizeof(struct sctp_cookie_ack_chunk)) {
5228 return (stcb);
5229 }
5230 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5231 /* We are not interested anymore */
5232 if ((stcb) && (stcb->asoc.total_output_queue_size)) {
5233 ;
5234 } else if (stcb) {
5235 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5236 so = SCTP_INP_SO(inp);
5237 atomic_add_int(&stcb->asoc.refcnt, 1);
5238 SCTP_TCB_UNLOCK(stcb);
5239 SCTP_SOCKET_LOCK(so, 1);
5240 SCTP_TCB_LOCK(stcb);
5241 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5242 #endif
5243 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5244 SCTP_FROM_SCTP_INPUT + SCTP_LOC_30);
5245 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5246 SCTP_SOCKET_UNLOCK(so, 1);
5247 #endif
5248 *offset = length;
5249 return (NULL);
5250 }
5251 }
5252 if ((netp != NULL) && (*netp != NULL)) {
5253 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, *netp);
5254 }
5255 break;
5256 case SCTP_ECN_ECHO:
5257 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_ECHO\n");
5258 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_ecne_chunk))) {
5259 /* Its not ours */
5260 *offset = length;
5261 return (stcb);
5262 }
5263 if (stcb->asoc.ecn_supported == 0) {
5264 goto unknown_chunk;
5265 }
5266 sctp_handle_ecn_echo((struct sctp_ecne_chunk *)ch, stcb);
5267 ecne_seen = 1;
5268 break;
5269 case SCTP_ECN_CWR:
5270 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ECN_CWR\n");
5271 if ((stcb == NULL) || (chk_length != sizeof(struct sctp_cwr_chunk))) {
5272 *offset = length;
5273 return (stcb);
5274 }
5275 if (stcb->asoc.ecn_supported == 0) {
5276 goto unknown_chunk;
5277 }
5278 sctp_handle_ecn_cwr((struct sctp_cwr_chunk *)ch, stcb, *netp);
5279 break;
5280 case SCTP_SHUTDOWN_COMPLETE:
5281 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_SHUTDOWN_COMPLETE, stcb %p\n", (void *)stcb);
5282 /* must be first and only chunk */
5283 if ((num_chunks > 1) ||
5284 (length - *offset > (int)SCTP_SIZE32(chk_length))) {
5285 *offset = length;
5286 return (stcb);
5287 }
5288 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5289 sctp_handle_shutdown_complete((struct sctp_shutdown_complete_chunk *)ch,
5290 stcb, *netp);
5291 }
5292 *offset = length;
5293 return (NULL);
5294 break;
5295 case SCTP_ASCONF:
5296 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF\n");
5297 if (stcb != NULL) {
5298 if (stcb->asoc.asconf_supported == 0) {
5299 goto unknown_chunk;
5300 }
5301 sctp_handle_asconf(m, *offset, src,
5302 (struct sctp_asconf_chunk *)ch, stcb, asconf_cnt == 0);
5303 asconf_cnt++;
5304 }
5305 break;
5306 case SCTP_ASCONF_ACK:
5307 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_ASCONF_ACK\n");
5308 if (chk_length < sizeof(struct sctp_asconf_ack_chunk)) {
5309 /* Its not ours */
5310 *offset = length;
5311 return (stcb);
5312 }
5313 if ((stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5314 if (stcb->asoc.asconf_supported == 0) {
5315 goto unknown_chunk;
5316 }
5317 /* He's alive so give him credit */
5318 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
5319 sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
5320 stcb->asoc.overall_error_count,
5321 0,
5322 SCTP_FROM_SCTP_INPUT,
5323 __LINE__);
5324 }
5325 stcb->asoc.overall_error_count = 0;
5326 sctp_handle_asconf_ack(m, *offset,
5327 (struct sctp_asconf_ack_chunk *)ch, stcb, *netp, &abort_no_unlock);
5328 if (abort_no_unlock)
5329 return (NULL);
5330 }
5331 break;
5332 case SCTP_FORWARD_CUM_TSN:
5333 case SCTP_IFORWARD_CUM_TSN:
5334 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_FWD_TSN\n");
5335 if (chk_length < sizeof(struct sctp_forward_tsn_chunk)) {
5336 /* Its not ours */
5337 *offset = length;
5338 return (stcb);
5339 }
5340
5341 if (stcb != NULL) {
5342 int abort_flag = 0;
5343
5344 if (stcb->asoc.prsctp_supported == 0) {
5345 goto unknown_chunk;
5346 }
5347 *fwd_tsn_seen = 1;
5348 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5349 /* We are not interested anymore */
5350 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5351 so = SCTP_INP_SO(inp);
5352 atomic_add_int(&stcb->asoc.refcnt, 1);
5353 SCTP_TCB_UNLOCK(stcb);
5354 SCTP_SOCKET_LOCK(so, 1);
5355 SCTP_TCB_LOCK(stcb);
5356 atomic_subtract_int(&stcb->asoc.refcnt, 1);
5357 #endif
5358 (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
5359 SCTP_FROM_SCTP_INPUT + SCTP_LOC_31);
5360 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5361 SCTP_SOCKET_UNLOCK(so, 1);
5362 #endif
5363 *offset = length;
5364 return (NULL);
5365 }
5366 /*
5367 * For sending a SACK this looks like DATA
5368 * chunks.
5369 */
5370 stcb->asoc.last_data_chunk_from = stcb->asoc.last_control_chunk_from;
5371 sctp_handle_forward_tsn(stcb,
5372 (struct sctp_forward_tsn_chunk *)ch, &abort_flag, m, *offset);
5373 if (abort_flag) {
5374 *offset = length;
5375 return (NULL);
5376 }
5377 }
5378 break;
5379 case SCTP_STREAM_RESET:
5380 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_STREAM_RESET\n");
5381 if (((stcb == NULL) || (ch == NULL) || (chk_length < sizeof(struct sctp_stream_reset_tsn_req)))) {
5382 /* Its not ours */
5383 *offset = length;
5384 return (stcb);
5385 }
5386 if (stcb->asoc.reconfig_supported == 0) {
5387 goto unknown_chunk;
5388 }
5389 if (sctp_handle_stream_reset(stcb, m, *offset, ch)) {
5390 /* stop processing */
5391 *offset = length;
5392 return (NULL);
5393 }
5394 break;
5395 case SCTP_PACKET_DROPPED:
5396 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_PACKET_DROPPED\n");
5397 /* re-get it all please */
5398 if (chk_length < sizeof(struct sctp_pktdrop_chunk)) {
5399 /* Its not ours */
5400 *offset = length;
5401 return (stcb);
5402 }
5403
5404 if ((ch != NULL) && (stcb != NULL) && (netp != NULL) && (*netp != NULL)) {
5405 if (stcb->asoc.pktdrop_supported == 0) {
5406 goto unknown_chunk;
5407 }
5408 sctp_handle_packet_dropped((struct sctp_pktdrop_chunk *)ch,
5409 stcb, *netp,
5410 min(chk_length, contiguous));
5411 }
5412 break;
5413 case SCTP_AUTHENTICATION:
5414 SCTPDBG(SCTP_DEBUG_INPUT3, "SCTP_AUTHENTICATION\n");
5415 if (stcb == NULL) {
5416 /* save the first AUTH for later processing */
5417 if (auth_skipped == 0) {
5418 auth_offset = *offset;
5419 auth_len = chk_length;
5420 auth_skipped = 1;
5421 }
5422 /* skip this chunk (temporarily) */
5423 goto next_chunk;
5424 }
5425 if (stcb->asoc.auth_supported == 0) {
5426 goto unknown_chunk;
5427 }
5428 if ((chk_length < (sizeof(struct sctp_auth_chunk))) ||
5429 (chk_length > (sizeof(struct sctp_auth_chunk) +
5430 SCTP_AUTH_DIGEST_LEN_MAX))) {
5431 /* Its not ours */
5432 *offset = length;
5433 return (stcb);
5434 }
5435 if (got_auth == 1) {
5436 /* skip this chunk... it's already auth'd */
5437 goto next_chunk;
5438 }
5439 got_auth = 1;
5440 if ((ch == NULL) || sctp_handle_auth(stcb, (struct sctp_auth_chunk *)ch,
5441 m, *offset)) {
5442 /* auth HMAC failed so dump the packet */
5443 *offset = length;
5444 return (stcb);
5445 } else {
5446 /* remaining chunks are HMAC checked */
5447 stcb->asoc.authenticated = 1;
5448 }
5449 break;
5450
5451 default:
5452 unknown_chunk:
5453 /* it's an unknown chunk! */
5454 if ((ch->chunk_type & 0x40) && (stcb != NULL)) {
5455 struct sctp_gen_error_cause *cause;
5456 int len;
5457
5458 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_gen_error_cause),
5459 0, M_NOWAIT, 1, MT_DATA);
5460 if (op_err != NULL) {
5461 len = min(SCTP_SIZE32(chk_length), (uint32_t)(length - *offset));
5462 cause = mtod(op_err, struct sctp_gen_error_cause *);
5463 cause->code = htons(SCTP_CAUSE_UNRECOG_CHUNK);
5464 cause->length = htons((uint16_t)(len + sizeof(struct sctp_gen_error_cause)));
5465 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5466 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(m, *offset, len, M_NOWAIT);
5467 if (SCTP_BUF_NEXT(op_err) != NULL) {
5468 #ifdef SCTP_MBUF_LOGGING
5469 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5470 sctp_log_mbc(SCTP_BUF_NEXT(op_err), SCTP_MBUF_ICOPY);
5471 }
5472 #endif
5473 sctp_queue_op_err(stcb, op_err);
5474 } else {
5475 sctp_m_freem(op_err);
5476 }
5477 }
5478 }
5479 if ((ch->chunk_type & 0x80) == 0) {
5480 /* discard this packet */
5481 *offset = length;
5482 return (stcb);
5483 } /* else skip this bad chunk and continue... */
5484 break;
5485 } /* switch (ch->chunk_type) */
5486
5487
5488 next_chunk:
5489 /* get the next chunk */
5490 *offset += SCTP_SIZE32(chk_length);
5491 if (*offset >= length) {
5492 /* no more data left in the mbuf chain */
5493 break;
5494 }
5495 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, *offset,
5496 sizeof(struct sctp_chunkhdr), chunk_buf);
5497 if (ch == NULL) {
5498 *offset = length;
5499 return (stcb);
5500 }
5501 } /* while */
5502
5503 if ((asconf_cnt > 0) && (stcb != NULL)) {
5504 sctp_send_asconf_ack(stcb);
5505 }
5506 return (stcb);
5507 }
5508
5509
5510 /*
5511 * common input chunk processing (v4 and v6)
5512 */
5513 void
sctp_common_input_processing(struct mbuf ** mm,int iphlen,int offset,int length,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_chunkhdr * ch,uint8_t compute_crc,uint8_t ecn_bits,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)5514 sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int length,
5515 struct sockaddr *src, struct sockaddr *dst,
5516 struct sctphdr *sh, struct sctp_chunkhdr *ch,
5517 uint8_t compute_crc,
5518 uint8_t ecn_bits,
5519 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
5520 uint32_t vrf_id, uint16_t port)
5521 {
5522 uint32_t high_tsn;
5523 int fwd_tsn_seen = 0, data_processed = 0;
5524 struct mbuf *m = *mm, *op_err;
5525 char msg[SCTP_DIAG_INFO_LEN];
5526 int un_sent;
5527 int cnt_ctrl_ready = 0;
5528 struct sctp_inpcb *inp = NULL, *inp_decr = NULL;
5529 struct sctp_tcb *stcb = NULL;
5530 struct sctp_nets *net = NULL;
5531
5532 SCTP_STAT_INCR(sctps_recvdatagrams);
5533 #ifdef SCTP_AUDITING_ENABLED
5534 sctp_audit_log(0xE0, 1);
5535 sctp_auditing(0, inp, stcb, net);
5536 #endif
5537 if (compute_crc != 0) {
5538 uint32_t check, calc_check;
5539
5540 check = sh->checksum;
5541 sh->checksum = 0;
5542 calc_check = sctp_calculate_cksum(m, iphlen);
5543 sh->checksum = check;
5544 if (calc_check != check) {
5545 SCTPDBG(SCTP_DEBUG_INPUT1, "Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
5546 calc_check, check, (void *)m, length, iphlen);
5547 stcb = sctp_findassociation_addr(m, offset, src, dst,
5548 sh, ch, &inp, &net, vrf_id);
5549 #if defined(INET) || defined(INET6)
5550 if ((ch->chunk_type != SCTP_INITIATION) &&
5551 (net != NULL) && (net->port != port)) {
5552 if (net->port == 0) {
5553 /* UDP encapsulation turned on. */
5554 net->mtu -= sizeof(struct udphdr);
5555 if (stcb->asoc.smallest_mtu > net->mtu) {
5556 sctp_pathmtu_adjustment(stcb, net->mtu);
5557 }
5558 } else if (port == 0) {
5559 /* UDP encapsulation turned off. */
5560 net->mtu += sizeof(struct udphdr);
5561 /* XXX Update smallest_mtu */
5562 }
5563 net->port = port;
5564 }
5565 #endif
5566 if (net != NULL) {
5567 net->flowtype = mflowtype;
5568 net->flowid = mflowid;
5569 }
5570 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5571 if ((inp != NULL) && (stcb != NULL)) {
5572 sctp_send_packet_dropped(stcb, net, m, length, iphlen, 1);
5573 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_INPUT_ERROR, SCTP_SO_NOT_LOCKED);
5574 } else if ((inp != NULL) && (stcb == NULL)) {
5575 inp_decr = inp;
5576 }
5577 SCTP_STAT_INCR(sctps_badsum);
5578 SCTP_STAT_INCR_COUNTER32(sctps_checksumerrors);
5579 goto out;
5580 }
5581 }
5582 /* Destination port of 0 is illegal, based on RFC4960. */
5583 if (sh->dest_port == 0) {
5584 SCTP_STAT_INCR(sctps_hdrops);
5585 goto out;
5586 }
5587 stcb = sctp_findassociation_addr(m, offset, src, dst,
5588 sh, ch, &inp, &net, vrf_id);
5589 #if defined(INET) || defined(INET6)
5590 if ((ch->chunk_type != SCTP_INITIATION) &&
5591 (net != NULL) && (net->port != port)) {
5592 if (net->port == 0) {
5593 /* UDP encapsulation turned on. */
5594 net->mtu -= sizeof(struct udphdr);
5595 if (stcb->asoc.smallest_mtu > net->mtu) {
5596 sctp_pathmtu_adjustment(stcb, net->mtu);
5597 }
5598 } else if (port == 0) {
5599 /* UDP encapsulation turned off. */
5600 net->mtu += sizeof(struct udphdr);
5601 /* XXX Update smallest_mtu */
5602 }
5603 net->port = port;
5604 }
5605 #endif
5606 if (net != NULL) {
5607 net->flowtype = mflowtype;
5608 net->flowid = mflowid;
5609 }
5610 if (inp == NULL) {
5611 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5612 SCTP_STAT_INCR(sctps_noport);
5613 if (badport_bandlim(BANDLIM_SCTP_OOTB) < 0) {
5614 goto out;
5615 }
5616 if (ch->chunk_type == SCTP_SHUTDOWN_ACK) {
5617 sctp_send_shutdown_complete2(src, dst, sh,
5618 mflowtype, mflowid, fibnum,
5619 vrf_id, port);
5620 goto out;
5621 }
5622 if (ch->chunk_type == SCTP_SHUTDOWN_COMPLETE) {
5623 goto out;
5624 }
5625 if (ch->chunk_type != SCTP_ABORT_ASSOCIATION) {
5626 if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
5627 ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
5628 (ch->chunk_type != SCTP_INIT))) {
5629 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5630 "Out of the blue");
5631 sctp_send_abort(m, iphlen, src, dst,
5632 sh, 0, op_err,
5633 mflowtype, mflowid, fibnum,
5634 vrf_id, port);
5635 }
5636 }
5637 goto out;
5638 } else if (stcb == NULL) {
5639 inp_decr = inp;
5640 }
5641 SCTPDBG(SCTP_DEBUG_INPUT1, "Ok, Common input processing called, m:%p iphlen:%d offset:%d length:%d stcb:%p\n",
5642 (void *)m, iphlen, offset, length, (void *)stcb);
5643 if (stcb) {
5644 /* always clear this before beginning a packet */
5645 stcb->asoc.authenticated = 0;
5646 stcb->asoc.seen_a_sack_this_pkt = 0;
5647 SCTPDBG(SCTP_DEBUG_INPUT1, "stcb:%p state:%x\n",
5648 (void *)stcb, stcb->asoc.state);
5649
5650 if ((stcb->asoc.state & SCTP_STATE_WAS_ABORTED) ||
5651 (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
5652 /*-
5653 * If we hit here, we had a ref count
5654 * up when the assoc was aborted and the
5655 * timer is clearing out the assoc, we should
5656 * NOT respond to any packet.. its OOTB.
5657 */
5658 SCTP_TCB_UNLOCK(stcb);
5659 stcb = NULL;
5660 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5661 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5662 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5663 msg);
5664 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5665 mflowtype, mflowid, inp->fibnum,
5666 vrf_id, port);
5667 goto out;
5668 }
5669 }
5670 if (IS_SCTP_CONTROL(ch)) {
5671 /* process the control portion of the SCTP packet */
5672 /* sa_ignore NO_NULL_CHK */
5673 stcb = sctp_process_control(m, iphlen, &offset, length,
5674 src, dst, sh, ch,
5675 inp, stcb, &net, &fwd_tsn_seen,
5676 mflowtype, mflowid, fibnum,
5677 vrf_id, port);
5678 if (stcb) {
5679 /*
5680 * This covers us if the cookie-echo was there and
5681 * it changes our INP.
5682 */
5683 inp = stcb->sctp_ep;
5684 #if defined(INET) || defined(INET6)
5685 if ((ch->chunk_type != SCTP_INITIATION) &&
5686 (net != NULL) && (net->port != port)) {
5687 if (net->port == 0) {
5688 /* UDP encapsulation turned on. */
5689 net->mtu -= sizeof(struct udphdr);
5690 if (stcb->asoc.smallest_mtu > net->mtu) {
5691 sctp_pathmtu_adjustment(stcb, net->mtu);
5692 }
5693 } else if (port == 0) {
5694 /* UDP encapsulation turned off. */
5695 net->mtu += sizeof(struct udphdr);
5696 /* XXX Update smallest_mtu */
5697 }
5698 net->port = port;
5699 }
5700 #endif
5701 }
5702 } else {
5703 /*
5704 * no control chunks, so pre-process DATA chunks (these
5705 * checks are taken care of by control processing)
5706 */
5707
5708 /*
5709 * if DATA only packet, and auth is required, then punt...
5710 * can't have authenticated without any AUTH (control)
5711 * chunks
5712 */
5713 if ((stcb != NULL) &&
5714 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks)) {
5715 /* "silently" ignore */
5716 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5717 SCTP_STAT_INCR(sctps_recvauthmissing);
5718 goto out;
5719 }
5720 if (stcb == NULL) {
5721 /* out of the blue DATA chunk */
5722 SCTP_PROBE5(receive, NULL, NULL, m, NULL, sh);
5723 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5724 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5725 msg);
5726 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5727 mflowtype, mflowid, fibnum,
5728 vrf_id, port);
5729 goto out;
5730 }
5731 if (stcb->asoc.my_vtag != ntohl(sh->v_tag)) {
5732 /* v_tag mismatch! */
5733 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5734 SCTP_STAT_INCR(sctps_badvtag);
5735 goto out;
5736 }
5737 }
5738
5739 SCTP_PROBE5(receive, NULL, stcb, m, stcb, sh);
5740 if (stcb == NULL) {
5741 /*
5742 * no valid TCB for this packet, or we found it's a bad
5743 * packet while processing control, or we're done with this
5744 * packet (done or skip rest of data), so we drop it...
5745 */
5746 goto out;
5747 }
5748
5749 /*
5750 * DATA chunk processing
5751 */
5752 /* plow through the data chunks while length > offset */
5753
5754 /*
5755 * Rest should be DATA only. Check authentication state if AUTH for
5756 * DATA is required.
5757 */
5758 if ((length > offset) &&
5759 (stcb != NULL) &&
5760 sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.local_auth_chunks) &&
5761 !stcb->asoc.authenticated) {
5762 /* "silently" ignore */
5763 SCTP_STAT_INCR(sctps_recvauthmissing);
5764 SCTPDBG(SCTP_DEBUG_AUTH1,
5765 "Data chunk requires AUTH, skipped\n");
5766 goto trigger_send;
5767 }
5768 if (length > offset) {
5769 int retval;
5770
5771 /*
5772 * First check to make sure our state is correct. We would
5773 * not get here unless we really did have a tag, so we don't
5774 * abort if this happens, just dump the chunk silently.
5775 */
5776 switch (SCTP_GET_STATE(stcb)) {
5777 case SCTP_STATE_COOKIE_ECHOED:
5778 /*
5779 * we consider data with valid tags in this state
5780 * shows us the cookie-ack was lost. Imply it was
5781 * there.
5782 */
5783 sctp_handle_cookie_ack((struct sctp_cookie_ack_chunk *)ch, stcb, net);
5784 break;
5785 case SCTP_STATE_COOKIE_WAIT:
5786 /*
5787 * We consider OOTB any data sent during asoc setup.
5788 */
5789 snprintf(msg, sizeof(msg), "OOTB, %s:%d at %s", __FILE__, __LINE__, __func__);
5790 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5791 msg);
5792 sctp_handle_ootb(m, iphlen, offset, src, dst, sh, inp, op_err,
5793 mflowtype, mflowid, inp->fibnum,
5794 vrf_id, port);
5795 goto out;
5796 /* sa_ignore NOTREACHED */
5797 break;
5798 case SCTP_STATE_EMPTY: /* should not happen */
5799 case SCTP_STATE_INUSE: /* should not happen */
5800 case SCTP_STATE_SHUTDOWN_RECEIVED: /* This is a peer error */
5801 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5802 default:
5803 goto out;
5804 /* sa_ignore NOTREACHED */
5805 break;
5806 case SCTP_STATE_OPEN:
5807 case SCTP_STATE_SHUTDOWN_SENT:
5808 break;
5809 }
5810 /* plow through the data chunks while length > offset */
5811 retval = sctp_process_data(mm, iphlen, &offset, length,
5812 inp, stcb, net, &high_tsn);
5813 if (retval == 2) {
5814 /*
5815 * The association aborted, NO UNLOCK needed since
5816 * the association is destroyed.
5817 */
5818 stcb = NULL;
5819 goto out;
5820 }
5821 data_processed = 1;
5822 /*
5823 * Anything important needs to have been m_copy'ed in
5824 * process_data
5825 */
5826 }
5827
5828 /* take care of ecn */
5829 if ((data_processed == 1) &&
5830 (stcb->asoc.ecn_supported == 1) &&
5831 ((ecn_bits & SCTP_CE_BITS) == SCTP_CE_BITS)) {
5832 /* Yep, we need to add a ECNE */
5833 sctp_send_ecn_echo(stcb, net, high_tsn);
5834 }
5835
5836 if ((data_processed == 0) && (fwd_tsn_seen)) {
5837 int was_a_gap;
5838 uint32_t highest_tsn;
5839
5840 if (SCTP_TSN_GT(stcb->asoc.highest_tsn_inside_nr_map, stcb->asoc.highest_tsn_inside_map)) {
5841 highest_tsn = stcb->asoc.highest_tsn_inside_nr_map;
5842 } else {
5843 highest_tsn = stcb->asoc.highest_tsn_inside_map;
5844 }
5845 was_a_gap = SCTP_TSN_GT(highest_tsn, stcb->asoc.cumulative_tsn);
5846 stcb->asoc.send_sack = 1;
5847 sctp_sack_check(stcb, was_a_gap);
5848 } else if (fwd_tsn_seen) {
5849 stcb->asoc.send_sack = 1;
5850 }
5851 /* trigger send of any chunks in queue... */
5852 trigger_send:
5853 #ifdef SCTP_AUDITING_ENABLED
5854 sctp_audit_log(0xE0, 2);
5855 sctp_auditing(1, inp, stcb, net);
5856 #endif
5857 SCTPDBG(SCTP_DEBUG_INPUT1,
5858 "Check for chunk output prw:%d tqe:%d tf=%d\n",
5859 stcb->asoc.peers_rwnd,
5860 TAILQ_EMPTY(&stcb->asoc.control_send_queue),
5861 stcb->asoc.total_flight);
5862 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
5863 if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
5864 cnt_ctrl_ready = stcb->asoc.ctrl_queue_cnt - stcb->asoc.ecn_echo_cnt_onq;
5865 }
5866 if (!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue) ||
5867 cnt_ctrl_ready ||
5868 stcb->asoc.trigger_reset ||
5869 ((un_sent) &&
5870 (stcb->asoc.peers_rwnd > 0 ||
5871 (stcb->asoc.peers_rwnd <= 0 && stcb->asoc.total_flight == 0)))) {
5872 SCTPDBG(SCTP_DEBUG_INPUT3, "Calling chunk OUTPUT\n");
5873 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CONTROL_PROC, SCTP_SO_NOT_LOCKED);
5874 SCTPDBG(SCTP_DEBUG_INPUT3, "chunk OUTPUT returns\n");
5875 }
5876 #ifdef SCTP_AUDITING_ENABLED
5877 sctp_audit_log(0xE0, 3);
5878 sctp_auditing(2, inp, stcb, net);
5879 #endif
5880 out:
5881 if (stcb != NULL) {
5882 SCTP_TCB_UNLOCK(stcb);
5883 }
5884 if (inp_decr != NULL) {
5885 /* reduce ref-count */
5886 SCTP_INP_WLOCK(inp_decr);
5887 SCTP_INP_DECR_REF(inp_decr);
5888 SCTP_INP_WUNLOCK(inp_decr);
5889 }
5890 return;
5891 }
5892
5893 #ifdef INET
5894 void
sctp_input_with_port(struct mbuf * i_pak,int off,uint16_t port)5895 sctp_input_with_port(struct mbuf *i_pak, int off, uint16_t port)
5896 {
5897 struct mbuf *m;
5898 int iphlen;
5899 uint32_t vrf_id = 0;
5900 uint8_t ecn_bits;
5901 struct sockaddr_in src, dst;
5902 struct ip *ip;
5903 struct sctphdr *sh;
5904 struct sctp_chunkhdr *ch;
5905 int length, offset;
5906 uint8_t compute_crc;
5907 uint32_t mflowid;
5908 uint8_t mflowtype;
5909 uint16_t fibnum;
5910
5911 iphlen = off;
5912 if (SCTP_GET_PKT_VRFID(i_pak, vrf_id)) {
5913 SCTP_RELEASE_PKT(i_pak);
5914 return;
5915 }
5916 m = SCTP_HEADER_TO_CHAIN(i_pak);
5917 #ifdef SCTP_MBUF_LOGGING
5918 /* Log in any input mbufs */
5919 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5920 sctp_log_mbc(m, SCTP_MBUF_INPUT);
5921 }
5922 #endif
5923 #ifdef SCTP_PACKET_LOGGING
5924 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
5925 sctp_packet_log(m);
5926 }
5927 #endif
5928 SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
5929 "sctp_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
5930 m->m_pkthdr.len,
5931 if_name(m->m_pkthdr.rcvif),
5932 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
5933 mflowid = m->m_pkthdr.flowid;
5934 mflowtype = M_HASHTYPE_GET(m);
5935 fibnum = M_GETFIB(m);
5936 SCTP_STAT_INCR(sctps_recvpackets);
5937 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
5938 /* Get IP, SCTP, and first chunk header together in the first mbuf. */
5939 offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5940 if (SCTP_BUF_LEN(m) < offset) {
5941 if ((m = m_pullup(m, offset)) == NULL) {
5942 SCTP_STAT_INCR(sctps_hdrops);
5943 return;
5944 }
5945 }
5946 ip = mtod(m, struct ip *);
5947 sh = (struct sctphdr *)((caddr_t)ip + iphlen);
5948 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
5949 offset -= sizeof(struct sctp_chunkhdr);
5950 memset(&src, 0, sizeof(struct sockaddr_in));
5951 src.sin_family = AF_INET;
5952 src.sin_len = sizeof(struct sockaddr_in);
5953 src.sin_port = sh->src_port;
5954 src.sin_addr = ip->ip_src;
5955 memset(&dst, 0, sizeof(struct sockaddr_in));
5956 dst.sin_family = AF_INET;
5957 dst.sin_len = sizeof(struct sockaddr_in);
5958 dst.sin_port = sh->dest_port;
5959 dst.sin_addr = ip->ip_dst;
5960 length = ntohs(ip->ip_len);
5961 /* Validate mbuf chain length with IP payload length. */
5962 if (SCTP_HEADER_LEN(m) != length) {
5963 SCTPDBG(SCTP_DEBUG_INPUT1,
5964 "sctp_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
5965 SCTP_STAT_INCR(sctps_hdrops);
5966 goto out;
5967 }
5968 /* SCTP does not allow broadcasts or multicasts */
5969 if (IN_MULTICAST(ntohl(dst.sin_addr.s_addr))) {
5970 goto out;
5971 }
5972 if (SCTP_IS_IT_BROADCAST(dst.sin_addr, m)) {
5973 goto out;
5974 }
5975 ecn_bits = ip->ip_tos;
5976 if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
5977 SCTP_STAT_INCR(sctps_recvhwcrc);
5978 compute_crc = 0;
5979 } else {
5980 SCTP_STAT_INCR(sctps_recvswcrc);
5981 compute_crc = 1;
5982 }
5983 sctp_common_input_processing(&m, iphlen, offset, length,
5984 (struct sockaddr *)&src,
5985 (struct sockaddr *)&dst,
5986 sh, ch,
5987 compute_crc,
5988 ecn_bits,
5989 mflowtype, mflowid, fibnum,
5990 vrf_id, port);
5991 out:
5992 if (m) {
5993 sctp_m_freem(m);
5994 }
5995 return;
5996 }
5997
5998 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5999 extern int *sctp_cpuarry;
6000 #endif
6001
6002 int
sctp_input(struct mbuf ** mp,int * offp,int proto SCTP_UNUSED)6003 sctp_input(struct mbuf **mp, int *offp, int proto SCTP_UNUSED)
6004 {
6005 struct mbuf *m;
6006 int off;
6007
6008 m = *mp;
6009 off = *offp;
6010 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
6011 if (mp_ncpus > 1) {
6012 struct ip *ip;
6013 struct sctphdr *sh;
6014 int offset;
6015 int cpu_to_use;
6016 uint32_t flowid, tag;
6017
6018 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
6019 flowid = m->m_pkthdr.flowid;
6020 } else {
6021 /*
6022 * No flow id built by lower layers fix it so we
6023 * create one.
6024 */
6025 offset = off + sizeof(struct sctphdr);
6026 if (SCTP_BUF_LEN(m) < offset) {
6027 if ((m = m_pullup(m, offset)) == NULL) {
6028 SCTP_STAT_INCR(sctps_hdrops);
6029 return (IPPROTO_DONE);
6030 }
6031 }
6032 ip = mtod(m, struct ip *);
6033 sh = (struct sctphdr *)((caddr_t)ip + off);
6034 tag = htonl(sh->v_tag);
6035 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6036 m->m_pkthdr.flowid = flowid;
6037 M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE_HASH);
6038 }
6039 cpu_to_use = sctp_cpuarry[flowid % mp_ncpus];
6040 sctp_queue_to_mcore(m, off, cpu_to_use);
6041 return (IPPROTO_DONE);
6042 }
6043 #endif
6044 sctp_input_with_port(m, off, 0);
6045 return (IPPROTO_DONE);
6046 }
6047 #endif
6048