sctp6_usrreq.c revision 1.14.2.2 1 1.14.2.2 jdolecek /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
2 1.14.2.2 jdolecek /* $NetBSD: sctp6_usrreq.c,v 1.14.2.2 2017/12/03 11:39:05 jdolecek Exp $ */
3 1.14.2.2 jdolecek
4 1.14.2.2 jdolecek /*
5 1.14.2.2 jdolecek * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6 1.14.2.2 jdolecek * All rights reserved.
7 1.14.2.2 jdolecek *
8 1.14.2.2 jdolecek * Redistribution and use in source and binary forms, with or without
9 1.14.2.2 jdolecek * modification, are permitted provided that the following conditions
10 1.14.2.2 jdolecek * are met:
11 1.14.2.2 jdolecek * 1. Redistributions of source code must retain the above copyright
12 1.14.2.2 jdolecek * notice, this list of conditions and the following disclaimer.
13 1.14.2.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
14 1.14.2.2 jdolecek * notice, this list of conditions and the following disclaimer in the
15 1.14.2.2 jdolecek * documentation and/or other materials provided with the distribution.
16 1.14.2.2 jdolecek * 3. All advertising materials mentioning features or use of this software
17 1.14.2.2 jdolecek * must display the following acknowledgement:
18 1.14.2.2 jdolecek * This product includes software developed by Cisco Systems, Inc.
19 1.14.2.2 jdolecek * 4. Neither the name of the project nor the names of its contributors
20 1.14.2.2 jdolecek * may be used to endorse or promote products derived from this software
21 1.14.2.2 jdolecek * without specific prior written permission.
22 1.14.2.2 jdolecek *
23 1.14.2.2 jdolecek * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 1.14.2.2 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.14.2.2 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.14.2.2 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 1.14.2.2 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.14.2.2 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.14.2.2 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.14.2.2 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.14.2.2 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.14.2.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.14.2.2 jdolecek * SUCH DAMAGE.
34 1.14.2.2 jdolecek */
35 1.14.2.2 jdolecek #include <sys/cdefs.h>
36 1.14.2.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: sctp6_usrreq.c,v 1.14.2.2 2017/12/03 11:39:05 jdolecek Exp $");
37 1.14.2.2 jdolecek
38 1.14.2.2 jdolecek #ifdef _KERNEL_OPT
39 1.14.2.2 jdolecek #include "opt_inet.h"
40 1.14.2.2 jdolecek #include "opt_ipsec.h"
41 1.14.2.2 jdolecek #include "opt_sctp.h"
42 1.14.2.2 jdolecek #include "opt_net_mpsafe.h"
43 1.14.2.2 jdolecek #endif /* _KERNEL_OPT */
44 1.14.2.2 jdolecek
45 1.14.2.2 jdolecek #include <sys/param.h>
46 1.14.2.2 jdolecek #include <sys/kernel.h>
47 1.14.2.2 jdolecek #include <sys/mbuf.h>
48 1.14.2.2 jdolecek #include <sys/domain.h>
49 1.14.2.2 jdolecek #include <sys/protosw.h>
50 1.14.2.2 jdolecek #include <sys/socket.h>
51 1.14.2.2 jdolecek #include <sys/malloc.h>
52 1.14.2.2 jdolecek #include <sys/socketvar.h>
53 1.14.2.2 jdolecek #include <sys/sysctl.h>
54 1.14.2.2 jdolecek #include <sys/errno.h>
55 1.14.2.2 jdolecek #include <sys/stat.h>
56 1.14.2.2 jdolecek #include <sys/systm.h>
57 1.14.2.2 jdolecek #include <sys/syslog.h>
58 1.14.2.2 jdolecek #include <sys/proc.h>
59 1.14.2.2 jdolecek #include <net/if.h>
60 1.14.2.2 jdolecek #include <net/route.h>
61 1.14.2.2 jdolecek #include <net/if_types.h>
62 1.14.2.2 jdolecek #include <netinet/in.h>
63 1.14.2.2 jdolecek #include <netinet/in_systm.h>
64 1.14.2.2 jdolecek #include <netinet/ip.h>
65 1.14.2.2 jdolecek #include <netinet/in_pcb.h>
66 1.14.2.2 jdolecek #include <netinet/in_var.h>
67 1.14.2.2 jdolecek #include <netinet/ip_var.h>
68 1.14.2.2 jdolecek #include <netinet/sctp_pcb.h>
69 1.14.2.2 jdolecek #include <netinet/sctp_header.h>
70 1.14.2.2 jdolecek #include <netinet/sctp_var.h>
71 1.14.2.2 jdolecek #include <netinet/sctputil.h>
72 1.14.2.2 jdolecek #include <netinet/sctp_output.h>
73 1.14.2.2 jdolecek #include <netinet/sctp_input.h>
74 1.14.2.2 jdolecek #include <netinet/sctp_asconf.h>
75 1.14.2.2 jdolecek #include <netinet6/ip6_var.h>
76 1.14.2.2 jdolecek #include <netinet6/scope6_var.h>
77 1.14.2.2 jdolecek #include <netinet/ip6.h>
78 1.14.2.2 jdolecek #include <netinet6/in6_pcb.h>
79 1.14.2.2 jdolecek #include <netinet/icmp6.h>
80 1.14.2.2 jdolecek #include <netinet6/sctp6_var.h>
81 1.14.2.2 jdolecek #include <netinet6/ip6protosw.h>
82 1.14.2.2 jdolecek
83 1.14.2.2 jdolecek #ifdef IPSEC
84 1.14.2.2 jdolecek #include <netipsec/ipsec.h>
85 1.14.2.2 jdolecek #include <netipsec/ipsec6.h>
86 1.14.2.2 jdolecek #endif /*IPSEC*/
87 1.14.2.2 jdolecek
88 1.14.2.2 jdolecek #if defined(NFAITH) && NFAITH > 0
89 1.14.2.2 jdolecek #include <net/if_faith.h>
90 1.14.2.2 jdolecek #endif
91 1.14.2.2 jdolecek
92 1.14.2.2 jdolecek #include <net/net_osdep.h>
93 1.14.2.2 jdolecek
94 1.14.2.2 jdolecek #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
95 1.14.2.2 jdolecek #ifndef in6pcb
96 1.14.2.2 jdolecek #define in6pcb inpcb
97 1.14.2.2 jdolecek #endif
98 1.14.2.2 jdolecek #ifndef sotoin6pcb
99 1.14.2.2 jdolecek #define sotoin6pcb sotoinpcb
100 1.14.2.2 jdolecek #endif
101 1.14.2.2 jdolecek #endif
102 1.14.2.2 jdolecek
103 1.14.2.2 jdolecek #ifdef SCTP_DEBUG
104 1.14.2.2 jdolecek extern u_int32_t sctp_debug_on;
105 1.14.2.2 jdolecek #endif
106 1.14.2.2 jdolecek
107 1.14.2.2 jdolecek static int sctp6_detach(struct socket *so);
108 1.14.2.2 jdolecek
109 1.14.2.2 jdolecek extern int sctp_no_csum_on_loopback;
110 1.14.2.2 jdolecek
111 1.14.2.2 jdolecek int
112 1.14.2.2 jdolecek sctp6_input(struct mbuf **mp, int *offp, int proto)
113 1.14.2.2 jdolecek {
114 1.14.2.2 jdolecek struct mbuf *m = *mp;
115 1.14.2.2 jdolecek struct ip6_hdr *ip6;
116 1.14.2.2 jdolecek struct sctphdr *sh;
117 1.14.2.2 jdolecek struct sctp_inpcb *in6p = NULL;
118 1.14.2.2 jdolecek struct sctp_nets *net;
119 1.14.2.2 jdolecek int refcount_up = 0;
120 1.14.2.2 jdolecek u_int32_t check, calc_check;
121 1.14.2.2 jdolecek struct inpcb *in6p_ip;
122 1.14.2.2 jdolecek struct sctp_chunkhdr *ch;
123 1.14.2.2 jdolecek struct mbuf *opts = NULL;
124 1.14.2.2 jdolecek int length, mlen, offset, iphlen;
125 1.14.2.2 jdolecek u_int8_t ecn_bits;
126 1.14.2.2 jdolecek struct sctp_tcb *stcb = NULL;
127 1.14.2.2 jdolecek int off = *offp;
128 1.14.2.2 jdolecek int s;
129 1.14.2.2 jdolecek
130 1.14.2.2 jdolecek ip6 = mtod(m, struct ip6_hdr *);
131 1.14.2.2 jdolecek /* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
132 1.14.2.2 jdolecek IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
133 1.14.2.2 jdolecek if (sh == NULL) {
134 1.14.2.2 jdolecek sctp_pegs[SCTP_HDR_DROPS]++;
135 1.14.2.2 jdolecek return IPPROTO_DONE;
136 1.14.2.2 jdolecek }
137 1.14.2.2 jdolecek ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(struct sctphdr));
138 1.14.2.2 jdolecek
139 1.14.2.2 jdolecek iphlen = off;
140 1.14.2.2 jdolecek offset = iphlen + sizeof(*sh) + sizeof(*ch);
141 1.14.2.2 jdolecek
142 1.14.2.2 jdolecek #if defined(NFAITH) && NFAITH > 0
143 1.14.2.2 jdolecek if (faithprefix(&ip6->ip6_dst))
144 1.14.2.2 jdolecek goto bad;
145 1.14.2.2 jdolecek #endif /* NFAITH defined and > 0 */
146 1.14.2.2 jdolecek sctp_pegs[SCTP_INPKTS]++;
147 1.14.2.2 jdolecek #ifdef SCTP_DEBUG
148 1.14.2.2 jdolecek if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
149 1.14.2.2 jdolecek printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
150 1.14.2.2 jdolecek }
151 1.14.2.2 jdolecek #endif
152 1.14.2.2 jdolecek if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
153 1.14.2.2 jdolecek /* No multi-cast support in SCTP */
154 1.14.2.2 jdolecek sctp_pegs[SCTP_IN_MCAST]++;
155 1.14.2.2 jdolecek goto bad;
156 1.14.2.2 jdolecek }
157 1.14.2.2 jdolecek /* destination port of 0 is illegal, based on RFC2960. */
158 1.14.2.2 jdolecek if (sh->dest_port == 0)
159 1.14.2.2 jdolecek goto bad;
160 1.14.2.2 jdolecek if ((sctp_no_csum_on_loopback == 0) ||
161 1.14.2.2 jdolecek (m_get_rcvif_NOMPSAFE(m) == NULL) ||
162 1.14.2.2 jdolecek (m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
163 1.14.2.2 jdolecek /* we do NOT validate things from the loopback if the
164 1.14.2.2 jdolecek * sysctl is set to 1.
165 1.14.2.2 jdolecek */
166 1.14.2.2 jdolecek check = sh->checksum; /* save incoming checksum */
167 1.14.2.2 jdolecek if ((check == 0) && (sctp_no_csum_on_loopback)) {
168 1.14.2.2 jdolecek /* special hook for where we got a local address
169 1.14.2.2 jdolecek * somehow routed across a non IFT_LOOP type interface
170 1.14.2.2 jdolecek */
171 1.14.2.2 jdolecek if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
172 1.14.2.2 jdolecek goto sctp_skip_csum;
173 1.14.2.2 jdolecek }
174 1.14.2.2 jdolecek sh->checksum = 0; /* prepare for calc */
175 1.14.2.2 jdolecek calc_check = sctp_calculate_sum(m, &mlen, iphlen);
176 1.14.2.2 jdolecek if (calc_check != check) {
177 1.14.2.2 jdolecek #ifdef SCTP_DEBUG
178 1.14.2.2 jdolecek if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
179 1.14.2.2 jdolecek printf("Bad CSUM on SCTP packet calc_check:%x check:%x m:%p mlen:%d iphlen:%d\n",
180 1.14.2.2 jdolecek calc_check, check, m,
181 1.14.2.2 jdolecek mlen, iphlen);
182 1.14.2.2 jdolecek }
183 1.14.2.2 jdolecek #endif
184 1.14.2.2 jdolecek stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
185 1.14.2.2 jdolecek sh, ch, &in6p, &net);
186 1.14.2.2 jdolecek /* in6p's ref-count increased && stcb locked */
187 1.14.2.2 jdolecek if ((in6p) && (stcb)) {
188 1.14.2.2 jdolecek sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
189 1.14.2.2 jdolecek sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
190 1.14.2.2 jdolecek } else if ((in6p != NULL) && (stcb == NULL)) {
191 1.14.2.2 jdolecek refcount_up = 1;
192 1.14.2.2 jdolecek }
193 1.14.2.2 jdolecek sctp_pegs[SCTP_BAD_CSUM]++;
194 1.14.2.2 jdolecek goto bad;
195 1.14.2.2 jdolecek }
196 1.14.2.2 jdolecek sh->checksum = calc_check;
197 1.14.2.2 jdolecek } else {
198 1.14.2.2 jdolecek sctp_skip_csum:
199 1.14.2.2 jdolecek mlen = m->m_pkthdr.len;
200 1.14.2.2 jdolecek }
201 1.14.2.2 jdolecek net = NULL;
202 1.14.2.2 jdolecek /*
203 1.14.2.2 jdolecek * Locate pcb and tcb for datagram
204 1.14.2.2 jdolecek * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
205 1.14.2.2 jdolecek */
206 1.14.2.2 jdolecek #ifdef SCTP_DEBUG
207 1.14.2.2 jdolecek if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
208 1.14.2.2 jdolecek printf("V6 Find the association\n");
209 1.14.2.2 jdolecek }
210 1.14.2.2 jdolecek #endif
211 1.14.2.2 jdolecek stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
212 1.14.2.2 jdolecek sh, ch, &in6p, &net);
213 1.14.2.2 jdolecek /* in6p's ref-count increased */
214 1.14.2.2 jdolecek if (in6p == NULL) {
215 1.14.2.2 jdolecek struct sctp_init_chunk *init_chk, chunk_buf;
216 1.14.2.2 jdolecek
217 1.14.2.2 jdolecek sctp_pegs[SCTP_NOPORTS]++;
218 1.14.2.2 jdolecek if (ch->chunk_type == SCTP_INITIATION) {
219 1.14.2.2 jdolecek /* we do a trick here to get the INIT tag,
220 1.14.2.2 jdolecek * dig in and get the tag from the INIT and
221 1.14.2.2 jdolecek * put it in the common header.
222 1.14.2.2 jdolecek */
223 1.14.2.2 jdolecek init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
224 1.14.2.2 jdolecek iphlen + sizeof(*sh), sizeof(*init_chk),
225 1.14.2.2 jdolecek (u_int8_t *)&chunk_buf);
226 1.14.2.2 jdolecek sh->v_tag = init_chk->init.initiate_tag;
227 1.14.2.2 jdolecek }
228 1.14.2.2 jdolecek sctp_send_abort(m, iphlen, sh, 0, NULL);
229 1.14.2.2 jdolecek goto bad;
230 1.14.2.2 jdolecek } else if (stcb == NULL) {
231 1.14.2.2 jdolecek refcount_up = 1;
232 1.14.2.2 jdolecek }
233 1.14.2.2 jdolecek in6p_ip = (struct inpcb *)in6p;
234 1.14.2.2 jdolecek #ifdef IPSEC
235 1.14.2.2 jdolecek /*
236 1.14.2.2 jdolecek * Check AH/ESP integrity.
237 1.14.2.2 jdolecek */
238 1.14.2.2 jdolecek if (ipsec_used && ipsec6_in_reject(m, (struct in6pcb *)in6p_ip)) {
239 1.14.2.2 jdolecek /* XXX */
240 1.14.2.2 jdolecek #if 0
241 1.14.2.2 jdolecek /* FIX ME: need to find right stat */
242 1.14.2.2 jdolecek ipsec6stat.in_polvio++;
243 1.14.2.2 jdolecek #endif
244 1.14.2.2 jdolecek goto bad;
245 1.14.2.2 jdolecek }
246 1.14.2.2 jdolecek #endif /*IPSEC*/
247 1.14.2.2 jdolecek
248 1.14.2.2 jdolecek /*
249 1.14.2.2 jdolecek * Construct sockaddr format source address.
250 1.14.2.2 jdolecek * Stuff source address and datagram in user buffer.
251 1.14.2.2 jdolecek */
252 1.14.2.2 jdolecek if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
253 1.14.2.2 jdolecek #ifndef __OpenBSD__
254 1.14.2.2 jdolecek || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
255 1.14.2.2 jdolecek #endif
256 1.14.2.2 jdolecek ) {
257 1.14.2.2 jdolecek #if defined(__FreeBSD__) || defined(__APPLE__)
258 1.14.2.2 jdolecek #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || defined(__APPLE__)
259 1.14.2.2 jdolecek ip6_savecontrol(in6p_ip, &opts, ip6, m);
260 1.14.2.2 jdolecek #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
261 1.14.2.2 jdolecek ip6_savecontrol(in6p_ip, m, &opts);
262 1.14.2.2 jdolecek #else
263 1.14.2.2 jdolecek ip6_savecontrol(in6p_ip, m, &opts, NULL);
264 1.14.2.2 jdolecek #endif
265 1.14.2.2 jdolecek #elif defined(__NetBSD__)
266 1.14.2.2 jdolecek ip6_savecontrol((struct in6pcb *)in6p_ip, &opts, ip6, m);
267 1.14.2.2 jdolecek #else
268 1.14.2.2 jdolecek ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
269 1.14.2.2 jdolecek #endif
270 1.14.2.2 jdolecek }
271 1.14.2.2 jdolecek
272 1.14.2.2 jdolecek /*
273 1.14.2.2 jdolecek * CONTROL chunk processing
274 1.14.2.2 jdolecek */
275 1.14.2.2 jdolecek length = ntohs(ip6->ip6_plen) + iphlen;
276 1.14.2.2 jdolecek offset -= sizeof(*ch);
277 1.14.2.2 jdolecek ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
278 1.14.2.2 jdolecek s = splsoftnet();
279 1.14.2.2 jdolecek (void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
280 1.14.2.2 jdolecek in6p, stcb, net, ecn_bits);
281 1.14.2.2 jdolecek /* inp's ref-count reduced && stcb unlocked */
282 1.14.2.2 jdolecek splx(s);
283 1.14.2.2 jdolecek /* XXX this stuff below gets moved to appropriate parts later... */
284 1.14.2.2 jdolecek if (m)
285 1.14.2.2 jdolecek m_freem(m);
286 1.14.2.2 jdolecek if (opts)
287 1.14.2.2 jdolecek m_freem(opts);
288 1.14.2.2 jdolecek
289 1.14.2.2 jdolecek if ((in6p) && refcount_up){
290 1.14.2.2 jdolecek /* reduce ref-count */
291 1.14.2.2 jdolecek SCTP_INP_WLOCK(in6p);
292 1.14.2.2 jdolecek SCTP_INP_DECR_REF(in6p);
293 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(in6p);
294 1.14.2.2 jdolecek }
295 1.14.2.2 jdolecek
296 1.14.2.2 jdolecek return IPPROTO_DONE;
297 1.14.2.2 jdolecek
298 1.14.2.2 jdolecek bad:
299 1.14.2.2 jdolecek if (stcb) {
300 1.14.2.2 jdolecek SCTP_TCB_UNLOCK(stcb);
301 1.14.2.2 jdolecek }
302 1.14.2.2 jdolecek
303 1.14.2.2 jdolecek if ((in6p) && refcount_up){
304 1.14.2.2 jdolecek /* reduce ref-count */
305 1.14.2.2 jdolecek SCTP_INP_WLOCK(in6p);
306 1.14.2.2 jdolecek SCTP_INP_DECR_REF(in6p);
307 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(in6p);
308 1.14.2.2 jdolecek }
309 1.14.2.2 jdolecek if (m) {
310 1.14.2.2 jdolecek m_freem(m);
311 1.14.2.2 jdolecek }
312 1.14.2.2 jdolecek if (opts) {
313 1.14.2.2 jdolecek m_freem(opts);
314 1.14.2.2 jdolecek }
315 1.14.2.2 jdolecek return IPPROTO_DONE;
316 1.14.2.2 jdolecek }
317 1.14.2.2 jdolecek
318 1.14.2.2 jdolecek
319 1.14.2.2 jdolecek static void
320 1.14.2.2 jdolecek sctp6_notify_mbuf(struct sctp_inpcb *inp,
321 1.14.2.2 jdolecek struct icmp6_hdr *icmp6,
322 1.14.2.2 jdolecek struct sctphdr *sh,
323 1.14.2.2 jdolecek struct sctp_tcb *stcb,
324 1.14.2.2 jdolecek struct sctp_nets *net)
325 1.14.2.2 jdolecek {
326 1.14.2.2 jdolecek unsigned int nxtsz;
327 1.14.2.2 jdolecek
328 1.14.2.2 jdolecek if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
329 1.14.2.2 jdolecek (icmp6 == NULL) || (sh == NULL)) {
330 1.14.2.2 jdolecek goto out;
331 1.14.2.2 jdolecek }
332 1.14.2.2 jdolecek
333 1.14.2.2 jdolecek /* First do we even look at it? */
334 1.14.2.2 jdolecek if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
335 1.14.2.2 jdolecek goto out;
336 1.14.2.2 jdolecek
337 1.14.2.2 jdolecek if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
338 1.14.2.2 jdolecek /* not PACKET TO BIG */
339 1.14.2.2 jdolecek goto out;
340 1.14.2.2 jdolecek }
341 1.14.2.2 jdolecek /*
342 1.14.2.2 jdolecek * ok we need to look closely. We could even get smarter and
343 1.14.2.2 jdolecek * look at anyone that we sent to in case we get a different
344 1.14.2.2 jdolecek * ICMP that tells us there is no way to reach a host, but for
345 1.14.2.2 jdolecek * this impl, all we care about is MTU discovery.
346 1.14.2.2 jdolecek */
347 1.14.2.2 jdolecek nxtsz = ntohl(icmp6->icmp6_mtu);
348 1.14.2.2 jdolecek /* Stop any PMTU timer */
349 1.14.2.2 jdolecek sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
350 1.14.2.2 jdolecek
351 1.14.2.2 jdolecek /* Adjust destination size limit */
352 1.14.2.2 jdolecek if (net->mtu > nxtsz) {
353 1.14.2.2 jdolecek net->mtu = nxtsz;
354 1.14.2.2 jdolecek }
355 1.14.2.2 jdolecek /* now what about the ep? */
356 1.14.2.2 jdolecek if (stcb->asoc.smallest_mtu > nxtsz) {
357 1.14.2.2 jdolecek struct sctp_tmit_chunk *chk;
358 1.14.2.2 jdolecek struct sctp_stream_out *strm;
359 1.14.2.2 jdolecek /* Adjust that too */
360 1.14.2.2 jdolecek stcb->asoc.smallest_mtu = nxtsz;
361 1.14.2.2 jdolecek /* now off to subtract IP_DF flag if needed */
362 1.14.2.2 jdolecek
363 1.14.2.2 jdolecek TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
364 1.14.2.2 jdolecek if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
365 1.14.2.2 jdolecek chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
366 1.14.2.2 jdolecek }
367 1.14.2.2 jdolecek }
368 1.14.2.2 jdolecek TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
369 1.14.2.2 jdolecek if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
370 1.14.2.2 jdolecek /*
371 1.14.2.2 jdolecek * For this guy we also mark for immediate
372 1.14.2.2 jdolecek * resend since we sent to big of chunk
373 1.14.2.2 jdolecek */
374 1.14.2.2 jdolecek chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
375 1.14.2.2 jdolecek if (chk->sent != SCTP_DATAGRAM_RESEND)
376 1.14.2.2 jdolecek stcb->asoc.sent_queue_retran_cnt++;
377 1.14.2.2 jdolecek chk->sent = SCTP_DATAGRAM_RESEND;
378 1.14.2.2 jdolecek chk->rec.data.doing_fast_retransmit = 0;
379 1.14.2.2 jdolecek
380 1.14.2.2 jdolecek chk->sent = SCTP_DATAGRAM_RESEND;
381 1.14.2.2 jdolecek /* Clear any time so NO RTT is being done */
382 1.14.2.2 jdolecek chk->sent_rcv_time.tv_sec = 0;
383 1.14.2.2 jdolecek chk->sent_rcv_time.tv_usec = 0;
384 1.14.2.2 jdolecek stcb->asoc.total_flight -= chk->send_size;
385 1.14.2.2 jdolecek net->flight_size -= chk->send_size;
386 1.14.2.2 jdolecek }
387 1.14.2.2 jdolecek }
388 1.14.2.2 jdolecek TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
389 1.14.2.2 jdolecek TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
390 1.14.2.2 jdolecek if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
391 1.14.2.2 jdolecek chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
392 1.14.2.2 jdolecek }
393 1.14.2.2 jdolecek }
394 1.14.2.2 jdolecek }
395 1.14.2.2 jdolecek }
396 1.14.2.2 jdolecek sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
397 1.14.2.2 jdolecek out:
398 1.14.2.2 jdolecek if (inp) {
399 1.14.2.2 jdolecek /* reduce inp's ref-count */
400 1.14.2.2 jdolecek SCTP_INP_WLOCK(inp);
401 1.14.2.2 jdolecek SCTP_INP_DECR_REF(inp);
402 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(inp);
403 1.14.2.2 jdolecek }
404 1.14.2.2 jdolecek if (stcb) {
405 1.14.2.2 jdolecek SCTP_TCB_UNLOCK(stcb);
406 1.14.2.2 jdolecek }
407 1.14.2.2 jdolecek }
408 1.14.2.2 jdolecek
409 1.14.2.2 jdolecek
410 1.14.2.2 jdolecek void *
411 1.14.2.2 jdolecek sctp6_ctlinput(int cmd, const struct sockaddr *pktdst, void *d)
412 1.14.2.2 jdolecek {
413 1.14.2.2 jdolecek struct sctphdr sh;
414 1.14.2.2 jdolecek struct ip6ctlparam *ip6cp = NULL;
415 1.14.2.2 jdolecek int s, cm;
416 1.14.2.2 jdolecek
417 1.14.2.2 jdolecek if (pktdst->sa_family != AF_INET6 ||
418 1.14.2.2 jdolecek pktdst->sa_len != sizeof(struct sockaddr_in6))
419 1.14.2.2 jdolecek return NULL;
420 1.14.2.2 jdolecek
421 1.14.2.2 jdolecek if ((unsigned)cmd >= PRC_NCMDS)
422 1.14.2.2 jdolecek return NULL;
423 1.14.2.2 jdolecek if (PRC_IS_REDIRECT(cmd)) {
424 1.14.2.2 jdolecek d = NULL;
425 1.14.2.2 jdolecek } else if (inet6ctlerrmap[cmd] == 0) {
426 1.14.2.2 jdolecek return NULL;
427 1.14.2.2 jdolecek }
428 1.14.2.2 jdolecek
429 1.14.2.2 jdolecek /* if the parameter is from icmp6, decode it. */
430 1.14.2.2 jdolecek if (d != NULL) {
431 1.14.2.2 jdolecek ip6cp = (struct ip6ctlparam *)d;
432 1.14.2.2 jdolecek } else {
433 1.14.2.2 jdolecek ip6cp = (struct ip6ctlparam *)NULL;
434 1.14.2.2 jdolecek }
435 1.14.2.2 jdolecek
436 1.14.2.2 jdolecek if (ip6cp) {
437 1.14.2.2 jdolecek /*
438 1.14.2.2 jdolecek * XXX: We assume that when IPV6 is non NULL,
439 1.14.2.2 jdolecek * M and OFF are valid.
440 1.14.2.2 jdolecek */
441 1.14.2.2 jdolecek /* check if we can safely examine src and dst ports */
442 1.14.2.2 jdolecek struct sctp_inpcb *inp;
443 1.14.2.2 jdolecek struct sctp_tcb *stcb;
444 1.14.2.2 jdolecek struct sctp_nets *net;
445 1.14.2.2 jdolecek struct sockaddr_in6 final;
446 1.14.2.2 jdolecek
447 1.14.2.2 jdolecek if (ip6cp->ip6c_m == NULL ||
448 1.14.2.2 jdolecek (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
449 1.14.2.2 jdolecek return NULL;
450 1.14.2.2 jdolecek
451 1.14.2.2 jdolecek memset(&sh, 0, sizeof(sh));
452 1.14.2.2 jdolecek memset(&final, 0, sizeof(final));
453 1.14.2.2 jdolecek inp = NULL;
454 1.14.2.2 jdolecek net = NULL;
455 1.14.2.2 jdolecek m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
456 1.14.2.2 jdolecek (void *)&sh);
457 1.14.2.2 jdolecek ip6cp->ip6c_src->sin6_port = sh.src_port;
458 1.14.2.2 jdolecek final.sin6_len = sizeof(final);
459 1.14.2.2 jdolecek final.sin6_family = AF_INET6;
460 1.14.2.2 jdolecek final.sin6_addr = ((const struct sockaddr_in6 *)pktdst)->sin6_addr;
461 1.14.2.2 jdolecek final.sin6_port = sh.dest_port;
462 1.14.2.2 jdolecek s = splsoftnet();
463 1.14.2.2 jdolecek stcb = sctp_findassociation_addr_sa(sin6tosa(ip6cp->ip6c_src),
464 1.14.2.2 jdolecek sin6tosa(&final),
465 1.14.2.2 jdolecek &inp, &net, 1);
466 1.14.2.2 jdolecek /* inp's ref-count increased && stcb locked */
467 1.14.2.2 jdolecek if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
468 1.14.2.2 jdolecek if (cmd == PRC_MSGSIZE) {
469 1.14.2.2 jdolecek sctp6_notify_mbuf(inp,
470 1.14.2.2 jdolecek ip6cp->ip6c_icmp6,
471 1.14.2.2 jdolecek &sh,
472 1.14.2.2 jdolecek stcb,
473 1.14.2.2 jdolecek net);
474 1.14.2.2 jdolecek /* inp's ref-count reduced && stcb unlocked */
475 1.14.2.2 jdolecek } else {
476 1.14.2.2 jdolecek if (cmd == PRC_HOSTDEAD) {
477 1.14.2.2 jdolecek cm = EHOSTUNREACH;
478 1.14.2.2 jdolecek } else {
479 1.14.2.2 jdolecek cm = inet6ctlerrmap[cmd];
480 1.14.2.2 jdolecek }
481 1.14.2.2 jdolecek sctp_notify(inp, cm, &sh, sin6tosa(&final),
482 1.14.2.2 jdolecek stcb, net);
483 1.14.2.2 jdolecek /* inp's ref-count reduced && stcb unlocked */
484 1.14.2.2 jdolecek }
485 1.14.2.2 jdolecek } else {
486 1.14.2.2 jdolecek if (PRC_IS_REDIRECT(cmd) && inp) {
487 1.14.2.2 jdolecek in6_rtchange((struct in6pcb *)inp,
488 1.14.2.2 jdolecek inet6ctlerrmap[cmd]);
489 1.14.2.2 jdolecek }
490 1.14.2.2 jdolecek if (inp) {
491 1.14.2.2 jdolecek /* reduce inp's ref-count */
492 1.14.2.2 jdolecek SCTP_INP_WLOCK(inp);
493 1.14.2.2 jdolecek SCTP_INP_DECR_REF(inp);
494 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(inp);
495 1.14.2.2 jdolecek }
496 1.14.2.2 jdolecek if (stcb) {
497 1.14.2.2 jdolecek SCTP_TCB_UNLOCK(stcb);
498 1.14.2.2 jdolecek }
499 1.14.2.2 jdolecek }
500 1.14.2.2 jdolecek splx(s);
501 1.14.2.2 jdolecek }
502 1.14.2.2 jdolecek return NULL;
503 1.14.2.2 jdolecek }
504 1.14.2.2 jdolecek
505 1.14.2.2 jdolecek /*
506 1.14.2.2 jdolecek * this routine can probably be collasped into the one in sctp_userreq.c
507 1.14.2.2 jdolecek * since they do the same thing and now we lookup with a sockaddr
508 1.14.2.2 jdolecek */
509 1.14.2.2 jdolecek #ifdef __FreeBSD__
510 1.14.2.2 jdolecek static int
511 1.14.2.2 jdolecek sctp6_getcred(SYSCTL_HANDLER_ARGS)
512 1.14.2.2 jdolecek {
513 1.14.2.2 jdolecek struct sockaddr_in6 addrs[2];
514 1.14.2.2 jdolecek struct sctp_inpcb *inp;
515 1.14.2.2 jdolecek struct sctp_nets *net;
516 1.14.2.2 jdolecek struct sctp_tcb *stcb;
517 1.14.2.2 jdolecek int error, s;
518 1.14.2.2 jdolecek
519 1.14.2.2 jdolecek #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
520 1.14.2.2 jdolecek error = suser(req->td);
521 1.14.2.2 jdolecek #else
522 1.14.2.2 jdolecek error = suser(req->p);
523 1.14.2.2 jdolecek #endif
524 1.14.2.2 jdolecek if (error)
525 1.14.2.2 jdolecek return (error);
526 1.14.2.2 jdolecek
527 1.14.2.2 jdolecek if (req->newlen != sizeof(addrs))
528 1.14.2.2 jdolecek return (EINVAL);
529 1.14.2.2 jdolecek if (req->oldlen != sizeof(struct ucred))
530 1.14.2.2 jdolecek return (EINVAL);
531 1.14.2.2 jdolecek error = SYSCTL_IN(req, addrs, sizeof(addrs));
532 1.14.2.2 jdolecek if (error)
533 1.14.2.2 jdolecek return (error);
534 1.14.2.2 jdolecek s = splsoftnet();
535 1.14.2.2 jdolecek
536 1.14.2.2 jdolecek stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
537 1.14.2.2 jdolecek sin6tosa(&addrs[1]),
538 1.14.2.2 jdolecek &inp, &net, 1);
539 1.14.2.2 jdolecek if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
540 1.14.2.2 jdolecek error = ENOENT;
541 1.14.2.2 jdolecek if (inp) {
542 1.14.2.2 jdolecek SCTP_INP_WLOCK(inp);
543 1.14.2.2 jdolecek SCTP_INP_DECR_REF(inp);
544 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(inp);
545 1.14.2.2 jdolecek }
546 1.14.2.2 jdolecek goto out;
547 1.14.2.2 jdolecek }
548 1.14.2.2 jdolecek error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
549 1.14.2.2 jdolecek sizeof(struct ucred));
550 1.14.2.2 jdolecek
551 1.14.2.2 jdolecek SCTP_TCB_UNLOCK (stcb);
552 1.14.2.2 jdolecek out:
553 1.14.2.2 jdolecek splx(s);
554 1.14.2.2 jdolecek return (error);
555 1.14.2.2 jdolecek }
556 1.14.2.2 jdolecek
557 1.14.2.2 jdolecek SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
558 1.14.2.2 jdolecek 0, 0,
559 1.14.2.2 jdolecek sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
560 1.14.2.2 jdolecek
561 1.14.2.2 jdolecek #endif
562 1.14.2.2 jdolecek
563 1.14.2.2 jdolecek /* This is the same as the sctp_abort() could be made common */
564 1.14.2.2 jdolecek static int
565 1.14.2.2 jdolecek sctp6_abort(struct socket *so)
566 1.14.2.2 jdolecek {
567 1.14.2.2 jdolecek int s;
568 1.14.2.2 jdolecek struct sctp_inpcb *inp;
569 1.14.2.2 jdolecek
570 1.14.2.2 jdolecek KASSERT(solocked(so));
571 1.14.2.2 jdolecek
572 1.14.2.2 jdolecek s = splsoftnet();
573 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
574 1.14.2.2 jdolecek if (inp == 0)
575 1.14.2.2 jdolecek return EINVAL; /* ??? possible? panic instead? */
576 1.14.2.2 jdolecek soisdisconnected(so);
577 1.14.2.2 jdolecek sctp_inpcb_free(inp, 1);
578 1.14.2.2 jdolecek splx(s);
579 1.14.2.2 jdolecek return 0;
580 1.14.2.2 jdolecek }
581 1.14.2.2 jdolecek
582 1.14.2.2 jdolecek static int
583 1.14.2.2 jdolecek sctp6_attach(struct socket *so, int proto)
584 1.14.2.2 jdolecek {
585 1.14.2.2 jdolecek struct in6pcb *inp6;
586 1.14.2.2 jdolecek int error;
587 1.14.2.2 jdolecek struct sctp_inpcb *inp;
588 1.14.2.2 jdolecek
589 1.14.2.2 jdolecek sosetlock(so);
590 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
591 1.14.2.2 jdolecek if (inp != NULL)
592 1.14.2.2 jdolecek return EINVAL;
593 1.14.2.2 jdolecek
594 1.14.2.2 jdolecek if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
595 1.14.2.2 jdolecek error = soreserve(so, sctp_sendspace, sctp_recvspace);
596 1.14.2.2 jdolecek if (error)
597 1.14.2.2 jdolecek return error;
598 1.14.2.2 jdolecek }
599 1.14.2.2 jdolecek error = sctp_inpcb_alloc(so);
600 1.14.2.2 jdolecek if (error)
601 1.14.2.2 jdolecek return error;
602 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
603 1.14.2.2 jdolecek inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
604 1.14.2.2 jdolecek inp6 = (struct in6pcb *)inp;
605 1.14.2.2 jdolecek
606 1.14.2.2 jdolecek inp->inp_vflag |= INP_IPV6;
607 1.14.2.2 jdolecek if (ip6_v6only) {
608 1.14.2.2 jdolecek inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
609 1.14.2.2 jdolecek }
610 1.14.2.2 jdolecek so->so_send = sctp_sosend;
611 1.14.2.2 jdolecek
612 1.14.2.2 jdolecek #ifdef IPSEC
613 1.14.2.2 jdolecek inp6->in6p_af = proto;
614 1.14.2.2 jdolecek #endif
615 1.14.2.2 jdolecek inp6->in6p_hops = -1; /* use kernel default */
616 1.14.2.2 jdolecek inp6->in6p_cksum = -1; /* just to be sure */
617 1.14.2.2 jdolecek #ifdef INET
618 1.14.2.2 jdolecek /*
619 1.14.2.2 jdolecek * XXX: ugly!!
620 1.14.2.2 jdolecek * IPv4 TTL initialization is necessary for an IPv6 socket as well,
621 1.14.2.2 jdolecek * because the socket may be bound to an IPv6 wildcard address,
622 1.14.2.2 jdolecek * which may match an IPv4-mapped IPv6 address.
623 1.14.2.2 jdolecek */
624 1.14.2.2 jdolecek inp->inp_ip_ttl = ip_defttl;
625 1.14.2.2 jdolecek #endif
626 1.14.2.2 jdolecek /*
627 1.14.2.2 jdolecek * Hmm what about the IPSEC stuff that is missing here but
628 1.14.2.2 jdolecek * in sctp_attach()?
629 1.14.2.2 jdolecek */
630 1.14.2.2 jdolecek return 0;
631 1.14.2.2 jdolecek }
632 1.14.2.2 jdolecek
633 1.14.2.2 jdolecek static int
634 1.14.2.2 jdolecek sctp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
635 1.14.2.2 jdolecek {
636 1.14.2.2 jdolecek struct sctp_inpcb *inp;
637 1.14.2.2 jdolecek struct in6pcb *inp6;
638 1.14.2.2 jdolecek int error;
639 1.14.2.2 jdolecek
640 1.14.2.2 jdolecek KASSERT(solocked(so));
641 1.14.2.2 jdolecek
642 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
643 1.14.2.2 jdolecek if (inp == 0)
644 1.14.2.2 jdolecek return EINVAL;
645 1.14.2.2 jdolecek
646 1.14.2.2 jdolecek inp6 = (struct in6pcb *)inp;
647 1.14.2.2 jdolecek inp->inp_vflag &= ~INP_IPV4;
648 1.14.2.2 jdolecek inp->inp_vflag |= INP_IPV6;
649 1.14.2.2 jdolecek
650 1.14.2.2 jdolecek if (nam != NULL && (inp6->in6p_flags & IN6P_IPV6_V6ONLY) == 0) {
651 1.14.2.2 jdolecek if (nam->sa_family == AF_INET) {
652 1.14.2.2 jdolecek /* binding v4 addr to v6 socket, so reset flags */
653 1.14.2.2 jdolecek inp->inp_vflag |= INP_IPV4;
654 1.14.2.2 jdolecek inp->inp_vflag &= ~INP_IPV6;
655 1.14.2.2 jdolecek } else {
656 1.14.2.2 jdolecek struct sockaddr_in6 *sin6_p;
657 1.14.2.2 jdolecek sin6_p = (struct sockaddr_in6 *)nam;
658 1.14.2.2 jdolecek
659 1.14.2.2 jdolecek if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
660 1.14.2.2 jdolecek inp->inp_vflag |= INP_IPV4;
661 1.14.2.2 jdolecek }
662 1.14.2.2 jdolecek else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
663 1.14.2.2 jdolecek struct sockaddr_in sin;
664 1.14.2.2 jdolecek in6_sin6_2_sin(&sin, sin6_p);
665 1.14.2.2 jdolecek inp->inp_vflag |= INP_IPV4;
666 1.14.2.2 jdolecek inp->inp_vflag &= ~INP_IPV6;
667 1.14.2.2 jdolecek error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, l);
668 1.14.2.2 jdolecek return error;
669 1.14.2.2 jdolecek }
670 1.14.2.2 jdolecek }
671 1.14.2.2 jdolecek } else if (nam != NULL) {
672 1.14.2.2 jdolecek /* IPV6_V6ONLY socket */
673 1.14.2.2 jdolecek if (nam->sa_family == AF_INET) {
674 1.14.2.2 jdolecek /* can't bind v4 addr to v6 only socket! */
675 1.14.2.2 jdolecek return EINVAL;
676 1.14.2.2 jdolecek } else {
677 1.14.2.2 jdolecek struct sockaddr_in6 *sin6_p;
678 1.14.2.2 jdolecek sin6_p = (struct sockaddr_in6 *)nam;
679 1.14.2.2 jdolecek
680 1.14.2.2 jdolecek if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
681 1.14.2.2 jdolecek /* can't bind v4-mapped addrs either! */
682 1.14.2.2 jdolecek /* NOTE: we don't support SIIT */
683 1.14.2.2 jdolecek return EINVAL;
684 1.14.2.2 jdolecek }
685 1.14.2.2 jdolecek }
686 1.14.2.2 jdolecek error = sctp_inpcb_bind(so, nam, l);
687 1.14.2.2 jdolecek return error;
688 1.14.2.2 jdolecek }
689 1.14.2.2 jdolecek
690 1.14.2.2 jdolecek /*This could be made common with sctp_detach() since they are identical */
691 1.14.2.2 jdolecek static int
692 1.14.2.2 jdolecek sctp6_detach(struct socket *so)
693 1.14.2.2 jdolecek {
694 1.14.2.2 jdolecek struct sctp_inpcb *inp;
695 1.14.2.2 jdolecek
696 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
697 1.14.2.2 jdolecek if (inp == 0)
698 1.14.2.2 jdolecek return EINVAL;
699 1.14.2.2 jdolecek
700 1.14.2.2 jdolecek if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
701 1.14.2.2 jdolecek (so->so_rcv.sb_cc > 0))
702 1.14.2.2 jdolecek sctp_inpcb_free(inp, 1);
703 1.14.2.2 jdolecek else
704 1.14.2.2 jdolecek sctp_inpcb_free(inp, 0);
705 1.14.2.2 jdolecek return 0;
706 1.14.2.2 jdolecek }
707 1.14.2.2 jdolecek
708 1.14.2.2 jdolecek static int
709 1.14.2.2 jdolecek sctp6_disconnect(struct socket *so)
710 1.14.2.2 jdolecek {
711 1.14.2.2 jdolecek struct sctp_inpcb *inp;
712 1.14.2.2 jdolecek
713 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
714 1.14.2.2 jdolecek if (inp == NULL) {
715 1.14.2.2 jdolecek return (ENOTCONN);
716 1.14.2.2 jdolecek }
717 1.14.2.2 jdolecek if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
718 1.14.2.2 jdolecek if (LIST_EMPTY(&inp->sctp_asoc_list)) {
719 1.14.2.2 jdolecek /* No connection */
720 1.14.2.2 jdolecek return (ENOTCONN);
721 1.14.2.2 jdolecek } else {
722 1.14.2.2 jdolecek int some_on_streamwheel = 0;
723 1.14.2.2 jdolecek struct sctp_association *asoc;
724 1.14.2.2 jdolecek struct sctp_tcb *stcb;
725 1.14.2.2 jdolecek
726 1.14.2.2 jdolecek stcb = LIST_FIRST(&inp->sctp_asoc_list);
727 1.14.2.2 jdolecek if (stcb == NULL) {
728 1.14.2.2 jdolecek return (EINVAL);
729 1.14.2.2 jdolecek }
730 1.14.2.2 jdolecek asoc = &stcb->asoc;
731 1.14.2.2 jdolecek if (!TAILQ_EMPTY(&asoc->out_wheel)) {
732 1.14.2.2 jdolecek /* Check to see if some data queued */
733 1.14.2.2 jdolecek struct sctp_stream_out *outs;
734 1.14.2.2 jdolecek TAILQ_FOREACH(outs, &asoc->out_wheel,
735 1.14.2.2 jdolecek next_spoke) {
736 1.14.2.2 jdolecek if (!TAILQ_EMPTY(&outs->outqueue)) {
737 1.14.2.2 jdolecek some_on_streamwheel = 1;
738 1.14.2.2 jdolecek break;
739 1.14.2.2 jdolecek }
740 1.14.2.2 jdolecek }
741 1.14.2.2 jdolecek }
742 1.14.2.2 jdolecek
743 1.14.2.2 jdolecek if (TAILQ_EMPTY(&asoc->send_queue) &&
744 1.14.2.2 jdolecek TAILQ_EMPTY(&asoc->sent_queue) &&
745 1.14.2.2 jdolecek (some_on_streamwheel == 0)) {
746 1.14.2.2 jdolecek /* nothing queued to send, so I'm done... */
747 1.14.2.2 jdolecek if ((SCTP_GET_STATE(asoc) !=
748 1.14.2.2 jdolecek SCTP_STATE_SHUTDOWN_SENT) &&
749 1.14.2.2 jdolecek (SCTP_GET_STATE(asoc) !=
750 1.14.2.2 jdolecek SCTP_STATE_SHUTDOWN_ACK_SENT)) {
751 1.14.2.2 jdolecek /* only send SHUTDOWN the first time */
752 1.14.2.2 jdolecek #ifdef SCTP_DEBUG
753 1.14.2.2 jdolecek if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
754 1.14.2.2 jdolecek printf("%s:%d sends a shutdown\n",
755 1.14.2.2 jdolecek __FILE__,
756 1.14.2.2 jdolecek __LINE__
757 1.14.2.2 jdolecek );
758 1.14.2.2 jdolecek }
759 1.14.2.2 jdolecek #endif
760 1.14.2.2 jdolecek sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
761 1.14.2.2 jdolecek sctp_chunk_output(stcb->sctp_ep, stcb, 1);
762 1.14.2.2 jdolecek asoc->state = SCTP_STATE_SHUTDOWN_SENT;
763 1.14.2.2 jdolecek sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
764 1.14.2.2 jdolecek stcb->sctp_ep, stcb,
765 1.14.2.2 jdolecek asoc->primary_destination);
766 1.14.2.2 jdolecek sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
767 1.14.2.2 jdolecek stcb->sctp_ep, stcb,
768 1.14.2.2 jdolecek asoc->primary_destination);
769 1.14.2.2 jdolecek }
770 1.14.2.2 jdolecek } else {
771 1.14.2.2 jdolecek /*
772 1.14.2.2 jdolecek * we still got (or just got) data to send,
773 1.14.2.2 jdolecek * so set SHUTDOWN_PENDING
774 1.14.2.2 jdolecek */
775 1.14.2.2 jdolecek /*
776 1.14.2.2 jdolecek * XXX sockets draft says that MSG_EOF should
777 1.14.2.2 jdolecek * be sent with no data. currently, we will
778 1.14.2.2 jdolecek * allow user data to be sent first and move
779 1.14.2.2 jdolecek * to SHUTDOWN-PENDING
780 1.14.2.2 jdolecek */
781 1.14.2.2 jdolecek asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
782 1.14.2.2 jdolecek }
783 1.14.2.2 jdolecek return (0);
784 1.14.2.2 jdolecek }
785 1.14.2.2 jdolecek } else {
786 1.14.2.2 jdolecek /* UDP model does not support this */
787 1.14.2.2 jdolecek return EOPNOTSUPP;
788 1.14.2.2 jdolecek }
789 1.14.2.2 jdolecek }
790 1.14.2.2 jdolecek
791 1.14.2.2 jdolecek static int
792 1.14.2.2 jdolecek sctp6_recvoob(struct socket *so, struct mbuf *m, int flags)
793 1.14.2.2 jdolecek {
794 1.14.2.2 jdolecek KASSERT(solocked(so));
795 1.14.2.2 jdolecek
796 1.14.2.2 jdolecek return EOPNOTSUPP;
797 1.14.2.2 jdolecek }
798 1.14.2.2 jdolecek
799 1.14.2.2 jdolecek static int
800 1.14.2.2 jdolecek sctp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
801 1.14.2.2 jdolecek struct mbuf *control, struct lwp *l)
802 1.14.2.2 jdolecek {
803 1.14.2.2 jdolecek struct sctp_inpcb *inp;
804 1.14.2.2 jdolecek struct in6pcb *inp6;
805 1.14.2.2 jdolecek #ifdef INET
806 1.14.2.2 jdolecek struct sockaddr_in6 *sin6;
807 1.14.2.2 jdolecek #endif /* INET */
808 1.14.2.2 jdolecek /* No SPL needed since sctp_output does this */
809 1.14.2.2 jdolecek
810 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
811 1.14.2.2 jdolecek if (inp == NULL) {
812 1.14.2.2 jdolecek if (control) {
813 1.14.2.2 jdolecek m_freem(control);
814 1.14.2.2 jdolecek control = NULL;
815 1.14.2.2 jdolecek }
816 1.14.2.2 jdolecek m_freem(m);
817 1.14.2.2 jdolecek return EINVAL;
818 1.14.2.2 jdolecek }
819 1.14.2.2 jdolecek inp6 = (struct in6pcb *)inp;
820 1.14.2.2 jdolecek /* For the TCP model we may get a NULL addr, if we
821 1.14.2.2 jdolecek * are a connected socket thats ok.
822 1.14.2.2 jdolecek */
823 1.14.2.2 jdolecek if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
824 1.14.2.2 jdolecek (nam == NULL)) {
825 1.14.2.2 jdolecek goto connected_type;
826 1.14.2.2 jdolecek }
827 1.14.2.2 jdolecek if (nam == NULL) {
828 1.14.2.2 jdolecek m_freem(m);
829 1.14.2.2 jdolecek if (control) {
830 1.14.2.2 jdolecek m_freem(control);
831 1.14.2.2 jdolecek control = NULL;
832 1.14.2.2 jdolecek }
833 1.14.2.2 jdolecek return (EDESTADDRREQ);
834 1.14.2.2 jdolecek }
835 1.14.2.2 jdolecek
836 1.14.2.2 jdolecek #ifdef INET
837 1.14.2.2 jdolecek sin6 = (struct sockaddr_in6 *)nam;
838 1.14.2.2 jdolecek if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
839 1.14.2.2 jdolecek /*
840 1.14.2.2 jdolecek * if IPV6_V6ONLY flag, we discard datagrams
841 1.14.2.2 jdolecek * destined to a v4 addr or v4-mapped addr
842 1.14.2.2 jdolecek */
843 1.14.2.2 jdolecek if (nam->sa_family == AF_INET) {
844 1.14.2.2 jdolecek return EINVAL;
845 1.14.2.2 jdolecek }
846 1.14.2.2 jdolecek if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
847 1.14.2.2 jdolecek return EINVAL;
848 1.14.2.2 jdolecek }
849 1.14.2.2 jdolecek }
850 1.14.2.2 jdolecek
851 1.14.2.2 jdolecek if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
852 1.14.2.2 jdolecek if (!ip6_v6only) {
853 1.14.2.2 jdolecek struct sockaddr_in sin;
854 1.14.2.2 jdolecek /* convert v4-mapped into v4 addr and send */
855 1.14.2.2 jdolecek in6_sin6_2_sin(&sin, sin6);
856 1.14.2.2 jdolecek return sctp_send(so, m, (struct sockaddr *)&sin,
857 1.14.2.2 jdolecek control, l);
858 1.14.2.2 jdolecek } else {
859 1.14.2.2 jdolecek /* mapped addresses aren't enabled */
860 1.14.2.2 jdolecek return EINVAL;
861 1.14.2.2 jdolecek }
862 1.14.2.2 jdolecek }
863 1.14.2.2 jdolecek #endif /* INET */
864 1.14.2.2 jdolecek connected_type:
865 1.14.2.2 jdolecek /* now what about control */
866 1.14.2.2 jdolecek if (control) {
867 1.14.2.2 jdolecek if (inp->control) {
868 1.14.2.2 jdolecek printf("huh? control set?\n");
869 1.14.2.2 jdolecek m_freem(inp->control);
870 1.14.2.2 jdolecek inp->control = NULL;
871 1.14.2.2 jdolecek }
872 1.14.2.2 jdolecek inp->control = control;
873 1.14.2.2 jdolecek }
874 1.14.2.2 jdolecek /* add it in possibly */
875 1.14.2.2 jdolecek if ((inp->pkt) &&
876 1.14.2.2 jdolecek (inp->pkt->m_flags & M_PKTHDR)) {
877 1.14.2.2 jdolecek struct mbuf *x;
878 1.14.2.2 jdolecek int c_len;
879 1.14.2.2 jdolecek
880 1.14.2.2 jdolecek c_len = 0;
881 1.14.2.2 jdolecek /* How big is it */
882 1.14.2.2 jdolecek for (x=m;x;x = x->m_next) {
883 1.14.2.2 jdolecek c_len += x->m_len;
884 1.14.2.2 jdolecek }
885 1.14.2.2 jdolecek inp->pkt->m_pkthdr.len += c_len;
886 1.14.2.2 jdolecek }
887 1.14.2.2 jdolecek /* Place the data */
888 1.14.2.2 jdolecek if (inp->pkt) {
889 1.14.2.2 jdolecek inp->pkt_last->m_next = m;
890 1.14.2.2 jdolecek inp->pkt_last = m;
891 1.14.2.2 jdolecek } else {
892 1.14.2.2 jdolecek inp->pkt_last = inp->pkt = m;
893 1.14.2.2 jdolecek }
894 1.14.2.2 jdolecek if ((so->so_state & SS_MORETOCOME) == 0) {
895 1.14.2.2 jdolecek /*
896 1.14.2.2 jdolecek * note with the current version this code will only be
897 1.14.2.2 jdolecek * used by OpenBSD, NetBSD and FreeBSD have methods for
898 1.14.2.2 jdolecek * re-defining sosend() to use sctp_sosend(). One can
899 1.14.2.2 jdolecek * optionaly switch back to this code (by changing back
900 1.14.2.2 jdolecek * the defininitions but this is not advisable.
901 1.14.2.2 jdolecek */
902 1.14.2.2 jdolecek int ret;
903 1.14.2.2 jdolecek ret = sctp_output(inp, inp->pkt , nam, inp->control, l, 0);
904 1.14.2.2 jdolecek inp->pkt = NULL;
905 1.14.2.2 jdolecek inp->control = NULL;
906 1.14.2.2 jdolecek return (ret);
907 1.14.2.2 jdolecek } else {
908 1.14.2.2 jdolecek return (0);
909 1.14.2.2 jdolecek }
910 1.14.2.2 jdolecek }
911 1.14.2.2 jdolecek
912 1.14.2.2 jdolecek static int
913 1.14.2.2 jdolecek sctp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
914 1.14.2.2 jdolecek {
915 1.14.2.2 jdolecek KASSERT(solocked(so));
916 1.14.2.2 jdolecek
917 1.14.2.2 jdolecek if (m)
918 1.14.2.2 jdolecek m_freem(m);
919 1.14.2.2 jdolecek if (control)
920 1.14.2.2 jdolecek m_freem(control);
921 1.14.2.2 jdolecek
922 1.14.2.2 jdolecek return EOPNOTSUPP;
923 1.14.2.2 jdolecek }
924 1.14.2.2 jdolecek
925 1.14.2.2 jdolecek static int
926 1.14.2.2 jdolecek sctp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
927 1.14.2.2 jdolecek {
928 1.14.2.2 jdolecek int error = 0;
929 1.14.2.2 jdolecek struct sctp_inpcb *inp;
930 1.14.2.2 jdolecek struct in6pcb *inp6;
931 1.14.2.2 jdolecek struct sctp_tcb *stcb;
932 1.14.2.2 jdolecek #ifdef INET
933 1.14.2.2 jdolecek struct sockaddr_in6 *sin6;
934 1.14.2.2 jdolecek struct sockaddr_storage ss;
935 1.14.2.2 jdolecek #endif /* INET */
936 1.14.2.2 jdolecek
937 1.14.2.2 jdolecek inp6 = (struct in6pcb *)so->so_pcb;
938 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
939 1.14.2.2 jdolecek if (inp == 0) {
940 1.14.2.2 jdolecek return (ECONNRESET); /* I made the same as TCP since
941 1.14.2.2 jdolecek * we are not setup? */
942 1.14.2.2 jdolecek }
943 1.14.2.2 jdolecek SCTP_ASOC_CREATE_LOCK(inp);
944 1.14.2.2 jdolecek SCTP_INP_RLOCK(inp);
945 1.14.2.2 jdolecek if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
946 1.14.2.2 jdolecek SCTP_PCB_FLAGS_UNBOUND) {
947 1.14.2.2 jdolecek /* Bind a ephemeral port */
948 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
949 1.14.2.2 jdolecek error = sctp6_bind(so, NULL, l);
950 1.14.2.2 jdolecek if (error) {
951 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
952 1.14.2.2 jdolecek
953 1.14.2.2 jdolecek return (error);
954 1.14.2.2 jdolecek }
955 1.14.2.2 jdolecek SCTP_INP_RLOCK(inp);
956 1.14.2.2 jdolecek }
957 1.14.2.2 jdolecek
958 1.14.2.2 jdolecek if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
959 1.14.2.2 jdolecek (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
960 1.14.2.2 jdolecek /* We are already connected AND the TCP model */
961 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
962 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
963 1.14.2.2 jdolecek return (EADDRINUSE);
964 1.14.2.2 jdolecek }
965 1.14.2.2 jdolecek
966 1.14.2.2 jdolecek #ifdef INET
967 1.14.2.2 jdolecek sin6 = (struct sockaddr_in6 *)nam;
968 1.14.2.2 jdolecek if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
969 1.14.2.2 jdolecek /*
970 1.14.2.2 jdolecek * if IPV6_V6ONLY flag, ignore connections
971 1.14.2.2 jdolecek * destined to a v4 addr or v4-mapped addr
972 1.14.2.2 jdolecek */
973 1.14.2.2 jdolecek if (nam->sa_family == AF_INET) {
974 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
975 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
976 1.14.2.2 jdolecek return EINVAL;
977 1.14.2.2 jdolecek }
978 1.14.2.2 jdolecek if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
979 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
980 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
981 1.14.2.2 jdolecek return EINVAL;
982 1.14.2.2 jdolecek }
983 1.14.2.2 jdolecek }
984 1.14.2.2 jdolecek
985 1.14.2.2 jdolecek if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
986 1.14.2.2 jdolecek if (!ip6_v6only) {
987 1.14.2.2 jdolecek /* convert v4-mapped into v4 addr */
988 1.14.2.2 jdolecek in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
989 1.14.2.2 jdolecek nam = (struct sockaddr *)&ss;
990 1.14.2.2 jdolecek } else {
991 1.14.2.2 jdolecek /* mapped addresses aren't enabled */
992 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
993 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
994 1.14.2.2 jdolecek return EINVAL;
995 1.14.2.2 jdolecek }
996 1.14.2.2 jdolecek }
997 1.14.2.2 jdolecek #endif /* INET */
998 1.14.2.2 jdolecek
999 1.14.2.2 jdolecek /* Now do we connect? */
1000 1.14.2.2 jdolecek if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1001 1.14.2.2 jdolecek stcb = LIST_FIRST(&inp->sctp_asoc_list);
1002 1.14.2.2 jdolecek if (stcb) {
1003 1.14.2.2 jdolecek SCTP_TCB_UNLOCK (stcb);
1004 1.14.2.2 jdolecek }
1005 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
1006 1.14.2.2 jdolecek } else {
1007 1.14.2.2 jdolecek SCTP_INP_RUNLOCK(inp);
1008 1.14.2.2 jdolecek SCTP_INP_WLOCK(inp);
1009 1.14.2.2 jdolecek SCTP_INP_INCR_REF(inp);
1010 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(inp);
1011 1.14.2.2 jdolecek stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
1012 1.14.2.2 jdolecek if (stcb == NULL) {
1013 1.14.2.2 jdolecek SCTP_INP_WLOCK(inp);
1014 1.14.2.2 jdolecek SCTP_INP_DECR_REF(inp);
1015 1.14.2.2 jdolecek SCTP_INP_WUNLOCK(inp);
1016 1.14.2.2 jdolecek }
1017 1.14.2.2 jdolecek }
1018 1.14.2.2 jdolecek
1019 1.14.2.2 jdolecek if (stcb != NULL) {
1020 1.14.2.2 jdolecek /* Already have or am bring up an association */
1021 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
1022 1.14.2.2 jdolecek SCTP_TCB_UNLOCK (stcb);
1023 1.14.2.2 jdolecek return (EALREADY);
1024 1.14.2.2 jdolecek }
1025 1.14.2.2 jdolecek /* We are GOOD to go */
1026 1.14.2.2 jdolecek stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
1027 1.14.2.2 jdolecek SCTP_ASOC_CREATE_UNLOCK(inp);
1028 1.14.2.2 jdolecek if (stcb == NULL) {
1029 1.14.2.2 jdolecek /* Gak! no memory */
1030 1.14.2.2 jdolecek return (error);
1031 1.14.2.2 jdolecek }
1032 1.14.2.2 jdolecek if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1033 1.14.2.2 jdolecek stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1034 1.14.2.2 jdolecek /* Set the connected flag so we can queue data */
1035 1.14.2.2 jdolecek soisconnecting(so);
1036 1.14.2.2 jdolecek }
1037 1.14.2.2 jdolecek stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1038 1.14.2.2 jdolecek SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1039 1.14.2.2 jdolecek sctp_send_initiate(inp, stcb);
1040 1.14.2.2 jdolecek SCTP_TCB_UNLOCK (stcb);
1041 1.14.2.2 jdolecek return error;
1042 1.14.2.2 jdolecek }
1043 1.14.2.2 jdolecek
1044 1.14.2.2 jdolecek static int
1045 1.14.2.2 jdolecek sctp6_connect2(struct socket *so, struct socket *so2)
1046 1.14.2.2 jdolecek {
1047 1.14.2.2 jdolecek KASSERT(solocked(so));
1048 1.14.2.2 jdolecek
1049 1.14.2.2 jdolecek return EOPNOTSUPP;
1050 1.14.2.2 jdolecek }
1051 1.14.2.2 jdolecek
1052 1.14.2.2 jdolecek static int
1053 1.14.2.2 jdolecek sctp6_getaddr(struct socket *so, struct sockaddr *nam)
1054 1.14.2.2 jdolecek {
1055 1.14.2.2 jdolecek struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1056 1.14.2.2 jdolecek struct sctp_inpcb *inp;
1057 1.14.2.2 jdolecek int error;
1058 1.14.2.2 jdolecek
1059 1.14.2.2 jdolecek /*
1060 1.14.2.2 jdolecek * Do the malloc first in case it blocks.
1061 1.14.2.2 jdolecek */
1062 1.14.2.2 jdolecek memset(sin6, 0, sizeof(*sin6));
1063 1.14.2.2 jdolecek sin6->sin6_family = AF_INET6;
1064 1.14.2.2 jdolecek sin6->sin6_len = sizeof(*sin6);
1065 1.14.2.2 jdolecek
1066 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
1067 1.14.2.2 jdolecek if (inp == NULL) {
1068 1.14.2.2 jdolecek return ECONNRESET;
1069 1.14.2.2 jdolecek }
1070 1.14.2.2 jdolecek
1071 1.14.2.2 jdolecek sin6->sin6_port = inp->sctp_lport;
1072 1.14.2.2 jdolecek if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1073 1.14.2.2 jdolecek /* For the bound all case you get back 0 */
1074 1.14.2.2 jdolecek if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1075 1.14.2.2 jdolecek struct sctp_tcb *stcb;
1076 1.14.2.2 jdolecek const struct sockaddr_in6 *sin_a6;
1077 1.14.2.2 jdolecek struct sctp_nets *net;
1078 1.14.2.2 jdolecek int fnd;
1079 1.14.2.2 jdolecek
1080 1.14.2.2 jdolecek stcb = LIST_FIRST(&inp->sctp_asoc_list);
1081 1.14.2.2 jdolecek if (stcb == NULL) {
1082 1.14.2.2 jdolecek goto notConn6;
1083 1.14.2.2 jdolecek }
1084 1.14.2.2 jdolecek fnd = 0;
1085 1.14.2.2 jdolecek sin_a6 = NULL;
1086 1.14.2.2 jdolecek TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1087 1.14.2.2 jdolecek sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
1088 1.14.2.2 jdolecek if (sin_a6->sin6_family == AF_INET6) {
1089 1.14.2.2 jdolecek fnd = 1;
1090 1.14.2.2 jdolecek break;
1091 1.14.2.2 jdolecek }
1092 1.14.2.2 jdolecek }
1093 1.14.2.2 jdolecek if ((!fnd) || (sin_a6 == NULL)) {
1094 1.14.2.2 jdolecek /* punt */
1095 1.14.2.2 jdolecek goto notConn6;
1096 1.14.2.2 jdolecek }
1097 1.14.2.2 jdolecek sin6->sin6_addr = sctp_ipv6_source_address_selection(
1098 1.14.2.2 jdolecek inp, stcb, &net->ro, net, 0);
1099 1.14.2.2 jdolecek
1100 1.14.2.2 jdolecek } else {
1101 1.14.2.2 jdolecek /* For the bound all case you get back 0 */
1102 1.14.2.2 jdolecek notConn6:
1103 1.14.2.2 jdolecek memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1104 1.14.2.2 jdolecek }
1105 1.14.2.2 jdolecek } else {
1106 1.14.2.2 jdolecek /* Take the first IPv6 address in the list */
1107 1.14.2.2 jdolecek struct sctp_laddr *laddr;
1108 1.14.2.2 jdolecek int fnd = 0;
1109 1.14.2.2 jdolecek LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1110 1.14.2.2 jdolecek if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
1111 1.14.2.2 jdolecek struct sockaddr_in6 *sin_a;
1112 1.14.2.2 jdolecek sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1113 1.14.2.2 jdolecek sin6->sin6_addr = sin_a->sin6_addr;
1114 1.14.2.2 jdolecek fnd = 1;
1115 1.14.2.2 jdolecek break;
1116 1.14.2.2 jdolecek }
1117 1.14.2.2 jdolecek }
1118 1.14.2.2 jdolecek if (!fnd) {
1119 1.14.2.2 jdolecek return ENOENT;
1120 1.14.2.2 jdolecek }
1121 1.14.2.2 jdolecek }
1122 1.14.2.2 jdolecek /* Scoping things for v6 */
1123 1.14.2.2 jdolecek if ((error = sa6_recoverscope(sin6)) != 0)
1124 1.14.2.2 jdolecek return (error);
1125 1.14.2.2 jdolecek
1126 1.14.2.2 jdolecek return (0);
1127 1.14.2.2 jdolecek }
1128 1.14.2.2 jdolecek
1129 1.14.2.2 jdolecek static int
1130 1.14.2.2 jdolecek sctp6_peeraddr(struct socket *so, struct sockaddr *nam)
1131 1.14.2.2 jdolecek {
1132 1.14.2.2 jdolecek struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1133 1.14.2.2 jdolecek int fnd, error;
1134 1.14.2.2 jdolecek const struct sockaddr_in6 *sin_a6;
1135 1.14.2.2 jdolecek struct sctp_inpcb *inp;
1136 1.14.2.2 jdolecek struct sctp_tcb *stcb;
1137 1.14.2.2 jdolecek struct sctp_nets *net;
1138 1.14.2.2 jdolecek /*
1139 1.14.2.2 jdolecek * Do the malloc first in case it blocks.
1140 1.14.2.2 jdolecek */
1141 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
1142 1.14.2.2 jdolecek if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1143 1.14.2.2 jdolecek /* UDP type and listeners will drop out here */
1144 1.14.2.2 jdolecek return (ENOTCONN);
1145 1.14.2.2 jdolecek }
1146 1.14.2.2 jdolecek memset(sin6, 0, sizeof(*sin6));
1147 1.14.2.2 jdolecek sin6->sin6_family = AF_INET6;
1148 1.14.2.2 jdolecek sin6->sin6_len = sizeof(*sin6);
1149 1.14.2.2 jdolecek
1150 1.14.2.2 jdolecek /* We must recapture incase we blocked */
1151 1.14.2.2 jdolecek inp = (struct sctp_inpcb *)so->so_pcb;
1152 1.14.2.2 jdolecek if (inp == NULL) {
1153 1.14.2.2 jdolecek return ECONNRESET;
1154 1.14.2.2 jdolecek }
1155 1.14.2.2 jdolecek stcb = LIST_FIRST(&inp->sctp_asoc_list);
1156 1.14.2.2 jdolecek if (stcb == NULL) {
1157 1.14.2.2 jdolecek return ECONNRESET;
1158 1.14.2.2 jdolecek }
1159 1.14.2.2 jdolecek fnd = 0;
1160 1.14.2.2 jdolecek TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1161 1.14.2.2 jdolecek sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
1162 1.14.2.2 jdolecek if (sin_a6->sin6_family == AF_INET6) {
1163 1.14.2.2 jdolecek fnd = 1;
1164 1.14.2.2 jdolecek sin6->sin6_port = stcb->rport;
1165 1.14.2.2 jdolecek sin6->sin6_addr = sin_a6->sin6_addr;
1166 1.14.2.2 jdolecek break;
1167 1.14.2.2 jdolecek }
1168 1.14.2.2 jdolecek }
1169 1.14.2.2 jdolecek if (!fnd) {
1170 1.14.2.2 jdolecek /* No IPv4 address */
1171 1.14.2.2 jdolecek return ENOENT;
1172 1.14.2.2 jdolecek }
1173 1.14.2.2 jdolecek if ((error = sa6_recoverscope(sin6)) != 0)
1174 1.14.2.2 jdolecek return (error);
1175 1.14.2.2 jdolecek
1176 1.14.2.2 jdolecek return (0);
1177 1.14.2.2 jdolecek }
1178 1.14.2.2 jdolecek
1179 1.14.2.2 jdolecek static int
1180 1.14.2.2 jdolecek sctp6_sockaddr(struct socket *so, struct sockaddr *nam)
1181 1.14.2.2 jdolecek {
1182 1.14.2.2 jdolecek struct in6pcb *inp6 = sotoin6pcb(so);
1183 1.14.2.2 jdolecek int error;
1184 1.14.2.2 jdolecek
1185 1.14.2.2 jdolecek if (inp6 == NULL)
1186 1.14.2.2 jdolecek return EINVAL;
1187 1.14.2.2 jdolecek
1188 1.14.2.2 jdolecek /* allow v6 addresses precedence */
1189 1.14.2.2 jdolecek error = sctp6_getaddr(so, nam);
1190 1.14.2.2 jdolecek if (error) {
1191 1.14.2.2 jdolecek /* try v4 next if v6 failed */
1192 1.14.2.2 jdolecek error = sctp_sockaddr(so, nam);
1193 1.14.2.2 jdolecek if (error) {
1194 1.14.2.2 jdolecek return (error);
1195 1.14.2.2 jdolecek }
1196 1.14.2.2 jdolecek
1197 1.14.2.2 jdolecek /* if I'm V6ONLY, convert it to v4-mapped */
1198 1.14.2.2 jdolecek if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
1199 1.14.2.2 jdolecek struct sockaddr_in6 sin6;
1200 1.14.2.2 jdolecek in6_sin_2_v4mapsin6((struct sockaddr_in *)nam, &sin6);
1201 1.14.2.2 jdolecek memcpy(nam, &sin6, sizeof(struct sockaddr_in6));
1202 1.14.2.2 jdolecek }
1203 1.14.2.2 jdolecek }
1204 1.14.2.2 jdolecek return (error);
1205 1.14.2.2 jdolecek }
1206 1.14.2.2 jdolecek
1207 1.14.2.2 jdolecek #if 0
1208 1.14.2.2 jdolecek static int
1209 1.14.2.2 jdolecek sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam)
1210 1.14.2.2 jdolecek {
1211 1.14.2.2 jdolecek struct in6pcb *inp6 = sotoin6pcb(so);
1212 1.14.2.2 jdolecek int error;
1213 1.14.2.2 jdolecek
1214 1.14.2.2 jdolecek if (inp6 == NULL)
1215 1.14.2.2 jdolecek return EINVAL;
1216 1.14.2.2 jdolecek
1217 1.14.2.2 jdolecek /* allow v6 addresses precedence */
1218 1.14.2.2 jdolecek error = sctp6_peeraddr(so, nam);
1219 1.14.2.2 jdolecek if (error) {
1220 1.14.2.2 jdolecek /* try v4 next if v6 failed */
1221 1.14.2.2 jdolecek error = sctp_peeraddr(so, nam);
1222 1.14.2.2 jdolecek if (error) {
1223 1.14.2.2 jdolecek return (error);
1224 1.14.2.2 jdolecek }
1225 1.14.2.2 jdolecek /* if I'm V6ONLY, convert it to v4-mapped */
1226 1.14.2.2 jdolecek if ((inp6->in6p_flags & IN6P_IPV6_V6ONLY)) {
1227 1.14.2.2 jdolecek struct sockaddr_in6 sin6;
1228 1.14.2.2 jdolecek in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1229 1.14.2.2 jdolecek memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1230 1.14.2.2 jdolecek }
1231 1.14.2.2 jdolecek }
1232 1.14.2.2 jdolecek return error;
1233 1.14.2.2 jdolecek }
1234 1.14.2.2 jdolecek #endif
1235 1.14.2.2 jdolecek
1236 1.14.2.2 jdolecek static int
1237 1.14.2.2 jdolecek sctp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1238 1.14.2.2 jdolecek {
1239 1.14.2.2 jdolecek int error = 0;
1240 1.14.2.2 jdolecek int family;
1241 1.14.2.2 jdolecek
1242 1.14.2.2 jdolecek family = so->so_proto->pr_domain->dom_family;
1243 1.14.2.2 jdolecek switch (family) {
1244 1.14.2.2 jdolecek #ifdef INET
1245 1.14.2.2 jdolecek case PF_INET:
1246 1.14.2.2 jdolecek error = in_control(so, cmd, nam, ifp);
1247 1.14.2.2 jdolecek break;
1248 1.14.2.2 jdolecek #endif
1249 1.14.2.2 jdolecek #ifdef INET6
1250 1.14.2.2 jdolecek case PF_INET6:
1251 1.14.2.2 jdolecek error = in6_control(so, cmd, nam, ifp);
1252 1.14.2.2 jdolecek break;
1253 1.14.2.2 jdolecek #endif
1254 1.14.2.2 jdolecek default:
1255 1.14.2.2 jdolecek error = EAFNOSUPPORT;
1256 1.14.2.2 jdolecek }
1257 1.14.2.2 jdolecek return (error);
1258 1.14.2.2 jdolecek }
1259 1.14.2.2 jdolecek
1260 1.14.2.2 jdolecek static int
1261 1.14.2.2 jdolecek sctp6_accept(struct socket *so, struct sockaddr *nam)
1262 1.14.2.2 jdolecek {
1263 1.14.2.2 jdolecek KASSERT(solocked(so));
1264 1.14.2.2 jdolecek
1265 1.14.2.2 jdolecek return EOPNOTSUPP;
1266 1.14.2.2 jdolecek }
1267 1.14.2.2 jdolecek
1268 1.14.2.2 jdolecek static int
1269 1.14.2.2 jdolecek sctp6_stat(struct socket *so, struct stat *ub)
1270 1.14.2.2 jdolecek {
1271 1.14.2.2 jdolecek return 0;
1272 1.14.2.2 jdolecek }
1273 1.14.2.2 jdolecek
1274 1.14.2.2 jdolecek static int
1275 1.14.2.2 jdolecek sctp6_listen(struct socket *so, struct lwp *l)
1276 1.14.2.2 jdolecek {
1277 1.14.2.2 jdolecek return sctp_listen(so, l);
1278 1.14.2.2 jdolecek }
1279 1.14.2.2 jdolecek
1280 1.14.2.2 jdolecek static int
1281 1.14.2.2 jdolecek sctp6_shutdown(struct socket *so)
1282 1.14.2.2 jdolecek {
1283 1.14.2.2 jdolecek return sctp_shutdown(so);
1284 1.14.2.2 jdolecek }
1285 1.14.2.2 jdolecek
1286 1.14.2.2 jdolecek static int
1287 1.14.2.2 jdolecek sctp6_rcvd(struct socket *so, int flags, struct lwp *l)
1288 1.14.2.2 jdolecek {
1289 1.14.2.2 jdolecek KASSERT(solocked(so));
1290 1.14.2.2 jdolecek
1291 1.14.2.2 jdolecek return sctp_rcvd(so, flags, l);
1292 1.14.2.2 jdolecek }
1293 1.14.2.2 jdolecek
1294 1.14.2.2 jdolecek static int
1295 1.14.2.2 jdolecek sctp6_purgeif(struct socket *so, struct ifnet *ifp)
1296 1.14.2.2 jdolecek {
1297 1.14.2.2 jdolecek struct ifaddr *ifa;
1298 1.14.2.2 jdolecek /* FIXME NOMPSAFE */
1299 1.14.2.2 jdolecek IFADDR_READER_FOREACH(ifa, ifp) {
1300 1.14.2.2 jdolecek if (ifa->ifa_addr->sa_family == PF_INET6) {
1301 1.14.2.2 jdolecek sctp_delete_ip_address(ifa);
1302 1.14.2.2 jdolecek }
1303 1.14.2.2 jdolecek }
1304 1.14.2.2 jdolecek
1305 1.14.2.2 jdolecek #ifndef NET_MPSAFE
1306 1.14.2.2 jdolecek mutex_enter(softnet_lock);
1307 1.14.2.2 jdolecek #endif
1308 1.14.2.2 jdolecek in6_purgeif(ifp);
1309 1.14.2.2 jdolecek #ifndef NET_MPSAFE
1310 1.14.2.2 jdolecek mutex_exit(softnet_lock);
1311 1.14.2.2 jdolecek #endif
1312 1.14.2.2 jdolecek
1313 1.14.2.2 jdolecek return 0;
1314 1.14.2.2 jdolecek }
1315 1.14.2.2 jdolecek
1316 1.14.2.2 jdolecek PR_WRAP_USRREQS(sctp6)
1317 1.14.2.2 jdolecek #define sctp6_attach sctp6_attach_wrapper
1318 1.14.2.2 jdolecek #define sctp6_detach sctp6_detach_wrapper
1319 1.14.2.2 jdolecek #define sctp6_accept sctp6_accept_wrapper
1320 1.14.2.2 jdolecek #define sctp6_bind sctp6_bind_wrapper
1321 1.14.2.2 jdolecek #define sctp6_listen sctp6_listen_wrapper
1322 1.14.2.2 jdolecek #define sctp6_connect sctp6_connect_wrapper
1323 1.14.2.2 jdolecek #define sctp6_connect2 sctp6_connect2_wrapper
1324 1.14.2.2 jdolecek #define sctp6_disconnect sctp6_disconnect_wrapper
1325 1.14.2.2 jdolecek #define sctp6_shutdown sctp6_shutdown_wrapper
1326 1.14.2.2 jdolecek #define sctp6_abort sctp6_abort_wrapper
1327 1.14.2.2 jdolecek #define sctp6_ioctl sctp6_ioctl_wrapper
1328 1.14.2.2 jdolecek #define sctp6_stat sctp6_stat_wrapper
1329 1.14.2.2 jdolecek #define sctp6_peeraddr sctp6_peeraddr_wrapper
1330 1.14.2.2 jdolecek #define sctp6_sockaddr sctp6_sockaddr_wrapper
1331 1.14.2.2 jdolecek #define sctp6_rcvd sctp6_rcvd_wrapper
1332 1.14.2.2 jdolecek #define sctp6_recvoob sctp6_recvoob_wrapper
1333 1.14.2.2 jdolecek #define sctp6_send sctp6_send_wrapper
1334 1.14.2.2 jdolecek #define sctp6_sendoob sctp6_sendoob_wrapper
1335 1.14.2.2 jdolecek #define sctp6_purgeif sctp6_purgeif_wrapper
1336 1.14.2.2 jdolecek
1337 1.14.2.2 jdolecek const struct pr_usrreqs sctp6_usrreqs = {
1338 1.14.2.2 jdolecek .pr_attach = sctp6_attach,
1339 1.14.2.2 jdolecek .pr_detach = sctp6_detach,
1340 1.14.2.2 jdolecek .pr_accept = sctp6_accept,
1341 1.14.2.2 jdolecek .pr_bind = sctp6_bind,
1342 1.14.2.2 jdolecek .pr_listen = sctp6_listen,
1343 1.14.2.2 jdolecek .pr_connect = sctp6_connect,
1344 1.14.2.2 jdolecek .pr_connect2 = sctp6_connect2,
1345 1.14.2.2 jdolecek .pr_disconnect = sctp6_disconnect,
1346 1.14.2.2 jdolecek .pr_shutdown = sctp6_shutdown,
1347 1.14.2.2 jdolecek .pr_abort = sctp6_abort,
1348 1.14.2.2 jdolecek .pr_ioctl = sctp6_ioctl,
1349 1.14.2.2 jdolecek .pr_stat = sctp6_stat,
1350 1.14.2.2 jdolecek .pr_peeraddr = sctp6_peeraddr,
1351 1.14.2.2 jdolecek .pr_sockaddr = sctp6_sockaddr,
1352 1.14.2.2 jdolecek .pr_rcvd = sctp6_rcvd,
1353 1.14.2.2 jdolecek .pr_recvoob = sctp6_recvoob,
1354 1.14.2.2 jdolecek .pr_send = sctp6_send,
1355 1.14.2.2 jdolecek .pr_sendoob = sctp6_sendoob,
1356 1.14.2.2 jdolecek .pr_purgeif = sctp6_purgeif,
1357 1.14.2.2 jdolecek };
1358