1 /* $KAME: sctp_pcb.c,v 1.39 2005/06/16 18:29:25 jinmei Exp $ */
2 /* $NetBSD: sctp_pcb.c,v 1.27 2024/07/05 04:31:54 rin Exp $ */
3
4 /*
5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Cisco Systems, Inc.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sctp_pcb.c,v 1.27 2024/07/05 04:31:54 rin Exp $");
37
38 #ifdef _KERNEL_OPT
39 #include "opt_inet.h"
40 #include "opt_ipsec.h"
41 #include "opt_sctp.h"
42 #endif /* _KERNEL_OPT */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/proc.h>
53 #include <sys/kauth.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/callout.h>
57
58 #include <machine/limits.h>
59 #include <machine/cpu.h>
60
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70
71 #ifdef INET6
72 #include <netinet/ip6.h>
73 #include <netinet6/ip6_var.h>
74 #include <netinet6/scope6_var.h>
75 #include <netinet6/in6_pcb.h>
76 #endif /* INET6 */
77
78 #ifdef IPSEC
79 #include <netipsec/ipsec.h>
80 #include <netipsec/key.h>
81 #endif /* IPSEC */
82
83 #include <netinet/sctp_var.h>
84 #include <netinet/sctp_pcb.h>
85 #include <netinet/sctputil.h>
86 #include <netinet/sctp.h>
87 #include <netinet/sctp_header.h>
88 #include <netinet/sctp_asconf.h>
89 #include <netinet/sctp_output.h>
90 #include <netinet/sctp_timer.h>
91
92 #ifndef SCTP_PCBHASHSIZE
93 /* default number of association hash buckets in each endpoint */
94 #define SCTP_PCBHASHSIZE 256
95 #endif
96
97 #ifdef SCTP_DEBUG
98 u_int32_t sctp_debug_on = SCTP_DEBUG_ALL;
99 #endif /* SCTP_DEBUG */
100
101 u_int32_t sctp_pegs[SCTP_NUMBER_OF_PEGS];
102
103 int sctp_pcbtblsize = SCTP_PCBHASHSIZE;
104
105 struct sctp_epinfo sctppcbinfo;
106
107 /* FIX: we don't handle multiple link local scopes */
108 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
109 int
SCTP6_ARE_ADDR_EQUAL(const struct in6_addr * a,const struct in6_addr * b)110 SCTP6_ARE_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b)
111 {
112 struct in6_addr tmp_a, tmp_b;
113 /* use a copy of a and b */
114 tmp_a = *a;
115 tmp_b = *b;
116 in6_clearscope(&tmp_a);
117 in6_clearscope(&tmp_b);
118 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
119 }
120
121 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
122
123 #ifndef xyzzy
124 void sctp_validate_no_locks(void);
125
126 void
SCTP_INP_RLOCK(struct sctp_inpcb * inp)127 SCTP_INP_RLOCK(struct sctp_inpcb *inp)
128 {
129 struct sctp_tcb *stcb;
130 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
131 if (mtx_owned(&(stcb)->tcb_mtx))
132 panic("I own TCB lock?");
133 }
134 if (mtx_owned(&(inp)->inp_mtx))
135 panic("INP Recursive Lock-R");
136 mtx_lock(&(inp)->inp_mtx);
137 }
138
139 void
SCTP_INP_WLOCK(struct sctp_inpcb * inp)140 SCTP_INP_WLOCK(struct sctp_inpcb *inp)
141 {
142 SCTP_INP_RLOCK(inp);
143 }
144
145 void
SCTP_INP_INFO_RLOCK()146 SCTP_INP_INFO_RLOCK()
147 {
148 struct sctp_inpcb *inp;
149 struct sctp_tcb *stcb;
150 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
151 if (mtx_owned(&(inp)->inp_mtx))
152 panic("info-lock and own inp lock?");
153 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
154 if (mtx_owned(&(stcb)->tcb_mtx))
155 panic("Info lock and own a tcb lock?");
156 }
157 }
158 if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
159 panic("INP INFO Recursive Lock-R");
160 mtx_lock(&sctppcbinfo.ipi_ep_mtx);
161 }
162
163 void
SCTP_INP_INFO_WLOCK()164 SCTP_INP_INFO_WLOCK()
165 {
166 SCTP_INP_INFO_RLOCK();
167 }
168
169
sctp_validate_no_locks()170 void sctp_validate_no_locks()
171 {
172 struct sctp_inpcb *inp;
173 struct sctp_tcb *stcb;
174
175 if (mtx_owned(&sctppcbinfo.ipi_ep_mtx))
176 panic("INP INFO lock is owned?");
177
178 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
179 if (mtx_owned(&(inp)->inp_mtx))
180 panic("You own an INP lock?");
181 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
182 if (mtx_owned(&(stcb)->tcb_mtx))
183 panic("You own a TCB lock?");
184 }
185 }
186 }
187
188 #endif
189 #endif
190
191 void
sctp_fill_pcbinfo(struct sctp_pcbinfo * spcb)192 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
193 {
194 /* We really don't need
195 * to lock this, but I will
196 * just because it does not hurt.
197 */
198 SCTP_INP_INFO_RLOCK();
199 spcb->ep_count = sctppcbinfo.ipi_count_ep;
200 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
201 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
202 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
203 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
204 spcb->sockq_count = sctppcbinfo.ipi_count_sockq;
205 spcb->mbuf_track = sctppcbinfo.mbuf_track;
206 SCTP_INP_INFO_RUNLOCK();
207 }
208
209
210 /*
211 * Notes on locks for FreeBSD 5 and up. All association
212 * lookups that have a definte ep, the INP structure is
213 * assumed to be locked for reading. If we need to go
214 * find the INP (usually when a **inp is passed) then
215 * we must lock the INFO structure first and if needed
216 * lock the INP too. Note that if we lock it we must
217 *
218 */
219
220
221 /*
222 * Given a endpoint, look and find in its association list any association
223 * with the "to" address given. This can be a "from" address, too, for
224 * inbound packets. For outbound packets it is a true "to" address.
225 */
226 static struct sctp_tcb *
sctp_tcb_special_locate(struct sctp_inpcb ** inp_p,struct sockaddr * from,struct sockaddr * to,struct sctp_nets ** netp)227 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
228 struct sockaddr *to, struct sctp_nets **netp)
229 {
230 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
231
232 /*
233 * Note for this module care must be taken when observing what to is
234 * for. In most of the rest of the code the TO field represents my
235 * peer and the FROM field represents my address. For this module it
236 * is reversed of that.
237 */
238 /*
239 * If we support the TCP model, then we must now dig through to
240 * see if we can find our endpoint in the list of tcp ep's.
241 */
242 uint16_t lport, rport;
243 struct sctppcbhead *ephead;
244 struct sctp_inpcb *inp;
245 struct sctp_laddr *laddr;
246 struct sctp_tcb *stcb;
247 struct sctp_nets *net;
248
249 if ((to == NULL) || (from == NULL)) {
250 return (NULL);
251 }
252
253 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
254 lport = ((struct sockaddr_in *)to)->sin_port;
255 rport = ((struct sockaddr_in *)from)->sin_port;
256 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
257 lport = ((struct sockaddr_in6 *)to)->sin6_port;
258 rport = ((struct sockaddr_in6 *)from)->sin6_port;
259 } else {
260 return NULL;
261 }
262 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
263 (lport + rport), sctppcbinfo.hashtcpmark)];
264 /*
265 * Ok now for each of the guys in this bucket we must look
266 * and see:
267 * - Does the remote port match.
268 * - Does there single association's addresses match this
269 * address (to).
270 * If so we update p_ep to point to this ep and return the
271 * tcb from it.
272 */
273 LIST_FOREACH(inp, ephead, sctp_hash) {
274 if (lport != inp->sctp_lport) {
275 continue;
276 }
277 SCTP_INP_RLOCK(inp);
278 /* check to see if the ep has one of the addresses */
279 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
280 /* We are NOT bound all, so look further */
281 int match = 0;
282
283 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
284 if (laddr->ifa == NULL) {
285 #ifdef SCTP_DEBUG
286 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
287 printf("An ounce of prevention is worth a pound of cure\n");
288 }
289 #endif
290 continue;
291 }
292 if (laddr->ifa->ifa_addr == NULL) {
293 #ifdef SCTP_DEBUG
294 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
295 printf("ifa with a NULL address\n");
296 }
297 #endif
298 continue;
299 }
300 if (laddr->ifa->ifa_addr->sa_family ==
301 to->sa_family) {
302 /* see if it matches */
303 struct sockaddr_in *intf_addr, *sin;
304 intf_addr = (struct sockaddr_in *)
305 laddr->ifa->ifa_addr;
306 sin = (struct sockaddr_in *)to;
307 if (from->sa_family == AF_INET) {
308 if (sin->sin_addr.s_addr ==
309 intf_addr->sin_addr.s_addr) {
310 match = 1;
311 SCTP_INP_RUNLOCK(inp);
312 break;
313 }
314 } else {
315 struct sockaddr_in6 *intf_addr6;
316 struct sockaddr_in6 *sin6;
317 sin6 = (struct sockaddr_in6 *)
318 to;
319 intf_addr6 = (struct sockaddr_in6 *)
320 laddr->ifa->ifa_addr;
321
322 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
323 &intf_addr6->sin6_addr)) {
324 match = 1;
325 SCTP_INP_RUNLOCK(inp);
326 break;
327 }
328 }
329 }
330 }
331 if (match == 0) {
332 /* This endpoint does not have this address */
333 SCTP_INP_RUNLOCK(inp);
334 continue;
335 }
336 }
337 /*
338 * Ok if we hit here the ep has the address, does it hold the
339 * tcb?
340 */
341
342 stcb = LIST_FIRST(&inp->sctp_asoc_list);
343 if (stcb == NULL) {
344 SCTP_INP_RUNLOCK(inp);
345 continue;
346 }
347 SCTP_TCB_LOCK(stcb);
348 if (stcb->rport != rport) {
349 /* remote port does not match. */
350 SCTP_TCB_UNLOCK(stcb);
351 SCTP_INP_RUNLOCK(inp);
352 continue;
353 }
354 /* Does this TCB have a matching address? */
355 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
356 if (sctp_cmpaddr(from, rtcache_getdst(&net->ro))) {
357 /* found it */
358 if (netp != NULL) {
359 *netp = net;
360 }
361 /* Update the endpoint pointer */
362 *inp_p = inp;
363 SCTP_INP_RUNLOCK(inp);
364 return (stcb);
365 }
366 }
367 SCTP_TCB_UNLOCK(stcb);
368
369 SCTP_INP_RUNLOCK(inp);
370 }
371 return (NULL);
372 }
373
374 struct sctp_tcb *
sctp_findassociation_ep_asconf(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp)375 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
376 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
377 {
378 struct sctp_tcb *stcb;
379 struct sockaddr_in *sin;
380 struct sockaddr_in6 *sin6;
381 struct sockaddr_storage local_store, remote_store;
382 struct ip *iph;
383 struct sctp_paramhdr parm_buf, *phdr;
384 int ptype;
385
386 memset(&local_store, 0, sizeof(local_store));
387 memset(&remote_store, 0, sizeof(remote_store));
388
389 /* First get the destination address setup too. */
390 iph = mtod(m, struct ip *);
391 if (iph->ip_v == IPVERSION) {
392 /* its IPv4 */
393 sin = (struct sockaddr_in *)&local_store;
394 sin->sin_family = AF_INET;
395 sin->sin_len = sizeof(*sin);
396 sin->sin_port = sh->dest_port;
397 sin->sin_addr.s_addr = iph->ip_dst.s_addr ;
398 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
399 /* its IPv6 */
400 struct ip6_hdr *ip6;
401 ip6 = mtod(m, struct ip6_hdr *);
402 sin6 = (struct sockaddr_in6 *)&local_store;
403 sin6->sin6_family = AF_INET6;
404 sin6->sin6_len = sizeof(*sin6);
405 sin6->sin6_port = sh->dest_port;
406 sin6->sin6_addr = ip6->ip6_dst;
407 } else {
408 return NULL;
409 }
410
411 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
412 &parm_buf, sizeof(struct sctp_paramhdr));
413 if (phdr == NULL) {
414 #ifdef SCTP_DEBUG
415 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
416 printf("sctp_process_control: failed to get asconf lookup addr\n");
417 }
418 #endif /* SCTP_DEBUG */
419 return NULL;
420 }
421 ptype = (int)((u_int)ntohs(phdr->param_type));
422 /* get the correlation address */
423 if (ptype == SCTP_IPV6_ADDRESS) {
424 /* ipv6 address param */
425 struct sctp_ipv6addr_param *p6, p6_buf;
426 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
427 return NULL;
428 }
429
430 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
431 offset + sizeof(struct sctp_asconf_chunk),
432 &p6_buf.ph, sizeof(*p6));
433 if (p6 == NULL) {
434 #ifdef SCTP_DEBUG
435 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
436 printf("sctp_process_control: failed to get asconf v6 lookup addr\n");
437 }
438 #endif /* SCTP_DEBUG */
439 return (NULL);
440 }
441 sin6 = (struct sockaddr_in6 *)&remote_store;
442 sin6->sin6_family = AF_INET6;
443 sin6->sin6_len = sizeof(*sin6);
444 sin6->sin6_port = sh->src_port;
445 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
446 } else if (ptype == SCTP_IPV4_ADDRESS) {
447 /* ipv4 address param */
448 struct sctp_ipv4addr_param *p4, p4_buf;
449 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
450 return NULL;
451 }
452
453 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
454 offset + sizeof(struct sctp_asconf_chunk),
455 &p4_buf.ph, sizeof(*p4));
456 if (p4 == NULL) {
457 #ifdef SCTP_DEBUG
458 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
459 printf("sctp_process_control: failed to get asconf v4 lookup addr\n");
460 }
461 #endif /* SCTP_DEBUG */
462 return (NULL);
463 }
464 sin = (struct sockaddr_in *)&remote_store;
465 sin->sin_family = AF_INET;
466 sin->sin_len = sizeof(*sin);
467 sin->sin_port = sh->src_port;
468 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
469 } else {
470 /* invalid address param type */
471 return NULL;
472 }
473
474 stcb = sctp_findassociation_ep_addr(inp_p,
475 (struct sockaddr *)&remote_store, netp,
476 (struct sockaddr *)&local_store, NULL);
477 return (stcb);
478 }
479
480 struct sctp_tcb *
sctp_findassociation_ep_addr(struct sctp_inpcb ** inp_p,struct sockaddr * remote,struct sctp_nets ** netp,struct sockaddr * local,struct sctp_tcb * locked_tcb)481 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
482 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
483 {
484 struct sctpasochead *head;
485 struct sctp_inpcb *inp;
486 struct sctp_tcb *stcb;
487 struct sctp_nets *net;
488 uint16_t rport;
489
490 inp = *inp_p;
491 if (remote->sa_family == AF_INET) {
492 rport = (((struct sockaddr_in *)remote)->sin_port);
493 } else if (remote->sa_family == AF_INET6) {
494 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
495 } else {
496 return (NULL);
497 }
498 if (locked_tcb) {
499 /* UN-lock so we can do proper locking here
500 * this occurs when called from load_addresses_from_init.
501 */
502 SCTP_TCB_UNLOCK(locked_tcb);
503 }
504 SCTP_INP_INFO_RLOCK();
505 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
506 /*
507 * Now either this guy is our listner or it's the connector.
508 * If it is the one that issued the connect, then it's only
509 * chance is to be the first TCB in the list. If it is the
510 * acceptor, then do the special_lookup to hash and find the
511 * real inp.
512 */
513 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
514 /* to is peer addr, from is my addr */
515 stcb = sctp_tcb_special_locate(inp_p, remote, local,
516 netp);
517 if ((stcb != NULL) && (locked_tcb == NULL)){
518 /* we have a locked tcb, lower refcount */
519 SCTP_INP_WLOCK(inp);
520 SCTP_INP_DECR_REF(inp);
521 SCTP_INP_WUNLOCK(inp);
522 }
523 if (locked_tcb != NULL) {
524 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
525 SCTP_TCB_LOCK(locked_tcb);
526 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
527 if (stcb != NULL) {
528 SCTP_TCB_UNLOCK(stcb);
529 }
530 }
531 SCTP_INP_INFO_RUNLOCK();
532 return (stcb);
533 } else {
534 SCTP_INP_WLOCK(inp);
535 stcb = LIST_FIRST(&inp->sctp_asoc_list);
536 if (stcb == NULL) {
537 goto null_return;
538 }
539 SCTP_TCB_LOCK(stcb);
540 if (stcb->rport != rport) {
541 /* remote port does not match. */
542 SCTP_TCB_UNLOCK(stcb);
543 goto null_return;
544 }
545 /* now look at the list of remote addresses */
546 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
547 if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
548 /* found it */
549 if (netp != NULL) {
550 *netp = net;
551 }
552 if (locked_tcb == NULL) {
553 SCTP_INP_DECR_REF(inp);
554 }
555 SCTP_INP_WUNLOCK(inp);
556 SCTP_INP_INFO_RUNLOCK();
557 return (stcb);
558 }
559 }
560 SCTP_TCB_UNLOCK(stcb);
561 }
562 } else {
563 SCTP_INP_WLOCK(inp);
564 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
565 inp->sctp_hashmark)];
566 if (head == NULL) {
567 goto null_return;
568 }
569 LIST_FOREACH(stcb, head, sctp_tcbhash) {
570 if (stcb->rport != rport) {
571 /* remote port does not match */
572 continue;
573 }
574 /* now look at the list of remote addresses */
575 SCTP_TCB_LOCK(stcb);
576 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
577 if (sctp_cmpaddr(remote, rtcache_getdst(&net->ro))) {
578 /* found it */
579 if (netp != NULL) {
580 *netp = net;
581 }
582 if (locked_tcb == NULL) {
583 SCTP_INP_DECR_REF(inp);
584 }
585 SCTP_INP_WUNLOCK(inp);
586 SCTP_INP_INFO_RUNLOCK();
587 return (stcb);
588 }
589 }
590 SCTP_TCB_UNLOCK(stcb);
591 }
592 }
593 null_return:
594 /* clean up for returning null */
595 if (locked_tcb){
596 if (locked_tcb->sctp_ep != inp) {
597 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
598 SCTP_TCB_LOCK(locked_tcb);
599 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
600 } else {
601 SCTP_TCB_LOCK(locked_tcb);
602 }
603 }
604 SCTP_INP_WUNLOCK(inp);
605 SCTP_INP_INFO_RUNLOCK();
606 /* not found */
607 return (NULL);
608 }
609
610 /*
611 * Find an association for a specific endpoint using the association id
612 * given out in the COMM_UP notification
613 */
614 struct sctp_tcb *
sctp_findassociation_ep_asocid(struct sctp_inpcb * inp,vaddr_t asoc_id)615 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, vaddr_t asoc_id)
616 {
617 /*
618 * Use my the assoc_id to find a endpoint
619 */
620 struct sctpasochead *head;
621 struct sctp_tcb *stcb;
622 u_int32_t vtag;
623
624 if (asoc_id == 0 || inp == NULL) {
625 return (NULL);
626 }
627 SCTP_INP_INFO_RLOCK();
628 vtag = (u_int32_t)asoc_id;
629 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
630 sctppcbinfo.hashasocmark)];
631 if (head == NULL) {
632 /* invalid vtag */
633 SCTP_INP_INFO_RUNLOCK();
634 return (NULL);
635 }
636 LIST_FOREACH(stcb, head, sctp_asocs) {
637 SCTP_INP_RLOCK(stcb->sctp_ep);
638 SCTP_TCB_LOCK(stcb);
639 SCTP_INP_RUNLOCK(stcb->sctp_ep);
640 if (stcb->asoc.my_vtag == vtag) {
641 /* candidate */
642 if (inp != stcb->sctp_ep) {
643 /* some other guy has the
644 * same vtag active (vtag collision).
645 */
646 sctp_pegs[SCTP_VTAG_BOGUS]++;
647 SCTP_TCB_UNLOCK(stcb);
648 continue;
649 }
650 sctp_pegs[SCTP_VTAG_EXPR]++;
651 SCTP_INP_INFO_RUNLOCK();
652 return (stcb);
653 }
654 SCTP_TCB_UNLOCK(stcb);
655 }
656 SCTP_INP_INFO_RUNLOCK();
657 return (NULL);
658 }
659
660 static struct sctp_inpcb *
sctp_endpoint_probe(struct sockaddr * nam,struct sctppcbhead * head,uint16_t lport)661 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
662 uint16_t lport)
663 {
664 struct sctp_inpcb *inp;
665 struct sockaddr_in *sin;
666 struct sockaddr_in6 *sin6;
667 struct sctp_laddr *laddr;
668
669 /* Endpoing probe expects
670 * that the INP_INFO is locked.
671 */
672 if (nam->sa_family == AF_INET) {
673 sin = (struct sockaddr_in *)nam;
674 sin6 = NULL;
675 } else if (nam->sa_family == AF_INET6) {
676 sin6 = (struct sockaddr_in6 *)nam;
677 sin = NULL;
678 } else {
679 /* unsupported family */
680 return (NULL);
681 }
682 if (head == NULL)
683 return (NULL);
684
685 LIST_FOREACH(inp, head, sctp_hash) {
686 SCTP_INP_RLOCK(inp);
687
688 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
689 (inp->sctp_lport == lport)) {
690 /* got it */
691 if ((nam->sa_family == AF_INET) &&
692 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
693 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
694 ) {
695 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
696 SCTP_INP_RUNLOCK(inp);
697 continue;
698 }
699 /* A V6 address and the endpoint is NOT bound V6 */
700 if (nam->sa_family == AF_INET6 &&
701 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
702 SCTP_INP_RUNLOCK(inp);
703 continue;
704 }
705 SCTP_INP_RUNLOCK(inp);
706 return (inp);
707 }
708 SCTP_INP_RUNLOCK(inp);
709 }
710
711 if ((nam->sa_family == AF_INET) &&
712 (sin->sin_addr.s_addr == INADDR_ANY)) {
713 /* Can't hunt for one that has no address specified */
714 return (NULL);
715 } else if ((nam->sa_family == AF_INET6) &&
716 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
717 /* Can't hunt for one that has no address specified */
718 return (NULL);
719 }
720 /*
721 * ok, not bound to all so see if we can find a EP bound to this
722 * address.
723 */
724 #ifdef SCTP_DEBUG
725 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
726 printf("Ok, there is NO bound-all available for port:%x\n", ntohs(lport));
727 }
728 #endif
729 LIST_FOREACH(inp, head, sctp_hash) {
730 SCTP_INP_RLOCK(inp);
731 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
732 SCTP_INP_RUNLOCK(inp);
733 continue;
734 }
735 /*
736 * Ok this could be a likely candidate, look at all of
737 * its addresses
738 */
739 if (inp->sctp_lport != lport) {
740 SCTP_INP_RUNLOCK(inp);
741 continue;
742 }
743 #ifdef SCTP_DEBUG
744 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
745 printf("Ok, found matching local port\n");
746 }
747 #endif
748 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
749 if (laddr->ifa == NULL) {
750 #ifdef SCTP_DEBUG
751 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
752 printf("An ounce of prevention is worth a pound of cure\n");
753 }
754 #endif
755 continue;
756 }
757 #ifdef SCTP_DEBUG
758 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
759 printf("Ok laddr->ifa:%p is possible, ",
760 laddr->ifa);
761 }
762 #endif
763 if (laddr->ifa->ifa_addr == NULL) {
764 #ifdef SCTP_DEBUG
765 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
766 printf("Huh IFA as an ifa_addr=NULL, ");
767 }
768 #endif
769 continue;
770 }
771 #ifdef SCTP_DEBUG
772 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
773 printf("Ok laddr->ifa:%p is possible, ",
774 laddr->ifa->ifa_addr);
775 sctp_print_address(laddr->ifa->ifa_addr);
776 printf("looking for ");
777 sctp_print_address(nam);
778 }
779 #endif
780 if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
781 /* possible, see if it matches */
782 struct sockaddr_in *intf_addr;
783 intf_addr = (struct sockaddr_in *)
784 laddr->ifa->ifa_addr;
785 if (nam->sa_family == AF_INET) {
786 if (sin->sin_addr.s_addr ==
787 intf_addr->sin_addr.s_addr) {
788 #ifdef SCTP_DEBUG
789 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
790 printf("YES, return ep:%p\n", inp);
791 }
792 #endif
793 SCTP_INP_RUNLOCK(inp);
794 return (inp);
795 }
796 } else if (nam->sa_family == AF_INET6) {
797 struct sockaddr_in6 *intf_addr6;
798 intf_addr6 = (struct sockaddr_in6 *)
799 laddr->ifa->ifa_addr;
800 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
801 &intf_addr6->sin6_addr)) {
802 #ifdef SCTP_DEBUG
803 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
804 printf("YES, return ep:%p\n", inp);
805 }
806 #endif
807 SCTP_INP_RUNLOCK(inp);
808 return (inp);
809 }
810 }
811 }
812 SCTP_INP_RUNLOCK(inp);
813 }
814 }
815 #ifdef SCTP_DEBUG
816 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
817 printf("NO, Falls out to NULL\n");
818 }
819 #endif
820 return (NULL);
821 }
822
823
824 struct sctp_inpcb *
sctp_pcb_findep(struct sockaddr * nam,int find_tcp_pool,int have_lock)825 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
826 {
827 /*
828 * First we check the hash table to see if someone has this port
829 * bound with just the port.
830 */
831 struct sctp_inpcb *inp;
832 struct sctppcbhead *head;
833 int lport;
834 #ifdef SCTP_DEBUG
835 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
836 printf("Looking for endpoint %d :",
837 ntohs(((struct sockaddr_in *)nam)->sin_port));
838 sctp_print_address(nam);
839 }
840 #endif
841 if (nam->sa_family == AF_INET) {
842 lport = ((struct sockaddr_in *)nam)->sin_port;
843 } else if (nam->sa_family == AF_INET6) {
844 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
845 } else {
846 /* unsupported family */
847 return (NULL);
848 }
849 /*
850 * I could cheat here and just cast to one of the types but we will
851 * do it right. It also provides the check against an Unsupported
852 * type too.
853 */
854 /* Find the head of the ALLADDR chain */
855 if (have_lock == 0) {
856 SCTP_INP_INFO_RLOCK();
857 }
858 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
859 sctppcbinfo.hashmark)];
860 #ifdef SCTP_DEBUG
861 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
862 printf("Main hash to lookup at head:%p\n", head);
863 }
864 #endif
865 inp = sctp_endpoint_probe(nam, head, lport);
866
867 /*
868 * If the TCP model exists it could be that the main listening
869 * endpoint is gone but there exists a connected socket for this
870 * guy yet. If so we can return the first one that we find. This
871 * may NOT be the correct one but the sctp_findassociation_ep_addr
872 * has further code to look at all TCP models.
873 */
874 if (inp == NULL && find_tcp_pool) {
875 unsigned int i;
876 #ifdef SCTP_DEBUG
877 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
878 printf("EP was NULL and TCP model is supported\n");
879 }
880 #endif
881 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
882 /*
883 * This is real gross, but we do NOT have a remote
884 * port at this point depending on who is calling. We
885 * must therefore look for ANY one that matches our
886 * local port :/
887 */
888 head = &sctppcbinfo.sctp_tcpephash[i];
889 if (LIST_FIRST(head)) {
890 inp = sctp_endpoint_probe(nam, head, lport);
891 if (inp) {
892 /* Found one */
893 break;
894 }
895 }
896 }
897 }
898 #ifdef SCTP_DEBUG
899 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
900 printf("EP to return is %p\n", inp);
901 }
902 #endif
903 if (have_lock == 0) {
904 if (inp) {
905 SCTP_INP_WLOCK(inp);
906 SCTP_INP_INCR_REF(inp);
907 SCTP_INP_WUNLOCK(inp);
908 }
909 SCTP_INP_INFO_RUNLOCK();
910 } else {
911 if (inp) {
912 SCTP_INP_WLOCK(inp);
913 SCTP_INP_INCR_REF(inp);
914 SCTP_INP_WUNLOCK(inp);
915 }
916 }
917 return (inp);
918 }
919
920 /*
921 * Find an association for an endpoint with the pointer to whom you want
922 * to send to and the endpoint pointer. The address can be IPv4 or IPv6.
923 * We may need to change the *to to some other struct like a mbuf...
924 */
925 struct sctp_tcb *
sctp_findassociation_addr_sa(struct sockaddr * to,struct sockaddr * from,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,int find_tcp_pool)926 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
927 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
928 {
929 struct sctp_inpcb *inp;
930 struct sctp_tcb *retval;
931
932 SCTP_INP_INFO_RLOCK();
933 if (find_tcp_pool) {
934 if (inp_p != NULL) {
935 retval = sctp_tcb_special_locate(inp_p, from, to, netp);
936 } else {
937 retval = sctp_tcb_special_locate(&inp, from, to, netp);
938 }
939 if (retval != NULL) {
940 SCTP_INP_INFO_RUNLOCK();
941 return (retval);
942 }
943 }
944 inp = sctp_pcb_findep(to, 0, 1);
945 if (inp_p != NULL) {
946 *inp_p = inp;
947 }
948 SCTP_INP_INFO_RUNLOCK();
949
950 if (inp == NULL) {
951 return (NULL);
952 }
953
954 /*
955 * ok, we have an endpoint, now lets find the assoc for it (if any)
956 * we now place the source address or from in the to of the find
957 * endpoint call. Since in reality this chain is used from the
958 * inbound packet side.
959 */
960 if (inp_p != NULL) {
961 return (sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL));
962 } else {
963 return (sctp_findassociation_ep_addr(&inp, from, netp, to, NULL));
964 }
965 }
966
967
968 /*
969 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
970 * find all addresses that the sender has specified in any address list.
971 * Each address will be used to lookup the TCB and see if one exits.
972 */
973 static struct sctp_tcb *
sctp_findassociation_special_addr(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,struct sockaddr * dest)974 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
975 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
976 struct sockaddr *dest)
977 {
978 struct sockaddr_in sin4;
979 struct sockaddr_in6 sin6;
980 struct sctp_paramhdr *phdr, parm_buf;
981 struct sctp_tcb *retval;
982 u_int32_t ptype, plen;
983
984 memset(&sin4, 0, sizeof(sin4));
985 memset(&sin6, 0, sizeof(sin6));
986 sin4.sin_len = sizeof(sin4);
987 sin4.sin_family = AF_INET;
988 sin4.sin_port = sh->src_port;
989 sin6.sin6_len = sizeof(sin6);
990 sin6.sin6_family = AF_INET6;
991 sin6.sin6_port = sh->src_port;
992
993 retval = NULL;
994 offset += sizeof(struct sctp_init_chunk);
995
996 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
997 while (phdr != NULL) {
998 /* now we must see if we want the parameter */
999 ptype = ntohs(phdr->param_type);
1000 plen = ntohs(phdr->param_length);
1001 if (plen == 0) {
1002 #ifdef SCTP_DEBUG
1003 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1004 printf("sctp_findassociation_special_addr: Impossible length in parameter\n");
1005 }
1006 #endif /* SCTP_DEBUG */
1007 break;
1008 }
1009 if (ptype == SCTP_IPV4_ADDRESS &&
1010 plen == sizeof(struct sctp_ipv4addr_param)) {
1011 /* Get the rest of the address */
1012 struct sctp_ipv4addr_param ip4_parm, *p4;
1013
1014 phdr = sctp_get_next_param(m, offset,
1015 (struct sctp_paramhdr *)&ip4_parm, plen);
1016 if (phdr == NULL) {
1017 return (NULL);
1018 }
1019 p4 = (struct sctp_ipv4addr_param *)phdr;
1020 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1021 /* look it up */
1022 retval = sctp_findassociation_ep_addr(inp_p,
1023 (struct sockaddr *)&sin4, netp, dest, NULL);
1024 if (retval != NULL) {
1025 return (retval);
1026 }
1027 } else if (ptype == SCTP_IPV6_ADDRESS &&
1028 plen == sizeof(struct sctp_ipv6addr_param)) {
1029 /* Get the rest of the address */
1030 struct sctp_ipv6addr_param ip6_parm, *p6;
1031
1032 phdr = sctp_get_next_param(m, offset,
1033 (struct sctp_paramhdr *)&ip6_parm, plen);
1034 if (phdr == NULL) {
1035 return (NULL);
1036 }
1037 p6 = (struct sctp_ipv6addr_param *)phdr;
1038 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1039 /* look it up */
1040 retval = sctp_findassociation_ep_addr(inp_p,
1041 (struct sockaddr *)&sin6, netp, dest, NULL);
1042 if (retval != NULL) {
1043 return (retval);
1044 }
1045 }
1046 offset += SCTP_SIZE32(plen);
1047 phdr = sctp_get_next_param(m, offset, &parm_buf,
1048 sizeof(parm_buf));
1049 }
1050 return (NULL);
1051 }
1052
1053 static struct sctp_tcb *
sctp_findassoc_by_vtag(struct sockaddr * from,uint32_t vtag,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp,uint16_t rport,uint16_t lport)1054 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1055 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1056 uint16_t lport)
1057 {
1058 /*
1059 * Use my vtag to hash. If we find it we then verify the source addr
1060 * is in the assoc. If all goes well we save a bit on rec of a packet.
1061 */
1062 struct sctpasochead *head;
1063 struct sctp_nets *net;
1064 struct sctp_tcb *stcb;
1065
1066 SCTP_INP_INFO_RLOCK();
1067 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1068 sctppcbinfo.hashasocmark)];
1069 if (head == NULL) {
1070 /* invalid vtag */
1071 SCTP_INP_INFO_RUNLOCK();
1072 return (NULL);
1073 }
1074 LIST_FOREACH(stcb, head, sctp_asocs) {
1075 SCTP_INP_RLOCK(stcb->sctp_ep);
1076 SCTP_TCB_LOCK(stcb);
1077 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1078 if (stcb->asoc.my_vtag == vtag) {
1079 /* candidate */
1080 if (stcb->rport != rport) {
1081 /*
1082 * we could remove this if vtags are unique
1083 * across the system.
1084 */
1085 SCTP_TCB_UNLOCK(stcb);
1086 continue;
1087 }
1088 if (stcb->sctp_ep->sctp_lport != lport) {
1089 /*
1090 * we could remove this if vtags are unique
1091 * across the system.
1092 */
1093 SCTP_TCB_UNLOCK(stcb);
1094 continue;
1095 }
1096 net = sctp_findnet(stcb, from);
1097 if (net) {
1098 /* yep its him. */
1099 *netp = net;
1100 sctp_pegs[SCTP_VTAG_EXPR]++;
1101 *inp_p = stcb->sctp_ep;
1102 SCTP_INP_INFO_RUNLOCK();
1103 return (stcb);
1104 } else {
1105 /* not him, this should only
1106 * happen in rare cases so
1107 * I peg it.
1108 */
1109 sctp_pegs[SCTP_VTAG_BOGUS]++;
1110 }
1111 }
1112 SCTP_TCB_UNLOCK(stcb);
1113 }
1114 SCTP_INP_INFO_RUNLOCK();
1115 return (NULL);
1116 }
1117
1118 /*
1119 * Find an association with the pointer to the inbound IP packet. This
1120 * can be a IPv4 or IPv6 packet.
1121 */
1122 struct sctp_tcb *
sctp_findassociation_addr(struct mbuf * m,int iphlen,int offset,struct sctphdr * sh,struct sctp_chunkhdr * ch,struct sctp_inpcb ** inp_p,struct sctp_nets ** netp)1123 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1124 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1125 struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1126 {
1127 int find_tcp_pool;
1128 struct ip *iph;
1129 struct sctp_tcb *retval;
1130 struct sockaddr_storage to_store, from_store;
1131 struct sockaddr *to = (struct sockaddr *)&to_store;
1132 struct sockaddr *from = (struct sockaddr *)&from_store;
1133 struct sctp_inpcb *inp;
1134
1135
1136 iph = mtod(m, struct ip *);
1137 if (iph->ip_v == IPVERSION) {
1138 /* its IPv4 */
1139 struct sockaddr_in *to4, *from4;
1140
1141 to4 = (struct sockaddr_in *)&to_store;
1142 from4 = (struct sockaddr_in *)&from_store;
1143 memset(to4, 0, sizeof(*to4));
1144 memset(from4, 0, sizeof(*from4));
1145 from4->sin_family = to4->sin_family = AF_INET;
1146 from4->sin_len = to4->sin_len = sizeof(struct sockaddr_in);
1147 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1148 to4->sin_addr.s_addr = iph->ip_dst.s_addr ;
1149 from4->sin_port = sh->src_port;
1150 to4->sin_port = sh->dest_port;
1151 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1152 /* its IPv6 */
1153 struct ip6_hdr *ip6;
1154 struct sockaddr_in6 *to6, *from6;
1155
1156 ip6 = mtod(m, struct ip6_hdr *);
1157 to6 = (struct sockaddr_in6 *)&to_store;
1158 from6 = (struct sockaddr_in6 *)&from_store;
1159 memset(to6, 0, sizeof(*to6));
1160 memset(from6, 0, sizeof(*from6));
1161 from6->sin6_family = to6->sin6_family = AF_INET6;
1162 from6->sin6_len = to6->sin6_len = sizeof(struct sockaddr_in6);
1163 from6->sin6_addr = ip6->ip6_src;
1164 to6->sin6_addr = ip6->ip6_dst;
1165 from6->sin6_port = sh->src_port;
1166 to6->sin6_port = sh->dest_port;
1167 /* Get the scopes in properly to the sin6 addr's */
1168 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
1169 /* We probably don't need this operation (jinmei@kame) */
1170 (void)in6_recoverscope(to6, &to6->sin6_addr, NULL);
1171 (void)in6_embedscope(&to6->sin6_addr, to6, NULL, NULL);
1172
1173 (void)in6_recoverscope(from6, &from6->sin6_addr, NULL);
1174 (void)in6_embedscope(&from6->sin6_addr, from6, NULL, NULL);
1175 #endif
1176 } else {
1177 /* Currently not supported. */
1178 return (NULL);
1179 }
1180 #ifdef SCTP_DEBUG
1181 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1182 printf("Looking for port %d address :",
1183 ntohs(((struct sockaddr_in *)to)->sin_port));
1184 sctp_print_address(to);
1185 printf("From for port %d address :",
1186 ntohs(((struct sockaddr_in *)from)->sin_port));
1187 sctp_print_address(from);
1188 }
1189 #endif
1190
1191 if (sh->v_tag) {
1192 /* we only go down this path if vtag is non-zero */
1193 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1194 inp_p, netp, sh->src_port, sh->dest_port);
1195 if (retval) {
1196 return (retval);
1197 }
1198 }
1199 find_tcp_pool = 0;
1200 if ((ch->chunk_type != SCTP_INITIATION) &&
1201 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1202 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1203 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1204 /* Other chunk types go to the tcp pool. */
1205 find_tcp_pool = 1;
1206 }
1207 if (inp_p) {
1208 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1209 find_tcp_pool);
1210 inp = *inp_p;
1211 } else {
1212 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1213 find_tcp_pool);
1214 }
1215 #ifdef SCTP_DEBUG
1216 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1217 printf("retval:%p inp:%p\n", retval, inp);
1218 }
1219 #endif
1220 if (retval == NULL && inp) {
1221 /* Found a EP but not this address */
1222 #ifdef SCTP_DEBUG
1223 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1224 printf("Found endpoint %p but no asoc - ep state:%x\n",
1225 inp, inp->sctp_flags);
1226 }
1227 #endif
1228 if ((ch->chunk_type == SCTP_INITIATION) ||
1229 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1230 /*
1231 * special hook, we do NOT return linp or an
1232 * association that is linked to an existing
1233 * association that is under the TCP pool (i.e. no
1234 * listener exists). The endpoint finding routine
1235 * will always find a listner before examining the
1236 * TCP pool.
1237 */
1238 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1239 #ifdef SCTP_DEBUG
1240 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1241 printf("Gak, its in the TCP pool... return NULL");
1242 }
1243 #endif
1244 if (inp_p) {
1245 *inp_p = NULL;
1246 }
1247 return (NULL);
1248 }
1249 #ifdef SCTP_DEBUG
1250 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1251 printf("Now doing SPECIAL find\n");
1252 }
1253 #endif
1254 retval = sctp_findassociation_special_addr(m, iphlen,
1255 offset, sh, inp_p, netp, to);
1256 }
1257 }
1258 #ifdef SCTP_DEBUG
1259 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1260 printf("retval is %p\n", retval);
1261 }
1262 #endif
1263 return (retval);
1264 }
1265
1266 extern int sctp_max_burst_default;
1267
1268 extern unsigned int sctp_delayed_sack_time_default;
1269 extern unsigned int sctp_heartbeat_interval_default;
1270 extern unsigned int sctp_pmtu_raise_time_default;
1271 extern unsigned int sctp_shutdown_guard_time_default;
1272 extern unsigned int sctp_secret_lifetime_default;
1273
1274 extern unsigned int sctp_rto_max_default;
1275 extern unsigned int sctp_rto_min_default;
1276 extern unsigned int sctp_rto_initial_default;
1277 extern unsigned int sctp_init_rto_max_default;
1278 extern unsigned int sctp_valid_cookie_life_default;
1279 extern unsigned int sctp_init_rtx_max_default;
1280 extern unsigned int sctp_assoc_rtx_max_default;
1281 extern unsigned int sctp_path_rtx_max_default;
1282 extern unsigned int sctp_nr_outgoing_streams_default;
1283
1284 /*
1285 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1286 * addresses. This way if we don't get a bind we by default pick a ephemeral
1287 * port with all addresses bound.
1288 */
1289 int
sctp_inpcb_alloc(struct socket * so)1290 sctp_inpcb_alloc(struct socket *so)
1291 {
1292 /*
1293 * we get called when a new endpoint starts up. We need to allocate
1294 * the sctp_inpcb structure from the zone and init it. Mark it as
1295 * unbound and find a port that we can use as an ephemeral with
1296 * INADDR_ANY. If the user binds later no problem we can then add
1297 * in the specific addresses. And setup the default parameters for
1298 * the EP.
1299 */
1300 int i, error;
1301 struct sctp_inpcb *inp;
1302 #ifdef DEBUG
1303 struct sctp_inpcb *n_inp;
1304 #endif
1305 #ifdef IPSEC
1306 struct inpcbpolicy *pcb_sp = NULL;
1307 #endif
1308 struct sctp_pcb *m;
1309 struct timeval time;
1310
1311 error = 0;
1312
1313 /* Hack alert:
1314 *
1315 * This code audits the entire INP list to see if
1316 * any ep's that are in the GONE state are now
1317 * all free. This should not happen really since when
1318 * the last association if freed we should end up deleting
1319 * the inpcb. This code including the locks should
1320 * be taken out ... since the last set of fixes I
1321 * have not seen the "Found a GONE on list" has not
1322 * came out. But i am paranoid and we will leave this
1323 * in at the cost of efficiency on allocation of PCB's.
1324 * Probably we should move this to the invariant
1325 * compile options
1326 */
1327 #ifdef DEBUG
1328 SCTP_INP_INFO_RLOCK();
1329 inp = LIST_FIRST(&sctppcbinfo.listhead);
1330 while (inp) {
1331 n_inp = LIST_NEXT(inp, sctp_list);
1332 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1333 if (LIST_FIRST(&inp->sctp_asoc_list) == NULL) {
1334 /* finish the job now */
1335 printf("Found a GONE on list\n");
1336 SCTP_INP_INFO_RUNLOCK();
1337 sctp_inpcb_free(inp, 1);
1338 SCTP_INP_INFO_RLOCK();
1339 }
1340 }
1341 inp = n_inp;
1342 }
1343 SCTP_INP_INFO_RUNLOCK();
1344 #endif /* DEBUG */
1345
1346 SCTP_INP_INFO_WLOCK();
1347 inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1348 if (inp == NULL) {
1349 printf("Out of SCTP-INPCB structures - no resources\n");
1350 SCTP_INP_INFO_WUNLOCK();
1351 return (ENOBUFS);
1352 }
1353
1354 /* zap it */
1355 memset(inp, 0, sizeof(*inp));
1356
1357 /* setup socket pointers */
1358 inp->sctp_socket = so;
1359
1360 /* setup inpcb socket too */
1361 inp->ip_inp.inp.inp_socket = so;
1362 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1363 #ifdef IPSEC
1364 if (ipsec_enabled) {
1365 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1366 if (error != 0) {
1367 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1368 SCTP_INP_INFO_WUNLOCK();
1369 return error;
1370 }
1371 /* Arrange to share the policy */
1372 inp->ip_inp.inp.inp_sp = pcb_sp;
1373 pcb_sp->sp_inph = (struct inpcb_hdr *)inp;
1374 }
1375 #endif /* IPSEC */
1376 sctppcbinfo.ipi_count_ep++;
1377 inp->inp_ip_ttl = ip_defttl;
1378 inp->inp_ip_tos = 0;
1379
1380 so->so_pcb = (void *)inp;
1381
1382 if ((so->so_type == SOCK_DGRAM) ||
1383 (so->so_type == SOCK_SEQPACKET)) {
1384 /* UDP style socket */
1385 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1386 SCTP_PCB_FLAGS_UNBOUND);
1387 inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1388 /* Be sure it is NON-BLOCKING IO for UDP */
1389 /*so->so_state |= SS_NBIO;*/
1390 } else if (so->so_type == SOCK_STREAM) {
1391 /* TCP style socket */
1392 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1393 SCTP_PCB_FLAGS_UNBOUND);
1394 inp->sctp_flags |= (SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1395 /* Be sure we have blocking IO bu default */
1396 so->so_state &= ~SS_NBIO;
1397 } else {
1398 /*
1399 * unsupported socket type (RAW, etc)- in case we missed
1400 * it in protosw
1401 */
1402 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1403 SCTP_INP_INFO_WUNLOCK();
1404 return (EOPNOTSUPP);
1405 }
1406 inp->sctp_tcbhash = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_hash);
1407 if (inp->sctp_tcbhash == NULL) {
1408 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1409 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1410 SCTP_INP_INFO_WUNLOCK();
1411 return (ENOBUFS);
1412 } else {
1413 for (i = 0; i < sctp_pcbtblsize; i++)
1414 LIST_INIT(&inp->sctp_tcbhash[i]);
1415 for (i = 1; i < sctp_pcbtblsize; i <<= 1)
1416 continue;
1417 inp->sctp_hashmark = i - 1;
1418 }
1419 /* LOCK init's */
1420 SCTP_INP_LOCK_INIT(inp);
1421 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1422 /* lock the new ep */
1423 SCTP_INP_WLOCK(inp);
1424
1425 /* add it to the info area */
1426 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1427 SCTP_INP_INFO_WUNLOCK();
1428
1429 LIST_INIT(&inp->sctp_addr_list);
1430 LIST_INIT(&inp->sctp_asoc_list);
1431 TAILQ_INIT(&inp->sctp_queue_list);
1432 /* Init the timer structure for signature change */
1433 callout_init(&inp->sctp_ep.signature_change.timer, 0);
1434 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1435
1436 /* now init the actual endpoint default data */
1437 m = &inp->sctp_ep;
1438
1439 /* setup the base timeout information */
1440 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1441 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1442 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1443 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_heartbeat_interval_default; /* this is in MSEC */
1444 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1445 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1446 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1447 /* all max/min max are in ms */
1448 m->sctp_maxrto = sctp_rto_max_default;
1449 m->sctp_minrto = sctp_rto_min_default;
1450 m->initial_rto = sctp_rto_initial_default;
1451 m->initial_init_rto_max = sctp_init_rto_max_default;
1452
1453 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1454
1455 m->max_init_times = sctp_init_rtx_max_default;
1456 m->max_send_times = sctp_assoc_rtx_max_default;
1457 m->def_net_failure = sctp_path_rtx_max_default;
1458 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1459 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1460 m->max_burst = sctp_max_burst_default;
1461 /* number of streams to pre-open on a association */
1462 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1463
1464 /* Add adaption cookie */
1465 m->adaption_layer_indicator = 0x504C5253;
1466
1467 /* Minimum cookie size */
1468 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1469 sizeof(struct sctp_state_cookie);
1470 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1471
1472 /* Setup the initial secret */
1473 SCTP_GETTIME_TIMEVAL(&time);
1474 m->time_of_secret_change = time.tv_sec;
1475
1476 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1477 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1478 }
1479 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1480
1481 /* How long is a cookie good for ? */
1482 m->def_cookie_life = sctp_valid_cookie_life_default;
1483 SCTP_INP_WUNLOCK(inp);
1484 return (error);
1485 }
1486
1487
1488 void
sctp_move_pcb_and_assoc(struct sctp_inpcb * old_inp,struct sctp_inpcb * new_inp,struct sctp_tcb * stcb)1489 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1490 struct sctp_tcb *stcb)
1491 {
1492 uint16_t lport, rport;
1493 struct sctppcbhead *head;
1494 struct sctp_laddr *laddr, *oladdr;
1495
1496 SCTP_TCB_UNLOCK(stcb);
1497 SCTP_INP_INFO_WLOCK();
1498 SCTP_INP_WLOCK(old_inp);
1499 SCTP_INP_WLOCK(new_inp);
1500 SCTP_TCB_LOCK(stcb);
1501
1502 new_inp->sctp_ep.time_of_secret_change =
1503 old_inp->sctp_ep.time_of_secret_change;
1504 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1505 sizeof(old_inp->sctp_ep.secret_key));
1506 new_inp->sctp_ep.current_secret_number =
1507 old_inp->sctp_ep.current_secret_number;
1508 new_inp->sctp_ep.last_secret_number =
1509 old_inp->sctp_ep.last_secret_number;
1510 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1511
1512 /* Copy the port across */
1513 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1514 rport = stcb->rport;
1515 /* Pull the tcb from the old association */
1516 LIST_REMOVE(stcb, sctp_tcbhash);
1517 LIST_REMOVE(stcb, sctp_tcblist);
1518
1519 /* Now insert the new_inp into the TCP connected hash */
1520 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1521 sctppcbinfo.hashtcpmark)];
1522
1523 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1524
1525 /* Now move the tcb into the endpoint list */
1526 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1527 /*
1528 * Question, do we even need to worry about the ep-hash since
1529 * we only have one connection? Probably not :> so lets
1530 * get rid of it and not suck up any kernel memory in that.
1531 */
1532 SCTP_INP_INFO_WUNLOCK();
1533 stcb->sctp_socket = new_inp->sctp_socket;
1534 stcb->sctp_ep = new_inp;
1535 if (new_inp->sctp_tcbhash != NULL) {
1536 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash,
1537 new_inp->sctp_tcbhash);
1538 new_inp->sctp_tcbhash = NULL;
1539 }
1540 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1541 /* Subset bound, so copy in the laddr list from the old_inp */
1542 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1543 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1544 sctppcbinfo.ipi_zone_laddr);
1545 if (laddr == NULL) {
1546 /*
1547 * Gak, what can we do? This assoc is really
1548 * HOSED. We probably should send an abort
1549 * here.
1550 */
1551 #ifdef SCTP_DEBUG
1552 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1553 printf("Association hosed in TCP model, out of laddr memory\n");
1554 }
1555 #endif /* SCTP_DEBUG */
1556 continue;
1557 }
1558 sctppcbinfo.ipi_count_laddr++;
1559 sctppcbinfo.ipi_gencnt_laddr++;
1560 memset(laddr, 0, sizeof(*laddr));
1561 laddr->ifa = oladdr->ifa;
1562 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1563 sctp_nxt_addr);
1564 new_inp->laddr_count++;
1565 }
1566 }
1567 SCTP_INP_WUNLOCK(new_inp);
1568 SCTP_INP_WUNLOCK(old_inp);
1569 }
1570
1571 static int
sctp_isport_inuse(struct sctp_inpcb * inp,uint16_t lport)1572 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1573 {
1574 struct sctppcbhead *head;
1575 struct sctp_inpcb *t_inp;
1576
1577 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1578 sctppcbinfo.hashmark)];
1579 LIST_FOREACH(t_inp, head, sctp_hash) {
1580 if (t_inp->sctp_lport != lport) {
1581 continue;
1582 }
1583 /* This one is in use. */
1584 /* check the v6/v4 binding issue */
1585 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1586 (((struct in6pcb *)t_inp)->in6p_flags & IN6P_IPV6_V6ONLY)) {
1587 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1588 /* collision in V6 space */
1589 return (1);
1590 } else {
1591 /* inp is BOUND_V4 no conflict */
1592 continue;
1593 }
1594 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1595 /* t_inp is bound v4 and v6, conflict always */
1596 return (1);
1597 } else {
1598 /* t_inp is bound only V4 */
1599 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1600 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1601 ) {
1602 /* no conflict */
1603 continue;
1604 }
1605 /* else fall through to conflict */
1606 }
1607 return (1);
1608 }
1609 return (0);
1610 }
1611
1612 int
sctp_inpcb_bind(struct socket * so,struct sockaddr * addr,struct lwp * l)1613 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct lwp *l)
1614 {
1615 /* bind a ep to a socket address */
1616 struct sctppcbhead *head;
1617 struct sctp_inpcb *inp, *inp_tmp;
1618 int bindall;
1619 uint16_t lport;
1620 int error;
1621
1622 lport = 0;
1623 error = 0;
1624 bindall = 1;
1625 inp = (struct sctp_inpcb *)so->so_pcb;
1626 #ifdef SCTP_DEBUG
1627 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1628 if (addr) {
1629 printf("Bind called port:%d\n",
1630 ntohs(((struct sockaddr_in *)addr)->sin_port));
1631 printf("Addr :");
1632 sctp_print_address(addr);
1633 }
1634 }
1635 #endif /* SCTP_DEBUG */
1636 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1637 /* already did a bind, subsequent binds NOT allowed ! */
1638 return (EINVAL);
1639 }
1640
1641 if (addr != NULL) {
1642 if (addr->sa_family == AF_INET) {
1643 struct sockaddr_in *sin;
1644
1645 /* IPV6_V6ONLY socket? */
1646 if (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY) {
1647 return (EINVAL);
1648 }
1649
1650 if (addr->sa_len != sizeof(*sin))
1651 return (EINVAL);
1652
1653 sin = (struct sockaddr_in *)addr;
1654 lport = sin->sin_port;
1655
1656 if (sin->sin_addr.s_addr != INADDR_ANY) {
1657 bindall = 0;
1658 }
1659 #ifdef IPSEC
1660 inp->ip_inp.inp.inp_af = AF_INET;
1661 #endif
1662 } else if (addr->sa_family == AF_INET6) {
1663 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1664 struct sockaddr_in6 *sin6;
1665
1666 sin6 = (struct sockaddr_in6 *)addr;
1667
1668 if (addr->sa_len != sizeof(*sin6))
1669 return (EINVAL);
1670
1671 lport = sin6->sin6_port;
1672 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1673 bindall = 0;
1674 /* KAME hack: embed scopeid */
1675 error = sa6_embedscope(sin6, ip6_use_defzone);
1676 if (error != 0)
1677 return (error);
1678 }
1679 #ifndef SCOPEDROUTING
1680 /* this must be cleared for ifa_ifwithaddr() */
1681 sin6->sin6_scope_id = 0;
1682 #endif /* SCOPEDROUTING */
1683 #ifdef IPSEC
1684 inp->ip_inp.inp.inp_af = AF_INET6;
1685 #endif
1686 } else {
1687 return (EAFNOSUPPORT);
1688 }
1689 #ifdef IPSEC
1690 if (ipsec_enabled) {
1691 inp->ip_inp.inp.inp_socket = so;
1692 error = ipsec_init_pcbpolicy(so, &inp->ip_inp.inp.inp_sp);
1693 if (error != 0)
1694 return (error);
1695 inp->ip_inp.inp.inp_sp->sp_inph = (struct inpcb_hdr *)inp;
1696 }
1697 #endif
1698 }
1699 SCTP_INP_INFO_WLOCK();
1700 #ifdef SCTP_DEBUG
1701 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1702 printf("sctp_inpcb_bind: after SCTP_INP_INFO_WLOCK\n");
1703 }
1704 #endif /* SCTP_DEBUG */
1705 SCTP_INP_WLOCK(inp);
1706 /* increase our count due to the unlock we do */
1707 SCTP_INP_INCR_REF(inp);
1708 if (lport) {
1709 enum kauth_network_req req;
1710 /*
1711 * Did the caller specify a port? if so we must see if a
1712 * ep already has this one bound.
1713 */
1714 if (ntohs(lport) < IPPORT_RESERVED)
1715 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
1716 else
1717 req = KAUTH_REQ_NETWORK_BIND_PORT;
1718
1719 error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_BIND,
1720 req, so, addr, NULL);
1721 if (error) {
1722 SCTP_INP_DECR_REF(inp);
1723 SCTP_INP_WUNLOCK(inp);
1724 SCTP_INP_INFO_WUNLOCK();
1725 return (EACCES);
1726 }
1727 SCTP_INP_WUNLOCK(inp);
1728 inp_tmp = sctp_pcb_findep(addr, 0, 1);
1729 if (inp_tmp != NULL) {
1730 /* lock guy returned and lower count
1731 * note that we are not bound so inp_tmp
1732 * should NEVER be inp. And it is this
1733 * inp (inp_tmp) that gets the reference
1734 * bump, so we must lower it.
1735 */
1736 SCTP_INP_WLOCK(inp_tmp);
1737 SCTP_INP_DECR_REF(inp_tmp);
1738 SCTP_INP_WUNLOCK(inp_tmp);
1739
1740 /* unlock info */
1741 SCTP_INP_INFO_WUNLOCK();
1742 return EADDRINUSE;
1743 }
1744 SCTP_INP_WLOCK(inp);
1745 if (bindall) {
1746 /* verify that no lport is not used by a singleton */
1747 if (sctp_isport_inuse(inp, lport)) {
1748 /* Sorry someone already has this one bound */
1749 SCTP_INP_DECR_REF(inp);
1750 SCTP_INP_WUNLOCK(inp);
1751 SCTP_INP_INFO_WUNLOCK();
1752 return EADDRINUSE;
1753 }
1754 }
1755 } else {
1756 /*
1757 * get any port but lets make sure no one has any address
1758 * with this port bound
1759 */
1760
1761 /*
1762 * setup the inp to the top (I could use the union but this
1763 * is just as easy
1764 */
1765 uint32_t port_guess;
1766 uint16_t port_attempt;
1767 int not_done=1;
1768
1769 while (not_done) {
1770 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1771 port_attempt = (port_guess & 0x0000ffff);
1772 if (port_attempt == 0) {
1773 goto next_half;
1774 }
1775 if (port_attempt < IPPORT_RESERVED) {
1776 port_attempt += IPPORT_RESERVED;
1777 }
1778
1779 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1780 /* got a port we can use */
1781 not_done = 0;
1782 continue;
1783 }
1784 /* try upper half */
1785 next_half:
1786 port_attempt = ((port_guess >> 16) & 0x0000ffff);
1787 if (port_attempt == 0) {
1788 goto last_try;
1789 }
1790 if (port_attempt < IPPORT_RESERVED) {
1791 port_attempt += IPPORT_RESERVED;
1792 }
1793 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1794 /* got a port we can use */
1795 not_done = 0;
1796 continue;
1797 }
1798 /* try two half's added together */
1799 last_try:
1800 port_attempt = (((port_guess >> 16) & 0x0000ffff) + (port_guess & 0x0000ffff));
1801 if (port_attempt == 0) {
1802 /* get a new random number */
1803 continue;
1804 }
1805 if (port_attempt < IPPORT_RESERVED) {
1806 port_attempt += IPPORT_RESERVED;
1807 }
1808 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1809 /* got a port we can use */
1810 not_done = 0;
1811 continue;
1812 }
1813 }
1814 /* we don't get out of the loop until we have a port */
1815 lport = htons(port_attempt);
1816 }
1817 SCTP_INP_DECR_REF(inp);
1818 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
1819 /* this really should not happen. The guy
1820 * did a non-blocking bind and then did a close
1821 * at the same time.
1822 */
1823 SCTP_INP_WUNLOCK(inp);
1824 SCTP_INP_INFO_WUNLOCK();
1825 return (EINVAL);
1826 }
1827 /* ok we look clear to give out this port, so lets setup the binding */
1828 if (bindall) {
1829 /* binding to all addresses, so just set in the proper flags */
1830 inp->sctp_flags |= (SCTP_PCB_FLAGS_BOUNDALL |
1831 SCTP_PCB_FLAGS_DO_ASCONF);
1832 /* set the automatic addr changes from kernel flag */
1833 if (sctp_auto_asconf == 0) {
1834 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1835 } else {
1836 inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
1837 }
1838 } else {
1839 /*
1840 * bind specific, make sure flags is off and add a new address
1841 * structure to the sctp_addr_list inside the ep structure.
1842 *
1843 * We will need to allocate one and insert it at the head.
1844 * The socketopt call can just insert new addresses in there
1845 * as well. It will also have to do the embed scope kame hack
1846 * too (before adding).
1847 */
1848 struct ifaddr *ifa;
1849 struct sockaddr_storage store_sa;
1850
1851 memset(&store_sa, 0, sizeof(store_sa));
1852 if (addr->sa_family == AF_INET) {
1853 struct sockaddr_in *sin;
1854
1855 sin = (struct sockaddr_in *)&store_sa;
1856 memcpy(sin, addr, sizeof(struct sockaddr_in));
1857 sin->sin_port = 0;
1858 } else if (addr->sa_family == AF_INET6) {
1859 struct sockaddr_in6 *sin6;
1860
1861 sin6 = (struct sockaddr_in6 *)&store_sa;
1862 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
1863 sin6->sin6_port = 0;
1864 }
1865 /*
1866 * first find the interface with the bound address
1867 * need to zero out the port to find the address! yuck!
1868 * can't do this earlier since need port for sctp_pcb_findep()
1869 */
1870 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
1871 if (ifa == NULL) {
1872 /* Can't find an interface with that address */
1873 SCTP_INP_WUNLOCK(inp);
1874 SCTP_INP_INFO_WUNLOCK();
1875 return (EADDRNOTAVAIL);
1876 }
1877 if (addr->sa_family == AF_INET6) {
1878 struct in6_ifaddr *ifa6;
1879 ifa6 = (struct in6_ifaddr *)ifa;
1880 /*
1881 * allow binding of deprecated addresses as per
1882 * RFC 2462 and ipng discussion
1883 */
1884 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
1885 IN6_IFF_ANYCAST |
1886 IN6_IFF_NOTREADY)) {
1887 /* Can't bind a non-existent addr. */
1888 SCTP_INP_WUNLOCK(inp);
1889 SCTP_INP_INFO_WUNLOCK();
1890 return (EINVAL);
1891 }
1892 }
1893 /* we're not bound all */
1894 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
1895 #if 0 /* use sysctl now */
1896 /* don't allow automatic addr changes from kernel */
1897 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1898 #endif
1899 /* set the automatic addr changes from kernel flag */
1900 if (sctp_auto_asconf == 0) {
1901 inp->sctp_flags &= ~SCTP_PCB_FLAGS_AUTO_ASCONF;
1902 } else {
1903 inp->sctp_flags |= SCTP_PCB_FLAGS_AUTO_ASCONF;
1904 }
1905 /* allow bindx() to send ASCONF's for binding changes */
1906 inp->sctp_flags |= SCTP_PCB_FLAGS_DO_ASCONF;
1907 /* add this address to the endpoint list */
1908 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
1909 if (error != 0) {
1910 SCTP_INP_WUNLOCK(inp);
1911 SCTP_INP_INFO_WUNLOCK();
1912 return (error);
1913 }
1914 inp->laddr_count++;
1915 }
1916 /* find the bucket */
1917 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1918 sctppcbinfo.hashmark)];
1919 /* put it in the bucket */
1920 LIST_INSERT_HEAD(head, inp, sctp_hash);
1921 #ifdef SCTP_DEBUG
1922 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1923 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
1924 }
1925 #endif
1926 /* set in the port */
1927 inp->sctp_lport = lport;
1928
1929 /* turn off just the unbound flag */
1930 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1931 SCTP_INP_WUNLOCK(inp);
1932 SCTP_INP_INFO_WUNLOCK();
1933 return (0);
1934 }
1935
1936
1937 static void
sctp_iterator_inp_being_freed(struct sctp_inpcb * inp,struct sctp_inpcb * inp_next)1938 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
1939 {
1940 struct sctp_iterator *it;
1941 /* We enter with the only the ITERATOR_LOCK in place and
1942 * A write lock on the inp_info stuff.
1943 */
1944
1945 /* Go through all iterators, we must do this since
1946 * it is possible that some iterator does NOT have
1947 * the lock, but is waiting for it. And the one that
1948 * had the lock has either moved in the last iteration
1949 * or we just cleared it above. We need to find all
1950 * of those guys. The list of iterators should never
1951 * be very big though.
1952 */
1953 LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
1954 if (it == inp->inp_starting_point_for_iterator)
1955 /* skip this guy, he's special */
1956 continue;
1957 if (it->inp == inp) {
1958 /* This is tricky and we DON'T lock the iterator.
1959 * Reason is he's running but waiting for me since
1960 * inp->inp_starting_point_for_iterator has the lock
1961 * on me (the guy above we skipped). This tells us
1962 * its is not running but waiting for inp->inp_starting_point_for_iterator
1963 * to be released by the guy that does have our INP in a lock.
1964 */
1965 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1966 it->inp = NULL;
1967 it->stcb = NULL;
1968 } else {
1969 /* set him up to do the next guy not me */
1970 it->inp = inp_next;
1971 it->stcb = NULL;
1972 }
1973 }
1974 }
1975 it = inp->inp_starting_point_for_iterator;
1976 if (it) {
1977 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1978 it->inp = NULL;
1979 } else {
1980 it->inp = inp_next;
1981 }
1982 it->stcb = NULL;
1983 }
1984 }
1985
1986 /* release sctp_inpcb unbind the port */
1987 void
sctp_inpcb_free(struct sctp_inpcb * inp,int immediate)1988 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate)
1989 {
1990 /*
1991 * Here we free a endpoint. We must find it (if it is in the Hash
1992 * table) and remove it from there. Then we must also find it in
1993 * the overall list and remove it from there. After all removals are
1994 * complete then any timer has to be stopped. Then start the actual
1995 * freeing.
1996 * a) Any local lists.
1997 * b) Any associations.
1998 * c) The hash of all associations.
1999 * d) finally the ep itself.
2000 */
2001 struct sctp_inpcb *inp_save;
2002 struct sctp_tcb *asoc, *nasoc;
2003 struct sctp_laddr *laddr, *nladdr;
2004 struct inpcb *ip_pcb;
2005 struct socket *so;
2006 struct sctp_socket_q_list *sq;
2007 int s, cnt;
2008 struct rtentry *rt;
2009
2010 s = splsoftnet();
2011 SCTP_ASOC_CREATE_LOCK(inp);
2012 SCTP_INP_WLOCK(inp);
2013
2014 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2015 /* been here before */
2016 splx(s);
2017 printf("Endpoint was all gone (dup free)?\n");
2018 SCTP_INP_WUNLOCK(inp);
2019 SCTP_ASOC_CREATE_UNLOCK(inp);
2020 return;
2021 }
2022 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2023
2024 sctp_m_freem(inp->control);
2025 inp->control = NULL;
2026 sctp_m_freem(inp->pkt);
2027 inp->pkt = NULL;
2028 so = inp->sctp_socket;
2029 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main
2030 * pointer here but I will
2031 * be nice :> (i.e. ip_pcb = ep;)
2032 */
2033
2034 if (immediate == 0) {
2035 int cnt_in_sd;
2036 cnt_in_sd = 0;
2037 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2038 asoc = nasoc) {
2039 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2040 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2041 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2042 /* Just abandon things in the front states */
2043 SCTP_TCB_LOCK(asoc);
2044 SCTP_INP_WUNLOCK(inp);
2045 sctp_free_assoc(inp, asoc);
2046 SCTP_INP_WLOCK(inp);
2047 continue;
2048 } else {
2049 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2050 }
2051 if ((asoc->asoc.size_on_delivery_queue > 0) ||
2052 (asoc->asoc.size_on_reasm_queue > 0) ||
2053 (asoc->asoc.size_on_all_streams > 0) ||
2054 (so && (so->so_rcv.sb_cc > 0))
2055 ) {
2056 /* Left with Data unread */
2057 struct mbuf *op_err;
2058 MGET(op_err, M_DONTWAIT, MT_DATA);
2059 if (op_err) {
2060 /* Fill in the user initiated abort */
2061 struct sctp_paramhdr *ph;
2062 op_err->m_len =
2063 sizeof(struct sctp_paramhdr);
2064 ph = mtod(op_err,
2065 struct sctp_paramhdr *);
2066 ph->param_type = htons(
2067 SCTP_CAUSE_USER_INITIATED_ABT);
2068 ph->param_length = htons(op_err->m_len);
2069 }
2070 SCTP_TCB_LOCK(asoc);
2071 sctp_send_abort_tcb(asoc, op_err);
2072
2073 SCTP_INP_WUNLOCK(inp);
2074 sctp_free_assoc(inp, asoc);
2075 SCTP_INP_WLOCK(inp);
2076 continue;
2077 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2078 TAILQ_EMPTY(&asoc->asoc.sent_queue)) {
2079 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2080 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2081 /* there is nothing queued to send, so I send shutdown */
2082 SCTP_TCB_LOCK(asoc);
2083 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2084 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2085 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2086 asoc->asoc.primary_destination);
2087 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2088 asoc->asoc.primary_destination);
2089 sctp_chunk_output(inp, asoc, 1);
2090 SCTP_TCB_UNLOCK(asoc);
2091 }
2092 } else {
2093 /* mark into shutdown pending */
2094 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2095 }
2096 cnt_in_sd++;
2097 }
2098 /* now is there some left in our SHUTDOWN state? */
2099 if (cnt_in_sd) {
2100 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_GONE;
2101 splx(s);
2102 SCTP_INP_WUNLOCK(inp);
2103 SCTP_ASOC_CREATE_UNLOCK(inp);
2104 return;
2105 }
2106 }
2107 #if defined(__FreeBSD__) && __FreeBSD_version >= 503000
2108 if (inp->refcount) {
2109 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2110 SCTP_INP_WUNLOCK(inp);
2111 SCTP_ASOC_CREATE_UNLOCK(inp);
2112 return;
2113 }
2114 #endif
2115 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2116
2117 /* XXX */
2118 rt = rtcache_validate(&ip_pcb->inp_route);
2119 rtcache_unref(rt, &ip_pcb->inp_route);
2120
2121 callout_stop(&inp->sctp_ep.signature_change.timer);
2122 callout_destroy(&inp->sctp_ep.signature_change.timer);
2123
2124 if (so) {
2125 /* First take care of socket level things */
2126 #ifdef IPSEC
2127 if (ipsec_enabled)
2128 ipsec_delete_pcbpolicy(ip_pcb);
2129 #endif /*IPSEC*/
2130 so->so_pcb = 0;
2131 }
2132
2133 if (ip_pcb->inp_options) {
2134 (void)m_free(ip_pcb->inp_options);
2135 ip_pcb->inp_options = 0;
2136 }
2137 rtcache_free(&ip_pcb->inp_route);
2138 if (ip_pcb->inp_moptions) {
2139 ip_freemoptions(ip_pcb->inp_moptions);
2140 ip_pcb->inp_moptions = 0;
2141 }
2142 inp->inp_vflag = 0;
2143
2144 /* Now the sctp_pcb things */
2145 /*
2146 * free each asoc if it is not already closed/free. we can't use
2147 * the macro here since le_next will get freed as part of the
2148 * sctp_free_assoc() call.
2149 */
2150 cnt = 0;
2151 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2152 asoc = nasoc) {
2153 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2154 SCTP_TCB_LOCK(asoc);
2155 if (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) {
2156 struct mbuf *op_err;
2157 MGET(op_err, M_DONTWAIT, MT_DATA);
2158 if (op_err) {
2159 /* Fill in the user initiated abort */
2160 struct sctp_paramhdr *ph;
2161 op_err->m_len = sizeof(struct sctp_paramhdr);
2162 ph = mtod(op_err, struct sctp_paramhdr *);
2163 ph->param_type = htons(
2164 SCTP_CAUSE_USER_INITIATED_ABT);
2165 ph->param_length = htons(op_err->m_len);
2166 }
2167 sctp_send_abort_tcb(asoc, op_err);
2168 }
2169 cnt++;
2170 /*
2171 * sctp_free_assoc() will call sctp_inpcb_free(),
2172 * if SCTP_PCB_FLAGS_SOCKET_GONE set.
2173 * So, we clear it before sctp_free_assoc() making sure
2174 * no double sctp_inpcb_free().
2175 */
2176 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SOCKET_GONE;
2177 SCTP_INP_WUNLOCK(inp);
2178 sctp_free_assoc(inp, asoc);
2179 SCTP_INP_WLOCK(inp);
2180 }
2181 while ((sq = TAILQ_FIRST(&inp->sctp_queue_list)) != NULL) {
2182 TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
2183 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
2184 sctppcbinfo.ipi_count_sockq--;
2185 sctppcbinfo.ipi_gencnt_sockq++;
2186 }
2187 inp->sctp_socket = 0;
2188 /* Now first we remove ourselves from the overall list of all EP's */
2189
2190 /* Unlock inp first, need correct order */
2191 SCTP_INP_WUNLOCK(inp);
2192 /* now iterator lock */
2193 SCTP_ITERATOR_LOCK();
2194 /* now info lock */
2195 SCTP_INP_INFO_WLOCK();
2196 /* now reget the inp lock */
2197 SCTP_INP_WLOCK(inp);
2198
2199 inp_save = LIST_NEXT(inp, sctp_list);
2200 LIST_REMOVE(inp, sctp_list);
2201 /*
2202 * Now the question comes as to if this EP was ever bound at all.
2203 * If it was, then we must pull it out of the EP hash list.
2204 */
2205 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2206 SCTP_PCB_FLAGS_UNBOUND) {
2207 /*
2208 * ok, this guy has been bound. It's port is somewhere
2209 * in the sctppcbinfo hash table. Remove it!
2210 */
2211 LIST_REMOVE(inp, sctp_hash);
2212 }
2213 /* fix any iterators only after out of the list */
2214 sctp_iterator_inp_being_freed(inp, inp_save);
2215 SCTP_ITERATOR_UNLOCK();
2216 /*
2217 * if we have an address list the following will free the list of
2218 * ifaddr's that are set into this ep. Again macro limitations here,
2219 * since the LIST_FOREACH could be a bad idea.
2220 */
2221 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2222 laddr = nladdr) {
2223 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2224 LIST_REMOVE(laddr, sctp_nxt_addr);
2225 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2226 sctppcbinfo.ipi_gencnt_laddr++;
2227 sctppcbinfo.ipi_count_laddr--;
2228 }
2229
2230 /* Now lets see about freeing the EP hash table. */
2231 if (inp->sctp_tcbhash != NULL) {
2232 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_hash, inp->sctp_tcbhash);
2233 inp->sctp_tcbhash = NULL;
2234 }
2235 SCTP_INP_WUNLOCK(inp);
2236 SCTP_ASOC_CREATE_UNLOCK(inp);
2237 SCTP_INP_LOCK_DESTROY(inp);
2238 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2239
2240 /* Now we must put the ep memory back into the zone pool */
2241 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2242 sctppcbinfo.ipi_count_ep--;
2243
2244 SCTP_INP_INFO_WUNLOCK();
2245 splx(s);
2246
2247 sofree(so);
2248 mutex_enter(softnet_lock);
2249 }
2250
2251
2252 struct sctp_nets *
sctp_findnet(struct sctp_tcb * stcb,struct sockaddr * addr)2253 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2254 {
2255 struct sctp_nets *net;
2256
2257 /* use the peer's/remote port for lookup if unspecified */
2258 #if 0 /* why do we need to check the port for a nets list on an assoc? */
2259 if (stcb->rport != sin->sin_port) {
2260 /* we cheat and just a sin for this test */
2261 return (NULL);
2262 }
2263 #endif
2264 /* locate the address */
2265 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2266 if (sctp_cmpaddr(addr, rtcache_getdst(&net->ro)))
2267 return (net);
2268 }
2269 return (NULL);
2270 }
2271
2272
2273 /*
2274 * add's a remote endpoint address, done with the INIT/INIT-ACK
2275 * as well as when a ASCONF arrives that adds it. It will also
2276 * initialize all the cwnd stats of stuff.
2277 */
2278 int
sctp_is_address_on_local_host(struct sockaddr * addr)2279 sctp_is_address_on_local_host(struct sockaddr *addr)
2280 {
2281 struct ifnet *ifn;
2282 struct ifaddr *ifa;
2283 int s;
2284
2285 s = pserialize_read_enter();
2286 IFNET_READER_FOREACH(ifn) {
2287 IFADDR_READER_FOREACH(ifa, ifn) {
2288 if (addr->sa_family == ifa->ifa_addr->sa_family) {
2289 /* same family */
2290 if (addr->sa_family == AF_INET) {
2291 struct sockaddr_in *sin, *sin_c;
2292 sin = (struct sockaddr_in *)addr;
2293 sin_c = (struct sockaddr_in *)
2294 ifa->ifa_addr;
2295 if (sin->sin_addr.s_addr ==
2296 sin_c->sin_addr.s_addr) {
2297 /* we are on the same machine */
2298 pserialize_read_exit(s);
2299 return (1);
2300 }
2301 } else if (addr->sa_family == AF_INET6) {
2302 struct sockaddr_in6 *sin6, *sin_c6;
2303 sin6 = (struct sockaddr_in6 *)addr;
2304 sin_c6 = (struct sockaddr_in6 *)
2305 ifa->ifa_addr;
2306 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2307 &sin_c6->sin6_addr)) {
2308 /* we are on the same machine */
2309 pserialize_read_exit(s);
2310 return (1);
2311 }
2312 }
2313 }
2314 }
2315 }
2316 pserialize_read_exit(s);
2317
2318 return (0);
2319 }
2320
2321 int
sctp_add_remote_addr(struct sctp_tcb * stcb,struct sockaddr * newaddr,int set_scope,int from)2322 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2323 int set_scope, int from)
2324 {
2325 /*
2326 * The following is redundant to the same lines in the
2327 * sctp_aloc_assoc() but is needed since other's call the add
2328 * address function
2329 */
2330 struct sctp_nets *net, *netfirst;
2331 struct rtentry *rt, *netfirst_rt;
2332 int addr_inscope;
2333
2334 #ifdef SCTP_DEBUG
2335 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2336 printf("Adding an address (from:%d) to the peer: ", from);
2337 sctp_print_address(newaddr);
2338 }
2339 #endif
2340 netfirst = sctp_findnet(stcb, newaddr);
2341 if (netfirst) {
2342 /*
2343 * Lie and return ok, we don't want to make the association
2344 * go away for this behavior. It will happen in the TCP model
2345 * in a connected socket. It does not reach the hash table
2346 * until after the association is built so it can't be found.
2347 * Mark as reachable, since the initial creation will have
2348 * been cleared and the NOT_IN_ASSOC flag will have been
2349 * added... and we don't want to end up removing it back out.
2350 */
2351 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2352 netfirst->dest_state = (SCTP_ADDR_REACHABLE|
2353 SCTP_ADDR_UNCONFIRMED);
2354 } else {
2355 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2356 }
2357
2358 return (0);
2359 }
2360 addr_inscope = 1;
2361 if (newaddr->sa_family == AF_INET) {
2362 struct sockaddr_in *sin;
2363 sin = (struct sockaddr_in *)newaddr;
2364 if (sin->sin_addr.s_addr == 0) {
2365 /* Invalid address */
2366 return (-1);
2367 }
2368 /* zero out the bzero area */
2369 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2370
2371 /* assure len is set */
2372 sin->sin_len = sizeof(struct sockaddr_in);
2373 if (set_scope) {
2374 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2375 stcb->ipv4_local_scope = 1;
2376 #else
2377 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2378 stcb->asoc.ipv4_local_scope = 1;
2379 }
2380 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2381
2382 if (sctp_is_address_on_local_host(newaddr)) {
2383 stcb->asoc.loopback_scope = 1;
2384 stcb->asoc.ipv4_local_scope = 1;
2385 stcb->asoc.local_scope = 1;
2386 stcb->asoc.site_scope = 1;
2387 }
2388 } else {
2389 if (from == 8) {
2390 /* From connectx */
2391 if (sctp_is_address_on_local_host(newaddr)) {
2392 stcb->asoc.loopback_scope = 1;
2393 stcb->asoc.ipv4_local_scope = 1;
2394 stcb->asoc.local_scope = 1;
2395 stcb->asoc.site_scope = 1;
2396 }
2397 }
2398 /* Validate the address is in scope */
2399 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2400 (stcb->asoc.ipv4_local_scope == 0)) {
2401 addr_inscope = 0;
2402 }
2403 }
2404 } else if (newaddr->sa_family == AF_INET6) {
2405 struct sockaddr_in6 *sin6;
2406 sin6 = (struct sockaddr_in6 *)newaddr;
2407 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2408 /* Invalid address */
2409 return (-1);
2410 }
2411 /* assure len is set */
2412 sin6->sin6_len = sizeof(struct sockaddr_in6);
2413 if (set_scope) {
2414 if (sctp_is_address_on_local_host(newaddr)) {
2415 stcb->asoc.loopback_scope = 1;
2416 stcb->asoc.local_scope = 1;
2417 stcb->asoc.ipv4_local_scope = 1;
2418 stcb->asoc.site_scope = 1;
2419 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2420 /*
2421 * If the new destination is a LINK_LOCAL
2422 * we must have common site scope. Don't set
2423 * the local scope since we may not share all
2424 * links, only loopback can do this.
2425 * Links on the local network would also
2426 * be on our private network for v4 too.
2427 */
2428 stcb->asoc.ipv4_local_scope = 1;
2429 stcb->asoc.site_scope = 1;
2430 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2431 /*
2432 * If the new destination is SITE_LOCAL
2433 * then we must have site scope in common.
2434 */
2435 stcb->asoc.site_scope = 1;
2436 }
2437 } else {
2438 if (from == 8) {
2439 /* From connectx */
2440 if (sctp_is_address_on_local_host(newaddr)) {
2441 stcb->asoc.loopback_scope = 1;
2442 stcb->asoc.ipv4_local_scope = 1;
2443 stcb->asoc.local_scope = 1;
2444 stcb->asoc.site_scope = 1;
2445 }
2446 }
2447 /* Validate the address is in scope */
2448 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2449 (stcb->asoc.loopback_scope == 0)) {
2450 addr_inscope = 0;
2451 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2452 (stcb->asoc.local_scope == 0)) {
2453 addr_inscope = 0;
2454 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2455 (stcb->asoc.site_scope == 0)) {
2456 addr_inscope = 0;
2457 }
2458 }
2459 } else {
2460 /* not supported family type */
2461 return (-1);
2462 }
2463 net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2464 if (net == NULL) {
2465 return (-1);
2466 }
2467 sctppcbinfo.ipi_count_raddr++;
2468 sctppcbinfo.ipi_gencnt_raddr++;
2469 memset(net, 0, sizeof(*net));
2470 if (newaddr->sa_family == AF_INET) {
2471 ((struct sockaddr_in *)newaddr)->sin_port = stcb->rport;
2472 } else if (newaddr->sa_family == AF_INET6) {
2473 ((struct sockaddr_in6 *)newaddr)->sin6_port = stcb->rport;
2474 }
2475 net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2476 net->failure_threshold = stcb->asoc.def_net_failure;
2477 if (addr_inscope == 0) {
2478 #ifdef SCTP_DEBUG
2479 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2480 printf("Adding an address which is OUT OF SCOPE\n");
2481 }
2482 #endif /* SCTP_DEBUG */
2483 net->dest_state = (SCTP_ADDR_REACHABLE |
2484 SCTP_ADDR_OUT_OF_SCOPE);
2485 } else {
2486 if (from == 8)
2487 /* 8 is passed by connect_x */
2488 net->dest_state = SCTP_ADDR_REACHABLE;
2489 else
2490 net->dest_state = SCTP_ADDR_REACHABLE |
2491 SCTP_ADDR_UNCONFIRMED;
2492 }
2493 net->RTO = stcb->asoc.initial_rto;
2494 stcb->asoc.numnets++;
2495 net->ref_count = 1;
2496
2497 /* Init the timer structure */
2498 callout_init(&net->rxt_timer.timer, 0);
2499 callout_init(&net->pmtu_timer.timer, 0);
2500
2501 /* Now generate a route for this guy */
2502 /* KAME hack: embed scope zone ID */
2503 if (newaddr->sa_family == AF_INET6) {
2504 struct sockaddr_in6 *sin6;
2505 sin6 = (struct sockaddr_in6 *)newaddr;
2506 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2507 return (-1);
2508 }
2509 rt = rtcache_lookup(&net->ro, newaddr);
2510 if (rt) {
2511 net->mtu = rt->rt_ifp->if_mtu;
2512 if (from == 1) {
2513 stcb->asoc.smallest_mtu = net->mtu;
2514 }
2515 /* start things off to match mtu of interface please. */
2516 rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
2517 } else {
2518 net->mtu = stcb->asoc.smallest_mtu;
2519 }
2520 #ifdef SCTP_DEBUG
2521 printf("After lookup\n");
2522 #endif
2523 if (stcb->asoc.smallest_mtu > net->mtu) {
2524 stcb->asoc.smallest_mtu = net->mtu;
2525 }
2526 /* We take the max of the burst limit times a MTU or the INITIAL_CWND.
2527 * We then limit this to 4 MTU's of sending.
2528 */
2529 net->cwnd = uimin((net->mtu * 4), uimax((stcb->asoc.max_burst * net->mtu), SCTP_INITIAL_CWND));
2530
2531 /* we always get at LEAST 2 MTU's */
2532 if (net->cwnd < (2 * net->mtu)) {
2533 net->cwnd = 2 * net->mtu;
2534 }
2535
2536 net->ssthresh = stcb->asoc.peers_rwnd;
2537
2538 net->src_addr_selected = 0;
2539 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2540 if (rt == NULL) {
2541 /* Since we have no route put it at the back */
2542 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2543 } else if (netfirst == NULL) {
2544 /* We are the first one in the pool. */
2545 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2546 } else if ((netfirst_rt = rtcache_validate(&netfirst->ro)) == NULL) {
2547 /*
2548 * First one has NO route. Place this one ahead of the
2549 * first one.
2550 */
2551 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2552 } else if (rt->rt_ifp != netfirst_rt->rt_ifp) {
2553 rtcache_unref(netfirst_rt, &netfirst->ro);
2554 /*
2555 * This one has a different interface than the one at the
2556 * top of the list. Place it ahead.
2557 */
2558 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2559 } else {
2560 /*
2561 * Ok we have the same interface as the first one. Move
2562 * forward until we find either
2563 * a) one with a NULL route... insert ahead of that
2564 * b) one with a different ifp.. insert after that.
2565 * c) end of the list.. insert at the tail.
2566 */
2567 struct sctp_nets *netlook;
2568 struct rtentry *netlook_rt;
2569 do {
2570 netlook = TAILQ_NEXT(netfirst, sctp_next);
2571 if (netlook == NULL) {
2572 /* End of the list */
2573 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2574 sctp_next);
2575 break;
2576 } else if ((netlook_rt = rtcache_validate(&netlook->ro)) == NULL) {
2577 /* next one has NO route */
2578 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2579 break;
2580 } else if (netlook_rt->rt_ifp != rt->rt_ifp) {
2581 rtcache_unref(netlook_rt, &netlook->ro);
2582 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2583 net, sctp_next);
2584 break;
2585 }
2586 rtcache_unref(netlook_rt, &netlook->ro);
2587 /* Shift forward */
2588 netfirst = netlook;
2589 } while (netlook != NULL);
2590 rtcache_unref(netfirst_rt, &netfirst->ro);
2591 }
2592 /* got to have a primary set */
2593 if (stcb->asoc.primary_destination == 0) {
2594 stcb->asoc.primary_destination = net;
2595 } else if (!rtcache_validate(&stcb->asoc.primary_destination->ro)) {
2596 /* No route to current primary adopt new primary */
2597 stcb->asoc.primary_destination = net;
2598 }
2599 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2600 net);
2601
2602 return (0);
2603 }
2604
2605
2606 /*
2607 * allocate an association and add it to the endpoint. The caller must
2608 * be careful to add all additional addresses once they are know right
2609 * away or else the assoc will be may experience a blackout scenario.
2610 */
2611 struct sctp_tcb *
sctp_aloc_assoc(struct sctp_inpcb * inp,struct sockaddr * firstaddr,int for_a_init,int * error,uint32_t override_tag)2612 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2613 int for_a_init, int *error, uint32_t override_tag)
2614 {
2615 struct sctp_tcb *stcb;
2616 struct sctp_association *asoc;
2617 struct sctpasochead *head;
2618 uint16_t rport;
2619 int err;
2620
2621 /*
2622 * Assumption made here:
2623 * Caller has done a sctp_findassociation_ep_addr(ep, addr's);
2624 * to make sure the address does not exist already.
2625 */
2626 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2627 /* Hit max assoc, sorry no more */
2628 *error = ENOBUFS;
2629 return (NULL);
2630 }
2631 SCTP_INP_RLOCK(inp);
2632 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2633 /*
2634 * If its in the TCP pool, its NOT allowed to create an
2635 * association. The parent listener needs to call
2636 * sctp_aloc_assoc.. or the one-2-many socket. If a
2637 * peeled off, or connected one does this.. its an error.
2638 */
2639 SCTP_INP_RUNLOCK(inp);
2640 *error = EINVAL;
2641 return (NULL);
2642 }
2643
2644 #ifdef SCTP_DEBUG
2645 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2646 printf("Allocate an association for peer:");
2647 if (firstaddr)
2648 sctp_print_address(firstaddr);
2649 else
2650 printf("None\n");
2651 printf("Port:%d\n",
2652 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2653 }
2654 #endif /* SCTP_DEBUG */
2655 if (firstaddr->sa_family == AF_INET) {
2656 struct sockaddr_in *sin;
2657 sin = (struct sockaddr_in *)firstaddr;
2658 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2659 /* Invalid address */
2660 #ifdef SCTP_DEBUG
2661 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2662 printf("peer address invalid\n");
2663 }
2664 #endif
2665 SCTP_INP_RUNLOCK(inp);
2666 *error = EINVAL;
2667 return (NULL);
2668 }
2669 rport = sin->sin_port;
2670 } else if (firstaddr->sa_family == AF_INET6) {
2671 struct sockaddr_in6 *sin6;
2672 sin6 = (struct sockaddr_in6 *)firstaddr;
2673 if ((sin6->sin6_port == 0) ||
2674 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2675 /* Invalid address */
2676 #ifdef SCTP_DEBUG
2677 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2678 printf("peer address invalid\n");
2679 }
2680 #endif
2681 SCTP_INP_RUNLOCK(inp);
2682 *error = EINVAL;
2683 return (NULL);
2684 }
2685 rport = sin6->sin6_port;
2686 } else {
2687 /* not supported family type */
2688 #ifdef SCTP_DEBUG
2689 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2690 printf("BAD family %d\n", firstaddr->sa_family);
2691 }
2692 #endif
2693 SCTP_INP_RUNLOCK(inp);
2694 *error = EINVAL;
2695 return (NULL);
2696 }
2697 SCTP_INP_RUNLOCK(inp);
2698 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2699 /*
2700 * If you have not performed a bind, then we need to do
2701 * the ephemerial bind for you.
2702 */
2703 #ifdef SCTP_DEBUG
2704 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2705 printf("Doing implicit BIND\n");
2706 }
2707 #endif
2708
2709 if ((err = sctp_inpcb_bind(inp->sctp_socket,
2710 (struct sockaddr *)NULL, (struct lwp *)NULL))){
2711 /* bind error, probably perm */
2712 #ifdef SCTP_DEBUG
2713 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2714 printf("BIND FAILS ret:%d\n", err);
2715 }
2716 #endif
2717
2718 *error = err;
2719 return (NULL);
2720 }
2721 }
2722 stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
2723 if (stcb == NULL) {
2724 /* out of memory? */
2725 #ifdef SCTP_DEBUG
2726 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2727 printf("aloc_assoc: no assoc mem left, stcb=NULL\n");
2728 }
2729 #endif
2730 *error = ENOMEM;
2731 return (NULL);
2732 }
2733 sctppcbinfo.ipi_count_asoc++;
2734 sctppcbinfo.ipi_gencnt_asoc++;
2735
2736 memset(stcb, 0, sizeof(*stcb));
2737 asoc = &stcb->asoc;
2738 SCTP_TCB_LOCK_INIT(stcb);
2739 /* setup back pointers */
2740 #ifdef SCTP_DEBUG
2741 printf("Before back pointers\n");
2742 #endif
2743 stcb->sctp_ep = inp;
2744 stcb->sctp_socket = inp->sctp_socket;
2745 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
2746 /* failed */
2747 SCTP_TCB_LOCK_DESTROY (stcb);
2748 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2749 sctppcbinfo.ipi_count_asoc--;
2750 #ifdef SCTP_DEBUG
2751 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2752 printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
2753 }
2754 #endif
2755 *error = err;
2756 return (NULL);
2757 }
2758 /* and the port */
2759 stcb->rport = rport;
2760 SCTP_INP_INFO_WLOCK();
2761 SCTP_INP_WLOCK(inp);
2762 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2763 /* inpcb freed while alloc going on */
2764 SCTP_TCB_LOCK_DESTROY (stcb);
2765 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2766 SCTP_INP_WUNLOCK(inp);
2767 SCTP_INP_INFO_WUNLOCK();
2768 sctppcbinfo.ipi_count_asoc--;
2769 #ifdef SCTP_DEBUG
2770 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2771 printf("aloc_assoc: couldn't init asoc, out of mem?!\n");
2772 }
2773 #endif
2774 *error = EINVAL;
2775 return (NULL);
2776 }
2777 SCTP_TCB_LOCK(stcb);
2778
2779 /* now that my_vtag is set, add it to the hash */
2780 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2781 sctppcbinfo.hashasocmark)];
2782 /* put it in the bucket in the vtag hash of assoc's for the system */
2783 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2784 SCTP_INP_INFO_WUNLOCK();
2785
2786
2787 if ((err = sctp_add_remote_addr(stcb, firstaddr, 1, 1))) {
2788 /* failure.. memory error? */
2789 if (asoc->strmout)
2790 free(asoc->strmout, M_PCB);
2791 if (asoc->mapping_array)
2792 free(asoc->mapping_array, M_PCB);
2793
2794 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2795 sctppcbinfo.ipi_count_asoc--;
2796 #ifdef SCTP_DEBUG
2797 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2798 printf("aloc_assoc: couldn't add remote addr!\n");
2799 }
2800 #endif
2801 SCTP_TCB_LOCK_DESTROY (stcb);
2802 *error = ENOBUFS;
2803 return (NULL);
2804 }
2805 /* Init all the timers */
2806 callout_init(&asoc->hb_timer.timer, 0);
2807 callout_init(&asoc->dack_timer.timer, 0);
2808 callout_init(&asoc->asconf_timer.timer, 0);
2809 callout_init(&asoc->shut_guard_timer.timer, 0);
2810 callout_init(&asoc->autoclose_timer.timer, 0);
2811 callout_init(&asoc->delayed_event_timer.timer, 0);
2812 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
2813 /* now file the port under the hash as well */
2814 #ifdef SCTP_DEBUG
2815 printf("Before hashing %ld size %d\n",
2816 inp->sctp_hashmark, sctp_pcbtblsize);
2817 #endif
2818 if (inp->sctp_tcbhash != NULL) {
2819 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
2820 inp->sctp_hashmark)];
2821 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
2822 }
2823 #ifdef SCTP_DEBUG
2824 printf("After hashing\n");
2825 #endif
2826 SCTP_INP_WUNLOCK(inp);
2827 #ifdef SCTP_DEBUG
2828 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2829 printf("Association %p now allocated\n", stcb);
2830 }
2831 #endif
2832 return (stcb);
2833 }
2834
2835 void
sctp_free_remote_addr(struct sctp_nets * net)2836 sctp_free_remote_addr(struct sctp_nets *net)
2837 {
2838 if (net == NULL)
2839 return;
2840 net->ref_count--;
2841 if (net->ref_count <= 0) {
2842 /* stop timer if running */
2843 callout_stop(&net->rxt_timer.timer);
2844 callout_stop(&net->pmtu_timer.timer);
2845 callout_destroy(&net->rxt_timer.timer);
2846 callout_destroy(&net->pmtu_timer.timer);
2847 net->dest_state = SCTP_ADDR_NOT_REACHABLE;
2848 rtcache_free(&net->ro);
2849 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
2850 sctppcbinfo.ipi_count_raddr--;
2851 }
2852 }
2853
2854 /*
2855 * remove a remote endpoint address from an association, it
2856 * will fail if the address does not exist.
2857 */
2858 int
sctp_del_remote_addr(struct sctp_tcb * stcb,struct sockaddr * remaddr)2859 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
2860 {
2861 /*
2862 * Here we need to remove a remote address. This is quite simple, we
2863 * first find it in the list of address for the association
2864 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE on
2865 * that item.
2866 * Note we do not allow it to be removed if there are no other
2867 * addresses.
2868 */
2869 struct sctp_association *asoc;
2870 struct sctp_nets *net, *net_tmp;
2871 asoc = &stcb->asoc;
2872 if (asoc->numnets < 2) {
2873 /* Must have at LEAST two remote addresses */
2874 return (-1);
2875 }
2876 /* locate the address */
2877 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
2878 net_tmp = TAILQ_NEXT(net, sctp_next);
2879 if (rtcache_getdst(&net->ro)->sa_family != remaddr->sa_family) {
2880 continue;
2881 }
2882 if (sctp_cmpaddr(rtcache_getdst(&net->ro), remaddr)) {
2883 /* we found the guy */
2884 asoc->numnets--;
2885 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
2886 sctp_free_remote_addr(net);
2887 if (net == asoc->primary_destination) {
2888 /* Reset primary */
2889 struct sctp_nets *lnet;
2890 lnet = TAILQ_FIRST(&asoc->nets);
2891 /* Try to find a confirmed primary */
2892 asoc->primary_destination =
2893 sctp_find_alternate_net(stcb, lnet);
2894 }
2895 if (net == asoc->last_data_chunk_from) {
2896 /* Reset primary */
2897 asoc->last_data_chunk_from =
2898 TAILQ_FIRST(&asoc->nets);
2899 }
2900 if (net == asoc->last_control_chunk_from) {
2901 /* Reset primary */
2902 asoc->last_control_chunk_from =
2903 TAILQ_FIRST(&asoc->nets);
2904 }
2905 if (net == asoc->asconf_last_sent_to) {
2906 /* Reset primary */
2907 asoc->asconf_last_sent_to =
2908 TAILQ_FIRST(&asoc->nets);
2909 }
2910 return (0);
2911 }
2912 }
2913 /* not found. */
2914 return (-2);
2915 }
2916
2917
2918 static void
sctp_add_vtag_to_timewait(struct sctp_inpcb * inp,u_int32_t tag)2919 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, u_int32_t tag)
2920 {
2921 struct sctpvtaghead *chain;
2922 struct sctp_tagblock *twait_block;
2923 struct timeval now;
2924 int set, i;
2925 SCTP_GETTIME_TIMEVAL(&now);
2926 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
2927 set = 0;
2928 if (!LIST_EMPTY(chain)) {
2929 /* Block(s) present, lets find space, and expire on the fly */
2930 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
2931 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
2932 if ((twait_block->vtag_block[i].v_tag == 0) &&
2933 !set) {
2934 twait_block->vtag_block[0].tv_sec_at_expire =
2935 now.tv_sec + SCTP_TIME_WAIT;
2936 twait_block->vtag_block[0].v_tag = tag;
2937 set = 1;
2938 } else if ((twait_block->vtag_block[i].v_tag) &&
2939 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
2940 now.tv_sec)) {
2941 /* Audit expires this guy */
2942 twait_block->vtag_block[i].tv_sec_at_expire = 0;
2943 twait_block->vtag_block[i].v_tag = 0;
2944 if (set == 0) {
2945 /* Reuse it for my new tag */
2946 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
2947 twait_block->vtag_block[0].v_tag = tag;
2948 set = 1;
2949 }
2950 }
2951 }
2952 if (set) {
2953 /*
2954 * We only do up to the block where we can
2955 * place our tag for audits
2956 */
2957 break;
2958 }
2959 }
2960 }
2961 /* Need to add a new block to chain */
2962 if (!set) {
2963 twait_block = malloc(sizeof(struct sctp_tagblock), M_PCB, M_NOWAIT);
2964 if (twait_block == NULL) {
2965 return;
2966 }
2967 memset(twait_block, 0, sizeof(struct sctp_timewait));
2968 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
2969 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
2970 SCTP_TIME_WAIT;
2971 twait_block->vtag_block[0].v_tag = tag;
2972 }
2973 }
2974
2975
2976 static void
sctp_iterator_asoc_being_freed(struct sctp_inpcb * inp,struct sctp_tcb * stcb)2977 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2978 {
2979 struct sctp_iterator *it;
2980
2981
2982
2983 /* Unlock the tcb lock we do this so
2984 * we avoid a dead lock scenario where
2985 * the iterator is waiting on the TCB lock
2986 * and the TCB lock is waiting on the iterator
2987 * lock.
2988 */
2989 SCTP_ITERATOR_LOCK();
2990 SCTP_INP_INFO_WLOCK();
2991 SCTP_INP_WLOCK(inp);
2992 SCTP_TCB_LOCK(stcb);
2993
2994 it = stcb->asoc.stcb_starting_point_for_iterator;
2995 if (it == NULL) {
2996 return;
2997 }
2998 if (it->inp != stcb->sctp_ep) {
2999 /* hm, focused on the wrong one? */
3000 return;
3001 }
3002 if (it->stcb != stcb) {
3003 return;
3004 }
3005 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3006 if (it->stcb == NULL) {
3007 /* done with all asoc's in this assoc */
3008 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3009 it->inp = NULL;
3010 } else {
3011
3012 it->inp = LIST_NEXT(inp, sctp_list);
3013 }
3014 }
3015 }
3016
3017 /*
3018 * Free the association after un-hashing the remote port.
3019 */
3020 void
sctp_free_assoc(struct sctp_inpcb * inp,struct sctp_tcb * stcb)3021 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3022 {
3023 struct sctp_association *asoc;
3024 struct sctp_nets *net, *prev;
3025 struct sctp_laddr *laddr;
3026 struct sctp_tmit_chunk *chk;
3027 struct sctp_asconf_addr *aparam;
3028 struct sctp_socket_q_list *sq;
3029 int s;
3030
3031 /* first, lets purge the entry from the hash table. */
3032 s = splsoftnet();
3033 if (stcb->asoc.state == 0) {
3034 printf("Freeing already free association:%p - huh??\n",
3035 stcb);
3036 splx(s);
3037 return;
3038 }
3039 asoc = &stcb->asoc;
3040 asoc->state = 0;
3041 /* now clean up any other timers */
3042 callout_stop(&asoc->hb_timer.timer);
3043 callout_destroy(&asoc->hb_timer.timer);
3044 callout_stop(&asoc->dack_timer.timer);
3045 callout_destroy(&asoc->dack_timer.timer);
3046 callout_stop(&asoc->asconf_timer.timer);
3047 callout_destroy(&asoc->asconf_timer.timer);
3048 callout_stop(&asoc->shut_guard_timer.timer);
3049 callout_destroy(&asoc->shut_guard_timer.timer);
3050 callout_stop(&asoc->autoclose_timer.timer);
3051 callout_destroy(&asoc->autoclose_timer.timer);
3052 callout_stop(&asoc->delayed_event_timer.timer);
3053 callout_destroy(&asoc->delayed_event_timer.timer);
3054 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3055 callout_stop(&net->rxt_timer.timer);
3056 callout_stop(&net->pmtu_timer.timer);
3057 callout_destroy(&net->rxt_timer.timer);
3058 callout_destroy(&net->pmtu_timer.timer);
3059 }
3060
3061 /* Iterator asoc being freed we send an
3062 * unlocked TCB. It returns with INP_INFO
3063 * and INP write locked and the TCB locked
3064 * too and of course the iterator lock
3065 * in place as well..
3066 */
3067 SCTP_TCB_UNLOCK(stcb);
3068 sctp_iterator_asoc_being_freed(inp, stcb);
3069
3070 /* Null all of my entry's on the socket q */
3071 TAILQ_FOREACH(sq, &inp->sctp_queue_list, next_sq) {
3072 if (sq->tcb == stcb) {
3073 sq->tcb = NULL;
3074 }
3075 }
3076
3077 if (inp->sctp_tcb_at_block == (void *)stcb) {
3078 inp->error_on_block = ECONNRESET;
3079 }
3080
3081 if (inp->sctp_tcbhash) {
3082 LIST_REMOVE(stcb, sctp_tcbhash);
3083 }
3084 /* Now lets remove it from the list of ALL associations in the EP */
3085 LIST_REMOVE(stcb, sctp_tcblist);
3086 SCTP_INP_WUNLOCK(inp);
3087 SCTP_ITERATOR_UNLOCK();
3088
3089
3090 /* pull from vtag hash */
3091 LIST_REMOVE(stcb, sctp_asocs);
3092
3093 /*
3094 * Now before we can free the assoc, we must remove all of the
3095 * networks and any other allocated space.. i.e. add removes here
3096 * before the SCTP_ZONE_FREE() of the tasoc entry.
3097 */
3098
3099 sctp_add_vtag_to_timewait(inp, asoc->my_vtag);
3100 SCTP_INP_INFO_WUNLOCK();
3101 prev = NULL;
3102 while (!TAILQ_EMPTY(&asoc->nets)) {
3103 net = TAILQ_FIRST(&asoc->nets);
3104 /* pull from list */
3105 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3106 break;
3107 }
3108 prev = net;
3109 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3110 rtcache_free(&net->ro);
3111 /* free it */
3112 net->ref_count = 0;
3113 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_net, net);
3114 sctppcbinfo.ipi_count_raddr--;
3115 }
3116 /*
3117 * The chunk lists and such SHOULD be empty but we check them
3118 * just in case.
3119 */
3120 /* anything on the wheel needs to be removed */
3121 while (!TAILQ_EMPTY(&asoc->out_wheel)) {
3122 struct sctp_stream_out *outs;
3123 outs = TAILQ_FIRST(&asoc->out_wheel);
3124 TAILQ_REMOVE(&asoc->out_wheel, outs, next_spoke);
3125 /* now clean up any chunks here */
3126 chk = TAILQ_FIRST(&outs->outqueue);
3127 while (chk) {
3128 TAILQ_REMOVE(&outs->outqueue, chk, sctp_next);
3129 sctp_m_freem(chk->data);
3130 chk->data = NULL;
3131 chk->whoTo = NULL;
3132 chk->asoc = NULL;
3133 /* Free the chunk */
3134 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3135 sctppcbinfo.ipi_count_chunk--;
3136 sctppcbinfo.ipi_gencnt_chunk++;
3137 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3138 panic("Chunk count is negative");
3139 }
3140 chk = TAILQ_FIRST(&outs->outqueue);
3141 }
3142 outs = TAILQ_FIRST(&asoc->out_wheel);
3143 }
3144
3145 if (asoc->pending_reply) {
3146 free(asoc->pending_reply, M_PCB);
3147 asoc->pending_reply = NULL;
3148 }
3149 chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3150 while (chk) {
3151 TAILQ_REMOVE(&asoc->pending_reply_queue, chk, sctp_next);
3152 sctp_m_freem(chk->data);
3153 chk->data = NULL;
3154 chk->whoTo = NULL;
3155 chk->asoc = NULL;
3156 /* Free the chunk */
3157 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3158 sctppcbinfo.ipi_count_chunk--;
3159 sctppcbinfo.ipi_gencnt_chunk++;
3160 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3161 panic("Chunk count is negative");
3162 }
3163 chk = TAILQ_FIRST(&asoc->pending_reply_queue);
3164 }
3165 /* pending send queue SHOULD be empty */
3166 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3167 chk = TAILQ_FIRST(&asoc->send_queue);
3168 while (chk) {
3169 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3170 sctp_m_freem(chk->data);
3171 chk->data = NULL;
3172 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3173 sctppcbinfo.ipi_count_chunk--;
3174 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3175 panic("Chunk count is negative");
3176 }
3177 sctppcbinfo.ipi_gencnt_chunk++;
3178 chk = TAILQ_FIRST(&asoc->send_queue);
3179 }
3180 }
3181 /* sent queue SHOULD be empty */
3182 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3183 chk = TAILQ_FIRST(&asoc->sent_queue);
3184 while (chk) {
3185 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3186 sctp_m_freem(chk->data);
3187 chk->data = NULL;
3188 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3189 sctppcbinfo.ipi_count_chunk--;
3190 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3191 panic("Chunk count is negative");
3192 }
3193 sctppcbinfo.ipi_gencnt_chunk++;
3194 chk = TAILQ_FIRST(&asoc->sent_queue);
3195 }
3196 }
3197 /* control queue MAY not be empty */
3198 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3199 chk = TAILQ_FIRST(&asoc->control_send_queue);
3200 while (chk) {
3201 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3202 sctp_m_freem(chk->data);
3203 chk->data = NULL;
3204 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3205 sctppcbinfo.ipi_count_chunk--;
3206 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3207 panic("Chunk count is negative");
3208 }
3209 sctppcbinfo.ipi_gencnt_chunk++;
3210 chk = TAILQ_FIRST(&asoc->control_send_queue);
3211 }
3212 }
3213 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3214 chk = TAILQ_FIRST(&asoc->reasmqueue);
3215 while (chk) {
3216 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3217 sctp_m_freem(chk->data);
3218 chk->data = NULL;
3219 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3220 sctppcbinfo.ipi_count_chunk--;
3221 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3222 panic("Chunk count is negative");
3223 }
3224 sctppcbinfo.ipi_gencnt_chunk++;
3225 chk = TAILQ_FIRST(&asoc->reasmqueue);
3226 }
3227 }
3228 if (!TAILQ_EMPTY(&asoc->delivery_queue)) {
3229 chk = TAILQ_FIRST(&asoc->delivery_queue);
3230 while (chk) {
3231 TAILQ_REMOVE(&asoc->delivery_queue, chk, sctp_next);
3232 sctp_m_freem(chk->data);
3233 chk->data = NULL;
3234 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3235 sctppcbinfo.ipi_count_chunk--;
3236 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3237 panic("Chunk count is negative");
3238 }
3239 sctppcbinfo.ipi_gencnt_chunk++;
3240 chk = TAILQ_FIRST(&asoc->delivery_queue);
3241 }
3242 }
3243 if (asoc->mapping_array) {
3244 free(asoc->mapping_array, M_PCB);
3245 asoc->mapping_array = NULL;
3246 }
3247
3248 /* the stream outs */
3249 if (asoc->strmout) {
3250 free(asoc->strmout, M_PCB);
3251 asoc->strmout = NULL;
3252 }
3253 asoc->streamoutcnt = 0;
3254 if (asoc->strmin) {
3255 int i;
3256 for (i = 0; i < asoc->streamincnt; i++) {
3257 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3258 /* We have somethings on the streamin queue */
3259 chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3260 while (chk) {
3261 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3262 chk, sctp_next);
3263 sctp_m_freem(chk->data);
3264 chk->data = NULL;
3265 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk,
3266 chk);
3267 sctppcbinfo.ipi_count_chunk--;
3268 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
3269 panic("Chunk count is negative");
3270 }
3271 sctppcbinfo.ipi_gencnt_chunk++;
3272 chk = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3273 }
3274 }
3275 }
3276 free(asoc->strmin, M_PCB);
3277 asoc->strmin = NULL;
3278 }
3279 asoc->streamincnt = 0;
3280 /* local addresses, if any */
3281 while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3282 laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3283 LIST_REMOVE(laddr, sctp_nxt_addr);
3284 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3285 sctppcbinfo.ipi_count_laddr--;
3286 }
3287 /* pending asconf (address) parameters */
3288 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3289 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3290 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3291 free(aparam, M_PCB);
3292 }
3293 sctp_m_freem(asoc->last_asconf_ack_sent);
3294 asoc->last_asconf_ack_sent = NULL;
3295 /* Insert new items here :> */
3296
3297 /* Get rid of LOCK */
3298 SCTP_TCB_LOCK_DESTROY(stcb);
3299
3300 /* now clean up the tasoc itself */
3301 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3302 sctppcbinfo.ipi_count_asoc--;
3303 if ((inp->sctp_socket->so_snd.sb_cc) ||
3304 (inp->sctp_socket->so_snd.sb_mbcnt)) {
3305 /* This will happen when a abort is done */
3306 inp->sctp_socket->so_snd.sb_cc = 0;
3307 inp->sctp_socket->so_snd.sb_mbcnt = 0;
3308 }
3309 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3310 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
3311 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3312 /*
3313 * For the base fd, that is NOT in TCP pool we
3314 * turn off the connected flag. This allows
3315 * non-listening endpoints to connect/shutdown/
3316 * connect.
3317 */
3318 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3319 soisdisconnected(inp->sctp_socket);
3320 }
3321 /*
3322 * For those that are in the TCP pool we just leave
3323 * so it cannot be used. When they close the fd we
3324 * will free it all.
3325 */
3326 }
3327 }
3328 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3329 sctp_inpcb_free(inp, 0);
3330 }
3331 splx(s);
3332 }
3333
3334
3335 /*
3336 * determine if a destination is "reachable" based upon the addresses
3337 * bound to the current endpoint (e.g. only v4 or v6 currently bound)
3338 */
3339 /*
3340 * FIX: if we allow assoc-level bindx(), then this needs to be fixed
3341 * to use assoc level v4/v6 flags, as the assoc *may* not have the
3342 * same address types bound as its endpoint
3343 */
3344 int
sctp_destination_is_reachable(struct sctp_tcb * stcb,const struct sockaddr * destaddr)3345 sctp_destination_is_reachable(struct sctp_tcb *stcb, const struct sockaddr *destaddr)
3346 {
3347 struct sctp_inpcb *inp;
3348 int answer;
3349
3350 /* No locks here, the TCB, in all cases is already
3351 * locked and an assoc is up. There is either a
3352 * INP lock by the caller applied (in asconf case when
3353 * deleting an address) or NOT in the HB case, however
3354 * if HB then the INP increment is up and the INP
3355 * will not be removed (on top of the fact that
3356 * we have a TCB lock). So we only want to
3357 * read the sctp_flags, which is either bound-all
3358 * or not.. no protection needed since once an
3359 * assoc is up you can't be changing your binding.
3360 */
3361 inp = stcb->sctp_ep;
3362 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3363 /* if bound all, destination is not restricted */
3364 /* RRS: Question during lock work: Is this
3365 * correct? If you are bound-all you still
3366 * might need to obey the V4--V6 flags???
3367 * IMO this bound-all stuff needs to be removed!
3368 */
3369 return (1);
3370 }
3371 /* NOTE: all "scope" checks are done when local addresses are added */
3372 if (destaddr->sa_family == AF_INET6) {
3373 answer = inp->inp_vflag & INP_IPV6;
3374 } else if (destaddr->sa_family == AF_INET) {
3375 answer = inp->inp_vflag & INP_IPV4;
3376 } else {
3377 /* invalid family, so it's unreachable */
3378 answer = 0;
3379 }
3380 return (answer);
3381 }
3382
3383 /*
3384 * update the inp_vflags on an endpoint
3385 */
3386 static void
sctp_update_ep_vflag(struct sctp_inpcb * inp)3387 sctp_update_ep_vflag(struct sctp_inpcb *inp) {
3388 struct sctp_laddr *laddr;
3389
3390 /* first clear the flag */
3391 inp->inp_vflag = 0;
3392
3393 /* set the flag based on addresses on the ep list */
3394 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3395 if (laddr->ifa == NULL) {
3396 #ifdef SCTP_DEBUG
3397 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3398 printf("An ounce of prevention is worth a pound of cure\n");
3399 }
3400 #endif /* SCTP_DEBUG */
3401 continue;
3402 }
3403 if (laddr->ifa->ifa_addr) {
3404 continue;
3405 }
3406 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3407 inp->inp_vflag |= INP_IPV6;
3408 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3409 inp->inp_vflag |= INP_IPV4;
3410 }
3411 }
3412 }
3413
3414 /*
3415 * Add the address to the endpoint local address list
3416 * There is nothing to be done if we are bound to all addresses
3417 */
3418 int
sctp_add_local_addr_ep(struct sctp_inpcb * inp,struct ifaddr * ifa)3419 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3420 {
3421 struct sctp_laddr *laddr;
3422 int fnd, error;
3423 fnd = 0;
3424
3425 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3426 /* You are already bound to all. You have it already */
3427 return (0);
3428 }
3429 if (ifa->ifa_addr->sa_family == AF_INET6) {
3430 struct in6_ifaddr *ifa6;
3431 ifa6 = (struct in6_ifaddr *)ifa;
3432 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3433 IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3434 /* Can't bind a non-existent addr. */
3435 return (-1);
3436 }
3437 /* first, is it already present? */
3438 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3439 if (laddr->ifa == ifa) {
3440 fnd = 1;
3441 break;
3442 }
3443 }
3444
3445 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3446 /* Not bound to all */
3447 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3448 if (error != 0)
3449 return (error);
3450 inp->laddr_count++;
3451 /* update inp_vflag flags */
3452 if (ifa->ifa_addr->sa_family == AF_INET6) {
3453 inp->inp_vflag |= INP_IPV6;
3454 } else if (ifa->ifa_addr->sa_family == AF_INET) {
3455 inp->inp_vflag |= INP_IPV4;
3456 }
3457 }
3458 return (0);
3459 }
3460
3461
3462 /*
3463 * select a new (hopefully reachable) destination net
3464 * (should only be used when we deleted an ep addr that is the
3465 * only usable source address to reach the destination net)
3466 */
3467 static void
sctp_select_primary_destination(struct sctp_tcb * stcb)3468 sctp_select_primary_destination(struct sctp_tcb *stcb)
3469 {
3470 struct sctp_nets *net;
3471
3472 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3473 /* for now, we'll just pick the first reachable one we find */
3474 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3475 continue;
3476 if (sctp_destination_is_reachable(stcb,
3477 rtcache_getdst(&net->ro))) {
3478 /* found a reachable destination */
3479 stcb->asoc.primary_destination = net;
3480 }
3481 }
3482 /* I can't there from here! ...we're gonna die shortly... */
3483 }
3484
3485
3486 /*
3487 * Delete the address from the endpoint local address list
3488 * There is nothing to be done if we are bound to all addresses
3489 */
3490 int
sctp_del_local_addr_ep(struct sctp_inpcb * inp,struct ifaddr * ifa)3491 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3492 {
3493 struct sctp_laddr *laddr;
3494 int fnd;
3495 fnd = 0;
3496 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3497 /* You are already bound to all. You have it already */
3498 return (EINVAL);
3499 }
3500
3501 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3502 if (laddr->ifa == ifa) {
3503 fnd = 1;
3504 break;
3505 }
3506 }
3507 if (fnd && (inp->laddr_count < 2)) {
3508 /* can't delete unless there are at LEAST 2 addresses */
3509 return (-1);
3510 }
3511 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3512 /*
3513 * clean up any use of this address
3514 * go through our associations and clear any
3515 * last_used_address that match this one
3516 * for each assoc, see if a new primary_destination is needed
3517 */
3518 struct sctp_tcb *stcb;
3519
3520 /* clean up "next_addr_touse" */
3521 if (inp->next_addr_touse == laddr)
3522 /* delete this address */
3523 inp->next_addr_touse = NULL;
3524
3525 /* clean up "last_used_address" */
3526 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3527 if (stcb->asoc.last_used_address == laddr)
3528 /* delete this address */
3529 stcb->asoc.last_used_address = NULL;
3530 } /* for each tcb */
3531
3532 /* remove it from the ep list */
3533 sctp_remove_laddr(laddr);
3534 inp->laddr_count--;
3535 /* update inp_vflag flags */
3536 sctp_update_ep_vflag(inp);
3537 /* select a new primary destination if needed */
3538 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3539 /* presume caller (sctp_asconf.c) already owns INP lock */
3540 SCTP_TCB_LOCK(stcb);
3541 if (sctp_destination_is_reachable(stcb,
3542 rtcache_getdst(&stcb->asoc.primary_destination->ro)) == 0) {
3543 sctp_select_primary_destination(stcb);
3544 }
3545 SCTP_TCB_UNLOCK(stcb);
3546 } /* for each tcb */
3547 }
3548 return (0);
3549 }
3550
3551 /*
3552 * Add the addr to the TCB local address list
3553 * For the BOUNDALL or dynamic case, this is a "pending" address list
3554 * (eg. addresses waiting for an ASCONF-ACK response)
3555 * For the subset binding, static case, this is a "valid" address list
3556 */
3557 int
sctp_add_local_addr_assoc(struct sctp_tcb * stcb,struct ifaddr * ifa)3558 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3559 {
3560 struct sctp_laddr *laddr;
3561 int error;
3562
3563 /* Assumes TCP is locked.. and possiblye
3564 * the INP. May need to confirm/fix that if
3565 * we need it and is not the case.
3566 */
3567 if (ifa->ifa_addr->sa_family == AF_INET6) {
3568 struct in6_ifaddr *ifa6;
3569 ifa6 = (struct in6_ifaddr *)ifa;
3570 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3571 /* IN6_IFF_DEPRECATED | */
3572 IN6_IFF_ANYCAST |
3573 IN6_IFF_NOTREADY))
3574 /* Can't bind a non-existent addr. */
3575 return (-1);
3576 }
3577 /* does the address already exist? */
3578 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3579 if (laddr->ifa == ifa) {
3580 return (-1);
3581 }
3582 }
3583
3584 /* add to the list */
3585 error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
3586 if (error != 0)
3587 return (error);
3588 return (0);
3589 }
3590
3591 /*
3592 * insert an laddr entry with the given ifa for the desired list
3593 */
3594 int
sctp_insert_laddr(struct sctpladdr * list,struct ifaddr * ifa)3595 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa) {
3596 struct sctp_laddr *laddr;
3597 int s;
3598
3599 s = splsoftnet();
3600
3601 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
3602 if (laddr == NULL) {
3603 /* out of memory? */
3604 splx(s);
3605 return (EINVAL);
3606 }
3607 sctppcbinfo.ipi_count_laddr++;
3608 sctppcbinfo.ipi_gencnt_laddr++;
3609 memset(laddr, 0, sizeof(*laddr));
3610 laddr->ifa = ifa;
3611 /* insert it */
3612 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3613
3614 splx(s);
3615 return (0);
3616 }
3617
3618 /*
3619 * Remove an laddr entry from the local address list (on an assoc)
3620 */
3621 void
sctp_remove_laddr(struct sctp_laddr * laddr)3622 sctp_remove_laddr(struct sctp_laddr *laddr)
3623 {
3624 int s;
3625 s = splsoftnet();
3626 /* remove from the list */
3627 LIST_REMOVE(laddr, sctp_nxt_addr);
3628 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3629 sctppcbinfo.ipi_count_laddr--;
3630 sctppcbinfo.ipi_gencnt_laddr++;
3631
3632 splx(s);
3633 }
3634
3635 /*
3636 * Remove an address from the TCB local address list
3637 */
3638 int
sctp_del_local_addr_assoc(struct sctp_tcb * stcb,struct ifaddr * ifa)3639 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3640 {
3641 struct sctp_inpcb *inp;
3642 struct sctp_laddr *laddr;
3643
3644 /* This is called by asconf work. It is assumed that
3645 * a) The TCB is locked
3646 * and
3647 * b) The INP is locked.
3648 * This is true in as much as I can trace through
3649 * the entry asconf code where I did these locks.
3650 * Again, the ASCONF code is a bit different in
3651 * that it does lock the INP during its work often
3652 * times. This must be since we don't want other
3653 * proc's looking up things while what they are
3654 * looking up is changing :-D
3655 */
3656
3657 inp = stcb->sctp_ep;
3658 /* if subset bound and don't allow ASCONF's, can't delete last */
3659 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
3660 ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
3661 if (stcb->asoc.numnets < 2) {
3662 /* can't delete last address */
3663 return (-1);
3664 }
3665 }
3666
3667 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3668 /* remove the address if it exists */
3669 if (laddr->ifa == NULL)
3670 continue;
3671 if (laddr->ifa == ifa) {
3672 sctp_remove_laddr(laddr);
3673 return (0);
3674 }
3675 }
3676
3677 /* address not found! */
3678 return (-1);
3679 }
3680
3681 /*
3682 * Remove an address from the TCB local address list
3683 * lookup using a sockaddr addr
3684 */
3685 int
sctp_del_local_addr_assoc_sa(struct sctp_tcb * stcb,struct sockaddr * sa)3686 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
3687 {
3688 struct sctp_inpcb *inp;
3689 struct sctp_laddr *laddr;
3690 struct sockaddr *l_sa;
3691
3692 /*
3693 * This function I find does not seem to have a caller.
3694 * As such we NEED TO DELETE this code. If we do
3695 * find a caller, the caller MUST have locked the TCB
3696 * at the least and probably the INP as well.
3697 */
3698 inp = stcb->sctp_ep;
3699 /* if subset bound and don't allow ASCONF's, can't delete last */
3700 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
3701 ((inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
3702 if (stcb->asoc.numnets < 2) {
3703 /* can't delete last address */
3704 return (-1);
3705 }
3706 }
3707
3708 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3709 /* make sure the address exists */
3710 if (laddr->ifa == NULL)
3711 continue;
3712 if (laddr->ifa->ifa_addr == NULL)
3713 continue;
3714
3715 l_sa = laddr->ifa->ifa_addr;
3716 if (l_sa->sa_family == AF_INET6) {
3717 /* IPv6 address */
3718 struct sockaddr_in6 *sin1, *sin2;
3719 sin1 = (struct sockaddr_in6 *)l_sa;
3720 sin2 = (struct sockaddr_in6 *)sa;
3721 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
3722 sizeof(struct in6_addr)) == 0) {
3723 /* matched */
3724 sctp_remove_laddr(laddr);
3725 return (0);
3726 }
3727 } else if (l_sa->sa_family == AF_INET) {
3728 /* IPv4 address */
3729 struct sockaddr_in *sin1, *sin2;
3730 sin1 = (struct sockaddr_in *)l_sa;
3731 sin2 = (struct sockaddr_in *)sa;
3732 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
3733 /* matched */
3734 sctp_remove_laddr(laddr);
3735 return (0);
3736 }
3737 } else {
3738 /* invalid family */
3739 return (-1);
3740 }
3741 } /* end foreach */
3742 /* address not found! */
3743 return (-1);
3744 }
3745
3746 static char sctp_pcb_initialized = 0;
3747
3748 #if defined(__FreeBSD__) || defined(__APPLE__)
3749 /* sysctl */
3750 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
3751 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
3752
3753 #endif /* FreeBSD || APPLE */
3754
3755 #ifndef SCTP_TCBHASHSIZE
3756 #define SCTP_TCBHASHSIZE 1024
3757 #endif
3758
3759 #ifndef SCTP_CHUNKQUEUE_SCALE
3760 #define SCTP_CHUNKQUEUE_SCALE 10
3761 #endif
3762
3763 void
sctp_pcb_init(void)3764 sctp_pcb_init(void)
3765 {
3766 /*
3767 * SCTP initialization for the PCB structures
3768 * should be called by the sctp_init() function.
3769 */
3770 int i;
3771 int hashtblsize = SCTP_TCBHASHSIZE;
3772
3773 #if defined(__FreeBSD__) || defined(__APPLE__)
3774 int sctp_chunkscale = SCTP_CHUNKQUEUE_SCALE;
3775 #endif
3776
3777 if (sctp_pcb_initialized != 0) {
3778 /* error I was called twice */
3779 return;
3780 }
3781 sctp_pcb_initialized = 1;
3782
3783 /* Init all peg counts */
3784 for (i = 0; i < SCTP_NUMBER_OF_PEGS; i++) {
3785 sctp_pegs[i] = 0;
3786 }
3787
3788 /* init the empty list of (All) Endpoints */
3789 LIST_INIT(&sctppcbinfo.listhead);
3790
3791 /* init the iterator head */
3792 LIST_INIT(&sctppcbinfo.iteratorhead);
3793
3794 /* init the hash table of endpoints */
3795 #if defined(__FreeBSD__)
3796 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000
3797 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &hashtblsize);
3798 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
3799 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
3800 #else
3801 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE,
3802 hashtblsize);
3803 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE,
3804 sctp_pcbtblsize);
3805 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE,
3806 sctp_chunkscale);
3807 #endif
3808 #endif
3809
3810 sctppcbinfo.sctp_asochash = hashinit((hashtblsize * 31), HASH_LIST,
3811 M_WAITOK, &sctppcbinfo.hashasocmark);
3812
3813 sctppcbinfo.sctp_ephash = hashinit(hashtblsize, HASH_LIST,
3814 M_WAITOK, &sctppcbinfo.hashmark);
3815
3816 sctppcbinfo.sctp_tcpephash = hashinit(hashtblsize, HASH_LIST,
3817 M_WAITOK, &sctppcbinfo.hashtcpmark);
3818
3819 sctppcbinfo.hashtblsize = hashtblsize;
3820
3821 /* init the zones */
3822 /*
3823 * FIX ME: Should check for NULL returns, but if it does fail we
3824 * are doomed to panic anyways... add later maybe.
3825 */
3826 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
3827 sizeof(struct sctp_inpcb), maxsockets);
3828
3829 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
3830 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
3831
3832 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
3833 sizeof(struct sctp_laddr),
3834 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
3835
3836 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
3837 sizeof(struct sctp_nets),
3838 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
3839
3840 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
3841 sizeof(struct sctp_tmit_chunk),
3842 (sctp_max_number_of_assoc * sctp_scale_up_for_address *
3843 sctp_chunkscale));
3844
3845 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_sockq, "sctp_sockq",
3846 sizeof(struct sctp_socket_q_list),
3847 (sctp_max_number_of_assoc * sctp_scale_up_for_address *
3848 sctp_chunkscale));
3849
3850 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_hash, "sctp_hash",
3851 sizeof(void *) * sctp_pcbtblsize, maxsockets);
3852
3853 /* Master Lock INIT for info structure */
3854 SCTP_INP_INFO_LOCK_INIT();
3855 SCTP_ITERATOR_LOCK_INIT();
3856 /* not sure if we need all the counts */
3857 sctppcbinfo.ipi_count_ep = 0;
3858 sctppcbinfo.ipi_gencnt_ep = 0;
3859 /* assoc/tcb zone info */
3860 sctppcbinfo.ipi_count_asoc = 0;
3861 sctppcbinfo.ipi_gencnt_asoc = 0;
3862 /* local addrlist zone info */
3863 sctppcbinfo.ipi_count_laddr = 0;
3864 sctppcbinfo.ipi_gencnt_laddr = 0;
3865 /* remote addrlist zone info */
3866 sctppcbinfo.ipi_count_raddr = 0;
3867 sctppcbinfo.ipi_gencnt_raddr = 0;
3868 /* chunk info */
3869 sctppcbinfo.ipi_count_chunk = 0;
3870 sctppcbinfo.ipi_gencnt_chunk = 0;
3871
3872 /* socket queue zone info */
3873 sctppcbinfo.ipi_count_sockq = 0;
3874 sctppcbinfo.ipi_gencnt_sockq = 0;
3875
3876 /* mbuf tracker */
3877 sctppcbinfo.mbuf_track = 0;
3878 /* port stuff */
3879 sctppcbinfo.lastlow = anonportmin;
3880
3881 /* Init the TIMEWAIT list */
3882 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
3883 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
3884 }
3885
3886 #if defined(_SCTP_NEEDS_CALLOUT_) && !defined(__APPLE__)
3887 TAILQ_INIT(&sctppcbinfo.callqueue);
3888 #endif
3889
3890 }
3891
3892 int
sctp_load_addresses_from_init(struct sctp_tcb * stcb,struct mbuf * m,int iphlen,int offset,int limit,struct sctphdr * sh,struct sockaddr * altsa)3893 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
3894 int iphlen, int offset, int limit, struct sctphdr *sh,
3895 struct sockaddr *altsa)
3896 {
3897 /*
3898 * grub through the INIT pulling addresses and
3899 * loading them to the nets structure in the asoc.
3900 * The from address in the mbuf should also be loaded
3901 * (if it is not already). This routine can be called
3902 * with either INIT or INIT-ACK's as long as the
3903 * m points to the IP packet and the offset points
3904 * to the beginning of the parameters.
3905 */
3906 struct sctp_inpcb *inp, *l_inp;
3907 struct sctp_nets *net, *net_tmp;
3908 struct ip *iph;
3909 struct sctp_paramhdr *phdr, parm_buf;
3910 struct sctp_tcb *stcb_tmp;
3911 u_int16_t ptype, plen;
3912 struct sockaddr *sa;
3913 struct sockaddr_storage dest_store;
3914 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
3915 struct sockaddr_in sin;
3916 struct sockaddr_in6 sin6;
3917
3918 /* First get the destination address setup too. */
3919 memset(&sin, 0, sizeof(sin));
3920 memset(&sin6, 0, sizeof(sin6));
3921
3922 sin.sin_family = AF_INET;
3923 sin.sin_len = sizeof(sin);
3924 sin.sin_port = stcb->rport;
3925
3926 sin6.sin6_family = AF_INET6;
3927 sin6.sin6_len = sizeof(struct sockaddr_in6);
3928 sin6.sin6_port = stcb->rport;
3929 if (altsa == NULL) {
3930 iph = mtod(m, struct ip *);
3931 if (iph->ip_v == IPVERSION) {
3932 /* its IPv4 */
3933 struct sockaddr_in *sin_2;
3934 sin_2 = (struct sockaddr_in *)(local_sa);
3935 memset(sin_2, 0, sizeof(sin));
3936 sin_2->sin_family = AF_INET;
3937 sin_2->sin_len = sizeof(sin);
3938 sin_2->sin_port = sh->dest_port;
3939 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr ;
3940 sin.sin_addr = iph->ip_src;
3941 sa = (struct sockaddr *)&sin;
3942 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3943 /* its IPv6 */
3944 struct ip6_hdr *ip6;
3945 struct sockaddr_in6 *sin6_2;
3946
3947 ip6 = mtod(m, struct ip6_hdr *);
3948 sin6_2 = (struct sockaddr_in6 *)(local_sa);
3949 memset(sin6_2, 0, sizeof(sin6));
3950 sin6_2->sin6_family = AF_INET6;
3951 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
3952 sin6_2->sin6_port = sh->dest_port;
3953 sin6.sin6_addr = ip6->ip6_src;
3954 sa = (struct sockaddr *)&sin6;
3955 } else {
3956 sa = NULL;
3957 }
3958 } else {
3959 /*
3960 * For cookies we use the src address NOT from the packet
3961 * but from the original INIT
3962 */
3963 sa = altsa;
3964 }
3965 /* Turn off ECN until we get through all params */
3966 stcb->asoc.ecn_allowed = 0;
3967
3968 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3969 /* mark all addresses that we have currently on the list */
3970 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
3971 }
3972 /* does the source address already exist? if so skip it */
3973 l_inp = inp = stcb->sctp_ep;
3974 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
3975 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
3976 /* we must add the source address */
3977 /* no scope set here since we have a tcb already. */
3978 if ((sa->sa_family == AF_INET) &&
3979 (stcb->asoc.ipv4_addr_legal)) {
3980 if (sctp_add_remote_addr(stcb, sa, 0, 2)) {
3981 return (-1);
3982 }
3983 } else if ((sa->sa_family == AF_INET6) &&
3984 (stcb->asoc.ipv6_addr_legal)) {
3985 if (sctp_add_remote_addr(stcb, sa, 0, 3)) {
3986 return (-1);
3987 }
3988 }
3989 } else {
3990 if (net_tmp != NULL && stcb_tmp == stcb) {
3991 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
3992 } else if (stcb_tmp != stcb) {
3993 /* It belongs to another association? */
3994 return (-1);
3995 }
3996 }
3997 /* since a unlock occurred we must check the
3998 * TCB's state and the pcb's gone flags.
3999 */
4000 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4001 /* the user freed the ep */
4002 return (-1);
4003 }
4004 if (stcb->asoc.state == 0) {
4005 /* the assoc was freed? */
4006 return (-1);
4007 }
4008
4009 /* now we must go through each of the params. */
4010 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4011 while (phdr) {
4012 ptype = ntohs(phdr->param_type);
4013 plen = ntohs(phdr->param_length);
4014 /*printf("ptype => %d, plen => %d\n", ptype, plen);*/
4015 if (offset + plen > limit) {
4016 break;
4017 }
4018 if (plen == 0) {
4019 break;
4020 }
4021 if ((ptype == SCTP_IPV4_ADDRESS) &&
4022 (stcb->asoc.ipv4_addr_legal)) {
4023 struct sctp_ipv4addr_param *p4, p4_buf;
4024 /* ok get the v4 address and check/add */
4025 phdr = sctp_get_next_param(m, offset,
4026 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4027 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4028 phdr == NULL) {
4029 return (-1);
4030 }
4031 p4 = (struct sctp_ipv4addr_param *)phdr;
4032 sin.sin_addr.s_addr = p4->addr;
4033 sa = (struct sockaddr *)&sin;
4034 inp = stcb->sctp_ep;
4035 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4036 local_sa, stcb);
4037
4038 if ((stcb_tmp== NULL && inp == stcb->sctp_ep) ||
4039 inp == NULL) {
4040 /* we must add the source address */
4041 /* no scope set since we have a tcb already */
4042
4043 /* we must validate the state again here */
4044 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4045 /* the user freed the ep */
4046 return (-1);
4047 }
4048 if (stcb->asoc.state == 0) {
4049 /* the assoc was freed? */
4050 return (-1);
4051 }
4052 if (sctp_add_remote_addr(stcb, sa, 0, 4)) {
4053 return (-1);
4054 }
4055 } else if (stcb_tmp == stcb) {
4056 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4057 /* the user freed the ep */
4058 return (-1);
4059 }
4060 if (stcb->asoc.state == 0) {
4061 /* the assoc was freed? */
4062 return (-1);
4063 }
4064 if (net != NULL) {
4065 /* clear flag */
4066 net->dest_state &=
4067 ~SCTP_ADDR_NOT_IN_ASSOC;
4068 }
4069 } else {
4070 /* strange, address is in another assoc?
4071 * straighten out locks.
4072 */
4073 SCTP_TCB_UNLOCK(stcb_tmp);
4074 SCTP_INP_RLOCK(inp);
4075 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4076 /* the user freed the ep */
4077 SCTP_INP_RUNLOCK(l_inp);
4078 return (-1);
4079 }
4080 if (stcb->asoc.state == 0) {
4081 /* the assoc was freed? */
4082 SCTP_INP_RUNLOCK(l_inp);
4083 return (-1);
4084 }
4085 SCTP_TCB_LOCK(stcb);
4086 SCTP_INP_RUNLOCK(stcb->sctp_ep);
4087 return (-1);
4088 }
4089 } else if ((ptype == SCTP_IPV6_ADDRESS) &&
4090 (stcb->asoc.ipv6_addr_legal)) {
4091 /* ok get the v6 address and check/add */
4092 struct sctp_ipv6addr_param *p6, p6_buf;
4093 phdr = sctp_get_next_param(m, offset,
4094 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4095 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4096 phdr == NULL) {
4097 return (-1);
4098 }
4099 p6 = (struct sctp_ipv6addr_param *)phdr;
4100 memcpy((void *)&sin6.sin6_addr, p6->addr,
4101 sizeof(p6->addr));
4102 sa = (struct sockaddr *)&sin6;
4103 inp = stcb->sctp_ep;
4104 stcb_tmp= sctp_findassociation_ep_addr(&inp, sa, &net,
4105 local_sa, stcb);
4106 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4107 inp == NULL)) {
4108 /* we must validate the state again here */
4109 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4110 /* the user freed the ep */
4111 return (-1);
4112 }
4113 if (stcb->asoc.state == 0) {
4114 /* the assoc was freed? */
4115 return (-1);
4116 }
4117 /* we must add the address, no scope set */
4118 if (sctp_add_remote_addr(stcb, sa, 0, 5)) {
4119 return (-1);
4120 }
4121 } else if (stcb_tmp == stcb) {
4122 /* we must validate the state again here */
4123 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4124 /* the user freed the ep */
4125 return (-1);
4126 }
4127 if (stcb->asoc.state == 0) {
4128 /* the assoc was freed? */
4129 return (-1);
4130 }
4131 if (net != NULL) {
4132 /* clear flag */
4133 net->dest_state &=
4134 ~SCTP_ADDR_NOT_IN_ASSOC;
4135 }
4136 } else {
4137 /* strange, address is in another assoc?
4138 * straighten out locks.
4139 */
4140 SCTP_TCB_UNLOCK(stcb_tmp);
4141 SCTP_INP_RLOCK(l_inp);
4142 /* we must validate the state again here */
4143 if (l_inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4144 /* the user freed the ep */
4145 SCTP_INP_RUNLOCK(l_inp);
4146 return (-1);
4147 }
4148 if (stcb->asoc.state == 0) {
4149 /* the assoc was freed? */
4150 SCTP_INP_RUNLOCK(l_inp);
4151 return (-1);
4152 }
4153 SCTP_TCB_LOCK(stcb);
4154 SCTP_INP_RUNLOCK(l_inp);
4155 return (-1);
4156 }
4157 } else if (ptype == SCTP_ECN_CAPABLE) {
4158 stcb->asoc.ecn_allowed = 1;
4159 } else if (ptype == SCTP_ULP_ADAPTION) {
4160 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4161 struct sctp_adaption_layer_indication ai, *aip;
4162
4163 phdr = sctp_get_next_param(m, offset,
4164 (struct sctp_paramhdr *)&ai, sizeof(ai));
4165 aip = (struct sctp_adaption_layer_indication *)phdr;
4166 sctp_ulp_notify(SCTP_NOTIFY_ADAPTION_INDICATION,
4167 stcb, ntohl(aip->indication), NULL);
4168 }
4169 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4170 struct sctp_asconf_addr_param lstore, *fee;
4171 struct sctp_asconf_addrv4_param *fii;
4172 int lptype;
4173 struct sockaddr *lsa = NULL;
4174
4175 stcb->asoc.peer_supports_asconf = 1;
4176 stcb->asoc.peer_supports_asconf_setprim = 1;
4177 if (plen > sizeof(lstore)) {
4178 return (-1);
4179 }
4180 phdr = sctp_get_next_param(m, offset,
4181 (struct sctp_paramhdr *)&lstore, plen);
4182 if (phdr == NULL) {
4183 return (-1);
4184 }
4185
4186 fee = (struct sctp_asconf_addr_param *)phdr;
4187 lptype = ntohs(fee->addrp.ph.param_type);
4188 if (lptype == SCTP_IPV4_ADDRESS) {
4189 if (plen !=
4190 sizeof(struct sctp_asconf_addrv4_param)) {
4191 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4192 (int)sizeof(struct sctp_asconf_addrv4_param),
4193 plen);
4194 } else {
4195 fii = (struct sctp_asconf_addrv4_param *)fee;
4196 sin.sin_addr.s_addr = fii->addrp.addr;
4197 lsa = (struct sockaddr *)&sin;
4198 }
4199 } else if (lptype == SCTP_IPV6_ADDRESS) {
4200 if (plen !=
4201 sizeof(struct sctp_asconf_addr_param)) {
4202 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4203 (int)sizeof(struct sctp_asconf_addr_param),
4204 plen);
4205 } else {
4206 memcpy(sin6.sin6_addr.s6_addr,
4207 fee->addrp.addr,
4208 sizeof(fee->addrp.addr));
4209 lsa = (struct sockaddr *)&sin6;
4210 }
4211 }
4212 if (lsa) {
4213 sctp_set_primary_addr(stcb, sa, NULL);
4214 }
4215
4216 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4217 /* Peer supports pr-sctp */
4218 stcb->asoc.peer_supports_prsctp = 1;
4219 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4220 /* A supported extension chunk */
4221 struct sctp_supported_chunk_types_param *pr_supported;
4222 uint8_t local_store[128];
4223 int num_ent, i;
4224
4225 phdr = sctp_get_next_param(m, offset,
4226 (struct sctp_paramhdr *)&local_store, plen);
4227 if (phdr == NULL) {
4228 return (-1);
4229 }
4230 stcb->asoc.peer_supports_asconf = 0;
4231 stcb->asoc.peer_supports_asconf_setprim = 0;
4232 stcb->asoc.peer_supports_prsctp = 0;
4233 stcb->asoc.peer_supports_pktdrop = 0;
4234 stcb->asoc.peer_supports_strreset = 0;
4235 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4236 num_ent = plen - sizeof(struct sctp_paramhdr);
4237 for (i=0; i<num_ent; i++) {
4238 switch (pr_supported->chunk_types[i]) {
4239 case SCTP_ASCONF:
4240 stcb->asoc.peer_supports_asconf = 1;
4241 stcb->asoc.peer_supports_asconf_setprim = 1;
4242 break;
4243 case SCTP_ASCONF_ACK:
4244 stcb->asoc.peer_supports_asconf = 1;
4245 stcb->asoc.peer_supports_asconf_setprim = 1;
4246 break;
4247 case SCTP_FORWARD_CUM_TSN:
4248 stcb->asoc.peer_supports_prsctp = 1;
4249 break;
4250 case SCTP_PACKET_DROPPED:
4251 stcb->asoc.peer_supports_pktdrop = 1;
4252 break;
4253 case SCTP_STREAM_RESET:
4254 stcb->asoc.peer_supports_strreset = 1;
4255 break;
4256 default:
4257 /* one I have not learned yet */
4258 break;
4259
4260 }
4261 }
4262 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4263 /* Peer supports ECN-nonce */
4264 stcb->asoc.peer_supports_ecn_nonce = 1;
4265 stcb->asoc.ecn_nonce_allowed = 1;
4266 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4267 (ptype == SCTP_STATE_COOKIE) ||
4268 (ptype == SCTP_UNRECOG_PARAM) ||
4269 (ptype == SCTP_COOKIE_PRESERVE) ||
4270 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4271 (ptype == SCTP_ADD_IP_ADDRESS) ||
4272 (ptype == SCTP_DEL_IP_ADDRESS) ||
4273 (ptype == SCTP_ERROR_CAUSE_IND) ||
4274 (ptype == SCTP_SUCCESS_REPORT)) {
4275 /* don't care */;
4276 } else {
4277 if ((ptype & 0x8000) == 0x0000) {
4278 /* must stop processing the rest of
4279 * the param's. Any report bits were
4280 * handled with the call to sctp_arethere_unrecognized_parameters()
4281 * when the INIT or INIT-ACK was first seen.
4282 */
4283 break;
4284 }
4285 }
4286 offset += SCTP_SIZE32(plen);
4287 if (offset >= limit) {
4288 break;
4289 }
4290 phdr = sctp_get_next_param(m, offset, &parm_buf,
4291 sizeof(parm_buf));
4292 }
4293 /* Now check to see if we need to purge any addresses */
4294 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4295 net_tmp = TAILQ_NEXT(net, sctp_next);
4296 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4297 SCTP_ADDR_NOT_IN_ASSOC) {
4298 /* This address has been removed from the asoc */
4299 /* remove and free it */
4300 stcb->asoc.numnets--;
4301 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4302 sctp_free_remote_addr(net);
4303 if (net == stcb->asoc.primary_destination) {
4304 stcb->asoc.primary_destination = NULL;
4305 sctp_select_primary_destination(stcb);
4306 }
4307 }
4308 }
4309 return (0);
4310 }
4311
4312 int
sctp_set_primary_addr(struct sctp_tcb * stcb,struct sockaddr * sa,struct sctp_nets * net)4313 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4314 struct sctp_nets *net)
4315 {
4316 /* make sure the requested primary address exists in the assoc */
4317 if (net == NULL && sa)
4318 net = sctp_findnet(stcb, sa);
4319
4320 if (net == NULL) {
4321 /* didn't find the requested primary address! */
4322 return (-1);
4323 } else {
4324 /* set the primary address */
4325 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4326 /* Must be confirmed */
4327 return (-1);
4328 }
4329 stcb->asoc.primary_destination = net;
4330 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4331 return (0);
4332 }
4333 }
4334
4335
4336 int
sctp_is_vtag_good(struct sctp_inpcb * inp,u_int32_t tag,struct timeval * now)4337 sctp_is_vtag_good(struct sctp_inpcb *inp, u_int32_t tag, struct timeval *now)
4338 {
4339 /*
4340 * This function serves two purposes. It will see if a TAG can be
4341 * re-used and return 1 for yes it is ok and 0 for don't use that
4342 * tag.
4343 * A secondary function it will do is purge out old tags that can
4344 * be removed.
4345 */
4346 struct sctpasochead *head;
4347 struct sctpvtaghead *chain;
4348 struct sctp_tagblock *twait_block;
4349 struct sctp_tcb *stcb;
4350
4351 int i;
4352 SCTP_INP_INFO_WLOCK();
4353 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4354 /* First is the vtag in use ? */
4355
4356 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4357 sctppcbinfo.hashasocmark)];
4358 if (head == NULL) {
4359 SCTP_INP_INFO_WUNLOCK();
4360 return (0);
4361 }
4362 LIST_FOREACH(stcb, head, sctp_asocs) {
4363 if (stcb->asoc.my_vtag == tag) {
4364 /* We should remove this if and
4365 * return 0 always if we want vtags
4366 * unique across all endpoints. For
4367 * now within a endpoint is ok.
4368 */
4369 if (inp == stcb->sctp_ep) {
4370 /* bad tag, in use */
4371 SCTP_INP_INFO_WUNLOCK();
4372 return (0);
4373 }
4374 }
4375 }
4376 if (!LIST_EMPTY(chain)) {
4377 /*
4378 * Block(s) are present, lets see if we have this tag in
4379 * the list
4380 */
4381 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4382 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4383 if (twait_block->vtag_block[i].v_tag == 0) {
4384 /* not used */
4385 continue;
4386 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4387 now->tv_sec) {
4388 /* Audit expires this guy */
4389 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4390 twait_block->vtag_block[i].v_tag = 0;
4391 } else if (twait_block->vtag_block[i].v_tag ==
4392 tag) {
4393 /* Bad tag, sorry :< */
4394 SCTP_INP_INFO_WUNLOCK();
4395 return (0);
4396 }
4397 }
4398 }
4399 }
4400 /* Not found, ok to use the tag */
4401 SCTP_INP_INFO_WUNLOCK();
4402 return (1);
4403 }
4404
4405
4406 /*
4407 * Delete the address from the endpoint local address list
4408 * Lookup using a sockaddr address (ie. not an ifaddr)
4409 */
4410 int
sctp_del_local_addr_ep_sa(struct sctp_inpcb * inp,struct sockaddr * sa)4411 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4412 {
4413 struct sctp_laddr *laddr;
4414 struct sockaddr *l_sa;
4415 int found = 0;
4416 /* Here is another function I cannot find a
4417 * caller for. As such we SHOULD delete it
4418 * if we have no users. If we find a user that
4419 * user MUST have the INP locked.
4420 *
4421 */
4422
4423 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4424 /* You are already bound to all. You have it already */
4425 return (EINVAL);
4426 }
4427
4428 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4429 /* make sure the address exists */
4430 if (laddr->ifa == NULL)
4431 continue;
4432 if (laddr->ifa->ifa_addr == NULL)
4433 continue;
4434
4435 l_sa = laddr->ifa->ifa_addr;
4436 if (l_sa->sa_family == AF_INET6) {
4437 /* IPv6 address */
4438 struct sockaddr_in6 *sin1, *sin2;
4439 sin1 = (struct sockaddr_in6 *)l_sa;
4440 sin2 = (struct sockaddr_in6 *)sa;
4441 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4442 sizeof(struct in6_addr)) == 0) {
4443 /* matched */
4444 found = 1;
4445 break;
4446 }
4447 } else if (l_sa->sa_family == AF_INET) {
4448 /* IPv4 address */
4449 struct sockaddr_in *sin1, *sin2;
4450 sin1 = (struct sockaddr_in *)l_sa;
4451 sin2 = (struct sockaddr_in *)sa;
4452 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4453 /* matched */
4454 found = 1;
4455 break;
4456 }
4457 } else {
4458 /* invalid family */
4459 return (-1);
4460 }
4461 }
4462
4463 if (found && inp->laddr_count < 2) {
4464 /* can't delete unless there are at LEAST 2 addresses */
4465 return (-1);
4466 }
4467
4468 if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4469 /*
4470 * remove it from the ep list, this should NOT be
4471 * done until its really gone from the interface list and
4472 * we won't be receiving more of these. Probably right
4473 * away. If we do allow a removal of an address from
4474 * an association (sub-set bind) than this should NOT
4475 * be called until the all ASCONF come back from this
4476 * association.
4477 */
4478 sctp_remove_laddr(laddr);
4479 return (0);
4480 } else {
4481 return (-1);
4482 }
4483 }
4484
4485 static void
sctp_drain_mbufs(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4486 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4487 {
4488 /*
4489 * We must hunt this association for MBUF's past the cumack
4490 * (i.e. out of order data that we can renege on).
4491 */
4492 struct sctp_association *asoc;
4493 struct sctp_tmit_chunk *chk, *nchk;
4494 u_int32_t cumulative_tsn_p1, tsn;
4495 int cnt, strmat, gap;
4496 /* We look for anything larger than the cum-ack + 1 */
4497
4498 asoc = &stcb->asoc;
4499 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
4500 cnt = 0;
4501 /* First look in the re-assembly queue */
4502 chk = TAILQ_FIRST(&asoc->reasmqueue);
4503 while (chk) {
4504 /* Get the next one */
4505 nchk = TAILQ_NEXT(chk, sctp_next);
4506 if (compare_with_wrap(chk->rec.data.TSN_seq,
4507 cumulative_tsn_p1, MAX_TSN)) {
4508 /* Yep it is above cum-ack */
4509 cnt++;
4510 tsn = chk->rec.data.TSN_seq;
4511 if (tsn >= asoc->mapping_array_base_tsn) {
4512 gap = tsn - asoc->mapping_array_base_tsn;
4513 } else {
4514 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
4515 tsn + 1;
4516 }
4517 asoc->size_on_reasm_queue -= chk->send_size;
4518 asoc->cnt_on_reasm_queue--;
4519 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
4520 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4521 sctp_m_freem(chk->data);
4522 chk->data = NULL;
4523 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4524 sctppcbinfo.ipi_count_chunk--;
4525 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4526 panic("Chunk count is negative");
4527 }
4528 sctppcbinfo.ipi_gencnt_chunk++;
4529 }
4530 chk = nchk;
4531 }
4532 /* Ok that was fun, now we will drain all the inbound streams? */
4533 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
4534 chk = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
4535 while (chk) {
4536 nchk = TAILQ_NEXT(chk, sctp_next);
4537 if (compare_with_wrap(chk->rec.data.TSN_seq,
4538 cumulative_tsn_p1, MAX_TSN)) {
4539 /* Yep it is above cum-ack */
4540 cnt++;
4541 tsn = chk->rec.data.TSN_seq;
4542 if (tsn >= asoc->mapping_array_base_tsn) {
4543 gap = tsn -
4544 asoc->mapping_array_base_tsn;
4545 } else {
4546 gap = (MAX_TSN -
4547 asoc->mapping_array_base_tsn) +
4548 tsn + 1;
4549 }
4550 asoc->size_on_all_streams -= chk->send_size;
4551 asoc->cnt_on_all_streams--;
4552
4553 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
4554 gap);
4555 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
4556 chk, sctp_next);
4557 sctp_m_freem(chk->data);
4558 chk->data = NULL;
4559 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4560 sctppcbinfo.ipi_count_chunk--;
4561 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4562 panic("Chunk count is negative");
4563 }
4564 sctppcbinfo.ipi_gencnt_chunk++;
4565 }
4566 chk = nchk;
4567 }
4568 }
4569 /*
4570 * Question, should we go through the delivery queue?
4571 * The only reason things are on here is the app not reading OR a
4572 * p-d-api up. An attacker COULD send enough in to initiate the
4573 * PD-API and then send a bunch of stuff to other streams... these
4574 * would wind up on the delivery queue.. and then we would not get
4575 * to them. But in order to do this I then have to back-track and
4576 * un-deliver sequence numbers in streams.. el-yucko. I think for
4577 * now we will NOT look at the delivery queue and leave it to be
4578 * something to consider later. An alternative would be to abort
4579 * the P-D-API with a notification and then deliver the data....
4580 * Or another method might be to keep track of how many times the
4581 * situation occurs and if we see a possible attack underway just
4582 * abort the association.
4583 */
4584 #ifdef SCTP_DEBUG
4585 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
4586 if (cnt) {
4587 printf("Freed %d chunks from reneg harvest\n", cnt);
4588 }
4589 }
4590 #endif /* SCTP_DEBUG */
4591
4592 /*
4593 * Another issue, in un-setting the TSN's in the mapping array we
4594 * DID NOT adjust the highest_tsn marker. This will cause one of
4595 * two things to occur. It may cause us to do extra work in checking
4596 * for our mapping array movement. More importantly it may cause us
4597 * to SACK every datagram. This may not be a bad thing though since
4598 * we will recover once we get our cum-ack above and all this stuff
4599 * we dumped recovered.
4600 */
4601 }
4602
4603 void
sctp_drain(void)4604 sctp_drain(void)
4605 {
4606 /*
4607 * We must walk the PCB lists for ALL associations here. The system
4608 * is LOW on MBUF's and needs help. This is where reneging will
4609 * occur. We really hope this does NOT happen!
4610 */
4611 struct sctp_inpcb *inp;
4612 struct sctp_tcb *stcb;
4613
4614 SCTP_INP_INFO_RLOCK();
4615 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
4616 /* For each endpoint */
4617 SCTP_INP_RLOCK(inp);
4618 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4619 /* For each association */
4620 SCTP_TCB_LOCK(stcb);
4621 sctp_drain_mbufs(inp, stcb);
4622 SCTP_TCB_UNLOCK(stcb);
4623 }
4624 SCTP_INP_RUNLOCK(inp);
4625 }
4626 SCTP_INP_INFO_RUNLOCK();
4627 }
4628
4629 int
sctp_add_to_socket_q(struct sctp_inpcb * inp,struct sctp_tcb * stcb)4630 sctp_add_to_socket_q(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4631 {
4632 struct sctp_socket_q_list *sq;
4633
4634 /* write lock on INP assumed */
4635 if ((inp == NULL) || (stcb == NULL)) {
4636 /* I am paranoid */
4637 return (0);
4638 }
4639 sq = (struct sctp_socket_q_list *)SCTP_ZONE_GET(
4640 sctppcbinfo.ipi_zone_sockq);
4641 if (sq == NULL) {
4642 /* out of sq structs */
4643 return (0);
4644 }
4645 sctppcbinfo.ipi_count_sockq++;
4646 sctppcbinfo.ipi_gencnt_sockq++;
4647 if (stcb)
4648 stcb->asoc.cnt_msg_on_sb++;
4649 sq->tcb = stcb;
4650 TAILQ_INSERT_TAIL(&inp->sctp_queue_list, sq, next_sq);
4651 return (1);
4652 }
4653
4654
4655 struct sctp_tcb *
sctp_remove_from_socket_q(struct sctp_inpcb * inp)4656 sctp_remove_from_socket_q(struct sctp_inpcb *inp)
4657 {
4658 struct sctp_tcb *stcb = NULL;
4659 struct sctp_socket_q_list *sq;
4660
4661 /* W-Lock on INP assumed held */
4662 sq = TAILQ_FIRST(&inp->sctp_queue_list);
4663 if (sq == NULL)
4664 return (NULL);
4665
4666 stcb = sq->tcb;
4667 TAILQ_REMOVE(&inp->sctp_queue_list, sq, next_sq);
4668 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_sockq, sq);
4669 sctppcbinfo.ipi_count_sockq--;
4670 sctppcbinfo.ipi_gencnt_sockq++;
4671 if (stcb) {
4672 stcb->asoc.cnt_msg_on_sb--;
4673 }
4674 return (stcb);
4675 }
4676
4677 int
sctp_initiate_iterator(asoc_func af,uint32_t pcb_state,uint32_t asoc_state,void * argp,uint32_t argi,end_func ef,struct sctp_inpcb * s_inp)4678 sctp_initiate_iterator(asoc_func af, uint32_t pcb_state, uint32_t asoc_state,
4679 void *argp, uint32_t argi, end_func ef,
4680 struct sctp_inpcb *s_inp)
4681 {
4682 struct sctp_iterator *it=NULL;
4683 int s;
4684 if (af == NULL) {
4685 return (-1);
4686 }
4687 it = malloc(sizeof(struct sctp_iterator), M_PCB, M_WAITOK);
4688 if (it == NULL) {
4689 return (ENOMEM);
4690 }
4691 memset(it, 0, sizeof(*it));
4692 it->function_toapply = af;
4693 it->function_atend = ef;
4694 it->pointer = argp;
4695 it->val = argi;
4696 it->pcb_flags = pcb_state;
4697 it->asoc_state = asoc_state;
4698 if (s_inp) {
4699 it->inp = s_inp;
4700 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
4701 } else {
4702 SCTP_INP_INFO_RLOCK();
4703 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
4704 SCTP_INP_INFO_RUNLOCK();
4705 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
4706
4707 }
4708 /* Init the timer */
4709 callout_init(&it->tmr.timer, 0);
4710 /* add to the list of all iterators */
4711 SCTP_INP_INFO_WLOCK();
4712 LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
4713 SCTP_INP_INFO_WUNLOCK();
4714 s = splsoftnet();
4715 sctp_iterator_timer(it);
4716 splx(s);
4717 return (0);
4718 }
4719
4720
4721