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