sctp_output.c revision 1.29 1 1.29 andvar /* $NetBSD: sctp_output.c,v 1.29 2022/04/08 10:17:55 andvar Exp $ */
2 1.1 rjs /* $KAME: sctp_output.c,v 1.48 2005/06/16 18:29:24 jinmei Exp $ */
3 1.1 rjs
4 1.1 rjs /*
5 1.1 rjs * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
6 1.1 rjs * All rights reserved.
7 1.1 rjs *
8 1.1 rjs * Redistribution and use in source and binary forms, with or without
9 1.1 rjs * modification, are permitted provided that the following conditions
10 1.1 rjs * are met:
11 1.1 rjs * 1. Redistributions of source code must retain the above copyright
12 1.1 rjs * notice, this list of conditions and the following disclaimer.
13 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rjs * notice, this list of conditions and the following disclaimer in the
15 1.1 rjs * documentation and/or other materials provided with the distribution.
16 1.1 rjs * 3. Neither the name of the project nor the names of its contributors
17 1.1 rjs * may be used to endorse or promote products derived from this software
18 1.1 rjs * without specific prior written permission.
19 1.1 rjs *
20 1.1 rjs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 rjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 rjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 rjs * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 rjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 rjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 rjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 rjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 rjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 rjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 rjs * SUCH DAMAGE.
31 1.1 rjs */
32 1.1 rjs #include <sys/cdefs.h>
33 1.29 andvar __KERNEL_RCSID(0, "$NetBSD: sctp_output.c,v 1.29 2022/04/08 10:17:55 andvar Exp $");
34 1.1 rjs
35 1.1 rjs #ifdef _KERNEL_OPT
36 1.1 rjs #include "opt_ipsec.h"
37 1.1 rjs #include "opt_inet.h"
38 1.1 rjs #include "opt_sctp.h"
39 1.1 rjs #endif /* _KERNEL_OPT */
40 1.1 rjs
41 1.1 rjs #include <sys/param.h>
42 1.1 rjs #include <sys/systm.h>
43 1.1 rjs #include <sys/malloc.h>
44 1.1 rjs #include <sys/mbuf.h>
45 1.1 rjs #include <sys/domain.h>
46 1.1 rjs #include <sys/protosw.h>
47 1.1 rjs #include <sys/socket.h>
48 1.1 rjs #include <sys/socketvar.h>
49 1.1 rjs #include <sys/proc.h>
50 1.1 rjs #include <sys/kernel.h>
51 1.1 rjs #include <sys/sysctl.h>
52 1.1 rjs #include <sys/resourcevar.h>
53 1.1 rjs #include <sys/uio.h>
54 1.1 rjs #ifdef INET6
55 1.1 rjs #include <sys/domain.h>
56 1.1 rjs #endif
57 1.1 rjs
58 1.1 rjs #include <machine/limits.h>
59 1.1 rjs #include <machine/cpu.h>
60 1.1 rjs
61 1.1 rjs #include <net/if.h>
62 1.1 rjs #include <net/if_types.h>
63 1.1 rjs
64 1.1 rjs #include <net/route.h>
65 1.1 rjs
66 1.1 rjs #include <netinet/in.h>
67 1.1 rjs #include <netinet/in_systm.h>
68 1.1 rjs #include <netinet/ip.h>
69 1.1 rjs #include <netinet/in_pcb.h>
70 1.1 rjs #include <netinet/in_var.h>
71 1.1 rjs #include <netinet/ip_var.h>
72 1.1 rjs
73 1.1 rjs #ifdef INET6
74 1.1 rjs #include <netinet/ip6.h>
75 1.1 rjs #include <netinet6/ip6_var.h>
76 1.1 rjs #include <netinet6/scope6_var.h>
77 1.1 rjs #include <netinet6/nd6.h>
78 1.1 rjs
79 1.1 rjs #include <netinet6/in6_pcb.h>
80 1.1 rjs
81 1.1 rjs #include <netinet/icmp6.h>
82 1.1 rjs
83 1.1 rjs #endif /* INET6 */
84 1.1 rjs
85 1.1 rjs #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
86 1.1 rjs #ifndef in6pcb
87 1.1 rjs #define in6pcb inpcb
88 1.1 rjs #endif
89 1.1 rjs #endif
90 1.1 rjs
91 1.1 rjs #include <netinet/sctp_pcb.h>
92 1.1 rjs
93 1.1 rjs #ifdef IPSEC
94 1.3 rjs #include <netipsec/ipsec.h>
95 1.3 rjs #include <netipsec/key.h>
96 1.1 rjs #endif /* IPSEC */
97 1.1 rjs
98 1.1 rjs #include <netinet/sctp_var.h>
99 1.1 rjs #include <netinet/sctp_header.h>
100 1.1 rjs #include <netinet/sctputil.h>
101 1.1 rjs #include <netinet/sctp_pcb.h>
102 1.1 rjs #include <netinet/sctp_output.h>
103 1.1 rjs #include <netinet/sctp_uio.h>
104 1.1 rjs #include <netinet/sctputil.h>
105 1.1 rjs #include <netinet/sctp_hashdriver.h>
106 1.1 rjs #include <netinet/sctp_timer.h>
107 1.1 rjs #include <netinet/sctp_asconf.h>
108 1.1 rjs #include <netinet/sctp_indata.h>
109 1.1 rjs
110 1.1 rjs #ifdef SCTP_DEBUG
111 1.1 rjs extern uint32_t sctp_debug_on;
112 1.1 rjs #endif
113 1.1 rjs
114 1.1 rjs extern int sctp_peer_chunk_oh;
115 1.1 rjs
116 1.1 rjs static int
117 1.1 rjs sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
118 1.1 rjs {
119 1.1 rjs struct cmsghdr cmh;
120 1.1 rjs int tlen, at;
121 1.1 rjs
122 1.1 rjs tlen = control->m_len;
123 1.1 rjs at = 0;
124 1.1 rjs /*
125 1.1 rjs * Independent of how many mbufs, find the c_type inside the control
126 1.1 rjs * structure and copy out the data.
127 1.1 rjs */
128 1.1 rjs while (at < tlen) {
129 1.1 rjs if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
130 1.1 rjs /* not enough room for one more we are done. */
131 1.1 rjs return (0);
132 1.1 rjs }
133 1.1 rjs m_copydata(control, at, sizeof(cmh), (void *)&cmh);
134 1.1 rjs if ((cmh.cmsg_len + at) > tlen) {
135 1.1 rjs /*
136 1.1 rjs * this is real messed up since there is not enough
137 1.1 rjs * data here to cover the cmsg header. We are done.
138 1.1 rjs */
139 1.1 rjs return (0);
140 1.1 rjs }
141 1.1 rjs if ((cmh.cmsg_level == IPPROTO_SCTP) &&
142 1.1 rjs (c_type == cmh.cmsg_type)) {
143 1.1 rjs /* found the one we want, copy it out */
144 1.1 rjs at += CMSG_ALIGN(sizeof(struct cmsghdr));
145 1.1 rjs if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
146 1.1 rjs /*
147 1.1 rjs * space of cmsg_len after header not
148 1.1 rjs * big enough
149 1.1 rjs */
150 1.1 rjs return (0);
151 1.1 rjs }
152 1.1 rjs m_copydata(control, at, cpsize, data);
153 1.1 rjs return (1);
154 1.1 rjs } else {
155 1.1 rjs at += CMSG_ALIGN(cmh.cmsg_len);
156 1.1 rjs if (cmh.cmsg_len == 0) {
157 1.1 rjs break;
158 1.1 rjs }
159 1.1 rjs }
160 1.1 rjs }
161 1.1 rjs /* not found */
162 1.1 rjs return (0);
163 1.1 rjs }
164 1.1 rjs
165 1.1 rjs static struct mbuf *
166 1.1 rjs sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
167 1.1 rjs {
168 1.1 rjs struct sctp_paramhdr *parmh;
169 1.1 rjs struct mbuf *mret;
170 1.1 rjs int len;
171 1.1 rjs if (ifa->ifa_addr->sa_family == AF_INET) {
172 1.1 rjs len = sizeof(struct sctp_ipv4addr_param);
173 1.1 rjs } else if (ifa->ifa_addr->sa_family == AF_INET6) {
174 1.1 rjs len = sizeof(struct sctp_ipv6addr_param);
175 1.1 rjs } else {
176 1.1 rjs /* unknown type */
177 1.1 rjs return (m);
178 1.1 rjs }
179 1.1 rjs
180 1.1 rjs if (M_TRAILINGSPACE(m) >= len) {
181 1.1 rjs /* easy side we just drop it on the end */
182 1.1 rjs parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
183 1.1 rjs mret = m;
184 1.1 rjs } else {
185 1.1 rjs /* Need more space */
186 1.1 rjs mret = m;
187 1.1 rjs while (mret->m_next != NULL) {
188 1.1 rjs mret = mret->m_next;
189 1.1 rjs }
190 1.1 rjs MGET(mret->m_next, M_DONTWAIT, MT_DATA);
191 1.1 rjs if (mret->m_next == NULL) {
192 1.1 rjs /* We are hosed, can't add more addresses */
193 1.1 rjs return (m);
194 1.1 rjs }
195 1.1 rjs mret = mret->m_next;
196 1.1 rjs parmh = mtod(mret, struct sctp_paramhdr *);
197 1.1 rjs }
198 1.1 rjs /* now add the parameter */
199 1.1 rjs if (ifa->ifa_addr->sa_family == AF_INET) {
200 1.1 rjs struct sctp_ipv4addr_param *ipv4p;
201 1.1 rjs struct sockaddr_in *sin;
202 1.1 rjs sin = (struct sockaddr_in *)ifa->ifa_addr;
203 1.1 rjs ipv4p = (struct sctp_ipv4addr_param *)parmh;
204 1.1 rjs parmh->param_type = htons(SCTP_IPV4_ADDRESS);
205 1.1 rjs parmh->param_length = htons(len);
206 1.1 rjs ipv4p->addr = sin->sin_addr.s_addr;
207 1.1 rjs mret->m_len += len;
208 1.1 rjs } else if (ifa->ifa_addr->sa_family == AF_INET6) {
209 1.1 rjs struct sctp_ipv6addr_param *ipv6p;
210 1.1 rjs struct sockaddr_in6 *sin6;
211 1.1 rjs sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
212 1.1 rjs ipv6p = (struct sctp_ipv6addr_param *)parmh;
213 1.1 rjs parmh->param_type = htons(SCTP_IPV6_ADDRESS);
214 1.1 rjs parmh->param_length = htons(len);
215 1.1 rjs memcpy(ipv6p->addr, &sin6->sin6_addr,
216 1.1 rjs sizeof(ipv6p->addr));
217 1.1 rjs /* clear embedded scope in the address */
218 1.1 rjs in6_clearscope((struct in6_addr *)ipv6p->addr);
219 1.1 rjs mret->m_len += len;
220 1.1 rjs } else {
221 1.1 rjs return (m);
222 1.1 rjs }
223 1.1 rjs return (mret);
224 1.1 rjs }
225 1.1 rjs
226 1.1 rjs
227 1.1 rjs
228 1.1 rjs static struct mbuf *
229 1.1 rjs sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
230 1.1 rjs struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
231 1.1 rjs {
232 1.1 rjs struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
233 1.1 rjs struct sctp_state_cookie *stc;
234 1.1 rjs struct sctp_paramhdr *ph;
235 1.1 rjs uint8_t *signature;
236 1.1 rjs int sig_offset;
237 1.1 rjs uint16_t cookie_sz;
238 1.1 rjs
239 1.1 rjs mret = NULL;
240 1.1 rjs
241 1.1 rjs MGET(mret, M_DONTWAIT, MT_DATA);
242 1.1 rjs if (mret == NULL) {
243 1.1 rjs return (NULL);
244 1.1 rjs }
245 1.1 rjs copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
246 1.1 rjs if (copy_init == NULL) {
247 1.1 rjs sctp_m_freem(mret);
248 1.1 rjs return (NULL);
249 1.1 rjs }
250 1.1 rjs copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
251 1.1 rjs M_DONTWAIT);
252 1.1 rjs if (copy_initack == NULL) {
253 1.1 rjs sctp_m_freem(mret);
254 1.1 rjs sctp_m_freem(copy_init);
255 1.1 rjs return (NULL);
256 1.1 rjs }
257 1.1 rjs /* easy side we just drop it on the end */
258 1.1 rjs ph = mtod(mret, struct sctp_paramhdr *);
259 1.1 rjs mret->m_len = sizeof(struct sctp_state_cookie) +
260 1.1 rjs sizeof(struct sctp_paramhdr);
261 1.1 rjs stc = (struct sctp_state_cookie *)((vaddr_t)ph +
262 1.1 rjs sizeof(struct sctp_paramhdr));
263 1.1 rjs ph->param_type = htons(SCTP_STATE_COOKIE);
264 1.1 rjs ph->param_length = 0; /* fill in at the end */
265 1.1 rjs /* Fill in the stc cookie data */
266 1.1 rjs *stc = *stc_in;
267 1.1 rjs
268 1.1 rjs /* tack the INIT and then the INIT-ACK onto the chain */
269 1.1 rjs cookie_sz = 0;
270 1.1 rjs m_at = mret;
271 1.1 rjs for (m_at = mret; m_at; m_at = m_at->m_next) {
272 1.1 rjs cookie_sz += m_at->m_len;
273 1.1 rjs if (m_at->m_next == NULL) {
274 1.1 rjs m_at->m_next = copy_init;
275 1.1 rjs break;
276 1.1 rjs }
277 1.1 rjs }
278 1.1 rjs
279 1.1 rjs for (m_at = copy_init; m_at; m_at = m_at->m_next) {
280 1.1 rjs cookie_sz += m_at->m_len;
281 1.1 rjs if (m_at->m_next == NULL) {
282 1.1 rjs m_at->m_next = copy_initack;
283 1.1 rjs break;
284 1.1 rjs }
285 1.1 rjs }
286 1.1 rjs
287 1.1 rjs for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
288 1.1 rjs cookie_sz += m_at->m_len;
289 1.1 rjs if (m_at->m_next == NULL) {
290 1.1 rjs break;
291 1.1 rjs }
292 1.1 rjs }
293 1.1 rjs MGET(sig, M_DONTWAIT, MT_DATA);
294 1.1 rjs if (sig == NULL) {
295 1.1 rjs /* no space */
296 1.1 rjs sctp_m_freem(mret);
297 1.1 rjs sctp_m_freem(copy_init);
298 1.1 rjs sctp_m_freem(copy_initack);
299 1.1 rjs return (NULL);
300 1.1 rjs }
301 1.1 rjs sig->m_len = 0;
302 1.1 rjs m_at->m_next = sig;
303 1.1 rjs sig_offset = 0;
304 1.1 rjs signature = (uint8_t *)(mtod(sig, vaddr_t) + sig_offset);
305 1.1 rjs /* Time to sign the cookie */
306 1.1 rjs sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
307 1.1 rjs (int)(inp->sctp_ep.current_secret_number)],
308 1.1 rjs SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
309 1.1 rjs (uint8_t *)signature);
310 1.1 rjs sig->m_len += SCTP_SIGNATURE_SIZE;
311 1.1 rjs cookie_sz += SCTP_SIGNATURE_SIZE;
312 1.1 rjs
313 1.1 rjs ph->param_length = htons(cookie_sz);
314 1.1 rjs return (mret);
315 1.1 rjs }
316 1.1 rjs
317 1.1 rjs
318 1.1 rjs static struct sockaddr_in *
319 1.1 rjs sctp_is_v4_ifa_addr_prefered (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
320 1.1 rjs {
321 1.1 rjs struct sockaddr_in *sin;
322 1.1 rjs /*
323 1.1 rjs * Here we determine if its a prefered address. A
324 1.1 rjs * prefered address means it is the same scope or
325 1.1 rjs * higher scope then the destination.
326 1.1 rjs * L = loopback, P = private, G = global
327 1.1 rjs * -----------------------------------------
328 1.1 rjs * src | dest | result
329 1.1 rjs *-----------------------------------------
330 1.1 rjs * L | L | yes
331 1.1 rjs *-----------------------------------------
332 1.1 rjs * P | L | yes
333 1.1 rjs *-----------------------------------------
334 1.1 rjs * G | L | yes
335 1.1 rjs *-----------------------------------------
336 1.1 rjs * L | P | no
337 1.1 rjs *-----------------------------------------
338 1.1 rjs * P | P | yes
339 1.1 rjs *-----------------------------------------
340 1.1 rjs * G | P | no
341 1.1 rjs *-----------------------------------------
342 1.1 rjs * L | G | no
343 1.1 rjs *-----------------------------------------
344 1.1 rjs * P | G | no
345 1.1 rjs *-----------------------------------------
346 1.1 rjs * G | G | yes
347 1.1 rjs *-----------------------------------------
348 1.1 rjs */
349 1.1 rjs
350 1.1 rjs if (ifa->ifa_addr->sa_family != AF_INET) {
351 1.1 rjs /* forget non-v4 */
352 1.1 rjs return (NULL);
353 1.1 rjs }
354 1.1 rjs /* Ok the address may be ok */
355 1.1 rjs sin = (struct sockaddr_in *)ifa->ifa_addr;
356 1.1 rjs if (sin->sin_addr.s_addr == 0) {
357 1.1 rjs return (NULL);
358 1.1 rjs }
359 1.1 rjs *sin_local = *sin_loop = 0;
360 1.1 rjs if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
361 1.1 rjs (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
362 1.1 rjs *sin_loop = 1;
363 1.1 rjs *sin_local = 1;
364 1.1 rjs }
365 1.1 rjs if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
366 1.1 rjs *sin_local = 1;
367 1.1 rjs }
368 1.1 rjs if (!loopscope && *sin_loop) {
369 1.1 rjs /* Its a loopback address and we don't have loop scope */
370 1.1 rjs return (NULL);
371 1.1 rjs }
372 1.1 rjs if (!ipv4_scope && *sin_local) {
373 1.1 rjs /* Its a private address, and we don't have private address scope */
374 1.1 rjs return (NULL);
375 1.1 rjs }
376 1.1 rjs if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
377 1.1 rjs /* its a global src and a private dest */
378 1.1 rjs return (NULL);
379 1.1 rjs }
380 1.1 rjs /* its a prefered address */
381 1.1 rjs return (sin);
382 1.1 rjs }
383 1.1 rjs
384 1.1 rjs static struct sockaddr_in *
385 1.1 rjs sctp_is_v4_ifa_addr_acceptable (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
386 1.1 rjs {
387 1.1 rjs struct sockaddr_in *sin;
388 1.1 rjs /*
389 1.1 rjs * Here we determine if its a acceptable address. A
390 1.1 rjs * acceptable address means it is the same scope or
391 1.1 rjs * higher scope but we can allow for NAT which means
392 1.1 rjs * its ok to have a global dest and a private src.
393 1.1 rjs *
394 1.1 rjs * L = loopback, P = private, G = global
395 1.1 rjs * -----------------------------------------
396 1.1 rjs * src | dest | result
397 1.1 rjs *-----------------------------------------
398 1.1 rjs * L | L | yes
399 1.1 rjs *-----------------------------------------
400 1.1 rjs * P | L | yes
401 1.1 rjs *-----------------------------------------
402 1.1 rjs * G | L | yes
403 1.1 rjs *-----------------------------------------
404 1.1 rjs * L | P | no
405 1.1 rjs *-----------------------------------------
406 1.1 rjs * P | P | yes
407 1.1 rjs *-----------------------------------------
408 1.1 rjs * G | P | yes - probably this won't work.
409 1.1 rjs *-----------------------------------------
410 1.1 rjs * L | G | no
411 1.1 rjs *-----------------------------------------
412 1.1 rjs * P | G | yes
413 1.1 rjs *-----------------------------------------
414 1.1 rjs * G | G | yes
415 1.1 rjs *-----------------------------------------
416 1.1 rjs */
417 1.1 rjs
418 1.1 rjs if (ifa->ifa_addr->sa_family != AF_INET) {
419 1.1 rjs /* forget non-v4 */
420 1.1 rjs return (NULL);
421 1.1 rjs }
422 1.1 rjs /* Ok the address may be ok */
423 1.1 rjs sin = (struct sockaddr_in *)ifa->ifa_addr;
424 1.1 rjs if (sin->sin_addr.s_addr == 0) {
425 1.1 rjs return (NULL);
426 1.1 rjs }
427 1.1 rjs *sin_local = *sin_loop = 0;
428 1.1 rjs if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
429 1.1 rjs (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
430 1.1 rjs *sin_loop = 1;
431 1.1 rjs *sin_local = 1;
432 1.1 rjs }
433 1.1 rjs if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
434 1.1 rjs *sin_local = 1;
435 1.1 rjs }
436 1.1 rjs if (!loopscope && *sin_loop) {
437 1.1 rjs /* Its a loopback address and we don't have loop scope */
438 1.1 rjs return (NULL);
439 1.1 rjs }
440 1.1 rjs /* its an acceptable address */
441 1.1 rjs return (sin);
442 1.1 rjs }
443 1.1 rjs
444 1.1 rjs /*
445 1.1 rjs * This treats the address list on the ep as a restricted list
446 1.1 rjs * (negative list). If a the passed address is listed, then
447 1.1 rjs * the address is NOT allowed on the association.
448 1.1 rjs */
449 1.1 rjs int
450 1.1 rjs sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
451 1.1 rjs {
452 1.1 rjs struct sctp_laddr *laddr;
453 1.1 rjs #ifdef SCTP_DEBUG
454 1.1 rjs int cnt=0;
455 1.1 rjs #endif
456 1.1 rjs if (stcb == NULL) {
457 1.1 rjs /* There are no restrictions, no TCB :-) */
458 1.1 rjs return (0);
459 1.1 rjs }
460 1.1 rjs #ifdef SCTP_DEBUG
461 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
462 1.1 rjs cnt++;
463 1.1 rjs }
464 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
465 1.1 rjs printf("There are %d addresses on the restricted list\n", cnt);
466 1.1 rjs }
467 1.1 rjs cnt = 0;
468 1.1 rjs #endif
469 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
470 1.1 rjs if (laddr->ifa == NULL) {
471 1.1 rjs #ifdef SCTP_DEBUG
472 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
473 1.1 rjs printf("Help I have fallen and I can't get up!\n");
474 1.1 rjs }
475 1.1 rjs #endif
476 1.1 rjs continue;
477 1.1 rjs }
478 1.1 rjs #ifdef SCTP_DEBUG
479 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
480 1.1 rjs cnt++;
481 1.1 rjs printf("Restricted address[%d]:", cnt);
482 1.1 rjs sctp_print_address(laddr->ifa->ifa_addr);
483 1.1 rjs }
484 1.1 rjs #endif
485 1.1 rjs if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
486 1.1 rjs /* Yes it is on the list */
487 1.1 rjs return (1);
488 1.1 rjs }
489 1.1 rjs }
490 1.1 rjs return (0);
491 1.1 rjs }
492 1.1 rjs
493 1.1 rjs static int
494 1.1 rjs sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
495 1.1 rjs {
496 1.1 rjs struct sctp_laddr *laddr;
497 1.1 rjs
498 1.1 rjs if (ifa == NULL)
499 1.1 rjs return (0);
500 1.1 rjs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
501 1.1 rjs if (laddr->ifa == NULL) {
502 1.1 rjs #ifdef SCTP_DEBUG
503 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
504 1.1 rjs printf("Help I have fallen and I can't get up!\n");
505 1.1 rjs }
506 1.1 rjs #endif
507 1.1 rjs continue;
508 1.1 rjs }
509 1.1 rjs if (laddr->ifa->ifa_addr == NULL)
510 1.1 rjs continue;
511 1.1 rjs if (laddr->ifa == ifa)
512 1.1 rjs /* same pointer */
513 1.1 rjs return (1);
514 1.1 rjs if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
515 1.1 rjs /* skip non compatible address comparison */
516 1.1 rjs continue;
517 1.1 rjs }
518 1.1 rjs if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
519 1.1 rjs /* Yes it is restricted */
520 1.1 rjs return (1);
521 1.1 rjs }
522 1.1 rjs }
523 1.1 rjs return (0);
524 1.1 rjs }
525 1.1 rjs
526 1.1 rjs
527 1.1 rjs
528 1.1 rjs static struct in_addr
529 1.1 rjs sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
530 1.1 rjs struct rtentry *rt,
531 1.1 rjs uint8_t ipv4_scope,
532 1.1 rjs uint8_t loopscope)
533 1.1 rjs {
534 1.1 rjs struct in_addr ans;
535 1.1 rjs struct sctp_laddr *laddr;
536 1.1 rjs struct sockaddr_in *sin;
537 1.1 rjs struct ifnet *ifn;
538 1.1 rjs struct ifaddr *ifa;
539 1.1 rjs uint8_t sin_loop, sin_local;
540 1.1 rjs
541 1.1 rjs /* first question, is the ifn we will emit on
542 1.1 rjs * in our list, if so, we want that one.
543 1.1 rjs */
544 1.1 rjs ifn = rt->rt_ifp;
545 1.1 rjs if (ifn) {
546 1.1 rjs /* is a prefered one on the interface we route out? */
547 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
548 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
549 1.1 rjs if (sin == NULL)
550 1.1 rjs continue;
551 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
552 1.1 rjs return (sin->sin_addr);
553 1.1 rjs }
554 1.1 rjs }
555 1.1 rjs /* is an acceptable one on the interface we route out? */
556 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
557 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
558 1.1 rjs if (sin == NULL)
559 1.1 rjs continue;
560 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
561 1.1 rjs return (sin->sin_addr);
562 1.1 rjs }
563 1.1 rjs }
564 1.1 rjs }
565 1.1 rjs /* ok, what about a prefered address in the inp */
566 1.1 rjs for (laddr = LIST_FIRST(&inp->sctp_addr_list);
567 1.1 rjs laddr && (laddr != inp->next_addr_touse);
568 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
569 1.1 rjs if (laddr->ifa == NULL) {
570 1.1 rjs /* address has been removed */
571 1.1 rjs continue;
572 1.1 rjs }
573 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
574 1.1 rjs if (sin == NULL)
575 1.1 rjs continue;
576 1.1 rjs return (sin->sin_addr);
577 1.1 rjs
578 1.1 rjs }
579 1.1 rjs /* ok, what about an acceptable address in the inp */
580 1.1 rjs for (laddr = LIST_FIRST(&inp->sctp_addr_list);
581 1.1 rjs laddr && (laddr != inp->next_addr_touse);
582 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
583 1.1 rjs if (laddr->ifa == NULL) {
584 1.1 rjs /* address has been removed */
585 1.1 rjs continue;
586 1.1 rjs }
587 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
588 1.1 rjs if (sin == NULL)
589 1.1 rjs continue;
590 1.1 rjs return (sin->sin_addr);
591 1.1 rjs
592 1.1 rjs }
593 1.1 rjs
594 1.1 rjs /* no address bound can be a source for the destination we are in trouble */
595 1.1 rjs #ifdef SCTP_DEBUG
596 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
597 1.1 rjs printf("Src address selection for EP, no acceptable src address found for address\n");
598 1.1 rjs }
599 1.1 rjs #endif
600 1.1 rjs memset(&ans, 0, sizeof(ans));
601 1.1 rjs return (ans);
602 1.1 rjs }
603 1.1 rjs
604 1.1 rjs
605 1.1 rjs
606 1.1 rjs static struct in_addr
607 1.1 rjs sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
608 1.1 rjs struct sctp_tcb *stcb,
609 1.1 rjs struct sctp_nets *net,
610 1.1 rjs struct rtentry *rt,
611 1.1 rjs uint8_t ipv4_scope,
612 1.1 rjs uint8_t loopscope,
613 1.1 rjs int non_asoc_addr_ok)
614 1.1 rjs {
615 1.1 rjs /*
616 1.1 rjs * Here we have two cases, bound all asconf
617 1.1 rjs * allowed. bound all asconf not allowed.
618 1.1 rjs *
619 1.1 rjs */
620 1.1 rjs struct sctp_laddr *laddr, *starting_point;
621 1.1 rjs struct in_addr ans;
622 1.1 rjs struct ifnet *ifn;
623 1.1 rjs struct ifaddr *ifa;
624 1.1 rjs uint8_t sin_loop, sin_local, start_at_beginning=0;
625 1.1 rjs struct sockaddr_in *sin;
626 1.1 rjs
627 1.1 rjs /* first question, is the ifn we will emit on
628 1.1 rjs * in our list, if so, we want that one.
629 1.1 rjs */
630 1.1 rjs ifn = rt->rt_ifp;
631 1.1 rjs
632 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
633 1.1 rjs /*
634 1.1 rjs * Here we use the list of addresses on the endpoint. Then
635 1.1 rjs * the addresses listed on the "restricted" list is just that,
636 1.1 rjs * address that have not been added and can't be used (unless
637 1.1 rjs * the non_asoc_addr_ok is set).
638 1.1 rjs */
639 1.1 rjs #ifdef SCTP_DEBUG
640 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
641 1.1 rjs printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
642 1.1 rjs }
643 1.1 rjs #endif
644 1.1 rjs /* first question, is the ifn we will emit on
645 1.1 rjs * in our list, if so, we want that one.
646 1.1 rjs */
647 1.1 rjs if (ifn) {
648 1.1 rjs /* first try for an prefered address on the ep */
649 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
650 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
651 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
652 1.1 rjs if (sin == NULL)
653 1.1 rjs continue;
654 1.1 rjs if ((non_asoc_addr_ok == 0) &&
655 1.1 rjs (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
656 1.1 rjs /* on the no-no list */
657 1.1 rjs continue;
658 1.1 rjs }
659 1.1 rjs return (sin->sin_addr);
660 1.1 rjs }
661 1.1 rjs }
662 1.1 rjs /* next try for an acceptable address on the ep */
663 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
664 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
665 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
666 1.1 rjs if (sin == NULL)
667 1.1 rjs continue;
668 1.1 rjs if ((non_asoc_addr_ok == 0) &&
669 1.1 rjs (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
670 1.1 rjs /* on the no-no list */
671 1.1 rjs continue;
672 1.1 rjs }
673 1.1 rjs return (sin->sin_addr);
674 1.1 rjs }
675 1.1 rjs }
676 1.1 rjs
677 1.1 rjs }
678 1.1 rjs /* if we can't find one like that then we must
679 1.1 rjs * look at all addresses bound to pick one at
680 1.1 rjs * first prefereable then secondly acceptable.
681 1.1 rjs */
682 1.1 rjs starting_point = stcb->asoc.last_used_address;
683 1.1 rjs sctpv4_from_the_top:
684 1.1 rjs if (stcb->asoc.last_used_address == NULL) {
685 1.1 rjs start_at_beginning=1;
686 1.1 rjs stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
687 1.1 rjs }
688 1.1 rjs /* search beginning with the last used address */
689 1.1 rjs for (laddr = stcb->asoc.last_used_address; laddr;
690 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
691 1.1 rjs if (laddr->ifa == NULL) {
692 1.1 rjs /* address has been removed */
693 1.1 rjs continue;
694 1.1 rjs }
695 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
696 1.1 rjs if (sin == NULL)
697 1.1 rjs continue;
698 1.1 rjs if ((non_asoc_addr_ok == 0) &&
699 1.1 rjs (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
700 1.1 rjs /* on the no-no list */
701 1.1 rjs continue;
702 1.1 rjs }
703 1.1 rjs return (sin->sin_addr);
704 1.1 rjs
705 1.1 rjs }
706 1.1 rjs if (start_at_beginning == 0) {
707 1.1 rjs stcb->asoc.last_used_address = NULL;
708 1.1 rjs goto sctpv4_from_the_top;
709 1.1 rjs }
710 1.1 rjs /* now try for any higher scope than the destination */
711 1.1 rjs stcb->asoc.last_used_address = starting_point;
712 1.1 rjs start_at_beginning = 0;
713 1.1 rjs sctpv4_from_the_top2:
714 1.1 rjs if (stcb->asoc.last_used_address == NULL) {
715 1.1 rjs start_at_beginning=1;
716 1.1 rjs stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
717 1.1 rjs }
718 1.1 rjs /* search beginning with the last used address */
719 1.1 rjs for (laddr = stcb->asoc.last_used_address; laddr;
720 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
721 1.1 rjs if (laddr->ifa == NULL) {
722 1.1 rjs /* address has been removed */
723 1.1 rjs continue;
724 1.1 rjs }
725 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
726 1.1 rjs if (sin == NULL)
727 1.1 rjs continue;
728 1.1 rjs if ((non_asoc_addr_ok == 0) &&
729 1.1 rjs (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
730 1.1 rjs /* on the no-no list */
731 1.1 rjs continue;
732 1.1 rjs }
733 1.1 rjs return (sin->sin_addr);
734 1.1 rjs }
735 1.1 rjs if (start_at_beginning == 0) {
736 1.1 rjs stcb->asoc.last_used_address = NULL;
737 1.1 rjs goto sctpv4_from_the_top2;
738 1.1 rjs }
739 1.1 rjs } else {
740 1.1 rjs /*
741 1.1 rjs * Here we have an address list on the association, thats the
742 1.1 rjs * only valid source addresses that we can use.
743 1.1 rjs */
744 1.1 rjs #ifdef SCTP_DEBUG
745 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
746 1.29 andvar printf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
747 1.1 rjs }
748 1.1 rjs #endif
749 1.1 rjs /* First look at all addresses for one that is on
750 1.1 rjs * the interface we route out
751 1.1 rjs */
752 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
753 1.1 rjs sctp_nxt_addr) {
754 1.1 rjs if (laddr->ifa == NULL) {
755 1.1 rjs /* address has been removed */
756 1.1 rjs continue;
757 1.1 rjs }
758 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
759 1.1 rjs if (sin == NULL)
760 1.1 rjs continue;
761 1.1 rjs /* first question, is laddr->ifa an address associated with the emit interface */
762 1.1 rjs if (ifn) {
763 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
764 1.1 rjs if (laddr->ifa == ifa) {
765 1.1 rjs sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
766 1.1 rjs return (sin->sin_addr);
767 1.1 rjs }
768 1.1 rjs if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
769 1.1 rjs sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
770 1.1 rjs return (sin->sin_addr);
771 1.1 rjs }
772 1.1 rjs }
773 1.1 rjs }
774 1.1 rjs }
775 1.1 rjs /* what about an acceptable one on the interface? */
776 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
777 1.1 rjs sctp_nxt_addr) {
778 1.1 rjs if (laddr->ifa == NULL) {
779 1.1 rjs /* address has been removed */
780 1.1 rjs continue;
781 1.1 rjs }
782 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
783 1.1 rjs if (sin == NULL)
784 1.1 rjs continue;
785 1.1 rjs /* first question, is laddr->ifa an address associated with the emit interface */
786 1.1 rjs if (ifn) {
787 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
788 1.1 rjs if (laddr->ifa == ifa) {
789 1.1 rjs sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
790 1.1 rjs return (sin->sin_addr);
791 1.1 rjs }
792 1.1 rjs if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
793 1.1 rjs sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
794 1.1 rjs return (sin->sin_addr);
795 1.1 rjs }
796 1.1 rjs }
797 1.1 rjs }
798 1.1 rjs }
799 1.1 rjs /* ok, next one that is preferable in general */
800 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
801 1.1 rjs sctp_nxt_addr) {
802 1.1 rjs if (laddr->ifa == NULL) {
803 1.1 rjs /* address has been removed */
804 1.1 rjs continue;
805 1.1 rjs }
806 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
807 1.1 rjs if (sin == NULL)
808 1.1 rjs continue;
809 1.1 rjs return (sin->sin_addr);
810 1.1 rjs }
811 1.1 rjs
812 1.1 rjs /* last, what about one that is acceptable */
813 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
814 1.1 rjs sctp_nxt_addr) {
815 1.1 rjs if (laddr->ifa == NULL) {
816 1.1 rjs /* address has been removed */
817 1.1 rjs continue;
818 1.1 rjs }
819 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
820 1.1 rjs if (sin == NULL)
821 1.1 rjs continue;
822 1.1 rjs return (sin->sin_addr);
823 1.1 rjs }
824 1.1 rjs }
825 1.1 rjs memset(&ans, 0, sizeof(ans));
826 1.1 rjs return (ans);
827 1.1 rjs }
828 1.1 rjs
829 1.1 rjs static struct sockaddr_in *
830 1.1 rjs sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
831 1.1 rjs uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
832 1.1 rjs {
833 1.1 rjs struct ifaddr *ifa;
834 1.1 rjs struct sockaddr_in *sin;
835 1.1 rjs uint8_t sin_loop, sin_local;
836 1.1 rjs int num_eligible_addr = 0;
837 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
838 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
839 1.1 rjs if (sin == NULL)
840 1.1 rjs continue;
841 1.1 rjs if (stcb) {
842 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
843 1.1 rjs /* It is restricted for some reason.. probably
844 1.1 rjs * not yet added.
845 1.1 rjs */
846 1.1 rjs continue;
847 1.1 rjs }
848 1.1 rjs }
849 1.1 rjs if (cur_addr_num == num_eligible_addr) {
850 1.1 rjs return (sin);
851 1.1 rjs }
852 1.1 rjs }
853 1.1 rjs return (NULL);
854 1.1 rjs }
855 1.1 rjs
856 1.1 rjs
857 1.1 rjs static int
858 1.1 rjs sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
859 1.1 rjs uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
860 1.1 rjs {
861 1.1 rjs struct ifaddr *ifa;
862 1.1 rjs struct sockaddr_in *sin;
863 1.1 rjs int num_eligible_addr = 0;
864 1.1 rjs
865 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
866 1.1 rjs sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
867 1.1 rjs if (sin == NULL)
868 1.1 rjs continue;
869 1.1 rjs if (stcb) {
870 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
871 1.1 rjs /* It is restricted for some reason.. probably
872 1.1 rjs * not yet added.
873 1.1 rjs */
874 1.1 rjs continue;
875 1.1 rjs }
876 1.1 rjs }
877 1.1 rjs num_eligible_addr++;
878 1.1 rjs }
879 1.1 rjs return (num_eligible_addr);
880 1.1 rjs
881 1.1 rjs }
882 1.1 rjs
883 1.1 rjs static struct in_addr
884 1.1 rjs sctp_choose_v4_boundall(struct sctp_inpcb *inp,
885 1.1 rjs struct sctp_tcb *stcb,
886 1.1 rjs struct sctp_nets *net,
887 1.1 rjs struct rtentry *rt,
888 1.1 rjs uint8_t ipv4_scope,
889 1.1 rjs uint8_t loopscope,
890 1.1 rjs int non_asoc_addr_ok)
891 1.1 rjs {
892 1.1 rjs int cur_addr_num=0, num_prefered=0;
893 1.1 rjs uint8_t sin_loop, sin_local;
894 1.1 rjs struct ifnet *ifn;
895 1.1 rjs struct sockaddr_in *sin;
896 1.1 rjs struct in_addr ans;
897 1.1 rjs struct ifaddr *ifa;
898 1.4 ozaki int s;
899 1.1 rjs /*
900 1.1 rjs * For v4 we can use (in boundall) any address in the association. If
901 1.1 rjs * non_asoc_addr_ok is set we can use any address (at least in theory).
902 1.1 rjs * So we look for prefered addresses first. If we find one, we use it.
903 1.1 rjs * Otherwise we next try to get an address on the interface, which we
904 1.1 rjs * should be able to do (unless non_asoc_addr_ok is false and we are
905 1.1 rjs * routed out that way). In these cases where we can't use the address
906 1.1 rjs * of the interface we go through all the ifn's looking for an address
907 1.1 rjs * we can use and fill that in. Punting means we send back address
908 1.1 rjs * 0, which will probably cause problems actually since then IP will
909 1.1 rjs * fill in the address of the route ifn, which means we probably already
910 1.1 rjs * rejected it.. i.e. here comes an abort :-<.
911 1.1 rjs */
912 1.1 rjs ifn = rt->rt_ifp;
913 1.1 rjs if (net) {
914 1.1 rjs cur_addr_num = net->indx_of_eligible_next_to_use;
915 1.1 rjs }
916 1.1 rjs if (ifn == NULL) {
917 1.1 rjs goto bound_all_v4_plan_c;
918 1.1 rjs }
919 1.1 rjs num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
920 1.1 rjs #ifdef SCTP_DEBUG
921 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
922 1.1 rjs printf("Found %d prefered source addresses\n", num_prefered);
923 1.1 rjs }
924 1.1 rjs #endif
925 1.1 rjs if (num_prefered == 0) {
926 1.1 rjs /* no eligible addresses, we must use some other
927 1.1 rjs * interface address if we can find one.
928 1.1 rjs */
929 1.1 rjs goto bound_all_v4_plan_b;
930 1.1 rjs }
931 1.1 rjs /* Ok we have num_eligible_addr set with how many we can use,
932 1.1 rjs * this may vary from call to call due to addresses being deprecated etc..
933 1.1 rjs */
934 1.1 rjs if (cur_addr_num >= num_prefered) {
935 1.1 rjs cur_addr_num = 0;
936 1.1 rjs }
937 1.1 rjs /* select the nth address from the list (where cur_addr_num is the nth) and
938 1.1 rjs * 0 is the first one, 1 is the second one etc...
939 1.1 rjs */
940 1.1 rjs #ifdef SCTP_DEBUG
941 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
942 1.1 rjs printf("cur_addr_num:%d\n", cur_addr_num);
943 1.1 rjs }
944 1.1 rjs #endif
945 1.1 rjs sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
946 1.1 rjs ipv4_scope, cur_addr_num);
947 1.1 rjs
948 1.1 rjs /* if sin is NULL something changed??, plan_a now */
949 1.1 rjs if (sin) {
950 1.1 rjs return (sin->sin_addr);
951 1.1 rjs }
952 1.1 rjs
953 1.1 rjs /*
954 1.1 rjs * plan_b: Look at the interface that we emit on
955 1.1 rjs * and see if we can find an acceptable address.
956 1.1 rjs */
957 1.1 rjs bound_all_v4_plan_b:
958 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
959 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
960 1.1 rjs if (sin == NULL)
961 1.1 rjs continue;
962 1.1 rjs if (stcb) {
963 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
964 1.1 rjs /* It is restricted for some reason.. probably
965 1.1 rjs * not yet added.
966 1.1 rjs */
967 1.1 rjs continue;
968 1.1 rjs }
969 1.1 rjs }
970 1.1 rjs return (sin->sin_addr);
971 1.1 rjs }
972 1.1 rjs /*
973 1.1 rjs * plan_c: Look at all interfaces and find a prefered
974 1.1 rjs * address. If we reache here we are in trouble I think.
975 1.1 rjs */
976 1.1 rjs bound_all_v4_plan_c:
977 1.4 ozaki s = pserialize_read_enter();
978 1.4 ozaki IFNET_READER_FOREACH(ifn) {
979 1.2 mlelstv if (ifn == inp->next_ifn_touse)
980 1.2 mlelstv break;
981 1.1 rjs if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
982 1.1 rjs /* wrong base scope */
983 1.1 rjs continue;
984 1.1 rjs }
985 1.1 rjs if (ifn == rt->rt_ifp)
986 1.1 rjs /* already looked at this guy */
987 1.1 rjs continue;
988 1.1 rjs num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
989 1.1 rjs loopscope, ipv4_scope, &sin_loop, &sin_local);
990 1.1 rjs #ifdef SCTP_DEBUG
991 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
992 1.1 rjs printf("Found ifn:%p %d prefered source addresses\n", ifn, num_prefered);
993 1.1 rjs }
994 1.1 rjs #endif
995 1.1 rjs if (num_prefered == 0) {
996 1.1 rjs /*
997 1.1 rjs * None on this interface.
998 1.1 rjs */
999 1.1 rjs continue;
1000 1.1 rjs }
1001 1.1 rjs /* Ok we have num_eligible_addr set with how many we can use,
1002 1.1 rjs * this may vary from call to call due to addresses being deprecated etc..
1003 1.1 rjs */
1004 1.1 rjs if (cur_addr_num >= num_prefered) {
1005 1.1 rjs cur_addr_num = 0;
1006 1.1 rjs }
1007 1.1 rjs sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1008 1.1 rjs ipv4_scope, cur_addr_num);
1009 1.1 rjs if (sin == NULL)
1010 1.1 rjs continue;
1011 1.4 ozaki pserialize_read_exit(s);
1012 1.1 rjs return (sin->sin_addr);
1013 1.1 rjs
1014 1.1 rjs }
1015 1.4 ozaki pserialize_read_exit(s);
1016 1.1 rjs
1017 1.1 rjs /*
1018 1.1 rjs * plan_d: We are in deep trouble. No prefered address on
1019 1.1 rjs * any interface. And the emit interface does not
1020 1.1 rjs * even have an acceptable address. Take anything
1021 1.1 rjs * we can get! If this does not work we are
1022 1.1 rjs * probably going to emit a packet that will
1023 1.1 rjs * illicit an ABORT, falling through.
1024 1.1 rjs */
1025 1.1 rjs
1026 1.4 ozaki s = pserialize_read_enter();
1027 1.4 ozaki IFNET_READER_FOREACH(ifn) {
1028 1.2 mlelstv if (ifn == inp->next_ifn_touse)
1029 1.2 mlelstv break;
1030 1.1 rjs if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1031 1.1 rjs /* wrong base scope */
1032 1.1 rjs continue;
1033 1.1 rjs }
1034 1.1 rjs if (ifn == rt->rt_ifp)
1035 1.1 rjs /* already looked at this guy */
1036 1.1 rjs continue;
1037 1.1 rjs
1038 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1039 1.1 rjs sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
1040 1.1 rjs if (sin == NULL)
1041 1.1 rjs continue;
1042 1.1 rjs if (stcb) {
1043 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
1044 1.1 rjs /* It is restricted for some reason.. probably
1045 1.1 rjs * not yet added.
1046 1.1 rjs */
1047 1.1 rjs continue;
1048 1.1 rjs }
1049 1.1 rjs }
1050 1.4 ozaki pserialize_read_exit(s);
1051 1.1 rjs return (sin->sin_addr);
1052 1.1 rjs }
1053 1.1 rjs }
1054 1.4 ozaki pserialize_read_exit(s);
1055 1.1 rjs /*
1056 1.1 rjs * Ok we can find NO address to source from that is
1057 1.1 rjs * not on our negative list. It is either the special
1058 1.1 rjs * ASCONF case where we are sourceing from a intf that
1059 1.1 rjs * has been ifconfig'd to a different address (i.e.
1060 1.1 rjs * it holds a ADD/DEL/SET-PRIM and the proper lookup
1061 1.1 rjs * address. OR we are hosed, and this baby is going
1062 1.1 rjs * to abort the association.
1063 1.1 rjs */
1064 1.1 rjs if (non_asoc_addr_ok) {
1065 1.1 rjs return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
1066 1.1 rjs } else {
1067 1.1 rjs memset(&ans, 0, sizeof(ans));
1068 1.1 rjs return (ans);
1069 1.1 rjs }
1070 1.1 rjs }
1071 1.1 rjs
1072 1.1 rjs
1073 1.1 rjs
1074 1.1 rjs /* tcb may be NULL */
1075 1.1 rjs struct in_addr
1076 1.1 rjs sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
1077 1.1 rjs struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1078 1.1 rjs int non_asoc_addr_ok)
1079 1.1 rjs {
1080 1.1 rjs struct in_addr ans;
1081 1.1 rjs const struct sockaddr_in *to;
1082 1.1 rjs struct rtentry *rt;
1083 1.1 rjs uint8_t ipv4_scope, loopscope;
1084 1.8 ozaki
1085 1.1 rjs /*
1086 1.1 rjs * Rules:
1087 1.1 rjs * - Find the route if needed, cache if I can.
1088 1.1 rjs * - Look at interface address in route, Is it
1089 1.1 rjs * in the bound list. If so we have the best source.
1090 1.1 rjs * - If not we must rotate amongst the addresses.
1091 1.1 rjs *
1092 1.1 rjs * Cavets and issues
1093 1.1 rjs *
1094 1.1 rjs * Do we need to pay attention to scope. We can have
1095 1.1 rjs * a private address or a global address we are sourcing
1096 1.1 rjs * or sending to. So if we draw it out
1097 1.1 rjs * source * dest * result
1098 1.1 rjs * ------------------------------------------
1099 1.1 rjs * a Private * Global * NAT?
1100 1.1 rjs * ------------------------------------------
1101 1.1 rjs * b Private * Private * No problem
1102 1.1 rjs * ------------------------------------------
1103 1.1 rjs * c Global * Private * Huh, How will this work?
1104 1.1 rjs * ------------------------------------------
1105 1.1 rjs * d Global * Global * No Problem
1106 1.1 rjs * ------------------------------------------
1107 1.1 rjs *
1108 1.1 rjs * And then we add to that what happens if there are multiple
1109 1.1 rjs * addresses assigned to an interface. Remember the ifa on a
1110 1.1 rjs * ifn is a linked list of addresses. So one interface can
1111 1.1 rjs * have more than one IPv4 address. What happens if we
1112 1.1 rjs * have both a private and a global address? Do we then
1113 1.1 rjs * use context of destination to sort out which one is
1114 1.1 rjs * best? And what about NAT's sending P->G may get you
1115 1.1 rjs * a NAT translation, or should you select the G thats
1116 1.1 rjs * on the interface in preference.
1117 1.1 rjs *
1118 1.1 rjs * Decisions:
1119 1.1 rjs *
1120 1.1 rjs * - count the number of addresses on the interface.
1121 1.1 rjs * - if its one, no problem except case <c>. For <a>
1122 1.1 rjs * we will assume a NAT out there.
1123 1.1 rjs * - if there are more than one, then we need to worry
1124 1.1 rjs * about scope P or G. We should prefer G -> G and
1125 1.1 rjs * P -> P if possible. Then as a secondary fall back
1126 1.1 rjs * to mixed types G->P being a last ditch one.
1127 1.1 rjs * - The above all works for bound all, but bound
1128 1.1 rjs * specific we need to use the same concept but instead
1129 1.1 rjs * only consider the bound addresses. If the bound set
1130 1.1 rjs * is NOT assigned to the interface then we must use
1131 1.1 rjs * rotation amongst them.
1132 1.1 rjs *
1133 1.1 rjs * Notes: For v4, we can always punt and let ip_output
1134 1.1 rjs * decide by sending back a source of 0.0.0.0
1135 1.1 rjs */
1136 1.1 rjs
1137 1.1 rjs /*
1138 1.1 rjs * Need a route to cache.
1139 1.1 rjs *
1140 1.1 rjs */
1141 1.1 rjs rt = rtcache_validate(ro);
1142 1.1 rjs if (rt == NULL) {
1143 1.1 rjs /* No route to host .. punt */
1144 1.1 rjs memset(&ans, 0, sizeof(ans));
1145 1.1 rjs return (ans);
1146 1.1 rjs } else {
1147 1.1 rjs to = satocsin(rtcache_getdst(ro));
1148 1.1 rjs }
1149 1.1 rjs /* Setup our scopes */
1150 1.1 rjs if (stcb) {
1151 1.1 rjs ipv4_scope = stcb->asoc.ipv4_local_scope;
1152 1.1 rjs loopscope = stcb->asoc.loopback_scope;
1153 1.1 rjs } else {
1154 1.1 rjs /* Scope based on outbound address */
1155 1.1 rjs if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
1156 1.1 rjs ipv4_scope = 1;
1157 1.1 rjs loopscope = 0;
1158 1.1 rjs } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
1159 1.1 rjs ipv4_scope = 1;
1160 1.1 rjs loopscope = 1;
1161 1.1 rjs } else {
1162 1.1 rjs ipv4_scope = 0;
1163 1.1 rjs loopscope = 0;
1164 1.1 rjs }
1165 1.1 rjs }
1166 1.1 rjs #ifdef SCTP_DEBUG
1167 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1168 1.1 rjs printf("Scope setup loop:%d ipv4_scope:%d\n",
1169 1.1 rjs loopscope, ipv4_scope);
1170 1.1 rjs }
1171 1.1 rjs #endif
1172 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1173 1.1 rjs /*
1174 1.1 rjs * When bound to all if the address list is set
1175 1.1 rjs * it is a negative list. Addresses being added
1176 1.1 rjs * by asconf.
1177 1.1 rjs */
1178 1.8 ozaki ans = sctp_choose_v4_boundall(inp, stcb, net, rt,
1179 1.8 ozaki ipv4_scope, loopscope, non_asoc_addr_ok);
1180 1.8 ozaki goto out;
1181 1.1 rjs }
1182 1.1 rjs /*
1183 1.1 rjs * Three possiblities here:
1184 1.1 rjs *
1185 1.1 rjs * a) stcb is NULL, which means we operate only from
1186 1.1 rjs * the list of addresses (ifa's) bound to the assoc and
1187 1.1 rjs * we care not about the list.
1188 1.1 rjs * b) stcb is NOT-NULL, which means we have an assoc structure and
1189 1.1 rjs * auto-asconf is on. This means that the list of addresses is
1190 1.1 rjs * a NOT list. We use the list from the inp, but any listed address
1191 1.1 rjs * in our list is NOT yet added. However if the non_asoc_addr_ok is
1192 1.1 rjs * set we CAN use an address NOT available (i.e. being added). Its
1193 1.1 rjs * a negative list.
1194 1.1 rjs * c) stcb is NOT-NULL, which means we have an assoc structure and
1195 1.1 rjs * auto-asconf is off. This means that the list of addresses is
1196 1.1 rjs * the ONLY addresses I can use.. its positive.
1197 1.1 rjs *
1198 1.1 rjs * Note we collapse b & c into the same function just like in
1199 1.1 rjs * the v6 address selection.
1200 1.1 rjs */
1201 1.1 rjs if (stcb) {
1202 1.8 ozaki ans = sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
1203 1.8 ozaki rt, ipv4_scope, loopscope, non_asoc_addr_ok);
1204 1.8 ozaki goto out;
1205 1.1 rjs } else {
1206 1.8 ozaki ans = sctp_choose_v4_boundspecific_inp(inp, rt,
1207 1.8 ozaki ipv4_scope, loopscope);
1208 1.8 ozaki goto out;
1209 1.1 rjs }
1210 1.1 rjs /* this should not be reached */
1211 1.1 rjs memset(&ans, 0, sizeof(ans));
1212 1.8 ozaki out:
1213 1.8 ozaki rtcache_unref(rt, ro);
1214 1.8 ozaki return ans;
1215 1.1 rjs }
1216 1.1 rjs
1217 1.1 rjs
1218 1.1 rjs
1219 1.1 rjs static struct sockaddr_in6 *
1220 1.1 rjs sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
1221 1.1 rjs {
1222 1.1 rjs struct in6_ifaddr *ifa6;
1223 1.1 rjs struct sockaddr_in6 *sin6;
1224 1.1 rjs
1225 1.1 rjs if (ifa->ifa_addr->sa_family != AF_INET6) {
1226 1.1 rjs /* forget non-v6 */
1227 1.1 rjs return (NULL);
1228 1.1 rjs }
1229 1.1 rjs ifa6 = (struct in6_ifaddr *)ifa;
1230 1.1 rjs /* ok to use deprecated addresses? */
1231 1.1 rjs if (!ip6_use_deprecated) {
1232 1.1 rjs if (IFA6_IS_DEPRECATED(ifa6)) {
1233 1.1 rjs /* can't use this type */
1234 1.1 rjs return (NULL);
1235 1.1 rjs }
1236 1.1 rjs }
1237 1.1 rjs /* are we ok, with the current state of this address? */
1238 1.1 rjs if (ifa6->ia6_flags &
1239 1.1 rjs (IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
1240 1.1 rjs /* Can't use these types */
1241 1.1 rjs return (NULL);
1242 1.1 rjs }
1243 1.1 rjs /* Ok the address may be ok */
1244 1.1 rjs sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1245 1.1 rjs *sin_local = *sin_loop = 0;
1246 1.1 rjs if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
1247 1.1 rjs (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
1248 1.1 rjs *sin_loop = 1;
1249 1.1 rjs }
1250 1.1 rjs if (!loopscope && *sin_loop) {
1251 1.1 rjs /* Its a loopback address and we don't have loop scope */
1252 1.1 rjs return (NULL);
1253 1.1 rjs }
1254 1.1 rjs if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1255 1.1 rjs /* we skip unspecifed addresses */
1256 1.1 rjs return (NULL);
1257 1.1 rjs }
1258 1.1 rjs
1259 1.1 rjs if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1260 1.1 rjs *sin_local = 1;
1261 1.1 rjs }
1262 1.1 rjs if (!loc_scope && *sin_local) {
1263 1.1 rjs /* Its a link local address, and we don't have link local scope */
1264 1.1 rjs return (NULL);
1265 1.1 rjs }
1266 1.1 rjs return (sin6);
1267 1.1 rjs }
1268 1.1 rjs
1269 1.1 rjs
1270 1.1 rjs static struct sockaddr_in6 *
1271 1.1 rjs sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
1272 1.1 rjs struct sctp_tcb *stcb,
1273 1.1 rjs struct sctp_nets *net,
1274 1.1 rjs struct rtentry *rt,
1275 1.1 rjs uint8_t loc_scope,
1276 1.1 rjs uint8_t loopscope,
1277 1.1 rjs int non_asoc_addr_ok)
1278 1.1 rjs {
1279 1.1 rjs /*
1280 1.1 rjs * Each endpoint has a list of local addresses associated
1281 1.1 rjs * with it. The address list is either a "negative list" i.e.
1282 1.1 rjs * those addresses that are NOT allowed to be used as a source OR
1283 1.29 andvar * a "positive list" i.e. those addresses that CAN be used.
1284 1.1 rjs *
1285 1.1 rjs * Its a negative list if asconf is allowed. What we do
1286 1.1 rjs * in this case is use the ep address list BUT we have
1287 1.1 rjs * to cross check it against the negative list.
1288 1.1 rjs *
1289 1.1 rjs * In the case where NO asconf is allowed, we have just
1290 1.1 rjs * a straight association level list that we must use to
1291 1.1 rjs * find a source address.
1292 1.1 rjs */
1293 1.1 rjs struct sctp_laddr *laddr, *starting_point;
1294 1.1 rjs struct sockaddr_in6 *sin6;
1295 1.1 rjs int sin_loop, sin_local;
1296 1.1 rjs int start_at_beginning=0;
1297 1.1 rjs struct ifnet *ifn;
1298 1.1 rjs struct ifaddr *ifa;
1299 1.1 rjs
1300 1.1 rjs ifn = rt->rt_ifp;
1301 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1302 1.1 rjs #ifdef SCTP_DEBUG
1303 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1304 1.1 rjs printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
1305 1.1 rjs }
1306 1.1 rjs #endif
1307 1.1 rjs /* first question, is the ifn we will emit on
1308 1.1 rjs * in our list, if so, we want that one.
1309 1.1 rjs */
1310 1.1 rjs if (ifn) {
1311 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1312 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
1313 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1314 1.1 rjs if (sin6 == NULL)
1315 1.1 rjs continue;
1316 1.1 rjs if ((non_asoc_addr_ok == 0) &&
1317 1.1 rjs (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1318 1.1 rjs /* on the no-no list */
1319 1.1 rjs continue;
1320 1.1 rjs }
1321 1.1 rjs return (sin6);
1322 1.1 rjs }
1323 1.1 rjs }
1324 1.1 rjs }
1325 1.1 rjs starting_point = stcb->asoc.last_used_address;
1326 1.1 rjs /* First try for matching scope */
1327 1.1 rjs sctp_from_the_top:
1328 1.1 rjs if (stcb->asoc.last_used_address == NULL) {
1329 1.1 rjs start_at_beginning=1;
1330 1.1 rjs stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1331 1.1 rjs }
1332 1.1 rjs /* search beginning with the last used address */
1333 1.1 rjs for (laddr = stcb->asoc.last_used_address; laddr;
1334 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1335 1.1 rjs if (laddr->ifa == NULL) {
1336 1.1 rjs /* address has been removed */
1337 1.1 rjs continue;
1338 1.1 rjs }
1339 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1340 1.1 rjs if (sin6 == NULL)
1341 1.1 rjs continue;
1342 1.1 rjs if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1343 1.1 rjs /* on the no-no list */
1344 1.1 rjs continue;
1345 1.1 rjs }
1346 1.1 rjs /* is it of matching scope ? */
1347 1.1 rjs if ((loopscope == 0) &&
1348 1.1 rjs (loc_scope == 0) &&
1349 1.1 rjs (sin_loop == 0) &&
1350 1.1 rjs (sin_local == 0)) {
1351 1.1 rjs /* all of global scope we are ok with it */
1352 1.1 rjs return (sin6);
1353 1.1 rjs }
1354 1.1 rjs if (loopscope && sin_loop)
1355 1.1 rjs /* both on the loopback, thats ok */
1356 1.1 rjs return (sin6);
1357 1.1 rjs if (loc_scope && sin_local)
1358 1.1 rjs /* both local scope */
1359 1.1 rjs return (sin6);
1360 1.1 rjs
1361 1.1 rjs }
1362 1.1 rjs if (start_at_beginning == 0) {
1363 1.1 rjs stcb->asoc.last_used_address = NULL;
1364 1.1 rjs goto sctp_from_the_top;
1365 1.1 rjs }
1366 1.1 rjs /* now try for any higher scope than the destination */
1367 1.1 rjs stcb->asoc.last_used_address = starting_point;
1368 1.1 rjs start_at_beginning = 0;
1369 1.1 rjs sctp_from_the_top2:
1370 1.1 rjs if (stcb->asoc.last_used_address == NULL) {
1371 1.1 rjs start_at_beginning=1;
1372 1.1 rjs stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1373 1.1 rjs }
1374 1.1 rjs /* search beginning with the last used address */
1375 1.1 rjs for (laddr = stcb->asoc.last_used_address; laddr;
1376 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1377 1.1 rjs if (laddr->ifa == NULL) {
1378 1.1 rjs /* address has been removed */
1379 1.1 rjs continue;
1380 1.1 rjs }
1381 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1382 1.1 rjs if (sin6 == NULL)
1383 1.1 rjs continue;
1384 1.1 rjs if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1385 1.1 rjs /* on the no-no list */
1386 1.1 rjs continue;
1387 1.1 rjs }
1388 1.1 rjs return (sin6);
1389 1.1 rjs }
1390 1.1 rjs if (start_at_beginning == 0) {
1391 1.1 rjs stcb->asoc.last_used_address = NULL;
1392 1.1 rjs goto sctp_from_the_top2;
1393 1.1 rjs }
1394 1.1 rjs } else {
1395 1.1 rjs #ifdef SCTP_DEBUG
1396 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1397 1.29 andvar printf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
1398 1.1 rjs }
1399 1.1 rjs #endif
1400 1.1 rjs /* First try for interface output match */
1401 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1402 1.1 rjs sctp_nxt_addr) {
1403 1.1 rjs if (laddr->ifa == NULL) {
1404 1.1 rjs /* address has been removed */
1405 1.1 rjs continue;
1406 1.1 rjs }
1407 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1408 1.1 rjs if (sin6 == NULL)
1409 1.1 rjs continue;
1410 1.1 rjs /* first question, is laddr->ifa an address associated with the emit interface */
1411 1.1 rjs if (ifn) {
1412 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1413 1.1 rjs if (laddr->ifa == ifa) {
1414 1.1 rjs sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1415 1.1 rjs return (sin6);
1416 1.1 rjs }
1417 1.1 rjs if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
1418 1.1 rjs sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1419 1.1 rjs return (sin6);
1420 1.1 rjs }
1421 1.1 rjs }
1422 1.1 rjs }
1423 1.1 rjs }
1424 1.1 rjs /* Next try for matching scope */
1425 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1426 1.1 rjs sctp_nxt_addr) {
1427 1.1 rjs if (laddr->ifa == NULL) {
1428 1.1 rjs /* address has been removed */
1429 1.1 rjs continue;
1430 1.1 rjs }
1431 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1432 1.1 rjs if (sin6 == NULL)
1433 1.1 rjs continue;
1434 1.1 rjs
1435 1.1 rjs if ((loopscope == 0) &&
1436 1.1 rjs (loc_scope == 0) &&
1437 1.1 rjs (sin_loop == 0) &&
1438 1.1 rjs (sin_local == 0)) {
1439 1.1 rjs /* all of global scope we are ok with it */
1440 1.1 rjs return (sin6);
1441 1.1 rjs }
1442 1.1 rjs if (loopscope && sin_loop)
1443 1.1 rjs /* both on the loopback, thats ok */
1444 1.1 rjs return (sin6);
1445 1.1 rjs if (loc_scope && sin_local)
1446 1.1 rjs /* both local scope */
1447 1.1 rjs return (sin6);
1448 1.1 rjs }
1449 1.1 rjs /* ok, now try for a higher scope in the source address */
1450 1.1 rjs /* First try for matching scope */
1451 1.1 rjs LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1452 1.1 rjs sctp_nxt_addr) {
1453 1.1 rjs if (laddr->ifa == NULL) {
1454 1.1 rjs /* address has been removed */
1455 1.1 rjs continue;
1456 1.1 rjs }
1457 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1458 1.1 rjs if (sin6 == NULL)
1459 1.1 rjs continue;
1460 1.1 rjs return (sin6);
1461 1.1 rjs }
1462 1.1 rjs }
1463 1.1 rjs return (NULL);
1464 1.1 rjs }
1465 1.1 rjs
1466 1.1 rjs static struct sockaddr_in6 *
1467 1.1 rjs sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
1468 1.1 rjs struct rtentry *rt,
1469 1.1 rjs uint8_t loc_scope,
1470 1.1 rjs uint8_t loopscope)
1471 1.1 rjs {
1472 1.1 rjs /*
1473 1.1 rjs * Here we are bound specific and have only
1474 1.1 rjs * an inp. We must find an address that is bound
1475 1.1 rjs * that we can give out as a src address. We
1476 1.1 rjs * prefer two addresses of same scope if we can
1477 1.1 rjs * find them that way.
1478 1.1 rjs */
1479 1.1 rjs struct sctp_laddr *laddr;
1480 1.1 rjs struct sockaddr_in6 *sin6;
1481 1.1 rjs struct ifnet *ifn;
1482 1.1 rjs struct ifaddr *ifa;
1483 1.1 rjs int sin_loop, sin_local;
1484 1.1 rjs
1485 1.1 rjs /* first question, is the ifn we will emit on
1486 1.1 rjs * in our list, if so, we want that one.
1487 1.1 rjs */
1488 1.1 rjs
1489 1.1 rjs ifn = rt->rt_ifp;
1490 1.1 rjs if (ifn) {
1491 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1492 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1493 1.1 rjs if (sin6 == NULL)
1494 1.1 rjs continue;
1495 1.1 rjs if (sctp_is_addr_in_ep(inp, ifa)) {
1496 1.1 rjs return (sin6);
1497 1.1 rjs }
1498 1.1 rjs }
1499 1.1 rjs }
1500 1.1 rjs for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1501 1.1 rjs laddr && (laddr != inp->next_addr_touse);
1502 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1503 1.1 rjs if (laddr->ifa == NULL) {
1504 1.1 rjs /* address has been removed */
1505 1.1 rjs continue;
1506 1.1 rjs }
1507 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1508 1.1 rjs if (sin6 == NULL)
1509 1.1 rjs continue;
1510 1.1 rjs
1511 1.1 rjs if ((loopscope == 0) &&
1512 1.1 rjs (loc_scope == 0) &&
1513 1.1 rjs (sin_loop == 0) &&
1514 1.1 rjs (sin_local == 0)) {
1515 1.1 rjs /* all of global scope we are ok with it */
1516 1.1 rjs return (sin6);
1517 1.1 rjs }
1518 1.1 rjs if (loopscope && sin_loop)
1519 1.1 rjs /* both on the loopback, thats ok */
1520 1.1 rjs return (sin6);
1521 1.1 rjs if (loc_scope && sin_local)
1522 1.1 rjs /* both local scope */
1523 1.1 rjs return (sin6);
1524 1.1 rjs
1525 1.1 rjs }
1526 1.1 rjs /* if we reach here, we could not find two addresses
1527 1.1 rjs * of the same scope to give out. Lets look for any higher level
1528 1.1 rjs * scope for a source address.
1529 1.1 rjs */
1530 1.1 rjs for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1531 1.1 rjs laddr && (laddr != inp->next_addr_touse);
1532 1.1 rjs laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1533 1.1 rjs if (laddr->ifa == NULL) {
1534 1.1 rjs /* address has been removed */
1535 1.1 rjs continue;
1536 1.1 rjs }
1537 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1538 1.1 rjs if (sin6 == NULL)
1539 1.1 rjs continue;
1540 1.1 rjs return (sin6);
1541 1.1 rjs }
1542 1.1 rjs /* no address bound can be a source for the destination */
1543 1.1 rjs #ifdef SCTP_DEBUG
1544 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1545 1.1 rjs printf("Src address selection for EP, no acceptable src address found for address\n");
1546 1.1 rjs }
1547 1.1 rjs #endif
1548 1.1 rjs return (NULL);
1549 1.1 rjs }
1550 1.1 rjs
1551 1.1 rjs
1552 1.1 rjs static struct sockaddr_in6 *
1553 1.1 rjs sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
1554 1.1 rjs uint8_t loc_scope, int cur_addr_num, int match_scope)
1555 1.1 rjs {
1556 1.1 rjs struct ifaddr *ifa;
1557 1.1 rjs struct sockaddr_in6 *sin6;
1558 1.1 rjs int sin_loop, sin_local;
1559 1.1 rjs int num_eligible_addr = 0;
1560 1.1 rjs
1561 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1562 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1563 1.1 rjs if (sin6 == NULL)
1564 1.1 rjs continue;
1565 1.1 rjs if (stcb) {
1566 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1567 1.1 rjs /* It is restricted for some reason.. probably
1568 1.1 rjs * not yet added.
1569 1.1 rjs */
1570 1.1 rjs continue;
1571 1.1 rjs }
1572 1.1 rjs }
1573 1.1 rjs if (match_scope) {
1574 1.1 rjs /* Here we are asked to match scope if possible */
1575 1.1 rjs if (loopscope && sin_loop)
1576 1.1 rjs /* src and destination are loopback scope */
1577 1.1 rjs return (sin6);
1578 1.1 rjs if (loc_scope && sin_local)
1579 1.1 rjs /* src and destination are local scope */
1580 1.1 rjs return (sin6);
1581 1.1 rjs if ((loopscope == 0) &&
1582 1.1 rjs (loc_scope == 0) &&
1583 1.1 rjs (sin_loop == 0) &&
1584 1.1 rjs (sin_local == 0)) {
1585 1.1 rjs /* src and destination are global scope */
1586 1.1 rjs return (sin6);
1587 1.1 rjs }
1588 1.1 rjs continue;
1589 1.1 rjs }
1590 1.1 rjs if (num_eligible_addr == cur_addr_num) {
1591 1.1 rjs /* this is it */
1592 1.1 rjs return (sin6);
1593 1.1 rjs }
1594 1.1 rjs num_eligible_addr++;
1595 1.1 rjs }
1596 1.1 rjs return (NULL);
1597 1.1 rjs }
1598 1.1 rjs
1599 1.1 rjs
1600 1.1 rjs static int
1601 1.1 rjs sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
1602 1.1 rjs int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
1603 1.1 rjs {
1604 1.1 rjs struct ifaddr *ifa;
1605 1.1 rjs struct sockaddr_in6 *sin6;
1606 1.1 rjs int num_eligible_addr = 0;
1607 1.1 rjs int sin_loop, sin_local;
1608 1.1 rjs
1609 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
1610 1.1 rjs sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1611 1.1 rjs if (sin6 == NULL)
1612 1.1 rjs continue;
1613 1.1 rjs if (stcb) {
1614 1.1 rjs if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1615 1.1 rjs /* It is restricted for some reason.. probably
1616 1.1 rjs * not yet added.
1617 1.1 rjs */
1618 1.1 rjs continue;
1619 1.1 rjs }
1620 1.1 rjs }
1621 1.1 rjs num_eligible_addr++;
1622 1.1 rjs }
1623 1.1 rjs return (num_eligible_addr);
1624 1.1 rjs }
1625 1.1 rjs
1626 1.1 rjs
1627 1.1 rjs static struct sockaddr_in6 *
1628 1.1 rjs sctp_choose_v6_boundall(struct sctp_inpcb *inp,
1629 1.1 rjs struct sctp_tcb *stcb,
1630 1.1 rjs struct sctp_nets *net,
1631 1.1 rjs struct rtentry *rt,
1632 1.1 rjs uint8_t loc_scope,
1633 1.1 rjs uint8_t loopscope,
1634 1.1 rjs int non_asoc_addr_ok)
1635 1.1 rjs {
1636 1.1 rjs /* Ok, we are bound all SO any address
1637 1.1 rjs * is ok to use as long as it is NOT in the negative
1638 1.1 rjs * list.
1639 1.1 rjs */
1640 1.1 rjs int num_eligible_addr;
1641 1.1 rjs int cur_addr_num=0;
1642 1.1 rjs int started_at_beginning=0;
1643 1.1 rjs int match_scope_prefered;
1644 1.1 rjs /* first question is, how many eligible addresses are
1645 1.1 rjs * there for the destination ifn that we are using that
1646 1.1 rjs * are within the proper scope?
1647 1.1 rjs */
1648 1.1 rjs struct ifnet *ifn;
1649 1.1 rjs struct sockaddr_in6 *sin6;
1650 1.4 ozaki int s;
1651 1.1 rjs
1652 1.1 rjs ifn = rt->rt_ifp;
1653 1.1 rjs if (net) {
1654 1.1 rjs cur_addr_num = net->indx_of_eligible_next_to_use;
1655 1.1 rjs }
1656 1.1 rjs if (cur_addr_num == 0) {
1657 1.1 rjs match_scope_prefered = 1;
1658 1.1 rjs } else {
1659 1.1 rjs match_scope_prefered = 0;
1660 1.1 rjs }
1661 1.1 rjs num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1662 1.1 rjs #ifdef SCTP_DEBUG
1663 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1664 1.1 rjs printf("Found %d eligible source addresses\n", num_eligible_addr);
1665 1.1 rjs }
1666 1.1 rjs #endif
1667 1.1 rjs if (num_eligible_addr == 0) {
1668 1.1 rjs /* no eligible addresses, we must use some other
1669 1.1 rjs * interface address if we can find one.
1670 1.1 rjs */
1671 1.1 rjs goto bound_all_v6_plan_b;
1672 1.1 rjs }
1673 1.1 rjs /* Ok we have num_eligible_addr set with how many we can use,
1674 1.1 rjs * this may vary from call to call due to addresses being deprecated etc..
1675 1.1 rjs */
1676 1.1 rjs if (cur_addr_num >= num_eligible_addr) {
1677 1.1 rjs cur_addr_num = 0;
1678 1.1 rjs }
1679 1.1 rjs /* select the nth address from the list (where cur_addr_num is the nth) and
1680 1.1 rjs * 0 is the first one, 1 is the second one etc...
1681 1.1 rjs */
1682 1.1 rjs #ifdef SCTP_DEBUG
1683 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1684 1.1 rjs printf("cur_addr_num:%d match_scope_prefered:%d select it\n",
1685 1.1 rjs cur_addr_num, match_scope_prefered);
1686 1.1 rjs }
1687 1.1 rjs #endif
1688 1.1 rjs sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1689 1.1 rjs loc_scope, cur_addr_num, match_scope_prefered);
1690 1.1 rjs if (match_scope_prefered && (sin6 == NULL)) {
1691 1.1 rjs /* retry without the preference for matching scope */
1692 1.1 rjs #ifdef SCTP_DEBUG
1693 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1694 1.1 rjs printf("retry with no match_scope_prefered\n");
1695 1.1 rjs }
1696 1.1 rjs #endif
1697 1.1 rjs sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1698 1.1 rjs loc_scope, cur_addr_num, 0);
1699 1.1 rjs }
1700 1.1 rjs if (sin6) {
1701 1.1 rjs #ifdef SCTP_DEBUG
1702 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1703 1.1 rjs printf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
1704 1.1 rjs }
1705 1.1 rjs #endif
1706 1.1 rjs if (net) {
1707 1.1 rjs /* store so we get the next one */
1708 1.1 rjs if (cur_addr_num < 255)
1709 1.1 rjs net->indx_of_eligible_next_to_use = cur_addr_num + 1;
1710 1.1 rjs else
1711 1.1 rjs net->indx_of_eligible_next_to_use = 0;
1712 1.1 rjs }
1713 1.1 rjs return (sin6);
1714 1.1 rjs }
1715 1.1 rjs num_eligible_addr = 0;
1716 1.1 rjs bound_all_v6_plan_b:
1717 1.1 rjs /* ok, if we reach here we either fell through
1718 1.17 skrll * due to something changing during an interrupt (unlikely)
1719 1.1 rjs * or we have NO eligible source addresses for the ifn
1720 1.1 rjs * of the route (most likely). We must look at all the other
1721 1.1 rjs * interfaces EXCEPT rt->rt_ifp and do the same game.
1722 1.1 rjs */
1723 1.1 rjs #ifdef SCTP_DEBUG
1724 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1725 1.1 rjs printf("bound-all Plan B\n");
1726 1.1 rjs }
1727 1.1 rjs #endif
1728 1.1 rjs if (inp->next_ifn_touse == NULL) {
1729 1.1 rjs started_at_beginning=1;
1730 1.4 ozaki inp->next_ifn_touse = IFNET_READER_FIRST();
1731 1.1 rjs #ifdef SCTP_DEBUG
1732 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1733 1.1 rjs printf("Start at first IFN:%p\n", inp->next_ifn_touse);
1734 1.1 rjs }
1735 1.1 rjs #endif
1736 1.1 rjs } else {
1737 1.4 ozaki inp->next_ifn_touse = IFNET_READER_NEXT(inp->next_ifn_touse);
1738 1.1 rjs #ifdef SCTP_DEBUG
1739 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1740 1.1 rjs printf("Resume at IFN:%p\n", inp->next_ifn_touse);
1741 1.1 rjs }
1742 1.1 rjs #endif
1743 1.1 rjs if (inp->next_ifn_touse == NULL) {
1744 1.1 rjs #ifdef SCTP_DEBUG
1745 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1746 1.1 rjs printf("IFN Resets\n");
1747 1.1 rjs }
1748 1.1 rjs #endif
1749 1.1 rjs started_at_beginning=1;
1750 1.4 ozaki inp->next_ifn_touse = IFNET_READER_FIRST();
1751 1.1 rjs }
1752 1.1 rjs }
1753 1.4 ozaki
1754 1.4 ozaki s = pserialize_read_enter();
1755 1.4 ozaki IFNET_READER_FOREACH(ifn) {
1756 1.1 rjs if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1757 1.1 rjs /* wrong base scope */
1758 1.1 rjs continue;
1759 1.1 rjs }
1760 1.1 rjs if (loc_scope && (ifn->if_index != loc_scope)) {
1761 1.1 rjs /* by definition the scope (from to->sin6_scopeid)
1762 1.1 rjs * must match that of the interface. If not then
1763 1.1 rjs * we could pick a wrong scope for the address.
1764 1.21 msaitoh * Usually we don't hit plan-b since the route
1765 1.1 rjs * handles this. However we can hit plan-b when
1766 1.1 rjs * we send to local-host so the route is the
1767 1.1 rjs * loopback interface, but the destination is a
1768 1.1 rjs * link local.
1769 1.1 rjs */
1770 1.1 rjs continue;
1771 1.1 rjs }
1772 1.1 rjs if (ifn == rt->rt_ifp) {
1773 1.1 rjs /* already looked at this guy */
1774 1.1 rjs continue;
1775 1.1 rjs }
1776 1.1 rjs /* Address rotation will only work when we are not
1777 1.1 rjs * rotating sourced interfaces and are using the interface
1778 1.1 rjs * of the route. We would need to have a per interface index
1779 1.1 rjs * in order to do proper rotation.
1780 1.1 rjs */
1781 1.1 rjs num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1782 1.1 rjs #ifdef SCTP_DEBUG
1783 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1784 1.1 rjs printf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
1785 1.1 rjs }
1786 1.1 rjs #endif
1787 1.1 rjs if (num_eligible_addr == 0) {
1788 1.1 rjs /* none we can use */
1789 1.1 rjs continue;
1790 1.1 rjs }
1791 1.1 rjs /* Ok we have num_eligible_addr set with how many we can use,
1792 1.1 rjs * this may vary from call to call due to addresses being deprecated etc..
1793 1.1 rjs */
1794 1.1 rjs inp->next_ifn_touse = ifn;
1795 1.1 rjs
1796 1.1 rjs /* select the first one we can find with perference for matching scope.
1797 1.1 rjs */
1798 1.1 rjs sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
1799 1.1 rjs if (sin6 == NULL) {
1800 1.1 rjs /* can't find one with matching scope how about a source with higher
1801 1.1 rjs * scope
1802 1.1 rjs */
1803 1.1 rjs sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
1804 1.1 rjs if (sin6 == NULL)
1805 1.1 rjs /* Hmm, can't find one in the interface now */
1806 1.1 rjs continue;
1807 1.1 rjs }
1808 1.1 rjs #ifdef SCTP_DEBUG
1809 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1810 1.1 rjs printf("Selected the %d'th address of ifn:%p\n",
1811 1.1 rjs cur_addr_num, ifn);
1812 1.1 rjs }
1813 1.1 rjs #endif
1814 1.4 ozaki pserialize_read_exit(s);
1815 1.1 rjs return (sin6);
1816 1.1 rjs }
1817 1.4 ozaki pserialize_read_exit(s);
1818 1.4 ozaki
1819 1.1 rjs if (started_at_beginning == 0) {
1820 1.1 rjs /* we have not been through all of them yet, force
1821 1.1 rjs * us to go through them all.
1822 1.1 rjs */
1823 1.1 rjs #ifdef SCTP_DEBUG
1824 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1825 1.1 rjs printf("Force a recycle\n");
1826 1.1 rjs }
1827 1.1 rjs #endif
1828 1.1 rjs inp->next_ifn_touse = NULL;
1829 1.1 rjs goto bound_all_v6_plan_b;
1830 1.1 rjs }
1831 1.1 rjs return (NULL);
1832 1.1 rjs
1833 1.1 rjs }
1834 1.1 rjs
1835 1.1 rjs /* stcb and net may be NULL */
1836 1.1 rjs struct in6_addr
1837 1.1 rjs sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
1838 1.1 rjs struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1839 1.1 rjs int non_asoc_addr_ok)
1840 1.1 rjs {
1841 1.1 rjs struct in6_addr ans;
1842 1.1 rjs struct sockaddr_in6 *rt_addr;
1843 1.1 rjs uint8_t loc_scope, loopscope;
1844 1.1 rjs struct sockaddr_in6 to;
1845 1.1 rjs struct rtentry *rt;
1846 1.1 rjs
1847 1.1 rjs /*
1848 1.1 rjs * This routine is tricky standard v6 src address
1849 1.1 rjs * selection cannot take into account what we have
1850 1.1 rjs * bound etc, so we can't use it.
1851 1.1 rjs *
1852 1.1 rjs * Instead here is what we must do:
1853 1.1 rjs * 1) Make sure we have a route, if we
1854 1.1 rjs * don't have a route we can never reach the peer.
1855 1.1 rjs * 2) Once we have a route, determine the scope of the
1856 1.1 rjs * route. Link local, loopback or global.
1857 1.1 rjs * 3) Next we divide into three types. Either we
1858 1.1 rjs * are bound all.. which means we want to use
1859 1.1 rjs * one of the addresses of the interface we are
1860 1.1 rjs * going out. <or>
1861 1.1 rjs * 4a) We have not stcb, which means we are using the
1862 1.1 rjs * specific addresses bound on an inp, in this
1863 1.1 rjs * case we are similar to the stcb case (4b below)
1864 1.1 rjs * accept the list is always a positive list.<or>
1865 1.1 rjs * 4b) We are bound specific with a stcb, which means we have a
1866 1.1 rjs * list of bound addresses and we must see if the
1867 1.1 rjs * ifn of the route is actually one of the bound addresses.
1868 1.1 rjs * If not, then we must rotate addresses amongst properly
1869 1.1 rjs * scoped bound addresses, if so we use the address
1870 1.1 rjs * of the interface.
1871 1.1 rjs * 5) Always, no matter which path we take through the above
1872 1.1 rjs * we must be sure the source address we use is allowed to
1873 1.1 rjs * be used. I.e. IN6_IFF_DETACHED, IN6_IFF_NOTREADY, and IN6_IFF_ANYCAST
1874 1.1 rjs * addresses cannot be used.
1875 1.1 rjs * 6) Addresses that are deprecated MAY be used
1876 1.1 rjs * if (!ip6_use_deprecated) {
1877 1.1 rjs * if (IFA6_IS_DEPRECATED(ifa6)) {
1878 1.1 rjs * skip the address
1879 1.1 rjs * }
1880 1.1 rjs * }
1881 1.1 rjs */
1882 1.1 rjs
1883 1.1 rjs /*** 1> determine route, if not already done */
1884 1.1 rjs rt = rtcache_validate(ro);
1885 1.1 rjs if (rt == NULL) {
1886 1.1 rjs /*
1887 1.1 rjs * Need a route to cache.
1888 1.1 rjs */
1889 1.1 rjs int scope_save;
1890 1.1 rjs
1891 1.1 rjs memcpy(&to, rtcache_getdst(ro), sizeof(struct sockaddr));
1892 1.1 rjs scope_save = to.sin6_scope_id;
1893 1.1 rjs to.sin6_scope_id = 0;
1894 1.1 rjs
1895 1.1 rjs rt = rtcache_lookup(ro, (struct sockaddr *)&to);
1896 1.1 rjs to.sin6_scope_id = scope_save;
1897 1.1 rjs }
1898 1.1 rjs if (rt == NULL) {
1899 1.1 rjs /*
1900 1.1 rjs * no route to host. this packet is going no-where.
1901 1.1 rjs * We probably should make sure we arrange to send back
1902 1.1 rjs * an error.
1903 1.1 rjs */
1904 1.1 rjs #ifdef SCTP_DEBUG
1905 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1906 1.1 rjs printf("No route to host, this packet cannot be sent!\n");
1907 1.1 rjs }
1908 1.1 rjs #endif
1909 1.1 rjs memset(&ans, 0, sizeof(ans));
1910 1.1 rjs return (ans);
1911 1.1 rjs }
1912 1.1 rjs
1913 1.1 rjs /*** 2a> determine scope for outbound address/route */
1914 1.1 rjs loc_scope = loopscope = 0;
1915 1.1 rjs /*
1916 1.1 rjs * We base our scope on the outbound packet scope and route,
1917 1.1 rjs * NOT the TCB (if there is one). This way in local scope we will only
1918 1.1 rjs * use a local scope src address when we send to a local address.
1919 1.1 rjs */
1920 1.1 rjs
1921 1.1 rjs if (IN6_IS_ADDR_LOOPBACK(&to.sin6_addr)) {
1922 1.1 rjs /* If the route goes to the loopback address OR
1923 1.1 rjs * the address is a loopback address, we are loopback
1924 1.1 rjs * scope.
1925 1.1 rjs */
1926 1.1 rjs #ifdef SCTP_DEBUG
1927 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1928 1.1 rjs printf("Loopback scope is set\n");
1929 1.1 rjs }
1930 1.1 rjs #endif
1931 1.1 rjs loc_scope = 0;
1932 1.1 rjs loopscope = 1;
1933 1.1 rjs if (net != NULL) {
1934 1.1 rjs /* mark it as local */
1935 1.1 rjs net->addr_is_local = 1;
1936 1.1 rjs }
1937 1.1 rjs
1938 1.1 rjs } else if (IN6_IS_ADDR_LINKLOCAL(&to.sin6_addr)) {
1939 1.1 rjs #ifdef SCTP_DEBUG
1940 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1941 1.1 rjs printf("Link local scope is set, id:%d\n", to.sin6_scope_id);
1942 1.1 rjs }
1943 1.1 rjs #endif
1944 1.1 rjs if (to.sin6_scope_id)
1945 1.1 rjs loc_scope = to.sin6_scope_id;
1946 1.1 rjs else {
1947 1.1 rjs loc_scope = 1;
1948 1.1 rjs }
1949 1.1 rjs loopscope = 0;
1950 1.1 rjs } else {
1951 1.1 rjs #ifdef SCTP_DEBUG
1952 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1953 1.1 rjs printf("Global scope is set\n");
1954 1.1 rjs }
1955 1.1 rjs #endif
1956 1.1 rjs }
1957 1.1 rjs
1958 1.1 rjs /* now, depending on which way we are bound we call the appropriate
1959 1.1 rjs * routine to do steps 3-6
1960 1.1 rjs */
1961 1.1 rjs #ifdef SCTP_DEBUG
1962 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1963 1.1 rjs printf("Destination address:");
1964 1.1 rjs sctp_print_address((struct sockaddr *)&to);
1965 1.1 rjs }
1966 1.1 rjs #endif
1967 1.1 rjs
1968 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1969 1.1 rjs #ifdef SCTP_DEBUG
1970 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1971 1.1 rjs printf("Calling bound-all src addr selection for v6\n");
1972 1.1 rjs }
1973 1.1 rjs #endif
1974 1.1 rjs rt_addr = sctp_choose_v6_boundall(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
1975 1.1 rjs } else {
1976 1.1 rjs #ifdef SCTP_DEBUG
1977 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1978 1.1 rjs printf("Calling bound-specific src addr selection for v6\n");
1979 1.1 rjs }
1980 1.1 rjs #endif
1981 1.1 rjs if (stcb)
1982 1.1 rjs rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
1983 1.1 rjs else
1984 1.1 rjs /* we can't have a non-asoc address since we have no association */
1985 1.1 rjs rt_addr = sctp_choose_v6_boundspecific_inp(inp, rt, loc_scope, loopscope);
1986 1.1 rjs }
1987 1.8 ozaki rtcache_unref(rt, ro);
1988 1.1 rjs if (rt_addr == NULL) {
1989 1.1 rjs /* no suitable address? */
1990 1.1 rjs struct in6_addr in6;
1991 1.1 rjs #ifdef SCTP_DEBUG
1992 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1993 1.1 rjs printf("V6 packet will reach dead-end no suitable src address\n");
1994 1.1 rjs }
1995 1.1 rjs #endif
1996 1.1 rjs memset(&in6, 0, sizeof(in6));
1997 1.1 rjs return (in6);
1998 1.1 rjs }
1999 1.1 rjs #ifdef SCTP_DEBUG
2000 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2001 1.1 rjs printf("Source address selected is:");
2002 1.1 rjs sctp_print_address((struct sockaddr *)rt_addr);
2003 1.1 rjs }
2004 1.1 rjs #endif
2005 1.1 rjs return (rt_addr->sin6_addr);
2006 1.1 rjs }
2007 1.1 rjs
2008 1.1 rjs static uint8_t
2009 1.1 rjs sctp_get_ect(struct sctp_tcb *stcb,
2010 1.1 rjs struct sctp_tmit_chunk *chk)
2011 1.1 rjs {
2012 1.1 rjs uint8_t this_random;
2013 1.1 rjs
2014 1.1 rjs /* Huh? */
2015 1.1 rjs if (sctp_ecn == 0)
2016 1.1 rjs return (0);
2017 1.1 rjs
2018 1.1 rjs if (sctp_ecn_nonce == 0)
2019 1.1 rjs /* no nonce, always return ECT0 */
2020 1.1 rjs return (SCTP_ECT0_BIT);
2021 1.1 rjs
2022 1.1 rjs if (stcb->asoc.peer_supports_ecn_nonce == 0) {
2023 1.1 rjs /* Peer does NOT support it, so we send a ECT0 only */
2024 1.1 rjs return (SCTP_ECT0_BIT);
2025 1.1 rjs }
2026 1.1 rjs
2027 1.1 rjs if (chk == NULL)
2028 1.1 rjs return (SCTP_ECT0_BIT);
2029 1.1 rjs
2030 1.1 rjs if (((stcb->asoc.hb_random_idx == 3) &&
2031 1.1 rjs (stcb->asoc.hb_ect_randombit > 7)) ||
2032 1.1 rjs (stcb->asoc.hb_random_idx > 3)) {
2033 1.1 rjs uint32_t rndval;
2034 1.1 rjs rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
2035 1.1 rjs memcpy(stcb->asoc.hb_random_values, &rndval,
2036 1.1 rjs sizeof(stcb->asoc.hb_random_values));
2037 1.1 rjs this_random = stcb->asoc.hb_random_values[0];
2038 1.1 rjs stcb->asoc.hb_random_idx = 0;
2039 1.1 rjs stcb->asoc.hb_ect_randombit = 0;
2040 1.1 rjs } else {
2041 1.1 rjs if (stcb->asoc.hb_ect_randombit > 7) {
2042 1.1 rjs stcb->asoc.hb_ect_randombit = 0;
2043 1.1 rjs stcb->asoc.hb_random_idx++;
2044 1.1 rjs }
2045 1.1 rjs this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2046 1.1 rjs }
2047 1.1 rjs if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
2048 1.1 rjs if (chk != NULL)
2049 1.1 rjs /* ECN Nonce stuff */
2050 1.1 rjs chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
2051 1.1 rjs stcb->asoc.hb_ect_randombit++;
2052 1.1 rjs return (SCTP_ECT1_BIT);
2053 1.1 rjs } else {
2054 1.1 rjs stcb->asoc.hb_ect_randombit++;
2055 1.1 rjs return (SCTP_ECT0_BIT);
2056 1.1 rjs }
2057 1.1 rjs }
2058 1.1 rjs
2059 1.1 rjs extern int sctp_no_csum_on_loopback;
2060 1.1 rjs
2061 1.1 rjs static int
2062 1.1 rjs sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
2063 1.1 rjs struct sctp_tcb *stcb, /* may be NULL */
2064 1.1 rjs struct sctp_nets *net,
2065 1.1 rjs const struct sockaddr *to,
2066 1.1 rjs struct mbuf *m,
2067 1.1 rjs int nofragment_flag,
2068 1.1 rjs int ecn_ok,
2069 1.1 rjs struct sctp_tmit_chunk *chk,
2070 1.1 rjs int out_of_asoc_ok)
2071 1.1 rjs /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
2072 1.1 rjs {
2073 1.1 rjs /*
2074 1.1 rjs * Given a mbuf chain (via m_next) that holds a packet header
2075 1.1 rjs * WITH a SCTPHDR but no IP header, endpoint inp and sa structure.
2076 1.1 rjs * - calculate SCTP checksum and fill in
2077 1.1 rjs * - prepend a IP address header
2078 1.1 rjs * - if boundall use INADDR_ANY
2079 1.1 rjs * - if boundspecific do source address selection
2080 1.1 rjs * - set fragmentation option for ipV4
2081 1.1 rjs * - On return from IP output, check/adjust mtu size
2082 1.1 rjs * - of output interface and smallest_mtu size as well.
2083 1.1 rjs */
2084 1.1 rjs struct sctphdr *sctphdr;
2085 1.1 rjs int o_flgs;
2086 1.1 rjs uint32_t csum;
2087 1.1 rjs int ret;
2088 1.1 rjs unsigned int have_mtu;
2089 1.1 rjs struct route *ro;
2090 1.1 rjs struct rtentry *rt;
2091 1.1 rjs
2092 1.1 rjs if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
2093 1.1 rjs sctp_m_freem(m);
2094 1.1 rjs return (EFAULT);
2095 1.1 rjs }
2096 1.1 rjs if ((m->m_flags & M_PKTHDR) == 0) {
2097 1.1 rjs #ifdef SCTP_DEBUG
2098 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2099 1.1 rjs printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
2100 1.1 rjs }
2101 1.1 rjs #endif
2102 1.1 rjs sctp_m_freem(m);
2103 1.1 rjs return (EFAULT);
2104 1.1 rjs }
2105 1.1 rjs /* Calculate the csum and fill in the length of the packet */
2106 1.1 rjs sctphdr = mtod(m, struct sctphdr *);
2107 1.1 rjs have_mtu = 0;
2108 1.1 rjs if (sctp_no_csum_on_loopback &&
2109 1.1 rjs (stcb) &&
2110 1.1 rjs (stcb->asoc.loopback_scope)) {
2111 1.1 rjs sctphdr->checksum = 0;
2112 1.1 rjs m->m_pkthdr.len = sctp_calculate_len(m);
2113 1.1 rjs } else {
2114 1.1 rjs sctphdr->checksum = 0;
2115 1.1 rjs csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
2116 1.1 rjs sctphdr->checksum = csum;
2117 1.1 rjs }
2118 1.1 rjs if (to->sa_family == AF_INET) {
2119 1.1 rjs struct ip *ip;
2120 1.1 rjs static struct route iproute;
2121 1.1 rjs M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
2122 1.1 rjs if (m == NULL) {
2123 1.1 rjs /* failed to prepend data, give up */
2124 1.1 rjs return (ENOMEM);
2125 1.1 rjs }
2126 1.1 rjs ip = mtod(m, struct ip *);
2127 1.1 rjs ip->ip_v = IPVERSION;
2128 1.1 rjs ip->ip_hl = (sizeof(struct ip) >> 2);
2129 1.1 rjs if (nofragment_flag) {
2130 1.1 rjs ip->ip_off = htons(IP_DF);
2131 1.1 rjs } else
2132 1.1 rjs ip->ip_off = 0;
2133 1.1 rjs
2134 1.1 rjs ip->ip_id = htons(ip_newid(NULL));
2135 1.1 rjs ip->ip_ttl = inp->inp_ip_ttl;
2136 1.1 rjs ip->ip_len = htons(m->m_pkthdr.len);
2137 1.1 rjs if (stcb) {
2138 1.1 rjs if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2139 1.1 rjs /* Enable ECN */
2140 1.1 rjs ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip.ip_tos & 0x000000fc) |
2141 1.1 rjs sctp_get_ect(stcb, chk));
2142 1.1 rjs } else {
2143 1.1 rjs /* No ECN */
2144 1.1 rjs ip->ip_tos = inp->ip_inp.inp.inp_ip.ip_tos;
2145 1.1 rjs }
2146 1.1 rjs } else {
2147 1.1 rjs /* no association at all */
2148 1.1 rjs ip->ip_tos = inp->inp_ip_tos;
2149 1.1 rjs }
2150 1.1 rjs ip->ip_p = IPPROTO_SCTP;
2151 1.1 rjs ip->ip_sum = 0;
2152 1.1 rjs #ifdef SCTP_DEBUG
2153 1.1 rjs printf("chunk_output: net %p\n", net);
2154 1.1 rjs #endif
2155 1.1 rjs if (net == NULL) {
2156 1.1 rjs ro = &iproute;
2157 1.1 rjs memset(&iproute, 0, sizeof(iproute));
2158 1.8 ozaki /* XXX */
2159 1.8 ozaki rt = rtcache_lookup(ro, to);
2160 1.8 ozaki rtcache_unref(rt, ro);
2161 1.1 rjs } else {
2162 1.1 rjs ro = (struct route *)&net->ro;
2163 1.1 rjs }
2164 1.1 rjs /* Now the address selection part */
2165 1.1 rjs ip->ip_dst.s_addr = satocsin(to)->sin_addr.s_addr;
2166 1.1 rjs
2167 1.1 rjs /* call the routine to select the src address */
2168 1.1 rjs if (net) {
2169 1.1 rjs if (net->src_addr_selected == 0) {
2170 1.1 rjs /* Cache the source address */
2171 1.1 rjs ((struct sockaddr_in *)&net->_s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
2172 1.1 rjs stcb,
2173 1.1 rjs ro, net, out_of_asoc_ok);
2174 1.8 ozaki rt = rtcache_validate(ro);
2175 1.8 ozaki if (rt != NULL) {
2176 1.1 rjs net->src_addr_selected = 1;
2177 1.1 rjs }
2178 1.8 ozaki rtcache_unref(rt, ro);
2179 1.1 rjs }
2180 1.1 rjs ip->ip_src = ((struct sockaddr_in *)&net->_s_addr)->sin_addr;
2181 1.1 rjs } else {
2182 1.1 rjs ip->ip_src = sctp_ipv4_source_address_selection(inp,
2183 1.1 rjs stcb, ro, net, out_of_asoc_ok);
2184 1.1 rjs }
2185 1.1 rjs #ifdef SCTP_DEBUG
2186 1.1 rjs printf("src addr %x\n", ip->ip_src.s_addr);
2187 1.1 rjs #endif
2188 1.1 rjs /*
2189 1.1 rjs * If source address selection fails and we find no route then
2190 1.23 andvar * the ip_output should fail as well with a NO_ROUTE_TO_HOST
2191 1.1 rjs * type error. We probably should catch that somewhere and
2192 1.1 rjs * abort the association right away (assuming this is an INIT
2193 1.1 rjs * being sent).
2194 1.1 rjs */
2195 1.1 rjs rt = rtcache_validate(ro);
2196 1.9 maya if (rt == NULL) {
2197 1.1 rjs /*
2198 1.1 rjs * src addr selection failed to find a route (or valid
2199 1.1 rjs * source addr), so we can't get there from here!
2200 1.1 rjs */
2201 1.1 rjs #ifdef SCTP_DEBUG
2202 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2203 1.1 rjs printf("low_level_output: dropped v4 packet- no valid source addr\n");
2204 1.1 rjs printf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2205 1.1 rjs }
2206 1.1 rjs #endif /* SCTP_DEBUG */
2207 1.1 rjs if (net) {
2208 1.1 rjs if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2209 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2210 1.1 rjs stcb,
2211 1.1 rjs SCTP_FAILED_THRESHOLD,
2212 1.1 rjs (void *)net);
2213 1.1 rjs net->dest_state &= ~SCTP_ADDR_REACHABLE;
2214 1.1 rjs net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2215 1.1 rjs if (stcb) {
2216 1.1 rjs if (net == stcb->asoc.primary_destination) {
2217 1.1 rjs /* need a new primary */
2218 1.1 rjs struct sctp_nets *alt;
2219 1.1 rjs alt = sctp_find_alternate_net(stcb, net);
2220 1.1 rjs if (alt != net) {
2221 1.1 rjs if (sctp_set_primary_addr(stcb,
2222 1.1 rjs (struct sockaddr *)NULL,
2223 1.1 rjs alt) == 0) {
2224 1.1 rjs net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2225 1.1 rjs net->src_addr_selected = 0;
2226 1.1 rjs }
2227 1.1 rjs }
2228 1.1 rjs }
2229 1.1 rjs }
2230 1.1 rjs }
2231 1.1 rjs sctp_m_freem(m);
2232 1.1 rjs return (EHOSTUNREACH);
2233 1.1 rjs } else {
2234 1.1 rjs have_mtu = rt->rt_ifp->if_mtu;
2235 1.1 rjs }
2236 1.1 rjs
2237 1.1 rjs o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
2238 1.1 rjs #ifdef SCTP_DEBUG
2239 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2240 1.1 rjs printf("Calling ipv4 output routine from low level src addr:%x\n",
2241 1.1 rjs (u_int)(ntohl(ip->ip_src.s_addr)));
2242 1.1 rjs printf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2243 1.1 rjs printf("RTP route is %p through\n", rt);
2244 1.11 rjs printf("length %d\n", ntohs(ip->ip_len));
2245 1.1 rjs }
2246 1.1 rjs #endif
2247 1.1 rjs if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
2248 1.1 rjs rt->rt_ifp->if_mtu = net->mtu;
2249 1.1 rjs }
2250 1.1 rjs ret = ip_output(m, inp->ip_inp.inp.inp_options,
2251 1.1 rjs ro, o_flgs, inp->ip_inp.inp.inp_moptions,
2252 1.10 ozaki &inp->ip_inp.inp);
2253 1.1 rjs if ((rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
2254 1.1 rjs rt->rt_ifp->if_mtu = have_mtu;
2255 1.1 rjs }
2256 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2257 1.1 rjs #ifdef SCTP_DEBUG
2258 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2259 1.1 rjs printf("Ip output returns %d\n", ret);
2260 1.1 rjs }
2261 1.1 rjs #endif
2262 1.1 rjs if (net == NULL) {
2263 1.1 rjs } else {
2264 1.1 rjs /* PMTU check versus smallest asoc MTU goes here */
2265 1.1 rjs if (rt != NULL) {
2266 1.1 rjs if (rt->rt_rmx.rmx_mtu &&
2267 1.1 rjs (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
2268 1.1 rjs sctp_mtu_size_reset(inp, &stcb->asoc,
2269 1.1 rjs rt->rt_rmx.rmx_mtu);
2270 1.1 rjs }
2271 1.1 rjs } else {
2272 1.1 rjs /* route was freed */
2273 1.1 rjs net->src_addr_selected = 0;
2274 1.1 rjs }
2275 1.1 rjs }
2276 1.8 ozaki rtcache_unref(rt, ro);
2277 1.1 rjs return (ret);
2278 1.1 rjs }
2279 1.1 rjs #ifdef INET6
2280 1.1 rjs else if (to->sa_family == AF_INET6) {
2281 1.1 rjs struct ip6_hdr *ip6h;
2282 1.1 rjs static struct route ip6route;
2283 1.1 rjs struct ifnet *ifp;
2284 1.1 rjs u_char flowTop;
2285 1.1 rjs uint16_t flowBottom;
2286 1.1 rjs u_char tosBottom, tosTop;
2287 1.1 rjs struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
2288 1.1 rjs int prev_scope=0;
2289 1.1 rjs u_short prev_port=0;
2290 1.1 rjs
2291 1.1 rjs M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
2292 1.1 rjs if (m == NULL) {
2293 1.1 rjs /* failed to prepend data, give up */
2294 1.1 rjs return (ENOMEM);
2295 1.1 rjs }
2296 1.1 rjs ip6h = mtod(m, struct ip6_hdr *);
2297 1.1 rjs
2298 1.1 rjs /*
2299 1.1 rjs * We assume here that inp_flow is in host byte order within
2300 1.1 rjs * the TCB!
2301 1.1 rjs */
2302 1.1 rjs flowBottom = ((struct in6pcb *)inp)->in6p_flowinfo & 0x0000ffff;
2303 1.1 rjs flowTop = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x000f0000) >> 16);
2304 1.1 rjs
2305 1.1 rjs tosTop = (((((struct in6pcb *)inp)->in6p_flowinfo & 0xf0) >> 4) | IPV6_VERSION);
2306 1.1 rjs
2307 1.1 rjs /* protect *sin6 from overwrite */
2308 1.1 rjs memcpy(&tmp, to, sizeof(struct sockaddr_in6));
2309 1.1 rjs sin6 = &tmp;
2310 1.1 rjs
2311 1.1 rjs /* KAME hack: embed scopeid */
2312 1.1 rjs #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
2313 1.1 rjs if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
2314 1.1 rjs #else
2315 1.1 rjs /*
2316 1.1 rjs * XXX: appropriate scope zone must be provided or otherwise
2317 1.1 rjs * ip6_use_defzone must be 1.
2318 1.1 rjs */
2319 1.1 rjs if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2320 1.1 rjs #endif
2321 1.1 rjs return (EINVAL);
2322 1.1 rjs if (net == NULL) {
2323 1.1 rjs memset(&ip6route, 0, sizeof(ip6route));
2324 1.1 rjs ro = (struct route *)&ip6route;
2325 1.8 ozaki /* XXX */
2326 1.8 ozaki rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
2327 1.8 ozaki rtcache_unref(rt, ro);
2328 1.1 rjs } else {
2329 1.1 rjs ro = (struct route *)&net->ro;
2330 1.1 rjs }
2331 1.1 rjs if (stcb != NULL) {
2332 1.1 rjs if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2333 1.1 rjs /* Enable ECN */
2334 1.1 rjs tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
2335 1.1 rjs } else {
2336 1.1 rjs /* No ECN */
2337 1.1 rjs tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2338 1.1 rjs }
2339 1.1 rjs } else {
2340 1.1 rjs /* we could get no asoc if it is a O-O-T-B packet */
2341 1.1 rjs tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2342 1.1 rjs }
2343 1.1 rjs ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
2344 1.1 rjs ip6h->ip6_nxt = IPPROTO_SCTP;
2345 1.1 rjs ip6h->ip6_plen = m->m_pkthdr.len;
2346 1.1 rjs ip6h->ip6_dst = sin6->sin6_addr;
2347 1.1 rjs
2348 1.1 rjs /*
2349 1.1 rjs * Add SRC address selection here:
2350 1.1 rjs * we can only reuse to a limited degree the kame src-addr-sel,
2351 1.1 rjs * since we can try their selection but it may not be bound.
2352 1.1 rjs */
2353 1.1 rjs memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
2354 1.1 rjs lsa6_tmp.sin6_family = AF_INET6;
2355 1.1 rjs lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
2356 1.1 rjs lsa6 = &lsa6_tmp;
2357 1.1 rjs rt = rtcache_validate(ro);
2358 1.1 rjs if (net) {
2359 1.1 rjs if (net->src_addr_selected == 0) {
2360 1.1 rjs /* Cache the source address */
2361 1.1 rjs ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
2362 1.1 rjs stcb, ro, net, out_of_asoc_ok);
2363 1.1 rjs
2364 1.1 rjs if (rt != NULL) {
2365 1.1 rjs net->src_addr_selected = 1;
2366 1.1 rjs }
2367 1.1 rjs }
2368 1.1 rjs lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr;
2369 1.1 rjs } else {
2370 1.1 rjs lsa6->sin6_addr = sctp_ipv6_source_address_selection(
2371 1.1 rjs inp, stcb, ro, net, out_of_asoc_ok);
2372 1.1 rjs }
2373 1.1 rjs lsa6->sin6_port = inp->sctp_lport;
2374 1.1 rjs
2375 1.9 maya if (rt == NULL) {
2376 1.1 rjs /*
2377 1.1 rjs * src addr selection failed to find a route (or valid
2378 1.1 rjs * source addr), so we can't get there from here!
2379 1.1 rjs */
2380 1.1 rjs #ifdef SCTP_DEBUG
2381 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2382 1.1 rjs printf("low_level_output: dropped v6 pkt- no valid source addr\n");
2383 1.1 rjs }
2384 1.1 rjs #endif
2385 1.1 rjs sctp_m_freem(m);
2386 1.1 rjs if (net) {
2387 1.1 rjs if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2388 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2389 1.1 rjs stcb,
2390 1.1 rjs SCTP_FAILED_THRESHOLD,
2391 1.1 rjs (void *)net);
2392 1.1 rjs net->dest_state &= ~SCTP_ADDR_REACHABLE;
2393 1.1 rjs net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2394 1.1 rjs if (stcb) {
2395 1.1 rjs if (net == stcb->asoc.primary_destination) {
2396 1.1 rjs /* need a new primary */
2397 1.1 rjs struct sctp_nets *alt;
2398 1.1 rjs alt = sctp_find_alternate_net(stcb, net);
2399 1.1 rjs if (alt != net) {
2400 1.1 rjs if (sctp_set_primary_addr(stcb,
2401 1.1 rjs (struct sockaddr *)NULL,
2402 1.1 rjs alt) == 0) {
2403 1.1 rjs net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2404 1.1 rjs net->src_addr_selected = 0;
2405 1.1 rjs }
2406 1.1 rjs }
2407 1.1 rjs }
2408 1.1 rjs }
2409 1.1 rjs }
2410 1.1 rjs return (EHOSTUNREACH);
2411 1.1 rjs }
2412 1.1 rjs
2413 1.1 rjs ip6h->ip6_src = lsa6->sin6_addr;
2414 1.1 rjs
2415 1.1 rjs /*
2416 1.1 rjs * We set the hop limit now since there is a good chance that
2417 1.1 rjs * our ro pointer is now filled
2418 1.1 rjs */
2419 1.1 rjs ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
2420 1.1 rjs (ro ?
2421 1.1 rjs (rt ? (rt->rt_ifp) : (NULL)) :
2422 1.1 rjs (NULL)));
2423 1.1 rjs o_flgs = 0;
2424 1.1 rjs ifp = rt->rt_ifp;
2425 1.1 rjs #ifdef SCTP_DEBUG
2426 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2427 1.1 rjs /* Copy to be sure something bad is not happening */
2428 1.1 rjs sin6->sin6_addr = ip6h->ip6_dst;
2429 1.1 rjs lsa6->sin6_addr = ip6h->ip6_src;
2430 1.1 rjs
2431 1.1 rjs printf("Calling ipv6 output routine from low level\n");
2432 1.1 rjs printf("src: ");
2433 1.1 rjs sctp_print_address((struct sockaddr *)lsa6);
2434 1.1 rjs printf("dst: ");
2435 1.1 rjs sctp_print_address((struct sockaddr *)sin6);
2436 1.1 rjs }
2437 1.1 rjs #endif /* SCTP_DEBUG */
2438 1.1 rjs if (net) {
2439 1.1 rjs sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
2440 1.1 rjs /* preserve the port and scope for link local send */
2441 1.1 rjs prev_scope = sin6->sin6_scope_id;
2442 1.1 rjs prev_port = sin6->sin6_port;
2443 1.1 rjs }
2444 1.8 ozaki /* XXX NOMPSAFE need to hold ifp here */
2445 1.8 ozaki rtcache_unref(rt, ro);
2446 1.1 rjs ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
2447 1.1 rjs ro,
2448 1.1 rjs o_flgs,
2449 1.1 rjs ((struct in6pcb *)inp)->in6p_moptions,
2450 1.10 ozaki (struct in6pcb *)inp,
2451 1.1 rjs &ifp);
2452 1.1 rjs if (net) {
2453 1.1 rjs /* for link local this must be done */
2454 1.1 rjs sin6->sin6_scope_id = prev_scope;
2455 1.1 rjs sin6->sin6_port = prev_port;
2456 1.1 rjs }
2457 1.1 rjs #ifdef SCTP_DEBUG
2458 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2459 1.1 rjs printf("return from send is %d\n", ret);
2460 1.1 rjs }
2461 1.1 rjs #endif /* SCTP_DEBUG_OUTPUT */
2462 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2463 1.1 rjs if (net) {
2464 1.1 rjs /* PMTU check versus smallest asoc MTU goes here */
2465 1.1 rjs rt = rtcache_validate(ro);
2466 1.1 rjs if (rt == NULL) {
2467 1.1 rjs /* Route was freed */
2468 1.1 rjs net->src_addr_selected = 0;
2469 1.1 rjs }
2470 1.1 rjs if (rt != NULL) {
2471 1.1 rjs if (rt->rt_rmx.rmx_mtu &&
2472 1.1 rjs (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
2473 1.1 rjs sctp_mtu_size_reset(inp,
2474 1.1 rjs &stcb->asoc,
2475 1.1 rjs rt->rt_rmx.rmx_mtu);
2476 1.1 rjs }
2477 1.8 ozaki rtcache_unref(rt, ro);
2478 1.1 rjs } else if (ifp) {
2479 1.22 roy if (ifp->if_mtu &&
2480 1.22 roy (stcb->asoc.smallest_mtu > ifp->if_mtu)) {
2481 1.1 rjs sctp_mtu_size_reset(inp,
2482 1.1 rjs &stcb->asoc,
2483 1.22 roy ifp->if_mtu);
2484 1.1 rjs }
2485 1.1 rjs }
2486 1.1 rjs }
2487 1.1 rjs return (ret);
2488 1.1 rjs }
2489 1.1 rjs #endif
2490 1.1 rjs else {
2491 1.1 rjs #ifdef SCTP_DEBUG
2492 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2493 1.1 rjs printf("Unknown protocol (TSNH) type %d\n", ((const struct sockaddr *)to)->sa_family);
2494 1.1 rjs }
2495 1.1 rjs #endif
2496 1.1 rjs sctp_m_freem(m);
2497 1.1 rjs return (EFAULT);
2498 1.1 rjs }
2499 1.1 rjs }
2500 1.1 rjs
2501 1.1 rjs static
2502 1.1 rjs int sctp_is_address_in_scope(struct ifaddr *ifa,
2503 1.1 rjs int ipv4_addr_legal,
2504 1.1 rjs int ipv6_addr_legal,
2505 1.1 rjs int loopback_scope,
2506 1.1 rjs int ipv4_local_scope,
2507 1.1 rjs int local_scope,
2508 1.1 rjs int site_scope)
2509 1.1 rjs {
2510 1.19 ozaki if ((loopback_scope == 0) && (ifa->ifa_ifp->if_type == IFT_LOOP)) {
2511 1.1 rjs /* skip loopback if not in scope *
2512 1.1 rjs */
2513 1.1 rjs return (0);
2514 1.1 rjs }
2515 1.1 rjs if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
2516 1.1 rjs struct sockaddr_in *sin;
2517 1.1 rjs sin = (struct sockaddr_in *)ifa->ifa_addr;
2518 1.1 rjs if (sin->sin_addr.s_addr == 0) {
2519 1.1 rjs /* not in scope , unspecified */
2520 1.1 rjs return (0);
2521 1.1 rjs }
2522 1.1 rjs if ((ipv4_local_scope == 0) &&
2523 1.1 rjs (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
2524 1.1 rjs /* private address not in scope */
2525 1.1 rjs return (0);
2526 1.1 rjs }
2527 1.1 rjs } else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
2528 1.1 rjs struct sockaddr_in6 *sin6;
2529 1.1 rjs struct in6_ifaddr *ifa6;
2530 1.1 rjs
2531 1.1 rjs ifa6 = (struct in6_ifaddr *)ifa;
2532 1.1 rjs /* ok to use deprecated addresses? */
2533 1.1 rjs if (!ip6_use_deprecated) {
2534 1.1 rjs if (ifa6->ia6_flags &
2535 1.1 rjs IN6_IFF_DEPRECATED) {
2536 1.1 rjs return (0);
2537 1.1 rjs }
2538 1.1 rjs }
2539 1.1 rjs if (ifa6->ia6_flags &
2540 1.1 rjs (IN6_IFF_DETACHED |
2541 1.1 rjs IN6_IFF_ANYCAST |
2542 1.1 rjs IN6_IFF_NOTREADY)) {
2543 1.1 rjs return (0);
2544 1.1 rjs }
2545 1.1 rjs sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2546 1.1 rjs if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2547 1.1 rjs /* skip unspecifed addresses */
2548 1.1 rjs return (0);
2549 1.1 rjs }
2550 1.1 rjs if (/*(local_scope == 0) && */
2551 1.1 rjs (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
2552 1.1 rjs return (0);
2553 1.1 rjs }
2554 1.1 rjs if ((site_scope == 0) &&
2555 1.1 rjs (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
2556 1.1 rjs return (0);
2557 1.1 rjs }
2558 1.1 rjs } else {
2559 1.1 rjs return (0);
2560 1.1 rjs }
2561 1.1 rjs return (1);
2562 1.1 rjs }
2563 1.1 rjs
2564 1.1 rjs
2565 1.1 rjs void
2566 1.1 rjs sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2567 1.1 rjs {
2568 1.1 rjs struct mbuf *m, *m_at, *m_last;
2569 1.1 rjs struct sctp_nets *net;
2570 1.1 rjs struct sctp_init_msg *initm;
2571 1.1 rjs struct sctp_supported_addr_param *sup_addr;
2572 1.1 rjs struct sctp_ecn_supported_param *ecn;
2573 1.1 rjs struct sctp_prsctp_supported_param *prsctp;
2574 1.1 rjs struct sctp_ecn_nonce_supported_param *ecn_nonce;
2575 1.1 rjs struct sctp_supported_chunk_types_param *pr_supported;
2576 1.1 rjs int cnt_inits_to=0;
2577 1.1 rjs int padval, ret;
2578 1.1 rjs
2579 1.1 rjs /* INIT's always go to the primary (and usually ONLY address) */
2580 1.1 rjs m_last = NULL;
2581 1.1 rjs net = stcb->asoc.primary_destination;
2582 1.1 rjs if (net == NULL) {
2583 1.1 rjs net = TAILQ_FIRST(&stcb->asoc.nets);
2584 1.1 rjs if (net == NULL) {
2585 1.1 rjs /* TSNH */
2586 1.1 rjs return;
2587 1.1 rjs }
2588 1.1 rjs /* we confirm any address we send an INIT to */
2589 1.1 rjs net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2590 1.1 rjs sctp_set_primary_addr(stcb, NULL, net);
2591 1.1 rjs } else {
2592 1.1 rjs /* we confirm any address we send an INIT to */
2593 1.1 rjs net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2594 1.1 rjs }
2595 1.1 rjs #ifdef SCTP_DEBUG
2596 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2597 1.1 rjs printf("Sending INIT to ");
2598 1.1 rjs sctp_print_address (rtcache_getdst(&net->ro));
2599 1.1 rjs }
2600 1.1 rjs #endif
2601 1.1 rjs if (rtcache_getdst(&net->ro)->sa_family == AF_INET6) {
2602 1.1 rjs /* special hook, if we are sending to link local
2603 1.1 rjs * it will not show up in our private address count.
2604 1.1 rjs */
2605 1.1 rjs if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&net->ro)->sa_data))
2606 1.1 rjs cnt_inits_to = 1;
2607 1.1 rjs }
2608 1.1 rjs if (callout_pending(&net->rxt_timer.timer)) {
2609 1.1 rjs /* This case should not happen */
2610 1.1 rjs return;
2611 1.1 rjs }
2612 1.1 rjs /* start the INIT timer */
2613 1.1 rjs if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
2614 1.1 rjs /* we are hosed since I can't start the INIT timer? */
2615 1.1 rjs return;
2616 1.1 rjs }
2617 1.1 rjs MGETHDR(m, M_DONTWAIT, MT_HEADER);
2618 1.1 rjs if (m == NULL) {
2619 1.1 rjs /* No memory, INIT timer will re-attempt. */
2620 1.1 rjs return;
2621 1.1 rjs }
2622 1.1 rjs /* make it into a M_EXT */
2623 1.1 rjs MCLGET(m, M_DONTWAIT);
2624 1.1 rjs if ((m->m_flags & M_EXT) != M_EXT) {
2625 1.1 rjs /* Failed to get cluster buffer */
2626 1.1 rjs sctp_m_freem(m);
2627 1.1 rjs return;
2628 1.1 rjs }
2629 1.1 rjs m->m_data += SCTP_MIN_OVERHEAD;
2630 1.1 rjs m->m_len = sizeof(struct sctp_init_msg);
2631 1.1 rjs /* Now lets put the SCTP header in place */
2632 1.1 rjs initm = mtod(m, struct sctp_init_msg *);
2633 1.1 rjs initm->sh.src_port = inp->sctp_lport;
2634 1.1 rjs initm->sh.dest_port = stcb->rport;
2635 1.1 rjs initm->sh.v_tag = 0;
2636 1.1 rjs initm->sh.checksum = 0; /* calculate later */
2637 1.1 rjs /* now the chunk header */
2638 1.1 rjs initm->msg.ch.chunk_type = SCTP_INITIATION;
2639 1.1 rjs initm->msg.ch.chunk_flags = 0;
2640 1.1 rjs /* fill in later from mbuf we build */
2641 1.1 rjs initm->msg.ch.chunk_length = 0;
2642 1.1 rjs /* place in my tag */
2643 1.1 rjs initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
2644 1.1 rjs /* set up some of the credits. */
2645 1.16 riastrad initm->msg.init.a_rwnd = htonl(uimax(inp->sctp_socket->so_rcv.sb_hiwat,
2646 1.1 rjs SCTP_MINIMAL_RWND));
2647 1.1 rjs
2648 1.1 rjs initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
2649 1.1 rjs initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
2650 1.1 rjs initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
2651 1.1 rjs /* now the address restriction */
2652 1.1 rjs sup_addr = (struct sctp_supported_addr_param *)((vaddr_t)initm +
2653 1.1 rjs sizeof(*initm));
2654 1.1 rjs sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
2655 1.1 rjs /* we support 2 types IPv6/IPv4 */
2656 1.1 rjs sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
2657 1.1 rjs sizeof(uint16_t));
2658 1.1 rjs sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
2659 1.1 rjs sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
2660 1.1 rjs m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
2661 1.1 rjs
2662 1.1 rjs /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
2663 1.1 rjs if (inp->sctp_ep.adaption_layer_indicator) {
2664 1.1 rjs struct sctp_adaption_layer_indication *ali;
2665 1.1 rjs ali = (struct sctp_adaption_layer_indication *)(
2666 1.1 rjs (vaddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
2667 1.1 rjs ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
2668 1.1 rjs ali->ph.param_length = htons(sizeof(*ali));
2669 1.1 rjs ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
2670 1.1 rjs m->m_len += sizeof(*ali);
2671 1.1 rjs ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
2672 1.1 rjs sizeof(*ali));
2673 1.1 rjs } else {
2674 1.1 rjs ecn = (struct sctp_ecn_supported_param *)((vaddr_t)sup_addr +
2675 1.1 rjs sizeof(*sup_addr) + sizeof(uint16_t));
2676 1.1 rjs }
2677 1.1 rjs
2678 1.1 rjs /* now any cookie time extensions */
2679 1.1 rjs if (stcb->asoc.cookie_preserve_req) {
2680 1.1 rjs struct sctp_cookie_perserve_param *cookie_preserve;
2681 1.1 rjs cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
2682 1.1 rjs cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
2683 1.1 rjs cookie_preserve->ph.param_length = htons(
2684 1.1 rjs sizeof(*cookie_preserve));
2685 1.1 rjs cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
2686 1.1 rjs m->m_len += sizeof(*cookie_preserve);
2687 1.1 rjs ecn = (struct sctp_ecn_supported_param *)(
2688 1.1 rjs (vaddr_t)cookie_preserve + sizeof(*cookie_preserve));
2689 1.1 rjs stcb->asoc.cookie_preserve_req = 0;
2690 1.1 rjs }
2691 1.1 rjs
2692 1.1 rjs /* ECN parameter */
2693 1.1 rjs if (sctp_ecn == 1) {
2694 1.1 rjs ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
2695 1.1 rjs ecn->ph.param_length = htons(sizeof(*ecn));
2696 1.1 rjs m->m_len += sizeof(*ecn);
2697 1.1 rjs prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
2698 1.1 rjs sizeof(*ecn));
2699 1.1 rjs } else {
2700 1.1 rjs prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
2701 1.1 rjs }
2702 1.1 rjs /* And now tell the peer we do pr-sctp */
2703 1.1 rjs prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
2704 1.1 rjs prsctp->ph.param_length = htons(sizeof(*prsctp));
2705 1.1 rjs m->m_len += sizeof(*prsctp);
2706 1.1 rjs
2707 1.1 rjs
2708 1.1 rjs /* And now tell the peer we do all the extensions */
2709 1.1 rjs pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
2710 1.1 rjs sizeof(*prsctp));
2711 1.1 rjs
2712 1.1 rjs pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
2713 1.1 rjs pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
2714 1.1 rjs pr_supported->chunk_types[0] = SCTP_ASCONF;
2715 1.1 rjs pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
2716 1.1 rjs pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
2717 1.1 rjs pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
2718 1.1 rjs pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
2719 1.1 rjs pr_supported->chunk_types[5] = 0; /* pad */
2720 1.1 rjs pr_supported->chunk_types[6] = 0; /* pad */
2721 1.1 rjs pr_supported->chunk_types[7] = 0; /* pad */
2722 1.1 rjs
2723 1.1 rjs m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2724 1.1 rjs /* ECN nonce: And now tell the peer we support ECN nonce */
2725 1.1 rjs
2726 1.1 rjs if (sctp_ecn_nonce) {
2727 1.1 rjs ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
2728 1.1 rjs sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2729 1.1 rjs ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
2730 1.1 rjs ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
2731 1.1 rjs m->m_len += sizeof(*ecn_nonce);
2732 1.1 rjs }
2733 1.1 rjs
2734 1.1 rjs m_at = m;
2735 1.1 rjs /* now the addresses */
2736 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2737 1.1 rjs struct ifnet *ifn;
2738 1.1 rjs struct ifaddr *ifa;
2739 1.1 rjs int cnt;
2740 1.4 ozaki int s;
2741 1.1 rjs
2742 1.1 rjs cnt = cnt_inits_to;
2743 1.4 ozaki s = pserialize_read_enter();
2744 1.4 ozaki IFNET_READER_FOREACH(ifn) {
2745 1.1 rjs if ((stcb->asoc.loopback_scope == 0) &&
2746 1.1 rjs (ifn->if_type == IFT_LOOP)) {
2747 1.1 rjs /*
2748 1.1 rjs * Skip loopback devices if loopback_scope
2749 1.1 rjs * not set
2750 1.1 rjs */
2751 1.1 rjs continue;
2752 1.1 rjs }
2753 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
2754 1.1 rjs if (sctp_is_address_in_scope(ifa,
2755 1.1 rjs stcb->asoc.ipv4_addr_legal,
2756 1.1 rjs stcb->asoc.ipv6_addr_legal,
2757 1.1 rjs stcb->asoc.loopback_scope,
2758 1.1 rjs stcb->asoc.ipv4_local_scope,
2759 1.1 rjs stcb->asoc.local_scope,
2760 1.1 rjs stcb->asoc.site_scope) == 0) {
2761 1.1 rjs continue;
2762 1.1 rjs }
2763 1.1 rjs cnt++;
2764 1.1 rjs }
2765 1.1 rjs }
2766 1.4 ozaki pserialize_read_exit(s);
2767 1.4 ozaki
2768 1.1 rjs if (cnt > 1) {
2769 1.4 ozaki s = pserialize_read_enter();
2770 1.4 ozaki IFNET_READER_FOREACH(ifn) {
2771 1.1 rjs if ((stcb->asoc.loopback_scope == 0) &&
2772 1.1 rjs (ifn->if_type == IFT_LOOP)) {
2773 1.1 rjs /*
2774 1.1 rjs * Skip loopback devices if loopback_scope
2775 1.1 rjs * not set
2776 1.1 rjs */
2777 1.1 rjs continue;
2778 1.1 rjs }
2779 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
2780 1.1 rjs if (sctp_is_address_in_scope(ifa,
2781 1.1 rjs stcb->asoc.ipv4_addr_legal,
2782 1.1 rjs stcb->asoc.ipv6_addr_legal,
2783 1.1 rjs stcb->asoc.loopback_scope,
2784 1.1 rjs stcb->asoc.ipv4_local_scope,
2785 1.1 rjs stcb->asoc.local_scope,
2786 1.1 rjs stcb->asoc.site_scope) == 0) {
2787 1.1 rjs continue;
2788 1.1 rjs }
2789 1.1 rjs m_at = sctp_add_addr_to_mbuf(m_at, ifa);
2790 1.1 rjs }
2791 1.1 rjs }
2792 1.4 ozaki pserialize_read_exit(s);
2793 1.1 rjs }
2794 1.1 rjs } else {
2795 1.1 rjs struct sctp_laddr *laddr;
2796 1.1 rjs int cnt;
2797 1.1 rjs cnt = cnt_inits_to;
2798 1.1 rjs /* First, how many ? */
2799 1.1 rjs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2800 1.1 rjs if (laddr->ifa == NULL) {
2801 1.1 rjs continue;
2802 1.1 rjs }
2803 1.1 rjs if (laddr->ifa->ifa_addr == NULL)
2804 1.1 rjs continue;
2805 1.1 rjs if (sctp_is_address_in_scope(laddr->ifa,
2806 1.1 rjs stcb->asoc.ipv4_addr_legal,
2807 1.1 rjs stcb->asoc.ipv6_addr_legal,
2808 1.1 rjs stcb->asoc.loopback_scope,
2809 1.1 rjs stcb->asoc.ipv4_local_scope,
2810 1.1 rjs stcb->asoc.local_scope,
2811 1.1 rjs stcb->asoc.site_scope) == 0) {
2812 1.1 rjs continue;
2813 1.1 rjs }
2814 1.1 rjs cnt++;
2815 1.1 rjs }
2816 1.1 rjs /* To get through a NAT we only list addresses if
2817 1.1 rjs * we have more than one. That way if you just
2818 1.1 rjs * bind a single address we let the source of the init
2819 1.1 rjs * dictate our address.
2820 1.1 rjs */
2821 1.1 rjs if (cnt > 1) {
2822 1.1 rjs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2823 1.1 rjs if (laddr->ifa == NULL) {
2824 1.1 rjs continue;
2825 1.1 rjs }
2826 1.1 rjs if (laddr->ifa->ifa_addr == NULL) {
2827 1.1 rjs continue;
2828 1.1 rjs }
2829 1.1 rjs
2830 1.1 rjs if (sctp_is_address_in_scope(laddr->ifa,
2831 1.1 rjs stcb->asoc.ipv4_addr_legal,
2832 1.1 rjs stcb->asoc.ipv6_addr_legal,
2833 1.1 rjs stcb->asoc.loopback_scope,
2834 1.1 rjs stcb->asoc.ipv4_local_scope,
2835 1.1 rjs stcb->asoc.local_scope,
2836 1.1 rjs stcb->asoc.site_scope) == 0) {
2837 1.1 rjs continue;
2838 1.1 rjs }
2839 1.1 rjs m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2840 1.1 rjs }
2841 1.1 rjs }
2842 1.1 rjs }
2843 1.1 rjs /* calulate the size and update pkt header and chunk header */
2844 1.1 rjs m->m_pkthdr.len = 0;
2845 1.1 rjs for (m_at = m; m_at; m_at = m_at->m_next) {
2846 1.1 rjs if (m_at->m_next == NULL)
2847 1.1 rjs m_last = m_at;
2848 1.1 rjs m->m_pkthdr.len += m_at->m_len;
2849 1.1 rjs }
2850 1.1 rjs initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
2851 1.1 rjs sizeof(struct sctphdr)));
2852 1.1 rjs #ifdef SCTP_DEBUG
2853 1.1 rjs printf("chunk_length %d\n", ntohs(initm->msg.ch.chunk_length));
2854 1.1 rjs #endif
2855 1.1 rjs /* We pass 0 here to NOT set IP_DF if its IPv4, we
2856 1.1 rjs * ignore the return here since the timer will drive
2857 1.1 rjs * a retranmission.
2858 1.1 rjs */
2859 1.1 rjs
2860 1.1 rjs /* I don't expect this to execute but we will be safe here */
2861 1.1 rjs padval = m->m_pkthdr.len % 4;
2862 1.1 rjs if ((padval) && (m_last)) {
2863 1.1 rjs /* The compiler worries that m_last may not be
2864 1.1 rjs * set even though I think it is impossible :->
2865 1.1 rjs * however we add m_last here just in case.
2866 1.1 rjs */
2867 1.1 rjs ret = sctp_add_pad_tombuf(m_last, (4-padval));
2868 1.1 rjs if (ret) {
2869 1.1 rjs /* Houston we have a problem, no space */
2870 1.1 rjs sctp_m_freem(m);
2871 1.1 rjs return;
2872 1.1 rjs }
2873 1.1 rjs m->m_pkthdr.len += padval;
2874 1.1 rjs }
2875 1.1 rjs #ifdef SCTP_DEBUG
2876 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2877 1.1 rjs printf("Calling lowlevel output stcb:%p net:%p\n",
2878 1.1 rjs stcb, net);
2879 1.1 rjs }
2880 1.1 rjs #endif
2881 1.1 rjs ret = sctp_lowlevel_chunk_output(inp, stcb, net,
2882 1.1 rjs rtcache_getdst(&net->ro), m, 0, 0, NULL, 0);
2883 1.1 rjs #ifdef SCTP_DEBUG
2884 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2885 1.1 rjs printf("Low level output returns %d\n", ret);
2886 1.1 rjs }
2887 1.1 rjs #endif
2888 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
2889 1.1 rjs SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
2890 1.1 rjs }
2891 1.1 rjs
2892 1.1 rjs struct mbuf *
2893 1.1 rjs sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
2894 1.1 rjs int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
2895 1.1 rjs {
2896 1.1 rjs /* Given a mbuf containing an INIT or INIT-ACK
2897 1.1 rjs * with the param_offset being equal to the
2898 1.1 rjs * beginning of the params i.e. (iphlen + sizeof(struct sctp_init_msg)
2899 1.1 rjs * parse through the parameters to the end of the mbuf verifying
2900 1.1 rjs * that all parameters are known.
2901 1.1 rjs *
2902 1.1 rjs * For unknown parameters build and return a mbuf with
2903 1.1 rjs * UNRECOGNIZED_PARAMETER errors. If the flags indicate
2904 1.1 rjs * to stop processing this chunk stop, and set *abort_processing
2905 1.1 rjs * to 1.
2906 1.1 rjs *
2907 1.1 rjs * By having param_offset be pre-set to where parameters begin
2908 1.1 rjs * it is hoped that this routine may be reused in the future
2909 1.1 rjs * by new features.
2910 1.1 rjs */
2911 1.1 rjs struct sctp_paramhdr *phdr, params;
2912 1.1 rjs
2913 1.1 rjs struct mbuf *mat, *op_err;
2914 1.1 rjs char tempbuf[2048];
2915 1.1 rjs int at, limit, pad_needed;
2916 1.1 rjs uint16_t ptype, plen;
2917 1.1 rjs int err_at;
2918 1.1 rjs
2919 1.1 rjs *abort_processing = 0;
2920 1.1 rjs mat = in_initpkt;
2921 1.1 rjs err_at = 0;
2922 1.1 rjs limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
2923 1.1 rjs #ifdef SCTP_DEBUG
2924 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2925 1.1 rjs printf("Limit is %d bytes\n", limit);
2926 1.1 rjs }
2927 1.1 rjs #endif
2928 1.1 rjs at = param_offset;
2929 1.1 rjs op_err = NULL;
2930 1.1 rjs
2931 1.1 rjs phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
2932 1.1 rjs while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
2933 1.1 rjs ptype = ntohs(phdr->param_type);
2934 1.1 rjs plen = ntohs(phdr->param_length);
2935 1.1 rjs limit -= SCTP_SIZE32(plen);
2936 1.1 rjs if (plen < sizeof(struct sctp_paramhdr)) {
2937 1.1 rjs #ifdef SCTP_DEBUG
2938 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2939 1.1 rjs printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
2940 1.1 rjs }
2941 1.1 rjs #endif
2942 1.1 rjs *abort_processing = 1;
2943 1.1 rjs break;
2944 1.1 rjs }
2945 1.1 rjs /* All parameters for all chunks that we
2946 1.1 rjs * know/understand are listed here. We process
2947 1.1 rjs * them other places and make appropriate
2948 1.1 rjs * stop actions per the upper bits. However
2949 1.1 rjs * this is the generic routine processor's can
2950 1.1 rjs * call to get back an operr.. to either incorporate (init-ack)
2951 1.1 rjs * or send.
2952 1.1 rjs */
2953 1.1 rjs if ((ptype == SCTP_HEARTBEAT_INFO) ||
2954 1.1 rjs (ptype == SCTP_IPV4_ADDRESS) ||
2955 1.1 rjs (ptype == SCTP_IPV6_ADDRESS) ||
2956 1.1 rjs (ptype == SCTP_STATE_COOKIE) ||
2957 1.1 rjs (ptype == SCTP_UNRECOG_PARAM) ||
2958 1.1 rjs (ptype == SCTP_COOKIE_PRESERVE) ||
2959 1.1 rjs (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
2960 1.1 rjs (ptype == SCTP_PRSCTP_SUPPORTED) ||
2961 1.1 rjs (ptype == SCTP_ADD_IP_ADDRESS) ||
2962 1.1 rjs (ptype == SCTP_DEL_IP_ADDRESS) ||
2963 1.1 rjs (ptype == SCTP_ECN_CAPABLE) ||
2964 1.1 rjs (ptype == SCTP_ULP_ADAPTION) ||
2965 1.1 rjs (ptype == SCTP_ERROR_CAUSE_IND) ||
2966 1.1 rjs (ptype == SCTP_SET_PRIM_ADDR) ||
2967 1.1 rjs (ptype == SCTP_SUCCESS_REPORT) ||
2968 1.1 rjs (ptype == SCTP_ULP_ADAPTION) ||
2969 1.1 rjs (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
2970 1.1 rjs (ptype == SCTP_ECN_NONCE_SUPPORTED)
2971 1.1 rjs ) {
2972 1.1 rjs /* no skip it */
2973 1.1 rjs at += SCTP_SIZE32(plen);
2974 1.1 rjs } else if (ptype == SCTP_HOSTNAME_ADDRESS) {
2975 1.1 rjs /* We can NOT handle HOST NAME addresses!! */
2976 1.1 rjs #ifdef SCTP_DEBUG
2977 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2978 1.1 rjs printf("Can't handle hostname addresses.. abort processing\n");
2979 1.1 rjs }
2980 1.1 rjs #endif
2981 1.1 rjs *abort_processing = 1;
2982 1.1 rjs if (op_err == NULL) {
2983 1.1 rjs /* Ok need to try to get a mbuf */
2984 1.1 rjs MGETHDR(op_err, M_DONTWAIT, MT_DATA);
2985 1.1 rjs if (op_err) {
2986 1.1 rjs op_err->m_len = 0;
2987 1.1 rjs op_err->m_pkthdr.len = 0;
2988 1.1 rjs /* pre-reserve space for ip and sctp header and chunk hdr*/
2989 1.1 rjs op_err->m_data += sizeof(struct ip6_hdr);
2990 1.1 rjs op_err->m_data += sizeof(struct sctphdr);
2991 1.1 rjs op_err->m_data += sizeof(struct sctp_chunkhdr);
2992 1.1 rjs }
2993 1.1 rjs }
2994 1.1 rjs if (op_err) {
2995 1.1 rjs /* If we have space */
2996 1.1 rjs struct sctp_paramhdr s;
2997 1.1 rjs if (err_at % 4) {
2998 1.1 rjs u_int32_t cpthis=0;
2999 1.1 rjs pad_needed = 4 - (err_at % 4);
3000 1.1 rjs m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
3001 1.1 rjs err_at += pad_needed;
3002 1.1 rjs }
3003 1.1 rjs s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
3004 1.1 rjs s.param_length = htons(sizeof(s) + plen);
3005 1.1 rjs m_copyback(op_err, err_at, sizeof(s), (void *)&s);
3006 1.1 rjs err_at += sizeof(s);
3007 1.1 rjs phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3008 1.1 rjs if (phdr == NULL) {
3009 1.1 rjs sctp_m_freem(op_err);
3010 1.1 rjs /* we are out of memory but we
3011 1.1 rjs * still need to have a look at what to
3012 1.1 rjs * do (the system is in trouble though).
3013 1.1 rjs */
3014 1.1 rjs return (NULL);
3015 1.1 rjs }
3016 1.1 rjs m_copyback(op_err, err_at, plen, (void *)phdr);
3017 1.1 rjs err_at += plen;
3018 1.1 rjs }
3019 1.1 rjs return (op_err);
3020 1.1 rjs } else {
3021 1.1 rjs /* we do not recognize the parameter
3022 1.1 rjs * figure out what we do.
3023 1.1 rjs */
3024 1.1 rjs #ifdef SCTP_DEBUG
3025 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3026 1.1 rjs printf("Got parameter type %x - unknown\n",
3027 1.1 rjs (u_int)ptype);
3028 1.1 rjs }
3029 1.1 rjs #endif
3030 1.1 rjs if ((ptype & 0x4000) == 0x4000) {
3031 1.1 rjs /* Report bit is set?? */
3032 1.1 rjs #ifdef SCTP_DEBUG
3033 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3034 1.1 rjs printf("Report bit is set\n");
3035 1.1 rjs }
3036 1.1 rjs #endif
3037 1.1 rjs if (op_err == NULL) {
3038 1.1 rjs /* Ok need to try to get an mbuf */
3039 1.1 rjs MGETHDR(op_err, M_DONTWAIT, MT_DATA);
3040 1.1 rjs if (op_err) {
3041 1.1 rjs op_err->m_len = 0;
3042 1.1 rjs op_err->m_pkthdr.len = 0;
3043 1.1 rjs op_err->m_data += sizeof(struct ip6_hdr);
3044 1.1 rjs op_err->m_data += sizeof(struct sctphdr);
3045 1.1 rjs op_err->m_data += sizeof(struct sctp_chunkhdr);
3046 1.1 rjs }
3047 1.1 rjs }
3048 1.1 rjs if (op_err) {
3049 1.1 rjs /* If we have space */
3050 1.1 rjs struct sctp_paramhdr s;
3051 1.1 rjs if (err_at % 4) {
3052 1.1 rjs u_int32_t cpthis=0;
3053 1.1 rjs pad_needed = 4 - (err_at % 4);
3054 1.1 rjs m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
3055 1.1 rjs err_at += pad_needed;
3056 1.1 rjs }
3057 1.1 rjs s.param_type = htons(SCTP_UNRECOG_PARAM);
3058 1.1 rjs s.param_length = htons(sizeof(s) + plen);
3059 1.1 rjs m_copyback(op_err, err_at, sizeof(s), (void *)&s);
3060 1.1 rjs err_at += sizeof(s);
3061 1.1 rjs if (plen > sizeof(tempbuf)) {
3062 1.1 rjs plen = sizeof(tempbuf);
3063 1.1 rjs }
3064 1.1 rjs phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3065 1.1 rjs if (phdr == NULL) {
3066 1.1 rjs sctp_m_freem(op_err);
3067 1.1 rjs /* we are out of memory but we
3068 1.1 rjs * still need to have a look at what to
3069 1.1 rjs * do (the system is in trouble though).
3070 1.1 rjs */
3071 1.1 rjs goto more_processing;
3072 1.1 rjs }
3073 1.1 rjs m_copyback(op_err, err_at, plen, (void *)phdr);
3074 1.1 rjs err_at += plen;
3075 1.1 rjs }
3076 1.1 rjs }
3077 1.1 rjs more_processing:
3078 1.1 rjs if ((ptype & 0x8000) == 0x0000) {
3079 1.1 rjs #ifdef SCTP_DEBUG
3080 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3081 1.1 rjs printf("Abort bit is now setting1\n");
3082 1.1 rjs }
3083 1.1 rjs #endif
3084 1.1 rjs return (op_err);
3085 1.1 rjs } else {
3086 1.1 rjs /* skip this chunk and continue processing */
3087 1.1 rjs at += SCTP_SIZE32(plen);
3088 1.1 rjs }
3089 1.1 rjs
3090 1.1 rjs }
3091 1.1 rjs phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
3092 1.1 rjs }
3093 1.1 rjs return (op_err);
3094 1.1 rjs }
3095 1.1 rjs
3096 1.1 rjs static int
3097 1.1 rjs sctp_are_there_new_addresses(struct sctp_association *asoc,
3098 1.1 rjs struct mbuf *in_initpkt, int iphlen, int offset)
3099 1.1 rjs {
3100 1.1 rjs /*
3101 1.1 rjs * Given a INIT packet, look through the packet to verify that
3102 1.1 rjs * there are NO new addresses. As we go through the parameters
3103 1.1 rjs * add reports of any un-understood parameters that require an
3104 1.1 rjs * error. Also we must return (1) to drop the packet if we see
3105 1.1 rjs * a un-understood parameter that tells us to drop the chunk.
3106 1.1 rjs */
3107 1.1 rjs struct sockaddr_in sin4, *sa4;
3108 1.1 rjs struct sockaddr_in6 sin6, *sa6;
3109 1.1 rjs struct sockaddr *sa_touse;
3110 1.1 rjs struct sockaddr *sa;
3111 1.1 rjs struct sctp_paramhdr *phdr, params;
3112 1.1 rjs struct ip *iph;
3113 1.1 rjs struct mbuf *mat;
3114 1.1 rjs uint16_t ptype, plen;
3115 1.1 rjs uint8_t fnd;
3116 1.1 rjs struct sctp_nets *net;
3117 1.1 rjs
3118 1.1 rjs memset(&sin4, 0, sizeof(sin4));
3119 1.1 rjs memset(&sin6, 0, sizeof(sin6));
3120 1.1 rjs sin4.sin_family = AF_INET;
3121 1.1 rjs sin4.sin_len = sizeof(sin4);
3122 1.1 rjs sin6.sin6_family = AF_INET6;
3123 1.1 rjs sin6.sin6_len = sizeof(sin6);
3124 1.1 rjs
3125 1.1 rjs sa_touse = NULL;
3126 1.1 rjs /* First what about the src address of the pkt ? */
3127 1.1 rjs iph = mtod(in_initpkt, struct ip *);
3128 1.1 rjs if (iph->ip_v == IPVERSION) {
3129 1.1 rjs /* source addr is IPv4 */
3130 1.1 rjs sin4.sin_addr = iph->ip_src;
3131 1.1 rjs sa_touse = (struct sockaddr *)&sin4;
3132 1.1 rjs } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3133 1.1 rjs /* source addr is IPv6 */
3134 1.1 rjs struct ip6_hdr *ip6h;
3135 1.1 rjs ip6h = mtod(in_initpkt, struct ip6_hdr *);
3136 1.1 rjs sin6.sin6_addr = ip6h->ip6_src;
3137 1.1 rjs sa_touse = (struct sockaddr *)&sin6;
3138 1.1 rjs } else {
3139 1.1 rjs return (1);
3140 1.1 rjs }
3141 1.1 rjs
3142 1.1 rjs fnd = 0;
3143 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3144 1.1 rjs sa = (struct sockaddr *)&net->ro.ro_sa;
3145 1.1 rjs if (sa->sa_family == sa_touse->sa_family) {
3146 1.1 rjs if (sa->sa_family == AF_INET) {
3147 1.1 rjs sa4 = (struct sockaddr_in *)sa;
3148 1.1 rjs if (sa4->sin_addr.s_addr ==
3149 1.1 rjs sin4.sin_addr.s_addr) {
3150 1.1 rjs fnd = 1;
3151 1.1 rjs break;
3152 1.1 rjs }
3153 1.1 rjs } else if (sa->sa_family == AF_INET6) {
3154 1.1 rjs sa6 = (struct sockaddr_in6 *)sa;
3155 1.1 rjs if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
3156 1.1 rjs &sin6.sin6_addr)) {
3157 1.1 rjs fnd = 1;
3158 1.1 rjs break;
3159 1.1 rjs }
3160 1.1 rjs }
3161 1.1 rjs }
3162 1.1 rjs }
3163 1.1 rjs if (fnd == 0) {
3164 1.28 msaitoh /* New address added! no need to look further. */
3165 1.1 rjs return (1);
3166 1.1 rjs }
3167 1.1 rjs /* Ok so far lets munge through the rest of the packet */
3168 1.1 rjs mat = in_initpkt;
3169 1.1 rjs sa_touse = NULL;
3170 1.1 rjs offset += sizeof(struct sctp_init_chunk);
3171 1.1 rjs phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3172 1.1 rjs while (phdr) {
3173 1.1 rjs ptype = ntohs(phdr->param_type);
3174 1.1 rjs plen = ntohs(phdr->param_length);
3175 1.1 rjs if (ptype == SCTP_IPV4_ADDRESS) {
3176 1.1 rjs struct sctp_ipv4addr_param *p4, p4_buf;
3177 1.1 rjs
3178 1.1 rjs phdr = sctp_get_next_param(mat, offset,
3179 1.1 rjs (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
3180 1.1 rjs if (plen != sizeof(struct sctp_ipv4addr_param) ||
3181 1.1 rjs phdr == NULL) {
3182 1.1 rjs return (1);
3183 1.1 rjs }
3184 1.1 rjs p4 = (struct sctp_ipv4addr_param *)phdr;
3185 1.1 rjs sin4.sin_addr.s_addr = p4->addr;
3186 1.1 rjs sa_touse = (struct sockaddr *)&sin4;
3187 1.1 rjs } else if (ptype == SCTP_IPV6_ADDRESS) {
3188 1.1 rjs struct sctp_ipv6addr_param *p6, p6_buf;
3189 1.1 rjs
3190 1.1 rjs phdr = sctp_get_next_param(mat, offset,
3191 1.1 rjs (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
3192 1.1 rjs if (plen != sizeof(struct sctp_ipv6addr_param) ||
3193 1.1 rjs phdr == NULL) {
3194 1.1 rjs return (1);
3195 1.1 rjs }
3196 1.1 rjs p6 = (struct sctp_ipv6addr_param *)phdr;
3197 1.1 rjs memcpy((void *)&sin6.sin6_addr, p6->addr,
3198 1.1 rjs sizeof(p6->addr));
3199 1.1 rjs sa_touse = (struct sockaddr *)&sin4;
3200 1.1 rjs }
3201 1.1 rjs
3202 1.1 rjs if (sa_touse) {
3203 1.1 rjs /* ok, sa_touse points to one to check */
3204 1.1 rjs fnd = 0;
3205 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3206 1.1 rjs sa = (struct sockaddr *)&net->ro.ro_sa;
3207 1.1 rjs if (sa->sa_family != sa_touse->sa_family) {
3208 1.1 rjs continue;
3209 1.1 rjs }
3210 1.1 rjs if (sa->sa_family == AF_INET) {
3211 1.1 rjs sa4 = (struct sockaddr_in *)sa;
3212 1.1 rjs if (sa4->sin_addr.s_addr ==
3213 1.1 rjs sin4.sin_addr.s_addr) {
3214 1.1 rjs fnd = 1;
3215 1.1 rjs break;
3216 1.1 rjs }
3217 1.1 rjs } else if (sa->sa_family == AF_INET6) {
3218 1.1 rjs sa6 = (struct sockaddr_in6 *)sa;
3219 1.1 rjs if (SCTP6_ARE_ADDR_EQUAL(
3220 1.1 rjs &sa6->sin6_addr, &sin6.sin6_addr)) {
3221 1.1 rjs fnd = 1;
3222 1.1 rjs break;
3223 1.1 rjs }
3224 1.1 rjs }
3225 1.1 rjs }
3226 1.1 rjs if (!fnd) {
3227 1.1 rjs /* New addr added! no need to look further */
3228 1.1 rjs return (1);
3229 1.1 rjs }
3230 1.1 rjs }
3231 1.1 rjs offset += SCTP_SIZE32(plen);
3232 1.1 rjs phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3233 1.1 rjs }
3234 1.1 rjs return (0);
3235 1.1 rjs }
3236 1.1 rjs
3237 1.1 rjs /*
3238 1.1 rjs * Given a MBUF chain that was sent into us containing an
3239 1.1 rjs * INIT. Build a INIT-ACK with COOKIE and send back.
3240 1.1 rjs * We assume that the in_initpkt has done a pullup to
3241 1.1 rjs * include IPv6/4header, SCTP header and initial part of
3242 1.1 rjs * INIT message (i.e. the struct sctp_init_msg).
3243 1.1 rjs */
3244 1.1 rjs void
3245 1.1 rjs sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3246 1.1 rjs struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
3247 1.1 rjs struct sctp_init_chunk *init_chk)
3248 1.1 rjs {
3249 1.1 rjs struct sctp_association *asoc;
3250 1.1 rjs struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
3251 1.1 rjs struct sctp_init_msg *initackm_out;
3252 1.1 rjs struct sctp_ecn_supported_param *ecn;
3253 1.1 rjs struct sctp_prsctp_supported_param *prsctp;
3254 1.1 rjs struct sctp_ecn_nonce_supported_param *ecn_nonce;
3255 1.1 rjs struct sctp_supported_chunk_types_param *pr_supported;
3256 1.1 rjs struct sockaddr_storage store;
3257 1.1 rjs struct sockaddr_in *sin;
3258 1.1 rjs struct sockaddr_in6 *sin6;
3259 1.1 rjs struct route *ro;
3260 1.1 rjs struct ip *iph;
3261 1.1 rjs struct ip6_hdr *ip6;
3262 1.1 rjs const struct sockaddr *to;
3263 1.1 rjs struct sctp_state_cookie stc;
3264 1.1 rjs struct sctp_nets *net=NULL;
3265 1.1 rjs int cnt_inits_to=0;
3266 1.1 rjs uint16_t his_limit, i_want;
3267 1.1 rjs int abort_flag, padval, sz_of;
3268 1.8 ozaki struct rtentry *rt;
3269 1.1 rjs
3270 1.1 rjs if (stcb) {
3271 1.1 rjs asoc = &stcb->asoc;
3272 1.1 rjs } else {
3273 1.1 rjs asoc = NULL;
3274 1.1 rjs }
3275 1.1 rjs m_last = NULL;
3276 1.1 rjs if ((asoc != NULL) &&
3277 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
3278 1.1 rjs (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
3279 1.1 rjs /* new addresses, out of here in non-cookie-wait states */
3280 1.1 rjs /*
3281 1.1 rjs * Send a ABORT, we don't add the new address error clause though
3282 1.1 rjs * we even set the T bit and copy in the 0 tag.. this looks no
3283 1.1 rjs * different than if no listner was present.
3284 1.1 rjs */
3285 1.1 rjs sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3286 1.1 rjs return;
3287 1.1 rjs }
3288 1.1 rjs abort_flag = 0;
3289 1.1 rjs op_err = sctp_arethere_unrecognized_parameters(init_pkt,
3290 1.1 rjs (offset+sizeof(struct sctp_init_chunk)),
3291 1.1 rjs &abort_flag, (struct sctp_chunkhdr *)init_chk);
3292 1.1 rjs if (abort_flag) {
3293 1.1 rjs sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
3294 1.1 rjs return;
3295 1.1 rjs }
3296 1.1 rjs MGETHDR(m, M_DONTWAIT, MT_HEADER);
3297 1.1 rjs if (m == NULL) {
3298 1.1 rjs /* No memory, INIT timer will re-attempt. */
3299 1.1 rjs if (op_err)
3300 1.1 rjs sctp_m_freem(op_err);
3301 1.1 rjs return;
3302 1.1 rjs }
3303 1.1 rjs MCLGET(m, M_DONTWAIT);
3304 1.1 rjs if ((m->m_flags & M_EXT) != M_EXT) {
3305 1.1 rjs /* Failed to get cluster buffer */
3306 1.1 rjs if (op_err)
3307 1.1 rjs sctp_m_freem(op_err);
3308 1.1 rjs sctp_m_freem(m);
3309 1.1 rjs return;
3310 1.1 rjs }
3311 1.1 rjs m->m_data += SCTP_MIN_OVERHEAD;
3312 1.5 ozaki m_reset_rcvif(m);
3313 1.1 rjs m->m_len = sizeof(struct sctp_init_msg);
3314 1.1 rjs
3315 1.1 rjs /* the time I built cookie */
3316 1.1 rjs SCTP_GETTIME_TIMEVAL(&stc.time_entered);
3317 1.1 rjs
3318 1.1 rjs /* populate any tie tags */
3319 1.1 rjs if (asoc != NULL) {
3320 1.1 rjs /* unlock before tag selections */
3321 1.1 rjs SCTP_TCB_UNLOCK(stcb);
3322 1.1 rjs if (asoc->my_vtag_nonce == 0)
3323 1.1 rjs asoc->my_vtag_nonce = sctp_select_a_tag(inp);
3324 1.1 rjs stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
3325 1.1 rjs
3326 1.1 rjs if (asoc->peer_vtag_nonce == 0)
3327 1.1 rjs asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
3328 1.1 rjs stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
3329 1.1 rjs
3330 1.1 rjs stc.cookie_life = asoc->cookie_life;
3331 1.1 rjs net = asoc->primary_destination;
3332 1.1 rjs /* now we must relock */
3333 1.1 rjs SCTP_INP_RLOCK(inp);
3334 1.1 rjs /* we may be in trouble here if the inp got freed
3335 1.1 rjs * most likely this set of tests will protect
3336 1.1 rjs * us but there is a chance not.
3337 1.1 rjs */
3338 1.1 rjs if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3339 1.1 rjs if (op_err)
3340 1.1 rjs sctp_m_freem(op_err);
3341 1.1 rjs sctp_m_freem(m);
3342 1.1 rjs sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3343 1.1 rjs return;
3344 1.1 rjs }
3345 1.1 rjs SCTP_TCB_LOCK(stcb);
3346 1.1 rjs SCTP_INP_RUNLOCK(stcb->sctp_ep);
3347 1.1 rjs } else {
3348 1.1 rjs stc.tie_tag_my_vtag = 0;
3349 1.1 rjs stc.tie_tag_peer_vtag = 0;
3350 1.1 rjs /* life I will award this cookie */
3351 1.1 rjs stc.cookie_life = inp->sctp_ep.def_cookie_life;
3352 1.1 rjs }
3353 1.1 rjs
3354 1.1 rjs /* copy in the ports for later check */
3355 1.1 rjs stc.myport = sh->dest_port;
3356 1.1 rjs stc.peerport = sh->src_port;
3357 1.1 rjs
3358 1.1 rjs /*
3359 1.24 andvar * If we wanted to honor cookie life extensions, we would add
3360 1.1 rjs * to stc.cookie_life. For now we should NOT honor any extension
3361 1.1 rjs */
3362 1.1 rjs stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
3363 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3364 1.1 rjs struct inpcb *in_inp;
3365 1.1 rjs /* Its a V6 socket */
3366 1.1 rjs in_inp = (struct inpcb *)inp;
3367 1.1 rjs stc.ipv6_addr_legal = 1;
3368 1.1 rjs /* Now look at the binding flag to see if V4 will be legal */
3369 1.1 rjs if (
3370 1.1 rjs #if defined(__FreeBSD__) || defined(__APPLE__)
3371 1.1 rjs (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
3372 1.1 rjs #elif defined(__OpenBSD__)
3373 1.1 rjs (0) /* For openbsd we do dual bind only */
3374 1.1 rjs #else
3375 1.1 rjs (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
3376 1.1 rjs #endif
3377 1.1 rjs == 0) {
3378 1.1 rjs stc.ipv4_addr_legal = 1;
3379 1.1 rjs } else {
3380 1.1 rjs /* V4 addresses are NOT legal on the association */
3381 1.1 rjs stc.ipv4_addr_legal = 0;
3382 1.1 rjs }
3383 1.1 rjs } else {
3384 1.1 rjs /* Its a V4 socket, no - V6 */
3385 1.1 rjs stc.ipv4_addr_legal = 1;
3386 1.1 rjs stc.ipv6_addr_legal = 0;
3387 1.1 rjs }
3388 1.1 rjs
3389 1.1 rjs #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3390 1.1 rjs stc.ipv4_scope = 1;
3391 1.1 rjs #else
3392 1.1 rjs stc.ipv4_scope = 0;
3393 1.1 rjs #endif
3394 1.1 rjs /* now for scope setup */
3395 1.1 rjs memset((void *)&store, 0, sizeof(store));
3396 1.1 rjs sin = (struct sockaddr_in *)&store;
3397 1.1 rjs sin6 = (struct sockaddr_in6 *)&store;
3398 1.1 rjs if (net == NULL) {
3399 1.1 rjs to = (struct sockaddr *)&store;
3400 1.1 rjs iph = mtod(init_pkt, struct ip *);
3401 1.1 rjs if (iph->ip_v == IPVERSION) {
3402 1.1 rjs struct in_addr addr;
3403 1.1 rjs static struct route iproute;
3404 1.1 rjs
3405 1.1 rjs sin->sin_family = AF_INET;
3406 1.1 rjs sin->sin_len = sizeof(struct sockaddr_in);
3407 1.1 rjs sin->sin_port = sh->src_port;
3408 1.1 rjs sin->sin_addr = iph->ip_src;
3409 1.1 rjs /* lookup address */
3410 1.1 rjs stc.address[0] = sin->sin_addr.s_addr;
3411 1.1 rjs stc.address[1] = 0;
3412 1.1 rjs stc.address[2] = 0;
3413 1.1 rjs stc.address[3] = 0;
3414 1.1 rjs stc.addr_type = SCTP_IPV4_ADDRESS;
3415 1.1 rjs /* local from address */
3416 1.1 rjs memset(&iproute, 0, sizeof(iproute));
3417 1.1 rjs ro = &iproute;
3418 1.1 rjs
3419 1.8 ozaki /* XXX */
3420 1.8 ozaki rt = rtcache_lookup(ro, (struct sockaddr *) sin);
3421 1.8 ozaki rtcache_unref(rt, ro);
3422 1.1 rjs addr = sctp_ipv4_source_address_selection(inp, NULL,
3423 1.1 rjs ro, NULL, 0);
3424 1.1 rjs stc.laddress[0] = addr.s_addr;
3425 1.1 rjs stc.laddress[1] = 0;
3426 1.1 rjs stc.laddress[2] = 0;
3427 1.1 rjs stc.laddress[3] = 0;
3428 1.1 rjs stc.laddr_type = SCTP_IPV4_ADDRESS;
3429 1.1 rjs /* scope_id is only for v6 */
3430 1.1 rjs stc.scope_id = 0;
3431 1.1 rjs #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3432 1.1 rjs if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3433 1.1 rjs stc.ipv4_scope = 1;
3434 1.1 rjs }
3435 1.1 rjs #else
3436 1.1 rjs stc.ipv4_scope = 1;
3437 1.1 rjs #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3438 1.1 rjs /* Must use the address in this case */
3439 1.1 rjs if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3440 1.1 rjs stc.loopback_scope = 1;
3441 1.1 rjs stc.ipv4_scope = 1;
3442 1.1 rjs stc.site_scope = 1;
3443 1.1 rjs stc.local_scope = 1;
3444 1.1 rjs }
3445 1.1 rjs } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3446 1.1 rjs struct in6_addr addr;
3447 1.1 rjs static struct route iproute6;
3448 1.1 rjs ip6 = mtod(init_pkt, struct ip6_hdr *);
3449 1.1 rjs sin6->sin6_family = AF_INET6;
3450 1.1 rjs sin6->sin6_len = sizeof(struct sockaddr_in6);
3451 1.1 rjs sin6->sin6_port = sh->src_port;
3452 1.1 rjs sin6->sin6_addr = ip6->ip6_src;
3453 1.1 rjs /* lookup address */
3454 1.1 rjs memcpy(&stc.address, &sin6->sin6_addr,
3455 1.1 rjs sizeof(struct in6_addr));
3456 1.1 rjs sin6->sin6_scope_id = 0;
3457 1.1 rjs stc.addr_type = SCTP_IPV6_ADDRESS;
3458 1.1 rjs stc.scope_id = 0;
3459 1.1 rjs if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3460 1.1 rjs stc.loopback_scope = 1;
3461 1.1 rjs stc.local_scope = 1;
3462 1.1 rjs stc.site_scope = 1;
3463 1.1 rjs stc.ipv4_scope = 1;
3464 1.1 rjs } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3465 1.1 rjs /*
3466 1.1 rjs * If the new destination is a LINK_LOCAL
3467 1.1 rjs * we must have common both site and local
3468 1.1 rjs * scope. Don't set local scope though since
3469 1.1 rjs * we must depend on the source to be added
3470 1.1 rjs * implicitly. We cannot assure just because
3471 1.1 rjs * we share one link that all links are common.
3472 1.1 rjs *
3473 1.1 rjs * XXX: never treat link-local case explicitly.
3474 1.1 rjs * Use general routines defined in scope6.c.
3475 1.1 rjs * (jinmei@kame)
3476 1.1 rjs */
3477 1.1 rjs stc.local_scope = 0;
3478 1.1 rjs stc.site_scope = 1;
3479 1.1 rjs stc.ipv4_scope = 1;
3480 1.1 rjs /* we start counting for the private
3481 1.1 rjs * address stuff at 1. since the link
3482 1.1 rjs * local we source from won't show
3483 1.1 rjs * up in our scoped count.
3484 1.1 rjs */
3485 1.1 rjs cnt_inits_to=1;
3486 1.1 rjs /* pull out the scope_id from incoming pkt */
3487 1.1 rjs #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
3488 1.1 rjs (void)in6_recoverscope(sin6, &in6_src,
3489 1.6 ozaki m_get_rcvif_NOMPSAFE(init_pkt));
3490 1.1 rjs in6_embedscope(&sin6->sin6_addr, sin6, NULL,
3491 1.1 rjs NULL);
3492 1.1 rjs #else
3493 1.1 rjs (void)sa6_recoverscope(sin6);
3494 1.1 rjs #endif
3495 1.1 rjs stc.scope_id = sin6->sin6_scope_id;
3496 1.1 rjs
3497 1.1 rjs } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3498 1.1 rjs /*
3499 1.1 rjs * If the new destination is SITE_LOCAL
3500 1.1 rjs * then we must have site scope in common.
3501 1.1 rjs */
3502 1.1 rjs stc.site_scope = 1;
3503 1.1 rjs }
3504 1.1 rjs /* local from address */
3505 1.1 rjs memset(&iproute6, 0, sizeof(iproute6));
3506 1.1 rjs ro = (struct route *)&iproute6;
3507 1.8 ozaki /* XXX */
3508 1.8 ozaki rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
3509 1.8 ozaki rtcache_unref(rt, ro);
3510 1.1 rjs addr = sctp_ipv6_source_address_selection(inp, NULL,
3511 1.1 rjs ro, NULL, 0);
3512 1.1 rjs memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3513 1.1 rjs stc.laddr_type = SCTP_IPV6_ADDRESS;
3514 1.1 rjs }
3515 1.1 rjs } else {
3516 1.1 rjs /* set the scope per the existing tcb */
3517 1.1 rjs struct sctp_nets *lnet;
3518 1.1 rjs
3519 1.1 rjs stc.loopback_scope = asoc->loopback_scope;
3520 1.1 rjs stc.ipv4_scope = asoc->ipv4_local_scope;
3521 1.1 rjs stc.site_scope = asoc->site_scope;
3522 1.1 rjs stc.local_scope = asoc->local_scope;
3523 1.1 rjs TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3524 1.1 rjs if (rtcache_getdst(&lnet->ro)->sa_family == AF_INET6) {
3525 1.1 rjs if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&lnet->ro)->sa_data)) {
3526 1.1 rjs /* if we have a LL address, start counting
3527 1.1 rjs * at 1.
3528 1.1 rjs */
3529 1.1 rjs cnt_inits_to = 1;
3530 1.1 rjs }
3531 1.1 rjs }
3532 1.1 rjs }
3533 1.1 rjs
3534 1.1 rjs /* use the net pointer */
3535 1.1 rjs to = rtcache_getdst(&net->ro);
3536 1.1 rjs if (to->sa_family == AF_INET) {
3537 1.1 rjs memcpy(&stc.address[0], to, sizeof(struct in_addr));
3538 1.1 rjs stc.address[1] = 0;
3539 1.1 rjs stc.address[2] = 0;
3540 1.1 rjs stc.address[3] = 0;
3541 1.1 rjs stc.addr_type = SCTP_IPV4_ADDRESS;
3542 1.1 rjs if (net->src_addr_selected == 0) {
3543 1.1 rjs /* strange case here, the INIT
3544 1.1 rjs * should have did the selection.
3545 1.1 rjs */
3546 1.1 rjs net->_s_addr.sin.sin_addr =
3547 1.1 rjs sctp_ipv4_source_address_selection(inp,
3548 1.1 rjs stcb, &net->ro, net, 0);
3549 1.1 rjs net->src_addr_selected = 1;
3550 1.1 rjs
3551 1.1 rjs }
3552 1.1 rjs
3553 1.1 rjs stc.laddress[0] = net->_s_addr.sin.sin_addr.s_addr;
3554 1.1 rjs stc.laddress[1] = 0;
3555 1.1 rjs stc.laddress[2] = 0;
3556 1.1 rjs stc.laddress[3] = 0;
3557 1.1 rjs stc.laddr_type = SCTP_IPV4_ADDRESS;
3558 1.1 rjs } else if (to->sa_family == AF_INET6) {
3559 1.1 rjs memcpy(&stc.address, &to->sa_data,
3560 1.1 rjs sizeof(struct in6_addr));
3561 1.1 rjs stc.addr_type = SCTP_IPV6_ADDRESS;
3562 1.1 rjs if (net->src_addr_selected == 0) {
3563 1.1 rjs /* strange case here, the INIT
3564 1.1 rjs * should have did the selection.
3565 1.1 rjs */
3566 1.1 rjs net->_s_addr.sin6.sin6_addr =
3567 1.1 rjs sctp_ipv6_source_address_selection(inp,
3568 1.1 rjs stcb, &net->ro, net, 0);
3569 1.1 rjs net->src_addr_selected = 1;
3570 1.1 rjs }
3571 1.1 rjs memcpy(&stc.laddress, &net->_s_addr.sin6.sin6_addr,
3572 1.1 rjs sizeof(struct in6_addr));
3573 1.1 rjs stc.laddr_type = SCTP_IPV6_ADDRESS;
3574 1.1 rjs }
3575 1.1 rjs }
3576 1.1 rjs /* Now lets put the SCTP header in place */
3577 1.1 rjs initackm_out = mtod(m, struct sctp_init_msg *);
3578 1.1 rjs initackm_out->sh.src_port = inp->sctp_lport;
3579 1.1 rjs initackm_out->sh.dest_port = sh->src_port;
3580 1.1 rjs initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3581 1.1 rjs /* Save it off for quick ref */
3582 1.1 rjs stc.peers_vtag = init_chk->init.initiate_tag;
3583 1.1 rjs initackm_out->sh.checksum = 0; /* calculate later */
3584 1.1 rjs /* who are we */
3585 1.1 rjs strncpy(stc.identification, SCTP_VERSION_STRING,
3586 1.16 riastrad uimin(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3587 1.1 rjs /* now the chunk header */
3588 1.1 rjs initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3589 1.1 rjs initackm_out->msg.ch.chunk_flags = 0;
3590 1.1 rjs /* fill in later from mbuf we build */
3591 1.1 rjs initackm_out->msg.ch.chunk_length = 0;
3592 1.1 rjs /* place in my tag */
3593 1.1 rjs if ((asoc != NULL) &&
3594 1.1 rjs ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3595 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3596 1.1 rjs /* re-use the v-tags and init-seq here */
3597 1.1 rjs initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3598 1.1 rjs initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3599 1.1 rjs } else {
3600 1.1 rjs initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3601 1.1 rjs /* get a TSN to use too */
3602 1.1 rjs initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3603 1.1 rjs }
3604 1.1 rjs /* save away my tag to */
3605 1.1 rjs stc.my_vtag = initackm_out->msg.init.initiate_tag;
3606 1.1 rjs
3607 1.1 rjs /* set up some of the credits. */
3608 1.16 riastrad initackm_out->msg.init.a_rwnd = htonl(uimax(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
3609 1.1 rjs /* set what I want */
3610 1.1 rjs his_limit = ntohs(init_chk->init.num_inbound_streams);
3611 1.1 rjs /* choose what I want */
3612 1.1 rjs if (asoc != NULL) {
3613 1.1 rjs if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3614 1.1 rjs i_want = asoc->streamoutcnt;
3615 1.1 rjs } else {
3616 1.1 rjs i_want = inp->sctp_ep.pre_open_stream_count;
3617 1.1 rjs }
3618 1.1 rjs } else {
3619 1.1 rjs i_want = inp->sctp_ep.pre_open_stream_count;
3620 1.1 rjs }
3621 1.1 rjs if (his_limit < i_want) {
3622 1.1 rjs /* I Want more :< */
3623 1.1 rjs initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3624 1.1 rjs } else {
3625 1.1 rjs /* I can have what I want :> */
3626 1.1 rjs initackm_out->msg.init.num_outbound_streams = htons(i_want);
3627 1.1 rjs }
3628 1.1 rjs /* tell him his limt. */
3629 1.1 rjs initackm_out->msg.init.num_inbound_streams =
3630 1.1 rjs htons(inp->sctp_ep.max_open_streams_intome);
3631 1.1 rjs /* setup the ECN pointer */
3632 1.1 rjs
3633 1.1 rjs /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
3634 1.1 rjs if (inp->sctp_ep.adaption_layer_indicator) {
3635 1.1 rjs struct sctp_adaption_layer_indication *ali;
3636 1.1 rjs ali = (struct sctp_adaption_layer_indication *)(
3637 1.1 rjs (vaddr_t)initackm_out + sizeof(*initackm_out));
3638 1.1 rjs ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
3639 1.1 rjs ali->ph.param_length = htons(sizeof(*ali));
3640 1.1 rjs ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
3641 1.1 rjs m->m_len += sizeof(*ali);
3642 1.1 rjs ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
3643 1.1 rjs sizeof(*ali));
3644 1.1 rjs } else {
3645 1.1 rjs ecn = (struct sctp_ecn_supported_param*)(
3646 1.1 rjs (vaddr_t)initackm_out + sizeof(*initackm_out));
3647 1.1 rjs }
3648 1.1 rjs
3649 1.1 rjs /* ECN parameter */
3650 1.1 rjs if (sctp_ecn == 1) {
3651 1.1 rjs ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3652 1.1 rjs ecn->ph.param_length = htons(sizeof(*ecn));
3653 1.1 rjs m->m_len += sizeof(*ecn);
3654 1.1 rjs
3655 1.1 rjs prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
3656 1.1 rjs sizeof(*ecn));
3657 1.1 rjs } else {
3658 1.1 rjs prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
3659 1.1 rjs }
3660 1.1 rjs /* And now tell the peer we do pr-sctp */
3661 1.1 rjs prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3662 1.1 rjs prsctp->ph.param_length = htons(sizeof(*prsctp));
3663 1.1 rjs m->m_len += sizeof(*prsctp);
3664 1.1 rjs
3665 1.1 rjs
3666 1.1 rjs /* And now tell the peer we do all the extensions */
3667 1.1 rjs pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
3668 1.1 rjs sizeof(*prsctp));
3669 1.1 rjs
3670 1.1 rjs pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3671 1.1 rjs pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
3672 1.1 rjs pr_supported->chunk_types[0] = SCTP_ASCONF;
3673 1.1 rjs pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
3674 1.1 rjs pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
3675 1.1 rjs pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
3676 1.1 rjs pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
3677 1.1 rjs pr_supported->chunk_types[5] = 0; /* pad */
3678 1.1 rjs pr_supported->chunk_types[6] = 0; /* pad */
3679 1.1 rjs pr_supported->chunk_types[7] = 0; /* pad */
3680 1.1 rjs
3681 1.1 rjs m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3682 1.1 rjs if (sctp_ecn_nonce) {
3683 1.1 rjs /* ECN nonce: And now tell the peer we support ECN nonce */
3684 1.1 rjs ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
3685 1.1 rjs sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3686 1.1 rjs ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3687 1.1 rjs ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3688 1.1 rjs m->m_len += sizeof(*ecn_nonce);
3689 1.1 rjs }
3690 1.1 rjs
3691 1.1 rjs m_at = m;
3692 1.1 rjs /* now the addresses */
3693 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3694 1.1 rjs struct ifnet *ifn;
3695 1.1 rjs struct ifaddr *ifa;
3696 1.1 rjs int cnt = cnt_inits_to;
3697 1.4 ozaki int s;
3698 1.1 rjs
3699 1.4 ozaki s = pserialize_read_enter();
3700 1.4 ozaki IFNET_READER_FOREACH(ifn) {
3701 1.1 rjs if ((stc.loopback_scope == 0) &&
3702 1.1 rjs (ifn->if_type == IFT_LOOP)) {
3703 1.1 rjs /*
3704 1.1 rjs * Skip loopback devices if loopback_scope
3705 1.1 rjs * not set
3706 1.1 rjs */
3707 1.1 rjs continue;
3708 1.1 rjs }
3709 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
3710 1.1 rjs if (sctp_is_address_in_scope(ifa,
3711 1.1 rjs stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3712 1.1 rjs stc.loopback_scope, stc.ipv4_scope,
3713 1.1 rjs stc.local_scope, stc.site_scope) == 0) {
3714 1.1 rjs continue;
3715 1.1 rjs }
3716 1.1 rjs cnt++;
3717 1.1 rjs }
3718 1.1 rjs }
3719 1.4 ozaki pserialize_read_exit(s);
3720 1.4 ozaki
3721 1.1 rjs if (cnt > 1) {
3722 1.4 ozaki s = pserialize_read_enter();
3723 1.4 ozaki IFNET_READER_FOREACH(ifn) {
3724 1.1 rjs if ((stc.loopback_scope == 0) &&
3725 1.1 rjs (ifn->if_type == IFT_LOOP)) {
3726 1.1 rjs /*
3727 1.1 rjs * Skip loopback devices if
3728 1.1 rjs * loopback_scope not set
3729 1.1 rjs */
3730 1.1 rjs continue;
3731 1.1 rjs }
3732 1.7 ozaki IFADDR_READER_FOREACH(ifa, ifn) {
3733 1.1 rjs if (sctp_is_address_in_scope(ifa,
3734 1.1 rjs stc.ipv4_addr_legal,
3735 1.1 rjs stc.ipv6_addr_legal,
3736 1.1 rjs stc.loopback_scope, stc.ipv4_scope,
3737 1.1 rjs stc.local_scope, stc.site_scope) == 0) {
3738 1.1 rjs continue;
3739 1.1 rjs }
3740 1.1 rjs m_at = sctp_add_addr_to_mbuf(m_at, ifa);
3741 1.1 rjs }
3742 1.1 rjs }
3743 1.4 ozaki pserialize_read_exit(s);
3744 1.1 rjs }
3745 1.1 rjs } else {
3746 1.1 rjs struct sctp_laddr *laddr;
3747 1.1 rjs int cnt;
3748 1.1 rjs cnt = cnt_inits_to;
3749 1.1 rjs /* First, how many ? */
3750 1.1 rjs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3751 1.1 rjs if (laddr->ifa == NULL) {
3752 1.1 rjs continue;
3753 1.1 rjs }
3754 1.1 rjs if (laddr->ifa->ifa_addr == NULL)
3755 1.1 rjs continue;
3756 1.1 rjs if (sctp_is_address_in_scope(laddr->ifa,
3757 1.1 rjs stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3758 1.1 rjs stc.loopback_scope, stc.ipv4_scope,
3759 1.1 rjs stc.local_scope, stc.site_scope) == 0) {
3760 1.1 rjs continue;
3761 1.1 rjs }
3762 1.1 rjs cnt++;
3763 1.1 rjs }
3764 1.1 rjs /* If we bind a single address only we won't list
3765 1.1 rjs * any. This way you can get through a NAT
3766 1.1 rjs */
3767 1.1 rjs if (cnt > 1) {
3768 1.1 rjs LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3769 1.1 rjs if (laddr->ifa == NULL) {
3770 1.1 rjs #ifdef SCTP_DEBUG
3771 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
3772 1.1 rjs printf("Help I have fallen and I can't get up!\n");
3773 1.1 rjs }
3774 1.1 rjs #endif
3775 1.1 rjs continue;
3776 1.1 rjs }
3777 1.1 rjs if (laddr->ifa->ifa_addr == NULL)
3778 1.1 rjs continue;
3779 1.1 rjs if (sctp_is_address_in_scope(laddr->ifa,
3780 1.1 rjs stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3781 1.1 rjs stc.loopback_scope, stc.ipv4_scope,
3782 1.1 rjs stc.local_scope, stc.site_scope) == 0) {
3783 1.1 rjs continue;
3784 1.1 rjs }
3785 1.1 rjs m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3786 1.1 rjs }
3787 1.1 rjs }
3788 1.1 rjs }
3789 1.1 rjs
3790 1.1 rjs /* tack on the operational error if present */
3791 1.1 rjs if (op_err) {
3792 1.1 rjs if (op_err->m_pkthdr.len % 4) {
3793 1.1 rjs /* must add a pad to the param */
3794 1.1 rjs u_int32_t cpthis=0;
3795 1.1 rjs int padlen;
3796 1.1 rjs padlen = 4 - (op_err->m_pkthdr.len % 4);
3797 1.1 rjs m_copyback(op_err, op_err->m_pkthdr.len, padlen, (void *)&cpthis);
3798 1.1 rjs }
3799 1.1 rjs while (m_at->m_next != NULL) {
3800 1.1 rjs m_at = m_at->m_next;
3801 1.1 rjs }
3802 1.1 rjs m_at->m_next = op_err;
3803 1.1 rjs while (m_at->m_next != NULL) {
3804 1.1 rjs m_at = m_at->m_next;
3805 1.1 rjs }
3806 1.1 rjs }
3807 1.1 rjs /* Get total size of init packet */
3808 1.1 rjs sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3809 1.1 rjs /* pre-calulate the size and update pkt header and chunk header */
3810 1.1 rjs m->m_pkthdr.len = 0;
3811 1.1 rjs for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3812 1.1 rjs m->m_pkthdr.len += m_tmp->m_len;
3813 1.1 rjs if (m_tmp->m_next == NULL) {
3814 1.1 rjs /* m_tmp should now point to last one */
3815 1.1 rjs break;
3816 1.1 rjs }
3817 1.1 rjs }
3818 1.1 rjs /*
3819 1.1 rjs * Figure now the size of the cookie. We know the size of the
3820 1.1 rjs * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3821 1.1 rjs * COOKIE-STRUCTURE and SIGNATURE.
3822 1.1 rjs */
3823 1.1 rjs
3824 1.1 rjs /*
3825 1.1 rjs * take our earlier INIT calc and add in the sz we just calculated
3826 1.1 rjs * minus the size of the sctphdr (its not included in chunk size
3827 1.1 rjs */
3828 1.1 rjs
3829 1.1 rjs /* add once for the INIT-ACK */
3830 1.1 rjs sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3831 1.1 rjs
3832 1.1 rjs /* add a second time for the INIT-ACK in the cookie */
3833 1.1 rjs sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3834 1.1 rjs
3835 1.1 rjs /* Now add the cookie header and cookie message struct */
3836 1.1 rjs sz_of += sizeof(struct sctp_state_cookie_param);
3837 1.1 rjs /* ...and add the size of our signature */
3838 1.1 rjs sz_of += SCTP_SIGNATURE_SIZE;
3839 1.1 rjs initackm_out->msg.ch.chunk_length = htons(sz_of);
3840 1.1 rjs
3841 1.1 rjs /* Now we must build a cookie */
3842 1.1 rjs m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3843 1.1 rjs sizeof(struct sctphdr), &stc);
3844 1.1 rjs if (m_cookie == NULL) {
3845 1.1 rjs /* memory problem */
3846 1.1 rjs sctp_m_freem(m);
3847 1.1 rjs return;
3848 1.1 rjs }
3849 1.1 rjs /* Now append the cookie to the end and update the space/size */
3850 1.1 rjs m_tmp->m_next = m_cookie;
3851 1.1 rjs
3852 1.1 rjs /*
3853 1.1 rjs * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
3854 1.1 rjs * return here since the timer will drive a retranmission.
3855 1.1 rjs */
3856 1.1 rjs padval = m->m_pkthdr.len % 4;
3857 1.1 rjs if ((padval) && (m_last)) {
3858 1.1 rjs /* see my previous comments on m_last */
3859 1.1 rjs int ret;
3860 1.1 rjs ret = sctp_add_pad_tombuf(m_last, (4-padval));
3861 1.1 rjs if (ret) {
3862 1.1 rjs /* Houston we have a problem, no space */
3863 1.1 rjs sctp_m_freem(m);
3864 1.1 rjs return;
3865 1.1 rjs }
3866 1.1 rjs m->m_pkthdr.len += padval;
3867 1.1 rjs }
3868 1.1 rjs sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
3869 1.1 rjs }
3870 1.1 rjs
3871 1.1 rjs
3872 1.1 rjs static void
3873 1.1 rjs sctp_insert_on_wheel(struct sctp_association *asoc,
3874 1.1 rjs struct sctp_stream_out *strq)
3875 1.1 rjs {
3876 1.1 rjs struct sctp_stream_out *stre, *strn;
3877 1.1 rjs stre = TAILQ_FIRST(&asoc->out_wheel);
3878 1.1 rjs if (stre == NULL) {
3879 1.1 rjs /* only one on wheel */
3880 1.1 rjs TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
3881 1.1 rjs return;
3882 1.1 rjs }
3883 1.1 rjs for (; stre; stre = strn) {
3884 1.1 rjs strn = TAILQ_NEXT(stre, next_spoke);
3885 1.1 rjs if (stre->stream_no > strq->stream_no) {
3886 1.1 rjs TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
3887 1.1 rjs return;
3888 1.1 rjs } else if (stre->stream_no == strq->stream_no) {
3889 1.1 rjs /* huh, should not happen */
3890 1.1 rjs return;
3891 1.1 rjs } else if (strn == NULL) {
3892 1.1 rjs /* next one is null */
3893 1.1 rjs TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
3894 1.1 rjs next_spoke);
3895 1.1 rjs }
3896 1.1 rjs }
3897 1.1 rjs }
3898 1.1 rjs
3899 1.1 rjs static void
3900 1.1 rjs sctp_remove_from_wheel(struct sctp_association *asoc,
3901 1.1 rjs struct sctp_stream_out *strq)
3902 1.1 rjs {
3903 1.1 rjs /* take off and then setup so we know it is not on the wheel */
3904 1.1 rjs TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
3905 1.1 rjs strq->next_spoke.tqe_next = NULL;
3906 1.1 rjs strq->next_spoke.tqe_prev = NULL;
3907 1.1 rjs }
3908 1.1 rjs
3909 1.1 rjs
3910 1.1 rjs static void
3911 1.1 rjs sctp_prune_prsctp(struct sctp_tcb *stcb,
3912 1.1 rjs struct sctp_association *asoc,
3913 1.1 rjs struct sctp_sndrcvinfo *srcv,
3914 1.1 rjs int dataout
3915 1.1 rjs )
3916 1.1 rjs {
3917 1.1 rjs int freed_spc=0;
3918 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
3919 1.1 rjs if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
3920 1.1 rjs TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3921 1.1 rjs /*
3922 1.1 rjs * Look for chunks marked with the PR_SCTP
3923 1.1 rjs * flag AND the buffer space flag. If the one
3924 1.1 rjs * being sent is equal or greater priority then
3925 1.1 rjs * purge the old one and free some space.
3926 1.1 rjs */
3927 1.1 rjs if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
3928 1.1 rjs SCTP_PR_SCTP_BUFFER)) ==
3929 1.1 rjs (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
3930 1.1 rjs /*
3931 1.1 rjs * This one is PR-SCTP AND buffer space
3932 1.1 rjs * limited type
3933 1.1 rjs */
3934 1.1 rjs if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3935 1.1 rjs /* Lower numbers equates to
3936 1.1 rjs * higher priority so if the
3937 1.1 rjs * one we are looking at has a
3938 1.1 rjs * larger or equal priority we
3939 1.1 rjs * want to drop the data and
3940 1.1 rjs * NOT retransmit it.
3941 1.1 rjs */
3942 1.1 rjs if (chk->data) {
3943 1.1 rjs /* We release the
3944 1.1 rjs * book_size if the
3945 1.1 rjs * mbuf is here
3946 1.1 rjs */
3947 1.1 rjs int ret_spc;
3948 1.1 rjs int cause;
3949 1.1 rjs if (chk->sent > SCTP_DATAGRAM_UNSENT)
3950 1.1 rjs cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
3951 1.1 rjs else
3952 1.1 rjs cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
3953 1.1 rjs ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3954 1.1 rjs cause,
3955 1.1 rjs &asoc->sent_queue);
3956 1.1 rjs freed_spc += ret_spc;
3957 1.1 rjs if (freed_spc >= dataout) {
3958 1.1 rjs return;
3959 1.1 rjs }
3960 1.1 rjs } /* if chunk was present */
3961 1.24 andvar } /* if of sufficient priority */
3962 1.1 rjs } /* if chunk has enabled */
3963 1.1 rjs } /* tailqforeach */
3964 1.1 rjs
3965 1.1 rjs chk = TAILQ_FIRST(&asoc->send_queue);
3966 1.1 rjs while (chk) {
3967 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
3968 1.1 rjs /* Here we must move to the sent queue and mark */
3969 1.1 rjs if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
3970 1.1 rjs SCTP_PR_SCTP_BUFFER)) ==
3971 1.1 rjs (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
3972 1.1 rjs if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3973 1.1 rjs if (chk->data) {
3974 1.1 rjs /* We release the
3975 1.1 rjs * book_size if the
3976 1.1 rjs * mbuf is here
3977 1.1 rjs */
3978 1.1 rjs int ret_spc;
3979 1.1 rjs ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3980 1.1 rjs SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
3981 1.1 rjs &asoc->send_queue);
3982 1.1 rjs
3983 1.1 rjs freed_spc += ret_spc;
3984 1.1 rjs if (freed_spc >= dataout) {
3985 1.1 rjs return;
3986 1.1 rjs }
3987 1.1 rjs } /* end if chk->data */
3988 1.1 rjs } /* end if right class */
3989 1.1 rjs } /* end if chk pr-sctp */
3990 1.1 rjs chk = nchk;
3991 1.1 rjs } /* end while (chk) */
3992 1.1 rjs } /* if enabled in asoc */
3993 1.1 rjs }
3994 1.1 rjs
3995 1.1 rjs static void
3996 1.1 rjs sctp_prepare_chunk(struct sctp_tmit_chunk *template,
3997 1.1 rjs struct sctp_tcb *stcb,
3998 1.1 rjs struct sctp_sndrcvinfo *srcv,
3999 1.1 rjs struct sctp_stream_out *strq,
4000 1.1 rjs struct sctp_nets *net)
4001 1.1 rjs {
4002 1.1 rjs memset(template, 0, sizeof(struct sctp_tmit_chunk));
4003 1.1 rjs template->sent = SCTP_DATAGRAM_UNSENT;
4004 1.1 rjs if ((stcb->asoc.peer_supports_prsctp) &&
4005 1.12 rjs (srcv->sinfo_flags & (SCTP_PR_SCTP_TTL|SCTP_PR_SCTP_BUF)) &&
4006 1.1 rjs (srcv->sinfo_timetolive > 0)
4007 1.1 rjs ) {
4008 1.1 rjs /* If:
4009 1.1 rjs * Peer supports PR-SCTP
4010 1.1 rjs * The flags is set against this send for PR-SCTP
4011 1.29 andvar * And timetolive is a positive value, zero is reserved
4012 1.1 rjs * to mean a reliable send for both buffer/time
4013 1.1 rjs * related one.
4014 1.1 rjs */
4015 1.12 rjs if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
4016 1.1 rjs /*
4017 1.1 rjs * Time to live is a priority stored in tv_sec
4018 1.1 rjs * when doing the buffer drop thing.
4019 1.1 rjs */
4020 1.1 rjs template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
4021 1.1 rjs } else {
4022 1.1 rjs struct timeval tv;
4023 1.1 rjs
4024 1.1 rjs SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
4025 1.1 rjs tv.tv_sec = srcv->sinfo_timetolive / 1000;
4026 1.1 rjs tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
4027 1.1 rjs #ifndef __FreeBSD__
4028 1.1 rjs timeradd(&template->rec.data.timetodrop, &tv,
4029 1.1 rjs &template->rec.data.timetodrop);
4030 1.1 rjs #else
4031 1.1 rjs timevaladd(&template->rec.data.timetodrop, &tv);
4032 1.1 rjs #endif
4033 1.1 rjs }
4034 1.1 rjs }
4035 1.12 rjs if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
4036 1.1 rjs template->rec.data.stream_seq = strq->next_sequence_sent;
4037 1.1 rjs } else {
4038 1.1 rjs template->rec.data.stream_seq = 0;
4039 1.1 rjs }
4040 1.1 rjs template->rec.data.TSN_seq = 0; /* not yet assigned */
4041 1.1 rjs
4042 1.1 rjs template->rec.data.stream_number = srcv->sinfo_stream;
4043 1.1 rjs template->rec.data.payloadtype = srcv->sinfo_ppid;
4044 1.1 rjs template->rec.data.context = srcv->sinfo_context;
4045 1.1 rjs template->rec.data.doing_fast_retransmit = 0;
4046 1.1 rjs template->rec.data.ect_nonce = 0; /* ECN Nonce */
4047 1.1 rjs
4048 1.12 rjs if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
4049 1.1 rjs template->whoTo = net;
4050 1.1 rjs } else {
4051 1.1 rjs if (stcb->asoc.primary_destination)
4052 1.1 rjs template->whoTo = stcb->asoc.primary_destination;
4053 1.1 rjs else {
4054 1.1 rjs /* TSNH */
4055 1.1 rjs template->whoTo = net;
4056 1.1 rjs }
4057 1.1 rjs }
4058 1.1 rjs /* the actual chunk flags */
4059 1.12 rjs if (srcv->sinfo_flags & SCTP_UNORDERED) {
4060 1.1 rjs template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
4061 1.1 rjs } else {
4062 1.1 rjs template->rec.data.rcv_flags = 0;
4063 1.1 rjs }
4064 1.1 rjs /* no flags yet, FRAGMENT_OK goes here */
4065 1.1 rjs template->flags = 0;
4066 1.1 rjs /* PR sctp flags */
4067 1.1 rjs if (stcb->asoc.peer_supports_prsctp) {
4068 1.1 rjs if (srcv->sinfo_timetolive > 0) {
4069 1.1 rjs /*
4070 1.1 rjs * We only set the flag if timetolive (or
4071 1.1 rjs * priority) was set to a positive number.
4072 1.1 rjs * Zero is reserved specifically to be
4073 1.1 rjs * EXCLUDED and sent reliable.
4074 1.1 rjs */
4075 1.12 rjs if (srcv->sinfo_flags & SCTP_PR_SCTP_TTL) {
4076 1.1 rjs template->flags |= SCTP_PR_SCTP_ENABLED;
4077 1.1 rjs }
4078 1.12 rjs if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
4079 1.1 rjs template->flags |= SCTP_PR_SCTP_BUFFER;
4080 1.1 rjs }
4081 1.1 rjs }
4082 1.1 rjs }
4083 1.1 rjs template->asoc = &stcb->asoc;
4084 1.1 rjs }
4085 1.1 rjs
4086 1.1 rjs
4087 1.1 rjs int
4088 1.1 rjs sctp_get_frag_point(struct sctp_tcb *stcb,
4089 1.1 rjs struct sctp_association *asoc)
4090 1.1 rjs {
4091 1.1 rjs int siz, ovh;
4092 1.1 rjs
4093 1.1 rjs /* For endpoints that have both 6 and 4 addresses
4094 1.1 rjs * we must reserver room for the 6 ip header, for
4095 1.1 rjs * those that are only dealing with V4 we use
4096 1.1 rjs * a larger frag point.
4097 1.1 rjs */
4098 1.1 rjs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4099 1.1 rjs ovh = SCTP_MED_OVERHEAD;
4100 1.1 rjs } else {
4101 1.1 rjs ovh = SCTP_MED_V4_OVERHEAD;
4102 1.1 rjs }
4103 1.1 rjs
4104 1.1 rjs if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4105 1.1 rjs siz = asoc->smallest_mtu - ovh;
4106 1.1 rjs else
4107 1.1 rjs siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4108 1.1 rjs /*
4109 1.1 rjs if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
4110 1.1 rjs /* A data chunk MUST fit in a cluster */
4111 1.1 rjs /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
4112 1.1 rjs /* }*/
4113 1.1 rjs
4114 1.1 rjs if (siz % 4) {
4115 1.1 rjs /* make it an even word boundary please */
4116 1.1 rjs siz -= (siz % 4);
4117 1.1 rjs }
4118 1.1 rjs return (siz);
4119 1.1 rjs }
4120 1.1 rjs extern unsigned int sctp_max_chunks_on_queue;
4121 1.1 rjs
4122 1.1 rjs #define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
4123 1.1 rjs
4124 1.1 rjs static int
4125 1.1 rjs sctp_msg_append(struct sctp_tcb *stcb,
4126 1.1 rjs struct sctp_nets *net,
4127 1.1 rjs struct mbuf *m,
4128 1.1 rjs struct sctp_sndrcvinfo *srcv,
4129 1.1 rjs int flags)
4130 1.1 rjs {
4131 1.1 rjs struct socket *so;
4132 1.1 rjs struct sctp_association *asoc;
4133 1.1 rjs struct sctp_stream_out *strq;
4134 1.1 rjs struct sctp_tmit_chunk *chk;
4135 1.1 rjs struct sctpchunk_listhead tmp;
4136 1.1 rjs struct sctp_tmit_chunk template;
4137 1.1 rjs struct mbuf *n, *mnext;
4138 1.1 rjs struct mbuf *mm;
4139 1.1 rjs unsigned int dataout, siz;
4140 1.1 rjs int mbcnt = 0;
4141 1.1 rjs int mbcnt_e = 0;
4142 1.1 rjs int error = 0;
4143 1.1 rjs
4144 1.1 rjs if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
4145 1.1 rjs /* Software fault, you blew it on the call */
4146 1.1 rjs #ifdef SCTP_DEBUG
4147 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4148 1.1 rjs printf("software error in sctp_msg_append:1\n");
4149 1.1 rjs printf("stcb:%p net:%p m:%p srcv:%p\n",
4150 1.1 rjs stcb, net, m, srcv);
4151 1.1 rjs }
4152 1.1 rjs #endif
4153 1.1 rjs if (m)
4154 1.1 rjs sctp_m_freem(m);
4155 1.1 rjs return (EFAULT);
4156 1.1 rjs }
4157 1.1 rjs so = stcb->sctp_socket;
4158 1.1 rjs asoc = &stcb->asoc;
4159 1.12 rjs if (srcv->sinfo_flags & SCTP_ABORT) {
4160 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4161 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
4162 1.1 rjs /* It has to be up before we abort */
4163 1.1 rjs /* how big is the user initiated abort? */
4164 1.1 rjs if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4165 1.1 rjs dataout = m->m_pkthdr.len;
4166 1.1 rjs } else {
4167 1.1 rjs /* we must count */
4168 1.1 rjs dataout = 0;
4169 1.1 rjs for (n = m; n; n = n->m_next) {
4170 1.1 rjs dataout += n->m_len;
4171 1.1 rjs }
4172 1.1 rjs }
4173 1.1 rjs M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
4174 1.1 rjs if (m) {
4175 1.1 rjs struct sctp_paramhdr *ph;
4176 1.1 rjs m->m_len = sizeof(struct sctp_paramhdr) + dataout;
4177 1.1 rjs ph = mtod(m, struct sctp_paramhdr *);
4178 1.1 rjs ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4179 1.1 rjs ph->param_length = htons(m->m_len);
4180 1.1 rjs }
4181 1.1 rjs sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
4182 1.1 rjs m = NULL;
4183 1.1 rjs } else {
4184 1.1 rjs /* Only free if we don't send an abort */
4185 1.1 rjs ;
4186 1.1 rjs }
4187 1.1 rjs goto out;
4188 1.1 rjs }
4189 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4190 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4191 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4192 1.1 rjs (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
4193 1.1 rjs /* got data while shutting down */
4194 1.1 rjs error = ECONNRESET;
4195 1.1 rjs goto out;
4196 1.1 rjs }
4197 1.1 rjs
4198 1.1 rjs if (srcv->sinfo_stream >= asoc->streamoutcnt) {
4199 1.1 rjs /* Invalid stream number */
4200 1.1 rjs error = EINVAL;
4201 1.1 rjs goto out;
4202 1.1 rjs }
4203 1.1 rjs if (asoc->strmout == NULL) {
4204 1.1 rjs /* huh? software error */
4205 1.1 rjs #ifdef SCTP_DEBUG
4206 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4207 1.1 rjs printf("software error in sctp_msg_append:2\n");
4208 1.1 rjs }
4209 1.1 rjs #endif
4210 1.1 rjs error = EFAULT;
4211 1.1 rjs goto out;
4212 1.1 rjs }
4213 1.1 rjs strq = &asoc->strmout[srcv->sinfo_stream];
4214 1.1 rjs /* how big is it ? */
4215 1.1 rjs if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4216 1.1 rjs dataout = m->m_pkthdr.len;
4217 1.1 rjs } else {
4218 1.1 rjs /* we must count */
4219 1.1 rjs dataout = 0;
4220 1.1 rjs for (n = m; n; n = n->m_next) {
4221 1.1 rjs dataout += n->m_len;
4222 1.1 rjs }
4223 1.1 rjs }
4224 1.1 rjs #ifdef SCTP_DEBUG
4225 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4226 1.1 rjs printf("Attempt to send out %d bytes\n",
4227 1.1 rjs dataout);
4228 1.1 rjs }
4229 1.1 rjs #endif
4230 1.1 rjs
4231 1.1 rjs /* lock the socket buf */
4232 1.1 rjs error = sblock(&so->so_snd, SBLOCKWAIT(flags));
4233 1.1 rjs if (error)
4234 1.1 rjs goto out_locked;
4235 1.1 rjs
4236 1.1 rjs if (dataout > so->so_snd.sb_hiwat) {
4237 1.1 rjs /* It will NEVER fit */
4238 1.1 rjs error = EMSGSIZE;
4239 1.1 rjs goto release;
4240 1.1 rjs }
4241 1.12 rjs if ((srcv->sinfo_flags & SCTP_EOF) &&
4242 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4243 1.1 rjs (dataout == 0)
4244 1.1 rjs ) {
4245 1.1 rjs goto zap_by_it_all;
4246 1.1 rjs }
4247 1.1 rjs if ((so->so_snd.sb_hiwat <
4248 1.1 rjs (dataout + asoc->total_output_queue_size)) ||
4249 1.1 rjs (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4250 1.1 rjs (asoc->total_output_mbuf_queue_size >
4251 1.1 rjs so->so_snd.sb_mbmax)
4252 1.1 rjs ) {
4253 1.1 rjs /* XXX Buffer space hunt for data to skip */
4254 1.1 rjs if (asoc->peer_supports_prsctp) {
4255 1.1 rjs sctp_prune_prsctp(stcb, asoc, srcv, dataout);
4256 1.1 rjs }
4257 1.1 rjs while ((so->so_snd.sb_hiwat <
4258 1.1 rjs (dataout + asoc->total_output_queue_size)) ||
4259 1.1 rjs (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4260 1.1 rjs (asoc->total_output_mbuf_queue_size >
4261 1.1 rjs so->so_snd.sb_mbmax)) {
4262 1.1 rjs struct sctp_inpcb *inp;
4263 1.1 rjs /* Now did we free up enough room? */
4264 1.1 rjs if (so->so_state & SS_NBIO) {
4265 1.1 rjs /* Non-blocking io in place */
4266 1.1 rjs error = EWOULDBLOCK;
4267 1.1 rjs goto release;
4268 1.1 rjs }
4269 1.1 rjs /*
4270 1.1 rjs * We store off a pointer to the endpoint.
4271 1.1 rjs * Since on return from this we must check to
4272 1.1 rjs * see if an so_error is set. If so we may have
4273 1.1 rjs * been reset and our stcb destroyed. Returning
4274 1.1 rjs * an error will cause the correct error return
4275 1.1 rjs * through and fix this all.
4276 1.1 rjs */
4277 1.1 rjs inp = stcb->sctp_ep;
4278 1.1 rjs /*
4279 1.1 rjs * Not sure how else to do this since
4280 1.1 rjs * the level we suspended at is not
4281 1.1 rjs * known deep down where we are. I will
4282 1.1 rjs * drop to spl0() so that others can
4283 1.1 rjs * get in.
4284 1.1 rjs */
4285 1.1 rjs
4286 1.1 rjs inp->sctp_tcb_at_block = (void *)stcb;
4287 1.1 rjs inp->error_on_block = 0;
4288 1.1 rjs sbunlock(&so->so_snd);
4289 1.1 rjs error = sbwait(&so->so_snd);
4290 1.1 rjs /*
4291 1.1 rjs * XXX: This is ugly but I have
4292 1.1 rjs * recreated most of what goes on to
4293 1.1 rjs * block in the sb. UGHH
4294 1.1 rjs * May want to add the bit about being
4295 1.1 rjs * no longer connected.. but this then
4296 1.1 rjs * further dooms the UDP model NOT to
4297 1.1 rjs * allow this.
4298 1.1 rjs */
4299 1.1 rjs inp->sctp_tcb_at_block = 0;
4300 1.1 rjs if (inp->error_on_block)
4301 1.1 rjs error = inp->error_on_block;
4302 1.1 rjs if (so->so_error)
4303 1.1 rjs error = so->so_error;
4304 1.1 rjs if (error) {
4305 1.1 rjs goto out_locked;
4306 1.1 rjs }
4307 1.1 rjs error = sblock(&so->so_snd, M_WAITOK);
4308 1.1 rjs if (error)
4309 1.1 rjs goto out_locked;
4310 1.1 rjs /* Otherwise we cycle back and recheck
4311 1.1 rjs * the space
4312 1.1 rjs */
4313 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
4314 1.1 rjs if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
4315 1.1 rjs #else
4316 1.1 rjs if (so->so_state & SS_CANTSENDMORE) {
4317 1.1 rjs #endif
4318 1.1 rjs error = EPIPE;
4319 1.1 rjs goto release;
4320 1.1 rjs }
4321 1.1 rjs if (so->so_error) {
4322 1.1 rjs error = so->so_error;
4323 1.1 rjs goto release;
4324 1.1 rjs }
4325 1.1 rjs }
4326 1.1 rjs }
4327 1.1 rjs /* If we have a packet header fix it if it was broke */
4328 1.1 rjs if (m->m_flags & M_PKTHDR) {
4329 1.1 rjs m->m_pkthdr.len = dataout;
4330 1.1 rjs }
4331 1.1 rjs /* use the smallest one, user set value or
4332 1.1 rjs * smallest mtu of the asoc
4333 1.1 rjs */
4334 1.1 rjs siz = sctp_get_frag_point(stcb, asoc);
4335 1.1 rjs if ((dataout) && (dataout <= siz)) {
4336 1.1 rjs /* Fast path */
4337 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4338 1.1 rjs if (chk == NULL) {
4339 1.1 rjs error = ENOMEM;
4340 1.1 rjs goto release;
4341 1.1 rjs }
4342 1.1 rjs sctp_prepare_chunk(chk, stcb, srcv, strq, net);
4343 1.1 rjs chk->whoTo->ref_count++;
4344 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
4345 1.1 rjs
4346 1.1 rjs /* no flags yet, FRAGMENT_OK goes here */
4347 1.1 rjs sctppcbinfo.ipi_count_chunk++;
4348 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4349 1.1 rjs asoc->chunks_on_out_queue++;
4350 1.1 rjs chk->data = m;
4351 1.1 rjs m = NULL;
4352 1.1 rjs /* Total in the MSIZE */
4353 1.1 rjs for (mm = chk->data; mm; mm = mm->m_next) {
4354 1.1 rjs mbcnt += MSIZE;
4355 1.1 rjs if (mm->m_flags & M_EXT) {
4356 1.1 rjs mbcnt += chk->data->m_ext.ext_size;
4357 1.1 rjs }
4358 1.1 rjs }
4359 1.1 rjs /* fix up the send_size if it is not present */
4360 1.1 rjs chk->send_size = dataout;
4361 1.1 rjs chk->book_size = chk->send_size;
4362 1.1 rjs chk->mbcnt = mbcnt;
4363 1.26 andvar /* ok, we are committed */
4364 1.12 rjs if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
4365 1.1 rjs /* bump the ssn if we are unordered. */
4366 1.1 rjs strq->next_sequence_sent++;
4367 1.1 rjs }
4368 1.1 rjs chk->data->m_nextpkt = 0;
4369 1.1 rjs asoc->stream_queue_cnt++;
4370 1.1 rjs TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4371 1.1 rjs /* now check if this stream is on the wheel */
4372 1.1 rjs if ((strq->next_spoke.tqe_next == NULL) &&
4373 1.1 rjs (strq->next_spoke.tqe_prev == NULL)) {
4374 1.1 rjs /* Insert it on the wheel since it is not
4375 1.1 rjs * on it currently
4376 1.1 rjs */
4377 1.1 rjs sctp_insert_on_wheel(asoc, strq);
4378 1.1 rjs }
4379 1.1 rjs } else if ((dataout) && (dataout > siz)) {
4380 1.1 rjs /* Slow path */
4381 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
4382 1.1 rjs (dataout > siz)) {
4383 1.1 rjs error = EMSGSIZE;
4384 1.1 rjs goto release;
4385 1.1 rjs }
4386 1.1 rjs /* setup the template */
4387 1.1 rjs sctp_prepare_chunk(&template, stcb, srcv, strq, net);
4388 1.1 rjs
4389 1.1 rjs n = m;
4390 1.1 rjs while (dataout > siz) {
4391 1.1 rjs /*
4392 1.1 rjs * We can wait since this is called from the user
4393 1.1 rjs * send side
4394 1.1 rjs */
4395 1.1 rjs n->m_nextpkt = m_split(n, siz, M_WAIT);
4396 1.1 rjs if (n->m_nextpkt == NULL) {
4397 1.1 rjs error = EFAULT;
4398 1.1 rjs goto release;
4399 1.1 rjs }
4400 1.1 rjs dataout -= siz;
4401 1.1 rjs n = n->m_nextpkt;
4402 1.1 rjs }
4403 1.1 rjs /*
4404 1.1 rjs * ok, now we have a chain on m where m->m_nextpkt points to
4405 1.1 rjs * the next chunk and m/m->m_next chain is the piece to send.
4406 1.1 rjs * We must go through the chains and thread them on to
4407 1.1 rjs * sctp_tmit_chunk chains and place them all on the stream
4408 1.1 rjs * queue, breaking the m->m_nextpkt pointers as we go.
4409 1.1 rjs */
4410 1.1 rjs n = m;
4411 1.1 rjs TAILQ_INIT(&tmp);
4412 1.1 rjs while (n) {
4413 1.1 rjs /*
4414 1.1 rjs * first go through and allocate a sctp_tmit chunk
4415 1.1 rjs * for each chunk piece
4416 1.1 rjs */
4417 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4418 1.1 rjs if (chk == NULL) {
4419 1.1 rjs /*
4420 1.1 rjs * ok we must spin through and dump anything
4421 1.1 rjs * we have allocated and then jump to the
4422 1.1 rjs * no_membad
4423 1.1 rjs */
4424 1.1 rjs chk = TAILQ_FIRST(&tmp);
4425 1.1 rjs while (chk) {
4426 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
4427 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4428 1.1 rjs sctppcbinfo.ipi_count_chunk--;
4429 1.1 rjs asoc->chunks_on_out_queue--;
4430 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4431 1.1 rjs panic("Chunk count is negative");
4432 1.1 rjs }
4433 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4434 1.1 rjs chk = TAILQ_FIRST(&tmp);
4435 1.1 rjs }
4436 1.1 rjs error = ENOMEM;
4437 1.1 rjs goto release;
4438 1.1 rjs }
4439 1.1 rjs sctppcbinfo.ipi_count_chunk++;
4440 1.1 rjs asoc->chunks_on_out_queue++;
4441 1.1 rjs
4442 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4443 1.1 rjs *chk = template;
4444 1.1 rjs chk->whoTo->ref_count++;
4445 1.1 rjs chk->data = n;
4446 1.1 rjs /* Total in the MSIZE */
4447 1.1 rjs mbcnt_e = 0;
4448 1.1 rjs for (mm = chk->data; mm; mm = mm->m_next) {
4449 1.1 rjs mbcnt_e += MSIZE;
4450 1.1 rjs if (mm->m_flags & M_EXT) {
4451 1.1 rjs mbcnt_e += chk->data->m_ext.ext_size;
4452 1.1 rjs }
4453 1.1 rjs }
4454 1.1 rjs /* now fix the chk->send_size */
4455 1.1 rjs if (chk->data->m_flags & M_PKTHDR) {
4456 1.1 rjs chk->send_size = chk->data->m_pkthdr.len;
4457 1.1 rjs } else {
4458 1.1 rjs struct mbuf *nn;
4459 1.1 rjs chk->send_size = 0;
4460 1.1 rjs for (nn = chk->data; nn; nn = nn->m_next) {
4461 1.1 rjs chk->send_size += nn->m_len;
4462 1.1 rjs }
4463 1.1 rjs }
4464 1.1 rjs chk->book_size = chk->send_size;
4465 1.1 rjs chk->mbcnt = mbcnt_e;
4466 1.1 rjs mbcnt += mbcnt_e;
4467 1.1 rjs if (chk->flags & SCTP_PR_SCTP_BUFFER) {
4468 1.1 rjs asoc->sent_queue_cnt_removeable++;
4469 1.1 rjs }
4470 1.1 rjs n = n->m_nextpkt;
4471 1.1 rjs TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
4472 1.1 rjs }
4473 1.1 rjs m = NULL;
4474 1.1 rjs /* now that we have enough space for all de-couple the
4475 1.1 rjs * chain of mbufs by going through our temp array
4476 1.1 rjs * and breaking the pointers.
4477 1.1 rjs */
4478 1.26 andvar /* ok, we are committed */
4479 1.12 rjs if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
4480 1.1 rjs /* bump the ssn if we are unordered. */
4481 1.1 rjs strq->next_sequence_sent++;
4482 1.1 rjs }
4483 1.1 rjs /* Mark the first/last flags. This will
4484 1.1 rjs * result int a 3 for a single item on the list
4485 1.1 rjs */
4486 1.1 rjs chk = TAILQ_FIRST(&tmp);
4487 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
4488 1.1 rjs chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
4489 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4490 1.1 rjs /* now break any chains on the queue and
4491 1.1 rjs * move it to the streams actual queue.
4492 1.1 rjs */
4493 1.1 rjs chk = TAILQ_FIRST(&tmp);
4494 1.1 rjs while (chk) {
4495 1.1 rjs chk->data->m_nextpkt = 0;
4496 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
4497 1.1 rjs asoc->stream_queue_cnt++;
4498 1.1 rjs TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4499 1.1 rjs chk = TAILQ_FIRST(&tmp);
4500 1.1 rjs }
4501 1.1 rjs /* now check if this stream is on the wheel */
4502 1.1 rjs if ((strq->next_spoke.tqe_next == NULL) &&
4503 1.1 rjs (strq->next_spoke.tqe_prev == NULL)) {
4504 1.1 rjs /* Insert it on the wheel since it is not
4505 1.1 rjs * on it currently
4506 1.1 rjs */
4507 1.1 rjs sctp_insert_on_wheel(asoc, strq);
4508 1.1 rjs }
4509 1.1 rjs }
4510 1.1 rjs /* has a SHUTDOWN been (also) requested by the user on this asoc? */
4511 1.1 rjs zap_by_it_all:
4512 1.1 rjs
4513 1.12 rjs if ((srcv->sinfo_flags & SCTP_EOF) &&
4514 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4515 1.1 rjs
4516 1.1 rjs int some_on_streamwheel = 0;
4517 1.1 rjs
4518 1.1 rjs if (!TAILQ_EMPTY(&asoc->out_wheel)) {
4519 1.1 rjs /* Check to see if some data queued */
4520 1.1 rjs struct sctp_stream_out *outs;
4521 1.1 rjs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
4522 1.1 rjs if (!TAILQ_EMPTY(&outs->outqueue)) {
4523 1.1 rjs some_on_streamwheel = 1;
4524 1.1 rjs break;
4525 1.1 rjs }
4526 1.1 rjs }
4527 1.1 rjs }
4528 1.1 rjs
4529 1.1 rjs if (TAILQ_EMPTY(&asoc->send_queue) &&
4530 1.1 rjs TAILQ_EMPTY(&asoc->sent_queue) &&
4531 1.1 rjs (some_on_streamwheel == 0)) {
4532 1.1 rjs /* there is nothing queued to send, so I'm done... */
4533 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4534 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4535 1.1 rjs /* only send SHUTDOWN the first time through */
4536 1.1 rjs #ifdef SCTP_DEBUG
4537 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
4538 1.1 rjs printf("%s:%d sends a shutdown\n",
4539 1.1 rjs __FILE__,
4540 1.1 rjs __LINE__
4541 1.1 rjs );
4542 1.1 rjs }
4543 1.1 rjs #endif
4544 1.1 rjs sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4545 1.1 rjs asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4546 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4547 1.1 rjs asoc->primary_destination);
4548 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4549 1.1 rjs asoc->primary_destination);
4550 1.1 rjs }
4551 1.1 rjs } else {
4552 1.1 rjs /*
4553 1.1 rjs * we still got (or just got) data to send, so set
4554 1.1 rjs * SHUTDOWN_PENDING
4555 1.1 rjs */
4556 1.1 rjs asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4557 1.1 rjs }
4558 1.1 rjs }
4559 1.1 rjs #ifdef SCTP_MBCNT_LOGGING
4560 1.1 rjs sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4561 1.1 rjs asoc->total_output_queue_size,
4562 1.1 rjs dataout,
4563 1.1 rjs asoc->total_output_mbuf_queue_size,
4564 1.1 rjs mbcnt);
4565 1.1 rjs #endif
4566 1.1 rjs asoc->total_output_queue_size += dataout;
4567 1.1 rjs asoc->total_output_mbuf_queue_size += mbcnt;
4568 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4569 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4570 1.1 rjs so->so_snd.sb_cc += dataout;
4571 1.1 rjs so->so_snd.sb_mbcnt += mbcnt;
4572 1.1 rjs }
4573 1.1 rjs
4574 1.1 rjs #ifdef SCTP_DEBUG
4575 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
4576 1.1 rjs printf("++total out:%d total_mbuf_out:%d\n",
4577 1.1 rjs (int)asoc->total_output_queue_size,
4578 1.1 rjs (int)asoc->total_output_mbuf_queue_size);
4579 1.1 rjs }
4580 1.1 rjs #endif
4581 1.1 rjs
4582 1.1 rjs release:
4583 1.1 rjs sbunlock(&so->so_snd);
4584 1.1 rjs out_locked:
4585 1.1 rjs
4586 1.1 rjs out:
4587 1.1 rjs if (m && m->m_nextpkt) {
4588 1.1 rjs n = m;
4589 1.1 rjs while (n) {
4590 1.1 rjs mnext = n->m_nextpkt;
4591 1.1 rjs n->m_nextpkt = NULL;
4592 1.1 rjs sctp_m_freem(n);
4593 1.1 rjs n = mnext;
4594 1.1 rjs }
4595 1.1 rjs } else if (m)
4596 1.1 rjs sctp_m_freem(m);
4597 1.1 rjs
4598 1.1 rjs return (error);
4599 1.1 rjs }
4600 1.1 rjs
4601 1.1 rjs static struct mbuf *
4602 1.1 rjs sctp_copy_mbufchain(struct mbuf *clonechain,
4603 1.1 rjs struct mbuf *outchain)
4604 1.1 rjs {
4605 1.1 rjs struct mbuf *appendchain;
4606 1.1 rjs #if defined(__FreeBSD__) || defined(__NetBSD__)
4607 1.1 rjs /* Supposedly m_copypacket is an optimization, use it if we can */
4608 1.1 rjs if (clonechain->m_flags & M_PKTHDR) {
4609 1.1 rjs appendchain = m_copypacket(clonechain, M_DONTWAIT);
4610 1.1 rjs sctp_pegs[SCTP_CACHED_SRC]++;
4611 1.1 rjs } else
4612 1.15 maxv appendchain = m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
4613 1.1 rjs #elif defined(__APPLE__)
4614 1.1 rjs appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
4615 1.1 rjs #else
4616 1.1 rjs appendchain = m_copy(clonechain, 0, M_COPYALL);
4617 1.1 rjs #endif
4618 1.1 rjs
4619 1.1 rjs if (appendchain == NULL) {
4620 1.1 rjs /* error */
4621 1.1 rjs if (outchain)
4622 1.1 rjs sctp_m_freem(outchain);
4623 1.1 rjs return (NULL);
4624 1.1 rjs }
4625 1.1 rjs if (outchain) {
4626 1.1 rjs /* tack on to the end */
4627 1.1 rjs struct mbuf *m;
4628 1.1 rjs m = outchain;
4629 1.1 rjs while (m) {
4630 1.1 rjs if (m->m_next == NULL) {
4631 1.1 rjs m->m_next = appendchain;
4632 1.1 rjs break;
4633 1.1 rjs }
4634 1.1 rjs m = m->m_next;
4635 1.1 rjs }
4636 1.1 rjs if (outchain->m_flags & M_PKTHDR) {
4637 1.1 rjs int append_tot;
4638 1.1 rjs struct mbuf *t;
4639 1.1 rjs t = appendchain;
4640 1.1 rjs append_tot = 0;
4641 1.1 rjs while (t) {
4642 1.1 rjs append_tot += t->m_len;
4643 1.1 rjs t = t->m_next;
4644 1.1 rjs }
4645 1.1 rjs outchain->m_pkthdr.len += append_tot;
4646 1.1 rjs }
4647 1.1 rjs return (outchain);
4648 1.1 rjs } else {
4649 1.1 rjs return (appendchain);
4650 1.1 rjs }
4651 1.1 rjs }
4652 1.1 rjs
4653 1.1 rjs static void
4654 1.1 rjs sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
4655 1.1 rjs {
4656 1.1 rjs struct sctp_copy_all *ca;
4657 1.1 rjs struct mbuf *m;
4658 1.1 rjs int turned_on_nonblock=0, ret;
4659 1.1 rjs
4660 1.1 rjs ca = (struct sctp_copy_all *)ptr;
4661 1.1 rjs if (ca->m == NULL) {
4662 1.1 rjs return;
4663 1.1 rjs }
4664 1.1 rjs if (ca->inp != inp) {
4665 1.1 rjs /* TSNH */
4666 1.1 rjs return;
4667 1.1 rjs }
4668 1.1 rjs m = sctp_copy_mbufchain(ca->m, NULL);
4669 1.1 rjs if (m == NULL) {
4670 1.1 rjs /* can't copy so we are done */
4671 1.1 rjs ca->cnt_failed++;
4672 1.1 rjs return;
4673 1.1 rjs }
4674 1.1 rjs if ((stcb->sctp_socket->so_state & SS_NBIO) == 0) {
4675 1.1 rjs /* we have to do this non-blocking */
4676 1.1 rjs turned_on_nonblock = 1;
4677 1.1 rjs stcb->sctp_socket->so_state |= SS_NBIO;
4678 1.1 rjs }
4679 1.1 rjs ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m, &ca->sndrcv, 0);
4680 1.1 rjs if (turned_on_nonblock) {
4681 1.1 rjs /* we turned on non-blocking so turn it off */
4682 1.1 rjs stcb->sctp_socket->so_state &= ~SS_NBIO;
4683 1.1 rjs }
4684 1.1 rjs if (ret) {
4685 1.1 rjs ca->cnt_failed++;
4686 1.1 rjs } else {
4687 1.1 rjs ca->cnt_sent++;
4688 1.1 rjs }
4689 1.1 rjs }
4690 1.1 rjs
4691 1.1 rjs static void
4692 1.1 rjs sctp_sendall_completes(void *ptr, u_int32_t val)
4693 1.1 rjs {
4694 1.1 rjs struct sctp_copy_all *ca;
4695 1.1 rjs ca = (struct sctp_copy_all *)ptr;
4696 1.1 rjs /* Do a notify here?
4697 1.1 rjs * Kacheong suggests that the notify
4698 1.1 rjs * be done at the send time.. so you would
4699 1.1 rjs * push up a notification if any send failed.
4700 1.1 rjs * Don't know if this is feasable since the
4701 1.1 rjs * only failures we have is "memory" related and
4702 1.1 rjs * if you cannot get an mbuf to send the data
4703 1.1 rjs * you surely can't get an mbuf to send up
4704 1.1 rjs * to notify the user you can't send the data :->
4705 1.1 rjs */
4706 1.1 rjs
4707 1.1 rjs /* now free everything */
4708 1.1 rjs m_freem(ca->m);
4709 1.1 rjs free(ca, M_PCB);
4710 1.1 rjs }
4711 1.1 rjs
4712 1.1 rjs
4713 1.1 rjs #define MC_ALIGN(m, len) do { \
4714 1.1 rjs (m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
4715 1.1 rjs } while (0)
4716 1.1 rjs
4717 1.1 rjs
4718 1.1 rjs
4719 1.1 rjs static struct mbuf *
4720 1.1 rjs sctp_copy_out_all(struct uio *uio, int len)
4721 1.1 rjs {
4722 1.1 rjs struct mbuf *ret, *at;
4723 1.1 rjs int left, willcpy, cancpy, error;
4724 1.1 rjs
4725 1.1 rjs MGETHDR(ret, M_WAIT, MT_HEADER);
4726 1.1 rjs if (ret == NULL) {
4727 1.1 rjs /* TSNH */
4728 1.1 rjs return (NULL);
4729 1.1 rjs }
4730 1.1 rjs left = len;
4731 1.1 rjs ret->m_len = 0;
4732 1.1 rjs ret->m_pkthdr.len = len;
4733 1.1 rjs MCLGET(ret, M_WAIT);
4734 1.1 rjs if (ret == NULL) {
4735 1.1 rjs return (NULL);
4736 1.1 rjs }
4737 1.1 rjs if ((ret->m_flags & M_EXT) == 0) {
4738 1.1 rjs m_freem (ret);
4739 1.1 rjs return (NULL);
4740 1.1 rjs }
4741 1.1 rjs cancpy = M_TRAILINGSPACE(ret);
4742 1.16 riastrad willcpy = uimin(cancpy, left);
4743 1.1 rjs at = ret;
4744 1.1 rjs while (left > 0) {
4745 1.1 rjs /* Align data to the end */
4746 1.1 rjs MC_ALIGN(at, willcpy);
4747 1.1 rjs error = uiomove(mtod(at, void *), willcpy, uio);
4748 1.1 rjs if (error) {
4749 1.1 rjs err_out_now:
4750 1.1 rjs m_freem(ret);
4751 1.1 rjs return (NULL);
4752 1.1 rjs }
4753 1.1 rjs at->m_len = willcpy;
4754 1.1 rjs at->m_nextpkt = at->m_next = 0;
4755 1.1 rjs left -= willcpy;
4756 1.1 rjs if (left > 0) {
4757 1.1 rjs MGET(at->m_next, M_WAIT, MT_DATA);
4758 1.1 rjs if (at->m_next == NULL) {
4759 1.1 rjs goto err_out_now;
4760 1.1 rjs }
4761 1.1 rjs at = at->m_next;
4762 1.1 rjs at->m_len = 0;
4763 1.1 rjs MCLGET(at, M_WAIT);
4764 1.1 rjs if (at == NULL) {
4765 1.1 rjs goto err_out_now;
4766 1.1 rjs }
4767 1.1 rjs if ((at->m_flags & M_EXT) == 0) {
4768 1.1 rjs goto err_out_now;
4769 1.1 rjs }
4770 1.1 rjs cancpy = M_TRAILINGSPACE(at);
4771 1.16 riastrad willcpy = uimin(cancpy, left);
4772 1.1 rjs }
4773 1.1 rjs }
4774 1.1 rjs return (ret);
4775 1.1 rjs }
4776 1.1 rjs
4777 1.1 rjs static int
4778 1.1 rjs sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
4779 1.1 rjs {
4780 1.1 rjs int ret;
4781 1.1 rjs struct sctp_copy_all *ca;
4782 1.1 rjs ca = malloc(sizeof(struct sctp_copy_all), M_PCB, M_WAIT);
4783 1.1 rjs if (ca == NULL) {
4784 1.1 rjs m_freem(m);
4785 1.1 rjs return (ENOMEM);
4786 1.1 rjs }
4787 1.1 rjs memset (ca, 0, sizeof(struct sctp_copy_all));
4788 1.1 rjs
4789 1.1 rjs ca->inp = inp;
4790 1.1 rjs ca->sndrcv = *srcv;
4791 1.1 rjs /* take off the sendall flag, it would
4792 1.1 rjs * be bad if we failed to do this :-0
4793 1.1 rjs */
4794 1.12 rjs ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
4795 1.1 rjs
4796 1.1 rjs /* get length and mbuf chain */
4797 1.1 rjs if (uio) {
4798 1.1 rjs ca->sndlen = uio->uio_resid;
4799 1.1 rjs ca->m = sctp_copy_out_all(uio, ca->sndlen);
4800 1.1 rjs if (ca->m == NULL) {
4801 1.1 rjs free(ca, M_PCB);
4802 1.1 rjs return (ENOMEM);
4803 1.1 rjs }
4804 1.1 rjs } else {
4805 1.1 rjs if ((m->m_flags & M_PKTHDR) == 0) {
4806 1.1 rjs ca->sndlen = 0;
4807 1.1 rjs while(m) {
4808 1.1 rjs ca->sndlen += m->m_len;
4809 1.1 rjs m = m->m_next;
4810 1.1 rjs }
4811 1.1 rjs } else {
4812 1.1 rjs ca->sndlen = m->m_pkthdr.len;
4813 1.1 rjs }
4814 1.1 rjs ca->m = m;
4815 1.1 rjs }
4816 1.1 rjs
4817 1.1 rjs ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
4818 1.1 rjs (void *)ca, 0, sctp_sendall_completes, inp);
4819 1.1 rjs if (ret) {
4820 1.1 rjs #ifdef SCTP_DEBUG
4821 1.1 rjs printf("Failed to initate iterator to takeover associations\n");
4822 1.1 rjs #endif
4823 1.1 rjs free(ca, M_PCB);
4824 1.1 rjs return (EFAULT);
4825 1.1 rjs
4826 1.1 rjs }
4827 1.1 rjs return (0);
4828 1.1 rjs }
4829 1.1 rjs
4830 1.1 rjs
4831 1.1 rjs void
4832 1.1 rjs sctp_toss_old_cookies(struct sctp_association *asoc)
4833 1.1 rjs {
4834 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
4835 1.1 rjs chk = TAILQ_FIRST(&asoc->control_send_queue);
4836 1.1 rjs while (chk) {
4837 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
4838 1.1 rjs if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
4839 1.1 rjs TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4840 1.1 rjs if (chk->data) {
4841 1.1 rjs sctp_m_freem(chk->data);
4842 1.1 rjs chk->data = NULL;
4843 1.1 rjs }
4844 1.1 rjs asoc->ctrl_queue_cnt--;
4845 1.1 rjs if (chk->whoTo)
4846 1.1 rjs sctp_free_remote_addr(chk->whoTo);
4847 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4848 1.1 rjs sctppcbinfo.ipi_count_chunk--;
4849 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4850 1.1 rjs panic("Chunk count is negative");
4851 1.1 rjs }
4852 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4853 1.1 rjs }
4854 1.1 rjs chk = nchk;
4855 1.1 rjs }
4856 1.1 rjs }
4857 1.1 rjs
4858 1.1 rjs void
4859 1.1 rjs sctp_toss_old_asconf(struct sctp_tcb *stcb)
4860 1.1 rjs {
4861 1.1 rjs struct sctp_association *asoc;
4862 1.1 rjs struct sctp_tmit_chunk *chk, *chk_tmp;
4863 1.1 rjs
4864 1.1 rjs asoc = &stcb->asoc;
4865 1.1 rjs for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
4866 1.1 rjs chk = chk_tmp) {
4867 1.1 rjs /* get next chk */
4868 1.1 rjs chk_tmp = TAILQ_NEXT(chk, sctp_next);
4869 1.1 rjs /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
4870 1.1 rjs if (chk->rec.chunk_id == SCTP_ASCONF) {
4871 1.1 rjs TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4872 1.1 rjs if (chk->data) {
4873 1.1 rjs sctp_m_freem(chk->data);
4874 1.1 rjs chk->data = NULL;
4875 1.1 rjs }
4876 1.1 rjs asoc->ctrl_queue_cnt--;
4877 1.1 rjs if (chk->whoTo)
4878 1.1 rjs sctp_free_remote_addr(chk->whoTo);
4879 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4880 1.1 rjs sctppcbinfo.ipi_count_chunk--;
4881 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4882 1.1 rjs panic("Chunk count is negative");
4883 1.1 rjs }
4884 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4885 1.1 rjs }
4886 1.1 rjs }
4887 1.1 rjs }
4888 1.1 rjs
4889 1.1 rjs
4890 1.1 rjs static void
4891 1.1 rjs sctp_clean_up_datalist(struct sctp_tcb *stcb,
4892 1.1 rjs struct sctp_association *asoc,
4893 1.1 rjs struct sctp_tmit_chunk **data_list,
4894 1.1 rjs int bundle_at,
4895 1.1 rjs struct sctp_nets *net)
4896 1.1 rjs {
4897 1.1 rjs int i;
4898 1.1 rjs for (i = 0; i < bundle_at; i++) {
4899 1.1 rjs /* off of the send queue */
4900 1.1 rjs if (i) {
4901 1.1 rjs /* Any chunk NOT 0 you zap the time
4902 1.1 rjs * chunk 0 gets zapped or set based on
4903 1.27 msaitoh * if a RTO measurement is needed.
4904 1.1 rjs */
4905 1.1 rjs data_list[i]->do_rtt = 0;
4906 1.1 rjs }
4907 1.1 rjs /* record time */
4908 1.1 rjs data_list[i]->sent_rcv_time = net->last_sent_time;
4909 1.1 rjs TAILQ_REMOVE(&asoc->send_queue,
4910 1.1 rjs data_list[i],
4911 1.1 rjs sctp_next);
4912 1.1 rjs /* on to the sent queue */
4913 1.1 rjs TAILQ_INSERT_TAIL(&asoc->sent_queue,
4914 1.1 rjs data_list[i],
4915 1.1 rjs sctp_next);
4916 1.1 rjs /* This does not lower until the cum-ack passes it */
4917 1.1 rjs asoc->sent_queue_cnt++;
4918 1.1 rjs asoc->send_queue_cnt--;
4919 1.1 rjs if ((asoc->peers_rwnd <= 0) &&
4920 1.1 rjs (asoc->total_flight == 0) &&
4921 1.1 rjs (bundle_at == 1)) {
4922 1.1 rjs /* Mark the chunk as being a window probe */
4923 1.1 rjs #ifdef SCTP_DEBUG
4924 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4925 1.1 rjs printf("WINDOW PROBE SET\n");
4926 1.1 rjs }
4927 1.1 rjs #endif
4928 1.1 rjs sctp_pegs[SCTP_WINDOW_PROBES]++;
4929 1.1 rjs data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
4930 1.1 rjs } else {
4931 1.1 rjs data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
4932 1.1 rjs }
4933 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
4934 1.1 rjs sctp_audit_log(0xC2, 3);
4935 1.1 rjs #endif
4936 1.1 rjs data_list[i]->sent = SCTP_DATAGRAM_SENT;
4937 1.1 rjs data_list[i]->snd_count = 1;
4938 1.1 rjs net->flight_size += data_list[i]->book_size;
4939 1.1 rjs asoc->total_flight += data_list[i]->book_size;
4940 1.1 rjs asoc->total_flight_count++;
4941 1.1 rjs #ifdef SCTP_LOG_RWND
4942 1.1 rjs sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
4943 1.1 rjs asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
4944 1.1 rjs #endif
4945 1.1 rjs asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
4946 1.1 rjs (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
4947 1.1 rjs if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4948 1.1 rjs /* SWS sender side engages */
4949 1.1 rjs asoc->peers_rwnd = 0;
4950 1.1 rjs }
4951 1.1 rjs }
4952 1.1 rjs }
4953 1.1 rjs
4954 1.1 rjs static void
4955 1.1 rjs sctp_clean_up_ctl(struct sctp_association *asoc)
4956 1.1 rjs {
4957 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
4958 1.1 rjs for (chk = TAILQ_FIRST(&asoc->control_send_queue);
4959 1.1 rjs chk; chk = nchk) {
4960 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
4961 1.1 rjs if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
4962 1.1 rjs (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
4963 1.1 rjs (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
4964 1.1 rjs (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
4965 1.1 rjs (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
4966 1.1 rjs (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
4967 1.1 rjs (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
4968 1.1 rjs (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
4969 1.1 rjs (chk->rec.chunk_id == SCTP_ECN_CWR) ||
4970 1.1 rjs (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
4971 1.1 rjs /* Stray chunks must be cleaned up */
4972 1.1 rjs clean_up_anyway:
4973 1.1 rjs TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4974 1.1 rjs if (chk->data) {
4975 1.1 rjs sctp_m_freem(chk->data);
4976 1.1 rjs chk->data = NULL;
4977 1.1 rjs }
4978 1.1 rjs asoc->ctrl_queue_cnt--;
4979 1.1 rjs sctp_free_remote_addr(chk->whoTo);
4980 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4981 1.1 rjs sctppcbinfo.ipi_count_chunk--;
4982 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4983 1.1 rjs panic("Chunk count is negative");
4984 1.1 rjs }
4985 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
4986 1.1 rjs } else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
4987 1.1 rjs struct sctp_stream_reset_req *strreq;
4988 1.1 rjs /* special handling, we must look into the param */
4989 1.1 rjs strreq = mtod(chk->data, struct sctp_stream_reset_req *);
4990 1.1 rjs if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
4991 1.1 rjs goto clean_up_anyway;
4992 1.1 rjs }
4993 1.1 rjs }
4994 1.1 rjs }
4995 1.1 rjs }
4996 1.1 rjs
4997 1.1 rjs static int
4998 1.1 rjs sctp_move_to_outqueue(struct sctp_tcb *stcb,
4999 1.1 rjs struct sctp_stream_out *strq)
5000 1.1 rjs {
5001 1.1 rjs /* Move from the stream to the send_queue keeping track of the total */
5002 1.1 rjs struct sctp_association *asoc;
5003 1.1 rjs int tot_moved = 0;
5004 1.1 rjs int failed = 0;
5005 1.1 rjs int padval;
5006 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
5007 1.1 rjs struct sctp_data_chunk *dchkh;
5008 1.1 rjs struct sctpchunk_listhead tmp;
5009 1.1 rjs struct mbuf *orig;
5010 1.1 rjs
5011 1.1 rjs asoc = &stcb->asoc;
5012 1.1 rjs TAILQ_INIT(&tmp);
5013 1.1 rjs chk = TAILQ_FIRST(&strq->outqueue);
5014 1.1 rjs while (chk) {
5015 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
5016 1.1 rjs /* now put in the chunk header */
5017 1.1 rjs orig = chk->data;
5018 1.1 rjs M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
5019 1.1 rjs if (chk->data == NULL) {
5020 1.1 rjs /* HELP */
5021 1.1 rjs failed++;
5022 1.1 rjs break;
5023 1.1 rjs }
5024 1.1 rjs if (orig != chk->data) {
5025 1.1 rjs /* A new mbuf was added, account for it */
5026 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5027 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5028 1.1 rjs stcb->sctp_socket->so_snd.sb_mbcnt += MSIZE;
5029 1.1 rjs }
5030 1.1 rjs #ifdef SCTP_MBCNT_LOGGING
5031 1.1 rjs sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
5032 1.1 rjs asoc->total_output_queue_size,
5033 1.1 rjs 0,
5034 1.1 rjs asoc->total_output_mbuf_queue_size,
5035 1.1 rjs MSIZE);
5036 1.1 rjs #endif
5037 1.1 rjs stcb->asoc.total_output_mbuf_queue_size += MSIZE;
5038 1.1 rjs chk->mbcnt += MSIZE;
5039 1.1 rjs }
5040 1.1 rjs chk->send_size += sizeof(struct sctp_data_chunk);
5041 1.1 rjs /* This should NOT have to do anything, but
5042 1.1 rjs * I would rather be cautious
5043 1.1 rjs */
5044 1.1 rjs if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
5045 1.1 rjs m_pullup(chk->data, sizeof(struct sctp_data_chunk));
5046 1.1 rjs if (chk->data == NULL) {
5047 1.1 rjs failed++;
5048 1.1 rjs break;
5049 1.1 rjs }
5050 1.1 rjs }
5051 1.1 rjs dchkh = mtod(chk->data, struct sctp_data_chunk *);
5052 1.1 rjs dchkh->ch.chunk_length = htons(chk->send_size);
5053 1.1 rjs /* Chunks must be padded to even word boundary */
5054 1.1 rjs padval = chk->send_size % 4;
5055 1.1 rjs if (padval) {
5056 1.1 rjs /* For fragmented messages this should not
5057 1.1 rjs * run except possibly on the last chunk
5058 1.1 rjs */
5059 1.1 rjs if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
5060 1.1 rjs /* we are in big big trouble no mbufs :< */
5061 1.1 rjs failed++;
5062 1.1 rjs break;
5063 1.1 rjs }
5064 1.1 rjs chk->send_size += (4 - padval);
5065 1.1 rjs }
5066 1.1 rjs /* pull from stream queue */
5067 1.1 rjs TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
5068 1.1 rjs asoc->stream_queue_cnt--;
5069 1.1 rjs TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
5070 1.1 rjs /* add it in to the size of moved chunks */
5071 1.1 rjs if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5072 1.1 rjs /* we pull only one message */
5073 1.1 rjs break;
5074 1.1 rjs }
5075 1.1 rjs chk = nchk;
5076 1.1 rjs }
5077 1.1 rjs if (failed) {
5078 1.1 rjs /* Gak, we just lost the user message */
5079 1.1 rjs chk = TAILQ_FIRST(&tmp);
5080 1.1 rjs while (chk) {
5081 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
5082 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
5083 1.1 rjs
5084 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5085 1.1 rjs (SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
5086 1.1 rjs chk);
5087 1.1 rjs
5088 1.1 rjs if (chk->data) {
5089 1.1 rjs sctp_m_freem(chk->data);
5090 1.1 rjs chk->data = NULL;
5091 1.1 rjs }
5092 1.1 rjs if (chk->whoTo) {
5093 1.1 rjs sctp_free_remote_addr(chk->whoTo);
5094 1.1 rjs chk->whoTo = NULL;
5095 1.1 rjs }
5096 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5097 1.1 rjs sctppcbinfo.ipi_count_chunk--;
5098 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5099 1.1 rjs panic("Chunk count is negative");
5100 1.1 rjs }
5101 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
5102 1.1 rjs chk = nchk;
5103 1.1 rjs }
5104 1.1 rjs return (0);
5105 1.1 rjs }
5106 1.1 rjs /* now pull them off of temp wheel */
5107 1.1 rjs chk = TAILQ_FIRST(&tmp);
5108 1.1 rjs while (chk) {
5109 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
5110 1.1 rjs /* insert on send_queue */
5111 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
5112 1.1 rjs TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5113 1.1 rjs asoc->send_queue_cnt++;
5114 1.1 rjs /* assign TSN */
5115 1.1 rjs chk->rec.data.TSN_seq = asoc->sending_seq++;
5116 1.1 rjs
5117 1.1 rjs dchkh = mtod(chk->data, struct sctp_data_chunk *);
5118 1.1 rjs /* Put the rest of the things in place now. Size
5119 1.1 rjs * was done earlier in previous loop prior to
5120 1.1 rjs * padding.
5121 1.1 rjs */
5122 1.1 rjs dchkh->ch.chunk_type = SCTP_DATA;
5123 1.1 rjs dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5124 1.1 rjs dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5125 1.1 rjs dchkh->dp.stream_id = htons(strq->stream_no);
5126 1.1 rjs dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5127 1.1 rjs dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5128 1.1 rjs /* total count moved */
5129 1.1 rjs tot_moved += chk->send_size;
5130 1.1 rjs chk = nchk;
5131 1.1 rjs }
5132 1.1 rjs return (tot_moved);
5133 1.1 rjs }
5134 1.1 rjs
5135 1.1 rjs static void
5136 1.1 rjs sctp_fill_outqueue(struct sctp_tcb *stcb,
5137 1.1 rjs struct sctp_nets *net)
5138 1.1 rjs {
5139 1.1 rjs struct sctp_association *asoc;
5140 1.1 rjs struct sctp_tmit_chunk *chk;
5141 1.1 rjs struct sctp_stream_out *strq, *strqn;
5142 1.1 rjs int mtu_fromwheel, goal_mtu;
5143 1.1 rjs unsigned int moved, seenend, cnt_mvd=0;
5144 1.1 rjs
5145 1.1 rjs asoc = &stcb->asoc;
5146 1.1 rjs /* Attempt to move at least 1 MTU's worth
5147 1.1 rjs * onto the wheel for each destination address
5148 1.1 rjs */
5149 1.1 rjs goal_mtu = net->cwnd - net->flight_size;
5150 1.1 rjs if ((unsigned int)goal_mtu < net->mtu) {
5151 1.1 rjs goal_mtu = net->mtu;
5152 1.1 rjs }
5153 1.1 rjs if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
5154 1.1 rjs sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
5155 1.1 rjs }
5156 1.1 rjs seenend = moved = mtu_fromwheel = 0;
5157 1.1 rjs if (asoc->last_out_stream == NULL) {
5158 1.1 rjs strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5159 1.1 rjs if (asoc->last_out_stream == NULL) {
5160 1.1 rjs /* huh nothing on the wheel, TSNH */
5161 1.1 rjs return;
5162 1.1 rjs }
5163 1.1 rjs goto done_it;
5164 1.1 rjs }
5165 1.1 rjs strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5166 1.1 rjs done_it:
5167 1.1 rjs if (strq == NULL) {
5168 1.1 rjs asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5169 1.1 rjs }
5170 1.1 rjs while (mtu_fromwheel < goal_mtu) {
5171 1.1 rjs if (strq == NULL) {
5172 1.1 rjs if (seenend == 0) {
5173 1.1 rjs seenend = 1;
5174 1.1 rjs strq = TAILQ_FIRST(&asoc->out_wheel);
5175 1.1 rjs } else if ((moved == 0) && (seenend)) {
5176 1.1 rjs /* none left on the wheel */
5177 1.1 rjs sctp_pegs[SCTP_MOVED_NLEF]++;
5178 1.1 rjs return;
5179 1.1 rjs } else if (moved) {
5180 1.1 rjs /*
5181 1.1 rjs * clear the flags and rotate back through
5182 1.1 rjs * again
5183 1.1 rjs */
5184 1.1 rjs moved = 0;
5185 1.1 rjs seenend = 0;
5186 1.1 rjs strq = TAILQ_FIRST(&asoc->out_wheel);
5187 1.1 rjs }
5188 1.1 rjs if (strq == NULL)
5189 1.1 rjs break;
5190 1.1 rjs continue;
5191 1.1 rjs }
5192 1.1 rjs strqn = TAILQ_NEXT(strq, next_spoke);
5193 1.1 rjs if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
5194 1.1 rjs /* none left on this queue, prune a spoke? */
5195 1.1 rjs sctp_remove_from_wheel(asoc, strq);
5196 1.1 rjs if (strq == asoc->last_out_stream) {
5197 1.1 rjs /* the last one we used went off the wheel */
5198 1.1 rjs asoc->last_out_stream = NULL;
5199 1.1 rjs }
5200 1.1 rjs strq = strqn;
5201 1.1 rjs continue;
5202 1.1 rjs }
5203 1.1 rjs if (chk->whoTo != net) {
5204 1.1 rjs /* Skip this stream, first one on stream
5205 1.1 rjs * does not head to our current destination.
5206 1.1 rjs */
5207 1.1 rjs strq = strqn;
5208 1.1 rjs continue;
5209 1.1 rjs }
5210 1.1 rjs mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
5211 1.1 rjs cnt_mvd++;
5212 1.1 rjs moved++;
5213 1.1 rjs asoc->last_out_stream = strq;
5214 1.1 rjs strq = strqn;
5215 1.1 rjs }
5216 1.1 rjs sctp_pegs[SCTP_MOVED_MAX]++;
5217 1.1 rjs #ifdef SCTP_DEBUG
5218 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5219 1.1 rjs printf("Ok we moved %d chunks to send queue\n",
5220 1.1 rjs moved);
5221 1.1 rjs }
5222 1.1 rjs #endif
5223 1.1 rjs if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
5224 1.1 rjs sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
5225 1.1 rjs }
5226 1.1 rjs }
5227 1.1 rjs
5228 1.1 rjs void
5229 1.1 rjs sctp_fix_ecn_echo(struct sctp_association *asoc)
5230 1.1 rjs {
5231 1.1 rjs struct sctp_tmit_chunk *chk;
5232 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5233 1.1 rjs if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
5234 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
5235 1.1 rjs }
5236 1.1 rjs }
5237 1.1 rjs }
5238 1.1 rjs
5239 1.1 rjs static void
5240 1.1 rjs sctp_move_to_an_alt(struct sctp_tcb *stcb,
5241 1.1 rjs struct sctp_association *asoc,
5242 1.1 rjs struct sctp_nets *net)
5243 1.1 rjs {
5244 1.1 rjs struct sctp_tmit_chunk *chk;
5245 1.1 rjs struct sctp_nets *a_net;
5246 1.1 rjs a_net = sctp_find_alternate_net(stcb, net);
5247 1.1 rjs if ((a_net != net) &&
5248 1.1 rjs ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5249 1.1 rjs /*
5250 1.1 rjs * We only proceed if a valid alternate is found that is
5251 1.1 rjs * not this one and is reachable. Here we must move all
5252 1.1 rjs * chunks queued in the send queue off of the destination
5253 1.1 rjs * address to our alternate.
5254 1.1 rjs */
5255 1.1 rjs TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5256 1.1 rjs if (chk->whoTo == net) {
5257 1.1 rjs /* Move the chunk to our alternate */
5258 1.1 rjs sctp_free_remote_addr(chk->whoTo);
5259 1.1 rjs chk->whoTo = a_net;
5260 1.1 rjs a_net->ref_count++;
5261 1.1 rjs }
5262 1.1 rjs }
5263 1.1 rjs }
5264 1.1 rjs }
5265 1.1 rjs
5266 1.1 rjs static int sctp_from_user_send=0;
5267 1.1 rjs
5268 1.1 rjs static int
5269 1.1 rjs sctp_med_chunk_output(struct sctp_inpcb *inp,
5270 1.1 rjs struct sctp_tcb *stcb,
5271 1.1 rjs struct sctp_association *asoc,
5272 1.1 rjs int *num_out,
5273 1.1 rjs int *reason_code,
5274 1.1 rjs int control_only, int *cwnd_full, int from_where,
5275 1.1 rjs struct timeval *now, int *now_filled)
5276 1.1 rjs {
5277 1.1 rjs /*
5278 1.1 rjs * Ok this is the generic chunk service queue.
5279 1.1 rjs * we must do the following:
5280 1.1 rjs * - Service the stream queue that is next, moving any message
5281 1.1 rjs * (note I must get a complete message i.e. FIRST/MIDDLE and
5282 1.1 rjs * LAST to the out queue in one pass) and assigning TSN's
5283 1.1 rjs * - Check to see if the cwnd/rwnd allows any output, if so we
5284 1.1 rjs * go ahead and fomulate and send the low level chunks. Making
5285 1.1 rjs * sure to combine any control in the control chunk queue also.
5286 1.1 rjs */
5287 1.1 rjs struct sctp_nets *net;
5288 1.1 rjs struct mbuf *outchain;
5289 1.1 rjs struct sctp_tmit_chunk *chk, *nchk;
5290 1.1 rjs struct sctphdr *shdr;
5291 1.1 rjs /* temp arrays for unlinking */
5292 1.1 rjs struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5293 1.1 rjs int no_fragmentflg, error;
5294 1.1 rjs int one_chunk, hbflag;
5295 1.1 rjs int asconf, cookie, no_out_cnt;
5296 1.1 rjs int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
5297 1.1 rjs unsigned int mtu, r_mtu, omtu;
5298 1.1 rjs *num_out = 0;
5299 1.1 rjs cwnd_full_ind = 0;
5300 1.1 rjs ctl_cnt = no_out_cnt = asconf = cookie = 0;
5301 1.1 rjs /*
5302 1.1 rjs * First lets prime the pump. For each destination, if there
5303 1.1 rjs * is room in the flight size, attempt to pull an MTU's worth
5304 1.1 rjs * out of the stream queues into the general send_queue
5305 1.1 rjs */
5306 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
5307 1.1 rjs sctp_audit_log(0xC2, 2);
5308 1.1 rjs #endif
5309 1.1 rjs #ifdef SCTP_DEBUG
5310 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5311 1.1 rjs printf("***********************\n");
5312 1.1 rjs }
5313 1.1 rjs #endif
5314 1.1 rjs hbflag = 0;
5315 1.1 rjs if (control_only)
5316 1.1 rjs no_data_chunks = 1;
5317 1.1 rjs else
5318 1.1 rjs no_data_chunks = 0;
5319 1.1 rjs
5320 1.1 rjs /* Nothing to possible to send? */
5321 1.1 rjs if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5322 1.1 rjs TAILQ_EMPTY(&asoc->send_queue) &&
5323 1.1 rjs TAILQ_EMPTY(&asoc->out_wheel)) {
5324 1.1 rjs #ifdef SCTP_DEBUG
5325 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5326 1.1 rjs printf("All wheels empty\n");
5327 1.1 rjs }
5328 1.1 rjs #endif
5329 1.1 rjs return (0);
5330 1.1 rjs }
5331 1.1 rjs if (asoc->peers_rwnd <= 0) {
5332 1.1 rjs /* No room in peers rwnd */
5333 1.1 rjs *cwnd_full = 1;
5334 1.1 rjs *reason_code = 1;
5335 1.1 rjs if (asoc->total_flight > 0) {
5336 1.1 rjs /* we are allowed one chunk in flight */
5337 1.1 rjs no_data_chunks = 1;
5338 1.1 rjs sctp_pegs[SCTP_RWND_BLOCKED]++;
5339 1.1 rjs }
5340 1.1 rjs }
5341 1.1 rjs #ifdef SCTP_DEBUG
5342 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5343 1.1 rjs printf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
5344 1.1 rjs (int)no_data_chunks,
5345 1.1 rjs (int)asoc->total_flight, (int)asoc->peers_rwnd);
5346 1.1 rjs }
5347 1.1 rjs #endif
5348 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5349 1.1 rjs #ifdef SCTP_DEBUG
5350 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5351 1.1 rjs printf("net:%p fs:%d cwnd:%d\n",
5352 1.1 rjs net, net->flight_size, net->cwnd);
5353 1.1 rjs }
5354 1.1 rjs #endif
5355 1.1 rjs if (net->flight_size >= net->cwnd) {
5356 1.1 rjs /* skip this network, no room */
5357 1.1 rjs cwnd_full_ind++;
5358 1.1 rjs #ifdef SCTP_DEBUG
5359 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5360 1.1 rjs printf("Ok skip fillup->fs:%d > cwnd:%d\n",
5361 1.1 rjs net->flight_size,
5362 1.1 rjs net->cwnd);
5363 1.1 rjs }
5364 1.1 rjs #endif
5365 1.1 rjs sctp_pegs[SCTP_CWND_NOFILL]++;
5366 1.1 rjs continue;
5367 1.1 rjs }
5368 1.1 rjs /*
5369 1.1 rjs * spin through the stream queues moving one message and
5370 1.1 rjs * assign TSN's as appropriate.
5371 1.1 rjs */
5372 1.1 rjs sctp_fill_outqueue(stcb, net);
5373 1.1 rjs }
5374 1.1 rjs *cwnd_full = cwnd_full_ind;
5375 1.1 rjs /* now service each destination and send out what we can for it */
5376 1.1 rjs #ifdef SCTP_DEBUG
5377 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5378 1.1 rjs int chk_cnt = 0;
5379 1.1 rjs TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5380 1.1 rjs chk_cnt++;
5381 1.1 rjs }
5382 1.1 rjs printf("We have %d chunks on the send_queue\n", chk_cnt);
5383 1.1 rjs chk_cnt = 0;
5384 1.1 rjs TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5385 1.1 rjs chk_cnt++;
5386 1.1 rjs }
5387 1.1 rjs printf("We have %d chunks on the sent_queue\n", chk_cnt);
5388 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5389 1.1 rjs chk_cnt++;
5390 1.1 rjs }
5391 1.1 rjs printf("We have %d chunks on the control_queue\n", chk_cnt);
5392 1.1 rjs }
5393 1.1 rjs #endif
5394 1.1 rjs /* If we have data to send, and DSACK is running, stop it
5395 1.1 rjs * and build a SACK to dump on to bundle with output. This
5396 1.1 rjs * actually MAY make it so the bundling does not occur if
5397 1.1 rjs * the SACK is big but I think this is ok because basic SACK
5398 1.1 rjs * space is pre-reserved in our fragmentation size choice.
5399 1.1 rjs */
5400 1.1 rjs if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
5401 1.1 rjs (no_data_chunks == 0)) {
5402 1.1 rjs /* We will be sending something */
5403 1.1 rjs if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5404 1.1 rjs /* Yep a callout is pending */
5405 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5406 1.1 rjs stcb->sctp_ep,
5407 1.1 rjs stcb, NULL);
5408 1.1 rjs sctp_send_sack(stcb);
5409 1.1 rjs }
5410 1.1 rjs }
5411 1.1 rjs /* Nothing to send? */
5412 1.1 rjs if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5413 1.1 rjs (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5414 1.1 rjs return (0);
5415 1.1 rjs }
5416 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5417 1.8 ozaki struct rtentry *rt;
5418 1.1 rjs /* how much can we send? */
5419 1.1 rjs if (net->ref_count < 2) {
5420 1.1 rjs /* Ref-count of 1 so we cannot have data or control
5421 1.1 rjs * queued to this address. Skip it.
5422 1.1 rjs */
5423 1.1 rjs continue;
5424 1.1 rjs }
5425 1.1 rjs ctl_cnt = bundle_at = 0;
5426 1.1 rjs outchain = NULL;
5427 1.1 rjs no_fragmentflg = 1;
5428 1.1 rjs one_chunk = 0;
5429 1.1 rjs
5430 1.8 ozaki rt = rtcache_validate(&net->ro);
5431 1.8 ozaki if (rt != NULL) {
5432 1.1 rjs /* if we have a route and an ifp
5433 1.1 rjs * check to see if we have room to
5434 1.1 rjs * send to this guy
5435 1.1 rjs */
5436 1.1 rjs struct ifnet *ifp;
5437 1.1 rjs ifp = net->ro._ro_rt->rt_ifp;
5438 1.1 rjs if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
5439 1.1 rjs sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
5440 1.1 rjs #ifdef SCTP_LOG_MAXBURST
5441 1.1 rjs sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
5442 1.1 rjs #endif
5443 1.8 ozaki rtcache_unref(rt, &net->ro);
5444 1.1 rjs continue;
5445 1.1 rjs }
5446 1.8 ozaki rtcache_unref(rt, &net->ro);
5447 1.1 rjs }
5448 1.1 rjs if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
5449 1.1 rjs mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5450 1.1 rjs } else {
5451 1.1 rjs mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5452 1.1 rjs }
5453 1.1 rjs if (mtu > asoc->peers_rwnd) {
5454 1.1 rjs if (asoc->total_flight > 0) {
5455 1.1 rjs /* We have a packet in flight somewhere */
5456 1.1 rjs r_mtu = asoc->peers_rwnd;
5457 1.1 rjs } else {
5458 1.1 rjs /* We are always allowed to send one MTU out */
5459 1.1 rjs one_chunk = 1;
5460 1.1 rjs r_mtu = mtu;
5461 1.1 rjs }
5462 1.1 rjs } else {
5463 1.1 rjs r_mtu = mtu;
5464 1.1 rjs }
5465 1.1 rjs #ifdef SCTP_DEBUG
5466 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5467 1.1 rjs printf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
5468 1.1 rjs r_mtu, mtu, net, one_chunk);
5469 1.1 rjs }
5470 1.1 rjs #endif
5471 1.1 rjs /************************/
5472 1.1 rjs /* Control transmission */
5473 1.1 rjs /************************/
5474 1.1 rjs /* Now first lets go through the control queue */
5475 1.1 rjs for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5476 1.1 rjs chk; chk = nchk) {
5477 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
5478 1.1 rjs if (chk->whoTo != net) {
5479 1.1 rjs /*
5480 1.1 rjs * No, not sent to the network we are
5481 1.1 rjs * looking at
5482 1.1 rjs */
5483 1.1 rjs continue;
5484 1.1 rjs }
5485 1.1 rjs if (chk->data == NULL) {
5486 1.1 rjs continue;
5487 1.1 rjs }
5488 1.1 rjs if ((chk->data->m_flags & M_PKTHDR) == 0) {
5489 1.1 rjs /*
5490 1.1 rjs * NOTE: the chk queue MUST have the PKTHDR
5491 1.1 rjs * flag set on it with a total in the
5492 1.1 rjs * m_pkthdr.len field!! else the chunk will
5493 1.1 rjs * ALWAYS be skipped
5494 1.1 rjs */
5495 1.1 rjs continue;
5496 1.1 rjs }
5497 1.1 rjs if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5498 1.1 rjs /*
5499 1.1 rjs * It must be unsent. Cookies and ASCONF's
5500 1.1 rjs * hang around but there timers will force
5501 1.1 rjs * when marked for resend.
5502 1.1 rjs */
5503 1.1 rjs continue;
5504 1.1 rjs }
5505 1.1 rjs /* Here we do NOT factor the r_mtu */
5506 1.1 rjs if ((chk->data->m_pkthdr.len < (int)mtu) ||
5507 1.1 rjs (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5508 1.1 rjs /*
5509 1.1 rjs * We probably should glom the mbuf chain from
5510 1.1 rjs * the chk->data for control but the problem
5511 1.1 rjs * is it becomes yet one more level of
5512 1.1 rjs * tracking to do if for some reason output
5513 1.1 rjs * fails. Then I have got to reconstruct the
5514 1.1 rjs * merged control chain.. el yucko.. for now
5515 1.1 rjs * we take the easy way and do the copy
5516 1.1 rjs */
5517 1.1 rjs outchain = sctp_copy_mbufchain(chk->data,
5518 1.1 rjs outchain);
5519 1.1 rjs if (outchain == NULL) {
5520 1.1 rjs return (ENOMEM);
5521 1.1 rjs }
5522 1.1 rjs /* update our MTU size */
5523 1.1 rjs if (mtu > chk->data->m_pkthdr.len)
5524 1.1 rjs mtu -= chk->data->m_pkthdr.len;
5525 1.1 rjs else
5526 1.1 rjs mtu = 0;
5527 1.1 rjs /* Do clear IP_DF ? */
5528 1.1 rjs if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5529 1.1 rjs no_fragmentflg = 0;
5530 1.1 rjs }
5531 1.1 rjs /* Mark things to be removed, if needed */
5532 1.1 rjs if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5533 1.1 rjs (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5534 1.1 rjs (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5535 1.1 rjs (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5536 1.1 rjs (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5537 1.1 rjs (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5538 1.1 rjs (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5539 1.1 rjs (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5540 1.1 rjs (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5541 1.1 rjs (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5542 1.1 rjs
5543 1.1 rjs if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
5544 1.1 rjs hbflag = 1;
5545 1.1 rjs /* remove these chunks at the end */
5546 1.1 rjs if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
5547 1.1 rjs /* turn off the timer */
5548 1.1 rjs if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5549 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5550 1.1 rjs inp, stcb, net);
5551 1.1 rjs }
5552 1.1 rjs }
5553 1.1 rjs ctl_cnt++;
5554 1.1 rjs } else {
5555 1.1 rjs /*
5556 1.1 rjs * Other chunks, since they have
5557 1.1 rjs * timers running (i.e. COOKIE or
5558 1.1 rjs * ASCONF) we just "trust" that it
5559 1.1 rjs * gets sent or retransmitted.
5560 1.1 rjs */
5561 1.1 rjs ctl_cnt++;
5562 1.1 rjs if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
5563 1.1 rjs cookie = 1;
5564 1.1 rjs no_out_cnt = 1;
5565 1.1 rjs } else if (chk->rec.chunk_id == SCTP_ASCONF) {
5566 1.1 rjs /*
5567 1.1 rjs * set hb flag since we can use
5568 1.1 rjs * these for RTO
5569 1.1 rjs */
5570 1.1 rjs hbflag = 1;
5571 1.1 rjs asconf = 1;
5572 1.1 rjs }
5573 1.1 rjs chk->sent = SCTP_DATAGRAM_SENT;
5574 1.1 rjs chk->snd_count++;
5575 1.1 rjs }
5576 1.1 rjs if (mtu == 0) {
5577 1.1 rjs /*
5578 1.1 rjs * Ok we are out of room but we can
5579 1.1 rjs * output without effecting the flight
5580 1.1 rjs * size since this little guy is a
5581 1.1 rjs * control only packet.
5582 1.1 rjs */
5583 1.1 rjs if (asconf) {
5584 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5585 1.1 rjs asconf = 0;
5586 1.1 rjs }
5587 1.1 rjs if (cookie) {
5588 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5589 1.1 rjs cookie = 0;
5590 1.1 rjs }
5591 1.1 rjs if (outchain->m_len == 0) {
5592 1.1 rjs /*
5593 1.1 rjs * Special case for when you
5594 1.1 rjs * get a 0 len mbuf at the
5595 1.1 rjs * head due to the lack of a
5596 1.1 rjs * MHDR at the beginning.
5597 1.1 rjs */
5598 1.1 rjs outchain->m_len = sizeof(struct sctphdr);
5599 1.1 rjs } else {
5600 1.1 rjs M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5601 1.1 rjs if (outchain == NULL) {
5602 1.1 rjs /* no memory */
5603 1.1 rjs error = ENOBUFS;
5604 1.1 rjs goto error_out_again;
5605 1.1 rjs }
5606 1.1 rjs }
5607 1.1 rjs shdr = mtod(outchain, struct sctphdr *);
5608 1.1 rjs shdr->src_port = inp->sctp_lport;
5609 1.1 rjs shdr->dest_port = stcb->rport;
5610 1.1 rjs shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5611 1.1 rjs shdr->checksum = 0;
5612 1.1 rjs
5613 1.1 rjs if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5614 1.1 rjs rtcache_getdst(&net->ro),
5615 1.1 rjs outchain,
5616 1.1 rjs no_fragmentflg, 0, NULL, asconf))) {
5617 1.1 rjs if (error == ENOBUFS) {
5618 1.1 rjs asoc->ifp_had_enobuf = 1;
5619 1.1 rjs }
5620 1.1 rjs sctp_pegs[SCTP_DATA_OUT_ERR]++;
5621 1.1 rjs if (from_where == 0) {
5622 1.1 rjs sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5623 1.1 rjs }
5624 1.1 rjs error_out_again:
5625 1.1 rjs #ifdef SCTP_DEBUG
5626 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
5627 1.1 rjs printf("Gak got ctrl error %d\n", error);
5628 1.1 rjs }
5629 1.1 rjs #endif
5630 1.1 rjs /* error, could not output */
5631 1.1 rjs if (hbflag) {
5632 1.1 rjs #ifdef SCTP_DEBUG
5633 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5634 1.1 rjs printf("Update HB anyway\n");
5635 1.1 rjs }
5636 1.1 rjs #endif
5637 1.1 rjs if (*now_filled == 0) {
5638 1.1 rjs SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5639 1.1 rjs *now_filled = 1;
5640 1.1 rjs *now = net->last_sent_time;
5641 1.1 rjs } else {
5642 1.1 rjs net->last_sent_time = *now;
5643 1.1 rjs }
5644 1.1 rjs hbflag = 0;
5645 1.1 rjs }
5646 1.1 rjs if (error == EHOSTUNREACH) {
5647 1.1 rjs /*
5648 1.1 rjs * Destination went
5649 1.1 rjs * unreachable during
5650 1.1 rjs * this send
5651 1.1 rjs */
5652 1.1 rjs #ifdef SCTP_DEBUG
5653 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5654 1.1 rjs printf("Moving data to an alterante\n");
5655 1.1 rjs }
5656 1.1 rjs #endif
5657 1.1 rjs sctp_move_to_an_alt(stcb, asoc, net);
5658 1.1 rjs }
5659 1.1 rjs sctp_clean_up_ctl (asoc);
5660 1.1 rjs return (error);
5661 1.1 rjs } else
5662 1.1 rjs asoc->ifp_had_enobuf = 0;
5663 1.1 rjs /* Only HB or ASCONF advances time */
5664 1.1 rjs if (hbflag) {
5665 1.1 rjs if (*now_filled == 0) {
5666 1.1 rjs SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5667 1.1 rjs *now_filled = 1;
5668 1.1 rjs *now = net->last_sent_time;
5669 1.1 rjs } else {
5670 1.1 rjs net->last_sent_time = *now;
5671 1.1 rjs }
5672 1.1 rjs hbflag = 0;
5673 1.1 rjs }
5674 1.1 rjs /*
5675 1.1 rjs * increase the number we sent, if a
5676 1.1 rjs * cookie is sent we don't tell them
5677 1.1 rjs * any was sent out.
5678 1.1 rjs */
5679 1.1 rjs if (!no_out_cnt)
5680 1.1 rjs *num_out += ctl_cnt;
5681 1.1 rjs /* recalc a clean slate and setup */
5682 1.1 rjs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5683 1.1 rjs mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5684 1.1 rjs } else {
5685 1.1 rjs mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5686 1.1 rjs }
5687 1.1 rjs no_fragmentflg = 1;
5688 1.1 rjs }
5689 1.1 rjs }
5690 1.1 rjs }
5691 1.1 rjs /*********************/
5692 1.1 rjs /* Data transmission */
5693 1.1 rjs /*********************/
5694 1.1 rjs /* now lets add any data within the MTU constraints */
5695 1.1 rjs if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
5696 1.1 rjs omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5697 1.1 rjs } else {
5698 1.1 rjs omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5699 1.1 rjs }
5700 1.1 rjs
5701 1.1 rjs #ifdef SCTP_DEBUG
5702 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5703 1.1 rjs printf("Now to data transmission\n");
5704 1.1 rjs }
5705 1.1 rjs #endif
5706 1.1 rjs
5707 1.1 rjs if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5708 1.1 rjs (cookie)) {
5709 1.1 rjs for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5710 1.1 rjs if (no_data_chunks) {
5711 1.1 rjs /* let only control go out */
5712 1.1 rjs #ifdef SCTP_DEBUG
5713 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5714 1.1 rjs printf("Either nothing to send or we are full\n");
5715 1.1 rjs }
5716 1.1 rjs #endif
5717 1.1 rjs break;
5718 1.1 rjs }
5719 1.1 rjs if (net->flight_size >= net->cwnd) {
5720 1.1 rjs /* skip this net, no room for data */
5721 1.1 rjs #ifdef SCTP_DEBUG
5722 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5723 1.1 rjs printf("fs:%d > cwnd:%d\n",
5724 1.1 rjs net->flight_size, net->cwnd);
5725 1.1 rjs }
5726 1.1 rjs #endif
5727 1.1 rjs sctp_pegs[SCTP_CWND_BLOCKED]++;
5728 1.1 rjs *reason_code = 2;
5729 1.1 rjs break;
5730 1.1 rjs }
5731 1.1 rjs nchk = TAILQ_NEXT(chk, sctp_next);
5732 1.1 rjs if (chk->whoTo != net) {
5733 1.1 rjs /* No, not sent to this net */
5734 1.1 rjs #ifdef SCTP_DEBUG
5735 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5736 1.1 rjs printf("chk->whoTo:%p not %p\n",
5737 1.1 rjs chk->whoTo, net);
5738 1.1 rjs
5739 1.1 rjs }
5740 1.1 rjs #endif
5741 1.1 rjs continue;
5742 1.1 rjs }
5743 1.1 rjs #ifdef SCTP_DEBUG
5744 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5745 1.1 rjs printf("Can we pick up a chunk?\n");
5746 1.1 rjs }
5747 1.1 rjs #endif
5748 1.1 rjs if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5749 1.1 rjs /* strange, we have a chunk that is to bit
5750 1.1 rjs * for its destination and yet no fragment ok flag.
5751 1.1 rjs * Something went wrong when the PMTU changed...we did
5752 1.1 rjs * not mark this chunk for some reason?? I will
5753 1.1 rjs * fix it here by letting IP fragment it for now and
5754 1.1 rjs * printing a warning. This really should not happen ...
5755 1.1 rjs */
5756 1.1 rjs /*#ifdef SCTP_DEBUG*/
5757 1.1 rjs printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5758 1.1 rjs chk->send_size, mtu);
5759 1.1 rjs /*#endif*/
5760 1.1 rjs chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5761 1.1 rjs }
5762 1.1 rjs
5763 1.1 rjs if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5764 1.1 rjs ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5765 1.1 rjs /* ok we will add this one */
5766 1.1 rjs #ifdef SCTP_DEBUG
5767 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5768 1.1 rjs printf("Picking up the chunk\n");
5769 1.1 rjs }
5770 1.1 rjs #endif
5771 1.1 rjs outchain = sctp_copy_mbufchain(chk->data, outchain);
5772 1.1 rjs if (outchain == NULL) {
5773 1.1 rjs #ifdef SCTP_DEBUG
5774 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5775 1.1 rjs printf("Gakk no memory\n");
5776 1.1 rjs }
5777 1.1 rjs #endif
5778 1.1 rjs if (!callout_pending(&net->rxt_timer.timer)) {
5779 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5780 1.1 rjs }
5781 1.1 rjs return (ENOMEM);
5782 1.1 rjs }
5783 1.20 msaitoh /* update our MTU size */
5784 1.1 rjs /* Do clear IP_DF ? */
5785 1.1 rjs if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5786 1.1 rjs no_fragmentflg = 0;
5787 1.1 rjs }
5788 1.1 rjs mtu -= chk->send_size;
5789 1.1 rjs r_mtu -= chk->send_size;
5790 1.1 rjs data_list[bundle_at++] = chk;
5791 1.1 rjs if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
5792 1.1 rjs mtu = 0;
5793 1.1 rjs break;
5794 1.1 rjs }
5795 1.1 rjs if (mtu <= 0) {
5796 1.1 rjs mtu = 0;
5797 1.1 rjs break;
5798 1.1 rjs }
5799 1.1 rjs if ((r_mtu <= 0) || one_chunk) {
5800 1.1 rjs r_mtu = 0;
5801 1.1 rjs break;
5802 1.1 rjs }
5803 1.1 rjs } else {
5804 1.1 rjs /*
5805 1.1 rjs * Must be sent in order of the TSN's
5806 1.1 rjs * (on a network)
5807 1.1 rjs */
5808 1.1 rjs #ifdef SCTP_DEBUG
5809 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5810 1.1 rjs printf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
5811 1.1 rjs chk->send_size, mtu, r_mtu);
5812 1.1 rjs }
5813 1.1 rjs #endif
5814 1.1 rjs
5815 1.1 rjs break;
5816 1.1 rjs }
5817 1.1 rjs }/* for () */
5818 1.1 rjs } /* if asoc.state OPEN */
5819 1.1 rjs /* Is there something to send for this destination? */
5820 1.1 rjs #ifdef SCTP_DEBUG
5821 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5822 1.1 rjs printf("ok now is chain assembled? %p\n",
5823 1.1 rjs outchain);
5824 1.1 rjs }
5825 1.1 rjs #endif
5826 1.1 rjs
5827 1.1 rjs if (outchain) {
5828 1.1 rjs /* We may need to start a control timer or two */
5829 1.1 rjs if (asconf) {
5830 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5831 1.1 rjs asconf = 0;
5832 1.1 rjs }
5833 1.1 rjs if (cookie) {
5834 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5835 1.1 rjs cookie = 0;
5836 1.1 rjs }
5837 1.1 rjs /* must start a send timer if data is being sent */
5838 1.1 rjs if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
5839 1.1 rjs /* no timer running on this destination
5840 1.1 rjs * restart it.
5841 1.1 rjs */
5842 1.1 rjs #ifdef SCTP_DEBUG
5843 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5844 1.1 rjs printf("ok lets start a send timer .. we will transmit %p\n",
5845 1.1 rjs outchain);
5846 1.1 rjs }
5847 1.1 rjs #endif
5848 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5849 1.1 rjs }
5850 1.1 rjs /* Now send it, if there is anything to send :> */
5851 1.1 rjs if ((outchain->m_flags & M_PKTHDR) == 0) {
5852 1.1 rjs struct mbuf *t;
5853 1.1 rjs
5854 1.1 rjs MGETHDR(t, M_DONTWAIT, MT_HEADER);
5855 1.1 rjs if (t == NULL) {
5856 1.1 rjs sctp_m_freem(outchain);
5857 1.1 rjs return (ENOMEM);
5858 1.1 rjs }
5859 1.1 rjs t->m_next = outchain;
5860 1.1 rjs t->m_pkthdr.len = 0;
5861 1.5 ozaki m_reset_rcvif(t);
5862 1.1 rjs t->m_len = 0;
5863 1.1 rjs
5864 1.1 rjs outchain = t;
5865 1.1 rjs while (t) {
5866 1.1 rjs outchain->m_pkthdr.len += t->m_len;
5867 1.1 rjs t = t->m_next;
5868 1.1 rjs }
5869 1.1 rjs }
5870 1.1 rjs if (outchain->m_len == 0) {
5871 1.1 rjs /* Special case for when you get a 0 len
5872 1.1 rjs * mbuf at the head due to the lack
5873 1.1 rjs * of a MHDR at the beginning.
5874 1.1 rjs */
5875 1.18 maxv m_align(outchain, sizeof(struct sctphdr));
5876 1.1 rjs outchain->m_len = sizeof(struct sctphdr);
5877 1.1 rjs } else {
5878 1.1 rjs M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5879 1.1 rjs if (outchain == NULL) {
5880 1.1 rjs /* out of mbufs */
5881 1.1 rjs error = ENOBUFS;
5882 1.1 rjs goto errored_send;
5883 1.1 rjs }
5884 1.1 rjs }
5885 1.1 rjs shdr = mtod(outchain, struct sctphdr *);
5886 1.1 rjs shdr->src_port = inp->sctp_lport;
5887 1.1 rjs shdr->dest_port = stcb->rport;
5888 1.1 rjs shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5889 1.1 rjs shdr->checksum = 0;
5890 1.1 rjs if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5891 1.1 rjs rtcache_getdst(&net->ro),
5892 1.1 rjs outchain,
5893 1.1 rjs no_fragmentflg, bundle_at, data_list[0], asconf))) {
5894 1.1 rjs /* error, we could not output */
5895 1.1 rjs if (error == ENOBUFS) {
5896 1.1 rjs asoc->ifp_had_enobuf = 1;
5897 1.1 rjs }
5898 1.1 rjs sctp_pegs[SCTP_DATA_OUT_ERR]++;
5899 1.1 rjs if (from_where == 0) {
5900 1.1 rjs sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5901 1.1 rjs }
5902 1.1 rjs
5903 1.1 rjs errored_send:
5904 1.1 rjs #ifdef SCTP_DEBUG
5905 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5906 1.1 rjs printf("Gak send error %d\n", error);
5907 1.1 rjs }
5908 1.1 rjs #endif
5909 1.1 rjs if (hbflag) {
5910 1.1 rjs #ifdef SCTP_DEBUG
5911 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5912 1.1 rjs printf("Update HB time anyway\n");
5913 1.1 rjs }
5914 1.1 rjs #endif
5915 1.1 rjs if (*now_filled == 0) {
5916 1.1 rjs SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5917 1.1 rjs *now_filled = 1;
5918 1.1 rjs *now = net->last_sent_time;
5919 1.1 rjs } else {
5920 1.1 rjs net->last_sent_time = *now;
5921 1.1 rjs }
5922 1.1 rjs hbflag = 0;
5923 1.1 rjs }
5924 1.1 rjs if (error == EHOSTUNREACH) {
5925 1.1 rjs /*
5926 1.1 rjs * Destination went unreachable during
5927 1.1 rjs * this send
5928 1.1 rjs */
5929 1.1 rjs #ifdef SCTP_DEBUG
5930 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5931 1.1 rjs printf("Calling the movement routine\n");
5932 1.1 rjs }
5933 1.1 rjs #endif
5934 1.1 rjs sctp_move_to_an_alt(stcb, asoc, net);
5935 1.1 rjs }
5936 1.1 rjs sctp_clean_up_ctl (asoc);
5937 1.1 rjs return (error);
5938 1.1 rjs } else {
5939 1.1 rjs asoc->ifp_had_enobuf = 0;
5940 1.1 rjs }
5941 1.1 rjs if (bundle_at || hbflag) {
5942 1.1 rjs /* For data/asconf and hb set time */
5943 1.1 rjs if (*now_filled == 0) {
5944 1.1 rjs SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5945 1.1 rjs *now_filled = 1;
5946 1.1 rjs *now = net->last_sent_time;
5947 1.1 rjs } else {
5948 1.1 rjs net->last_sent_time = *now;
5949 1.1 rjs }
5950 1.1 rjs }
5951 1.1 rjs
5952 1.1 rjs if (!no_out_cnt) {
5953 1.1 rjs *num_out += (ctl_cnt + bundle_at);
5954 1.1 rjs }
5955 1.1 rjs if (bundle_at) {
5956 1.1 rjs if (!net->rto_pending) {
5957 1.1 rjs /* setup for a RTO measurement */
5958 1.1 rjs net->rto_pending = 1;
5959 1.1 rjs data_list[0]->do_rtt = 1;
5960 1.1 rjs } else {
5961 1.1 rjs data_list[0]->do_rtt = 0;
5962 1.1 rjs }
5963 1.1 rjs sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
5964 1.1 rjs sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
5965 1.1 rjs }
5966 1.1 rjs if (one_chunk) {
5967 1.1 rjs break;
5968 1.1 rjs }
5969 1.1 rjs }
5970 1.1 rjs }
5971 1.1 rjs /* At the end there should be no NON timed
5972 1.1 rjs * chunks hanging on this queue.
5973 1.1 rjs */
5974 1.1 rjs if ((*num_out == 0) && (*reason_code == 0)) {
5975 1.1 rjs *reason_code = 3;
5976 1.1 rjs }
5977 1.1 rjs sctp_clean_up_ctl (asoc);
5978 1.1 rjs return (0);
5979 1.1 rjs }
5980 1.1 rjs
5981 1.1 rjs void
5982 1.1 rjs sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
5983 1.1 rjs {
5984 1.1 rjs /* Prepend a OPERATIONAL_ERROR chunk header
5985 1.1 rjs * and put on the end of the control chunk queue.
5986 1.1 rjs */
5987 1.1 rjs /* Sender had better have gotten a MGETHDR or else
5988 1.1 rjs * the control chunk will be forever skipped
5989 1.1 rjs */
5990 1.1 rjs struct sctp_chunkhdr *hdr;
5991 1.1 rjs struct sctp_tmit_chunk *chk;
5992 1.1 rjs struct mbuf *mat;
5993 1.1 rjs
5994 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
5995 1.1 rjs if (chk == NULL) {
5996 1.1 rjs /* no memory */
5997 1.1 rjs sctp_m_freem(op_err);
5998 1.1 rjs return;
5999 1.1 rjs }
6000 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6001 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6002 1.1 rjs M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
6003 1.1 rjs if (op_err == NULL) {
6004 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
6005 1.1 rjs sctppcbinfo.ipi_count_chunk--;
6006 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
6007 1.1 rjs panic("Chunk count is negative");
6008 1.1 rjs }
6009 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6010 1.1 rjs return;
6011 1.1 rjs }
6012 1.1 rjs chk->send_size = 0;
6013 1.1 rjs mat = op_err;
6014 1.1 rjs while (mat != NULL) {
6015 1.1 rjs chk->send_size += mat->m_len;
6016 1.1 rjs mat = mat->m_next;
6017 1.1 rjs }
6018 1.1 rjs chk->rec.chunk_id = SCTP_OPERATION_ERROR;
6019 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6020 1.1 rjs chk->snd_count = 0;
6021 1.1 rjs chk->flags = 0;
6022 1.1 rjs chk->asoc = &stcb->asoc;
6023 1.1 rjs chk->data = op_err;
6024 1.1 rjs chk->whoTo = chk->asoc->primary_destination;
6025 1.1 rjs chk->whoTo->ref_count++;
6026 1.1 rjs hdr = mtod(op_err, struct sctp_chunkhdr *);
6027 1.1 rjs hdr->chunk_type = SCTP_OPERATION_ERROR;
6028 1.1 rjs hdr->chunk_flags = 0;
6029 1.1 rjs hdr->chunk_length = htons(chk->send_size);
6030 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6031 1.1 rjs chk,
6032 1.1 rjs sctp_next);
6033 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6034 1.1 rjs }
6035 1.1 rjs
6036 1.1 rjs int
6037 1.1 rjs sctp_send_cookie_echo(struct mbuf *m,
6038 1.1 rjs int offset,
6039 1.1 rjs struct sctp_tcb *stcb,
6040 1.1 rjs struct sctp_nets *net)
6041 1.1 rjs {
6042 1.1 rjs /*
6043 1.1 rjs * pull out the cookie and put it at the front of the control
6044 1.1 rjs * chunk queue.
6045 1.1 rjs */
6046 1.1 rjs int at;
6047 1.1 rjs struct mbuf *cookie, *mat;
6048 1.1 rjs struct sctp_paramhdr parm, *phdr;
6049 1.1 rjs struct sctp_chunkhdr *hdr;
6050 1.1 rjs struct sctp_tmit_chunk *chk;
6051 1.1 rjs uint16_t ptype, plen;
6052 1.1 rjs /* First find the cookie in the param area */
6053 1.1 rjs cookie = NULL;
6054 1.1 rjs at = offset + sizeof(struct sctp_init_chunk);
6055 1.1 rjs
6056 1.1 rjs do {
6057 1.1 rjs phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6058 1.1 rjs if (phdr == NULL) {
6059 1.1 rjs return (-3);
6060 1.1 rjs }
6061 1.1 rjs ptype = ntohs(phdr->param_type);
6062 1.1 rjs plen = ntohs(phdr->param_length);
6063 1.1 rjs if (ptype == SCTP_STATE_COOKIE) {
6064 1.1 rjs int pad;
6065 1.1 rjs /* found the cookie */
6066 1.1 rjs if ((pad = (plen % 4))) {
6067 1.1 rjs plen += 4 - pad;
6068 1.1 rjs }
6069 1.1 rjs cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
6070 1.1 rjs if (cookie == NULL) {
6071 1.1 rjs /* No memory */
6072 1.1 rjs return (-2);
6073 1.1 rjs }
6074 1.1 rjs break;
6075 1.1 rjs }
6076 1.1 rjs at += SCTP_SIZE32(plen);
6077 1.1 rjs } while (phdr);
6078 1.1 rjs if (cookie == NULL) {
6079 1.1 rjs /* Did not find the cookie */
6080 1.1 rjs return (-3);
6081 1.1 rjs }
6082 1.1 rjs /* ok, we got the cookie lets change it into a cookie echo chunk */
6083 1.1 rjs
6084 1.1 rjs /* first the change from param to cookie */
6085 1.1 rjs hdr = mtod(cookie, struct sctp_chunkhdr *);
6086 1.1 rjs hdr->chunk_type = SCTP_COOKIE_ECHO;
6087 1.1 rjs hdr->chunk_flags = 0;
6088 1.1 rjs /* now we MUST have a PKTHDR on it */
6089 1.1 rjs if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6090 1.1 rjs /* we hope this happens rarely */
6091 1.1 rjs MGETHDR(mat, M_DONTWAIT, MT_HEADER);
6092 1.1 rjs if (mat == NULL) {
6093 1.1 rjs sctp_m_freem(cookie);
6094 1.1 rjs return (-4);
6095 1.1 rjs }
6096 1.1 rjs mat->m_len = 0;
6097 1.5 ozaki m_reset_rcvif(mat);
6098 1.1 rjs mat->m_next = cookie;
6099 1.1 rjs cookie = mat;
6100 1.1 rjs }
6101 1.1 rjs cookie->m_pkthdr.len = plen;
6102 1.1 rjs /* get the chunk stuff now and place it in the FRONT of the queue */
6103 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6104 1.1 rjs if (chk == NULL) {
6105 1.1 rjs /* no memory */
6106 1.1 rjs sctp_m_freem(cookie);
6107 1.1 rjs return (-5);
6108 1.1 rjs }
6109 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6110 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6111 1.1 rjs chk->send_size = cookie->m_pkthdr.len;
6112 1.1 rjs chk->rec.chunk_id = SCTP_COOKIE_ECHO;
6113 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6114 1.1 rjs chk->snd_count = 0;
6115 1.1 rjs chk->flags = 0;
6116 1.1 rjs chk->asoc = &stcb->asoc;
6117 1.1 rjs chk->data = cookie;
6118 1.1 rjs chk->whoTo = chk->asoc->primary_destination;
6119 1.1 rjs chk->whoTo->ref_count++;
6120 1.1 rjs TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6121 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6122 1.1 rjs return (0);
6123 1.1 rjs }
6124 1.1 rjs
6125 1.1 rjs void
6126 1.1 rjs sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6127 1.1 rjs struct mbuf *m,
6128 1.1 rjs int offset,
6129 1.1 rjs int chk_length,
6130 1.1 rjs struct sctp_nets *net)
6131 1.1 rjs {
6132 1.1 rjs /* take a HB request and make it into a
6133 1.1 rjs * HB ack and send it.
6134 1.1 rjs */
6135 1.1 rjs struct mbuf *outchain;
6136 1.1 rjs struct sctp_chunkhdr *chdr;
6137 1.1 rjs struct sctp_tmit_chunk *chk;
6138 1.1 rjs
6139 1.1 rjs
6140 1.1 rjs if (net == NULL)
6141 1.1 rjs /* must have a net pointer */
6142 1.1 rjs return;
6143 1.1 rjs
6144 1.1 rjs outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
6145 1.1 rjs if (outchain == NULL) {
6146 1.1 rjs /* gak out of memory */
6147 1.1 rjs return;
6148 1.1 rjs }
6149 1.1 rjs chdr = mtod(outchain, struct sctp_chunkhdr *);
6150 1.1 rjs chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6151 1.1 rjs chdr->chunk_flags = 0;
6152 1.1 rjs if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6153 1.1 rjs /* should not happen but we are cautious. */
6154 1.1 rjs struct mbuf *tmp;
6155 1.1 rjs MGETHDR(tmp, M_DONTWAIT, MT_HEADER);
6156 1.1 rjs if (tmp == NULL) {
6157 1.1 rjs return;
6158 1.1 rjs }
6159 1.1 rjs tmp->m_len = 0;
6160 1.5 ozaki m_reset_rcvif(tmp);
6161 1.1 rjs tmp->m_next = outchain;
6162 1.1 rjs outchain = tmp;
6163 1.1 rjs }
6164 1.1 rjs outchain->m_pkthdr.len = chk_length;
6165 1.1 rjs if (chk_length % 4) {
6166 1.1 rjs /* need pad */
6167 1.1 rjs u_int32_t cpthis=0;
6168 1.1 rjs int padlen;
6169 1.1 rjs padlen = 4 - (outchain->m_pkthdr.len % 4);
6170 1.1 rjs m_copyback(outchain, outchain->m_pkthdr.len, padlen, (void *)&cpthis);
6171 1.1 rjs }
6172 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6173 1.1 rjs if (chk == NULL) {
6174 1.1 rjs /* no memory */
6175 1.1 rjs sctp_m_freem(outchain);
6176 1.1 rjs return ;
6177 1.1 rjs }
6178 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6179 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6180 1.1 rjs
6181 1.1 rjs chk->send_size = chk_length;
6182 1.1 rjs chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
6183 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6184 1.1 rjs chk->snd_count = 0;
6185 1.1 rjs chk->flags = 0;
6186 1.1 rjs chk->asoc = &stcb->asoc;
6187 1.1 rjs chk->data = outchain;
6188 1.1 rjs chk->whoTo = net;
6189 1.1 rjs chk->whoTo->ref_count++;
6190 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6191 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6192 1.1 rjs }
6193 1.1 rjs
6194 1.1 rjs int
6195 1.1 rjs sctp_send_cookie_ack(struct sctp_tcb *stcb) {
6196 1.1 rjs /* formulate and queue a cookie-ack back to sender */
6197 1.1 rjs struct mbuf *cookie_ack;
6198 1.1 rjs struct sctp_chunkhdr *hdr;
6199 1.1 rjs struct sctp_tmit_chunk *chk;
6200 1.1 rjs
6201 1.1 rjs cookie_ack = NULL;
6202 1.1 rjs MGETHDR(cookie_ack, M_DONTWAIT, MT_HEADER);
6203 1.1 rjs if (cookie_ack == NULL) {
6204 1.1 rjs /* no mbuf's */
6205 1.1 rjs return (-1);
6206 1.1 rjs }
6207 1.1 rjs cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6208 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6209 1.1 rjs if (chk == NULL) {
6210 1.1 rjs /* no memory */
6211 1.1 rjs sctp_m_freem(cookie_ack);
6212 1.1 rjs return (-1);
6213 1.1 rjs }
6214 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6215 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6216 1.1 rjs
6217 1.1 rjs chk->send_size = sizeof(struct sctp_chunkhdr);
6218 1.1 rjs chk->rec.chunk_id = SCTP_COOKIE_ACK;
6219 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6220 1.1 rjs chk->snd_count = 0;
6221 1.1 rjs chk->flags = 0;
6222 1.1 rjs chk->asoc = &stcb->asoc;
6223 1.1 rjs chk->data = cookie_ack;
6224 1.1 rjs if (chk->asoc->last_control_chunk_from != NULL) {
6225 1.1 rjs chk->whoTo = chk->asoc->last_control_chunk_from;
6226 1.1 rjs } else {
6227 1.1 rjs chk->whoTo = chk->asoc->primary_destination;
6228 1.1 rjs }
6229 1.1 rjs chk->whoTo->ref_count++;
6230 1.1 rjs hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6231 1.1 rjs hdr->chunk_type = SCTP_COOKIE_ACK;
6232 1.1 rjs hdr->chunk_flags = 0;
6233 1.1 rjs hdr->chunk_length = htons(chk->send_size);
6234 1.1 rjs cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6235 1.5 ozaki m_reset_rcvif(cookie_ack);
6236 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6237 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6238 1.1 rjs return (0);
6239 1.1 rjs }
6240 1.1 rjs
6241 1.1 rjs
6242 1.1 rjs int
6243 1.1 rjs sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6244 1.1 rjs {
6245 1.1 rjs /* formulate and queue a SHUTDOWN-ACK back to the sender */
6246 1.1 rjs struct mbuf *m_shutdown_ack;
6247 1.1 rjs struct sctp_shutdown_ack_chunk *ack_cp;
6248 1.1 rjs struct sctp_tmit_chunk *chk;
6249 1.1 rjs
6250 1.1 rjs m_shutdown_ack = NULL;
6251 1.1 rjs MGETHDR(m_shutdown_ack, M_DONTWAIT, MT_HEADER);
6252 1.1 rjs if (m_shutdown_ack == NULL) {
6253 1.1 rjs /* no mbuf's */
6254 1.1 rjs return (-1);
6255 1.1 rjs }
6256 1.1 rjs m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6257 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6258 1.1 rjs if (chk == NULL) {
6259 1.1 rjs /* no memory */
6260 1.1 rjs sctp_m_freem(m_shutdown_ack);
6261 1.1 rjs return (-1);
6262 1.1 rjs }
6263 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6264 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6265 1.1 rjs
6266 1.1 rjs chk->send_size = sizeof(struct sctp_chunkhdr);
6267 1.1 rjs chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
6268 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6269 1.1 rjs chk->snd_count = 0;
6270 1.1 rjs chk->flags = 0;
6271 1.1 rjs chk->asoc = &stcb->asoc;
6272 1.1 rjs chk->data = m_shutdown_ack;
6273 1.1 rjs chk->whoTo = net;
6274 1.1 rjs net->ref_count++;
6275 1.1 rjs
6276 1.1 rjs ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6277 1.1 rjs ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6278 1.1 rjs ack_cp->ch.chunk_flags = 0;
6279 1.1 rjs ack_cp->ch.chunk_length = htons(chk->send_size);
6280 1.1 rjs m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6281 1.5 ozaki m_reset_rcvif(m_shutdown_ack);
6282 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6283 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6284 1.1 rjs return (0);
6285 1.1 rjs }
6286 1.1 rjs
6287 1.1 rjs int
6288 1.1 rjs sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6289 1.1 rjs {
6290 1.1 rjs /* formulate and queue a SHUTDOWN to the sender */
6291 1.1 rjs struct mbuf *m_shutdown;
6292 1.1 rjs struct sctp_shutdown_chunk *shutdown_cp;
6293 1.1 rjs struct sctp_tmit_chunk *chk;
6294 1.1 rjs
6295 1.1 rjs m_shutdown = NULL;
6296 1.1 rjs MGETHDR(m_shutdown, M_DONTWAIT, MT_HEADER);
6297 1.1 rjs if (m_shutdown == NULL) {
6298 1.1 rjs /* no mbuf's */
6299 1.1 rjs return (-1);
6300 1.1 rjs }
6301 1.1 rjs m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6302 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6303 1.1 rjs if (chk == NULL) {
6304 1.1 rjs /* no memory */
6305 1.1 rjs sctp_m_freem(m_shutdown);
6306 1.1 rjs return (-1);
6307 1.1 rjs }
6308 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6309 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6310 1.1 rjs
6311 1.1 rjs chk->send_size = sizeof(struct sctp_shutdown_chunk);
6312 1.1 rjs chk->rec.chunk_id = SCTP_SHUTDOWN;
6313 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6314 1.1 rjs chk->snd_count = 0;
6315 1.1 rjs chk->flags = 0;
6316 1.1 rjs chk->asoc = &stcb->asoc;
6317 1.1 rjs chk->data = m_shutdown;
6318 1.1 rjs chk->whoTo = net;
6319 1.1 rjs net->ref_count++;
6320 1.1 rjs
6321 1.1 rjs shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6322 1.1 rjs shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6323 1.1 rjs shutdown_cp->ch.chunk_flags = 0;
6324 1.1 rjs shutdown_cp->ch.chunk_length = htons(chk->send_size);
6325 1.1 rjs shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6326 1.1 rjs m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6327 1.5 ozaki m_reset_rcvif(m_shutdown);
6328 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6329 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6330 1.1 rjs
6331 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6332 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6333 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
6334 1.1 rjs soisdisconnecting(stcb->sctp_ep->sctp_socket);
6335 1.1 rjs }
6336 1.1 rjs return (0);
6337 1.1 rjs }
6338 1.1 rjs
6339 1.1 rjs int
6340 1.1 rjs sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6341 1.1 rjs {
6342 1.1 rjs /*
6343 1.1 rjs * formulate and queue an ASCONF to the peer
6344 1.1 rjs * ASCONF parameters should be queued on the assoc queue
6345 1.1 rjs */
6346 1.1 rjs struct sctp_tmit_chunk *chk;
6347 1.1 rjs struct mbuf *m_asconf;
6348 1.1 rjs
6349 1.1 rjs /* compose an ASCONF chunk, maximum length is PMTU */
6350 1.1 rjs m_asconf = sctp_compose_asconf(stcb);
6351 1.1 rjs if (m_asconf == NULL) {
6352 1.1 rjs return (-1);
6353 1.1 rjs }
6354 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6355 1.1 rjs if (chk == NULL) {
6356 1.1 rjs /* no memory */
6357 1.1 rjs sctp_m_freem(m_asconf);
6358 1.1 rjs return (-1);
6359 1.1 rjs }
6360 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6361 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6362 1.1 rjs
6363 1.1 rjs chk->data = m_asconf;
6364 1.1 rjs chk->send_size = m_asconf->m_pkthdr.len;
6365 1.1 rjs chk->rec.chunk_id = SCTP_ASCONF;
6366 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6367 1.1 rjs chk->snd_count = 0;
6368 1.1 rjs chk->flags = 0;
6369 1.1 rjs chk->asoc = &stcb->asoc;
6370 1.1 rjs chk->whoTo = chk->asoc->primary_destination;
6371 1.1 rjs chk->whoTo->ref_count++;
6372 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6373 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6374 1.1 rjs return (0);
6375 1.1 rjs }
6376 1.1 rjs
6377 1.1 rjs int
6378 1.1 rjs sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6379 1.1 rjs {
6380 1.1 rjs /*
6381 1.1 rjs * formulate and queue a asconf-ack back to sender
6382 1.1 rjs * the asconf-ack must be stored in the tcb
6383 1.1 rjs */
6384 1.1 rjs struct sctp_tmit_chunk *chk;
6385 1.1 rjs struct mbuf *m_ack;
6386 1.1 rjs
6387 1.1 rjs /* is there a asconf-ack mbuf chain to send? */
6388 1.1 rjs if (stcb->asoc.last_asconf_ack_sent == NULL) {
6389 1.1 rjs return (-1);
6390 1.1 rjs }
6391 1.1 rjs
6392 1.1 rjs /* copy the asconf_ack */
6393 1.1 rjs #if defined(__FreeBSD__) || defined(__NetBSD__)
6394 1.1 rjs /* Supposedly the m_copypacket is a optimzation,
6395 1.1 rjs * use it if we can.
6396 1.1 rjs */
6397 1.1 rjs if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6398 1.1 rjs m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
6399 1.1 rjs sctp_pegs[SCTP_CACHED_SRC]++;
6400 1.1 rjs } else
6401 1.15 maxv m_ack = m_copym(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL, M_DONTWAIT);
6402 1.1 rjs #else
6403 1.1 rjs m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6404 1.1 rjs #endif
6405 1.1 rjs if (m_ack == NULL) {
6406 1.1 rjs /* couldn't copy it */
6407 1.1 rjs
6408 1.1 rjs return (-1);
6409 1.1 rjs }
6410 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6411 1.1 rjs if (chk == NULL) {
6412 1.1 rjs /* no memory */
6413 1.1 rjs if (m_ack)
6414 1.1 rjs sctp_m_freem(m_ack);
6415 1.1 rjs return (-1);
6416 1.1 rjs }
6417 1.1 rjs sctppcbinfo.ipi_count_chunk++;
6418 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
6419 1.1 rjs
6420 1.1 rjs /* figure out where it goes to */
6421 1.1 rjs if (retrans) {
6422 1.1 rjs /* we're doing a retransmission */
6423 1.1 rjs if (stcb->asoc.used_alt_asconfack > 2) {
6424 1.1 rjs /* tried alternate nets already, go back */
6425 1.1 rjs chk->whoTo = NULL;
6426 1.1 rjs } else {
6427 1.1 rjs /* need to try and alternate net */
6428 1.1 rjs chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
6429 1.1 rjs stcb->asoc.used_alt_asconfack++;
6430 1.1 rjs }
6431 1.1 rjs if (chk->whoTo == NULL) {
6432 1.1 rjs /* no alternate */
6433 1.1 rjs if (stcb->asoc.last_control_chunk_from == NULL)
6434 1.1 rjs chk->whoTo = stcb->asoc.primary_destination;
6435 1.1 rjs else
6436 1.1 rjs chk->whoTo = stcb->asoc.last_control_chunk_from;
6437 1.1 rjs stcb->asoc.used_alt_asconfack = 0;
6438 1.1 rjs }
6439 1.1 rjs } else {
6440 1.1 rjs /* normal case */
6441 1.1 rjs if (stcb->asoc.last_control_chunk_from == NULL)
6442 1.1 rjs chk->whoTo = stcb->asoc.primary_destination;
6443 1.1 rjs else
6444 1.1 rjs chk->whoTo = stcb->asoc.last_control_chunk_from;
6445 1.1 rjs stcb->asoc.used_alt_asconfack = 0;
6446 1.1 rjs }
6447 1.1 rjs chk->data = m_ack;
6448 1.1 rjs chk->send_size = m_ack->m_pkthdr.len;
6449 1.1 rjs chk->rec.chunk_id = SCTP_ASCONF_ACK;
6450 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
6451 1.1 rjs chk->snd_count = 0;
6452 1.1 rjs chk->flags = 0;
6453 1.1 rjs chk->asoc = &stcb->asoc;
6454 1.1 rjs chk->whoTo->ref_count++;
6455 1.1 rjs TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6456 1.1 rjs chk->asoc->ctrl_queue_cnt++;
6457 1.1 rjs return (0);
6458 1.1 rjs }
6459 1.1 rjs
6460 1.1 rjs
6461 1.1 rjs static int
6462 1.1 rjs sctp_chunk_retransmission(struct sctp_inpcb *inp,
6463 1.1 rjs struct sctp_tcb *stcb,
6464 1.1 rjs struct sctp_association *asoc,
6465 1.1 rjs int *cnt_out, struct timeval *now, int *now_filled)
6466 1.1 rjs {
6467 1.1 rjs /*
6468 1.1 rjs * send out one MTU of retransmission.
6469 1.1 rjs * If fast_retransmit is happening we ignore the cwnd.
6470 1.1 rjs * Otherwise we obey the cwnd and rwnd.
6471 1.1 rjs * For a Cookie or Asconf in the control chunk queue we retransmit
6472 1.1 rjs * them by themselves.
6473 1.1 rjs *
6474 1.1 rjs * For data chunks we will pick out the lowest TSN's in the
6475 1.1 rjs * sent_queue marked for resend and bundle them all together
6476 1.1 rjs * (up to a MTU of destination). The address to send to should
6477 1.1 rjs * have been selected/changed where the retransmission was
6478 1.1 rjs * marked (i.e. in FR or t3-timeout routines).
6479 1.1 rjs */
6480 1.1 rjs struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6481 1.1 rjs struct sctp_tmit_chunk *chk, *fwd;
6482 1.1 rjs struct mbuf *m;
6483 1.1 rjs struct sctphdr *shdr;
6484 1.1 rjs int asconf;
6485 1.1 rjs struct sctp_nets *net;
6486 1.1 rjs int no_fragmentflg, bundle_at, cnt_thru;
6487 1.1 rjs unsigned int mtu;
6488 1.1 rjs int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6489 1.1 rjs
6490 1.1 rjs tmr_started = ctl_cnt = bundle_at = error = 0;
6491 1.1 rjs no_fragmentflg = 1;
6492 1.1 rjs asconf = 0;
6493 1.1 rjs fwd_tsn = 0;
6494 1.1 rjs *cnt_out = 0;
6495 1.1 rjs fwd = NULL;
6496 1.1 rjs m = NULL;
6497 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6498 1.1 rjs sctp_audit_log(0xC3, 1);
6499 1.1 rjs #endif
6500 1.1 rjs if (TAILQ_EMPTY(&asoc->sent_queue)) {
6501 1.1 rjs #ifdef SCTP_DEBUG
6502 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6503 1.1 rjs printf("SCTP hits empty queue with cnt set to %d?\n",
6504 1.1 rjs asoc->sent_queue_retran_cnt);
6505 1.1 rjs }
6506 1.1 rjs #endif
6507 1.1 rjs asoc->sent_queue_cnt = 0;
6508 1.1 rjs asoc->sent_queue_cnt_removeable = 0;
6509 1.1 rjs }
6510 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6511 1.1 rjs if (chk->sent != SCTP_DATAGRAM_RESEND) {
6512 1.1 rjs /* we only worry about things marked for resend */
6513 1.1 rjs continue;
6514 1.1 rjs }
6515 1.1 rjs if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
6516 1.1 rjs (chk->rec.chunk_id == SCTP_ASCONF) ||
6517 1.1 rjs (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
6518 1.1 rjs (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
6519 1.1 rjs if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
6520 1.1 rjs /* For stream reset we only retran the request
6521 1.1 rjs * not the response.
6522 1.1 rjs */
6523 1.1 rjs struct sctp_stream_reset_req *strreq;
6524 1.1 rjs strreq = mtod(chk->data, struct sctp_stream_reset_req *);
6525 1.1 rjs if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
6526 1.1 rjs continue;
6527 1.1 rjs }
6528 1.1 rjs }
6529 1.1 rjs ctl_cnt++;
6530 1.1 rjs if (chk->rec.chunk_id == SCTP_ASCONF) {
6531 1.1 rjs no_fragmentflg = 1;
6532 1.1 rjs asconf = 1;
6533 1.1 rjs }
6534 1.1 rjs if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
6535 1.1 rjs fwd_tsn = 1;
6536 1.1 rjs fwd = chk;
6537 1.1 rjs }
6538 1.1 rjs m = sctp_copy_mbufchain(chk->data, m);
6539 1.1 rjs break;
6540 1.1 rjs }
6541 1.1 rjs }
6542 1.1 rjs one_chunk = 0;
6543 1.1 rjs cnt_thru = 0;
6544 1.1 rjs /* do we have control chunks to retransmit? */
6545 1.1 rjs if (m != NULL) {
6546 1.1 rjs /* Start a timer no matter if we suceed or fail */
6547 1.1 rjs if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
6548 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6549 1.1 rjs } else if (chk->rec.chunk_id == SCTP_ASCONF)
6550 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6551 1.1 rjs
6552 1.1 rjs if (m->m_len == 0) {
6553 1.1 rjs /* Special case for when you get a 0 len
6554 1.1 rjs * mbuf at the head due to the lack
6555 1.1 rjs * of a MHDR at the beginning.
6556 1.1 rjs */
6557 1.1 rjs m->m_len = sizeof(struct sctphdr);
6558 1.1 rjs } else {
6559 1.1 rjs M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6560 1.1 rjs if (m == NULL) {
6561 1.1 rjs return (ENOBUFS);
6562 1.1 rjs }
6563 1.1 rjs }
6564 1.1 rjs shdr = mtod(m, struct sctphdr *);
6565 1.1 rjs shdr->src_port = inp->sctp_lport;
6566 1.1 rjs shdr->dest_port = stcb->rport;
6567 1.1 rjs shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6568 1.1 rjs shdr->checksum = 0;
6569 1.1 rjs chk->snd_count++; /* update our count */
6570 1.1 rjs
6571 1.1 rjs if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6572 1.1 rjs rtcache_getdst(&chk->whoTo->ro), m,
6573 1.1 rjs no_fragmentflg, 0, NULL, asconf))) {
6574 1.1 rjs sctp_pegs[SCTP_DATA_OUT_ERR]++;
6575 1.1 rjs return (error);
6576 1.1 rjs }
6577 1.1 rjs /*
6578 1.1 rjs *We don't want to mark the net->sent time here since this
6579 1.1 rjs * we use this for HB and retrans cannot measure RTT
6580 1.1 rjs */
6581 1.1 rjs /* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
6582 1.1 rjs *cnt_out += 1;
6583 1.1 rjs chk->sent = SCTP_DATAGRAM_SENT;
6584 1.1 rjs sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6585 1.1 rjs if (fwd_tsn == 0) {
6586 1.1 rjs return (0);
6587 1.1 rjs } else {
6588 1.1 rjs /* Clean up the fwd-tsn list */
6589 1.1 rjs sctp_clean_up_ctl (asoc);
6590 1.1 rjs return (0);
6591 1.1 rjs }
6592 1.1 rjs }
6593 1.1 rjs /* Ok, it is just data retransmission we need to do or
6594 1.1 rjs * that and a fwd-tsn with it all.
6595 1.1 rjs */
6596 1.1 rjs if (TAILQ_EMPTY(&asoc->sent_queue)) {
6597 1.1 rjs return (-1);
6598 1.1 rjs }
6599 1.1 rjs #ifdef SCTP_DEBUG
6600 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6601 1.1 rjs printf("Normal chunk retransmission cnt:%d\n",
6602 1.1 rjs asoc->sent_queue_retran_cnt);
6603 1.1 rjs }
6604 1.1 rjs #endif
6605 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6606 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6607 1.1 rjs /* not yet open, resend the cookie and that is it */
6608 1.1 rjs return (1);
6609 1.1 rjs }
6610 1.1 rjs
6611 1.1 rjs
6612 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6613 1.1 rjs sctp_auditing(20, inp, stcb, NULL);
6614 1.1 rjs #endif
6615 1.1 rjs TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6616 1.1 rjs if (chk->sent != SCTP_DATAGRAM_RESEND) {
6617 1.1 rjs /* No, not sent to this net or not ready for rtx */
6618 1.1 rjs continue;
6619 1.1 rjs
6620 1.1 rjs }
6621 1.1 rjs /* pick up the net */
6622 1.1 rjs net = chk->whoTo;
6623 1.1 rjs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6624 1.1 rjs mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6625 1.1 rjs } else {
6626 1.1 rjs mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
6627 1.1 rjs }
6628 1.1 rjs
6629 1.1 rjs if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6630 1.1 rjs /* No room in peers rwnd */
6631 1.1 rjs uint32_t tsn;
6632 1.1 rjs tsn = asoc->last_acked_seq + 1;
6633 1.1 rjs if (tsn == chk->rec.data.TSN_seq) {
6634 1.1 rjs /* we make a special exception for this case.
6635 1.1 rjs * The peer has no rwnd but is missing the
6636 1.1 rjs * lowest chunk.. which is probably what is
6637 1.1 rjs * holding up the rwnd.
6638 1.1 rjs */
6639 1.1 rjs goto one_chunk_around;
6640 1.1 rjs }
6641 1.1 rjs #ifdef SCTP_DEBUG
6642 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6643 1.1 rjs printf("blocked-peers_rwnd:%d tf:%d\n",
6644 1.1 rjs (int)asoc->peers_rwnd,
6645 1.1 rjs (int)asoc->total_flight);
6646 1.1 rjs }
6647 1.1 rjs #endif
6648 1.1 rjs sctp_pegs[SCTP_RWND_BLOCKED]++;
6649 1.1 rjs return (1);
6650 1.1 rjs }
6651 1.1 rjs one_chunk_around:
6652 1.1 rjs if (asoc->peers_rwnd < mtu) {
6653 1.1 rjs one_chunk = 1;
6654 1.1 rjs }
6655 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6656 1.1 rjs sctp_audit_log(0xC3, 2);
6657 1.1 rjs #endif
6658 1.1 rjs bundle_at = 0;
6659 1.1 rjs m = NULL;
6660 1.1 rjs net->fast_retran_ip = 0;
6661 1.1 rjs if (chk->rec.data.doing_fast_retransmit == 0) {
6662 1.1 rjs /* if no FR in progress skip destination that
6663 1.1 rjs * have flight_size > cwnd.
6664 1.1 rjs */
6665 1.1 rjs if (net->flight_size >= net->cwnd) {
6666 1.1 rjs sctp_pegs[SCTP_CWND_BLOCKED]++;
6667 1.1 rjs continue;
6668 1.1 rjs }
6669 1.1 rjs } else {
6670 1.1 rjs /* Mark the destination net to have FR recovery
6671 1.1 rjs * limits put on it.
6672 1.1 rjs */
6673 1.1 rjs net->fast_retran_ip = 1;
6674 1.1 rjs }
6675 1.1 rjs
6676 1.1 rjs if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6677 1.1 rjs /* ok we will add this one */
6678 1.1 rjs m = sctp_copy_mbufchain(chk->data, m);
6679 1.1 rjs if (m == NULL) {
6680 1.1 rjs return (ENOMEM);
6681 1.1 rjs }
6682 1.20 msaitoh /* update our MTU size */
6683 1.1 rjs /* Do clear IP_DF ? */
6684 1.1 rjs if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6685 1.1 rjs no_fragmentflg = 0;
6686 1.1 rjs }
6687 1.1 rjs mtu -= chk->send_size;
6688 1.1 rjs data_list[bundle_at++] = chk;
6689 1.1 rjs if (one_chunk && (asoc->total_flight <= 0)) {
6690 1.1 rjs sctp_pegs[SCTP_WINDOW_PROBES]++;
6691 1.1 rjs chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6692 1.1 rjs }
6693 1.1 rjs }
6694 1.1 rjs if (one_chunk == 0) {
6695 1.1 rjs /* now are there anymore forward from chk to pick up?*/
6696 1.1 rjs fwd = TAILQ_NEXT(chk, sctp_next);
6697 1.1 rjs while (fwd) {
6698 1.1 rjs if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6699 1.1 rjs /* Nope, not for retran */
6700 1.1 rjs fwd = TAILQ_NEXT(fwd, sctp_next);
6701 1.1 rjs continue;
6702 1.1 rjs }
6703 1.1 rjs if (fwd->whoTo != net) {
6704 1.1 rjs /* Nope, not the net in question */
6705 1.1 rjs fwd = TAILQ_NEXT(fwd, sctp_next);
6706 1.1 rjs continue;
6707 1.1 rjs }
6708 1.1 rjs if (fwd->send_size <= mtu) {
6709 1.1 rjs m = sctp_copy_mbufchain(fwd->data, m);
6710 1.1 rjs if (m == NULL) {
6711 1.1 rjs return (ENOMEM);
6712 1.1 rjs }
6713 1.20 msaitoh /* update our MTU size */
6714 1.1 rjs /* Do clear IP_DF ? */
6715 1.1 rjs if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6716 1.1 rjs no_fragmentflg = 0;
6717 1.1 rjs }
6718 1.1 rjs mtu -= fwd->send_size;
6719 1.1 rjs data_list[bundle_at++] = fwd;
6720 1.1 rjs if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6721 1.1 rjs break;
6722 1.1 rjs }
6723 1.1 rjs fwd = TAILQ_NEXT(fwd, sctp_next);
6724 1.1 rjs } else {
6725 1.1 rjs /* can't fit so we are done */
6726 1.1 rjs break;
6727 1.1 rjs }
6728 1.1 rjs }
6729 1.1 rjs }
6730 1.1 rjs /* Is there something to send for this destination? */
6731 1.1 rjs if (m) {
6732 1.1 rjs /* No matter if we fail/or suceed we should
6733 1.1 rjs * start a timer. A failure is like a lost
6734 1.1 rjs * IP packet :-)
6735 1.1 rjs */
6736 1.1 rjs if (!callout_pending(&net->rxt_timer.timer)) {
6737 1.1 rjs /* no timer running on this destination
6738 1.1 rjs * restart it.
6739 1.1 rjs */
6740 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6741 1.1 rjs tmr_started = 1;
6742 1.1 rjs }
6743 1.1 rjs if (m->m_len == 0) {
6744 1.1 rjs /* Special case for when you get a 0 len
6745 1.1 rjs * mbuf at the head due to the lack
6746 1.1 rjs * of a MHDR at the beginning.
6747 1.1 rjs */
6748 1.1 rjs m->m_len = sizeof(struct sctphdr);
6749 1.1 rjs } else {
6750 1.1 rjs M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6751 1.1 rjs if (m == NULL) {
6752 1.1 rjs return (ENOBUFS);
6753 1.1 rjs }
6754 1.1 rjs }
6755 1.1 rjs shdr = mtod(m, struct sctphdr *);
6756 1.1 rjs shdr->src_port = inp->sctp_lport;
6757 1.1 rjs shdr->dest_port = stcb->rport;
6758 1.1 rjs shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6759 1.1 rjs shdr->checksum = 0;
6760 1.1 rjs
6761 1.1 rjs /* Now lets send it, if there is anything to send :> */
6762 1.1 rjs if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6763 1.1 rjs rtcache_getdst(&net->ro),
6764 1.1 rjs m,
6765 1.1 rjs no_fragmentflg, 0, NULL, asconf))) {
6766 1.1 rjs /* error, we could not output */
6767 1.1 rjs sctp_pegs[SCTP_DATA_OUT_ERR]++;
6768 1.1 rjs return (error);
6769 1.1 rjs }
6770 1.1 rjs /* For HB's */
6771 1.1 rjs /*
6772 1.1 rjs * We don't want to mark the net->sent time here since
6773 1.1 rjs * this we use this for HB and retrans cannot measure
6774 1.1 rjs * RTT
6775 1.1 rjs */
6776 1.1 rjs /* SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
6777 1.1 rjs
6778 1.1 rjs /* For auto-close */
6779 1.1 rjs cnt_thru++;
6780 1.1 rjs if (*now_filled == 0) {
6781 1.1 rjs SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
6782 1.1 rjs *now = asoc->time_last_sent;
6783 1.1 rjs *now_filled = 1;
6784 1.1 rjs } else {
6785 1.1 rjs asoc->time_last_sent = *now;
6786 1.1 rjs }
6787 1.1 rjs *cnt_out += bundle_at;
6788 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6789 1.1 rjs sctp_audit_log(0xC4, bundle_at);
6790 1.1 rjs #endif
6791 1.1 rjs for (i = 0; i < bundle_at; i++) {
6792 1.1 rjs sctp_pegs[SCTP_RETRANTSN_SENT]++;
6793 1.1 rjs data_list[i]->sent = SCTP_DATAGRAM_SENT;
6794 1.1 rjs data_list[i]->snd_count++;
6795 1.1 rjs sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6796 1.1 rjs /* record the time */
6797 1.1 rjs data_list[i]->sent_rcv_time = asoc->time_last_sent;
6798 1.1 rjs net->flight_size += data_list[i]->book_size;
6799 1.1 rjs asoc->total_flight += data_list[i]->book_size;
6800 1.1 rjs asoc->total_flight_count++;
6801 1.1 rjs
6802 1.1 rjs #ifdef SCTP_LOG_RWND
6803 1.1 rjs sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6804 1.1 rjs asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
6805 1.1 rjs #endif
6806 1.1 rjs asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6807 1.1 rjs (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
6808 1.1 rjs if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6809 1.1 rjs /* SWS sender side engages */
6810 1.1 rjs asoc->peers_rwnd = 0;
6811 1.1 rjs }
6812 1.1 rjs
6813 1.1 rjs if ((i == 0) &&
6814 1.1 rjs (data_list[i]->rec.data.doing_fast_retransmit)) {
6815 1.1 rjs sctp_pegs[SCTP_FAST_RETRAN]++;
6816 1.1 rjs if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
6817 1.1 rjs (tmr_started == 0)) {
6818 1.1 rjs /*
6819 1.1 rjs * ok we just fast-retrans'd
6820 1.1 rjs * the lowest TSN, i.e the
6821 1.1 rjs * first on the list. In this
6822 1.1 rjs * case we want to give some
6823 1.1 rjs * more time to get a SACK
6824 1.1 rjs * back without a t3-expiring.
6825 1.1 rjs */
6826 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6827 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6828 1.1 rjs }
6829 1.1 rjs }
6830 1.1 rjs }
6831 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6832 1.1 rjs sctp_auditing(21, inp, stcb, NULL);
6833 1.1 rjs #endif
6834 1.1 rjs } else {
6835 1.1 rjs /* None will fit */
6836 1.1 rjs return (1);
6837 1.1 rjs }
6838 1.1 rjs if (asoc->sent_queue_retran_cnt <= 0) {
6839 1.1 rjs /* all done we have no more to retran */
6840 1.1 rjs asoc->sent_queue_retran_cnt = 0;
6841 1.1 rjs break;
6842 1.1 rjs }
6843 1.1 rjs if (one_chunk) {
6844 1.1 rjs /* No more room in rwnd */
6845 1.1 rjs return (1);
6846 1.1 rjs }
6847 1.1 rjs /* stop the for loop here. we sent out a packet */
6848 1.1 rjs break;
6849 1.1 rjs }
6850 1.1 rjs return (0);
6851 1.1 rjs }
6852 1.1 rjs
6853 1.1 rjs
6854 1.1 rjs static int
6855 1.1 rjs sctp_timer_validation(struct sctp_inpcb *inp,
6856 1.1 rjs struct sctp_tcb *stcb,
6857 1.1 rjs struct sctp_association *asoc,
6858 1.1 rjs int ret)
6859 1.1 rjs {
6860 1.1 rjs struct sctp_nets *net;
6861 1.1 rjs /* Validate that a timer is running somewhere */
6862 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6863 1.1 rjs if (callout_pending(&net->rxt_timer.timer)) {
6864 1.1 rjs /* Here is a timer */
6865 1.1 rjs return (ret);
6866 1.1 rjs }
6867 1.1 rjs }
6868 1.1 rjs /* Gak, we did not have a timer somewhere */
6869 1.1 rjs #ifdef SCTP_DEBUG
6870 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6871 1.1 rjs printf("Deadlock avoided starting timer on a dest at retran\n");
6872 1.1 rjs }
6873 1.1 rjs #endif
6874 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
6875 1.1 rjs return (ret);
6876 1.1 rjs }
6877 1.1 rjs
6878 1.1 rjs int
6879 1.1 rjs sctp_chunk_output(struct sctp_inpcb *inp,
6880 1.1 rjs struct sctp_tcb *stcb,
6881 1.1 rjs int from_where)
6882 1.1 rjs {
6883 1.1 rjs /* Ok this is the generic chunk service queue.
6884 1.1 rjs * we must do the following:
6885 1.1 rjs * - See if there are retransmits pending, if so we
6886 1.1 rjs * must do these first and return.
6887 1.1 rjs * - Service the stream queue that is next,
6888 1.1 rjs * moving any message (note I must get a complete
6889 1.1 rjs * message i.e. FIRST/MIDDLE and LAST to the out
6890 1.1 rjs * queue in one pass) and assigning TSN's
6891 1.1 rjs * - Check to see if the cwnd/rwnd allows any output, if
6892 1.1 rjs * so we go ahead and fomulate and send the low level
6893 1.1 rjs * chunks. Making sure to combine any control in the
6894 1.1 rjs * control chunk queue also.
6895 1.1 rjs */
6896 1.1 rjs struct sctp_association *asoc;
6897 1.1 rjs struct sctp_nets *net;
6898 1.1 rjs int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
6899 1.1 rjs struct timeval now;
6900 1.1 rjs int now_filled=0;
6901 1.1 rjs int cwnd_full=0;
6902 1.1 rjs asoc = &stcb->asoc;
6903 1.1 rjs tot_out = 0;
6904 1.1 rjs num_out = 0;
6905 1.1 rjs reason_code = 0;
6906 1.1 rjs sctp_pegs[SCTP_CALLS_TO_CO]++;
6907 1.1 rjs #ifdef SCTP_DEBUG
6908 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6909 1.1 rjs printf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
6910 1.1 rjs }
6911 1.1 rjs #endif
6912 1.1 rjs while (asoc->sent_queue_retran_cnt) {
6913 1.1 rjs /* Ok, it is retransmission time only, we send out only ONE
6914 1.1 rjs * packet with a single call off to the retran code.
6915 1.1 rjs */
6916 1.1 rjs ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
6917 1.1 rjs if (ret > 0) {
6918 1.1 rjs /* Can't send anymore */
6919 1.1 rjs #ifdef SCTP_DEBUG
6920 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6921 1.1 rjs printf("retransmission ret:%d -- full\n", ret);
6922 1.1 rjs }
6923 1.1 rjs #endif
6924 1.1 rjs /*
6925 1.1 rjs * now lets push out control by calling med-level
6926 1.1 rjs * output once. this assures that we WILL send HB's
6927 1.1 rjs * if queued too.
6928 1.1 rjs */
6929 1.1 rjs (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
6930 1.1 rjs &cwnd_full, from_where,
6931 1.1 rjs &now, &now_filled);
6932 1.1 rjs #ifdef SCTP_DEBUG
6933 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6934 1.1 rjs printf("Control send outputs:%d@full\n", num_out);
6935 1.1 rjs }
6936 1.1 rjs #endif
6937 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6938 1.1 rjs sctp_auditing(8, inp, stcb, NULL);
6939 1.1 rjs #endif
6940 1.1 rjs return (sctp_timer_validation(inp, stcb, asoc, ret));
6941 1.1 rjs }
6942 1.1 rjs if (ret < 0) {
6943 1.1 rjs /*
6944 1.1 rjs * The count was off.. retran is not happening so do
6945 1.1 rjs * the normal retransmission.
6946 1.1 rjs */
6947 1.1 rjs #ifdef SCTP_DEBUG
6948 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6949 1.1 rjs printf("Done with retrans, none left fill up window\n");
6950 1.1 rjs }
6951 1.1 rjs #endif
6952 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6953 1.1 rjs sctp_auditing(9, inp, stcb, NULL);
6954 1.1 rjs #endif
6955 1.1 rjs break;
6956 1.1 rjs }
6957 1.1 rjs if (from_where == 1) {
6958 1.1 rjs /* Only one transmission allowed out of a timeout */
6959 1.1 rjs #ifdef SCTP_DEBUG
6960 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6961 1.1 rjs printf("Only one packet allowed out\n");
6962 1.1 rjs }
6963 1.1 rjs #endif
6964 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6965 1.1 rjs sctp_auditing(10, inp, stcb, NULL);
6966 1.1 rjs #endif
6967 1.1 rjs /* Push out any control */
6968 1.1 rjs (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
6969 1.1 rjs &now, &now_filled);
6970 1.1 rjs return (ret);
6971 1.1 rjs }
6972 1.1 rjs if ((num_out == 0) && (ret == 0)) {
6973 1.1 rjs /* No more retrans to send */
6974 1.1 rjs break;
6975 1.1 rjs }
6976 1.1 rjs }
6977 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
6978 1.1 rjs sctp_auditing(12, inp, stcb, NULL);
6979 1.1 rjs #endif
6980 1.1 rjs /* Check for bad destinations, if they exist move chunks around. */
6981 1.1 rjs burst_limit = asoc->max_burst;
6982 1.1 rjs TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6983 1.1 rjs if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
6984 1.1 rjs SCTP_ADDR_NOT_REACHABLE) {
6985 1.1 rjs /*
6986 1.1 rjs * if possible move things off of this address
6987 1.1 rjs * we still may send below due to the dormant state
6988 1.1 rjs * but we try to find an alternate address to send
6989 1.1 rjs * to and if we have one we move all queued data on
6990 1.1 rjs * the out wheel to this alternate address.
6991 1.1 rjs */
6992 1.1 rjs sctp_move_to_an_alt(stcb, asoc, net);
6993 1.1 rjs } else {
6994 1.1 rjs /*
6995 1.1 rjs if ((asoc->sat_network) || (net->addr_is_local)) {
6996 1.1 rjs burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
6997 1.1 rjs }
6998 1.1 rjs */
6999 1.1 rjs #ifdef SCTP_DEBUG
7000 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7001 1.1 rjs printf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
7002 1.1 rjs }
7003 1.1 rjs #endif
7004 1.1 rjs
7005 1.1 rjs #ifdef SCTP_USE_ALLMAN_BURST
7006 1.1 rjs if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
7007 1.1 rjs if (net->ssthresh < net->cwnd)
7008 1.1 rjs net->ssthresh = net->cwnd;
7009 1.1 rjs net->cwnd = (net->flight_size+(burst_limit*net->mtu));
7010 1.1 rjs #ifdef SCTP_LOG_MAXBURST
7011 1.1 rjs sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7012 1.1 rjs #endif
7013 1.1 rjs sctp_pegs[SCTP_MAX_BURST_APL]++;
7014 1.1 rjs }
7015 1.1 rjs net->fast_retran_ip = 0;
7016 1.1 rjs #endif
7017 1.1 rjs }
7018 1.1 rjs
7019 1.1 rjs }
7020 1.1 rjs /* Fill up what we can to the destination */
7021 1.1 rjs burst_cnt = 0;
7022 1.1 rjs cwnd_full = 0;
7023 1.1 rjs do {
7024 1.1 rjs #ifdef SCTP_DEBUG
7025 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7026 1.1 rjs printf("Burst count:%d - call m-c-o\n", burst_cnt);
7027 1.1 rjs }
7028 1.1 rjs #endif
7029 1.1 rjs error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7030 1.1 rjs &reason_code, 0, &cwnd_full, from_where,
7031 1.1 rjs &now, &now_filled);
7032 1.1 rjs if (error) {
7033 1.1 rjs #ifdef SCTP_DEBUG
7034 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7035 1.1 rjs printf("Error %d was returned from med-c-op\n", error);
7036 1.1 rjs }
7037 1.1 rjs #endif
7038 1.1 rjs #ifdef SCTP_LOG_MAXBURST
7039 1.1 rjs sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7040 1.1 rjs #endif
7041 1.1 rjs break;
7042 1.1 rjs }
7043 1.1 rjs #ifdef SCTP_DEBUG
7044 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7045 1.1 rjs printf("m-c-o put out %d\n", num_out);
7046 1.1 rjs }
7047 1.1 rjs #endif
7048 1.1 rjs tot_out += num_out;
7049 1.1 rjs burst_cnt++;
7050 1.1 rjs } while (num_out
7051 1.1 rjs #ifndef SCTP_USE_ALLMAN_BURST
7052 1.1 rjs && (burst_cnt < burst_limit)
7053 1.1 rjs #endif
7054 1.1 rjs );
7055 1.1 rjs #ifndef SCTP_USE_ALLMAN_BURST
7056 1.1 rjs if (burst_cnt >= burst_limit) {
7057 1.1 rjs sctp_pegs[SCTP_MAX_BURST_APL]++;
7058 1.1 rjs asoc->burst_limit_applied = 1;
7059 1.1 rjs #ifdef SCTP_LOG_MAXBURST
7060 1.1 rjs sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
7061 1.1 rjs #endif
7062 1.1 rjs } else {
7063 1.1 rjs asoc->burst_limit_applied = 0;
7064 1.1 rjs }
7065 1.1 rjs #endif
7066 1.1 rjs
7067 1.1 rjs #ifdef SCTP_DEBUG
7068 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7069 1.1 rjs printf("Ok, we have put out %d chunks\n", tot_out);
7070 1.1 rjs }
7071 1.1 rjs #endif
7072 1.1 rjs if (tot_out == 0) {
7073 1.1 rjs sctp_pegs[SCTP_CO_NODATASNT]++;
7074 1.1 rjs if (asoc->stream_queue_cnt > 0) {
7075 1.1 rjs sctp_pegs[SCTP_SOS_NOSNT]++;
7076 1.1 rjs } else {
7077 1.1 rjs sctp_pegs[SCTP_NOS_NOSNT]++;
7078 1.1 rjs }
7079 1.1 rjs if (asoc->send_queue_cnt > 0) {
7080 1.1 rjs sctp_pegs[SCTP_SOSE_NOSNT]++;
7081 1.1 rjs } else {
7082 1.1 rjs sctp_pegs[SCTP_NOSE_NOSNT]++;
7083 1.1 rjs }
7084 1.1 rjs }
7085 1.1 rjs /* Now we need to clean up the control chunk chain if
7086 1.1 rjs * a ECNE is on it. It must be marked as UNSENT again
7087 1.1 rjs * so next call will continue to send it until
7088 1.1 rjs * such time that we get a CWR, to remove it.
7089 1.1 rjs */
7090 1.1 rjs sctp_fix_ecn_echo(asoc);
7091 1.1 rjs return (error);
7092 1.1 rjs }
7093 1.1 rjs
7094 1.1 rjs
7095 1.1 rjs int
7096 1.1 rjs sctp_output(struct sctp_inpcb *inp, struct mbuf *m,
7097 1.1 rjs struct sockaddr *addr, struct mbuf *control, struct lwp *l, int flags)
7098 1.1 rjs {
7099 1.1 rjs struct sctp_inpcb *t_inp;
7100 1.1 rjs struct sctp_tcb *stcb;
7101 1.1 rjs struct sctp_nets *net;
7102 1.1 rjs struct sctp_association *asoc;
7103 1.1 rjs int create_lock_applied = 0;
7104 1.1 rjs int queue_only, error = 0;
7105 1.1 rjs struct sctp_sndrcvinfo srcv;
7106 1.1 rjs int un_sent = 0;
7107 1.1 rjs int use_rcvinfo = 0;
7108 1.1 rjs t_inp = inp;
7109 1.1 rjs /* struct route ro;*/
7110 1.1 rjs
7111 1.1 rjs queue_only = 0;
7112 1.1 rjs stcb = NULL;
7113 1.1 rjs asoc = NULL;
7114 1.1 rjs net = NULL;
7115 1.1 rjs
7116 1.1 rjs #ifdef SCTP_DEBUG
7117 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7118 1.1 rjs printf("USR Send BEGINS\n");
7119 1.1 rjs }
7120 1.1 rjs #endif
7121 1.1 rjs
7122 1.1 rjs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7123 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
7124 1.1 rjs /* The listner can NOT send */
7125 1.1 rjs if (control) {
7126 1.1 rjs sctppcbinfo.mbuf_track--;
7127 1.1 rjs sctp_m_freem(control);
7128 1.1 rjs control = NULL;
7129 1.1 rjs }
7130 1.1 rjs sctp_m_freem(m);
7131 1.1 rjs return (EFAULT);
7132 1.1 rjs }
7133 1.1 rjs /* Can't allow a V6 address on a non-v6 socket */
7134 1.1 rjs if (addr) {
7135 1.1 rjs SCTP_ASOC_CREATE_LOCK(inp);
7136 1.1 rjs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
7137 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7138 1.1 rjs /* Should I really unlock ? */
7139 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7140 1.1 rjs if (control) {
7141 1.1 rjs sctppcbinfo.mbuf_track--;
7142 1.1 rjs sctp_m_freem(control);
7143 1.1 rjs control = NULL;
7144 1.1 rjs }
7145 1.1 rjs sctp_m_freem(m);
7146 1.1 rjs return (EFAULT);
7147 1.1 rjs }
7148 1.1 rjs create_lock_applied = 1;
7149 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7150 1.1 rjs (addr->sa_family == AF_INET6)) {
7151 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7152 1.1 rjs if (control) {
7153 1.1 rjs sctppcbinfo.mbuf_track--;
7154 1.1 rjs sctp_m_freem(control);
7155 1.1 rjs control = NULL;
7156 1.1 rjs }
7157 1.1 rjs sctp_m_freem(m);
7158 1.1 rjs return (EINVAL);
7159 1.1 rjs }
7160 1.1 rjs }
7161 1.1 rjs if (control) {
7162 1.1 rjs sctppcbinfo.mbuf_track++;
7163 1.1 rjs if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
7164 1.1 rjs sizeof(srcv))) {
7165 1.12 rjs if (srcv.sinfo_flags & SCTP_SENDALL) {
7166 1.1 rjs /* its a sendall */
7167 1.1 rjs sctppcbinfo.mbuf_track--;
7168 1.1 rjs sctp_m_freem(control);
7169 1.1 rjs if (create_lock_applied) {
7170 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7171 1.1 rjs create_lock_applied = 0;
7172 1.1 rjs }
7173 1.1 rjs return (sctp_sendall(inp, NULL, m, &srcv));
7174 1.1 rjs }
7175 1.1 rjs if (srcv.sinfo_assoc_id) {
7176 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7177 1.1 rjs SCTP_INP_RLOCK(inp);
7178 1.1 rjs stcb = LIST_FIRST(&inp->sctp_asoc_list);
7179 1.1 rjs if (stcb) {
7180 1.1 rjs SCTP_TCB_LOCK(stcb);
7181 1.1 rjs }
7182 1.1 rjs SCTP_INP_RUNLOCK(inp);
7183 1.1 rjs
7184 1.1 rjs if (stcb == NULL) {
7185 1.1 rjs if (create_lock_applied) {
7186 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7187 1.1 rjs create_lock_applied = 0;
7188 1.1 rjs }
7189 1.1 rjs sctppcbinfo.mbuf_track--;
7190 1.1 rjs sctp_m_freem(control);
7191 1.1 rjs sctp_m_freem(m);
7192 1.1 rjs return (ENOTCONN);
7193 1.1 rjs }
7194 1.1 rjs net = stcb->asoc.primary_destination;
7195 1.1 rjs } else {
7196 1.1 rjs stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
7197 1.1 rjs }
7198 1.1 rjs /*
7199 1.1 rjs * Question: Should I error here if the
7200 1.1 rjs
7201 1.1 rjs * assoc_id is no longer valid?
7202 1.1 rjs * i.e. I can't find it?
7203 1.1 rjs */
7204 1.1 rjs if ((stcb) &&
7205 1.1 rjs (addr != NULL)) {
7206 1.1 rjs /* Must locate the net structure */
7207 1.1 rjs if (addr)
7208 1.1 rjs net = sctp_findnet(stcb, addr);
7209 1.1 rjs }
7210 1.1 rjs if (net == NULL)
7211 1.1 rjs net = stcb->asoc.primary_destination;
7212 1.1 rjs }
7213 1.1 rjs use_rcvinfo = 1;
7214 1.1 rjs }
7215 1.1 rjs }
7216 1.1 rjs if (stcb == NULL) {
7217 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7218 1.1 rjs SCTP_INP_RLOCK(inp);
7219 1.1 rjs stcb = LIST_FIRST(&inp->sctp_asoc_list);
7220 1.1 rjs if (stcb) {
7221 1.1 rjs SCTP_TCB_LOCK(stcb);
7222 1.1 rjs }
7223 1.1 rjs SCTP_INP_RUNLOCK(inp);
7224 1.1 rjs if (stcb == NULL) {
7225 1.1 rjs if (create_lock_applied) {
7226 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7227 1.1 rjs create_lock_applied = 0;
7228 1.1 rjs }
7229 1.1 rjs if (control) {
7230 1.1 rjs sctppcbinfo.mbuf_track--;
7231 1.1 rjs sctp_m_freem(control);
7232 1.1 rjs control = NULL;
7233 1.1 rjs }
7234 1.1 rjs sctp_m_freem(m);
7235 1.1 rjs return (ENOTCONN);
7236 1.1 rjs }
7237 1.1 rjs if (addr == NULL) {
7238 1.1 rjs net = stcb->asoc.primary_destination;
7239 1.1 rjs } else {
7240 1.1 rjs net = sctp_findnet(stcb, addr);
7241 1.1 rjs if (net == NULL) {
7242 1.1 rjs net = stcb->asoc.primary_destination;
7243 1.1 rjs }
7244 1.1 rjs }
7245 1.1 rjs } else {
7246 1.1 rjs if (addr != NULL) {
7247 1.1 rjs SCTP_INP_WLOCK(inp);
7248 1.1 rjs SCTP_INP_INCR_REF(inp);
7249 1.1 rjs SCTP_INP_WUNLOCK(inp);
7250 1.1 rjs stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
7251 1.1 rjs if (stcb == NULL) {
7252 1.1 rjs SCTP_INP_WLOCK(inp);
7253 1.1 rjs SCTP_INP_DECR_REF(inp);
7254 1.1 rjs SCTP_INP_WUNLOCK(inp);
7255 1.1 rjs }
7256 1.1 rjs }
7257 1.1 rjs }
7258 1.1 rjs }
7259 1.1 rjs if ((stcb == NULL) &&
7260 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
7261 1.1 rjs if (control) {
7262 1.1 rjs sctppcbinfo.mbuf_track--;
7263 1.1 rjs sctp_m_freem(control);
7264 1.1 rjs control = NULL;
7265 1.1 rjs }
7266 1.1 rjs if (create_lock_applied) {
7267 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7268 1.1 rjs create_lock_applied = 0;
7269 1.1 rjs }
7270 1.1 rjs sctp_m_freem(m);
7271 1.1 rjs return (ENOTCONN);
7272 1.1 rjs } else if ((stcb == NULL) &&
7273 1.1 rjs (addr == NULL)) {
7274 1.1 rjs if (control) {
7275 1.1 rjs sctppcbinfo.mbuf_track--;
7276 1.1 rjs sctp_m_freem(control);
7277 1.1 rjs control = NULL;
7278 1.1 rjs }
7279 1.1 rjs if (create_lock_applied) {
7280 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7281 1.1 rjs create_lock_applied = 0;
7282 1.1 rjs }
7283 1.1 rjs sctp_m_freem(m);
7284 1.1 rjs return (ENOENT);
7285 1.1 rjs } else if (stcb == NULL) {
7286 1.1 rjs /* UDP mode, we must go ahead and start the INIT process */
7287 1.12 rjs if ((use_rcvinfo) && (srcv.sinfo_flags & SCTP_ABORT)) {
7288 1.1 rjs /* Strange user to do this */
7289 1.1 rjs if (control) {
7290 1.1 rjs sctppcbinfo.mbuf_track--;
7291 1.1 rjs sctp_m_freem(control);
7292 1.1 rjs control = NULL;
7293 1.1 rjs }
7294 1.1 rjs if (create_lock_applied) {
7295 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7296 1.1 rjs create_lock_applied = 0;
7297 1.1 rjs }
7298 1.1 rjs sctp_m_freem(m);
7299 1.1 rjs return (ENOENT);
7300 1.1 rjs }
7301 1.1 rjs stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
7302 1.1 rjs if (stcb == NULL) {
7303 1.1 rjs if (control) {
7304 1.1 rjs sctppcbinfo.mbuf_track--;
7305 1.1 rjs sctp_m_freem(control);
7306 1.1 rjs control = NULL;
7307 1.1 rjs }
7308 1.1 rjs if (create_lock_applied) {
7309 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7310 1.1 rjs create_lock_applied = 0;
7311 1.1 rjs }
7312 1.1 rjs sctp_m_freem(m);
7313 1.1 rjs return (error);
7314 1.1 rjs }
7315 1.1 rjs if (create_lock_applied) {
7316 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7317 1.1 rjs create_lock_applied = 0;
7318 1.1 rjs } else {
7319 1.1 rjs printf("Huh-1, create lock should have been applied!\n");
7320 1.1 rjs }
7321 1.1 rjs queue_only = 1;
7322 1.1 rjs asoc = &stcb->asoc;
7323 1.1 rjs asoc->state = SCTP_STATE_COOKIE_WAIT;
7324 1.1 rjs SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
7325 1.1 rjs if (control) {
7326 1.1 rjs /* see if a init structure exists in cmsg headers */
7327 1.1 rjs struct sctp_initmsg initm;
7328 1.1 rjs int i;
7329 1.1 rjs if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
7330 1.1 rjs sizeof(initm))) {
7331 1.1 rjs /* we have an INIT override of the default */
7332 1.1 rjs if (initm.sinit_max_attempts)
7333 1.1 rjs asoc->max_init_times = initm.sinit_max_attempts;
7334 1.1 rjs if (initm.sinit_num_ostreams)
7335 1.1 rjs asoc->pre_open_streams = initm.sinit_num_ostreams;
7336 1.1 rjs if (initm.sinit_max_instreams)
7337 1.1 rjs asoc->max_inbound_streams = initm.sinit_max_instreams;
7338 1.1 rjs if (initm.sinit_max_init_timeo)
7339 1.1 rjs asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
7340 1.1 rjs }
7341 1.1 rjs if (asoc->streamoutcnt < asoc->pre_open_streams) {
7342 1.1 rjs /* Default is NOT correct */
7343 1.1 rjs #ifdef SCTP_DEBUG
7344 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7345 1.1 rjs printf("Ok, defout:%d pre_open:%d\n",
7346 1.1 rjs asoc->streamoutcnt, asoc->pre_open_streams);
7347 1.1 rjs }
7348 1.1 rjs #endif
7349 1.1 rjs free(asoc->strmout, M_PCB);
7350 1.1 rjs asoc->strmout = NULL;
7351 1.1 rjs asoc->streamoutcnt = asoc->pre_open_streams;
7352 1.1 rjs asoc->strmout = malloc(asoc->streamoutcnt *
7353 1.1 rjs sizeof(struct sctp_stream_out), M_PCB,
7354 1.1 rjs M_WAIT);
7355 1.1 rjs for (i = 0; i < asoc->streamoutcnt; i++) {
7356 1.1 rjs /*
7357 1.1 rjs * inbound side must be set to 0xffff,
7358 1.1 rjs * also NOTE when we get the INIT-ACK
7359 1.1 rjs * back (for INIT sender) we MUST
7360 1.1 rjs * reduce the count (streamoutcnt) but
7361 1.1 rjs * first check if we sent to any of the
7362 1.1 rjs * upper streams that were dropped (if
7363 1.1 rjs * some were). Those that were dropped
7364 1.1 rjs * must be notified to the upper layer
7365 1.1 rjs * as failed to send.
7366 1.1 rjs */
7367 1.1 rjs asoc->strmout[i].next_sequence_sent = 0x0;
7368 1.1 rjs TAILQ_INIT(&asoc->strmout[i].outqueue);
7369 1.1 rjs asoc->strmout[i].stream_no = i;
7370 1.1 rjs asoc->strmout[i].next_spoke.tqe_next = 0;
7371 1.1 rjs asoc->strmout[i].next_spoke.tqe_prev = 0;
7372 1.1 rjs }
7373 1.1 rjs }
7374 1.1 rjs }
7375 1.1 rjs sctp_send_initiate(inp, stcb);
7376 1.1 rjs /*
7377 1.1 rjs * we may want to dig in after this call and adjust the MTU
7378 1.1 rjs * value. It defaulted to 1500 (constant) but the ro structure
7379 1.1 rjs * may now have an update and thus we may need to change it
7380 1.1 rjs * BEFORE we append the message.
7381 1.1 rjs */
7382 1.1 rjs net = stcb->asoc.primary_destination;
7383 1.1 rjs } else {
7384 1.1 rjs if (create_lock_applied) {
7385 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7386 1.1 rjs create_lock_applied = 0;
7387 1.1 rjs }
7388 1.1 rjs asoc = &stcb->asoc;
7389 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7390 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
7391 1.1 rjs queue_only = 1;
7392 1.1 rjs }
7393 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7394 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7395 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7396 1.1 rjs (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
7397 1.1 rjs if (control) {
7398 1.1 rjs sctppcbinfo.mbuf_track--;
7399 1.1 rjs sctp_m_freem(control);
7400 1.1 rjs control = NULL;
7401 1.1 rjs }
7402 1.1 rjs if ((use_rcvinfo) &&
7403 1.12 rjs (srcv.sinfo_flags & SCTP_ABORT)) {
7404 1.1 rjs sctp_msg_append(stcb, net, m, &srcv, flags);
7405 1.1 rjs error = 0;
7406 1.1 rjs } else {
7407 1.1 rjs if (m)
7408 1.1 rjs sctp_m_freem(m);
7409 1.1 rjs error = ECONNRESET;
7410 1.1 rjs }
7411 1.1 rjs SCTP_TCB_UNLOCK(stcb);
7412 1.1 rjs return (error);
7413 1.1 rjs }
7414 1.1 rjs }
7415 1.1 rjs if (create_lock_applied) {
7416 1.1 rjs /* we should never hit here with the create lock applied
7417 1.1 rjs *
7418 1.1 rjs */
7419 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
7420 1.1 rjs create_lock_applied = 0;
7421 1.1 rjs }
7422 1.1 rjs
7423 1.1 rjs
7424 1.1 rjs if (use_rcvinfo == 0) {
7425 1.1 rjs srcv = stcb->asoc.def_send;
7426 1.1 rjs }
7427 1.1 rjs #ifdef SCTP_DEBUG
7428 1.1 rjs else {
7429 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
7430 1.1 rjs printf("stream:%d\n", srcv.sinfo_stream);
7431 1.1 rjs printf("flags:%x\n", (u_int)srcv.sinfo_flags);
7432 1.1 rjs printf("ppid:%d\n", srcv.sinfo_ppid);
7433 1.1 rjs printf("context:%d\n", srcv.sinfo_context);
7434 1.1 rjs }
7435 1.1 rjs }
7436 1.1 rjs #endif
7437 1.1 rjs if (control) {
7438 1.1 rjs sctppcbinfo.mbuf_track--;
7439 1.1 rjs sctp_m_freem(control);
7440 1.1 rjs control = NULL;
7441 1.1 rjs }
7442 1.12 rjs if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
7443 1.1 rjs /* we take the override or the unconfirmed */
7444 1.1 rjs ;
7445 1.1 rjs } else {
7446 1.1 rjs net = stcb->asoc.primary_destination;
7447 1.1 rjs }
7448 1.1 rjs if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
7449 1.1 rjs SCTP_TCB_UNLOCK(stcb);
7450 1.1 rjs return (error);
7451 1.1 rjs }
7452 1.1 rjs if (net->flight_size > net->cwnd) {
7453 1.1 rjs sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
7454 1.1 rjs queue_only = 1;
7455 1.1 rjs } else if (asoc->ifp_had_enobuf) {
7456 1.1 rjs sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
7457 1.1 rjs queue_only = 1;
7458 1.1 rjs } else {
7459 1.1 rjs un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7460 1.1 rjs ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
7461 1.1 rjs SCTP_MED_OVERHEAD);
7462 1.1 rjs
7463 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
7464 1.1 rjs (stcb->asoc.total_flight > 0) &&
7465 1.1 rjs (un_sent < (int)stcb->asoc.smallest_mtu)
7466 1.1 rjs ) {
7467 1.1 rjs
7468 1.1 rjs /* Ok, Nagle is set on and we have
7469 1.1 rjs * data outstanding. Don't send anything
7470 1.1 rjs * and let the SACK drive out the data.
7471 1.1 rjs */
7472 1.1 rjs sctp_pegs[SCTP_NAGLE_NOQ]++;
7473 1.1 rjs queue_only = 1;
7474 1.1 rjs } else {
7475 1.1 rjs sctp_pegs[SCTP_NAGLE_OFF]++;
7476 1.1 rjs }
7477 1.1 rjs }
7478 1.1 rjs if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
7479 1.1 rjs /* we can attempt to send too.*/
7480 1.1 rjs #ifdef SCTP_DEBUG
7481 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7482 1.1 rjs printf("USR Send calls sctp_chunk_output\n");
7483 1.1 rjs }
7484 1.1 rjs #endif
7485 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
7486 1.1 rjs sctp_audit_log(0xC0, 1);
7487 1.1 rjs sctp_auditing(6, inp, stcb, net);
7488 1.1 rjs #endif
7489 1.1 rjs sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
7490 1.1 rjs sctp_chunk_output(inp, stcb, 0);
7491 1.1 rjs #ifdef SCTP_AUDITING_ENABLED
7492 1.1 rjs sctp_audit_log(0xC0, 2);
7493 1.1 rjs sctp_auditing(7, inp, stcb, net);
7494 1.1 rjs #endif
7495 1.1 rjs
7496 1.1 rjs }
7497 1.1 rjs #ifdef SCTP_DEBUG
7498 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7499 1.1 rjs printf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
7500 1.1 rjs }
7501 1.1 rjs #endif
7502 1.1 rjs SCTP_TCB_UNLOCK(stcb);
7503 1.1 rjs return (0);
7504 1.1 rjs }
7505 1.1 rjs
7506 1.1 rjs void
7507 1.1 rjs send_forward_tsn(struct sctp_tcb *stcb,
7508 1.1 rjs struct sctp_association *asoc)
7509 1.1 rjs {
7510 1.1 rjs struct sctp_tmit_chunk *chk;
7511 1.1 rjs struct sctp_forward_tsn_chunk *fwdtsn;
7512 1.1 rjs
7513 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7514 1.1 rjs if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
7515 1.1 rjs /* mark it to unsent */
7516 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
7517 1.1 rjs chk->snd_count = 0;
7518 1.1 rjs /* Do we correct its output location? */
7519 1.1 rjs if (chk->whoTo != asoc->primary_destination) {
7520 1.1 rjs sctp_free_remote_addr(chk->whoTo);
7521 1.1 rjs chk->whoTo = asoc->primary_destination;
7522 1.1 rjs chk->whoTo->ref_count++;
7523 1.1 rjs }
7524 1.1 rjs goto sctp_fill_in_rest;
7525 1.1 rjs }
7526 1.1 rjs }
7527 1.1 rjs /* Ok if we reach here we must build one */
7528 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7529 1.1 rjs if (chk == NULL) {
7530 1.1 rjs return;
7531 1.1 rjs }
7532 1.1 rjs sctppcbinfo.ipi_count_chunk++;
7533 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
7534 1.1 rjs chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
7535 1.1 rjs chk->asoc = asoc;
7536 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
7537 1.1 rjs if (chk->data == NULL) {
7538 1.1 rjs chk->whoTo->ref_count--;
7539 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
7540 1.1 rjs sctppcbinfo.ipi_count_chunk--;
7541 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7542 1.1 rjs panic("Chunk count is negative");
7543 1.1 rjs }
7544 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
7545 1.1 rjs return;
7546 1.1 rjs }
7547 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
7548 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
7549 1.1 rjs chk->snd_count = 0;
7550 1.1 rjs chk->whoTo = asoc->primary_destination;
7551 1.1 rjs chk->whoTo->ref_count++;
7552 1.1 rjs TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7553 1.1 rjs asoc->ctrl_queue_cnt++;
7554 1.1 rjs sctp_fill_in_rest:
7555 1.1 rjs /* Here we go through and fill out the part that
7556 1.1 rjs * deals with stream/seq of the ones we skip.
7557 1.1 rjs */
7558 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = 0;
7559 1.1 rjs {
7560 1.1 rjs struct sctp_tmit_chunk *at, *tp1, *last;
7561 1.1 rjs struct sctp_strseq *strseq;
7562 1.1 rjs unsigned int cnt_of_space, i, ovh;
7563 1.1 rjs unsigned int space_needed;
7564 1.1 rjs unsigned int cnt_of_skipped = 0;
7565 1.1 rjs TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7566 1.1 rjs if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7567 1.1 rjs /* no more to look at */
7568 1.1 rjs break;
7569 1.1 rjs }
7570 1.1 rjs if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7571 1.1 rjs /* We don't report these */
7572 1.1 rjs continue;
7573 1.1 rjs }
7574 1.1 rjs cnt_of_skipped++;
7575 1.1 rjs }
7576 1.1 rjs space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7577 1.1 rjs (cnt_of_skipped * sizeof(struct sctp_strseq)));
7578 1.1 rjs if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
7579 1.1 rjs ((chk->data->m_flags & M_EXT) == 0)) {
7580 1.1 rjs /* Need a M_EXT, get one and move
7581 1.1 rjs * fwdtsn to data area.
7582 1.1 rjs */
7583 1.1 rjs MCLGET(chk->data, M_DONTWAIT);
7584 1.1 rjs }
7585 1.1 rjs cnt_of_space = M_TRAILINGSPACE(chk->data);
7586 1.1 rjs
7587 1.1 rjs if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7588 1.1 rjs ovh = SCTP_MIN_OVERHEAD;
7589 1.1 rjs } else {
7590 1.1 rjs ovh = SCTP_MIN_V4_OVERHEAD;
7591 1.1 rjs }
7592 1.1 rjs if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
7593 1.1 rjs /* trim to a mtu size */
7594 1.1 rjs cnt_of_space = asoc->smallest_mtu - ovh;
7595 1.1 rjs }
7596 1.1 rjs if (cnt_of_space < space_needed) {
7597 1.1 rjs /* ok we must trim down the chunk by lowering
7598 1.1 rjs * the advance peer ack point.
7599 1.1 rjs */
7600 1.1 rjs cnt_of_skipped = (cnt_of_space-
7601 1.1 rjs ((sizeof(struct sctp_forward_tsn_chunk))/
7602 1.1 rjs sizeof(struct sctp_strseq)));
7603 1.1 rjs /* Go through and find the TSN that
7604 1.1 rjs * will be the one we report.
7605 1.1 rjs */
7606 1.1 rjs at = TAILQ_FIRST(&asoc->sent_queue);
7607 1.1 rjs for (i = 0; i < cnt_of_skipped; i++) {
7608 1.1 rjs tp1 = TAILQ_NEXT(at, sctp_next);
7609 1.1 rjs at = tp1;
7610 1.1 rjs }
7611 1.1 rjs last = at;
7612 1.1 rjs /* last now points to last one I can report, update peer ack point */
7613 1.1 rjs asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7614 1.1 rjs space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7615 1.1 rjs }
7616 1.1 rjs chk->send_size = space_needed;
7617 1.1 rjs /* Setup the chunk */
7618 1.1 rjs fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7619 1.1 rjs fwdtsn->ch.chunk_length = htons(chk->send_size);
7620 1.1 rjs fwdtsn->ch.chunk_flags = 0;
7621 1.1 rjs fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7622 1.1 rjs fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7623 1.1 rjs chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7624 1.1 rjs (cnt_of_skipped * sizeof(struct sctp_strseq)));
7625 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7626 1.1 rjs fwdtsn++;
7627 1.1 rjs /* Move pointer to after the fwdtsn and transfer to
7628 1.1 rjs * the strseq pointer.
7629 1.1 rjs */
7630 1.1 rjs strseq = (struct sctp_strseq *)fwdtsn;
7631 1.1 rjs /*
7632 1.1 rjs * Now populate the strseq list. This is done blindly
7633 1.1 rjs * without pulling out duplicate stream info. This is
7634 1.1 rjs * inefficent but won't harm the process since the peer
7635 1.1 rjs * will look at these in sequence and will thus release
7636 1.1 rjs * anything. It could mean we exceed the PMTU and chop
7637 1.1 rjs * off some that we could have included.. but this is
7638 1.1 rjs * unlikely (aka 1432/4 would mean 300+ stream seq's would
7639 1.1 rjs * have to be reported in one FWD-TSN. With a bit of work
7640 1.1 rjs * we can later FIX this to optimize and pull out duplcates..
7641 1.1 rjs * but it does add more overhead. So for now... not!
7642 1.1 rjs */
7643 1.1 rjs at = TAILQ_FIRST(&asoc->sent_queue);
7644 1.1 rjs for (i = 0; i < cnt_of_skipped; i++) {
7645 1.1 rjs tp1 = TAILQ_NEXT(at, sctp_next);
7646 1.1 rjs if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7647 1.1 rjs /* We don't report these */
7648 1.1 rjs i--;
7649 1.1 rjs at = tp1;
7650 1.1 rjs continue;
7651 1.1 rjs }
7652 1.1 rjs strseq->stream = ntohs(at->rec.data.stream_number);
7653 1.1 rjs strseq->sequence = ntohs(at->rec.data.stream_seq);
7654 1.1 rjs strseq++;
7655 1.1 rjs at = tp1;
7656 1.1 rjs }
7657 1.1 rjs }
7658 1.1 rjs return;
7659 1.1 rjs
7660 1.1 rjs }
7661 1.1 rjs
7662 1.1 rjs void
7663 1.1 rjs sctp_send_sack(struct sctp_tcb *stcb)
7664 1.1 rjs {
7665 1.1 rjs /*
7666 1.1 rjs * Queue up a SACK in the control queue. We must first check to
7667 1.1 rjs * see if a SACK is somehow on the control queue. If so, we will
7668 1.13 maya * take and remove the old one.
7669 1.1 rjs */
7670 1.1 rjs struct sctp_association *asoc;
7671 1.1 rjs struct sctp_tmit_chunk *chk, *a_chk;
7672 1.1 rjs struct sctp_sack_chunk *sack;
7673 1.1 rjs struct sctp_gap_ack_block *gap_descriptor;
7674 1.1 rjs uint32_t *dup;
7675 1.1 rjs int start;
7676 1.1 rjs unsigned int i, maxi, seeing_ones, m_size;
7677 1.1 rjs unsigned int num_gap_blocks, space;
7678 1.1 rjs
7679 1.1 rjs start = maxi = 0;
7680 1.1 rjs seeing_ones = 1;
7681 1.1 rjs a_chk = NULL;
7682 1.1 rjs asoc = &stcb->asoc;
7683 1.1 rjs if (asoc->last_data_chunk_from == NULL) {
7684 1.1 rjs /* Hmm we never received anything */
7685 1.1 rjs return;
7686 1.1 rjs }
7687 1.1 rjs sctp_set_rwnd(stcb, asoc);
7688 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7689 1.1 rjs if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
7690 1.1 rjs /* Hmm, found a sack already on queue, remove it */
7691 1.1 rjs TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7692 1.1 rjs asoc->ctrl_queue_cnt++;
7693 1.1 rjs a_chk = chk;
7694 1.1 rjs if (a_chk->data)
7695 1.1 rjs sctp_m_freem(a_chk->data);
7696 1.1 rjs a_chk->data = NULL;
7697 1.1 rjs sctp_free_remote_addr(a_chk->whoTo);
7698 1.1 rjs a_chk->whoTo = NULL;
7699 1.1 rjs break;
7700 1.1 rjs }
7701 1.1 rjs }
7702 1.1 rjs if (a_chk == NULL) {
7703 1.1 rjs a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7704 1.1 rjs if (a_chk == NULL) {
7705 1.1 rjs /* No memory so we drop the idea, and set a timer */
7706 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7707 1.1 rjs stcb->sctp_ep, stcb, NULL);
7708 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7709 1.1 rjs stcb->sctp_ep, stcb, NULL);
7710 1.1 rjs return;
7711 1.1 rjs }
7712 1.1 rjs sctppcbinfo.ipi_count_chunk++;
7713 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
7714 1.1 rjs a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
7715 1.1 rjs }
7716 1.1 rjs a_chk->asoc = asoc;
7717 1.1 rjs a_chk->snd_count = 0;
7718 1.1 rjs a_chk->send_size = 0; /* fill in later */
7719 1.1 rjs a_chk->sent = SCTP_DATAGRAM_UNSENT;
7720 1.1 rjs m_size = (asoc->mapping_array_size << 3);
7721 1.1 rjs
7722 1.1 rjs if ((asoc->numduptsns) ||
7723 1.1 rjs (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7724 1.1 rjs ) {
7725 1.1 rjs /* Ok, we have some duplicates or the destination for the
7726 1.1 rjs * sack is unreachable, lets see if we can select an alternate
7727 1.1 rjs * than asoc->last_data_chunk_from
7728 1.1 rjs */
7729 1.1 rjs if ((!(asoc->last_data_chunk_from->dest_state &
7730 1.1 rjs SCTP_ADDR_NOT_REACHABLE)) &&
7731 1.1 rjs (asoc->used_alt_onsack > 2)) {
7732 1.1 rjs /* We used an alt last time, don't this time */
7733 1.1 rjs a_chk->whoTo = NULL;
7734 1.1 rjs } else {
7735 1.1 rjs asoc->used_alt_onsack++;
7736 1.1 rjs a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
7737 1.1 rjs }
7738 1.1 rjs if (a_chk->whoTo == NULL) {
7739 1.1 rjs /* Nope, no alternate */
7740 1.1 rjs a_chk->whoTo = asoc->last_data_chunk_from;
7741 1.1 rjs asoc->used_alt_onsack = 0;
7742 1.1 rjs }
7743 1.1 rjs } else {
7744 1.1 rjs /* No duplicates so we use the last
7745 1.1 rjs * place we received data from.
7746 1.1 rjs */
7747 1.1 rjs #ifdef SCTP_DEBUG
7748 1.1 rjs if (asoc->last_data_chunk_from == NULL) {
7749 1.1 rjs printf("Huh, last_data_chunk_from is null when we want to sack??\n");
7750 1.1 rjs }
7751 1.1 rjs #endif
7752 1.1 rjs asoc->used_alt_onsack = 0;
7753 1.1 rjs a_chk->whoTo = asoc->last_data_chunk_from;
7754 1.1 rjs }
7755 1.1 rjs if (a_chk->whoTo)
7756 1.1 rjs a_chk->whoTo->ref_count++;
7757 1.1 rjs
7758 1.1 rjs /* Ok now lets formulate a MBUF with our sack */
7759 1.1 rjs MGETHDR(a_chk->data, M_DONTWAIT, MT_DATA);
7760 1.1 rjs if ((a_chk->data == NULL) ||
7761 1.1 rjs (a_chk->whoTo == NULL)) {
7762 1.1 rjs /* rats, no mbuf memory */
7763 1.1 rjs if (a_chk->data) {
7764 1.1 rjs /* was a problem with the destination */
7765 1.1 rjs sctp_m_freem(a_chk->data);
7766 1.1 rjs a_chk->data = NULL;
7767 1.1 rjs }
7768 1.1 rjs a_chk->whoTo->ref_count--;
7769 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7770 1.1 rjs sctppcbinfo.ipi_count_chunk--;
7771 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7772 1.1 rjs panic("Chunk count is negative");
7773 1.1 rjs }
7774 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
7775 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7776 1.1 rjs stcb->sctp_ep, stcb, NULL);
7777 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7778 1.1 rjs stcb->sctp_ep, stcb, NULL);
7779 1.1 rjs return;
7780 1.1 rjs }
7781 1.1 rjs /* First count the number of gap ack blocks we need */
7782 1.1 rjs if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7783 1.1 rjs /* We know if there are none above the cum-ack we
7784 1.1 rjs * have everything with NO gaps
7785 1.1 rjs */
7786 1.1 rjs num_gap_blocks = 0;
7787 1.1 rjs } else {
7788 1.1 rjs /* Ok we must count how many gaps we
7789 1.1 rjs * have.
7790 1.1 rjs */
7791 1.1 rjs num_gap_blocks = 0;
7792 1.1 rjs if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
7793 1.1 rjs maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
7794 1.1 rjs } else {
7795 1.1 rjs maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
7796 1.1 rjs }
7797 1.1 rjs if (maxi > m_size) {
7798 1.1 rjs /* impossible but who knows, someone is playing with us :> */
7799 1.1 rjs #ifdef SCTP_DEBUG
7800 1.1 rjs printf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
7801 1.1 rjs maxi,
7802 1.1 rjs m_size,
7803 1.1 rjs asoc->highest_tsn_inside_map,
7804 1.1 rjs asoc->mapping_array_base_tsn,
7805 1.1 rjs asoc->cumulative_tsn
7806 1.1 rjs );
7807 1.1 rjs #endif
7808 1.1 rjs num_gap_blocks = 0;
7809 1.1 rjs goto no_gaps_now;
7810 1.1 rjs }
7811 1.1 rjs if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
7812 1.1 rjs start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
7813 1.1 rjs } else {
7814 1.1 rjs /* Set it so we start at 0 */
7815 1.1 rjs start = -1;
7816 1.1 rjs }
7817 1.1 rjs /* Ok move start up one to look at the NEXT past the cum-ack */
7818 1.1 rjs start++;
7819 1.1 rjs for (i = start; i <= maxi; i++) {
7820 1.1 rjs if (seeing_ones) {
7821 1.1 rjs /* while seeing ones I must
7822 1.1 rjs * transition back to 0 before
7823 1.1 rjs * finding the next gap and
7824 1.1 rjs * counting the segment.
7825 1.1 rjs */
7826 1.1 rjs if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7827 1.1 rjs seeing_ones = 0;
7828 1.1 rjs }
7829 1.1 rjs } else {
7830 1.1 rjs if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7831 1.1 rjs seeing_ones = 1;
7832 1.1 rjs num_gap_blocks++;
7833 1.1 rjs }
7834 1.1 rjs }
7835 1.1 rjs }
7836 1.1 rjs no_gaps_now:
7837 1.1 rjs if (num_gap_blocks == 0) {
7838 1.1 rjs /*
7839 1.1 rjs * Traveled all of the bits and NO one,
7840 1.1 rjs * must have reneged
7841 1.1 rjs */
7842 1.1 rjs if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
7843 1.1 rjs asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7844 1.1 rjs #ifdef SCTP_MAP_LOGGING
7845 1.1 rjs sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
7846 1.1 rjs #endif
7847 1.1 rjs }
7848 1.1 rjs }
7849 1.1 rjs }
7850 1.1 rjs
7851 1.1 rjs /* Now calculate the space needed */
7852 1.1 rjs space = (sizeof(struct sctp_sack_chunk) +
7853 1.1 rjs (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7854 1.1 rjs (asoc->numduptsns * sizeof(int32_t))
7855 1.1 rjs );
7856 1.1 rjs if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
7857 1.1 rjs /* Reduce the size of the sack to fit */
7858 1.1 rjs int calc, fit;
7859 1.1 rjs calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
7860 1.1 rjs calc -= sizeof(struct sctp_gap_ack_block);
7861 1.1 rjs fit = calc/sizeof(struct sctp_gap_ack_block);
7862 1.1 rjs if (fit > (int)num_gap_blocks) {
7863 1.1 rjs /* discard some dups */
7864 1.1 rjs asoc->numduptsns = (fit - num_gap_blocks);
7865 1.1 rjs } else {
7866 1.1 rjs /* discard all dups and some gaps */
7867 1.1 rjs num_gap_blocks = fit;
7868 1.1 rjs asoc->numduptsns = 0;
7869 1.1 rjs }
7870 1.1 rjs /* recalc space */
7871 1.1 rjs space = (sizeof(struct sctp_sack_chunk) +
7872 1.1 rjs (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7873 1.1 rjs (asoc->numduptsns * sizeof(int32_t))
7874 1.1 rjs );
7875 1.1 rjs
7876 1.1 rjs }
7877 1.1 rjs
7878 1.1 rjs if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
7879 1.1 rjs /* We need a cluster */
7880 1.1 rjs MCLGET(a_chk->data, M_DONTWAIT);
7881 1.1 rjs if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
7882 1.1 rjs /* can't get a cluster
7883 1.1 rjs * give up and try later.
7884 1.1 rjs */
7885 1.1 rjs if (a_chk->data)
7886 1.1 rjs sctp_m_freem(a_chk->data);
7887 1.1 rjs a_chk->data = NULL;
7888 1.1 rjs a_chk->whoTo->ref_count--;
7889 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7890 1.1 rjs sctppcbinfo.ipi_count_chunk--;
7891 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7892 1.1 rjs panic("Chunk count is negative");
7893 1.1 rjs }
7894 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
7895 1.1 rjs sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7896 1.1 rjs stcb->sctp_ep, stcb, NULL);
7897 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7898 1.1 rjs stcb->sctp_ep, stcb, NULL);
7899 1.1 rjs return;
7900 1.1 rjs }
7901 1.1 rjs }
7902 1.1 rjs
7903 1.1 rjs /* ok, lets go through and fill it in */
7904 1.1 rjs a_chk->data->m_data += SCTP_MIN_OVERHEAD;
7905 1.1 rjs sack = mtod(a_chk->data, struct sctp_sack_chunk *);
7906 1.1 rjs sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
7907 1.1 rjs sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
7908 1.1 rjs sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
7909 1.1 rjs sack->sack.a_rwnd = htonl(asoc->my_rwnd);
7910 1.1 rjs asoc->my_last_reported_rwnd = asoc->my_rwnd;
7911 1.1 rjs sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
7912 1.1 rjs sack->sack.num_dup_tsns = htons(asoc->numduptsns);
7913 1.1 rjs
7914 1.1 rjs a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
7915 1.1 rjs (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7916 1.1 rjs (asoc->numduptsns * sizeof(int32_t)));
7917 1.1 rjs a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
7918 1.1 rjs sack->ch.chunk_length = htons(a_chk->send_size);
7919 1.1 rjs
7920 1.1 rjs gap_descriptor = (struct sctp_gap_ack_block *)((vaddr_t)sack + sizeof(struct sctp_sack_chunk));
7921 1.1 rjs seeing_ones = 0;
7922 1.1 rjs for (i = start; i <= maxi; i++) {
7923 1.1 rjs if (num_gap_blocks == 0) {
7924 1.1 rjs break;
7925 1.1 rjs }
7926 1.1 rjs if (seeing_ones) {
7927 1.1 rjs /* while seeing Ones I must
7928 1.1 rjs * transition back to 0 before
7929 1.1 rjs * finding the next gap
7930 1.1 rjs */
7931 1.1 rjs if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7932 1.1 rjs gap_descriptor->end = htons(((uint16_t)(i-start)));
7933 1.1 rjs gap_descriptor++;
7934 1.1 rjs seeing_ones = 0;
7935 1.1 rjs num_gap_blocks--;
7936 1.1 rjs }
7937 1.1 rjs } else {
7938 1.1 rjs if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7939 1.1 rjs gap_descriptor->start = htons(((uint16_t)(i+1-start)));
7940 1.1 rjs /* advance struct to next pointer */
7941 1.1 rjs seeing_ones = 1;
7942 1.1 rjs }
7943 1.1 rjs }
7944 1.1 rjs }
7945 1.1 rjs if (num_gap_blocks) {
7946 1.1 rjs /* special case where the array is all 1's
7947 1.1 rjs * to the end of the array.
7948 1.1 rjs */
7949 1.1 rjs gap_descriptor->end = htons(((uint16_t)((i-start))));
7950 1.1 rjs gap_descriptor++;
7951 1.1 rjs }
7952 1.1 rjs /* now we must add any dups we are going to report. */
7953 1.1 rjs if (asoc->numduptsns) {
7954 1.1 rjs dup = (uint32_t *)gap_descriptor;
7955 1.1 rjs for (i = 0; i < asoc->numduptsns; i++) {
7956 1.1 rjs *dup = htonl(asoc->dup_tsns[i]);
7957 1.1 rjs dup++;
7958 1.1 rjs }
7959 1.1 rjs asoc->numduptsns = 0;
7960 1.1 rjs }
7961 1.1 rjs /* now that the chunk is prepared queue it to the control
7962 1.1 rjs * chunk queue.
7963 1.1 rjs */
7964 1.1 rjs TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
7965 1.1 rjs asoc->ctrl_queue_cnt++;
7966 1.1 rjs sctp_pegs[SCTP_PEG_SACKS_SENT]++;
7967 1.1 rjs return;
7968 1.1 rjs }
7969 1.1 rjs
7970 1.1 rjs void
7971 1.1 rjs sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
7972 1.1 rjs {
7973 1.1 rjs struct mbuf *m_abort;
7974 1.1 rjs struct sctp_abort_msg *abort_m;
7975 1.1 rjs int sz;
7976 1.1 rjs abort_m = NULL;
7977 1.1 rjs MGETHDR(m_abort, M_DONTWAIT, MT_HEADER);
7978 1.1 rjs if (m_abort == NULL) {
7979 1.1 rjs /* no mbuf's */
7980 1.1 rjs return;
7981 1.1 rjs }
7982 1.1 rjs m_abort->m_data += SCTP_MIN_OVERHEAD;
7983 1.1 rjs abort_m = mtod(m_abort, struct sctp_abort_msg *);
7984 1.1 rjs m_abort->m_len = sizeof(struct sctp_abort_msg);
7985 1.1 rjs m_abort->m_next = operr;
7986 1.1 rjs sz = 0;
7987 1.1 rjs if (operr) {
7988 1.1 rjs struct mbuf *n;
7989 1.1 rjs n = operr;
7990 1.1 rjs while (n) {
7991 1.1 rjs sz += n->m_len;
7992 1.1 rjs n = n->m_next;
7993 1.1 rjs }
7994 1.1 rjs }
7995 1.1 rjs abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
7996 1.1 rjs abort_m->msg.ch.chunk_flags = 0;
7997 1.1 rjs abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
7998 1.1 rjs sz);
7999 1.1 rjs abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
8000 1.1 rjs abort_m->sh.dest_port = stcb->rport;
8001 1.1 rjs abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8002 1.1 rjs abort_m->sh.checksum = 0;
8003 1.1 rjs m_abort->m_pkthdr.len = m_abort->m_len + sz;
8004 1.5 ozaki m_reset_rcvif(m_abort);
8005 1.1 rjs sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
8006 1.1 rjs stcb->asoc.primary_destination,
8007 1.1 rjs rtcache_getdst(&stcb->asoc.primary_destination->ro),
8008 1.1 rjs m_abort, 1, 0, NULL, 0);
8009 1.1 rjs }
8010 1.1 rjs
8011 1.1 rjs int
8012 1.1 rjs sctp_send_shutdown_complete(struct sctp_tcb *stcb,
8013 1.1 rjs struct sctp_nets *net)
8014 1.1 rjs
8015 1.1 rjs {
8016 1.1 rjs /* formulate and SEND a SHUTDOWN-COMPLETE */
8017 1.1 rjs struct mbuf *m_shutdown_comp;
8018 1.1 rjs struct sctp_shutdown_complete_msg *comp_cp;
8019 1.1 rjs
8020 1.1 rjs m_shutdown_comp = NULL;
8021 1.1 rjs MGETHDR(m_shutdown_comp, M_DONTWAIT, MT_HEADER);
8022 1.1 rjs if (m_shutdown_comp == NULL) {
8023 1.1 rjs /* no mbuf's */
8024 1.1 rjs return (-1);
8025 1.1 rjs }
8026 1.1 rjs m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
8027 1.1 rjs comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
8028 1.1 rjs comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8029 1.1 rjs comp_cp->shut_cmp.ch.chunk_flags = 0;
8030 1.1 rjs comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8031 1.1 rjs comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
8032 1.1 rjs comp_cp->sh.dest_port = stcb->rport;
8033 1.1 rjs comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8034 1.1 rjs comp_cp->sh.checksum = 0;
8035 1.1 rjs
8036 1.1 rjs m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
8037 1.5 ozaki m_reset_rcvif(m_shutdown_comp);
8038 1.1 rjs sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
8039 1.1 rjs rtcache_getdst(&net->ro), m_shutdown_comp,
8040 1.1 rjs 1, 0, NULL, 0);
8041 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
8042 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
8043 1.1 rjs stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
8044 1.1 rjs stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
8045 1.1 rjs soisdisconnected(stcb->sctp_ep->sctp_socket);
8046 1.1 rjs }
8047 1.1 rjs return (0);
8048 1.1 rjs }
8049 1.1 rjs
8050 1.1 rjs int
8051 1.1 rjs sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
8052 1.1 rjs {
8053 1.1 rjs /* formulate and SEND a SHUTDOWN-COMPLETE */
8054 1.1 rjs struct mbuf *mout;
8055 1.1 rjs struct ip *iph, *iph_out;
8056 1.1 rjs struct ip6_hdr *ip6, *ip6_out;
8057 1.1 rjs int offset_out;
8058 1.1 rjs struct sctp_shutdown_complete_msg *comp_cp;
8059 1.1 rjs
8060 1.1 rjs MGETHDR(mout, M_DONTWAIT, MT_HEADER);
8061 1.1 rjs if (mout == NULL) {
8062 1.1 rjs /* no mbuf's */
8063 1.1 rjs return (-1);
8064 1.1 rjs }
8065 1.1 rjs iph = mtod(m, struct ip *);
8066 1.1 rjs iph_out = NULL;
8067 1.1 rjs ip6_out = NULL;
8068 1.1 rjs offset_out = 0;
8069 1.1 rjs if (iph->ip_v == IPVERSION) {
8070 1.1 rjs mout->m_len = sizeof(struct ip) +
8071 1.1 rjs sizeof(struct sctp_shutdown_complete_msg);
8072 1.1 rjs mout->m_next = NULL;
8073 1.1 rjs iph_out = mtod(mout, struct ip *);
8074 1.1 rjs
8075 1.1 rjs /* Fill in the IP header for the ABORT */
8076 1.1 rjs iph_out->ip_v = IPVERSION;
8077 1.1 rjs iph_out->ip_hl = (sizeof(struct ip)/4);
8078 1.1 rjs iph_out->ip_tos = (u_char)0;
8079 1.1 rjs iph_out->ip_id = 0;
8080 1.1 rjs iph_out->ip_off = 0;
8081 1.1 rjs iph_out->ip_ttl = MAXTTL;
8082 1.1 rjs iph_out->ip_p = IPPROTO_SCTP;
8083 1.1 rjs iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8084 1.1 rjs iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8085 1.1 rjs
8086 1.1 rjs /* let IP layer calculate this */
8087 1.1 rjs iph_out->ip_sum = 0;
8088 1.1 rjs offset_out += sizeof(*iph_out);
8089 1.1 rjs comp_cp = (struct sctp_shutdown_complete_msg *)(
8090 1.1 rjs (vaddr_t)iph_out + offset_out);
8091 1.1 rjs } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8092 1.1 rjs ip6 = (struct ip6_hdr *)iph;
8093 1.1 rjs mout->m_len = sizeof(struct ip6_hdr) +
8094 1.1 rjs sizeof(struct sctp_shutdown_complete_msg);
8095 1.1 rjs mout->m_next = NULL;
8096 1.1 rjs ip6_out = mtod(mout, struct ip6_hdr *);
8097 1.1 rjs
8098 1.1 rjs /* Fill in the IPv6 header for the ABORT */
8099 1.1 rjs ip6_out->ip6_flow = ip6->ip6_flow;
8100 1.1 rjs ip6_out->ip6_hlim = ip6_defhlim;
8101 1.1 rjs ip6_out->ip6_nxt = IPPROTO_SCTP;
8102 1.1 rjs ip6_out->ip6_src = ip6->ip6_dst;
8103 1.1 rjs ip6_out->ip6_dst = ip6->ip6_src;
8104 1.1 rjs ip6_out->ip6_plen = mout->m_len;
8105 1.1 rjs offset_out += sizeof(*ip6_out);
8106 1.1 rjs comp_cp = (struct sctp_shutdown_complete_msg *)(
8107 1.1 rjs (vaddr_t)ip6_out + offset_out);
8108 1.1 rjs } else {
8109 1.1 rjs /* Currently not supported. */
8110 1.1 rjs return (-1);
8111 1.1 rjs }
8112 1.1 rjs
8113 1.1 rjs /* Now copy in and fill in the ABORT tags etc. */
8114 1.1 rjs comp_cp->sh.src_port = sh->dest_port;
8115 1.1 rjs comp_cp->sh.dest_port = sh->src_port;
8116 1.1 rjs comp_cp->sh.checksum = 0;
8117 1.1 rjs comp_cp->sh.v_tag = sh->v_tag;
8118 1.1 rjs comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8119 1.1 rjs comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8120 1.1 rjs comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8121 1.1 rjs
8122 1.1 rjs mout->m_pkthdr.len = mout->m_len;
8123 1.1 rjs /* add checksum */
8124 1.6 ozaki if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
8125 1.6 ozaki m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
8126 1.1 rjs comp_cp->sh.checksum = 0;
8127 1.1 rjs } else {
8128 1.1 rjs comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8129 1.1 rjs }
8130 1.1 rjs
8131 1.1 rjs /* zap the rcvif, it should be null */
8132 1.5 ozaki m_reset_rcvif(mout);
8133 1.1 rjs /* zap the stack pointer to the route */
8134 1.1 rjs if (iph_out != NULL) {
8135 1.1 rjs struct route ro;
8136 1.1 rjs
8137 1.1 rjs memset(&ro, 0, sizeof ro);
8138 1.1 rjs #ifdef SCTP_DEBUG
8139 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8140 1.1 rjs printf("sctp_shutdown_complete2 calling ip_output:\n");
8141 1.1 rjs sctp_print_address_pkt(iph_out, &comp_cp->sh);
8142 1.1 rjs }
8143 1.1 rjs #endif
8144 1.1 rjs /* set IPv4 length */
8145 1.1 rjs iph_out->ip_len = htons(mout->m_pkthdr.len);
8146 1.1 rjs /* out it goes */
8147 1.1 rjs ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
8148 1.1 rjs } else if (ip6_out != NULL) {
8149 1.1 rjs struct route ro;
8150 1.1 rjs
8151 1.1 rjs memset(&ro, 0, sizeof(ro));
8152 1.1 rjs #ifdef SCTP_DEBUG
8153 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8154 1.1 rjs printf("sctp_shutdown_complete2 calling ip6_output:\n");
8155 1.1 rjs sctp_print_address_pkt((struct ip *)ip6_out,
8156 1.1 rjs &comp_cp->sh);
8157 1.1 rjs }
8158 1.1 rjs #endif
8159 1.1 rjs ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
8160 1.1 rjs }
8161 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
8162 1.1 rjs return (0);
8163 1.1 rjs }
8164 1.1 rjs
8165 1.1 rjs static struct sctp_nets *
8166 1.1 rjs sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8167 1.1 rjs {
8168 1.1 rjs struct sctp_nets *net, *hnet;
8169 1.23 andvar int ms_goneby, highest_ms, state_override=0;
8170 1.1 rjs
8171 1.1 rjs SCTP_GETTIME_TIMEVAL(now);
8172 1.1 rjs highest_ms = 0;
8173 1.1 rjs hnet = NULL;
8174 1.1 rjs TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8175 1.1 rjs if (
8176 1.1 rjs ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8177 1.1 rjs (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8178 1.1 rjs ) {
8179 1.1 rjs /* Skip this guy from consideration if HB is off AND its confirmed*/
8180 1.1 rjs #ifdef SCTP_DEBUG
8181 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8182 1.1 rjs printf("Skipping net:%p state:%d nohb/out-of-scope\n",
8183 1.1 rjs net, net->dest_state);
8184 1.1 rjs }
8185 1.1 rjs #endif
8186 1.1 rjs continue;
8187 1.1 rjs }
8188 1.1 rjs if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro.ro_sa) == 0) {
8189 1.1 rjs /* skip this dest net from consideration */
8190 1.1 rjs #ifdef SCTP_DEBUG
8191 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8192 1.1 rjs printf("Skipping net:%p reachable NOT\n",
8193 1.1 rjs net);
8194 1.1 rjs }
8195 1.1 rjs #endif
8196 1.1 rjs continue;
8197 1.1 rjs }
8198 1.1 rjs if (net->last_sent_time.tv_sec) {
8199 1.1 rjs /* Sent to so we subtract */
8200 1.1 rjs ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8201 1.1 rjs } else
8202 1.1 rjs /* Never been sent to */
8203 1.1 rjs ms_goneby = 0x7fffffff;
8204 1.1 rjs #ifdef SCTP_DEBUG
8205 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8206 1.1 rjs printf("net:%p ms_goneby:%d\n",
8207 1.1 rjs net, ms_goneby);
8208 1.1 rjs }
8209 1.1 rjs #endif
8210 1.1 rjs /* When the address state is unconfirmed but still considered reachable, we
8211 1.1 rjs * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
8212 1.23 andvar * state, then we cut it back to HB at a more normal pace.
8213 1.1 rjs */
8214 1.1 rjs if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8215 1.23 andvar state_override = 1;
8216 1.1 rjs } else {
8217 1.23 andvar state_override = 0;
8218 1.1 rjs }
8219 1.1 rjs
8220 1.23 andvar if ((((unsigned int)ms_goneby >= net->RTO) || (state_override)) &&
8221 1.1 rjs (ms_goneby > highest_ms)) {
8222 1.1 rjs highest_ms = ms_goneby;
8223 1.1 rjs hnet = net;
8224 1.1 rjs #ifdef SCTP_DEBUG
8225 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8226 1.1 rjs printf("net:%p is the new high\n",
8227 1.1 rjs net);
8228 1.1 rjs }
8229 1.1 rjs #endif
8230 1.1 rjs }
8231 1.1 rjs }
8232 1.1 rjs if (hnet &&
8233 1.1 rjs ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8234 1.23 andvar state_override = 1;
8235 1.1 rjs } else {
8236 1.23 andvar state_override = 0;
8237 1.1 rjs }
8238 1.1 rjs
8239 1.23 andvar if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_override)) {
8240 1.1 rjs /* Found the one with longest delay bounds
8241 1.1 rjs * OR it is unconfirmed and still not marked
8242 1.1 rjs * unreachable.
8243 1.1 rjs */
8244 1.1 rjs #ifdef SCTP_DEBUG
8245 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8246 1.1 rjs printf("net:%p is the hb winner -",
8247 1.1 rjs hnet);
8248 1.1 rjs if (hnet)
8249 1.1 rjs sctp_print_address((struct sockaddr *)&hnet->ro.ro_sa);
8250 1.1 rjs else
8251 1.1 rjs printf(" none\n");
8252 1.1 rjs }
8253 1.1 rjs #endif
8254 1.1 rjs /* update the timer now */
8255 1.1 rjs hnet->last_sent_time = *now;
8256 1.1 rjs return (hnet);
8257 1.1 rjs }
8258 1.1 rjs /* Nothing to HB */
8259 1.1 rjs return (NULL);
8260 1.1 rjs }
8261 1.1 rjs
8262 1.1 rjs int
8263 1.1 rjs sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8264 1.1 rjs {
8265 1.1 rjs struct sctp_tmit_chunk *chk;
8266 1.1 rjs struct sctp_nets *net;
8267 1.1 rjs struct sctp_heartbeat_chunk *hb;
8268 1.1 rjs struct timeval now;
8269 1.1 rjs struct sockaddr_in *sin;
8270 1.1 rjs struct sockaddr_in6 *sin6;
8271 1.1 rjs
8272 1.1 rjs if (user_req == 0) {
8273 1.1 rjs net = sctp_select_hb_destination(stcb, &now);
8274 1.1 rjs if (net == NULL) {
8275 1.1 rjs /* All our busy none to send to, just
8276 1.1 rjs * start the timer again.
8277 1.1 rjs */
8278 1.1 rjs if (stcb->asoc.state == 0) {
8279 1.1 rjs return (0);
8280 1.1 rjs }
8281 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8282 1.1 rjs stcb->sctp_ep,
8283 1.1 rjs stcb,
8284 1.1 rjs net);
8285 1.1 rjs return (0);
8286 1.1 rjs }
8287 1.1 rjs #ifndef SCTP_USE_ALLMAN_BURST
8288 1.1 rjs else {
8289 1.1 rjs /* found one idle.. decay cwnd on this one
8290 1.1 rjs * by 1/2 if none outstanding.
8291 1.1 rjs */
8292 1.1 rjs
8293 1.1 rjs if (net->flight_size == 0) {
8294 1.1 rjs net->cwnd /= 2;
8295 1.1 rjs if (net->addr_is_local) {
8296 1.1 rjs if (net->cwnd < (net->mtu *4)) {
8297 1.1 rjs net->cwnd = net->mtu * 4;
8298 1.1 rjs }
8299 1.1 rjs } else {
8300 1.1 rjs if (net->cwnd < (net->mtu * 2)) {
8301 1.1 rjs net->cwnd = net->mtu * 2;
8302 1.1 rjs }
8303 1.1 rjs }
8304 1.1 rjs
8305 1.1 rjs }
8306 1.1 rjs
8307 1.1 rjs }
8308 1.1 rjs #endif
8309 1.1 rjs } else {
8310 1.1 rjs net = u_net;
8311 1.1 rjs if (net == NULL) {
8312 1.1 rjs return (0);
8313 1.1 rjs }
8314 1.1 rjs SCTP_GETTIME_TIMEVAL(&now);
8315 1.1 rjs }
8316 1.1 rjs sin = (struct sockaddr_in *)&net->ro.ro_sa;
8317 1.1 rjs if (sin->sin_family != AF_INET) {
8318 1.1 rjs if (sin->sin_family != AF_INET6) {
8319 1.1 rjs /* huh */
8320 1.1 rjs return (0);
8321 1.1 rjs }
8322 1.1 rjs }
8323 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8324 1.1 rjs if (chk == NULL) {
8325 1.1 rjs #ifdef SCTP_DEBUG
8326 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8327 1.1 rjs printf("Gak, can't get a chunk for hb\n");
8328 1.1 rjs }
8329 1.1 rjs #endif
8330 1.1 rjs return (0);
8331 1.1 rjs }
8332 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8333 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8334 1.1 rjs chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
8335 1.1 rjs chk->asoc = &stcb->asoc;
8336 1.1 rjs chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8337 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8338 1.1 rjs if (chk->data == NULL) {
8339 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8340 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8341 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8342 1.1 rjs panic("Chunk count is negative");
8343 1.1 rjs }
8344 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8345 1.1 rjs return (0);
8346 1.1 rjs }
8347 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8348 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8349 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8350 1.1 rjs chk->snd_count = 0;
8351 1.1 rjs chk->whoTo = net;
8352 1.1 rjs chk->whoTo->ref_count++;
8353 1.1 rjs /* Now we have a mbuf that we can fill in with the details */
8354 1.1 rjs hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8355 1.1 rjs
8356 1.1 rjs /* fill out chunk header */
8357 1.1 rjs hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8358 1.1 rjs hb->ch.chunk_flags = 0;
8359 1.1 rjs hb->ch.chunk_length = htons(chk->send_size);
8360 1.1 rjs /* Fill out hb parameter */
8361 1.1 rjs hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8362 1.1 rjs hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8363 1.1 rjs hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8364 1.1 rjs hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8365 1.1 rjs /* Did our user request this one, put it in */
8366 1.1 rjs hb->heartbeat.hb_info.user_req = user_req;
8367 1.1 rjs hb->heartbeat.hb_info.addr_family = sin->sin_family;
8368 1.1 rjs hb->heartbeat.hb_info.addr_len = sin->sin_len;
8369 1.1 rjs if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8370 1.1 rjs /* we only take from the entropy pool if the address is
8371 1.1 rjs * not confirmed.
8372 1.1 rjs */
8373 1.1 rjs net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8374 1.1 rjs net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8375 1.1 rjs } else {
8376 1.1 rjs net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8377 1.1 rjs net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8378 1.1 rjs }
8379 1.1 rjs if (sin->sin_family == AF_INET) {
8380 1.1 rjs memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8381 1.1 rjs } else if (sin->sin_family == AF_INET6) {
8382 1.1 rjs /* We leave the scope the way it is in our lookup table. */
8383 1.1 rjs sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
8384 1.1 rjs memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8385 1.1 rjs } else {
8386 1.1 rjs /* huh compiler bug */
8387 1.1 rjs #ifdef SCTP_DEBUG
8388 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
8389 1.1 rjs printf("Compiler bug bleeds a mbuf and a chunk\n");
8390 1.1 rjs }
8391 1.1 rjs #endif
8392 1.1 rjs return (0);
8393 1.1 rjs }
8394 1.1 rjs /* ok we have a destination that needs a beat */
8395 1.1 rjs /* lets do the theshold management Qiaobing style */
8396 1.1 rjs if (user_req == 0) {
8397 1.1 rjs if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8398 1.1 rjs stcb->asoc.max_send_times)) {
8399 1.1 rjs /* we have lost the association, in a way this
8400 1.1 rjs * is quite bad since we really are one less time
8401 1.1 rjs * since we really did not send yet. This is the
8402 1.1 rjs * down side to the Q's style as defined in the RFC
8403 1.1 rjs * and not my alternate style defined in the RFC.
8404 1.1 rjs */
8405 1.1 rjs if (chk->data != NULL) {
8406 1.1 rjs sctp_m_freem(chk->data);
8407 1.1 rjs chk->data = NULL;
8408 1.1 rjs }
8409 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8410 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8411 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8412 1.1 rjs panic("Chunk count is negative");
8413 1.1 rjs }
8414 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8415 1.1 rjs return (-1);
8416 1.1 rjs }
8417 1.1 rjs }
8418 1.1 rjs net->hb_responded = 0;
8419 1.1 rjs #ifdef SCTP_DEBUG
8420 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8421 1.1 rjs printf("Inserting chunk for HB\n");
8422 1.1 rjs }
8423 1.1 rjs #endif
8424 1.1 rjs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8425 1.1 rjs stcb->asoc.ctrl_queue_cnt++;
8426 1.1 rjs sctp_pegs[SCTP_HB_SENT]++;
8427 1.1 rjs /*
8428 1.1 rjs * Call directly med level routine to put out the chunk. It will
8429 1.1 rjs * always tumble out control chunks aka HB but it may even tumble
8430 1.1 rjs * out data too.
8431 1.1 rjs */
8432 1.1 rjs if (user_req == 0) {
8433 1.1 rjs /* Ok now lets start the HB timer if it is NOT a user req */
8434 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
8435 1.1 rjs stcb, net);
8436 1.1 rjs }
8437 1.1 rjs return (1);
8438 1.1 rjs }
8439 1.1 rjs
8440 1.1 rjs void
8441 1.1 rjs sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8442 1.1 rjs uint32_t high_tsn)
8443 1.1 rjs {
8444 1.1 rjs struct sctp_association *asoc;
8445 1.1 rjs struct sctp_ecne_chunk *ecne;
8446 1.1 rjs struct sctp_tmit_chunk *chk;
8447 1.1 rjs asoc = &stcb->asoc;
8448 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8449 1.1 rjs if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
8450 1.1 rjs /* found a previous ECN_ECHO update it if needed */
8451 1.1 rjs ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8452 1.1 rjs ecne->tsn = htonl(high_tsn);
8453 1.1 rjs return;
8454 1.1 rjs }
8455 1.1 rjs }
8456 1.1 rjs /* nope could not find one to update so we must build one */
8457 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8458 1.1 rjs if (chk == NULL) {
8459 1.1 rjs return;
8460 1.1 rjs }
8461 1.1 rjs sctp_pegs[SCTP_ECNE_SENT]++;
8462 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8463 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8464 1.1 rjs chk->rec.chunk_id = SCTP_ECN_ECHO;
8465 1.1 rjs chk->asoc = &stcb->asoc;
8466 1.1 rjs chk->send_size = sizeof(struct sctp_ecne_chunk);
8467 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8468 1.1 rjs if (chk->data == NULL) {
8469 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8470 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8471 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8472 1.1 rjs panic("Chunk count is negative");
8473 1.1 rjs }
8474 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8475 1.1 rjs return;
8476 1.1 rjs }
8477 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8478 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8479 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8480 1.1 rjs chk->snd_count = 0;
8481 1.1 rjs chk->whoTo = net;
8482 1.1 rjs chk->whoTo->ref_count++;
8483 1.1 rjs ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8484 1.1 rjs ecne->ch.chunk_type = SCTP_ECN_ECHO;
8485 1.1 rjs ecne->ch.chunk_flags = 0;
8486 1.1 rjs ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8487 1.1 rjs ecne->tsn = htonl(high_tsn);
8488 1.1 rjs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8489 1.1 rjs asoc->ctrl_queue_cnt++;
8490 1.1 rjs }
8491 1.1 rjs
8492 1.1 rjs void
8493 1.1 rjs sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8494 1.1 rjs struct mbuf *m, int iphlen, int bad_crc)
8495 1.1 rjs {
8496 1.1 rjs struct sctp_association *asoc;
8497 1.1 rjs struct sctp_pktdrop_chunk *drp;
8498 1.1 rjs struct sctp_tmit_chunk *chk;
8499 1.1 rjs uint8_t *datap;
8500 1.1 rjs int len;
8501 1.1 rjs unsigned int small_one;
8502 1.1 rjs struct ip *iph;
8503 1.1 rjs
8504 1.1 rjs long spc;
8505 1.1 rjs asoc = &stcb->asoc;
8506 1.1 rjs if (asoc->peer_supports_pktdrop == 0) {
8507 1.1 rjs /* peer must declare support before I
8508 1.1 rjs * send one.
8509 1.1 rjs */
8510 1.1 rjs return;
8511 1.1 rjs }
8512 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8513 1.1 rjs if (chk == NULL) {
8514 1.1 rjs return;
8515 1.1 rjs }
8516 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8517 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8518 1.1 rjs
8519 1.1 rjs iph = mtod(m, struct ip *);
8520 1.1 rjs if (iph == NULL) {
8521 1.1 rjs return;
8522 1.1 rjs }
8523 1.1 rjs if (iph->ip_v == IPVERSION) {
8524 1.1 rjs /* IPv4 */
8525 1.1 rjs #if defined(__FreeBSD__)
8526 1.1 rjs len = chk->send_size = iph->ip_len;
8527 1.1 rjs #else
8528 1.1 rjs len = chk->send_size = (iph->ip_len - iphlen);
8529 1.1 rjs #endif
8530 1.1 rjs } else {
8531 1.1 rjs struct ip6_hdr *ip6h;
8532 1.1 rjs /* IPv6 */
8533 1.1 rjs ip6h = mtod(m, struct ip6_hdr *);
8534 1.1 rjs len = chk->send_size = htons(ip6h->ip6_plen);
8535 1.1 rjs }
8536 1.1 rjs if ((len+iphlen) > m->m_pkthdr.len) {
8537 1.1 rjs /* huh */
8538 1.1 rjs chk->send_size = len = m->m_pkthdr.len - iphlen;
8539 1.1 rjs }
8540 1.1 rjs chk->asoc = &stcb->asoc;
8541 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8542 1.1 rjs if (chk->data == NULL) {
8543 1.1 rjs jump_out:
8544 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8545 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8546 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8547 1.1 rjs panic("Chunk count is negative");
8548 1.1 rjs }
8549 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8550 1.1 rjs return;
8551 1.1 rjs }
8552 1.1 rjs if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
8553 1.1 rjs MCLGET(chk->data, M_DONTWAIT);
8554 1.1 rjs if ((chk->data->m_flags & M_EXT) == 0) {
8555 1.1 rjs /* Give up */
8556 1.1 rjs sctp_m_freem(chk->data);
8557 1.1 rjs chk->data = NULL;
8558 1.1 rjs goto jump_out;
8559 1.1 rjs }
8560 1.1 rjs }
8561 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8562 1.1 rjs drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8563 1.1 rjs if (drp == NULL) {
8564 1.1 rjs sctp_m_freem(chk->data);
8565 1.1 rjs chk->data = NULL;
8566 1.1 rjs goto jump_out;
8567 1.1 rjs }
8568 1.1 rjs small_one = asoc->smallest_mtu;
8569 1.1 rjs if (small_one > MCLBYTES) {
8570 1.1 rjs /* Only one cluster worth of data MAX */
8571 1.1 rjs small_one = MCLBYTES;
8572 1.1 rjs }
8573 1.1 rjs chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8574 1.1 rjs sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
8575 1.1 rjs if (chk->book_size > small_one) {
8576 1.1 rjs drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8577 1.1 rjs drp->trunc_len = htons(chk->send_size);
8578 1.1 rjs chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8579 1.1 rjs sizeof(struct sctp_pktdrop_chunk) +
8580 1.1 rjs sizeof(struct sctphdr));
8581 1.1 rjs len = chk->send_size;
8582 1.1 rjs } else {
8583 1.1 rjs /* no truncation needed */
8584 1.1 rjs drp->ch.chunk_flags = 0;
8585 1.1 rjs drp->trunc_len = htons(0);
8586 1.1 rjs }
8587 1.1 rjs if (bad_crc) {
8588 1.1 rjs drp->ch.chunk_flags |= SCTP_BADCRC;
8589 1.1 rjs }
8590 1.1 rjs chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8591 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8592 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8593 1.1 rjs chk->snd_count = 0;
8594 1.1 rjs if (net) {
8595 1.1 rjs /* we should hit here */
8596 1.1 rjs chk->whoTo = net;
8597 1.1 rjs } else {
8598 1.1 rjs chk->whoTo = asoc->primary_destination;
8599 1.1 rjs }
8600 1.1 rjs chk->whoTo->ref_count++;
8601 1.1 rjs chk->rec.chunk_id = SCTP_PACKET_DROPPED;
8602 1.1 rjs drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8603 1.1 rjs drp->ch.chunk_length = htons(chk->send_size);
8604 1.1 rjs spc = stcb->sctp_socket->so_rcv.sb_hiwat;
8605 1.1 rjs if (spc < 0) {
8606 1.1 rjs spc = 0;
8607 1.1 rjs }
8608 1.1 rjs drp->bottle_bw = htonl(spc);
8609 1.1 rjs drp->current_onq = htonl(asoc->size_on_delivery_queue +
8610 1.1 rjs asoc->size_on_reasm_queue +
8611 1.1 rjs asoc->size_on_all_streams +
8612 1.1 rjs asoc->my_rwnd_control_len +
8613 1.1 rjs stcb->sctp_socket->so_rcv.sb_cc);
8614 1.1 rjs drp->reserved = 0;
8615 1.1 rjs datap = drp->data;
8616 1.1 rjs m_copydata(m, iphlen, len, datap);
8617 1.1 rjs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8618 1.1 rjs asoc->ctrl_queue_cnt++;
8619 1.1 rjs }
8620 1.1 rjs
8621 1.1 rjs void
8622 1.1 rjs sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8623 1.1 rjs {
8624 1.1 rjs struct sctp_association *asoc;
8625 1.1 rjs struct sctp_cwr_chunk *cwr;
8626 1.1 rjs struct sctp_tmit_chunk *chk;
8627 1.1 rjs
8628 1.1 rjs asoc = &stcb->asoc;
8629 1.1 rjs TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8630 1.1 rjs if (chk->rec.chunk_id == SCTP_ECN_CWR) {
8631 1.1 rjs /* found a previous ECN_CWR update it if needed */
8632 1.1 rjs cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8633 1.1 rjs if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8634 1.1 rjs MAX_TSN)) {
8635 1.1 rjs cwr->tsn = htonl(high_tsn);
8636 1.1 rjs }
8637 1.1 rjs return;
8638 1.1 rjs }
8639 1.1 rjs }
8640 1.1 rjs /* nope could not find one to update so we must build one */
8641 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8642 1.1 rjs if (chk == NULL) {
8643 1.1 rjs return;
8644 1.1 rjs }
8645 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8646 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8647 1.1 rjs chk->rec.chunk_id = SCTP_ECN_CWR;
8648 1.1 rjs chk->asoc = &stcb->asoc;
8649 1.1 rjs chk->send_size = sizeof(struct sctp_cwr_chunk);
8650 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8651 1.1 rjs if (chk->data == NULL) {
8652 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8653 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8654 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8655 1.1 rjs panic("Chunk count is negative");
8656 1.1 rjs }
8657 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8658 1.1 rjs return;
8659 1.1 rjs }
8660 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8661 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8662 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8663 1.1 rjs chk->snd_count = 0;
8664 1.1 rjs chk->whoTo = net;
8665 1.1 rjs chk->whoTo->ref_count++;
8666 1.1 rjs cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8667 1.1 rjs cwr->ch.chunk_type = SCTP_ECN_CWR;
8668 1.1 rjs cwr->ch.chunk_flags = 0;
8669 1.1 rjs cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8670 1.1 rjs cwr->tsn = htonl(high_tsn);
8671 1.1 rjs TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8672 1.1 rjs asoc->ctrl_queue_cnt++;
8673 1.1 rjs }
8674 1.1 rjs static void
8675 1.1 rjs sctp_reset_the_streams(struct sctp_tcb *stcb,
8676 1.1 rjs struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
8677 1.1 rjs {
8678 1.1 rjs int i;
8679 1.1 rjs
8680 1.1 rjs if (req->reset_flags & SCTP_RESET_ALL) {
8681 1.1 rjs for (i=0; i<stcb->asoc.streamoutcnt; i++) {
8682 1.1 rjs stcb->asoc.strmout[i].next_sequence_sent = 0;
8683 1.1 rjs }
8684 1.1 rjs } else if (number_entries) {
8685 1.1 rjs for (i=0; i<number_entries; i++) {
8686 1.1 rjs if (list[i] >= stcb->asoc.streamoutcnt) {
8687 1.1 rjs /* no such stream */
8688 1.1 rjs continue;
8689 1.1 rjs }
8690 1.1 rjs stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
8691 1.1 rjs }
8692 1.1 rjs }
8693 1.1 rjs sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
8694 1.1 rjs }
8695 1.1 rjs
8696 1.1 rjs void
8697 1.1 rjs sctp_send_str_reset_ack(struct sctp_tcb *stcb,
8698 1.1 rjs struct sctp_stream_reset_request *req)
8699 1.1 rjs {
8700 1.1 rjs struct sctp_association *asoc;
8701 1.1 rjs struct sctp_stream_reset_resp *strack;
8702 1.1 rjs struct sctp_tmit_chunk *chk;
8703 1.1 rjs uint32_t seq;
8704 1.1 rjs int number_entries, i;
8705 1.1 rjs uint8_t two_way=0, not_peer=0;
8706 1.1 rjs uint16_t *list=NULL;
8707 1.1 rjs
8708 1.1 rjs asoc = &stcb->asoc;
8709 1.1 rjs if (req->reset_flags & SCTP_RESET_ALL)
8710 1.1 rjs number_entries = 0;
8711 1.1 rjs else
8712 1.1 rjs number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
8713 1.1 rjs
8714 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8715 1.1 rjs if (chk == NULL) {
8716 1.1 rjs return;
8717 1.1 rjs }
8718 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8719 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8720 1.1 rjs chk->rec.chunk_id = SCTP_STREAM_RESET;
8721 1.1 rjs chk->asoc = &stcb->asoc;
8722 1.1 rjs chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
8723 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8724 1.1 rjs if (chk->data == NULL) {
8725 1.1 rjs strresp_jump_out:
8726 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8727 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8728 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8729 1.1 rjs panic("Chunk count is negative");
8730 1.1 rjs }
8731 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8732 1.1 rjs return;
8733 1.1 rjs }
8734 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8735 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8736 1.1 rjs if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8737 1.1 rjs MCLGET(chk->data, M_DONTWAIT);
8738 1.1 rjs if ((chk->data->m_flags & M_EXT) == 0) {
8739 1.1 rjs /* Give up */
8740 1.1 rjs sctp_m_freem(chk->data);
8741 1.1 rjs chk->data = NULL;
8742 1.1 rjs goto strresp_jump_out;
8743 1.1 rjs }
8744 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8745 1.1 rjs }
8746 1.1 rjs if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8747 1.1 rjs /* can't do it, no room */
8748 1.1 rjs /* Give up */
8749 1.1 rjs sctp_m_freem(chk->data);
8750 1.1 rjs chk->data = NULL;
8751 1.1 rjs goto strresp_jump_out;
8752 1.1 rjs
8753 1.1 rjs }
8754 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8755 1.1 rjs chk->snd_count = 0;
8756 1.1 rjs chk->whoTo = asoc->primary_destination;
8757 1.1 rjs chk->whoTo->ref_count++;
8758 1.1 rjs strack = mtod(chk->data, struct sctp_stream_reset_resp *);
8759 1.1 rjs
8760 1.1 rjs strack->ch.chunk_type = SCTP_STREAM_RESET;
8761 1.1 rjs strack->ch.chunk_flags = 0;
8762 1.1 rjs strack->ch.chunk_length = htons(chk->send_size);
8763 1.1 rjs
8764 1.1 rjs memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
8765 1.1 rjs
8766 1.1 rjs strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
8767 1.1 rjs strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8768 1.1 rjs
8769 1.1 rjs
8770 1.1 rjs
8771 1.1 rjs if (chk->send_size % 4) {
8772 1.1 rjs /* need a padding for the end */
8773 1.1 rjs int pad;
8774 1.1 rjs uint8_t *end;
8775 1.1 rjs end = (uint8_t *)((vaddr_t)strack + chk->send_size);
8776 1.1 rjs pad = chk->send_size % 4;
8777 1.1 rjs for (i = 0; i < pad; i++) {
8778 1.1 rjs end[i] = 0;
8779 1.1 rjs }
8780 1.1 rjs chk->send_size += pad;
8781 1.1 rjs }
8782 1.1 rjs
8783 1.1 rjs /* actual response */
8784 1.1 rjs if (req->reset_flags & SCTP_RESET_YOUR) {
8785 1.1 rjs strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
8786 1.1 rjs } else {
8787 1.1 rjs strack->sr_resp.reset_flags = 0;
8788 1.1 rjs }
8789 1.1 rjs
8790 1.1 rjs /* copied from reset request */
8791 1.1 rjs strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
8792 1.1 rjs seq = ntohl(req->reset_req_seq);
8793 1.1 rjs
8794 1.1 rjs list = req->list_of_streams;
8795 1.1 rjs /* copy the un-converted network byte order streams */
8796 1.1 rjs for (i=0; i<number_entries; i++) {
8797 1.1 rjs strack->sr_resp.list_of_streams[i] = list[i];
8798 1.1 rjs }
8799 1.1 rjs if (asoc->str_reset_seq_in == seq) {
8800 1.1 rjs /* is it the next expected? */
8801 1.1 rjs asoc->str_reset_seq_in++;
8802 1.1 rjs strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
8803 1.1 rjs asoc->str_reset_sending_seq = asoc->sending_seq;
8804 1.1 rjs if (number_entries) {
8805 1.1 rjs uint16_t temp;
8806 1.1 rjs /* convert them to host byte order */
8807 1.1 rjs for (i=0 ; i<number_entries; i++) {
8808 1.1 rjs temp = ntohs(list[i]);
8809 1.1 rjs list[i] = temp;
8810 1.1 rjs }
8811 1.1 rjs }
8812 1.1 rjs if (req->reset_flags & SCTP_RESET_YOUR) {
8813 1.1 rjs /* reset my outbound streams */
8814 1.1 rjs sctp_reset_the_streams(stcb, req , number_entries, list);
8815 1.1 rjs }
8816 1.1 rjs if (req->reset_flags & SCTP_RECIPRICAL) {
8817 1.1 rjs /* reset peer too */
8818 1.1 rjs sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
8819 1.1 rjs }
8820 1.1 rjs
8821 1.1 rjs } else {
8822 1.1 rjs /* no its a retran so I must just ack and do nothing */
8823 1.1 rjs strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
8824 1.1 rjs }
8825 1.1 rjs strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
8826 1.1 rjs TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8827 1.1 rjs chk,
8828 1.1 rjs sctp_next);
8829 1.1 rjs asoc->ctrl_queue_cnt++;
8830 1.1 rjs }
8831 1.1 rjs
8832 1.1 rjs
8833 1.1 rjs void
8834 1.1 rjs sctp_send_str_reset_req(struct sctp_tcb *stcb,
8835 1.1 rjs int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
8836 1.1 rjs {
8837 1.1 rjs /* Send a stream reset request. The number_entrys may be 0 and list NULL
8838 1.1 rjs * if the request is to reset all streams. If two_way is true then we
8839 1.1 rjs * not only request a RESET of the received streams but we also
8840 1.1 rjs * request the peer to send a reset req to us too.
8841 1.1 rjs * Flag combinations in table:
8842 1.1 rjs *
8843 1.1 rjs * two_way | not_peer | = | Flags
8844 1.1 rjs * ------------------------------
8845 1.1 rjs * 0 | 0 | = | SCTP_RESET_YOUR (just the peer)
8846 1.1 rjs * 1 | 0 | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
8847 1.1 rjs * 0 | 1 | = | Not a Valid Request (not anyone)
8848 1.1 rjs * 1 | 1 | = | SCTP_RESET_RECIPRICAL (Just local host)
8849 1.1 rjs */
8850 1.1 rjs struct sctp_association *asoc;
8851 1.1 rjs struct sctp_stream_reset_req *strreq;
8852 1.1 rjs struct sctp_tmit_chunk *chk;
8853 1.1 rjs
8854 1.1 rjs
8855 1.1 rjs asoc = &stcb->asoc;
8856 1.1 rjs if (asoc->stream_reset_outstanding) {
8857 1.1 rjs /* Already one pending, must get ACK back
8858 1.1 rjs * to clear the flag.
8859 1.1 rjs */
8860 1.1 rjs return;
8861 1.1 rjs }
8862 1.1 rjs
8863 1.1 rjs if ((two_way == 0) && (not_peer == 1)) {
8864 1.1 rjs /* not a valid request */
8865 1.1 rjs return;
8866 1.1 rjs }
8867 1.1 rjs
8868 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8869 1.1 rjs if (chk == NULL) {
8870 1.1 rjs return;
8871 1.1 rjs }
8872 1.1 rjs sctppcbinfo.ipi_count_chunk++;
8873 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8874 1.1 rjs chk->rec.chunk_id = SCTP_STREAM_RESET;
8875 1.1 rjs chk->asoc = &stcb->asoc;
8876 1.1 rjs chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
8877 1.1 rjs MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8878 1.1 rjs if (chk->data == NULL) {
8879 1.1 rjs strreq_jump_out:
8880 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8881 1.1 rjs sctppcbinfo.ipi_count_chunk--;
8882 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8883 1.1 rjs panic("Chunk count is negative");
8884 1.1 rjs }
8885 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
8886 1.1 rjs return;
8887 1.1 rjs }
8888 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8889 1.1 rjs chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8890 1.1 rjs if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8891 1.1 rjs MCLGET(chk->data, M_DONTWAIT);
8892 1.1 rjs if ((chk->data->m_flags & M_EXT) == 0) {
8893 1.1 rjs /* Give up */
8894 1.1 rjs sctp_m_freem(chk->data);
8895 1.1 rjs chk->data = NULL;
8896 1.1 rjs goto strreq_jump_out;
8897 1.1 rjs }
8898 1.1 rjs chk->data->m_data += SCTP_MIN_OVERHEAD;
8899 1.1 rjs }
8900 1.1 rjs if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8901 1.1 rjs /* can't do it, no room */
8902 1.1 rjs /* Give up */
8903 1.1 rjs sctp_m_freem(chk->data);
8904 1.1 rjs chk->data = NULL;
8905 1.1 rjs goto strreq_jump_out;
8906 1.1 rjs }
8907 1.1 rjs chk->sent = SCTP_DATAGRAM_UNSENT;
8908 1.1 rjs chk->snd_count = 0;
8909 1.1 rjs chk->whoTo = asoc->primary_destination;
8910 1.1 rjs chk->whoTo->ref_count++;
8911 1.1 rjs
8912 1.1 rjs strreq = mtod(chk->data, struct sctp_stream_reset_req *);
8913 1.1 rjs strreq->ch.chunk_type = SCTP_STREAM_RESET;
8914 1.1 rjs strreq->ch.chunk_flags = 0;
8915 1.1 rjs strreq->ch.chunk_length = htons(chk->send_size);
8916 1.1 rjs
8917 1.1 rjs strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
8918 1.1 rjs strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8919 1.1 rjs
8920 1.1 rjs if (chk->send_size % 4) {
8921 1.1 rjs /* need a padding for the end */
8922 1.1 rjs int pad, i;
8923 1.1 rjs uint8_t *end;
8924 1.1 rjs end = (uint8_t *)((vaddr_t)strreq + chk->send_size);
8925 1.1 rjs pad = chk->send_size % 4;
8926 1.1 rjs for (i=0; i<pad; i++) {
8927 1.1 rjs end[i] = 0;
8928 1.1 rjs }
8929 1.1 rjs chk->send_size += pad;
8930 1.1 rjs }
8931 1.1 rjs
8932 1.1 rjs strreq->sr_req.reset_flags = 0;
8933 1.1 rjs if (number_entrys == 0) {
8934 1.1 rjs strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
8935 1.1 rjs }
8936 1.1 rjs if (two_way == 0) {
8937 1.1 rjs strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
8938 1.1 rjs } else {
8939 1.1 rjs if (not_peer == 0) {
8940 1.1 rjs strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
8941 1.1 rjs } else {
8942 1.1 rjs strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
8943 1.1 rjs }
8944 1.1 rjs }
8945 1.1 rjs memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
8946 1.1 rjs strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
8947 1.1 rjs if (number_entrys) {
8948 1.1 rjs /* populate the specific entry's */
8949 1.1 rjs int i;
8950 1.1 rjs for (i=0; i < number_entrys; i++) {
8951 1.1 rjs strreq->sr_req.list_of_streams[i] = htons(list[i]);
8952 1.1 rjs }
8953 1.1 rjs }
8954 1.1 rjs TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8955 1.1 rjs chk,
8956 1.1 rjs sctp_next);
8957 1.1 rjs asoc->ctrl_queue_cnt++;
8958 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
8959 1.1 rjs asoc->stream_reset_outstanding = 1;
8960 1.1 rjs }
8961 1.1 rjs
8962 1.1 rjs void
8963 1.1 rjs sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
8964 1.1 rjs struct mbuf *err_cause)
8965 1.1 rjs {
8966 1.1 rjs /*
8967 1.1 rjs * Formulate the abort message, and send it back down.
8968 1.1 rjs */
8969 1.1 rjs struct mbuf *mout;
8970 1.1 rjs struct sctp_abort_msg *abm;
8971 1.1 rjs struct ip *iph, *iph_out;
8972 1.1 rjs struct ip6_hdr *ip6, *ip6_out;
8973 1.1 rjs int iphlen_out;
8974 1.1 rjs
8975 1.1 rjs /* don't respond to ABORT with ABORT */
8976 1.1 rjs if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
8977 1.1 rjs if (err_cause)
8978 1.1 rjs sctp_m_freem(err_cause);
8979 1.1 rjs return;
8980 1.1 rjs }
8981 1.1 rjs MGETHDR(mout, M_DONTWAIT, MT_HEADER);
8982 1.1 rjs if (mout == NULL) {
8983 1.1 rjs if (err_cause)
8984 1.1 rjs sctp_m_freem(err_cause);
8985 1.1 rjs return;
8986 1.1 rjs }
8987 1.1 rjs iph = mtod(m, struct ip *);
8988 1.1 rjs iph_out = NULL;
8989 1.1 rjs ip6_out = NULL;
8990 1.1 rjs if (iph->ip_v == IPVERSION) {
8991 1.1 rjs iph_out = mtod(mout, struct ip *);
8992 1.1 rjs mout->m_len = sizeof(*iph_out) + sizeof(*abm);
8993 1.1 rjs mout->m_next = err_cause;
8994 1.1 rjs
8995 1.1 rjs /* Fill in the IP header for the ABORT */
8996 1.1 rjs iph_out->ip_v = IPVERSION;
8997 1.1 rjs iph_out->ip_hl = (sizeof(struct ip) / 4);
8998 1.1 rjs iph_out->ip_tos = (u_char)0;
8999 1.1 rjs iph_out->ip_id = 0;
9000 1.1 rjs iph_out->ip_off = 0;
9001 1.1 rjs iph_out->ip_ttl = MAXTTL;
9002 1.1 rjs iph_out->ip_p = IPPROTO_SCTP;
9003 1.1 rjs iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9004 1.1 rjs iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9005 1.1 rjs /* let IP layer calculate this */
9006 1.1 rjs iph_out->ip_sum = 0;
9007 1.1 rjs
9008 1.1 rjs iphlen_out = sizeof(*iph_out);
9009 1.1 rjs abm = (struct sctp_abort_msg *)((vaddr_t)iph_out + iphlen_out);
9010 1.1 rjs } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9011 1.1 rjs ip6 = (struct ip6_hdr *)iph;
9012 1.1 rjs ip6_out = mtod(mout, struct ip6_hdr *);
9013 1.1 rjs mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
9014 1.1 rjs mout->m_next = err_cause;
9015 1.1 rjs
9016 1.1 rjs /* Fill in the IP6 header for the ABORT */
9017 1.1 rjs ip6_out->ip6_flow = ip6->ip6_flow;
9018 1.1 rjs ip6_out->ip6_hlim = ip6_defhlim;
9019 1.1 rjs ip6_out->ip6_nxt = IPPROTO_SCTP;
9020 1.1 rjs ip6_out->ip6_src = ip6->ip6_dst;
9021 1.1 rjs ip6_out->ip6_dst = ip6->ip6_src;
9022 1.1 rjs
9023 1.1 rjs iphlen_out = sizeof(*ip6_out);
9024 1.1 rjs abm = (struct sctp_abort_msg *)((vaddr_t)ip6_out + iphlen_out);
9025 1.1 rjs } else {
9026 1.1 rjs /* Currently not supported */
9027 1.1 rjs return;
9028 1.1 rjs }
9029 1.1 rjs
9030 1.1 rjs abm->sh.src_port = sh->dest_port;
9031 1.1 rjs abm->sh.dest_port = sh->src_port;
9032 1.1 rjs abm->sh.checksum = 0;
9033 1.1 rjs if (vtag == 0) {
9034 1.1 rjs abm->sh.v_tag = sh->v_tag;
9035 1.1 rjs abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
9036 1.1 rjs } else {
9037 1.1 rjs abm->sh.v_tag = htonl(vtag);
9038 1.1 rjs abm->msg.ch.chunk_flags = 0;
9039 1.1 rjs }
9040 1.1 rjs abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9041 1.1 rjs
9042 1.1 rjs if (err_cause) {
9043 1.1 rjs struct mbuf *m_tmp = err_cause;
9044 1.1 rjs int err_len = 0;
9045 1.1 rjs /* get length of the err_cause chain */
9046 1.1 rjs while (m_tmp != NULL) {
9047 1.1 rjs err_len += m_tmp->m_len;
9048 1.1 rjs m_tmp = m_tmp->m_next;
9049 1.1 rjs }
9050 1.1 rjs mout->m_pkthdr.len = mout->m_len + err_len;
9051 1.1 rjs if (err_len % 4) {
9052 1.1 rjs /* need pad at end of chunk */
9053 1.1 rjs u_int32_t cpthis=0;
9054 1.1 rjs int padlen;
9055 1.1 rjs padlen = 4 - (mout->m_pkthdr.len % 4);
9056 1.1 rjs m_copyback(mout, mout->m_pkthdr.len, padlen, (void *)&cpthis);
9057 1.1 rjs }
9058 1.1 rjs abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
9059 1.1 rjs } else {
9060 1.1 rjs mout->m_pkthdr.len = mout->m_len;
9061 1.1 rjs abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
9062 1.1 rjs }
9063 1.1 rjs
9064 1.1 rjs /* add checksum */
9065 1.6 ozaki if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
9066 1.6 ozaki m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
9067 1.1 rjs abm->sh.checksum = 0;
9068 1.1 rjs } else {
9069 1.1 rjs abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
9070 1.1 rjs }
9071 1.1 rjs
9072 1.1 rjs /* zap the rcvif, it should be null */
9073 1.5 ozaki m_reset_rcvif(mout);
9074 1.1 rjs if (iph_out != NULL) {
9075 1.1 rjs struct route ro;
9076 1.1 rjs
9077 1.1 rjs /* zap the stack pointer to the route */
9078 1.1 rjs memset(&ro, 0, sizeof ro);
9079 1.1 rjs #ifdef SCTP_DEBUG
9080 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9081 1.1 rjs printf("sctp_send_abort calling ip_output:\n");
9082 1.1 rjs sctp_print_address_pkt(iph_out, &abm->sh);
9083 1.1 rjs }
9084 1.1 rjs #endif
9085 1.1 rjs /* set IPv4 length */
9086 1.1 rjs iph_out->ip_len = htons(mout->m_pkthdr.len);
9087 1.1 rjs /* out it goes */
9088 1.1 rjs (void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
9089 1.1 rjs } else if (ip6_out != NULL) {
9090 1.1 rjs struct route ro;
9091 1.1 rjs
9092 1.1 rjs /* zap the stack pointer to the route */
9093 1.1 rjs memset(&ro, 0, sizeof(ro));
9094 1.1 rjs #ifdef SCTP_DEBUG
9095 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9096 1.1 rjs printf("sctp_send_abort calling ip6_output:\n");
9097 1.1 rjs sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
9098 1.1 rjs }
9099 1.1 rjs #endif
9100 1.1 rjs ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
9101 1.1 rjs }
9102 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9103 1.1 rjs }
9104 1.1 rjs
9105 1.1 rjs void
9106 1.1 rjs sctp_send_operr_to(struct mbuf *m, int iphlen,
9107 1.1 rjs struct mbuf *scm,
9108 1.1 rjs uint32_t vtag)
9109 1.1 rjs {
9110 1.1 rjs struct sctphdr *ihdr;
9111 1.1 rjs struct sctphdr *ohdr;
9112 1.1 rjs struct sctp_chunkhdr *ophdr;
9113 1.1 rjs
9114 1.1 rjs struct ip *iph;
9115 1.1 rjs #ifdef SCTP_DEBUG
9116 1.1 rjs struct sockaddr_in6 lsa6, fsa6;
9117 1.1 rjs #endif
9118 1.1 rjs uint32_t val;
9119 1.1 rjs iph = mtod(m, struct ip *);
9120 1.1 rjs ihdr = (struct sctphdr *)((vaddr_t)iph + iphlen);
9121 1.1 rjs if (!(scm->m_flags & M_PKTHDR)) {
9122 1.1 rjs /* must be a pkthdr */
9123 1.1 rjs printf("Huh, not a packet header in send_operr\n");
9124 1.1 rjs m_freem(scm);
9125 1.1 rjs return;
9126 1.1 rjs }
9127 1.1 rjs M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
9128 1.1 rjs if (scm == NULL) {
9129 1.1 rjs /* can't send because we can't add a mbuf */
9130 1.1 rjs return;
9131 1.1 rjs }
9132 1.1 rjs ohdr = mtod(scm, struct sctphdr *);
9133 1.1 rjs ohdr->src_port = ihdr->dest_port;
9134 1.1 rjs ohdr->dest_port = ihdr->src_port;
9135 1.1 rjs ohdr->v_tag = vtag;
9136 1.1 rjs ohdr->checksum = 0;
9137 1.1 rjs ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9138 1.1 rjs ophdr->chunk_type = SCTP_OPERATION_ERROR;
9139 1.1 rjs ophdr->chunk_flags = 0;
9140 1.1 rjs ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9141 1.1 rjs if (scm->m_pkthdr.len % 4) {
9142 1.1 rjs /* need padding */
9143 1.1 rjs u_int32_t cpthis=0;
9144 1.1 rjs int padlen;
9145 1.1 rjs padlen = 4 - (scm->m_pkthdr.len % 4);
9146 1.1 rjs m_copyback(scm, scm->m_pkthdr.len, padlen, (void *)&cpthis);
9147 1.1 rjs }
9148 1.6 ozaki if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
9149 1.6 ozaki m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
9150 1.1 rjs val = 0;
9151 1.1 rjs } else {
9152 1.1 rjs val = sctp_calculate_sum(scm, NULL, 0);
9153 1.1 rjs }
9154 1.1 rjs ohdr->checksum = val;
9155 1.1 rjs if (iph->ip_v == IPVERSION) {
9156 1.1 rjs /* V4 */
9157 1.1 rjs struct ip *out;
9158 1.1 rjs struct route ro;
9159 1.1 rjs M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
9160 1.1 rjs if (scm == NULL)
9161 1.1 rjs return;
9162 1.1 rjs memset(&ro, 0, sizeof ro);
9163 1.1 rjs out = mtod(scm, struct ip *);
9164 1.1 rjs out->ip_v = iph->ip_v;
9165 1.1 rjs out->ip_hl = (sizeof(struct ip)/4);
9166 1.1 rjs out->ip_tos = iph->ip_tos;
9167 1.1 rjs out->ip_id = iph->ip_id;
9168 1.1 rjs out->ip_off = 0;
9169 1.1 rjs out->ip_ttl = MAXTTL;
9170 1.1 rjs out->ip_p = IPPROTO_SCTP;
9171 1.1 rjs out->ip_sum = 0;
9172 1.1 rjs out->ip_src = iph->ip_dst;
9173 1.1 rjs out->ip_dst = iph->ip_src;
9174 1.1 rjs out->ip_len = htons(scm->m_pkthdr.len);
9175 1.1 rjs ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
9176 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9177 1.1 rjs } else {
9178 1.1 rjs /* V6 */
9179 1.1 rjs struct route ro;
9180 1.1 rjs struct ip6_hdr *out6, *in6;
9181 1.1 rjs
9182 1.1 rjs M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
9183 1.1 rjs if (scm == NULL)
9184 1.1 rjs return;
9185 1.1 rjs memset(&ro, 0, sizeof ro);
9186 1.1 rjs in6 = mtod(m, struct ip6_hdr *);
9187 1.1 rjs out6 = mtod(scm, struct ip6_hdr *);
9188 1.1 rjs out6->ip6_flow = in6->ip6_flow;
9189 1.1 rjs out6->ip6_hlim = ip6_defhlim;
9190 1.1 rjs out6->ip6_nxt = IPPROTO_SCTP;
9191 1.1 rjs out6->ip6_src = in6->ip6_dst;
9192 1.1 rjs out6->ip6_dst = in6->ip6_src;
9193 1.1 rjs
9194 1.1 rjs #ifdef SCTP_DEBUG
9195 1.1 rjs memset(&lsa6, 0, sizeof(lsa6));
9196 1.1 rjs lsa6.sin6_len = sizeof(lsa6);
9197 1.1 rjs lsa6.sin6_family = AF_INET6;
9198 1.1 rjs lsa6.sin6_addr = out6->ip6_src;
9199 1.1 rjs memset(&fsa6, 0, sizeof(fsa6));
9200 1.1 rjs fsa6.sin6_len = sizeof(fsa6);
9201 1.1 rjs fsa6.sin6_family = AF_INET6;
9202 1.1 rjs fsa6.sin6_addr = out6->ip6_dst;
9203 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9204 1.1 rjs printf("sctp_operr_to calling ipv6 output:\n");
9205 1.1 rjs printf("src: ");
9206 1.1 rjs sctp_print_address((struct sockaddr *)&lsa6);
9207 1.1 rjs printf("dst ");
9208 1.1 rjs sctp_print_address((struct sockaddr *)&fsa6);
9209 1.1 rjs }
9210 1.1 rjs #endif /* SCTP_DEBUG */
9211 1.1 rjs ip6_output(scm, NULL, &ro, 0, NULL, NULL, NULL);
9212 1.1 rjs sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9213 1.1 rjs }
9214 1.1 rjs }
9215 1.1 rjs
9216 1.1 rjs static int
9217 1.1 rjs sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
9218 1.1 rjs {
9219 1.1 rjs int left, cancpy, willcpy, error;
9220 1.1 rjs left = cpsz;
9221 1.1 rjs
9222 1.1 rjs if (m == NULL) {
9223 1.1 rjs /* TSNH */
9224 1.1 rjs *mbcnt = 0;
9225 1.1 rjs return (ENOMEM);
9226 1.1 rjs }
9227 1.1 rjs m->m_len = 0;
9228 1.1 rjs if ((left+resv_upfront) > (int)MHLEN) {
9229 1.1 rjs MCLGET(m, M_WAIT);
9230 1.1 rjs if (m == NULL) {
9231 1.1 rjs *mbcnt = 0;
9232 1.1 rjs return (ENOMEM);
9233 1.1 rjs }
9234 1.1 rjs if ((m->m_flags & M_EXT) == 0) {
9235 1.1 rjs *mbcnt = 0;
9236 1.1 rjs return (ENOMEM);
9237 1.1 rjs }
9238 1.1 rjs *mbcnt += m->m_ext.ext_size;
9239 1.1 rjs }
9240 1.1 rjs *mbcnt += MSIZE;
9241 1.1 rjs cancpy = M_TRAILINGSPACE(m);
9242 1.16 riastrad willcpy = uimin(cancpy, left);
9243 1.1 rjs if ((willcpy + resv_upfront) > cancpy) {
9244 1.1 rjs willcpy -= resv_upfront;
9245 1.1 rjs }
9246 1.1 rjs while (left > 0) {
9247 1.1 rjs /* Align data to the end */
9248 1.1 rjs if ((m->m_flags & M_EXT) == 0) {
9249 1.18 maxv m_align(m, willcpy);
9250 1.1 rjs } else {
9251 1.1 rjs MC_ALIGN(m, willcpy);
9252 1.1 rjs }
9253 1.1 rjs error = uiomove(mtod(m, void *), willcpy, uio);
9254 1.1 rjs if (error) {
9255 1.1 rjs return (error);
9256 1.1 rjs }
9257 1.1 rjs m->m_len = willcpy;
9258 1.1 rjs m->m_nextpkt = 0;
9259 1.1 rjs left -= willcpy;
9260 1.1 rjs if (left > 0) {
9261 1.1 rjs MGET(m->m_next, M_WAIT, MT_DATA);
9262 1.1 rjs if (m->m_next == NULL) {
9263 1.1 rjs *mbcnt = 0;
9264 1.1 rjs return (ENOMEM);
9265 1.1 rjs }
9266 1.1 rjs m = m->m_next;
9267 1.1 rjs m->m_len = 0;
9268 1.1 rjs *mbcnt += MSIZE;
9269 1.1 rjs if (left > (int)MHLEN) {
9270 1.1 rjs MCLGET(m, M_WAIT);
9271 1.1 rjs if (m == NULL) {
9272 1.1 rjs *mbcnt = 0;
9273 1.1 rjs return (ENOMEM);
9274 1.1 rjs }
9275 1.1 rjs if ((m->m_flags & M_EXT) == 0) {
9276 1.1 rjs *mbcnt = 0;
9277 1.1 rjs return (ENOMEM);
9278 1.1 rjs }
9279 1.1 rjs *mbcnt += m->m_ext.ext_size;
9280 1.1 rjs }
9281 1.1 rjs cancpy = M_TRAILINGSPACE(m);
9282 1.16 riastrad willcpy = uimin(cancpy, left);
9283 1.1 rjs }
9284 1.1 rjs }
9285 1.1 rjs return (0);
9286 1.1 rjs }
9287 1.1 rjs
9288 1.1 rjs static int
9289 1.1 rjs sctp_copy_it_in(struct sctp_inpcb *inp,
9290 1.1 rjs struct sctp_tcb *stcb,
9291 1.1 rjs struct sctp_association *asoc,
9292 1.1 rjs struct sctp_nets *net,
9293 1.1 rjs struct sctp_sndrcvinfo *srcv,
9294 1.1 rjs struct uio *uio,
9295 1.1 rjs int flags)
9296 1.1 rjs {
9297 1.1 rjs /* This routine must be very careful in
9298 1.1 rjs * its work. Protocol processing is
9299 1.1 rjs * up and running so care must be taken to
9300 1.1 rjs * spl...() when you need to do something
9301 1.1 rjs * that may effect the stcb/asoc. The sb is
9302 1.1 rjs * locked however. When data is copied the
9303 1.1 rjs * protocol processing should be enabled since
9304 1.1 rjs * this is a slower operation...
9305 1.1 rjs */
9306 1.1 rjs struct socket *so;
9307 1.1 rjs int error = 0;
9308 1.1 rjs int frag_size, mbcnt = 0, mbcnt_e = 0;
9309 1.1 rjs unsigned int sndlen;
9310 1.1 rjs unsigned int tot_demand;
9311 1.1 rjs int tot_out, dataout;
9312 1.1 rjs struct sctp_tmit_chunk *chk;
9313 1.1 rjs struct mbuf *mm;
9314 1.1 rjs struct sctp_stream_out *strq;
9315 1.1 rjs uint32_t my_vtag;
9316 1.1 rjs int resv_in_first;
9317 1.1 rjs
9318 1.1 rjs so = stcb->sctp_socket;
9319 1.1 rjs solock(so);
9320 1.1 rjs chk = NULL;
9321 1.1 rjs mm = NULL;
9322 1.1 rjs
9323 1.1 rjs sndlen = uio->uio_resid;
9324 1.1 rjs /* lock the socket buf */
9325 1.1 rjs error = sblock(&so->so_snd, SBLOCKWAIT(flags));
9326 1.1 rjs if (error)
9327 1.1 rjs goto out_locked;
9328 1.1 rjs
9329 1.1 rjs #ifdef SCTP_DEBUG
9330 1.1 rjs printf("sctp_copy_it_in: %d\n", sndlen);
9331 1.1 rjs #endif
9332 1.1 rjs /* will it ever fit ? */
9333 1.1 rjs if (sndlen > so->so_snd.sb_hiwat) {
9334 1.1 rjs /* It will NEVER fit */
9335 1.1 rjs error = EMSGSIZE;
9336 1.1 rjs goto release;
9337 1.1 rjs }
9338 1.1 rjs /* Do I need to block? */
9339 1.1 rjs if ((so->so_snd.sb_hiwat <
9340 1.1 rjs (sndlen + asoc->total_output_queue_size)) ||
9341 1.1 rjs (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
9342 1.1 rjs (asoc->total_output_mbuf_queue_size >
9343 1.1 rjs so->so_snd.sb_mbmax)
9344 1.1 rjs ) {
9345 1.1 rjs /* prune any prsctp bufs out */
9346 1.1 rjs if (asoc->peer_supports_prsctp) {
9347 1.1 rjs sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9348 1.1 rjs }
9349 1.1 rjs /*
9350 1.1 rjs * We store off a pointer to the endpoint.
9351 1.1 rjs * Since on return from this we must check to
9352 1.1 rjs * see if an so_error is set. If so we may have
9353 1.1 rjs * been reset and our stcb destroyed. Returning
9354 1.1 rjs * an error will flow back to the user...
9355 1.1 rjs */
9356 1.1 rjs while ((so->so_snd.sb_hiwat <
9357 1.1 rjs (sndlen + asoc->total_output_queue_size)) ||
9358 1.1 rjs (asoc->chunks_on_out_queue >
9359 1.1 rjs sctp_max_chunks_on_queue) ||
9360 1.1 rjs (asoc->total_output_mbuf_queue_size >
9361 1.1 rjs so->so_snd.sb_mbmax)
9362 1.1 rjs ) {
9363 1.1 rjs if ((so->so_state & SS_NBIO)
9364 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
9365 1.1 rjs || (flags & MSG_NBIO)
9366 1.1 rjs #endif
9367 1.1 rjs ) {
9368 1.1 rjs /* Non-blocking io in place */
9369 1.1 rjs error = EWOULDBLOCK;
9370 1.1 rjs goto release;
9371 1.1 rjs }
9372 1.1 rjs inp->sctp_tcb_at_block = (void *)stcb;
9373 1.1 rjs inp->error_on_block = 0;
9374 1.1 rjs #ifdef SCTP_BLK_LOGGING
9375 1.1 rjs sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
9376 1.1 rjs so, asoc);
9377 1.1 rjs #endif
9378 1.1 rjs sbunlock(&so->so_snd);
9379 1.1 rjs SCTP_TCB_UNLOCK(stcb);
9380 1.1 rjs error = sbwait(&so->so_snd);
9381 1.1 rjs SCTP_INP_RLOCK(inp);
9382 1.1 rjs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9383 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9384 1.1 rjs /* Should I really unlock ? */
9385 1.1 rjs SCTP_INP_RUNLOCK(inp);
9386 1.1 rjs error = EFAULT;
9387 1.1 rjs goto out_locked;
9388 1.1 rjs }
9389 1.1 rjs SCTP_TCB_LOCK(stcb);
9390 1.1 rjs SCTP_INP_RUNLOCK(inp);
9391 1.1 rjs
9392 1.1 rjs inp->sctp_tcb_at_block = 0;
9393 1.1 rjs #ifdef SCTP_BLK_LOGGING
9394 1.1 rjs sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9395 1.1 rjs so, asoc);
9396 1.1 rjs #endif
9397 1.1 rjs if (inp->error_on_block) {
9398 1.1 rjs /*
9399 1.1 rjs * if our asoc was killed, the free code
9400 1.1 rjs * (in sctp_pcb.c) will save a error in
9401 1.1 rjs * here for us
9402 1.1 rjs */
9403 1.1 rjs error = inp->error_on_block;
9404 1.1 rjs goto out_locked;
9405 1.1 rjs }
9406 1.1 rjs if (error) {
9407 1.1 rjs goto out_locked;
9408 1.1 rjs }
9409 1.1 rjs /* did we encounter a socket error? */
9410 1.1 rjs if (so->so_error) {
9411 1.1 rjs error = so->so_error;
9412 1.1 rjs goto out_locked;
9413 1.1 rjs }
9414 1.1 rjs error = sblock(&so->so_snd, M_WAITOK);
9415 1.1 rjs if (error) {
9416 1.25 andvar /* Can't acquire the lock */
9417 1.1 rjs goto out_locked;
9418 1.1 rjs }
9419 1.1 rjs #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
9420 1.1 rjs if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
9421 1.1 rjs #else
9422 1.1 rjs if (so->so_state & SS_CANTSENDMORE) {
9423 1.1 rjs #endif
9424 1.1 rjs /* The socket is now set not to sendmore.. its gone */
9425 1.1 rjs error = EPIPE;
9426 1.1 rjs goto release;
9427 1.1 rjs }
9428 1.1 rjs if (so->so_error) {
9429 1.1 rjs error = so->so_error;
9430 1.1 rjs goto release;
9431 1.1 rjs }
9432 1.1 rjs if (asoc->peer_supports_prsctp) {
9433 1.1 rjs sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9434 1.1 rjs }
9435 1.1 rjs }
9436 1.1 rjs }
9437 1.1 rjs dataout = tot_out = uio->uio_resid;
9438 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9439 1.1 rjs resv_in_first = SCTP_MED_OVERHEAD;
9440 1.1 rjs } else {
9441 1.1 rjs resv_in_first = SCTP_MED_V4_OVERHEAD;
9442 1.1 rjs }
9443 1.1 rjs
9444 1.1 rjs /* Are we aborting? */
9445 1.12 rjs if (srcv->sinfo_flags & SCTP_ABORT) {
9446 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
9447 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
9448 1.1 rjs /* It has to be up before we abort */
9449 1.1 rjs /* how big is the user initiated abort? */
9450 1.1 rjs
9451 1.1 rjs /* I wonder about doing a MGET without a splnet set.
9452 1.1 rjs * it is done that way in the sosend code so I guess
9453 1.1 rjs * it is ok :-0
9454 1.1 rjs */
9455 1.1 rjs MGETHDR(mm, M_WAIT, MT_DATA);
9456 1.1 rjs if (mm) {
9457 1.1 rjs struct sctp_paramhdr *ph;
9458 1.1 rjs
9459 1.1 rjs tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9460 1.1 rjs if (tot_demand > MHLEN) {
9461 1.1 rjs if (tot_demand > MCLBYTES) {
9462 1.1 rjs /* truncate user data */
9463 1.1 rjs tot_demand = MCLBYTES;
9464 1.1 rjs tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9465 1.1 rjs }
9466 1.1 rjs MCLGET(mm, M_WAIT);
9467 1.1 rjs if ((mm->m_flags & M_EXT) == 0) {
9468 1.1 rjs /* truncate further */
9469 1.1 rjs tot_demand = MHLEN;
9470 1.1 rjs tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9471 1.1 rjs }
9472 1.1 rjs }
9473 1.1 rjs /* now move forward the data pointer */
9474 1.1 rjs ph = mtod(mm, struct sctp_paramhdr *);
9475 1.1 rjs ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9476 1.1 rjs ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9477 1.1 rjs ph++;
9478 1.1 rjs mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9479 1.1 rjs mm->m_len = mm->m_pkthdr.len;
9480 1.1 rjs error = uiomove((void *)ph, (int)tot_out, uio);
9481 1.1 rjs if (error) {
9482 1.1 rjs /*
9483 1.1 rjs * Here if we can't get his data we
9484 1.1 rjs * still abort we just don't get to
9485 1.1 rjs * send the users note :-0
9486 1.1 rjs */
9487 1.1 rjs sctp_m_freem(mm);
9488 1.1 rjs mm = NULL;
9489 1.1 rjs }
9490 1.1 rjs }
9491 1.1 rjs sbunlock(&so->so_snd);
9492 1.1 rjs sctp_abort_an_association(stcb->sctp_ep, stcb,
9493 1.1 rjs SCTP_RESPONSE_TO_USER_REQ,
9494 1.1 rjs mm);
9495 1.1 rjs mm = NULL;
9496 1.1 rjs goto out_locked;
9497 1.1 rjs }
9498 1.1 rjs goto release;
9499 1.1 rjs }
9500 1.1 rjs
9501 1.1 rjs /* Now can we send this? */
9502 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9503 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9504 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9505 1.1 rjs (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9506 1.1 rjs /* got data while shutting down */
9507 1.1 rjs error = ECONNRESET;
9508 1.1 rjs goto release;
9509 1.1 rjs }
9510 1.1 rjs /* Is the stream no. valid? */
9511 1.1 rjs if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9512 1.1 rjs /* Invalid stream number */
9513 1.1 rjs error = EINVAL;
9514 1.1 rjs goto release;
9515 1.1 rjs }
9516 1.1 rjs if (asoc->strmout == NULL) {
9517 1.1 rjs /* huh? software error */
9518 1.1 rjs #ifdef SCTP_DEBUG
9519 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9520 1.1 rjs printf("software error in sctp_copy_it_in\n");
9521 1.1 rjs }
9522 1.1 rjs #endif
9523 1.1 rjs error = EFAULT;
9524 1.1 rjs goto release;
9525 1.1 rjs }
9526 1.12 rjs if ((srcv->sinfo_flags & SCTP_EOF) &&
9527 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
9528 1.1 rjs (tot_out == 0)) {
9529 1.1 rjs sounlock(so);
9530 1.1 rjs goto zap_by_it_now;
9531 1.1 rjs }
9532 1.1 rjs if (tot_out == 0) {
9533 1.1 rjs /* not allowed */
9534 1.1 rjs error = EMSGSIZE;
9535 1.1 rjs goto release;
9536 1.1 rjs }
9537 1.1 rjs /* save off the tag */
9538 1.1 rjs my_vtag = asoc->my_vtag;
9539 1.1 rjs strq = &asoc->strmout[srcv->sinfo_stream];
9540 1.1 rjs /* First lets figure out the "chunking" point */
9541 1.1 rjs frag_size = sctp_get_frag_point(stcb, asoc);
9542 1.1 rjs
9543 1.1 rjs /* two choices here, it all fits in one chunk or
9544 1.1 rjs * we need multiple chunks.
9545 1.1 rjs */
9546 1.1 rjs sounlock(so);
9547 1.1 rjs if (tot_out <= frag_size) {
9548 1.1 rjs /* no need to setup a template */
9549 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9550 1.1 rjs if (chk == NULL) {
9551 1.1 rjs error = ENOMEM;
9552 1.1 rjs goto release;
9553 1.1 rjs }
9554 1.1 rjs sctppcbinfo.ipi_count_chunk++;
9555 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
9556 1.1 rjs asoc->chunks_on_out_queue++;
9557 1.1 rjs MGETHDR(mm, M_WAIT, MT_DATA);
9558 1.1 rjs if (mm == NULL) {
9559 1.1 rjs error = ENOMEM;
9560 1.1 rjs goto clean_up;
9561 1.1 rjs }
9562 1.1 rjs error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
9563 1.1 rjs if (error)
9564 1.1 rjs goto clean_up;
9565 1.1 rjs sctp_prepare_chunk(chk, stcb, srcv, strq, net);
9566 1.1 rjs chk->mbcnt = mbcnt_e;
9567 1.1 rjs mbcnt += mbcnt_e;
9568 1.1 rjs mbcnt_e = 0;
9569 1.1 rjs mm->m_pkthdr.len = tot_out;
9570 1.1 rjs chk->data = mm;
9571 1.1 rjs mm = NULL;
9572 1.1 rjs
9573 1.1 rjs /* the actual chunk flags */
9574 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
9575 1.1 rjs chk->whoTo->ref_count++;
9576 1.1 rjs
9577 1.1 rjs /* fix up the send_size if it is not present */
9578 1.1 rjs chk->send_size = tot_out;
9579 1.1 rjs chk->book_size = chk->send_size;
9580 1.26 andvar /* ok, we are committed */
9581 1.12 rjs if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
9582 1.1 rjs /* bump the ssn if we are unordered. */
9583 1.1 rjs strq->next_sequence_sent++;
9584 1.1 rjs }
9585 1.1 rjs if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9586 1.1 rjs asoc->sent_queue_cnt_removeable++;
9587 1.1 rjs }
9588 1.1 rjs solock(so);
9589 1.1 rjs if ((asoc->state == 0) ||
9590 1.1 rjs (my_vtag != asoc->my_vtag) ||
9591 1.1 rjs (so != inp->sctp_socket) ||
9592 1.1 rjs (inp->sctp_socket == 0)) {
9593 1.1 rjs /* connection was aborted */
9594 1.1 rjs sounlock(so);
9595 1.1 rjs error = ECONNRESET;
9596 1.1 rjs goto clean_up;
9597 1.1 rjs }
9598 1.1 rjs asoc->stream_queue_cnt++;
9599 1.1 rjs TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9600 1.1 rjs /* now check if this stream is on the wheel */
9601 1.1 rjs if ((strq->next_spoke.tqe_next == NULL) &&
9602 1.1 rjs (strq->next_spoke.tqe_prev == NULL)) {
9603 1.1 rjs /* Insert it on the wheel since it is not
9604 1.1 rjs * on it currently
9605 1.1 rjs */
9606 1.1 rjs sctp_insert_on_wheel(asoc, strq);
9607 1.1 rjs }
9608 1.1 rjs sounlock(so);
9609 1.1 rjs clean_up:
9610 1.1 rjs if (error) {
9611 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9612 1.1 rjs sctppcbinfo.ipi_count_chunk--;
9613 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9614 1.1 rjs panic("Chunk count is negative");
9615 1.1 rjs }
9616 1.1 rjs goto release;
9617 1.1 rjs }
9618 1.1 rjs } else {
9619 1.1 rjs /* we need to setup a template */
9620 1.1 rjs struct sctp_tmit_chunk template;
9621 1.1 rjs struct sctpchunk_listhead tmp;
9622 1.1 rjs
9623 1.1 rjs /* setup the template */
9624 1.1 rjs sctp_prepare_chunk(&template, stcb, srcv, strq, net);
9625 1.1 rjs
9626 1.1 rjs /* Prepare the temp list */
9627 1.1 rjs TAILQ_INIT(&tmp);
9628 1.1 rjs
9629 1.1 rjs /* Template is complete, now time for the work */
9630 1.1 rjs while (tot_out > 0) {
9631 1.1 rjs /* Get a chunk */
9632 1.1 rjs chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9633 1.1 rjs if (chk == NULL) {
9634 1.1 rjs /*
9635 1.1 rjs * ok we must spin through and dump anything
9636 1.1 rjs * we have allocated and then jump to the
9637 1.1 rjs * no_membad
9638 1.1 rjs */
9639 1.1 rjs error = ENOMEM;
9640 1.1 rjs }
9641 1.1 rjs sctppcbinfo.ipi_count_chunk++;
9642 1.1 rjs asoc->chunks_on_out_queue++;
9643 1.1 rjs
9644 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
9645 1.1 rjs *chk = template;
9646 1.1 rjs chk->whoTo->ref_count++;
9647 1.1 rjs MGETHDR(chk->data, M_WAIT, MT_DATA);
9648 1.1 rjs if (chk->data == NULL) {
9649 1.1 rjs error = ENOMEM;
9650 1.1 rjs goto temp_clean_up;
9651 1.1 rjs }
9652 1.16 riastrad tot_demand = uimin(tot_out, frag_size);
9653 1.1 rjs error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
9654 1.1 rjs if (error)
9655 1.1 rjs goto temp_clean_up;
9656 1.1 rjs /* now fix the chk->send_size */
9657 1.1 rjs chk->mbcnt = mbcnt_e;
9658 1.1 rjs mbcnt += mbcnt_e;
9659 1.1 rjs mbcnt_e = 0;
9660 1.1 rjs chk->send_size = tot_demand;
9661 1.1 rjs chk->data->m_pkthdr.len = tot_demand;
9662 1.1 rjs chk->book_size = chk->send_size;
9663 1.1 rjs if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9664 1.1 rjs asoc->sent_queue_cnt_removeable++;
9665 1.1 rjs }
9666 1.1 rjs TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
9667 1.1 rjs tot_out -= tot_demand;
9668 1.1 rjs }
9669 1.1 rjs /* Now the tmp list holds all chunks and data */
9670 1.12 rjs if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
9671 1.1 rjs /* bump the ssn if we are unordered. */
9672 1.1 rjs strq->next_sequence_sent++;
9673 1.1 rjs }
9674 1.1 rjs /* Mark the first/last flags. This will
9675 1.1 rjs * result int a 3 for a single item on the list
9676 1.1 rjs */
9677 1.1 rjs chk = TAILQ_FIRST(&tmp);
9678 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
9679 1.1 rjs chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
9680 1.1 rjs chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
9681 1.1 rjs
9682 1.1 rjs /* now move it to the streams actual queue */
9683 1.1 rjs /* first stop protocol processing */
9684 1.1 rjs mutex_enter(softnet_lock);
9685 1.1 rjs if ((asoc->state == 0) ||
9686 1.1 rjs (my_vtag != asoc->my_vtag) ||
9687 1.1 rjs (so != inp->sctp_socket) ||
9688 1.1 rjs (inp->sctp_socket == 0)) {
9689 1.1 rjs /* connection was aborted */
9690 1.1 rjs mutex_exit(softnet_lock);
9691 1.1 rjs error = ECONNRESET;
9692 1.1 rjs goto temp_clean_up;
9693 1.1 rjs }
9694 1.1 rjs chk = TAILQ_FIRST(&tmp);
9695 1.1 rjs while (chk) {
9696 1.1 rjs chk->data->m_nextpkt = 0;
9697 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
9698 1.1 rjs asoc->stream_queue_cnt++;
9699 1.1 rjs TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9700 1.1 rjs chk = TAILQ_FIRST(&tmp);
9701 1.1 rjs }
9702 1.1 rjs /* now check if this stream is on the wheel */
9703 1.1 rjs if ((strq->next_spoke.tqe_next == NULL) &&
9704 1.1 rjs (strq->next_spoke.tqe_prev == NULL)) {
9705 1.1 rjs /* Insert it on the wheel since it is not
9706 1.1 rjs * on it currently
9707 1.1 rjs */
9708 1.1 rjs sctp_insert_on_wheel(asoc, strq);
9709 1.1 rjs }
9710 1.1 rjs /* Ok now we can allow pping */
9711 1.1 rjs mutex_exit(softnet_lock);
9712 1.1 rjs temp_clean_up:
9713 1.1 rjs if (error) {
9714 1.1 rjs chk = TAILQ_FIRST(&tmp);
9715 1.1 rjs while (chk) {
9716 1.1 rjs if (chk->data) {
9717 1.1 rjs sctp_m_freem(chk->data);
9718 1.1 rjs chk->data = NULL;
9719 1.1 rjs }
9720 1.1 rjs TAILQ_REMOVE(&tmp, chk, sctp_next);
9721 1.1 rjs SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9722 1.1 rjs sctppcbinfo.ipi_count_chunk--;
9723 1.1 rjs asoc->chunks_on_out_queue--;
9724 1.1 rjs if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9725 1.1 rjs panic("Chunk count is negative");
9726 1.1 rjs }
9727 1.1 rjs sctppcbinfo.ipi_gencnt_chunk++;
9728 1.1 rjs chk = TAILQ_FIRST(&tmp);
9729 1.1 rjs }
9730 1.1 rjs goto release;
9731 1.1 rjs }
9732 1.1 rjs }
9733 1.1 rjs zap_by_it_now:
9734 1.1 rjs #ifdef SCTP_MBCNT_LOGGING
9735 1.1 rjs sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
9736 1.1 rjs asoc->total_output_queue_size,
9737 1.1 rjs dataout,
9738 1.1 rjs asoc->total_output_mbuf_queue_size,
9739 1.1 rjs mbcnt);
9740 1.1 rjs #endif
9741 1.1 rjs solock(so);
9742 1.1 rjs asoc->total_output_queue_size += dataout;
9743 1.1 rjs asoc->total_output_mbuf_queue_size += mbcnt;
9744 1.1 rjs if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
9745 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
9746 1.1 rjs so->so_snd.sb_cc += dataout;
9747 1.1 rjs so->so_snd.sb_mbcnt += mbcnt;
9748 1.1 rjs }
9749 1.12 rjs if ((srcv->sinfo_flags & SCTP_EOF) &&
9750 1.1 rjs (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
9751 1.1 rjs ) {
9752 1.1 rjs int some_on_streamwheel = 0;
9753 1.1 rjs error = 0;
9754 1.1 rjs if (!TAILQ_EMPTY(&asoc->out_wheel)) {
9755 1.1 rjs /* Check to see if some data queued */
9756 1.1 rjs struct sctp_stream_out *outs;
9757 1.1 rjs TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
9758 1.1 rjs if (!TAILQ_EMPTY(&outs->outqueue)) {
9759 1.1 rjs some_on_streamwheel = 1;
9760 1.1 rjs break;
9761 1.1 rjs }
9762 1.1 rjs }
9763 1.1 rjs }
9764 1.1 rjs if (TAILQ_EMPTY(&asoc->send_queue) &&
9765 1.1 rjs TAILQ_EMPTY(&asoc->sent_queue) &&
9766 1.1 rjs (some_on_streamwheel == 0)) {
9767 1.1 rjs /* there is nothing queued to send, so I'm done... */
9768 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
9769 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
9770 1.1 rjs /* only send SHUTDOWN the first time through */
9771 1.1 rjs #ifdef SCTP_DEBUG
9772 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
9773 1.1 rjs printf("%s:%d sends a shutdown\n",
9774 1.1 rjs __FILE__,
9775 1.1 rjs __LINE__
9776 1.1 rjs );
9777 1.1 rjs }
9778 1.1 rjs #endif
9779 1.1 rjs sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
9780 1.1 rjs asoc->state = SCTP_STATE_SHUTDOWN_SENT;
9781 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
9782 1.1 rjs asoc->primary_destination);
9783 1.1 rjs sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
9784 1.1 rjs asoc->primary_destination);
9785 1.1 rjs }
9786 1.1 rjs } else {
9787 1.1 rjs /*
9788 1.1 rjs * we still got (or just got) data to send, so set
9789 1.1 rjs * SHUTDOWN_PENDING
9790 1.1 rjs */
9791 1.1 rjs /*
9792 1.12 rjs * XXX sockets draft says that SCTP_EOF should be sent
9793 1.1 rjs * with no data. currently, we will allow user data
9794 1.1 rjs * to be sent first and move to SHUTDOWN-PENDING
9795 1.1 rjs */
9796 1.1 rjs asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
9797 1.1 rjs }
9798 1.1 rjs }
9799 1.1 rjs #ifdef SCTP_DEBUG
9800 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9801 1.1 rjs printf("++total out:%d total_mbuf_out:%d\n",
9802 1.1 rjs (int)asoc->total_output_queue_size,
9803 1.1 rjs (int)asoc->total_output_mbuf_queue_size);
9804 1.1 rjs }
9805 1.1 rjs #endif
9806 1.1 rjs
9807 1.1 rjs release:
9808 1.1 rjs sbunlock(&so->so_snd);
9809 1.1 rjs out_locked:
9810 1.1 rjs sounlock(so);
9811 1.1 rjs
9812 1.1 rjs if (mm)
9813 1.1 rjs sctp_m_freem(mm);
9814 1.1 rjs return (error);
9815 1.1 rjs }
9816 1.1 rjs
9817 1.1 rjs
9818 1.1 rjs int
9819 1.1 rjs sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
9820 1.1 rjs struct mbuf *top, struct mbuf *control, int flags, struct lwp *p)
9821 1.1 rjs {
9822 1.1 rjs int error, use_rcvinfo;
9823 1.1 rjs int queue_only = 0, queue_only_for_init=0;
9824 1.1 rjs int un_sent = 0;
9825 1.1 rjs int now_filled=0;
9826 1.1 rjs struct sctp_inpcb *inp;
9827 1.1 rjs struct sctp_tcb *stcb=NULL;
9828 1.1 rjs struct sctp_sndrcvinfo srcv;
9829 1.1 rjs struct timeval now;
9830 1.1 rjs struct sctp_nets *net;
9831 1.1 rjs struct sctp_association *asoc;
9832 1.1 rjs struct sctp_inpcb *t_inp;
9833 1.1 rjs int create_lock_applied = 0;
9834 1.1 rjs
9835 1.1 rjs error = use_rcvinfo = 0;
9836 1.1 rjs net = NULL;
9837 1.1 rjs stcb = NULL;
9838 1.1 rjs asoc = NULL;
9839 1.1 rjs t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
9840 1.1 rjs
9841 1.1 rjs solock(so);
9842 1.1 rjs if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
9843 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
9844 1.1 rjs /* The listner can NOT send */
9845 1.1 rjs error = EFAULT;
9846 1.1 rjs sounlock(so);
9847 1.1 rjs goto out;
9848 1.1 rjs }
9849 1.1 rjs if (addr) {
9850 1.1 rjs SCTP_ASOC_CREATE_LOCK(inp);
9851 1.1 rjs if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9852 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9853 1.1 rjs /* Should I really unlock ? */
9854 1.1 rjs error = EFAULT;
9855 1.1 rjs sounlock(so);
9856 1.1 rjs goto out;
9857 1.1 rjs
9858 1.1 rjs }
9859 1.1 rjs create_lock_applied = 1;
9860 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
9861 1.1 rjs (addr->sa_family == AF_INET6)) {
9862 1.1 rjs error = EINVAL;
9863 1.1 rjs sounlock(so);
9864 1.1 rjs goto out;
9865 1.1 rjs }
9866 1.1 rjs }
9867 1.1 rjs /* now we must find the assoc */
9868 1.1 rjs if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
9869 1.1 rjs SCTP_INP_RLOCK(inp);
9870 1.1 rjs stcb = LIST_FIRST(&inp->sctp_asoc_list);
9871 1.1 rjs if (stcb == NULL) {
9872 1.1 rjs SCTP_INP_RUNLOCK(inp);
9873 1.1 rjs error = ENOTCONN;
9874 1.1 rjs sounlock(so);
9875 1.1 rjs goto out;
9876 1.1 rjs }
9877 1.1 rjs SCTP_TCB_LOCK(stcb);
9878 1.1 rjs SCTP_INP_RUNLOCK(inp);
9879 1.1 rjs net = stcb->asoc.primary_destination;
9880 1.1 rjs }
9881 1.1 rjs #ifdef SCTP_DEBUG
9882 1.1 rjs printf("sctp_sosend: get control\n");
9883 1.1 rjs #endif
9884 1.1 rjs /* get control */
9885 1.1 rjs if (control) {
9886 1.1 rjs /* process cmsg snd/rcv info (maybe a assoc-id) */
9887 1.1 rjs if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
9888 1.1 rjs sizeof(srcv))) {
9889 1.1 rjs /* got one */
9890 1.12 rjs if (srcv.sinfo_flags & SCTP_SENDALL) {
9891 1.1 rjs /* its a sendall */
9892 1.1 rjs sctppcbinfo.mbuf_track--;
9893 1.1 rjs sctp_m_freem(control);
9894 1.1 rjs
9895 1.1 rjs if (create_lock_applied) {
9896 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
9897 1.1 rjs create_lock_applied = 0;
9898 1.1 rjs }
9899 1.1 rjs return (sctp_sendall(inp, uio, top, &srcv));
9900 1.1 rjs }
9901 1.1 rjs use_rcvinfo = 1;
9902 1.1 rjs }
9903 1.1 rjs }
9904 1.1 rjs #ifdef SCTP_DEBUG
9905 1.1 rjs printf("sctp_sosend: doing lookup\n");
9906 1.1 rjs #endif
9907 1.1 rjs if (stcb == NULL) {
9908 1.1 rjs /* Need to do a lookup */
9909 1.1 rjs if (use_rcvinfo && srcv.sinfo_assoc_id) {
9910 1.1 rjs stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
9911 1.1 rjs /*
9912 1.1 rjs * Question: Should I error here if the assoc_id is
9913 1.1 rjs * no longer valid? i.e. I can't find it?
9914 1.1 rjs */
9915 1.1 rjs if ((stcb) &&
9916 1.1 rjs (addr != NULL)) {
9917 1.1 rjs /* Must locate the net structure */
9918 1.1 rjs net = sctp_findnet(stcb, addr);
9919 1.1 rjs }
9920 1.1 rjs }
9921 1.1 rjs if (stcb == NULL) {
9922 1.1 rjs if (addr != NULL) {
9923 1.1 rjs /* Since we did not use findep we must
9924 1.1 rjs * increment it, and if we don't find a
9925 1.1 rjs * tcb decrement it.
9926 1.1 rjs */
9927 1.1 rjs SCTP_INP_WLOCK(inp);
9928 1.1 rjs SCTP_INP_INCR_REF(inp);
9929 1.1 rjs SCTP_INP_WUNLOCK(inp);
9930 1.1 rjs stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
9931 1.1 rjs if (stcb == NULL) {
9932 1.1 rjs SCTP_INP_WLOCK(inp);
9933 1.1 rjs SCTP_INP_DECR_REF(inp);
9934 1.1 rjs SCTP_INP_WUNLOCK(inp);
9935 1.1 rjs }
9936 1.1 rjs }
9937 1.1 rjs }
9938 1.1 rjs }
9939 1.1 rjs if ((stcb == NULL) &&
9940 1.1 rjs (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
9941 1.1 rjs error = ENOTCONN;
9942 1.1 rjs sounlock(so);
9943 1.1 rjs goto out;
9944 1.1 rjs } else if ((stcb == NULL) && (addr == NULL)) {
9945 1.1 rjs error = ENOENT;
9946 1.1 rjs sounlock(so);
9947 1.1 rjs goto out;
9948 1.1 rjs } else if (stcb == NULL) {
9949 1.1 rjs /* UDP style, we must go ahead and start the INIT process */
9950 1.1 rjs if ((use_rcvinfo) &&
9951 1.12 rjs (srcv.sinfo_flags & SCTP_ABORT)) {
9952 1.24 andvar /* User asks to abort a non-existent asoc */
9953 1.1 rjs error = ENOENT;
9954 1.1 rjs sounlock(so);
9955 1.1 rjs goto out;
9956 1.1 rjs }
9957 1.1 rjs /* get an asoc/stcb struct */
9958 1.1 rjs stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
9959 1.1 rjs if (stcb == NULL) {
9960 1.1 rjs /* Error is setup for us in the call */
9961 1.1 rjs sounlock(so);
9962 1.1 rjs goto out;
9963 1.1 rjs }
9964 1.1 rjs if (create_lock_applied) {
9965 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
9966 1.1 rjs create_lock_applied = 0;
9967 1.1 rjs } else {
9968 1.1 rjs printf("Huh-3? create lock should have been on??\n");
9969 1.1 rjs }
9970 1.1 rjs /* Turn on queue only flag to prevent data from being sent */
9971 1.1 rjs queue_only = 1;
9972 1.1 rjs asoc = &stcb->asoc;
9973 1.1 rjs asoc->state = SCTP_STATE_COOKIE_WAIT;
9974 1.1 rjs SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
9975 1.1 rjs if (control) {
9976 1.1 rjs /* see if a init structure exists in cmsg headers */
9977 1.1 rjs struct sctp_initmsg initm;
9978 1.1 rjs int i;
9979 1.1 rjs if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
9980 1.1 rjs /* we have an INIT override of the default */
9981 1.1 rjs if (initm.sinit_max_attempts)
9982 1.1 rjs asoc->max_init_times = initm.sinit_max_attempts;
9983 1.1 rjs if (initm.sinit_num_ostreams)
9984 1.1 rjs asoc->pre_open_streams = initm.sinit_num_ostreams;
9985 1.1 rjs if (initm.sinit_max_instreams)
9986 1.1 rjs asoc->max_inbound_streams = initm.sinit_max_instreams;
9987 1.1 rjs if (initm.sinit_max_init_timeo)
9988 1.1 rjs asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
9989 1.1 rjs if (asoc->streamoutcnt < asoc->pre_open_streams) {
9990 1.1 rjs /* Default is NOT correct */
9991 1.1 rjs #ifdef SCTP_DEBUG
9992 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9993 1.1 rjs printf("Ok, defout:%d pre_open:%d\n",
9994 1.1 rjs asoc->streamoutcnt, asoc->pre_open_streams);
9995 1.1 rjs }
9996 1.1 rjs #endif
9997 1.1 rjs free(asoc->strmout, M_PCB);
9998 1.1 rjs asoc->strmout = NULL;
9999 1.1 rjs asoc->streamoutcnt = asoc->pre_open_streams;
10000 1.1 rjs
10001 1.1 rjs /* What happesn if this fails? .. we panic ...*/
10002 1.1 rjs asoc->strmout = malloc(
10003 1.1 rjs asoc->streamoutcnt *
10004 1.1 rjs sizeof(struct sctp_stream_out),
10005 1.1 rjs M_PCB, M_WAIT);
10006 1.1 rjs for (i = 0; i < asoc->streamoutcnt; i++) {
10007 1.1 rjs /*
10008 1.1 rjs * inbound side must be set to 0xffff,
10009 1.1 rjs * also NOTE when we get the INIT-ACK
10010 1.1 rjs * back (for INIT sender) we MUST
10011 1.1 rjs * reduce the count (streamoutcnt) but
10012 1.1 rjs * first check if we sent to any of the
10013 1.1 rjs * upper streams that were dropped (if
10014 1.1 rjs * some were). Those that were dropped
10015 1.1 rjs * must be notified to the upper layer
10016 1.1 rjs * as failed to send.
10017 1.1 rjs */
10018 1.1 rjs asoc->strmout[i].next_sequence_sent = 0x0;
10019 1.1 rjs TAILQ_INIT(&asoc->strmout[i].outqueue);
10020 1.1 rjs asoc->strmout[i].stream_no = i;
10021 1.1 rjs asoc->strmout[i].next_spoke.tqe_next = 0;
10022 1.1 rjs asoc->strmout[i].next_spoke.tqe_prev = 0;
10023 1.1 rjs }
10024 1.1 rjs }
10025 1.1 rjs }
10026 1.1 rjs
10027 1.1 rjs }
10028 1.1 rjs /* out with the INIT */
10029 1.1 rjs queue_only_for_init = 1;
10030 1.1 rjs sctp_send_initiate(inp, stcb);
10031 1.1 rjs /*
10032 1.1 rjs * we may want to dig in after this call and adjust the MTU
10033 1.1 rjs * value. It defaulted to 1500 (constant) but the ro structure
10034 1.1 rjs * may now have an update and thus we may need to change it
10035 1.1 rjs * BEFORE we append the message.
10036 1.1 rjs */
10037 1.1 rjs net = stcb->asoc.primary_destination;
10038 1.1 rjs asoc = &stcb->asoc;
10039 1.1 rjs } else {
10040 1.1 rjs asoc = &stcb->asoc;
10041 1.1 rjs }
10042 1.1 rjs if (create_lock_applied) {
10043 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
10044 1.1 rjs create_lock_applied = 0;
10045 1.1 rjs }
10046 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10047 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10048 1.1 rjs queue_only = 1;
10049 1.1 rjs }
10050 1.1 rjs if (use_rcvinfo == 0) {
10051 1.1 rjs /* Grab the default stuff from the asoc */
10052 1.1 rjs srcv = stcb->asoc.def_send;
10053 1.1 rjs }
10054 1.1 rjs /* we are now done with all control */
10055 1.1 rjs if (control) {
10056 1.1 rjs sctp_m_freem(control);
10057 1.1 rjs control = NULL;
10058 1.1 rjs }
10059 1.1 rjs
10060 1.1 rjs if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10061 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10062 1.1 rjs (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10063 1.1 rjs (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10064 1.1 rjs if ((use_rcvinfo) &&
10065 1.12 rjs (srcv.sinfo_flags & SCTP_ABORT)) {
10066 1.1 rjs ;
10067 1.1 rjs } else {
10068 1.1 rjs error = ECONNRESET;
10069 1.1 rjs sounlock(so);
10070 1.1 rjs goto out;
10071 1.1 rjs }
10072 1.1 rjs }
10073 1.1 rjs /* Ok, we will attempt a msgsnd :> */
10074 1.1 rjs #if 0 /* XXX */
10075 1.1 rjs if (p)
10076 1.1 rjs p->p_stats->p_ru.ru_msgsnd++;
10077 1.1 rjs #endif
10078 1.1 rjs
10079 1.1 rjs if (stcb) {
10080 1.12 rjs if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
10081 1.1 rjs /* we take the override or the unconfirmed */
10082 1.1 rjs ;
10083 1.1 rjs } else {
10084 1.1 rjs net = stcb->asoc.primary_destination;
10085 1.1 rjs }
10086 1.1 rjs }
10087 1.1 rjs
10088 1.1 rjs #ifdef SCTP_DEBUG
10089 1.1 rjs printf("sctp_sosend: before copying in %p\n", top);
10090 1.1 rjs #endif
10091 1.1 rjs if (top == NULL) {
10092 1.1 rjs /* Must copy it all in from user land. The
10093 1.1 rjs * socket buf is locked but we don't suspend
10094 1.1 rjs * protocol processing until we are ready to
10095 1.1 rjs * send/queue it.
10096 1.1 rjs */
10097 1.1 rjs sounlock(so);
10098 1.1 rjs #ifdef SCTP_DEBUG
10099 1.1 rjs printf("sctp_sosend: before cii\n");
10100 1.1 rjs #endif
10101 1.1 rjs error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
10102 1.1 rjs #ifdef SCTP_DEBUG
10103 1.1 rjs printf("sctp_sosend: after cii\n");
10104 1.1 rjs #endif
10105 1.1 rjs if (error)
10106 1.1 rjs goto out;
10107 1.1 rjs } else {
10108 1.1 rjs /* Here we must either pull in the user data to chunk
10109 1.1 rjs * buffers, or use top to do a msg_append.
10110 1.1 rjs */
10111 1.1 rjs error = sctp_msg_append(stcb, net, top, &srcv, flags);
10112 1.1 rjs sounlock(so);
10113 1.1 rjs if (error)
10114 1.1 rjs goto out;
10115 1.1 rjs /* zap the top since it is now being used */
10116 1.1 rjs top = 0;
10117 1.1 rjs }
10118 1.1 rjs #ifdef SCTP_DEBUG
10119 1.1 rjs printf("sctp_sosend: after copying in\n");
10120 1.1 rjs #endif
10121 1.1 rjs if (net->flight_size > net->cwnd) {
10122 1.1 rjs sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
10123 1.1 rjs queue_only = 1;
10124 1.1 rjs
10125 1.1 rjs } else if (asoc->ifp_had_enobuf) {
10126 1.1 rjs sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
10127 1.1 rjs queue_only = 1;
10128 1.1 rjs } else {
10129 1.1 rjs un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10130 1.1 rjs ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
10131 1.1 rjs SCTP_MED_OVERHEAD);
10132 1.1 rjs
10133 1.1 rjs if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
10134 1.1 rjs (stcb->asoc.total_flight > 0) &&
10135 1.1 rjs (un_sent < (int)stcb->asoc.smallest_mtu)) {
10136 1.1 rjs
10137 1.1 rjs /* Ok, Nagle is set on and we have data outstanding. Don't
10138 1.1 rjs * send anything and let SACKs drive out the data unless we
10139 1.1 rjs * have a "full" segment to send.
10140 1.1 rjs */
10141 1.1 rjs sctp_pegs[SCTP_NAGLE_NOQ]++;
10142 1.1 rjs queue_only = 1;
10143 1.1 rjs } else {
10144 1.1 rjs sctp_pegs[SCTP_NAGLE_OFF]++;
10145 1.1 rjs }
10146 1.1 rjs }
10147 1.1 rjs if (queue_only_for_init) {
10148 1.1 rjs /* It is possible to have a turn around of the
10149 1.1 rjs * INIT/INIT-ACK/COOKIE before I have a chance to
10150 1.1 rjs * copy in the data. In such a case I DO want to
10151 1.1 rjs * send it out by reversing the queue only flag.
10152 1.1 rjs */
10153 1.1 rjs if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
10154 1.1 rjs (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
10155 1.1 rjs /* yep, reverse it */
10156 1.1 rjs queue_only = 0;
10157 1.1 rjs }
10158 1.1 rjs }
10159 1.1 rjs
10160 1.1 rjs #ifdef SCTP_DEBUG
10161 1.1 rjs printf("sctp_sosend: before sending chunk\n");
10162 1.1 rjs #endif
10163 1.1 rjs if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10164 1.1 rjs /* we can attempt to send too.*/
10165 1.1 rjs #ifdef SCTP_DEBUG
10166 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10167 1.1 rjs printf("USR Send calls sctp_chunk_output\n");
10168 1.1 rjs }
10169 1.1 rjs #endif
10170 1.1 rjs solock(so);
10171 1.1 rjs sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
10172 1.1 rjs sctp_chunk_output(inp, stcb, 0);
10173 1.1 rjs sounlock(so);
10174 1.1 rjs } else if ((queue_only == 0) &&
10175 1.1 rjs (stcb->asoc.peers_rwnd == 0) &&
10176 1.1 rjs (stcb->asoc.total_flight == 0)) {
10177 1.1 rjs /* We get to have a probe outstanding */
10178 1.1 rjs solock(so);
10179 1.1 rjs sctp_from_user_send = 1;
10180 1.1 rjs sctp_chunk_output(inp, stcb, 0);
10181 1.1 rjs sctp_from_user_send = 0;
10182 1.1 rjs sounlock(so);
10183 1.1 rjs
10184 1.1 rjs } else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10185 1.1 rjs int num_out, reason, cwnd_full;
10186 1.1 rjs /* Here we do control only */
10187 1.1 rjs solock(so);
10188 1.1 rjs sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10189 1.1 rjs &reason, 1, &cwnd_full, 1, &now, &now_filled);
10190 1.1 rjs sounlock(so);
10191 1.1 rjs }
10192 1.1 rjs #ifdef SCTP_DEBUG
10193 1.1 rjs if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10194 1.1 rjs printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10195 1.1 rjs queue_only, stcb->asoc.peers_rwnd, un_sent,
10196 1.1 rjs stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10197 1.1 rjs stcb->asoc.total_output_queue_size);
10198 1.1 rjs }
10199 1.1 rjs #endif
10200 1.1 rjs out:
10201 1.1 rjs if (create_lock_applied) {
10202 1.1 rjs SCTP_ASOC_CREATE_UNLOCK(inp);
10203 1.1 rjs create_lock_applied = 0;
10204 1.1 rjs }
10205 1.1 rjs if (stcb) {
10206 1.1 rjs SCTP_TCB_UNLOCK(stcb);
10207 1.1 rjs }
10208 1.1 rjs if (top)
10209 1.1 rjs sctp_m_freem(top);
10210 1.1 rjs if (control)
10211 1.1 rjs sctp_m_freem(control);
10212 1.1 rjs return (error);
10213 1.1 rjs }
10214